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