import gdb-1999-06-28 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
085dd6e6
JM
66### Only procedures should come after this point.
67
c906108c
SS
68#
69# gdb_version -- extract and print the version number of GDB
70#
71proc 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
92proc gdb_version { } {
93 return [default_gdb_version];
94}
95
96#
97# gdb_unload -- unload a file if one is loaded
98#
99
100proc 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
131proc 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#
164proc 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
242proc 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
264proc 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#
303proc 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
7a292a7a 318
c906108c
SS
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#
335proc 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 }
9e0b60a8 419 -re "Ending remote debugging.*$gdb_prompt $" {
c906108c
SS
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 }
9e0b60a8 437 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
c906108c 438 perror "Undefined command \"$command\"."
9e0b60a8 439 fail "$message"
c906108c
SS
440 set result 1
441 }
442 -re "Ambiguous command.*$gdb_prompt $" {
443 perror "\"$command\" is not a unique command name."
9e0b60a8 444 fail "$message"
c906108c
SS
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."
9e0b60a8 474 fail "$message"
c906108c
SS
475 }
476 -re "\\(y or n\\) " {
477 send_gdb "n\n"
478 perror "Got interactive prompt."
9e0b60a8 479 fail "$message"
c906108c
SS
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."
9e0b60a8 490 fail "$message"
c906108c
SS
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
508proc 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
571proc 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
580proc 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
614proc 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#
650proc 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#
686proc 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#
759proc 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
836proc 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
7a292a7a
SS
845# Skip all the tests in the file if you are not on an hppa running
846# hpux target.
847
848proc skip_hp_tests {} {
849 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
c906108c
SS
850 verbose "Skip hp tests is $skip_hp"
851 return $skip_hp
852}
853
854proc 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
085dd6e6
JM
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
c906108c
SS
932 return 0;
933}
934
935proc 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
997proc 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
1025proc 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
1036proc 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
085dd6e6
JM
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#
1105proc gdb_expect_list {test sentinal list} {
1106 global gdb_prompt
1107 set index 0
1108 while { ${index} >= 0 && ${index} < [llength ${list}] } {
1109 set pattern [lindex ${list} ${index}]
1110 set index [expr ${index} + 1]
1111 if { ${index} == [llength ${list}] } {
1112 gdb_expect {
1113 -re "${pattern}${sentinal}" {
1114 pass "${test} (sentinal)"
1115 }
1116 timeout {
1117 fail "(timeout on sentinal) ${test}"
1118 set index -1
1119 }
1120 }
1121 } else {
1122 gdb_expect {
1123 -re "${pattern}" {
1124 pass "${test} (line ${index})"
1125 }
1126 -re "${sentinal}" {
1127 fail "${test} (line ${index})"
1128 set index -1
1129 }
1130 timeout {
1131 fail "(timeout on line ${index}) ${test}"
1132 set index -1
1133 }
1134 }
1135 }
1136 }
1137 if { ${index} >= 0 } {
1138 pass "${test}"
1139 }
1140}
1141
1142#
1143#
c906108c
SS
1144proc gdb_suppress_entire_file { reason } {
1145 global suppress_flag;
1146
1147 warning "$reason\n";
1148 set suppress_flag -1;
1149}
1150
1151#
1152# Set suppress_flag, which will cause all subsequent calls to send_gdb and
1153# gdb_expect to fail immediately (until the next call to
1154# gdb_stop_suppressing_tests).
1155#
1156proc gdb_suppress_tests { args } {
1157 global suppress_flag;
1158
1159 return; # fnf - disable pending review of results where
1160 # testsuite ran better without this
1161 incr suppress_flag;
1162
1163 if { $suppress_flag == 1 } {
1164 if { [llength $args] > 0 } {
1165 warning "[lindex $args 0]\n";
1166 } else {
1167 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1168 }
1169 }
1170}
1171
1172#
1173# Clear suppress_flag.
1174#
1175proc gdb_stop_suppressing_tests { } {
1176 global suppress_flag;
1177
1178 if [info exists suppress_flag] {
1179 if { $suppress_flag > 0 } {
1180 set suppress_flag 0;
1181 clone_output "Tests restarted.\n";
1182 }
1183 } else {
1184 set suppress_flag 0;
1185 }
1186}
1187
1188proc gdb_clear_suppressed { } {
1189 global suppress_flag;
1190
1191 set suppress_flag 0;
1192}
1193
1194proc gdb_start { } {
1195 default_gdb_start
1196}
1197
1198proc gdb_exit { } {
1199 catch default_gdb_exit
1200}
1201
1202#
1203# gdb_load -- load a file into the debugger.
1204# return a -1 if anything goes wrong.
1205#
1206proc gdb_load { arg } {
1207 return [gdb_file_cmd $arg]
1208}
1209
1210proc gdb_continue { function } {
1211 global decimal
1212
1213 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
1214}
1215
1216proc default_gdb_init { args } {
1217 gdb_clear_suppressed;
1218
1219 # Uh, this is lame. Really, really, really lame. But there's this *one*
1220 # testcase that will fail in random places if we don't increase this.
1221 match_max -d 20000
1222
1223 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
1224 if { [llength $args] > 0 } {
1225 global pf_prefix
1226
1227 set file [lindex $args 0];
1228
1229 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
1230 }
1231 global gdb_prompt;
1232 if [target_info exists gdb_prompt] {
1233 set gdb_prompt [target_info gdb_prompt];
1234 } else {
1235 set gdb_prompt "\\(gdb\\)"
1236 }
1237}
1238
1239proc gdb_init { args } {
1240 return [eval default_gdb_init $args];
1241}
1242
1243proc gdb_finish { } {
1244 gdb_exit;
1245}
1246
1247global debug_format
7a292a7a 1248set debug_format "unknown"
c906108c
SS
1249
1250# Run the gdb command "info source" and extract the debugging format
1251# information from the output and save it in debug_format.
1252
1253proc get_debug_format { } {
1254 global gdb_prompt
1255 global verbose
1256 global expect_out
1257 global debug_format
1258
1259 set debug_format "unknown"
1260 send_gdb "info source\n"
1261 gdb_expect 10 {
1262 -re "Compiled with (.*) debugging format.\r\n$gdb_prompt $" {
1263 set debug_format $expect_out(1,string)
1264 verbose "debug format is $debug_format"
1265 return 1;
1266 }
1267 -re "No current source file.\r\n$gdb_prompt $" {
1268 perror "get_debug_format used when no current source file"
1269 return 0;
1270 }
1271 -re "$gdb_prompt $" {
1272 warning "couldn't check debug format (no valid response)."
1273 return 1;
1274 }
1275 timeout {
1276 warning "couldn't check debug format (timed out)."
1277 return 1;
1278 }
1279 }
1280}
1281
1282# Like setup_xfail, but takes the name of a debug format (DWARF 1,
1283# COFF, stabs, etc). If that format matches the format that the
1284# current test was compiled with, then the next test is expected to
1285# fail for any target. Returns 1 if the next test or set of tests is
1286# expected to fail, 0 otherwise (or if it is unknown). Must have
1287# previously called get_debug_format.
1288
1289proc setup_xfail_format { format } {
1290 global debug_format
1291
1292 if [string match $debug_format $format] then {
1293 setup_xfail "*-*-*"
1294 return 1;
1295 }
1296 return 0
1297}
1298
1299proc gdb_step_for_stub { } {
1300 global gdb_prompt;
1301
1302 if ![target_info exists gdb,use_breakpoint_for_stub] {
1303 if [target_info exists gdb_stub_step_command] {
1304 set command [target_info gdb_stub_step_command];
1305 } else {
1306 set command "step";
1307 }
1308 send_gdb "${command}\n";
1309 set tries 0;
1310 gdb_expect 60 {
1311 -re "(main.* at |.*in .*start).*$gdb_prompt" {
1312 return;
1313 }
1314 -re ".*$gdb_prompt" {
1315 incr tries;
1316 if { $tries == 5 } {
1317 fail "stepping out of breakpoint function";
1318 return;
1319 }
1320 send_gdb "${command}\n";
1321 exp_continue;
1322 }
1323 default {
1324 fail "stepping out of breakpoint function";
1325 return;
1326 }
1327 }
1328 }
1329 send_gdb "where\n";
1330 gdb_expect {
1331 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
1332 set file $expect_out(1,string);
1333 set linenum [expr $expect_out(2,string) + 1];
1334 set breakplace "${file}:${linenum}";
1335 }
1336 default {}
1337 }
1338 send_gdb "break ${breakplace}\n";
1339 gdb_expect 60 {
1340 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
1341 set breakpoint $expect_out(1,string);
1342 }
1343 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
1344 set breakpoint $expect_out(1,string);
1345 }
1346 default {}
1347 }
1348 send_gdb "continue\n";
1349 gdb_expect 60 {
1350 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
1351 gdb_test "delete $breakpoint" ".*" "";
1352 return;
1353 }
1354 default {}
1355 }
1356}
1357
1358### gdb_get_line_number TEXT [FILE]
1359###
1360### Search the source file FILE, and return the line number of a line
1361### containing TEXT. Use this function instead of hard-coding line
1362### numbers into your test script.
1363###
1364### Specifically, this function uses GDB's "search" command to search
1365### FILE for the first line containing TEXT, and returns its line
1366### number. Thus, FILE must be a source file, compiled into the
1367### executable you are running. If omitted, FILE defaults to the
1368### value of the global variable `srcfile'; most test scripts set
1369### `srcfile' appropriately at the top anyway.
1370###
1371### Use this function to keep your test scripts independent of the
1372### exact line numbering of the source file. Don't write:
1373###
1374### send_gdb "break 20"
1375###
1376### This means that if anyone ever edits your test's source file,
1377### your test could break. Instead, put a comment like this on the
1378### source file line you want to break at:
1379###
1380### /* breakpoint spot: frotz.exp: test name */
1381###
1382### and then write, in your test script (which we assume is named
1383### frotz.exp):
1384###
1385### send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
1386###
1387### (Yes, Tcl knows how to handle the nested quotes and brackets.
1388### Try this:
1389### $ tclsh
1390### % puts "foo [lindex "bar baz" 1]"
1391### foo baz
1392### %
1393### Tcl is quite clever, for a little stringy language.)
1394
1395proc gdb_get_line_number {text {file /omitted/}} {
1396 global gdb_prompt;
1397 global srcfile;
1398
1399 if {! [string compare $file /omitted/]} {
1400 set file $srcfile
1401 }
1402
1403 set result -1;
1404 gdb_test "list ${file}:1,1" ".*" ""
1405 send_gdb "search ${text}\n"
1406 gdb_expect {
1407 -re "\[\r\n\]+(\[0-9\]+)\[ \t\].*${text}.*$gdb_prompt $" {
1408 set result $expect_out(1,string)
1409 }
1410 -re ".*$gdb_prompt $" {
1411 fail "find line number containing \"${text}\""
1412 }
1413 timeout {
1414 fail "find line number containing \"${text}\" (timeout)"
1415 }
1416 }
1417 return $result;
1418}
1419
7a292a7a
SS
1420# gdb_continue_to_end:
1421# The case where the target uses stubs has to be handled specially. If a
1422# stub is used, we set a breakpoint at exit because we cannot rely on
1423# exit() behavior of a remote target.
1424#
1425# mssg is the error message that gets printed.
1426
1427proc gdb_continue_to_end {mssg} {
1428 if [target_info exists use_gdb_stub] {
1429 if {![gdb_breakpoint "exit"]} {
1430 return 0
1431 }
1432 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
1433 "continue until exit at $mssg"
1434 } else {
1435 # Continue until we exit. Should not stop again.
1436 # Don't bother to check the output of the program, that may be
1437 # extremely tough for some remote systems.
1438 gdb_test "continue"\
1439 "Continuing.\[\r\n0-9\]+Program exited normally\\..*"\
1440 "continue until exit at $mssg"
1441 }
1442}
1443
1444proc rerun_to_main {} {
1445 global gdb_prompt
1446
1447 if [target_info exists use_gdb_stub] {
1448 gdb_run_cmd
1449 gdb_expect {
1450 -re ".*Breakpoint .*main .*$gdb_prompt $"\
1451 {pass "rerun to main" ; return 0}
1452 -re "$gdb_prompt $"\
1453 {fail "rerun to main" ; return 0}
1454 timeout {fail "(timeout) rerun to main" ; return 0}
1455 }
1456 } else {
1457 send_gdb "run\n"
1458 gdb_expect {
1459 -re "Starting program.*$gdb_prompt $"\
1460 {pass "rerun to main" ; return 0}
1461 -re "$gdb_prompt $"\
1462 {fail "rerun to main" ; return 0}
1463 timeout {fail "(timeout) rerun to main" ; return 0}
1464 }
1465 }
1466}
c906108c 1467
This page took 0.093248 seconds and 4 git commands to generate.