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