Testsuite: fully migrate to use_gdb_stub convenience func
[binutils-gdb.git] / gdb / testsuite / gdb.threads / threadapply.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2004-2018 Free Software Foundation, Inc.
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
7 # the Free Software Foundation; either version 3 of the License, or
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
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
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
23 standard_testfile
24 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
25 return -1
26 }
27
28 clean_restart ${binfile}
29
30 #
31 # Run to `main' where we begin our tests.
32 #
33
34 if ![runto_main] then {
35 gdb_suppress_tests
36 }
37
38 # Break after all threads have been started.
39 set break_line [gdb_get_line_number "Break here"]
40 gdb_test "b $break_line" ".*"
41 gdb_test "continue"
42
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" {
46 -re "$gdb_prompt $" {
47 pass "macro details"
48 }
49 }
50 pass "defining macro"
51 }
52 }
53
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"
59
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"
66
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
69 # PR threads/13217.
70
71 proc thr_apply_detach {thread_set} {
72 with_test_prefix "thread apply $thread_set" {
73 global binfile
74 global break_line
75
76 clean_restart ${binfile}
77
78 if ![runto_main] {
79 fail "can't run to main"
80 return -1
81 }
82
83 gdb_breakpoint "$break_line"
84 gdb_continue_to_breakpoint "all threads started"
85
86 gdb_test "thread apply $thread_set detach" "Thread .*"
87 gdb_test "thread" "No thread selected" "switched to no thread selected"
88 }
89 }
90
91 # Test both "all" and a thread list, because those are implemented as
92 # different commands in GDB.
93 foreach thread_set {"all" "1.1 1.2 1.3"} {
94 thr_apply_detach $thread_set
95 }
96
97 # Test killing and removing inferiors from a command run via "thread
98 # apply THREAD_SET". THREAD_SET can be either "1.1", or "all". GDB
99 # used to mistakenly allow deleting the previously-selected inferior,
100 # in some cases, leading to crashes.
101
102 proc kill_and_remove_inferior {thread_set} {
103 global binfile
104 global gdb_prompt
105
106 # The test starts multiple inferiors, therefore non-extended
107 # remote is not supported.
108 if [use_gdb_stub] {
109 unsupported "using gdb stub"
110 return
111 }
112
113 set any "\[^\r\n\]*"
114 set ws "\[ \t\]\+"
115
116 clean_restart ${binfile}
117
118 with_test_prefix "start inferior 1" {
119 runto_main
120 }
121
122 # Add and start inferior number NUM.
123 proc add_and_start_inferior {num} {
124 global binfile
125
126 # Start another inferior.
127 gdb_test "add-inferior" "Added inferior $num.*" \
128 "add empty inferior $num"
129 gdb_test "inferior $num" "Switching to inferior $num.*" \
130 "switch to inferior $num"
131 gdb_test "file ${binfile}" ".*" "load file in inferior $num"
132
133 with_test_prefix "start inferior $num" {
134 runto_main
135 }
136 }
137
138 # Start another inferior.
139 add_and_start_inferior 2
140
141 # And yet another.
142 add_and_start_inferior 3
143
144 gdb_test "thread 2.1" "Switching to thread 2.1 .*"
145
146 # Try removing an active inferior via a "thread apply" command.
147 # Should fail/warn.
148 with_test_prefix "try remove" {
149
150 gdb_define_cmd "remove" {
151 "remove-inferiors 3"
152 }
153
154 # Inferior 3 is still alive, so can't remove it.
155 gdb_test "thread apply $thread_set remove" \
156 "warning: Can not remove active inferior 3.*"
157 # Check that GDB restored the selected thread.
158 gdb_test "thread" "Current thread is 2.1 .*"
159
160 gdb_test "info inferiors" \
161 [multi_line \
162 "${ws}1${ws}process ${any}" \
163 "\\\* 2${ws}process ${any}" \
164 "${ws}3${ws}process ${any}" \
165 ]
166 }
167
168 # Kill and try to remove inferior 2 while inferior 2 is selected.
169 # Removing the inferior should fail/warn.
170 with_test_prefix "try kill-and-remove" {
171
172 # The "inferior 1" command works around PR gdb/19318 ("kill
173 # inferior N" shouldn't switch to inferior N).
174 gdb_define_cmd "kill-and-remove" {
175 "kill inferiors 2"
176 "inferior 1"
177 "remove-inferiors 2"
178 }
179
180 # Note that when threads=1.1, this makes sure we're really
181 # testing failure to remove the inferior the user had selected
182 # before the "thread apply" command, instead of testing
183 # refusal to remove the currently-iterated inferior.
184 gdb_test "thread apply $thread_set kill-and-remove" \
185 "warning: Can not remove current inferior 2.*"
186 gdb_test "thread" "No thread selected" \
187 "switched to no thread selected"
188
189 gdb_test "info inferiors" \
190 [multi_line \
191 "${ws}1${ws}process ${any}" \
192 "\\\* 2${ws}<null>${any}" \
193 "${ws}3${ws}process ${any}" \
194 ]
195 }
196
197 # Try removing (the now dead) inferior 2 while inferior 1 is
198 # selected. This should succeed.
199 with_test_prefix "try remove 2" {
200
201 gdb_test "thread 1.1" "Switching to thread 1.1 .*"
202
203 gdb_define_cmd "remove-again" {
204 "remove-inferiors 2"
205 }
206
207 set test "thread apply $thread_set remove-again"
208 gdb_test_multiple $test $test {
209 -re "warning: Can not remove.*$gdb_prompt $" {
210 fail $test
211 }
212 -re "$gdb_prompt $" {
213 pass $test
214 }
215 }
216 gdb_test "thread" "Current thread is 1.1 .*"
217 # Check that only inferiors 1 and 3 are around.
218 gdb_test "info inferiors" \
219 [multi_line \
220 "\\\* 1${ws}process ${any}" \
221 "${ws}3${ws}process ${any}" \
222 ]
223 }
224 }
225
226 # Test both "all" and a thread list, because those are implemented as
227 # different commands in GDB.
228 foreach_with_prefix thread_set {"all" "1.1"} {
229 kill_and_remove_inferior $thread_set
230 }