aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads/threadapply.exp
blob: 8d729cb3d424bbe8034ff72068cb6f901a1adec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# This testcase is part of GDB, the GNU debugger.

# Copyright 2004-2023 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# This test verifies that a macro using backtrace can be applied to all threads
# and will continue for each thread even though an error may occur in
# backtracing one of the threads.

standard_testfile
if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
    return -1
}

clean_restart ${binfile}

#
# Run to `main' where we begin our tests.
#

if {![runto_main]} {
    return 0
}

# Break after all threads have been started.
set break_line [gdb_get_line_number "Break here"]
gdb_test "b $break_line" ".*"
gdb_test "continue"

gdb_test_multiple "define backthread" "defining macro" {
  -re "Type commands for definition of \"backthread\".\r\nEnd with a line saying just \"end\".\r\n>$" {
    gdb_test_multiple "bt\np/x 20\nend" "macro details" {
      -re "$gdb_prompt $" {
        pass "macro details"
      }
    }
    pass "defining macro"
  }
}

# Cause backtraces to fail by setting a limit.  This allows us to
# verify that the macro can get past the backtrace error and perform
# subsequent commands.
gdb_test_no_output "set backtrace limit 3"
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"

# Go into the thread_function to check that a simple "thread apply"
# does not change the selected frame.
gdb_test "step" "thread_function.*" "step to the thread_function"
gdb_test "up" ".*in main.*" "go up in the stack frame" 
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"
gdb_test "down" "#0.*thread_function.*" "go down and check selected frame"

# Make sure that GDB doesn't crash when the previously selected thread
# exits due to the command run via thread apply.  Regression test for
# PR threads/13217.

proc thr_apply_detach {thread_set} {
    with_test_prefix "thread apply $thread_set" {
	global binfile
	global break_line

	clean_restart ${binfile}

	if ![runto_main] {
	    return -1
	}

	gdb_breakpoint "$break_line"
	gdb_continue_to_breakpoint "all threads started"

	gdb_test "thread apply $thread_set detach" "Thread .*"
	gdb_test "thread" "No thread selected" "switched to no thread selected"
    }
}

# Test both "all" and a thread list, because those are implemented as
# different commands in GDB.
foreach thread_set {"all" "1.1 1.2 1.3"} {
    thr_apply_detach $thread_set
}

# Test killing and removing inferiors from a command run via "thread
# apply THREAD_SET".  THREAD_SET can be either "1.1", or "all".  GDB
# used to mistakenly allow deleting the previously-selected inferior,
# in some cases, leading to crashes.

proc kill_and_remove_inferior {thread_set} {
    global binfile
    global gdb_prompt

    # The test starts multiple inferiors, therefore non-extended
    # remote is not supported.
    if [use_gdb_stub] {
	unsupported "using gdb stub"
	return
    }

    set any "\[^\r\n\]*"
    set ws "\[ \t\]\+"

    clean_restart ${binfile}

    with_test_prefix "start inferior 1" {
	runto_main
    }

    # Add and start inferior number NUM.
    proc add_and_start_inferior {num} {
	global binfile

	# Start another inferior.
	gdb_test "add-inferior" "Added inferior $num.*" \
	    "add empty inferior $num"
	gdb_test "inferior $num" "Switching to inferior $num.*" \
	    "switch to inferior $num"
	gdb_test "file ${binfile}" ".*" "load file in inferior $num"

	with_test_prefix "start inferior $num" {
	    runto_main
	}
    }

    # Start another inferior.
    add_and_start_inferior 2

    # And yet another.
    add_and_start_inferior 3

    gdb_test "thread 2.1" "Switching to thread 2.1 .*"

    # Try removing an active inferior via a "thread apply" command.
    # Should fail/warn.
    with_test_prefix "try remove" {

	gdb_define_cmd "remove" {
	    "remove-inferiors 3"
	}

	# Inferior 3 is still alive, so can't remove it.
	gdb_test "thread apply $thread_set remove" \
	    "warning: Can not remove active inferior 3.*"
	# Check that GDB restored the selected thread.
	gdb_test "thread" "Current thread is 2.1 .*"

	gdb_test "info inferiors" \
	    [multi_line \
		 "${ws}1${ws}process ${any}" \
		 "\\\* 2${ws}process ${any}" \
		 "${ws}3${ws}process ${any}" \
		]
    }

    # Kill and try to remove inferior 2 while inferior 2 is selected.
    # Removing the inferior should fail/warn.
    with_test_prefix "try kill-and-remove" {

	# The "inferior 1" command works around PR gdb/19318 ("kill
	# inferior N" shouldn't switch to inferior N).
	gdb_define_cmd "kill-and-remove" {
	    "kill inferiors 2"
	    "inferior 1"
	    "remove-inferiors 2"
	}

	# Note that when threads=1.1, this makes sure we're really
	# testing failure to remove the inferior the user had selected
	# before the "thread apply" command, instead of testing
	# refusal to remove the currently-iterated inferior.
	gdb_test "thread apply $thread_set kill-and-remove" \
	    "warning: Can not remove current inferior 2.*"
	gdb_test "thread" "No thread selected" \
	    "switched to no thread selected"

	gdb_test "info inferiors" \
	    [multi_line \
		 "${ws}1${ws}process ${any}" \
		 "\\\* 2${ws}<null>${any}" \
		 "${ws}3${ws}process ${any}" \
		]
    }

    # Try removing (the now dead) inferior 2 while inferior 1 is
    # selected.  This should succeed.
    with_test_prefix "try remove 2" {

	gdb_test "thread 1.1" "Switching to thread 1.1 .*"

	gdb_define_cmd "remove-again" {
	    "remove-inferiors 2"
	}

	set test "thread apply $thread_set remove-again"
	gdb_test_multiple $test $test {
	    -re "warning: Can not remove.*$gdb_prompt $" {
		fail $test
	    }
	    -re "$gdb_prompt $" {
		pass $test
	    }
	}
	gdb_test "thread" "Current thread is 1.1 .*"
	# Check that only inferiors 1 and 3 are around.
	gdb_test "info inferiors" \
	    [multi_line \
		 "\\\* 1${ws}process ${any}" \
		 "${ws}3${ws}process ${any}" \
		]
    }
}

# Test both "all" and a thread list, because those are implemented as
# different commands in GDB.
foreach_with_prefix thread_set {"all" "1.1"} {
    kill_and_remove_inferior $thread_set
}