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