Update copyright year range in all GDB files.
[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 global testfile
57 global subdir
58 global timeout
59 global decimal
60
61 clean_restart $binfile
62
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
70 # $sysroot simple.
71 set sysroot "\[^\r\n\]*"
72
73 # Start the program running and then wait for a bit, to be sure
74 # that it can be attached to.
75
76 set test_spawn_id [spawn_wait_for_attach $binfile]
77 set testpid [spawn_id_get_pid $test_spawn_id]
78
79 # Verify that we cannot attach to nonsense.
80
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 $" {
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 $" {
105 pass "$test"
106 }
107 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
108 # Response expected from /proc-based systems.
109 pass "$test"
110 }
111 -re "Can't attach to process..*$gdb_prompt $" {
112 # Response expected on Cygwin
113 pass "$test"
114 }
115 -re "Attaching to.*$gdb_prompt $" {
116 fail "$test (bogus pid allowed)"
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 }
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).
136 pass "$test"
137 }
138 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
139 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
140 pass "$test"
141 }
142 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
143 pass "$test"
144 }
145 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
146 pass "$test"
147 }
148 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
149 # Response expected from /proc-based systems.
150 pass "$test"
151 }
152 -re "Can't attach to process..*$gdb_prompt $" {
153 # Response expected on Cygwin
154 pass "$test"
155 }
156 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
157 # Response expected on the extended-remote target.
158 pass "$test"
159 }
160 }
161
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
203 proc_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
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
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\.\.\.*" \
243 "$test (re-read)"
244 }
245 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
246 pass "$test"
247 }
248 }
249
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 $" {
253 pass "$test"
254 }
255 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
256 # Response expected on Cygwin
257 pass "$test"
258 }
259 }
260
261 # Verify that we can "see" the variable "should_exit" in the
262 # program, and that it is zero.
263
264 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
265
266 # Detach the process.
267
268 gdb_test "detach" \
269 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
270 "attach1 detach"
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
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"
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
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
300 # executable.
301 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
302 "$test (reset file)"
303
304 set found_exec_file 1
305 }
306 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
307 pass "$test"
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. $" {
316 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
317 "$test (re-read)"
318 }
319 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
320 pass "$test"
321 }
322 }
323 }
324
325 # Verify that we can modify the variable "should_exit" in the
326 # program.
327
328 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
329
330 # Verify that the modification really happened.
331
332 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
333 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
334
335 # Allow the test process to exit, to cleanup after ourselves.
336
337 gdb_continue_to_end "after attach2, exit"
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.
343
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]
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
353 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
354 "set source path"
355
356 gdb_test "cd /tmp" "Working directory /tmp." \
357 "cd away from process working directory"
358
359 # Explicitly flush out any knowledge of the previous attachment.
360
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." \
365 "$test"
366 }
367 -re "No symbol file now.*$gdb_prompt $" {
368 pass "$test"
369 }
370 }
371
372 gdb_test "exec" "No executable file now." \
373 "before attach3, flush exec"
374
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"
378
379 set test "after attach3, exit"
380 gdb_test "kill" \
381 "" \
382 "$test" \
383 "Kill the program being debugged.*y or n. $" \
384 "y"
385
386 # Another "don't leave a process around"
387 kill_wait_spawned_process $test_spawn_id
388 }
389
390 # Test attaching when the target is inside a system call.
391
392 proc_with_prefix do_call_attach_tests {} {
393 global gdb_prompt
394 global binfile2
395
396 clean_restart
397
398 set test_spawn_id [spawn_wait_for_attach $binfile2]
399 set testpid [spawn_id_get_pid $test_spawn_id]
400
401 # Attach
402
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)"
408 }
409 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
410 pass "$test"
411 }
412 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
413 pass "$test"
414 }
415 }
416
417 # See if other registers are problems
418
419 set test "info other register"
420 gdb_test_multiple "i r r3" "$test" {
421 -re "warning: reading register.*$gdb_prompt $" {
422 fail "$test"
423 }
424 -re "r3.*$gdb_prompt $" {
425 pass "$test"
426 }
427 }
428
429 # Get rid of the process
430
431 gdb_test "p should_exit = 1"
432 gdb_continue_to_end
433
434 # Be paranoid
435
436 kill_wait_spawned_process $test_spawn_id
437 }
438
439 proc_with_prefix do_command_attach_tests {} {
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
452 set test_spawn_id [spawn_wait_for_attach $binfile]
453 set testpid [spawn_id_get_pid $test_spawn_id]
454
455 gdb_exit
456
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 $" {
462 pass "$test"
463 }
464 }
465
466 # Get rid of the process
467 kill_wait_spawned_process $test_spawn_id
468 }
469
470 # Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
471 # "run" would run before the attach finished - PR17347.
472
473 proc 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" {
483 set test_spawn_id [spawn_wait_for_attach $binfile]
484 set testpid [spawn_id_get_pid $test_spawn_id]
485
486 set test "run to prompt"
487 gdb_exit
488
489 set res [gdb_spawn_with_cmdline_opts \
490 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
491 if { $res != 0} {
492 fail $test
493 kill_wait_spawned_process $test_spawn_id
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
512 kill_wait_spawned_process $test_spawn_id
513 }
514 }
515
516 do_attach_tests
517 do_attach_failure_tests
518 do_call_attach_tests
519
520 # Test "gdb --pid"
521
522 do_command_attach_tests
523
524 test_command_line_attach_run
525
526 return 0
This page took 0.040157 seconds and 4 git commands to generate.