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