1cad1b12977d6562cb1a3740e068c04dceb39bde
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / mi-support.exp
1 # Copyright (C) 1999, 2000 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 set MIFLAGS "-i=mi"
32
33 #
34 # mi_gdb_exit -- exit the GDB, killing the target program if necessary
35 #
36 proc mi_gdb_exit {} {
37 catch mi_uncatched_gdb_exit
38 }
39
40 proc mi_uncatched_gdb_exit {} {
41 global GDB
42 global GDBFLAGS
43 global verbose
44 global gdb_spawn_id;
45 global gdb_prompt
46 global mi_gdb_prompt
47 global MIFLAGS
48
49 gdb_stop_suppressing_tests;
50
51 if { [info procs sid_exit] != "" } {
52 sid_exit
53 }
54
55 if ![info exists gdb_spawn_id] {
56 return;
57 }
58
59 verbose "Quitting $GDB $GDBFLAGS $MIFLAGS"
60
61 if { [is_remote host] && [board_info host exists fileid] } {
62 send_gdb "999-gdb-exit\n";
63 gdb_expect 10 {
64 -re "y or n" {
65 send_gdb "y\n";
66 exp_continue;
67 }
68 -re "Undefined command.*$gdb_prompt $" {
69 send_gdb "quit\n"
70 exp_continue;
71 }
72 -re "DOSEXIT code" { }
73 default { }
74 }
75 }
76
77 if ![is_remote host] {
78 remote_close host;
79 }
80 unset gdb_spawn_id
81 }
82
83 #
84 # start gdb -- start gdb running, default procedure
85 #
86 # When running over NFS, particularly if running many simultaneous
87 # tests on different hosts all using the same server, things can
88 # get really slow. Give gdb at least 3 minutes to start up.
89 #
90 proc mi_gdb_start { } {
91 global verbose
92 global GDB
93 global GDBFLAGS
94 global gdb_prompt
95 global mi_gdb_prompt
96 global timeout
97 global gdb_spawn_id;
98 global MIFLAGS
99
100 gdb_stop_suppressing_tests;
101
102 verbose "Spawning $GDB -nw $GDBFLAGS $MIFLAGS"
103
104 if [info exists gdb_spawn_id] {
105 return 0;
106 }
107
108 if ![is_remote host] {
109 if { [which $GDB] == 0 } then {
110 perror "$GDB does not exist."
111 exit 1
112 }
113 }
114 set res [remote_spawn host "$GDB -nw $GDBFLAGS $MIFLAGS [host_info gdb_opts]"];
115 if { $res < 0 || $res == "" } {
116 perror "Spawning $GDB failed."
117 return 1;
118 }
119 gdb_expect {
120 -re ".*UI_OUT.*$mi_gdb_prompt$" {
121 verbose "GDB initialized."
122 }
123 -re ".*$mi_gdb_prompt$" {
124 untested "Skip mi tests (output not in headless format)."
125 remote_close host;
126 return -1;
127 }
128 -re ".*$gdb_prompt $" {
129 untested "Skip mi tests (got non-mi prompt)."
130 remote_close host;
131 return -1;
132 }
133 -re ".*unrecognized option.*for a complete list of options." {
134 untested "Skip mi tests (not compiled with mi support)."
135 remote_close host;
136 return -1;
137 }
138 timeout {
139 perror "(timeout) GDB never initialized after 10 seconds."
140 remote_close host;
141 return -1
142 }
143 }
144 set gdb_spawn_id -1;
145
146 # FIXME: mi output does not go through pagers, so these can be removed.
147 # force the height to "unlimited", so no pagers get used
148 send_gdb "100-gdb-set height 0\n"
149 gdb_expect 10 {
150 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
151 verbose "Setting height to 0." 2
152 }
153 timeout {
154 warning "Couldn't set the height to 0"
155 }
156 }
157 # force the width to "unlimited", so no wraparound occurs
158 send_gdb "101-gdb-set width 0\n"
159 gdb_expect 10 {
160 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
161 verbose "Setting width to 0." 2
162 }
163 timeout {
164 warning "Couldn't set the width to 0."
165 }
166 }
167
168 # Finally start SID.
169 if { [info procs sid_start] != "" } {
170 verbose "Spawning SID"
171 sid_start
172 }
173
174 return 0;
175 }
176
177 # Many of the tests depend on setting breakpoints at various places and
178 # running until that breakpoint is reached. At times, we want to start
179 # with a clean-slate with respect to breakpoints, so this utility proc
180 # lets us do this without duplicating this code everywhere.
181 #
182
183 proc mi_delete_breakpoints {} {
184 global mi_gdb_prompt
185
186 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
187 send_gdb "102-break-delete\n"
188 gdb_expect 30 {
189 -re "Delete all breakpoints.*y or n.*$" {
190 send_gdb "y\n";
191 exp_continue
192 }
193 -re ".*102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
194 # This happens if there were no breakpoints
195 }
196 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
197 }
198
199 # The correct output is not "No breakpoints or watchpoints." but an
200 # empty BreakpointTable. Also, a query is not acceptable with mi.
201 send_gdb "103-break-list\n"
202 gdb_expect 30 {
203 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
204 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
205 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
206 -re "Delete all breakpoints.*or n.*$" {
207 warning "Unexpected prompt for breakpoints deletion";
208 send_gdb "y\n";
209 exp_continue
210 }
211 timeout { perror "-break-list (timeout)" ; return }
212 }
213 }
214
215 proc mi_gdb_reinitialize_dir { subdir } {
216 global mi_gdb_prompt
217
218 global suppress_flag
219 if { $suppress_flag } {
220 return
221 }
222
223 if [is_remote host] {
224 return "";
225 }
226
227 send_gdb "104-environment-directory\n"
228 gdb_expect 60 {
229 -re ".*Reinitialize source path to empty.*y or n. " {
230 warning "Got confirmation prompt for dir reinitialization."
231 send_gdb "y\n"
232 gdb_expect 60 {
233 -re "$mi_gdb_prompt$" {}
234 timeout {error "Dir reinitialization failed (timeout)"}
235 }
236 }
237 -re "$mi_gdb_prompt$" {}
238 timeout {error "Dir reinitialization failed (timeout)"}
239 }
240
241 send_gdb "105-environment-directory $subdir\n"
242 gdb_expect 60 {
243 -re "Source directories searched.*$mi_gdb_prompt$" {
244 verbose "Dir set to $subdir"
245 }
246 -re "105\\\^done\r\n$mi_gdb_prompt$" {
247 # FIXME: We return just the prompt for now.
248 verbose "Dir set to $subdir"
249 # perror "Dir \"$subdir\" failed."
250 }
251 }
252 }
253
254 #
255 # load a file into the debugger.
256 # return a -1 if anything goes wrong.
257 #
258 proc mi_gdb_load { arg } {
259 global verbose
260 global loadpath
261 global loadfile
262 global GDB
263 global mi_gdb_prompt
264 upvar timeout timeout
265
266 # ``gdb_unload''
267
268 # ``gdb_file_cmd''
269 # FIXME: Several of these patterns are only acceptable for console
270 # output. Queries are an error for mi.
271 send_gdb "105-file-exec-and-symbols $arg\n"
272 gdb_expect 120 {
273 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
274 verbose "\t\tLoaded $arg into the $GDB"
275 # All OK
276 }
277 -re "has no symbol-table.*$mi_gdb_prompt$" {
278 perror "$arg wasn't compiled with \"-g\""
279 return -1
280 }
281 -re "A program is being debugged already.*Kill it.*y or n. $" {
282 send_gdb "y\n"
283 verbose "\t\tKilling previous program being debugged"
284 exp_continue
285 }
286 -re "Load new symbol table from \".*\".*y or n. $" {
287 send_gdb "y\n"
288 gdb_expect 120 {
289 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
290 verbose "\t\tLoaded $arg with new symbol table into $GDB"
291 # All OK
292 }
293 timeout {
294 perror "(timeout) Couldn't load $arg, other program already loaded."
295 return -1
296 }
297 }
298 }
299 -re "No such file or directory.*$mi_gdb_prompt$" {
300 perror "($arg) No such file or directory\n"
301 return -1
302 }
303 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
304 # We are just giving the prompt back for now
305 # All OK
306 }
307 timeout {
308 perror "couldn't load $arg into $GDB (timed out)."
309 return -1
310 }
311 eof {
312 # This is an attempt to detect a core dump, but seems not to
313 # work. Perhaps we need to match .* followed by eof, in which
314 # gdb_expect does not seem to have a way to do that.
315 perror "couldn't load $arg into $GDB (end of file)."
316 return -1
317 }
318 }
319
320 # ``load''
321 if { [info procs send_target_sid] != "" } {
322 # For SID, things get complex
323 send_target_sid
324 gdb_expect 60 {
325 -re "\\^done,.*$mi_gdb_prompt$" {
326 }
327 timeout {
328 perror "Unable to connect to SID target"
329 return -1
330 }
331 }
332 send_gdb "48-target-download\n"
333 gdb_expect 10 {
334 -re "48\\^done.*$mi_gdb_prompt$" {
335 }
336 timeout {
337 perror "Unable to download to SID target"
338 return -1
339 }
340 }
341 } elseif { [target_info protocol] == "sim" } {
342 # For the simulator, just connect to it directly.
343 send_gdb "47-target-select sim\n"
344 gdb_expect 10 {
345 -re "47\\^connected.*$mi_gdb_prompt$" {
346 }
347 timeout {
348 perror "Unable to select sim target"
349 return -1
350 }
351 }
352 send_gdb "48-target-download\n"
353 gdb_expect 10 {
354 -re "48\\^done.*$mi_gdb_prompt$" {
355 }
356 timeout {
357 perror "Unable to download to sim target"
358 return -1
359 }
360 }
361 }
362 return 0
363 }
364
365 # mi_gdb_test COMMAND PATTERN MESSAGE -- send a command to gdb; test the result.
366 #
367 # COMMAND is the command to execute, send to GDB with send_gdb. If
368 # this is the null string no command is sent.
369 # PATTERN is the pattern to match for a PASS, and must NOT include
370 # the \r\n sequence immediately before the gdb prompt.
371 # MESSAGE is an optional message to be printed. If this is
372 # omitted, then the pass/fail messages use the command string as the
373 # message. (If this is the empty string, then sometimes we don't
374 # call pass or fail at all; I don't understand this at all.)
375 #
376 # Returns:
377 # 1 if the test failed,
378 # 0 if the test passes,
379 # -1 if there was an internal error.
380 #
381 proc mi_gdb_test { args } {
382 global verbose
383 global mi_gdb_prompt
384 global GDB
385 upvar timeout timeout
386
387 if [llength $args]>2 then {
388 set message [lindex $args 2]
389 } else {
390 set message [lindex $args 0]
391 }
392 set command [lindex $args 0]
393 set pattern [lindex $args 1]
394
395 if [llength $args]==5 {
396 set question_string [lindex $args 3];
397 set response_string [lindex $args 4];
398 } else {
399 set question_string "^FOOBAR$"
400 }
401
402 if $verbose>2 then {
403 send_user "Sending \"$command\" to gdb\n"
404 send_user "Looking to match \"$pattern\"\n"
405 send_user "Message is \"$message\"\n"
406 }
407
408 set result -1
409 set string "${command}\n";
410 if { $command != "" } {
411 while { "$string" != "" } {
412 set foo [string first "\n" "$string"];
413 set len [string length "$string"];
414 if { $foo < [expr $len - 1] } {
415 set str [string range "$string" 0 $foo];
416 if { [send_gdb "$str"] != "" } {
417 global suppress_flag;
418
419 if { ! $suppress_flag } {
420 perror "Couldn't send $command to GDB.";
421 }
422 fail "$message";
423 return $result;
424 }
425 gdb_expect 2 {
426 -re "\[\r\n\]" { }
427 timeout { }
428 }
429 set string [string range "$string" [expr $foo + 1] end];
430 } else {
431 break;
432 }
433 }
434 if { "$string" != "" } {
435 if { [send_gdb "$string"] != "" } {
436 global suppress_flag;
437
438 if { ! $suppress_flag } {
439 perror "Couldn't send $command to GDB.";
440 }
441 fail "$message";
442 return $result;
443 }
444 }
445 }
446
447 if [info exists timeout] {
448 set tmt $timeout;
449 } else {
450 global timeout;
451 if [info exists timeout] {
452 set tmt $timeout;
453 } else {
454 set tmt 60;
455 }
456 }
457 gdb_expect $tmt {
458 -re "\\*\\*\\* DOSEXIT code.*" {
459 if { $message != "" } {
460 fail "$message";
461 }
462 gdb_suppress_entire_file "GDB died";
463 return -1;
464 }
465 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
466 if ![isnative] then {
467 warning "Can`t communicate to remote target."
468 }
469 gdb_exit
470 gdb_start
471 set result -1
472 }
473 -re "(${question_string})$" {
474 send_gdb "$response_string\n";
475 exp_continue;
476 }
477 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
478 perror "Undefined command \"$command\"."
479 fail "$message"
480 set result 1
481 }
482 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
483 perror "\"$command\" is not a unique command name."
484 fail "$message"
485 set result 1
486 }
487 -re "\[\r\n\]*($pattern)\[\r\n\]+$mi_gdb_prompt\[ \]*$" {
488 if ![string match "" $message] then {
489 pass "$message"
490 }
491 set result 0
492 }
493 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
494 if ![string match "" $message] then {
495 set errmsg "$message: the program exited"
496 } else {
497 set errmsg "$command: the program exited"
498 }
499 fail "$errmsg"
500 return -1
501 }
502 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
503 if ![string match "" $message] then {
504 set errmsg "$message: the program is no longer running"
505 } else {
506 set errmsg "$command: the program is no longer running"
507 }
508 fail "$errmsg"
509 return -1
510 }
511 -re ".*$mi_gdb_prompt\[ \]*$" {
512 if ![string match "" $message] then {
513 fail "$message"
514 }
515 set result 1
516 }
517 "<return>" {
518 send_gdb "\n"
519 perror "Window too small."
520 fail "$message"
521 }
522 -re "\\(y or n\\) " {
523 send_gdb "n\n"
524 perror "Got interactive prompt."
525 fail "$message"
526 }
527 eof {
528 perror "Process no longer exists"
529 if { $message != "" } {
530 fail "$message"
531 }
532 return -1
533 }
534 full_buffer {
535 perror "internal buffer is full."
536 fail "$message"
537 }
538 timeout {
539 if ![string match "" $message] then {
540 fail "$message (timeout)"
541 }
542 set result 1
543 }
544 }
545 return $result
546 }
547
548 #
549 # MI run command. (A modified version of gdb_run_cmd)
550 #
551
552 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
553 # ``.*$'' could swallow up output that we attempt to match elsewhere.
554
555 proc mi_run_cmd {args} {
556 global suppress_flag
557 if { $suppress_flag } {
558 return -1
559 }
560 global mi_gdb_prompt
561
562 if [target_info exists gdb_init_command] {
563 send_gdb "[target_info gdb_init_command]\n";
564 gdb_expect 30 {
565 -re "$mi_gdb_prompt$" { }
566 default {
567 perror "gdb_init_command for target failed";
568 return;
569 }
570 }
571 }
572
573 if [target_info exists use_gdb_stub] {
574 if [target_info exists gdb,do_reload_on_run] {
575 # Specifying no file, defaults to the executable
576 # currently being debugged.
577 if { [mi_gdb_load ""] < 0 } {
578 return;
579 }
580 send_gdb "000-exec-continue\n";
581 gdb_expect 60 {
582 -re "Continu\[^\r\n\]*\[\r\n\]" {}
583 default {}
584 }
585 return;
586 }
587 }
588
589 send_gdb "000-exec-run $args\n"
590 gdb_expect {
591 -re "000\\^running\r\n${mi_gdb_prompt}" {
592 }
593 timeout {
594 perror "Unable to start target"
595 return
596 }
597 }
598 # NOTE: Shortly after this there will be a ``000*stopping,...(gdb)''
599 }
600
601 #
602 # Just like run-to-main but works with the MI interface
603 #
604
605 proc mi_run_to_main { } {
606 global suppress_flag
607 if { $suppress_flag } {
608 return -1
609 }
610
611 global mi_gdb_prompt
612 global hex
613 global decimal
614 global srcdir
615 global subdir
616 global binfile
617 global srcfile
618
619 set test "mi run-to-main"
620 mi_delete_breakpoints
621 mi_gdb_reinitialize_dir $srcdir/$subdir
622 mi_gdb_load ${binfile}
623
624 mi_gdb_test "200-break-insert main" \
625 "200\\^done,bkpt=\{number=\"1\",type=\"breakpoint\",disp=\"keep\",enabled=\"y\",addr=\"$hex\",func=\"main\",file=\".*\",line=\"\[0-9\]*\",times=\"0\"\}" \
626 "breakpoint at main"
627
628 mi_run_cmd
629 gdb_expect {
630 -re "000\\*stopped,reason=\"breakpoint-hit\",bkptno=\"1\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\"main\",args=\{\},file=\".*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
631 pass "$test"
632 return 0
633 }
634 timeout {
635 fail "$test (timeout)"
636 return -1
637 }
638 }
639 }
640
641
642 # Next to the next statement
643
644 proc mi_next { test } {
645 global suppress_flag
646 if { $suppress_flag } {
647 return -1
648 }
649 global mi_gdb_prompt
650 send_gdb "220-exec-next\n"
651 gdb_expect {
652 -re "220\\^running\r\n${mi_gdb_prompt}220\\*stopped,reason=\"end-stepping-range\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\".*\",args=\{.*\},,file=\".*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
653 pass "$test"
654 return 0
655 }
656 timeout {
657 fail "$test"
658 return -1
659 }
660 }
661 }
662
663
664 # Step to the next statement
665
666 proc mi_step { test } {
667 global suppress_flag
668 if { $suppress_flag } {
669 return -1
670 }
671 global mi_gdb_prompt
672 send_gdb "220-exec-step\n"
673 gdb_expect {
674 -re "220\\^running\r\n${mi_gdb_prompt}220\\*stopped,reason=\"end-stepping-range\",thread-id=\"$decimal\",frame=\{addr=\"$hex\",func=\".*\",args=\{.*\},,file=\".*\",line=\"\[0-9\]*\"\}\r\n$mi_gdb_prompt$" {
675 pass "$test"
676 return 0
677 }
678 timeout {
679 fail "$test"
680 return -1
681 }
682 }
683 }
684
685
686 # Local variables:
687 # change-log-default-name: "../gdb.mi/ChangeLog-mi"
688 # End:
This page took 0.051125 seconds and 4 git commands to generate.