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