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