#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <cxxabi.h>
#include <vector>
Go to the source code of this file.
◆ print_stacktrace()
static void print_stacktrace |
( |
FILE * |
out = stderr , |
|
|
unsigned int |
max_frames = 63 |
|
) |
| |
|
inlinestatic |
Print a demangled stack backtrace of the caller function to FILE* out.
Definition at line 15 of file stacktrace.h.
17 fprintf(out,
"Stack trace:\n");
20 std::vector<void *> addrlist(max_frames + 1);
23 int addrlen = backtrace(addrlist.data(), addrlist.size());
27 fprintf(out,
" <empty, possibly corrupt>\n");
33 char * *symbollist = backtrace_symbols(addrlist.data(), addrlen);
36 size_t funcnamesize = 256;
37 char *funcname =
static_cast<char *
>(malloc(funcnamesize));
39 int functionNamesFound = 0;
42 for (
int i = 2; i < addrlen; i++)
44 char *begin_name = 0, *begin_offset = 0, *end_offset = 0;
49 for (
char *p = symbollist[i]; *p; ++p)
59 else if ((*p ==
')') && begin_offset)
66 if (begin_name && begin_offset && end_offset
67 && (begin_name < begin_offset))
70 *begin_offset++ =
'\0';
78 char *ret = abi::__cxa_demangle(begin_name,
79 funcname, &funcnamesize, &status);
83 fprintf(out,
" %s : %s+%s %s\n",
84 symbollist[i], funcname, begin_offset, ++end_offset);
90 fprintf(out,
" %s : %s()+%s %s\n",
91 symbollist[i], begin_name, begin_offset, ++end_offset);
98 fprintf(out,
" %s\n", symbollist[i]);
102 if (!functionNamesFound)
104 fprintf(out,
"There were no function names found in the stack trace\n."
105 "Seems like debug symbols are not installed, and the stack trace is useless.\n");
107 if (functionNamesFound < addrlen - 2)
109 fprintf(out,
"Consider installing debug symbols for packages containing files with empty"
110 " function names (i.e. empty braces \"()\") to make your stack trace more useful\n");