3e71adcae6b70e2c67fbc945136d90022b74efa0
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright (C) 1992, 1994, 1995, 1997, 1999 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 written by Fred Fish. (fnf@cygnus.com)
21
22 # Generic gdb subroutines that should work for any target. If these
23 # need to be modified for any target, it can be done with a variable
24 # or by passing arguments.
25
26 load_lib libgloss.exp
27
28 global GDB
29 global CHILL_LIB
30 global CHILL_RT0
31
32 if ![info exists CHILL_LIB] {
33 set CHILL_LIB [findfile $base_dir/../../gcc/ch/runtime/libchill.a "$base_dir/../../gcc/ch/runtime/libchill.a" [transform -lchill]]
34 }
35 verbose "using CHILL_LIB = $CHILL_LIB" 2
36 if ![info exists CHILL_RT0] {
37 set CHILL_RT0 [findfile $base_dir/../../gcc/ch/runtime/chillrt0.o "$base_dir/../../gcc/ch/runtime/chillrt0.o" ""]
38 }
39 verbose "using CHILL_RT0 = $CHILL_RT0" 2
40
41 if [info exists TOOL_EXECUTABLE] {
42 set GDB $TOOL_EXECUTABLE;
43 }
44 if ![info exists GDB] {
45 if ![is_remote host] {
46 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
47 } else {
48 set GDB [transform gdb];
49 }
50 }
51 verbose "using GDB = $GDB" 2
52
53 global GDBFLAGS
54 if ![info exists GDBFLAGS] {
55 set GDBFLAGS "-nx"
56 }
57 verbose "using GDBFLAGS = $GDBFLAGS" 2
58
59 # The variable gdb_prompt is a regexp which matches the gdb prompt.
60 # Set it if it is not already set.
61 global gdb_prompt
62 if ![info exists gdb_prompt] then {
63 set gdb_prompt "\[(\]gdb\[)\]"
64 }
65
66 ### Only procedures should come after this point.
67
68 #
69 # gdb_version -- extract and print the version number of GDB
70 #
71 proc default_gdb_version {} {
72 global GDB
73 global GDBFLAGS
74 global gdb_prompt
75 set fileid [open "gdb_cmd" w];
76 puts $fileid "q";
77 close $fileid;
78 set cmdfile [remote_download host "gdb_cmd"];
79 set output [remote_exec host "$GDB -nw --command $cmdfile"]
80 remote_file build delete "gdb_cmd";
81 remote_file host delete "$cmdfile";
82 set tmp [lindex $output 1];
83 set version ""
84 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
85 if ![is_remote host] {
86 clone_output "[which $GDB] version $version $GDBFLAGS\n"
87 } else {
88 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
89 }
90 }
91
92 proc gdb_version { } {
93 return [default_gdb_version];
94 }
95
96 #
97 # gdb_unload -- unload a file if one is loaded
98 #
99
100 proc gdb_unload {} {
101 global verbose
102 global GDB
103 global gdb_prompt
104 send_gdb "file\n"
105 gdb_expect 60 {
106 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
107 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
108 -re "A program is being debugged already..*Kill it.*y or n. $"\
109 { send_gdb "y\n"
110 verbose "\t\tKilling previous program being debugged"
111 exp_continue
112 }
113 -re "Discard symbol table from .*y or n.*$" {
114 send_gdb "y\n"
115 exp_continue
116 }
117 -re "$gdb_prompt $" {}
118 timeout {
119 perror "couldn't unload file in $GDB (timed out)."
120 return -1
121 }
122 }
123 }
124
125 # Many of the tests depend on setting breakpoints at various places and
126 # running until that breakpoint is reached. At times, we want to start
127 # with a clean-slate with respect to breakpoints, so this utility proc
128 # lets us do this without duplicating this code everywhere.
129 #
130
131 proc delete_breakpoints {} {
132 global gdb_prompt
133
134 send_gdb "delete breakpoints\n"
135 gdb_expect 30 {
136 -re "Delete all breakpoints.*y or n.*$" {
137 send_gdb "y\n";
138 exp_continue
139 }
140 -re "$gdb_prompt $" { # This happens if there were no breakpoints
141 }
142 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
143 }
144 send_gdb "info breakpoints\n"
145 gdb_expect 30 {
146 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
147 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
148 -re "Delete all breakpoints.*or n.*$" {
149 send_gdb "y\n";
150 exp_continue
151 }
152 timeout { perror "info breakpoints (timeout)" ; return }
153 }
154 }
155
156
157 #
158 # Generic run command.
159 #
160 # The second pattern below matches up to the first newline *only*.
161 # Using ``.*$'' could swallow up output that we attempt to match
162 # elsewhere.
163 #
164 proc gdb_run_cmd {args} {
165 global gdb_prompt
166
167 if [target_info exists gdb_init_command] {
168 send_gdb "[target_info gdb_init_command]\n";
169 gdb_expect 30 {
170 -re "$gdb_prompt $" { }
171 default {
172 perror "gdb_init_command for target failed";
173 return;
174 }
175 }
176 }
177
178 if [target_info exists use_gdb_stub] {
179 if [target_info exists gdb,do_reload_on_run] {
180 # According to Stu, this will always work.
181 gdb_load "";
182 send_gdb "continue\n";
183 gdb_expect 60 {
184 -re "Continu\[^\r\n\]*\[\r\n\]" {}
185 default {}
186 }
187 return;
188 }
189
190 if [target_info exists gdb,start_symbol] {
191 set start [target_info gdb,start_symbol];
192 } else {
193 set start "start";
194 }
195 send_gdb "jump *$start\n"
196 gdb_expect 30 {
197 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
198 if ![target_info exists gdb_stub] {
199 return;
200 }
201 }
202 -re "No symbol \"start\" in current.*$gdb_prompt $" {
203 send_gdb "jump *_start\n";
204 exp_continue;
205 }
206 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
207 perror "Can't find start symbol to run in gdb_run";
208 return;
209 }
210 -re "Line.* Jump anyway.*y or n. $" {
211 send_gdb "y\n"
212 exp_continue;
213 }
214 -re "No symbol.*context.*$gdb_prompt $" {}
215 -re "The program is not being run.*$gdb_prompt $" {
216 gdb_load "";
217 send_gdb "jump *$start\n";
218 exp_continue;
219 }
220 timeout { perror "Jump to start() failed (timeout)"; return }
221 }
222 if [target_info exists gdb_stub] {
223 gdb_expect 60 {
224 -re "$gdb_prompt $" {
225 send_gdb "continue\n"
226 }
227 }
228 }
229 return
230 }
231 send_gdb "run $args\n"
232 # This doesn't work quite right yet.
233 gdb_expect 60 {
234 -re "The program .* has been started already.*y or n. $" {
235 send_gdb "y\n"
236 exp_continue
237 }
238 -re "Starting program: \[^\r\n\]*" {}
239 }
240 }
241
242 proc gdb_breakpoint { function } {
243 global gdb_prompt
244 global decimal
245
246 send_gdb "break $function\n"
247 # The first two regexps are what we get with -g, the third is without -g.
248 gdb_expect 30 {
249 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
250 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
251 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
252 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
253 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
254 }
255 return 1;
256 }
257
258 # Set breakpoint at function and run gdb until it breaks there.
259 # Since this is the only breakpoint that will be set, if it stops
260 # at a breakpoint, we will assume it is the one we want. We can't
261 # just compare to "function" because it might be a fully qualified,
262 # single quoted C++ function specifier.
263
264 proc runto { function } {
265 global gdb_prompt
266 global decimal
267
268 delete_breakpoints
269
270 if ![gdb_breakpoint $function] {
271 return 0;
272 }
273
274 gdb_run_cmd
275
276 # the "at foo.c:36" output we get with -g.
277 # the "in func" output we get without -g.
278 gdb_expect 30 {
279 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
280 return 1
281 }
282 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
283 return 1
284 }
285 -re "$gdb_prompt $" {
286 fail "running to $function in runto"
287 return 0
288 }
289 timeout {
290 fail "running to $function in runto (timeout)"
291 return 0
292 }
293 }
294 return 1
295 }
296
297 #
298 # runto_main -- ask gdb to run until we hit a breakpoint at main.
299 # The case where the target uses stubs has to be handled
300 # specially--if it uses stubs, assuming we hit
301 # breakpoint() and just step out of the function.
302 #
303 proc runto_main { } {
304 global gdb_prompt
305 global decimal
306
307 if ![target_info exists gdb_stub] {
308 return [runto main]
309 }
310
311 delete_breakpoints
312
313 gdb_step_for_stub;
314
315 return 1
316 }
317
318
319 # gdb_test COMMAND PATTERN MESSAGE -- send a command to gdb; test the result.
320 #
321 # COMMAND is the command to execute, send to GDB with send_gdb. If
322 # this is the null string no command is sent.
323 # PATTERN is the pattern to match for a PASS, and must NOT include
324 # the \r\n sequence immediately before the gdb prompt.
325 # MESSAGE is an optional message to be printed. If this is
326 # omitted, then the pass/fail messages use the command string as the
327 # message. (If this is the empty string, then sometimes we don't
328 # call pass or fail at all; I don't understand this at all.)
329 #
330 # Returns:
331 # 1 if the test failed,
332 # 0 if the test passes,
333 # -1 if there was an internal error.
334 #
335 proc gdb_test { args } {
336 global verbose
337 global gdb_prompt
338 global GDB
339 upvar timeout timeout
340
341 if [llength $args]>2 then {
342 set message [lindex $args 2]
343 } else {
344 set message [lindex $args 0]
345 }
346 set command [lindex $args 0]
347 set pattern [lindex $args 1]
348
349 if [llength $args]==5 {
350 set question_string [lindex $args 3];
351 set response_string [lindex $args 4];
352 } else {
353 set question_string "^FOOBAR$"
354 }
355
356 if $verbose>2 then {
357 send_user "Sending \"$command\" to gdb\n"
358 send_user "Looking to match \"$pattern\"\n"
359 send_user "Message is \"$message\"\n"
360 }
361
362 set result -1
363 set string "${command}\n";
364 if { $command != "" } {
365 while { "$string" != "" } {
366 set foo [string first "\n" "$string"];
367 set len [string length "$string"];
368 if { $foo < [expr $len - 1] } {
369 set str [string range "$string" 0 $foo];
370 if { [send_gdb "$str"] != "" } {
371 global suppress_flag;
372
373 if { ! $suppress_flag } {
374 perror "Couldn't send $command to GDB.";
375 }
376 fail "$message";
377 return $result;
378 }
379 gdb_expect 2 {
380 -re "\[\r\n\]" { }
381 timeout { }
382 }
383 set string [string range "$string" [expr $foo + 1] end];
384 } else {
385 break;
386 }
387 }
388 if { "$string" != "" } {
389 if { [send_gdb "$string"] != "" } {
390 global suppress_flag;
391
392 if { ! $suppress_flag } {
393 perror "Couldn't send $command to GDB.";
394 }
395 fail "$message";
396 return $result;
397 }
398 }
399 }
400
401 if [info exists timeout] {
402 set tmt $timeout;
403 } else {
404 global timeout;
405 if [info exists timeout] {
406 set tmt $timeout;
407 } else {
408 set tmt 60;
409 }
410 }
411 gdb_expect $tmt {
412 -re "\\*\\*\\* DOSEXIT code.*" {
413 if { $message != "" } {
414 fail "$message";
415 }
416 gdb_suppress_entire_file "GDB died";
417 return -1;
418 }
419 -re "Ending remote debugging.*$gdb_prompt $" {
420 if ![isnative] then {
421 warning "Can`t communicate to remote target."
422 }
423 gdb_exit
424 gdb_start
425 set result -1
426 }
427 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
428 if ![string match "" $message] then {
429 pass "$message"
430 }
431 set result 0
432 }
433 -re "(${question_string})$" {
434 send_gdb "$response_string\n";
435 exp_continue;
436 }
437 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
438 perror "Undefined command \"$command\"."
439 fail "$message"
440 set result 1
441 }
442 -re "Ambiguous command.*$gdb_prompt $" {
443 perror "\"$command\" is not a unique command name."
444 fail "$message"
445 set result 1
446 }
447 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
448 if ![string match "" $message] then {
449 set errmsg "$message: the program exited"
450 } else {
451 set errmsg "$command: the program exited"
452 }
453 fail "$errmsg"
454 return -1
455 }
456 -re "The program is not being run.*$gdb_prompt $" {
457 if ![string match "" $message] then {
458 set errmsg "$message: the program is no longer running"
459 } else {
460 set errmsg "$command: the program is no longer running"
461 }
462 fail "$errmsg"
463 return -1
464 }
465 -re ".*$gdb_prompt $" {
466 if ![string match "" $message] then {
467 fail "$message"
468 }
469 set result 1
470 }
471 "<return>" {
472 send_gdb "\n"
473 perror "Window too small."
474 fail "$message"
475 }
476 -re "\\(y or n\\) " {
477 send_gdb "n\n"
478 perror "Got interactive prompt."
479 fail "$message"
480 }
481 eof {
482 perror "Process no longer exists"
483 if { $message != "" } {
484 fail "$message"
485 }
486 return -1
487 }
488 full_buffer {
489 perror "internal buffer is full."
490 fail "$message"
491 }
492 timeout {
493 if ![string match "" $message] then {
494 fail "$message (timeout)"
495 }
496 set result 1
497 }
498 }
499 return $result
500 }
501 \f
502 # Test that a command gives an error. For pass or fail, return
503 # a 1 to indicate that more tests can proceed. However a timeout
504 # is a serious error, generates a special fail message, and causes
505 # a 0 to be returned to indicate that more tests are likely to fail
506 # as well.
507
508 proc test_print_reject { args } {
509 global gdb_prompt
510 global verbose
511
512 if [llength $args]==2 then {
513 set expectthis [lindex $args 1]
514 } else {
515 set expectthis "should never match this bogus string"
516 }
517 set sendthis [lindex $args 0]
518 if $verbose>2 then {
519 send_user "Sending \"$sendthis\" to gdb\n"
520 send_user "Looking to match \"$expectthis\"\n"
521 }
522 send_gdb "$sendthis\n"
523 #FIXME: Should add timeout as parameter.
524 gdb_expect {
525 -re "A .* in expression.*\\.*$gdb_prompt $" {
526 pass "reject $sendthis"
527 return 1
528 }
529 -re "Invalid syntax in expression.*$gdb_prompt $" {
530 pass "reject $sendthis"
531 return 1
532 }
533 -re "Junk after end of expression.*$gdb_prompt $" {
534 pass "reject $sendthis"
535 return 1
536 }
537 -re "Invalid number.*$gdb_prompt $" {
538 pass "reject $sendthis"
539 return 1
540 }
541 -re "Invalid character constant.*$gdb_prompt $" {
542 pass "reject $sendthis"
543 return 1
544 }
545 -re "No symbol table is loaded.*$gdb_prompt $" {
546 pass "reject $sendthis"
547 return 1
548 }
549 -re "No symbol .* in current context.*$gdb_prompt $" {
550 pass "reject $sendthis"
551 return 1
552 }
553 -re "$expectthis.*$gdb_prompt $" {
554 pass "reject $sendthis"
555 return 1
556 }
557 -re ".*$gdb_prompt $" {
558 fail "reject $sendthis"
559 return 1
560 }
561 default {
562 fail "reject $sendthis (eof or timeout)"
563 return 0
564 }
565 }
566 }
567 \f
568 # Given an input string, adds backslashes as needed to create a
569 # regexp that will match the string.
570
571 proc string_to_regexp {str} {
572 set result $str
573 regsub -all {[]*+.|()^$\[]} $str {\\&} result
574 return $result
575 }
576
577 # Same as gdb_test, but the second parameter is not a regexp,
578 # but a string that must match exactly.
579
580 proc gdb_test_exact { args } {
581 upvar timeout timeout
582
583 set command [lindex $args 0]
584
585 # This applies a special meaning to a null string pattern. Without
586 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
587 # messages from commands that should have no output except a new
588 # prompt. With this, only results of a null string will match a null
589 # string pattern.
590
591 set pattern [lindex $args 1]
592 if [string match $pattern ""] {
593 set pattern [string_to_regexp [lindex $args 0]]
594 } else {
595 set pattern [string_to_regexp [lindex $args 1]]
596 }
597
598 # It is most natural to write the pattern argument with only
599 # embedded \n's, especially if you are trying to avoid Tcl quoting
600 # problems. But gdb_expect really wants to see \r\n in patterns. So
601 # transform the pattern here. First transform \r\n back to \n, in
602 # case some users of gdb_test_exact already do the right thing.
603 regsub -all "\r\n" $pattern "\n" pattern
604 regsub -all "\n" $pattern "\r\n" pattern
605 if [llength $args]==3 then {
606 set message [lindex $args 2]
607 } else {
608 set message $command
609 }
610
611 return [gdb_test $command $pattern $message]
612 }
613 \f
614 proc gdb_reinitialize_dir { subdir } {
615 global gdb_prompt
616
617 if [is_remote host] {
618 return "";
619 }
620 send_gdb "dir\n"
621 gdb_expect 60 {
622 -re "Reinitialize source path to empty.*y or n. " {
623 send_gdb "y\n"
624 gdb_expect 60 {
625 -re "Source directories searched.*$gdb_prompt $" {
626 send_gdb "dir $subdir\n"
627 gdb_expect 60 {
628 -re "Source directories searched.*$gdb_prompt $" {
629 verbose "Dir set to $subdir"
630 }
631 -re "$gdb_prompt $" {
632 perror "Dir \"$subdir\" failed."
633 }
634 }
635 }
636 -re "$gdb_prompt $" {
637 perror "Dir \"$subdir\" failed."
638 }
639 }
640 }
641 -re "$gdb_prompt $" {
642 perror "Dir \"$subdir\" failed."
643 }
644 }
645 }
646
647 #
648 # gdb_exit -- exit the GDB, killing the target program if necessary
649 #
650 proc default_gdb_exit {} {
651 global GDB
652 global GDBFLAGS
653 global verbose
654 global gdb_spawn_id;
655
656 gdb_stop_suppressing_tests;
657
658 if ![info exists gdb_spawn_id] {
659 return;
660 }
661
662 verbose "Quitting $GDB $GDBFLAGS"
663
664 if { [is_remote host] && [board_info host exists fileid] } {
665 send_gdb "quit\n";
666 gdb_expect 10 {
667 -re "y or n" {
668 send_gdb "y\n";
669 exp_continue;
670 }
671 -re "DOSEXIT code" { }
672 default { }
673 }
674 }
675
676 if ![is_remote host] {
677 remote_close host;
678 }
679 unset gdb_spawn_id
680 }
681
682 #
683 # load a file into the debugger.
684 # return a -1 if anything goes wrong.
685 #
686 proc gdb_file_cmd { arg } {
687 global verbose
688 global loadpath
689 global loadfile
690 global GDB
691 global gdb_prompt
692 upvar timeout timeout
693
694 if [is_remote host] {
695 set arg [remote_download host $arg];
696 if { $arg == "" } {
697 error "download failed"
698 return -1;
699 }
700 }
701
702 send_gdb "file $arg\n"
703 gdb_expect 120 {
704 -re "Reading symbols from.*done.*$gdb_prompt $" {
705 verbose "\t\tLoaded $arg into the $GDB"
706 return 0
707 }
708 -re "has no symbol-table.*$gdb_prompt $" {
709 perror "$arg wasn't compiled with \"-g\""
710 return -1
711 }
712 -re "A program is being debugged already.*Kill it.*y or n. $" {
713 send_gdb "y\n"
714 verbose "\t\tKilling previous program being debugged"
715 exp_continue
716 }
717 -re "Load new symbol table from \".*\".*y or n. $" {
718 send_gdb "y\n"
719 gdb_expect 120 {
720 -re "Reading symbols from.*done.*$gdb_prompt $" {
721 verbose "\t\tLoaded $arg with new symbol table into $GDB"
722 return 0
723 }
724 timeout {
725 perror "(timeout) Couldn't load $arg, other program already loaded."
726 return -1
727 }
728 }
729 }
730 -re "No such file or directory.*$gdb_prompt $" {
731 perror "($arg) No such file or directory\n"
732 return -1
733 }
734 -re "$gdb_prompt $" {
735 perror "couldn't load $arg into $GDB."
736 return -1
737 }
738 timeout {
739 perror "couldn't load $arg into $GDB (timed out)."
740 return -1
741 }
742 eof {
743 # This is an attempt to detect a core dump, but seems not to
744 # work. Perhaps we need to match .* followed by eof, in which
745 # gdb_expect does not seem to have a way to do that.
746 perror "couldn't load $arg into $GDB (end of file)."
747 return -1
748 }
749 }
750 }
751
752 #
753 # start gdb -- start gdb running, default procedure
754 #
755 # When running over NFS, particularly if running many simultaneous
756 # tests on different hosts all using the same server, things can
757 # get really slow. Give gdb at least 3 minutes to start up.
758 #
759 proc default_gdb_start { } {
760 global verbose
761 global GDB
762 global GDBFLAGS
763 global gdb_prompt
764 global timeout
765 global gdb_spawn_id;
766
767 gdb_stop_suppressing_tests;
768
769 verbose "Spawning $GDB -nw $GDBFLAGS"
770
771 if [info exists gdb_spawn_id] {
772 return 0;
773 }
774
775 if ![is_remote host] {
776 if { [which $GDB] == 0 } then {
777 perror "$GDB does not exist."
778 exit 1
779 }
780 }
781 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
782 if { $res < 0 || $res == "" } {
783 perror "Spawning $GDB failed."
784 return 1;
785 }
786 gdb_expect 360 {
787 -re "\[\r\n\]$gdb_prompt $" {
788 verbose "GDB initialized."
789 }
790 -re "$gdb_prompt $" {
791 perror "GDB never initialized."
792 return -1
793 }
794 timeout {
795 perror "(timeout) GDB never initialized after 10 seconds."
796 remote_close host;
797 return -1
798 }
799 }
800 set gdb_spawn_id -1;
801 # force the height to "unlimited", so no pagers get used
802
803 send_gdb "set height 0\n"
804 gdb_expect 10 {
805 -re "$gdb_prompt $" {
806 verbose "Setting height to 0." 2
807 }
808 timeout {
809 warning "Couldn't set the height to 0"
810 }
811 }
812 # force the width to "unlimited", so no wraparound occurs
813 send_gdb "set width 0\n"
814 gdb_expect 10 {
815 -re "$gdb_prompt $" {
816 verbose "Setting width to 0." 2
817 }
818 timeout {
819 warning "Couldn't set the width to 0."
820 }
821 }
822 return 0;
823 }
824
825 # * For crosses, the CHILL runtime doesn't build because it can't find
826 # setjmp.h, stdio.h, etc.
827 # * For AIX (as of 16 Mar 95), (a) there is no language code for
828 # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
829 # does not get along with AIX's too-clever linker.
830 # * On Irix5, there is a bug whereby set of bool, etc., don't get
831 # TYPE_LOW_BOUND for the bool right because force_to_range_type doesn't
832 # work with stub types.
833 # Lots of things seem to fail on the PA, and since it's not a supported
834 # chill target at the moment, don't run the chill tests.
835
836 proc skip_chill_tests {} {
837 if ![info exists do_chill_tests] {
838 return 1;
839 }
840 eval set skip_chill [expr ![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-irix5*"] || [istarget "*-*-irix6*"] || [istarget "alpha-*-osf*"] || [istarget "hppa*-*-*"]]
841 verbose "Skip chill tests is $skip_chill"
842 return $skip_chill
843 }
844
845 # Skip all the tests in the file if you are not on an hppa running
846 # hpux target.
847
848 proc skip_hp_tests {} {
849 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
850 verbose "Skip hp tests is $skip_hp"
851 return $skip_hp
852 }
853
854 proc get_compiler_info {binfile args} {
855 # Create and source the file that provides information about the compiler
856 # used to compile the test case.
857 # Compiler_type can be null or c++. If null we assume c.
858 global srcdir
859 global subdir
860 # These two come from compiler.c.
861 global signed_keyword_not_used
862 global gcc_compiled
863
864 if {![istarget "hppa*-*-hpux*"]} {
865 if { [llength $args] > 0 } {
866 if {$args == "c++"} {
867 if { [gdb_compile "${srcdir}/${subdir}/compiler.cc" "${binfile}.ci" preprocess {}] != "" } {
868 perror "Couldn't make ${binfile}.ci file"
869 return 1;
870 }
871 }
872 } else {
873 if { [gdb_compile "${srcdir}/${subdir}/compiler.c" "${binfile}.ci" preprocess {}] != "" } {
874 perror "Couldn't make ${binfile}.ci file"
875 return 1;
876 }
877 }
878 } else {
879 if { [llength $args] > 0 } {
880 if {$args == "c++"} {
881 if { [eval gdb_preprocess \
882 [list "${srcdir}/${subdir}/compiler.cc" "${binfile}.ci"] \
883 $args] != "" } {
884 perror "Couldn't make ${binfile}.ci file"
885 return 1;
886 }
887 }
888 } else {
889 if { [eval gdb_preprocess \
890 [list "${srcdir}/${subdir}/compiler.c" "${binfile}.ci"] \
891 $args] != "" } {
892 perror "Couldn't make ${binfile}.ci file"
893 return 1;
894 }
895 }
896 }
897
898 uplevel \#0 { set gcc_compiled 0 }
899
900 if { [llength $args] == 0 || $args != "f77" } {
901 source ${binfile}.ci
902 }
903
904 # Most compilers will evaluate comparisons and other boolean
905 # operations to 0 or 1.
906 uplevel \#0 { set true 1 }
907 uplevel \#0 { set false 0 }
908
909 uplevel \#0 { set hp_cc_compiler 0 }
910 uplevel \#0 { set hp_aCC_compiler 0 }
911 uplevel \#0 { set hp_f77_compiler 0 }
912 uplevel \#0 { set hp_f90_compiler 0 }
913 if { !$gcc_compiled && [istarget "hppa*-*-hpux*"] } {
914 # Check for the HP compilers
915 set compiler [lindex [split [get_compiler $args] " "] 0]
916 catch "exec what $compiler" output
917 if [regexp ".*HP aC\\+\\+.*" $output] {
918 uplevel \#0 { set hp_aCC_compiler 1 }
919 # Use of aCC results in boolean results being displayed as
920 # "true" or "false"
921 uplevel \#0 { set true true }
922 uplevel \#0 { set false false }
923 } elseif [regexp ".*HP C Compiler.*" $output] {
924 uplevel \#0 { set hp_cc_compiler 1 }
925 } elseif [regexp ".*HP-UX f77.*" $output] {
926 uplevel \#0 { set hp_f77_compiler 1 }
927 } elseif [regexp ".*HP-UX f90.*" $output] {
928 uplevel \#0 { set hp_f90_compiler 1 }
929 }
930 }
931
932 return 0;
933 }
934
935 proc gdb_preprocess {source dest args} {
936 global CC_FOR_TARGET
937 global CXX_FOR_TARGET
938
939 if { [llength $args] == 0 } {
940 set which_compiler "c"
941 } else {
942 if { $args =="c++" } {
943 set which_compiler "c++"
944 } else {
945 perror "Unknown compiler type supplied to gdb_preprocess"
946 return 1;
947 }
948 }
949
950 if [info exists CC_FOR_TARGET] {
951 if { $which_compiler == "c"} {
952 set compiler $CC_FOR_TARGET;
953 }
954 }
955
956 if [info exists CXX_FOR_TARGET] {
957 if { $which_compiler == "c++"} {
958 set compiler $CXX_FOR_TARGET;
959 }
960 }
961
962 if { ![info exists compiler] } {
963 if { $which_compiler == "c" } {
964 if {[info exists CC]} {
965 set compiler $CC;
966 }
967 }
968 if { $which_compiler == "c++" } {
969 if {[info exists CXX]} {
970 set compiler $CXX;
971 }
972 }
973 if {![info exists compiler]} {
974 set compiler [board_info [target_info name] compiler];
975 if { $compiler == "" } {
976 puts "default_target_compile: No compiler to compile with";
977 return "default_target_compile: No compiler to compile with";
978 }
979 }
980 }
981
982 set cmdline "$compiler -E $source > $dest"
983
984 verbose "Invoking $compiler -E $source > $dest"
985 verbose -log "Executing on local host: $cmdline" 2
986 set status [catch "exec ${cmdline}" exec_output]
987
988 set result [prune_warnings $exec_output]
989 regsub "\[\r\n\]*$" "$result" "" result;
990 regsub "^\[\r\n\]*" "$result" "" result;
991 if { $result != "" } {
992 clone_output "gdb compile failed, $result"
993 }
994 return $result;
995 }
996
997 proc gdb_compile {source dest type options} {
998 global GDB_TESTCASE_OPTIONS;
999
1000 if [target_info exists gdb_stub] {
1001 set options2 { "additional_flags=-Dusestubs" }
1002 lappend options "libs=[target_info gdb_stub]";
1003 set options [concat $options2 $options]
1004 }
1005 if [target_info exists is_vxworks] {
1006 set options2 { "additional_flags=-Dvxworks" }
1007 lappend options "libs=[target_info gdb_stub]";
1008 set options [concat $options2 $options]
1009 }
1010 if [info exists GDB_TESTCASE_OPTIONS] {
1011 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1012 }
1013 verbose "options are $options"
1014 verbose "source is $source $dest $type $options"
1015
1016 set result [target_compile $source $dest $type $options];
1017 regsub "\[\r\n\]*$" "$result" "" result;
1018 regsub "^\[\r\n\]*" "$result" "" result;
1019 if { $result != "" } {
1020 clone_output "gdb compile failed, $result"
1021 }
1022 return $result;
1023 }
1024
1025 proc send_gdb { string } {
1026 global suppress_flag;
1027 if { $suppress_flag } {
1028 return "suppressed";
1029 }
1030 return [remote_send host "$string"];
1031 }
1032
1033 #
1034 #
1035
1036 proc gdb_expect { args } {
1037 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
1038 set gtimeout [lindex $args 0];
1039 set expcode [list [lindex $args 1]];
1040 } else {
1041 upvar timeout timeout;
1042
1043 set expcode $args;
1044 if [target_info exists gdb,timeout] {
1045 if [info exists timeout] {
1046 if { $timeout < [target_info gdb,timeout] } {
1047 set gtimeout [target_info gdb,timeout];
1048 } else {
1049 set gtimeout $timeout;
1050 }
1051 } else {
1052 set gtimeout [target_info gdb,timeout];
1053 }
1054 }
1055
1056 if ![info exists gtimeout] {
1057 global timeout;
1058 if [info exists timeout] {
1059 set gtimeout $timeout;
1060 } else {
1061 # Eeeeew.
1062 set gtimeout 60;
1063 }
1064 }
1065 }
1066 global suppress_flag;
1067 global remote_suppress_flag;
1068 if [info exists remote_suppress_flag] {
1069 set old_val $remote_suppress_flag;
1070 }
1071 if [info exists suppress_flag] {
1072 if { $suppress_flag } {
1073 set remote_suppress_flag 1;
1074 }
1075 }
1076 set code [catch {uplevel remote_expect host $gtimeout $expcode} string];
1077 if [info exists old_val] {
1078 set remote_suppress_flag $old_val;
1079 } else {
1080 if [info exists remote_suppress_flag] {
1081 unset remote_suppress_flag;
1082 }
1083 }
1084
1085 if {$code == 1} {
1086 global errorInfo errorCode;
1087
1088 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1089 } elseif {$code == 2} {
1090 return -code return $string
1091 } elseif {$code == 3} {
1092 return
1093 } elseif {$code > 4} {
1094 return -code $code $string
1095 }
1096 }
1097
1098 #
1099 # Check for long sequence of output by parts.
1100 # TEST: is the test message.
1101 # SENTINEL: Is the terminal pattern indicating that output has finished.
1102 # LIST: is the sequence of outputs to match.
1103 # If the sentinel is recognized early, it is considered an error.
1104 #
1105 proc gdb_expect_list {test sentinal list} {
1106 global gdb_prompt
1107 set index 0
1108 set ok 1
1109 while { ${index} < [llength ${list}] } {
1110 set pattern [lindex ${list} ${index}]
1111 set index [expr ${index} + 1]
1112 if { ${index} == [llength ${list}] } {
1113 if { ${ok} } {
1114 gdb_expect {
1115 -re "${pattern}${sentinal}" {
1116 pass "${test}, pattern ${index} + sentinal"
1117 }
1118 timeout {
1119 fail "${test}, pattern ${index} + sentinal (timeout)"
1120 set ok 0
1121 }
1122 }
1123 } else {
1124 fail "${test}, pattern ${index} + sentinal"
1125 }
1126 } else {
1127 if { ${ok} } {
1128 gdb_expect {
1129 -re "${pattern}" {
1130 pass "${test}, pattern ${index}"
1131 }
1132 -re "${sentinal}" {
1133 fail "${test}, pattern ${index}"
1134 set ok 0
1135 }
1136 timeout {
1137 fail "${test}, pattern ${index} (timeout)"
1138 set ok 0
1139 }
1140 }
1141 } else {
1142 fail "${test}, pattern ${index}"
1143 }
1144 }
1145 }
1146 }
1147
1148 #
1149 #
1150 proc gdb_suppress_entire_file { reason } {
1151 global suppress_flag;
1152
1153 warning "$reason\n";
1154 set suppress_flag -1;
1155 }
1156
1157 #
1158 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
1159 # gdb_expect to fail immediately (until the next call to
1160 # gdb_stop_suppressing_tests).
1161 #
1162 proc gdb_suppress_tests { args } {
1163 global suppress_flag;
1164
1165 return; # fnf - disable pending review of results where
1166 # testsuite ran better without this
1167 incr suppress_flag;
1168
1169 if { $suppress_flag == 1 } {
1170 if { [llength $args] > 0 } {
1171 warning "[lindex $args 0]\n";
1172 } else {
1173 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1174 }
1175 }
1176 }
1177
1178 #
1179 # Clear suppress_flag.
1180 #
1181 proc gdb_stop_suppressing_tests { } {
1182 global suppress_flag;
1183
1184 if [info exists suppress_flag] {
1185 if { $suppress_flag > 0 } {
1186 set suppress_flag 0;
1187 clone_output "Tests restarted.\n";
1188 }
1189 } else {
1190 set suppress_flag 0;
1191 }
1192 }
1193
1194 proc gdb_clear_suppressed { } {
1195 global suppress_flag;
1196
1197 set suppress_flag 0;
1198 }
1199
1200 proc gdb_start { } {
1201 default_gdb_start
1202 }
1203
1204 proc gdb_exit { } {
1205 catch default_gdb_exit
1206 }
1207
1208 #
1209 # gdb_load -- load a file into the debugger.
1210 # return a -1 if anything goes wrong.
1211 #
1212 proc gdb_load { arg } {
1213 return [gdb_file_cmd $arg]
1214 }
1215
1216 proc gdb_continue { function } {
1217 global decimal
1218
1219 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
1220 }
1221
1222 proc default_gdb_init { args } {
1223 gdb_clear_suppressed;
1224
1225 # Uh, this is lame. Really, really, really lame. But there's this *one*
1226 # testcase that will fail in random places if we don't increase this.
1227 match_max -d 20000
1228
1229 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
1230 if { [llength $args] > 0 } {
1231 global pf_prefix
1232
1233 set file [lindex $args 0];
1234
1235 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
1236 }
1237 global gdb_prompt;
1238 if [target_info exists gdb_prompt] {
1239 set gdb_prompt [target_info gdb_prompt];
1240 } else {
1241 set gdb_prompt "\\(gdb\\)"
1242 }
1243 }
1244
1245 proc gdb_init { args } {
1246 return [eval default_gdb_init $args];
1247 }
1248
1249 proc gdb_finish { } {
1250 gdb_exit;
1251 }
1252
1253 global debug_format
1254 set debug_format "unknown"
1255
1256 # Run the gdb command "info source" and extract the debugging format
1257 # information from the output and save it in debug_format.
1258
1259 proc get_debug_format { } {
1260 global gdb_prompt
1261 global verbose
1262 global expect_out
1263 global debug_format
1264
1265 set debug_format "unknown"
1266 send_gdb "info source\n"
1267 gdb_expect 10 {
1268 -re "Compiled with (.*) debugging format.\r\n$gdb_prompt $" {
1269 set debug_format $expect_out(1,string)
1270 verbose "debug format is $debug_format"
1271 return 1;
1272 }
1273 -re "No current source file.\r\n$gdb_prompt $" {
1274 perror "get_debug_format used when no current source file"
1275 return 0;
1276 }
1277 -re "$gdb_prompt $" {
1278 warning "couldn't check debug format (no valid response)."
1279 return 1;
1280 }
1281 timeout {
1282 warning "couldn't check debug format (timed out)."
1283 return 1;
1284 }
1285 }
1286 }
1287
1288 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
1289 # COFF, stabs, etc). If that format matches the format that the
1290 # current test was compiled with, then the next test is expected to
1291 # fail for any target. Returns 1 if the next test or set of tests is
1292 # expected to fail, 0 otherwise (or if it is unknown). Must have
1293 # previously called get_debug_format.
1294
1295 proc setup_xfail_format { format } {
1296 global debug_format
1297
1298 if [string match $debug_format $format] then {
1299 setup_xfail "*-*-*"
1300 return 1;
1301 }
1302 return 0
1303 }
1304
1305 proc gdb_step_for_stub { } {
1306 global gdb_prompt;
1307
1308 if ![target_info exists gdb,use_breakpoint_for_stub] {
1309 if [target_info exists gdb_stub_step_command] {
1310 set command [target_info gdb_stub_step_command];
1311 } else {
1312 set command "step";
1313 }
1314 send_gdb "${command}\n";
1315 set tries 0;
1316 gdb_expect 60 {
1317 -re "(main.* at |.*in .*start).*$gdb_prompt" {
1318 return;
1319 }
1320 -re ".*$gdb_prompt" {
1321 incr tries;
1322 if { $tries == 5 } {
1323 fail "stepping out of breakpoint function";
1324 return;
1325 }
1326 send_gdb "${command}\n";
1327 exp_continue;
1328 }
1329 default {
1330 fail "stepping out of breakpoint function";
1331 return;
1332 }
1333 }
1334 }
1335 send_gdb "where\n";
1336 gdb_expect {
1337 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
1338 set file $expect_out(1,string);
1339 set linenum [expr $expect_out(2,string) + 1];
1340 set breakplace "${file}:${linenum}";
1341 }
1342 default {}
1343 }
1344 send_gdb "break ${breakplace}\n";
1345 gdb_expect 60 {
1346 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
1347 set breakpoint $expect_out(1,string);
1348 }
1349 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
1350 set breakpoint $expect_out(1,string);
1351 }
1352 default {}
1353 }
1354 send_gdb "continue\n";
1355 gdb_expect 60 {
1356 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
1357 gdb_test "delete $breakpoint" ".*" "";
1358 return;
1359 }
1360 default {}
1361 }
1362 }
1363
1364 ### gdb_get_line_number TEXT [FILE]
1365 ###
1366 ### Search the source file FILE, and return the line number of a line
1367 ### containing TEXT. Use this function instead of hard-coding line
1368 ### numbers into your test script.
1369 ###
1370 ### Specifically, this function uses GDB's "search" command to search
1371 ### FILE for the first line containing TEXT, and returns its line
1372 ### number. Thus, FILE must be a source file, compiled into the
1373 ### executable you are running. If omitted, FILE defaults to the
1374 ### value of the global variable `srcfile'; most test scripts set
1375 ### `srcfile' appropriately at the top anyway.
1376 ###
1377 ### Use this function to keep your test scripts independent of the
1378 ### exact line numbering of the source file. Don't write:
1379 ###
1380 ### send_gdb "break 20"
1381 ###
1382 ### This means that if anyone ever edits your test's source file,
1383 ### your test could break. Instead, put a comment like this on the
1384 ### source file line you want to break at:
1385 ###
1386 ### /* breakpoint spot: frotz.exp: test name */
1387 ###
1388 ### and then write, in your test script (which we assume is named
1389 ### frotz.exp):
1390 ###
1391 ### send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
1392 ###
1393 ### (Yes, Tcl knows how to handle the nested quotes and brackets.
1394 ### Try this:
1395 ### $ tclsh
1396 ### % puts "foo [lindex "bar baz" 1]"
1397 ### foo baz
1398 ### %
1399 ### Tcl is quite clever, for a little stringy language.)
1400
1401 proc gdb_get_line_number {text {file /omitted/}} {
1402 global gdb_prompt;
1403 global srcfile;
1404
1405 if {! [string compare $file /omitted/]} {
1406 set file $srcfile
1407 }
1408
1409 set result -1;
1410 gdb_test "list ${file}:1,1" ".*" ""
1411 send_gdb "search ${text}\n"
1412 gdb_expect {
1413 -re "\[\r\n\]+(\[0-9\]+)\[ \t\].*${text}.*$gdb_prompt $" {
1414 set result $expect_out(1,string)
1415 }
1416 -re ".*$gdb_prompt $" {
1417 fail "find line number containing \"${text}\""
1418 }
1419 timeout {
1420 fail "find line number containing \"${text}\" (timeout)"
1421 }
1422 }
1423 return $result;
1424 }
1425
1426 # gdb_continue_to_end:
1427 # The case where the target uses stubs has to be handled specially. If a
1428 # stub is used, we set a breakpoint at exit because we cannot rely on
1429 # exit() behavior of a remote target.
1430 #
1431 # mssg is the error message that gets printed.
1432
1433 proc gdb_continue_to_end {mssg} {
1434 if [target_info exists use_gdb_stub] {
1435 if {![gdb_breakpoint "exit"]} {
1436 return 0
1437 }
1438 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
1439 "continue until exit at $mssg"
1440 } else {
1441 # Continue until we exit. Should not stop again.
1442 # Don't bother to check the output of the program, that may be
1443 # extremely tough for some remote systems.
1444 gdb_test "continue"\
1445 "Continuing.\[\r\n0-9\]+Program exited normally\\..*"\
1446 "continue until exit at $mssg"
1447 }
1448 }
1449
1450 proc rerun_to_main {} {
1451 global gdb_prompt
1452
1453 if [target_info exists use_gdb_stub] {
1454 gdb_run_cmd
1455 gdb_expect {
1456 -re ".*Breakpoint .*main .*$gdb_prompt $"\
1457 {pass "rerun to main" ; return 0}
1458 -re "$gdb_prompt $"\
1459 {fail "rerun to main" ; return 0}
1460 timeout {fail "(timeout) rerun to main" ; return 0}
1461 }
1462 } else {
1463 send_gdb "run\n"
1464 gdb_expect {
1465 -re "Starting program.*$gdb_prompt $"\
1466 {pass "rerun to main" ; return 0}
1467 -re "$gdb_prompt $"\
1468 {fail "rerun to main" ; return 0}
1469 timeout {fail "(timeout) rerun to main" ; return 0}
1470 }
1471 }
1472 }
1473
This page took 0.096497 seconds and 3 git commands to generate.