/
exception_handler.cc
207 lines (163 loc) · 5.46 KB
/
exception_handler.cc
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
197
198
199
200
201
202
203
204
205
206
207
/* exception_handler.cc
Jeremy Barnes, 26 February 2008
Copyright (c) 2009 Jeremy Barnes. All rights reserved.
*/
#include <cxxabi.h>
#include <cstring>
#include <fstream>
#include "jml/compiler/compiler.h"
#include "jml/utils/environment.h"
#include "backtrace.h"
#include "demangle.h"
#include "exception.h"
#include "exception_hook.h"
#include "format.h"
#include "threads.h"
using namespace std;
namespace ML {
Env_Option<bool> TRACE_EXCEPTIONS("JML_TRACE_EXCEPTIONS", true);
__thread bool trace_exceptions = false;
__thread bool trace_exceptions_initialized = false;
void set_default_trace_exceptions(bool val)
{
TRACE_EXCEPTIONS.set(val);
}
bool get_default_trace_exceptions()
{
return TRACE_EXCEPTIONS;
}
void set_trace_exceptions(bool trace)
{
//cerr << "set_trace_exceptions to " << trace << " at " << &trace_exceptions
// << endl;
trace_exceptions = trace;
trace_exceptions_initialized = true;
}
bool get_trace_exceptions()
{
if (!trace_exceptions_initialized) {
//cerr << "trace_exceptions initialized to = "
// << trace_exceptions << " at " << &trace_exceptions << endl;
set_trace_exceptions(TRACE_EXCEPTIONS);
trace_exceptions_initialized = true;
}
//cerr << "get_trace_exceptions returned " << trace_exceptions
// << " at " << &trace_exceptions << endl;
return trace_exceptions;
}
static const std::exception *
to_std_exception(void* object, const std::type_info * tinfo)
{
/* Check if its a class. If not, we can't see if it's a std::exception.
The abi::__class_type_info is the base class of all types of type
info for types that are classes (of which std::exception is one).
*/
const abi::__class_type_info * ctinfo
= dynamic_cast<const abi::__class_type_info *>(tinfo);
if (!ctinfo) return 0;
/* The thing thrown was an object. Now, check if it is derived from
std::exception. */
const std::type_info * etinfo = &typeid(std::exception);
/* See if the exception could catch this. This is the mechanism
used internally by the compiler in catch {} blocks to see if
the exception matches the catch type.
In the case of success, the object will be adjusted to point to
the start of the std::exception object.
*/
void * obj_ptr = object;
bool can_catch = etinfo->__do_catch(tinfo, &obj_ptr, 0);
if (!can_catch) return 0;
/* obj_ptr points to a std::exception; extract it and get the
exception message.
*/
return (const std::exception *)obj_ptr;
}
/** We install this handler for when an exception is thrown. */
void default_exception_tracer(void * object, const std::type_info * tinfo)
{
//cerr << "trace_exception: trace_exceptions = " << get_trace_exceptions()
// << " at " << &trace_exceptions << endl;
if (!get_trace_exceptions()) return;
const std::exception * exc = to_std_exception(object, tinfo);
// We don't want these exceptions to be printed out.
if (dynamic_cast<const ML::SilentException *>(exc)) return;
/* avoid allocations when std::bad_alloc is thrown */
bool noAlloc = dynamic_cast<const std::bad_alloc *>(exc);
size_t bufferSize(1024*1024);
char buffer[bufferSize];
char datetime[128];
size_t totalWritten(0), written, remaining(bufferSize);
time_t now;
time(&now);
struct tm lt_tm;
strftime(datetime, sizeof(datetime), "%FT%H:%M:%SZ",
gmtime_r(&now, <_tm));
const char * demangled;
char * heapDemangled;
if (noAlloc) {
heapDemangled = nullptr;
demangled = "std::bad_alloc";
}
else {
heapDemangled = char_demangle(tinfo->name());
demangled = heapDemangled;
}
auto pid = getpid();
auto tid = gettid();
written = ::snprintf(buffer, remaining,
"\n"
"--------------------------[Exception thrown]"
"---------------------------\n"
"time: %s\n"
"type: %s\n"
"pid: %d; tid: %d\n",
datetime, demangled, pid, tid);
if (heapDemangled) {
free(heapDemangled);
}
if (written >= remaining) {
goto end;
}
totalWritten += written;
remaining -= written;
if (exc) {
written = snprintf(buffer + totalWritten, remaining,
"what: %s\n", exc->what());
if (written >= remaining) {
goto end;
}
totalWritten += written;
remaining -= written;
}
if (noAlloc) {
goto end;
}
written = snprintf(buffer + totalWritten, remaining, "stack:\n");
if (written >= remaining) {
goto end;
}
totalWritten += written;
remaining -= written;
written = backtrace(buffer + totalWritten, remaining, 3);
if (written >= remaining) {
goto end;
}
totalWritten += written;
if (totalWritten < bufferSize - 1) {
strcpy(buffer + totalWritten, "\n");
}
end:
cerr << buffer;
char const * reports = getenv("ENABLE_EXCEPTION_REPORTS");
if (!noAlloc && reports) {
std::string path = ML::format("%s/exception-report-%s-%d-%d.log",
reports, datetime, pid, tid);
std::ofstream file(path, std::ios_base::app);
if(file) {
file << getenv("_") << endl;
backtrace(file, 3);
file.close();
}
}
}
} // namespace ML