gdb.base/printcmds.c C++-ify
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / attach.exp
1 # Copyright 1997-2020 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 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 with_test_prefix "cmdline attach run" {
486 set test_spawn_id [spawn_wait_for_attach $binfile]
487 set testpid [spawn_id_get_pid $test_spawn_id]
488
489 set test "run to prompt"
490 gdb_exit
491
492 set res [gdb_spawn_with_cmdline_opts \
493 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
494 if { $res != 0} {
495 fail $test
496 kill_wait_spawned_process $test_spawn_id
497 return $res
498 }
499 gdb_test_multiple "" $test {
500 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
501 pass $test
502 }
503 }
504
505 send_gdb "y\n"
506
507 set test "run to main"
508 gdb_test_multiple "" $test {
509 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
510 pass $test
511 }
512 }
513
514 # Get rid of the process
515 kill_wait_spawned_process $test_spawn_id
516 }
517 }
518
519
520 # This is a test of 'set exec-file-mismatch' handling.
521
522 proc_with_prefix do_attach_exec_mismatch_handling_tests {} {
523 global gdb_prompt
524 global binfile
525 global binfile2
526 global binfile3
527
528 clean_restart $binfile
529
530 # Start two programs that can be attached to.
531 # The first program contains a 'int bidule' variable, the second a 'float bidule'.
532
533 set test_spawn_id [spawn_wait_for_attach $binfile]
534 set testpid [spawn_id_get_pid $test_spawn_id]
535 set test_spawn_id2 [spawn_wait_for_attach $binfile2]
536 set testpid2 [spawn_id_get_pid $test_spawn_id2]
537
538
539 # Test with the default value of 'set exec-file-mismatch load".
540 set test "mismatch load"
541 gdb_test "attach $testpid" "Attaching to program.*" "$test attach1"
542 # Verify that we can "see" the variable "bidule" in the
543 # program, and that it is an integer.
544 gdb_test "ptype bidule" " = int" "$test after attach1, bidule is int"
545 # Detach the process.
546 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach1"
547 gdb_test_multiple "attach $testpid2" "$test attach2" {
548 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
549 pass "$test attach2"
550 }
551 }
552 gdb_test "y" "Reading symbols from .*attach2.*" "$test load attach2"
553 # Verify that we can "see" the variable "bidule" in the
554 # program, and that it is a float.
555 gdb_test "ptype bidule" " = float" "$test after attach2 and load, bidule is float"
556 # Detach the process.
557 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach2"
558
559
560 # Test with 'set exec-file-mismatch warn".
561 set test "mismatch warn"
562 gdb_test_no_output "set exec-file-mismatch warn"
563 gdb_test_multiple "attach $testpid" "$test attach" {
564 -re "Attaching to program.*exec-file-mismatch handling is currently \"warn\".*$gdb_prompt" {
565 pass "$test attach"
566 }
567 }
568 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
569 # as we have not loaded the correct exec-file.
570 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
571 # Detach the process.
572 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
573
574
575 # Same test but with 'set exec-file-mismatch off".
576 set test "mismatch off"
577 gdb_test_no_output "set exec-file-mismatch off"
578 gdb_test_multiple "attach $testpid" "$test attach" {
579 -re "Attaching to program.*$gdb_prompt" {
580 pass "$test attach"
581 }
582 }
583 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
584 # as we have not warned the user and not loaded the correct exec-file
585 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
586 # Detach the process.
587 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
588
589 # Test that the 'exec-file' changed is checked before exec-file-mismatch.
590 set test "mismatch exec-file changed has priority"
591 gdb_test_no_output "set exec-file-mismatch ask"
592 gdb_test_multiple "attach $testpid" "$test attach1 again, initial exec-file" {
593 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach\".*\(y or n\)" {
594 gdb_test "y" "Reading symbols from .*attach.*" $gdb_test_name
595 }
596 }
597
598
599 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach initial exec-file"
600
601 # Change the exec-file and attach to a new process using the changed file.
602 remote_exec build "mv ${binfile} ${binfile}.initial"
603 remote_exec build "mv ${binfile3} ${binfile}"
604 # Ensure GDB detects ${binfile} has changed when checking timestamp.
605 sleep 1
606 remote_exec build "touch ${binfile}"
607 set test_spawn_id3 [spawn_wait_for_attach $binfile]
608 set testpid3 [spawn_id_get_pid $test_spawn_id3]
609
610 gdb_test "attach $testpid3" "Attaching to program.*attach' has changed; re-reading symbols.*" \
611 "$test attach1 again, after changing exec-file"
612 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach after attach changed exec-file"
613
614 # Now, test the situation when current exec-file has changed
615 # and we attach to a pid using another file.
616 # Ensure GDB detects ${binfile} has changed when checking timestamp.
617 sleep 1
618 remote_exec build "touch ${binfile}"
619
620 gdb_test_multiple "attach $testpid2" "$test attach2" {
621 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
622 gdb_test "y" "Reading symbols from .*attach2.*" $gdb_test_name
623 }
624 }
625
626 # Restore initial build situation.
627 remote_exec build "mv ${binfile} ${binfile3}"
628 remote_exec build "mv ${binfile}.initial ${binfile}"
629
630 # Don't leave a process around
631 kill_wait_spawned_process $test_spawn_id
632 kill_wait_spawned_process $test_spawn_id2
633 kill_wait_spawned_process $test_spawn_id3
634 }
635
636 do_attach_tests
637 do_attach_failure_tests
638 do_call_attach_tests
639 do_attach_exec_mismatch_handling_tests
640
641 # Test "gdb --pid"
642
643 do_command_attach_tests
644
645
646 test_command_line_attach_run
647
648 return 0
This page took 0.065861 seconds and 4 git commands to generate.