Updated copyright notices for most files.
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / mi-support.exp
1 # Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008
2 # Free Software Foundation, Inc.
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
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
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.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
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
31 global mi_inferior_spawn_id
32 global mi_inferior_tty_name
33
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 #
87 # mi_gdb_start [INFERIOR_PTY] -- start gdb running, default procedure
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.
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 #
97 proc mi_gdb_start { args } {
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;
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]
115
116 # Start SID.
117 if { [info procs sid_start] != "" } {
118 verbose "Spawning SID"
119 sid_start
120 }
121
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 }
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
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 {
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 }
170 verbose "GDB initialized."
171 }
172 -re ".*$gdb_prompt $" {
173 untested "Skip mi tests (got non-mi prompt)."
174 remote_close host;
175 return -1;
176 }
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 }
182 -re ".*Interpreter `mi' unrecognized." {
183 untested "Skip mi tests (not compiled with mi support)."
184 remote_close host;
185 return -1;
186 }
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 }
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 }
229
230 return 0;
231 }
232
233 # Many of the tests depend on setting breakpoints at various places and
234 # running until that breakpoint is reached. At times, we want to start
235 # with a clean-slate with respect to breakpoints, so this utility proc
236 # lets us do this without duplicating this code everywhere.
237 #
238
239 proc mi_delete_breakpoints {} {
240 global mi_gdb_prompt
241
242 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
243 send_gdb "102-break-delete\n"
244 gdb_expect 30 {
245 -re "Delete all breakpoints.*y or n.*$" {
246 send_gdb "y\n";
247 exp_continue
248 }
249 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
250 # This happens if there were no breakpoints
251 }
252 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
253 }
254
255 # The correct output is not "No breakpoints or watchpoints." but an
256 # empty BreakpointTable. Also, a query is not acceptable with mi.
257 send_gdb "103-break-list\n"
258 gdb_expect 30 {
259 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
260 -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=\\\[\\\]\}" {}
261 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
262 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
263 -re "Delete all breakpoints.*or n.*$" {
264 warning "Unexpected prompt for breakpoints deletion";
265 send_gdb "y\n";
266 exp_continue
267 }
268 timeout { perror "-break-list (timeout)" ; return }
269 }
270 }
271
272 proc mi_gdb_reinitialize_dir { subdir } {
273 global mi_gdb_prompt
274 global MIFLAGS
275
276 global suppress_flag
277 if { $suppress_flag } {
278 return
279 }
280
281 if [is_remote host] {
282 return "";
283 }
284
285 if { $MIFLAGS == "-i=mi1" } {
286 send_gdb "104-environment-directory\n"
287 gdb_expect 60 {
288 -re ".*Reinitialize source path to empty.*y or n. " {
289 warning "Got confirmation prompt for dir reinitialization."
290 send_gdb "y\n"
291 gdb_expect 60 {
292 -re "$mi_gdb_prompt$" {}
293 timeout {error "Dir reinitialization failed (timeout)"}
294 }
295 }
296 -re "$mi_gdb_prompt$" {}
297 timeout {error "Dir reinitialization failed (timeout)"}
298 }
299 } else {
300 send_gdb "104-environment-directory -r\n"
301 gdb_expect 60 {
302 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
303 -re "$mi_gdb_prompt$" {}
304 timeout {error "Dir reinitialization failed (timeout)"}
305 }
306 }
307
308 send_gdb "105-environment-directory $subdir\n"
309 gdb_expect 60 {
310 -re "Source directories searched.*$mi_gdb_prompt$" {
311 verbose "Dir set to $subdir"
312 }
313 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
314 # FIXME: We return just the prompt for now.
315 verbose "Dir set to $subdir"
316 # perror "Dir \"$subdir\" failed."
317 }
318 }
319 }
320
321 # Send GDB the "target" command.
322 # FIXME: Some of these patterns are not appropriate for MI. Based on
323 # config/monitor.exp:gdb_target_command.
324 proc mi_gdb_target_cmd { targetname serialport } {
325 global mi_gdb_prompt
326
327 set serialport_re [string_to_regexp $serialport]
328 for {set i 1} {$i <= 3} {incr i} {
329 send_gdb "47-target-select $targetname $serialport\n"
330 gdb_expect 60 {
331 -re "47\\^connected.*$mi_gdb_prompt$" {
332 verbose "Set target to $targetname";
333 return 0;
334 }
335 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
336 verbose "Connection failed";
337 }
338 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
339 verbose "Set target to $targetname";
340 return 0;
341 }
342 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
343 verbose "Set target to $targetname";
344 return 0;
345 }
346 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
347 verbose "Set target to $targetname";
348 return 0;
349 }
350 -re "Connected to.*$mi_gdb_prompt$" {
351 verbose "Set target to $targetname";
352 return 0;
353 }
354 -re "Ending remote.*$mi_gdb_prompt$" { }
355 -re "Connection refused.*$mi_gdb_prompt$" {
356 verbose "Connection refused by remote target. Pausing, and trying again."
357 sleep 5
358 continue
359 }
360 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
361 verbose "Got timeout error from gdb.";
362 }
363 timeout {
364 send_gdb "\ 3";
365 break
366 }
367 }
368 }
369 return 1
370 }
371
372 #
373 # load a file into the debugger (file command only).
374 # return a -1 if anything goes wrong.
375 #
376 proc mi_gdb_file_cmd { arg } {
377 global verbose
378 global loadpath
379 global loadfile
380 global GDB
381 global mi_gdb_prompt
382 global last_loaded_file
383 upvar timeout timeout
384
385 set last_loaded_file $arg
386
387 if [is_remote host] {
388 set arg [remote_download host $arg];
389 if { $arg == "" } {
390 error "download failed"
391 return -1;
392 }
393 }
394
395 # FIXME: Several of these patterns are only acceptable for console
396 # output. Queries are an error for mi.
397 send_gdb "105-file-exec-and-symbols $arg\n"
398 gdb_expect 120 {
399 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
400 verbose "\t\tLoaded $arg into the $GDB"
401 return 0
402 }
403 -re "has no symbol-table.*$mi_gdb_prompt$" {
404 perror "$arg wasn't compiled with \"-g\""
405 return -1
406 }
407 -re "Load new symbol table from \".*\".*y or n. $" {
408 send_gdb "y\n"
409 gdb_expect 120 {
410 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
411 verbose "\t\tLoaded $arg with new symbol table into $GDB"
412 # All OK
413 }
414 timeout {
415 perror "(timeout) Couldn't load $arg, other program already loaded."
416 return -1
417 }
418 }
419 }
420 -re "No such file or directory.*$mi_gdb_prompt$" {
421 perror "($arg) No such file or directory\n"
422 return -1
423 }
424 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
425 # We (MI) are just giving the prompt back for now, instead of giving
426 # some acknowledgement.
427 return 0
428 }
429 timeout {
430 perror "couldn't load $arg into $GDB (timed out)."
431 return -1
432 }
433 eof {
434 # This is an attempt to detect a core dump, but seems not to
435 # work. Perhaps we need to match .* followed by eof, in which
436 # gdb_expect does not seem to have a way to do that.
437 perror "couldn't load $arg into $GDB (end of file)."
438 return -1
439 }
440 }
441 }
442
443 #
444 # connect to the target and download a file, if necessary.
445 # return a -1 if anything goes wrong.
446 #
447 proc mi_gdb_target_load { } {
448 global verbose
449 global loadpath
450 global loadfile
451 global GDB
452 global mi_gdb_prompt
453 upvar timeout timeout
454
455 if { [info procs gdbserver_gdb_load] != "" } {
456 mi_gdb_test "kill" ".*" ""
457 set res [gdbserver_gdb_load]
458 set protocol [lindex $res 0]
459 set gdbport [lindex $res 1]
460
461 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
462 return -1
463 }
464 } elseif { [info procs send_target_sid] != "" } {
465 # For SID, things get complex
466 send_gdb "kill\n"
467 gdb_expect 10 {
468 -re ".*$mi_gdb_prompt$"
469 }
470 send_target_sid
471 gdb_expect 60 {
472 -re "\\^done.*$mi_gdb_prompt$" {
473 }
474 timeout {
475 perror "Unable to connect to SID target"
476 return -1
477 }
478 }
479 send_gdb "48-target-download\n"
480 gdb_expect 10 {
481 -re "48\\^done.*$mi_gdb_prompt$" {
482 }
483 timeout {
484 perror "Unable to download to SID target"
485 return -1
486 }
487 }
488 } elseif { [target_info protocol] == "sim" } {
489 # For the simulator, just connect to it directly.
490 send_gdb "47-target-select sim\n"
491 gdb_expect 10 {
492 -re "47\\^connected.*$mi_gdb_prompt$" {
493 }
494 timeout {
495 perror "Unable to select sim target"
496 return -1
497 }
498 }
499 send_gdb "48-target-download\n"
500 gdb_expect 10 {
501 -re "48\\^done.*$mi_gdb_prompt$" {
502 }
503 timeout {
504 perror "Unable to download to sim target"
505 return -1
506 }
507 }
508 } elseif { [target_info gdb_protocol] == "remote" } {
509 # remote targets
510 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
511 perror "Unable to connect to remote target"
512 return -1
513 }
514 send_gdb "48-target-download\n"
515 gdb_expect 10 {
516 -re "48\\^done.*$mi_gdb_prompt$" {
517 }
518 timeout {
519 perror "Unable to download to remote target"
520 return -1
521 }
522 }
523 }
524 return 0
525 }
526
527 #
528 # load a file into the debugger.
529 # return a -1 if anything goes wrong.
530 #
531 proc mi_gdb_load { arg } {
532 if { $arg != "" } {
533 return [mi_gdb_file_cmd $arg]
534 }
535 return 0
536 }
537
538 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
539 # test the result.
540 #
541 # COMMAND is the command to execute, send to GDB with send_gdb. If
542 # this is the null string no command is sent.
543 # PATTERN is the pattern to match for a PASS, and must NOT include
544 # the \r\n sequence immediately before the gdb prompt.
545 # MESSAGE is the message to be printed. (If this is the empty string,
546 # then sometimes we don't call pass or fail at all; I don't
547 # understand this at all.)
548 # IPATTERN is the pattern to match for the inferior's output. This parameter
549 # is optional. If present, it will produce a PASS if the match is
550 # successful, and a FAIL if unsuccessful.
551 #
552 # Returns:
553 # 1 if the test failed,
554 # 0 if the test passes,
555 # -1 if there was an internal error.
556 #
557 proc mi_gdb_test { args } {
558 global verbose
559 global mi_gdb_prompt
560 global GDB expect_out
561 upvar timeout timeout
562
563 set command [lindex $args 0]
564 set pattern [lindex $args 1]
565 set message [lindex $args 2]
566
567 if [llength $args]==4 {
568 set ipattern [lindex $args 3]
569 }
570
571 if [llength $args]==5 {
572 set question_string [lindex $args 3];
573 set response_string [lindex $args 4];
574 } else {
575 set question_string "^FOOBAR$"
576 }
577
578 if $verbose>2 then {
579 send_user "Sending \"$command\" to gdb\n"
580 send_user "Looking to match \"$pattern\"\n"
581 send_user "Message is \"$message\"\n"
582 }
583
584 set result -1
585 set string "${command}\n";
586 set string_regex [string_to_regexp $command]
587
588 if { $command != "" } {
589 while { "$string" != "" } {
590 set foo [string first "\n" "$string"];
591 set len [string length "$string"];
592 if { $foo < [expr $len - 1] } {
593 set str [string range "$string" 0 $foo];
594 if { [send_gdb "$str"] != "" } {
595 global suppress_flag;
596
597 if { ! $suppress_flag } {
598 perror "Couldn't send $command to GDB.";
599 }
600 fail "$message";
601 return $result;
602 }
603 gdb_expect 2 {
604 -re "\[\r\n\]" { }
605 timeout { }
606 }
607 set string [string range "$string" [expr $foo + 1] end];
608 } else {
609 break;
610 }
611 }
612 if { "$string" != "" } {
613 if { [send_gdb "$string"] != "" } {
614 global suppress_flag;
615
616 if { ! $suppress_flag } {
617 perror "Couldn't send $command to GDB.";
618 }
619 fail "$message";
620 return $result;
621 }
622 }
623 }
624
625 if [info exists timeout] {
626 set tmt $timeout;
627 } else {
628 global timeout;
629 if [info exists timeout] {
630 set tmt $timeout;
631 } else {
632 set tmt 60;
633 }
634 }
635 gdb_expect $tmt {
636 -re "\\*\\*\\* DOSEXIT code.*" {
637 if { $message != "" } {
638 fail "$message";
639 }
640 gdb_suppress_entire_file "GDB died";
641 return -1;
642 }
643 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
644 if ![isnative] then {
645 warning "Can`t communicate to remote target."
646 }
647 gdb_exit
648 gdb_start
649 set result -1
650 }
651 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
652 # At this point, $expect_out(1,string) is the MI input command.
653 # and $expect_out(2,string) is the MI output command.
654 # If $expect_out(1,string) is "", then there was no MI input command here.
655
656 # NOTE, there is no trailing anchor because with GDB/MI,
657 # asynchronous responses can happen at any point, causing more
658 # data to be available. Normally an anchor is used to make
659 # sure the end of the output is matched, however, $mi_gdb_prompt
660 # is just as good of an anchor since mi_gdb_test is meant to
661 # match a single mi output command. If a second GDB/MI output
662 # response is sent, it will be in the buffer for the next
663 # time mi_gdb_test is called.
664 if ![string match "" $message] then {
665 pass "$message"
666 }
667 set result 0
668 }
669 -re "(${question_string})$" {
670 send_gdb "$response_string\n";
671 exp_continue;
672 }
673 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
674 perror "Undefined command \"$command\"."
675 fail "$message"
676 set result 1
677 }
678 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
679 perror "\"$command\" is not a unique command name."
680 fail "$message"
681 set result 1
682 }
683 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
684 if ![string match "" $message] then {
685 set errmsg "$message (the program exited)"
686 } else {
687 set errmsg "$command (the program exited)"
688 }
689 fail "$errmsg"
690 return -1
691 }
692 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
693 if ![string match "" $message] then {
694 set errmsg "$message (the program is no longer running)"
695 } else {
696 set errmsg "$command (the program is no longer running)"
697 }
698 fail "$errmsg"
699 return -1
700 }
701 -re ".*$mi_gdb_prompt\[ \]*$" {
702 if ![string match "" $message] then {
703 fail "$message"
704 }
705 set result 1
706 }
707 "<return>" {
708 send_gdb "\n"
709 perror "Window too small."
710 fail "$message"
711 }
712 -re "\\(y or n\\) " {
713 send_gdb "n\n"
714 perror "Got interactive prompt."
715 fail "$message"
716 }
717 eof {
718 perror "Process no longer exists"
719 if { $message != "" } {
720 fail "$message"
721 }
722 return -1
723 }
724 full_buffer {
725 perror "internal buffer is full."
726 fail "$message"
727 }
728 timeout {
729 if ![string match "" $message] then {
730 fail "$message (timeout)"
731 }
732 set result 1
733 }
734 }
735
736 # If the GDB output matched, compare the inferior output.
737 if { $result == 0 } {
738 if [ info exists ipattern ] {
739 if { ![target_info exists gdb,noinferiorio] } {
740 global mi_inferior_spawn_id
741 expect {
742 -i $mi_inferior_spawn_id -re "$ipattern" {
743 pass "$message inferior output"
744 }
745 timeout {
746 fail "$message inferior output (timeout)"
747 set result 1
748 }
749 }
750 } else {
751 unsupported "$message inferior output"
752 }
753 }
754 }
755
756 return $result
757 }
758
759 #
760 # MI run command. (A modified version of gdb_run_cmd)
761 #
762
763 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
764 # ``.*$'' could swallow up output that we attempt to match elsewhere.
765
766 proc mi_run_cmd {args} {
767 global suppress_flag
768 if { $suppress_flag } {
769 return -1
770 }
771 global mi_gdb_prompt
772
773 if [target_info exists gdb_init_command] {
774 send_gdb "[target_info gdb_init_command]\n";
775 gdb_expect 30 {
776 -re "$mi_gdb_prompt$" { }
777 default {
778 perror "gdb_init_command for target failed";
779 return;
780 }
781 }
782 }
783
784 if { [mi_gdb_target_load] < 0 } {
785 return
786 }
787
788 if [target_info exists use_gdb_stub] {
789 if [target_info exists gdb,do_reload_on_run] {
790 send_gdb "000-exec-continue\n";
791 gdb_expect 60 {
792 -re "000\\^running\[\r\n\]+$mi_gdb_prompt$" {}
793 default {}
794 }
795 return;
796 }
797
798 if [target_info exists gdb,start_symbol] {
799 set start [target_info gdb,start_symbol];
800 } else {
801 set start "start";
802 }
803
804 # HACK: Should either use 000-jump or fix the target code
805 # to better handle RUN.
806 send_gdb "jump *$start\n"
807 warning "Using CLI jump command, expect run-to-main FAIL"
808 return
809 }
810
811 send_gdb "000-exec-run $args\n"
812 gdb_expect {
813 -re "000\\^running\r\n${mi_gdb_prompt}" {
814 }
815 timeout {
816 perror "Unable to start target"
817 return
818 }
819 }
820 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
821 }
822
823 #
824 # Just like run-to-main but works with the MI interface
825 #
826
827 proc mi_run_to_main { } {
828 global suppress_flag
829 if { $suppress_flag } {
830 return -1
831 }
832
833 global srcdir
834 global subdir
835 global binfile
836 global srcfile
837
838 mi_delete_breakpoints
839 mi_gdb_reinitialize_dir $srcdir/$subdir
840 mi_gdb_load ${binfile}
841
842 mi_runto main
843 }
844
845
846 # Just like gdb's "runto" proc, it will run the target to a given
847 # function. The big difference here between mi_runto and mi_execute_to
848 # is that mi_execute_to must have the inferior running already. This
849 # proc will (like gdb's runto) (re)start the inferior, too.
850 #
851 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
852 # It returns:
853 # -1 if test suppressed, failed, timedout
854 # 0 if test passed
855
856 proc mi_runto_helper {func run_or_continue} {
857 global suppress_flag
858 if { $suppress_flag } {
859 return -1
860 }
861
862 global mi_gdb_prompt expect_out
863 global hex decimal fullname_syntax
864
865 set test "mi runto $func"
866 mi_gdb_test "200-break-insert -t $func" \
867 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
868 "breakpoint at $func"
869
870 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
871 || ![scan $str {number="%d"} bkptno]} {
872 set bkptno {[0-9]+}
873 }
874
875 if {$run_or_continue == "run"} {
876 mi_run_cmd
877 } else {
878 send_gdb "000-exec-continue\n"
879 gdb_expect {
880 -re "000\\^running\r\n${mi_gdb_prompt}" {
881 }
882 timeout {
883 fail "$test"
884 return -1
885 }
886 }
887 }
888
889 gdb_expect {
890 -re ".*000\\*stopped,thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=\(\\\[.*\\\]\|\{.*\}\),file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
891 pass "$test"
892 return 0
893 }
894 -re ".*$mi_gdb_prompt$" {
895 fail "$test (2)"
896 }
897 timeout {
898 fail "$test (timeout)"
899 return -1
900 }
901 }
902 }
903
904 proc mi_runto {func} {
905 mi_runto_helper $func "run"
906 }
907
908 # Next to the next statement
909 # For return values, see mi_execute_to_helper
910
911 proc mi_next { test } {
912 return [mi_next_to {.*} {.*} {.*} {.*} $test]
913 }
914
915
916 # Step to the next statement
917 # For return values, see mi_execute_to_helper
918
919 proc mi_step { test } {
920 return [mi_step_to {.*} {.*} {.*} {.*} $test]
921 }
922
923 # cmd should not include the number or newline (i.e. "exec-step 3", not
924 # "220-exec-step 3\n"
925
926 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
927 # after the first prompt is printed.
928
929 proc mi_execute_to_helper { cmd reason func args file line extra test } {
930 global suppress_flag
931 if { $suppress_flag } {
932 return -1
933 }
934 global mi_gdb_prompt
935 global hex
936 global decimal
937 global fullname_syntax
938 send_gdb "220-$cmd\n"
939 gdb_expect {
940 -re "220\\^running\r\n${mi_gdb_prompt}.*220\\*stopped,reason=\"$reason\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\".*$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\"\}$extra\r\n$mi_gdb_prompt$" {
941 pass "$test"
942 return 0
943 }
944 -re "220\\^running\r\n${mi_gdb_prompt}.*220\\*stopped,reason=\"$reason\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\".*\",args=\[\\\[\{\].*\[\\\]\}\],file=\".*\",fullname=\"${fullname_syntax}.*\",line=\"\[0-9\]*\"\}.*\r\n$mi_gdb_prompt$" {
945 fail "$test (stopped at wrong place)"
946 return -1
947 }
948 -re "220\\^running\r\n${mi_gdb_prompt}.*\r\n${mi_gdb_prompt}$" {
949 fail "$test (unknown output after running)"
950 return -1
951 }
952 timeout {
953 fail "$test (timeout)"
954 return -1
955 }
956 }
957 }
958
959 proc mi_execute_to { cmd reason func args file line extra test } {
960 mi_execute_to_helper "$cmd" "$reason" "$func" "\\\[$args\\\]" \
961 "$file" "$line" "$extra" "$test"
962 }
963
964 proc mi_next_to { func args file line test } {
965 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
966 "$file" "$line" "" "$test"
967 }
968
969 proc mi_step_to { func args file line test } {
970 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
971 "$file" "$line" "" "$test"
972 }
973
974 proc mi_finish_to { func args file line result ret test } {
975 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
976 "$file" "$line" \
977 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
978 "$test"
979 }
980
981 proc mi_continue_to {func} {
982 mi_runto_helper $func "continue"
983 }
984
985 proc mi0_execute_to { cmd reason func args file line extra test } {
986 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
987 "$file" "$line" "$extra" "$test"
988 }
989
990 proc mi0_next_to { func args file line test } {
991 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
992 "$file" "$line" "" "$test"
993 }
994
995 proc mi0_step_to { func args file line test } {
996 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
997 "$file" "$line" "" "$test"
998 }
999
1000 proc mi0_finish_to { func args file line result ret test } {
1001 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1002 "$file" "$line" \
1003 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1004 "$test"
1005 }
1006
1007 proc mi0_continue_to { bkptno func args file line test } {
1008 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1009 "$func" "$args" "$file" "$line" "" "$test"
1010 }
1011
1012 # Creates varobj named NAME for EXPRESSION.
1013 # Name cannot be "-".
1014 proc mi_create_varobj { name expression testname } {
1015 mi_gdb_test "-var-create $name * $expression" \
1016 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1017 $testname
1018 }
1019
1020 # Deletes the specified NAME.
1021 proc mi_delete_varobj { name testname } {
1022 mi_gdb_test "-var-delete $name" \
1023 "\\^done,ndeleted=.*" \
1024 $testname
1025 }
1026
1027 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1028 # are reported as updated, and no other varobj is updated.
1029 # Assumes that no varobj is out of scope and that no varobj changes
1030 # types.
1031 proc mi_varobj_update { name expected testname } {
1032 set er "\\^done,changelist=\\\["
1033 set first 1
1034 foreach item $expected {
1035 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1036 if {$first == 1} {
1037 set er "$er$v"
1038 set first 0
1039 } else {
1040 set er "$er,$v"
1041 }
1042 }
1043 set er "$er\\\]"
1044
1045 verbose -log "Expecting: $er" 2
1046 mi_gdb_test "-var-update $name" $er $testname
1047 }
1048
1049 proc mi_check_varobj_value { name value testname } {
1050
1051 mi_gdb_test "-var-evaluate-expression $name" \
1052 "\\^done,value=\"$value\"" \
1053 $testname
1054 }
1055
1056 # Check the results of the:
1057 #
1058 # -var-list-children VARNAME
1059 #
1060 # command. The CHILDREN parement should be a list of lists.
1061 # Each inner list can have either 3 or 4 elements, describing
1062 # fields that gdb is expected to report for child variable object,
1063 # in the following order
1064 #
1065 # - Name
1066 # - Expression
1067 # - Number of children
1068 # - Type
1069 #
1070 # If inner list has 3 elements, the gdb is expected to output no
1071 # type for a child.
1072 #
1073 proc mi_list_varobj_children { varname children testname } {
1074
1075 set numchildren [llength $children]
1076 set children_exp {}
1077 set whatever "\"\[^\"\]+\""
1078
1079 foreach item $children {
1080
1081 set name [lindex $item 0]
1082 set exp [lindex $item 1]
1083 set numchild [lindex $item 2]
1084 if {[llength $item] == 4} {
1085 set type [lindex $item 3]
1086
1087 lappend children_exp\
1088 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"}"
1089 } else {
1090 lappend children_exp\
1091 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"}"
1092 }
1093 }
1094 set children_exp_j [join $children_exp ","]
1095 set expected "\\^done,numchild=\"$numchildren\",children=\\\[$children_exp_j\\\]"
1096
1097 verbose -log "Expecting: $expected"
1098
1099 mi_gdb_test "-var-list-children $varname" $expected $testname
1100 }
1101
1102 # A list of two-element lists. First element of each list is
1103 # a Tcl statement, and the second element is the line
1104 # number of source C file where the statement originates.
1105 set mi_autotest_data ""
1106 # The name of the source file for autotesting.
1107 set mi_autotest_source ""
1108
1109 proc count_newlines { string } {
1110 return [regexp -all "\n" $string]
1111 }
1112
1113 # Prepares for running inline tests in FILENAME.
1114 # See comments for mi_run_inline_test for detailed
1115 # explanation of the idea and syntax.
1116 proc mi_prepare_inline_tests { filename } {
1117
1118 global srcdir
1119 global subdir
1120 global mi_autotest_source
1121 global mi_autotest_data
1122
1123 set mi_autotest_data {}
1124
1125 set mi_autotest_source $filename
1126
1127 if { ! [regexp "^/" "$filename"] } then {
1128 set filename "$srcdir/$subdir/$filename"
1129 }
1130
1131 set chan [open $filename]
1132 set content [read $chan]
1133 set line_number 1
1134 while {1} {
1135 set start [string first "/*:" $content]
1136 if {$start != -1} {
1137 set end [string first ":*/" $content]
1138 if {$end == -1} {
1139 error "Unterminated special comment in $filename"
1140 }
1141
1142 set prefix [string range $content 0 $start]
1143 set prefix_newlines [count_newlines $prefix]
1144
1145 set line_number [expr $line_number+$prefix_newlines]
1146 set comment_line $line_number
1147
1148 set comment [string range $content [expr $start+3] [expr $end-1]]
1149
1150 set comment_newlines [count_newlines $comment]
1151 set line_number [expr $line_number+$comment_newlines]
1152
1153 set comment [string trim $comment]
1154 set content [string range $content [expr $end+3] \
1155 [string length $content]]
1156 lappend mi_autotest_data [list $comment $comment_line]
1157 } else {
1158 break
1159 }
1160 }
1161 close $chan
1162 }
1163
1164 # Helper to mi_run_inline_test below.
1165 # Return the list of all (statement,line_number) lists
1166 # that comprise TESTCASE. The begin and end markers
1167 # are not included.
1168 proc mi_get_inline_test {testcase} {
1169
1170 global mi_gdb_prompt
1171 global mi_autotest_data
1172 global mi_autotest_source
1173
1174 set result {}
1175
1176 set seen_begin 0
1177 set seen_end 0
1178 foreach l $mi_autotest_data {
1179
1180 set comment [lindex $l 0]
1181
1182 if {$comment == "BEGIN: $testcase"} {
1183 set seen_begin 1
1184 } elseif {$comment == "END: $testcase"} {
1185 set seen_end 1
1186 break
1187 } elseif {$seen_begin==1} {
1188 lappend result $l
1189 }
1190 }
1191
1192 if {$seen_begin == 0} {
1193 error "Autotest $testcase not found"
1194 }
1195
1196 if {$seen_begin == 1 && $seen_end == 0} {
1197 error "Missing end marker for test $testcase"
1198 }
1199
1200 return $result
1201 }
1202
1203 # Sets temporary breakpoint at LOCATION.
1204 proc mi_tbreak {location} {
1205
1206 global mi_gdb_prompt
1207
1208 mi_gdb_test "-break-insert -t $location" \
1209 {\^done,bkpt=.*} \
1210 "run to $location (set breakpoint)"
1211 }
1212
1213 # Send COMMAND that must be a command that resumes
1214 # the inferiour (run/continue/next/etc) and consumes
1215 # the "^running" output from it.
1216 proc mi_send_resuming_command {command test} {
1217
1218 global mi_gdb_prompt
1219
1220 send_gdb "220-$command\n"
1221 gdb_expect {
1222 -re "220\\^running\r\n${mi_gdb_prompt}" {
1223 }
1224 timeout {
1225 fail $test
1226 }
1227 }
1228 }
1229
1230 # Helper to mi_run_inline_test below.
1231 # Sets a temporary breakpoint at LOCATION and runs
1232 # the program using COMMAND. When the program is stopped
1233 # returns the line at which it. Returns -1 if line cannot
1234 # be determined.
1235 # Does not check that the line is the same as requested.
1236 # The caller can check itself if required.
1237 proc mi_continue_to_line {location test} {
1238
1239 mi_tbreak $location
1240 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1241 return [mi_wait_for_stop $test]
1242 }
1243
1244 # Wait until gdb prints the current line.
1245 proc mi_wait_for_stop {test} {
1246
1247 global mi_gdb_prompt
1248
1249 gdb_expect {
1250 -re ".*line=\"(.*)\".*\r\n$mi_gdb_prompt$" {
1251 return $expect_out(1,string)
1252 }
1253 -re ".*$mi_gdb_prompt$" {
1254 fail "wait for stop ($test)"
1255 }
1256 timeout {
1257 fail "wait for stop ($test)"
1258 }
1259 }
1260 }
1261
1262 # Run a MI test embedded in comments in a C file.
1263 # The C file should contain special comments in the following
1264 # three forms:
1265 #
1266 # /*: BEGIN: testname :*/
1267 # /*: <Tcl statements> :*/
1268 # /*: END: testname :*/
1269 #
1270 # This procedure find the begin and end marker for the requested
1271 # test. Then, a temporary breakpoint is set at the begin
1272 # marker and the program is run (from start).
1273 #
1274 # After that, for each special comment between the begin and end
1275 # marker, the Tcl statements are executed. It is assumed that
1276 # for each comment, the immediately preceding line is executable
1277 # C statement. Then, gdb will be single-stepped until that
1278 # preceding C statement is executed, and after that the
1279 # Tcl statements in the comment will be executed.
1280 #
1281 # For example:
1282 #
1283 # /*: BEGIN: assignment-test :*/
1284 # v = 10;
1285 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1286 # /*: END: assignment-test :*/
1287 #
1288 # The mi_prepare_inline_tests function should be called before
1289 # calling this function. A given C file can contain several
1290 # inline tests. The names of the tests must be unique within one
1291 # C file.
1292 #
1293 proc mi_run_inline_test { testcase } {
1294
1295 global mi_gdb_prompt
1296 global hex
1297 global decimal
1298 global fullname_syntax
1299 global mi_autotest_source
1300
1301 set commands [mi_get_inline_test $testcase]
1302
1303 set first 1
1304 set line_now 1
1305
1306 foreach c $commands {
1307 set statements [lindex $c 0]
1308 set line [lindex $c 1]
1309 set line [expr $line-1]
1310
1311 # We want gdb to be stopped at the expression immediately
1312 # before the comment. If this is the first comment, the
1313 # program is either not started yet or is in some random place,
1314 # so we run it. For further comments, we might be already
1315 # standing at the right line. If not continue till the
1316 # right line.
1317
1318 if {$first==1} {
1319 # Start the program afresh.
1320 mi_tbreak "$mi_autotest_source:$line"
1321 mi_run_cmd
1322 set line_now [mi_wait_for_stop "$testcase: step to $line"]
1323 set first 0
1324 } elseif {$line_now!=$line} {
1325 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1326 }
1327
1328 if {$line_now!=$line} {
1329 fail "$testcase: go to line $line"
1330 }
1331
1332 # We're not at the statement right above the comment.
1333 # Execute that statement so that the comment can test
1334 # the state after the statement is executed.
1335
1336 # Single-step past the line.
1337 mi_send_resuming_command "exec-next" "$testcase: step over $line"
1338 set line_now [mi_wait_for_stop "$testcase: step over $line"]
1339
1340 # We probably want to use 'uplevel' so that statements
1341 # have direct access to global variables that the
1342 # main 'exp' file has set up. But it's not yet clear,
1343 # will need more experience to be sure.
1344 eval $statements
1345 }
1346 }
This page took 0.058552 seconds and 4 git commands to generate.