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