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