From 7417c5e9e2f4dbd3ae2c17512835f1d23e1b35fb Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 16 Jun 2015 18:49:56 +0100 Subject: [PATCH] Resolve symbol names in Cygwin Backtrace::desc implementation --- src/Makefile | 3 +++ src/backtrace.cc | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Makefile b/src/Makefile index 2945e536..9107794c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -15,6 +15,9 @@ os := $(shell uname) ifeq ($(os),Darwin) LIBS += -lncurses -lboost_regex-mt +else ifneq (,$(findstring CYGWIN,$(os))) + LIBS += -lncursesw -lboost_regex -ldbghelp + LDFLAGS += -rdynamic else LIBS += -lncursesw -lboost_regex LDFLAGS += -rdynamic diff --git a/src/backtrace.cc b/src/backtrace.cc index 7c195f67..adeb9301 100644 --- a/src/backtrace.cc +++ b/src/backtrace.cc @@ -7,6 +7,7 @@ # include #elif defined(__CYGWIN__) # include +# include # include #endif @@ -39,12 +40,27 @@ String Backtrace::desc() const free(symbols); return res; #elif defined(__CYGWIN__) - String res; res.reserve(num_frames * 20); + HANDLE process = GetCurrentProcess(); + + static bool symbols_initialized = false; + if (not symbols_initialized) + { + SymInitialize(process, nullptr, true); + symbols_initialized = true; + } + + alignas(SYMBOL_INFO) char symbol_info_buffer[sizeof(SYMBOL_INFO) + 256]; + SYMBOL_INFO* symbol_info = reinterpret_cast(symbol_info_buffer); + symbol_info->MaxNameLen = 255; + symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO); + + String res; // res.reserve(num_frames * 276); for (int i = 0; i < num_frames; ++i) { - char addr[20]; - snprintf(addr, 20, "0x%p\n", stackframes[i]); - res += addr; + SymFromAddr(process, (DWORD64)stackframes[i], 0, symbol_info); + char desc[276]; + snprintf(desc, 276, "0x%0llx (%s)\n", symbol_info->Address, symbol_info->Name); + res += desc; } return res; #else