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