fix memory leak
[model-checker.git] / stacktrace.h
1 // stacktrace.h (c) 2008, Timo Bingmann from http://idlebox.net/
2 // published under the WTFPL v2.0
3
4 #ifndef __STACKTRACE_H__
5 #define __STACKTRACE_H__
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <execinfo.h>
10 #include <cxxabi.h>
11
12 /** Print a demangled stack backtrace of the caller function to FILE* out. */
13 static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames = 63)
14 {
15         fprintf(out, "stack trace:\n");
16
17         // storage array for stack trace address data
18         void* addrlist[max_frames+1];
19
20         // retrieve current stack addresses
21         int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
22
23         if (addrlen == 0) {
24                 fprintf(out, "  <empty, possibly corrupt>\n");
25                 return;
26         }
27
28         // resolve addresses into strings containing "filename(function+address)",
29         // this array must be free()-ed
30         char** symbollist = backtrace_symbols(addrlist, addrlen);
31
32         // allocate string which will be filled with the demangled function name
33         size_t funcnamesize = 256;
34         char* funcname = (char*)malloc(funcnamesize);
35
36         // iterate over the returned symbol lines. skip the first, it is the
37         // address of this function.
38         for (int i = 1; i < addrlen; i++) {
39                 char *begin_name = 0, *begin_offset = 0, *end_offset = 0;
40
41                 // find parentheses and +address offset surrounding the mangled name:
42                 // ./module(function+0x15c) [0x8048a6d]
43                 for (char *p = symbollist[i]; *p; ++p) {
44                         if (*p == '(')
45                                 begin_name = p;
46                         else if (*p == '+')
47                                 begin_offset = p;
48                         else if (*p == ')' && begin_offset) {
49                                 end_offset = p;
50                                 break;
51                         }
52                 }
53
54                 if (begin_name && begin_offset && end_offset && begin_name < begin_offset) {
55                         *begin_name++ = '\0';
56                         *begin_offset++ = '\0';
57                         *end_offset = '\0';
58
59                         // mangled name is now in [begin_name, begin_offset) and caller
60                         // offset in [begin_offset, end_offset). now apply
61                         // __cxa_demangle():
62
63                         int status;
64                         char* ret = abi::__cxa_demangle(begin_name,
65                                         funcname, &funcnamesize, &status);
66                         if (status == 0) {
67                                 funcname = ret; // use possibly realloc()-ed string
68                                 fprintf(out, "  %s : %s+%s\n",
69                                                 symbollist[i], funcname, begin_offset);
70                         } else {
71                                 // demangling failed. Output function name as a C function with
72                                 // no arguments.
73                                 fprintf(out, "  %s : %s()+%s\n",
74                                                 symbollist[i], begin_name, begin_offset);
75                         }
76                 } else {
77                         // couldn't parse the line? print the whole line.
78                         fprintf(out, "  %s\n", symbollist[i]);
79                 }
80         }
81
82         free(funcname);
83         free(symbollist);
84 }
85
86 #endif // __STACKTRACE_H__