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