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 set serialport_re [string_to_regexp $serialport]
329 for {set i 1} {$i <= 3} {incr i} {
330 send_gdb "47-target-select $targetname $serialport\n"
332 -re "47\\^connected.*$mi_gdb_prompt$" {
333 verbose "Set target to $targetname";
336 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
337 verbose "Connection failed";
339 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
340 verbose "Set target to $targetname";
343 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
344 verbose "Set target to $targetname";
347 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
348 verbose "Set target to $targetname";
351 -re "Connected to.*$mi_gdb_prompt$" {
352 verbose "Set target to $targetname";
355 -re "Ending remote.*$mi_gdb_prompt$" { }
356 -re "Connection refused.*$mi_gdb_prompt$" {
357 verbose "Connection refused by remote target. Pausing, and trying again."
361 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
362 verbose "Got timeout error from gdb.";
374 # load a file into the debugger (file command only).
375 # return a -1 if anything goes wrong.
377 proc mi_gdb_file_cmd { arg } {
383 global last_loaded_file
384 upvar timeout timeout
386 set last_loaded_file $arg
388 if [is_remote host] {
389 set arg [remote_download host $arg];
391 error "download failed"
396 # FIXME: Several of these patterns are only acceptable for console
397 # output. Queries are an error for mi.
398 send_gdb "105-file-exec-and-symbols $arg\n"
400 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
401 verbose "\t\tLoaded $arg into the $GDB"
404 -re "has no symbol-table.*$mi_gdb_prompt$" {
405 perror "$arg wasn't compiled with \"-g\""
408 -re "Load new symbol table from \".*\".*y or n. $" {
411 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
412 verbose "\t\tLoaded $arg with new symbol table into $GDB"
416 perror "(timeout) Couldn't load $arg, other program already loaded."
421 -re "No such file or directory.*$mi_gdb_prompt$" {
422 perror "($arg) No such file or directory\n"
425 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
426 # We (MI) are just giving the prompt back for now, instead of giving
427 # some acknowledgement.
431 perror "couldn't load $arg into $GDB (timed out)."
435 # This is an attempt to detect a core dump, but seems not to
436 # work. Perhaps we need to match .* followed by eof, in which
437 # gdb_expect does not seem to have a way to do that.
438 perror "couldn't load $arg into $GDB (end of file)."
445 # connect to the target and download a file, if necessary.
446 # return a -1 if anything goes wrong.
448 proc mi_gdb_target_load { } {
454 upvar timeout timeout
456 if { [info procs gdbserver_gdb_load] != "" } {
457 mi_gdb_test "kill" ".*" ""
458 set res [gdbserver_gdb_load]
459 set protocol [lindex $res 0]
460 set gdbport [lindex $res 1]
462 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
465 } elseif { [info procs send_target_sid] != "" } {
466 # For SID, things get complex
469 -re "\\^done.*$mi_gdb_prompt$" {
472 perror "Unable to connect to SID target"
476 send_gdb "48-target-download\n"
478 -re "48\\^done.*$mi_gdb_prompt$" {
481 perror "Unable to download to SID target"
485 } elseif { [target_info protocol] == "sim" } {
486 # For the simulator, just connect to it directly.
487 send_gdb "47-target-select sim\n"
489 -re "47\\^connected.*$mi_gdb_prompt$" {
492 perror "Unable to select sim target"
496 send_gdb "48-target-download\n"
498 -re "48\\^done.*$mi_gdb_prompt$" {
501 perror "Unable to download to sim target"
505 } elseif { [target_info gdb_protocol] == "remote" } {
507 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
508 perror "Unable to connect to remote target"
511 send_gdb "48-target-download\n"
513 -re "48\\^done.*$mi_gdb_prompt$" {
516 perror "Unable to download to remote target"
525 # load a file into the debugger.
526 # return a -1 if anything goes wrong.
528 proc mi_gdb_load { arg } {
530 return [mi_gdb_file_cmd $arg]
535 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
538 # COMMAND is the command to execute, send to GDB with send_gdb. If
539 # this is the null string no command is sent.
540 # PATTERN is the pattern to match for a PASS, and must NOT include
541 # the \r\n sequence immediately before the gdb prompt.
542 # MESSAGE is the message to be printed. (If this is the empty string,
543 # then sometimes we don't call pass or fail at all; I don't
544 # understand this at all.)
545 # IPATTERN is the pattern to match for the inferior's output. This parameter
546 # is optional. If present, it will produce a PASS if the match is
547 # successful, and a FAIL if unsuccessful.
550 # 1 if the test failed,
551 # 0 if the test passes,
552 # -1 if there was an internal error.
554 proc mi_gdb_test { args } {
557 global GDB expect_out
558 upvar timeout timeout
560 set command [lindex $args 0]
561 set pattern [lindex $args 1]
562 set message [lindex $args 2]
564 if [llength $args]==4 {
565 set ipattern [lindex $args 3]
568 if [llength $args]==5 {
569 set question_string [lindex $args 3];
570 set response_string [lindex $args 4];
572 set question_string "^FOOBAR$"
576 send_user "Sending \"$command\" to gdb\n"
577 send_user "Looking to match \"$pattern\"\n"
578 send_user "Message is \"$message\"\n"
582 set string "${command}\n";
583 set string_regex [string_to_regexp $command]
585 if { $command != "" } {
586 while { "$string" != "" } {
587 set foo [string first "\n" "$string"];
588 set len [string length "$string"];
589 if { $foo < [expr $len - 1] } {
590 set str [string range "$string" 0 $foo];
591 if { [send_gdb "$str"] != "" } {
592 global suppress_flag;
594 if { ! $suppress_flag } {
595 perror "Couldn't send $command to GDB.";
604 set string [string range "$string" [expr $foo + 1] end];
609 if { "$string" != "" } {
610 if { [send_gdb "$string"] != "" } {
611 global suppress_flag;
613 if { ! $suppress_flag } {
614 perror "Couldn't send $command to GDB.";
622 if [info exists timeout] {
626 if [info exists timeout] {
633 -re "\\*\\*\\* DOSEXIT code.*" {
634 if { $message != "" } {
637 gdb_suppress_entire_file "GDB died";
640 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
641 if ![isnative] then {
642 warning "Can`t communicate to remote target."
648 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
649 # At this point, $expect_out(1,string) is the MI input command.
650 # and $expect_out(2,string) is the MI output command.
651 # If $expect_out(1,string) is "", then there was no MI input command here.
653 # NOTE, there is no trailing anchor because with GDB/MI,
654 # asynchronous responses can happen at any point, causing more
655 # data to be available. Normally an anchor is used to make
656 # sure the end of the output is matched, however, $mi_gdb_prompt
657 # is just as good of an anchor since mi_gdb_test is meant to
658 # match a single mi output command. If a second GDB/MI output
659 # response is sent, it will be in the buffer for the next
660 # time mi_gdb_test is called.
661 if ![string match "" $message] then {
666 -re "(${question_string})$" {
667 send_gdb "$response_string\n";
670 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
671 perror "Undefined command \"$command\"."
675 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
676 perror "\"$command\" is not a unique command name."
680 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
681 if ![string match "" $message] then {
682 set errmsg "$message (the program exited)"
684 set errmsg "$command (the program exited)"
689 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
690 if ![string match "" $message] then {
691 set errmsg "$message (the program is no longer running)"
693 set errmsg "$command (the program is no longer running)"
698 -re ".*$mi_gdb_prompt\[ \]*$" {
699 if ![string match "" $message] then {
706 perror "Window too small."
709 -re "\\(y or n\\) " {
711 perror "Got interactive prompt."
715 perror "Process no longer exists"
716 if { $message != "" } {
722 perror "internal buffer is full."
726 if ![string match "" $message] then {
727 fail "$message (timeout)"
733 # If the GDB output matched, compare the inferior output.
734 if { $result == 0 } {
735 if [ info exists ipattern ] {
736 if { ![target_info exists gdb,noinferiorio] } {
737 global mi_inferior_spawn_id
739 -i $mi_inferior_spawn_id -re "$ipattern" {
740 pass "$message inferior output"
743 fail "$message inferior output (timeout)"
748 unsupported "$message inferior output"
757 # MI run command. (A modified version of gdb_run_cmd)
760 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
761 # ``.*$'' could swallow up output that we attempt to match elsewhere.
763 proc mi_run_cmd {args} {
765 if { $suppress_flag } {
770 if [target_info exists gdb_init_command] {
771 send_gdb "[target_info gdb_init_command]\n";
773 -re "$mi_gdb_prompt$" { }
775 perror "gdb_init_command for target failed";
781 if { [mi_gdb_target_load] < 0 } {
785 if [target_info exists use_gdb_stub] {
786 if [target_info exists gdb,do_reload_on_run] {
787 send_gdb "000-exec-continue\n";
789 -re "000\\^running\[\r\n\]+$mi_gdb_prompt$" {}
795 if [target_info exists gdb,start_symbol] {
796 set start [target_info gdb,start_symbol];
801 # HACK: Should either use 000-jump or fix the target code
802 # to better handle RUN.
803 send_gdb "jump *$start\n"
804 warning "Using CLI jump command, expect run-to-main FAIL"
808 send_gdb "000-exec-run $args\n"
810 -re "000\\^running\r\n${mi_gdb_prompt}" {
813 perror "Unable to start target"
817 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
821 # Just like run-to-main but works with the MI interface
824 proc mi_run_to_main { } {
826 if { $suppress_flag } {
835 mi_delete_breakpoints
836 mi_gdb_reinitialize_dir $srcdir/$subdir
837 mi_gdb_load ${binfile}
843 # Just like gdb's "runto" proc, it will run the target to a given
844 # function. The big difference here between mi_runto and mi_execute_to
845 # is that mi_execute_to must have the inferior running already. This
846 # proc will (like gdb's runto) (re)start the inferior, too.
848 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
850 # -1 if test suppressed, failed, timedout
853 proc mi_runto_helper {func run_or_continue} {
855 if { $suppress_flag } {
859 global mi_gdb_prompt expect_out
860 global hex decimal fullname_syntax
862 set test "mi runto $func"
863 mi_gdb_test "200-break-insert -t $func" \
864 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
865 "breakpoint at $func"
867 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
868 || ![scan $str {number="%d"} bkptno]} {
872 if {$run_or_continue == "run"} {
875 send_gdb "000-exec-continue\n"
877 -re "000\\^running\r\n${mi_gdb_prompt}" {
887 -re ".*000\\*stopped,thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=\(\\\[.*\\\]\|\{.*\}\),file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
891 -re ".*$mi_gdb_prompt$" {
895 fail "$test (timeout)"
901 proc mi_runto {func} {
902 mi_runto_helper $func "run"
905 # Next to the next statement
906 # For return values, see mi_execute_to_helper
908 proc mi_next { test } {
909 return [mi_next_to {.*} {.*} {.*} {.*} $test]
913 # Step to the next statement
914 # For return values, see mi_execute_to_helper
916 proc mi_step { test } {
917 return [mi_step_to {.*} {.*} {.*} {.*} $test]
920 # cmd should not include the number or newline (i.e. "exec-step 3", not
921 # "220-exec-step 3\n"
923 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
924 # after the first prompt is printed.
926 proc mi_execute_to_helper { cmd reason func args file line extra test } {
928 if { $suppress_flag } {
934 global fullname_syntax
935 send_gdb "220-$cmd\n"
937 -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$" {
941 -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$" {
942 fail "$test (stopped at wrong place)"
945 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
946 fail "$test (unknown output after running)"
950 fail "$test (timeout)"
956 proc mi_execute_to { cmd reason func args file line extra test } {
957 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
958 "$file" "$line" "$extra" "$test"
961 proc mi_next_to { func args file line test } {
962 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
963 "$file" "$line" "" "$test"
966 proc mi_step_to { func args file line test } {
967 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
968 "$file" "$line" "" "$test"
971 proc mi_finish_to { func args file line result ret test } {
972 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
974 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
978 proc mi_continue_to {func} {
979 mi_runto_helper $func "continue"
982 proc mi0_execute_to { cmd reason func args file line extra test } {
983 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
984 "$file" "$line" "$extra" "$test"
987 proc mi0_next_to { func args file line test } {
988 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
989 "$file" "$line" "" "$test"
992 proc mi0_step_to { func args file line test } {
993 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
994 "$file" "$line" "" "$test"
997 proc mi0_finish_to { func args file line result ret test } {
998 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1000 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1004 proc mi0_continue_to { bkptno func args file line test } {
1005 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1006 "$func" "$args" "$file" "$line" "" "$test"
1009 # Creates varobj named NAME for EXPRESSION.
1010 # Name cannot be "-".
1011 proc mi_create_varobj { name expression testname } {
1012 mi_gdb_test "-var-create $name * $expression" \
1013 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1017 # Deletes the specified NAME.
1018 proc mi_delete_varobj { name testname } {
1019 mi_gdb_test "-var-delete $name" \
1020 "\\^done,ndeleted=.*" \
1024 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1025 # are reported as updated, and no other varobj is updated.
1026 # Assumes that no varobj is out of scope and that no varobj changes
1028 proc mi_varobj_update { name expected testname } {
1029 set er "\\^done,changelist=\\\["
1031 foreach item $expected {
1032 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1042 verbose -log "Expecting: $er" 2
1043 mi_gdb_test "-var-update $name" $er $testname
1046 proc mi_check_varobj_value { name value testname } {
1048 mi_gdb_test "-var-evaluate-expression $name" \
1049 "\\^done,value=\"$value\"" \
1053 # Check the results of the:
1055 # -var-list-children VARNAME
1057 # command. The CHILDREN parement should be a list of lists.
1058 # Each inner list can have either 3 or 4 elements, describing
1059 # fields that gdb is expected to report for child variable object,
1060 # in the following order
1064 # - Number of children
1067 # If inner list has 3 elements, the gdb is expected to output no
1070 proc mi_list_varobj_children { varname children testname } {
1072 set numchildren [llength $children]
1074 set whatever "\"\[^\"\]+\""
1076 foreach item $children {
1078 set name [lindex $item 0]
1079 set exp [lindex $item 1]
1080 set numchild [lindex $item 2]
1081 if {[llength $item] == 4} {
1082 set type [lindex $item 3]
1084 lappend children_exp\
1085 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"}"
1087 lappend children_exp\
1088 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"}"
1091 set children_exp_j [join $children_exp ","]
1092 set expected "\\^done,numchild=\"$numchildren\",children=\\\[$children_exp_j\\\]"
1094 verbose -log "Expecting: $expected"
1096 mi_gdb_test "-var-list-children $varname" $expected $testname
1099 # A list of two-element lists. First element of each list is
1100 # a Tcl statement, and the second element is the line
1101 # number of source C file where the statement originates.
1102 set mi_autotest_data ""
1103 # The name of the source file for autotesting.
1104 set mi_autotest_source ""
1106 proc count_newlines { string } {
1107 return [regexp -all "\n" $string]
1110 # Prepares for running inline tests in FILENAME.
1111 # See comments for mi_run_inline_test for detailed
1112 # explanation of the idea and syntax.
1113 proc mi_prepare_inline_tests { filename } {
1117 global mi_autotest_source
1118 global mi_autotest_data
1120 set mi_autotest_data {}
1122 set mi_autotest_source $filename
1124 if { ! [regexp "^/" "$filename"] } then {
1125 set filename "$srcdir/$subdir/$filename"
1128 set chan [open $filename]
1129 set content [read $chan]
1132 set start [string first "/*:" $content]
1134 set end [string first ":*/" $content]
1136 error "Unterminated special comment in $filename"
1139 set prefix [string range $content 0 $start]
1140 set prefix_newlines [count_newlines $prefix]
1142 set line_number [expr $line_number+$prefix_newlines]
1143 set comment_line $line_number
1145 set comment [string range $content [expr $start+3] [expr $end-1]]
1147 set comment_newlines [count_newlines $comment]
1148 set line_number [expr $line_number+$comment_newlines]
1150 set comment [string trim $comment]
1151 set content [string range $content [expr $end+3] \
1152 [string length $content]]
1153 lappend mi_autotest_data [list $comment $comment_line]
1161 # Helper to mi_run_inline_test below.
1162 # Return the list of all (statement,line_number) lists
1163 # that comprise TESTCASE. The begin and end markers
1165 proc mi_get_inline_test {testcase} {
1167 global mi_gdb_prompt
1168 global mi_autotest_data
1169 global mi_autotest_source
1175 foreach l $mi_autotest_data {
1177 set comment [lindex $l 0]
1179 if {$comment == "BEGIN: $testcase"} {
1181 } elseif {$comment == "END: $testcase"} {
1184 } elseif {$seen_begin==1} {
1189 if {$seen_begin == 0} {
1190 error "Autotest $testcase not found"
1193 if {$seen_begin == 1 && $seen_end == 0} {
1194 error "Missing end marker for test $testcase"
1200 # Sets temporary breakpoint at LOCATION.
1201 proc mi_tbreak {location} {
1203 global mi_gdb_prompt
1205 mi_gdb_test "-break-insert -t $location" \
1207 "run to $location (set breakpoint)"
1210 # Send COMMAND that must be a command that resumes
1211 # the inferiour (run/continue/next/etc) and consumes
1212 # the "^running" output from it.
1213 proc mi_send_resuming_command {command test} {
1215 global mi_gdb_prompt
1217 send_gdb "220-$command\n"
1219 -re "220\\^running\r\n${mi_gdb_prompt}" {
1227 # Helper to mi_run_inline_test below.
1228 # Sets a temporary breakpoint at LOCATION and runs
1229 # the program using COMMAND. When the program is stopped
1230 # returns the line at which it. Returns -1 if line cannot
1232 # Does not check that the line is the same as requested.
1233 # The caller can check itself if required.
1234 proc mi_continue_to_line {location test} {
1237 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1238 return [mi_wait_for_stop $test]
1241 # Wait until gdb prints the current line.
1242 proc mi_wait_for_stop {test} {
1244 global mi_gdb_prompt
1247 -re ".*line=\"(.*)\".*\r\n$mi_gdb_prompt$" {
1248 return $expect_out(1,string)
1250 -re ".*$mi_gdb_prompt$" {
1251 fail "wait for stop ($test)"
1254 fail "wait for stop ($test)"
1259 # Run a MI test embedded in comments in a C file.
1260 # The C file should contain special comments in the following
1263 # /*: BEGIN: testname :*/
1264 # /*: <Tcl statements> :*/
1265 # /*: END: testname :*/
1267 # This procedure find the begin and end marker for the requested
1268 # test. Then, a temporary breakpoint is set at the begin
1269 # marker and the program is run (from start).
1271 # After that, for each special comment between the begin and end
1272 # marker, the Tcl statements are executed. It is assumed that
1273 # for each comment, the immediately preceding line is executable
1274 # C statement. Then, gdb will be single-stepped until that
1275 # preceding C statement is executed, and after that the
1276 # Tcl statements in the comment will be executed.
1280 # /*: BEGIN: assignment-test :*/
1282 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1283 # /*: END: assignment-test :*/
1285 # The mi_prepare_inline_tests function should be called before
1286 # calling this function. A given C file can contain several
1287 # inline tests. The names of the tests must be unique within one
1290 proc mi_run_inline_test { testcase } {
1292 global mi_gdb_prompt
1295 global fullname_syntax
1296 global mi_autotest_source
1298 set commands [mi_get_inline_test $testcase]
1303 foreach c $commands {
1304 set statements [lindex $c 0]
1305 set line [lindex $c 1]
1306 set line [expr $line-1]
1308 # We want gdb to be stopped at the expression immediately
1309 # before the comment. If this is the first comment, the
1310 # program is either not started yet or is in some random place,
1311 # so we run it. For further comments, we might be already
1312 # standing at the right line. If not continue till the
1316 # Start the program afresh.
1317 mi_tbreak "$mi_autotest_source:$line"
1319 set line_now [mi_wait_for_stop "$testcase: step to $line"]
1321 } elseif {$line_now!=$line} {
1322 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1325 if {$line_now!=$line} {
1326 fail "$testcase: go to line $line"
1329 # We're not at the statement right above the comment.
1330 # Execute that statement so that the comment can test
1331 # the state after the statement is executed.
1333 # Single-step past the line.
1334 mi_send_resuming_command "exec-next" "$testcase: step over $line"
1335 set line_now [mi_wait_for_stop "$testcase: step over $line"]
1337 # We probably want to use 'uplevel' so that statements
1338 # have direct access to global variables that the
1339 # main 'exp' file has set up. But it's not yet clear,
1340 # will need more experience to be sure.