Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/camera/src/util/signal_handler.cc @ 10018

Last change on this file since 10018 was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 6.0 KB
Line 
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#include <assert.h>
18
19SignalHandler * SignalHandler::singletonRef = NULL;
20
21#ifndef __WIN32__
22
23#include <wait.h>
24
25SignalHandler::SignalHandler()
26{
27}
28
29/**
30 * register signal handlers for SIGSEGV and SIGABRT
31 * @param appName path to executable eg argv[0]
32 * @param fileName filename to append backtrace to
33 */
34void SignalHandler::doCatch( const std::string & appName, const std::string & fileName )
35{
36  this->appName = appName;
37  this->fileName = fileName;
38 
39  // make sure doCatch is only called once without calling dontCatch
40  assert( sigRecList.size() == 0 );
41
42  catchSignal( SIGSEGV );
43  catchSignal( SIGABRT );
44}
45
46/**
47 * restore previous signal handlers
48 */
49void SignalHandler::dontCatch()
50{
51  for ( SignalRecList::iterator it = sigRecList.begin(); it != sigRecList.end(); it++ )
52  {
53    signal( it->signal, it->handler );
54  }
55
56  sigRecList.clear();
57}
58
59/**
60 * catch signal sig
61 * @param sig signal to catch
62 */
63void SignalHandler::catchSignal( int sig )
64{
65  sig_t handler = signal( sig, SignalHandler::sigHandler );
66
67  assert( handler != SIG_ERR );
68
69  SignalRec rec;
70  rec.signal = sig;
71  rec.handler = handler;
72
73  sigRecList.push_front( rec );
74}
75
76/**
77 * sigHandler is called when receiving signals
78 * @param sig
79 */
80void SignalHandler::sigHandler( int sig )
81{
82  for ( SignalCallbackList::iterator it = SignalHandler::getInstance()->callbackList.begin(); it != SignalHandler::getInstance()->callbackList.end(); it++  )
83  {
84    (*(it->cb))( it->someData );
85  }
86
87  std::string sigName = "UNKNOWN";
88 
89  switch ( sig )
90  {
91    case SIGSEGV:
92      sigName = "SIGSEGV";
93      break;
94    case SIGABRT:
95      sigName = "SIGABRT";
96      break;
97  }
98
99  printf( "recieved signal %s\ntry to write backtrace to file orxonox.backtrace\n", sigName.c_str() );
100 
101  int sigPipe[2];
102  if ( pipe(sigPipe) == -1 )
103  {
104    perror("pipe failed!\n");
105    exit(EXIT_FAILURE);
106  }
107 
108  int sigPid = fork();
109
110  if ( sigPid == -1 )
111  {
112    perror("fork failed!\n");
113    exit(EXIT_FAILURE);
114  }
115
116  // gdb will be attached to this process
117  if ( sigPid == 0 )
118  {
119    getInstance()->dontCatch();
120    // wait for message from parent when it has attached gdb
121    int someData;
122
123    read( sigPipe[0], &someData, sizeof(someData) );
124
125    if ( someData != 0x12345678 )
126    {
127      printf("something went wrong :(\n");
128    }
129
130    return;
131  }
132
133  int gdbIn[2];
134  int gdbOut[2];
135  int gdbErr[2];
136 
137  if ( pipe(gdbIn) == -1 || pipe(gdbOut) == -1 || pipe(gdbErr) == -1 )
138  {
139    perror("pipe failed!\n");
140    kill( sigPid, SIGTERM );
141    waitpid( sigPid, NULL, 0 );
142    exit(EXIT_FAILURE);
143  }
144
145  int gdbPid = fork();
146  // this process will run gdb
147 
148  if ( gdbPid == -1 )
149  {
150    perror("fork failed\n");
151    kill( sigPid, SIGTERM );
152    waitpid( sigPid, NULL, 0 );
153    exit(EXIT_FAILURE);
154  }
155 
156  if ( gdbPid == 0 )
157  {
158    // start gdb
159   
160    close(gdbIn[1]);
161    close(gdbOut[0]);
162    close(gdbErr[0]);
163
164    dup2( gdbIn[0], STDIN_FILENO );
165    dup2( gdbOut[1], STDOUT_FILENO );
166    dup2( gdbErr[1], STDERR_FILENO );
167   
168    execlp( "sh", "sh", "-c", "gdb", (void*)NULL);
169  }
170
171  char cmd[256];
172  snprintf( cmd, 256, "file %s\nattach %d\nc\n", getInstance()->appName.c_str(), sigPid );
173  write( gdbIn[1], cmd, strlen(cmd) );
174
175  int charsFound = 0;
176  int promptFound = 0;
177  char byte;
178  while ( read( gdbOut[0], &byte, 1 ) == 1 )
179  {
180    if ( 
181      charsFound == 0 && byte == '(' ||
182      charsFound == 1 && byte == 'g' ||
183      charsFound == 2 && byte == 'd' ||
184      charsFound == 3 && byte == 'b' ||
185      charsFound == 4 && byte == ')' ||
186      charsFound == 5 && byte == ' '
187        )
188          charsFound++;
189    else
190      charsFound = 0;
191
192    if ( charsFound == 6 )
193    {
194      promptFound++;
195      charsFound = 0;
196    }
197
198    if ( promptFound == 3 )
199    {
200      break;
201    }
202  }
203 
204  int someData = 0x12345678;
205  write( sigPipe[1], &someData, sizeof(someData) );
206
207  write( gdbIn[1], "bt\nk\nq\n", 7 );
208
209 
210  charsFound = 0;
211  promptFound = 0;
212  std::string bt;
213  while ( read( gdbOut[0], &byte, 1 ) == 1 )
214  {
215    bt += std::string( &byte, 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      bt += "\n";
234    }
235
236    if ( promptFound == 3 )
237    {
238      break;
239    }
240  }
241
242
243  waitpid( sigPid, NULL, 0 );
244  waitpid( gdbPid, NULL, 0 );
245 
246  int wsRemoved = 0;
247
248  while ( wsRemoved < 2 && bt.length() > 0 )
249  {
250    if ( bt[1] == '\n' )
251      wsRemoved++;
252    bt.erase(0, 1);
253  }
254
255  if ( bt.length() > 0 )
256    bt.erase(0, 1);
257
258  time_t now = time(NULL);
259
260  std::string timeString = "\n\n\n\n"
261                           "=======================================================\n"
262                           "= time: " + std::string(ctime(&now)) +
263                           "=======================================================\n";
264  bt.insert(0, timeString);
265 
266  FILE * f = fopen( getInstance()->fileName.c_str(), "a" );
267
268  if ( !f )
269  {
270    perror( ( std::string( "could not append to " ) + getInstance()->fileName ).c_str() );
271    exit(EXIT_FAILURE);
272  }
273
274  if ( fwrite( bt.c_str(), 1, bt.length(), f ) != bt.length() )
275  {
276    printf( ( std::string("could not write %d byte to ") + getInstance()->fileName ).c_str(), bt.length());
277    exit(EXIT_FAILURE);
278  }
279
280  exit(EXIT_FAILURE);
281}
282
283void SignalHandler::registerCallback( SignalCallback cb, void * someData )
284{
285  SignalCallbackRec rec;
286  rec.cb = cb;
287  rec.someData = someData;
288 
289  callbackList.push_back(rec);
290}
291
292#endif /* __WIN32__ */
Note: See TracBrowser for help on using the repository browser.