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