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