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