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