]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/lib/mi-support.exp
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / testsuite / lib / mi-support.exp
CommitLineData
6aba47ca
DJ
1# Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2007
2# Free Software Foundation, Inc.
fb40c209
AC
3
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.
8#
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.
13#
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.
17
18# Please email any bugs, comments, and/or additions to this file to:
19# bug-gdb@prep.ai.mit.edu
20
21# This file was based on a file written by Fred Fish. (fnf@cygnus.com)
22
23# Test setup routines that work with the MI interpreter.
24
25# The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
26# Set it if it is not already set.
27global mi_gdb_prompt
28if ![info exists mi_gdb_prompt] then {
29 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
30}
31
ecd3fd0f
BR
32global mi_inferior_spawn_id
33global mi_inferior_tty_name
34
fb40c209
AC
35set MIFLAGS "-i=mi"
36
37#
38# mi_gdb_exit -- exit the GDB, killing the target program if necessary
39#
40proc mi_gdb_exit {} {
41 catch mi_uncatched_gdb_exit
42}
43
44proc mi_uncatched_gdb_exit {} {
45 global GDB
46 global GDBFLAGS
47 global verbose
48 global gdb_spawn_id;
49 global gdb_prompt
50 global mi_gdb_prompt
51 global MIFLAGS
52
53 gdb_stop_suppressing_tests;
54
55 if { [info procs sid_exit] != "" } {
56 sid_exit
57 }
58
59 if ![info exists gdb_spawn_id] {
60 return;
61 }
62
63 verbose "Quitting $GDB $GDBFLAGS $MIFLAGS"
64
65 if { [is_remote host] && [board_info host exists fileid] } {
66 send_gdb "999-gdb-exit\n";
67 gdb_expect 10 {
68 -re "y or n" {
69 send_gdb "y\n";
70 exp_continue;
71 }
72 -re "Undefined command.*$gdb_prompt $" {
73 send_gdb "quit\n"
74 exp_continue;
75 }
76 -re "DOSEXIT code" { }
77 default { }
78 }
79 }
80
81 if ![is_remote host] {
82 remote_close host;
83 }
84 unset gdb_spawn_id
85}
86
87#
ecd3fd0f
BR
88# mi_gdb_start [INFERIOR_PTY] -- start gdb running, default procedure
89#
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.
fb40c209
AC
93#
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.
97#
ecd3fd0f 98proc mi_gdb_start { args } {
fb40c209
AC
99 global verbose
100 global GDB
101 global GDBFLAGS
102 global gdb_prompt
103 global mi_gdb_prompt
104 global timeout
105 global gdb_spawn_id;
106 global MIFLAGS
107
108 gdb_stop_suppressing_tests;
ecd3fd0f
BR
109 set inferior_pty no-tty
110
111 if { [llength $args] == 1} {
112 set inferior_pty [lindex $args 0]
113 }
114
115 set separate_inferior_pty [string match $inferior_pty separate-inferior-tty]
fb40c209 116
1759b3c3
AC
117 # Start SID.
118 if { [info procs sid_start] != "" } {
119 verbose "Spawning SID"
120 sid_start
121 }
122
fb40c209
AC
123 verbose "Spawning $GDB -nw $GDBFLAGS $MIFLAGS"
124
125 if [info exists gdb_spawn_id] {
126 return 0;
127 }
128
129 if ![is_remote host] {
130 if { [which $GDB] == 0 } then {
131 perror "$GDB does not exist."
132 exit 1
133 }
134 }
ecd3fd0f
BR
135
136 # Create the new PTY for the inferior process.
137 if { $separate_inferior_pty } {
138 spawn -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)
143 }
144
fb40c209
AC
145 set res [remote_spawn host "$GDB -nw $GDBFLAGS $MIFLAGS [host_info gdb_opts]"];
146 if { $res < 0 || $res == "" } {
147 perror "Spawning $GDB failed."
148 return 1;
149 }
150 gdb_expect {
1f312e79
JJ
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."
157 remote_close host;
158 return -1;
159 }
160 verbose "GDB initialized."
161 }
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."
168 remote_close host;
169 return -1;
170 }
d20bf2e8 171 verbose "GDB initialized."
fb40c209
AC
172 }
173 -re ".*$gdb_prompt $" {
174 untested "Skip mi tests (got non-mi prompt)."
175 remote_close host;
176 return -1;
177 }
76c520e0
AC
178 -re ".*unrecognized option.*for a complete list of options." {
179 untested "Skip mi tests (not compiled with mi support)."
180 remote_close host;
181 return -1;
182 }
7d76bd60
MK
183 -re ".*Interpreter `mi' unrecognized." {
184 untested "Skip mi tests (not compiled with mi support)."
185 remote_close host;
186 return -1;
187 }
fb40c209
AC
188 timeout {
189 perror "(timeout) GDB never initialized after 10 seconds."
190 remote_close host;
191 return -1
192 }
193 }
194 set gdb_spawn_id -1;
195
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"
199 gdb_expect 10 {
200 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
201 verbose "Setting height to 0." 2
202 }
203 timeout {
204 warning "Couldn't set the height to 0"
205 }
206 }
207 # force the width to "unlimited", so no wraparound occurs
208 send_gdb "101-gdb-set width 0\n"
209 gdb_expect 10 {
210 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
211 verbose "Setting width to 0." 2
212 }
213 timeout {
214 warning "Couldn't set the width to 0."
215 }
216 }
ecd3fd0f
BR
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"
221 gdb_expect 10 {
222 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
223 verbose "redirect inferior output to new terminal device."
224 }
225 timeout {
226 warning "Couldn't redirect inferior output." 2
227 }
228 }
229 }
fb40c209 230
fb40c209
AC
231 return 0;
232}
233
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.
238#
239
240proc mi_delete_breakpoints {} {
241 global mi_gdb_prompt
242
243# FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
244 send_gdb "102-break-delete\n"
245 gdb_expect 30 {
246 -re "Delete all breakpoints.*y or n.*$" {
247 send_gdb "y\n";
248 exp_continue
249 }
39fb8e9e 250 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
fb40c209
AC
251 # This happens if there were no breakpoints
252 }
f1c8a949 253 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
fb40c209
AC
254 }
255
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"
259 gdb_expect 30 {
260 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
cff22675 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=\\\[\\\]\}" {}
fb40c209
AC
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";
266 send_gdb "y\n";
267 exp_continue
268 }
269 timeout { perror "-break-list (timeout)" ; return }
270 }
271}
272
273proc mi_gdb_reinitialize_dir { subdir } {
274 global mi_gdb_prompt
da81390b 275 global MIFLAGS
fb40c209
AC
276
277 global suppress_flag
278 if { $suppress_flag } {
279 return
280 }
281
282 if [is_remote host] {
283 return "";
284 }
285
da81390b
JJ
286 if { $MIFLAGS == "-i=mi1" } {
287 send_gdb "104-environment-directory\n"
288 gdb_expect 60 {
fb40c209
AC
289 -re ".*Reinitialize source path to empty.*y or n. " {
290 warning "Got confirmation prompt for dir reinitialization."
291 send_gdb "y\n"
292 gdb_expect 60 {
293 -re "$mi_gdb_prompt$" {}
294 timeout {error "Dir reinitialization failed (timeout)"}
295 }
296 }
297 -re "$mi_gdb_prompt$" {}
298 timeout {error "Dir reinitialization failed (timeout)"}
da81390b
JJ
299 }
300 } else {
301 send_gdb "104-environment-directory -r\n"
302 gdb_expect 60 {
303 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
304 -re "$mi_gdb_prompt$" {}
305 timeout {error "Dir reinitialization failed (timeout)"}
306 }
fb40c209
AC
307 }
308
309 send_gdb "105-environment-directory $subdir\n"
310 gdb_expect 60 {
311 -re "Source directories searched.*$mi_gdb_prompt$" {
312 verbose "Dir set to $subdir"
313 }
da81390b 314 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
fb40c209
AC
315 # FIXME: We return just the prompt for now.
316 verbose "Dir set to $subdir"
317 # perror "Dir \"$subdir\" failed."
318 }
319 }
320}
321
da6012e5
DJ
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.
325proc mi_gdb_target_cmd { targetname serialport } {
326 global mi_gdb_prompt
327
328 for {set i 1} {$i <= 3} {incr i} {
329 send_gdb "47-target-select $targetname $serialport\n"
330 gdb_expect 60 {
331 -re "47\\^connected.*$mi_gdb_prompt$" {
332 verbose "Set target to $targetname";
333 return 0;
334 }
335 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
336 verbose "Connection failed";
337 }
338 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
339 verbose "Set target to $targetname";
340 return 0;
341 }
342 -re "Remote debugging using .*$serialport.*$mi_gdb_prompt$" {
343 verbose "Set target to $targetname";
344 return 0;
345 }
346 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
347 verbose "Set target to $targetname";
348 return 0;
349 }
350 -re "Connected to.*$mi_gdb_prompt$" {
351 verbose "Set target to $targetname";
352 return 0;
353 }
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."
357 sleep 5
358 continue
359 }
360 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
361 verbose "Got timeout error from gdb.";
362 }
363 timeout {
364 send_gdb "\ 3";
365 break
366 }
367 }
368 }
369 return 1
370}
371
fb40c209 372#
da6012e5 373# load a file into the debugger (file command only).
fb40c209
AC
374# return a -1 if anything goes wrong.
375#
da6012e5 376proc mi_gdb_file_cmd { arg } {
fb40c209
AC
377 global verbose
378 global loadpath
379 global loadfile
380 global GDB
381 global mi_gdb_prompt
b53f9b27 382 global last_mi_gdb_file
da6012e5 383 global last_mi_remote_file
fb40c209
AC
384 upvar timeout timeout
385
b53f9b27
MS
386 if { $arg == "" } {
387 set arg $last_mi_gdb_file;
da6012e5
DJ
388 } else {
389 set last_mi_gdb_file $arg
390 if { [ info exists last_mi_remote_file ] } {
391 unset last_mi_remote_file
392 }
b53f9b27
MS
393 }
394
da6012e5
DJ
395 if [is_remote host] {
396 set arg [remote_download host $arg];
397 if { $arg == "" } {
398 error "download failed"
399 return -1;
400 }
401 }
fb40c209 402
fb40c209
AC
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"
406 gdb_expect 120 {
407 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
408 verbose "\t\tLoaded $arg into the $GDB"
da6012e5 409 return 0
fb40c209
AC
410 }
411 -re "has no symbol-table.*$mi_gdb_prompt$" {
412 perror "$arg wasn't compiled with \"-g\""
413 return -1
414 }
fb40c209
AC
415 -re "Load new symbol table from \".*\".*y or n. $" {
416 send_gdb "y\n"
417 gdb_expect 120 {
418 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
419 verbose "\t\tLoaded $arg with new symbol table into $GDB"
420 # All OK
421 }
422 timeout {
423 perror "(timeout) Couldn't load $arg, other program already loaded."
424 return -1
425 }
426 }
427 }
428 -re "No such file or directory.*$mi_gdb_prompt$" {
429 perror "($arg) No such file or directory\n"
430 return -1
431 }
432 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
da6012e5
DJ
433 # We (MI) are just giving the prompt back for now, instead of giving
434 # some acknowledgement.
435 return 0
436 }
fb40c209
AC
437 timeout {
438 perror "couldn't load $arg into $GDB (timed out)."
439 return -1
440 }
da6012e5 441 eof {
fb40c209
AC
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)."
446 return -1
447 }
448 }
da6012e5
DJ
449}
450
451#
452# load a file into the debugger.
453# return a -1 if anything goes wrong.
454#
455proc mi_gdb_load { arg } {
456 global verbose
457 global loadpath
458 global loadfile
459 global GDB
460 global mi_gdb_prompt
461 upvar timeout timeout
462
463 # ``gdb_unload''
464 if { $arg != "" } {
465 mi_gdb_file_cmd $arg
466 }
467
fb40c209 468 # ``load''
da6012e5
DJ
469 if { [info procs gdbserver_gdb_load] != "" } {
470 global last_mi_gdb_file
471 global last_mi_remote_file
472
473 if { ! [info exists last_mi_remote_file] } {
474 if [is_remote target] {
7e813ac5 475 set last_mi_remote_file [remote_download target $arg /tmp/[file tail $arg].[pid]]
da6012e5
DJ
476 } else {
477 set last_mi_remote_file $last_mi_gdb_file
478 }
479 }
480
481 set res [gdbserver_gdb_load $last_mi_remote_file]
482 set protocol [lindex $res 0]
483 set gdbport [lindex $res 1]
484
485 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
486 return -1
487 }
488 } elseif { [info procs send_target_sid] != "" } {
fb40c209
AC
489 # For SID, things get complex
490 send_target_sid
491 gdb_expect 60 {
2f168eed 492 -re "\\^done.*$mi_gdb_prompt$" {
fb40c209
AC
493 }
494 timeout {
495 perror "Unable to connect to SID target"
496 return -1
497 }
498 }
499 send_gdb "48-target-download\n"
500 gdb_expect 10 {
501 -re "48\\^done.*$mi_gdb_prompt$" {
502 }
503 timeout {
504 perror "Unable to download to SID target"
505 return -1
506 }
507 }
508 } elseif { [target_info protocol] == "sim" } {
509 # For the simulator, just connect to it directly.
510 send_gdb "47-target-select sim\n"
511 gdb_expect 10 {
512 -re "47\\^connected.*$mi_gdb_prompt$" {
513 }
514 timeout {
515 perror "Unable to select sim target"
516 return -1
517 }
518 }
519 send_gdb "48-target-download\n"
520 gdb_expect 10 {
521 -re "48\\^done.*$mi_gdb_prompt$" {
522 }
523 timeout {
524 perror "Unable to download to sim target"
525 return -1
526 }
527 }
b53f9b27
MS
528 } elseif { [target_info gdb_protocol] == "remote" } {
529 # remote targets
8e3049aa
PB
530 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
531 perror "Unable to connect to remote target"
532 return -1
b53f9b27
MS
533 }
534 send_gdb "48-target-download\n"
535 gdb_expect 10 {
536 -re "48\\^done.*$mi_gdb_prompt$" {
537 }
538 timeout {
539 perror "Unable to download to remote target"
540 return -1
541 }
542 }
fb40c209
AC
543 }
544 return 0
545}
546
ecd3fd0f
BR
547# mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
548# test the result.
fb40c209
AC
549#
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.
f1ea48cb
BR
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.)
ecd3fd0f
BR
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.
fb40c209
AC
560#
561# Returns:
562# 1 if the test failed,
563# 0 if the test passes,
564# -1 if there was an internal error.
565#
566proc mi_gdb_test { args } {
567 global verbose
568 global mi_gdb_prompt
07c98896 569 global GDB expect_out
fb40c209
AC
570 upvar timeout timeout
571
fb40c209
AC
572 set command [lindex $args 0]
573 set pattern [lindex $args 1]
f1ea48cb 574 set message [lindex $args 2]
fb40c209 575
ecd3fd0f
BR
576 if [llength $args]==4 {
577 set ipattern [lindex $args 3]
578 }
579
fb40c209
AC
580 if [llength $args]==5 {
581 set question_string [lindex $args 3];
582 set response_string [lindex $args 4];
583 } else {
584 set question_string "^FOOBAR$"
585 }
586
587 if $verbose>2 then {
588 send_user "Sending \"$command\" to gdb\n"
589 send_user "Looking to match \"$pattern\"\n"
590 send_user "Message is \"$message\"\n"
591 }
592
593 set result -1
594 set string "${command}\n";
39fb8e9e
BR
595 set string_regex [string_to_regexp $command]
596
fb40c209
AC
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;
605
606 if { ! $suppress_flag } {
607 perror "Couldn't send $command to GDB.";
608 }
609 fail "$message";
610 return $result;
611 }
612 gdb_expect 2 {
613 -re "\[\r\n\]" { }
614 timeout { }
615 }
616 set string [string range "$string" [expr $foo + 1] end];
617 } else {
618 break;
619 }
620 }
621 if { "$string" != "" } {
622 if { [send_gdb "$string"] != "" } {
623 global suppress_flag;
624
625 if { ! $suppress_flag } {
626 perror "Couldn't send $command to GDB.";
627 }
628 fail "$message";
629 return $result;
630 }
631 }
632 }
633
634 if [info exists timeout] {
635 set tmt $timeout;
636 } else {
637 global timeout;
638 if [info exists timeout] {
639 set tmt $timeout;
640 } else {
641 set tmt 60;
642 }
643 }
644 gdb_expect $tmt {
645 -re "\\*\\*\\* DOSEXIT code.*" {
646 if { $message != "" } {
647 fail "$message";
648 }
649 gdb_suppress_entire_file "GDB died";
650 return -1;
651 }
652 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
653 if ![isnative] then {
654 warning "Can`t communicate to remote target."
655 }
656 gdb_exit
657 gdb_start
658 set result -1
7ddebc7e 659 }
39fb8e9e
BR
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.
664
40e55bef
BR
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.
7ddebc7e
KS
673 if ![string match "" $message] then {
674 pass "$message"
675 }
676 set result 0
fb40c209
AC
677 }
678 -re "(${question_string})$" {
679 send_gdb "$response_string\n";
680 exp_continue;
681 }
682 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
683 perror "Undefined command \"$command\"."
684 fail "$message"
685 set result 1
686 }
687 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
688 perror "\"$command\" is not a unique command name."
689 fail "$message"
690 set result 1
fb40c209
AC
691 }
692 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
693 if ![string match "" $message] then {
ed4c619a 694 set errmsg "$message (the program exited)"
fb40c209 695 } else {
ed4c619a 696 set errmsg "$command (the program exited)"
fb40c209
AC
697 }
698 fail "$errmsg"
699 return -1
700 }
701 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
702 if ![string match "" $message] then {
ed4c619a 703 set errmsg "$message (the program is no longer running)"
fb40c209 704 } else {
ed4c619a 705 set errmsg "$command (the program is no longer running)"
fb40c209
AC
706 }
707 fail "$errmsg"
708 return -1
709 }
710 -re ".*$mi_gdb_prompt\[ \]*$" {
711 if ![string match "" $message] then {
712 fail "$message"
713 }
714 set result 1
715 }
716 "<return>" {
717 send_gdb "\n"
718 perror "Window too small."
719 fail "$message"
720 }
721 -re "\\(y or n\\) " {
722 send_gdb "n\n"
723 perror "Got interactive prompt."
724 fail "$message"
725 }
726 eof {
727 perror "Process no longer exists"
728 if { $message != "" } {
729 fail "$message"
730 }
731 return -1
732 }
733 full_buffer {
734 perror "internal buffer is full."
735 fail "$message"
736 }
737 timeout {
738 if ![string match "" $message] then {
739 fail "$message (timeout)"
740 }
741 set result 1
742 }
743 }
ecd3fd0f
BR
744
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
749 expect {
750 -i $mi_inferior_spawn_id -re "$ipattern" {
751 pass "inferior_output:$message"
752 }
753 timeout {
754 fail "inferior output timeout"
755 set result 1
756 }
757 }
758 }
759 }
760
fb40c209
AC
761 return $result
762}
763
764#
765# MI run command. (A modified version of gdb_run_cmd)
766#
767
768# In patterns, the newline sequence ``\r\n'' is matched explicitly as
769# ``.*$'' could swallow up output that we attempt to match elsewhere.
770
771proc mi_run_cmd {args} {
772 global suppress_flag
773 if { $suppress_flag } {
774 return -1
775 }
776 global mi_gdb_prompt
777
778 if [target_info exists gdb_init_command] {
779 send_gdb "[target_info gdb_init_command]\n";
780 gdb_expect 30 {
781 -re "$mi_gdb_prompt$" { }
782 default {
783 perror "gdb_init_command for target failed";
784 return;
785 }
786 }
787 }
788
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 } {
794 return;
795 }
796 send_gdb "000-exec-continue\n";
797 gdb_expect 60 {
1df0c130 798 -re "000\\^running\[\r\n\]+$mi_gdb_prompt$" {}
fb40c209
AC
799 default {}
800 }
801 return;
802 }
6a90e1d0
AC
803
804 if [target_info exists gdb,start_symbol] {
805 set start [target_info gdb,start_symbol];
806 } else {
807 set start "start";
808 }
809
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"
814 return
fb40c209
AC
815 }
816
817 send_gdb "000-exec-run $args\n"
818 gdb_expect {
819 -re "000\\^running\r\n${mi_gdb_prompt}" {
820 }
821 timeout {
822 perror "Unable to start target"
823 return
824 }
825 }
2d0720d9 826 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
fb40c209
AC
827}
828
829#
830# Just like run-to-main but works with the MI interface
831#
832
833proc mi_run_to_main { } {
834 global suppress_flag
835 if { $suppress_flag } {
836 return -1
837 }
838
fb40c209
AC
839 global srcdir
840 global subdir
841 global binfile
842 global srcfile
843
fb40c209
AC
844 mi_delete_breakpoints
845 mi_gdb_reinitialize_dir $srcdir/$subdir
846 mi_gdb_load ${binfile}
847
08b468e0
KS
848 mi_runto main
849}
fb40c209 850
08b468e0
KS
851
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.
856#
857# FUNC is the linespec of the place to stop (it inserts a breakpoint here).
858# It returns:
859# -1 if test suppressed, failed, timedout
860# 0 if test passed
861
f7e97bb3 862proc mi_runto_helper {func run_or_continue} {
08b468e0
KS
863 global suppress_flag
864 if { $suppress_flag } {
865 return -1
866 }
867
868 global mi_gdb_prompt expect_out
76ff342d 869 global hex decimal fullname_syntax
08b468e0
KS
870
871 set test "mi runto $func"
038224f6
VP
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\"\}" \
08b468e0
KS
874 "breakpoint at $func"
875
876 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
877 || ![scan $str {number="%d"} bkptno]} {
878 set bkptno {[0-9]+}
879 }
880
f7e97bb3
VP
881 if {$run_or_continue == "run"} {
882 mi_run_cmd
883 } else {
74a44383
DJ
884 send_gdb "000-exec-continue\n"
885 gdb_expect {
886 -re "000\\^running\r\n${mi_gdb_prompt}" {
887 }
888 timeout {
889 fail "$test"
890 return -1
891 }
892 }
f7e97bb3 893 }
74a44383 894
08b468e0 895 gdb_expect {
038224f6 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$" {
08b468e0
KS
897 pass "$test"
898 return 0
899 }
900 -re ".*$mi_gdb_prompt$" {
901 fail "$test (2)"
902 }
903 timeout {
904 fail "$test (timeout)"
905 return -1
906 }
907 }
fb40c209
AC
908}
909
f7e97bb3
VP
910proc mi_runto {func} {
911 mi_runto_helper $func "run"
912}
fb40c209
AC
913
914# Next to the next statement
08b468e0 915# For return values, see mi_execute_to_helper
fb40c209
AC
916
917proc mi_next { test } {
dc360f58 918 return [mi_next_to {.*} {.*} {.*} {.*} $test]
fb40c209
AC
919}
920
921
922# Step to the next statement
08b468e0 923# For return values, see mi_execute_to_helper
fb40c209
AC
924
925proc mi_step { test } {
dc360f58 926 return [mi_step_to {.*} {.*} {.*} {.*} $test]
fb40c209 927}
dcf95b47
DJ
928
929# cmd should not include the number or newline (i.e. "exec-step 3", not
930# "220-exec-step 3\n"
931
1902c51f
DJ
932# Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
933# after the first prompt is printed.
934
08b468e0 935proc mi_execute_to_helper { cmd reason func args file line extra test } {
dcf95b47
DJ
936 global suppress_flag
937 if { $suppress_flag } {
938 return -1
939 }
940 global mi_gdb_prompt
941 global hex
942 global decimal
76ff342d 943 global fullname_syntax
dcf95b47
DJ
944 send_gdb "220-$cmd\n"
945 gdb_expect {
39fb8e9e 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$" {
dcf95b47
DJ
947 pass "$test"
948 return 0
949 }
39fb8e9e 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$" {
dcf95b47
DJ
951 fail "$test (stopped at wrong place)"
952 return -1
953 }
954 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
955 fail "$test (unknown output after running)"
956 return -1
957 }
dcf95b47
DJ
958 timeout {
959 fail "$test (timeout)"
960 return -1
961 }
962 }
963}
964
08b468e0
KS
965proc mi_execute_to { cmd reason func args file line extra test } {
966 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
dcf95b47
DJ
967 "$file" "$line" "$extra" "$test"
968}
969
970proc mi_next_to { func args file line test } {
08b468e0 971 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
972 "$file" "$line" "" "$test"
973}
974
975proc mi_step_to { func args file line test } {
08b468e0 976 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
977 "$file" "$line" "" "$test"
978}
979
980proc mi_finish_to { func args file line result ret test } {
08b468e0 981 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
dcf95b47
DJ
982 "$file" "$line" \
983 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
984 "$test"
985}
986
f7e97bb3
VP
987proc mi_continue_to {func} {
988 mi_runto_helper $func "continue"
dcf95b47
DJ
989}
990
08b468e0
KS
991proc mi0_execute_to { cmd reason func args file line extra test } {
992 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
dcf95b47
DJ
993 "$file" "$line" "$extra" "$test"
994}
995
996proc mi0_next_to { func args file line test } {
08b468e0 997 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
998 "$file" "$line" "" "$test"
999}
1000
1001proc mi0_step_to { func args file line test } {
08b468e0 1002 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
1003 "$file" "$line" "" "$test"
1004}
1005
1006proc mi0_finish_to { func args file line result ret test } {
08b468e0 1007 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
dcf95b47
DJ
1008 "$file" "$line" \
1009 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1010 "$test"
1011}
1012
1013proc mi0_continue_to { bkptno func args file line test } {
08b468e0 1014 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
dcf95b47
DJ
1015 "$func" "$args" "$file" "$line" "" "$test"
1016}
b26ed50d
VP
1017
1018# Creates varobj named NAME for EXPRESSION.
1019# Name cannot be "-".
1020proc mi_create_varobj { name expression testname } {
1021 mi_gdb_test "-var-create $name * $expression" \
1022 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",type=.*" \
1023 $testname
1024}
1025
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
1029# types.
1030proc mi_varobj_update { name expected testname } {
1031 set er "\\^done,changelist=\\\["
1032 set first 1
1033 foreach item $expected {
1034 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1035 if {$first} {
1036 set er "$er$v"
1037 } else {
1038 set er "$er,$v"
1039 }
1040 }
1041 set er "$er\\\]"
1042
1043 verbose -log "Expecting: $er" 2
1044 mi_gdb_test "-var-update $name" $er $testname
1045}
1046
1047proc mi_check_varobj_value { name value testname } {
1048
1049 mi_gdb_test "-var-evaluate-expression $name" \
1050 "\\^done,value=\"$value\"" \
1051 $testname
1052}
038224f6
VP
1053
1054# Check the results of the:
1055#
1056# -var-list-children VARNAME
1057#
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
1062#
1063# - Name
1064# - Expression
1065# - Number of children
1066# - Type
1067#
1068# If inner list has 3 elements, the gdb is expected to output no
1069# type for a child.
1070#
1071proc mi_list_varobj_children { varname children testname } {
1072
1073 set numchildren [llength $children]
1074 set children_exp {}
1075 set whatever "\"\[^\"\]+\""
1076
1077 foreach item $children {
1078
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]
1084
1085 lappend children_exp\
1086 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"}"
1087 } else {
1088 lappend children_exp\
1089 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"}"
1090 }
1091 }
1092 set children_exp_j [join $children_exp ","]
1093 set expected "\\^done,numchild=\"$numchildren\",children=\\\[$children_exp_j\\\]"
1094
1095 verbose -log "Expecting: $expected"
1096
1097 mi_gdb_test "-var-list-children $varname" $expected $testname
1098}
2d0720d9
VP
1099
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.
1103set mi_autotest_data ""
1104# The name of the source file for autotesting.
1105set mi_autotest_source ""
1106
1107proc count_newlines { string } {
1108 return [regexp -all "\n" $string]
1109}
1110
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.
1114proc mi_prepare_inline_tests { filename } {
1115
1116 global srcdir
1117 global subdir
1118 global mi_autotest_source
1119 global mi_autotest_data
1120
1121 set mi_autotest_data {}
1122
1123 set mi_autotest_source $filename
1124
1125 if { ! [regexp "^/" "$filename"] } then {
1126 set filename "$srcdir/$subdir/$filename"
1127 }
1128
1129 set chan [open $filename]
1130 set content [read $chan]
1131 set line_number 1
1132 while {1} {
1133 set start [string first "/*:" $content]
1134 if {$start != -1} {
1135 set end [string first ":*/" $content]
1136 if {$end == -1} {
1137 error "Unterminated special comment in $filename"
1138 }
1139
1140 set prefix [string range $content 0 $start]
1141 set prefix_newlines [count_newlines $prefix]
1142
1143 set line_number [expr $line_number+$prefix_newlines]
1144 set comment_line $line_number
1145
1146 set comment [string range $content [expr $start+3] [expr $end-1]]
1147
1148 set comment_newlines [count_newlines $comment]
1149 set line_number [expr $line_number+$comment_newlines]
1150
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]
1155 } else {
1156 break
1157 }
1158 }
1159 close $chan
1160}
1161
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
1165# are not included.
1166proc mi_get_inline_test {testcase} {
1167
1168 global mi_gdb_prompt
1169 global mi_autotest_data
1170 global mi_autotest_source
1171
1172 set result {}
1173
1174 set seen_begin 0
1175 set seen_end 0
1176 foreach l $mi_autotest_data {
1177
1178 set comment [lindex $l 0]
1179
1180 if {$comment == "BEGIN: $testcase"} {
1181 set seen_begin 1
1182 } elseif {$comment == "END: $testcase"} {
1183 set seen_end 1
1184 break
1185 } elseif {$seen_begin==1} {
1186 lappend result $l
1187 }
1188 }
1189
1190 if {$seen_begin == 0} {
1191 error "Autotest $testcase not found"
1192 }
1193
1194 if {$seen_begin == 1 && $seen_end == 0} {
1195 error "Missing end marker for test $testcase"
1196 }
1197
1198 return $result
1199}
1200
1201# Sets temporary breakpoint at LOCATION.
1202proc mi_tbreak {location} {
1203
1204 global mi_gdb_prompt
1205
1206 mi_gdb_test "-break-insert -t $location" \
1207 {\^done,bkpt=.*} \
1208 "run to $location (set breakpoint)"
1209}
1210
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.
1214proc mi_send_resuming_command {command test} {
1215
1216 global mi_gdb_prompt
1217
1218 send_gdb "220-$command\n"
1219 gdb_expect {
1220 -re "220\\^running\r\n${mi_gdb_prompt}" {
1221 }
1222 timeout {
1223 fail $test
1224 }
1225 }
1226}
1227
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
1232# be determined.
1233# Does not check that the line is the same as requested.
1234# The caller can check itself if required.
1235proc mi_continue_to_line {location command} {
1236
1237 mi_tbreak $location
1238 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1239 return [mi_wait_for_stop]
1240}
1241
1242# Wait until gdb prints the current line.
1243proc mi_wait_for_stop {test} {
1244
1245 global mi_gdb_prompt
1246
1247 gdb_expect {
1248 -re ".*line=\"(.*)\".*\r\n$mi_gdb_prompt$" {
1249 return $expect_out(1,string)
1250 }
1251 -re ".*$mi_gdb_prompt$" {
1252 fail "wait for stop ($test)"
1253 }
1254 timeout {
1255 fail "wait for stop ($test)"
1256 }
1257 }
1258}
1259
1260# Run a MI test embedded in comments in a C file.
1261# The C file should contain special comments in the following
1262# three forms:
1263#
1264# /*: BEGIN: testname :*/
1265# /*: <Tcl statements> :*/
1266# /*: END: testname :*/
1267#
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).
1271#
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.
1278#
1279# For example:
1280#
1281# /*: BEGIN: assignment-test :*/
1282# v = 10;
1283# /*: <Tcl code to check that 'v' is indeed 10 :*/
1284# /*: END: assignment-test :*/
1285#
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
1289# C file.
1290#
1291proc mi_run_inline_test { testcase } {
1292
1293 global mi_gdb_prompt
1294 global hex
1295 global decimal
1296 global fullname_syntax
1297 global mi_autotest_source
1298
1299 set commands [mi_get_inline_test $testcase]
1300
1301 set first 1
1302 set line_now 1
1303
1304 foreach c $commands {
1305 set statements [lindex $c 0]
1306 set line [lindex $c 1]
1307 set line [expr $line-1]
1308
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
1314 # right line.
1315
1316 if {$first==1} {
1317 # Start the program afresh.
1318 mi_tbreak "$mi_autotest_source:$line"
1319 mi_run_cmd
1320 set line_now [mi_wait_for_stop "$testcase: step to $line"]
1321 set first 0
1322 } elseif {$line_now!=$line} {
1323 set line_now [mi_continue_to_line "$mi_autotest_source:$line"]
1324 }
1325
1326 if {$line_now!=$line} {
1327 fail "$testcase: go to line $line"
1328 }
1329
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.
1333
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"]
1337
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.
1342 eval $statements
1343 }
1344}