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