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