[Committing the `catch syscall' patch for ARM, from Samuel Bronson.]
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / attach.exp
CommitLineData
28e7fd62 1# Copyright 1997-2013 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
74cf1395 16
33c3e192
FF
17# On HP-UX 11.0, this test is causing a process running the program
18# "attach" to be left around spinning. Until we figure out why, I am
19# commenting out the test to avoid polluting tiamat (our 11.0 nightly
20# test machine) with these processes. RT
21#
22# Setting the magic bit in the target app should work. I added a
23# "kill", and also a test for the R3 register warning. JB
24if { [istarget "hppa*-*-hpux*"] } {
25 return 0
26}
74cf1395 27
33c3e192 28# are we on a target board
2d1676a0 29if [is_remote target] then {
74cf1395
JM
30 return 0
31}
32
a64d2530
TT
33standard_testfile attach.c attach2.c
34set binfile2 ${binfile}2
35set escapedbinfile [string_to_regexp $binfile]
74cf1395
JM
36
37#execute_anywhere "rm -f ${binfile} ${binfile2}"
38remote_exec build "rm -f ${binfile} ${binfile2}"
39# For debugging this test
40#
41#log_user 1
42
74cf1395
JM
43# build the first test case
44#
45if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
b60f0898
JB
46 untested attach.exp
47 return -1
74cf1395
JM
48}
49
74cf1395
JM
50# Build the in-system-call test
51
52if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
b60f0898
JB
53 untested attach.exp
54 return -1
74cf1395
JM
55}
56
4c93b1db 57if [get_compiler_info] {
74cf1395
JM
58 return -1
59}
60
61proc do_attach_tests {} {
1279f4ff
AC
62 global gdb_prompt
63 global binfile
64 global escapedbinfile
65 global srcfile
66 global testfile
1279f4ff
AC
67 global subdir
68 global timeout
69
70 # Start the program running and then wait for a bit, to be sure
71 # that it can be attached to.
72
73 set testpid [eval exec $binfile &]
74 exec sleep 2
75 if { [istarget "*-*-cygwin*"] } {
76 # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
77 # different due to the way fork/exec works.
78 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
79 }
80
81 # Verify that we cannot attach to nonsense.
82
8b1b3228
AC
83 set test "attach to nonsense is prohibited"
84 gdb_test_multiple "attach abc" "$test" {
74164c56
JK
85 -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
86 pass "$test"
87 }
88 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
89 # Response expected from /proc-based systems.
90 pass "$test"
91 }
92 -re "Can't attach to process..*$gdb_prompt $" {
93 # Response expected on Cygwin
94 pass "$test"
95 }
96 -re "Attaching to.*$gdb_prompt $" {
97 fail "$test (bogus pid allowed)"
98 }
99 }
100
101 # Verify that we cannot attach to nonsense even if its initial part is
102 # a valid PID.
103
104 set test "attach to digits-starting nonsense is prohibited"
105 gdb_test_multiple "attach ${testpid}x" "$test" {
106 -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" {
8b1b3228 107 pass "$test"
1279f4ff
AC
108 }
109 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
110 # Response expected from /proc-based systems.
8b1b3228 111 pass "$test"
1279f4ff 112 }
8b1b3228 113 -re "Can't attach to process..*$gdb_prompt $" {
1279f4ff 114 # Response expected on Cygwin
8b1b3228 115 pass "$test"
1279f4ff
AC
116 }
117 -re "Attaching to.*$gdb_prompt $" {
8b1b3228 118 fail "$test (bogus pid allowed)"
1279f4ff
AC
119 }
120 }
121
122 # Verify that we cannot attach to what appears to be a valid
123 # process ID, but is a process that doesn't exist. Traditionally,
124 # most systems didn't have a process with ID 0, so we take that as
125 # the default. However, there are a few exceptions.
126
127 set boguspid 0
128 if { [istarget "*-*-*bsd*"] } {
129 # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead
130 # (which should have the desired effect on any version of
131 # FreeBSD, and probably other *BSD's too).
132 set boguspid -1
133 }
8b1b3228
AC
134 set test "attach to nonexistent process is prohibited"
135 gdb_test_multiple "attach $boguspid" "$test" {
1279f4ff
AC
136 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
137 # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
8b1b3228 138 pass "$test"
1279f4ff
AC
139 }
140 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
141 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
8b1b3228 142 pass "$test"
1279f4ff
AC
143 }
144 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
8b1b3228 145 pass "$test"
1279f4ff
AC
146 }
147 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
8b1b3228 148 pass "$test"
1279f4ff
AC
149 }
150 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
151 # Response expected from /proc-based systems.
8b1b3228 152 pass "$test"
1279f4ff 153 }
8b1b3228 154 -re "Can't attach to process..*$gdb_prompt $" {
1279f4ff 155 # Response expected on Cygwin
8b1b3228 156 pass "$test"
1279f4ff 157 }
7cee1e54
PA
158 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
159 # Response expected on the extended-remote target.
160 pass "$test"
161 }
1279f4ff
AC
162 }
163
164 # Verify that we can attach to the process by first giving its
165 # executable name via the file command, and using attach with the
166 # process ID.
167
168 # (Actually, the test system appears to do this automatically for
169 # us. So, we must also be prepared to be asked if we want to
170 # discard an existing set of symbols.)
171
8b1b3228
AC
172 set test "set file, before attach1"
173 gdb_test_multiple "file $binfile" "$test" {
174 -re "Load new symbol table from.*y or n. $" {
175 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
176 "$test (re-read)"
1279f4ff
AC
177 }
178 -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
8b1b3228 179 pass "$test"
1279f4ff
AC
180 }
181 }
182
8b1b3228
AC
183 set test "attach1, after setting file"
184 gdb_test_multiple "attach $testpid" "$test" {
1279f4ff 185 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
8b1b3228 186 pass "$test"
1279f4ff
AC
187 }
188 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
189 # Response expected on Cygwin
8b1b3228 190 pass "$test"
1279f4ff
AC
191 }
192 }
193
194 # Verify that we can "see" the variable "should_exit" in the
195 # program, and that it is zero.
196
8b1b3228 197 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
1279f4ff
AC
198
199 # Detach the process.
200
8b1b3228
AC
201 gdb_test "detach" \
202 "Detaching from program: .*$escapedbinfile, process $testpid" \
203 "attach1 detach"
1279f4ff
AC
204
205 # Wait a bit for gdb to finish detaching
206
207 exec sleep 5
208
209 # Purge the symbols from gdb's brain. (We want to be certain the
210 # next attach, which won't be preceded by a "file" command, is
211 # really getting the executable file without our help.)
212
213 set old_timeout $timeout
214 set timeout 15
8b1b3228
AC
215 set test "attach1, purging symbols after detach"
216 gdb_test_multiple "file" "$test" {
217 -re "No executable file now.*Discard symbol table.*y or n. $" {
218 gdb_test "y" "No symbol file now." "$test"
1279f4ff
AC
219 }
220 }
221 set timeout $old_timeout
222
223 # Verify that we can attach to the process just by giving the
224 # process ID.
225
7cee1e54
PA
226 set test "attach2, with no file"
227 set found_exec_file 0
8b1b3228
AC
228 gdb_test_multiple "attach $testpid" "$test" {
229 -re "Attaching to process $testpid.*Load new symbol table from \"$escapedbinfile\.exe\".*y or n. $" {
1279f4ff
AC
230 # On Cygwin, the DLL's symbol tables are loaded prior to the
231 # executable's symbol table. This in turn always results in
232 # asking the user for actually loading the symbol table of the
233 # executable.
8b1b3228
AC
234 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
235 "$test (reset file)"
7cee1e54
PA
236
237 set found_exec_file 1
1279f4ff
AC
238 }
239 -re "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*$gdb_prompt $" {
8b1b3228 240 pass "$test"
7cee1e54
PA
241 set found_exec_file 1
242 }
243 }
244
245 if {$found_exec_file == 0} {
246 set test "load file manually, after attach2"
247 gdb_test_multiple "file $binfile" "$test" {
248 -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
249 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
250 "$test (re-read)"
251 }
252 -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
253 pass "$test"
254 }
1279f4ff
AC
255 }
256 }
257
258 # Verify that we can modify the variable "should_exit" in the
259 # program.
260
27d3a1a2 261 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
1279f4ff
AC
262
263 # Verify that the modification really happened.
264
6acb16a2
MS
265 gdb_test "tbreak 19" "Temporary breakpoint .*at.*$srcfile, line 19.*" \
266 "after attach2, set tbreak postloop"
267
268 gdb_test "continue" "main.*at.*$srcfile:19.*" \
269 "after attach2, reach tbreak postloop"
1279f4ff
AC
270
271 # Allow the test process to exit, to cleanup after ourselves.
272
fda326dd 273 gdb_continue_to_end "after attach2, exit"
1279f4ff
AC
274
275 # Make sure we don't leave a process around to confuse
276 # the next test run (and prevent the compile by keeping
277 # the text file busy), in case the "set should_exit" didn't
278 # work.
279
280 remote_exec build "kill -9 ${testpid}"
8b1b3228 281
1279f4ff
AC
282 # Start the program running and then wait for a bit, to be sure
283 # that it can be attached to.
284
285 set testpid [eval exec $binfile &]
286 exec sleep 2
287 if { [istarget "*-*-cygwin*"] } {
288 # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
289 # different due to the way fork/exec works.
290 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
291 }
292
293 # Verify that we can attach to the process, and find its a.out
294 # when we're cd'd to some directory that doesn't contain the
295 # a.out. (We use the source path set by the "dir" command.)
296
a64d2530 297 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
8b1b3228 298 "set source path"
1279f4ff 299
8b1b3228
AC
300 gdb_test "cd /tmp" "Working directory /tmp." \
301 "cd away from process working directory"
1279f4ff
AC
302
303 # Explicitly flush out any knowledge of the previous attachment.
1279f4ff 304
8b1b3228 305 set test "before attach3, flush symbols"
6c95b8df 306 gdb_test_multiple "symbol-file" "$test" {
8b1b3228
AC
307 -re "Discard symbol table from.*y or n. $" {
308 gdb_test "y" "No symbol file now." \
309 "$test"
1279f4ff 310 }
8b1b3228
AC
311 -re "No symbol file now.*$gdb_prompt $" {
312 pass "$test"
1279f4ff
AC
313 }
314 }
315
8b1b3228
AC
316 gdb_test "exec" "No executable file now." \
317 "before attach3, flush exec"
318
319 gdb_test "attach $testpid" \
320 "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*" \
321 "attach when process' a.out not in cwd"
322
323 set test "after attach3, exit"
dfb88a23
MS
324 gdb_test "kill" \
325 "" \
326 "$test" \
327 "Kill the program being debugged.*y or n. $" \
328 "y"
1279f4ff
AC
329
330 # Another "don't leave a process around"
331 remote_exec build "kill -9 ${testpid}"
74cf1395
JM
332}
333
334proc do_call_attach_tests {} {
1279f4ff
AC
335 global gdb_prompt
336 global binfile2
337
338 # Start the program running and then wait for a bit, to be sure
339 # that it can be attached to.
74cf1395 340
1279f4ff
AC
341 set testpid [eval exec $binfile2 &]
342 exec sleep 2
343 if { [istarget "*-*-cygwin*"] } {
344 # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
345 # different due to the way fork/exec works.
346 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
347 }
348
349 # Attach
350
f6978de9 351 gdb_test "file $binfile2" ".*" "force switch to gdb64, if necessary"
8b1b3228
AC
352 set test "attach call"
353 gdb_test_multiple "attach $testpid" "$test" {
354 -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
355 fail "$test (read register error)"
1279f4ff
AC
356 }
357 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
8b1b3228 358 pass "$test"
1279f4ff
AC
359 }
360 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
8b1b3228 361 pass "$test"
1279f4ff
AC
362 }
363 }
364
365 # See if other registers are problems
366
8b1b3228
AC
367 set test "info other register"
368 gdb_test_multiple "i r r3" "$test" {
369 -re "warning: reading register.*$gdb_prompt $" {
370 fail "$test"
1279f4ff 371 }
8b1b3228
AC
372 -re "r3.*$gdb_prompt $" {
373 pass "$test"
1279f4ff 374 }
1279f4ff 375 }
74cf1395 376
1279f4ff
AC
377 # Get rid of the process
378
8b1b3228 379 gdb_test "p should_exit = 1"
fda326dd 380 gdb_continue_to_end
1279f4ff
AC
381
382 # Be paranoid
383
384 remote_exec build "kill -9 ${testpid}"
74cf1395
JM
385}
386
387
388# Start with a fresh gdb
1279f4ff 389
74cf1395
JM
390gdb_exit
391gdb_start
392gdb_reinitialize_dir $srcdir/$subdir
393gdb_load ${binfile}
394
395# This is a test of gdb's ability to attach to a running process.
1279f4ff 396
74cf1395
JM
397do_attach_tests
398
399# Test attaching when the target is inside a system call
1279f4ff 400
74cf1395
JM
401gdb_exit
402gdb_start
403
74cf1395
JM
404gdb_reinitialize_dir $srcdir/$subdir
405do_call_attach_tests
406
74cf1395 407return 0
This page took 1.369828 seconds and 4 git commands to generate.