Remove special cases for printing SC executions.
[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 /**
13  * @brief Print a demangled stack backtrace of the caller function to file
14  * descriptor fd.
15  */
16 static inline void print_stacktrace(int fd = STDERR_FILENO, unsigned int max_frames = 63)
17 {
18         dprintf(fd, "stack trace:\n");
19
20         // storage array for stack trace address data
21         void* addrlist[max_frames+1];
22
23         // retrieve current stack addresses
24         int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
25
26         if (addrlen == 0) {
27                 dprintf(fd, "  <empty, possibly corrupt>\n");
28                 return;
29         }
30
31         // resolve addresses into strings containing "filename(function+address)",
32         // this array must be free()-ed
33         char** symbollist = backtrace_symbols(addrlist, addrlen);
34
35         // allocate string which will be filled with the demangled function name
36         size_t funcnamesize = 256;
37         char* funcname = (char*)malloc(funcnamesize);
38
39         // iterate over the returned symbol lines. skip the first, it is the
40         // address of this function.
41         for (int i = 1; i < addrlen; i++) {
42                 char *begin_name = 0, *begin_offset = 0, *end_offset = 0;
43
44                 // find parentheses and +address offset surrounding the mangled name:
45                 // ./module(function+0x15c) [0x8048a6d]
46                 for (char *p = symbollist[i]; *p; ++p) {
47                         if (*p == '(')
48                                 begin_name = p;
49                         else if (*p == '+')
50                                 begin_offset = p;
51                         else if (*p == ')' && begin_offset) {
52                                 end_offset = p;
53                                 break;
54                         }
55                 }
56
57                 if (begin_name && begin_offset && end_offset && begin_name < begin_offset) {
58                         *begin_name++ = '\0';
59                         *begin_offset++ = '\0';
60                         *end_offset = '\0';
61
62                         // mangled name is now in [begin_name, begin_offset) and caller
63                         // offset in [begin_offset, end_offset). now apply
64                         // __cxa_demangle():
65
66                         int status;
67                         char* ret = abi::__cxa_demangle(begin_name,
68                                         funcname, &funcnamesize, &status);
69                         if (status == 0) {
70                                 funcname = ret; // use possibly realloc()-ed string
71                                 dprintf(fd, "  %s : %s+%s\n",
72                                                 symbollist[i], funcname, begin_offset);
73                         } else {
74                                 // demangling failed. Output function name as a C function with
75                                 // no arguments.
76                                 dprintf(fd, "  %s : %s()+%s\n",
77                                                 symbollist[i], begin_name, begin_offset);
78                         }
79                 } else {
80                         // couldn't parse the line? print the whole line.
81                         dprintf(fd, "  %s\n", symbollist[i]);
82                 }
83         }
84
85         free(funcname);
86         free(symbollist);
87 }
88
89 static inline void print_stacktrace(FILE *out, unsigned int max_frames = 63)
90 {
91         print_stacktrace(fileno(out), max_frames);
92 }
93
94 #endif // __STACKTRACE_H__