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