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