-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_stacktrace_on_exception.cpp
executable file
·196 lines (166 loc) · 5.98 KB
/
get_stacktrace_on_exception.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// from https://web.archive.org/web/20150706050621/http://blog.sjinks.pro/c-cpp/969-track-uncaught-exceptions
// credit Опубликовано Vladimir (?)
#include <typeinfo>
#include <exception>
#include <dlfcn.h>
#include <pthread.h>
#include <cstdio>
#include <cstdlib>
#include <inttypes.h>
#include <execinfo.h>
#include <cxxabi.h> // Defines types from namespace abi
#include <cstring>
#include <stdexcept>
#include <unistd.h>
namespace { // Important: without an anonymous namespace, gcc starts to go crazy
// Data types / structures from http://sourcery.mentor.com/public/cxx-abi/abi-eh.html
typedef enum {
_URC_NO_REASON = 0,
_URC_FOREIGN_EXCEPTION_CAUGHT = 1,
_URC_FATAL_PHASE2_ERROR = 2,
_URC_FATAL_PHASE1_ERROR = 3,
_URC_NORMAL_STOP = 4,
_URC_END_OF_STACK = 5,
_URC_HANDLER_FOUND = 6,
_URC_INSTALL_CONTEXT = 7,
_URC_CONTINUE_UNWIND = 8
} _Unwind_Reason_Code;
typedef void (*_Unwind_Exception_Cleanup_Fn)(_Unwind_Reason_Code reason, struct _Unwind_Exception* exc);
struct _Unwind_Exception {
uint64_t exception_class;
_Unwind_Exception_Cleanup_Fn exception_cleanup;
uint64_t private_1;
uint64_t private_2;
};
struct __cxa_exception {
std::type_info* exceptionType;
void (*exceptionDestructor)(void*);
std::unexpected_handler unexpectedHandler;
std::terminate_handler terminateHandler;
__cxa_exception* nextException;
int handlerCount;
int handlerSwitchValue;
const char* actionRecord;
const char* languageSpecificData;
void* catchTemp;
void* adjustedPtr;
_Unwind_Exception unwindHeader;
};
struct __cxa_eh_globals {
__cxa_exception* caughtExceptions;
unsigned int uncaughtExceptions;
};
extern "C" __cxa_eh_globals* __cxa_get_globals(void);
// Function type __cxa_throw
typedef void(*cxa_throw_type)(void*, std::type_info*, void(*)(void*));
// Function type __cxa_rethrow
typedef void(*cxa_rethrow_type)(void);
static cxa_throw_type orig_cxa_throw = 0; // Address of the original function __cxa_throw
static cxa_rethrow_type orig_cxa_rethrow = 0; // Address of the original function __cxa_rethrow
static pthread_mutex_t guard = PTHREAD_MUTEX_INITIALIZER; // Mutex for every fireman
// Get call trace
// Hereinafter, functions from the C standard library are deliberately used, since they do not throw exceptions
// An exception in a low-level exception handler is something
static void get_backtrace(void)
{
static void* buf[128];
int n = backtrace(buf, 128);
std::fprintf(stderr, "%s\n", "*** BACKTRACE ***");
backtrace_symbols_fd(buf, n, STDERR_FILENO);
// Code below assumes readlink and addr2line programs are installed in /usr/bin/
// the idea is to feed the addresses received by backtrace (3) to addr2line (1)
// in order to get a human-readable result
std::size_t bufsize = 19*n + std::strlen("/usr/bin/addr2line -pifCa -e `/bin/readlink /proc/XXXXX/exe` 1>&2") + 1;
char* space = reinterpret_cast<char*>(std::calloc(bufsize, 1));
if (space) {
char* orig = space;
int c = std::sprintf(space, "/usr/bin/addr2line -pifCa -e `/bin/readlink /proc/%d/exe` ", getpid());
space += c;
for (int i=0; i<n; ++i) {
c = std::sprintf(space, "%p ", buf[i]);
space += c;
}
std::fprintf(stderr, "%s\n", "\n*** DECODED BACKTRACE ***");
std::sprintf(space, "%s", "1>&2");
std::fprintf(stderr, "%s\n", orig);
if (std::system(orig)) {} // Чтобы не ругался gcc
std::free(orig);
}
}
// Exception handling common to __cxz_throw and __cxa_rethrow
static void handle_exception(void* thrown_exception, std::type_info* tinfo, bool rethrown)
{
char* demangled = abi:: __cxa_demangle(tinfo->name(), 0, 0, 0);
std::fprintf(stderr, "%s exception of type %s\n", (rethrown ? "Rethrown" : "Thrown"), (demangled ? demangled : tinfo->name()));
if (demangled) {
std::free(demangled);
}
const abi::__class_type_info* exc = dynamic_cast<const abi::__class_type_info*>(&typeid(std::exception));
const abi::__class_type_info* cti = dynamic_cast<abi::__class_type_info*>(tinfo);
if (cti && exc) {
std::exception* the_exception = reinterpret_cast<std::exception*>(abi::__dynamic_cast(thrown_exception, exc, cti, -1));
if (the_exception) {
std::fprintf(stderr, "what(): %s\n", the_exception->what());
}
}
get_backtrace();
std::fprintf(stderr, "\n\n");
}
extern "C" void __cxa_throw(void* thrown_exception, std::type_info* tinfo, void (*dest)(void*))
{
pthread_mutex_lock(&guard);
handle_exception(thrown_exception, tinfo, false);
pthread_mutex_unlock(&guard);
if (orig_cxa_throw) {
orig_cxa_throw(thrown_exception, tinfo, dest);
}
else {
std::terminate();
}
}
extern "C" void __cxa_rethrow(void)
{
pthread_mutex_lock(&guard);
__cxa_eh_globals* g = __cxa_get_globals();
if (g && g->caughtExceptions) {
void* thrown_exception = reinterpret_cast<uint8_t*>(g->caughtExceptions) + sizeof(struct __cxa_exception);
handle_exception(thrown_exception, g->caughtExceptions->exceptionType, true);
}
pthread_mutex_unlock(&guard);
if (orig_cxa_rethrow) {
orig_cxa_rethrow();
}
else {
std::terminate();
}
}
// Initialize variables. You can do this in the handler, but it's safer this way
static void initialize(void)
{
orig_cxa_throw = reinterpret_cast<cxa_throw_type>(dlsym(RTLD_NEXT, "__cxa_throw"));
orig_cxa_rethrow = reinterpret_cast<cxa_rethrow_type>(dlsym(RTLD_NEXT, "__cxa_rethrow"));
}
}
int main(int, char**)
{
initialize();
try {
try {
throw std::runtime_error("123");
}
catch (const std::exception& e) {
std::printf("e.what(): %s\n", e.what());
throw;
}
}
catch (const std::exception& d) {
std::printf("d.what(): %s\n", d.what());
}
try {
throw 1;
}
catch (int x) {
std::printf("%d\n", x);
}
return 0;
}