2004-07-08 Andrew Cagney <cagney@gnu.org>
[external/binutils.git] / gdb / testsuite / gdb.base / attach.exp
1 # Copyright 1997, 1999, 2002, 2003, 2004 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 if $tracelevel then {
18     strace $tracelevel
19 }
20
21 set prms_id 0
22 set bug_id 0
23
24 # On HP-UX 11.0, this test is causing a process running the program
25 # "attach" to be left around spinning.  Until we figure out why, I am
26 # commenting out the test to avoid polluting tiamat (our 11.0 nightly
27 # test machine) with these processes. RT
28 #
29 # Setting the magic bit in the target app should work.  I added a
30 # "kill", and also a test for the R3 register warning.  JB
31 if { [istarget "hppa*-*-hpux*"] } {
32     return 0
33 }
34
35 # are we on a target board
36 if [is_remote target] then {
37     return 0
38 }
39
40 set testfile "attach"
41 set srcfile  ${testfile}.c
42 set srcfile2 ${testfile}2.c
43 set binfile  ${objdir}/${subdir}/${testfile}
44 set binfile2 ${objdir}/${subdir}/${testfile}2
45 set escapedbinfile  [string_to_regexp ${objdir}/${subdir}/${testfile}]
46 set cleanupfile ${objdir}/${subdir}/${testfile}.awk
47
48 #execute_anywhere "rm -f ${binfile} ${binfile2}"
49 remote_exec build "rm -f ${binfile} ${binfile2}"
50 # For debugging this test
51 #
52 #log_user 1
53
54 # Clean out any old files from past runs.
55 #
56 remote_exec build "${cleanupfile}"
57
58 # build the first test case
59 #
60 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
61     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
62 }
63
64 # Build the in-system-call test
65
66 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
67     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
68 }
69
70 if [get_compiler_info ${binfile}] {
71     return -1
72 }
73
74 proc do_attach_tests {} {
75     global gdb_prompt
76     global binfile
77     global escapedbinfile
78     global srcfile
79     global testfile
80     global objdir
81     global subdir
82     global timeout
83     
84     # Start the program running and then wait for a bit, to be sure
85     # that it can be attached to.
86
87     set testpid [eval exec $binfile &]
88     exec sleep 2
89     if { [istarget "*-*-cygwin*"] } {
90         # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
91         # different due to the way fork/exec works.
92         set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
93     }
94
95     # Verify that we cannot attach to nonsense.
96
97     send_gdb "attach abc\n"
98     gdb_expect {
99         -re ".*Illegal process-id: abc.*$gdb_prompt $" {
100             pass "attach to nonsense is prohibited"
101         }
102         -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
103             # Response expected from /proc-based systems.
104             pass "attach to nonsense is prohibited" 
105         }
106         -re ".*Can't attach to process..*$gdb_prompt $" {
107             # Response expected on Cygwin
108             pass "attach to nonsense is prohibited"
109         }
110         -re "Attaching to.*$gdb_prompt $" {
111             fail "attach to nonsense is prohibited (bogus pid allowed)"
112         }
113         -re "$gdb_prompt $" {
114             fail "attach to nonsense is prohibited"
115         }
116         timeout {
117             fail "(timeout) attach to nonsense is prohibited"
118         }
119     }
120
121     # Verify that we cannot attach to what appears to be a valid
122     # process ID, but is a process that doesn't exist.  Traditionally,
123     # most systems didn't have a process with ID 0, so we take that as
124     # the default.  However, there are a few exceptions.
125
126     set boguspid 0
127     if { [istarget "*-*-*bsd*"] } {
128         # In FreeBSD 5.0, PID 0 is used for "swapper".  Use -1 instead
129         # (which should have the desired effect on any version of
130         # FreeBSD, and probably other *BSD's too).
131         set boguspid -1
132     }
133     send_gdb "attach $boguspid\n"
134     gdb_expect {
135         -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
136             # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
137             pass "attach to nonexistent process is prohibited"
138         }
139         -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
140             # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
141             pass "attach to nonexistent process is prohibited"
142         }
143         -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
144             pass "attach to nonexistent process is prohibited"
145         }
146         -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
147             pass "attach to nonexistent process is prohibited"
148         }
149         -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
150             # Response expected from /proc-based systems.
151             pass "attach to nonexistent process is prohibited"
152         }
153         -re ".*Can't attach to process..*$gdb_prompt $" {
154             # Response expected on Cygwin
155             pass "attach to nonexistent process is prohibited"
156         }
157         -re "$gdb_prompt $" {
158             fail "attach to nonexistent process is prohibited"
159         }
160         timeout {
161             fail "(timeout) attach to nonexistent process is prohibited"
162         }
163     }
164     
165     # Verify that we can attach to the process by first giving its
166     # executable name via the file command, and using attach with the
167     # process ID.
168
169     # (Actually, the test system appears to do this automatically for
170     # us.  So, we must also be prepared to be asked if we want to
171     # discard an existing set of symbols.)
172     
173     send_gdb "file $binfile\n"
174     gdb_expect {
175         -re "Load new symbol table from.*y or n.*$" {
176             send_gdb "y\n"
177             gdb_expect {
178                 -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
179                     pass "(re)set file, before attach1"
180                 }
181                 -re "$gdb_prompt $" {
182                     fail "(re)set file, before attach1"
183                 }
184                 timeout {
185                     fail "(timeout) (re)set file, before attach1"
186                 }
187             }
188         }
189         -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
190             pass "set file, before attach1"
191         }
192         -re "$gdb_prompt $" {
193             fail "set file, before attach1"
194         }
195         timeout {
196             fail "(timeout) set file, before attach1"
197         }
198     }
199
200     send_gdb "attach $testpid\n"
201     gdb_expect {
202         -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
203             pass "attach1, after setting file"
204         }
205         -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
206             # Response expected on Cygwin
207             pass "attach1, after setting file"
208         }
209         -re "$gdb_prompt $" {
210             fail "attach1, after setting file"
211         }
212         timeout {
213             fail "(timeout) attach1, after setting file"
214         }
215     }
216
217     # Verify that we can "see" the variable "should_exit" in the
218     # program, and that it is zero.
219    
220     send_gdb "print should_exit\n"
221     gdb_expect {
222         -re ".* = 0.*$gdb_prompt $" {
223             pass "after attach1, print should_exit"
224         }
225         -re "$gdb_prompt $" {
226             fail "after attach1, print should_exit"
227         }
228         timeout {
229             fail "(timeout) after attach1, print should_exit"
230         }
231     }
232
233     # Detach the process.
234    
235     send_gdb "detach\n"
236     gdb_expect {
237         -re "Detaching from program: .*$escapedbinfile.*$gdb_prompt $" {
238             pass "attach1 detach"
239         }
240         -re "$gdb_prompt $" {
241             fail "attach1 detach"
242         }
243         timeout {
244             fail "(timeout) attach1 detach"
245         }
246     }
247
248     # Wait a bit for gdb to finish detaching
249     
250     exec sleep 5
251
252     # Purge the symbols from gdb's brain.  (We want to be certain the
253     # next attach, which won't be preceded by a "file" command, is
254     # really getting the executable file without our help.)
255     
256     set old_timeout $timeout
257     set timeout 15 
258     send_gdb "file\n"
259     gdb_expect {
260         -re ".*gdb internal error.*$" { 
261             fail "Internal error, prob. Memory corruption" 
262         }
263         -re "No executable file now.*Discard symbol table.*y or n.*$" {
264             send_gdb "y\n"
265             gdb_expect {
266                 -re "No symbol file now.*$gdb_prompt $" {
267                     pass "attach1, purging symbols after detach"
268                 }
269                 -re "$gdb_prompt $" {
270                     fail "attach1, purging symbols after detach"
271                 }
272                 timeout {
273                     fail "(timeout) attach1, purging symbols after detach"
274                 }
275             }
276         }
277         -re "$gdb_prompt $" {
278             fail "attach1, purging file after detach"
279         }
280         timeout {
281             fail "(timeout) attach1, purging file after detach"
282         }
283     }
284     set timeout $old_timeout
285
286     # Verify that we can attach to the process just by giving the
287     # process ID.
288    
289     send_gdb "attach $testpid\n"
290     gdb_expect {
291         -re "Attaching to process $testpid.*Load new symbol table from \"$escapedbinfile\.exe\".*y or n.*$" {
292             # On Cygwin, the DLL's symbol tables are loaded prior to the
293             # executable's symbol table.  This in turn always results in
294             # asking the user for actually loading the symbol table of the
295             # executable.
296             send_gdb "y\n"
297             gdb_expect {
298                 -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
299                     pass "(re)set file, before attach2"
300                 }
301                 -re "$gdb_prompt $" {
302                     fail "(re)set file, before attach2"
303                 }
304                 timeout {
305                     fail "(timeout) (re)set file, before attach2"
306                 }
307             }
308         }
309         -re "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*$gdb_prompt $" {
310             pass "attach2"
311         }
312         -re "$gdb_prompt $" {
313             fail "attach2"
314         }
315         timeout {
316             fail "(timeout) attach2"
317         }
318     }
319
320     # Verify that we can modify the variable "should_exit" in the
321     # program.
322
323     send_gdb "set should_exit=1\n"
324     gdb_expect {
325         -re "$gdb_prompt $" {
326             pass "after attach2, set should_exit"
327         }
328         timeout {
329             fail "(timeout) after attach2, set should_exit"
330         }
331     }
332
333     # Verify that the modification really happened.
334
335     send_gdb "tbreak 19\n"
336     gdb_expect {
337         -re "Breakpoint .*at.*$srcfile, line 19.*$gdb_prompt $" {
338             pass "after attach2, set tbreak postloop"
339         }
340         -re "$gdb_prompt $" {
341             fail "after attach2, set tbreak postloop"
342         }
343         timeout {
344             fail "(timeout) after attach2, set tbreak postloop"
345         }
346     }
347     send_gdb "continue\n"
348     gdb_expect {
349         -re "main.*at.*$srcfile:19.*$gdb_prompt $" {
350             pass "after attach2, reach tbreak postloop"
351         }
352         -re "$gdb_prompt $" {
353             fail "after attach2, reach tbreak postloop"
354         }
355         timeout {
356             fail "(timeout) after attach2, reach tbreak postloop"
357         }
358     }
359
360     # Allow the test process to exit, to cleanup after ourselves.
361
362     send_gdb "continue\n"
363     gdb_expect {
364         -re "Program exited normally.*$gdb_prompt $" {
365             pass "after attach2, exit"
366         }
367         -re "$gdb_prompt $" {
368             fail "after attach2, exit"
369         }
370         timeout {
371             fail "(timeout) after attach2, exit"
372         }
373     }
374
375     # Make sure we don't leave a process around to confuse
376     # the next test run (and prevent the compile by keeping
377     # the text file busy), in case the "set should_exit" didn't
378     # work.
379    
380     remote_exec build "kill -9 ${testpid}"
381     # Start the program running and then wait for a bit, to be sure
382     # that it can be attached to.
383    
384     set testpid [eval exec $binfile &]
385     exec sleep 2
386     if { [istarget "*-*-cygwin*"] } {
387         # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
388         # different due to the way fork/exec works.
389         set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
390     }
391
392     # Verify that we can attach to the process, and find its a.out
393     # when we're cd'd to some directory that doesn't contain the
394     # a.out.  (We use the source path set by the "dir" command.)
395     
396     send_gdb "dir ${objdir}/${subdir}\n"
397     gdb_expect {
398         -re ".*Source directories searched: .*$gdb_prompt $" {
399             pass "set source path"
400         }
401         -re "$gdb_prompt $" {
402             fail "set source path"
403         }
404         timeout {
405             fail "(timeout) set source path"
406         }
407     }
408
409     send_gdb "cd /tmp\n"
410     gdb_expect {
411         -re ".*Working directory /tmp.*$gdb_prompt $" {
412             pass "cd away from process' a.out"
413         }
414         -re "$gdb_prompt $" {
415             fail "cd away from process' a.out"
416         }
417         timeout {
418             fail "(timeout) cd away from process' a.out"
419         }
420     }
421
422     # Explicitly flush out any knowledge of the previous attachment.
423     send_gdb "symbol\n"
424     gdb_expect {
425         -re ".*Discard symbol table from.*y or n. $" {
426             send_gdb "y\n"
427             gdb_expect {
428                 -re ".*No symbol file now.*$gdb_prompt $" {
429                     pass "before attach3, flush symbols"
430                 }
431                 -re "$gdb_prompt $" {
432                     fail "before attach3, flush symbols"
433                 }
434                 timeout {
435                     fail "(timeout) before attach3, flush symbols"
436                 }
437             }
438         }
439         -re ".*No symbol file now.*$gdb_prompt $" {
440             pass "before attach3, flush symbols"
441         }
442         -re "$gdb_prompt $" {
443             fail "before attach3, flush symbols"
444         }
445         timeout {
446             fail "(timeout) before attach3, flush symbols"
447         }
448     }
449     send_gdb "exec\n"
450     gdb_expect {
451         -re ".*No executable file now.*$gdb_prompt $" {
452             pass "before attach3, flush exec"
453         }
454         -re "$gdb_prompt $" {
455             fail "before attach3, flush exec"
456         }
457         timeout {
458             fail "(timeout) before attach3, flush exec"
459         }
460     }
461
462     send_gdb "attach $testpid\n"
463     gdb_expect {
464         -re "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*$gdb_prompt $" {
465             pass "attach when process' a.out not in cwd"
466         }
467         -re "$gdb_prompt $" {
468             fail "attach when process' a.out not in cwd"
469         }
470         timeout {
471             fail "(timeout) attach when process' a.out not in cwd"
472         }
473     }
474
475     send_gdb "kill\n"
476     gdb_expect {
477         -re ".*Kill the program being debugged.*y or n. $" {
478             send_gdb "y\n"
479             gdb_expect {
480                 -re "$gdb_prompt $" {
481                     pass "after attach3, exit"
482                 }
483                 timeout {
484                     fail "(timeout) after attach3, exit"
485                 }
486             }
487         }
488         -re "$gdb_prompt $" {
489             fail "after attach3, exit"
490         }
491         timeout {
492             fail "(timeout) after attach3, exit"
493         }
494     }
495     
496     # Another "don't leave a process around"
497     remote_exec build "kill -9 ${testpid}"
498 }
499
500 proc do_call_attach_tests {} {
501     global gdb_prompt
502     global binfile2
503     
504     # Start the program running and then wait for a bit, to be sure
505     # that it can be attached to.
506    
507     set testpid [eval exec $binfile2 &]
508     exec sleep 2
509     if { [istarget "*-*-cygwin*"] } {
510         # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
511         # different due to the way fork/exec works.
512         set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
513     }
514
515     # Attach
516    
517     gdb_test "file $binfile2" ".*" "force switch to gdb64, if necessary"
518     send_gdb "attach $testpid\n"
519     gdb_expect {
520         -re ".*warning: reading register.*I.*O error.*$gdb_prompt $" {
521             fail "attach call, read register 3 error"
522         }
523         -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
524             pass "attach call"
525         }
526         -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
527             pass "attach call"
528         }
529         -re "$gdb_prompt $" {
530             fail "attach call"
531         }
532         timeout {
533             fail "(timeout) attach call"
534         }
535     }
536
537     # See if other registers are problems
538     
539     send_gdb "i r r3\n"
540     gdb_expect {
541         -re ".*warning: reading register.*$gdb_prompt $" {
542             pass "CHFts23490: known bug"
543         }
544         -re ".*r3.*$gdb_prompt $" {
545             pass "Bug fixed, Yayyy!"
546         }
547         timeout { fail "timeout on info reg" }
548     }
549
550     # Get rid of the process
551     
552     gdb_test "p should_exit = 1" ".*"
553     gdb_test "c" ".*Program exited normally.*"
554    
555     # Be paranoid
556    
557     remote_exec build "kill -9 ${testpid}"
558 }
559
560
561 # Start with a fresh gdb
562
563 gdb_exit
564 gdb_start
565 gdb_reinitialize_dir $srcdir/$subdir
566 gdb_load ${binfile}
567
568 # This is a test of gdb's ability to attach to a running process.
569
570 do_attach_tests
571
572 # Test attaching when the target is inside a system call
573
574 gdb_exit
575 gdb_start
576
577 gdb_reinitialize_dir $srcdir/$subdir
578 do_call_attach_tests
579
580 return 0