1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Christoph Renner |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief Implementation of the SignalHandler class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "SignalHandler.h" |
---|
35 | |
---|
36 | #include <iostream> |
---|
37 | #include <cstdlib> |
---|
38 | #include <cstring> |
---|
39 | #include <cstdio> |
---|
40 | |
---|
41 | #include "Output.h" |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | SignalHandler* SignalHandler::singletonPtr_s = NULL; |
---|
46 | } |
---|
47 | |
---|
48 | #if defined(ORXONOX_PLATFORM_LINUX) |
---|
49 | |
---|
50 | #include <wait.h> |
---|
51 | #include <X11/Xlib.h> |
---|
52 | #include <X11/Xutil.h> |
---|
53 | #include <X11/keysym.h> |
---|
54 | #include <sys/prctl.h> |
---|
55 | |
---|
56 | namespace orxonox |
---|
57 | { |
---|
58 | /** |
---|
59 | * register signal handlers for SIGSEGV and SIGABRT |
---|
60 | * @param appName path to executable eg argv[0] |
---|
61 | * @param filename filename to append backtrace to |
---|
62 | */ |
---|
63 | void SignalHandler::doCatch( const std::string & appName, const std::string & filename ) |
---|
64 | { |
---|
65 | this->appName = appName; |
---|
66 | this->filename = filename; |
---|
67 | |
---|
68 | // make sure doCatch is only called once without calling dontCatch |
---|
69 | assert( sigRecList.size() == 0 ); |
---|
70 | |
---|
71 | catchSignal( SIGSEGV ); |
---|
72 | catchSignal( SIGABRT ); |
---|
73 | catchSignal( SIGILL ); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * restore previous signal handlers |
---|
78 | */ |
---|
79 | void SignalHandler::dontCatch() |
---|
80 | { |
---|
81 | for ( SignalRecList::iterator it = sigRecList.begin(); it != sigRecList.end(); it++ ) |
---|
82 | { |
---|
83 | signal( it->signal, it->handler ); |
---|
84 | } |
---|
85 | |
---|
86 | sigRecList.clear(); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * catch signal sig |
---|
91 | * @param sig signal to catch |
---|
92 | */ |
---|
93 | void SignalHandler::catchSignal( int sig ) |
---|
94 | { |
---|
95 | sig_t handler = signal( sig, SignalHandler::sigHandler ); |
---|
96 | |
---|
97 | assert( handler != SIG_ERR ); |
---|
98 | |
---|
99 | SignalRec rec; |
---|
100 | rec.signal = sig; |
---|
101 | rec.handler = handler; |
---|
102 | |
---|
103 | sigRecList.push_front( rec ); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * sigHandler is called when receiving signals |
---|
108 | * @param sig |
---|
109 | */ |
---|
110 | void SignalHandler::sigHandler( int sig ) |
---|
111 | { |
---|
112 | std::string sigName = "UNKNOWN"; |
---|
113 | |
---|
114 | switch ( sig ) |
---|
115 | { |
---|
116 | case SIGSEGV: |
---|
117 | sigName = "SIGSEGV"; |
---|
118 | break; |
---|
119 | case SIGABRT: |
---|
120 | sigName = "SIGABRT"; |
---|
121 | break; |
---|
122 | case SIGILL: |
---|
123 | sigName = "SIGILL"; |
---|
124 | break; |
---|
125 | } |
---|
126 | // if the signalhandler has already been destroyed then don't do anything |
---|
127 | if( SignalHandler::singletonPtr_s == 0 ) |
---|
128 | { |
---|
129 | orxout(user_error) << "Received signal " << sigName.c_str() << endl << "Can't write backtrace because SignalHandler is already destroyed" << endl; |
---|
130 | exit(EXIT_FAILURE); |
---|
131 | } |
---|
132 | |
---|
133 | for ( SignalCallbackList::iterator it = SignalHandler::getInstance().callbackList.begin(); it != SignalHandler::getInstance().callbackList.end(); it++ ) |
---|
134 | { |
---|
135 | (*(it->cb))( it->someData ); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | orxout(user_error) << "Received signal " << sigName.c_str() << endl << "Try to write backtrace to file orxonox_crash.log" << endl; |
---|
140 | |
---|
141 | |
---|
142 | // First start GDB which will be attached to this process later on |
---|
143 | |
---|
144 | int gdbIn[2]; |
---|
145 | int gdbOut[2]; |
---|
146 | int gdbErr[2]; |
---|
147 | |
---|
148 | if ( pipe(gdbIn) == -1 || pipe(gdbOut) == -1 || pipe(gdbErr) == -1 ) |
---|
149 | { |
---|
150 | perror("pipe failed!\n"); |
---|
151 | exit(EXIT_FAILURE); |
---|
152 | } |
---|
153 | |
---|
154 | int gdbPid = fork(); |
---|
155 | // this process will run gdb |
---|
156 | |
---|
157 | if ( gdbPid == -1 ) |
---|
158 | { |
---|
159 | perror("fork failed\n"); |
---|
160 | exit(EXIT_FAILURE); |
---|
161 | } |
---|
162 | |
---|
163 | if ( gdbPid == 0 ) |
---|
164 | { |
---|
165 | // start gdb |
---|
166 | |
---|
167 | close(gdbIn[1]); |
---|
168 | close(gdbOut[0]); |
---|
169 | close(gdbErr[0]); |
---|
170 | |
---|
171 | dup2( gdbIn[0], STDIN_FILENO ); |
---|
172 | dup2( gdbOut[1], STDOUT_FILENO ); |
---|
173 | dup2( gdbErr[1], STDERR_FILENO ); |
---|
174 | |
---|
175 | execlp( "sh", "sh", "-c", "gdb", static_cast<void*>(NULL)); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | // Now start a fork of this process on which GDB will be attached on |
---|
180 | |
---|
181 | int sigPipe[2]; |
---|
182 | if ( pipe(sigPipe) == -1 ) |
---|
183 | { |
---|
184 | perror("pipe failed!\n"); |
---|
185 | kill( gdbPid, SIGTERM ); |
---|
186 | waitpid( gdbPid, NULL, 0 ); |
---|
187 | exit(EXIT_FAILURE); |
---|
188 | } |
---|
189 | |
---|
190 | int sigPid = fork(); |
---|
191 | |
---|
192 | if ( sigPid == -1 ) |
---|
193 | { |
---|
194 | perror("fork failed!\n"); |
---|
195 | kill( gdbPid, SIGTERM ); |
---|
196 | waitpid( gdbPid, NULL, 0 ); |
---|
197 | exit(EXIT_FAILURE); |
---|
198 | } |
---|
199 | |
---|
200 | // gdb will be attached to this process |
---|
201 | if ( sigPid == 0 ) |
---|
202 | { |
---|
203 | getInstance().dontCatch(); |
---|
204 | |
---|
205 | // make sure gdb is allowed to attach to our PID even if there are some system restrictions |
---|
206 | #ifdef PR_SET_PTRACER |
---|
207 | if( prctl(PR_SET_PTRACER, gdbPid, 0, 0, 0) == -1 ) |
---|
208 | orxout(user_error) << "could not set proper permissions for GDB to attach to process..." << endl; |
---|
209 | #endif |
---|
210 | |
---|
211 | // wait for message from parent when it has attached gdb |
---|
212 | int someData; |
---|
213 | |
---|
214 | if( read( sigPipe[0], &someData, sizeof(someData) ) != sizeof(someData) ) |
---|
215 | orxout(user_error) << "something went wrong :(" << endl; |
---|
216 | |
---|
217 | if ( someData != 0x12345678 ) |
---|
218 | { |
---|
219 | orxout(user_error) << "something went wrong :(" << endl; |
---|
220 | } |
---|
221 | |
---|
222 | return; |
---|
223 | } |
---|
224 | |
---|
225 | char cmd[256]; |
---|
226 | snprintf( cmd, 256, "file %s\nattach %d\nc\n", getInstance().appName.c_str(), sigPid ); |
---|
227 | write( gdbIn[1], cmd, strlen(cmd) ); |
---|
228 | |
---|
229 | int charsFound = 0; |
---|
230 | int promptFound = 0; |
---|
231 | char byte; |
---|
232 | while ( read( gdbOut[0], &byte, 1 ) == 1 ) |
---|
233 | { |
---|
234 | if ( |
---|
235 | (charsFound == 0 && byte == '(') || |
---|
236 | (charsFound == 1 && byte == 'g') || |
---|
237 | (charsFound == 2 && byte == 'd') || |
---|
238 | (charsFound == 3 && byte == 'b') || |
---|
239 | (charsFound == 4 && byte == ')') || |
---|
240 | (charsFound == 5 && byte == ' ') |
---|
241 | ) |
---|
242 | charsFound++; |
---|
243 | else |
---|
244 | charsFound = 0; |
---|
245 | |
---|
246 | if ( charsFound == 6 ) |
---|
247 | { |
---|
248 | promptFound++; |
---|
249 | charsFound = 0; |
---|
250 | } |
---|
251 | |
---|
252 | if ( promptFound == 3 ) |
---|
253 | { |
---|
254 | break; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | int someData = 0x12345678; |
---|
259 | write( sigPipe[1], &someData, sizeof(someData) ); |
---|
260 | |
---|
261 | write( gdbIn[1], "bt\nk\nq\n", 7 ); |
---|
262 | |
---|
263 | |
---|
264 | charsFound = 0; |
---|
265 | promptFound = 0; |
---|
266 | std::string bt; |
---|
267 | while ( read( gdbOut[0], &byte, 1 ) == 1 ) |
---|
268 | { |
---|
269 | bt += std::string( &byte, 1 ); |
---|
270 | |
---|
271 | if ( |
---|
272 | (charsFound == 0 && byte == '(') || |
---|
273 | (charsFound == 1 && byte == 'g') || |
---|
274 | (charsFound == 2 && byte == 'd') || |
---|
275 | (charsFound == 3 && byte == 'b') || |
---|
276 | (charsFound == 4 && byte == ')') || |
---|
277 | (charsFound == 5 && byte == ' ') |
---|
278 | ) |
---|
279 | charsFound++; |
---|
280 | else |
---|
281 | charsFound = 0; |
---|
282 | |
---|
283 | if ( charsFound == 6 ) |
---|
284 | { |
---|
285 | promptFound++; |
---|
286 | charsFound = 0; |
---|
287 | bt += "\n"; |
---|
288 | } |
---|
289 | |
---|
290 | if ( promptFound == 3 ) |
---|
291 | { |
---|
292 | break; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | |
---|
297 | waitpid( sigPid, NULL, 0 ); |
---|
298 | waitpid( gdbPid, NULL, 0 ); |
---|
299 | |
---|
300 | int wsRemoved = 0; |
---|
301 | |
---|
302 | while ( wsRemoved < 2 && bt.length() > 0 ) |
---|
303 | { |
---|
304 | if ( bt[1] == '\n' ) |
---|
305 | wsRemoved++; |
---|
306 | bt.erase(0, 1); |
---|
307 | } |
---|
308 | |
---|
309 | if ( bt.length() > 0 ) |
---|
310 | bt.erase(0, 1); |
---|
311 | |
---|
312 | time_t now = time(NULL); |
---|
313 | |
---|
314 | std::string timeString = |
---|
315 | "=======================================================\n" |
---|
316 | "= time: " + std::string(ctime(&now)) + |
---|
317 | "=======================================================\n"; |
---|
318 | bt.insert(0, timeString); |
---|
319 | |
---|
320 | FILE * f = fopen( getInstance().filename.c_str(), "w" ); |
---|
321 | |
---|
322 | if ( !f ) |
---|
323 | { |
---|
324 | perror( ( std::string( "could not append to " ) + getInstance().filename ).c_str() ); |
---|
325 | exit(EXIT_FAILURE); |
---|
326 | } |
---|
327 | |
---|
328 | if ( fwrite( bt.c_str(), 1, bt.length(), f ) != bt.length() ) |
---|
329 | { |
---|
330 | orxout(user_error) << "could not write " << bt.length() << " byte to " << getInstance().filename << endl; |
---|
331 | exit(EXIT_FAILURE); |
---|
332 | } |
---|
333 | |
---|
334 | exit(EXIT_FAILURE); |
---|
335 | } |
---|
336 | |
---|
337 | void SignalHandler::registerCallback( SignalCallback cb, void * someData ) |
---|
338 | { |
---|
339 | SignalCallbackRec rec; |
---|
340 | rec.cb = cb; |
---|
341 | rec.someData = someData; |
---|
342 | |
---|
343 | callbackList.push_back(rec); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | #elif defined(ORXONOX_PLATFORM_WINDOWS) && defined(DBGHELP_FOUND) |
---|
348 | |
---|
349 | #include <iostream> |
---|
350 | #include <iomanip> |
---|
351 | #include <fstream> |
---|
352 | #include <ctime> |
---|
353 | #include <dbghelp.h> |
---|
354 | #include <tlhelp32.h> |
---|
355 | |
---|
356 | #include "Convert.h" |
---|
357 | |
---|
358 | #ifdef ORXONOX_COMPILER_GCC |
---|
359 | # include <cxxabi.h> |
---|
360 | #endif |
---|
361 | |
---|
362 | #ifdef ORXONOX_COMPILER_GCC |
---|
363 | /// Overwrite the original abort() function in MinGW to enable a break point. |
---|
364 | _UtilExport void __cdecl abort() |
---|
365 | { |
---|
366 | using namespace orxonox; |
---|
367 | orxout(user_error) << "This application has requested the Runtime to terminate it in an unusual way." << endl; |
---|
368 | orxout(user_error) << "Please contact the application's support team for more information." << endl; |
---|
369 | DebugBreak(); |
---|
370 | exit(0x3); |
---|
371 | } |
---|
372 | |
---|
373 | /// Overwrite the original _abort() function in MinGW to enable a break point. |
---|
374 | _UtilExport void __cdecl _assert(const char* expression, const char* file, int line) |
---|
375 | { |
---|
376 | using namespace orxonox; |
---|
377 | orxout(user_error) << "Assertion failed: " << expression << ", file " << file << ", line " << line << endl; |
---|
378 | orxout(user_error) << endl; |
---|
379 | abort(); |
---|
380 | } |
---|
381 | #endif |
---|
382 | |
---|
383 | namespace orxonox |
---|
384 | { |
---|
385 | /// Constructor: Initializes the values, but doesn't register the exception handler. |
---|
386 | SignalHandler::SignalHandler() |
---|
387 | { |
---|
388 | this->prevExceptionFilter_ = NULL; |
---|
389 | } |
---|
390 | |
---|
391 | /// Destructor: Removes the exception handler. |
---|
392 | SignalHandler::~SignalHandler() |
---|
393 | { |
---|
394 | if (this->prevExceptionFilter_ != NULL) |
---|
395 | { |
---|
396 | // Remove the unhandled exception filter function |
---|
397 | SetUnhandledExceptionFilter(this->prevExceptionFilter_); |
---|
398 | this->prevExceptionFilter_ = NULL; |
---|
399 | } |
---|
400 | } |
---|
401 | |
---|
402 | /// Registers an exception handler and initializes the filename of the crash log. |
---|
403 | void SignalHandler::doCatch(const std::string&, const std::string& filename) |
---|
404 | { |
---|
405 | this->filename_ = filename; |
---|
406 | |
---|
407 | // don't register twice |
---|
408 | assert(this->prevExceptionFilter_ == NULL); |
---|
409 | |
---|
410 | if (this->prevExceptionFilter_ == NULL) |
---|
411 | { |
---|
412 | // Install the unhandled exception filter function |
---|
413 | this->prevExceptionFilter_ = SetUnhandledExceptionFilter(&SignalHandler::exceptionFilter); |
---|
414 | } |
---|
415 | } |
---|
416 | |
---|
417 | /// Exception handler: Will be called by Windows if an unhandled exceptions occurs. |
---|
418 | /* static */ LONG WINAPI SignalHandler::exceptionFilter(PEXCEPTION_POINTERS pExceptionInfo) |
---|
419 | { |
---|
420 | // avoid loops |
---|
421 | static bool bExecuting = false; |
---|
422 | |
---|
423 | if (!bExecuting) |
---|
424 | { |
---|
425 | bExecuting = true; |
---|
426 | |
---|
427 | orxout(user_error) << endl; |
---|
428 | |
---|
429 | // if the signalhandler has already been destroyed then don't do anything |
---|
430 | if (SignalHandler::singletonPtr_s == 0) |
---|
431 | { |
---|
432 | orxout(user_error) << "Caught an unhandled exception" << endl << "Can't write backtrace because SignalHandler is already destroyed" << endl; |
---|
433 | exit(EXIT_FAILURE); |
---|
434 | } |
---|
435 | |
---|
436 | orxout(user_error) << "Caught an unhandled exception" << endl << "Try to write backtrace to orxonox_crash.log..." << endl; |
---|
437 | |
---|
438 | // write the crash log |
---|
439 | std::ofstream crashlog(SignalHandler::getInstance().filename_.c_str()); |
---|
440 | |
---|
441 | time_t now = time(NULL); |
---|
442 | |
---|
443 | crashlog << "=======================================================" << endl; |
---|
444 | crashlog << "= Time: " << std::string(ctime(&now)); |
---|
445 | crashlog << "=======================================================" << endl; |
---|
446 | crashlog << endl; |
---|
447 | |
---|
448 | const std::string& error = SignalHandler::getExceptionType(pExceptionInfo); |
---|
449 | |
---|
450 | crashlog << error << endl; |
---|
451 | crashlog << endl; |
---|
452 | |
---|
453 | const std::string& callstack = SignalHandler::getStackTrace(pExceptionInfo); |
---|
454 | |
---|
455 | crashlog << "Call stack:" << endl; |
---|
456 | crashlog << callstack << endl; |
---|
457 | |
---|
458 | crashlog.close(); |
---|
459 | |
---|
460 | // print the same information also to the console |
---|
461 | orxout(user_error) << endl; |
---|
462 | orxout(user_error) << error << endl; |
---|
463 | orxout(user_error) << endl; |
---|
464 | orxout(user_error) << "Call stack:" << endl; |
---|
465 | orxout(user_error) << callstack << endl; |
---|
466 | |
---|
467 | bExecuting = false; |
---|
468 | } |
---|
469 | else |
---|
470 | { |
---|
471 | orxout(user_error) << "An error occurred while writing the backtrace" << endl; |
---|
472 | } |
---|
473 | |
---|
474 | if (SignalHandler::getInstance().prevExceptionFilter_) |
---|
475 | return SignalHandler::getInstance().prevExceptionFilter_(pExceptionInfo); |
---|
476 | else |
---|
477 | return EXCEPTION_CONTINUE_SEARCH; |
---|
478 | } |
---|
479 | |
---|
480 | /// Returns the stack trace for either the current function, or, if @a pExceptionInfo is not NULL, for the given exception context. |
---|
481 | /* static */ std::string SignalHandler::getStackTrace(PEXCEPTION_POINTERS pExceptionInfo) |
---|
482 | { |
---|
483 | // Initialise the symbol table to get function names: |
---|
484 | SymSetOptions |
---|
485 | ( |
---|
486 | SYMOPT_DEFERRED_LOADS |
---|
487 | #ifndef ORXONOX_COMPILER_GCC |
---|
488 | | SYMOPT_UNDNAME |
---|
489 | #endif |
---|
490 | ); |
---|
491 | SymInitialize(GetCurrentProcess(), 0, true); |
---|
492 | |
---|
493 | // Store the current stack frame here: |
---|
494 | STACKFRAME64 frame; |
---|
495 | memset(&frame, 0, sizeof(STACKFRAME64)); |
---|
496 | |
---|
497 | // Get processor information for the current thread: |
---|
498 | CONTEXT context; |
---|
499 | memset(&context, 0, sizeof(CONTEXT)); |
---|
500 | |
---|
501 | if (pExceptionInfo) |
---|
502 | { |
---|
503 | // get the context of the exception |
---|
504 | context = *pExceptionInfo->ContextRecord; |
---|
505 | } |
---|
506 | else |
---|
507 | { |
---|
508 | context.ContextFlags = CONTEXT_FULL; |
---|
509 | |
---|
510 | // Load the RTLCapture context function: |
---|
511 | HMODULE kernel32 = LoadLibrary("Kernel32.dll"); |
---|
512 | typedef void (*RtlCaptureContextFunc) (CONTEXT* ContextRecord); |
---|
513 | RtlCaptureContextFunc rtlCaptureContext = (RtlCaptureContextFunc) GetProcAddress(kernel32, "RtlCaptureContext"); |
---|
514 | |
---|
515 | // Capture the thread context |
---|
516 | rtlCaptureContext(&context); |
---|
517 | } |
---|
518 | |
---|
519 | DWORD type; |
---|
520 | |
---|
521 | // set the flags and initialize the stackframe struct |
---|
522 | #ifdef _M_IX86 |
---|
523 | type = IMAGE_FILE_MACHINE_I386; |
---|
524 | |
---|
525 | frame.AddrPC.Offset = context.Eip; // program counter |
---|
526 | frame.AddrFrame.Offset = context.Ebp; // frame pointer (for function arguments) |
---|
527 | frame.AddrStack.Offset = context.Esp; // stack pointer |
---|
528 | #elif _M_X64 |
---|
529 | type = IMAGE_FILE_MACHINE_AMD64; |
---|
530 | |
---|
531 | frame.AddrPC.Offset = context.Rip; // program counter |
---|
532 | frame.AddrFrame.Offset = context.Rbp; // (or Rdi) // frame pointer (for function arguments) |
---|
533 | frame.AddrStack.Offset = context.Rsp; // stack pointer |
---|
534 | #elif _M_IA64 |
---|
535 | type = IMAGE_FILE_MACHINE_IA64; |
---|
536 | |
---|
537 | frame.AddrPC.Offset = context.StIIP; // program counter |
---|
538 | frame.AddrFrame.Offset = context.RsBSP; // frame pointer (for function arguments) // <-- unneeded on Intel IPF, may be removed |
---|
539 | frame.AddrStack.Offset = context.IntSp; // stack pointer |
---|
540 | frame.AddrBStore.Offset = context.RsBSP; // backing store |
---|
541 | frame.AddrBStore.Mode = AddrModeFlat; |
---|
542 | #else |
---|
543 | return ""; |
---|
544 | #endif |
---|
545 | |
---|
546 | frame.AddrPC.Mode = AddrModeFlat; |
---|
547 | frame.AddrFrame.Mode = AddrModeFlat; |
---|
548 | frame.AddrStack.Mode = AddrModeFlat; |
---|
549 | |
---|
550 | std::string output; |
---|
551 | |
---|
552 | // Keep getting stack frames from windows till there are no more left: |
---|
553 | for (int i = 0; |
---|
554 | StackWalk64 |
---|
555 | ( |
---|
556 | type , // MachineType |
---|
557 | GetCurrentProcess() , // Process to get stack trace for |
---|
558 | GetCurrentThread() , // Thread to get stack trace for |
---|
559 | &frame , // Where to store next stack frame |
---|
560 | &context , // Pointer to processor context record |
---|
561 | 0 , // Routine to read process memory: use the default ReadProcessMemory |
---|
562 | &SymFunctionTableAccess64 , // Routine to access the modules symbol table |
---|
563 | &SymGetModuleBase64 , // Routine to access the modules base address |
---|
564 | 0 // Something to do with 16-bit code. Not needed. |
---|
565 | ); |
---|
566 | ++i |
---|
567 | ) |
---|
568 | { |
---|
569 | //------------------------------------------------------------------ |
---|
570 | // Declare an image help symbol structure to hold symbol info and |
---|
571 | // name up to 256 chars This struct is of variable lenght though so |
---|
572 | // it must be declared as a raw byte buffer. |
---|
573 | //------------------------------------------------------------------ |
---|
574 | static char symbolBuffer[sizeof(SYMBOL_INFO) + 255]; |
---|
575 | memset(symbolBuffer, 0, sizeof(symbolBuffer)); |
---|
576 | |
---|
577 | // Cast it to a symbol struct: |
---|
578 | SYMBOL_INFO* symbol = (SYMBOL_INFO*)symbolBuffer; |
---|
579 | |
---|
580 | // Need to set two fields of this symbol before obtaining name info: |
---|
581 | symbol->SizeOfStruct = sizeof(SYMBOL_INFO); |
---|
582 | symbol->MaxNameLen = 255; |
---|
583 | |
---|
584 | // The displacement from the beginning of the symbol is stored here: pretty useless |
---|
585 | long long unsigned int displacement = 0; |
---|
586 | |
---|
587 | if (i < 10) |
---|
588 | output += " "; |
---|
589 | output += multi_cast<std::string>(i) + ": "; |
---|
590 | |
---|
591 | // Print the function's address: |
---|
592 | output += SignalHandler::pointerToString(frame.AddrPC.Offset); |
---|
593 | |
---|
594 | // Get the symbol information from the address of the instruction pointer register: |
---|
595 | bool bCorrected = false; |
---|
596 | BOOL result = SymFromAddr |
---|
597 | ( |
---|
598 | GetCurrentProcess() , // Process to get symbol information for |
---|
599 | frame.AddrPC.Offset , // Address to get symbol for: instruction pointer register |
---|
600 | &displacement , // Displacement from the beginning of the symbol |
---|
601 | symbol // Where to save the symbol |
---|
602 | ); |
---|
603 | |
---|
604 | // If the symbol was found, but the displacement is 0, we likely got the wrong symbol - decrease the program counter and try again |
---|
605 | if (result && displacement == 0) |
---|
606 | { |
---|
607 | bCorrected = true; |
---|
608 | result = SymFromAddr |
---|
609 | ( |
---|
610 | GetCurrentProcess() , |
---|
611 | frame.AddrPC.Offset - 1 , |
---|
612 | &displacement , |
---|
613 | symbol |
---|
614 | ); |
---|
615 | } |
---|
616 | |
---|
617 | // Display the function name + offset |
---|
618 | if (result) |
---|
619 | { |
---|
620 | // Add the name of the function to the function list: |
---|
621 | output += " "; |
---|
622 | |
---|
623 | #ifdef ORXONOX_COMPILER_GCC |
---|
624 | int status; |
---|
625 | char* demangled = __cxxabiv1::__cxa_demangle(symbol->Name, NULL, NULL, &status); |
---|
626 | if (demangled) |
---|
627 | { |
---|
628 | output += demangled; |
---|
629 | free(demangled); |
---|
630 | } |
---|
631 | else |
---|
632 | #endif |
---|
633 | { |
---|
634 | output += symbol->Name; |
---|
635 | } |
---|
636 | |
---|
637 | output += " +" + SignalHandler::pointerToString(displacement, false); |
---|
638 | if (bCorrected) |
---|
639 | output += " (?)"; |
---|
640 | } |
---|
641 | |
---|
642 | output += "\n"; |
---|
643 | |
---|
644 | // Get the file name and line number |
---|
645 | IMAGEHLP_LINE64 line; |
---|
646 | memset(&line, 0, sizeof(IMAGEHLP_LINE64)); |
---|
647 | line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
---|
648 | |
---|
649 | DWORD displacement2 = 0; |
---|
650 | |
---|
651 | if |
---|
652 | ( |
---|
653 | SymGetLineFromAddr64 |
---|
654 | ( |
---|
655 | GetCurrentProcess(), |
---|
656 | frame.AddrPC.Offset - bCorrected ? 1 : 0, |
---|
657 | &displacement2, |
---|
658 | &line |
---|
659 | ) |
---|
660 | ) |
---|
661 | { |
---|
662 | output += " "; |
---|
663 | output += line.FileName; |
---|
664 | output += ":"; |
---|
665 | output += multi_cast<std::string>(line.LineNumber); |
---|
666 | output += "\n"; |
---|
667 | } |
---|
668 | } |
---|
669 | |
---|
670 | // Cleanup the symbol table: |
---|
671 | SymCleanup(GetCurrentProcess()); |
---|
672 | |
---|
673 | return output; |
---|
674 | } |
---|
675 | |
---|
676 | /// Returns a description of the given exception. |
---|
677 | // Based on code from Dr. Mingw by Jos\E9 Fonseca |
---|
678 | /* static */ std::string SignalHandler::getExceptionType(PEXCEPTION_POINTERS pExceptionInfo) |
---|
679 | { |
---|
680 | PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord; |
---|
681 | TCHAR szModule[MAX_PATH]; |
---|
682 | HMODULE hModule; |
---|
683 | |
---|
684 | std::string output = (GetModuleFileName(NULL, szModule, MAX_PATH) ? SignalHandler::getModuleName(szModule) : "Application"); |
---|
685 | output += " caused "; |
---|
686 | |
---|
687 | switch(pExceptionRecord->ExceptionCode) |
---|
688 | { |
---|
689 | case EXCEPTION_ACCESS_VIOLATION: output += "an Access Violation"; break; |
---|
690 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: output += "an Array Bound Exceeded"; break; |
---|
691 | case EXCEPTION_BREAKPOINT: output += "a Breakpoint"; break; |
---|
692 | case EXCEPTION_DATATYPE_MISALIGNMENT: output += "a Datatype Misalignment"; break; |
---|
693 | case EXCEPTION_FLT_DENORMAL_OPERAND: output += "a Float Denormal Operand"; break; |
---|
694 | case EXCEPTION_FLT_DIVIDE_BY_ZERO: output += "a Float Divide By Zero"; break; |
---|
695 | case EXCEPTION_FLT_INEXACT_RESULT: output += "a Float Inexact Result"; break; |
---|
696 | case EXCEPTION_FLT_INVALID_OPERATION: output += "a Float Invalid Operation"; break; |
---|
697 | case EXCEPTION_FLT_OVERFLOW: output += "a Float Overflow"; break; |
---|
698 | case EXCEPTION_FLT_STACK_CHECK: output += "a Float Stack Check"; break; |
---|
699 | case EXCEPTION_FLT_UNDERFLOW: output += "a Float Underflow"; break; |
---|
700 | case EXCEPTION_GUARD_PAGE: output += "a Guard Page"; break; |
---|
701 | case EXCEPTION_ILLEGAL_INSTRUCTION: output += "an Illegal Instruction"; break; |
---|
702 | case EXCEPTION_IN_PAGE_ERROR: output += "an In Page Error"; break; |
---|
703 | case EXCEPTION_INT_DIVIDE_BY_ZERO: output += "an Integer Divide By Zero"; break; |
---|
704 | case EXCEPTION_INT_OVERFLOW: output += "an Integer Overflow"; break; |
---|
705 | case EXCEPTION_INVALID_DISPOSITION: output += "an Invalid Disposition"; break; |
---|
706 | case EXCEPTION_INVALID_HANDLE: output += "an Invalid Handle"; break; |
---|
707 | case EXCEPTION_NONCONTINUABLE_EXCEPTION: output += "a Noncontinuable Exception"; break; |
---|
708 | case EXCEPTION_PRIV_INSTRUCTION: output += "a Privileged Instruction"; break; |
---|
709 | case EXCEPTION_SINGLE_STEP: output += "a Single Step"; break; |
---|
710 | case EXCEPTION_STACK_OVERFLOW: output += "a Stack Overflow"; break; |
---|
711 | case DBG_CONTROL_C: output += "a Control+C"; break; |
---|
712 | case DBG_CONTROL_BREAK: output += "a Control+Break"; break; |
---|
713 | case DBG_TERMINATE_THREAD: output += "a Terminate Thread"; break; |
---|
714 | case DBG_TERMINATE_PROCESS: output += "a Terminate Process"; break; |
---|
715 | case RPC_S_UNKNOWN_IF: output += "an Unknown Interface"; break; |
---|
716 | case RPC_S_SERVER_UNAVAILABLE: output += "a Server Unavailable"; break; |
---|
717 | default: output += "an Unknown Exception (" + SignalHandler::pointerToString(pExceptionRecord->ExceptionCode) + ")"; break; |
---|
718 | } |
---|
719 | |
---|
720 | // Now print information about where the fault occured |
---|
721 | output += " at location " + SignalHandler::pointerToString(pExceptionRecord->ExceptionAddress); |
---|
722 | if ((hModule = (HMODULE) SignalHandler::getModuleBase((DWORD) pExceptionRecord->ExceptionAddress)) && GetModuleFileName(hModule, szModule, MAX_PATH)) |
---|
723 | { |
---|
724 | output += " in module "; |
---|
725 | output += SignalHandler::getModuleName(szModule); |
---|
726 | } |
---|
727 | |
---|
728 | // If the exception was an access violation, print out some additional information, to the error log and the debugger. |
---|
729 | if(pExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && pExceptionRecord->NumberParameters >= 2) |
---|
730 | { |
---|
731 | output += " "; |
---|
732 | output += pExceptionRecord->ExceptionInformation[0] ? "writing to" : "reading from"; |
---|
733 | output += " location "; |
---|
734 | output += SignalHandler::pointerToString(pExceptionRecord->ExceptionInformation[1]); |
---|
735 | } |
---|
736 | |
---|
737 | return output; |
---|
738 | } |
---|
739 | |
---|
740 | /// Strips the directories from the path to the module and returns the module's name only. |
---|
741 | /* static */ std::string SignalHandler::getModuleName(const std::string& path) |
---|
742 | { |
---|
743 | return path.substr(path.find_last_of('\\') + 1); |
---|
744 | } |
---|
745 | |
---|
746 | /// Retrieves the base address of the module that contains the specified address. |
---|
747 | // Code from Dr. Mingw by Jos\E9 Fonseca |
---|
748 | /* static */ DWORD SignalHandler::getModuleBase(DWORD dwAddress) |
---|
749 | { |
---|
750 | MEMORY_BASIC_INFORMATION Buffer; |
---|
751 | |
---|
752 | return VirtualQuery((LPCVOID) dwAddress, &Buffer, sizeof(Buffer)) ? (DWORD) Buffer.AllocationBase : 0; |
---|
753 | } |
---|
754 | |
---|
755 | /// Converts a value to string, formatted as pointer. |
---|
756 | template <typename T> |
---|
757 | /* static */ std::string SignalHandler::pointerToString(T pointer, bool bFillZeros) |
---|
758 | { |
---|
759 | std::ostringstream oss; |
---|
760 | |
---|
761 | if (bFillZeros) |
---|
762 | oss << std::setw(8) << std::setfill('0'); |
---|
763 | |
---|
764 | oss << std::hex << pointer; |
---|
765 | |
---|
766 | return std::string("0x") + oss.str(); |
---|
767 | } |
---|
768 | |
---|
769 | /// Converts a pointer to string. |
---|
770 | template <typename T> |
---|
771 | /* static */ std::string SignalHandler::pointerToString(T* pointer) |
---|
772 | { |
---|
773 | std::ostringstream oss; |
---|
774 | |
---|
775 | oss << pointer; |
---|
776 | |
---|
777 | return oss.str(); |
---|
778 | } |
---|
779 | } |
---|
780 | |
---|
781 | #endif |
---|