1 # Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2007
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 2 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, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-gdb@prep.ai.mit.edu
21 # This file was based on a file written by Fred Fish. (fnf@cygnus.com)
23 # Test setup routines that work with the MI interpreter.
25 # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
26 # Set it if it is not already set.
28 if ![info exists mi_gdb_prompt] then {
29 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
32 global mi_inferior_spawn_id
33 global mi_inferior_tty_name
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 {} {
53 gdb_stop_suppressing_tests;
55 if { [info procs sid_exit] != "" } {
59 if ![info exists gdb_spawn_id] {
63 verbose "Quitting $GDB $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 # 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 mi_gdb_start { args } {
108 gdb_stop_suppressing_tests;
109 set inferior_pty no-tty
111 if { [llength $args] == 1} {
112 set inferior_pty [lindex $args 0]
115 set separate_inferior_pty [string match $inferior_pty separate-inferior-tty]
118 if { [info procs sid_start] != "" } {
119 verbose "Spawning SID"
123 verbose "Spawning $GDB -nw $GDBFLAGS $MIFLAGS"
125 if [info exists gdb_spawn_id] {
129 if ![is_remote host] {
130 if { [which $GDB] == 0 } then {
131 perror "$GDB does not exist."
136 # Create the new PTY for the inferior process.
137 if { $separate_inferior_pty } {
139 global mi_inferior_spawn_id
140 global mi_inferior_tty_name
141 set mi_inferior_spawn_id $spawn_id
142 set mi_inferior_tty_name $spawn_out(slave,name)
145 set res [remote_spawn host "$GDB -nw $GDBFLAGS $MIFLAGS [host_info gdb_opts]"];
146 if { $res < 0 || $res == "" } {
147 perror "Spawning $GDB failed."
151 -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
152 # We have a new format mi startup prompt. If we are
153 # running mi1, then this is an error as we should be
154 # using the old-style prompt.
155 if { $MIFLAGS == "-i=mi1" } {
156 perror "(mi startup) Got unexpected new mi prompt."
160 verbose "GDB initialized."
162 -re "\[^~\].*$mi_gdb_prompt$" {
163 # We have an old format mi startup prompt. If we are
164 # not running mi1, then this is an error as we should be
165 # using the new-style prompt.
166 if { $MIFLAGS != "-i=mi1" } {
167 perror "(mi startup) Got unexpected old mi prompt."
171 verbose "GDB initialized."
173 -re ".*$gdb_prompt $" {
174 untested "Skip mi tests (got non-mi prompt)."
178 -re ".*unrecognized option.*for a complete list of options." {
179 untested "Skip mi tests (not compiled with mi support)."
183 -re ".*Interpreter `mi' unrecognized." {
184 untested "Skip mi tests (not compiled with mi support)."
189 perror "(timeout) GDB never initialized after 10 seconds."
196 # FIXME: mi output does not go through pagers, so these can be removed.
197 # force the height to "unlimited", so no pagers get used
198 send_gdb "100-gdb-set height 0\n"
200 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
201 verbose "Setting height to 0." 2
204 warning "Couldn't set the height to 0"
207 # force the width to "unlimited", so no wraparound occurs
208 send_gdb "101-gdb-set width 0\n"
210 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
211 verbose "Setting width to 0." 2
214 warning "Couldn't set the width to 0."
217 # If allowing the inferior to have its own PTY then assign the inferior
218 # its own terminal device here.
219 if { $separate_inferior_pty } {
220 send_gdb "102-inferior-tty-set $mi_inferior_tty_name\n"
222 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
223 verbose "redirect inferior output to new terminal device."
226 warning "Couldn't redirect inferior output." 2
234 # Many of the tests depend on setting breakpoints at various places and
235 # running until that breakpoint is reached. At times, we want to start
236 # with a clean-slate with respect to breakpoints, so this utility proc
237 # lets us do this without duplicating this code everywhere.
240 proc mi_delete_breakpoints {} {
243 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
244 send_gdb "102-break-delete\n"
246 -re "Delete all breakpoints.*y or n.*$" {
250 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
251 # This happens if there were no breakpoints
253 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
256 # The correct output is not "No breakpoints or watchpoints." but an
257 # empty BreakpointTable. Also, a query is not acceptable with mi.
258 send_gdb "103-break-list\n"
260 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
261 -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=\\\[\\\]\}" {}
262 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
263 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
264 -re "Delete all breakpoints.*or n.*$" {
265 warning "Unexpected prompt for breakpoints deletion";
269 timeout { perror "-break-list (timeout)" ; return }
273 proc mi_gdb_reinitialize_dir { subdir } {
278 if { $suppress_flag } {
282 if [is_remote host] {
286 if { $MIFLAGS == "-i=mi1" } {
287 send_gdb "104-environment-directory\n"
289 -re ".*Reinitialize source path to empty.*y or n. " {
290 warning "Got confirmation prompt for dir reinitialization."
293 -re "$mi_gdb_prompt$" {}
294 timeout {error "Dir reinitialization failed (timeout)"}
297 -re "$mi_gdb_prompt$" {}
298 timeout {error "Dir reinitialization failed (timeout)"}
301 send_gdb "104-environment-directory -r\n"
303 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
304 -re "$mi_gdb_prompt$" {}
305 timeout {error "Dir reinitialization failed (timeout)"}
309 send_gdb "105-environment-directory $subdir\n"
311 -re "Source directories searched.*$mi_gdb_prompt$" {
312 verbose "Dir set to $subdir"
314 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
315 # FIXME: We return just the prompt for now.
316 verbose "Dir set to $subdir"
317 # perror "Dir \"$subdir\" failed."
322 # Send GDB the "target" command.
323 # FIXME: Some of these patterns are not appropriate for MI. Based on
324 # config/monitor.exp:gdb_target_command.
325 proc mi_gdb_target_cmd { targetname serialport } {
328 for {set i 1} {$i <= 3} {incr i} {
329 send_gdb "47-target-select $targetname $serialport\n"
331 -re "47\\^connected.*$mi_gdb_prompt$" {
332 verbose "Set target to $targetname";
335 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
336 verbose "Connection failed";
338 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
339 verbose "Set target to $targetname";
342 -re "Remote debugging using .*$serialport.*$mi_gdb_prompt$" {
343 verbose "Set target to $targetname";
346 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
347 verbose "Set target to $targetname";
350 -re "Connected to.*$mi_gdb_prompt$" {
351 verbose "Set target to $targetname";
354 -re "Ending remote.*$mi_gdb_prompt$" { }
355 -re "Connection refused.*$mi_gdb_prompt$" {
356 verbose "Connection refused by remote target. Pausing, and trying again."
360 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
361 verbose "Got timeout error from gdb.";
373 # load a file into the debugger (file command only).
374 # return a -1 if anything goes wrong.
376 proc mi_gdb_file_cmd { arg } {
382 global last_loaded_file
383 upvar timeout timeout
385 set last_loaded_file $arg
387 if [is_remote host] {
388 set arg [remote_download host $arg];
390 error "download failed"
395 # FIXME: Several of these patterns are only acceptable for console
396 # output. Queries are an error for mi.
397 send_gdb "105-file-exec-and-symbols $arg\n"
399 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
400 verbose "\t\tLoaded $arg into the $GDB"
403 -re "has no symbol-table.*$mi_gdb_prompt$" {
404 perror "$arg wasn't compiled with \"-g\""
407 -re "Load new symbol table from \".*\".*y or n. $" {
410 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
411 verbose "\t\tLoaded $arg with new symbol table into $GDB"
415 perror "(timeout) Couldn't load $arg, other program already loaded."
420 -re "No such file or directory.*$mi_gdb_prompt$" {
421 perror "($arg) No such file or directory\n"
424 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
425 # We (MI) are just giving the prompt back for now, instead of giving
426 # some acknowledgement.
430 perror "couldn't load $arg into $GDB (timed out)."
434 # This is an attempt to detect a core dump, but seems not to
435 # work. Perhaps we need to match .* followed by eof, in which
436 # gdb_expect does not seem to have a way to do that.
437 perror "couldn't load $arg into $GDB (end of file)."
444 # connect to the target and download a file, if necessary.
445 # return a -1 if anything goes wrong.
447 proc mi_gdb_target_load { } {
453 upvar timeout timeout
455 if { [info procs gdbserver_gdb_load] != "" } {
456 set res [gdbserver_gdb_load]
457 set protocol [lindex $res 0]
458 set gdbport [lindex $res 1]
460 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
463 } elseif { [info procs send_target_sid] != "" } {
464 # For SID, things get complex
467 -re "\\^done.*$mi_gdb_prompt$" {
470 perror "Unable to connect to SID target"
474 send_gdb "48-target-download\n"
476 -re "48\\^done.*$mi_gdb_prompt$" {
479 perror "Unable to download to SID target"
483 } elseif { [target_info protocol] == "sim" } {
484 # For the simulator, just connect to it directly.
485 send_gdb "47-target-select sim\n"
487 -re "47\\^connected.*$mi_gdb_prompt$" {
490 perror "Unable to select sim target"
494 send_gdb "48-target-download\n"
496 -re "48\\^done.*$mi_gdb_prompt$" {
499 perror "Unable to download to sim target"
503 } elseif { [target_info gdb_protocol] == "remote" } {
505 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
506 perror "Unable to connect to remote target"
509 send_gdb "48-target-download\n"
511 -re "48\\^done.*$mi_gdb_prompt$" {
514 perror "Unable to download to remote target"
523 # load a file into the debugger.
524 # return a -1 if anything goes wrong.
526 proc mi_gdb_load { arg } {
528 return [mi_gdb_file_cmd $arg]
533 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
536 # COMMAND is the command to execute, send to GDB with send_gdb. If
537 # this is the null string no command is sent.
538 # PATTERN is the pattern to match for a PASS, and must NOT include
539 # the \r\n sequence immediately before the gdb prompt.
540 # MESSAGE is the message to be printed. (If this is the empty string,
541 # then sometimes we don't call pass or fail at all; I don't
542 # understand this at all.)
543 # IPATTERN is the pattern to match for the inferior's output. This parameter
544 # is optional. If present, it will produce a PASS if the match is
545 # successful, and a FAIL if unsuccessful.
548 # 1 if the test failed,
549 # 0 if the test passes,
550 # -1 if there was an internal error.
552 proc mi_gdb_test { args } {
555 global GDB expect_out
556 upvar timeout timeout
558 set command [lindex $args 0]
559 set pattern [lindex $args 1]
560 set message [lindex $args 2]
562 if [llength $args]==4 {
563 set ipattern [lindex $args 3]
566 if [llength $args]==5 {
567 set question_string [lindex $args 3];
568 set response_string [lindex $args 4];
570 set question_string "^FOOBAR$"
574 send_user "Sending \"$command\" to gdb\n"
575 send_user "Looking to match \"$pattern\"\n"
576 send_user "Message is \"$message\"\n"
580 set string "${command}\n";
581 set string_regex [string_to_regexp $command]
583 if { $command != "" } {
584 while { "$string" != "" } {
585 set foo [string first "\n" "$string"];
586 set len [string length "$string"];
587 if { $foo < [expr $len - 1] } {
588 set str [string range "$string" 0 $foo];
589 if { [send_gdb "$str"] != "" } {
590 global suppress_flag;
592 if { ! $suppress_flag } {
593 perror "Couldn't send $command to GDB.";
602 set string [string range "$string" [expr $foo + 1] end];
607 if { "$string" != "" } {
608 if { [send_gdb "$string"] != "" } {
609 global suppress_flag;
611 if { ! $suppress_flag } {
612 perror "Couldn't send $command to GDB.";
620 if [info exists timeout] {
624 if [info exists timeout] {
631 -re "\\*\\*\\* DOSEXIT code.*" {
632 if { $message != "" } {
635 gdb_suppress_entire_file "GDB died";
638 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
639 if ![isnative] then {
640 warning "Can`t communicate to remote target."
646 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
647 # At this point, $expect_out(1,string) is the MI input command.
648 # and $expect_out(2,string) is the MI output command.
649 # If $expect_out(1,string) is "", then there was no MI input command here.
651 # NOTE, there is no trailing anchor because with GDB/MI,
652 # asynchronous responses can happen at any point, causing more
653 # data to be available. Normally an anchor is used to make
654 # sure the end of the output is matched, however, $mi_gdb_prompt
655 # is just as good of an anchor since mi_gdb_test is meant to
656 # match a single mi output command. If a second GDB/MI output
657 # response is sent, it will be in the buffer for the next
658 # time mi_gdb_test is called.
659 if ![string match "" $message] then {
664 -re "(${question_string})$" {
665 send_gdb "$response_string\n";
668 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
669 perror "Undefined command \"$command\"."
673 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
674 perror "\"$command\" is not a unique command name."
678 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
679 if ![string match "" $message] then {
680 set errmsg "$message (the program exited)"
682 set errmsg "$command (the program exited)"
687 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
688 if ![string match "" $message] then {
689 set errmsg "$message (the program is no longer running)"
691 set errmsg "$command (the program is no longer running)"
696 -re ".*$mi_gdb_prompt\[ \]*$" {
697 if ![string match "" $message] then {
704 perror "Window too small."
707 -re "\\(y or n\\) " {
709 perror "Got interactive prompt."
713 perror "Process no longer exists"
714 if { $message != "" } {
720 perror "internal buffer is full."
724 if ![string match "" $message] then {
725 fail "$message (timeout)"
731 # If the GDB output matched, compare the inferior output.
732 if { $result == 0 } {
733 if [ info exists ipattern ] {
734 global mi_inferior_spawn_id
736 -i $mi_inferior_spawn_id -re "$ipattern" {
737 pass "inferior_output:$message"
740 fail "inferior output timeout"
751 # MI run command. (A modified version of gdb_run_cmd)
754 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
755 # ``.*$'' could swallow up output that we attempt to match elsewhere.
757 proc mi_run_cmd {args} {
759 if { $suppress_flag } {
764 if [target_info exists gdb_init_command] {
765 send_gdb "[target_info gdb_init_command]\n";
767 -re "$mi_gdb_prompt$" { }
769 perror "gdb_init_command for target failed";
775 if { [mi_gdb_target_load] < 0 } {
779 if [target_info exists use_gdb_stub] {
780 if [target_info exists gdb,do_reload_on_run] {
781 send_gdb "000-exec-continue\n";
783 -re "000\\^running\[\r\n\]+$mi_gdb_prompt$" {}
789 if [target_info exists gdb,start_symbol] {
790 set start [target_info gdb,start_symbol];
795 # HACK: Should either use 000-jump or fix the target code
796 # to better handle RUN.
797 send_gdb "jump *$start\n"
798 warning "Using CLI jump command, expect run-to-main FAIL"
802 send_gdb "000-exec-run $args\n"
804 -re "000\\^running\r\n${mi_gdb_prompt}" {
807 perror "Unable to start target"
811 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
815 # Just like run-to-main but works with the MI interface
818 proc mi_run_to_main { } {
820 if { $suppress_flag } {
829 mi_delete_breakpoints
830 mi_gdb_reinitialize_dir $srcdir/$subdir
831 mi_gdb_load ${binfile}
837 # Just like gdb's "runto" proc, it will run the target to a given
838 # function. The big difference here between mi_runto and mi_execute_to
839 # is that mi_execute_to must have the inferior running already. This
840 # proc will (like gdb's runto) (re)start the inferior, too.
842 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
844 # -1 if test suppressed, failed, timedout
847 proc mi_runto_helper {func run_or_continue} {
849 if { $suppress_flag } {
853 global mi_gdb_prompt expect_out
854 global hex decimal fullname_syntax
856 set test "mi runto $func"
857 mi_gdb_test "200-break-insert -t $func" \
858 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
859 "breakpoint at $func"
861 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
862 || ![scan $str {number="%d"} bkptno]} {
866 if {$run_or_continue == "run"} {
869 send_gdb "000-exec-continue\n"
871 -re "000\\^running\r\n${mi_gdb_prompt}" {
881 -re ".*000\\*stopped,thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=\(\\\[.*\\\]\|\{.*\}\),file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
885 -re ".*$mi_gdb_prompt$" {
889 fail "$test (timeout)"
895 proc mi_runto {func} {
896 mi_runto_helper $func "run"
899 # Next to the next statement
900 # For return values, see mi_execute_to_helper
902 proc mi_next { test } {
903 return [mi_next_to {.*} {.*} {.*} {.*} $test]
907 # Step to the next statement
908 # For return values, see mi_execute_to_helper
910 proc mi_step { test } {
911 return [mi_step_to {.*} {.*} {.*} {.*} $test]
914 # cmd should not include the number or newline (i.e. "exec-step 3", not
915 # "220-exec-step 3\n"
917 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
918 # after the first prompt is printed.
920 proc mi_execute_to_helper { cmd reason func args file line extra test } {
922 if { $suppress_flag } {
928 global fullname_syntax
929 send_gdb "220-$cmd\n"
931 -re "220\\^running\r\n${mi_gdb_prompt}.*220\\*stopped,reason=\"$reason\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\".*$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\"\}$extra\r\n$mi_gdb_prompt$" {
935 -re "220\\^running\r\n${mi_gdb_prompt}.*220\\*stopped,reason=\"$reason\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\".*\",args=\[\\\[\{\].*\[\\\]\}\],file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}.*\r\n$mi_gdb_prompt$" {
936 fail "$test (stopped at wrong place)"
939 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
940 fail "$test (unknown output after running)"
944 fail "$test (timeout)"
950 proc mi_execute_to { cmd reason func args file line extra test } {
951 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
952 "$file" "$line" "$extra" "$test"
955 proc mi_next_to { func args file line test } {
956 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
957 "$file" "$line" "" "$test"
960 proc mi_step_to { func args file line test } {
961 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
962 "$file" "$line" "" "$test"
965 proc mi_finish_to { func args file line result ret test } {
966 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
968 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
972 proc mi_continue_to {func} {
973 mi_runto_helper $func "continue"
976 proc mi0_execute_to { cmd reason func args file line extra test } {
977 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
978 "$file" "$line" "$extra" "$test"
981 proc mi0_next_to { func args file line test } {
982 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
983 "$file" "$line" "" "$test"
986 proc mi0_step_to { func args file line test } {
987 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
988 "$file" "$line" "" "$test"
991 proc mi0_finish_to { func args file line result ret test } {
992 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
994 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
998 proc mi0_continue_to { bkptno func args file line test } {
999 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1000 "$func" "$args" "$file" "$line" "" "$test"
1003 # Creates varobj named NAME for EXPRESSION.
1004 # Name cannot be "-".
1005 proc mi_create_varobj { name expression testname } {
1006 mi_gdb_test "-var-create $name * $expression" \
1007 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1011 # Deletes the specified NAME.
1012 proc mi_delete_varobj { name testname } {
1013 mi_gdb_test "-var-delete $name" \
1014 "\\^done,ndeleted=.*" \
1018 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1019 # are reported as updated, and no other varobj is updated.
1020 # Assumes that no varobj is out of scope and that no varobj changes
1022 proc mi_varobj_update { name expected testname } {
1023 set er "\\^done,changelist=\\\["
1025 foreach item $expected {
1026 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1035 verbose -log "Expecting: $er" 2
1036 mi_gdb_test "-var-update $name" $er $testname
1039 proc mi_check_varobj_value { name value testname } {
1041 mi_gdb_test "-var-evaluate-expression $name" \
1042 "\\^done,value=\"$value\"" \
1046 # Check the results of the:
1048 # -var-list-children VARNAME
1050 # command. The CHILDREN parement should be a list of lists.
1051 # Each inner list can have either 3 or 4 elements, describing
1052 # fields that gdb is expected to report for child variable object,
1053 # in the following order
1057 # - Number of children
1060 # If inner list has 3 elements, the gdb is expected to output no
1063 proc mi_list_varobj_children { varname children testname } {
1065 set numchildren [llength $children]
1067 set whatever "\"\[^\"\]+\""
1069 foreach item $children {
1071 set name [lindex $item 0]
1072 set exp [lindex $item 1]
1073 set numchild [lindex $item 2]
1074 if {[llength $item] == 4} {
1075 set type [lindex $item 3]
1077 lappend children_exp\
1078 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"}"
1080 lappend children_exp\
1081 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"}"
1084 set children_exp_j [join $children_exp ","]
1085 set expected "\\^done,numchild=\"$numchildren\",children=\\\[$children_exp_j\\\]"
1087 verbose -log "Expecting: $expected"
1089 mi_gdb_test "-var-list-children $varname" $expected $testname
1092 # A list of two-element lists. First element of each list is
1093 # a Tcl statement, and the second element is the line
1094 # number of source C file where the statement originates.
1095 set mi_autotest_data ""
1096 # The name of the source file for autotesting.
1097 set mi_autotest_source ""
1099 proc count_newlines { string } {
1100 return [regexp -all "\n" $string]
1103 # Prepares for running inline tests in FILENAME.
1104 # See comments for mi_run_inline_test for detailed
1105 # explanation of the idea and syntax.
1106 proc mi_prepare_inline_tests { filename } {
1110 global mi_autotest_source
1111 global mi_autotest_data
1113 set mi_autotest_data {}
1115 set mi_autotest_source $filename
1117 if { ! [regexp "^/" "$filename"] } then {
1118 set filename "$srcdir/$subdir/$filename"
1121 set chan [open $filename]
1122 set content [read $chan]
1125 set start [string first "/*:" $content]
1127 set end [string first ":*/" $content]
1129 error "Unterminated special comment in $filename"
1132 set prefix [string range $content 0 $start]
1133 set prefix_newlines [count_newlines $prefix]
1135 set line_number [expr $line_number+$prefix_newlines]
1136 set comment_line $line_number
1138 set comment [string range $content [expr $start+3] [expr $end-1]]
1140 set comment_newlines [count_newlines $comment]
1141 set line_number [expr $line_number+$comment_newlines]
1143 set comment [string trim $comment]
1144 set content [string range $content [expr $end+3] \
1145 [string length $content]]
1146 lappend mi_autotest_data [list $comment $comment_line]
1154 # Helper to mi_run_inline_test below.
1155 # Return the list of all (statement,line_number) lists
1156 # that comprise TESTCASE. The begin and end markers
1158 proc mi_get_inline_test {testcase} {
1160 global mi_gdb_prompt
1161 global mi_autotest_data
1162 global mi_autotest_source
1168 foreach l $mi_autotest_data {
1170 set comment [lindex $l 0]
1172 if {$comment == "BEGIN: $testcase"} {
1174 } elseif {$comment == "END: $testcase"} {
1177 } elseif {$seen_begin==1} {
1182 if {$seen_begin == 0} {
1183 error "Autotest $testcase not found"
1186 if {$seen_begin == 1 && $seen_end == 0} {
1187 error "Missing end marker for test $testcase"
1193 # Sets temporary breakpoint at LOCATION.
1194 proc mi_tbreak {location} {
1196 global mi_gdb_prompt
1198 mi_gdb_test "-break-insert -t $location" \
1200 "run to $location (set breakpoint)"
1203 # Send COMMAND that must be a command that resumes
1204 # the inferiour (run/continue/next/etc) and consumes
1205 # the "^running" output from it.
1206 proc mi_send_resuming_command {command test} {
1208 global mi_gdb_prompt
1210 send_gdb "220-$command\n"
1212 -re "220\\^running\r\n${mi_gdb_prompt}" {
1220 # Helper to mi_run_inline_test below.
1221 # Sets a temporary breakpoint at LOCATION and runs
1222 # the program using COMMAND. When the program is stopped
1223 # returns the line at which it. Returns -1 if line cannot
1225 # Does not check that the line is the same as requested.
1226 # The caller can check itself if required.
1227 proc mi_continue_to_line {location test} {
1230 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1231 return [mi_wait_for_stop $test]
1234 # Wait until gdb prints the current line.
1235 proc mi_wait_for_stop {test} {
1237 global mi_gdb_prompt
1240 -re ".*line=\"(.*)\".*\r\n$mi_gdb_prompt$" {
1241 return $expect_out(1,string)
1243 -re ".*$mi_gdb_prompt$" {
1244 fail "wait for stop ($test)"
1247 fail "wait for stop ($test)"
1252 # Run a MI test embedded in comments in a C file.
1253 # The C file should contain special comments in the following
1256 # /*: BEGIN: testname :*/
1257 # /*: <Tcl statements> :*/
1258 # /*: END: testname :*/
1260 # This procedure find the begin and end marker for the requested
1261 # test. Then, a temporary breakpoint is set at the begin
1262 # marker and the program is run (from start).
1264 # After that, for each special comment between the begin and end
1265 # marker, the Tcl statements are executed. It is assumed that
1266 # for each comment, the immediately preceding line is executable
1267 # C statement. Then, gdb will be single-stepped until that
1268 # preceding C statement is executed, and after that the
1269 # Tcl statements in the comment will be executed.
1273 # /*: BEGIN: assignment-test :*/
1275 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1276 # /*: END: assignment-test :*/
1278 # The mi_prepare_inline_tests function should be called before
1279 # calling this function. A given C file can contain several
1280 # inline tests. The names of the tests must be unique within one
1283 proc mi_run_inline_test { testcase } {
1285 global mi_gdb_prompt
1288 global fullname_syntax
1289 global mi_autotest_source
1291 set commands [mi_get_inline_test $testcase]
1296 foreach c $commands {
1297 set statements [lindex $c 0]
1298 set line [lindex $c 1]
1299 set line [expr $line-1]
1301 # We want gdb to be stopped at the expression immediately
1302 # before the comment. If this is the first comment, the
1303 # program is either not started yet or is in some random place,
1304 # so we run it. For further comments, we might be already
1305 # standing at the right line. If not continue till the
1309 # Start the program afresh.
1310 mi_tbreak "$mi_autotest_source:$line"
1312 set line_now [mi_wait_for_stop "$testcase: step to $line"]
1314 } elseif {$line_now!=$line} {
1315 set line_now [mi_continue_to_line "$mi_autotest_source:$line"]
1318 if {$line_now!=$line} {
1319 fail "$testcase: go to line $line"
1322 # We're not at the statement right above the comment.
1323 # Execute that statement so that the comment can test
1324 # the state after the statement is executed.
1326 # Single-step past the line.
1327 mi_send_resuming_command "exec-next" "$testcase: step over $line"
1328 set line_now [mi_wait_for_stop "$testcase: step over $line"]
1330 # We probably want to use 'uplevel' so that statements
1331 # have direct access to global variables that the
1332 # main 'exp' file has set up. But it's not yet clear,
1333 # will need more experience to be sure.