1 # Copyright 1999, 2000, 2002 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."
285 # load a file into the debugger.
286 # return a -1 if anything goes wrong.
288 proc mi_gdb_load { arg } {
294 global last_mi_gdb_file
295 upvar timeout timeout
298 set arg $last_mi_gdb_file;
301 set last_mi_gdb_file $arg;
306 # FIXME: Several of these patterns are only acceptable for console
307 # output. Queries are an error for mi.
308 send_gdb "105-file-exec-and-symbols $arg\n"
310 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
311 verbose "\t\tLoaded $arg into the $GDB"
314 -re "has no symbol-table.*$mi_gdb_prompt$" {
315 perror "$arg wasn't compiled with \"-g\""
318 -re "A program is being debugged already.*Kill it.*y or n. $" {
320 verbose "\t\tKilling previous program being debugged"
323 -re "Load new symbol table from \".*\".*y or n. $" {
326 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
327 verbose "\t\tLoaded $arg with new symbol table into $GDB"
331 perror "(timeout) Couldn't load $arg, other program already loaded."
336 -re "No such file or directory.*$mi_gdb_prompt$" {
337 perror "($arg) No such file or directory\n"
340 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
341 # We are just giving the prompt back for now
345 perror "couldn't load $arg into $GDB (timed out)."
349 # This is an attempt to detect a core dump, but seems not to
350 # work. Perhaps we need to match .* followed by eof, in which
351 # gdb_expect does not seem to have a way to do that.
352 perror "couldn't load $arg into $GDB (end of file)."
358 if { [info procs send_target_sid] != "" } {
359 # For SID, things get complex
362 -re "\\^done,.*$mi_gdb_prompt$" {
365 perror "Unable to connect to SID target"
369 send_gdb "48-target-download\n"
371 -re "48\\^done.*$mi_gdb_prompt$" {
374 perror "Unable to download to SID target"
378 } elseif { [target_info protocol] == "sim" } {
379 # For the simulator, just connect to it directly.
380 send_gdb "47-target-select sim\n"
382 -re "47\\^connected.*$mi_gdb_prompt$" {
385 perror "Unable to select sim target"
389 send_gdb "48-target-download\n"
391 -re "48\\^done.*$mi_gdb_prompt$" {
394 perror "Unable to download to sim target"
398 } elseif { [target_info gdb_protocol] == "remote" } {
400 send_gdb "target [target_info gdb_protocol] [target_info netport]\n"
402 -re "\\^done,.*$mi_gdb_prompt$" {
405 perror "Unable to connect to remote target"
409 send_gdb "48-target-download\n"
411 -re "48\\^done.*$mi_gdb_prompt$" {
414 perror "Unable to download to remote target"
422 # mi_gdb_test COMMAND PATTERN MESSAGE -- send a command to gdb; test the result.
424 # COMMAND is the command to execute, send to GDB with send_gdb. If
425 # this is the null string no command is sent.
426 # PATTERN is the pattern to match for a PASS, and must NOT include
427 # the \r\n sequence immediately before the gdb prompt.
428 # MESSAGE is an optional message to be printed. If this is
429 # omitted, then the pass/fail messages use the command string as the
430 # message. (If this is the empty string, then sometimes we don't
431 # call pass or fail at all; I don't understand this at all.)
434 # 1 if the test failed,
435 # 0 if the test passes,
436 # -1 if there was an internal error.
438 proc mi_gdb_test { args } {
441 global GDB expect_out
442 upvar timeout timeout
444 if [llength $args]>2 then {
445 set message [lindex $args 2]
447 set message [lindex $args 0]
449 set command [lindex $args 0]
450 set pattern [lindex $args 1]
452 if [llength $args]==5 {
453 set question_string [lindex $args 3];
454 set response_string [lindex $args 4];
456 set question_string "^FOOBAR$"
460 send_user "Sending \"$command\" to gdb\n"
461 send_user "Looking to match \"$pattern\"\n"
462 send_user "Message is \"$message\"\n"
466 set string "${command}\n";
467 if { $command != "" } {
468 while { "$string" != "" } {
469 set foo [string first "\n" "$string"];
470 set len [string length "$string"];
471 if { $foo < [expr $len - 1] } {
472 set str [string range "$string" 0 $foo];
473 if { [send_gdb "$str"] != "" } {
474 global suppress_flag;
476 if { ! $suppress_flag } {
477 perror "Couldn't send $command to GDB.";
486 set string [string range "$string" [expr $foo + 1] end];
491 if { "$string" != "" } {
492 if { [send_gdb "$string"] != "" } {
493 global suppress_flag;
495 if { ! $suppress_flag } {
496 perror "Couldn't send $command to GDB.";
504 if [info exists timeout] {
508 if [info exists timeout] {
515 -re "\\*\\*\\* DOSEXIT code.*" {
516 if { $message != "" } {
519 gdb_suppress_entire_file "GDB died";
522 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
523 if ![isnative] then {
524 warning "Can`t communicate to remote target."
530 -re "\[\r\n\]*($pattern)\[\r\n\]+$mi_gdb_prompt\[ \]*$" {
531 if ![string match "" $message] then {
536 -re "(${question_string})$" {
537 send_gdb "$response_string\n";
540 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
541 perror "Undefined command \"$command\"."
545 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
546 perror "\"$command\" is not a unique command name."
550 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
551 if ![string match "" $message] then {
552 set errmsg "$message: the program exited"
554 set errmsg "$command: the program exited"
559 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
560 if ![string match "" $message] then {
561 set errmsg "$message: the program is no longer running"
563 set errmsg "$command: the program is no longer running"
568 -re ".*$mi_gdb_prompt\[ \]*$" {
569 if ![string match "" $message] then {
576 perror "Window too small."
579 -re "\\(y or n\\) " {
581 perror "Got interactive prompt."
585 perror "Process no longer exists"
586 if { $message != "" } {
592 perror "internal buffer is full."
596 if ![string match "" $message] then {
597 fail "$message (timeout)"
606 # MI run command. (A modified version of gdb_run_cmd)
609 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
610 # ``.*$'' could swallow up output that we attempt to match elsewhere.
612 proc mi_run_cmd {args} {
614 if { $suppress_flag } {
619 if [target_info exists gdb_init_command] {
620 send_gdb "[target_info gdb_init_command]\n";
622 -re "$mi_gdb_prompt$" { }
624 perror "gdb_init_command for target failed";
630 if [target_info exists use_gdb_stub] {
631 if [target_info exists gdb,do_reload_on_run] {
632 # Specifying no file, defaults to the executable
633 # currently being debugged.
634 if { [mi_gdb_load ""] < 0 } {
637 send_gdb "000-exec-continue\n";
639 -re "Continu\[^\r\n\]*\[\r\n\]" {}
645 if [target_info exists gdb,start_symbol] {
646 set start [target_info gdb,start_symbol];
651 # HACK: Should either use 000-jump or fix the target code
652 # to better handle RUN.
653 send_gdb "jump *$start\n"
654 warning "Using CLI jump command, expect run-to-main FAIL"
658 send_gdb "000-exec-run $args\n"
660 -re "000\\^running\r\n${mi_gdb_prompt}" {
663 perror "Unable to start target"
667 # NOTE: Shortly after this there will be a ``000*stopping,...(gdb)''
671 # Just like run-to-main but works with the MI interface
674 proc mi_run_to_main { } {
676 if { $suppress_flag } {
685 mi_delete_breakpoints
686 mi_gdb_reinitialize_dir $srcdir/$subdir
687 mi_gdb_load ${binfile}
693 # Just like gdb's "runto" proc, it will run the target to a given
694 # function. The big difference here between mi_runto and mi_execute_to
695 # is that mi_execute_to must have the inferior running already. This
696 # proc will (like gdb's runto) (re)start the inferior, too.
698 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
700 # -1 if test suppressed, failed, timedout
703 proc mi_runto {func} {
705 if { $suppress_flag } {
709 global mi_gdb_prompt expect_out
712 set test "mi runto $func"
713 mi_gdb_test "200-break-insert $func" \
714 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"keep\",enabled=\"y\",addr=\"$hex\",func=\"$func\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
715 "breakpoint at $func"
717 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
718 || ![scan $str {number="%d"} bkptno]} {
724 -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$" {
728 -re ".*$mi_gdb_prompt$" {
732 fail "$test (timeout)"
739 # Next to the next statement
740 # For return values, see mi_execute_to_helper
742 proc mi_next { test } {
743 return [mi_next_to {.*} {.*} {.*} {.*} $test]
747 # Step to the next statement
748 # For return values, see mi_execute_to_helper
750 proc mi_step { test } {
751 return [mi_step_to {.*} {.*} {.*} {.*} $test]
754 # cmd should not include the number or newline (i.e. "exec-step 3", not
755 # "220-exec-step 3\n"
757 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
758 # after the first prompt is printed.
760 proc mi_execute_to_helper { cmd reason func args file line extra test } {
762 if { $suppress_flag } {
768 send_gdb "220-$cmd\n"
770 -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$" {
774 -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$" {
775 fail "$test (stopped at wrong place)"
778 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
779 fail "$test (unknown output after running)"
783 fail "$test (timeout)"
789 proc mi_execute_to { cmd reason func args file line extra test } {
790 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
791 "$file" "$line" "$extra" "$test"
794 proc mi_next_to { func args file line test } {
795 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
796 "$file" "$line" "" "$test"
799 proc mi_step_to { func args file line test } {
800 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
801 "$file" "$line" "" "$test"
804 proc mi_finish_to { func args file line result ret test } {
805 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
807 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
811 proc mi_continue_to { bkptno func args file line test } {
812 mi_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
813 "$func" "$args" "$file" "$line" "" "$test"
816 proc mi0_execute_to { cmd reason func args file line extra test } {
817 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
818 "$file" "$line" "$extra" "$test"
821 proc mi0_next_to { func args file line test } {
822 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
823 "$file" "$line" "" "$test"
826 proc mi0_step_to { func args file line test } {
827 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
828 "$file" "$line" "" "$test"
831 proc mi0_finish_to { func args file line result ret test } {
832 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
834 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
838 proc mi0_continue_to { bkptno func args file line test } {
839 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
840 "$func" "$args" "$file" "$line" "" "$test"