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