From 3e686e0ba5e1aa4b621f5e34399d07ca95950cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 10 Jul 2019 10:48:26 +0300 Subject: [PATCH] Extend stacktrace printing There was a case that wasn't handled which would happen with executables. Also removed the path truncation to show where the library was loaded from. --- maxutils/maxbase/src/stacktrace.cc | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/maxutils/maxbase/src/stacktrace.cc b/maxutils/maxbase/src/stacktrace.cc index 8f52709a3..11d352153 100644 --- a/maxutils/maxbase/src/stacktrace.cc +++ b/maxutils/maxbase/src/stacktrace.cc @@ -71,7 +71,7 @@ static void extract_file_and_line(char* symbols, char* cmd, size_t size) const char* symname_start = filename_end + 1; - if (*symname_start != '+') + if (*symname_start != '+' && symname_start != symname_end) { // We have a string form symbol name and an offset, we need to // extract the symbol address @@ -111,6 +111,17 @@ static void extract_file_and_line(char* symbols, char* cmd, size_t size) } else { + if (symname_start == symname_end) + { + // Symbol is of the format `./executable [0xdeadbeef]` + if (!(symname_start = strchr(symname_start, '[')) + || !(symname_end = strchr(symname_start, ']'))) + { + snprintf(cmd, size, "Unexpected symbol format"); + return; + } + } + // Raw offset into library symname_start++; snprintf(symname, sizeof(symname), "%.*s", (int)(symname_end - symname_start), symname_start); @@ -126,13 +137,6 @@ static void extract_file_and_line(char* symbols, char* cmd, size_t size) memmove(cmd, str, strlen(cmd) - (str - cmd) + 1); } - // Strip the directory name from the symbols (could this be useful?) - if (char* str = strrchr(symbols, '/')) - { - ++str; - memmove(symbols, str, strlen(symbols) - (str - symbols) + 1); - } - // Remove the address where the symbol is in memory (i.e. the [0xdeadbeef] that follows the // (main+0xa1) part), we're only interested where it is in the library. if (char* str = strchr(symbols, '['))