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