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