1 # Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2 # 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # This file was written by Fred Fish. (fnf@cygnus.com)
19 # Generic gdb subroutines that should work for any target. If these
20 # need to be modified for any target, it can be done with a variable
21 # or by passing arguments.
24 # Tests would fail, logs on get_compiler_info() would be missing.
25 send_error "`site.exp' not found, run `make site.exp'!\n"
33 if [info exists TOOL_EXECUTABLE] {
34 set GDB $TOOL_EXECUTABLE;
36 if ![info exists GDB] {
37 if ![is_remote host] {
38 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
40 set GDB [transform gdb];
43 verbose "using GDB = $GDB" 2
46 if ![info exists GDBFLAGS] {
49 verbose "using GDBFLAGS = $GDBFLAGS" 2
51 # The variable gdb_prompt is a regexp which matches the gdb prompt.
52 # Set it if it is not already set.
54 if ![info exists gdb_prompt] then {
55 set gdb_prompt "\[(\]gdb\[)\]"
58 # The variable fullname_syntax_POSIX is a regexp which matches a POSIX
59 # absolute path ie. /foo/
60 set fullname_syntax_POSIX "/.*/"
61 # The variable fullname_syntax_UNC is a regexp which matches a Windows
62 # UNC path ie. \\D\foo\
63 set fullname_syntax_UNC {\\\\[^\\]+\\.+\\}
64 # The variable fullname_syntax_DOS_CASE is a regexp which matches a
65 # particular DOS case that GDB most likely will output
66 # ie. \foo\, but don't match \\.*\
67 set fullname_syntax_DOS_CASE {\\[^\\].*\\}
68 # The variable fullname_syntax_DOS is a regexp which matches a DOS path
69 # ie. a:\foo\ && a:foo\
70 set fullname_syntax_DOS {[a-zA-Z]:.*\\}
71 # The variable fullname_syntax is a regexp which matches what GDB considers
72 # an absolute path. It is currently debatable if the Windows style paths
73 # d:foo and \abc should be considered valid as an absolute path.
74 # Also, the purpse of this regexp is not to recognize a well formed
75 # absolute path, but to say with certainty that a path is absolute.
76 set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
78 # Needed for some tests under Cygwin.
82 if ![info exists env(EXEEXT)] {
85 set EXEEXT $env(EXEEXT)
90 ### Only procedures should come after this point.
93 # gdb_version -- extract and print the version number of GDB
95 proc default_gdb_version {} {
99 set fileid [open "gdb_cmd" w];
102 set cmdfile [remote_download host "gdb_cmd"];
103 set output [remote_exec host "$GDB -nw --command $cmdfile"]
104 remote_file build delete "gdb_cmd";
105 remote_file host delete "$cmdfile";
106 set tmp [lindex $output 1];
108 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
109 if ![is_remote host] {
110 clone_output "[which $GDB] version $version $GDBFLAGS\n"
112 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
116 proc gdb_version { } {
117 return [default_gdb_version];
121 # gdb_unload -- unload a file if one is loaded
130 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
131 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
132 -re "A program is being debugged already..*Kill it.*y or n. $"\
134 verbose "\t\tKilling previous program being debugged"
137 -re "Discard symbol table from .*y or n.*$" {
141 -re "$gdb_prompt $" {}
143 perror "couldn't unload file in $GDB (timed out)."
149 # Many of the tests depend on setting breakpoints at various places and
150 # running until that breakpoint is reached. At times, we want to start
151 # with a clean-slate with respect to breakpoints, so this utility proc
152 # lets us do this without duplicating this code everywhere.
155 proc delete_breakpoints {} {
158 # we need a larger timeout value here or this thing just confuses
159 # itself. May need a better implementation if possible. - guo
161 send_gdb "delete breakpoints\n"
163 -re "Delete all breakpoints.*y or n.*$" {
167 -re "$gdb_prompt $" { # This happens if there were no breakpoints
169 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
171 send_gdb "info breakpoints\n"
173 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
174 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
175 -re "Delete all breakpoints.*or n.*$" {
179 timeout { perror "info breakpoints (timeout)" ; return }
185 # Generic run command.
187 # The second pattern below matches up to the first newline *only*.
188 # Using ``.*$'' could swallow up output that we attempt to match
191 proc gdb_run_cmd {args} {
194 if [target_info exists gdb_init_command] {
195 send_gdb "[target_info gdb_init_command]\n";
197 -re "$gdb_prompt $" { }
199 perror "gdb_init_command for target failed";
205 if [target_info exists use_gdb_stub] {
206 if [target_info exists gdb,do_reload_on_run] {
207 if { [gdb_reload] != 0 } {
210 send_gdb "continue\n";
212 -re "Continu\[^\r\n\]*\[\r\n\]" {}
218 if [target_info exists gdb,start_symbol] {
219 set start [target_info gdb,start_symbol];
223 send_gdb "jump *$start\n"
225 while { $start_attempt } {
226 # Cap (re)start attempts at three to ensure that this loop
227 # always eventually fails. Don't worry about trying to be
228 # clever and not send a command when it has failed.
229 if [expr $start_attempt > 3] {
230 perror "Jump to start() failed (retry count exceeded)";
233 set start_attempt [expr $start_attempt + 1];
235 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
238 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
239 perror "Can't find start symbol to run in gdb_run";
242 -re "No symbol \"start\" in current.*$gdb_prompt $" {
243 send_gdb "jump *_start\n";
245 -re "No symbol.*context.*$gdb_prompt $" {
248 -re "Line.* Jump anyway.*y or n. $" {
251 -re "The program is not being run.*$gdb_prompt $" {
252 if { [gdb_reload] != 0 } {
255 send_gdb "jump *$start\n";
258 perror "Jump to start() failed (timeout)";
263 if [target_info exists gdb_stub] {
265 -re "$gdb_prompt $" {
266 send_gdb "continue\n"
273 if [target_info exists gdb,do_reload_on_run] {
274 if { [gdb_reload] != 0 } {
278 send_gdb "run $args\n"
279 # This doesn't work quite right yet.
280 # Use -notransfer here so that test cases (like chng-sym.exp)
281 # may test for additional start-up messages.
283 -re "The program .* has been started already.*y or n. $" {
287 -notransfer -re "Starting program: \[^\r\n\]*" {}
291 # Generic start command. Return 0 if we could start the program, -1
294 proc gdb_start_cmd {args} {
297 if [target_info exists gdb_init_command] {
298 send_gdb "[target_info gdb_init_command]\n";
300 -re "$gdb_prompt $" { }
302 perror "gdb_init_command for target failed";
308 if [target_info exists use_gdb_stub] {
312 send_gdb "start $args\n"
314 -re "The program .* has been started already.*y or n. $" {
318 # Use -notransfer here so that test cases (like chng-sym.exp)
319 # may test for additional start-up messages.
320 -notransfer -re "Starting program: \[^\r\n\]*" {
327 # Set a breakpoint at FUNCTION. If there is an additional argument it is
328 # a list of options; the supported options are allow-pending, temporary,
331 proc gdb_breakpoint { function args } {
335 set pending_response n
336 if {[lsearch -exact [lindex $args 0] allow-pending] != -1} {
337 set pending_response y
340 set break_command "break"
341 set break_message "Breakpoint"
342 if {[lsearch -exact [lindex $args 0] temporary] != -1} {
343 set break_command "tbreak"
344 set break_message "Temporary breakpoint"
348 if {[lsearch -exact [lindex $args 0] no-message] != -1} {
352 send_gdb "$break_command $function\n"
353 # The first two regexps are what we get with -g, the third is without -g.
355 -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
356 -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
357 -re "$break_message \[0-9\]* at .*$gdb_prompt $" {}
358 -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
359 if {$pending_response == "n"} {
360 if { $no_message == 0 } {
361 fail "setting breakpoint at $function"
366 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
367 send_gdb "$pending_response\n"
370 -re "$gdb_prompt $" {
371 if { $no_message == 0 } {
372 fail "setting breakpoint at $function"
377 if { $no_message == 0 } {
378 fail "setting breakpoint at $function (timeout)"
386 # Set breakpoint at function and run gdb until it breaks there.
387 # Since this is the only breakpoint that will be set, if it stops
388 # at a breakpoint, we will assume it is the one we want. We can't
389 # just compare to "function" because it might be a fully qualified,
390 # single quoted C++ function specifier. If there's an additional argument,
391 # pass it to gdb_breakpoint.
393 proc runto { function args } {
399 if ![gdb_breakpoint $function [lindex $args 0]] {
405 # the "at foo.c:36" output we get with -g.
406 # the "in func" output we get without -g.
408 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
411 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
414 -re "$gdb_prompt $" {
415 fail "running to $function in runto"
419 fail "running to $function in runto (timeout)"
427 # runto_main -- ask gdb to run until we hit a breakpoint at main.
428 # The case where the target uses stubs has to be handled
429 # specially--if it uses stubs, assuming we hit
430 # breakpoint() and just step out of the function.
432 proc runto_main { } {
436 if ![target_info exists gdb_stub] {
448 ### Continue, and expect to hit a breakpoint.
449 ### Report a pass or fail, depending on whether it seems to have
450 ### worked. Use NAME as part of the test name; each call to
451 ### continue_to_breakpoint should use a NAME which is unique within
453 proc gdb_continue_to_breakpoint {name {location_pattern .*}} {
455 set full_name "continue to breakpoint: $name"
457 send_gdb "continue\n"
459 -re "Breakpoint .* at $location_pattern\r\n$gdb_prompt $" {
462 -re ".*$gdb_prompt $" {
466 fail "$full_name (timeout)"
472 # gdb_internal_error_resync:
474 # Answer the questions GDB asks after it reports an internal error
475 # until we get back to a GDB prompt. Decline to quit the debugging
476 # session, and decline to create a core file. Return non-zero if the
479 # This procedure just answers whatever questions come up until it sees
480 # a GDB prompt; it doesn't require you to have matched the input up to
481 # any specific point. However, it only answers questions it sees in
482 # the output itself, so if you've matched a question, you had better
483 # answer it yourself before calling this.
485 # You can use this function thus:
489 # -re ".*A problem internal to GDB has been detected" {
490 # gdb_internal_error_resync
495 proc gdb_internal_error_resync {} {
499 while {$count < 10} {
501 -re "Quit this debugging session\\? \\(y or n\\) $" {
505 -re "Create a core file of GDB\\? \\(y or n\\) $" {
509 -re "$gdb_prompt $" {
510 # We're resynchronized.
514 perror "Could not resync from internal error (timeout)"
519 perror "Could not resync from internal error (resync count exceeded)"
524 # gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
525 # Send a command to gdb; test the result.
527 # COMMAND is the command to execute, send to GDB with send_gdb. If
528 # this is the null string no command is sent.
529 # MESSAGE is a message to be printed with the built-in failure patterns
530 # if one of them matches. If MESSAGE is empty COMMAND will be used.
531 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
532 # patterns. Pattern elements will be evaluated in the caller's
533 # context; action elements will be executed in the caller's context.
534 # Unlike patterns for gdb_test, these patterns should generally include
535 # the final newline and prompt.
538 # 1 if the test failed, according to a built-in failure pattern
539 # 0 if only user-supplied patterns matched
540 # -1 if there was an internal error.
542 # You can use this function thus:
544 # gdb_test_multiple "print foo" "test foo" {
545 # -re "expected output 1" {
548 # -re "expected output 2" {
553 # The standard patterns, such as "Program exited..." and "A problem
554 # ...", all being implicitly appended to that list.
556 proc gdb_test_multiple { command message user_code } {
560 upvar timeout timeout
561 upvar expect_out expect_out
563 if { $message == "" } {
567 # TCL/EXPECT WART ALERT
568 # Expect does something very strange when it receives a single braced
569 # argument. It splits it along word separators and performs substitutions.
570 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
571 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
572 # double-quoted list item, "\[ab\]" is just a long way of representing
573 # "[ab]", because the backslashes will be removed by lindex.
575 # Unfortunately, there appears to be no easy way to duplicate the splitting
576 # that expect will do from within TCL. And many places make use of the
577 # "\[0-9\]" construct, so we need to support that; and some places make use
578 # of the "[func]" construct, so we need to support that too. In order to
579 # get this right we have to substitute quoted list elements differently
580 # from braced list elements.
582 # We do this roughly the same way that Expect does it. We have to use two
583 # lists, because if we leave unquoted newlines in the argument to uplevel
584 # they'll be treated as command separators, and if we escape newlines
585 # we mangle newlines inside of command blocks. This assumes that the
586 # input doesn't contain a pattern which contains actual embedded newlines
589 regsub -all {\n} ${user_code} { } subst_code
590 set subst_code [uplevel list $subst_code]
592 set processed_code ""
594 set expecting_action 0
595 foreach item $user_code subst_item $subst_code {
596 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
597 lappend processed_code $item
600 if {$item == "-indices" || $item == "-re" || $item == "-ex"} {
601 lappend processed_code $item
604 if { $expecting_action } {
605 lappend processed_code "uplevel [list $item]"
606 set expecting_action 0
607 # Cosmetic, no effect on the list.
608 append processed_code "\n"
611 set expecting_action 1
612 lappend processed_code $subst_item
613 if {$patterns != ""} {
616 append patterns "\"$subst_item\""
619 # Also purely cosmetic.
620 regsub -all {\r} $patterns {\\r} patterns
621 regsub -all {\n} $patterns {\\n} patterns
624 send_user "Sending \"$command\" to gdb\n"
625 send_user "Looking to match \"$patterns\"\n"
626 send_user "Message is \"$message\"\n"
630 set string "${command}\n";
631 if { $command != "" } {
632 while { "$string" != "" } {
633 set foo [string first "\n" "$string"];
634 set len [string length "$string"];
635 if { $foo < [expr $len - 1] } {
636 set str [string range "$string" 0 $foo];
637 if { [send_gdb "$str"] != "" } {
638 global suppress_flag;
640 if { ! $suppress_flag } {
641 perror "Couldn't send $command to GDB.";
646 # since we're checking if each line of the multi-line
647 # command are 'accepted' by GDB here,
648 # we need to set -notransfer expect option so that
649 # command output is not lost for pattern matching
652 -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
653 timeout { verbose "partial: timeout" 3 }
655 set string [string range "$string" [expr $foo + 1] end];
660 if { "$string" != "" } {
661 if { [send_gdb "$string"] != "" } {
662 global suppress_flag;
664 if { ! $suppress_flag } {
665 perror "Couldn't send $command to GDB.";
673 if [target_info exists gdb,timeout] {
674 set tmt [target_info gdb,timeout];
676 if [info exists timeout] {
680 if [info exists timeout] {
689 -re ".*A problem internal to GDB has been detected" {
690 fail "$message (GDB internal error)"
691 gdb_internal_error_resync
693 -re "\\*\\*\\* DOSEXIT code.*" {
694 if { $message != "" } {
697 gdb_suppress_entire_file "GDB died";
701 append code $processed_code
703 -re "Ending remote debugging.*$gdb_prompt $" {
704 if ![isnative] then {
705 warning "Can`t communicate to remote target."
711 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
712 perror "Undefined command \"$command\"."
716 -re "Ambiguous command.*$gdb_prompt $" {
717 perror "\"$command\" is not a unique command name."
721 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
722 if ![string match "" $message] then {
723 set errmsg "$message (the program exited)"
725 set errmsg "$command (the program exited)"
730 -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" {
731 if ![string match "" $message] then {
732 set errmsg "$message (the program exited)"
734 set errmsg "$command (the program exited)"
739 -re "The program is not being run.*$gdb_prompt $" {
740 if ![string match "" $message] then {
741 set errmsg "$message (the program is no longer running)"
743 set errmsg "$command (the program is no longer running)"
748 -re "\r\n$gdb_prompt $" {
749 if ![string match "" $message] then {
756 perror "Window too small."
760 -re "\\(y or n\\) " {
762 perror "Got interactive prompt."
767 perror "Process no longer exists"
768 if { $message != "" } {
774 perror "internal buffer is full."
779 if ![string match "" $message] then {
780 fail "$message (timeout)"
787 set code [catch {gdb_expect $tmt $code} string]
789 global errorInfo errorCode;
790 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
791 } elseif {$code == 2} {
792 return -code return $string
793 } elseif {$code == 3} {
795 } elseif {$code > 4} {
796 return -code $code $string
801 # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
802 # Send a command to gdb; test the result.
804 # COMMAND is the command to execute, send to GDB with send_gdb. If
805 # this is the null string no command is sent.
806 # PATTERN is the pattern to match for a PASS, and must NOT include
807 # the \r\n sequence immediately before the gdb prompt.
808 # MESSAGE is an optional message to be printed. If this is
809 # omitted, then the pass/fail messages use the command string as the
810 # message. (If this is the empty string, then sometimes we don't
811 # call pass or fail at all; I don't understand this at all.)
812 # QUESTION is a question GDB may ask in response to COMMAND, like
814 # RESPONSE is the response to send if QUESTION appears.
817 # 1 if the test failed,
818 # 0 if the test passes,
819 # -1 if there was an internal error.
821 proc gdb_test { args } {
825 upvar timeout timeout
827 if [llength $args]>2 then {
828 set message [lindex $args 2]
830 set message [lindex $args 0]
832 set command [lindex $args 0]
833 set pattern [lindex $args 1]
835 if [llength $args]==5 {
836 set question_string [lindex $args 3];
837 set response_string [lindex $args 4];
839 set question_string "^FOOBAR$"
842 return [gdb_test_multiple $command $message {
843 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
844 if ![string match "" $message] then {
848 -re "(${question_string})$" {
849 send_gdb "$response_string\n";
855 # Test that a command gives an error. For pass or fail, return
856 # a 1 to indicate that more tests can proceed. However a timeout
857 # is a serious error, generates a special fail message, and causes
858 # a 0 to be returned to indicate that more tests are likely to fail
861 proc test_print_reject { args } {
865 if [llength $args]==2 then {
866 set expectthis [lindex $args 1]
868 set expectthis "should never match this bogus string"
870 set sendthis [lindex $args 0]
872 send_user "Sending \"$sendthis\" to gdb\n"
873 send_user "Looking to match \"$expectthis\"\n"
875 send_gdb "$sendthis\n"
876 #FIXME: Should add timeout as parameter.
878 -re "A .* in expression.*\\.*$gdb_prompt $" {
879 pass "reject $sendthis"
882 -re "Invalid syntax in expression.*$gdb_prompt $" {
883 pass "reject $sendthis"
886 -re "Junk after end of expression.*$gdb_prompt $" {
887 pass "reject $sendthis"
890 -re "Invalid number.*$gdb_prompt $" {
891 pass "reject $sendthis"
894 -re "Invalid character constant.*$gdb_prompt $" {
895 pass "reject $sendthis"
898 -re "No symbol table is loaded.*$gdb_prompt $" {
899 pass "reject $sendthis"
902 -re "No symbol .* in current context.*$gdb_prompt $" {
903 pass "reject $sendthis"
906 -re "Unmatched single quote.*$gdb_prompt $" {
907 pass "reject $sendthis"
910 -re "A character constant must contain at least one character.*$gdb_prompt $" {
911 pass "reject $sendthis"
914 -re "$expectthis.*$gdb_prompt $" {
915 pass "reject $sendthis"
918 -re ".*$gdb_prompt $" {
919 fail "reject $sendthis"
923 fail "reject $sendthis (eof or timeout)"
929 # Given an input string, adds backslashes as needed to create a
930 # regexp that will match the string.
932 proc string_to_regexp {str} {
934 regsub -all {[]*+.|()^$\[\\]} $str {\\&} result
938 # Same as gdb_test, but the second parameter is not a regexp,
939 # but a string that must match exactly.
941 proc gdb_test_exact { args } {
942 upvar timeout timeout
944 set command [lindex $args 0]
946 # This applies a special meaning to a null string pattern. Without
947 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
948 # messages from commands that should have no output except a new
949 # prompt. With this, only results of a null string will match a null
952 set pattern [lindex $args 1]
953 if [string match $pattern ""] {
954 set pattern [string_to_regexp [lindex $args 0]]
956 set pattern [string_to_regexp [lindex $args 1]]
959 # It is most natural to write the pattern argument with only
960 # embedded \n's, especially if you are trying to avoid Tcl quoting
961 # problems. But gdb_expect really wants to see \r\n in patterns. So
962 # transform the pattern here. First transform \r\n back to \n, in
963 # case some users of gdb_test_exact already do the right thing.
964 regsub -all "\r\n" $pattern "\n" pattern
965 regsub -all "\n" $pattern "\r\n" pattern
966 if [llength $args]==3 then {
967 set message [lindex $args 2]
972 return [gdb_test $command $pattern $message]
975 proc gdb_reinitialize_dir { subdir } {
978 if [is_remote host] {
983 -re "Reinitialize source path to empty.*y or n. " {
986 -re "Source directories searched.*$gdb_prompt $" {
987 send_gdb "dir $subdir\n"
989 -re "Source directories searched.*$gdb_prompt $" {
990 verbose "Dir set to $subdir"
992 -re "$gdb_prompt $" {
993 perror "Dir \"$subdir\" failed."
997 -re "$gdb_prompt $" {
998 perror "Dir \"$subdir\" failed."
1002 -re "$gdb_prompt $" {
1003 perror "Dir \"$subdir\" failed."
1009 # gdb_exit -- exit the GDB, killing the target program if necessary
1011 proc default_gdb_exit {} {
1015 global gdb_spawn_id;
1017 gdb_stop_suppressing_tests;
1019 if ![info exists gdb_spawn_id] {
1023 verbose "Quitting $GDB $GDBFLAGS"
1025 if { [is_remote host] && [board_info host exists fileid] } {
1032 -re "DOSEXIT code" { }
1037 if ![is_remote host] {
1043 # Load a file into the debugger.
1044 # The return value is 0 for success, -1 for failure.
1046 # This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1047 # to one of these values:
1049 # debug file was loaded successfully and has debug information
1050 # nodebug file was loaded successfully and has no debug information
1051 # fail file was not loaded
1053 # I tried returning this information as part of the return value,
1054 # but ran into a mess because of the many re-implementations of
1055 # gdb_load in config/*.exp.
1057 # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
1058 # this if they can get more information set.
1060 proc gdb_file_cmd { arg } {
1064 global last_loaded_file
1066 set last_loaded_file $arg
1068 # Set whether debug info was found.
1069 # Default to "fail".
1070 global gdb_file_cmd_debug_info
1071 set gdb_file_cmd_debug_info "fail"
1073 if [is_remote host] {
1074 set arg [remote_download host $arg]
1076 perror "download failed"
1081 # The file command used to kill the remote target. For the benefit
1082 # of the testsuite, preserve this behavior.
1085 -re "Kill the program being debugged. .y or n. $" {
1087 verbose "\t\tKilling previous program being debugged"
1090 -re "$gdb_prompt $" {
1095 send_gdb "file $arg\n"
1097 -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" {
1098 verbose "\t\tLoaded $arg into the $GDB with no debugging symbols"
1099 set gdb_file_cmd_debug_info "nodebug"
1102 -re "Reading symbols from.*done.*$gdb_prompt $" {
1103 verbose "\t\tLoaded $arg into the $GDB"
1104 set gdb_file_cmd_debug_info "debug"
1107 -re "Load new symbol table from \".*\".*y or n. $" {
1110 -re "Reading symbols from.*done.*$gdb_prompt $" {
1111 verbose "\t\tLoaded $arg with new symbol table into $GDB"
1112 set gdb_file_cmd_debug_info "debug"
1116 perror "(timeout) Couldn't load $arg, other program already loaded."
1121 -re "No such file or directory.*$gdb_prompt $" {
1122 perror "($arg) No such file or directory"
1125 -re "$gdb_prompt $" {
1126 perror "couldn't load $arg into $GDB."
1130 perror "couldn't load $arg into $GDB (timed out)."
1134 # This is an attempt to detect a core dump, but seems not to
1135 # work. Perhaps we need to match .* followed by eof, in which
1136 # gdb_expect does not seem to have a way to do that.
1137 perror "couldn't load $arg into $GDB (end of file)."
1144 # start gdb -- start gdb running, default procedure
1146 # When running over NFS, particularly if running many simultaneous
1147 # tests on different hosts all using the same server, things can
1148 # get really slow. Give gdb at least 3 minutes to start up.
1150 proc default_gdb_start { } {
1156 global gdb_spawn_id;
1158 gdb_stop_suppressing_tests;
1160 verbose "Spawning $GDB -nw $GDBFLAGS"
1162 if [info exists gdb_spawn_id] {
1166 if ![is_remote host] {
1167 if { [which $GDB] == 0 } then {
1168 perror "$GDB does not exist."
1172 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
1173 if { $res < 0 || $res == "" } {
1174 perror "Spawning $GDB failed."
1178 -re "\[\r\n\]$gdb_prompt $" {
1179 verbose "GDB initialized."
1181 -re "$gdb_prompt $" {
1182 perror "GDB never initialized."
1186 perror "(timeout) GDB never initialized after 10 seconds."
1191 set gdb_spawn_id -1;
1192 # force the height to "unlimited", so no pagers get used
1194 send_gdb "set height 0\n"
1196 -re "$gdb_prompt $" {
1197 verbose "Setting height to 0." 2
1200 warning "Couldn't set the height to 0"
1203 # force the width to "unlimited", so no wraparound occurs
1204 send_gdb "set width 0\n"
1206 -re "$gdb_prompt $" {
1207 verbose "Setting width to 0." 2
1210 warning "Couldn't set the width to 0."
1216 # Return a 1 for configurations for which we don't even want to try to
1219 proc skip_cplus_tests {} {
1220 if { [istarget "h8300-*-*"] } {
1224 # The C++ IO streams are too large for HC11/HC12 and are thus not
1225 # available. The gdb C++ tests use them and don't compile.
1226 if { [istarget "m6811-*-*"] } {
1229 if { [istarget "m6812-*-*"] } {
1235 # Return a 1 if I don't even want to try to test FORTRAN.
1237 proc skip_fortran_tests {} {
1241 # Return a 1 if we should skip shared library tests.
1243 proc skip_shlib_tests {} {
1244 # Run the shared library tests on native systems.
1249 # An abbreviated list of remote targets where we should be able to
1250 # run shared library tests.
1251 if {([istarget *-*-linux*]
1252 || [istarget *-*-*bsd*]
1253 || [istarget *-*-solaris2*]
1254 || [istarget arm*-*-symbianelf*]
1255 || [istarget *-*-mingw*]
1256 || [istarget *-*-cygwin*]
1257 || [istarget *-*-pe*])} {
1264 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1265 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1267 proc skip_altivec_tests {} {
1268 global skip_vmx_tests_saved
1269 global srcdir subdir gdb_prompt
1271 # Use the cached value, if it exists.
1272 set me "skip_altivec_tests"
1273 if [info exists skip_vmx_tests_saved] {
1274 verbose "$me: returning saved $skip_vmx_tests_saved" 2
1275 return $skip_vmx_tests_saved
1278 # Some simulators are known to not support VMX instructions.
1279 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1280 verbose "$me: target known to not support VMX, returning 1" 2
1281 return [set skip_vmx_tests_saved 1]
1284 # Make sure we have a compiler that understands altivec.
1285 set compile_flags {debug nowarnings}
1286 if [get_compiler_info not-used] {
1287 warning "Could not get compiler info"
1290 if [test_compiler_info gcc*] {
1291 set compile_flags "$compile_flags additional_flags=-maltivec"
1292 } elseif [test_compiler_info xlc*] {
1293 set compile_flags "$compile_flags additional_flags=-qaltivec"
1295 verbose "Could not compile with altivec support, returning 1" 2
1299 # Set up, compile, and execute a test program containing VMX instructions.
1300 # Include the current process ID in the file names to prevent conflicts
1301 # with invocations for multiple testsuites.
1305 set f [open $src "w"]
1306 puts $f "int main() {"
1307 puts $f "#ifdef __MACH__"
1308 puts $f " asm volatile (\"vor v0,v0,v0\");"
1310 puts $f " asm volatile (\"vor 0,0,0\");"
1312 puts $f " return 0; }"
1315 verbose "$me: compiling testfile $src" 2
1316 set lines [gdb_compile $src $exe executable $compile_flags]
1319 if ![string match "" $lines] then {
1320 verbose "$me: testfile compilation failed, returning 1" 2
1321 return [set skip_vmx_tests_saved 1]
1324 # No error message, compilation succeeded so now run it via gdb.
1328 gdb_reinitialize_dir $srcdir/$subdir
1332 -re ".*Illegal instruction.*${gdb_prompt} $" {
1333 verbose -log "\n$me altivec hardware not detected"
1334 set skip_vmx_tests_saved 1
1336 -re ".*Program exited normally.*${gdb_prompt} $" {
1337 verbose -log "\n$me: altivec hardware detected"
1338 set skip_vmx_tests_saved 0
1341 warning "\n$me: default case taken"
1342 set skip_vmx_tests_saved 1
1346 remote_file build delete $exe
1348 verbose "$me: returning $skip_vmx_tests_saved" 2
1349 return $skip_vmx_tests_saved
1352 # Skip all the tests in the file if you are not on an hppa running
1355 proc skip_hp_tests {} {
1356 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
1357 verbose "Skip hp tests is $skip_hp"
1361 set compiler_info "unknown"
1363 set hp_cc_compiler 0
1364 set hp_aCC_compiler 0
1366 # Figure out what compiler I am using.
1368 # BINFILE is a "compiler information" output file. This implementation
1369 # does not use BINFILE.
1371 # ARGS can be empty or "C++". If empty, "C" is assumed.
1373 # There are several ways to do this, with various problems.
1375 # [ gdb_compile -E $ifile -o $binfile.ci ]
1376 # source $binfile.ci
1378 # Single Unix Spec v3 says that "-E -o ..." together are not
1379 # specified. And in fact, the native compiler on hp-ux 11 (among
1380 # others) does not work with "-E -o ...". Most targets used to do
1381 # this, and it mostly worked, because it works with gcc.
1383 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1384 # source $binfile.ci
1386 # This avoids the problem with -E and -o together. This almost works
1387 # if the build machine is the same as the host machine, which is
1388 # usually true of the targets which are not gcc. But this code does
1389 # not figure which compiler to call, and it always ends up using the C
1390 # compiler. Not good for setting hp_aCC_compiler. Targets
1391 # hppa*-*-hpux* and mips*-*-irix* used to do this.
1393 # [ gdb_compile -E $ifile > $binfile.ci ]
1394 # source $binfile.ci
1396 # dejagnu target_compile says that it supports output redirection,
1397 # but the code is completely different from the normal path and I
1398 # don't want to sweep the mines from that path. So I didn't even try
1401 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1404 # I actually do this for all targets now. gdb_compile runs the right
1405 # compiler, and TCL captures the output, and I eval the output.
1407 # Unfortunately, expect logs the output of the command as it goes by,
1408 # and dejagnu helpfully prints a second copy of it right afterwards.
1409 # So I turn off expect logging for a moment.
1411 # [ gdb_compile $ifile $ciexe_file executable $args ]
1412 # [ remote_exec $ciexe_file ]
1413 # [ source $ci_file.out ]
1415 # I could give up on -E and just do this.
1416 # I didn't get desperate enough to try this.
1418 # -- chastain 2004-01-06
1420 proc get_compiler_info {binfile args} {
1421 # For compiler.c and compiler.cc
1424 # I am going to play with the log to keep noise out.
1428 # These come from compiler.c or compiler.cc
1429 global compiler_info
1431 # Legacy global data symbols.
1433 global hp_cc_compiler
1434 global hp_aCC_compiler
1436 # Choose which file to preprocess.
1437 set ifile "${srcdir}/lib/compiler.c"
1438 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1439 set ifile "${srcdir}/lib/compiler.cc"
1442 # Run $ifile through the right preprocessor.
1443 # Toggle gdb.log to keep the compiler output out of the log.
1445 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1446 log_file -a "$outdir/$tool.log"
1450 foreach cppline [ split "$cppout" "\n" ] {
1451 if { [ regexp "^#" "$cppline" ] } {
1453 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1455 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
1457 verbose "get_compiler_info: $cppline" 2
1461 verbose -log "get_compiler_info: $cppline"
1466 # Reset to unknown compiler if any diagnostics happened.
1468 set compiler_info "unknown"
1471 # Set the legacy symbols.
1473 set hp_cc_compiler 0
1474 set hp_aCC_compiler 0
1475 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
1476 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
1477 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
1478 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
1479 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
1480 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
1481 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
1483 # Log what happened.
1484 verbose -log "get_compiler_info: $compiler_info"
1486 # Most compilers will evaluate comparisons and other boolean
1487 # operations to 0 or 1.
1488 uplevel \#0 { set true 1 }
1489 uplevel \#0 { set false 0 }
1491 # Use of aCC results in boolean results being displayed as
1493 if { $hp_aCC_compiler } {
1494 uplevel \#0 { set true true }
1495 uplevel \#0 { set false false }
1501 proc test_compiler_info { {compiler ""} } {
1502 global compiler_info
1504 # if no arg, return the compiler_info string
1506 if [string match "" $compiler] {
1507 if [info exists compiler_info] {
1508 return $compiler_info
1510 perror "No compiler info found."
1514 return [string match $compiler $compiler_info]
1517 set gdb_wrapper_initialized 0
1519 proc gdb_wrapper_init { args } {
1520 global gdb_wrapper_initialized;
1521 global gdb_wrapper_file;
1522 global gdb_wrapper_flags;
1524 if { $gdb_wrapper_initialized == 1 } { return; }
1526 if {[target_info exists needs_status_wrapper] && \
1527 [target_info needs_status_wrapper] != "0"} {
1528 set result [build_wrapper "testglue.o"];
1529 if { $result != "" } {
1530 set gdb_wrapper_file [lindex $result 0];
1531 set gdb_wrapper_flags [lindex $result 1];
1533 warning "Status wrapper failed to build."
1536 set gdb_wrapper_initialized 1
1539 # Some targets need to always link a special object in. Save its path here.
1540 global gdb_saved_set_unbuffered_mode_obj
1541 set gdb_saved_set_unbuffered_mode_obj ""
1543 proc gdb_compile {source dest type options} {
1544 global GDB_TESTCASE_OPTIONS;
1545 global gdb_wrapper_file;
1546 global gdb_wrapper_flags;
1547 global gdb_wrapper_initialized;
1550 global gdb_saved_set_unbuffered_mode_obj
1552 set outdir [file dirname $dest]
1554 # Add platform-specific options if a shared library was specified using
1555 # "shlib=librarypath" in OPTIONS.
1558 foreach opt $options {
1559 if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
1560 if [test_compiler_info "xlc-*"] {
1561 # IBM xlc compiler doesn't accept shared library named other
1562 # than .so: use "-Wl," to bypass this
1563 lappend source "-Wl,$shlib_name"
1564 } elseif { ([istarget "*-*-mingw*"]
1565 || [istarget *-*-cygwin*]
1566 || [istarget *-*-pe*])} {
1567 lappend source "${shlib_name}.a"
1569 lappend source $shlib_name
1571 if {$shlib_found == 0} {
1573 if { ([test_compiler_info "gcc-*"]
1574 && ([istarget "powerpc*-*-aix*"]
1575 || [istarget "rs6000*-*-aix*"] )) } {
1576 lappend options "additional_flags=-L${outdir}"
1577 } elseif { [istarget "mips-sgi-irix*"] } {
1578 lappend options "additional_flags=-rpath ${outdir}"
1581 } elseif { $opt == "shlib_load" } {
1582 if { ([istarget "*-*-mingw*"]
1583 || [istarget *-*-cygwin*]
1584 || [istarget *-*-pe*]
1585 || [istarget arm*-*-symbianelf*]
1586 || [istarget hppa*-*-hpux*])} {
1587 # Do not need anything.
1588 } elseif { [istarget *-*-openbsd*] } {
1589 lappend new_options "additional_flags=-Wl,-rpath,${outdir}"
1591 lappend new_options "libs=-ldl"
1592 lappend new_options "additional_flags=-Wl,-rpath,\\\$ORIGIN"
1595 lappend new_options $opt
1598 set options $new_options
1600 if [target_info exists gdb_stub] {
1601 set options2 { "additional_flags=-Dusestubs" }
1602 lappend options "libs=[target_info gdb_stub]";
1603 set options [concat $options2 $options]
1605 if [target_info exists is_vxworks] {
1606 set options2 { "additional_flags=-Dvxworks" }
1607 lappend options "libs=[target_info gdb_stub]";
1608 set options [concat $options2 $options]
1610 if [info exists GDB_TESTCASE_OPTIONS] {
1611 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1613 verbose "options are $options"
1614 verbose "source is $source $dest $type $options"
1616 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
1618 if {[target_info exists needs_status_wrapper] && \
1619 [target_info needs_status_wrapper] != "0" && \
1620 [info exists gdb_wrapper_file]} {
1621 lappend options "libs=${gdb_wrapper_file}"
1622 lappend options "ldflags=${gdb_wrapper_flags}"
1625 # Replace the "nowarnings" option with the appropriate additional_flags
1626 # to disable compiler warnings.
1627 set nowarnings [lsearch -exact $options nowarnings]
1628 if {$nowarnings != -1} {
1629 if [target_info exists gdb,nowarnings_flag] {
1630 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
1632 set flag "additional_flags=-w"
1634 set options [lreplace $options $nowarnings $nowarnings $flag]
1637 if { $type == "executable" } {
1638 if { ([istarget "*-*-mingw*"]
1639 || [istarget "*-*-cygwin*"])} {
1640 # Force output to unbuffered mode, by linking in an object file
1641 # with a global contructor that calls setvbuf.
1643 # Compile the special object seperatelly for two reasons:
1644 # 1) Insulate it from $options.
1645 # 2) Avoid compiling it for every gdb_compile invocation,
1646 # which is time consuming, especially if we're remote
1649 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
1650 verbose "compiling gdb_saved_set_unbuffered_obj"
1651 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
1652 set unbuf_obj ${objdir}/set_unbuffered_mode.o
1654 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
1655 if { $result != "" } {
1659 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
1660 # Link a copy of the output object, because the
1661 # original may be automatically deleted.
1662 remote_exec host "cp -f $unbuf_obj $gdb_saved_set_unbuffered_mode_obj"
1664 verbose "gdb_saved_set_unbuffered_obj already compiled"
1667 # Rely on the internal knowledge that the global ctors are ran in
1668 # reverse link order. In that case, we can use ldflags to
1669 # avoid copying the object file to the host multiple
1671 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
1675 set result [target_compile $source $dest $type $options];
1677 # Prune uninteresting compiler (and linker) output.
1678 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
1680 regsub "\[\r\n\]*$" "$result" "" result;
1681 regsub "^\[\r\n\]*" "$result" "" result;
1683 if { $result != "" && [lsearch $options quiet] == -1} {
1684 clone_output "gdb compile failed, $result"
1690 # This is just like gdb_compile, above, except that it tries compiling
1691 # against several different thread libraries, to see which one this
1693 proc gdb_compile_pthreads {source dest type options} {
1695 set why_msg "unrecognized error"
1696 foreach lib {-lpthreads -lpthread -lthread} {
1697 # This kind of wipes out whatever libs the caller may have
1698 # set. Or maybe theirs will override ours. How infelicitous.
1699 set options_with_lib [concat $options [list libs=$lib quiet]]
1700 set ccout [gdb_compile $source $dest $type $options_with_lib]
1701 switch -regexp -- $ccout {
1702 ".*no posix threads support.*" {
1703 set why_msg "missing threads include file"
1706 ".*cannot open -lpthread.*" {
1707 set why_msg "missing runtime threads library"
1709 ".*Can't find library for -lpthread.*" {
1710 set why_msg "missing runtime threads library"
1713 pass "successfully compiled posix threads test case"
1719 if {!$built_binfile} {
1720 unsupported "Couldn't compile $source: ${why_msg}"
1725 # Build a shared library from SOURCES. You must use get_compiler_info
1728 proc gdb_compile_shlib {sources dest options} {
1729 set obj_options $options
1731 switch -glob [test_compiler_info] {
1733 lappend obj_options "additional_flags=-qpic"
1736 if { !([istarget "powerpc*-*-aix*"]
1737 || [istarget "rs6000*-*-aix*"]
1738 || [istarget "*-*-cygwin*"]
1739 || [istarget "*-*-mingw*"]
1740 || [istarget "*-*-pe*"]) } {
1741 lappend obj_options "additional_flags=-fpic"
1745 switch -glob [istarget] {
1747 lappend obj_options "additional_flags=+z"
1750 # Disable SGI compiler's implicit -Dsgi
1751 lappend obj_options "additional_flags=-Usgi"
1754 # don't know what the compiler is...
1760 set outdir [file dirname $dest]
1762 foreach source $sources {
1763 set sourcebase [file tail $source]
1764 if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
1767 lappend objects ${outdir}/${sourcebase}.o
1770 if [istarget "hppa*-*-hpux*"] {
1771 remote_exec build "ld -b ${objects} -o ${dest}"
1773 set link_options $options
1774 if [test_compiler_info "xlc-*"] {
1775 lappend link_options "additional_flags=-qmkshrobj"
1777 lappend link_options "additional_flags=-shared"
1779 if { ([istarget "*-*-mingw*"]
1780 || [istarget *-*-cygwin*]
1781 || [istarget *-*-pe*])} {
1782 lappend link_options "additional_flags=-Wl,--out-implib,${dest}.a"
1785 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
1791 # This is just like gdb_compile_pthreads, above, except that we always add the
1792 # objc library for compiling Objective-C programs
1793 proc gdb_compile_objc {source dest type options} {
1795 set why_msg "unrecognized error"
1796 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
1797 # This kind of wipes out whatever libs the caller may have
1798 # set. Or maybe theirs will override ours. How infelicitous.
1799 if { $lib == "solaris" } {
1800 set lib "-lpthread -lposix4"
1802 if { $lib != "-lobjc" } {
1803 set lib "-lobjc $lib"
1805 set options_with_lib [concat $options [list libs=$lib quiet]]
1806 set ccout [gdb_compile $source $dest $type $options_with_lib]
1807 switch -regexp -- $ccout {
1808 ".*no posix threads support.*" {
1809 set why_msg "missing threads include file"
1812 ".*cannot open -lpthread.*" {
1813 set why_msg "missing runtime threads library"
1815 ".*Can't find library for -lpthread.*" {
1816 set why_msg "missing runtime threads library"
1819 pass "successfully compiled objc with posix threads test case"
1825 if {!$built_binfile} {
1826 unsupported "Couldn't compile $source: ${why_msg}"
1831 proc send_gdb { string } {
1832 global suppress_flag;
1833 if { $suppress_flag } {
1834 return "suppressed";
1836 return [remote_send host "$string"];
1842 proc gdb_expect { args } {
1843 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
1844 set atimeout [lindex $args 0];
1845 set expcode [list [lindex $args 1]];
1850 upvar timeout timeout;
1852 if [target_info exists gdb,timeout] {
1853 if [info exists timeout] {
1854 if { $timeout < [target_info gdb,timeout] } {
1855 set gtimeout [target_info gdb,timeout];
1857 set gtimeout $timeout;
1860 set gtimeout [target_info gdb,timeout];
1864 if ![info exists gtimeout] {
1866 if [info exists timeout] {
1867 set gtimeout $timeout;
1871 if [info exists atimeout] {
1872 if { ![info exists gtimeout] || $gtimeout < $atimeout } {
1873 set $gtimeout $atimeout;
1876 if ![info exists gtimeout] {
1882 global suppress_flag;
1883 global remote_suppress_flag;
1884 if [info exists remote_suppress_flag] {
1885 set old_val $remote_suppress_flag;
1887 if [info exists suppress_flag] {
1888 if { $suppress_flag } {
1889 set remote_suppress_flag 1;
1893 {uplevel remote_expect host $gtimeout $expcode} string];
1894 if [info exists old_val] {
1895 set remote_suppress_flag $old_val;
1897 if [info exists remote_suppress_flag] {
1898 unset remote_suppress_flag;
1903 global errorInfo errorCode;
1905 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1906 } elseif {$code == 2} {
1907 return -code return $string
1908 } elseif {$code == 3} {
1910 } elseif {$code > 4} {
1911 return -code $code $string
1915 # gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs
1917 # Check for long sequence of output by parts.
1918 # MESSAGE: is the test message to be printed with the test success/fail.
1919 # SENTINEL: Is the terminal pattern indicating that output has finished.
1920 # LIST: is the sequence of outputs to match.
1921 # If the sentinel is recognized early, it is considered an error.
1924 # 1 if the test failed,
1925 # 0 if the test passes,
1926 # -1 if there was an internal error.
1928 proc gdb_expect_list {test sentinel list} {
1930 global suppress_flag
1933 if { $suppress_flag } {
1935 unresolved "${test}"
1937 while { ${index} < [llength ${list}] } {
1938 set pattern [lindex ${list} ${index}]
1939 set index [expr ${index} + 1]
1940 if { ${index} == [llength ${list}] } {
1943 -re "${pattern}${sentinel}" {
1944 # pass "${test}, pattern ${index} + sentinel"
1947 fail "${test} (pattern ${index} + sentinel)"
1950 -re ".*A problem internal to GDB has been detected" {
1951 fail "${test} (GDB internal error)"
1953 gdb_internal_error_resync
1956 fail "${test} (pattern ${index} + sentinel) (timeout)"
1961 # unresolved "${test}, pattern ${index} + sentinel"
1967 # pass "${test}, pattern ${index}"
1970 fail "${test} (pattern ${index})"
1973 -re ".*A problem internal to GDB has been detected" {
1974 fail "${test} (GDB internal error)"
1976 gdb_internal_error_resync
1979 fail "${test} (pattern ${index}) (timeout)"
1984 # unresolved "${test}, pattern ${index}"
1998 proc gdb_suppress_entire_file { reason } {
1999 global suppress_flag;
2001 warning "$reason\n";
2002 set suppress_flag -1;
2006 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
2007 # gdb_expect to fail immediately (until the next call to
2008 # gdb_stop_suppressing_tests).
2010 proc gdb_suppress_tests { args } {
2011 global suppress_flag;
2013 return; # fnf - disable pending review of results where
2014 # testsuite ran better without this
2017 if { $suppress_flag == 1 } {
2018 if { [llength $args] > 0 } {
2019 warning "[lindex $args 0]\n";
2021 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
2027 # Clear suppress_flag.
2029 proc gdb_stop_suppressing_tests { } {
2030 global suppress_flag;
2032 if [info exists suppress_flag] {
2033 if { $suppress_flag > 0 } {
2034 set suppress_flag 0;
2035 clone_output "Tests restarted.\n";
2038 set suppress_flag 0;
2042 proc gdb_clear_suppressed { } {
2043 global suppress_flag;
2045 set suppress_flag 0;
2048 proc gdb_start { } {
2053 catch default_gdb_exit
2057 # gdb_load_cmd -- load a file into the debugger.
2058 # ARGS - additional args to load command.
2059 # return a -1 if anything goes wrong.
2061 proc gdb_load_cmd { args } {
2064 if [target_info exists gdb_load_timeout] {
2065 set loadtimeout [target_info gdb_load_timeout]
2067 set loadtimeout 1600
2069 send_gdb "load $args\n"
2070 verbose "Timeout is now $loadtimeout seconds" 2
2071 gdb_expect $loadtimeout {
2072 -re "Loading section\[^\r\]*\r\n" {
2075 -re "Start address\[\r\]*\r\n" {
2078 -re "Transfer rate\[\r\]*\r\n" {
2081 -re "Memory access error\[^\r\]*\r\n" {
2082 perror "Failed to load program"
2085 -re "$gdb_prompt $" {
2088 -re "(.*)\r\n$gdb_prompt " {
2089 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
2093 perror "Timed out trying to load $args."
2102 # Copy a file to the remote target and return its target filename.
2103 # Schedule the file to be deleted at the end of this test.
2105 proc gdb_download { filename } {
2108 set destname [remote_download target $filename]
2109 lappend cleanfiles $destname
2113 # gdb_load_shlibs LIB...
2115 # Copy the listed libraries to the target.
2117 proc gdb_load_shlibs { args } {
2118 if {![is_remote target]} {
2122 foreach file $args {
2126 # Even if the target supplies full paths for shared libraries,
2127 # they may not be paths for this system.
2128 gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "" ""
2132 # gdb_load -- load a file into the debugger.
2133 # Many files in config/*.exp override this procedure.
2135 proc gdb_load { arg } {
2136 return [gdb_file_cmd $arg]
2139 # gdb_reload -- load a file into the target. Called before "running",
2140 # either the first time or after already starting the program once,
2141 # for remote targets. Most files that override gdb_load should now
2142 # override this instead.
2144 proc gdb_reload { } {
2145 # For the benefit of existing configurations, default to gdb_load.
2146 # Specifying no file defaults to the executable currently being
2148 return [gdb_load ""]
2151 proc gdb_continue { function } {
2154 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
2157 proc default_gdb_init { args } {
2158 global gdb_wrapper_initialized
2163 gdb_clear_suppressed;
2165 # Make sure that the wrapper is rebuilt
2166 # with the appropriate multilib option.
2167 set gdb_wrapper_initialized 0
2169 # Unlike most tests, we have a small number of tests that generate
2170 # a very large amount of output. We therefore increase the expect
2171 # buffer size to be able to contain the entire test output.
2174 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
2175 if { [llength $args] > 0 } {
2178 set file [lindex $args 0];
2180 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
2183 if [target_info exists gdb_prompt] {
2184 set gdb_prompt [target_info gdb_prompt];
2186 set gdb_prompt "\\(gdb\\)"
2190 proc gdb_init { args } {
2191 return [eval default_gdb_init $args];
2194 proc gdb_finish { } {
2197 # Exit first, so that the files are no longer in use.
2200 if { [llength $cleanfiles] > 0 } {
2201 eval remote_file target delete $cleanfiles
2207 set debug_format "unknown"
2209 # Run the gdb command "info source" and extract the debugging format
2210 # information from the output and save it in debug_format.
2212 proc get_debug_format { } {
2218 set debug_format "unknown"
2219 send_gdb "info source\n"
2221 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
2222 set debug_format $expect_out(1,string)
2223 verbose "debug format is $debug_format"
2226 -re "No current source file.\r\n$gdb_prompt $" {
2227 perror "get_debug_format used when no current source file"
2230 -re "$gdb_prompt $" {
2231 warning "couldn't check debug format (no valid response)."
2235 warning "couldn't check debug format (timed out)."
2241 # Return true if FORMAT matches the debug format the current test was
2242 # compiled with. FORMAT is a shell-style globbing pattern; it can use
2243 # `*', `[...]', and so on.
2245 # This function depends on variables set by `get_debug_format', above.
2247 proc test_debug_format {format} {
2250 return [expr [string match $format $debug_format] != 0]
2253 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
2254 # COFF, stabs, etc). If that format matches the format that the
2255 # current test was compiled with, then the next test is expected to
2256 # fail for any target. Returns 1 if the next test or set of tests is
2257 # expected to fail, 0 otherwise (or if it is unknown). Must have
2258 # previously called get_debug_format.
2259 proc setup_xfail_format { format } {
2260 set ret [test_debug_format $format];
2268 proc gdb_step_for_stub { } {
2271 if ![target_info exists gdb,use_breakpoint_for_stub] {
2272 if [target_info exists gdb_stub_step_command] {
2273 set command [target_info gdb_stub_step_command];
2277 send_gdb "${command}\n";
2280 -re "(main.* at |.*in .*start).*$gdb_prompt" {
2283 -re ".*$gdb_prompt" {
2285 if { $tries == 5 } {
2286 fail "stepping out of breakpoint function";
2289 send_gdb "${command}\n";
2293 fail "stepping out of breakpoint function";
2300 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
2301 set file $expect_out(1,string);
2302 set linenum [expr $expect_out(2,string) + 1];
2303 set breakplace "${file}:${linenum}";
2307 send_gdb "break ${breakplace}\n";
2309 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
2310 set breakpoint $expect_out(1,string);
2312 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
2313 set breakpoint $expect_out(1,string);
2317 send_gdb "continue\n";
2319 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
2320 gdb_test "delete $breakpoint" ".*" "";
2327 # gdb_get_line_number TEXT [FILE]
2329 # Search the source file FILE, and return the line number of the
2330 # first line containing TEXT. If no match is found, return -1.
2332 # TEXT is a string literal, not a regular expression.
2334 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
2335 # specified, and does not start with "/", then it is assumed to be in
2336 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
2337 # by changing the callers and the interface at the same time.
2338 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
2339 # gdb.base/ena-dis-br.exp.
2341 # Use this function to keep your test scripts independent of the
2342 # exact line numbering of the source file. Don't write:
2344 # send_gdb "break 20"
2346 # This means that if anyone ever edits your test's source file,
2347 # your test could break. Instead, put a comment like this on the
2348 # source file line you want to break at:
2350 # /* breakpoint spot: frotz.exp: test name */
2352 # and then write, in your test script (which we assume is named
2355 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
2357 # (Yes, Tcl knows how to handle the nested quotes and brackets.
2360 # % puts "foo [lindex "bar baz" 1]"
2363 # Tcl is quite clever, for a little stringy language.)
2367 # The previous implementation of this procedure used the gdb search command.
2368 # This version is different:
2370 # . It works with MI, and it also works when gdb is not running.
2372 # . It operates on the build machine, not the host machine.
2374 # . For now, this implementation fakes a current directory of
2375 # $srcdir/$subdir to be compatible with the old implementation.
2376 # This will go away eventually and some callers will need to
2379 # . The TEXT argument is literal text and matches literally,
2380 # not a regular expression as it was before.
2382 # . State changes in gdb, such as changing the current file
2383 # and setting $_, no longer happen.
2385 # After a bit of time we can forget about the differences from the
2386 # old implementation.
2388 # --chastain 2004-08-05
2390 proc gdb_get_line_number { text { file "" } } {
2395 if { "$file" == "" } then {
2398 if { ! [regexp "^/" "$file"] } then {
2399 set file "$srcdir/$subdir/$file"
2402 if { [ catch { set fd [open "$file"] } message ] } then {
2408 for { set line 1 } { 1 } { incr line } {
2409 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
2413 if { $nchar < 0 } then {
2416 if { [string first "$text" "$body"] >= 0 } then {
2422 if { [ catch { close "$fd" } message ] } then {
2430 # gdb_continue_to_end:
2431 # The case where the target uses stubs has to be handled specially. If a
2432 # stub is used, we set a breakpoint at exit because we cannot rely on
2433 # exit() behavior of a remote target.
2435 # mssg is the error message that gets printed.
2437 proc gdb_continue_to_end {mssg} {
2438 if [target_info exists use_gdb_stub] {
2439 if {![gdb_breakpoint "exit"]} {
2442 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
2443 "continue until exit at $mssg"
2445 # Continue until we exit. Should not stop again.
2446 # Don't bother to check the output of the program, that may be
2447 # extremely tough for some remote systems.
2448 gdb_test "continue"\
2449 "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
2450 "continue until exit at $mssg"
2454 proc rerun_to_main {} {
2457 if [target_info exists use_gdb_stub] {
2460 -re ".*Breakpoint .*main .*$gdb_prompt $"\
2461 {pass "rerun to main" ; return 0}
2462 -re "$gdb_prompt $"\
2463 {fail "rerun to main" ; return 0}
2464 timeout {fail "(timeout) rerun to main" ; return 0}
2469 -re "The program .* has been started already.*y or n. $" {
2473 -re "Starting program.*$gdb_prompt $"\
2474 {pass "rerun to main" ; return 0}
2475 -re "$gdb_prompt $"\
2476 {fail "rerun to main" ; return 0}
2477 timeout {fail "(timeout) rerun to main" ; return 0}
2482 # Print a message and return true if a test should be skipped
2483 # due to lack of floating point suport.
2485 proc gdb_skip_float_test { msg } {
2486 if [target_info exists gdb,skip_float_tests] {
2487 verbose "Skipping test '$msg': no float tests.";
2493 # Print a message and return true if a test should be skipped
2494 # due to lack of stdio support.
2496 proc gdb_skip_stdio_test { msg } {
2497 if [target_info exists gdb,noinferiorio] {
2498 verbose "Skipping test '$msg': no inferior i/o.";
2504 proc gdb_skip_bogus_test { msg } {
2508 # Return true if a test should be skipped due to lack of XML support
2511 proc gdb_skip_xml_test { } {
2514 global xml_missing_cached
2516 if {[info exists xml_missing_cached]} {
2517 return $xml_missing_cached
2521 set xml_missing_cached 0
2522 gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" {
2523 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
2524 set xml_missing_cached 1
2526 -re ".*$gdb_prompt $" { }
2529 return $xml_missing_cached
2532 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
2533 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
2534 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
2535 # the name of a debuginfo only file. This file will be stored in the
2536 # gdb.base/.debug subdirectory.
2538 # Functions for separate debug info testing
2540 # starting with an executable:
2541 # foo --> original executable
2543 # at the end of the process we have:
2544 # foo.stripped --> foo w/o debug info
2545 # .debug/foo.debug --> foo's debug info
2546 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
2548 # Return the name of the file in which we should stor EXEC's separated
2549 # debug info. EXEC contains the full path.
2550 proc separate_debug_filename { exec } {
2552 # In a .debug subdirectory off the same directory where the testcase
2553 # executable is going to be. Something like:
2554 # <your-path>/gdb/testsuite/gdb.base/.debug/blah.debug.
2555 # This is the default location where gdb expects to findi
2556 # the debug info file.
2558 set exec_dir [file dirname $exec]
2559 set exec_file [file tail $exec]
2560 set debug_dir [file join $exec_dir ".debug"]
2561 set debug_file [file join $debug_dir "${exec_file}.debug"]
2566 # Return the build-id hex string (usually 160 bits as 40 hex characters)
2567 # converted to the form: .build-id/ab/cdef1234...89.debug
2568 # Return "" if no build-id found.
2569 proc build_id_debug_filename_get { exec } {
2570 set tmp "${exec}-tmp"
2571 set objcopy_program [transform objcopy]
2573 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $exec $tmp" output]
2574 verbose "result is $result"
2575 verbose "output is $output"
2580 fconfigure $fi -translation binary
2581 # Skip the NOTE header.
2586 if ![string compare $data ""] then {
2589 # Convert it to hex.
2590 binary scan $data H* data
2591 set data [regsub {^..} $data {\0/}]
2592 return ".build-id/${data}.debug";
2595 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
2596 # list of optional flags. The only currently supported flag is no-main,
2597 # which removes the symbol entry for main from the separate debug file.
2599 proc gdb_gnu_strip_debug { dest args } {
2601 set debug_file [separate_debug_filename $dest]
2602 set strip_to_file_program [transform strip]
2603 set objcopy_program [transform objcopy]
2605 # Make sure the directory that will hold the separated debug
2606 # info actually exists.
2607 set debug_dir [file dirname $debug_file]
2608 if {! [file isdirectory $debug_dir]} {
2609 file mkdir $debug_dir
2612 set debug_link [file tail $debug_file]
2613 set stripped_file "${dest}.stripped"
2615 # Get rid of the debug info, and store result in stripped_file
2616 # something like gdb/testsuite/gdb.base/blah.stripped.
2617 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
2618 verbose "result is $result"
2619 verbose "output is $output"
2624 # Get rid of everything but the debug info, and store result in debug_file
2625 # This will be in the .debug subdirectory, see above.
2626 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
2627 verbose "result is $result"
2628 verbose "output is $output"
2633 # If no-main is passed, strip the symbol for main from the separate
2634 # file. This is to simulate the behavior of elfutils's eu-strip, which
2635 # leaves the symtab in the original file only. There's no way to get
2636 # objcopy or strip to remove the symbol table without also removing the
2637 # debugging sections, so this is as close as we can get.
2638 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
2639 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
2640 verbose "result is $result"
2641 verbose "output is $output"
2645 file delete "${debug_file}"
2646 file rename "${debug_file}-tmp" "${debug_file}"
2649 # Link the two previous output files together, adding the .gnu_debuglink
2650 # section to the stripped_file, containing a pointer to the debug_file,
2651 # save the new file in dest.
2652 # This will be the regular executable filename, in the usual location.
2653 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
2654 verbose "result is $result"
2655 verbose "output is $output"
2663 # Test the output of GDB_COMMAND matches the pattern obtained
2664 # by concatenating all elements of EXPECTED_LINES. This makes
2665 # it possible to split otherwise very long string into pieces.
2666 # If third argument is not empty, it's used as the name of the
2667 # test to be printed on pass/fail.
2668 proc help_test_raw { gdb_command expected_lines args } {
2669 set message $gdb_command
2670 if [llength $args]>0 then {
2671 set message [lindex $args 0]
2673 set expected_output [join $expected_lines ""]
2674 gdb_test "${gdb_command}" "${expected_output}" $message
2677 # Test the output of "help COMMNAD_CLASS". EXPECTED_INITIAL_LINES
2678 # are regular expressions that should match the beginning of output,
2679 # before the list of commands in that class. The presence of
2680 # command list and standard epilogue will be tested automatically.
2681 proc test_class_help { command_class expected_initial_lines args } {
2683 "List of commands\:.*\[\r\n\]+"
2684 "Type \"help\" followed by command name for full documentation\.\[\r\n\]+"
2685 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+"
2686 "Command name abbreviations are allowed if unambiguous\."
2688 set l_entire_body [concat $expected_initial_lines $l_stock_body]
2690 eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
2693 # COMMAND_LIST should have either one element -- command to test, or
2694 # two elements -- abbreviated command to test, and full command the first
2695 # element is abbreviation of.
2696 # The command must be a prefix command. EXPECTED_INITIAL_LINES
2697 # are regular expressions that should match the beginning of output,
2698 # before the list of subcommands. The presence of
2699 # subcommand list and standard epilogue will be tested automatically.
2700 proc test_prefix_command_help { command_list expected_initial_lines args } {
2701 set command [lindex $command_list 0]
2702 if {[llength $command_list]>1} {
2703 set full_command [lindex $command_list 1]
2705 set full_command $command
2707 # Use 'list' and not just {} because we want variables to
2708 # be expanded in this list.
2709 set l_stock_body [list\
2710 "List of $full_command subcommands\:.*\[\r\n\]+"\
2711 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
2712 "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
2713 "Command name abbreviations are allowed if unambiguous\."]
2714 set l_entire_body [concat $expected_initial_lines $l_stock_body]
2715 if {[llength $args]>0} {
2716 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
2718 help_test_raw "help ${command}" $l_entire_body
2722 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
2723 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
2724 # to pass to untested, if something is wrong. OPTIONS are passed
2725 # to gdb_compile directly.
2726 proc build_executable { testname executable {sources ""} {options {debug}} } {
2731 if {[llength $sources]==0} {
2732 set sources ${executable}.c
2735 set binfile ${objdir}/${subdir}/${executable}
2738 for {set i 0} "\$i<[llength $sources]" {incr i} {
2739 set s [lindex $sources $i]
2740 if { [gdb_compile "${srcdir}/${subdir}/${s}" "${binfile}${i}.o" object $options] != "" } {
2744 lappend objects "${binfile}${i}.o"
2747 if { [gdb_compile $objects "${binfile}" executable $options] != "" } {
2752 if [get_compiler_info ${binfile}] {
2758 # Starts fresh GDB binary and loads EXECUTABLE into GDB. EXECUTABLE is
2759 # the name of binary in ${objdir}/${subdir}.
2760 proc clean_restart { executable } {
2764 set binfile ${objdir}/${subdir}/${executable}
2768 gdb_reinitialize_dir $srcdir/$subdir
2771 if [target_info exists gdb_stub] {
2776 # Prepares for testing, by calling build_executable, and then clean_restart.
2777 # Please refer to build_executable for parameter description.
2778 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
2780 if {[build_executable $testname $executable $sources $options] == -1} {
2783 clean_restart $executable