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