1 # Copyright 1997-2019 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 if {![can_spawn_for_attach]} {
20 standard_testfile attach.c attach2.c
21 set binfile2 ${binfile}2
22 set escapedbinfile [string_to_regexp $binfile]
24 #execute_anywhere "rm -f ${binfile} ${binfile2}"
25 remote_exec build "rm -f ${binfile} ${binfile2}"
26 # For debugging this test
30 # build the first test case
32 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
33 untested "failed to compile"
37 # Build the in-system-call test
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
40 untested "failed to compile in-system-call test"
44 if [get_compiler_info] {
48 # This is a test of the error cases for gdb's ability to attach to a
51 proc_with_prefix do_attach_failure_tests {} {
61 clean_restart $binfile
63 # Figure out a regular expression that will match the sysroot,
64 # noting that the default sysroot is "target:", and also noting
65 # that GDB will strip "target:" from the start of filenames when
66 # operating on the local filesystem. However the default sysroot
67 # can be set via configure option --with-sysroot, which can be "/".
68 # If $binfile is a absolute path, so pattern
69 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
71 set sysroot "\[^\r\n\]*"
73 # Start the program running and then wait for a bit, to be sure
74 # that it can be attached to.
76 set test_spawn_id [spawn_wait_for_attach $binfile]
77 set testpid [spawn_id_get_pid $test_spawn_id]
79 # Verify that we cannot attach to nonsense.
81 set test "attach to nonsense is prohibited"
82 gdb_test_multiple "attach abc" "$test" {
83 -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
86 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
87 # Response expected from /proc-based systems.
90 -re "Can't attach to process..*$gdb_prompt $" {
91 # Response expected on Cygwin
94 -re "Attaching to.*$gdb_prompt $" {
95 fail "$test (bogus pid allowed)"
99 # Verify that we cannot attach to nonsense even if its initial part is
102 set test "attach to digits-starting nonsense is prohibited"
103 gdb_test_multiple "attach ${testpid}x" "$test" {
104 -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" {
107 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
108 # Response expected from /proc-based systems.
111 -re "Can't attach to process..*$gdb_prompt $" {
112 # Response expected on Cygwin
115 -re "Attaching to.*$gdb_prompt $" {
116 fail "$test (bogus pid allowed)"
120 # Verify that we cannot attach to what appears to be a valid
121 # process ID, but is a process that doesn't exist. Traditionally,
122 # most systems didn't have a process with ID 0, so we take that as
123 # the default. However, there are a few exceptions.
126 if { [istarget "*-*-*bsd*"] } {
127 # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead
128 # (which should have the desired effect on any version of
129 # FreeBSD, and probably other *BSD's too).
132 set test "attach to nonexistent process is prohibited"
133 gdb_test_multiple "attach $boguspid" "$test" {
134 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
135 # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
138 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
139 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
142 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
145 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
148 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
149 # Response expected from /proc-based systems.
152 -re "Can't attach to process..*$gdb_prompt $" {
153 # Response expected on Cygwin
156 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
157 # Response expected on the extended-remote target.
162 # Verify that we can't double attach to the process.
164 set test "first attach"
165 gdb_test_multiple "attach $testpid" "$test" {
166 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
169 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
170 # Response expected on Cygwin.
175 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
176 gdb_test "inferior 2" "Switching to inferior 2.*" "switch to inferior 2"
178 set test "fail to attach again"
179 gdb_test_multiple "attach $testpid" "$test" {
180 -re "Attaching to process $testpid.*warning: process .* is already traced by process .*$gdb_prompt $" {
183 -re "Attaching to process .* failed.*$gdb_prompt $" {
184 # Response expected when using gdbserver.
189 gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1"
190 set test "exit after attach failures"
194 "Kill the program being debugged.*y or n. $" \
197 # Another "don't leave a process around"
198 kill_wait_spawned_process $test_spawn_id
201 # This is a test of gdb's ability to attach to a running process.
203 proc_with_prefix do_attach_tests {} {
206 global escapedbinfile
213 clean_restart $binfile
215 # Figure out a regular expression that will match the sysroot,
216 # noting that the default sysroot is "target:", and also noting
217 # that GDB will strip "target:" from the start of filenames when
218 # operating on the local filesystem. However the default sysroot
219 # can be set via configure option --with-sysroot, which can be "/".
220 # If $binfile is a absolute path, so pattern
221 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
223 set sysroot "\[^\r\n\]*"
225 # Start the program running and then wait for a bit, to be sure
226 # that it can be attached to.
228 set test_spawn_id [spawn_wait_for_attach $binfile]
229 set testpid [spawn_id_get_pid $test_spawn_id]
231 # Verify that we can attach to the process by first giving its
232 # executable name via the file command, and using attach with the
235 # (Actually, the test system appears to do this automatically for
236 # us. So, we must also be prepared to be asked if we want to
237 # discard an existing set of symbols.)
239 set test "set file, before attach1"
240 gdb_test_multiple "file $binfile" "$test" {
241 -re "Load new symbol table from.*y or n. $" {
242 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
245 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
250 set test "attach1, after setting file"
251 gdb_test_multiple "attach $testpid" "$test" {
252 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
255 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
256 # Response expected on Cygwin
261 # Verify that we can "see" the variable "should_exit" in the
262 # program, and that it is zero.
264 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
266 # Detach the process.
269 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
272 # Wait a bit for gdb to finish detaching
276 # Purge the symbols from gdb's brain. (We want to be certain the
277 # next attach, which won't be preceded by a "file" command, is
278 # really getting the executable file without our help.)
280 set old_timeout $timeout
282 set test "attach1, purging symbols after detach"
283 gdb_test_multiple "file" "$test" {
284 -re "No executable file now.*Discard symbol table.*y or n. $" {
285 gdb_test "y" "No symbol file now." "$test"
288 set timeout $old_timeout
290 # Verify that we can attach to the process just by giving the
293 set test "attach2, with no file"
294 set found_exec_file 0
295 gdb_test_multiple "attach $testpid" "$test" {
296 -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
297 # On Cygwin, the DLL's symbol tables are loaded prior to the
298 # executable's symbol table. This in turn always results in
299 # asking the user for actually loading the symbol table of the
301 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
304 set found_exec_file 1
306 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
308 set found_exec_file 1
312 if {$found_exec_file == 0} {
313 set test "load file manually, after attach2"
314 gdb_test_multiple "file $binfile" "$test" {
315 -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
316 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
319 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
325 # Verify that we can modify the variable "should_exit" in the
328 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
330 # Verify that the modification really happened.
332 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
333 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
335 # Allow the test process to exit, to cleanup after ourselves.
337 gdb_continue_to_end "after attach2, exit"
339 # Make sure we don't leave a process around to confuse
340 # the next test run (and prevent the compile by keeping
341 # the text file busy), in case the "set should_exit" didn't
344 kill_wait_spawned_process $test_spawn_id
346 set test_spawn_id [spawn_wait_for_attach $binfile]
347 set testpid [spawn_id_get_pid $test_spawn_id]
349 # Verify that we can attach to the process, and find its a.out
350 # when we're cd'd to some directory that doesn't contain the
351 # a.out. (We use the source path set by the "dir" command.)
353 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
356 gdb_test "cd /tmp" "Working directory /tmp." \
357 "cd away from process working directory"
359 # Explicitly flush out any knowledge of the previous attachment.
361 set test "before attach3, flush symbols"
362 gdb_test_multiple "symbol-file" "$test" {
363 -re "Discard symbol table from.*y or n. $" {
364 gdb_test "y" "No symbol file now." \
367 -re "No symbol file now.*$gdb_prompt $" {
372 gdb_test "exec" "No executable file now." \
373 "before attach3, flush exec"
375 gdb_test "attach $testpid" \
376 "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
377 "attach when process' a.out not in cwd"
379 set test "after attach3, exit"
383 "Kill the program being debugged.*y or n. $" \
386 # Another "don't leave a process around"
387 kill_wait_spawned_process $test_spawn_id
390 # Test attaching when the target is inside a system call.
392 proc_with_prefix do_call_attach_tests {} {
398 set test_spawn_id [spawn_wait_for_attach $binfile2]
399 set testpid [spawn_id_get_pid $test_spawn_id]
403 gdb_test "file $binfile2" ".*" "load file"
404 set test "attach call"
405 gdb_test_multiple "attach $testpid" "$test" {
406 -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
407 fail "$test (read register error)"
409 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
412 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
417 # See if other registers are problems
419 set test "info other register"
420 gdb_test_multiple "i r r3" "$test" {
421 -re "warning: reading register.*$gdb_prompt $" {
424 -re "r3.*$gdb_prompt $" {
429 # Get rid of the process
431 gdb_test "p should_exit = 1"
436 kill_wait_spawned_process $test_spawn_id
439 proc_with_prefix do_command_attach_tests {} {
444 global INTERNAL_GDBFLAGS
447 if ![isnative] then {
448 unsupported "command attach test"
452 set test_spawn_id [spawn_wait_for_attach $binfile]
453 set testpid [spawn_id_get_pid $test_spawn_id]
457 set res [gdb_spawn_with_cmdline_opts \
458 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid"]
459 set test "starting with --pid"
460 gdb_test_multiple "" $test {
461 -re "Reading symbols from.*$gdb_prompt $" {
466 # Get rid of the process
467 kill_wait_spawned_process $test_spawn_id
470 # Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
471 # "run" would run before the attach finished - PR17347.
473 proc test_command_line_attach_run {} {
477 if ![isnative] then {
478 unsupported "commandline attach run test"
482 with_test_prefix "cmdline attach run" {
483 set test_spawn_id [spawn_wait_for_attach $binfile]
484 set testpid [spawn_id_get_pid $test_spawn_id]
486 set test "run to prompt"
489 set res [gdb_spawn_with_cmdline_opts \
490 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
493 kill_wait_spawned_process $test_spawn_id
496 gdb_test_multiple "" $test {
497 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
504 set test "run to main"
505 gdb_test_multiple "" $test {
506 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
511 # Get rid of the process
512 kill_wait_spawned_process $test_spawn_id
517 do_attach_failure_tests
522 do_command_attach_tests
524 test_command_line_attach_run