From ecc0033b54847f6c9ee37dbb0be8aa17e5b6d37b Mon Sep 17 00:00:00 2001 From: Loo Rong Jie Date: Tue, 22 Oct 2019 08:34:46 +0800 Subject: Always enable proper symbolize implementation on Windows (#257) --- absl/debugging/BUILD.bazel | 10 ++++++++-- absl/debugging/CMakeLists.txt | 3 +++ absl/debugging/symbolize.cc | 9 +++------ absl/debugging/symbolize_test.cc | 35 +++++++++++++++++++++++++++++------ absl/debugging/symbolize_win32.inc | 5 ++--- 5 files changed, 45 insertions(+), 17 deletions(-) (limited to 'absl') diff --git a/absl/debugging/BUILD.bazel b/absl/debugging/BUILD.bazel index b87c55a..beec279 100644 --- a/absl/debugging/BUILD.bazel +++ b/absl/debugging/BUILD.bazel @@ -70,8 +70,14 @@ cc_library( cc_test( name = "symbolize_test", srcs = ["symbolize_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, + copts = ABSL_TEST_COPTS + select({ + "//absl:windows": ["/Z7"], + "//conditions:default": [], + }), + linkopts = ABSL_DEFAULT_LINKOPTS + select({ + "//absl:windows": ["/DEBUG"], + "//conditions:default": [], + }), deps = [ ":stack_consumption", ":symbolize", diff --git a/absl/debugging/CMakeLists.txt b/absl/debugging/CMakeLists.txt index 81492c0..b3e35b6 100644 --- a/absl/debugging/CMakeLists.txt +++ b/absl/debugging/CMakeLists.txt @@ -62,6 +62,9 @@ absl_cc_test( "symbolize_test.cc" COPTS ${ABSL_TEST_COPTS} + $<$:-Z7> + LINKOPTS + $<$:-DEBUG> DEPS absl::stack_consumption absl::symbolize diff --git a/absl/debugging/symbolize.cc b/absl/debugging/symbolize.cc index 24e3a7f..54ed970 100644 --- a/absl/debugging/symbolize.cc +++ b/absl/debugging/symbolize.cc @@ -16,12 +16,9 @@ #if defined(ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE) #include "absl/debugging/symbolize_elf.inc" -#elif defined(_WIN32) && defined(_DEBUG) -// The Windows Symbolizer only works in debug mode. Note that _DEBUG -// is the macro that defines whether or not MS C-Runtime debug info is -// available. Note that the PDB files containing the debug info must -// also be available to the program at runtime for the symbolizer to -// work. +#elif defined(_WIN32) +// The Windows Symbolizer only works if PDB files containing the debug info +// are available to the program at runtime. #include "absl/debugging/symbolize_win32.inc" #else #include "absl/debugging/symbolize_unimplemented.inc" diff --git a/absl/debugging/symbolize_test.cc b/absl/debugging/symbolize_test.cc index 533799a..0bac60b 100644 --- a/absl/debugging/symbolize_test.cc +++ b/absl/debugging/symbolize_test.cc @@ -35,10 +35,29 @@ using testing::Contains; +#ifdef _WIN32 +#define ABSL_SYMBOLIZE_TEST_NOINLINE __declspec(noinline) +#else +#define ABSL_SYMBOLIZE_TEST_NOINLINE ABSL_ATTRIBUTE_NOINLINE +#endif + // Functions to symbolize. Use C linkage to avoid mangled names. extern "C" { -void nonstatic_func() { ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); } -static void static_func() { ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); } +ABSL_SYMBOLIZE_TEST_NOINLINE void nonstatic_func() { + // The next line makes this a unique function to prevent the compiler from + // folding identical functions together. + volatile int x = __LINE__; + static_cast(x); + ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); +} + +ABSL_SYMBOLIZE_TEST_NOINLINE static void static_func() { + // The next line makes this a unique function to prevent the compiler from + // folding identical functions together. + volatile int x = __LINE__; + static_cast(x); + ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); +} } // extern "C" struct Foo { @@ -46,7 +65,11 @@ struct Foo { }; // A C++ method that should have a mangled name. -void ABSL_ATTRIBUTE_NOINLINE Foo::func(int) { +ABSL_SYMBOLIZE_TEST_NOINLINE void Foo::func(int) { + // The next line makes this a unique function to prevent the compiler from + // folding identical functions together. + volatile int x = __LINE__; + static_cast(x); ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); } @@ -449,14 +472,14 @@ void ABSL_ATTRIBUTE_NOINLINE TestWithReturnAddress() { #endif } -#elif defined(_WIN32) && defined(_DEBUG) +#elif defined(_WIN32) TEST(Symbolize, Basics) { EXPECT_STREQ("nonstatic_func", TrySymbolize((void *)(&nonstatic_func))); // The name of an internal linkage symbol is not specified; allow either a // mangled or an unmangled name here. - const char* static_func_symbol = TrySymbolize((void *)(&static_func)); + const char *static_func_symbol = TrySymbolize((void *)(&static_func)); ASSERT_TRUE(static_func_symbol != nullptr); EXPECT_TRUE(strstr(static_func_symbol, "static_func") != nullptr); @@ -483,7 +506,7 @@ TEST(Symbolize, Truncation) { } TEST(Symbolize, SymbolizeWithDemangling) { - const char* result = TrySymbolize((void *)(&Foo::func)); + const char *result = TrySymbolize((void *)(&Foo::func)); ASSERT_TRUE(result != nullptr); EXPECT_TRUE(strstr(result, "Foo::func") != nullptr) << result; } diff --git a/absl/debugging/symbolize_win32.inc b/absl/debugging/symbolize_win32.inc index 5a55f29..0fb4b80 100644 --- a/absl/debugging/symbolize_win32.inc +++ b/absl/debugging/symbolize_win32.inc @@ -57,9 +57,8 @@ bool Symbolize(const void *pc, char *out, int out_size) { if (out_size <= 0) { return false; } - std::aligned_storage::type buf; - SYMBOL_INFO *symbol = reinterpret_cast(&buf); + alignas(SYMBOL_INFO) char buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME]; + SYMBOL_INFO *symbol = reinterpret_cast(buf); symbol->SizeOfStruct = sizeof(SYMBOL_INFO); symbol->MaxNameLen = MAX_SYM_NAME; if (!SymFromAddr(process, reinterpret_cast(pc), nullptr, symbol)) { -- cgit v1.2.3