# Re: Just-in-time debugging on Linux

Michael Veksler veksler@il.ibm.com
Sun Jun 9 01:30:00 GMT 2002


/References/: <007a01c20e82$daf93e20$0301a8c0@rowboat 
<http://sources.redhat.com/ml/gdb/2002-06/msg00063.html>> 
<20020608172614.GA16912@redhat.com 
<http://sources.redhat.com/ml/gdb/2002-06/msg00064.html>>

 > Christopher,
 >
 > > On windows you'd do:
 > >
 > > set cygwin=error_start=x:/path/to/gdb.exe
 >
 > Thanks lots, but I meant Linux. Does anyone know the trick to launch gdb
 > automatically there?
 >
 > Robin

Try the following code:

#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>

static char* exec_name="";

static void crash_handler(int sig)
{
   int status=0;
   int pid;
   char * gdb_array[]={"gdb", exec_name, "PID", NULL};
   char pid_str[40];

   sprintf(pid_str, "%d%c", getpid(), '\0');
   gdb_array[2]= pid_str;

   pid= fork();

   if (pid < 0)    /* error */
     abort();
   else if (pid)   /* parent */
   {
     sleep(60);   /* Give GDB time to attach */
     _exit(1);     /* you can skip this line by telling gdb to "return" */
   }
   else            /* child */
     execvp("gdb", gdb_array);
}
void register_gdb()
{
   signal(SIGQUIT, crash_handler);  /* Normally got from Ctrl-\ */
   signal(SIGILL,  crash_handler);
   signal(SIGTRAP, crash_handler);
   signal(SIGABRT, crash_handler);
   signal(SIGFPE,  crash_handler);
   signal(SIGBUS,  crash_handler);
   signal(SIGSEGV, crash_handler);  /* This is the most common crash */
   signal(SIGSYS,  crash_handler);
}

void crash_segv()
{
   int *p=0;
   *p=1;
}

int main(int argc, char *argv[])
{
   exec_name=argv[0];
   register_gdb();
   crash_segv();
   return 0;
}




More information about the Gdb mailing list