21 #ifdef USE_UNIX_BACKTRACE_SUPPORT
31 void print_stacktrace(FILE* output,
int start_idx,
bool demangling,
int maxdepth,
bool omit_above_own)
34 #define ADDRESSDISPLAYLENGTH ((sizeof(long)==8)?12:8)
35 void *callstackArray[32]= {
nullptr};
36 const int currentdepth = backtrace(callstackArray, (
int)
getArrayLength(callstackArray));
38 int offset=start_idx+1;
40 maxdepth=currentdepth-offset;
42 maxdepth = std::min(maxdepth, currentdepth);
44 char **symbolStringList = backtrace_symbols(callstackArray, currentdepth);
45 if (!symbolStringList) {
46 fputs(
"Callstack could not be obtained\n", output);
50 fputs(
"Callstack:\n", output);
51 bool own_code =
false;
52 char demangle_buffer[2048]= {0};
53 for (
int i = offset; i < maxdepth; ++i) {
54 const char *
const symbolString = symbolStringList[i];
56 if (omit_above_own && !own_code) {
57 if (strstr(symbolString,
"/libc.so.6") !=
nullptr)
62 char * realnameString =
nullptr;
63 const char *
const firstBracketName = strchr(symbolString,
'(');
64 const char *
const firstBracketAddress = strchr(symbolString,
'[');
65 const char *
const secondBracketAddress = strchr(firstBracketAddress,
']');
66 const char *
const beginAddress = firstBracketAddress+3;
67 const int addressLen = int(secondBracketAddress-beginAddress);
68 const int padLen = int(ADDRESSDISPLAYLENGTH-addressLen);
69 if (demangling && firstBracketName) {
70 const char *
const plus = strchr(firstBracketName,
'+');
71 if (plus && (plus>(firstBracketName+1))) {
72 char input_buffer[1024]= {0};
73 strncpy(input_buffer, firstBracketName+1, plus-firstBracketName-1);
78 realnameString = abi::__cxa_demangle(input_buffer, demangle_buffer, &length, &status);
81 const int ordinal=i-offset;
82 fprintf(output,
"#%-2d 0x",
85 fprintf(output,
"%0*d",
88 fprintf(output,
"%.*s in %s\n",
89 (
int)(secondBracketAddress-firstBracketAddress-3), firstBracketAddress+3,
92 fprintf(output,
"%.*s in %.*s\n",
93 (
int)(secondBracketAddress-firstBracketAddress-3), firstBracketAddress+3,
94 (
int)(firstBracketAddress-symbolString), symbolString);
98 free(symbolStringList);
99 #undef ADDRESSDISPLAYLENGTH
std::size_t getArrayLength(const T(&)[size])
Simple helper function: