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