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 gdb_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. $" {
128 -re "$gdb_prompt $" {}
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 ".*$gdb_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..*$gdb_prompt $" {}
159 -i $gdb_spawn_id -re ".*$gdb_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 "Continuing at \[^\r\n\]*\[\r\n\]" {
186 if ![target_info exists gdb_stub] {
190 -re "Line.* Jump anyway.*y or n. $" {
194 -re "No symbol.*context.*$gdb_prompt $" {}
195 -re "The program is not being run.*$gdb_prompt $" {
198 timeout { perror "Jump to start() failed (timeout)"; return }
200 if [target_info exists gdb_stub] {
202 -re ".*$gdb_prompt $" {
203 send_gdb "continue\n"
209 send_gdb "run $args\n"
210 # This doesn't work quite right yet.
212 -re "The program .* has been started already.*y or n. $" {
216 -re "Starting program: \[^\n\]*" {}
220 proc gdb_breakpoint { function } {
225 set spawn_id $gdb_spawn_id
227 send_gdb "break $function\n"
228 # The first two regexps are what we get with -g, the third is without -g.
230 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
231 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
232 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
233 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
234 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
239 # Set breakpoint at function and run gdb until it breaks there.
240 # Since this is the only breakpoint that will be set, if it stops
241 # at a breakpoint, we will assume it is the one we want. We can't
242 # just compare to "function" because it might be a fully qualified,
243 # single quoted C++ function specifier.
245 proc runto { function } {
250 set spawn_id $gdb_spawn_id
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 } {
328 upvar timeout timeout
330 if [llength $args]>2 then {
331 set message [lindex $args 2]
333 set message [lindex $args 0]
335 set command [lindex $args 0]
336 set pattern [lindex $args 1]
338 if [llength $args]==5 {
339 set question_string [lindex $args 3];
340 set response_string [lindex $args 4];
342 set question_string "^FOOBAR$"
346 send_user "Sending \"$command\" to gdb\n"
347 send_user "Looking to match \"$pattern\"\n"
348 send_user "Message is \"$message\"\n"
352 if ![string match $command ""] {
353 send_gdb "$command\n"
357 -re ".*Ending remote debugging.*$gdb_prompt$" {
358 if ![isnative] then {
359 warning "Can`t communicate to remote target."
365 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
366 if ![string match "" $message] then {
371 -re "(${question_string})$" {
372 send_gdb "$response_string\n";
375 -re "Undefined command:.*$gdb_prompt" {
376 perror "Undefined command \"$command\"."
379 -re "Ambiguous command.*$gdb_prompt $" {
380 perror "\"$command\" is not a unique command name."
383 -re ".*Program exited with code \[0-9\]+.*$gdb_prompt $" {
384 if ![string match "" $message] then {
385 set errmsg "$message: the program exited"
387 set errmsg "$command: the program exited"
392 -re "The program is not being run.*$gdb_prompt $" {
393 if ![string match "" $message] then {
394 set errmsg "$message: the program is no longer running"
396 set errmsg "$command: the program is no longer running"
401 -re ".*$gdb_prompt $" {
402 if ![string match "" $message] then {
409 perror "Window too small."
411 -re "\\(y or n\\) " {
413 perror "Got interactive prompt."
416 perror "Process no longer exists"
420 perror "internal buffer is full."
423 if ![string match "" $message] then {
424 fail "(timeout) $message"
432 # Test that a command gives an error. For pass or fail, return
433 # a 1 to indicate that more tests can proceed. However a timeout
434 # is a serious error, generates a special fail message, and causes
435 # a 0 to be returned to indicate that more tests are likely to fail
438 proc test_print_reject { args } {
442 if [llength $args]==2 then {
443 set expectthis [lindex $args 1]
445 set expectthis "should never match this bogus string"
447 set sendthis [lindex $args 0]
449 send_user "Sending \"$sendthis\" to gdb\n"
450 send_user "Looking to match \"$expectthis\"\n"
452 send_gdb "$sendthis\n"
454 -re ".*A .* in expression.*\\.*$gdb_prompt $" {
455 pass "reject $sendthis"
458 -re ".*Invalid syntax in expression.*$gdb_prompt $" {
459 pass "reject $sendthis"
462 -re ".*Junk after end of expression.*$gdb_prompt $" {
463 pass "reject $sendthis"
466 -re ".*Invalid number.*$gdb_prompt $" {
467 pass "reject $sendthis"
470 -re ".*Invalid character constant.*$gdb_prompt $" {
471 pass "reject $sendthis"
474 -re ".*No symbol table is loaded.*$gdb_prompt $" {
475 pass "reject $sendthis"
478 -re ".*No symbol .* in current context.*$gdb_prompt $" {
479 pass "reject $sendthis"
482 -re ".*$expectthis.*$gdb_prompt $" {
483 pass "reject $sendthis"
486 -re ".*$gdb_prompt $" {
487 fail "reject $sendthis"
491 fail "reject $sendthis (eof or timeout)"
497 # Given an input string, adds backslashes as needed to create a
498 # regexp that will match the string.
500 proc string_to_regexp {str} {
502 regsub -all {[]*+.|()^$\[]} $str {\\&} result
506 # Same as gdb_test, but the second parameter is not a regexp,
507 # but a string that must match exactly.
509 proc gdb_test_exact { args } {
510 upvar timeout timeout
512 set command [lindex $args 0]
514 # This applies a special meaning to a null string pattern. Without
515 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
516 # messages from commands that should have no output except a new
517 # prompt. With this, only results of a null string will match a null
520 set pattern [lindex $args 1]
521 if [string match $pattern ""] {
522 set pattern [string_to_regexp [lindex $args 0]]
524 set pattern [string_to_regexp [lindex $args 1]]
527 # It is most natural to write the pattern argument with only
528 # embedded \n's, especially if you are trying to avoid Tcl quoting
529 # problems. But expect really wants to see \r\n in patterns. So
530 # transform the pattern here. First transform \r\n back to \n, in
531 # case some users of gdb_test_exact already do the right thing.
532 regsub -all "\r\n" $pattern "\n" pattern
533 regsub -all "\n" $pattern "\r\n" pattern
534 if [llength $args]==3 then {
535 set message [lindex $args 2]
540 return [gdb_test $command $pattern $message]
543 proc gdb_reinitialize_dir { subdir } {
546 set spawn_id $gdb_spawn_id
548 if [is_remote host] {
553 -re "Reinitialize source path to empty.*y or n. " {
556 -re "Source directories searched.*$gdb_prompt $" {
557 send_gdb "dir $subdir\n"
559 -re "Source directories searched.*$gdb_prompt $" {
560 verbose "Dir set to $subdir"
562 -re ".*$gdb_prompt $" {
563 perror "Dir \"$subdir\" failed."
567 -re ".*$gdb_prompt $" {
568 perror "Dir \"$subdir\" failed."
572 -re ".*$gdb_prompt $" {
573 perror "Dir \"$subdir\" failed."
579 # gdb_exit -- exit the GDB, killing the target program if necessary
581 proc default_gdb_exit {} {
587 if ![info exists gdb_spawn_id] {
591 verbose "Quitting $GDB $GDBFLAGS"
593 # This used to be 1 for unix-gdb.exp
595 verbose "Timeout is now $timeout seconds" 2
597 if [is_remote host] {
600 -i $gdb_spawn_id -re ".*and kill it.*y or n. " {
604 -i $gdb_spawn_id timeout { }
607 # We used to try to send_gdb "quit" to GDB, and wait for it to die.
608 # Dealing with all the cases and errors got pretty hairy. Just close it,
610 catch "close -i $gdb_spawn_id"
612 # Omitting this probably would cause strange timing-dependent failures.
613 catch "wait -i $gdb_spawn_id"
621 # load a file into the debugger.
622 # return a -1 if anything goes wrong.
624 proc gdb_file_cmd { arg } {
630 upvar timeout timeout
632 set spawn_id $gdb_spawn_id
634 if [is_remote host] {
635 set arg [remote_download host $arg];
637 error "download failed"
642 send_gdb "file $arg\n"
644 -re "Reading symbols from.*done.*$gdb_prompt $" {
645 verbose "\t\tLoaded $arg into the $GDB"
648 -re "has no symbol-table.*$gdb_prompt $" {
649 perror "$arg wasn't compiled with \"-g\""
652 -re "A program is being debugged already.*Kill it.*y or n. $" {
654 verbose "\t\tKilling previous program being debugged"
657 -re "Load new symbol table from \".*\".*y or n. $" {
660 -re "Reading symbols from.*done.*$gdb_prompt $" {
661 verbose "\t\tLoaded $arg with new symbol table into $GDB"
665 perror "(timeout) Couldn't load $arg, other program already loaded."
670 -re ".*No such file or directory.*$gdb_prompt $" {
671 perror "($arg) No such file or directory\n"
674 -re "$gdb_prompt $" {
675 perror "couldn't load $arg into $GDB."
679 perror "couldn't load $arg into $GDB (timed out)."
683 # This is an attempt to detect a core dump, but seems not to
684 # work. Perhaps we need to match .* followed by eof, in which
685 # expect does not seem to have a way to do that.
686 perror "couldn't load $arg into $GDB (end of file)."
693 # start gdb -- start gdb running, default procedure
695 # When running over NFS, particularly if running many simultaneous
696 # tests on different hosts all using the same server, things can
697 # get really slow. Give gdb at least 3 minutes to start up.
699 proc default_gdb_start { } {
707 verbose "Spawning $GDB -nw $GDBFLAGS"
709 if [info exists gdb_spawn_id] {
713 set oldtimeout $timeout
714 set timeout [expr "$timeout + 180"]
715 if [is_remote host] {
716 set shell_id [remote_spawn host "$GDB -nw $GDBFLAGS --command gdbinit"]
718 if { [which $GDB] == 0 } then {
719 perror "$GDB does not exist."
723 set shell_id [remote_spawn host "$GDB -nw $GDBFLAGS"]
728 -i $shell_id -re ".*\[\r\n\]$gdb_prompt $" {
729 verbose "GDB initialized."
731 -i $shell_id -re "$gdb_prompt $" {
732 perror "GDB never initialized."
733 set timeout $oldtimeout
734 verbose "Timeout restored to $timeout seconds" 2
737 -i $shell_id timeout {
738 perror "(timeout) GDB never initialized after $timeout seconds."
739 set timeout $oldtimeout
740 verbose "Timeout restored to $timeout seconds" 2
744 set timeout $oldtimeout
745 verbose "Timeout restored to $timeout seconds" 2
746 set gdb_spawn_id $shell_id
747 set spawn_id $gdb_spawn_id
748 # force the height to "unlimited", so no pagers get used
749 send_gdb "set height 0\n"
751 -i $shell_id -re ".*$gdb_prompt $" {
752 verbose "Setting height to 0." 2
754 -i $shell_id timeout {
755 warning "Couldn't set the height to 0"
758 # force the width to "unlimited", so no wraparound occurs
759 send_gdb "set width 0\n"
761 -i $shell_id -re ".*$gdb_prompt $" {
762 verbose "Setting width to 0." 2
764 -i $shell_id timeout {
765 warning "Couldn't set the width to 0."
772 # FIXME: this is a copy of the new library procedure, but it's here too
773 # till the new dejagnu gets installed everywhere. I'd hate to break the
777 if ![info exists argv0] then {
778 proc exp_continue { } {
783 # * For crosses, the CHILL runtime doesn't build because it can't find
784 # setjmp.h, stdio.h, etc.
785 # * For AIX (as of 16 Mar 95), (a) there is no language code for
786 # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
787 # does not get along with AIX's too-clever linker.
788 # * On Irix5, there is a bug whereby set of bool, etc., don't get
789 # TYPE_LOW_BOUND for the bool right because force_to_range_type doesn't
790 # work with stub types.
791 # Lots of things seem to fail on the PA, and since it's not a supported
792 # chill target at the moment, don't run the chill tests.
794 proc skip_chill_tests {} {
795 if ![info exists do_chill_tests] {
798 eval set skip_chill [expr ![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-irix5*"] || [istarget "*-*-irix6*"] || [istarget "alpha-*-osf*"] || [istarget "hppa*-*-*"]]
799 verbose "Skip chill tests is $skip_chill"
803 proc get_compiler_info {binfile} {
804 # Create and source the file that provides information about the compiler
805 # used to compile the test case.
808 # These two come from compiler.c.
809 global signed_keyword_not_used
812 if { [gdb_compile "${srcdir}/${subdir}/compiler.c" "${binfile}.ci" preprocess {}] != "" } {
813 perror "Couldn't make ${binfile}.ci file"
820 proc gdb_compile {source dest type options} {
821 if [target_info exists gdb_stub] {
822 set options2 { "additional_flags=-Dusestubs" }
823 lappend options "libs=[target_info gdb_stub]";
824 set options [concat $options2 $options]
826 verbose "options are $options"
827 verbose "source is $source $dest $type $options"
828 set result [target_compile $source $dest $type $options];
829 regsub "\[\r\n\]*$" "$result" "" result;
830 regsub "^\[\r\n\]*" "$result" "" result;
831 if { $result != "" } {
832 clone_output "gdb compile failed, $result"
837 proc send_gdb { string } {
838 return [remote_send host "$string"];
846 catch default_gdb_exit
850 # gdb_load -- load a file into the debugger.
851 # return a -1 if anything goes wrong.
853 proc gdb_load { arg } {
854 return [gdb_file_cmd $arg]
857 proc gdb_continue { function } {
860 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
863 proc gdb_finish { } {