| 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 "Debug.h" | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 | SignalHandler* SignalHandler::singletonPtr_s = NULL; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | #ifdef ORXONOX_PLATFORM_LINUX | 
|---|
| 47 |  | 
|---|
| 48 | #include <wait.h> | 
|---|
| 49 | #include <X11/Xlib.h> | 
|---|
| 50 | #include <X11/Xutil.h> | 
|---|
| 51 | #include <X11/keysym.h> | 
|---|
| 52 |  | 
|---|
| 53 | namespace orxonox | 
|---|
| 54 | { | 
|---|
| 55 | /** | 
|---|
| 56 | * register signal handlers for SIGSEGV and SIGABRT | 
|---|
| 57 | * @param appName path to executable eg argv[0] | 
|---|
| 58 | * @param filename filename to append backtrace to | 
|---|
| 59 | */ | 
|---|
| 60 | void SignalHandler::doCatch( const std::string & appName, const std::string & filename ) | 
|---|
| 61 | { | 
|---|
| 62 | this->appName = appName; | 
|---|
| 63 | this->filename = filename; | 
|---|
| 64 |  | 
|---|
| 65 | // make sure doCatch is only called once without calling dontCatch | 
|---|
| 66 | assert( sigRecList.size() == 0 ); | 
|---|
| 67 |  | 
|---|
| 68 | catchSignal( SIGSEGV ); | 
|---|
| 69 | catchSignal( SIGABRT ); | 
|---|
| 70 | catchSignal( SIGILL ); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | /** | 
|---|
| 74 | * restore previous signal handlers | 
|---|
| 75 | */ | 
|---|
| 76 | void SignalHandler::dontCatch() | 
|---|
| 77 | { | 
|---|
| 78 | for ( SignalRecList::iterator it = sigRecList.begin(); it != sigRecList.end(); it++ ) | 
|---|
| 79 | { | 
|---|
| 80 | signal( it->signal, it->handler ); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | sigRecList.clear(); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | /** | 
|---|
| 87 | * catch signal sig | 
|---|
| 88 | * @param sig signal to catch | 
|---|
| 89 | */ | 
|---|
| 90 | void SignalHandler::catchSignal( int sig ) | 
|---|
| 91 | { | 
|---|
| 92 | sig_t handler = signal( sig, SignalHandler::sigHandler ); | 
|---|
| 93 |  | 
|---|
| 94 | assert( handler != SIG_ERR ); | 
|---|
| 95 |  | 
|---|
| 96 | SignalRec rec; | 
|---|
| 97 | rec.signal = sig; | 
|---|
| 98 | rec.handler = handler; | 
|---|
| 99 |  | 
|---|
| 100 | sigRecList.push_front( rec ); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | /** | 
|---|
| 104 | * sigHandler is called when receiving signals | 
|---|
| 105 | * @param sig | 
|---|
| 106 | */ | 
|---|
| 107 | void SignalHandler::sigHandler( int sig ) | 
|---|
| 108 | { | 
|---|
| 109 | std::string sigName = "UNKNOWN"; | 
|---|
| 110 |  | 
|---|
| 111 | switch ( sig ) | 
|---|
| 112 | { | 
|---|
| 113 | case SIGSEGV: | 
|---|
| 114 | sigName = "SIGSEGV"; | 
|---|
| 115 | break; | 
|---|
| 116 | case SIGABRT: | 
|---|
| 117 | sigName = "SIGABRT"; | 
|---|
| 118 | break; | 
|---|
| 119 | case SIGILL: | 
|---|
| 120 | sigName = "SIGILL"; | 
|---|
| 121 | break; | 
|---|
| 122 | } | 
|---|
| 123 | // if the signalhandler has already been destroyed then don't do anything | 
|---|
| 124 | if( SignalHandler::singletonPtr_s == 0 ) | 
|---|
| 125 | { | 
|---|
| 126 | COUT(0) << "recieved signal " << sigName.c_str() << std::endl << "can't write backtrace because SignalHandler already destroyed" << std::endl; | 
|---|
| 127 | exit(EXIT_FAILURE); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | for ( SignalCallbackList::iterator it = SignalHandler::getInstance().callbackList.begin(); it != SignalHandler::getInstance().callbackList.end(); it++  ) | 
|---|
| 131 | { | 
|---|
| 132 | (*(it->cb))( it->someData ); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 | COUT(0) << "recieved signal " << sigName.c_str() << std::endl << "try to write backtrace to file orxonox_crash.log" << std::endl; | 
|---|
| 137 |  | 
|---|
| 138 | int sigPipe[2]; | 
|---|
| 139 | if ( pipe(sigPipe) == -1 ) | 
|---|
| 140 | { | 
|---|
| 141 | perror("pipe failed!\n"); | 
|---|
| 142 | exit(EXIT_FAILURE); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | int sigPid = fork(); | 
|---|
| 146 |  | 
|---|
| 147 | if ( sigPid == -1 ) | 
|---|
| 148 | { | 
|---|
| 149 | perror("fork failed!\n"); | 
|---|
| 150 | exit(EXIT_FAILURE); | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | // gdb will be attached to this process | 
|---|
| 154 | if ( sigPid == 0 ) | 
|---|
| 155 | { | 
|---|
| 156 | getInstance().dontCatch(); | 
|---|
| 157 | // wait for message from parent when it has attached gdb | 
|---|
| 158 | int someData; | 
|---|
| 159 |  | 
|---|
| 160 | read( sigPipe[0], &someData, sizeof(someData) ); | 
|---|
| 161 |  | 
|---|
| 162 | if ( someData != 0x12345678 ) | 
|---|
| 163 | { | 
|---|
| 164 | COUT(0) << "something went wrong :(" << std::endl; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | return; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | int gdbIn[2]; | 
|---|
| 171 | int gdbOut[2]; | 
|---|
| 172 | int gdbErr[2]; | 
|---|
| 173 |  | 
|---|
| 174 | if ( pipe(gdbIn) == -1 || pipe(gdbOut) == -1 || pipe(gdbErr) == -1 ) | 
|---|
| 175 | { | 
|---|
| 176 | perror("pipe failed!\n"); | 
|---|
| 177 | kill( sigPid, SIGTERM ); | 
|---|
| 178 | waitpid( sigPid, NULL, 0 ); | 
|---|
| 179 | exit(EXIT_FAILURE); | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | int gdbPid = fork(); | 
|---|
| 183 | // this process will run gdb | 
|---|
| 184 |  | 
|---|
| 185 | if ( gdbPid == -1 ) | 
|---|
| 186 | { | 
|---|
| 187 | perror("fork failed\n"); | 
|---|
| 188 | kill( sigPid, SIGTERM ); | 
|---|
| 189 | waitpid( sigPid, NULL, 0 ); | 
|---|
| 190 | exit(EXIT_FAILURE); | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | if ( gdbPid == 0 ) | 
|---|
| 194 | { | 
|---|
| 195 | // start gdb | 
|---|
| 196 |  | 
|---|
| 197 | close(gdbIn[1]); | 
|---|
| 198 | close(gdbOut[0]); | 
|---|
| 199 | close(gdbErr[0]); | 
|---|
| 200 |  | 
|---|
| 201 | dup2( gdbIn[0], STDIN_FILENO ); | 
|---|
| 202 | dup2( gdbOut[1], STDOUT_FILENO ); | 
|---|
| 203 | dup2( gdbErr[1], STDERR_FILENO ); | 
|---|
| 204 |  | 
|---|
| 205 | execlp( "sh", "sh", "-c", "gdb", static_cast<void*>(NULL)); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | char cmd[256]; | 
|---|
| 209 | snprintf( cmd, 256, "file %s\nattach %d\nc\n", getInstance().appName.c_str(), sigPid ); | 
|---|
| 210 | write( gdbIn[1], cmd, strlen(cmd) ); | 
|---|
| 211 |  | 
|---|
| 212 | int charsFound = 0; | 
|---|
| 213 | int promptFound = 0; | 
|---|
| 214 | char byte; | 
|---|
| 215 | while ( read( gdbOut[0], &byte, 1 ) == 1 ) | 
|---|
| 216 | { | 
|---|
| 217 | if ( | 
|---|
| 218 | (charsFound == 0 && byte == '(') || | 
|---|
| 219 | (charsFound == 1 && byte == 'g') || | 
|---|
| 220 | (charsFound == 2 && byte == 'd') || | 
|---|
| 221 | (charsFound == 3 && byte == 'b') || | 
|---|
| 222 | (charsFound == 4 && byte == ')') || | 
|---|
| 223 | (charsFound == 5 && byte == ' ') | 
|---|
| 224 | ) | 
|---|
| 225 | charsFound++; | 
|---|
| 226 | else | 
|---|
| 227 | charsFound = 0; | 
|---|
| 228 |  | 
|---|
| 229 | if ( charsFound == 6 ) | 
|---|
| 230 | { | 
|---|
| 231 | promptFound++; | 
|---|
| 232 | charsFound = 0; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | if ( promptFound == 3 ) | 
|---|
| 236 | { | 
|---|
| 237 | break; | 
|---|
| 238 | } | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | int someData = 0x12345678; | 
|---|
| 242 | write( sigPipe[1], &someData, sizeof(someData) ); | 
|---|
| 243 |  | 
|---|
| 244 | write( gdbIn[1], "bt\nk\nq\n", 7 ); | 
|---|
| 245 |  | 
|---|
| 246 |  | 
|---|
| 247 | charsFound = 0; | 
|---|
| 248 | promptFound = 0; | 
|---|
| 249 | std::string bt; | 
|---|
| 250 | while ( read( gdbOut[0], &byte, 1 ) == 1 ) | 
|---|
| 251 | { | 
|---|
| 252 | bt += std::string( &byte, 1 ); | 
|---|
| 253 |  | 
|---|
| 254 | if ( | 
|---|
| 255 | (charsFound == 0 && byte == '(') || | 
|---|
| 256 | (charsFound == 1 && byte == 'g') || | 
|---|
| 257 | (charsFound == 2 && byte == 'd') || | 
|---|
| 258 | (charsFound == 3 && byte == 'b') || | 
|---|
| 259 | (charsFound == 4 && byte == ')') || | 
|---|
| 260 | (charsFound == 5 && byte == ' ') | 
|---|
| 261 | ) | 
|---|
| 262 | charsFound++; | 
|---|
| 263 | else | 
|---|
| 264 | charsFound = 0; | 
|---|
| 265 |  | 
|---|
| 266 | if ( charsFound == 6 ) | 
|---|
| 267 | { | 
|---|
| 268 | promptFound++; | 
|---|
| 269 | charsFound = 0; | 
|---|
| 270 | bt += "\n"; | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | if ( promptFound == 3 ) | 
|---|
| 274 | { | 
|---|
| 275 | break; | 
|---|
| 276 | } | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 |  | 
|---|
| 280 | waitpid( sigPid, NULL, 0 ); | 
|---|
| 281 | waitpid( gdbPid, NULL, 0 ); | 
|---|
| 282 |  | 
|---|
| 283 | int wsRemoved = 0; | 
|---|
| 284 |  | 
|---|
| 285 | while ( wsRemoved < 2 && bt.length() > 0 ) | 
|---|
| 286 | { | 
|---|
| 287 | if ( bt[1] == '\n' ) | 
|---|
| 288 | wsRemoved++; | 
|---|
| 289 | bt.erase(0, 1); | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | if ( bt.length() > 0 ) | 
|---|
| 293 | bt.erase(0, 1); | 
|---|
| 294 |  | 
|---|
| 295 | time_t now = time(NULL); | 
|---|
| 296 |  | 
|---|
| 297 | std::string timeString = | 
|---|
| 298 | "=======================================================\n" | 
|---|
| 299 | "= time: " + std::string(ctime(&now)) + | 
|---|
| 300 | "=======================================================\n"; | 
|---|
| 301 | bt.insert(0, timeString); | 
|---|
| 302 |  | 
|---|
| 303 | FILE * f = fopen( getInstance().filename.c_str(), "w" ); | 
|---|
| 304 |  | 
|---|
| 305 | if ( !f ) | 
|---|
| 306 | { | 
|---|
| 307 | perror( ( std::string( "could not append to " ) + getInstance().filename ).c_str() ); | 
|---|
| 308 | exit(EXIT_FAILURE); | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 | if ( fwrite( bt.c_str(), 1, bt.length(), f ) != bt.length() ) | 
|---|
| 312 | { | 
|---|
| 313 | COUT(0) << "could not write " << bt.length() << " byte to " << getInstance().filename << std::endl; | 
|---|
| 314 | exit(EXIT_FAILURE); | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | exit(EXIT_FAILURE); | 
|---|
| 318 | } | 
|---|
| 319 |  | 
|---|
| 320 | void SignalHandler::registerCallback( SignalCallback cb, void * someData ) | 
|---|
| 321 | { | 
|---|
| 322 | SignalCallbackRec rec; | 
|---|
| 323 | rec.cb = cb; | 
|---|
| 324 | rec.someData = someData; | 
|---|
| 325 |  | 
|---|
| 326 | callbackList.push_back(rec); | 
|---|
| 327 | } | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|
| 330 | #endif /* ORXONOX_PLATFORM_LINUX */ | 
|---|