13 static inline void print_stacktrace(FILE *out = stderr,
unsigned int max_frames = 63)
15 fprintf(out,
"stack trace:\n");
18 void* addrlist[max_frames+1];
21 int addrlen = backtrace(addrlist,
sizeof(addrlist) /
sizeof(
void*));
24 fprintf(out,
" <empty, possibly corrupt>\n");
30 char** symbollist = backtrace_symbols(addrlist, addrlen);
33 size_t funcnamesize = 256;
34 char* funcname = (
char*)malloc(funcnamesize);
38 for (
int i = 1;
i < addrlen;
i++)
40 char *begin_name = 0, *begin_offset = 0, *end_offset = 0;
44 for (
char *p = symbollist[
i]; *p; ++p)
50 else if (*p ==
')' && begin_offset) {
56 if (begin_name && begin_offset && end_offset
57 && begin_name < begin_offset)
60 *begin_offset++ =
'\0';
68 char* ret = abi::__cxa_demangle(begin_name,
69 funcname, &funcnamesize, &status);
72 fprintf(out,
" %s : %s+%s\n",
73 symbollist[
i], funcname, begin_offset);
78 fprintf(out,
" %s : %s()+%s\n",
79 symbollist[
i], begin_name, begin_offset);
85 fprintf(out,
" %s\n", symbollist[
i]);
93 #endif // _STACKTRACE_H_