]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/lib/mi-support.exp
2008-07-30 Paolo Bonzini <bonzini@gnu.org>
[thirdparty/binutils-gdb.git] / gdb / testsuite / lib / mi-support.exp
CommitLineData
9b254dd1 1# Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008
6aba47ca 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
e22f8b7c 6# the Free Software Foundation; either version 3 of the License, or
fb40c209 7# (at your option) any later version.
e22f8b7c 8#
fb40c209
AC
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.
e22f8b7c 13#
fb40c209 14# You should have received a copy of the GNU General Public License
e22f8b7c 15# along with this program. If not, see <http://www.gnu.org/licenses/>.
fb40c209
AC
16
17# Please email any bugs, comments, and/or additions to this file to:
18# bug-gdb@prep.ai.mit.edu
19
20# This file was based on a file written by Fred Fish. (fnf@cygnus.com)
21
22# Test setup routines that work with the MI interpreter.
23
24# The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
25# Set it if it is not already set.
26global mi_gdb_prompt
27if ![info exists mi_gdb_prompt] then {
28 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
29}
30
ecd3fd0f
BR
31global mi_inferior_spawn_id
32global mi_inferior_tty_name
33
fb40c209
AC
34set MIFLAGS "-i=mi"
35
36#
37# mi_gdb_exit -- exit the GDB, killing the target program if necessary
38#
39proc mi_gdb_exit {} {
40 catch mi_uncatched_gdb_exit
41}
42
43proc mi_uncatched_gdb_exit {} {
44 global GDB
45 global GDBFLAGS
46 global verbose
47 global gdb_spawn_id;
48 global gdb_prompt
49 global mi_gdb_prompt
50 global MIFLAGS
51
52 gdb_stop_suppressing_tests;
53
54 if { [info procs sid_exit] != "" } {
55 sid_exit
56 }
57
58 if ![info exists gdb_spawn_id] {
59 return;
60 }
61
62 verbose "Quitting $GDB $GDBFLAGS $MIFLAGS"
63
64 if { [is_remote host] && [board_info host exists fileid] } {
65 send_gdb "999-gdb-exit\n";
66 gdb_expect 10 {
67 -re "y or n" {
68 send_gdb "y\n";
69 exp_continue;
70 }
71 -re "Undefined command.*$gdb_prompt $" {
72 send_gdb "quit\n"
73 exp_continue;
74 }
75 -re "DOSEXIT code" { }
76 default { }
77 }
78 }
79
80 if ![is_remote host] {
81 remote_close host;
82 }
83 unset gdb_spawn_id
84}
85
86#
79732189 87# default_mi_gdb_start [INFERIOR_PTY] -- start gdb running, default procedure
ecd3fd0f
BR
88#
89# INFERIOR_PTY should be set to separate-inferior-tty to have the inferior work
90# with it's own PTY. If set to same-inferior-tty, the inferior shares GDB's PTY.
91# The default value is same-inferior-tty.
fb40c209
AC
92#
93# When running over NFS, particularly if running many simultaneous
94# tests on different hosts all using the same server, things can
95# get really slow. Give gdb at least 3 minutes to start up.
96#
79732189 97proc default_mi_gdb_start { args } {
fb40c209
AC
98 global verbose
99 global GDB
100 global GDBFLAGS
101 global gdb_prompt
102 global mi_gdb_prompt
103 global timeout
104 global gdb_spawn_id;
105 global MIFLAGS
106
107 gdb_stop_suppressing_tests;
ecd3fd0f
BR
108 set inferior_pty no-tty
109
110 if { [llength $args] == 1} {
111 set inferior_pty [lindex $args 0]
112 }
113
114 set separate_inferior_pty [string match $inferior_pty separate-inferior-tty]
fb40c209 115
1759b3c3
AC
116 # Start SID.
117 if { [info procs sid_start] != "" } {
118 verbose "Spawning SID"
119 sid_start
120 }
121
fb40c209
AC
122 verbose "Spawning $GDB -nw $GDBFLAGS $MIFLAGS"
123
124 if [info exists gdb_spawn_id] {
125 return 0;
126 }
127
128 if ![is_remote host] {
129 if { [which $GDB] == 0 } then {
130 perror "$GDB does not exist."
131 exit 1
132 }
133 }
ecd3fd0f
BR
134
135 # Create the new PTY for the inferior process.
136 if { $separate_inferior_pty } {
137 spawn -pty
138 global mi_inferior_spawn_id
139 global mi_inferior_tty_name
140 set mi_inferior_spawn_id $spawn_id
141 set mi_inferior_tty_name $spawn_out(slave,name)
142 }
143
fb40c209
AC
144 set res [remote_spawn host "$GDB -nw $GDBFLAGS $MIFLAGS [host_info gdb_opts]"];
145 if { $res < 0 || $res == "" } {
146 perror "Spawning $GDB failed."
147 return 1;
148 }
149 gdb_expect {
1f312e79
JJ
150 -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
151 # We have a new format mi startup prompt. If we are
152 # running mi1, then this is an error as we should be
153 # using the old-style prompt.
154 if { $MIFLAGS == "-i=mi1" } {
155 perror "(mi startup) Got unexpected new mi prompt."
156 remote_close host;
157 return -1;
158 }
159 verbose "GDB initialized."
160 }
161 -re "\[^~\].*$mi_gdb_prompt$" {
162 # We have an old format mi startup prompt. If we are
163 # not running mi1, then this is an error as we should be
164 # using the new-style prompt.
165 if { $MIFLAGS != "-i=mi1" } {
166 perror "(mi startup) Got unexpected old mi prompt."
167 remote_close host;
168 return -1;
169 }
d20bf2e8 170 verbose "GDB initialized."
fb40c209 171 }
76c520e0
AC
172 -re ".*unrecognized option.*for a complete list of options." {
173 untested "Skip mi tests (not compiled with mi support)."
174 remote_close host;
175 return -1;
176 }
7d76bd60
MK
177 -re ".*Interpreter `mi' unrecognized." {
178 untested "Skip mi tests (not compiled with mi support)."
179 remote_close host;
180 return -1;
181 }
fb40c209
AC
182 timeout {
183 perror "(timeout) GDB never initialized after 10 seconds."
184 remote_close host;
185 return -1
186 }
187 }
188 set gdb_spawn_id -1;
189
190 # FIXME: mi output does not go through pagers, so these can be removed.
191 # force the height to "unlimited", so no pagers get used
192 send_gdb "100-gdb-set height 0\n"
193 gdb_expect 10 {
194 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
195 verbose "Setting height to 0." 2
196 }
197 timeout {
198 warning "Couldn't set the height to 0"
199 }
200 }
201 # force the width to "unlimited", so no wraparound occurs
202 send_gdb "101-gdb-set width 0\n"
203 gdb_expect 10 {
204 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
205 verbose "Setting width to 0." 2
206 }
207 timeout {
208 warning "Couldn't set the width to 0."
209 }
210 }
ecd3fd0f
BR
211 # If allowing the inferior to have its own PTY then assign the inferior
212 # its own terminal device here.
213 if { $separate_inferior_pty } {
214 send_gdb "102-inferior-tty-set $mi_inferior_tty_name\n"
215 gdb_expect 10 {
216 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
217 verbose "redirect inferior output to new terminal device."
218 }
219 timeout {
220 warning "Couldn't redirect inferior output." 2
221 }
222 }
223 }
fb40c209 224
f7f9a841
VP
225 detect_async
226
fb40c209
AC
227 return 0;
228}
229
79732189
AR
230#
231# Overridable function. You can override this function in your
232# baseboard file.
233#
234proc mi_gdb_start { args } {
235 return [default_mi_gdb_start $args]
236}
237
fb40c209
AC
238# Many of the tests depend on setting breakpoints at various places and
239# running until that breakpoint is reached. At times, we want to start
240# with a clean-slate with respect to breakpoints, so this utility proc
241# lets us do this without duplicating this code everywhere.
242#
243
244proc mi_delete_breakpoints {} {
245 global mi_gdb_prompt
246
247# FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
248 send_gdb "102-break-delete\n"
249 gdb_expect 30 {
250 -re "Delete all breakpoints.*y or n.*$" {
251 send_gdb "y\n";
252 exp_continue
253 }
39fb8e9e 254 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
fb40c209
AC
255 # This happens if there were no breakpoints
256 }
f1c8a949 257 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
fb40c209
AC
258 }
259
260# The correct output is not "No breakpoints or watchpoints." but an
261# empty BreakpointTable. Also, a query is not acceptable with mi.
262 send_gdb "103-break-list\n"
263 gdb_expect 30 {
264 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
6f3f3097 265 -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=\\\[\\\]\}\r\n$mi_gdb_prompt$" {}
fb40c209
AC
266 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
267 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
268 -re "Delete all breakpoints.*or n.*$" {
269 warning "Unexpected prompt for breakpoints deletion";
270 send_gdb "y\n";
271 exp_continue
272 }
273 timeout { perror "-break-list (timeout)" ; return }
274 }
275}
276
277proc mi_gdb_reinitialize_dir { subdir } {
278 global mi_gdb_prompt
da81390b 279 global MIFLAGS
fb40c209
AC
280
281 global suppress_flag
282 if { $suppress_flag } {
283 return
284 }
285
286 if [is_remote host] {
287 return "";
288 }
289
da81390b
JJ
290 if { $MIFLAGS == "-i=mi1" } {
291 send_gdb "104-environment-directory\n"
292 gdb_expect 60 {
fb40c209
AC
293 -re ".*Reinitialize source path to empty.*y or n. " {
294 warning "Got confirmation prompt for dir reinitialization."
295 send_gdb "y\n"
296 gdb_expect 60 {
297 -re "$mi_gdb_prompt$" {}
298 timeout {error "Dir reinitialization failed (timeout)"}
299 }
300 }
301 -re "$mi_gdb_prompt$" {}
302 timeout {error "Dir reinitialization failed (timeout)"}
da81390b
JJ
303 }
304 } else {
305 send_gdb "104-environment-directory -r\n"
306 gdb_expect 60 {
307 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
308 -re "$mi_gdb_prompt$" {}
309 timeout {error "Dir reinitialization failed (timeout)"}
310 }
fb40c209
AC
311 }
312
313 send_gdb "105-environment-directory $subdir\n"
314 gdb_expect 60 {
315 -re "Source directories searched.*$mi_gdb_prompt$" {
316 verbose "Dir set to $subdir"
317 }
da81390b 318 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
fb40c209
AC
319 # FIXME: We return just the prompt for now.
320 verbose "Dir set to $subdir"
321 # perror "Dir \"$subdir\" failed."
322 }
323 }
324}
325
da6012e5
DJ
326# Send GDB the "target" command.
327# FIXME: Some of these patterns are not appropriate for MI. Based on
328# config/monitor.exp:gdb_target_command.
329proc mi_gdb_target_cmd { targetname serialport } {
330 global mi_gdb_prompt
331
ef783a7d 332 set serialport_re [string_to_regexp $serialport]
da6012e5
DJ
333 for {set i 1} {$i <= 3} {incr i} {
334 send_gdb "47-target-select $targetname $serialport\n"
335 gdb_expect 60 {
336 -re "47\\^connected.*$mi_gdb_prompt$" {
337 verbose "Set target to $targetname";
338 return 0;
339 }
401ea829
JB
340 -re "unknown host.*$mi_gdb_prompt" {
341 verbose "Couldn't look up $serialport"
342 }
da6012e5
DJ
343 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
344 verbose "Connection failed";
345 }
346 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
347 verbose "Set target to $targetname";
348 return 0;
349 }
ef783a7d 350 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
da6012e5
DJ
351 verbose "Set target to $targetname";
352 return 0;
353 }
354 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
355 verbose "Set target to $targetname";
356 return 0;
357 }
358 -re "Connected to.*$mi_gdb_prompt$" {
359 verbose "Set target to $targetname";
360 return 0;
361 }
362 -re "Ending remote.*$mi_gdb_prompt$" { }
363 -re "Connection refused.*$mi_gdb_prompt$" {
364 verbose "Connection refused by remote target. Pausing, and trying again."
365 sleep 5
366 continue
367 }
368 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
369 verbose "Got timeout error from gdb.";
370 }
371 timeout {
372 send_gdb "\ 3";
373 break
374 }
375 }
376 }
377 return 1
378}
379
fb40c209 380#
da6012e5 381# load a file into the debugger (file command only).
fb40c209
AC
382# return a -1 if anything goes wrong.
383#
da6012e5 384proc mi_gdb_file_cmd { arg } {
fb40c209
AC
385 global verbose
386 global loadpath
387 global loadfile
388 global GDB
389 global mi_gdb_prompt
b741e217 390 global last_loaded_file
fb40c209
AC
391 upvar timeout timeout
392
b741e217 393 set last_loaded_file $arg
b53f9b27 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#
b741e217 452# connect to the target and download a file, if necessary.
da6012e5
DJ
453# return a -1 if anything goes wrong.
454#
b741e217 455proc mi_gdb_target_load { } {
da6012e5
DJ
456 global verbose
457 global loadpath
458 global loadfile
459 global GDB
460 global mi_gdb_prompt
461 upvar timeout timeout
462
da6012e5 463 if { [info procs gdbserver_gdb_load] != "" } {
2226f861 464 mi_gdb_test "kill" ".*" ""
b741e217 465 set res [gdbserver_gdb_load]
da6012e5
DJ
466 set protocol [lindex $res 0]
467 set gdbport [lindex $res 1]
468
469 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
470 return -1
471 }
472 } elseif { [info procs send_target_sid] != "" } {
fb40c209 473 # For SID, things get complex
2b97317d
KB
474 send_gdb "kill\n"
475 gdb_expect 10 {
476 -re ".*$mi_gdb_prompt$"
477 }
fb40c209
AC
478 send_target_sid
479 gdb_expect 60 {
2f168eed 480 -re "\\^done.*$mi_gdb_prompt$" {
fb40c209
AC
481 }
482 timeout {
483 perror "Unable to connect to SID target"
484 return -1
485 }
486 }
487 send_gdb "48-target-download\n"
488 gdb_expect 10 {
489 -re "48\\^done.*$mi_gdb_prompt$" {
490 }
491 timeout {
492 perror "Unable to download to SID target"
493 return -1
494 }
495 }
496 } elseif { [target_info protocol] == "sim" } {
497 # For the simulator, just connect to it directly.
498 send_gdb "47-target-select sim\n"
499 gdb_expect 10 {
500 -re "47\\^connected.*$mi_gdb_prompt$" {
501 }
502 timeout {
503 perror "Unable to select sim target"
504 return -1
505 }
506 }
507 send_gdb "48-target-download\n"
508 gdb_expect 10 {
509 -re "48\\^done.*$mi_gdb_prompt$" {
510 }
511 timeout {
512 perror "Unable to download to sim target"
513 return -1
514 }
515 }
b53f9b27
MS
516 } elseif { [target_info gdb_protocol] == "remote" } {
517 # remote targets
8e3049aa
PB
518 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
519 perror "Unable to connect to remote target"
520 return -1
b53f9b27
MS
521 }
522 send_gdb "48-target-download\n"
523 gdb_expect 10 {
524 -re "48\\^done.*$mi_gdb_prompt$" {
525 }
526 timeout {
527 perror "Unable to download to remote target"
528 return -1
529 }
530 }
fb40c209
AC
531 }
532 return 0
533}
534
b741e217
DJ
535#
536# load a file into the debugger.
537# return a -1 if anything goes wrong.
538#
539proc mi_gdb_load { arg } {
540 if { $arg != "" } {
541 return [mi_gdb_file_cmd $arg]
542 }
543 return 0
544}
545
ecd3fd0f
BR
546# mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
547# test the result.
fb40c209
AC
548#
549# COMMAND is the command to execute, send to GDB with send_gdb. If
550# this is the null string no command is sent.
551# PATTERN is the pattern to match for a PASS, and must NOT include
552# the \r\n sequence immediately before the gdb prompt.
f1ea48cb
BR
553# MESSAGE is the message to be printed. (If this is the empty string,
554# then sometimes we don't call pass or fail at all; I don't
555# understand this at all.)
ecd3fd0f
BR
556# IPATTERN is the pattern to match for the inferior's output. This parameter
557# is optional. If present, it will produce a PASS if the match is
558# successful, and a FAIL if unsuccessful.
fb40c209
AC
559#
560# Returns:
561# 1 if the test failed,
562# 0 if the test passes,
563# -1 if there was an internal error.
564#
565proc mi_gdb_test { args } {
566 global verbose
567 global mi_gdb_prompt
07c98896 568 global GDB expect_out
fb40c209
AC
569 upvar timeout timeout
570
fb40c209
AC
571 set command [lindex $args 0]
572 set pattern [lindex $args 1]
f1ea48cb 573 set message [lindex $args 2]
fb40c209 574
ecd3fd0f
BR
575 if [llength $args]==4 {
576 set ipattern [lindex $args 3]
577 }
578
fb40c209
AC
579 if [llength $args]==5 {
580 set question_string [lindex $args 3];
581 set response_string [lindex $args 4];
582 } else {
583 set question_string "^FOOBAR$"
584 }
585
586 if $verbose>2 then {
587 send_user "Sending \"$command\" to gdb\n"
588 send_user "Looking to match \"$pattern\"\n"
589 send_user "Message is \"$message\"\n"
590 }
591
592 set result -1
593 set string "${command}\n";
39fb8e9e
BR
594 set string_regex [string_to_regexp $command]
595
fb40c209
AC
596 if { $command != "" } {
597 while { "$string" != "" } {
598 set foo [string first "\n" "$string"];
599 set len [string length "$string"];
600 if { $foo < [expr $len - 1] } {
601 set str [string range "$string" 0 $foo];
602 if { [send_gdb "$str"] != "" } {
603 global suppress_flag;
604
605 if { ! $suppress_flag } {
606 perror "Couldn't send $command to GDB.";
607 }
608 fail "$message";
609 return $result;
610 }
611 gdb_expect 2 {
612 -re "\[\r\n\]" { }
613 timeout { }
614 }
615 set string [string range "$string" [expr $foo + 1] end];
616 } else {
617 break;
618 }
619 }
620 if { "$string" != "" } {
621 if { [send_gdb "$string"] != "" } {
622 global suppress_flag;
623
624 if { ! $suppress_flag } {
625 perror "Couldn't send $command to GDB.";
626 }
627 fail "$message";
628 return $result;
629 }
630 }
631 }
632
633 if [info exists timeout] {
634 set tmt $timeout;
635 } else {
636 global timeout;
637 if [info exists timeout] {
638 set tmt $timeout;
639 } else {
640 set tmt 60;
641 }
642 }
643 gdb_expect $tmt {
644 -re "\\*\\*\\* DOSEXIT code.*" {
645 if { $message != "" } {
646 fail "$message";
647 }
648 gdb_suppress_entire_file "GDB died";
649 return -1;
650 }
651 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
652 if ![isnative] then {
653 warning "Can`t communicate to remote target."
654 }
655 gdb_exit
656 gdb_start
657 set result -1
7ddebc7e 658 }
39fb8e9e
BR
659 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
660 # At this point, $expect_out(1,string) is the MI input command.
661 # and $expect_out(2,string) is the MI output command.
662 # If $expect_out(1,string) is "", then there was no MI input command here.
663
40e55bef
BR
664 # NOTE, there is no trailing anchor because with GDB/MI,
665 # asynchronous responses can happen at any point, causing more
666 # data to be available. Normally an anchor is used to make
667 # sure the end of the output is matched, however, $mi_gdb_prompt
668 # is just as good of an anchor since mi_gdb_test is meant to
669 # match a single mi output command. If a second GDB/MI output
670 # response is sent, it will be in the buffer for the next
671 # time mi_gdb_test is called.
7ddebc7e
KS
672 if ![string match "" $message] then {
673 pass "$message"
674 }
675 set result 0
fb40c209
AC
676 }
677 -re "(${question_string})$" {
678 send_gdb "$response_string\n";
679 exp_continue;
680 }
681 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
682 perror "Undefined command \"$command\"."
683 fail "$message"
684 set result 1
685 }
686 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
687 perror "\"$command\" is not a unique command name."
688 fail "$message"
689 set result 1
fb40c209
AC
690 }
691 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
692 if ![string match "" $message] then {
ed4c619a 693 set errmsg "$message (the program exited)"
fb40c209 694 } else {
ed4c619a 695 set errmsg "$command (the program exited)"
fb40c209
AC
696 }
697 fail "$errmsg"
698 return -1
699 }
700 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
701 if ![string match "" $message] then {
ed4c619a 702 set errmsg "$message (the program is no longer running)"
fb40c209 703 } else {
ed4c619a 704 set errmsg "$command (the program is no longer running)"
fb40c209
AC
705 }
706 fail "$errmsg"
707 return -1
708 }
709 -re ".*$mi_gdb_prompt\[ \]*$" {
710 if ![string match "" $message] then {
711 fail "$message"
712 }
713 set result 1
714 }
715 "<return>" {
716 send_gdb "\n"
717 perror "Window too small."
718 fail "$message"
719 }
720 -re "\\(y or n\\) " {
721 send_gdb "n\n"
722 perror "Got interactive prompt."
723 fail "$message"
724 }
725 eof {
726 perror "Process no longer exists"
727 if { $message != "" } {
728 fail "$message"
729 }
730 return -1
731 }
732 full_buffer {
733 perror "internal buffer is full."
734 fail "$message"
735 }
736 timeout {
737 if ![string match "" $message] then {
738 fail "$message (timeout)"
739 }
740 set result 1
741 }
742 }
ecd3fd0f
BR
743
744 # If the GDB output matched, compare the inferior output.
745 if { $result == 0 } {
746 if [ info exists ipattern ] {
d084b331
DJ
747 if { ![target_info exists gdb,noinferiorio] } {
748 global mi_inferior_spawn_id
749 expect {
750 -i $mi_inferior_spawn_id -re "$ipattern" {
751 pass "$message inferior output"
752 }
753 timeout {
754 fail "$message inferior output (timeout)"
755 set result 1
756 }
ecd3fd0f 757 }
d084b331
DJ
758 } else {
759 unsupported "$message inferior output"
ecd3fd0f
BR
760 }
761 }
762 }
763
fb40c209
AC
764 return $result
765}
766
767#
768# MI run command. (A modified version of gdb_run_cmd)
769#
770
771# In patterns, the newline sequence ``\r\n'' is matched explicitly as
772# ``.*$'' could swallow up output that we attempt to match elsewhere.
773
774proc mi_run_cmd {args} {
775 global suppress_flag
776 if { $suppress_flag } {
777 return -1
778 }
779 global mi_gdb_prompt
780
781 if [target_info exists gdb_init_command] {
782 send_gdb "[target_info gdb_init_command]\n";
783 gdb_expect 30 {
784 -re "$mi_gdb_prompt$" { }
785 default {
786 perror "gdb_init_command for target failed";
787 return;
788 }
789 }
790 }
791
b741e217
DJ
792 if { [mi_gdb_target_load] < 0 } {
793 return
794 }
795
fb40c209
AC
796 if [target_info exists use_gdb_stub] {
797 if [target_info exists gdb,do_reload_on_run] {
bb378428 798 send_gdb "220-exec-continue\n";
fb40c209 799 gdb_expect 60 {
a2840c35 800 -re "220\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\r\n$mi_gdb_prompt$" {}
fb40c209
AC
801 default {}
802 }
803 return;
804 }
6a90e1d0
AC
805
806 if [target_info exists gdb,start_symbol] {
807 set start [target_info gdb,start_symbol];
808 } else {
809 set start "start";
810 }
811
812 # HACK: Should either use 000-jump or fix the target code
813 # to better handle RUN.
814 send_gdb "jump *$start\n"
815 warning "Using CLI jump command, expect run-to-main FAIL"
816 return
fb40c209
AC
817 }
818
bb378428 819 send_gdb "220-exec-run $args\n"
fb40c209 820 gdb_expect {
e1ac3328 821 -re "220\\^running\r\n(\\*running,thread-id=\"\[^\"\]+\"\r\n|=thread-created,id=\"1\"\r\n)*${mi_gdb_prompt}" {
fb40c209
AC
822 }
823 timeout {
824 perror "Unable to start target"
825 return
826 }
827 }
2d0720d9 828 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
fb40c209
AC
829}
830
831#
832# Just like run-to-main but works with the MI interface
833#
834
835proc mi_run_to_main { } {
836 global suppress_flag
837 if { $suppress_flag } {
838 return -1
839 }
840
fb40c209
AC
841 global srcdir
842 global subdir
843 global binfile
844 global srcfile
845
fb40c209
AC
846 mi_delete_breakpoints
847 mi_gdb_reinitialize_dir $srcdir/$subdir
848 mi_gdb_load ${binfile}
849
08b468e0
KS
850 mi_runto main
851}
fb40c209 852
08b468e0
KS
853
854# Just like gdb's "runto" proc, it will run the target to a given
855# function. The big difference here between mi_runto and mi_execute_to
856# is that mi_execute_to must have the inferior running already. This
857# proc will (like gdb's runto) (re)start the inferior, too.
858#
859# FUNC is the linespec of the place to stop (it inserts a breakpoint here).
860# It returns:
861# -1 if test suppressed, failed, timedout
862# 0 if test passed
863
f7e97bb3 864proc mi_runto_helper {func run_or_continue} {
08b468e0
KS
865 global suppress_flag
866 if { $suppress_flag } {
867 return -1
868 }
869
870 global mi_gdb_prompt expect_out
76ff342d 871 global hex decimal fullname_syntax
08b468e0
KS
872
873 set test "mi runto $func"
038224f6 874 mi_gdb_test "200-break-insert -t $func" \
d24317b4 875 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\",original-location=\".*\"\}" \
08b468e0
KS
876 "breakpoint at $func"
877
878 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
879 || ![scan $str {number="%d"} bkptno]} {
880 set bkptno {[0-9]+}
881 }
882
f7e97bb3
VP
883 if {$run_or_continue == "run"} {
884 mi_run_cmd
885 } else {
bb378428 886 mi_send_resuming_command "exec-continue" "$test"
f7e97bb3 887 }
74a44383 888
18ac113b 889 mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test
fb40c209
AC
890}
891
f7e97bb3
VP
892proc mi_runto {func} {
893 mi_runto_helper $func "run"
894}
fb40c209
AC
895
896# Next to the next statement
08b468e0 897# For return values, see mi_execute_to_helper
fb40c209
AC
898
899proc mi_next { test } {
dc360f58 900 return [mi_next_to {.*} {.*} {.*} {.*} $test]
fb40c209
AC
901}
902
903
904# Step to the next statement
08b468e0 905# For return values, see mi_execute_to_helper
fb40c209
AC
906
907proc mi_step { test } {
dc360f58 908 return [mi_step_to {.*} {.*} {.*} {.*} $test]
fb40c209 909}
dcf95b47 910
f7f9a841
VP
911set async "unknown"
912
913proc detect_async {} {
914 global async
915 global mi_gdb_prompt
916
a2840c35 917 send_gdb "maint show linux-async\n"
f7f9a841 918
a2840c35
VP
919 gdb_expect {
920 -re ".*Controlling the GNU/Linux inferior in asynchronous mode is on...*$mi_gdb_prompt$" {
921 set async 1
922 }
923 -re ".*$mi_gdb_prompt$" {
924 set async 0
925 }
926 timeout {
927 set async 0
f7f9a841
VP
928 }
929 }
930 return $async
931}
932
bb378428
VP
933# Wait for MI *stopped notification to appear.
934# The REASON, FUNC, ARGS, FILE and LINE are regular expressions
935# to match against whatever is output in *stopped. ARGS should
936# not include [] the list of argument is enclosed in, and other
937# regular expressions should not include quotes.
938# If EXTRA is a list of one element, it's the regular expression
939# for output expected right after *stopped, and before GDB prompt.
940# If EXTRA is a list of two elements, the first element is for
941# output right after *stopped, and the second element is output
942# right after reason field. The regex after reason should not include
943# the comma separating it from the following fields.
944#
945# When we fail to match output at all, -1 is returned. Otherwise,
946# the line at which we stop is returned. This is useful when exact
947# line is not possible to specify for some reason -- one can pass
948# the .* regexp for line, and then check the line programmatically.
949proc mi_expect_stop { reason func args file line extra test } {
1902c51f 950
dcf95b47
DJ
951 global mi_gdb_prompt
952 global hex
953 global decimal
76ff342d 954 global fullname_syntax
f7f9a841 955 global async
bb378428
VP
956
957 set after_stopped ""
958 set after_reason ""
959 if { [llength $extra] == 2 } {
960 set after_stopped [lindex $extra 0]
961 set after_reason [lindex $extra 1]
962 set after_reason "${after_reason},"
963 } elseif { [llength $extra] == 1 } {
964 set after_stopped [lindex $extra 0]
965 }
966
f7f9a841
VP
967 if {$async} {
968 set prompt_re ""
969 } else {
970 set prompt_re "$mi_gdb_prompt"
971 }
972
973 if { $reason == "really-no-reason" } {
974 gdb_expect {
975 -re "\\*stopped\r\n$prompt_re$" {
976 pass "$test"
977 }
978 timeout {
979 fail "$test (unknown output after running)"
980 }
981 }
982 return
983 }
984
bb378428
VP
985 if { $reason == "exited-normally" } {
986
987 gdb_expect {
f7f9a841 988 -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re$" {
bb378428
VP
989 pass "$test"
990 }
991 -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
992 timeout {
993 fail "$test (unknown output after running)"
994 }
995 }
996 return
997 }
998
999 set args "\\\[$args\\\]"
1000
1001 set bn ""
1002 if { $reason == "breakpoint-hit" } {
1003 set bn {bkptno="[0-9]+",}
1004 }
1005
1006 set r ""
1007 if { $reason != "" } {
1008 set r "reason=\"$reason\","
1009 }
1010
18ac113b
AR
1011
1012 set a $after_reason
1013
b1a268e5 1014 verbose -log "mi_expect_stop: expecting: \\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=.*,frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\".*$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\"\}$after_stopped\r\n$prompt_re$"
dcf95b47 1015 gdb_expect {
b1a268e5 1016 -re "\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=.*,frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\".*$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\"\}$after_stopped\r\n$prompt_re$" {
dcf95b47 1017 pass "$test"
bb378428 1018 return $expect_out(2,string)
dcf95b47 1019 }
b1a268e5 1020 -re "\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=.*,frame=\{addr=\"$hex\",func=\".*\",args=\[\\\[\{\].*\[\\\]\}\],file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}.*\r\n$prompt_re$" {
dcf95b47
DJ
1021 fail "$test (stopped at wrong place)"
1022 return -1
1023 }
f7f9a841 1024 -re ".*\r\n$mi_gdb_prompt$" {
dcf95b47
DJ
1025 fail "$test (unknown output after running)"
1026 return -1
1027 }
dcf95b47
DJ
1028 timeout {
1029 fail "$test (timeout)"
1030 return -1
1031 }
bb378428 1032 }
dcf95b47
DJ
1033}
1034
bb378428
VP
1035# cmd should not include the number or newline (i.e. "exec-step 3", not
1036# "220-exec-step 3\n"
1037
1038# Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1039# after the first prompt is printed.
1040
08b468e0 1041proc mi_execute_to { cmd reason func args file line extra test } {
bb378428
VP
1042 global suppress_flag
1043 if { $suppress_flag } {
1044 return -1
1045 }
1046
1047 mi_send_resuming_command "$cmd" "$test"
1048 set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1049 return $r
dcf95b47
DJ
1050}
1051
1052proc mi_next_to { func args file line test } {
08b468e0 1053 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
1054 "$file" "$line" "" "$test"
1055}
1056
1057proc mi_step_to { func args file line test } {
08b468e0 1058 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
1059 "$file" "$line" "" "$test"
1060}
1061
1062proc mi_finish_to { func args file line result ret test } {
08b468e0 1063 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
dcf95b47
DJ
1064 "$file" "$line" \
1065 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1066 "$test"
1067}
1068
f7e97bb3
VP
1069proc mi_continue_to {func} {
1070 mi_runto_helper $func "continue"
dcf95b47
DJ
1071}
1072
08b468e0
KS
1073proc mi0_execute_to { cmd reason func args file line extra test } {
1074 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
dcf95b47
DJ
1075 "$file" "$line" "$extra" "$test"
1076}
1077
1078proc mi0_next_to { func args file line test } {
08b468e0 1079 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
1080 "$file" "$line" "" "$test"
1081}
1082
1083proc mi0_step_to { func args file line test } {
08b468e0 1084 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
1085 "$file" "$line" "" "$test"
1086}
1087
1088proc mi0_finish_to { func args file line result ret test } {
08b468e0 1089 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
dcf95b47
DJ
1090 "$file" "$line" \
1091 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1092 "$test"
1093}
1094
1095proc mi0_continue_to { bkptno func args file line test } {
08b468e0 1096 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
dcf95b47
DJ
1097 "$func" "$args" "$file" "$line" "" "$test"
1098}
b26ed50d 1099
d24317b4
VP
1100# Creates a breakpoint and checks the reported fields are as expected
1101proc mi_create_breakpoint { location number disp func file line address test } {
1102 verbose -log "Expecting: 222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}"
1103 mi_gdb_test "222-break-insert $location" \
1104 "222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}" \
1105 $test
1106}
1107
1108proc mi_list_breakpoints { expected test } {
1109 set fullname ".*"
1110
1111 set body ""
1112 set first 1
1113
1114 foreach item $children {
1115 if {$first == 0} {
1116 set body "$body,"
1117 }
1118 set number disp func file line address
1119 set number [lindex $item 0]
1120 set disp [lindex $item 1]
1121 set func [lindex $item 2]
1122 set line [lindex $item 3]
1123 set address [lindex $item 4]
1124 set body "$body,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",${fullname},line=\"$line\",times=\"0\",original-location=\".*\"\}"
1125 set first 0
1126 }
1127
1128 verbose -log "Expecint: 666\\\^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=\\\[$body\\\]\}" \
1129 mi_gdb_test "666-break-list" \
1130 "666\\\^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=\\\[$body\\\]\}" \
1131 $test
1132}
1133
b26ed50d
VP
1134# Creates varobj named NAME for EXPRESSION.
1135# Name cannot be "-".
1136proc mi_create_varobj { name expression testname } {
1137 mi_gdb_test "-var-create $name * $expression" \
983c9c8f 1138 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
b26ed50d
VP
1139 $testname
1140}
1141
fcacd99f
VP
1142proc mi_create_floating_varobj { name expression testname } {
1143 mi_gdb_test "-var-create $name @ $expression" \
1144 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1145 $testname
1146}
1147
1148
9e8e3afe
VP
1149# Same as mi_create_varobj, but also checks the reported type
1150# of the varobj.
1151proc mi_create_varobj_checked { name expression type testname } {
1152 mi_gdb_test "-var-create $name * $expression" \
1153 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1154 $testname
1155}
1156
6e2a9270
VP
1157# Deletes the specified NAME.
1158proc mi_delete_varobj { name testname } {
1159 mi_gdb_test "-var-delete $name" \
1160 "\\^done,ndeleted=.*" \
1161 $testname
1162}
1163
b26ed50d
VP
1164# Updates varobj named NAME and checks that all varobjs in EXPECTED
1165# are reported as updated, and no other varobj is updated.
1166# Assumes that no varobj is out of scope and that no varobj changes
1167# types.
1168proc mi_varobj_update { name expected testname } {
1169 set er "\\^done,changelist=\\\["
1170 set first 1
1171 foreach item $expected {
1172 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
25d5ea92 1173 if {$first == 1} {
b26ed50d 1174 set er "$er$v"
25d5ea92 1175 set first 0
b26ed50d
VP
1176 } else {
1177 set er "$er,$v"
1178 }
1179 }
1180 set er "$er\\\]"
1181
1182 verbose -log "Expecting: $er" 2
1183 mi_gdb_test "-var-update $name" $er $testname
1184}
1185
fcacd99f
VP
1186proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1187 set v "{name=\"$name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\"}"
1188 set er "\\^done,changelist=\\\[$v\\\]"
1189 verbose -log "Expecting: $er"
1190 mi_gdb_test "-var-update $name" $er $testname
1191}
1192
b26ed50d
VP
1193proc mi_check_varobj_value { name value testname } {
1194
1195 mi_gdb_test "-var-evaluate-expression $name" \
1196 "\\^done,value=\"$value\"" \
1197 $testname
1198}
038224f6
VP
1199
1200# Check the results of the:
1201#
1202# -var-list-children VARNAME
1203#
1204# command. The CHILDREN parement should be a list of lists.
1205# Each inner list can have either 3 or 4 elements, describing
1206# fields that gdb is expected to report for child variable object,
1207# in the following order
1208#
1209# - Name
1210# - Expression
1211# - Number of children
1212# - Type
1213#
1214# If inner list has 3 elements, the gdb is expected to output no
9e8e3afe
VP
1215# type for a child and no value.
1216#
1217# If the inner list has 4 elements, gdb output is expected to
1218# have no value.
038224f6
VP
1219#
1220proc mi_list_varobj_children { varname children testname } {
1221
9e8e3afe
VP
1222 set options ""
1223 if {[llength $varname] == 2} {
1224 set options [lindex $varname 1]
1225 set varname [lindex $varname 0]
1226 }
1227
038224f6
VP
1228 set numchildren [llength $children]
1229 set children_exp {}
1230 set whatever "\"\[^\"\]+\""
1231
1232 foreach item $children {
1233
1234 set name [lindex $item 0]
1235 set exp [lindex $item 1]
1236 set numchild [lindex $item 2]
9e8e3afe 1237 if {[llength $item] == 5} {
038224f6 1238 set type [lindex $item 3]
9e8e3afe 1239 set value [lindex $item 4]
038224f6
VP
1240
1241 lappend children_exp\
9e8e3afe
VP
1242 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
1243 } elseif {[llength $item] == 4} {
1244 set type [lindex $item 3]
1245
1246 lappend children_exp\
1247 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
038224f6
VP
1248 } else {
1249 lappend children_exp\
9e8e3afe 1250 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
038224f6
VP
1251 }
1252 }
1253 set children_exp_j [join $children_exp ","]
9e8e3afe
VP
1254 if {$numchildren} {
1255 set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1256 } {
1257 set expected "\\^done,numchild=\"0\""
1258 }
038224f6
VP
1259
1260 verbose -log "Expecting: $expected"
1261
9e8e3afe
VP
1262 mi_gdb_test "-var-list-children $options $varname" $expected $testname
1263}
1264
1265# Verifies that variable object VARNAME has NUMBER children,
1266# where each one is named $VARNAME.<index-of-child> and has type TYPE.
1267proc mi_list_array_varobj_children { varname number type testname } {
1268 set t {}
1269 for {set i 0} {$i < $number} {incr i} {
1270 lappend t [list $varname.$i $i 0 $type]
1271 }
1272 mi_list_varobj_children $varname $t $testname
038224f6 1273}
2d0720d9
VP
1274
1275# A list of two-element lists. First element of each list is
1276# a Tcl statement, and the second element is the line
1277# number of source C file where the statement originates.
1278set mi_autotest_data ""
1279# The name of the source file for autotesting.
1280set mi_autotest_source ""
1281
1282proc count_newlines { string } {
1283 return [regexp -all "\n" $string]
1284}
1285
1286# Prepares for running inline tests in FILENAME.
1287# See comments for mi_run_inline_test for detailed
1288# explanation of the idea and syntax.
1289proc mi_prepare_inline_tests { filename } {
1290
1291 global srcdir
1292 global subdir
1293 global mi_autotest_source
1294 global mi_autotest_data
1295
1296 set mi_autotest_data {}
1297
1298 set mi_autotest_source $filename
1299
1300 if { ! [regexp "^/" "$filename"] } then {
1301 set filename "$srcdir/$subdir/$filename"
1302 }
1303
1304 set chan [open $filename]
1305 set content [read $chan]
1306 set line_number 1
1307 while {1} {
1308 set start [string first "/*:" $content]
1309 if {$start != -1} {
1310 set end [string first ":*/" $content]
1311 if {$end == -1} {
1312 error "Unterminated special comment in $filename"
1313 }
1314
1315 set prefix [string range $content 0 $start]
1316 set prefix_newlines [count_newlines $prefix]
1317
1318 set line_number [expr $line_number+$prefix_newlines]
1319 set comment_line $line_number
1320
1321 set comment [string range $content [expr $start+3] [expr $end-1]]
1322
1323 set comment_newlines [count_newlines $comment]
1324 set line_number [expr $line_number+$comment_newlines]
1325
1326 set comment [string trim $comment]
1327 set content [string range $content [expr $end+3] \
1328 [string length $content]]
1329 lappend mi_autotest_data [list $comment $comment_line]
1330 } else {
1331 break
1332 }
1333 }
1334 close $chan
1335}
1336
1337# Helper to mi_run_inline_test below.
1338# Return the list of all (statement,line_number) lists
1339# that comprise TESTCASE. The begin and end markers
1340# are not included.
1341proc mi_get_inline_test {testcase} {
1342
1343 global mi_gdb_prompt
1344 global mi_autotest_data
1345 global mi_autotest_source
1346
1347 set result {}
1348
1349 set seen_begin 0
1350 set seen_end 0
1351 foreach l $mi_autotest_data {
1352
1353 set comment [lindex $l 0]
1354
1355 if {$comment == "BEGIN: $testcase"} {
1356 set seen_begin 1
1357 } elseif {$comment == "END: $testcase"} {
1358 set seen_end 1
1359 break
1360 } elseif {$seen_begin==1} {
1361 lappend result $l
1362 }
1363 }
1364
1365 if {$seen_begin == 0} {
1366 error "Autotest $testcase not found"
1367 }
1368
1369 if {$seen_begin == 1 && $seen_end == 0} {
1370 error "Missing end marker for test $testcase"
1371 }
1372
1373 return $result
1374}
1375
1376# Sets temporary breakpoint at LOCATION.
1377proc mi_tbreak {location} {
1378
1379 global mi_gdb_prompt
1380
1381 mi_gdb_test "-break-insert -t $location" \
1382 {\^done,bkpt=.*} \
1383 "run to $location (set breakpoint)"
1384}
1385
1386# Send COMMAND that must be a command that resumes
1387# the inferiour (run/continue/next/etc) and consumes
1388# the "^running" output from it.
a2840c35 1389proc mi_send_resuming_command_raw {command test} {
2d0720d9
VP
1390
1391 global mi_gdb_prompt
1392
a2840c35 1393 send_gdb "$command\n"
2d0720d9 1394 gdb_expect {
a2840c35 1395 -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n${mi_gdb_prompt}" {
7ebd49dc
VP
1396 # Note that lack of 'pass' call here -- this works around limitation
1397 # in DejaGNU xfail mechanism. mi-until.exp has this:
1398 #
1399 # setup_kfail gdb/2104 "*-*-*"
1400 # mi_execute_to ...
1401 #
1402 # and mi_execute_to uses mi_send_resuming_command. If we use 'pass' here,
1403 # it will reset kfail, so when the actual test fails, it will be flagged
1404 # as real failure.
2d0720d9 1405 }
bb378428
VP
1406 -re ".*${mi_gdb_prompt}" {
1407 fail "$test (failed to resume)"
1408 }
a2840c35 1409 -re "\\^error,msg=.*" {
bb378428
VP
1410 fail "$test (MI error)"
1411 return -1
1412 }
2d0720d9 1413 timeout {
bb378428
VP
1414 fail "$test"
1415 return -1
2d0720d9
VP
1416 }
1417 }
1418}
1419
a2840c35
VP
1420proc mi_send_resuming_command {command test} {
1421 mi_send_resuming_command_raw -$command $test
1422}
1423
2d0720d9
VP
1424# Helper to mi_run_inline_test below.
1425# Sets a temporary breakpoint at LOCATION and runs
1426# the program using COMMAND. When the program is stopped
1427# returns the line at which it. Returns -1 if line cannot
1428# be determined.
1429# Does not check that the line is the same as requested.
1430# The caller can check itself if required.
a73bafbc 1431proc mi_continue_to_line {location test} {
2d0720d9
VP
1432
1433 mi_tbreak $location
1434 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
bb378428 1435 return [mi_get_stop_line $test]
2d0720d9
VP
1436}
1437
1438# Wait until gdb prints the current line.
bb378428 1439proc mi_get_stop_line {test} {
2d0720d9
VP
1440
1441 global mi_gdb_prompt
f7f9a841
VP
1442 global async
1443
1444 if {$async} {
1445 set prompt_re ""
1446 } else {
1447 set prompt_re "$mi_gdb_prompt"
1448 }
2d0720d9
VP
1449
1450 gdb_expect {
f7f9a841 1451 -re ".*line=\"(.*)\".*\r\n$prompt_re$" {
2d0720d9
VP
1452 return $expect_out(1,string)
1453 }
1454 -re ".*$mi_gdb_prompt$" {
1455 fail "wait for stop ($test)"
1456 }
1457 timeout {
1458 fail "wait for stop ($test)"
1459 }
1460 }
1461}
1462
1463# Run a MI test embedded in comments in a C file.
1464# The C file should contain special comments in the following
1465# three forms:
1466#
1467# /*: BEGIN: testname :*/
1468# /*: <Tcl statements> :*/
1469# /*: END: testname :*/
1470#
1471# This procedure find the begin and end marker for the requested
1472# test. Then, a temporary breakpoint is set at the begin
1473# marker and the program is run (from start).
1474#
1475# After that, for each special comment between the begin and end
1476# marker, the Tcl statements are executed. It is assumed that
1477# for each comment, the immediately preceding line is executable
1478# C statement. Then, gdb will be single-stepped until that
1479# preceding C statement is executed, and after that the
1480# Tcl statements in the comment will be executed.
1481#
1482# For example:
1483#
1484# /*: BEGIN: assignment-test :*/
1485# v = 10;
1486# /*: <Tcl code to check that 'v' is indeed 10 :*/
1487# /*: END: assignment-test :*/
1488#
1489# The mi_prepare_inline_tests function should be called before
1490# calling this function. A given C file can contain several
1491# inline tests. The names of the tests must be unique within one
1492# C file.
1493#
1494proc mi_run_inline_test { testcase } {
1495
1496 global mi_gdb_prompt
1497 global hex
1498 global decimal
1499 global fullname_syntax
1500 global mi_autotest_source
1501
1502 set commands [mi_get_inline_test $testcase]
1503
1504 set first 1
1505 set line_now 1
1506
1507 foreach c $commands {
1508 set statements [lindex $c 0]
1509 set line [lindex $c 1]
1510 set line [expr $line-1]
1511
1512 # We want gdb to be stopped at the expression immediately
1513 # before the comment. If this is the first comment, the
1514 # program is either not started yet or is in some random place,
1515 # so we run it. For further comments, we might be already
1516 # standing at the right line. If not continue till the
1517 # right line.
1518
1519 if {$first==1} {
1520 # Start the program afresh.
1521 mi_tbreak "$mi_autotest_source:$line"
1522 mi_run_cmd
bb378428 1523 set line_now [mi_get_stop_line "$testcase: step to $line"]
2d0720d9
VP
1524 set first 0
1525 } elseif {$line_now!=$line} {
25d5ea92 1526 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
2d0720d9
VP
1527 }
1528
1529 if {$line_now!=$line} {
1530 fail "$testcase: go to line $line"
1531 }
1532
1533 # We're not at the statement right above the comment.
1534 # Execute that statement so that the comment can test
1535 # the state after the statement is executed.
1536
1537 # Single-step past the line.
1538 mi_send_resuming_command "exec-next" "$testcase: step over $line"
bb378428 1539 set line_now [mi_get_stop_line "$testcase: step over $line"]
2d0720d9
VP
1540
1541 # We probably want to use 'uplevel' so that statements
1542 # have direct access to global variables that the
1543 # main 'exp' file has set up. But it's not yet clear,
1544 # will need more experience to be sure.
1545 eval $statements
1546 }
1547}