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