Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / threadapply.exp
CommitLineData
19ad59e7
JJ
1# This testcase is part of GDB, the GNU debugger.
2
88b9d363 3# Copyright 2004-2022 Free Software Foundation, Inc.
19ad59e7
JJ
4
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
e22f8b7c 7# the Free Software Foundation; either version 3 of the License, or
19ad59e7
JJ
8# (at your option) any later version.
9#
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.
14#
15# You should have received a copy of the GNU General Public License
e22f8b7c 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
19ad59e7 17
19ad59e7
JJ
18
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.
22
0efbbabc
TT
23standard_testfile
24if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
19ad59e7
JJ
25 return -1
26}
27
0efbbabc 28clean_restart ${binfile}
19ad59e7
JJ
29
30#
31# Run to `main' where we begin our tests.
32#
33
34if ![runto_main] then {
c8ee3f04
PW
35 fail "can't run to main"
36 return 0
19ad59e7
JJ
37}
38
39# Break after all threads have been started.
40set break_line [gdb_get_line_number "Break here"]
f6978de9 41gdb_test "b $break_line" ".*"
19ad59e7
JJ
42gdb_test "continue"
43
44gdb_test_multiple "define backthread" "defining macro" {
45 -re "Type commands for definition of \"backthread\".\r\nEnd with a line saying just \"end\".\r\n>$" {
f073ae8c 46 gdb_test_multiple "bt\np/x 20\nend" "macro details" {
19ad59e7
JJ
47 -re "$gdb_prompt $" {
48 pass "macro details"
49 }
50 }
51 pass "defining macro"
52 }
53}
54
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.
12b5d08a 58gdb_test_no_output "set backtrace limit 3"
d701dcab 59gdb_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"
19ad59e7 60
d701dcab
DP
61# Go into the thread_function to check that a simple "thread apply"
62# does not change the selected frame.
63gdb_test "step" "thread_function.*" "step to the thread_function"
64gdb_test "up" ".*in main.*" "go up in the stack frame"
65gdb_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"
66gdb_test "down" "#0.*thread_function.*" "go down and check selected frame"
5fd69d0a
PA
67
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
70# PR threads/13217.
71
72proc thr_apply_detach {thread_set} {
73 with_test_prefix "thread apply $thread_set" {
74 global binfile
75 global break_line
76
77 clean_restart ${binfile}
78
79 if ![runto_main] {
80 fail "can't run to main"
81 return -1
82 }
83
84 gdb_breakpoint "$break_line"
85 gdb_continue_to_breakpoint "all threads started"
86
87 gdb_test "thread apply $thread_set detach" "Thread .*"
88 gdb_test "thread" "No thread selected" "switched to no thread selected"
89 }
90}
91
92# Test both "all" and a thread list, because those are implemented as
93# different commands in GDB.
94foreach thread_set {"all" "1.1 1.2 1.3"} {
95 thr_apply_detach $thread_set
96}
3a3fd0fd
PA
97
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.
102
103proc kill_and_remove_inferior {thread_set} {
104 global binfile
105 global gdb_prompt
106
107 # The test starts multiple inferiors, therefore non-extended
108 # remote is not supported.
079670b9 109 if [use_gdb_stub] {
3a3fd0fd
PA
110 unsupported "using gdb stub"
111 return
112 }
113
114 set any "\[^\r\n\]*"
115 set ws "\[ \t\]\+"
116
117 clean_restart ${binfile}
118
119 with_test_prefix "start inferior 1" {
120 runto_main
121 }
122
123 # Add and start inferior number NUM.
124 proc add_and_start_inferior {num} {
125 global binfile
126
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"
133
134 with_test_prefix "start inferior $num" {
135 runto_main
136 }
137 }
138
139 # Start another inferior.
140 add_and_start_inferior 2
141
142 # And yet another.
143 add_and_start_inferior 3
144
145 gdb_test "thread 2.1" "Switching to thread 2.1 .*"
146
147 # Try removing an active inferior via a "thread apply" command.
148 # Should fail/warn.
149 with_test_prefix "try remove" {
150
151 gdb_define_cmd "remove" {
152 "remove-inferiors 3"
153 }
154
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 .*"
160
161 gdb_test "info inferiors" \
162 [multi_line \
163 "${ws}1${ws}process ${any}" \
164 "\\\* 2${ws}process ${any}" \
165 "${ws}3${ws}process ${any}" \
166 ]
167 }
168
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" {
172
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" {
176 "kill inferiors 2"
177 "inferior 1"
178 "remove-inferiors 2"
179 }
180
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"
189
190 gdb_test "info inferiors" \
191 [multi_line \
192 "${ws}1${ws}process ${any}" \
193 "\\\* 2${ws}<null>${any}" \
194 "${ws}3${ws}process ${any}" \
195 ]
196 }
197
198 # Try removing (the now dead) inferior 2 while inferior 1 is
199 # selected. This should succeed.
200 with_test_prefix "try remove 2" {
201
202 gdb_test "thread 1.1" "Switching to thread 1.1 .*"
203
204 gdb_define_cmd "remove-again" {
205 "remove-inferiors 2"
206 }
207
208 set test "thread apply $thread_set remove-again"
209 gdb_test_multiple $test $test {
210 -re "warning: Can not remove.*$gdb_prompt $" {
211 fail $test
212 }
213 -re "$gdb_prompt $" {
214 pass $test
215 }
216 }
217 gdb_test "thread" "Current thread is 1.1 .*"
218 # Check that only inferiors 1 and 3 are around.
219 gdb_test "info inferiors" \
220 [multi_line \
221 "\\\* 1${ws}process ${any}" \
222 "${ws}3${ws}process ${any}" \
223 ]
224 }
225}
226
227# Test both "all" and a thread list, because those are implemented as
228# different commands in GDB.
229foreach_with_prefix thread_set {"all" "1.1"} {
230 kill_and_remove_inferior $thread_set
231}
This page took 2.435008 seconds and 4 git commands to generate.