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