Commit | Line | Data |
---|---|---|
ced2dffb PA |
1 | # Copyright 2016 Free Software Foundation, Inc. |
2 | # This program is free software; you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation; either version 3 of the License, or | |
5 | # (at your option) any later version. | |
6 | # | |
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. | |
11 | # | |
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | ||
15 | # This test spawns a few threads that immediately exit the whole | |
16 | # process. On targets where the debugger needs to detach from each | |
17 | # thread individually (such as on the Linux kernel), the debugger must | |
18 | # handle the case of the process exiting while the detach is ongoing. | |
19 | # | |
20 | # Similarly, the process can also be killed from outside the debugger | |
21 | # (e.g., with SIGKILL), _before_ the user requests a detach. The | |
22 | # debugger must likewise detach gracefully. | |
23 | # | |
24 | # The testcase actually builds two variants of the test program: | |
25 | # single-process, and multi-process. In the multi-process variant, | |
26 | # the test program forks, and it's the fork child that spawns threads | |
27 | # that exit just while the process is being detached from. The fork | |
28 | # parent waits for its child to exit, so if GDB fails to detach from | |
29 | # the child correctly, the parent hangs. Because continuing the | |
30 | # parent can mask failure to detach from the child correctly (e.g., | |
31 | # due to waitpid(-1,...) calls deep in the target layers managing to | |
32 | # reap the child), we try immediately detaching from the parent too, | |
33 | # and observing whether the parent exits via standard output. | |
34 | # | |
35 | # Normally, if testing with "target remote" against gdbserver, then | |
36 | # after detaching from all attached processes, gdbserver exits. | |
37 | # However, when gdbserver detaches from a process that is its own | |
38 | # direct child, gdbserver does not exit immediately. Instead it | |
39 | # "joins" (waits for) the child, only exiting when the child itself | |
40 | # exits too. Thus, on Linux, if gdbserver fails to detach from the | |
41 | # zombie child's threads correctly (or rather, reap them), it'll hang, | |
42 | # because the leader thread will only return an exit status after all | |
43 | # threads are reaped. We test that as well. | |
44 | ||
45 | standard_testfile | |
46 | ||
47 | # Test that GDBserver exits. | |
48 | ||
49 | proc test_server_exit {} { | |
50 | global server_spawn_id | |
51 | ||
52 | set test "server exits" | |
53 | gdb_expect { | |
54 | -i $server_spawn_id | |
55 | eof { | |
56 | pass $test | |
57 | wait -i $server_spawn_id | |
58 | unset server_spawn_id | |
59 | } | |
60 | timeout { | |
61 | fail "$test (timeout)" | |
62 | } | |
63 | } | |
64 | } | |
65 | ||
66 | # If RESULT is not zero, make the caller return. | |
67 | ||
68 | proc return_if_fail { result } { | |
69 | if {$result != 0} { | |
70 | return -code return | |
71 | } | |
72 | } | |
73 | ||
74 | # Detach from a process, and ensure that it exits after detaching. | |
75 | # This relies on inferior I/O. | |
76 | ||
77 | proc detach_and_expect_exit {test} { | |
78 | global decimal | |
79 | global gdb_spawn_id | |
80 | global inferior_spawn_id | |
81 | global gdb_prompt | |
82 | ||
83 | return_if_fail [gdb_test_multiple "detach" $test { | |
84 | -re "Detaching from .*, process $decimal" { | |
85 | } | |
86 | }] | |
87 | ||
88 | set saw_prompt 0 | |
89 | set saw_inf_exit 0 | |
90 | while { !$saw_prompt && ! $saw_inf_exit } { | |
91 | # We don't know what order the interesting things will arrive in. | |
92 | # Using a pattern of the form 'x|y|z' instead of -re x ... -re y | |
93 | # ... -re z ensures that expect always chooses the match that | |
94 | # occurs leftmost in the input, and not the pattern appearing | |
95 | # first in the script that occurs anywhere in the input, so that | |
96 | # we don't skip anything. | |
97 | return_if_fail [gdb_test_multiple "" $test { | |
98 | -i "$inferior_spawn_id $gdb_spawn_id" | |
99 | -re "(exited, status=0)|($gdb_prompt )" { | |
100 | if {[info exists expect_out(1,string)]} { | |
101 | verbose -log "saw inferior exit" | |
102 | set saw_inf_exit 1 | |
103 | } elseif {[info exists expect_out(2,string)]} { | |
104 | verbose -log "saw prompt" | |
105 | set saw_prompt 1 | |
106 | } | |
107 | array unset expect_out | |
108 | } | |
109 | }] | |
110 | } | |
111 | ||
112 | pass $test | |
113 | } | |
114 | ||
115 | # Run to _exit in the child. | |
116 | ||
117 | proc continue_to_exit_bp {} { | |
118 | gdb_breakpoint "_exit" temporary | |
119 | gdb_continue_to_breakpoint "_exit" ".*_exit.*" | |
120 | } | |
121 | ||
122 | # If testing single-process, simply detach from the process. | |
123 | # | |
124 | # If testing multi-process, first detach from the child, then detach | |
125 | # from the parent and confirm that the parent exits, thus ensuring | |
126 | # we've detached from the child successfully, as the parent hangs in | |
127 | # its waitpid call otherwise. | |
128 | # | |
129 | # If connected with "target remote", make sure gdbserver exits. | |
130 | # | |
131 | # CMD indicates what to do with the parent after detaching the child. | |
132 | # Can be either "detach" to detach, or "continue", to continue to | |
133 | # exit. If "continue", then CONTINUE_RE is the regexp to expect. | |
134 | # Defaults to normal exit output. | |
135 | # | |
136 | proc do_detach {multi_process cmd {continue_re ""}} { | |
137 | global decimal | |
138 | global server_spawn_id | |
139 | ||
140 | if {$continue_re == ""} { | |
141 | set continue_re "exited normally.*" | |
142 | } | |
143 | ||
144 | set is_remote [expr {[target_info exists gdb_protocol] | |
145 | && [target_info gdb_protocol] == "remote"}] | |
146 | ||
147 | if {$multi_process} { | |
148 | gdb_test "detach" "Detaching from .*, process $decimal" \ | |
149 | "detach child" | |
150 | ||
151 | gdb_test "inferior 1" "\[Switching to inferior $decimal\].*" \ | |
152 | "switch to parent" | |
153 | ||
154 | if {$cmd == "detach"} { | |
155 | # Make sure that detach works and that the parent process | |
156 | # exits cleanly. | |
157 | detach_and_expect_exit "detach parent" | |
158 | } elseif {$cmd == "continue"} { | |
159 | # Make sure that continuing works and that the parent process | |
160 | # exits cleanly. | |
161 | gdb_test "continue" $continue_re | |
162 | } else { | |
163 | perror "unhandled command: $cmd" | |
164 | } | |
165 | } else { | |
166 | if $is_remote { | |
167 | set extra "\r\nEnding remote debugging\." | |
168 | } else { | |
169 | set extra "" | |
170 | } | |
171 | if {$cmd == "detach"} { | |
172 | gdb_test "detach" "Detaching from .*, process $decimal$extra" | |
173 | } elseif {$cmd == "continue"} { | |
174 | gdb_test "continue" $continue_re | |
175 | } else { | |
176 | perror "unhandled command: $cmd" | |
177 | } | |
178 | } | |
179 | ||
180 | # When connected in "target remote" mode, the server should exit | |
181 | # when there are no processes left to debug. | |
182 | if { $is_remote && [info exists server_spawn_id]} { | |
183 | test_server_exit | |
184 | } | |
185 | } | |
186 | ||
187 | # Test detaching from a process that dies just while GDB is detaching. | |
188 | ||
189 | proc test_detach {multi_process cmd} { | |
190 | with_test_prefix "detach" { | |
191 | global binfile | |
192 | ||
193 | clean_restart ${binfile} | |
194 | ||
195 | if ![runto_main] { | |
196 | fail "Can't run to main" | |
197 | return -1 | |
198 | } | |
199 | ||
200 | if {$multi_process} { | |
201 | gdb_test_no_output "set detach-on-fork off" | |
202 | gdb_test_no_output "set follow-fork-mode child" | |
203 | } | |
204 | ||
205 | # Run to _exit in the child. | |
206 | continue_to_exit_bp | |
207 | ||
208 | do_detach $multi_process $cmd | |
209 | } | |
210 | } | |
211 | ||
212 | # Same as test_detach, except set a watchpoint before detaching. | |
213 | ||
214 | proc test_detach_watch {multi_process cmd} { | |
215 | with_test_prefix "watchpoint" { | |
216 | global binfile decimal | |
217 | ||
218 | clean_restart ${binfile} | |
219 | ||
220 | if ![runto_main] { | |
221 | fail "Can't run to main" | |
222 | return -1 | |
223 | } | |
224 | ||
225 | if {$multi_process} { | |
226 | gdb_test_no_output "set detach-on-fork off" | |
227 | gdb_test_no_output "set follow-fork-mode child" | |
228 | ||
229 | gdb_breakpoint "child_function" temporary | |
230 | gdb_continue_to_breakpoint "child_function" ".*" | |
231 | } | |
232 | ||
233 | # Set a watchpoint in the child. | |
234 | gdb_test "watch globalvar" ".* watchpoint $decimal: globalvar" | |
235 | ||
236 | # Continue to the _exit breakpoint. This arms the watchpoint | |
237 | # registers in all threads. Detaching will thus need to clear | |
238 | # them out, and handle the case of the thread disappearing | |
239 | # while doing that (on targets that need to detach from each | |
240 | # thread individually). | |
241 | continue_to_exit_bp | |
242 | ||
243 | do_detach $multi_process $cmd | |
244 | } | |
245 | } | |
246 | ||
247 | # Test detaching from a process that dies _before_ GDB starts | |
248 | # detaching. | |
249 | ||
250 | proc test_detach_killed_outside {multi_process cmd} { | |
251 | with_test_prefix "killed outside" { | |
252 | global binfile | |
253 | ||
254 | clean_restart ${binfile} | |
255 | ||
256 | if ![runto_main] { | |
257 | fail "Can't run to main" | |
258 | return -1 | |
259 | } | |
260 | ||
261 | gdb_test_no_output "set breakpoint always-inserted on" | |
262 | ||
263 | if {$multi_process} { | |
264 | gdb_test_no_output "set detach-on-fork off" | |
265 | gdb_test_no_output "set follow-fork-mode child" | |
266 | } | |
267 | ||
268 | # Run to _exit in the child. | |
269 | continue_to_exit_bp | |
270 | ||
271 | set childpid [get_integer_valueof "mypid" -1] | |
272 | if { $childpid == -1 } { | |
273 | untested "failed to extract child pid" | |
274 | return -1 | |
275 | } | |
276 | ||
277 | remote_exec target "kill -9 ${childpid}" | |
278 | ||
279 | # Give it some time to die. | |
280 | sleep 2 | |
281 | ||
282 | if {$multi_process} { | |
283 | set continue_re "exited with code 02.*" | |
284 | } else { | |
285 | set continue_re "terminated with signal SIGKILL.*" | |
286 | } | |
287 | do_detach $multi_process $cmd $continue_re | |
288 | } | |
289 | } | |
290 | ||
291 | # The test proper. MULTI_PROCESS is true if testing the multi-process | |
292 | # variant. | |
293 | ||
294 | proc do_test {multi_process cmd} { | |
295 | global testfile srcfile binfile | |
296 | ||
297 | if {$multi_process && $cmd == "detach" | |
298 | && [target_info exists gdb,noinferiorio]} { | |
299 | # This requires inferior I/O to tell whether both the parent | |
300 | # and child exit successfully. | |
301 | return | |
302 | } | |
303 | ||
304 | set binfile [standard_output_file ${testfile}-$multi_process-$cmd] | |
305 | set options {debug pthreads} | |
306 | if {$multi_process} { | |
307 | lappend options "additional_flags=-DMULTIPROCESS" | |
308 | } | |
309 | ||
310 | if {[build_executable "failed to build" \ | |
311 | $testfile-$multi_process-$cmd $srcfile $options] == -1} { | |
312 | return -1 | |
313 | } | |
314 | ||
315 | test_detach $multi_process $cmd | |
316 | test_detach_watch $multi_process $cmd | |
317 | test_detach_killed_outside $multi_process $cmd | |
318 | } | |
319 | ||
320 | foreach multi_process {0 1} { | |
321 | set mode [expr {$multi_process ? "single-process" : "multi-process"}] | |
322 | foreach cmd {"detach" "continue"} { | |
323 | with_test_prefix "$mode: $cmd" { | |
324 | do_test $multi_process $cmd | |
325 | } | |
326 | } | |
327 | } |