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