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_mi_gdb_file
383 global last_mi_remote_file
384 upvar timeout timeout
387 set arg $last_mi_gdb_file;
389 set last_mi_gdb_file $arg
390 if { [ info exists last_mi_remote_file ] } {
391 unset last_mi_remote_file
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 # load a file into the debugger.
453 # return a -1 if anything goes wrong.
455 proc mi_gdb_load { arg } {
461 upvar timeout timeout
469 if { [info procs gdbserver_gdb_load] != "" } {
470 global last_mi_gdb_file
471 global last_mi_remote_file
473 if { ! [info exists last_mi_remote_file] } {
474 if [is_remote target] {
475 set last_mi_remote_file [remote_download target $arg /tmp/[file tail $arg].[pid]]
477 set last_mi_remote_file $last_mi_gdb_file
481 set res [gdbserver_gdb_load $last_mi_remote_file]
482 set protocol [lindex $res 0]
483 set gdbport [lindex $res 1]
485 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
488 } elseif { [info procs send_target_sid] != "" } {
489 # For SID, things get complex
492 -re "\\^done.*$mi_gdb_prompt$" {
495 perror "Unable to connect to SID target"
499 send_gdb "48-target-download\n"
501 -re "48\\^done.*$mi_gdb_prompt$" {
504 perror "Unable to download to SID target"
508 } elseif { [target_info protocol] == "sim" } {
509 # For the simulator, just connect to it directly.
510 send_gdb "47-target-select sim\n"
512 -re "47\\^connected.*$mi_gdb_prompt$" {
515 perror "Unable to select sim target"
519 send_gdb "48-target-download\n"
521 -re "48\\^done.*$mi_gdb_prompt$" {
524 perror "Unable to download to sim target"
528 } elseif { [target_info gdb_protocol] == "remote" } {
530 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
531 perror "Unable to connect to remote target"
534 send_gdb "48-target-download\n"
536 -re "48\\^done.*$mi_gdb_prompt$" {
539 perror "Unable to download to remote target"
547 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
550 # COMMAND is the command to execute, send to GDB with send_gdb. If
551 # this is the null string no command is sent.
552 # PATTERN is the pattern to match for a PASS, and must NOT include
553 # the \r\n sequence immediately before the gdb prompt.
554 # MESSAGE is the message to be printed. (If this is the empty string,
555 # then sometimes we don't call pass or fail at all; I don't
556 # understand this at all.)
557 # IPATTERN is the pattern to match for the inferior's output. This parameter
558 # is optional. If present, it will produce a PASS if the match is
559 # successful, and a FAIL if unsuccessful.
562 # 1 if the test failed,
563 # 0 if the test passes,
564 # -1 if there was an internal error.
566 proc mi_gdb_test { args } {
569 global GDB expect_out
570 upvar timeout timeout
572 set command [lindex $args 0]
573 set pattern [lindex $args 1]
574 set message [lindex $args 2]
576 if [llength $args]==4 {
577 set ipattern [lindex $args 3]
580 if [llength $args]==5 {
581 set question_string [lindex $args 3];
582 set response_string [lindex $args 4];
584 set question_string "^FOOBAR$"
588 send_user "Sending \"$command\" to gdb\n"
589 send_user "Looking to match \"$pattern\"\n"
590 send_user "Message is \"$message\"\n"
594 set string "${command}\n";
595 set string_regex [string_to_regexp $command]
597 if { $command != "" } {
598 while { "$string" != "" } {
599 set foo [string first "\n" "$string"];
600 set len [string length "$string"];
601 if { $foo < [expr $len - 1] } {
602 set str [string range "$string" 0 $foo];
603 if { [send_gdb "$str"] != "" } {
604 global suppress_flag;
606 if { ! $suppress_flag } {
607 perror "Couldn't send $command to GDB.";
616 set string [string range "$string" [expr $foo + 1] end];
621 if { "$string" != "" } {
622 if { [send_gdb "$string"] != "" } {
623 global suppress_flag;
625 if { ! $suppress_flag } {
626 perror "Couldn't send $command to GDB.";
634 if [info exists timeout] {
638 if [info exists timeout] {
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 global mi_inferior_spawn_id
750 -i $mi_inferior_spawn_id -re "$ipattern" {
751 pass "inferior_output:$message"
754 fail "inferior output timeout"
765 # MI run command. (A modified version of gdb_run_cmd)
768 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
769 # ``.*$'' could swallow up output that we attempt to match elsewhere.
771 proc mi_run_cmd {args} {
773 if { $suppress_flag } {
778 if [target_info exists gdb_init_command] {
779 send_gdb "[target_info gdb_init_command]\n";
781 -re "$mi_gdb_prompt$" { }
783 perror "gdb_init_command for target failed";
789 if [target_info exists use_gdb_stub] {
790 if [target_info exists gdb,do_reload_on_run] {
791 # Specifying no file, defaults to the executable
792 # currently being debugged.
793 if { [mi_gdb_load ""] < 0 } {
796 send_gdb "000-exec-continue\n";
798 -re "000\\^running\[\r\n\]+$mi_gdb_prompt$" {}
804 if [target_info exists gdb,start_symbol] {
805 set start [target_info gdb,start_symbol];
810 # HACK: Should either use 000-jump or fix the target code
811 # to better handle RUN.
812 send_gdb "jump *$start\n"
813 warning "Using CLI jump command, expect run-to-main FAIL"
817 send_gdb "000-exec-run $args\n"
819 -re "000\\^running\r\n${mi_gdb_prompt}" {
822 perror "Unable to start target"
826 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
830 # Just like run-to-main but works with the MI interface
833 proc mi_run_to_main { } {
835 if { $suppress_flag } {
844 mi_delete_breakpoints
845 mi_gdb_reinitialize_dir $srcdir/$subdir
846 mi_gdb_load ${binfile}
852 # Just like gdb's "runto" proc, it will run the target to a given
853 # function. The big difference here between mi_runto and mi_execute_to
854 # is that mi_execute_to must have the inferior running already. This
855 # proc will (like gdb's runto) (re)start the inferior, too.
857 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
859 # -1 if test suppressed, failed, timedout
862 proc mi_runto_helper {func run_or_continue} {
864 if { $suppress_flag } {
868 global mi_gdb_prompt expect_out
869 global hex decimal fullname_syntax
871 set test "mi runto $func"
872 mi_gdb_test "200-break-insert -t $func" \
873 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
874 "breakpoint at $func"
876 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
877 || ![scan $str {number="%d"} bkptno]} {
881 if {$run_or_continue == "run"} {
884 send_gdb "000-exec-continue\n"
886 -re "000\\^running\r\n${mi_gdb_prompt}" {
896 -re ".*000\\*stopped,thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=\(\\\[.*\\\]\|\{.*\}\),file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
900 -re ".*$mi_gdb_prompt$" {
904 fail "$test (timeout)"
910 proc mi_runto {func} {
911 mi_runto_helper $func "run"
914 # Next to the next statement
915 # For return values, see mi_execute_to_helper
917 proc mi_next { test } {
918 return [mi_next_to {.*} {.*} {.*} {.*} $test]
922 # Step to the next statement
923 # For return values, see mi_execute_to_helper
925 proc mi_step { test } {
926 return [mi_step_to {.*} {.*} {.*} {.*} $test]
929 # cmd should not include the number or newline (i.e. "exec-step 3", not
930 # "220-exec-step 3\n"
932 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
933 # after the first prompt is printed.
935 proc mi_execute_to_helper { cmd reason func args file line extra test } {
937 if { $suppress_flag } {
943 global fullname_syntax
944 send_gdb "220-$cmd\n"
946 -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$" {
950 -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$" {
951 fail "$test (stopped at wrong place)"
954 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
955 fail "$test (unknown output after running)"
959 fail "$test (timeout)"
965 proc mi_execute_to { cmd reason func args file line extra test } {
966 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
967 "$file" "$line" "$extra" "$test"
970 proc mi_next_to { func args file line test } {
971 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
972 "$file" "$line" "" "$test"
975 proc mi_step_to { func args file line test } {
976 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
977 "$file" "$line" "" "$test"
980 proc mi_finish_to { func args file line result ret test } {
981 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
983 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
987 proc mi_continue_to {func} {
988 mi_runto_helper $func "continue"
991 proc mi0_execute_to { cmd reason func args file line extra test } {
992 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
993 "$file" "$line" "$extra" "$test"
996 proc mi0_next_to { func args file line test } {
997 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
998 "$file" "$line" "" "$test"
1001 proc mi0_step_to { func args file line test } {
1002 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1003 "$file" "$line" "" "$test"
1006 proc mi0_finish_to { func args file line result ret test } {
1007 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1009 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1013 proc mi0_continue_to { bkptno func args file line test } {
1014 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1015 "$func" "$args" "$file" "$line" "" "$test"
1018 # Creates varobj named NAME for EXPRESSION.
1019 # Name cannot be "-".
1020 proc mi_create_varobj { name expression testname } {
1021 mi_gdb_test "-var-create $name * $expression" \
1022 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",type=.*" \
1026 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1027 # are reported as updated, and no other varobj is updated.
1028 # Assumes that no varobj is out of scope and that no varobj changes
1030 proc mi_varobj_update { name expected testname } {
1031 set er "\\^done,changelist=\\\["
1033 foreach item $expected {
1034 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1043 verbose -log "Expecting: $er" 2
1044 mi_gdb_test "-var-update $name" $er $testname
1047 proc mi_check_varobj_value { name value testname } {
1049 mi_gdb_test "-var-evaluate-expression $name" \
1050 "\\^done,value=\"$value\"" \
1054 # Check the results of the:
1056 # -var-list-children VARNAME
1058 # command. The CHILDREN parement should be a list of lists.
1059 # Each inner list can have either 3 or 4 elements, describing
1060 # fields that gdb is expected to report for child variable object,
1061 # in the following order
1065 # - Number of children
1068 # If inner list has 3 elements, the gdb is expected to output no
1071 proc mi_list_varobj_children { varname children testname } {
1073 set numchildren [llength $children]
1075 set whatever "\"\[^\"\]+\""
1077 foreach item $children {
1079 set name [lindex $item 0]
1080 set exp [lindex $item 1]
1081 set numchild [lindex $item 2]
1082 if {[llength $item] == 4} {
1083 set type [lindex $item 3]
1085 lappend children_exp\
1086 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"}"
1088 lappend children_exp\
1089 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"}"
1092 set children_exp_j [join $children_exp ","]
1093 set expected "\\^done,numchild=\"$numchildren\",children=\\\[$children_exp_j\\\]"
1095 verbose -log "Expecting: $expected"
1097 mi_gdb_test "-var-list-children $varname" $expected $testname
1100 # A list of two-element lists. First element of each list is
1101 # a Tcl statement, and the second element is the line
1102 # number of source C file where the statement originates.
1103 set mi_autotest_data ""
1104 # The name of the source file for autotesting.
1105 set mi_autotest_source ""
1107 proc count_newlines { string } {
1108 return [regexp -all "\n" $string]
1111 # Prepares for running inline tests in FILENAME.
1112 # See comments for mi_run_inline_test for detailed
1113 # explanation of the idea and syntax.
1114 proc mi_prepare_inline_tests { filename } {
1118 global mi_autotest_source
1119 global mi_autotest_data
1121 set mi_autotest_data {}
1123 set mi_autotest_source $filename
1125 if { ! [regexp "^/" "$filename"] } then {
1126 set filename "$srcdir/$subdir/$filename"
1129 set chan [open $filename]
1130 set content [read $chan]
1133 set start [string first "/*:" $content]
1135 set end [string first ":*/" $content]
1137 error "Unterminated special comment in $filename"
1140 set prefix [string range $content 0 $start]
1141 set prefix_newlines [count_newlines $prefix]
1143 set line_number [expr $line_number+$prefix_newlines]
1144 set comment_line $line_number
1146 set comment [string range $content [expr $start+3] [expr $end-1]]
1148 set comment_newlines [count_newlines $comment]
1149 set line_number [expr $line_number+$comment_newlines]
1151 set comment [string trim $comment]
1152 set content [string range $content [expr $end+3] \
1153 [string length $content]]
1154 lappend mi_autotest_data [list $comment $comment_line]
1162 # Helper to mi_run_inline_test below.
1163 # Return the list of all (statement,line_number) lists
1164 # that comprise TESTCASE. The begin and end markers
1166 proc mi_get_inline_test {testcase} {
1168 global mi_gdb_prompt
1169 global mi_autotest_data
1170 global mi_autotest_source
1176 foreach l $mi_autotest_data {
1178 set comment [lindex $l 0]
1180 if {$comment == "BEGIN: $testcase"} {
1182 } elseif {$comment == "END: $testcase"} {
1185 } elseif {$seen_begin==1} {
1190 if {$seen_begin == 0} {
1191 error "Autotest $testcase not found"
1194 if {$seen_begin == 1 && $seen_end == 0} {
1195 error "Missing end marker for test $testcase"
1201 # Sets temporary breakpoint at LOCATION.
1202 proc mi_tbreak {location} {
1204 global mi_gdb_prompt
1206 mi_gdb_test "-break-insert -t $location" \
1208 "run to $location (set breakpoint)"
1211 # Send COMMAND that must be a command that resumes
1212 # the inferiour (run/continue/next/etc) and consumes
1213 # the "^running" output from it.
1214 proc mi_send_resuming_command {command test} {
1216 global mi_gdb_prompt
1218 send_gdb "220-$command\n"
1220 -re "220\\^running\r\n${mi_gdb_prompt}" {
1228 # Helper to mi_run_inline_test below.
1229 # Sets a temporary breakpoint at LOCATION and runs
1230 # the program using COMMAND. When the program is stopped
1231 # returns the line at which it. Returns -1 if line cannot
1233 # Does not check that the line is the same as requested.
1234 # The caller can check itself if required.
1235 proc mi_continue_to_line {location command} {
1238 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1239 return [mi_wait_for_stop]
1242 # Wait until gdb prints the current line.
1243 proc mi_wait_for_stop {test} {
1245 global mi_gdb_prompt
1248 -re ".*line=\"(.*)\".*\r\n$mi_gdb_prompt$" {
1249 return $expect_out(1,string)
1251 -re ".*$mi_gdb_prompt$" {
1252 fail "wait for stop ($test)"
1255 fail "wait for stop ($test)"
1260 # Run a MI test embedded in comments in a C file.
1261 # The C file should contain special comments in the following
1264 # /*: BEGIN: testname :*/
1265 # /*: <Tcl statements> :*/
1266 # /*: END: testname :*/
1268 # This procedure find the begin and end marker for the requested
1269 # test. Then, a temporary breakpoint is set at the begin
1270 # marker and the program is run (from start).
1272 # After that, for each special comment between the begin and end
1273 # marker, the Tcl statements are executed. It is assumed that
1274 # for each comment, the immediately preceding line is executable
1275 # C statement. Then, gdb will be single-stepped until that
1276 # preceding C statement is executed, and after that the
1277 # Tcl statements in the comment will be executed.
1281 # /*: BEGIN: assignment-test :*/
1283 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1284 # /*: END: assignment-test :*/
1286 # The mi_prepare_inline_tests function should be called before
1287 # calling this function. A given C file can contain several
1288 # inline tests. The names of the tests must be unique within one
1291 proc mi_run_inline_test { testcase } {
1293 global mi_gdb_prompt
1296 global fullname_syntax
1297 global mi_autotest_source
1299 set commands [mi_get_inline_test $testcase]
1304 foreach c $commands {
1305 set statements [lindex $c 0]
1306 set line [lindex $c 1]
1307 set line [expr $line-1]
1309 # We want gdb to be stopped at the expression immediately
1310 # before the comment. If this is the first comment, the
1311 # program is either not started yet or is in some random place,
1312 # so we run it. For further comments, we might be already
1313 # standing at the right line. If not continue till the
1317 # Start the program afresh.
1318 mi_tbreak "$mi_autotest_source:$line"
1320 set line_now [mi_wait_for_stop "$testcase: step to $line"]
1322 } elseif {$line_now!=$line} {
1323 set line_now [mi_continue_to_line "$mi_autotest_source:$line"]
1326 if {$line_now!=$line} {
1327 fail "$testcase: go to line $line"
1330 # We're not at the statement right above the comment.
1331 # Execute that statement so that the comment can test
1332 # the state after the statement is executed.
1334 # Single-step past the line.
1335 mi_send_resuming_command "exec-next" "$testcase: step over $line"
1336 set line_now [mi_wait_for_stop "$testcase: step over $line"]
1338 # We probably want to use 'uplevel' so that statements
1339 # have direct access to global variables that the
1340 # main 'exp' file has set up. But it's not yet clear,
1341 # will need more experience to be sure.