1 # Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009
2 # Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # This file was based on a file written by Fred Fish. (fnf@cygnus.com)
19 # Test setup routines that work with the MI interpreter.
21 # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
22 # Set it if it is not already set.
24 if ![info exists mi_gdb_prompt] then {
25 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
28 global mi_inferior_spawn_id
29 global mi_inferior_tty_name
33 set thread_selected_re "=thread-selected,id=\"\[0-9+\]\"\r\n"
36 # mi_gdb_exit -- exit the GDB, killing the target program if necessary
39 catch mi_uncatched_gdb_exit
42 proc mi_uncatched_gdb_exit {} {
44 global INTERNAL_GDBFLAGS GDBFLAGS
51 gdb_stop_suppressing_tests;
53 if { [info procs sid_exit] != "" } {
57 if ![info exists gdb_spawn_id] {
61 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
63 if { [is_remote host] && [board_info host exists fileid] } {
64 send_gdb "999-gdb-exit\n";
70 -re "Undefined command.*$gdb_prompt $" {
74 -re "DOSEXIT code" { }
79 if ![is_remote host] {
86 # default_mi_gdb_start [INFERIOR_PTY] -- start gdb running, default procedure
88 # INFERIOR_PTY should be set to separate-inferior-tty to have the inferior work
89 # with it's own PTY. If set to same-inferior-tty, the inferior shares GDB's PTY.
90 # The default value is same-inferior-tty.
92 # When running over NFS, particularly if running many simultaneous
93 # tests on different hosts all using the same server, things can
94 # get really slow. Give gdb at least 3 minutes to start up.
96 proc default_mi_gdb_start { args } {
99 global INTERNAL_GDBFLAGS GDBFLAGS
106 gdb_stop_suppressing_tests;
107 set inferior_pty no-tty
109 if { [llength $args] == 1} {
110 set inferior_pty [lindex $args 0]
113 set separate_inferior_pty [string match $inferior_pty separate-inferior-tty]
116 if { [info procs sid_start] != "" } {
117 verbose "Spawning SID"
121 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
123 if [info exists gdb_spawn_id] {
127 if ![is_remote host] {
128 if { [which $GDB] == 0 } then {
129 perror "$GDB does not exist."
134 # Create the new PTY for the inferior process.
135 if { $separate_inferior_pty } {
137 global mi_inferior_spawn_id
138 global mi_inferior_tty_name
139 set mi_inferior_spawn_id $spawn_id
140 set mi_inferior_tty_name $spawn_out(slave,name)
143 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS [host_info gdb_opts]"];
144 if { $res < 0 || $res == "" } {
145 perror "Spawning $GDB failed."
149 -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
150 # We have a new format mi startup prompt. If we are
151 # running mi1, then this is an error as we should be
152 # using the old-style prompt.
153 if { $MIFLAGS == "-i=mi1" } {
154 perror "(mi startup) Got unexpected new mi prompt."
158 verbose "GDB initialized."
160 -re "\[^~\].*$mi_gdb_prompt$" {
161 # We have an old format mi startup prompt. If we are
162 # not running mi1, then this is an error as we should be
163 # using the new-style prompt.
164 if { $MIFLAGS != "-i=mi1" } {
165 perror "(mi startup) Got unexpected old mi prompt."
169 verbose "GDB initialized."
171 -re ".*unrecognized option.*for a complete list of options." {
172 untested "Skip mi tests (not compiled with mi support)."
176 -re ".*Interpreter `mi' unrecognized." {
177 untested "Skip mi tests (not compiled with mi support)."
182 perror "(timeout) GDB never initialized after 10 seconds."
189 # FIXME: mi output does not go through pagers, so these can be removed.
190 # force the height to "unlimited", so no pagers get used
191 send_gdb "100-gdb-set height 0\n"
193 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
194 verbose "Setting height to 0." 2
197 warning "Couldn't set the height to 0"
200 # force the width to "unlimited", so no wraparound occurs
201 send_gdb "101-gdb-set width 0\n"
203 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
204 verbose "Setting width to 0." 2
207 warning "Couldn't set the width to 0."
210 # If allowing the inferior to have its own PTY then assign the inferior
211 # its own terminal device here.
212 if { $separate_inferior_pty } {
213 send_gdb "102-inferior-tty-set $mi_inferior_tty_name\n"
215 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
216 verbose "redirect inferior output to new terminal device."
219 warning "Couldn't redirect inferior output." 2
230 # Overridable function. You can override this function in your
233 proc mi_gdb_start { args } {
234 return [default_mi_gdb_start $args]
237 # Many of the tests depend on setting breakpoints at various places and
238 # running until that breakpoint is reached. At times, we want to start
239 # with a clean-slate with respect to breakpoints, so this utility proc
240 # lets us do this without duplicating this code everywhere.
243 proc mi_delete_breakpoints {} {
246 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
247 send_gdb "102-break-delete\n"
249 -re "Delete all breakpoints.*y or n.*$" {
253 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
254 # This happens if there were no breakpoints
256 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
259 # The correct output is not "No breakpoints or watchpoints." but an
260 # empty BreakpointTable. Also, a query is not acceptable with mi.
261 send_gdb "103-break-list\n"
263 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
264 -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$" {}
265 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
266 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
267 -re "Delete all breakpoints.*or n.*$" {
268 warning "Unexpected prompt for breakpoints deletion";
272 timeout { perror "-break-list (timeout)" ; return }
276 proc mi_gdb_reinitialize_dir { subdir } {
281 if { $suppress_flag } {
285 if [is_remote host] {
289 if { $MIFLAGS == "-i=mi1" } {
290 send_gdb "104-environment-directory\n"
292 -re ".*Reinitialize source path to empty.*y or n. " {
293 warning "Got confirmation prompt for dir reinitialization."
296 -re "$mi_gdb_prompt$" {}
297 timeout {error "Dir reinitialization failed (timeout)"}
300 -re "$mi_gdb_prompt$" {}
301 timeout {error "Dir reinitialization failed (timeout)"}
304 send_gdb "104-environment-directory -r\n"
306 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
307 -re "$mi_gdb_prompt$" {}
308 timeout {error "Dir reinitialization failed (timeout)"}
312 send_gdb "105-environment-directory $subdir\n"
314 -re "Source directories searched.*$mi_gdb_prompt$" {
315 verbose "Dir set to $subdir"
317 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
318 # FIXME: We return just the prompt for now.
319 verbose "Dir set to $subdir"
320 # perror "Dir \"$subdir\" failed."
325 # Send GDB the "target" command.
326 # FIXME: Some of these patterns are not appropriate for MI. Based on
327 # config/monitor.exp:gdb_target_command.
328 proc mi_gdb_target_cmd { targetname serialport } {
331 set serialport_re [string_to_regexp $serialport]
332 for {set i 1} {$i <= 3} {incr i} {
333 send_gdb "47-target-select $targetname $serialport\n"
335 -re "47\\^connected.*$mi_gdb_prompt$" {
336 verbose "Set target to $targetname";
339 -re "unknown host.*$mi_gdb_prompt" {
340 verbose "Couldn't look up $serialport"
342 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
343 verbose "Connection failed";
345 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
346 verbose "Set target to $targetname";
349 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
350 verbose "Set target to $targetname";
353 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
354 verbose "Set target to $targetname";
357 -re "Connected to.*$mi_gdb_prompt$" {
358 verbose "Set target to $targetname";
361 -re "Ending remote.*$mi_gdb_prompt$" { }
362 -re "Connection refused.*$mi_gdb_prompt$" {
363 verbose "Connection refused by remote target. Pausing, and trying again."
367 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
368 verbose "Got timeout error from gdb.";
380 # load a file into the debugger (file command only).
381 # return a -1 if anything goes wrong.
383 proc mi_gdb_file_cmd { arg } {
389 global last_loaded_file
390 upvar timeout timeout
392 set last_loaded_file $arg
394 if [is_remote host] {
395 set arg [remote_download host $arg];
397 error "download failed"
402 # FIXME: Several of these patterns are only acceptable for console
403 # output. Queries are an error for mi.
404 send_gdb "105-file-exec-and-symbols $arg\n"
406 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
407 verbose "\t\tLoaded $arg into the $GDB"
410 -re "has no symbol-table.*$mi_gdb_prompt$" {
411 perror "$arg wasn't compiled with \"-g\""
414 -re "Load new symbol table from \".*\".*y or n. $" {
417 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
418 verbose "\t\tLoaded $arg with new symbol table into $GDB"
422 perror "(timeout) Couldn't load $arg, other program already loaded."
427 -re "No such file or directory.*$mi_gdb_prompt$" {
428 perror "($arg) No such file or directory\n"
431 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
432 # We (MI) are just giving the prompt back for now, instead of giving
433 # some acknowledgement.
437 perror "couldn't load $arg into $GDB (timed out)."
441 # This is an attempt to detect a core dump, but seems not to
442 # work. Perhaps we need to match .* followed by eof, in which
443 # gdb_expect does not seem to have a way to do that.
444 perror "couldn't load $arg into $GDB (end of file)."
451 # connect to the target and download a file, if necessary.
452 # return a -1 if anything goes wrong.
454 proc mi_gdb_target_load { } {
460 upvar timeout timeout
462 if { [info procs gdbserver_gdb_load] != "" } {
463 mi_gdb_test "kill" ".*" ""
464 set res [gdbserver_gdb_load]
465 set protocol [lindex $res 0]
466 set gdbport [lindex $res 1]
468 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
471 } elseif { [info procs send_target_sid] != "" } {
472 # For SID, things get complex
475 -re ".*$mi_gdb_prompt$"
479 -re "\\^done.*$mi_gdb_prompt$" {
482 perror "Unable to connect to SID target"
486 send_gdb "48-target-download\n"
488 -re "48\\^done.*$mi_gdb_prompt$" {
491 perror "Unable to download to SID target"
495 } elseif { [target_info protocol] == "sim" } {
496 # For the simulator, just connect to it directly.
497 send_gdb "47-target-select sim\n"
499 -re "47\\^connected.*$mi_gdb_prompt$" {
502 perror "Unable to select sim target"
506 send_gdb "48-target-download\n"
508 -re "48\\^done.*$mi_gdb_prompt$" {
511 perror "Unable to download to sim target"
515 } elseif { [target_info gdb_protocol] == "remote" } {
517 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
518 perror "Unable to connect to remote target"
521 send_gdb "48-target-download\n"
523 -re "48\\^done.*$mi_gdb_prompt$" {
526 perror "Unable to download to remote target"
535 # load a file into the debugger.
536 # return a -1 if anything goes wrong.
538 proc mi_gdb_load { arg } {
540 return [mi_gdb_file_cmd $arg]
545 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
548 # COMMAND is the command to execute, send to GDB with send_gdb. If
549 # this is the null string no command is sent.
550 # PATTERN is the pattern to match for a PASS, and must NOT include
551 # the \r\n sequence immediately before the gdb prompt.
552 # MESSAGE is the message to be printed. (If this is the empty string,
553 # then sometimes we don't call pass or fail at all; I don't
554 # understand this at all.)
555 # IPATTERN is the pattern to match for the inferior's output. This parameter
556 # is optional. If present, it will produce a PASS if the match is
557 # successful, and a FAIL if unsuccessful.
560 # 1 if the test failed,
561 # 0 if the test passes,
562 # -1 if there was an internal error.
564 proc mi_gdb_test { args } {
567 global GDB expect_out
568 upvar timeout timeout
570 set command [lindex $args 0]
571 set pattern [lindex $args 1]
572 set message [lindex $args 2]
574 if [llength $args]==4 {
575 set ipattern [lindex $args 3]
578 if [llength $args]==5 {
579 set question_string [lindex $args 3];
580 set response_string [lindex $args 4];
582 set question_string "^FOOBAR$"
586 send_user "Sending \"$command\" to gdb\n"
587 send_user "Looking to match \"$pattern\"\n"
588 send_user "Message is \"$message\"\n"
592 set string "${command}\n";
593 set string_regex [string_to_regexp $command]
595 if { $command != "" } {
596 while { "$string" != "" } {
597 set foo [string first "\n" "$string"];
598 set len [string length "$string"];
599 if { $foo < [expr $len - 1] } {
600 set str [string range "$string" 0 $foo];
601 if { [send_gdb "$str"] != "" } {
602 global suppress_flag;
604 if { ! $suppress_flag } {
605 perror "Couldn't send $command to GDB.";
614 set string [string range "$string" [expr $foo + 1] end];
619 if { "$string" != "" } {
620 if { [send_gdb "$string"] != "" } {
621 global suppress_flag;
623 if { ! $suppress_flag } {
624 perror "Couldn't send $command to GDB.";
632 if [info exists timeout] {
636 if [info exists timeout] {
642 verbose -log "Expecting: ^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)"
644 -re "\\*\\*\\* DOSEXIT code.*" {
645 if { $message != "" } {
648 gdb_suppress_entire_file "GDB died";
651 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
652 if ![isnative] then {
653 warning "Can`t communicate to remote target."
659 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
660 # At this point, $expect_out(1,string) is the MI input command.
661 # and $expect_out(2,string) is the MI output command.
662 # If $expect_out(1,string) is "", then there was no MI input command here.
664 # NOTE, there is no trailing anchor because with GDB/MI,
665 # asynchronous responses can happen at any point, causing more
666 # data to be available. Normally an anchor is used to make
667 # sure the end of the output is matched, however, $mi_gdb_prompt
668 # is just as good of an anchor since mi_gdb_test is meant to
669 # match a single mi output command. If a second GDB/MI output
670 # response is sent, it will be in the buffer for the next
671 # time mi_gdb_test is called.
672 if ![string match "" $message] then {
677 -re "(${question_string})$" {
678 send_gdb "$response_string\n";
681 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
682 perror "Undefined command \"$command\"."
686 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
687 perror "\"$command\" is not a unique command name."
691 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
692 if ![string match "" $message] then {
693 set errmsg "$message (the program exited)"
695 set errmsg "$command (the program exited)"
700 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
701 if ![string match "" $message] then {
702 set errmsg "$message (the program is no longer running)"
704 set errmsg "$command (the program is no longer running)"
709 -re ".*$mi_gdb_prompt\[ \]*$" {
710 if ![string match "" $message] then {
717 perror "Window too small."
720 -re "\\(y or n\\) " {
722 perror "Got interactive prompt."
726 perror "Process no longer exists"
727 if { $message != "" } {
733 perror "internal buffer is full."
737 if ![string match "" $message] then {
738 fail "$message (timeout)"
744 # If the GDB output matched, compare the inferior output.
745 if { $result == 0 } {
746 if [ info exists ipattern ] {
747 if { ![target_info exists gdb,noinferiorio] } {
748 global mi_inferior_spawn_id
750 -i $mi_inferior_spawn_id -re "$ipattern" {
751 pass "$message inferior output"
754 fail "$message inferior output (timeout)"
759 unsupported "$message inferior output"
768 # MI run command. (A modified version of gdb_run_cmd)
771 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
772 # ``.*$'' could swallow up output that we attempt to match elsewhere.
774 proc mi_run_cmd {args} {
776 if { $suppress_flag } {
780 global thread_selected_re
782 if [target_info exists gdb_init_command] {
783 send_gdb "[target_info gdb_init_command]\n";
785 -re "$mi_gdb_prompt$" { }
787 perror "gdb_init_command for target failed";
793 if { [mi_gdb_target_load] < 0 } {
797 if [target_info exists use_gdb_stub] {
798 if [target_info exists gdb,do_reload_on_run] {
799 send_gdb "220-exec-continue\n";
801 -re "220\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\r\n$mi_gdb_prompt" {}
807 if [target_info exists gdb,start_symbol] {
808 set start [target_info gdb,start_symbol];
813 # HACK: Should either use 000-jump or fix the target code
814 # to better handle RUN.
815 send_gdb "jump *$start\n"
816 warning "Using CLI jump command, expect run-to-main FAIL"
820 send_gdb "220-exec-run $args\n"
822 -re "220\\^running\r\n(\\*running,thread-id=\"\[^\"\]+\"\r\n|=thread-created,id=\"1\",group-id=\"\[0-9\]+\"\r\n)*(${thread_selected_re})?${mi_gdb_prompt}" {
825 perror "Unable to start target"
829 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
833 # Just like run-to-main but works with the MI interface
836 proc mi_run_to_main { } {
838 if { $suppress_flag } {
847 mi_delete_breakpoints
848 mi_gdb_reinitialize_dir $srcdir/$subdir
849 mi_gdb_load ${binfile}
855 # Just like gdb's "runto" proc, it will run the target to a given
856 # function. The big difference here between mi_runto and mi_execute_to
857 # is that mi_execute_to must have the inferior running already. This
858 # proc will (like gdb's runto) (re)start the inferior, too.
860 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
862 # -1 if test suppressed, failed, timedout
865 proc mi_runto_helper {func run_or_continue} {
867 if { $suppress_flag } {
871 global mi_gdb_prompt expect_out
872 global hex decimal fullname_syntax
874 set test "mi runto $func"
875 mi_gdb_test "200-break-insert -t $func" \
876 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\",original-location=\".*\"\}" \
877 "breakpoint at $func"
879 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
880 || ![scan $str {number="%d"} bkptno]} {
884 if {$run_or_continue == "run"} {
887 mi_send_resuming_command "exec-continue" "$test"
890 mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test
893 proc mi_runto {func} {
894 mi_runto_helper $func "run"
897 # Next to the next statement
898 # For return values, see mi_execute_to_helper
900 proc mi_next { test } {
901 return [mi_next_to {.*} {.*} {.*} {.*} $test]
905 # Step to the next statement
906 # For return values, see mi_execute_to_helper
908 proc mi_step { test } {
909 return [mi_step_to {.*} {.*} {.*} {.*} $test]
914 proc detect_async {} {
918 send_gdb "show target-async\n"
921 -re ".*Controlling the inferior in asynchronous mode is on...*$mi_gdb_prompt$" {
924 -re ".*$mi_gdb_prompt$" {
934 # Wait for MI *stopped notification to appear.
935 # The REASON, FUNC, ARGS, FILE and LINE are regular expressions
936 # to match against whatever is output in *stopped. ARGS should
937 # not include [] the list of argument is enclosed in, and other
938 # regular expressions should not include quotes.
939 # If EXTRA is a list of one element, it's the regular expression
940 # for output expected right after *stopped, and before GDB prompt.
941 # If EXTRA is a list of two elements, the first element is for
942 # output right after *stopped, and the second element is output
943 # right after reason field. The regex after reason should not include
944 # the comma separating it from the following fields.
946 # When we fail to match output at all, -1 is returned. Otherwise,
947 # the line at which we stop is returned. This is useful when exact
948 # line is not possible to specify for some reason -- one can pass
949 # the .* or "\[0-9\]*" regexps for line, and then check the line
952 # Do not pass .* for any argument if you are expecting more than one stop.
953 proc mi_expect_stop { reason func args file line extra test } {
958 global fullname_syntax
960 global thread_selected_re
964 if { [llength $extra] == 2 } {
965 set after_stopped [lindex $extra 0]
966 set after_reason [lindex $extra 1]
967 set after_reason "${after_reason},"
968 } elseif { [llength $extra] == 1 } {
969 set after_stopped [lindex $extra 0]
975 set prompt_re "$mi_gdb_prompt$"
978 if { $reason == "really-no-reason" } {
980 -re "\\*stopped\r\n$prompt_re" {
984 fail "$test (unknown output after running)"
990 if { $reason == "exited-normally" } {
993 -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re" {
996 -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
998 fail "$test (unknown output after running)"
1004 set args "\\\[$args\\\]"
1007 if { $reason == "breakpoint-hit" } {
1008 set bn {bkptno="[0-9]+",}
1012 if { $reason != "" } {
1013 set r "reason=\"$reason\","
1021 verbose -log "mi_expect_stop: expecting: \\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=$any,frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\"\}$after_stopped\r\n($thread_selected_re)?$prompt_re"
1023 -re "\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=$any,frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\"\}$after_stopped\r\n($thread_selected_re)?$prompt_re" {
1025 return $expect_out(2,string)
1027 -re "\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=$any,frame=\{addr=\"$hex\",func=\"$any\",args=\[\\\[\{\]$any\[\\\]\}\],file=\"$any\",fullname=\"${fullname_syntax}$any\",line=\"\[0-9\]*\"\}$any\r\n$prompt_re" {
1028 verbose -log "got $expect_out(buffer)"
1029 fail "$test (stopped at wrong place)"
1032 -re ".*\r\n$mi_gdb_prompt$" {
1033 verbose -log "got $expect_out(buffer)"
1034 fail "$test (unknown output after running)"
1038 fail "$test (timeout)"
1044 # Wait for MI *stopped notification related to an interrupt request to
1046 proc mi_expect_interrupt { test } {
1047 global mi_gdb_prompt
1054 set prompt_re "$mi_gdb_prompt$"
1057 set r "reason=\"signal-received\",signal-name=\"0\",signal-meaning=\"Signal 0\""
1061 # A signal can land anywhere, just ignore the location
1062 verbose -log "mi_expect_interrupt: expecting: \\*stopped,${r},thread-id=\"$decimal\",stopped-threads=$any\r\n$prompt_re"
1064 -re "\\*stopped,${r},thread-id=\"$decimal\",stopped-threads=$any\r\n$prompt_re" {
1068 -re ".*\r\n$mi_gdb_prompt$" {
1069 verbose -log "got $expect_out(buffer)"
1070 fail "$test (unknown output after running)"
1074 fail "$test (timeout)"
1080 # cmd should not include the number or newline (i.e. "exec-step 3", not
1081 # "220-exec-step 3\n"
1083 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1084 # after the first prompt is printed.
1086 proc mi_execute_to { cmd reason func args file line extra test } {
1087 global suppress_flag
1088 if { $suppress_flag } {
1092 mi_send_resuming_command "$cmd" "$test"
1093 set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1097 proc mi_next_to { func args file line test } {
1098 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1099 "$file" "$line" "" "$test"
1102 proc mi_step_to { func args file line test } {
1103 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1104 "$file" "$line" "" "$test"
1107 proc mi_finish_to { func args file line result ret test } {
1108 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
1110 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1114 proc mi_continue_to {func} {
1115 mi_runto_helper $func "continue"
1118 proc mi0_execute_to { cmd reason func args file line extra test } {
1119 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
1120 "$file" "$line" "$extra" "$test"
1123 proc mi0_next_to { func args file line test } {
1124 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1125 "$file" "$line" "" "$test"
1128 proc mi0_step_to { func args file line test } {
1129 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1130 "$file" "$line" "" "$test"
1133 proc mi0_finish_to { func args file line result ret test } {
1134 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1136 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1140 proc mi0_continue_to { bkptno func args file line test } {
1141 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1142 "$func" "$args" "$file" "$line" "" "$test"
1145 # Creates a breakpoint and checks the reported fields are as expected
1146 proc mi_create_breakpoint { location number disp func file line address test } {
1147 verbose -log "Expecting: 222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}"
1148 mi_gdb_test "222-break-insert $location" \
1149 "222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}" \
1153 proc mi_list_breakpoints { expected test } {
1159 foreach item $children {
1163 set number disp func file line address
1164 set number [lindex $item 0]
1165 set disp [lindex $item 1]
1166 set func [lindex $item 2]
1167 set line [lindex $item 3]
1168 set address [lindex $item 4]
1169 set body "$body,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",${fullname},line=\"$line\",times=\"0\",original-location=\".*\"\}"
1173 verbose -log "Expecint: 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\\\]\}" \
1174 mi_gdb_test "666-break-list" \
1175 "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\\\]\}" \
1179 # Creates varobj named NAME for EXPRESSION.
1180 # Name cannot be "-".
1181 proc mi_create_varobj { name expression testname } {
1182 mi_gdb_test "-var-create $name * $expression" \
1183 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1187 proc mi_create_floating_varobj { name expression testname } {
1188 mi_gdb_test "-var-create $name @ $expression" \
1189 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1194 # Same as mi_create_varobj, but also checks the reported type
1196 proc mi_create_varobj_checked { name expression type testname } {
1197 mi_gdb_test "-var-create $name * $expression" \
1198 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1202 # Deletes the specified NAME.
1203 proc mi_delete_varobj { name testname } {
1204 mi_gdb_test "-var-delete $name" \
1205 "\\^done,ndeleted=.*" \
1209 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1210 # are reported as updated, and no other varobj is updated.
1211 # Assumes that no varobj is out of scope and that no varobj changes
1213 proc mi_varobj_update { name expected testname } {
1214 set er "\\^done,changelist=\\\["
1216 foreach item $expected {
1217 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1227 verbose -log "Expecting: $er" 2
1228 mi_gdb_test "-var-update $name" $er $testname
1231 proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1232 set v "{name=\"$name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\"}"
1233 set er "\\^done,changelist=\\\[$v\\\]"
1234 verbose -log "Expecting: $er"
1235 mi_gdb_test "-var-update $name" $er $testname
1238 proc mi_check_varobj_value { name value testname } {
1240 mi_gdb_test "-var-evaluate-expression $name" \
1241 "\\^done,value=\"$value\"" \
1245 # Check the results of the:
1247 # -var-list-children VARNAME
1249 # command. The CHILDREN parement should be a list of lists.
1250 # Each inner list can have either 3 or 4 elements, describing
1251 # fields that gdb is expected to report for child variable object,
1252 # in the following order
1256 # - Number of children
1259 # If inner list has 3 elements, the gdb is expected to output no
1260 # type for a child and no value.
1262 # If the inner list has 4 elements, gdb output is expected to
1265 proc mi_list_varobj_children { varname children testname } {
1268 if {[llength $varname] == 2} {
1269 set options [lindex $varname 1]
1270 set varname [lindex $varname 0]
1273 set numchildren [llength $children]
1275 set whatever "\"\[^\"\]+\""
1277 foreach item $children {
1279 set name [lindex $item 0]
1280 set exp [lindex $item 1]
1281 set numchild [lindex $item 2]
1282 if {[llength $item] == 5} {
1283 set type [lindex $item 3]
1284 set value [lindex $item 4]
1286 lappend children_exp\
1287 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
1288 } elseif {[llength $item] == 4} {
1289 set type [lindex $item 3]
1291 lappend children_exp\
1292 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
1294 lappend children_exp\
1295 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
1298 set children_exp_j [join $children_exp ","]
1300 set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1302 set expected "\\^done,numchild=\"0\""
1305 verbose -log "Expecting: $expected"
1307 mi_gdb_test "-var-list-children $options $varname" $expected $testname
1310 # Verifies that variable object VARNAME has NUMBER children,
1311 # where each one is named $VARNAME.<index-of-child> and has type TYPE.
1312 proc mi_list_array_varobj_children { varname number type testname } {
1314 for {set i 0} {$i < $number} {incr i} {
1315 lappend t [list $varname.$i $i 0 $type]
1317 mi_list_varobj_children $varname $t $testname
1320 # A list of two-element lists. First element of each list is
1321 # a Tcl statement, and the second element is the line
1322 # number of source C file where the statement originates.
1323 set mi_autotest_data ""
1324 # The name of the source file for autotesting.
1325 set mi_autotest_source ""
1327 proc count_newlines { string } {
1328 return [regexp -all "\n" $string]
1331 # Prepares for running inline tests in FILENAME.
1332 # See comments for mi_run_inline_test for detailed
1333 # explanation of the idea and syntax.
1334 proc mi_prepare_inline_tests { filename } {
1338 global mi_autotest_source
1339 global mi_autotest_data
1341 set mi_autotest_data {}
1343 set mi_autotest_source $filename
1345 if { ! [regexp "^/" "$filename"] } then {
1346 set filename "$srcdir/$subdir/$filename"
1349 set chan [open $filename]
1350 set content [read $chan]
1353 set start [string first "/*:" $content]
1355 set end [string first ":*/" $content]
1357 error "Unterminated special comment in $filename"
1360 set prefix [string range $content 0 $start]
1361 set prefix_newlines [count_newlines $prefix]
1363 set line_number [expr $line_number+$prefix_newlines]
1364 set comment_line $line_number
1366 set comment [string range $content [expr $start+3] [expr $end-1]]
1368 set comment_newlines [count_newlines $comment]
1369 set line_number [expr $line_number+$comment_newlines]
1371 set comment [string trim $comment]
1372 set content [string range $content [expr $end+3] \
1373 [string length $content]]
1374 lappend mi_autotest_data [list $comment $comment_line]
1382 # Helper to mi_run_inline_test below.
1383 # Return the list of all (statement,line_number) lists
1384 # that comprise TESTCASE. The begin and end markers
1386 proc mi_get_inline_test {testcase} {
1388 global mi_gdb_prompt
1389 global mi_autotest_data
1390 global mi_autotest_source
1396 foreach l $mi_autotest_data {
1398 set comment [lindex $l 0]
1400 if {$comment == "BEGIN: $testcase"} {
1402 } elseif {$comment == "END: $testcase"} {
1405 } elseif {$seen_begin==1} {
1410 if {$seen_begin == 0} {
1411 error "Autotest $testcase not found"
1414 if {$seen_begin == 1 && $seen_end == 0} {
1415 error "Missing end marker for test $testcase"
1421 # Sets temporary breakpoint at LOCATION.
1422 proc mi_tbreak {location} {
1424 global mi_gdb_prompt
1426 mi_gdb_test "-break-insert -t $location" \
1428 "run to $location (set breakpoint)"
1431 # Send COMMAND that must be a command that resumes
1432 # the inferiour (run/continue/next/etc) and consumes
1433 # the "^running" output from it.
1434 proc mi_send_resuming_command_raw {command test} {
1436 global mi_gdb_prompt
1437 global thread_selected_re
1439 send_gdb "$command\n"
1441 -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n($thread_selected_re)?${mi_gdb_prompt}" {
1442 # Note that lack of 'pass' call here -- this works around limitation
1443 # in DejaGNU xfail mechanism. mi-until.exp has this:
1445 # setup_kfail gdb/2104 "*-*-*"
1448 # and mi_execute_to uses mi_send_resuming_command. If we use 'pass' here,
1449 # it will reset kfail, so when the actual test fails, it will be flagged
1453 -re ".*${mi_gdb_prompt}" {
1454 fail "$test (failed to resume)"
1457 -re "\\^error,msg=.*" {
1458 fail "$test (MI error)"
1468 proc mi_send_resuming_command {command test} {
1469 mi_send_resuming_command_raw -$command $test
1472 # Helper to mi_run_inline_test below.
1473 # Sets a temporary breakpoint at LOCATION and runs
1474 # the program using COMMAND. When the program is stopped
1475 # returns the line at which it. Returns -1 if line cannot
1477 # Does not check that the line is the same as requested.
1478 # The caller can check itself if required.
1479 proc mi_continue_to_line {location test} {
1482 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1483 return [mi_get_stop_line $test]
1486 # Wait until gdb prints the current line.
1487 proc mi_get_stop_line {test} {
1489 global mi_gdb_prompt
1495 set prompt_re "$mi_gdb_prompt$"
1499 -re ".*line=\"(\[0-9\]*)\".*\r\n$prompt_re" {
1500 return $expect_out(1,string)
1502 -re ".*$mi_gdb_prompt" {
1503 fail "wait for stop ($test)"
1506 fail "wait for stop ($test)"
1511 # Run a MI test embedded in comments in a C file.
1512 # The C file should contain special comments in the following
1515 # /*: BEGIN: testname :*/
1516 # /*: <Tcl statements> :*/
1517 # /*: END: testname :*/
1519 # This procedure find the begin and end marker for the requested
1520 # test. Then, a temporary breakpoint is set at the begin
1521 # marker and the program is run (from start).
1523 # After that, for each special comment between the begin and end
1524 # marker, the Tcl statements are executed. It is assumed that
1525 # for each comment, the immediately preceding line is executable
1526 # C statement. Then, gdb will be single-stepped until that
1527 # preceding C statement is executed, and after that the
1528 # Tcl statements in the comment will be executed.
1532 # /*: BEGIN: assignment-test :*/
1534 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1535 # /*: END: assignment-test :*/
1537 # The mi_prepare_inline_tests function should be called before
1538 # calling this function. A given C file can contain several
1539 # inline tests. The names of the tests must be unique within one
1542 proc mi_run_inline_test { testcase } {
1544 global mi_gdb_prompt
1547 global fullname_syntax
1548 global mi_autotest_source
1550 set commands [mi_get_inline_test $testcase]
1555 foreach c $commands {
1556 set statements [lindex $c 0]
1557 set line [lindex $c 1]
1558 set line [expr $line-1]
1560 # We want gdb to be stopped at the expression immediately
1561 # before the comment. If this is the first comment, the
1562 # program is either not started yet or is in some random place,
1563 # so we run it. For further comments, we might be already
1564 # standing at the right line. If not continue till the
1568 # Start the program afresh.
1569 mi_tbreak "$mi_autotest_source:$line"
1571 set line_now [mi_get_stop_line "$testcase: step to $line"]
1573 } elseif {$line_now!=$line} {
1574 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1577 if {$line_now!=$line} {
1578 fail "$testcase: go to line $line"
1581 # We're not at the statement right above the comment.
1582 # Execute that statement so that the comment can test
1583 # the state after the statement is executed.
1585 # Single-step past the line.
1586 if { [mi_send_resuming_command "exec-next" "$testcase: step over $line"] != 0 } {
1589 set line_now [mi_get_stop_line "$testcase: step over $line"]
1591 # We probably want to use 'uplevel' so that statements
1592 # have direct access to global variables that the
1593 # main 'exp' file has set up. But it's not yet clear,
1594 # will need more experience to be sure.
1599 proc get_mi_thread_list {name} {
1602 # MI will return a list of thread ids:
1605 # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
1607 mi_gdb_test "-thread-list-ids" \
1608 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1609 "-thread_list_ids ($name)"
1612 if {[info exists expect_out(buffer)]} {
1613 set output $expect_out(buffer)
1617 if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
1618 fail "finding threads in MI output ($name)"
1620 pass "finding threads in MI output ($name)"
1622 # Make list of console threads
1623 set start [expr {[string first \{ $threads] + 1}]
1624 set end [expr {[string first \} $threads] - 1}]
1625 set threads [string range $threads $start $end]
1626 foreach thread [split $threads ,] {
1627 if {[scan $thread {thread-id="%d"} num]} {
1628 lappend thread_list $num
1636 # Check that MI and the console know of the same threads.
1637 # Appends NAME to all test names.
1638 proc check_mi_and_console_threads {name} {
1641 mi_gdb_test "-thread-list-ids" \
1642 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1643 "-thread-list-ids ($name)"
1645 if {[info exists expect_out(buffer)]} {
1646 set mi_output $expect_out(buffer)
1649 # GDB will return a list of thread ids and some more info:
1652 # -interpreter-exec console "info threads"
1653 # ~" 4 Thread 2051 (LWP 7734) 0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
1654 # ~" 3 Thread 1026 (LWP 7733) () at __libc_nanosleep:-1"
1655 # ~" 2 Thread 2049 (LWP 7732) 0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
1656 # ~"* 1 Thread 1024 (LWP 7731) main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
1657 # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
1658 mi_gdb_test "info threads" \
1659 {.*(~".*"[\r\n]*)+.*} \
1660 "info threads ($name)"
1661 set console_output {}
1662 if {[info exists expect_out(buffer)]} {
1663 set console_output $expect_out(buffer)
1666 # Make a list of all known threads to console (gdb's thread IDs)
1667 set console_thread_list {}
1668 foreach line [split $console_output \n] {
1669 if {[string index $line 0] == "~"} {
1670 # This is a line from the console; trim off "~", " ", "*", and "\""
1671 set line [string trim $line ~\ \"\*]
1672 if {[scan $line "%d" id] == 1} {
1673 lappend console_thread_list $id
1678 # Now find the result string from MI
1680 foreach line [split $mi_output \n] {
1681 if {[string range $line 0 4] == "^done"} {
1685 if {$mi_result == ""} {
1686 fail "finding MI result string ($name)"
1688 pass "finding MI result string ($name)"
1691 # Finally, extract the thread ids and compare them to the console
1692 set num_mi_threads_str ""
1693 if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
1694 fail "finding number of threads in MI output ($name)"
1696 pass "finding number of threads in MI output ($name)"
1698 # Extract the number of threads from the MI result
1699 if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
1700 fail "got number of threads from MI ($name)"
1702 pass "got number of threads from MI ($name)"
1704 # Check if MI and console have same number of threads
1705 if {$num_mi_threads != [llength $console_thread_list]} {
1706 fail "console and MI have same number of threads ($name)"
1708 pass "console and MI have same number of threads ($name)"
1710 # Get MI thread list
1711 set mi_thread_list [get_mi_thread_list $name]
1713 # Check if MI and console have the same threads
1715 foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
1721 fail "MI and console have same threads ($name)"
1723 # Send a list of failures to the log
1724 send_log "Console has thread ids: $console_thread_list\n"
1725 send_log "MI has thread ids: $mi_thread_list\n"
1727 pass "MI and console have same threads ($name)"
1734 proc mi_load_shlibs { args } {
1735 if {![is_remote target]} {
1739 foreach file $args {
1743 # Even if the target supplies full paths for shared libraries,
1744 # they may not be paths for this system.
1745 mi_gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "\^done" ""
1748 proc mi_reverse_list { list } {
1749 if { [llength $list] <= 1 } {
1752 set tail [lrange $list 1 [llength $list]]
1753 set rtail [mi_reverse_list $tail]
1754 lappend rtail [lindex $list 0]
1758 proc mi_check_thread_states { xstates test } {
1760 set states [mi_reverse_list $xstates]
1761 set pattern "\\^done,threads=\\\["
1763 set pattern "${pattern}(.*)state=\"$s\""
1765 set pattern "$pattern\\\}\\\].*"
1767 verbose -log "expecting: $pattern"
1768 mi_gdb_test "-thread-info" $pattern $test