]> 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
42a4f53d 1# Copyright 1997-2019 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
48proc do_attach_tests {} {
1279f4ff
AC
49 global gdb_prompt
50 global binfile
51 global escapedbinfile
52 global srcfile
53 global testfile
1279f4ff
AC
54 global subdir
55 global timeout
f67c0c91 56 global decimal
1279f4ff 57
1586c8fb
GB
58 # Figure out a regular expression that will match the sysroot,
59 # noting that the default sysroot is "target:", and also noting
60 # that GDB will strip "target:" from the start of filenames when
d4d38844
YQ
61 # operating on the local filesystem. However the default sysroot
62 # can be set via configure option --with-sysroot, which can be "/".
63 # If $binfile is a absolute path, so pattern
64 # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
65 # $sysroot simple.
66 set sysroot "\[^\r\n\]*"
1586c8fb 67
1279f4ff
AC
68 # Start the program running and then wait for a bit, to be sure
69 # that it can be attached to.
70
2c8c5d37
PA
71 set test_spawn_id [spawn_wait_for_attach $binfile]
72 set testpid [spawn_id_get_pid $test_spawn_id]
1279f4ff
AC
73
74 # Verify that we cannot attach to nonsense.
75
8b1b3228
AC
76 set test "attach to nonsense is prohibited"
77 gdb_test_multiple "attach abc" "$test" {
74164c56
JK
78 -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
79 pass "$test"
80 }
81 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
82 # Response expected from /proc-based systems.
83 pass "$test"
84 }
85 -re "Can't attach to process..*$gdb_prompt $" {
86 # Response expected on Cygwin
87 pass "$test"
88 }
89 -re "Attaching to.*$gdb_prompt $" {
90 fail "$test (bogus pid allowed)"
91 }
92 }
93
94 # Verify that we cannot attach to nonsense even if its initial part is
95 # a valid PID.
96
97 set test "attach to digits-starting nonsense is prohibited"
98 gdb_test_multiple "attach ${testpid}x" "$test" {
99 -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" {
8b1b3228 100 pass "$test"
1279f4ff
AC
101 }
102 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
103 # Response expected from /proc-based systems.
8b1b3228 104 pass "$test"
1279f4ff 105 }
8b1b3228 106 -re "Can't attach to process..*$gdb_prompt $" {
1279f4ff 107 # Response expected on Cygwin
8b1b3228 108 pass "$test"
1279f4ff
AC
109 }
110 -re "Attaching to.*$gdb_prompt $" {
8b1b3228 111 fail "$test (bogus pid allowed)"
1279f4ff
AC
112 }
113 }
114
115 # Verify that we cannot attach to what appears to be a valid
116 # process ID, but is a process that doesn't exist. Traditionally,
117 # most systems didn't have a process with ID 0, so we take that as
118 # the default. However, there are a few exceptions.
119
120 set boguspid 0
121 if { [istarget "*-*-*bsd*"] } {
122 # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead
123 # (which should have the desired effect on any version of
124 # FreeBSD, and probably other *BSD's too).
125 set boguspid -1
126 }
8b1b3228
AC
127 set test "attach to nonexistent process is prohibited"
128 gdb_test_multiple "attach $boguspid" "$test" {
1279f4ff
AC
129 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
130 # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
8b1b3228 131 pass "$test"
1279f4ff
AC
132 }
133 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
134 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
8b1b3228 135 pass "$test"
1279f4ff
AC
136 }
137 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
8b1b3228 138 pass "$test"
1279f4ff
AC
139 }
140 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
8b1b3228 141 pass "$test"
1279f4ff
AC
142 }
143 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
144 # Response expected from /proc-based systems.
8b1b3228 145 pass "$test"
1279f4ff 146 }
8b1b3228 147 -re "Can't attach to process..*$gdb_prompt $" {
1279f4ff 148 # Response expected on Cygwin
8b1b3228 149 pass "$test"
1279f4ff 150 }
7cee1e54
PA
151 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
152 # Response expected on the extended-remote target.
153 pass "$test"
154 }
1279f4ff
AC
155 }
156
157 # Verify that we can attach to the process by first giving its
158 # executable name via the file command, and using attach with the
159 # process ID.
160
161 # (Actually, the test system appears to do this automatically for
162 # us. So, we must also be prepared to be asked if we want to
163 # discard an existing set of symbols.)
164
8b1b3228
AC
165 set test "set file, before attach1"
166 gdb_test_multiple "file $binfile" "$test" {
167 -re "Load new symbol table from.*y or n. $" {
3453e7e4 168 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
8b1b3228 169 "$test (re-read)"
1279f4ff 170 }
3453e7e4 171 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
8b1b3228 172 pass "$test"
1279f4ff
AC
173 }
174 }
175
8b1b3228
AC
176 set test "attach1, after setting file"
177 gdb_test_multiple "attach $testpid" "$test" {
1279f4ff 178 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
8b1b3228 179 pass "$test"
1279f4ff
AC
180 }
181 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
182 # Response expected on Cygwin
8b1b3228 183 pass "$test"
1279f4ff
AC
184 }
185 }
186
187 # Verify that we can "see" the variable "should_exit" in the
188 # program, and that it is zero.
189
8b1b3228 190 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
1279f4ff
AC
191
192 # Detach the process.
193
8b1b3228 194 gdb_test "detach" \
f67c0c91 195 "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
8b1b3228 196 "attach1 detach"
1279f4ff
AC
197
198 # Wait a bit for gdb to finish detaching
199
200 exec sleep 5
201
202 # Purge the symbols from gdb's brain. (We want to be certain the
203 # next attach, which won't be preceded by a "file" command, is
204 # really getting the executable file without our help.)
205
206 set old_timeout $timeout
207 set timeout 15
8b1b3228
AC
208 set test "attach1, purging symbols after detach"
209 gdb_test_multiple "file" "$test" {
210 -re "No executable file now.*Discard symbol table.*y or n. $" {
211 gdb_test "y" "No symbol file now." "$test"
1279f4ff
AC
212 }
213 }
214 set timeout $old_timeout
215
216 # Verify that we can attach to the process just by giving the
217 # process ID.
218
7cee1e54
PA
219 set test "attach2, with no file"
220 set found_exec_file 0
8b1b3228 221 gdb_test_multiple "attach $testpid" "$test" {
1586c8fb 222 -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
1279f4ff
AC
223 # On Cygwin, the DLL's symbol tables are loaded prior to the
224 # executable's symbol table. This in turn always results in
225 # asking the user for actually loading the symbol table of the
226 # executable.
3453e7e4 227 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
8b1b3228 228 "$test (reset file)"
7cee1e54
PA
229
230 set found_exec_file 1
1279f4ff 231 }
1586c8fb 232 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
8b1b3228 233 pass "$test"
7cee1e54
PA
234 set found_exec_file 1
235 }
236 }
237
238 if {$found_exec_file == 0} {
239 set test "load file manually, after attach2"
240 gdb_test_multiple "file $binfile" "$test" {
241 -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
3453e7e4 242 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
7cee1e54
PA
243 "$test (re-read)"
244 }
3453e7e4 245 -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
7cee1e54
PA
246 pass "$test"
247 }
1279f4ff
AC
248 }
249 }
250
251 # Verify that we can modify the variable "should_exit" in the
252 # program.
253
27d3a1a2 254 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
1279f4ff
AC
255
256 # Verify that the modification really happened.
257
1cf2f1b0
JK
258 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
259 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
1279f4ff
AC
260
261 # Allow the test process to exit, to cleanup after ourselves.
262
fda326dd 263 gdb_continue_to_end "after attach2, exit"
1279f4ff
AC
264
265 # Make sure we don't leave a process around to confuse
266 # the next test run (and prevent the compile by keeping
267 # the text file busy), in case the "set should_exit" didn't
268 # work.
8b1b3228 269
2c8c5d37
PA
270 kill_wait_spawned_process $test_spawn_id
271
272 set test_spawn_id [spawn_wait_for_attach $binfile]
273 set testpid [spawn_id_get_pid $test_spawn_id]
1279f4ff
AC
274
275 # Verify that we can attach to the process, and find its a.out
276 # when we're cd'd to some directory that doesn't contain the
277 # a.out. (We use the source path set by the "dir" command.)
278
a64d2530 279 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
8b1b3228 280 "set source path"
1279f4ff 281
8b1b3228
AC
282 gdb_test "cd /tmp" "Working directory /tmp." \
283 "cd away from process working directory"
1279f4ff
AC
284
285 # Explicitly flush out any knowledge of the previous attachment.
1279f4ff 286
8b1b3228 287 set test "before attach3, flush symbols"
6c95b8df 288 gdb_test_multiple "symbol-file" "$test" {
8b1b3228
AC
289 -re "Discard symbol table from.*y or n. $" {
290 gdb_test "y" "No symbol file now." \
291 "$test"
1279f4ff 292 }
8b1b3228
AC
293 -re "No symbol file now.*$gdb_prompt $" {
294 pass "$test"
1279f4ff
AC
295 }
296 }
297
8b1b3228
AC
298 gdb_test "exec" "No executable file now." \
299 "before attach3, flush exec"
300
301 gdb_test "attach $testpid" \
1586c8fb 302 "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
8b1b3228
AC
303 "attach when process' a.out not in cwd"
304
305 set test "after attach3, exit"
dfb88a23
MS
306 gdb_test "kill" \
307 "" \
308 "$test" \
309 "Kill the program being debugged.*y or n. $" \
310 "y"
1279f4ff
AC
311
312 # Another "don't leave a process around"
2c8c5d37 313 kill_wait_spawned_process $test_spawn_id
74cf1395
JM
314}
315
316proc do_call_attach_tests {} {
1279f4ff
AC
317 global gdb_prompt
318 global binfile2
319
2c8c5d37
PA
320 set test_spawn_id [spawn_wait_for_attach $binfile2]
321 set testpid [spawn_id_get_pid $test_spawn_id]
1279f4ff
AC
322
323 # Attach
324
8aed1c0d 325 gdb_test "file $binfile2" ".*" "load file"
8b1b3228
AC
326 set test "attach call"
327 gdb_test_multiple "attach $testpid" "$test" {
328 -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
329 fail "$test (read register error)"
1279f4ff
AC
330 }
331 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
8b1b3228 332 pass "$test"
1279f4ff
AC
333 }
334 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
8b1b3228 335 pass "$test"
1279f4ff
AC
336 }
337 }
338
339 # See if other registers are problems
340
8b1b3228
AC
341 set test "info other register"
342 gdb_test_multiple "i r r3" "$test" {
343 -re "warning: reading register.*$gdb_prompt $" {
344 fail "$test"
1279f4ff 345 }
8b1b3228
AC
346 -re "r3.*$gdb_prompt $" {
347 pass "$test"
1279f4ff 348 }
1279f4ff 349 }
74cf1395 350
1279f4ff
AC
351 # Get rid of the process
352
8b1b3228 353 gdb_test "p should_exit = 1"
fda326dd 354 gdb_continue_to_end
1279f4ff
AC
355
356 # Be paranoid
357
2c8c5d37 358 kill_wait_spawned_process $test_spawn_id
74cf1395
JM
359}
360
ccdd1909
HZ
361proc do_command_attach_tests {} {
362 global gdb_prompt
363 global binfile
364 global verbose
365 global GDB
366 global INTERNAL_GDBFLAGS
367 global GDBFLAGS
368
369 if ![isnative] then {
370 unsupported "command attach test"
371 return 0
372 }
373
2c8c5d37
PA
374 set test_spawn_id [spawn_wait_for_attach $binfile]
375 set testpid [spawn_id_get_pid $test_spawn_id]
ccdd1909
HZ
376
377 gdb_exit
ccdd1909 378
fef1b293
TT
379 set res [gdb_spawn_with_cmdline_opts \
380 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid"]
ccdd1909 381 set test "starting with --pid"
2c8c5d37 382 gdb_test_multiple "" $test {
ccdd1909
HZ
383 -re "Reading symbols from.*$gdb_prompt $" {
384 pass "$test"
385 }
ccdd1909
HZ
386 }
387
388 # Get rid of the process
2c8c5d37 389 kill_wait_spawned_process $test_spawn_id
ccdd1909
HZ
390}
391
98880d46
PA
392# Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
393# "run" would run before the attach finished - PR17347.
394
395proc test_command_line_attach_run {} {
396 global gdb_prompt
397 global binfile
398
399 if ![isnative] then {
400 unsupported "commandline attach run test"
401 return 0
402 }
403
404 with_test_prefix "cmdline attach run" {
2c8c5d37
PA
405 set test_spawn_id [spawn_wait_for_attach $binfile]
406 set testpid [spawn_id_get_pid $test_spawn_id]
98880d46
PA
407
408 set test "run to prompt"
409 gdb_exit
410
411 set res [gdb_spawn_with_cmdline_opts \
fef1b293 412 "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
98880d46
PA
413 if { $res != 0} {
414 fail $test
2c8c5d37 415 kill_wait_spawned_process $test_spawn_id
98880d46
PA
416 return $res
417 }
418 gdb_test_multiple "" $test {
419 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
420 pass $test
421 }
422 }
423
424 send_gdb "y\n"
425
426 set test "run to main"
427 gdb_test_multiple "" $test {
428 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
429 pass $test
430 }
431 }
432
433 # Get rid of the process
2c8c5d37 434 kill_wait_spawned_process $test_spawn_id
98880d46
PA
435 }
436}
74cf1395
JM
437
438# Start with a fresh gdb
1279f4ff 439
74cf1395
JM
440gdb_exit
441gdb_start
442gdb_reinitialize_dir $srcdir/$subdir
443gdb_load ${binfile}
444
445# This is a test of gdb's ability to attach to a running process.
1279f4ff 446
74cf1395
JM
447do_attach_tests
448
449# Test attaching when the target is inside a system call
1279f4ff 450
74cf1395
JM
451gdb_exit
452gdb_start
453
74cf1395
JM
454gdb_reinitialize_dir $srcdir/$subdir
455do_call_attach_tests
456
ccdd1909
HZ
457# Test "gdb --pid"
458
459do_command_attach_tests
460
98880d46
PA
461test_command_line_attach_run
462
74cf1395 463return 0