c1381a5c249133fa4de7ad7beeb467803866ac64
[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 set serialport_re [string_to_regexp $serialport]
329 for {set i 1} {$i <= 3} {incr i} {
330 send_gdb "47-target-select $targetname $serialport\n"
331 gdb_expect 60 {
332 -re "47\\^connected.*$mi_gdb_prompt$" {
333 verbose "Set target to $targetname";
334 return 0;
335 }
336 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
337 verbose "Connection failed";
338 }
339 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
340 verbose "Set target to $targetname";
341 return 0;
342 }
343 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
344 verbose "Set target to $targetname";
345 return 0;
346 }
347 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
348 verbose "Set target to $targetname";
349 return 0;
350 }
351 -re "Connected to.*$mi_gdb_prompt$" {
352 verbose "Set target to $targetname";
353 return 0;
354 }
355 -re "Ending remote.*$mi_gdb_prompt$" { }
356 -re "Connection refused.*$mi_gdb_prompt$" {
357 verbose "Connection refused by remote target. Pausing, and trying again."
358 sleep 5
359 continue
360 }
361 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
362 verbose "Got timeout error from gdb.";
363 }
364 timeout {
365 send_gdb "\ 3";
366 break
367 }
368 }
369 }
370 return 1
371 }
372
373 #
374 # load a file into the debugger (file command only).
375 # return a -1 if anything goes wrong.
376 #
377 proc mi_gdb_file_cmd { arg } {
378 global verbose
379 global loadpath
380 global loadfile
381 global GDB
382 global mi_gdb_prompt
383 global last_loaded_file
384 upvar timeout timeout
385
386 set last_loaded_file $arg
387
388 if [is_remote host] {
389 set arg [remote_download host $arg];
390 if { $arg == "" } {
391 error "download failed"
392 return -1;
393 }
394 }
395
396 # FIXME: Several of these patterns are only acceptable for console
397 # output. Queries are an error for mi.
398 send_gdb "105-file-exec-and-symbols $arg\n"
399 gdb_expect 120 {
400 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
401 verbose "\t\tLoaded $arg into the $GDB"
402 return 0
403 }
404 -re "has no symbol-table.*$mi_gdb_prompt$" {
405 perror "$arg wasn't compiled with \"-g\""
406 return -1
407 }
408 -re "Load new symbol table from \".*\".*y or n. $" {
409 send_gdb "y\n"
410 gdb_expect 120 {
411 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
412 verbose "\t\tLoaded $arg with new symbol table into $GDB"
413 # All OK
414 }
415 timeout {
416 perror "(timeout) Couldn't load $arg, other program already loaded."
417 return -1
418 }
419 }
420 }
421 -re "No such file or directory.*$mi_gdb_prompt$" {
422 perror "($arg) No such file or directory\n"
423 return -1
424 }
425 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
426 # We (MI) are just giving the prompt back for now, instead of giving
427 # some acknowledgement.
428 return 0
429 }
430 timeout {
431 perror "couldn't load $arg into $GDB (timed out)."
432 return -1
433 }
434 eof {
435 # This is an attempt to detect a core dump, but seems not to
436 # work. Perhaps we need to match .* followed by eof, in which
437 # gdb_expect does not seem to have a way to do that.
438 perror "couldn't load $arg into $GDB (end of file)."
439 return -1
440 }
441 }
442 }
443
444 #
445 # connect to the target and download a file, if necessary.
446 # return a -1 if anything goes wrong.
447 #
448 proc mi_gdb_target_load { } {
449 global verbose
450 global loadpath
451 global loadfile
452 global GDB
453 global mi_gdb_prompt
454 upvar timeout timeout
455
456 if { [info procs gdbserver_gdb_load] != "" } {
457 mi_gdb_test "kill" ".*" ""
458 set res [gdbserver_gdb_load]
459 set protocol [lindex $res 0]
460 set gdbport [lindex $res 1]
461
462 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
463 return -1
464 }
465 } elseif { [info procs send_target_sid] != "" } {
466 # For SID, things get complex
467 send_target_sid
468 gdb_expect 60 {
469 -re "\\^done.*$mi_gdb_prompt$" {
470 }
471 timeout {
472 perror "Unable to connect to SID target"
473 return -1
474 }
475 }
476 send_gdb "48-target-download\n"
477 gdb_expect 10 {
478 -re "48\\^done.*$mi_gdb_prompt$" {
479 }
480 timeout {
481 perror "Unable to download to SID target"
482 return -1
483 }
484 }
485 } elseif { [target_info protocol] == "sim" } {
486 # For the simulator, just connect to it directly.
487 send_gdb "47-target-select sim\n"
488 gdb_expect 10 {
489 -re "47\\^connected.*$mi_gdb_prompt$" {
490 }
491 timeout {
492 perror "Unable to select sim target"
493 return -1
494 }
495 }
496 send_gdb "48-target-download\n"
497 gdb_expect 10 {
498 -re "48\\^done.*$mi_gdb_prompt$" {
499 }
500 timeout {
501 perror "Unable to download to sim target"
502 return -1
503 }
504 }
505 } elseif { [target_info gdb_protocol] == "remote" } {
506 # remote targets
507 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
508 perror "Unable to connect to remote target"
509 return -1
510 }
511 send_gdb "48-target-download\n"
512 gdb_expect 10 {
513 -re "48\\^done.*$mi_gdb_prompt$" {
514 }
515 timeout {
516 perror "Unable to download to remote target"
517 return -1
518 }
519 }
520 }
521 return 0
522 }
523
524 #
525 # load a file into the debugger.
526 # return a -1 if anything goes wrong.
527 #
528 proc mi_gdb_load { arg } {
529 if { $arg != "" } {
530 return [mi_gdb_file_cmd $arg]
531 }
532 return 0
533 }
534
535 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
536 # test the result.
537 #
538 # COMMAND is the command to execute, send to GDB with send_gdb. If
539 # this is the null string no command is sent.
540 # PATTERN is the pattern to match for a PASS, and must NOT include
541 # the \r\n sequence immediately before the gdb prompt.
542 # MESSAGE is the message to be printed. (If this is the empty string,
543 # then sometimes we don't call pass or fail at all; I don't
544 # understand this at all.)
545 # IPATTERN is the pattern to match for the inferior's output. This parameter
546 # is optional. If present, it will produce a PASS if the match is
547 # successful, and a FAIL if unsuccessful.
548 #
549 # Returns:
550 # 1 if the test failed,
551 # 0 if the test passes,
552 # -1 if there was an internal error.
553 #
554 proc mi_gdb_test { args } {
555 global verbose
556 global mi_gdb_prompt
557 global GDB expect_out
558 upvar timeout timeout
559
560 set command [lindex $args 0]
561 set pattern [lindex $args 1]
562 set message [lindex $args 2]
563
564 if [llength $args]==4 {
565 set ipattern [lindex $args 3]
566 }
567
568 if [llength $args]==5 {
569 set question_string [lindex $args 3];
570 set response_string [lindex $args 4];
571 } else {
572 set question_string "^FOOBAR$"
573 }
574
575 if $verbose>2 then {
576 send_user "Sending \"$command\" to gdb\n"
577 send_user "Looking to match \"$pattern\"\n"
578 send_user "Message is \"$message\"\n"
579 }
580
581 set result -1
582 set string "${command}\n";
583 set string_regex [string_to_regexp $command]
584
585 if { $command != "" } {
586 while { "$string" != "" } {
587 set foo [string first "\n" "$string"];
588 set len [string length "$string"];
589 if { $foo < [expr $len - 1] } {
590 set str [string range "$string" 0 $foo];
591 if { [send_gdb "$str"] != "" } {
592 global suppress_flag;
593
594 if { ! $suppress_flag } {
595 perror "Couldn't send $command to GDB.";
596 }
597 fail "$message";
598 return $result;
599 }
600 gdb_expect 2 {
601 -re "\[\r\n\]" { }
602 timeout { }
603 }
604 set string [string range "$string" [expr $foo + 1] end];
605 } else {
606 break;
607 }
608 }
609 if { "$string" != "" } {
610 if { [send_gdb "$string"] != "" } {
611 global suppress_flag;
612
613 if { ! $suppress_flag } {
614 perror "Couldn't send $command to GDB.";
615 }
616 fail "$message";
617 return $result;
618 }
619 }
620 }
621
622 if [info exists timeout] {
623 set tmt $timeout;
624 } else {
625 global timeout;
626 if [info exists timeout] {
627 set tmt $timeout;
628 } else {
629 set tmt 60;
630 }
631 }
632 gdb_expect $tmt {
633 -re "\\*\\*\\* DOSEXIT code.*" {
634 if { $message != "" } {
635 fail "$message";
636 }
637 gdb_suppress_entire_file "GDB died";
638 return -1;
639 }
640 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
641 if ![isnative] then {
642 warning "Can`t communicate to remote target."
643 }
644 gdb_exit
645 gdb_start
646 set result -1
647 }
648 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
649 # At this point, $expect_out(1,string) is the MI input command.
650 # and $expect_out(2,string) is the MI output command.
651 # If $expect_out(1,string) is "", then there was no MI input command here.
652
653 # NOTE, there is no trailing anchor because with GDB/MI,
654 # asynchronous responses can happen at any point, causing more
655 # data to be available. Normally an anchor is used to make
656 # sure the end of the output is matched, however, $mi_gdb_prompt
657 # is just as good of an anchor since mi_gdb_test is meant to
658 # match a single mi output command. If a second GDB/MI output
659 # response is sent, it will be in the buffer for the next
660 # time mi_gdb_test is called.
661 if ![string match "" $message] then {
662 pass "$message"
663 }
664 set result 0
665 }
666 -re "(${question_string})$" {
667 send_gdb "$response_string\n";
668 exp_continue;
669 }
670 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
671 perror "Undefined command \"$command\"."
672 fail "$message"
673 set result 1
674 }
675 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
676 perror "\"$command\" is not a unique command name."
677 fail "$message"
678 set result 1
679 }
680 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
681 if ![string match "" $message] then {
682 set errmsg "$message (the program exited)"
683 } else {
684 set errmsg "$command (the program exited)"
685 }
686 fail "$errmsg"
687 return -1
688 }
689 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
690 if ![string match "" $message] then {
691 set errmsg "$message (the program is no longer running)"
692 } else {
693 set errmsg "$command (the program is no longer running)"
694 }
695 fail "$errmsg"
696 return -1
697 }
698 -re ".*$mi_gdb_prompt\[ \]*$" {
699 if ![string match "" $message] then {
700 fail "$message"
701 }
702 set result 1
703 }
704 "<return>" {
705 send_gdb "\n"
706 perror "Window too small."
707 fail "$message"
708 }
709 -re "\\(y or n\\) " {
710 send_gdb "n\n"
711 perror "Got interactive prompt."
712 fail "$message"
713 }
714 eof {
715 perror "Process no longer exists"
716 if { $message != "" } {
717 fail "$message"
718 }
719 return -1
720 }
721 full_buffer {
722 perror "internal buffer is full."
723 fail "$message"
724 }
725 timeout {
726 if ![string match "" $message] then {
727 fail "$message (timeout)"
728 }
729 set result 1
730 }
731 }
732
733 # If the GDB output matched, compare the inferior output.
734 if { $result == 0 } {
735 if [ info exists ipattern ] {
736 if { ![target_info exists gdb,noinferiorio] } {
737 global mi_inferior_spawn_id
738 expect {
739 -i $mi_inferior_spawn_id -re "$ipattern" {
740 pass "$message inferior output"
741 }
742 timeout {
743 fail "$message inferior output (timeout)"
744 set result 1
745 }
746 }
747 } else {
748 unsupported "$message inferior output"
749 }
750 }
751 }
752
753 return $result
754 }
755
756 #
757 # MI run command. (A modified version of gdb_run_cmd)
758 #
759
760 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
761 # ``.*$'' could swallow up output that we attempt to match elsewhere.
762
763 proc mi_run_cmd {args} {
764 global suppress_flag
765 if { $suppress_flag } {
766 return -1
767 }
768 global mi_gdb_prompt
769
770 if [target_info exists gdb_init_command] {
771 send_gdb "[target_info gdb_init_command]\n";
772 gdb_expect 30 {
773 -re "$mi_gdb_prompt$" { }
774 default {
775 perror "gdb_init_command for target failed";
776 return;
777 }
778 }
779 }
780
781 if { [mi_gdb_target_load] < 0 } {
782 return
783 }
784
785 if [target_info exists use_gdb_stub] {
786 if [target_info exists gdb,do_reload_on_run] {
787 send_gdb "000-exec-continue\n";
788 gdb_expect 60 {
789 -re "000\\^running\[\r\n\]+$mi_gdb_prompt$" {}
790 default {}
791 }
792 return;
793 }
794
795 if [target_info exists gdb,start_symbol] {
796 set start [target_info gdb,start_symbol];
797 } else {
798 set start "start";
799 }
800
801 # HACK: Should either use 000-jump or fix the target code
802 # to better handle RUN.
803 send_gdb "jump *$start\n"
804 warning "Using CLI jump command, expect run-to-main FAIL"
805 return
806 }
807
808 send_gdb "000-exec-run $args\n"
809 gdb_expect {
810 -re "000\\^running\r\n${mi_gdb_prompt}" {
811 }
812 timeout {
813 perror "Unable to start target"
814 return
815 }
816 }
817 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
818 }
819
820 #
821 # Just like run-to-main but works with the MI interface
822 #
823
824 proc mi_run_to_main { } {
825 global suppress_flag
826 if { $suppress_flag } {
827 return -1
828 }
829
830 global srcdir
831 global subdir
832 global binfile
833 global srcfile
834
835 mi_delete_breakpoints
836 mi_gdb_reinitialize_dir $srcdir/$subdir
837 mi_gdb_load ${binfile}
838
839 mi_runto main
840 }
841
842
843 # Just like gdb's "runto" proc, it will run the target to a given
844 # function. The big difference here between mi_runto and mi_execute_to
845 # is that mi_execute_to must have the inferior running already. This
846 # proc will (like gdb's runto) (re)start the inferior, too.
847 #
848 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
849 # It returns:
850 # -1 if test suppressed, failed, timedout
851 # 0 if test passed
852
853 proc mi_runto_helper {func run_or_continue} {
854 global suppress_flag
855 if { $suppress_flag } {
856 return -1
857 }
858
859 global mi_gdb_prompt expect_out
860 global hex decimal fullname_syntax
861
862 set test "mi runto $func"
863 mi_gdb_test "200-break-insert -t $func" \
864 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
865 "breakpoint at $func"
866
867 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
868 || ![scan $str {number="%d"} bkptno]} {
869 set bkptno {[0-9]+}
870 }
871
872 if {$run_or_continue == "run"} {
873 mi_run_cmd
874 } else {
875 send_gdb "000-exec-continue\n"
876 gdb_expect {
877 -re "000\\^running\r\n${mi_gdb_prompt}" {
878 }
879 timeout {
880 fail "$test"
881 return -1
882 }
883 }
884 }
885
886 gdb_expect {
887 -re ".*000\\*stopped,thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=\(\\\[.*\\\]\|\{.*\}\),file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
888 pass "$test"
889 return 0
890 }
891 -re ".*$mi_gdb_prompt$" {
892 fail "$test (2)"
893 }
894 timeout {
895 fail "$test (timeout)"
896 return -1
897 }
898 }
899 }
900
901 proc mi_runto {func} {
902 mi_runto_helper $func "run"
903 }
904
905 # Next to the next statement
906 # For return values, see mi_execute_to_helper
907
908 proc mi_next { test } {
909 return [mi_next_to {.*} {.*} {.*} {.*} $test]
910 }
911
912
913 # Step to the next statement
914 # For return values, see mi_execute_to_helper
915
916 proc mi_step { test } {
917 return [mi_step_to {.*} {.*} {.*} {.*} $test]
918 }
919
920 # cmd should not include the number or newline (i.e. "exec-step 3", not
921 # "220-exec-step 3\n"
922
923 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
924 # after the first prompt is printed.
925
926 proc mi_execute_to_helper { cmd reason func args file line extra test } {
927 global suppress_flag
928 if { $suppress_flag } {
929 return -1
930 }
931 global mi_gdb_prompt
932 global hex
933 global decimal
934 global fullname_syntax
935 send_gdb "220-$cmd\n"
936 gdb_expect {
937 -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$" {
938 pass "$test"
939 return 0
940 }
941 -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$" {
942 fail "$test (stopped at wrong place)"
943 return -1
944 }
945 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
946 fail "$test (unknown output after running)"
947 return -1
948 }
949 timeout {
950 fail "$test (timeout)"
951 return -1
952 }
953 }
954 }
955
956 proc mi_execute_to { cmd reason func args file line extra test } {
957 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
958 "$file" "$line" "$extra" "$test"
959 }
960
961 proc mi_next_to { func args file line test } {
962 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
963 "$file" "$line" "" "$test"
964 }
965
966 proc mi_step_to { func args file line test } {
967 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
968 "$file" "$line" "" "$test"
969 }
970
971 proc mi_finish_to { func args file line result ret test } {
972 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
973 "$file" "$line" \
974 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
975 "$test"
976 }
977
978 proc mi_continue_to {func} {
979 mi_runto_helper $func "continue"
980 }
981
982 proc mi0_execute_to { cmd reason func args file line extra test } {
983 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
984 "$file" "$line" "$extra" "$test"
985 }
986
987 proc mi0_next_to { func args file line test } {
988 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
989 "$file" "$line" "" "$test"
990 }
991
992 proc mi0_step_to { func args file line test } {
993 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
994 "$file" "$line" "" "$test"
995 }
996
997 proc mi0_finish_to { func args file line result ret test } {
998 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
999 "$file" "$line" \
1000 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1001 "$test"
1002 }
1003
1004 proc mi0_continue_to { bkptno func args file line test } {
1005 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1006 "$func" "$args" "$file" "$line" "" "$test"
1007 }
1008
1009 # Creates varobj named NAME for EXPRESSION.
1010 # Name cannot be "-".
1011 proc mi_create_varobj { name expression testname } {
1012 mi_gdb_test "-var-create $name * $expression" \
1013 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1014 $testname
1015 }
1016
1017 # Deletes the specified NAME.
1018 proc mi_delete_varobj { name testname } {
1019 mi_gdb_test "-var-delete $name" \
1020 "\\^done,ndeleted=.*" \
1021 $testname
1022 }
1023
1024 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1025 # are reported as updated, and no other varobj is updated.
1026 # Assumes that no varobj is out of scope and that no varobj changes
1027 # types.
1028 proc mi_varobj_update { name expected testname } {
1029 set er "\\^done,changelist=\\\["
1030 set first 1
1031 foreach item $expected {
1032 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1033 if {$first == 1} {
1034 set er "$er$v"
1035 set first 0
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 test} {
1235
1236 mi_tbreak $location
1237 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1238 return [mi_wait_for_stop $test]
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" "continue to $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.070438 seconds and 4 git commands to generate.