-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathStackTraceUnix.cpp
More file actions
229 lines (194 loc) · 6.71 KB
/
Copy pathStackTraceUnix.cpp
File metadata and controls
229 lines (194 loc) · 6.71 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (C) 2011-2016 OpenDungeons Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils/StackTracePrint.h"
#include "utils/Helper.h"
#include "utils/LogManager.h"
#include <SFML/System.hpp>
#include <cxxabi.h>
#include <execinfo.h>
#include <unistd.h>
#include <signal.h>
#ifndef __OpenBSD__
#include <ucontext.h>
#endif
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
//! \brief This structure mirrors the one found in /usr/include/asm/ucontext.h
//! \note: Its members should stay same named.
typedef struct _sig_ucontext
{
unsigned long uc_flags;
struct ucontext* uc_link;
stack_t uc_stack;
struct sigcontext uc_mcontext;
sigset_t uc_sigmask;
} sig_ucontext_t;
class StackTracePrintPrivateData
{
public:
StackTracePrintPrivateData()
{
//Init the error hanlder used to get a full stacktrace when crashing
struct sigaction sigact;
std::memset(&sigact, 0, sizeof(sigact));
sigact.sa_sigaction = critErrHandler;
sigact.sa_flags = SA_RESTART | SA_SIGINFO;
if (sigaction(SIGSEGV, &sigact, nullptr) != 0)
{
std::cerr << "error setting signal handler for: "
<< SIGSEGV << strsignal(SIGSEGV) << std::endl;
exit(EXIT_FAILURE);
}
}
static void critErrHandler(int sig_num, siginfo_t* info, void* ucontext);
};
static sf::Mutex gMutex;
StackTracePrintPrivateData* StackTracePrint::mPrivateData = nullptr;
StackTracePrint::StackTracePrint(const std::string& crashFilePath)
{
sf::Lock lock(gMutex);
if(mPrivateData != nullptr)
throw std::exception();
mPrivateData = new StackTracePrintPrivateData;
}
StackTracePrint::~StackTracePrint()
{
if(mPrivateData != nullptr)
{
delete mPrivateData;
mPrivateData = nullptr;
}
}
void StackTracePrintPrivateData::critErrHandler(int sig_num, siginfo_t* info, void* ucontext)
{
// Prevent running a function that isn't supported on certain platforms.
#if not defined (__i386__) & not defined (__x86_64__)
std::cout << "No error handler supported for this platform" << std::endl;
return;
#endif
// We print the callstack in a stringstream. Then, we will print it to the crashStream
// and, then, to the log manager
std::stringstream crashStream;
void* array[50];
void* caller_address;
sig_ucontext_t* uc = static_cast<sig_ucontext_t*>(ucontext);
#if defined(__i386__) // gcc specific
#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
caller_address = reinterpret_cast<void*>(uc->uc_mcontext.eip); // EIP: x86 specific
#else
caller_address = reinterpret_cast<void*>(uc->uc_mcontext.sc_eip); // EIP: x86 specific
#endif
#elif defined(__x86_64__) // gcc specific
#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
caller_address = reinterpret_cast<void*>(uc->uc_mcontext.rip); // RIP: x86_64 specific
#else
caller_address = reinterpret_cast<void*>(uc->uc_mcontext.sc_rip); // RIP: x86_64 specific
#endif
#else
#warning Unsupported architecture. // TODO: Add support for other arch.
#endif
// void* caller_address = (void*) uc->uc_mcontext.eip; // x86 specific
crashStream << "signal " << sig_num
<< " (" << strsignal(sig_num) << "), address is "
<< info->si_addr << " from " << caller_address
<< std::endl << std::endl;
int size = backtrace(array, 50);
array[1] = caller_address;
char** messages = backtrace_symbols(array, size);
// skip first stack frame (points here)
for (int i = 1; i < size && messages != nullptr; ++i)
{
char* mangled_name = nullptr, *offset_begin = nullptr, *offset_end = nullptr;
// find parantheses and +address offset surrounding mangled name
for (char* p = messages[i]; *p; ++p)
{
if (*p == '(')
{
mangled_name = p;
}
else if (*p == '+')
{
offset_begin = p;
}
else if (*p == ')')
{
offset_end = p;
break;
}
}
// if the line could be processed, attempt to demangle the symbol
if (mangled_name && offset_begin && offset_end &&
mangled_name < offset_begin)
{
*mangled_name++ = '\0';
*offset_begin++ = '\0';
*offset_end++ = '\0';
int status;
char * real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);
// if demangling is successful, output the demangled function name
if (status == 0)
{
crashStream << "[bt]: (" << i << ") " << messages[i] << " : "
<< real_name << "+" << offset_begin << offset_end
<< std::endl;
}
// otherwise, output the mangled function name
else
{
crashStream << "[bt]: (" << i << ") " << messages[i] << " : "
<< mangled_name << "+" << offset_begin << offset_end
<< std::endl;
}
free(real_name);
}
// otherwise, print the whole line
else
{
crashStream << "[bt]: (" << i << ") " << messages[i] << std::endl;
}
}
crashStream.flush();
// We try to export to the logs
LogManager* logMgr = LogManager::getSingletonPtr();
if(logMgr != nullptr)
{
while(crashStream.good())
{
std::string log;
if(!Helper::readNextLineNotEmpty(crashStream, log))
break;
logMgr->logMessage(LogMessageLevel::CRITICAL, __FILE__, __LINE__, log);
}
}
// Set the stream at beginning
crashStream.clear();
crashStream.seekg(0, crashStream.beg);
// We export everything to the crash file
while(crashStream.good())
{
std::string log;
if(!Helper::readNextLineNotEmpty(crashStream, log))
break;
std::cerr << log << std::endl;
}
std::cerr.flush();
free(messages);
exit(EXIT_FAILURE);
}