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