1 # Copyright 2021-2022 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 multi-threaded process, in all combinations of:
18 # - set non-stop on/off
19 # - maint target non-stop off/on
20 # - "attach" vs "attach &"
22 if {![can_spawn_for_attach]} {
28 # The test proper. See description above.
30 proc test {target_non_stop non_stop cmd} {
31 global binfile srcfile
36 # Number of threads started by the program.
39 save_vars { GDBFLAGS } {
40 append GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
41 append GDBFLAGS " -ex \"set non-stop $non_stop\""
42 clean_restart $binfile
45 set test_spawn_id [spawn_wait_for_attach $binfile]
46 set testpid [spawn_id_get_pid $test_spawn_id]
52 if {$cmd == "attach"} {
53 gdb_test_multiple "attach $testpid" $test {
54 -re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
61 kill_wait_spawned_process $test_spawn_id
66 # In non-stop, we will see one stop per thread after
69 set test "seen all stops"
70 for {set thread 1} { $thread <= $n_threads } { incr thread } {
71 gdb_test_multiple "" $test {
72 -re "Thread $::decimal ${any} stopped" {
78 # If we haven't seen all stops, then the gdb_test_multiple
79 # in the loop above will have already issued a FAIL.
80 if {$stops == $n_threads} {
85 gdb_test_multiple "info threads" "" {
86 -re "\\(running\\).*$gdb_prompt $" {
94 gdb_test_multiple "attach $testpid &" $test {
95 -re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
102 kill_wait_spawned_process $test_spawn_id
107 gdb_test_multiple "info threads" "all threads running" {
108 -re "\\(running\\)" {
112 -re "Cannot execute this command while the target is running.*$gdb_prompt $" {
113 # Testing against a remote server that doesn't do
114 # non-stop mode. Explicitly interrupt. This doesn't
115 # test the same code paths in GDB, but it's still
117 gdb_test_multiple "interrupt" "" {
119 gdb_test_multiple "" $gdb_test_name {
120 -re "received signal SIGINT, Interrupt" {
127 -re "$gdb_prompt $" {
128 gdb_assert {$running_count == ($n_threads + 1)} $gdb_test_name
133 gdb_test "detach" "Detaching from.*"
135 kill_wait_spawned_process $test_spawn_id
138 if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
142 foreach_with_prefix target-non-stop {"off" "on"} {
143 foreach_with_prefix non-stop {"off" "on"} {
144 foreach_with_prefix cmd {"attach" "attach&"} {
145 test ${target-non-stop} ${non-stop} ${cmd}