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