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