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