OSDN Git Service

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