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