Thread: Core dumps

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    Core dumps

    Good morrow,

    I've got some code giving a seg fault intermitently, so I want to get it to core dump.

    Problem is that the seg fault occurs within a linux pthread, and its doesn't core dump. It core dumps in the main thread if I force a seg fault, but not the child.

    I just use the ulimit -c unlimited command and compile with -g.

    Any ideas? Is this possible?

    TIA, rotis23

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    well, I need a core dump, because it happens hours after the process starts, and my ssh session will no longer exist.

    I presume that I could call gcore in my seg fault signal handler.

    Any ideas on linux alternatives to gcore that actually produce a core dump?

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    actually, testing with gdb process <pid> I can't attach to the pthread processes.

    i get:
    Code:
    Attaching to program: /path/to/my_process, process 15338
    Child process unexpectedly missing: No child processes.
    
    Program terminated with signal ?, Unknown signal.
    The program no longer exists.
    (gdb) bt
    No stack.
    (gdb) quit
    Is this another Linux pthread issue?

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    seems to be gdb and linux pthreads:

    http://sources.redhat.com/ml/gdb-pat.../msg00187.html

    I got gdb 5.2 from:

    www.redhat.com/errata

    and I can attach to pthreads with gdb!!

    Thanks for your input vVv.
    Last edited by rotis23; 06-06-2003 at 09:20 AM.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    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.
    Last edited by rotis23; 06-06-2003 at 10:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enable core dumping
    By steve1_rm in forum C Programming
    Replies: 3
    Last Post: 01-21-2009, 10:41 AM
  2. buying a laptop ; intel dual core?
    By BobMcGee123 in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 07-30-2006, 02:28 PM
  3. Core dumps
    By kocika73 in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 08:14 PM
  4. program segfaults without dumping a core
    By ladar in forum C Programming
    Replies: 4
    Last Post: 04-04-2005, 11:21 AM
  5. core dump
    By kermit in forum Linux Programming
    Replies: 0
    Last Post: 08-03-2004, 06:25 PM