summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/foll-fork.exp
blob: 0ab2290c82c720c81941efdb20fa427c54ab8e77 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#   Copyright 1997-2022 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/>.

# Test relies on checking follow-fork output. Do not run if gdb debug is
# enabled as it will be redirected to the log.
if [gdb_debug_enabled] {
    untested "debug is enabled"
    return 0
}

standard_testfile

if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
    return -1
}

# Restart GDB and run the inferior to main.  Return 1 on success, 0 on failure.

proc setup {} {
    clean_restart $::testfile

    if { ![runto_main] } {
	return 0
    }

    return 1
}

# Check that fork catchpoints are supported, as an indicator for whether
# fork-following is supported.  Return 1 if they are, else 0.

proc_with_prefix check_fork_catchpoints {} {
  global gdb_prompt

  if { ![setup] } {
      return
  }

  # Verify that the system supports "catch fork".
  gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
  set has_fork_catchpoints 0
  gdb_test_multiple "continue" "continue to first fork catchpoint" {
    -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
      unsupported "continue to first fork catchpoint"
    }
    -re ".*Catchpoint.*$gdb_prompt $" {
      set has_fork_catchpoints 1
      pass "continue to first fork catchpoint"
    }
  }

  return $has_fork_catchpoints
}

# Test follow-fork to ensure that the correct process is followed, that
# the followed process stops where it is expected to stop, that processes
# are detached (or not) as expected, and that the inferior list has the
# expected contents after following the fork.  WHO is the argument to
# the 'set follow-fork-mode' command, DETACH is the argument to the 
# 'set detach-on-fork' command, and CMD is the GDB command used to 
# execute the program past the fork.  If the value of WHO or DETACH is
# 'default', the corresponding GDB command is skipped for that test.
# The value of CMD must be either 'next 2' or 'continue'.
proc_with_prefix test_follow_fork { follow-fork-mode detach-on-fork cmd } {
    global gdb_prompt
    global srcfile
    global testfile

    # Start a new debugger session each time so defaults are legitimate.
    if { ![setup] } {
	return
    }

    # The "Detaching..." and "Attaching..." messages may be hidden by
    # default.
    gdb_test_no_output "set verbose"

    # Set follow-fork-mode if we aren't using the default.
    if {${follow-fork-mode} == "default"} {
	set follow-fork-mode "parent"
    } else {
	gdb_test_no_output "set follow-fork ${follow-fork-mode}"
    }

    gdb_test "show follow-fork" \
    "Debugger response to a program call of fork or vfork is \"${follow-fork-mode}\"."

    # Set detach-on-fork mode if we aren't using the default.
    if {${detach-on-fork} == "default"} {
	set detach-on-fork "on"
    } else {
	gdb_test_no_output "set detach-on-fork ${detach-on-fork}"
    }

    gdb_test "show detach-on-fork" \
	     "Whether gdb will detach.* fork is ${detach-on-fork}."

    # Set a breakpoint after the fork if we aren't single-stepping
    # past the fork.
    if {$cmd == "continue"} {
	set bp_after_fork [gdb_get_line_number "set breakpoint here"]
	gdb_test "break ${srcfile}:$bp_after_fork" \
		 "Breakpoint.*, line $bp_after_fork.*" \
		 "set breakpoint after fork"
    }

    # Set up the output we expect to see after we run.
    set expected_re ""
    if {${follow-fork-mode} == "child"} {
	set expected_re "\\\[Attaching after.* fork to.*"
	if {${detach-on-fork} == "on"} {
	    append expected_re "\\\[Detaching after fork from .*"
	}
	append expected_re "set breakpoint here.*"
    } elseif {${follow-fork-mode} == "parent" && ${detach-on-fork} == "on"} {
	set expected_re "\\\[Detaching after fork from .*set breakpoint here.*"
    } else {
	set expected_re ".*set breakpoint here.*"
    }

    # Test running past and following the fork, using the parameters
    # set above.
    gdb_test $cmd $expected_re "$cmd past fork"

    # Check that we have the inferiors arranged correctly after
    # following the fork.
    set resume_unfollowed 0
    if {${follow-fork-mode} == "parent" && ${detach-on-fork} == "on"} {

	# Follow parent / detach child: the only inferior is the parent.
	gdb_test "info inferiors" "\\* 1 .* process.*"

    } elseif {${follow-fork-mode} == "parent" && ${detach-on-fork} == "off"} {

	# Follow parent / keep child: two inferiors under debug, the
	# parent is the current inferior.
	gdb_test "info inferiors" "\\* 1 .*process.* 2 .*process.*"

	gdb_test "inferior 2" "Switching to inferior 2 .*"
	set resume_unfollowed 1

    } elseif {${follow-fork-mode} == "child" && ${detach-on-fork} == "on"} {

	# Follow child / detach parent: the child is under debug and is
	# the current inferior.  The parent is listed but is not under
	# debug.
	gdb_test "info inferiors" " 1 .*<null>.*\\* 2 .*process.*"

    } elseif {${follow-fork-mode} == "child" && ${detach-on-fork} == "off"} {

	# Follow child / keep parent: two inferiors under debug, the
	# child is the current inferior.
	gdb_test "info inferiors" " 1 .*process.*\\* 2 .*process.*"

	gdb_test "inferior 1" "Switching to inferior 1 .*"
	set resume_unfollowed 1
    }

    if {$resume_unfollowed == 1} {
	if {$cmd == "next 2"} {

	    gdb_continue_to_end "continue unfollowed inferior to end"

	} elseif {$cmd == "continue"} {

	    gdb_continue_to_breakpoint \
		"continue unfollowed inferior to bp" \
		".* set breakpoint here.*"
	}
    }

    # If we end up with two inferiors, verify that they each end up with their
    # own program space.  Do this by setting a breakpoint, if we see two
    # locations it means there are two program spaces.
    if {${detach-on-fork} == "off" || ${follow-fork-mode} == "child"} {
	set bpnum "<unset>"
	gdb_test_multiple "break callee" "break callee" {
	    -re -wrap "Breakpoint ($::decimal) at $::hex: callee\\. \\(2 locations\\)" {
		set bpnum $expect_out(1,string)
		pass $gdb_test_name
	    }
	}

	set any {[^\r\n]+}

	set loc1_inf1 "$bpnum\\.1 $any inf 1"
	set loc1_inf2 "$bpnum\\.1 $any inf 2"

	set loc2_inf1 "$bpnum\\.2 $any inf 1"
	set loc2_inf2 "$bpnum\\.2 $any inf 2"

	gdb_test "info breakpoints $bpnum" \
	    "($loc1_inf1\r\n$loc2_inf2|$loc1_inf2\r\n$loc2_inf1)" \
	    "info breakpoints"
    }
}

set reading_in_symbols_re {(?:\r\nReading in symbols for [^\r\n]*)?}

# Test the ability to catch a fork, specify that the child be
# followed, and continue.  Make the catchpoint permanent.

proc_with_prefix catch_fork_child_follow {} {
    global gdb_prompt
    global srcfile
    global reading_in_symbols_re

    if { ![setup] } {
	return
    }

    set bp_after_fork [gdb_get_line_number "set breakpoint here"]

    gdb_test "catch fork" \
	"Catchpoint \[0-9\]* \\(fork\\)$reading_in_symbols_re" \
	"explicit child follow, set catch fork"

    # Verify that the catchpoint is mentioned in an "info breakpoints",
    # and further that the catchpoint mentions no process id.
    gdb_test "info breakpoints" \
	".*catchpoint.*keep y.*fork\[\r\n\]+" \
	"info breakpoints before fork"

    gdb_test "continue" \
	"Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
	"explicit child follow, catch fork"

    # Verify that the catchpoint is mentioned in an "info breakpoints",
    # and further that the catchpoint managed to capture a process id.
    gdb_test "info breakpoints" \
	".*catchpoint.*keep y.*fork, process.*" \
	"info breakpoints after fork"

    gdb_test_no_output "set follow-fork child"

    gdb_test "tbreak ${srcfile}:$bp_after_fork" \
	"Temporary breakpoint.*, line $bp_after_fork.*" \
	"set follow-fork child, tbreak"

    set expected_re "\\\[Attaching after.* fork to.*\\\[Detaching after fork from"
    append expected_re ".* at .*$bp_after_fork.*"
    gdb_test "continue" $expected_re "set follow-fork child, hit tbreak"

    # The parent has been detached; allow time for any output it might
    # generate to arrive, so that output doesn't get confused with
    # any expected debugger output from a subsequent testpoint.
    #
    exec sleep 1

    gdb_test "delete breakpoints" \
	"" \
	"set follow-fork child, cleanup" \
	"Delete all breakpoints. \\(y or n\\) $" \
	"y"
}

# Test that parent breakpoints are successfully detached from the
# child at fork time, even if the user removes them from the
# breakpoints list after stopping at a fork catchpoint.

proc_with_prefix catch_fork_unpatch_child {} {
    global gdb_prompt
    global srcfile

    if { ![setup] } {
	return
    }

    set bp_exit [gdb_get_line_number "at exit"]

    gdb_test "break callee" "file .*$srcfile, line .*" \
	"unpatch child, break at callee"
    gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
	"unpatch child, set catch fork"

    gdb_test "continue" \
	"Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
	"unpatch child, catch fork"

    # Delete all breakpoints and catchpoints.
    delete_breakpoints

    # Force $srcfile as the current GDB source can be in glibc sourcetree.
    gdb_test "break $srcfile:$bp_exit" \
	"Breakpoint .*file .*$srcfile, line .*" \
	"unpatch child, breakpoint at exit call"

    gdb_test_no_output "set follow-fork child" \
	"unpatch child, set follow-fork child"

    set test "unpatch child, unpatched parent breakpoints from child"
    gdb_test_multiple "continue" $test {
	-re "at exit.*$gdb_prompt $" {
	    pass "$test"
	}
	-re "SIGTRAP.*$gdb_prompt $" {
	    fail "$test"

	    # Explicitly kill this child, so we can continue gracefully
	    # with further testing...
	    send_gdb "kill\n"
	    gdb_expect {
		-re ".*Kill the program being debugged.*y or n. $" {
		    send_gdb "y\n"
		    gdb_expect -re "$gdb_prompt $" {}
		}
	    }
	}
    }
}

# Test the ability to catch a fork, specify via a -do clause that
# the parent be followed, and continue.  Make the catchpoint temporary.

proc_with_prefix tcatch_fork_parent_follow {} {
    global gdb_prompt
    global srcfile
    global reading_in_symbols_re

    if { ![setup] } {
	return
    }

    set bp_after_fork [gdb_get_line_number "set breakpoint here"]

    gdb_test "catch fork" \
	"Catchpoint \[0-9\]* \\(fork\\)$reading_in_symbols_re" \
	"explicit parent follow, set tcatch fork"

# ??rehrauer: I don't yet know how to get the id of the tcatch
# via this script, so that I can add a -do list to it.  For now,
# do the follow stuff after the catch happens.

    gdb_test "continue" \
	"Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
	"explicit parent follow, tcatch fork"

    gdb_test_no_output "set follow-fork parent"

    gdb_test "tbreak ${srcfile}:$bp_after_fork" \
	"Temporary breakpoint.*, line $bp_after_fork.*" \
	"set follow-fork parent, tbreak"

    gdb_test "continue" \
	"\\\[Detaching after fork from.* at .*$bp_after_fork.*" \
	"set follow-fork parent, hit tbreak"

    # The child has been detached; allow time for any output it might
    # generate to arrive, so that output doesn't get confused with
    # any expected debugger output from a subsequent testpoint.
    #
    exec sleep 1

    gdb_test "delete breakpoints" \
	"" \
	"set follow-fork parent, cleanup" \
	"Delete all breakpoints. \\(y or n\\) $" \
	"y"
}

# Test simple things about the "set follow-fork-mode" command.

proc_with_prefix test_set_follow_fork_command {} {
    clean_restart

    # Verify that help is available for "set follow-fork-mode".
    #
    gdb_test "help set follow-fork-mode" \
	"Set debugger response to a program call of fork or vfork..*
A fork or vfork creates a new process.  follow-fork-mode can be:.*
.*parent  - the original process is debugged after a fork.*
.*child   - the new process is debugged after a fork.*
The unfollowed process will continue to run..*
By default, the debugger will follow the parent process..*"

    # Verify that we can set follow-fork-mode, using an abbreviation
    # for both the flag and its value.
    #
    gdb_test_no_output "set follow-fork ch"

    gdb_test "show follow-fork" \
	"Debugger response to a program call of fork or vfork is \"child\".*" \
	"set follow-fork, using abbreviations"

    # Verify that we cannot set follow-fork-mode to nonsense.
    #
    gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \
	"set follow-fork to nonsense is prohibited"

    gdb_test_no_output "set follow-fork parent" "reset parent"
}

test_set_follow_fork_command

if { ![check_fork_catchpoints] } {
    untested "follow-fork not supported"
    return
}

# Test the basic follow-fork functionality using all combinations of
# values for follow-fork-mode and detach-on-fork, using either a
# breakpoint or single-step to execute past the fork.
#
# The first loop should be sufficient to test the defaults.  There
# is no need to test using the defaults in other permutations (e.g.
# "default" "on", "parent" "default", etc.).
foreach_with_prefix cmd {"next 2" "continue"} {
    test_follow_fork "default" "default" $cmd
}

# Now test all explicit permutations.
foreach_with_prefix follow-fork-mode {"parent" "child"} {
    foreach_with_prefix detach-on-fork {"on" "off"} {
	foreach_with_prefix cmd {"next 2" "continue"} {
	    test_follow_fork ${follow-fork-mode} ${detach-on-fork} $cmd
	}
    }
}

# Catchpoint tests.

catch_fork_child_follow
catch_fork_unpatch_child
tcatch_fork_parent_follow