Browse Source

Demangle system::getStackTrace() symbols on Linux.

tags/v2.0.6
Andrew Belt 2 years ago
parent
commit
d9cdc1e3bc
1 changed files with 14 additions and 7 deletions
  1. +14
    -7
      src/system.cpp

+ 14
- 7
src/system.cpp View File

@@ -5,7 +5,7 @@


#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <cxxabi.h> // for __cxxabiv1::__cxa_demangle
#include <cxxabi.h> // for abi::__cxa_demangle


#if defined ARCH_LIN || defined ARCH_MAC #if defined ARCH_LIN || defined ARCH_MAC
#include <pthread.h> #include <pthread.h>
@@ -631,28 +631,31 @@ void setThreadName(const std::string& name) {




std::string getStackTrace() { std::string getStackTrace() {
int stackLen = 128;
void* stack[stackLen];
void* stack[128];
int stackLen = LENGTHOF(stack);
std::string s; std::string s;


#if defined ARCH_LIN || defined ARCH_MAC #if defined ARCH_LIN || defined ARCH_MAC
stackLen = backtrace(stack, stackLen); stackLen = backtrace(stack, stackLen);
char** strings = backtrace_symbols(stack, stackLen); char** strings = backtrace_symbols(stack, stackLen);
if (!strings)
return "";


// Skip the first line because it's this function. // Skip the first line because it's this function.
for (int i = 1; i < stackLen; i++) { for (int i = 1; i < stackLen; i++) {
s += string::f("%d: ", stackLen - i - 1); s += string::f("%d: ", stackLen - i - 1);
std::string line = strings[i]; std::string line = strings[i];
#if 0
// Parse line
#if ARCH_LIN
// Parse line, e.g.
// ./main(__mangled_symbol+0x100) [0x12345678]
std::regex r(R"((.*)\((.*)\+(.*)\) (.*))"); std::regex r(R"((.*)\((.*)\+(.*)\) (.*))");
std::smatch match; std::smatch match;
if (std::regex_search(line, match, r)) {
if (std::regex_match(line, match, r)) {
s += match[1].str(); s += match[1].str();
s += "("; s += "(";
std::string symbol = match[2].str(); std::string symbol = match[2].str();
// Demangle symbol // Demangle symbol
char* symbolD = __cxxabiv1::__cxa_demangle(symbol.c_str(), NULL, NULL, NULL);
char* symbolD = abi::__cxa_demangle(symbol.c_str(), NULL, NULL, NULL);
if (symbolD) { if (symbolD) {
symbol = symbolD; symbol = symbolD;
free(symbolD); free(symbolD);
@@ -662,6 +665,10 @@ std::string getStackTrace() {
s += match[3].str(); s += match[3].str();
s += ")"; s += ")";
} }
else {
// If regex fails, just use the raw line
s += line;
}
#else #else
s += line; s += line;
#endif #endif


Loading…
Cancel
Save