OSDN Git Service

Updated copyright notices for most files.
[pf3gnuchains/pf3gnuchains3x.git] / gdb / testsuite / gdb.base / attach.exp
1 # Copyright 1997, 1999, 2002, 2003, 2004, 2007, 2008
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 # On SPU, this test currently fails because "sleep" is not supported.
36 if { [istarget "spu*-*-*"] } {
37         return 0
38 }
39
40 # are we on a target board
41 if [is_remote target] then {
42     return 0
43 }
44
45 set testfile "attach"
46 set srcfile  ${testfile}.c
47 set srcfile2 ${testfile}2.c
48 set binfile  ${objdir}/${subdir}/${testfile}
49 set binfile2 ${objdir}/${subdir}/${testfile}2
50 set escapedbinfile  [string_to_regexp ${objdir}/${subdir}/${testfile}]
51
52 #execute_anywhere "rm -f ${binfile} ${binfile2}"
53 remote_exec build "rm -f ${binfile} ${binfile2}"
54 # For debugging this test
55 #
56 #log_user 1
57
58 # build the first test case
59 #
60 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
61     untested attach.exp
62     return -1
63 }
64
65 # Build the in-system-call test
66
67 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
68     untested attach.exp
69     return -1
70 }
71
72 if [get_compiler_info ${binfile}] {
73     return -1
74 }
75
76 proc do_attach_tests {} {
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
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"
103         }
104         -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
105             # Response expected from /proc-based systems.
106             pass "$test" 
107         }
108         -re "Can't attach to process..*$gdb_prompt $" {
109             # Response expected on Cygwin
110             pass "$test"
111         }
112         -re "Attaching to.*$gdb_prompt $" {
113             fail "$test (bogus pid allowed)"
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     }
129     set test "attach to nonexistent process is prohibited"
130     gdb_test_multiple "attach $boguspid" "$test" {
131         -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
132             # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
133             pass "$test"
134         }
135         -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
136             # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
137             pass "$test"
138         }
139         -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
140             pass "$test"
141         }
142         -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
143             pass "$test"
144         }
145         -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
146             # Response expected from /proc-based systems.
147             pass "$test"
148         }
149         -re "Can't attach to process..*$gdb_prompt $" {
150             # Response expected on Cygwin
151             pass "$test"
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     
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)"
168         }
169         -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
170             pass "$test"
171         }
172     }
173
174     set test "attach1, after setting file"
175     gdb_test_multiple "attach $testpid" "$test" {
176         -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
177             pass "$test"
178         }
179         -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
180             # Response expected on Cygwin
181             pass "$test"
182         }
183     }
184
185     # Verify that we can "see" the variable "should_exit" in the
186     # program, and that it is zero.
187    
188     gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
189
190     # Detach the process.
191    
192     gdb_test "detach" \
193         "Detaching from program: .*$escapedbinfile, process $testpid" \
194         "attach1 detach"
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 
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"
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    
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. $" {
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.
224             gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
225                 "$test (reset file)"
226         }
227         -re "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*$gdb_prompt $" {
228             pass "$test"
229         }
230     }
231
232     # Verify that we can modify the variable "should_exit" in the
233     # program.
234
235     gdb_test "set should_exit=1" "" "after attach2, set should_exit"
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
266     gdb_test "continue" "Program exited normally." "after attach2, exit"
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}"
274
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     
290     gdb_test "dir ${objdir}/${subdir}" "Source directories searched: .*" \
291         "set source path"
292
293     gdb_test "cd /tmp" "Working directory /tmp." \
294         "cd away from process working directory"
295
296     # Explicitly flush out any knowledge of the previous attachment.
297
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"
303         }
304         -re "No symbol file now.*$gdb_prompt $" {
305             pass "$test"
306         }
307     }
308
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"
320         }
321     }
322     
323     # Another "don't leave a process around"
324     remote_exec build "kill -9 ${testpid}"
325 }
326
327 proc do_call_attach_tests {} {
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.
333    
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    
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)"
349         }
350         -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
351             pass "$test"
352         }
353         -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
354             pass "$test"
355         }
356     }
357
358     # See if other registers are problems
359     
360     set test "info other register"
361     gdb_test_multiple "i r r3" "$test" {
362         -re "warning: reading register.*$gdb_prompt $" {
363             fail "$test"
364         }
365         -re "r3.*$gdb_prompt $" {
366             pass "$test"
367         }
368     }
369
370     # Get rid of the process
371     
372     gdb_test "p should_exit = 1"
373     gdb_test "c" "Program exited normally."
374    
375     # Be paranoid
376    
377     remote_exec build "kill -9 ${testpid}"
378 }
379
380
381 # Start with a fresh gdb
382
383 gdb_exit
384 gdb_start
385 gdb_reinitialize_dir $srcdir/$subdir
386 gdb_load ${binfile}
387
388 # This is a test of gdb's ability to attach to a running process.
389
390 do_attach_tests
391
392 # Test attaching when the target is inside a system call
393
394 gdb_exit
395 gdb_start
396
397 gdb_reinitialize_dir $srcdir/$subdir
398 do_call_attach_tests
399
400 return 0