ec3b94e962d06c3889d5212f9db12d32370af10b
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright (C) 1992, 1994, 1995 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 Fred Fish. (fnf@cygnus.com)
21
22 # Generic gdb subroutines that should work for any target. If these
23 # need to be modified for any target, it can be done with a variable
24 # or by passing arguments.
25
26 load_lib libgloss.exp
27
28 global GDB
29 global CC
30 global CXX
31 global CFLAGS
32 global CXXFLAGS
33 global CHILL_LIB
34 global CHILL_RT0
35
36 if ![info exists CC] {
37 set CC [findfile $base_dir/../../gcc/xgcc "$base_dir/../../gcc/xgcc -B$base_dir/../../gcc/" [transform gcc]]
38 }
39 verbose "using CC = $CC" 2
40 if ![info exists CXX] {
41 set CXX [findfile $base_dir/../../gcc/xgcc "$base_dir/../../gcc/xgcc -B$base_dir/../../gcc/" [transform g++]]
42 }
43 verbose "using CXX = $CXX" 2
44 if ![info exists CHILL_LIB] {
45 set CHILL_LIB [findfile $base_dir/../../gcc/ch/runtime/libchill.a "$base_dir/../../gcc/ch/runtime/libchill.a" [transform -lchill]]
46 }
47 verbose "using CHILL_LIB = $CHILL_LIB" 2
48 if ![info exists CHILL_RT0] {
49 set CHILL_RT0 [findfile $base_dir/../../gcc/ch/runtime/chillrt0.o "$base_dir/../../gcc/ch/runtime/chillrt0.o" ""]
50 }
51 verbose "using CHILL_RT0 = $CHILL_RT0" 2
52
53 if ![info exists LDFLAGS] {
54 if [is3way] {
55 append LDFLAGS " [libgloss_flags] [newlib_flags]"
56 }
57 set LDFLAGS ""
58 }
59 verbose "using LDFLAGS = $LDFLAGS" 2
60
61 if ![info exists GDB] then {
62 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
63 }
64 verbose "using GDB = $GDB" 2
65
66 global GDBFLAGS
67 if ![info exists GDBFLAGS] then {
68 set GDBFLAGS "-nx"
69 }
70 verbose "using GDBFLAGS = $GDBFLAGS" 2
71
72 # The variable prompt is a regexp which matches the gdb prompt. Set it if it
73 # is not already set.
74 global prompt
75 if ![info exists prompt] then {
76 set prompt "\\(gdb\\)"
77 }
78
79 global usestubs
80 if [istarget "sparclite-*-*"] then {
81 set usestubs 1
82 } else {
83 set usestubs 0
84 }
85
86 if ![info exists noargs] then {
87 set noargs 0
88 }
89
90 if ![info exists nosignals] then {
91 set nosignals 0
92 }
93
94 if ![info exists noinferiorio] then {
95 set noinferiorio 0
96 }
97
98 if ![info exists noresults] then {
99 set noresults 0
100 }
101
102 #
103 # gdb_version -- extract and print the version number of GDB
104 #
105 proc default_gdb_version {} {
106 global GDB
107 global GDBFLAGS
108 if {[which $GDB] != 0} then {
109 set tmp [exec echo "q" | $GDB -nw $GDBFLAGS]
110 regexp " \[0-9\]\[^ \t\n\]+" $tmp version
111 clone_output "[which $GDB] version$version -nw $GDBFLAGS \n"
112 } else {
113 warning "$GDB does not exist"
114 }
115 }
116
117 #
118 # gdb_unload -- unload a file if one is loaded
119 #
120
121 proc gdb_unload {} {
122 global verbose
123 global GDB
124 global prompt
125 send "file\n"
126 expect {
127 -re "No exec file now.*\r" { exp_continue }
128 -re "No symbol file now.*\r" { exp_continue }
129 -re "A program is being debugged already..*Kill it.*y or n. $"\
130 { send "y\n"
131 verbose "\t\tKilling previous program being debugged"
132 exp_continue
133 }
134 -re "Discard symbol table from .*y or n. $" {
135 send "y\n"
136 exp_continue
137 }
138 -re "$prompt $" {}
139 timeout {
140 perror "couldn't unload file in $GDB (timed out)."
141 return -1
142 }
143 }
144 }
145
146 # Many of the tests depend on setting breakpoints at various places and
147 # running until that breakpoint is reached. At times, we want to start
148 # with a clean-slate with respect to breakpoints, so this utility proc
149 # lets us do this without duplicating this code everywhere.
150 #
151
152 proc delete_breakpoints {} {
153 global prompt
154
155 send "delete breakpoints\n"
156 expect {
157 -re "Delete all breakpoints.*y or n. $" {
158 send "y\n"
159 exp_continue
160 }
161 -re "y\r\n$prompt $" {}
162 -re ".*$prompt $" { # This happens if there were no breakpoints
163 }
164 timeout { perror "Delete all breakpoints (timeout)" ; return }
165 }
166 send "info breakpoints\n"
167 expect {
168 -re "No breakpoints or watchpoints..*$prompt $" {}
169 -re ".*$prompt $" { perror "breakpoints not deleted" ; return }
170 timeout { perror "info breakpoints (timeout)" ; return }
171 }
172 }
173
174
175 #
176 # Generic run command.
177 #
178 # The second pattern below matches up to the first newline *only*.
179 # Using ``.*$'' could swallow up output that we attempt to match
180 # elsewhere.
181 #
182 proc gdb_run_cmd {} {
183 global usestubs
184 global prompt
185
186 if $usestubs!=0 {
187 send "jump *start\n"
188 expect {
189 -re "Line.* Jump anyway.*y or n. $" {
190 send "y\n"
191 expect {
192 -re "Continuing.*$prompt $" {}
193 timeout { perror "Jump to start() failed (timeout)"; return }
194 }
195 }
196 timeout { perror "Jump to start() failed (timeout)"; return }
197 }
198 send "continue\n"
199 return
200 }
201 send "run\n"
202 expect {
203 -re "The program .* has been started already.*y or n. $" {
204 send "y\n"
205 exp_continue
206 }
207 -re "Starting program: \[^\n\]*" {}
208 }
209 }
210
211
212 # Set breakpoint at function and run gdb until it breaks there.
213 # Since this is the only breakpoint that will be set, if it stops
214 # at a breakpoint, we will assume it is the one we want. We can't
215 # just compare to "function" because it might be a fully qualified,
216 # single quoted C++ function specifier.
217
218 proc runto { function } {
219 global prompt
220 global decimal
221
222 send "delete\n"
223 expect {
224 -re "delete.*Delete all breakpoints.*y or n. $" {
225 send "y\n"
226 expect {
227 -re "$prompt $" {}
228 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
229 }
230 }
231 -re ".*$prompt $" {}
232 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
233 }
234
235 send "break $function\n"
236 # The first two regexps are what we get with -g, the third is without -g.
237 expect {
238 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$prompt $" {}
239 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
240 -re "Breakpoint \[0-9\]* at .*$prompt $" {}
241 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
242 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
243 }
244
245 gdb_run_cmd
246
247 # the "at foo.c:36" output we get with -g.
248 # the "in func" output we get without -g.
249 expect {
250 -re "Break.* at .*:$decimal.*$prompt $" {
251 return 1
252 }
253 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
254 return 1
255 }
256 -re "$prompt $" {
257 fail "running to $function"
258 return 0
259 }
260 timeout {
261 fail "running to $function (timeout)"
262 return 0
263 }
264 }
265 }
266
267 #
268 # runto_main -- ask gdb to run and untill hit break point at main.
269 # if it uses stubs, assuming we hit breakpoint() and just
270 # step out of the function.
271 #
272 proc runto_main {} {
273 global prompt
274 global decimal
275 global usestubs
276
277 if $usestubs==0 {
278 return [runto main]
279 }
280
281 send "delete\n"
282 expect {
283 -re "delete.*Delete all breakpoints.*y or n. $" {
284 send "y\n"
285 expect {
286 -re "$prompt $" {}
287 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
288 }
289 }
290 -re ".*$prompt $" {}
291 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
292 }
293
294 send "step\n"
295 # if use stubs step out of the breakpoint() function.
296 expect {
297 -re "main.* at .*$prompt $" {}
298 timeout { fail "single step at breakpoint() (timeout)" ; return 0 }
299 }
300 return 1
301 }
302
303 #
304 # gdb_test -- send a command to gdb and test the result.
305 # Takes three parameters.
306 # Parameters:
307 # First one is the command to execute,
308 # Second one is the pattern to match for a PASS, and must NOT include
309 # the \r\n sequence immediately before the gdb prompt.
310 # Third one is an optional message to be printed. If this
311 # a null string "", then the pass/fail messages are not printed.
312 # Returns:
313 # 1 if the test failed,
314 # 0 if the test passes,
315 # -1 if there was an internal error.
316 #
317 proc gdb_test { args } {
318 global verbose
319 global prompt
320 global GDB
321 global spawn_id
322 global expect_out
323 upvar timeout timeout
324
325 if [llength $args]==3 then {
326 set message [lindex $args 2]
327 } else {
328 set message [lindex $args 0]
329 }
330 set command [lindex $args 0]
331 set pattern [lindex $args 1]
332
333 if $verbose>2 then {
334 send_user "Sending \"$command\" to gdb\n"
335 send_user "Looking to match \"$pattern\"\n"
336 send_user "Message is \"$message\"\n"
337 }
338
339 set result -1
340 if ![string match $command ""] {
341 send "$command\n"
342 }
343
344 expect {
345 -re ".*Ending remote debugging.*$prompt$" {
346 if ![isnative] then {
347 warning "Can`t communicate to remote target."
348 }
349 gdb_exit
350 gdb_start
351 set result -1
352 }
353 -re "$pattern\r\n$prompt $" {
354 if ![string match "" $message] then {
355 pass "$message"
356 }
357 set result 0
358 }
359 -re "Undefined command:.*$prompt" {
360 perror "Undefined command \"$command\"."
361 set result 1
362 }
363 -re "Ambiguous command.*$prompt $" {
364 perror "\"$command\" is not a unique command name."
365 set result 1
366 }
367 -re "(.*)(Program exited with code \[0-9\]+)(.*$prompt $)" {
368 if ![string match "" $message] then {
369 set errmsg "$message: $expect_out(2,string)"
370 } else {
371 set errmsg "$command: $expect_out(2,string)"
372 }
373 perror "$errmsg"
374 return -1
375 }
376 -re "The program is not being run.*$prompt $" {
377 if ![string match "" $message] then {
378 set errmsg "$message: the program is no longer running"
379 } else {
380 set errmsg "$command: the program is no longer running"
381 }
382 perror "$errmsg"
383 return -1
384 }
385 -re ".*$prompt $" {
386 if ![string match "" $message] then {
387 fail "$message"
388 }
389 set result 1
390 }
391 "<return>" {
392 send "\n"
393 perror "Window too small."
394 }
395 -re "\\(y or n\\) " {
396 send "n\n"
397 perror "Got interactive prompt."
398 }
399 eof {
400 perror "Process no longer exists"
401 return -1
402 }
403 full_buffer {
404 perror "internal buffer is full."
405 }
406 timeout {
407 if ![string match "" $message] then {
408 fail "(timeout) $message"
409 }
410 set result 1
411 }
412 }
413 return $result
414 }
415 \f
416 # Test that a command gives an error. For pass or fail, return
417 # a 1 to indicate that more tests can proceed. However a timeout
418 # is a serious error, generates a special fail message, and causes
419 # a 0 to be returned to indicate that more tests are likely to fail
420 # as well.
421
422 proc test_print_reject { args } {
423 global prompt
424 global verbose
425
426 if [llength $args]==2 then {
427 set expectthis [lindex $args 1]
428 } else {
429 set expectthis "should never match this bogus string"
430 }
431 set sendthis [lindex $args 0]
432 if $verbose>2 then {
433 send_user "Sending \"$sendthis\" to gdb\n"
434 send_user "Looking to match \"$expectthis\"\n"
435 }
436 send "$sendthis\n"
437 expect {
438 -re ".*A .* in expression.*\\.*$prompt $" {
439 pass "reject $sendthis"
440 return 1
441 }
442 -re ".*Invalid syntax in expression.*$prompt $" {
443 pass "reject $sendthis"
444 return 1
445 }
446 -re ".*Junk after end of expression.*$prompt $" {
447 pass "reject $sendthis"
448 return 1
449 }
450 -re ".*Invalid number.*$prompt $" {
451 pass "reject $sendthis"
452 return 1
453 }
454 -re ".*Invalid character constant.*$prompt $" {
455 pass "reject $sendthis"
456 return 1
457 }
458 -re ".*No symbol table is loaded.*$prompt $" {
459 pass "reject $sendthis"
460 return 1
461 }
462 -re ".*No symbol .* in current context.*$prompt $" {
463 pass "reject $sendthis"
464 return 1
465 }
466 -re ".*$expectthis.*$prompt $" {
467 pass "reject $sendthis"
468 return 1
469 }
470 -re ".*$prompt $" {
471 fail "reject $sendthis"
472 return 1
473 }
474 default {
475 fail "reject $sendthis (eof or timeout)"
476 return 0
477 }
478 }
479 }
480 \f
481 # Given an input string, adds backslashes as needed to create a
482 # regexp that will match the string.
483
484 proc string_to_regexp {str} {
485 set result $str
486 regsub -all {[]*+.|()^$\[]} $str {\\&} result
487 return $result
488 }
489
490 # Same as gdb_test, but the second parameter is not a regexp,
491 # but a string that must match exactly.
492
493 proc gdb_test_exact { args } {
494 upvar timeout timeout
495
496 set command [lindex $args 0]
497 set pattern [string_to_regexp [lindex $args 1]]
498 # It is most natural to write the pattern argument with only
499 # embedded \n's, especially if you are trying to avoid Tcl quoting
500 # problems. But expect really wants to see \r\n in patterns. So
501 # transform the pattern here. First transform \r\n back to \n, in
502 # case some users of gdb_test_exact already do the right thing.
503 regsub -all "\r\n" $pattern "\n" pattern
504 regsub -all "\n" $pattern "\r\n" pattern
505 if [llength $args]==3 then {
506 set message [lindex $args 2]
507 } else {
508 set message $command
509 }
510 return [gdb_test $command $pattern $message]
511 }
512 \f
513 proc gdb_reinitialize_dir { subdir } {
514 global prompt
515
516 send "dir\n"
517 expect {
518 -re "Reinitialize source path to empty.*y or n. " {
519 send "y\n"
520 expect {
521 -re "Source directories searched.*$prompt $" {
522 send "dir $subdir\n"
523 expect {
524 -re "Source directories searched.*$prompt $" {
525 verbose "Dir set to $subdir"
526 }
527 -re ".*$prompt $" {
528 perror "Dir \"$subdir\" failed."
529 }
530 }
531 }
532 -re ".*$prompt $" {
533 perror "Dir \"$subdir\" failed."
534 }
535 }
536 }
537 -re ".*$prompt $" {
538 perror "Dir \"$subdir\" failed."
539 }
540 }
541 }
542
543 #
544 # gdb_exit -- exit the GDB, killing the target program if necessary
545 #
546 proc default_gdb_exit {} {
547 global GDB
548 global GDBFLAGS
549 global verbose
550
551 verbose "Quitting $GDB $GDBFLAGS"
552
553 # This used to be 1 for unix-gdb.exp
554 set timeout 5
555 verbose "Timeout is now $timeout seconds" 2
556
557 # We used to try to send "quit" to GDB, and wait for it to die.
558 # Dealing with all the cases and errors got pretty hairy. Just close it,
559 # that is simpler.
560 close
561
562 # Omitting this probably would cause strange timing-dependent failures.
563 wait
564 }
565
566 #
567 # load a file into the debugger.
568 # return a -1 if anything goes wrong.
569 #
570 proc gdb_file_cmd { arg } {
571 global verbose
572 global loadpath
573 global loadfile
574 global GDB
575 global prompt
576 global spawn_id
577 upvar timeout timeout
578
579 send "file $arg\n"
580 expect {
581 -re "Reading symbols from.*done.*$prompt $" {
582 verbose "\t\tLoaded $arg into the $GDB"
583 return 0
584 }
585 -re "has no symbol-table.*$prompt $" {
586 perror "$arg wasn't compiled with \"-g\""
587 return -1
588 }
589 -re "A program is being debugged already.*Kill it.*y or n. $" {
590 send "y\n"
591 verbose "\t\tKilling previous program being debugged"
592 exp_continue
593 }
594 -re "Load new symbol table from \".*\".*y or n. $" {
595 send "y\n"
596 expect {
597 -re "Reading symbols from.*done.*$prompt $" {
598 verbose "\t\tLoaded $arg with new symbol table into $GDB"
599 return 0
600 }
601 timeout {
602 perror "(timeout) Couldn't load $arg, other program already l
603 oaded."
604 return -1
605 }
606 }
607 }
608 -re ".*No such file or directory.*$prompt $" {
609 perror "($arg) No such file or directory\n"
610 return -1
611 }
612 -re "$prompt $" {
613 perror "couldn't load $arg into $GDB."
614 return -1
615 }
616 timeout {
617 perror "couldn't load $arg into $GDB (timed out)."
618 return -1
619 }
620 eof {
621 # This is an attempt to detect a core dump, but seems not to
622 # work. Perhaps we need to match .* followed by eof, in which
623 # expect does not seem to have a way to do that.
624 perror "couldn't load $arg into $GDB (end of file)."
625 return -1
626 }
627 }
628 }
629
630 #
631 # start gdb -- start gdb running, default procedure
632 #
633 # When running over NFS, particularly if running many simultaneous
634 # tests on different hosts all using the same server, things can
635 # get really slow. Give gdb at least 3 minutes to start up.
636 #
637 proc default_gdb_start { } {
638 global verbose
639 global GDB
640 global GDBFLAGS
641 global prompt
642 global spawn_id
643 global timeout
644 verbose "Spawning $GDB -nw $GDBFLAGS"
645
646 if { [which $GDB] == 0 } then {
647 perror "$GDB does not exist."
648 exit 1
649 }
650
651 set oldtimeout $timeout
652 set timeout [expr "$timeout + 180"]
653 verbose "Timeout increased to $timeout seconds" 2
654 eval "spawn $GDB -nw $GDBFLAGS"
655 expect {
656 -re ".*\r\n$prompt $" {
657 verbose "GDB initialized."
658 }
659 -re "$prompt $" {
660 perror "GDB never initialized."
661 set timeout $oldtimeout
662 verbose "Timeout restored to $timeout seconds" 2
663 return -1
664 }
665 timeout {
666 perror "(timeout) GDB never initialized after $timeout seconds."
667 set timeout $oldtimeout
668 verbose "Timeout restored to $timeout seconds" 2
669 return -1
670 }
671 }
672 set timeout $oldtimeout
673 verbose "Timeout restored to $timeout seconds" 2
674 # force the height to "unlimited", so no pagers get used
675 send "set height 0\n"
676 expect {
677 -re ".*$prompt $" {
678 verbose "Setting height to 0." 2
679 }
680 timeout {
681 warning "Couldn't set the height to 0."
682 }
683 }
684 # force the width to "unlimited", so no wraparound occurs
685 send "set width 0\n"
686 expect {
687 -re ".*$prompt $" {
688 verbose "Setting width to 0." 2
689 }
690 timeout {
691 warning "Couldn't set the width to 0."
692 }
693 }
694 }
695
696 #
697 # FIXME: this is a copy of the new library procedure, but it's here too
698 # till the new dejagnu gets installed everywhere. I'd hate to break the
699 # gdb tests suite.
700 #
701 global argv0
702 if ![info exists argv0] then {
703 proc exp_continue { } {
704 continue -expect
705 }
706 }
707
708 # * For crosses, the CHILL runtime doesn't build because it can't find
709 # setjmp.h, stdio.h, etc.
710 # * For AIX (as of 16 Mar 95), (a) there is no language code for
711 # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
712 # does not get along with AIX's too-clever linker.
713 # * On Irix5, there is a bug whereby set of bool, etc., don't get
714 # TYPE_LOW_BOUND for the bool right because force_to_range_type doesn't
715 # work with stub types.
716 # Lots of things seem to fail on the PA, and since it's not a supported
717 # chill target at the moment, don't run the chill tests.
718
719 proc skip_chill_tests {} {
720 eval set skip_chill [expr ![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-irix5*"] || [istarget "alpha-*-osf*"] || [istarget "hppa*-*-*"]]
721 verbose "Skip chill tests is $skip_chill"
722 return $skip_chill
723 }
This page took 0.045776 seconds and 4 git commands to generate.