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