1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2004-2024 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # This test verifies that a macro using backtrace can be applied to all threads
20 # and will continue for each thread even though an error may occur in
21 # backtracing one of the threads.
24 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
28 clean_restart ${binfile}
31 # Run to `main' where we begin our tests.
38 # Break after all threads have been started.
39 set break_line [gdb_get_line_number "Break here"]
40 gdb_test "b $break_line" ".*"
43 gdb_test_multiple "define backthread" "defining macro" {
44 -re "Type commands for definition of \"backthread\".\r\nEnd with a line saying just \"end\".\r\n>$" {
45 gdb_test_multiple "bt\np/x 20\nend" "macro details" {
54 # Cause backtraces to fail by setting a limit. This allows us to
55 # verify that the macro can get past the backtrace error and perform
56 # subsequent commands.
57 gdb_test_no_output "set backtrace limit 3"
58 gdb_test "thread apply all backthread" "Thread ..*\\\$\[0-9]+ = 0x14.*Thread ..*\\\$\[0-9]+ = 0x14.*Thread ..*\\\$\[0-9]+ = 0x14.*Thread ..*\\\$\[0-9]+ = 0x14.*Thread ..*\\\$\[0-9]+ = 0x14.*Thread ..*\\\$\[0-9]+ = 0x14"
60 # Go into the thread_function to check that a simple "thread apply"
61 # does not change the selected frame.
62 gdb_test "step" "thread_function.*" "step to the thread_function"
63 gdb_test "up" ".*in main.*" "go up in the stack frame"
64 gdb_test "thread apply all print 1" "Thread ..*\\\$\[0-9]+ = 1.*Thread ..*\\\$\[0-9]+ = 1.*Thread ..*\\\$\[0-9]+ = 1.*Thread ..*\\\$\[0-9]+ = 1.*Thread ..*\\\$\[0-9]+ = 1.*Thread ..*\\\$\[0-9]+ = 1" "run a simple print command on all threads"
65 gdb_test "down" "#0.*thread_function.*" "go down and check selected frame"
67 # Make sure that GDB doesn't crash when the previously selected thread
68 # exits due to the command run via thread apply. Regression test for
71 proc thr_apply_detach {thread_set} {
72 with_test_prefix "thread apply $thread_set" {
76 clean_restart ${binfile}
82 gdb_breakpoint "$break_line"
83 gdb_continue_to_breakpoint "all threads started"
85 gdb_test "thread apply $thread_set detach" "Thread .*"
86 gdb_test "thread" "No thread selected" "switched to no thread selected"
90 # Test both "all" and a thread list, because those are implemented as
91 # different commands in GDB.
92 foreach thread_set {"all" "1.1 1.2 1.3"} {
93 thr_apply_detach $thread_set
96 # Test killing and removing inferiors from a command run via "thread
97 # apply THREAD_SET". THREAD_SET can be either "1.1", or "all". GDB
98 # used to mistakenly allow deleting the previously-selected inferior,
99 # in some cases, leading to crashes.
101 proc kill_and_remove_inferior {thread_set} {
105 # The test starts multiple inferiors, therefore non-extended
106 # remote is not supported.
108 unsupported "using gdb stub"
115 clean_restart ${binfile}
117 with_test_prefix "start inferior 1" {
121 # Add and start inferior number NUM.
122 proc add_and_start_inferior {num} {
125 # Start another inferior.
126 gdb_test "add-inferior" "Added inferior $num.*" \
127 "add empty inferior $num"
128 gdb_test "inferior $num" "Switching to inferior $num.*" \
129 "switch to inferior $num"
130 gdb_test "file ${binfile}" ".*" "load file in inferior $num"
132 with_test_prefix "start inferior $num" {
137 # Start another inferior.
138 add_and_start_inferior 2
141 add_and_start_inferior 3
143 gdb_test "thread 2.1" "Switching to thread 2.1 .*"
145 # Try removing an active inferior via a "thread apply" command.
147 with_test_prefix "try remove" {
149 gdb_define_cmd "remove" {
153 # Inferior 3 is still alive, so can't remove it.
154 gdb_test "thread apply $thread_set remove" \
155 "warning: Can not remove active inferior 3.*"
156 # Check that GDB restored the selected thread.
157 gdb_test "thread" "Current thread is 2.1 .*"
159 gdb_test "info inferiors" \
161 "${ws}1${ws}process ${any}" \
162 "\\\* 2${ws}process ${any}" \
163 "${ws}3${ws}process ${any}" \
167 # Kill and try to remove inferior 2 while inferior 2 is selected.
168 # Removing the inferior should fail/warn.
169 with_test_prefix "try kill-and-remove" {
171 # The "inferior 1" command works around PR gdb/19318 ("kill
172 # inferior N" shouldn't switch to inferior N).
173 gdb_define_cmd "kill-and-remove" {
179 # Note that when threads=1.1, this makes sure we're really
180 # testing failure to remove the inferior the user had selected
181 # before the "thread apply" command, instead of testing
182 # refusal to remove the currently-iterated inferior.
183 gdb_test "thread apply $thread_set kill-and-remove" \
184 "warning: Can not remove current inferior 2.*"
185 gdb_test "thread" "No thread selected" \
186 "switched to no thread selected"
188 gdb_test "info inferiors" \
190 "${ws}1${ws}process ${any}" \
191 "\\\* 2${ws}<null>${any}" \
192 "${ws}3${ws}process ${any}" \
196 # Try removing (the now dead) inferior 2 while inferior 1 is
197 # selected. This should succeed.
198 with_test_prefix "try remove 2" {
200 gdb_test "thread 1.1" "Switching to thread 1.1 .*"
202 gdb_define_cmd "remove-again" {
206 set test "thread apply $thread_set remove-again"
207 gdb_test_multiple $test $test {
208 -re "warning: Can not remove.*$gdb_prompt $" {
211 -re "$gdb_prompt $" {
215 gdb_test "thread" "Current thread is 1.1 .*"
216 # Check that only inferiors 1 and 3 are around.
217 gdb_test "info inferiors" \
219 "\\\* 1${ws}process ${any}" \
220 "${ws}3${ws}process ${any}" \
225 # Test both "all" and a thread list, because those are implemented as
226 # different commands in GDB.
227 foreach_with_prefix thread_set {"all" "1.1"} {
228 kill_and_remove_inferior $thread_set