Bug 48183

Summary: [patch] gdb(1) on a core(5)-file from a threaded process can't see
Product: Base System Reporter: Peter Edwards <peter.edwards>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me CC: emaste
Priority: Normal    
Version: 5.0-CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description Peter Edwards 2003-02-11 19:10:09 UTC
This was originally posted to -current in
<20030207124244.74bf9df7.peter.edwards@openet-telecom.com>

I figured i'd record it here so it's not forgotten.

gdb doesn't find threads in corefiles: The support was just missing.
Attached is a patch, sample program, and gdb output when running it.

How-To-Repeat: 
Compile this:
=09
#include <sys/types.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <err.h>

void *thr(void *a)
{
    printf("started thread %d\n", (int)a);
    pause();
    return 0;
}

int main(int argc, char **argv)
{
    const int THREADCOUNT =3D 4;
    int i;
    pthread_t threads[THREADCOUNT];

    for (i =3D 0; i < THREADCOUNT; i++)
	if (pthread_create(&threads[i], 0, thr, (void *)i) !=3D 0)
	    errx(-1, "cannot create thread");
    sleep(1);
    kill(0, SIGBUS);
    return 0;
}

like this:

 petere@rocklobster$ cc -o threadcore -Wall -g -pthread threadcore.c

then do this:
 petere@rocklobster$ ./threadcore
 started thread 0
 started thread 1
 started thread 2
 started thread 3
 zsh: bus error (core dumped)  ./threadcore
 petere@rocklobster$ gdb threadcore threadcore.core
 GNU gdb 5.2.1 (FreeBSD)
 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-undermydesk-freebsd"...
 Core was generated by `threadcore'.
 Program terminated with signal 10, Bus error.
 Reading symbols from /usr/lib/libc_r.so.5...done.
 Loaded symbols for /usr/lib/libc_r.so.5
 Reading symbols from /usr/lib/libc.so.5...done.
 Loaded symbols for /usr/lib/libc.so.5
 Reading symbols from /usr/libexec/ld-elf.so.1...done.
 Loaded symbols for /usr/libexec/ld-elf.so.1
 #0  0x280d5463 in kill () from /usr/lib/libc.so.5
 (gdb) i thr
 * 1 process 41359  0x280d5463 in kill () from /usr/lib/libc.so.5
 (gdb) quit

After applying the patch, try this:
 petere@rocklobster$ /usr/src/gnu/usr.bin/binutils/gdb/gdb threadcore
 threadcore.core GNU gdb 5.2.1 (FreeBSD)
 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-undermydesk-freebsd"...
 Core was generated by `threadcore'.
 Program terminated with signal 10, Bus error.
 Reading symbols from /usr/lib/libc_r.so.5...done.
 Loaded symbols for /usr/lib/libc_r.so.5
 Reading symbols from /usr/lib/libc.so.5...done.
 Loaded symbols for /usr/lib/libc.so.5
 Reading symbols from /usr/libexec/ld-elf.so.1...done.
 Loaded symbols for /usr/libexec/ld-elf.so.1
 #0  0x280d5463 in kill () from /usr/lib/libc.so.5
 (gdb) i thr
   7 Process 41359  0x280d5463 in kill () from /usr/lib/libc.so.5
   6 Process 41359  0x2807b7ec in _thread_kern_sched () from
/usr/lib/libc_r.so.5
   5 Process 41359  0x2807b7ec in _thread_kern_sched () from
/usr/lib/libc_r.so.5
   4 Process 41359  0x2807b7ec in _thread_kern_sched () from
/usr/lib/libc_r.so.5
   3 Process 41359  0x2807b7ec in _thread_kern_sched () from
/usr/lib/libc_r.so.5
   2 Process 41359  0x2807b7ec in _thread_kern_sched () from
/usr/lib/libc_r.so.5
 * 1 Process 41359  0x280d5463 in kill () from /usr/lib/libc.so.5
 (gdb)=20

Enjoy,
Peter.
Comment 1 david 2004-03-23 15:10:06 UTC
Hi,

I was not able to apply this patch for the gdb used on FreeBSD 4.9. Does
anyone know of a similar patch for that gdb version?

\David
Comment 2 Peter Edwards 2004-03-23 15:17:53 UTC
David Eriksson wrote:

>Hi,
>
>I was not able to apply this patch for the gdb used on FreeBSD 4.9. Does
>anyone know of a similar patch for that gdb version?
>
>\David
>
>  
>
I'll try and roll an update of it towards the end of the week. Things 
are a bit hectic at my day/night job for the next few days, so be 
patient. The patch is more additions than anything else, so I'm 
surprised you had that much grief with it.
Comment 3 Marcel Moolenaar freebsd_committer freebsd_triage 2004-07-17 02:28:45 UTC
Responsible Changed
From-To: freebsd-bugs->marcel

Let's collect the GDB defects under my wing. This PR may actually be 
a non-issue on 5.x, but I need to check on 4.x
Comment 4 david 2005-12-08 15:58:45 UTC
Hi again,

As far as I can see this also applies to gdb-6_20040810 on FreeBSD
4.10-STABLE.

\David
Comment 5 Marcel Moolenaar freebsd_committer freebsd_triage 2011-02-21 18:43:15 UTC
Responsible Changed
From-To: marcel->freebsd-bugs

Assign back to the pool. I haven't been working on it and it's unlikely 
I will in the near future.
Comment 6 Ed Maste freebsd_committer freebsd_triage 2014-07-03 20:17:40 UTC
This has been addressed years ago.  I'm not sure exactly when it was fixed, but it has been working since at least somewhere in the 6.x lifetime.

(gdb) info threads
* 5 Thread 801007400 (LWP 102211/threadcore)  0x0000000800b45edc in kill ()
    at kill.S:3
  4 Thread 801007800 (LWP 102162/threadcore)  0x0000000800aa0dac in sigsuspend
    () at sigsuspend.S:3
  3 Thread 801007c00 (LWP 102194/threadcore)  0x0000000800aa0dac in sigsuspend
    () at sigsuspend.S:3
  2 Thread 801008000 (LWP 102291/threadcore)  0x0000000800aa0dac in sigsuspend
    () at sigsuspend.S:3
  1 Thread 801008400 (LWP 102387/threadcore)  0x0000000800aa0dac in sigsuspend
    () at sigsuspend.S:3