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