-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegvcatch.cpp
More file actions
173 lines (139 loc) · 3.31 KB
/
segvcatch.cpp
File metadata and controls
173 lines (139 loc) · 3.31 KB
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
/***************************************************************************
* Copyright (C) 2009 by VisualData *
* *
* Redistributed under LGPL license terms. *
***************************************************************************/
#include "segvcatch/segvcatch.h"
#include <string>
#include <stdexcept>
using namespace std;
namespace
{
segvcatch::handler handler_segv = 0;
segvcatch::handler handler_fpe = 0;
#if defined __GNUC__ && __linux
#ifdef __i386__
#include "segvcatch/i386-signal.h"
#endif /*__i386__*/
#ifdef __x86_64__
#include "segvcatch/x86_64-signal.h"
#endif /*__x86_64__*/
#endif /*defined __GNUC__ && __linux*/
void default_segv()
{
// throw std::runtime_error("Segmentation fault");
asm(
"pushq %%r12\n"
"\tmovl $4, %%edi\n"
"\tpushq %%rbp\n"
"\tpushq %%rax\n"
"\tcall __cxa_allocate_exception@PLT\n"
"\tmovq %%rax, %%rbp\n"
"\tmovl $1, (%%rax)\n"
"\txorl %%edx, %%edx\n"
"\tleaq _ZTIi(%%rip), %%rsi\n"
"\tmovq %%rax, %%rdi\n"
"\tcall __cxa_throw@PLT\n"
"\tmovq %%rax, %%r12\n"
"\tmovq %%rbp, %%rdi\n"
"\tcall __cxa_free_exception@PLT\n"
"\tmovq %%r12, %%rdi\n"
"\tcall _Unwind_Resume@PLT\n"::
);
}
void default_fpe()
{
throw std::runtime_error("Floating-point exception");
}
void handle_segv()
{
if (handler_segv)
handler_segv();
}
void handle_fpe()
{
if (handler_fpe)
handler_fpe();
}
#if defined (HANDLE_SEGV) || defined(HANDLE_FPE)
#include <execinfo.h>
/* Unblock a signal. Unless we do this, the signal may only be sent
once. */
static void unblock_signal(int signum __attribute__((__unused__)))
{
#ifdef _POSIX_VERSION
sigset_t sigs;
sigemptyset(&sigs);
sigaddset(&sigs, signum);
sigprocmask(SIG_UNBLOCK, &sigs, NULL);
#endif
}
#endif
#ifdef HANDLE_SEGV
SIGNAL_HANDLER(catch_segv)
{
unblock_signal(SIGSEGV);
MAKE_THROW_FRAME(nullp);
handle_segv();
}
#endif
#ifdef HANDLE_FPE
SIGNAL_HANDLER(catch_fpe)
{
unblock_signal(SIGFPE);
#ifdef HANDLE_DIVIDE_OVERFLOW
HANDLE_DIVIDE_OVERFLOW;
#else
MAKE_THROW_FRAME(arithexception);
#endif
handle_fpe();
}
#endif
#ifdef WIN32
#include <windows.h>
static LONG CALLBACK win32_exception_handler(LPEXCEPTION_POINTERS e)
{
if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
{
handle_segv();
return EXCEPTION_CONTINUE_EXECUTION;
}
else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
{
handle_fpe();
return EXCEPTION_CONTINUE_EXECUTION;
}
else
return EXCEPTION_CONTINUE_SEARCH;
}
#endif
}
namespace segvcatch
{
void init_segv(handler h)
{
if (h)
handler_segv = h;
else
handler_segv = default_segv;
#ifdef HANDLE_SEGV
INIT_SEGV;
#endif
#ifdef WIN32
SetUnhandledExceptionFilter(win32_exception_handler);
#endif
}
void init_fpe(handler h)
{
if (h)
handler_fpe = h;
else
handler_fpe = default_fpe;
#ifdef HANDLE_FPE
INIT_FPE;
#endif
#ifdef WIN32
SetUnhandledExceptionFilter(win32_exception_handler);
#endif
}
}