1 # Copyright (C) 2024 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Test attaching to a program after its main thread has exited.
18 require can_spawn_for_attach
20 standard_testfile leader-exit.c
22 if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
26 set escapedbinfile [string_to_regexp ${binfile}]
28 set test_spawn_id [spawn_wait_for_attach $binfile]
29 set testpid [spawn_id_get_pid $test_spawn_id]
31 # Wait a bit for the leader thread to exit, before attaching.
34 clean_restart ${binfile}
36 # Save this early as we may not be able to talk with GDBserver anymore
37 # when we need to check it.
38 set is_gdbserver [target_is_gdbserver]
40 # True if successfully attached.
43 gdb_test_multiple "attach $testpid" "attach" {
44 -re -wrap "Attaching to process $testpid failed.*" {
45 # GNU/Linux gdbserver. Linux ptrace does not let you attach
47 setup_kfail "gdb/31555" *-*-linux*
50 -re -wrap "warning: process $testpid is a zombie - the process has already terminated.*" {
51 # Native GNU/Linux. Linux ptrace does not let you attach to
53 setup_kfail "gdb/31555" *-*-linux*
56 -re -wrap "Attaching to program: $escapedbinfile, process $testpid.*" {
62 set thread_count [get_valueof "" "\$_inferior_thread_count" -1]
64 if {$thread_count == -1} {
65 kill_wait_spawned_process $test_spawn_id
70 # Check that we have at least one thread. We can't assume there
71 # will only be exactly one thread, because on some systems, like
72 # Cygwin, the runtime spawns extra threads. Also, on Windows,
73 # attaching always injects one extra thread.
74 gdb_assert {$thread_count >= 1}
76 gdb_assert {$thread_count == 0}
79 kill_wait_spawned_process $test_spawn_id