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