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