]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/attach.exp
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / attach.exp
CommitLineData
6aba47ca 1# Copyright 1997, 1999, 2002, 2003, 2004, 2007 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
5# the Free Software Foundation; either version 2 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, write to the Free Software
15# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
74cf1395 17if $tracelevel then {
1279f4ff
AC
18 strace $tracelevel
19}
74cf1395
JM
20
21set prms_id 0
22set bug_id 0
23
33c3e192
FF
24# On HP-UX 11.0, this test is causing a process running the program
25# "attach" to be left around spinning. Until we figure out why, I am
26# commenting out the test to avoid polluting tiamat (our 11.0 nightly
27# test machine) with these processes. RT
28#
29# Setting the magic bit in the target app should work. I added a
30# "kill", and also a test for the R3 register warning. JB
31if { [istarget "hppa*-*-hpux*"] } {
32 return 0
33}
74cf1395 34
c1d88655
UW
35# On SPU, this test currently fails because "sleep" is not supported.
36if { [istarget "spu*-*-*"] } {
37 return 0
38}
39
33c3e192 40# are we on a target board
2d1676a0 41if [is_remote target] then {
74cf1395
JM
42 return 0
43}
44
45set testfile "attach"
46set srcfile ${testfile}.c
47set srcfile2 ${testfile}2.c
48set binfile ${objdir}/${subdir}/${testfile}
49set binfile2 ${objdir}/${subdir}/${testfile}2
e0f353ce 50set escapedbinfile [string_to_regexp ${objdir}/${subdir}/${testfile}]
74cf1395
JM
51
52#execute_anywhere "rm -f ${binfile} ${binfile2}"
53remote_exec build "rm -f ${binfile} ${binfile2}"
54# For debugging this test
55#
56#log_user 1
57
74cf1395
JM
58# build the first test case
59#
60if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
b60f0898
JB
61 untested attach.exp
62 return -1
74cf1395
JM
63}
64
74cf1395
JM
65# Build the in-system-call test
66
67if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
b60f0898
JB
68 untested attach.exp
69 return -1
74cf1395
JM
70}
71
74cf1395
JM
72if [get_compiler_info ${binfile}] {
73 return -1
74}
75
76proc do_attach_tests {} {
1279f4ff
AC
77 global gdb_prompt
78 global binfile
79 global escapedbinfile
80 global srcfile
81 global testfile
82 global objdir
83 global subdir
84 global timeout
85
86 # Start the program running and then wait for a bit, to be sure
87 # that it can be attached to.
88
89 set testpid [eval exec $binfile &]
90 exec sleep 2
91 if { [istarget "*-*-cygwin*"] } {
92 # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
93 # different due to the way fork/exec works.
94 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
95 }
96
97 # Verify that we cannot attach to nonsense.
98
8b1b3228
AC
99 set test "attach to nonsense is prohibited"
100 gdb_test_multiple "attach abc" "$test" {
101 -re "Illegal process-id: abc.*$gdb_prompt $" {
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
AC
152 }
153 }
154
155 # Verify that we can attach to the process by first giving its
156 # executable name via the file command, and using attach with the
157 # process ID.
158
159 # (Actually, the test system appears to do this automatically for
160 # us. So, we must also be prepared to be asked if we want to
161 # discard an existing set of symbols.)
162
8b1b3228
AC
163 set test "set file, before attach1"
164 gdb_test_multiple "file $binfile" "$test" {
165 -re "Load new symbol table from.*y or n. $" {
166 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
167 "$test (re-read)"
1279f4ff
AC
168 }
169 -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
8b1b3228 170 pass "$test"
1279f4ff
AC
171 }
172 }
173
8b1b3228
AC
174 set test "attach1, after setting file"
175 gdb_test_multiple "attach $testpid" "$test" {
1279f4ff 176 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
8b1b3228 177 pass "$test"
1279f4ff
AC
178 }
179 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
180 # Response expected on Cygwin
8b1b3228 181 pass "$test"
1279f4ff
AC
182 }
183 }
184
185 # Verify that we can "see" the variable "should_exit" in the
186 # program, and that it is zero.
187
8b1b3228 188 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
1279f4ff
AC
189
190 # Detach the process.
191
8b1b3228
AC
192 gdb_test "detach" \
193 "Detaching from program: .*$escapedbinfile, process $testpid" \
194 "attach1 detach"
1279f4ff
AC
195
196 # Wait a bit for gdb to finish detaching
197
198 exec sleep 5
199
200 # Purge the symbols from gdb's brain. (We want to be certain the
201 # next attach, which won't be preceded by a "file" command, is
202 # really getting the executable file without our help.)
203
204 set old_timeout $timeout
205 set timeout 15
8b1b3228
AC
206 set test "attach1, purging symbols after detach"
207 gdb_test_multiple "file" "$test" {
208 -re "No executable file now.*Discard symbol table.*y or n. $" {
209 gdb_test "y" "No symbol file now." "$test"
1279f4ff
AC
210 }
211 }
212 set timeout $old_timeout
213
214 # Verify that we can attach to the process just by giving the
215 # process ID.
216
8b1b3228
AC
217 set test "set file, before attach2"
218 gdb_test_multiple "attach $testpid" "$test" {
219 -re "Attaching to process $testpid.*Load new symbol table from \"$escapedbinfile\.exe\".*y or n. $" {
1279f4ff
AC
220 # On Cygwin, the DLL's symbol tables are loaded prior to the
221 # executable's symbol table. This in turn always results in
222 # asking the user for actually loading the symbol table of the
223 # executable.
8b1b3228
AC
224 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
225 "$test (reset file)"
1279f4ff
AC
226 }
227 -re "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*$gdb_prompt $" {
8b1b3228 228 pass "$test"
1279f4ff
AC
229 }
230 }
231
232 # Verify that we can modify the variable "should_exit" in the
233 # program.
234
8b1b3228 235 gdb_test "set should_exit=1" "" "after attach2, set should_exit"
1279f4ff
AC
236
237 # Verify that the modification really happened.
238
239 send_gdb "tbreak 19\n"
240 gdb_expect {
241 -re "Breakpoint .*at.*$srcfile, line 19.*$gdb_prompt $" {
242 pass "after attach2, set tbreak postloop"
243 }
244 -re "$gdb_prompt $" {
245 fail "after attach2, set tbreak postloop"
246 }
247 timeout {
248 fail "(timeout) after attach2, set tbreak postloop"
249 }
250 }
251 send_gdb "continue\n"
252 gdb_expect {
253 -re "main.*at.*$srcfile:19.*$gdb_prompt $" {
254 pass "after attach2, reach tbreak postloop"
255 }
256 -re "$gdb_prompt $" {
257 fail "after attach2, reach tbreak postloop"
258 }
259 timeout {
260 fail "(timeout) after attach2, reach tbreak postloop"
261 }
262 }
263
264 # Allow the test process to exit, to cleanup after ourselves.
265
8b1b3228 266 gdb_test "continue" "Program exited normally." "after attach2, exit"
1279f4ff
AC
267
268 # Make sure we don't leave a process around to confuse
269 # the next test run (and prevent the compile by keeping
270 # the text file busy), in case the "set should_exit" didn't
271 # work.
272
273 remote_exec build "kill -9 ${testpid}"
8b1b3228 274
1279f4ff
AC
275 # Start the program running and then wait for a bit, to be sure
276 # that it can be attached to.
277
278 set testpid [eval exec $binfile &]
279 exec sleep 2
280 if { [istarget "*-*-cygwin*"] } {
281 # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
282 # different due to the way fork/exec works.
283 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
284 }
285
286 # Verify that we can attach to the process, and find its a.out
287 # when we're cd'd to some directory that doesn't contain the
288 # a.out. (We use the source path set by the "dir" command.)
289
8b1b3228
AC
290 gdb_test "dir ${objdir}/${subdir}" "Source directories searched: .*" \
291 "set source path"
1279f4ff 292
8b1b3228
AC
293 gdb_test "cd /tmp" "Working directory /tmp." \
294 "cd away from process working directory"
1279f4ff
AC
295
296 # Explicitly flush out any knowledge of the previous attachment.
1279f4ff 297
8b1b3228
AC
298 set test "before attach3, flush symbols"
299 gdb_test_multiple "symbol" "$test" {
300 -re "Discard symbol table from.*y or n. $" {
301 gdb_test "y" "No symbol file now." \
302 "$test"
1279f4ff 303 }
8b1b3228
AC
304 -re "No symbol file now.*$gdb_prompt $" {
305 pass "$test"
1279f4ff
AC
306 }
307 }
308
8b1b3228
AC
309 gdb_test "exec" "No executable file now." \
310 "before attach3, flush exec"
311
312 gdb_test "attach $testpid" \
313 "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*" \
314 "attach when process' a.out not in cwd"
315
316 set test "after attach3, exit"
317 gdb_test_multiple "kill" "$test" {
318 -re "Kill the program being debugged.*y or n. $" {
319 gdb_test "y" "" "$test"
1279f4ff
AC
320 }
321 }
322
323 # Another "don't leave a process around"
324 remote_exec build "kill -9 ${testpid}"
74cf1395
JM
325}
326
327proc do_call_attach_tests {} {
1279f4ff
AC
328 global gdb_prompt
329 global binfile2
330
331 # Start the program running and then wait for a bit, to be sure
332 # that it can be attached to.
74cf1395 333
1279f4ff
AC
334 set testpid [eval exec $binfile2 &]
335 exec sleep 2
336 if { [istarget "*-*-cygwin*"] } {
337 # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
338 # different due to the way fork/exec works.
339 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
340 }
341
342 # Attach
343
8b1b3228
AC
344 gdb_test "file $binfile2" "" "force switch to gdb64, if necessary"
345 set test "attach call"
346 gdb_test_multiple "attach $testpid" "$test" {
347 -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
348 fail "$test (read register error)"
1279f4ff
AC
349 }
350 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
8b1b3228 351 pass "$test"
1279f4ff
AC
352 }
353 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
8b1b3228 354 pass "$test"
1279f4ff
AC
355 }
356 }
357
358 # See if other registers are problems
359
8b1b3228
AC
360 set test "info other register"
361 gdb_test_multiple "i r r3" "$test" {
362 -re "warning: reading register.*$gdb_prompt $" {
363 fail "$test"
1279f4ff 364 }
8b1b3228
AC
365 -re "r3.*$gdb_prompt $" {
366 pass "$test"
1279f4ff 367 }
1279f4ff 368 }
74cf1395 369
1279f4ff
AC
370 # Get rid of the process
371
8b1b3228
AC
372 gdb_test "p should_exit = 1"
373 gdb_test "c" "Program exited normally."
1279f4ff
AC
374
375 # Be paranoid
376
377 remote_exec build "kill -9 ${testpid}"
74cf1395
JM
378}
379
380
381# Start with a fresh gdb
1279f4ff 382
74cf1395
JM
383gdb_exit
384gdb_start
385gdb_reinitialize_dir $srcdir/$subdir
386gdb_load ${binfile}
387
388# This is a test of gdb's ability to attach to a running process.
1279f4ff 389
74cf1395
JM
390do_attach_tests
391
392# Test attaching when the target is inside a system call
1279f4ff 393
74cf1395
JM
394gdb_exit
395gdb_start
396
74cf1395
JM
397gdb_reinitialize_dir $srcdir/$subdir
398do_call_attach_tests
399
74cf1395 400return 0