1 # Copyright 1997-2020 Free Software Foundation, Inc.
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.
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.
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/>. */
16 if {![can_spawn_for_attach]} {
20 standard_testfile attach.c attach2.c attach3.c
21 set binfile2 ${binfile}2
22 set binfile3 ${binfile}3
23 set escapedbinfile [string_to_regexp $binfile]
25 #execute_anywhere "rm -f ${binfile} ${binfile2}"
26 remote_exec build "rm -f ${binfile} ${binfile2} ${binfile3}"
27 # For debugging this test
31 # build the first test case
33 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
34 untested "failed to compile"
38 # Build the in-system-call test
40 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
41 untested "failed to compile in-system-call test"
45 # Build the third file, used to check attach when the exec-file has changed.
47 if { [gdb_compile "${srcdir}/${subdir}/${srcfile3}" "${binfile3}" executable {debug}] != "" } {
48 untested "failed to compile attach exec-file changed test"
52 if [get_compiler_info] {
56 # This is a test of the error cases for gdb's ability to attach to a
59 proc_with_prefix do_attach_failure_tests {} {
65 clean_restart $binfile
67 # Figure out a regular expression that will match the sysroot,
68 # noting that the default sysroot is "target:", and also noting
69 # that GDB will strip "target:" from the start of filenames when
70 # operating on the local filesystem. However the default sysroot
71 # can be set via configure option --with-sysroot, which can be "/".
72 # If $binfile is a absolute path, so pattern
73 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
75 set sysroot "\[^\r\n\]*"
77 # Start the program running and then wait for a bit, to be sure
78 # that it can be attached to.
80 set test_spawn_id [spawn_wait_for_attach $binfile]
81 set testpid [spawn_id_get_pid $test_spawn_id]
83 # Verify that we cannot attach to nonsense.
85 set test "attach to nonsense is prohibited"
86 gdb_test_multiple "attach abc" "$test" {
87 -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
90 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
91 # Response expected from /proc-based systems.
94 -re "Can't attach to process..*$gdb_prompt $" {
95 # Response expected on Cygwin
98 -re "Attaching to.*$gdb_prompt $" {
99 fail "$test (bogus pid allowed)"
103 # Verify that we cannot attach to nonsense even if its initial part is
106 set test "attach to digits-starting nonsense is prohibited"
107 gdb_test_multiple "attach ${testpid}x" "$test" {
108 -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" {
111 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
112 # Response expected from /proc-based systems.
115 -re "Can't attach to process..*$gdb_prompt $" {
116 # Response expected on Cygwin
119 -re "Attaching to.*$gdb_prompt $" {
120 fail "$test (bogus pid allowed)"
124 # Verify that we cannot attach to what appears to be a valid
125 # process ID, but is a process that doesn't exist. Traditionally,
126 # most systems didn't have a process with ID 0, so we take that as
127 # the default. However, there are a few exceptions.
130 if { [istarget "*-*-*bsd*"] } {
131 # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead
132 # (which should have the desired effect on any version of
133 # FreeBSD, and probably other *BSD's too).
136 set test "attach to nonexistent process is prohibited"
137 gdb_test_multiple "attach $boguspid" "$test" {
138 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
139 # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
142 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
143 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
146 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
149 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
152 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
153 # Response expected from /proc-based systems.
156 -re "Can't attach to process..*$gdb_prompt $" {
157 # Response expected on Cygwin
160 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
161 # Response expected on the extended-remote target.
166 # Verify that we can't double attach to the process.
168 set test "first attach"
169 gdb_test_multiple "attach $testpid" "$test" {
170 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
173 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
174 # Response expected on Cygwin.
179 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
180 gdb_test "inferior 2" "Switching to inferior 2.*" "switch to inferior 2"
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 $" {
187 -re "Attaching to process .* failed.*$gdb_prompt $" {
188 # Response expected when using gdbserver.
193 gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1"
194 set test "exit after attach failures"
198 "Kill the program being debugged.*y or n. $" \
201 # Another "don't leave a process around"
202 kill_wait_spawned_process $test_spawn_id
205 # This is a test of gdb's ability to attach to a running process.
207 proc_with_prefix do_attach_tests {} {
210 global escapedbinfile
215 clean_restart $binfile
217 # Figure out a regular expression that will match the sysroot,
218 # noting that the default sysroot is "target:", and also noting
219 # that GDB will strip "target:" from the start of filenames when
220 # operating on the local filesystem. However the default sysroot
221 # can be set via configure option --with-sysroot, which can be "/".
222 # If $binfile is a absolute path, so pattern
223 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
225 set sysroot "\[^\r\n\]*"
227 # Start the program running and then wait for a bit, to be sure
228 # that it can be attached to.
230 set test_spawn_id [spawn_wait_for_attach $binfile]
231 set testpid [spawn_id_get_pid $test_spawn_id]
233 # Verify that we can attach to the process by first giving its
234 # executable name via the file command, and using attach with the
237 # (Actually, the test system appears to do this automatically for
238 # us. So, we must also be prepared to be asked if we want to
239 # discard an existing set of symbols.)
241 set test "set file, before attach1"
242 gdb_test_multiple "file $binfile" "$test" {
243 -re "Load new symbol table from.*y or n. $" {
244 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
247 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
252 set test "attach1, after setting file"
253 gdb_test_multiple "attach $testpid" "$test" {
254 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
257 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
258 # Response expected on Cygwin
263 # Verify that we can "see" the variable "should_exit" in the
264 # program, and that it is zero.
266 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
268 # Detach the process.
271 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
274 # Wait a bit for gdb to finish detaching
278 # Purge the symbols from gdb's brain. (We want to be certain the
279 # next attach, which won't be preceded by a "file" command, is
280 # really getting the executable file without our help.)
282 set old_timeout $timeout
284 set test "attach1, purging symbols after detach"
285 gdb_test_multiple "file" "$test" {
286 -re "No executable file now.*Discard symbol table.*y or n. $" {
287 gdb_test "y" "No symbol file now." "$test"
290 set timeout $old_timeout
292 # Verify that we can attach to the process just by giving the
295 set test "attach2, with no file"
296 set found_exec_file 0
297 gdb_test_multiple "attach $testpid" "$test" {
298 -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
299 # On Cygwin, the DLL's symbol tables are loaded prior to the
300 # executable's symbol table. This in turn always results in
301 # asking the user for actually loading the symbol table of the
303 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
306 set found_exec_file 1
308 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
310 set found_exec_file 1
314 if {$found_exec_file == 0} {
315 set test "load file manually, after attach2"
316 gdb_test_multiple "file $binfile" "$test" {
317 -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
318 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
321 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
327 # Verify that we can modify the variable "should_exit" in the
330 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
332 # Verify that the modification really happened.
334 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
335 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
337 # Allow the test process to exit, to cleanup after ourselves.
339 gdb_continue_to_end "after attach2, exit"
341 # Make sure we don't leave a process around to confuse
342 # the next test run (and prevent the compile by keeping
343 # the text file busy), in case the "set should_exit" didn't
346 kill_wait_spawned_process $test_spawn_id
348 set test_spawn_id [spawn_wait_for_attach $binfile]
349 set testpid [spawn_id_get_pid $test_spawn_id]
351 # Verify that we can attach to the process, and find its a.out
352 # when we're cd'd to some directory that doesn't contain the
353 # a.out. (We use the source path set by the "dir" command.)
355 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
358 gdb_test "cd /tmp" "Working directory /tmp." \
359 "cd away from process working directory"
361 # Explicitly flush out any knowledge of the previous attachment.
363 set test "before attach3, flush symbols"
364 gdb_test_multiple "symbol-file" "$test" {
365 -re "Discard symbol table from.*y or n. $" {
366 gdb_test "y" "No symbol file now." \
369 -re "No symbol file now.*$gdb_prompt $" {
374 gdb_test "exec" "No executable file now." \
375 "before attach3, flush exec"
377 gdb_test "attach $testpid" \
378 "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
379 "attach when process' a.out not in cwd"
381 set test "after attach3, exit"
385 "Kill the program being debugged.*y or n. $" \
388 # Another "don't leave a process around"
389 kill_wait_spawned_process $test_spawn_id
392 # Test attaching when the target is inside a system call.
394 proc_with_prefix do_call_attach_tests {} {
400 set test_spawn_id [spawn_wait_for_attach $binfile2]
401 set testpid [spawn_id_get_pid $test_spawn_id]
405 gdb_test "file $binfile2" ".*" "load file"
406 set test "attach call"
407 gdb_test_multiple "attach $testpid" "$test" {
408 -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
409 fail "$test (read register error)"
411 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
414 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
419 # See if other registers are problems
421 set test "info other register"
422 gdb_test_multiple "i r r3" "$test" {
423 -re "warning: reading register.*$gdb_prompt $" {
426 -re "r3.*$gdb_prompt $" {
431 # Get rid of the process
433 gdb_test "p should_exit = 1"
438 kill_wait_spawned_process $test_spawn_id
441 proc_with_prefix do_command_attach_tests {} {
445 if ![isnative] then {
446 unsupported "command attach test"
450 set test_spawn_id [spawn_wait_for_attach $binfile]
451 set testpid [spawn_id_get_pid $test_spawn_id]
455 set res [gdb_spawn_with_cmdline_opts \
456 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid"]
457 set test "starting with --pid"
458 gdb_test_multiple "" $test {
459 -re "Reading symbols from.*$gdb_prompt $" {
464 # Get rid of the process
465 kill_wait_spawned_process $test_spawn_id
468 # Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
469 # "run" would run before the attach finished - PR17347.
471 proc test_command_line_attach_run {} {
475 # Skip test if we cannot attach on the command line and use the run command.
476 # ??? Unclear what condition to use to return here when using gdbserver.
477 # ??? None of the below works.
478 # ![isnative] || [target_is_gdbserver]
479 # ![isnative] || [use_gdb_stub]
480 if { ![isnative] || [is_remote target] } then {
481 unsupported "commandline attach run test"
485 with_test_prefix "cmdline attach run" {
486 set test_spawn_id [spawn_wait_for_attach $binfile]
487 set testpid [spawn_id_get_pid $test_spawn_id]
489 set test "run to prompt"
492 set res [gdb_spawn_with_cmdline_opts \
493 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
496 kill_wait_spawned_process $test_spawn_id
499 gdb_test_multiple "" $test {
500 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
507 set test "run to main"
508 gdb_test_multiple "" $test {
509 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
514 # Get rid of the process
515 kill_wait_spawned_process $test_spawn_id
520 # This is a test of 'set exec-file-mismatch' handling.
522 proc_with_prefix do_attach_exec_mismatch_handling_tests {} {
528 clean_restart $binfile
530 # Start two programs that can be attached to.
531 # The first program contains a 'int bidule' variable, the second a 'float bidule'.
533 set test_spawn_id [spawn_wait_for_attach $binfile]
534 set testpid [spawn_id_get_pid $test_spawn_id]
535 set test_spawn_id2 [spawn_wait_for_attach $binfile2]
536 set testpid2 [spawn_id_get_pid $test_spawn_id2]
539 # Test with the default value of 'set exec-file-mismatch load".
540 set test "mismatch load"
541 gdb_test "attach $testpid" "Attaching to program.*" "$test attach1"
542 # Verify that we can "see" the variable "bidule" in the
543 # program, and that it is an integer.
544 gdb_test "ptype bidule" " = int" "$test after attach1, bidule is int"
545 # Detach the process.
546 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach1"
547 gdb_test_multiple "attach $testpid2" "$test attach2" {
548 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
552 gdb_test "y" "Reading symbols from .*attach2.*" "$test load attach2"
553 # Verify that we can "see" the variable "bidule" in the
554 # program, and that it is a float.
555 gdb_test "ptype bidule" " = float" "$test after attach2 and load, bidule is float"
556 # Detach the process.
557 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach2"
560 # Test with 'set exec-file-mismatch warn".
561 set test "mismatch warn"
562 gdb_test_no_output "set exec-file-mismatch warn"
563 gdb_test_multiple "attach $testpid" "$test attach" {
564 -re "Attaching to program.*exec-file-mismatch handling is currently \"warn\".*$gdb_prompt" {
568 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
569 # as we have not loaded the correct exec-file.
570 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
571 # Detach the process.
572 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
575 # Same test but with 'set exec-file-mismatch off".
576 set test "mismatch off"
577 gdb_test_no_output "set exec-file-mismatch off"
578 gdb_test_multiple "attach $testpid" "$test attach" {
579 -re "Attaching to program.*$gdb_prompt" {
583 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
584 # as we have not warned the user and not loaded the correct exec-file
585 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
586 # Detach the process.
587 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
589 # Test that the 'exec-file' changed is checked before exec-file-mismatch.
590 set test "mismatch exec-file changed has priority"
591 gdb_test_no_output "set exec-file-mismatch ask"
592 gdb_test_multiple "attach $testpid" "$test attach1 again, initial exec-file" {
593 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach\".*\(y or n\)" {
594 gdb_test "y" "Reading symbols from .*attach.*" $gdb_test_name
599 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach initial exec-file"
601 # Change the exec-file and attach to a new process using the changed file.
602 remote_exec build "mv ${binfile} ${binfile}.initial"
603 remote_exec build "mv ${binfile3} ${binfile}"
604 # Ensure GDB detects ${binfile} has changed when checking timestamp.
606 remote_exec build "touch ${binfile}"
607 set test_spawn_id3 [spawn_wait_for_attach $binfile]
608 set testpid3 [spawn_id_get_pid $test_spawn_id3]
610 gdb_test "attach $testpid3" "Attaching to program.*attach' has changed; re-reading symbols.*" \
611 "$test attach1 again, after changing exec-file"
612 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach after attach changed exec-file"
614 # Now, test the situation when current exec-file has changed
615 # and we attach to a pid using another file.
616 # Ensure GDB detects ${binfile} has changed when checking timestamp.
618 remote_exec build "touch ${binfile}"
620 gdb_test_multiple "attach $testpid2" "$test attach2" {
621 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
622 gdb_test "y" "Reading symbols from .*attach2.*" $gdb_test_name
626 # Restore initial build situation.
627 remote_exec build "mv ${binfile} ${binfile3}"
628 remote_exec build "mv ${binfile}.initial ${binfile}"
630 # Don't leave a process around
631 kill_wait_spawned_process $test_spawn_id
632 kill_wait_spawned_process $test_spawn_id2
633 kill_wait_spawned_process $test_spawn_id3
637 do_attach_failure_tests
639 do_attach_exec_mismatch_handling_tests
643 do_command_attach_tests
646 test_command_line_attach_run