1 # Copyright (C) 1992, 1994, 1995, 1997 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:
18 # bug-gdb@prep.ai.mit.edu
20 # This file was written by Fred Fish. (fnf@cygnus.com)
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 TOOL_EXECUTABLE] {
42 set GDB $TOOL_EXECUTABLE;
44 if ![info exists GDB] {
45 if ![is_remote host] {
46 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
48 set GDB [transform gdb];
51 verbose "using GDB = $GDB" 2
54 if ![info exists GDBFLAGS] {
57 verbose "using GDBFLAGS = $GDBFLAGS" 2
59 # The variable prompt is a regexp which matches the gdb prompt. Set it if it
62 if ![info exists prompt] then {
63 set gdb_prompt "\[(\]gdb\[)\]"
67 # gdb_version -- extract and print the version number of GDB
69 proc default_gdb_version {} {
73 set fileid [open "gdb_cmd" w];
76 set cmdfile [remote_download host "gdb_cmd"];
77 set output [remote_exec host "$GDB -nw --command $cmdfile"]
78 remote_file build delete "gdb_cmd";
79 remote_file host delete "$cmdfile";
80 set tmp [lindex $output 1];
82 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
83 if ![is_remote host] {
84 clone_output "[which $GDB] version $version $GDBFLAGS\n"
86 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
90 proc gdb_version { } {
91 return [default_gdb_version];
95 # gdb_unload -- unload a file if one is loaded
104 -re "No exec file now\[^\r\n\]*\[\r\n\]" { exp_continue }
105 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
106 -re "A program is being debugged already..*Kill it.*y or n. $"\
108 verbose "\t\tKilling previous program being debugged"
111 -re "Discard symbol table from .*y or n.*$" {
115 -re "$gdb_prompt $" {}
117 perror "couldn't unload file in $GDB (timed out)."
123 # Many of the tests depend on setting breakpoints at various places and
124 # running until that breakpoint is reached. At times, we want to start
125 # with a clean-slate with respect to breakpoints, so this utility proc
126 # lets us do this without duplicating this code everywhere.
129 proc delete_breakpoints {} {
132 send_gdb "delete breakpoints\n"
134 -re "Delete all breakpoints.*y or n.*$" {
138 -re "$gdb_prompt $" { # This happens if there were no breakpoints
140 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
142 send_gdb "info breakpoints\n"
144 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
145 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
146 -re "Delete all breakpoints.*or n.*$" {
150 timeout { perror "info breakpoints (timeout)" ; return }
156 # Generic run command.
158 # The second pattern below matches up to the first newline *only*.
159 # Using ``.*$'' could swallow up output that we attempt to match
162 proc gdb_run_cmd {args} {
165 if [target_info exists gdb_init_command] {
166 send_gdb "[target_info gdb_init_command]\n";
168 -re "$gdb_prompt $" { }
170 perror "gdb_init_command for target failed";
176 if [target_info exists use_gdb_stub] {
177 if [target_info exists gdb,do_reload_on_run] {
178 # According to Stu, this will always work.
180 send_gdb "continue\n";
182 -re "Continu\[^\r\n\]*\[\r\n\]" {}
188 if [target_info exists gdb,start_symbol] {
189 set start [target_info gdb,start_symbol];
193 send_gdb "jump *$start\n"
195 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
196 if ![target_info exists gdb_stub] {
200 -re "No symbol \"start\" in current.*$gdb_prompt $" {
201 send_gdb "jump *_start\n";
204 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
205 perror "Can't find start symbol to run in gdb_run";
208 -re "Line.* Jump anyway.*y or n. $" {
212 -re "No symbol.*context.*$gdb_prompt $" {}
213 -re "The program is not being run.*$gdb_prompt $" {
215 send_gdb "jump *$start\n";
218 timeout { perror "Jump to start() failed (timeout)"; return }
220 if [target_info exists gdb_stub] {
222 -re "$gdb_prompt $" {
223 send_gdb "continue\n"
229 send_gdb "run $args\n"
230 # This doesn't work quite right yet.
232 -re "The program .* has been started already.*y or n. $" {
236 -re "Starting program: \[^\r\n\]*" {}
240 proc gdb_breakpoint { function } {
244 send_gdb "break $function\n"
245 # The first two regexps are what we get with -g, the third is without -g.
247 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
248 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
249 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
250 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
251 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
256 # Set breakpoint at function and run gdb until it breaks there.
257 # Since this is the only breakpoint that will be set, if it stops
258 # at a breakpoint, we will assume it is the one we want. We can't
259 # just compare to "function" because it might be a fully qualified,
260 # single quoted C++ function specifier.
262 proc runto { function } {
268 if ![gdb_breakpoint $function] {
274 # the "at foo.c:36" output we get with -g.
275 # the "in func" output we get without -g.
277 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
280 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
283 -re "$gdb_prompt $" {
284 fail "running to $function in runto"
288 fail "running to $function in runto (timeout)"
296 # runto_main -- ask gdb to run until we hit a breakpoint at main.
297 # The case where the target uses stubs has to be handled
298 # specially--if it uses stubs, assuming we hit
299 # breakpoint() and just step out of the function.
301 proc runto_main { } {
305 if ![target_info exists gdb_stub] {
317 # gdb_test -- send_gdb a command to gdb and test the result.
318 # Takes three parameters.
320 # First one is the command to execute. If this is the null string
321 # then no command is sent.
322 # Second one is the pattern to match for a PASS, and must NOT include
323 # the \r\n sequence immediately before the gdb prompt.
324 # Third one is an optional message to be printed. If this
325 # a null string "", then the pass/fail messages use the command
326 # string as the message.
328 # 1 if the test failed,
329 # 0 if the test passes,
330 # -1 if there was an internal error.
332 proc gdb_test { args } {
336 upvar timeout timeout
338 if [llength $args]>2 then {
339 set message [lindex $args 2]
341 set message [lindex $args 0]
343 set command [lindex $args 0]
344 set pattern [lindex $args 1]
346 if [llength $args]==5 {
347 set question_string [lindex $args 3];
348 set response_string [lindex $args 4];
350 set question_string "^FOOBAR$"
354 send_user "Sending \"$command\" to gdb\n"
355 send_user "Looking to match \"$pattern\"\n"
356 send_user "Message is \"$message\"\n"
360 if ![string match $command ""] {
361 if { [send_gdb "$command\n"] != "" } {
362 global suppress_flag;
364 if { ! $suppress_flag } {
365 perror "Couldn't send $command to GDB.";
372 if [info exists timeout] {
376 if [info exists timeout] {
383 -re "Ending remote debugging.*$gdb_prompt$" {
384 if ![isnative] then {
385 warning "Can`t communicate to remote target."
391 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
392 if ![string match "" $message] then {
397 -re "(${question_string})$" {
398 send_gdb "$response_string\n";
401 -re "Undefined command:.*$gdb_prompt" {
402 perror "Undefined command \"$command\"."
405 -re "Ambiguous command.*$gdb_prompt $" {
406 perror "\"$command\" is not a unique command name."
409 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
410 if ![string match "" $message] then {
411 set errmsg "$message: the program exited"
413 set errmsg "$command: the program exited"
418 -re "The program is not being run.*$gdb_prompt $" {
419 if ![string match "" $message] then {
420 set errmsg "$message: the program is no longer running"
422 set errmsg "$command: the program is no longer running"
427 -re ".*$gdb_prompt $" {
428 if ![string match "" $message] then {
435 perror "Window too small."
437 -re "\\(y or n\\) " {
439 perror "Got interactive prompt."
442 perror "Process no longer exists"
443 if { $message != "" } {
449 perror "internal buffer is full."
452 if ![string match "" $message] then {
453 fail "$message (timeout)"
461 # Test that a command gives an error. For pass or fail, return
462 # a 1 to indicate that more tests can proceed. However a timeout
463 # is a serious error, generates a special fail message, and causes
464 # a 0 to be returned to indicate that more tests are likely to fail
467 proc test_print_reject { args } {
471 if [llength $args]==2 then {
472 set expectthis [lindex $args 1]
474 set expectthis "should never match this bogus string"
476 set sendthis [lindex $args 0]
478 send_user "Sending \"$sendthis\" to gdb\n"
479 send_user "Looking to match \"$expectthis\"\n"
481 send_gdb "$sendthis\n"
482 #FIXME: Should add timeout as parameter.
484 -re "A .* in expression.*\\.*$gdb_prompt $" {
485 pass "reject $sendthis"
488 -re "Invalid syntax in expression.*$gdb_prompt $" {
489 pass "reject $sendthis"
492 -re "Junk after end of expression.*$gdb_prompt $" {
493 pass "reject $sendthis"
496 -re "Invalid number.*$gdb_prompt $" {
497 pass "reject $sendthis"
500 -re "Invalid character constant.*$gdb_prompt $" {
501 pass "reject $sendthis"
504 -re "No symbol table is loaded.*$gdb_prompt $" {
505 pass "reject $sendthis"
508 -re "No symbol .* in current context.*$gdb_prompt $" {
509 pass "reject $sendthis"
512 -re "$expectthis.*$gdb_prompt $" {
513 pass "reject $sendthis"
516 -re ".*$gdb_prompt $" {
517 fail "reject $sendthis"
521 fail "reject $sendthis (eof or timeout)"
527 # Given an input string, adds backslashes as needed to create a
528 # regexp that will match the string.
530 proc string_to_regexp {str} {
532 regsub -all {[]*+.|()^$\[]} $str {\\&} result
536 # Same as gdb_test, but the second parameter is not a regexp,
537 # but a string that must match exactly.
539 proc gdb_test_exact { args } {
540 upvar timeout timeout
542 set command [lindex $args 0]
544 # This applies a special meaning to a null string pattern. Without
545 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
546 # messages from commands that should have no output except a new
547 # prompt. With this, only results of a null string will match a null
550 set pattern [lindex $args 1]
551 if [string match $pattern ""] {
552 set pattern [string_to_regexp [lindex $args 0]]
554 set pattern [string_to_regexp [lindex $args 1]]
557 # It is most natural to write the pattern argument with only
558 # embedded \n's, especially if you are trying to avoid Tcl quoting
559 # problems. But gdb_expect really wants to see \r\n in patterns. So
560 # transform the pattern here. First transform \r\n back to \n, in
561 # case some users of gdb_test_exact already do the right thing.
562 regsub -all "\r\n" $pattern "\n" pattern
563 regsub -all "\n" $pattern "\r\n" pattern
564 if [llength $args]==3 then {
565 set message [lindex $args 2]
570 return [gdb_test $command $pattern $message]
573 proc gdb_reinitialize_dir { subdir } {
576 if [is_remote host] {
581 -re "Reinitialize source path to empty.*y or n. " {
584 -re "Source directories searched.*$gdb_prompt $" {
585 send_gdb "dir $subdir\n"
587 -re "Source directories searched.*$gdb_prompt $" {
588 verbose "Dir set to $subdir"
590 -re "$gdb_prompt $" {
591 perror "Dir \"$subdir\" failed."
595 -re "$gdb_prompt $" {
596 perror "Dir \"$subdir\" failed."
600 -re "$gdb_prompt $" {
601 perror "Dir \"$subdir\" failed."
607 # gdb_exit -- exit the GDB, killing the target program if necessary
609 proc default_gdb_exit {} {
615 gdb_stop_suppressing_tests;
617 if ![info exists gdb_spawn_id] {
621 verbose "Quitting $GDB $GDBFLAGS"
623 if { [is_remote host] && [board_info host exists fileid] } {
626 -re "and kill it.*y or n. " {
630 -re "DOSEXIT code" { }
640 # load a file into the debugger.
641 # return a -1 if anything goes wrong.
643 proc gdb_file_cmd { arg } {
649 upvar timeout timeout
651 if [is_remote host] {
652 set arg [remote_download host $arg];
654 error "download failed"
659 send_gdb "file $arg\n"
661 -re "Reading symbols from.*done.*$gdb_prompt $" {
662 verbose "\t\tLoaded $arg into the $GDB"
665 -re "has no symbol-table.*$gdb_prompt $" {
666 perror "$arg wasn't compiled with \"-g\""
669 -re "A program is being debugged already.*Kill it.*y or n. $" {
671 verbose "\t\tKilling previous program being debugged"
674 -re "Load new symbol table from \".*\".*y or n. $" {
677 -re "Reading symbols from.*done.*$gdb_prompt $" {
678 verbose "\t\tLoaded $arg with new symbol table into $GDB"
682 perror "(timeout) Couldn't load $arg, other program already loaded."
687 -re "No such file or directory.*$gdb_prompt $" {
688 perror "($arg) No such file or directory\n"
691 -re "$gdb_prompt $" {
692 perror "couldn't load $arg into $GDB."
696 perror "couldn't load $arg into $GDB (timed out)."
700 # This is an attempt to detect a core dump, but seems not to
701 # work. Perhaps we need to match .* followed by eof, in which
702 # gdb_expect does not seem to have a way to do that.
703 perror "couldn't load $arg into $GDB (end of file)."
710 # start gdb -- start gdb running, default procedure
712 # When running over NFS, particularly if running many simultaneous
713 # tests on different hosts all using the same server, things can
714 # get really slow. Give gdb at least 3 minutes to start up.
716 proc default_gdb_start { } {
724 gdb_stop_suppressing_tests;
726 verbose "Spawning $GDB -nw $GDBFLAGS"
728 if [info exists gdb_spawn_id] {
732 if ![is_remote host] {
733 if { [which $GDB] == 0 } then {
734 perror "$GDB does not exist."
738 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
739 if { $res < 0 || $res == "" } {
740 perror "Spawning $GDB failed."
744 -re "\[\r\n\]$gdb_prompt $" {
745 verbose "GDB initialized."
747 -re "$gdb_prompt $" {
748 perror "GDB never initialized."
752 perror "(timeout) GDB never initialized after 10 seconds."
758 # force the height to "unlimited", so no pagers get used
760 send_gdb "set height 0\n"
762 -re "$gdb_prompt $" {
763 verbose "Setting height to 0." 2
766 warning "Couldn't set the height to 0"
769 # force the width to "unlimited", so no wraparound occurs
770 send_gdb "set width 0\n"
772 -re "$gdb_prompt $" {
773 verbose "Setting width to 0." 2
776 warning "Couldn't set the width to 0."
782 # * For crosses, the CHILL runtime doesn't build because it can't find
783 # setjmp.h, stdio.h, etc.
784 # * For AIX (as of 16 Mar 95), (a) there is no language code for
785 # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
786 # does not get along with AIX's too-clever linker.
787 # * On Irix5, there is a bug whereby set of bool, etc., don't get
788 # TYPE_LOW_BOUND for the bool right because force_to_range_type doesn't
789 # work with stub types.
790 # Lots of things seem to fail on the PA, and since it's not a supported
791 # chill target at the moment, don't run the chill tests.
793 proc skip_chill_tests {} {
794 if ![info exists do_chill_tests] {
797 eval set skip_chill [expr ![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-irix5*"] || [istarget "*-*-irix6*"] || [istarget "alpha-*-osf*"] || [istarget "hppa*-*-*"]]
798 verbose "Skip chill tests is $skip_chill"
802 proc get_compiler_info {binfile} {
803 # Create and source the file that provides information about the compiler
804 # used to compile the test case.
807 # These two come from compiler.c.
808 global signed_keyword_not_used
811 if { [gdb_compile "${srcdir}/${subdir}/compiler.c" "${binfile}.ci" preprocess {}] != "" } {
812 perror "Couldn't make ${binfile}.ci file"
819 proc gdb_compile {source dest type options} {
820 global GDB_TESTCASE_OPTIONS;
822 if [target_info exists gdb_stub] {
823 set options2 { "additional_flags=-Dusestubs" }
824 lappend options "libs=[target_info gdb_stub]";
825 set options [concat $options2 $options]
827 if [info exists GDB_TESTCASE_OPTIONS] {
828 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
830 verbose "options are $options"
831 verbose "source is $source $dest $type $options"
832 set result [target_compile $source $dest $type $options];
833 regsub "\[\r\n\]*$" "$result" "" result;
834 regsub "^\[\r\n\]*" "$result" "" result;
835 if { $result != "" } {
836 clone_output "gdb compile failed, $result"
841 proc send_gdb { string } {
842 global suppress_flag;
843 if { $suppress_flag } {
846 return [remote_send host "$string"];
852 proc gdb_expect { args } {
853 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
854 set gtimeout [lindex $args 0];
855 set expcode [list [lindex $args 1]];
857 upvar timeout timeout;
860 if [target_info exists gdb,timeout] {
861 if [info exists timeout] {
862 if { $timeout < [target_info gdb,timeout] } {
863 set gtimeout [target_info gdb,timeout];
865 set gtimeout $timeout;
868 set gtimeout [target_info gdb,timeout];
872 if ![info exists gtimeout] {
874 if [info exists timeout] {
875 set gtimeout $timeout;
882 set code [catch {uplevel remote_expect host $gtimeout $expcode} string];
885 global errorInfo errorCode;
887 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
888 } elseif {$code == 2} {
889 return -code return $string
890 } elseif {$code == 3} {
892 } elseif {$code > 4} {
893 return -code $code $string
897 proc gdb_suppress_entire_file { reason } {
898 global suppress_flag;
901 set suppress_flag -1;
905 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
906 # gdb_expect to fail immediately (until the next call to
907 # gdb_stop_suppressing_tests).
909 proc gdb_suppress_tests { args } {
910 global suppress_flag;
914 if { $suppress_flag == 1 } {
915 if { [llength $args] > 0 } {
916 warning "[lindex $args 0]\n";
918 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
924 # Clear suppress_flag.
926 proc gdb_stop_suppressing_tests { } {
927 global suppress_flag;
929 if [info exists suppress_flag] {
930 if { $suppress_flag > 0 } {
932 clone_output "Tests restarted.\n";
939 proc gdb_clear_suppressed { } {
940 global suppress_flag;
950 catch default_gdb_exit
954 # gdb_load -- load a file into the debugger.
955 # return a -1 if anything goes wrong.
957 proc gdb_load { arg } {
958 return [gdb_file_cmd $arg]
961 proc gdb_continue { function } {
964 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
967 proc default_gdb_init { args } {
968 gdb_clear_suppressed;
970 # Uh, this is lame. Really, really, really lame. But there's this *one*
971 # testcase that will fail in random places if we don't increase this.
974 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
975 if { [llength $args] > 0 } {
978 set file [lindex $args 0];
980 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
983 if [target_info exists gdb_prompt] {
984 set gdb_prompt [target_info gdb_prompt];
986 set gdb_prompt "\\(gdb\\)"
990 proc gdb_init { args } {
991 return [eval default_gdb_init $args];
994 proc gdb_finish { } {
1000 # Run the gdb command "info source" and extract the debugging format information
1001 # from the output and save it in debug_format.
1003 proc get_debug_format { } {
1009 set debug_format "unknown"
1010 send_gdb "info source\n"
1012 -re "Compiled with (.*) debugging format.\r\n$gdb_prompt $" {
1013 set debug_format $expect_out(1,string)
1014 verbose "debug format is $debug_format"
1017 -re "No current source file.\r\n$gdb_prompt $" {
1018 perror "get_debug_format used when no current source file"
1021 -re "$gdb_prompt $" {
1022 warning "couldn't check debug format (no valid response)."
1026 warning "couldn't check debug format (timed out)."
1032 # Like setup_xfail, but takes the name of a debug format (DWARF 1, COFF, stabs, etc).
1033 # If that format matches the format that the current test was compiled with, then
1034 # the next test is expected to fail for any target. Returns 1 if the next test or
1035 # set of tests is expected to fail, 0 otherwise (or if it is unknown). Must
1036 # have previously called get_debug_format.
1038 proc setup_xfail_format { format } {
1041 if [string match $debug_format $format] then {
1048 proc gdb_step_for_stub { } {
1051 if [target_info exists gdb_stub_step_command] {
1052 set command [target_info gdb_stub_step_command];
1056 send_gdb "${command}\n";
1059 -re "(main.* at |.*in .*start).*$gdb_prompt" {
1062 -re "libgloss/\[a-z\]*\[0-9\]*/stub.c" {
1065 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
1066 set file $expect_out(1,string);
1067 set linenum [expr $expect_out(2,string) + 1];
1068 set breakplace "${file}:${linenum}";
1072 send_gdb "break ${breakplace}\n";
1074 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
1075 set breakpoint $expect_out(1,string);
1077 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
1078 set breakpoint $expect_out(1,string);
1082 send_gdb "continue\n";
1084 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
1085 gdb_test "delete $breakpoint" ".*" "";
1091 -re ".*$gdb_prompt" {
1093 if { $tries == 5 } {
1094 fail "stepping out of breakpoint function";
1097 send_gdb "${command}\n";
1101 fail "stepping out of breakpoint function";