1 # Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 # Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 # Please email any bugs, comments, and/or additions to this file to:
20 # bug-gdb@prep.ai.mit.edu
22 # This file was written by Fred Fish. (fnf@cygnus.com)
24 # Generic gdb subroutines that should work for any target. If these
25 # need to be modified for any target, it can be done with a variable
26 # or by passing arguments.
32 if [info exists TOOL_EXECUTABLE] {
33 set GDB $TOOL_EXECUTABLE;
35 if ![info exists GDB] {
36 if ![is_remote host] {
37 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
39 set GDB [transform gdb];
42 verbose "using GDB = $GDB" 2
45 if ![info exists GDBFLAGS] {
48 verbose "using GDBFLAGS = $GDBFLAGS" 2
50 # The variable gdb_prompt is a regexp which matches the gdb prompt.
51 # Set it if it is not already set.
53 if ![info exists gdb_prompt] then {
54 set gdb_prompt "\[(\]gdb\[)\]"
57 # Needed for some tests under Cygwin.
61 if ![info exists env(EXEEXT)] {
64 set EXEEXT $env(EXEEXT)
67 ### Only procedures should come after this point.
70 # gdb_version -- extract and print the version number of GDB
72 proc default_gdb_version {} {
76 set fileid [open "gdb_cmd" w];
79 set cmdfile [remote_download host "gdb_cmd"];
80 set output [remote_exec host "$GDB -nw --command $cmdfile"]
81 remote_file build delete "gdb_cmd";
82 remote_file host delete "$cmdfile";
83 set tmp [lindex $output 1];
85 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
86 if ![is_remote host] {
87 clone_output "[which $GDB] version $version $GDBFLAGS\n"
89 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
93 proc gdb_version { } {
94 return [default_gdb_version];
98 # gdb_unload -- unload a file if one is loaded
107 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
108 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
109 -re "A program is being debugged already..*Kill it.*y or n. $"\
111 verbose "\t\tKilling previous program being debugged"
114 -re "Discard symbol table from .*y or n.*$" {
118 -re "$gdb_prompt $" {}
120 perror "couldn't unload file in $GDB (timed out)."
126 # Many of the tests depend on setting breakpoints at various places and
127 # running until that breakpoint is reached. At times, we want to start
128 # with a clean-slate with respect to breakpoints, so this utility proc
129 # lets us do this without duplicating this code everywhere.
132 proc delete_breakpoints {} {
135 # we need a larger timeout value here or this thing just confuses
136 # itself. May need a better implementation if possible. - guo
138 send_gdb "delete breakpoints\n"
140 -re "Delete all breakpoints.*y or n.*$" {
144 -re "$gdb_prompt $" { # This happens if there were no breakpoints
146 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
148 send_gdb "info breakpoints\n"
150 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
151 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
152 -re "Delete all breakpoints.*or n.*$" {
156 timeout { perror "info breakpoints (timeout)" ; return }
162 # Generic run command.
164 # The second pattern below matches up to the first newline *only*.
165 # Using ``.*$'' could swallow up output that we attempt to match
168 proc gdb_run_cmd {args} {
171 if [target_info exists gdb_init_command] {
172 send_gdb "[target_info gdb_init_command]\n";
174 -re "$gdb_prompt $" { }
176 perror "gdb_init_command for target failed";
182 if [target_info exists use_gdb_stub] {
183 if [target_info exists gdb,do_reload_on_run] {
184 # Specifying no file, defaults to the executable
185 # currently being debugged.
186 if { [gdb_load ""] < 0 } {
189 send_gdb "continue\n";
191 -re "Continu\[^\r\n\]*\[\r\n\]" {}
197 if [target_info exists gdb,start_symbol] {
198 set start [target_info gdb,start_symbol];
202 send_gdb "jump *$start\n"
204 while { $start_attempt } {
205 # Cap (re)start attempts at three to ensure that this loop
206 # always eventually fails. Don't worry about trying to be
207 # clever and not send a command when it has failed.
208 if [expr $start_attempt > 3] {
209 perror "Jump to start() failed (retry count exceeded)";
212 set start_attempt [expr $start_attempt + 1];
214 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
217 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
218 perror "Can't find start symbol to run in gdb_run";
221 -re "No symbol \"start\" in current.*$gdb_prompt $" {
222 send_gdb "jump *_start\n";
224 -re "No symbol.*context.*$gdb_prompt $" {
227 -re "Line.* Jump anyway.*y or n. $" {
230 -re "The program is not being run.*$gdb_prompt $" {
231 if { [gdb_load ""] < 0 } {
234 send_gdb "jump *$start\n";
237 perror "Jump to start() failed (timeout)";
242 if [target_info exists gdb_stub] {
244 -re "$gdb_prompt $" {
245 send_gdb "continue\n"
252 if [target_info exists gdb,do_reload_on_run] {
253 if { [gdb_load ""] < 0 } {
257 send_gdb "run $args\n"
258 # This doesn't work quite right yet.
260 -re "The program .* has been started already.*y or n. $" {
264 -re "Starting program: \[^\r\n\]*" {}
268 proc gdb_breakpoint { function } {
272 send_gdb "break $function\n"
273 # The first two regexps are what we get with -g, the third is without -g.
275 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
276 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
277 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
278 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
282 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
283 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
288 # Set breakpoint at function and run gdb until it breaks there.
289 # Since this is the only breakpoint that will be set, if it stops
290 # at a breakpoint, we will assume it is the one we want. We can't
291 # just compare to "function" because it might be a fully qualified,
292 # single quoted C++ function specifier.
294 proc runto { function } {
300 if ![gdb_breakpoint $function] {
306 # the "at foo.c:36" output we get with -g.
307 # the "in func" output we get without -g.
309 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
312 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
315 -re "$gdb_prompt $" {
316 fail "running to $function in runto"
320 fail "running to $function in runto (timeout)"
328 # runto_main -- ask gdb to run until we hit a breakpoint at main.
329 # The case where the target uses stubs has to be handled
330 # specially--if it uses stubs, assuming we hit
331 # breakpoint() and just step out of the function.
333 proc runto_main { } {
337 if ![target_info exists gdb_stub] {
349 ### Continue, and expect to hit a breakpoint.
350 ### Report a pass or fail, depending on whether it seems to have
351 ### worked. Use NAME as part of the test name; each call to
352 ### continue_to_breakpoint should use a NAME which is unique within
354 proc gdb_continue_to_breakpoint {name} {
356 set full_name "continue to breakpoint: $name"
358 send_gdb "continue\n"
360 -re "Breakpoint .* at .*\r\n$gdb_prompt $" {
363 -re ".*$gdb_prompt $" {
367 fail "$full_name (timeout)"
373 # gdb_internal_error_resync:
375 # Answer the questions GDB asks after it reports an internal error
376 # until we get back to a GDB prompt. Decline to quit the debugging
377 # session, and decline to create a core file. Return non-zero if the
380 # This procedure just answers whatever questions come up until it sees
381 # a GDB prompt; it doesn't require you to have matched the input up to
382 # any specific point. However, it only answers questions it sees in
383 # the output itself, so if you've matched a question, you had better
384 # answer it yourself before calling this.
386 # You can use this function thus:
390 # -re ".*A problem internal to GDB has been detected" {
391 # gdb_internal_error_resync
396 proc gdb_internal_error_resync {} {
400 while {$count < 10} {
402 -re "Quit this debugging session\\? \\(y or n\\) $" {
406 -re "Create a core file of GDB\\? \\(y or n\\) $" {
410 -re "$gdb_prompt $" {
411 # We're resynchronized.
415 perror "Could not resync from internal error (timeout)"
420 perror "Could not resync from internal error (resync count exceeded)"
425 # gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
426 # Send a command to gdb; test the result.
428 # COMMAND is the command to execute, send to GDB with send_gdb. If
429 # this is the null string no command is sent.
430 # MESSAGE is a message to be printed with the built-in failure patterns
431 # if one of them matches. If MESSAGE is empty COMMAND will be used.
432 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
433 # patterns. Pattern elements will be evaluated in the caller's
434 # context; action elements will be executed in the caller's context.
435 # Unlike patterns for gdb_test, these patterns should generally include
436 # the final newline and prompt.
439 # 1 if the test failed, according to a built-in failure pattern
440 # 0 if only user-supplied patterns matched
441 # -1 if there was an internal error.
443 # You can use this function thus:
445 # gdb_test_multiple "print foo" "test foo" {
446 # -re "expected output 1" {
449 # -re "expected output 2" {
454 # The standard patterns, such as "Program exited..." and "A problem
455 # ...", all being implicitly appended to that list.
457 proc gdb_test_multiple { command message user_code } {
461 upvar timeout timeout
462 upvar expect_out expect_out
464 if { $message == "" } {
468 # TCL/EXPECT WART ALERT
469 # Expect does something very strange when it receives a single braced
470 # argument. It splits it along word separators and performs substitutions.
471 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
472 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
473 # double-quoted list item, "\[ab\]" is just a long way of representing
474 # "[ab]", because the backslashes will be removed by lindex.
476 # Unfortunately, there appears to be no easy way to duplicate the splitting
477 # that expect will do from within TCL. And many places make use of the
478 # "\[0-9\]" construct, so we need to support that; and some places make use
479 # of the "[func]" construct, so we need to support that too. In order to
480 # get this right we have to substitute quoted list elements differently
481 # from braced list elements.
483 # We do this roughly the same way that Expect does it. We have to use two
484 # lists, because if we leave unquoted newlines in the argument to uplevel
485 # they'll be treated as command separators, and if we escape newlines
486 # we mangle newlines inside of command blocks. This assumes that the
487 # input doesn't contain a pattern which contains actual embedded newlines
490 regsub -all {\n} ${user_code} { } subst_code
491 set subst_code [uplevel list $subst_code]
493 set processed_code ""
495 set expecting_action 0
496 foreach item $user_code subst_item $subst_code {
497 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
498 lappend processed_code $item
501 if {$item == "-indices" || $item == "-re" || $item == "-ex"} {
502 lappend processed_code $item
505 if { $expecting_action } {
506 lappend processed_code "uplevel [list $item]"
507 set expecting_action 0
508 # Cosmetic, no effect on the list.
509 append processed_code "\n"
512 set expecting_action 1
513 lappend processed_code $subst_item
514 if {$patterns != ""} {
517 append patterns "\"$subst_item\""
520 # Also purely cosmetic.
521 regsub -all {\r} $patterns {\\r} patterns
522 regsub -all {\n} $patterns {\\n} patterns
525 send_user "Sending \"$command\" to gdb\n"
526 send_user "Looking to match \"$patterns\"\n"
527 send_user "Message is \"$message\"\n"
531 set string "${command}\n";
532 if { $command != "" } {
533 while { "$string" != "" } {
534 set foo [string first "\n" "$string"];
535 set len [string length "$string"];
536 if { $foo < [expr $len - 1] } {
537 set str [string range "$string" 0 $foo];
538 if { [send_gdb "$str"] != "" } {
539 global suppress_flag;
541 if { ! $suppress_flag } {
542 perror "Couldn't send $command to GDB.";
547 # since we're checking if each line of the multi-line
548 # command are 'accepted' by GDB here,
549 # we need to set -notransfer expect option so that
550 # command output is not lost for pattern matching
553 -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
554 timeout { verbose "partial: timeout" 3 }
556 set string [string range "$string" [expr $foo + 1] end];
561 if { "$string" != "" } {
562 if { [send_gdb "$string"] != "" } {
563 global suppress_flag;
565 if { ! $suppress_flag } {
566 perror "Couldn't send $command to GDB.";
574 if [target_info exists gdb,timeout] {
575 set tmt [target_info gdb,timeout];
577 if [info exists timeout] {
581 if [info exists timeout] {
590 -re ".*A problem internal to GDB has been detected" {
591 fail "$message (GDB internal error)"
592 gdb_internal_error_resync
594 -re "\\*\\*\\* DOSEXIT code.*" {
595 if { $message != "" } {
598 gdb_suppress_entire_file "GDB died";
601 -re "Ending remote debugging.*$gdb_prompt $" {
602 if ![isnative] then {
603 warning "Can`t communicate to remote target."
610 append code $processed_code
612 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
613 perror "Undefined command \"$command\"."
617 -re "Ambiguous command.*$gdb_prompt $" {
618 perror "\"$command\" is not a unique command name."
622 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
623 if ![string match "" $message] then {
624 set errmsg "$message (the program exited)"
626 set errmsg "$command (the program exited)"
631 -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" {
632 if ![string match "" $message] then {
633 set errmsg "$message (the program exited)"
635 set errmsg "$command (the program exited)"
640 -re "The program is not being run.*$gdb_prompt $" {
641 if ![string match "" $message] then {
642 set errmsg "$message (the program is no longer running)"
644 set errmsg "$command (the program is no longer running)"
649 -re ".*$gdb_prompt $" {
650 if ![string match "" $message] then {
657 perror "Window too small."
661 -re "\\(y or n\\) " {
663 perror "Got interactive prompt."
668 perror "Process no longer exists"
669 if { $message != "" } {
675 perror "internal buffer is full."
680 if ![string match "" $message] then {
681 fail "$message (timeout)"
688 gdb_expect $tmt $code
692 # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
693 # Send a command to gdb; test the result.
695 # COMMAND is the command to execute, send to GDB with send_gdb. If
696 # this is the null string no command is sent.
697 # PATTERN is the pattern to match for a PASS, and must NOT include
698 # the \r\n sequence immediately before the gdb prompt.
699 # MESSAGE is an optional message to be printed. If this is
700 # omitted, then the pass/fail messages use the command string as the
701 # message. (If this is the empty string, then sometimes we don't
702 # call pass or fail at all; I don't understand this at all.)
703 # QUESTION is a question GDB may ask in response to COMMAND, like
705 # RESPONSE is the response to send if QUESTION appears.
708 # 1 if the test failed,
709 # 0 if the test passes,
710 # -1 if there was an internal error.
712 proc gdb_test { args } {
716 upvar timeout timeout
718 if [llength $args]>2 then {
719 set message [lindex $args 2]
721 set message [lindex $args 0]
723 set command [lindex $args 0]
724 set pattern [lindex $args 1]
726 if [llength $args]==5 {
727 set question_string [lindex $args 3];
728 set response_string [lindex $args 4];
730 set question_string "^FOOBAR$"
733 return [gdb_test_multiple $command $message {
734 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
735 if ![string match "" $message] then {
739 -re "(${question_string})$" {
740 send_gdb "$response_string\n";
746 # Test that a command gives an error. For pass or fail, return
747 # a 1 to indicate that more tests can proceed. However a timeout
748 # is a serious error, generates a special fail message, and causes
749 # a 0 to be returned to indicate that more tests are likely to fail
752 proc test_print_reject { args } {
756 if [llength $args]==2 then {
757 set expectthis [lindex $args 1]
759 set expectthis "should never match this bogus string"
761 set sendthis [lindex $args 0]
763 send_user "Sending \"$sendthis\" to gdb\n"
764 send_user "Looking to match \"$expectthis\"\n"
766 send_gdb "$sendthis\n"
767 #FIXME: Should add timeout as parameter.
769 -re "A .* in expression.*\\.*$gdb_prompt $" {
770 pass "reject $sendthis"
773 -re "Invalid syntax in expression.*$gdb_prompt $" {
774 pass "reject $sendthis"
777 -re "Junk after end of expression.*$gdb_prompt $" {
778 pass "reject $sendthis"
781 -re "Invalid number.*$gdb_prompt $" {
782 pass "reject $sendthis"
785 -re "Invalid character constant.*$gdb_prompt $" {
786 pass "reject $sendthis"
789 -re "No symbol table is loaded.*$gdb_prompt $" {
790 pass "reject $sendthis"
793 -re "No symbol .* in current context.*$gdb_prompt $" {
794 pass "reject $sendthis"
797 -re "Unmatched single quote.*$gdb_prompt $" {
798 pass "reject $sendthis"
801 -re "A character constant must contain at least one character.*$gdb_prompt $" {
802 pass "reject $sendthis"
805 -re "$expectthis.*$gdb_prompt $" {
806 pass "reject $sendthis"
809 -re ".*$gdb_prompt $" {
810 fail "reject $sendthis"
814 fail "reject $sendthis (eof or timeout)"
820 # Given an input string, adds backslashes as needed to create a
821 # regexp that will match the string.
823 proc string_to_regexp {str} {
825 regsub -all {[]*+.|()^$\[]} $str {\\&} result
829 # Same as gdb_test, but the second parameter is not a regexp,
830 # but a string that must match exactly.
832 proc gdb_test_exact { args } {
833 upvar timeout timeout
835 set command [lindex $args 0]
837 # This applies a special meaning to a null string pattern. Without
838 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
839 # messages from commands that should have no output except a new
840 # prompt. With this, only results of a null string will match a null
843 set pattern [lindex $args 1]
844 if [string match $pattern ""] {
845 set pattern [string_to_regexp [lindex $args 0]]
847 set pattern [string_to_regexp [lindex $args 1]]
850 # It is most natural to write the pattern argument with only
851 # embedded \n's, especially if you are trying to avoid Tcl quoting
852 # problems. But gdb_expect really wants to see \r\n in patterns. So
853 # transform the pattern here. First transform \r\n back to \n, in
854 # case some users of gdb_test_exact already do the right thing.
855 regsub -all "\r\n" $pattern "\n" pattern
856 regsub -all "\n" $pattern "\r\n" pattern
857 if [llength $args]==3 then {
858 set message [lindex $args 2]
863 return [gdb_test $command $pattern $message]
866 proc gdb_reinitialize_dir { subdir } {
869 if [is_remote host] {
874 -re "Reinitialize source path to empty.*y or n. " {
877 -re "Source directories searched.*$gdb_prompt $" {
878 send_gdb "dir $subdir\n"
880 -re "Source directories searched.*$gdb_prompt $" {
881 verbose "Dir set to $subdir"
883 -re "$gdb_prompt $" {
884 perror "Dir \"$subdir\" failed."
888 -re "$gdb_prompt $" {
889 perror "Dir \"$subdir\" failed."
893 -re "$gdb_prompt $" {
894 perror "Dir \"$subdir\" failed."
900 # gdb_exit -- exit the GDB, killing the target program if necessary
902 proc default_gdb_exit {} {
908 gdb_stop_suppressing_tests;
910 if ![info exists gdb_spawn_id] {
914 verbose "Quitting $GDB $GDBFLAGS"
916 if { [is_remote host] && [board_info host exists fileid] } {
923 -re "DOSEXIT code" { }
928 if ![is_remote host] {
935 # load a file into the debugger.
936 # return a -1 if anything goes wrong.
938 proc gdb_file_cmd { arg } {
944 upvar timeout timeout
946 if [is_remote host] {
947 set arg [remote_download host $arg];
949 error "download failed"
954 send_gdb "file $arg\n"
956 -re "Reading symbols from.*done.*$gdb_prompt $" {
957 verbose "\t\tLoaded $arg into the $GDB"
960 -re "has no symbol-table.*$gdb_prompt $" {
961 perror "$arg wasn't compiled with \"-g\""
964 -re "A program is being debugged already.*Kill it.*y or n. $" {
966 verbose "\t\tKilling previous program being debugged"
969 -re "Load new symbol table from \".*\".*y or n. $" {
972 -re "Reading symbols from.*done.*$gdb_prompt $" {
973 verbose "\t\tLoaded $arg with new symbol table into $GDB"
977 perror "(timeout) Couldn't load $arg, other program already loaded."
982 -re "No such file or directory.*$gdb_prompt $" {
983 perror "($arg) No such file or directory\n"
986 -re "$gdb_prompt $" {
987 perror "couldn't load $arg into $GDB."
991 perror "couldn't load $arg into $GDB (timed out)."
995 # This is an attempt to detect a core dump, but seems not to
996 # work. Perhaps we need to match .* followed by eof, in which
997 # gdb_expect does not seem to have a way to do that.
998 perror "couldn't load $arg into $GDB (end of file)."
1005 # start gdb -- start gdb running, default procedure
1007 # When running over NFS, particularly if running many simultaneous
1008 # tests on different hosts all using the same server, things can
1009 # get really slow. Give gdb at least 3 minutes to start up.
1011 proc default_gdb_start { } {
1017 global gdb_spawn_id;
1019 gdb_stop_suppressing_tests;
1021 verbose "Spawning $GDB -nw $GDBFLAGS"
1023 if [info exists gdb_spawn_id] {
1027 if ![is_remote host] {
1028 if { [which $GDB] == 0 } then {
1029 perror "$GDB does not exist."
1033 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
1034 if { $res < 0 || $res == "" } {
1035 perror "Spawning $GDB failed."
1039 -re "\[\r\n\]$gdb_prompt $" {
1040 verbose "GDB initialized."
1042 -re "$gdb_prompt $" {
1043 perror "GDB never initialized."
1047 perror "(timeout) GDB never initialized after 10 seconds."
1052 set gdb_spawn_id -1;
1053 # force the height to "unlimited", so no pagers get used
1055 send_gdb "set height 0\n"
1057 -re "$gdb_prompt $" {
1058 verbose "Setting height to 0." 2
1061 warning "Couldn't set the height to 0"
1064 # force the width to "unlimited", so no wraparound occurs
1065 send_gdb "set width 0\n"
1067 -re "$gdb_prompt $" {
1068 verbose "Setting width to 0." 2
1071 warning "Couldn't set the width to 0."
1077 # Return a 1 for configurations for which we don't even want to try to
1080 proc skip_cplus_tests {} {
1081 if { [istarget "d10v-*-*"] } {
1084 if { [istarget "h8300-*-*"] } {
1088 # The C++ IO streams are too large for HC11/HC12 and are thus not
1089 # available. The gdb C++ tests use them and don't compile.
1090 if { [istarget "m6811-*-*"] } {
1093 if { [istarget "m6812-*-*"] } {
1099 # Skip all the tests in the file if you are not on an hppa running
1102 proc skip_hp_tests {} {
1103 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
1104 verbose "Skip hp tests is $skip_hp"
1108 set compiler_info "unknown"
1110 set hp_cc_compiler 0
1111 set hp_aCC_compiler 0
1112 set signed_keyword_not_used 0
1114 # Figure out what compiler I am using.
1116 # BINFILE is a "compiler information" output file. This implementation
1117 # does not use BINFILE.
1119 # ARGS can be empty or "C++". If empty, "C" is assumed.
1121 # There are several ways to do this, with various problems.
1123 # [ gdb_compile -E $ifile -o $binfile.ci ]
1124 # source $binfile.ci
1126 # Single Unix Spec v3 says that "-E -o ..." together are not
1127 # specified. And in fact, the native compiler on hp-ux 11 (among
1128 # others) does not work with "-E -o ...". Most targets used to do
1129 # this, and it mostly worked, because it works with gcc.
1131 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1132 # source $binfile.ci
1134 # This avoids the problem with -E and -o together. This almost works
1135 # if the build machine is the same as the host machine, which is
1136 # usually true of the targets which are not gcc. But this code does
1137 # not figure which compiler to call, and it always ends up using the C
1138 # compiler. Not good for setting hp_aCC_compiler. Targets
1139 # hppa*-*-hpux* and mips*-*-irix* used to do this.
1141 # [ gdb_compile -E $ifile > $binfile.ci ]
1142 # source $binfile.ci
1144 # dejagnu target_compile says that it supports output redirection,
1145 # but the code is completely different from the normal path and I
1146 # don't want to sweep the mines from that path. So I didn't even try
1149 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1152 # I actually do this for all targets now. gdb_compile runs the right
1153 # compiler, and TCL captures the output, and I eval the output.
1155 # Unfortunately, expect logs the output of the command as it goes by,
1156 # and dejagnu helpfully prints a second copy of it right afterwards.
1157 # So I turn off expect logging for a moment.
1159 # [ gdb_compile $ifile $ciexe_file executable $args ]
1160 # [ remote_exec $ciexe_file ]
1161 # [ source $ci_file.out ]
1163 # I could give up on -E and just do this.
1164 # I didn't get desperate enough to try this.
1166 # -- chastain 2004-01-06
1168 proc get_compiler_info {binfile args} {
1169 # For compiler.c and compiler.cc
1172 # I am going to play with the log to keep noise out.
1176 # These come from compiler.c or compiler.cc
1177 global compiler_info
1179 global hp_cc_compiler
1180 global hp_aCC_compiler
1181 global signed_keyword_not_used
1183 # Choose which file to preprocess.
1184 set ifile "${srcdir}/lib/compiler.c"
1185 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1186 set ifile "${srcdir}/lib/compiler.cc"
1189 # Run $ifile through the right preprocessor.
1190 # Toggle gdb.log to keep the compiler output out of the log.
1192 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1193 log_file -a "$outdir/$tool.log"
1195 # Source the output.
1196 foreach cppline [ split "$cppout" "\n" ] {
1197 if { ! [ regexp "^#" "$cppline" ] } {
1198 if { ! [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1199 verbose "get_compiler_info: $cppline" 2
1204 verbose -log "get_compiler_info: $compiler_info"
1206 # Most compilers will evaluate comparisons and other boolean
1207 # operations to 0 or 1.
1208 uplevel \#0 { set true 1 }
1209 uplevel \#0 { set false 0 }
1211 # Use of aCC results in boolean results being displayed as
1213 if { $hp_aCC_compiler } {
1214 uplevel \#0 { set true true }
1215 uplevel \#0 { set false false }
1221 proc test_compiler_info { compiler } {
1222 global compiler_info
1223 return [string match $compiler $compiler_info]
1226 set gdb_wrapper_initialized 0
1228 proc gdb_wrapper_init { args } {
1229 global gdb_wrapper_initialized;
1230 global gdb_wrapper_file;
1231 global gdb_wrapper_flags;
1233 if { $gdb_wrapper_initialized == 1 } { return; }
1235 if {[target_info exists needs_status_wrapper] && \
1236 [target_info needs_status_wrapper] != "0"} {
1237 set result [build_wrapper "testglue.o"];
1238 if { $result != "" } {
1239 set gdb_wrapper_file [lindex $result 0];
1240 set gdb_wrapper_flags [lindex $result 1];
1242 warning "Status wrapper failed to build."
1245 set gdb_wrapper_initialized 1
1248 proc gdb_compile {source dest type options} {
1249 global GDB_TESTCASE_OPTIONS;
1250 global gdb_wrapper_file;
1251 global gdb_wrapper_flags;
1252 global gdb_wrapper_initialized;
1254 if [target_info exists gdb_stub] {
1255 set options2 { "additional_flags=-Dusestubs" }
1256 lappend options "libs=[target_info gdb_stub]";
1257 set options [concat $options2 $options]
1259 if [target_info exists is_vxworks] {
1260 set options2 { "additional_flags=-Dvxworks" }
1261 lappend options "libs=[target_info gdb_stub]";
1262 set options [concat $options2 $options]
1264 if [info exists GDB_TESTCASE_OPTIONS] {
1265 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1267 verbose "options are $options"
1268 verbose "source is $source $dest $type $options"
1270 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
1272 if {[target_info exists needs_status_wrapper] && \
1273 [target_info needs_status_wrapper] != "0" && \
1274 [info exists gdb_wrapper_file]} {
1275 lappend options "libs=${gdb_wrapper_file}"
1276 lappend options "ldflags=${gdb_wrapper_flags}"
1279 set result [target_compile $source $dest $type $options];
1280 regsub "\[\r\n\]*$" "$result" "" result;
1281 regsub "^\[\r\n\]*" "$result" "" result;
1282 if { $result != "" && [lsearch $options quiet] == -1} {
1283 clone_output "gdb compile failed, $result"
1289 # This is just like gdb_compile, above, except that it tries compiling
1290 # against several different thread libraries, to see which one this
1292 proc gdb_compile_pthreads {source dest type options} {
1294 set why_msg "unrecognized error"
1295 foreach lib {-lpthreads -lpthread -lthread} {
1296 # This kind of wipes out whatever libs the caller may have
1297 # set. Or maybe theirs will override ours. How infelicitous.
1298 set options_with_lib [concat $options [list libs=$lib quiet]]
1299 set ccout [gdb_compile $source $dest $type $options_with_lib]
1300 switch -regexp -- $ccout {
1301 ".*no posix threads support.*" {
1302 set why_msg "missing threads include file"
1305 ".*cannot open -lpthread.*" {
1306 set why_msg "missing runtime threads library"
1308 ".*Can't find library for -lpthread.*" {
1309 set why_msg "missing runtime threads library"
1312 pass "successfully compiled posix threads test case"
1318 if {!$built_binfile} {
1319 unsupported "Couldn't compile $source: ${why_msg}"
1324 # This is just like gdb_compile_pthreads, above, except that we always add the
1325 # objc library for compiling Objective-C programs
1326 proc gdb_compile_objc {source dest type options} {
1328 set why_msg "unrecognized error"
1329 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
1330 # This kind of wipes out whatever libs the caller may have
1331 # set. Or maybe theirs will override ours. How infelicitous.
1332 if { $lib == "solaris" } {
1333 set lib "-lpthread -lposix4"
1335 if { $lib != "-lobjc" } {
1336 set lib "-lobjc $lib"
1338 set options_with_lib [concat $options [list libs=$lib quiet]]
1339 set ccout [gdb_compile $source $dest $type $options_with_lib]
1340 switch -regexp -- $ccout {
1341 ".*no posix threads support.*" {
1342 set why_msg "missing threads include file"
1345 ".*cannot open -lpthread.*" {
1346 set why_msg "missing runtime threads library"
1348 ".*Can't find library for -lpthread.*" {
1349 set why_msg "missing runtime threads library"
1352 pass "successfully compiled objc with posix threads test case"
1358 if {!$built_binfile} {
1359 unsupported "Couldn't compile $source: ${why_msg}"
1364 proc send_gdb { string } {
1365 global suppress_flag;
1366 if { $suppress_flag } {
1367 return "suppressed";
1369 return [remote_send host "$string"];
1375 proc gdb_expect { args } {
1376 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
1377 set gtimeout [lindex $args 0];
1378 set expcode [list [lindex $args 1]];
1380 upvar timeout timeout;
1383 if [target_info exists gdb,timeout] {
1384 if [info exists timeout] {
1385 if { $timeout < [target_info gdb,timeout] } {
1386 set gtimeout [target_info gdb,timeout];
1388 set gtimeout $timeout;
1391 set gtimeout [target_info gdb,timeout];
1395 if ![info exists gtimeout] {
1397 if [info exists timeout] {
1398 set gtimeout $timeout;
1405 global suppress_flag;
1406 global remote_suppress_flag;
1407 if [info exists remote_suppress_flag] {
1408 set old_val $remote_suppress_flag;
1410 if [info exists suppress_flag] {
1411 if { $suppress_flag } {
1412 set remote_suppress_flag 1;
1416 {uplevel remote_expect host $gtimeout $expcode} string];
1417 if [info exists old_val] {
1418 set remote_suppress_flag $old_val;
1420 if [info exists remote_suppress_flag] {
1421 unset remote_suppress_flag;
1426 global errorInfo errorCode;
1428 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1429 } elseif {$code == 2} {
1430 return -code return $string
1431 } elseif {$code == 3} {
1433 } elseif {$code > 4} {
1434 return -code $code $string
1438 # gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs
1440 # Check for long sequence of output by parts.
1441 # MESSAGE: is the test message to be printed with the test success/fail.
1442 # SENTINEL: Is the terminal pattern indicating that output has finished.
1443 # LIST: is the sequence of outputs to match.
1444 # If the sentinel is recognized early, it is considered an error.
1447 # 1 if the test failed,
1448 # 0 if the test passes,
1449 # -1 if there was an internal error.
1451 proc gdb_expect_list {test sentinel list} {
1453 global suppress_flag
1456 if { $suppress_flag } {
1458 unresolved "${test}"
1460 while { ${index} < [llength ${list}] } {
1461 set pattern [lindex ${list} ${index}]
1462 set index [expr ${index} + 1]
1463 if { ${index} == [llength ${list}] } {
1466 -re "${pattern}${sentinel}" {
1467 # pass "${test}, pattern ${index} + sentinel"
1470 fail "${test} (pattern ${index} + sentinel)"
1473 -re ".*A problem internal to GDB has been detected" {
1474 fail "${test} (GDB internal error)"
1476 gdb_internal_error_resync
1479 fail "${test} (pattern ${index} + sentinel) (timeout)"
1484 # unresolved "${test}, pattern ${index} + sentinel"
1490 # pass "${test}, pattern ${index}"
1493 fail "${test} (pattern ${index})"
1496 -re ".*A problem internal to GDB has been detected" {
1497 fail "${test} (GDB internal error)"
1499 gdb_internal_error_resync
1502 fail "${test} (pattern ${index}) (timeout)"
1507 # unresolved "${test}, pattern ${index}"
1521 proc gdb_suppress_entire_file { reason } {
1522 global suppress_flag;
1524 warning "$reason\n";
1525 set suppress_flag -1;
1529 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
1530 # gdb_expect to fail immediately (until the next call to
1531 # gdb_stop_suppressing_tests).
1533 proc gdb_suppress_tests { args } {
1534 global suppress_flag;
1536 return; # fnf - disable pending review of results where
1537 # testsuite ran better without this
1540 if { $suppress_flag == 1 } {
1541 if { [llength $args] > 0 } {
1542 warning "[lindex $args 0]\n";
1544 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1550 # Clear suppress_flag.
1552 proc gdb_stop_suppressing_tests { } {
1553 global suppress_flag;
1555 if [info exists suppress_flag] {
1556 if { $suppress_flag > 0 } {
1557 set suppress_flag 0;
1558 clone_output "Tests restarted.\n";
1561 set suppress_flag 0;
1565 proc gdb_clear_suppressed { } {
1566 global suppress_flag;
1568 set suppress_flag 0;
1571 proc gdb_start { } {
1576 catch default_gdb_exit
1580 # gdb_load -- load a file into the debugger.
1581 # return a -1 if anything goes wrong.
1583 proc gdb_load { arg } {
1584 return [gdb_file_cmd $arg]
1587 proc gdb_continue { function } {
1590 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
1593 proc default_gdb_init { args } {
1594 global gdb_wrapper_initialized
1596 gdb_clear_suppressed;
1598 # Make sure that the wrapper is rebuilt
1599 # with the appropriate multilib option.
1600 set gdb_wrapper_initialized 0
1602 # Uh, this is lame. Really, really, really lame. But there's this *one*
1603 # testcase that will fail in random places if we don't increase this.
1606 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
1607 if { [llength $args] > 0 } {
1610 set file [lindex $args 0];
1612 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
1615 if [target_info exists gdb_prompt] {
1616 set gdb_prompt [target_info gdb_prompt];
1618 set gdb_prompt "\\(gdb\\)"
1622 proc gdb_init { args } {
1623 return [eval default_gdb_init $args];
1626 proc gdb_finish { } {
1631 set debug_format "unknown"
1633 # Run the gdb command "info source" and extract the debugging format
1634 # information from the output and save it in debug_format.
1636 proc get_debug_format { } {
1642 set debug_format "unknown"
1643 send_gdb "info source\n"
1645 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
1646 set debug_format $expect_out(1,string)
1647 verbose "debug format is $debug_format"
1650 -re "No current source file.\r\n$gdb_prompt $" {
1651 perror "get_debug_format used when no current source file"
1654 -re "$gdb_prompt $" {
1655 warning "couldn't check debug format (no valid response)."
1659 warning "couldn't check debug format (timed out)."
1665 # Return true if FORMAT matches the debug format the current test was
1666 # compiled with. FORMAT is a shell-style globbing pattern; it can use
1667 # `*', `[...]', and so on.
1669 # This function depends on variables set by `get_debug_format', above.
1671 proc test_debug_format {format} {
1674 return [expr [string match $format $debug_format] != 0]
1677 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
1678 # COFF, stabs, etc). If that format matches the format that the
1679 # current test was compiled with, then the next test is expected to
1680 # fail for any target. Returns 1 if the next test or set of tests is
1681 # expected to fail, 0 otherwise (or if it is unknown). Must have
1682 # previously called get_debug_format.
1683 proc setup_xfail_format { format } {
1684 set ret [test_debug_format $format];
1692 proc gdb_step_for_stub { } {
1695 if ![target_info exists gdb,use_breakpoint_for_stub] {
1696 if [target_info exists gdb_stub_step_command] {
1697 set command [target_info gdb_stub_step_command];
1701 send_gdb "${command}\n";
1704 -re "(main.* at |.*in .*start).*$gdb_prompt" {
1707 -re ".*$gdb_prompt" {
1709 if { $tries == 5 } {
1710 fail "stepping out of breakpoint function";
1713 send_gdb "${command}\n";
1717 fail "stepping out of breakpoint function";
1724 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
1725 set file $expect_out(1,string);
1726 set linenum [expr $expect_out(2,string) + 1];
1727 set breakplace "${file}:${linenum}";
1731 send_gdb "break ${breakplace}\n";
1733 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
1734 set breakpoint $expect_out(1,string);
1736 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
1737 set breakpoint $expect_out(1,string);
1741 send_gdb "continue\n";
1743 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
1744 gdb_test "delete $breakpoint" ".*" "";
1751 ### gdb_get_line_number TEXT [FILE]
1753 ### Search the source file FILE, and return the line number of a line
1754 ### containing TEXT. Use this function instead of hard-coding line
1755 ### numbers into your test script.
1757 ### Specifically, this function uses GDB's "search" command to search
1758 ### FILE for the first line containing TEXT, and returns its line
1759 ### number. Thus, FILE must be a source file, compiled into the
1760 ### executable you are running. If omitted, FILE defaults to the
1761 ### value of the global variable `srcfile'; most test scripts set
1762 ### `srcfile' appropriately at the top anyway.
1764 ### Use this function to keep your test scripts independent of the
1765 ### exact line numbering of the source file. Don't write:
1767 ### send_gdb "break 20"
1769 ### This means that if anyone ever edits your test's source file,
1770 ### your test could break. Instead, put a comment like this on the
1771 ### source file line you want to break at:
1773 ### /* breakpoint spot: frotz.exp: test name */
1775 ### and then write, in your test script (which we assume is named
1778 ### send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
1780 ### (Yes, Tcl knows how to handle the nested quotes and brackets.
1783 ### % puts "foo [lindex "bar baz" 1]"
1786 ### Tcl is quite clever, for a little stringy language.)
1788 proc gdb_get_line_number {text {file /omitted/}} {
1792 if {! [string compare $file /omitted/]} {
1797 gdb_test "list ${file}:1,1" ".*" ""
1798 send_gdb "search ${text}\n"
1800 -re "\[\r\n\]+(\[0-9\]+)\[ \t\].*${text}.*$gdb_prompt $" {
1801 set result $expect_out(1,string)
1803 -re ".*$gdb_prompt $" {
1804 fail "find line number containing \"${text}\""
1807 fail "find line number containing \"${text}\" (timeout)"
1813 # gdb_continue_to_end:
1814 # The case where the target uses stubs has to be handled specially. If a
1815 # stub is used, we set a breakpoint at exit because we cannot rely on
1816 # exit() behavior of a remote target.
1818 # mssg is the error message that gets printed.
1820 proc gdb_continue_to_end {mssg} {
1821 if [target_info exists use_gdb_stub] {
1822 if {![gdb_breakpoint "exit"]} {
1825 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
1826 "continue until exit at $mssg"
1828 # Continue until we exit. Should not stop again.
1829 # Don't bother to check the output of the program, that may be
1830 # extremely tough for some remote systems.
1831 gdb_test "continue"\
1832 "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
1833 "continue until exit at $mssg"
1837 proc rerun_to_main {} {
1840 if [target_info exists use_gdb_stub] {
1843 -re ".*Breakpoint .*main .*$gdb_prompt $"\
1844 {pass "rerun to main" ; return 0}
1845 -re "$gdb_prompt $"\
1846 {fail "rerun to main" ; return 0}
1847 timeout {fail "(timeout) rerun to main" ; return 0}
1852 -re "The program .* has been started already.*y or n. $" {
1856 -re "Starting program.*$gdb_prompt $"\
1857 {pass "rerun to main" ; return 0}
1858 -re "$gdb_prompt $"\
1859 {fail "rerun to main" ; return 0}
1860 timeout {fail "(timeout) rerun to main" ; return 0}
1865 # Print a message and return true if a test should be skipped
1866 # due to lack of floating point suport.
1868 proc gdb_skip_float_test { msg } {
1869 if [target_info exists gdb,skip_float_tests] {
1870 verbose "Skipping test '$msg': no float tests.";
1876 # Print a message and return true if a test should be skipped
1877 # due to lack of stdio support.
1879 proc gdb_skip_stdio_test { msg } {
1880 if [target_info exists gdb,noinferiorio] {
1881 verbose "Skipping test '$msg': no inferior i/o.";
1887 proc gdb_skip_bogus_test { msg } {
1892 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
1893 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
1894 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
1895 # the name of a idebuginfo only file. This file will be stored in the
1896 # gdb.base/.debug subdirectory.
1898 # Functions for separate debug info testing
1900 # starting with an executable:
1901 # foo --> original executable
1903 # at the end of the process we have:
1904 # foo.stripped --> foo w/o debug info
1905 # .debug/foo.debug --> foo's debug info
1906 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
1908 # Return the name of the file in which we should stor EXEC's separated
1909 # debug info. EXEC contains the full path.
1910 proc separate_debug_filename { exec } {
1912 # In a .debug subdirectory off the same directory where the testcase
1913 # executable is going to be. Something like:
1914 # <your-path>/gdb/testsuite/gdb.base/.debug/blah.debug.
1915 # This is the default location where gdb expects to findi
1916 # the debug info file.
1918 set exec_dir [file dirname $exec]
1919 set exec_file [file tail $exec]
1920 set debug_dir [file join $exec_dir ".debug"]
1921 set debug_file [file join $debug_dir "${exec_file}.debug"]
1927 proc gdb_gnu_strip_debug { dest } {
1929 set debug_file [separate_debug_filename $dest]
1930 set strip_to_file_program strip
1931 set objcopy_program objcopy
1933 # Make sure the directory that will hold the separated debug
1934 # info actually exists.
1935 set debug_dir [file dirname $debug_file]
1936 if {! [file isdirectory $debug_dir]} {
1937 file mkdir $debug_dir
1940 set debug_link [file tail $debug_file]
1941 set stripped_file "${dest}.stripped"
1943 # Get rid of the debug info, and store result in stripped_file
1944 # something like gdb/testsuite/gdb.base/blah.stripped.
1945 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
1946 verbose "result is $result"
1947 verbose "output is $output"
1952 # Get rid of everything but the debug info, and store result in debug_file
1953 # This will be in the .debug subdirectory, see above.
1954 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
1955 verbose "result is $result"
1956 verbose "output is $output"
1961 # Link the two previous output files together, adding the .gnu_debuglink
1962 # section to the stripped_file, containing a pointer to the debug_file,
1963 # save the new file in dest.
1964 # This will be the regular executable filename, in the usual location.
1965 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
1966 verbose "result is $result"
1967 verbose "output is $output"