From 05e2e7b9c0dfa157f450d38d05a7e10fe3788d4f Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 13 Jul 2019 22:56:13 -0700 Subject: [PATCH] Reenable backtrace printing in log. Demangle symbols on Linux. --- src/main.cpp | 5 ++--- src/system.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 961e490b..94b54c0d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,6 @@ #include #include // for getopt #include // for signal - #if defined ARCH_WIN #include // for CreateMutex #endif @@ -40,7 +39,7 @@ static void fatalSignalHandler(int sig) { FATAL("Fatal signal %d. Stack trace:\n%s", sig, system::getStackTrace().c_str()); - // osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Rack has crashed. See log.txt for details."); + osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Rack has crashed. See log.txt for details."); exit(1); } @@ -99,7 +98,7 @@ int main(int argc, char *argv[]) { // We can now install a signal handler and log the output // Mac has its own decent crash handler -#if 0 +#if defined ARCH_LIN if (!settings::devMode) { signal(SIGABRT, fatalSignalHandler); signal(SIGFPE, fatalSignalHandler); diff --git a/src/system.cpp b/src/system.cpp index 29737277..d864964d 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -2,8 +2,10 @@ #include #include +#include #include #include +#include // for __cxxabiv1::__cxa_demangle #if defined ARCH_LIN || defined ARCH_MAC #include @@ -166,10 +168,32 @@ std::string getStackTrace() { stackLen = backtrace(stack, stackLen); char **strings = backtrace_symbols(stack, stackLen); + // Skip the first line because it's this function. for (int i = 1; i < stackLen; i++) { - s += string::f("%d: %s\n", stackLen - i - 1, strings[i]); + s += string::f("%d: ", stackLen - i - 1); + // Parse line + std::regex r(R"((.*)\((.*)\+(.*)\) (.*))"); + std::smatch match; + std::string line = strings[i]; + if (std::regex_search(line, match, r)) { + s += match[1].str(); + s += "("; + std::string symbol = match[2].str(); + // Demangle symbol + char *symbolD = __cxxabiv1::__cxa_demangle(symbol.c_str(), NULL, NULL, NULL); + if (symbolD) { + symbol = symbolD; + free(symbolD); + } + s += symbol; + s += "+"; + s += match[3].str(); + s += ")"; + } + s += "\n"; } free(strings); + #elif defined ARCH_WIN HANDLE process = GetCurrentProcess(); SymInitialize(process, NULL, true);