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