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