* lib/gdb.exp (gdb_test): Between $pattern and $prompt, expect
[binutils-gdb.git] / gdb / testsuite / gdb.base / a1-selftest.exp
1 # Copyright (C) 1988, 1990, 1991, 1992, 1994 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., 675 Mass Ave, Cambridge, MA 02139, 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 ![isnative] then {
31 continue
32 }
33 if [istarget "m68k*-*-hpux*"] then {
34 # The top-level makefile passes CFLAGS= (no -g) for hp300. This probably
35 # should be fixed (it is only needed for gcc bootstrapping, not gdb),
36 # but until then.....
37 setup_xfail "*-*-*"
38 fail "cannot test self if compiled without debug info"
39 continue
40 }
41
42 proc test_with_self {} {
43 global prompt
44 global tool
45 global det_file
46 global timeout
47 global decimal
48
49 # load yourself into the debugger
50 # This can take a relatively long time, particularly for testing where
51 # the executable is being accessed over a network, or where gdb does not
52 # support partial symbols for a particular target and has to load the
53 # entire symbol table. Set the timeout to 10 minutes, which should be
54 # adequate for most environments (it *has* timed out with 5 min on a
55 # SPARCstation SLC under moderate load, so this isn't unreasonable).
56 # After gdb is loaded, set the timeout to 30 seconds for the duration
57 # of this test, and then back to the original value.
58
59 set oldtimeout $timeout
60 set timeout 600
61 if {[gdb_load "./x$tool"] <0} then {
62 return -1
63 }
64
65 set timeout 30
66
67 # disassemble yourself
68 send "x/10i main\n"
69 expect {
70 -re "x/10i.*main.*main.$decimal.*main.$decimal.*$prompt $"\
71 { pass "Disassemble main" }
72 -re ".*$prompt $" { fail "Disassemble main" }
73 timeout { fail "(timeout) Disassemble main" }
74 }
75
76 set description "set breakpoint at main"
77 send "break main\n"
78 expect {
79 -re "Breakpoint.*at.* file.*, line.*$prompt $" {
80 pass "$description"
81 }
82 -re ".*$prompt $" {
83 fail "$description"
84 }
85 timeout {
86 fail "$description (timeout)"
87 }
88 }
89
90 # We'll need this when we send a ^C to GDB. Need to do it before we
91 # run the program and gdb starts saving and restoring tty states.
92 # On Ultrix, we don't need it and it is really slow (because shell_escape
93 # doesn't use vfork).
94 if ![istarget "*-*-ultrix*"] then {
95 gdb_test "shell stty intr '^C'" "" \
96 "set interrupt character in test_with_self"
97 }
98
99 # run yourself
100 set description "run until breakpoint at main"
101 send "run\n"
102 expect {
103 -re "Starting program.*Breakpoint \[0-9\]+,.*main .argc.*argv.* at .*main.c:.*$prompt $" {
104 pass "$description"
105 }
106 -re "Starting program.*Breakpoint \[0-9\]+,.*main .argc.*argv.*$prompt $" {
107 xfail "$description (line numbers scrambled?)"
108 }
109 -re "vfork: No more processes.*$prompt $" {
110 fail "$description (out of virtual memory)"
111 return -1
112 }
113 -re ".*$prompt $" {
114 fail "$description"
115 return -1
116 }
117 timeout {
118 fail "$description (timeout)"
119 }
120 }
121
122 # do we have a version number ?
123 send "print version\n"
124 expect {
125 -re ".\[0-9\]+ = +0x.*\[0-9.\]+.*$prompt $" {
126 pass "printed version"
127 }
128 -re ".\[0-9\]+ = +.+ +0x.*\[0-9.\]+.*$prompt $" {
129 pass "printed version with cast"
130 }
131 -re ".*$prompt $" { fail "printed version" }
132 timeout { fail "(timeout) printed version" }
133 }
134
135 # lets do a few single steps
136
137 set description "step over symarg initialization"
138 send "step\n"
139 expect {
140 -re "char \[*\]+execarg = NULL;.*$prompt $" {
141 pass "$description"
142 }
143 -re ".*No such file or directory.\r\n$prompt $" {
144 pass "$description (no source available)"
145 }
146 -re ".*A file or directory .* does not exist..\r\n$prompt $" {
147 pass "$description (no source available)"
148 }
149 -re ".*$prompt $" {
150 fail "$description"
151 }
152 timeout {
153 fail "$description (timeout)"
154 }
155 }
156
157 set description "step over execarg initialization"
158 send "step\n"
159 expect {
160 -re "char \[*\]corearg = NULL;.*$prompt $" {
161 pass "$description"
162 }
163 -re ".*No such file or directory.\r\n$prompt $" {
164 pass "$description (no source available)"
165 }
166 -re ".*A file or directory .* does not exist..\r\n$prompt $" {
167 pass "$description (no source available)"
168 }
169 -re ".*$prompt $" {
170 fail "$description"
171 }
172 timeout {
173 fail "$description" (timeout)"
174 }
175 }
176
177 set description "step over corearg initialization"
178 send "step\n"
179 expect {
180 -re "char \[*\]cdarg = NULL;.*$prompt $" {
181 pass "$description"
182 }
183 -re ".*No such file or directory.\r\n$prompt $" {
184 pass "$description (no source available)"
185 }
186 -re ".*A file or directory .* does not exist..\r\n$prompt $" {
187 pass "$description (no source available)"
188 }
189 -re ".*$prompt $" {
190 fail "$description"
191 }
192 timeout {
193 fail "$description" (timeout)"
194 }
195 }
196
197 set description "step over cdarg initialization"
198 send "step\n"
199 expect {
200 -re "char \[*\]ttyarg = NULL;.*$prompt $" {
201 pass "$description"
202 }
203 -re ".*No such file or directory.\r\n$prompt $" {
204 pass "$description (no source available)"
205 }
206 -re ".*A file or directory .* does not exist..\r\n$prompt $" {
207 pass "$description (no source available)"
208 }
209 -re ".*$prompt $" {
210 fail "$description"
211 }
212 timeout {
213 fail "$description" (timeout)"
214 }
215 }
216
217 set description "step over ttyarg initialization"
218 send "step\n"
219 expect {
220 -re ".*time_at_startup = get_run_time .*$prompt $" {
221 pass "$description"
222 }
223 -re ".*No such file or directory.\r\n$prompt $" {
224 pass "$description (no source available)"
225 }
226 -re ".*A file or directory .* does not exist..\r\n$prompt $" {
227 pass "$description (no source available)"
228 }
229 -re ".*$prompt $" {
230 fail "$description"
231 }
232 timeout {
233 fail "$description" (timeout)"
234 }
235 }
236
237 # now jump over a few functions
238
239 set description "next over get_run_time and everything it calls"
240 set got_over_get_run_time 0
241 send "next\n"
242 expect {
243 -re ".*init_malloc.*$prompt $" {
244 pass "$description"
245 set got_over_get_run_time 1
246 }
247 -re ".*No such file or directory.\r\n$prompt $" {
248 pass "$description (no source available)"
249 set got_over_get_run_time 1
250 }
251 -re ".*A file or directory .* does not exist..\r\n$prompt $" {
252 pass "$description (no source available)"
253 set got_over_get_run_time 1
254 }
255 -re ".*$prompt $" {
256 fail "$description"
257 }
258 timeout {
259 fail "$description (timeout)"
260 }
261 }
262 # -re "if \(setmp \(to_top_level\)\).*$prompt $" { pass "first next" }
263
264 if $got_over_get_run_time then {
265 set description "next over init_malloc and everything it calls"
266 send "next\n"
267 expect {
268 -re "if.*SET_TOP_LEVEL.*$prompt $" {
269 pass "$description"
270 }
271 -re "i = .*count.*$prompt $" {
272 pass "$description"
273 set description "next over ALIGN_STACK_ON_STARTUP code"
274 send "next\n"
275 expect {
276 -re "if.*i != 0.*$prompt $" {
277 pass "$description"
278 send "next\n"
279 expect {
280 -re "if.*SET_TOP_LEVEL.*$prompt $" {
281 pass "$description"
282 }
283 -re "alloca.*$prompt $" {
284 send "next\n"
285 expect {
286 -re "if.*level.*$prompt $" {
287 pass "$description"
288 }
289 }
290 }
291 }
292 }
293 }
294 }
295 -re ".*No such file or directory.\r\n$prompt $" {
296 pass "$description (no source available)"
297 }
298 -re ".*A file or directory .* does not exist..\r\n$prompt $" {
299 pass "$description (no source available)"
300 }
301 -re ".*$prompt $" {
302 fail "$description"
303 }
304 default {
305 fail "$description (timeout or eof)"
306 }
307 }
308 }
309
310 set next_ok 1
311 set need_to_step_over_cmdsize 1
312 set description "next over SET_TOP_LEVEL call"
313 send "next\n"
314 expect {
315 -re "cmdsize = 1;.*$prompt $" {
316 pass "$description"
317 }
318 -re "cmdarg = .*xmalloc.*$prompt $" {
319 # This can happen in optimized code where the "cmdsize = 1" line
320 # is basically removed.
321 pass "$description"
322 set need_to_step_over_cmdsize 0
323 }
324 -re ".*No such file or directory.\r\n$prompt $" {
325 pass "$description (no source available)"
326 }
327 -re ".*A file or directory .* does not exist..\r\n$prompt $" {
328 pass "$description (no source available)"
329 }
330 -re ".*$prompt $" {
331 fail "$description"
332 set next_ok 0
333 }
334 timeout {
335 fail "$description (timeout)"
336 set next_ok 0
337 }
338 }
339
340 set description "print a string"
341 send "print \"foo\"\n"
342 expect {
343 -re ".\[0-9\]+ = \"foo\".*$prompt $" {
344 pass "$description"
345 }
346 -re ".*$prompt $" {
347 fail "$description"
348 }
349 timeout {
350 fail "$description (timeout)"
351 }
352 }
353
354 if $need_to_step_over_cmdsize {
355 set description "step over cmdsize initialization"
356 send "step\n"
357 expect {
358 -re "cmdarg = .*xmalloc.*$prompt $" {
359 pass "$description"
360 }
361 -re ".*No such file or directory.\r\n$prompt $" {
362 pass "$description (no source available)"
363 }
364 -re ".*A file or directory .* does not exist..\r\n$prompt $" {
365 pass "$description (no source available)"
366 }
367 -re ".*$prompt $" {
368 fail "$description"
369 }
370 timeout {
371 fail "$description" (timeout)"
372 }
373 }
374 }
375
376 # and another step for good luck
377 # This one fails on Solaris (for some versions of gdb) because you
378 # can't step into library functions
379 if $next_ok {
380 set description "step into xmalloc call"
381 send "step\n"
382 expect {
383 -re "xmalloc.*size=.*at.*utils.c.*$prompt $" {
384 pass "$description"
385 }
386 -re ".*No such file or directory.\r\n$prompt $" {
387 pass "$description (no source available)"
388 }
389 -re "A file or directory .* does not exist..\r\n$prompt $" {
390 pass "$description (no source available)"
391 }
392 -re ".*$prompt $" {
393 fail "$description"
394 }
395 timeout {
396 fail "$description (timeout)"
397 }
398 }
399 }
400
401 # start the "xgdb" process
402 send "continue\n"
403 expect {
404 -re "GDB is free software and you are welcome to distribute copies of it.*
405 under certain conditions; type \"show copying\" to see the conditions..*
406 There is absolutely no warranty for GDB; type \"show warranty\" for details..*
407 GDB.*Copyright \[0-9\]+ Free Software Foundation, Inc..*$prompt $"\
408 { pass "xgdb is at prompt" }
409 -re ".*$prompt $" { fail "xgdb is at prompt" }
410 timeout { fail "(timeout) xgdb is at prompt" }
411 }
412
413 # set xgdb prompt so we can tell which is which
414 send "set prompt (xgdb) \n"
415 expect {
416 -re "\[(\]xgdb\[)\].*\[(\]xgdb\[)\] $" { pass "Set xgdb prompt" }
417 -re ".*$prompt $" { fail "Set xgdb prompt" }
418 default { fail "(timeout) Set xgdb prompt" }
419 }
420
421 # kill the xgdb process
422 set description "send ^C to child process"
423 send "\003"
424 expect {
425 -re "Program received signal SIGINT.*$prompt $" {
426 pass "$description"
427 }
428 -re ".*$prompt $" {
429 fail "$description"
430 }
431 timeout {
432 fail "$description (timeout)"
433 }
434 }
435
436 set description "send SIGINT signal to child process"
437 send "signal SIGINT\n"
438 expect {
439 -re "Continuing with signal SIGINT.*$prompt $" {
440 pass "$description"
441 }
442 -re ".*$prompt $" {
443 fail "$description"
444 }
445 timeout {
446 fail "$description (timeout)"
447 }
448 }
449
450 # get a stack trace
451 setup_xfail "i486-*-sysv4"
452 set description "backtrace through signal handler"
453 send "backtrace\n"
454 expect {
455 -re "#0.*read.*#\[1-9\].*main.c.*$prompt $" {
456 pass "$description"
457 }
458 -re ".*$prompt $" {
459 fail "$description"
460 }
461 timeout {
462 fail "$description (timeout)"
463 }
464 }
465
466 gdb_exit
467
468 # Set the timeout back to the value it had when we were called.
469 set timeout $oldtimeout
470
471 # Restart gdb in case next test expects it to be started already.
472 gdb_start
473 return 0
474 }
475
476 # Find a pathname to a file that we would execute if the shell was asked
477 # to run $arg using the current PATH.
478
479 proc find_gdb { arg } {
480
481 # If the arg directly specifies an existing executable file, then
482 # simply use it.
483
484 if [file executable $arg] then {
485 return $arg
486 }
487
488 set result [which $arg]
489 if [string match "/" [ string range $result 0 0 ]] then {
490 return $result
491 }
492
493 # If everything fails, just return the unqualified pathname as default
494 # and hope for best.
495
496 return $arg
497 }
498
499 # Run the test with self.
500 # Copy the file executable file in case this OS doesn't like to edit it's own
501 # text space.
502
503 set GDB_FULLPATH [find_gdb $GDB]
504
505 # Remove any old copy lying around.
506 catch "exec rm -f ./x$tool"
507
508 if ![file executable $GDB_FULLPATH] then {
509 fail "couldn't convert $GDB to absolute pathname to make local copy"
510 } else {
511 if [catch "exec cp $GDB_FULLPATH ./x$tool"] then {
512 fail "couldn't copy $GDB_FULLPATH to current directory"
513 } else {
514 verbose "\t\tCopied $GDB_FULLPATH to ./x$tool"
515 if {[test_with_self] <0} then {
516 warning "Couldn't test self"
517 catch "exec rm -f ./x$tool"
518 return -1
519 }
520 catch "exec rm -f ./x$tool"
521 }
522 }