This is the mail archive of the
gdb@sources.redhat.com
mailing list for the GDB project.
# Re: Just-in-time debugging on Linux
- From: Michael Veksler <veksler at il dot ibm dot com>
- To: Robin Rowe <rower at MovieEditor dot com>
- Cc: gdb <gdb at sources dot redhat dot com>
- Date: Sun, 09 Jun 2002 11:32:59 +0300
- Subject: # Re: Just-in-time debugging on Linux
- Reply-to: veksler at il dot ibm dot com
/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;
}