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