gdb: handle endbr64 instruction in amd64_analyze_prologue
[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
21 set binfile2 ${binfile}2
22 set escapedbinfile [string_to_regexp $binfile]
23
24 #execute_anywhere "rm -f ${binfile} ${binfile2}"
25 remote_exec build "rm -f ${binfile} ${binfile2}"
26 # For debugging this test
27 #
28 #log_user 1
29
30 # build the first test case
31 #
32 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
33 untested "failed to compile"
34 return -1
35 }
36
37 # Build the in-system-call test
38
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
40 untested "failed to compile in-system-call test"
41 return -1
42 }
43
44 if [get_compiler_info] {
45 return -1
46 }
47
48 # This is a test of the error cases for gdb's ability to attach to a
49 # running process.
50
51 proc_with_prefix do_attach_failure_tests {} {
52 global gdb_prompt
53 global binfile
54 global escapedbinfile
55 global srcfile
56
57 clean_restart $binfile
58
59 # Figure out a regular expression that will match the sysroot,
60 # noting that the default sysroot is "target:", and also noting
61 # that GDB will strip "target:" from the start of filenames when
62 # operating on the local filesystem. However the default sysroot
63 # can be set via configure option --with-sysroot, which can be "/".
64 # If $binfile is a absolute path, so pattern
65 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
66 # $sysroot simple.
67 set sysroot "\[^\r\n\]*"
68
69 # Start the program running and then wait for a bit, to be sure
70 # that it can be attached to.
71
72 set test_spawn_id [spawn_wait_for_attach $binfile]
73 set testpid [spawn_id_get_pid $test_spawn_id]
74
75 # Verify that we cannot attach to nonsense.
76
77 set test "attach to nonsense is prohibited"
78 gdb_test_multiple "attach abc" "$test" {
79 -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
80 pass "$test"
81 }
82 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
83 # Response expected from /proc-based systems.
84 pass "$test"
85 }
86 -re "Can't attach to process..*$gdb_prompt $" {
87 # Response expected on Cygwin
88 pass "$test"
89 }
90 -re "Attaching to.*$gdb_prompt $" {
91 fail "$test (bogus pid allowed)"
92 }
93 }
94
95 # Verify that we cannot attach to nonsense even if its initial part is
96 # a valid PID.
97
98 set test "attach to digits-starting nonsense is prohibited"
99 gdb_test_multiple "attach ${testpid}x" "$test" {
100 -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" {
101 pass "$test"
102 }
103 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
104 # Response expected from /proc-based systems.
105 pass "$test"
106 }
107 -re "Can't attach to process..*$gdb_prompt $" {
108 # Response expected on Cygwin
109 pass "$test"
110 }
111 -re "Attaching to.*$gdb_prompt $" {
112 fail "$test (bogus pid allowed)"
113 }
114 }
115
116 # Verify that we cannot attach to what appears to be a valid
117 # process ID, but is a process that doesn't exist. Traditionally,
118 # most systems didn't have a process with ID 0, so we take that as
119 # the default. However, there are a few exceptions.
120
121 set boguspid 0
122 if { [istarget "*-*-*bsd*"] } {
123 # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead
124 # (which should have the desired effect on any version of
125 # FreeBSD, and probably other *BSD's too).
126 set boguspid -1
127 }
128 set test "attach to nonexistent process is prohibited"
129 gdb_test_multiple "attach $boguspid" "$test" {
130 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
131 # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
132 pass "$test"
133 }
134 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
135 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
136 pass "$test"
137 }
138 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
139 pass "$test"
140 }
141 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
142 pass "$test"
143 }
144 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
145 # Response expected from /proc-based systems.
146 pass "$test"
147 }
148 -re "Can't attach to process..*$gdb_prompt $" {
149 # Response expected on Cygwin
150 pass "$test"
151 }
152 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
153 # Response expected on the extended-remote target.
154 pass "$test"
155 }
156 }
157
158 # Verify that we can't double attach to the process.
159
160 set test "first attach"
161 gdb_test_multiple "attach $testpid" "$test" {
162 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
163 pass "$test"
164 }
165 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
166 # Response expected on Cygwin.
167 pass "$test"
168 }
169 }
170
171 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
172 gdb_test "inferior 2" "Switching to inferior 2.*" "switch to inferior 2"
173
174 set test "fail to attach again"
175 gdb_test_multiple "attach $testpid" "$test" {
176 -re "Attaching to process $testpid.*warning: process .* is already traced by process .*$gdb_prompt $" {
177 pass "$test"
178 }
179 -re "Attaching to process .* failed.*$gdb_prompt $" {
180 # Response expected when using gdbserver.
181 pass "$test"
182 }
183 }
184
185 gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1"
186 set test "exit after attach failures"
187 gdb_test "kill" \
188 "" \
189 "$test" \
190 "Kill the program being debugged.*y or n. $" \
191 "y"
192
193 # Another "don't leave a process around"
194 kill_wait_spawned_process $test_spawn_id
195 }
196
197 # This is a test of gdb's ability to attach to a running process.
198
199 proc_with_prefix do_attach_tests {} {
200 global gdb_prompt
201 global binfile
202 global escapedbinfile
203 global srcfile
204 global timeout
205 global decimal
206
207 clean_restart $binfile
208
209 # Figure out a regular expression that will match the sysroot,
210 # noting that the default sysroot is "target:", and also noting
211 # that GDB will strip "target:" from the start of filenames when
212 # operating on the local filesystem. However the default sysroot
213 # can be set via configure option --with-sysroot, which can be "/".
214 # If $binfile is a absolute path, so pattern
215 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
216 # $sysroot simple.
217 set sysroot "\[^\r\n\]*"
218
219 # Start the program running and then wait for a bit, to be sure
220 # that it can be attached to.
221
222 set test_spawn_id [spawn_wait_for_attach $binfile]
223 set testpid [spawn_id_get_pid $test_spawn_id]
224
225 # Verify that we can attach to the process by first giving its
226 # executable name via the file command, and using attach with the
227 # process ID.
228
229 # (Actually, the test system appears to do this automatically for
230 # us. So, we must also be prepared to be asked if we want to
231 # discard an existing set of symbols.)
232
233 set test "set file, before attach1"
234 gdb_test_multiple "file $binfile" "$test" {
235 -re "Load new symbol table from.*y or n. $" {
236 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
237 "$test (re-read)"
238 }
239 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
240 pass "$test"
241 }
242 }
243
244 set test "attach1, after setting file"
245 gdb_test_multiple "attach $testpid" "$test" {
246 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
247 pass "$test"
248 }
249 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
250 # Response expected on Cygwin
251 pass "$test"
252 }
253 }
254
255 # Verify that we can "see" the variable "should_exit" in the
256 # program, and that it is zero.
257
258 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
259
260 # Detach the process.
261
262 gdb_test "detach" \
263 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
264 "attach1 detach"
265
266 # Wait a bit for gdb to finish detaching
267
268 exec sleep 5
269
270 # Purge the symbols from gdb's brain. (We want to be certain the
271 # next attach, which won't be preceded by a "file" command, is
272 # really getting the executable file without our help.)
273
274 set old_timeout $timeout
275 set timeout 15
276 set test "attach1, purging symbols after detach"
277 gdb_test_multiple "file" "$test" {
278 -re "No executable file now.*Discard symbol table.*y or n. $" {
279 gdb_test "y" "No symbol file now." "$test"
280 }
281 }
282 set timeout $old_timeout
283
284 # Verify that we can attach to the process just by giving the
285 # process ID.
286
287 set test "attach2, with no file"
288 set found_exec_file 0
289 gdb_test_multiple "attach $testpid" "$test" {
290 -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
291 # On Cygwin, the DLL's symbol tables are loaded prior to the
292 # executable's symbol table. This in turn always results in
293 # asking the user for actually loading the symbol table of the
294 # executable.
295 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
296 "$test (reset file)"
297
298 set found_exec_file 1
299 }
300 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
301 pass "$test"
302 set found_exec_file 1
303 }
304 }
305
306 if {$found_exec_file == 0} {
307 set test "load file manually, after attach2"
308 gdb_test_multiple "file $binfile" "$test" {
309 -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
310 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
311 "$test (re-read)"
312 }
313 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
314 pass "$test"
315 }
316 }
317 }
318
319 # Verify that we can modify the variable "should_exit" in the
320 # program.
321
322 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
323
324 # Verify that the modification really happened.
325
326 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
327 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
328
329 # Allow the test process to exit, to cleanup after ourselves.
330
331 gdb_continue_to_end "after attach2, exit"
332
333 # Make sure we don't leave a process around to confuse
334 # the next test run (and prevent the compile by keeping
335 # the text file busy), in case the "set should_exit" didn't
336 # work.
337
338 kill_wait_spawned_process $test_spawn_id
339
340 set test_spawn_id [spawn_wait_for_attach $binfile]
341 set testpid [spawn_id_get_pid $test_spawn_id]
342
343 # Verify that we can attach to the process, and find its a.out
344 # when we're cd'd to some directory that doesn't contain the
345 # a.out. (We use the source path set by the "dir" command.)
346
347 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
348 "set source path"
349
350 gdb_test "cd /tmp" "Working directory /tmp." \
351 "cd away from process working directory"
352
353 # Explicitly flush out any knowledge of the previous attachment.
354
355 set test "before attach3, flush symbols"
356 gdb_test_multiple "symbol-file" "$test" {
357 -re "Discard symbol table from.*y or n. $" {
358 gdb_test "y" "No symbol file now." \
359 "$test"
360 }
361 -re "No symbol file now.*$gdb_prompt $" {
362 pass "$test"
363 }
364 }
365
366 gdb_test "exec" "No executable file now." \
367 "before attach3, flush exec"
368
369 gdb_test "attach $testpid" \
370 "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
371 "attach when process' a.out not in cwd"
372
373 set test "after attach3, exit"
374 gdb_test "kill" \
375 "" \
376 "$test" \
377 "Kill the program being debugged.*y or n. $" \
378 "y"
379
380 # Another "don't leave a process around"
381 kill_wait_spawned_process $test_spawn_id
382 }
383
384 # Test attaching when the target is inside a system call.
385
386 proc_with_prefix do_call_attach_tests {} {
387 global gdb_prompt
388 global binfile2
389
390 clean_restart
391
392 set test_spawn_id [spawn_wait_for_attach $binfile2]
393 set testpid [spawn_id_get_pid $test_spawn_id]
394
395 # Attach
396
397 gdb_test "file $binfile2" ".*" "load file"
398 set test "attach call"
399 gdb_test_multiple "attach $testpid" "$test" {
400 -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
401 fail "$test (read register error)"
402 }
403 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
404 pass "$test"
405 }
406 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
407 pass "$test"
408 }
409 }
410
411 # See if other registers are problems
412
413 set test "info other register"
414 gdb_test_multiple "i r r3" "$test" {
415 -re "warning: reading register.*$gdb_prompt $" {
416 fail "$test"
417 }
418 -re "r3.*$gdb_prompt $" {
419 pass "$test"
420 }
421 }
422
423 # Get rid of the process
424
425 gdb_test "p should_exit = 1"
426 gdb_continue_to_end
427
428 # Be paranoid
429
430 kill_wait_spawned_process $test_spawn_id
431 }
432
433 proc_with_prefix do_command_attach_tests {} {
434 global gdb_prompt
435 global binfile
436
437 if ![isnative] then {
438 unsupported "command attach test"
439 return 0
440 }
441
442 set test_spawn_id [spawn_wait_for_attach $binfile]
443 set testpid [spawn_id_get_pid $test_spawn_id]
444
445 gdb_exit
446
447 set res [gdb_spawn_with_cmdline_opts \
448 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid"]
449 set test "starting with --pid"
450 gdb_test_multiple "" $test {
451 -re "Reading symbols from.*$gdb_prompt $" {
452 pass "$test"
453 }
454 }
455
456 # Get rid of the process
457 kill_wait_spawned_process $test_spawn_id
458 }
459
460 # Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
461 # "run" would run before the attach finished - PR17347.
462
463 proc test_command_line_attach_run {} {
464 global gdb_prompt
465 global binfile
466
467 # Skip test if we cannot attach on the command line and use the run command.
468 # ??? Unclear what condition to use to return here when using gdbserver.
469 # ??? None of the below works.
470 # ![isnative] || [target_is_gdbserver]
471 # ![isnative] || [use_gdb_stub]
472 if { ![isnative] || [is_remote target] } then {
473 unsupported "commandline attach run test"
474 return 0
475 }
476
477 with_test_prefix "cmdline attach run" {
478 set test_spawn_id [spawn_wait_for_attach $binfile]
479 set testpid [spawn_id_get_pid $test_spawn_id]
480
481 set test "run to prompt"
482 gdb_exit
483
484 set res [gdb_spawn_with_cmdline_opts \
485 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
486 if { $res != 0} {
487 fail $test
488 kill_wait_spawned_process $test_spawn_id
489 return $res
490 }
491 gdb_test_multiple "" $test {
492 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
493 pass $test
494 }
495 }
496
497 send_gdb "y\n"
498
499 set test "run to main"
500 gdb_test_multiple "" $test {
501 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
502 pass $test
503 }
504 }
505
506 # Get rid of the process
507 kill_wait_spawned_process $test_spawn_id
508 }
509 }
510
511
512 # This is a test of 'set exec-file-mismatch' handling.
513
514 proc_with_prefix do_attach_exec_mismatch_handling_tests {} {
515 global gdb_prompt
516 global binfile
517 global binfile2
518
519 clean_restart $binfile
520
521 # Start two programs that can be attached to.
522 # The first program contains a 'int bidule' variable, the second a 'float bidule'.
523
524 set test_spawn_id [spawn_wait_for_attach $binfile]
525 set testpid [spawn_id_get_pid $test_spawn_id]
526 set test_spawn_id2 [spawn_wait_for_attach $binfile2]
527 set testpid2 [spawn_id_get_pid $test_spawn_id2]
528
529
530 # Test with the default value of 'set exec-file-mismatch load".
531 set test "mismatch load"
532 gdb_test "attach $testpid" "Attaching to program.*" "$test attach1"
533 # Verify that we can "see" the variable "bidule" in the
534 # program, and that it is an integer.
535 gdb_test "ptype bidule" " = int" "$test after attach1, bidule is int"
536 # Detach the process.
537 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach1"
538 gdb_test_multiple "attach $testpid2" "$test attach2" {
539 -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" {
540 pass "$test attach2"
541 }
542 }
543 gdb_test "y" "Reading symbols from .*attach2.*" "$test load attach2"
544 # Verify that we can "see" the variable "bidule" in the
545 # program, and that it is a float.
546 gdb_test "ptype bidule" " = float" "$test after attach2 and load, bidule is float"
547 # Detach the process.
548 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach2"
549
550
551 # Test with 'set exec-file-mismatch warn".
552 set test "mismatch warn"
553 gdb_test_no_output "set exec-file-mismatch warn"
554 gdb_test_multiple "attach $testpid" "$test attach" {
555 -re "Attaching to program.*exec-file-mismatch handling is currently \"warn\".*$gdb_prompt" {
556 pass "$test attach"
557 }
558 }
559 # Verify that we still (wrongly) "see" the variable "bidule" as a float,
560 # as we have not loaded the correct exec-file.
561 gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float"
562 # Detach the process.
563 gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach"
564
565
566 # Same test but with 'set exec-file-mismatch off".
567 set test "mismatch off"
568 gdb_test_no_output "set exec-file-mismatch off"
569 gdb_test_multiple "attach $testpid" "$test attach" {
570 -re "Attaching to program.*$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 warned the user and 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 # Don't leave a process around
582 kill_wait_spawned_process $test_spawn_id
583 kill_wait_spawned_process $test_spawn_id2
584 }
585
586 do_attach_tests
587 do_attach_failure_tests
588 do_call_attach_tests
589 do_attach_exec_mismatch_handling_tests
590
591 # Test "gdb --pid"
592
593 do_command_attach_tests
594
595
596 test_command_line_attach_run
597
598 return 0
This page took 0.041475 seconds and 4 git commands to generate.