Make ftrace tests work with remote targets
[binutils-gdb.git] / gdb / testsuite / lib / mi-support.exp
1 # Copyright 1999-2016 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 gdb_expect {
890 -re "${run_match}&\"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}" {}
891 }
892 return 0
893 }
894
895 send_gdb "${run_prefix}run $args\n"
896 gdb_expect {
897 -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}" {
898 }
899 -re "\\^error,msg=\"The target does not support running in non-stop mode.\"" {
900 unsupported "Non-stop mode not supported"
901 return -1
902 }
903 timeout {
904 perror "Unable to start target"
905 return -1
906 }
907 }
908 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
909
910 return 0
911 }
912
913 # A wrapper for mi_run_cmd_full which uses -exec-run and
914 # -exec-continue, as appropriate. ARGS are passed verbatim to
915 # mi_run_cmd_full.
916 proc mi_run_cmd {args} {
917 return [eval mi_run_cmd_full 1 $args]
918 }
919
920 # A wrapper for mi_run_cmd_full which uses the CLI commands 'run' and
921 # 'continue', as appropriate. ARGS are passed verbatim to
922 # mi_run_cmd_full.
923 proc mi_run_with_cli {args} {
924 return [eval mi_run_cmd_full 0 $args]
925 }
926
927 #
928 # Just like run-to-main but works with the MI interface
929 #
930
931 proc mi_run_to_main { } {
932 global suppress_flag
933 if { $suppress_flag } {
934 return -1
935 }
936
937 global srcdir
938 global subdir
939 global binfile
940 global srcfile
941
942 mi_delete_breakpoints
943 mi_gdb_reinitialize_dir $srcdir/$subdir
944 mi_gdb_load ${binfile}
945
946 mi_runto main
947 }
948
949
950 # Just like gdb's "runto" proc, it will run the target to a given
951 # function. The big difference here between mi_runto and mi_execute_to
952 # is that mi_execute_to must have the inferior running already. This
953 # proc will (like gdb's runto) (re)start the inferior, too.
954 #
955 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
956 # It returns:
957 # -1 if test suppressed, failed, timedout
958 # 0 if test passed
959
960 proc mi_runto_helper {func run_or_continue} {
961 global suppress_flag
962 if { $suppress_flag } {
963 return -1
964 }
965
966 global mi_gdb_prompt expect_out
967 global hex decimal fullname_syntax
968
969 set test "mi runto $func"
970 set bp [mi_make_breakpoint -type breakpoint -disp del \
971 -func $func\(\\\(.*\\\)\)?]
972 mi_gdb_test "200-break-insert -t $func" "200\\^done,$bp" \
973 "breakpoint at $func"
974
975 if {$run_or_continue == "run"} {
976 if { [mi_run_cmd] < 0 } {
977 return -1
978 }
979 } else {
980 mi_send_resuming_command "exec-continue" "$test"
981 }
982
983 mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test
984 }
985
986 proc mi_runto {func} {
987 return [mi_runto_helper $func "run"]
988 }
989
990 # Next to the next statement
991 # For return values, see mi_execute_to_helper
992
993 proc mi_next { test } {
994 return [mi_next_to {.*} {.*} {.*} {.*} $test]
995 }
996
997
998 # Step to the next statement
999 # For return values, see mi_execute_to_helper
1000
1001 proc mi_step { test } {
1002 return [mi_step_to {.*} {.*} {.*} {.*} $test]
1003 }
1004
1005 set async "unknown"
1006
1007 proc mi_detect_async {} {
1008 global async
1009 global mi_gdb_prompt
1010
1011 send_gdb "show mi-async\n"
1012
1013 gdb_expect {
1014 -re "asynchronous mode is on...*$mi_gdb_prompt$" {
1015 set async 1
1016 }
1017 -re ".*$mi_gdb_prompt$" {
1018 set async 0
1019 }
1020 timeout {
1021 set async 0
1022 }
1023 }
1024 return $async
1025 }
1026
1027 # Wait for MI *stopped notification to appear.
1028 # The REASON, FUNC, ARGS, FILE and LINE are regular expressions
1029 # to match against whatever is output in *stopped. FILE may also match
1030 # filename of a file without debug info. ARGS should not include [] the
1031 # list of argument is enclosed in, and other regular expressions should
1032 # not include quotes.
1033 # If EXTRA is a list of one element, it's the regular expression
1034 # for output expected right after *stopped, and before GDB prompt.
1035 # If EXTRA is a list of two elements, the first element is for
1036 # output right after *stopped, and the second element is output
1037 # right after reason field. The regex after reason should not include
1038 # the comma separating it from the following fields.
1039 #
1040 # When we fail to match output at all, -1 is returned. If FILE does
1041 # match and the target system has no debug info for FILE return 0.
1042 # Otherwise, the line at which we stop is returned. This is useful when
1043 # exact line is not possible to specify for some reason -- one can pass
1044 # the .* or "\[0-9\]*" regexps for line, and then check the line
1045 # programmatically.
1046 #
1047 # Do not pass .* for any argument if you are expecting more than one stop.
1048 proc mi_expect_stop { reason func args file line extra test } {
1049
1050 global mi_gdb_prompt
1051 global hex
1052 global decimal
1053 global fullname_syntax
1054 global async
1055 global thread_selected_re
1056 global breakpoint_re
1057
1058 set any "\[^\n\]*"
1059
1060 set after_stopped ""
1061 set after_reason ""
1062 if { [llength $extra] == 2 } {
1063 set after_stopped [lindex $extra 0]
1064 set after_reason [lindex $extra 1]
1065 set after_reason "${after_reason},"
1066 } elseif { [llength $extra] == 1 } {
1067 set after_stopped [lindex $extra 0]
1068 }
1069
1070 if {$async} {
1071 set prompt_re ""
1072 } else {
1073 set prompt_re "$mi_gdb_prompt$"
1074 }
1075
1076 if { $reason == "really-no-reason" } {
1077 gdb_expect {
1078 -re "\\*stopped\r\n$prompt_re" {
1079 pass "$test"
1080 }
1081 timeout {
1082 fail "$test (timeout)"
1083 }
1084 }
1085 return
1086 }
1087
1088 if { $reason == "exited-normally" } {
1089
1090 gdb_expect {
1091 -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re" {
1092 pass "$test"
1093 }
1094 -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
1095 timeout {
1096 fail "$test (timeout)"
1097 }
1098 }
1099 return
1100 }
1101 if { $reason == "exited" } {
1102 gdb_expect {
1103 -re "\\*stopped,reason=\"exited\",exit-code=\"\[0-7\]+\"\r\n$prompt_re" {
1104 pass "$test"
1105 }
1106 -re ".*$mi_gdb_prompt$" {
1107 fail "$test (inferior not stopped)"
1108 }
1109 timeout {
1110 fail "$test (timeout)"
1111 }
1112 }
1113 return
1114 }
1115
1116 if { $reason == "solib-event" } {
1117 set pattern "\\*stopped,reason=\"solib-event\",thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1118 verbose -log "mi_expect_stop: expecting: $pattern"
1119 gdb_expect {
1120 -re "$pattern" {
1121 pass "$test"
1122 }
1123 timeout {
1124 fail "$test (timeout)"
1125 }
1126 }
1127 return
1128 }
1129
1130 set args "\\\[$args\\\]"
1131
1132 set bn ""
1133 if { $reason == "breakpoint-hit" } {
1134 set bn {bkptno="[0-9]+",}
1135 } elseif { $reason == "solib-event" } {
1136 set bn ".*"
1137 }
1138
1139 set r ""
1140 if { $reason != "" } {
1141 set r "reason=\"$reason\","
1142 }
1143
1144
1145 set a $after_reason
1146
1147 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"
1148 gdb_expect {
1149 -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" {
1150 pass "$test"
1151 if {[array names expect_out "2,string"] != ""} {
1152 return $expect_out(2,string)
1153 }
1154 # No debug info available but $file does match.
1155 return 0
1156 }
1157 -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" {
1158 verbose -log "got $expect_out(buffer)"
1159 fail "$test (stopped at wrong place)"
1160 return -1
1161 }
1162 -re ".*\r\n$mi_gdb_prompt$" {
1163 verbose -log "got $expect_out(buffer)"
1164 fail "$test (unknown output after running)"
1165 return -1
1166 }
1167 timeout {
1168 fail "$test (timeout)"
1169 return -1
1170 }
1171 }
1172 }
1173
1174 # Wait for MI *stopped notification related to an interrupt request to
1175 # appear.
1176 proc mi_expect_interrupt { test } {
1177 global mi_gdb_prompt
1178 global decimal
1179 global async
1180
1181 if {$async} {
1182 set prompt_re ""
1183 } else {
1184 set prompt_re "$mi_gdb_prompt$"
1185 }
1186
1187 set r_nonstop "reason=\"signal-received\",signal-name=\"0\",signal-meaning=\"Signal 0\""
1188 set r_allstop "reason=\"signal-received\",signal-name=\"SIGINT\",signal-meaning=\"Interrupt\""
1189 set r "(${r_nonstop}|${r_allstop})"
1190 set any "\[^\n\]*"
1191
1192 # A signal can land anywhere, just ignore the location
1193 verbose -log "mi_expect_interrupt: expecting: \\*stopped,${r}$any\r\n$prompt_re"
1194 gdb_expect {
1195 -re "\\*stopped,${r}$any\r\n$prompt_re" {
1196 pass "$test"
1197 return 0
1198 }
1199 -re ".*\r\n$mi_gdb_prompt$" {
1200 verbose -log "got $expect_out(buffer)"
1201 fail "$test (unknown output after running)"
1202 return -1
1203 }
1204 timeout {
1205 fail "$test (timeout)"
1206 return -1
1207 }
1208 }
1209 }
1210
1211 # cmd should not include the number or newline (i.e. "exec-step 3", not
1212 # "220-exec-step 3\n"
1213
1214 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1215 # after the first prompt is printed.
1216
1217 proc mi_execute_to { cmd reason func args file line extra test } {
1218 global suppress_flag
1219 if { $suppress_flag } {
1220 return -1
1221 }
1222
1223 mi_send_resuming_command "$cmd" "$test"
1224 set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1225 return $r
1226 }
1227
1228 proc mi_next_to { func args file line test } {
1229 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1230 "$file" "$line" "" "$test"
1231 }
1232
1233 proc mi_step_to { func args file line test } {
1234 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1235 "$file" "$line" "" "$test"
1236 }
1237
1238 proc mi_finish_to { func args file line result ret test } {
1239 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
1240 "$file" "$line" \
1241 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1242 "$test"
1243 }
1244
1245 proc mi_continue_to {func} {
1246 mi_runto_helper $func "continue"
1247 }
1248
1249 proc mi0_execute_to { cmd reason func args file line extra test } {
1250 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
1251 "$file" "$line" "$extra" "$test"
1252 }
1253
1254 proc mi0_next_to { func args file line test } {
1255 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1256 "$file" "$line" "" "$test"
1257 }
1258
1259 proc mi0_step_to { func args file line test } {
1260 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1261 "$file" "$line" "" "$test"
1262 }
1263
1264 proc mi0_finish_to { func args file line result ret test } {
1265 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1266 "$file" "$line" \
1267 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1268 "$test"
1269 }
1270
1271 proc mi0_continue_to { bkptno func args file line test } {
1272 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1273 "$func" "$args" "$file" "$line" "" "$test"
1274 }
1275
1276 # Creates a breakpoint and checks the reported fields are as expected.
1277 # This procedure takes the same options as mi_make_breakpoint and
1278 # returns the breakpoint regexp from that procedure.
1279
1280 proc mi_create_breakpoint {location test args} {
1281 set bp [eval mi_make_breakpoint $args]
1282 mi_gdb_test "222-break-insert $location" "222\\^done,$bp" $test
1283 return $bp
1284 }
1285
1286 # Creates varobj named NAME for EXPRESSION.
1287 # Name cannot be "-".
1288 proc mi_create_varobj { name expression testname } {
1289 mi_gdb_test "-var-create $name * $expression" \
1290 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*,has_more=\"0\"" \
1291 $testname
1292 }
1293
1294 proc mi_create_floating_varobj { name expression testname } {
1295 mi_gdb_test "-var-create $name @ $expression" \
1296 "\\^done,name=\"$name\",numchild=\"\(-1\|\[0-9\]+\)\",value=\".*\",type=.*" \
1297 $testname
1298 }
1299
1300
1301 # Same as mi_create_varobj, but also checks the reported type
1302 # of the varobj.
1303 proc mi_create_varobj_checked { name expression type testname } {
1304 mi_gdb_test "-var-create $name * $expression" \
1305 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1306 $testname
1307 }
1308
1309 # Same as mi_create_floating_varobj, but assumes the test is creating
1310 # a dynamic varobj that has children, so the value must be "{...}".
1311 # The "has_more" attribute is checked.
1312 proc mi_create_dynamic_varobj {name expression has_more testname} {
1313 mi_gdb_test "-var-create $name @ $expression" \
1314 "\\^done,name=\"$name\",numchild=\"0\",value=\"{\\.\\.\\.}\",type=.*,has_more=\"${has_more}\"" \
1315 $testname
1316 }
1317
1318 # Deletes the specified NAME.
1319 proc mi_delete_varobj { name testname } {
1320 mi_gdb_test "-var-delete $name" \
1321 "\\^done,ndeleted=.*" \
1322 $testname
1323 }
1324
1325 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1326 # are reported as updated, and no other varobj is updated.
1327 # Assumes that no varobj is out of scope and that no varobj changes
1328 # types.
1329 proc mi_varobj_update { name expected testname } {
1330 set er "\\^done,changelist=\\\["
1331 set first 1
1332 foreach item $expected {
1333 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\",has_more=\".\"}"
1334 if {$first == 1} {
1335 set er "$er$v"
1336 set first 0
1337 } else {
1338 set er "$er,$v"
1339 }
1340 }
1341 set er "$er\\\]"
1342
1343 verbose -log "Expecting: $er" 2
1344 mi_gdb_test "-var-update $name" $er $testname
1345 }
1346
1347 proc mi_varobj_update_with_child_type_change { name child_name new_type new_children testname } {
1348 set v "{name=\"$child_name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\",has_more=\".\"}"
1349 set er "\\^done,changelist=\\\[$v\\\]"
1350 verbose -log "Expecting: $er"
1351 mi_gdb_test "-var-update $name" $er $testname
1352 }
1353
1354 proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1355 mi_varobj_update_with_child_type_change $name $name $new_type $new_children $testname
1356 }
1357
1358 # A helper that turns a key/value list into a regular expression
1359 # matching some MI output.
1360 proc mi_varobj_update_kv_helper {list} {
1361 set first 1
1362 set rx ""
1363 foreach {key value} $list {
1364 if {!$first} {
1365 append rx ,
1366 }
1367 set first 0
1368 if {$key == "new_children"} {
1369 append rx "$key=\\\[$value\\\]"
1370 } else {
1371 append rx "$key=\"$value\""
1372 }
1373 }
1374 return $rx
1375 }
1376
1377 # A helper for mi_varobj_update_dynamic that computes a match
1378 # expression given a child list.
1379 proc mi_varobj_update_dynamic_helper {children} {
1380 set crx ""
1381
1382 set first 1
1383 foreach child $children {
1384 if {!$first} {
1385 append crx ,
1386 }
1387 set first 0
1388 append crx "{"
1389 append crx [mi_varobj_update_kv_helper $child]
1390 append crx "}"
1391 }
1392
1393 return $crx
1394 }
1395
1396 # Update a dynamic varobj named NAME. CHILDREN is a list of children
1397 # that have been updated; NEW_CHILDREN is a list of children that were
1398 # added to the primary varobj. Each child is a list of key/value
1399 # pairs that are expected. SELF is a key/value list holding
1400 # information about the varobj itself. TESTNAME is the name of the
1401 # test.
1402 proc mi_varobj_update_dynamic {name testname self children new_children} {
1403 if {[llength $new_children]} {
1404 set newrx [mi_varobj_update_dynamic_helper $new_children]
1405 lappend self new_children $newrx
1406 }
1407 set selfrx [mi_varobj_update_kv_helper $self]
1408 set crx [mi_varobj_update_dynamic_helper $children]
1409
1410 set er "\\^done,changelist=\\\[\{name=\"$name\",in_scope=\"true\""
1411 append er ",$selfrx\}"
1412 if {"$crx" != ""} {
1413 append er ",$crx"
1414 }
1415 append er "\\\]"
1416
1417 verbose -log "Expecting: $er"
1418 mi_gdb_test "-var-update $name" $er $testname
1419 }
1420
1421 proc mi_check_varobj_value { name value testname } {
1422
1423 mi_gdb_test "-var-evaluate-expression $name" \
1424 "\\^done,value=\"$value\"" \
1425 $testname
1426 }
1427
1428 # Helper proc which constructs a child regexp for
1429 # mi_list_varobj_children and mi_varobj_update_dynamic.
1430 proc mi_child_regexp {children add_child} {
1431 set children_exp {}
1432
1433 if {$add_child} {
1434 set pre "child="
1435 } else {
1436 set pre ""
1437 }
1438
1439 foreach item $children {
1440
1441 set name [lindex $item 0]
1442 set exp [lindex $item 1]
1443 set numchild [lindex $item 2]
1444 if {[llength $item] == 5} {
1445 set type [lindex $item 3]
1446 set value [lindex $item 4]
1447
1448 lappend children_exp\
1449 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1450 } elseif {[llength $item] == 4} {
1451 set type [lindex $item 3]
1452
1453 lappend children_exp\
1454 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1455 } else {
1456 lappend children_exp\
1457 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
1458 }
1459 }
1460 return [join $children_exp ","]
1461 }
1462
1463 # Check the results of the:
1464 #
1465 # -var-list-children VARNAME
1466 #
1467 # command. The CHILDREN parement should be a list of lists.
1468 # Each inner list can have either 3 or 4 elements, describing
1469 # fields that gdb is expected to report for child variable object,
1470 # in the following order
1471 #
1472 # - Name
1473 # - Expression
1474 # - Number of children
1475 # - Type
1476 #
1477 # If inner list has 3 elements, the gdb is expected to output no
1478 # type for a child and no value.
1479 #
1480 # If the inner list has 4 elements, gdb output is expected to
1481 # have no value.
1482 #
1483 proc mi_list_varobj_children { varname children testname } {
1484 mi_list_varobj_children_range $varname "" "" [llength $children] $children \
1485 $testname
1486 }
1487
1488 # Like mi_list_varobj_children, but sets a subrange. NUMCHILDREN is
1489 # the total number of children.
1490 proc mi_list_varobj_children_range {varname from to numchildren children testname} {
1491 set options ""
1492 if {[llength $varname] == 2} {
1493 set options [lindex $varname 1]
1494 set varname [lindex $varname 0]
1495 }
1496
1497 set children_exp_j [mi_child_regexp $children 1]
1498 if {$numchildren} {
1499 set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1500 } {
1501 set expected "\\^done,numchild=\"0\""
1502 }
1503
1504 if {"$to" == ""} {
1505 append expected ",has_more=\"0\""
1506 } elseif {$to >= 0 && $numchildren > $to} {
1507 append expected ",has_more=\"1\""
1508 } else {
1509 append expected ",has_more=\"0\""
1510 }
1511
1512 verbose -log "Expecting: $expected"
1513
1514 mi_gdb_test "-var-list-children $options $varname $from $to" \
1515 $expected $testname
1516 }
1517
1518 # Verifies that variable object VARNAME has NUMBER children,
1519 # where each one is named $VARNAME.<index-of-child> and has type TYPE.
1520 proc mi_list_array_varobj_children { varname number type testname } {
1521 mi_list_array_varobj_children_with_index $varname $number 0 $type $testname
1522 }
1523
1524 # Same as mi_list_array_varobj_children, but allowing to pass a start index
1525 # for an array.
1526 proc mi_list_array_varobj_children_with_index { varname number start_index \
1527 type testname } {
1528 set t {}
1529 set index $start_index
1530 for {set i 0} {$i < $number} {incr i} {
1531 lappend t [list $varname.$index $index 0 $type]
1532 incr index
1533 }
1534 mi_list_varobj_children $varname $t $testname
1535 }
1536
1537 # A list of two-element lists. First element of each list is
1538 # a Tcl statement, and the second element is the line
1539 # number of source C file where the statement originates.
1540 set mi_autotest_data ""
1541 # The name of the source file for autotesting.
1542 set mi_autotest_source ""
1543
1544 proc count_newlines { string } {
1545 return [regexp -all "\n" $string]
1546 }
1547
1548 # Prepares for running inline tests in FILENAME.
1549 # See comments for mi_run_inline_test for detailed
1550 # explanation of the idea and syntax.
1551 proc mi_prepare_inline_tests { filename } {
1552
1553 global srcdir
1554 global subdir
1555 global mi_autotest_source
1556 global mi_autotest_data
1557
1558 set mi_autotest_data {}
1559
1560 set mi_autotest_source $filename
1561
1562 if { ! [regexp "^/" "$filename"] } then {
1563 set filename "$srcdir/$subdir/$filename"
1564 }
1565
1566 set chan [open $filename]
1567 set content [read $chan]
1568 set line_number 1
1569 while {1} {
1570 set start [string first "/*:" $content]
1571 if {$start != -1} {
1572 set end [string first ":*/" $content]
1573 if {$end == -1} {
1574 error "Unterminated special comment in $filename"
1575 }
1576
1577 set prefix [string range $content 0 $start]
1578 set prefix_newlines [count_newlines $prefix]
1579
1580 set line_number [expr $line_number+$prefix_newlines]
1581 set comment_line $line_number
1582
1583 set comment [string range $content [expr $start+3] [expr $end-1]]
1584
1585 set comment_newlines [count_newlines $comment]
1586 set line_number [expr $line_number+$comment_newlines]
1587
1588 set comment [string trim $comment]
1589 set content [string range $content [expr $end+3] \
1590 [string length $content]]
1591 lappend mi_autotest_data [list $comment $comment_line]
1592 } else {
1593 break
1594 }
1595 }
1596 close $chan
1597 }
1598
1599 # Helper to mi_run_inline_test below.
1600 # Return the list of all (statement,line_number) lists
1601 # that comprise TESTCASE. The begin and end markers
1602 # are not included.
1603 proc mi_get_inline_test {testcase} {
1604
1605 global mi_gdb_prompt
1606 global mi_autotest_data
1607 global mi_autotest_source
1608
1609 set result {}
1610
1611 set seen_begin 0
1612 set seen_end 0
1613 foreach l $mi_autotest_data {
1614
1615 set comment [lindex $l 0]
1616
1617 if {$comment == "BEGIN: $testcase"} {
1618 set seen_begin 1
1619 } elseif {$comment == "END: $testcase"} {
1620 set seen_end 1
1621 break
1622 } elseif {$seen_begin==1} {
1623 lappend result $l
1624 }
1625 }
1626
1627 if {$seen_begin == 0} {
1628 error "Autotest $testcase not found"
1629 }
1630
1631 if {$seen_begin == 1 && $seen_end == 0} {
1632 error "Missing end marker for test $testcase"
1633 }
1634
1635 return $result
1636 }
1637
1638 # Sets temporary breakpoint at LOCATION.
1639 proc mi_tbreak {location} {
1640
1641 global mi_gdb_prompt
1642
1643 mi_gdb_test "-break-insert -t $location" \
1644 {\^done,bkpt=.*} \
1645 "run to $location (set breakpoint)"
1646 }
1647
1648 # Send COMMAND that must be a command that resumes
1649 # the inferior (run/continue/next/etc) and consumes
1650 # the "^running" output from it.
1651 proc mi_send_resuming_command_raw {command test} {
1652
1653 global mi_gdb_prompt
1654 global thread_selected_re
1655 global library_loaded_re
1656
1657 send_gdb "$command\n"
1658 gdb_expect {
1659 -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n($library_loaded_re)*($thread_selected_re)?${mi_gdb_prompt}" {
1660 # Note that lack of 'pass' call here -- this works around limitation
1661 # in DejaGNU xfail mechanism. mi-until.exp has this:
1662 #
1663 # setup_kfail gdb/2104 "*-*-*"
1664 # mi_execute_to ...
1665 #
1666 # and mi_execute_to uses mi_send_resuming_command. If we use 'pass' here,
1667 # it will reset kfail, so when the actual test fails, it will be flagged
1668 # as real failure.
1669 return 0
1670 }
1671 -re "\\^error,msg=\"Displaced stepping is only supported in ARM mode\".*" {
1672 unsupported "$test (Thumb mode)"
1673 return -1
1674 }
1675 -re "\\^error,msg=.*" {
1676 fail "$test (MI error)"
1677 return -1
1678 }
1679 -re ".*${mi_gdb_prompt}" {
1680 fail "$test (failed to resume)"
1681 return -1
1682 }
1683 timeout {
1684 fail "$test"
1685 return -1
1686 }
1687 }
1688 }
1689
1690 proc mi_send_resuming_command {command test} {
1691 mi_send_resuming_command_raw -$command $test
1692 }
1693
1694 # Helper to mi_run_inline_test below.
1695 # Sets a temporary breakpoint at LOCATION and runs
1696 # the program using COMMAND. When the program is stopped
1697 # returns the line at which it. Returns -1 if line cannot
1698 # be determined.
1699 # Does not check that the line is the same as requested.
1700 # The caller can check itself if required.
1701 proc mi_continue_to_line {location test} {
1702
1703 mi_tbreak $location
1704 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1705 return [mi_get_stop_line $test]
1706 }
1707
1708 # Wait until gdb prints the current line.
1709 proc mi_get_stop_line {test} {
1710
1711 global mi_gdb_prompt
1712 global async
1713
1714 if {$async} {
1715 set prompt_re ""
1716 } else {
1717 set prompt_re "$mi_gdb_prompt$"
1718 }
1719
1720 gdb_expect {
1721 -re ".*line=\"(\[0-9\]*)\".*\r\n$prompt_re" {
1722 return $expect_out(1,string)
1723 }
1724 -re ".*$mi_gdb_prompt" {
1725 fail "wait for stop ($test)"
1726 }
1727 timeout {
1728 fail "wait for stop ($test)"
1729 }
1730 }
1731 }
1732
1733 # Run a MI test embedded in comments in a C file.
1734 # The C file should contain special comments in the following
1735 # three forms:
1736 #
1737 # /*: BEGIN: testname :*/
1738 # /*: <Tcl statements> :*/
1739 # /*: END: testname :*/
1740 #
1741 # This procedure find the begin and end marker for the requested
1742 # test. Then, a temporary breakpoint is set at the begin
1743 # marker and the program is run (from start).
1744 #
1745 # After that, for each special comment between the begin and end
1746 # marker, the Tcl statements are executed. It is assumed that
1747 # for each comment, the immediately preceding line is executable
1748 # C statement. Then, gdb will be single-stepped until that
1749 # preceding C statement is executed, and after that the
1750 # Tcl statements in the comment will be executed.
1751 #
1752 # For example:
1753 #
1754 # /*: BEGIN: assignment-test :*/
1755 # v = 10;
1756 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1757 # /*: END: assignment-test :*/
1758 #
1759 # The mi_prepare_inline_tests function should be called before
1760 # calling this function. A given C file can contain several
1761 # inline tests. The names of the tests must be unique within one
1762 # C file.
1763 #
1764 proc mi_run_inline_test { testcase } {
1765
1766 global mi_gdb_prompt
1767 global hex
1768 global decimal
1769 global fullname_syntax
1770 global mi_autotest_source
1771
1772 set commands [mi_get_inline_test $testcase]
1773
1774 set first 1
1775 set line_now 1
1776
1777 foreach c $commands {
1778 set statements [lindex $c 0]
1779 set line [lindex $c 1]
1780 set line [expr $line-1]
1781
1782 # We want gdb to be stopped at the expression immediately
1783 # before the comment. If this is the first comment, the
1784 # program is either not started yet or is in some random place,
1785 # so we run it. For further comments, we might be already
1786 # standing at the right line. If not continue till the
1787 # right line.
1788
1789 if {$first==1} {
1790 # Start the program afresh.
1791 mi_tbreak "$mi_autotest_source:$line"
1792 mi_run_cmd
1793 set line_now [mi_get_stop_line "$testcase: step to $line"]
1794 set first 0
1795 } elseif {$line_now!=$line} {
1796 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1797 }
1798
1799 if {$line_now!=$line} {
1800 fail "$testcase: go to line $line"
1801 }
1802
1803 # We're not at the statement right above the comment.
1804 # Execute that statement so that the comment can test
1805 # the state after the statement is executed.
1806
1807 # Single-step past the line.
1808 if { [mi_send_resuming_command "exec-next" "$testcase: step over $line"] != 0 } {
1809 return -1
1810 }
1811 set line_now [mi_get_stop_line "$testcase: step over $line"]
1812
1813 # We probably want to use 'uplevel' so that statements
1814 # have direct access to global variables that the
1815 # main 'exp' file has set up. But it's not yet clear,
1816 # will need more experience to be sure.
1817 eval $statements
1818 }
1819 }
1820
1821 proc get_mi_thread_list {name} {
1822 global expect_out
1823
1824 # MI will return a list of thread ids:
1825 #
1826 # -thread-list-ids
1827 # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
1828 # (gdb)
1829 mi_gdb_test "-thread-list-ids" \
1830 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1831 "-thread_list_ids ($name)"
1832
1833 set output {}
1834 if {[info exists expect_out(buffer)]} {
1835 set output $expect_out(buffer)
1836 }
1837
1838 set thread_list {}
1839 if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
1840 fail "finding threads in MI output ($name)"
1841 } else {
1842 pass "finding threads in MI output ($name)"
1843
1844 # Make list of console threads
1845 set start [expr {[string first \{ $threads] + 1}]
1846 set end [expr {[string first \} $threads] - 1}]
1847 set threads [string range $threads $start $end]
1848 foreach thread [split $threads ,] {
1849 if {[scan $thread {thread-id="%d"} num]} {
1850 lappend thread_list $num
1851 }
1852 }
1853 }
1854
1855 return $thread_list
1856 }
1857
1858 # Check that MI and the console know of the same threads.
1859 # Appends NAME to all test names.
1860 proc check_mi_and_console_threads {name} {
1861 global expect_out
1862
1863 mi_gdb_test "-thread-list-ids" \
1864 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
1865 "-thread-list-ids ($name)"
1866 set mi_output {}
1867 if {[info exists expect_out(buffer)]} {
1868 set mi_output $expect_out(buffer)
1869 }
1870
1871 # GDB will return a list of thread ids and some more info:
1872 #
1873 # (gdb)
1874 # -interpreter-exec console "info threads"
1875 # ~" 4 Thread 2051 (LWP 7734) 0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
1876 # ~" 3 Thread 1026 (LWP 7733) () at __libc_nanosleep:-1"
1877 # ~" 2 Thread 2049 (LWP 7732) 0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
1878 # ~"* 1 Thread 1024 (LWP 7731) main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
1879 # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
1880 mi_gdb_test "info threads" \
1881 {.*(~".*"[\r\n]*)+.*} \
1882 "info threads ($name)"
1883 set console_output {}
1884 if {[info exists expect_out(buffer)]} {
1885 set console_output $expect_out(buffer)
1886 }
1887
1888 # Make a list of all known threads to console (gdb's thread IDs)
1889 set console_thread_list {}
1890 foreach line [split $console_output \n] {
1891 if {[string index $line 0] == "~"} {
1892 # This is a line from the console; trim off "~", " ", "*", and "\""
1893 set line [string trim $line ~\ \"\*]
1894 if {[scan $line "%d" id] == 1} {
1895 lappend console_thread_list $id
1896 }
1897 }
1898 }
1899
1900 # Now find the result string from MI
1901 set mi_result ""
1902 foreach line [split $mi_output \n] {
1903 if {[string range $line 0 4] == "^done"} {
1904 set mi_result $line
1905 }
1906 }
1907 if {$mi_result == ""} {
1908 fail "finding MI result string ($name)"
1909 } else {
1910 pass "finding MI result string ($name)"
1911 }
1912
1913 # Finally, extract the thread ids and compare them to the console
1914 set num_mi_threads_str ""
1915 if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
1916 fail "finding number of threads in MI output ($name)"
1917 } else {
1918 pass "finding number of threads in MI output ($name)"
1919
1920 # Extract the number of threads from the MI result
1921 if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
1922 fail "got number of threads from MI ($name)"
1923 } else {
1924 pass "got number of threads from MI ($name)"
1925
1926 # Check if MI and console have same number of threads
1927 if {$num_mi_threads != [llength $console_thread_list]} {
1928 fail "console and MI have same number of threads ($name)"
1929 } else {
1930 pass "console and MI have same number of threads ($name)"
1931
1932 # Get MI thread list
1933 set mi_thread_list [get_mi_thread_list $name]
1934
1935 # Check if MI and console have the same threads
1936 set fails 0
1937 foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
1938 if {$ct != $mt} {
1939 incr fails
1940 }
1941 }
1942 if {$fails > 0} {
1943 fail "MI and console have same threads ($name)"
1944
1945 # Send a list of failures to the log
1946 send_log "Console has thread ids: $console_thread_list\n"
1947 send_log "MI has thread ids: $mi_thread_list\n"
1948 } else {
1949 pass "MI and console have same threads ($name)"
1950 }
1951 }
1952 }
1953 }
1954 }
1955
1956 # Download shared libraries to the target.
1957 proc mi_load_shlibs { args } {
1958 foreach file $args {
1959 gdb_remote_download target [shlib_target_file $file]
1960 }
1961
1962 if {[is_remote target]} {
1963 # If the target is remote, we need to tell gdb where to find the
1964 # libraries.
1965 #
1966 # We could set this even when not testing remotely, but a user
1967 # generally won't set it unless necessary. In order to make the tests
1968 # more like the real-life scenarios, we don't set it for local testing.
1969 mi_gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "\^done" ""
1970 }
1971 }
1972
1973 proc mi_check_thread_states { states test } {
1974 global expect_out
1975 set pattern ".*\\^done,threads=\\\["
1976 foreach s $states {
1977 set pattern "${pattern}(.*)state=\"$s\""
1978 }
1979 set pattern "${pattern}(,core=\"\[0-9\]*\")?\\\}\\\].*"
1980
1981 verbose -log "expecting: $pattern"
1982 mi_gdb_test "-thread-info" $pattern $test
1983 }
1984
1985 # Return a list of MI features supported by this gdb.
1986 proc mi_get_features {} {
1987 global expect_out mi_gdb_prompt
1988
1989 send_gdb "-list-features\n"
1990
1991 gdb_expect {
1992 -re "\\^done,features=\\\[(.*)\\\]\r\n$mi_gdb_prompt$" {
1993 regsub -all -- \" $expect_out(1,string) "" features
1994 return [split $features ,]
1995 }
1996 -re ".*\r\n$mi_gdb_prompt$" {
1997 verbose -log "got $expect_out(buffer)"
1998 return ""
1999 }
2000 timeout {
2001 verbose -log "timeout in mi_gdb_prompt"
2002 return ""
2003 }
2004 }
2005 }
2006
2007 # Variable Object Trees
2008 #
2009 # Yet another way to check varobjs. Pass mi_walk_varobj_tree a "list" of
2010 # variables (not unlike the actual source code definition), and it will
2011 # automagically test the children for you (by default).
2012 #
2013 # Example:
2014 #
2015 # source code:
2016 # struct bar {
2017 # union {
2018 # int integer;
2019 # void *ptr;
2020 # };
2021 # const int *iPtr;
2022 # };
2023 #
2024 # class foo {
2025 # public:
2026 # int a;
2027 # struct {
2028 # int b;
2029 # struct bar *c;
2030 # };
2031 # };
2032 #
2033 # foo *f = new foo (); <-- break here
2034 #
2035 # We want to check all the children of "f".
2036 #
2037 # Translate the above structures into the following tree:
2038 #
2039 # set tree {
2040 # foo f {
2041 # {} public {
2042 # int a {}
2043 # anonymous struct {
2044 # {} public {
2045 # int b {}
2046 # {bar *} c {
2047 # {} public {
2048 # anonymous union {
2049 # {} public {
2050 # int integer {}
2051 # {void *} ptr {}
2052 # }
2053 # }
2054 # {const int *} iPtr {
2055 # {const int} {*iPtr} {}
2056 # }
2057 # }
2058 # }
2059 # }
2060 # }
2061 # }
2062 # }
2063 # }
2064 #
2065 # mi_walk_varobj_tree c++ $tree
2066 #
2067 # If you'd prefer to walk the tree using your own callback,
2068 # simply pass the name of the callback to mi_walk_varobj_tree.
2069 #
2070 # This callback should take one argument, the name of the variable
2071 # to process. This name is the name of a global array holding the
2072 # variable's properties (object name, type, etc).
2073 #
2074 # An example callback:
2075 #
2076 # proc my_callback {var} {
2077 # upvar #0 $var varobj
2078 #
2079 # puts "my_callback: called on varobj $varobj(obj_name)"
2080 # }
2081 #
2082 # The arrays created for each variable object contain the following
2083 # members:
2084 #
2085 # obj_name - the object name for accessing this variable via MI
2086 # display_name - the display name for this variable (exp="display_name" in
2087 # the output of -var-list-children)
2088 # type - the type of this variable (type="type" in the output
2089 # of -var-list-children, or the special tag "anonymous"
2090 # path_expr - the "-var-info-path-expression" for this variable
2091 # NOTE: This member cannot be used reliably with typedefs.
2092 # Use with caution!
2093 # See notes inside get_path_expr for more.
2094 # parent - the variable name of the parent varobj
2095 # children - a list of children variable names (which are the
2096 # names Tcl arrays, not object names)
2097 #
2098 # For each variable object, an array containing the above fields will
2099 # be created under the root node (conveniently called, "root"). For example,
2100 # a variable object with handle "OBJ.public.0_anonymous.a" will have
2101 # a corresponding global Tcl variable named "root.OBJ.public.0_anonymous.a".
2102 #
2103 # Note that right now, this mechanism cannot be used for recursive data
2104 # structures like linked lists.
2105
2106 namespace eval ::varobj_tree {
2107 # An index which is appended to root varobjs to ensure uniqueness.
2108 variable _root_idx 0
2109
2110 # A procedure to help with debuggging varobj trees.
2111 # VARIABLE_NAME is the name of the variable to dump.
2112 # CMD, if present, is the name of the callback to output the contstructed
2113 # strings. By default, it uses expect's "send_log" command.
2114 # TERM, if present, is a terminating character. By default it is the newline.
2115 #
2116 # To output to the terminal (not the expect log), use
2117 # mi_varobj_tree_dump_variable my_variable puts ""
2118
2119 proc mi_varobj_tree_dump_variable {variable_name {cmd send_log} {term "\n"}} {
2120 upvar #0 $variable_name varobj
2121
2122 eval "$cmd \"VAR = $variable_name$term\""
2123
2124 # Explicitly encode the array indices, since outputting them
2125 # in some logical order is better than what "array names" might
2126 # return.
2127 foreach idx {obj_name parent display_name type path_expr} {
2128 eval "$cmd \"\t$idx = $varobj($idx)$term\""
2129 }
2130
2131 # Output children
2132 set num [llength $varobj(children)]
2133 eval "$cmd \"\tnum_children = $num$term\""
2134 if {$num > 0} {
2135 eval "$cmd \"\tchildren = $varobj(children)$term\""
2136 }
2137 }
2138
2139 # The default callback used by mi_walk_varobj_tree. This callback
2140 # simply checks all of VAR's children. It specifically does not test
2141 # path expressions, since that is very problematic.
2142 #
2143 # This procedure may be used in custom callbacks.
2144 proc test_children_callback {variable_name} {
2145 upvar #0 $variable_name varobj
2146
2147 if {[llength $varobj(children)] > 0} {
2148 # Construct the list of children the way mi_list_varobj_children
2149 # expects to get it:
2150 # { {obj_name display_name num_children type} ... }
2151 set children_list {}
2152 foreach child $varobj(children) {
2153 upvar #0 $child c
2154 set clist [list [string_to_regexp $c(obj_name)] \
2155 [string_to_regexp $c(display_name)] \
2156 [llength $c(children)]]
2157 if {[string length $c(type)] > 0} {
2158 lappend clist [string_to_regexp $c(type)]
2159 }
2160 lappend children_list $clist
2161 }
2162
2163 mi_list_varobj_children $varobj(obj_name) $children_list \
2164 "VT: list children of $varobj(obj_name)"
2165 }
2166 }
2167
2168 # Set the properties of the varobj represented by
2169 # PARENT_VARIABLE - the name of the parent's variable
2170 # OBJNAME - the MI object name of this variable
2171 # DISP_NAME - the display name of this variable
2172 # TYPE - the type of this variable
2173 # PATH - the path expression for this variable
2174 # CHILDREN - a list of the variable's children
2175 proc create_varobj {parent_variable objname disp_name \
2176 type path children} {
2177 upvar #0 $parent_variable parent
2178
2179 set var_name "root.$objname"
2180 global $var_name
2181 array set $var_name [list obj_name $objname]
2182 array set $var_name [list display_name $disp_name]
2183 array set $var_name [list type $type]
2184 array set $var_name [list path_expr $path]
2185 array set $var_name [list parent "$parent_variable"]
2186 array set $var_name [list children \
2187 [get_tree_children $var_name $children]]
2188 return $var_name
2189 }
2190
2191 # Should VARIABLE be used in path expressions? The CPLUS_FAKE_CHILD
2192 # varobjs and anonymous structs/unions are not used for path expressions.
2193 proc is_path_expr_parent {variable} {
2194 upvar #0 $variable varobj
2195
2196 # If the varobj's type is "", it is a CPLUS_FAKE_CHILD.
2197 # If the tail of the varobj's object name is "%d_anonymous",
2198 # then it represents an anonymous struct or union.
2199 if {[string length $varobj(type)] == 0 \
2200 || [regexp {[0-9]+_anonymous$} $varobj(obj_name)]} {
2201 return false
2202 }
2203
2204 return true
2205 }
2206
2207 # Return the path expression for the variable named NAME in
2208 # parent varobj whose variable name is given by PARENT_VARIABLE.
2209 proc get_path_expr {parent_variable name type} {
2210 upvar #0 $parent_variable parent
2211 upvar #0 $parent_variable path_parent
2212
2213 # If TYPE is "", this is one of the CPLUS_FAKE_CHILD varobjs,
2214 # which has no path expression. Likewsise for anonymous structs
2215 # and unions.
2216 if {[string length $type] == 0 \
2217 || [string compare $type "anonymous"] == 0} {
2218 return ""
2219 }
2220
2221 # Find the path parent variable.
2222 while {![is_path_expr_parent $parent_variable]} {
2223 set parent_variable $path_parent(parent)
2224 upvar #0 $parent_variable path_parent
2225 }
2226
2227 # This is where things get difficult. We do not actually know
2228 # the real type for variables defined via typedefs, so we don't actually
2229 # know whether the parent is a structure/union or not.
2230 #
2231 # So we assume everything that isn't a simple type is a compound type.
2232 set stars ""
2233 regexp {\*+} $parent(type) stars
2234 set is_compound 1
2235 if {[string index $name 0] == "*"} {
2236 set is_compound 0
2237 }
2238
2239 if {[string index $parent(type) end] == "\]"} {
2240 # Parent is an array.
2241 return "($path_parent(path_expr))\[$name\]"
2242 } elseif {$is_compound} {
2243 # Parent is a structure or union or a pointer to one.
2244 if {[string length $stars]} {
2245 set join "->"
2246 } else {
2247 set join "."
2248 }
2249
2250 global root
2251
2252 # To make matters even more hideous, varobj.c has slightly different
2253 # path expressions for C and C++.
2254 set path_expr "($path_parent(path_expr))$join$name"
2255 if {[string compare -nocase $root(language) "c"] == 0} {
2256 return $path_expr
2257 } else {
2258 return "($path_expr)"
2259 }
2260 } else {
2261 # Parent is a pointer.
2262 return "*($path_parent(path_expr))"
2263 }
2264 }
2265
2266 # Process the CHILDREN (a list of varobj_tree elements) of the variable
2267 # given by PARENT_VARIABLE. Returns a list of children variables.
2268 proc get_tree_children {parent_variable children} {
2269 upvar #0 $parent_variable parent
2270
2271 set field_idx 0
2272 set children_list {}
2273 foreach {type name children} $children {
2274 if {[string compare $parent_variable "root"] == 0} {
2275 # Root variable
2276 variable _root_idx
2277 incr _root_idx
2278 set objname "$name$_root_idx"
2279 set disp_name "$name"
2280 set path_expr "$name"
2281 } elseif {[string compare $type "anonymous"] == 0} {
2282 # Special case: anonymous types. In this case, NAME will either be
2283 # "struct" or "union".
2284 set objname "$parent(obj_name).${field_idx}_anonymous"
2285 set disp_name "<anonymous $name>"
2286 set path_expr ""
2287 set type "$name {...}"
2288 } else {
2289 set objname "$parent(obj_name).$name"
2290 set disp_name $name
2291 set path_expr [get_path_expr $parent_variable $name $type]
2292 }
2293
2294 lappend children_list [create_varobj $parent_variable $objname \
2295 $disp_name $type $path_expr $children]
2296 incr field_idx
2297 }
2298
2299 return $children_list
2300 }
2301
2302 # The main procedure to call the given CALLBACK on the elements of the
2303 # given varobj TREE. See detailed explanation above.
2304 proc walk_tree {language tree callback} {
2305 global root
2306 variable _root_idx
2307
2308 if {[llength $tree] < 3} {
2309 error "tree does not contain enough elements"
2310 }
2311
2312 set _root_idx 0
2313
2314 # Create root node and process the tree.
2315 array set root [list language $language]
2316 array set root [list obj_name "root"]
2317 array set root [list display_name "root"]
2318 array set root [list type "root"]
2319 array set root [list path_expr "root"]
2320 array set root [list parent "root"]
2321 array set root [list children [get_tree_children root $tree]]
2322
2323 # Walk the tree
2324 set all_nodes $root(children); # a stack of nodes
2325 while {[llength $all_nodes] > 0} {
2326 # "Pop" the name of the global variable containing this varobj's
2327 # information from the stack of nodes.
2328 set var_name [lindex $all_nodes 0]
2329 set all_nodes [lreplace $all_nodes 0 0]
2330
2331 # Bring the global named in VAR_NAME into scope as the local variable
2332 # VAROBJ.
2333 upvar #0 $var_name varobj
2334
2335 # Append any children of VAROBJ to the list of nodes to walk.
2336 if {[llength $varobj(children)] > 0} {
2337 set all_nodes [concat $all_nodes $varobj(children)]
2338 }
2339
2340 # If this is a root variable, create the variable object for it.
2341 if {[string compare $varobj(parent) "root"] == 0} {
2342 mi_create_varobj $varobj(obj_name) $varobj(display_name) \
2343 "VT: create root varobj for $varobj(display_name)"
2344 }
2345
2346 # Now call the callback for VAROBJ.
2347 uplevel #0 $callback $var_name
2348 }
2349 }
2350 }
2351
2352 # The default varobj tree callback, which simply tests -var-list-children.
2353 proc mi_varobj_tree_test_children_callback {variable} {
2354 ::varobj_tree::test_children_callback $variable
2355 }
2356
2357 # Walk the variable object tree given by TREE, calling the specified
2358 # CALLBACK. By default this uses mi_varobj_tree_test_children_callback.
2359 proc mi_walk_varobj_tree {language tree \
2360 {callback \
2361 mi_varobj_tree_test_children_callback}} {
2362 ::varobj_tree::walk_tree $language $tree $callback
2363 }
2364
2365 # Build a list of key-value pairs given by the list ATTR_LIST. Flatten
2366 # this list using the optional JOINER, a comma by default.
2367 #
2368 # The list must contain an even number of elements, which are the key-value
2369 # pairs. Each value will be surrounded by quotes, according to the grammar,
2370 # except if the value starts with \[ or \{, when the quotes will be omitted.
2371 #
2372 # Example: mi_build_kv_pairs {a b c d e f g \[.*\]}
2373 # returns a=\"b\",c=\"d\",e=\"f\",g=\[.*\]
2374 proc mi_build_kv_pairs {attr_list {joiner ,}} {
2375 set l {}
2376 foreach {var value} $attr_list {
2377 if {[string range $value 0 1] == "\\\["
2378 || [string range $value 0 1] == "\\\{"} {
2379 lappend l "$var=$value"
2380 } else {
2381 lappend l "$var=\"$value\""
2382 }
2383 }
2384 return "[join $l $joiner]"
2385 }
2386
2387 # Construct a breakpoint regexp. This may be used to test the output of
2388 # -break-insert, -dprintf-insert, or -break-info.
2389 #
2390 # All arguments for the breakpoint may be specified using the options
2391 # number, type, disp, enabled, addr, func, file, fullanme, line,
2392 # thread-groups, cond, evaluated-by, times, ignore, script,
2393 # and original-location.
2394 #
2395 # Only if -script and -ignore are given will they appear in the output.
2396 # Otherwise, this procedure will skip them using ".*".
2397 #
2398 # Example: mi_make_breakpoint -number 2 -file ".*/myfile.c" -line 3
2399 # will return the breakpoint:
2400 # bkpt={number="2",type=".*",disp=".*",enabled=".*",addr=".*",func=".*",
2401 # file=".*/myfile.c",fullname=".*",line="3",thread-groups=\[.*\],
2402 # times="0".*original-location=".*"}
2403
2404 proc mi_make_breakpoint {args} {
2405 parse_args {{number .*} {type .*} {disp .*} {enabled .*} {addr .*}
2406 {func .*} {file .*} {fullname .*} {line .*}
2407 {thread-groups \\\[.*\\\]} {times .*} {ignore 0}
2408 {script ""} {original-location .*} {cond ""} {evaluated-by ""}}
2409
2410 set attr_list {}
2411 foreach attr [list number type disp enabled addr func file \
2412 fullname line thread-groups] {
2413 lappend attr_list $attr [set $attr]
2414 }
2415
2416 set result "bkpt={[mi_build_kv_pairs $attr_list]"
2417
2418 # There are always exceptions.
2419
2420 # If COND is not preset, do not output it.
2421 if {[string length $cond] > 0} {
2422 append result ","
2423 append result [mi_build_kv_pairs [list "cond" $cond]]
2424
2425 # When running on a remote, GDB may output who is evaluating
2426 # breakpoint conditions.
2427 if {[string length ${evaluated-by}] > 0} {
2428 append result [mi_build_kv_pairs \
2429 [list "evaluated-by" ${evaluated-by}]]
2430 } else {
2431 append result {(,evaluated-by=".*")?}
2432 }
2433 }
2434
2435 append result ","
2436 append result [mi_build_kv_pairs [list "times" $times]]
2437
2438 # If SCRIPT and IGNORE are not present, do not output them.
2439 if {$ignore != 0} {
2440 append result ","
2441 append result [mi_build_kv_pairs [list "ignore" $ignore]]
2442 append result ","
2443 }
2444 if {[string length $script] > 0} {
2445 append result ","
2446 append result [mi_build_kv_pairs [list "script" $script]]
2447 append result ","
2448 } else {
2449 # Allow anything up until the next "official"/required attribute.
2450 # This pattern skips over script/ignore if matches on those
2451 # were not specifically required by the caller.
2452 append result ".*"
2453 }
2454 append result [mi_build_kv_pairs \
2455 [list "original-location" ${original-location}]]
2456 append result "}"
2457 return $result
2458 }
2459
2460 # Build a breakpoint table regexp given the list of breakpoints in `bp_list',
2461 # constructed by mi_make_breakpoint.
2462 #
2463 # Example: Construct a breakpoint table where the only attributes we
2464 # test for are the existence of three breakpoints numbered 1, 2, and 3.
2465 #
2466 # set bps {}
2467 # lappend bps [mi_make_breakpoint -number 1]
2468 # lappend bps [mi_make_breakpoint -number 2]
2469 # lappned bps [mi_make_breakpoint -number 3]
2470 # mi_make_breakpoint_table $bps
2471 # will return (abbreviated for clarity):
2472 # BreakpointTable={nr_rows="3",nr_cols="6",hdr=[{width=".*",...} ...],
2473 # body=[bkpt={number="1",...},bkpt={number="2",...},bkpt={number="3",...}]}
2474
2475 proc mi_make_breakpoint_table {bp_list} {
2476 # Build header -- assume a standard header for all breakpoint tables.
2477 set hl {}
2478 foreach {nm hdr} [list number Num type Type disp Disp enabled Enb \
2479 addr Address what What] {
2480 # The elements here are the MI table headers, which have the
2481 # format:
2482 # {width="7",alignment="-1",col_name="number",colhdr="Num"}
2483 lappend hl "{[mi_build_kv_pairs [list width .* alignment .* \
2484 col_name $nm colhdr $hdr]]}"
2485 }
2486 set header "hdr=\\\[[join $hl ,]\\\]"
2487
2488 # The caller has implicitly supplied the number of columns and rows.
2489 set nc [llength $hl]
2490 set nr [llength $bp_list]
2491
2492 # Build body -- mi_make_breakpoint has done most of the work.
2493 set body "body=\\\[[join $bp_list ,]\\\]"
2494
2495 # Assemble the final regexp.
2496 return "BreakpointTable={nr_rows=\"$nr\",nr_cols=\"$nc\",$header,$body}"
2497 }
2498
2499 # Return a 1 for configurations that do not support Python scripting.
2500 # Note: This also sets various globals that specify which version of Python
2501 # is in use. See skip_python_tests_prompt.
2502
2503 proc mi_skip_python_tests {} {
2504 global mi_gdb_prompt
2505 return [skip_python_tests_prompt "$mi_gdb_prompt$"]
2506 }
2507
2508 # Check whether we're testing with the remote or extended-remote
2509 # targets.
2510
2511 proc mi_is_target_remote {} {
2512 global mi_gdb_prompt
2513
2514 return [gdb_is_target_remote_prompt "$mi_gdb_prompt"]
2515 }