* gdb.base/auxv.exp (core_works): Also check is_remote.
[deliverable/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
ef783a7d 328 set serialport_re [string_to_regexp $serialport]
da6012e5
DJ
329 for {set i 1} {$i <= 3} {incr i} {
330 send_gdb "47-target-select $targetname $serialport\n"
331 gdb_expect 60 {
332 -re "47\\^connected.*$mi_gdb_prompt$" {
333 verbose "Set target to $targetname";
334 return 0;
335 }
336 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
337 verbose "Connection failed";
338 }
339 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
340 verbose "Set target to $targetname";
341 return 0;
342 }
ef783a7d 343 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
da6012e5
DJ
344 verbose "Set target to $targetname";
345 return 0;
346 }
347 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
348 verbose "Set target to $targetname";
349 return 0;
350 }
351 -re "Connected to.*$mi_gdb_prompt$" {
352 verbose "Set target to $targetname";
353 return 0;
354 }
355 -re "Ending remote.*$mi_gdb_prompt$" { }
356 -re "Connection refused.*$mi_gdb_prompt$" {
357 verbose "Connection refused by remote target. Pausing, and trying again."
358 sleep 5
359 continue
360 }
361 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
362 verbose "Got timeout error from gdb.";
363 }
364 timeout {
365 send_gdb "\ 3";
366 break
367 }
368 }
369 }
370 return 1
371}
372
fb40c209 373#
da6012e5 374# load a file into the debugger (file command only).
fb40c209
AC
375# return a -1 if anything goes wrong.
376#
da6012e5 377proc mi_gdb_file_cmd { arg } {
fb40c209
AC
378 global verbose
379 global loadpath
380 global loadfile
381 global GDB
382 global mi_gdb_prompt
b741e217 383 global last_loaded_file
fb40c209
AC
384 upvar timeout timeout
385
b741e217 386 set last_loaded_file $arg
b53f9b27 387
da6012e5
DJ
388 if [is_remote host] {
389 set arg [remote_download host $arg];
390 if { $arg == "" } {
391 error "download failed"
392 return -1;
393 }
394 }
fb40c209 395
fb40c209
AC
396# FIXME: Several of these patterns are only acceptable for console
397# output. Queries are an error for mi.
398 send_gdb "105-file-exec-and-symbols $arg\n"
399 gdb_expect 120 {
400 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
401 verbose "\t\tLoaded $arg into the $GDB"
da6012e5 402 return 0
fb40c209
AC
403 }
404 -re "has no symbol-table.*$mi_gdb_prompt$" {
405 perror "$arg wasn't compiled with \"-g\""
406 return -1
407 }
fb40c209
AC
408 -re "Load new symbol table from \".*\".*y or n. $" {
409 send_gdb "y\n"
410 gdb_expect 120 {
411 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
412 verbose "\t\tLoaded $arg with new symbol table into $GDB"
413 # All OK
414 }
415 timeout {
416 perror "(timeout) Couldn't load $arg, other program already loaded."
417 return -1
418 }
419 }
420 }
421 -re "No such file or directory.*$mi_gdb_prompt$" {
422 perror "($arg) No such file or directory\n"
423 return -1
424 }
425 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
da6012e5
DJ
426 # We (MI) are just giving the prompt back for now, instead of giving
427 # some acknowledgement.
428 return 0
429 }
fb40c209
AC
430 timeout {
431 perror "couldn't load $arg into $GDB (timed out)."
432 return -1
433 }
da6012e5 434 eof {
fb40c209
AC
435 # This is an attempt to detect a core dump, but seems not to
436 # work. Perhaps we need to match .* followed by eof, in which
437 # gdb_expect does not seem to have a way to do that.
438 perror "couldn't load $arg into $GDB (end of file)."
439 return -1
440 }
441 }
da6012e5
DJ
442}
443
444#
b741e217 445# connect to the target and download a file, if necessary.
da6012e5
DJ
446# return a -1 if anything goes wrong.
447#
b741e217 448proc mi_gdb_target_load { } {
da6012e5
DJ
449 global verbose
450 global loadpath
451 global loadfile
452 global GDB
453 global mi_gdb_prompt
454 upvar timeout timeout
455
da6012e5 456 if { [info procs gdbserver_gdb_load] != "" } {
b741e217 457 set res [gdbserver_gdb_load]
da6012e5
DJ
458 set protocol [lindex $res 0]
459 set gdbport [lindex $res 1]
460
461 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
462 return -1
463 }
464 } elseif { [info procs send_target_sid] != "" } {
fb40c209
AC
465 # For SID, things get complex
466 send_target_sid
467 gdb_expect 60 {
2f168eed 468 -re "\\^done.*$mi_gdb_prompt$" {
fb40c209
AC
469 }
470 timeout {
471 perror "Unable to connect to SID target"
472 return -1
473 }
474 }
475 send_gdb "48-target-download\n"
476 gdb_expect 10 {
477 -re "48\\^done.*$mi_gdb_prompt$" {
478 }
479 timeout {
480 perror "Unable to download to SID target"
481 return -1
482 }
483 }
484 } elseif { [target_info protocol] == "sim" } {
485 # For the simulator, just connect to it directly.
486 send_gdb "47-target-select sim\n"
487 gdb_expect 10 {
488 -re "47\\^connected.*$mi_gdb_prompt$" {
489 }
490 timeout {
491 perror "Unable to select sim target"
492 return -1
493 }
494 }
495 send_gdb "48-target-download\n"
496 gdb_expect 10 {
497 -re "48\\^done.*$mi_gdb_prompt$" {
498 }
499 timeout {
500 perror "Unable to download to sim target"
501 return -1
502 }
503 }
b53f9b27
MS
504 } elseif { [target_info gdb_protocol] == "remote" } {
505 # remote targets
8e3049aa
PB
506 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
507 perror "Unable to connect to remote target"
508 return -1
b53f9b27
MS
509 }
510 send_gdb "48-target-download\n"
511 gdb_expect 10 {
512 -re "48\\^done.*$mi_gdb_prompt$" {
513 }
514 timeout {
515 perror "Unable to download to remote target"
516 return -1
517 }
518 }
fb40c209
AC
519 }
520 return 0
521}
522
b741e217
DJ
523#
524# load a file into the debugger.
525# return a -1 if anything goes wrong.
526#
527proc mi_gdb_load { arg } {
528 if { $arg != "" } {
529 return [mi_gdb_file_cmd $arg]
530 }
531 return 0
532}
533
ecd3fd0f
BR
534# mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
535# test the result.
fb40c209
AC
536#
537# COMMAND is the command to execute, send to GDB with send_gdb. If
538# this is the null string no command is sent.
539# PATTERN is the pattern to match for a PASS, and must NOT include
540# the \r\n sequence immediately before the gdb prompt.
f1ea48cb
BR
541# MESSAGE is the message to be printed. (If this is the empty string,
542# then sometimes we don't call pass or fail at all; I don't
543# understand this at all.)
ecd3fd0f
BR
544# IPATTERN is the pattern to match for the inferior's output. This parameter
545# is optional. If present, it will produce a PASS if the match is
546# successful, and a FAIL if unsuccessful.
fb40c209
AC
547#
548# Returns:
549# 1 if the test failed,
550# 0 if the test passes,
551# -1 if there was an internal error.
552#
553proc mi_gdb_test { args } {
554 global verbose
555 global mi_gdb_prompt
07c98896 556 global GDB expect_out
fb40c209
AC
557 upvar timeout timeout
558
fb40c209
AC
559 set command [lindex $args 0]
560 set pattern [lindex $args 1]
f1ea48cb 561 set message [lindex $args 2]
fb40c209 562
ecd3fd0f
BR
563 if [llength $args]==4 {
564 set ipattern [lindex $args 3]
565 }
566
fb40c209
AC
567 if [llength $args]==5 {
568 set question_string [lindex $args 3];
569 set response_string [lindex $args 4];
570 } else {
571 set question_string "^FOOBAR$"
572 }
573
574 if $verbose>2 then {
575 send_user "Sending \"$command\" to gdb\n"
576 send_user "Looking to match \"$pattern\"\n"
577 send_user "Message is \"$message\"\n"
578 }
579
580 set result -1
581 set string "${command}\n";
39fb8e9e
BR
582 set string_regex [string_to_regexp $command]
583
fb40c209
AC
584 if { $command != "" } {
585 while { "$string" != "" } {
586 set foo [string first "\n" "$string"];
587 set len [string length "$string"];
588 if { $foo < [expr $len - 1] } {
589 set str [string range "$string" 0 $foo];
590 if { [send_gdb "$str"] != "" } {
591 global suppress_flag;
592
593 if { ! $suppress_flag } {
594 perror "Couldn't send $command to GDB.";
595 }
596 fail "$message";
597 return $result;
598 }
599 gdb_expect 2 {
600 -re "\[\r\n\]" { }
601 timeout { }
602 }
603 set string [string range "$string" [expr $foo + 1] end];
604 } else {
605 break;
606 }
607 }
608 if { "$string" != "" } {
609 if { [send_gdb "$string"] != "" } {
610 global suppress_flag;
611
612 if { ! $suppress_flag } {
613 perror "Couldn't send $command to GDB.";
614 }
615 fail "$message";
616 return $result;
617 }
618 }
619 }
620
621 if [info exists timeout] {
622 set tmt $timeout;
623 } else {
624 global timeout;
625 if [info exists timeout] {
626 set tmt $timeout;
627 } else {
628 set tmt 60;
629 }
630 }
631 gdb_expect $tmt {
632 -re "\\*\\*\\* DOSEXIT code.*" {
633 if { $message != "" } {
634 fail "$message";
635 }
636 gdb_suppress_entire_file "GDB died";
637 return -1;
638 }
639 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
640 if ![isnative] then {
641 warning "Can`t communicate to remote target."
642 }
643 gdb_exit
644 gdb_start
645 set result -1
7ddebc7e 646 }
39fb8e9e
BR
647 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
648 # At this point, $expect_out(1,string) is the MI input command.
649 # and $expect_out(2,string) is the MI output command.
650 # If $expect_out(1,string) is "", then there was no MI input command here.
651
40e55bef
BR
652 # NOTE, there is no trailing anchor because with GDB/MI,
653 # asynchronous responses can happen at any point, causing more
654 # data to be available. Normally an anchor is used to make
655 # sure the end of the output is matched, however, $mi_gdb_prompt
656 # is just as good of an anchor since mi_gdb_test is meant to
657 # match a single mi output command. If a second GDB/MI output
658 # response is sent, it will be in the buffer for the next
659 # time mi_gdb_test is called.
7ddebc7e
KS
660 if ![string match "" $message] then {
661 pass "$message"
662 }
663 set result 0
fb40c209
AC
664 }
665 -re "(${question_string})$" {
666 send_gdb "$response_string\n";
667 exp_continue;
668 }
669 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
670 perror "Undefined command \"$command\"."
671 fail "$message"
672 set result 1
673 }
674 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
675 perror "\"$command\" is not a unique command name."
676 fail "$message"
677 set result 1
fb40c209
AC
678 }
679 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
680 if ![string match "" $message] then {
ed4c619a 681 set errmsg "$message (the program exited)"
fb40c209 682 } else {
ed4c619a 683 set errmsg "$command (the program exited)"
fb40c209
AC
684 }
685 fail "$errmsg"
686 return -1
687 }
688 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
689 if ![string match "" $message] then {
ed4c619a 690 set errmsg "$message (the program is no longer running)"
fb40c209 691 } else {
ed4c619a 692 set errmsg "$command (the program is no longer running)"
fb40c209
AC
693 }
694 fail "$errmsg"
695 return -1
696 }
697 -re ".*$mi_gdb_prompt\[ \]*$" {
698 if ![string match "" $message] then {
699 fail "$message"
700 }
701 set result 1
702 }
703 "<return>" {
704 send_gdb "\n"
705 perror "Window too small."
706 fail "$message"
707 }
708 -re "\\(y or n\\) " {
709 send_gdb "n\n"
710 perror "Got interactive prompt."
711 fail "$message"
712 }
713 eof {
714 perror "Process no longer exists"
715 if { $message != "" } {
716 fail "$message"
717 }
718 return -1
719 }
720 full_buffer {
721 perror "internal buffer is full."
722 fail "$message"
723 }
724 timeout {
725 if ![string match "" $message] then {
726 fail "$message (timeout)"
727 }
728 set result 1
729 }
730 }
ecd3fd0f
BR
731
732 # If the GDB output matched, compare the inferior output.
733 if { $result == 0 } {
734 if [ info exists ipattern ] {
735 global mi_inferior_spawn_id
736 expect {
737 -i $mi_inferior_spawn_id -re "$ipattern" {
738 pass "inferior_output:$message"
739 }
740 timeout {
741 fail "inferior output timeout"
742 set result 1
743 }
744 }
745 }
746 }
747
fb40c209
AC
748 return $result
749}
750
751#
752# MI run command. (A modified version of gdb_run_cmd)
753#
754
755# In patterns, the newline sequence ``\r\n'' is matched explicitly as
756# ``.*$'' could swallow up output that we attempt to match elsewhere.
757
758proc mi_run_cmd {args} {
759 global suppress_flag
760 if { $suppress_flag } {
761 return -1
762 }
763 global mi_gdb_prompt
764
765 if [target_info exists gdb_init_command] {
766 send_gdb "[target_info gdb_init_command]\n";
767 gdb_expect 30 {
768 -re "$mi_gdb_prompt$" { }
769 default {
770 perror "gdb_init_command for target failed";
771 return;
772 }
773 }
774 }
775
b741e217
DJ
776 if { [mi_gdb_target_load] < 0 } {
777 return
778 }
779
fb40c209
AC
780 if [target_info exists use_gdb_stub] {
781 if [target_info exists gdb,do_reload_on_run] {
fb40c209
AC
782 send_gdb "000-exec-continue\n";
783 gdb_expect 60 {
1df0c130 784 -re "000\\^running\[\r\n\]+$mi_gdb_prompt$" {}
fb40c209
AC
785 default {}
786 }
787 return;
788 }
6a90e1d0
AC
789
790 if [target_info exists gdb,start_symbol] {
791 set start [target_info gdb,start_symbol];
792 } else {
793 set start "start";
794 }
795
796 # HACK: Should either use 000-jump or fix the target code
797 # to better handle RUN.
798 send_gdb "jump *$start\n"
799 warning "Using CLI jump command, expect run-to-main FAIL"
800 return
fb40c209
AC
801 }
802
803 send_gdb "000-exec-run $args\n"
804 gdb_expect {
805 -re "000\\^running\r\n${mi_gdb_prompt}" {
806 }
807 timeout {
808 perror "Unable to start target"
809 return
810 }
811 }
2d0720d9 812 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
fb40c209
AC
813}
814
815#
816# Just like run-to-main but works with the MI interface
817#
818
819proc mi_run_to_main { } {
820 global suppress_flag
821 if { $suppress_flag } {
822 return -1
823 }
824
fb40c209
AC
825 global srcdir
826 global subdir
827 global binfile
828 global srcfile
829
fb40c209
AC
830 mi_delete_breakpoints
831 mi_gdb_reinitialize_dir $srcdir/$subdir
832 mi_gdb_load ${binfile}
833
08b468e0
KS
834 mi_runto main
835}
fb40c209 836
08b468e0
KS
837
838# Just like gdb's "runto" proc, it will run the target to a given
839# function. The big difference here between mi_runto and mi_execute_to
840# is that mi_execute_to must have the inferior running already. This
841# proc will (like gdb's runto) (re)start the inferior, too.
842#
843# FUNC is the linespec of the place to stop (it inserts a breakpoint here).
844# It returns:
845# -1 if test suppressed, failed, timedout
846# 0 if test passed
847
f7e97bb3 848proc mi_runto_helper {func run_or_continue} {
08b468e0
KS
849 global suppress_flag
850 if { $suppress_flag } {
851 return -1
852 }
853
854 global mi_gdb_prompt expect_out
76ff342d 855 global hex decimal fullname_syntax
08b468e0
KS
856
857 set test "mi runto $func"
038224f6
VP
858 mi_gdb_test "200-break-insert -t $func" \
859 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
08b468e0
KS
860 "breakpoint at $func"
861
862 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
863 || ![scan $str {number="%d"} bkptno]} {
864 set bkptno {[0-9]+}
865 }
866
f7e97bb3
VP
867 if {$run_or_continue == "run"} {
868 mi_run_cmd
869 } else {
74a44383
DJ
870 send_gdb "000-exec-continue\n"
871 gdb_expect {
872 -re "000\\^running\r\n${mi_gdb_prompt}" {
873 }
874 timeout {
875 fail "$test"
876 return -1
877 }
878 }
f7e97bb3 879 }
74a44383 880
08b468e0 881 gdb_expect {
038224f6 882 -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
883 pass "$test"
884 return 0
885 }
886 -re ".*$mi_gdb_prompt$" {
887 fail "$test (2)"
888 }
889 timeout {
890 fail "$test (timeout)"
891 return -1
892 }
893 }
fb40c209
AC
894}
895
f7e97bb3
VP
896proc mi_runto {func} {
897 mi_runto_helper $func "run"
898}
fb40c209
AC
899
900# Next to the next statement
08b468e0 901# For return values, see mi_execute_to_helper
fb40c209
AC
902
903proc mi_next { test } {
dc360f58 904 return [mi_next_to {.*} {.*} {.*} {.*} $test]
fb40c209
AC
905}
906
907
908# Step to the next statement
08b468e0 909# For return values, see mi_execute_to_helper
fb40c209
AC
910
911proc mi_step { test } {
dc360f58 912 return [mi_step_to {.*} {.*} {.*} {.*} $test]
fb40c209 913}
dcf95b47
DJ
914
915# cmd should not include the number or newline (i.e. "exec-step 3", not
916# "220-exec-step 3\n"
917
1902c51f
DJ
918# Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
919# after the first prompt is printed.
920
08b468e0 921proc mi_execute_to_helper { cmd reason func args file line extra test } {
dcf95b47
DJ
922 global suppress_flag
923 if { $suppress_flag } {
924 return -1
925 }
926 global mi_gdb_prompt
927 global hex
928 global decimal
76ff342d 929 global fullname_syntax
dcf95b47
DJ
930 send_gdb "220-$cmd\n"
931 gdb_expect {
39fb8e9e 932 -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
933 pass "$test"
934 return 0
935 }
39fb8e9e 936 -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
937 fail "$test (stopped at wrong place)"
938 return -1
939 }
940 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
941 fail "$test (unknown output after running)"
942 return -1
943 }
dcf95b47
DJ
944 timeout {
945 fail "$test (timeout)"
946 return -1
947 }
948 }
949}
950
08b468e0
KS
951proc mi_execute_to { cmd reason func args file line extra test } {
952 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
dcf95b47
DJ
953 "$file" "$line" "$extra" "$test"
954}
955
956proc mi_next_to { func args file line test } {
08b468e0 957 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
958 "$file" "$line" "" "$test"
959}
960
961proc mi_step_to { func args file line test } {
08b468e0 962 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
963 "$file" "$line" "" "$test"
964}
965
966proc mi_finish_to { func args file line result ret test } {
08b468e0 967 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
dcf95b47
DJ
968 "$file" "$line" \
969 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
970 "$test"
971}
972
f7e97bb3
VP
973proc mi_continue_to {func} {
974 mi_runto_helper $func "continue"
dcf95b47
DJ
975}
976
08b468e0
KS
977proc mi0_execute_to { cmd reason func args file line extra test } {
978 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
dcf95b47
DJ
979 "$file" "$line" "$extra" "$test"
980}
981
982proc mi0_next_to { func args file line test } {
08b468e0 983 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
984 "$file" "$line" "" "$test"
985}
986
987proc mi0_step_to { func args file line test } {
08b468e0 988 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
dcf95b47
DJ
989 "$file" "$line" "" "$test"
990}
991
992proc mi0_finish_to { func args file line result ret test } {
08b468e0 993 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
dcf95b47
DJ
994 "$file" "$line" \
995 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
996 "$test"
997}
998
999proc mi0_continue_to { bkptno func args file line test } {
08b468e0 1000 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
dcf95b47
DJ
1001 "$func" "$args" "$file" "$line" "" "$test"
1002}
b26ed50d
VP
1003
1004# Creates varobj named NAME for EXPRESSION.
1005# Name cannot be "-".
1006proc mi_create_varobj { name expression testname } {
1007 mi_gdb_test "-var-create $name * $expression" \
983c9c8f 1008 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
b26ed50d
VP
1009 $testname
1010}
1011
6e2a9270
VP
1012# Deletes the specified NAME.
1013proc mi_delete_varobj { name testname } {
1014 mi_gdb_test "-var-delete $name" \
1015 "\\^done,ndeleted=.*" \
1016 $testname
1017}
1018
b26ed50d
VP
1019# Updates varobj named NAME and checks that all varobjs in EXPECTED
1020# are reported as updated, and no other varobj is updated.
1021# Assumes that no varobj is out of scope and that no varobj changes
1022# types.
1023proc mi_varobj_update { name expected testname } {
1024 set er "\\^done,changelist=\\\["
1025 set first 1
1026 foreach item $expected {
1027 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
25d5ea92 1028 if {$first == 1} {
b26ed50d 1029 set er "$er$v"
25d5ea92 1030 set first 0
b26ed50d
VP
1031 } else {
1032 set er "$er,$v"
1033 }
1034 }
1035 set er "$er\\\]"
1036
1037 verbose -log "Expecting: $er" 2
1038 mi_gdb_test "-var-update $name" $er $testname
1039}
1040
1041proc mi_check_varobj_value { name value testname } {
1042
1043 mi_gdb_test "-var-evaluate-expression $name" \
1044 "\\^done,value=\"$value\"" \
1045 $testname
1046}
038224f6
VP
1047
1048# Check the results of the:
1049#
1050# -var-list-children VARNAME
1051#
1052# command. The CHILDREN parement should be a list of lists.
1053# Each inner list can have either 3 or 4 elements, describing
1054# fields that gdb is expected to report for child variable object,
1055# in the following order
1056#
1057# - Name
1058# - Expression
1059# - Number of children
1060# - Type
1061#
1062# If inner list has 3 elements, the gdb is expected to output no
1063# type for a child.
1064#
1065proc mi_list_varobj_children { varname children testname } {
1066
1067 set numchildren [llength $children]
1068 set children_exp {}
1069 set whatever "\"\[^\"\]+\""
1070
1071 foreach item $children {
1072
1073 set name [lindex $item 0]
1074 set exp [lindex $item 1]
1075 set numchild [lindex $item 2]
1076 if {[llength $item] == 4} {
1077 set type [lindex $item 3]
1078
1079 lappend children_exp\
1080 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"}"
1081 } else {
1082 lappend children_exp\
1083 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"}"
1084 }
1085 }
1086 set children_exp_j [join $children_exp ","]
1087 set expected "\\^done,numchild=\"$numchildren\",children=\\\[$children_exp_j\\\]"
1088
1089 verbose -log "Expecting: $expected"
1090
1091 mi_gdb_test "-var-list-children $varname" $expected $testname
1092}
2d0720d9
VP
1093
1094# A list of two-element lists. First element of each list is
1095# a Tcl statement, and the second element is the line
1096# number of source C file where the statement originates.
1097set mi_autotest_data ""
1098# The name of the source file for autotesting.
1099set mi_autotest_source ""
1100
1101proc count_newlines { string } {
1102 return [regexp -all "\n" $string]
1103}
1104
1105# Prepares for running inline tests in FILENAME.
1106# See comments for mi_run_inline_test for detailed
1107# explanation of the idea and syntax.
1108proc mi_prepare_inline_tests { filename } {
1109
1110 global srcdir
1111 global subdir
1112 global mi_autotest_source
1113 global mi_autotest_data
1114
1115 set mi_autotest_data {}
1116
1117 set mi_autotest_source $filename
1118
1119 if { ! [regexp "^/" "$filename"] } then {
1120 set filename "$srcdir/$subdir/$filename"
1121 }
1122
1123 set chan [open $filename]
1124 set content [read $chan]
1125 set line_number 1
1126 while {1} {
1127 set start [string first "/*:" $content]
1128 if {$start != -1} {
1129 set end [string first ":*/" $content]
1130 if {$end == -1} {
1131 error "Unterminated special comment in $filename"
1132 }
1133
1134 set prefix [string range $content 0 $start]
1135 set prefix_newlines [count_newlines $prefix]
1136
1137 set line_number [expr $line_number+$prefix_newlines]
1138 set comment_line $line_number
1139
1140 set comment [string range $content [expr $start+3] [expr $end-1]]
1141
1142 set comment_newlines [count_newlines $comment]
1143 set line_number [expr $line_number+$comment_newlines]
1144
1145 set comment [string trim $comment]
1146 set content [string range $content [expr $end+3] \
1147 [string length $content]]
1148 lappend mi_autotest_data [list $comment $comment_line]
1149 } else {
1150 break
1151 }
1152 }
1153 close $chan
1154}
1155
1156# Helper to mi_run_inline_test below.
1157# Return the list of all (statement,line_number) lists
1158# that comprise TESTCASE. The begin and end markers
1159# are not included.
1160proc mi_get_inline_test {testcase} {
1161
1162 global mi_gdb_prompt
1163 global mi_autotest_data
1164 global mi_autotest_source
1165
1166 set result {}
1167
1168 set seen_begin 0
1169 set seen_end 0
1170 foreach l $mi_autotest_data {
1171
1172 set comment [lindex $l 0]
1173
1174 if {$comment == "BEGIN: $testcase"} {
1175 set seen_begin 1
1176 } elseif {$comment == "END: $testcase"} {
1177 set seen_end 1
1178 break
1179 } elseif {$seen_begin==1} {
1180 lappend result $l
1181 }
1182 }
1183
1184 if {$seen_begin == 0} {
1185 error "Autotest $testcase not found"
1186 }
1187
1188 if {$seen_begin == 1 && $seen_end == 0} {
1189 error "Missing end marker for test $testcase"
1190 }
1191
1192 return $result
1193}
1194
1195# Sets temporary breakpoint at LOCATION.
1196proc mi_tbreak {location} {
1197
1198 global mi_gdb_prompt
1199
1200 mi_gdb_test "-break-insert -t $location" \
1201 {\^done,bkpt=.*} \
1202 "run to $location (set breakpoint)"
1203}
1204
1205# Send COMMAND that must be a command that resumes
1206# the inferiour (run/continue/next/etc) and consumes
1207# the "^running" output from it.
1208proc mi_send_resuming_command {command test} {
1209
1210 global mi_gdb_prompt
1211
1212 send_gdb "220-$command\n"
1213 gdb_expect {
1214 -re "220\\^running\r\n${mi_gdb_prompt}" {
1215 }
1216 timeout {
1217 fail $test
1218 }
1219 }
1220}
1221
1222# Helper to mi_run_inline_test below.
1223# Sets a temporary breakpoint at LOCATION and runs
1224# the program using COMMAND. When the program is stopped
1225# returns the line at which it. Returns -1 if line cannot
1226# be determined.
1227# Does not check that the line is the same as requested.
1228# The caller can check itself if required.
a73bafbc 1229proc mi_continue_to_line {location test} {
2d0720d9
VP
1230
1231 mi_tbreak $location
1232 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
a73bafbc 1233 return [mi_wait_for_stop $test]
2d0720d9
VP
1234}
1235
1236# Wait until gdb prints the current line.
1237proc mi_wait_for_stop {test} {
1238
1239 global mi_gdb_prompt
1240
1241 gdb_expect {
1242 -re ".*line=\"(.*)\".*\r\n$mi_gdb_prompt$" {
1243 return $expect_out(1,string)
1244 }
1245 -re ".*$mi_gdb_prompt$" {
1246 fail "wait for stop ($test)"
1247 }
1248 timeout {
1249 fail "wait for stop ($test)"
1250 }
1251 }
1252}
1253
1254# Run a MI test embedded in comments in a C file.
1255# The C file should contain special comments in the following
1256# three forms:
1257#
1258# /*: BEGIN: testname :*/
1259# /*: <Tcl statements> :*/
1260# /*: END: testname :*/
1261#
1262# This procedure find the begin and end marker for the requested
1263# test. Then, a temporary breakpoint is set at the begin
1264# marker and the program is run (from start).
1265#
1266# After that, for each special comment between the begin and end
1267# marker, the Tcl statements are executed. It is assumed that
1268# for each comment, the immediately preceding line is executable
1269# C statement. Then, gdb will be single-stepped until that
1270# preceding C statement is executed, and after that the
1271# Tcl statements in the comment will be executed.
1272#
1273# For example:
1274#
1275# /*: BEGIN: assignment-test :*/
1276# v = 10;
1277# /*: <Tcl code to check that 'v' is indeed 10 :*/
1278# /*: END: assignment-test :*/
1279#
1280# The mi_prepare_inline_tests function should be called before
1281# calling this function. A given C file can contain several
1282# inline tests. The names of the tests must be unique within one
1283# C file.
1284#
1285proc mi_run_inline_test { testcase } {
1286
1287 global mi_gdb_prompt
1288 global hex
1289 global decimal
1290 global fullname_syntax
1291 global mi_autotest_source
1292
1293 set commands [mi_get_inline_test $testcase]
1294
1295 set first 1
1296 set line_now 1
1297
1298 foreach c $commands {
1299 set statements [lindex $c 0]
1300 set line [lindex $c 1]
1301 set line [expr $line-1]
1302
1303 # We want gdb to be stopped at the expression immediately
1304 # before the comment. If this is the first comment, the
1305 # program is either not started yet or is in some random place,
1306 # so we run it. For further comments, we might be already
1307 # standing at the right line. If not continue till the
1308 # right line.
1309
1310 if {$first==1} {
1311 # Start the program afresh.
1312 mi_tbreak "$mi_autotest_source:$line"
1313 mi_run_cmd
1314 set line_now [mi_wait_for_stop "$testcase: step to $line"]
1315 set first 0
1316 } elseif {$line_now!=$line} {
25d5ea92 1317 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
2d0720d9
VP
1318 }
1319
1320 if {$line_now!=$line} {
1321 fail "$testcase: go to line $line"
1322 }
1323
1324 # We're not at the statement right above the comment.
1325 # Execute that statement so that the comment can test
1326 # the state after the statement is executed.
1327
1328 # Single-step past the line.
1329 mi_send_resuming_command "exec-next" "$testcase: step over $line"
1330 set line_now [mi_wait_for_stop "$testcase: step over $line"]
1331
1332 # We probably want to use 'uplevel' so that statements
1333 # have direct access to global variables that the
1334 # main 'exp' file has set up. But it's not yet clear,
1335 # will need more experience to be sure.
1336 eval $statements
1337 }
1338}
This page took 0.719989 seconds and 4 git commands to generate.