1 # Copyright 1992-2017 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # This file was written by Fred Fish. (fnf@cygnus.com)
18 # Generic gdb subroutines that should work for any target. If these
19 # need to be modified for any target, it can be done with a variable
20 # or by passing arguments.
23 # Tests would fail, logs on get_compiler_info() would be missing.
24 send_error "`site.exp' not found, run `make site.exp'!\n"
30 load_lib gdb-utils.exp
35 # The spawn ID used for I/O interaction with the inferior. For native
36 # targets, or remote targets that can do I/O through GDB
37 # (semi-hosting) this will be the same as the host/GDB's spawn ID.
38 # Otherwise, the board may set this to some other spawn ID. E.g.,
39 # when debugging with GDBserver, this is set to GDBserver's spawn ID,
40 # so input/output is done on gdbserver's tty.
41 global inferior_spawn_id
43 if [info exists TOOL_EXECUTABLE] {
44 set GDB $TOOL_EXECUTABLE
46 if ![info exists GDB] {
47 if ![is_remote host] {
48 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
50 set GDB [transform gdb]
53 verbose "using GDB = $GDB" 2
55 # GDBFLAGS is available for the user to set on the command line.
56 # E.g. make check RUNTESTFLAGS=GDBFLAGS=mumble
57 # Testcases may use it to add additional flags, but they must:
58 # - append new flags, not overwrite
59 # - restore the original value when done
61 if ![info exists GDBFLAGS] {
64 verbose "using GDBFLAGS = $GDBFLAGS" 2
66 # Make the build data directory available to tests.
67 set BUILD_DATA_DIRECTORY "[pwd]/../data-directory"
69 # INTERNAL_GDBFLAGS contains flags that the testsuite requires.
70 global INTERNAL_GDBFLAGS
71 if ![info exists INTERNAL_GDBFLAGS] {
72 set INTERNAL_GDBFLAGS "-nw -nx -data-directory $BUILD_DATA_DIRECTORY"
75 # The variable gdb_prompt is a regexp which matches the gdb prompt.
76 # Set it if it is not already set. This is also set by default_gdb_init
77 # but it's not clear what removing one of them will break.
78 # See with_gdb_prompt for more details on prompt handling.
80 if ![info exists gdb_prompt] then {
81 set gdb_prompt "\\(gdb\\)"
84 # A regexp that matches the pagination prompt.
85 set pagination_prompt [string_to_regexp "---Type <return> to continue, or q <return> to quit---"]
87 # The variable fullname_syntax_POSIX is a regexp which matches a POSIX
88 # absolute path ie. /foo/
89 set fullname_syntax_POSIX {/[^\n]*/}
90 # The variable fullname_syntax_UNC is a regexp which matches a Windows
91 # UNC path ie. \\D\foo\
92 set fullname_syntax_UNC {\\\\[^\\]+\\[^\n]+\\}
93 # The variable fullname_syntax_DOS_CASE is a regexp which matches a
94 # particular DOS case that GDB most likely will output
95 # ie. \foo\, but don't match \\.*\
96 set fullname_syntax_DOS_CASE {\\[^\\][^\n]*\\}
97 # The variable fullname_syntax_DOS is a regexp which matches a DOS path
98 # ie. a:\foo\ && a:foo\
99 set fullname_syntax_DOS {[a-zA-Z]:[^\n]*\\}
100 # The variable fullname_syntax is a regexp which matches what GDB considers
101 # an absolute path. It is currently debatable if the Windows style paths
102 # d:foo and \abc should be considered valid as an absolute path.
103 # Also, the purpse of this regexp is not to recognize a well formed
104 # absolute path, but to say with certainty that a path is absolute.
105 set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
107 # Needed for some tests under Cygwin.
111 if ![info exists env(EXEEXT)] {
114 set EXEEXT $env(EXEEXT)
119 set inferior_exited_re "(\\\[Inferior \[0-9\]+ \\(.*\\) exited)"
121 # A regular expression that matches a value history number.
123 set valnum_re "\\\$$decimal"
125 ### Only procedures should come after this point.
128 # gdb_version -- extract and print the version number of GDB
130 proc default_gdb_version {} {
132 global INTERNAL_GDBFLAGS GDBFLAGS
136 if {[info exists inotify_pid]} {
137 eval exec kill $inotify_pid
140 set output [remote_exec host "$GDB $INTERNAL_GDBFLAGS --version"]
141 set tmp [lindex $output 1]
143 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
144 if ![is_remote host] {
145 clone_output "[which $GDB] version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
147 clone_output "$GDB on remote host version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
151 proc gdb_version { } {
152 return [default_gdb_version]
156 # gdb_unload -- unload a file if one is loaded
157 # Return 0 on success, -1 on error.
166 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
167 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
168 -re "A program is being debugged already.*Are you sure you want to change the file.*y or n. $" {
172 -re "Discard symbol table from .*y or n.*$" {
176 -re "$gdb_prompt $" {}
178 perror "couldn't unload file in $GDB (timeout)."
185 # Many of the tests depend on setting breakpoints at various places and
186 # running until that breakpoint is reached. At times, we want to start
187 # with a clean-slate with respect to breakpoints, so this utility proc
188 # lets us do this without duplicating this code everywhere.
191 proc delete_breakpoints {} {
194 # we need a larger timeout value here or this thing just confuses
195 # itself. May need a better implementation if possible. - guo
199 set msg "delete all breakpoints in delete_breakpoints"
201 gdb_test_multiple "delete breakpoints" "$msg" {
202 -re "Delete all breakpoints.*y or n.*$" {
206 -re "$gdb_prompt $" {
212 # Confirm with "info breakpoints".
214 set msg "info breakpoints"
215 gdb_test_multiple $msg $msg {
216 -re "No breakpoints or watchpoints..*$gdb_prompt $" {
219 -re "$gdb_prompt $" {
225 perror "breakpoints not deleted"
229 # Generic run command.
231 # The second pattern below matches up to the first newline *only*.
232 # Using ``.*$'' could swallow up output that we attempt to match
235 # N.B. This function does not wait for gdb to return to the prompt,
236 # that is the caller's responsibility.
238 proc gdb_run_cmd {args} {
239 global gdb_prompt use_gdb_stub
241 foreach command [gdb_init_commands] {
242 send_gdb "$command\n"
244 -re "$gdb_prompt $" { }
246 perror "gdb_init_command for target failed"
253 if [target_info exists gdb,do_reload_on_run] {
254 if { [gdb_reload] != 0 } {
257 send_gdb "continue\n"
259 -re "Continu\[^\r\n\]*\[\r\n\]" {}
265 if [target_info exists gdb,start_symbol] {
266 set start [target_info gdb,start_symbol]
270 send_gdb "jump *$start\n"
272 while { $start_attempt } {
273 # Cap (re)start attempts at three to ensure that this loop
274 # always eventually fails. Don't worry about trying to be
275 # clever and not send a command when it has failed.
276 if [expr $start_attempt > 3] {
277 perror "Jump to start() failed (retry count exceeded)"
280 set start_attempt [expr $start_attempt + 1]
282 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
285 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
286 perror "Can't find start symbol to run in gdb_run"
289 -re "No symbol \"start\" in current.*$gdb_prompt $" {
290 send_gdb "jump *_start\n"
292 -re "No symbol.*context.*$gdb_prompt $" {
295 -re "Line.* Jump anyway.*y or n. $" {
298 -re "The program is not being run.*$gdb_prompt $" {
299 if { [gdb_reload] != 0 } {
302 send_gdb "jump *$start\n"
305 perror "Jump to start() failed (timeout)"
313 if [target_info exists gdb,do_reload_on_run] {
314 if { [gdb_reload] != 0 } {
318 send_gdb "run $args\n"
319 # This doesn't work quite right yet.
320 # Use -notransfer here so that test cases (like chng-sym.exp)
321 # may test for additional start-up messages.
323 -re "The program .* has been started already.*y or n. $" {
327 -notransfer -re "Starting program: \[^\r\n\]*" {}
328 -notransfer -re "$gdb_prompt $" {
329 # There is no more input expected.
334 # Generic start command. Return 0 if we could start the program, -1
337 # N.B. This function does not wait for gdb to return to the prompt,
338 # that is the caller's responsibility.
340 proc gdb_start_cmd {args} {
341 global gdb_prompt use_gdb_stub
343 foreach command [gdb_init_commands] {
344 send_gdb "$command\n"
346 -re "$gdb_prompt $" { }
348 perror "gdb_init_command for target failed"
358 send_gdb "start $args\n"
359 # Use -notransfer here so that test cases (like chng-sym.exp)
360 # may test for additional start-up messages.
362 -re "The program .* has been started already.*y or n. $" {
366 -notransfer -re "Starting program: \[^\r\n\]*" {
373 # Set a breakpoint at FUNCTION. If there is an additional argument it is
374 # a list of options; the supported options are allow-pending, temporary,
375 # message, no-message, and passfail.
376 # The result is 1 for success, 0 for failure.
378 # Note: The handling of message vs no-message is messed up, but it's based
379 # on historical usage. By default this function does not print passes,
381 # no-message: turns off printing of fails (and passes, but they're already off)
382 # message: turns on printing of passes (and fails, but they're already on)
384 proc gdb_breakpoint { function args } {
388 set pending_response n
389 if {[lsearch -exact $args allow-pending] != -1} {
390 set pending_response y
393 set break_command "break"
394 set break_message "Breakpoint"
395 if {[lsearch -exact $args temporary] != -1} {
396 set break_command "tbreak"
397 set break_message "Temporary breakpoint"
402 set no_message_loc [lsearch -exact $args no-message]
403 set message_loc [lsearch -exact $args message]
404 # The last one to appear in args wins.
405 if { $no_message_loc > $message_loc } {
407 } elseif { $message_loc > $no_message_loc } {
411 set test_name "setting breakpoint at $function"
413 send_gdb "$break_command $function\n"
414 # The first two regexps are what we get with -g, the third is without -g.
416 -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
417 -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
418 -re "$break_message \[0-9\]* at .*$gdb_prompt $" {}
419 -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
420 if {$pending_response == "n"} {
427 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
428 send_gdb "$pending_response\n"
431 -re "A problem internal to GDB has been detected" {
433 fail "$test_name (GDB internal error)"
435 gdb_internal_error_resync
438 -re "$gdb_prompt $" {
446 fail "$test_name (eof)"
452 fail "$test_name (timeout)"
463 # Set breakpoint at function and run gdb until it breaks there.
464 # Since this is the only breakpoint that will be set, if it stops
465 # at a breakpoint, we will assume it is the one we want. We can't
466 # just compare to "function" because it might be a fully qualified,
467 # single quoted C++ function specifier.
469 # If there are additional arguments, pass them to gdb_breakpoint.
470 # We recognize no-message/message ourselves.
471 # The default is no-message.
472 # no-message is messed up here, like gdb_breakpoint: to preserve
473 # historical usage fails are always printed by default.
474 # no-message: turns off printing of fails (and passes, but they're already off)
475 # message: turns on printing of passes (and fails, but they're already on)
477 proc runto { function args } {
483 # Default to "no-message".
484 set args "no-message $args"
488 set no_message_loc [lsearch -exact $args no-message]
489 set message_loc [lsearch -exact $args message]
490 # The last one to appear in args wins.
491 if { $no_message_loc > $message_loc } {
493 } elseif { $message_loc > $no_message_loc } {
497 set test_name "running to $function in runto"
499 # We need to use eval here to pass our varargs args to gdb_breakpoint
500 # which is also a varargs function.
501 # But we also have to be careful because $function may have multiple
502 # elements, and we don't want Tcl to move the remaining elements after
503 # the first to $args. That is why $function is wrapped in {}.
504 if ![eval gdb_breakpoint {$function} $args] {
510 # the "at foo.c:36" output we get with -g.
511 # the "in func" output we get without -g.
513 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
519 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
525 -re "The target does not support running in non-stop mode.\r\n$gdb_prompt $" {
527 unsupported "non-stop mode not supported"
531 -re ".*A problem internal to GDB has been detected" {
533 fail "$test_name (GDB internal error)"
535 gdb_internal_error_resync
538 -re "$gdb_prompt $" {
546 fail "$test_name (eof)"
552 fail "$test_name (timeout)"
563 # Ask gdb to run until we hit a breakpoint at main.
565 # N.B. This function deletes all existing breakpoints.
566 # If you don't want that, use gdb_start_cmd.
568 proc runto_main { } {
569 return [runto main no-message]
572 ### Continue, and expect to hit a breakpoint.
573 ### Report a pass or fail, depending on whether it seems to have
574 ### worked. Use NAME as part of the test name; each call to
575 ### continue_to_breakpoint should use a NAME which is unique within
577 proc gdb_continue_to_breakpoint {name {location_pattern .*}} {
579 set full_name "continue to breakpoint: $name"
581 gdb_test_multiple "continue" $full_name {
582 -re "(?:Breakpoint|Temporary breakpoint) .* (at|in) $location_pattern\r\n$gdb_prompt $" {
589 # gdb_internal_error_resync:
591 # Answer the questions GDB asks after it reports an internal error
592 # until we get back to a GDB prompt. Decline to quit the debugging
593 # session, and decline to create a core file. Return non-zero if the
596 # This procedure just answers whatever questions come up until it sees
597 # a GDB prompt; it doesn't require you to have matched the input up to
598 # any specific point. However, it only answers questions it sees in
599 # the output itself, so if you've matched a question, you had better
600 # answer it yourself before calling this.
602 # You can use this function thus:
606 # -re ".*A problem internal to GDB has been detected" {
607 # gdb_internal_error_resync
612 proc gdb_internal_error_resync {} {
615 verbose -log "Resyncing due to internal error."
618 while {$count < 10} {
620 -re "Quit this debugging session\\? \\(y or n\\) $" {
624 -re "Create a core file of GDB\\? \\(y or n\\) $" {
628 -re "$gdb_prompt $" {
629 # We're resynchronized.
633 perror "Could not resync from internal error (timeout)"
638 perror "Could not resync from internal error (resync count exceeded)"
643 # gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
644 # Send a command to gdb; test the result.
646 # COMMAND is the command to execute, send to GDB with send_gdb. If
647 # this is the null string no command is sent.
648 # MESSAGE is a message to be printed with the built-in failure patterns
649 # if one of them matches. If MESSAGE is empty COMMAND will be used.
650 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
651 # patterns. Pattern elements will be evaluated in the caller's
652 # context; action elements will be executed in the caller's context.
653 # Unlike patterns for gdb_test, these patterns should generally include
654 # the final newline and prompt.
657 # 1 if the test failed, according to a built-in failure pattern
658 # 0 if only user-supplied patterns matched
659 # -1 if there was an internal error.
661 # You can use this function thus:
663 # gdb_test_multiple "print foo" "test foo" {
664 # -re "expected output 1" {
667 # -re "expected output 2" {
672 # Like with "expect", you can also specify the spawn id to match with
673 # -i "$id". Interesting spawn ids are $inferior_spawn_id and
674 # $gdb_spawn_id. The former matches inferior I/O, while the latter
675 # matches GDB I/O. E.g.:
677 # send_inferior "hello\n"
678 # gdb_test_multiple "continue" "test echo" {
679 # -i "$inferior_spawn_id" -re "^hello\r\nhello\r\n$" {
682 # -i "$gdb_spawn_id" -re "Breakpoint.*$gdb_prompt $" {
683 # fail "hit breakpoint"
687 # The standard patterns, such as "Inferior exited..." and "A problem
688 # ...", all being implicitly appended to that list. These are always
689 # expected from $gdb_spawn_id. IOW, callers do not need to worry
690 # about resetting "-i" back to $gdb_spawn_id explicitly.
692 proc gdb_test_multiple { command message user_code } {
693 global verbose use_gdb_stub
694 global gdb_prompt pagination_prompt
697 global inferior_exited_re
698 upvar timeout timeout
699 upvar expect_out expect_out
702 if { $message == "" } {
706 if [string match "*\[\r\n\]" $command] {
707 error "Invalid trailing newline in \"$message\" test"
710 if [string match "*\[\r\n\]*" $message] {
711 error "Invalid newline in \"$message\" test"
715 && [regexp -nocase {^\s*(r|run|star|start|at|att|atta|attac|attach)\M} \
717 error "gdbserver does not support $command without extended-remote"
720 # TCL/EXPECT WART ALERT
721 # Expect does something very strange when it receives a single braced
722 # argument. It splits it along word separators and performs substitutions.
723 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
724 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
725 # double-quoted list item, "\[ab\]" is just a long way of representing
726 # "[ab]", because the backslashes will be removed by lindex.
728 # Unfortunately, there appears to be no easy way to duplicate the splitting
729 # that expect will do from within TCL. And many places make use of the
730 # "\[0-9\]" construct, so we need to support that; and some places make use
731 # of the "[func]" construct, so we need to support that too. In order to
732 # get this right we have to substitute quoted list elements differently
733 # from braced list elements.
735 # We do this roughly the same way that Expect does it. We have to use two
736 # lists, because if we leave unquoted newlines in the argument to uplevel
737 # they'll be treated as command separators, and if we escape newlines
738 # we mangle newlines inside of command blocks. This assumes that the
739 # input doesn't contain a pattern which contains actual embedded newlines
742 regsub -all {\n} ${user_code} { } subst_code
743 set subst_code [uplevel list $subst_code]
745 set processed_code ""
747 set expecting_action 0
749 foreach item $user_code subst_item $subst_code {
750 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
751 lappend processed_code $item
754 if { $item == "-indices" || $item == "-re" || $item == "-ex" } {
755 lappend processed_code $item
758 if { $item == "-timeout" || $item == "-i" } {
760 lappend processed_code $item
763 if { $expecting_arg } {
765 lappend processed_code $subst_item
768 if { $expecting_action } {
769 lappend processed_code "uplevel [list $item]"
770 set expecting_action 0
771 # Cosmetic, no effect on the list.
772 append processed_code "\n"
775 set expecting_action 1
776 lappend processed_code $subst_item
777 if {$patterns != ""} {
780 append patterns "\"$subst_item\""
783 # Also purely cosmetic.
784 regsub -all {\r} $patterns {\\r} patterns
785 regsub -all {\n} $patterns {\\n} patterns
788 send_user "Sending \"$command\" to gdb\n"
789 send_user "Looking to match \"$patterns\"\n"
790 send_user "Message is \"$message\"\n"
794 set string "${command}\n"
795 if { $command != "" } {
796 set multi_line_re "\[\r\n\] *>"
797 while { "$string" != "" } {
798 set foo [string first "\n" "$string"]
799 set len [string length "$string"]
800 if { $foo < [expr $len - 1] } {
801 set str [string range "$string" 0 $foo]
802 if { [send_gdb "$str"] != "" } {
805 if { ! $suppress_flag } {
806 perror "Couldn't send $command to GDB."
811 # since we're checking if each line of the multi-line
812 # command are 'accepted' by GDB here,
813 # we need to set -notransfer expect option so that
814 # command output is not lost for pattern matching
817 -notransfer -re "$multi_line_re$" { verbose "partial: match" 3 }
818 timeout { verbose "partial: timeout" 3 }
820 set string [string range "$string" [expr $foo + 1] end]
821 set multi_line_re "$multi_line_re.*\[\r\n\] *>"
826 if { "$string" != "" } {
827 if { [send_gdb "$string"] != "" } {
830 if { ! $suppress_flag } {
831 perror "Couldn't send $command to GDB."
840 -re ".*A problem internal to GDB has been detected" {
841 fail "$message (GDB internal error)"
842 gdb_internal_error_resync
845 -re "\\*\\*\\* DOSEXIT code.*" {
846 if { $message != "" } {
849 gdb_suppress_entire_file "GDB died"
853 append code $processed_code
855 # Reset the spawn id, in case the processed code used -i.
858 -re "Ending remote debugging.*$gdb_prompt $" {
859 if ![isnative] then {
860 warning "Can`t communicate to remote target."
866 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
867 perror "Undefined command \"$command\"."
871 -re "Ambiguous command.*$gdb_prompt $" {
872 perror "\"$command\" is not a unique command name."
876 -re "$inferior_exited_re with code \[0-9\]+.*$gdb_prompt $" {
877 if ![string match "" $message] then {
878 set errmsg "$message (the program exited)"
880 set errmsg "$command (the program exited)"
885 -re "$inferior_exited_re normally.*$gdb_prompt $" {
886 if ![string match "" $message] then {
887 set errmsg "$message (the program exited)"
889 set errmsg "$command (the program exited)"
894 -re "The program is not being run.*$gdb_prompt $" {
895 if ![string match "" $message] then {
896 set errmsg "$message (the program is no longer running)"
898 set errmsg "$command (the program is no longer running)"
903 -re "\r\n$gdb_prompt $" {
904 if ![string match "" $message] then {
909 -re "$pagination_prompt" {
911 perror "Window too small."
915 -re "\\((y or n|y or \\\[n\\\]|\\\[y\\\] or n)\\) " {
917 gdb_expect -re "$gdb_prompt $"
918 fail "$message (got interactive prompt)"
921 -re "\\\[0\\\] cancel\r\n\\\[1\\\] all.*\r\n> $" {
923 gdb_expect -re "$gdb_prompt $"
924 fail "$message (got breakpoint menu)"
928 # Patterns below apply to any spawn id specified.
931 perror "Process no longer exists"
932 if { $message != "" } {
938 perror "internal buffer is full."
943 if ![string match "" $message] then {
944 fail "$message (timeout)"
951 set code [catch {gdb_expect $code} string]
953 global errorInfo errorCode
954 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
955 } elseif {$code > 1} {
956 return -code $code $string
961 # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
962 # Send a command to gdb; test the result.
964 # COMMAND is the command to execute, send to GDB with send_gdb. If
965 # this is the null string no command is sent.
966 # PATTERN is the pattern to match for a PASS, and must NOT include
967 # the \r\n sequence immediately before the gdb prompt. This argument
968 # may be omitted to just match the prompt, ignoring whatever output
970 # MESSAGE is an optional message to be printed. If this is
971 # omitted, then the pass/fail messages use the command string as the
972 # message. (If this is the empty string, then sometimes we don't
973 # call pass or fail at all; I don't understand this at all.)
974 # QUESTION is a question GDB may ask in response to COMMAND, like
976 # RESPONSE is the response to send if QUESTION appears.
979 # 1 if the test failed,
980 # 0 if the test passes,
981 # -1 if there was an internal error.
983 proc gdb_test { args } {
985 upvar timeout timeout
987 if [llength $args]>2 then {
988 set message [lindex $args 2]
990 set message [lindex $args 0]
992 set command [lindex $args 0]
993 set pattern [lindex $args 1]
995 if [llength $args]==5 {
996 set question_string [lindex $args 3]
997 set response_string [lindex $args 4]
999 set question_string "^FOOBAR$"
1002 return [gdb_test_multiple $command $message {
1003 -re "\[\r\n\]*(?:$pattern)\[\r\n\]+$gdb_prompt $" {
1004 if ![string match "" $message] then {
1008 -re "(${question_string})$" {
1009 send_gdb "$response_string\n"
1015 # gdb_test_no_output COMMAND MESSAGE
1016 # Send a command to GDB and verify that this command generated no output.
1018 # See gdb_test_multiple for a description of the COMMAND and MESSAGE
1019 # parameters. If MESSAGE is ommitted, then COMMAND will be used as
1020 # the message. (If MESSAGE is the empty string, then sometimes we do not
1021 # call pass or fail at all; I don't understand this at all.)
1023 proc gdb_test_no_output { args } {
1025 set command [lindex $args 0]
1026 if [llength $args]>1 then {
1027 set message [lindex $args 1]
1029 set message $command
1032 set command_regex [string_to_regexp $command]
1033 gdb_test_multiple $command $message {
1034 -re "^$command_regex\r\n$gdb_prompt $" {
1035 if ![string match "" $message] then {
1042 # Send a command and then wait for a sequence of outputs.
1043 # This is useful when the sequence is long and contains ".*", a single
1044 # regexp to match the entire output can get a timeout much easier.
1046 # COMMAND is the command to send.
1047 # TEST_NAME is passed to pass/fail. COMMAND is used if TEST_NAME is "".
1048 # EXPECTED_OUTPUT_LIST is a list of regexps of expected output, which are
1049 # processed in order, and all must be present in the output.
1051 # It is unnecessary to specify ".*" at the beginning or end of any regexp,
1052 # there is an implicit ".*" between each element of EXPECTED_OUTPUT_LIST.
1053 # There is also an implicit ".*" between the last regexp and the gdb prompt.
1055 # Like gdb_test and gdb_test_multiple, the output is expected to end with the
1056 # gdb prompt, which must not be specified in EXPECTED_OUTPUT_LIST.
1059 # 1 if the test failed,
1060 # 0 if the test passes,
1061 # -1 if there was an internal error.
1063 proc gdb_test_sequence { command test_name expected_output_list } {
1065 if { $test_name == "" } {
1066 set test_name $command
1068 lappend expected_output_list ""; # implicit ".*" before gdb prompt
1069 send_gdb "$command\n"
1070 return [gdb_expect_list $test_name "$gdb_prompt $" $expected_output_list]
1074 # Test that a command gives an error. For pass or fail, return
1075 # a 1 to indicate that more tests can proceed. However a timeout
1076 # is a serious error, generates a special fail message, and causes
1077 # a 0 to be returned to indicate that more tests are likely to fail
1080 proc test_print_reject { args } {
1084 if [llength $args]==2 then {
1085 set expectthis [lindex $args 1]
1087 set expectthis "should never match this bogus string"
1089 set sendthis [lindex $args 0]
1090 if $verbose>2 then {
1091 send_user "Sending \"$sendthis\" to gdb\n"
1092 send_user "Looking to match \"$expectthis\"\n"
1094 send_gdb "$sendthis\n"
1095 #FIXME: Should add timeout as parameter.
1097 -re "A .* in expression.*\\.*$gdb_prompt $" {
1098 pass "reject $sendthis"
1101 -re "Invalid syntax in expression.*$gdb_prompt $" {
1102 pass "reject $sendthis"
1105 -re "Junk after end of expression.*$gdb_prompt $" {
1106 pass "reject $sendthis"
1109 -re "Invalid number.*$gdb_prompt $" {
1110 pass "reject $sendthis"
1113 -re "Invalid character constant.*$gdb_prompt $" {
1114 pass "reject $sendthis"
1117 -re "No symbol table is loaded.*$gdb_prompt $" {
1118 pass "reject $sendthis"
1121 -re "No symbol .* in current context.*$gdb_prompt $" {
1122 pass "reject $sendthis"
1125 -re "Unmatched single quote.*$gdb_prompt $" {
1126 pass "reject $sendthis"
1129 -re "A character constant must contain at least one character.*$gdb_prompt $" {
1130 pass "reject $sendthis"
1133 -re "$expectthis.*$gdb_prompt $" {
1134 pass "reject $sendthis"
1137 -re ".*$gdb_prompt $" {
1138 fail "reject $sendthis"
1142 fail "reject $sendthis (eof or timeout)"
1149 # Same as gdb_test, but the second parameter is not a regexp,
1150 # but a string that must match exactly.
1152 proc gdb_test_exact { args } {
1153 upvar timeout timeout
1155 set command [lindex $args 0]
1157 # This applies a special meaning to a null string pattern. Without
1158 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
1159 # messages from commands that should have no output except a new
1160 # prompt. With this, only results of a null string will match a null
1163 set pattern [lindex $args 1]
1164 if [string match $pattern ""] {
1165 set pattern [string_to_regexp [lindex $args 0]]
1167 set pattern [string_to_regexp [lindex $args 1]]
1170 # It is most natural to write the pattern argument with only
1171 # embedded \n's, especially if you are trying to avoid Tcl quoting
1172 # problems. But gdb_expect really wants to see \r\n in patterns. So
1173 # transform the pattern here. First transform \r\n back to \n, in
1174 # case some users of gdb_test_exact already do the right thing.
1175 regsub -all "\r\n" $pattern "\n" pattern
1176 regsub -all "\n" $pattern "\r\n" pattern
1177 if [llength $args]==3 then {
1178 set message [lindex $args 2]
1180 set message $command
1183 return [gdb_test $command $pattern $message]
1186 # Wrapper around gdb_test_multiple that looks for a list of expected
1187 # output elements, but which can appear in any order.
1188 # CMD is the gdb command.
1189 # NAME is the name of the test.
1190 # ELM_FIND_REGEXP specifies how to partition the output into elements to
1192 # ELM_EXTRACT_REGEXP specifies the part of ELM_FIND_REGEXP to compare.
1193 # RESULT_MATCH_LIST is a list of exact matches for each expected element.
1194 # All elements of RESULT_MATCH_LIST must appear for the test to pass.
1196 # A typical use of ELM_FIND_REGEXP/ELM_EXTRACT_REGEXP is to extract one line
1197 # of text per element and then strip trailing \r\n's.
1199 # gdb_test_list_exact "foo" "bar" \
1200 # "\[^\r\n\]+\[\r\n\]+" \
1203 # {expected result 1} \
1204 # {expected result 2} \
1207 proc gdb_test_list_exact { cmd name elm_find_regexp elm_extract_regexp result_match_list } {
1210 set matches [lsort $result_match_list]
1212 gdb_test_multiple $cmd $name {
1213 "$cmd\[\r\n\]" { exp_continue }
1214 -re $elm_find_regexp {
1215 set str $expect_out(0,string)
1216 verbose -log "seen: $str" 3
1217 regexp -- $elm_extract_regexp $str elm_seen
1218 verbose -log "extracted: $elm_seen" 3
1219 lappend seen $elm_seen
1222 -re "$gdb_prompt $" {
1224 foreach got [lsort $seen] have $matches {
1225 if {![string equal $got $have]} {
1230 if {[string length $failed] != 0} {
1231 fail "$name ($failed not found)"
1239 # gdb_test_stdio COMMAND INFERIOR_PATTERN GDB_PATTERN MESSAGE
1240 # Send a command to gdb; expect inferior and gdb output.
1242 # See gdb_test_multiple for a description of the COMMAND and MESSAGE
1245 # INFERIOR_PATTERN is the pattern to match against inferior output.
1247 # GDB_PATTERN is the pattern to match against gdb output, and must NOT
1248 # include the \r\n sequence immediately before the gdb prompt, nor the
1249 # prompt. The default is empty.
1251 # Both inferior and gdb patterns must match for a PASS.
1253 # If MESSAGE is ommitted, then COMMAND will be used as the message.
1256 # 1 if the test failed,
1257 # 0 if the test passes,
1258 # -1 if there was an internal error.
1261 proc gdb_test_stdio {command inferior_pattern {gdb_pattern ""} {message ""}} {
1262 global inferior_spawn_id gdb_spawn_id
1265 if {$message == ""} {
1266 set message $command
1269 set inferior_matched 0
1272 # Use an indirect spawn id list, and remove the inferior spawn id
1273 # from the expected output as soon as it matches, in case
1274 # $inferior_pattern happens to be a prefix of the resulting full
1275 # gdb pattern below (e.g., "\r\n").
1276 global gdb_test_stdio_spawn_id_list
1277 set gdb_test_stdio_spawn_id_list "$inferior_spawn_id"
1279 # Note that if $inferior_spawn_id and $gdb_spawn_id are different,
1280 # then we may see gdb's output arriving before the inferior's
1282 set res [gdb_test_multiple $command $message {
1283 -i gdb_test_stdio_spawn_id_list -re "$inferior_pattern" {
1284 set inferior_matched 1
1285 if {!$gdb_matched} {
1286 set gdb_test_stdio_spawn_id_list ""
1290 -i $gdb_spawn_id -re "$gdb_pattern\r\n$gdb_prompt $" {
1292 if {!$inferior_matched} {
1300 verbose -log "inferior_matched=$inferior_matched, gdb_matched=$gdb_matched"
1307 # Issue a PASS and return true if evaluating CONDITION in the caller's
1308 # frame returns true, and issue a FAIL and return false otherwise.
1309 # MESSAGE is the pass/fail message to be printed. If MESSAGE is
1310 # omitted or is empty, then the pass/fail messages use the condition
1311 # string as the message.
1313 proc gdb_assert { condition {message ""} } {
1314 if { $message == ""} {
1315 set message $condition
1318 set res [uplevel 1 expr $condition]
1327 proc gdb_reinitialize_dir { subdir } {
1330 if [is_remote host] {
1335 -re "Reinitialize source path to empty.*y or n. " {
1338 -re "Source directories searched.*$gdb_prompt $" {
1339 send_gdb "dir $subdir\n"
1341 -re "Source directories searched.*$gdb_prompt $" {
1342 verbose "Dir set to $subdir"
1344 -re "$gdb_prompt $" {
1345 perror "Dir \"$subdir\" failed."
1349 -re "$gdb_prompt $" {
1350 perror "Dir \"$subdir\" failed."
1354 -re "$gdb_prompt $" {
1355 perror "Dir \"$subdir\" failed."
1361 # gdb_exit -- exit the GDB, killing the target program if necessary
1363 proc default_gdb_exit {} {
1365 global INTERNAL_GDBFLAGS GDBFLAGS
1367 global gdb_spawn_id inferior_spawn_id
1368 global inotify_log_file
1370 gdb_stop_suppressing_tests
1372 if ![info exists gdb_spawn_id] {
1376 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1378 if {[info exists inotify_log_file] && [file exists $inotify_log_file]} {
1379 set fd [open $inotify_log_file]
1380 set data [read -nonewline $fd]
1383 if {[string compare $data ""] != 0} {
1384 warning "parallel-unsafe file creations noticed"
1387 set fd [open $inotify_log_file w]
1392 if { [is_remote host] && [board_info host exists fileid] } {
1399 -re "DOSEXIT code" { }
1404 if ![is_remote host] {
1408 unset inferior_spawn_id
1411 # Load a file into the debugger.
1412 # The return value is 0 for success, -1 for failure.
1414 # This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1415 # to one of these values:
1417 # debug file was loaded successfully and has debug information
1418 # nodebug file was loaded successfully and has no debug information
1419 # lzma file was loaded, .gnu_debugdata found, but no LZMA support
1421 # fail file was not loaded
1423 # I tried returning this information as part of the return value,
1424 # but ran into a mess because of the many re-implementations of
1425 # gdb_load in config/*.exp.
1427 # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
1428 # this if they can get more information set.
1430 proc gdb_file_cmd { arg } {
1434 global last_loaded_file
1436 # Save this for the benefit of gdbserver-support.exp.
1437 set last_loaded_file $arg
1439 # Set whether debug info was found.
1440 # Default to "fail".
1441 global gdb_file_cmd_debug_info
1442 set gdb_file_cmd_debug_info "fail"
1444 if [is_remote host] {
1445 set arg [remote_download host $arg]
1447 perror "download failed"
1452 # The file command used to kill the remote target. For the benefit
1453 # of the testsuite, preserve this behavior.
1456 -re "Kill the program being debugged. .y or n. $" {
1458 verbose "\t\tKilling previous program being debugged"
1461 -re "$gdb_prompt $" {
1466 send_gdb "file $arg\n"
1468 -re "Reading symbols from.*LZMA support was disabled.*done.*$gdb_prompt $" {
1469 verbose "\t\tLoaded $arg into $GDB; .gnu_debugdata found but no LZMA available"
1470 set gdb_file_cmd_debug_info "lzma"
1473 -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" {
1474 verbose "\t\tLoaded $arg into $GDB with no debugging symbols"
1475 set gdb_file_cmd_debug_info "nodebug"
1478 -re "Reading symbols from.*done.*$gdb_prompt $" {
1479 verbose "\t\tLoaded $arg into $GDB"
1480 set gdb_file_cmd_debug_info "debug"
1483 -re "Load new symbol table from \".*\".*y or n. $" {
1486 -re "Reading symbols from.*done.*$gdb_prompt $" {
1487 verbose "\t\tLoaded $arg with new symbol table into $GDB"
1488 set gdb_file_cmd_debug_info "debug"
1492 perror "Couldn't load $arg, other program already loaded (timeout)."
1496 perror "Couldn't load $arg, other program already loaded (eof)."
1501 -re "No such file or directory.*$gdb_prompt $" {
1502 perror "($arg) No such file or directory"
1505 -re "A problem internal to GDB has been detected" {
1506 fail "($arg) (GDB internal error)"
1507 gdb_internal_error_resync
1510 -re "$gdb_prompt $" {
1511 perror "Couldn't load $arg into $GDB."
1515 perror "Couldn't load $arg into $GDB (timeout)."
1519 # This is an attempt to detect a core dump, but seems not to
1520 # work. Perhaps we need to match .* followed by eof, in which
1521 # gdb_expect does not seem to have a way to do that.
1522 perror "Couldn't load $arg into $GDB (eof)."
1528 # Default gdb_spawn procedure.
1530 proc default_gdb_spawn { } {
1533 global INTERNAL_GDBFLAGS GDBFLAGS
1536 gdb_stop_suppressing_tests
1538 # Set the default value, it may be overriden later by specific testfile.
1540 # Use `set_board_info use_gdb_stub' for the board file to flag the inferior
1541 # is already started after connecting and run/attach are not supported.
1542 # This is used for the "remote" protocol. After GDB starts you should
1543 # check global $use_gdb_stub instead of the board as the testfile may force
1544 # a specific different target protocol itself.
1545 set use_gdb_stub [target_info exists use_gdb_stub]
1547 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1549 if [info exists gdb_spawn_id] {
1553 if ![is_remote host] {
1554 if { [which $GDB] == 0 } then {
1555 perror "$GDB does not exist."
1559 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]"]
1560 if { $res < 0 || $res == "" } {
1561 perror "Spawning $GDB failed."
1565 set gdb_spawn_id $res
1569 # Default gdb_start procedure.
1571 proc default_gdb_start { } {
1572 global gdb_prompt pagination_prompt
1574 global inferior_spawn_id
1576 if [info exists gdb_spawn_id] {
1585 # Default to assuming inferior I/O is done on GDB's terminal.
1586 if {![info exists inferior_spawn_id]} {
1587 set inferior_spawn_id $gdb_spawn_id
1590 # When running over NFS, particularly if running many simultaneous
1591 # tests on different hosts all using the same server, things can
1592 # get really slow. Give gdb at least 3 minutes to start up.
1594 while { $loop_again } {
1597 -re "$pagination_prompt" {
1598 verbose "Hit pagination during startup. Pressing enter to continue."
1602 -re "\[\r\n\]$gdb_prompt $" {
1603 verbose "GDB initialized."
1605 -re "$gdb_prompt $" {
1606 perror "GDB never initialized."
1611 perror "(timeout) GDB never initialized after 10 seconds."
1619 # force the height to "unlimited", so no pagers get used
1621 send_gdb "set height 0\n"
1623 -re "$gdb_prompt $" {
1624 verbose "Setting height to 0." 2
1627 warning "Couldn't set the height to 0"
1630 # force the width to "unlimited", so no wraparound occurs
1631 send_gdb "set width 0\n"
1633 -re "$gdb_prompt $" {
1634 verbose "Setting width to 0." 2
1637 warning "Couldn't set the width to 0."
1643 # Utility procedure to give user control of the gdb prompt in a script. It is
1644 # meant to be used for debugging test cases, and should not be left in the
1647 proc gdb_interact { } {
1649 set spawn_id $gdb_spawn_id
1651 send_user "+------------------------------------------+\n"
1652 send_user "| Script interrupted, you can now interact |\n"
1653 send_user "| with by gdb. Type >>> to continue. |\n"
1654 send_user "+------------------------------------------+\n"
1661 # Examine the output of compilation to determine whether compilation
1662 # failed or not. If it failed determine whether it is due to missing
1663 # compiler or due to compiler error. Report pass, fail or unsupported
1666 proc gdb_compile_test {src output} {
1667 if { $output == "" } {
1668 pass "compilation [file tail $src]"
1669 } elseif { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output] } {
1670 unsupported "compilation [file tail $src]"
1671 } elseif { [regexp {.*: command not found[\r|\n]*$} $output] } {
1672 unsupported "compilation [file tail $src]"
1673 } elseif { [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } {
1674 unsupported "compilation [file tail $src]"
1676 verbose -log "compilation failed: $output" 2
1677 fail "compilation [file tail $src]"
1681 # Return a 1 for configurations for which we don't even want to try to
1684 proc skip_cplus_tests {} {
1685 if { [istarget "h8300-*-*"] } {
1689 # The C++ IO streams are too large for HC11/HC12 and are thus not
1690 # available. The gdb C++ tests use them and don't compile.
1691 if { [istarget "m6811-*-*"] } {
1694 if { [istarget "m6812-*-*"] } {
1700 # Return a 1 for configurations for which don't have both C++ and the STL.
1702 proc skip_stl_tests {} {
1703 # Symbian supports the C++ language, but the STL is missing
1704 # (both headers and libraries).
1705 if { [istarget "arm*-*-symbianelf*"] } {
1709 return [skip_cplus_tests]
1712 # Return a 1 if I don't even want to try to test FORTRAN.
1714 proc skip_fortran_tests {} {
1718 # Return a 1 if I don't even want to try to test ada.
1720 proc skip_ada_tests {} {
1724 # Return a 1 if I don't even want to try to test GO.
1726 proc skip_go_tests {} {
1730 # Return a 1 if I don't even want to try to test D.
1732 proc skip_d_tests {} {
1736 # Return 1 to skip Rust tests, 0 to try them.
1737 proc skip_rust_tests {} {
1738 return [expr {![isnative]}]
1741 # Return a 1 for configurations that do not support Python scripting.
1742 # PROMPT_REGEXP is the expected prompt.
1744 proc skip_python_tests_prompt { prompt_regexp } {
1745 global gdb_py_is_py3k
1746 global gdb_py_is_py24
1748 gdb_test_multiple "python print ('test')" "verify python support" {
1749 -re "not supported.*$prompt_regexp" {
1750 unsupported "Python support is disabled."
1753 -re "$prompt_regexp" {}
1756 set gdb_py_is_py24 0
1757 gdb_test_multiple "python print (sys.version_info\[0\])" "check if python 3" {
1758 -re "3.*$prompt_regexp" {
1759 set gdb_py_is_py3k 1
1761 -re ".*$prompt_regexp" {
1762 set gdb_py_is_py3k 0
1765 if { $gdb_py_is_py3k == 0 } {
1766 gdb_test_multiple "python print (sys.version_info\[1\])" "check if python 2.4" {
1767 -re "\[45\].*$prompt_regexp" {
1768 set gdb_py_is_py24 1
1770 -re ".*$prompt_regexp" {
1771 set gdb_py_is_py24 0
1779 # Return a 1 for configurations that do not support Python scripting.
1780 # Note: This also sets various globals that specify which version of Python
1781 # is in use. See skip_python_tests_prompt.
1783 proc skip_python_tests {} {
1785 return [skip_python_tests_prompt "$gdb_prompt $"]
1788 # Return a 1 if we should skip shared library tests.
1790 proc skip_shlib_tests {} {
1791 # Run the shared library tests on native systems.
1796 # An abbreviated list of remote targets where we should be able to
1797 # run shared library tests.
1798 if {([istarget *-*-linux*]
1799 || [istarget *-*-*bsd*]
1800 || [istarget *-*-solaris2*]
1801 || [istarget arm*-*-symbianelf*]
1802 || [istarget *-*-mingw*]
1803 || [istarget *-*-cygwin*]
1804 || [istarget *-*-pe*])} {
1811 # Return 1 if we should skip tui related tests.
1813 proc skip_tui_tests {} {
1816 gdb_test_multiple "help layout" "verify tui support" {
1817 -re "Undefined command: \"layout\".*$gdb_prompt $" {
1820 -re "$gdb_prompt $" {
1827 # Test files shall make sure all the test result lines in gdb.sum are
1828 # unique in a test run, so that comparing the gdb.sum files of two
1829 # test runs gives correct results. Test files that exercise
1830 # variations of the same tests more than once, shall prefix the
1831 # different test invocations with different identifying strings in
1832 # order to make them unique.
1834 # About test prefixes:
1836 # $pf_prefix is the string that dejagnu prints after the result (FAIL,
1837 # PASS, etc.), and before the test message/name in gdb.sum. E.g., the
1838 # underlined substring in
1840 # PASS: gdb.base/mytest.exp: some test
1841 # ^^^^^^^^^^^^^^^^^^^^
1845 # The easiest way to adjust the test prefix is to append a test
1846 # variation prefix to the $pf_prefix, using the with_test_prefix
1849 # proc do_tests {} {
1850 # gdb_test ... ... "test foo"
1851 # gdb_test ... ... "test bar"
1853 # with_test_prefix "subvariation a" {
1854 # gdb_test ... ... "test x"
1857 # with_test_prefix "subvariation b" {
1858 # gdb_test ... ... "test x"
1862 # with_test_prefix "variation1" {
1863 # ...do setup for variation 1...
1867 # with_test_prefix "variation2" {
1868 # ...do setup for variation 2...
1874 # PASS: gdb.base/mytest.exp: variation1: test foo
1875 # PASS: gdb.base/mytest.exp: variation1: test bar
1876 # PASS: gdb.base/mytest.exp: variation1: subvariation a: test x
1877 # PASS: gdb.base/mytest.exp: variation1: subvariation b: test x
1878 # PASS: gdb.base/mytest.exp: variation2: test foo
1879 # PASS: gdb.base/mytest.exp: variation2: test bar
1880 # PASS: gdb.base/mytest.exp: variation2: subvariation a: test x
1881 # PASS: gdb.base/mytest.exp: variation2: subvariation b: test x
1883 # If for some reason more flexibility is necessary, one can also
1884 # manipulate the pf_prefix global directly, treating it as a string.
1888 # set saved_pf_prefix
1889 # append pf_prefix "${foo}: bar"
1890 # ... actual tests ...
1891 # set pf_prefix $saved_pf_prefix
1894 # Run BODY in the context of the caller, with the current test prefix
1895 # (pf_prefix) appended with one space, then PREFIX, and then a colon.
1896 # Returns the result of BODY.
1898 proc with_test_prefix { prefix body } {
1901 set saved $pf_prefix
1902 append pf_prefix " " $prefix ":"
1903 set code [catch {uplevel 1 $body} result]
1904 set pf_prefix $saved
1907 global errorInfo errorCode
1908 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
1910 return -code $code $result
1914 # Wrapper for foreach that calls with_test_prefix on each iteration,
1915 # including the iterator's name and current value in the prefix.
1917 proc foreach_with_prefix {var list body} {
1919 foreach myvar $list {
1920 with_test_prefix "$var=$myvar" {
1926 # Like TCL's native proc, but defines a procedure that wraps its body
1927 # within 'with_test_prefix "$proc_name" { ... }'.
1928 proc proc_with_prefix {name arguments body} {
1929 # Define the advertised proc.
1930 proc $name $arguments [list with_test_prefix $name $body]
1934 # Run BODY in the context of the caller. After BODY is run, the variables
1935 # listed in VARS will be reset to the values they had before BODY was run.
1937 # This is useful for providing a scope in which it is safe to temporarily
1938 # modify global variables, e.g.
1940 # global INTERNAL_GDBFLAGS
1943 # set foo GDBHISTSIZE
1945 # save_vars { INTERNAL_GDBFLAGS env($foo) env(HOME) } {
1946 # append INTERNAL_GDBFLAGS " -nx"
1947 # unset -nocomplain env(GDBHISTSIZE)
1952 # Here, although INTERNAL_GDBFLAGS, env(GDBHISTSIZE) and env(HOME) may be
1953 # modified inside BODY, this proc guarantees that the modifications will be
1954 # undone after BODY finishes executing.
1956 proc save_vars { vars body } {
1957 array set saved_scalars { }
1958 array set saved_arrays { }
1962 # First evaluate VAR in the context of the caller in case the variable
1963 # name may be a not-yet-interpolated string like env($foo)
1964 set var [uplevel 1 list $var]
1966 if [uplevel 1 [list info exists $var]] {
1967 if [uplevel 1 [list array exists $var]] {
1968 set saved_arrays($var) [uplevel 1 [list array get $var]]
1970 set saved_scalars($var) [uplevel 1 [list set $var]]
1973 lappend unset_vars $var
1977 set code [catch {uplevel 1 $body} result]
1979 foreach {var value} [array get saved_scalars] {
1980 uplevel 1 [list set $var $value]
1983 foreach {var value} [array get saved_arrays] {
1984 uplevel 1 [list unset $var]
1985 uplevel 1 [list array set $var $value]
1988 foreach var $unset_vars {
1989 uplevel 1 [list unset -nocomplain $var]
1993 global errorInfo errorCode
1994 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
1996 return -code $code $result
2001 # Run tests in BODY with GDB prompt and variable $gdb_prompt set to
2002 # PROMPT. When BODY is finished, restore GDB prompt and variable
2004 # Returns the result of BODY.
2008 # 1) If you want to use, for example, "(foo)" as the prompt you must pass it
2009 # as "(foo)", and not the regexp form "\(foo\)" (expressed as "\\(foo\\)" in
2010 # TCL). PROMPT is internally converted to a suitable regexp for matching.
2011 # We do the conversion from "(foo)" to "\(foo\)" here for a few reasons:
2012 # a) It's more intuitive for callers to pass the plain text form.
2013 # b) We need two forms of the prompt:
2014 # - a regexp to use in output matching,
2015 # - a value to pass to the "set prompt" command.
2016 # c) It's easier to convert the plain text form to its regexp form.
2018 # 2) Don't add a trailing space, we do that here.
2020 proc with_gdb_prompt { prompt body } {
2023 # Convert "(foo)" to "\(foo\)".
2024 # We don't use string_to_regexp because while it works today it's not
2025 # clear it will work tomorrow: the value we need must work as both a
2026 # regexp *and* as the argument to the "set prompt" command, at least until
2027 # we start recording both forms separately instead of just $gdb_prompt.
2028 # The testsuite is pretty-much hardwired to interpret $gdb_prompt as the
2030 regsub -all {[]*+.|()^$\[\\]} $prompt {\\&} prompt
2032 set saved $gdb_prompt
2034 verbose -log "Setting gdb prompt to \"$prompt \"."
2035 set gdb_prompt $prompt
2036 gdb_test_no_output "set prompt $prompt " ""
2038 set code [catch {uplevel 1 $body} result]
2040 verbose -log "Restoring gdb prompt to \"$saved \"."
2041 set gdb_prompt $saved
2042 gdb_test_no_output "set prompt $saved " ""
2045 global errorInfo errorCode
2046 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2048 return -code $code $result
2052 # Run tests in BODY with target-charset setting to TARGET_CHARSET. When
2053 # BODY is finished, restore target-charset.
2055 proc with_target_charset { target_charset body } {
2059 gdb_test_multiple "show target-charset" "" {
2060 -re "The target character set is \".*; currently (.*)\"\..*$gdb_prompt " {
2061 set saved $expect_out(1,string)
2063 -re "The target character set is \"(.*)\".*$gdb_prompt " {
2064 set saved $expect_out(1,string)
2066 -re ".*$gdb_prompt " {
2067 fail "get target-charset"
2071 gdb_test_no_output "set target-charset $target_charset" ""
2073 set code [catch {uplevel 1 $body} result]
2075 gdb_test_no_output "set target-charset $saved" ""
2078 global errorInfo errorCode
2079 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2081 return -code $code $result
2085 # Switch the default spawn id to SPAWN_ID, so that gdb_test,
2086 # mi_gdb_test etc. default to using it.
2088 proc switch_gdb_spawn_id {spawn_id} {
2090 global board board_info
2092 set gdb_spawn_id $spawn_id
2093 set board [host_info name]
2094 set board_info($board,fileid) $spawn_id
2097 # Clear the default spawn id.
2099 proc clear_gdb_spawn_id {} {
2101 global board board_info
2103 unset -nocomplain gdb_spawn_id
2104 set board [host_info name]
2105 unset -nocomplain board_info($board,fileid)
2108 # Run BODY with SPAWN_ID as current spawn id.
2110 proc with_spawn_id { spawn_id body } {
2113 if [info exists gdb_spawn_id] {
2114 set saved_spawn_id $gdb_spawn_id
2117 switch_gdb_spawn_id $spawn_id
2119 set code [catch {uplevel 1 $body} result]
2121 if [info exists saved_spawn_id] {
2122 switch_gdb_spawn_id $saved_spawn_id
2128 global errorInfo errorCode
2129 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2131 return -code $code $result
2135 # Select the largest timeout from all the timeouts:
2136 # - the local "timeout" variable of the scope two levels above,
2137 # - the global "timeout" variable,
2138 # - the board variable "gdb,timeout".
2140 proc get_largest_timeout {} {
2141 upvar #0 timeout gtimeout
2142 upvar 2 timeout timeout
2145 if [info exists timeout] {
2148 if { [info exists gtimeout] && $gtimeout > $tmt } {
2151 if { [target_info exists gdb,timeout]
2152 && [target_info gdb,timeout] > $tmt } {
2153 set tmt [target_info gdb,timeout]
2163 # Run tests in BODY with timeout increased by factor of FACTOR. When
2164 # BODY is finished, restore timeout.
2166 proc with_timeout_factor { factor body } {
2169 set savedtimeout $timeout
2171 set timeout [expr [get_largest_timeout] * $factor]
2172 set code [catch {uplevel 1 $body} result]
2174 set timeout $savedtimeout
2176 global errorInfo errorCode
2177 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2179 return -code $code $result
2183 # Return 1 if _Complex types are supported, otherwise, return 0.
2185 gdb_caching_proc support_complex_tests {
2187 if { [gdb_skip_float_test] } {
2188 # If floating point is not supported, _Complex is not
2193 # Set up, compile, and execute a test program containing _Complex types.
2194 # Include the current process ID in the file names to prevent conflicts
2195 # with invocations for multiple testsuites.
2196 set src [standard_temp_file complex[pid].c]
2197 set exe [standard_temp_file complex[pid].x]
2199 gdb_produce_source $src {
2203 _Complex long double cld;
2208 verbose "compiling testfile $src" 2
2209 set compile_flags {debug nowarnings quiet}
2210 set lines [gdb_compile $src $exe executable $compile_flags]
2214 if ![string match "" $lines] then {
2215 verbose "testfile compilation failed, returning 0" 2
2224 # Return 1 if GDB can get a type for siginfo from the target, otherwise
2227 proc supports_get_siginfo_type {} {
2228 if { [istarget "*-*-linux*"] } {
2235 # Return 1 if the target supports hardware single stepping.
2237 proc can_hardware_single_step {} {
2239 if { [istarget "arm*-*-*"] || [istarget "mips*-*-*"]
2240 || [istarget "tic6x-*-*"] || [istarget "sparc*-*-linux*"]
2241 || [istarget "nios2-*-*"] } {
2248 # Return 1 if target hardware or OS supports single stepping to signal
2249 # handler, otherwise, return 0.
2251 proc can_single_step_to_signal_handler {} {
2252 # Targets don't have hardware single step. On these targets, when
2253 # a signal is delivered during software single step, gdb is unable
2254 # to determine the next instruction addresses, because start of signal
2255 # handler is one of them.
2256 return [can_hardware_single_step]
2259 # Return 1 if target supports process record, otherwise return 0.
2261 proc supports_process_record {} {
2263 if [target_info exists gdb,use_precord] {
2264 return [target_info gdb,use_precord]
2267 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
2268 || [istarget "i\[34567\]86-*-linux*"]
2269 || [istarget "aarch64*-*-linux*"]
2270 || [istarget "powerpc*-*-linux*"]
2271 || [istarget "s390*-*-linux*"] } {
2278 # Return 1 if target supports reverse debugging, otherwise return 0.
2280 proc supports_reverse {} {
2282 if [target_info exists gdb,can_reverse] {
2283 return [target_info gdb,can_reverse]
2286 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
2287 || [istarget "i\[34567\]86-*-linux*"]
2288 || [istarget "aarch64*-*-linux*"]
2289 || [istarget "powerpc*-*-linux*"]
2290 || [istarget "s390*-*-linux*"] } {
2297 # Return 1 if readline library is used.
2299 proc readline_is_used { } {
2302 gdb_test_multiple "show editing" "" {
2303 -re ".*Editing of command lines as they are typed is on\..*$gdb_prompt $" {
2306 -re ".*$gdb_prompt $" {
2312 # Return 1 if target is ELF.
2313 gdb_caching_proc is_elf_target {
2314 set me "is_elf_target"
2316 set src [standard_temp_file is_elf_target[pid].c]
2317 set obj [standard_temp_file is_elf_target[pid].o]
2319 gdb_produce_source $src {
2320 int foo () {return 0;}
2323 verbose "$me: compiling testfile $src" 2
2324 set lines [gdb_compile $src $obj object {quiet}]
2328 if ![string match "" $lines] then {
2329 verbose "$me: testfile compilation failed, returning 0" 2
2333 set fp_obj [open $obj "r"]
2334 fconfigure $fp_obj -translation binary
2335 set data [read $fp_obj]
2340 set ELFMAG "\u007FELF"
2342 if {[string compare -length 4 $data $ELFMAG] != 0} {
2343 verbose "$me: returning 0" 2
2347 verbose "$me: returning 1" 2
2351 # Return 1 if the memory at address zero is readable.
2353 gdb_caching_proc is_address_zero_readable {
2357 gdb_test_multiple "x 0" "" {
2358 -re "Cannot access memory at address 0x0.*$gdb_prompt $" {
2361 -re ".*$gdb_prompt $" {
2369 # Produce source file NAME and write SOURCES into it.
2371 proc gdb_produce_source { name sources } {
2373 set f [open $name "w"]
2379 # Return 1 if target is ILP32.
2380 # This cannot be decided simply from looking at the target string,
2381 # as it might depend on externally passed compiler options like -m64.
2382 gdb_caching_proc is_ilp32_target {
2383 set me "is_ilp32_target"
2385 set src [standard_temp_file ilp32[pid].c]
2386 set obj [standard_temp_file ilp32[pid].o]
2388 gdb_produce_source $src {
2389 int dummy[sizeof (int) == 4
2390 && sizeof (void *) == 4
2391 && sizeof (long) == 4 ? 1 : -1];
2394 verbose "$me: compiling testfile $src" 2
2395 set lines [gdb_compile $src $obj object {quiet}]
2399 if ![string match "" $lines] then {
2400 verbose "$me: testfile compilation failed, returning 0" 2
2404 verbose "$me: returning 1" 2
2408 # Return 1 if target is LP64.
2409 # This cannot be decided simply from looking at the target string,
2410 # as it might depend on externally passed compiler options like -m64.
2411 gdb_caching_proc is_lp64_target {
2412 set me "is_lp64_target"
2414 set src [standard_temp_file lp64[pid].c]
2415 set obj [standard_temp_file lp64[pid].o]
2417 gdb_produce_source $src {
2418 int dummy[sizeof (int) == 4
2419 && sizeof (void *) == 8
2420 && sizeof (long) == 8 ? 1 : -1];
2423 verbose "$me: compiling testfile $src" 2
2424 set lines [gdb_compile $src $obj object {quiet}]
2428 if ![string match "" $lines] then {
2429 verbose "$me: testfile compilation failed, returning 0" 2
2433 verbose "$me: returning 1" 2
2437 # Return 1 if target has 64 bit addresses.
2438 # This cannot be decided simply from looking at the target string,
2439 # as it might depend on externally passed compiler options like -m64.
2440 gdb_caching_proc is_64_target {
2441 set me "is_64_target"
2443 set src [standard_temp_file is64[pid].c]
2444 set obj [standard_temp_file is64[pid].o]
2446 gdb_produce_source $src {
2447 int function(void) { return 3; }
2448 int dummy[sizeof (&function) == 8 ? 1 : -1];
2451 verbose "$me: compiling testfile $src" 2
2452 set lines [gdb_compile $src $obj object {quiet}]
2456 if ![string match "" $lines] then {
2457 verbose "$me: testfile compilation failed, returning 0" 2
2461 verbose "$me: returning 1" 2
2465 # Return 1 if target has x86_64 registers - either amd64 or x32.
2466 # x32 target identifies as x86_64-*-linux*, therefore it cannot be determined
2467 # just from the target string.
2468 gdb_caching_proc is_amd64_regs_target {
2469 if {![istarget "x86_64-*-*"] && ![istarget "i?86-*"]} {
2473 set me "is_amd64_regs_target"
2475 set src [standard_temp_file reg64[pid].s]
2476 set obj [standard_temp_file reg64[pid].o]
2480 {rax rbx rcx rdx rsi rdi rbp rsp r8 r9 r10 r11 r12 r13 r14 r15} {
2481 lappend list "\tincq %$reg"
2483 gdb_produce_source $src [join $list \n]
2485 verbose "$me: compiling testfile $src" 2
2486 set lines [gdb_compile $src $obj object {quiet}]
2490 if ![string match "" $lines] then {
2491 verbose "$me: testfile compilation failed, returning 0" 2
2495 verbose "$me: returning 1" 2
2499 # Return 1 if this target is an x86 or x86-64 with -m32.
2500 proc is_x86_like_target {} {
2501 if {![istarget "x86_64-*-*"] && ![istarget i?86-*]} {
2504 return [expr [is_ilp32_target] && ![is_amd64_regs_target]]
2507 # Return 1 if this target is an arm or aarch32 on aarch64.
2509 gdb_caching_proc is_aarch32_target {
2510 if { [istarget "arm*-*-*"] } {
2514 if { ![istarget "aarch64*-*-*"] } {
2518 set me "is_aarch32_target"
2520 set src [standard_temp_file aarch32[pid].s]
2521 set obj [standard_temp_file aarch32[pid].o]
2526 lappend list "\tmov $reg, $reg"
2528 gdb_produce_source $src [join $list \n]
2530 verbose "$me: compiling testfile $src" 2
2531 set lines [gdb_compile $src $obj object {quiet}]
2535 if ![string match "" $lines] then {
2536 verbose "$me: testfile compilation failed, returning 0" 2
2540 verbose "$me: returning 1" 2
2544 # Return 1 if this target is an aarch64, either lp64 or ilp32.
2546 proc is_aarch64_target {} {
2547 if { ![istarget "aarch64*-*-*"] } {
2551 return [expr ![is_aarch32_target]]
2554 # Return 1 if displaced stepping is supported on target, otherwise, return 0.
2555 proc support_displaced_stepping {} {
2557 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"]
2558 || [istarget "arm*-*-linux*"] || [istarget "powerpc-*-linux*"]
2559 || [istarget "powerpc64-*-linux*"] || [istarget "s390*-*-*"]
2560 || [istarget "aarch64*-*-linux*"] } {
2567 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
2568 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2570 gdb_caching_proc skip_altivec_tests {
2571 global srcdir subdir gdb_prompt inferior_exited_re
2573 set me "skip_altivec_tests"
2575 # Some simulators are known to not support VMX instructions.
2576 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
2577 verbose "$me: target known to not support VMX, returning 1" 2
2581 # Make sure we have a compiler that understands altivec.
2582 set compile_flags {debug nowarnings}
2583 if [get_compiler_info] {
2584 warning "Could not get compiler info"
2587 if [test_compiler_info gcc*] {
2588 set compile_flags "$compile_flags additional_flags=-maltivec"
2589 } elseif [test_compiler_info xlc*] {
2590 set compile_flags "$compile_flags additional_flags=-qaltivec"
2592 verbose "Could not compile with altivec support, returning 1" 2
2596 # Set up, compile, and execute a test program containing VMX instructions.
2597 # Include the current process ID in the file names to prevent conflicts
2598 # with invocations for multiple testsuites.
2599 set src [standard_temp_file vmx[pid].c]
2600 set exe [standard_temp_file vmx[pid].x]
2602 gdb_produce_source $src {
2605 asm volatile ("vor v0,v0,v0");
2607 asm volatile ("vor 0,0,0");
2613 verbose "$me: compiling testfile $src" 2
2614 set lines [gdb_compile $src $exe executable $compile_flags]
2617 if ![string match "" $lines] then {
2618 verbose "$me: testfile compilation failed, returning 1" 2
2622 # No error message, compilation succeeded so now run it via gdb.
2626 gdb_reinitialize_dir $srcdir/$subdir
2630 -re ".*Illegal instruction.*${gdb_prompt} $" {
2631 verbose -log "\n$me altivec hardware not detected"
2632 set skip_vmx_tests 1
2634 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
2635 verbose -log "\n$me: altivec hardware detected"
2636 set skip_vmx_tests 0
2639 warning "\n$me: default case taken"
2640 set skip_vmx_tests 1
2644 remote_file build delete $exe
2646 verbose "$me: returning $skip_vmx_tests" 2
2647 return $skip_vmx_tests
2650 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
2651 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2653 gdb_caching_proc skip_vsx_tests {
2654 global srcdir subdir gdb_prompt inferior_exited_re
2656 set me "skip_vsx_tests"
2658 # Some simulators are known to not support Altivec instructions, so
2659 # they won't support VSX instructions as well.
2660 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
2661 verbose "$me: target known to not support VSX, returning 1" 2
2665 # Make sure we have a compiler that understands altivec.
2666 set compile_flags {debug nowarnings quiet}
2667 if [get_compiler_info] {
2668 warning "Could not get compiler info"
2671 if [test_compiler_info gcc*] {
2672 set compile_flags "$compile_flags additional_flags=-mvsx"
2673 } elseif [test_compiler_info xlc*] {
2674 set compile_flags "$compile_flags additional_flags=-qasm=gcc"
2676 verbose "Could not compile with vsx support, returning 1" 2
2680 set src [standard_temp_file vsx[pid].c]
2681 set exe [standard_temp_file vsx[pid].x]
2683 gdb_produce_source $src {
2685 double a[2] = { 1.0, 2.0 };
2687 asm volatile ("lxvd2x v0,v0,%[addr]" : : [addr] "r" (a));
2689 asm volatile ("lxvd2x 0,0,%[addr]" : : [addr] "r" (a));
2695 verbose "$me: compiling testfile $src" 2
2696 set lines [gdb_compile $src $exe executable $compile_flags]
2699 if ![string match "" $lines] then {
2700 verbose "$me: testfile compilation failed, returning 1" 2
2704 # No error message, compilation succeeded so now run it via gdb.
2708 gdb_reinitialize_dir $srcdir/$subdir
2712 -re ".*Illegal instruction.*${gdb_prompt} $" {
2713 verbose -log "\n$me VSX hardware not detected"
2714 set skip_vsx_tests 1
2716 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
2717 verbose -log "\n$me: VSX hardware detected"
2718 set skip_vsx_tests 0
2721 warning "\n$me: default case taken"
2722 set skip_vsx_tests 1
2726 remote_file build delete $exe
2728 verbose "$me: returning $skip_vsx_tests" 2
2729 return $skip_vsx_tests
2732 # Run a test on the target to see if it supports TSX hardware. Return 0 if so,
2733 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2735 gdb_caching_proc skip_tsx_tests {
2736 global srcdir subdir gdb_prompt inferior_exited_re
2738 set me "skip_tsx_tests"
2740 set src [standard_temp_file tsx[pid].c]
2741 set exe [standard_temp_file tsx[pid].x]
2743 gdb_produce_source $src {
2745 asm volatile ("xbegin .L0");
2746 asm volatile ("xend");
2747 asm volatile (".L0: nop");
2752 verbose "$me: compiling testfile $src" 2
2753 set lines [gdb_compile $src $exe executable {nowarnings quiet}]
2756 if ![string match "" $lines] then {
2757 verbose "$me: testfile compilation failed." 2
2761 # No error message, compilation succeeded so now run it via gdb.
2765 gdb_reinitialize_dir $srcdir/$subdir
2769 -re ".*Illegal instruction.*${gdb_prompt} $" {
2770 verbose -log "$me: TSX hardware not detected."
2771 set skip_tsx_tests 1
2773 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
2774 verbose -log "$me: TSX hardware detected."
2775 set skip_tsx_tests 0
2778 warning "\n$me: default case taken."
2779 set skip_tsx_tests 1
2783 remote_file build delete $exe
2785 verbose "$me: returning $skip_tsx_tests" 2
2786 return $skip_tsx_tests
2789 # Run a test on the target to see if it supports btrace hardware. Return 0 if so,
2790 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2792 gdb_caching_proc skip_btrace_tests {
2793 global srcdir subdir gdb_prompt inferior_exited_re
2795 set me "skip_btrace_tests"
2796 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
2797 verbose "$me: target does not support btrace, returning 1" 2
2801 # Set up, compile, and execute a test program.
2802 # Include the current process ID in the file names to prevent conflicts
2803 # with invocations for multiple testsuites.
2804 set src [standard_temp_file btrace[pid].c]
2805 set exe [standard_temp_file btrace[pid].x]
2807 gdb_produce_source $src {
2808 int main(void) { return 0; }
2811 verbose "$me: compiling testfile $src" 2
2812 set compile_flags {debug nowarnings quiet}
2813 set lines [gdb_compile $src $exe executable $compile_flags]
2815 if ![string match "" $lines] then {
2816 verbose "$me: testfile compilation failed, returning 1" 2
2821 # No error message, compilation succeeded so now run it via gdb.
2825 gdb_reinitialize_dir $srcdir/$subdir
2832 # In case of an unexpected output, we return 2 as a fail value.
2833 set skip_btrace_tests 2
2834 gdb_test_multiple "record btrace" "check btrace support" {
2835 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
2836 set skip_btrace_tests 1
2838 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
2839 set skip_btrace_tests 1
2841 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
2842 set skip_btrace_tests 1
2844 -re "^record btrace\r\n$gdb_prompt $" {
2845 set skip_btrace_tests 0
2849 remote_file build delete $exe
2851 verbose "$me: returning $skip_btrace_tests" 2
2852 return $skip_btrace_tests
2855 # Run a test on the target to see if it supports btrace pt hardware.
2856 # Return 0 if so, 1 if it does not. Based on 'check_vmx_hw_available'
2857 # from the GCC testsuite.
2859 gdb_caching_proc skip_btrace_pt_tests {
2860 global srcdir subdir gdb_prompt inferior_exited_re
2862 set me "skip_btrace_tests"
2863 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
2864 verbose "$me: target does not support btrace, returning 1" 2
2868 # Set up, compile, and execute a test program.
2869 # Include the current process ID in the file names to prevent conflicts
2870 # with invocations for multiple testsuites.
2871 set src [standard_temp_file btrace[pid].c]
2872 set exe [standard_temp_file btrace[pid].x]
2874 gdb_produce_source $src {
2875 int main(void) { return 0; }
2878 verbose "$me: compiling testfile $src" 2
2879 set compile_flags {debug nowarnings quiet}
2880 set lines [gdb_compile $src $exe executable $compile_flags]
2882 if ![string match "" $lines] then {
2883 verbose "$me: testfile compilation failed, returning 1" 2
2888 # No error message, compilation succeeded so now run it via gdb.
2892 gdb_reinitialize_dir $srcdir/$subdir
2899 # In case of an unexpected output, we return 2 as a fail value.
2900 set skip_btrace_tests 2
2901 gdb_test_multiple "record btrace pt" "check btrace support" {
2902 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
2903 set skip_btrace_tests 1
2905 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
2906 set skip_btrace_tests 1
2908 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
2909 set skip_btrace_tests 1
2911 -re "GDB does not support.*\r\n$gdb_prompt $" {
2912 set skip_btrace_tests 1
2914 -re "^record btrace pt\r\n$gdb_prompt $" {
2915 set skip_btrace_tests 0
2919 remote_file build delete $exe
2921 verbose "$me: returning $skip_btrace_tests" 2
2922 return $skip_btrace_tests
2925 # Return whether we should skip tests for showing inlined functions in
2926 # backtraces. Requires get_compiler_info and get_debug_format.
2928 proc skip_inline_frame_tests {} {
2929 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
2930 if { ! [test_debug_format "DWARF 2"] } {
2934 # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line.
2935 if { ([test_compiler_info "gcc-2-*"]
2936 || [test_compiler_info "gcc-3-*"]
2937 || [test_compiler_info "gcc-4-0-*"]) } {
2944 # Return whether we should skip tests for showing variables from
2945 # inlined functions. Requires get_compiler_info and get_debug_format.
2947 proc skip_inline_var_tests {} {
2948 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
2949 if { ! [test_debug_format "DWARF 2"] } {
2956 # Return a 1 if we should skip tests that require hardware breakpoints
2958 proc skip_hw_breakpoint_tests {} {
2959 # Skip tests if requested by the board (note that no_hardware_watchpoints
2960 # disables both watchpoints and breakpoints)
2961 if { [target_info exists gdb,no_hardware_watchpoints]} {
2965 # These targets support hardware breakpoints natively
2966 if { [istarget "i?86-*-*"]
2967 || [istarget "x86_64-*-*"]
2968 || [istarget "ia64-*-*"]
2969 || [istarget "arm*-*-*"]
2970 || [istarget "aarch64*-*-*"]
2971 || [istarget "s390*-*-*"] } {
2978 # Return a 1 if we should skip tests that require hardware watchpoints
2980 proc skip_hw_watchpoint_tests {} {
2981 # Skip tests if requested by the board
2982 if { [target_info exists gdb,no_hardware_watchpoints]} {
2986 # These targets support hardware watchpoints natively
2987 if { [istarget "i?86-*-*"]
2988 || [istarget "x86_64-*-*"]
2989 || [istarget "ia64-*-*"]
2990 || [istarget "arm*-*-*"]
2991 || [istarget "aarch64*-*-*"]
2992 || [istarget "powerpc*-*-linux*"]
2993 || [istarget "s390*-*-*"] } {
3000 # Return a 1 if we should skip tests that require *multiple* hardware
3001 # watchpoints to be active at the same time
3003 proc skip_hw_watchpoint_multi_tests {} {
3004 if { [skip_hw_watchpoint_tests] } {
3008 # These targets support just a single hardware watchpoint
3009 if { [istarget "arm*-*-*"]
3010 || [istarget "powerpc*-*-linux*"] } {
3017 # Return a 1 if we should skip tests that require read/access watchpoints
3019 proc skip_hw_watchpoint_access_tests {} {
3020 if { [skip_hw_watchpoint_tests] } {
3024 # These targets support just write watchpoints
3025 if { [istarget "s390*-*-*"] } {
3032 # Return 1 if we should skip tests that require the runtime unwinder
3033 # hook. This must be invoked while gdb is running, after shared
3034 # libraries have been loaded. This is needed because otherwise a
3035 # shared libgcc won't be visible.
3037 proc skip_unwinder_tests {} {
3041 gdb_test_multiple "print _Unwind_DebugHook" "check for unwinder hook" {
3042 -re "= .*no debug info.*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
3044 -re "= .*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
3047 -re "No symbol .* in current context.\r\n$gdb_prompt $" {
3051 gdb_test_multiple "info probe" "check for stap probe in unwinder" {
3052 -re ".*libgcc.*unwind.*\r\n$gdb_prompt $" {
3055 -re "\r\n$gdb_prompt $" {
3062 # Return 0 if we should skip tests that require the libstdc++ stap
3063 # probes. This must be invoked while gdb is running, after shared
3064 # libraries have been loaded.
3066 proc skip_libstdcxx_probe_tests {} {
3070 gdb_test_multiple "info probe" "check for stap probe in libstdc++" {
3071 -re ".*libstdcxx.*catch.*\r\n$gdb_prompt $" {
3074 -re "\r\n$gdb_prompt $" {
3080 # Return 1 if we should skip tests of the "compile" feature.
3081 # This must be invoked after the inferior has been started.
3083 proc skip_compile_feature_tests {} {
3087 gdb_test_multiple "compile code -- ;" "check for working compile command" {
3088 "Could not load libcc1.*\r\n$gdb_prompt $" {
3091 -re "Command not supported on this host\\..*\r\n$gdb_prompt $" {
3094 -re "\r\n$gdb_prompt $" {
3100 # Helper for gdb_is_target_remote. PROMPT_REGEXP is the expected
3103 proc gdb_is_target_remote_prompt { prompt_regexp } {
3105 set test "probe for target remote"
3106 gdb_test_multiple "maint print target-stack" $test {
3107 -re ".*emote serial target in gdb-specific protocol.*$prompt_regexp" {
3111 -re "$prompt_regexp" {
3118 # Check whether we're testing with the remote or extended-remote
3121 proc gdb_is_target_remote {} {
3124 return [gdb_is_target_remote_prompt "$gdb_prompt $"]
3127 # Return the effective value of use_gdb_stub.
3129 # If the use_gdb_stub global has been set (it is set when the gdb process is
3130 # spawned), return that. Otherwise, return the value of the use_gdb_stub
3131 # property from the board file.
3133 # This is the preferred way of checking use_gdb_stub, since it allows to check
3134 # the value before the gdb has been spawned and it will return the correct value
3135 # even when it was overriden by the test.
3137 proc use_gdb_stub {} {
3140 if [info exists use_gdb_stub] {
3141 return $use_gdb_stub
3144 return [target_info exists use_gdb_stub]
3147 # Return 1 if the current remote target is an instance of our GDBserver, 0
3148 # otherwise. Return -1 if there was an error and we can't tell.
3150 gdb_caching_proc target_is_gdbserver {
3154 set test "probing for GDBserver"
3156 gdb_test_multiple "monitor help" $test {
3157 -re "The following monitor commands are supported.*Quit GDBserver.*$gdb_prompt $" {
3160 -re "$gdb_prompt $" {
3165 if { $is_gdbserver == -1 } {
3166 verbose -log "Unable to tell whether we are using GDBserver or not."
3169 return $is_gdbserver
3172 # N.B. compiler_info is intended to be local to this file.
3173 # Call test_compiler_info with no arguments to fetch its value.
3174 # Yes, this is counterintuitive when there's get_compiler_info,
3175 # but that's the current API.
3176 if [info exists compiler_info] {
3182 # Figure out what compiler I am using.
3183 # The result is cached so only the first invocation runs the compiler.
3185 # ARG can be empty or "C++". If empty, "C" is assumed.
3187 # There are several ways to do this, with various problems.
3189 # [ gdb_compile -E $ifile -o $binfile.ci ]
3190 # source $binfile.ci
3192 # Single Unix Spec v3 says that "-E -o ..." together are not
3193 # specified. And in fact, the native compiler on hp-ux 11 (among
3194 # others) does not work with "-E -o ...". Most targets used to do
3195 # this, and it mostly worked, because it works with gcc.
3197 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
3198 # source $binfile.ci
3200 # This avoids the problem with -E and -o together. This almost works
3201 # if the build machine is the same as the host machine, which is
3202 # usually true of the targets which are not gcc. But this code does
3203 # not figure which compiler to call, and it always ends up using the C
3204 # compiler. Not good for setting hp_aCC_compiler. Target
3205 # hppa*-*-hpux* used to do this.
3207 # [ gdb_compile -E $ifile > $binfile.ci ]
3208 # source $binfile.ci
3210 # dejagnu target_compile says that it supports output redirection,
3211 # but the code is completely different from the normal path and I
3212 # don't want to sweep the mines from that path. So I didn't even try
3215 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
3218 # I actually do this for all targets now. gdb_compile runs the right
3219 # compiler, and TCL captures the output, and I eval the output.
3221 # Unfortunately, expect logs the output of the command as it goes by,
3222 # and dejagnu helpfully prints a second copy of it right afterwards.
3223 # So I turn off expect logging for a moment.
3225 # [ gdb_compile $ifile $ciexe_file executable $args ]
3226 # [ remote_exec $ciexe_file ]
3227 # [ source $ci_file.out ]
3229 # I could give up on -E and just do this.
3230 # I didn't get desperate enough to try this.
3232 # -- chastain 2004-01-06
3234 proc get_compiler_info {{arg ""}} {
3235 # For compiler.c and compiler.cc
3238 # I am going to play with the log to keep noise out.
3242 # These come from compiler.c or compiler.cc
3243 global compiler_info
3245 # Legacy global data symbols.
3248 if [info exists compiler_info] {
3253 # Choose which file to preprocess.
3254 set ifile "${srcdir}/lib/compiler.c"
3255 if { $arg == "c++" } {
3256 set ifile "${srcdir}/lib/compiler.cc"
3259 # Run $ifile through the right preprocessor.
3260 # Toggle gdb.log to keep the compiler output out of the log.
3261 set saved_log [log_file -info]
3263 if [is_remote host] {
3264 # We have to use -E and -o together, despite the comments
3265 # above, because of how DejaGnu handles remote host testing.
3266 set ppout "$outdir/compiler.i"
3267 gdb_compile "${ifile}" "$ppout" preprocess [list "$arg" quiet]
3268 set file [open $ppout r]
3269 set cppout [read $file]
3272 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$arg" quiet] ]
3274 eval log_file $saved_log
3278 foreach cppline [ split "$cppout" "\n" ] {
3279 if { [ regexp "^#" "$cppline" ] } {
3281 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
3283 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
3285 verbose "get_compiler_info: $cppline" 2
3289 verbose -log "get_compiler_info: $cppline"
3294 # Set to unknown if for some reason compiler_info didn't get defined.
3295 if ![info exists compiler_info] {
3296 verbose -log "get_compiler_info: compiler_info not provided"
3297 set compiler_info "unknown"
3299 # Also set to unknown compiler if any diagnostics happened.
3301 verbose -log "get_compiler_info: got unexpected diagnostics"
3302 set compiler_info "unknown"
3305 # Set the legacy symbols.
3307 regexp "^gcc-(\[0-9\]+)-" "$compiler_info" matchall gcc_compiled
3309 # Log what happened.
3310 verbose -log "get_compiler_info: $compiler_info"
3312 # Most compilers will evaluate comparisons and other boolean
3313 # operations to 0 or 1.
3314 uplevel \#0 { set true 1 }
3315 uplevel \#0 { set false 0 }
3320 # Return the compiler_info string if no arg is provided.
3321 # Otherwise the argument is a glob-style expression to match against
3324 proc test_compiler_info { {compiler ""} } {
3325 global compiler_info
3328 # If no arg, return the compiler_info string.
3329 if [string match "" $compiler] {
3330 return $compiler_info
3333 return [string match $compiler $compiler_info]
3336 proc current_target_name { } {
3338 if [info exists target_info(target,name)] {
3339 set answer $target_info(target,name)
3346 set gdb_wrapper_initialized 0
3347 set gdb_wrapper_target ""
3349 proc gdb_wrapper_init { args } {
3350 global gdb_wrapper_initialized
3351 global gdb_wrapper_file
3352 global gdb_wrapper_flags
3353 global gdb_wrapper_target
3355 if { $gdb_wrapper_initialized == 1 } { return; }
3357 if {[target_info exists needs_status_wrapper] && \
3358 [target_info needs_status_wrapper] != "0"} {
3359 set result [build_wrapper "testglue.o"]
3360 if { $result != "" } {
3361 set gdb_wrapper_file [lindex $result 0]
3362 set gdb_wrapper_flags [lindex $result 1]
3364 warning "Status wrapper failed to build."
3367 set gdb_wrapper_initialized 1
3368 set gdb_wrapper_target [current_target_name]
3371 # Determine options that we always want to pass to the compiler.
3372 gdb_caching_proc universal_compile_options {
3373 set me "universal_compile_options"
3376 set src [standard_temp_file ccopts[pid].c]
3377 set obj [standard_temp_file ccopts[pid].o]
3379 gdb_produce_source $src {
3380 int foo(void) { return 0; }
3383 # Try an option for disabling colored diagnostics. Some compilers
3384 # yield colored diagnostics by default (when run from a tty) unless
3385 # such an option is specified.
3386 set opt "additional_flags=-fdiagnostics-color=never"
3387 set lines [target_compile $src $obj object [list "quiet" $opt]]
3388 if [string match "" $lines] then {
3389 # Seems to have worked; use the option.
3390 lappend options $opt
3395 verbose "$me: returning $options" 2
3399 # Some targets need to always link a special object in. Save its path here.
3400 global gdb_saved_set_unbuffered_mode_obj
3401 set gdb_saved_set_unbuffered_mode_obj ""
3403 # Compile source files specified by SOURCE into a binary of type TYPE at path
3404 # DEST. gdb_compile is implemented using DejaGnu's target_compile, so the type
3405 # parameter and most options are passed directly to it.
3407 # The type can be one of the following:
3409 # - object: Compile into an object file.
3410 # - executable: Compile and link into an executable.
3411 # - preprocess: Preprocess the source files.
3412 # - assembly: Generate assembly listing.
3414 # The following options are understood and processed by gdb_compile:
3416 # - shlib=so_path: Add SO_PATH to the sources, and enable some target-specific
3417 # quirks to be able to use shared libraries.
3418 # - shlib_load: Link with appropriate libraries to allow the test to
3419 # dynamically load libraries at runtime. For example, on Linux, this adds
3420 # -ldl so that the test can use dlopen.
3421 # - nowarnings: Inhibit all compiler warnings.
3423 # And here are some of the not too obscure options understood by DejaGnu that
3424 # influence the compilation:
3426 # - additional_flags=flag: Add FLAG to the compiler flags.
3427 # - libs=library: Add LIBRARY to the libraries passed to the linker. The
3428 # argument can be a file, in which case it's added to the sources, or a
3430 # - ldflags=flag: Add FLAG to the linker flags.
3431 # - incdir=path: Add PATH to the searched include directories.
3432 # - libdir=path: Add PATH to the linker searched directories.
3433 # - ada, c++, f77: Compile the file as Ada, C++ or Fortran.
3434 # - debug: Build with debug information.
3435 # - optimize: Build with optimization.
3437 proc gdb_compile {source dest type options} {
3438 global GDB_TESTCASE_OPTIONS
3439 global gdb_wrapper_file
3440 global gdb_wrapper_flags
3441 global gdb_wrapper_initialized
3444 global gdb_saved_set_unbuffered_mode_obj
3446 set outdir [file dirname $dest]
3448 # Add platform-specific options if a shared library was specified using
3449 # "shlib=librarypath" in OPTIONS.
3450 set new_options [universal_compile_options]
3453 foreach opt $options {
3454 if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
3455 if [test_compiler_info "xlc-*"] {
3456 # IBM xlc compiler doesn't accept shared library named other
3457 # than .so: use "-Wl," to bypass this
3458 lappend source "-Wl,$shlib_name"
3459 } elseif { ([istarget "*-*-mingw*"]
3460 || [istarget *-*-cygwin*]
3461 || [istarget *-*-pe*])} {
3462 lappend source "${shlib_name}.a"
3464 lappend source $shlib_name
3466 if { $shlib_found == 0 } {
3468 if { ([istarget "*-*-mingw*"]
3469 || [istarget *-*-cygwin*]) } {
3470 lappend new_options "additional_flags=-Wl,--enable-auto-import"
3472 if { [test_compiler_info "gcc-*"] || [test_compiler_info "clang-*"] } {
3473 # Undo debian's change in the default.
3474 # Put it at the front to not override any user-provided
3475 # value, and to make sure it appears in front of all the
3477 lappend new_options "early_flags=-Wl,--no-as-needed"
3480 } elseif { $opt == "shlib_load" } {
3483 lappend new_options $opt
3487 # Because we link with libraries using their basename, we may need
3488 # (depending on the platform) to set a special rpath value, to allow
3489 # the executable to find the libraries it depends on.
3490 if { $shlib_load || $shlib_found } {
3491 if { ([istarget "*-*-mingw*"]
3492 || [istarget *-*-cygwin*]
3493 || [istarget *-*-pe*]) } {
3494 # Do not need anything.
3495 } elseif { [istarget *-*-freebsd*] || [istarget *-*-openbsd*] } {
3496 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
3497 } elseif { [istarget arm*-*-symbianelf*] } {
3498 if { $shlib_load } {
3499 lappend new_options "libs=-ldl"
3502 if { $shlib_load } {
3503 lappend new_options "libs=-ldl"
3505 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
3508 set options $new_options
3510 if [info exists GDB_TESTCASE_OPTIONS] {
3511 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS"
3513 verbose "options are $options"
3514 verbose "source is $source $dest $type $options"
3516 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
3518 if {[target_info exists needs_status_wrapper] && \
3519 [target_info needs_status_wrapper] != "0" && \
3520 [info exists gdb_wrapper_file]} {
3521 lappend options "libs=${gdb_wrapper_file}"
3522 lappend options "ldflags=${gdb_wrapper_flags}"
3525 # Replace the "nowarnings" option with the appropriate additional_flags
3526 # to disable compiler warnings.
3527 set nowarnings [lsearch -exact $options nowarnings]
3528 if {$nowarnings != -1} {
3529 if [target_info exists gdb,nowarnings_flag] {
3530 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
3532 set flag "additional_flags=-w"
3534 set options [lreplace $options $nowarnings $nowarnings $flag]
3537 if { $type == "executable" } {
3538 if { ([istarget "*-*-mingw*"]
3539 || [istarget "*-*-*djgpp"]
3540 || [istarget "*-*-cygwin*"])} {
3541 # Force output to unbuffered mode, by linking in an object file
3542 # with a global contructor that calls setvbuf.
3544 # Compile the special object seperatelly for two reasons:
3545 # 1) Insulate it from $options.
3546 # 2) Avoid compiling it for every gdb_compile invocation,
3547 # which is time consuming, especially if we're remote
3550 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
3551 verbose "compiling gdb_saved_set_unbuffered_obj"
3552 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
3553 set unbuf_obj ${objdir}/set_unbuffered_mode.o
3555 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
3556 if { $result != "" } {
3559 if {[is_remote host]} {
3560 set gdb_saved_set_unbuffered_mode_obj set_unbuffered_mode_saved.o
3562 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
3564 # Link a copy of the output object, because the
3565 # original may be automatically deleted.
3566 remote_download host $unbuf_obj $gdb_saved_set_unbuffered_mode_obj
3568 verbose "gdb_saved_set_unbuffered_obj already compiled"
3571 # Rely on the internal knowledge that the global ctors are ran in
3572 # reverse link order. In that case, we can use ldflags to
3573 # avoid copying the object file to the host multiple
3575 # This object can only be added if standard libraries are
3576 # used. Thus, we need to disable it if -nostdlib option is used
3577 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
3578 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
3583 set result [target_compile $source $dest $type $options]
3585 # Prune uninteresting compiler (and linker) output.
3586 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
3588 regsub "\[\r\n\]*$" "$result" "" result
3589 regsub "^\[\r\n\]*" "$result" "" result
3591 if {[lsearch $options quiet] < 0} {
3592 # We shall update this on a per language basis, to avoid
3593 # changing the entire testsuite in one go.
3594 if {[lsearch $options f77] >= 0} {
3595 gdb_compile_test $source $result
3596 } elseif { $result != "" } {
3597 clone_output "gdb compile failed, $result"
3604 # This is just like gdb_compile, above, except that it tries compiling
3605 # against several different thread libraries, to see which one this
3607 proc gdb_compile_pthreads {source dest type options} {
3609 set why_msg "unrecognized error"
3610 foreach lib {-lpthreads -lpthread -lthread ""} {
3611 # This kind of wipes out whatever libs the caller may have
3612 # set. Or maybe theirs will override ours. How infelicitous.
3613 set options_with_lib [concat $options [list libs=$lib quiet]]
3614 set ccout [gdb_compile $source $dest $type $options_with_lib]
3615 switch -regexp -- $ccout {
3616 ".*no posix threads support.*" {
3617 set why_msg "missing threads include file"
3620 ".*cannot open -lpthread.*" {
3621 set why_msg "missing runtime threads library"
3623 ".*Can't find library for -lpthread.*" {
3624 set why_msg "missing runtime threads library"
3627 pass "successfully compiled posix threads test case"
3633 if {!$built_binfile} {
3634 unsupported "couldn't compile [file tail $source]: ${why_msg}"
3639 # Build a shared library from SOURCES.
3641 proc gdb_compile_shlib {sources dest options} {
3642 set obj_options $options
3645 if { [lsearch -exact $options "c++"] >= 0 } {
3646 set info_options "c++"
3648 if [get_compiler_info ${info_options}] {
3652 switch -glob [test_compiler_info] {
3654 lappend obj_options "additional_flags=-qpic"
3657 if { !([istarget "*-*-cygwin*"]
3658 || [istarget "*-*-mingw*"]) } {
3659 lappend obj_options "additional_flags=-fpic"
3663 if { !([istarget "powerpc*-*-aix*"]
3664 || [istarget "rs6000*-*-aix*"]
3665 || [istarget "*-*-cygwin*"]
3666 || [istarget "*-*-mingw*"]
3667 || [istarget "*-*-pe*"]) } {
3668 lappend obj_options "additional_flags=-fpic"
3672 lappend obj_options "additional_flags=-fpic"
3675 # don't know what the compiler is...
3679 set outdir [file dirname $dest]
3681 foreach source $sources {
3682 set sourcebase [file tail $source]
3683 if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
3686 lappend objects ${outdir}/${sourcebase}.o
3689 set link_options $options
3690 if [test_compiler_info "xlc-*"] {
3691 lappend link_options "additional_flags=-qmkshrobj"
3693 lappend link_options "additional_flags=-shared"
3695 if { ([istarget "*-*-mingw*"]
3696 || [istarget *-*-cygwin*]
3697 || [istarget *-*-pe*]) } {
3698 if { [is_remote host] } {
3699 set name [file tail ${dest}]
3703 lappend link_options "additional_flags=-Wl,--out-implib,${name}.a"
3705 # Set the soname of the library. This causes the linker on ELF
3706 # systems to create the DT_NEEDED entry in the executable referring
3707 # to the soname of the library, and not its absolute path. This
3708 # (using the absolute path) would be problem when testing on a
3711 # In conjunction with setting the soname, we add the special
3712 # rpath=$ORIGIN value when building the executable, so that it's
3713 # able to find the library in its own directory.
3714 set destbase [file tail $dest]
3715 lappend link_options "additional_flags=-Wl,-soname,$destbase"
3718 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
3721 if { [is_remote host]
3722 && ([istarget "*-*-mingw*"]
3723 || [istarget *-*-cygwin*]
3724 || [istarget *-*-pe*]) } {
3725 set dest_tail_name [file tail ${dest}]
3726 remote_upload host $dest_tail_name.a ${dest}.a
3727 remote_file host delete $dest_tail_name.a
3733 # This is just like gdb_compile_shlib, above, except that it tries compiling
3734 # against several different thread libraries, to see which one this
3736 proc gdb_compile_shlib_pthreads {sources dest options} {
3738 set why_msg "unrecognized error"
3739 foreach lib {-lpthreads -lpthread -lthread ""} {
3740 # This kind of wipes out whatever libs the caller may have
3741 # set. Or maybe theirs will override ours. How infelicitous.
3742 set options_with_lib [concat $options [list libs=$lib quiet]]
3743 set ccout [gdb_compile_shlib $sources $dest $options_with_lib]
3744 switch -regexp -- $ccout {
3745 ".*no posix threads support.*" {
3746 set why_msg "missing threads include file"
3749 ".*cannot open -lpthread.*" {
3750 set why_msg "missing runtime threads library"
3752 ".*Can't find library for -lpthread.*" {
3753 set why_msg "missing runtime threads library"
3756 pass "successfully compiled posix threads test case"
3762 if {!$built_binfile} {
3763 unsupported "couldn't compile $sources: ${why_msg}"
3768 # This is just like gdb_compile_pthreads, above, except that we always add the
3769 # objc library for compiling Objective-C programs
3770 proc gdb_compile_objc {source dest type options} {
3772 set why_msg "unrecognized error"
3773 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
3774 # This kind of wipes out whatever libs the caller may have
3775 # set. Or maybe theirs will override ours. How infelicitous.
3776 if { $lib == "solaris" } {
3777 set lib "-lpthread -lposix4"
3779 if { $lib != "-lobjc" } {
3780 set lib "-lobjc $lib"
3782 set options_with_lib [concat $options [list libs=$lib quiet]]
3783 set ccout [gdb_compile $source $dest $type $options_with_lib]
3784 switch -regexp -- $ccout {
3785 ".*no posix threads support.*" {
3786 set why_msg "missing threads include file"
3789 ".*cannot open -lpthread.*" {
3790 set why_msg "missing runtime threads library"
3792 ".*Can't find library for -lpthread.*" {
3793 set why_msg "missing runtime threads library"
3796 pass "successfully compiled objc with posix threads test case"
3802 if {!$built_binfile} {
3803 unsupported "couldn't compile [file tail $source]: ${why_msg}"
3808 proc send_gdb { string } {
3809 global suppress_flag
3810 if { $suppress_flag } {
3813 return [remote_send host "$string"]
3816 # Send STRING to the inferior's terminal.
3818 proc send_inferior { string } {
3819 global inferior_spawn_id
3821 if {[catch "send -i $inferior_spawn_id -- \$string" errorInfo]} {
3831 proc gdb_expect { args } {
3832 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
3833 set atimeout [lindex $args 0]
3834 set expcode [list [lindex $args 1]]
3839 # A timeout argument takes precedence, otherwise of all the timeouts
3840 # select the largest.
3841 if [info exists atimeout] {
3844 set tmt [get_largest_timeout]
3847 global suppress_flag
3848 global remote_suppress_flag
3849 if [info exists remote_suppress_flag] {
3850 set old_val $remote_suppress_flag
3852 if [info exists suppress_flag] {
3853 if { $suppress_flag } {
3854 set remote_suppress_flag 1
3858 {uplevel remote_expect host $tmt $expcode} string]
3859 if [info exists old_val] {
3860 set remote_suppress_flag $old_val
3862 if [info exists remote_suppress_flag] {
3863 unset remote_suppress_flag
3868 global errorInfo errorCode
3870 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
3872 return -code $code $string
3876 # gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
3878 # Check for long sequence of output by parts.
3879 # TEST: is the test message to be printed with the test success/fail.
3880 # SENTINEL: Is the terminal pattern indicating that output has finished.
3881 # LIST: is the sequence of outputs to match.
3882 # If the sentinel is recognized early, it is considered an error.
3885 # 1 if the test failed,
3886 # 0 if the test passes,
3887 # -1 if there was an internal error.
3889 proc gdb_expect_list {test sentinel list} {
3891 global suppress_flag
3894 if { $suppress_flag } {
3896 unresolved "${test}"
3898 while { ${index} < [llength ${list}] } {
3899 set pattern [lindex ${list} ${index}]
3900 set index [expr ${index} + 1]
3901 verbose -log "gdb_expect_list pattern: /$pattern/" 2
3902 if { ${index} == [llength ${list}] } {
3905 -re "${pattern}${sentinel}" {
3906 # pass "${test}, pattern ${index} + sentinel"
3909 fail "${test} (pattern ${index} + sentinel)"
3912 -re ".*A problem internal to GDB has been detected" {
3913 fail "${test} (GDB internal error)"
3915 gdb_internal_error_resync
3918 fail "${test} (pattern ${index} + sentinel) (timeout)"
3923 # unresolved "${test}, pattern ${index} + sentinel"
3929 # pass "${test}, pattern ${index}"
3932 fail "${test} (pattern ${index})"
3935 -re ".*A problem internal to GDB has been detected" {
3936 fail "${test} (GDB internal error)"
3938 gdb_internal_error_resync
3941 fail "${test} (pattern ${index}) (timeout)"
3946 # unresolved "${test}, pattern ${index}"
3960 proc gdb_suppress_entire_file { reason } {
3961 global suppress_flag
3964 set suppress_flag -1
3968 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
3969 # gdb_expect to fail immediately (until the next call to
3970 # gdb_stop_suppressing_tests).
3972 proc gdb_suppress_tests { args } {
3973 global suppress_flag
3975 return; # fnf - disable pending review of results where
3976 # testsuite ran better without this
3979 if { $suppress_flag == 1 } {
3980 if { [llength $args] > 0 } {
3981 warning "[lindex $args 0]\n"
3983 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n"
3989 # Clear suppress_flag.
3991 proc gdb_stop_suppressing_tests { } {
3992 global suppress_flag
3994 if [info exists suppress_flag] {
3995 if { $suppress_flag > 0 } {
3997 clone_output "Tests restarted.\n"
4004 proc gdb_clear_suppressed { } {
4005 global suppress_flag
4010 # Spawn the gdb process.
4012 # This doesn't expect any output or do any other initialization,
4013 # leaving those to the caller.
4015 # Overridable function -- you can override this function in your
4018 proc gdb_spawn { } {
4022 # Spawn GDB with CMDLINE_FLAGS appended to the GDBFLAGS global.
4024 proc gdb_spawn_with_cmdline_opts { cmdline_flags } {
4027 set saved_gdbflags $GDBFLAGS
4029 if {$GDBFLAGS != ""} {
4032 append GDBFLAGS $cmdline_flags
4036 set GDBFLAGS $saved_gdbflags
4041 # Start gdb running, wait for prompt, and disable the pagers.
4043 # Overridable function -- you can override this function in your
4046 proc gdb_start { } {
4051 catch default_gdb_exit
4054 # Return true if we can spawn a program on the target and attach to
4057 proc can_spawn_for_attach { } {
4058 # We use exp_pid to get the inferior's pid, assuming that gives
4059 # back the pid of the program. On remote boards, that would give
4060 # us instead the PID of e.g., the ssh client, etc.
4061 if [is_remote target] then {
4065 # The "attach" command doesn't make sense when the target is
4066 # stub-like, where GDB finds the program already started on
4067 # initial connection.
4068 if {[target_info exists use_gdb_stub]} {
4076 # Kill a progress previously started with spawn_wait_for_attach, and
4077 # reap its wait status. PROC_SPAWN_ID is the spawn id associated with
4080 proc kill_wait_spawned_process { proc_spawn_id } {
4081 set pid [exp_pid -i $proc_spawn_id]
4083 verbose -log "killing ${pid}"
4084 remote_exec build "kill -9 ${pid}"
4086 verbose -log "closing ${proc_spawn_id}"
4087 catch "close -i $proc_spawn_id"
4088 verbose -log "waiting for ${proc_spawn_id}"
4090 # If somehow GDB ends up still attached to the process here, a
4091 # blocking wait hangs until gdb is killed (or until gdb / the
4092 # ptracer reaps the exit status too, but that won't happen because
4093 # something went wrong.) Passing -nowait makes expect tell Tcl to
4094 # wait for the PID in the background. That's fine because we
4095 # don't care about the exit status. */
4096 wait -nowait -i $proc_spawn_id
4099 # Returns the process id corresponding to the given spawn id.
4101 proc spawn_id_get_pid { spawn_id } {
4102 set testpid [exp_pid -i $spawn_id]
4104 if { [istarget "*-*-cygwin*"] } {
4105 # testpid is the Cygwin PID, GDB uses the Windows PID, which
4106 # might be different due to the way fork/exec works.
4107 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
4113 # Start a set of programs running and then wait for a bit, to be sure
4114 # that they can be attached to. Return a list of processes spawn IDs,
4115 # one element for each process spawned. It's a test error to call
4116 # this when [can_spawn_for_attach] is false.
4118 proc spawn_wait_for_attach { executable_list } {
4119 set spawn_id_list {}
4121 if ![can_spawn_for_attach] {
4122 # The caller should have checked can_spawn_for_attach itself
4123 # before getting here.
4124 error "can't spawn for attach with this target/board"
4127 foreach {executable} $executable_list {
4128 # Note we use Expect's spawn, not Tcl's exec, because with
4129 # spawn we control when to wait for/reap the process. That
4130 # allows killing the process by PID without being subject to
4132 lappend spawn_id_list [remote_spawn target $executable]
4137 return $spawn_id_list
4141 # gdb_load_cmd -- load a file into the debugger.
4142 # ARGS - additional args to load command.
4143 # return a -1 if anything goes wrong.
4145 proc gdb_load_cmd { args } {
4148 if [target_info exists gdb_load_timeout] {
4149 set loadtimeout [target_info gdb_load_timeout]
4151 set loadtimeout 1600
4153 send_gdb "load $args\n"
4154 verbose "Timeout is now $loadtimeout seconds" 2
4155 gdb_expect $loadtimeout {
4156 -re "Loading section\[^\r\]*\r\n" {
4159 -re "Start address\[\r\]*\r\n" {
4162 -re "Transfer rate\[\r\]*\r\n" {
4165 -re "Memory access error\[^\r\]*\r\n" {
4166 perror "Failed to load program"
4169 -re "$gdb_prompt $" {
4172 -re "(.*)\r\n$gdb_prompt " {
4173 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
4177 perror "Timed out trying to load $args."
4184 # Invoke "gcore". CORE is the name of the core file to write. TEST
4185 # is the name of the test case. This will return 1 if the core file
4186 # was created, 0 otherwise. If this fails to make a core file because
4187 # this configuration of gdb does not support making core files, it
4188 # will call "unsupported", not "fail". However, if this fails to make
4189 # a core file for some other reason, then it will call "fail".
4191 proc gdb_gcore_cmd {core test} {
4195 gdb_test_multiple "gcore $core" $test {
4196 -re "Saved corefile .*\[\r\n\]+$gdb_prompt $" {
4200 -re "(?:Can't create a corefile|Target does not support core file generation\\.)\[\r\n\]+$gdb_prompt $" {
4208 # Load core file CORE. TEST is the name of the test case.
4209 # This will record a pass/fail for loading the core file.
4211 # 1 - core file is successfully loaded
4212 # 0 - core file loaded but has a non fatal error
4213 # -1 - core file failed to load
4215 proc gdb_core_cmd { core test } {
4218 gdb_test_multiple "core $core" "$test" {
4219 -re "\\\[Thread debugging using \[^ \r\n\]* enabled\\\]\r\n" {
4222 -re " is not a core dump:.*\r\n$gdb_prompt $" {
4223 fail "$test (bad file format)"
4226 -re ": No such file or directory.*\r\n$gdb_prompt $" {
4227 fail "$test (file not found)"
4230 -re "Couldn't find .* registers in core file.*\r\n$gdb_prompt $" {
4231 fail "$test (incomplete note section)"
4234 -re "Core was generated by .*\r\n$gdb_prompt $" {
4238 -re ".*$gdb_prompt $" {
4243 fail "$test (timeout)"
4247 fail "unsupported output from 'core' command"
4251 # Return the filename to download to the target and load on the target
4252 # for this shared library. Normally just LIBNAME, unless shared libraries
4253 # for this target have separate link and load images.
4255 proc shlib_target_file { libname } {
4259 # Return the filename GDB will load symbols from when debugging this
4260 # shared library. Normally just LIBNAME, unless shared libraries for
4261 # this target have separate link and load images.
4263 proc shlib_symbol_file { libname } {
4267 # Return the filename to download to the target and load for this
4268 # executable. Normally just BINFILE unless it is renamed to something
4269 # else for this target.
4271 proc exec_target_file { binfile } {
4275 # Return the filename GDB will load symbols from when debugging this
4276 # executable. Normally just BINFILE unless executables for this target
4277 # have separate files for symbols.
4279 proc exec_symbol_file { binfile } {
4283 # Rename the executable file. Normally this is just BINFILE1 being renamed
4284 # to BINFILE2, but some targets require multiple binary files.
4285 proc gdb_rename_execfile { binfile1 binfile2 } {
4286 file rename -force [exec_target_file ${binfile1}] \
4287 [exec_target_file ${binfile2}]
4288 if { [exec_target_file ${binfile1}] != [exec_symbol_file ${binfile1}] } {
4289 file rename -force [exec_symbol_file ${binfile1}] \
4290 [exec_symbol_file ${binfile2}]
4294 # "Touch" the executable file to update the date. Normally this is just
4295 # BINFILE, but some targets require multiple files.
4296 proc gdb_touch_execfile { binfile } {
4297 set time [clock seconds]
4298 file mtime [exec_target_file ${binfile}] $time
4299 if { [exec_target_file ${binfile}] != [exec_symbol_file ${binfile}] } {
4300 file mtime [exec_symbol_file ${binfile}] $time
4304 # Like remote_download but provides a gdb-specific behavior.
4306 # If the destination board is remote, the local file FROMFILE is transferred as
4307 # usual with remote_download to TOFILE on the remote board. The destination
4308 # filename is added to the CLEANFILES global, so it can be cleaned up at the
4311 # If the destination board is local, the destination path TOFILE is passed
4312 # through standard_output_file, and FROMFILE is copied there.
4314 # In both cases, if TOFILE is omitted, it defaults to the [file tail] of
4317 proc gdb_remote_download {dest fromfile {tofile {}}} {
4318 # If TOFILE is not given, default to the same filename as FROMFILE.
4319 if {[string length $tofile] == 0} {
4320 set tofile [file tail $fromfile]
4323 if {[is_remote $dest]} {
4324 # When the DEST is remote, we simply send the file to DEST.
4327 set destname [remote_download $dest $fromfile $tofile]
4328 lappend cleanfiles $destname
4332 # When the DEST is local, we copy the file to the test directory (where
4333 # the executable is).
4335 # Note that we pass TOFILE through standard_output_file, regardless of
4336 # whether it is absolute or relative, because we don't want the tests
4337 # to be able to write outside their standard output directory.
4339 set tofile [standard_output_file $tofile]
4341 file copy -force $fromfile $tofile
4347 # gdb_load_shlib LIB...
4349 # Copy the listed library to the target.
4351 proc gdb_load_shlib { file } {
4352 set dest [gdb_remote_download target [shlib_target_file $file]]
4354 if {[is_remote target]} {
4355 # If the target is remote, we need to tell gdb where to find the
4358 # We could set this even when not testing remotely, but a user
4359 # generally won't set it unless necessary. In order to make the tests
4360 # more like the real-life scenarios, we don't set it for local testing.
4361 gdb_test "set solib-search-path [file dirname $file]" "" ""
4368 # gdb_load -- load a file into the debugger. Specifying no file
4369 # defaults to the executable currently being debugged.
4370 # The return value is 0 for success, -1 for failure.
4371 # Many files in config/*.exp override this procedure.
4373 proc gdb_load { arg } {
4375 return [gdb_file_cmd $arg]
4380 # gdb_reload -- load a file into the target. Called before "running",
4381 # either the first time or after already starting the program once,
4382 # for remote targets. Most files that override gdb_load should now
4383 # override this instead.
4385 proc gdb_reload { } {
4386 # For the benefit of existing configurations, default to gdb_load.
4387 # Specifying no file defaults to the executable currently being
4389 return [gdb_load ""]
4392 proc gdb_continue { function } {
4395 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"]
4398 proc default_gdb_init { test_file_name } {
4399 global gdb_wrapper_initialized
4400 global gdb_wrapper_target
4401 global gdb_test_file_name
4407 gdb_clear_suppressed
4409 set gdb_test_file_name [file rootname [file tail $test_file_name]]
4411 # Make sure that the wrapper is rebuilt
4412 # with the appropriate multilib option.
4413 if { $gdb_wrapper_target != [current_target_name] } {
4414 set gdb_wrapper_initialized 0
4417 # Unlike most tests, we have a small number of tests that generate
4418 # a very large amount of output. We therefore increase the expect
4419 # buffer size to be able to contain the entire test output. This
4420 # is especially needed by gdb.base/info-macros.exp.
4422 # Also set this value for the currently running GDB.
4423 match_max [match_max -d]
4425 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
4426 set pf_prefix "[file tail [file dirname $test_file_name]]/[file tail $test_file_name]:"
4429 if [target_info exists gdb_prompt] {
4430 set gdb_prompt [target_info gdb_prompt]
4432 set gdb_prompt "\\(gdb\\)"
4435 if [info exists use_gdb_stub] {
4440 # Return a path using GDB_PARALLEL.
4441 # ARGS is a list of path elements to append to "$objdir/$GDB_PARALLEL".
4442 # GDB_PARALLEL must be defined, the caller must check.
4444 # The default value for GDB_PARALLEL is, canonically, ".".
4445 # The catch is that tests don't expect an additional "./" in file paths so
4446 # omit any directory for the default case.
4447 # GDB_PARALLEL is written as "yes" for the default case in Makefile.in to mark
4448 # its special handling.
4450 proc make_gdb_parallel_path { args } {
4451 global GDB_PARALLEL objdir
4452 set joiner [list "file" "join" $objdir]
4453 if { [info exists GDB_PARALLEL] && $GDB_PARALLEL != "yes" } {
4454 lappend joiner $GDB_PARALLEL
4456 set joiner [concat $joiner $args]
4457 return [eval $joiner]
4460 # Turn BASENAME into a full file name in the standard output
4461 # directory. It is ok if BASENAME is the empty string; in this case
4462 # the directory is returned.
4464 proc standard_output_file {basename} {
4465 global objdir subdir gdb_test_file_name
4467 set dir [make_gdb_parallel_path outputs $subdir $gdb_test_file_name]
4469 return [file join $dir $basename]
4472 # Return the name of a file in our standard temporary directory.
4474 proc standard_temp_file {basename} {
4475 # Since a particular runtest invocation is only executing a single test
4476 # file at any given time, we can use the runtest pid to build the
4477 # path of the temp directory.
4478 set dir [make_gdb_parallel_path temp [pid]]
4480 return [file join $dir $basename]
4483 # Set 'testfile', 'srcfile', and 'binfile'.
4485 # ARGS is a list of source file specifications.
4486 # Without any arguments, the .exp file's base name is used to
4487 # compute the source file name. The ".c" extension is added in this case.
4488 # If ARGS is not empty, each entry is a source file specification.
4489 # If the specification starts with a ".", it is treated as a suffix
4490 # to append to the .exp file's base name.
4491 # If the specification is the empty string, it is treated as if it
4493 # Otherwise it is a file name.
4494 # The first file in the list is used to set the 'srcfile' global.
4495 # Each subsequent name is used to set 'srcfile2', 'srcfile3', etc.
4497 # Most tests should call this without arguments.
4499 # If a completely different binary file name is needed, then it
4500 # should be handled in the .exp file with a suitable comment.
4502 proc standard_testfile {args} {
4503 global gdb_test_file_name
4505 global gdb_test_file_last_vars
4508 global testfile binfile
4510 set testfile $gdb_test_file_name
4511 set binfile [standard_output_file ${testfile}]
4513 if {[llength $args] == 0} {
4517 # Unset our previous output variables.
4518 # This can help catch hidden bugs.
4519 if {[info exists gdb_test_file_last_vars]} {
4520 foreach varname $gdb_test_file_last_vars {
4522 catch {unset $varname}
4525 # 'executable' is often set by tests.
4526 set gdb_test_file_last_vars {executable}
4530 set varname srcfile$suffix
4533 # Handle an extension.
4536 } elseif {[string range $arg 0 0] == "."} {
4537 set arg $testfile$arg
4541 lappend gdb_test_file_last_vars $varname
4543 if {$suffix == ""} {
4551 # The default timeout used when testing GDB commands. We want to use
4552 # the same timeout as the default dejagnu timeout, unless the user has
4553 # already provided a specific value (probably through a site.exp file).
4554 global gdb_test_timeout
4555 if ![info exists gdb_test_timeout] {
4556 set gdb_test_timeout $timeout
4559 # A list of global variables that GDB testcases should not use.
4560 # We try to prevent their use by monitoring write accesses and raising
4561 # an error when that happens.
4562 set banned_variables { bug_id prms_id }
4564 # A list of procedures that GDB testcases should not use.
4565 # We try to prevent their use by monitoring invocations and raising
4566 # an error when that happens.
4567 set banned_procedures { strace }
4569 # gdb_init is called by runtest at start, but also by several
4570 # tests directly; gdb_finish is only called from within runtest after
4571 # each test source execution.
4572 # Placing several traces by repetitive calls to gdb_init leads
4573 # to problems, as only one trace is removed in gdb_finish.
4574 # To overcome this possible problem, we add a variable that records
4575 # if the banned variables and procedures are already traced.
4578 proc gdb_init { test_file_name } {
4579 # Reset the timeout value to the default. This way, any testcase
4580 # that changes the timeout value without resetting it cannot affect
4581 # the timeout used in subsequent testcases.
4582 global gdb_test_timeout
4584 set timeout $gdb_test_timeout
4586 if { [regexp ".*gdb\.reverse\/.*" $test_file_name]
4587 && [target_info exists gdb_reverse_timeout] } {
4588 set timeout [target_info gdb_reverse_timeout]
4591 # If GDB_INOTIFY is given, check for writes to '.'. This is a
4592 # debugging tool to help confirm that the test suite is
4593 # parallel-safe. You need "inotifywait" from the
4594 # inotify-tools package to use this.
4595 global GDB_INOTIFY inotify_pid
4596 if {[info exists GDB_INOTIFY] && ![info exists inotify_pid]} {
4597 global outdir tool inotify_log_file
4599 set exclusions {outputs temp gdb[.](log|sum) cache}
4600 set exclusion_re ([join $exclusions |])
4602 set inotify_log_file [standard_temp_file inotify.out]
4603 set inotify_pid [exec inotifywait -r -m -e move,create,delete . \
4604 --exclude $exclusion_re \
4605 |& tee -a $outdir/$tool.log $inotify_log_file &]
4607 # Wait for the watches; hopefully this is long enough.
4610 # Clear the log so that we don't emit a warning the first time
4612 set fd [open $inotify_log_file w]
4616 # Block writes to all banned variables, and invocation of all
4617 # banned procedures...
4618 global banned_variables
4619 global banned_procedures
4620 global banned_traced
4621 if (!$banned_traced) {
4622 foreach banned_var $banned_variables {
4623 global "$banned_var"
4624 trace add variable "$banned_var" write error
4626 foreach banned_proc $banned_procedures {
4627 global "$banned_proc"
4628 trace add execution "$banned_proc" enter error
4633 # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same
4634 # messages as expected.
4639 # Don't let a .inputrc file or an existing setting of INPUTRC mess up
4640 # the test results. Even if /dev/null doesn't exist on the particular
4641 # platform, the readline library will use the default setting just by
4642 # failing to open the file. OTOH, opening /dev/null successfully will
4643 # also result in the default settings being used since nothing will be
4644 # read from this file.
4645 setenv INPUTRC "/dev/null"
4647 # The gdb.base/readline.exp arrow key test relies on the standard VT100
4648 # bindings, so make sure that an appropriate terminal is selected.
4649 # The same bug doesn't show up if we use ^P / ^N instead.
4652 # Some tests (for example gdb.base/maint.exp) shell out from gdb to use
4653 # grep. Clear GREP_OPTIONS to make the behavior predictable,
4654 # especially having color output turned on can cause tests to fail.
4655 setenv GREP_OPTIONS ""
4657 # Clear $gdbserver_reconnect_p.
4658 global gdbserver_reconnect_p
4659 set gdbserver_reconnect_p 1
4660 unset gdbserver_reconnect_p
4662 return [default_gdb_init $test_file_name]
4665 proc gdb_finish { } {
4666 global gdbserver_reconnect_p
4670 # Exit first, so that the files are no longer in use.
4673 if { [llength $cleanfiles] > 0 } {
4674 eval remote_file target delete $cleanfiles
4678 # Unblock write access to the banned variables. Dejagnu typically
4679 # resets some of them between testcases.
4680 global banned_variables
4681 global banned_procedures
4682 global banned_traced
4683 if ($banned_traced) {
4684 foreach banned_var $banned_variables {
4685 global "$banned_var"
4686 trace remove variable "$banned_var" write error
4688 foreach banned_proc $banned_procedures {
4689 global "$banned_proc"
4690 trace remove execution "$banned_proc" enter error
4697 set debug_format "unknown"
4699 # Run the gdb command "info source" and extract the debugging format
4700 # information from the output and save it in debug_format.
4702 proc get_debug_format { } {
4708 set debug_format "unknown"
4709 send_gdb "info source\n"
4711 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
4712 set debug_format $expect_out(1,string)
4713 verbose "debug format is $debug_format"
4716 -re "No current source file.\r\n$gdb_prompt $" {
4717 perror "get_debug_format used when no current source file"
4720 -re "$gdb_prompt $" {
4721 warning "couldn't check debug format (no valid response)."
4725 warning "couldn't check debug format (timeout)."
4731 # Return true if FORMAT matches the debug format the current test was
4732 # compiled with. FORMAT is a shell-style globbing pattern; it can use
4733 # `*', `[...]', and so on.
4735 # This function depends on variables set by `get_debug_format', above.
4737 proc test_debug_format {format} {
4740 return [expr [string match $format $debug_format] != 0]
4743 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
4744 # COFF, stabs, etc). If that format matches the format that the
4745 # current test was compiled with, then the next test is expected to
4746 # fail for any target. Returns 1 if the next test or set of tests is
4747 # expected to fail, 0 otherwise (or if it is unknown). Must have
4748 # previously called get_debug_format.
4749 proc setup_xfail_format { format } {
4750 set ret [test_debug_format $format]
4758 # gdb_get_line_number TEXT [FILE]
4760 # Search the source file FILE, and return the line number of the
4761 # first line containing TEXT. If no match is found, an error is thrown.
4763 # TEXT is a string literal, not a regular expression.
4765 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
4766 # specified, and does not start with "/", then it is assumed to be in
4767 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
4768 # by changing the callers and the interface at the same time.
4769 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
4770 # gdb.base/ena-dis-br.exp.
4772 # Use this function to keep your test scripts independent of the
4773 # exact line numbering of the source file. Don't write:
4775 # send_gdb "break 20"
4777 # This means that if anyone ever edits your test's source file,
4778 # your test could break. Instead, put a comment like this on the
4779 # source file line you want to break at:
4781 # /* breakpoint spot: frotz.exp: test name */
4783 # and then write, in your test script (which we assume is named
4786 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
4788 # (Yes, Tcl knows how to handle the nested quotes and brackets.
4791 # % puts "foo [lindex "bar baz" 1]"
4794 # Tcl is quite clever, for a little stringy language.)
4798 # The previous implementation of this procedure used the gdb search command.
4799 # This version is different:
4801 # . It works with MI, and it also works when gdb is not running.
4803 # . It operates on the build machine, not the host machine.
4805 # . For now, this implementation fakes a current directory of
4806 # $srcdir/$subdir to be compatible with the old implementation.
4807 # This will go away eventually and some callers will need to
4810 # . The TEXT argument is literal text and matches literally,
4811 # not a regular expression as it was before.
4813 # . State changes in gdb, such as changing the current file
4814 # and setting $_, no longer happen.
4816 # After a bit of time we can forget about the differences from the
4817 # old implementation.
4819 # --chastain 2004-08-05
4821 proc gdb_get_line_number { text { file "" } } {
4826 if { "$file" == "" } then {
4829 if { ! [regexp "^/" "$file"] } then {
4830 set file "$srcdir/$subdir/$file"
4833 if { [ catch { set fd [open "$file"] } message ] } then {
4838 for { set line 1 } { 1 } { incr line } {
4839 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
4842 if { $nchar < 0 } then {
4845 if { [string first "$text" "$body"] >= 0 } then {
4851 if { [ catch { close "$fd" } message ] } then {
4856 error "undefined tag \"$text\""
4862 # Continue the program until it ends.
4864 # MSSG is the error message that gets printed. If not given, a
4866 # COMMAND is the command to invoke. If not given, "continue" is
4868 # ALLOW_EXTRA is a flag indicating whether the test should expect
4869 # extra output between the "Continuing." line and the program
4870 # exiting. By default it is zero; if nonzero, any extra output
4873 proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} {
4874 global inferior_exited_re use_gdb_stub
4877 set text "continue until exit"
4879 set text "continue until exit at $mssg"
4887 # By default, we don't rely on exit() behavior of remote stubs --
4888 # it's common for exit() to be implemented as a simple infinite
4889 # loop, or a forced crash/reset. For native targets, by default, we
4890 # assume process exit is reported as such. If a non-reliable target
4891 # is used, we set a breakpoint at exit, and continue to that.
4892 if { [target_info exists exit_is_reliable] } {
4893 set exit_is_reliable [target_info exit_is_reliable]
4895 set exit_is_reliable [expr ! $use_gdb_stub]
4898 if { ! $exit_is_reliable } {
4899 if {![gdb_breakpoint "exit"]} {
4902 gdb_test $command "Continuing..*Breakpoint .*exit.*" \
4905 # Continue until we exit. Should not stop again.
4906 # Don't bother to check the output of the program, that may be
4907 # extremely tough for some remote systems.
4909 "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\
4914 proc rerun_to_main {} {
4915 global gdb_prompt use_gdb_stub
4920 -re ".*Breakpoint .*main .*$gdb_prompt $"\
4921 {pass "rerun to main" ; return 0}
4922 -re "$gdb_prompt $"\
4923 {fail "rerun to main" ; return 0}
4924 timeout {fail "(timeout) rerun to main" ; return 0}
4929 -re "The program .* has been started already.*y or n. $" {
4933 -re "Starting program.*$gdb_prompt $"\
4934 {pass "rerun to main" ; return 0}
4935 -re "$gdb_prompt $"\
4936 {fail "rerun to main" ; return 0}
4937 timeout {fail "(timeout) rerun to main" ; return 0}
4942 # Return true if a test should be skipped due to lack of floating
4943 # point support or GDB can't fetch the contents from floating point
4946 gdb_caching_proc gdb_skip_float_test {
4947 if [target_info exists gdb,skip_float_tests] {
4951 # There is an ARM kernel ptrace bug that hardware VFP registers
4952 # are not updated after GDB ptrace set VFP registers. The bug
4953 # was introduced by kernel commit 8130b9d7b9d858aa04ce67805e8951e3cb6e9b2f
4954 # in 2012 and is fixed in e2dfb4b880146bfd4b6aa8e138c0205407cebbaf
4955 # in May 2016. In other words, kernels older than 4.6.3, 4.4.14,
4956 # 4.1.27, 3.18.36, and 3.14.73 have this bug.
4957 # This kernel bug is detected by check how does GDB change the
4958 # program result by changing one VFP register.
4959 if { [istarget "arm*-*-linux*"] } {
4961 set compile_flags {debug nowarnings }
4963 # Set up, compile, and execute a test program having VFP
4965 set src [standard_temp_file arm_vfp[pid].c]
4966 set exe [standard_temp_file arm_vfp[pid].x]
4968 gdb_produce_source $src {
4973 asm ("vldr d0, [%0]" : : "r" (&d));
4974 asm ("vldr d1, [%0]" : : "r" (&d));
4975 asm (".global break_here\n"
4977 asm ("vcmp.f64 d0, d1\n"
4978 "vmrs APSR_nzcv, fpscr\n"
4979 "bne L_value_different\n"
4982 "L_value_different:\n"
4984 "L_end:\n" : "=r" (ret) :);
4986 /* Return $d0 != $d1. */
4991 verbose "compiling testfile $src" 2
4992 set lines [gdb_compile $src $exe executable $compile_flags]
4995 if ![string match "" $lines] then {
4996 verbose "testfile compilation failed, returning 1" 2
5000 # No error message, compilation succeeded so now run it via gdb.
5001 # Run the test up to 5 times to detect whether ptrace can
5002 # correctly update VFP registers or not.
5004 for {set i 0} {$i < 5} {incr i} {
5005 global gdb_prompt srcdir subdir
5009 gdb_reinitialize_dir $srcdir/$subdir
5013 gdb_test "break *break_here"
5014 gdb_continue_to_breakpoint "break_here"
5016 # Modify $d0 to a different value, so the exit code should
5018 gdb_test "set \$d0 = 5.0"
5020 set test "continue to exit"
5021 gdb_test_multiple "continue" "$test" {
5022 -re "exited with code 01.*$gdb_prompt $" {
5024 -re "exited normally.*$gdb_prompt $" {
5025 # However, the exit code is 0. That means something
5026 # wrong in setting VFP registers.
5034 remote_file build delete $exe
5036 return $skip_vfp_test
5041 # Print a message and return true if a test should be skipped
5042 # due to lack of stdio support.
5044 proc gdb_skip_stdio_test { msg } {
5045 if [target_info exists gdb,noinferiorio] {
5046 verbose "Skipping test '$msg': no inferior i/o."
5052 proc gdb_skip_bogus_test { msg } {
5056 # Return true if a test should be skipped due to lack of XML support
5058 # NOTE: This must be called while gdb is *not* running.
5060 gdb_caching_proc gdb_skip_xml_test {
5065 if { [info exists gdb_spawn_id] } {
5066 error "GDB must not be running in gdb_skip_xml_tests."
5069 set xml_file [gdb_remote_download host "${srcdir}/gdb.xml/trivial.xml"]
5073 gdb_test_multiple "set tdesc filename $xml_file" "" {
5074 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
5077 -re ".*$gdb_prompt $" { }
5083 # Return true if argv[0] is available.
5085 gdb_caching_proc gdb_has_argv0 {
5088 # Set up, compile, and execute a test program to check whether
5089 # argv[0] is available.
5090 set src [standard_temp_file has_argv0[pid].c]
5091 set exe [standard_temp_file has_argv0[pid].x]
5093 gdb_produce_source $src {
5094 int main (int argc, char **argv) {
5099 gdb_compile $src $exe executable {debug}
5102 proc gdb_has_argv0_1 { exe } {
5103 global srcdir subdir
5104 global gdb_prompt hex
5108 gdb_reinitialize_dir $srcdir/$subdir
5111 # Set breakpoint on main.
5112 gdb_test_multiple "break main" "break main" {
5113 -re "Breakpoint.*${gdb_prompt} $" {
5115 -re "${gdb_prompt} $" {
5122 gdb_test_multiple "" "run to main" {
5123 -re "Breakpoint.*${gdb_prompt} $" {
5125 -re "${gdb_prompt} $" {
5130 set old_elements "200"
5131 set test "show print elements"
5132 gdb_test_multiple $test $test {
5133 -re "Limit on string chars or array elements to print is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
5134 set old_elements $expect_out(1,string)
5137 set old_repeats "200"
5138 set test "show print repeats"
5139 gdb_test_multiple $test $test {
5140 -re "Threshold for repeated print elements is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
5141 set old_repeats $expect_out(1,string)
5144 gdb_test_no_output "set print elements unlimited" ""
5145 gdb_test_no_output "set print repeats unlimited" ""
5148 # Check whether argc is 1.
5149 gdb_test_multiple "p argc" "p argc" {
5150 -re " = 1\r\n${gdb_prompt} $" {
5152 gdb_test_multiple "p argv\[0\]" "p argv\[0\]" {
5153 -re " = $hex \".*[file tail $exe]\"\r\n${gdb_prompt} $" {
5156 -re "${gdb_prompt} $" {
5160 -re "${gdb_prompt} $" {
5164 gdb_test_no_output "set print elements $old_elements" ""
5165 gdb_test_no_output "set print repeats $old_repeats" ""
5170 set result [gdb_has_argv0_1 $exe]
5177 && ([istarget *-*-linux*]
5178 || [istarget *-*-freebsd*] || [istarget *-*-kfreebsd*]
5179 || [istarget *-*-netbsd*] || [istarget *-*-knetbsd*]
5180 || [istarget *-*-openbsd*]
5181 || [istarget *-*-darwin*]
5182 || [istarget *-*-solaris*]
5183 || [istarget *-*-aix*]
5184 || [istarget *-*-gnu*]
5185 || [istarget *-*-cygwin*] || [istarget *-*-mingw32*]
5186 || [istarget *-*-*djgpp*] || [istarget *-*-go32*]
5187 || [istarget *-wince-pe] || [istarget *-*-mingw32ce*]
5188 || [istarget *-*-symbianelf*]
5189 || [istarget *-*-osf*]
5190 || [istarget *-*-dicos*]
5191 || [istarget *-*-nto*]
5192 || [istarget *-*-*vms*]
5193 || [istarget *-*-lynx*178]) } {
5194 fail "argv\[0\] should be available on this target"
5200 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
5201 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
5202 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
5203 # the name of a debuginfo only file. This file will be stored in the same
5206 # Functions for separate debug info testing
5208 # starting with an executable:
5209 # foo --> original executable
5211 # at the end of the process we have:
5212 # foo.stripped --> foo w/o debug info
5213 # foo.debug --> foo's debug info
5214 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
5216 # Fetch the build id from the file.
5217 # Returns "" if there is none.
5219 proc get_build_id { filename } {
5220 if { ([istarget "*-*-mingw*"]
5221 || [istarget *-*-cygwin*]) } {
5222 set objdump_program [gdb_find_objdump]
5223 set result [catch {set data [exec $objdump_program -p $filename | grep signature | cut "-d " -f4]} output]
5224 verbose "result is $result"
5225 verbose "output is $output"
5231 set tmp [standard_output_file "${filename}-tmp"]
5232 set objcopy_program [gdb_find_objcopy]
5233 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $filename $tmp" output]
5234 verbose "result is $result"
5235 verbose "output is $output"
5240 fconfigure $fi -translation binary
5241 # Skip the NOTE header.
5246 if ![string compare $data ""] then {
5249 # Convert it to hex.
5250 binary scan $data H* data
5255 # Return the build-id hex string (usually 160 bits as 40 hex characters)
5256 # converted to the form: .build-id/ab/cdef1234...89.debug
5257 # Return "" if no build-id found.
5258 proc build_id_debug_filename_get { filename } {
5259 set data [get_build_id $filename]
5260 if { $data == "" } {
5263 regsub {^..} $data {\0/} data
5264 return ".build-id/${data}.debug"
5267 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
5268 # list of optional flags. The only currently supported flag is no-main,
5269 # which removes the symbol entry for main from the separate debug file.
5271 # Function returns zero on success. Function will return non-zero failure code
5272 # on some targets not supporting separate debug info (such as i386-msdos).
5274 proc gdb_gnu_strip_debug { dest args } {
5276 # Use the first separate debug info file location searched by GDB so the
5277 # run cannot be broken by some stale file searched with higher precedence.
5278 set debug_file "${dest}.debug"
5280 set strip_to_file_program [transform strip]
5281 set objcopy_program [gdb_find_objcopy]
5283 set debug_link [file tail $debug_file]
5284 set stripped_file "${dest}.stripped"
5286 # Get rid of the debug info, and store result in stripped_file
5287 # something like gdb/testsuite/gdb.base/blah.stripped.
5288 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
5289 verbose "result is $result"
5290 verbose "output is $output"
5295 # Workaround PR binutils/10802:
5296 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
5297 set perm [file attributes ${dest} -permissions]
5298 file attributes ${stripped_file} -permissions $perm
5300 # Get rid of everything but the debug info, and store result in debug_file
5301 # This will be in the .debug subdirectory, see above.
5302 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
5303 verbose "result is $result"
5304 verbose "output is $output"
5309 # If no-main is passed, strip the symbol for main from the separate
5310 # file. This is to simulate the behavior of elfutils's eu-strip, which
5311 # leaves the symtab in the original file only. There's no way to get
5312 # objcopy or strip to remove the symbol table without also removing the
5313 # debugging sections, so this is as close as we can get.
5314 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
5315 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
5316 verbose "result is $result"
5317 verbose "output is $output"
5321 file delete "${debug_file}"
5322 file rename "${debug_file}-tmp" "${debug_file}"
5325 # Link the two previous output files together, adding the .gnu_debuglink
5326 # section to the stripped_file, containing a pointer to the debug_file,
5327 # save the new file in dest.
5328 # This will be the regular executable filename, in the usual location.
5329 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
5330 verbose "result is $result"
5331 verbose "output is $output"
5336 # Workaround PR binutils/10802:
5337 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
5338 set perm [file attributes ${stripped_file} -permissions]
5339 file attributes ${dest} -permissions $perm
5344 # Test the output of GDB_COMMAND matches the pattern obtained
5345 # by concatenating all elements of EXPECTED_LINES. This makes
5346 # it possible to split otherwise very long string into pieces.
5347 # If third argument is not empty, it's used as the name of the
5348 # test to be printed on pass/fail.
5349 proc help_test_raw { gdb_command expected_lines args } {
5350 set message $gdb_command
5351 if [llength $args]>0 then {
5352 set message [lindex $args 0]
5354 set expected_output [join $expected_lines ""]
5355 gdb_test "${gdb_command}" "${expected_output}" $message
5358 # Test the output of "help COMMAND_CLASS". EXPECTED_INITIAL_LINES
5359 # are regular expressions that should match the beginning of output,
5360 # before the list of commands in that class. The presence of
5361 # command list and standard epilogue will be tested automatically.
5362 # Notice that the '[' and ']' characters don't need to be escaped for strings
5363 # wrapped in {} braces.
5364 proc test_class_help { command_class expected_initial_lines args } {
5366 "List of commands\:.*[\r\n]+"
5367 "Type \"help\" followed by command name for full documentation\.[\r\n]+"
5368 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n]+"
5369 "Command name abbreviations are allowed if unambiguous\."
5371 set l_entire_body [concat $expected_initial_lines $l_stock_body]
5373 eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
5376 # COMMAND_LIST should have either one element -- command to test, or
5377 # two elements -- abbreviated command to test, and full command the first
5378 # element is abbreviation of.
5379 # The command must be a prefix command. EXPECTED_INITIAL_LINES
5380 # are regular expressions that should match the beginning of output,
5381 # before the list of subcommands. The presence of
5382 # subcommand list and standard epilogue will be tested automatically.
5383 proc test_prefix_command_help { command_list expected_initial_lines args } {
5384 set command [lindex $command_list 0]
5385 if {[llength $command_list]>1} {
5386 set full_command [lindex $command_list 1]
5388 set full_command $command
5390 # Use 'list' and not just {} because we want variables to
5391 # be expanded in this list.
5392 set l_stock_body [list\
5393 "List of $full_command subcommands\:.*\[\r\n\]+"\
5394 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
5395 "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
5396 "Command name abbreviations are allowed if unambiguous\."]
5397 set l_entire_body [concat $expected_initial_lines $l_stock_body]
5398 if {[llength $args]>0} {
5399 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
5401 help_test_raw "help ${command}" $l_entire_body
5405 # Build executable named EXECUTABLE from specifications that allow
5406 # different options to be passed to different sub-compilations.
5407 # TESTNAME is the name of the test; this is passed to 'untested' if
5409 # OPTIONS is passed to the final link, using gdb_compile. If OPTIONS
5410 # contains the option "pthreads", then gdb_compile_pthreads is used.
5411 # ARGS is a flat list of source specifications, of the form:
5412 # { SOURCE1 OPTIONS1 [ SOURCE2 OPTIONS2 ]... }
5413 # Each SOURCE is compiled to an object file using its OPTIONS,
5414 # using gdb_compile.
5415 # Returns 0 on success, -1 on failure.
5416 proc build_executable_from_specs {testname executable options args} {
5420 set binfile [standard_output_file $executable]
5423 if { [lsearch -exact $options "c++"] >= 0 } {
5424 set info_options "c++"
5426 if [get_compiler_info ${info_options}] {
5430 set func gdb_compile
5431 set func_index [lsearch -regexp $options {^(pthreads|shlib|shlib_pthreads)$}]
5432 if {$func_index != -1} {
5433 set func "${func}_[lindex $options $func_index]"
5436 # gdb_compile_shlib and gdb_compile_shlib_pthreads do not use the 3rd
5437 # parameter. They also requires $sources while gdb_compile and
5438 # gdb_compile_pthreads require $objects. Moreover they ignore any options.
5439 if [string match gdb_compile_shlib* $func] {
5441 foreach {s local_options} $args {
5442 if { [regexp "^/" "$s"] } then {
5443 lappend sources_path "$s"
5445 lappend sources_path "$srcdir/$subdir/$s"
5448 set ret [$func $sources_path "${binfile}" $options]
5449 } elseif {[lsearch -exact $options rust] != -1} {
5451 foreach {s local_options} $args {
5452 if { [regexp "^/" "$s"] } then {
5453 lappend sources_path "$s"
5455 lappend sources_path "$srcdir/$subdir/$s"
5458 set ret [gdb_compile_rust $sources_path "${binfile}" $options]
5462 foreach {s local_options} $args {
5463 if { ! [regexp "^/" "$s"] } then {
5464 set s "$srcdir/$subdir/$s"
5466 if { [gdb_compile "${s}" "${binfile}${i}.o" object $local_options] != "" } {
5470 lappend objects "${binfile}${i}.o"
5473 set ret [$func $objects "${binfile}" executable $options]
5483 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
5484 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
5485 # to pass to untested, if something is wrong. OPTIONS are passed
5486 # to gdb_compile directly.
5487 proc build_executable { testname executable {sources ""} {options {debug}} } {
5488 if {[llength $sources]==0} {
5489 set sources ${executable}.c
5492 set arglist [list $testname $executable $options]
5493 foreach source $sources {
5494 lappend arglist $source $options
5497 return [eval build_executable_from_specs $arglist]
5500 # Starts fresh GDB binary and loads an optional executable into GDB.
5501 # Usage: clean_restart [executable]
5502 # EXECUTABLE is the basename of the binary.
5504 proc clean_restart { args } {
5508 if { [llength $args] > 1 } {
5509 error "bad number of args: [llength $args]"
5514 gdb_reinitialize_dir $srcdir/$subdir
5516 if { [llength $args] >= 1 } {
5517 set executable [lindex $args 0]
5518 set binfile [standard_output_file ${executable}]
5523 # Prepares for testing by calling build_executable_full, then
5525 # TESTNAME is the name of the test.
5526 # Each element in ARGS is a list of the form
5527 # { EXECUTABLE OPTIONS SOURCE_SPEC... }
5528 # These are passed to build_executable_from_specs, which see.
5529 # The last EXECUTABLE is passed to clean_restart.
5530 # Returns 0 on success, non-zero on failure.
5531 proc prepare_for_testing_full {testname args} {
5532 foreach spec $args {
5533 if {[eval build_executable_from_specs [list $testname] $spec] == -1} {
5536 set executable [lindex $spec 0]
5538 clean_restart $executable
5542 # Prepares for testing, by calling build_executable, and then clean_restart.
5543 # Please refer to build_executable for parameter description.
5544 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
5546 if {[build_executable $testname $executable $sources $options] == -1} {
5549 clean_restart $executable
5554 # Retrieve the value of EXP in the inferior, represented in format
5555 # specified in FMT (using "printFMT"). DEFAULT is used as fallback if
5556 # print fails. TEST is the test message to use. It can be omitted,
5557 # in which case a test message is built from EXP.
5559 proc get_valueof { fmt exp default {test ""} } {
5563 set test "get valueof \"${exp}\""
5567 gdb_test_multiple "print${fmt} ${exp}" "$test" {
5568 -re "\\$\[0-9\]* = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
5569 set val $expect_out(1,string)
5573 fail "$test (timeout)"
5579 # Retrieve the value of EXP in the inferior, as a signed decimal value
5580 # (using "print /d"). DEFAULT is used as fallback if print fails.
5581 # TEST is the test message to use. It can be omitted, in which case
5582 # a test message is built from EXP.
5584 proc get_integer_valueof { exp default {test ""} } {
5588 set test "get integer valueof \"${exp}\""
5592 gdb_test_multiple "print /d ${exp}" "$test" {
5593 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
5594 set val $expect_out(1,string)
5598 fail "$test (timeout)"
5604 # Retrieve the value of EXP in the inferior, as an hexadecimal value
5605 # (using "print /x"). DEFAULT is used as fallback if print fails.
5606 # TEST is the test message to use. It can be omitted, in which case
5607 # a test message is built from EXP.
5609 proc get_hexadecimal_valueof { exp default {test ""} } {
5613 set test "get hexadecimal valueof \"${exp}\""
5617 gdb_test_multiple "print /x ${exp}" $test {
5618 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
5619 set val $expect_out(1,string)
5626 # Retrieve the size of TYPE in the inferior, as a decimal value. DEFAULT
5627 # is used as fallback if print fails. TEST is the test message to use.
5628 # It can be omitted, in which case a test message is 'sizeof (TYPE)'.
5630 proc get_sizeof { type default {test ""} } {
5631 return [get_integer_valueof "sizeof (${type})" $default $test]
5634 proc get_target_charset { } {
5637 gdb_test_multiple "show target-charset" "" {
5638 -re "The target character set is \"auto; currently (\[^\"\]*)\".*$gdb_prompt $" {
5639 return $expect_out(1,string)
5641 -re "The target character set is \"(\[^\"\]*)\".*$gdb_prompt $" {
5642 return $expect_out(1,string)
5646 # Pick a reasonable default.
5647 warning "Unable to read target-charset."
5651 # Get the address of VAR.
5653 proc get_var_address { var } {
5654 global gdb_prompt hex
5656 # Match output like:
5658 # $5 = (int (*)()) 0
5659 # $6 = (int (*)()) 0x24 <function_bar>
5661 gdb_test_multiple "print &${var}" "get address of ${var}" {
5662 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $"
5664 pass "get address of ${var}"
5665 if { $expect_out(1,string) == "0" } {
5668 return $expect_out(1,string)
5675 # Get the current value for remotetimeout and return it.
5676 proc get_remotetimeout { } {
5680 gdb_test_multiple "show remotetimeout" "" {
5681 -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" {
5682 return $expect_out(1,string)
5686 # Pick the default that gdb uses
5687 warning "Unable to read remotetimeout"
5691 # Set the remotetimeout to the specified timeout. Nothing is returned.
5692 proc set_remotetimeout { timeout } {
5695 gdb_test_multiple "set remotetimeout $timeout" "" {
5696 -re "$gdb_prompt $" {
5697 verbose "Set remotetimeout to $timeout\n"
5702 # Get the target's current endianness and return it.
5703 proc get_endianness { } {
5706 gdb_test_multiple "show endian" "determine endianness" {
5707 -re ".* (little|big) endian.*\r\n$gdb_prompt $" {
5709 return $expect_out(1,string)
5715 # ROOT and FULL are file names. Returns the relative path from ROOT
5716 # to FULL. Note that FULL must be in a subdirectory of ROOT.
5717 # For example, given ROOT = /usr/bin and FULL = /usr/bin/ls, this
5720 proc relative_filename {root full} {
5721 set root_split [file split $root]
5722 set full_split [file split $full]
5724 set len [llength $root_split]
5726 if {[eval file join $root_split]
5727 != [eval file join [lrange $full_split 0 [expr {$len - 1}]]]} {
5728 error "$full not a subdir of $root"
5731 return [eval file join [lrange $full_split $len end]]
5734 # Log gdb command line and script if requested.
5735 if {[info exists TRANSCRIPT]} {
5736 rename send_gdb real_send_gdb
5737 rename remote_spawn real_remote_spawn
5738 rename remote_close real_remote_close
5740 global gdb_transcript
5741 set gdb_transcript ""
5743 global gdb_trans_count
5744 set gdb_trans_count 1
5746 proc remote_spawn {args} {
5747 global gdb_transcript gdb_trans_count outdir
5749 if {$gdb_transcript != ""} {
5750 close $gdb_transcript
5752 set gdb_transcript [open [file join $outdir transcript.$gdb_trans_count] w]
5753 puts $gdb_transcript [lindex $args 1]
5754 incr gdb_trans_count
5756 return [uplevel real_remote_spawn $args]
5759 proc remote_close {args} {
5760 global gdb_transcript
5762 if {$gdb_transcript != ""} {
5763 close $gdb_transcript
5764 set gdb_transcript ""
5767 return [uplevel real_remote_close $args]
5770 proc send_gdb {args} {
5771 global gdb_transcript
5773 if {$gdb_transcript != ""} {
5774 puts -nonewline $gdb_transcript [lindex $args 0]
5777 return [uplevel real_send_gdb $args]
5781 # If GDB_PARALLEL exists, then set up the parallel-mode directories.
5782 if {[info exists GDB_PARALLEL]} {
5783 if {[is_remote host]} {
5787 [make_gdb_parallel_path outputs] \
5788 [make_gdb_parallel_path temp] \
5789 [make_gdb_parallel_path cache]
5793 proc core_find {binfile {deletefiles {}} {arg ""}} {
5794 global objdir subdir
5796 set destcore "$binfile.core"
5797 file delete $destcore
5799 # Create a core file named "$destcore" rather than just "core", to
5800 # avoid problems with sys admin types that like to regularly prune all
5801 # files named "core" from the system.
5803 # Arbitrarily try setting the core size limit to "unlimited" since
5804 # this does not hurt on systems where the command does not work and
5805 # allows us to generate a core on systems where it does.
5807 # Some systems append "core" to the name of the program; others append
5808 # the name of the program to "core"; still others (like Linux, as of
5809 # May 2003) create cores named "core.PID". In the latter case, we
5810 # could have many core files lying around, and it may be difficult to
5811 # tell which one is ours, so let's run the program in a subdirectory.
5813 set coredir [standard_output_file coredir.[getpid]]
5815 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
5816 # remote_exec host "${binfile}"
5817 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
5818 if [remote_file build exists $i] {
5819 remote_exec build "mv $i $destcore"
5823 # Check for "core.PID".
5824 if { $found == 0 } {
5825 set names [glob -nocomplain -directory $coredir core.*]
5826 if {[llength $names] == 1} {
5827 set corefile [file join $coredir [lindex $names 0]]
5828 remote_exec build "mv $corefile $destcore"
5832 if { $found == 0 } {
5833 # The braindamaged HPUX shell quits after the ulimit -c above
5834 # without executing ${binfile}. So we try again without the
5835 # ulimit here if we didn't find a core file above.
5836 # Oh, I should mention that any "braindamaged" non-Unix system has
5837 # the same problem. I like the cd bit too, it's really neat'n stuff.
5838 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
5839 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
5840 if [remote_file build exists $i] {
5841 remote_exec build "mv $i $destcore"
5847 # Try to clean up after ourselves.
5848 foreach deletefile $deletefiles {
5849 remote_file build delete [file join $coredir $deletefile]
5851 remote_exec build "rmdir $coredir"
5853 if { $found == 0 } {
5854 warning "can't generate a core file - core tests suppressed - check ulimit -c"
5860 # gdb_target_symbol_prefix compiles a test program and then examines
5861 # the output from objdump to determine the prefix (such as underscore)
5862 # for linker symbol prefixes.
5864 gdb_caching_proc gdb_target_symbol_prefix {
5865 # Set up and compile a simple test program...
5866 set src [standard_temp_file main[pid].c]
5867 set exe [standard_temp_file main[pid].x]
5869 gdb_produce_source $src {
5875 verbose "compiling testfile $src" 2
5876 set compile_flags {debug nowarnings quiet}
5877 set lines [gdb_compile $src $exe executable $compile_flags]
5881 if ![string match "" $lines] then {
5882 verbose "gdb_target_symbol_prefix: testfile compilation failed, returning null prefix" 2
5884 set objdump_program [gdb_find_objdump]
5885 set result [catch "exec $objdump_program --syms $exe" output]
5888 && ![regexp -lineanchor \
5889 { ([^ a-zA-Z0-9]*)main$} $output dummy prefix] } {
5890 verbose "gdb_target_symbol_prefix: Could not find main in objdump output; returning null prefix" 2
5900 # gdb_target_symbol returns the provided symbol with the correct prefix
5901 # prepended. (See gdb_target_symbol_prefix, above.)
5903 proc gdb_target_symbol { symbol } {
5904 set prefix [gdb_target_symbol_prefix]
5905 return "${prefix}${symbol}"
5908 # gdb_target_symbol_prefix_flags_asm returns a string that can be
5909 # added to gdb_compile options to define the C-preprocessor macro
5910 # SYMBOL_PREFIX with a value that can be prepended to symbols
5911 # for targets which require a prefix, such as underscore.
5913 # This version (_asm) defines the prefix without double quotes
5914 # surrounding the prefix. It is used to define the macro
5915 # SYMBOL_PREFIX for assembly language files. Another version, below,
5916 # is used for symbols in inline assembler in C/C++ files.
5918 # The lack of quotes in this version (_asm) makes it possible to
5919 # define supporting macros in the .S file. (The version which
5920 # uses quotes for the prefix won't work for such files since it's
5921 # impossible to define a quote-stripping macro in C.)
5923 # It's possible to use this version (_asm) for C/C++ source files too,
5924 # but a string is usually required in such files; providing a version
5925 # (no _asm) which encloses the prefix with double quotes makes it
5926 # somewhat easier to define the supporting macros in the test case.
5928 proc gdb_target_symbol_prefix_flags_asm {} {
5929 set prefix [gdb_target_symbol_prefix]
5930 if {$prefix ne ""} {
5931 return "additional_flags=-DSYMBOL_PREFIX=$prefix"
5937 # gdb_target_symbol_prefix_flags returns the same string as
5938 # gdb_target_symbol_prefix_flags_asm, above, but with the prefix
5939 # enclosed in double quotes if there is a prefix.
5941 # See the comment for gdb_target_symbol_prefix_flags_asm for an
5942 # extended discussion.
5944 proc gdb_target_symbol_prefix_flags {} {
5945 set prefix [gdb_target_symbol_prefix]
5946 if {$prefix ne ""} {
5947 return "additional_flags=-DSYMBOL_PREFIX=\"$prefix\""
5953 # A wrapper for 'remote_exec host' that passes or fails a test.
5954 # Returns 0 if all went well, nonzero on failure.
5955 # TEST is the name of the test, other arguments are as for remote_exec.
5957 proc run_on_host { test program args } {
5958 verbose -log "run_on_host: $program $args"
5959 # remote_exec doesn't work properly if the output is set but the
5960 # input is the empty string -- so replace an empty input with
5962 if {[llength $args] > 1 && [lindex $args 1] == ""} {
5963 set args [lreplace $args 1 1 "/dev/null"]
5965 set result [eval remote_exec host [list $program] $args]
5966 verbose "result is $result"
5967 set status [lindex $result 0]
5968 set output [lindex $result 1]
5973 verbose -log "run_on_host failed: $output"
5979 # Return non-zero if "board_info debug_flags" mentions Fission.
5980 # http://gcc.gnu.org/wiki/DebugFission
5981 # Fission doesn't support everything yet.
5982 # This supports working around bug 15954.
5984 proc using_fission { } {
5985 set debug_flags [board_info [target_info name] debug_flags]
5986 return [regexp -- "-gsplit-dwarf" $debug_flags]
5989 # Search the caller's ARGS list and set variables according to the list of
5990 # valid options described by ARGSET.
5992 # The first member of each one- or two-element list in ARGSET defines the
5993 # name of a variable that will be added to the caller's scope.
5995 # If only one element is given to describe an option, it the value is
5996 # 0 if the option is not present in (the caller's) ARGS or 1 if
5999 # If two elements are given, the second element is the default value of
6000 # the variable. This is then overwritten if the option exists in ARGS.
6002 # Any parse_args elements in (the caller's) ARGS will be removed, leaving
6003 # any optional components.
6006 # proc myproc {foo args} {
6007 # parse_args {{bar} {baz "abc"} {qux}}
6010 # myproc ABC -bar -baz DEF peanut butter
6011 # will define the following variables in myproc:
6012 # foo (=ABC), bar (=1), baz (=DEF), and qux (=0)
6013 # args will be the list {peanut butter}
6015 proc parse_args { argset } {
6018 foreach argument $argset {
6019 if {[llength $argument] == 1} {
6020 # No default specified, so we assume that we should set
6021 # the value to 1 if the arg is present and 0 if it's not.
6022 # It is assumed that no value is given with the argument.
6023 set result [lsearch -exact $args "-$argument"]
6024 if {$result != -1} then {
6025 uplevel 1 [list set $argument 1]
6026 set args [lreplace $args $result $result]
6028 uplevel 1 [list set $argument 0]
6030 } elseif {[llength $argument] == 2} {
6031 # There are two items in the argument. The second is a
6032 # default value to use if the item is not present.
6033 # Otherwise, the variable is set to whatever is provided
6034 # after the item in the args.
6035 set arg [lindex $argument 0]
6036 set result [lsearch -exact $args "-[lindex $arg 0]"]
6037 if {$result != -1} then {
6038 uplevel 1 [list set $arg [lindex $args [expr $result+1]]]
6039 set args [lreplace $args $result [expr $result+1]]
6041 uplevel 1 [list set $arg [lindex $argument 1]]
6044 error "Badly formatted argument \"$argument\" in argument set"
6048 # The remaining args should be checked to see that they match the
6049 # number of items expected to be passed into the procedure...
6052 # Capture the output of COMMAND in a string ignoring PREFIX (a regexp);
6053 # return that string.
6055 proc capture_command_output { command prefix } {
6059 set output_string ""
6060 gdb_test_multiple "$command" "capture_command_output for $command" {
6061 -re "[string_to_regexp ${command}]\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $" {
6062 set output_string $expect_out(1,string)
6065 return $output_string
6068 # A convenience function that joins all the arguments together, with a
6069 # regexp that matches exactly one end of line in between each argument.
6070 # This function is ideal to write the expected output of a GDB command
6071 # that generates more than a couple of lines, as this allows us to write
6072 # each line as a separate string, which is easier to read by a human
6075 proc multi_line { args } {
6076 return [join $args "\r\n"]
6079 # Similar to the above, but while multi_line is meant to be used to
6080 # match GDB output, this one is meant to be used to build strings to
6081 # send as GDB input.
6083 proc multi_line_input { args } {
6084 return [join $args "\n"]
6087 # Return the version of the DejaGnu framework.
6089 # The return value is a list containing the major, minor and patch version
6090 # numbers. If the version does not contain a minor or patch number, they will
6091 # be set to 0. For example:
6097 proc dejagnu_version { } {
6098 # The frame_version variable is defined by DejaGnu, in runtest.exp.
6099 global frame_version
6101 verbose -log "DejaGnu version: $frame_version"
6102 verbose -log "Expect version: [exp_version]"
6103 verbose -log "Tcl version: [info tclversion]"
6105 set dg_ver [split $frame_version .]
6107 while { [llength $dg_ver] < 3 } {
6114 # Define user-defined command COMMAND using the COMMAND_LIST as the
6115 # command's definition. The terminating "end" is added automatically.
6117 proc gdb_define_cmd {command command_list} {
6120 set input [multi_line_input {*}$command_list "end"]
6121 set test "define $command"
6123 gdb_test_multiple "define $command" $test {
6125 gdb_test_multiple $input $test {
6126 -re "\r\n$gdb_prompt " {
6133 # Always load compatibility stuff.