[gdb/syscalls] Sync with strace v6.13
[binutils-gdb.git] / gdb / testsuite / gdb.base / attach.exp
blob2b655e69f8badf31b7f66ec49f881737ccd7bb60
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
27 #log_user 1
29 # build the first test case
31 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
32     untested "failed to compile"
33     return -1
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"
40     return -1
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"
47     return -1
50 # This is a test of the error cases for gdb's ability to attach to a
51 # running process.
53 proc_with_prefix do_attach_failure_tests {} {
54     global gdb_prompt
55     global binfile
56     global escapedbinfile
57     global srcfile
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
68     # $sysroot simple.
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 $" {
82             pass "$test"
83         }
84         -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
85             # Response expected from /proc-based systems.
86             pass "$test" 
87         }
88         -re "Can't attach to process..*$gdb_prompt $" {
89             # Response expected on Cygwin
90             pass "$test"
91         }
92         -re "Attaching to.*$gdb_prompt $" {
93             fail "$test (bogus pid allowed)"
94         }
95     }
97     # Verify that we cannot attach to nonsense even if its initial part is
98     # a valid PID.
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 $" {
103             pass "$test"
104         }
105         -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
106             # Response expected from /proc-based systems.
107             pass "$test" 
108         }
109         -re "Can't attach to process..*$gdb_prompt $" {
110             # Response expected on Cygwin
111             pass "$test"
112         }
113         -re "Attaching to.*$gdb_prompt $" {
114             fail "$test (bogus pid allowed)"
115         }
116     }
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.
123     set boguspid 0
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).
128         set boguspid -1
129     }
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).
134             pass "$test"
135         }
136         -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
137             # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
138             pass "$test"
139         }
140         -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
141             pass "$test"
142         }
143         -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
144             pass "$test"
145         }
146         -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
147             # Response expected from /proc-based systems.
148             pass "$test"
149         }
150         -re "Can't attach to process..*$gdb_prompt $" {
151             # Response expected on Cygwin
152             pass "$test"
153         }
154         -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
155             # Response expected on the extended-remote target.
156             pass "$test"
157         }
158     }
159     
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 $" {
165             pass "$test"
166         }
167         -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
168             # Response expected on Cygwin.
169             pass "$test"
170         }
171     }
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 $" {
179             pass "$test"
180         }
181         -re "Attaching to process .* failed.*$gdb_prompt $" {
182             # Response expected when using gdbserver.
183             pass "$test"
184         }
185     }
187     # To ensure the target is still alive and working after this, try to run
188     # inferior 1.
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"
197     gdb_run_cmd
198     gdb_test_multiple "" "stop at main" {
199         -wrap -re "Breakpoint $::decimal, main .*" {
200             pass $gdb_test_name
201         }
202     }
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 {} {
211     global gdb_prompt
212     global binfile
213     global escapedbinfile
214     global srcfile
215     global timeout
216     global decimal
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
227     # $sysroot simple.
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
238     # process ID.
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.)
243     
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\.\.\.*" \
248                 "$test (re-read)"
249         }
250         -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
251             pass "$test"
252         }
253     }
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 $" {
258             pass "$test"
259         }
260         -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
261             # Response expected on Cygwin
262             pass "$test"
263         }
264     }
266     # Verify that we can "see" the variable "should_exit" in the
267     # program, and that it is zero.
268    
269     gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
271     # Detach the process.
272    
273     gdb_test "detach" \
274         "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
275         "attach1 detach"
277     # Wait a bit for gdb to finish detaching
278     
279     exec sleep 5
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.)
284     
285     set old_timeout $timeout
286     set timeout 15 
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"
291         }
292     }
293     set timeout $old_timeout
295     # Verify that we can attach to the process just by giving the
296     # process ID.
297    
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
305             # executable.
306             gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
307                 "$test (reset file)"
309             set found_exec_file 1
310         }
311         -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
312             pass "$test"
313             set found_exec_file 1
314         }
315     }
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\.\.\.*" \
322                     "$test (re-read)"
323             }
324             -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
325                 pass "$test"
326             }
327         }
328     }
330     # Verify that we can modify the variable "should_exit" in the
331     # program.
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
347     # work.
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.)
357     
358     gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
359         "set source path"
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." \
370                 "$test"
371         }
372         -re "No symbol file now.*$gdb_prompt $" {
373             pass "$test"
374         }
375     }
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"
385     gdb_test "kill" \
386         "" \
387         "$test" \
388         "Kill the program being debugged.*y or n. $" \
389         "y"
390     
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 {} {
398     global gdb_prompt
399     global binfile2
401     clean_restart
403     set test_spawn_id [spawn_wait_for_attach $binfile2]
404     set testpid [spawn_id_get_pid $test_spawn_id]
406     # Attach
407    
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)"
413         }
414         -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
415             pass "$test"
416         }
417         -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
418             pass "$test"
419         }
420     }
422     # See if other registers are problems
423     
424     set test "info other register"
425     gdb_test_multiple "i r r3" "$test" {
426         -re "warning: reading register.*$gdb_prompt $" {
427             fail "$test"
428         }
429         -re "r3.*$gdb_prompt $" {
430             pass "$test"
431         }
432     }
434     # Get rid of the process
435     
436     gdb_test "p should_exit = 1"
437     gdb_continue_to_end
438    
439     # Be paranoid
440    
441     kill_wait_spawned_process $test_spawn_id
444 proc_with_prefix do_command_attach_tests {} {
445     global gdb_prompt
446     global binfile
448     # The --pid option is used to attach to a process using the native
449     # target.
450     if { ![gdb_protocol_is_native] } {
451         unsupported "command attach test"
452         return 0
453     }
455     set test_spawn_id [spawn_wait_for_attach $binfile]
456     set testpid [spawn_id_get_pid $test_spawn_id]
458     gdb_exit
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 {} {
472     global gdb_prompt
473     global binfile
475     # The --pid option is used to attach to a process using the native
476     # target.
477     if { ![gdb_protocol_is_native] } {
478         unsupported "commandline attach run test"
479         return
480     }
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"
486     gdb_exit
488     set res [gdb_spawn_with_cmdline_opts "--pid=$testpid -ex \"start\""]
489     if { $res != 0} {
490         fail $test
491         kill_wait_spawned_process $test_spawn_id
492         return $res
493     }
494     gdb_test_multiple "" $test {
495         -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
496             pass $test
497         }
498     }
500     send_gdb "y\n"
502     set test "run to main"
503     gdb_test_multiple "" $test {
504         -re "Temporary breakpoint .* main .*$gdb_prompt $" {
505             pass $test
506         }
507     }
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 {} {
517     global gdb_prompt
518     global binfile
519     global binfile2
520     global binfile3
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\)" {
543             pass "$test attach2"
544         }
545     }
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" {
559             pass "$test attach"
560         }
561     }
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" {
574             pass "$test attach"
575         }
576     }
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
589         }
590     }
591     
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.
599     sleep 1
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.
611     sleep 1
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
617         }
618     }
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
630 do_attach_tests
631 do_attach_failure_tests
632 do_call_attach_tests
633 do_attach_exec_mismatch_handling_tests
635 # Test "gdb --pid"
637 do_command_attach_tests
640 test_command_line_attach_run