1 # Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008
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 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
20 # This file was based on a file written by Fred Fish. (fnf@cygnus.com)
22 # Test setup routines that work with the MI interpreter.
24 # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
25 # Set it if it is not already set.
27 if ![info exists mi_gdb_prompt] then {
28 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
31 global mi_inferior_spawn_id
32 global mi_inferior_tty_name
37 # mi_gdb_exit -- exit the GDB, killing the target program if necessary
40 catch mi_uncatched_gdb_exit
43 proc mi_uncatched_gdb_exit {} {
52 gdb_stop_suppressing_tests;
54 if { [info procs sid_exit] != "" } {
58 if ![info exists gdb_spawn_id] {
62 verbose "Quitting $GDB $GDBFLAGS $MIFLAGS"
64 if { [is_remote host] && [board_info host exists fileid] } {
65 send_gdb "999-gdb-exit\n";
71 -re "Undefined command.*$gdb_prompt $" {
75 -re "DOSEXIT code" { }
80 if ![is_remote host] {
87 # default_mi_gdb_start [INFERIOR_PTY] -- start gdb running, default procedure
89 # INFERIOR_PTY should be set to separate-inferior-tty to have the inferior work
90 # with it's own PTY. If set to same-inferior-tty, the inferior shares GDB's PTY.
91 # The default value is same-inferior-tty.
93 # When running over NFS, particularly if running many simultaneous
94 # tests on different hosts all using the same server, things can
95 # get really slow. Give gdb at least 3 minutes to start up.
97 proc default_mi_gdb_start { args } {
107 gdb_stop_suppressing_tests;
108 set inferior_pty no-tty
110 if { [llength $args] == 1} {
111 set inferior_pty [lindex $args 0]
114 set separate_inferior_pty [string match $inferior_pty separate-inferior-tty]
117 if { [info procs sid_start] != "" } {
118 verbose "Spawning SID"
122 verbose "Spawning $GDB -nw $GDBFLAGS $MIFLAGS"
124 if [info exists gdb_spawn_id] {
128 if ![is_remote host] {
129 if { [which $GDB] == 0 } then {
130 perror "$GDB does not exist."
135 # Create the new PTY for the inferior process.
136 if { $separate_inferior_pty } {
138 global mi_inferior_spawn_id
139 global mi_inferior_tty_name
140 set mi_inferior_spawn_id $spawn_id
141 set mi_inferior_tty_name $spawn_out(slave,name)
144 set res [remote_spawn host "$GDB -nw $GDBFLAGS $MIFLAGS [host_info gdb_opts]"];
145 if { $res < 0 || $res == "" } {
146 perror "Spawning $GDB failed."
150 -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
151 # We have a new format mi startup prompt. If we are
152 # running mi1, then this is an error as we should be
153 # using the old-style prompt.
154 if { $MIFLAGS == "-i=mi1" } {
155 perror "(mi startup) Got unexpected new mi prompt."
159 verbose "GDB initialized."
161 -re "\[^~\].*$mi_gdb_prompt$" {
162 # We have an old format mi startup prompt. If we are
163 # not running mi1, then this is an error as we should be
164 # using the new-style prompt.
165 if { $MIFLAGS != "-i=mi1" } {
166 perror "(mi startup) Got unexpected old mi prompt."
170 verbose "GDB initialized."
172 -re ".*unrecognized option.*for a complete list of options." {
173 untested "Skip mi tests (not compiled with mi support)."
177 -re ".*Interpreter `mi' unrecognized." {
178 untested "Skip mi tests (not compiled with mi support)."
183 perror "(timeout) GDB never initialized after 10 seconds."
190 # FIXME: mi output does not go through pagers, so these can be removed.
191 # force the height to "unlimited", so no pagers get used
192 send_gdb "100-gdb-set height 0\n"
194 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
195 verbose "Setting height to 0." 2
198 warning "Couldn't set the height to 0"
201 # force the width to "unlimited", so no wraparound occurs
202 send_gdb "101-gdb-set width 0\n"
204 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
205 verbose "Setting width to 0." 2
208 warning "Couldn't set the width to 0."
211 # If allowing the inferior to have its own PTY then assign the inferior
212 # its own terminal device here.
213 if { $separate_inferior_pty } {
214 send_gdb "102-inferior-tty-set $mi_inferior_tty_name\n"
216 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
217 verbose "redirect inferior output to new terminal device."
220 warning "Couldn't redirect inferior output." 2
231 # Overridable function. You can override this function in your
234 proc mi_gdb_start { args } {
235 return [default_mi_gdb_start $args]
238 # Many of the tests depend on setting breakpoints at various places and
239 # running until that breakpoint is reached. At times, we want to start
240 # with a clean-slate with respect to breakpoints, so this utility proc
241 # lets us do this without duplicating this code everywhere.
244 proc mi_delete_breakpoints {} {
247 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
248 send_gdb "102-break-delete\n"
250 -re "Delete all breakpoints.*y or n.*$" {
254 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
255 # This happens if there were no breakpoints
257 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
260 # The correct output is not "No breakpoints or watchpoints." but an
261 # empty BreakpointTable. Also, a query is not acceptable with mi.
262 send_gdb "103-break-list\n"
264 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
265 -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$" {}
266 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
267 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
268 -re "Delete all breakpoints.*or n.*$" {
269 warning "Unexpected prompt for breakpoints deletion";
273 timeout { perror "-break-list (timeout)" ; return }
277 proc mi_gdb_reinitialize_dir { subdir } {
282 if { $suppress_flag } {
286 if [is_remote host] {
290 if { $MIFLAGS == "-i=mi1" } {
291 send_gdb "104-environment-directory\n"
293 -re ".*Reinitialize source path to empty.*y or n. " {
294 warning "Got confirmation prompt for dir reinitialization."
297 -re "$mi_gdb_prompt$" {}
298 timeout {error "Dir reinitialization failed (timeout)"}
301 -re "$mi_gdb_prompt$" {}
302 timeout {error "Dir reinitialization failed (timeout)"}
305 send_gdb "104-environment-directory -r\n"
307 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
308 -re "$mi_gdb_prompt$" {}
309 timeout {error "Dir reinitialization failed (timeout)"}
313 send_gdb "105-environment-directory $subdir\n"
315 -re "Source directories searched.*$mi_gdb_prompt$" {
316 verbose "Dir set to $subdir"
318 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
319 # FIXME: We return just the prompt for now.
320 verbose "Dir set to $subdir"
321 # perror "Dir \"$subdir\" failed."
326 # Send GDB the "target" command.
327 # FIXME: Some of these patterns are not appropriate for MI. Based on
328 # config/monitor.exp:gdb_target_command.
329 proc mi_gdb_target_cmd { targetname serialport } {
332 set serialport_re [string_to_regexp $serialport]
333 for {set i 1} {$i <= 3} {incr i} {
334 send_gdb "47-target-select $targetname $serialport\n"
336 -re "47\\^connected.*$mi_gdb_prompt$" {
337 verbose "Set target to $targetname";
340 -re "unknown host.*$mi_gdb_prompt" {
341 verbose "Couldn't look up $serialport"
343 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
344 verbose "Connection failed";
346 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
347 verbose "Set target to $targetname";
350 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
351 verbose "Set target to $targetname";
354 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
355 verbose "Set target to $targetname";
358 -re "Connected to.*$mi_gdb_prompt$" {
359 verbose "Set target to $targetname";
362 -re "Ending remote.*$mi_gdb_prompt$" { }
363 -re "Connection refused.*$mi_gdb_prompt$" {
364 verbose "Connection refused by remote target. Pausing, and trying again."
368 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
369 verbose "Got timeout error from gdb.";
381 # load a file into the debugger (file command only).
382 # return a -1 if anything goes wrong.
384 proc mi_gdb_file_cmd { arg } {
390 global last_loaded_file
391 upvar timeout timeout
393 set last_loaded_file $arg
395 if [is_remote host] {
396 set arg [remote_download host $arg];
398 error "download failed"
403 # FIXME: Several of these patterns are only acceptable for console
404 # output. Queries are an error for mi.
405 send_gdb "105-file-exec-and-symbols $arg\n"
407 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
408 verbose "\t\tLoaded $arg into the $GDB"
411 -re "has no symbol-table.*$mi_gdb_prompt$" {
412 perror "$arg wasn't compiled with \"-g\""
415 -re "Load new symbol table from \".*\".*y or n. $" {
418 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
419 verbose "\t\tLoaded $arg with new symbol table into $GDB"
423 perror "(timeout) Couldn't load $arg, other program already loaded."
428 -re "No such file or directory.*$mi_gdb_prompt$" {
429 perror "($arg) No such file or directory\n"
432 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
433 # We (MI) are just giving the prompt back for now, instead of giving
434 # some acknowledgement.
438 perror "couldn't load $arg into $GDB (timed out)."
442 # This is an attempt to detect a core dump, but seems not to
443 # work. Perhaps we need to match .* followed by eof, in which
444 # gdb_expect does not seem to have a way to do that.
445 perror "couldn't load $arg into $GDB (end of file)."
452 # connect to the target and download a file, if necessary.
453 # return a -1 if anything goes wrong.
455 proc mi_gdb_target_load { } {
461 upvar timeout timeout
463 if { [info procs gdbserver_gdb_load] != "" } {
464 mi_gdb_test "kill" ".*" ""
465 set res [gdbserver_gdb_load]
466 set protocol [lindex $res 0]
467 set gdbport [lindex $res 1]
469 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
472 } elseif { [info procs send_target_sid] != "" } {
473 # For SID, things get complex
476 -re ".*$mi_gdb_prompt$"
480 -re "\\^done.*$mi_gdb_prompt$" {
483 perror "Unable to connect to SID target"
487 send_gdb "48-target-download\n"
489 -re "48\\^done.*$mi_gdb_prompt$" {
492 perror "Unable to download to SID target"
496 } elseif { [target_info protocol] == "sim" } {
497 # For the simulator, just connect to it directly.
498 send_gdb "47-target-select sim\n"
500 -re "47\\^connected.*$mi_gdb_prompt$" {
503 perror "Unable to select sim target"
507 send_gdb "48-target-download\n"
509 -re "48\\^done.*$mi_gdb_prompt$" {
512 perror "Unable to download to sim target"
516 } elseif { [target_info gdb_protocol] == "remote" } {
518 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
519 perror "Unable to connect to remote target"
522 send_gdb "48-target-download\n"
524 -re "48\\^done.*$mi_gdb_prompt$" {
527 perror "Unable to download to remote target"
536 # load a file into the debugger.
537 # return a -1 if anything goes wrong.
539 proc mi_gdb_load { arg } {
541 return [mi_gdb_file_cmd $arg]
546 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
549 # COMMAND is the command to execute, send to GDB with send_gdb. If
550 # this is the null string no command is sent.
551 # PATTERN is the pattern to match for a PASS, and must NOT include
552 # the \r\n sequence immediately before the gdb prompt.
553 # MESSAGE is the message to be printed. (If this is the empty string,
554 # then sometimes we don't call pass or fail at all; I don't
555 # understand this at all.)
556 # IPATTERN is the pattern to match for the inferior's output. This parameter
557 # is optional. If present, it will produce a PASS if the match is
558 # successful, and a FAIL if unsuccessful.
561 # 1 if the test failed,
562 # 0 if the test passes,
563 # -1 if there was an internal error.
565 proc mi_gdb_test { args } {
568 global GDB expect_out
569 upvar timeout timeout
571 set command [lindex $args 0]
572 set pattern [lindex $args 1]
573 set message [lindex $args 2]
575 if [llength $args]==4 {
576 set ipattern [lindex $args 3]
579 if [llength $args]==5 {
580 set question_string [lindex $args 3];
581 set response_string [lindex $args 4];
583 set question_string "^FOOBAR$"
587 send_user "Sending \"$command\" to gdb\n"
588 send_user "Looking to match \"$pattern\"\n"
589 send_user "Message is \"$message\"\n"
593 set string "${command}\n";
594 set string_regex [string_to_regexp $command]
596 if { $command != "" } {
597 while { "$string" != "" } {
598 set foo [string first "\n" "$string"];
599 set len [string length "$string"];
600 if { $foo < [expr $len - 1] } {
601 set str [string range "$string" 0 $foo];
602 if { [send_gdb "$str"] != "" } {
603 global suppress_flag;
605 if { ! $suppress_flag } {
606 perror "Couldn't send $command to GDB.";
615 set string [string range "$string" [expr $foo + 1] end];
620 if { "$string" != "" } {
621 if { [send_gdb "$string"] != "" } {
622 global suppress_flag;
624 if { ! $suppress_flag } {
625 perror "Couldn't send $command to GDB.";
633 if [info exists timeout] {
637 if [info exists timeout] {
643 verbose -log "Expecting: ^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)"
645 -re "\\*\\*\\* DOSEXIT code.*" {
646 if { $message != "" } {
649 gdb_suppress_entire_file "GDB died";
652 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
653 if ![isnative] then {
654 warning "Can`t communicate to remote target."
660 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
661 # At this point, $expect_out(1,string) is the MI input command.
662 # and $expect_out(2,string) is the MI output command.
663 # If $expect_out(1,string) is "", then there was no MI input command here.
665 # NOTE, there is no trailing anchor because with GDB/MI,
666 # asynchronous responses can happen at any point, causing more
667 # data to be available. Normally an anchor is used to make
668 # sure the end of the output is matched, however, $mi_gdb_prompt
669 # is just as good of an anchor since mi_gdb_test is meant to
670 # match a single mi output command. If a second GDB/MI output
671 # response is sent, it will be in the buffer for the next
672 # time mi_gdb_test is called.
673 if ![string match "" $message] then {
678 -re "(${question_string})$" {
679 send_gdb "$response_string\n";
682 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
683 perror "Undefined command \"$command\"."
687 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
688 perror "\"$command\" is not a unique command name."
692 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
693 if ![string match "" $message] then {
694 set errmsg "$message (the program exited)"
696 set errmsg "$command (the program exited)"
701 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
702 if ![string match "" $message] then {
703 set errmsg "$message (the program is no longer running)"
705 set errmsg "$command (the program is no longer running)"
710 -re ".*$mi_gdb_prompt\[ \]*$" {
711 if ![string match "" $message] then {
718 perror "Window too small."
721 -re "\\(y or n\\) " {
723 perror "Got interactive prompt."
727 perror "Process no longer exists"
728 if { $message != "" } {
734 perror "internal buffer is full."
738 if ![string match "" $message] then {
739 fail "$message (timeout)"
745 # If the GDB output matched, compare the inferior output.
746 if { $result == 0 } {
747 if [ info exists ipattern ] {
748 if { ![target_info exists gdb,noinferiorio] } {
749 global mi_inferior_spawn_id
751 -i $mi_inferior_spawn_id -re "$ipattern" {
752 pass "$message inferior output"
755 fail "$message inferior output (timeout)"
760 unsupported "$message inferior output"
769 # MI run command. (A modified version of gdb_run_cmd)
772 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
773 # ``.*$'' could swallow up output that we attempt to match elsewhere.
775 proc mi_run_cmd {args} {
777 if { $suppress_flag } {
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\"\r\n)*${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 "maint show linux-async\n"
921 -re ".*Controlling the GNU/Linux 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 .* regexp for line, and then check the line programmatically.
950 proc mi_expect_stop { reason func args file line extra test } {
955 global fullname_syntax
960 if { [llength $extra] == 2 } {
961 set after_stopped [lindex $extra 0]
962 set after_reason [lindex $extra 1]
963 set after_reason "${after_reason},"
964 } elseif { [llength $extra] == 1 } {
965 set after_stopped [lindex $extra 0]
971 set prompt_re "$mi_gdb_prompt"
974 if { $reason == "really-no-reason" } {
976 -re "\\*stopped\r\n$prompt_re$" {
980 fail "$test (unknown output after running)"
986 if { $reason == "exited-normally" } {
989 -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re$" {
992 -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
994 fail "$test (unknown output after running)"
1000 set args "\\\[$args\\\]"
1003 if { $reason == "breakpoint-hit" } {
1004 set bn {bkptno="[0-9]+",}
1008 if { $reason != "" } {
1009 set r "reason=\"$reason\","
1015 verbose -log "mi_expect_stop: expecting: \\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=.*,frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\".*$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\"\}$after_stopped\r\n$prompt_re$"
1017 -re "\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=.*,frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\".*$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\"\}$after_stopped\r\n$prompt_re$" {
1019 return $expect_out(2,string)
1021 -re "\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=.*,frame=\{addr=\"$hex\",func=\".*\",args=\[\\\[\{\].*\[\\\]\}\],file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}.*\r\n$prompt_re$" {
1022 fail "$test (stopped at wrong place)"
1025 -re ".*\r\n$mi_gdb_prompt$" {
1026 fail "$test (unknown output after running)"
1030 fail "$test (timeout)"
1036 # cmd should not include the number or newline (i.e. "exec-step 3", not
1037 # "220-exec-step 3\n"
1039 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1040 # after the first prompt is printed.
1042 proc mi_execute_to { cmd reason func args file line extra test } {
1043 global suppress_flag
1044 if { $suppress_flag } {
1048 mi_send_resuming_command "$cmd" "$test"
1049 set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1053 proc mi_next_to { func args file line test } {
1054 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1055 "$file" "$line" "" "$test"
1058 proc mi_step_to { func args file line test } {
1059 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1060 "$file" "$line" "" "$test"
1063 proc mi_finish_to { func args file line result ret test } {
1064 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
1066 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1070 proc mi_continue_to {func} {
1071 mi_runto_helper $func "continue"
1074 proc mi0_execute_to { cmd reason func args file line extra test } {
1075 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
1076 "$file" "$line" "$extra" "$test"
1079 proc mi0_next_to { func args file line test } {
1080 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1081 "$file" "$line" "" "$test"
1084 proc mi0_step_to { func args file line test } {
1085 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1086 "$file" "$line" "" "$test"
1089 proc mi0_finish_to { func args file line result ret test } {
1090 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1092 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1096 proc mi0_continue_to { bkptno func args file line test } {
1097 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1098 "$func" "$args" "$file" "$line" "" "$test"
1101 # Creates a breakpoint and checks the reported fields are as expected
1102 proc mi_create_breakpoint { location number disp func file line address test } {
1103 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=\".*\"\}"
1104 mi_gdb_test "222-break-insert $location" \
1105 "222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}" \
1109 proc mi_list_breakpoints { expected test } {
1115 foreach item $children {
1119 set number disp func file line address
1120 set number [lindex $item 0]
1121 set disp [lindex $item 1]
1122 set func [lindex $item 2]
1123 set line [lindex $item 3]
1124 set address [lindex $item 4]
1125 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=\".*\"\}"
1129 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\\\]\}" \
1130 mi_gdb_test "666-break-list" \
1131 "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\\\]\}" \
1135 # Creates varobj named NAME for EXPRESSION.
1136 # Name cannot be "-".
1137 proc mi_create_varobj { name expression testname } {
1138 mi_gdb_test "-var-create $name * $expression" \
1139 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1143 proc mi_create_floating_varobj { name expression testname } {
1144 mi_gdb_test "-var-create $name @ $expression" \
1145 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1150 # Same as mi_create_varobj, but also checks the reported type
1152 proc mi_create_varobj_checked { name expression type testname } {
1153 mi_gdb_test "-var-create $name * $expression" \
1154 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1158 # Deletes the specified NAME.
1159 proc mi_delete_varobj { name testname } {
1160 mi_gdb_test "-var-delete $name" \
1161 "\\^done,ndeleted=.*" \
1165 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1166 # are reported as updated, and no other varobj is updated.
1167 # Assumes that no varobj is out of scope and that no varobj changes
1169 proc mi_varobj_update { name expected testname } {
1170 set er "\\^done,changelist=\\\["
1172 foreach item $expected {
1173 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1183 verbose -log "Expecting: $er" 2
1184 mi_gdb_test "-var-update $name" $er $testname
1187 proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1188 set v "{name=\"$name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\"}"
1189 set er "\\^done,changelist=\\\[$v\\\]"
1190 verbose -log "Expecting: $er"
1191 mi_gdb_test "-var-update $name" $er $testname
1194 proc mi_check_varobj_value { name value testname } {
1196 mi_gdb_test "-var-evaluate-expression $name" \
1197 "\\^done,value=\"$value\"" \
1201 # Check the results of the:
1203 # -var-list-children VARNAME
1205 # command. The CHILDREN parement should be a list of lists.
1206 # Each inner list can have either 3 or 4 elements, describing
1207 # fields that gdb is expected to report for child variable object,
1208 # in the following order
1212 # - Number of children
1215 # If inner list has 3 elements, the gdb is expected to output no
1216 # type for a child and no value.
1218 # If the inner list has 4 elements, gdb output is expected to
1221 proc mi_list_varobj_children { varname children testname } {
1224 if {[llength $varname] == 2} {
1225 set options [lindex $varname 1]
1226 set varname [lindex $varname 0]
1229 set numchildren [llength $children]
1231 set whatever "\"\[^\"\]+\""
1233 foreach item $children {
1235 set name [lindex $item 0]
1236 set exp [lindex $item 1]
1237 set numchild [lindex $item 2]
1238 if {[llength $item] == 5} {
1239 set type [lindex $item 3]
1240 set value [lindex $item 4]
1242 lappend children_exp\
1243 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
1244 } elseif {[llength $item] == 4} {
1245 set type [lindex $item 3]
1247 lappend children_exp\
1248 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
1250 lappend children_exp\
1251 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
1254 set children_exp_j [join $children_exp ","]
1256 set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1258 set expected "\\^done,numchild=\"0\""
1261 verbose -log "Expecting: $expected"
1263 mi_gdb_test "-var-list-children $options $varname" $expected $testname
1266 # Verifies that variable object VARNAME has NUMBER children,
1267 # where each one is named $VARNAME.<index-of-child> and has type TYPE.
1268 proc mi_list_array_varobj_children { varname number type testname } {
1270 for {set i 0} {$i < $number} {incr i} {
1271 lappend t [list $varname.$i $i 0 $type]
1273 mi_list_varobj_children $varname $t $testname
1276 # A list of two-element lists. First element of each list is
1277 # a Tcl statement, and the second element is the line
1278 # number of source C file where the statement originates.
1279 set mi_autotest_data ""
1280 # The name of the source file for autotesting.
1281 set mi_autotest_source ""
1283 proc count_newlines { string } {
1284 return [regexp -all "\n" $string]
1287 # Prepares for running inline tests in FILENAME.
1288 # See comments for mi_run_inline_test for detailed
1289 # explanation of the idea and syntax.
1290 proc mi_prepare_inline_tests { filename } {
1294 global mi_autotest_source
1295 global mi_autotest_data
1297 set mi_autotest_data {}
1299 set mi_autotest_source $filename
1301 if { ! [regexp "^/" "$filename"] } then {
1302 set filename "$srcdir/$subdir/$filename"
1305 set chan [open $filename]
1306 set content [read $chan]
1309 set start [string first "/*:" $content]
1311 set end [string first ":*/" $content]
1313 error "Unterminated special comment in $filename"
1316 set prefix [string range $content 0 $start]
1317 set prefix_newlines [count_newlines $prefix]
1319 set line_number [expr $line_number+$prefix_newlines]
1320 set comment_line $line_number
1322 set comment [string range $content [expr $start+3] [expr $end-1]]
1324 set comment_newlines [count_newlines $comment]
1325 set line_number [expr $line_number+$comment_newlines]
1327 set comment [string trim $comment]
1328 set content [string range $content [expr $end+3] \
1329 [string length $content]]
1330 lappend mi_autotest_data [list $comment $comment_line]
1338 # Helper to mi_run_inline_test below.
1339 # Return the list of all (statement,line_number) lists
1340 # that comprise TESTCASE. The begin and end markers
1342 proc mi_get_inline_test {testcase} {
1344 global mi_gdb_prompt
1345 global mi_autotest_data
1346 global mi_autotest_source
1352 foreach l $mi_autotest_data {
1354 set comment [lindex $l 0]
1356 if {$comment == "BEGIN: $testcase"} {
1358 } elseif {$comment == "END: $testcase"} {
1361 } elseif {$seen_begin==1} {
1366 if {$seen_begin == 0} {
1367 error "Autotest $testcase not found"
1370 if {$seen_begin == 1 && $seen_end == 0} {
1371 error "Missing end marker for test $testcase"
1377 # Sets temporary breakpoint at LOCATION.
1378 proc mi_tbreak {location} {
1380 global mi_gdb_prompt
1382 mi_gdb_test "-break-insert -t $location" \
1384 "run to $location (set breakpoint)"
1387 # Send COMMAND that must be a command that resumes
1388 # the inferiour (run/continue/next/etc) and consumes
1389 # the "^running" output from it.
1390 proc mi_send_resuming_command_raw {command test} {
1392 global mi_gdb_prompt
1394 send_gdb "$command\n"
1396 -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n${mi_gdb_prompt}" {
1397 # Note that lack of 'pass' call here -- this works around limitation
1398 # in DejaGNU xfail mechanism. mi-until.exp has this:
1400 # setup_kfail gdb/2104 "*-*-*"
1403 # and mi_execute_to uses mi_send_resuming_command. If we use 'pass' here,
1404 # it will reset kfail, so when the actual test fails, it will be flagged
1407 -re ".*${mi_gdb_prompt}" {
1408 fail "$test (failed to resume)"
1410 -re "\\^error,msg=.*" {
1411 fail "$test (MI error)"
1421 proc mi_send_resuming_command {command test} {
1422 mi_send_resuming_command_raw -$command $test
1425 # Helper to mi_run_inline_test below.
1426 # Sets a temporary breakpoint at LOCATION and runs
1427 # the program using COMMAND. When the program is stopped
1428 # returns the line at which it. Returns -1 if line cannot
1430 # Does not check that the line is the same as requested.
1431 # The caller can check itself if required.
1432 proc mi_continue_to_line {location test} {
1435 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1436 return [mi_get_stop_line $test]
1439 # Wait until gdb prints the current line.
1440 proc mi_get_stop_line {test} {
1442 global mi_gdb_prompt
1448 set prompt_re "$mi_gdb_prompt"
1452 -re ".*line=\"(.*)\".*\r\n$prompt_re$" {
1453 return $expect_out(1,string)
1455 -re ".*$mi_gdb_prompt$" {
1456 fail "wait for stop ($test)"
1459 fail "wait for stop ($test)"
1464 # Run a MI test embedded in comments in a C file.
1465 # The C file should contain special comments in the following
1468 # /*: BEGIN: testname :*/
1469 # /*: <Tcl statements> :*/
1470 # /*: END: testname :*/
1472 # This procedure find the begin and end marker for the requested
1473 # test. Then, a temporary breakpoint is set at the begin
1474 # marker and the program is run (from start).
1476 # After that, for each special comment between the begin and end
1477 # marker, the Tcl statements are executed. It is assumed that
1478 # for each comment, the immediately preceding line is executable
1479 # C statement. Then, gdb will be single-stepped until that
1480 # preceding C statement is executed, and after that the
1481 # Tcl statements in the comment will be executed.
1485 # /*: BEGIN: assignment-test :*/
1487 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1488 # /*: END: assignment-test :*/
1490 # The mi_prepare_inline_tests function should be called before
1491 # calling this function. A given C file can contain several
1492 # inline tests. The names of the tests must be unique within one
1495 proc mi_run_inline_test { testcase } {
1497 global mi_gdb_prompt
1500 global fullname_syntax
1501 global mi_autotest_source
1503 set commands [mi_get_inline_test $testcase]
1508 foreach c $commands {
1509 set statements [lindex $c 0]
1510 set line [lindex $c 1]
1511 set line [expr $line-1]
1513 # We want gdb to be stopped at the expression immediately
1514 # before the comment. If this is the first comment, the
1515 # program is either not started yet or is in some random place,
1516 # so we run it. For further comments, we might be already
1517 # standing at the right line. If not continue till the
1521 # Start the program afresh.
1522 mi_tbreak "$mi_autotest_source:$line"
1524 set line_now [mi_get_stop_line "$testcase: step to $line"]
1526 } elseif {$line_now!=$line} {
1527 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1530 if {$line_now!=$line} {
1531 fail "$testcase: go to line $line"
1534 # We're not at the statement right above the comment.
1535 # Execute that statement so that the comment can test
1536 # the state after the statement is executed.
1538 # Single-step past the line.
1539 mi_send_resuming_command "exec-next" "$testcase: step over $line"
1540 set line_now [mi_get_stop_line "$testcase: step over $line"]
1542 # We probably want to use 'uplevel' so that statements
1543 # have direct access to global variables that the
1544 # main 'exp' file has set up. But it's not yet clear,
1545 # will need more experience to be sure.
1550 proc get_mi_thread_list {name} {
1553 # MI will return a list of thread ids:
1556 # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
1558 mi_gdb_test "-thread-list-ids" \
1559 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},number-of-threads="[0-9]+"} \
1560 "-thread_list_ids ($name)"
1563 if {[info exists expect_out(buffer)]} {
1564 set output $expect_out(buffer)
1568 if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
1569 fail "finding threads in MI output ($name)"
1571 pass "finding threads in MI output ($name)"
1573 # Make list of console threads
1574 set start [expr {[string first \{ $threads] + 1}]
1575 set end [expr {[string first \} $threads] - 1}]
1576 set threads [string range $threads $start $end]
1577 foreach thread [split $threads ,] {
1578 if {[scan $thread {thread-id="%d"} num]} {
1579 lappend thread_list $num
1587 # Check that MI and the console know of the same threads.
1588 # Appends NAME to all test names.
1589 proc check_mi_and_console_threads {name} {
1592 mi_gdb_test "-thread-list-ids" \
1593 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},number-of-threads="[0-9]+"} \
1594 "-thread-list-ids ($name)"
1596 if {[info exists expect_out(buffer)]} {
1597 set mi_output $expect_out(buffer)
1600 # GDB will return a list of thread ids and some more info:
1603 # -interpreter-exec console "info threads"
1604 # ~" 4 Thread 2051 (LWP 7734) 0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
1605 # ~" 3 Thread 1026 (LWP 7733) () at __libc_nanosleep:-1"
1606 # ~" 2 Thread 2049 (LWP 7732) 0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
1607 # ~"* 1 Thread 1024 (LWP 7731) main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
1608 # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
1609 mi_gdb_test "info threads" \
1610 {.*(~".*"[\r\n]*)+.*} \
1611 "info threads ($name)"
1612 set console_output {}
1613 if {[info exists expect_out(buffer)]} {
1614 set console_output $expect_out(buffer)
1617 # Make a list of all known threads to console (gdb's thread IDs)
1618 set console_thread_list {}
1619 foreach line [split $console_output \n] {
1620 if {[string index $line 0] == "~"} {
1621 # This is a line from the console; trim off "~", " ", "*", and "\""
1622 set line [string trim $line ~\ \"\*]
1623 if {[scan $line "%d" id] == 1} {
1624 lappend console_thread_list $id
1629 # Now find the result string from MI
1631 foreach line [split $mi_output \n] {
1632 if {[string range $line 0 4] == "^done"} {
1636 if {$mi_result == ""} {
1637 fail "finding MI result string ($name)"
1639 pass "finding MI result string ($name)"
1642 # Finally, extract the thread ids and compare them to the console
1643 set num_mi_threads_str ""
1644 if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
1645 fail "finding number of threads in MI output ($name)"
1647 pass "finding number of threads in MI output ($name)"
1649 # Extract the number of threads from the MI result
1650 if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
1651 fail "got number of threads from MI ($name)"
1653 pass "got number of threads from MI ($name)"
1655 # Check if MI and console have same number of threads
1656 if {$num_mi_threads != [llength $console_thread_list]} {
1657 fail "console and MI have same number of threads ($name)"
1659 pass "console and MI have same number of threads ($name)"
1661 # Get MI thread list
1662 set mi_thread_list [get_mi_thread_list $name]
1664 # Check if MI and console have the same threads
1666 foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
1672 fail "MI and console have same threads ($name)"
1674 # Send a list of failures to the log
1675 send_log "Console has thread ids: $console_thread_list\n"
1676 send_log "MI has thread ids: $mi_thread_list\n"
1678 pass "MI and console have same threads ($name)"
1685 proc mi_load_shlibs { args } {
1686 if {![is_remote target]} {
1690 foreach file $args {
1694 # Even if the target supplies full paths for shared libraries,
1695 # they may not be paths for this system.
1696 mi_gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "\^done" ""