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