For those who want to debug seg faults within pthreads on Linux; this is how I did it.
Please excuse the machine name
Code:
tossface:~# cat new2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
void *foo(void *bar)
{
char *p = NULL;
printf("child pid: %d\n", getpid());
*p = 0;
return NULL;
}
void segfault(int sig)
{
printf("segfault caught: %d\n", getpid()); getchar();
exit(0);
}
int main(int argc, char *argv[])
{
pthread_t thr;
int rc;
struct sigaction segact;
char *p = NULL;
segact.sa_handler = segfault;
sigemptyset(&segact.sa_mask);
segact.sa_flags = 0;
sigaction(SIGSEGV, &segact, 0);
if (argc > 1)
{
*p = 0;
}
if ((rc = pthread_create(&thr, NULL, foo, NULL)) != 0)
{
fprintf(stderr, "pthread_create: %s\n", strerror(rc));
}
pthread_exit(NULL);
}
tossface:~# gcc -g new2.c -o new -lpthread
tossface:~# ./new
child pid: 24725
segfault caught: 24725
tossface:~#
When the segmentation fault occurs, it's caught and the process remains running until getchar suceeds. While the code is waiting for keyboard input I used gdb in another session:
Code:
tossface:~# gdb new 24725
GNU gdb Red Hat Linux (5.2-2)
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
Attaching to program: /root/new, process 24725
Reading symbols from /lib/i686/libpthread.so.0...done.
[New Thread 2049 (LWP 24724)]
[New Thread 1026 (LWP 24725)]
Loaded symbols for /lib/i686/libpthread.so.0
Reading symbols from /lib/i686/libc.so.6...done.
Loaded symbols for /lib/i686/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
0x40117544 in __libc_read () at __libc_read:-1
-1 __libc_read: No such file or directory.
in __libc_read
(gdb) bt
#0 0x40117544 in __libc_read () at __libc_read:-1
#1 0x400365f8 in __DTOR_END__ () from /lib/i686/libpthread.so.0
#2 0x400b3226 in _IO_file_read (fp=0x4016b000, buf=0x40018000, size=1024) at fileops.c:838
#3 0x400b242d in _IO_new_file_underflow (fp=0x4016b000) at fileops.c:542
#4 0x400b4cc9 in _IO_default_uflow (fp=0x4016b000) at genops.c:420
#5 0x400b3cff in __uflow (fp=0x4016b000) at genops.c:377
#6 0x400af22e in getchar () at getchar.c:40
#7 0x08048761 in segfault (sig=11) at new2.c:17
#8 0x4002b8d5 in pthread_sighandler (signo=11, ctx=
{gs = 23, __gsh = 0, fs = 0, __fsh = 0, es = 43, __esh = 0, ds = 43, __dsh = 0, edi = 0,
esi = 1083644896, ebp = 1083644644, esp = 1083644636, ebx = 1073964536,
edx = 0, ecx = 0, eax = 0, trapno = 14, err = 6, eip = 134514478, cs = 35, __csh = 0,
eflags = 66050, esp_at_signal = 1083644636, ss = 43, __ssh = 0, fpstate = 0x0,
oldmask = 2147483648, cr2 = 0}) at signals.c:97
#9 <signal handler called>
#10 0x0804872e in foo (bar=0x0) at new2.c:11
#11 0x40028b9c in pthread_start_thread (arg=0x40971be0) at manager.c:274
(gdb) quit
The program is running. Quit anyway (and detach it)? (y or n) y
Detaching from program: /root/new, process 24725
tossface:~#
As you can see the problem was traced back to foo, line 11.