* amd64-tdep.c (amd64_init_frame_cache): New function.
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
CommitLineData
6aba47ca
DJ
1# Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2# 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
c906108c
SS
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
c906108c
SS
18# This file was written by Fred Fish. (fnf@cygnus.com)
19
20# Generic gdb subroutines that should work for any target. If these
21# need to be modified for any target, it can be done with a variable
22# or by passing arguments.
23
97c3f1f3
JK
24if {$tool == ""} {
25 # Tests would fail, logs on get_compiler_info() would be missing.
26 send_error "`site.exp' not found, run `make site.exp'!\n"
27 exit 2
28}
29
c906108c
SS
30load_lib libgloss.exp
31
32global GDB
c906108c
SS
33
34if [info exists TOOL_EXECUTABLE] {
35 set GDB $TOOL_EXECUTABLE;
36}
37if ![info exists GDB] {
38 if ![is_remote host] {
39 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
40 } else {
41 set GDB [transform gdb];
42 }
43}
44verbose "using GDB = $GDB" 2
45
46global GDBFLAGS
47if ![info exists GDBFLAGS] {
48 set GDBFLAGS "-nx"
49}
50verbose "using GDBFLAGS = $GDBFLAGS" 2
51
9e0b60a8
JM
52# The variable gdb_prompt is a regexp which matches the gdb prompt.
53# Set it if it is not already set.
c906108c 54global gdb_prompt
9e0b60a8 55if ![info exists gdb_prompt] then {
c906108c
SS
56 set gdb_prompt "\[(\]gdb\[)\]"
57}
58
6006a3a1
BR
59# The variable fullname_syntax_POSIX is a regexp which matches a POSIX
60# absolute path ie. /foo/
61set fullname_syntax_POSIX "/.*/"
62# The variable fullname_syntax_UNC is a regexp which matches a Windows
63# UNC path ie. \\D\foo\
64set fullname_syntax_UNC {\\\\[^\\]+\\.+\\}
65# The variable fullname_syntax_DOS_CASE is a regexp which matches a
66# particular DOS case that GDB most likely will output
67# ie. \foo\, but don't match \\.*\
68set fullname_syntax_DOS_CASE {\\[^\\].*\\}
69# The variable fullname_syntax_DOS is a regexp which matches a DOS path
70# ie. a:\foo\ && a:foo\
71set fullname_syntax_DOS {[a-zA-Z]:.*\\}
72# The variable fullname_syntax is a regexp which matches what GDB considers
73# an absolute path. It is currently debatable if the Windows style paths
74# d:foo and \abc should be considered valid as an absolute path.
75# Also, the purpse of this regexp is not to recognize a well formed
76# absolute path, but to say with certainty that a path is absolute.
77set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
78
93076499
ND
79# Needed for some tests under Cygwin.
80global EXEEXT
81global env
82
83if ![info exists env(EXEEXT)] {
84 set EXEEXT ""
85} else {
86 set EXEEXT $env(EXEEXT)
87}
88
085dd6e6
JM
89### Only procedures should come after this point.
90
c906108c
SS
91#
92# gdb_version -- extract and print the version number of GDB
93#
94proc default_gdb_version {} {
95 global GDB
96 global GDBFLAGS
97 global gdb_prompt
98 set fileid [open "gdb_cmd" w];
99 puts $fileid "q";
100 close $fileid;
101 set cmdfile [remote_download host "gdb_cmd"];
102 set output [remote_exec host "$GDB -nw --command $cmdfile"]
103 remote_file build delete "gdb_cmd";
104 remote_file host delete "$cmdfile";
105 set tmp [lindex $output 1];
106 set version ""
107 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
108 if ![is_remote host] {
109 clone_output "[which $GDB] version $version $GDBFLAGS\n"
110 } else {
111 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
112 }
113}
114
115proc gdb_version { } {
116 return [default_gdb_version];
117}
118
119#
120# gdb_unload -- unload a file if one is loaded
121#
122
123proc gdb_unload {} {
124 global verbose
125 global GDB
126 global gdb_prompt
127 send_gdb "file\n"
128 gdb_expect 60 {
129 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
130 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
131 -re "A program is being debugged already..*Kill it.*y or n. $"\
132 { send_gdb "y\n"
133 verbose "\t\tKilling previous program being debugged"
134 exp_continue
135 }
136 -re "Discard symbol table from .*y or n.*$" {
137 send_gdb "y\n"
138 exp_continue
139 }
140 -re "$gdb_prompt $" {}
141 timeout {
142 perror "couldn't unload file in $GDB (timed out)."
143 return -1
144 }
145 }
146}
147
148# Many of the tests depend on setting breakpoints at various places and
149# running until that breakpoint is reached. At times, we want to start
150# with a clean-slate with respect to breakpoints, so this utility proc
151# lets us do this without duplicating this code everywhere.
152#
153
154proc delete_breakpoints {} {
155 global gdb_prompt
156
a0b3c4fd
JM
157 # we need a larger timeout value here or this thing just confuses
158 # itself. May need a better implementation if possible. - guo
159 #
c906108c 160 send_gdb "delete breakpoints\n"
a0b3c4fd 161 gdb_expect 100 {
c906108c
SS
162 -re "Delete all breakpoints.*y or n.*$" {
163 send_gdb "y\n";
164 exp_continue
165 }
166 -re "$gdb_prompt $" { # This happens if there were no breakpoints
167 }
168 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
169 }
170 send_gdb "info breakpoints\n"
a0b3c4fd 171 gdb_expect 100 {
c906108c
SS
172 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
173 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
174 -re "Delete all breakpoints.*or n.*$" {
175 send_gdb "y\n";
176 exp_continue
177 }
178 timeout { perror "info breakpoints (timeout)" ; return }
179 }
180}
181
182
183#
184# Generic run command.
185#
186# The second pattern below matches up to the first newline *only*.
187# Using ``.*$'' could swallow up output that we attempt to match
188# elsewhere.
189#
190proc gdb_run_cmd {args} {
191 global gdb_prompt
192
193 if [target_info exists gdb_init_command] {
194 send_gdb "[target_info gdb_init_command]\n";
195 gdb_expect 30 {
196 -re "$gdb_prompt $" { }
197 default {
198 perror "gdb_init_command for target failed";
199 return;
200 }
201 }
202 }
203
204 if [target_info exists use_gdb_stub] {
205 if [target_info exists gdb,do_reload_on_run] {
917317f4
JM
206 # Specifying no file, defaults to the executable
207 # currently being debugged.
2db8e78e 208 if { [gdb_load ""] != 0 } {
917317f4
JM
209 return;
210 }
c906108c
SS
211 send_gdb "continue\n";
212 gdb_expect 60 {
213 -re "Continu\[^\r\n\]*\[\r\n\]" {}
214 default {}
215 }
216 return;
217 }
218
219 if [target_info exists gdb,start_symbol] {
220 set start [target_info gdb,start_symbol];
221 } else {
222 set start "start";
223 }
224 send_gdb "jump *$start\n"
917317f4
JM
225 set start_attempt 1;
226 while { $start_attempt } {
227 # Cap (re)start attempts at three to ensure that this loop
228 # always eventually fails. Don't worry about trying to be
229 # clever and not send a command when it has failed.
230 if [expr $start_attempt > 3] {
231 perror "Jump to start() failed (retry count exceeded)";
c906108c
SS
232 return;
233 }
917317f4
JM
234 set start_attempt [expr $start_attempt + 1];
235 gdb_expect 30 {
236 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
237 set start_attempt 0;
238 }
239 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
240 perror "Can't find start symbol to run in gdb_run";
241 return;
242 }
243 -re "No symbol \"start\" in current.*$gdb_prompt $" {
244 send_gdb "jump *_start\n";
245 }
246 -re "No symbol.*context.*$gdb_prompt $" {
247 set start_attempt 0;
248 }
249 -re "Line.* Jump anyway.*y or n. $" {
250 send_gdb "y\n"
251 }
252 -re "The program is not being run.*$gdb_prompt $" {
2db8e78e 253 if { [gdb_load ""] != 0 } {
917317f4
JM
254 return;
255 }
256 send_gdb "jump *$start\n";
257 }
258 timeout {
259 perror "Jump to start() failed (timeout)";
260 return
261 }
c906108c 262 }
c906108c
SS
263 }
264 if [target_info exists gdb_stub] {
265 gdb_expect 60 {
266 -re "$gdb_prompt $" {
267 send_gdb "continue\n"
268 }
269 }
270 }
271 return
272 }
83f66e8f
DJ
273
274 if [target_info exists gdb,do_reload_on_run] {
2db8e78e 275 if { [gdb_load ""] != 0 } {
83f66e8f
DJ
276 return;
277 }
278 }
c906108c
SS
279 send_gdb "run $args\n"
280# This doesn't work quite right yet.
281 gdb_expect 60 {
282 -re "The program .* has been started already.*y or n. $" {
283 send_gdb "y\n"
284 exp_continue
285 }
bbb88ebf
UW
286 # Use -notransfer here so that test cases (like chng-sym.exp)
287 # may test for additional start-up messages.
288 -notransfer -re "Starting program: \[^\r\n\]*" {}
c906108c
SS
289 }
290}
291
78a1a894
DJ
292# Set a breakpoint at FUNCTION. If there is an additional argument it is
293# a list of options; the only currently supported option is allow-pending.
294
295proc gdb_breakpoint { function args } {
c906108c
SS
296 global gdb_prompt
297 global decimal
298
78a1a894
DJ
299 set pending_response n
300 if {[lsearch -exact [lindex $args 0] allow-pending] != -1} {
301 set pending_response y
302 }
303
c906108c
SS
304 send_gdb "break $function\n"
305 # The first two regexps are what we get with -g, the third is without -g.
306 gdb_expect 30 {
307 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
308 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
309 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
78a1a894
DJ
310 -re "Breakpoint \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
311 if {$pending_response == "n"} {
312 fail "setting breakpoint at $function"
313 return 0
314 }
315 }
9f27c604 316 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
78a1a894 317 send_gdb "$pending_response\n"
14b1a056 318 exp_continue
18fe2033 319 }
c906108c
SS
320 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
321 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
322 }
323 return 1;
324}
325
326# Set breakpoint at function and run gdb until it breaks there.
327# Since this is the only breakpoint that will be set, if it stops
328# at a breakpoint, we will assume it is the one we want. We can't
329# just compare to "function" because it might be a fully qualified,
78a1a894
DJ
330# single quoted C++ function specifier. If there's an additional argument,
331# pass it to gdb_breakpoint.
c906108c 332
78a1a894 333proc runto { function args } {
c906108c
SS
334 global gdb_prompt
335 global decimal
336
337 delete_breakpoints
338
78a1a894 339 if ![gdb_breakpoint $function [lindex $args 0]] {
c906108c
SS
340 return 0;
341 }
342
343 gdb_run_cmd
344
345 # the "at foo.c:36" output we get with -g.
346 # the "in func" output we get without -g.
347 gdb_expect 30 {
348 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
349 return 1
350 }
351 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
352 return 1
353 }
354 -re "$gdb_prompt $" {
355 fail "running to $function in runto"
356 return 0
357 }
358 timeout {
359 fail "running to $function in runto (timeout)"
360 return 0
361 }
362 }
363 return 1
364}
365
366#
367# runto_main -- ask gdb to run until we hit a breakpoint at main.
368# The case where the target uses stubs has to be handled
369# specially--if it uses stubs, assuming we hit
370# breakpoint() and just step out of the function.
371#
372proc runto_main { } {
373 global gdb_prompt
374 global decimal
375
376 if ![target_info exists gdb_stub] {
377 return [runto main]
378 }
379
380 delete_breakpoints
381
382 gdb_step_for_stub;
383
384 return 1
385}
386
7a292a7a 387
4ce44c66
JM
388### Continue, and expect to hit a breakpoint.
389### Report a pass or fail, depending on whether it seems to have
390### worked. Use NAME as part of the test name; each call to
391### continue_to_breakpoint should use a NAME which is unique within
392### that test file.
393proc gdb_continue_to_breakpoint {name} {
394 global gdb_prompt
395 set full_name "continue to breakpoint: $name"
396
397 send_gdb "continue\n"
398 gdb_expect {
399 -re "Breakpoint .* at .*\r\n$gdb_prompt $" {
400 pass $full_name
401 }
402 -re ".*$gdb_prompt $" {
403 fail $full_name
404 }
405 timeout {
406 fail "$full_name (timeout)"
407 }
408 }
409}
410
411
039cf96d
AC
412# gdb_internal_error_resync:
413#
414# Answer the questions GDB asks after it reports an internal error
415# until we get back to a GDB prompt. Decline to quit the debugging
416# session, and decline to create a core file. Return non-zero if the
417# resync succeeds.
418#
419# This procedure just answers whatever questions come up until it sees
420# a GDB prompt; it doesn't require you to have matched the input up to
421# any specific point. However, it only answers questions it sees in
422# the output itself, so if you've matched a question, you had better
423# answer it yourself before calling this.
424#
425# You can use this function thus:
426#
427# gdb_expect {
428# ...
429# -re ".*A problem internal to GDB has been detected" {
430# gdb_internal_error_resync
431# }
432# ...
433# }
434#
435proc gdb_internal_error_resync {} {
436 global gdb_prompt
437
438 set count 0
439 while {$count < 10} {
440 gdb_expect {
441 -re "Quit this debugging session\\? \\(y or n\\) $" {
442 send_gdb "n\n"
443 incr count
444 }
445 -re "Create a core file of GDB\\? \\(y or n\\) $" {
446 send_gdb "n\n"
447 incr count
448 }
449 -re "$gdb_prompt $" {
450 # We're resynchronized.
451 return 1
452 }
453 timeout {
454 perror "Could not resync from internal error (timeout)"
455 return 0
456 }
457 }
458 }
2b211c59
AC
459 perror "Could not resync from internal error (resync count exceeded)"
460 return 0
039cf96d
AC
461}
462
4ce44c66 463
2307bd6a 464# gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
8dbfb380 465# Send a command to gdb; test the result.
c906108c
SS
466#
467# COMMAND is the command to execute, send to GDB with send_gdb. If
468# this is the null string no command is sent.
2307bd6a
DJ
469# MESSAGE is a message to be printed with the built-in failure patterns
470# if one of them matches. If MESSAGE is empty COMMAND will be used.
471# EXPECT_ARGUMENTS will be fed to expect in addition to the standard
472# patterns. Pattern elements will be evaluated in the caller's
473# context; action elements will be executed in the caller's context.
474# Unlike patterns for gdb_test, these patterns should generally include
475# the final newline and prompt.
c906108c
SS
476#
477# Returns:
2307bd6a
DJ
478# 1 if the test failed, according to a built-in failure pattern
479# 0 if only user-supplied patterns matched
c906108c
SS
480# -1 if there was an internal error.
481#
d422fe19
AC
482# You can use this function thus:
483#
484# gdb_test_multiple "print foo" "test foo" {
485# -re "expected output 1" {
486# pass "print foo"
487# }
488# -re "expected output 2" {
489# fail "print foo"
490# }
491# }
492#
493# The standard patterns, such as "Program exited..." and "A problem
494# ...", all being implicitly appended to that list.
495#
2307bd6a 496proc gdb_test_multiple { command message user_code } {
c906108c
SS
497 global verbose
498 global gdb_prompt
499 global GDB
500 upvar timeout timeout
c47cebdb 501 upvar expect_out expect_out
c906108c 502
2307bd6a
DJ
503 if { $message == "" } {
504 set message $command
c906108c 505 }
c906108c 506
2307bd6a
DJ
507 # TCL/EXPECT WART ALERT
508 # Expect does something very strange when it receives a single braced
509 # argument. It splits it along word separators and performs substitutions.
510 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
511 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
512 # double-quoted list item, "\[ab\]" is just a long way of representing
513 # "[ab]", because the backslashes will be removed by lindex.
514
515 # Unfortunately, there appears to be no easy way to duplicate the splitting
516 # that expect will do from within TCL. And many places make use of the
517 # "\[0-9\]" construct, so we need to support that; and some places make use
518 # of the "[func]" construct, so we need to support that too. In order to
519 # get this right we have to substitute quoted list elements differently
520 # from braced list elements.
521
522 # We do this roughly the same way that Expect does it. We have to use two
523 # lists, because if we leave unquoted newlines in the argument to uplevel
524 # they'll be treated as command separators, and if we escape newlines
525 # we mangle newlines inside of command blocks. This assumes that the
526 # input doesn't contain a pattern which contains actual embedded newlines
527 # at this point!
528
529 regsub -all {\n} ${user_code} { } subst_code
530 set subst_code [uplevel list $subst_code]
531
532 set processed_code ""
533 set patterns ""
534 set expecting_action 0
535 foreach item $user_code subst_item $subst_code {
536 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
537 lappend processed_code $item
538 continue
539 }
540 if {$item == "-indices" || $item == "-re" || $item == "-ex"} {
541 lappend processed_code $item
542 continue
543 }
544 if { $expecting_action } {
545 lappend processed_code "uplevel [list $item]"
546 set expecting_action 0
547 # Cosmetic, no effect on the list.
548 append processed_code "\n"
549 continue
550 }
551 set expecting_action 1
552 lappend processed_code $subst_item
553 if {$patterns != ""} {
554 append patterns "; "
555 }
556 append patterns "\"$subst_item\""
c906108c
SS
557 }
558
2307bd6a
DJ
559 # Also purely cosmetic.
560 regsub -all {\r} $patterns {\\r} patterns
561 regsub -all {\n} $patterns {\\n} patterns
562
c906108c
SS
563 if $verbose>2 then {
564 send_user "Sending \"$command\" to gdb\n"
2307bd6a 565 send_user "Looking to match \"$patterns\"\n"
c906108c
SS
566 send_user "Message is \"$message\"\n"
567 }
568
569 set result -1
570 set string "${command}\n";
571 if { $command != "" } {
572 while { "$string" != "" } {
573 set foo [string first "\n" "$string"];
574 set len [string length "$string"];
575 if { $foo < [expr $len - 1] } {
576 set str [string range "$string" 0 $foo];
577 if { [send_gdb "$str"] != "" } {
578 global suppress_flag;
579
580 if { ! $suppress_flag } {
581 perror "Couldn't send $command to GDB.";
582 }
583 fail "$message";
584 return $result;
585 }
a0b3c4fd
JM
586 # since we're checking if each line of the multi-line
587 # command are 'accepted' by GDB here,
588 # we need to set -notransfer expect option so that
589 # command output is not lost for pattern matching
590 # - guo
5f279fa6
DJ
591 gdb_expect 2 {
592 -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
593 timeout { verbose "partial: timeout" 3 }
c906108c
SS
594 }
595 set string [string range "$string" [expr $foo + 1] end];
596 } else {
597 break;
598 }
599 }
600 if { "$string" != "" } {
601 if { [send_gdb "$string"] != "" } {
602 global suppress_flag;
603
604 if { ! $suppress_flag } {
605 perror "Couldn't send $command to GDB.";
606 }
607 fail "$message";
608 return $result;
609 }
610 }
611 }
612
9d2e1bab
ND
613 if [target_info exists gdb,timeout] {
614 set tmt [target_info gdb,timeout];
c906108c 615 } else {
c906108c
SS
616 if [info exists timeout] {
617 set tmt $timeout;
618 } else {
9d2e1bab
ND
619 global timeout;
620 if [info exists timeout] {
621 set tmt $timeout;
622 } else {
623 set tmt 60;
624 }
c906108c
SS
625 }
626 }
2307bd6a
DJ
627
628 set code {
039cf96d
AC
629 -re ".*A problem internal to GDB has been detected" {
630 fail "$message (GDB internal error)"
631 gdb_internal_error_resync
632 }
c906108c
SS
633 -re "\\*\\*\\* DOSEXIT code.*" {
634 if { $message != "" } {
635 fail "$message";
636 }
637 gdb_suppress_entire_file "GDB died";
2307bd6a 638 set result -1;
c906108c 639 }
9e0b60a8 640 -re "Ending remote debugging.*$gdb_prompt $" {
c906108c
SS
641 if ![isnative] then {
642 warning "Can`t communicate to remote target."
643 }
644 gdb_exit
645 gdb_start
646 set result -1
647 }
2307bd6a
DJ
648 }
649 append code $processed_code
650 append code {
9e0b60a8 651 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
c906108c 652 perror "Undefined command \"$command\"."
9e0b60a8 653 fail "$message"
c906108c
SS
654 set result 1
655 }
656 -re "Ambiguous command.*$gdb_prompt $" {
657 perror "\"$command\" is not a unique command name."
9e0b60a8 658 fail "$message"
c906108c
SS
659 set result 1
660 }
661 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
662 if ![string match "" $message] then {
ed4c619a 663 set errmsg "$message (the program exited)"
c906108c 664 } else {
ed4c619a 665 set errmsg "$command (the program exited)"
c906108c
SS
666 }
667 fail "$errmsg"
2307bd6a 668 set result -1
cb9a9d3e
MS
669 }
670 -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" {
671 if ![string match "" $message] then {
ed4c619a 672 set errmsg "$message (the program exited)"
cb9a9d3e 673 } else {
ed4c619a 674 set errmsg "$command (the program exited)"
cb9a9d3e
MS
675 }
676 fail "$errmsg"
2307bd6a 677 set result -1
c906108c
SS
678 }
679 -re "The program is not being run.*$gdb_prompt $" {
680 if ![string match "" $message] then {
ed4c619a 681 set errmsg "$message (the program is no longer running)"
c906108c 682 } else {
ed4c619a 683 set errmsg "$command (the program is no longer running)"
c906108c
SS
684 }
685 fail "$errmsg"
2307bd6a 686 set result -1
c906108c 687 }
734b8fe8 688 -re "\r\n$gdb_prompt $" {
c906108c
SS
689 if ![string match "" $message] then {
690 fail "$message"
691 }
692 set result 1
693 }
694 "<return>" {
695 send_gdb "\n"
696 perror "Window too small."
9e0b60a8 697 fail "$message"
2307bd6a 698 set result -1
c906108c
SS
699 }
700 -re "\\(y or n\\) " {
701 send_gdb "n\n"
702 perror "Got interactive prompt."
9e0b60a8 703 fail "$message"
2307bd6a 704 set result -1
c906108c
SS
705 }
706 eof {
707 perror "Process no longer exists"
708 if { $message != "" } {
709 fail "$message"
710 }
711 return -1
712 }
713 full_buffer {
714 perror "internal buffer is full."
9e0b60a8 715 fail "$message"
2307bd6a 716 set result -1
c906108c
SS
717 }
718 timeout {
719 if ![string match "" $message] then {
720 fail "$message (timeout)"
721 }
722 set result 1
723 }
724 }
2307bd6a
DJ
725
726 set result 0
04f6ecf2
DJ
727 set code [catch {gdb_expect $tmt $code} string]
728 if {$code == 1} {
729 global errorInfo errorCode;
730 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
731 } elseif {$code == 2} {
732 return -code return $string
733 } elseif {$code == 3} {
734 return
735 } elseif {$code > 4} {
736 return -code $code $string
737 }
c906108c
SS
738 return $result
739}
2307bd6a
DJ
740
741# gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
742# Send a command to gdb; test the result.
743#
744# COMMAND is the command to execute, send to GDB with send_gdb. If
745# this is the null string no command is sent.
746# PATTERN is the pattern to match for a PASS, and must NOT include
747# the \r\n sequence immediately before the gdb prompt.
748# MESSAGE is an optional message to be printed. If this is
749# omitted, then the pass/fail messages use the command string as the
750# message. (If this is the empty string, then sometimes we don't
751# call pass or fail at all; I don't understand this at all.)
752# QUESTION is a question GDB may ask in response to COMMAND, like
753# "are you sure?"
754# RESPONSE is the response to send if QUESTION appears.
755#
756# Returns:
757# 1 if the test failed,
758# 0 if the test passes,
759# -1 if there was an internal error.
760#
761proc gdb_test { args } {
762 global verbose
763 global gdb_prompt
764 global GDB
765 upvar timeout timeout
766
767 if [llength $args]>2 then {
768 set message [lindex $args 2]
769 } else {
770 set message [lindex $args 0]
771 }
772 set command [lindex $args 0]
773 set pattern [lindex $args 1]
774
775 if [llength $args]==5 {
776 set question_string [lindex $args 3];
777 set response_string [lindex $args 4];
778 } else {
779 set question_string "^FOOBAR$"
780 }
781
782 return [gdb_test_multiple $command $message {
783 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
784 if ![string match "" $message] then {
785 pass "$message"
786 }
787 }
788 -re "(${question_string})$" {
789 send_gdb "$response_string\n";
790 exp_continue;
791 }
792 }]
793}
c906108c
SS
794\f
795# Test that a command gives an error. For pass or fail, return
796# a 1 to indicate that more tests can proceed. However a timeout
797# is a serious error, generates a special fail message, and causes
798# a 0 to be returned to indicate that more tests are likely to fail
799# as well.
800
801proc test_print_reject { args } {
802 global gdb_prompt
803 global verbose
804
805 if [llength $args]==2 then {
806 set expectthis [lindex $args 1]
807 } else {
808 set expectthis "should never match this bogus string"
809 }
810 set sendthis [lindex $args 0]
811 if $verbose>2 then {
812 send_user "Sending \"$sendthis\" to gdb\n"
813 send_user "Looking to match \"$expectthis\"\n"
814 }
815 send_gdb "$sendthis\n"
816 #FIXME: Should add timeout as parameter.
817 gdb_expect {
818 -re "A .* in expression.*\\.*$gdb_prompt $" {
819 pass "reject $sendthis"
820 return 1
821 }
822 -re "Invalid syntax in expression.*$gdb_prompt $" {
823 pass "reject $sendthis"
824 return 1
825 }
826 -re "Junk after end of expression.*$gdb_prompt $" {
827 pass "reject $sendthis"
828 return 1
829 }
830 -re "Invalid number.*$gdb_prompt $" {
831 pass "reject $sendthis"
832 return 1
833 }
834 -re "Invalid character constant.*$gdb_prompt $" {
835 pass "reject $sendthis"
836 return 1
837 }
838 -re "No symbol table is loaded.*$gdb_prompt $" {
839 pass "reject $sendthis"
840 return 1
841 }
842 -re "No symbol .* in current context.*$gdb_prompt $" {
843 pass "reject $sendthis"
844 return 1
845 }
c4b7bc2b
JB
846 -re "Unmatched single quote.*$gdb_prompt $" {
847 pass "reject $sendthis"
848 return 1
849 }
850 -re "A character constant must contain at least one character.*$gdb_prompt $" {
851 pass "reject $sendthis"
852 return 1
853 }
c906108c
SS
854 -re "$expectthis.*$gdb_prompt $" {
855 pass "reject $sendthis"
856 return 1
857 }
858 -re ".*$gdb_prompt $" {
859 fail "reject $sendthis"
860 return 1
861 }
862 default {
863 fail "reject $sendthis (eof or timeout)"
864 return 0
865 }
866 }
867}
868\f
869# Given an input string, adds backslashes as needed to create a
870# regexp that will match the string.
871
872proc string_to_regexp {str} {
873 set result $str
39fb8e9e 874 regsub -all {[]*+.|()^$\[\\]} $str {\\&} result
c906108c
SS
875 return $result
876}
877
878# Same as gdb_test, but the second parameter is not a regexp,
879# but a string that must match exactly.
880
881proc gdb_test_exact { args } {
882 upvar timeout timeout
883
884 set command [lindex $args 0]
885
886 # This applies a special meaning to a null string pattern. Without
887 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
888 # messages from commands that should have no output except a new
889 # prompt. With this, only results of a null string will match a null
890 # string pattern.
891
892 set pattern [lindex $args 1]
893 if [string match $pattern ""] {
894 set pattern [string_to_regexp [lindex $args 0]]
895 } else {
896 set pattern [string_to_regexp [lindex $args 1]]
897 }
898
899 # It is most natural to write the pattern argument with only
900 # embedded \n's, especially if you are trying to avoid Tcl quoting
901 # problems. But gdb_expect really wants to see \r\n in patterns. So
902 # transform the pattern here. First transform \r\n back to \n, in
903 # case some users of gdb_test_exact already do the right thing.
904 regsub -all "\r\n" $pattern "\n" pattern
905 regsub -all "\n" $pattern "\r\n" pattern
906 if [llength $args]==3 then {
907 set message [lindex $args 2]
908 } else {
909 set message $command
910 }
911
912 return [gdb_test $command $pattern $message]
913}
914\f
915proc gdb_reinitialize_dir { subdir } {
916 global gdb_prompt
917
918 if [is_remote host] {
919 return "";
920 }
921 send_gdb "dir\n"
922 gdb_expect 60 {
923 -re "Reinitialize source path to empty.*y or n. " {
924 send_gdb "y\n"
925 gdb_expect 60 {
926 -re "Source directories searched.*$gdb_prompt $" {
927 send_gdb "dir $subdir\n"
928 gdb_expect 60 {
929 -re "Source directories searched.*$gdb_prompt $" {
930 verbose "Dir set to $subdir"
931 }
932 -re "$gdb_prompt $" {
933 perror "Dir \"$subdir\" failed."
934 }
935 }
936 }
937 -re "$gdb_prompt $" {
938 perror "Dir \"$subdir\" failed."
939 }
940 }
941 }
942 -re "$gdb_prompt $" {
943 perror "Dir \"$subdir\" failed."
944 }
945 }
946}
947
948#
949# gdb_exit -- exit the GDB, killing the target program if necessary
950#
951proc default_gdb_exit {} {
952 global GDB
953 global GDBFLAGS
954 global verbose
955 global gdb_spawn_id;
956
957 gdb_stop_suppressing_tests;
958
959 if ![info exists gdb_spawn_id] {
960 return;
961 }
962
963 verbose "Quitting $GDB $GDBFLAGS"
964
965 if { [is_remote host] && [board_info host exists fileid] } {
966 send_gdb "quit\n";
967 gdb_expect 10 {
968 -re "y or n" {
969 send_gdb "y\n";
970 exp_continue;
971 }
972 -re "DOSEXIT code" { }
973 default { }
974 }
975 }
976
977 if ![is_remote host] {
978 remote_close host;
979 }
980 unset gdb_spawn_id
981}
982
3e3ffd2b 983# Load a file into the debugger.
2db8e78e 984# The return value is 0 for success, -1 for failure.
c906108c 985#
2db8e78e
MC
986# This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
987# to one of these values:
3e3ffd2b 988#
2db8e78e
MC
989# debug file was loaded successfully and has debug information
990# nodebug file was loaded successfully and has no debug information
991# fail file was not loaded
c906108c 992#
2db8e78e
MC
993# I tried returning this information as part of the return value,
994# but ran into a mess because of the many re-implementations of
995# gdb_load in config/*.exp.
3e3ffd2b 996#
2db8e78e
MC
997# TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
998# this if they can get more information set.
3e3ffd2b 999
c906108c 1000proc gdb_file_cmd { arg } {
3e3ffd2b 1001 global gdb_prompt
c906108c 1002 global verbose
c906108c 1003 global GDB
c906108c 1004
2db8e78e
MC
1005 # Set whether debug info was found.
1006 # Default to "fail".
1007 global gdb_file_cmd_debug_info
1008 set gdb_file_cmd_debug_info "fail"
1009
c906108c 1010 if [is_remote host] {
3e3ffd2b 1011 set arg [remote_download host $arg]
c906108c 1012 if { $arg == "" } {
2db8e78e
MC
1013 perror "download failed"
1014 return -1
c906108c
SS
1015 }
1016 }
1017
4c42eaff
DJ
1018 # The file command used to kill the remote target. For the benefit
1019 # of the testsuite, preserve this behavior.
1020 send_gdb "kill\n"
1021 gdb_expect 120 {
1022 -re "Kill the program being debugged. .y or n. $" {
1023 send_gdb "y\n"
1024 verbose "\t\tKilling previous program being debugged"
1025 exp_continue
1026 }
1027 -re "$gdb_prompt $" {
1028 # OK.
1029 }
1030 }
1031
c906108c
SS
1032 send_gdb "file $arg\n"
1033 gdb_expect 120 {
3e3ffd2b
MC
1034 -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" {
1035 verbose "\t\tLoaded $arg into the $GDB with no debugging symbols"
2db8e78e
MC
1036 set gdb_file_cmd_debug_info "nodebug"
1037 return 0
3e3ffd2b 1038 }
c906108c
SS
1039 -re "Reading symbols from.*done.*$gdb_prompt $" {
1040 verbose "\t\tLoaded $arg into the $GDB"
2db8e78e
MC
1041 set gdb_file_cmd_debug_info "debug"
1042 return 0
c906108c 1043 }
c906108c
SS
1044 -re "Load new symbol table from \".*\".*y or n. $" {
1045 send_gdb "y\n"
1046 gdb_expect 120 {
1047 -re "Reading symbols from.*done.*$gdb_prompt $" {
1048 verbose "\t\tLoaded $arg with new symbol table into $GDB"
2db8e78e
MC
1049 set gdb_file_cmd_debug_info "debug"
1050 return 0
c906108c
SS
1051 }
1052 timeout {
2db8e78e
MC
1053 perror "(timeout) Couldn't load $arg, other program already loaded."
1054 return -1
c906108c
SS
1055 }
1056 }
1057 }
1058 -re "No such file or directory.*$gdb_prompt $" {
2db8e78e
MC
1059 perror "($arg) No such file or directory"
1060 return -1
c906108c
SS
1061 }
1062 -re "$gdb_prompt $" {
2db8e78e
MC
1063 perror "couldn't load $arg into $GDB."
1064 return -1
c906108c
SS
1065 }
1066 timeout {
2db8e78e
MC
1067 perror "couldn't load $arg into $GDB (timed out)."
1068 return -1
c906108c
SS
1069 }
1070 eof {
1071 # This is an attempt to detect a core dump, but seems not to
1072 # work. Perhaps we need to match .* followed by eof, in which
1073 # gdb_expect does not seem to have a way to do that.
2db8e78e
MC
1074 perror "couldn't load $arg into $GDB (end of file)."
1075 return -1
c906108c
SS
1076 }
1077 }
1078}
1079
1080#
1081# start gdb -- start gdb running, default procedure
1082#
1083# When running over NFS, particularly if running many simultaneous
1084# tests on different hosts all using the same server, things can
1085# get really slow. Give gdb at least 3 minutes to start up.
1086#
1087proc default_gdb_start { } {
1088 global verbose
1089 global GDB
1090 global GDBFLAGS
1091 global gdb_prompt
1092 global timeout
1093 global gdb_spawn_id;
1094
1095 gdb_stop_suppressing_tests;
1096
1097 verbose "Spawning $GDB -nw $GDBFLAGS"
1098
1099 if [info exists gdb_spawn_id] {
1100 return 0;
1101 }
1102
1103 if ![is_remote host] {
1104 if { [which $GDB] == 0 } then {
1105 perror "$GDB does not exist."
1106 exit 1
1107 }
1108 }
1109 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
1110 if { $res < 0 || $res == "" } {
1111 perror "Spawning $GDB failed."
1112 return 1;
1113 }
1114 gdb_expect 360 {
1115 -re "\[\r\n\]$gdb_prompt $" {
1116 verbose "GDB initialized."
1117 }
1118 -re "$gdb_prompt $" {
1119 perror "GDB never initialized."
1120 return -1
1121 }
1122 timeout {
1123 perror "(timeout) GDB never initialized after 10 seconds."
1124 remote_close host;
1125 return -1
1126 }
1127 }
1128 set gdb_spawn_id -1;
1129 # force the height to "unlimited", so no pagers get used
1130
1131 send_gdb "set height 0\n"
1132 gdb_expect 10 {
1133 -re "$gdb_prompt $" {
1134 verbose "Setting height to 0." 2
1135 }
1136 timeout {
1137 warning "Couldn't set the height to 0"
1138 }
1139 }
1140 # force the width to "unlimited", so no wraparound occurs
1141 send_gdb "set width 0\n"
1142 gdb_expect 10 {
1143 -re "$gdb_prompt $" {
1144 verbose "Setting width to 0." 2
1145 }
1146 timeout {
1147 warning "Couldn't set the width to 0."
1148 }
1149 }
1150 return 0;
1151}
1152
d4f3574e
SS
1153# Return a 1 for configurations for which we don't even want to try to
1154# test C++.
1155
1156proc skip_cplus_tests {} {
1157 if { [istarget "d10v-*-*"] } {
1158 return 1
1159 }
1160 if { [istarget "h8300-*-*"] } {
1161 return 1
1162 }
81d2cbae 1163
1146c7f1
SC
1164 # The C++ IO streams are too large for HC11/HC12 and are thus not
1165 # available. The gdb C++ tests use them and don't compile.
1166 if { [istarget "m6811-*-*"] } {
1167 return 1
1168 }
1169 if { [istarget "m6812-*-*"] } {
1170 return 1
1171 }
d4f3574e
SS
1172 return 0
1173}
1174
89a237cb
MC
1175# Return a 1 if I don't even want to try to test FORTRAN.
1176
1177proc skip_fortran_tests {} {
1178 return 0
1179}
1180
3c95e6af
PG
1181# Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1182# 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1183
1184proc skip_altivec_tests {} {
1185 global skip_vmx_tests_saved
1186 global srcdir subdir gdb_prompt
1187
1188 # Use the cached value, if it exists.
1189 set me "skip_altivec_tests"
1190 if [info exists skip_vmx_tests_saved] {
1191 verbose "$me: returning saved $skip_vmx_tests_saved" 2
1192 return $skip_vmx_tests_saved
1193 }
1194
1195 # Some simulators are known to not support VMX instructions.
1196 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1197 verbose "$me: target known to not support VMX, returning 1" 2
476308bf 1198 return [set skip_vmx_tests_saved 1]
3c95e6af
PG
1199 }
1200
1201 # Make sure we have a compiler that understands altivec.
fc91c6c2 1202 set compile_flags {debug nowarnings}
3c95e6af
PG
1203 if [get_compiler_info not-used] {
1204 warning "Could not get compiler info"
1205 return 1
1206 }
1207 if [test_compiler_info gcc*] {
1208 set compile_flags "$compile_flags additional_flags=-maltivec"
1209 } elseif [test_compiler_info xlc*] {
1210 set compile_flags "$compile_flags additional_flags=-qaltivec"
1211 } else {
1212 verbose "Could not compile with altivec support, returning 1" 2
1213 return 1
1214 }
1215
1216 # Set up, compile, and execute a test program containing VMX instructions.
1217 # Include the current process ID in the file names to prevent conflicts
1218 # with invocations for multiple testsuites.
1219 set src vmx[pid].c
1220 set exe vmx[pid].x
1221
1222 set f [open $src "w"]
1223 puts $f "int main() {"
1224 puts $f "#ifdef __MACH__"
1225 puts $f " asm volatile (\"vor v0,v0,v0\");"
1226 puts $f "#else"
1227 puts $f " asm volatile (\"vor 0,0,0\");"
1228 puts $f "#endif"
1229 puts $f " return 0; }"
1230 close $f
1231
1232 verbose "$me: compiling testfile $src" 2
1233 set lines [gdb_compile $src $exe executable $compile_flags]
1234 file delete $src
1235
1236 if ![string match "" $lines] then {
1237 verbose "$me: testfile compilation failed, returning 1" 2
1238 return [set skip_vmx_tests_saved 1]
1239 }
1240
1241 # No error message, compilation succeeded so now run it via gdb.
1242
1243 gdb_exit
1244 gdb_start
1245 gdb_reinitialize_dir $srcdir/$subdir
1246 gdb_load "$exe"
1247 gdb_run_cmd
1248 gdb_expect {
1249 -re ".*Illegal instruction.*${gdb_prompt} $" {
1250 verbose -log "\n$me altivec hardware not detected"
1251 set skip_vmx_tests_saved 1
1252 }
1253 -re ".*Program exited normally.*${gdb_prompt} $" {
1254 verbose -log "\n$me: altivec hardware detected"
1255 set skip_vmx_tests_saved 0
1256 }
1257 default {
1258 warning "\n$me: default case taken"
1259 set skip_vmx_tests_saved 1
1260 }
1261 }
1262 gdb_exit
1263 remote_file build delete $exe
1264
1265 verbose "$me: returning $skip_vmx_tests_saved" 2
1266 return $skip_vmx_tests_saved
1267}
1268
7a292a7a
SS
1269# Skip all the tests in the file if you are not on an hppa running
1270# hpux target.
1271
1272proc skip_hp_tests {} {
1273 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
c906108c
SS
1274 verbose "Skip hp tests is $skip_hp"
1275 return $skip_hp
1276}
1277
94b8e876
MC
1278set compiler_info "unknown"
1279set gcc_compiled 0
1280set hp_cc_compiler 0
1281set hp_aCC_compiler 0
94b8e876
MC
1282
1283# Figure out what compiler I am using.
1284#
1285# BINFILE is a "compiler information" output file. This implementation
1286# does not use BINFILE.
1287#
1288# ARGS can be empty or "C++". If empty, "C" is assumed.
1289#
1290# There are several ways to do this, with various problems.
1291#
1292# [ gdb_compile -E $ifile -o $binfile.ci ]
1293# source $binfile.ci
1294#
1295# Single Unix Spec v3 says that "-E -o ..." together are not
1296# specified. And in fact, the native compiler on hp-ux 11 (among
1297# others) does not work with "-E -o ...". Most targets used to do
1298# this, and it mostly worked, because it works with gcc.
1299#
1300# [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1301# source $binfile.ci
1302#
1303# This avoids the problem with -E and -o together. This almost works
1304# if the build machine is the same as the host machine, which is
1305# usually true of the targets which are not gcc. But this code does
1306# not figure which compiler to call, and it always ends up using the C
1307# compiler. Not good for setting hp_aCC_compiler. Targets
1308# hppa*-*-hpux* and mips*-*-irix* used to do this.
1309#
1310# [ gdb_compile -E $ifile > $binfile.ci ]
1311# source $binfile.ci
1312#
1313# dejagnu target_compile says that it supports output redirection,
1314# but the code is completely different from the normal path and I
1315# don't want to sweep the mines from that path. So I didn't even try
1316# this.
1317#
1318# set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1319# eval $cppout
1320#
1321# I actually do this for all targets now. gdb_compile runs the right
1322# compiler, and TCL captures the output, and I eval the output.
1323#
1324# Unfortunately, expect logs the output of the command as it goes by,
1325# and dejagnu helpfully prints a second copy of it right afterwards.
1326# So I turn off expect logging for a moment.
1327#
1328# [ gdb_compile $ifile $ciexe_file executable $args ]
1329# [ remote_exec $ciexe_file ]
1330# [ source $ci_file.out ]
1331#
1332# I could give up on -E and just do this.
1333# I didn't get desperate enough to try this.
1334#
1335# -- chastain 2004-01-06
853d6e5b 1336
c906108c 1337proc get_compiler_info {binfile args} {
94b8e876 1338 # For compiler.c and compiler.cc
c906108c 1339 global srcdir
94b8e876
MC
1340
1341 # I am going to play with the log to keep noise out.
1342 global outdir
1343 global tool
1344
1345 # These come from compiler.c or compiler.cc
853d6e5b 1346 global compiler_info
4f70a4c9
MC
1347
1348 # Legacy global data symbols.
94b8e876
MC
1349 global gcc_compiled
1350 global hp_cc_compiler
1351 global hp_aCC_compiler
c906108c 1352
94b8e876
MC
1353 # Choose which file to preprocess.
1354 set ifile "${srcdir}/lib/compiler.c"
1355 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1356 set ifile "${srcdir}/lib/compiler.cc"
c906108c 1357 }
085dd6e6 1358
94b8e876
MC
1359 # Run $ifile through the right preprocessor.
1360 # Toggle gdb.log to keep the compiler output out of the log.
1361 log_file
1362 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1363 log_file -a "$outdir/$tool.log"
1364
4f70a4c9
MC
1365 # Eval the output.
1366 set unknown 0
94b8e876 1367 foreach cppline [ split "$cppout" "\n" ] {
4f70a4c9
MC
1368 if { [ regexp "^#" "$cppline" ] } {
1369 # line marker
1370 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1371 # blank line
1372 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
1373 # eval this line
1374 verbose "get_compiler_info: $cppline" 2
1375 eval "$cppline"
1376 } else {
1377 # unknown line
1378 verbose -log "get_compiler_info: $cppline"
1379 set unknown 1
94b8e876 1380 }
085dd6e6 1381 }
4f70a4c9
MC
1382
1383 # Reset to unknown compiler if any diagnostics happened.
1384 if { $unknown } {
1385 set compiler_info "unknown"
4f70a4c9
MC
1386 }
1387
1388 # Set the legacy symbols.
1389 set gcc_compiled 0
1390 set hp_cc_compiler 0
1391 set hp_aCC_compiler 0
1392 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
1393 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
1394 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
1395 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
1396 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
1397 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
1398 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
1399
1400 # Log what happened.
94b8e876 1401 verbose -log "get_compiler_info: $compiler_info"
085dd6e6
JM
1402
1403 # Most compilers will evaluate comparisons and other boolean
1404 # operations to 0 or 1.
1405 uplevel \#0 { set true 1 }
1406 uplevel \#0 { set false 0 }
1407
94b8e876
MC
1408 # Use of aCC results in boolean results being displayed as
1409 # "true" or "false"
1410 if { $hp_aCC_compiler } {
1411 uplevel \#0 { set true true }
1412 uplevel \#0 { set false false }
085dd6e6
JM
1413 }
1414
c906108c
SS
1415 return 0;
1416}
1417
9b593790 1418proc test_compiler_info { {compiler ""} } {
853d6e5b 1419 global compiler_info
6e87504d
PG
1420
1421 # if no arg, return the compiler_info string
1422
1423 if [string match "" $compiler] {
1424 if [info exists compiler_info] {
1425 return $compiler_info
1426 } else {
1427 perror "No compiler info found."
1428 }
1429 }
1430
853d6e5b
AC
1431 return [string match $compiler $compiler_info]
1432}
1433
f1c47eb2
MS
1434set gdb_wrapper_initialized 0
1435
1436proc gdb_wrapper_init { args } {
1437 global gdb_wrapper_initialized;
1438 global gdb_wrapper_file;
1439 global gdb_wrapper_flags;
1440
1441 if { $gdb_wrapper_initialized == 1 } { return; }
1442
1443 if {[target_info exists needs_status_wrapper] && \
277254ba 1444 [target_info needs_status_wrapper] != "0"} {
f1c47eb2
MS
1445 set result [build_wrapper "testglue.o"];
1446 if { $result != "" } {
1447 set gdb_wrapper_file [lindex $result 0];
1448 set gdb_wrapper_flags [lindex $result 1];
1449 } else {
1450 warning "Status wrapper failed to build."
1451 }
1452 }
1453 set gdb_wrapper_initialized 1
1454}
1455
c906108c
SS
1456proc gdb_compile {source dest type options} {
1457 global GDB_TESTCASE_OPTIONS;
f1c47eb2
MS
1458 global gdb_wrapper_file;
1459 global gdb_wrapper_flags;
1460 global gdb_wrapper_initialized;
c906108c 1461
57bf0e56
DJ
1462 # Add platform-specific options if a shared library was specified using
1463 # "shlib=librarypath" in OPTIONS.
1464 set new_options ""
1465 set shlib_found 0
1466 foreach opt $options {
1467 if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
1468 if [test_compiler_info "xlc-*"] {
1469 # IBM xlc compiler doesn't accept shared library named other
1470 # than .so: use "-Wl," to bypass this
1471 lappend source "-Wl,$shlib_name"
1472 } else {
1473 lappend source $shlib_name
1474 }
1475 if {$shlib_found == 0} {
1476 set shlib_found 1
1477 if { ([test_compiler_info "gcc-*"]
1478 && ([istarget "powerpc*-*-aix*"]
1479 || [istarget "rs6000*-*-aix*"] )) } {
1480 lappend options "additional_flags=-L${objdir}/${subdir}"
1481 } elseif { [istarget "mips-sgi-irix*"] } {
1482 lappend options "additional_flags=-rpath ${objdir}/${subdir}"
1483 }
1484 }
1485 } else {
1486 lappend new_options $opt
1487 }
1488 }
1489 set options $new_options
1490
c906108c
SS
1491 if [target_info exists gdb_stub] {
1492 set options2 { "additional_flags=-Dusestubs" }
1493 lappend options "libs=[target_info gdb_stub]";
1494 set options [concat $options2 $options]
1495 }
1496 if [target_info exists is_vxworks] {
1497 set options2 { "additional_flags=-Dvxworks" }
1498 lappend options "libs=[target_info gdb_stub]";
1499 set options [concat $options2 $options]
1500 }
1501 if [info exists GDB_TESTCASE_OPTIONS] {
1502 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1503 }
1504 verbose "options are $options"
1505 verbose "source is $source $dest $type $options"
1506
f1c47eb2
MS
1507 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
1508
1509 if {[target_info exists needs_status_wrapper] && \
1510 [target_info needs_status_wrapper] != "0" && \
1511 [info exists gdb_wrapper_file]} {
1512 lappend options "libs=${gdb_wrapper_file}"
1513 lappend options "ldflags=${gdb_wrapper_flags}"
1514 }
1515
fc91c6c2
PB
1516 # Replace the "nowarnings" option with the appropriate additional_flags
1517 # to disable compiler warnings.
1518 set nowarnings [lsearch -exact $options nowarnings]
1519 if {$nowarnings != -1} {
1520 if [target_info exists gdb,nowarnings_flag] {
1521 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
1522 } else {
1523 set flag "additional_flags=-w"
1524 }
1525 set options [lreplace $options $nowarnings $nowarnings $flag]
1526 }
1527
c906108c
SS
1528 set result [target_compile $source $dest $type $options];
1529 regsub "\[\r\n\]*$" "$result" "" result;
1530 regsub "^\[\r\n\]*" "$result" "" result;
81d2cbae
NS
1531 if { $result != "" && [lsearch $options quiet] == -1} {
1532 clone_output "gdb compile failed, $result"
c906108c
SS
1533 }
1534 return $result;
1535}
1536
b6ff0e81
JB
1537
1538# This is just like gdb_compile, above, except that it tries compiling
1539# against several different thread libraries, to see which one this
1540# system has.
1541proc gdb_compile_pthreads {source dest type options} {
0ae67eb3 1542 set built_binfile 0
b6ff0e81
JB
1543 set why_msg "unrecognized error"
1544 foreach lib {-lpthreads -lpthread -lthread} {
1545 # This kind of wipes out whatever libs the caller may have
1546 # set. Or maybe theirs will override ours. How infelicitous.
b5ab8ff3 1547 set options_with_lib [concat $options [list libs=$lib quiet]]
b6ff0e81
JB
1548 set ccout [gdb_compile $source $dest $type $options_with_lib]
1549 switch -regexp -- $ccout {
1550 ".*no posix threads support.*" {
1551 set why_msg "missing threads include file"
1552 break
1553 }
1554 ".*cannot open -lpthread.*" {
1555 set why_msg "missing runtime threads library"
1556 }
1557 ".*Can't find library for -lpthread.*" {
1558 set why_msg "missing runtime threads library"
1559 }
1560 {^$} {
1561 pass "successfully compiled posix threads test case"
1562 set built_binfile 1
1563 break
1564 }
1565 }
1566 }
0ae67eb3 1567 if {!$built_binfile} {
b6ff0e81
JB
1568 unsupported "Couldn't compile $source: ${why_msg}"
1569 return -1
1570 }
57bf0e56
DJ
1571}
1572
1573# Build a shared library from SOURCES. You must use get_compiler_info
1574# first.
1575
1576proc gdb_compile_shlib {sources dest options} {
1577 set obj_options $options
1578
1579 switch -glob [test_compiler_info] {
1580 "xlc-*" {
1581 lappend obj_options "additional_flags=-qpic"
1582 }
1583 "gcc-*" {
1584 if { !([istarget "powerpc*-*-aix*"]
227c54da
DJ
1585 || [istarget "rs6000*-*-aix*"]
1586 || [istarget "*-*-cygwin*"]
1587 || [istarget "*-*-mingw*"]
1588 || [istarget "*-*-pe*"]) } {
57bf0e56
DJ
1589 lappend obj_options "additional_flags=-fpic"
1590 }
1591 }
1592 default {
1593 switch -glob [istarget] {
1594 "hppa*-hp-hpux*" {
1595 lappend obj_options "additional_flags=+z"
1596 }
1597 "mips-sgi-irix*" {
1598 # Disable SGI compiler's implicit -Dsgi
1599 lappend obj_options "additional_flags=-Usgi"
1600 }
1601 default {
1602 # don't know what the compiler is...
1603 }
1604 }
1605 }
1606 }
1607
1608 set outdir [file dirname $dest]
1609 set objects ""
1610 foreach source $sources {
1611 set sourcebase [file tail $source]
1612 if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
1613 return -1
1614 }
1615 lappend objects ${outdir}/${sourcebase}.o
1616 }
1617
1618 if [istarget "hppa*-*-hpux*"] {
1619 remote_exec build "ld -b ${objects} -o ${dest}"
1620 } else {
1621 set link_options $options
1622 if [test_compiler_info "xlc-*"] {
1623 lappend link_options "additional_flags=-qmkshrobj"
1624 } else {
1625 lappend link_options "additional_flags=-shared"
1626 }
1627 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
1628 return -1
1629 }
1630 }
b6ff0e81
JB
1631}
1632
130cacce
AF
1633# This is just like gdb_compile_pthreads, above, except that we always add the
1634# objc library for compiling Objective-C programs
1635proc gdb_compile_objc {source dest type options} {
1636 set built_binfile 0
1637 set why_msg "unrecognized error"
1638 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
1639 # This kind of wipes out whatever libs the caller may have
1640 # set. Or maybe theirs will override ours. How infelicitous.
1641 if { $lib == "solaris" } {
1642 set lib "-lpthread -lposix4"
1643 }
1644 if { $lib != "-lobjc" } {
1645 set lib "-lobjc $lib"
1646 }
1647 set options_with_lib [concat $options [list libs=$lib quiet]]
1648 set ccout [gdb_compile $source $dest $type $options_with_lib]
1649 switch -regexp -- $ccout {
1650 ".*no posix threads support.*" {
1651 set why_msg "missing threads include file"
1652 break
1653 }
1654 ".*cannot open -lpthread.*" {
1655 set why_msg "missing runtime threads library"
1656 }
1657 ".*Can't find library for -lpthread.*" {
1658 set why_msg "missing runtime threads library"
1659 }
1660 {^$} {
1661 pass "successfully compiled objc with posix threads test case"
1662 set built_binfile 1
1663 break
1664 }
1665 }
1666 }
1667 if {!$built_binfile} {
1668 unsupported "Couldn't compile $source: ${why_msg}"
1669 return -1
1670 }
1671}
1672
c906108c
SS
1673proc send_gdb { string } {
1674 global suppress_flag;
1675 if { $suppress_flag } {
1676 return "suppressed";
1677 }
1678 return [remote_send host "$string"];
1679}
1680
1681#
1682#
1683
1684proc gdb_expect { args } {
1685 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
1686 set gtimeout [lindex $args 0];
1687 set expcode [list [lindex $args 1]];
1688 } else {
1689 upvar timeout timeout;
1690
1691 set expcode $args;
1692 if [target_info exists gdb,timeout] {
1693 if [info exists timeout] {
1694 if { $timeout < [target_info gdb,timeout] } {
1695 set gtimeout [target_info gdb,timeout];
1696 } else {
1697 set gtimeout $timeout;
1698 }
1699 } else {
1700 set gtimeout [target_info gdb,timeout];
1701 }
1702 }
1703
1704 if ![info exists gtimeout] {
1705 global timeout;
1706 if [info exists timeout] {
1707 set gtimeout $timeout;
1708 } else {
1709 # Eeeeew.
1710 set gtimeout 60;
1711 }
1712 }
1713 }
1714 global suppress_flag;
1715 global remote_suppress_flag;
1716 if [info exists remote_suppress_flag] {
1717 set old_val $remote_suppress_flag;
1718 }
1719 if [info exists suppress_flag] {
1720 if { $suppress_flag } {
1721 set remote_suppress_flag 1;
1722 }
1723 }
a0b3c4fd 1724 set code [catch \
5f279fa6 1725 {uplevel remote_expect host $gtimeout $expcode} string];
c906108c
SS
1726 if [info exists old_val] {
1727 set remote_suppress_flag $old_val;
1728 } else {
1729 if [info exists remote_suppress_flag] {
1730 unset remote_suppress_flag;
1731 }
1732 }
1733
1734 if {$code == 1} {
1735 global errorInfo errorCode;
1736
1737 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1738 } elseif {$code == 2} {
1739 return -code return $string
1740 } elseif {$code == 3} {
1741 return
1742 } elseif {$code > 4} {
1743 return -code $code $string
1744 }
1745}
1746
c2d11a7d 1747# gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs
085dd6e6
JM
1748#
1749# Check for long sequence of output by parts.
11cf8741 1750# MESSAGE: is the test message to be printed with the test success/fail.
085dd6e6
JM
1751# SENTINEL: Is the terminal pattern indicating that output has finished.
1752# LIST: is the sequence of outputs to match.
1753# If the sentinel is recognized early, it is considered an error.
1754#
11cf8741
JM
1755# Returns:
1756# 1 if the test failed,
1757# 0 if the test passes,
1758# -1 if there was an internal error.
1759#
c2d11a7d 1760proc gdb_expect_list {test sentinel list} {
085dd6e6 1761 global gdb_prompt
11cf8741 1762 global suppress_flag
085dd6e6 1763 set index 0
43ff13b4 1764 set ok 1
11cf8741
JM
1765 if { $suppress_flag } {
1766 set ok 0
a20ce2c3 1767 unresolved "${test}"
11cf8741 1768 }
43ff13b4 1769 while { ${index} < [llength ${list}] } {
085dd6e6
JM
1770 set pattern [lindex ${list} ${index}]
1771 set index [expr ${index} + 1]
1772 if { ${index} == [llength ${list}] } {
43ff13b4
JM
1773 if { ${ok} } {
1774 gdb_expect {
c2d11a7d 1775 -re "${pattern}${sentinel}" {
a20ce2c3 1776 # pass "${test}, pattern ${index} + sentinel"
c2d11a7d
JM
1777 }
1778 -re "${sentinel}" {
a20ce2c3 1779 fail "${test} (pattern ${index} + sentinel)"
c2d11a7d 1780 set ok 0
43ff13b4 1781 }
5c5455dc
AC
1782 -re ".*A problem internal to GDB has been detected" {
1783 fail "${test} (GDB internal error)"
1784 set ok 0
1785 gdb_internal_error_resync
1786 }
43ff13b4 1787 timeout {
a20ce2c3 1788 fail "${test} (pattern ${index} + sentinel) (timeout)"
43ff13b4
JM
1789 set ok 0
1790 }
085dd6e6 1791 }
43ff13b4 1792 } else {
a20ce2c3 1793 # unresolved "${test}, pattern ${index} + sentinel"
085dd6e6
JM
1794 }
1795 } else {
43ff13b4
JM
1796 if { ${ok} } {
1797 gdb_expect {
1798 -re "${pattern}" {
a20ce2c3 1799 # pass "${test}, pattern ${index}"
43ff13b4 1800 }
c2d11a7d 1801 -re "${sentinel}" {
a20ce2c3 1802 fail "${test} (pattern ${index})"
43ff13b4
JM
1803 set ok 0
1804 }
5c5455dc
AC
1805 -re ".*A problem internal to GDB has been detected" {
1806 fail "${test} (GDB internal error)"
1807 set ok 0
1808 gdb_internal_error_resync
1809 }
43ff13b4 1810 timeout {
a20ce2c3 1811 fail "${test} (pattern ${index}) (timeout)"
43ff13b4
JM
1812 set ok 0
1813 }
085dd6e6 1814 }
43ff13b4 1815 } else {
a20ce2c3 1816 # unresolved "${test}, pattern ${index}"
085dd6e6
JM
1817 }
1818 }
1819 }
11cf8741 1820 if { ${ok} } {
a20ce2c3 1821 pass "${test}"
11cf8741
JM
1822 return 0
1823 } else {
1824 return 1
1825 }
085dd6e6
JM
1826}
1827
1828#
1829#
c906108c
SS
1830proc gdb_suppress_entire_file { reason } {
1831 global suppress_flag;
1832
1833 warning "$reason\n";
1834 set suppress_flag -1;
1835}
1836
1837#
1838# Set suppress_flag, which will cause all subsequent calls to send_gdb and
1839# gdb_expect to fail immediately (until the next call to
1840# gdb_stop_suppressing_tests).
1841#
1842proc gdb_suppress_tests { args } {
1843 global suppress_flag;
1844
1845 return; # fnf - disable pending review of results where
1846 # testsuite ran better without this
1847 incr suppress_flag;
1848
1849 if { $suppress_flag == 1 } {
1850 if { [llength $args] > 0 } {
1851 warning "[lindex $args 0]\n";
1852 } else {
1853 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1854 }
1855 }
1856}
1857
1858#
1859# Clear suppress_flag.
1860#
1861proc gdb_stop_suppressing_tests { } {
1862 global suppress_flag;
1863
1864 if [info exists suppress_flag] {
1865 if { $suppress_flag > 0 } {
1866 set suppress_flag 0;
1867 clone_output "Tests restarted.\n";
1868 }
1869 } else {
1870 set suppress_flag 0;
1871 }
1872}
1873
1874proc gdb_clear_suppressed { } {
1875 global suppress_flag;
1876
1877 set suppress_flag 0;
1878}
1879
1880proc gdb_start { } {
1881 default_gdb_start
1882}
1883
1884proc gdb_exit { } {
1885 catch default_gdb_exit
1886}
1887
e63b55d1
NS
1888#
1889# gdb_load_cmd -- load a file into the debugger.
1890# ARGS - additional args to load command.
1891# return a -1 if anything goes wrong.
1892#
1893proc gdb_load_cmd { args } {
1894 global gdb_prompt
1895
1896 if [target_info exists gdb_load_timeout] {
1897 set loadtimeout [target_info gdb_load_timeout]
1898 } else {
1899 set loadtimeout 1600
1900 }
1901 send_gdb "load $args\n"
1902 verbose "Timeout is now $timeout seconds" 2
1903 gdb_expect $loadtimeout {
1904 -re "Loading section\[^\r\]*\r\n" {
1905 exp_continue
1906 }
1907 -re "Start address\[\r\]*\r\n" {
1908 exp_continue
1909 }
1910 -re "Transfer rate\[\r\]*\r\n" {
1911 exp_continue
1912 }
1913 -re "Memory access error\[^\r\]*\r\n" {
1914 perror "Failed to load program"
1915 return -1
1916 }
1917 -re "$gdb_prompt $" {
1918 return 0
1919 }
1920 -re "(.*)\r\n$gdb_prompt " {
1921 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
1922 return -1
1923 }
1924 timeout {
1925 perror "Timed out trying to load $arg."
1926 return -1
1927 }
1928 }
1929 return -1
1930}
1931
c906108c
SS
1932#
1933# gdb_load -- load a file into the debugger.
2db8e78e 1934# Many files in config/*.exp override this procedure.
c906108c
SS
1935#
1936proc gdb_load { arg } {
1937 return [gdb_file_cmd $arg]
1938}
1939
1940proc gdb_continue { function } {
1941 global decimal
1942
1943 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
1944}
1945
1946proc default_gdb_init { args } {
277254ba
MS
1947 global gdb_wrapper_initialized
1948
c906108c
SS
1949 gdb_clear_suppressed;
1950
277254ba
MS
1951 # Make sure that the wrapper is rebuilt
1952 # with the appropriate multilib option.
1953 set gdb_wrapper_initialized 0
1954
c906108c
SS
1955 # Uh, this is lame. Really, really, really lame. But there's this *one*
1956 # testcase that will fail in random places if we don't increase this.
1957 match_max -d 20000
1958
1959 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
1960 if { [llength $args] > 0 } {
1961 global pf_prefix
1962
1963 set file [lindex $args 0];
1964
1965 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
1966 }
1967 global gdb_prompt;
1968 if [target_info exists gdb_prompt] {
1969 set gdb_prompt [target_info gdb_prompt];
1970 } else {
1971 set gdb_prompt "\\(gdb\\)"
1972 }
1973}
1974
1975proc gdb_init { args } {
1976 return [eval default_gdb_init $args];
1977}
1978
1979proc gdb_finish { } {
1980 gdb_exit;
1981}
1982
1983global debug_format
7a292a7a 1984set debug_format "unknown"
c906108c
SS
1985
1986# Run the gdb command "info source" and extract the debugging format
1987# information from the output and save it in debug_format.
1988
1989proc get_debug_format { } {
1990 global gdb_prompt
1991 global verbose
1992 global expect_out
1993 global debug_format
1994
1995 set debug_format "unknown"
1996 send_gdb "info source\n"
1997 gdb_expect 10 {
919d772c 1998 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
c906108c
SS
1999 set debug_format $expect_out(1,string)
2000 verbose "debug format is $debug_format"
2001 return 1;
2002 }
2003 -re "No current source file.\r\n$gdb_prompt $" {
2004 perror "get_debug_format used when no current source file"
2005 return 0;
2006 }
2007 -re "$gdb_prompt $" {
2008 warning "couldn't check debug format (no valid response)."
2009 return 1;
2010 }
2011 timeout {
2012 warning "couldn't check debug format (timed out)."
2013 return 1;
2014 }
2015 }
2016}
2017
838ae6c4
JB
2018# Return true if FORMAT matches the debug format the current test was
2019# compiled with. FORMAT is a shell-style globbing pattern; it can use
2020# `*', `[...]', and so on.
2021#
2022# This function depends on variables set by `get_debug_format', above.
2023
2024proc test_debug_format {format} {
2025 global debug_format
2026
2027 return [expr [string match $format $debug_format] != 0]
2028}
2029
c906108c
SS
2030# Like setup_xfail, but takes the name of a debug format (DWARF 1,
2031# COFF, stabs, etc). If that format matches the format that the
2032# current test was compiled with, then the next test is expected to
2033# fail for any target. Returns 1 if the next test or set of tests is
2034# expected to fail, 0 otherwise (or if it is unknown). Must have
2035# previously called get_debug_format.
b55a4771 2036proc setup_xfail_format { format } {
838ae6c4 2037 set ret [test_debug_format $format];
b55a4771 2038
838ae6c4 2039 if {$ret} then {
b55a4771
MS
2040 setup_xfail "*-*-*"
2041 }
2042 return $ret;
2043}
c906108c
SS
2044
2045proc gdb_step_for_stub { } {
2046 global gdb_prompt;
2047
2048 if ![target_info exists gdb,use_breakpoint_for_stub] {
2049 if [target_info exists gdb_stub_step_command] {
2050 set command [target_info gdb_stub_step_command];
2051 } else {
2052 set command "step";
2053 }
2054 send_gdb "${command}\n";
2055 set tries 0;
2056 gdb_expect 60 {
2057 -re "(main.* at |.*in .*start).*$gdb_prompt" {
2058 return;
2059 }
2060 -re ".*$gdb_prompt" {
2061 incr tries;
2062 if { $tries == 5 } {
2063 fail "stepping out of breakpoint function";
2064 return;
2065 }
2066 send_gdb "${command}\n";
2067 exp_continue;
2068 }
2069 default {
2070 fail "stepping out of breakpoint function";
2071 return;
2072 }
2073 }
2074 }
2075 send_gdb "where\n";
2076 gdb_expect {
2077 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
2078 set file $expect_out(1,string);
2079 set linenum [expr $expect_out(2,string) + 1];
2080 set breakplace "${file}:${linenum}";
2081 }
2082 default {}
2083 }
2084 send_gdb "break ${breakplace}\n";
2085 gdb_expect 60 {
2086 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
2087 set breakpoint $expect_out(1,string);
2088 }
2089 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
2090 set breakpoint $expect_out(1,string);
2091 }
2092 default {}
2093 }
2094 send_gdb "continue\n";
2095 gdb_expect 60 {
2096 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
2097 gdb_test "delete $breakpoint" ".*" "";
2098 return;
2099 }
2100 default {}
2101 }
2102}
2103
c6fee705
MC
2104# gdb_get_line_number TEXT [FILE]
2105#
2106# Search the source file FILE, and return the line number of the
2107# first line containing TEXT. If no match is found, return -1.
2108#
2109# TEXT is a string literal, not a regular expression.
2110#
2111# The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
2112# specified, and does not start with "/", then it is assumed to be in
2113# "$srcdir/$subdir". This is awkward, and can be fixed in the future,
2114# by changing the callers and the interface at the same time.
2115# In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
2116# gdb.base/ena-dis-br.exp.
2117#
2118# Use this function to keep your test scripts independent of the
2119# exact line numbering of the source file. Don't write:
2120#
2121# send_gdb "break 20"
2122#
2123# This means that if anyone ever edits your test's source file,
2124# your test could break. Instead, put a comment like this on the
2125# source file line you want to break at:
2126#
2127# /* breakpoint spot: frotz.exp: test name */
2128#
2129# and then write, in your test script (which we assume is named
2130# frotz.exp):
2131#
2132# send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
2133#
2134# (Yes, Tcl knows how to handle the nested quotes and brackets.
2135# Try this:
2136# $ tclsh
2137# % puts "foo [lindex "bar baz" 1]"
2138# foo baz
2139# %
2140# Tcl is quite clever, for a little stringy language.)
2141#
2142# ===
2143#
2144# The previous implementation of this procedure used the gdb search command.
2145# This version is different:
2146#
2147# . It works with MI, and it also works when gdb is not running.
2148#
2149# . It operates on the build machine, not the host machine.
2150#
2151# . For now, this implementation fakes a current directory of
2152# $srcdir/$subdir to be compatible with the old implementation.
2153# This will go away eventually and some callers will need to
2154# be changed.
2155#
2156# . The TEXT argument is literal text and matches literally,
2157# not a regular expression as it was before.
2158#
2159# . State changes in gdb, such as changing the current file
2160# and setting $_, no longer happen.
2161#
2162# After a bit of time we can forget about the differences from the
2163# old implementation.
2164#
2165# --chastain 2004-08-05
2166
2167proc gdb_get_line_number { text { file "" } } {
2168 global srcdir
2169 global subdir
2170 global srcfile
c906108c 2171
c6fee705
MC
2172 if { "$file" == "" } then {
2173 set file "$srcfile"
2174 }
2175 if { ! [regexp "^/" "$file"] } then {
2176 set file "$srcdir/$subdir/$file"
c906108c
SS
2177 }
2178
c6fee705
MC
2179 if { [ catch { set fd [open "$file"] } message ] } then {
2180 perror "$message"
2181 return -1
c906108c 2182 }
c6fee705
MC
2183
2184 set found -1
2185 for { set line 1 } { 1 } { incr line } {
2186 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
2187 perror "$message"
2188 return -1
2189 }
2190 if { $nchar < 0 } then {
2191 break
2192 }
2193 if { [string first "$text" "$body"] >= 0 } then {
2194 set found $line
2195 break
2196 }
2197 }
2198
2199 if { [ catch { close "$fd" } message ] } then {
2200 perror "$message"
2201 return -1
2202 }
2203
2204 return $found
c906108c
SS
2205}
2206
7a292a7a
SS
2207# gdb_continue_to_end:
2208# The case where the target uses stubs has to be handled specially. If a
2209# stub is used, we set a breakpoint at exit because we cannot rely on
2210# exit() behavior of a remote target.
2211#
2212# mssg is the error message that gets printed.
2213
2214proc gdb_continue_to_end {mssg} {
2215 if [target_info exists use_gdb_stub] {
2216 if {![gdb_breakpoint "exit"]} {
2217 return 0
2218 }
2219 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
2220 "continue until exit at $mssg"
2221 } else {
2222 # Continue until we exit. Should not stop again.
2223 # Don't bother to check the output of the program, that may be
2224 # extremely tough for some remote systems.
2225 gdb_test "continue"\
1c56143a 2226 "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
7a292a7a
SS
2227 "continue until exit at $mssg"
2228 }
2229}
2230
2231proc rerun_to_main {} {
2232 global gdb_prompt
2233
2234 if [target_info exists use_gdb_stub] {
2235 gdb_run_cmd
2236 gdb_expect {
2237 -re ".*Breakpoint .*main .*$gdb_prompt $"\
2238 {pass "rerun to main" ; return 0}
2239 -re "$gdb_prompt $"\
2240 {fail "rerun to main" ; return 0}
2241 timeout {fail "(timeout) rerun to main" ; return 0}
2242 }
2243 } else {
2244 send_gdb "run\n"
2245 gdb_expect {
11350d2a
CV
2246 -re "The program .* has been started already.*y or n. $" {
2247 send_gdb "y\n"
2248 exp_continue
2249 }
7a292a7a
SS
2250 -re "Starting program.*$gdb_prompt $"\
2251 {pass "rerun to main" ; return 0}
2252 -re "$gdb_prompt $"\
2253 {fail "rerun to main" ; return 0}
2254 timeout {fail "(timeout) rerun to main" ; return 0}
2255 }
2256 }
2257}
c906108c 2258
13a5e3b8
MS
2259# Print a message and return true if a test should be skipped
2260# due to lack of floating point suport.
2261
2262proc gdb_skip_float_test { msg } {
2263 if [target_info exists gdb,skip_float_tests] {
2264 verbose "Skipping test '$msg': no float tests.";
2265 return 1;
2266 }
2267 return 0;
2268}
2269
2270# Print a message and return true if a test should be skipped
2271# due to lack of stdio support.
2272
2273proc gdb_skip_stdio_test { msg } {
2274 if [target_info exists gdb,noinferiorio] {
2275 verbose "Skipping test '$msg': no inferior i/o.";
2276 return 1;
2277 }
2278 return 0;
2279}
2280
2281proc gdb_skip_bogus_test { msg } {
2282 return 0;
2283}
2284
e515b470
DJ
2285# Return true if a test should be skipped due to lack of XML support
2286# in the host GDB.
2287
2288proc gdb_skip_xml_test { } {
2289 global gdb_prompt
2290 global srcdir
2291 global xml_missing_cached
2292
2293 if {[info exists xml_missing_cached]} {
2294 return $xml_missing_cached
2295 }
2296
2297 gdb_start
2298 set xml_missing_cached 0
2299 gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" {
2300 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
2301 set xml_missing_cached 1
2302 }
2303 -re ".*$gdb_prompt $" { }
2304 }
2305 gdb_exit
2306 return $xml_missing_cached
2307}
1f8a6abb
EZ
2308
2309# Note: the procedure gdb_gnu_strip_debug will produce an executable called
2310# ${binfile}.dbglnk, which is just like the executable ($binfile) but without
2311# the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
94277a38 2312# the name of a debuginfo only file. This file will be stored in the
1f8a6abb
EZ
2313# gdb.base/.debug subdirectory.
2314
2315# Functions for separate debug info testing
2316
2317# starting with an executable:
2318# foo --> original executable
2319
2320# at the end of the process we have:
2321# foo.stripped --> foo w/o debug info
2322# .debug/foo.debug --> foo's debug info
2323# foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
2324
2325# Return the name of the file in which we should stor EXEC's separated
2326# debug info. EXEC contains the full path.
2327proc separate_debug_filename { exec } {
2328
2329 # In a .debug subdirectory off the same directory where the testcase
2330 # executable is going to be. Something like:
2331 # <your-path>/gdb/testsuite/gdb.base/.debug/blah.debug.
2332 # This is the default location where gdb expects to findi
2333 # the debug info file.
2334
2335 set exec_dir [file dirname $exec]
2336 set exec_file [file tail $exec]
2337 set debug_dir [file join $exec_dir ".debug"]
2338 set debug_file [file join $debug_dir "${exec_file}.debug"]
2339
2340 return $debug_file
2341}
2342
94277a38
DJ
2343# Create stripped files for DEST, replacing it. If ARGS is passed, it is a
2344# list of optional flags. The only currently supported flag is no-main,
2345# which removes the symbol entry for main from the separate debug file.
1f8a6abb 2346
94277a38
DJ
2347proc gdb_gnu_strip_debug { dest args } {
2348
2349 # First, make sure that we can do this. This is nasty. We need to
2350 # check for the stabs debug format. To do this we must run gdb on
2351 # the unstripped executable, list 'main' (as to have a default
2352 # source file), use get_debug_format (which does 'info source')
2353 # and then see if the debug info is stabs. If so, we bail out. We
2354 # cannot do this any other way because get_debug_format finds out
2355 # the debug format using gdb itself, and in case of stabs we get
2356 # an error loading the program if it is already stripped. An
2357 # alternative would be to find out the debug info from the flags
2358 # passed to dejagnu when the test is run.
2359
2360 gdb_exit
2361 gdb_start
2362 gdb_load ${dest}
2363 gdb_test "list main" "" ""
2364 get_debug_format
2365 if { [test_debug_format "stabs"] } then {
2366 # The separate debug info feature doesn't work well in
2367 # binutils with stabs. It produces a corrupted debug info
2368 # only file, and gdb chokes on it. It is almost impossible to
2369 # capture the failing message out of gdb, because it happens
2370 # inside gdb_load. At that point any error message is
2371 # intercepted by dejagnu itself, and, because of the error
2372 # threshold, any faulty test result is changed into an
2373 # UNRESOLVED. (see dejagnu/lib/framework.exp)
2374 unsupported "no separate debug info handling with stabs"
2375 return -1
2376 } elseif { [test_debug_format "unknown"] } then {
2377 # gdb doesn't know what the debug format is. We are out of luck here.
2378 unsupported "unknown debugging format"
2379 return -1
2380 }
2381 gdb_exit
1f8a6abb
EZ
2382
2383 set debug_file [separate_debug_filename $dest]
2384 set strip_to_file_program strip
2385 set objcopy_program objcopy
2386
2387 # Make sure the directory that will hold the separated debug
2388 # info actually exists.
2389 set debug_dir [file dirname $debug_file]
2390 if {! [file isdirectory $debug_dir]} {
2391 file mkdir $debug_dir
2392 }
2393
2394 set debug_link [file tail $debug_file]
2395 set stripped_file "${dest}.stripped"
2396
2397 # Get rid of the debug info, and store result in stripped_file
2398 # something like gdb/testsuite/gdb.base/blah.stripped.
2399 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
2400 verbose "result is $result"
2401 verbose "output is $output"
2402 if {$result == 1} {
2403 return 1
2404 }
2405
2406 # Get rid of everything but the debug info, and store result in debug_file
2407 # This will be in the .debug subdirectory, see above.
2408 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
2409 verbose "result is $result"
2410 verbose "output is $output"
2411 if {$result == 1} {
2412 return 1
2413 }
2414
94277a38
DJ
2415 # If no-main is passed, strip the symbol for main from the separate
2416 # file. This is to simulate the behavior of elfutils's eu-strip, which
2417 # leaves the symtab in the original file only. There's no way to get
2418 # objcopy or strip to remove the symbol table without also removing the
2419 # debugging sections, so this is as close as we can get.
2420 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
2421 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
2422 verbose "result is $result"
2423 verbose "output is $output"
2424 if {$result == 1} {
2425 return 1
2426 }
2427 file delete "${debug_file}"
2428 file rename "${debug_file}-tmp" "${debug_file}"
2429 }
2430
1f8a6abb
EZ
2431 # Link the two previous output files together, adding the .gnu_debuglink
2432 # section to the stripped_file, containing a pointer to the debug_file,
2433 # save the new file in dest.
2434 # This will be the regular executable filename, in the usual location.
2435 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
2436 verbose "result is $result"
2437 verbose "output is $output"
2438 if {$result == 1} {
2439 return 1
2440 }
2441
2442 return 0
2443}
2444
d8295fe9
VP
2445# Test the output of GDB_COMMAND matches the pattern obtained
2446# by concatenating all elements of EXPECTED_LINES. This makes
2447# it possible to split otherwise very long string into pieces.
2448# If third argument is not empty, it's used as the name of the
2449# test to be printed on pass/fail.
2450proc help_test_raw { gdb_command expected_lines args } {
2451 set message $gdb_command
2452 if [llength $args]>0 then {
2453 set message [lindex $args 0]
2454 }
2455 set expected_output [join $expected_lines ""]
2456 gdb_test "${gdb_command}" "${expected_output}" $message
2457}
2458
2459# Test the output of "help COMMNAD_CLASS". EXPECTED_INITIAL_LINES
2460# are regular expressions that should match the beginning of output,
2461# before the list of commands in that class. The presence of
2462# command list and standard epilogue will be tested automatically.
2463proc test_class_help { command_class expected_initial_lines args } {
2464 set l_stock_body {
2465 "List of commands\:.*\[\r\n\]+"
2466 "Type \"help\" followed by command name for full documentation\.\[\r\n\]+"
2467 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+"
2468 "Command name abbreviations are allowed if unambiguous\."
2469 }
2470 set l_entire_body [concat $expected_initial_lines $l_stock_body]
2471
2472 eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
2473}
2474
2475# COMMAND_LIST should have either one element -- command to test, or
2476# two elements -- abbreviated command to test, and full command the first
2477# element is abbreviation of.
2478# The command must be a prefix command. EXPECTED_INITIAL_LINES
2479# are regular expressions that should match the beginning of output,
2480# before the list of subcommands. The presence of
2481# subcommand list and standard epilogue will be tested automatically.
2482proc test_prefix_command_help { command_list expected_initial_lines args } {
2483 set command [lindex $command_list 0]
2484 if {[llength $command_list]>1} {
2485 set full_command [lindex $command_list 1]
2486 } else {
2487 set full_command $command
2488 }
2489 # Use 'list' and not just {} because we want variables to
2490 # be expanded in this list.
2491 set l_stock_body [list\
2492 "List of $full_command subcommands\:.*\[\r\n\]+"\
2493 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
2494 "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
2495 "Command name abbreviations are allowed if unambiguous\."]
2496 set l_entire_body [concat $expected_initial_lines $l_stock_body]
2497 if {[llength $args]>0} {
2498 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
2499 } else {
2500 help_test_raw "help ${command}" $l_entire_body
2501 }
2502}
This page took 0.805649 seconds and 4 git commands to generate.