Put pretty-printers to_string output in varobj result
[binutils-gdb.git] / gdb / testsuite / lib / mi-support.exp
1 # Copyright 1999-2023 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # This file was based on a file written by Fred Fish. (fnf@cygnus.com)
17
18 # Test setup routines that work with the MI interpreter.
19
20 load_lib gdb-utils.exp
21
22 # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
23 # Set it if it is not already set.
24 global mi_gdb_prompt
25 if {![info exists mi_gdb_prompt]} {
26 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
27 }
28
29 global mi_inferior_tty_name
30
31 # Always points to GDB's main UI spawn ID, even when testing with MI
32 # running on a secondary UI.
33 global gdb_main_spawn_id
34
35 # Points to the spawn id of the MI channel. When testing with MI
36 # running as the primary/main UI, this is the same as
37 # gdb_main_spawn_id, but will be different when testing with MI
38 # running on a secondary UI.
39 global mi_spawn_id
40
41 set MIFLAGS "-i=mi"
42
43 set thread_selected_re "=thread-selected,id=\"\[0-9\]+\"\r\n"
44 set gdbindex_warning_re "&\"warning: Skipping \[^\r\n\]+ \.gdb_index section in \[^\r\n\]+\"\r\n(?:&\"\\\\n\"\r\n)?"
45 set library_loaded_re "=library-loaded\[^\n\]+\"\r\n(?:$gdbindex_warning_re)?"
46 set breakpoint_re "=(?:breakpoint-created|breakpoint-deleted)\[^\n\]+\"\r\n"
47
48 #
49 # mi_gdb_exit -- exit the GDB, killing the target program if necessary
50 #
51 proc mi_gdb_exit {} {
52 catch mi_uncatched_gdb_exit
53 }
54
55 proc mi_uncatched_gdb_exit {} {
56 global GDB
57 global INTERNAL_GDBFLAGS GDBFLAGS
58 global gdb_spawn_id gdb_main_spawn_id
59 global mi_spawn_id inferior_spawn_id
60 global gdb_prompt
61 global mi_gdb_prompt
62 global MIFLAGS
63
64 if { [info procs sid_exit] != "" } {
65 sid_exit
66 }
67
68 if ![info exists gdb_spawn_id] {
69 return
70 }
71
72 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
73
74 if { [is_remote host] && [board_info host exists fileid] } {
75 send_gdb "999-gdb-exit\n"
76 gdb_expect 10 {
77 -re "y or n" {
78 send_gdb "y\n"
79 exp_continue
80 }
81 -re "Undefined command.*$gdb_prompt $" {
82 send_gdb "quit\n"
83 exp_continue
84 }
85 -re "DOSEXIT code" { }
86 -re "\r\n999\\^exit\r\n" { }
87 }
88 }
89
90 # Switch back to the main spawn id, so that remote_close below
91 # closes it, and not a secondary channel. Closing a secondary
92 # channel does not make GDB exit.
93 if {$gdb_spawn_id != $gdb_main_spawn_id} {
94 switch_gdb_spawn_id $gdb_main_spawn_id
95 }
96
97 # Close secondary MI channel, if there's one.
98 if {$mi_spawn_id != $gdb_main_spawn_id} {
99 close -i $mi_spawn_id
100 }
101
102 if ![is_remote host] {
103 remote_close host
104 }
105 unset gdb_spawn_id
106 unset gdb_main_spawn_id
107 unset mi_spawn_id
108 unset inferior_spawn_id
109 }
110
111 # Create the PTY for the inferior process and tell GDB about it.
112
113 proc mi_create_inferior_pty {} {
114 global mi_gdb_prompt
115 global inferior_spawn_id
116 global mi_inferior_tty_name
117
118 spawn -pty
119 set inferior_spawn_id $spawn_id
120 set tty_name $spawn_out(slave,name)
121 set mi_inferior_tty_name $tty_name
122
123 send_gdb "102-inferior-tty-set $tty_name\n"
124 gdb_expect 10 {
125 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
126 verbose "redirect inferior output to new terminal device."
127 }
128 timeout {
129 warning "Couldn't redirect inferior output." 2
130 }
131 }
132 }
133
134 #
135 # Like default_mi_gdb_start below, but the MI is created as a separate
136 # ui in a new tty. The global MI_SPAWN_ID is updated to point at the
137 # new tty created for the MI interface. The global GDB_MAIN_SPAWN_ID
138 # is updated to the current value of the global GDB_SPAWN_ID.
139 #
140 proc mi_gdb_start_separate_mi_tty { { flags {} } } {
141 global gdb_prompt mi_gdb_prompt
142 global timeout
143 global gdb_spawn_id gdb_main_spawn_id mi_spawn_id
144 global inferior_spawn_id
145
146 set separate_inferior_pty 0
147
148 foreach flag $flags {
149 if {$flag == "separate-inferior-tty"} {
150 set separate_inferior_pty 1
151 }
152 }
153
154 gdb_start
155
156 # Create the new PTY for the MI UI.
157 spawn -pty
158 set mi_spawn_id $spawn_id
159 set mi_tty_name $spawn_out(slave,name)
160 gdb_test_multiple "new-ui mi $mi_tty_name" "new-ui" {
161 -re "New UI allocated\r\n$gdb_prompt $" {
162 }
163 }
164
165 # Switch to the MI channel.
166 set gdb_main_spawn_id $gdb_spawn_id
167 switch_gdb_spawn_id $mi_spawn_id
168
169 # Consume pending output and MI prompt.
170 gdb_expect {
171 -re "$mi_gdb_prompt$" {
172 }
173 default {
174 perror "MI channel failed"
175 remote_close host
176 return -1
177 }
178 }
179
180 if {$separate_inferior_pty} {
181 mi_create_inferior_pty
182 }
183
184 mi_detect_async
185
186 return 0
187 }
188
189 #
190 # default_mi_gdb_start [FLAGS] -- start gdb running, default procedure
191 #
192 # FLAGS is a list of flags, each flag is a string.
193 #
194 # If "separate-inferior-tty" is specified, the inferior works with
195 # it's own PTY.
196 #
197 # If "separate-mi-tty" is specified, the gdb starts in CLI mode, with
198 # MI running on a secondary UI, on its own tty.
199 #
200 # When running over NFS, particularly if running many simultaneous
201 # tests on different hosts all using the same server, things can
202 # get really slow. Give gdb at least 3 minutes to start up.
203 #
204 proc default_mi_gdb_start { { flags {} } } {
205 global use_gdb_stub
206 global GDB
207 global INTERNAL_GDBFLAGS GDBFLAGS
208 global gdb_prompt
209 global mi_gdb_prompt
210 global timeout
211 global gdb_spawn_id gdb_main_spawn_id inferior_spawn_id mi_spawn_id
212 global MIFLAGS
213 global FORCE_SEPARATE_MI_TTY
214
215 # Keep track of the number of times GDB has been launched.
216 global gdb_instances
217 incr gdb_instances
218
219 gdb_stdin_log_init
220
221 if {[info exists FORCE_SEPARATE_MI_TTY]} {
222 set separate_mi_pty $FORCE_SEPARATE_MI_TTY
223 } else {
224 set separate_mi_pty 0
225 }
226
227 set separate_inferior_pty 0
228
229 foreach flag $flags {
230 if {$flag == "separate-mi-tty"} {
231 set separate_mi_pty 1
232 } elseif {$flag == "separate-inferior-tty"} {
233 set separate_inferior_pty 1
234 }
235 }
236
237 if {$separate_mi_pty} {
238 return [mi_gdb_start_separate_mi_tty $flags]
239 }
240
241 # Set the default value, it may be overriden later by specific testfile.
242 set use_gdb_stub [target_info exists use_gdb_stub]
243
244 # Start SID.
245 if { [info procs sid_start] != "" } {
246 verbose "Spawning SID"
247 sid_start
248 }
249
250 if [info exists gdb_spawn_id] {
251 return 0
252 }
253
254 save_vars { GDBFLAGS } {
255 append GDBFLAGS " $MIFLAGS"
256
257 set res [gdb_spawn]
258 if { $res != 0} {
259 return $res
260 }
261 }
262
263 gdb_expect {
264 -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
265 # We have a new format mi startup prompt.
266 verbose "GDB initialized."
267 }
268 -re ".*unrecognized option.*for a complete list of options." {
269 untested "skip mi tests (not compiled with mi support)."
270 remote_close host
271 unset gdb_spawn_id
272 return -1
273 }
274 -re ".*Interpreter `mi' unrecognized." {
275 untested "skip mi tests (not compiled with mi support)."
276 remote_close host
277 unset gdb_spawn_id
278 return -1
279 }
280 timeout {
281 perror "(timeout) GDB never initialized after 10 seconds."
282 remote_close host
283 unset gdb_spawn_id
284 return -1
285 }
286 }
287 set gdb_main_spawn_id $gdb_spawn_id
288 set mi_spawn_id $gdb_spawn_id
289
290 # FIXME: mi output does not go through pagers, so these can be removed.
291 # force the height to "unlimited", so no pagers get used
292 send_gdb "100-gdb-set height 0\n"
293 gdb_expect 10 {
294 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
295 verbose "Setting height to 0." 2
296 }
297 timeout {
298 warning "Couldn't set the height to 0"
299 }
300 }
301 # force the width to "unlimited", so no wraparound occurs
302 send_gdb "101-gdb-set width 0\n"
303 gdb_expect 10 {
304 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
305 verbose "Setting width to 0." 2
306 }
307 timeout {
308 warning "Couldn't set the width to 0."
309 }
310 }
311
312 if { $separate_inferior_pty } {
313 mi_create_inferior_pty
314 }
315
316 if {![info exists inferior_spawn_id]} {
317 set inferior_spawn_id $gdb_spawn_id
318 }
319
320 mi_detect_async
321
322 return 0
323 }
324
325 #
326 # Overridable function. You can override this function in your
327 # baseboard file.
328 #
329 proc mi_gdb_start { args } {
330 return [eval default_mi_gdb_start $args]
331 }
332
333 # Many of the tests depend on setting breakpoints at various places and
334 # running until that breakpoint is reached. At times, we want to start
335 # with a clean-slate with respect to breakpoints, so this utility proc
336 # lets us do this without duplicating this code everywhere.
337 #
338
339 proc mi_delete_breakpoints {} {
340 global mi_gdb_prompt
341
342 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
343 send_gdb "102-break-delete\n"
344 gdb_expect 30 {
345 -re "Delete all breakpoints.*y or n.*$" {
346 send_gdb "y\n"
347 exp_continue
348 }
349 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
350 # This happens if there were no breakpoints
351 }
352 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
353 }
354
355 # The correct output is not "No breakpoints or watchpoints." but an
356 # empty BreakpointTable. Also, a query is not acceptable with mi.
357 send_gdb "103-break-list\n"
358 gdb_expect 30 {
359 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
360 -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$" {}
361 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
362 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
363 -re "Delete all breakpoints.*or n.*$" {
364 warning "Unexpected prompt for breakpoints deletion"
365 send_gdb "y\n"
366 exp_continue
367 }
368 timeout { perror "-break-list (timeout)" ; return }
369 }
370 }
371
372 proc mi_gdb_reinitialize_dir { subdir } {
373 global mi_gdb_prompt
374 global MIFLAGS
375
376 if [is_remote host] {
377 return ""
378 }
379
380 send_gdb "104-environment-directory -r\n"
381 gdb_expect 60 {
382 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
383 -re "$mi_gdb_prompt$" {}
384 timeout {error "Dir reinitialization failed (timeout)"}
385 }
386
387 send_gdb "105-environment-directory $subdir\n"
388 gdb_expect 60 {
389 -re "Source directories searched.*$mi_gdb_prompt$" {
390 verbose "Dir set to $subdir"
391 }
392 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
393 # FIXME: We return just the prompt for now.
394 verbose "Dir set to $subdir"
395 # perror "Dir \"$subdir\" failed."
396 }
397 }
398 }
399
400 # Send GDB the "target" command.
401 # FIXME: Some of these patterns are not appropriate for MI. Based on
402 # config/monitor.exp:gdb_target_command.
403 proc mi_gdb_target_cmd { targetname serialport } {
404 global mi_gdb_prompt
405
406 set serialport_re [string_to_regexp $serialport]
407 for {set i 1} {$i <= 3} {incr i} {
408 send_gdb "47-target-select $targetname $serialport\n"
409 gdb_expect 60 {
410 -re "47\\^connected.*$mi_gdb_prompt" {
411 verbose "Set target to $targetname"
412 return 0
413 }
414 -re "unknown host.*$mi_gdb_prompt" {
415 verbose "Couldn't look up $serialport"
416 }
417 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
418 verbose "Connection failed"
419 }
420 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
421 verbose "Set target to $targetname"
422 return 0
423 }
424 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
425 verbose "Set target to $targetname"
426 return 0
427 }
428 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
429 verbose "Set target to $targetname"
430 return 0
431 }
432 -re "Connected to.*$mi_gdb_prompt$" {
433 verbose "Set target to $targetname"
434 return 0
435 }
436 -re "Ending remote.*$mi_gdb_prompt$" { }
437 -re "Connection refused.*$mi_gdb_prompt$" {
438 verbose "Connection refused by remote target. Pausing, and trying again."
439 sleep 5
440 continue
441 }
442 -re "Non-stop mode requested, but remote does not support non-stop.*$mi_gdb_prompt" {
443 unsupported "non-stop mode not supported"
444 return 1
445 }
446 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
447 verbose "Got timeout error from gdb."
448 }
449 timeout {
450 send_gdb "\ 3"
451 break
452 }
453 }
454 }
455 return 1
456 }
457
458 #
459 # load a file into the debugger (file command only).
460 # return a -1 if anything goes wrong.
461 #
462 proc mi_gdb_file_cmd { arg } {
463 global loadpath
464 global loadfile
465 global GDB
466 global mi_gdb_prompt
467 global last_loaded_file
468 upvar timeout timeout
469
470 # GCC for Windows target may create foo.exe given "-o foo".
471 if { ![file exists $arg] && [file exists "$arg.exe"] } {
472 set arg "$arg.exe"
473 }
474
475 set last_loaded_file $arg
476
477 if [is_remote host] {
478 set arg [remote_download host $arg]
479 if { $arg == "" } {
480 error "download failed"
481 return -1
482 }
483 }
484
485 # FIXME: Several of these patterns are only acceptable for console
486 # output. Queries are an error for mi.
487 send_gdb "105-file-exec-and-symbols $arg\n"
488 gdb_expect 120 {
489 -re "Reading symbols from.*$mi_gdb_prompt$" {
490 verbose "\t\tLoaded $arg into the $GDB"
491 return 0
492 }
493 -re "has no symbol-table.*$mi_gdb_prompt$" {
494 perror "$arg wasn't compiled with \"-g\""
495 return -1
496 }
497 -re "Load new symbol table from \".*\".*y or n. $" {
498 send_gdb "y\n"
499 gdb_expect 120 {
500 -re "Reading symbols from.*$mi_gdb_prompt$" {
501 verbose "\t\tLoaded $arg with new symbol table into $GDB"
502 # All OK
503 }
504 timeout {
505 perror "(timeout) Couldn't load $arg, other program already loaded."
506 return -1
507 }
508 }
509 }
510 -re "No such file or directory.*$mi_gdb_prompt$" {
511 perror "($arg) No such file or directory\n"
512 return -1
513 }
514 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
515 # We (MI) are just giving the prompt back for now, instead of giving
516 # some acknowledgement.
517 return 0
518 }
519 timeout {
520 perror "couldn't load $arg into $GDB (timed out)."
521 return -1
522 }
523 eof {
524 # This is an attempt to detect a core dump, but seems not to
525 # work. Perhaps we need to match .* followed by eof, in which
526 # gdb_expect does not seem to have a way to do that.
527 perror "couldn't load $arg into $GDB (end of file)."
528 return -1
529 }
530 }
531 }
532
533 #
534 # connect to the target and download a file, if necessary.
535 # return a -1 if anything goes wrong.
536 #
537 proc mi_gdb_target_load { } {
538 global loadpath
539 global loadfile
540 global GDB
541 global mi_gdb_prompt
542
543 if [target_info exists gdb_load_timeout] {
544 set loadtimeout [target_info gdb_load_timeout]
545 } else {
546 set loadtimeout 1600
547 }
548
549 if { [info procs gdbserver_gdb_load] != "" } {
550 mi_gdb_test "kill" ".*" ""
551 if { [catch gdbserver_gdb_load res] == 1 } {
552 perror $res
553 return -1
554 }
555 set protocol [lindex $res 0]
556 set gdbport [lindex $res 1]
557
558 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
559 return -1
560 }
561 } elseif { [info procs send_target_sid] != "" } {
562 # For SID, things get complex
563 send_gdb "kill\n"
564 gdb_expect 10 {
565 -re ".*$mi_gdb_prompt$"
566 }
567 send_target_sid
568 gdb_expect $loadtimeout {
569 -re "\\^done.*$mi_gdb_prompt$" {
570 }
571 timeout {
572 perror "Unable to connect to SID target (timeout)"
573 return -1
574 }
575 }
576 send_gdb "48-target-download\n"
577 gdb_expect $loadtimeout {
578 -re "48\\^done.*$mi_gdb_prompt$" {
579 }
580 timeout {
581 perror "Unable to download to SID target (timeout)"
582 return -1
583 }
584 }
585 } elseif { [target_info protocol] == "sim" } {
586 set target_sim_options "[board_info target gdb,target_sim_options]"
587 # For the simulator, just connect to it directly.
588 send_gdb "47-target-select sim $target_sim_options\n"
589 gdb_expect $loadtimeout {
590 -re "47\\^connected.*$mi_gdb_prompt$" {
591 }
592 timeout {
593 perror "Unable to select sim target (timeout)"
594 return -1
595 }
596 }
597 send_gdb "48-target-download\n"
598 gdb_expect $loadtimeout {
599 -re "48\\^done.*$mi_gdb_prompt$" {
600 }
601 timeout {
602 perror "Unable to download to sim target (timeout)"
603 return -1
604 }
605 }
606 } elseif { [target_info gdb_protocol] == "remote" } {
607 # remote targets
608 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
609 perror "Unable to connect to remote target"
610 return -1
611 }
612 send_gdb "48-target-download\n"
613 gdb_expect $loadtimeout {
614 -re "48\\^done.*$mi_gdb_prompt$" {
615 }
616 timeout {
617 perror "Unable to download to remote target (timeout)"
618 return -1
619 }
620 }
621 }
622 return 0
623 }
624
625 #
626 # load a file into the debugger.
627 # return a -1 if anything goes wrong.
628 #
629 proc mi_gdb_load { arg } {
630 if { $arg != "" } {
631 return [mi_gdb_file_cmd $arg]
632 }
633 return 0
634 }
635
636 # Return true if symbols were read in using -readnow. Otherwise,
637 # return false.
638
639 proc mi_readnow { args } {
640 # Just defer to gdb.exp.
641 return [readnow]
642 }
643
644 # mi_gdb_test COMMAND [PATTERN [MESSAGE [IPATTERN]]] -- send a command to gdb;
645 # test the result.
646 #
647 # COMMAND is the command to execute, send to GDB with send_gdb. If
648 # this is the null string no command is sent.
649 # PATTERN is the pattern to match for a PASS, and must NOT include
650 # the \r\n sequence immediately before the gdb prompt.
651 # If not specified, .* is used.
652 # MESSAGE is the message to be printed. (If this is the empty string,
653 # then sometimes we don't call pass or fail at all; I don't
654 # understand this at all.)
655 # If not specified, COMMAND is used.
656 # IPATTERN is the pattern to match for the inferior's output. This parameter
657 # is optional. If present, it will produce a PASS if the match is
658 # successful, and a FAIL if unsuccessful.
659 #
660 # Returns:
661 # 1 if the test failed,
662 # 0 if the test passes,
663 # -1 if there was an internal error.
664 #
665 proc mi_gdb_test { args } {
666 global verbose
667 global mi_gdb_prompt
668 global GDB expect_out
669 global inferior_exited_re async
670 upvar timeout timeout
671
672 if {[llength $args] >= 1} {
673 set command [lindex $args 0]
674 } else {
675 error "Not enough arguments in mi_gdb_test"
676 }
677
678 if {[llength $args] >= 2} {
679 set pattern [lindex $args 1]
680 } else {
681 set pattern ".*"
682 }
683
684 if {[llength $args] >= 3} {
685 set message [lindex $args 2]
686 } else {
687 set message $command
688 }
689
690 if [llength $args]==4 {
691 set ipattern [lindex $args 3]
692 }
693
694 if [llength $args]==5 {
695 set question_string [lindex $args 3]
696 set response_string [lindex $args 4]
697 } else {
698 set question_string "^FOOBAR$"
699 }
700
701 if { [llength $args] >= 6 } {
702 error "Too many arguments in mi_gdb_test"
703 }
704
705 if {$verbose > 2} {
706 send_user "Sending \"$command\" to gdb\n"
707 send_user "Looking to match \"$pattern\"\n"
708 send_user "Message is \"$message\"\n"
709 }
710
711 set result -1
712 set string "${command}\n"
713 set string_regex [string_to_regexp $command]
714
715 if { $command != "" } {
716 while { "$string" != "" } {
717 set foo [string first "\n" "$string"]
718 set len [string length "$string"]
719 if { $foo < [expr $len - 1] } {
720 set str [string range "$string" 0 $foo]
721 if { [send_gdb "$str"] != "" } {
722 perror "Couldn't send $command to GDB."
723 }
724 gdb_expect 2 {
725 -re "\[\r\n\]" { }
726 timeout { }
727 }
728 set string [string range "$string" [expr $foo + 1] end]
729 } else {
730 break
731 }
732 }
733 if { "$string" != "" } {
734 if { [send_gdb "$string"] != "" } {
735 perror "Couldn't send $command to GDB."
736 }
737 }
738 }
739
740 if [info exists timeout] {
741 set tmt $timeout
742 } else {
743 global timeout
744 if [info exists timeout] {
745 set tmt $timeout
746 } else {
747 set tmt 60
748 }
749 }
750 if {$async} {
751 # With $prompt_re "" there may come arbitrary asynchronous response
752 # from the previous command, before or after $string_regex.
753 set string_regex ".*"
754 }
755 verbose -log "Expecting: ^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)"
756 gdb_expect $tmt {
757 -re "\\*\\*\\* DOSEXIT code.*" {
758 if { $message != "" } {
759 fail "$message"
760 }
761 return -1
762 }
763 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
764 if {![isnative]} {
765 warning "Can`t communicate to remote target."
766 }
767 gdb_exit
768 gdb_start
769 set result -1
770 }
771 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
772 # At this point, $expect_out(1,string) is the MI input command.
773 # and $expect_out(2,string) is the MI output command.
774 # If $expect_out(1,string) is "", then there was no MI input command here.
775
776 # NOTE, there is no trailing anchor because with GDB/MI,
777 # asynchronous responses can happen at any point, causing more
778 # data to be available. Normally an anchor is used to make
779 # sure the end of the output is matched, however, $mi_gdb_prompt
780 # is just as good of an anchor since mi_gdb_test is meant to
781 # match a single mi output command. If a second GDB/MI output
782 # response is sent, it will be in the buffer for the next
783 # time mi_gdb_test is called.
784 if {![string match "" $message]} {
785 pass "$message"
786 }
787 set result 0
788 }
789 -re "(${question_string})$" {
790 send_gdb "$response_string\n"
791 exp_continue
792 }
793 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
794 perror "Undefined command \"$command\"."
795 fail "$message"
796 set result 1
797 }
798 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
799 perror "\"$command\" is not a unique command name."
800 fail "$message"
801 set result 1
802 }
803 -re "$inferior_exited_re with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
804 if {![string match "" $message]} {
805 set errmsg "$message (the program exited)"
806 } else {
807 set errmsg "$command (the program exited)"
808 }
809 fail "$errmsg"
810 return -1
811 }
812 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
813 if {![string match "" $message]} {
814 set errmsg "$message (the program is no longer running)"
815 } else {
816 set errmsg "$command (the program is no longer running)"
817 }
818 fail "$errmsg"
819 return -1
820 }
821 -re ".*$mi_gdb_prompt\[ \]*$" {
822 if {![string match "" $message]} {
823 fail "$message (unexpected output)"
824 }
825 set result 1
826 }
827 "<return>" {
828 send_gdb "\n"
829 perror "Window too small."
830 fail "$message"
831 }
832 eof {
833 perror "Process no longer exists"
834 if { $message != "" } {
835 fail "$message"
836 }
837 return -1
838 }
839 full_buffer {
840 perror "internal buffer is full."
841 fail "$message"
842 }
843 timeout {
844 if {![string match "" $message]} {
845 fail "$message (timeout)"
846 }
847 set result 1
848 }
849 }
850
851 # If the GDB output matched, compare the inferior output.
852 if { $result == 0 } {
853 if [ info exists ipattern ] {
854 if { ![target_info exists gdb,noinferiorio] } {
855 global gdb_spawn_id inferior_spawn_id
856
857 set sid "$inferior_spawn_id $gdb_spawn_id"
858 gdb_expect {
859 -i "$sid" -re "$ipattern" {
860 pass "$message inferior output"
861 }
862 timeout {
863 fail "$message inferior output (timeout)"
864 set result 1
865 }
866 }
867 } else {
868 unsupported "$message inferior output"
869 }
870 }
871 }
872
873 return $result
874 }
875
876 # Collect output sent to the console output stream until UNTIL is
877 # seen. UNTIL is a regular expression. MESSAGE is the message to be
878 # printed in case of timeout.
879
880 proc mi_gdb_expect_cli_output {until message} {
881
882 set output ""
883 gdb_expect {
884 -re "~\"(\[^\r\n\]+)\"\r\n" {
885 append output $expect_out(1,string)
886 exp_continue
887 }
888 -notransfer -re "$until" {
889 # Done
890 }
891 timeout {
892 fail "$message (timeout)"
893 return ""
894 }
895 }
896
897 return $output
898 }
899
900 #
901 # MI run command. (A modified version of gdb_run_cmd)
902 #
903
904 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
905 # ``.*$'' could swallow up output that we attempt to match elsewhere.
906
907 # Send the command to run the test program.
908 #
909 # If USE_MI_COMMAND is true, the "-exec-run" command is used.
910 # Otherwise, the "run" (CLI) command is used. If the global USE_GDB_STUB is
911 # true, -exec-continue and continue are used instead of their run counterparts.
912 #
913 # ARGS is passed as argument to the command used to run the test program.
914 # Beware that arguments to "-exec-run" do not have the same semantics as
915 # arguments to the "run" command, so USE_MI_COMMAND influences the meaning
916 # of ARGS. If USE_MI_COMMAND is true, they are arguments to -exec-run.
917 # If USE_MI_COMMAND is false, they are effectively arguments passed
918 # to the test program. If the global USE_GDB_STUB is true, ARGS is not used.
919 proc mi_run_cmd_full {use_mi_command args} {
920 global mi_gdb_prompt use_gdb_stub
921 global thread_selected_re
922 global library_loaded_re
923
924 if {$use_mi_command} {
925 set run_prefix "220-exec-"
926 set run_match "220"
927 } else {
928 set run_prefix ""
929 set run_match ""
930 }
931
932 foreach command [gdb_init_commands] {
933 send_gdb "$command\n"
934 gdb_expect 30 {
935 -re "$mi_gdb_prompt$" { }
936 default {
937 unresolved "gdb_init_command for target failed"
938 return -1
939 }
940 }
941 }
942
943 if { [mi_gdb_target_load] < 0 } {
944 return -1
945 }
946
947 if $use_gdb_stub {
948 if [target_info exists gdb,do_reload_on_run] {
949 send_gdb "${run_prefix}continue\n"
950 gdb_expect 60 {
951 -re "${run_match}\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\r\n$mi_gdb_prompt" {}
952 -re "${run_match}\\^error.*$mi_gdb_prompt" {return -1}
953 default {}
954 }
955 return 0
956 }
957
958 if [target_info exists gdb,start_symbol] {
959 set start [target_info gdb,start_symbol]
960 } else {
961 set start "start"
962 }
963
964 # HACK: Should either use 000-jump or fix the target code
965 # to better handle RUN.
966 send_gdb "jump *$start\n"
967 warning "Using CLI jump command, expect run-to-main FAIL"
968 gdb_expect {
969 -re "&\"jump \\*${start}\\\\n\"\[\r\n\]+~\"Continuing at 0x\[0-9A-Fa-f\]+\.\\\\n\"\[\r\n\]+\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\[\r\n\]+${mi_gdb_prompt}" {}
970 timeout {
971 unresolved "unable to start target"
972 return -1
973 }
974 }
975 return 0
976 }
977
978 send_gdb "${run_prefix}run $args\n"
979 gdb_expect {
980 -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}" {
981 }
982 -re "\\^error,msg=\"The target does not support running in non-stop mode.\"" {
983 unsupported "non-stop mode not supported"
984 return -1
985 }
986 timeout {
987 unresolved "unable to start target"
988 return -1
989 }
990 }
991 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
992
993 return 0
994 }
995
996 # A wrapper for mi_run_cmd_full which uses -exec-run and
997 # -exec-continue, as appropriate. ARGS are passed verbatim to
998 # mi_run_cmd_full.
999 proc mi_run_cmd {args} {
1000 return [eval mi_run_cmd_full 1 $args]
1001 }
1002
1003 # A wrapper for mi_run_cmd_full which uses the CLI commands 'run' and
1004 # 'continue', as appropriate. ARGS are passed verbatim to
1005 # mi_run_cmd_full.
1006 proc mi_run_with_cli {args} {
1007 return [eval mi_run_cmd_full 0 $args]
1008 }
1009
1010 # Starts fresh GDB binary and loads an optional executable into GDB.
1011 # Usage: mi_clean_restart [EXECUTABLE]
1012 # EXECUTABLE is the basename of the binary.
1013 # Return -1 if starting gdb or loading the executable failed.
1014
1015 proc mi_clean_restart {{executable ""} {flags {}}} {
1016 global srcdir
1017 global subdir
1018 global errcnt
1019 global warncnt
1020
1021 gdb_exit
1022
1023 # This is a clean restart, so reset error and warning count.
1024 set errcnt 0
1025 set warncnt 0
1026
1027 if {[mi_gdb_start $flags]} {
1028 return -1
1029 }
1030
1031 mi_gdb_reinitialize_dir $srcdir/$subdir
1032
1033 if {$executable != ""} {
1034 set binfile [standard_output_file ${executable}]
1035 return [mi_gdb_load ${binfile}]
1036 }
1037
1038 return 0
1039 }
1040
1041 # Just like gdb's "runto" proc, it will run the target to a given
1042 # function. The big difference here between mi_runto and mi_execute_to
1043 # is that mi_execute_to must have the inferior running already. This
1044 # proc will (like gdb's runto) (re)start the inferior, too.
1045 #
1046 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
1047 # It returns:
1048 # -1 if failed, timedout
1049 # 0 if test passed
1050 #
1051 # Supported options:
1052 #
1053 # -qualified -- pass --qualified to -break-insert
1054 # -pending -- pass -f to -break-insert to create a pending
1055 # breakpoint.
1056
1057 proc mi_runto_helper {func run_or_continue args} {
1058 global mi_gdb_prompt expect_out
1059 global hex decimal fullname_syntax
1060
1061 parse_args {{qualified} {pending}}
1062
1063 set test "mi runto $func"
1064 if {$pending} {
1065 set bp [mi_make_breakpoint_pending -type breakpoint -disp del]
1066 } else {
1067 set bp [mi_make_breakpoint -type breakpoint -disp del \
1068 -func $func\(\\\(.*\\\)\)?]
1069 }
1070 set extra_opts ""
1071 set extra_output ""
1072 if {$qualified} {
1073 lappend extra_opts "--qualified"
1074 }
1075 if {$pending} {
1076 lappend extra_opts "-f"
1077 # MI prints "Function FUNC not defined", "No line NNN in current
1078 # file.", etc. to the CLI stream.
1079 set extra_output "&\"\[^\r\n\]+\"\r\n"
1080 }
1081 mi_gdb_test "200-break-insert [join $extra_opts " "] -t $func" "${extra_output}200\\^done,$bp" \
1082 "breakpoint at $func"
1083
1084 if {$run_or_continue == "run"} {
1085 if { [mi_run_cmd] < 0 } {
1086 return -1
1087 }
1088 } else {
1089 mi_send_resuming_command "exec-continue" "$test"
1090 }
1091
1092 mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test
1093 }
1094
1095 proc mi_runto {func args} {
1096 return [mi_runto_helper $func "run" {*}$args]
1097 }
1098
1099 # Just like runto_main but works with the MI interface.
1100
1101 proc mi_runto_main {} {
1102 return [mi_runto_helper "main" "run" -qualified]
1103 }
1104
1105 # Next to the next statement
1106 # For return values, see mi_execute_to_helper
1107
1108 proc mi_next { test } {
1109 return [mi_next_to {.*} {.*} {.*} {.*} $test]
1110 }
1111
1112
1113 # Step to the next statement
1114 # For return values, see mi_execute_to_helper
1115
1116 proc mi_step { test } {
1117 return [mi_step_to {.*} {.*} {.*} {.*} $test]
1118 }
1119
1120 set async "unknown"
1121
1122 proc mi_detect_async {} {
1123 global async
1124 global mi_gdb_prompt
1125
1126 send_gdb "show mi-async\n"
1127
1128 gdb_expect {
1129 -re "asynchronous mode is on...*$mi_gdb_prompt$" {
1130 set async 1
1131 }
1132 -re ".*$mi_gdb_prompt$" {
1133 set async 0
1134 }
1135 timeout {
1136 set async 0
1137 }
1138 }
1139 return $async
1140 }
1141
1142 # Wait for MI *stopped notification to appear.
1143 # The REASON, FUNC, ARGS, FILE and LINE are regular expressions
1144 # to match against whatever is output in *stopped. FILE may also match
1145 # filename of a file without debug info. ARGS should not include [] the
1146 # list of argument is enclosed in, and other regular expressions should
1147 # not include quotes.
1148 # EXTRA can be a list of one, two or three elements.
1149 # The first element is the regular expression
1150 # for output expected right after *stopped, and before GDB prompt.
1151 # The third element is the regular expression for the locno
1152 # right after bkptno field. The locno regex should not include
1153 # the comma separating it from the following fields.
1154 #
1155 # When we fail to match output at all, -1 is returned. If FILE does
1156 # match and the target system has no debug info for FILE return 0.
1157 # Otherwise, the line at which we stop is returned. This is useful when
1158 # exact line is not possible to specify for some reason -- one can pass
1159 # the .* or "\[0-9\]*" regexps for line, and then check the line
1160 # programmatically.
1161 #
1162 # Do not pass .* for any argument if you are expecting more than one stop.
1163 proc mi_expect_stop { reason func args file line extra test } {
1164
1165 global mi_gdb_prompt
1166 global hex
1167 global decimal
1168 global fullname_syntax
1169 global async
1170 global thread_selected_re
1171 global breakpoint_re
1172
1173 set any "\[^\n\]*"
1174
1175 set after_stopped ""
1176 set after_reason ""
1177 set locno ""
1178 if { [llength $extra] == 3 } {
1179 set after_stopped [lindex $extra 0]
1180 set after_reason [lindex $extra 1]
1181 set after_reason "${after_reason},"
1182 set locno [lindex $extra 2]
1183 set locno "${locno},"
1184 } elseif { [llength $extra] == 2 } {
1185 set after_stopped [lindex $extra 0]
1186 set after_reason [lindex $extra 1]
1187 set after_reason "${after_reason},"
1188 } elseif { [llength $extra] == 1 } {
1189 set after_stopped [lindex $extra 0]
1190 }
1191
1192 if {$async} {
1193 set prompt_re ""
1194 } else {
1195 set prompt_re "$mi_gdb_prompt$"
1196 }
1197
1198 if { $reason == "really-no-reason" } {
1199 gdb_expect {
1200 -re "\\*stopped\r\n$prompt_re" {
1201 pass "$test"
1202 }
1203 timeout {
1204 fail "$test (timeout)"
1205 }
1206 }
1207 return
1208 }
1209
1210 if { $reason == "exited-normally" } {
1211
1212 gdb_expect {
1213 -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re" {
1214 pass "$test"
1215 }
1216 -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
1217 timeout {
1218 fail "$test (timeout)"
1219 }
1220 }
1221 return
1222 }
1223 if { $reason == "exited" } {
1224 gdb_expect {
1225 -re "\\*stopped,reason=\"exited\",exit-code=\"\[0-7\]+\"\r\n$prompt_re" {
1226 pass "$test"
1227 }
1228 -re ".*$mi_gdb_prompt$" {
1229 fail "$test (inferior not stopped)"
1230 }
1231 timeout {
1232 fail "$test (timeout)"
1233 }
1234 }
1235 return
1236 }
1237
1238 if { $reason == "solib-event" } {
1239 set pattern "\\*stopped,reason=\"solib-event\",thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1240 verbose -log "mi_expect_stop: expecting: $pattern"
1241 gdb_expect {
1242 -re "$pattern" {
1243 pass "$test"
1244 }
1245 timeout {
1246 fail "$test (timeout)"
1247 }
1248 }
1249 return
1250 }
1251
1252 set args "\\\[$args\\\]"
1253
1254 set bn ""
1255 set ebn ""
1256 if { $reason == "breakpoint-hit" } {
1257 set bn {bkptno="[0-9]+",}
1258 set bn "${bn}${locno}"
1259 } elseif { $reason == "solib-event" } {
1260 set bn ".*"
1261 } elseif { $reason == "exception-caught" } {
1262 set ebn {bkptno="[0-9]+",}
1263 set ebn "${ebn}${locno}"
1264 set bn ".*"
1265 set reason "breakpoint-hit"
1266 }
1267
1268 set r ""
1269 if { $reason != "" } {
1270 if { [regexp {"} $reason] } {
1271 set r "reason=$reason,"
1272 } else {
1273 set r "reason=\"$reason\","
1274 }
1275 }
1276
1277
1278 set a $after_reason
1279
1280 verbose -log "mi_expect_stop: expecting: \\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1281
1282 gdb_expect {
1283 -re "\\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1284 pass "$test"
1285 if {[array names expect_out "2,string"] != ""} {
1286 return $expect_out(2,string)
1287 }
1288 # No debug info available but $file does match.
1289 return 0
1290 }
1291 -re "\\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$any\",args=\[\\\[\{\]$any\[\\\]\}\],file=\"$any\",fullname=\"${fullname_syntax}$any\",line=\"\[0-9\]*\",arch=\"$any\"\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1292 verbose -log "got $expect_out(buffer)"
1293 fail "$test (stopped at wrong place)"
1294 return -1
1295 }
1296 -re ".*\r\n$mi_gdb_prompt$" {
1297 verbose -log "got $expect_out(buffer)"
1298 fail "$test (unknown output after running)"
1299 return -1
1300 }
1301 timeout {
1302 fail "$test (timeout)"
1303 return -1
1304 }
1305 }
1306 }
1307
1308 # Wait for MI *stopped notification related to an interrupt request to
1309 # appear.
1310 proc mi_expect_interrupt { test } {
1311 global mi_gdb_prompt
1312 global decimal
1313 global async
1314
1315 if {$async} {
1316 set prompt_re ""
1317 } else {
1318 set prompt_re "$mi_gdb_prompt"
1319 }
1320
1321 set r_nonstop "reason=\"signal-received\",signal-name=\"0\",signal-meaning=\"Signal 0\""
1322 set r_allstop "reason=\"signal-received\",signal-name=\"SIGINT\",signal-meaning=\"Interrupt\""
1323 set r "(${r_nonstop}|${r_allstop})"
1324 set any "\[^\n\]*"
1325
1326 # A signal can land anywhere, just ignore the location
1327 verbose -log "mi_expect_interrupt: expecting: \\*stopped,${r}$any\r\n$prompt_re"
1328 gdb_expect {
1329 -re "\\*stopped,${r}$any\r\n$prompt_re" {
1330 pass "$test"
1331 return 0
1332 }
1333 -re ".*\r\n$mi_gdb_prompt" {
1334 verbose -log "got $expect_out(buffer)"
1335 fail "$test (unknown output after running)"
1336 return -1
1337 }
1338 timeout {
1339 fail "$test (timeout)"
1340 return -1
1341 }
1342 }
1343 }
1344
1345 # cmd should not include the number or newline (i.e. "exec-step 3", not
1346 # "220-exec-step 3\n"
1347
1348 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1349 # after the first prompt is printed.
1350
1351 proc mi_execute_to { cmd reason func args file line extra test } {
1352 mi_send_resuming_command "$cmd" "$test"
1353 set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1354 return $r
1355 }
1356
1357 proc mi_next_to { func args file line test } {
1358 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1359 "$file" "$line" "" "$test"
1360 }
1361
1362 proc mi_step_to { func args file line test } {
1363 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1364 "$file" "$line" "" "$test"
1365 }
1366
1367 proc mi_finish_to { func args file line result ret test } {
1368 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
1369 "$file" "$line" \
1370 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1371 "$test"
1372 }
1373
1374 proc mi_continue_to {func} {
1375 mi_runto_helper $func "continue"
1376 }
1377
1378 # Creates a breakpoint and checks the reported fields are as expected.
1379 # This procedure takes the same options as mi_make_breakpoint and
1380 # returns the breakpoint regexp from that procedure.
1381
1382 proc mi_create_breakpoint {location test args} {
1383 set bp [eval mi_make_breakpoint $args]
1384 mi_gdb_test "222-break-insert $location" "222\\^done,$bp" $test
1385 return $bp
1386 }
1387
1388 # Like mi_create_breakpoint, but creates a breakpoint with multiple
1389 # locations using mi_make_breakpoint_multi instead.
1390
1391 proc mi_create_breakpoint_multi {location test args} {
1392 set bp [eval mi_make_breakpoint_multi $args]
1393 mi_gdb_test "222-break-insert $location" "222\\^done,$bp" $test
1394 return $bp
1395 }
1396
1397 # Like mi_create_breakpoint, but creates a pending breakpoint.
1398
1399 proc mi_create_breakpoint_pending {location test args} {
1400 set bp [eval mi_make_breakpoint_pending $args]
1401 mi_gdb_test "222-break-insert $location" ".*\r\n222\\^done,$bp" $test
1402 return $bp
1403 }
1404
1405 # Creates varobj named NAME for EXPRESSION.
1406 # Name cannot be "-".
1407 proc mi_create_varobj { name expression testname } {
1408 mi_gdb_test "-var-create $name * $expression" \
1409 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*,has_more=\"0\"" \
1410 $testname
1411 }
1412
1413 proc mi_create_floating_varobj { name expression testname } {
1414 mi_gdb_test "-var-create $name @ $expression" \
1415 "\\^done,name=\"$name\",numchild=\"\(-1\|\[0-9\]+\)\",value=\".*\",type=.*" \
1416 $testname
1417 }
1418
1419
1420 # Same as mi_create_varobj, but also checks the reported type
1421 # of the varobj.
1422 proc mi_create_varobj_checked { name expression type testname } {
1423 mi_gdb_test "-var-create $name * $expression" \
1424 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1425 $testname
1426 }
1427
1428 # Same as mi_create_floating_varobj, but assumes the test is creating
1429 # a dynamic varobj that has children. The "value" and "has_more"
1430 # attributes are checked.
1431 proc mi_create_dynamic_varobj {name expression value has_more testname} {
1432 mi_gdb_test "-var-create $name @ $expression" \
1433 "\\^done,name=\"$name\",numchild=\"0\",value=\"$value\",type=.*,has_more=\"${has_more}\"" \
1434 $testname
1435 }
1436
1437 # Deletes the specified NAME.
1438 proc mi_delete_varobj { name testname } {
1439 mi_gdb_test "-var-delete $name" \
1440 "\\^done,ndeleted=.*" \
1441 $testname
1442 }
1443
1444 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1445 # are reported as updated, and no other varobj is updated.
1446 # Assumes that no varobj is out of scope and that no varobj changes
1447 # types.
1448 proc mi_varobj_update { name expected testname } {
1449 set er "\\^done,changelist=\\\["
1450 set first 1
1451 foreach item $expected {
1452 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\",has_more=\".\"}"
1453 if {$first == 1} {
1454 set er "$er$v"
1455 set first 0
1456 } else {
1457 set er "$er,$v"
1458 }
1459 }
1460 set er "$er\\\]"
1461
1462 verbose -log "Expecting: $er" 2
1463 mi_gdb_test "-var-update $name" $er $testname
1464 }
1465
1466 proc mi_varobj_update_with_child_type_change { name child_name new_type new_children testname } {
1467 set v "{name=\"$child_name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\",has_more=\".\"}"
1468 set er "\\^done,changelist=\\\[$v\\\]"
1469 verbose -log "Expecting: $er"
1470 mi_gdb_test "-var-update $name" $er $testname
1471 }
1472
1473 proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1474 mi_varobj_update_with_child_type_change $name $name $new_type $new_children $testname
1475 }
1476
1477 # A helper that turns a key/value list into a regular expression
1478 # matching some MI output.
1479 proc mi_varobj_update_kv_helper {list} {
1480 set first 1
1481 set rx ""
1482 foreach {key value} $list {
1483 if {!$first} {
1484 append rx ,
1485 }
1486 set first 0
1487 if {$key == "new_children"} {
1488 append rx "$key=\\\[$value\\\]"
1489 } else {
1490 append rx "$key=\"$value\""
1491 }
1492 }
1493 return $rx
1494 }
1495
1496 # A helper for mi_varobj_update_dynamic that computes a match
1497 # expression given a child list.
1498 proc mi_varobj_update_dynamic_helper {children} {
1499 set crx ""
1500
1501 set first 1
1502 foreach child $children {
1503 if {!$first} {
1504 append crx ,
1505 }
1506 set first 0
1507 append crx "{"
1508 append crx [mi_varobj_update_kv_helper $child]
1509 append crx "}"
1510 }
1511
1512 return $crx
1513 }
1514
1515 # Update a dynamic varobj named NAME. CHILDREN is a list of children
1516 # that have been updated; NEW_CHILDREN is a list of children that were
1517 # added to the primary varobj. Each child is a list of key/value
1518 # pairs that are expected. SELF is a key/value list holding
1519 # information about the varobj itself. TESTNAME is the name of the
1520 # test.
1521 proc mi_varobj_update_dynamic {name testname self children new_children} {
1522 if {[llength $new_children]} {
1523 set newrx [mi_varobj_update_dynamic_helper $new_children]
1524 lappend self new_children $newrx
1525 }
1526 set selfrx [mi_varobj_update_kv_helper $self]
1527 set crx [mi_varobj_update_dynamic_helper $children]
1528
1529 set er "\\^done,changelist=\\\[\{name=\"$name\",in_scope=\"true\""
1530 append er ",$selfrx\}"
1531 if {"$crx" != ""} {
1532 append er ",$crx"
1533 }
1534 append er "\\\]"
1535
1536 verbose -log "Expecting: $er"
1537 mi_gdb_test "-var-update $name" $er $testname
1538 }
1539
1540 proc mi_check_varobj_value { name value testname } {
1541
1542 mi_gdb_test "-var-evaluate-expression $name" \
1543 "\\^done,value=\"$value\"" \
1544 $testname
1545 }
1546
1547 # Helper proc which constructs a child regexp for
1548 # mi_list_varobj_children and mi_varobj_update_dynamic.
1549 proc mi_child_regexp {children add_child} {
1550 set children_exp {}
1551
1552 if {$add_child} {
1553 set pre "child="
1554 } else {
1555 set pre ""
1556 }
1557
1558 foreach item $children {
1559
1560 set name [lindex $item 0]
1561 set exp [lindex $item 1]
1562 set numchild [lindex $item 2]
1563 if {[llength $item] == 5} {
1564 set type [lindex $item 3]
1565 set value [lindex $item 4]
1566
1567 lappend children_exp\
1568 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1569 } elseif {[llength $item] == 4} {
1570 set type [lindex $item 3]
1571
1572 lappend children_exp\
1573 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1574 } else {
1575 lappend children_exp\
1576 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
1577 }
1578 }
1579 return [join $children_exp ","]
1580 }
1581
1582 # Check the results of the:
1583 #
1584 # -var-list-children VARNAME
1585 #
1586 # command. The CHILDREN parement should be a list of lists.
1587 # Each inner list can have either 3 or 4 elements, describing
1588 # fields that gdb is expected to report for child variable object,
1589 # in the following order
1590 #
1591 # - Name
1592 # - Expression
1593 # - Number of children
1594 # - Type
1595 #
1596 # If inner list has 3 elements, the gdb is expected to output no
1597 # type for a child and no value.
1598 #
1599 # If the inner list has 4 elements, gdb output is expected to
1600 # have no value.
1601 #
1602 proc mi_list_varobj_children { varname children testname } {
1603 mi_list_varobj_children_range $varname "" "" [llength $children] $children \
1604 $testname
1605 }
1606
1607 # Like mi_list_varobj_children, but sets a subrange. NUMCHILDREN is
1608 # the total number of children.
1609 proc mi_list_varobj_children_range {varname from to numchildren children testname} {
1610 set options ""
1611 if {[llength $varname] == 2} {
1612 set options [lindex $varname 1]
1613 set varname [lindex $varname 0]
1614 }
1615
1616 set children_exp_j [mi_child_regexp $children 1]
1617 if {$numchildren} {
1618 set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1619 } {
1620 set expected "\\^done,numchild=\"0\""
1621 }
1622
1623 if {"$to" == ""} {
1624 append expected ",has_more=\"0\""
1625 } elseif {$to >= 0 && $numchildren > $to} {
1626 append expected ",has_more=\"1\""
1627 } else {
1628 append expected ",has_more=\"0\""
1629 }
1630
1631 verbose -log "Expecting: $expected"
1632
1633 mi_gdb_test "-var-list-children $options $varname $from $to" \
1634 $expected $testname
1635 }
1636
1637 # Verifies that variable object VARNAME has NUMBER children,
1638 # where each one is named $VARNAME.<index-of-child> and has type TYPE.
1639 proc mi_list_array_varobj_children { varname number type testname } {
1640 mi_list_array_varobj_children_with_index $varname $number 0 $type $testname
1641 }
1642
1643 # Same as mi_list_array_varobj_children, but allowing to pass a start index
1644 # for an array.
1645 proc mi_list_array_varobj_children_with_index { varname number start_index \
1646 type testname } {
1647 set t {}
1648 set index $start_index
1649 for {set i 0} {$i < $number} {incr i} {
1650 lappend t [list $varname.$index $index 0 $type]
1651 incr index
1652 }
1653 mi_list_varobj_children $varname $t $testname
1654 }
1655
1656 # A list of two-element lists. First element of each list is
1657 # a Tcl statement, and the second element is the line
1658 # number of source C file where the statement originates.
1659 set mi_autotest_data ""
1660 # The name of the source file for autotesting.
1661 set mi_autotest_source ""
1662
1663 # Prepares for running inline tests in FILENAME.
1664 # See comments for mi_run_inline_test for detailed
1665 # explanation of the idea and syntax.
1666 proc mi_prepare_inline_tests { filename } {
1667
1668 global srcdir
1669 global subdir
1670 global mi_autotest_source
1671 global mi_autotest_data
1672
1673 set mi_autotest_data {}
1674
1675 set mi_autotest_source $filename
1676
1677 if {![regexp "^/" "$filename"]} {
1678 set filename "$srcdir/$subdir/$filename"
1679 }
1680
1681 set chan [open $filename]
1682 set content [read $chan]
1683 set line_number 1
1684 while {1} {
1685 set start [string first "/*:" $content]
1686 if {$start != -1} {
1687 set end [string first ":*/" $content]
1688 if {$end == -1} {
1689 error "Unterminated special comment in $filename"
1690 }
1691
1692 set prefix [string range $content 0 $start]
1693 set prefix_newlines [count_newlines $prefix]
1694
1695 set line_number [expr $line_number+$prefix_newlines]
1696 set comment_line $line_number
1697
1698 set comment [string range $content [expr $start+3] [expr $end-1]]
1699
1700 set comment_newlines [count_newlines $comment]
1701 set line_number [expr $line_number+$comment_newlines]
1702
1703 set comment [string trim $comment]
1704 set content [string range $content [expr $end+3] \
1705 [string length $content]]
1706 lappend mi_autotest_data [list $comment $comment_line]
1707 } else {
1708 break
1709 }
1710 }
1711 close $chan
1712 }
1713
1714 # Helper to mi_run_inline_test below.
1715 # Return the list of all (statement,line_number) lists
1716 # that comprise TESTCASE. The begin and end markers
1717 # are not included.
1718 proc mi_get_inline_test {testcase} {
1719
1720 global mi_gdb_prompt
1721 global mi_autotest_data
1722 global mi_autotest_source
1723
1724 set result {}
1725
1726 set seen_begin 0
1727 set seen_end 0
1728 foreach l $mi_autotest_data {
1729
1730 set comment [lindex $l 0]
1731
1732 if {$comment == "BEGIN: $testcase"} {
1733 set seen_begin 1
1734 } elseif {$comment == "END: $testcase"} {
1735 set seen_end 1
1736 break
1737 } elseif {$seen_begin==1} {
1738 lappend result $l
1739 }
1740 }
1741
1742 if {$seen_begin == 0} {
1743 error "Autotest $testcase not found"
1744 }
1745
1746 if {$seen_begin == 1 && $seen_end == 0} {
1747 error "Missing end marker for test $testcase"
1748 }
1749
1750 return $result
1751 }
1752
1753 # Sets temporary breakpoint at LOCATION.
1754 proc mi_tbreak {location test} {
1755
1756 global mi_gdb_prompt
1757
1758 mi_gdb_test "-break-insert -t $location" \
1759 {\^done,bkpt=.*} \
1760 $test
1761 }
1762
1763 # Send COMMAND that must be a command that resumes
1764 # the inferior (run/continue/next/etc) and consumes
1765 # the "^running" output from it.
1766 proc mi_send_resuming_command_raw {command test} {
1767
1768 global mi_gdb_prompt
1769 global thread_selected_re
1770 global library_loaded_re
1771
1772 send_gdb "$command\n"
1773 gdb_expect {
1774 -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n($library_loaded_re)*($thread_selected_re)?${mi_gdb_prompt}" {
1775 # Note that lack of 'pass' call here -- this works around limitation
1776 # in DejaGNU xfail mechanism. mi-until.exp has this:
1777 #
1778 # setup_kfail gdb/2104 "*-*-*"
1779 # mi_execute_to ...
1780 #
1781 # and mi_execute_to uses mi_send_resuming_command. If we use 'pass' here,
1782 # it will reset kfail, so when the actual test fails, it will be flagged
1783 # as real failure.
1784 return 0
1785 }
1786 -re "\\^error,msg=\"Displaced stepping is only supported in ARM mode\".*" {
1787 unsupported "$test (Thumb mode)"
1788 return -1
1789 }
1790 -re "\\^error,msg=.*" {
1791 fail "$test (MI error)"
1792 return -1
1793 }
1794 -re ".*${mi_gdb_prompt}" {
1795 fail "$test (failed to resume)"
1796 return -1
1797 }
1798 timeout {
1799 fail "$test"
1800 return -1
1801 }
1802 }
1803 }
1804
1805 proc mi_send_resuming_command {command test} {
1806 mi_send_resuming_command_raw -$command $test
1807 }
1808
1809 # Helper to mi_run_inline_test below.
1810 # Sets a temporary breakpoint at LOCATION and runs
1811 # the program using COMMAND. When the program is stopped
1812 # returns the line at which it. Returns -1 if line cannot
1813 # be determined.
1814 # Does not check that the line is the same as requested.
1815 # The caller can check itself if required.
1816 proc_with_prefix mi_continue_to_line {location test} {
1817 with_test_prefix $test {
1818 mi_tbreak $location "set temporary breakpoint"
1819 mi_send_resuming_command "exec-continue" "continue to breakpoint"
1820 return [mi_get_stop_line]
1821 }
1822 }
1823
1824 # Wait until gdb prints the current line.
1825 proc mi_get_stop_line {} {
1826
1827 global mi_gdb_prompt
1828 global async
1829
1830 if {$async} {
1831 set prompt_re ""
1832 } else {
1833 set prompt_re "$mi_gdb_prompt$"
1834 }
1835
1836 gdb_expect {
1837 -re ".*line=\"(\[0-9\]*)\".*\r\n$prompt_re" {
1838 return $expect_out(1,string)
1839 }
1840 -re ".*$mi_gdb_prompt" {
1841 fail "wait for stop (unexpected output)"
1842 }
1843 timeout {
1844 fail "wait for stop (timeout)"
1845 }
1846 }
1847 }
1848
1849 # Run a MI test embedded in comments in a C file.
1850 # The C file should contain special comments in the following
1851 # three forms:
1852 #
1853 # /*: BEGIN: testname :*/
1854 # /*: <Tcl statements> :*/
1855 # /*: END: testname :*/
1856 #
1857 # This procedure find the begin and end marker for the requested
1858 # test. Then, a temporary breakpoint is set at the begin
1859 # marker and the program is run (from start).
1860 #
1861 # After that, for each special comment between the begin and end
1862 # marker, the Tcl statements are executed. It is assumed that
1863 # for each comment, the immediately preceding line is executable
1864 # C statement. Then, gdb will be single-stepped until that
1865 # preceding C statement is executed, and after that the
1866 # Tcl statements in the comment will be executed.
1867 #
1868 # For example:
1869 #
1870 # /*: BEGIN: assignment-test :*/
1871 # v = 10;
1872 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1873 # /*: END: assignment-test :*/
1874 #
1875 # The mi_prepare_inline_tests function should be called before
1876 # calling this function. A given C file can contain several
1877 # inline tests. The names of the tests must be unique within one
1878 # C file.
1879 #
1880 proc mi_run_inline_test { testcase } {
1881
1882 global mi_gdb_prompt
1883 global hex
1884 global decimal
1885 global fullname_syntax
1886 global mi_autotest_source
1887
1888 set commands [mi_get_inline_test $testcase]
1889
1890 set first 1
1891 set line_now 1
1892
1893 foreach c $commands {
1894 set statements [lindex $c 0]
1895 set line [lindex $c 1]
1896 set line [expr $line-1]
1897
1898 # We want gdb to be stopped at the expression immediately
1899 # before the comment. If this is the first comment, the
1900 # program is either not started yet or is in some random place,
1901 # so we run it. For further comments, we might be already
1902 # standing at the right line. If not continue till the
1903 # right line.
1904
1905 if {$first==1} {
1906 # Start the program afresh.
1907 mi_tbreak "$mi_autotest_source:$line" "set temporary breakpoint"
1908 if { [mi_run_cmd] < 0 } {
1909 return -1
1910 }
1911 set line_now [mi_get_stop_line]
1912 set first 0
1913 } elseif {$line_now!=$line} {
1914 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1915 }
1916
1917 if {$line_now!=$line} {
1918 fail "$testcase: go to line $line"
1919 }
1920
1921 # We're not at the statement right above the comment.
1922 # Execute that statement so that the comment can test
1923 # the state after the statement is executed.
1924
1925 # Single-step past the line.
1926 if { [mi_send_resuming_command "exec-next" "$testcase: step over $line"] != 0 } {
1927 return -1
1928 }
1929 set line_now [mi_get_stop_line]
1930
1931 # We probably want to use 'uplevel' so that statements
1932 # have direct access to global variables that the
1933 # main 'exp' file has set up. But it's not yet clear,
1934 # will need more experience to be sure.
1935 eval $statements
1936 }
1937
1938 return 0
1939 }
1940
1941 proc get_mi_thread_list {name} {
1942 global expect_out
1943
1944 # MI will return a list of thread ids:
1945 #
1946 # -thread-list-ids
1947 # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
1948 # (gdb)
1949 mi_gdb_test "-thread-list-ids" \
1950 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1951 "-thread_list_ids ($name)"
1952
1953 set output {}
1954 if {[info exists expect_out(buffer)]} {
1955 set output $expect_out(buffer)
1956 }
1957
1958 set thread_list {}
1959 if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
1960 fail "finding threads in MI output ($name)"
1961 } else {
1962 pass "finding threads in MI output ($name)"
1963
1964 # Make list of console threads
1965 set start [expr {[string first \{ $threads] + 1}]
1966 set end [expr {[string first \} $threads] - 1}]
1967 set threads [string range $threads $start $end]
1968 foreach thread [split $threads ,] {
1969 if {[scan $thread {thread-id="%d"} num]} {
1970 lappend thread_list $num
1971 }
1972 }
1973 }
1974
1975 return $thread_list
1976 }
1977
1978 # Check that MI and the console know of the same threads.
1979 # Appends NAME to all test names.
1980 proc check_mi_and_console_threads {name} {
1981 global expect_out
1982
1983 mi_gdb_test "-thread-list-ids" \
1984 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1985 "-thread-list-ids ($name)"
1986 set mi_output {}
1987 if {[info exists expect_out(buffer)]} {
1988 set mi_output $expect_out(buffer)
1989 }
1990
1991 # GDB will return a list of thread ids and some more info:
1992 #
1993 # (gdb)
1994 # -interpreter-exec console "info threads"
1995 # ~" 4 Thread 2051 (LWP 7734) 0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
1996 # ~" 3 Thread 1026 (LWP 7733) () at __libc_nanosleep:-1"
1997 # ~" 2 Thread 2049 (LWP 7732) 0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
1998 # ~"* 1 Thread 1024 (LWP 7731) main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
1999 # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
2000 mi_gdb_test "info threads" \
2001 {.*(~".*"[\r\n]*)+.*} \
2002 "info threads ($name)"
2003 set console_output {}
2004 if {[info exists expect_out(buffer)]} {
2005 set console_output $expect_out(buffer)
2006 }
2007
2008 # Make a list of all known threads to console (gdb's thread IDs)
2009 set console_thread_list {}
2010 foreach line [split $console_output \n] {
2011 if {[string index $line 0] == "~"} {
2012 # This is a line from the console; trim off "~", " ", "*", and "\""
2013 set line [string trim $line ~\ \"\*]
2014 if {[scan $line "%d" id] == 1} {
2015 lappend console_thread_list $id
2016 }
2017 }
2018 }
2019
2020 # Now find the result string from MI
2021 set mi_result ""
2022 foreach line [split $mi_output \n] {
2023 if {[string range $line 0 4] == "^done"} {
2024 set mi_result $line
2025 }
2026 }
2027 if {$mi_result == ""} {
2028 fail "finding MI result string ($name)"
2029 } else {
2030 pass "finding MI result string ($name)"
2031 }
2032
2033 # Finally, extract the thread ids and compare them to the console
2034 set num_mi_threads_str ""
2035 if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
2036 fail "finding number of threads in MI output ($name)"
2037 } else {
2038 pass "finding number of threads in MI output ($name)"
2039
2040 # Extract the number of threads from the MI result
2041 if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
2042 fail "got number of threads from MI ($name)"
2043 } else {
2044 pass "got number of threads from MI ($name)"
2045
2046 # Check if MI and console have same number of threads
2047 if {$num_mi_threads != [llength $console_thread_list]} {
2048 fail "console and MI have same number of threads ($name)"
2049 } else {
2050 pass "console and MI have same number of threads ($name)"
2051
2052 # Get MI thread list
2053 set mi_thread_list [get_mi_thread_list $name]
2054
2055 # Check if MI and console have the same threads
2056 set fails 0
2057 foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
2058 if {$ct != $mt} {
2059 incr fails
2060 }
2061 }
2062 if {$fails > 0} {
2063 fail "MI and console have same threads ($name)"
2064
2065 # Send a list of failures to the log
2066 send_log "Console has thread ids: $console_thread_list\n"
2067 send_log "MI has thread ids: $mi_thread_list\n"
2068 } else {
2069 pass "MI and console have same threads ($name)"
2070 }
2071 }
2072 }
2073 }
2074 }
2075
2076 # Set solib-search-path to allow gdb to locate shlib FILE.
2077 proc mi_locate_shlib { file } {
2078 global mi_spawn_id
2079
2080 if ![info exists mi_spawn_id] {
2081 perror "mi_locate_shlib: GDB is not running"
2082 }
2083
2084 # If the target is remote, we need to tell gdb where to find the
2085 # libraries.
2086 if { ![is_remote target] } {
2087 return
2088 }
2089
2090 # We could set this even when not testing remotely, but a user
2091 # generally won't set it unless necessary. In order to make the tests
2092 # more like the real-life scenarios, we don't set it for local testing.
2093 mi_gdb_test "set solib-search-path [file dirname $file]" "\^done" ""
2094 }
2095
2096 # Copy shlib FILE to the target and set solib-search-path to allow gdb to
2097 # locate it.
2098 proc mi_load_shlib { file } {
2099 set dest [gdb_download_shlib $file]
2100 mi_locate_shlib $file
2101 return $dest
2102 }
2103
2104 # Download shared libraries to the target.
2105 proc mi_load_shlibs { args } {
2106 foreach file $args {
2107 mi_load_shlib $file
2108 }
2109 }
2110
2111 proc mi_check_thread_states { states test } {
2112 global expect_out
2113 set pattern ".*\\^done,threads=\\\["
2114 foreach s $states {
2115 set pattern "${pattern}(.*)state=\"$s\""
2116 }
2117 set pattern "${pattern}(,core=\"\[0-9\]*\")?\\\}\\\].*"
2118
2119 verbose -log "expecting: $pattern"
2120 mi_gdb_test "-thread-info" $pattern $test
2121 }
2122
2123 # Return a list of MI features supported by this gdb.
2124 proc mi_get_features {} {
2125 global expect_out mi_gdb_prompt
2126
2127 send_gdb "-list-features\n"
2128
2129 gdb_expect {
2130 -re "\\^done,features=\\\[(.*)\\\]\r\n$mi_gdb_prompt$" {
2131 regsub -all -- \" $expect_out(1,string) "" features
2132 return [split $features ,]
2133 }
2134 -re ".*\r\n$mi_gdb_prompt$" {
2135 verbose -log "got $expect_out(buffer)"
2136 return ""
2137 }
2138 timeout {
2139 verbose -log "timeout in mi_gdb_prompt"
2140 return ""
2141 }
2142 }
2143 }
2144
2145 # Variable Object Trees
2146 #
2147 # Yet another way to check varobjs. Pass mi_walk_varobj_tree a "list" of
2148 # variables (not unlike the actual source code definition), and it will
2149 # automagically test the children for you (by default).
2150 #
2151 # Example:
2152 #
2153 # source code:
2154 # struct bar {
2155 # union {
2156 # int integer;
2157 # void *ptr;
2158 # };
2159 # const int *iPtr;
2160 # };
2161 #
2162 # class foo {
2163 # public:
2164 # int a;
2165 # struct {
2166 # int b;
2167 # struct bar *c;
2168 # };
2169 # };
2170 #
2171 # foo *f = new foo (); <-- break here
2172 #
2173 # We want to check all the children of "f".
2174 #
2175 # Translate the above structures into the following tree:
2176 #
2177 # set tree {
2178 # foo f {
2179 # {} public {
2180 # int a {}
2181 # anonymous struct {
2182 # {} public {
2183 # int b {}
2184 # {bar *} c {
2185 # {} public {
2186 # anonymous union {
2187 # {} public {
2188 # int integer {}
2189 # {void *} ptr {}
2190 # }
2191 # }
2192 # {const int *} iPtr {
2193 # {const int} {*iPtr} {}
2194 # }
2195 # }
2196 # }
2197 # }
2198 # }
2199 # }
2200 # }
2201 # }
2202 #
2203 # mi_walk_varobj_tree c++ $tree
2204 #
2205 # If you'd prefer to walk the tree using your own callback,
2206 # simply pass the name of the callback to mi_walk_varobj_tree.
2207 #
2208 # This callback should take one argument, the name of the variable
2209 # to process. This name is the name of a global array holding the
2210 # variable's properties (object name, type, etc).
2211 #
2212 # An example callback:
2213 #
2214 # proc my_callback {var} {
2215 # upvar #0 $var varobj
2216 #
2217 # puts "my_callback: called on varobj $varobj(obj_name)"
2218 # }
2219 #
2220 # The arrays created for each variable object contain the following
2221 # members:
2222 #
2223 # obj_name - the object name for accessing this variable via MI
2224 # display_name - the display name for this variable (exp="display_name" in
2225 # the output of -var-list-children)
2226 # type - the type of this variable (type="type" in the output
2227 # of -var-list-children, or the special tag "anonymous"
2228 # path_expr - the "-var-info-path-expression" for this variable
2229 # NOTE: This member cannot be used reliably with typedefs.
2230 # Use with caution!
2231 # See notes inside get_path_expr for more.
2232 # parent - the variable name of the parent varobj
2233 # children - a list of children variable names (which are the
2234 # names Tcl arrays, not object names)
2235 #
2236 # For each variable object, an array containing the above fields will
2237 # be created under the root node (conveniently called, "root"). For example,
2238 # a variable object with handle "OBJ.public.0_anonymous.a" will have
2239 # a corresponding global Tcl variable named "root.OBJ.public.0_anonymous.a".
2240 #
2241 # Note that right now, this mechanism cannot be used for recursive data
2242 # structures like linked lists.
2243
2244 namespace eval ::varobj_tree {
2245 # An index which is appended to root varobjs to ensure uniqueness.
2246 variable _root_idx 0
2247
2248 # A procedure to help with debuggging varobj trees.
2249 # VARIABLE_NAME is the name of the variable to dump.
2250 # CMD, if present, is the name of the callback to output the contstructed
2251 # strings. By default, it uses expect's "send_log" command.
2252 # TERM, if present, is a terminating character. By default it is the newline.
2253 #
2254 # To output to the terminal (not the expect log), use
2255 # mi_varobj_tree_dump_variable my_variable puts ""
2256
2257 proc mi_varobj_tree_dump_variable {variable_name {cmd send_log} {term "\n"}} {
2258 upvar #0 $variable_name varobj
2259
2260 eval "$cmd \"VAR = $variable_name$term\""
2261
2262 # Explicitly encode the array indices, since outputting them
2263 # in some logical order is better than what "array names" might
2264 # return.
2265 foreach idx {obj_name parent display_name type path_expr} {
2266 eval "$cmd \"\t$idx = $varobj($idx)$term\""
2267 }
2268
2269 # Output children
2270 set num [llength $varobj(children)]
2271 eval "$cmd \"\tnum_children = $num$term\""
2272 if {$num > 0} {
2273 eval "$cmd \"\tchildren = $varobj(children)$term\""
2274 }
2275 }
2276
2277 # The default callback used by mi_walk_varobj_tree. This callback
2278 # simply checks all of VAR's children. It specifically does not test
2279 # path expressions, since that is very problematic.
2280 #
2281 # This procedure may be used in custom callbacks.
2282 proc test_children_callback {variable_name} {
2283 upvar #0 $variable_name varobj
2284
2285 if {[llength $varobj(children)] > 0} {
2286 # Construct the list of children the way mi_list_varobj_children
2287 # expects to get it:
2288 # { {obj_name display_name num_children type} ... }
2289 set children_list {}
2290 foreach child $varobj(children) {
2291 upvar #0 $child c
2292 set clist [list [string_to_regexp $c(obj_name)] \
2293 [string_to_regexp $c(display_name)] \
2294 [llength $c(children)]]
2295 if {[string length $c(type)] > 0} {
2296 lappend clist [string_to_regexp $c(type)]
2297 }
2298 lappend children_list $clist
2299 }
2300
2301 mi_list_varobj_children $varobj(obj_name) $children_list \
2302 "VT: list children of $varobj(obj_name)"
2303 }
2304 }
2305
2306 # Set the properties of the varobj represented by
2307 # PARENT_VARIABLE - the name of the parent's variable
2308 # OBJNAME - the MI object name of this variable
2309 # DISP_NAME - the display name of this variable
2310 # TYPE - the type of this variable
2311 # PATH - the path expression for this variable
2312 # CHILDREN - a list of the variable's children
2313 proc create_varobj {parent_variable objname disp_name \
2314 type path children} {
2315 upvar #0 $parent_variable parent
2316
2317 set var_name "root.$objname"
2318 global $var_name
2319 array set $var_name [list obj_name $objname]
2320 array set $var_name [list display_name $disp_name]
2321 array set $var_name [list type $type]
2322 array set $var_name [list path_expr $path]
2323 array set $var_name [list parent "$parent_variable"]
2324 array set $var_name [list children \
2325 [get_tree_children $var_name $children]]
2326 return $var_name
2327 }
2328
2329 # Should VARIABLE be used in path expressions? The CPLUS_FAKE_CHILD
2330 # varobjs and anonymous structs/unions are not used for path expressions.
2331 proc is_path_expr_parent {variable} {
2332 upvar #0 $variable varobj
2333
2334 # If the varobj's type is "", it is a CPLUS_FAKE_CHILD.
2335 # If the tail of the varobj's object name is "%d_anonymous",
2336 # then it represents an anonymous struct or union.
2337 if {[string length $varobj(type)] == 0 \
2338 || [regexp {[0-9]+_anonymous$} $varobj(obj_name)]} {
2339 return false
2340 }
2341
2342 return true
2343 }
2344
2345 # Return the path expression for the variable named NAME in
2346 # parent varobj whose variable name is given by PARENT_VARIABLE.
2347 proc get_path_expr {parent_variable name type} {
2348 upvar #0 $parent_variable parent
2349 upvar #0 $parent_variable path_parent
2350
2351 # If TYPE is "", this is one of the CPLUS_FAKE_CHILD varobjs,
2352 # which has no path expression. Likewsise for anonymous structs
2353 # and unions.
2354 if {[string length $type] == 0 \
2355 || [string compare $type "anonymous"] == 0} {
2356 return ""
2357 }
2358
2359 # Find the path parent variable.
2360 while {![is_path_expr_parent $parent_variable]} {
2361 set parent_variable $path_parent(parent)
2362 upvar #0 $parent_variable path_parent
2363 }
2364
2365 # This is where things get difficult. We do not actually know
2366 # the real type for variables defined via typedefs, so we don't actually
2367 # know whether the parent is a structure/union or not.
2368 #
2369 # So we assume everything that isn't a simple type is a compound type.
2370 set stars ""
2371 regexp {\*+} $parent(type) stars
2372 set is_compound 1
2373 if {[string index $name 0] == "*"} {
2374 set is_compound 0
2375 }
2376
2377 if {[string index $parent(type) end] == "\]"} {
2378 # Parent is an array.
2379 return "($path_parent(path_expr))\[$name\]"
2380 } elseif {$is_compound} {
2381 # Parent is a structure or union or a pointer to one.
2382 if {[string length $stars]} {
2383 set join "->"
2384 } else {
2385 set join "."
2386 }
2387
2388 global root
2389
2390 # To make matters even more hideous, varobj.c has slightly different
2391 # path expressions for C and C++.
2392 set path_expr "($path_parent(path_expr))$join$name"
2393 if {[string compare -nocase $root(language) "c"] == 0} {
2394 return $path_expr
2395 } else {
2396 return "($path_expr)"
2397 }
2398 } else {
2399 # Parent is a pointer.
2400 return "*($path_parent(path_expr))"
2401 }
2402 }
2403
2404 # Process the CHILDREN (a list of varobj_tree elements) of the variable
2405 # given by PARENT_VARIABLE. Returns a list of children variables.
2406 proc get_tree_children {parent_variable children} {
2407 upvar #0 $parent_variable parent
2408
2409 set field_idx 0
2410 set children_list {}
2411 foreach {type name children} $children {
2412 if {[string compare $parent_variable "root"] == 0} {
2413 # Root variable
2414 variable _root_idx
2415 incr _root_idx
2416 set objname "$name$_root_idx"
2417 set disp_name "$name"
2418 set path_expr "$name"
2419 } elseif {[string compare $type "anonymous"] == 0} {
2420 # Special case: anonymous types. In this case, NAME will either be
2421 # "struct" or "union".
2422 set objname "$parent(obj_name).${field_idx}_anonymous"
2423 set disp_name "<anonymous $name>"
2424 set path_expr ""
2425 set type "$name {...}"
2426 } else {
2427 set objname "$parent(obj_name).$name"
2428 set disp_name $name
2429 set path_expr [get_path_expr $parent_variable $name $type]
2430 }
2431
2432 lappend children_list [create_varobj $parent_variable $objname \
2433 $disp_name $type $path_expr $children]
2434 incr field_idx
2435 }
2436
2437 return $children_list
2438 }
2439
2440 # The main procedure to call the given CALLBACK on the elements of the
2441 # given varobj TREE. See detailed explanation above.
2442 proc walk_tree {language tree callback} {
2443 global root
2444 variable _root_idx
2445
2446 if {[llength $tree] < 3} {
2447 error "tree does not contain enough elements"
2448 }
2449
2450 set _root_idx 0
2451
2452 # Create root node and process the tree.
2453 array set root [list language $language]
2454 array set root [list obj_name "root"]
2455 array set root [list display_name "root"]
2456 array set root [list type "root"]
2457 array set root [list path_expr "root"]
2458 array set root [list parent "root"]
2459 array set root [list children [get_tree_children root $tree]]
2460
2461 # Walk the tree
2462 set all_nodes $root(children); # a stack of nodes
2463 while {[llength $all_nodes] > 0} {
2464 # "Pop" the name of the global variable containing this varobj's
2465 # information from the stack of nodes.
2466 set var_name [lindex $all_nodes 0]
2467 set all_nodes [lreplace $all_nodes 0 0]
2468
2469 # Bring the global named in VAR_NAME into scope as the local variable
2470 # VAROBJ.
2471 upvar #0 $var_name varobj
2472
2473 # Append any children of VAROBJ to the list of nodes to walk.
2474 if {[llength $varobj(children)] > 0} {
2475 set all_nodes [concat $all_nodes $varobj(children)]
2476 }
2477
2478 # If this is a root variable, create the variable object for it.
2479 if {[string compare $varobj(parent) "root"] == 0} {
2480 mi_create_varobj $varobj(obj_name) $varobj(display_name) \
2481 "VT: create root varobj for $varobj(display_name)"
2482 }
2483
2484 # Now call the callback for VAROBJ.
2485 uplevel #0 $callback $var_name
2486 }
2487 }
2488 }
2489
2490 # The default varobj tree callback, which simply tests -var-list-children.
2491 proc mi_varobj_tree_test_children_callback {variable} {
2492 ::varobj_tree::test_children_callback $variable
2493 }
2494
2495 # Walk the variable object tree given by TREE, calling the specified
2496 # CALLBACK. By default this uses mi_varobj_tree_test_children_callback.
2497 proc mi_walk_varobj_tree {language tree \
2498 {callback \
2499 mi_varobj_tree_test_children_callback}} {
2500 ::varobj_tree::walk_tree $language $tree $callback
2501 }
2502
2503 # Build a list of key-value pairs given by the list ATTR_LIST. Flatten
2504 # this list using the optional JOINER, a comma by default.
2505 #
2506 # The list must contain an even number of elements, which are the key-value
2507 # pairs. Each value will be surrounded by quotes, according to the grammar,
2508 # except if the value starts with \[ or \{, when the quotes will be omitted.
2509 #
2510 # Example: mi_build_kv_pairs {a b c d e f g \[.*\]}
2511 # returns a=\"b\",c=\"d\",e=\"f\",g=\[.*\]
2512 proc mi_build_kv_pairs {attr_list {joiner ,}} {
2513 set l {}
2514 foreach {var value} $attr_list {
2515 if {[string range $value 0 1] == "\\\["
2516 || [string range $value 0 1] == "\\\{"} {
2517 lappend l "$var=$value"
2518 } else {
2519 lappend l "$var=\"$value\""
2520 }
2521 }
2522 return "[join $l $joiner]"
2523 }
2524
2525 # Construct a breakpoint location regexp. This may be used along with
2526 # mi_make_breakpoint_multi to test the output of -break-insert,
2527 # -dprintf-insert, or -break-info with breapoints with multiple
2528 # locations.
2529 #
2530 # All arguments for the breakpoint location may be specified using the
2531 # options: number, enabled, addr, func, file, fullname, line,
2532 # thread-groups, and thread.
2533 #
2534 # For the option -thread the corresponding output field is only added
2535 # if the option is present and not set to the empty string.
2536 #
2537 # Example: mi_make_breakpoint_loc -number 2.1 -file ".*/myfile.c" -line 3
2538 # will return the breakpoint location:
2539 # {number="2.1",enabled=".*",addr=".*",func=".*",
2540 # file=".*/myfile.c",fullname=".*",line="3",thread-groups=\[.*\]}
2541
2542 proc mi_make_breakpoint_loc {args} {
2543 parse_args {{number .*} {enabled .*} {addr .*}
2544 {func .*} {file .*} {fullname .*} {line .*}
2545 {thread-groups \\\[.*\\\]} {thread ""}}
2546
2547 set attr_list {}
2548 foreach attr [list number enabled addr func file \
2549 fullname line thread-groups] {
2550 lappend attr_list $attr [set $attr]
2551 }
2552
2553 set result [mi_build_kv_pairs $attr_list]
2554
2555 if {[string length $thread] > 0} {
2556 append result ","
2557 append result [mi_build_kv_pairs [list "thread" $thread]]
2558 }
2559
2560 return "{$result}"
2561 }
2562
2563 # Bits shared between mi_make_breakpoint and mi_make_breakpoint_multi.
2564
2565 proc mi_make_breakpoint_1 {attr_list thread cond evaluated-by times \
2566 ignore script original-location} {
2567 set result "bkpt=\\\{[mi_build_kv_pairs $attr_list]"
2568
2569 # There are always exceptions.
2570
2571 # If THREAD is not present, do not output it.
2572 if {[string length $thread] > 0} {
2573 append result ","
2574 append result [mi_build_kv_pairs [list "thread" $thread]]
2575 }
2576
2577 # If COND is not present, do not output it.
2578 if {[string length $cond] > 0} {
2579 append result ","
2580 append result [mi_build_kv_pairs [list "cond" $cond]]
2581
2582 # When running on a remote, GDB may output who is evaluating
2583 # breakpoint conditions.
2584 if {[string length ${evaluated-by}] > 0} {
2585 append result [mi_build_kv_pairs \
2586 [list "evaluated-by" ${evaluated-by}]]
2587 } else {
2588 append result {(,evaluated-by=".*")?}
2589 }
2590 }
2591
2592 append result ","
2593 append result [mi_build_kv_pairs [list "times" $times]]
2594
2595 # If SCRIPT and IGNORE are not present, do not output them.
2596 if {$ignore != 0} {
2597 append result ","
2598 append result [mi_build_kv_pairs [list "ignore" $ignore]]
2599 append result ","
2600 }
2601 if {[string length $script] > 0} {
2602 append result ","
2603 append result [mi_build_kv_pairs [list "script" $script]]
2604 append result ","
2605 } else {
2606 # Allow anything up until the next "official"/required attribute.
2607 # This pattern skips over script/ignore if matches on those
2608 # were not specifically required by the caller.
2609 append result ".*"
2610 }
2611 append result [mi_build_kv_pairs \
2612 [list "original-location" ${original-location}]]
2613
2614 return $result
2615 }
2616
2617
2618 # Construct a breakpoint regexp, for a breakpoint with multiple
2619 # locations. This may be used to test the output of -break-insert,
2620 # -dprintf-insert, or -break-info with breakpoints with multiple
2621 # locations.
2622 #
2623 # All arguments for the breakpoint may be specified using the options:
2624 # number, type, disp, enabled, times, ignore, script,
2625 # original-location, cond, evaluated-by, locations, and thread.
2626 #
2627 # Only if -script and -ignore are given will they appear in the output.
2628 # Otherwise, this procedure will skip them using ".*".
2629 #
2630 # For the options -thread and -cond the corresponding output fields
2631 # are only added if the options are present and not set to the empty
2632 # string.
2633 #
2634 # Example: mi_make_breakpoint_multi -number 2 -locations "$loc"
2635 # will return the breakpoint:
2636 # bkpt={number="2",type=".*",disp=".*",enabled=".*",addr="<MULTIPLE>",
2637 # times="0".*original-location=".*",locations=$loc}
2638 #
2639 # You can construct the list of locations with mi_make_breakpoint_loc.
2640
2641 proc mi_make_breakpoint_multi {args} {
2642 parse_args {{number .*} {type .*} {disp .*} {enabled .*}
2643 {times .*} {ignore 0}
2644 {script ""} {original-location .*} {cond ""} {evaluated-by ""}
2645 {locations .*} {thread ""}}
2646
2647 set attr_list {}
2648 foreach attr [list number type disp enabled] {
2649 lappend attr_list $attr [set $attr]
2650 }
2651
2652 lappend attr_list "addr" "<MULTIPLE>"
2653
2654 set result [mi_make_breakpoint_1 \
2655 $attr_list $thread $cond ${evaluated-by} $times \
2656 $ignore $script ${original-location}]
2657
2658 append result ","
2659 append result [mi_build_kv_pairs [list "locations" $locations]]
2660
2661 append result "\\\}"
2662 return $result
2663 }
2664
2665 # Construct a breakpoint regexp, for a pending breakpoint. This may
2666 # be used to test the output of -break-insert, -dprintf-insert, or
2667 # -break-info for pending breakpoints.
2668 #
2669 # Arguments for the breakpoint may be specified using the options:
2670 # number, type, disp, enabled, pending, original-location, thread, and
2671 # cond.
2672 #
2673 # For the options -thread and -cond the corresponding output fields
2674 # are only included if the options are present and not the empty
2675 # string.
2676 #
2677 # Example: mi_make_breakpoint_pending -number 2 -pending func
2678 # will return the breakpoint:
2679 # bkpt={number="2",type=".*",disp=".*",enabled=".*",addr="<PENDING>",
2680 # pending="func", times="0".*original-location=".*"}
2681
2682 proc mi_make_breakpoint_pending {args} {
2683 parse_args {{number .*} {type .*} {disp .*} {enabled .*}
2684 {pending .*} {original-location .*} {thread ""} {cond ""}}
2685
2686 set attr_list {}
2687 foreach attr [list number type disp enabled] {
2688 lappend attr_list $attr [set $attr]
2689 }
2690
2691 lappend attr_list "addr" "<PENDING>"
2692
2693 foreach attr [list pending] {
2694 lappend attr_list $attr [set $attr]
2695 }
2696
2697 set ignore 0
2698 set times 0
2699 set script ""
2700 set evaluated-by ""
2701
2702 set result [mi_make_breakpoint_1 \
2703 $attr_list $thread $cond ${evaluated-by} $times \
2704 $ignore $script ${original-location}]
2705
2706 append result "\\\}"
2707 return $result
2708 }
2709
2710 # Construct a breakpoint regexp. This may be used to test the output of
2711 # -break-insert, -dprintf-insert, or -break-info.
2712 #
2713 # All arguments for the breakpoint may be specified using the options
2714 # number, type, disp, enabled, addr, func, file, fullanme, line,
2715 # thread-groups, cond, evaluated-by, times, ignore, script,
2716 # and original-location.
2717 #
2718 # Only if -script and -ignore are given will they appear in the output.
2719 # Otherwise, this procedure will skip them using ".*".
2720 #
2721 # Example: mi_make_breakpoint -number 2 -file ".*/myfile.c" -line 3
2722 # will return the breakpoint:
2723 # bkpt={number="2",type=".*",disp=".*",enabled=".*",addr=".*",func=".*",
2724 # file=".*/myfile.c",fullname=".*",line="3",thread-groups=\[.*\],
2725 # times="0".*original-location=".*"}
2726
2727 proc mi_make_breakpoint {args} {
2728 parse_args {{number .*} {type .*} {disp .*} {enabled .*} {addr .*}
2729 {func .*} {file .*} {fullname .*} {line .*}
2730 {thread-groups \\\[.*\\\]} {times .*} {ignore 0}
2731 {script ""} {original-location .*} {cond ""} {evaluated-by ""}
2732 {thread ""}}
2733
2734 set attr_list {}
2735 foreach attr [list number type disp enabled addr func file \
2736 fullname line thread-groups] {
2737 lappend attr_list $attr [set $attr]
2738 }
2739
2740 set result [mi_make_breakpoint_1 \
2741 $attr_list $thread $cond ${evaluated-by} $times \
2742 $ignore $script ${original-location}]
2743
2744 append result "\\\}"
2745 return $result
2746 }
2747
2748 # Build a breakpoint table regexp given the list of breakpoints in `bp_list',
2749 # constructed by mi_make_breakpoint.
2750 #
2751 # Example: Construct a breakpoint table where the only attributes we
2752 # test for are the existence of three breakpoints numbered 1, 2, and 3.
2753 #
2754 # set bps {}
2755 # lappend bps [mi_make_breakpoint -number 1]
2756 # lappend bps [mi_make_breakpoint -number 2]
2757 # lappned bps [mi_make_breakpoint -number 3]
2758 # mi_make_breakpoint_table $bps
2759 # will return (abbreviated for clarity):
2760 # BreakpointTable={nr_rows="3",nr_cols="6",hdr=[{width=".*",...} ...],
2761 # body=[bkpt={number="1",...},bkpt={number="2",...},bkpt={number="3",...}]}
2762
2763 proc mi_make_breakpoint_table {bp_list} {
2764 # Build header -- assume a standard header for all breakpoint tables.
2765 set hl {}
2766 foreach {nm hdr} [list number Num type Type disp Disp enabled Enb \
2767 addr Address what What] {
2768 # The elements here are the MI table headers, which have the
2769 # format:
2770 # {width="7",alignment="-1",col_name="number",colhdr="Num"}
2771 lappend hl "{[mi_build_kv_pairs [list width .* alignment .* \
2772 col_name $nm colhdr $hdr]]}"
2773 }
2774 set header "hdr=\\\[[join $hl ,]\\\]"
2775
2776 # The caller has implicitly supplied the number of columns and rows.
2777 set nc [llength $hl]
2778 set nr [llength $bp_list]
2779
2780 # Build body -- mi_make_breakpoint has done most of the work.
2781 set body "body=\\\[[join $bp_list ,]\\\]"
2782
2783 # Assemble the final regexp.
2784 return "BreakpointTable={nr_rows=\"$nr\",nr_cols=\"$nc\",$header,$body}"
2785 }
2786
2787 # As skip_libstdcxx_probe_tests_prompt, with mi_gdb_prompt.
2788
2789 proc mi_skip_libstdcxx_probe_tests {} {
2790 global mi_gdb_prompt
2791 return [skip_libstdcxx_probe_tests_prompt "$mi_gdb_prompt$"]
2792 }
2793
2794 # Check whether we're testing with the remote or extended-remote
2795 # targets.
2796
2797 proc mi_is_target_remote {} {
2798 global mi_gdb_prompt
2799
2800 return [gdb_is_target_remote_prompt "$mi_gdb_prompt"]
2801 }
2802
2803 # Retrieve the value of EXP in the inferior, represented in format
2804 # specified in FMT (using "printFMT"). DEFAULT is used as fallback if
2805 # print fails. TEST is the test message to use. It can be omitted,
2806 # in which case a test message is built from EXP.
2807 #
2808 # This is an MI version of gdb_valueof.
2809
2810 proc mi_get_valueof { fmt exp default {test ""} } {
2811 global mi_gdb_prompt
2812
2813 if {$test == "" } {
2814 set test "get valueof \"${exp}\""
2815 }
2816
2817 set val ${default}
2818 gdb_test_multiple "print${fmt} ${exp}" "$test" -prompt "$::mi_gdb_prompt$" {
2819 -re "~\"\\$\[0-9\]* = (\[^\r\n\]*)\\\\n\"\r\n\\^done\r\n$mi_gdb_prompt$" {
2820 set val $expect_out(1,string)
2821 pass "$test"
2822 }
2823 timeout {
2824 fail "$test (timeout)"
2825 }
2826 }
2827 return ${val}
2828 }
2829
2830 # Some MI tests should be run in the normal way, on the main UI, while
2831 # other tests should be run twice, once when the MI is on the main UI,
2832 # and once with the MI on a secondary UI, this proc facilitates that.
2833 #
2834 # Use as:
2835 #
2836 # foreach_mi_ui_mode mode {
2837 # # ... body ...
2838 # }
2839 #
2840 # The BODY will then be run once with MODE set to 'main' and once with
2841 # MODE set to 'separate'.
2842 #
2843 # However, there are times when we know using the 'separate' UI will
2844 # not work. This proc handles figuring that out, if the 'separate' UI
2845 # is known not to work then the 'separate' mode will be skipped and
2846 # BODY will be run just once with MODE set to 'main'.
2847
2848 proc foreach_mi_ui_mode { var_name body } {
2849 upvar 1 $var_name var
2850
2851 if [gdb_debug_enabled] {
2852 # gdb debug doesn't work for separate-mi-tty.
2853 set modes {"main"}
2854 } else {
2855 set modes {"main" "separate"}
2856 }
2857
2858 foreach var $modes {
2859 with_test_prefix "$var_name=$var" {
2860 set code [catch {uplevel 1 $body} result]
2861 }
2862
2863 if {$code == 1} {
2864 global errorInfo errorCode
2865 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2866 } elseif {$code == 3} {
2867 break
2868 } elseif {$code == 2} {
2869 return -code $code $result
2870 }
2871 }
2872 }