1 # Copyright 1999-2019 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 based on a file written by Fred Fish. (fnf@cygnus.com)
18 # Test setup routines that work with the MI interpreter.
20 load_lib gdb-utils.exp
22 # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
23 # Set it if it is not already set.
25 if ![info exists mi_gdb_prompt] then {
26 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
29 global mi_inferior_tty_name
31 # Always points to GDB's main UI spawn ID, even when testing with MI
32 # running on a secondary UI.
33 global gdb_main_spawn_id
35 # Points to the spawn id of the MI channel. When testing with MI
36 # running as the primary/main UI, this is the same as
37 # gdb_main_spawn_id, but will be different when testing with MI
38 # running on a secondary UI.
43 set thread_selected_re "=thread-selected,id=\"\[0-9\]+\"\r\n"
44 set gdbindex_warning_re "&\"warning: Skipping \[^\r\n\]+ \.gdb_index section in \[^\r\n\]+\"\r\n(?:&\"\\\\n\"\r\n)?"
45 set library_loaded_re "=library-loaded\[^\n\]+\"\r\n(?:$gdbindex_warning_re)?"
46 set breakpoint_re "=(?:breakpoint-created|breakpoint-deleted)\[^\n\]+\"\r\n"
49 # mi_gdb_exit -- exit the GDB, killing the target program if necessary
52 catch mi_uncatched_gdb_exit
55 proc mi_uncatched_gdb_exit {} {
57 global INTERNAL_GDBFLAGS GDBFLAGS
59 global gdb_spawn_id gdb_main_spawn_id
60 global mi_spawn_id inferior_spawn_id
65 gdb_stop_suppressing_tests
67 if { [info procs sid_exit] != "" } {
71 if ![info exists gdb_spawn_id] {
75 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
77 if { [is_remote host] && [board_info host exists fileid] } {
78 send_gdb "999-gdb-exit\n"
84 -re "Undefined command.*$gdb_prompt $" {
88 -re "DOSEXIT code" { }
93 if ![is_remote host] {
97 unset gdb_main_spawn_id
99 unset inferior_spawn_id
102 # Create the PTY for the inferior process and tell GDB about it.
104 proc mi_create_inferior_pty {} {
106 global inferior_spawn_id
107 global mi_inferior_tty_name
110 set inferior_spawn_id $spawn_id
111 set tty_name $spawn_out(slave,name)
112 set mi_inferior_tty_name $tty_name
114 send_gdb "102-inferior-tty-set $tty_name\n"
116 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
117 verbose "redirect inferior output to new terminal device."
120 warning "Couldn't redirect inferior output." 2
125 proc mi_gdb_start_separate_mi_tty { args } {
126 global gdb_prompt mi_gdb_prompt
128 global gdb_spawn_id gdb_main_spawn_id mi_spawn_id
129 global inferior_spawn_id
131 set separate_inferior_pty 0
134 if {$arg == "separate-inferior-tty"} {
135 set separate_inferior_pty 1
141 # Create the new PTY for the MI UI.
143 set mi_spawn_id $spawn_id
144 set mi_tty_name $spawn_out(slave,name)
145 gdb_test_multiple "new-ui mi $mi_tty_name" "new-ui" {
146 -re "New UI allocated\r\n$gdb_prompt $" {
150 # Switch to the MI channel.
151 set gdb_main_spawn_id $gdb_spawn_id
152 switch_gdb_spawn_id $mi_spawn_id
154 # Consume pending output and MI prompt.
156 -re "$mi_gdb_prompt$" {
159 perror "MI channel failed"
165 if {$separate_inferior_pty} {
166 mi_create_inferior_pty
175 # default_mi_gdb_start [FLAGS] -- start gdb running, default procedure
177 # If "separate-inferior-tty" is specified, the inferior works with
180 # If "separate-mi-tty" is specified, the gdb starts in CLI mode, with
181 # MI running on a secondary UI, on its own tty.
183 # When running over NFS, particularly if running many simultaneous
184 # tests on different hosts all using the same server, things can
185 # get really slow. Give gdb at least 3 minutes to start up.
187 proc default_mi_gdb_start { args } {
188 global verbose use_gdb_stub
190 global INTERNAL_GDBFLAGS GDBFLAGS
194 global gdb_spawn_id gdb_main_spawn_id inferior_spawn_id mi_spawn_id
196 global FORCE_SEPARATE_MI_TTY
198 if {[info exists FORCE_SEPARATE_MI_TTY]} {
199 set separate_mi_pty $FORCE_SEPARATE_MI_TTY
201 set separate_mi_pty 0
204 set separate_inferior_pty 0
207 if {$arg == "separate-mi-tty"} {
208 set separate_mi_pty 1
209 } elseif {$arg == "separate-inferior-tty"} {
210 set separate_inferior_pty 1
214 if {$separate_mi_pty} {
215 return [eval mi_gdb_start_separate_mi_tty $args]
218 gdb_stop_suppressing_tests
219 set inferior_pty no-tty
221 # Set the default value, it may be overriden later by specific testfile.
222 set use_gdb_stub [target_info exists use_gdb_stub]
225 if { [info procs sid_start] != "" } {
226 verbose "Spawning SID"
230 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
232 if [info exists gdb_spawn_id] {
236 if ![is_remote host] {
237 if { [which $GDB] == 0 } then {
238 perror "$GDB does not exist."
243 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS [host_info gdb_opts]"]
244 if { $res < 0 || $res == "" } {
245 perror "Spawning $GDB failed."
249 -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
250 # We have a new format mi startup prompt. If we are
251 # running mi1, then this is an error as we should be
252 # using the old-style prompt.
253 if { $MIFLAGS == "-i=mi1" } {
254 perror "(mi startup) Got unexpected new mi prompt."
258 verbose "GDB initialized."
260 -re "\[^~\].*$mi_gdb_prompt$" {
261 # We have an old format mi startup prompt. If we are
262 # not running mi1, then this is an error as we should be
263 # using the new-style prompt.
264 if { $MIFLAGS != "-i=mi1" } {
265 perror "(mi startup) Got unexpected old mi prompt."
269 verbose "GDB initialized."
271 -re ".*unrecognized option.*for a complete list of options." {
272 untested "skip mi tests (not compiled with mi support)."
276 -re ".*Interpreter `mi' unrecognized." {
277 untested "skip mi tests (not compiled with mi support)."
282 perror "(timeout) GDB never initialized after 10 seconds."
287 set gdb_spawn_id $res
288 set gdb_main_spawn_id $res
291 # FIXME: mi output does not go through pagers, so these can be removed.
292 # force the height to "unlimited", so no pagers get used
293 send_gdb "100-gdb-set height 0\n"
295 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
296 verbose "Setting height to 0." 2
299 warning "Couldn't set the height to 0"
302 # force the width to "unlimited", so no wraparound occurs
303 send_gdb "101-gdb-set width 0\n"
305 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
306 verbose "Setting width to 0." 2
309 warning "Couldn't set the width to 0."
313 if { $separate_inferior_pty } {
314 mi_create_inferior_pty
317 if {![info exists inferior_spawn_id]} {
318 set inferior_spawn_id $gdb_spawn_id
327 # Overridable function. You can override this function in your
330 proc mi_gdb_start { args } {
331 return [eval default_mi_gdb_start $args]
334 # Many of the tests depend on setting breakpoints at various places and
335 # running until that breakpoint is reached. At times, we want to start
336 # with a clean-slate with respect to breakpoints, so this utility proc
337 # lets us do this without duplicating this code everywhere.
340 proc mi_delete_breakpoints {} {
343 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
344 send_gdb "102-break-delete\n"
346 -re "Delete all breakpoints.*y or n.*$" {
350 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
351 # This happens if there were no breakpoints
353 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
356 # The correct output is not "No breakpoints or watchpoints." but an
357 # empty BreakpointTable. Also, a query is not acceptable with mi.
358 send_gdb "103-break-list\n"
360 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
361 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{nr_rows=\".\",nr_cols=\".\",hdr=\\\[\{width=\".*\",alignment=\".*\",col_name=\"number\",colhdr=\"Num\"\}.*colhdr=\"Type\".*colhdr=\"Disp\".*colhdr=\"Enb\".*colhdr=\"Address\".*colhdr=\"What\".*\\\],body=\\\[\\\]\}\r\n$mi_gdb_prompt$" {}
362 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
363 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
364 -re "Delete all breakpoints.*or n.*$" {
365 warning "Unexpected prompt for breakpoints deletion"
369 timeout { perror "-break-list (timeout)" ; return }
373 proc mi_gdb_reinitialize_dir { subdir } {
378 if { $suppress_flag } {
382 if [is_remote host] {
386 if { $MIFLAGS == "-i=mi1" } {
387 send_gdb "104-environment-directory\n"
389 -re ".*Reinitialize source path to empty.*y or n. " {
390 warning "Got confirmation prompt for dir reinitialization."
393 -re "$mi_gdb_prompt$" {}
394 timeout {error "Dir reinitialization failed (timeout)"}
397 -re "$mi_gdb_prompt$" {}
398 timeout {error "Dir reinitialization failed (timeout)"}
401 send_gdb "104-environment-directory -r\n"
403 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
404 -re "$mi_gdb_prompt$" {}
405 timeout {error "Dir reinitialization failed (timeout)"}
409 send_gdb "105-environment-directory $subdir\n"
411 -re "Source directories searched.*$mi_gdb_prompt$" {
412 verbose "Dir set to $subdir"
414 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
415 # FIXME: We return just the prompt for now.
416 verbose "Dir set to $subdir"
417 # perror "Dir \"$subdir\" failed."
422 # Send GDB the "target" command.
423 # FIXME: Some of these patterns are not appropriate for MI. Based on
424 # config/monitor.exp:gdb_target_command.
425 proc mi_gdb_target_cmd { targetname serialport } {
428 set serialport_re [string_to_regexp $serialport]
429 for {set i 1} {$i <= 3} {incr i} {
430 send_gdb "47-target-select $targetname $serialport\n"
432 -re "47\\^connected.*$mi_gdb_prompt" {
433 verbose "Set target to $targetname"
436 -re "unknown host.*$mi_gdb_prompt" {
437 verbose "Couldn't look up $serialport"
439 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
440 verbose "Connection failed"
442 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
443 verbose "Set target to $targetname"
446 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
447 verbose "Set target to $targetname"
450 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
451 verbose "Set target to $targetname"
454 -re "Connected to.*$mi_gdb_prompt$" {
455 verbose "Set target to $targetname"
458 -re "Ending remote.*$mi_gdb_prompt$" { }
459 -re "Connection refused.*$mi_gdb_prompt$" {
460 verbose "Connection refused by remote target. Pausing, and trying again."
464 -re "Non-stop mode requested, but remote does not support non-stop.*$mi_gdb_prompt" {
465 unsupported "non-stop mode not supported"
468 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
469 verbose "Got timeout error from gdb."
481 # load a file into the debugger (file command only).
482 # return a -1 if anything goes wrong.
484 proc mi_gdb_file_cmd { arg } {
490 global last_loaded_file
491 upvar timeout timeout
493 set last_loaded_file $arg
495 if [is_remote host] {
496 set arg [remote_download host $arg]
498 error "download failed"
503 # FIXME: Several of these patterns are only acceptable for console
504 # output. Queries are an error for mi.
505 send_gdb "105-file-exec-and-symbols $arg\n"
507 -re "Reading symbols from.*$mi_gdb_prompt$" {
508 verbose "\t\tLoaded $arg into the $GDB"
511 -re "has no symbol-table.*$mi_gdb_prompt$" {
512 perror "$arg wasn't compiled with \"-g\""
515 -re "Load new symbol table from \".*\".*y or n. $" {
518 -re "Reading symbols from.*$mi_gdb_prompt$" {
519 verbose "\t\tLoaded $arg with new symbol table into $GDB"
523 perror "(timeout) Couldn't load $arg, other program already loaded."
528 -re "No such file or directory.*$mi_gdb_prompt$" {
529 perror "($arg) No such file or directory\n"
532 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
533 # We (MI) are just giving the prompt back for now, instead of giving
534 # some acknowledgement.
538 perror "couldn't load $arg into $GDB (timed out)."
542 # This is an attempt to detect a core dump, but seems not to
543 # work. Perhaps we need to match .* followed by eof, in which
544 # gdb_expect does not seem to have a way to do that.
545 perror "couldn't load $arg into $GDB (end of file)."
552 # connect to the target and download a file, if necessary.
553 # return a -1 if anything goes wrong.
555 proc mi_gdb_target_load { } {
562 if [target_info exists gdb_load_timeout] {
563 set loadtimeout [target_info gdb_load_timeout]
568 if { [info procs gdbserver_gdb_load] != "" } {
569 mi_gdb_test "kill" ".*" ""
570 if { [catch gdbserver_gdb_load res] == 1 } {
574 set protocol [lindex $res 0]
575 set gdbport [lindex $res 1]
577 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
580 } elseif { [info procs send_target_sid] != "" } {
581 # For SID, things get complex
584 -re ".*$mi_gdb_prompt$"
587 gdb_expect $loadtimeout {
588 -re "\\^done.*$mi_gdb_prompt$" {
591 perror "Unable to connect to SID target (timeout)"
595 send_gdb "48-target-download\n"
596 gdb_expect $loadtimeout {
597 -re "48\\^done.*$mi_gdb_prompt$" {
600 perror "Unable to download to SID target (timeout)"
604 } elseif { [target_info protocol] == "sim" } {
605 set target_sim_options "[board_info target gdb,target_sim_options]"
606 # For the simulator, just connect to it directly.
607 send_gdb "47-target-select sim $target_sim_options\n"
608 gdb_expect $loadtimeout {
609 -re "47\\^connected.*$mi_gdb_prompt$" {
612 perror "Unable to select sim target (timeout)"
616 send_gdb "48-target-download\n"
617 gdb_expect $loadtimeout {
618 -re "48\\^done.*$mi_gdb_prompt$" {
621 perror "Unable to download to sim target (timeout)"
625 } elseif { [target_info gdb_protocol] == "remote" } {
627 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
628 perror "Unable to connect to remote target"
631 send_gdb "48-target-download\n"
632 gdb_expect $loadtimeout {
633 -re "48\\^done.*$mi_gdb_prompt$" {
636 perror "Unable to download to remote target (timeout)"
645 # load a file into the debugger.
646 # return a -1 if anything goes wrong.
648 proc mi_gdb_load { arg } {
650 return [mi_gdb_file_cmd $arg]
655 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
658 # COMMAND is the command to execute, send to GDB with send_gdb. If
659 # this is the null string no command is sent.
660 # PATTERN is the pattern to match for a PASS, and must NOT include
661 # the \r\n sequence immediately before the gdb prompt.
662 # MESSAGE is the message to be printed. (If this is the empty string,
663 # then sometimes we don't call pass or fail at all; I don't
664 # understand this at all.)
665 # IPATTERN is the pattern to match for the inferior's output. This parameter
666 # is optional. If present, it will produce a PASS if the match is
667 # successful, and a FAIL if unsuccessful.
670 # 1 if the test failed,
671 # 0 if the test passes,
672 # -1 if there was an internal error.
674 proc mi_gdb_test { args } {
677 global GDB expect_out
678 global inferior_exited_re async
679 upvar timeout timeout
681 set command [lindex $args 0]
682 set pattern [lindex $args 1]
683 set message [lindex $args 2]
685 if [llength $args]==4 {
686 set ipattern [lindex $args 3]
689 if [llength $args]==5 {
690 set question_string [lindex $args 3]
691 set response_string [lindex $args 4]
693 set question_string "^FOOBAR$"
697 send_user "Sending \"$command\" to gdb\n"
698 send_user "Looking to match \"$pattern\"\n"
699 send_user "Message is \"$message\"\n"
703 set string "${command}\n"
704 set string_regex [string_to_regexp $command]
706 if { $command != "" } {
707 while { "$string" != "" } {
708 set foo [string first "\n" "$string"]
709 set len [string length "$string"]
710 if { $foo < [expr $len - 1] } {
711 set str [string range "$string" 0 $foo]
712 if { [send_gdb "$str"] != "" } {
715 if { ! $suppress_flag } {
716 perror "Couldn't send $command to GDB."
725 set string [string range "$string" [expr $foo + 1] end]
730 if { "$string" != "" } {
731 if { [send_gdb "$string"] != "" } {
734 if { ! $suppress_flag } {
735 perror "Couldn't send $command to GDB."
743 if [info exists timeout] {
747 if [info exists timeout] {
754 # With $prompt_re "" there may come arbitrary asynchronous response
755 # from the previous command, before or after $string_regex.
756 set string_regex ".*"
758 verbose -log "Expecting: ^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)"
760 -re "\\*\\*\\* DOSEXIT code.*" {
761 if { $message != "" } {
764 gdb_suppress_entire_file "GDB died"
767 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
768 if ![isnative] then {
769 warning "Can`t communicate to remote target."
775 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
776 # At this point, $expect_out(1,string) is the MI input command.
777 # and $expect_out(2,string) is the MI output command.
778 # If $expect_out(1,string) is "", then there was no MI input command here.
780 # NOTE, there is no trailing anchor because with GDB/MI,
781 # asynchronous responses can happen at any point, causing more
782 # data to be available. Normally an anchor is used to make
783 # sure the end of the output is matched, however, $mi_gdb_prompt
784 # is just as good of an anchor since mi_gdb_test is meant to
785 # match a single mi output command. If a second GDB/MI output
786 # response is sent, it will be in the buffer for the next
787 # time mi_gdb_test is called.
788 if ![string match "" $message] then {
793 -re "(${question_string})$" {
794 send_gdb "$response_string\n"
797 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
798 perror "Undefined command \"$command\"."
802 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
803 perror "\"$command\" is not a unique command name."
807 -re "$inferior_exited_re with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
808 if ![string match "" $message] then {
809 set errmsg "$message (the program exited)"
811 set errmsg "$command (the program exited)"
816 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
817 if ![string match "" $message] then {
818 set errmsg "$message (the program is no longer running)"
820 set errmsg "$command (the program is no longer running)"
825 -re ".*$mi_gdb_prompt\[ \]*$" {
826 if ![string match "" $message] then {
827 fail "$message (unexpected output)"
833 perror "Window too small."
837 perror "Process no longer exists"
838 if { $message != "" } {
844 perror "internal buffer is full."
848 if ![string match "" $message] then {
849 fail "$message (timeout)"
855 # If the GDB output matched, compare the inferior output.
856 if { $result == 0 } {
857 if [ info exists ipattern ] {
858 if { ![target_info exists gdb,noinferiorio] } {
859 global gdb_spawn_id inferior_spawn_id
861 set sid "$inferior_spawn_id $gdb_spawn_id"
863 -i "$sid" -re "$ipattern" {
864 pass "$message inferior output"
867 fail "$message inferior output (timeout)"
872 unsupported "$message inferior output"
880 # Collect output sent to the console output stream until UNTIL is
881 # seen. UNTIL is a regular expression. MESSAGE is the message to be
882 # printed in case of timeout.
884 proc mi_gdb_expect_cli_output {until message} {
888 -re "~\"(\[^\r\n\]+)\"\r\n" {
889 append output $expect_out(1,string)
892 -notransfer -re "$until" {
896 fail "$message (timeout)"
905 # MI run command. (A modified version of gdb_run_cmd)
908 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
909 # ``.*$'' could swallow up output that we attempt to match elsewhere.
911 # Send the command to run the test program.
913 # If USE_MI_COMMAND is true, the "-exec-run" command is used.
914 # Otherwise, the "run" (CLI) command is used. If the global USE_GDB_STUB is
915 # true, -exec-continue and continue are used instead of their run counterparts.
917 # ARGS is passed as argument to the command used to run the test program.
918 # Beware that arguments to "-exec-run" do not have the same semantics as
919 # arguments to the "run" command, so USE_MI_COMMAND influences the meaning
920 # of ARGS. If USE_MI_COMMAND is true, they are arguments to -exec-run.
921 # If USE_MI_COMMAND is false, they are effectively arguments passed
922 # to the test program. If the global USE_GDB_STUB is true, ARGS is not used.
923 proc mi_run_cmd_full {use_mi_command args} {
925 if { $suppress_flag } {
928 global mi_gdb_prompt use_gdb_stub
929 global thread_selected_re
930 global library_loaded_re
932 if {$use_mi_command} {
933 set run_prefix "220-exec-"
940 foreach command [gdb_init_commands] {
941 send_gdb "$command\n"
943 -re "$mi_gdb_prompt$" { }
945 perror "gdb_init_command for target failed"
951 if { [mi_gdb_target_load] < 0 } {
956 if [target_info exists gdb,do_reload_on_run] {
957 send_gdb "${run_prefix}continue\n"
959 -re "${run_match}\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\r\n$mi_gdb_prompt" {}
960 -re "${run_match}\\^error.*$mi_gdb_prompt" {return -1}
966 if [target_info exists gdb,start_symbol] {
967 set start [target_info gdb,start_symbol]
972 # HACK: Should either use 000-jump or fix the target code
973 # to better handle RUN.
974 send_gdb "jump *$start\n"
975 warning "Using CLI jump command, expect run-to-main FAIL"
977 -re "&\"jump \\*${start}\\\\n\"\[\r\n\]+~\"Continuing at 0x\[0-9A-Fa-f\]+\.\\\\n\"\[\r\n\]+\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\[\r\n\]+${mi_gdb_prompt}" {}
979 perror "Unable to start target"
986 send_gdb "${run_prefix}run $args\n"
988 -re "${run_match}\\^running\r\n(\\*running,thread-id=\"\[^\"\]+\"\r\n|=thread-created,id=\"1\",group-id=\"\[0-9\]+\"\r\n)*(${library_loaded_re})*(${thread_selected_re})?${mi_gdb_prompt}" {
990 -re "\\^error,msg=\"The target does not support running in non-stop mode.\"" {
991 unsupported "non-stop mode not supported"
995 perror "Unable to start target"
999 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
1004 # A wrapper for mi_run_cmd_full which uses -exec-run and
1005 # -exec-continue, as appropriate. ARGS are passed verbatim to
1007 proc mi_run_cmd {args} {
1008 return [eval mi_run_cmd_full 1 $args]
1011 # A wrapper for mi_run_cmd_full which uses the CLI commands 'run' and
1012 # 'continue', as appropriate. ARGS are passed verbatim to
1014 proc mi_run_with_cli {args} {
1015 return [eval mi_run_cmd_full 0 $args]
1019 # Just like run-to-main but works with the MI interface
1022 proc mi_run_to_main { } {
1023 global suppress_flag
1024 if { $suppress_flag } {
1033 mi_delete_breakpoints
1034 mi_gdb_reinitialize_dir $srcdir/$subdir
1035 mi_gdb_load ${binfile}
1041 # Just like gdb's "runto" proc, it will run the target to a given
1042 # function. The big difference here between mi_runto and mi_execute_to
1043 # is that mi_execute_to must have the inferior running already. This
1044 # proc will (like gdb's runto) (re)start the inferior, too.
1046 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
1048 # -1 if test suppressed, failed, timedout
1051 proc mi_runto_helper {func run_or_continue} {
1052 global suppress_flag
1053 if { $suppress_flag } {
1057 global mi_gdb_prompt expect_out
1058 global hex decimal fullname_syntax
1060 set test "mi runto $func"
1061 set bp [mi_make_breakpoint -type breakpoint -disp del \
1062 -func $func\(\\\(.*\\\)\)?]
1063 mi_gdb_test "200-break-insert -t $func" "200\\^done,$bp" \
1064 "breakpoint at $func"
1066 if {$run_or_continue == "run"} {
1067 if { [mi_run_cmd] < 0 } {
1071 mi_send_resuming_command "exec-continue" "$test"
1074 mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test
1077 proc mi_runto {func} {
1078 return [mi_runto_helper $func "run"]
1081 # Next to the next statement
1082 # For return values, see mi_execute_to_helper
1084 proc mi_next { test } {
1085 return [mi_next_to {.*} {.*} {.*} {.*} $test]
1089 # Step to the next statement
1090 # For return values, see mi_execute_to_helper
1092 proc mi_step { test } {
1093 return [mi_step_to {.*} {.*} {.*} {.*} $test]
1098 proc mi_detect_async {} {
1100 global mi_gdb_prompt
1102 send_gdb "show mi-async\n"
1105 -re "asynchronous mode is on...*$mi_gdb_prompt$" {
1108 -re ".*$mi_gdb_prompt$" {
1118 # Wait for MI *stopped notification to appear.
1119 # The REASON, FUNC, ARGS, FILE and LINE are regular expressions
1120 # to match against whatever is output in *stopped. FILE may also match
1121 # filename of a file without debug info. ARGS should not include [] the
1122 # list of argument is enclosed in, and other regular expressions should
1123 # not include quotes.
1124 # If EXTRA is a list of one element, it's the regular expression
1125 # for output expected right after *stopped, and before GDB prompt.
1126 # If EXTRA is a list of two elements, the first element is for
1127 # output right after *stopped, and the second element is output
1128 # right after reason field. The regex after reason should not include
1129 # the comma separating it from the following fields.
1131 # When we fail to match output at all, -1 is returned. If FILE does
1132 # match and the target system has no debug info for FILE return 0.
1133 # Otherwise, the line at which we stop is returned. This is useful when
1134 # exact line is not possible to specify for some reason -- one can pass
1135 # the .* or "\[0-9\]*" regexps for line, and then check the line
1138 # Do not pass .* for any argument if you are expecting more than one stop.
1139 proc mi_expect_stop { reason func args file line extra test } {
1141 global mi_gdb_prompt
1144 global fullname_syntax
1146 global thread_selected_re
1147 global breakpoint_re
1151 set after_stopped ""
1153 if { [llength $extra] == 2 } {
1154 set after_stopped [lindex $extra 0]
1155 set after_reason [lindex $extra 1]
1156 set after_reason "${after_reason},"
1157 } elseif { [llength $extra] == 1 } {
1158 set after_stopped [lindex $extra 0]
1164 set prompt_re "$mi_gdb_prompt$"
1167 if { $reason == "really-no-reason" } {
1169 -re "\\*stopped\r\n$prompt_re" {
1173 fail "$test (timeout)"
1179 if { $reason == "exited-normally" } {
1182 -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re" {
1185 -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
1187 fail "$test (timeout)"
1192 if { $reason == "exited" } {
1194 -re "\\*stopped,reason=\"exited\",exit-code=\"\[0-7\]+\"\r\n$prompt_re" {
1197 -re ".*$mi_gdb_prompt$" {
1198 fail "$test (inferior not stopped)"
1201 fail "$test (timeout)"
1207 if { $reason == "solib-event" } {
1208 set pattern "\\*stopped,reason=\"solib-event\",thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1209 verbose -log "mi_expect_stop: expecting: $pattern"
1215 fail "$test (timeout)"
1221 set args "\\\[$args\\\]"
1225 if { $reason == "breakpoint-hit" } {
1226 set bn {bkptno="[0-9]+",}
1227 } elseif { $reason == "solib-event" } {
1229 } elseif { $reason == "exception-caught" } {
1230 set ebn {bkptno="[0-9]+",}
1232 set reason "breakpoint-hit"
1236 if { $reason != "" } {
1237 set r "reason=\"$reason\","
1243 verbose -log "mi_expect_stop: expecting: \\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1245 -re "\\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1247 if {[array names expect_out "2,string"] != ""} {
1248 return $expect_out(2,string)
1250 # No debug info available but $file does match.
1253 -re "\\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$any\",args=\[\\\[\{\]$any\[\\\]\}\],file=\"$any\",fullname=\"${fullname_syntax}$any\",line=\"\[0-9\]*\",arch=\"$any\"\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1254 verbose -log "got $expect_out(buffer)"
1255 fail "$test (stopped at wrong place)"
1258 -re ".*\r\n$mi_gdb_prompt$" {
1259 verbose -log "got $expect_out(buffer)"
1260 fail "$test (unknown output after running)"
1264 fail "$test (timeout)"
1270 # Wait for MI *stopped notification related to an interrupt request to
1272 proc mi_expect_interrupt { test } {
1273 global mi_gdb_prompt
1280 set prompt_re "$mi_gdb_prompt"
1283 set r_nonstop "reason=\"signal-received\",signal-name=\"0\",signal-meaning=\"Signal 0\""
1284 set r_allstop "reason=\"signal-received\",signal-name=\"SIGINT\",signal-meaning=\"Interrupt\""
1285 set r "(${r_nonstop}|${r_allstop})"
1288 # A signal can land anywhere, just ignore the location
1289 verbose -log "mi_expect_interrupt: expecting: \\*stopped,${r}$any\r\n$prompt_re"
1291 -re "\\*stopped,${r}$any\r\n$prompt_re" {
1295 -re ".*\r\n$mi_gdb_prompt" {
1296 verbose -log "got $expect_out(buffer)"
1297 fail "$test (unknown output after running)"
1301 fail "$test (timeout)"
1307 # cmd should not include the number or newline (i.e. "exec-step 3", not
1308 # "220-exec-step 3\n"
1310 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1311 # after the first prompt is printed.
1313 proc mi_execute_to { cmd reason func args file line extra test } {
1314 global suppress_flag
1315 if { $suppress_flag } {
1319 mi_send_resuming_command "$cmd" "$test"
1320 set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1324 proc mi_next_to { func args file line test } {
1325 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1326 "$file" "$line" "" "$test"
1329 proc mi_step_to { func args file line test } {
1330 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1331 "$file" "$line" "" "$test"
1334 proc mi_finish_to { func args file line result ret test } {
1335 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
1337 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1341 proc mi_continue_to {func} {
1342 mi_runto_helper $func "continue"
1345 proc mi0_execute_to { cmd reason func args file line extra test } {
1346 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
1347 "$file" "$line" "$extra" "$test"
1350 proc mi0_next_to { func args file line test } {
1351 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1352 "$file" "$line" "" "$test"
1355 proc mi0_step_to { func args file line test } {
1356 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1357 "$file" "$line" "" "$test"
1360 proc mi0_finish_to { func args file line result ret test } {
1361 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1363 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1367 proc mi0_continue_to { bkptno func args file line test } {
1368 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1369 "$func" "$args" "$file" "$line" "" "$test"
1372 # Creates a breakpoint and checks the reported fields are as expected.
1373 # This procedure takes the same options as mi_make_breakpoint and
1374 # returns the breakpoint regexp from that procedure.
1376 proc mi_create_breakpoint {location test args} {
1377 set bp [eval mi_make_breakpoint $args]
1378 mi_gdb_test "222-break-insert $location" "222\\^done,$bp" $test
1382 # Creates varobj named NAME for EXPRESSION.
1383 # Name cannot be "-".
1384 proc mi_create_varobj { name expression testname } {
1385 mi_gdb_test "-var-create $name * $expression" \
1386 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*,has_more=\"0\"" \
1390 proc mi_create_floating_varobj { name expression testname } {
1391 mi_gdb_test "-var-create $name @ $expression" \
1392 "\\^done,name=\"$name\",numchild=\"\(-1\|\[0-9\]+\)\",value=\".*\",type=.*" \
1397 # Same as mi_create_varobj, but also checks the reported type
1399 proc mi_create_varobj_checked { name expression type testname } {
1400 mi_gdb_test "-var-create $name * $expression" \
1401 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1405 # Same as mi_create_floating_varobj, but assumes the test is creating
1406 # a dynamic varobj that has children, so the value must be "{...}".
1407 # The "has_more" attribute is checked.
1408 proc mi_create_dynamic_varobj {name expression has_more testname} {
1409 mi_gdb_test "-var-create $name @ $expression" \
1410 "\\^done,name=\"$name\",numchild=\"0\",value=\"{\\.\\.\\.}\",type=.*,has_more=\"${has_more}\"" \
1414 # Deletes the specified NAME.
1415 proc mi_delete_varobj { name testname } {
1416 mi_gdb_test "-var-delete $name" \
1417 "\\^done,ndeleted=.*" \
1421 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1422 # are reported as updated, and no other varobj is updated.
1423 # Assumes that no varobj is out of scope and that no varobj changes
1425 proc mi_varobj_update { name expected testname } {
1426 set er "\\^done,changelist=\\\["
1428 foreach item $expected {
1429 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\",has_more=\".\"}"
1439 verbose -log "Expecting: $er" 2
1440 mi_gdb_test "-var-update $name" $er $testname
1443 proc mi_varobj_update_with_child_type_change { name child_name new_type new_children testname } {
1444 set v "{name=\"$child_name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\",has_more=\".\"}"
1445 set er "\\^done,changelist=\\\[$v\\\]"
1446 verbose -log "Expecting: $er"
1447 mi_gdb_test "-var-update $name" $er $testname
1450 proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1451 mi_varobj_update_with_child_type_change $name $name $new_type $new_children $testname
1454 # A helper that turns a key/value list into a regular expression
1455 # matching some MI output.
1456 proc mi_varobj_update_kv_helper {list} {
1459 foreach {key value} $list {
1464 if {$key == "new_children"} {
1465 append rx "$key=\\\[$value\\\]"
1467 append rx "$key=\"$value\""
1473 # A helper for mi_varobj_update_dynamic that computes a match
1474 # expression given a child list.
1475 proc mi_varobj_update_dynamic_helper {children} {
1479 foreach child $children {
1485 append crx [mi_varobj_update_kv_helper $child]
1492 # Update a dynamic varobj named NAME. CHILDREN is a list of children
1493 # that have been updated; NEW_CHILDREN is a list of children that were
1494 # added to the primary varobj. Each child is a list of key/value
1495 # pairs that are expected. SELF is a key/value list holding
1496 # information about the varobj itself. TESTNAME is the name of the
1498 proc mi_varobj_update_dynamic {name testname self children new_children} {
1499 if {[llength $new_children]} {
1500 set newrx [mi_varobj_update_dynamic_helper $new_children]
1501 lappend self new_children $newrx
1503 set selfrx [mi_varobj_update_kv_helper $self]
1504 set crx [mi_varobj_update_dynamic_helper $children]
1506 set er "\\^done,changelist=\\\[\{name=\"$name\",in_scope=\"true\""
1507 append er ",$selfrx\}"
1513 verbose -log "Expecting: $er"
1514 mi_gdb_test "-var-update $name" $er $testname
1517 proc mi_check_varobj_value { name value testname } {
1519 mi_gdb_test "-var-evaluate-expression $name" \
1520 "\\^done,value=\"$value\"" \
1524 # Helper proc which constructs a child regexp for
1525 # mi_list_varobj_children and mi_varobj_update_dynamic.
1526 proc mi_child_regexp {children add_child} {
1535 foreach item $children {
1537 set name [lindex $item 0]
1538 set exp [lindex $item 1]
1539 set numchild [lindex $item 2]
1540 if {[llength $item] == 5} {
1541 set type [lindex $item 3]
1542 set value [lindex $item 4]
1544 lappend children_exp\
1545 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1546 } elseif {[llength $item] == 4} {
1547 set type [lindex $item 3]
1549 lappend children_exp\
1550 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1552 lappend children_exp\
1553 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
1556 return [join $children_exp ","]
1559 # Check the results of the:
1561 # -var-list-children VARNAME
1563 # command. The CHILDREN parement should be a list of lists.
1564 # Each inner list can have either 3 or 4 elements, describing
1565 # fields that gdb is expected to report for child variable object,
1566 # in the following order
1570 # - Number of children
1573 # If inner list has 3 elements, the gdb is expected to output no
1574 # type for a child and no value.
1576 # If the inner list has 4 elements, gdb output is expected to
1579 proc mi_list_varobj_children { varname children testname } {
1580 mi_list_varobj_children_range $varname "" "" [llength $children] $children \
1584 # Like mi_list_varobj_children, but sets a subrange. NUMCHILDREN is
1585 # the total number of children.
1586 proc mi_list_varobj_children_range {varname from to numchildren children testname} {
1588 if {[llength $varname] == 2} {
1589 set options [lindex $varname 1]
1590 set varname [lindex $varname 0]
1593 set children_exp_j [mi_child_regexp $children 1]
1595 set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1597 set expected "\\^done,numchild=\"0\""
1601 append expected ",has_more=\"0\""
1602 } elseif {$to >= 0 && $numchildren > $to} {
1603 append expected ",has_more=\"1\""
1605 append expected ",has_more=\"0\""
1608 verbose -log "Expecting: $expected"
1610 mi_gdb_test "-var-list-children $options $varname $from $to" \
1614 # Verifies that variable object VARNAME has NUMBER children,
1615 # where each one is named $VARNAME.<index-of-child> and has type TYPE.
1616 proc mi_list_array_varobj_children { varname number type testname } {
1617 mi_list_array_varobj_children_with_index $varname $number 0 $type $testname
1620 # Same as mi_list_array_varobj_children, but allowing to pass a start index
1622 proc mi_list_array_varobj_children_with_index { varname number start_index \
1625 set index $start_index
1626 for {set i 0} {$i < $number} {incr i} {
1627 lappend t [list $varname.$index $index 0 $type]
1630 mi_list_varobj_children $varname $t $testname
1633 # A list of two-element lists. First element of each list is
1634 # a Tcl statement, and the second element is the line
1635 # number of source C file where the statement originates.
1636 set mi_autotest_data ""
1637 # The name of the source file for autotesting.
1638 set mi_autotest_source ""
1640 proc count_newlines { string } {
1641 return [regexp -all "\n" $string]
1644 # Prepares for running inline tests in FILENAME.
1645 # See comments for mi_run_inline_test for detailed
1646 # explanation of the idea and syntax.
1647 proc mi_prepare_inline_tests { filename } {
1651 global mi_autotest_source
1652 global mi_autotest_data
1654 set mi_autotest_data {}
1656 set mi_autotest_source $filename
1658 if { ! [regexp "^/" "$filename"] } then {
1659 set filename "$srcdir/$subdir/$filename"
1662 set chan [open $filename]
1663 set content [read $chan]
1666 set start [string first "/*:" $content]
1668 set end [string first ":*/" $content]
1670 error "Unterminated special comment in $filename"
1673 set prefix [string range $content 0 $start]
1674 set prefix_newlines [count_newlines $prefix]
1676 set line_number [expr $line_number+$prefix_newlines]
1677 set comment_line $line_number
1679 set comment [string range $content [expr $start+3] [expr $end-1]]
1681 set comment_newlines [count_newlines $comment]
1682 set line_number [expr $line_number+$comment_newlines]
1684 set comment [string trim $comment]
1685 set content [string range $content [expr $end+3] \
1686 [string length $content]]
1687 lappend mi_autotest_data [list $comment $comment_line]
1695 # Helper to mi_run_inline_test below.
1696 # Return the list of all (statement,line_number) lists
1697 # that comprise TESTCASE. The begin and end markers
1699 proc mi_get_inline_test {testcase} {
1701 global mi_gdb_prompt
1702 global mi_autotest_data
1703 global mi_autotest_source
1709 foreach l $mi_autotest_data {
1711 set comment [lindex $l 0]
1713 if {$comment == "BEGIN: $testcase"} {
1715 } elseif {$comment == "END: $testcase"} {
1718 } elseif {$seen_begin==1} {
1723 if {$seen_begin == 0} {
1724 error "Autotest $testcase not found"
1727 if {$seen_begin == 1 && $seen_end == 0} {
1728 error "Missing end marker for test $testcase"
1734 # Sets temporary breakpoint at LOCATION.
1735 proc mi_tbreak {location} {
1737 global mi_gdb_prompt
1739 mi_gdb_test "-break-insert -t $location" \
1741 "run to $location (set breakpoint)"
1744 # Send COMMAND that must be a command that resumes
1745 # the inferior (run/continue/next/etc) and consumes
1746 # the "^running" output from it.
1747 proc mi_send_resuming_command_raw {command test} {
1749 global mi_gdb_prompt
1750 global thread_selected_re
1751 global library_loaded_re
1753 send_gdb "$command\n"
1755 -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n($library_loaded_re)*($thread_selected_re)?${mi_gdb_prompt}" {
1756 # Note that lack of 'pass' call here -- this works around limitation
1757 # in DejaGNU xfail mechanism. mi-until.exp has this:
1759 # setup_kfail gdb/2104 "*-*-*"
1762 # and mi_execute_to uses mi_send_resuming_command. If we use 'pass' here,
1763 # it will reset kfail, so when the actual test fails, it will be flagged
1767 -re "\\^error,msg=\"Displaced stepping is only supported in ARM mode\".*" {
1768 unsupported "$test (Thumb mode)"
1771 -re "\\^error,msg=.*" {
1772 fail "$test (MI error)"
1775 -re ".*${mi_gdb_prompt}" {
1776 fail "$test (failed to resume)"
1786 proc mi_send_resuming_command {command test} {
1787 mi_send_resuming_command_raw -$command $test
1790 # Helper to mi_run_inline_test below.
1791 # Sets a temporary breakpoint at LOCATION and runs
1792 # the program using COMMAND. When the program is stopped
1793 # returns the line at which it. Returns -1 if line cannot
1795 # Does not check that the line is the same as requested.
1796 # The caller can check itself if required.
1797 proc mi_continue_to_line {location test} {
1800 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1801 return [mi_get_stop_line $test]
1804 # Wait until gdb prints the current line.
1805 proc mi_get_stop_line {test} {
1807 global mi_gdb_prompt
1813 set prompt_re "$mi_gdb_prompt$"
1817 -re ".*line=\"(\[0-9\]*)\".*\r\n$prompt_re" {
1818 return $expect_out(1,string)
1820 -re ".*$mi_gdb_prompt" {
1821 fail "wait for stop ($test)"
1824 fail "wait for stop ($test)"
1829 # Run a MI test embedded in comments in a C file.
1830 # The C file should contain special comments in the following
1833 # /*: BEGIN: testname :*/
1834 # /*: <Tcl statements> :*/
1835 # /*: END: testname :*/
1837 # This procedure find the begin and end marker for the requested
1838 # test. Then, a temporary breakpoint is set at the begin
1839 # marker and the program is run (from start).
1841 # After that, for each special comment between the begin and end
1842 # marker, the Tcl statements are executed. It is assumed that
1843 # for each comment, the immediately preceding line is executable
1844 # C statement. Then, gdb will be single-stepped until that
1845 # preceding C statement is executed, and after that the
1846 # Tcl statements in the comment will be executed.
1850 # /*: BEGIN: assignment-test :*/
1852 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1853 # /*: END: assignment-test :*/
1855 # The mi_prepare_inline_tests function should be called before
1856 # calling this function. A given C file can contain several
1857 # inline tests. The names of the tests must be unique within one
1860 proc mi_run_inline_test { testcase } {
1862 global mi_gdb_prompt
1865 global fullname_syntax
1866 global mi_autotest_source
1868 set commands [mi_get_inline_test $testcase]
1873 foreach c $commands {
1874 set statements [lindex $c 0]
1875 set line [lindex $c 1]
1876 set line [expr $line-1]
1878 # We want gdb to be stopped at the expression immediately
1879 # before the comment. If this is the first comment, the
1880 # program is either not started yet or is in some random place,
1881 # so we run it. For further comments, we might be already
1882 # standing at the right line. If not continue till the
1886 # Start the program afresh.
1887 mi_tbreak "$mi_autotest_source:$line"
1889 set line_now [mi_get_stop_line "$testcase: step to $line"]
1891 } elseif {$line_now!=$line} {
1892 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1895 if {$line_now!=$line} {
1896 fail "$testcase: go to line $line"
1899 # We're not at the statement right above the comment.
1900 # Execute that statement so that the comment can test
1901 # the state after the statement is executed.
1903 # Single-step past the line.
1904 if { [mi_send_resuming_command "exec-next" "$testcase: step over $line"] != 0 } {
1907 set line_now [mi_get_stop_line "$testcase: step over $line"]
1909 # We probably want to use 'uplevel' so that statements
1910 # have direct access to global variables that the
1911 # main 'exp' file has set up. But it's not yet clear,
1912 # will need more experience to be sure.
1917 proc get_mi_thread_list {name} {
1920 # MI will return a list of thread ids:
1923 # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
1925 mi_gdb_test "-thread-list-ids" \
1926 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1927 "-thread_list_ids ($name)"
1930 if {[info exists expect_out(buffer)]} {
1931 set output $expect_out(buffer)
1935 if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
1936 fail "finding threads in MI output ($name)"
1938 pass "finding threads in MI output ($name)"
1940 # Make list of console threads
1941 set start [expr {[string first \{ $threads] + 1}]
1942 set end [expr {[string first \} $threads] - 1}]
1943 set threads [string range $threads $start $end]
1944 foreach thread [split $threads ,] {
1945 if {[scan $thread {thread-id="%d"} num]} {
1946 lappend thread_list $num
1954 # Check that MI and the console know of the same threads.
1955 # Appends NAME to all test names.
1956 proc check_mi_and_console_threads {name} {
1959 mi_gdb_test "-thread-list-ids" \
1960 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1961 "-thread-list-ids ($name)"
1963 if {[info exists expect_out(buffer)]} {
1964 set mi_output $expect_out(buffer)
1967 # GDB will return a list of thread ids and some more info:
1970 # -interpreter-exec console "info threads"
1971 # ~" 4 Thread 2051 (LWP 7734) 0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
1972 # ~" 3 Thread 1026 (LWP 7733) () at __libc_nanosleep:-1"
1973 # ~" 2 Thread 2049 (LWP 7732) 0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
1974 # ~"* 1 Thread 1024 (LWP 7731) main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
1975 # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
1976 mi_gdb_test "info threads" \
1977 {.*(~".*"[\r\n]*)+.*} \
1978 "info threads ($name)"
1979 set console_output {}
1980 if {[info exists expect_out(buffer)]} {
1981 set console_output $expect_out(buffer)
1984 # Make a list of all known threads to console (gdb's thread IDs)
1985 set console_thread_list {}
1986 foreach line [split $console_output \n] {
1987 if {[string index $line 0] == "~"} {
1988 # This is a line from the console; trim off "~", " ", "*", and "\""
1989 set line [string trim $line ~\ \"\*]
1990 if {[scan $line "%d" id] == 1} {
1991 lappend console_thread_list $id
1996 # Now find the result string from MI
1998 foreach line [split $mi_output \n] {
1999 if {[string range $line 0 4] == "^done"} {
2003 if {$mi_result == ""} {
2004 fail "finding MI result string ($name)"
2006 pass "finding MI result string ($name)"
2009 # Finally, extract the thread ids and compare them to the console
2010 set num_mi_threads_str ""
2011 if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
2012 fail "finding number of threads in MI output ($name)"
2014 pass "finding number of threads in MI output ($name)"
2016 # Extract the number of threads from the MI result
2017 if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
2018 fail "got number of threads from MI ($name)"
2020 pass "got number of threads from MI ($name)"
2022 # Check if MI and console have same number of threads
2023 if {$num_mi_threads != [llength $console_thread_list]} {
2024 fail "console and MI have same number of threads ($name)"
2026 pass "console and MI have same number of threads ($name)"
2028 # Get MI thread list
2029 set mi_thread_list [get_mi_thread_list $name]
2031 # Check if MI and console have the same threads
2033 foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
2039 fail "MI and console have same threads ($name)"
2041 # Send a list of failures to the log
2042 send_log "Console has thread ids: $console_thread_list\n"
2043 send_log "MI has thread ids: $mi_thread_list\n"
2045 pass "MI and console have same threads ($name)"
2052 # Download shared libraries to the target.
2053 proc mi_load_shlibs { args } {
2054 foreach file $args {
2055 gdb_remote_download target [shlib_target_file $file]
2058 if {[is_remote target]} {
2059 # If the target is remote, we need to tell gdb where to find the
2062 # We could set this even when not testing remotely, but a user
2063 # generally won't set it unless necessary. In order to make the tests
2064 # more like the real-life scenarios, we don't set it for local testing.
2065 mi_gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "\^done" ""
2069 proc mi_check_thread_states { states test } {
2071 set pattern ".*\\^done,threads=\\\["
2073 set pattern "${pattern}(.*)state=\"$s\""
2075 set pattern "${pattern}(,core=\"\[0-9\]*\")?\\\}\\\].*"
2077 verbose -log "expecting: $pattern"
2078 mi_gdb_test "-thread-info" $pattern $test
2081 # Return a list of MI features supported by this gdb.
2082 proc mi_get_features {} {
2083 global expect_out mi_gdb_prompt
2085 send_gdb "-list-features\n"
2088 -re "\\^done,features=\\\[(.*)\\\]\r\n$mi_gdb_prompt$" {
2089 regsub -all -- \" $expect_out(1,string) "" features
2090 return [split $features ,]
2092 -re ".*\r\n$mi_gdb_prompt$" {
2093 verbose -log "got $expect_out(buffer)"
2097 verbose -log "timeout in mi_gdb_prompt"
2103 # Variable Object Trees
2105 # Yet another way to check varobjs. Pass mi_walk_varobj_tree a "list" of
2106 # variables (not unlike the actual source code definition), and it will
2107 # automagically test the children for you (by default).
2129 # foo *f = new foo (); <-- break here
2131 # We want to check all the children of "f".
2133 # Translate the above structures into the following tree:
2139 # anonymous struct {
2150 # {const int *} iPtr {
2151 # {const int} {*iPtr} {}
2161 # mi_walk_varobj_tree c++ $tree
2163 # If you'd prefer to walk the tree using your own callback,
2164 # simply pass the name of the callback to mi_walk_varobj_tree.
2166 # This callback should take one argument, the name of the variable
2167 # to process. This name is the name of a global array holding the
2168 # variable's properties (object name, type, etc).
2170 # An example callback:
2172 # proc my_callback {var} {
2173 # upvar #0 $var varobj
2175 # puts "my_callback: called on varobj $varobj(obj_name)"
2178 # The arrays created for each variable object contain the following
2181 # obj_name - the object name for accessing this variable via MI
2182 # display_name - the display name for this variable (exp="display_name" in
2183 # the output of -var-list-children)
2184 # type - the type of this variable (type="type" in the output
2185 # of -var-list-children, or the special tag "anonymous"
2186 # path_expr - the "-var-info-path-expression" for this variable
2187 # NOTE: This member cannot be used reliably with typedefs.
2189 # See notes inside get_path_expr for more.
2190 # parent - the variable name of the parent varobj
2191 # children - a list of children variable names (which are the
2192 # names Tcl arrays, not object names)
2194 # For each variable object, an array containing the above fields will
2195 # be created under the root node (conveniently called, "root"). For example,
2196 # a variable object with handle "OBJ.public.0_anonymous.a" will have
2197 # a corresponding global Tcl variable named "root.OBJ.public.0_anonymous.a".
2199 # Note that right now, this mechanism cannot be used for recursive data
2200 # structures like linked lists.
2202 namespace eval ::varobj_tree {
2203 # An index which is appended to root varobjs to ensure uniqueness.
2204 variable _root_idx 0
2206 # A procedure to help with debuggging varobj trees.
2207 # VARIABLE_NAME is the name of the variable to dump.
2208 # CMD, if present, is the name of the callback to output the contstructed
2209 # strings. By default, it uses expect's "send_log" command.
2210 # TERM, if present, is a terminating character. By default it is the newline.
2212 # To output to the terminal (not the expect log), use
2213 # mi_varobj_tree_dump_variable my_variable puts ""
2215 proc mi_varobj_tree_dump_variable {variable_name {cmd send_log} {term "\n"}} {
2216 upvar #0 $variable_name varobj
2218 eval "$cmd \"VAR = $variable_name$term\""
2220 # Explicitly encode the array indices, since outputting them
2221 # in some logical order is better than what "array names" might
2223 foreach idx {obj_name parent display_name type path_expr} {
2224 eval "$cmd \"\t$idx = $varobj($idx)$term\""
2228 set num [llength $varobj(children)]
2229 eval "$cmd \"\tnum_children = $num$term\""
2231 eval "$cmd \"\tchildren = $varobj(children)$term\""
2235 # The default callback used by mi_walk_varobj_tree. This callback
2236 # simply checks all of VAR's children. It specifically does not test
2237 # path expressions, since that is very problematic.
2239 # This procedure may be used in custom callbacks.
2240 proc test_children_callback {variable_name} {
2241 upvar #0 $variable_name varobj
2243 if {[llength $varobj(children)] > 0} {
2244 # Construct the list of children the way mi_list_varobj_children
2245 # expects to get it:
2246 # { {obj_name display_name num_children type} ... }
2247 set children_list {}
2248 foreach child $varobj(children) {
2250 set clist [list [string_to_regexp $c(obj_name)] \
2251 [string_to_regexp $c(display_name)] \
2252 [llength $c(children)]]
2253 if {[string length $c(type)] > 0} {
2254 lappend clist [string_to_regexp $c(type)]
2256 lappend children_list $clist
2259 mi_list_varobj_children $varobj(obj_name) $children_list \
2260 "VT: list children of $varobj(obj_name)"
2264 # Set the properties of the varobj represented by
2265 # PARENT_VARIABLE - the name of the parent's variable
2266 # OBJNAME - the MI object name of this variable
2267 # DISP_NAME - the display name of this variable
2268 # TYPE - the type of this variable
2269 # PATH - the path expression for this variable
2270 # CHILDREN - a list of the variable's children
2271 proc create_varobj {parent_variable objname disp_name \
2272 type path children} {
2273 upvar #0 $parent_variable parent
2275 set var_name "root.$objname"
2277 array set $var_name [list obj_name $objname]
2278 array set $var_name [list display_name $disp_name]
2279 array set $var_name [list type $type]
2280 array set $var_name [list path_expr $path]
2281 array set $var_name [list parent "$parent_variable"]
2282 array set $var_name [list children \
2283 [get_tree_children $var_name $children]]
2287 # Should VARIABLE be used in path expressions? The CPLUS_FAKE_CHILD
2288 # varobjs and anonymous structs/unions are not used for path expressions.
2289 proc is_path_expr_parent {variable} {
2290 upvar #0 $variable varobj
2292 # If the varobj's type is "", it is a CPLUS_FAKE_CHILD.
2293 # If the tail of the varobj's object name is "%d_anonymous",
2294 # then it represents an anonymous struct or union.
2295 if {[string length $varobj(type)] == 0 \
2296 || [regexp {[0-9]+_anonymous$} $varobj(obj_name)]} {
2303 # Return the path expression for the variable named NAME in
2304 # parent varobj whose variable name is given by PARENT_VARIABLE.
2305 proc get_path_expr {parent_variable name type} {
2306 upvar #0 $parent_variable parent
2307 upvar #0 $parent_variable path_parent
2309 # If TYPE is "", this is one of the CPLUS_FAKE_CHILD varobjs,
2310 # which has no path expression. Likewsise for anonymous structs
2312 if {[string length $type] == 0 \
2313 || [string compare $type "anonymous"] == 0} {
2317 # Find the path parent variable.
2318 while {![is_path_expr_parent $parent_variable]} {
2319 set parent_variable $path_parent(parent)
2320 upvar #0 $parent_variable path_parent
2323 # This is where things get difficult. We do not actually know
2324 # the real type for variables defined via typedefs, so we don't actually
2325 # know whether the parent is a structure/union or not.
2327 # So we assume everything that isn't a simple type is a compound type.
2329 regexp {\*+} $parent(type) stars
2331 if {[string index $name 0] == "*"} {
2335 if {[string index $parent(type) end] == "\]"} {
2336 # Parent is an array.
2337 return "($path_parent(path_expr))\[$name\]"
2338 } elseif {$is_compound} {
2339 # Parent is a structure or union or a pointer to one.
2340 if {[string length $stars]} {
2348 # To make matters even more hideous, varobj.c has slightly different
2349 # path expressions for C and C++.
2350 set path_expr "($path_parent(path_expr))$join$name"
2351 if {[string compare -nocase $root(language) "c"] == 0} {
2354 return "($path_expr)"
2357 # Parent is a pointer.
2358 return "*($path_parent(path_expr))"
2362 # Process the CHILDREN (a list of varobj_tree elements) of the variable
2363 # given by PARENT_VARIABLE. Returns a list of children variables.
2364 proc get_tree_children {parent_variable children} {
2365 upvar #0 $parent_variable parent
2368 set children_list {}
2369 foreach {type name children} $children {
2370 if {[string compare $parent_variable "root"] == 0} {
2374 set objname "$name$_root_idx"
2375 set disp_name "$name"
2376 set path_expr "$name"
2377 } elseif {[string compare $type "anonymous"] == 0} {
2378 # Special case: anonymous types. In this case, NAME will either be
2379 # "struct" or "union".
2380 set objname "$parent(obj_name).${field_idx}_anonymous"
2381 set disp_name "<anonymous $name>"
2383 set type "$name {...}"
2385 set objname "$parent(obj_name).$name"
2387 set path_expr [get_path_expr $parent_variable $name $type]
2390 lappend children_list [create_varobj $parent_variable $objname \
2391 $disp_name $type $path_expr $children]
2395 return $children_list
2398 # The main procedure to call the given CALLBACK on the elements of the
2399 # given varobj TREE. See detailed explanation above.
2400 proc walk_tree {language tree callback} {
2404 if {[llength $tree] < 3} {
2405 error "tree does not contain enough elements"
2410 # Create root node and process the tree.
2411 array set root [list language $language]
2412 array set root [list obj_name "root"]
2413 array set root [list display_name "root"]
2414 array set root [list type "root"]
2415 array set root [list path_expr "root"]
2416 array set root [list parent "root"]
2417 array set root [list children [get_tree_children root $tree]]
2420 set all_nodes $root(children); # a stack of nodes
2421 while {[llength $all_nodes] > 0} {
2422 # "Pop" the name of the global variable containing this varobj's
2423 # information from the stack of nodes.
2424 set var_name [lindex $all_nodes 0]
2425 set all_nodes [lreplace $all_nodes 0 0]
2427 # Bring the global named in VAR_NAME into scope as the local variable
2429 upvar #0 $var_name varobj
2431 # Append any children of VAROBJ to the list of nodes to walk.
2432 if {[llength $varobj(children)] > 0} {
2433 set all_nodes [concat $all_nodes $varobj(children)]
2436 # If this is a root variable, create the variable object for it.
2437 if {[string compare $varobj(parent) "root"] == 0} {
2438 mi_create_varobj $varobj(obj_name) $varobj(display_name) \
2439 "VT: create root varobj for $varobj(display_name)"
2442 # Now call the callback for VAROBJ.
2443 uplevel #0 $callback $var_name
2448 # The default varobj tree callback, which simply tests -var-list-children.
2449 proc mi_varobj_tree_test_children_callback {variable} {
2450 ::varobj_tree::test_children_callback $variable
2453 # Walk the variable object tree given by TREE, calling the specified
2454 # CALLBACK. By default this uses mi_varobj_tree_test_children_callback.
2455 proc mi_walk_varobj_tree {language tree \
2457 mi_varobj_tree_test_children_callback}} {
2458 ::varobj_tree::walk_tree $language $tree $callback
2461 # Build a list of key-value pairs given by the list ATTR_LIST. Flatten
2462 # this list using the optional JOINER, a comma by default.
2464 # The list must contain an even number of elements, which are the key-value
2465 # pairs. Each value will be surrounded by quotes, according to the grammar,
2466 # except if the value starts with \[ or \{, when the quotes will be omitted.
2468 # Example: mi_build_kv_pairs {a b c d e f g \[.*\]}
2469 # returns a=\"b\",c=\"d\",e=\"f\",g=\[.*\]
2470 proc mi_build_kv_pairs {attr_list {joiner ,}} {
2472 foreach {var value} $attr_list {
2473 if {[string range $value 0 1] == "\\\["
2474 || [string range $value 0 1] == "\\\{"} {
2475 lappend l "$var=$value"
2477 lappend l "$var=\"$value\""
2480 return "[join $l $joiner]"
2483 # Construct a breakpoint regexp. This may be used to test the output of
2484 # -break-insert, -dprintf-insert, or -break-info.
2486 # All arguments for the breakpoint may be specified using the options
2487 # number, type, disp, enabled, addr, func, file, fullanme, line,
2488 # thread-groups, cond, evaluated-by, times, ignore, script,
2489 # and original-location.
2491 # Only if -script and -ignore are given will they appear in the output.
2492 # Otherwise, this procedure will skip them using ".*".
2494 # Example: mi_make_breakpoint -number 2 -file ".*/myfile.c" -line 3
2495 # will return the breakpoint:
2496 # bkpt={number="2",type=".*",disp=".*",enabled=".*",addr=".*",func=".*",
2497 # file=".*/myfile.c",fullname=".*",line="3",thread-groups=\[.*\],
2498 # times="0".*original-location=".*"}
2500 proc mi_make_breakpoint {args} {
2501 parse_args {{number .*} {type .*} {disp .*} {enabled .*} {addr .*}
2502 {func .*} {file .*} {fullname .*} {line .*}
2503 {thread-groups \\\[.*\\\]} {times .*} {ignore 0}
2504 {script ""} {original-location .*} {cond ""} {evaluated-by ""}}
2507 foreach attr [list number type disp enabled addr func file \
2508 fullname line thread-groups] {
2509 lappend attr_list $attr [set $attr]
2512 set result "bkpt={[mi_build_kv_pairs $attr_list]"
2514 # There are always exceptions.
2516 # If COND is not preset, do not output it.
2517 if {[string length $cond] > 0} {
2519 append result [mi_build_kv_pairs [list "cond" $cond]]
2521 # When running on a remote, GDB may output who is evaluating
2522 # breakpoint conditions.
2523 if {[string length ${evaluated-by}] > 0} {
2524 append result [mi_build_kv_pairs \
2525 [list "evaluated-by" ${evaluated-by}]]
2527 append result {(,evaluated-by=".*")?}
2532 append result [mi_build_kv_pairs [list "times" $times]]
2534 # If SCRIPT and IGNORE are not present, do not output them.
2537 append result [mi_build_kv_pairs [list "ignore" $ignore]]
2540 if {[string length $script] > 0} {
2542 append result [mi_build_kv_pairs [list "script" $script]]
2545 # Allow anything up until the next "official"/required attribute.
2546 # This pattern skips over script/ignore if matches on those
2547 # were not specifically required by the caller.
2550 append result [mi_build_kv_pairs \
2551 [list "original-location" ${original-location}]]
2556 # Build a breakpoint table regexp given the list of breakpoints in `bp_list',
2557 # constructed by mi_make_breakpoint.
2559 # Example: Construct a breakpoint table where the only attributes we
2560 # test for are the existence of three breakpoints numbered 1, 2, and 3.
2563 # lappend bps [mi_make_breakpoint -number 1]
2564 # lappend bps [mi_make_breakpoint -number 2]
2565 # lappned bps [mi_make_breakpoint -number 3]
2566 # mi_make_breakpoint_table $bps
2567 # will return (abbreviated for clarity):
2568 # BreakpointTable={nr_rows="3",nr_cols="6",hdr=[{width=".*",...} ...],
2569 # body=[bkpt={number="1",...},bkpt={number="2",...},bkpt={number="3",...}]}
2571 proc mi_make_breakpoint_table {bp_list} {
2572 # Build header -- assume a standard header for all breakpoint tables.
2574 foreach {nm hdr} [list number Num type Type disp Disp enabled Enb \
2575 addr Address what What] {
2576 # The elements here are the MI table headers, which have the
2578 # {width="7",alignment="-1",col_name="number",colhdr="Num"}
2579 lappend hl "{[mi_build_kv_pairs [list width .* alignment .* \
2580 col_name $nm colhdr $hdr]]}"
2582 set header "hdr=\\\[[join $hl ,]\\\]"
2584 # The caller has implicitly supplied the number of columns and rows.
2585 set nc [llength $hl]
2586 set nr [llength $bp_list]
2588 # Build body -- mi_make_breakpoint has done most of the work.
2589 set body "body=\\\[[join $bp_list ,]\\\]"
2591 # Assemble the final regexp.
2592 return "BreakpointTable={nr_rows=\"$nr\",nr_cols=\"$nc\",$header,$body}"
2595 # Return a 1 for configurations that do not support Python scripting.
2596 # Note: This also sets various globals that specify which version of Python
2597 # is in use. See skip_python_tests_prompt.
2599 proc mi_skip_python_tests {} {
2600 global mi_gdb_prompt
2601 return [skip_python_tests_prompt "$mi_gdb_prompt$"]
2604 # As skip_libstdcxx_probe_tests_prompt, with mi_gdb_prompt.
2606 proc mi_skip_libstdcxx_probe_tests {} {
2607 global mi_gdb_prompt
2608 return [skip_libstdcxx_probe_tests_prompt "$mi_gdb_prompt$"]
2611 # Check whether we're testing with the remote or extended-remote
2614 proc mi_is_target_remote {} {
2615 global mi_gdb_prompt
2617 return [gdb_is_target_remote_prompt "$mi_gdb_prompt"]