[gdb/symtab] Fix gdb.base/fission-macro.exp with unix/-m32
[binutils-gdb.git] / gdb / testsuite / gdb.threads / attach-non-stop.exp
blobeb12e6bbda18bbc08e485348f3fcd9423d063a43
1 # Copyright 2021-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 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 require can_spawn_for_attach
24 standard_testfile
26 # The test proper.  See description above.
28 proc test {target_non_stop non_stop cmd} {
29     global binfile srcfile
30     global gdb_prompt
31     global decimal
32     global GDBFLAGS
34     # Number of threads started by the program.
35     set n_threads 10
37     save_vars { GDBFLAGS } {
38         append GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
39         append GDBFLAGS " -ex \"set non-stop $non_stop\""
40         clean_restart $binfile
41     }
43     set test_spawn_id [spawn_wait_for_attach $binfile]
44     set testpid [spawn_id_get_pid $test_spawn_id]
46     set attached 0
47     set test "attach"
48     set any "\[^\r\n\]*"
50     if {$cmd == "attach"} {
51         gdb_test_multiple "attach $testpid" $test {
52             -re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
53                 pass $test
54                 set attached 1
55             }
56         }
58         if {!$attached} {
59             kill_wait_spawned_process $test_spawn_id
60             return
61         }
63         if {$non_stop} {
64             # In non-stop, we will see one stop per thread after
65             # the prompt.
66             set stops 0
67             set test "seen all stops"
68             for {set thread 1} { $thread <= $n_threads } { incr thread } {
69                 gdb_test_multiple "" $test {
70                     -re "Thread $::decimal ${any} stopped" {
71                         incr stops
72                     }
73                 }
74             }
76             # If we haven't seen all stops, then the gdb_test_multiple
77             # in the loop above will have already issued a FAIL.
78             if {$stops == $n_threads} {
79                 pass $test
80             }
81         }
83         gdb_test_multiple "info threads" "" {
84             -re "\\(running\\).*$gdb_prompt $" {
85                 fail $gdb_test_name
86             }
87             -re "$gdb_prompt $" {
88                 pass $gdb_test_name
89             }
90         }
91     } else {
92         gdb_test_multiple "attach $testpid &" $test {
93             -re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
94                 pass $test
95                 set attached 1
96             }
97         }
99         if {!$attached} {
100             kill_wait_spawned_process $test_spawn_id
101             return
102         }
104         set running_count 0
105         gdb_test_multiple "info threads" "all threads running" {
106             -re "\\(running\\)" {
107                 incr running_count
108                 exp_continue
109             }
110             -re "Cannot execute this command while the target is running.*$gdb_prompt $" {
111                 # Testing against a remote server that doesn't do
112                 # non-stop mode.  Explicitly interrupt.  This doesn't
113                 # test the same code paths in GDB, but it's still
114                 # something.
115                 gdb_test_multiple "interrupt" "" {
116                     -re "$gdb_prompt " {
117                         gdb_test_multiple "" $gdb_test_name {
118                             -re "received signal SIGINT, Interrupt" {
119                                 pass $gdb_test_name
120                             }
121                         }
122                     }
123                 }
124             }
125             -re "$gdb_prompt $" {
126                 gdb_assert {$running_count == ($n_threads + 1)} $gdb_test_name
127             }
128         }
129     }
131     gdb_test "detach" "Detaching from.*"
133     kill_wait_spawned_process $test_spawn_id
136 if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
137     return -1
140 foreach_with_prefix target-non-stop {"off" "on"} {
141     foreach_with_prefix non-stop {"off" "on"} {
142         foreach_with_prefix cmd {"attach" "attach&"} {
143             test ${target-non-stop} ${non-stop} ${cmd}
144         }
145     }