]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/lib/gdb.exp
Remove conflict indicators.
[thirdparty/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
CommitLineData
b5ab8ff3 1# Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
94b8e876 2# 2002, 2003, 2004
b6ba6518 3# Free Software Foundation, Inc.
c906108c
SS
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19# Please email any bugs, comments, and/or additions to this file to:
20# bug-gdb@prep.ai.mit.edu
21
22# This file was written by Fred Fish. (fnf@cygnus.com)
23
24# Generic gdb subroutines that should work for any target. If these
25# need to be modified for any target, it can be done with a variable
26# or by passing arguments.
27
28load_lib libgloss.exp
29
30global GDB
c906108c
SS
31
32if [info exists TOOL_EXECUTABLE] {
33 set GDB $TOOL_EXECUTABLE;
34}
35if ![info exists GDB] {
36 if ![is_remote host] {
37 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
38 } else {
39 set GDB [transform gdb];
40 }
41}
42verbose "using GDB = $GDB" 2
43
44global GDBFLAGS
45if ![info exists GDBFLAGS] {
46 set GDBFLAGS "-nx"
47}
48verbose "using GDBFLAGS = $GDBFLAGS" 2
49
9e0b60a8
JM
50# The variable gdb_prompt is a regexp which matches the gdb prompt.
51# Set it if it is not already set.
c906108c 52global gdb_prompt
9e0b60a8 53if ![info exists gdb_prompt] then {
c906108c
SS
54 set gdb_prompt "\[(\]gdb\[)\]"
55}
56
93076499
ND
57# Needed for some tests under Cygwin.
58global EXEEXT
59global env
60
61if ![info exists env(EXEEXT)] {
62 set EXEEXT ""
63} else {
64 set EXEEXT $env(EXEEXT)
65}
66
085dd6e6
JM
67### Only procedures should come after this point.
68
c906108c
SS
69#
70# gdb_version -- extract and print the version number of GDB
71#
72proc default_gdb_version {} {
73 global GDB
74 global GDBFLAGS
75 global gdb_prompt
76 set fileid [open "gdb_cmd" w];
77 puts $fileid "q";
78 close $fileid;
79 set cmdfile [remote_download host "gdb_cmd"];
80 set output [remote_exec host "$GDB -nw --command $cmdfile"]
81 remote_file build delete "gdb_cmd";
82 remote_file host delete "$cmdfile";
83 set tmp [lindex $output 1];
84 set version ""
85 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
86 if ![is_remote host] {
87 clone_output "[which $GDB] version $version $GDBFLAGS\n"
88 } else {
89 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
90 }
91}
92
93proc gdb_version { } {
94 return [default_gdb_version];
95}
96
97#
98# gdb_unload -- unload a file if one is loaded
99#
100
101proc gdb_unload {} {
102 global verbose
103 global GDB
104 global gdb_prompt
105 send_gdb "file\n"
106 gdb_expect 60 {
107 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
108 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
109 -re "A program is being debugged already..*Kill it.*y or n. $"\
110 { send_gdb "y\n"
111 verbose "\t\tKilling previous program being debugged"
112 exp_continue
113 }
114 -re "Discard symbol table from .*y or n.*$" {
115 send_gdb "y\n"
116 exp_continue
117 }
118 -re "$gdb_prompt $" {}
119 timeout {
120 perror "couldn't unload file in $GDB (timed out)."
121 return -1
122 }
123 }
124}
125
126# Many of the tests depend on setting breakpoints at various places and
127# running until that breakpoint is reached. At times, we want to start
128# with a clean-slate with respect to breakpoints, so this utility proc
129# lets us do this without duplicating this code everywhere.
130#
131
132proc delete_breakpoints {} {
133 global gdb_prompt
134
a0b3c4fd
JM
135 # we need a larger timeout value here or this thing just confuses
136 # itself. May need a better implementation if possible. - guo
137 #
c906108c 138 send_gdb "delete breakpoints\n"
a0b3c4fd 139 gdb_expect 100 {
c906108c
SS
140 -re "Delete all breakpoints.*y or n.*$" {
141 send_gdb "y\n";
142 exp_continue
143 }
144 -re "$gdb_prompt $" { # This happens if there were no breakpoints
145 }
146 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
147 }
148 send_gdb "info breakpoints\n"
a0b3c4fd 149 gdb_expect 100 {
c906108c
SS
150 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
151 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
152 -re "Delete all breakpoints.*or n.*$" {
153 send_gdb "y\n";
154 exp_continue
155 }
156 timeout { perror "info breakpoints (timeout)" ; return }
157 }
158}
159
160
161#
162# Generic run command.
163#
164# The second pattern below matches up to the first newline *only*.
165# Using ``.*$'' could swallow up output that we attempt to match
166# elsewhere.
167#
168proc gdb_run_cmd {args} {
169 global gdb_prompt
170
171 if [target_info exists gdb_init_command] {
172 send_gdb "[target_info gdb_init_command]\n";
173 gdb_expect 30 {
174 -re "$gdb_prompt $" { }
175 default {
176 perror "gdb_init_command for target failed";
177 return;
178 }
179 }
180 }
181
182 if [target_info exists use_gdb_stub] {
183 if [target_info exists gdb,do_reload_on_run] {
917317f4
JM
184 # Specifying no file, defaults to the executable
185 # currently being debugged.
186 if { [gdb_load ""] < 0 } {
187 return;
188 }
c906108c
SS
189 send_gdb "continue\n";
190 gdb_expect 60 {
191 -re "Continu\[^\r\n\]*\[\r\n\]" {}
192 default {}
193 }
194 return;
195 }
196
197 if [target_info exists gdb,start_symbol] {
198 set start [target_info gdb,start_symbol];
199 } else {
200 set start "start";
201 }
202 send_gdb "jump *$start\n"
917317f4
JM
203 set start_attempt 1;
204 while { $start_attempt } {
205 # Cap (re)start attempts at three to ensure that this loop
206 # always eventually fails. Don't worry about trying to be
207 # clever and not send a command when it has failed.
208 if [expr $start_attempt > 3] {
209 perror "Jump to start() failed (retry count exceeded)";
c906108c
SS
210 return;
211 }
917317f4
JM
212 set start_attempt [expr $start_attempt + 1];
213 gdb_expect 30 {
214 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
215 set start_attempt 0;
216 }
217 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
218 perror "Can't find start symbol to run in gdb_run";
219 return;
220 }
221 -re "No symbol \"start\" in current.*$gdb_prompt $" {
222 send_gdb "jump *_start\n";
223 }
224 -re "No symbol.*context.*$gdb_prompt $" {
225 set start_attempt 0;
226 }
227 -re "Line.* Jump anyway.*y or n. $" {
228 send_gdb "y\n"
229 }
230 -re "The program is not being run.*$gdb_prompt $" {
231 if { [gdb_load ""] < 0 } {
232 return;
233 }
234 send_gdb "jump *$start\n";
235 }
236 timeout {
237 perror "Jump to start() failed (timeout)";
238 return
239 }
c906108c 240 }
c906108c
SS
241 }
242 if [target_info exists gdb_stub] {
243 gdb_expect 60 {
244 -re "$gdb_prompt $" {
245 send_gdb "continue\n"
246 }
247 }
248 }
249 return
250 }
83f66e8f
DJ
251
252 if [target_info exists gdb,do_reload_on_run] {
253 if { [gdb_load ""] < 0 } {
254 return;
255 }
256 }
c906108c
SS
257 send_gdb "run $args\n"
258# This doesn't work quite right yet.
259 gdb_expect 60 {
260 -re "The program .* has been started already.*y or n. $" {
261 send_gdb "y\n"
262 exp_continue
263 }
264 -re "Starting program: \[^\r\n\]*" {}
265 }
266}
267
268proc gdb_breakpoint { function } {
269 global gdb_prompt
270 global decimal
271
272 send_gdb "break $function\n"
273 # The first two regexps are what we get with -g, the third is without -g.
274 gdb_expect 30 {
275 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
276 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
277 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
18fe2033 278 -re "Make breakpoint pending.*y or n. $" {
14b1a056
DJ
279 send_gdb "n\n"
280 exp_continue
18fe2033 281 }
c906108c
SS
282 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
283 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
284 }
285 return 1;
286}
287
288# Set breakpoint at function and run gdb until it breaks there.
289# Since this is the only breakpoint that will be set, if it stops
290# at a breakpoint, we will assume it is the one we want. We can't
291# just compare to "function" because it might be a fully qualified,
292# single quoted C++ function specifier.
293
294proc runto { function } {
295 global gdb_prompt
296 global decimal
297
298 delete_breakpoints
299
300 if ![gdb_breakpoint $function] {
301 return 0;
302 }
303
304 gdb_run_cmd
305
306 # the "at foo.c:36" output we get with -g.
307 # the "in func" output we get without -g.
308 gdb_expect 30 {
309 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
310 return 1
311 }
312 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
313 return 1
314 }
315 -re "$gdb_prompt $" {
316 fail "running to $function in runto"
317 return 0
318 }
319 timeout {
320 fail "running to $function in runto (timeout)"
321 return 0
322 }
323 }
324 return 1
325}
326
327#
328# runto_main -- ask gdb to run until we hit a breakpoint at main.
329# The case where the target uses stubs has to be handled
330# specially--if it uses stubs, assuming we hit
331# breakpoint() and just step out of the function.
332#
333proc runto_main { } {
334 global gdb_prompt
335 global decimal
336
337 if ![target_info exists gdb_stub] {
338 return [runto main]
339 }
340
341 delete_breakpoints
342
343 gdb_step_for_stub;
344
345 return 1
346}
347
7a292a7a 348
4ce44c66
JM
349### Continue, and expect to hit a breakpoint.
350### Report a pass or fail, depending on whether it seems to have
351### worked. Use NAME as part of the test name; each call to
352### continue_to_breakpoint should use a NAME which is unique within
353### that test file.
354proc gdb_continue_to_breakpoint {name} {
355 global gdb_prompt
356 set full_name "continue to breakpoint: $name"
357
358 send_gdb "continue\n"
359 gdb_expect {
360 -re "Breakpoint .* at .*\r\n$gdb_prompt $" {
361 pass $full_name
362 }
363 -re ".*$gdb_prompt $" {
364 fail $full_name
365 }
366 timeout {
367 fail "$full_name (timeout)"
368 }
369 }
370}
371
372
039cf96d
AC
373# gdb_internal_error_resync:
374#
375# Answer the questions GDB asks after it reports an internal error
376# until we get back to a GDB prompt. Decline to quit the debugging
377# session, and decline to create a core file. Return non-zero if the
378# resync succeeds.
379#
380# This procedure just answers whatever questions come up until it sees
381# a GDB prompt; it doesn't require you to have matched the input up to
382# any specific point. However, it only answers questions it sees in
383# the output itself, so if you've matched a question, you had better
384# answer it yourself before calling this.
385#
386# You can use this function thus:
387#
388# gdb_expect {
389# ...
390# -re ".*A problem internal to GDB has been detected" {
391# gdb_internal_error_resync
392# }
393# ...
394# }
395#
396proc gdb_internal_error_resync {} {
397 global gdb_prompt
398
399 set count 0
400 while {$count < 10} {
401 gdb_expect {
402 -re "Quit this debugging session\\? \\(y or n\\) $" {
403 send_gdb "n\n"
404 incr count
405 }
406 -re "Create a core file of GDB\\? \\(y or n\\) $" {
407 send_gdb "n\n"
408 incr count
409 }
410 -re "$gdb_prompt $" {
411 # We're resynchronized.
412 return 1
413 }
414 timeout {
415 perror "Could not resync from internal error (timeout)"
416 return 0
417 }
418 }
419 }
2b211c59
AC
420 perror "Could not resync from internal error (resync count exceeded)"
421 return 0
039cf96d
AC
422}
423
4ce44c66 424
2307bd6a 425# gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
8dbfb380 426# Send a command to gdb; test the result.
c906108c
SS
427#
428# COMMAND is the command to execute, send to GDB with send_gdb. If
429# this is the null string no command is sent.
2307bd6a
DJ
430# MESSAGE is a message to be printed with the built-in failure patterns
431# if one of them matches. If MESSAGE is empty COMMAND will be used.
432# EXPECT_ARGUMENTS will be fed to expect in addition to the standard
433# patterns. Pattern elements will be evaluated in the caller's
434# context; action elements will be executed in the caller's context.
435# Unlike patterns for gdb_test, these patterns should generally include
436# the final newline and prompt.
c906108c
SS
437#
438# Returns:
2307bd6a
DJ
439# 1 if the test failed, according to a built-in failure pattern
440# 0 if only user-supplied patterns matched
c906108c
SS
441# -1 if there was an internal error.
442#
d422fe19
AC
443# You can use this function thus:
444#
445# gdb_test_multiple "print foo" "test foo" {
446# -re "expected output 1" {
447# pass "print foo"
448# }
449# -re "expected output 2" {
450# fail "print foo"
451# }
452# }
453#
454# The standard patterns, such as "Program exited..." and "A problem
455# ...", all being implicitly appended to that list.
456#
2307bd6a 457proc gdb_test_multiple { command message user_code } {
c906108c
SS
458 global verbose
459 global gdb_prompt
460 global GDB
461 upvar timeout timeout
c47cebdb 462 upvar expect_out expect_out
c906108c 463
2307bd6a
DJ
464 if { $message == "" } {
465 set message $command
c906108c 466 }
c906108c 467
2307bd6a
DJ
468 # TCL/EXPECT WART ALERT
469 # Expect does something very strange when it receives a single braced
470 # argument. It splits it along word separators and performs substitutions.
471 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
472 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
473 # double-quoted list item, "\[ab\]" is just a long way of representing
474 # "[ab]", because the backslashes will be removed by lindex.
475
476 # Unfortunately, there appears to be no easy way to duplicate the splitting
477 # that expect will do from within TCL. And many places make use of the
478 # "\[0-9\]" construct, so we need to support that; and some places make use
479 # of the "[func]" construct, so we need to support that too. In order to
480 # get this right we have to substitute quoted list elements differently
481 # from braced list elements.
482
483 # We do this roughly the same way that Expect does it. We have to use two
484 # lists, because if we leave unquoted newlines in the argument to uplevel
485 # they'll be treated as command separators, and if we escape newlines
486 # we mangle newlines inside of command blocks. This assumes that the
487 # input doesn't contain a pattern which contains actual embedded newlines
488 # at this point!
489
490 regsub -all {\n} ${user_code} { } subst_code
491 set subst_code [uplevel list $subst_code]
492
493 set processed_code ""
494 set patterns ""
495 set expecting_action 0
496 foreach item $user_code subst_item $subst_code {
497 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
498 lappend processed_code $item
499 continue
500 }
501 if {$item == "-indices" || $item == "-re" || $item == "-ex"} {
502 lappend processed_code $item
503 continue
504 }
505 if { $expecting_action } {
506 lappend processed_code "uplevel [list $item]"
507 set expecting_action 0
508 # Cosmetic, no effect on the list.
509 append processed_code "\n"
510 continue
511 }
512 set expecting_action 1
513 lappend processed_code $subst_item
514 if {$patterns != ""} {
515 append patterns "; "
516 }
517 append patterns "\"$subst_item\""
c906108c
SS
518 }
519
2307bd6a
DJ
520 # Also purely cosmetic.
521 regsub -all {\r} $patterns {\\r} patterns
522 regsub -all {\n} $patterns {\\n} patterns
523
c906108c
SS
524 if $verbose>2 then {
525 send_user "Sending \"$command\" to gdb\n"
2307bd6a 526 send_user "Looking to match \"$patterns\"\n"
c906108c
SS
527 send_user "Message is \"$message\"\n"
528 }
529
530 set result -1
531 set string "${command}\n";
532 if { $command != "" } {
533 while { "$string" != "" } {
534 set foo [string first "\n" "$string"];
535 set len [string length "$string"];
536 if { $foo < [expr $len - 1] } {
537 set str [string range "$string" 0 $foo];
538 if { [send_gdb "$str"] != "" } {
539 global suppress_flag;
540
541 if { ! $suppress_flag } {
542 perror "Couldn't send $command to GDB.";
543 }
544 fail "$message";
545 return $result;
546 }
a0b3c4fd
JM
547 # since we're checking if each line of the multi-line
548 # command are 'accepted' by GDB here,
549 # we need to set -notransfer expect option so that
550 # command output is not lost for pattern matching
551 # - guo
5f279fa6
DJ
552 gdb_expect 2 {
553 -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
554 timeout { verbose "partial: timeout" 3 }
c906108c
SS
555 }
556 set string [string range "$string" [expr $foo + 1] end];
557 } else {
558 break;
559 }
560 }
561 if { "$string" != "" } {
562 if { [send_gdb "$string"] != "" } {
563 global suppress_flag;
564
565 if { ! $suppress_flag } {
566 perror "Couldn't send $command to GDB.";
567 }
568 fail "$message";
569 return $result;
570 }
571 }
572 }
573
9d2e1bab
ND
574 if [target_info exists gdb,timeout] {
575 set tmt [target_info gdb,timeout];
c906108c 576 } else {
c906108c
SS
577 if [info exists timeout] {
578 set tmt $timeout;
579 } else {
9d2e1bab
ND
580 global timeout;
581 if [info exists timeout] {
582 set tmt $timeout;
583 } else {
584 set tmt 60;
585 }
c906108c
SS
586 }
587 }
2307bd6a
DJ
588
589 set code {
039cf96d
AC
590 -re ".*A problem internal to GDB has been detected" {
591 fail "$message (GDB internal error)"
592 gdb_internal_error_resync
593 }
c906108c
SS
594 -re "\\*\\*\\* DOSEXIT code.*" {
595 if { $message != "" } {
596 fail "$message";
597 }
598 gdb_suppress_entire_file "GDB died";
2307bd6a 599 set result -1;
c906108c 600 }
9e0b60a8 601 -re "Ending remote debugging.*$gdb_prompt $" {
c906108c
SS
602 if ![isnative] then {
603 warning "Can`t communicate to remote target."
604 }
605 gdb_exit
606 gdb_start
607 set result -1
608 }
2307bd6a
DJ
609 }
610 append code $processed_code
611 append code {
9e0b60a8 612 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
c906108c 613 perror "Undefined command \"$command\"."
9e0b60a8 614 fail "$message"
c906108c
SS
615 set result 1
616 }
617 -re "Ambiguous command.*$gdb_prompt $" {
618 perror "\"$command\" is not a unique command name."
9e0b60a8 619 fail "$message"
c906108c
SS
620 set result 1
621 }
622 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
623 if ![string match "" $message] then {
ed4c619a 624 set errmsg "$message (the program exited)"
c906108c 625 } else {
ed4c619a 626 set errmsg "$command (the program exited)"
c906108c
SS
627 }
628 fail "$errmsg"
2307bd6a 629 set result -1
cb9a9d3e
MS
630 }
631 -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" {
632 if ![string match "" $message] then {
ed4c619a 633 set errmsg "$message (the program exited)"
cb9a9d3e 634 } else {
ed4c619a 635 set errmsg "$command (the program exited)"
cb9a9d3e
MS
636 }
637 fail "$errmsg"
2307bd6a 638 set result -1
c906108c
SS
639 }
640 -re "The program is not being run.*$gdb_prompt $" {
641 if ![string match "" $message] then {
ed4c619a 642 set errmsg "$message (the program is no longer running)"
c906108c 643 } else {
ed4c619a 644 set errmsg "$command (the program is no longer running)"
c906108c
SS
645 }
646 fail "$errmsg"
2307bd6a 647 set result -1
c906108c
SS
648 }
649 -re ".*$gdb_prompt $" {
650 if ![string match "" $message] then {
651 fail "$message"
652 }
653 set result 1
654 }
655 "<return>" {
656 send_gdb "\n"
657 perror "Window too small."
9e0b60a8 658 fail "$message"
2307bd6a 659 set result -1
c906108c
SS
660 }
661 -re "\\(y or n\\) " {
662 send_gdb "n\n"
663 perror "Got interactive prompt."
9e0b60a8 664 fail "$message"
2307bd6a 665 set result -1
c906108c
SS
666 }
667 eof {
668 perror "Process no longer exists"
669 if { $message != "" } {
670 fail "$message"
671 }
672 return -1
673 }
674 full_buffer {
675 perror "internal buffer is full."
9e0b60a8 676 fail "$message"
2307bd6a 677 set result -1
c906108c
SS
678 }
679 timeout {
680 if ![string match "" $message] then {
681 fail "$message (timeout)"
682 }
683 set result 1
684 }
685 }
2307bd6a
DJ
686
687 set result 0
688 gdb_expect $tmt $code
c906108c
SS
689 return $result
690}
2307bd6a
DJ
691
692# gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
693# Send a command to gdb; test the result.
694#
695# COMMAND is the command to execute, send to GDB with send_gdb. If
696# this is the null string no command is sent.
697# PATTERN is the pattern to match for a PASS, and must NOT include
698# the \r\n sequence immediately before the gdb prompt.
699# MESSAGE is an optional message to be printed. If this is
700# omitted, then the pass/fail messages use the command string as the
701# message. (If this is the empty string, then sometimes we don't
702# call pass or fail at all; I don't understand this at all.)
703# QUESTION is a question GDB may ask in response to COMMAND, like
704# "are you sure?"
705# RESPONSE is the response to send if QUESTION appears.
706#
707# Returns:
708# 1 if the test failed,
709# 0 if the test passes,
710# -1 if there was an internal error.
711#
712proc gdb_test { args } {
713 global verbose
714 global gdb_prompt
715 global GDB
716 upvar timeout timeout
717
718 if [llength $args]>2 then {
719 set message [lindex $args 2]
720 } else {
721 set message [lindex $args 0]
722 }
723 set command [lindex $args 0]
724 set pattern [lindex $args 1]
725
726 if [llength $args]==5 {
727 set question_string [lindex $args 3];
728 set response_string [lindex $args 4];
729 } else {
730 set question_string "^FOOBAR$"
731 }
732
733 return [gdb_test_multiple $command $message {
734 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
735 if ![string match "" $message] then {
736 pass "$message"
737 }
738 }
739 -re "(${question_string})$" {
740 send_gdb "$response_string\n";
741 exp_continue;
742 }
743 }]
744}
c906108c
SS
745\f
746# Test that a command gives an error. For pass or fail, return
747# a 1 to indicate that more tests can proceed. However a timeout
748# is a serious error, generates a special fail message, and causes
749# a 0 to be returned to indicate that more tests are likely to fail
750# as well.
751
752proc test_print_reject { args } {
753 global gdb_prompt
754 global verbose
755
756 if [llength $args]==2 then {
757 set expectthis [lindex $args 1]
758 } else {
759 set expectthis "should never match this bogus string"
760 }
761 set sendthis [lindex $args 0]
762 if $verbose>2 then {
763 send_user "Sending \"$sendthis\" to gdb\n"
764 send_user "Looking to match \"$expectthis\"\n"
765 }
766 send_gdb "$sendthis\n"
767 #FIXME: Should add timeout as parameter.
768 gdb_expect {
769 -re "A .* in expression.*\\.*$gdb_prompt $" {
770 pass "reject $sendthis"
771 return 1
772 }
773 -re "Invalid syntax in expression.*$gdb_prompt $" {
774 pass "reject $sendthis"
775 return 1
776 }
777 -re "Junk after end of expression.*$gdb_prompt $" {
778 pass "reject $sendthis"
779 return 1
780 }
781 -re "Invalid number.*$gdb_prompt $" {
782 pass "reject $sendthis"
783 return 1
784 }
785 -re "Invalid character constant.*$gdb_prompt $" {
786 pass "reject $sendthis"
787 return 1
788 }
789 -re "No symbol table is loaded.*$gdb_prompt $" {
790 pass "reject $sendthis"
791 return 1
792 }
793 -re "No symbol .* in current context.*$gdb_prompt $" {
794 pass "reject $sendthis"
795 return 1
796 }
c4b7bc2b
JB
797 -re "Unmatched single quote.*$gdb_prompt $" {
798 pass "reject $sendthis"
799 return 1
800 }
801 -re "A character constant must contain at least one character.*$gdb_prompt $" {
802 pass "reject $sendthis"
803 return 1
804 }
c906108c
SS
805 -re "$expectthis.*$gdb_prompt $" {
806 pass "reject $sendthis"
807 return 1
808 }
809 -re ".*$gdb_prompt $" {
810 fail "reject $sendthis"
811 return 1
812 }
813 default {
814 fail "reject $sendthis (eof or timeout)"
815 return 0
816 }
817 }
818}
819\f
820# Given an input string, adds backslashes as needed to create a
821# regexp that will match the string.
822
823proc string_to_regexp {str} {
824 set result $str
825 regsub -all {[]*+.|()^$\[]} $str {\\&} result
826 return $result
827}
828
829# Same as gdb_test, but the second parameter is not a regexp,
830# but a string that must match exactly.
831
832proc gdb_test_exact { args } {
833 upvar timeout timeout
834
835 set command [lindex $args 0]
836
837 # This applies a special meaning to a null string pattern. Without
838 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
839 # messages from commands that should have no output except a new
840 # prompt. With this, only results of a null string will match a null
841 # string pattern.
842
843 set pattern [lindex $args 1]
844 if [string match $pattern ""] {
845 set pattern [string_to_regexp [lindex $args 0]]
846 } else {
847 set pattern [string_to_regexp [lindex $args 1]]
848 }
849
850 # It is most natural to write the pattern argument with only
851 # embedded \n's, especially if you are trying to avoid Tcl quoting
852 # problems. But gdb_expect really wants to see \r\n in patterns. So
853 # transform the pattern here. First transform \r\n back to \n, in
854 # case some users of gdb_test_exact already do the right thing.
855 regsub -all "\r\n" $pattern "\n" pattern
856 regsub -all "\n" $pattern "\r\n" pattern
857 if [llength $args]==3 then {
858 set message [lindex $args 2]
859 } else {
860 set message $command
861 }
862
863 return [gdb_test $command $pattern $message]
864}
865\f
866proc gdb_reinitialize_dir { subdir } {
867 global gdb_prompt
868
869 if [is_remote host] {
870 return "";
871 }
872 send_gdb "dir\n"
873 gdb_expect 60 {
874 -re "Reinitialize source path to empty.*y or n. " {
875 send_gdb "y\n"
876 gdb_expect 60 {
877 -re "Source directories searched.*$gdb_prompt $" {
878 send_gdb "dir $subdir\n"
879 gdb_expect 60 {
880 -re "Source directories searched.*$gdb_prompt $" {
881 verbose "Dir set to $subdir"
882 }
883 -re "$gdb_prompt $" {
884 perror "Dir \"$subdir\" failed."
885 }
886 }
887 }
888 -re "$gdb_prompt $" {
889 perror "Dir \"$subdir\" failed."
890 }
891 }
892 }
893 -re "$gdb_prompt $" {
894 perror "Dir \"$subdir\" failed."
895 }
896 }
897}
898
899#
900# gdb_exit -- exit the GDB, killing the target program if necessary
901#
902proc default_gdb_exit {} {
903 global GDB
904 global GDBFLAGS
905 global verbose
906 global gdb_spawn_id;
907
908 gdb_stop_suppressing_tests;
909
910 if ![info exists gdb_spawn_id] {
911 return;
912 }
913
914 verbose "Quitting $GDB $GDBFLAGS"
915
916 if { [is_remote host] && [board_info host exists fileid] } {
917 send_gdb "quit\n";
918 gdb_expect 10 {
919 -re "y or n" {
920 send_gdb "y\n";
921 exp_continue;
922 }
923 -re "DOSEXIT code" { }
924 default { }
925 }
926 }
927
928 if ![is_remote host] {
929 remote_close host;
930 }
931 unset gdb_spawn_id
932}
933
934#
935# load a file into the debugger.
936# return a -1 if anything goes wrong.
937#
938proc gdb_file_cmd { arg } {
939 global verbose
940 global loadpath
941 global loadfile
942 global GDB
943 global gdb_prompt
944 upvar timeout timeout
945
946 if [is_remote host] {
947 set arg [remote_download host $arg];
948 if { $arg == "" } {
949 error "download failed"
950 return -1;
951 }
952 }
953
954 send_gdb "file $arg\n"
955 gdb_expect 120 {
956 -re "Reading symbols from.*done.*$gdb_prompt $" {
957 verbose "\t\tLoaded $arg into the $GDB"
958 return 0
959 }
960 -re "has no symbol-table.*$gdb_prompt $" {
961 perror "$arg wasn't compiled with \"-g\""
962 return -1
963 }
964 -re "A program is being debugged already.*Kill it.*y or n. $" {
965 send_gdb "y\n"
966 verbose "\t\tKilling previous program being debugged"
967 exp_continue
968 }
969 -re "Load new symbol table from \".*\".*y or n. $" {
970 send_gdb "y\n"
971 gdb_expect 120 {
972 -re "Reading symbols from.*done.*$gdb_prompt $" {
973 verbose "\t\tLoaded $arg with new symbol table into $GDB"
974 return 0
975 }
976 timeout {
977 perror "(timeout) Couldn't load $arg, other program already loaded."
978 return -1
979 }
980 }
981 }
982 -re "No such file or directory.*$gdb_prompt $" {
983 perror "($arg) No such file or directory\n"
984 return -1
985 }
986 -re "$gdb_prompt $" {
987 perror "couldn't load $arg into $GDB."
988 return -1
989 }
990 timeout {
991 perror "couldn't load $arg into $GDB (timed out)."
992 return -1
993 }
994 eof {
995 # This is an attempt to detect a core dump, but seems not to
996 # work. Perhaps we need to match .* followed by eof, in which
997 # gdb_expect does not seem to have a way to do that.
998 perror "couldn't load $arg into $GDB (end of file)."
999 return -1
1000 }
1001 }
1002}
1003
1004#
1005# start gdb -- start gdb running, default procedure
1006#
1007# When running over NFS, particularly if running many simultaneous
1008# tests on different hosts all using the same server, things can
1009# get really slow. Give gdb at least 3 minutes to start up.
1010#
1011proc default_gdb_start { } {
1012 global verbose
1013 global GDB
1014 global GDBFLAGS
1015 global gdb_prompt
1016 global timeout
1017 global gdb_spawn_id;
1018
1019 gdb_stop_suppressing_tests;
1020
1021 verbose "Spawning $GDB -nw $GDBFLAGS"
1022
1023 if [info exists gdb_spawn_id] {
1024 return 0;
1025 }
1026
1027 if ![is_remote host] {
1028 if { [which $GDB] == 0 } then {
1029 perror "$GDB does not exist."
1030 exit 1
1031 }
1032 }
1033 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
1034 if { $res < 0 || $res == "" } {
1035 perror "Spawning $GDB failed."
1036 return 1;
1037 }
1038 gdb_expect 360 {
1039 -re "\[\r\n\]$gdb_prompt $" {
1040 verbose "GDB initialized."
1041 }
1042 -re "$gdb_prompt $" {
1043 perror "GDB never initialized."
1044 return -1
1045 }
1046 timeout {
1047 perror "(timeout) GDB never initialized after 10 seconds."
1048 remote_close host;
1049 return -1
1050 }
1051 }
1052 set gdb_spawn_id -1;
1053 # force the height to "unlimited", so no pagers get used
1054
1055 send_gdb "set height 0\n"
1056 gdb_expect 10 {
1057 -re "$gdb_prompt $" {
1058 verbose "Setting height to 0." 2
1059 }
1060 timeout {
1061 warning "Couldn't set the height to 0"
1062 }
1063 }
1064 # force the width to "unlimited", so no wraparound occurs
1065 send_gdb "set width 0\n"
1066 gdb_expect 10 {
1067 -re "$gdb_prompt $" {
1068 verbose "Setting width to 0." 2
1069 }
1070 timeout {
1071 warning "Couldn't set the width to 0."
1072 }
1073 }
1074 return 0;
1075}
1076
d4f3574e
SS
1077# Return a 1 for configurations for which we don't even want to try to
1078# test C++.
1079
1080proc skip_cplus_tests {} {
1081 if { [istarget "d10v-*-*"] } {
1082 return 1
1083 }
1084 if { [istarget "h8300-*-*"] } {
1085 return 1
1086 }
1146c7f1
SC
1087
1088 # The C++ IO streams are too large for HC11/HC12 and are thus not
1089 # available. The gdb C++ tests use them and don't compile.
1090 if { [istarget "m6811-*-*"] } {
1091 return 1
1092 }
1093 if { [istarget "m6812-*-*"] } {
1094 return 1
1095 }
d4f3574e
SS
1096 return 0
1097}
1098
7a292a7a
SS
1099# Skip all the tests in the file if you are not on an hppa running
1100# hpux target.
1101
1102proc skip_hp_tests {} {
1103 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
c906108c
SS
1104 verbose "Skip hp tests is $skip_hp"
1105 return $skip_hp
1106}
1107
94b8e876
MC
1108set compiler_info "unknown"
1109set gcc_compiled 0
1110set hp_cc_compiler 0
1111set hp_aCC_compiler 0
1112set signed_keyword_not_used 0
1113
1114# Figure out what compiler I am using.
1115#
1116# BINFILE is a "compiler information" output file. This implementation
1117# does not use BINFILE.
1118#
1119# ARGS can be empty or "C++". If empty, "C" is assumed.
1120#
1121# There are several ways to do this, with various problems.
1122#
1123# [ gdb_compile -E $ifile -o $binfile.ci ]
1124# source $binfile.ci
1125#
1126# Single Unix Spec v3 says that "-E -o ..." together are not
1127# specified. And in fact, the native compiler on hp-ux 11 (among
1128# others) does not work with "-E -o ...". Most targets used to do
1129# this, and it mostly worked, because it works with gcc.
1130#
1131# [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1132# source $binfile.ci
1133#
1134# This avoids the problem with -E and -o together. This almost works
1135# if the build machine is the same as the host machine, which is
1136# usually true of the targets which are not gcc. But this code does
1137# not figure which compiler to call, and it always ends up using the C
1138# compiler. Not good for setting hp_aCC_compiler. Targets
1139# hppa*-*-hpux* and mips*-*-irix* used to do this.
1140#
1141# [ gdb_compile -E $ifile > $binfile.ci ]
1142# source $binfile.ci
1143#
1144# dejagnu target_compile says that it supports output redirection,
1145# but the code is completely different from the normal path and I
1146# don't want to sweep the mines from that path. So I didn't even try
1147# this.
1148#
1149# set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1150# eval $cppout
1151#
1152# I actually do this for all targets now. gdb_compile runs the right
1153# compiler, and TCL captures the output, and I eval the output.
1154#
1155# Unfortunately, expect logs the output of the command as it goes by,
1156# and dejagnu helpfully prints a second copy of it right afterwards.
1157# So I turn off expect logging for a moment.
1158#
1159# [ gdb_compile $ifile $ciexe_file executable $args ]
1160# [ remote_exec $ciexe_file ]
1161# [ source $ci_file.out ]
1162#
1163# I could give up on -E and just do this.
1164# I didn't get desperate enough to try this.
1165#
1166# -- chastain 2004-01-06
853d6e5b 1167
c906108c 1168proc get_compiler_info {binfile args} {
94b8e876 1169 # For compiler.c and compiler.cc
c906108c 1170 global srcdir
94b8e876
MC
1171
1172 # I am going to play with the log to keep noise out.
1173 global outdir
1174 global tool
1175
1176 # These come from compiler.c or compiler.cc
853d6e5b 1177 global compiler_info
94b8e876
MC
1178 global gcc_compiled
1179 global hp_cc_compiler
1180 global hp_aCC_compiler
1181 global signed_keyword_not_used
c906108c 1182
94b8e876
MC
1183 # Choose which file to preprocess.
1184 set ifile "${srcdir}/lib/compiler.c"
1185 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1186 set ifile "${srcdir}/lib/compiler.cc"
c906108c 1187 }
085dd6e6 1188
94b8e876
MC
1189 # Run $ifile through the right preprocessor.
1190 # Toggle gdb.log to keep the compiler output out of the log.
1191 log_file
1192 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1193 log_file -a "$outdir/$tool.log"
1194
1195 # Source the output.
1196 foreach cppline [ split "$cppout" "\n" ] {
1197 if { ! [ regexp "^#" "$cppline" ] } {
1198 if { ! [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1199 verbose "get_compiler_info: $cppline" 2
1200 eval "$cppline"
1201 }
1202 }
085dd6e6 1203 }
94b8e876 1204 verbose -log "get_compiler_info: $compiler_info"
085dd6e6
JM
1205
1206 # Most compilers will evaluate comparisons and other boolean
1207 # operations to 0 or 1.
1208 uplevel \#0 { set true 1 }
1209 uplevel \#0 { set false 0 }
1210
94b8e876
MC
1211 # Use of aCC results in boolean results being displayed as
1212 # "true" or "false"
1213 if { $hp_aCC_compiler } {
1214 uplevel \#0 { set true true }
1215 uplevel \#0 { set false false }
085dd6e6
JM
1216 }
1217
c906108c
SS
1218 return 0;
1219}
1220
853d6e5b
AC
1221proc test_compiler_info { compiler } {
1222 global compiler_info
1223 return [string match $compiler $compiler_info]
1224}
1225
f1c47eb2
MS
1226set gdb_wrapper_initialized 0
1227
1228proc gdb_wrapper_init { args } {
1229 global gdb_wrapper_initialized;
1230 global gdb_wrapper_file;
1231 global gdb_wrapper_flags;
1232
1233 if { $gdb_wrapper_initialized == 1 } { return; }
1234
1235 if {[target_info exists needs_status_wrapper] && \
277254ba 1236 [target_info needs_status_wrapper] != "0"} {
f1c47eb2
MS
1237 set result [build_wrapper "testglue.o"];
1238 if { $result != "" } {
1239 set gdb_wrapper_file [lindex $result 0];
1240 set gdb_wrapper_flags [lindex $result 1];
1241 } else {
1242 warning "Status wrapper failed to build."
1243 }
1244 }
1245 set gdb_wrapper_initialized 1
1246}
1247
c906108c
SS
1248proc gdb_compile {source dest type options} {
1249 global GDB_TESTCASE_OPTIONS;
f1c47eb2
MS
1250 global gdb_wrapper_file;
1251 global gdb_wrapper_flags;
1252 global gdb_wrapper_initialized;
c906108c
SS
1253
1254 if [target_info exists gdb_stub] {
1255 set options2 { "additional_flags=-Dusestubs" }
1256 lappend options "libs=[target_info gdb_stub]";
1257 set options [concat $options2 $options]
1258 }
1259 if [target_info exists is_vxworks] {
1260 set options2 { "additional_flags=-Dvxworks" }
1261 lappend options "libs=[target_info gdb_stub]";
1262 set options [concat $options2 $options]
1263 }
1264 if [info exists GDB_TESTCASE_OPTIONS] {
1265 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1266 }
1267 verbose "options are $options"
1268 verbose "source is $source $dest $type $options"
1269
f1c47eb2
MS
1270 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
1271
1272 if {[target_info exists needs_status_wrapper] && \
1273 [target_info needs_status_wrapper] != "0" && \
1274 [info exists gdb_wrapper_file]} {
1275 lappend options "libs=${gdb_wrapper_file}"
1276 lappend options "ldflags=${gdb_wrapper_flags}"
1277 }
1278
c906108c
SS
1279 set result [target_compile $source $dest $type $options];
1280 regsub "\[\r\n\]*$" "$result" "" result;
1281 regsub "^\[\r\n\]*" "$result" "" result;
b5ab8ff3 1282 if { $result != "" && [lsearch $options quiet] == -1} {
c906108c
SS
1283 clone_output "gdb compile failed, $result"
1284 }
1285 return $result;
1286}
1287
b6ff0e81
JB
1288
1289# This is just like gdb_compile, above, except that it tries compiling
1290# against several different thread libraries, to see which one this
1291# system has.
1292proc gdb_compile_pthreads {source dest type options} {
0ae67eb3 1293 set built_binfile 0
b6ff0e81
JB
1294 set why_msg "unrecognized error"
1295 foreach lib {-lpthreads -lpthread -lthread} {
1296 # This kind of wipes out whatever libs the caller may have
1297 # set. Or maybe theirs will override ours. How infelicitous.
b5ab8ff3 1298 set options_with_lib [concat $options [list libs=$lib quiet]]
b6ff0e81
JB
1299 set ccout [gdb_compile $source $dest $type $options_with_lib]
1300 switch -regexp -- $ccout {
1301 ".*no posix threads support.*" {
1302 set why_msg "missing threads include file"
1303 break
1304 }
1305 ".*cannot open -lpthread.*" {
1306 set why_msg "missing runtime threads library"
1307 }
1308 ".*Can't find library for -lpthread.*" {
1309 set why_msg "missing runtime threads library"
1310 }
1311 {^$} {
1312 pass "successfully compiled posix threads test case"
1313 set built_binfile 1
1314 break
1315 }
1316 }
1317 }
0ae67eb3 1318 if {!$built_binfile} {
b6ff0e81
JB
1319 unsupported "Couldn't compile $source: ${why_msg}"
1320 return -1
1321 }
1322}
1323
130cacce
AF
1324# This is just like gdb_compile_pthreads, above, except that we always add the
1325# objc library for compiling Objective-C programs
1326proc gdb_compile_objc {source dest type options} {
1327 set built_binfile 0
1328 set why_msg "unrecognized error"
1329 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
1330 # This kind of wipes out whatever libs the caller may have
1331 # set. Or maybe theirs will override ours. How infelicitous.
1332 if { $lib == "solaris" } {
1333 set lib "-lpthread -lposix4"
1334 }
1335 if { $lib != "-lobjc" } {
1336 set lib "-lobjc $lib"
1337 }
1338 set options_with_lib [concat $options [list libs=$lib quiet]]
1339 set ccout [gdb_compile $source $dest $type $options_with_lib]
1340 switch -regexp -- $ccout {
1341 ".*no posix threads support.*" {
1342 set why_msg "missing threads include file"
1343 break
1344 }
1345 ".*cannot open -lpthread.*" {
1346 set why_msg "missing runtime threads library"
1347 }
1348 ".*Can't find library for -lpthread.*" {
1349 set why_msg "missing runtime threads library"
1350 }
1351 {^$} {
1352 pass "successfully compiled objc with posix threads test case"
1353 set built_binfile 1
1354 break
1355 }
1356 }
1357 }
1358 if {!$built_binfile} {
1359 unsupported "Couldn't compile $source: ${why_msg}"
1360 return -1
1361 }
1362}
1363
c906108c
SS
1364proc send_gdb { string } {
1365 global suppress_flag;
1366 if { $suppress_flag } {
1367 return "suppressed";
1368 }
1369 return [remote_send host "$string"];
1370}
1371
1372#
1373#
1374
1375proc gdb_expect { args } {
1376 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
1377 set gtimeout [lindex $args 0];
1378 set expcode [list [lindex $args 1]];
1379 } else {
1380 upvar timeout timeout;
1381
1382 set expcode $args;
1383 if [target_info exists gdb,timeout] {
1384 if [info exists timeout] {
1385 if { $timeout < [target_info gdb,timeout] } {
1386 set gtimeout [target_info gdb,timeout];
1387 } else {
1388 set gtimeout $timeout;
1389 }
1390 } else {
1391 set gtimeout [target_info gdb,timeout];
1392 }
1393 }
1394
1395 if ![info exists gtimeout] {
1396 global timeout;
1397 if [info exists timeout] {
1398 set gtimeout $timeout;
1399 } else {
1400 # Eeeeew.
1401 set gtimeout 60;
1402 }
1403 }
1404 }
1405 global suppress_flag;
1406 global remote_suppress_flag;
1407 if [info exists remote_suppress_flag] {
1408 set old_val $remote_suppress_flag;
1409 }
1410 if [info exists suppress_flag] {
1411 if { $suppress_flag } {
1412 set remote_suppress_flag 1;
1413 }
1414 }
a0b3c4fd 1415 set code [catch \
5f279fa6 1416 {uplevel remote_expect host $gtimeout $expcode} string];
c906108c
SS
1417 if [info exists old_val] {
1418 set remote_suppress_flag $old_val;
1419 } else {
1420 if [info exists remote_suppress_flag] {
1421 unset remote_suppress_flag;
1422 }
1423 }
1424
1425 if {$code == 1} {
1426 global errorInfo errorCode;
1427
1428 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1429 } elseif {$code == 2} {
1430 return -code return $string
1431 } elseif {$code == 3} {
1432 return
1433 } elseif {$code > 4} {
1434 return -code $code $string
1435 }
1436}
1437
c2d11a7d 1438# gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs
085dd6e6
JM
1439#
1440# Check for long sequence of output by parts.
11cf8741 1441# MESSAGE: is the test message to be printed with the test success/fail.
085dd6e6
JM
1442# SENTINEL: Is the terminal pattern indicating that output has finished.
1443# LIST: is the sequence of outputs to match.
1444# If the sentinel is recognized early, it is considered an error.
1445#
11cf8741
JM
1446# Returns:
1447# 1 if the test failed,
1448# 0 if the test passes,
1449# -1 if there was an internal error.
1450#
c2d11a7d 1451proc gdb_expect_list {test sentinel list} {
085dd6e6 1452 global gdb_prompt
11cf8741 1453 global suppress_flag
085dd6e6 1454 set index 0
43ff13b4 1455 set ok 1
11cf8741
JM
1456 if { $suppress_flag } {
1457 set ok 0
a20ce2c3 1458 unresolved "${test}"
11cf8741 1459 }
43ff13b4 1460 while { ${index} < [llength ${list}] } {
085dd6e6
JM
1461 set pattern [lindex ${list} ${index}]
1462 set index [expr ${index} + 1]
1463 if { ${index} == [llength ${list}] } {
43ff13b4
JM
1464 if { ${ok} } {
1465 gdb_expect {
c2d11a7d 1466 -re "${pattern}${sentinel}" {
a20ce2c3 1467 # pass "${test}, pattern ${index} + sentinel"
c2d11a7d
JM
1468 }
1469 -re "${sentinel}" {
a20ce2c3 1470 fail "${test} (pattern ${index} + sentinel)"
c2d11a7d 1471 set ok 0
43ff13b4 1472 }
5c5455dc
AC
1473 -re ".*A problem internal to GDB has been detected" {
1474 fail "${test} (GDB internal error)"
1475 set ok 0
1476 gdb_internal_error_resync
1477 }
43ff13b4 1478 timeout {
a20ce2c3 1479 fail "${test} (pattern ${index} + sentinel) (timeout)"
43ff13b4
JM
1480 set ok 0
1481 }
085dd6e6 1482 }
43ff13b4 1483 } else {
a20ce2c3 1484 # unresolved "${test}, pattern ${index} + sentinel"
085dd6e6
JM
1485 }
1486 } else {
43ff13b4
JM
1487 if { ${ok} } {
1488 gdb_expect {
1489 -re "${pattern}" {
a20ce2c3 1490 # pass "${test}, pattern ${index}"
43ff13b4 1491 }
c2d11a7d 1492 -re "${sentinel}" {
a20ce2c3 1493 fail "${test} (pattern ${index})"
43ff13b4
JM
1494 set ok 0
1495 }
5c5455dc
AC
1496 -re ".*A problem internal to GDB has been detected" {
1497 fail "${test} (GDB internal error)"
1498 set ok 0
1499 gdb_internal_error_resync
1500 }
43ff13b4 1501 timeout {
a20ce2c3 1502 fail "${test} (pattern ${index}) (timeout)"
43ff13b4
JM
1503 set ok 0
1504 }
085dd6e6 1505 }
43ff13b4 1506 } else {
a20ce2c3 1507 # unresolved "${test}, pattern ${index}"
085dd6e6
JM
1508 }
1509 }
1510 }
11cf8741 1511 if { ${ok} } {
a20ce2c3 1512 pass "${test}"
11cf8741
JM
1513 return 0
1514 } else {
1515 return 1
1516 }
085dd6e6
JM
1517}
1518
1519#
1520#
c906108c
SS
1521proc gdb_suppress_entire_file { reason } {
1522 global suppress_flag;
1523
1524 warning "$reason\n";
1525 set suppress_flag -1;
1526}
1527
1528#
1529# Set suppress_flag, which will cause all subsequent calls to send_gdb and
1530# gdb_expect to fail immediately (until the next call to
1531# gdb_stop_suppressing_tests).
1532#
1533proc gdb_suppress_tests { args } {
1534 global suppress_flag;
1535
1536 return; # fnf - disable pending review of results where
1537 # testsuite ran better without this
1538 incr suppress_flag;
1539
1540 if { $suppress_flag == 1 } {
1541 if { [llength $args] > 0 } {
1542 warning "[lindex $args 0]\n";
1543 } else {
1544 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1545 }
1546 }
1547}
1548
1549#
1550# Clear suppress_flag.
1551#
1552proc gdb_stop_suppressing_tests { } {
1553 global suppress_flag;
1554
1555 if [info exists suppress_flag] {
1556 if { $suppress_flag > 0 } {
1557 set suppress_flag 0;
1558 clone_output "Tests restarted.\n";
1559 }
1560 } else {
1561 set suppress_flag 0;
1562 }
1563}
1564
1565proc gdb_clear_suppressed { } {
1566 global suppress_flag;
1567
1568 set suppress_flag 0;
1569}
1570
1571proc gdb_start { } {
1572 default_gdb_start
1573}
1574
1575proc gdb_exit { } {
1576 catch default_gdb_exit
1577}
1578
1579#
1580# gdb_load -- load a file into the debugger.
1581# return a -1 if anything goes wrong.
1582#
1583proc gdb_load { arg } {
1584 return [gdb_file_cmd $arg]
1585}
1586
1587proc gdb_continue { function } {
1588 global decimal
1589
1590 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
1591}
1592
1593proc default_gdb_init { args } {
277254ba
MS
1594 global gdb_wrapper_initialized
1595
c906108c
SS
1596 gdb_clear_suppressed;
1597
277254ba
MS
1598 # Make sure that the wrapper is rebuilt
1599 # with the appropriate multilib option.
1600 set gdb_wrapper_initialized 0
1601
c906108c
SS
1602 # Uh, this is lame. Really, really, really lame. But there's this *one*
1603 # testcase that will fail in random places if we don't increase this.
1604 match_max -d 20000
1605
1606 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
1607 if { [llength $args] > 0 } {
1608 global pf_prefix
1609
1610 set file [lindex $args 0];
1611
1612 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
1613 }
1614 global gdb_prompt;
1615 if [target_info exists gdb_prompt] {
1616 set gdb_prompt [target_info gdb_prompt];
1617 } else {
1618 set gdb_prompt "\\(gdb\\)"
1619 }
1620}
1621
1622proc gdb_init { args } {
1623 return [eval default_gdb_init $args];
1624}
1625
1626proc gdb_finish { } {
1627 gdb_exit;
1628}
1629
1630global debug_format
7a292a7a 1631set debug_format "unknown"
c906108c
SS
1632
1633# Run the gdb command "info source" and extract the debugging format
1634# information from the output and save it in debug_format.
1635
1636proc get_debug_format { } {
1637 global gdb_prompt
1638 global verbose
1639 global expect_out
1640 global debug_format
1641
1642 set debug_format "unknown"
1643 send_gdb "info source\n"
1644 gdb_expect 10 {
919d772c 1645 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
c906108c
SS
1646 set debug_format $expect_out(1,string)
1647 verbose "debug format is $debug_format"
1648 return 1;
1649 }
1650 -re "No current source file.\r\n$gdb_prompt $" {
1651 perror "get_debug_format used when no current source file"
1652 return 0;
1653 }
1654 -re "$gdb_prompt $" {
1655 warning "couldn't check debug format (no valid response)."
1656 return 1;
1657 }
1658 timeout {
1659 warning "couldn't check debug format (timed out)."
1660 return 1;
1661 }
1662 }
1663}
1664
838ae6c4
JB
1665# Return true if FORMAT matches the debug format the current test was
1666# compiled with. FORMAT is a shell-style globbing pattern; it can use
1667# `*', `[...]', and so on.
1668#
1669# This function depends on variables set by `get_debug_format', above.
1670
1671proc test_debug_format {format} {
1672 global debug_format
1673
1674 return [expr [string match $format $debug_format] != 0]
1675}
1676
c906108c
SS
1677# Like setup_xfail, but takes the name of a debug format (DWARF 1,
1678# COFF, stabs, etc). If that format matches the format that the
1679# current test was compiled with, then the next test is expected to
1680# fail for any target. Returns 1 if the next test or set of tests is
1681# expected to fail, 0 otherwise (or if it is unknown). Must have
1682# previously called get_debug_format.
b55a4771 1683proc setup_xfail_format { format } {
838ae6c4 1684 set ret [test_debug_format $format];
b55a4771 1685
838ae6c4 1686 if {$ret} then {
b55a4771
MS
1687 setup_xfail "*-*-*"
1688 }
1689 return $ret;
1690}
c906108c
SS
1691
1692proc gdb_step_for_stub { } {
1693 global gdb_prompt;
1694
1695 if ![target_info exists gdb,use_breakpoint_for_stub] {
1696 if [target_info exists gdb_stub_step_command] {
1697 set command [target_info gdb_stub_step_command];
1698 } else {
1699 set command "step";
1700 }
1701 send_gdb "${command}\n";
1702 set tries 0;
1703 gdb_expect 60 {
1704 -re "(main.* at |.*in .*start).*$gdb_prompt" {
1705 return;
1706 }
1707 -re ".*$gdb_prompt" {
1708 incr tries;
1709 if { $tries == 5 } {
1710 fail "stepping out of breakpoint function";
1711 return;
1712 }
1713 send_gdb "${command}\n";
1714 exp_continue;
1715 }
1716 default {
1717 fail "stepping out of breakpoint function";
1718 return;
1719 }
1720 }
1721 }
1722 send_gdb "where\n";
1723 gdb_expect {
1724 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
1725 set file $expect_out(1,string);
1726 set linenum [expr $expect_out(2,string) + 1];
1727 set breakplace "${file}:${linenum}";
1728 }
1729 default {}
1730 }
1731 send_gdb "break ${breakplace}\n";
1732 gdb_expect 60 {
1733 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
1734 set breakpoint $expect_out(1,string);
1735 }
1736 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
1737 set breakpoint $expect_out(1,string);
1738 }
1739 default {}
1740 }
1741 send_gdb "continue\n";
1742 gdb_expect 60 {
1743 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
1744 gdb_test "delete $breakpoint" ".*" "";
1745 return;
1746 }
1747 default {}
1748 }
1749}
1750
1751### gdb_get_line_number TEXT [FILE]
1752###
1753### Search the source file FILE, and return the line number of a line
1754### containing TEXT. Use this function instead of hard-coding line
1755### numbers into your test script.
1756###
1757### Specifically, this function uses GDB's "search" command to search
1758### FILE for the first line containing TEXT, and returns its line
1759### number. Thus, FILE must be a source file, compiled into the
1760### executable you are running. If omitted, FILE defaults to the
1761### value of the global variable `srcfile'; most test scripts set
1762### `srcfile' appropriately at the top anyway.
1763###
1764### Use this function to keep your test scripts independent of the
1765### exact line numbering of the source file. Don't write:
1766###
1767### send_gdb "break 20"
1768###
1769### This means that if anyone ever edits your test's source file,
1770### your test could break. Instead, put a comment like this on the
1771### source file line you want to break at:
1772###
1773### /* breakpoint spot: frotz.exp: test name */
1774###
1775### and then write, in your test script (which we assume is named
1776### frotz.exp):
1777###
1778### send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
1779###
1780### (Yes, Tcl knows how to handle the nested quotes and brackets.
1781### Try this:
1782### $ tclsh
1783### % puts "foo [lindex "bar baz" 1]"
1784### foo baz
1785### %
1786### Tcl is quite clever, for a little stringy language.)
1787
1788proc gdb_get_line_number {text {file /omitted/}} {
1789 global gdb_prompt;
1790 global srcfile;
1791
1792 if {! [string compare $file /omitted/]} {
1793 set file $srcfile
1794 }
1795
1796 set result -1;
1797 gdb_test "list ${file}:1,1" ".*" ""
1798 send_gdb "search ${text}\n"
1799 gdb_expect {
1800 -re "\[\r\n\]+(\[0-9\]+)\[ \t\].*${text}.*$gdb_prompt $" {
1801 set result $expect_out(1,string)
1802 }
1803 -re ".*$gdb_prompt $" {
1804 fail "find line number containing \"${text}\""
1805 }
1806 timeout {
1807 fail "find line number containing \"${text}\" (timeout)"
1808 }
1809 }
1810 return $result;
1811}
1812
7a292a7a
SS
1813# gdb_continue_to_end:
1814# The case where the target uses stubs has to be handled specially. If a
1815# stub is used, we set a breakpoint at exit because we cannot rely on
1816# exit() behavior of a remote target.
1817#
1818# mssg is the error message that gets printed.
1819
1820proc gdb_continue_to_end {mssg} {
1821 if [target_info exists use_gdb_stub] {
1822 if {![gdb_breakpoint "exit"]} {
1823 return 0
1824 }
1825 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
1826 "continue until exit at $mssg"
1827 } else {
1828 # Continue until we exit. Should not stop again.
1829 # Don't bother to check the output of the program, that may be
1830 # extremely tough for some remote systems.
1831 gdb_test "continue"\
1c56143a 1832 "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
7a292a7a
SS
1833 "continue until exit at $mssg"
1834 }
1835}
1836
1837proc rerun_to_main {} {
1838 global gdb_prompt
1839
1840 if [target_info exists use_gdb_stub] {
1841 gdb_run_cmd
1842 gdb_expect {
1843 -re ".*Breakpoint .*main .*$gdb_prompt $"\
1844 {pass "rerun to main" ; return 0}
1845 -re "$gdb_prompt $"\
1846 {fail "rerun to main" ; return 0}
1847 timeout {fail "(timeout) rerun to main" ; return 0}
1848 }
1849 } else {
1850 send_gdb "run\n"
1851 gdb_expect {
11350d2a
CV
1852 -re "The program .* has been started already.*y or n. $" {
1853 send_gdb "y\n"
1854 exp_continue
1855 }
7a292a7a
SS
1856 -re "Starting program.*$gdb_prompt $"\
1857 {pass "rerun to main" ; return 0}
1858 -re "$gdb_prompt $"\
1859 {fail "rerun to main" ; return 0}
1860 timeout {fail "(timeout) rerun to main" ; return 0}
1861 }
1862 }
1863}
c906108c 1864
13a5e3b8
MS
1865# Print a message and return true if a test should be skipped
1866# due to lack of floating point suport.
1867
1868proc gdb_skip_float_test { msg } {
1869 if [target_info exists gdb,skip_float_tests] {
1870 verbose "Skipping test '$msg': no float tests.";
1871 return 1;
1872 }
1873 return 0;
1874}
1875
1876# Print a message and return true if a test should be skipped
1877# due to lack of stdio support.
1878
1879proc gdb_skip_stdio_test { msg } {
1880 if [target_info exists gdb,noinferiorio] {
1881 verbose "Skipping test '$msg': no inferior i/o.";
1882 return 1;
1883 }
1884 return 0;
1885}
1886
1887proc gdb_skip_bogus_test { msg } {
1888 return 0;
1889}
1890
1f8a6abb
EZ
1891
1892# Note: the procedure gdb_gnu_strip_debug will produce an executable called
1893# ${binfile}.dbglnk, which is just like the executable ($binfile) but without
1894# the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
1895# the name of a idebuginfo only file. This file will be stored in the
1896# gdb.base/.debug subdirectory.
1897
1898# Functions for separate debug info testing
1899
1900# starting with an executable:
1901# foo --> original executable
1902
1903# at the end of the process we have:
1904# foo.stripped --> foo w/o debug info
1905# .debug/foo.debug --> foo's debug info
1906# foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
1907
1908# Return the name of the file in which we should stor EXEC's separated
1909# debug info. EXEC contains the full path.
1910proc separate_debug_filename { exec } {
1911
1912 # In a .debug subdirectory off the same directory where the testcase
1913 # executable is going to be. Something like:
1914 # <your-path>/gdb/testsuite/gdb.base/.debug/blah.debug.
1915 # This is the default location where gdb expects to findi
1916 # the debug info file.
1917
1918 set exec_dir [file dirname $exec]
1919 set exec_file [file tail $exec]
1920 set debug_dir [file join $exec_dir ".debug"]
1921 set debug_file [file join $debug_dir "${exec_file}.debug"]
1922
1923 return $debug_file
1924}
1925
1926
1927proc gdb_gnu_strip_debug { dest } {
1928
1929 set debug_file [separate_debug_filename $dest]
1930 set strip_to_file_program strip
1931 set objcopy_program objcopy
1932
1933 # Make sure the directory that will hold the separated debug
1934 # info actually exists.
1935 set debug_dir [file dirname $debug_file]
1936 if {! [file isdirectory $debug_dir]} {
1937 file mkdir $debug_dir
1938 }
1939
1940 set debug_link [file tail $debug_file]
1941 set stripped_file "${dest}.stripped"
1942
1943 # Get rid of the debug info, and store result in stripped_file
1944 # something like gdb/testsuite/gdb.base/blah.stripped.
1945 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
1946 verbose "result is $result"
1947 verbose "output is $output"
1948 if {$result == 1} {
1949 return 1
1950 }
1951
1952 # Get rid of everything but the debug info, and store result in debug_file
1953 # This will be in the .debug subdirectory, see above.
1954 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
1955 verbose "result is $result"
1956 verbose "output is $output"
1957 if {$result == 1} {
1958 return 1
1959 }
1960
1961 # Link the two previous output files together, adding the .gnu_debuglink
1962 # section to the stripped_file, containing a pointer to the debug_file,
1963 # save the new file in dest.
1964 # This will be the regular executable filename, in the usual location.
1965 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
1966 verbose "result is $result"
1967 verbose "output is $output"
1968 if {$result == 1} {
1969 return 1
1970 }
1971
1972 return 0
1973}
1974