1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2004-2020 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.
34 if ![runto_main] then {
35 fail "can't run to main"
39 # Break after all threads have been started.
40 set break_line [gdb_get_line_number "Break here"]
41 gdb_test "b $break_line" ".*"
44 gdb_test_multiple "define backthread" "defining macro" {
45 -re "Type commands for definition of \"backthread\".\r\nEnd with a line saying just \"end\".\r\n>$" {
46 gdb_test_multiple "bt\np/x 20\nend" "macro details" {
55 # Cause backtraces to fail by setting a limit. This allows us to
56 # verify that the macro can get past the backtrace error and perform
57 # subsequent commands.
58 gdb_test_no_output "set backtrace limit 3"
59 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"
61 # Go into the thread_function to check that a simple "thread apply"
62 # does not change the selected frame.
63 gdb_test "step" "thread_function.*" "step to the thread_function"
64 gdb_test "up" ".*in main.*" "go up in the stack frame"
65 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"
66 gdb_test "down" "#0.*thread_function.*" "go down and check selected frame"
68 # Make sure that GDB doesn't crash when the previously selected thread
69 # exits due to the command run via thread apply. Regression test for
72 proc thr_apply_detach {thread_set} {
73 with_test_prefix "thread apply $thread_set" {
77 clean_restart ${binfile}
80 fail "can't run to main"
84 gdb_breakpoint "$break_line"
85 gdb_continue_to_breakpoint "all threads started"
87 gdb_test "thread apply $thread_set detach" "Thread .*"
88 gdb_test "thread" "No thread selected" "switched to no thread selected"
92 # Test both "all" and a thread list, because those are implemented as
93 # different commands in GDB.
94 foreach thread_set {"all" "1.1 1.2 1.3"} {
95 thr_apply_detach $thread_set
98 # Test killing and removing inferiors from a command run via "thread
99 # apply THREAD_SET". THREAD_SET can be either "1.1", or "all". GDB
100 # used to mistakenly allow deleting the previously-selected inferior,
101 # in some cases, leading to crashes.
103 proc kill_and_remove_inferior {thread_set} {
107 # The test starts multiple inferiors, therefore non-extended
108 # remote is not supported.
110 unsupported "using gdb stub"
117 clean_restart ${binfile}
119 with_test_prefix "start inferior 1" {
123 # Add and start inferior number NUM.
124 proc add_and_start_inferior {num} {
127 # Start another inferior.
128 gdb_test "add-inferior" "Added inferior $num.*" \
129 "add empty inferior $num"
130 gdb_test "inferior $num" "Switching to inferior $num.*" \
131 "switch to inferior $num"
132 gdb_test "file ${binfile}" ".*" "load file in inferior $num"
134 with_test_prefix "start inferior $num" {
139 # Start another inferior.
140 add_and_start_inferior 2
143 add_and_start_inferior 3
145 gdb_test "thread 2.1" "Switching to thread 2.1 .*"
147 # Try removing an active inferior via a "thread apply" command.
149 with_test_prefix "try remove" {
151 gdb_define_cmd "remove" {
155 # Inferior 3 is still alive, so can't remove it.
156 gdb_test "thread apply $thread_set remove" \
157 "warning: Can not remove active inferior 3.*"
158 # Check that GDB restored the selected thread.
159 gdb_test "thread" "Current thread is 2.1 .*"
161 gdb_test "info inferiors" \
163 "${ws}1${ws}process ${any}" \
164 "\\\* 2${ws}process ${any}" \
165 "${ws}3${ws}process ${any}" \
169 # Kill and try to remove inferior 2 while inferior 2 is selected.
170 # Removing the inferior should fail/warn.
171 with_test_prefix "try kill-and-remove" {
173 # The "inferior 1" command works around PR gdb/19318 ("kill
174 # inferior N" shouldn't switch to inferior N).
175 gdb_define_cmd "kill-and-remove" {
181 # Note that when threads=1.1, this makes sure we're really
182 # testing failure to remove the inferior the user had selected
183 # before the "thread apply" command, instead of testing
184 # refusal to remove the currently-iterated inferior.
185 gdb_test "thread apply $thread_set kill-and-remove" \
186 "warning: Can not remove current inferior 2.*"
187 gdb_test "thread" "No thread selected" \
188 "switched to no thread selected"
190 gdb_test "info inferiors" \
192 "${ws}1${ws}process ${any}" \
193 "\\\* 2${ws}<null>${any}" \
194 "${ws}3${ws}process ${any}" \
198 # Try removing (the now dead) inferior 2 while inferior 1 is
199 # selected. This should succeed.
200 with_test_prefix "try remove 2" {
202 gdb_test "thread 1.1" "Switching to thread 1.1 .*"
204 gdb_define_cmd "remove-again" {
208 set test "thread apply $thread_set remove-again"
209 gdb_test_multiple $test $test {
210 -re "warning: Can not remove.*$gdb_prompt $" {
213 -re "$gdb_prompt $" {
217 gdb_test "thread" "Current thread is 1.1 .*"
218 # Check that only inferiors 1 and 3 are around.
219 gdb_test "info inferiors" \
221 "\\\* 1${ws}process ${any}" \
222 "${ws}3${ws}process ${any}" \
227 # Test both "all" and a thread list, because those are implemented as
228 # different commands in GDB.
229 foreach_with_prefix thread_set {"all" "1.1"} {
230 kill_and_remove_inferior $thread_set