From 24ef590532290b74acb347ff5b394b9845cd1e32 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Wed, 20 Apr 2022 17:25:24 -0700 Subject: [PATCH] Call SymInitialize at process start as it is single threaded --- src/init.c | 23 ++++++++++++++++++++++- src/options.c | 11 ++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/init.c b/src/init.c index 6afe3d67..b441d661 100644 --- a/src/init.c +++ b/src/init.c @@ -527,6 +527,25 @@ static void mi_detect_cpu_features(void) { } #endif +#if defined(_WIN32) && (MI_DEBUG_TRACE > 0) +#include +static void mi_debug_init(void) { + if (SymInitialize(GetCurrentProcess(), NULL, TRUE) != TRUE) { // initialize here as it is single threaded. + _mi_warning_message("unable to initialize debug symbol information (error 0x%x)", GetLastError()); + } +} +static void mi_debug_done(void) { + SymCleanup(GetCurrentProcess()); +} +#else +static void mi_debug_init(void) { + // nothing +} +static void mi_debug_done(void) { + // nothing +} +#endif + // Initialize the process; called by thread_init or the process loader void mi_process_init(void) mi_attr_noexcept { // ensure we are called once @@ -543,6 +562,7 @@ void mi_process_init(void) mi_attr_noexcept { _mi_verbose_message("debug level : %d\n", MI_DEBUG); #endif _mi_verbose_message("secure level: %d\n", MI_SECURE); + mi_debug_init(); mi_thread_init(); #if defined(_WIN32) && !defined(MI_SHARED_LIB) @@ -551,7 +571,7 @@ void mi_process_init(void) mi_attr_noexcept { // will not call _mi_thread_done on the (still executing) main thread. See issue #508. FlsSetValue(mi_fls_key, NULL); #endif - + mi_stats_reset(); // only call stat reset *after* thread init (or the heap tld == NULL) if (mi_option_is_enabled(mi_option_reserve_huge_os_pages)) { @@ -597,6 +617,7 @@ static void mi_process_done(void) { mi_stats_print(NULL); } mi_allocator_done(); + mi_debug_done(); _mi_verbose_message("process done: 0x%zx\n", _mi_heap_main.thread_id); os_preloading = true; // don't call the C runtime anymore } diff --git a/src/options.c b/src/options.c index 5b6de7eb..eb24d9b3 100644 --- a/src/options.c +++ b/src/options.c @@ -424,21 +424,14 @@ void _mi_stack_trace_print(const char* msg, void** strace, size_t len, const mi_ info->MaxNameLen = 255; info->SizeOfStruct = sizeof(SYMBOL_INFO); HANDLE current_process = GetCurrentProcess(); - bool initialized = (SymInitialize(current_process, NULL, TRUE) == TRUE); for (size_t i = 0; i < len && strace[i] != NULL; i++) { - if (!initialized) { - _mi_fprintf(NULL, NULL, " %2zu: %8p: \n", i, strace[i]); - } - else if (SymFromAddr(current_process, (DWORD64)(strace[i]), 0, info)) { + if (SymFromAddr(current_process, (DWORD64)(strace[i]), 0, info)) { _mi_fprintf(NULL, NULL, " %2zu: %8p: %s\n", i, strace[i], info->Name); } else { _mi_fprintf(NULL, NULL, " %2zu: %8p: \n", i, strace[i], GetLastError()); } - } - if (initialized) { - SymCleanup(current_process); - } + } } } #elif (MI_DEBUG_TRACE > 0) && (defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__))