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" |
---|
17 | |
---|
18 | SignalHandler * SignalHandler::singletonRef = NULL; |
---|
19 | |
---|
20 | SignalHandler::SignalHandler() |
---|
21 | { |
---|
22 | } |
---|
23 | |
---|
24 | void SignalHandler::doCatch( std::string appName, GdbRunType type ) |
---|
25 | { |
---|
26 | this->type = type; |
---|
27 | this->appName = appName; |
---|
28 | |
---|
29 | catchSignal( SIGSEGV ); |
---|
30 | catchSignal( SIGABRT ); |
---|
31 | } |
---|
32 | |
---|
33 | void SignalHandler::dontCatch() |
---|
34 | { |
---|
35 | for ( SignalRecList::iterator it = sigRecList.begin(); it != sigRecList.end(); it++ ) |
---|
36 | { |
---|
37 | signal( it->signal, it->handler ); |
---|
38 | } |
---|
39 | |
---|
40 | sigRecList.clear(); |
---|
41 | } |
---|
42 | |
---|
43 | void SignalHandler::catchSignal( int sig ) |
---|
44 | { |
---|
45 | sighandler_t handler = signal( sig, SignalHandler::sigHandler ); |
---|
46 | |
---|
47 | assert( handler != SIG_ERR ); |
---|
48 | |
---|
49 | SignalRec rec; |
---|
50 | rec.signal = sig; |
---|
51 | rec.handler = handler; |
---|
52 | |
---|
53 | sigRecList.push_front( rec ); |
---|
54 | } |
---|
55 | |
---|
56 | void SignalHandler::sigHandler( int sig ) |
---|
57 | { |
---|
58 | std::string sigName = "UNKNOWN"; |
---|
59 | |
---|
60 | switch ( sig ) |
---|
61 | { |
---|
62 | case SIGSEGV: |
---|
63 | sigName = "SIGSEGV"; |
---|
64 | break; |
---|
65 | case SIGABRT: |
---|
66 | sigName = "SIGABRT"; |
---|
67 | break; |
---|
68 | } |
---|
69 | |
---|
70 | printf( "recieved signal %s\ntry to write backtrace to file orxonox.backtrace\n", sigName.c_str() ); |
---|
71 | |
---|
72 | int fd[2]; |
---|
73 | |
---|
74 | assert( pipe(fd) != -1 ); |
---|
75 | |
---|
76 | int pid = fork(); |
---|
77 | |
---|
78 | assert( pid != -1 ); |
---|
79 | |
---|
80 | if ( pid == 0 ) |
---|
81 | { |
---|
82 | getInstance()->dontCatch(); |
---|
83 | |
---|
84 | sleep(2); |
---|
85 | |
---|
86 | |
---|
87 | return; |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | if ( getInstance()->type == GDB_RUN_WRITE_TO_FILE && fork() == 0 ) |
---|
92 | { |
---|
93 | sleep(1); |
---|
94 | write( fd[1], "c\nbt\nq\n", 7 ); |
---|
95 | |
---|
96 | exit(0); |
---|
97 | } |
---|
98 | |
---|
99 | char command[256]; |
---|
100 | if ( getInstance()->type == GDB_RUN_WRITE_TO_FILE ) |
---|
101 | { |
---|
102 | dup2( fd[0], STDIN_FILENO ); |
---|
103 | snprintf( command, 255, "gdb -p %d %s 1>" GDB_BT_FILE " 2>&1", pid, getInstance()->appName.c_str() ); |
---|
104 | } |
---|
105 | else |
---|
106 | snprintf( command, 255, "gdb -p %d %s", pid, getInstance()->appName.c_str() ); |
---|
107 | execlp( "sh", "sh", "-c", command, (void*)NULL); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|