Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / attach.exp
1 # Copyright 1997-2022 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 if {![can_spawn_for_attach]} {
17 return 0
18 }
19
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]
24
25 #execute_anywhere "rm -f ${binfile} ${binfile2}"
26 remote_exec build "rm -f ${binfile} ${binfile2} ${binfile3}"
27 # For debugging this test
28 #
29 #log_user 1
30
31 # build the first test case
32 #
33 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
34 untested "failed to compile"
35 return -1
36 }
37
38 # Build the in-system-call test
39
40 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
41 untested "failed to compile in-system-call test"
42 return -1
43 }
44
45 # Build the third file, used to check attach when the exec-file has changed.
46
47 if { [gdb_compile "${srcdir}/${subdir}/${srcfile3}" "${binfile3}" executable {debug}] != "" } {
48 untested "failed to compile attach exec-file changed test"
49 return -1
50 }
51
52 if [get_compiler_info] {
53 return -1
54 }
55
56 # This is a test of the error cases for gdb's ability to attach to a
57 # running process.
58
59 proc_with_prefix do_attach_failure_tests {} {
60 global gdb_prompt
61 global binfile
62 global escapedbinfile
63 global srcfile
64
65 clean_restart $binfile
66
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
74 # $sysroot simple.
75 set sysroot "\[^\r\n\]*"
76
77 # Start the program running and then wait for a bit, to be sure
78 # that it can be attached to.
79
80 set test_spawn_id [spawn_wait_for_attach $binfile]
81 set testpid [spawn_id_get_pid $test_spawn_id]
82
83 # Verify that we cannot attach to nonsense.
84
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 $" {
88 pass "$test"
89 }
90 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
91 # Response expected from /proc-based systems.
92 pass "$test"
93 }
94 -re "Can't attach to process..*$gdb_prompt $" {
95 # Response expected on Cygwin
96 pass "$test"
97 }
98 -re "Attaching to.*$gdb_prompt $" {
99 fail "$test (bogus pid allowed)"
100 }
101 }
102
103 # Verify that we cannot attach to nonsense even if its initial part is
104 # a valid PID.
105
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 $" {
109 pass "$test"
110 }
111 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
112 # Response expected from /proc-based systems.
113 pass "$test"
114 }
115 -re "Can't attach to process..*$gdb_prompt $" {
116 # Response expected on Cygwin
117 pass "$test"
118 }
119 -re "Attaching to.*$gdb_prompt $" {
120 fail "$test (bogus pid allowed)"
121 }
122 }
123
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.
128
129 set boguspid 0
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).
134 set boguspid -1
135 }
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).
140 pass "$test"
141 }
142 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
143 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
144 pass "$test"
145 }
146 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
147 pass "$test"
148 }
149 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
150 pass "$test"
151 }
152 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
153 # Response expected from /proc-based systems.
154 pass "$test"
155 }
156 -re "Can't attach to process..*$gdb_prompt $" {
157 # Response expected on Cygwin
158 pass "$test"
159 }
160 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
161 # Response expected on the extended-remote target.
162 pass "$test"
163 }
164 }
165
166 # Verify that we can't double attach to the process.
167
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 $" {
171 pass "$test"
172 }
173 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
174 # Response expected on Cygwin.
175 pass "$test"
176 }
177 }
178
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"
181
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 $" {
185 pass "$test"
186 }
187 -re "Attaching to process .* failed.*$gdb_prompt $" {
188 # Response expected when using gdbserver.
189 pass "$test"
190 }
191 }
192
193 gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1"
194 set test "exit after attach failures"
195 gdb_test "kill" \
196 "" \
197 "$test" \
198 "Kill the program being debugged.*y or n. $" \
199 "y"
200
201 # Another "don't leave a process around"
202 kill_wait_spawned_process $test_spawn_id
203 }
204
205 # This is a test of gdb's ability to attach to a running process.
206
207 proc_with_prefix do_attach_tests {} {
208 global gdb_prompt
209 global binfile
210 global escapedbinfile
211 global srcfile
212 global timeout
213 global decimal
214
215 clean_restart $binfile
216
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
224 # $sysroot simple.
225 set sysroot "\[^\r\n\]*"
226
227 # Start the program running and then wait for a bit, to be sure
228 # that it can be attached to.
229
230 set test_spawn_id [spawn_wait_for_attach $binfile]
231 set testpid [spawn_id_get_pid $test_spawn_id]
232
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
235 # process ID.
236
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.)
240
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\.\.\.*" \
245 "$test (re-read)"
246 }
247 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
248 pass "$test"
249 }
250 }
251
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 $" {
255 pass "$test"
256 }
257 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
258 # Response expected on Cygwin
259 pass "$test"
260 }
261 }
262
263 # Verify that we can "see" the variable "should_exit" in the
264 # program, and that it is zero.
265
266 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
267
268 # Detach the process.
269
270 gdb_test "detach" \
271 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
272 "attach1 detach"
273
274 # Wait a bit for gdb to finish detaching
275
276 exec sleep 5
277
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.)
281
282 set old_timeout $timeout
283 set timeout 15
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"
288 }
289 }
290 set timeout $old_timeout
291
292 # Verify that we can attach to the process just by giving the
293 # process ID.
294
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
302 # executable.
303 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
304 "$test (reset file)"
305
306 set found_exec_file 1
307 }
308 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
309 pass "$test"
310 set found_exec_file 1
311 }
312 }
313
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\.\.\.*" \
319 "$test (re-read)"
320 }
321 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
322 pass "$test"
323 }
324 }
325 }
326
327 # Verify that we can modify the variable "should_exit" in the
328 # program.
329
330 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
331
332 # Verify that the modification really happened.
333
334 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
335 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
336
337 # Allow the test process to exit, to cleanup after ourselves.
338
339 gdb_continue_to_end "after attach2, exit"
340
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
344 # work.
345
346 kill_wait_spawned_process $test_spawn_id
347
348 set test_spawn_id [spawn_wait_for_attach $binfile]
349 set testpid [spawn_id_get_pid $test_spawn_id]
350
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.)
354
355 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
356 "set source path"
357
358 gdb_test "cd /tmp" "Working directory /tmp." \
359 "cd away from process working directory"
360
361 # Explicitly flush out any knowledge of the previous attachment.
362
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." \
367 "$test"
368 }
369 -re "No symbol file now.*$gdb_prompt $" {
370 pass "$test"
371 }
372 }
373
374 gdb_test "exec" "No executable file now." \
375 "before attach3, flush exec"
376
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"
380
381 set test "after attach3, exit"
382 gdb_test "kill" \
383 "" \
384 "$test" \
385 "Kill the program being debugged.*y or n. $" \
386 "y"
387
388 # Another "don't leave a process around"
389 kill_wait_spawned_process $test_spawn_id
390 }
391
392 # Test attaching when the target is inside a system call.
393
394 proc_with_prefix do_call_attach_tests {} {
395 global gdb_prompt
396 global binfile2
397
398 clean_restart
399
400 set test_spawn_id [spawn_wait_for_attach $binfile2]
401 set testpid [spawn_id_get_pid $test_spawn_id]
402
403 # Attach
404
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)"
410 }
411 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
412 pass "$test"
413 }
414 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
415 pass "$test"
416 }
417 }
418
419 # See if other registers are problems
420
421 set test "info other register"
422 gdb_test_multiple "i r r3" "$test" {
423 -re "warning: reading register.*$gdb_prompt $" {
424 fail "$test"
425 }
426 -re "r3.*$gdb_prompt $" {
427 pass "$test"
428 }
429 }
430
431 # Get rid of the process
432
433 gdb_test "p should_exit = 1"
434 gdb_continue_to_end
435
436 # Be paranoid
437
438 kill_wait_spawned_process $test_spawn_id
439 }
440
441 proc_with_prefix do_command_attach_tests {} {
442 global gdb_prompt
443 global binfile
444
445 if ![isnative] then {
446 unsupported "command attach test"
447 return 0
448 }
449
450 set test_spawn_id [spawn_wait_for_attach $binfile]
451 set testpid [spawn_id_get_pid $test_spawn_id]
452
453 gdb_exit
454
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 $" {
460 pass "$test"
461 }
462 }
463
464 # Get rid of the process
465 kill_wait_spawned_process $test_spawn_id
466 }
467
468 # Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
469 # "run" would run before the attach finished - PR17347.
470
471 proc_with_prefix test_command_line_attach_run {} {
472 global gdb_prompt
473 global binfile
474
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"
482 return 0
483 }
484
485 set test_spawn_id [spawn_wait_for_attach $binfile]
486 set testpid [spawn_id_get_pid $test_spawn_id]
487
488 set test "run to prompt"
489 gdb_exit
490
491 set res [gdb_spawn_with_cmdline_opts \
492 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
493 if { $res != 0} {
494 fail $test
495 kill_wait_spawned_process $test_spawn_id
496 return $res
497 }
498 gdb_test_multiple "" $test {
499 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
500 pass $test
501 }
502 }
503
504 send_gdb "y\n"
505
506 set test "run to main"
507 gdb_test_multiple "" $test {
508 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
509 pass $test
510 }
511 }
512
513 # Get rid of the process
514 kill_wait_spawned_process $test_spawn_id
515 }
516
517
518 # This is a test of 'set exec-file-mismatch' handling.
519
520 proc_with_prefix do_attach_exec_mismatch_handling_tests {} {
521 global gdb_prompt
522 global binfile
523 global binfile2
524 global binfile3
525
526 clean_restart $binfile
527
528 # Start two programs that can be attached to.
529 # The first program contains a 'int bidule' variable, the second a 'float bidule'.
530
531 set test_spawn_id [spawn_wait_for_attach $binfile]
532 set testpid [spawn_id_get_pid $test_spawn_id]
533 set test_spawn_id2 [spawn_wait_for_attach $binfile2]
534 set testpid2 [spawn_id_get_pid $test_spawn_id2]
535
536
537 # Test with the default value of 'set exec-file-mismatch load".
538 set test "mismatch load"
539 gdb_test "attach $testpid" "Attaching to program.*" "$test attach1"
540 # Verify that we can "see" the variable "bidule" in the
541 # program, and that it is an integer.
542 gdb_test "ptype bidule" " = int" "$test after attach1, bidule is int"
543 # Detach the process.
544 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach1"
545 gdb_test_multiple "attach $testpid2" "$test attach2" {
546 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
547 pass "$test attach2"
548 }
549 }
550 gdb_test "y" "Reading symbols from .*attach2.*" "$test load attach2"
551 # Verify that we can "see" the variable "bidule" in the
552 # program, and that it is a float.
553 gdb_test "ptype bidule" " = float" "$test after attach2 and load, bidule is float"
554 # Detach the process.
555 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach2"
556
557
558 # Test with 'set exec-file-mismatch warn".
559 set test "mismatch warn"
560 gdb_test_no_output "set exec-file-mismatch warn"
561 gdb_test_multiple "attach $testpid" "$test attach" {
562 -re "Attaching to program.*exec-file-mismatch handling is currently \"warn\".*$gdb_prompt" {
563 pass "$test attach"
564 }
565 }
566 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
567 # as we have not loaded the correct exec-file.
568 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
569 # Detach the process.
570 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
571
572
573 # Same test but with 'set exec-file-mismatch off".
574 set test "mismatch off"
575 gdb_test_no_output "set exec-file-mismatch off"
576 gdb_test_multiple "attach $testpid" "$test attach" {
577 -re "Attaching to program.*$gdb_prompt" {
578 pass "$test attach"
579 }
580 }
581 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
582 # as we have not warned the user and not loaded the correct exec-file
583 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
584 # Detach the process.
585 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
586
587 # Test that the 'exec-file' changed is checked before exec-file-mismatch.
588 set test "mismatch exec-file changed has priority"
589 gdb_test_no_output "set exec-file-mismatch ask"
590 gdb_test_multiple "attach $testpid" "$test attach1 again, initial exec-file" {
591 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach\".*\(y or n\)" {
592 gdb_test "y" "Reading symbols from .*attach.*" $gdb_test_name
593 }
594 }
595
596
597 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach initial exec-file"
598
599 # Change the exec-file and attach to a new process using the changed file.
600 remote_exec build "mv ${binfile} ${binfile}.initial"
601 remote_exec build "mv ${binfile3} ${binfile}"
602 # Ensure GDB detects ${binfile} has changed when checking timestamp.
603 sleep 1
604 remote_exec build "touch ${binfile}"
605 set test_spawn_id3 [spawn_wait_for_attach $binfile]
606 set testpid3 [spawn_id_get_pid $test_spawn_id3]
607
608 gdb_test "attach $testpid3" "Attaching to program.*attach' has changed; re-reading symbols.*" \
609 "$test attach1 again, after changing exec-file"
610 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach after attach changed exec-file"
611
612 # Now, test the situation when current exec-file has changed
613 # and we attach to a pid using another file.
614 # Ensure GDB detects ${binfile} has changed when checking timestamp.
615 sleep 1
616 remote_exec build "touch ${binfile}"
617
618 gdb_test_multiple "attach $testpid2" "$test attach2" {
619 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
620 gdb_test "y" "Reading symbols from .*attach2.*" $gdb_test_name
621 }
622 }
623
624 # Restore initial build situation.
625 remote_exec build "mv ${binfile} ${binfile3}"
626 remote_exec build "mv ${binfile}.initial ${binfile}"
627
628 # Don't leave a process around
629 kill_wait_spawned_process $test_spawn_id
630 kill_wait_spawned_process $test_spawn_id2
631 kill_wait_spawned_process $test_spawn_id3
632 }
633
634 do_attach_tests
635 do_attach_failure_tests
636 do_call_attach_tests
637 do_attach_exec_mismatch_handling_tests
638
639 # Test "gdb --pid"
640
641 do_command_attach_tests
642
643
644 test_command_line_attach_run
645
646 return 0
This page took 0.047553 seconds and 4 git commands to generate.