Commit | Line | Data |
---|---|---|
0b302171 | 1 | # Copyright 1997, 1999, 2007-2012 Free Software Foundation, Inc. |
74cf1395 JM |
2 | |
3 | # This program is free software; you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
e22f8b7c | 5 | # the Free Software Foundation; either version 3 of the License, or |
74cf1395 | 6 | # (at your option) any later version. |
e22f8b7c | 7 | # |
74cf1395 JM |
8 | # This program is distributed in the hope that it will be useful, |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
e22f8b7c | 12 | # |
74cf1395 | 13 | # You should have received a copy of the GNU General Public License |
e85a822c | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
74cf1395 | 15 | |
e85a822c | 16 | if { [is_remote target] || ![isnative] } then { |
5a2468f5 JM |
17 | continue |
18 | } | |
19 | ||
2d4e0376 YQ |
20 | # Until "set follow-fork-mode" and "catch fork" are implemented on |
21 | # other targets... | |
22 | # | |
23 | if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then { | |
24 | continue | |
25 | } | |
74cf1395 | 26 | |
e85a822c | 27 | global srcfile |
74cf1395 JM |
28 | set testfile "foll-fork" |
29 | set srcfile ${testfile}.c | |
30 | set binfile ${objdir}/${subdir}/${testfile} | |
31 | ||
32 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } { | |
b60f0898 JB |
33 | untested foll-fork.exp |
34 | return -1 | |
74cf1395 JM |
35 | } |
36 | ||
e85a822c DJ |
37 | proc check_fork_catchpoints {} { |
38 | global gdb_prompt | |
39 | ||
40 | # Verify that the system supports "catch fork". | |
41 | gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint" | |
42 | set has_fork_catchpoints 0 | |
43 | gdb_test_multiple "continue" "continue to first fork catchpoint" { | |
77b06cd7 | 44 | -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" { |
e85a822c DJ |
45 | unsupported "continue to first fork catchpoint" |
46 | } | |
47 | -re ".*Catchpoint.*$gdb_prompt $" { | |
48 | set has_fork_catchpoints 1 | |
49 | pass "continue to first fork catchpoint" | |
50 | } | |
51 | } | |
52 | ||
53 | if {$has_fork_catchpoints == 0} { | |
54 | unsupported "fork catchpoints" | |
55 | return -code return | |
56 | } | |
57 | } | |
58 | ||
74cf1395 | 59 | proc default_fork_parent_follow {} { |
a76e022a MS |
60 | global gdb_prompt |
61 | ||
62 | gdb_test "show follow-fork" \ | |
63 | "Debugger response to a program call of fork or vfork is \"parent\".*" \ | |
64 | "default show parent follow, no catchpoints" | |
65 | ||
66 | gdb_test "next 2" \ | |
67 | "Detaching after fork from.*" \ | |
68 | "default parent follow, no catchpoints" | |
69 | ||
70 | # The child has been detached; allow time for any output it might | |
71 | # generate to arrive, so that output doesn't get confused with | |
72 | # any expected debugger output from a subsequent testpoint. | |
73 | # | |
74 | exec sleep 1 | |
74cf1395 JM |
75 | } |
76 | ||
77 | proc explicit_fork_parent_follow {} { | |
a76e022a MS |
78 | global gdb_prompt |
79 | ||
80 | gdb_test_no_output "set follow-fork parent" | |
81 | ||
82 | gdb_test "show follow-fork" \ | |
83 | "Debugger response to a program call of fork or vfork is \"parent\"." \ | |
84 | "explicit show parent follow, no catchpoints" | |
85 | ||
86 | gdb_test "next 2" "Detaching after fork from.*" \ | |
87 | "explicit parent follow, no catchpoints" | |
88 | ||
89 | # The child has been detached; allow time for any output it might | |
90 | # generate to arrive, so that output doesn't get confused with | |
91 | # any expected debugger output from a subsequent testpoint. | |
92 | # | |
93 | exec sleep 1 | |
74cf1395 JM |
94 | } |
95 | ||
96 | proc explicit_fork_child_follow {} { | |
a76e022a MS |
97 | global gdb_prompt |
98 | ||
99 | gdb_test_no_output "set follow-fork child" | |
100 | ||
101 | gdb_test "show follow-fork" \ | |
102 | "Debugger response to a program call of fork or vfork is \"child\"." \ | |
103 | "explicit show child follow, no catchpoints" | |
104 | ||
105 | gdb_test "next 2" "Attaching after.* fork to.*" \ | |
106 | "explicit child follow, no catchpoints" | |
107 | ||
108 | # The child has been detached; allow time for any output it might | |
109 | # generate to arrive, so that output doesn't get confused with | |
110 | # any gdb_expected debugger output from a subsequent testpoint. | |
111 | # | |
112 | exec sleep 1 | |
74cf1395 JM |
113 | } |
114 | ||
115 | proc catch_fork_child_follow {} { | |
a76e022a MS |
116 | global gdb_prompt |
117 | global srcfile | |
118 | ||
119 | set bp_after_fork [gdb_get_line_number "set breakpoint here"] | |
120 | ||
2c04da0d | 121 | gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \ |
a76e022a MS |
122 | "explicit child follow, set catch fork" |
123 | ||
124 | # Verify that the catchpoint is mentioned in an "info breakpoints", | |
125 | # and further that the catchpoint mentions no process id. | |
126 | # | |
127 | set test_name "info shows catchpoint without pid" | |
128 | gdb_test_multiple "info breakpoints" "$test_name" { | |
129 | -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" { | |
130 | pass "$test_name" | |
131 | } | |
132 | } | |
133 | ||
134 | gdb_test "continue" \ | |
2c04da0d | 135 | "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \ |
a76e022a MS |
136 | "explicit child follow, catch fork" |
137 | ||
138 | # Verify that the catchpoint is mentioned in an "info breakpoints", | |
139 | # and further that the catchpoint managed to capture a process id. | |
140 | # | |
141 | set test_name "info shows catchpoint without pid" | |
142 | gdb_test_multiple "info breakpoints" "$test_name" { | |
143 | -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" { | |
144 | pass "$test_name" | |
145 | } | |
146 | } | |
147 | ||
148 | gdb_test_no_output "set follow-fork child" | |
149 | ||
150 | gdb_test "tbreak ${srcfile}:$bp_after_fork" \ | |
151 | "Temporary breakpoint.*, line $bp_after_fork.*" \ | |
152 | "set follow-fork child, tbreak" | |
153 | ||
154 | gdb_test "continue" \ | |
155 | "Attaching after.* fork to.* at .*$bp_after_fork.*" \ | |
156 | "set follow-fork child, hit tbreak" | |
157 | ||
158 | # The parent has been detached; allow time for any output it might | |
159 | # generate to arrive, so that output doesn't get confused with | |
160 | # any expected debugger output from a subsequent testpoint. | |
161 | # | |
162 | exec sleep 1 | |
163 | ||
164 | gdb_test "delete breakpoints" \ | |
165 | "" \ | |
166 | "set follow-fork child, cleanup" \ | |
e9101ff5 | 167 | "Delete all breakpoints. \\(y or n\\) $" \ |
a76e022a | 168 | "y" |
74cf1395 JM |
169 | } |
170 | ||
b242c3c2 | 171 | proc catch_fork_unpatch_child {} { |
a76e022a MS |
172 | global gdb_prompt |
173 | global srcfile | |
174 | ||
175 | set bp_exit [gdb_get_line_number "at exit"] | |
176 | ||
177 | gdb_test "break callee" "file .*$srcfile, line .*" \ | |
178 | "unpatch child, break at callee" | |
179 | gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \ | |
180 | "unpatch child, set catch fork" | |
181 | ||
182 | gdb_test "continue" \ | |
2c04da0d | 183 | "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \ |
a76e022a MS |
184 | "unpatch child, catch fork" |
185 | ||
186 | # Delete all breakpoints and catchpoints. | |
187 | delete_breakpoints | |
188 | ||
189 | # Force $srcfile as the current GDB source can be in glibc sourcetree. | |
190 | gdb_test "break $srcfile:$bp_exit" \ | |
191 | "Breakpoint .*file .*$srcfile, line .*" \ | |
192 | "unpatch child, breakpoint at exit call" | |
193 | ||
27d3a1a2 MS |
194 | gdb_test_no_output "set follow-fork child" \ |
195 | "unpatch child, set follow-fork child" | |
a76e022a MS |
196 | |
197 | set test "unpatch child, unpatched parent breakpoints from child" | |
198 | gdb_test_multiple "continue" $test { | |
199 | -re "at exit.*$gdb_prompt $" { | |
200 | pass "$test" | |
201 | } | |
202 | -re "SIGTRAP.*$gdb_prompt $" { | |
203 | fail "$test" | |
204 | ||
205 | # Explicitly kill this child, so we can continue gracefully | |
206 | # with further testing... | |
207 | send_gdb "kill\n" | |
208 | gdb_expect { | |
209 | -re ".*Kill the program being debugged.*y or n. $" { | |
210 | send_gdb "y\n" | |
211 | gdb_expect -re "$gdb_prompt $" {} | |
212 | } | |
213 | } | |
214 | } | |
215 | } | |
b242c3c2 PA |
216 | } |
217 | ||
74cf1395 | 218 | proc tcatch_fork_parent_follow {} { |
a76e022a MS |
219 | global gdb_prompt |
220 | global srcfile | |
221 | ||
222 | set bp_after_fork [gdb_get_line_number "set breakpoint here"] | |
223 | ||
2c04da0d | 224 | gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \ |
a76e022a MS |
225 | "explicit parent follow, set tcatch fork" |
226 | ||
74cf1395 JM |
227 | # ??rehrauer: I don't yet know how to get the id of the tcatch |
228 | # via this script, so that I can add a -do list to it. For now, | |
229 | # do the follow stuff after the catch happens. | |
230 | ||
2c04da0d UW |
231 | gdb_test "continue" \ |
232 | "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \ | |
a76e022a MS |
233 | "explicit parent follow, tcatch fork" |
234 | ||
235 | gdb_test_no_output "set follow-fork parent" | |
236 | ||
237 | gdb_test "tbreak ${srcfile}:$bp_after_fork" \ | |
238 | "Temporary breakpoint.*, line $bp_after_fork.*" \ | |
239 | "set follow-fork parent, tbreak" | |
240 | ||
241 | gdb_test "continue" \ | |
242 | "Detaching after fork from.* at .*$bp_after_fork.*" \ | |
243 | "set follow-fork parent, hit tbreak" | |
244 | ||
245 | # The child has been detached; allow time for any output it might | |
246 | # generate to arrive, so that output doesn't get confused with | |
247 | # any expected debugger output from a subsequent testpoint. | |
248 | # | |
249 | exec sleep 1 | |
250 | ||
251 | gdb_test "delete breakpoints" \ | |
252 | "" \ | |
253 | "set follow-fork parent, cleanup" \ | |
e9101ff5 | 254 | "Delete all breakpoints. \\(y or n\\) $" \ |
a76e022a | 255 | "y" |
74cf1395 JM |
256 | } |
257 | ||
258 | proc do_fork_tests {} { | |
a76e022a | 259 | global gdb_prompt |
74cf1395 | 260 | |
a76e022a MS |
261 | # Verify that help is available for "set follow-fork-mode". |
262 | # | |
263 | gdb_test "help set follow-fork-mode" \ | |
264 | "Set debugger response to a program call of fork or vfork..* | |
74cf1395 JM |
265 | A fork or vfork creates a new process. follow-fork-mode can be:.* |
266 | .*parent - the original process is debugged after a fork.* | |
267 | .*child - the new process is debugged after a fork.* | |
ef04f396 | 268 | The unfollowed process will continue to run..* |
a76e022a MS |
269 | By default, the debugger will follow the parent process..*" \ |
270 | "help set follow-fork" | |
271 | ||
272 | # Verify that we can set follow-fork-mode, using an abbreviation | |
273 | # for both the flag and its value. | |
274 | # | |
275 | gdb_test_no_output "set follow-fork ch" | |
276 | ||
277 | gdb_test "show follow-fork" \ | |
278 | "Debugger response to a program call of fork or vfork is \"child\".*" \ | |
279 | "set follow-fork, using abbreviations" | |
280 | ||
281 | # Verify that we cannot set follow-fork-mode to nonsense. | |
282 | # | |
283 | gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \ | |
284 | "set follow-fork to nonsense is prohibited" | |
285 | ||
286 | gdb_test_no_output "set follow-fork parent" "reset parent" | |
287 | ||
288 | # Check that fork catchpoints are supported, as an indicator for whether | |
289 | # fork-following is supported. | |
290 | if [runto_main] then { check_fork_catchpoints } | |
291 | ||
292 | # Test the default behaviour, which is to follow the parent of a | |
293 | # fork, and detach from the child. Do this without catchpoints. | |
294 | # | |
295 | if [runto_main] then { default_fork_parent_follow } | |
296 | ||
297 | # Test the ability to explicitly follow the parent of a fork, and | |
298 | # detach from the child. Do this without catchpoints. | |
299 | # | |
300 | if [runto_main] then { explicit_fork_parent_follow } | |
301 | ||
302 | # Test the ability to follow the child of a fork, and detach from | |
303 | # the parent. Do this without catchpoints. | |
304 | # | |
305 | if [runto_main] then { explicit_fork_child_follow } | |
306 | ||
307 | # Test the ability to follow both child and parent of a fork. Do | |
308 | # this without catchpoints. | |
309 | # ??rehrauer: NYI. Will add testpoints here when implemented. | |
310 | # | |
311 | ||
312 | # Test the ability to have the debugger ask the user at fork-time | |
313 | # whether to follow the parent, child or both. Do this without | |
314 | # catchpoints. | |
315 | # ??rehrauer: NYI. Will add testpoints here when implemented. | |
316 | # | |
317 | ||
318 | # Test the ability to catch a fork, specify that the child be | |
319 | # followed, and continue. Make the catchpoint permanent. | |
320 | # | |
321 | if [runto_main] then { catch_fork_child_follow } | |
322 | ||
323 | # Test that parent breakpoints are successfully detached from the | |
324 | # child at fork time, even if the user removes them from the | |
325 | # breakpoints list after stopping at a fork catchpoint. | |
326 | if [runto_main] then { catch_fork_unpatch_child } | |
327 | ||
328 | # Test the ability to catch a fork, specify via a -do clause that | |
329 | # the parent be followed, and continue. Make the catchpoint temporary. | |
330 | # | |
331 | if [runto_main] then { tcatch_fork_parent_follow } | |
74cf1395 JM |
332 | } |
333 | ||
334 | # Start with a fresh gdb | |
335 | ||
336 | gdb_exit | |
337 | gdb_start | |
338 | gdb_reinitialize_dir $srcdir/$subdir | |
339 | gdb_load ${binfile} | |
340 | ||
e85a822c DJ |
341 | # The "Detaching..." and "Attaching..." messages may be hidden by |
342 | # default. | |
27d3a1a2 | 343 | gdb_test_no_output "set verbose" |
74cf1395 JM |
344 | |
345 | # This is a test of gdb's ability to follow the parent, child or both | |
346 | # parent and child of a Unix fork() system call. | |
347 | # | |
348 | do_fork_tests | |
349 | ||
350 | return 0 |