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