import gdb-1999-07-19 snapshot
[platform/upstream/binutils.git] / gdb / testsuite / gdb.base / selftest.exp
1 #   Copyright (C) 1988, 1990, 1991, 1992, 1994, 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 # This file was written by Rob Savoye. (rob@cygnus.com)
21
22 if $tracelevel then {
23     strace $tracelevel
24 }
25
26 set prms_id 0
27 set bug_id 0
28
29 # are we on a target board
30 if [is_remote target] {
31     return
32 }
33
34 if [istarget "m68k*-*-hpux*"] then {
35     # The top-level makefile passes CFLAGS= (no -g) for hp300.  This probably
36     # should be fixed (it is only needed for gcc bootstrapping, not gdb),
37     # but until then.....
38     setup_xfail "*-*-*"
39     fail "cannot test self if compiled without debug info"
40     return -1
41 }
42
43 # Not all of the lines of code near the start of main are executed for
44 # every machine.  Also, optimization may reorder some of the lines.
45 # So all we do is try to step or next over everything until we get
46 # to a line that we know is always executed.
47
48 proc do_steps_and_nexts {} {
49     global gdb_prompt
50     global srcdir
51  
52     gdb_reinitialize_dir $srcdir/..
53
54     for {set count 0} {$count < 20} {incr count} {
55         send_gdb "list\n"
56         gdb_expect {
57             -re ".*symarg = NULL.*$gdb_prompt $" {
58                 set description "step over symarg initialization"
59                 set command "step"
60             }
61             -re ".*execarg = NULL.*$gdb_prompt $" {
62                 set description "step over execarg initialization"
63                 set command "step"
64             }
65             -re ".*corearg = NULL.*$gdb_prompt $" {
66                 set description "step over corearg initialization"
67                 set command "step"
68             }
69             -re ".*cdarg = NULL.*$gdb_prompt $" {
70                 set description "step over cdarg initialization"
71                 set command "step"
72             }
73             -re ".*ttyarg = NULL.*$gdb_prompt $" {
74                 set description "step over ttyarg initialization"
75                 set command "step"
76             }
77             -re ".*interparg = NULL.*$gdb_prompt $" {
78                 set description "step over interparg initialization"
79                 set command "step"
80             }
81             -re ".*time_at_startup = get_run_time.*$gdb_prompt $" {
82                 set description "next over get_run_time and everything it calls"
83                 set command "next"
84             }
85             -re ".*START_PROGRESS.*$gdb_prompt $" {
86                 set description "next over START_PROGRESS and everything it calls"
87                 set command "next"
88             }
89             -re ".*mac_init.*$gdb_prompt $" {
90                 set description "next over mac_init and everything it calls"
91                 set command "next"
92             }
93             -re ".*init_malloc.*$gdb_prompt $" {
94                 set description "next over init_malloc and everything it calls"
95                 set command "next"
96             }
97             -re ".*count . 0x3.*$gdb_prompt $" {
98                 set description "next over conditional stack alignment code 1"
99                 set command "next"
100             }
101             -re ".*if .i != 0.*$gdb_prompt $" {
102                 set description "next over conditional stack alignment code 2"
103                 set command "next"
104             }
105             -re ".*alloca .i - 4.*$gdb_prompt $" {
106                 set description "next over conditional stack alignment alloca"
107                 set command "next"
108             }
109             -re ".*SET_TOP_LEVEL.*$gdb_prompt $" {
110                 set description "next over SET_TOP_LEVEL call"
111                 set command "next"
112             }
113             -re ".*cmdsize = 1.*$gdb_prompt $" {
114                 set description "step over cmdsize initialization"
115                 set command "next"
116             }
117             -re ".*cmdarg = .* xmalloc.*$gdb_prompt $" {
118                 set description "next over cmdarg initialization via xmalloc"
119                 set command "next"
120             }
121             -re ".*ncmd = 0.*$gdb_prompt $" {
122                 set description "next over ncmd initialization"
123                 set command "next"
124             }
125             -re ".*dirsize = 1.*$gdb_prompt $" {
126                 set description "next over dirsize initialization"
127                 set command "next"
128             }
129             -re ".*dirarg = .* xmalloc.*$gdb_prompt $" {
130                 return
131             }
132             -re "\[ \t\]+\{\r\n$gdb_prompt $" {
133                 setup_xfail "mips-*-irix5*"
134                 fail "$description ended up at odd location"
135             }
136             -re ".*main.c.*No such file or directory.*$gdb_prompt $" {
137                 setup_xfail "rs6000-*-aix3*"
138                 fail "must be able to list source lines"
139                 return
140             }
141             -re ".*$gdb_prompt $" {
142                 fail "unknown source line after $description"
143                 return
144             }
145             default {
146                 fail "unknown source line near main"
147                 return
148             }
149         }
150         send_gdb "$command\n"
151         gdb_expect {
152             -re ".*No such file or directory.\r\n$gdb_prompt $" {
153                 fail "$description (no source available)"
154             }
155             -re ".*A file or directory .* does not exist..\r\n$gdb_prompt $" {
156                 fail "$description (no source available)"
157             }
158             -re ".*$gdb_prompt $" {
159                 pass "$description"
160             }
161             timeout {
162                 fail "$description (timeout)"
163             }
164         }
165     }
166 }
167
168 proc test_with_self { executable } {
169     global gdb_prompt
170     global tool
171     global det_file
172     global decimal
173     global timeout
174
175     # load yourself into the debugger
176     # This can take a relatively long time, particularly for testing where
177     # the executable is being accessed over a network, or where gdb does not
178     # support partial symbols for a particular target and has to load the
179     # entire symbol table.  Set the timeout to 10 minutes, which should be
180     # adequate for most environments (it *has* timed out with 5 min on a
181     # SPARCstation SLC under moderate load, so this isn't unreasonable).
182     # After gdb is started, set the timeout to 30 seconds for the duration
183     # of this test, and then back to the original value.
184
185     set oldtimeout $timeout
186     set timeout 600
187     verbose "Timeout is now $timeout seconds" 2
188     if {[gdb_load $executable] <0} then {
189         set timeout $oldtimeout
190         verbose "Timeout is now $timeout seconds" 2
191         return -1
192     }
193     set timeout $oldtimeout
194     verbose "Timeout is now $timeout seconds" 2
195
196     # disassemble yourself
197     gdb_test "x/10i main" \
198             "x/10i.*main.*main.$decimal.*main.$decimal.*" \
199             "Disassemble main"
200
201     # Set a breakpoint at main
202     gdb_test "break main" \
203             "Breakpoint.*at.* file.*, line.*" \
204             "breakpoint in main"
205
206     # We'll need this when we send a ^C to GDB.  Need to do it before we
207     # run the program and gdb starts saving and restoring tty states.
208     # On Ultrix, we don't need it and it is really slow (because shell_escape
209     # doesn't use vfork).
210     if ![istarget "*-*-ultrix*"] then {
211         gdb_test "shell stty intr '^C'" "" \
212             "set interrupt character in test_with_self"
213     }
214
215     # FIXME: If we put this after the run to main, the first list
216     # command doesn't print the same line as the current line where
217     # gdb is stopped.
218     gdb_test "set listsize 1" "" "set listsize to 1"
219
220     # run yourself
221     # It may take a very long time for the inferior gdb to start (lynx),
222     # so we bump it back up for the duration of this command.
223     set timeout 600
224
225     set description "run until breakpoint at main"
226     send_gdb "run -nw\n"
227     gdb_expect {
228         -re "Starting program.*Breakpoint \[0-9\]+,.*main .argc.*argv.* at .*main.c:.*$gdb_prompt $" {
229             pass "$description"
230         }
231         -re "Starting program.*Breakpoint \[0-9\]+,.*main .argc.*argv.*$gdb_prompt $" {
232             xfail "$description (line numbers scrambled?)"
233         }
234         -re "vfork: No more processes.*$gdb_prompt $" {
235             fail "$description (out of virtual memory)"
236             set timeout $oldtimeout
237             verbose "Timeout is now $timeout seconds" 2
238             return -1
239         }
240         -re ".*$gdb_prompt $" {
241             fail "$description"
242             set timeout $oldtimeout
243             verbose "Timeout is now $timeout seconds" 2
244             return -1
245         }
246         timeout {
247             fail "$description (timeout)"
248         }
249     }
250
251     set timeout $oldtimeout
252     verbose "Timeout is now $timeout seconds" 2
253
254     # do we have a version number ?
255     send_gdb "print version\n"
256     gdb_expect {
257         -re ".\[0-9\]+ = +0x.*\[0-9.\]+.*$gdb_prompt $" {
258             pass "printed version"
259         }
260         -re ".\[0-9\]+ = +.+ +0x.*\[0-9.\]+.*$gdb_prompt $" {
261             pass "printed version with cast"
262         }
263         -re ".*$gdb_prompt $"   { fail "printed version" }
264         timeout         { fail "(timeout) printed version" }
265     }
266
267     do_steps_and_nexts
268
269     gdb_test "print \"foo\"" ".\[0-9\]+ = \"foo\"" "print a string"
270
271     # do_steps_and_nexts left us ready to execute an xmalloc call,
272     # so give that a try.
273     # If we don't actually enter the xmalloc call when we give a
274     # step command that seems like a genuine bug.  It seems to happen
275     # on most RISC processors.
276     setup_xfail "alpha-*-*" "mips-*-*"
277     set description "step into xmalloc call"
278     send_gdb "step\n"
279     gdb_expect {
280         -re "xmalloc.*size=.*at.*utils.c.*$gdb_prompt $" {
281             pass "$description"
282         }
283         -re ".*No such file or directory.\r\n$gdb_prompt $" {
284             pass "$description (no source available)"
285         }
286         -re "A file or directory .* does not exist..\r\n$gdb_prompt $" {
287             pass "$description (no source available)"
288         }
289         -re ".*$gdb_prompt $" {
290             fail "$description"
291         }
292         timeout {
293             fail "$description (timeout)"
294         }
295     }
296
297     # start the "xgdb" process
298     send_gdb "continue\n"
299     gdb_expect {
300         -re "GNU gdb \[0-9\.\]*.*
301 Copyright \[0-9\]* Free Software Foundation, Inc.*
302 GDB is free software, covered by the GNU General Public License, and you are.*
303 welcome to change it and/or distribute copies of it under certain conditions.*
304 Type \"show copying\" to see the conditions.*
305 There is absolutely no warranty for GDB.  Type \"show warranty\" for details.*
306 This GDB was configured as .*$gdb_prompt $"\
307             { pass "xgdb is at prompt" }
308         -re "GDB is free software and you are welcome to distribute copies of it.*
309  under certain conditions; type \"show copying\" to see the conditions..*
310 There is absolutely no warranty for GDB; type \"show warranty\" for details..*
311 GDB.*Copyright \[0-9\]+ Free Software Foundation, Inc..*$gdb_prompt $"\
312             { pass "xgdb is at prompt (obsolescent gdb)" }
313         -re ".*$gdb_prompt $"       { fail "xgdb is at prompt" }
314         timeout             { fail "(timeout) xgdb is at prompt" }
315     }
316     
317     # set xgdb prompt so we can tell which is which
318     send_gdb "set prompt (xgdb) \n"
319     gdb_expect {
320         -re "\[(\]xgdb\[)\].*\[(\]xgdb\[)\] $"  { pass "Set xgdb prompt" }
321         -re ".*$gdb_prompt $"           { fail "Set xgdb prompt" }
322         default                         { fail "(timeout) Set xgdb prompt" }
323     }
324     
325     # kill the xgdb process
326     set description "send ^C to child process"
327     send_gdb "\003"
328     gdb_expect {
329         -re "Program received signal SIGINT.*$gdb_prompt $" {
330             pass "$description"
331         }
332         -re ".*$gdb_prompt $" {
333             fail "$description"
334         }
335         timeout {
336             fail "$description (timeout)"
337         }
338     }
339     
340     set description "send SIGINT signal to child process"
341     send_gdb "signal SIGINT\n"
342     gdb_expect {
343         -re "Continuing with signal SIGINT.*$gdb_prompt $" {
344             pass "$description"
345         }
346         -re ".*$gdb_prompt $" {
347             fail "$description"
348         }
349         timeout {
350             fail "$description (timeout)"
351         }
352     }
353     
354     # get a stack trace
355     #
356     # This fails on some linux systems for unknown reasons.  On the
357     # systems where it fails, sometimes it works fine when run manually.
358     # The testsuite failures may not be limited to just aout systems.
359     setup_xfail "i*86-pc-linuxaout-gnu" "hppa*-*-hpux*"
360     set description "backtrace through signal handler"
361     send_gdb "backtrace\n"
362     gdb_expect {
363         -re "#0.*read.*in main \\(.*\\) at .*main\\.c.*$gdb_prompt $" {
364             pass "$description"
365         }
366         -re ".*$gdb_prompt $" {
367             # On the alpha, we hit the infamous problem about gdb
368             # being unable to get the frame pointer (mentioned in
369             # gdb/README).  As it is intermittent, there is no way to
370             # XFAIL it which will give us an XPASS if the problem goes
371             # away.
372             setup_xfail "alpha*-*-osf*"
373             fail "$description"
374         }
375         timeout {
376             fail "$description (timeout)"
377         }
378     }
379
380
381     # Set the timeout back to the value it had when we were called.
382     set timeout $oldtimeout
383     verbose "Timeout is now $timeout seconds" 2
384
385     # Restart gdb in case next test expects it to be started already.
386     return 0
387 }
388
389 # Find a pathname to a file that we would execute if the shell was asked
390 # to run $arg using the current PATH.
391
392 proc find_gdb { arg } {
393
394     # If the arg directly specifies an existing executable file, then
395     # simply use it.
396
397     if [file executable $arg] then {
398         return $arg
399     }
400
401     set result [which $arg]
402     if [string match "/" [ string range $result 0 0 ]] then {
403         return $result
404     }
405
406     # If everything fails, just return the unqualified pathname as default
407     # and hope for best.
408
409     return $arg
410 }
411
412 # Run the test with self.
413 # Copy the file executable file in case this OS doesn't like to edit its own
414 # text space.
415
416 set GDB_FULLPATH [find_gdb $GDB]
417
418 # Remove any old copy lying around.
419 remote_file host delete x$tool
420
421 gdb_start
422 set file [remote_download host $GDB_FULLPATH x$tool]
423 set result [test_with_self $file];
424 gdb_exit;
425 catch "remote_file host delete $file";
426
427 if {$result <0} then {
428     warning "Couldn't test self"
429     return -1
430 }