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:
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]]
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" { exp_continue }
102 -re "No symbol file now.*\r" { 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 {} {
130 send_gdb "delete breakpoints\n"
132 -i $gdb_spawn_id -re ".*Delete all breakpoints.*y or n.*$" {
136 -i $gdb_spawn_id -re ".*$gdb_prompt $" { # This happens if there were no breakpoints
138 -i $gdb_spawn_id timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
140 send_gdb "info breakpoints\n"
142 -i $gdb_spawn_id -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
143 -i $gdb_spawn_id -re ".*$gdb_prompt $" { perror "breakpoints not deleted" ; return }
144 -i $gdb_spawn_id -re "Delete all breakpoints.*or n.*$" {
148 -i $gdb_spawn_id timeout { perror "info breakpoints (timeout)" ; return }
154 # Generic run command.
156 # The second pattern below matches up to the first newline *only*.
157 # Using ``.*$'' could swallow up output that we attempt to match
160 proc gdb_run_cmd {args} {
164 set spawn_id $gdb_spawn_id
166 if [target_info exists use_gdb_stub] {
167 send_gdb "jump *start\n"
169 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
170 if ![target_info exists gdb_stub] {
174 -re "Line.* Jump anyway.*y or n. $" {
178 -re "No symbol.*context.*$gdb_prompt $" {}
179 -re "The program is not being run.*$gdb_prompt $" {
182 timeout { perror "Jump to start() failed (timeout)"; return }
184 if [target_info exists gdb_stub] {
186 -re ".*$gdb_prompt $" {
187 send_gdb "continue\n"
193 send_gdb "run $args\n"
194 # This doesn't work quite right yet.
196 -re "The program .* has been started already.*y or n. $" {
200 -re "Starting program: \[^\n\]*" {}
204 proc gdb_breakpoint { function } {
209 set spawn_id $gdb_spawn_id
211 send_gdb "break $function\n"
212 # The first two regexps are what we get with -g, the third is without -g.
214 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
215 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
216 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
217 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
218 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
223 # Set breakpoint at function and run gdb until it breaks there.
224 # Since this is the only breakpoint that will be set, if it stops
225 # at a breakpoint, we will assume it is the one we want. We can't
226 # just compare to "function" because it might be a fully qualified,
227 # single quoted C++ function specifier.
229 proc runto { function } {
234 set spawn_id $gdb_spawn_id
238 if ![gdb_breakpoint $function] {
244 # the "at foo.c:36" output we get with -g.
245 # the "in func" output we get without -g.
247 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
250 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
253 -re "$gdb_prompt $" {
254 fail "running to $function in runto"
258 fail "running to $function in runto (timeout)"
266 # runto_main -- ask gdb to run until we hit a breakpoint at main.
267 # The case where the target uses stubs has to be handled
268 # specially--if it uses stubs, assuming we hit
269 # breakpoint() and just step out of the function.
275 if ![target_info exists gdb_stub] {
282 # if use stubs step out of the breakpoint() function.
284 -re "main.* at .*$gdb_prompt $" {}
285 -re "_start.*$gdb_prompt $" {}
286 timeout { fail "single step at breakpoint() (timeout)" ; return 0 }
292 # gdb_test -- send_gdb a command to gdb and test the result.
293 # Takes three parameters.
295 # First one is the command to execute. If this is the null string
296 # then no command is sent.
297 # Second one is the pattern to match for a PASS, and must NOT include
298 # the \r\n sequence immediately before the gdb prompt.
299 # Third one is an optional message to be printed. If this
300 # a null string "", then the pass/fail messages use the command
301 # string as the message.
303 # 1 if the test failed,
304 # 0 if the test passes,
305 # -1 if there was an internal error.
307 proc gdb_test { args } {
312 upvar timeout timeout
314 if [llength $args]>2 then {
315 set message [lindex $args 2]
317 set message [lindex $args 0]
319 set command [lindex $args 0]
320 set pattern [lindex $args 1]
322 if [llength $args]==5 {
323 set question_string [lindex $args 3];
324 set response_string [lindex $args 4];
326 set question_string "^FOOBAR$"
330 send_user "Sending \"$command\" to gdb\n"
331 send_user "Looking to match \"$pattern\"\n"
332 send_user "Message is \"$message\"\n"
336 if ![string match $command ""] {
337 send_gdb "$command\n"
341 -re ".*Ending remote debugging.*$gdb_prompt$" {
342 if ![isnative] then {
343 warning "Can`t communicate to remote target."
349 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
350 if ![string match "" $message] then {
355 -re "(${question_string})$" {
356 send_gdb "$response_string\n";
359 -re "Undefined command:.*$gdb_prompt" {
360 perror "Undefined command \"$command\"."
363 -re "Ambiguous command.*$gdb_prompt $" {
364 perror "\"$command\" is not a unique command name."
367 -re ".*Program exited with code \[0-9\]+.*$gdb_prompt $" {
368 if ![string match "" $message] then {
369 set errmsg "$message: the program exited"
371 set errmsg "$command: the program exited"
376 -re "The program is not being run.*$gdb_prompt $" {
377 if ![string match "" $message] then {
378 set errmsg "$message: the program is no longer running"
380 set errmsg "$command: the program is no longer running"
385 -re ".*$gdb_prompt $" {
386 if ![string match "" $message] then {
393 perror "Window too small."
395 -re "\\(y or n\\) " {
397 perror "Got interactive prompt."
400 perror "Process no longer exists"
404 perror "internal buffer is full."
407 if ![string match "" $message] then {
408 fail "(timeout) $message"
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
422 proc test_print_reject { args } {
426 if [llength $args]==2 then {
427 set expectthis [lindex $args 1]
429 set expectthis "should never match this bogus string"
431 set sendthis [lindex $args 0]
433 send_user "Sending \"$sendthis\" to gdb\n"
434 send_user "Looking to match \"$expectthis\"\n"
436 send_gdb "$sendthis\n"
438 -re ".*A .* in expression.*\\.*$gdb_prompt $" {
439 pass "reject $sendthis"
442 -re ".*Invalid syntax in expression.*$gdb_prompt $" {
443 pass "reject $sendthis"
446 -re ".*Junk after end of expression.*$gdb_prompt $" {
447 pass "reject $sendthis"
450 -re ".*Invalid number.*$gdb_prompt $" {
451 pass "reject $sendthis"
454 -re ".*Invalid character constant.*$gdb_prompt $" {
455 pass "reject $sendthis"
458 -re ".*No symbol table is loaded.*$gdb_prompt $" {
459 pass "reject $sendthis"
462 -re ".*No symbol .* in current context.*$gdb_prompt $" {
463 pass "reject $sendthis"
466 -re ".*$expectthis.*$gdb_prompt $" {
467 pass "reject $sendthis"
470 -re ".*$gdb_prompt $" {
471 fail "reject $sendthis"
475 fail "reject $sendthis (eof or timeout)"
481 # Given an input string, adds backslashes as needed to create a
482 # regexp that will match the string.
484 proc string_to_regexp {str} {
486 regsub -all {[]*+.|()^$\[]} $str {\\&} result
490 # Same as gdb_test, but the second parameter is not a regexp,
491 # but a string that must match exactly.
493 proc gdb_test_exact { args } {
494 upvar timeout timeout
496 set command [lindex $args 0]
498 # This applies a special meaning to a null string pattern. Without
499 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
500 # messages from commands that should have no output except a new
501 # prompt. With this, only results of a null string will match a null
504 set pattern [lindex $args 1]
505 if [string match $pattern ""] {
506 set pattern [string_to_regexp [lindex $args 0]]
508 set pattern [string_to_regexp [lindex $args 1]]
511 # It is most natural to write the pattern argument with only
512 # embedded \n's, especially if you are trying to avoid Tcl quoting
513 # problems. But expect really wants to see \r\n in patterns. So
514 # transform the pattern here. First transform \r\n back to \n, in
515 # case some users of gdb_test_exact already do the right thing.
516 regsub -all "\r\n" $pattern "\n" pattern
517 regsub -all "\n" $pattern "\r\n" pattern
518 if [llength $args]==3 then {
519 set message [lindex $args 2]
524 return [gdb_test $command $pattern $message]
527 proc gdb_reinitialize_dir { subdir } {
530 set spawn_id $gdb_spawn_id
532 if [is_remote host] {
537 -re "Reinitialize source path to empty.*y or n. " {
540 -re "Source directories searched.*$gdb_prompt $" {
541 send_gdb "dir $subdir\n"
543 -re "Source directories searched.*$gdb_prompt $" {
544 verbose "Dir set to $subdir"
546 -re ".*$gdb_prompt $" {
547 perror "Dir \"$subdir\" failed."
551 -re ".*$gdb_prompt $" {
552 perror "Dir \"$subdir\" failed."
556 -re ".*$gdb_prompt $" {
557 perror "Dir \"$subdir\" failed."
563 # gdb_exit -- exit the GDB, killing the target program if necessary
565 proc default_gdb_exit {} {
571 if ![info exists gdb_spawn_id] {
575 verbose "Quitting $GDB $GDBFLAGS"
577 # This used to be 1 for unix-gdb.exp
579 verbose "Timeout is now $timeout seconds" 2
581 if [is_remote host] {
584 -i $gdb_spawn_id -re ".*and kill it.*y or n. " {
588 -i $gdb_spawn_id timeout { }
591 # We used to try to send_gdb "quit" to GDB, and wait for it to die.
592 # Dealing with all the cases and errors got pretty hairy. Just close it,
594 catch "close -i $gdb_spawn_id"
596 # Omitting this probably would cause strange timing-dependent failures.
597 catch "wait -i $gdb_spawn_id"
605 # load a file into the debugger.
606 # return a -1 if anything goes wrong.
608 proc gdb_file_cmd { arg } {
614 upvar timeout timeout
616 set spawn_id $gdb_spawn_id
618 if [is_remote host] {
619 set arg [remote_download host $arg];
621 error "download failed"
626 send_gdb "file $arg\n"
628 -re "Reading symbols from.*done.*$gdb_prompt $" {
629 verbose "\t\tLoaded $arg into the $GDB"
632 -re "has no symbol-table.*$gdb_prompt $" {
633 perror "$arg wasn't compiled with \"-g\""
636 -re "A program is being debugged already.*Kill it.*y or n. $" {
638 verbose "\t\tKilling previous program being debugged"
641 -re "Load new symbol table from \".*\".*y or n. $" {
644 -re "Reading symbols from.*done.*$gdb_prompt $" {
645 verbose "\t\tLoaded $arg with new symbol table into $GDB"
649 perror "(timeout) Couldn't load $arg, other program already loaded."
654 -re ".*No such file or directory.*$gdb_prompt $" {
655 perror "($arg) No such file or directory\n"
658 -re "$gdb_prompt $" {
659 perror "couldn't load $arg into $GDB."
663 perror "couldn't load $arg into $GDB (timed out)."
667 # This is an attempt to detect a core dump, but seems not to
668 # work. Perhaps we need to match .* followed by eof, in which
669 # expect does not seem to have a way to do that.
670 perror "couldn't load $arg into $GDB (end of file)."
677 # start gdb -- start gdb running, default procedure
679 # When running over NFS, particularly if running many simultaneous
680 # tests on different hosts all using the same server, things can
681 # get really slow. Give gdb at least 3 minutes to start up.
683 proc default_gdb_start { } {
691 verbose "Spawning $GDB -nw $GDBFLAGS"
693 if [info exists gdb_spawn_id] {
697 set oldtimeout $timeout
698 set timeout [expr "$timeout + 180"]
699 if [is_remote host] {
700 set shell_id [remote_spawn host "$GDB -nw $GDBFLAGS --command gdbinit"]
702 if { [which $GDB] == 0 } then {
703 perror "$GDB does not exist."
707 set shell_id [remote_spawn host "$GDB -nw $GDBFLAGS"]
712 -i $shell_id -re ".*\[\r\n\]$gdb_prompt $" {
713 verbose "GDB initialized."
715 -i $shell_id -re "$gdb_prompt $" {
716 perror "GDB never initialized."
717 set timeout $oldtimeout
718 verbose "Timeout restored to $timeout seconds" 2
721 -i $shell_id timeout {
722 perror "(timeout) GDB never initialized after $timeout seconds."
723 set timeout $oldtimeout
724 verbose "Timeout restored to $timeout seconds" 2
728 set timeout $oldtimeout
729 verbose "Timeout restored to $timeout seconds" 2
730 set gdb_spawn_id $shell_id
731 set spawn_id $gdb_spawn_id
732 # force the height to "unlimited", so no pagers get used
733 send_gdb "set height 0\n"
735 -i $shell_id -re ".*$gdb_prompt $" {
736 verbose "Setting height to 0." 2
738 -i $shell_id timeout {
739 warning "Couldn't set the height to 0"
742 # force the width to "unlimited", so no wraparound occurs
743 send_gdb "set width 0\n"
745 -i $shell_id -re ".*$gdb_prompt $" {
746 verbose "Setting width to 0." 2
748 -i $shell_id timeout {
749 warning "Couldn't set the width to 0."
756 # FIXME: this is a copy of the new library procedure, but it's here too
757 # till the new dejagnu gets installed everywhere. I'd hate to break the
761 if ![info exists argv0] then {
762 proc exp_continue { } {
767 # * For crosses, the CHILL runtime doesn't build because it can't find
768 # setjmp.h, stdio.h, etc.
769 # * For AIX (as of 16 Mar 95), (a) there is no language code for
770 # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
771 # does not get along with AIX's too-clever linker.
772 # * On Irix5, there is a bug whereby set of bool, etc., don't get
773 # TYPE_LOW_BOUND for the bool right because force_to_range_type doesn't
774 # work with stub types.
775 # Lots of things seem to fail on the PA, and since it's not a supported
776 # chill target at the moment, don't run the chill tests.
778 proc skip_chill_tests {} {
779 if ![info exists do_chill_tests] {
782 eval set skip_chill [expr ![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-irix5*"] || [istarget "*-*-irix6*"] || [istarget "alpha-*-osf*"] || [istarget "hppa*-*-*"]]
783 verbose "Skip chill tests is $skip_chill"
787 proc get_compiler_info {binfile} {
788 # Create and source the file that provides information about the compiler
789 # used to compile the test case.
792 # These two come from compiler.c.
793 global signed_keyword_not_used
796 if { [gdb_compile "${srcdir}/${subdir}/compiler.c" "${binfile}.ci" preprocess {}] != "" } {
797 perror "Couldn't make ${binfile}.ci file"
804 proc gdb_compile {source dest type options} {
805 if [target_info exists gdb_stub] {
806 set options2 { "additional_flags=-Dusestubs" }
807 lappend options "libs=[target_info gdb_stub]";
808 set options [concat $options2 $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 return [remote_send host "$string"];
830 catch default_gdb_exit
834 # gdb_load -- load a file into the debugger.
835 # return a -1 if anything goes wrong.
837 proc gdb_load { arg } {
838 return [gdb_file_cmd $arg]
841 proc gdb_continue { function } {
844 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
847 proc gdb_finish { } {