1# Copyright 1997-2019 Free Software Foundation, Inc. 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 5# the Free Software Foundation; either version 3 of the License, or 6# (at your option) any later version. 7# 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. 12# 13# You should have received a copy of the GNU General Public License 14# along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16# Until "set follow-fork-mode" and "catch fork" are implemented on 17# other targets... 18# 19if { ![istarget "*-*-linux*"] && ![istarget "*-*-openbsd*"] } then { 20 continue 21} 22 23standard_testfile 24 25if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} { 26 return -1 27} 28 29proc check_fork_catchpoints {} { 30 global gdb_prompt 31 32 # Verify that the system supports "catch fork". 33 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint" 34 set has_fork_catchpoints 0 35 gdb_test_multiple "continue" "continue to first fork catchpoint" { 36 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" { 37 unsupported "continue to first fork catchpoint" 38 } 39 -re ".*Catchpoint.*$gdb_prompt $" { 40 set has_fork_catchpoints 1 41 pass "continue to first fork catchpoint" 42 } 43 } 44 45 if {$has_fork_catchpoints == 0} { 46 unsupported "fork catchpoints" 47 return -code return 48 } 49} 50 51# Test follow-fork to ensure that the correct process is followed, that 52# the followed process stops where it is expected to stop, that processes 53# are detached (or not) as expected, and that the inferior list has the 54# expected contents after following the fork. WHO is the argument to 55# the 'set follow-fork-mode' command, DETACH is the argument to the 56# 'set detach-on-fork' command, and CMD is the GDB command used to 57# execute the program past the fork. If the value of WHO or DETACH is 58# 'default', the corresponding GDB command is skipped for that test. 59# The value of CMD must be either 'next 2' or 'continue'. 60proc test_follow_fork { who detach cmd } { 61 global gdb_prompt 62 global srcfile 63 global testfile 64 65 with_test_prefix "follow $who, detach $detach, command \"$cmd\"" { 66 67 # Start a new debugger session each time so defaults are legitimate. 68 clean_restart $testfile 69 70 if ![runto_main] { 71 untested "could not run to main" 72 return -1 73 } 74 75 # The "Detaching..." and "Attaching..." messages may be hidden by 76 # default. 77 gdb_test_no_output "set verbose" 78 79 # Set follow-fork-mode if we aren't using the default. 80 if {$who == "default"} { 81 set who "parent" 82 } else { 83 gdb_test_no_output "set follow-fork $who" 84 } 85 86 gdb_test "show follow-fork" \ 87 "Debugger response to a program call of fork or vfork is \"$who\"." \ 88 "show follow-fork" 89 90 # Set detach-on-fork mode if we aren't using the default. 91 if {$detach == "default"} { 92 set detach "on" 93 } else { 94 gdb_test_no_output "set detach-on-fork $detach" 95 } 96 97 gdb_test "show detach-on-fork" \ 98 "Whether gdb will detach.* fork is $detach." \ 99 "show detach-on-fork" 100 101 # Set a breakpoint after the fork if we aren't single-stepping 102 # past the fork. 103 if {$cmd == "continue"} { 104 set bp_after_fork [gdb_get_line_number "set breakpoint here"] 105 gdb_test "break ${srcfile}:$bp_after_fork" \ 106 "Breakpoint.*, line $bp_after_fork.*" \ 107 "set breakpoint after fork" 108 } 109 110 # Set up the output we expect to see after we run. 111 set expected_re "" 112 if {$who == "child"} { 113 set expected_re "\\\[Attaching after.* fork to.*" 114 if {$detach == "on"} { 115 append expected_re "\\\[Detaching after fork from .*" 116 } 117 append expected_re "set breakpoint here.*" 118 } elseif {$who == "parent" && $detach == "on"} { 119 set expected_re "\\\[Detaching after fork from .*set breakpoint here.*" 120 } else { 121 set expected_re ".*set breakpoint here.*" 122 } 123 124 # Test running past and following the fork, using the parameters 125 # set above. 126 gdb_test $cmd $expected_re "$cmd past fork" 127 128 # Check that we have the inferiors arranged correctly after 129 # following the fork. 130 set resume_unfollowed 0 131 if {$who == "parent" && $detach == "on"} { 132 133 # Follow parent / detach child: the only inferior is the parent. 134 gdb_test "info inferiors" "\\* 1 .* process.*" \ 135 "info inferiors" 136 137 } elseif {$who == "parent" && $detach == "off"} { 138 139 # Follow parent / keep child: two inferiors under debug, the 140 # parent is the current inferior. 141 gdb_test "info inferiors" "\\* 1 .*process.* 2 .*process.*" \ 142 "info inferiors" 143 144 gdb_test "inferior 2" "Switching to inferior 2 .*" 145 set resume_unfollowed 1 146 147 } elseif {$who == "child" && $detach == "on"} { 148 149 # Follow child / detach parent: the child is under debug and is 150 # the current inferior. The parent is listed but is not under 151 # debug. 152 gdb_test "info inferiors" " 1 .*<null>.*\\* 2 .*process.*" \ 153 "info inferiors" 154 155 } elseif {$who == "child" && $detach == "off"} { 156 157 # Follow child / keep parent: two inferiors under debug, the 158 # child is the current inferior. 159 gdb_test "info inferiors" " 1 .*process.*\\* 2 .*process.*" \ 160 "info inferiors" 161 162 gdb_test "inferior 1" "Switching to inferior 1 .*" 163 set resume_unfollowed 1 164 } 165 166 if {$resume_unfollowed == 1} { 167 if {$cmd == "next 2"} { 168 169 gdb_continue_to_end "continue unfollowed inferior to end" 170 171 } elseif {$cmd == "continue"} { 172 173 gdb_continue_to_breakpoint \ 174 "continue unfollowed inferior to bp" \ 175 ".* set breakpoint here.*" 176 } 177 } 178 } 179} 180 181proc catch_fork_child_follow {} { 182 global gdb_prompt 183 global srcfile 184 185 set bp_after_fork [gdb_get_line_number "set breakpoint here"] 186 187 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \ 188 "explicit child follow, set catch fork" 189 190 # Verify that the catchpoint is mentioned in an "info breakpoints", 191 # and further that the catchpoint mentions no process id. 192 # 193 set test_name "info shows catchpoint without pid" 194 gdb_test_multiple "info breakpoints" "$test_name" { 195 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" { 196 pass "$test_name" 197 } 198 } 199 200 gdb_test "continue" \ 201 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \ 202 "explicit child follow, catch fork" 203 204 # Verify that the catchpoint is mentioned in an "info breakpoints", 205 # and further that the catchpoint managed to capture a process id. 206 # 207 set test_name "info shows catchpoint without pid" 208 gdb_test_multiple "info breakpoints" "$test_name" { 209 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" { 210 pass "$test_name" 211 } 212 } 213 214 gdb_test_no_output "set follow-fork child" 215 216 gdb_test "tbreak ${srcfile}:$bp_after_fork" \ 217 "Temporary breakpoint.*, line $bp_after_fork.*" \ 218 "set follow-fork child, tbreak" 219 220 set expected_re "\\\[Attaching after.* fork to.*\\\[Detaching after fork from" 221 append expected_re ".* at .*$bp_after_fork.*" 222 gdb_test "continue" $expected_re "set follow-fork child, hit tbreak" 223 224 # The parent has been detached; allow time for any output it might 225 # generate to arrive, so that output doesn't get confused with 226 # any expected debugger output from a subsequent testpoint. 227 # 228 exec sleep 1 229 230 gdb_test "delete breakpoints" \ 231 "" \ 232 "set follow-fork child, cleanup" \ 233 "Delete all breakpoints. \\(y or n\\) $" \ 234 "y" 235} 236 237proc catch_fork_unpatch_child {} { 238 global gdb_prompt 239 global srcfile 240 241 set bp_exit [gdb_get_line_number "at exit"] 242 243 gdb_test "break callee" "file .*$srcfile, line .*" \ 244 "unpatch child, break at callee" 245 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \ 246 "unpatch child, set catch fork" 247 248 gdb_test "continue" \ 249 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \ 250 "unpatch child, catch fork" 251 252 # Delete all breakpoints and catchpoints. 253 delete_breakpoints 254 255 # Force $srcfile as the current GDB source can be in glibc sourcetree. 256 gdb_test "break $srcfile:$bp_exit" \ 257 "Breakpoint .*file .*$srcfile, line .*" \ 258 "unpatch child, breakpoint at exit call" 259 260 gdb_test_no_output "set follow-fork child" \ 261 "unpatch child, set follow-fork child" 262 263 set test "unpatch child, unpatched parent breakpoints from child" 264 gdb_test_multiple "continue" $test { 265 -re "at exit.*$gdb_prompt $" { 266 pass "$test" 267 } 268 -re "SIGTRAP.*$gdb_prompt $" { 269 fail "$test" 270 271 # Explicitly kill this child, so we can continue gracefully 272 # with further testing... 273 send_gdb "kill\n" 274 gdb_expect { 275 -re ".*Kill the program being debugged.*y or n. $" { 276 send_gdb "y\n" 277 gdb_expect -re "$gdb_prompt $" {} 278 } 279 } 280 } 281 } 282} 283 284proc tcatch_fork_parent_follow {} { 285 global gdb_prompt 286 global srcfile 287 288 set bp_after_fork [gdb_get_line_number "set breakpoint here"] 289 290 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \ 291 "explicit parent follow, set tcatch fork" 292 293# ??rehrauer: I don't yet know how to get the id of the tcatch 294# via this script, so that I can add a -do list to it. For now, 295# do the follow stuff after the catch happens. 296 297 gdb_test "continue" \ 298 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \ 299 "explicit parent follow, tcatch fork" 300 301 gdb_test_no_output "set follow-fork parent" 302 303 gdb_test "tbreak ${srcfile}:$bp_after_fork" \ 304 "Temporary breakpoint.*, line $bp_after_fork.*" \ 305 "set follow-fork parent, tbreak" 306 307 gdb_test "continue" \ 308 "\\\[Detaching after fork from.* at .*$bp_after_fork.*" \ 309 "set follow-fork parent, hit tbreak" 310 311 # The child has been detached; allow time for any output it might 312 # generate to arrive, so that output doesn't get confused with 313 # any expected debugger output from a subsequent testpoint. 314 # 315 exec sleep 1 316 317 gdb_test "delete breakpoints" \ 318 "" \ 319 "set follow-fork parent, cleanup" \ 320 "Delete all breakpoints. \\(y or n\\) $" \ 321 "y" 322} 323 324proc do_fork_tests {} { 325 global gdb_prompt 326 global testfile 327 328 # Verify that help is available for "set follow-fork-mode". 329 # 330 gdb_test "help set follow-fork-mode" \ 331 "Set debugger response to a program call of fork or vfork..* 332A fork or vfork creates a new process. follow-fork-mode can be:.* 333.*parent - the original process is debugged after a fork.* 334.*child - the new process is debugged after a fork.* 335The unfollowed process will continue to run..* 336By default, the debugger will follow the parent process..*" \ 337 "help set follow-fork" 338 339 # Verify that we can set follow-fork-mode, using an abbreviation 340 # for both the flag and its value. 341 # 342 gdb_test_no_output "set follow-fork ch" 343 344 gdb_test "show follow-fork" \ 345 "Debugger response to a program call of fork or vfork is \"child\".*" \ 346 "set follow-fork, using abbreviations" 347 348 # Verify that we cannot set follow-fork-mode to nonsense. 349 # 350 gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \ 351 "set follow-fork to nonsense is prohibited" 352 353 gdb_test_no_output "set follow-fork parent" "reset parent" 354 355 # Check that fork catchpoints are supported, as an indicator for whether 356 # fork-following is supported. 357 if [runto_main] then { check_fork_catchpoints } 358 359 # Test the basic follow-fork functionality using all combinations of 360 # values for follow-fork-mode and detach-on-fork, using either a 361 # breakpoint or single-step to execute past the fork. 362 # 363 # The first loop should be sufficient to test the defaults. There 364 # is no need to test using the defaults in other permutations (e.g. 365 # "default" "on", "parent" "default", etc.). 366 foreach cmd {"next 2" "continue"} { 367 test_follow_fork "default" "default" $cmd 368 } 369 370 # Now test all explicit permutations. 371 foreach who {"parent" "child"} { 372 foreach detach {"on" "off"} { 373 foreach cmd {"next 2" "continue"} { 374 test_follow_fork $who $detach $cmd 375 } 376 } 377 } 378 379 # Catchpoint tests. 380 381 # Restart to eliminate any effects of the follow-fork tests. 382 clean_restart $testfile 383 gdb_test_no_output "set verbose" 384 385 # Test the ability to catch a fork, specify that the child be 386 # followed, and continue. Make the catchpoint permanent. 387 # 388 if [runto_main] then { catch_fork_child_follow } 389 390 # Test that parent breakpoints are successfully detached from the 391 # child at fork time, even if the user removes them from the 392 # breakpoints list after stopping at a fork catchpoint. 393 if [runto_main] then { catch_fork_unpatch_child } 394 395 # Test the ability to catch a fork, specify via a -do clause that 396 # the parent be followed, and continue. Make the catchpoint temporary. 397 # 398 if [runto_main] then { tcatch_fork_parent_follow } 399} 400 401# This is a test of gdb's ability to follow the parent, child or both 402# parent and child of a Unix fork() system call. 403# 404do_fork_tests 405 406return 0 407