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