1 # Copyright 1997-2024 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 require can_spawn_for_attach
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]
23 #execute_anywhere "rm -f ${binfile} ${binfile2}"
24 remote_exec build "rm -f ${binfile} ${binfile2} ${binfile3}"
25 # For debugging this test
29 # build the first test case
31 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
32 untested "failed to compile"
36 # Build the in-system-call test
38 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
39 untested "failed to compile in-system-call test"
43 # Build the third file, used to check attach when the exec-file has changed.
45 if { [gdb_compile "${srcdir}/${subdir}/${srcfile3}" "${binfile3}" executable {debug}] != "" } {
46 untested "failed to compile attach exec-file changed test"
50 # This is a test of the error cases for gdb's ability to attach to a
53 proc_with_prefix do_attach_failure_tests {} {
59 clean_restart $binfile
61 # Figure out a regular expression that will match the sysroot,
62 # noting that the default sysroot is "target:", and also noting
63 # that GDB will strip "target:" from the start of filenames when
64 # operating on the local filesystem. However the default sysroot
65 # can be set via configure option --with-sysroot, which can be "/".
66 # If $binfile is a absolute path, so pattern
67 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
69 set sysroot "\[^\r\n\]*"
71 # Start the program running and then wait for a bit, to be sure
72 # that it can be attached to.
74 set test_spawn_id [spawn_wait_for_attach $binfile]
75 set testpid [spawn_id_get_pid $test_spawn_id]
77 # Verify that we cannot attach to nonsense.
79 set test "attach to nonsense is prohibited"
80 gdb_test_multiple "attach abc" "$test" {
81 -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
84 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
85 # Response expected from /proc-based systems.
88 -re "Can't attach to process..*$gdb_prompt $" {
89 # Response expected on Cygwin
92 -re "Attaching to.*$gdb_prompt $" {
93 fail "$test (bogus pid allowed)"
97 # Verify that we cannot attach to nonsense even if its initial part is
100 set test "attach to digits-starting nonsense is prohibited"
101 gdb_test_multiple "attach ${testpid}x" "$test" {
102 -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" {
105 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
106 # Response expected from /proc-based systems.
109 -re "Can't attach to process..*$gdb_prompt $" {
110 # Response expected on Cygwin
113 -re "Attaching to.*$gdb_prompt $" {
114 fail "$test (bogus pid allowed)"
118 # Verify that we cannot attach to what appears to be a valid
119 # process ID, but is a process that doesn't exist. Traditionally,
120 # most systems didn't have a process with ID 0, so we take that as
121 # the default. However, there are a few exceptions.
124 if { [istarget "*-*-*bsd*"] } {
125 # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead
126 # (which should have the desired effect on any version of
127 # FreeBSD, and probably other *BSD's too).
130 set test "attach to nonexistent process is prohibited"
131 gdb_test_multiple "attach $boguspid" "$test" {
132 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
133 # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
136 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
137 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
140 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
143 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
146 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
147 # Response expected from /proc-based systems.
150 -re "Can't attach to process..*$gdb_prompt $" {
151 # Response expected on Cygwin
154 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
155 # Response expected on the extended-remote target.
160 # Verify that we can't double attach to the process.
162 set test "first attach"
163 gdb_test_multiple "attach $testpid" "$test" {
164 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
167 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
168 # Response expected on Cygwin.
173 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
174 gdb_test "inferior 2" "Switching to inferior 2.*" "switch to inferior 2"
176 set test "fail to attach again"
177 gdb_test_multiple "attach $testpid" "$test" {
178 -re "Attaching to process $testpid.*warning: process .* is already traced by process .*$gdb_prompt $" {
181 -re "Attaching to process .* failed.*$gdb_prompt $" {
182 # Response expected when using gdbserver.
187 # To ensure the target is still alive and working after this, try to run
189 gdb_test_no_output "set confirm off"
190 gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1"
192 gdb_test "kill" "killed.*" "exit after attach failures"
194 # This can probably be replaced with a call to runto or runto_main once
195 # the kfail is removed.
196 gdb_breakpoint "main"
198 gdb_test_multiple "" "stop at main" {
199 -wrap -re "Breakpoint $::decimal, main .*" {
204 # Another "don't leave a process around"
205 kill_wait_spawned_process $test_spawn_id
208 # This is a test of gdb's ability to attach to a running process.
210 proc_with_prefix do_attach_tests {} {
213 global escapedbinfile
218 clean_restart $binfile
220 # Figure out a regular expression that will match the sysroot,
221 # noting that the default sysroot is "target:", and also noting
222 # that GDB will strip "target:" from the start of filenames when
223 # operating on the local filesystem. However the default sysroot
224 # can be set via configure option --with-sysroot, which can be "/".
225 # If $binfile is a absolute path, so pattern
226 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
228 set sysroot "\[^\r\n\]*"
230 # Start the program running and then wait for a bit, to be sure
231 # that it can be attached to.
233 set test_spawn_id [spawn_wait_for_attach $binfile]
234 set testpid [spawn_id_get_pid $test_spawn_id]
236 # Verify that we can attach to the process by first giving its
237 # executable name via the file command, and using attach with the
240 # (Actually, the test system appears to do this automatically for
241 # us. So, we must also be prepared to be asked if we want to
242 # discard an existing set of symbols.)
244 set test "set file, before attach1"
245 gdb_test_multiple "file $binfile" "$test" {
246 -re "Load new symbol table from.*y or n. $" {
247 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
250 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
255 set test "attach1, after setting file"
256 gdb_test_multiple "attach $testpid" "$test" {
257 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
260 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
261 # Response expected on Cygwin
266 # Verify that we can "see" the variable "should_exit" in the
267 # program, and that it is zero.
269 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
271 # Detach the process.
274 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
277 # Wait a bit for gdb to finish detaching
281 # Purge the symbols from gdb's brain. (We want to be certain the
282 # next attach, which won't be preceded by a "file" command, is
283 # really getting the executable file without our help.)
285 set old_timeout $timeout
287 set test "attach1, purging symbols after detach"
288 gdb_test_multiple "file" "$test" {
289 -re "No executable file now.*Discard symbol table.*y or n. $" {
290 gdb_test "y" "No symbol file now." "$test"
293 set timeout $old_timeout
295 # Verify that we can attach to the process just by giving the
298 set test "attach2, with no file"
299 set found_exec_file 0
300 gdb_test_multiple "attach $testpid" "$test" {
301 -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
302 # On Cygwin, the DLL's symbol tables are loaded prior to the
303 # executable's symbol table. This in turn always results in
304 # asking the user for actually loading the symbol table of the
306 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
309 set found_exec_file 1
311 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
313 set found_exec_file 1
317 if {$found_exec_file == 0} {
318 set test "load file manually, after attach2"
319 gdb_test_multiple "file $binfile" "$test" {
320 -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
321 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
324 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
330 # Verify that we can modify the variable "should_exit" in the
333 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
335 # Verify that the modification really happened.
337 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
338 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
340 # Allow the test process to exit, to cleanup after ourselves.
342 gdb_continue_to_end "after attach2, exit"
344 # Make sure we don't leave a process around to confuse
345 # the next test run (and prevent the compile by keeping
346 # the text file busy), in case the "set should_exit" didn't
349 kill_wait_spawned_process $test_spawn_id
351 set test_spawn_id [spawn_wait_for_attach $binfile]
352 set testpid [spawn_id_get_pid $test_spawn_id]
354 # Verify that we can attach to the process, and find its a.out
355 # when we're cd'd to some directory that doesn't contain the
356 # a.out. (We use the source path set by the "dir" command.)
358 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
361 gdb_test "cd /tmp" "Working directory /tmp." \
362 "cd away from process working directory"
364 # Explicitly flush out any knowledge of the previous attachment.
366 set test "before attach3, flush symbols"
367 gdb_test_multiple "symbol-file" "$test" {
368 -re "Discard symbol table from.*y or n. $" {
369 gdb_test "y" "No symbol file now." \
372 -re "No symbol file now.*$gdb_prompt $" {
377 gdb_test "exec" "No executable file now." \
378 "before attach3, flush exec"
380 gdb_test "attach $testpid" \
381 "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
382 "attach when process' a.out not in cwd"
384 set test "after attach3, exit"
388 "Kill the program being debugged.*y or n. $" \
391 # Another "don't leave a process around"
392 kill_wait_spawned_process $test_spawn_id
395 # Test attaching when the target is inside a system call.
397 proc_with_prefix do_call_attach_tests {} {
403 set test_spawn_id [spawn_wait_for_attach $binfile2]
404 set testpid [spawn_id_get_pid $test_spawn_id]
408 gdb_test "file $binfile2" ".*" "load file"
409 set test "attach call"
410 gdb_test_multiple "attach $testpid" "$test" {
411 -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
412 fail "$test (read register error)"
414 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
417 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
422 # See if other registers are problems
424 set test "info other register"
425 gdb_test_multiple "i r r3" "$test" {
426 -re "warning: reading register.*$gdb_prompt $" {
429 -re "r3.*$gdb_prompt $" {
434 # Get rid of the process
436 gdb_test "p should_exit = 1"
441 kill_wait_spawned_process $test_spawn_id
444 proc_with_prefix do_command_attach_tests {} {
448 # The --pid option is used to attach to a process using the native
450 if { ![gdb_protocol_is_native] } {
451 unsupported "command attach test"
455 set test_spawn_id [spawn_wait_for_attach $binfile]
456 set testpid [spawn_id_get_pid $test_spawn_id]
460 # gdb_spawn_attach_cmdline records test results. No need to explicitly
461 # call pass/fail here.
462 gdb_spawn_attach_cmdline $testpid
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_with_prefix test_command_line_attach_run {} {
475 # The --pid option is used to attach to a process using the native
477 if { ![gdb_protocol_is_native] } {
478 unsupported "commandline attach run test"
482 set test_spawn_id [spawn_wait_for_attach $binfile]
483 set testpid [spawn_id_get_pid $test_spawn_id]
485 set test "run to prompt"
488 set res [gdb_spawn_with_cmdline_opts "--pid=$testpid -ex \"start\""]
491 kill_wait_spawned_process $test_spawn_id
494 gdb_test_multiple "" $test {
495 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
502 set test "run to main"
503 gdb_test_multiple "" $test {
504 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
509 # Get rid of the process
510 kill_wait_spawned_process $test_spawn_id
514 # This is a test of 'set exec-file-mismatch' handling.
516 proc_with_prefix do_attach_exec_mismatch_handling_tests {} {
522 clean_restart $binfile
524 # Start two programs that can be attached to.
525 # The first program contains a 'int bidule' variable, the second a 'float bidule'.
527 set test_spawn_id [spawn_wait_for_attach $binfile]
528 set testpid [spawn_id_get_pid $test_spawn_id]
529 set test_spawn_id2 [spawn_wait_for_attach $binfile2]
530 set testpid2 [spawn_id_get_pid $test_spawn_id2]
533 # Test with the default value of 'set exec-file-mismatch load".
534 set test "mismatch load"
535 gdb_test "attach $testpid" "Attaching to program.*" "$test attach1"
536 # Verify that we can "see" the variable "bidule" in the
537 # program, and that it is an integer.
538 gdb_test "ptype bidule" " = int" "$test after attach1, bidule is int"
539 # Detach the process.
540 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach1"
541 gdb_test_multiple "attach $testpid2" "$test attach2" {
542 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
546 gdb_test "y" "Reading symbols from .*attach2.*" "$test load attach2"
547 # Verify that we can "see" the variable "bidule" in the
548 # program, and that it is a float.
549 gdb_test "ptype bidule" " = float" "$test after attach2 and load, bidule is float"
550 # Detach the process.
551 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach2"
554 # Test with 'set exec-file-mismatch warn".
555 set test "mismatch warn"
556 gdb_test_no_output "set exec-file-mismatch warn"
557 gdb_test_multiple "attach $testpid" "$test attach" {
558 -re "Attaching to program.*exec-file-mismatch handling is currently \"warn\".*$gdb_prompt" {
562 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
563 # as we have not loaded the correct exec-file.
564 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
565 # Detach the process.
566 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
569 # Same test but with 'set exec-file-mismatch off".
570 set test "mismatch off"
571 gdb_test_no_output "set exec-file-mismatch off"
572 gdb_test_multiple "attach $testpid" "$test attach" {
573 -re "Attaching to program.*$gdb_prompt" {
577 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
578 # as we have not warned the user and not loaded the correct exec-file
579 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
580 # Detach the process.
581 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
583 # Test that the 'exec-file' changed is checked before exec-file-mismatch.
584 set test "mismatch exec-file changed has priority"
585 gdb_test_no_output "set exec-file-mismatch ask"
586 gdb_test_multiple "attach $testpid" "$test attach1 again, initial exec-file" {
587 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach\".*\(y or n\)" {
588 gdb_test "y" "Reading symbols from .*attach.*" $gdb_test_name
593 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach initial exec-file"
595 # Change the exec-file and attach to a new process using the changed file.
596 remote_exec build "mv ${binfile} ${binfile}.initial"
597 remote_exec build "mv ${binfile3} ${binfile}"
598 # Ensure GDB detects ${binfile} has changed when checking timestamp.
600 remote_exec build "touch ${binfile}"
601 set test_spawn_id3 [spawn_wait_for_attach $binfile]
602 set testpid3 [spawn_id_get_pid $test_spawn_id3]
604 gdb_test "attach $testpid3" "Attaching to program.*attach' has changed; re-reading symbols.*" \
605 "$test attach1 again, after changing exec-file"
606 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach after attach changed exec-file"
608 # Now, test the situation when current exec-file has changed
609 # and we attach to a pid using another file.
610 # Ensure GDB detects ${binfile} has changed when checking timestamp.
612 remote_exec build "touch ${binfile}"
614 gdb_test_multiple "attach $testpid2" "$test attach2" {
615 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
616 gdb_test "y" "Reading symbols from .*attach2.*" $gdb_test_name
620 # Restore initial build situation.
621 remote_exec build "mv ${binfile} ${binfile3}"
622 remote_exec build "mv ${binfile}.initial ${binfile}"
624 # Don't leave a process around
625 kill_wait_spawned_process $test_spawn_id
626 kill_wait_spawned_process $test_spawn_id2
627 kill_wait_spawned_process $test_spawn_id3
631 do_attach_failure_tests
633 do_attach_exec_mismatch_handling_tests
637 do_command_attach_tests
640 test_command_line_attach_run