forked from augcampos/asterisk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandler.cpp
More file actions
132 lines (110 loc) · 4.13 KB
/
ExceptionHandler.cpp
File metadata and controls
132 lines (110 loc) · 4.13 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
/*
* ExceptionHandler.cpp
*
* Created on: 29 de Jun de 2011
* Author: augcampos
*/
#include "asteriskcpp/exceptions/ExceptionHandler.h"
#include "asteriskcpp/utils/LogHandler.h"
namespace asteriskcpp {
ExceptionHandler* ExceptionHandler::exceptionHandlerPtr = NULL;
ExceptionHandler::~ExceptionHandler() {
}
void ExceptionHandler::createExceptionHandler() {
if (!ExceptionHandler::exceptionHandlerPtr)
ExceptionHandler::exceptionHandlerPtr = new ExceptionHandler();
}
std::string ExceptionHandler::getStackTraceString() {
if (!ExceptionHandler::exceptionHandlerPtr)
ExceptionHandler::createExceptionHandler();
std::stringstream stream;
#ifdef _WIN32
#else
void* addrList[MAX_STACKTRACE_FRAMES + 1];
int addrLen = backtrace(addrList, sizeof (addrList) / sizeof (void*));
if (addrLen == 0) {
stream << "Stack empty: Possibly corrupted..." << std::endl;
} else {
int status;
char* endOffset;
char* beginName;
char* beginOffset;
size_t funcNameSize = MAX_STACKTRACE_NAME;
char* funcName = (char*) malloc(MAX_STACKTRACE_NAME);
char** symbolList = backtrace_symbols(addrList, addrLen);
for (int i = addrLen - 3; i > 1; i--) {
beginName = NULL;
beginOffset = NULL;
endOffset = NULL;
for (char *p = symbolList[i]; *p; ++p) {
if (*p == '(')
beginName = p;
else if (*p == '+')
beginOffset = p;
else if (*p == ')' && beginOffset) {
endOffset = p;
break;
}
}
if (beginName && beginOffset && endOffset && beginName < beginOffset) {
*beginName++ = '\0';
*beginOffset++ = '\0';
*endOffset = '\0';
char* newFuncName = abi::__cxa_demangle(beginName, funcName, &funcNameSize, &status);
if (status == 0) {
funcName = newFuncName;
stream << "[" << ((addrLen - i) - 3) << "] " << symbolList[i] << " : " << funcName << "+" << beginOffset << std::endl;
} else {
stream << "[" << ((addrLen - i) - 3) << "] " << symbolList[i] << " : " << beginName << "()+" << beginOffset << std::endl;
}
} else {
stream << "[" << ((addrLen - i) - 3) << "] " << symbolList[i] << std::endl;
}
}
free(funcName);
free(symbolList);
}
#endif
std::string result = stream.str();
return (result);
}
void ExceptionHandler::destroyExceptionHandler() {
if (ExceptionHandler::exceptionHandlerPtr)
delete ExceptionHandler::exceptionHandlerPtr;
ExceptionHandler::exceptionHandlerPtr = NULL;
}
ExceptionHandler::ExceptionHandler() {
#ifdef _WIN32
#else
struct sigaction newh, oldh;
sigset_t sMask;
sigemptyset(&sMask);
newh.sa_mask = sMask;
newh.sa_flags = SA_RESTART | SA_NOMASK;
newh.sa_handler = &ExceptionHandler::signalHandler;
if (sigaction(SIGSEGV, &newh, &oldh) < 0) {
LOG_ERROR_STR("Failed to install signal handler for SIGSEGV");
exit(0);
}
#endif
}
void ExceptionHandler::signalHandler(int signal) {
#ifdef _WIN32
#else
switch (signal) {
case SIGSEGV:
{
LOG_ERROR_STR(ExceptionHandler::getStackTraceString());
LOG_ERROR_STR("Segmentation fault");
exit(0);
}
break;
default:
{
LOG_ERROR_DATA("Expected: " << SIGSEGV << ", got: " << signal << ", returning");
}
break;
}
#endif
}
}