1 # Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
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.
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.
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. */
17 # Please email any bugs, comments, and/or additions to this file to:
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.
32 if ![info exists CHILL_LIB] {
33 set CHILL_LIB [findfile $base_dir/../../gcc/ch/runtime/libchill.a "$base_dir/../../gcc/ch/runtime/libchill.a" [transform -lchill]]
35 verbose "using CHILL_LIB = $CHILL_LIB" 2
36 if ![info exists CHILL_RT0] {
37 set CHILL_RT0 [findfile $base_dir/../../gcc/ch/runtime/chillrt0.o "$base_dir/../../gcc/ch/runtime/chillrt0.o" ""]
39 verbose "using CHILL_RT0 = $CHILL_RT0" 2
41 if ![info exists GDB] {
42 if ![is_remote host] {
43 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
48 verbose "using GDB = $GDB" 2
51 if ![info exists GDBFLAGS] {
54 verbose "using GDBFLAGS = $GDBFLAGS" 2
56 # The variable prompt is a regexp which matches the gdb prompt. Set it if it
59 if ![info exists prompt] then {
60 set prompt "\[(\]gdb\[)\]"
63 if ![info exists noargs] then {
67 if ![info exists nosignals] then {
71 if ![info exists noinferiorio] then {
75 if ![info exists noresults] then {
80 # gdb_version -- extract and print the version number of GDB
82 proc default_gdb_version {} {
86 set fileid [open "gdb_cmd" w];
89 set cmdfile [remote_download host "gdb_cmd"];
90 set output [remote_exec host "$GDB -nw --command $cmdfile"]
91 remote_file build delete "gdb_cmd";
92 remote_file host delete "$cmdfile";
93 set tmp [lindex $output 1];
95 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
96 if ![is_remote host] {
97 clone_output "[which $GDB] version $version $GDBFLAGS\n"
99 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
103 proc gdb_version { } {
104 return [default_gdb_version];
108 # gdb_unload -- unload a file if one is loaded
117 -re "No exec file now.*\r" { exp_continue }
118 -re "No symbol file now.*\r" { exp_continue }
119 -re "A program is being debugged already..*Kill it.*y or n. $"\
121 verbose "\t\tKilling previous program being debugged"
124 -re "Discard symbol table from .*y or n. $" {
130 perror "couldn't unload file in $GDB (timed out)."
136 # Many of the tests depend on setting breakpoints at various places and
137 # running until that breakpoint is reached. At times, we want to start
138 # with a clean-slate with respect to breakpoints, so this utility proc
139 # lets us do this without duplicating this code everywhere.
142 proc delete_breakpoints {} {
146 send_gdb "delete breakpoints\n"
148 -i $gdb_spawn_id -re ".*Delete all breakpoints.*y or n.*$" {
152 -i $gdb_spawn_id -re ".*$prompt $" { # This happens if there were no breakpoints
154 -i $gdb_spawn_id timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
156 send_gdb "info breakpoints\n"
158 -i $gdb_spawn_id -re "No breakpoints or watchpoints..*$prompt $" {}
159 -i $gdb_spawn_id -re ".*$prompt $" { perror "breakpoints not deleted" ; return }
160 -i $gdb_spawn_id -re "Delete all breakpoints.*or n.*$" {
164 -i $gdb_spawn_id timeout { perror "info breakpoints (timeout)" ; return }
170 # Generic run command.
172 # The second pattern below matches up to the first newline *only*.
173 # Using ``.*$'' could swallow up output that we attempt to match
176 proc gdb_run_cmd {args} {
180 set spawn_id $gdb_spawn_id
182 if [target_info exists use_gdb_stub] {
183 send_gdb "jump *start\n"
185 -re "Line.* Jump anyway.*y or n. $" {
188 -re "Continuing.*$prompt $" {}
189 timeout { perror "Jump to start() failed (timeout)"; return }
192 -re "No symbol.*context.*$prompt $" {}
193 -re "The program is not being run.*$prompt $" {
196 timeout { perror "Jump to start() failed (timeout)"; return }
198 send_gdb "continue\n"
201 send_gdb "run $args\n"
202 # This doesn't work quite right yet.
204 -re "The program .* has been started already.*y or n. $" {
208 -re "Starting program: \[^\n\]*" {}
212 proc gdb_breakpoint { function } {
217 set spawn_id $gdb_spawn_id
219 send_gdb "break $function\n"
220 # The first two regexps are what we get with -g, the third is without -g.
222 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$prompt $" {}
223 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
224 -re "Breakpoint \[0-9\]* at .*$prompt $" {}
225 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
226 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
231 # Set breakpoint at function and run gdb until it breaks there.
232 # Since this is the only breakpoint that will be set, if it stops
233 # at a breakpoint, we will assume it is the one we want. We can't
234 # just compare to "function" because it might be a fully qualified,
235 # single quoted C++ function specifier.
237 proc runto { function } {
242 set spawn_id $gdb_spawn_id
246 if ![gdb_breakpoint $function] {
252 # the "at foo.c:36" output we get with -g.
253 # the "in func" output we get without -g.
255 -re "Break.* at .*:$decimal.*$prompt $" {
258 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
262 fail "running to $function in runto"
266 fail "running to $function in runto (timeout)"
274 # runto_main -- ask gdb to run until we hit a breakpoint at main.
275 # The case where the target uses stubs has to be handled
276 # specially--if it uses stubs, assuming we hit
277 # breakpoint() and just step out of the function.
283 if ![target_info exists gdb_stub] {
290 # if use stubs step out of the breakpoint() function.
292 -re "main.* at .*$prompt $" {}
293 -re "_start.*$prompt $" {}
294 timeout { fail "single step at breakpoint() (timeout)" ; return 0 }
300 # gdb_test -- send_gdb a command to gdb and test the result.
301 # Takes three parameters.
303 # First one is the command to execute. If this is the null string
304 # then no command is sent.
305 # Second one is the pattern to match for a PASS, and must NOT include
306 # the \r\n sequence immediately before the gdb prompt.
307 # Third one is an optional message to be printed. If this
308 # a null string "", then the pass/fail messages use the command
309 # string as the message.
311 # 1 if the test failed,
312 # 0 if the test passes,
313 # -1 if there was an internal error.
315 proc gdb_test { args } {
320 upvar timeout timeout
322 if [llength $args]>2 then {
323 set message [lindex $args 2]
325 set message [lindex $args 0]
327 set command [lindex $args 0]
328 set pattern [lindex $args 1]
330 if [llength $args]==5 {
331 set question_string [lindex $args 3];
332 set response_string [lindex $args 4];
334 set question_string "^FOOBAR$"
338 send_user "Sending \"$command\" to gdb\n"
339 send_user "Looking to match \"$pattern\"\n"
340 send_user "Message is \"$message\"\n"
344 if ![string match $command ""] {
345 send_gdb "$command\n"
349 -re ".*Ending remote debugging.*$prompt$" {
350 if ![isnative] then {
351 warning "Can`t communicate to remote target."
357 -re "\[\r\n\]*$pattern\[\r\n\]+$prompt $" {
358 if ![string match "" $message] then {
363 -re "${question_string}$" {
364 send_gdb "$response_string\n";
367 -re "Undefined command:.*$prompt" {
368 perror "Undefined command \"$command\"."
371 -re "Ambiguous command.*$prompt $" {
372 perror "\"$command\" is not a unique command name."
375 -re ".*Program exited with code \[0-9\]+.*$prompt $" {
376 if ![string match "" $message] then {
377 set errmsg "$message: the program exited"
379 set errmsg "$command: the program exited"
384 -re "The program is not being run.*$prompt $" {
385 if ![string match "" $message] then {
386 set errmsg "$message: the program is no longer running"
388 set errmsg "$command: the program is no longer running"
394 if ![string match "" $message] then {
401 perror "Window too small."
403 -re "\\(y or n\\) " {
405 perror "Got interactive prompt."
408 perror "Process no longer exists"
412 perror "internal buffer is full."
415 if ![string match "" $message] then {
416 fail "(timeout) $message"
424 # Test that a command gives an error. For pass or fail, return
425 # a 1 to indicate that more tests can proceed. However a timeout
426 # is a serious error, generates a special fail message, and causes
427 # a 0 to be returned to indicate that more tests are likely to fail
430 proc test_print_reject { args } {
434 if [llength $args]==2 then {
435 set expectthis [lindex $args 1]
437 set expectthis "should never match this bogus string"
439 set sendthis [lindex $args 0]
441 send_user "Sending \"$sendthis\" to gdb\n"
442 send_user "Looking to match \"$expectthis\"\n"
444 send_gdb "$sendthis\n"
446 -re ".*A .* in expression.*\\.*$prompt $" {
447 pass "reject $sendthis"
450 -re ".*Invalid syntax in expression.*$prompt $" {
451 pass "reject $sendthis"
454 -re ".*Junk after end of expression.*$prompt $" {
455 pass "reject $sendthis"
458 -re ".*Invalid number.*$prompt $" {
459 pass "reject $sendthis"
462 -re ".*Invalid character constant.*$prompt $" {
463 pass "reject $sendthis"
466 -re ".*No symbol table is loaded.*$prompt $" {
467 pass "reject $sendthis"
470 -re ".*No symbol .* in current context.*$prompt $" {
471 pass "reject $sendthis"
474 -re ".*$expectthis.*$prompt $" {
475 pass "reject $sendthis"
479 fail "reject $sendthis"
483 fail "reject $sendthis (eof or timeout)"
489 # Given an input string, adds backslashes as needed to create a
490 # regexp that will match the string.
492 proc string_to_regexp {str} {
494 regsub -all {[]*+.|()^$\[]} $str {\\&} result
498 # Same as gdb_test, but the second parameter is not a regexp,
499 # but a string that must match exactly.
501 proc gdb_test_exact { args } {
502 upvar timeout timeout
504 set command [lindex $args 0]
506 # This applies a special meaning to a null string pattern. Without
507 # this, "$pattern\r\n$prompt $" will match anything, including error
508 # messages from commands that should have no output except a new
509 # prompt. With this, only results of a null string will match a null
512 set pattern [lindex $args 1]
513 if [string match $pattern ""] {
514 set pattern [string_to_regexp [lindex $args 0]]
516 set pattern [string_to_regexp [lindex $args 1]]
519 # It is most natural to write the pattern argument with only
520 # embedded \n's, especially if you are trying to avoid Tcl quoting
521 # problems. But expect really wants to see \r\n in patterns. So
522 # transform the pattern here. First transform \r\n back to \n, in
523 # case some users of gdb_test_exact already do the right thing.
524 regsub -all "\r\n" $pattern "\n" pattern
525 regsub -all "\n" $pattern "\r\n" pattern
526 if [llength $args]==3 then {
527 set message [lindex $args 2]
532 return [gdb_test $command $pattern $message]
535 proc gdb_reinitialize_dir { subdir } {
538 set spawn_id $gdb_spawn_id
540 if [is_remote host] {
545 -re "Reinitialize source path to empty.*y or n. " {
548 -re "Source directories searched.*$prompt $" {
549 send_gdb "dir $subdir\n"
551 -re "Source directories searched.*$prompt $" {
552 verbose "Dir set to $subdir"
555 perror "Dir \"$subdir\" failed."
560 perror "Dir \"$subdir\" failed."
565 perror "Dir \"$subdir\" failed."
571 # gdb_exit -- exit the GDB, killing the target program if necessary
573 proc default_gdb_exit {} {
579 if ![info exists gdb_spawn_id] {
583 verbose "Quitting $GDB $GDBFLAGS"
585 # This used to be 1 for unix-gdb.exp
587 verbose "Timeout is now $timeout seconds" 2
589 if [is_remote host] {
592 -i $gdb_spawn_id -re ".*and kill it.*y or n. " {
596 -i $gdb_spawn_id timeout { }
599 # We used to try to send_gdb "quit" to GDB, and wait for it to die.
600 # Dealing with all the cases and errors got pretty hairy. Just close it,
602 catch "close -i $gdb_spawn_id"
604 # Omitting this probably would cause strange timing-dependent failures.
605 catch "wait -i $gdb_spawn_id"
613 # load a file into the debugger.
614 # return a -1 if anything goes wrong.
616 proc gdb_file_cmd { arg } {
622 upvar timeout timeout
624 set spawn_id $gdb_spawn_id
626 if [is_remote host] {
627 set arg [remote_download host $arg];
629 error "download failed"
634 send_gdb "file $arg\n"
636 -re "Reading symbols from.*done.*$prompt $" {
637 verbose "\t\tLoaded $arg into the $GDB"
640 -re "has no symbol-table.*$prompt $" {
641 perror "$arg wasn't compiled with \"-g\""
644 -re "A program is being debugged already.*Kill it.*y or n. $" {
646 verbose "\t\tKilling previous program being debugged"
649 -re "Load new symbol table from \".*\".*y or n. $" {
652 -re "Reading symbols from.*done.*$prompt $" {
653 verbose "\t\tLoaded $arg with new symbol table into $GDB"
657 perror "(timeout) Couldn't load $arg, other program already l
663 -re ".*No such file or directory.*$prompt $" {
664 perror "($arg) No such file or directory\n"
668 perror "couldn't load $arg into $GDB."
672 perror "couldn't load $arg into $GDB (timed out)."
676 # This is an attempt to detect a core dump, but seems not to
677 # work. Perhaps we need to match .* followed by eof, in which
678 # expect does not seem to have a way to do that.
679 perror "couldn't load $arg into $GDB (end of file)."
686 # start gdb -- start gdb running, default procedure
688 # When running over NFS, particularly if running many simultaneous
689 # tests on different hosts all using the same server, things can
690 # get really slow. Give gdb at least 3 minutes to start up.
692 proc default_gdb_start { } {
700 verbose "Spawning $GDB -nw $GDBFLAGS"
702 if [info exists gdb_spawn_id] {
706 set oldtimeout $timeout
707 set timeout [expr "$timeout + 180"]
708 if [is_remote host] {
709 set shell_id [remote_spawn host "$GDB -nw $GDBFLAGS --command gdbinit"]
711 if { [which $GDB] == 0 } then {
712 perror "$GDB does not exist."
716 set shell_id [remote_spawn host "$GDB -nw $GDBFLAGS"]
721 -i $shell_id -re ".*\[\r\n\]$prompt $" {
722 verbose "GDB initialized."
724 -i $shell_id -re "$prompt $" {
725 perror "GDB never initialized."
726 set timeout $oldtimeout
727 verbose "Timeout restored to $timeout seconds" 2
730 -i $shell_id timeout {
731 perror "(timeout) GDB never initialized after $timeout seconds."
732 set timeout $oldtimeout
733 verbose "Timeout restored to $timeout seconds" 2
737 set timeout $oldtimeout
738 verbose "Timeout restored to $timeout seconds" 2
739 set gdb_spawn_id $shell_id
740 set spawn_id $gdb_spawn_id
741 # force the height to "unlimited", so no pagers get used
742 send_gdb "set height 0\n"
744 -i $shell_id -re ".*$prompt $" {
745 verbose "Setting height to 0." 2
747 -i $shell_id timeout {
748 warning "Couldn't set the height to 0"
751 # force the width to "unlimited", so no wraparound occurs
752 send_gdb "set width 0\n"
754 -i $shell_id -re ".*$prompt $" {
755 verbose "Setting width to 0." 2
757 -i $shell_id timeout {
758 warning "Couldn't set the width to 0."
765 # FIXME: this is a copy of the new library procedure, but it's here too
766 # till the new dejagnu gets installed everywhere. I'd hate to break the
770 if ![info exists argv0] then {
771 proc exp_continue { } {
776 # * For crosses, the CHILL runtime doesn't build because it can't find
777 # setjmp.h, stdio.h, etc.
778 # * For AIX (as of 16 Mar 95), (a) there is no language code for
779 # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
780 # does not get along with AIX's too-clever linker.
781 # * On Irix5, there is a bug whereby set of bool, etc., don't get
782 # TYPE_LOW_BOUND for the bool right because force_to_range_type doesn't
783 # work with stub types.
784 # Lots of things seem to fail on the PA, and since it's not a supported
785 # chill target at the moment, don't run the chill tests.
787 proc skip_chill_tests {} {
788 if ![info exists do_chill_tests] {
791 eval set skip_chill [expr ![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-irix5*"] || [istarget "*-*-irix6*"] || [istarget "alpha-*-osf*"] || [istarget "hppa*-*-*"]]
792 verbose "Skip chill tests is $skip_chill"
796 proc get_compiler_info {binfile} {
797 # Create and source the file that provides information about the compiler
798 # used to compile the test case.
801 # These two come from compiler.c.
802 global signed_keyword_not_used
805 if { [gdb_compile "${srcdir}/${subdir}/compiler.c" "${binfile}.ci" preprocess {}] != "" } {
806 perror "Couldn't make ${binfile}.ci file"
813 proc gdb_compile {source dest type options} {
814 if [target_info exists gdb_stub] {
815 set options2 { "additional_flags=-Dusestubs" }
816 lappend options "libs=[target_info gdb_stub]";
817 set options [concat $options2 $options]
819 verbose "options are $options"
820 verbose "source is $source $dest $type $options"
821 set result [target_compile $source $dest $type $options];
822 regsub "\[\r\n\]*$" "$result" "" result;
823 regsub "^\[\r\n\]*" "$result" "" result;
824 if { $result != "" } {
825 clone_output "gdb compile failed, $result"
830 proc send_gdb { string } {
831 return [remote_send host "$string"];
839 catch default_gdb_exit
843 # gdb_load -- load a file into the debugger.
844 # return a -1 if anything goes wrong.
846 proc gdb_load { arg } {
847 return [gdb_file_cmd $arg]
850 proc gdb_continue { function } {
853 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
856 proc gdb_finish { } {