Commit | Line | Data |
---|---|---|
9b254dd1 | 1 | # Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008 |
6aba47ca | 2 | # Free Software Foundation, Inc. |
fb40c209 AC |
3 | |
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by | |
e22f8b7c | 6 | # the Free Software Foundation; either version 3 of the License, or |
fb40c209 | 7 | # (at your option) any later version. |
e22f8b7c | 8 | # |
fb40c209 AC |
9 | # This program is distributed in the hope that it will be useful, |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
e22f8b7c | 13 | # |
fb40c209 | 14 | # You should have received a copy of the GNU General Public License |
e22f8b7c | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
fb40c209 AC |
16 | |
17 | # Please email any bugs, comments, and/or additions to this file to: | |
18 | # bug-gdb@prep.ai.mit.edu | |
19 | ||
20 | # This file was based on a file written by Fred Fish. (fnf@cygnus.com) | |
21 | ||
22 | # Test setup routines that work with the MI interpreter. | |
23 | ||
24 | # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt. | |
25 | # Set it if it is not already set. | |
26 | global mi_gdb_prompt | |
27 | if ![info exists mi_gdb_prompt] then { | |
28 | set mi_gdb_prompt "\[(\]gdb\[)\] \r\n" | |
29 | } | |
30 | ||
ecd3fd0f BR |
31 | global mi_inferior_spawn_id |
32 | global mi_inferior_tty_name | |
33 | ||
fb40c209 AC |
34 | set MIFLAGS "-i=mi" |
35 | ||
36 | # | |
37 | # mi_gdb_exit -- exit the GDB, killing the target program if necessary | |
38 | # | |
39 | proc mi_gdb_exit {} { | |
40 | catch mi_uncatched_gdb_exit | |
41 | } | |
42 | ||
43 | proc mi_uncatched_gdb_exit {} { | |
44 | global GDB | |
45 | global GDBFLAGS | |
46 | global verbose | |
47 | global gdb_spawn_id; | |
48 | global gdb_prompt | |
49 | global mi_gdb_prompt | |
50 | global MIFLAGS | |
51 | ||
52 | gdb_stop_suppressing_tests; | |
53 | ||
54 | if { [info procs sid_exit] != "" } { | |
55 | sid_exit | |
56 | } | |
57 | ||
58 | if ![info exists gdb_spawn_id] { | |
59 | return; | |
60 | } | |
61 | ||
62 | verbose "Quitting $GDB $GDBFLAGS $MIFLAGS" | |
63 | ||
64 | if { [is_remote host] && [board_info host exists fileid] } { | |
65 | send_gdb "999-gdb-exit\n"; | |
66 | gdb_expect 10 { | |
67 | -re "y or n" { | |
68 | send_gdb "y\n"; | |
69 | exp_continue; | |
70 | } | |
71 | -re "Undefined command.*$gdb_prompt $" { | |
72 | send_gdb "quit\n" | |
73 | exp_continue; | |
74 | } | |
75 | -re "DOSEXIT code" { } | |
76 | default { } | |
77 | } | |
78 | } | |
79 | ||
80 | if ![is_remote host] { | |
81 | remote_close host; | |
82 | } | |
83 | unset gdb_spawn_id | |
84 | } | |
85 | ||
86 | # | |
79732189 | 87 | # default_mi_gdb_start [INFERIOR_PTY] -- start gdb running, default procedure |
ecd3fd0f BR |
88 | # |
89 | # INFERIOR_PTY should be set to separate-inferior-tty to have the inferior work | |
90 | # with it's own PTY. If set to same-inferior-tty, the inferior shares GDB's PTY. | |
91 | # The default value is same-inferior-tty. | |
fb40c209 AC |
92 | # |
93 | # When running over NFS, particularly if running many simultaneous | |
94 | # tests on different hosts all using the same server, things can | |
95 | # get really slow. Give gdb at least 3 minutes to start up. | |
96 | # | |
79732189 | 97 | proc default_mi_gdb_start { args } { |
fb40c209 AC |
98 | global verbose |
99 | global GDB | |
100 | global GDBFLAGS | |
101 | global gdb_prompt | |
102 | global mi_gdb_prompt | |
103 | global timeout | |
104 | global gdb_spawn_id; | |
105 | global MIFLAGS | |
106 | ||
107 | gdb_stop_suppressing_tests; | |
ecd3fd0f BR |
108 | set inferior_pty no-tty |
109 | ||
110 | if { [llength $args] == 1} { | |
111 | set inferior_pty [lindex $args 0] | |
112 | } | |
113 | ||
114 | set separate_inferior_pty [string match $inferior_pty separate-inferior-tty] | |
fb40c209 | 115 | |
1759b3c3 AC |
116 | # Start SID. |
117 | if { [info procs sid_start] != "" } { | |
118 | verbose "Spawning SID" | |
119 | sid_start | |
120 | } | |
121 | ||
fb40c209 AC |
122 | verbose "Spawning $GDB -nw $GDBFLAGS $MIFLAGS" |
123 | ||
124 | if [info exists gdb_spawn_id] { | |
125 | return 0; | |
126 | } | |
127 | ||
128 | if ![is_remote host] { | |
129 | if { [which $GDB] == 0 } then { | |
130 | perror "$GDB does not exist." | |
131 | exit 1 | |
132 | } | |
133 | } | |
ecd3fd0f BR |
134 | |
135 | # Create the new PTY for the inferior process. | |
136 | if { $separate_inferior_pty } { | |
137 | spawn -pty | |
138 | global mi_inferior_spawn_id | |
139 | global mi_inferior_tty_name | |
140 | set mi_inferior_spawn_id $spawn_id | |
141 | set mi_inferior_tty_name $spawn_out(slave,name) | |
142 | } | |
143 | ||
fb40c209 AC |
144 | set res [remote_spawn host "$GDB -nw $GDBFLAGS $MIFLAGS [host_info gdb_opts]"]; |
145 | if { $res < 0 || $res == "" } { | |
146 | perror "Spawning $GDB failed." | |
147 | return 1; | |
148 | } | |
149 | gdb_expect { | |
1f312e79 JJ |
150 | -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" { |
151 | # We have a new format mi startup prompt. If we are | |
152 | # running mi1, then this is an error as we should be | |
153 | # using the old-style prompt. | |
154 | if { $MIFLAGS == "-i=mi1" } { | |
155 | perror "(mi startup) Got unexpected new mi prompt." | |
156 | remote_close host; | |
157 | return -1; | |
158 | } | |
159 | verbose "GDB initialized." | |
160 | } | |
161 | -re "\[^~\].*$mi_gdb_prompt$" { | |
162 | # We have an old format mi startup prompt. If we are | |
163 | # not running mi1, then this is an error as we should be | |
164 | # using the new-style prompt. | |
165 | if { $MIFLAGS != "-i=mi1" } { | |
166 | perror "(mi startup) Got unexpected old mi prompt." | |
167 | remote_close host; | |
168 | return -1; | |
169 | } | |
d20bf2e8 | 170 | verbose "GDB initialized." |
fb40c209 AC |
171 | } |
172 | -re ".*$gdb_prompt $" { | |
173 | untested "Skip mi tests (got non-mi prompt)." | |
174 | remote_close host; | |
175 | return -1; | |
176 | } | |
76c520e0 AC |
177 | -re ".*unrecognized option.*for a complete list of options." { |
178 | untested "Skip mi tests (not compiled with mi support)." | |
179 | remote_close host; | |
180 | return -1; | |
181 | } | |
7d76bd60 MK |
182 | -re ".*Interpreter `mi' unrecognized." { |
183 | untested "Skip mi tests (not compiled with mi support)." | |
184 | remote_close host; | |
185 | return -1; | |
186 | } | |
fb40c209 AC |
187 | timeout { |
188 | perror "(timeout) GDB never initialized after 10 seconds." | |
189 | remote_close host; | |
190 | return -1 | |
191 | } | |
192 | } | |
193 | set gdb_spawn_id -1; | |
194 | ||
195 | # FIXME: mi output does not go through pagers, so these can be removed. | |
196 | # force the height to "unlimited", so no pagers get used | |
197 | send_gdb "100-gdb-set height 0\n" | |
198 | gdb_expect 10 { | |
199 | -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" { | |
200 | verbose "Setting height to 0." 2 | |
201 | } | |
202 | timeout { | |
203 | warning "Couldn't set the height to 0" | |
204 | } | |
205 | } | |
206 | # force the width to "unlimited", so no wraparound occurs | |
207 | send_gdb "101-gdb-set width 0\n" | |
208 | gdb_expect 10 { | |
209 | -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" { | |
210 | verbose "Setting width to 0." 2 | |
211 | } | |
212 | timeout { | |
213 | warning "Couldn't set the width to 0." | |
214 | } | |
215 | } | |
ecd3fd0f BR |
216 | # If allowing the inferior to have its own PTY then assign the inferior |
217 | # its own terminal device here. | |
218 | if { $separate_inferior_pty } { | |
219 | send_gdb "102-inferior-tty-set $mi_inferior_tty_name\n" | |
220 | gdb_expect 10 { | |
221 | -re ".*102\\\^done\r\n$mi_gdb_prompt$" { | |
222 | verbose "redirect inferior output to new terminal device." | |
223 | } | |
224 | timeout { | |
225 | warning "Couldn't redirect inferior output." 2 | |
226 | } | |
227 | } | |
228 | } | |
fb40c209 | 229 | |
fb40c209 AC |
230 | return 0; |
231 | } | |
232 | ||
79732189 AR |
233 | # |
234 | # Overridable function. You can override this function in your | |
235 | # baseboard file. | |
236 | # | |
237 | proc mi_gdb_start { args } { | |
238 | return [default_mi_gdb_start $args] | |
239 | } | |
240 | ||
fb40c209 AC |
241 | # Many of the tests depend on setting breakpoints at various places and |
242 | # running until that breakpoint is reached. At times, we want to start | |
243 | # with a clean-slate with respect to breakpoints, so this utility proc | |
244 | # lets us do this without duplicating this code everywhere. | |
245 | # | |
246 | ||
247 | proc mi_delete_breakpoints {} { | |
248 | global mi_gdb_prompt | |
249 | ||
250 | # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg | |
251 | send_gdb "102-break-delete\n" | |
252 | gdb_expect 30 { | |
253 | -re "Delete all breakpoints.*y or n.*$" { | |
254 | send_gdb "y\n"; | |
255 | exp_continue | |
256 | } | |
39fb8e9e | 257 | -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" { |
fb40c209 AC |
258 | # This happens if there were no breakpoints |
259 | } | |
f1c8a949 | 260 | timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return } |
fb40c209 AC |
261 | } |
262 | ||
263 | # The correct output is not "No breakpoints or watchpoints." but an | |
264 | # empty BreakpointTable. Also, a query is not acceptable with mi. | |
265 | send_gdb "103-break-list\n" | |
266 | gdb_expect 30 { | |
267 | -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {} | |
6f3f3097 | 268 | -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 |
269 | -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"} |
270 | -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return } | |
271 | -re "Delete all breakpoints.*or n.*$" { | |
272 | warning "Unexpected prompt for breakpoints deletion"; | |
273 | send_gdb "y\n"; | |
274 | exp_continue | |
275 | } | |
276 | timeout { perror "-break-list (timeout)" ; return } | |
277 | } | |
278 | } | |
279 | ||
280 | proc mi_gdb_reinitialize_dir { subdir } { | |
281 | global mi_gdb_prompt | |
da81390b | 282 | global MIFLAGS |
fb40c209 AC |
283 | |
284 | global suppress_flag | |
285 | if { $suppress_flag } { | |
286 | return | |
287 | } | |
288 | ||
289 | if [is_remote host] { | |
290 | return ""; | |
291 | } | |
292 | ||
da81390b JJ |
293 | if { $MIFLAGS == "-i=mi1" } { |
294 | send_gdb "104-environment-directory\n" | |
295 | gdb_expect 60 { | |
fb40c209 AC |
296 | -re ".*Reinitialize source path to empty.*y or n. " { |
297 | warning "Got confirmation prompt for dir reinitialization." | |
298 | send_gdb "y\n" | |
299 | gdb_expect 60 { | |
300 | -re "$mi_gdb_prompt$" {} | |
301 | timeout {error "Dir reinitialization failed (timeout)"} | |
302 | } | |
303 | } | |
304 | -re "$mi_gdb_prompt$" {} | |
305 | timeout {error "Dir reinitialization failed (timeout)"} | |
da81390b JJ |
306 | } |
307 | } else { | |
308 | send_gdb "104-environment-directory -r\n" | |
309 | gdb_expect 60 { | |
310 | -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {} | |
311 | -re "$mi_gdb_prompt$" {} | |
312 | timeout {error "Dir reinitialization failed (timeout)"} | |
313 | } | |
fb40c209 AC |
314 | } |
315 | ||
316 | send_gdb "105-environment-directory $subdir\n" | |
317 | gdb_expect 60 { | |
318 | -re "Source directories searched.*$mi_gdb_prompt$" { | |
319 | verbose "Dir set to $subdir" | |
320 | } | |
da81390b | 321 | -re "105\\\^done.*\r\n$mi_gdb_prompt$" { |
fb40c209 AC |
322 | # FIXME: We return just the prompt for now. |
323 | verbose "Dir set to $subdir" | |
324 | # perror "Dir \"$subdir\" failed." | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
da6012e5 DJ |
329 | # Send GDB the "target" command. |
330 | # FIXME: Some of these patterns are not appropriate for MI. Based on | |
331 | # config/monitor.exp:gdb_target_command. | |
332 | proc mi_gdb_target_cmd { targetname serialport } { | |
333 | global mi_gdb_prompt | |
334 | ||
ef783a7d | 335 | set serialport_re [string_to_regexp $serialport] |
da6012e5 DJ |
336 | for {set i 1} {$i <= 3} {incr i} { |
337 | send_gdb "47-target-select $targetname $serialport\n" | |
338 | gdb_expect 60 { | |
339 | -re "47\\^connected.*$mi_gdb_prompt$" { | |
340 | verbose "Set target to $targetname"; | |
341 | return 0; | |
342 | } | |
401ea829 JB |
343 | -re "unknown host.*$mi_gdb_prompt" { |
344 | verbose "Couldn't look up $serialport" | |
345 | } | |
da6012e5 DJ |
346 | -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" { |
347 | verbose "Connection failed"; | |
348 | } | |
349 | -re "Remote MIPS debugging.*$mi_gdb_prompt$" { | |
350 | verbose "Set target to $targetname"; | |
351 | return 0; | |
352 | } | |
ef783a7d | 353 | -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" { |
da6012e5 DJ |
354 | verbose "Set target to $targetname"; |
355 | return 0; | |
356 | } | |
357 | -re "Remote target $targetname connected to.*$mi_gdb_prompt$" { | |
358 | verbose "Set target to $targetname"; | |
359 | return 0; | |
360 | } | |
361 | -re "Connected to.*$mi_gdb_prompt$" { | |
362 | verbose "Set target to $targetname"; | |
363 | return 0; | |
364 | } | |
365 | -re "Ending remote.*$mi_gdb_prompt$" { } | |
366 | -re "Connection refused.*$mi_gdb_prompt$" { | |
367 | verbose "Connection refused by remote target. Pausing, and trying again." | |
368 | sleep 5 | |
369 | continue | |
370 | } | |
371 | -re "Timeout reading from remote system.*$mi_gdb_prompt$" { | |
372 | verbose "Got timeout error from gdb."; | |
373 | } | |
374 | timeout { | |
375 | send_gdb "\ 3"; | |
376 | break | |
377 | } | |
378 | } | |
379 | } | |
380 | return 1 | |
381 | } | |
382 | ||
fb40c209 | 383 | # |
da6012e5 | 384 | # load a file into the debugger (file command only). |
fb40c209 AC |
385 | # return a -1 if anything goes wrong. |
386 | # | |
da6012e5 | 387 | proc mi_gdb_file_cmd { arg } { |
fb40c209 AC |
388 | global verbose |
389 | global loadpath | |
390 | global loadfile | |
391 | global GDB | |
392 | global mi_gdb_prompt | |
b741e217 | 393 | global last_loaded_file |
fb40c209 AC |
394 | upvar timeout timeout |
395 | ||
b741e217 | 396 | set last_loaded_file $arg |
b53f9b27 | 397 | |
da6012e5 DJ |
398 | if [is_remote host] { |
399 | set arg [remote_download host $arg]; | |
400 | if { $arg == "" } { | |
401 | error "download failed" | |
402 | return -1; | |
403 | } | |
404 | } | |
fb40c209 | 405 | |
fb40c209 AC |
406 | # FIXME: Several of these patterns are only acceptable for console |
407 | # output. Queries are an error for mi. | |
408 | send_gdb "105-file-exec-and-symbols $arg\n" | |
409 | gdb_expect 120 { | |
410 | -re "Reading symbols from.*done.*$mi_gdb_prompt$" { | |
411 | verbose "\t\tLoaded $arg into the $GDB" | |
da6012e5 | 412 | return 0 |
fb40c209 AC |
413 | } |
414 | -re "has no symbol-table.*$mi_gdb_prompt$" { | |
415 | perror "$arg wasn't compiled with \"-g\"" | |
416 | return -1 | |
417 | } | |
fb40c209 AC |
418 | -re "Load new symbol table from \".*\".*y or n. $" { |
419 | send_gdb "y\n" | |
420 | gdb_expect 120 { | |
421 | -re "Reading symbols from.*done.*$mi_gdb_prompt$" { | |
422 | verbose "\t\tLoaded $arg with new symbol table into $GDB" | |
423 | # All OK | |
424 | } | |
425 | timeout { | |
426 | perror "(timeout) Couldn't load $arg, other program already loaded." | |
427 | return -1 | |
428 | } | |
429 | } | |
430 | } | |
431 | -re "No such file or directory.*$mi_gdb_prompt$" { | |
432 | perror "($arg) No such file or directory\n" | |
433 | return -1 | |
434 | } | |
435 | -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" { | |
da6012e5 DJ |
436 | # We (MI) are just giving the prompt back for now, instead of giving |
437 | # some acknowledgement. | |
438 | return 0 | |
439 | } | |
fb40c209 AC |
440 | timeout { |
441 | perror "couldn't load $arg into $GDB (timed out)." | |
442 | return -1 | |
443 | } | |
da6012e5 | 444 | eof { |
fb40c209 AC |
445 | # This is an attempt to detect a core dump, but seems not to |
446 | # work. Perhaps we need to match .* followed by eof, in which | |
447 | # gdb_expect does not seem to have a way to do that. | |
448 | perror "couldn't load $arg into $GDB (end of file)." | |
449 | return -1 | |
450 | } | |
451 | } | |
da6012e5 DJ |
452 | } |
453 | ||
454 | # | |
b741e217 | 455 | # connect to the target and download a file, if necessary. |
da6012e5 DJ |
456 | # return a -1 if anything goes wrong. |
457 | # | |
b741e217 | 458 | proc mi_gdb_target_load { } { |
da6012e5 DJ |
459 | global verbose |
460 | global loadpath | |
461 | global loadfile | |
462 | global GDB | |
463 | global mi_gdb_prompt | |
464 | upvar timeout timeout | |
465 | ||
da6012e5 | 466 | if { [info procs gdbserver_gdb_load] != "" } { |
2226f861 | 467 | mi_gdb_test "kill" ".*" "" |
b741e217 | 468 | set res [gdbserver_gdb_load] |
da6012e5 DJ |
469 | set protocol [lindex $res 0] |
470 | set gdbport [lindex $res 1] | |
471 | ||
472 | if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } { | |
473 | return -1 | |
474 | } | |
475 | } elseif { [info procs send_target_sid] != "" } { | |
fb40c209 | 476 | # For SID, things get complex |
2b97317d KB |
477 | send_gdb "kill\n" |
478 | gdb_expect 10 { | |
479 | -re ".*$mi_gdb_prompt$" | |
480 | } | |
fb40c209 AC |
481 | send_target_sid |
482 | gdb_expect 60 { | |
2f168eed | 483 | -re "\\^done.*$mi_gdb_prompt$" { |
fb40c209 AC |
484 | } |
485 | timeout { | |
486 | perror "Unable to connect to SID target" | |
487 | return -1 | |
488 | } | |
489 | } | |
490 | send_gdb "48-target-download\n" | |
491 | gdb_expect 10 { | |
492 | -re "48\\^done.*$mi_gdb_prompt$" { | |
493 | } | |
494 | timeout { | |
495 | perror "Unable to download to SID target" | |
496 | return -1 | |
497 | } | |
498 | } | |
499 | } elseif { [target_info protocol] == "sim" } { | |
500 | # For the simulator, just connect to it directly. | |
501 | send_gdb "47-target-select sim\n" | |
502 | gdb_expect 10 { | |
503 | -re "47\\^connected.*$mi_gdb_prompt$" { | |
504 | } | |
505 | timeout { | |
506 | perror "Unable to select sim target" | |
507 | return -1 | |
508 | } | |
509 | } | |
510 | send_gdb "48-target-download\n" | |
511 | gdb_expect 10 { | |
512 | -re "48\\^done.*$mi_gdb_prompt$" { | |
513 | } | |
514 | timeout { | |
515 | perror "Unable to download to sim target" | |
516 | return -1 | |
517 | } | |
518 | } | |
b53f9b27 MS |
519 | } elseif { [target_info gdb_protocol] == "remote" } { |
520 | # remote targets | |
8e3049aa PB |
521 | if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } { |
522 | perror "Unable to connect to remote target" | |
523 | return -1 | |
b53f9b27 MS |
524 | } |
525 | send_gdb "48-target-download\n" | |
526 | gdb_expect 10 { | |
527 | -re "48\\^done.*$mi_gdb_prompt$" { | |
528 | } | |
529 | timeout { | |
530 | perror "Unable to download to remote target" | |
531 | return -1 | |
532 | } | |
533 | } | |
fb40c209 AC |
534 | } |
535 | return 0 | |
536 | } | |
537 | ||
b741e217 DJ |
538 | # |
539 | # load a file into the debugger. | |
540 | # return a -1 if anything goes wrong. | |
541 | # | |
542 | proc mi_gdb_load { arg } { | |
543 | if { $arg != "" } { | |
544 | return [mi_gdb_file_cmd $arg] | |
545 | } | |
546 | return 0 | |
547 | } | |
548 | ||
ecd3fd0f BR |
549 | # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb; |
550 | # test the result. | |
fb40c209 AC |
551 | # |
552 | # COMMAND is the command to execute, send to GDB with send_gdb. If | |
553 | # this is the null string no command is sent. | |
554 | # PATTERN is the pattern to match for a PASS, and must NOT include | |
555 | # the \r\n sequence immediately before the gdb prompt. | |
f1ea48cb BR |
556 | # MESSAGE is the message to be printed. (If this is the empty string, |
557 | # then sometimes we don't call pass or fail at all; I don't | |
558 | # understand this at all.) | |
ecd3fd0f BR |
559 | # IPATTERN is the pattern to match for the inferior's output. This parameter |
560 | # is optional. If present, it will produce a PASS if the match is | |
561 | # successful, and a FAIL if unsuccessful. | |
fb40c209 AC |
562 | # |
563 | # Returns: | |
564 | # 1 if the test failed, | |
565 | # 0 if the test passes, | |
566 | # -1 if there was an internal error. | |
567 | # | |
568 | proc mi_gdb_test { args } { | |
569 | global verbose | |
570 | global mi_gdb_prompt | |
07c98896 | 571 | global GDB expect_out |
fb40c209 AC |
572 | upvar timeout timeout |
573 | ||
fb40c209 AC |
574 | set command [lindex $args 0] |
575 | set pattern [lindex $args 1] | |
f1ea48cb | 576 | set message [lindex $args 2] |
fb40c209 | 577 | |
ecd3fd0f BR |
578 | if [llength $args]==4 { |
579 | set ipattern [lindex $args 3] | |
580 | } | |
581 | ||
fb40c209 AC |
582 | if [llength $args]==5 { |
583 | set question_string [lindex $args 3]; | |
584 | set response_string [lindex $args 4]; | |
585 | } else { | |
586 | set question_string "^FOOBAR$" | |
587 | } | |
588 | ||
589 | if $verbose>2 then { | |
590 | send_user "Sending \"$command\" to gdb\n" | |
591 | send_user "Looking to match \"$pattern\"\n" | |
592 | send_user "Message is \"$message\"\n" | |
593 | } | |
594 | ||
595 | set result -1 | |
596 | set string "${command}\n"; | |
39fb8e9e BR |
597 | set string_regex [string_to_regexp $command] |
598 | ||
fb40c209 AC |
599 | if { $command != "" } { |
600 | while { "$string" != "" } { | |
601 | set foo [string first "\n" "$string"]; | |
602 | set len [string length "$string"]; | |
603 | if { $foo < [expr $len - 1] } { | |
604 | set str [string range "$string" 0 $foo]; | |
605 | if { [send_gdb "$str"] != "" } { | |
606 | global suppress_flag; | |
607 | ||
608 | if { ! $suppress_flag } { | |
609 | perror "Couldn't send $command to GDB."; | |
610 | } | |
611 | fail "$message"; | |
612 | return $result; | |
613 | } | |
614 | gdb_expect 2 { | |
615 | -re "\[\r\n\]" { } | |
616 | timeout { } | |
617 | } | |
618 | set string [string range "$string" [expr $foo + 1] end]; | |
619 | } else { | |
620 | break; | |
621 | } | |
622 | } | |
623 | if { "$string" != "" } { | |
624 | if { [send_gdb "$string"] != "" } { | |
625 | global suppress_flag; | |
626 | ||
627 | if { ! $suppress_flag } { | |
628 | perror "Couldn't send $command to GDB."; | |
629 | } | |
630 | fail "$message"; | |
631 | return $result; | |
632 | } | |
633 | } | |
634 | } | |
635 | ||
636 | if [info exists timeout] { | |
637 | set tmt $timeout; | |
638 | } else { | |
639 | global timeout; | |
640 | if [info exists timeout] { | |
641 | set tmt $timeout; | |
642 | } else { | |
643 | set tmt 60; | |
644 | } | |
645 | } | |
646 | gdb_expect $tmt { | |
647 | -re "\\*\\*\\* DOSEXIT code.*" { | |
648 | if { $message != "" } { | |
649 | fail "$message"; | |
650 | } | |
651 | gdb_suppress_entire_file "GDB died"; | |
652 | return -1; | |
653 | } | |
654 | -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" { | |
655 | if ![isnative] then { | |
656 | warning "Can`t communicate to remote target." | |
657 | } | |
658 | gdb_exit | |
659 | gdb_start | |
660 | set result -1 | |
7ddebc7e | 661 | } |
39fb8e9e BR |
662 | -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" { |
663 | # At this point, $expect_out(1,string) is the MI input command. | |
664 | # and $expect_out(2,string) is the MI output command. | |
665 | # If $expect_out(1,string) is "", then there was no MI input command here. | |
666 | ||
40e55bef BR |
667 | # NOTE, there is no trailing anchor because with GDB/MI, |
668 | # asynchronous responses can happen at any point, causing more | |
669 | # data to be available. Normally an anchor is used to make | |
670 | # sure the end of the output is matched, however, $mi_gdb_prompt | |
671 | # is just as good of an anchor since mi_gdb_test is meant to | |
672 | # match a single mi output command. If a second GDB/MI output | |
673 | # response is sent, it will be in the buffer for the next | |
674 | # time mi_gdb_test is called. | |
7ddebc7e KS |
675 | if ![string match "" $message] then { |
676 | pass "$message" | |
677 | } | |
678 | set result 0 | |
fb40c209 AC |
679 | } |
680 | -re "(${question_string})$" { | |
681 | send_gdb "$response_string\n"; | |
682 | exp_continue; | |
683 | } | |
684 | -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" { | |
685 | perror "Undefined command \"$command\"." | |
686 | fail "$message" | |
687 | set result 1 | |
688 | } | |
689 | -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" { | |
690 | perror "\"$command\" is not a unique command name." | |
691 | fail "$message" | |
692 | set result 1 | |
fb40c209 AC |
693 | } |
694 | -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" { | |
695 | if ![string match "" $message] then { | |
ed4c619a | 696 | set errmsg "$message (the program exited)" |
fb40c209 | 697 | } else { |
ed4c619a | 698 | set errmsg "$command (the program exited)" |
fb40c209 AC |
699 | } |
700 | fail "$errmsg" | |
701 | return -1 | |
702 | } | |
703 | -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" { | |
704 | if ![string match "" $message] then { | |
ed4c619a | 705 | set errmsg "$message (the program is no longer running)" |
fb40c209 | 706 | } else { |
ed4c619a | 707 | set errmsg "$command (the program is no longer running)" |
fb40c209 AC |
708 | } |
709 | fail "$errmsg" | |
710 | return -1 | |
711 | } | |
712 | -re ".*$mi_gdb_prompt\[ \]*$" { | |
713 | if ![string match "" $message] then { | |
714 | fail "$message" | |
715 | } | |
716 | set result 1 | |
717 | } | |
718 | "<return>" { | |
719 | send_gdb "\n" | |
720 | perror "Window too small." | |
721 | fail "$message" | |
722 | } | |
723 | -re "\\(y or n\\) " { | |
724 | send_gdb "n\n" | |
725 | perror "Got interactive prompt." | |
726 | fail "$message" | |
727 | } | |
728 | eof { | |
729 | perror "Process no longer exists" | |
730 | if { $message != "" } { | |
731 | fail "$message" | |
732 | } | |
733 | return -1 | |
734 | } | |
735 | full_buffer { | |
736 | perror "internal buffer is full." | |
737 | fail "$message" | |
738 | } | |
739 | timeout { | |
740 | if ![string match "" $message] then { | |
741 | fail "$message (timeout)" | |
742 | } | |
743 | set result 1 | |
744 | } | |
745 | } | |
ecd3fd0f BR |
746 | |
747 | # If the GDB output matched, compare the inferior output. | |
748 | if { $result == 0 } { | |
749 | if [ info exists ipattern ] { | |
d084b331 DJ |
750 | if { ![target_info exists gdb,noinferiorio] } { |
751 | global mi_inferior_spawn_id | |
752 | expect { | |
753 | -i $mi_inferior_spawn_id -re "$ipattern" { | |
754 | pass "$message inferior output" | |
755 | } | |
756 | timeout { | |
757 | fail "$message inferior output (timeout)" | |
758 | set result 1 | |
759 | } | |
ecd3fd0f | 760 | } |
d084b331 DJ |
761 | } else { |
762 | unsupported "$message inferior output" | |
ecd3fd0f BR |
763 | } |
764 | } | |
765 | } | |
766 | ||
fb40c209 AC |
767 | return $result |
768 | } | |
769 | ||
770 | # | |
771 | # MI run command. (A modified version of gdb_run_cmd) | |
772 | # | |
773 | ||
774 | # In patterns, the newline sequence ``\r\n'' is matched explicitly as | |
775 | # ``.*$'' could swallow up output that we attempt to match elsewhere. | |
776 | ||
777 | proc mi_run_cmd {args} { | |
778 | global suppress_flag | |
779 | if { $suppress_flag } { | |
780 | return -1 | |
781 | } | |
782 | global mi_gdb_prompt | |
783 | ||
784 | if [target_info exists gdb_init_command] { | |
785 | send_gdb "[target_info gdb_init_command]\n"; | |
786 | gdb_expect 30 { | |
787 | -re "$mi_gdb_prompt$" { } | |
788 | default { | |
789 | perror "gdb_init_command for target failed"; | |
790 | return; | |
791 | } | |
792 | } | |
793 | } | |
794 | ||
b741e217 DJ |
795 | if { [mi_gdb_target_load] < 0 } { |
796 | return | |
797 | } | |
798 | ||
fb40c209 AC |
799 | if [target_info exists use_gdb_stub] { |
800 | if [target_info exists gdb,do_reload_on_run] { | |
bb378428 | 801 | send_gdb "220-exec-continue\n"; |
fb40c209 | 802 | gdb_expect 60 { |
bb378428 | 803 | -re "220\\^running\[\r\n\]+$mi_gdb_prompt$" {} |
fb40c209 AC |
804 | default {} |
805 | } | |
806 | return; | |
807 | } | |
6a90e1d0 AC |
808 | |
809 | if [target_info exists gdb,start_symbol] { | |
810 | set start [target_info gdb,start_symbol]; | |
811 | } else { | |
812 | set start "start"; | |
813 | } | |
814 | ||
815 | # HACK: Should either use 000-jump or fix the target code | |
816 | # to better handle RUN. | |
817 | send_gdb "jump *$start\n" | |
818 | warning "Using CLI jump command, expect run-to-main FAIL" | |
819 | return | |
fb40c209 AC |
820 | } |
821 | ||
bb378428 | 822 | send_gdb "220-exec-run $args\n" |
fb40c209 | 823 | gdb_expect { |
96dff6a2 | 824 | -re "220\\^running\r\n(=thread-created,id=\"1\"\r\n)?${mi_gdb_prompt}" { |
fb40c209 AC |
825 | } |
826 | timeout { | |
827 | perror "Unable to start target" | |
828 | return | |
829 | } | |
830 | } | |
2d0720d9 | 831 | # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)'' |
fb40c209 AC |
832 | } |
833 | ||
834 | # | |
835 | # Just like run-to-main but works with the MI interface | |
836 | # | |
837 | ||
838 | proc mi_run_to_main { } { | |
839 | global suppress_flag | |
840 | if { $suppress_flag } { | |
841 | return -1 | |
842 | } | |
843 | ||
fb40c209 AC |
844 | global srcdir |
845 | global subdir | |
846 | global binfile | |
847 | global srcfile | |
848 | ||
fb40c209 AC |
849 | mi_delete_breakpoints |
850 | mi_gdb_reinitialize_dir $srcdir/$subdir | |
851 | mi_gdb_load ${binfile} | |
852 | ||
08b468e0 KS |
853 | mi_runto main |
854 | } | |
fb40c209 | 855 | |
08b468e0 KS |
856 | |
857 | # Just like gdb's "runto" proc, it will run the target to a given | |
858 | # function. The big difference here between mi_runto and mi_execute_to | |
859 | # is that mi_execute_to must have the inferior running already. This | |
860 | # proc will (like gdb's runto) (re)start the inferior, too. | |
861 | # | |
862 | # FUNC is the linespec of the place to stop (it inserts a breakpoint here). | |
863 | # It returns: | |
864 | # -1 if test suppressed, failed, timedout | |
865 | # 0 if test passed | |
866 | ||
f7e97bb3 | 867 | proc mi_runto_helper {func run_or_continue} { |
08b468e0 KS |
868 | global suppress_flag |
869 | if { $suppress_flag } { | |
870 | return -1 | |
871 | } | |
872 | ||
873 | global mi_gdb_prompt expect_out | |
76ff342d | 874 | global hex decimal fullname_syntax |
08b468e0 KS |
875 | |
876 | set test "mi runto $func" | |
038224f6 | 877 | mi_gdb_test "200-break-insert -t $func" \ |
d24317b4 | 878 | "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\",original-location=\".*\"\}" \ |
08b468e0 KS |
879 | "breakpoint at $func" |
880 | ||
881 | if {![regexp {number="[0-9]+"} $expect_out(buffer) str] | |
882 | || ![scan $str {number="%d"} bkptno]} { | |
883 | set bkptno {[0-9]+} | |
884 | } | |
885 | ||
f7e97bb3 VP |
886 | if {$run_or_continue == "run"} { |
887 | mi_run_cmd | |
888 | } else { | |
bb378428 | 889 | mi_send_resuming_command "exec-continue" "$test" |
f7e97bb3 | 890 | } |
74a44383 | 891 | |
18ac113b | 892 | mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test |
fb40c209 AC |
893 | } |
894 | ||
f7e97bb3 VP |
895 | proc mi_runto {func} { |
896 | mi_runto_helper $func "run" | |
897 | } | |
fb40c209 AC |
898 | |
899 | # Next to the next statement | |
08b468e0 | 900 | # For return values, see mi_execute_to_helper |
fb40c209 AC |
901 | |
902 | proc mi_next { test } { | |
dc360f58 | 903 | return [mi_next_to {.*} {.*} {.*} {.*} $test] |
fb40c209 AC |
904 | } |
905 | ||
906 | ||
907 | # Step to the next statement | |
08b468e0 | 908 | # For return values, see mi_execute_to_helper |
fb40c209 AC |
909 | |
910 | proc mi_step { test } { | |
dc360f58 | 911 | return [mi_step_to {.*} {.*} {.*} {.*} $test] |
fb40c209 | 912 | } |
dcf95b47 | 913 | |
bb378428 VP |
914 | # Wait for MI *stopped notification to appear. |
915 | # The REASON, FUNC, ARGS, FILE and LINE are regular expressions | |
916 | # to match against whatever is output in *stopped. ARGS should | |
917 | # not include [] the list of argument is enclosed in, and other | |
918 | # regular expressions should not include quotes. | |
919 | # If EXTRA is a list of one element, it's the regular expression | |
920 | # for output expected right after *stopped, and before GDB prompt. | |
921 | # If EXTRA is a list of two elements, the first element is for | |
922 | # output right after *stopped, and the second element is output | |
923 | # right after reason field. The regex after reason should not include | |
924 | # the comma separating it from the following fields. | |
925 | # | |
926 | # When we fail to match output at all, -1 is returned. Otherwise, | |
927 | # the line at which we stop is returned. This is useful when exact | |
928 | # line is not possible to specify for some reason -- one can pass | |
929 | # the .* regexp for line, and then check the line programmatically. | |
930 | proc mi_expect_stop { reason func args file line extra test } { | |
1902c51f | 931 | |
dcf95b47 DJ |
932 | global mi_gdb_prompt |
933 | global hex | |
934 | global decimal | |
76ff342d | 935 | global fullname_syntax |
bb378428 VP |
936 | |
937 | set after_stopped "" | |
938 | set after_reason "" | |
939 | if { [llength $extra] == 2 } { | |
940 | set after_stopped [lindex $extra 0] | |
941 | set after_reason [lindex $extra 1] | |
942 | set after_reason "${after_reason}," | |
943 | } elseif { [llength $extra] == 1 } { | |
944 | set after_stopped [lindex $extra 0] | |
945 | } | |
946 | ||
947 | if { $reason == "exited-normally" } { | |
948 | ||
949 | gdb_expect { | |
721c02de | 950 | -re "\\*stopped,reason=\"exited-normally\"\r\n$mi_gdb_prompt$" { |
bb378428 VP |
951 | pass "$test" |
952 | } | |
953 | -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"} | |
954 | timeout { | |
955 | fail "$test (unknown output after running)" | |
956 | } | |
957 | } | |
958 | return | |
959 | } | |
960 | ||
961 | set args "\\\[$args\\\]" | |
962 | ||
963 | set bn "" | |
964 | if { $reason == "breakpoint-hit" } { | |
965 | set bn {bkptno="[0-9]+",} | |
966 | } | |
967 | ||
968 | set r "" | |
969 | if { $reason != "" } { | |
970 | set r "reason=\"$reason\"," | |
971 | } | |
972 | ||
18ac113b AR |
973 | |
974 | set a $after_reason | |
975 | ||
721c02de | 976 | verbose -log "mi_expect_stop: expecting: .*\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\".*$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\"\}$after_stopped\r\n$mi_gdb_prompt$" |
dcf95b47 | 977 | gdb_expect { |
721c02de | 978 | -re ".*\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\".*$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\"\}$after_stopped\r\n$mi_gdb_prompt$" { |
dcf95b47 | 979 | pass "$test" |
bb378428 | 980 | return $expect_out(2,string) |
dcf95b47 | 981 | } |
721c02de | 982 | -re ".*\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\".*\",args=\[\\\[\{\].*\[\\\]\}\],file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}.*\r\n$mi_gdb_prompt$" { |
dcf95b47 DJ |
983 | fail "$test (stopped at wrong place)" |
984 | return -1 | |
985 | } | |
bb378428 | 986 | -re ".*\r\n${mi_gdb_prompt}$" { |
dcf95b47 DJ |
987 | fail "$test (unknown output after running)" |
988 | return -1 | |
989 | } | |
dcf95b47 DJ |
990 | timeout { |
991 | fail "$test (timeout)" | |
992 | return -1 | |
993 | } | |
bb378428 | 994 | } |
dcf95b47 DJ |
995 | } |
996 | ||
bb378428 VP |
997 | # cmd should not include the number or newline (i.e. "exec-step 3", not |
998 | # "220-exec-step 3\n" | |
999 | ||
1000 | # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives | |
1001 | # after the first prompt is printed. | |
1002 | ||
08b468e0 | 1003 | proc mi_execute_to { cmd reason func args file line extra test } { |
bb378428 VP |
1004 | global suppress_flag |
1005 | if { $suppress_flag } { | |
1006 | return -1 | |
1007 | } | |
1008 | ||
1009 | mi_send_resuming_command "$cmd" "$test" | |
1010 | set r [mi_expect_stop $reason $func $args $file $line $extra $test] | |
1011 | return $r | |
dcf95b47 DJ |
1012 | } |
1013 | ||
1014 | proc mi_next_to { func args file line test } { | |
08b468e0 | 1015 | mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \ |
dcf95b47 DJ |
1016 | "$file" "$line" "" "$test" |
1017 | } | |
1018 | ||
1019 | proc mi_step_to { func args file line test } { | |
08b468e0 | 1020 | mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \ |
dcf95b47 DJ |
1021 | "$file" "$line" "" "$test" |
1022 | } | |
1023 | ||
1024 | proc mi_finish_to { func args file line result ret test } { | |
08b468e0 | 1025 | mi_execute_to "exec-finish" "function-finished" "$func" "$args" \ |
dcf95b47 DJ |
1026 | "$file" "$line" \ |
1027 | ",gdb-result-var=\"$result\",return-value=\"$ret\"" \ | |
1028 | "$test" | |
1029 | } | |
1030 | ||
f7e97bb3 VP |
1031 | proc mi_continue_to {func} { |
1032 | mi_runto_helper $func "continue" | |
dcf95b47 DJ |
1033 | } |
1034 | ||
08b468e0 KS |
1035 | proc mi0_execute_to { cmd reason func args file line extra test } { |
1036 | mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \ | |
dcf95b47 DJ |
1037 | "$file" "$line" "$extra" "$test" |
1038 | } | |
1039 | ||
1040 | proc mi0_next_to { func args file line test } { | |
08b468e0 | 1041 | mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \ |
dcf95b47 DJ |
1042 | "$file" "$line" "" "$test" |
1043 | } | |
1044 | ||
1045 | proc mi0_step_to { func args file line test } { | |
08b468e0 | 1046 | mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \ |
dcf95b47 DJ |
1047 | "$file" "$line" "" "$test" |
1048 | } | |
1049 | ||
1050 | proc mi0_finish_to { func args file line result ret test } { | |
08b468e0 | 1051 | mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \ |
dcf95b47 DJ |
1052 | "$file" "$line" \ |
1053 | ",gdb-result-var=\"$result\",return-value=\"$ret\"" \ | |
1054 | "$test" | |
1055 | } | |
1056 | ||
1057 | proc mi0_continue_to { bkptno func args file line test } { | |
08b468e0 | 1058 | mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \ |
dcf95b47 DJ |
1059 | "$func" "$args" "$file" "$line" "" "$test" |
1060 | } | |
b26ed50d | 1061 | |
d24317b4 VP |
1062 | # Creates a breakpoint and checks the reported fields are as expected |
1063 | proc mi_create_breakpoint { location number disp func file line address test } { | |
1064 | verbose -log "Expecting: 222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}" | |
1065 | mi_gdb_test "222-break-insert $location" \ | |
1066 | "222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}" \ | |
1067 | $test | |
1068 | } | |
1069 | ||
1070 | proc mi_list_breakpoints { expected test } { | |
1071 | set fullname ".*" | |
1072 | ||
1073 | set body "" | |
1074 | set first 1 | |
1075 | ||
1076 | foreach item $children { | |
1077 | if {$first == 0} { | |
1078 | set body "$body," | |
1079 | } | |
1080 | set number disp func file line address | |
1081 | set number [lindex $item 0] | |
1082 | set disp [lindex $item 1] | |
1083 | set func [lindex $item 2] | |
1084 | set line [lindex $item 3] | |
1085 | set address [lindex $item 4] | |
1086 | set body "$body,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",${fullname},line=\"$line\",times=\"0\",original-location=\".*\"\}" | |
1087 | set first 0 | |
1088 | } | |
1089 | ||
1090 | verbose -log "Expecint: 666\\\^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=\\\[$body\\\]\}" \ | |
1091 | mi_gdb_test "666-break-list" \ | |
1092 | "666\\\^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=\\\[$body\\\]\}" \ | |
1093 | $test | |
1094 | } | |
1095 | ||
b26ed50d VP |
1096 | # Creates varobj named NAME for EXPRESSION. |
1097 | # Name cannot be "-". | |
1098 | proc mi_create_varobj { name expression testname } { | |
1099 | mi_gdb_test "-var-create $name * $expression" \ | |
983c9c8f | 1100 | "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \ |
b26ed50d VP |
1101 | $testname |
1102 | } | |
1103 | ||
fcacd99f VP |
1104 | proc mi_create_floating_varobj { name expression testname } { |
1105 | mi_gdb_test "-var-create $name @ $expression" \ | |
1106 | "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \ | |
1107 | $testname | |
1108 | } | |
1109 | ||
1110 | ||
9e8e3afe VP |
1111 | # Same as mi_create_varobj, but also checks the reported type |
1112 | # of the varobj. | |
1113 | proc mi_create_varobj_checked { name expression type testname } { | |
1114 | mi_gdb_test "-var-create $name * $expression" \ | |
1115 | "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \ | |
1116 | $testname | |
1117 | } | |
1118 | ||
6e2a9270 VP |
1119 | # Deletes the specified NAME. |
1120 | proc mi_delete_varobj { name testname } { | |
1121 | mi_gdb_test "-var-delete $name" \ | |
1122 | "\\^done,ndeleted=.*" \ | |
1123 | $testname | |
1124 | } | |
1125 | ||
b26ed50d VP |
1126 | # Updates varobj named NAME and checks that all varobjs in EXPECTED |
1127 | # are reported as updated, and no other varobj is updated. | |
1128 | # Assumes that no varobj is out of scope and that no varobj changes | |
1129 | # types. | |
1130 | proc mi_varobj_update { name expected testname } { | |
1131 | set er "\\^done,changelist=\\\[" | |
1132 | set first 1 | |
1133 | foreach item $expected { | |
1134 | set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}" | |
25d5ea92 | 1135 | if {$first == 1} { |
b26ed50d | 1136 | set er "$er$v" |
25d5ea92 | 1137 | set first 0 |
b26ed50d VP |
1138 | } else { |
1139 | set er "$er,$v" | |
1140 | } | |
1141 | } | |
1142 | set er "$er\\\]" | |
1143 | ||
1144 | verbose -log "Expecting: $er" 2 | |
1145 | mi_gdb_test "-var-update $name" $er $testname | |
1146 | } | |
1147 | ||
fcacd99f VP |
1148 | proc mi_varobj_update_with_type_change { name new_type new_children testname } { |
1149 | set v "{name=\"$name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\"}" | |
1150 | set er "\\^done,changelist=\\\[$v\\\]" | |
1151 | verbose -log "Expecting: $er" | |
1152 | mi_gdb_test "-var-update $name" $er $testname | |
1153 | } | |
1154 | ||
b26ed50d VP |
1155 | proc mi_check_varobj_value { name value testname } { |
1156 | ||
1157 | mi_gdb_test "-var-evaluate-expression $name" \ | |
1158 | "\\^done,value=\"$value\"" \ | |
1159 | $testname | |
1160 | } | |
038224f6 VP |
1161 | |
1162 | # Check the results of the: | |
1163 | # | |
1164 | # -var-list-children VARNAME | |
1165 | # | |
1166 | # command. The CHILDREN parement should be a list of lists. | |
1167 | # Each inner list can have either 3 or 4 elements, describing | |
1168 | # fields that gdb is expected to report for child variable object, | |
1169 | # in the following order | |
1170 | # | |
1171 | # - Name | |
1172 | # - Expression | |
1173 | # - Number of children | |
1174 | # - Type | |
1175 | # | |
1176 | # If inner list has 3 elements, the gdb is expected to output no | |
9e8e3afe VP |
1177 | # type for a child and no value. |
1178 | # | |
1179 | # If the inner list has 4 elements, gdb output is expected to | |
1180 | # have no value. | |
038224f6 VP |
1181 | # |
1182 | proc mi_list_varobj_children { varname children testname } { | |
1183 | ||
9e8e3afe VP |
1184 | set options "" |
1185 | if {[llength $varname] == 2} { | |
1186 | set options [lindex $varname 1] | |
1187 | set varname [lindex $varname 0] | |
1188 | } | |
1189 | ||
038224f6 VP |
1190 | set numchildren [llength $children] |
1191 | set children_exp {} | |
1192 | set whatever "\"\[^\"\]+\"" | |
1193 | ||
1194 | foreach item $children { | |
1195 | ||
1196 | set name [lindex $item 0] | |
1197 | set exp [lindex $item 1] | |
1198 | set numchild [lindex $item 2] | |
9e8e3afe | 1199 | if {[llength $item] == 5} { |
038224f6 | 1200 | set type [lindex $item 3] |
9e8e3afe | 1201 | set value [lindex $item 4] |
038224f6 VP |
1202 | |
1203 | lappend children_exp\ | |
9e8e3afe VP |
1204 | "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}" |
1205 | } elseif {[llength $item] == 4} { | |
1206 | set type [lindex $item 3] | |
1207 | ||
1208 | lappend children_exp\ | |
1209 | "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}" | |
038224f6 VP |
1210 | } else { |
1211 | lappend children_exp\ | |
9e8e3afe | 1212 | "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}" |
038224f6 VP |
1213 | } |
1214 | } | |
1215 | set children_exp_j [join $children_exp ","] | |
9e8e3afe VP |
1216 | if {$numchildren} { |
1217 | set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]" | |
1218 | } { | |
1219 | set expected "\\^done,numchild=\"0\"" | |
1220 | } | |
038224f6 VP |
1221 | |
1222 | verbose -log "Expecting: $expected" | |
1223 | ||
9e8e3afe VP |
1224 | mi_gdb_test "-var-list-children $options $varname" $expected $testname |
1225 | } | |
1226 | ||
1227 | # Verifies that variable object VARNAME has NUMBER children, | |
1228 | # where each one is named $VARNAME.<index-of-child> and has type TYPE. | |
1229 | proc mi_list_array_varobj_children { varname number type testname } { | |
1230 | set t {} | |
1231 | for {set i 0} {$i < $number} {incr i} { | |
1232 | lappend t [list $varname.$i $i 0 $type] | |
1233 | } | |
1234 | mi_list_varobj_children $varname $t $testname | |
038224f6 | 1235 | } |
2d0720d9 VP |
1236 | |
1237 | # A list of two-element lists. First element of each list is | |
1238 | # a Tcl statement, and the second element is the line | |
1239 | # number of source C file where the statement originates. | |
1240 | set mi_autotest_data "" | |
1241 | # The name of the source file for autotesting. | |
1242 | set mi_autotest_source "" | |
1243 | ||
1244 | proc count_newlines { string } { | |
1245 | return [regexp -all "\n" $string] | |
1246 | } | |
1247 | ||
1248 | # Prepares for running inline tests in FILENAME. | |
1249 | # See comments for mi_run_inline_test for detailed | |
1250 | # explanation of the idea and syntax. | |
1251 | proc mi_prepare_inline_tests { filename } { | |
1252 | ||
1253 | global srcdir | |
1254 | global subdir | |
1255 | global mi_autotest_source | |
1256 | global mi_autotest_data | |
1257 | ||
1258 | set mi_autotest_data {} | |
1259 | ||
1260 | set mi_autotest_source $filename | |
1261 | ||
1262 | if { ! [regexp "^/" "$filename"] } then { | |
1263 | set filename "$srcdir/$subdir/$filename" | |
1264 | } | |
1265 | ||
1266 | set chan [open $filename] | |
1267 | set content [read $chan] | |
1268 | set line_number 1 | |
1269 | while {1} { | |
1270 | set start [string first "/*:" $content] | |
1271 | if {$start != -1} { | |
1272 | set end [string first ":*/" $content] | |
1273 | if {$end == -1} { | |
1274 | error "Unterminated special comment in $filename" | |
1275 | } | |
1276 | ||
1277 | set prefix [string range $content 0 $start] | |
1278 | set prefix_newlines [count_newlines $prefix] | |
1279 | ||
1280 | set line_number [expr $line_number+$prefix_newlines] | |
1281 | set comment_line $line_number | |
1282 | ||
1283 | set comment [string range $content [expr $start+3] [expr $end-1]] | |
1284 | ||
1285 | set comment_newlines [count_newlines $comment] | |
1286 | set line_number [expr $line_number+$comment_newlines] | |
1287 | ||
1288 | set comment [string trim $comment] | |
1289 | set content [string range $content [expr $end+3] \ | |
1290 | [string length $content]] | |
1291 | lappend mi_autotest_data [list $comment $comment_line] | |
1292 | } else { | |
1293 | break | |
1294 | } | |
1295 | } | |
1296 | close $chan | |
1297 | } | |
1298 | ||
1299 | # Helper to mi_run_inline_test below. | |
1300 | # Return the list of all (statement,line_number) lists | |
1301 | # that comprise TESTCASE. The begin and end markers | |
1302 | # are not included. | |
1303 | proc mi_get_inline_test {testcase} { | |
1304 | ||
1305 | global mi_gdb_prompt | |
1306 | global mi_autotest_data | |
1307 | global mi_autotest_source | |
1308 | ||
1309 | set result {} | |
1310 | ||
1311 | set seen_begin 0 | |
1312 | set seen_end 0 | |
1313 | foreach l $mi_autotest_data { | |
1314 | ||
1315 | set comment [lindex $l 0] | |
1316 | ||
1317 | if {$comment == "BEGIN: $testcase"} { | |
1318 | set seen_begin 1 | |
1319 | } elseif {$comment == "END: $testcase"} { | |
1320 | set seen_end 1 | |
1321 | break | |
1322 | } elseif {$seen_begin==1} { | |
1323 | lappend result $l | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | if {$seen_begin == 0} { | |
1328 | error "Autotest $testcase not found" | |
1329 | } | |
1330 | ||
1331 | if {$seen_begin == 1 && $seen_end == 0} { | |
1332 | error "Missing end marker for test $testcase" | |
1333 | } | |
1334 | ||
1335 | return $result | |
1336 | } | |
1337 | ||
1338 | # Sets temporary breakpoint at LOCATION. | |
1339 | proc mi_tbreak {location} { | |
1340 | ||
1341 | global mi_gdb_prompt | |
1342 | ||
1343 | mi_gdb_test "-break-insert -t $location" \ | |
1344 | {\^done,bkpt=.*} \ | |
1345 | "run to $location (set breakpoint)" | |
1346 | } | |
1347 | ||
1348 | # Send COMMAND that must be a command that resumes | |
1349 | # the inferiour (run/continue/next/etc) and consumes | |
1350 | # the "^running" output from it. | |
1351 | proc mi_send_resuming_command {command test} { | |
1352 | ||
1353 | global mi_gdb_prompt | |
1354 | ||
1355 | send_gdb "220-$command\n" | |
1356 | gdb_expect { | |
1357 | -re "220\\^running\r\n${mi_gdb_prompt}" { | |
1358 | } | |
bb378428 VP |
1359 | -re ".*${mi_gdb_prompt}" { |
1360 | fail "$test (failed to resume)" | |
1361 | } | |
1362 | -re "220\\^error,msg=.*" { | |
1363 | fail "$test (MI error)" | |
1364 | return -1 | |
1365 | } | |
2d0720d9 | 1366 | timeout { |
bb378428 VP |
1367 | fail "$test" |
1368 | return -1 | |
2d0720d9 VP |
1369 | } |
1370 | } | |
1371 | } | |
1372 | ||
1373 | # Helper to mi_run_inline_test below. | |
1374 | # Sets a temporary breakpoint at LOCATION and runs | |
1375 | # the program using COMMAND. When the program is stopped | |
1376 | # returns the line at which it. Returns -1 if line cannot | |
1377 | # be determined. | |
1378 | # Does not check that the line is the same as requested. | |
1379 | # The caller can check itself if required. | |
a73bafbc | 1380 | proc mi_continue_to_line {location test} { |
2d0720d9 VP |
1381 | |
1382 | mi_tbreak $location | |
1383 | mi_send_resuming_command "exec-continue" "run to $location (exec-continue)" | |
bb378428 | 1384 | return [mi_get_stop_line $test] |
2d0720d9 VP |
1385 | } |
1386 | ||
1387 | # Wait until gdb prints the current line. | |
bb378428 | 1388 | proc mi_get_stop_line {test} { |
2d0720d9 VP |
1389 | |
1390 | global mi_gdb_prompt | |
1391 | ||
1392 | gdb_expect { | |
1393 | -re ".*line=\"(.*)\".*\r\n$mi_gdb_prompt$" { | |
1394 | return $expect_out(1,string) | |
1395 | } | |
1396 | -re ".*$mi_gdb_prompt$" { | |
1397 | fail "wait for stop ($test)" | |
1398 | } | |
1399 | timeout { | |
1400 | fail "wait for stop ($test)" | |
1401 | } | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | # Run a MI test embedded in comments in a C file. | |
1406 | # The C file should contain special comments in the following | |
1407 | # three forms: | |
1408 | # | |
1409 | # /*: BEGIN: testname :*/ | |
1410 | # /*: <Tcl statements> :*/ | |
1411 | # /*: END: testname :*/ | |
1412 | # | |
1413 | # This procedure find the begin and end marker for the requested | |
1414 | # test. Then, a temporary breakpoint is set at the begin | |
1415 | # marker and the program is run (from start). | |
1416 | # | |
1417 | # After that, for each special comment between the begin and end | |
1418 | # marker, the Tcl statements are executed. It is assumed that | |
1419 | # for each comment, the immediately preceding line is executable | |
1420 | # C statement. Then, gdb will be single-stepped until that | |
1421 | # preceding C statement is executed, and after that the | |
1422 | # Tcl statements in the comment will be executed. | |
1423 | # | |
1424 | # For example: | |
1425 | # | |
1426 | # /*: BEGIN: assignment-test :*/ | |
1427 | # v = 10; | |
1428 | # /*: <Tcl code to check that 'v' is indeed 10 :*/ | |
1429 | # /*: END: assignment-test :*/ | |
1430 | # | |
1431 | # The mi_prepare_inline_tests function should be called before | |
1432 | # calling this function. A given C file can contain several | |
1433 | # inline tests. The names of the tests must be unique within one | |
1434 | # C file. | |
1435 | # | |
1436 | proc mi_run_inline_test { testcase } { | |
1437 | ||
1438 | global mi_gdb_prompt | |
1439 | global hex | |
1440 | global decimal | |
1441 | global fullname_syntax | |
1442 | global mi_autotest_source | |
1443 | ||
1444 | set commands [mi_get_inline_test $testcase] | |
1445 | ||
1446 | set first 1 | |
1447 | set line_now 1 | |
1448 | ||
1449 | foreach c $commands { | |
1450 | set statements [lindex $c 0] | |
1451 | set line [lindex $c 1] | |
1452 | set line [expr $line-1] | |
1453 | ||
1454 | # We want gdb to be stopped at the expression immediately | |
1455 | # before the comment. If this is the first comment, the | |
1456 | # program is either not started yet or is in some random place, | |
1457 | # so we run it. For further comments, we might be already | |
1458 | # standing at the right line. If not continue till the | |
1459 | # right line. | |
1460 | ||
1461 | if {$first==1} { | |
1462 | # Start the program afresh. | |
1463 | mi_tbreak "$mi_autotest_source:$line" | |
1464 | mi_run_cmd | |
bb378428 | 1465 | set line_now [mi_get_stop_line "$testcase: step to $line"] |
2d0720d9 VP |
1466 | set first 0 |
1467 | } elseif {$line_now!=$line} { | |
25d5ea92 | 1468 | set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"] |
2d0720d9 VP |
1469 | } |
1470 | ||
1471 | if {$line_now!=$line} { | |
1472 | fail "$testcase: go to line $line" | |
1473 | } | |
1474 | ||
1475 | # We're not at the statement right above the comment. | |
1476 | # Execute that statement so that the comment can test | |
1477 | # the state after the statement is executed. | |
1478 | ||
1479 | # Single-step past the line. | |
1480 | mi_send_resuming_command "exec-next" "$testcase: step over $line" | |
bb378428 | 1481 | set line_now [mi_get_stop_line "$testcase: step over $line"] |
2d0720d9 VP |
1482 | |
1483 | # We probably want to use 'uplevel' so that statements | |
1484 | # have direct access to global variables that the | |
1485 | # main 'exp' file has set up. But it's not yet clear, | |
1486 | # will need more experience to be sure. | |
1487 | eval $statements | |
1488 | } | |
1489 | } |