[7361] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Christoph Renner |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #include "signal_handler.h" |
---|
[7461] | 17 | #include <assert.h> |
---|
[7361] | 18 | |
---|
[7791] | 19 | #ifdef __LINUX__ |
---|
[7439] | 20 | |
---|
[7361] | 21 | SignalHandler * SignalHandler::singletonRef = NULL; |
---|
| 22 | |
---|
| 23 | SignalHandler::SignalHandler() |
---|
| 24 | { |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | void SignalHandler::doCatch( std::string appName, GdbRunType type ) |
---|
| 28 | { |
---|
| 29 | this->type = type; |
---|
| 30 | this->appName = appName; |
---|
[7461] | 31 | |
---|
[7361] | 32 | catchSignal( SIGSEGV ); |
---|
| 33 | catchSignal( SIGABRT ); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | void SignalHandler::dontCatch() |
---|
| 37 | { |
---|
| 38 | for ( SignalRecList::iterator it = sigRecList.begin(); it != sigRecList.end(); it++ ) |
---|
| 39 | { |
---|
| 40 | signal( it->signal, it->handler ); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | sigRecList.clear(); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void SignalHandler::catchSignal( int sig ) |
---|
| 47 | { |
---|
[8122] | 48 | sig_t handler = signal( sig, SignalHandler::sigHandler ); |
---|
[7361] | 49 | |
---|
| 50 | assert( handler != SIG_ERR ); |
---|
| 51 | |
---|
| 52 | SignalRec rec; |
---|
| 53 | rec.signal = sig; |
---|
| 54 | rec.handler = handler; |
---|
| 55 | |
---|
| 56 | sigRecList.push_front( rec ); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void SignalHandler::sigHandler( int sig ) |
---|
| 60 | { |
---|
[8122] | 61 | |
---|
[7361] | 62 | std::string sigName = "UNKNOWN"; |
---|
[8122] | 63 | |
---|
[7361] | 64 | switch ( sig ) |
---|
| 65 | { |
---|
| 66 | case SIGSEGV: |
---|
| 67 | sigName = "SIGSEGV"; |
---|
| 68 | break; |
---|
| 69 | case SIGABRT: |
---|
| 70 | sigName = "SIGABRT"; |
---|
| 71 | break; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | printf( "recieved signal %s\ntry to write backtrace to file orxonox.backtrace\n", sigName.c_str() ); |
---|
| 75 | |
---|
| 76 | int fd[2]; |
---|
| 77 | |
---|
| 78 | assert( pipe(fd) != -1 ); |
---|
[7461] | 79 | |
---|
[7361] | 80 | int pid = fork(); |
---|
| 81 | |
---|
| 82 | assert( pid != -1 ); |
---|
| 83 | |
---|
| 84 | if ( pid == 0 ) |
---|
| 85 | { |
---|
[8122] | 86 | getInstance()->dontCatch(); |
---|
[7361] | 87 | sleep(2); |
---|
| 88 | |
---|
| 89 | return; |
---|
| 90 | } |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | if ( getInstance()->type == GDB_RUN_WRITE_TO_FILE && fork() == 0 ) |
---|
| 94 | { |
---|
| 95 | sleep(1); |
---|
| 96 | write( fd[1], "c\nbt\nq\n", 7 ); |
---|
| 97 | |
---|
| 98 | exit(0); |
---|
| 99 | } |
---|
[7461] | 100 | |
---|
[7361] | 101 | char command[256]; |
---|
| 102 | if ( getInstance()->type == GDB_RUN_WRITE_TO_FILE ) |
---|
[7366] | 103 | { |
---|
| 104 | dup2( fd[0], STDIN_FILENO ); |
---|
[7361] | 105 | snprintf( command, 255, "gdb -p %d %s 1>" GDB_BT_FILE " 2>&1", pid, getInstance()->appName.c_str() ); |
---|
[7366] | 106 | } |
---|
[7361] | 107 | else |
---|
| 108 | snprintf( command, 255, "gdb -p %d %s", pid, getInstance()->appName.c_str() ); |
---|
| 109 | execlp( "sh", "sh", "-c", command, (void*)NULL); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
[7439] | 113 | #endif /* __WIN32__ */ |
---|