Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/signals/src/util/signal_handler.cc @ 7361

Last change on this file since 7361 was 7361, checked in by rennerc, 18 years ago

added signalhandling class to orx

File size: 2.1 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
18SignalHandler * SignalHandler::singletonRef = NULL;
19
20SignalHandler::SignalHandler()
21{
22}
23
24void 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
33void 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
43void 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
56void 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    dup2( fd[0], STDIN_FILENO );
99    char command[256];
100    if ( getInstance()->type == GDB_RUN_WRITE_TO_FILE )
101      snprintf( command, 255, "gdb -p %d %s 1>" GDB_BT_FILE " 2>&1", pid, getInstance()->appName.c_str() );
102    else
103      snprintf( command, 255, "gdb -p %d %s", pid, getInstance()->appName.c_str() );
104    execlp( "sh", "sh", "-c", command, (void*)NULL);
105  }
106}
107
Note: See TracBrowser for help on using the repository browser.