Bug 31555 - Cannot attach to program after its main thread has exited, on Linux
Summary: Cannot attach to program after its main thread has exited, on Linux
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: gdb (show other bugs)
Version: unknown
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-03-25 18:40 UTC by Pedro Alves
Modified: 2024-04-12 17:45 UTC (History)
0 users

See Also:
Host:
Target: *-*-linux*
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pedro Alves 2024-03-25 18:40:40 UTC
On Linux, it is not possible to attach to a process after its main thread has exited.  Ptrace does not let you attach to zombie threads.  E.g., with the gdb.threads/leader-exit.exp test program:

$ gdb -p 1804363
...
Attaching to process 1804363
warning: process 1804363 is a zombie - the process has already terminated
ptrace: Operation not permitted.
(gdb)


This is possible on other systems -- at least on Windows it is.
Comment 1 Sourceware Commits 2024-04-12 17:45:21 UTC
The master branch has been updated by Pedro Alves <palves@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ec48903170926f3827144525b50ddd3c6ae3fbf0

commit ec48903170926f3827144525b50ddd3c6ae3fbf0
Author: Pedro Alves <pedro@palves.net>
Date:   Mon Mar 25 15:17:02 2024 +0000

    New testcase gdb.threads/leader-exit-attach.exp (PR threads/8153)
    
    Add a new testcase for exercising attaching to a process after its
    main thread has exited.
    
    This is not possible on Linux, the kernel does not allow attaching to
    a zombie task, so the test is kfailed there.  It is possible however
    on Windows at least, and was the scenario addressed by the Windows
    backend fix in
    https://sourceware.org/legacy-ml/gdb-patches/2003-12/msg00479.html,
    nowadays PR threads/8153, back in 2003.
    
    Passes cleanly on Cygwin.
    KFAILed on GNU/Linux native and gdbserver.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=8153
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31554
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31555
    Change-Id: Ib554f92f68c965bb4603cdf2aadb55ca45ded53b