1# Copyright 1997-2023 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 16if {![can_spawn_for_attach]} { 17 return 0 18} 19 20standard_testfile attach.c attach2.c attach3.c 21set binfile2 ${binfile}2 22set binfile3 ${binfile}3 23set escapedbinfile [string_to_regexp $binfile] 24 25#execute_anywhere "rm -f ${binfile} ${binfile2}" 26remote_exec build "rm -f ${binfile} ${binfile2} ${binfile3}" 27# For debugging this test 28# 29#log_user 1 30 31# build the first test case 32# 33if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } { 34 untested "failed to compile" 35 return -1 36} 37 38# Build the in-system-call test 39 40if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } { 41 untested "failed to compile in-system-call test" 42 return -1 43} 44 45# Build the third file, used to check attach when the exec-file has changed. 46 47if { [gdb_compile "${srcdir}/${subdir}/${srcfile3}" "${binfile3}" executable {debug}] != "" } { 48 untested "failed to compile attach exec-file changed test" 49 return -1 50} 51 52# This is a test of the error cases for gdb's ability to attach to a 53# running process. 54 55proc_with_prefix do_attach_failure_tests {} { 56 global gdb_prompt 57 global binfile 58 global escapedbinfile 59 global srcfile 60 61 clean_restart $binfile 62 63 # Figure out a regular expression that will match the sysroot, 64 # noting that the default sysroot is "target:", and also noting 65 # that GDB will strip "target:" from the start of filenames when 66 # operating on the local filesystem. However the default sysroot 67 # can be set via configure option --with-sysroot, which can be "/". 68 # If $binfile is a absolute path, so pattern 69 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make 70 # $sysroot simple. 71 set sysroot "\[^\r\n\]*" 72 73 # Start the program running and then wait for a bit, to be sure 74 # that it can be attached to. 75 76 set test_spawn_id [spawn_wait_for_attach $binfile] 77 set testpid [spawn_id_get_pid $test_spawn_id] 78 79 # Verify that we cannot attach to nonsense. 80 81 set test "attach to nonsense is prohibited" 82 gdb_test_multiple "attach abc" "$test" { 83 -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" { 84 pass "$test" 85 } 86 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" { 87 # Response expected from /proc-based systems. 88 pass "$test" 89 } 90 -re "Can't attach to process..*$gdb_prompt $" { 91 # Response expected on Cygwin 92 pass "$test" 93 } 94 -re "Attaching to.*$gdb_prompt $" { 95 fail "$test (bogus pid allowed)" 96 } 97 } 98 99 # Verify that we cannot attach to nonsense even if its initial part is 100 # a valid PID. 101 102 set test "attach to digits-starting nonsense is prohibited" 103 gdb_test_multiple "attach ${testpid}x" "$test" { 104 -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" { 105 pass "$test" 106 } 107 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" { 108 # Response expected from /proc-based systems. 109 pass "$test" 110 } 111 -re "Can't attach to process..*$gdb_prompt $" { 112 # Response expected on Cygwin 113 pass "$test" 114 } 115 -re "Attaching to.*$gdb_prompt $" { 116 fail "$test (bogus pid allowed)" 117 } 118 } 119 120 # Verify that we cannot attach to what appears to be a valid 121 # process ID, but is a process that doesn't exist. Traditionally, 122 # most systems didn't have a process with ID 0, so we take that as 123 # the default. However, there are a few exceptions. 124 125 set boguspid 0 126 if { [istarget "*-*-*bsd*"] } { 127 # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead 128 # (which should have the desired effect on any version of 129 # FreeBSD, and probably other *BSD's too). 130 set boguspid -1 131 } 132 set test "attach to nonexistent process is prohibited" 133 gdb_test_multiple "attach $boguspid" "$test" { 134 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" { 135 # Response expected on ptrace-based systems (i.e. HP-UX 10.20). 136 pass "$test" 137 } 138 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" { 139 # Response expected on ttrace-based systems (i.e. HP-UX 11.0). 140 pass "$test" 141 } 142 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" { 143 pass "$test" 144 } 145 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" { 146 pass "$test" 147 } 148 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" { 149 # Response expected from /proc-based systems. 150 pass "$test" 151 } 152 -re "Can't attach to process..*$gdb_prompt $" { 153 # Response expected on Cygwin 154 pass "$test" 155 } 156 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" { 157 # Response expected on the extended-remote target. 158 pass "$test" 159 } 160 } 161 162 # Verify that we can't double attach to the process. 163 164 set test "first attach" 165 gdb_test_multiple "attach $testpid" "$test" { 166 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" { 167 pass "$test" 168 } 169 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" { 170 # Response expected on Cygwin. 171 pass "$test" 172 } 173 } 174 175 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2" 176 gdb_test "inferior 2" "Switching to inferior 2.*" "switch to inferior 2" 177 178 # Probe this before the failing attach: the failed attach against GDBserver 179 # currently leaves the extended-remote target in a bad state. 180 set do_kfail [target_is_gdbserver] 181 182 set test "fail to attach again" 183 gdb_test_multiple "attach $testpid" "$test" { 184 -re "Attaching to process $testpid.*warning: process .* is already traced by process .*$gdb_prompt $" { 185 pass "$test" 186 } 187 -re "Attaching to process .* failed.*$gdb_prompt $" { 188 # Response expected when using gdbserver. 189 pass "$test" 190 } 191 } 192 193 # To ensure the target is still alive and working after this, try to run 194 # inferior 1. 195 gdb_test_no_output "set confirm off" 196 gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1" 197 198 if { $do_kfail } { setup_kfail "gdb/19558" "*-*-*" } 199 gdb_test "kill" "killed.*" "exit after attach failures" 200 201 # This can probably be replaced with a call to runto or runto_main once 202 # the kfail is removed. 203 gdb_breakpoint "main" 204 gdb_run_cmd 205 if { $do_kfail } { setup_kfail "gdb/19558" "*-*-*" } 206 gdb_test_multiple "" "stop at main" { 207 -wrap -re "Breakpoint $::decimal, main .*" { 208 pass $gdb_test_name 209 } 210 } 211 212 # Another "don't leave a process around" 213 kill_wait_spawned_process $test_spawn_id 214} 215 216# This is a test of gdb's ability to attach to a running process. 217 218proc_with_prefix do_attach_tests {} { 219 global gdb_prompt 220 global binfile 221 global escapedbinfile 222 global srcfile 223 global timeout 224 global decimal 225 226 clean_restart $binfile 227 228 # Figure out a regular expression that will match the sysroot, 229 # noting that the default sysroot is "target:", and also noting 230 # that GDB will strip "target:" from the start of filenames when 231 # operating on the local filesystem. However the default sysroot 232 # can be set via configure option --with-sysroot, which can be "/". 233 # If $binfile is a absolute path, so pattern 234 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make 235 # $sysroot simple. 236 set sysroot "\[^\r\n\]*" 237 238 # Start the program running and then wait for a bit, to be sure 239 # that it can be attached to. 240 241 set test_spawn_id [spawn_wait_for_attach $binfile] 242 set testpid [spawn_id_get_pid $test_spawn_id] 243 244 # Verify that we can attach to the process by first giving its 245 # executable name via the file command, and using attach with the 246 # process ID. 247 248 # (Actually, the test system appears to do this automatically for 249 # us. So, we must also be prepared to be asked if we want to 250 # discard an existing set of symbols.) 251 252 set test "set file, before attach1" 253 gdb_test_multiple "file $binfile" "$test" { 254 -re "Load new symbol table from.*y or n. $" { 255 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \ 256 "$test (re-read)" 257 } 258 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" { 259 pass "$test" 260 } 261 } 262 263 set test "attach1, after setting file" 264 gdb_test_multiple "attach $testpid" "$test" { 265 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" { 266 pass "$test" 267 } 268 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" { 269 # Response expected on Cygwin 270 pass "$test" 271 } 272 } 273 274 # Verify that we can "see" the variable "should_exit" in the 275 # program, and that it is zero. 276 277 gdb_test "print should_exit" " = 0" "after attach1, print should_exit" 278 279 # Detach the process. 280 281 gdb_test "detach" \ 282 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \ 283 "attach1 detach" 284 285 # Wait a bit for gdb to finish detaching 286 287 exec sleep 5 288 289 # Purge the symbols from gdb's brain. (We want to be certain the 290 # next attach, which won't be preceded by a "file" command, is 291 # really getting the executable file without our help.) 292 293 set old_timeout $timeout 294 set timeout 15 295 set test "attach1, purging symbols after detach" 296 gdb_test_multiple "file" "$test" { 297 -re "No executable file now.*Discard symbol table.*y or n. $" { 298 gdb_test "y" "No symbol file now." "$test" 299 } 300 } 301 set timeout $old_timeout 302 303 # Verify that we can attach to the process just by giving the 304 # process ID. 305 306 set test "attach2, with no file" 307 set found_exec_file 0 308 gdb_test_multiple "attach $testpid" "$test" { 309 -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" { 310 # On Cygwin, the DLL's symbol tables are loaded prior to the 311 # executable's symbol table. This in turn always results in 312 # asking the user for actually loading the symbol table of the 313 # executable. 314 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \ 315 "$test (reset file)" 316 317 set found_exec_file 1 318 } 319 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" { 320 pass "$test" 321 set found_exec_file 1 322 } 323 } 324 325 if {$found_exec_file == 0} { 326 set test "load file manually, after attach2" 327 gdb_test_multiple "file $binfile" "$test" { 328 -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" { 329 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \ 330 "$test (re-read)" 331 } 332 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" { 333 pass "$test" 334 } 335 } 336 } 337 338 # Verify that we can modify the variable "should_exit" in the 339 # program. 340 341 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit" 342 343 # Verify that the modification really happened. 344 345 gdb_breakpoint [gdb_get_line_number "postloop"] temporary 346 gdb_continue_to_breakpoint "postloop" ".* postloop .*" 347 348 # Allow the test process to exit, to cleanup after ourselves. 349 350 gdb_continue_to_end "after attach2, exit" 351 352 # Make sure we don't leave a process around to confuse 353 # the next test run (and prevent the compile by keeping 354 # the text file busy), in case the "set should_exit" didn't 355 # work. 356 357 kill_wait_spawned_process $test_spawn_id 358 359 set test_spawn_id [spawn_wait_for_attach $binfile] 360 set testpid [spawn_id_get_pid $test_spawn_id] 361 362 # Verify that we can attach to the process, and find its a.out 363 # when we're cd'd to some directory that doesn't contain the 364 # a.out. (We use the source path set by the "dir" command.) 365 366 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \ 367 "set source path" 368 369 gdb_test "cd /tmp" "Working directory /tmp." \ 370 "cd away from process working directory" 371 372 # Explicitly flush out any knowledge of the previous attachment. 373 374 set test "before attach3, flush symbols" 375 gdb_test_multiple "symbol-file" "$test" { 376 -re "Discard symbol table from.*y or n. $" { 377 gdb_test "y" "No symbol file now." \ 378 "$test" 379 } 380 -re "No symbol file now.*$gdb_prompt $" { 381 pass "$test" 382 } 383 } 384 385 gdb_test "exec" "No executable file now." \ 386 "before attach3, flush exec" 387 388 gdb_test "attach $testpid" \ 389 "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \ 390 "attach when process' a.out not in cwd" 391 392 set test "after attach3, exit" 393 gdb_test "kill" \ 394 "" \ 395 "$test" \ 396 "Kill the program being debugged.*y or n. $" \ 397 "y" 398 399 # Another "don't leave a process around" 400 kill_wait_spawned_process $test_spawn_id 401} 402 403# Test attaching when the target is inside a system call. 404 405proc_with_prefix do_call_attach_tests {} { 406 global gdb_prompt 407 global binfile2 408 409 clean_restart 410 411 set test_spawn_id [spawn_wait_for_attach $binfile2] 412 set testpid [spawn_id_get_pid $test_spawn_id] 413 414 # Attach 415 416 gdb_test "file $binfile2" ".*" "load file" 417 set test "attach call" 418 gdb_test_multiple "attach $testpid" "$test" { 419 -re "warning: reading register.*I.*O error.*$gdb_prompt $" { 420 fail "$test (read register error)" 421 } 422 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" { 423 pass "$test" 424 } 425 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" { 426 pass "$test" 427 } 428 } 429 430 # See if other registers are problems 431 432 set test "info other register" 433 gdb_test_multiple "i r r3" "$test" { 434 -re "warning: reading register.*$gdb_prompt $" { 435 fail "$test" 436 } 437 -re "r3.*$gdb_prompt $" { 438 pass "$test" 439 } 440 } 441 442 # Get rid of the process 443 444 gdb_test "p should_exit = 1" 445 gdb_continue_to_end 446 447 # Be paranoid 448 449 kill_wait_spawned_process $test_spawn_id 450} 451 452proc_with_prefix do_command_attach_tests {} { 453 global gdb_prompt 454 global binfile 455 456 if {![isnative]} { 457 unsupported "command attach test" 458 return 0 459 } 460 461 set test_spawn_id [spawn_wait_for_attach $binfile] 462 set testpid [spawn_id_get_pid $test_spawn_id] 463 464 gdb_exit 465 466 # gdb_spawn_attach_cmdline records test results. No need to explicitly 467 # call pass/fail here. 468 gdb_spawn_attach_cmdline $testpid 469 470 # Get rid of the process 471 kill_wait_spawned_process $test_spawn_id 472} 473 474# Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where 475# "run" would run before the attach finished - PR17347. 476 477proc_with_prefix test_command_line_attach_run {} { 478 global gdb_prompt 479 global binfile 480 481 # The --pid option is used to attach to a process using the native target. 482 # Start GDB and run to main just to see what the execution target is, skip 483 # if it's not the native target. 484 clean_restart $binfile 485 486 if { ![runto_main] } { 487 return 488 } 489 490 if { ![gdb_is_target_native] } { 491 unsupported "commandline attach run test" 492 return 493 } 494 495 set test_spawn_id [spawn_wait_for_attach $binfile] 496 set testpid [spawn_id_get_pid $test_spawn_id] 497 498 set test "run to prompt" 499 gdb_exit 500 501 set res [gdb_spawn_with_cmdline_opts \ 502 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""] 503 if { $res != 0} { 504 fail $test 505 kill_wait_spawned_process $test_spawn_id 506 return $res 507 } 508 gdb_test_multiple "" $test { 509 -re {Attaching to.*Start it from the beginning\? \(y or n\) } { 510 pass $test 511 } 512 } 513 514 send_gdb "y\n" 515 516 set test "run to main" 517 gdb_test_multiple "" $test { 518 -re "Temporary breakpoint .* main .*$gdb_prompt $" { 519 pass $test 520 } 521 } 522 523 # Get rid of the process 524 kill_wait_spawned_process $test_spawn_id 525} 526 527 528# This is a test of 'set exec-file-mismatch' handling. 529 530proc_with_prefix do_attach_exec_mismatch_handling_tests {} { 531 global gdb_prompt 532 global binfile 533 global binfile2 534 global binfile3 535 536 clean_restart $binfile 537 538 # Start two programs that can be attached to. 539 # The first program contains a 'int bidule' variable, the second a 'float bidule'. 540 541 set test_spawn_id [spawn_wait_for_attach $binfile] 542 set testpid [spawn_id_get_pid $test_spawn_id] 543 set test_spawn_id2 [spawn_wait_for_attach $binfile2] 544 set testpid2 [spawn_id_get_pid $test_spawn_id2] 545 546 547 # Test with the default value of 'set exec-file-mismatch load". 548 set test "mismatch load" 549 gdb_test "attach $testpid" "Attaching to program.*" "$test attach1" 550 # Verify that we can "see" the variable "bidule" in the 551 # program, and that it is an integer. 552 gdb_test "ptype bidule" " = int" "$test after attach1, bidule is int" 553 # Detach the process. 554 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach1" 555 gdb_test_multiple "attach $testpid2" "$test attach2" { 556 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" { 557 pass "$test attach2" 558 } 559 } 560 gdb_test "y" "Reading symbols from .*attach2.*" "$test load attach2" 561 # Verify that we can "see" the variable "bidule" in the 562 # program, and that it is a float. 563 gdb_test "ptype bidule" " = float" "$test after attach2 and load, bidule is float" 564 # Detach the process. 565 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach2" 566 567 568 # Test with 'set exec-file-mismatch warn". 569 set test "mismatch warn" 570 gdb_test_no_output "set exec-file-mismatch warn" 571 gdb_test_multiple "attach $testpid" "$test attach" { 572 -re "Attaching to program.*exec-file-mismatch handling is currently \"warn\".*$gdb_prompt" { 573 pass "$test attach" 574 } 575 } 576 # Verify that we still (wrongly) "see" the variable "bidule" as a float, 577 # as we have not loaded the correct exec-file. 578 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float" 579 # Detach the process. 580 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach" 581 582 583 # Same test but with 'set exec-file-mismatch off". 584 set test "mismatch off" 585 gdb_test_no_output "set exec-file-mismatch off" 586 gdb_test_multiple "attach $testpid" "$test attach" { 587 -re "Attaching to program.*$gdb_prompt" { 588 pass "$test attach" 589 } 590 } 591 # Verify that we still (wrongly) "see" the variable "bidule" as a float, 592 # as we have not warned the user and not loaded the correct exec-file 593 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float" 594 # Detach the process. 595 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach" 596 597 # Test that the 'exec-file' changed is checked before exec-file-mismatch. 598 set test "mismatch exec-file changed has priority" 599 gdb_test_no_output "set exec-file-mismatch ask" 600 gdb_test_multiple "attach $testpid" "$test attach1 again, initial exec-file" { 601 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach\".*\(y or n\)" { 602 gdb_test "y" "Reading symbols from .*attach.*" $gdb_test_name 603 } 604 } 605 606 607 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach initial exec-file" 608 609 # Change the exec-file and attach to a new process using the changed file. 610 remote_exec build "mv ${binfile} ${binfile}.initial" 611 remote_exec build "mv ${binfile3} ${binfile}" 612 # Ensure GDB detects ${binfile} has changed when checking timestamp. 613 sleep 1 614 remote_exec build "touch ${binfile}" 615 set test_spawn_id3 [spawn_wait_for_attach $binfile] 616 set testpid3 [spawn_id_get_pid $test_spawn_id3] 617 618 gdb_test "attach $testpid3" "Attaching to program.*attach' has changed; re-reading symbols.*" \ 619 "$test attach1 again, after changing exec-file" 620 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach after attach changed exec-file" 621 622 # Now, test the situation when current exec-file has changed 623 # and we attach to a pid using another file. 624 # Ensure GDB detects ${binfile} has changed when checking timestamp. 625 sleep 1 626 remote_exec build "touch ${binfile}" 627 628 gdb_test_multiple "attach $testpid2" "$test attach2" { 629 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" { 630 gdb_test "y" "Reading symbols from .*attach2.*" $gdb_test_name 631 } 632 } 633 634 # Restore initial build situation. 635 remote_exec build "mv ${binfile} ${binfile3}" 636 remote_exec build "mv ${binfile}.initial ${binfile}" 637 638 # Don't leave a process around 639 kill_wait_spawned_process $test_spawn_id 640 kill_wait_spawned_process $test_spawn_id2 641 kill_wait_spawned_process $test_spawn_id3 642} 643 644do_attach_tests 645do_attach_failure_tests 646do_call_attach_tests 647do_attach_exec_mismatch_handling_tests 648 649# Test "gdb --pid" 650 651do_command_attach_tests 652 653 654test_command_line_attach_run 655 656return 0 657