* varobj.c (varobj_list_children): Stop if the number of children is
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / mi-support.exp
1 # Copyright 1999, 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 # 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 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 return 0;
231 }
232
233 # Many of the tests depend on setting breakpoints at various places and
234 # running until that breakpoint is reached. At times, we want to start
235 # with a clean-slate with respect to breakpoints, so this utility proc
236 # lets us do this without duplicating this code everywhere.
237 #
238
239 proc mi_delete_breakpoints {} {
240 global mi_gdb_prompt
241
242 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
243 send_gdb "102-break-delete\n"
244 gdb_expect 30 {
245 -re "Delete all breakpoints.*y or n.*$" {
246 send_gdb "y\n";
247 exp_continue
248 }
249 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
250 # This happens if there were no breakpoints
251 }
252 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
253 }
254
255 # The correct output is not "No breakpoints or watchpoints." but an
256 # empty BreakpointTable. Also, a query is not acceptable with mi.
257 send_gdb "103-break-list\n"
258 gdb_expect 30 {
259 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
260 -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=\\\[\\\]\}" {}
261 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
262 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
263 -re "Delete all breakpoints.*or n.*$" {
264 warning "Unexpected prompt for breakpoints deletion";
265 send_gdb "y\n";
266 exp_continue
267 }
268 timeout { perror "-break-list (timeout)" ; return }
269 }
270 }
271
272 proc mi_gdb_reinitialize_dir { subdir } {
273 global mi_gdb_prompt
274 global MIFLAGS
275
276 global suppress_flag
277 if { $suppress_flag } {
278 return
279 }
280
281 if [is_remote host] {
282 return "";
283 }
284
285 if { $MIFLAGS == "-i=mi1" } {
286 send_gdb "104-environment-directory\n"
287 gdb_expect 60 {
288 -re ".*Reinitialize source path to empty.*y or n. " {
289 warning "Got confirmation prompt for dir reinitialization."
290 send_gdb "y\n"
291 gdb_expect 60 {
292 -re "$mi_gdb_prompt$" {}
293 timeout {error "Dir reinitialization failed (timeout)"}
294 }
295 }
296 -re "$mi_gdb_prompt$" {}
297 timeout {error "Dir reinitialization failed (timeout)"}
298 }
299 } else {
300 send_gdb "104-environment-directory -r\n"
301 gdb_expect 60 {
302 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
303 -re "$mi_gdb_prompt$" {}
304 timeout {error "Dir reinitialization failed (timeout)"}
305 }
306 }
307
308 send_gdb "105-environment-directory $subdir\n"
309 gdb_expect 60 {
310 -re "Source directories searched.*$mi_gdb_prompt$" {
311 verbose "Dir set to $subdir"
312 }
313 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
314 # FIXME: We return just the prompt for now.
315 verbose "Dir set to $subdir"
316 # perror "Dir \"$subdir\" failed."
317 }
318 }
319 }
320
321 # Send GDB the "target" command.
322 # FIXME: Some of these patterns are not appropriate for MI. Based on
323 # config/monitor.exp:gdb_target_command.
324 proc mi_gdb_target_cmd { targetname serialport } {
325 global mi_gdb_prompt
326
327 for {set i 1} {$i <= 3} {incr i} {
328 send_gdb "47-target-select $targetname $serialport\n"
329 gdb_expect 60 {
330 -re "47\\^connected.*$mi_gdb_prompt$" {
331 verbose "Set target to $targetname";
332 return 0;
333 }
334 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
335 verbose "Connection failed";
336 }
337 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
338 verbose "Set target to $targetname";
339 return 0;
340 }
341 -re "Remote debugging using .*$serialport.*$mi_gdb_prompt$" {
342 verbose "Set target to $targetname";
343 return 0;
344 }
345 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
346 verbose "Set target to $targetname";
347 return 0;
348 }
349 -re "Connected to.*$mi_gdb_prompt$" {
350 verbose "Set target to $targetname";
351 return 0;
352 }
353 -re "Ending remote.*$mi_gdb_prompt$" { }
354 -re "Connection refused.*$mi_gdb_prompt$" {
355 verbose "Connection refused by remote target. Pausing, and trying again."
356 sleep 5
357 continue
358 }
359 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
360 verbose "Got timeout error from gdb.";
361 }
362 timeout {
363 send_gdb "\ 3";
364 break
365 }
366 }
367 }
368 return 1
369 }
370
371 #
372 # load a file into the debugger (file command only).
373 # return a -1 if anything goes wrong.
374 #
375 proc mi_gdb_file_cmd { arg } {
376 global verbose
377 global loadpath
378 global loadfile
379 global GDB
380 global mi_gdb_prompt
381 global last_mi_gdb_file
382 global last_mi_remote_file
383 upvar timeout timeout
384
385 if { $arg == "" } {
386 set arg $last_mi_gdb_file;
387 } else {
388 set last_mi_gdb_file $arg
389 if { [ info exists last_mi_remote_file ] } {
390 unset last_mi_remote_file
391 }
392 }
393
394 if [is_remote host] {
395 set arg [remote_download host $arg];
396 if { $arg == "" } {
397 error "download failed"
398 return -1;
399 }
400 }
401
402 # FIXME: Several of these patterns are only acceptable for console
403 # output. Queries are an error for mi.
404 send_gdb "105-file-exec-and-symbols $arg\n"
405 gdb_expect 120 {
406 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
407 verbose "\t\tLoaded $arg into the $GDB"
408 return 0
409 }
410 -re "has no symbol-table.*$mi_gdb_prompt$" {
411 perror "$arg wasn't compiled with \"-g\""
412 return -1
413 }
414 -re "Load new symbol table from \".*\".*y or n. $" {
415 send_gdb "y\n"
416 gdb_expect 120 {
417 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
418 verbose "\t\tLoaded $arg with new symbol table into $GDB"
419 # All OK
420 }
421 timeout {
422 perror "(timeout) Couldn't load $arg, other program already loaded."
423 return -1
424 }
425 }
426 }
427 -re "No such file or directory.*$mi_gdb_prompt$" {
428 perror "($arg) No such file or directory\n"
429 return -1
430 }
431 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
432 # We (MI) are just giving the prompt back for now, instead of giving
433 # some acknowledgement.
434 return 0
435 }
436 timeout {
437 perror "couldn't load $arg into $GDB (timed out)."
438 return -1
439 }
440 eof {
441 # This is an attempt to detect a core dump, but seems not to
442 # work. Perhaps we need to match .* followed by eof, in which
443 # gdb_expect does not seem to have a way to do that.
444 perror "couldn't load $arg into $GDB (end of file)."
445 return -1
446 }
447 }
448 }
449
450 #
451 # load a file into the debugger.
452 # return a -1 if anything goes wrong.
453 #
454 proc mi_gdb_load { arg } {
455 global verbose
456 global loadpath
457 global loadfile
458 global GDB
459 global mi_gdb_prompt
460 upvar timeout timeout
461
462 # ``gdb_unload''
463 if { $arg != "" } {
464 mi_gdb_file_cmd $arg
465 }
466
467 # ``load''
468 if { [info procs gdbserver_gdb_load] != "" } {
469 global last_mi_gdb_file
470 global last_mi_remote_file
471
472 if { ! [info exists last_mi_remote_file] } {
473 if [is_remote target] {
474 set last_mi_remote_file [remote_download target $arg /tmp/[file tail $arg].[pid]]
475 } else {
476 set last_mi_remote_file $last_mi_gdb_file
477 }
478 }
479
480 set res [gdbserver_gdb_load $last_mi_remote_file]
481 set protocol [lindex $res 0]
482 set gdbport [lindex $res 1]
483
484 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
485 return -1
486 }
487 } elseif { [info procs send_target_sid] != "" } {
488 # For SID, things get complex
489 send_target_sid
490 gdb_expect 60 {
491 -re "\\^done.*$mi_gdb_prompt$" {
492 }
493 timeout {
494 perror "Unable to connect to SID target"
495 return -1
496 }
497 }
498 send_gdb "48-target-download\n"
499 gdb_expect 10 {
500 -re "48\\^done.*$mi_gdb_prompt$" {
501 }
502 timeout {
503 perror "Unable to download to SID target"
504 return -1
505 }
506 }
507 } elseif { [target_info protocol] == "sim" } {
508 # For the simulator, just connect to it directly.
509 send_gdb "47-target-select sim\n"
510 gdb_expect 10 {
511 -re "47\\^connected.*$mi_gdb_prompt$" {
512 }
513 timeout {
514 perror "Unable to select sim target"
515 return -1
516 }
517 }
518 send_gdb "48-target-download\n"
519 gdb_expect 10 {
520 -re "48\\^done.*$mi_gdb_prompt$" {
521 }
522 timeout {
523 perror "Unable to download to sim target"
524 return -1
525 }
526 }
527 } elseif { [target_info gdb_protocol] == "remote" } {
528 # remote targets
529 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
530 perror "Unable to connect to remote target"
531 return -1
532 }
533 send_gdb "48-target-download\n"
534 gdb_expect 10 {
535 -re "48\\^done.*$mi_gdb_prompt$" {
536 }
537 timeout {
538 perror "Unable to download to remote target"
539 return -1
540 }
541 }
542 }
543 return 0
544 }
545
546 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
547 # test the result.
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.
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.)
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.
559 #
560 # Returns:
561 # 1 if the test failed,
562 # 0 if the test passes,
563 # -1 if there was an internal error.
564 #
565 proc mi_gdb_test { args } {
566 global verbose
567 global mi_gdb_prompt
568 global GDB expect_out
569 upvar timeout timeout
570
571 set command [lindex $args 0]
572 set pattern [lindex $args 1]
573 set message [lindex $args 2]
574
575 if [llength $args]==4 {
576 set ipattern [lindex $args 3]
577 }
578
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";
594 set string_regex [string_to_regexp $command]
595
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
658 }
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
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.
672 if ![string match "" $message] then {
673 pass "$message"
674 }
675 set result 0
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
690 }
691 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
692 if ![string match "" $message] then {
693 set errmsg "$message (the program exited)"
694 } else {
695 set errmsg "$command (the program exited)"
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 {
702 set errmsg "$message (the program is no longer running)"
703 } else {
704 set errmsg "$command (the program is no longer running)"
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 }
743
744 # If the GDB output matched, compare the inferior output.
745 if { $result == 0 } {
746 if [ info exists ipattern ] {
747 global mi_inferior_spawn_id
748 expect {
749 -i $mi_inferior_spawn_id -re "$ipattern" {
750 pass "inferior_output:$message"
751 }
752 timeout {
753 fail "inferior output timeout"
754 set result 1
755 }
756 }
757 }
758 }
759
760 return $result
761 }
762
763 #
764 # MI run command. (A modified version of gdb_run_cmd)
765 #
766
767 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
768 # ``.*$'' could swallow up output that we attempt to match elsewhere.
769
770 proc mi_run_cmd {args} {
771 global suppress_flag
772 if { $suppress_flag } {
773 return -1
774 }
775 global mi_gdb_prompt
776
777 if [target_info exists gdb_init_command] {
778 send_gdb "[target_info gdb_init_command]\n";
779 gdb_expect 30 {
780 -re "$mi_gdb_prompt$" { }
781 default {
782 perror "gdb_init_command for target failed";
783 return;
784 }
785 }
786 }
787
788 if [target_info exists use_gdb_stub] {
789 if [target_info exists gdb,do_reload_on_run] {
790 # Specifying no file, defaults to the executable
791 # currently being debugged.
792 if { [mi_gdb_load ""] < 0 } {
793 return;
794 }
795 send_gdb "000-exec-continue\n";
796 gdb_expect 60 {
797 -re "000\\^running\[\r\n\]+$mi_gdb_prompt$" {}
798 default {}
799 }
800 return;
801 }
802
803 if [target_info exists gdb,start_symbol] {
804 set start [target_info gdb,start_symbol];
805 } else {
806 set start "start";
807 }
808
809 # HACK: Should either use 000-jump or fix the target code
810 # to better handle RUN.
811 send_gdb "jump *$start\n"
812 warning "Using CLI jump command, expect run-to-main FAIL"
813 return
814 }
815
816 send_gdb "000-exec-run $args\n"
817 gdb_expect {
818 -re "000\\^running\r\n${mi_gdb_prompt}" {
819 }
820 timeout {
821 perror "Unable to start target"
822 return
823 }
824 }
825 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
826 }
827
828 #
829 # Just like run-to-main but works with the MI interface
830 #
831
832 proc mi_run_to_main { } {
833 global suppress_flag
834 if { $suppress_flag } {
835 return -1
836 }
837
838 global srcdir
839 global subdir
840 global binfile
841 global srcfile
842
843 mi_delete_breakpoints
844 mi_gdb_reinitialize_dir $srcdir/$subdir
845 mi_gdb_load ${binfile}
846
847 mi_runto main
848 }
849
850
851 # Just like gdb's "runto" proc, it will run the target to a given
852 # function. The big difference here between mi_runto and mi_execute_to
853 # is that mi_execute_to must have the inferior running already. This
854 # proc will (like gdb's runto) (re)start the inferior, too.
855 #
856 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
857 # It returns:
858 # -1 if test suppressed, failed, timedout
859 # 0 if test passed
860
861 proc mi_runto_helper {func run_or_continue} {
862 global suppress_flag
863 if { $suppress_flag } {
864 return -1
865 }
866
867 global mi_gdb_prompt expect_out
868 global hex decimal fullname_syntax
869
870 set test "mi runto $func"
871 mi_gdb_test "200-break-insert -t $func" \
872 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
873 "breakpoint at $func"
874
875 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
876 || ![scan $str {number="%d"} bkptno]} {
877 set bkptno {[0-9]+}
878 }
879
880 if {$run_or_continue == "run"} {
881 mi_run_cmd
882 } else {
883 send_gdb "000-exec-continue\n"
884 gdb_expect {
885 -re "000\\^running\r\n${mi_gdb_prompt}" {
886 }
887 timeout {
888 fail "$test"
889 return -1
890 }
891 }
892 }
893
894 gdb_expect {
895 -re ".*000\\*stopped,thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=\(\\\[.*\\\]\|\{.*\}\),file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
896 pass "$test"
897 return 0
898 }
899 -re ".*$mi_gdb_prompt$" {
900 fail "$test (2)"
901 }
902 timeout {
903 fail "$test (timeout)"
904 return -1
905 }
906 }
907 }
908
909 proc mi_runto {func} {
910 mi_runto_helper $func "run"
911 }
912
913 # Next to the next statement
914 # For return values, see mi_execute_to_helper
915
916 proc mi_next { test } {
917 return [mi_next_to {.*} {.*} {.*} {.*} $test]
918 }
919
920
921 # Step to the next statement
922 # For return values, see mi_execute_to_helper
923
924 proc mi_step { test } {
925 return [mi_step_to {.*} {.*} {.*} {.*} $test]
926 }
927
928 # cmd should not include the number or newline (i.e. "exec-step 3", not
929 # "220-exec-step 3\n"
930
931 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
932 # after the first prompt is printed.
933
934 proc mi_execute_to_helper { cmd reason func args file line extra test } {
935 global suppress_flag
936 if { $suppress_flag } {
937 return -1
938 }
939 global mi_gdb_prompt
940 global hex
941 global decimal
942 global fullname_syntax
943 send_gdb "220-$cmd\n"
944 gdb_expect {
945 -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$" {
946 pass "$test"
947 return 0
948 }
949 -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$" {
950 fail "$test (stopped at wrong place)"
951 return -1
952 }
953 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
954 fail "$test (unknown output after running)"
955 return -1
956 }
957 timeout {
958 fail "$test (timeout)"
959 return -1
960 }
961 }
962 }
963
964 proc mi_execute_to { cmd reason func args file line extra test } {
965 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
966 "$file" "$line" "$extra" "$test"
967 }
968
969 proc mi_next_to { func args file line test } {
970 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
971 "$file" "$line" "" "$test"
972 }
973
974 proc mi_step_to { func args file line test } {
975 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
976 "$file" "$line" "" "$test"
977 }
978
979 proc mi_finish_to { func args file line result ret test } {
980 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
981 "$file" "$line" \
982 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
983 "$test"
984 }
985
986 proc mi_continue_to {func} {
987 mi_runto_helper $func "continue"
988 }
989
990 proc mi0_execute_to { cmd reason func args file line extra test } {
991 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
992 "$file" "$line" "$extra" "$test"
993 }
994
995 proc mi0_next_to { func args file line test } {
996 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
997 "$file" "$line" "" "$test"
998 }
999
1000 proc mi0_step_to { func args file line test } {
1001 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1002 "$file" "$line" "" "$test"
1003 }
1004
1005 proc mi0_finish_to { func args file line result ret test } {
1006 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1007 "$file" "$line" \
1008 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1009 "$test"
1010 }
1011
1012 proc mi0_continue_to { bkptno func args file line test } {
1013 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1014 "$func" "$args" "$file" "$line" "" "$test"
1015 }
1016
1017 # Creates varobj named NAME for EXPRESSION.
1018 # Name cannot be "-".
1019 proc mi_create_varobj { name expression testname } {
1020 mi_gdb_test "-var-create $name * $expression" \
1021 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",type=.*" \
1022 $testname
1023 }
1024
1025 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1026 # are reported as updated, and no other varobj is updated.
1027 # Assumes that no varobj is out of scope and that no varobj changes
1028 # types.
1029 proc mi_varobj_update { name expected testname } {
1030 set er "\\^done,changelist=\\\["
1031 set first 1
1032 foreach item $expected {
1033 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1034 if {$first} {
1035 set er "$er$v"
1036 } else {
1037 set er "$er,$v"
1038 }
1039 }
1040 set er "$er\\\]"
1041
1042 verbose -log "Expecting: $er" 2
1043 mi_gdb_test "-var-update $name" $er $testname
1044 }
1045
1046 proc mi_check_varobj_value { name value testname } {
1047
1048 mi_gdb_test "-var-evaluate-expression $name" \
1049 "\\^done,value=\"$value\"" \
1050 $testname
1051 }
1052
1053 # Check the results of the:
1054 #
1055 # -var-list-children VARNAME
1056 #
1057 # command. The CHILDREN parement should be a list of lists.
1058 # Each inner list can have either 3 or 4 elements, describing
1059 # fields that gdb is expected to report for child variable object,
1060 # in the following order
1061 #
1062 # - Name
1063 # - Expression
1064 # - Number of children
1065 # - Type
1066 #
1067 # If inner list has 3 elements, the gdb is expected to output no
1068 # type for a child.
1069 #
1070 proc mi_list_varobj_children { varname children testname } {
1071
1072 set numchildren [llength $children]
1073 set children_exp {}
1074 set whatever "\"\[^\"\]+\""
1075
1076 foreach item $children {
1077
1078 set name [lindex $item 0]
1079 set exp [lindex $item 1]
1080 set numchild [lindex $item 2]
1081 if {[llength $item] == 4} {
1082 set type [lindex $item 3]
1083
1084 lappend children_exp\
1085 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"}"
1086 } else {
1087 lappend children_exp\
1088 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"}"
1089 }
1090 }
1091 set children_exp_j [join $children_exp ","]
1092 set expected "\\^done,numchild=\"$numchildren\",children=\\\[$children_exp_j\\\]"
1093
1094 verbose -log "Expecting: $expected"
1095
1096 mi_gdb_test "-var-list-children $varname" $expected $testname
1097 }
1098
1099 # A list of two-element lists. First element of each list is
1100 # a Tcl statement, and the second element is the line
1101 # number of source C file where the statement originates.
1102 set mi_autotest_data ""
1103 # The name of the source file for autotesting.
1104 set mi_autotest_source ""
1105
1106 proc count_newlines { string } {
1107 return [regexp -all "\n" $string]
1108 }
1109
1110 # Prepares for running inline tests in FILENAME.
1111 # See comments for mi_run_inline_test for detailed
1112 # explanation of the idea and syntax.
1113 proc mi_prepare_inline_tests { filename } {
1114
1115 global srcdir
1116 global subdir
1117 global mi_autotest_source
1118 global mi_autotest_data
1119
1120 set mi_autotest_data {}
1121
1122 set mi_autotest_source $filename
1123
1124 if { ! [regexp "^/" "$filename"] } then {
1125 set filename "$srcdir/$subdir/$filename"
1126 }
1127
1128 set chan [open $filename]
1129 set content [read $chan]
1130 set line_number 1
1131 while {1} {
1132 set start [string first "/*:" $content]
1133 if {$start != -1} {
1134 set end [string first ":*/" $content]
1135 if {$end == -1} {
1136 error "Unterminated special comment in $filename"
1137 }
1138
1139 set prefix [string range $content 0 $start]
1140 set prefix_newlines [count_newlines $prefix]
1141
1142 set line_number [expr $line_number+$prefix_newlines]
1143 set comment_line $line_number
1144
1145 set comment [string range $content [expr $start+3] [expr $end-1]]
1146
1147 set comment_newlines [count_newlines $comment]
1148 set line_number [expr $line_number+$comment_newlines]
1149
1150 set comment [string trim $comment]
1151 set content [string range $content [expr $end+3] \
1152 [string length $content]]
1153 lappend mi_autotest_data [list $comment $comment_line]
1154 } else {
1155 break
1156 }
1157 }
1158 close $chan
1159 }
1160
1161 # Helper to mi_run_inline_test below.
1162 # Return the list of all (statement,line_number) lists
1163 # that comprise TESTCASE. The begin and end markers
1164 # are not included.
1165 proc mi_get_inline_test {testcase} {
1166
1167 global mi_gdb_prompt
1168 global mi_autotest_data
1169 global mi_autotest_source
1170
1171 set result {}
1172
1173 set seen_begin 0
1174 set seen_end 0
1175 foreach l $mi_autotest_data {
1176
1177 set comment [lindex $l 0]
1178
1179 if {$comment == "BEGIN: $testcase"} {
1180 set seen_begin 1
1181 } elseif {$comment == "END: $testcase"} {
1182 set seen_end 1
1183 break
1184 } elseif {$seen_begin==1} {
1185 lappend result $l
1186 }
1187 }
1188
1189 if {$seen_begin == 0} {
1190 error "Autotest $testcase not found"
1191 }
1192
1193 if {$seen_begin == 1 && $seen_end == 0} {
1194 error "Missing end marker for test $testcase"
1195 }
1196
1197 return $result
1198 }
1199
1200 # Sets temporary breakpoint at LOCATION.
1201 proc mi_tbreak {location} {
1202
1203 global mi_gdb_prompt
1204
1205 mi_gdb_test "-break-insert -t $location" \
1206 {\^done,bkpt=.*} \
1207 "run to $location (set breakpoint)"
1208 }
1209
1210 # Send COMMAND that must be a command that resumes
1211 # the inferiour (run/continue/next/etc) and consumes
1212 # the "^running" output from it.
1213 proc mi_send_resuming_command {command test} {
1214
1215 global mi_gdb_prompt
1216
1217 send_gdb "220-$command\n"
1218 gdb_expect {
1219 -re "220\\^running\r\n${mi_gdb_prompt}" {
1220 }
1221 timeout {
1222 fail $test
1223 }
1224 }
1225 }
1226
1227 # Helper to mi_run_inline_test below.
1228 # Sets a temporary breakpoint at LOCATION and runs
1229 # the program using COMMAND. When the program is stopped
1230 # returns the line at which it. Returns -1 if line cannot
1231 # be determined.
1232 # Does not check that the line is the same as requested.
1233 # The caller can check itself if required.
1234 proc mi_continue_to_line {location command} {
1235
1236 mi_tbreak $location
1237 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1238 return [mi_wait_for_stop]
1239 }
1240
1241 # Wait until gdb prints the current line.
1242 proc mi_wait_for_stop {test} {
1243
1244 global mi_gdb_prompt
1245
1246 gdb_expect {
1247 -re ".*line=\"(.*)\".*\r\n$mi_gdb_prompt$" {
1248 return $expect_out(1,string)
1249 }
1250 -re ".*$mi_gdb_prompt$" {
1251 fail "wait for stop ($test)"
1252 }
1253 timeout {
1254 fail "wait for stop ($test)"
1255 }
1256 }
1257 }
1258
1259 # Run a MI test embedded in comments in a C file.
1260 # The C file should contain special comments in the following
1261 # three forms:
1262 #
1263 # /*: BEGIN: testname :*/
1264 # /*: <Tcl statements> :*/
1265 # /*: END: testname :*/
1266 #
1267 # This procedure find the begin and end marker for the requested
1268 # test. Then, a temporary breakpoint is set at the begin
1269 # marker and the program is run (from start).
1270 #
1271 # After that, for each special comment between the begin and end
1272 # marker, the Tcl statements are executed. It is assumed that
1273 # for each comment, the immediately preceding line is executable
1274 # C statement. Then, gdb will be single-stepped until that
1275 # preceding C statement is executed, and after that the
1276 # Tcl statements in the comment will be executed.
1277 #
1278 # For example:
1279 #
1280 # /*: BEGIN: assignment-test :*/
1281 # v = 10;
1282 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1283 # /*: END: assignment-test :*/
1284 #
1285 # The mi_prepare_inline_tests function should be called before
1286 # calling this function. A given C file can contain several
1287 # inline tests. The names of the tests must be unique within one
1288 # C file.
1289 #
1290 proc mi_run_inline_test { testcase } {
1291
1292 global mi_gdb_prompt
1293 global hex
1294 global decimal
1295 global fullname_syntax
1296 global mi_autotest_source
1297
1298 set commands [mi_get_inline_test $testcase]
1299
1300 set first 1
1301 set line_now 1
1302
1303 foreach c $commands {
1304 set statements [lindex $c 0]
1305 set line [lindex $c 1]
1306 set line [expr $line-1]
1307
1308 # We want gdb to be stopped at the expression immediately
1309 # before the comment. If this is the first comment, the
1310 # program is either not started yet or is in some random place,
1311 # so we run it. For further comments, we might be already
1312 # standing at the right line. If not continue till the
1313 # right line.
1314
1315 if {$first==1} {
1316 # Start the program afresh.
1317 mi_tbreak "$mi_autotest_source:$line"
1318 mi_run_cmd
1319 set line_now [mi_wait_for_stop "$testcase: step to $line"]
1320 set first 0
1321 } elseif {$line_now!=$line} {
1322 set line_now [mi_continue_to_line "$mi_autotest_source:$line"]
1323 }
1324
1325 if {$line_now!=$line} {
1326 fail "$testcase: go to line $line"
1327 }
1328
1329 # We're not at the statement right above the comment.
1330 # Execute that statement so that the comment can test
1331 # the state after the statement is executed.
1332
1333 # Single-step past the line.
1334 mi_send_resuming_command "exec-next" "$testcase: step over $line"
1335 set line_now [mi_wait_for_stop "$testcase: step over $line"]
1336
1337 # We probably want to use 'uplevel' so that statements
1338 # have direct access to global variables that the
1339 # main 'exp' file has set up. But it's not yet clear,
1340 # will need more experience to be sure.
1341 eval $statements
1342 }
1343 }
This page took 0.058068 seconds and 4 git commands to generate.