1 # Copyright 1999-2014 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 # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
21 # Set it if it is not already set.
23 if ![info exists mi_gdb_prompt] then {
24 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
27 global mi_inferior_spawn_id
28 global mi_inferior_tty_name
32 set thread_selected_re "=thread-selected,id=\"\[0-9\]+\"\r\n"
33 set gdbindex_warning_re "&\"warning: Skipping \[^\r\n\]+ \.gdb_index section in \[^\r\n\]+\"\r\n(?:&\"\\\\n\"\r\n)?"
34 set library_loaded_re "=library-loaded\[^\n\]+\"\r\n(?:$gdbindex_warning_re)?"
35 set breakpoint_re "=(?:breakpoint-created|breakpoint-deleted)\[^\n\]+\"\r\n"
38 # mi_gdb_exit -- exit the GDB, killing the target program if necessary
41 catch mi_uncatched_gdb_exit
44 proc mi_uncatched_gdb_exit {} {
46 global INTERNAL_GDBFLAGS GDBFLAGS
53 gdb_stop_suppressing_tests
55 if { [info procs sid_exit] != "" } {
59 if ![info exists gdb_spawn_id] {
63 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
65 if { [is_remote host] && [board_info host exists fileid] } {
66 send_gdb "999-gdb-exit\n"
72 -re "Undefined command.*$gdb_prompt $" {
76 -re "DOSEXIT code" { }
81 if ![is_remote host] {
88 # default_mi_gdb_start [INFERIOR_PTY] -- start gdb running, default procedure
90 # INFERIOR_PTY should be set to separate-inferior-tty to have the inferior work
91 # with it's own PTY. If set to same-inferior-tty, the inferior shares GDB's PTY.
92 # The default value is same-inferior-tty.
94 # When running over NFS, particularly if running many simultaneous
95 # tests on different hosts all using the same server, things can
96 # get really slow. Give gdb at least 3 minutes to start up.
98 proc default_mi_gdb_start { args } {
99 global verbose use_gdb_stub
101 global INTERNAL_GDBFLAGS GDBFLAGS
108 gdb_stop_suppressing_tests
109 set inferior_pty no-tty
111 # Set the default value, it may be overriden later by specific testfile.
112 set use_gdb_stub [target_info exists use_gdb_stub]
114 if { [llength $args] == 1} {
115 set inferior_pty [lindex $args 0]
118 set separate_inferior_pty [string match $inferior_pty separate-inferior-tty]
121 if { [info procs sid_start] != "" } {
122 verbose "Spawning SID"
126 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
128 if [info exists gdb_spawn_id] {
132 if ![is_remote host] {
133 if { [which $GDB] == 0 } then {
134 perror "$GDB does not exist."
139 # Create the new PTY for the inferior process.
140 if { $separate_inferior_pty } {
142 global mi_inferior_spawn_id
143 global mi_inferior_tty_name
144 set mi_inferior_spawn_id $spawn_id
145 set mi_inferior_tty_name $spawn_out(slave,name)
148 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS [host_info gdb_opts]"]
149 if { $res < 0 || $res == "" } {
150 perror "Spawning $GDB failed."
154 -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
155 # We have a new format mi startup prompt. If we are
156 # running mi1, then this is an error as we should be
157 # using the old-style prompt.
158 if { $MIFLAGS == "-i=mi1" } {
159 perror "(mi startup) Got unexpected new mi prompt."
163 verbose "GDB initialized."
165 -re "\[^~\].*$mi_gdb_prompt$" {
166 # We have an old format mi startup prompt. If we are
167 # not running mi1, then this is an error as we should be
168 # using the new-style prompt.
169 if { $MIFLAGS != "-i=mi1" } {
170 perror "(mi startup) Got unexpected old mi prompt."
174 verbose "GDB initialized."
176 -re ".*unrecognized option.*for a complete list of options." {
177 untested "Skip mi tests (not compiled with mi support)."
181 -re ".*Interpreter `mi' unrecognized." {
182 untested "Skip mi tests (not compiled with mi support)."
187 perror "(timeout) GDB never initialized after 10 seconds."
194 # FIXME: mi output does not go through pagers, so these can be removed.
195 # force the height to "unlimited", so no pagers get used
196 send_gdb "100-gdb-set height 0\n"
198 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
199 verbose "Setting height to 0." 2
202 warning "Couldn't set the height to 0"
205 # force the width to "unlimited", so no wraparound occurs
206 send_gdb "101-gdb-set width 0\n"
208 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
209 verbose "Setting width to 0." 2
212 warning "Couldn't set the width to 0."
215 # If allowing the inferior to have its own PTY then assign the inferior
216 # its own terminal device here.
217 if { $separate_inferior_pty } {
218 send_gdb "102-inferior-tty-set $mi_inferior_tty_name\n"
220 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
221 verbose "redirect inferior output to new terminal device."
224 warning "Couldn't redirect inferior output." 2
235 # Overridable function. You can override this function in your
238 proc mi_gdb_start { args } {
239 return [default_mi_gdb_start $args]
242 # Many of the tests depend on setting breakpoints at various places and
243 # running until that breakpoint is reached. At times, we want to start
244 # with a clean-slate with respect to breakpoints, so this utility proc
245 # lets us do this without duplicating this code everywhere.
248 proc mi_delete_breakpoints {} {
251 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
252 send_gdb "102-break-delete\n"
254 -re "Delete all breakpoints.*y or n.*$" {
258 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
259 # This happens if there were no breakpoints
261 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
264 # The correct output is not "No breakpoints or watchpoints." but an
265 # empty BreakpointTable. Also, a query is not acceptable with mi.
266 send_gdb "103-break-list\n"
268 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
269 -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$" {}
270 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
271 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
272 -re "Delete all breakpoints.*or n.*$" {
273 warning "Unexpected prompt for breakpoints deletion"
277 timeout { perror "-break-list (timeout)" ; return }
281 proc mi_gdb_reinitialize_dir { subdir } {
286 if { $suppress_flag } {
290 if [is_remote host] {
294 if { $MIFLAGS == "-i=mi1" } {
295 send_gdb "104-environment-directory\n"
297 -re ".*Reinitialize source path to empty.*y or n. " {
298 warning "Got confirmation prompt for dir reinitialization."
301 -re "$mi_gdb_prompt$" {}
302 timeout {error "Dir reinitialization failed (timeout)"}
305 -re "$mi_gdb_prompt$" {}
306 timeout {error "Dir reinitialization failed (timeout)"}
309 send_gdb "104-environment-directory -r\n"
311 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
312 -re "$mi_gdb_prompt$" {}
313 timeout {error "Dir reinitialization failed (timeout)"}
317 send_gdb "105-environment-directory $subdir\n"
319 -re "Source directories searched.*$mi_gdb_prompt$" {
320 verbose "Dir set to $subdir"
322 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
323 # FIXME: We return just the prompt for now.
324 verbose "Dir set to $subdir"
325 # perror "Dir \"$subdir\" failed."
330 # Send GDB the "target" command.
331 # FIXME: Some of these patterns are not appropriate for MI. Based on
332 # config/monitor.exp:gdb_target_command.
333 proc mi_gdb_target_cmd { targetname serialport } {
336 set serialport_re [string_to_regexp $serialport]
337 for {set i 1} {$i <= 3} {incr i} {
338 send_gdb "47-target-select $targetname $serialport\n"
340 -re "47\\^connected.*$mi_gdb_prompt" {
341 verbose "Set target to $targetname"
344 -re "unknown host.*$mi_gdb_prompt" {
345 verbose "Couldn't look up $serialport"
347 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
348 verbose "Connection failed"
350 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
351 verbose "Set target to $targetname"
354 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
355 verbose "Set target to $targetname"
358 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
359 verbose "Set target to $targetname"
362 -re "Connected to.*$mi_gdb_prompt$" {
363 verbose "Set target to $targetname"
366 -re "Ending remote.*$mi_gdb_prompt$" { }
367 -re "Connection refused.*$mi_gdb_prompt$" {
368 verbose "Connection refused by remote target. Pausing, and trying again."
372 -re "Non-stop mode requested, but remote does not support non-stop.*$mi_gdb_prompt" {
373 unsupported "Non-stop mode not supported"
376 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
377 verbose "Got timeout error from gdb."
389 # load a file into the debugger (file command only).
390 # return a -1 if anything goes wrong.
392 proc mi_gdb_file_cmd { arg } {
398 global last_loaded_file
399 upvar timeout timeout
401 set last_loaded_file $arg
403 if [is_remote host] {
404 set arg [remote_download host $arg]
406 error "download failed"
411 # FIXME: Several of these patterns are only acceptable for console
412 # output. Queries are an error for mi.
413 send_gdb "105-file-exec-and-symbols $arg\n"
415 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
416 verbose "\t\tLoaded $arg into the $GDB"
419 -re "has no symbol-table.*$mi_gdb_prompt$" {
420 perror "$arg wasn't compiled with \"-g\""
423 -re "Load new symbol table from \".*\".*y or n. $" {
426 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
427 verbose "\t\tLoaded $arg with new symbol table into $GDB"
431 perror "(timeout) Couldn't load $arg, other program already loaded."
436 -re "No such file or directory.*$mi_gdb_prompt$" {
437 perror "($arg) No such file or directory\n"
440 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
441 # We (MI) are just giving the prompt back for now, instead of giving
442 # some acknowledgement.
446 perror "couldn't load $arg into $GDB (timed out)."
450 # This is an attempt to detect a core dump, but seems not to
451 # work. Perhaps we need to match .* followed by eof, in which
452 # gdb_expect does not seem to have a way to do that.
453 perror "couldn't load $arg into $GDB (end of file)."
460 # connect to the target and download a file, if necessary.
461 # return a -1 if anything goes wrong.
463 proc mi_gdb_target_load { } {
470 if [target_info exists gdb_load_timeout] {
471 set loadtimeout [target_info gdb_load_timeout]
476 if { [info procs gdbserver_gdb_load] != "" } {
477 mi_gdb_test "kill" ".*" ""
478 set res [gdbserver_gdb_load]
479 set protocol [lindex $res 0]
480 set gdbport [lindex $res 1]
482 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
485 } elseif { [info procs send_target_sid] != "" } {
486 # For SID, things get complex
489 -re ".*$mi_gdb_prompt$"
492 gdb_expect $loadtimeout {
493 -re "\\^done.*$mi_gdb_prompt$" {
496 perror "Unable to connect to SID target (timeout)"
500 send_gdb "48-target-download\n"
501 gdb_expect $loadtimeout {
502 -re "48\\^done.*$mi_gdb_prompt$" {
505 perror "Unable to download to SID target (timeout)"
509 } elseif { [target_info protocol] == "sim" } {
510 # For the simulator, just connect to it directly.
511 send_gdb "47-target-select sim\n"
512 gdb_expect $loadtimeout {
513 -re "47\\^connected.*$mi_gdb_prompt$" {
516 perror "Unable to select sim target (timeout)"
520 send_gdb "48-target-download\n"
521 gdb_expect $loadtimeout {
522 -re "48\\^done.*$mi_gdb_prompt$" {
525 perror "Unable to download to sim target (timeout)"
529 } elseif { [target_info gdb_protocol] == "remote" } {
531 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
532 perror "Unable to connect to remote target"
535 send_gdb "48-target-download\n"
536 gdb_expect $loadtimeout {
537 -re "48\\^done.*$mi_gdb_prompt$" {
540 perror "Unable to download to remote target (timeout)"
549 # load a file into the debugger.
550 # return a -1 if anything goes wrong.
552 proc mi_gdb_load { arg } {
554 return [mi_gdb_file_cmd $arg]
559 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
562 # COMMAND is the command to execute, send to GDB with send_gdb. If
563 # this is the null string no command is sent.
564 # PATTERN is the pattern to match for a PASS, and must NOT include
565 # the \r\n sequence immediately before the gdb prompt.
566 # MESSAGE is the message to be printed. (If this is the empty string,
567 # then sometimes we don't call pass or fail at all; I don't
568 # understand this at all.)
569 # IPATTERN is the pattern to match for the inferior's output. This parameter
570 # is optional. If present, it will produce a PASS if the match is
571 # successful, and a FAIL if unsuccessful.
574 # 1 if the test failed,
575 # 0 if the test passes,
576 # -1 if there was an internal error.
578 proc mi_gdb_test { args } {
581 global GDB expect_out
582 global inferior_exited_re async
583 upvar timeout timeout
585 set command [lindex $args 0]
586 set pattern [lindex $args 1]
587 set message [lindex $args 2]
589 if [llength $args]==4 {
590 set ipattern [lindex $args 3]
593 if [llength $args]==5 {
594 set question_string [lindex $args 3]
595 set response_string [lindex $args 4]
597 set question_string "^FOOBAR$"
601 send_user "Sending \"$command\" to gdb\n"
602 send_user "Looking to match \"$pattern\"\n"
603 send_user "Message is \"$message\"\n"
607 set string "${command}\n"
608 set string_regex [string_to_regexp $command]
610 if { $command != "" } {
611 while { "$string" != "" } {
612 set foo [string first "\n" "$string"]
613 set len [string length "$string"]
614 if { $foo < [expr $len - 1] } {
615 set str [string range "$string" 0 $foo]
616 if { [send_gdb "$str"] != "" } {
619 if { ! $suppress_flag } {
620 perror "Couldn't send $command to GDB."
629 set string [string range "$string" [expr $foo + 1] end]
634 if { "$string" != "" } {
635 if { [send_gdb "$string"] != "" } {
638 if { ! $suppress_flag } {
639 perror "Couldn't send $command to GDB."
647 if [info exists timeout] {
651 if [info exists timeout] {
658 # With $prompt_re "" there may come arbitrary asynchronous response
659 # from the previous command, before or after $string_regex.
660 set string_regex ".*"
662 verbose -log "Expecting: ^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)"
664 -re "\\*\\*\\* DOSEXIT code.*" {
665 if { $message != "" } {
668 gdb_suppress_entire_file "GDB died"
671 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
672 if ![isnative] then {
673 warning "Can`t communicate to remote target."
679 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
680 # At this point, $expect_out(1,string) is the MI input command.
681 # and $expect_out(2,string) is the MI output command.
682 # If $expect_out(1,string) is "", then there was no MI input command here.
684 # NOTE, there is no trailing anchor because with GDB/MI,
685 # asynchronous responses can happen at any point, causing more
686 # data to be available. Normally an anchor is used to make
687 # sure the end of the output is matched, however, $mi_gdb_prompt
688 # is just as good of an anchor since mi_gdb_test is meant to
689 # match a single mi output command. If a second GDB/MI output
690 # response is sent, it will be in the buffer for the next
691 # time mi_gdb_test is called.
692 if ![string match "" $message] then {
697 -re "(${question_string})$" {
698 send_gdb "$response_string\n"
701 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
702 perror "Undefined command \"$command\"."
706 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
707 perror "\"$command\" is not a unique command name."
711 -re "$inferior_exited_re with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
712 if ![string match "" $message] then {
713 set errmsg "$message (the program exited)"
715 set errmsg "$command (the program exited)"
720 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
721 if ![string match "" $message] then {
722 set errmsg "$message (the program is no longer running)"
724 set errmsg "$command (the program is no longer running)"
729 -re ".*$mi_gdb_prompt\[ \]*$" {
730 if ![string match "" $message] then {
737 perror "Window too small."
740 -re "\\(y or n\\) " {
742 perror "Got interactive prompt."
746 perror "Process no longer exists"
747 if { $message != "" } {
753 perror "internal buffer is full."
757 if ![string match "" $message] then {
758 fail "$message (timeout)"
764 # If the GDB output matched, compare the inferior output.
765 if { $result == 0 } {
766 if [ info exists ipattern ] {
767 if { ![target_info exists gdb,noinferiorio] } {
768 if { [target_info gdb_protocol] == "remote"
769 || [target_info gdb_protocol] == "extended-remote"
770 || [target_info protocol] == "sim"} {
774 pass "$message inferior output"
777 fail "$message inferior output (timeout)"
782 global mi_inferior_spawn_id
784 -i $mi_inferior_spawn_id -re "$ipattern" {
785 pass "$message inferior output"
788 fail "$message inferior output (timeout)"
794 unsupported "$message inferior output"
803 # MI run command. (A modified version of gdb_run_cmd)
806 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
807 # ``.*$'' could swallow up output that we attempt to match elsewhere.
809 proc mi_run_cmd_full {use_mi_command args} {
811 if { $suppress_flag } {
814 global mi_gdb_prompt use_gdb_stub
815 global thread_selected_re
816 global library_loaded_re
818 if {$use_mi_command} {
819 set run_prefix "220-exec-"
826 if [target_info exists gdb_init_command] {
827 send_gdb "[target_info gdb_init_command]\n"
829 -re "$mi_gdb_prompt$" { }
831 perror "gdb_init_command for target failed"
837 if { [mi_gdb_target_load] < 0 } {
842 if [target_info exists gdb,do_reload_on_run] {
843 send_gdb "${run_prefix}continue\n"
845 -re "${run_match}\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\r\n$mi_gdb_prompt" {}
846 -re "${run_match}\\^error.*$mi_gdb_prompt" {return -1}
852 if [target_info exists gdb,start_symbol] {
853 set start [target_info gdb,start_symbol]
858 # HACK: Should either use 000-jump or fix the target code
859 # to better handle RUN.
860 send_gdb "jump *$start\n"
861 warning "Using CLI jump command, expect run-to-main FAIL"
865 send_gdb "${run_prefix}run $args\n"
867 -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}" {
869 -re "\\^error,msg=\"The target does not support running in non-stop mode.\"" {
870 unsupported "Non-stop mode not supported"
874 perror "Unable to start target"
878 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
883 # A wrapper for mi_run_cmd_full which uses -exec-run and
884 # -exec-continue, as appropriate. ARGS are passed verbatim to
886 proc mi_run_cmd {args} {
887 return [eval mi_run_cmd_full 1 $args]
890 # A wrapper for mi_run_cmd_full which uses the CLI commands 'run' and
891 # 'continue', as appropriate. ARGS are passed verbatim to
893 proc mi_run_with_cli {args} {
894 return [eval mi_run_cmd_full 0 $args]
898 # Just like run-to-main but works with the MI interface
901 proc mi_run_to_main { } {
903 if { $suppress_flag } {
912 mi_delete_breakpoints
913 mi_gdb_reinitialize_dir $srcdir/$subdir
914 mi_gdb_load ${binfile}
920 # Just like gdb's "runto" proc, it will run the target to a given
921 # function. The big difference here between mi_runto and mi_execute_to
922 # is that mi_execute_to must have the inferior running already. This
923 # proc will (like gdb's runto) (re)start the inferior, too.
925 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
927 # -1 if test suppressed, failed, timedout
930 proc mi_runto_helper {func run_or_continue} {
932 if { $suppress_flag } {
936 global mi_gdb_prompt expect_out
937 global hex decimal fullname_syntax
939 set test "mi runto $func"
940 mi_gdb_test "200-break-insert -t $func" \
941 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",thread-groups=\\\[\"i1\"\\\],times=\"0\",original-location=\".*\"\}" \
942 "breakpoint at $func"
944 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
945 || ![scan $str {number="%d"} bkptno]} {
949 if {$run_or_continue == "run"} {
950 if { [mi_run_cmd] < 0 } {
954 mi_send_resuming_command "exec-continue" "$test"
957 mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test
960 proc mi_runto {func} {
961 return [mi_runto_helper $func "run"]
964 # Next to the next statement
965 # For return values, see mi_execute_to_helper
967 proc mi_next { test } {
968 return [mi_next_to {.*} {.*} {.*} {.*} $test]
972 # Step to the next statement
973 # For return values, see mi_execute_to_helper
975 proc mi_step { test } {
976 return [mi_step_to {.*} {.*} {.*} {.*} $test]
981 proc mi_detect_async {} {
985 send_gdb "show target-async\n"
988 -re ".*Controlling the inferior in asynchronous mode is on...*$mi_gdb_prompt$" {
991 -re ".*$mi_gdb_prompt$" {
1001 # Wait for MI *stopped notification to appear.
1002 # The REASON, FUNC, ARGS, FILE and LINE are regular expressions
1003 # to match against whatever is output in *stopped. FILE may also match
1004 # filename of a file without debug info. ARGS should not include [] the
1005 # list of argument is enclosed in, and other regular expressions should
1006 # not include quotes.
1007 # If EXTRA is a list of one element, it's the regular expression
1008 # for output expected right after *stopped, and before GDB prompt.
1009 # If EXTRA is a list of two elements, the first element is for
1010 # output right after *stopped, and the second element is output
1011 # right after reason field. The regex after reason should not include
1012 # the comma separating it from the following fields.
1014 # When we fail to match output at all, -1 is returned. If FILE does
1015 # match and the target system has no debug info for FILE return 0.
1016 # Otherwise, the line at which we stop is returned. This is useful when
1017 # exact line is not possible to specify for some reason -- one can pass
1018 # the .* or "\[0-9\]*" regexps for line, and then check the line
1021 # Do not pass .* for any argument if you are expecting more than one stop.
1022 proc mi_expect_stop { reason func args file line extra test } {
1024 global mi_gdb_prompt
1027 global fullname_syntax
1029 global thread_selected_re
1030 global breakpoint_re
1034 set after_stopped ""
1036 if { [llength $extra] == 2 } {
1037 set after_stopped [lindex $extra 0]
1038 set after_reason [lindex $extra 1]
1039 set after_reason "${after_reason},"
1040 } elseif { [llength $extra] == 1 } {
1041 set after_stopped [lindex $extra 0]
1047 set prompt_re "$mi_gdb_prompt$"
1050 if { $reason == "really-no-reason" } {
1052 -re "\\*stopped\r\n$prompt_re" {
1056 fail "$test (unknown output after running)"
1062 if { $reason == "exited-normally" } {
1065 -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re" {
1068 -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
1070 fail "$test (unknown output after running)"
1076 if { $reason == "solib-event" } {
1077 set pattern "\\*stopped,reason=\"solib-event\",thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1078 verbose -log "mi_expect_stop: expecting: $pattern"
1084 fail "$test (unknown output after running)"
1090 set args "\\\[$args\\\]"
1093 if { $reason == "breakpoint-hit" } {
1094 set bn {bkptno="[0-9]+",}
1095 } elseif { $reason == "solib-event" } {
1100 if { $reason != "" } {
1101 set r "reason=\"$reason\","
1107 verbose -log "mi_expect_stop: expecting: \\*stopped,${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1109 -re "\\*stopped,${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1111 if {[array names expect_out "2,string"] != ""} {
1112 return $expect_out(2,string)
1114 # No debug info available but $file does match.
1117 -re "\\*stopped,${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$any\",args=\[\\\[\{\]$any\[\\\]\}\],file=\"$any\",fullname=\"${fullname_syntax}$any\",line=\"\[0-9\]*\"\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1118 verbose -log "got $expect_out(buffer)"
1119 fail "$test (stopped at wrong place)"
1122 -re ".*\r\n$mi_gdb_prompt$" {
1123 verbose -log "got $expect_out(buffer)"
1124 fail "$test (unknown output after running)"
1128 fail "$test (timeout)"
1134 # Wait for MI *stopped notification related to an interrupt request to
1136 proc mi_expect_interrupt { test } {
1137 global mi_gdb_prompt
1144 set prompt_re "$mi_gdb_prompt$"
1147 set r "reason=\"signal-received\",signal-name=\"0\",signal-meaning=\"Signal 0\""
1151 # A signal can land anywhere, just ignore the location
1152 verbose -log "mi_expect_interrupt: expecting: \\*stopped,${r}$any\r\n$prompt_re"
1154 -re "\\*stopped,${r}$any\r\n$prompt_re" {
1158 -re ".*\r\n$mi_gdb_prompt$" {
1159 verbose -log "got $expect_out(buffer)"
1160 fail "$test (unknown output after running)"
1164 fail "$test (timeout)"
1170 # cmd should not include the number or newline (i.e. "exec-step 3", not
1171 # "220-exec-step 3\n"
1173 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1174 # after the first prompt is printed.
1176 proc mi_execute_to { cmd reason func args file line extra test } {
1177 global suppress_flag
1178 if { $suppress_flag } {
1182 mi_send_resuming_command "$cmd" "$test"
1183 set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1187 proc mi_next_to { func args file line test } {
1188 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1189 "$file" "$line" "" "$test"
1192 proc mi_step_to { func args file line test } {
1193 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1194 "$file" "$line" "" "$test"
1197 proc mi_finish_to { func args file line result ret test } {
1198 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
1200 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1204 proc mi_continue_to {func} {
1205 mi_runto_helper $func "continue"
1208 proc mi0_execute_to { cmd reason func args file line extra test } {
1209 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
1210 "$file" "$line" "$extra" "$test"
1213 proc mi0_next_to { func args file line test } {
1214 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1215 "$file" "$line" "" "$test"
1218 proc mi0_step_to { func args file line test } {
1219 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1220 "$file" "$line" "" "$test"
1223 proc mi0_finish_to { func args file line result ret test } {
1224 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1226 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1230 proc mi0_continue_to { bkptno func args file line test } {
1231 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1232 "$func" "$args" "$file" "$line" "" "$test"
1235 # Creates a breakpoint and checks the reported fields are as expected
1236 proc mi_create_breakpoint { location number disp func file line address test } {
1237 verbose -log "Expecting: 222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",thread-groups=\\\[\".*\"\\\],times=\"0\",original-location=\".*\"\}"
1238 mi_gdb_test "222-break-insert $location" \
1239 "222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",thread-groups=\\\[\".*\"\\\],times=\"0\",original-location=\".*\"\}" \
1243 proc mi_list_breakpoints { expected test } {
1249 foreach item $expected {
1254 set number [lindex $item 0]
1255 set disp [lindex $item 1]
1256 set func [lindex $item 2]
1257 set file [lindex $item 3]
1258 set line [lindex $item 4]
1259 set address [lindex $item 5]
1260 set body "${body}bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\".*$file\",${fullname},line=\"$line\",thread-groups=\\\[\"i1\"\\\],times=\"0\",original-location=\".*\"\}"
1264 verbose -log "Expecting: 666\\\^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=\\\[$body\\\]\}"
1265 mi_gdb_test "666-break-list" \
1266 "666\\\^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=\\\[$body\\\]\}" \
1270 # Creates varobj named NAME for EXPRESSION.
1271 # Name cannot be "-".
1272 proc mi_create_varobj { name expression testname } {
1273 mi_gdb_test "-var-create $name * $expression" \
1274 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*,has_more=\"0\"" \
1278 proc mi_create_floating_varobj { name expression testname } {
1279 mi_gdb_test "-var-create $name @ $expression" \
1280 "\\^done,name=\"$name\",numchild=\"\(-1\|\[0-9\]+\)\",value=\".*\",type=.*" \
1285 # Same as mi_create_varobj, but also checks the reported type
1287 proc mi_create_varobj_checked { name expression type testname } {
1288 mi_gdb_test "-var-create $name * $expression" \
1289 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1293 # Same as mi_create_floating_varobj, but assumes the test is creating
1294 # a dynamic varobj that has children, so the value must be "{...}".
1295 # The "has_more" attribute is checked.
1296 proc mi_create_dynamic_varobj {name expression has_more testname} {
1297 mi_gdb_test "-var-create $name @ $expression" \
1298 "\\^done,name=\"$name\",numchild=\"0\",value=\"{\\.\\.\\.}\",type=.*,has_more=\"${has_more}\"" \
1302 # Deletes the specified NAME.
1303 proc mi_delete_varobj { name testname } {
1304 mi_gdb_test "-var-delete $name" \
1305 "\\^done,ndeleted=.*" \
1309 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1310 # are reported as updated, and no other varobj is updated.
1311 # Assumes that no varobj is out of scope and that no varobj changes
1313 proc mi_varobj_update { name expected testname } {
1314 set er "\\^done,changelist=\\\["
1316 foreach item $expected {
1317 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\",has_more=\".\"}"
1327 verbose -log "Expecting: $er" 2
1328 mi_gdb_test "-var-update $name" $er $testname
1331 proc mi_varobj_update_with_child_type_change { name child_name new_type new_children testname } {
1332 set v "{name=\"$child_name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\",has_more=\".\"}"
1333 set er "\\^done,changelist=\\\[$v\\\]"
1334 verbose -log "Expecting: $er"
1335 mi_gdb_test "-var-update $name" $er $testname
1338 proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1339 mi_varobj_update_with_child_type_change $name $name $new_type $new_children $testname
1342 # A helper that turns a key/value list into a regular expression
1343 # matching some MI output.
1344 proc mi_varobj_update_kv_helper {list} {
1347 foreach {key value} $list {
1352 if {$key == "new_children"} {
1353 append rx "$key=\\\[$value\\\]"
1355 append rx "$key=\"$value\""
1361 # A helper for mi_varobj_update_dynamic that computes a match
1362 # expression given a child list.
1363 proc mi_varobj_update_dynamic_helper {children} {
1367 foreach child $children {
1373 append crx [mi_varobj_update_kv_helper $child]
1380 # Update a dynamic varobj named NAME. CHILDREN is a list of children
1381 # that have been updated; NEW_CHILDREN is a list of children that were
1382 # added to the primary varobj. Each child is a list of key/value
1383 # pairs that are expected. SELF is a key/value list holding
1384 # information about the varobj itself. TESTNAME is the name of the
1386 proc mi_varobj_update_dynamic {name testname self children new_children} {
1387 if {[llength $new_children]} {
1388 set newrx [mi_varobj_update_dynamic_helper $new_children]
1389 lappend self new_children $newrx
1391 set selfrx [mi_varobj_update_kv_helper $self]
1392 set crx [mi_varobj_update_dynamic_helper $children]
1394 set er "\\^done,changelist=\\\[\{name=\"$name\",in_scope=\"true\""
1395 append er ",$selfrx\}"
1401 verbose -log "Expecting: $er"
1402 mi_gdb_test "-var-update $name" $er $testname
1405 proc mi_check_varobj_value { name value testname } {
1407 mi_gdb_test "-var-evaluate-expression $name" \
1408 "\\^done,value=\"$value\"" \
1412 # Helper proc which constructs a child regexp for
1413 # mi_list_varobj_children and mi_varobj_update_dynamic.
1414 proc mi_child_regexp {children add_child} {
1423 foreach item $children {
1425 set name [lindex $item 0]
1426 set exp [lindex $item 1]
1427 set numchild [lindex $item 2]
1428 if {[llength $item] == 5} {
1429 set type [lindex $item 3]
1430 set value [lindex $item 4]
1432 lappend children_exp\
1433 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1434 } elseif {[llength $item] == 4} {
1435 set type [lindex $item 3]
1437 lappend children_exp\
1438 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1440 lappend children_exp\
1441 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
1444 return [join $children_exp ","]
1447 # Check the results of the:
1449 # -var-list-children VARNAME
1451 # command. The CHILDREN parement should be a list of lists.
1452 # Each inner list can have either 3 or 4 elements, describing
1453 # fields that gdb is expected to report for child variable object,
1454 # in the following order
1458 # - Number of children
1461 # If inner list has 3 elements, the gdb is expected to output no
1462 # type for a child and no value.
1464 # If the inner list has 4 elements, gdb output is expected to
1467 proc mi_list_varobj_children { varname children testname } {
1468 mi_list_varobj_children_range $varname "" "" [llength $children] $children \
1472 # Like mi_list_varobj_children, but sets a subrange. NUMCHILDREN is
1473 # the total number of children.
1474 proc mi_list_varobj_children_range {varname from to numchildren children testname} {
1476 if {[llength $varname] == 2} {
1477 set options [lindex $varname 1]
1478 set varname [lindex $varname 0]
1481 set children_exp_j [mi_child_regexp $children 1]
1483 set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1485 set expected "\\^done,numchild=\"0\""
1489 append expected ",has_more=\"0\""
1490 } elseif {$to >= 0 && $numchildren > $to} {
1491 append expected ",has_more=\"1\""
1493 append expected ",has_more=\"0\""
1496 verbose -log "Expecting: $expected"
1498 mi_gdb_test "-var-list-children $options $varname $from $to" \
1502 # Verifies that variable object VARNAME has NUMBER children,
1503 # where each one is named $VARNAME.<index-of-child> and has type TYPE.
1504 proc mi_list_array_varobj_children { varname number type testname } {
1505 mi_list_array_varobj_children_with_index $varname $number 0 $type $testname
1508 # Same as mi_list_array_varobj_children, but allowing to pass a start index
1510 proc mi_list_array_varobj_children_with_index { varname number start_index \
1513 set index $start_index
1514 for {set i 0} {$i < $number} {incr i} {
1515 lappend t [list $varname.$index $index 0 $type]
1518 mi_list_varobj_children $varname $t $testname
1521 # A list of two-element lists. First element of each list is
1522 # a Tcl statement, and the second element is the line
1523 # number of source C file where the statement originates.
1524 set mi_autotest_data ""
1525 # The name of the source file for autotesting.
1526 set mi_autotest_source ""
1528 proc count_newlines { string } {
1529 return [regexp -all "\n" $string]
1532 # Prepares for running inline tests in FILENAME.
1533 # See comments for mi_run_inline_test for detailed
1534 # explanation of the idea and syntax.
1535 proc mi_prepare_inline_tests { filename } {
1539 global mi_autotest_source
1540 global mi_autotest_data
1542 set mi_autotest_data {}
1544 set mi_autotest_source $filename
1546 if { ! [regexp "^/" "$filename"] } then {
1547 set filename "$srcdir/$subdir/$filename"
1550 set chan [open $filename]
1551 set content [read $chan]
1554 set start [string first "/*:" $content]
1556 set end [string first ":*/" $content]
1558 error "Unterminated special comment in $filename"
1561 set prefix [string range $content 0 $start]
1562 set prefix_newlines [count_newlines $prefix]
1564 set line_number [expr $line_number+$prefix_newlines]
1565 set comment_line $line_number
1567 set comment [string range $content [expr $start+3] [expr $end-1]]
1569 set comment_newlines [count_newlines $comment]
1570 set line_number [expr $line_number+$comment_newlines]
1572 set comment [string trim $comment]
1573 set content [string range $content [expr $end+3] \
1574 [string length $content]]
1575 lappend mi_autotest_data [list $comment $comment_line]
1583 # Helper to mi_run_inline_test below.
1584 # Return the list of all (statement,line_number) lists
1585 # that comprise TESTCASE. The begin and end markers
1587 proc mi_get_inline_test {testcase} {
1589 global mi_gdb_prompt
1590 global mi_autotest_data
1591 global mi_autotest_source
1597 foreach l $mi_autotest_data {
1599 set comment [lindex $l 0]
1601 if {$comment == "BEGIN: $testcase"} {
1603 } elseif {$comment == "END: $testcase"} {
1606 } elseif {$seen_begin==1} {
1611 if {$seen_begin == 0} {
1612 error "Autotest $testcase not found"
1615 if {$seen_begin == 1 && $seen_end == 0} {
1616 error "Missing end marker for test $testcase"
1622 # Sets temporary breakpoint at LOCATION.
1623 proc mi_tbreak {location} {
1625 global mi_gdb_prompt
1627 mi_gdb_test "-break-insert -t $location" \
1629 "run to $location (set breakpoint)"
1632 # Send COMMAND that must be a command that resumes
1633 # the inferior (run/continue/next/etc) and consumes
1634 # the "^running" output from it.
1635 proc mi_send_resuming_command_raw {command test} {
1637 global mi_gdb_prompt
1638 global thread_selected_re
1639 global library_loaded_re
1641 send_gdb "$command\n"
1643 -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n($library_loaded_re)*($thread_selected_re)?${mi_gdb_prompt}" {
1644 # Note that lack of 'pass' call here -- this works around limitation
1645 # in DejaGNU xfail mechanism. mi-until.exp has this:
1647 # setup_kfail gdb/2104 "*-*-*"
1650 # and mi_execute_to uses mi_send_resuming_command. If we use 'pass' here,
1651 # it will reset kfail, so when the actual test fails, it will be flagged
1655 -re "\\^error,msg=\"Displaced stepping is only supported in ARM mode\".*" {
1656 unsupported "$test (Thumb mode)"
1659 -re "\\^error,msg=.*" {
1660 fail "$test (MI error)"
1663 -re ".*${mi_gdb_prompt}" {
1664 fail "$test (failed to resume)"
1674 proc mi_send_resuming_command {command test} {
1675 mi_send_resuming_command_raw -$command $test
1678 # Helper to mi_run_inline_test below.
1679 # Sets a temporary breakpoint at LOCATION and runs
1680 # the program using COMMAND. When the program is stopped
1681 # returns the line at which it. Returns -1 if line cannot
1683 # Does not check that the line is the same as requested.
1684 # The caller can check itself if required.
1685 proc mi_continue_to_line {location test} {
1688 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1689 return [mi_get_stop_line $test]
1692 # Wait until gdb prints the current line.
1693 proc mi_get_stop_line {test} {
1695 global mi_gdb_prompt
1701 set prompt_re "$mi_gdb_prompt$"
1705 -re ".*line=\"(\[0-9\]*)\".*\r\n$prompt_re" {
1706 return $expect_out(1,string)
1708 -re ".*$mi_gdb_prompt" {
1709 fail "wait for stop ($test)"
1712 fail "wait for stop ($test)"
1717 # Run a MI test embedded in comments in a C file.
1718 # The C file should contain special comments in the following
1721 # /*: BEGIN: testname :*/
1722 # /*: <Tcl statements> :*/
1723 # /*: END: testname :*/
1725 # This procedure find the begin and end marker for the requested
1726 # test. Then, a temporary breakpoint is set at the begin
1727 # marker and the program is run (from start).
1729 # After that, for each special comment between the begin and end
1730 # marker, the Tcl statements are executed. It is assumed that
1731 # for each comment, the immediately preceding line is executable
1732 # C statement. Then, gdb will be single-stepped until that
1733 # preceding C statement is executed, and after that the
1734 # Tcl statements in the comment will be executed.
1738 # /*: BEGIN: assignment-test :*/
1740 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1741 # /*: END: assignment-test :*/
1743 # The mi_prepare_inline_tests function should be called before
1744 # calling this function. A given C file can contain several
1745 # inline tests. The names of the tests must be unique within one
1748 proc mi_run_inline_test { testcase } {
1750 global mi_gdb_prompt
1753 global fullname_syntax
1754 global mi_autotest_source
1756 set commands [mi_get_inline_test $testcase]
1761 foreach c $commands {
1762 set statements [lindex $c 0]
1763 set line [lindex $c 1]
1764 set line [expr $line-1]
1766 # We want gdb to be stopped at the expression immediately
1767 # before the comment. If this is the first comment, the
1768 # program is either not started yet or is in some random place,
1769 # so we run it. For further comments, we might be already
1770 # standing at the right line. If not continue till the
1774 # Start the program afresh.
1775 mi_tbreak "$mi_autotest_source:$line"
1777 set line_now [mi_get_stop_line "$testcase: step to $line"]
1779 } elseif {$line_now!=$line} {
1780 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1783 if {$line_now!=$line} {
1784 fail "$testcase: go to line $line"
1787 # We're not at the statement right above the comment.
1788 # Execute that statement so that the comment can test
1789 # the state after the statement is executed.
1791 # Single-step past the line.
1792 if { [mi_send_resuming_command "exec-next" "$testcase: step over $line"] != 0 } {
1795 set line_now [mi_get_stop_line "$testcase: step over $line"]
1797 # We probably want to use 'uplevel' so that statements
1798 # have direct access to global variables that the
1799 # main 'exp' file has set up. But it's not yet clear,
1800 # will need more experience to be sure.
1805 proc get_mi_thread_list {name} {
1808 # MI will return a list of thread ids:
1811 # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
1813 mi_gdb_test "-thread-list-ids" \
1814 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1815 "-thread_list_ids ($name)"
1818 if {[info exists expect_out(buffer)]} {
1819 set output $expect_out(buffer)
1823 if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
1824 fail "finding threads in MI output ($name)"
1826 pass "finding threads in MI output ($name)"
1828 # Make list of console threads
1829 set start [expr {[string first \{ $threads] + 1}]
1830 set end [expr {[string first \} $threads] - 1}]
1831 set threads [string range $threads $start $end]
1832 foreach thread [split $threads ,] {
1833 if {[scan $thread {thread-id="%d"} num]} {
1834 lappend thread_list $num
1842 # Check that MI and the console know of the same threads.
1843 # Appends NAME to all test names.
1844 proc check_mi_and_console_threads {name} {
1847 mi_gdb_test "-thread-list-ids" \
1848 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1849 "-thread-list-ids ($name)"
1851 if {[info exists expect_out(buffer)]} {
1852 set mi_output $expect_out(buffer)
1855 # GDB will return a list of thread ids and some more info:
1858 # -interpreter-exec console "info threads"
1859 # ~" 4 Thread 2051 (LWP 7734) 0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
1860 # ~" 3 Thread 1026 (LWP 7733) () at __libc_nanosleep:-1"
1861 # ~" 2 Thread 2049 (LWP 7732) 0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
1862 # ~"* 1 Thread 1024 (LWP 7731) main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
1863 # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
1864 mi_gdb_test "info threads" \
1865 {.*(~".*"[\r\n]*)+.*} \
1866 "info threads ($name)"
1867 set console_output {}
1868 if {[info exists expect_out(buffer)]} {
1869 set console_output $expect_out(buffer)
1872 # Make a list of all known threads to console (gdb's thread IDs)
1873 set console_thread_list {}
1874 foreach line [split $console_output \n] {
1875 if {[string index $line 0] == "~"} {
1876 # This is a line from the console; trim off "~", " ", "*", and "\""
1877 set line [string trim $line ~\ \"\*]
1878 if {[scan $line "%d" id] == 1} {
1879 lappend console_thread_list $id
1884 # Now find the result string from MI
1886 foreach line [split $mi_output \n] {
1887 if {[string range $line 0 4] == "^done"} {
1891 if {$mi_result == ""} {
1892 fail "finding MI result string ($name)"
1894 pass "finding MI result string ($name)"
1897 # Finally, extract the thread ids and compare them to the console
1898 set num_mi_threads_str ""
1899 if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
1900 fail "finding number of threads in MI output ($name)"
1902 pass "finding number of threads in MI output ($name)"
1904 # Extract the number of threads from the MI result
1905 if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
1906 fail "got number of threads from MI ($name)"
1908 pass "got number of threads from MI ($name)"
1910 # Check if MI and console have same number of threads
1911 if {$num_mi_threads != [llength $console_thread_list]} {
1912 fail "console and MI have same number of threads ($name)"
1914 pass "console and MI have same number of threads ($name)"
1916 # Get MI thread list
1917 set mi_thread_list [get_mi_thread_list $name]
1919 # Check if MI and console have the same threads
1921 foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
1927 fail "MI and console have same threads ($name)"
1929 # Send a list of failures to the log
1930 send_log "Console has thread ids: $console_thread_list\n"
1931 send_log "MI has thread ids: $mi_thread_list\n"
1933 pass "MI and console have same threads ($name)"
1940 # Download shared libraries to the target.
1941 proc mi_load_shlibs { args } {
1942 if {![is_remote target]} {
1946 foreach file $args {
1947 gdb_download [shlib_target_file $file]
1950 # Even if the target supplies full paths for shared libraries,
1951 # they may not be paths for this system.
1952 mi_gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "\^done" ""
1955 proc mi_reverse_list { list } {
1956 if { [llength $list] <= 1 } {
1959 set tail [lrange $list 1 [llength $list]]
1960 set rtail [mi_reverse_list $tail]
1961 lappend rtail [lindex $list 0]
1965 proc mi_check_thread_states { xstates test } {
1967 set states [mi_reverse_list $xstates]
1968 set pattern ".*\\^done,threads=\\\["
1970 set pattern "${pattern}(.*)state=\"$s\""
1972 set pattern "${pattern}(,core=\"\[0-9\]*\")?\\\}\\\].*"
1974 verbose -log "expecting: $pattern"
1975 mi_gdb_test "-thread-info" $pattern $test
1978 # Return a list of MI features supported by this gdb.
1979 proc mi_get_features {} {
1980 global expect_out mi_gdb_prompt
1982 send_gdb "-list-features\n"
1985 -re "\\^done,features=\\\[(.*)\\\]\r\n$mi_gdb_prompt$" {
1986 regsub -all -- \" $expect_out(1,string) "" features
1987 return [split $features ,]
1989 -re ".*\r\n$mi_gdb_prompt$" {
1990 verbose -log "got $expect_out(buffer)"
1994 verbose -log "timeout in mi_gdb_prompt"
2000 # Variable Object Trees
2002 # Yet another way to check varobjs. Pass mi_walk_varobj_tree a "list" of
2003 # variables (not unlike the actual source code definition), and it will
2004 # automagically test the children for you (by default).
2026 # foo *f = new foo (); <-- break here
2028 # We want to check all the children of "f".
2030 # Translate the above structures into the following tree:
2036 # anonymous struct {
2047 # {const int *} iPtr {
2048 # {const int} {*iPtr} {}
2058 # mi_walk_varobj_tree c++ $tree
2060 # If you'd prefer to walk the tree using your own callback,
2061 # simply pass the name of the callback to mi_walk_varobj_tree.
2063 # This callback should take one argument, the name of the variable
2064 # to process. This name is the name of a global array holding the
2065 # variable's properties (object name, type, etc).
2067 # An example callback:
2069 # proc my_callback {var} {
2070 # upvar #0 $var varobj
2072 # puts "my_callback: called on varobj $varobj(obj_name)"
2075 # The arrays created for each variable object contain the following
2078 # obj_name - the object name for accessing this variable via MI
2079 # display_name - the display name for this variable (exp="display_name" in
2080 # the output of -var-list-children)
2081 # type - the type of this variable (type="type" in the output
2082 # of -var-list-children, or the special tag "anonymous"
2083 # path_expr - the "-var-info-path-expression" for this variable
2084 # NOTE: This member cannot be used reliably with typedefs.
2086 # See notes inside get_path_expr for more.
2087 # parent - the variable name of the parent varobj
2088 # children - a list of children variable names (which are the
2089 # names Tcl arrays, not object names)
2091 # For each variable object, an array containing the above fields will
2092 # be created under the root node (conveniently called, "root"). For example,
2093 # a variable object with handle "OBJ.public.0_anonymous.a" will have
2094 # a corresponding global Tcl variable named "root.OBJ.public.0_anonymous.a".
2096 # Note that right now, this mechanism cannot be used for recursive data
2097 # structures like linked lists.
2099 namespace eval ::varobj_tree {
2100 # An index which is appended to root varobjs to ensure uniqueness.
2101 variable _root_idx 0
2103 # A procedure to help with debuggging varobj trees.
2104 # VARIABLE_NAME is the name of the variable to dump.
2105 # CMD, if present, is the name of the callback to output the contstructed
2106 # strings. By default, it uses expect's "send_log" command.
2107 # TERM, if present, is a terminating character. By default it is the newline.
2109 # To output to the terminal (not the expect log), use
2110 # mi_varobj_tree_dump_variable my_variable puts ""
2112 proc mi_varobj_tree_dump_variable {variable_name {cmd send_log} {term "\n"}} {
2113 upvar #0 $variable_name varobj
2115 eval "$cmd \"VAR = $variable_name$term\""
2117 # Explicitly encode the array indices, since outputting them
2118 # in some logical order is better than what "array names" might
2120 foreach idx {obj_name parent display_name type path_expr} {
2121 eval "$cmd \"\t$idx = $varobj($idx)$term\""
2125 set num [llength $varobj(children)]
2126 eval "$cmd \"\tnum_children = $num$term\""
2128 eval "$cmd \"\tchildren = $varobj(children)$term\""
2132 # The default callback used by mi_walk_varobj_tree. This callback
2133 # simply checks all of VAR's children. It specifically does not test
2134 # path expressions, since that is very problematic.
2136 # This procedure may be used in custom callbacks.
2137 proc test_children_callback {variable_name} {
2138 upvar #0 $variable_name varobj
2140 if {[llength $varobj(children)] > 0} {
2141 # Construct the list of children the way mi_list_varobj_children
2142 # expects to get it:
2143 # { {obj_name display_name num_children type} ... }
2144 set children_list {}
2145 foreach child $varobj(children) {
2147 set clist [list [string_to_regexp $c(obj_name)] \
2148 [string_to_regexp $c(display_name)] \
2149 [llength $c(children)]]
2150 if {[string length $c(type)] > 0} {
2151 lappend clist [string_to_regexp $c(type)]
2153 lappend children_list $clist
2156 mi_list_varobj_children $varobj(obj_name) $children_list \
2157 "VT: list children of $varobj(obj_name)"
2161 # Set the properties of the varobj represented by
2162 # PARENT_VARIABLE - the name of the parent's variable
2163 # OBJNAME - the MI object name of this variable
2164 # DISP_NAME - the display name of this variable
2165 # TYPE - the type of this variable
2166 # PATH - the path expression for this variable
2167 # CHILDREN - a list of the variable's children
2168 proc create_varobj {parent_variable objname disp_name \
2169 type path children} {
2170 upvar #0 $parent_variable parent
2172 set var_name "root.$objname"
2174 array set $var_name [list obj_name $objname]
2175 array set $var_name [list display_name $disp_name]
2176 array set $var_name [list type $type]
2177 array set $var_name [list path_expr $path]
2178 array set $var_name [list parent "$parent_variable"]
2179 array set $var_name [list children \
2180 [get_tree_children $var_name $children]]
2184 # Should VARIABLE be used in path expressions? The CPLUS_FAKE_CHILD
2185 # varobjs and anonymous structs/unions are not used for path expressions.
2186 proc is_path_expr_parent {variable} {
2187 upvar #0 $variable varobj
2189 # If the varobj's type is "", it is a CPLUS_FAKE_CHILD.
2190 # If the tail of the varobj's object name is "%d_anonymous",
2191 # then it represents an anonymous struct or union.
2192 if {[string length $varobj(type)] == 0 \
2193 || [regexp {[0-9]+_anonymous$} $varobj(obj_name)]} {
2200 # Return the path expression for the variable named NAME in
2201 # parent varobj whose variable name is given by PARENT_VARIABLE.
2202 proc get_path_expr {parent_variable name type} {
2203 upvar #0 $parent_variable parent
2204 upvar #0 $parent_variable path_parent
2206 # If TYPE is "", this is one of the CPLUS_FAKE_CHILD varobjs,
2207 # which has no path expression. Likewsise for anonymous structs
2209 if {[string length $type] == 0 \
2210 || [string compare $type "anonymous"] == 0} {
2214 # Find the path parent variable.
2215 while {![is_path_expr_parent $parent_variable]} {
2216 set parent_variable $path_parent(parent)
2217 upvar #0 $parent_variable path_parent
2220 # This is where things get difficult. We do not actually know
2221 # the real type for variables defined via typedefs, so we don't actually
2222 # know whether the parent is a structure/union or not.
2224 # So we assume everything that isn't a simple type is a compound type.
2226 regexp {\*+} $parent(type) stars
2228 if {[string index $name 0] == "*"} {
2232 if {[string index $parent(type) end] == "\]"} {
2233 # Parent is an array.
2234 return "($path_parent(path_expr))\[$name\]"
2235 } elseif {$is_compound} {
2236 # Parent is a structure or union or a pointer to one.
2237 if {[string length $stars]} {
2245 # To make matters even more hideous, varobj.c has slightly different
2246 # path expressions for C and C++.
2247 set path_expr "($path_parent(path_expr))$join$name"
2248 if {[string compare -nocase $root(language) "c"] == 0} {
2251 return "($path_expr)"
2254 # Parent is a pointer.
2255 return "*($path_parent(path_expr))"
2259 # Process the CHILDREN (a list of varobj_tree elements) of the variable
2260 # given by PARENT_VARIABLE. Returns a list of children variables.
2261 proc get_tree_children {parent_variable children} {
2262 upvar #0 $parent_variable parent
2265 set children_list {}
2266 foreach {type name children} $children {
2267 if {[string compare $parent_variable "root"] == 0} {
2271 set objname "$name$_root_idx"
2272 set disp_name "$name"
2273 set path_expr "$name"
2274 } elseif {[string compare $type "anonymous"] == 0} {
2275 # Special case: anonymous types. In this case, NAME will either be
2276 # "struct" or "union".
2277 set objname "$parent(obj_name).${field_idx}_anonymous"
2278 set disp_name "<anonymous $name>"
2280 set type "$name {...}"
2282 set objname "$parent(obj_name).$name"
2284 set path_expr [get_path_expr $parent_variable $name $type]
2287 lappend children_list [create_varobj $parent_variable $objname \
2288 $disp_name $type $path_expr $children]
2292 return $children_list
2295 # The main procedure to call the given CALLBACK on the elements of the
2296 # given varobj TREE. See detailed explanation above.
2297 proc walk_tree {language tree callback} {
2301 if {[llength $tree] < 3} {
2302 error "tree does not contain enough elements"
2307 # Create root node and process the tree.
2308 array set root [list language $language]
2309 array set root [list obj_name "root"]
2310 array set root [list display_name "root"]
2311 array set root [list type "root"]
2312 array set root [list path_expr "root"]
2313 array set root [list parent "root"]
2314 array set root [list children [get_tree_children root $tree]]
2317 set all_nodes $root(children); # a stack of nodes
2318 while {[llength $all_nodes] > 0} {
2319 # "Pop" the name of the global variable containing this varobj's
2320 # information from the stack of nodes.
2321 set var_name [lindex $all_nodes 0]
2322 set all_nodes [lreplace $all_nodes 0 0]
2324 # Bring the global named in VAR_NAME into scope as the local variable
2326 upvar #0 $var_name varobj
2328 # Append any children of VAROBJ to the list of nodes to walk.
2329 if {[llength $varobj(children)] > 0} {
2330 set all_nodes [concat $all_nodes $varobj(children)]
2333 # If this is a root variable, create the variable object for it.
2334 if {[string compare $varobj(parent) "root"] == 0} {
2335 mi_create_varobj $varobj(obj_name) $varobj(display_name) \
2336 "VT: create root varobj for $varobj(display_name)"
2339 # Now call the callback for VAROBJ.
2340 uplevel #0 $callback $var_name
2345 # The default varobj tree callback, which simply tests -var-list-children.
2346 proc mi_varobj_tree_test_children_callback {variable} {
2347 ::varobj_tree::test_children_callback $variable
2350 # Walk the variable object tree given by TREE, calling the specified
2351 # CALLBACK. By default this uses mi_varobj_tree_test_children_callback.
2352 proc mi_walk_varobj_tree {language tree \
2354 mi_varobj_tree_test_children_callback}} {
2355 ::varobj_tree::walk_tree $language $tree $callback