Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/signal_handler.cc @ 7445

Last change on this file since 7445 was 7440, checked in by bensch, 19 years ago

orxonox/trunk: merged the Signal-Handler-Branche back to the TRUNK

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