Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/util/signal_handler.cc @ 9679

Last change on this file since 9679 was 8293, checked in by bensch, 18 years ago

orxonox/trunk: merged the osX-branche back here
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/osx . -r7763:HEAD

conflicts resolved, and everything is working as expected (or at least i hope so :) )

File size: 2.6 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
23SignalHandler::SignalHandler()
24{
25}
26
27void SignalHandler::doCatch( std::string appName, GdbRunType type )
28{
29  this->type = type;
30  this->appName = appName;
31
32  catchSignal( SIGSEGV );
33  catchSignal( SIGABRT );
34}
35
36void SignalHandler::dontCatch()
37{
38  for ( SignalRecList::iterator it = sigRecList.begin(); it != sigRecList.end(); it++ )
39  {
40    signal( it->signal, it->handler );
41  }
42
43  sigRecList.clear();
44}
45
46void SignalHandler::catchSignal( int sig )
47{
48  sig_t handler = signal( sig, SignalHandler::sigHandler );
49
50  assert( handler != SIG_ERR );
51
52  SignalRec rec;
53  rec.signal = sig;
54  rec.handler = handler;
55
56  sigRecList.push_front( rec );
57}
58
59void SignalHandler::sigHandler( int sig )
60{
61  for ( SignalCallbackList::iterator it = SignalHandler::getInstance()->callbackList.begin(); it != SignalHandler::getInstance()->callbackList.end(); it++  )
62  {
63    (*(it->cb))( it->someData );
64  }
65
66  std::string sigName = "UNKNOWN";
67 
68  switch ( sig )
69  {
70    case SIGSEGV:
71      sigName = "SIGSEGV";
72      break;
73    case SIGABRT:
74      sigName = "SIGABRT";
75      break;
76  }
77
78  printf( "recieved signal %s\ntry to write backtrace to file orxonox.backtrace\n", sigName.c_str() );
79
80  int fd[2];
81
82  assert( pipe(fd) != -1 );
83
84  int pid = fork();
85
86  assert( pid != -1 );
87
88
89  if ( pid == 0 )
90  {
91    getInstance()->dontCatch();
92    sleep(2);
93
94    return;
95  }
96  else
97  {
98    getInstance()->dontCatch();
99    if ( getInstance()->type == GDB_RUN_WRITE_TO_FILE && fork() == 0 )
100    {
101      sleep(1);
102      write( fd[1], "c\nbt\nq\n", 7 );
103
104      exit(0);
105    }
106
107    char command[256];
108    if ( getInstance()->type == GDB_RUN_WRITE_TO_FILE )
109    {
110      dup2( fd[0], STDIN_FILENO );
111      snprintf( command, 255, "gdb -p %d %s 1>>" GDB_BT_FILE " 2>&1", pid, getInstance()->appName.c_str() );
112    }
113    else
114      snprintf( command, 255, "gdb -p %d %s", pid, getInstance()->appName.c_str() );
115    execlp( "sh", "sh", "-c", command, (void*)NULL);
116  }
117}
118
119void SignalHandler::registerCallback( SignalCallback cb, void * someData )
120{
121  SignalCallbackRec rec;
122  rec.cb = cb;
123  rec.someData = someData;
124 
125  callbackList.push_back(rec);
126}
127
128#endif /* __WIN32__ */
Note: See TracBrowser for help on using the repository browser.