Remove obsolete test from gdb.cp/var-tag.exp
[binutils-gdb.git] / gdb / testsuite / gdb.threads / threadapply.exp
blobdf72909d99714887afa0bcd47484b4294773d688
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.
23 standard_testfile
24 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
25     return -1
28 clean_restart ${binfile}
31 # Run to `main' where we begin our tests.
34 if {![runto_main]} {
35     return 0
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"
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   }
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
69 # PR threads/13217.
71 proc thr_apply_detach {thread_set} {
72     with_test_prefix "thread apply $thread_set" {
73         global binfile
74         global break_line
76         clean_restart ${binfile}
78         if ![runto_main] {
79             return -1
80         }
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"
87     }
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} {
102     global binfile
103     global gdb_prompt
105     # The test starts multiple inferiors, therefore non-extended
106     # remote is not supported.
107     if [use_gdb_stub] {
108         unsupported "using gdb stub"
109         return
110     }
112     set any "\[^\r\n\]*"
113     set ws "\[ \t\]\+"
115     clean_restart ${binfile}
117     with_test_prefix "start inferior 1" {
118         runto_main
119     }
121     # Add and start inferior number NUM.
122     proc add_and_start_inferior {num} {
123         global binfile
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" {
133             runto_main
134         }
135     }
137     # Start another inferior.
138     add_and_start_inferior 2
140     # And yet another.
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.
146     # Should fail/warn.
147     with_test_prefix "try remove" {
149         gdb_define_cmd "remove" {
150             "remove-inferiors 3"
151         }
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" \
160             [multi_line \
161                  "${ws}1${ws}process ${any}" \
162                  "\\\* 2${ws}process ${any}" \
163                  "${ws}3${ws}process ${any}" \
164                 ]
165     }
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" {
174             "kill inferiors 2"
175             "inferior 1"
176             "remove-inferiors 2"
177         }
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" \
189             [multi_line \
190                  "${ws}1${ws}process ${any}" \
191                  "\\\* 2${ws}<null>${any}" \
192                  "${ws}3${ws}process ${any}" \
193                 ]
194     }
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" {
203             "remove-inferiors 2"
204         }
206         set test "thread apply $thread_set remove-again"
207         gdb_test_multiple $test $test {
208             -re "warning: Can not remove.*$gdb_prompt $" {
209                 fail $test
210             }
211             -re "$gdb_prompt $" {
212                 pass $test
213             }
214         }
215         gdb_test "thread" "Current thread is 1.1 .*"
216         # Check that only inferiors 1 and 3 are around.
217         gdb_test "info inferiors" \
218             [multi_line \
219                  "\\\* 1${ws}process ${any}" \
220                  "${ws}3${ws}process ${any}" \
221                 ]
222     }
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