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