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 GDB] {
42 if ![is_remote host] {
43 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
45 set 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 gdb_prompt "\[(\]gdb\[)\]"
64 # gdb_version -- extract and print the version number of GDB
66 proc default_gdb_version {} {
70 set fileid [open "gdb_cmd" w];
73 set cmdfile [remote_download host "gdb_cmd"];
74 set output [remote_exec host "$GDB -nw --command $cmdfile"]
75 remote_file build delete "gdb_cmd";
76 remote_file host delete "$cmdfile";
77 set tmp [lindex $output 1];
79 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
80 if ![is_remote host] {
81 clone_output "[which $GDB] version $version $GDBFLAGS\n"
83 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
87 proc gdb_version { } {
88 return [default_gdb_version];
92 # gdb_unload -- unload a file if one is loaded
101 -re "No exec file now\[^\r\n\]*\[\r\n\]" { exp_continue }
102 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
103 -re "A program is being debugged already..*Kill it.*y or n. $"\
105 verbose "\t\tKilling previous program being debugged"
108 -re "Discard symbol table from .*y or n.*$" {
112 -re "$gdb_prompt $" {}
114 perror "couldn't unload file in $GDB (timed out)."
120 # Many of the tests depend on setting breakpoints at various places and
121 # running until that breakpoint is reached. At times, we want to start
122 # with a clean-slate with respect to breakpoints, so this utility proc
123 # lets us do this without duplicating this code everywhere.
126 proc delete_breakpoints {} {
129 send_gdb "delete breakpoints\n"
131 -re "Delete all breakpoints.*y or n.*$" {
135 -re "$gdb_prompt $" { # This happens if there were no breakpoints
137 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
139 send_gdb "info breakpoints\n"
141 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
142 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
143 -re "Delete all breakpoints.*or n.*$" {
147 timeout { perror "info breakpoints (timeout)" ; return }
153 # Generic run command.
155 # The second pattern below matches up to the first newline *only*.
156 # Using ``.*$'' could swallow up output that we attempt to match
159 proc gdb_run_cmd {args} {
162 if [target_info exists gdb_init_command] {
163 send_gdb "[target_info gdb_init_command]\n";
165 -re "$gdb_prompt $" { }
167 perror "gdb_init_command for target failed";
173 if [target_info exists use_gdb_stub] {
174 if [target_info exists gdb,start_symbol] {
175 set start [target_info gdb,start_symbol];
179 send_gdb "jump *$start\n"
181 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
182 if ![target_info exists gdb_stub] {
186 -re "No symbol \"start\" in current.*$gdb_prompt $" {
187 send_gdb "jump *_start\n";
190 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
191 perror "Can't find start symbol to run in gdb_run";
194 -re "Line.* Jump anyway.*y or n. $" {
198 -re "No symbol.*context.*$gdb_prompt $" {}
199 -re "The program is not being run.*$gdb_prompt $" {
201 send_gdb "jump *$start\n";
204 timeout { perror "Jump to start() failed (timeout)"; return }
206 if [target_info exists gdb_stub] {
208 -re "$gdb_prompt $" {
209 send_gdb "continue\n"
215 send_gdb "run $args\n"
216 # This doesn't work quite right yet.
218 -re "The program .* has been started already.*y or n. $" {
222 -re "Starting program: \[^\r\n\]*" {}
226 proc gdb_breakpoint { function } {
230 send_gdb "break $function\n"
231 # The first two regexps are what we get with -g, the third is without -g.
233 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
234 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
235 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
236 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
237 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
242 # Set breakpoint at function and run gdb until it breaks there.
243 # Since this is the only breakpoint that will be set, if it stops
244 # at a breakpoint, we will assume it is the one we want. We can't
245 # just compare to "function" because it might be a fully qualified,
246 # single quoted C++ function specifier.
248 proc runto { function } {
254 if ![gdb_breakpoint $function] {
260 # the "at foo.c:36" output we get with -g.
261 # the "in func" output we get without -g.
263 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
266 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
269 -re "$gdb_prompt $" {
270 fail "running to $function in runto"
274 fail "running to $function in runto (timeout)"
282 # runto_main -- ask gdb to run until we hit a breakpoint at main.
283 # The case where the target uses stubs has to be handled
284 # specially--if it uses stubs, assuming we hit
285 # breakpoint() and just step out of the function.
291 if ![target_info exists gdb_stub] {
298 # if use stubs step out of the breakpoint() function.
300 -re "main.* at .*$gdb_prompt $" {}
301 -re "_start.*$gdb_prompt $" {}
302 timeout { fail "single step at breakpoint() (timeout)" ; return 0 }
308 # gdb_test -- send_gdb a command to gdb and test the result.
309 # Takes three parameters.
311 # First one is the command to execute. If this is the null string
312 # then no command is sent.
313 # Second one is the pattern to match for a PASS, and must NOT include
314 # the \r\n sequence immediately before the gdb prompt.
315 # Third one is an optional message to be printed. If this
316 # a null string "", then the pass/fail messages use the command
317 # string as the message.
319 # 1 if the test failed,
320 # 0 if the test passes,
321 # -1 if there was an internal error.
323 proc gdb_test { args } {
327 upvar timeout timeout
329 if [llength $args]>2 then {
330 set message [lindex $args 2]
332 set message [lindex $args 0]
334 set command [lindex $args 0]
335 set pattern [lindex $args 1]
337 if [llength $args]==5 {
338 set question_string [lindex $args 3];
339 set response_string [lindex $args 4];
341 set question_string "^FOOBAR$"
345 send_user "Sending \"$command\" to gdb\n"
346 send_user "Looking to match \"$pattern\"\n"
347 send_user "Message is \"$message\"\n"
351 if ![string match $command ""] {
352 if { [send_gdb "$command\n"] != "" } {
353 global suppress_flag;
355 if { ! $suppress_flag } {
356 perror "Couldn't send $command to GDB.";
364 -re "Ending remote debugging.*$gdb_prompt$" {
365 if ![isnative] then {
366 warning "Can`t communicate to remote target."
372 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
373 if ![string match "" $message] then {
378 -re "(${question_string})$" {
379 send_gdb "$response_string\n";
382 -re "Undefined command:.*$gdb_prompt" {
383 perror "Undefined command \"$command\"."
386 -re "Ambiguous command.*$gdb_prompt $" {
387 perror "\"$command\" is not a unique command name."
390 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
391 if ![string match "" $message] then {
392 set errmsg "$message: the program exited"
394 set errmsg "$command: the program exited"
399 -re "The program is not being run.*$gdb_prompt $" {
400 if ![string match "" $message] then {
401 set errmsg "$message: the program is no longer running"
403 set errmsg "$command: the program is no longer running"
408 -re ".*$gdb_prompt $" {
409 if ![string match "" $message] then {
416 perror "Window too small."
418 -re "\\(y or n\\) " {
420 perror "Got interactive prompt."
423 perror "Process no longer exists"
424 if { $message != "" } {
430 perror "internal buffer is full."
433 if ![string match "" $message] then {
434 fail "$message (timeout)"
442 # Test that a command gives an error. For pass or fail, return
443 # a 1 to indicate that more tests can proceed. However a timeout
444 # is a serious error, generates a special fail message, and causes
445 # a 0 to be returned to indicate that more tests are likely to fail
448 proc test_print_reject { args } {
452 if [llength $args]==2 then {
453 set expectthis [lindex $args 1]
455 set expectthis "should never match this bogus string"
457 set sendthis [lindex $args 0]
459 send_user "Sending \"$sendthis\" to gdb\n"
460 send_user "Looking to match \"$expectthis\"\n"
462 send_gdb "$sendthis\n"
463 #FIXME: Should add timeout as parameter.
465 -re "A .* in expression.*\\.*$gdb_prompt $" {
466 pass "reject $sendthis"
469 -re "Invalid syntax in expression.*$gdb_prompt $" {
470 pass "reject $sendthis"
473 -re "Junk after end of expression.*$gdb_prompt $" {
474 pass "reject $sendthis"
477 -re "Invalid number.*$gdb_prompt $" {
478 pass "reject $sendthis"
481 -re "Invalid character constant.*$gdb_prompt $" {
482 pass "reject $sendthis"
485 -re "No symbol table is loaded.*$gdb_prompt $" {
486 pass "reject $sendthis"
489 -re "No symbol .* in current context.*$gdb_prompt $" {
490 pass "reject $sendthis"
493 -re "$expectthis.*$gdb_prompt $" {
494 pass "reject $sendthis"
497 -re ".*$gdb_prompt $" {
498 fail "reject $sendthis"
502 fail "reject $sendthis (eof or timeout)"
508 # Given an input string, adds backslashes as needed to create a
509 # regexp that will match the string.
511 proc string_to_regexp {str} {
513 regsub -all {[]*+.|()^$\[]} $str {\\&} result
517 # Same as gdb_test, but the second parameter is not a regexp,
518 # but a string that must match exactly.
520 proc gdb_test_exact { args } {
521 upvar timeout timeout
523 set command [lindex $args 0]
525 # This applies a special meaning to a null string pattern. Without
526 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
527 # messages from commands that should have no output except a new
528 # prompt. With this, only results of a null string will match a null
531 set pattern [lindex $args 1]
532 if [string match $pattern ""] {
533 set pattern [string_to_regexp [lindex $args 0]]
535 set pattern [string_to_regexp [lindex $args 1]]
538 # It is most natural to write the pattern argument with only
539 # embedded \n's, especially if you are trying to avoid Tcl quoting
540 # problems. But gdb_expect really wants to see \r\n in patterns. So
541 # transform the pattern here. First transform \r\n back to \n, in
542 # case some users of gdb_test_exact already do the right thing.
543 regsub -all "\r\n" $pattern "\n" pattern
544 regsub -all "\n" $pattern "\r\n" pattern
545 if [llength $args]==3 then {
546 set message [lindex $args 2]
551 return [gdb_test $command $pattern $message]
554 proc gdb_reinitialize_dir { subdir } {
557 if [is_remote host] {
562 -re "Reinitialize source path to empty.*y or n. " {
565 -re "Source directories searched.*$gdb_prompt $" {
566 send_gdb "dir $subdir\n"
568 -re "Source directories searched.*$gdb_prompt $" {
569 verbose "Dir set to $subdir"
571 -re "$gdb_prompt $" {
572 perror "Dir \"$subdir\" failed."
576 -re "$gdb_prompt $" {
577 perror "Dir \"$subdir\" failed."
581 -re "$gdb_prompt $" {
582 perror "Dir \"$subdir\" failed."
588 # gdb_exit -- exit the GDB, killing the target program if necessary
590 proc default_gdb_exit {} {
596 gdb_stop_suppressing_tests;
598 if ![info exists gdb_spawn_id] {
602 verbose "Quitting $GDB $GDBFLAGS"
604 if { [is_remote host] && [board_info host exists fileid] } {
607 -re "and kill it.*y or n. " {
620 # load a file into the debugger.
621 # return a -1 if anything goes wrong.
623 proc gdb_file_cmd { arg } {
629 upvar timeout timeout
631 if [is_remote host] {
632 set arg [remote_download host $arg];
634 error "download failed"
639 send_gdb "file $arg\n"
641 -re "Reading symbols from.*done.*$gdb_prompt $" {
642 verbose "\t\tLoaded $arg into the $GDB"
645 -re "has no symbol-table.*$gdb_prompt $" {
646 perror "$arg wasn't compiled with \"-g\""
649 -re "A program is being debugged already.*Kill it.*y or n. $" {
651 verbose "\t\tKilling previous program being debugged"
654 -re "Load new symbol table from \".*\".*y or n. $" {
657 -re "Reading symbols from.*done.*$gdb_prompt $" {
658 verbose "\t\tLoaded $arg with new symbol table into $GDB"
662 perror "(timeout) Couldn't load $arg, other program already loaded."
667 -re "No such file or directory.*$gdb_prompt $" {
668 perror "($arg) No such file or directory\n"
671 -re "$gdb_prompt $" {
672 perror "couldn't load $arg into $GDB."
676 perror "couldn't load $arg into $GDB (timed out)."
680 # This is an attempt to detect a core dump, but seems not to
681 # work. Perhaps we need to match .* followed by eof, in which
682 # gdb_expect does not seem to have a way to do that.
683 perror "couldn't load $arg into $GDB (end of file)."
690 # start gdb -- start gdb running, default procedure
692 # When running over NFS, particularly if running many simultaneous
693 # tests on different hosts all using the same server, things can
694 # get really slow. Give gdb at least 3 minutes to start up.
696 proc default_gdb_start { } {
704 gdb_stop_suppressing_tests;
706 verbose "Spawning $GDB -nw $GDBFLAGS"
708 if [info exists gdb_spawn_id] {
712 if ![is_remote host] {
713 if { [which $GDB] == 0 } then {
714 perror "$GDB does not exist."
718 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
719 if { $res < 0 || $res == "" } {
720 perror "Spawning $GDB failed."
724 -re "\[\r\n\]$gdb_prompt $" {
725 verbose "GDB initialized."
727 -re "$gdb_prompt $" {
728 perror "GDB never initialized."
732 perror "(timeout) GDB never initialized after 10 seconds."
738 # force the height to "unlimited", so no pagers get used
740 send_gdb "set height 0\n"
742 -re "$gdb_prompt $" {
743 verbose "Setting height to 0." 2
746 warning "Couldn't set the height to 0"
749 # force the width to "unlimited", so no wraparound occurs
750 send_gdb "set width 0\n"
752 -re "$gdb_prompt $" {
753 verbose "Setting width to 0." 2
756 warning "Couldn't set the width to 0."
762 # * For crosses, the CHILL runtime doesn't build because it can't find
763 # setjmp.h, stdio.h, etc.
764 # * For AIX (as of 16 Mar 95), (a) there is no language code for
765 # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
766 # does not get along with AIX's too-clever linker.
767 # * On Irix5, there is a bug whereby set of bool, etc., don't get
768 # TYPE_LOW_BOUND for the bool right because force_to_range_type doesn't
769 # work with stub types.
770 # Lots of things seem to fail on the PA, and since it's not a supported
771 # chill target at the moment, don't run the chill tests.
773 proc skip_chill_tests {} {
774 if ![info exists do_chill_tests] {
777 eval set skip_chill [expr ![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-irix5*"] || [istarget "*-*-irix6*"] || [istarget "alpha-*-osf*"] || [istarget "hppa*-*-*"]]
778 verbose "Skip chill tests is $skip_chill"
782 proc get_compiler_info {binfile} {
783 # Create and source the file that provides information about the compiler
784 # used to compile the test case.
787 # These two come from compiler.c.
788 global signed_keyword_not_used
791 if { [gdb_compile "${srcdir}/${subdir}/compiler.c" "${binfile}.ci" preprocess {}] != "" } {
792 perror "Couldn't make ${binfile}.ci file"
799 proc gdb_compile {source dest type options} {
800 global GDB_TESTCASE_OPTIONS;
802 if [target_info exists gdb_stub] {
803 set options2 { "additional_flags=-Dusestubs" }
804 lappend options "libs=[target_info gdb_stub]";
805 set options [concat $options2 $options]
807 if [info exists GDB_TESTCASE_OPTIONS] {
808 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
810 verbose "options are $options"
811 verbose "source is $source $dest $type $options"
812 set result [target_compile $source $dest $type $options];
813 regsub "\[\r\n\]*$" "$result" "" result;
814 regsub "^\[\r\n\]*" "$result" "" result;
815 if { $result != "" } {
816 clone_output "gdb compile failed, $result"
821 proc send_gdb { string } {
822 global suppress_flag;
823 if { $suppress_flag } {
826 return [remote_send host "$string"];
832 proc gdb_expect { args } {
833 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
834 set gtimeout [lindex $args 0];
835 set expcode [list [lindex $args 1]];
837 upvar timeout timeout;
840 if [target_info exists gdb,timeout] {
841 if [info exists timeout] {
842 if { $timeout < [target_info gdb,timeout] } {
843 set gtimeout [target_info gdb,timeout];
845 set gtimeout $timeout;
848 set gtimeout [target_info gdb,timeout];
852 if ![info exists gtimeout] {
854 if [info exists timeout] {
855 set gtimeout $timeout;
862 set code [catch {uplevel remote_expect host $gtimeout $expcode} string];
865 global errorInfo errorCode;
867 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
868 } elseif {$code == 2} {
869 return -code return $string
870 } elseif {$code == 3} {
872 } elseif {$code > 4} {
873 return -code $code $string
878 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
879 # gdb_expect to fail immediately (until the next call to
880 # gdb_stop_suppressing_tests).
882 proc gdb_suppress_tests { args } {
883 global suppress_flag;
887 if { $suppress_flag == 1 } {
888 if { [llength $args] > 0 } {
889 warning "[lindex $args 0]\n";
891 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
897 # Clear suppress_flag.
899 proc gdb_stop_suppressing_tests { } {
900 global suppress_flag;
902 if [info exists suppress_flag] {
903 if { $suppress_flag != 0 } {
905 clone_output "Tests restarted.\n";
917 catch default_gdb_exit
921 # gdb_load -- load a file into the debugger.
922 # return a -1 if anything goes wrong.
924 proc gdb_load { arg } {
925 return [gdb_file_cmd $arg]
928 proc gdb_continue { function } {
931 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
934 proc default_gdb_init { args } {
935 gdb_stop_suppressing_tests;
937 # Uh, this is lame. Really, really, really lame. But there's this *one*
938 # testcase that will fail in random places if we don't increase this.
941 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
942 if { [llength $args] > 0 } {
945 set file [lindex $args 0];
947 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
950 if [target_info exists gdb_prompt] {
951 set gdb_prompt [target_info gdb_prompt];
953 set gdb_prompt "\\(gdb\\)"
957 proc gdb_init { args } {
958 return [eval default_gdb_init $args];
961 proc gdb_finish { } {
967 # Run the gdb command "info source" and extract the debugging format information
968 # from the output and save it in debug_format.
970 proc get_debug_format { } {
976 set debug_format "unknown"
977 send_gdb "info source\n"
979 -re ".*Compiled with (.*) debugging format.\r\n$gdb_prompt $" {
980 set debug_format $expect_out(1,string)
981 verbose "debug format is $debug_format"
984 -re ".*No current source file.\r\n$gdb_prompt $" {
985 perror "get_debug_format used when no current source file"
989 perror "couldn't check debug format (timed out)."
995 # Like setup_xfail, but takes the name of a debug format (DWARF 1, COFF, stabs, etc).
996 # If that format matches the format that the current test was compiled with, then
997 # the next test is expected to fail for any target. Returns 1 if the next test or
998 # set of tests is expected to fail, 0 otherwise (or if it is unknown). Must
999 # have previously called get_debug_format.
1001 proc setup_xfail_format { format } {
1004 if [string match $debug_format $format] then {