- Timestamp:
- Dec 17, 2010, 2:22:00 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network5/src/libraries/util/SignalHandler.cc
r7457 r7775 37 37 #include <cstdlib> 38 38 #include <cstring> 39 #include <sys/prctl.h> 39 40 40 41 #include "Debug.h" … … 137 138 COUT(0) << "Received signal " << sigName.c_str() << std::endl << "Try to write backtrace to file orxonox_crash.log" << std::endl; 138 139 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 171 143 int gdbIn[2]; 172 144 int gdbOut[2]; 173 145 int gdbErr[2]; 174 146 175 147 if ( pipe(gdbIn) == -1 || pipe(gdbOut) == -1 || pipe(gdbErr) == -1 ) 176 148 { 177 149 perror("pipe failed!\n"); 178 kill( sigPid, SIGTERM );179 waitpid( sigPid, NULL, 0 );180 150 exit(EXIT_FAILURE); 181 151 } 182 152 183 153 int gdbPid = fork(); 184 154 // this process will run gdb 185 155 186 156 if ( gdbPid == -1 ) 187 157 { 188 158 perror("fork failed\n"); 189 kill( sigPid, SIGTERM );190 waitpid( sigPid, NULL, 0 );191 159 exit(EXIT_FAILURE); 192 160 } 193 161 194 162 if ( gdbPid == 0 ) 195 163 { 196 164 // start gdb 197 165 198 166 close(gdbIn[1]); 199 167 close(gdbOut[0]); 200 168 close(gdbErr[0]); 201 169 202 170 dup2( gdbIn[0], STDIN_FILENO ); 203 171 dup2( gdbOut[1], STDOUT_FILENO ); 204 172 dup2( gdbErr[1], STDERR_FILENO ); 205 173 206 174 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; 207 220 } 208 221
Note: See TracChangeset
for help on using the changeset viewer.