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