From peter.edwards@openet-telecom.com  Tue Feb 11 11:05:07 2003
Return-Path: <peter.edwards@openet-telecom.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9BDF237B401
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 11 Feb 2003 11:05:07 -0800 (PST)
Received: from mail.openet-telecom.com (mail.openet-telecom.com [62.17.151.60])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 43D2443F85
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 11 Feb 2003 11:05:05 -0800 (PST)
	(envelope-from peter.edwards@openet-telecom.com)
Received: from mail.openet-telecom.com (unverified) by frodo.openet-telecom.lan
 (Content Technologies SMTPRS 4.2.10) with ESMTP id <T605aaf3fbb0a000012073@frodo.openet-telecom.lan> for <FreeBSD-gnats-submit@freebsd.org>;
 Tue, 11 Feb 2003 19:04:54 +0000
Received: from rocklobster.openet-telecom.lan (10.0.0.40) by mail.openet-telecom.com (NPlex 6.5.027)
        id 3DEC869E0002F0AE for FreeBSD-gnats-submit@freebsd.org; Tue, 11 Feb 2003 19:08:59 +0000
Message-Id: <20030211190913.247272d3.peter.edwards@openet-telecom.com>
Date: Tue, 11 Feb 2003 19:09:13 +0000
From: Peter Edwards <peter.edwards@openet-telecom.com>
To: FreeBSD-gnats-submit@freebsd.org
Subject: [patch] gdb on a corefile from a threaded process can't see

>Number:         48183
>Category:       bin
>Synopsis:       [patch] gdb(1) on a core(5)-file from a threaded process can't see
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Feb 11 11:10:09 PST 2003
>Closed-Date:    
>Last-Modified:  Mon Feb 21 18:45:22 UTC 2011
>Originator:     Peter Edwards
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
>Environment:
System: FreeBSD rocklobster 5.0-CURRENT FreeBSD 5.0-CURRENT #4: Tue Feb 11
09:17:38 GMT 2003
petere@archie:/pub/FreeBSD/obj/pub/FreeBSD/current/src/sys/ROCKLOBSTER
i386

>Description:
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.

>Fix:

Index: freebsd-uthread.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file:
/pub/FreeBSD/development/FreeBSD-CVS/src/gnu/usr.bin/binutils/gdb/freebsd
-uthread.c,v
retrieving revision 1.10
diff -u -r1.10 freebsd-uthread.c
--- freebsd-uthread.c	4 Jan 2003 17:35:54 -0000	1.10
+++ freebsd-uthread.c	7 Feb 2003 12:34:19 -0000
@@ -46,6 +46,10 @@
 #include <sys/stat.h>
 #include "gdbcore.h"
=20
+int coreops_suppress_target =3D 1; /* Ugh. Override the version in
corelow.c. */
+extern struct target_ops core_ops;	/* target vector for corelow.c */
+static struct target_ops orig_core_ops;	/* target vector for corelow.c */
+
 extern int child_suppress_run;
 extern struct target_ops child_ops; /* target vector for inftarg.c */
=20
@@ -60,6 +64,7 @@
=20
 /* Pointer to the next function on the objfile event chain.  */
 static void (*target_new_objfile_chain) (struct objfile *objfile);
+static void freebsd_uthread_find_new_threads (void);
=20
 static void freebsd_uthread_resume PARAMS ((ptid_t pid, int step,
 					enum target_signal signo));
@@ -472,7 +477,10 @@
=20
   if (freebsd_uthread_attaching || TIDGET(inferior_ptid) =3D=3D 0)
     {
-      child_ops.to_fetch_registers (regno);
+      if (target_has_execution)
+	child_ops.to_fetch_registers (regno);
+      else
+	orig_core_ops.to_fetch_registers (regno);
       return;
     }
=20
@@ -481,7 +489,10 @@
=20
   if (active)
     {
-      child_ops.to_fetch_registers (regno);
+      if (target_has_execution)
+	child_ops.to_fetch_registers (regno);
+      else
+	orig_core_ops.to_fetch_registers (regno);
       return;
     }
=20
@@ -501,8 +512,12 @@
=20
   for (regno =3D first_regno; regno <=3D last_regno; regno++)
     {
-      if (regmap[regno] =3D=3D -1)
-	child_ops.to_fetch_registers (regno);
+      if (regmap[regno] =3D=3D -1) {
+	if (target_has_execution)
+	  child_ops.to_fetch_registers (regno);
+	else
+	  orig_core_ops.to_fetch_registers (regno);
+      }
       else
 	if (thread)
 	  supply_register (regno, (char*) &regbase[regmap[regno]]);
@@ -683,6 +698,11 @@
   LOOKUP_VALUE(PS_RUNNING);
   LOOKUP_VALUE(PS_DEAD);
=20
+  if (!target_has_execution) {
+    read_thread_offsets();
+    freebsd_uthread_find_new_threads();
+  }
+
   freebsd_uthread_active =3D 1;
 }
=20
@@ -731,6 +751,27 @@
   return ret;
 }
=20
+static int
+freebsd_utcore_thread_alive (ptid_t ptid)
+{
+    return 1;
+}
+
+static void
+freebsd_utcore_attach (char *args, int from_tty)
+{
+  orig_core_ops.to_attach (args, from_tty);
+  push_target (&core_ops);
+  freebsd_uthread_attaching =3D 1;
+}
+
+static void
+freebsd_utcore_detach (char *args, int from_tty)
+{
+  unpush_target (&core_ops);
+  orig_core_ops.to_detach (args, from_tty);
+}
+
 static void
 freebsd_uthread_stop (void)
 {
@@ -874,12 +915,41 @@
   freebsd_uthread_vec.get_thread_info =3D freebsd_uthread_get_thread_info;
 #endif
 }
+=0C
+
+
+static void
+init_freebsd_core_ops ()
+{
+  orig_core_ops =3D core_ops;
+  core_ops.to_shortname =3D "freebsd-uthreads (corefile)";
+  core_ops.to_longname =3D "FreeBSD uthreads (corefile)";
+  core_ops.to_doc =3D "FreeBSD user threads support (for corefiles).";
+  core_ops.to_has_all_memory =3D 0;
+  core_ops.to_has_memory =3D 1;
+  core_ops.to_has_stack =3D 1;
+  core_ops.to_has_registers =3D 1;
+  core_ops.to_has_execution =3D 0;
+  core_ops.to_has_thread_control =3D tc_none;
+  core_ops.to_magic =3D OPS_MAGIC;
+  core_ops.to_pid_to_str =3D freebsd_uthread_pid_to_str;
+  core_ops.to_thread_alive =3D freebsd_utcore_thread_alive;
+  core_ops.to_attach =3D freebsd_utcore_attach;
+  core_ops.to_detach =3D freebsd_utcore_detach;
+  core_ops.to_stratum =3D core_stratum;
+  core_ops.to_find_new_threads =3D freebsd_uthread_find_new_threads;
+  core_ops.to_fetch_registers =3D freebsd_uthread_fetch_registers;
+  core_ops.to_sections =3D 0;
+  core_ops.to_sections_end =3D 0;
+}
=20
 void
 _initialize_freebsd_uthread ()
 {
   init_freebsd_uthread_ops ();
+  init_freebsd_core_ops ();
   add_target (&freebsd_uthread_ops);
+  add_target (&core_ops);
=20
   target_new_objfile_chain =3D target_new_objfile_hook;
   target_new_objfile_hook =3D freebsd_uthread_new_objfile;
>Release-Note:
>Audit-Trail:

From: David Eriksson <david@2good.nu>
To: freebsd-gnats-submit@FreeBSD.org,
	peter.edwards@openet-telecom.com
Cc:  
Subject: Re: gnu/48183: [patch] gdb on a corefile from a threaded process
	can't see
Date: Tue, 23 Mar 2004 16:10:06 +0100

 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
 

From: Peter Edwards <peter.edwards@openet-telecom.com>
To: David Eriksson <david@2good.nu>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: gnu/48183: [patch] gdb on a corefile from a threaded process
 can't see
Date: Tue, 23 Mar 2004 15:17:53 +0000

 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.
Responsible-Changed-From-To: freebsd-bugs->marcel 
Responsible-Changed-By: marcel 
Responsible-Changed-When: Sat Jul 17 01:28:45 GMT 2004 
Responsible-Changed-Why:  
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 

http://www.freebsd.org/cgi/query-pr.cgi?pr=48183 

From: David Eriksson <david@2good.nu>
To: peter.edwards@openet-telecom.com, bug-followup@FreeBSD.org
Cc: obrien@FreeBSD.org
Subject: Re: bin/48183: [patch] gdb on a corefile from a threaded process
	can't see
Date: Thu, 08 Dec 2005 16:58:45 +0100

 Hi again,
 
 As far as I can see this also applies to gdb-6_20040810 on FreeBSD
 4.10-STABLE.
 
 \David
 
Responsible-Changed-From-To: marcel->freebsd-bugs 
Responsible-Changed-By: marcel 
Responsible-Changed-When: Mon Feb 21 18:43:15 UTC 2011 
Responsible-Changed-Why:  
Assign back to the pool. I haven't been working on it and it's unlikely 
I will in the near future.  

http://www.freebsd.org/cgi/query-pr.cgi?pr=48183 
>Unformatted:
 >threads. Severity:	serious
