1 # Copyright 1992-2020 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"
28 # List of procs to run in gdb_finish.
29 set gdb_finish_hooks [list]
31 # Variable in which we keep track of globals that are allowed to be live
33 array set gdb_persistent_globals {}
35 # Mark variable names in ARG as a persistent global, and declare them as
36 # global in the calling context. Can be used to rewrite "global var_a var_b"
37 # into "gdb_persistent_global var_a var_b".
38 proc gdb_persistent_global { args } {
39 global gdb_persistent_globals
40 foreach varname $args {
41 uplevel 1 global $varname
42 set gdb_persistent_globals($varname) 1
46 # Mark variable names in ARG as a persistent global.
47 proc gdb_persistent_global_no_decl { args } {
48 global gdb_persistent_globals
49 foreach varname $args {
50 set gdb_persistent_globals($varname) 1
54 # Override proc load_lib.
55 rename load_lib saved_load_lib
56 # Run the runtest version of load_lib, and mark all variables that were
57 # created by this call as persistent.
58 proc load_lib { file } {
59 array set known_global {}
60 foreach varname [info globals] {
61 set known_globals($varname) 1
64 set code [catch "saved_load_lib $file" result]
66 foreach varname [info globals] {
67 if { ![info exists known_globals($varname)] } {
68 gdb_persistent_global_no_decl $varname
73 global errorInfo errorCode
74 return -code error -errorinfo $errorInfo -errorcode $errorCode $result
75 } elseif {$code > 1} {
76 return -code $code $result
84 load_lib gdb-utils.exp
86 load_lib check-test-names.exp
90 # The spawn ID used for I/O interaction with the inferior. For native
91 # targets, or remote targets that can do I/O through GDB
92 # (semi-hosting) this will be the same as the host/GDB's spawn ID.
93 # Otherwise, the board may set this to some other spawn ID. E.g.,
94 # when debugging with GDBserver, this is set to GDBserver's spawn ID,
95 # so input/output is done on gdbserver's tty.
96 global inferior_spawn_id
98 if [info exists TOOL_EXECUTABLE] {
99 set GDB $TOOL_EXECUTABLE
101 if ![info exists GDB] {
102 if ![is_remote host] {
103 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
105 set GDB [transform gdb]
108 verbose "using GDB = $GDB" 2
110 # GDBFLAGS is available for the user to set on the command line.
111 # E.g. make check RUNTESTFLAGS=GDBFLAGS=mumble
112 # Testcases may use it to add additional flags, but they must:
113 # - append new flags, not overwrite
114 # - restore the original value when done
116 if ![info exists GDBFLAGS] {
119 verbose "using GDBFLAGS = $GDBFLAGS" 2
121 # Make the build data directory available to tests.
122 set BUILD_DATA_DIRECTORY "[pwd]/../data-directory"
124 # INTERNAL_GDBFLAGS contains flags that the testsuite requires.
125 global INTERNAL_GDBFLAGS
126 if ![info exists INTERNAL_GDBFLAGS] {
127 set INTERNAL_GDBFLAGS \
131 "-data-directory $BUILD_DATA_DIRECTORY" \
132 {-iex "set height 0"} \
133 {-iex "set width 0"}]]
136 # The variable gdb_prompt is a regexp which matches the gdb prompt.
137 # Set it if it is not already set. This is also set by default_gdb_init
138 # but it's not clear what removing one of them will break.
139 # See with_gdb_prompt for more details on prompt handling.
141 if ![info exists gdb_prompt] then {
142 set gdb_prompt "\\(gdb\\)"
145 # A regexp that matches the pagination prompt.
146 set pagination_prompt \
147 "--Type <RET> for more, q to quit, c to continue without paging--"
149 # The variable fullname_syntax_POSIX is a regexp which matches a POSIX
150 # absolute path ie. /foo/
151 set fullname_syntax_POSIX {/[^\n]*/}
152 # The variable fullname_syntax_UNC is a regexp which matches a Windows
153 # UNC path ie. \\D\foo\
154 set fullname_syntax_UNC {\\\\[^\\]+\\[^\n]+\\}
155 # The variable fullname_syntax_DOS_CASE is a regexp which matches a
156 # particular DOS case that GDB most likely will output
157 # ie. \foo\, but don't match \\.*\
158 set fullname_syntax_DOS_CASE {\\[^\\][^\n]*\\}
159 # The variable fullname_syntax_DOS is a regexp which matches a DOS path
160 # ie. a:\foo\ && a:foo\
161 set fullname_syntax_DOS {[a-zA-Z]:[^\n]*\\}
162 # The variable fullname_syntax is a regexp which matches what GDB considers
163 # an absolute path. It is currently debatable if the Windows style paths
164 # d:foo and \abc should be considered valid as an absolute path.
165 # Also, the purpse of this regexp is not to recognize a well formed
166 # absolute path, but to say with certainty that a path is absolute.
167 set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
169 # Needed for some tests under Cygwin.
173 if ![info exists env(EXEEXT)] {
176 set EXEEXT $env(EXEEXT)
181 set inferior_exited_re "(?:\\\[Inferior \[0-9\]+ \\(\[^\n\r\]*\\) exited)"
183 # A regular expression that matches a value history number.
185 set valnum_re "\\\$$decimal"
187 ### Only procedures should come after this point.
190 # gdb_version -- extract and print the version number of GDB
192 proc default_gdb_version {} {
194 global INTERNAL_GDBFLAGS GDBFLAGS
198 if {[info exists inotify_pid]} {
199 eval exec kill $inotify_pid
202 set output [remote_exec host "$GDB $INTERNAL_GDBFLAGS --version"]
203 set tmp [lindex $output 1]
205 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
206 if ![is_remote host] {
207 clone_output "[which $GDB] version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
209 clone_output "$GDB on remote host version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
213 proc gdb_version { } {
214 return [default_gdb_version]
218 # gdb_unload -- unload a file if one is loaded
219 # Return 0 on success, -1 on error.
227 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
228 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
229 -re "A program is being debugged already.*Are you sure you want to change the file.*y or n. $" {
230 send_gdb "y\n" answer
233 -re "Discard symbol table from .*y or n.*$" {
234 send_gdb "y\n" answer
237 -re "$gdb_prompt $" {}
239 perror "couldn't unload file in $GDB (timeout)."
246 # Many of the tests depend on setting breakpoints at various places and
247 # running until that breakpoint is reached. At times, we want to start
248 # with a clean-slate with respect to breakpoints, so this utility proc
249 # lets us do this without duplicating this code everywhere.
252 proc delete_breakpoints {} {
255 # we need a larger timeout value here or this thing just confuses
256 # itself. May need a better implementation if possible. - guo
260 set msg "delete all breakpoints in delete_breakpoints"
262 gdb_test_multiple "delete breakpoints" "$msg" {
263 -re "Delete all breakpoints.*y or n.*$" {
264 send_gdb "y\n" answer
267 -re "$gdb_prompt $" {
273 # Confirm with "info breakpoints".
275 set msg "info breakpoints"
276 gdb_test_multiple $msg $msg {
277 -re "No breakpoints or watchpoints..*$gdb_prompt $" {
280 -re "$gdb_prompt $" {
286 perror "breakpoints not deleted"
290 # Returns true iff the target supports using the "run" command.
292 proc target_can_use_run_cmd {} {
293 if [target_info exists use_gdb_stub] {
294 # In this case, when we connect, the inferior is already
303 # Generic run command.
305 # Return 0 if we could start the program, -1 if we could not.
307 # The second pattern below matches up to the first newline *only*.
308 # Using ``.*$'' could swallow up output that we attempt to match
311 # INFERIOR_ARGS is passed as arguments to the start command, so may contain
312 # inferior arguments.
314 # N.B. This function does not wait for gdb to return to the prompt,
315 # that is the caller's responsibility.
317 proc gdb_run_cmd { {inferior_args {}} } {
318 global gdb_prompt use_gdb_stub
320 foreach command [gdb_init_commands] {
321 send_gdb "$command\n"
323 -re "$gdb_prompt $" { }
325 perror "gdb_init_command for target failed"
332 if [target_info exists gdb,do_reload_on_run] {
333 if { [gdb_reload $inferior_args] != 0 } {
336 send_gdb "continue\n"
338 -re "Continu\[^\r\n\]*\[\r\n\]" {}
344 if [target_info exists gdb,start_symbol] {
345 set start [target_info gdb,start_symbol]
349 send_gdb "jump *$start\n"
351 while { $start_attempt } {
352 # Cap (re)start attempts at three to ensure that this loop
353 # always eventually fails. Don't worry about trying to be
354 # clever and not send a command when it has failed.
355 if [expr $start_attempt > 3] {
356 perror "Jump to start() failed (retry count exceeded)"
359 set start_attempt [expr $start_attempt + 1]
361 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
364 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
365 perror "Can't find start symbol to run in gdb_run"
368 -re "No symbol \"start\" in current.*$gdb_prompt $" {
369 send_gdb "jump *_start\n"
371 -re "No symbol.*context.*$gdb_prompt $" {
374 -re "Line.* Jump anyway.*y or n. $" {
375 send_gdb "y\n" answer
377 -re "The program is not being run.*$gdb_prompt $" {
378 if { [gdb_reload $inferior_args] != 0 } {
381 send_gdb "jump *$start\n"
384 perror "Jump to start() failed (timeout)"
393 if [target_info exists gdb,do_reload_on_run] {
394 if { [gdb_reload $inferior_args] != 0 } {
398 send_gdb "run $inferior_args\n"
399 # This doesn't work quite right yet.
400 # Use -notransfer here so that test cases (like chng-sym.exp)
401 # may test for additional start-up messages.
403 -re "The program .* has been started already.*y or n. $" {
404 send_gdb "y\n" answer
407 -notransfer -re "Starting program: \[^\r\n\]*" {}
408 -notransfer -re "$gdb_prompt $" {
409 # There is no more input expected.
416 # Generic start command. Return 0 if we could start the program, -1
419 # INFERIOR_ARGS is passed as arguments to the start command, so may contain
420 # inferior arguments.
422 # N.B. This function does not wait for gdb to return to the prompt,
423 # that is the caller's responsibility.
425 proc gdb_start_cmd { {inferior_args {}} } {
426 global gdb_prompt use_gdb_stub
428 foreach command [gdb_init_commands] {
429 send_gdb "$command\n"
431 -re "$gdb_prompt $" { }
433 perror "gdb_init_command for target failed"
443 send_gdb "start $inferior_args\n"
444 # Use -notransfer here so that test cases (like chng-sym.exp)
445 # may test for additional start-up messages.
447 -re "The program .* has been started already.*y or n. $" {
448 send_gdb "y\n" answer
451 -notransfer -re "Starting program: \[^\r\n\]*" {
458 # Generic starti command. Return 0 if we could start the program, -1
461 # INFERIOR_ARGS is passed as arguments to the starti command, so may contain
462 # inferior arguments.
464 # N.B. This function does not wait for gdb to return to the prompt,
465 # that is the caller's responsibility.
467 proc gdb_starti_cmd { {inferior_args {}} } {
468 global gdb_prompt use_gdb_stub
470 foreach command [gdb_init_commands] {
471 send_gdb "$command\n"
473 -re "$gdb_prompt $" { }
475 perror "gdb_init_command for target failed"
485 send_gdb "starti $inferior_args\n"
487 -re "The program .* has been started already.*y or n. $" {
488 send_gdb "y\n" answer
491 -re "Starting program: \[^\r\n\]*" {
498 # Set a breakpoint at FUNCTION. If there is an additional argument it is
499 # a list of options; the supported options are allow-pending, temporary,
500 # message, no-message and qualified.
501 # The result is 1 for success, 0 for failure.
503 # Note: The handling of message vs no-message is messed up, but it's based
504 # on historical usage. By default this function does not print passes,
506 # no-message: turns off printing of fails (and passes, but they're already off)
507 # message: turns on printing of passes (and fails, but they're already on)
509 proc gdb_breakpoint { function args } {
513 set pending_response n
514 if {[lsearch -exact $args allow-pending] != -1} {
515 set pending_response y
518 set break_command "break"
519 set break_message "Breakpoint"
520 if {[lsearch -exact $args temporary] != -1} {
521 set break_command "tbreak"
522 set break_message "Temporary breakpoint"
525 if {[lsearch -exact $args qualified] != -1} {
526 append break_command " -qualified"
531 set no_message_loc [lsearch -exact $args no-message]
532 set message_loc [lsearch -exact $args message]
533 # The last one to appear in args wins.
534 if { $no_message_loc > $message_loc } {
536 } elseif { $message_loc > $no_message_loc } {
540 set test_name "setting breakpoint at $function"
542 send_gdb "$break_command $function\n"
543 # The first two regexps are what we get with -g, the third is without -g.
545 -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
546 -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
547 -re "$break_message \[0-9\]* at .*$gdb_prompt $" {}
548 -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
549 if {$pending_response == "n"} {
556 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
557 send_gdb "$pending_response\n"
560 -re "A problem internal to GDB has been detected" {
562 fail "$test_name (GDB internal error)"
564 gdb_internal_error_resync
567 -re "$gdb_prompt $" {
574 perror "GDB process no longer exists"
576 set wait_status [wait -i $gdb_spawn_id]
577 verbose -log "GDB process exited with wait status $wait_status"
579 fail "$test_name (eof)"
585 fail "$test_name (timeout)"
596 # Set breakpoint at function and run gdb until it breaks there.
597 # Since this is the only breakpoint that will be set, if it stops
598 # at a breakpoint, we will assume it is the one we want. We can't
599 # just compare to "function" because it might be a fully qualified,
600 # single quoted C++ function specifier.
602 # If there are additional arguments, pass them to gdb_breakpoint.
603 # We recognize no-message/message ourselves.
604 # The default is no-message.
605 # no-message is messed up here, like gdb_breakpoint: to preserve
606 # historical usage fails are always printed by default.
607 # no-message: turns off printing of fails (and passes, but they're already off)
608 # message: turns on printing of passes (and fails, but they're already on)
610 proc runto { function args } {
616 # Default to "no-message".
617 set args "no-message $args"
621 set no_message_loc [lsearch -exact $args no-message]
622 set message_loc [lsearch -exact $args message]
623 # The last one to appear in args wins.
624 if { $no_message_loc > $message_loc } {
626 } elseif { $message_loc > $no_message_loc } {
630 set test_name "running to $function in runto"
632 # We need to use eval here to pass our varargs args to gdb_breakpoint
633 # which is also a varargs function.
634 # But we also have to be careful because $function may have multiple
635 # elements, and we don't want Tcl to move the remaining elements after
636 # the first to $args. That is why $function is wrapped in {}.
637 if ![eval gdb_breakpoint {$function} $args] {
643 # the "at foo.c:36" output we get with -g.
644 # the "in func" output we get without -g.
646 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
652 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
658 -re "The target does not support running in non-stop mode.\r\n$gdb_prompt $" {
660 unsupported "non-stop mode not supported"
664 -re ".*A problem internal to GDB has been detected" {
665 # Always emit a FAIL if we encounter an internal error: internal
666 # errors are never expected.
667 fail "$test_name (GDB internal error)"
668 gdb_internal_error_resync
671 -re "$gdb_prompt $" {
679 fail "$test_name (eof)"
685 fail "$test_name (timeout)"
696 # Ask gdb to run until we hit a breakpoint at main.
698 # N.B. This function deletes all existing breakpoints.
699 # If you don't want that, use gdb_start_cmd.
701 proc runto_main { } {
702 return [runto main no-message qualified]
705 ### Continue, and expect to hit a breakpoint.
706 ### Report a pass or fail, depending on whether it seems to have
707 ### worked. Use NAME as part of the test name; each call to
708 ### continue_to_breakpoint should use a NAME which is unique within
710 proc gdb_continue_to_breakpoint {name {location_pattern .*}} {
712 set full_name "continue to breakpoint: $name"
714 set kfail_pattern "Process record does not support instruction 0xfae64 at.*"
715 gdb_test_multiple "continue" $full_name {
716 -re "(?:Breakpoint|Temporary breakpoint) .* (at|in) $location_pattern\r\n$gdb_prompt $" {
719 -re "\[\r\n\]*(?:$kfail_pattern)\[\r\n\]+$gdb_prompt $" {
720 kfail "gdb/25038" $full_name
726 # gdb_internal_error_resync:
728 # Answer the questions GDB asks after it reports an internal error
729 # until we get back to a GDB prompt. Decline to quit the debugging
730 # session, and decline to create a core file. Return non-zero if the
733 # This procedure just answers whatever questions come up until it sees
734 # a GDB prompt; it doesn't require you to have matched the input up to
735 # any specific point. However, it only answers questions it sees in
736 # the output itself, so if you've matched a question, you had better
737 # answer it yourself before calling this.
739 # You can use this function thus:
743 # -re ".*A problem internal to GDB has been detected" {
744 # gdb_internal_error_resync
749 proc gdb_internal_error_resync {} {
752 verbose -log "Resyncing due to internal error."
755 while {$count < 10} {
757 -re "Quit this debugging session\\? \\(y or n\\) $" {
758 send_gdb "n\n" answer
761 -re "Create a core file of GDB\\? \\(y or n\\) $" {
762 send_gdb "n\n" answer
765 -re "$gdb_prompt $" {
766 # We're resynchronized.
770 perror "Could not resync from internal error (timeout)"
775 perror "Could not resync from internal error (resync count exceeded)"
780 # gdb_test_multiple COMMAND MESSAGE [ -promp PROMPT_REGEXP] [ -lbl ]
782 # Send a command to gdb; test the result.
784 # COMMAND is the command to execute, send to GDB with send_gdb. If
785 # this is the null string no command is sent.
786 # MESSAGE is a message to be printed with the built-in failure patterns
787 # if one of them matches. If MESSAGE is empty COMMAND will be used.
788 # -prompt PROMPT_REGEXP specifies a regexp matching the expected prompt
789 # after the command output. If empty, defaults to "$gdb_prompt $".
790 # -lbl specifies that line-by-line matching will be used.
791 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
792 # patterns. Pattern elements will be evaluated in the caller's
793 # context; action elements will be executed in the caller's context.
794 # Unlike patterns for gdb_test, these patterns should generally include
795 # the final newline and prompt.
798 # 1 if the test failed, according to a built-in failure pattern
799 # 0 if only user-supplied patterns matched
800 # -1 if there was an internal error.
802 # You can use this function thus:
804 # gdb_test_multiple "print foo" "test foo" {
805 # -re "expected output 1" {
808 # -re "expected output 2" {
813 # Within action elements you can also make use of the variable
814 # gdb_test_name. This variable is setup automatically by
815 # gdb_test_multiple, and contains the value of MESSAGE. You can then
816 # write this, which is equivalent to the above:
818 # gdb_test_multiple "print foo" "test foo" {
819 # -re "expected output 1" {
820 # pass $gdb_test_name
822 # -re "expected output 2" {
823 # fail $gdb_test_name
827 # Like with "expect", you can also specify the spawn id to match with
828 # -i "$id". Interesting spawn ids are $inferior_spawn_id and
829 # $gdb_spawn_id. The former matches inferior I/O, while the latter
830 # matches GDB I/O. E.g.:
832 # send_inferior "hello\n"
833 # gdb_test_multiple "continue" "test echo" {
834 # -i "$inferior_spawn_id" -re "^hello\r\nhello\r\n$" {
837 # -i "$gdb_spawn_id" -re "Breakpoint.*$gdb_prompt $" {
838 # fail "hit breakpoint"
842 # The standard patterns, such as "Inferior exited..." and "A problem
843 # ...", all being implicitly appended to that list. These are always
844 # expected from $gdb_spawn_id. IOW, callers do not need to worry
845 # about resetting "-i" back to $gdb_spawn_id explicitly.
847 # In EXPECT_ARGUMENTS we can use a -wrap pattern flag, that wraps the regexp
848 # pattern as gdb_test wraps its message argument.
849 # This allows us to rewrite:
850 # gdb_test <command> <pattern> <message>
852 # gdb_test_multiple <command> <message> {
853 # -re -wrap <pattern> {
854 # pass $gdb_test_name
858 # In EXPECT_ARGUMENTS, a pattern flag -early can be used. It makes sure the
859 # pattern is inserted before any implicit pattern added by gdb_test_multiple.
860 # Using this pattern flag, we can f.i. setup a kfail for an assertion failure
861 # <assert> during gdb_continue_to_breakpoint by the rewrite:
862 # gdb_continue_to_breakpoint <msg> <pattern>
864 # set breakpoint_pattern "(?:Breakpoint|Temporary breakpoint) .* (at|in)"
865 # gdb_test_multiple "continue" "continue to breakpoint: <msg>" {
866 # -early -re "internal-error: <assert>" {
867 # setup_kfail gdb/nnnnn "*-*-*"
870 # -re "$breakpoint_pattern <pattern>\r\n$gdb_prompt $" {
871 # pass $gdb_test_name
875 proc gdb_test_multiple { command message args } {
876 global verbose use_gdb_stub
877 global gdb_prompt pagination_prompt
880 global inferior_exited_re
881 upvar timeout timeout
882 upvar expect_out expect_out
887 for {set i 0} {$i < [llength $args]} {incr i} {
888 set arg [lindex $args $i]
889 if { $arg == "-prompt" } {
891 set prompt_regexp [lindex $args $i]
892 } elseif { $arg == "-lbl" } {
899 if { [expr $i + 1] < [llength $args] } {
900 error "Too many arguments to gdb_test_multiple"
901 } elseif { ![info exists user_code] } {
902 error "Too few arguments to gdb_test_multiple"
905 if { "$prompt_regexp" == "" } {
906 set prompt_regexp "$gdb_prompt $"
909 if { $message == "" } {
913 if [string match "*\[\r\n\]" $command] {
914 error "Invalid trailing newline in \"$message\" test"
917 if [string match "*\[\r\n\]*" $message] {
918 error "Invalid newline in \"$message\" test"
922 && [regexp -nocase {^\s*(r|run|star|start|at|att|atta|attac|attach)\M} \
924 error "gdbserver does not support $command without extended-remote"
927 # TCL/EXPECT WART ALERT
928 # Expect does something very strange when it receives a single braced
929 # argument. It splits it along word separators and performs substitutions.
930 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
931 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
932 # double-quoted list item, "\[ab\]" is just a long way of representing
933 # "[ab]", because the backslashes will be removed by lindex.
935 # Unfortunately, there appears to be no easy way to duplicate the splitting
936 # that expect will do from within TCL. And many places make use of the
937 # "\[0-9\]" construct, so we need to support that; and some places make use
938 # of the "[func]" construct, so we need to support that too. In order to
939 # get this right we have to substitute quoted list elements differently
940 # from braced list elements.
942 # We do this roughly the same way that Expect does it. We have to use two
943 # lists, because if we leave unquoted newlines in the argument to uplevel
944 # they'll be treated as command separators, and if we escape newlines
945 # we mangle newlines inside of command blocks. This assumes that the
946 # input doesn't contain a pattern which contains actual embedded newlines
949 regsub -all {\n} ${user_code} { } subst_code
950 set subst_code [uplevel list $subst_code]
952 set processed_code ""
953 set early_processed_code ""
954 # The variable current_list holds the name of the currently processed
955 # list, either processed_code or early_processed_code.
956 set current_list "processed_code"
958 set expecting_action 0
961 foreach item $user_code subst_item $subst_code {
962 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
963 lappend $current_list $item
966 if { $item == "-indices" || $item == "-re" || $item == "-ex" } {
967 lappend $current_list $item
970 if { $item == "-early" } {
971 set current_list "early_processed_code"
974 if { $item == "-timeout" || $item == "-i" } {
976 lappend $current_list $item
979 if { $item == "-wrap" } {
983 if { $expecting_arg } {
985 lappend $current_list $subst_item
988 if { $expecting_action } {
989 lappend $current_list "uplevel [list $item]"
990 set expecting_action 0
991 # Cosmetic, no effect on the list.
992 append $current_list "\n"
993 # End the effect of -early, it only applies to one action.
994 set current_list "processed_code"
997 set expecting_action 1
998 if { $wrap_pattern } {
999 # Wrap subst_item as is done for the gdb_test PATTERN argument.
1000 lappend $current_list \
1001 "\[\r\n\]*(?:$subst_item)\[\r\n\]+$gdb_prompt $"
1004 lappend $current_list $subst_item
1006 if {$patterns != ""} {
1007 append patterns "; "
1009 append patterns "\"$subst_item\""
1012 # Also purely cosmetic.
1013 regsub -all {\r} $patterns {\\r} patterns
1014 regsub -all {\n} $patterns {\\n} patterns
1016 if $verbose>2 then {
1017 send_user "Sending \"$command\" to gdb\n"
1018 send_user "Looking to match \"$patterns\"\n"
1019 send_user "Message is \"$message\"\n"
1023 set string "${command}\n"
1024 if { $command != "" } {
1025 set multi_line_re "\[\r\n\] *>"
1026 while { "$string" != "" } {
1027 set foo [string first "\n" "$string"]
1028 set len [string length "$string"]
1029 if { $foo < [expr $len - 1] } {
1030 set str [string range "$string" 0 $foo]
1031 if { [send_gdb "$str"] != "" } {
1032 global suppress_flag
1034 if { ! $suppress_flag } {
1035 perror "Couldn't send $command to GDB."
1040 # since we're checking if each line of the multi-line
1041 # command are 'accepted' by GDB here,
1042 # we need to set -notransfer expect option so that
1043 # command output is not lost for pattern matching
1046 -notransfer -re "$multi_line_re$" { verbose "partial: match" 3 }
1047 timeout { verbose "partial: timeout" 3 }
1049 set string [string range "$string" [expr $foo + 1] end]
1050 set multi_line_re "$multi_line_re.*\[\r\n\] *>"
1055 if { "$string" != "" } {
1056 if { [send_gdb "$string"] != "" } {
1057 global suppress_flag
1059 if { ! $suppress_flag } {
1060 perror "Couldn't send $command to GDB."
1068 set code $early_processed_code
1070 -re ".*A problem internal to GDB has been detected" {
1071 fail "$message (GDB internal error)"
1072 gdb_internal_error_resync
1075 -re "\\*\\*\\* DOSEXIT code.*" {
1076 if { $message != "" } {
1079 gdb_suppress_entire_file "GDB died"
1083 append code $processed_code
1085 # Reset the spawn id, in case the processed code used -i.
1091 -re "Ending remote debugging.*$prompt_regexp" {
1092 if ![isnative] then {
1093 warning "Can`t communicate to remote target."
1099 -re "Undefined\[a-z\]* command:.*$prompt_regexp" {
1100 perror "Undefined command \"$command\"."
1104 -re "Ambiguous command.*$prompt_regexp" {
1105 perror "\"$command\" is not a unique command name."
1109 -re "$inferior_exited_re with code \[0-9\]+.*$prompt_regexp" {
1110 if ![string match "" $message] then {
1111 set errmsg "$message (the program exited)"
1113 set errmsg "$command (the program exited)"
1118 -re "$inferior_exited_re normally.*$prompt_regexp" {
1119 if ![string match "" $message] then {
1120 set errmsg "$message (the program exited)"
1122 set errmsg "$command (the program exited)"
1127 -re "The program is not being run.*$prompt_regexp" {
1128 if ![string match "" $message] then {
1129 set errmsg "$message (the program is no longer running)"
1131 set errmsg "$command (the program is no longer running)"
1136 -re "\r\n$prompt_regexp" {
1137 if ![string match "" $message] then {
1142 -re "$pagination_prompt" {
1144 perror "Window too small."
1148 -re "\\((y or n|y or \\\[n\\\]|\\\[y\\\] or n)\\) " {
1149 send_gdb "n\n" answer
1150 gdb_expect -re "$prompt_regexp"
1151 fail "$message (got interactive prompt)"
1154 -re "\\\[0\\\] cancel\r\n\\\[1\\\] all.*\r\n> $" {
1156 gdb_expect -re "$prompt_regexp"
1157 fail "$message (got breakpoint menu)"
1163 perror "GDB process no longer exists"
1164 set wait_status [wait -i $gdb_spawn_id]
1165 verbose -log "GDB process exited with wait status $wait_status"
1166 if { $message != "" } {
1173 if {$line_by_line} {
1175 -re "\r\n\[^\r\n\]*(?=\r\n)" {
1181 # Now patterns that apply to any spawn id specified.
1185 perror "Process no longer exists"
1186 if { $message != "" } {
1192 perror "internal buffer is full."
1197 if ![string match "" $message] then {
1198 fail "$message (timeout)"
1204 # remote_expect calls the eof section if there is an error on the
1205 # expect call. We already have eof sections above, and we don't
1206 # want them to get called in that situation. Since the last eof
1207 # section becomes the error section, here we define another eof
1208 # section, but with an empty spawn_id list, so that it won't ever
1212 # This comment is here because the eof section must not be
1213 # the empty string, otherwise remote_expect won't realize
1218 # Create gdb_test_name in the parent scope. If this variable
1219 # already exists, which it might if we have nested calls to
1220 # gdb_test_multiple, then preserve the old value, otherwise,
1221 # create a new variable in the parent scope.
1222 upvar gdb_test_name gdb_test_name
1223 if { [info exists gdb_test_name] } {
1224 set gdb_test_name_old "$gdb_test_name"
1226 set gdb_test_name "$message"
1229 set code [catch {gdb_expect $code} string]
1231 # Clean up the gdb_test_name variable. If we had a
1232 # previous value then restore it, otherwise, delete the variable
1233 # from the parent scope.
1234 if { [info exists gdb_test_name_old] } {
1235 set gdb_test_name "$gdb_test_name_old"
1241 global errorInfo errorCode
1242 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1243 } elseif {$code > 1} {
1244 return -code $code $string
1249 # Usage: gdb_test_multiline NAME INPUT RESULT {INPUT RESULT} ...
1250 # Run a test named NAME, consisting of multiple lines of input.
1251 # After each input line INPUT, search for result line RESULT.
1252 # Succeed if all results are seen; fail otherwise.
1254 proc gdb_test_multiline { name args } {
1257 foreach {input result} $args {
1259 if {[gdb_test_multiple $input "$name: input $inputnr: $input" {
1260 -re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" {
1271 # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
1272 # Send a command to gdb; test the result.
1274 # COMMAND is the command to execute, send to GDB with send_gdb. If
1275 # this is the null string no command is sent.
1276 # PATTERN is the pattern to match for a PASS, and must NOT include
1277 # the \r\n sequence immediately before the gdb prompt. This argument
1278 # may be omitted to just match the prompt, ignoring whatever output
1280 # MESSAGE is an optional message to be printed. If this is
1281 # omitted, then the pass/fail messages use the command string as the
1282 # message. (If this is the empty string, then sometimes we don't
1283 # call pass or fail at all; I don't understand this at all.)
1284 # QUESTION is a question GDB may ask in response to COMMAND, like
1286 # RESPONSE is the response to send if QUESTION appears.
1289 # 1 if the test failed,
1290 # 0 if the test passes,
1291 # -1 if there was an internal error.
1293 proc gdb_test { args } {
1295 upvar timeout timeout
1297 if [llength $args]>2 then {
1298 set message [lindex $args 2]
1300 set message [lindex $args 0]
1302 set command [lindex $args 0]
1303 set pattern [lindex $args 1]
1307 -re "\[\r\n\]*(?:$pattern)\[\r\n\]+$gdb_prompt $" {
1308 if ![string match "" $message] then {
1314 if { [llength $args] == 5 } {
1315 set question_string [lindex $args 3]
1316 set response_string [lindex $args 4]
1318 -re "(${question_string})$" {
1319 send_gdb "$response_string\n"
1325 set user_code [join $user_code]
1326 return [gdb_test_multiple $command $message $user_code]
1329 # Return 1 if version MAJOR.MINOR is at least AT_LEAST_MAJOR.AT_LEAST_MINOR.
1330 proc version_at_least { major minor at_least_major at_least_minor} {
1331 if { $major > $at_least_major } {
1333 } elseif { $major == $at_least_major \
1334 && $minor >= $at_least_minor } {
1341 # Return 1 if tcl version used is at least MAJOR.MINOR
1342 proc tcl_version_at_least { major minor } {
1344 regexp {^([0-9]+)\.([0-9]+)$} $tcl_version \
1345 dummy tcl_version_major tcl_version_minor
1346 return [version_at_least $tcl_version_major $tcl_version_minor \
1350 if { [tcl_version_at_least 8 5] == 0 } {
1351 # lrepeat was added in tcl 8.5. Only add if missing.
1352 proc lrepeat { n element } {
1353 if { [string is integer -strict $n] == 0 } {
1354 error "expected integer but got \"$n\""
1357 error "bad count \"$n\": must be integer >= 0"
1360 for {set i 0} {$i < $n} {incr i} {
1361 lappend res $element
1367 # gdb_test_no_output COMMAND MESSAGE
1368 # Send a command to GDB and verify that this command generated no output.
1370 # See gdb_test_multiple for a description of the COMMAND and MESSAGE
1371 # parameters. If MESSAGE is ommitted, then COMMAND will be used as
1372 # the message. (If MESSAGE is the empty string, then sometimes we do not
1373 # call pass or fail at all; I don't understand this at all.)
1375 proc gdb_test_no_output { args } {
1377 set command [lindex $args 0]
1378 if [llength $args]>1 then {
1379 set message [lindex $args 1]
1381 set message $command
1384 set command_regex [string_to_regexp $command]
1385 gdb_test_multiple $command $message {
1386 -re "^$command_regex\r\n$gdb_prompt $" {
1387 if ![string match "" $message] then {
1394 # Send a command and then wait for a sequence of outputs.
1395 # This is useful when the sequence is long and contains ".*", a single
1396 # regexp to match the entire output can get a timeout much easier.
1398 # COMMAND is the command to execute, send to GDB with send_gdb. If
1399 # this is the null string no command is sent.
1400 # TEST_NAME is passed to pass/fail. COMMAND is used if TEST_NAME is "".
1401 # EXPECTED_OUTPUT_LIST is a list of regexps of expected output, which are
1402 # processed in order, and all must be present in the output.
1404 # It is unnecessary to specify ".*" at the beginning or end of any regexp,
1405 # there is an implicit ".*" between each element of EXPECTED_OUTPUT_LIST.
1406 # There is also an implicit ".*" between the last regexp and the gdb prompt.
1408 # Like gdb_test and gdb_test_multiple, the output is expected to end with the
1409 # gdb prompt, which must not be specified in EXPECTED_OUTPUT_LIST.
1412 # 1 if the test failed,
1413 # 0 if the test passes,
1414 # -1 if there was an internal error.
1416 proc gdb_test_sequence { command test_name expected_output_list } {
1418 if { $test_name == "" } {
1419 set test_name $command
1421 lappend expected_output_list ""; # implicit ".*" before gdb prompt
1422 if { $command != "" } {
1423 send_gdb "$command\n"
1425 return [gdb_expect_list $test_name "$gdb_prompt $" $expected_output_list]
1429 # Test that a command gives an error. For pass or fail, return
1430 # a 1 to indicate that more tests can proceed. However a timeout
1431 # is a serious error, generates a special fail message, and causes
1432 # a 0 to be returned to indicate that more tests are likely to fail
1435 proc test_print_reject { args } {
1439 if [llength $args]==2 then {
1440 set expectthis [lindex $args 1]
1442 set expectthis "should never match this bogus string"
1444 set sendthis [lindex $args 0]
1445 if $verbose>2 then {
1446 send_user "Sending \"$sendthis\" to gdb\n"
1447 send_user "Looking to match \"$expectthis\"\n"
1449 send_gdb "$sendthis\n"
1450 #FIXME: Should add timeout as parameter.
1452 -re "A .* in expression.*\\.*$gdb_prompt $" {
1453 pass "reject $sendthis"
1456 -re "Invalid syntax in expression.*$gdb_prompt $" {
1457 pass "reject $sendthis"
1460 -re "Junk after end of expression.*$gdb_prompt $" {
1461 pass "reject $sendthis"
1464 -re "Invalid number.*$gdb_prompt $" {
1465 pass "reject $sendthis"
1468 -re "Invalid character constant.*$gdb_prompt $" {
1469 pass "reject $sendthis"
1472 -re "No symbol table is loaded.*$gdb_prompt $" {
1473 pass "reject $sendthis"
1476 -re "No symbol .* in current context.*$gdb_prompt $" {
1477 pass "reject $sendthis"
1480 -re "Unmatched single quote.*$gdb_prompt $" {
1481 pass "reject $sendthis"
1484 -re "A character constant must contain at least one character.*$gdb_prompt $" {
1485 pass "reject $sendthis"
1488 -re "$expectthis.*$gdb_prompt $" {
1489 pass "reject $sendthis"
1492 -re ".*$gdb_prompt $" {
1493 fail "reject $sendthis"
1497 fail "reject $sendthis (eof or timeout)"
1504 # Same as gdb_test, but the second parameter is not a regexp,
1505 # but a string that must match exactly.
1507 proc gdb_test_exact { args } {
1508 upvar timeout timeout
1510 set command [lindex $args 0]
1512 # This applies a special meaning to a null string pattern. Without
1513 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
1514 # messages from commands that should have no output except a new
1515 # prompt. With this, only results of a null string will match a null
1518 set pattern [lindex $args 1]
1519 if [string match $pattern ""] {
1520 set pattern [string_to_regexp [lindex $args 0]]
1522 set pattern [string_to_regexp [lindex $args 1]]
1525 # It is most natural to write the pattern argument with only
1526 # embedded \n's, especially if you are trying to avoid Tcl quoting
1527 # problems. But gdb_expect really wants to see \r\n in patterns. So
1528 # transform the pattern here. First transform \r\n back to \n, in
1529 # case some users of gdb_test_exact already do the right thing.
1530 regsub -all "\r\n" $pattern "\n" pattern
1531 regsub -all "\n" $pattern "\r\n" pattern
1532 if [llength $args]==3 then {
1533 set message [lindex $args 2]
1534 return [gdb_test $command $pattern $message]
1537 return [gdb_test $command $pattern]
1540 # Wrapper around gdb_test_multiple that looks for a list of expected
1541 # output elements, but which can appear in any order.
1542 # CMD is the gdb command.
1543 # NAME is the name of the test.
1544 # ELM_FIND_REGEXP specifies how to partition the output into elements to
1546 # ELM_EXTRACT_REGEXP specifies the part of ELM_FIND_REGEXP to compare.
1547 # RESULT_MATCH_LIST is a list of exact matches for each expected element.
1548 # All elements of RESULT_MATCH_LIST must appear for the test to pass.
1550 # A typical use of ELM_FIND_REGEXP/ELM_EXTRACT_REGEXP is to extract one line
1551 # of text per element and then strip trailing \r\n's.
1553 # gdb_test_list_exact "foo" "bar" \
1554 # "\[^\r\n\]+\[\r\n\]+" \
1557 # {expected result 1} \
1558 # {expected result 2} \
1561 proc gdb_test_list_exact { cmd name elm_find_regexp elm_extract_regexp result_match_list } {
1564 set matches [lsort $result_match_list]
1566 gdb_test_multiple $cmd $name {
1567 "$cmd\[\r\n\]" { exp_continue }
1568 -re $elm_find_regexp {
1569 set str $expect_out(0,string)
1570 verbose -log "seen: $str" 3
1571 regexp -- $elm_extract_regexp $str elm_seen
1572 verbose -log "extracted: $elm_seen" 3
1573 lappend seen $elm_seen
1576 -re "$gdb_prompt $" {
1578 foreach got [lsort $seen] have $matches {
1579 if {![string equal $got $have]} {
1584 if {[string length $failed] != 0} {
1585 fail "$name ($failed not found)"
1593 # gdb_test_stdio COMMAND INFERIOR_PATTERN GDB_PATTERN MESSAGE
1594 # Send a command to gdb; expect inferior and gdb output.
1596 # See gdb_test_multiple for a description of the COMMAND and MESSAGE
1599 # INFERIOR_PATTERN is the pattern to match against inferior output.
1601 # GDB_PATTERN is the pattern to match against gdb output, and must NOT
1602 # include the \r\n sequence immediately before the gdb prompt, nor the
1603 # prompt. The default is empty.
1605 # Both inferior and gdb patterns must match for a PASS.
1607 # If MESSAGE is ommitted, then COMMAND will be used as the message.
1610 # 1 if the test failed,
1611 # 0 if the test passes,
1612 # -1 if there was an internal error.
1615 proc gdb_test_stdio {command inferior_pattern {gdb_pattern ""} {message ""}} {
1616 global inferior_spawn_id gdb_spawn_id
1619 if {$message == ""} {
1620 set message $command
1623 set inferior_matched 0
1626 # Use an indirect spawn id list, and remove the inferior spawn id
1627 # from the expected output as soon as it matches, in case
1628 # $inferior_pattern happens to be a prefix of the resulting full
1629 # gdb pattern below (e.g., "\r\n").
1630 global gdb_test_stdio_spawn_id_list
1631 set gdb_test_stdio_spawn_id_list "$inferior_spawn_id"
1633 # Note that if $inferior_spawn_id and $gdb_spawn_id are different,
1634 # then we may see gdb's output arriving before the inferior's
1636 set res [gdb_test_multiple $command $message {
1637 -i gdb_test_stdio_spawn_id_list -re "$inferior_pattern" {
1638 set inferior_matched 1
1639 if {!$gdb_matched} {
1640 set gdb_test_stdio_spawn_id_list ""
1644 -i $gdb_spawn_id -re "$gdb_pattern\r\n$gdb_prompt $" {
1646 if {!$inferior_matched} {
1654 verbose -log "inferior_matched=$inferior_matched, gdb_matched=$gdb_matched"
1659 # get_print_expr_at_depths EXP OUTPUTS
1661 # Used for testing 'set print max-depth'. Prints the expression EXP
1662 # with 'set print max-depth' set to various depths. OUTPUTS is a list
1663 # of `n` different patterns to match at each of the depths from 0 to
1666 # This proc does one final check with the max-depth set to 'unlimited'
1667 # which is tested against the last pattern in the OUTPUTS list. The
1668 # OUTPUTS list is therefore required to match every depth from 0 to a
1669 # depth where the whole of EXP is printed with no ellipsis.
1671 # This proc leaves the 'set print max-depth' set to 'unlimited'.
1672 proc gdb_print_expr_at_depths {exp outputs} {
1673 for { set depth 0 } { $depth <= [llength $outputs] } { incr depth } {
1674 if { $depth == [llength $outputs] } {
1675 set expected_result [lindex $outputs [expr [llength $outputs] - 1]]
1676 set depth_string "unlimited"
1678 set expected_result [lindex $outputs $depth]
1679 set depth_string $depth
1682 with_test_prefix "exp='$exp': depth=${depth_string}" {
1683 gdb_test_no_output "set print max-depth ${depth_string}"
1684 gdb_test "p $exp" "$expected_result"
1691 # Issue a PASS and return true if evaluating CONDITION in the caller's
1692 # frame returns true, and issue a FAIL and return false otherwise.
1693 # MESSAGE is the pass/fail message to be printed. If MESSAGE is
1694 # omitted or is empty, then the pass/fail messages use the condition
1695 # string as the message.
1697 proc gdb_assert { condition {message ""} } {
1698 if { $message == ""} {
1699 set message $condition
1702 set code [catch {uplevel 1 expr $condition} res]
1703 if {$code != 0 || !$res} {
1711 proc gdb_reinitialize_dir { subdir } {
1714 if [is_remote host] {
1719 -re "Reinitialize source path to empty.*y or n. " {
1720 send_gdb "y\n" answer
1722 -re "Source directories searched.*$gdb_prompt $" {
1723 send_gdb "dir $subdir\n"
1725 -re "Source directories searched.*$gdb_prompt $" {
1726 verbose "Dir set to $subdir"
1728 -re "$gdb_prompt $" {
1729 perror "Dir \"$subdir\" failed."
1733 -re "$gdb_prompt $" {
1734 perror "Dir \"$subdir\" failed."
1738 -re "$gdb_prompt $" {
1739 perror "Dir \"$subdir\" failed."
1745 # gdb_exit -- exit the GDB, killing the target program if necessary
1747 proc default_gdb_exit {} {
1749 global INTERNAL_GDBFLAGS GDBFLAGS
1750 global gdb_spawn_id inferior_spawn_id
1751 global inotify_log_file
1753 gdb_stop_suppressing_tests
1755 if ![info exists gdb_spawn_id] {
1759 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1761 if {[info exists inotify_log_file] && [file exists $inotify_log_file]} {
1762 set fd [open $inotify_log_file]
1763 set data [read -nonewline $fd]
1766 if {[string compare $data ""] != 0} {
1767 warning "parallel-unsafe file creations noticed"
1770 set fd [open $inotify_log_file w]
1775 if { [is_remote host] && [board_info host exists fileid] } {
1779 send_gdb "y\n" answer
1782 -re "DOSEXIT code" { }
1787 if ![is_remote host] {
1791 unset inferior_spawn_id
1794 # Load a file into the debugger.
1795 # The return value is 0 for success, -1 for failure.
1797 # This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1798 # to one of these values:
1800 # debug file was loaded successfully and has debug information
1801 # nodebug file was loaded successfully and has no debug information
1802 # lzma file was loaded, .gnu_debugdata found, but no LZMA support
1804 # fail file was not loaded
1806 # This procedure also set the global variable GDB_FILE_CMD_MSG to the
1807 # output of the file command in case of success.
1809 # I tried returning this information as part of the return value,
1810 # but ran into a mess because of the many re-implementations of
1811 # gdb_load in config/*.exp.
1813 # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
1814 # this if they can get more information set.
1816 proc gdb_file_cmd { arg } {
1819 global last_loaded_file
1821 # Save this for the benefit of gdbserver-support.exp.
1822 set last_loaded_file $arg
1824 # Set whether debug info was found.
1825 # Default to "fail".
1826 global gdb_file_cmd_debug_info gdb_file_cmd_msg
1827 set gdb_file_cmd_debug_info "fail"
1829 if [is_remote host] {
1830 set arg [remote_download host $arg]
1832 perror "download failed"
1837 # The file command used to kill the remote target. For the benefit
1838 # of the testsuite, preserve this behavior. Mark as optional so it doesn't
1839 # get written to the stdin log.
1840 send_gdb "kill\n" optional
1842 -re "Kill the program being debugged. .y or n. $" {
1843 send_gdb "y\n" answer
1844 verbose "\t\tKilling previous program being debugged"
1847 -re "$gdb_prompt $" {
1852 send_gdb "file $arg\n"
1853 set new_symbol_table 0
1854 set basename [file tail $arg]
1856 -re "(Reading symbols from.*LZMA support was disabled.*$gdb_prompt $)" {
1857 verbose "\t\tLoaded $arg into $GDB; .gnu_debugdata found but no LZMA available"
1858 set gdb_file_cmd_msg $expect_out(1,string)
1859 set gdb_file_cmd_debug_info "lzma"
1862 -re "(Reading symbols from.*no debugging symbols found.*$gdb_prompt $)" {
1863 verbose "\t\tLoaded $arg into $GDB with no debugging symbols"
1864 set gdb_file_cmd_msg $expect_out(1,string)
1865 set gdb_file_cmd_debug_info "nodebug"
1868 -re "(Reading symbols from.*$gdb_prompt $)" {
1869 verbose "\t\tLoaded $arg into $GDB"
1870 set gdb_file_cmd_msg $expect_out(1,string)
1871 set gdb_file_cmd_debug_info "debug"
1874 -re "Load new symbol table from \".*\".*y or n. $" {
1875 if { $new_symbol_table > 0 } {
1876 perror [join [list "Couldn't load $basename,"
1877 "interactive prompt loop detected."]]
1880 send_gdb "y\n" answer
1881 incr new_symbol_table
1882 set suffix "-- with new symbol table"
1883 set arg "$arg $suffix"
1884 set basename "$basename $suffix"
1887 -re "No such file or directory.*$gdb_prompt $" {
1888 perror "($basename) No such file or directory"
1891 -re "A problem internal to GDB has been detected" {
1892 perror "Couldn't load $basename into GDB (GDB internal error)."
1893 gdb_internal_error_resync
1896 -re "$gdb_prompt $" {
1897 perror "Couldn't load $basename into GDB."
1901 perror "Couldn't load $basename into GDB (timeout)."
1905 # This is an attempt to detect a core dump, but seems not to
1906 # work. Perhaps we need to match .* followed by eof, in which
1907 # gdb_expect does not seem to have a way to do that.
1908 perror "Couldn't load $basename into GDB (eof)."
1914 # Default gdb_spawn procedure.
1916 proc default_gdb_spawn { } {
1919 global INTERNAL_GDBFLAGS GDBFLAGS
1922 gdb_stop_suppressing_tests
1924 # Set the default value, it may be overriden later by specific testfile.
1926 # Use `set_board_info use_gdb_stub' for the board file to flag the inferior
1927 # is already started after connecting and run/attach are not supported.
1928 # This is used for the "remote" protocol. After GDB starts you should
1929 # check global $use_gdb_stub instead of the board as the testfile may force
1930 # a specific different target protocol itself.
1931 set use_gdb_stub [target_info exists use_gdb_stub]
1933 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1934 gdb_write_cmd_file "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1936 if [info exists gdb_spawn_id] {
1940 if ![is_remote host] {
1941 if { [which $GDB] == 0 } then {
1942 perror "$GDB does not exist."
1946 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]"]
1947 if { $res < 0 || $res == "" } {
1948 perror "Spawning $GDB failed."
1952 set gdb_spawn_id $res
1956 # Default gdb_start procedure.
1958 proc default_gdb_start { } {
1961 global inferior_spawn_id
1963 if [info exists gdb_spawn_id] {
1967 # Keep track of the number of times GDB has been launched.
1968 global gdb_instances
1978 # Default to assuming inferior I/O is done on GDB's terminal.
1979 if {![info exists inferior_spawn_id]} {
1980 set inferior_spawn_id $gdb_spawn_id
1983 # When running over NFS, particularly if running many simultaneous
1984 # tests on different hosts all using the same server, things can
1985 # get really slow. Give gdb at least 3 minutes to start up.
1987 -re "\[\r\n\]$gdb_prompt $" {
1988 verbose "GDB initialized."
1990 -re "$gdb_prompt $" {
1991 perror "GDB never initialized."
1996 perror "(timeout) GDB never initialized after 10 seconds."
2002 perror "(eof) GDB never initialized."
2008 # force the height to "unlimited", so no pagers get used
2010 send_gdb "set height 0\n"
2012 -re "$gdb_prompt $" {
2013 verbose "Setting height to 0." 2
2016 warning "Couldn't set the height to 0"
2019 # force the width to "unlimited", so no wraparound occurs
2020 send_gdb "set width 0\n"
2022 -re "$gdb_prompt $" {
2023 verbose "Setting width to 0." 2
2026 warning "Couldn't set the width to 0."
2034 # Utility procedure to give user control of the gdb prompt in a script. It is
2035 # meant to be used for debugging test cases, and should not be left in the
2038 proc gdb_interact { } {
2040 set spawn_id $gdb_spawn_id
2042 send_user "+------------------------------------------+\n"
2043 send_user "| Script interrupted, you can now interact |\n"
2044 send_user "| with by gdb. Type >>> to continue. |\n"
2045 send_user "+------------------------------------------+\n"
2052 # Examine the output of compilation to determine whether compilation
2053 # failed or not. If it failed determine whether it is due to missing
2054 # compiler or due to compiler error. Report pass, fail or unsupported
2057 proc gdb_compile_test {src output} {
2058 if { $output == "" } {
2059 pass "compilation [file tail $src]"
2060 } elseif { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output] } {
2061 unsupported "compilation [file tail $src]"
2062 } elseif { [regexp {.*: command not found[\r|\n]*$} $output] } {
2063 unsupported "compilation [file tail $src]"
2064 } elseif { [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } {
2065 unsupported "compilation [file tail $src]"
2067 verbose -log "compilation failed: $output" 2
2068 fail "compilation [file tail $src]"
2072 # Return a 1 for configurations for which we don't even want to try to
2075 proc skip_cplus_tests {} {
2076 if { [istarget "h8300-*-*"] } {
2080 # The C++ IO streams are too large for HC11/HC12 and are thus not
2081 # available. The gdb C++ tests use them and don't compile.
2082 if { [istarget "m6811-*-*"] } {
2085 if { [istarget "m6812-*-*"] } {
2091 # Return a 1 for configurations for which don't have both C++ and the STL.
2093 proc skip_stl_tests {} {
2094 # Symbian supports the C++ language, but the STL is missing
2095 # (both headers and libraries).
2096 if { [istarget "arm*-*-symbianelf*"] } {
2100 return [skip_cplus_tests]
2103 # Return a 1 if I don't even want to try to test FORTRAN.
2105 proc skip_fortran_tests {} {
2109 # Return a 1 if I don't even want to try to test ada.
2111 proc skip_ada_tests {} {
2115 # Return a 1 if I don't even want to try to test GO.
2117 proc skip_go_tests {} {
2121 # Return a 1 if I don't even want to try to test D.
2123 proc skip_d_tests {} {
2127 # Return 1 to skip Rust tests, 0 to try them.
2128 proc skip_rust_tests {} {
2129 return [expr {![isnative]}]
2132 # Return a 1 for configurations that do not support Python scripting.
2133 # PROMPT_REGEXP is the expected prompt.
2135 proc skip_python_tests_prompt { prompt_regexp } {
2136 global gdb_py_is_py3k
2138 gdb_test_multiple "python print ('test')" "verify python support" \
2139 -prompt "$prompt_regexp" {
2140 -re "not supported.*$prompt_regexp" {
2141 unsupported "Python support is disabled."
2144 -re "$prompt_regexp" {}
2147 gdb_test_multiple "python print (sys.version_info\[0\])" "check if python 3" \
2148 -prompt "$prompt_regexp" {
2149 -re "3.*$prompt_regexp" {
2150 set gdb_py_is_py3k 1
2152 -re ".*$prompt_regexp" {
2153 set gdb_py_is_py3k 0
2160 # Return a 1 for configurations that do not support Python scripting.
2161 # Note: This also sets various globals that specify which version of Python
2162 # is in use. See skip_python_tests_prompt.
2164 proc skip_python_tests {} {
2166 return [skip_python_tests_prompt "$gdb_prompt $"]
2169 # Return a 1 if we should skip shared library tests.
2171 proc skip_shlib_tests {} {
2172 # Run the shared library tests on native systems.
2177 # An abbreviated list of remote targets where we should be able to
2178 # run shared library tests.
2179 if {([istarget *-*-linux*]
2180 || [istarget *-*-*bsd*]
2181 || [istarget *-*-solaris2*]
2182 || [istarget arm*-*-symbianelf*]
2183 || [istarget *-*-mingw*]
2184 || [istarget *-*-cygwin*]
2185 || [istarget *-*-pe*])} {
2192 # Return 1 if we should skip tui related tests.
2194 proc skip_tui_tests {} {
2197 gdb_test_multiple "help layout" "verify tui support" {
2198 -re "Undefined command: \"layout\".*$gdb_prompt $" {
2201 -re "$gdb_prompt $" {
2208 # Test files shall make sure all the test result lines in gdb.sum are
2209 # unique in a test run, so that comparing the gdb.sum files of two
2210 # test runs gives correct results. Test files that exercise
2211 # variations of the same tests more than once, shall prefix the
2212 # different test invocations with different identifying strings in
2213 # order to make them unique.
2215 # About test prefixes:
2217 # $pf_prefix is the string that dejagnu prints after the result (FAIL,
2218 # PASS, etc.), and before the test message/name in gdb.sum. E.g., the
2219 # underlined substring in
2221 # PASS: gdb.base/mytest.exp: some test
2222 # ^^^^^^^^^^^^^^^^^^^^
2226 # The easiest way to adjust the test prefix is to append a test
2227 # variation prefix to the $pf_prefix, using the with_test_prefix
2230 # proc do_tests {} {
2231 # gdb_test ... ... "test foo"
2232 # gdb_test ... ... "test bar"
2234 # with_test_prefix "subvariation a" {
2235 # gdb_test ... ... "test x"
2238 # with_test_prefix "subvariation b" {
2239 # gdb_test ... ... "test x"
2243 # with_test_prefix "variation1" {
2244 # ...do setup for variation 1...
2248 # with_test_prefix "variation2" {
2249 # ...do setup for variation 2...
2255 # PASS: gdb.base/mytest.exp: variation1: test foo
2256 # PASS: gdb.base/mytest.exp: variation1: test bar
2257 # PASS: gdb.base/mytest.exp: variation1: subvariation a: test x
2258 # PASS: gdb.base/mytest.exp: variation1: subvariation b: test x
2259 # PASS: gdb.base/mytest.exp: variation2: test foo
2260 # PASS: gdb.base/mytest.exp: variation2: test bar
2261 # PASS: gdb.base/mytest.exp: variation2: subvariation a: test x
2262 # PASS: gdb.base/mytest.exp: variation2: subvariation b: test x
2264 # If for some reason more flexibility is necessary, one can also
2265 # manipulate the pf_prefix global directly, treating it as a string.
2269 # set saved_pf_prefix
2270 # append pf_prefix "${foo}: bar"
2271 # ... actual tests ...
2272 # set pf_prefix $saved_pf_prefix
2275 # Run BODY in the context of the caller, with the current test prefix
2276 # (pf_prefix) appended with one space, then PREFIX, and then a colon.
2277 # Returns the result of BODY.
2279 proc with_test_prefix { prefix body } {
2282 set saved $pf_prefix
2283 append pf_prefix " " $prefix ":"
2284 set code [catch {uplevel 1 $body} result]
2285 set pf_prefix $saved
2288 global errorInfo errorCode
2289 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2291 return -code $code $result
2295 # Wrapper for foreach that calls with_test_prefix on each iteration,
2296 # including the iterator's name and current value in the prefix.
2298 proc foreach_with_prefix {var list body} {
2300 foreach myvar $list {
2301 with_test_prefix "$var=$myvar" {
2302 set code [catch {uplevel 1 $body} result]
2306 global errorInfo errorCode
2307 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2308 } elseif {$code == 3} {
2310 } elseif {$code == 2} {
2311 return -code $code $result
2316 # Like TCL's native proc, but defines a procedure that wraps its body
2317 # within 'with_test_prefix "$proc_name" { ... }'.
2318 proc proc_with_prefix {name arguments body} {
2319 # Define the advertised proc.
2320 proc $name $arguments [list with_test_prefix $name $body]
2324 # Run BODY in the context of the caller. After BODY is run, the variables
2325 # listed in VARS will be reset to the values they had before BODY was run.
2327 # This is useful for providing a scope in which it is safe to temporarily
2328 # modify global variables, e.g.
2330 # global INTERNAL_GDBFLAGS
2333 # set foo GDBHISTSIZE
2335 # save_vars { INTERNAL_GDBFLAGS env($foo) env(HOME) } {
2336 # append INTERNAL_GDBFLAGS " -nx"
2337 # unset -nocomplain env(GDBHISTSIZE)
2342 # Here, although INTERNAL_GDBFLAGS, env(GDBHISTSIZE) and env(HOME) may be
2343 # modified inside BODY, this proc guarantees that the modifications will be
2344 # undone after BODY finishes executing.
2346 proc save_vars { vars body } {
2347 array set saved_scalars { }
2348 array set saved_arrays { }
2352 # First evaluate VAR in the context of the caller in case the variable
2353 # name may be a not-yet-interpolated string like env($foo)
2354 set var [uplevel 1 list $var]
2356 if [uplevel 1 [list info exists $var]] {
2357 if [uplevel 1 [list array exists $var]] {
2358 set saved_arrays($var) [uplevel 1 [list array get $var]]
2360 set saved_scalars($var) [uplevel 1 [list set $var]]
2363 lappend unset_vars $var
2367 set code [catch {uplevel 1 $body} result]
2369 foreach {var value} [array get saved_scalars] {
2370 uplevel 1 [list set $var $value]
2373 foreach {var value} [array get saved_arrays] {
2374 uplevel 1 [list unset $var]
2375 uplevel 1 [list array set $var $value]
2378 foreach var $unset_vars {
2379 uplevel 1 [list unset -nocomplain $var]
2383 global errorInfo errorCode
2384 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2386 return -code $code $result
2390 # Run tests in BODY with the current working directory (CWD) set to
2391 # DIR. When BODY is finished, restore the original CWD. Return the
2394 # This procedure doesn't check if DIR is a valid directory, so you
2395 # have to make sure of that.
2397 proc with_cwd { dir body } {
2399 verbose -log "Switching to directory $dir (saved CWD: $saved_dir)."
2402 set code [catch {uplevel 1 $body} result]
2404 verbose -log "Switching back to $saved_dir."
2408 global errorInfo errorCode
2409 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2411 return -code $code $result
2415 # Run tests in BODY with GDB prompt and variable $gdb_prompt set to
2416 # PROMPT. When BODY is finished, restore GDB prompt and variable
2418 # Returns the result of BODY.
2422 # 1) If you want to use, for example, "(foo)" as the prompt you must pass it
2423 # as "(foo)", and not the regexp form "\(foo\)" (expressed as "\\(foo\\)" in
2424 # TCL). PROMPT is internally converted to a suitable regexp for matching.
2425 # We do the conversion from "(foo)" to "\(foo\)" here for a few reasons:
2426 # a) It's more intuitive for callers to pass the plain text form.
2427 # b) We need two forms of the prompt:
2428 # - a regexp to use in output matching,
2429 # - a value to pass to the "set prompt" command.
2430 # c) It's easier to convert the plain text form to its regexp form.
2432 # 2) Don't add a trailing space, we do that here.
2434 proc with_gdb_prompt { prompt body } {
2437 # Convert "(foo)" to "\(foo\)".
2438 # We don't use string_to_regexp because while it works today it's not
2439 # clear it will work tomorrow: the value we need must work as both a
2440 # regexp *and* as the argument to the "set prompt" command, at least until
2441 # we start recording both forms separately instead of just $gdb_prompt.
2442 # The testsuite is pretty-much hardwired to interpret $gdb_prompt as the
2444 regsub -all {[]*+.|()^$\[\\]} $prompt {\\&} prompt
2446 set saved $gdb_prompt
2448 verbose -log "Setting gdb prompt to \"$prompt \"."
2449 set gdb_prompt $prompt
2450 gdb_test_no_output "set prompt $prompt " ""
2452 set code [catch {uplevel 1 $body} result]
2454 verbose -log "Restoring gdb prompt to \"$saved \"."
2455 set gdb_prompt $saved
2456 gdb_test_no_output "set prompt $saved " ""
2459 global errorInfo errorCode
2460 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2462 return -code $code $result
2466 # Run tests in BODY with target-charset setting to TARGET_CHARSET. When
2467 # BODY is finished, restore target-charset.
2469 proc with_target_charset { target_charset body } {
2473 gdb_test_multiple "show target-charset" "" {
2474 -re "The target character set is \".*; currently (.*)\"\..*$gdb_prompt " {
2475 set saved $expect_out(1,string)
2477 -re "The target character set is \"(.*)\".*$gdb_prompt " {
2478 set saved $expect_out(1,string)
2480 -re ".*$gdb_prompt " {
2481 fail "get target-charset"
2485 gdb_test_no_output "set target-charset $target_charset" ""
2487 set code [catch {uplevel 1 $body} result]
2489 gdb_test_no_output "set target-charset $saved" ""
2492 global errorInfo errorCode
2493 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2495 return -code $code $result
2499 # Switch the default spawn id to SPAWN_ID, so that gdb_test,
2500 # mi_gdb_test etc. default to using it.
2502 proc switch_gdb_spawn_id {spawn_id} {
2504 global board board_info
2506 set gdb_spawn_id $spawn_id
2507 set board [host_info name]
2508 set board_info($board,fileid) $spawn_id
2511 # Clear the default spawn id.
2513 proc clear_gdb_spawn_id {} {
2515 global board board_info
2517 unset -nocomplain gdb_spawn_id
2518 set board [host_info name]
2519 unset -nocomplain board_info($board,fileid)
2522 # Run BODY with SPAWN_ID as current spawn id.
2524 proc with_spawn_id { spawn_id body } {
2527 if [info exists gdb_spawn_id] {
2528 set saved_spawn_id $gdb_spawn_id
2531 switch_gdb_spawn_id $spawn_id
2533 set code [catch {uplevel 1 $body} result]
2535 if [info exists saved_spawn_id] {
2536 switch_gdb_spawn_id $saved_spawn_id
2542 global errorInfo errorCode
2543 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2545 return -code $code $result
2549 # Select the largest timeout from all the timeouts:
2550 # - the local "timeout" variable of the scope two levels above,
2551 # - the global "timeout" variable,
2552 # - the board variable "gdb,timeout".
2554 proc get_largest_timeout {} {
2555 upvar #0 timeout gtimeout
2556 upvar 2 timeout timeout
2559 if [info exists timeout] {
2562 if { [info exists gtimeout] && $gtimeout > $tmt } {
2565 if { [target_info exists gdb,timeout]
2566 && [target_info gdb,timeout] > $tmt } {
2567 set tmt [target_info gdb,timeout]
2577 # Run tests in BODY with timeout increased by factor of FACTOR. When
2578 # BODY is finished, restore timeout.
2580 proc with_timeout_factor { factor body } {
2583 set savedtimeout $timeout
2585 set timeout [expr [get_largest_timeout] * $factor]
2586 set code [catch {uplevel 1 $body} result]
2588 set timeout $savedtimeout
2590 global errorInfo errorCode
2591 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2593 return -code $code $result
2597 # Run BODY with timeout factor FACTOR if check-read1 is used.
2599 proc with_read1_timeout_factor { factor body } {
2600 if { [info exists ::env(READ1)] == 1 && $::env(READ1) == 1 } {
2601 # Use timeout factor
2603 # Reset timeout factor
2606 return [uplevel [list with_timeout_factor $factor $body]]
2609 # Return 1 if _Complex types are supported, otherwise, return 0.
2611 gdb_caching_proc support_complex_tests {
2613 if { [gdb_skip_float_test] } {
2614 # If floating point is not supported, _Complex is not
2619 # Compile a test program containing _Complex types.
2621 return [gdb_can_simple_compile complex {
2625 _Complex long double cld;
2631 # Return 1 if compiling go is supported.
2632 gdb_caching_proc support_go_compile {
2634 return [gdb_can_simple_compile go-hello {
2638 fmt.Println("hello world")
2643 # Return 1 if GDB can get a type for siginfo from the target, otherwise
2646 proc supports_get_siginfo_type {} {
2647 if { [istarget "*-*-linux*"] } {
2654 # Return 1 if the target supports hardware single stepping.
2656 proc can_hardware_single_step {} {
2658 if { [istarget "arm*-*-*"] || [istarget "mips*-*-*"]
2659 || [istarget "tic6x-*-*"] || [istarget "sparc*-*-linux*"]
2660 || [istarget "nios2-*-*"] } {
2667 # Return 1 if target hardware or OS supports single stepping to signal
2668 # handler, otherwise, return 0.
2670 proc can_single_step_to_signal_handler {} {
2671 # Targets don't have hardware single step. On these targets, when
2672 # a signal is delivered during software single step, gdb is unable
2673 # to determine the next instruction addresses, because start of signal
2674 # handler is one of them.
2675 return [can_hardware_single_step]
2678 # Return 1 if target supports process record, otherwise return 0.
2680 proc supports_process_record {} {
2682 if [target_info exists gdb,use_precord] {
2683 return [target_info gdb,use_precord]
2686 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
2687 || [istarget "i\[34567\]86-*-linux*"]
2688 || [istarget "aarch64*-*-linux*"]
2689 || [istarget "powerpc*-*-linux*"]
2690 || [istarget "s390*-*-linux*"] } {
2697 # Return 1 if target supports reverse debugging, otherwise return 0.
2699 proc supports_reverse {} {
2701 if [target_info exists gdb,can_reverse] {
2702 return [target_info gdb,can_reverse]
2705 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
2706 || [istarget "i\[34567\]86-*-linux*"]
2707 || [istarget "aarch64*-*-linux*"]
2708 || [istarget "powerpc*-*-linux*"]
2709 || [istarget "s390*-*-linux*"] } {
2716 # Return 1 if readline library is used.
2718 proc readline_is_used { } {
2721 gdb_test_multiple "show editing" "" {
2722 -re ".*Editing of command lines as they are typed is on\..*$gdb_prompt $" {
2725 -re ".*$gdb_prompt $" {
2731 # Return 1 if target is ELF.
2732 gdb_caching_proc is_elf_target {
2733 set me "is_elf_target"
2735 set src { int foo () {return 0;} }
2736 if {![gdb_simple_compile elf_target $src]} {
2740 set fp_obj [open $obj "r"]
2741 fconfigure $fp_obj -translation binary
2742 set data [read $fp_obj]
2747 set ELFMAG "\u007FELF"
2749 if {[string compare -length 4 $data $ELFMAG] != 0} {
2750 verbose "$me: returning 0" 2
2754 verbose "$me: returning 1" 2
2758 # Return 1 if the memory at address zero is readable.
2760 gdb_caching_proc is_address_zero_readable {
2764 gdb_test_multiple "x 0" "" {
2765 -re "Cannot access memory at address 0x0.*$gdb_prompt $" {
2768 -re ".*$gdb_prompt $" {
2776 # Produce source file NAME and write SOURCES into it.
2778 proc gdb_produce_source { name sources } {
2780 set f [open $name "w"]
2786 # Return 1 if target is ILP32.
2787 # This cannot be decided simply from looking at the target string,
2788 # as it might depend on externally passed compiler options like -m64.
2789 gdb_caching_proc is_ilp32_target {
2790 return [gdb_can_simple_compile is_ilp32_target {
2791 int dummy[sizeof (int) == 4
2792 && sizeof (void *) == 4
2793 && sizeof (long) == 4 ? 1 : -1];
2797 # Return 1 if target is LP64.
2798 # This cannot be decided simply from looking at the target string,
2799 # as it might depend on externally passed compiler options like -m64.
2800 gdb_caching_proc is_lp64_target {
2801 return [gdb_can_simple_compile is_lp64_target {
2802 int dummy[sizeof (int) == 4
2803 && sizeof (void *) == 8
2804 && sizeof (long) == 8 ? 1 : -1];
2808 # Return 1 if target has 64 bit addresses.
2809 # This cannot be decided simply from looking at the target string,
2810 # as it might depend on externally passed compiler options like -m64.
2811 gdb_caching_proc is_64_target {
2812 return [gdb_can_simple_compile is_64_target {
2813 int function(void) { return 3; }
2814 int dummy[sizeof (&function) == 8 ? 1 : -1];
2818 # Return 1 if target has x86_64 registers - either amd64 or x32.
2819 # x32 target identifies as x86_64-*-linux*, therefore it cannot be determined
2820 # just from the target string.
2821 gdb_caching_proc is_amd64_regs_target {
2822 if {![istarget "x86_64-*-*"] && ![istarget "i?86-*"]} {
2826 return [gdb_can_simple_compile is_amd64_regs_target {
2836 # Return 1 if this target is an x86 or x86-64 with -m32.
2837 proc is_x86_like_target {} {
2838 if {![istarget "x86_64-*-*"] && ![istarget i?86-*]} {
2841 return [expr [is_ilp32_target] && ![is_amd64_regs_target]]
2844 # Return 1 if this target is an arm or aarch32 on aarch64.
2846 gdb_caching_proc is_aarch32_target {
2847 if { [istarget "arm*-*-*"] } {
2851 if { ![istarget "aarch64*-*-*"] } {
2858 lappend list "\tmov $reg, $reg"
2861 return [gdb_can_simple_compile aarch32 [join $list \n]]
2864 # Return 1 if this target is an aarch64, either lp64 or ilp32.
2866 proc is_aarch64_target {} {
2867 if { ![istarget "aarch64*-*-*"] } {
2871 return [expr ![is_aarch32_target]]
2874 # Return 1 if displaced stepping is supported on target, otherwise, return 0.
2875 proc support_displaced_stepping {} {
2877 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"]
2878 || [istarget "arm*-*-linux*"] || [istarget "powerpc-*-linux*"]
2879 || [istarget "powerpc64-*-linux*"] || [istarget "s390*-*-*"]
2880 || [istarget "aarch64*-*-linux*"] } {
2887 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
2888 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2890 gdb_caching_proc skip_altivec_tests {
2891 global srcdir subdir gdb_prompt inferior_exited_re
2893 set me "skip_altivec_tests"
2895 # Some simulators are known to not support VMX instructions.
2896 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
2897 verbose "$me: target known to not support VMX, returning 1" 2
2901 # Make sure we have a compiler that understands altivec.
2902 if [get_compiler_info] {
2903 warning "Could not get compiler info"
2906 if [test_compiler_info gcc*] {
2907 set compile_flags "additional_flags=-maltivec"
2908 } elseif [test_compiler_info xlc*] {
2909 set compile_flags "additional_flags=-qaltivec"
2911 verbose "Could not compile with altivec support, returning 1" 2
2915 # Compile a test program containing VMX instructions.
2919 asm volatile ("vor v0,v0,v0");
2921 asm volatile ("vor 0,0,0");
2926 if {![gdb_simple_compile $me $src executable $compile_flags]} {
2930 # Compilation succeeded so now run it via gdb.
2934 gdb_reinitialize_dir $srcdir/$subdir
2938 -re ".*Illegal instruction.*${gdb_prompt} $" {
2939 verbose -log "\n$me altivec hardware not detected"
2940 set skip_vmx_tests 1
2942 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
2943 verbose -log "\n$me: altivec hardware detected"
2944 set skip_vmx_tests 0
2947 warning "\n$me: default case taken"
2948 set skip_vmx_tests 1
2952 remote_file build delete $obj
2954 verbose "$me: returning $skip_vmx_tests" 2
2955 return $skip_vmx_tests
2958 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
2959 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2961 gdb_caching_proc skip_vsx_tests {
2962 global srcdir subdir gdb_prompt inferior_exited_re
2964 set me "skip_vsx_tests"
2966 # Some simulators are known to not support Altivec instructions, so
2967 # they won't support VSX instructions as well.
2968 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
2969 verbose "$me: target known to not support VSX, returning 1" 2
2973 # Make sure we have a compiler that understands altivec.
2974 if [get_compiler_info] {
2975 warning "Could not get compiler info"
2978 if [test_compiler_info gcc*] {
2979 set compile_flags "additional_flags=-mvsx"
2980 } elseif [test_compiler_info xlc*] {
2981 set compile_flags "additional_flags=-qasm=gcc"
2983 verbose "Could not compile with vsx support, returning 1" 2
2987 # Compile a test program containing VSX instructions.
2990 double a[2] = { 1.0, 2.0 };
2992 asm volatile ("lxvd2x v0,v0,%[addr]" : : [addr] "r" (a));
2994 asm volatile ("lxvd2x 0,0,%[addr]" : : [addr] "r" (a));
2999 if {![gdb_simple_compile $me $src executable $compile_flags]} {
3003 # No error message, compilation succeeded so now run it via gdb.
3007 gdb_reinitialize_dir $srcdir/$subdir
3011 -re ".*Illegal instruction.*${gdb_prompt} $" {
3012 verbose -log "\n$me VSX hardware not detected"
3013 set skip_vsx_tests 1
3015 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3016 verbose -log "\n$me: VSX hardware detected"
3017 set skip_vsx_tests 0
3020 warning "\n$me: default case taken"
3021 set skip_vsx_tests 1
3025 remote_file build delete $obj
3027 verbose "$me: returning $skip_vsx_tests" 2
3028 return $skip_vsx_tests
3031 # Run a test on the target to see if it supports TSX hardware. Return 0 if so,
3032 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
3034 gdb_caching_proc skip_tsx_tests {
3035 global srcdir subdir gdb_prompt inferior_exited_re
3037 set me "skip_tsx_tests"
3039 # Compile a test program.
3042 asm volatile ("xbegin .L0");
3043 asm volatile ("xend");
3044 asm volatile (".L0: nop");
3048 if {![gdb_simple_compile $me $src executable]} {
3052 # No error message, compilation succeeded so now run it via gdb.
3056 gdb_reinitialize_dir $srcdir/$subdir
3060 -re ".*Illegal instruction.*${gdb_prompt} $" {
3061 verbose -log "$me: TSX hardware not detected."
3062 set skip_tsx_tests 1
3064 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3065 verbose -log "$me: TSX hardware detected."
3066 set skip_tsx_tests 0
3069 warning "\n$me: default case taken."
3070 set skip_tsx_tests 1
3074 remote_file build delete $obj
3076 verbose "$me: returning $skip_tsx_tests" 2
3077 return $skip_tsx_tests
3080 # Run a test on the target to see if it supports avx512bf16. Return 0 if so,
3081 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
3083 gdb_caching_proc skip_avx512bf16_tests {
3084 global srcdir subdir gdb_prompt inferior_exited_re
3086 set me "skip_avx512bf16_tests"
3087 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
3088 verbose "$me: target does not support avx512bf16, returning 1" 2
3092 # Compile a test program.
3095 asm volatile ("vcvtne2ps2bf16 %xmm0, %xmm1, %xmm0");
3099 if {![gdb_simple_compile $me $src executable]} {
3103 # No error message, compilation succeeded so now run it via gdb.
3107 gdb_reinitialize_dir $srcdir/$subdir
3111 -re ".*Illegal instruction.*${gdb_prompt} $" {
3112 verbose -log "$me: avx512bf16 hardware not detected."
3113 set skip_avx512bf16_tests 1
3115 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3116 verbose -log "$me: avx512bf16 hardware detected."
3117 set skip_avx512bf16_tests 0
3120 warning "\n$me: default case taken."
3121 set skip_avx512bf16_tests 1
3125 remote_file build delete $obj
3127 verbose "$me: returning $skip_avx512bf16_tests" 2
3128 return $skip_avx512bf16_tests
3131 # Run a test on the target to see if it supports btrace hardware. Return 0 if so,
3132 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
3134 gdb_caching_proc skip_btrace_tests {
3135 global srcdir subdir gdb_prompt inferior_exited_re
3137 set me "skip_btrace_tests"
3138 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
3139 verbose "$me: target does not support btrace, returning 1" 2
3143 # Compile a test program.
3144 set src { int main() { return 0; } }
3145 if {![gdb_simple_compile $me $src executable]} {
3149 # No error message, compilation succeeded so now run it via gdb.
3153 gdb_reinitialize_dir $srcdir/$subdir
3158 # In case of an unexpected output, we return 2 as a fail value.
3159 set skip_btrace_tests 2
3160 gdb_test_multiple "record btrace" "check btrace support" {
3161 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
3162 set skip_btrace_tests 1
3164 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
3165 set skip_btrace_tests 1
3167 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
3168 set skip_btrace_tests 1
3170 -re "^record btrace\r\n$gdb_prompt $" {
3171 set skip_btrace_tests 0
3175 remote_file build delete $obj
3177 verbose "$me: returning $skip_btrace_tests" 2
3178 return $skip_btrace_tests
3181 # Run a test on the target to see if it supports btrace pt hardware.
3182 # Return 0 if so, 1 if it does not. Based on 'check_vmx_hw_available'
3183 # from the GCC testsuite.
3185 gdb_caching_proc skip_btrace_pt_tests {
3186 global srcdir subdir gdb_prompt inferior_exited_re
3188 set me "skip_btrace_tests"
3189 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
3190 verbose "$me: target does not support btrace, returning 1" 2
3194 # Compile a test program.
3195 set src { int main() { return 0; } }
3196 if {![gdb_simple_compile $me $src executable]} {
3200 # No error message, compilation succeeded so now run it via gdb.
3204 gdb_reinitialize_dir $srcdir/$subdir
3209 # In case of an unexpected output, we return 2 as a fail value.
3210 set skip_btrace_tests 2
3211 gdb_test_multiple "record btrace pt" "check btrace pt support" {
3212 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
3213 set skip_btrace_tests 1
3215 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
3216 set skip_btrace_tests 1
3218 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
3219 set skip_btrace_tests 1
3221 -re "support was disabled at compile time.*\r\n$gdb_prompt $" {
3222 set skip_btrace_tests 1
3224 -re "^record btrace pt\r\n$gdb_prompt $" {
3225 set skip_btrace_tests 0
3229 remote_file build delete $obj
3231 verbose "$me: returning $skip_btrace_tests" 2
3232 return $skip_btrace_tests
3235 # Run a test on the target to see if it supports Aarch64 SVE hardware.
3236 # Return 0 if so, 1 if it does not. Note this causes a restart of GDB.
3238 gdb_caching_proc skip_aarch64_sve_tests {
3239 global srcdir subdir gdb_prompt inferior_exited_re
3241 set me "skip_aarch64_sve_tests"
3243 if { ![is_aarch64_target]} {
3247 set compile_flags "{additional_flags=-march=armv8-a+sve}"
3249 # Compile a test program containing SVE instructions.
3252 asm volatile ("ptrue p0.b");
3256 if {![gdb_simple_compile $me $src executable $compile_flags]} {
3260 # Compilation succeeded so now run it via gdb.
3264 -re ".*Illegal instruction.*${gdb_prompt} $" {
3265 verbose -log "\n$me sve hardware not detected"
3266 set skip_sve_tests 1
3268 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3269 verbose -log "\n$me: sve hardware detected"
3270 set skip_sve_tests 0
3273 warning "\n$me: default case taken"
3274 set skip_sve_tests 1
3278 remote_file build delete $obj
3280 verbose "$me: returning $skip_sve_tests" 2
3281 return $skip_sve_tests
3285 # A helper that compiles a test case to see if __int128 is supported.
3286 proc gdb_int128_helper {lang} {
3287 return [gdb_can_simple_compile "i128-for-$lang" {
3289 int main() { return 0; }
3293 # Return true if the C compiler understands the __int128 type.
3294 gdb_caching_proc has_int128_c {
3295 return [gdb_int128_helper c]
3298 # Return true if the C++ compiler understands the __int128 type.
3299 gdb_caching_proc has_int128_cxx {
3300 return [gdb_int128_helper c++]
3303 # Return true if the IFUNC feature is unsupported.
3304 gdb_caching_proc skip_ifunc_tests {
3305 if [gdb_can_simple_compile ifunc {
3307 typedef void F (void);
3308 F* g (void) { return &f_; }
3309 void f () __attribute__ ((ifunc ("g")));
3317 # Return whether we should skip tests for showing inlined functions in
3318 # backtraces. Requires get_compiler_info and get_debug_format.
3320 proc skip_inline_frame_tests {} {
3321 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
3322 if { ! [test_debug_format "DWARF 2"] } {
3326 # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line.
3327 if { ([test_compiler_info "gcc-2-*"]
3328 || [test_compiler_info "gcc-3-*"]
3329 || [test_compiler_info "gcc-4-0-*"]) } {
3336 # Return whether we should skip tests for showing variables from
3337 # inlined functions. Requires get_compiler_info and get_debug_format.
3339 proc skip_inline_var_tests {} {
3340 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
3341 if { ! [test_debug_format "DWARF 2"] } {
3348 # Return a 1 if we should skip tests that require hardware breakpoints
3350 proc skip_hw_breakpoint_tests {} {
3351 # Skip tests if requested by the board (note that no_hardware_watchpoints
3352 # disables both watchpoints and breakpoints)
3353 if { [target_info exists gdb,no_hardware_watchpoints]} {
3357 # These targets support hardware breakpoints natively
3358 if { [istarget "i?86-*-*"]
3359 || [istarget "x86_64-*-*"]
3360 || [istarget "ia64-*-*"]
3361 || [istarget "arm*-*-*"]
3362 || [istarget "aarch64*-*-*"]
3363 || [istarget "s390*-*-*"] } {
3370 # Return a 1 if we should skip tests that require hardware watchpoints
3372 proc skip_hw_watchpoint_tests {} {
3373 # Skip tests if requested by the board
3374 if { [target_info exists gdb,no_hardware_watchpoints]} {
3378 # These targets support hardware watchpoints natively
3379 if { [istarget "i?86-*-*"]
3380 || [istarget "x86_64-*-*"]
3381 || [istarget "ia64-*-*"]
3382 || [istarget "arm*-*-*"]
3383 || [istarget "aarch64*-*-*"]
3384 || [istarget "powerpc*-*-linux*"]
3385 || [istarget "s390*-*-*"] } {
3392 # Return a 1 if we should skip tests that require *multiple* hardware
3393 # watchpoints to be active at the same time
3395 proc skip_hw_watchpoint_multi_tests {} {
3396 if { [skip_hw_watchpoint_tests] } {
3400 # These targets support just a single hardware watchpoint
3401 if { [istarget "arm*-*-*"]
3402 || [istarget "powerpc*-*-linux*"] } {
3409 # Return a 1 if we should skip tests that require read/access watchpoints
3411 proc skip_hw_watchpoint_access_tests {} {
3412 if { [skip_hw_watchpoint_tests] } {
3416 # These targets support just write watchpoints
3417 if { [istarget "s390*-*-*"] } {
3424 # Return 1 if we should skip tests that require the runtime unwinder
3425 # hook. This must be invoked while gdb is running, after shared
3426 # libraries have been loaded. This is needed because otherwise a
3427 # shared libgcc won't be visible.
3429 proc skip_unwinder_tests {} {
3433 gdb_test_multiple "print _Unwind_DebugHook" "check for unwinder hook" {
3434 -re "= .*no debug info.*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
3436 -re "= .*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
3439 -re "No symbol .* in current context.\r\n$gdb_prompt $" {
3443 gdb_test_multiple "info probe" "check for stap probe in unwinder" {
3444 -re ".*libgcc.*unwind.*\r\n$gdb_prompt $" {
3447 -re "\r\n$gdb_prompt $" {
3454 # Return 1 if we should skip tests that require the libstdc++ stap
3455 # probes. This must be invoked while gdb is running, after shared
3456 # libraries have been loaded. PROMPT_REGEXP is the expected prompt.
3458 proc skip_libstdcxx_probe_tests_prompt { prompt_regexp } {
3460 gdb_test_multiple "info probe" "check for stap probe in libstdc++" \
3461 -prompt "$prompt_regexp" {
3462 -re ".*libstdcxx.*catch.*\r\n$prompt_regexp" {
3465 -re "\r\n$prompt_regexp" {
3468 set skip [expr !$supported]
3472 # As skip_libstdcxx_probe_tests_prompt, with gdb_prompt.
3474 proc skip_libstdcxx_probe_tests {} {
3476 return [skip_libstdcxx_probe_tests_prompt "$gdb_prompt $"]
3479 # Return 1 if we should skip tests of the "compile" feature.
3480 # This must be invoked after the inferior has been started.
3482 proc skip_compile_feature_tests {} {
3486 gdb_test_multiple "compile code -- ;" "check for working compile command" {
3487 "Could not load libcc1.*\r\n$gdb_prompt $" {
3490 -re "Command not supported on this host\\..*\r\n$gdb_prompt $" {
3493 -re "\r\n$gdb_prompt $" {
3499 # Helper for gdb_is_target_* procs. TARGET_NAME is the name of the target
3500 # we're looking for (used to build the test name). TARGET_STACK_REGEXP
3501 # is a regexp that will match the output of "maint print target-stack" if
3502 # the target in question is currently pushed. PROMPT_REGEXP is a regexp
3503 # matching the expected prompt after the command output.
3505 proc gdb_is_target_1 { target_name target_stack_regexp prompt_regexp } {
3506 set test "probe for target ${target_name}"
3507 gdb_test_multiple "maint print target-stack" $test \
3508 -prompt "$prompt_regexp" {
3509 -re "${target_stack_regexp}${prompt_regexp}" {
3513 -re "$prompt_regexp" {
3520 # Helper for gdb_is_target_remote where the expected prompt is variable.
3522 proc gdb_is_target_remote_prompt { prompt_regexp } {
3523 return [gdb_is_target_1 "remote" ".*emote serial target in gdb-specific protocol.*" $prompt_regexp]
3526 # Check whether we're testing with the remote or extended-remote
3529 proc gdb_is_target_remote { } {
3532 return [gdb_is_target_remote_prompt "$gdb_prompt $"]
3535 # Check whether we're testing with the native target.
3537 proc gdb_is_target_native { } {
3540 return [gdb_is_target_1 "native" ".*native \\(Native process\\).*" "$gdb_prompt $"]
3543 # Return the effective value of use_gdb_stub.
3545 # If the use_gdb_stub global has been set (it is set when the gdb process is
3546 # spawned), return that. Otherwise, return the value of the use_gdb_stub
3547 # property from the board file.
3549 # This is the preferred way of checking use_gdb_stub, since it allows to check
3550 # the value before the gdb has been spawned and it will return the correct value
3551 # even when it was overriden by the test.
3553 proc use_gdb_stub {} {
3556 if [info exists use_gdb_stub] {
3557 return $use_gdb_stub
3560 return [target_info exists use_gdb_stub]
3563 # Return 1 if the current remote target is an instance of our GDBserver, 0
3564 # otherwise. Return -1 if there was an error and we can't tell.
3566 gdb_caching_proc target_is_gdbserver {
3570 set test "probing for GDBserver"
3572 gdb_test_multiple "monitor help" $test {
3573 -re "The following monitor commands are supported.*Quit GDBserver.*$gdb_prompt $" {
3576 -re "$gdb_prompt $" {
3581 if { $is_gdbserver == -1 } {
3582 verbose -log "Unable to tell whether we are using GDBserver or not."
3585 return $is_gdbserver
3588 # N.B. compiler_info is intended to be local to this file.
3589 # Call test_compiler_info with no arguments to fetch its value.
3590 # Yes, this is counterintuitive when there's get_compiler_info,
3591 # but that's the current API.
3592 if [info exists compiler_info] {
3598 # Figure out what compiler I am using.
3599 # The result is cached so only the first invocation runs the compiler.
3601 # ARG can be empty or "C++". If empty, "C" is assumed.
3603 # There are several ways to do this, with various problems.
3605 # [ gdb_compile -E $ifile -o $binfile.ci ]
3606 # source $binfile.ci
3608 # Single Unix Spec v3 says that "-E -o ..." together are not
3609 # specified. And in fact, the native compiler on hp-ux 11 (among
3610 # others) does not work with "-E -o ...". Most targets used to do
3611 # this, and it mostly worked, because it works with gcc.
3613 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
3614 # source $binfile.ci
3616 # This avoids the problem with -E and -o together. This almost works
3617 # if the build machine is the same as the host machine, which is
3618 # usually true of the targets which are not gcc. But this code does
3619 # not figure which compiler to call, and it always ends up using the C
3620 # compiler. Not good for setting hp_aCC_compiler. Target
3621 # hppa*-*-hpux* used to do this.
3623 # [ gdb_compile -E $ifile > $binfile.ci ]
3624 # source $binfile.ci
3626 # dejagnu target_compile says that it supports output redirection,
3627 # but the code is completely different from the normal path and I
3628 # don't want to sweep the mines from that path. So I didn't even try
3631 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
3634 # I actually do this for all targets now. gdb_compile runs the right
3635 # compiler, and TCL captures the output, and I eval the output.
3637 # Unfortunately, expect logs the output of the command as it goes by,
3638 # and dejagnu helpfully prints a second copy of it right afterwards.
3639 # So I turn off expect logging for a moment.
3641 # [ gdb_compile $ifile $ciexe_file executable $args ]
3642 # [ remote_exec $ciexe_file ]
3643 # [ source $ci_file.out ]
3645 # I could give up on -E and just do this.
3646 # I didn't get desperate enough to try this.
3648 # -- chastain 2004-01-06
3650 proc get_compiler_info {{arg ""}} {
3651 # For compiler.c and compiler.cc
3654 # I am going to play with the log to keep noise out.
3658 # These come from compiler.c or compiler.cc
3659 global compiler_info
3661 # Legacy global data symbols.
3664 if [info exists compiler_info] {
3669 # Choose which file to preprocess.
3670 set ifile "${srcdir}/lib/compiler.c"
3671 if { $arg == "c++" } {
3672 set ifile "${srcdir}/lib/compiler.cc"
3675 # Run $ifile through the right preprocessor.
3676 # Toggle gdb.log to keep the compiler output out of the log.
3677 set saved_log [log_file -info]
3679 if [is_remote host] {
3680 # We have to use -E and -o together, despite the comments
3681 # above, because of how DejaGnu handles remote host testing.
3682 set ppout "$outdir/compiler.i"
3683 gdb_compile "${ifile}" "$ppout" preprocess [list "$arg" quiet getting_compiler_info]
3684 set file [open $ppout r]
3685 set cppout [read $file]
3688 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$arg" quiet getting_compiler_info] ]
3690 eval log_file $saved_log
3694 foreach cppline [ split "$cppout" "\n" ] {
3695 if { [ regexp "^#" "$cppline" ] } {
3697 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
3699 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
3701 verbose "get_compiler_info: $cppline" 2
3705 verbose -log "get_compiler_info: $cppline"
3710 # Set to unknown if for some reason compiler_info didn't get defined.
3711 if ![info exists compiler_info] {
3712 verbose -log "get_compiler_info: compiler_info not provided"
3713 set compiler_info "unknown"
3715 # Also set to unknown compiler if any diagnostics happened.
3717 verbose -log "get_compiler_info: got unexpected diagnostics"
3718 set compiler_info "unknown"
3721 # Set the legacy symbols.
3723 regexp "^gcc-(\[0-9\]+)-" "$compiler_info" matchall gcc_compiled
3725 # Log what happened.
3726 verbose -log "get_compiler_info: $compiler_info"
3728 # Most compilers will evaluate comparisons and other boolean
3729 # operations to 0 or 1.
3730 uplevel \#0 { set true 1 }
3731 uplevel \#0 { set false 0 }
3736 # Return the compiler_info string if no arg is provided.
3737 # Otherwise the argument is a glob-style expression to match against
3740 proc test_compiler_info { {compiler ""} } {
3741 global compiler_info
3744 # If no arg, return the compiler_info string.
3745 if [string match "" $compiler] {
3746 return $compiler_info
3749 return [string match $compiler $compiler_info]
3752 proc current_target_name { } {
3754 if [info exists target_info(target,name)] {
3755 set answer $target_info(target,name)
3762 set gdb_wrapper_initialized 0
3763 set gdb_wrapper_target ""
3764 set gdb_wrapper_file ""
3765 set gdb_wrapper_flags ""
3767 proc gdb_wrapper_init { args } {
3768 global gdb_wrapper_initialized
3769 global gdb_wrapper_file
3770 global gdb_wrapper_flags
3771 global gdb_wrapper_target
3773 if { $gdb_wrapper_initialized == 1 } { return; }
3775 if {[target_info exists needs_status_wrapper] && \
3776 [target_info needs_status_wrapper] != "0"} {
3777 set result [build_wrapper "testglue.o"]
3778 if { $result != "" } {
3779 set gdb_wrapper_file [lindex $result 0]
3780 if ![is_remote host] {
3781 set gdb_wrapper_file [file join [pwd] $gdb_wrapper_file]
3783 set gdb_wrapper_flags [lindex $result 1]
3785 warning "Status wrapper failed to build."
3788 set gdb_wrapper_file ""
3789 set gdb_wrapper_flags ""
3791 verbose "set gdb_wrapper_file = $gdb_wrapper_file"
3792 set gdb_wrapper_initialized 1
3793 set gdb_wrapper_target [current_target_name]
3796 # Determine options that we always want to pass to the compiler.
3797 gdb_caching_proc universal_compile_options {
3798 set me "universal_compile_options"
3801 set src [standard_temp_file ccopts[pid].c]
3802 set obj [standard_temp_file ccopts[pid].o]
3804 gdb_produce_source $src {
3805 int foo(void) { return 0; }
3808 # Try an option for disabling colored diagnostics. Some compilers
3809 # yield colored diagnostics by default (when run from a tty) unless
3810 # such an option is specified.
3811 set opt "additional_flags=-fdiagnostics-color=never"
3812 set lines [target_compile $src $obj object [list "quiet" $opt]]
3813 if [string match "" $lines] then {
3814 # Seems to have worked; use the option.
3815 lappend options $opt
3820 verbose "$me: returning $options" 2
3824 # Compile the code in $code to a file based on $name, using the flags
3825 # $compile_flag as well as debug, nowarning and quiet.
3826 # Return 1 if code can be compiled
3827 # Leave the file name of the resulting object in the upvar object.
3829 proc gdb_simple_compile {name code {type object} {compile_flags {}} {object obj}} {
3832 switch -regexp -- $type {
3847 foreach flag $compile_flags {
3848 if { "$flag" == "go" } {
3853 set src [standard_temp_file $name-[pid].$ext]
3854 set obj [standard_temp_file $name-[pid].$postfix]
3855 set compile_flags [concat $compile_flags {debug nowarnings quiet}]
3857 gdb_produce_source $src $code
3859 verbose "$name: compiling testfile $src" 2
3860 set lines [gdb_compile $src $obj $type $compile_flags]
3864 if ![string match "" $lines] then {
3865 verbose "$name: compilation failed, returning 0" 2
3871 # Compile the code in $code to a file based on $name, using the flags
3872 # $compile_flag as well as debug, nowarning and quiet.
3873 # Return 1 if code can be compiled
3874 # Delete all created files and objects.
3876 proc gdb_can_simple_compile {name code {type object} {compile_flags ""}} {
3877 set ret [gdb_simple_compile $name $code $type $compile_flags temp_obj]
3878 file delete $temp_obj
3882 # Some targets need to always link a special object in. Save its path here.
3883 global gdb_saved_set_unbuffered_mode_obj
3884 set gdb_saved_set_unbuffered_mode_obj ""
3886 # Compile source files specified by SOURCE into a binary of type TYPE at path
3887 # DEST. gdb_compile is implemented using DejaGnu's target_compile, so the type
3888 # parameter and most options are passed directly to it.
3890 # The type can be one of the following:
3892 # - object: Compile into an object file.
3893 # - executable: Compile and link into an executable.
3894 # - preprocess: Preprocess the source files.
3895 # - assembly: Generate assembly listing.
3897 # The following options are understood and processed by gdb_compile:
3899 # - shlib=so_path: Add SO_PATH to the sources, and enable some target-specific
3900 # quirks to be able to use shared libraries.
3901 # - shlib_load: Link with appropriate libraries to allow the test to
3902 # dynamically load libraries at runtime. For example, on Linux, this adds
3903 # -ldl so that the test can use dlopen.
3904 # - nowarnings: Inhibit all compiler warnings.
3905 # - pie: Force creation of PIE executables.
3906 # - nopie: Prevent creation of PIE executables.
3908 # And here are some of the not too obscure options understood by DejaGnu that
3909 # influence the compilation:
3911 # - additional_flags=flag: Add FLAG to the compiler flags.
3912 # - libs=library: Add LIBRARY to the libraries passed to the linker. The
3913 # argument can be a file, in which case it's added to the sources, or a
3915 # - ldflags=flag: Add FLAG to the linker flags.
3916 # - incdir=path: Add PATH to the searched include directories.
3917 # - libdir=path: Add PATH to the linker searched directories.
3918 # - ada, c++, f77, f90, go, rust: Compile the file as Ada, C++,
3919 # Fortran 77, Fortran 90, Go or Rust.
3920 # - debug: Build with debug information.
3921 # - optimize: Build with optimization.
3923 proc gdb_compile {source dest type options} {
3924 global GDB_TESTCASE_OPTIONS
3925 global gdb_wrapper_file
3926 global gdb_wrapper_flags
3929 global gdb_saved_set_unbuffered_mode_obj
3931 set outdir [file dirname $dest]
3933 # Add platform-specific options if a shared library was specified using
3934 # "shlib=librarypath" in OPTIONS.
3936 if {[lsearch -exact $options rust] != -1} {
3937 # -fdiagnostics-color is not a rustcc option.
3939 set new_options [universal_compile_options]
3942 # Some C/C++ testcases unconditionally pass -Wno-foo as additional
3943 # options to disable some warning. That is OK with GCC, because
3944 # by design, GCC accepts any -Wno-foo option, even if it doesn't
3945 # support -Wfoo. Clang however warns about unknown -Wno-foo by
3946 # default, unless you pass -Wno-unknown-warning-option as well.
3947 # We do that here, so that individual testcases don't have to
3949 if {[lsearch -exact $options getting_compiler_info] == -1
3950 && [lsearch -exact $options rust] == -1
3951 && [lsearch -exact $options ada] == -1
3952 && [lsearch -exact $options f77] == -1
3953 && [lsearch -exact $options f90] == -1
3954 && [lsearch -exact $options go] == -1
3955 && [test_compiler_info "clang-*"]} {
3956 lappend new_options "additional_flags=-Wno-unknown-warning-option"
3959 # Treating .c input files as C++ is deprecated in Clang, so
3960 # explicitly force C++ language.
3961 if { [lsearch -exact $options getting_compiler_info] == -1
3962 && [lsearch -exact $options c++] != -1
3963 && [string match *.c $source] != 0 } {
3965 # gdb_compile cannot handle this combination of options, the
3966 # result is a command like "clang -x c++ foo.c bar.so -o baz"
3967 # which tells Clang to treat bar.so as C++. The solution is
3968 # to call gdb_compile twice--once to compile, once to link--
3969 # either directly, or via build_executable_from_specs.
3970 if { [lsearch $options shlib=*] != -1 } {
3971 error "incompatible gdb_compile options"
3974 if {[test_compiler_info "clang-*"]} {
3975 lappend new_options early_flags=-x\ c++
3979 # Place (and look for) Fortran `.mod` files in the output
3980 # directory for this specific test.
3981 if {[lsearch -exact $options f77] != -1 \
3982 || [lsearch -exact $options f90] != -1 } {
3984 set mod_path [standard_output_file ""]
3985 lappend new_options "additional_flags=-J${mod_path}"
3990 set getting_compiler_info 0
3991 foreach opt $options {
3992 if {[regexp {^shlib=(.*)} $opt dummy_var shlib_name]
3993 && $type == "executable"} {
3994 if [test_compiler_info "xlc-*"] {
3995 # IBM xlc compiler doesn't accept shared library named other
3996 # than .so: use "-Wl," to bypass this
3997 lappend source "-Wl,$shlib_name"
3998 } elseif { ([istarget "*-*-mingw*"]
3999 || [istarget *-*-cygwin*]
4000 || [istarget *-*-pe*])} {
4001 lappend source "${shlib_name}.a"
4003 lappend source $shlib_name
4005 if { $shlib_found == 0 } {
4007 if { ([istarget "*-*-mingw*"]
4008 || [istarget *-*-cygwin*]) } {
4009 lappend new_options "additional_flags=-Wl,--enable-auto-import"
4011 if { [test_compiler_info "gcc-*"] || [test_compiler_info "clang-*"] } {
4012 # Undo debian's change in the default.
4013 # Put it at the front to not override any user-provided
4014 # value, and to make sure it appears in front of all the
4016 lappend new_options "early_flags=-Wl,--no-as-needed"
4019 } elseif { $opt == "shlib_load" && $type == "executable" } {
4021 } elseif { $opt == "getting_compiler_info" } {
4022 # If this is set, calling test_compiler_info will cause recursion.
4023 set getting_compiler_info 1
4025 lappend new_options $opt
4029 # Ensure stack protector is disabled for GCC, as this causes problems with
4030 # DWARF line numbering.
4031 # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88432
4032 # This option defaults to on for Debian/Ubuntu.
4033 if { $getting_compiler_info == 0
4034 && [test_compiler_info {gcc-*-*}]
4035 && !([test_compiler_info {gcc-[0-3]-*}]
4036 || [test_compiler_info {gcc-4-0-*}])
4037 && [lsearch -exact $options rust] == -1} {
4038 # Put it at the front to not override any user-provided value.
4039 lappend new_options "early_flags=-fno-stack-protector"
4042 # Because we link with libraries using their basename, we may need
4043 # (depending on the platform) to set a special rpath value, to allow
4044 # the executable to find the libraries it depends on.
4045 if { $shlib_load || $shlib_found } {
4046 if { ([istarget "*-*-mingw*"]
4047 || [istarget *-*-cygwin*]
4048 || [istarget *-*-pe*]) } {
4049 # Do not need anything.
4050 } elseif { [istarget *-*-freebsd*] || [istarget *-*-openbsd*] } {
4051 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
4052 } elseif { [istarget arm*-*-symbianelf*] } {
4053 if { $shlib_load } {
4054 lappend new_options "libs=-ldl"
4057 if { $shlib_load } {
4058 lappend new_options "libs=-ldl"
4060 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
4063 set options $new_options
4065 if [info exists GDB_TESTCASE_OPTIONS] {
4066 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS"
4068 verbose "options are $options"
4069 verbose "source is $source $dest $type $options"
4073 if {[target_info exists needs_status_wrapper] && \
4074 [target_info needs_status_wrapper] != "0" && \
4075 $gdb_wrapper_file != "" } {
4076 lappend options "libs=${gdb_wrapper_file}"
4077 lappend options "ldflags=${gdb_wrapper_flags}"
4080 # Replace the "nowarnings" option with the appropriate additional_flags
4081 # to disable compiler warnings.
4082 set nowarnings [lsearch -exact $options nowarnings]
4083 if {$nowarnings != -1} {
4084 if [target_info exists gdb,nowarnings_flag] {
4085 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
4087 set flag "additional_flags=-w"
4089 set options [lreplace $options $nowarnings $nowarnings $flag]
4092 # Replace the "pie" option with the appropriate compiler and linker flags
4093 # to enable PIE executables.
4094 set pie [lsearch -exact $options pie]
4096 if [target_info exists gdb,pie_flag] {
4097 set flag "additional_flags=[target_info gdb,pie_flag]"
4099 # For safety, use fPIE rather than fpie. On AArch64, m68k, PowerPC
4100 # and SPARC, fpie can cause compile errors due to the GOT exceeding
4101 # a maximum size. On other architectures the two flags are
4102 # identical (see the GCC manual). Note Debian9 and Ubuntu16.10
4103 # onwards default GCC to using fPIE. If you do require fpie, then
4104 # it can be set using the pie_flag.
4105 set flag "additional_flags=-fPIE"
4107 set options [lreplace $options $pie $pie $flag]
4109 if [target_info exists gdb,pie_ldflag] {
4110 set flag "ldflags=[target_info gdb,pie_ldflag]"
4112 set flag "ldflags=-pie"
4114 lappend options "$flag"
4117 # Replace the "nopie" option with the appropriate linker flag to disable
4118 # PIE executables. There are no compiler flags for this option.
4119 set nopie [lsearch -exact $options nopie]
4121 if [target_info exists gdb,nopie_flag] {
4122 set flag "ldflags=[target_info gdb,nopie_flag]"
4124 set flag "ldflags=-no-pie"
4126 set options [lreplace $options $nopie $nopie $flag]
4129 if { $type == "executable" } {
4130 if { ([istarget "*-*-mingw*"]
4131 || [istarget "*-*-*djgpp"]
4132 || [istarget "*-*-cygwin*"])} {
4133 # Force output to unbuffered mode, by linking in an object file
4134 # with a global contructor that calls setvbuf.
4136 # Compile the special object separately for two reasons:
4137 # 1) Insulate it from $options.
4138 # 2) Avoid compiling it for every gdb_compile invocation,
4139 # which is time consuming, especially if we're remote
4142 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
4143 verbose "compiling gdb_saved_set_unbuffered_obj"
4144 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
4145 set unbuf_obj ${objdir}/set_unbuffered_mode.o
4147 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
4148 if { $result != "" } {
4151 if {[is_remote host]} {
4152 set gdb_saved_set_unbuffered_mode_obj set_unbuffered_mode_saved.o
4154 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
4156 # Link a copy of the output object, because the
4157 # original may be automatically deleted.
4158 remote_download host $unbuf_obj $gdb_saved_set_unbuffered_mode_obj
4160 verbose "gdb_saved_set_unbuffered_obj already compiled"
4163 # Rely on the internal knowledge that the global ctors are ran in
4164 # reverse link order. In that case, we can use ldflags to
4165 # avoid copying the object file to the host multiple
4167 # This object can only be added if standard libraries are
4168 # used. Thus, we need to disable it if -nostdlib option is used
4169 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
4170 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
4175 set result [target_compile $source $dest $type $options]
4177 # Prune uninteresting compiler (and linker) output.
4178 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
4180 regsub "\[\r\n\]*$" "$result" "" result
4181 regsub "^\[\r\n\]*" "$result" "" result
4183 if { $type == "executable" && $result == "" \
4184 && ($nopie != -1 || $pie != -1) } {
4185 set is_pie [exec_is_pie "$dest"]
4186 if { $nopie != -1 && $is_pie == 1 } {
4187 set result "nopie failed to prevent PIE executable"
4188 } elseif { $pie != -1 && $is_pie == 0 } {
4189 set result "pie failed to generate PIE executable"
4193 if {[lsearch $options quiet] < 0} {
4194 # We shall update this on a per language basis, to avoid
4195 # changing the entire testsuite in one go.
4196 if {[lsearch $options f77] >= 0} {
4197 gdb_compile_test $source $result
4198 } elseif { $result != "" } {
4199 clone_output "gdb compile failed, $result"
4206 # This is just like gdb_compile, above, except that it tries compiling
4207 # against several different thread libraries, to see which one this
4209 proc gdb_compile_pthreads {source dest type options} {
4210 if {$type != "executable"} {
4211 return [gdb_compile $source $dest $type $options]
4214 set why_msg "unrecognized error"
4215 foreach lib {-lpthreads -lpthread -lthread ""} {
4216 # This kind of wipes out whatever libs the caller may have
4217 # set. Or maybe theirs will override ours. How infelicitous.
4218 set options_with_lib [concat $options [list libs=$lib quiet]]
4219 set ccout [gdb_compile $source $dest $type $options_with_lib]
4220 switch -regexp -- $ccout {
4221 ".*no posix threads support.*" {
4222 set why_msg "missing threads include file"
4225 ".*cannot open -lpthread.*" {
4226 set why_msg "missing runtime threads library"
4228 ".*Can't find library for -lpthread.*" {
4229 set why_msg "missing runtime threads library"
4232 pass "successfully compiled posix threads test case"
4238 if {!$built_binfile} {
4239 unsupported "couldn't compile [file tail $source]: ${why_msg}"
4244 # Build a shared library from SOURCES.
4246 proc gdb_compile_shlib {sources dest options} {
4247 set obj_options $options
4250 if { [lsearch -exact $options "c++"] >= 0 } {
4251 set info_options "c++"
4253 if [get_compiler_info ${info_options}] {
4257 switch -glob [test_compiler_info] {
4259 lappend obj_options "additional_flags=-qpic"
4262 if { !([istarget "*-*-cygwin*"]
4263 || [istarget "*-*-mingw*"]) } {
4264 lappend obj_options "additional_flags=-fpic"
4268 if { !([istarget "powerpc*-*-aix*"]
4269 || [istarget "rs6000*-*-aix*"]
4270 || [istarget "*-*-cygwin*"]
4271 || [istarget "*-*-mingw*"]
4272 || [istarget "*-*-pe*"]) } {
4273 lappend obj_options "additional_flags=-fpic"
4277 lappend obj_options "additional_flags=-fpic"
4280 # don't know what the compiler is...
4284 set outdir [file dirname $dest]
4286 foreach source $sources {
4287 set sourcebase [file tail $source]
4288 if {[file extension $source] == ".o"} {
4289 # Already a .o file.
4290 lappend objects $source
4291 } elseif {[gdb_compile $source "${outdir}/${sourcebase}.o" object \
4292 $obj_options] != ""} {
4295 lappend objects ${outdir}/${sourcebase}.o
4299 set link_options $options
4300 if [test_compiler_info "xlc-*"] {
4301 lappend link_options "additional_flags=-qmkshrobj"
4303 lappend link_options "additional_flags=-shared"
4305 if { ([istarget "*-*-mingw*"]
4306 || [istarget *-*-cygwin*]
4307 || [istarget *-*-pe*]) } {
4308 if { [is_remote host] } {
4309 set name [file tail ${dest}]
4313 lappend link_options "additional_flags=-Wl,--out-implib,${name}.a"
4315 # Set the soname of the library. This causes the linker on ELF
4316 # systems to create the DT_NEEDED entry in the executable referring
4317 # to the soname of the library, and not its absolute path. This
4318 # (using the absolute path) would be problem when testing on a
4321 # In conjunction with setting the soname, we add the special
4322 # rpath=$ORIGIN value when building the executable, so that it's
4323 # able to find the library in its own directory.
4324 set destbase [file tail $dest]
4325 lappend link_options "additional_flags=-Wl,-soname,$destbase"
4328 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
4331 if { [is_remote host]
4332 && ([istarget "*-*-mingw*"]
4333 || [istarget *-*-cygwin*]
4334 || [istarget *-*-pe*]) } {
4335 set dest_tail_name [file tail ${dest}]
4336 remote_upload host $dest_tail_name.a ${dest}.a
4337 remote_file host delete $dest_tail_name.a
4343 # This is just like gdb_compile_shlib, above, except that it tries compiling
4344 # against several different thread libraries, to see which one this
4346 proc gdb_compile_shlib_pthreads {sources dest options} {
4348 set why_msg "unrecognized error"
4349 foreach lib {-lpthreads -lpthread -lthread ""} {
4350 # This kind of wipes out whatever libs the caller may have
4351 # set. Or maybe theirs will override ours. How infelicitous.
4352 set options_with_lib [concat $options [list libs=$lib quiet]]
4353 set ccout [gdb_compile_shlib $sources $dest $options_with_lib]
4354 switch -regexp -- $ccout {
4355 ".*no posix threads support.*" {
4356 set why_msg "missing threads include file"
4359 ".*cannot open -lpthread.*" {
4360 set why_msg "missing runtime threads library"
4362 ".*Can't find library for -lpthread.*" {
4363 set why_msg "missing runtime threads library"
4366 pass "successfully compiled posix threads test case"
4372 if {!$built_binfile} {
4373 unsupported "couldn't compile $sources: ${why_msg}"
4378 # This is just like gdb_compile_pthreads, above, except that we always add the
4379 # objc library for compiling Objective-C programs
4380 proc gdb_compile_objc {source dest type options} {
4382 set why_msg "unrecognized error"
4383 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
4384 # This kind of wipes out whatever libs the caller may have
4385 # set. Or maybe theirs will override ours. How infelicitous.
4386 if { $lib == "solaris" } {
4387 set lib "-lpthread -lposix4"
4389 if { $lib != "-lobjc" } {
4390 set lib "-lobjc $lib"
4392 set options_with_lib [concat $options [list libs=$lib quiet]]
4393 set ccout [gdb_compile $source $dest $type $options_with_lib]
4394 switch -regexp -- $ccout {
4395 ".*no posix threads support.*" {
4396 set why_msg "missing threads include file"
4399 ".*cannot open -lpthread.*" {
4400 set why_msg "missing runtime threads library"
4402 ".*Can't find library for -lpthread.*" {
4403 set why_msg "missing runtime threads library"
4406 pass "successfully compiled objc with posix threads test case"
4412 if {!$built_binfile} {
4413 unsupported "couldn't compile [file tail $source]: ${why_msg}"
4418 # Build an OpenMP program from SOURCE. See prefatory comment for
4419 # gdb_compile, above, for discussion of the parameters to this proc.
4421 proc gdb_compile_openmp {source dest type options} {
4422 lappend options "additional_flags=-fopenmp"
4423 return [gdb_compile $source $dest $type $options]
4426 # Send a command to GDB.
4427 # For options for TYPE see gdb_stdin_log_write
4429 proc send_gdb { string {type standard}} {
4430 global suppress_flag
4431 if { $suppress_flag } {
4434 gdb_stdin_log_write $string $type
4435 return [remote_send host "$string"]
4438 # Send STRING to the inferior's terminal.
4440 proc send_inferior { string } {
4441 global inferior_spawn_id
4443 if {[catch "send -i $inferior_spawn_id -- \$string" errorInfo]} {
4453 proc gdb_expect { args } {
4454 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
4455 set atimeout [lindex $args 0]
4456 set expcode [list [lindex $args 1]]
4461 # A timeout argument takes precedence, otherwise of all the timeouts
4462 # select the largest.
4463 if [info exists atimeout] {
4466 set tmt [get_largest_timeout]
4469 global suppress_flag
4470 global remote_suppress_flag
4471 if [info exists remote_suppress_flag] {
4472 set old_val $remote_suppress_flag
4474 if [info exists suppress_flag] {
4475 if { $suppress_flag } {
4476 set remote_suppress_flag 1
4480 {uplevel remote_expect host $tmt $expcode} string]
4481 if [info exists old_val] {
4482 set remote_suppress_flag $old_val
4484 if [info exists remote_suppress_flag] {
4485 unset remote_suppress_flag
4490 global errorInfo errorCode
4492 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
4494 return -code $code $string
4498 # gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
4500 # Check for long sequence of output by parts.
4501 # TEST: is the test message to be printed with the test success/fail.
4502 # SENTINEL: Is the terminal pattern indicating that output has finished.
4503 # LIST: is the sequence of outputs to match.
4504 # If the sentinel is recognized early, it is considered an error.
4507 # 1 if the test failed,
4508 # 0 if the test passes,
4509 # -1 if there was an internal error.
4511 proc gdb_expect_list {test sentinel list} {
4513 global suppress_flag
4516 if { $suppress_flag } {
4518 unresolved "${test}"
4520 while { ${index} < [llength ${list}] } {
4521 set pattern [lindex ${list} ${index}]
4522 set index [expr ${index} + 1]
4523 verbose -log "gdb_expect_list pattern: /$pattern/" 2
4524 if { ${index} == [llength ${list}] } {
4527 -re "${pattern}${sentinel}" {
4528 # pass "${test}, pattern ${index} + sentinel"
4531 fail "${test} (pattern ${index} + sentinel)"
4534 -re ".*A problem internal to GDB has been detected" {
4535 fail "${test} (GDB internal error)"
4537 gdb_internal_error_resync
4540 fail "${test} (pattern ${index} + sentinel) (timeout)"
4545 # unresolved "${test}, pattern ${index} + sentinel"
4551 # pass "${test}, pattern ${index}"
4554 fail "${test} (pattern ${index})"
4557 -re ".*A problem internal to GDB has been detected" {
4558 fail "${test} (GDB internal error)"
4560 gdb_internal_error_resync
4563 fail "${test} (pattern ${index}) (timeout)"
4568 # unresolved "${test}, pattern ${index}"
4582 proc gdb_suppress_entire_file { reason } {
4583 global suppress_flag
4586 set suppress_flag -1
4590 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
4591 # gdb_expect to fail immediately (until the next call to
4592 # gdb_stop_suppressing_tests).
4594 proc gdb_suppress_tests { args } {
4595 global suppress_flag
4597 return; # fnf - disable pending review of results where
4598 # testsuite ran better without this
4601 if { $suppress_flag == 1 } {
4602 if { [llength $args] > 0 } {
4603 warning "[lindex $args 0]\n"
4605 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n"
4611 # Clear suppress_flag.
4613 proc gdb_stop_suppressing_tests { } {
4614 global suppress_flag
4616 if [info exists suppress_flag] {
4617 if { $suppress_flag > 0 } {
4619 clone_output "Tests restarted.\n"
4626 proc gdb_clear_suppressed { } {
4627 global suppress_flag
4632 # Spawn the gdb process.
4634 # This doesn't expect any output or do any other initialization,
4635 # leaving those to the caller.
4637 # Overridable function -- you can override this function in your
4640 proc gdb_spawn { } {
4644 # Spawn GDB with CMDLINE_FLAGS appended to the GDBFLAGS global.
4646 proc gdb_spawn_with_cmdline_opts { cmdline_flags } {
4649 set saved_gdbflags $GDBFLAGS
4651 if {$GDBFLAGS != ""} {
4654 append GDBFLAGS $cmdline_flags
4658 set GDBFLAGS $saved_gdbflags
4663 # Start gdb running, wait for prompt, and disable the pagers.
4665 # Overridable function -- you can override this function in your
4668 proc gdb_start { } {
4673 catch default_gdb_exit
4676 # Return true if we can spawn a program on the target and attach to
4679 proc can_spawn_for_attach { } {
4680 # We use exp_pid to get the inferior's pid, assuming that gives
4681 # back the pid of the program. On remote boards, that would give
4682 # us instead the PID of e.g., the ssh client, etc.
4683 if [is_remote target] then {
4687 # The "attach" command doesn't make sense when the target is
4688 # stub-like, where GDB finds the program already started on
4689 # initial connection.
4690 if {[target_info exists use_gdb_stub]} {
4698 # Kill a progress previously started with spawn_wait_for_attach, and
4699 # reap its wait status. PROC_SPAWN_ID is the spawn id associated with
4702 proc kill_wait_spawned_process { proc_spawn_id } {
4703 set pid [exp_pid -i $proc_spawn_id]
4705 verbose -log "killing ${pid}"
4706 remote_exec build "kill -9 ${pid}"
4708 verbose -log "closing ${proc_spawn_id}"
4709 catch "close -i $proc_spawn_id"
4710 verbose -log "waiting for ${proc_spawn_id}"
4712 # If somehow GDB ends up still attached to the process here, a
4713 # blocking wait hangs until gdb is killed (or until gdb / the
4714 # ptracer reaps the exit status too, but that won't happen because
4715 # something went wrong.) Passing -nowait makes expect tell Tcl to
4716 # wait for the PID in the background. That's fine because we
4717 # don't care about the exit status. */
4718 wait -nowait -i $proc_spawn_id
4721 # Returns the process id corresponding to the given spawn id.
4723 proc spawn_id_get_pid { spawn_id } {
4724 set testpid [exp_pid -i $spawn_id]
4726 if { [istarget "*-*-cygwin*"] } {
4727 # testpid is the Cygwin PID, GDB uses the Windows PID, which
4728 # might be different due to the way fork/exec works.
4729 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
4735 # Start a set of programs running and then wait for a bit, to be sure
4736 # that they can be attached to. Return a list of processes spawn IDs,
4737 # one element for each process spawned. It's a test error to call
4738 # this when [can_spawn_for_attach] is false.
4740 proc spawn_wait_for_attach { executable_list } {
4741 set spawn_id_list {}
4743 if ![can_spawn_for_attach] {
4744 # The caller should have checked can_spawn_for_attach itself
4745 # before getting here.
4746 error "can't spawn for attach with this target/board"
4749 foreach {executable} $executable_list {
4750 # Note we use Expect's spawn, not Tcl's exec, because with
4751 # spawn we control when to wait for/reap the process. That
4752 # allows killing the process by PID without being subject to
4754 lappend spawn_id_list [remote_spawn target $executable]
4759 return $spawn_id_list
4763 # gdb_load_cmd -- load a file into the debugger.
4764 # ARGS - additional args to load command.
4765 # return a -1 if anything goes wrong.
4767 proc gdb_load_cmd { args } {
4770 if [target_info exists gdb_load_timeout] {
4771 set loadtimeout [target_info gdb_load_timeout]
4773 set loadtimeout 1600
4775 send_gdb "load $args\n"
4776 verbose "Timeout is now $loadtimeout seconds" 2
4777 gdb_expect $loadtimeout {
4778 -re "Loading section\[^\r\]*\r\n" {
4781 -re "Start address\[\r\]*\r\n" {
4784 -re "Transfer rate\[\r\]*\r\n" {
4787 -re "Memory access error\[^\r\]*\r\n" {
4788 perror "Failed to load program"
4791 -re "$gdb_prompt $" {
4794 -re "(.*)\r\n$gdb_prompt " {
4795 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
4799 perror "Timed out trying to load $args."
4806 # Invoke "gcore". CORE is the name of the core file to write. TEST
4807 # is the name of the test case. This will return 1 if the core file
4808 # was created, 0 otherwise. If this fails to make a core file because
4809 # this configuration of gdb does not support making core files, it
4810 # will call "unsupported", not "fail". However, if this fails to make
4811 # a core file for some other reason, then it will call "fail".
4813 proc gdb_gcore_cmd {core test} {
4817 gdb_test_multiple "gcore $core" $test {
4818 -re "Saved corefile .*\[\r\n\]+$gdb_prompt $" {
4822 -re "(?:Can't create a corefile|Target does not support core file generation\\.)\[\r\n\]+$gdb_prompt $" {
4830 # Load core file CORE. TEST is the name of the test case.
4831 # This will record a pass/fail for loading the core file.
4833 # 1 - core file is successfully loaded
4834 # 0 - core file loaded but has a non fatal error
4835 # -1 - core file failed to load
4837 proc gdb_core_cmd { core test } {
4840 gdb_test_multiple "core $core" "$test" {
4841 -re "\\\[Thread debugging using \[^ \r\n\]* enabled\\\]\r\n" {
4844 -re " is not a core dump:.*\r\n$gdb_prompt $" {
4845 fail "$test (bad file format)"
4848 -re -wrap "[string_to_regexp $core]: No such file or directory.*" {
4849 fail "$test (file not found)"
4852 -re "Couldn't find .* registers in core file.*\r\n$gdb_prompt $" {
4853 fail "$test (incomplete note section)"
4856 -re "Core was generated by .*\r\n$gdb_prompt $" {
4860 -re ".*$gdb_prompt $" {
4865 fail "$test (timeout)"
4869 fail "unsupported output from 'core' command"
4873 # Return the filename to download to the target and load on the target
4874 # for this shared library. Normally just LIBNAME, unless shared libraries
4875 # for this target have separate link and load images.
4877 proc shlib_target_file { libname } {
4881 # Return the filename GDB will load symbols from when debugging this
4882 # shared library. Normally just LIBNAME, unless shared libraries for
4883 # this target have separate link and load images.
4885 proc shlib_symbol_file { libname } {
4889 # Return the filename to download to the target and load for this
4890 # executable. Normally just BINFILE unless it is renamed to something
4891 # else for this target.
4893 proc exec_target_file { binfile } {
4897 # Return the filename GDB will load symbols from when debugging this
4898 # executable. Normally just BINFILE unless executables for this target
4899 # have separate files for symbols.
4901 proc exec_symbol_file { binfile } {
4905 # Rename the executable file. Normally this is just BINFILE1 being renamed
4906 # to BINFILE2, but some targets require multiple binary files.
4907 proc gdb_rename_execfile { binfile1 binfile2 } {
4908 file rename -force [exec_target_file ${binfile1}] \
4909 [exec_target_file ${binfile2}]
4910 if { [exec_target_file ${binfile1}] != [exec_symbol_file ${binfile1}] } {
4911 file rename -force [exec_symbol_file ${binfile1}] \
4912 [exec_symbol_file ${binfile2}]
4916 # "Touch" the executable file to update the date. Normally this is just
4917 # BINFILE, but some targets require multiple files.
4918 proc gdb_touch_execfile { binfile } {
4919 set time [clock seconds]
4920 file mtime [exec_target_file ${binfile}] $time
4921 if { [exec_target_file ${binfile}] != [exec_symbol_file ${binfile}] } {
4922 file mtime [exec_symbol_file ${binfile}] $time
4926 # Like remote_download but provides a gdb-specific behavior.
4928 # If the destination board is remote, the local file FROMFILE is transferred as
4929 # usual with remote_download to TOFILE on the remote board. The destination
4930 # filename is added to the CLEANFILES global, so it can be cleaned up at the
4933 # If the destination board is local, the destination path TOFILE is passed
4934 # through standard_output_file, and FROMFILE is copied there.
4936 # In both cases, if TOFILE is omitted, it defaults to the [file tail] of
4939 proc gdb_remote_download {dest fromfile {tofile {}}} {
4940 # If TOFILE is not given, default to the same filename as FROMFILE.
4941 if {[string length $tofile] == 0} {
4942 set tofile [file tail $fromfile]
4945 if {[is_remote $dest]} {
4946 # When the DEST is remote, we simply send the file to DEST.
4949 set destname [remote_download $dest $fromfile $tofile]
4950 lappend cleanfiles $destname
4954 # When the DEST is local, we copy the file to the test directory (where
4955 # the executable is).
4957 # Note that we pass TOFILE through standard_output_file, regardless of
4958 # whether it is absolute or relative, because we don't want the tests
4959 # to be able to write outside their standard output directory.
4961 set tofile [standard_output_file $tofile]
4963 file copy -force $fromfile $tofile
4969 # gdb_load_shlib LIB...
4971 # Copy the listed library to the target.
4973 proc gdb_load_shlib { file } {
4976 if ![info exists gdb_spawn_id] {
4977 perror "gdb_load_shlib: GDB is not running"
4980 set dest [gdb_remote_download target [shlib_target_file $file]]
4982 if {[is_remote target]} {
4983 # If the target is remote, we need to tell gdb where to find the
4986 # We could set this even when not testing remotely, but a user
4987 # generally won't set it unless necessary. In order to make the tests
4988 # more like the real-life scenarios, we don't set it for local testing.
4989 gdb_test "set solib-search-path [file dirname $file]" "" ""
4996 # gdb_load -- load a file into the debugger. Specifying no file
4997 # defaults to the executable currently being debugged.
4998 # The return value is 0 for success, -1 for failure.
4999 # Many files in config/*.exp override this procedure.
5001 proc gdb_load { arg } {
5003 return [gdb_file_cmd $arg]
5008 # gdb_reload -- load a file into the target. Called before "running",
5009 # either the first time or after already starting the program once,
5010 # for remote targets. Most files that override gdb_load should now
5011 # override this instead.
5013 # INFERIOR_ARGS contains the arguments to pass to the inferiors, as a
5014 # single string to get interpreted by a shell. If the target board
5015 # overriding gdb_reload is a "stub", then it should arrange things such
5016 # these arguments make their way to the inferior process.
5018 proc gdb_reload { {inferior_args {}} } {
5019 # For the benefit of existing configurations, default to gdb_load.
5020 # Specifying no file defaults to the executable currently being
5022 return [gdb_load ""]
5025 proc gdb_continue { function } {
5028 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"]
5031 # Default implementation of gdb_init.
5032 proc default_gdb_init { test_file_name } {
5033 global gdb_wrapper_initialized
5034 global gdb_wrapper_target
5035 global gdb_test_file_name
5039 # Reset the timeout value to the default. This way, any testcase
5040 # that changes the timeout value without resetting it cannot affect
5041 # the timeout used in subsequent testcases.
5042 global gdb_test_timeout
5044 set timeout $gdb_test_timeout
5046 if { [regexp ".*gdb\.reverse\/.*" $test_file_name]
5047 && [target_info exists gdb_reverse_timeout] } {
5048 set timeout [target_info gdb_reverse_timeout]
5051 # If GDB_INOTIFY is given, check for writes to '.'. This is a
5052 # debugging tool to help confirm that the test suite is
5053 # parallel-safe. You need "inotifywait" from the
5054 # inotify-tools package to use this.
5055 global GDB_INOTIFY inotify_pid
5056 if {[info exists GDB_INOTIFY] && ![info exists inotify_pid]} {
5057 global outdir tool inotify_log_file
5059 set exclusions {outputs temp gdb[.](log|sum) cache}
5060 set exclusion_re ([join $exclusions |])
5062 set inotify_log_file [standard_temp_file inotify.out]
5063 set inotify_pid [exec inotifywait -r -m -e move,create,delete . \
5064 --exclude $exclusion_re \
5065 |& tee -a $outdir/$tool.log $inotify_log_file &]
5067 # Wait for the watches; hopefully this is long enough.
5070 # Clear the log so that we don't emit a warning the first time
5072 set fd [open $inotify_log_file w]
5076 # Block writes to all banned variables, and invocation of all
5077 # banned procedures...
5078 global banned_variables
5079 global banned_procedures
5080 global banned_traced
5081 if (!$banned_traced) {
5082 foreach banned_var $banned_variables {
5083 global "$banned_var"
5084 trace add variable "$banned_var" write error
5086 foreach banned_proc $banned_procedures {
5087 global "$banned_proc"
5088 trace add execution "$banned_proc" enter error
5093 # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same
5094 # messages as expected.
5099 # Don't let a .inputrc file or an existing setting of INPUTRC mess up
5100 # the test results. Even if /dev/null doesn't exist on the particular
5101 # platform, the readline library will use the default setting just by
5102 # failing to open the file. OTOH, opening /dev/null successfully will
5103 # also result in the default settings being used since nothing will be
5104 # read from this file.
5105 setenv INPUTRC "/dev/null"
5107 # This disables style output, which would interfere with many
5111 # Ensure that GDBHISTFILE and GDBHISTSIZE are removed from the
5112 # environment, we don't want these modifications to the history
5114 unset -nocomplain ::env(GDBHISTFILE)
5115 unset -nocomplain ::env(GDBHISTSIZE)
5117 # Initialize GDB's pty with a fixed size, to make sure we avoid pagination
5118 # during startup. See "man expect" for details about stty_init.
5120 set stty_init "rows 25 cols 80"
5122 # Some tests (for example gdb.base/maint.exp) shell out from gdb to use
5123 # grep. Clear GREP_OPTIONS to make the behavior predictable,
5124 # especially having color output turned on can cause tests to fail.
5125 setenv GREP_OPTIONS ""
5127 # Clear $gdbserver_reconnect_p.
5128 global gdbserver_reconnect_p
5129 set gdbserver_reconnect_p 1
5130 unset gdbserver_reconnect_p
5132 # Clear $last_loaded_file
5133 global last_loaded_file
5134 unset -nocomplain last_loaded_file
5136 # Reset GDB number of instances
5137 global gdb_instances
5142 gdb_clear_suppressed
5144 set gdb_test_file_name [file rootname [file tail $test_file_name]]
5146 # Make sure that the wrapper is rebuilt
5147 # with the appropriate multilib option.
5148 if { $gdb_wrapper_target != [current_target_name] } {
5149 set gdb_wrapper_initialized 0
5152 # Unlike most tests, we have a small number of tests that generate
5153 # a very large amount of output. We therefore increase the expect
5154 # buffer size to be able to contain the entire test output. This
5155 # is especially needed by gdb.base/info-macros.exp.
5157 # Also set this value for the currently running GDB.
5158 match_max [match_max -d]
5160 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
5161 set pf_prefix "[file tail [file dirname $test_file_name]]/[file tail $test_file_name]:"
5164 if [target_info exists gdb_prompt] {
5165 set gdb_prompt [target_info gdb_prompt]
5167 set gdb_prompt "\\(gdb\\)"
5170 if [info exists use_gdb_stub] {
5174 gdb_setup_known_globals
5176 if { [info procs ::gdb_tcl_unknown] != "" } {
5177 # Dejagnu overrides proc unknown. The dejagnu version may trigger in a
5178 # test-case but abort the entire test run. To fix this, we install a
5179 # local version here, which reverts dejagnu's override, and restore
5180 # dejagnu's version in gdb_finish.
5181 rename ::unknown ::dejagnu_unknown
5182 proc unknown { args } {
5183 # Use tcl's unknown.
5184 set cmd [lindex $args 0]
5185 unresolved "testcase aborted due to invalid command name: $cmd"
5186 return [uplevel 1 ::gdb_tcl_unknown $args]
5191 # Return a path using GDB_PARALLEL.
5192 # ARGS is a list of path elements to append to "$objdir/$GDB_PARALLEL".
5193 # GDB_PARALLEL must be defined, the caller must check.
5195 # The default value for GDB_PARALLEL is, canonically, ".".
5196 # The catch is that tests don't expect an additional "./" in file paths so
5197 # omit any directory for the default case.
5198 # GDB_PARALLEL is written as "yes" for the default case in Makefile.in to mark
5199 # its special handling.
5201 proc make_gdb_parallel_path { args } {
5202 global GDB_PARALLEL objdir
5203 set joiner [list "file" "join" $objdir]
5204 if { [info exists GDB_PARALLEL] && $GDB_PARALLEL != "yes" } {
5205 lappend joiner $GDB_PARALLEL
5207 set joiner [concat $joiner $args]
5208 return [eval $joiner]
5211 # Turn BASENAME into a full file name in the standard output
5212 # directory. It is ok if BASENAME is the empty string; in this case
5213 # the directory is returned.
5215 proc standard_output_file {basename} {
5216 global objdir subdir gdb_test_file_name
5218 set dir [make_gdb_parallel_path outputs $subdir $gdb_test_file_name]
5220 # If running on MinGW, replace /c/foo with c:/foo
5221 if { [ishost *-*-mingw*] } {
5222 set dir [exec sh -c "cd ${dir} && pwd -W"]
5224 return [file join $dir $basename]
5227 # Turn BASENAME into a full file name in the standard output directory. If
5228 # GDB has been launched more than once then append the count, starting with
5231 proc standard_output_file_with_gdb_instance {basename} {
5232 global gdb_instances
5233 set count $gdb_instances
5236 return [standard_output_file $basename]
5238 return [standard_output_file ${basename}.${count}]
5241 # Return the name of a file in our standard temporary directory.
5243 proc standard_temp_file {basename} {
5244 # Since a particular runtest invocation is only executing a single test
5245 # file at any given time, we can use the runtest pid to build the
5246 # path of the temp directory.
5247 set dir [make_gdb_parallel_path temp [pid]]
5249 return [file join $dir $basename]
5252 # Rename file A to file B, if B does not already exists. Otherwise, leave B
5253 # as is and delete A. Return 1 if rename happened.
5255 proc tentative_rename { a b } {
5256 global errorInfo errorCode
5257 set code [catch {file rename -- $a $b} result]
5258 if { $code == 1 && [lindex $errorCode 0] == "POSIX" \
5259 && [lindex $errorCode 1] == "EEXIST" } {
5264 return -code error -errorinfo $errorInfo -errorcode $errorCode $result
5265 } elseif {$code > 1} {
5266 return -code $code $result
5271 # Create a file with name FILENAME and contents TXT in the cache directory.
5272 # If EXECUTABLE, mark the new file for execution.
5274 proc cached_file { filename txt {executable 0}} {
5275 set filename [make_gdb_parallel_path cache $filename]
5277 if { [file exists $filename] } {
5281 set dir [file dirname $filename]
5284 set tmp_filename $filename.[pid]
5285 set fd [open $tmp_filename w]
5289 if { $executable } {
5290 exec chmod +x $tmp_filename
5292 tentative_rename $tmp_filename $filename
5297 # Set 'testfile', 'srcfile', and 'binfile'.
5299 # ARGS is a list of source file specifications.
5300 # Without any arguments, the .exp file's base name is used to
5301 # compute the source file name. The ".c" extension is added in this case.
5302 # If ARGS is not empty, each entry is a source file specification.
5303 # If the specification starts with a "." or "-", it is treated as a suffix
5304 # to append to the .exp file's base name.
5305 # If the specification is the empty string, it is treated as if it
5307 # Otherwise it is a file name.
5308 # The first file in the list is used to set the 'srcfile' global.
5309 # Each subsequent name is used to set 'srcfile2', 'srcfile3', etc.
5311 # Most tests should call this without arguments.
5313 # If a completely different binary file name is needed, then it
5314 # should be handled in the .exp file with a suitable comment.
5316 proc standard_testfile {args} {
5317 global gdb_test_file_name
5319 global gdb_test_file_last_vars
5322 global testfile binfile
5324 set testfile $gdb_test_file_name
5325 set binfile [standard_output_file ${testfile}]
5327 if {[llength $args] == 0} {
5331 # Unset our previous output variables.
5332 # This can help catch hidden bugs.
5333 if {[info exists gdb_test_file_last_vars]} {
5334 foreach varname $gdb_test_file_last_vars {
5336 catch {unset $varname}
5339 # 'executable' is often set by tests.
5340 set gdb_test_file_last_vars {executable}
5344 set varname srcfile$suffix
5347 # Handle an extension.
5351 set first [string range $arg 0 0]
5352 if { $first == "." || $first == "-" } {
5353 set arg $testfile$arg
5358 lappend gdb_test_file_last_vars $varname
5360 if {$suffix == ""} {
5368 # The default timeout used when testing GDB commands. We want to use
5369 # the same timeout as the default dejagnu timeout, unless the user has
5370 # already provided a specific value (probably through a site.exp file).
5371 global gdb_test_timeout
5372 if ![info exists gdb_test_timeout] {
5373 set gdb_test_timeout $timeout
5376 # A list of global variables that GDB testcases should not use.
5377 # We try to prevent their use by monitoring write accesses and raising
5378 # an error when that happens.
5379 set banned_variables { bug_id prms_id }
5381 # A list of procedures that GDB testcases should not use.
5382 # We try to prevent their use by monitoring invocations and raising
5383 # an error when that happens.
5384 set banned_procedures { strace }
5386 # gdb_init is called by runtest at start, but also by several
5387 # tests directly; gdb_finish is only called from within runtest after
5388 # each test source execution.
5389 # Placing several traces by repetitive calls to gdb_init leads
5390 # to problems, as only one trace is removed in gdb_finish.
5391 # To overcome this possible problem, we add a variable that records
5392 # if the banned variables and procedures are already traced.
5395 # Global array that holds the name of all global variables at the time
5396 # a test script is started. After the test script has completed any
5397 # global not in this list is deleted.
5398 array set gdb_known_globals {}
5400 # Setup the GDB_KNOWN_GLOBALS array with the names of all current
5402 proc gdb_setup_known_globals {} {
5403 global gdb_known_globals
5405 array set gdb_known_globals {}
5406 foreach varname [info globals] {
5407 set gdb_known_globals($varname) 1
5411 # Cleanup the global namespace. Any global not in the
5412 # GDB_KNOWN_GLOBALS array is unset, this ensures we don't "leak"
5413 # globals from one test script to another.
5414 proc gdb_cleanup_globals {} {
5415 global gdb_known_globals gdb_persistent_globals
5417 foreach varname [info globals] {
5418 if {![info exists gdb_known_globals($varname)]} {
5419 if { [info exists gdb_persistent_globals($varname)] } {
5422 uplevel #0 unset $varname
5427 # Create gdb_tcl_unknown, a copy tcl's ::unknown, provided it's present as a
5429 set temp [interp create]
5430 if { [interp eval $temp "info procs ::unknown"] != "" } {
5431 set old_args [interp eval $temp "info args ::unknown"]
5432 set old_body [interp eval $temp "info body ::unknown"]
5433 eval proc gdb_tcl_unknown {$old_args} {$old_body}
5438 # GDB implementation of ${tool}_init. Called right before executing the
5440 # Overridable function -- you can override this function in your
5442 proc gdb_init { args } {
5443 # A baseboard file overriding this proc and calling the default version
5444 # should behave the same as this proc. So, don't add code here, but to
5445 # the default version instead.
5446 return [default_gdb_init {*}$args]
5449 # GDB implementation of ${tool}_finish. Called right after executing the
5451 proc gdb_finish { } {
5452 global gdbserver_reconnect_p
5455 global known_globals
5457 if { [info procs ::gdb_tcl_unknown] != "" } {
5458 # Restore dejagnu's version of proc unknown.
5460 rename ::dejagnu_unknown ::unknown
5463 # Exit first, so that the files are no longer in use.
5466 if { [llength $cleanfiles] > 0 } {
5467 eval remote_file target delete $cleanfiles
5471 # Unblock write access to the banned variables. Dejagnu typically
5472 # resets some of them between testcases.
5473 global banned_variables
5474 global banned_procedures
5475 global banned_traced
5476 if ($banned_traced) {
5477 foreach banned_var $banned_variables {
5478 global "$banned_var"
5479 trace remove variable "$banned_var" write error
5481 foreach banned_proc $banned_procedures {
5482 global "$banned_proc"
5483 trace remove execution "$banned_proc" enter error
5488 global gdb_finish_hooks
5489 foreach gdb_finish_hook $gdb_finish_hooks {
5492 set gdb_finish_hooks [list]
5498 set debug_format "unknown"
5500 # Run the gdb command "info source" and extract the debugging format
5501 # information from the output and save it in debug_format.
5503 proc get_debug_format { } {
5508 set debug_format "unknown"
5509 send_gdb "info source\n"
5511 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
5512 set debug_format $expect_out(1,string)
5513 verbose "debug format is $debug_format"
5516 -re "No current source file.\r\n$gdb_prompt $" {
5517 perror "get_debug_format used when no current source file"
5520 -re "$gdb_prompt $" {
5521 warning "couldn't check debug format (no valid response)."
5525 warning "couldn't check debug format (timeout)."
5531 # Return true if FORMAT matches the debug format the current test was
5532 # compiled with. FORMAT is a shell-style globbing pattern; it can use
5533 # `*', `[...]', and so on.
5535 # This function depends on variables set by `get_debug_format', above.
5537 proc test_debug_format {format} {
5540 return [expr [string match $format $debug_format] != 0]
5543 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
5544 # COFF, stabs, etc). If that format matches the format that the
5545 # current test was compiled with, then the next test is expected to
5546 # fail for any target. Returns 1 if the next test or set of tests is
5547 # expected to fail, 0 otherwise (or if it is unknown). Must have
5548 # previously called get_debug_format.
5549 proc setup_xfail_format { format } {
5550 set ret [test_debug_format $format]
5558 # gdb_get_line_number TEXT [FILE]
5560 # Search the source file FILE, and return the line number of the
5561 # first line containing TEXT. If no match is found, an error is thrown.
5563 # TEXT is a string literal, not a regular expression.
5565 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
5566 # specified, and does not start with "/", then it is assumed to be in
5567 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
5568 # by changing the callers and the interface at the same time.
5569 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
5570 # gdb.base/ena-dis-br.exp.
5572 # Use this function to keep your test scripts independent of the
5573 # exact line numbering of the source file. Don't write:
5575 # send_gdb "break 20"
5577 # This means that if anyone ever edits your test's source file,
5578 # your test could break. Instead, put a comment like this on the
5579 # source file line you want to break at:
5581 # /* breakpoint spot: frotz.exp: test name */
5583 # and then write, in your test script (which we assume is named
5586 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
5588 # (Yes, Tcl knows how to handle the nested quotes and brackets.
5591 # % puts "foo [lindex "bar baz" 1]"
5594 # Tcl is quite clever, for a little stringy language.)
5598 # The previous implementation of this procedure used the gdb search command.
5599 # This version is different:
5601 # . It works with MI, and it also works when gdb is not running.
5603 # . It operates on the build machine, not the host machine.
5605 # . For now, this implementation fakes a current directory of
5606 # $srcdir/$subdir to be compatible with the old implementation.
5607 # This will go away eventually and some callers will need to
5610 # . The TEXT argument is literal text and matches literally,
5611 # not a regular expression as it was before.
5613 # . State changes in gdb, such as changing the current file
5614 # and setting $_, no longer happen.
5616 # After a bit of time we can forget about the differences from the
5617 # old implementation.
5619 # --chastain 2004-08-05
5621 proc gdb_get_line_number { text { file "" } } {
5626 if { "$file" == "" } then {
5629 if { ! [regexp "^/" "$file"] } then {
5630 set file "$srcdir/$subdir/$file"
5633 if { [ catch { set fd [open "$file"] } message ] } then {
5638 for { set line 1 } { 1 } { incr line } {
5639 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
5642 if { $nchar < 0 } then {
5645 if { [string first "$text" "$body"] >= 0 } then {
5651 if { [ catch { close "$fd" } message ] } then {
5656 error "undefined tag \"$text\""
5662 # Continue the program until it ends.
5664 # MSSG is the error message that gets printed. If not given, a
5666 # COMMAND is the command to invoke. If not given, "continue" is
5668 # ALLOW_EXTRA is a flag indicating whether the test should expect
5669 # extra output between the "Continuing." line and the program
5670 # exiting. By default it is zero; if nonzero, any extra output
5673 proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} {
5674 global inferior_exited_re use_gdb_stub
5677 set text "continue until exit"
5679 set text "continue until exit at $mssg"
5687 # By default, we don't rely on exit() behavior of remote stubs --
5688 # it's common for exit() to be implemented as a simple infinite
5689 # loop, or a forced crash/reset. For native targets, by default, we
5690 # assume process exit is reported as such. If a non-reliable target
5691 # is used, we set a breakpoint at exit, and continue to that.
5692 if { [target_info exists exit_is_reliable] } {
5693 set exit_is_reliable [target_info exit_is_reliable]
5695 set exit_is_reliable [expr ! $use_gdb_stub]
5698 if { ! $exit_is_reliable } {
5699 if {![gdb_breakpoint "exit"]} {
5702 gdb_test $command "Continuing..*Breakpoint .*exit.*" \
5705 # Continue until we exit. Should not stop again.
5706 # Don't bother to check the output of the program, that may be
5707 # extremely tough for some remote systems.
5709 "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\
5714 proc rerun_to_main {} {
5715 global gdb_prompt use_gdb_stub
5720 -re ".*Breakpoint .*main .*$gdb_prompt $"\
5721 {pass "rerun to main" ; return 0}
5722 -re "$gdb_prompt $"\
5723 {fail "rerun to main" ; return 0}
5724 timeout {fail "(timeout) rerun to main" ; return 0}
5729 -re "The program .* has been started already.*y or n. $" {
5730 send_gdb "y\n" answer
5733 -re "Starting program.*$gdb_prompt $"\
5734 {pass "rerun to main" ; return 0}
5735 -re "$gdb_prompt $"\
5736 {fail "rerun to main" ; return 0}
5737 timeout {fail "(timeout) rerun to main" ; return 0}
5742 # Return true if EXECUTABLE contains a .gdb_index or .debug_names index section.
5744 proc exec_has_index_section { executable } {
5745 set readelf_program [gdb_find_readelf]
5746 set res [catch {exec $readelf_program -S $executable \
5747 | grep -E "\.gdb_index|\.debug_names" }]
5754 # Return list with major and minor version of readelf, or an empty list.
5755 gdb_caching_proc readelf_version {
5756 set readelf_program [gdb_find_readelf]
5757 set res [catch {exec $readelf_program --version} output]
5761 set lines [split $output \n]
5762 set line [lindex $lines 0]
5763 set res [regexp {[ \t]+([0-9]+)[.]([0-9]+)[^ \t]*$} \
5764 $line dummy major minor]
5768 return [list $major $minor]
5771 # Return 1 if readelf prints the PIE flag, 0 if is doesn't, and -1 if unknown.
5772 proc readelf_prints_pie { } {
5773 set version [readelf_version]
5774 if { [llength $version] == 0 } {
5777 set major [lindex $version 0]
5778 set minor [lindex $version 1]
5779 # It would be better to construct a PIE executable and test if the PIE
5780 # flag is printed by readelf, but we cannot reliably construct a PIE
5781 # executable if the multilib_flags dictate otherwise
5782 # (--target_board=unix/-no-pie/-fno-PIE).
5783 return [version_at_least $major $minor 2 26]
5786 # Return 1 if EXECUTABLE is a Position Independent Executable, 0 if it is not,
5787 # and -1 if unknown.
5789 proc exec_is_pie { executable } {
5790 set res [readelf_prints_pie]
5794 set readelf_program [gdb_find_readelf]
5795 # We're not testing readelf -d | grep "FLAGS_1.*Flags:.*PIE"
5796 # because the PIE flag is not set by all versions of gold, see PR
5798 set res [catch {exec $readelf_program -h $executable} output]
5802 set res [regexp -line {^[ \t]*Type:[ \t]*DYN \(Shared object file\)$} \
5810 # Return true if a test should be skipped due to lack of floating
5811 # point support or GDB can't fetch the contents from floating point
5814 gdb_caching_proc gdb_skip_float_test {
5815 if [target_info exists gdb,skip_float_tests] {
5819 # There is an ARM kernel ptrace bug that hardware VFP registers
5820 # are not updated after GDB ptrace set VFP registers. The bug
5821 # was introduced by kernel commit 8130b9d7b9d858aa04ce67805e8951e3cb6e9b2f
5822 # in 2012 and is fixed in e2dfb4b880146bfd4b6aa8e138c0205407cebbaf
5823 # in May 2016. In other words, kernels older than 4.6.3, 4.4.14,
5824 # 4.1.27, 3.18.36, and 3.14.73 have this bug.
5825 # This kernel bug is detected by check how does GDB change the
5826 # program result by changing one VFP register.
5827 if { [istarget "arm*-*-linux*"] } {
5829 set compile_flags {debug nowarnings }
5831 # Set up, compile, and execute a test program having VFP
5833 set src [standard_temp_file arm_vfp[pid].c]
5834 set exe [standard_temp_file arm_vfp[pid].x]
5836 gdb_produce_source $src {
5841 asm ("vldr d0, [%0]" : : "r" (&d));
5842 asm ("vldr d1, [%0]" : : "r" (&d));
5843 asm (".global break_here\n"
5845 asm ("vcmp.f64 d0, d1\n"
5846 "vmrs APSR_nzcv, fpscr\n"
5847 "bne L_value_different\n"
5850 "L_value_different:\n"
5852 "L_end:\n" : "=r" (ret) :);
5854 /* Return $d0 != $d1. */
5859 verbose "compiling testfile $src" 2
5860 set lines [gdb_compile $src $exe executable $compile_flags]
5863 if ![string match "" $lines] then {
5864 verbose "testfile compilation failed, returning 1" 2
5868 # No error message, compilation succeeded so now run it via gdb.
5869 # Run the test up to 5 times to detect whether ptrace can
5870 # correctly update VFP registers or not.
5872 for {set i 0} {$i < 5} {incr i} {
5873 global gdb_prompt srcdir subdir
5877 gdb_reinitialize_dir $srcdir/$subdir
5881 gdb_test "break *break_here"
5882 gdb_continue_to_breakpoint "break_here"
5884 # Modify $d0 to a different value, so the exit code should
5886 gdb_test "set \$d0 = 5.0"
5888 set test "continue to exit"
5889 gdb_test_multiple "continue" "$test" {
5890 -re "exited with code 01.*$gdb_prompt $" {
5892 -re "exited normally.*$gdb_prompt $" {
5893 # However, the exit code is 0. That means something
5894 # wrong in setting VFP registers.
5902 remote_file build delete $exe
5904 return $skip_vfp_test
5909 # Print a message and return true if a test should be skipped
5910 # due to lack of stdio support.
5912 proc gdb_skip_stdio_test { msg } {
5913 if [target_info exists gdb,noinferiorio] {
5914 verbose "Skipping test '$msg': no inferior i/o."
5920 proc gdb_skip_bogus_test { msg } {
5924 # Return true if a test should be skipped due to lack of XML support
5926 # NOTE: This must be called while gdb is *not* running.
5928 gdb_caching_proc gdb_skip_xml_test {
5933 if { [info exists gdb_spawn_id] } {
5934 error "GDB must not be running in gdb_skip_xml_tests."
5937 set xml_file [gdb_remote_download host "${srcdir}/gdb.xml/trivial.xml"]
5941 gdb_test_multiple "set tdesc filename $xml_file" "" {
5942 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
5945 -re ".*$gdb_prompt $" { }
5951 # Return true if argv[0] is available.
5953 gdb_caching_proc gdb_has_argv0 {
5956 # Compile and execute a test program to check whether argv[0] is available.
5957 gdb_simple_compile has_argv0 {
5958 int main (int argc, char **argv) {
5965 proc gdb_has_argv0_1 { exe } {
5966 global srcdir subdir
5967 global gdb_prompt hex
5971 gdb_reinitialize_dir $srcdir/$subdir
5974 # Set breakpoint on main.
5975 gdb_test_multiple "break -q main" "break -q main" {
5976 -re "Breakpoint.*${gdb_prompt} $" {
5978 -re "${gdb_prompt} $" {
5985 gdb_test_multiple "" "run to main" {
5986 -re "Breakpoint.*${gdb_prompt} $" {
5988 -re "${gdb_prompt} $" {
5993 set old_elements "200"
5994 set test "show print elements"
5995 gdb_test_multiple $test $test {
5996 -re "Limit on string chars or array elements to print is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
5997 set old_elements $expect_out(1,string)
6000 set old_repeats "200"
6001 set test "show print repeats"
6002 gdb_test_multiple $test $test {
6003 -re "Threshold for repeated print elements is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
6004 set old_repeats $expect_out(1,string)
6007 gdb_test_no_output "set print elements unlimited" ""
6008 gdb_test_no_output "set print repeats unlimited" ""
6011 # Check whether argc is 1.
6012 gdb_test_multiple "p argc" "p argc" {
6013 -re " = 1\r\n${gdb_prompt} $" {
6015 gdb_test_multiple "p argv\[0\]" "p argv\[0\]" {
6016 -re " = $hex \".*[file tail $exe]\"\r\n${gdb_prompt} $" {
6019 -re "${gdb_prompt} $" {
6023 -re "${gdb_prompt} $" {
6027 gdb_test_no_output "set print elements $old_elements" ""
6028 gdb_test_no_output "set print repeats $old_repeats" ""
6033 set result [gdb_has_argv0_1 $obj]
6039 && ([istarget *-*-linux*]
6040 || [istarget *-*-freebsd*] || [istarget *-*-kfreebsd*]
6041 || [istarget *-*-netbsd*] || [istarget *-*-knetbsd*]
6042 || [istarget *-*-openbsd*]
6043 || [istarget *-*-darwin*]
6044 || [istarget *-*-solaris*]
6045 || [istarget *-*-aix*]
6046 || [istarget *-*-gnu*]
6047 || [istarget *-*-cygwin*] || [istarget *-*-mingw32*]
6048 || [istarget *-*-*djgpp*] || [istarget *-*-go32*]
6049 || [istarget *-wince-pe] || [istarget *-*-mingw32ce*]
6050 || [istarget *-*-symbianelf*]
6051 || [istarget *-*-osf*]
6052 || [istarget *-*-dicos*]
6053 || [istarget *-*-nto*]
6054 || [istarget *-*-*vms*]
6055 || [istarget *-*-lynx*178]) } {
6056 fail "argv\[0\] should be available on this target"
6062 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
6063 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
6064 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
6065 # the name of a debuginfo only file. This file will be stored in the same
6068 # Functions for separate debug info testing
6070 # starting with an executable:
6071 # foo --> original executable
6073 # at the end of the process we have:
6074 # foo.stripped --> foo w/o debug info
6075 # foo.debug --> foo's debug info
6076 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
6078 # Fetch the build id from the file.
6079 # Returns "" if there is none.
6081 proc get_build_id { filename } {
6082 if { ([istarget "*-*-mingw*"]
6083 || [istarget *-*-cygwin*]) } {
6084 set objdump_program [gdb_find_objdump]
6085 set result [catch {set data [exec $objdump_program -p $filename | grep signature | cut "-d " -f4]} output]
6086 verbose "result is $result"
6087 verbose "output is $output"
6093 set tmp [standard_output_file "${filename}-tmp"]
6094 set objcopy_program [gdb_find_objcopy]
6095 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $filename $tmp" output]
6096 verbose "result is $result"
6097 verbose "output is $output"
6102 fconfigure $fi -translation binary
6103 # Skip the NOTE header.
6108 if ![string compare $data ""] then {
6111 # Convert it to hex.
6112 binary scan $data H* data
6117 # Return the build-id hex string (usually 160 bits as 40 hex characters)
6118 # converted to the form: .build-id/ab/cdef1234...89.debug
6119 # Return "" if no build-id found.
6120 proc build_id_debug_filename_get { filename } {
6121 set data [get_build_id $filename]
6122 if { $data == "" } {
6125 regsub {^..} $data {\0/} data
6126 return ".build-id/${data}.debug"
6129 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
6130 # list of optional flags. The only currently supported flag is no-main,
6131 # which removes the symbol entry for main from the separate debug file.
6133 # Function returns zero on success. Function will return non-zero failure code
6134 # on some targets not supporting separate debug info (such as i386-msdos).
6136 proc gdb_gnu_strip_debug { dest args } {
6138 # Use the first separate debug info file location searched by GDB so the
6139 # run cannot be broken by some stale file searched with higher precedence.
6140 set debug_file "${dest}.debug"
6142 set strip_to_file_program [transform strip]
6143 set objcopy_program [gdb_find_objcopy]
6145 set debug_link [file tail $debug_file]
6146 set stripped_file "${dest}.stripped"
6148 # Get rid of the debug info, and store result in stripped_file
6149 # something like gdb/testsuite/gdb.base/blah.stripped.
6150 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
6151 verbose "result is $result"
6152 verbose "output is $output"
6157 # Workaround PR binutils/10802:
6158 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
6159 set perm [file attributes ${dest} -permissions]
6160 file attributes ${stripped_file} -permissions $perm
6162 # Get rid of everything but the debug info, and store result in debug_file
6163 # This will be in the .debug subdirectory, see above.
6164 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
6165 verbose "result is $result"
6166 verbose "output is $output"
6171 # If no-main is passed, strip the symbol for main from the separate
6172 # file. This is to simulate the behavior of elfutils's eu-strip, which
6173 # leaves the symtab in the original file only. There's no way to get
6174 # objcopy or strip to remove the symbol table without also removing the
6175 # debugging sections, so this is as close as we can get.
6176 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
6177 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
6178 verbose "result is $result"
6179 verbose "output is $output"
6183 file delete "${debug_file}"
6184 file rename "${debug_file}-tmp" "${debug_file}"
6187 # Link the two previous output files together, adding the .gnu_debuglink
6188 # section to the stripped_file, containing a pointer to the debug_file,
6189 # save the new file in dest.
6190 # This will be the regular executable filename, in the usual location.
6191 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
6192 verbose "result is $result"
6193 verbose "output is $output"
6198 # Workaround PR binutils/10802:
6199 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
6200 set perm [file attributes ${stripped_file} -permissions]
6201 file attributes ${dest} -permissions $perm
6206 # Test the output of GDB_COMMAND matches the pattern obtained
6207 # by concatenating all elements of EXPECTED_LINES. This makes
6208 # it possible to split otherwise very long string into pieces.
6209 # If third argument TESTNAME is not empty, it's used as the name of the
6210 # test to be printed on pass/fail.
6211 proc help_test_raw { gdb_command expected_lines {testname {}} } {
6212 set expected_output [join $expected_lines ""]
6213 if {$testname != {}} {
6214 gdb_test "${gdb_command}" "${expected_output}" $testname
6218 gdb_test "${gdb_command}" "${expected_output}"
6221 # A regexp that matches the end of help CLASS|PREFIX_COMMAND
6222 set help_list_trailer {
6223 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n]+"
6224 "Type \"apropos -v word\" for full documentation of commands related to \"word\"\.[\r\n]+"
6225 "Command name abbreviations are allowed if unambiguous\."
6228 # Test the output of "help COMMAND_CLASS". EXPECTED_INITIAL_LINES
6229 # are regular expressions that should match the beginning of output,
6230 # before the list of commands in that class.
6231 # LIST_OF_COMMANDS are regular expressions that should match the
6232 # list of commands in that class. If empty, the command list will be
6233 # matched automatically. The presence of standard epilogue will be tested
6235 # If last argument TESTNAME is not empty, it's used as the name of the
6236 # test to be printed on pass/fail.
6237 # Notice that the '[' and ']' characters don't need to be escaped for strings
6238 # wrapped in {} braces.
6239 proc test_class_help { command_class expected_initial_lines {list_of_commands {}} {testname {}} } {
6240 global help_list_trailer
6241 if {[llength $list_of_commands]>0} {
6242 set l_list_of_commands {"List of commands:[\r\n]+[\r\n]+"}
6243 set l_list_of_commands [concat $l_list_of_commands $list_of_commands]
6244 set l_list_of_commands [concat $l_list_of_commands {"[\r\n]+[\r\n]+"}]
6246 set l_list_of_commands {"List of commands\:.*[\r\n]+"}
6249 "Type \"help\" followed by command name for full documentation\.[\r\n]+"
6251 set l_entire_body [concat $expected_initial_lines $l_list_of_commands \
6252 $l_stock_body $help_list_trailer]
6254 help_test_raw "help ${command_class}" $l_entire_body $testname
6257 # Like test_class_help but specialised to test "help user-defined".
6258 proc test_user_defined_class_help { {list_of_commands {}} {testname {}} } {
6259 test_class_help "user-defined" {
6260 "User-defined commands\.[\r\n]+"
6261 "The commands in this class are those defined by the user\.[\r\n]+"
6262 "Use the \"define\" command to define a command\.[\r\n]+"
6263 } $list_of_commands $testname
6267 # COMMAND_LIST should have either one element -- command to test, or
6268 # two elements -- abbreviated command to test, and full command the first
6269 # element is abbreviation of.
6270 # The command must be a prefix command. EXPECTED_INITIAL_LINES
6271 # are regular expressions that should match the beginning of output,
6272 # before the list of subcommands. The presence of
6273 # subcommand list and standard epilogue will be tested automatically.
6274 proc test_prefix_command_help { command_list expected_initial_lines args } {
6275 global help_list_trailer
6276 set command [lindex $command_list 0]
6277 if {[llength $command_list]>1} {
6278 set full_command [lindex $command_list 1]
6280 set full_command $command
6282 # Use 'list' and not just {} because we want variables to
6283 # be expanded in this list.
6284 set l_stock_body [list\
6285 "List of $full_command subcommands\:.*\[\r\n\]+"\
6286 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"]
6287 set l_entire_body [concat $expected_initial_lines $l_stock_body $help_list_trailer]
6288 if {[llength $args]>0} {
6289 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
6291 help_test_raw "help ${command}" $l_entire_body
6295 # Build executable named EXECUTABLE from specifications that allow
6296 # different options to be passed to different sub-compilations.
6297 # TESTNAME is the name of the test; this is passed to 'untested' if
6299 # OPTIONS is passed to the final link, using gdb_compile. If OPTIONS
6300 # contains the option "pthreads", then gdb_compile_pthreads is used.
6301 # ARGS is a flat list of source specifications, of the form:
6302 # { SOURCE1 OPTIONS1 [ SOURCE2 OPTIONS2 ]... }
6303 # Each SOURCE is compiled to an object file using its OPTIONS,
6304 # using gdb_compile.
6305 # Returns 0 on success, -1 on failure.
6306 proc build_executable_from_specs {testname executable options args} {
6310 set binfile [standard_output_file $executable]
6313 if { [lsearch -exact $options "c++"] >= 0 } {
6314 set info_options "c++"
6316 if [get_compiler_info ${info_options}] {
6320 set func gdb_compile
6321 set func_index [lsearch -regexp $options {^(pthreads|shlib|shlib_pthreads|openmp)$}]
6322 if {$func_index != -1} {
6323 set func "${func}_[lindex $options $func_index]"
6326 # gdb_compile_shlib and gdb_compile_shlib_pthreads do not use the 3rd
6327 # parameter. They also requires $sources while gdb_compile and
6328 # gdb_compile_pthreads require $objects. Moreover they ignore any options.
6329 if [string match gdb_compile_shlib* $func] {
6331 foreach {s local_options} $args {
6332 if { [regexp "^/" "$s"] } then {
6333 lappend sources_path "$s"
6335 lappend sources_path "$srcdir/$subdir/$s"
6338 set ret [$func $sources_path "${binfile}" $options]
6339 } elseif {[lsearch -exact $options rust] != -1} {
6341 foreach {s local_options} $args {
6342 if { [regexp "^/" "$s"] } then {
6343 lappend sources_path "$s"
6345 lappend sources_path "$srcdir/$subdir/$s"
6348 set ret [gdb_compile_rust $sources_path "${binfile}" $options]
6352 foreach {s local_options} $args {
6353 if { ! [regexp "^/" "$s"] } then {
6354 set s "$srcdir/$subdir/$s"
6356 if { [$func "${s}" "${binfile}${i}.o" object $local_options] != "" } {
6360 lappend objects "${binfile}${i}.o"
6363 set ret [$func $objects "${binfile}" executable $options]
6373 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
6374 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
6375 # to pass to untested, if something is wrong. OPTIONS are passed
6376 # to gdb_compile directly.
6377 proc build_executable { testname executable {sources ""} {options {debug}} } {
6378 if {[llength $sources]==0} {
6379 set sources ${executable}.c
6382 set arglist [list $testname $executable $options]
6383 foreach source $sources {
6384 lappend arglist $source $options
6387 return [eval build_executable_from_specs $arglist]
6390 # Starts fresh GDB binary and loads an optional executable into GDB.
6391 # Usage: clean_restart [executable]
6392 # EXECUTABLE is the basename of the binary.
6393 # Return -1 if starting gdb or loading the executable failed.
6395 proc clean_restart { args } {
6401 if { [llength $args] > 1 } {
6402 error "bad number of args: [llength $args]"
6407 # This is a clean restart, so reset error and warning count.
6412 # if { [gdb_start] == -1 } {
6415 # but gdb_start is a ${tool}_start proc, which doesn't have a defined
6416 # return value. So instead, we test for errcnt.
6418 if { $errcnt > 0 } {
6422 gdb_reinitialize_dir $srcdir/$subdir
6424 if { [llength $args] >= 1 } {
6425 set executable [lindex $args 0]
6426 set binfile [standard_output_file ${executable}]
6427 return [gdb_load ${binfile}]
6433 # Prepares for testing by calling build_executable_full, then
6435 # TESTNAME is the name of the test.
6436 # Each element in ARGS is a list of the form
6437 # { EXECUTABLE OPTIONS SOURCE_SPEC... }
6438 # These are passed to build_executable_from_specs, which see.
6439 # The last EXECUTABLE is passed to clean_restart.
6440 # Returns 0 on success, non-zero on failure.
6441 proc prepare_for_testing_full {testname args} {
6442 foreach spec $args {
6443 if {[eval build_executable_from_specs [list $testname] $spec] == -1} {
6446 set executable [lindex $spec 0]
6448 clean_restart $executable
6452 # Prepares for testing, by calling build_executable, and then clean_restart.
6453 # Please refer to build_executable for parameter description.
6454 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
6456 if {[build_executable $testname $executable $sources $options] == -1} {
6459 clean_restart $executable
6464 # Retrieve the value of EXP in the inferior, represented in format
6465 # specified in FMT (using "printFMT"). DEFAULT is used as fallback if
6466 # print fails. TEST is the test message to use. It can be omitted,
6467 # in which case a test message is built from EXP.
6469 proc get_valueof { fmt exp default {test ""} } {
6473 set test "get valueof \"${exp}\""
6477 gdb_test_multiple "print${fmt} ${exp}" "$test" {
6478 -re "\\$\[0-9\]* = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
6479 set val $expect_out(1,string)
6483 fail "$test (timeout)"
6489 # Retrieve the value of local var EXP in the inferior. DEFAULT is used as
6490 # fallback if print fails. TEST is the test message to use. It can be
6491 # omitted, in which case a test message is built from EXP.
6493 proc get_local_valueof { exp default {test ""} } {
6497 set test "get local valueof \"${exp}\""
6501 gdb_test_multiple "info locals ${exp}" "$test" {
6502 -re "$exp = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
6503 set val $expect_out(1,string)
6507 fail "$test (timeout)"
6513 # Retrieve the value of EXP in the inferior, as a signed decimal value
6514 # (using "print /d"). DEFAULT is used as fallback if print fails.
6515 # TEST is the test message to use. It can be omitted, in which case
6516 # a test message is built from EXP.
6518 proc get_integer_valueof { exp default {test ""} } {
6522 set test "get integer valueof \"${exp}\""
6526 gdb_test_multiple "print /d ${exp}" "$test" {
6527 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
6528 set val $expect_out(1,string)
6532 fail "$test (timeout)"
6538 # Retrieve the value of EXP in the inferior, as an hexadecimal value
6539 # (using "print /x"). DEFAULT is used as fallback if print fails.
6540 # TEST is the test message to use. It can be omitted, in which case
6541 # a test message is built from EXP.
6543 proc get_hexadecimal_valueof { exp default {test ""} } {
6547 set test "get hexadecimal valueof \"${exp}\""
6551 gdb_test_multiple "print /x ${exp}" $test {
6552 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
6553 set val $expect_out(1,string)
6560 # Retrieve the size of TYPE in the inferior, as a decimal value. DEFAULT
6561 # is used as fallback if print fails. TEST is the test message to use.
6562 # It can be omitted, in which case a test message is 'sizeof (TYPE)'.
6564 proc get_sizeof { type default {test ""} } {
6565 return [get_integer_valueof "sizeof (${type})" $default $test]
6568 proc get_target_charset { } {
6571 gdb_test_multiple "show target-charset" "" {
6572 -re "The target character set is \"auto; currently (\[^\"\]*)\".*$gdb_prompt $" {
6573 return $expect_out(1,string)
6575 -re "The target character set is \"(\[^\"\]*)\".*$gdb_prompt $" {
6576 return $expect_out(1,string)
6580 # Pick a reasonable default.
6581 warning "Unable to read target-charset."
6585 # Get the address of VAR.
6587 proc get_var_address { var } {
6588 global gdb_prompt hex
6590 # Match output like:
6592 # $5 = (int (*)()) 0
6593 # $6 = (int (*)()) 0x24 <function_bar>
6595 gdb_test_multiple "print &${var}" "get address of ${var}" {
6596 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $"
6598 pass "get address of ${var}"
6599 if { $expect_out(1,string) == "0" } {
6602 return $expect_out(1,string)
6609 # Return the frame number for the currently selected frame
6610 proc get_current_frame_number {{test_name ""}} {
6613 if { $test_name == "" } {
6614 set test_name "get current frame number"
6617 gdb_test_multiple "frame" $test_name {
6618 -re "#(\[0-9\]+) .*$gdb_prompt $" {
6619 set frame_num $expect_out(1,string)
6625 # Get the current value for remotetimeout and return it.
6626 proc get_remotetimeout { } {
6630 gdb_test_multiple "show remotetimeout" "" {
6631 -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" {
6632 return $expect_out(1,string)
6636 # Pick the default that gdb uses
6637 warning "Unable to read remotetimeout"
6641 # Set the remotetimeout to the specified timeout. Nothing is returned.
6642 proc set_remotetimeout { timeout } {
6645 gdb_test_multiple "set remotetimeout $timeout" "" {
6646 -re "$gdb_prompt $" {
6647 verbose "Set remotetimeout to $timeout\n"
6652 # Get the target's current endianness and return it.
6653 proc get_endianness { } {
6656 gdb_test_multiple "show endian" "determine endianness" {
6657 -re ".* (little|big) endian.*\r\n$gdb_prompt $" {
6659 return $expect_out(1,string)
6665 # ROOT and FULL are file names. Returns the relative path from ROOT
6666 # to FULL. Note that FULL must be in a subdirectory of ROOT.
6667 # For example, given ROOT = /usr/bin and FULL = /usr/bin/ls, this
6670 proc relative_filename {root full} {
6671 set root_split [file split $root]
6672 set full_split [file split $full]
6674 set len [llength $root_split]
6676 if {[eval file join $root_split]
6677 != [eval file join [lrange $full_split 0 [expr {$len - 1}]]]} {
6678 error "$full not a subdir of $root"
6681 return [eval file join [lrange $full_split $len end]]
6684 # If GDB_PARALLEL exists, then set up the parallel-mode directories.
6685 if {[info exists GDB_PARALLEL]} {
6686 if {[is_remote host]} {
6690 [make_gdb_parallel_path outputs] \
6691 [make_gdb_parallel_path temp] \
6692 [make_gdb_parallel_path cache]
6696 proc core_find {binfile {deletefiles {}} {arg ""}} {
6697 global objdir subdir
6699 set destcore "$binfile.core"
6700 file delete $destcore
6702 # Create a core file named "$destcore" rather than just "core", to
6703 # avoid problems with sys admin types that like to regularly prune all
6704 # files named "core" from the system.
6706 # Arbitrarily try setting the core size limit to "unlimited" since
6707 # this does not hurt on systems where the command does not work and
6708 # allows us to generate a core on systems where it does.
6710 # Some systems append "core" to the name of the program; others append
6711 # the name of the program to "core"; still others (like Linux, as of
6712 # May 2003) create cores named "core.PID". In the latter case, we
6713 # could have many core files lying around, and it may be difficult to
6714 # tell which one is ours, so let's run the program in a subdirectory.
6716 set coredir [standard_output_file coredir.[getpid]]
6718 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
6719 # remote_exec host "${binfile}"
6720 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
6721 if [remote_file build exists $i] {
6722 remote_exec build "mv $i $destcore"
6726 # Check for "core.PID".
6727 if { $found == 0 } {
6728 set names [glob -nocomplain -directory $coredir core.*]
6729 if {[llength $names] == 1} {
6730 set corefile [file join $coredir [lindex $names 0]]
6731 remote_exec build "mv $corefile $destcore"
6735 if { $found == 0 } {
6736 # The braindamaged HPUX shell quits after the ulimit -c above
6737 # without executing ${binfile}. So we try again without the
6738 # ulimit here if we didn't find a core file above.
6739 # Oh, I should mention that any "braindamaged" non-Unix system has
6740 # the same problem. I like the cd bit too, it's really neat'n stuff.
6741 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
6742 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
6743 if [remote_file build exists $i] {
6744 remote_exec build "mv $i $destcore"
6750 # Try to clean up after ourselves.
6751 foreach deletefile $deletefiles {
6752 remote_file build delete [file join $coredir $deletefile]
6754 remote_exec build "rmdir $coredir"
6756 if { $found == 0 } {
6757 warning "can't generate a core file - core tests suppressed - check ulimit -c"
6763 # gdb_target_symbol_prefix compiles a test program and then examines
6764 # the output from objdump to determine the prefix (such as underscore)
6765 # for linker symbol prefixes.
6767 gdb_caching_proc gdb_target_symbol_prefix {
6768 # Compile a simple test program...
6769 set src { int main() { return 0; } }
6770 if {![gdb_simple_compile target_symbol_prefix $src executable]} {
6776 set objdump_program [gdb_find_objdump]
6777 set result [catch "exec $objdump_program --syms $obj" output]
6780 && ![regexp -lineanchor \
6781 { ([^ a-zA-Z0-9]*)main$} $output dummy prefix] } {
6782 verbose "gdb_target_symbol_prefix: Could not find main in objdump output; returning null prefix" 2
6790 # Return 1 if target supports scheduler locking, otherwise return 0.
6792 gdb_caching_proc target_supports_scheduler_locking {
6795 set me "gdb_target_supports_scheduler_locking"
6797 set src { int main() { return 0; } }
6798 if {![gdb_simple_compile $me $src executable]} {
6807 set supports_schedule_locking -1
6808 set current_schedule_locking_mode ""
6810 set test "reading current scheduler-locking mode"
6811 gdb_test_multiple "show scheduler-locking" $test {
6812 -re "Mode for locking scheduler during execution is \"(\[\^\"\]*)\".*$gdb_prompt" {
6813 set current_schedule_locking_mode $expect_out(1,string)
6815 -re "$gdb_prompt $" {
6816 set supports_schedule_locking 0
6819 set supports_schedule_locking 0
6823 if { $supports_schedule_locking == -1 } {
6824 set test "checking for scheduler-locking support"
6825 gdb_test_multiple "set scheduler-locking $current_schedule_locking_mode" $test {
6826 -re "Target '\[^'\]+' cannot support this command\..*$gdb_prompt $" {
6827 set supports_schedule_locking 0
6829 -re "$gdb_prompt $" {
6830 set supports_schedule_locking 1
6833 set supports_schedule_locking 0
6838 if { $supports_schedule_locking == -1 } {
6839 set supports_schedule_locking 0
6843 remote_file build delete $obj
6844 verbose "$me: returning $supports_schedule_locking" 2
6845 return $supports_schedule_locking
6848 # Return 1 if compiler supports use of nested functions. Otherwise,
6851 gdb_caching_proc support_nested_function_tests {
6852 # Compile a test program containing a nested function
6853 return [gdb_can_simple_compile nested_func {
6863 # gdb_target_symbol returns the provided symbol with the correct prefix
6864 # prepended. (See gdb_target_symbol_prefix, above.)
6866 proc gdb_target_symbol { symbol } {
6867 set prefix [gdb_target_symbol_prefix]
6868 return "${prefix}${symbol}"
6871 # gdb_target_symbol_prefix_flags_asm returns a string that can be
6872 # added to gdb_compile options to define the C-preprocessor macro
6873 # SYMBOL_PREFIX with a value that can be prepended to symbols
6874 # for targets which require a prefix, such as underscore.
6876 # This version (_asm) defines the prefix without double quotes
6877 # surrounding the prefix. It is used to define the macro
6878 # SYMBOL_PREFIX for assembly language files. Another version, below,
6879 # is used for symbols in inline assembler in C/C++ files.
6881 # The lack of quotes in this version (_asm) makes it possible to
6882 # define supporting macros in the .S file. (The version which
6883 # uses quotes for the prefix won't work for such files since it's
6884 # impossible to define a quote-stripping macro in C.)
6886 # It's possible to use this version (_asm) for C/C++ source files too,
6887 # but a string is usually required in such files; providing a version
6888 # (no _asm) which encloses the prefix with double quotes makes it
6889 # somewhat easier to define the supporting macros in the test case.
6891 proc gdb_target_symbol_prefix_flags_asm {} {
6892 set prefix [gdb_target_symbol_prefix]
6893 if {$prefix ne ""} {
6894 return "additional_flags=-DSYMBOL_PREFIX=$prefix"
6900 # gdb_target_symbol_prefix_flags returns the same string as
6901 # gdb_target_symbol_prefix_flags_asm, above, but with the prefix
6902 # enclosed in double quotes if there is a prefix.
6904 # See the comment for gdb_target_symbol_prefix_flags_asm for an
6905 # extended discussion.
6907 proc gdb_target_symbol_prefix_flags {} {
6908 set prefix [gdb_target_symbol_prefix]
6909 if {$prefix ne ""} {
6910 return "additional_flags=-DSYMBOL_PREFIX=\"$prefix\""
6916 # A wrapper for 'remote_exec host' that passes or fails a test.
6917 # Returns 0 if all went well, nonzero on failure.
6918 # TEST is the name of the test, other arguments are as for remote_exec.
6920 proc run_on_host { test program args } {
6921 verbose -log "run_on_host: $program $args"
6922 # remote_exec doesn't work properly if the output is set but the
6923 # input is the empty string -- so replace an empty input with
6925 if {[llength $args] > 1 && [lindex $args 1] == ""} {
6926 set args [lreplace $args 1 1 "/dev/null"]
6928 set result [eval remote_exec host [list $program] $args]
6929 verbose "result is $result"
6930 set status [lindex $result 0]
6931 set output [lindex $result 1]
6936 verbose -log "run_on_host failed: $output"
6942 # Return non-zero if "board_info debug_flags" mentions Fission.
6943 # http://gcc.gnu.org/wiki/DebugFission
6944 # Fission doesn't support everything yet.
6945 # This supports working around bug 15954.
6947 proc using_fission { } {
6948 set debug_flags [board_info [target_info name] debug_flags]
6949 return [regexp -- "-gsplit-dwarf" $debug_flags]
6952 # Search the caller's ARGS list and set variables according to the list of
6953 # valid options described by ARGSET.
6955 # The first member of each one- or two-element list in ARGSET defines the
6956 # name of a variable that will be added to the caller's scope.
6958 # If only one element is given to describe an option, it the value is
6959 # 0 if the option is not present in (the caller's) ARGS or 1 if
6962 # If two elements are given, the second element is the default value of
6963 # the variable. This is then overwritten if the option exists in ARGS.
6965 # Any parse_args elements in (the caller's) ARGS will be removed, leaving
6966 # any optional components.
6969 # proc myproc {foo args} {
6970 # parse_args {{bar} {baz "abc"} {qux}}
6973 # myproc ABC -bar -baz DEF peanut butter
6974 # will define the following variables in myproc:
6975 # foo (=ABC), bar (=1), baz (=DEF), and qux (=0)
6976 # args will be the list {peanut butter}
6978 proc parse_args { argset } {
6981 foreach argument $argset {
6982 if {[llength $argument] == 1} {
6983 # No default specified, so we assume that we should set
6984 # the value to 1 if the arg is present and 0 if it's not.
6985 # It is assumed that no value is given with the argument.
6986 set result [lsearch -exact $args "-$argument"]
6987 if {$result != -1} then {
6988 uplevel 1 [list set $argument 1]
6989 set args [lreplace $args $result $result]
6991 uplevel 1 [list set $argument 0]
6993 } elseif {[llength $argument] == 2} {
6994 # There are two items in the argument. The second is a
6995 # default value to use if the item is not present.
6996 # Otherwise, the variable is set to whatever is provided
6997 # after the item in the args.
6998 set arg [lindex $argument 0]
6999 set result [lsearch -exact $args "-[lindex $arg 0]"]
7000 if {$result != -1} then {
7001 uplevel 1 [list set $arg [lindex $args [expr $result+1]]]
7002 set args [lreplace $args $result [expr $result+1]]
7004 uplevel 1 [list set $arg [lindex $argument 1]]
7007 error "Badly formatted argument \"$argument\" in argument set"
7011 # The remaining args should be checked to see that they match the
7012 # number of items expected to be passed into the procedure...
7015 # Capture the output of COMMAND in a string ignoring PREFIX (a regexp);
7016 # return that string.
7018 proc capture_command_output { command prefix } {
7022 set output_string ""
7023 gdb_test_multiple "$command" "capture_command_output for $command" {
7024 -re "[string_to_regexp ${command}]\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $" {
7025 set output_string $expect_out(1,string)
7028 return $output_string
7031 # A convenience function that joins all the arguments together, with a
7032 # regexp that matches exactly one end of line in between each argument.
7033 # This function is ideal to write the expected output of a GDB command
7034 # that generates more than a couple of lines, as this allows us to write
7035 # each line as a separate string, which is easier to read by a human
7038 proc multi_line { args } {
7039 return [join $args "\r\n"]
7042 # Similar to the above, but while multi_line is meant to be used to
7043 # match GDB output, this one is meant to be used to build strings to
7044 # send as GDB input.
7046 proc multi_line_input { args } {
7047 return [join $args "\n"]
7050 # Return the version of the DejaGnu framework.
7052 # The return value is a list containing the major, minor and patch version
7053 # numbers. If the version does not contain a minor or patch number, they will
7054 # be set to 0. For example:
7060 proc dejagnu_version { } {
7061 # The frame_version variable is defined by DejaGnu, in runtest.exp.
7062 global frame_version
7064 verbose -log "DejaGnu version: $frame_version"
7065 verbose -log "Expect version: [exp_version]"
7066 verbose -log "Tcl version: [info tclversion]"
7068 set dg_ver [split $frame_version .]
7070 while { [llength $dg_ver] < 3 } {
7077 # Define user-defined command COMMAND using the COMMAND_LIST as the
7078 # command's definition. The terminating "end" is added automatically.
7080 proc gdb_define_cmd {command command_list} {
7083 set input [multi_line_input {*}$command_list "end"]
7084 set test "define $command"
7086 gdb_test_multiple "define $command" $test {
7088 gdb_test_multiple $input $test {
7089 -re "\r\n$gdb_prompt " {
7096 # Override the 'cd' builtin with a version that ensures that the
7097 # log file keeps pointing at the same file. We need this because
7098 # unfortunately the path to the log file is recorded using an
7099 # relative path name, and, we sometimes need to close/reopen the log
7100 # after changing the current directory. See get_compiler_info.
7102 rename cd builtin_cd
7106 # Get the existing log file flags.
7107 set log_file_info [log_file -info]
7109 # Split the flags into args and file name.
7110 set log_file_flags ""
7111 set log_file_file ""
7112 foreach arg [ split "$log_file_info" " "] {
7113 if [string match "-*" $arg] {
7114 lappend log_file_flags $arg
7116 lappend log_file_file $arg
7120 # If there was an existing file, ensure it is an absolute path, and then
7122 if { $log_file_file != "" } {
7123 set log_file_file [file normalize $log_file_file]
7125 log_file $log_file_flags "$log_file_file"
7128 # Call the builtin version of cd.
7132 # Return a list of all languages supported by GDB, suitable for use in
7133 # 'set language NAME'. This doesn't include either the 'local' or
7135 proc gdb_supported_languages {} {
7136 return [list c objective-c c++ d go fortran modula-2 asm pascal \
7137 opencl rust minimal ada]
7140 # Check if debugging is enabled for gdb.
7142 proc gdb_debug_enabled { } {
7145 # If not already read, get the debug setting from environment or board setting.
7146 if {![info exists gdbdebug]} {
7148 if [info exists env(GDB_DEBUG)] {
7149 set gdbdebug $env(GDB_DEBUG)
7150 } elseif [target_info exists gdb,debug] {
7151 set gdbdebug [target_info gdb,debug]
7157 # Ensure it not empty.
7158 return [expr { $gdbdebug != "" }]
7161 # Turn on debugging if enabled, or reset if already on.
7163 proc gdb_debug_init { } {
7167 if ![gdb_debug_enabled] {
7171 # First ensure logging is off.
7172 send_gdb "set logging off\n"
7174 set debugfile [standard_output_file gdb.debug]
7175 send_gdb "set logging file $debugfile\n"
7177 send_gdb "set logging debugredirect\n"
7180 foreach entry [split $gdbdebug ,] {
7181 send_gdb "set debug $entry 1\n"
7184 # Now that everything is set, enable logging.
7185 send_gdb "set logging on\n"
7187 -re "Copying output to $debugfile.*Redirecting debug output to $debugfile.*$gdb_prompt $" {}
7188 timeout { warning "Couldn't set logging file" }
7192 # Check if debugging is enabled for gdbserver.
7194 proc gdbserver_debug_enabled { } {
7195 # Always disabled for GDB only setups.
7199 # Open the file for logging gdb input
7201 proc gdb_stdin_log_init { } {
7202 gdb_persistent_global in_file
7204 if {[info exists in_file]} {
7205 # Close existing file.
7206 catch "close $in_file"
7209 set logfile [standard_output_file_with_gdb_instance gdb.in]
7210 set in_file [open $logfile w]
7213 # Write to the file for logging gdb input.
7214 # TYPE can be one of the following:
7215 # "standard" : Default. Standard message written to the log
7216 # "answer" : Answer to a question (eg "Y"). Not written the log.
7217 # "optional" : Optional message. Not written to the log.
7219 proc gdb_stdin_log_write { message {type standard} } {
7222 if {![info exists in_file]} {
7226 # Check message types.
7227 switch -regexp -- $type {
7236 # Write to the log and make sure the output is there, even in case
7238 puts -nonewline $in_file "$message"
7242 # Write the command line used to invocate gdb to the cmd file.
7244 proc gdb_write_cmd_file { cmdline } {
7245 set logfile [standard_output_file_with_gdb_instance gdb.cmd]
7246 set cmd_file [open $logfile w]
7247 puts $cmd_file $cmdline
7248 catch "close $cmd_file"
7251 # Compare contents of FILE to string STR. Pass with MSG if equal, otherwise
7254 proc cmp_file_string { file str msg } {
7255 if { ![file exists $file]} {
7260 set caught_error [catch {
7261 set fp [open "$file" r]
7262 set file_contents [read $fp]
7265 if { $caught_error } then {
7266 error "$error_message"
7271 if { $file_contents == $str } {
7278 # Does the compiler support CTF debug output using '-gt' compiler
7279 # flag? If not then we should skip these tests. We should also
7280 # skip them if libctf was explicitly disabled.
7282 gdb_caching_proc skip_ctf_tests {
7283 global enable_libctf
7285 if {$enable_libctf eq "no"} {
7289 return ![gdb_can_simple_compile ctfdebug {
7293 } executable "additional_flags=-gt"]
7296 # Return 1 if compiler supports -gstatement-frontiers. Otherwise,
7299 gdb_caching_proc supports_statement_frontiers {
7300 return [gdb_can_simple_compile supports_statement_frontiers {
7304 } executable "additional_flags=-gstatement-frontiers"]
7307 # Return 1 if compiler supports -mmpx -fcheck-pointer-bounds. Otherwise,
7310 gdb_caching_proc supports_mpx_check_pointer_bounds {
7311 set flags "additional_flags=-mmpx additional_flags=-fcheck-pointer-bounds"
7312 return [gdb_can_simple_compile supports_mpx_check_pointer_bounds {
7316 } executable $flags]
7319 # Return 1 if compiler supports -fcf-protection=. Otherwise,
7322 gdb_caching_proc supports_fcf_protection {
7323 return [gdb_can_simple_compile supports_fcf_protection {
7327 } executable "additional_flags=-fcf-protection=full"]
7330 # Return 1 if symbols were read in using -readnow. Otherwise, return 0.
7332 proc readnow { args } {
7333 if { [llength $args] == 1 } {
7334 set re [lindex $args 0]
7338 set cmd "maint print objfiles $re"
7339 gdb_test_multiple $cmd "" {
7340 -re -wrap "\r\n.gdb_index: faked for \"readnow\"\r\n.*" {
7351 # Return 1 if partial symbols are available. Otherwise, return 0.
7353 proc psymtabs_p { } {
7356 set cmd "maint info psymtab"
7357 gdb_test_multiple $cmd "" {
7358 -re "$cmd\r\n$gdb_prompt $" {
7369 # Verify that partial symtab expansion for $filename has state $readin.
7371 proc verify_psymtab_expanded { filename readin } {
7374 set cmd "maint info psymtab"
7375 set test "$cmd: $filename: $readin"
7376 set re [multi_line \
7377 " \{ psymtab \[^\r\n\]*$filename\[^\r\n\]*" \
7381 gdb_test_multiple $cmd $test {
7382 -re "$cmd\r\n$gdb_prompt $" {
7383 unsupported $gdb_test_name
7391 # Add a .gdb_index section to PROGRAM.
7392 # PROGRAM is assumed to be the output of standard_output_file.
7393 # Returns the 0 if there is a failure, otherwise 1.
7395 proc add_gdb_index { program } {
7396 global srcdir GDB env BUILD_DATA_DIRECTORY
7397 set contrib_dir "$srcdir/../contrib"
7398 set env(GDB) "$GDB --data-directory=$BUILD_DATA_DIRECTORY"
7399 set result [catch "exec $contrib_dir/gdb-add-index.sh $program" output]
7400 if { $result != 0 } {
7401 verbose -log "result is $result"
7402 verbose -log "output is $output"
7409 # Add a .gdb_index section to PROGRAM, unless it alread has an index
7410 # (.gdb_index/.debug_names). Gdb doesn't support building an index from a
7411 # program already using one. Return 1 if a .gdb_index was added, return 0
7412 # if it already contained an index, and -1 if an error occurred.
7414 proc ensure_gdb_index { binfile } {
7415 set testfile [file tail $binfile]
7416 set test "check if index present"
7417 gdb_test_multiple "mt print objfiles ${testfile}" $test {
7418 -re -wrap "gdb_index.*" {
7421 -re -wrap "debug_names.*" {
7424 -re -wrap "Psymtabs.*" {
7425 if { [add_gdb_index $binfile] != "1" } {
7434 # Return 1 if executable contains .debug_types section. Otherwise, return 0.
7436 proc debug_types { } {
7439 set cmd "maint info sections"
7440 gdb_test_multiple $cmd "" {
7441 -re -wrap "at $hex: .debug_types.*" {
7452 # Return the addresses in the line table for FILE for which is_stmt is true.
7454 proc is_stmt_addresses { file } {
7460 gdb_test_multiple "maint info line-table $file" "" {
7461 -re "\r\n$decimal\[ \t\]+$decimal\[ \t\]+($hex)\[ \t\]+Y\[^\r\n\]*" {
7462 lappend is_stmt $expect_out(1,string)
7472 # Return 1 if hex number VAL is an element of HEXLIST.
7474 proc hex_in_list { val hexlist } {
7475 # Normalize val by removing 0x prefix, and leading zeros.
7476 set val [regsub ^0x $val ""]
7477 set val [regsub ^0+ $val "0"]
7480 set index [lsearch -regexp $hexlist $re]
7481 return [expr $index != -1]
7484 # Override proc NAME to proc OVERRIDE for the duration of the execution of
7487 proc with_override { name override body } {
7488 # Implementation note: It's possible to implement the override using
7489 # rename, like this:
7490 # rename $name save_$name
7491 # rename $override $name
7492 # set code [catch {uplevel 1 $body} result]
7493 # rename $name $override
7494 # rename save_$name $name
7495 # but there are two issues here:
7496 # - the save_$name might clash with an existing proc
7497 # - the override is no longer available under its original name during
7499 # So, we use this more elaborate but cleaner mechanism.
7501 # Save the old proc.
7502 set old_args [info args $name]
7503 set old_body [info body $name]
7505 # Install the override.
7506 set new_args [info args $override]
7507 set new_body [info body $override]
7508 eval proc $name {$new_args} {$new_body}
7511 set code [catch {uplevel 1 $body} result]
7514 eval proc $name {$old_args} {$old_body}
7516 # Return as appropriate.
7518 global errorInfo errorCode
7519 return -code error -errorinfo $errorInfo -errorcode $errorCode $result
7520 } elseif { $code > 1 } {
7521 return -code $code $result
7527 # Setup tuiterm.exp environment. To be used in test-cases instead of
7528 # "load_lib tuiterm.exp". Calls initialization function and schedules
7529 # finalization function.
7530 proc tuiterm_env { } {
7531 load_lib tuiterm.exp
7533 # Do initialization.
7536 # Schedule finalization.
7537 global gdb_finish_hooks
7538 lappend gdb_finish_hooks tuiterm_env_finish
7541 # Dejagnu has a version of note, but usage is not allowed outside of dejagnu.
7542 # Define a local version.
7543 proc gdb_note { message } {
7544 verbose -- "NOTE: $message" 0
7547 # Return 1 if compiler supports -fuse-ld=gold, otherwise return 0.
7548 gdb_caching_proc have_fuse_ld_gold {
7549 set me "have_fuse_ld_gold"
7550 set flags "additional_flags=-fuse-ld=gold"
7551 set src { int main() { return 0; } }
7552 return [gdb_simple_compile $me $src executable $flags]
7555 # Always load compatibility stuff.