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