]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/attach.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / attach.exp
1 # Copyright 1997-2024 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 $binfile
60
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\]*"
70
71 # Start the program running and then wait for a bit, to be sure
72 # that it can be attached to.
73
74 set test_spawn_id [spawn_wait_for_attach $binfile]
75 set testpid [spawn_id_get_pid $test_spawn_id]
76
77 # Verify that we cannot attach to nonsense.
78
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 }
96
97 # Verify that we cannot attach to nonsense even if its initial part is
98 # a valid PID.
99
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 }
117
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.
122
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.
161
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 }
172
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"
175
176 # Probe this before the failing attach: the failed attach against GDBserver
177 # currently leaves the extended-remote target in a bad state.
178 set do_kfail [target_is_gdbserver]
179
180 set test "fail to attach again"
181 gdb_test_multiple "attach $testpid" "$test" {
182 -re "Attaching to process $testpid.*warning: process .* is already traced by process .*$gdb_prompt $" {
183 pass "$test"
184 }
185 -re "Attaching to process .* failed.*$gdb_prompt $" {
186 # Response expected when using gdbserver.
187 pass "$test"
188 }
189 }
190
191 # To ensure the target is still alive and working after this, try to run
192 # inferior 1.
193 gdb_test_no_output "set confirm off"
194 gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1"
195
196 if { $do_kfail } { setup_kfail "gdb/19558" "*-*-*" }
197 gdb_test "kill" "killed.*" "exit after attach failures"
198
199 # This can probably be replaced with a call to runto or runto_main once
200 # the kfail is removed.
201 gdb_breakpoint "main"
202 gdb_run_cmd
203 if { $do_kfail } { setup_kfail "gdb/19558" "*-*-*" }
204 gdb_test_multiple "" "stop at main" {
205 -wrap -re "Breakpoint $::decimal, main .*" {
206 pass $gdb_test_name
207 }
208 }
209
210 # Another "don't leave a process around"
211 kill_wait_spawned_process $test_spawn_id
212 }
213
214 # This is a test of gdb's ability to attach to a running process.
215
216 proc_with_prefix do_attach_tests {} {
217 global gdb_prompt
218 global binfile
219 global escapedbinfile
220 global srcfile
221 global timeout
222 global decimal
223
224 clean_restart $binfile
225
226 # Figure out a regular expression that will match the sysroot,
227 # noting that the default sysroot is "target:", and also noting
228 # that GDB will strip "target:" from the start of filenames when
229 # operating on the local filesystem. However the default sysroot
230 # can be set via configure option --with-sysroot, which can be "/".
231 # If $binfile is a absolute path, so pattern
232 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
233 # $sysroot simple.
234 set sysroot "\[^\r\n\]*"
235
236 # Start the program running and then wait for a bit, to be sure
237 # that it can be attached to.
238
239 set test_spawn_id [spawn_wait_for_attach $binfile]
240 set testpid [spawn_id_get_pid $test_spawn_id]
241
242 # Verify that we can attach to the process by first giving its
243 # executable name via the file command, and using attach with the
244 # process ID.
245
246 # (Actually, the test system appears to do this automatically for
247 # us. So, we must also be prepared to be asked if we want to
248 # discard an existing set of symbols.)
249
250 set test "set file, before attach1"
251 gdb_test_multiple "file $binfile" "$test" {
252 -re "Load new symbol table from.*y or n. $" {
253 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
254 "$test (re-read)"
255 }
256 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
257 pass "$test"
258 }
259 }
260
261 set test "attach1, after setting file"
262 gdb_test_multiple "attach $testpid" "$test" {
263 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
264 pass "$test"
265 }
266 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
267 # Response expected on Cygwin
268 pass "$test"
269 }
270 }
271
272 # Verify that we can "see" the variable "should_exit" in the
273 # program, and that it is zero.
274
275 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
276
277 # Detach the process.
278
279 gdb_test "detach" \
280 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
281 "attach1 detach"
282
283 # Wait a bit for gdb to finish detaching
284
285 exec sleep 5
286
287 # Purge the symbols from gdb's brain. (We want to be certain the
288 # next attach, which won't be preceded by a "file" command, is
289 # really getting the executable file without our help.)
290
291 set old_timeout $timeout
292 set timeout 15
293 set test "attach1, purging symbols after detach"
294 gdb_test_multiple "file" "$test" {
295 -re "No executable file now.*Discard symbol table.*y or n. $" {
296 gdb_test "y" "No symbol file now." "$test"
297 }
298 }
299 set timeout $old_timeout
300
301 # Verify that we can attach to the process just by giving the
302 # process ID.
303
304 set test "attach2, with no file"
305 set found_exec_file 0
306 gdb_test_multiple "attach $testpid" "$test" {
307 -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
308 # On Cygwin, the DLL's symbol tables are loaded prior to the
309 # executable's symbol table. This in turn always results in
310 # asking the user for actually loading the symbol table of the
311 # executable.
312 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
313 "$test (reset file)"
314
315 set found_exec_file 1
316 }
317 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
318 pass "$test"
319 set found_exec_file 1
320 }
321 }
322
323 if {$found_exec_file == 0} {
324 set test "load file manually, after attach2"
325 gdb_test_multiple "file $binfile" "$test" {
326 -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
327 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
328 "$test (re-read)"
329 }
330 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
331 pass "$test"
332 }
333 }
334 }
335
336 # Verify that we can modify the variable "should_exit" in the
337 # program.
338
339 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
340
341 # Verify that the modification really happened.
342
343 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
344 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
345
346 # Allow the test process to exit, to cleanup after ourselves.
347
348 gdb_continue_to_end "after attach2, exit"
349
350 # Make sure we don't leave a process around to confuse
351 # the next test run (and prevent the compile by keeping
352 # the text file busy), in case the "set should_exit" didn't
353 # work.
354
355 kill_wait_spawned_process $test_spawn_id
356
357 set test_spawn_id [spawn_wait_for_attach $binfile]
358 set testpid [spawn_id_get_pid $test_spawn_id]
359
360 # Verify that we can attach to the process, and find its a.out
361 # when we're cd'd to some directory that doesn't contain the
362 # a.out. (We use the source path set by the "dir" command.)
363
364 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
365 "set source path"
366
367 gdb_test "cd /tmp" "Working directory /tmp." \
368 "cd away from process working directory"
369
370 # Explicitly flush out any knowledge of the previous attachment.
371
372 set test "before attach3, flush symbols"
373 gdb_test_multiple "symbol-file" "$test" {
374 -re "Discard symbol table from.*y or n. $" {
375 gdb_test "y" "No symbol file now." \
376 "$test"
377 }
378 -re "No symbol file now.*$gdb_prompt $" {
379 pass "$test"
380 }
381 }
382
383 gdb_test "exec" "No executable file now." \
384 "before attach3, flush exec"
385
386 gdb_test "attach $testpid" \
387 "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
388 "attach when process' a.out not in cwd"
389
390 set test "after attach3, exit"
391 gdb_test "kill" \
392 "" \
393 "$test" \
394 "Kill the program being debugged.*y or n. $" \
395 "y"
396
397 # Another "don't leave a process around"
398 kill_wait_spawned_process $test_spawn_id
399 }
400
401 # Test attaching when the target is inside a system call.
402
403 proc_with_prefix do_call_attach_tests {} {
404 global gdb_prompt
405 global binfile2
406
407 clean_restart
408
409 set test_spawn_id [spawn_wait_for_attach $binfile2]
410 set testpid [spawn_id_get_pid $test_spawn_id]
411
412 # Attach
413
414 gdb_test "file $binfile2" ".*" "load file"
415 set test "attach call"
416 gdb_test_multiple "attach $testpid" "$test" {
417 -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
418 fail "$test (read register error)"
419 }
420 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
421 pass "$test"
422 }
423 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
424 pass "$test"
425 }
426 }
427
428 # See if other registers are problems
429
430 set test "info other register"
431 gdb_test_multiple "i r r3" "$test" {
432 -re "warning: reading register.*$gdb_prompt $" {
433 fail "$test"
434 }
435 -re "r3.*$gdb_prompt $" {
436 pass "$test"
437 }
438 }
439
440 # Get rid of the process
441
442 gdb_test "p should_exit = 1"
443 gdb_continue_to_end
444
445 # Be paranoid
446
447 kill_wait_spawned_process $test_spawn_id
448 }
449
450 proc_with_prefix do_command_attach_tests {} {
451 global gdb_prompt
452 global binfile
453
454 if {![isnative]} {
455 unsupported "command attach test"
456 return 0
457 }
458
459 set test_spawn_id [spawn_wait_for_attach $binfile]
460 set testpid [spawn_id_get_pid $test_spawn_id]
461
462 gdb_exit
463
464 # gdb_spawn_attach_cmdline records test results. No need to explicitly
465 # call pass/fail here.
466 gdb_spawn_attach_cmdline $testpid
467
468 # Get rid of the process
469 kill_wait_spawned_process $test_spawn_id
470 }
471
472 # Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
473 # "run" would run before the attach finished - PR17347.
474
475 proc_with_prefix test_command_line_attach_run {} {
476 global gdb_prompt
477 global binfile
478
479 # The --pid option is used to attach to a process using the native target.
480 # Start GDB and run to main just to see what the execution target is, skip
481 # if it's not the native target.
482 clean_restart $binfile
483
484 if { ![runto_main] } {
485 return
486 }
487
488 if { ![gdb_is_target_native] } {
489 unsupported "commandline attach run test"
490 return
491 }
492
493 set test_spawn_id [spawn_wait_for_attach $binfile]
494 set testpid [spawn_id_get_pid $test_spawn_id]
495
496 set test "run to prompt"
497 gdb_exit
498
499 set res [gdb_spawn_with_cmdline_opts \
500 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
501 if { $res != 0} {
502 fail $test
503 kill_wait_spawned_process $test_spawn_id
504 return $res
505 }
506 gdb_test_multiple "" $test {
507 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
508 pass $test
509 }
510 }
511
512 send_gdb "y\n"
513
514 set test "run to main"
515 gdb_test_multiple "" $test {
516 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
517 pass $test
518 }
519 }
520
521 # Get rid of the process
522 kill_wait_spawned_process $test_spawn_id
523 }
524
525
526 # This is a test of 'set exec-file-mismatch' handling.
527
528 proc_with_prefix do_attach_exec_mismatch_handling_tests {} {
529 global gdb_prompt
530 global binfile
531 global binfile2
532 global binfile3
533
534 clean_restart $binfile
535
536 # Start two programs that can be attached to.
537 # The first program contains a 'int bidule' variable, the second a 'float bidule'.
538
539 set test_spawn_id [spawn_wait_for_attach $binfile]
540 set testpid [spawn_id_get_pid $test_spawn_id]
541 set test_spawn_id2 [spawn_wait_for_attach $binfile2]
542 set testpid2 [spawn_id_get_pid $test_spawn_id2]
543
544
545 # Test with the default value of 'set exec-file-mismatch load".
546 set test "mismatch load"
547 gdb_test "attach $testpid" "Attaching to program.*" "$test attach1"
548 # Verify that we can "see" the variable "bidule" in the
549 # program, and that it is an integer.
550 gdb_test "ptype bidule" " = int" "$test after attach1, bidule is int"
551 # Detach the process.
552 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach1"
553 gdb_test_multiple "attach $testpid2" "$test attach2" {
554 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
555 pass "$test attach2"
556 }
557 }
558 gdb_test "y" "Reading symbols from .*attach2.*" "$test load attach2"
559 # Verify that we can "see" the variable "bidule" in the
560 # program, and that it is a float.
561 gdb_test "ptype bidule" " = float" "$test after attach2 and load, bidule is float"
562 # Detach the process.
563 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach2"
564
565
566 # Test with 'set exec-file-mismatch warn".
567 set test "mismatch warn"
568 gdb_test_no_output "set exec-file-mismatch warn"
569 gdb_test_multiple "attach $testpid" "$test attach" {
570 -re "Attaching to program.*exec-file-mismatch handling is currently \"warn\".*$gdb_prompt" {
571 pass "$test attach"
572 }
573 }
574 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
575 # as we have not loaded the correct exec-file.
576 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
577 # Detach the process.
578 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
579
580
581 # Same test but with 'set exec-file-mismatch off".
582 set test "mismatch off"
583 gdb_test_no_output "set exec-file-mismatch off"
584 gdb_test_multiple "attach $testpid" "$test attach" {
585 -re "Attaching to program.*$gdb_prompt" {
586 pass "$test attach"
587 }
588 }
589 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
590 # as we have not warned the user and not loaded the correct exec-file
591 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
592 # Detach the process.
593 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
594
595 # Test that the 'exec-file' changed is checked before exec-file-mismatch.
596 set test "mismatch exec-file changed has priority"
597 gdb_test_no_output "set exec-file-mismatch ask"
598 gdb_test_multiple "attach $testpid" "$test attach1 again, initial exec-file" {
599 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach\".*\(y or n\)" {
600 gdb_test "y" "Reading symbols from .*attach.*" $gdb_test_name
601 }
602 }
603
604
605 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach initial exec-file"
606
607 # Change the exec-file and attach to a new process using the changed file.
608 remote_exec build "mv ${binfile} ${binfile}.initial"
609 remote_exec build "mv ${binfile3} ${binfile}"
610 # Ensure GDB detects ${binfile} has changed when checking timestamp.
611 sleep 1
612 remote_exec build "touch ${binfile}"
613 set test_spawn_id3 [spawn_wait_for_attach $binfile]
614 set testpid3 [spawn_id_get_pid $test_spawn_id3]
615
616 gdb_test "attach $testpid3" "Attaching to program.*attach' has changed; re-reading symbols.*" \
617 "$test attach1 again, after changing exec-file"
618 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach after attach changed exec-file"
619
620 # Now, test the situation when current exec-file has changed
621 # and we attach to a pid using another file.
622 # Ensure GDB detects ${binfile} has changed when checking timestamp.
623 sleep 1
624 remote_exec build "touch ${binfile}"
625
626 gdb_test_multiple "attach $testpid2" "$test attach2" {
627 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
628 gdb_test "y" "Reading symbols from .*attach2.*" $gdb_test_name
629 }
630 }
631
632 # Restore initial build situation.
633 remote_exec build "mv ${binfile} ${binfile3}"
634 remote_exec build "mv ${binfile}.initial ${binfile}"
635
636 # Don't leave a process around
637 kill_wait_spawned_process $test_spawn_id
638 kill_wait_spawned_process $test_spawn_id2
639 kill_wait_spawned_process $test_spawn_id3
640 }
641
642 do_attach_tests
643 do_attach_failure_tests
644 do_call_attach_tests
645 do_attach_exec_mismatch_handling_tests
646
647 # Test "gdb --pid"
648
649 do_command_attach_tests
650
651
652 test_command_line_attach_run