]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/lib/mi-support.exp
2011-08-09 Pedro Alves <pedro@codesourcery.com>
[thirdparty/binutils-gdb.git] / gdb / testsuite / lib / mi-support.exp
1 # Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
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 # This file was based on a file written by Fred Fish. (fnf@cygnus.com)
18
19 # Test setup routines that work with the MI interpreter.
20
21 # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
22 # Set it if it is not already set.
23 global mi_gdb_prompt
24 if ![info exists mi_gdb_prompt] then {
25 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
26 }
27
28 global mi_inferior_spawn_id
29 global mi_inferior_tty_name
30
31 set MIFLAGS "-i=mi"
32
33 set thread_selected_re "=thread-selected,id=\"\[0-9\]+\"\r\n"
34 set library_loaded_re "=library-loaded\[^\n\]+\"\r\n"
35 set breakpoint_re "=(breakpoint-created|breakpoint-deleted)\[^\n\]+\"\r\n"
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 INTERNAL_GDBFLAGS 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 $INTERNAL_GDBFLAGS $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 # default_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 default_mi_gdb_start { args } {
99 global verbose
100 global GDB
101 global INTERNAL_GDBFLAGS 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 $INTERNAL_GDBFLAGS $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 $INTERNAL_GDBFLAGS $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 ".*unrecognized option.*for a complete list of options." {
174 untested "Skip mi tests (not compiled with mi support)."
175 remote_close host;
176 return -1;
177 }
178 -re ".*Interpreter `mi' unrecognized." {
179 untested "Skip mi tests (not compiled with mi support)."
180 remote_close host;
181 return -1;
182 }
183 timeout {
184 perror "(timeout) GDB never initialized after 10 seconds."
185 remote_close host;
186 return -1
187 }
188 }
189 set gdb_spawn_id -1;
190
191 # FIXME: mi output does not go through pagers, so these can be removed.
192 # force the height to "unlimited", so no pagers get used
193 send_gdb "100-gdb-set height 0\n"
194 gdb_expect 10 {
195 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
196 verbose "Setting height to 0." 2
197 }
198 timeout {
199 warning "Couldn't set the height to 0"
200 }
201 }
202 # force the width to "unlimited", so no wraparound occurs
203 send_gdb "101-gdb-set width 0\n"
204 gdb_expect 10 {
205 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
206 verbose "Setting width to 0." 2
207 }
208 timeout {
209 warning "Couldn't set the width to 0."
210 }
211 }
212 # If allowing the inferior to have its own PTY then assign the inferior
213 # its own terminal device here.
214 if { $separate_inferior_pty } {
215 send_gdb "102-inferior-tty-set $mi_inferior_tty_name\n"
216 gdb_expect 10 {
217 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
218 verbose "redirect inferior output to new terminal device."
219 }
220 timeout {
221 warning "Couldn't redirect inferior output." 2
222 }
223 }
224 }
225
226 mi_detect_async
227
228 return 0;
229 }
230
231 #
232 # Overridable function. You can override this function in your
233 # baseboard file.
234 #
235 proc mi_gdb_start { args } {
236 return [default_mi_gdb_start $args]
237 }
238
239 # Many of the tests depend on setting breakpoints at various places and
240 # running until that breakpoint is reached. At times, we want to start
241 # with a clean-slate with respect to breakpoints, so this utility proc
242 # lets us do this without duplicating this code everywhere.
243 #
244
245 proc mi_delete_breakpoints {} {
246 global mi_gdb_prompt
247
248 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
249 send_gdb "102-break-delete\n"
250 gdb_expect 30 {
251 -re "Delete all breakpoints.*y or n.*$" {
252 send_gdb "y\n";
253 exp_continue
254 }
255 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
256 # This happens if there were no breakpoints
257 }
258 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
259 }
260
261 # The correct output is not "No breakpoints or watchpoints." but an
262 # empty BreakpointTable. Also, a query is not acceptable with mi.
263 send_gdb "103-break-list\n"
264 gdb_expect 30 {
265 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
266 -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=\\\[\\\]\}\r\n$mi_gdb_prompt$" {}
267 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
268 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
269 -re "Delete all breakpoints.*or n.*$" {
270 warning "Unexpected prompt for breakpoints deletion";
271 send_gdb "y\n";
272 exp_continue
273 }
274 timeout { perror "-break-list (timeout)" ; return }
275 }
276 }
277
278 proc mi_gdb_reinitialize_dir { subdir } {
279 global mi_gdb_prompt
280 global MIFLAGS
281
282 global suppress_flag
283 if { $suppress_flag } {
284 return
285 }
286
287 if [is_remote host] {
288 return "";
289 }
290
291 if { $MIFLAGS == "-i=mi1" } {
292 send_gdb "104-environment-directory\n"
293 gdb_expect 60 {
294 -re ".*Reinitialize source path to empty.*y or n. " {
295 warning "Got confirmation prompt for dir reinitialization."
296 send_gdb "y\n"
297 gdb_expect 60 {
298 -re "$mi_gdb_prompt$" {}
299 timeout {error "Dir reinitialization failed (timeout)"}
300 }
301 }
302 -re "$mi_gdb_prompt$" {}
303 timeout {error "Dir reinitialization failed (timeout)"}
304 }
305 } else {
306 send_gdb "104-environment-directory -r\n"
307 gdb_expect 60 {
308 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
309 -re "$mi_gdb_prompt$" {}
310 timeout {error "Dir reinitialization failed (timeout)"}
311 }
312 }
313
314 send_gdb "105-environment-directory $subdir\n"
315 gdb_expect 60 {
316 -re "Source directories searched.*$mi_gdb_prompt$" {
317 verbose "Dir set to $subdir"
318 }
319 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
320 # FIXME: We return just the prompt for now.
321 verbose "Dir set to $subdir"
322 # perror "Dir \"$subdir\" failed."
323 }
324 }
325 }
326
327 # Send GDB the "target" command.
328 # FIXME: Some of these patterns are not appropriate for MI. Based on
329 # config/monitor.exp:gdb_target_command.
330 proc mi_gdb_target_cmd { targetname serialport } {
331 global mi_gdb_prompt
332
333 set serialport_re [string_to_regexp $serialport]
334 for {set i 1} {$i <= 3} {incr i} {
335 send_gdb "47-target-select $targetname $serialport\n"
336 gdb_expect 60 {
337 -re "47\\^connected.*$mi_gdb_prompt" {
338 verbose "Set target to $targetname";
339 return 0;
340 }
341 -re "unknown host.*$mi_gdb_prompt" {
342 verbose "Couldn't look up $serialport"
343 }
344 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
345 verbose "Connection failed";
346 }
347 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
348 verbose "Set target to $targetname";
349 return 0;
350 }
351 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
352 verbose "Set target to $targetname";
353 return 0;
354 }
355 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
356 verbose "Set target to $targetname";
357 return 0;
358 }
359 -re "Connected to.*$mi_gdb_prompt$" {
360 verbose "Set target to $targetname";
361 return 0;
362 }
363 -re "Ending remote.*$mi_gdb_prompt$" { }
364 -re "Connection refused.*$mi_gdb_prompt$" {
365 verbose "Connection refused by remote target. Pausing, and trying again."
366 sleep 5
367 continue
368 }
369 -re "Non-stop mode requested, but remote does not support non-stop.*$mi_gdb_prompt" {
370 unsupported "Non-stop mode not supported"
371 return 1
372 }
373 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
374 verbose "Got timeout error from gdb.";
375 }
376 timeout {
377 send_gdb "\ 3";
378 break
379 }
380 }
381 }
382 return 1
383 }
384
385 #
386 # load a file into the debugger (file command only).
387 # return a -1 if anything goes wrong.
388 #
389 proc mi_gdb_file_cmd { arg } {
390 global verbose
391 global loadpath
392 global loadfile
393 global GDB
394 global mi_gdb_prompt
395 global last_loaded_file
396 upvar timeout timeout
397
398 set last_loaded_file $arg
399
400 if [is_remote host] {
401 set arg [remote_download host $arg];
402 if { $arg == "" } {
403 error "download failed"
404 return -1;
405 }
406 }
407
408 # FIXME: Several of these patterns are only acceptable for console
409 # output. Queries are an error for mi.
410 send_gdb "105-file-exec-and-symbols $arg\n"
411 gdb_expect 120 {
412 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
413 verbose "\t\tLoaded $arg into the $GDB"
414 return 0
415 }
416 -re "has no symbol-table.*$mi_gdb_prompt$" {
417 perror "$arg wasn't compiled with \"-g\""
418 return -1
419 }
420 -re "Load new symbol table from \".*\".*y or n. $" {
421 send_gdb "y\n"
422 gdb_expect 120 {
423 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
424 verbose "\t\tLoaded $arg with new symbol table into $GDB"
425 # All OK
426 }
427 timeout {
428 perror "(timeout) Couldn't load $arg, other program already loaded."
429 return -1
430 }
431 }
432 }
433 -re "No such file or directory.*$mi_gdb_prompt$" {
434 perror "($arg) No such file or directory\n"
435 return -1
436 }
437 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
438 # We (MI) are just giving the prompt back for now, instead of giving
439 # some acknowledgement.
440 return 0
441 }
442 timeout {
443 perror "couldn't load $arg into $GDB (timed out)."
444 return -1
445 }
446 eof {
447 # This is an attempt to detect a core dump, but seems not to
448 # work. Perhaps we need to match .* followed by eof, in which
449 # gdb_expect does not seem to have a way to do that.
450 perror "couldn't load $arg into $GDB (end of file)."
451 return -1
452 }
453 }
454 }
455
456 #
457 # connect to the target and download a file, if necessary.
458 # return a -1 if anything goes wrong.
459 #
460 proc mi_gdb_target_load { } {
461 global verbose
462 global loadpath
463 global loadfile
464 global GDB
465 global mi_gdb_prompt
466
467 if [target_info exists gdb_load_timeout] {
468 set loadtimeout [target_info gdb_load_timeout]
469 } else {
470 set loadtimeout 1600
471 }
472
473 if { [info procs gdbserver_gdb_load] != "" } {
474 mi_gdb_test "kill" ".*" ""
475 set res [gdbserver_gdb_load]
476 set protocol [lindex $res 0]
477 set gdbport [lindex $res 1]
478
479 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
480 return -1
481 }
482 } elseif { [info procs send_target_sid] != "" } {
483 # For SID, things get complex
484 send_gdb "kill\n"
485 gdb_expect 10 {
486 -re ".*$mi_gdb_prompt$"
487 }
488 send_target_sid
489 gdb_expect $loadtimeout {
490 -re "\\^done.*$mi_gdb_prompt$" {
491 }
492 timeout {
493 perror "Unable to connect to SID target (timeout)"
494 return -1
495 }
496 }
497 send_gdb "48-target-download\n"
498 gdb_expect $loadtimeout {
499 -re "48\\^done.*$mi_gdb_prompt$" {
500 }
501 timeout {
502 perror "Unable to download to SID target (timeout)"
503 return -1
504 }
505 }
506 } elseif { [target_info protocol] == "sim" } {
507 # For the simulator, just connect to it directly.
508 send_gdb "47-target-select sim\n"
509 gdb_expect $loadtimeout {
510 -re "47\\^connected.*$mi_gdb_prompt$" {
511 }
512 timeout {
513 perror "Unable to select sim target (timeout)"
514 return -1
515 }
516 }
517 send_gdb "48-target-download\n"
518 gdb_expect $loadtimeout {
519 -re "48\\^done.*$mi_gdb_prompt$" {
520 }
521 timeout {
522 perror "Unable to download to sim target (timeout)"
523 return -1
524 }
525 }
526 } elseif { [target_info gdb_protocol] == "remote" } {
527 # remote targets
528 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
529 perror "Unable to connect to remote target"
530 return -1
531 }
532 send_gdb "48-target-download\n"
533 gdb_expect $loadtimeout {
534 -re "48\\^done.*$mi_gdb_prompt$" {
535 }
536 timeout {
537 perror "Unable to download to remote target (timeout)"
538 return -1
539 }
540 }
541 }
542 return 0
543 }
544
545 #
546 # load a file into the debugger.
547 # return a -1 if anything goes wrong.
548 #
549 proc mi_gdb_load { arg } {
550 if { $arg != "" } {
551 return [mi_gdb_file_cmd $arg]
552 }
553 return 0
554 }
555
556 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
557 # test the result.
558 #
559 # COMMAND is the command to execute, send to GDB with send_gdb. If
560 # this is the null string no command is sent.
561 # PATTERN is the pattern to match for a PASS, and must NOT include
562 # the \r\n sequence immediately before the gdb prompt.
563 # MESSAGE is the message to be printed. (If this is the empty string,
564 # then sometimes we don't call pass or fail at all; I don't
565 # understand this at all.)
566 # IPATTERN is the pattern to match for the inferior's output. This parameter
567 # is optional. If present, it will produce a PASS if the match is
568 # successful, and a FAIL if unsuccessful.
569 #
570 # Returns:
571 # 1 if the test failed,
572 # 0 if the test passes,
573 # -1 if there was an internal error.
574 #
575 proc mi_gdb_test { args } {
576 global verbose
577 global mi_gdb_prompt
578 global GDB expect_out
579 global inferior_exited_re
580 upvar timeout timeout
581
582 set command [lindex $args 0]
583 set pattern [lindex $args 1]
584 set message [lindex $args 2]
585
586 if [llength $args]==4 {
587 set ipattern [lindex $args 3]
588 }
589
590 if [llength $args]==5 {
591 set question_string [lindex $args 3];
592 set response_string [lindex $args 4];
593 } else {
594 set question_string "^FOOBAR$"
595 }
596
597 if $verbose>2 then {
598 send_user "Sending \"$command\" to gdb\n"
599 send_user "Looking to match \"$pattern\"\n"
600 send_user "Message is \"$message\"\n"
601 }
602
603 set result -1
604 set string "${command}\n";
605 set string_regex [string_to_regexp $command]
606
607 if { $command != "" } {
608 while { "$string" != "" } {
609 set foo [string first "\n" "$string"];
610 set len [string length "$string"];
611 if { $foo < [expr $len - 1] } {
612 set str [string range "$string" 0 $foo];
613 if { [send_gdb "$str"] != "" } {
614 global suppress_flag;
615
616 if { ! $suppress_flag } {
617 perror "Couldn't send $command to GDB.";
618 }
619 fail "$message";
620 return $result;
621 }
622 gdb_expect 2 {
623 -re "\[\r\n\]" { }
624 timeout { }
625 }
626 set string [string range "$string" [expr $foo + 1] end];
627 } else {
628 break;
629 }
630 }
631 if { "$string" != "" } {
632 if { [send_gdb "$string"] != "" } {
633 global suppress_flag;
634
635 if { ! $suppress_flag } {
636 perror "Couldn't send $command to GDB.";
637 }
638 fail "$message";
639 return $result;
640 }
641 }
642 }
643
644 if [info exists timeout] {
645 set tmt $timeout;
646 } else {
647 global timeout;
648 if [info exists timeout] {
649 set tmt $timeout;
650 } else {
651 set tmt 60;
652 }
653 }
654 verbose -log "Expecting: ^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)"
655 gdb_expect $tmt {
656 -re "\\*\\*\\* DOSEXIT code.*" {
657 if { $message != "" } {
658 fail "$message";
659 }
660 gdb_suppress_entire_file "GDB died";
661 return -1;
662 }
663 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
664 if ![isnative] then {
665 warning "Can`t communicate to remote target."
666 }
667 gdb_exit
668 gdb_start
669 set result -1
670 }
671 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
672 # At this point, $expect_out(1,string) is the MI input command.
673 # and $expect_out(2,string) is the MI output command.
674 # If $expect_out(1,string) is "", then there was no MI input command here.
675
676 # NOTE, there is no trailing anchor because with GDB/MI,
677 # asynchronous responses can happen at any point, causing more
678 # data to be available. Normally an anchor is used to make
679 # sure the end of the output is matched, however, $mi_gdb_prompt
680 # is just as good of an anchor since mi_gdb_test is meant to
681 # match a single mi output command. If a second GDB/MI output
682 # response is sent, it will be in the buffer for the next
683 # time mi_gdb_test is called.
684 if ![string match "" $message] then {
685 pass "$message"
686 }
687 set result 0
688 }
689 -re "(${question_string})$" {
690 send_gdb "$response_string\n";
691 exp_continue;
692 }
693 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
694 perror "Undefined command \"$command\"."
695 fail "$message"
696 set result 1
697 }
698 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
699 perror "\"$command\" is not a unique command name."
700 fail "$message"
701 set result 1
702 }
703 -re "$inferior_exited_re with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
704 if ![string match "" $message] then {
705 set errmsg "$message (the program exited)"
706 } else {
707 set errmsg "$command (the program exited)"
708 }
709 fail "$errmsg"
710 return -1
711 }
712 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
713 if ![string match "" $message] then {
714 set errmsg "$message (the program is no longer running)"
715 } else {
716 set errmsg "$command (the program is no longer running)"
717 }
718 fail "$errmsg"
719 return -1
720 }
721 -re ".*$mi_gdb_prompt\[ \]*$" {
722 if ![string match "" $message] then {
723 fail "$message"
724 }
725 set result 1
726 }
727 "<return>" {
728 send_gdb "\n"
729 perror "Window too small."
730 fail "$message"
731 }
732 -re "\\(y or n\\) " {
733 send_gdb "n\n"
734 perror "Got interactive prompt."
735 fail "$message"
736 }
737 eof {
738 perror "Process no longer exists"
739 if { $message != "" } {
740 fail "$message"
741 }
742 return -1
743 }
744 full_buffer {
745 perror "internal buffer is full."
746 fail "$message"
747 }
748 timeout {
749 if ![string match "" $message] then {
750 fail "$message (timeout)"
751 }
752 set result 1
753 }
754 }
755
756 # If the GDB output matched, compare the inferior output.
757 if { $result == 0 } {
758 if [ info exists ipattern ] {
759 if { ![target_info exists gdb,noinferiorio] } {
760 global mi_inferior_spawn_id
761 expect {
762 -i $mi_inferior_spawn_id -re "$ipattern" {
763 pass "$message inferior output"
764 }
765 timeout {
766 fail "$message inferior output (timeout)"
767 set result 1
768 }
769 }
770 } else {
771 unsupported "$message inferior output"
772 }
773 }
774 }
775
776 return $result
777 }
778
779 #
780 # MI run command. (A modified version of gdb_run_cmd)
781 #
782
783 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
784 # ``.*$'' could swallow up output that we attempt to match elsewhere.
785
786 proc mi_run_cmd {args} {
787 global suppress_flag
788 if { $suppress_flag } {
789 return -1
790 }
791 global mi_gdb_prompt
792 global thread_selected_re
793 global library_loaded_re
794
795 if [target_info exists gdb_init_command] {
796 send_gdb "[target_info gdb_init_command]\n";
797 gdb_expect 30 {
798 -re "$mi_gdb_prompt$" { }
799 default {
800 perror "gdb_init_command for target failed";
801 return -1;
802 }
803 }
804 }
805
806 if { [mi_gdb_target_load] < 0 } {
807 return -1
808 }
809
810 if [target_info exists use_gdb_stub] {
811 if [target_info exists gdb,do_reload_on_run] {
812 send_gdb "220-exec-continue\n";
813 gdb_expect 60 {
814 -re "220\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\r\n$mi_gdb_prompt" {}
815 default {}
816 }
817 return 0;
818 }
819
820 if [target_info exists gdb,start_symbol] {
821 set start [target_info gdb,start_symbol];
822 } else {
823 set start "start";
824 }
825
826 # HACK: Should either use 000-jump or fix the target code
827 # to better handle RUN.
828 send_gdb "jump *$start\n"
829 warning "Using CLI jump command, expect run-to-main FAIL"
830 return 0
831 }
832
833 send_gdb "220-exec-run $args\n"
834 gdb_expect {
835 -re "220\\^running\r\n(\\*running,thread-id=\"\[^\"\]+\"\r\n|=thread-created,id=\"1\",group-id=\"\[0-9\]+\"\r\n)*(${library_loaded_re})*(${thread_selected_re})?${mi_gdb_prompt}" {
836 }
837 -re "\\^error,msg=\"The target does not support running in non-stop mode.\"" {
838 unsupported "Non-stop mode not supported"
839 return -1
840 }
841 timeout {
842 perror "Unable to start target"
843 return -1
844 }
845 }
846 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
847
848 return 0
849 }
850
851 #
852 # Just like run-to-main but works with the MI interface
853 #
854
855 proc mi_run_to_main { } {
856 global suppress_flag
857 if { $suppress_flag } {
858 return -1
859 }
860
861 global srcdir
862 global subdir
863 global binfile
864 global srcfile
865
866 mi_delete_breakpoints
867 mi_gdb_reinitialize_dir $srcdir/$subdir
868 mi_gdb_load ${binfile}
869
870 mi_runto main
871 }
872
873
874 # Just like gdb's "runto" proc, it will run the target to a given
875 # function. The big difference here between mi_runto and mi_execute_to
876 # is that mi_execute_to must have the inferior running already. This
877 # proc will (like gdb's runto) (re)start the inferior, too.
878 #
879 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
880 # It returns:
881 # -1 if test suppressed, failed, timedout
882 # 0 if test passed
883
884 proc mi_runto_helper {func run_or_continue} {
885 global suppress_flag
886 if { $suppress_flag } {
887 return -1
888 }
889
890 global mi_gdb_prompt expect_out
891 global hex decimal fullname_syntax
892
893 set test "mi runto $func"
894 mi_gdb_test "200-break-insert -t $func" \
895 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\",original-location=\".*\"\}" \
896 "breakpoint at $func"
897
898 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
899 || ![scan $str {number="%d"} bkptno]} {
900 set bkptno {[0-9]+}
901 }
902
903 if {$run_or_continue == "run"} {
904 if { [mi_run_cmd] < 0 } {
905 return -1
906 }
907 } else {
908 mi_send_resuming_command "exec-continue" "$test"
909 }
910
911 mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test
912 }
913
914 proc mi_runto {func} {
915 return [mi_runto_helper $func "run"]
916 }
917
918 # Next to the next statement
919 # For return values, see mi_execute_to_helper
920
921 proc mi_next { test } {
922 return [mi_next_to {.*} {.*} {.*} {.*} $test]
923 }
924
925
926 # Step to the next statement
927 # For return values, see mi_execute_to_helper
928
929 proc mi_step { test } {
930 return [mi_step_to {.*} {.*} {.*} {.*} $test]
931 }
932
933 set async "unknown"
934
935 proc mi_detect_async {} {
936 global async
937 global mi_gdb_prompt
938
939 send_gdb "show target-async\n"
940
941 gdb_expect {
942 -re ".*Controlling the inferior in asynchronous mode is on...*$mi_gdb_prompt$" {
943 set async 1
944 }
945 -re ".*$mi_gdb_prompt$" {
946 set async 0
947 }
948 timeout {
949 set async 0
950 }
951 }
952 return $async
953 }
954
955 # Wait for MI *stopped notification to appear.
956 # The REASON, FUNC, ARGS, FILE and LINE are regular expressions
957 # to match against whatever is output in *stopped. ARGS should
958 # not include [] the list of argument is enclosed in, and other
959 # regular expressions should not include quotes.
960 # If EXTRA is a list of one element, it's the regular expression
961 # for output expected right after *stopped, and before GDB prompt.
962 # If EXTRA is a list of two elements, the first element is for
963 # output right after *stopped, and the second element is output
964 # right after reason field. The regex after reason should not include
965 # the comma separating it from the following fields.
966 #
967 # When we fail to match output at all, -1 is returned. Otherwise,
968 # the line at which we stop is returned. This is useful when exact
969 # line is not possible to specify for some reason -- one can pass
970 # the .* or "\[0-9\]*" regexps for line, and then check the line
971 # programmatically.
972 #
973 # Do not pass .* for any argument if you are expecting more than one stop.
974 proc mi_expect_stop { reason func args file line extra test } {
975
976 global mi_gdb_prompt
977 global hex
978 global decimal
979 global fullname_syntax
980 global async
981 global thread_selected_re
982 global breakpoint_re
983
984 set after_stopped ""
985 set after_reason ""
986 if { [llength $extra] == 2 } {
987 set after_stopped [lindex $extra 0]
988 set after_reason [lindex $extra 1]
989 set after_reason "${after_reason},"
990 } elseif { [llength $extra] == 1 } {
991 set after_stopped [lindex $extra 0]
992 }
993
994 if {$async} {
995 set prompt_re ""
996 } else {
997 set prompt_re "$mi_gdb_prompt$"
998 }
999
1000 if { $reason == "really-no-reason" } {
1001 gdb_expect {
1002 -re "\\*stopped\r\n$prompt_re" {
1003 pass "$test"
1004 }
1005 timeout {
1006 fail "$test (unknown output after running)"
1007 }
1008 }
1009 return
1010 }
1011
1012 if { $reason == "exited-normally" } {
1013
1014 gdb_expect {
1015 -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re" {
1016 pass "$test"
1017 }
1018 -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
1019 timeout {
1020 fail "$test (unknown output after running)"
1021 }
1022 }
1023 return
1024 }
1025
1026 set args "\\\[$args\\\]"
1027
1028 set bn ""
1029 if { $reason == "breakpoint-hit" } {
1030 set bn {bkptno="[0-9]+",}
1031 }
1032
1033 set r ""
1034 if { $reason != "" } {
1035 set r "reason=\"$reason\","
1036 }
1037
1038
1039 set a $after_reason
1040
1041 set any "\[^\n\]*"
1042
1043 verbose -log "mi_expect_stop: expecting: \\*stopped,${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\"\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1044 gdb_expect {
1045 -re "\\*stopped,${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\"\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1046 pass "$test"
1047 return $expect_out(2,string)
1048 }
1049 -re "\\*stopped,${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$any\",args=\[\\\[\{\]$any\[\\\]\}\],file=\"$any\",fullname=\"${fullname_syntax}$any\",line=\"\[0-9\]*\"\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1050 verbose -log "got $expect_out(buffer)"
1051 fail "$test (stopped at wrong place)"
1052 return -1
1053 }
1054 -re ".*\r\n$mi_gdb_prompt$" {
1055 verbose -log "got $expect_out(buffer)"
1056 fail "$test (unknown output after running)"
1057 return -1
1058 }
1059 timeout {
1060 fail "$test (timeout)"
1061 return -1
1062 }
1063 }
1064 }
1065
1066 # Wait for MI *stopped notification related to an interrupt request to
1067 # appear.
1068 proc mi_expect_interrupt { test } {
1069 global mi_gdb_prompt
1070 global decimal
1071 global async
1072
1073 if {$async} {
1074 set prompt_re ""
1075 } else {
1076 set prompt_re "$mi_gdb_prompt$"
1077 }
1078
1079 set r "reason=\"signal-received\",signal-name=\"0\",signal-meaning=\"Signal 0\""
1080
1081 set any "\[^\n\]*"
1082
1083 # A signal can land anywhere, just ignore the location
1084 verbose -log "mi_expect_interrupt: expecting: \\*stopped,${r}$any\r\n$prompt_re"
1085 gdb_expect {
1086 -re "\\*stopped,${r}$any\r\n$prompt_re" {
1087 pass "$test"
1088 return 0;
1089 }
1090 -re ".*\r\n$mi_gdb_prompt$" {
1091 verbose -log "got $expect_out(buffer)"
1092 fail "$test (unknown output after running)"
1093 return -1
1094 }
1095 timeout {
1096 fail "$test (timeout)"
1097 return -1
1098 }
1099 }
1100 }
1101
1102 # cmd should not include the number or newline (i.e. "exec-step 3", not
1103 # "220-exec-step 3\n"
1104
1105 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1106 # after the first prompt is printed.
1107
1108 proc mi_execute_to { cmd reason func args file line extra test } {
1109 global suppress_flag
1110 if { $suppress_flag } {
1111 return -1
1112 }
1113
1114 mi_send_resuming_command "$cmd" "$test"
1115 set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1116 return $r
1117 }
1118
1119 proc mi_next_to { func args file line test } {
1120 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1121 "$file" "$line" "" "$test"
1122 }
1123
1124 proc mi_step_to { func args file line test } {
1125 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1126 "$file" "$line" "" "$test"
1127 }
1128
1129 proc mi_finish_to { func args file line result ret test } {
1130 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
1131 "$file" "$line" \
1132 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1133 "$test"
1134 }
1135
1136 proc mi_continue_to {func} {
1137 mi_runto_helper $func "continue"
1138 }
1139
1140 proc mi0_execute_to { cmd reason func args file line extra test } {
1141 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
1142 "$file" "$line" "$extra" "$test"
1143 }
1144
1145 proc mi0_next_to { func args file line test } {
1146 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1147 "$file" "$line" "" "$test"
1148 }
1149
1150 proc mi0_step_to { func args file line test } {
1151 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1152 "$file" "$line" "" "$test"
1153 }
1154
1155 proc mi0_finish_to { func args file line result ret test } {
1156 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1157 "$file" "$line" \
1158 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1159 "$test"
1160 }
1161
1162 proc mi0_continue_to { bkptno func args file line test } {
1163 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1164 "$func" "$args" "$file" "$line" "" "$test"
1165 }
1166
1167 # Creates a breakpoint and checks the reported fields are as expected
1168 proc mi_create_breakpoint { location number disp func file line address test } {
1169 verbose -log "Expecting: 222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}"
1170 mi_gdb_test "222-break-insert $location" \
1171 "222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}" \
1172 $test
1173 }
1174
1175 proc mi_list_breakpoints { expected test } {
1176 set fullname ".*"
1177
1178 set body ""
1179 set first 1
1180
1181 foreach item $expected {
1182 if {$first == 0} {
1183 set body "$body,"
1184 set first 0
1185 }
1186 set number [lindex $item 0]
1187 set disp [lindex $item 1]
1188 set func [lindex $item 2]
1189 set file [lindex $item 3]
1190 set line [lindex $item 4]
1191 set address [lindex $item 5]
1192 set body "${body}bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\".*$file\",${fullname},line=\"$line\",times=\"0\",original-location=\".*\"\}"
1193 set first 0
1194 }
1195
1196 verbose -log "Expecting: 666\\\^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=\\\[$body\\\]\}"
1197 mi_gdb_test "666-break-list" \
1198 "666\\\^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=\\\[$body\\\]\}" \
1199 $test
1200 }
1201
1202 # Creates varobj named NAME for EXPRESSION.
1203 # Name cannot be "-".
1204 proc mi_create_varobj { name expression testname } {
1205 mi_gdb_test "-var-create $name * $expression" \
1206 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*,has_more=\"0\"" \
1207 $testname
1208 }
1209
1210 proc mi_create_floating_varobj { name expression testname } {
1211 mi_gdb_test "-var-create $name @ $expression" \
1212 "\\^done,name=\"$name\",numchild=\"\(-1\|\[0-9\]+\)\",value=\".*\",type=.*" \
1213 $testname
1214 }
1215
1216
1217 # Same as mi_create_varobj, but also checks the reported type
1218 # of the varobj.
1219 proc mi_create_varobj_checked { name expression type testname } {
1220 mi_gdb_test "-var-create $name * $expression" \
1221 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1222 $testname
1223 }
1224
1225 # Same as mi_create_floating_varobj, but assumes the test is creating
1226 # a dynamic varobj that has children, so the value must be "{...}".
1227 proc mi_create_dynamic_varobj {name expression testname} {
1228 mi_gdb_test "-var-create $name @ $expression" \
1229 "\\^done,name=\"$name\",numchild=\"\(-1\|\[0-9\]+\)\",value=\"{\\.\\.\\.}\",type=.*" \
1230 $testname
1231 }
1232
1233 # Deletes the specified NAME.
1234 proc mi_delete_varobj { name testname } {
1235 mi_gdb_test "-var-delete $name" \
1236 "\\^done,ndeleted=.*" \
1237 $testname
1238 }
1239
1240 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1241 # are reported as updated, and no other varobj is updated.
1242 # Assumes that no varobj is out of scope and that no varobj changes
1243 # types.
1244 proc mi_varobj_update { name expected testname } {
1245 set er "\\^done,changelist=\\\["
1246 set first 1
1247 foreach item $expected {
1248 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\",has_more=\".\"}"
1249 if {$first == 1} {
1250 set er "$er$v"
1251 set first 0
1252 } else {
1253 set er "$er,$v"
1254 }
1255 }
1256 set er "$er\\\]"
1257
1258 verbose -log "Expecting: $er" 2
1259 mi_gdb_test "-var-update $name" $er $testname
1260 }
1261
1262 proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1263 set v "{name=\"$name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\",has_more=\".\"}"
1264 set er "\\^done,changelist=\\\[$v\\\]"
1265 verbose -log "Expecting: $er"
1266 mi_gdb_test "-var-update $name" $er $testname
1267 }
1268
1269 # A helper that turns a key/value list into a regular expression
1270 # matching some MI output.
1271 proc mi_varobj_update_kv_helper {list} {
1272 set first 1
1273 set rx ""
1274 foreach {key value} $list {
1275 if {!$first} {
1276 append rx ,
1277 }
1278 set first 0
1279 if {$key == "new_children"} {
1280 append rx "$key=\\\[$value\\\]"
1281 } else {
1282 append rx "$key=\"$value\""
1283 }
1284 }
1285 return $rx
1286 }
1287
1288 # A helper for mi_varobj_update_dynamic that computes a match
1289 # expression given a child list.
1290 proc mi_varobj_update_dynamic_helper {children} {
1291 set crx ""
1292
1293 set first 1
1294 foreach child $children {
1295 if {!$first} {
1296 append crx ,
1297 }
1298 set first 0
1299 append crx "{"
1300 append crx [mi_varobj_update_kv_helper $child]
1301 append crx "}"
1302 }
1303
1304 return $crx
1305 }
1306
1307 # Update a dynamic varobj named NAME. CHILDREN is a list of children
1308 # that have been updated; NEW_CHILDREN is a list of children that were
1309 # added to the primary varobj. Each child is a list of key/value
1310 # pairs that are expected. SELF is a key/value list holding
1311 # information about the varobj itself. TESTNAME is the name of the
1312 # test.
1313 proc mi_varobj_update_dynamic {name testname self children new_children} {
1314 if {[llength $new_children]} {
1315 set newrx [mi_varobj_update_dynamic_helper $new_children]
1316 lappend self new_children $newrx
1317 }
1318 set selfrx [mi_varobj_update_kv_helper $self]
1319 set crx [mi_varobj_update_dynamic_helper $children]
1320
1321 set er "\\^done,changelist=\\\[\{name=\"$name\",in_scope=\"true\""
1322 append er ",$selfrx\}"
1323 if {"$crx" != ""} {
1324 append er ",$crx"
1325 }
1326 append er "\\\]"
1327
1328 verbose -log "Expecting: $er"
1329 mi_gdb_test "-var-update $name" $er $testname
1330 }
1331
1332 proc mi_check_varobj_value { name value testname } {
1333
1334 mi_gdb_test "-var-evaluate-expression $name" \
1335 "\\^done,value=\"$value\"" \
1336 $testname
1337 }
1338
1339 # Helper proc which constructs a child regexp for
1340 # mi_list_varobj_children and mi_varobj_update_dynamic.
1341 proc mi_child_regexp {children add_child} {
1342 set children_exp {}
1343 set whatever "\"\[^\"\]+\""
1344
1345 if {$add_child} {
1346 set pre "child="
1347 } else {
1348 set pre ""
1349 }
1350
1351 foreach item $children {
1352
1353 set name [lindex $item 0]
1354 set exp [lindex $item 1]
1355 set numchild [lindex $item 2]
1356 if {[llength $item] == 5} {
1357 set type [lindex $item 3]
1358 set value [lindex $item 4]
1359
1360 lappend children_exp\
1361 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
1362 } elseif {[llength $item] == 4} {
1363 set type [lindex $item 3]
1364
1365 lappend children_exp\
1366 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
1367 } else {
1368 lappend children_exp\
1369 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
1370 }
1371 }
1372 return [join $children_exp ","]
1373 }
1374
1375 # Check the results of the:
1376 #
1377 # -var-list-children VARNAME
1378 #
1379 # command. The CHILDREN parement should be a list of lists.
1380 # Each inner list can have either 3 or 4 elements, describing
1381 # fields that gdb is expected to report for child variable object,
1382 # in the following order
1383 #
1384 # - Name
1385 # - Expression
1386 # - Number of children
1387 # - Type
1388 #
1389 # If inner list has 3 elements, the gdb is expected to output no
1390 # type for a child and no value.
1391 #
1392 # If the inner list has 4 elements, gdb output is expected to
1393 # have no value.
1394 #
1395 proc mi_list_varobj_children { varname children testname } {
1396 mi_list_varobj_children_range $varname "" "" [llength $children] $children \
1397 $testname
1398 }
1399
1400 # Like mi_list_varobj_children, but sets a subrange. NUMCHILDREN is
1401 # the total number of children.
1402 proc mi_list_varobj_children_range {varname from to numchildren children testname} {
1403 set options ""
1404 if {[llength $varname] == 2} {
1405 set options [lindex $varname 1]
1406 set varname [lindex $varname 0]
1407 }
1408
1409 set whatever "\"\[^\"\]+\""
1410
1411 set children_exp_j [mi_child_regexp $children 1]
1412 if {$numchildren} {
1413 set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1414 } {
1415 set expected "\\^done,numchild=\"0\""
1416 }
1417
1418 if {"$to" == ""} {
1419 append expected ",has_more=\"0\""
1420 } elseif {$to >= 0 && $numchildren > $to} {
1421 append expected ",has_more=\"1\""
1422 } else {
1423 append expected ",has_more=\"0\""
1424 }
1425
1426 verbose -log "Expecting: $expected"
1427
1428 mi_gdb_test "-var-list-children $options $varname $from $to" \
1429 $expected $testname
1430 }
1431
1432 # Verifies that variable object VARNAME has NUMBER children,
1433 # where each one is named $VARNAME.<index-of-child> and has type TYPE.
1434 proc mi_list_array_varobj_children { varname number type testname } {
1435 set t {}
1436 for {set i 0} {$i < $number} {incr i} {
1437 lappend t [list $varname.$i $i 0 $type]
1438 }
1439 mi_list_varobj_children $varname $t $testname
1440 }
1441
1442 # A list of two-element lists. First element of each list is
1443 # a Tcl statement, and the second element is the line
1444 # number of source C file where the statement originates.
1445 set mi_autotest_data ""
1446 # The name of the source file for autotesting.
1447 set mi_autotest_source ""
1448
1449 proc count_newlines { string } {
1450 return [regexp -all "\n" $string]
1451 }
1452
1453 # Prepares for running inline tests in FILENAME.
1454 # See comments for mi_run_inline_test for detailed
1455 # explanation of the idea and syntax.
1456 proc mi_prepare_inline_tests { filename } {
1457
1458 global srcdir
1459 global subdir
1460 global mi_autotest_source
1461 global mi_autotest_data
1462
1463 set mi_autotest_data {}
1464
1465 set mi_autotest_source $filename
1466
1467 if { ! [regexp "^/" "$filename"] } then {
1468 set filename "$srcdir/$subdir/$filename"
1469 }
1470
1471 set chan [open $filename]
1472 set content [read $chan]
1473 set line_number 1
1474 while {1} {
1475 set start [string first "/*:" $content]
1476 if {$start != -1} {
1477 set end [string first ":*/" $content]
1478 if {$end == -1} {
1479 error "Unterminated special comment in $filename"
1480 }
1481
1482 set prefix [string range $content 0 $start]
1483 set prefix_newlines [count_newlines $prefix]
1484
1485 set line_number [expr $line_number+$prefix_newlines]
1486 set comment_line $line_number
1487
1488 set comment [string range $content [expr $start+3] [expr $end-1]]
1489
1490 set comment_newlines [count_newlines $comment]
1491 set line_number [expr $line_number+$comment_newlines]
1492
1493 set comment [string trim $comment]
1494 set content [string range $content [expr $end+3] \
1495 [string length $content]]
1496 lappend mi_autotest_data [list $comment $comment_line]
1497 } else {
1498 break
1499 }
1500 }
1501 close $chan
1502 }
1503
1504 # Helper to mi_run_inline_test below.
1505 # Return the list of all (statement,line_number) lists
1506 # that comprise TESTCASE. The begin and end markers
1507 # are not included.
1508 proc mi_get_inline_test {testcase} {
1509
1510 global mi_gdb_prompt
1511 global mi_autotest_data
1512 global mi_autotest_source
1513
1514 set result {}
1515
1516 set seen_begin 0
1517 set seen_end 0
1518 foreach l $mi_autotest_data {
1519
1520 set comment [lindex $l 0]
1521
1522 if {$comment == "BEGIN: $testcase"} {
1523 set seen_begin 1
1524 } elseif {$comment == "END: $testcase"} {
1525 set seen_end 1
1526 break
1527 } elseif {$seen_begin==1} {
1528 lappend result $l
1529 }
1530 }
1531
1532 if {$seen_begin == 0} {
1533 error "Autotest $testcase not found"
1534 }
1535
1536 if {$seen_begin == 1 && $seen_end == 0} {
1537 error "Missing end marker for test $testcase"
1538 }
1539
1540 return $result
1541 }
1542
1543 # Sets temporary breakpoint at LOCATION.
1544 proc mi_tbreak {location} {
1545
1546 global mi_gdb_prompt
1547
1548 mi_gdb_test "-break-insert -t $location" \
1549 {\^done,bkpt=.*} \
1550 "run to $location (set breakpoint)"
1551 }
1552
1553 # Send COMMAND that must be a command that resumes
1554 # the inferiour (run/continue/next/etc) and consumes
1555 # the "^running" output from it.
1556 proc mi_send_resuming_command_raw {command test} {
1557
1558 global mi_gdb_prompt
1559 global thread_selected_re
1560 global library_loaded_re
1561
1562 send_gdb "$command\n"
1563 gdb_expect {
1564 -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n($library_loaded_re)*($thread_selected_re)?${mi_gdb_prompt}" {
1565 # Note that lack of 'pass' call here -- this works around limitation
1566 # in DejaGNU xfail mechanism. mi-until.exp has this:
1567 #
1568 # setup_kfail gdb/2104 "*-*-*"
1569 # mi_execute_to ...
1570 #
1571 # and mi_execute_to uses mi_send_resuming_command. If we use 'pass' here,
1572 # it will reset kfail, so when the actual test fails, it will be flagged
1573 # as real failure.
1574 return 0
1575 }
1576 -re "\\^error,msg=\"Displaced stepping is only supported in ARM mode\".*" {
1577 unsupported "$test (Thumb mode)"
1578 return -1
1579 }
1580 -re "\\^error,msg=.*" {
1581 fail "$test (MI error)"
1582 return -1
1583 }
1584 -re ".*${mi_gdb_prompt}" {
1585 fail "$test (failed to resume)"
1586 return -1
1587 }
1588 timeout {
1589 fail "$test"
1590 return -1
1591 }
1592 }
1593 }
1594
1595 proc mi_send_resuming_command {command test} {
1596 mi_send_resuming_command_raw -$command $test
1597 }
1598
1599 # Helper to mi_run_inline_test below.
1600 # Sets a temporary breakpoint at LOCATION and runs
1601 # the program using COMMAND. When the program is stopped
1602 # returns the line at which it. Returns -1 if line cannot
1603 # be determined.
1604 # Does not check that the line is the same as requested.
1605 # The caller can check itself if required.
1606 proc mi_continue_to_line {location test} {
1607
1608 mi_tbreak $location
1609 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1610 return [mi_get_stop_line $test]
1611 }
1612
1613 # Wait until gdb prints the current line.
1614 proc mi_get_stop_line {test} {
1615
1616 global mi_gdb_prompt
1617 global async
1618
1619 if {$async} {
1620 set prompt_re ""
1621 } else {
1622 set prompt_re "$mi_gdb_prompt$"
1623 }
1624
1625 gdb_expect {
1626 -re ".*line=\"(\[0-9\]*)\".*\r\n$prompt_re" {
1627 return $expect_out(1,string)
1628 }
1629 -re ".*$mi_gdb_prompt" {
1630 fail "wait for stop ($test)"
1631 }
1632 timeout {
1633 fail "wait for stop ($test)"
1634 }
1635 }
1636 }
1637
1638 # Run a MI test embedded in comments in a C file.
1639 # The C file should contain special comments in the following
1640 # three forms:
1641 #
1642 # /*: BEGIN: testname :*/
1643 # /*: <Tcl statements> :*/
1644 # /*: END: testname :*/
1645 #
1646 # This procedure find the begin and end marker for the requested
1647 # test. Then, a temporary breakpoint is set at the begin
1648 # marker and the program is run (from start).
1649 #
1650 # After that, for each special comment between the begin and end
1651 # marker, the Tcl statements are executed. It is assumed that
1652 # for each comment, the immediately preceding line is executable
1653 # C statement. Then, gdb will be single-stepped until that
1654 # preceding C statement is executed, and after that the
1655 # Tcl statements in the comment will be executed.
1656 #
1657 # For example:
1658 #
1659 # /*: BEGIN: assignment-test :*/
1660 # v = 10;
1661 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1662 # /*: END: assignment-test :*/
1663 #
1664 # The mi_prepare_inline_tests function should be called before
1665 # calling this function. A given C file can contain several
1666 # inline tests. The names of the tests must be unique within one
1667 # C file.
1668 #
1669 proc mi_run_inline_test { testcase } {
1670
1671 global mi_gdb_prompt
1672 global hex
1673 global decimal
1674 global fullname_syntax
1675 global mi_autotest_source
1676
1677 set commands [mi_get_inline_test $testcase]
1678
1679 set first 1
1680 set line_now 1
1681
1682 foreach c $commands {
1683 set statements [lindex $c 0]
1684 set line [lindex $c 1]
1685 set line [expr $line-1]
1686
1687 # We want gdb to be stopped at the expression immediately
1688 # before the comment. If this is the first comment, the
1689 # program is either not started yet or is in some random place,
1690 # so we run it. For further comments, we might be already
1691 # standing at the right line. If not continue till the
1692 # right line.
1693
1694 if {$first==1} {
1695 # Start the program afresh.
1696 mi_tbreak "$mi_autotest_source:$line"
1697 mi_run_cmd
1698 set line_now [mi_get_stop_line "$testcase: step to $line"]
1699 set first 0
1700 } elseif {$line_now!=$line} {
1701 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1702 }
1703
1704 if {$line_now!=$line} {
1705 fail "$testcase: go to line $line"
1706 }
1707
1708 # We're not at the statement right above the comment.
1709 # Execute that statement so that the comment can test
1710 # the state after the statement is executed.
1711
1712 # Single-step past the line.
1713 if { [mi_send_resuming_command "exec-next" "$testcase: step over $line"] != 0 } {
1714 return -1
1715 }
1716 set line_now [mi_get_stop_line "$testcase: step over $line"]
1717
1718 # We probably want to use 'uplevel' so that statements
1719 # have direct access to global variables that the
1720 # main 'exp' file has set up. But it's not yet clear,
1721 # will need more experience to be sure.
1722 eval $statements
1723 }
1724 }
1725
1726 proc get_mi_thread_list {name} {
1727 global expect_out
1728
1729 # MI will return a list of thread ids:
1730 #
1731 # -thread-list-ids
1732 # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
1733 # (gdb)
1734 mi_gdb_test "-thread-list-ids" \
1735 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1736 "-thread_list_ids ($name)"
1737
1738 set output {}
1739 if {[info exists expect_out(buffer)]} {
1740 set output $expect_out(buffer)
1741 }
1742
1743 set thread_list {}
1744 if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
1745 fail "finding threads in MI output ($name)"
1746 } else {
1747 pass "finding threads in MI output ($name)"
1748
1749 # Make list of console threads
1750 set start [expr {[string first \{ $threads] + 1}]
1751 set end [expr {[string first \} $threads] - 1}]
1752 set threads [string range $threads $start $end]
1753 foreach thread [split $threads ,] {
1754 if {[scan $thread {thread-id="%d"} num]} {
1755 lappend thread_list $num
1756 }
1757 }
1758 }
1759
1760 return $thread_list
1761 }
1762
1763 # Check that MI and the console know of the same threads.
1764 # Appends NAME to all test names.
1765 proc check_mi_and_console_threads {name} {
1766 global expect_out
1767
1768 mi_gdb_test "-thread-list-ids" \
1769 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1770 "-thread-list-ids ($name)"
1771 set mi_output {}
1772 if {[info exists expect_out(buffer)]} {
1773 set mi_output $expect_out(buffer)
1774 }
1775
1776 # GDB will return a list of thread ids and some more info:
1777 #
1778 # (gdb)
1779 # -interpreter-exec console "info threads"
1780 # ~" 4 Thread 2051 (LWP 7734) 0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
1781 # ~" 3 Thread 1026 (LWP 7733) () at __libc_nanosleep:-1"
1782 # ~" 2 Thread 2049 (LWP 7732) 0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
1783 # ~"* 1 Thread 1024 (LWP 7731) main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
1784 # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
1785 mi_gdb_test "info threads" \
1786 {.*(~".*"[\r\n]*)+.*} \
1787 "info threads ($name)"
1788 set console_output {}
1789 if {[info exists expect_out(buffer)]} {
1790 set console_output $expect_out(buffer)
1791 }
1792
1793 # Make a list of all known threads to console (gdb's thread IDs)
1794 set console_thread_list {}
1795 foreach line [split $console_output \n] {
1796 if {[string index $line 0] == "~"} {
1797 # This is a line from the console; trim off "~", " ", "*", and "\""
1798 set line [string trim $line ~\ \"\*]
1799 if {[scan $line "%d" id] == 1} {
1800 lappend console_thread_list $id
1801 }
1802 }
1803 }
1804
1805 # Now find the result string from MI
1806 set mi_result ""
1807 foreach line [split $mi_output \n] {
1808 if {[string range $line 0 4] == "^done"} {
1809 set mi_result $line
1810 }
1811 }
1812 if {$mi_result == ""} {
1813 fail "finding MI result string ($name)"
1814 } else {
1815 pass "finding MI result string ($name)"
1816 }
1817
1818 # Finally, extract the thread ids and compare them to the console
1819 set num_mi_threads_str ""
1820 if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
1821 fail "finding number of threads in MI output ($name)"
1822 } else {
1823 pass "finding number of threads in MI output ($name)"
1824
1825 # Extract the number of threads from the MI result
1826 if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
1827 fail "got number of threads from MI ($name)"
1828 } else {
1829 pass "got number of threads from MI ($name)"
1830
1831 # Check if MI and console have same number of threads
1832 if {$num_mi_threads != [llength $console_thread_list]} {
1833 fail "console and MI have same number of threads ($name)"
1834 } else {
1835 pass "console and MI have same number of threads ($name)"
1836
1837 # Get MI thread list
1838 set mi_thread_list [get_mi_thread_list $name]
1839
1840 # Check if MI and console have the same threads
1841 set fails 0
1842 foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
1843 if {$ct != $mt} {
1844 incr fails
1845 }
1846 }
1847 if {$fails > 0} {
1848 fail "MI and console have same threads ($name)"
1849
1850 # Send a list of failures to the log
1851 send_log "Console has thread ids: $console_thread_list\n"
1852 send_log "MI has thread ids: $mi_thread_list\n"
1853 } else {
1854 pass "MI and console have same threads ($name)"
1855 }
1856 }
1857 }
1858 }
1859 }
1860
1861 # Download shared libraries to the target.
1862 proc mi_load_shlibs { args } {
1863 if {![is_remote target]} {
1864 return
1865 }
1866
1867 foreach file $args {
1868 gdb_download [shlib_target_file $file]
1869 }
1870
1871 # Even if the target supplies full paths for shared libraries,
1872 # they may not be paths for this system.
1873 mi_gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "\^done" ""
1874 }
1875
1876 proc mi_reverse_list { list } {
1877 if { [llength $list] <= 1 } {
1878 return $list
1879 }
1880 set tail [lrange $list 1 [llength $list]]
1881 set rtail [mi_reverse_list $tail]
1882 lappend rtail [lindex $list 0]
1883 return $rtail
1884 }
1885
1886 proc mi_check_thread_states { xstates test } {
1887 global expect_out
1888 set states [mi_reverse_list $xstates]
1889 set pattern ".*\\^done,threads=\\\["
1890 foreach s $states {
1891 set pattern "${pattern}(.*)state=\"$s\""
1892 }
1893 set pattern "${pattern}(,core=\"\[0-9\]*\")?\\\}\\\].*"
1894
1895 verbose -log "expecting: $pattern"
1896 mi_gdb_test "-thread-info" $pattern $test
1897 }
1898
1899 # Return a list of MI features supported by this gdb.
1900 proc mi_get_features {} {
1901 global expect_out mi_gdb_prompt
1902
1903 send_gdb "-list-features\n"
1904
1905 gdb_expect {
1906 -re "\\^done,features=\\\[(.*)\\\]\r\n$mi_gdb_prompt$" {
1907 regsub -all -- \" $expect_out(1,string) "" features
1908 return [split $features ,]
1909 }
1910 -re ".*\r\n$mi_gdb_prompt$" {
1911 verbose -log "got $expect_out(buffer)"
1912 return ""
1913 }
1914 timeout {
1915 verbose -log "timeout in mi_gdb_prompt"
1916 return ""
1917 }
1918 }
1919 }