1 # Copyright 1997-2022 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 # This is a test of the error cases for gdb's ability to attach to a
55 proc_with_prefix do_attach_failure_tests {} {
61 clean_restart $binfile
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
71 set sysroot "\[^\r\n\]*"
73 # Start the program running and then wait for a bit, to be sure
74 # that it can be attached to.
76 set test_spawn_id [spawn_wait_for_attach $binfile]
77 set testpid [spawn_id_get_pid $test_spawn_id]
79 # Verify that we cannot attach to nonsense.
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 $" {
86 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
87 # Response expected from /proc-based systems.
90 -re "Can't attach to process..*$gdb_prompt $" {
91 # Response expected on Cygwin
94 -re "Attaching to.*$gdb_prompt $" {
95 fail "$test (bogus pid allowed)"
99 # Verify that we cannot attach to nonsense even if its initial part is
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 $" {
107 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
108 # Response expected from /proc-based systems.
111 -re "Can't attach to process..*$gdb_prompt $" {
112 # Response expected on Cygwin
115 -re "Attaching to.*$gdb_prompt $" {
116 fail "$test (bogus pid allowed)"
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.
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).
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).
138 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
139 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
142 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
145 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
148 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
149 # Response expected from /proc-based systems.
152 -re "Can't attach to process..*$gdb_prompt $" {
153 # Response expected on Cygwin
156 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
157 # Response expected on the extended-remote target.
162 # Verify that we can't double attach to the process.
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 $" {
169 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
170 # Response expected on Cygwin.
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"
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]
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 # To ensure the target is still alive and working after this, try to run
195 gdb_test_no_output "set confirm off"
196 gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1"
198 if { $do_kfail } { setup_kfail "gdb/19558" "*-*-*" }
199 gdb_test "kill" "killed.*" "exit after attach failures"
201 # This can probably be replaced with a call to runto or runto_main once
202 # the kfail is removed.
203 gdb_breakpoint "main"
205 if { $do_kfail } { setup_kfail "gdb/19558" "*-*-*" }
206 gdb_test_multiple "" "stop at main" {
207 -wrap -re "Breakpoint $::decimal, main .*" {
212 # Another "don't leave a process around"
213 kill_wait_spawned_process $test_spawn_id
216 # This is a test of gdb's ability to attach to a running process.
218 proc_with_prefix do_attach_tests {} {
221 global escapedbinfile
226 clean_restart $binfile
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
236 set sysroot "\[^\r\n\]*"
238 # Start the program running and then wait for a bit, to be sure
239 # that it can be attached to.
241 set test_spawn_id [spawn_wait_for_attach $binfile]
242 set testpid [spawn_id_get_pid $test_spawn_id]
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
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.)
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\.\.\.*" \
258 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
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 $" {
268 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
269 # Response expected on Cygwin
274 # Verify that we can "see" the variable "should_exit" in the
275 # program, and that it is zero.
277 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
279 # Detach the process.
282 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
285 # Wait a bit for gdb to finish detaching
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.)
293 set old_timeout $timeout
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"
301 set timeout $old_timeout
303 # Verify that we can attach to the process just by giving the
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
314 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
317 set found_exec_file 1
319 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
321 set found_exec_file 1
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\.\.\.*" \
332 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
338 # Verify that we can modify the variable "should_exit" in the
341 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
343 # Verify that the modification really happened.
345 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
346 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
348 # Allow the test process to exit, to cleanup after ourselves.
350 gdb_continue_to_end "after attach2, exit"
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
357 kill_wait_spawned_process $test_spawn_id
359 set test_spawn_id [spawn_wait_for_attach $binfile]
360 set testpid [spawn_id_get_pid $test_spawn_id]
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.)
366 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
369 gdb_test "cd /tmp" "Working directory /tmp." \
370 "cd away from process working directory"
372 # Explicitly flush out any knowledge of the previous attachment.
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." \
380 -re "No symbol file now.*$gdb_prompt $" {
385 gdb_test "exec" "No executable file now." \
386 "before attach3, flush exec"
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"
392 set test "after attach3, exit"
396 "Kill the program being debugged.*y or n. $" \
399 # Another "don't leave a process around"
400 kill_wait_spawned_process $test_spawn_id
403 # Test attaching when the target is inside a system call.
405 proc_with_prefix do_call_attach_tests {} {
411 set test_spawn_id [spawn_wait_for_attach $binfile2]
412 set testpid [spawn_id_get_pid $test_spawn_id]
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)"
422 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
425 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
430 # See if other registers are problems
432 set test "info other register"
433 gdb_test_multiple "i r r3" "$test" {
434 -re "warning: reading register.*$gdb_prompt $" {
437 -re "r3.*$gdb_prompt $" {
442 # Get rid of the process
444 gdb_test "p should_exit = 1"
449 kill_wait_spawned_process $test_spawn_id
452 proc_with_prefix do_command_attach_tests {} {
457 unsupported "command attach test"
461 set test_spawn_id [spawn_wait_for_attach $binfile]
462 set testpid [spawn_id_get_pid $test_spawn_id]
466 # gdb_spawn_attach_cmdline records test results. No need to explicitly
467 # call pass/fail here.
468 gdb_spawn_attach_cmdline $testpid
470 # Get rid of the process
471 kill_wait_spawned_process $test_spawn_id
474 # Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
475 # "run" would run before the attach finished - PR17347.
477 proc_with_prefix test_command_line_attach_run {} {
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
486 if { ![runto_main] } {
490 if { ![gdb_is_target_native] } {
491 unsupported "commandline attach run test"
495 set test_spawn_id [spawn_wait_for_attach $binfile]
496 set testpid [spawn_id_get_pid $test_spawn_id]
498 set test "run to prompt"
501 set res [gdb_spawn_with_cmdline_opts \
502 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
505 kill_wait_spawned_process $test_spawn_id
508 gdb_test_multiple "" $test {
509 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
516 set test "run to main"
517 gdb_test_multiple "" $test {
518 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
523 # Get rid of the process
524 kill_wait_spawned_process $test_spawn_id
528 # This is a test of 'set exec-file-mismatch' handling.
530 proc_with_prefix do_attach_exec_mismatch_handling_tests {} {
536 clean_restart $binfile
538 # Start two programs that can be attached to.
539 # The first program contains a 'int bidule' variable, the second a 'float bidule'.
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]
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\)" {
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"
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" {
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"
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" {
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"
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
607 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach initial exec-file"
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.
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]
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"
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.
626 remote_exec build "touch ${binfile}"
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
634 # Restore initial build situation.
635 remote_exec build "mv ${binfile} ${binfile3}"
636 remote_exec build "mv ${binfile}.initial ${binfile}"
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
645 do_attach_failure_tests
647 do_attach_exec_mismatch_handling_tests
651 do_command_attach_tests
654 test_command_line_attach_run