This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / testsuite / gdb.base / attach.exp
1 #   Copyright (C) 1997 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 # On HP-UX 11.0, this test is causing a process running
21 # the program "attach" to be left around spinning.
22 # Until we figure out why, I am commenting out the test
23 # to avoid polluting tiamat (our 11.0 nightly test machine)
24 # with these processes. RT
25 #
26 # Setting the magic bit in the target app should work.
27 # I added a "kill", and also a test for the R3 register
28 #  warning.           JB
29
30 if $tracelevel then {
31         strace $tracelevel
32         }
33
34 set prms_id 0
35 set bug_id 0
36
37
38 if { ![istarget "hppa*-*-hpux*"] } {
39     #setup_xfail "*-*-*"
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 # Because we can't attach over nfs, copy binfile to /tmp/${binfile}.${pid}
67 # and replace binfile with a symbolic link
68
69   set pid [pid]
70   exec /bin/cp -f ${binfile} /tmp/attach1.${pid}
71   exec rm -f ${binfile}
72   set binfile /tmp/attach1.${pid}
73 #  exec ln -s /tmp/attach1.${pid} ${binfile}
74
75 # Build the in-system-call test
76
77 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
78      gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
79 }
80
81 # Because we can't attach over nfs, copy binfile2 to /tmp/${binfile2}.${pid}
82 # and replace binfile2 with a symbolic link
83
84   set pid [pid]
85   exec cp -f ${binfile2} /tmp/attach2.${pid} 
86   exec rm -f ${binfile2}
87   set binfile2 /tmp/attach2.${pid}
88 #  exec ln -s /tmp/attach2.${pid} ${binfile2}
89
90
91 if [get_compiler_info ${binfile}] {
92     return -1
93 }
94
95 proc do_attach_tests {} {
96    global gdb_prompt
97    global binfile
98    global srcfile
99    global testfile
100    global objdir
101    global subdir
102    global timeout
103
104    # Start the program running and then wait for a bit, to be sure
105    # that it can be attached to.
106    #
107    set testpid [eval exec $binfile &]
108    exec sleep 2
109
110    # Verify that we cannot attach to nonsense.
111    #
112    send_gdb "attach abc\n"
113    gdb_expect {
114       -re ".*Illegal process-id: abc.*$gdb_prompt $"\
115                       {pass "attach to nonsense is prohibited"}
116       -re "Attaching to.*$gdb_prompt $"\
117                       {fail "attach to nonsense is prohibited (bogus pid allowed)"}
118       -re "$gdb_prompt $" {fail "attach to nonsense is prohibited"}
119       timeout         {fail "(timeout) attach to nonsense is prohibited"}
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.  (I don't
124    # believe any process is ever assigned #0, at least on HPUX.)
125    #
126    send_gdb "attach 0\n"
127    gdb_expect {
128       # This reponse is expected on HP-UX 10.20 (i.e., ptrace-based).
129       -re "Attaching to.*, process 0.*No such process.*$gdb_prompt $"\
130                       {pass "attach to nonexistent process is prohibited"}
131       # This response is expected on HP-UX 10.30 & 11.0 (i.e., ttrace-based).
132       -re "Attaching to.*, process 0 failed.*Hint.*$gdb_prompt $"\
133                       {pass "attach to nonexistent process is prohibited"}
134       -re "$gdb_prompt $" {fail "attach to nonexistent process is prohibited"}
135       timeout         {fail "(timeout) attach to nonexistent process is prohibited"}
136    }
137
138    # Verify that we can attach to the process by first giving its
139    # executable name via the file command, and using attach with
140    # the process ID.
141    #
142    # (Actually, the test system appears to do this automatically
143    # for us.  So, we must also be prepared to be asked if we want
144    # to discard an existing set of symbols.)
145    #
146    send_gdb "file $binfile\n"
147    gdb_expect {
148       -re "Load new symbol table from.*y or n.*$" {
149          send_gdb "y\n"
150          gdb_expect {
151             -re "Reading symbols from $binfile\.\.\.*done.*$gdb_prompt $"\
152                             {pass "(re)set file, before attach1"}
153             -re "$gdb_prompt $" {fail "(re)set file, before attach1"}
154             timeout         {fail "(timeout) (re)set file, before attach1"}
155          }
156       }
157       -re "Reading symbols from $binfile\.\.\.*done.*$gdb_prompt $"\
158                       {pass "set file, before attach1"}
159       -re "$gdb_prompt $" {fail "set file, before attach1"}
160       timeout         {fail "(timeout) set file, before attach1"}
161    }
162
163    send_gdb "attach $testpid\n"
164    gdb_expect {
165       -re "Attaching to program.*$binfile, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $"\
166                       {pass "attach1, after setting file"}
167       -re "$gdb_prompt $" {fail "attach1, after setting file"}
168       timeout         {fail "(timeout) attach1, after setting file"}
169    }
170
171    # Verify that we can "see" the variable "should_exit" in the
172    # program, and that it is zero.
173    #
174    send_gdb "print should_exit\n"
175    gdb_expect {
176       -re ".* = 0.*$gdb_prompt $"\
177                       {pass "after attach1, print should_exit"}
178       -re "$gdb_prompt $" {fail "after attach1, print should_exit"}
179       timeout         {fail "(timeout) after attach1, print should_exit"}
180    }
181
182    # Detach the process.
183    #
184    send_gdb "detach\n"
185    gdb_expect {
186       -re "Detaching from program: .*$binfile.*$gdb_prompt $"\
187                       {pass "attach1 detach"}
188       -re "$gdb_prompt $" {fail "attach1 detach"}
189       timeout         {fail "(timeout) attach1 detach"}
190    }
191
192    # Wait a bit for gdb to finish detaching
193    #
194    exec sleep 5
195
196    # Purge the symbols from gdb's brain.  (We want to be certain
197    # the next attach, which won't be preceded by a "file" command,
198    # is really getting the executable file without our help.)
199    #
200    set old_timeout $timeout
201    set timeout 15 
202    send_gdb "file\n"
203    gdb_expect {
204       -re ".*gdb internal error.*$" { 
205           fail "Internal error, prob. Memory corruption" 
206       }
207       -re "No executable file now.*Discard symbol table.*y or n.*$" {
208          send_gdb "y\n"
209          gdb_expect {
210             -re "No symbol file now.*$gdb_prompt $"\
211                             {pass "attach1, purging symbols after detach"}
212             -re "$gdb_prompt $" {fail "attach1, purging symbols after detach"}
213             timeout         {fail "(timeout) attach1, purging symbols after detach"}
214          }
215       }
216       -re "$gdb_prompt $" {fail "attach1, purging file after detach"}
217       timeout         {
218           fail "(timeout) attach1, purging file after detach"
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    send_gdb "attach $testpid\n"
227    gdb_expect {
228       -re "Attaching to process $testpid.*Reading symbols from $binfile.*main.*at .*$gdb_prompt $"\
229                       {pass "attach2"}
230       -re "$gdb_prompt $" {fail "attach2"}
231       timeout         {fail "(timeout) attach2"}
232    }
233
234    # Verify that we can modify the variable "should_exit" in the
235    # program.
236    #
237    send_gdb "set should_exit=1\n"
238    gdb_expect {
239       -re "$gdb_prompt $" {pass "after attach2, set should_exit"}
240       timeout         {fail "(timeout) after attach2, set should_exit"}
241    }
242
243    # Verify that the modification really happened.
244    #
245    send_gdb "tbreak 19\n"
246    gdb_expect {
247       -re "Breakpoint .*at.*$srcfile, line 19.*$gdb_prompt $"\
248                       {pass "after attach2, set tbreak postloop"}
249       -re "$gdb_prompt $" {fail "after attach2, set tbreak postloop"}
250       timeout         {fail "(timeout) after attach2, set tbreak postloop"}
251    }
252    send_gdb "continue\n"
253    gdb_expect {
254       -re "main.*at.*$srcfile:19.*$gdb_prompt $"\
255                       {pass "after attach2, reach tbreak postloop"}
256       -re "$gdb_prompt $" {fail "after attach2, reach tbreak postloop"}
257       timeout         {fail "(timeout) after attach2, reach tbreak postloop"}
258    }
259
260    # Allow the test process to exit, to cleanup after ourselves.
261    #
262    send_gdb "continue\n"
263    gdb_expect {
264       -re "Program exited normally.*$gdb_prompt $"\
265                       {pass "after attach2, exit"}
266       -re "$gdb_prompt $" {fail "after attach2, exit"}
267       timeout         {fail "(timeout) after attach2, exit"}
268    }
269
270    # Make sure we don't leave a process around to confuse
271    # the next test run (and prevent the compile by keeping
272    # the text file busy), in case the "set should_exit" didn't
273    # work.
274    #
275    remote_exec build "kill -9 ${testpid}"
276    # Start the program running and then wait for a bit, to be sure
277    # that it can be attached to.
278    #
279    set testpid [eval exec $binfile &]
280    exec sleep 2
281
282    # Verify that we can attach to the process, and find its a.out
283    # when we're cd'd to some directory that doesn't contain the
284    # a.out.  (We use the source path set by the "dir" command.)
285    #
286    send_gdb "dir ${objdir}/${subdir}\n"
287    gdb_expect {
288       -re ".*Source directories searched: .*$gdb_prompt $"\
289                       {pass "set source path"}
290       -re "$gdb_prompt $" {fail "set source path"}
291       timeout         {fail "(timeout) set source path"}
292    }
293
294    send_gdb "cd /tmp\n"
295    gdb_expect {
296       -re ".*Working directory /tmp.*$gdb_prompt $"\
297                       {pass "cd away from process' a.out"}
298       -re "$gdb_prompt $" {fail "cd away from process' a.out"}
299       timeout         {fail "(timeout) cd away from process' a.out"}
300    }
301
302    # Explicitly flush out any knowledge of the previous attachment.
303    send_gdb "symbol\n"
304    gdb_expect {
305       -re ".*Discard symbol table from.*y or n. $"\
306                       {send_gdb "y\n"
307                        gdb_expect {
308                           -re ".*No symbol file now.*$gdb_prompt $"\
309                                           {pass "before attach3, flush symbols"}
310                           -re "$gdb_prompt $" {fail "before attach3, flush symbols"}
311                           timeout         {fail "(timeout) before attach3, flush symbols"}
312                        }
313                       }
314       -re ".*No symbol file now.*$gdb_prompt $"\
315                       {pass "before attach3, flush symbols"}
316       -re "$gdb_prompt $" {fail "before attach3, flush symbols"}
317       timeout         {fail "(timeout) before attach3, flush symbols"}
318    }
319    send_gdb "exec\n"
320    gdb_expect {
321       -re ".*No executable file now.*$gdb_prompt $"\
322                       {pass "before attach3, flush exec"}
323       -re "$gdb_prompt $" {fail "before attach3, flush exec"}
324       timeout         {fail "(timeout) before attach3, flush exec"}
325    }
326
327    send_gdb "attach $testpid\n"
328    gdb_expect {
329       -re "Attaching to process $testpid.*Reading symbols from $binfile.*main.*at .*$gdb_prompt $"\
330                       {pass "attach when process' a.out not in cwd"}
331       -re "$gdb_prompt $" {fail "attach when process' a.out not in cwd"}
332       timeout         {fail "(timeout) attach when process' a.out not in cwd"}
333    }
334
335    send_gdb "kill\n"
336    gdb_expect {
337       -re ".*Kill the program being debugged.*y or n. $"\
338                       {send_gdb "y\n"
339                        gdb_expect {
340                           -re "$gdb_prompt $" {pass "after attach3, exit"}
341                           timeout {fail "(timeout) after attach3, exit"}
342                        }
343                       }
344       -re "$gdb_prompt $" {fail "after attach3, exit"}
345       timeout         {fail "(timeout) after attach3, exit"}
346    }
347 }
348
349 proc do_call_attach_tests {} {
350    global gdb_prompt
351    global binfile2
352
353    # Start the program running and then wait for a bit, to be sure
354    # that it can be attached to.
355    #
356    set testpid [eval exec $binfile2 &]
357    exec sleep 2
358
359    # Attach
360    #
361    gdb_test "file $binfile2" ".*" "force switch to gdb64, if necessary"
362    send_gdb "attach $testpid\n"
363    gdb_expect {
364       -re ".*warning: reading register.*I.*O error.*$gdb_prompt $" {
365          fail "attach call, read register 3 error"
366      }
367      -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
368          pass "attach call"
369      }
370       -re "$gdb_prompt $" {fail "attach call"}
371       timeout         {fail "(timeout) attach call"}
372    }
373
374    # See if other registers are problems
375    #
376    send_gdb "i r r3\n"
377    gdb_expect {
378        -re ".*warning: reading register.*$gdb_prompt $" {
379            pass "CHFts23490: known bug"
380        }
381        -re ".*r3.*$gdb_prompt $" {
382            pass "Bug fixed, Yayyy!"
383        }
384        timeout { fail "timeout on info reg" }
385    }
386
387    # Get rid of the process
388    #
389    gdb_test "p should_exit = 1" ".*" ""
390    gdb_test "c" ".*Program exited normally.*" ""
391    
392    # Be paranoid
393    #
394     remote_exec build "kill -9 ${testpid}"
395
396 }
397
398
399 # Start with a fresh gdb
400 #
401 gdb_exit
402 gdb_start
403 gdb_reinitialize_dir $srcdir/$subdir
404 gdb_load ${binfile}
405
406 # This is a test of gdb's ability to attach to a running process.
407 #
408 do_attach_tests
409
410 # Test attaching when the target is inside a system call
411 #
412 gdb_exit
413 gdb_start
414
415 # this seems not necessary. - guo
416 #
417 # # Since we have moved the executable to /tmp, it will be hard for gdb
418 # # to find the object file/executable to read the symbols.  This is
419 # # a known limitation.  We try and get the name of the executable the
420 # # process is running from a variety of methods, but none is foolproof.
421 # # Using "dir" will get us the symbols.
422
423 # gdb_test "dir ./gdb.base" ".*" "set up directory before attach"
424 gdb_reinitialize_dir $srcdir/$subdir
425 do_call_attach_tests
426
427 # Until "set follow-fork-mode" and "catch fork" are implemented on
428 # other targets...
429 #
430 if ![istarget "hppa*-hp-hpux*"] then {
431    setup_xfail "*-*-*"
432 }
433
434 # Cleanup the files placed in /tmp and the symlinks
435   remote_exec build "rm -f ${binfile} ${binfile2} /tmp/attach1.${pid} /tmp/attach2.${pid}"
436
437 return 0