1 # Copyright 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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"
34 # mi_gdb_exit -- exit the GDB, killing the target program if necessary
37 catch mi_uncatched_gdb_exit
40 proc mi_uncatched_gdb_exit {} {
49 gdb_stop_suppressing_tests;
51 if { [info procs sid_exit] != "" } {
55 if ![info exists gdb_spawn_id] {
59 verbose "Quitting $GDB $GDBFLAGS $MIFLAGS"
61 if { [is_remote host] && [board_info host exists fileid] } {
62 send_gdb "999-gdb-exit\n";
68 -re "Undefined command.*$gdb_prompt $" {
72 -re "DOSEXIT code" { }
77 if ![is_remote host] {
84 # start gdb -- start gdb running, default procedure
86 # When running over NFS, particularly if running many simultaneous
87 # tests on different hosts all using the same server, things can
88 # get really slow. Give gdb at least 3 minutes to start up.
90 proc mi_gdb_start { } {
100 gdb_stop_suppressing_tests;
103 if { [info procs sid_start] != "" } {
104 verbose "Spawning SID"
108 verbose "Spawning $GDB -nw $GDBFLAGS $MIFLAGS"
110 if [info exists gdb_spawn_id] {
114 if ![is_remote host] {
115 if { [which $GDB] == 0 } then {
116 perror "$GDB does not exist."
120 set res [remote_spawn host "$GDB -nw $GDBFLAGS $MIFLAGS [host_info gdb_opts]"];
121 if { $res < 0 || $res == "" } {
122 perror "Spawning $GDB failed."
126 -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
127 # We have a new format mi startup prompt. If we are
128 # running mi1, then this is an error as we should be
129 # using the old-style prompt.
130 if { $MIFLAGS == "-i=mi1" } {
131 perror "(mi startup) Got unexpected new mi prompt."
135 verbose "GDB initialized."
137 -re "\[^~\].*$mi_gdb_prompt$" {
138 # We have an old format mi startup prompt. If we are
139 # not running mi1, then this is an error as we should be
140 # using the new-style prompt.
141 if { $MIFLAGS != "-i=mi1" } {
142 perror "(mi startup) Got unexpected old mi prompt."
146 verbose "GDB initialized."
148 -re ".*$gdb_prompt $" {
149 untested "Skip mi tests (got non-mi prompt)."
153 -re ".*unrecognized option.*for a complete list of options." {
154 untested "Skip mi tests (not compiled with mi support)."
158 -re ".*Interpreter `mi' unrecognized." {
159 untested "Skip mi tests (not compiled with mi support)."
164 perror "(timeout) GDB never initialized after 10 seconds."
171 # FIXME: mi output does not go through pagers, so these can be removed.
172 # force the height to "unlimited", so no pagers get used
173 send_gdb "100-gdb-set height 0\n"
175 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
176 verbose "Setting height to 0." 2
179 warning "Couldn't set the height to 0"
182 # force the width to "unlimited", so no wraparound occurs
183 send_gdb "101-gdb-set width 0\n"
185 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
186 verbose "Setting width to 0." 2
189 warning "Couldn't set the width to 0."
196 # Many of the tests depend on setting breakpoints at various places and
197 # running until that breakpoint is reached. At times, we want to start
198 # with a clean-slate with respect to breakpoints, so this utility proc
199 # lets us do this without duplicating this code everywhere.
202 proc mi_delete_breakpoints {} {
205 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
206 send_gdb "102-break-delete\n"
208 -re "Delete all breakpoints.*y or n.*$" {
212 -re ".*102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
213 # This happens if there were no breakpoints
215 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
218 # The correct output is not "No breakpoints or watchpoints." but an
219 # empty BreakpointTable. Also, a query is not acceptable with mi.
220 send_gdb "103-break-list\n"
222 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
223 -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=\\\[\\\]\}" {}
224 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
225 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
226 -re "Delete all breakpoints.*or n.*$" {
227 warning "Unexpected prompt for breakpoints deletion";
231 timeout { perror "-break-list (timeout)" ; return }
235 proc mi_gdb_reinitialize_dir { subdir } {
240 if { $suppress_flag } {
244 if [is_remote host] {
248 if { $MIFLAGS == "-i=mi1" } {
249 send_gdb "104-environment-directory\n"
251 -re ".*Reinitialize source path to empty.*y or n. " {
252 warning "Got confirmation prompt for dir reinitialization."
255 -re "$mi_gdb_prompt$" {}
256 timeout {error "Dir reinitialization failed (timeout)"}
259 -re "$mi_gdb_prompt$" {}
260 timeout {error "Dir reinitialization failed (timeout)"}
263 send_gdb "104-environment-directory -r\n"
265 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
266 -re "$mi_gdb_prompt$" {}
267 timeout {error "Dir reinitialization failed (timeout)"}
271 send_gdb "105-environment-directory $subdir\n"
273 -re "Source directories searched.*$mi_gdb_prompt$" {
274 verbose "Dir set to $subdir"
276 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
277 # FIXME: We return just the prompt for now.
278 verbose "Dir set to $subdir"
279 # perror "Dir \"$subdir\" failed."
284 # Send GDB the "target" command.
285 # FIXME: Some of these patterns are not appropriate for MI. Based on
286 # config/monitor.exp:gdb_target_command.
287 proc mi_gdb_target_cmd { targetname serialport } {
290 for {set i 1} {$i <= 3} {incr i} {
291 send_gdb "47-target-select $targetname $serialport\n"
293 -re "47\\^connected.*$mi_gdb_prompt$" {
294 verbose "Set target to $targetname";
297 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
298 verbose "Connection failed";
300 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
301 verbose "Set target to $targetname";
304 -re "Remote debugging using .*$serialport.*$mi_gdb_prompt$" {
305 verbose "Set target to $targetname";
308 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
309 verbose "Set target to $targetname";
312 -re "Connected to.*$mi_gdb_prompt$" {
313 verbose "Set target to $targetname";
316 -re "Ending remote.*$mi_gdb_prompt$" { }
317 -re "Connection refused.*$mi_gdb_prompt$" {
318 verbose "Connection refused by remote target. Pausing, and trying again."
322 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
323 verbose "Got timeout error from gdb.";
335 # load a file into the debugger (file command only).
336 # return a -1 if anything goes wrong.
338 proc mi_gdb_file_cmd { arg } {
344 global last_mi_gdb_file
345 global last_mi_remote_file
346 upvar timeout timeout
349 set arg $last_mi_gdb_file;
351 set last_mi_gdb_file $arg
352 if { [ info exists last_mi_remote_file ] } {
353 unset last_mi_remote_file
357 if [is_remote host] {
358 set arg [remote_download host $arg];
360 error "download failed"
365 # FIXME: Several of these patterns are only acceptable for console
366 # output. Queries are an error for mi.
367 send_gdb "105-file-exec-and-symbols $arg\n"
369 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
370 verbose "\t\tLoaded $arg into the $GDB"
373 -re "has no symbol-table.*$mi_gdb_prompt$" {
374 perror "$arg wasn't compiled with \"-g\""
377 -re "A program is being debugged already.*Kill it.*y or n. $" {
379 verbose "\t\tKilling previous program being debugged"
382 -re "Load new symbol table from \".*\".*y or n. $" {
385 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
386 verbose "\t\tLoaded $arg with new symbol table into $GDB"
390 perror "(timeout) Couldn't load $arg, other program already loaded."
395 -re "No such file or directory.*$mi_gdb_prompt$" {
396 perror "($arg) No such file or directory\n"
399 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
400 # We (MI) are just giving the prompt back for now, instead of giving
401 # some acknowledgement.
405 perror "couldn't load $arg into $GDB (timed out)."
409 # This is an attempt to detect a core dump, but seems not to
410 # work. Perhaps we need to match .* followed by eof, in which
411 # gdb_expect does not seem to have a way to do that.
412 perror "couldn't load $arg into $GDB (end of file)."
419 # load a file into the debugger.
420 # return a -1 if anything goes wrong.
422 proc mi_gdb_load { arg } {
428 upvar timeout timeout
436 if { [info procs gdbserver_gdb_load] != "" } {
437 global last_mi_gdb_file
438 global last_mi_remote_file
440 if { ! [info exists last_mi_remote_file] } {
441 if [is_remote target] {
442 set last_mi_remote_file [remote_download target $arg]
444 set last_mi_remote_file $last_mi_gdb_file
448 set res [gdbserver_gdb_load $last_mi_remote_file]
449 set protocol [lindex $res 0]
450 set gdbport [lindex $res 1]
452 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
455 } elseif { [info procs send_target_sid] != "" } {
456 # For SID, things get complex
459 -re "\\^done,.*$mi_gdb_prompt$" {
462 perror "Unable to connect to SID target"
466 send_gdb "48-target-download\n"
468 -re "48\\^done.*$mi_gdb_prompt$" {
471 perror "Unable to download to SID target"
475 } elseif { [target_info protocol] == "sim" } {
476 # For the simulator, just connect to it directly.
477 send_gdb "47-target-select sim\n"
479 -re "47\\^connected.*$mi_gdb_prompt$" {
482 perror "Unable to select sim target"
486 send_gdb "48-target-download\n"
488 -re "48\\^done.*$mi_gdb_prompt$" {
491 perror "Unable to download to sim target"
495 } elseif { [target_info gdb_protocol] == "remote" } {
497 send_gdb "target [target_info gdb_protocol] [target_info netport]\n"
499 -re "\\^done,.*$mi_gdb_prompt$" {
502 perror "Unable to connect to remote target"
506 send_gdb "48-target-download\n"
508 -re "48\\^done.*$mi_gdb_prompt$" {
511 perror "Unable to download to remote target"
519 # mi_gdb_test COMMAND PATTERN MESSAGE -- send a command to gdb; test the result.
521 # COMMAND is the command to execute, send to GDB with send_gdb. If
522 # this is the null string no command is sent.
523 # PATTERN is the pattern to match for a PASS, and must NOT include
524 # the \r\n sequence immediately before the gdb prompt.
525 # MESSAGE is an optional message to be printed. If this is
526 # omitted, then the pass/fail messages use the command string as the
527 # message. (If this is the empty string, then sometimes we don't
528 # call pass or fail at all; I don't understand this at all.)
531 # 1 if the test failed,
532 # 0 if the test passes,
533 # -1 if there was an internal error.
535 proc mi_gdb_test { args } {
538 global GDB expect_out
539 upvar timeout timeout
541 if [llength $args]>2 then {
542 set message [lindex $args 2]
544 set message [lindex $args 0]
546 set command [lindex $args 0]
547 set pattern [lindex $args 1]
549 if [llength $args]==5 {
550 set question_string [lindex $args 3];
551 set response_string [lindex $args 4];
553 set question_string "^FOOBAR$"
557 send_user "Sending \"$command\" to gdb\n"
558 send_user "Looking to match \"$pattern\"\n"
559 send_user "Message is \"$message\"\n"
563 set string "${command}\n";
564 if { $command != "" } {
565 while { "$string" != "" } {
566 set foo [string first "\n" "$string"];
567 set len [string length "$string"];
568 if { $foo < [expr $len - 1] } {
569 set str [string range "$string" 0 $foo];
570 if { [send_gdb "$str"] != "" } {
571 global suppress_flag;
573 if { ! $suppress_flag } {
574 perror "Couldn't send $command to GDB.";
583 set string [string range "$string" [expr $foo + 1] end];
588 if { "$string" != "" } {
589 if { [send_gdb "$string"] != "" } {
590 global suppress_flag;
592 if { ! $suppress_flag } {
593 perror "Couldn't send $command to GDB.";
601 if [info exists timeout] {
605 if [info exists timeout] {
612 -re "\\*\\*\\* DOSEXIT code.*" {
613 if { $message != "" } {
616 gdb_suppress_entire_file "GDB died";
619 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
620 if ![isnative] then {
621 warning "Can`t communicate to remote target."
627 -re "\[\r\n\]*($pattern)\[\r\n\]+$mi_gdb_prompt\[ \]*$" {
628 if ![string match "" $message] then {
633 -re "(${question_string})$" {
634 send_gdb "$response_string\n";
637 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
638 perror "Undefined command \"$command\"."
642 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
643 perror "\"$command\" is not a unique command name."
647 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
648 if ![string match "" $message] then {
649 set errmsg "$message (the program exited)"
651 set errmsg "$command (the program exited)"
656 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
657 if ![string match "" $message] then {
658 set errmsg "$message (the program is no longer running)"
660 set errmsg "$command (the program is no longer running)"
665 -re ".*$mi_gdb_prompt\[ \]*$" {
666 if ![string match "" $message] then {
673 perror "Window too small."
676 -re "\\(y or n\\) " {
678 perror "Got interactive prompt."
682 perror "Process no longer exists"
683 if { $message != "" } {
689 perror "internal buffer is full."
693 if ![string match "" $message] then {
694 fail "$message (timeout)"
703 # MI run command. (A modified version of gdb_run_cmd)
706 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
707 # ``.*$'' could swallow up output that we attempt to match elsewhere.
709 proc mi_run_cmd {args} {
711 if { $suppress_flag } {
716 if [target_info exists gdb_init_command] {
717 send_gdb "[target_info gdb_init_command]\n";
719 -re "$mi_gdb_prompt$" { }
721 perror "gdb_init_command for target failed";
727 if [target_info exists use_gdb_stub] {
728 if [target_info exists gdb,do_reload_on_run] {
729 # Specifying no file, defaults to the executable
730 # currently being debugged.
731 if { [mi_gdb_load ""] < 0 } {
734 send_gdb "000-exec-continue\n";
736 -re "000\\^running\[\r\n\]+$mi_gdb_prompt$" {}
742 if [target_info exists gdb,start_symbol] {
743 set start [target_info gdb,start_symbol];
748 # HACK: Should either use 000-jump or fix the target code
749 # to better handle RUN.
750 send_gdb "jump *$start\n"
751 warning "Using CLI jump command, expect run-to-main FAIL"
755 send_gdb "000-exec-run $args\n"
757 -re "000\\^running\r\n${mi_gdb_prompt}" {
760 perror "Unable to start target"
764 # NOTE: Shortly after this there will be a ``000*stopping,...(gdb)''
768 # Just like run-to-main but works with the MI interface
771 proc mi_run_to_main { } {
773 if { $suppress_flag } {
782 mi_delete_breakpoints
783 mi_gdb_reinitialize_dir $srcdir/$subdir
784 mi_gdb_load ${binfile}
790 # Just like gdb's "runto" proc, it will run the target to a given
791 # function. The big difference here between mi_runto and mi_execute_to
792 # is that mi_execute_to must have the inferior running already. This
793 # proc will (like gdb's runto) (re)start the inferior, too.
795 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
797 # -1 if test suppressed, failed, timedout
800 proc mi_runto {func} {
802 if { $suppress_flag } {
806 global mi_gdb_prompt expect_out
809 set test "mi runto $func"
810 mi_gdb_test "200-break-insert $func" \
811 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"keep\",enabled=\"y\",addr=\"$hex\",func=\"$func\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
812 "breakpoint at $func"
814 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
815 || ![scan $str {number="%d"} bkptno]} {
821 -re ".*000\\*stopped,reason=\"breakpoint-hit\",bkptno=\"$bkptno\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=\(\\\[.*\\\]\|\{.*\}\),file=\".*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
825 -re ".*$mi_gdb_prompt$" {
829 fail "$test (timeout)"
836 # Next to the next statement
837 # For return values, see mi_execute_to_helper
839 proc mi_next { test } {
840 return [mi_next_to {.*} {.*} {.*} {.*} $test]
844 # Step to the next statement
845 # For return values, see mi_execute_to_helper
847 proc mi_step { test } {
848 return [mi_step_to {.*} {.*} {.*} {.*} $test]
851 # cmd should not include the number or newline (i.e. "exec-step 3", not
852 # "220-exec-step 3\n"
854 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
855 # after the first prompt is printed.
857 proc mi_execute_to_helper { cmd reason func args file line extra test } {
859 if { $suppress_flag } {
865 send_gdb "220-$cmd\n"
867 -re ".*220\\^running\r\n${mi_gdb_prompt}.*220\\*stopped,reason=\"$reason\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\".*$file\",line=\"$line\"\}$extra\r\n$mi_gdb_prompt$" {
871 -re ".*220\\^running\r\n${mi_gdb_prompt}.*220\\*stopped,reason=\"$reason\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\".*\",args=\[\\\[\{\].*\[\\\]\}\],file=\".*\",line=\"\[0-9\]*\"\}.*\r\n$mi_gdb_prompt$" {
872 fail "$test (stopped at wrong place)"
875 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
876 fail "$test (unknown output after running)"
880 fail "$test (timeout)"
886 proc mi_execute_to { cmd reason func args file line extra test } {
887 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
888 "$file" "$line" "$extra" "$test"
891 proc mi_next_to { func args file line test } {
892 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
893 "$file" "$line" "" "$test"
896 proc mi_step_to { func args file line test } {
897 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
898 "$file" "$line" "" "$test"
901 proc mi_finish_to { func args file line result ret test } {
902 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
904 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
908 proc mi_continue_to { bkptno func args file line test } {
909 mi_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
910 "$func" "$args" "$file" "$line" "" "$test"
913 proc mi0_execute_to { cmd reason func args file line extra test } {
914 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
915 "$file" "$line" "$extra" "$test"
918 proc mi0_next_to { func args file line test } {
919 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
920 "$file" "$line" "" "$test"
923 proc mi0_step_to { func args file line test } {
924 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
925 "$file" "$line" "" "$test"
928 proc mi0_finish_to { func args file line result ret test } {
929 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
931 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
935 proc mi0_continue_to { bkptno func args file line test } {
936 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
937 "$func" "$args" "$file" "$line" "" "$test"