This is the mail archive of the gdb-prs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug threads/10838] New: attach;detach not idempotent under Linux/threads


Attach followed by detach leaves libpthread with some event reporting
enabled.

If any thread exits after detach, all subsequent attach;continue will fail
with "Cannot get thread event message: debugger service failed" error.

Test case:

#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <syscall.h>
#include <unistd.h>

void *fn (void *p)
{
  pid_t tid = syscall(SYS_gettid);
  printf ("Thread %d starts.\n", tid);
  sleep (2);
  printf ("Thread %d exits.\n", tid);
  return NULL;
}

int main ()
{
  pid_t pid = getpid ();

  printf ("Execute:\ngdb -p %d -ex 'detach' -ex 'shell sleep 3' -ex 'attach %d'
-ex cont\n", pid, pid);
  while (1)
    {
      pthread_t tid;

      assert (0 == pthread_create (&tid, NULL, fn, NULL));
      assert (0 == pthread_join (tid, NULL));
    }
  return 0;
}

gcc -g -pthread t.c && ./a.out
Execute:
gdb -p 26361 -ex 'detach' -ex 'shell sleep 3' -ex 'attach 26361' -ex cont
Thread 26362 starts.
Thread 26362 exits.
Thread 26363 starts.
Thread 26363 exits.
Thread 26364 starts.
Thread 26364 exits.
Thread 26366 starts.
Thread 26366 exits.
Thread 26367 starts.
Thread 26367 exits.
Thread 26369 starts.
Thread 26369 exits.
Thread 26370 starts.
Thread 26370 exits.
Thread 26376 starts.
Thread 26376 exits.
Thread 26378 starts.
Thread 26378 exits.

### in another window ###

 ./gdb -nx -p 26361 -ex 'detach' -ex 'shell sleep 3' -ex 'attach 26361' -ex cont

GNU gdb (GDB) 7.0.50.20091023-cvs
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Attaching to process 26361
Reading symbols from /home/ppluzhnikov/bugs/2209373/a.out...done.
Reading symbols from /lib/libpthread.so.0...Reading symbols from
/usr/lib/debug/lib/libpthread-2.7.so...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New Thread 0x412d1950 (LWP 26370)]
(no debugging symbols found)...done.
Loaded symbols for /lib/libpthread.so.0
Reading symbols from /lib/libc.so.6...Reading symbols from
/usr/lib/debug/lib/libc-2.7.so...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from
/usr/lib/debug/lib/ld-2.7.so...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007f552773e796 in pthread_join () from /lib/libpthread.so.0
Detaching from program: /home/ppluzhnikov/bugs/2209373/a.out, process 26361
Attaching to program: /home/ppluzhnikov/bugs/2209373/a.out, process 26361
Reading symbols from /lib/libpthread.so.0...Reading symbols from
/usr/lib/debug/lib/libpthread-2.7.so...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New Thread 0x412d1950 (LWP 26378)]
(no debugging symbols found)...done.
Loaded symbols for /lib/libpthread.so.0
Reading symbols from /lib/libc.so.6...Reading symbols from
/usr/lib/debug/lib/libc-2.7.so...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from
/usr/lib/debug/lib/ld-2.7.so...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007f552773e796 in pthread_join () from /lib/libpthread.so.0
Continuing.
Cannot get thread event message: debugger service failed
(gdb) 


AFAICT, what's happening is:
- a live at the first attach thread 26370 has TD_DEATH reporting enabled
  (by GDB).
- GDB detaches.
- thread 26370 exits, and is enqueued on __nptl_last_event as TD_DEATH event
  (__nptl_last_event == 0x412d1950).

- a new thread 26378 is created, but *reuses* the same thread descriptor
  0x412d1950. Its eventbuf is copied from parent (which has no events
  pending) in __pthread_create_2_1:

    /* The debug events are inherited from the parent.  */
    pd->eventbuf = self->eventbuf;

  Voila: __nptl_last_event->eventbuf.eventnum == 0, and that triggers the
  error in td_ta_event_getmsg:
  
    /* If the structure is on the list there better be an event recorded.  */
    if ((int) (uintptr_t) eventnum == TD_EVENT_NONE)
      return TD_DBERR;

-- 
           Summary: attach;detach not idempotent under Linux/threads
           Product: gdb
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: threads
        AssignedTo: ppluzhnikov at google dot com
        ReportedBy: ppluzhnikov at google dot com
                CC: gdb-prs at sourceware dot org
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


http://sourceware.org/bugzilla/show_bug.cgi?id=10838

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]