]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/attach.exp
Update copyright year range in all GDB files.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / attach.exp
CommitLineData
b811d2c2 1# Copyright 1997-2020 Free Software Foundation, Inc.
74cf1395
JM
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
74cf1395 6# (at your option) any later version.
e22f8b7c 7#
74cf1395
JM
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.
e22f8b7c 12#
74cf1395 13# You should have received a copy of the GNU General Public License
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>. */
74cf1395 15
60b3033e 16if {![can_spawn_for_attach]} {
74cf1395
JM
17 return 0
18}
19
a64d2530
TT
20standard_testfile attach.c attach2.c
21set binfile2 ${binfile}2
22set escapedbinfile [string_to_regexp $binfile]
74cf1395
JM
23
24#execute_anywhere "rm -f ${binfile} ${binfile2}"
25remote_exec build "rm -f ${binfile} ${binfile2}"
26# For debugging this test
27#
28#log_user 1
29
74cf1395
JM
30# build the first test case
31#
32if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
84c93cd5 33 untested "failed to compile"
b60f0898 34 return -1
74cf1395
JM
35}
36
74cf1395
JM
37# Build the in-system-call test
38
39if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
84c93cd5 40 untested "failed to compile in-system-call test"
b60f0898 41 return -1
74cf1395
JM
42}
43
4c93b1db 44if [get_compiler_info] {
74cf1395
JM
45 return -1
46}
47
df0da8a2
AH
48# This is a test of the error cases for gdb's ability to attach to a
49# running process.
50
51proc_with_prefix do_attach_failure_tests {} {
1279f4ff
AC
52 global gdb_prompt
53 global binfile
54 global escapedbinfile
55 global srcfile
56 global testfile
1279f4ff
AC
57 global subdir
58 global timeout
f67c0c91 59 global decimal
df0da8a2
AH
60
61 clean_restart $binfile
62
1586c8fb
GB
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
d4d38844
YQ
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
70 # $sysroot simple.
71 set sysroot "\[^\r\n\]*"
1586c8fb 72
1279f4ff
AC
73 # Start the program running and then wait for a bit, to be sure
74 # that it can be attached to.
75
2c8c5d37
PA
76 set test_spawn_id [spawn_wait_for_attach $binfile]
77 set testpid [spawn_id_get_pid $test_spawn_id]
1279f4ff
AC
78
79 # Verify that we cannot attach to nonsense.
80
8b1b3228
AC
81 set test "attach to nonsense is prohibited"
82 gdb_test_multiple "attach abc" "$test" {
74164c56
JK
83 -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
84 pass "$test"
85 }
86 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
87 # Response expected from /proc-based systems.
88 pass "$test"
89 }
90 -re "Can't attach to process..*$gdb_prompt $" {
91 # Response expected on Cygwin
92 pass "$test"
93 }
94 -re "Attaching to.*$gdb_prompt $" {
95 fail "$test (bogus pid allowed)"
96 }
97 }
98
99 # Verify that we cannot attach to nonsense even if its initial part is
100 # a valid PID.
101
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 $" {
8b1b3228 105 pass "$test"
1279f4ff
AC
106 }
107 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
108 # Response expected from /proc-based systems.
8b1b3228 109 pass "$test"
1279f4ff 110 }
8b1b3228 111 -re "Can't attach to process..*$gdb_prompt $" {
1279f4ff 112 # Response expected on Cygwin
8b1b3228 113 pass "$test"
1279f4ff
AC
114 }
115 -re "Attaching to.*$gdb_prompt $" {
8b1b3228 116 fail "$test (bogus pid allowed)"
1279f4ff
AC
117 }
118 }
119
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.
124
125 set boguspid 0
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).
130 set boguspid -1
131 }
8b1b3228
AC
132 set test "attach to nonexistent process is prohibited"
133 gdb_test_multiple "attach $boguspid" "$test" {
1279f4ff
AC
134 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
135 # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
8b1b3228 136 pass "$test"
1279f4ff
AC
137 }
138 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
139 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
8b1b3228 140 pass "$test"
1279f4ff
AC
141 }
142 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
8b1b3228 143 pass "$test"
1279f4ff
AC
144 }
145 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
8b1b3228 146 pass "$test"
1279f4ff
AC
147 }
148 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
149 # Response expected from /proc-based systems.
8b1b3228 150 pass "$test"
1279f4ff 151 }
8b1b3228 152 -re "Can't attach to process..*$gdb_prompt $" {
1279f4ff 153 # Response expected on Cygwin
8b1b3228 154 pass "$test"
1279f4ff 155 }
7cee1e54
PA
156 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
157 # Response expected on the extended-remote target.
158 pass "$test"
159 }
1279f4ff
AC
160 }
161
df0da8a2
AH
162 # Verify that we can't double attach to the process.
163
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 $" {
167 pass "$test"
168 }
169 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
170 # Response expected on Cygwin.
171 pass "$test"
172 }
173 }
174
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"
177
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 $" {
181 pass "$test"
182 }
183 -re "Attaching to process .* failed.*$gdb_prompt $" {
184 # Response expected when using gdbserver.
185 pass "$test"
186 }
187 }
188
189 gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1"
190 set test "exit after attach failures"
191 gdb_test "kill" \
192 "" \
193 "$test" \
194 "Kill the program being debugged.*y or n. $" \
195 "y"
196
197 # Another "don't leave a process around"
198 kill_wait_spawned_process $test_spawn_id
199}
200
201# This is a test of gdb's ability to attach to a running process.
202
203proc_with_prefix do_attach_tests {} {
204 global gdb_prompt
205 global binfile
206 global escapedbinfile
207 global srcfile
208 global testfile
209 global subdir
210 global timeout
211 global decimal
212
213 clean_restart $binfile
214
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
222 # $sysroot simple.
223 set sysroot "\[^\r\n\]*"
224
225 # Start the program running and then wait for a bit, to be sure
226 # that it can be attached to.
227
228 set test_spawn_id [spawn_wait_for_attach $binfile]
229 set testpid [spawn_id_get_pid $test_spawn_id]
230
1279f4ff
AC
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
233 # process ID.
234
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.)
238
8b1b3228
AC
239 set test "set file, before attach1"
240 gdb_test_multiple "file $binfile" "$test" {
241 -re "Load new symbol table from.*y or n. $" {
3453e7e4 242 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
8b1b3228 243 "$test (re-read)"
1279f4ff 244 }
3453e7e4 245 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
8b1b3228 246 pass "$test"
1279f4ff
AC
247 }
248 }
249
8b1b3228
AC
250 set test "attach1, after setting file"
251 gdb_test_multiple "attach $testpid" "$test" {
1279f4ff 252 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
8b1b3228 253 pass "$test"
1279f4ff
AC
254 }
255 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
256 # Response expected on Cygwin
8b1b3228 257 pass "$test"
1279f4ff
AC
258 }
259 }
260
261 # Verify that we can "see" the variable "should_exit" in the
262 # program, and that it is zero.
263
8b1b3228 264 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
1279f4ff
AC
265
266 # Detach the process.
267
8b1b3228 268 gdb_test "detach" \
f67c0c91 269 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
8b1b3228 270 "attach1 detach"
1279f4ff
AC
271
272 # Wait a bit for gdb to finish detaching
273
274 exec sleep 5
275
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.)
279
280 set old_timeout $timeout
281 set timeout 15
8b1b3228
AC
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"
1279f4ff
AC
286 }
287 }
288 set timeout $old_timeout
289
290 # Verify that we can attach to the process just by giving the
291 # process ID.
292
7cee1e54
PA
293 set test "attach2, with no file"
294 set found_exec_file 0
8b1b3228 295 gdb_test_multiple "attach $testpid" "$test" {
1586c8fb 296 -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
1279f4ff
AC
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
300 # executable.
3453e7e4 301 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
8b1b3228 302 "$test (reset file)"
7cee1e54
PA
303
304 set found_exec_file 1
1279f4ff 305 }
1586c8fb 306 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
8b1b3228 307 pass "$test"
7cee1e54
PA
308 set found_exec_file 1
309 }
310 }
311
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. $" {
3453e7e4 316 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
7cee1e54
PA
317 "$test (re-read)"
318 }
3453e7e4 319 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
7cee1e54
PA
320 pass "$test"
321 }
1279f4ff
AC
322 }
323 }
324
325 # Verify that we can modify the variable "should_exit" in the
326 # program.
327
27d3a1a2 328 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
1279f4ff
AC
329
330 # Verify that the modification really happened.
331
1cf2f1b0
JK
332 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
333 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
1279f4ff
AC
334
335 # Allow the test process to exit, to cleanup after ourselves.
336
fda326dd 337 gdb_continue_to_end "after attach2, exit"
1279f4ff
AC
338
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
342 # work.
8b1b3228 343
2c8c5d37
PA
344 kill_wait_spawned_process $test_spawn_id
345
346 set test_spawn_id [spawn_wait_for_attach $binfile]
347 set testpid [spawn_id_get_pid $test_spawn_id]
1279f4ff
AC
348
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.)
352
a64d2530 353 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
8b1b3228 354 "set source path"
1279f4ff 355
8b1b3228
AC
356 gdb_test "cd /tmp" "Working directory /tmp." \
357 "cd away from process working directory"
1279f4ff
AC
358
359 # Explicitly flush out any knowledge of the previous attachment.
1279f4ff 360
8b1b3228 361 set test "before attach3, flush symbols"
6c95b8df 362 gdb_test_multiple "symbol-file" "$test" {
8b1b3228
AC
363 -re "Discard symbol table from.*y or n. $" {
364 gdb_test "y" "No symbol file now." \
365 "$test"
1279f4ff 366 }
8b1b3228
AC
367 -re "No symbol file now.*$gdb_prompt $" {
368 pass "$test"
1279f4ff
AC
369 }
370 }
371
8b1b3228
AC
372 gdb_test "exec" "No executable file now." \
373 "before attach3, flush exec"
374
375 gdb_test "attach $testpid" \
1586c8fb 376 "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
8b1b3228
AC
377 "attach when process' a.out not in cwd"
378
379 set test "after attach3, exit"
dfb88a23
MS
380 gdb_test "kill" \
381 "" \
382 "$test" \
383 "Kill the program being debugged.*y or n. $" \
384 "y"
1279f4ff
AC
385
386 # Another "don't leave a process around"
2c8c5d37 387 kill_wait_spawned_process $test_spawn_id
74cf1395
JM
388}
389
df0da8a2
AH
390# Test attaching when the target is inside a system call.
391
392proc_with_prefix do_call_attach_tests {} {
1279f4ff
AC
393 global gdb_prompt
394 global binfile2
df0da8a2
AH
395
396 clean_restart
397
2c8c5d37
PA
398 set test_spawn_id [spawn_wait_for_attach $binfile2]
399 set testpid [spawn_id_get_pid $test_spawn_id]
1279f4ff
AC
400
401 # Attach
402
8aed1c0d 403 gdb_test "file $binfile2" ".*" "load file"
8b1b3228
AC
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)"
1279f4ff
AC
408 }
409 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
8b1b3228 410 pass "$test"
1279f4ff
AC
411 }
412 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
8b1b3228 413 pass "$test"
1279f4ff
AC
414 }
415 }
416
417 # See if other registers are problems
418
8b1b3228
AC
419 set test "info other register"
420 gdb_test_multiple "i r r3" "$test" {
421 -re "warning: reading register.*$gdb_prompt $" {
422 fail "$test"
1279f4ff 423 }
8b1b3228
AC
424 -re "r3.*$gdb_prompt $" {
425 pass "$test"
1279f4ff 426 }
1279f4ff 427 }
74cf1395 428
1279f4ff
AC
429 # Get rid of the process
430
8b1b3228 431 gdb_test "p should_exit = 1"
fda326dd 432 gdb_continue_to_end
1279f4ff
AC
433
434 # Be paranoid
435
2c8c5d37 436 kill_wait_spawned_process $test_spawn_id
74cf1395
JM
437}
438
df0da8a2 439proc_with_prefix do_command_attach_tests {} {
ccdd1909
HZ
440 global gdb_prompt
441 global binfile
442 global verbose
443 global GDB
444 global INTERNAL_GDBFLAGS
445 global GDBFLAGS
446
447 if ![isnative] then {
448 unsupported "command attach test"
449 return 0
450 }
451
2c8c5d37
PA
452 set test_spawn_id [spawn_wait_for_attach $binfile]
453 set testpid [spawn_id_get_pid $test_spawn_id]
ccdd1909
HZ
454
455 gdb_exit
ccdd1909 456
fef1b293
TT
457 set res [gdb_spawn_with_cmdline_opts \
458 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid"]
ccdd1909 459 set test "starting with --pid"
2c8c5d37 460 gdb_test_multiple "" $test {
ccdd1909
HZ
461 -re "Reading symbols from.*$gdb_prompt $" {
462 pass "$test"
463 }
ccdd1909
HZ
464 }
465
466 # Get rid of the process
2c8c5d37 467 kill_wait_spawned_process $test_spawn_id
ccdd1909
HZ
468}
469
98880d46
PA
470# Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
471# "run" would run before the attach finished - PR17347.
472
473proc test_command_line_attach_run {} {
474 global gdb_prompt
475 global binfile
476
477 if ![isnative] then {
478 unsupported "commandline attach run test"
479 return 0
480 }
481
482 with_test_prefix "cmdline attach run" {
2c8c5d37
PA
483 set test_spawn_id [spawn_wait_for_attach $binfile]
484 set testpid [spawn_id_get_pid $test_spawn_id]
98880d46
PA
485
486 set test "run to prompt"
487 gdb_exit
488
489 set res [gdb_spawn_with_cmdline_opts \
fef1b293 490 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
98880d46
PA
491 if { $res != 0} {
492 fail $test
2c8c5d37 493 kill_wait_spawned_process $test_spawn_id
98880d46
PA
494 return $res
495 }
496 gdb_test_multiple "" $test {
497 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
498 pass $test
499 }
500 }
501
502 send_gdb "y\n"
503
504 set test "run to main"
505 gdb_test_multiple "" $test {
506 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
507 pass $test
508 }
509 }
510
511 # Get rid of the process
2c8c5d37 512 kill_wait_spawned_process $test_spawn_id
98880d46
PA
513 }
514}
74cf1395 515
74cf1395 516do_attach_tests
df0da8a2 517do_attach_failure_tests
74cf1395
JM
518do_call_attach_tests
519
ccdd1909
HZ
520# Test "gdb --pid"
521
522do_command_attach_tests
523
98880d46
PA
524test_command_line_attach_run
525
74cf1395 526return 0