Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7775 for code/branches


Ignore:
Timestamp:
Dec 17, 2010, 2:22:00 PM (14 years ago)
Author:
scheusso
Message:

this should reenable the SignalHandler's backtrace for more restrictive environments (e.g. Ubuntu 10.10)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network5/src/libraries/util/SignalHandler.cc

    r7457 r7775  
    3737#include <cstdlib>
    3838#include <cstring>
     39#include <sys/prctl.h>
    3940
    4041#include "Debug.h"
     
    137138      COUT(0) << "Received signal " << sigName.c_str() << std::endl << "Try to write backtrace to file orxonox_crash.log" << std::endl;
    138139
    139       int sigPipe[2];
    140       if ( pipe(sigPipe) == -1 )
    141       {
    142         perror("pipe failed!\n");
    143         exit(EXIT_FAILURE);
    144       }
    145 
    146       int sigPid = fork();
    147 
    148       if ( sigPid == -1 )
    149       {
    150         perror("fork failed!\n");
    151         exit(EXIT_FAILURE);
    152       }
    153 
    154       // gdb will be attached to this process
    155       if ( sigPid == 0 )
    156       {
    157         getInstance().dontCatch();
    158         // wait for message from parent when it has attached gdb
    159         int someData;
    160 
    161         read( sigPipe[0], &someData, sizeof(someData) );
    162 
    163         if ( someData != 0x12345678 )
    164         {
    165           COUT(0) << "something went wrong :(" << std::endl;
    166         }
    167 
    168         return;
    169       }
    170 
     140     
     141      // First start GDB which will be attached to this process later on
     142     
    171143      int gdbIn[2];
    172144      int gdbOut[2];
    173145      int gdbErr[2];
    174 
     146     
    175147      if ( pipe(gdbIn) == -1 || pipe(gdbOut) == -1 || pipe(gdbErr) == -1 )
    176148      {
    177149        perror("pipe failed!\n");
    178         kill( sigPid, SIGTERM );
    179         waitpid( sigPid, NULL, 0 );
    180150        exit(EXIT_FAILURE);
    181151      }
    182 
     152     
    183153      int gdbPid = fork();
    184154      // this process will run gdb
    185 
     155     
    186156      if ( gdbPid == -1 )
    187157      {
    188158        perror("fork failed\n");
    189         kill( sigPid, SIGTERM );
    190         waitpid( sigPid, NULL, 0 );
    191159        exit(EXIT_FAILURE);
    192160      }
    193 
     161     
    194162      if ( gdbPid == 0 )
    195163      {
    196164        // start gdb
    197 
     165       
    198166        close(gdbIn[1]);
    199167        close(gdbOut[0]);
    200168        close(gdbErr[0]);
    201 
     169       
    202170        dup2( gdbIn[0], STDIN_FILENO );
    203171        dup2( gdbOut[1], STDOUT_FILENO );
    204172        dup2( gdbErr[1], STDERR_FILENO );
    205 
     173       
    206174        execlp( "sh", "sh", "-c", "gdb", static_cast<void*>(NULL));
     175      }
     176     
     177     
     178      // Now start a fork of this process on which GDB will be attached on
     179     
     180      int sigPipe[2];
     181      if ( pipe(sigPipe) == -1 )
     182      {
     183        perror("pipe failed!\n");
     184        kill( gdbPid, SIGTERM );
     185        waitpid( gdbPid, NULL, 0 );
     186        exit(EXIT_FAILURE);
     187      }
     188
     189      int sigPid = fork();
     190
     191      if ( sigPid == -1 )
     192      {
     193        perror("fork failed!\n");
     194        kill( gdbPid, SIGTERM );
     195        waitpid( gdbPid, NULL, 0 );
     196        exit(EXIT_FAILURE);
     197      }
     198
     199      // gdb will be attached to this process
     200      if ( sigPid == 0 )
     201      {
     202        getInstance().dontCatch();
     203       
     204        // make sure gdb is allowed to attach to our PID even if there are some system restrictions
     205        if( prctl(PR_SET_PTRACER, gdbPid, 0, 0, 0) == -1 )
     206          COUT(0) << "could not set proper permissions for GDB to attach to process..." << endl;
     207       
     208        // wait for message from parent when it has attached gdb
     209        int someData;
     210
     211        if( read( sigPipe[0], &someData, sizeof(someData) ) != sizeof(someData) )
     212          COUT(0) << "something went wrong :(" << std::endl;
     213
     214        if ( someData != 0x12345678 )
     215        {
     216          COUT(0) << "something went wrong :(" << std::endl;
     217        }
     218
     219        return;
    207220      }
    208221
Note: See TracChangeset for help on using the changeset viewer.