Home | History | Annotate | Line # | Download | only in gdb.threads
      1 # Copyright (C) 2024 Free Software Foundation, Inc.
      2 
      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.
      7 #
      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.
     12 #
     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/>.
     15 
     16 # Test attaching to a program after its main thread has exited.
     17 
     18 require can_spawn_for_attach
     19 
     20 standard_testfile leader-exit.c
     21 
     22 if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
     23     return
     24 }
     25 
     26 set escapedbinfile [string_to_regexp ${binfile}]
     27 
     28 set test_spawn_id [spawn_wait_for_attach $binfile]
     29 set testpid [spawn_id_get_pid $test_spawn_id]
     30 
     31 # Wait a bit for the leader thread to exit, before attaching.
     32 sleep 2
     33 
     34 clean_restart ${binfile}
     35 
     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]
     39 
     40 # True if successfully attached.
     41 set attached 0
     42 
     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
     46 	# to zombie threads.
     47 	setup_kfail "gdb/31555" *-*-linux*
     48 	fail $gdb_test_name
     49     }
     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
     52 	# zombie threads.
     53 	setup_kfail "gdb/31555" *-*-linux*
     54 	fail $gdb_test_name
     55     }
     56     -re -wrap "Attaching to program: $escapedbinfile, process $testpid.*" {
     57 	pass $gdb_test_name
     58 	set attached 1
     59     }
     60 }
     61 
     62 set thread_count [get_valueof "" "\$_inferior_thread_count" -1]
     63 
     64 if {$thread_count == -1} {
     65     kill_wait_spawned_process $test_spawn_id
     66     return
     67 }
     68 
     69 if {$attached} {
     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}
     75 } else {
     76     gdb_assert {$thread_count == 0}
     77 }
     78 
     79 kill_wait_spawned_process $test_spawn_id
     80