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 } {
239 if { $suppress_flag } {
243 if [is_remote host] {
247 send_gdb "104-environment-directory\n"
249 -re ".*Reinitialize source path to empty.*y or n. " {
250 warning "Got confirmation prompt for dir reinitialization."
253 -re "$mi_gdb_prompt$" {}
254 timeout {error "Dir reinitialization failed (timeout)"}
257 -re "$mi_gdb_prompt$" {}
258 timeout {error "Dir reinitialization failed (timeout)"}
261 send_gdb "105-environment-directory $subdir\n"
263 -re "Source directories searched.*$mi_gdb_prompt$" {
264 verbose "Dir set to $subdir"
266 -re "105\\\^done\r\n$mi_gdb_prompt$" {
267 # FIXME: We return just the prompt for now.
268 verbose "Dir set to $subdir"
269 # perror "Dir \"$subdir\" failed."
275 # load a file into the debugger.
276 # return a -1 if anything goes wrong.
278 proc mi_gdb_load { arg } {
284 upvar timeout timeout
289 # FIXME: Several of these patterns are only acceptable for console
290 # output. Queries are an error for mi.
291 send_gdb "105-file-exec-and-symbols $arg\n"
293 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
294 verbose "\t\tLoaded $arg into the $GDB"
297 -re "has no symbol-table.*$mi_gdb_prompt$" {
298 perror "$arg wasn't compiled with \"-g\""
301 -re "A program is being debugged already.*Kill it.*y or n. $" {
303 verbose "\t\tKilling previous program being debugged"
306 -re "Load new symbol table from \".*\".*y or n. $" {
309 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
310 verbose "\t\tLoaded $arg with new symbol table into $GDB"
314 perror "(timeout) Couldn't load $arg, other program already loaded."
319 -re "No such file or directory.*$mi_gdb_prompt$" {
320 perror "($arg) No such file or directory\n"
323 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
324 # We are just giving the prompt back for now
328 perror "couldn't load $arg into $GDB (timed out)."
332 # This is an attempt to detect a core dump, but seems not to
333 # work. Perhaps we need to match .* followed by eof, in which
334 # gdb_expect does not seem to have a way to do that.
335 perror "couldn't load $arg into $GDB (end of file)."
341 if { [info procs send_target_sid] != "" } {
342 # For SID, things get complex
345 -re "\\^done,.*$mi_gdb_prompt$" {
348 perror "Unable to connect to SID target"
352 send_gdb "48-target-download\n"
354 -re "48\\^done.*$mi_gdb_prompt$" {
357 perror "Unable to download to SID target"
361 } elseif { [target_info protocol] == "sim" } {
362 # For the simulator, just connect to it directly.
363 send_gdb "47-target-select sim\n"
365 -re "47\\^connected.*$mi_gdb_prompt$" {
368 perror "Unable to select sim target"
372 send_gdb "48-target-download\n"
374 -re "48\\^done.*$mi_gdb_prompt$" {
377 perror "Unable to download to sim target"
385 # mi_gdb_test COMMAND PATTERN MESSAGE -- send a command to gdb; test the result.
387 # COMMAND is the command to execute, send to GDB with send_gdb. If
388 # this is the null string no command is sent.
389 # PATTERN is the pattern to match for a PASS, and must NOT include
390 # the \r\n sequence immediately before the gdb prompt.
391 # MESSAGE is an optional message to be printed. If this is
392 # omitted, then the pass/fail messages use the command string as the
393 # message. (If this is the empty string, then sometimes we don't
394 # call pass or fail at all; I don't understand this at all.)
397 # 1 if the test failed,
398 # 0 if the test passes,
399 # -1 if there was an internal error.
401 proc mi_gdb_test { args } {
404 global GDB expect_out
405 upvar timeout timeout
407 if [llength $args]>2 then {
408 set message [lindex $args 2]
410 set message [lindex $args 0]
412 set command [lindex $args 0]
413 set pattern [lindex $args 1]
415 if [llength $args]==5 {
416 set question_string [lindex $args 3];
417 set response_string [lindex $args 4];
419 set question_string "^FOOBAR$"
423 send_user "Sending \"$command\" to gdb\n"
424 send_user "Looking to match \"$pattern\"\n"
425 send_user "Message is \"$message\"\n"
429 set string "${command}\n";
430 if { $command != "" } {
431 while { "$string" != "" } {
432 set foo [string first "\n" "$string"];
433 set len [string length "$string"];
434 if { $foo < [expr $len - 1] } {
435 set str [string range "$string" 0 $foo];
436 if { [send_gdb "$str"] != "" } {
437 global suppress_flag;
439 if { ! $suppress_flag } {
440 perror "Couldn't send $command to GDB.";
449 set string [string range "$string" [expr $foo + 1] end];
454 if { "$string" != "" } {
455 if { [send_gdb "$string"] != "" } {
456 global suppress_flag;
458 if { ! $suppress_flag } {
459 perror "Couldn't send $command to GDB.";
467 if [info exists timeout] {
471 if [info exists timeout] {
478 -re "\\*\\*\\* DOSEXIT code.*" {
479 if { $message != "" } {
482 gdb_suppress_entire_file "GDB died";
485 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
486 if ![isnative] then {
487 warning "Can`t communicate to remote target."
493 -re "\[\r\n\]*($pattern)\[\r\n\]+$mi_gdb_prompt\[ \]*$" {
494 if ![string match "" $message] then {
499 -re "(${question_string})$" {
500 send_gdb "$response_string\n";
503 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
504 perror "Undefined command \"$command\"."
508 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
509 perror "\"$command\" is not a unique command name."
513 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
514 if ![string match "" $message] then {
515 set errmsg "$message: the program exited"
517 set errmsg "$command: the program exited"
522 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
523 if ![string match "" $message] then {
524 set errmsg "$message: the program is no longer running"
526 set errmsg "$command: the program is no longer running"
531 -re ".*$mi_gdb_prompt\[ \]*$" {
532 if ![string match "" $message] then {
539 perror "Window too small."
542 -re "\\(y or n\\) " {
544 perror "Got interactive prompt."
548 perror "Process no longer exists"
549 if { $message != "" } {
555 perror "internal buffer is full."
559 if ![string match "" $message] then {
560 fail "$message (timeout)"
569 # MI run command. (A modified version of gdb_run_cmd)
572 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
573 # ``.*$'' could swallow up output that we attempt to match elsewhere.
575 proc mi_run_cmd {args} {
577 if { $suppress_flag } {
582 if [target_info exists gdb_init_command] {
583 send_gdb "[target_info gdb_init_command]\n";
585 -re "$mi_gdb_prompt$" { }
587 perror "gdb_init_command for target failed";
593 if [target_info exists use_gdb_stub] {
594 if [target_info exists gdb,do_reload_on_run] {
595 # Specifying no file, defaults to the executable
596 # currently being debugged.
597 if { [mi_gdb_load ""] < 0 } {
600 send_gdb "000-exec-continue\n";
602 -re "Continu\[^\r\n\]*\[\r\n\]" {}
608 if [target_info exists gdb,start_symbol] {
609 set start [target_info gdb,start_symbol];
614 # HACK: Should either use 000-jump or fix the target code
615 # to better handle RUN.
616 send_gdb "jump *$start\n"
617 warning "Using CLI jump command, expect run-to-main FAIL"
621 send_gdb "000-exec-run $args\n"
623 -re "000\\^running\r\n${mi_gdb_prompt}" {
626 perror "Unable to start target"
630 # NOTE: Shortly after this there will be a ``000*stopping,...(gdb)''
634 # Just like run-to-main but works with the MI interface
637 proc mi_run_to_main { } {
639 if { $suppress_flag } {
648 mi_delete_breakpoints
649 mi_gdb_reinitialize_dir $srcdir/$subdir
650 mi_gdb_load ${binfile}
656 # Just like gdb's "runto" proc, it will run the target to a given
657 # function. The big difference here between mi_runto and mi_execute_to
658 # is that mi_execute_to must have the inferior running already. This
659 # proc will (like gdb's runto) (re)start the inferior, too.
661 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
663 # -1 if test suppressed, failed, timedout
666 proc mi_runto {func} {
668 if { $suppress_flag } {
672 global mi_gdb_prompt expect_out
675 set test "mi runto $func"
676 mi_gdb_test "200-break-insert $func" \
677 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"keep\",enabled=\"y\",addr=\"$hex\",func=\"$func\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
678 "breakpoint at $func"
680 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
681 || ![scan $str {number="%d"} bkptno]} {
687 -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$" {
691 -re ".*$mi_gdb_prompt$" {
695 fail "$test (timeout)"
702 # Next to the next statement
703 # For return values, see mi_execute_to_helper
705 proc mi_next { test } {
706 return [mi_next_to {.*} {.*} {.*} {.*} $test]
710 # Step to the next statement
711 # For return values, see mi_execute_to_helper
713 proc mi_step { test } {
714 return [mi_step_to {.*} {.*} {.*} {.*} $test]
717 # cmd should not include the number or newline (i.e. "exec-step 3", not
718 # "220-exec-step 3\n"
720 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
721 # after the first prompt is printed.
723 proc mi_execute_to_helper { cmd reason func args file line extra test } {
725 if { $suppress_flag } {
731 send_gdb "220-$cmd\n"
733 -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$" {
737 -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$" {
738 fail "$test (stopped at wrong place)"
741 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
742 fail "$test (unknown output after running)"
746 fail "$test (timeout)"
752 proc mi_execute_to { cmd reason func args file line extra test } {
753 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
754 "$file" "$line" "$extra" "$test"
757 proc mi_next_to { func args file line test } {
758 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
759 "$file" "$line" "" "$test"
762 proc mi_step_to { func args file line test } {
763 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
764 "$file" "$line" "" "$test"
767 proc mi_finish_to { func args file line result ret test } {
768 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
770 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
774 proc mi_continue_to { bkptno func args file line test } {
775 mi_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
776 "$func" "$args" "$file" "$line" "" "$test"
779 proc mi0_execute_to { cmd reason func args file line extra test } {
780 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
781 "$file" "$line" "$extra" "$test"
784 proc mi0_next_to { func args file line test } {
785 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
786 "$file" "$line" "" "$test"
789 proc mi0_step_to { func args file line test } {
790 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
791 "$file" "$line" "" "$test"
794 proc mi0_finish_to { func args file line result ret test } {
795 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
797 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
801 proc mi0_continue_to { bkptno func args file line test } {
802 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
803 "$func" "$args" "$file" "$line" "" "$test"