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