gdb/
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
CommitLineData
6aba47ca 1# Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
7b6bb8da
JB
2# 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
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
e22f8b7c 7# the Free Software Foundation; either version 3 of the License, or
c906108c 8# (at your option) any later version.
e22f8b7c 9#
c906108c
SS
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.
e22f8b7c 14#
c906108c 15# You should have received a copy of the GNU General Public License
e22f8b7c 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
c906108c 17
c906108c
SS
18# This file was written by Fred Fish. (fnf@cygnus.com)
19
20# Generic gdb subroutines that should work for any target. If these
21# need to be modified for any target, it can be done with a variable
22# or by passing arguments.
23
97c3f1f3
JK
24if {$tool == ""} {
25 # Tests would fail, logs on get_compiler_info() would be missing.
26 send_error "`site.exp' not found, run `make site.exp'!\n"
27 exit 2
28}
29
c906108c
SS
30load_lib libgloss.exp
31
32global GDB
c906108c
SS
33
34if [info exists TOOL_EXECUTABLE] {
35 set GDB $TOOL_EXECUTABLE;
36}
37if ![info exists GDB] {
38 if ![is_remote host] {
39 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
40 } else {
41 set GDB [transform gdb];
42 }
43}
44verbose "using GDB = $GDB" 2
45
6b8ce727
DE
46# GDBFLAGS is available for the user to set on the command line.
47# E.g. make check RUNTESTFLAGS=GDBFLAGS=mumble
48# Testcases may use it to add additional flags, but they must:
49# - append new flags, not overwrite
50# - restore the original value when done
c906108c
SS
51global GDBFLAGS
52if ![info exists GDBFLAGS] {
6b8ce727 53 set GDBFLAGS ""
c906108c
SS
54}
55verbose "using GDBFLAGS = $GDBFLAGS" 2
56
6b8ce727 57# INTERNAL_GDBFLAGS contains flags that the testsuite requires.
1be00882
DE
58global INTERNAL_GDBFLAGS
59if ![info exists INTERNAL_GDBFLAGS] {
aae1c79a 60 set INTERNAL_GDBFLAGS "-nw -nx -data-directory [pwd]/../data-directory"
1be00882 61}
6b8ce727 62
9e0b60a8
JM
63# The variable gdb_prompt is a regexp which matches the gdb prompt.
64# Set it if it is not already set.
c906108c 65global gdb_prompt
9e0b60a8 66if ![info exists gdb_prompt] then {
c906108c
SS
67 set gdb_prompt "\[(\]gdb\[)\]"
68}
69
6006a3a1
BR
70# The variable fullname_syntax_POSIX is a regexp which matches a POSIX
71# absolute path ie. /foo/
d0b76dc6 72set fullname_syntax_POSIX {/[^\n]*/}
6006a3a1
BR
73# The variable fullname_syntax_UNC is a regexp which matches a Windows
74# UNC path ie. \\D\foo\
d0b76dc6 75set fullname_syntax_UNC {\\\\[^\\]+\\[^\n]+\\}
6006a3a1
BR
76# The variable fullname_syntax_DOS_CASE is a regexp which matches a
77# particular DOS case that GDB most likely will output
78# ie. \foo\, but don't match \\.*\
d0b76dc6 79set fullname_syntax_DOS_CASE {\\[^\\][^\n]*\\}
6006a3a1
BR
80# The variable fullname_syntax_DOS is a regexp which matches a DOS path
81# ie. a:\foo\ && a:foo\
d0b76dc6 82set fullname_syntax_DOS {[a-zA-Z]:[^\n]*\\}
6006a3a1
BR
83# The variable fullname_syntax is a regexp which matches what GDB considers
84# an absolute path. It is currently debatable if the Windows style paths
85# d:foo and \abc should be considered valid as an absolute path.
86# Also, the purpse of this regexp is not to recognize a well formed
87# absolute path, but to say with certainty that a path is absolute.
88set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
89
93076499
ND
90# Needed for some tests under Cygwin.
91global EXEEXT
92global env
93
94if ![info exists env(EXEEXT)] {
95 set EXEEXT ""
96} else {
97 set EXEEXT $env(EXEEXT)
98}
99
bb2bed55
NR
100set octal "\[0-7\]+"
101
eceb0c5f 102set inferior_exited_re "(\\\[Inferior \[0-9\]+ \\(.*\\) exited)"
fda326dd 103
085dd6e6
JM
104### Only procedures should come after this point.
105
c906108c
SS
106#
107# gdb_version -- extract and print the version number of GDB
108#
109proc default_gdb_version {} {
110 global GDB
6b8ce727 111 global INTERNAL_GDBFLAGS GDBFLAGS
c906108c 112 global gdb_prompt
fa335448 113 set output [remote_exec host "$GDB $INTERNAL_GDBFLAGS --version"]
c906108c
SS
114 set tmp [lindex $output 1];
115 set version ""
116 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
117 if ![is_remote host] {
6b8ce727 118 clone_output "[which $GDB] version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
c906108c 119 } else {
6b8ce727 120 clone_output "$GDB on remote host version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
c906108c
SS
121 }
122}
123
124proc gdb_version { } {
125 return [default_gdb_version];
126}
127
128#
129# gdb_unload -- unload a file if one is loaded
130#
131
132proc gdb_unload {} {
133 global verbose
134 global GDB
135 global gdb_prompt
136 send_gdb "file\n"
137 gdb_expect 60 {
138 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
139 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
959e7469
PM
140 -re "A program is being debugged already.*Are you sure you want to change the file.*y or n. $" {
141 send_gdb "y\n"
c906108c
SS
142 exp_continue
143 }
144 -re "Discard symbol table from .*y or n.*$" {
145 send_gdb "y\n"
146 exp_continue
147 }
148 -re "$gdb_prompt $" {}
149 timeout {
150 perror "couldn't unload file in $GDB (timed out)."
151 return -1
152 }
153 }
154}
155
156# Many of the tests depend on setting breakpoints at various places and
157# running until that breakpoint is reached. At times, we want to start
158# with a clean-slate with respect to breakpoints, so this utility proc
159# lets us do this without duplicating this code everywhere.
160#
161
162proc delete_breakpoints {} {
163 global gdb_prompt
164
a0b3c4fd
JM
165 # we need a larger timeout value here or this thing just confuses
166 # itself. May need a better implementation if possible. - guo
167 #
c906108c 168 send_gdb "delete breakpoints\n"
a0b3c4fd 169 gdb_expect 100 {
c906108c
SS
170 -re "Delete all breakpoints.*y or n.*$" {
171 send_gdb "y\n";
172 exp_continue
173 }
174 -re "$gdb_prompt $" { # This happens if there were no breakpoints
175 }
176 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
177 }
178 send_gdb "info breakpoints\n"
a0b3c4fd 179 gdb_expect 100 {
c906108c
SS
180 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
181 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
182 -re "Delete all breakpoints.*or n.*$" {
183 send_gdb "y\n";
184 exp_continue
185 }
186 timeout { perror "info breakpoints (timeout)" ; return }
187 }
188}
189
190
191#
192# Generic run command.
193#
194# The second pattern below matches up to the first newline *only*.
195# Using ``.*$'' could swallow up output that we attempt to match
196# elsewhere.
197#
198proc gdb_run_cmd {args} {
199 global gdb_prompt
200
201 if [target_info exists gdb_init_command] {
202 send_gdb "[target_info gdb_init_command]\n";
203 gdb_expect 30 {
204 -re "$gdb_prompt $" { }
205 default {
206 perror "gdb_init_command for target failed";
207 return;
208 }
209 }
210 }
211
212 if [target_info exists use_gdb_stub] {
213 if [target_info exists gdb,do_reload_on_run] {
b741e217 214 if { [gdb_reload] != 0 } {
917317f4
JM
215 return;
216 }
c906108c
SS
217 send_gdb "continue\n";
218 gdb_expect 60 {
219 -re "Continu\[^\r\n\]*\[\r\n\]" {}
220 default {}
221 }
222 return;
223 }
224
225 if [target_info exists gdb,start_symbol] {
226 set start [target_info gdb,start_symbol];
227 } else {
228 set start "start";
229 }
230 send_gdb "jump *$start\n"
917317f4
JM
231 set start_attempt 1;
232 while { $start_attempt } {
233 # Cap (re)start attempts at three to ensure that this loop
234 # always eventually fails. Don't worry about trying to be
235 # clever and not send a command when it has failed.
236 if [expr $start_attempt > 3] {
237 perror "Jump to start() failed (retry count exceeded)";
c906108c
SS
238 return;
239 }
917317f4
JM
240 set start_attempt [expr $start_attempt + 1];
241 gdb_expect 30 {
242 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
243 set start_attempt 0;
244 }
245 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
246 perror "Can't find start symbol to run in gdb_run";
247 return;
248 }
249 -re "No symbol \"start\" in current.*$gdb_prompt $" {
250 send_gdb "jump *_start\n";
251 }
252 -re "No symbol.*context.*$gdb_prompt $" {
253 set start_attempt 0;
254 }
255 -re "Line.* Jump anyway.*y or n. $" {
256 send_gdb "y\n"
257 }
258 -re "The program is not being run.*$gdb_prompt $" {
b741e217 259 if { [gdb_reload] != 0 } {
917317f4
JM
260 return;
261 }
262 send_gdb "jump *$start\n";
263 }
264 timeout {
265 perror "Jump to start() failed (timeout)";
266 return
267 }
c906108c 268 }
c906108c
SS
269 }
270 if [target_info exists gdb_stub] {
271 gdb_expect 60 {
272 -re "$gdb_prompt $" {
273 send_gdb "continue\n"
274 }
275 }
276 }
277 return
278 }
83f66e8f
DJ
279
280 if [target_info exists gdb,do_reload_on_run] {
b741e217 281 if { [gdb_reload] != 0 } {
83f66e8f
DJ
282 return;
283 }
284 }
c906108c
SS
285 send_gdb "run $args\n"
286# This doesn't work quite right yet.
5aa7ddc2
PM
287# Use -notransfer here so that test cases (like chng-sym.exp)
288# may test for additional start-up messages.
289 gdb_expect 60 {
c906108c
SS
290 -re "The program .* has been started already.*y or n. $" {
291 send_gdb "y\n"
292 exp_continue
293 }
bbb88ebf 294 -notransfer -re "Starting program: \[^\r\n\]*" {}
8e46892c
JK
295 -notransfer -re "$gdb_prompt $" {
296 # There is no more input expected.
297 }
c906108c
SS
298 }
299}
300
b741e217
DJ
301# Generic start command. Return 0 if we could start the program, -1
302# if we could not.
303
304proc gdb_start_cmd {args} {
305 global gdb_prompt
306
307 if [target_info exists gdb_init_command] {
308 send_gdb "[target_info gdb_init_command]\n";
309 gdb_expect 30 {
310 -re "$gdb_prompt $" { }
311 default {
312 perror "gdb_init_command for target failed";
313 return;
314 }
315 }
316 }
317
318 if [target_info exists use_gdb_stub] {
319 return -1
320 }
321
322 send_gdb "start $args\n"
2de75e71
JB
323 # Use -notransfer here so that test cases (like chng-sym.exp)
324 # may test for additional start-up messages.
b741e217
DJ
325 gdb_expect 60 {
326 -re "The program .* has been started already.*y or n. $" {
327 send_gdb "y\n"
328 exp_continue
329 }
b741e217
DJ
330 -notransfer -re "Starting program: \[^\r\n\]*" {
331 return 0
332 }
333 }
334 return -1
335}
336
78a1a894 337# Set a breakpoint at FUNCTION. If there is an additional argument it is
55cd6f92
DJ
338# a list of options; the supported options are allow-pending, temporary,
339# and no-message.
78a1a894
DJ
340
341proc gdb_breakpoint { function args } {
c906108c
SS
342 global gdb_prompt
343 global decimal
344
78a1a894
DJ
345 set pending_response n
346 if {[lsearch -exact [lindex $args 0] allow-pending] != -1} {
347 set pending_response y
348 }
349
e48883f7 350 set break_command "break"
18ac113b 351 set break_message "Breakpoint"
e48883f7
DJ
352 if {[lsearch -exact [lindex $args 0] temporary] != -1} {
353 set break_command "tbreak"
18ac113b 354 set break_message "Temporary breakpoint"
e48883f7
DJ
355 }
356
55cd6f92
DJ
357 set no_message 0
358 if {[lsearch -exact [lindex $args 0] no-message] != -1} {
359 set no_message 1
360 }
361
e48883f7 362 send_gdb "$break_command $function\n"
c906108c
SS
363 # The first two regexps are what we get with -g, the third is without -g.
364 gdb_expect 30 {
18ac113b
AR
365 -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
366 -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
367 -re "$break_message \[0-9\]* at .*$gdb_prompt $" {}
368 -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
78a1a894 369 if {$pending_response == "n"} {
55cd6f92
DJ
370 if { $no_message == 0 } {
371 fail "setting breakpoint at $function"
372 }
78a1a894
DJ
373 return 0
374 }
375 }
9f27c604 376 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
78a1a894 377 send_gdb "$pending_response\n"
14b1a056 378 exp_continue
18fe2033 379 }
28781456
JK
380 -re "A problem internal to GDB has been detected" {
381 fail "setting breakpoint at $function in runto (GDB internal error)"
382 gdb_internal_error_resync
383 return 0
384 }
55cd6f92
DJ
385 -re "$gdb_prompt $" {
386 if { $no_message == 0 } {
387 fail "setting breakpoint at $function"
388 }
389 return 0
390 }
391 timeout {
392 if { $no_message == 0 } {
393 fail "setting breakpoint at $function (timeout)"
394 }
395 return 0
396 }
c906108c
SS
397 }
398 return 1;
399}
400
401# Set breakpoint at function and run gdb until it breaks there.
402# Since this is the only breakpoint that will be set, if it stops
403# at a breakpoint, we will assume it is the one we want. We can't
404# just compare to "function" because it might be a fully qualified,
78a1a894
DJ
405# single quoted C++ function specifier. If there's an additional argument,
406# pass it to gdb_breakpoint.
c906108c 407
78a1a894 408proc runto { function args } {
c906108c
SS
409 global gdb_prompt
410 global decimal
411
412 delete_breakpoints
413
78a1a894 414 if ![gdb_breakpoint $function [lindex $args 0]] {
c906108c
SS
415 return 0;
416 }
417
418 gdb_run_cmd
419
420 # the "at foo.c:36" output we get with -g.
421 # the "in func" output we get without -g.
422 gdb_expect 30 {
423 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
424 return 1
425 }
426 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
427 return 1
428 }
8e46892c
JK
429 -re "The target does not support running in non-stop mode.\r\n$gdb_prompt $" {
430 unsupported "Non-stop mode not supported"
431 return 0
432 }
569b05a5
JK
433 -re ".*A problem internal to GDB has been detected" {
434 fail "running to $function in runto (GDB internal error)"
435 gdb_internal_error_resync
436 return 0
437 }
c906108c
SS
438 -re "$gdb_prompt $" {
439 fail "running to $function in runto"
440 return 0
441 }
72c63395
JK
442 eof {
443 fail "running to $function in runto (end of file)"
444 return 0
445 }
c906108c
SS
446 timeout {
447 fail "running to $function in runto (timeout)"
448 return 0
449 }
450 }
451 return 1
452}
453
454#
455# runto_main -- ask gdb to run until we hit a breakpoint at main.
456# The case where the target uses stubs has to be handled
457# specially--if it uses stubs, assuming we hit
458# breakpoint() and just step out of the function.
459#
460proc runto_main { } {
461 global gdb_prompt
462 global decimal
463
464 if ![target_info exists gdb_stub] {
465 return [runto main]
466 }
467
468 delete_breakpoints
469
470 gdb_step_for_stub;
471
472 return 1
473}
474
7a292a7a 475
4ce44c66
JM
476### Continue, and expect to hit a breakpoint.
477### Report a pass or fail, depending on whether it seems to have
478### worked. Use NAME as part of the test name; each call to
479### continue_to_breakpoint should use a NAME which is unique within
480### that test file.
74960c60 481proc gdb_continue_to_breakpoint {name {location_pattern .*}} {
4ce44c66
JM
482 global gdb_prompt
483 set full_name "continue to breakpoint: $name"
484
485 send_gdb "continue\n"
486 gdb_expect {
b02a33db 487 -re "Breakpoint .* (at|in) $location_pattern\r\n$gdb_prompt $" {
4ce44c66
JM
488 pass $full_name
489 }
490 -re ".*$gdb_prompt $" {
491 fail $full_name
492 }
493 timeout {
494 fail "$full_name (timeout)"
495 }
496 }
497}
498
499
039cf96d
AC
500# gdb_internal_error_resync:
501#
502# Answer the questions GDB asks after it reports an internal error
503# until we get back to a GDB prompt. Decline to quit the debugging
504# session, and decline to create a core file. Return non-zero if the
505# resync succeeds.
506#
507# This procedure just answers whatever questions come up until it sees
508# a GDB prompt; it doesn't require you to have matched the input up to
509# any specific point. However, it only answers questions it sees in
510# the output itself, so if you've matched a question, you had better
511# answer it yourself before calling this.
512#
513# You can use this function thus:
514#
515# gdb_expect {
516# ...
517# -re ".*A problem internal to GDB has been detected" {
518# gdb_internal_error_resync
519# }
520# ...
521# }
522#
523proc gdb_internal_error_resync {} {
524 global gdb_prompt
525
526 set count 0
527 while {$count < 10} {
528 gdb_expect {
529 -re "Quit this debugging session\\? \\(y or n\\) $" {
530 send_gdb "n\n"
531 incr count
532 }
533 -re "Create a core file of GDB\\? \\(y or n\\) $" {
534 send_gdb "n\n"
535 incr count
536 }
537 -re "$gdb_prompt $" {
538 # We're resynchronized.
539 return 1
540 }
541 timeout {
542 perror "Could not resync from internal error (timeout)"
543 return 0
544 }
545 }
546 }
2b211c59
AC
547 perror "Could not resync from internal error (resync count exceeded)"
548 return 0
039cf96d
AC
549}
550
4ce44c66 551
2307bd6a 552# gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
8dbfb380 553# Send a command to gdb; test the result.
c906108c
SS
554#
555# COMMAND is the command to execute, send to GDB with send_gdb. If
556# this is the null string no command is sent.
2307bd6a
DJ
557# MESSAGE is a message to be printed with the built-in failure patterns
558# if one of them matches. If MESSAGE is empty COMMAND will be used.
559# EXPECT_ARGUMENTS will be fed to expect in addition to the standard
560# patterns. Pattern elements will be evaluated in the caller's
561# context; action elements will be executed in the caller's context.
562# Unlike patterns for gdb_test, these patterns should generally include
563# the final newline and prompt.
c906108c
SS
564#
565# Returns:
2307bd6a
DJ
566# 1 if the test failed, according to a built-in failure pattern
567# 0 if only user-supplied patterns matched
c906108c
SS
568# -1 if there was an internal error.
569#
d422fe19
AC
570# You can use this function thus:
571#
572# gdb_test_multiple "print foo" "test foo" {
573# -re "expected output 1" {
574# pass "print foo"
575# }
576# -re "expected output 2" {
577# fail "print foo"
578# }
579# }
580#
fda326dd 581# The standard patterns, such as "Inferior exited..." and "A problem
d422fe19
AC
582# ...", all being implicitly appended to that list.
583#
2307bd6a 584proc gdb_test_multiple { command message user_code } {
c906108c
SS
585 global verbose
586 global gdb_prompt
587 global GDB
fda326dd 588 global inferior_exited_re
c906108c 589 upvar timeout timeout
c47cebdb 590 upvar expect_out expect_out
c906108c 591
2307bd6a
DJ
592 if { $message == "" } {
593 set message $command
c906108c 594 }
c906108c 595
824cc8dd
JK
596 if [string match "*\[\r\n\]" $command] {
597 error "Invalid trailing newline in \"$message\" test"
598 }
599
8344e389
JK
600 if [string match "*\[\r\n\]*" $message] {
601 error "Invalid newline in \"$message\" test"
602 }
603
2307bd6a
DJ
604 # TCL/EXPECT WART ALERT
605 # Expect does something very strange when it receives a single braced
606 # argument. It splits it along word separators and performs substitutions.
607 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
608 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
609 # double-quoted list item, "\[ab\]" is just a long way of representing
610 # "[ab]", because the backslashes will be removed by lindex.
611
612 # Unfortunately, there appears to be no easy way to duplicate the splitting
613 # that expect will do from within TCL. And many places make use of the
614 # "\[0-9\]" construct, so we need to support that; and some places make use
615 # of the "[func]" construct, so we need to support that too. In order to
616 # get this right we have to substitute quoted list elements differently
617 # from braced list elements.
618
619 # We do this roughly the same way that Expect does it. We have to use two
620 # lists, because if we leave unquoted newlines in the argument to uplevel
621 # they'll be treated as command separators, and if we escape newlines
622 # we mangle newlines inside of command blocks. This assumes that the
623 # input doesn't contain a pattern which contains actual embedded newlines
624 # at this point!
625
626 regsub -all {\n} ${user_code} { } subst_code
627 set subst_code [uplevel list $subst_code]
628
629 set processed_code ""
630 set patterns ""
631 set expecting_action 0
21e24d21 632 set expecting_arg 0
2307bd6a
DJ
633 foreach item $user_code subst_item $subst_code {
634 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
635 lappend processed_code $item
636 continue
637 }
21e24d21
PA
638 if { $item == "-indices" || $item == "-re" || $item == "-ex" } {
639 lappend processed_code $item
640 continue
641 }
642 if { $item == "-timeout" } {
643 set expecting_arg 1
644 lappend processed_code $item
645 continue
646 }
647 if { $expecting_arg } {
648 set expecting_arg 0
2307bd6a
DJ
649 lappend processed_code $item
650 continue
651 }
652 if { $expecting_action } {
653 lappend processed_code "uplevel [list $item]"
654 set expecting_action 0
655 # Cosmetic, no effect on the list.
656 append processed_code "\n"
657 continue
658 }
659 set expecting_action 1
660 lappend processed_code $subst_item
661 if {$patterns != ""} {
662 append patterns "; "
663 }
664 append patterns "\"$subst_item\""
c906108c
SS
665 }
666
2307bd6a
DJ
667 # Also purely cosmetic.
668 regsub -all {\r} $patterns {\\r} patterns
669 regsub -all {\n} $patterns {\\n} patterns
670
c906108c
SS
671 if $verbose>2 then {
672 send_user "Sending \"$command\" to gdb\n"
2307bd6a 673 send_user "Looking to match \"$patterns\"\n"
c906108c
SS
674 send_user "Message is \"$message\"\n"
675 }
676
677 set result -1
678 set string "${command}\n";
679 if { $command != "" } {
680 while { "$string" != "" } {
681 set foo [string first "\n" "$string"];
682 set len [string length "$string"];
683 if { $foo < [expr $len - 1] } {
684 set str [string range "$string" 0 $foo];
685 if { [send_gdb "$str"] != "" } {
686 global suppress_flag;
687
688 if { ! $suppress_flag } {
689 perror "Couldn't send $command to GDB.";
690 }
691 fail "$message";
692 return $result;
693 }
a0b3c4fd
JM
694 # since we're checking if each line of the multi-line
695 # command are 'accepted' by GDB here,
696 # we need to set -notransfer expect option so that
697 # command output is not lost for pattern matching
698 # - guo
5f279fa6
DJ
699 gdb_expect 2 {
700 -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
701 timeout { verbose "partial: timeout" 3 }
c906108c
SS
702 }
703 set string [string range "$string" [expr $foo + 1] end];
704 } else {
705 break;
706 }
707 }
708 if { "$string" != "" } {
709 if { [send_gdb "$string"] != "" } {
710 global suppress_flag;
711
712 if { ! $suppress_flag } {
713 perror "Couldn't send $command to GDB.";
714 }
715 fail "$message";
716 return $result;
717 }
718 }
719 }
720
9d2e1bab
ND
721 if [target_info exists gdb,timeout] {
722 set tmt [target_info gdb,timeout];
c906108c 723 } else {
c906108c
SS
724 if [info exists timeout] {
725 set tmt $timeout;
726 } else {
9d2e1bab
ND
727 global timeout;
728 if [info exists timeout] {
729 set tmt $timeout;
730 } else {
731 set tmt 60;
732 }
c906108c
SS
733 }
734 }
2307bd6a
DJ
735
736 set code {
039cf96d
AC
737 -re ".*A problem internal to GDB has been detected" {
738 fail "$message (GDB internal error)"
739 gdb_internal_error_resync
740 }
c906108c
SS
741 -re "\\*\\*\\* DOSEXIT code.*" {
742 if { $message != "" } {
743 fail "$message";
744 }
745 gdb_suppress_entire_file "GDB died";
2307bd6a 746 set result -1;
c906108c 747 }
b0f4b84b
DJ
748 }
749 append code $processed_code
750 append code {
9e0b60a8 751 -re "Ending remote debugging.*$gdb_prompt $" {
c906108c
SS
752 if ![isnative] then {
753 warning "Can`t communicate to remote target."
754 }
755 gdb_exit
756 gdb_start
757 set result -1
758 }
9e0b60a8 759 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
c906108c 760 perror "Undefined command \"$command\"."
9e0b60a8 761 fail "$message"
c906108c
SS
762 set result 1
763 }
764 -re "Ambiguous command.*$gdb_prompt $" {
765 perror "\"$command\" is not a unique command name."
9e0b60a8 766 fail "$message"
c906108c
SS
767 set result 1
768 }
fda326dd 769 -re "$inferior_exited_re with code \[0-9\]+.*$gdb_prompt $" {
c906108c 770 if ![string match "" $message] then {
ed4c619a 771 set errmsg "$message (the program exited)"
c906108c 772 } else {
ed4c619a 773 set errmsg "$command (the program exited)"
c906108c
SS
774 }
775 fail "$errmsg"
2307bd6a 776 set result -1
cb9a9d3e 777 }
fda326dd 778 -re "$inferior_exited_re normally.*$gdb_prompt $" {
cb9a9d3e 779 if ![string match "" $message] then {
ed4c619a 780 set errmsg "$message (the program exited)"
cb9a9d3e 781 } else {
ed4c619a 782 set errmsg "$command (the program exited)"
cb9a9d3e
MS
783 }
784 fail "$errmsg"
2307bd6a 785 set result -1
c906108c
SS
786 }
787 -re "The program is not being run.*$gdb_prompt $" {
788 if ![string match "" $message] then {
ed4c619a 789 set errmsg "$message (the program is no longer running)"
c906108c 790 } else {
ed4c619a 791 set errmsg "$command (the program is no longer running)"
c906108c
SS
792 }
793 fail "$errmsg"
2307bd6a 794 set result -1
c906108c 795 }
734b8fe8 796 -re "\r\n$gdb_prompt $" {
c906108c
SS
797 if ![string match "" $message] then {
798 fail "$message"
799 }
800 set result 1
801 }
802 "<return>" {
803 send_gdb "\n"
804 perror "Window too small."
9e0b60a8 805 fail "$message"
2307bd6a 806 set result -1
c906108c 807 }
b598bfda 808 -re "\\((y or n|y or \\\[n\\\]|\\\[y\\\] or n)\\) " {
c906108c 809 send_gdb "n\n"
b598bfda
DJ
810 gdb_expect -re "$gdb_prompt $"
811 fail "$message (got interactive prompt)"
812 set result -1
813 }
814 -re "\\\[0\\\] cancel\r\n\\\[1\\\] all.*\r\n> $" {
815 send_gdb "0\n"
816 gdb_expect -re "$gdb_prompt $"
817 fail "$message (got breakpoint menu)"
2307bd6a 818 set result -1
c906108c
SS
819 }
820 eof {
821 perror "Process no longer exists"
822 if { $message != "" } {
823 fail "$message"
824 }
825 return -1
826 }
827 full_buffer {
828 perror "internal buffer is full."
9e0b60a8 829 fail "$message"
2307bd6a 830 set result -1
c906108c
SS
831 }
832 timeout {
833 if ![string match "" $message] then {
834 fail "$message (timeout)"
835 }
836 set result 1
837 }
838 }
2307bd6a
DJ
839
840 set result 0
04f6ecf2
DJ
841 set code [catch {gdb_expect $tmt $code} string]
842 if {$code == 1} {
843 global errorInfo errorCode;
844 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
845 } elseif {$code == 2} {
846 return -code return $string
847 } elseif {$code == 3} {
848 return
849 } elseif {$code > 4} {
850 return -code $code $string
851 }
c906108c
SS
852 return $result
853}
2307bd6a
DJ
854
855# gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
856# Send a command to gdb; test the result.
857#
858# COMMAND is the command to execute, send to GDB with send_gdb. If
859# this is the null string no command is sent.
860# PATTERN is the pattern to match for a PASS, and must NOT include
861# the \r\n sequence immediately before the gdb prompt.
862# MESSAGE is an optional message to be printed. If this is
863# omitted, then the pass/fail messages use the command string as the
864# message. (If this is the empty string, then sometimes we don't
865# call pass or fail at all; I don't understand this at all.)
866# QUESTION is a question GDB may ask in response to COMMAND, like
867# "are you sure?"
868# RESPONSE is the response to send if QUESTION appears.
869#
870# Returns:
871# 1 if the test failed,
872# 0 if the test passes,
873# -1 if there was an internal error.
874#
875proc gdb_test { args } {
876 global verbose
877 global gdb_prompt
878 global GDB
879 upvar timeout timeout
880
881 if [llength $args]>2 then {
882 set message [lindex $args 2]
883 } else {
884 set message [lindex $args 0]
885 }
886 set command [lindex $args 0]
887 set pattern [lindex $args 1]
888
889 if [llength $args]==5 {
890 set question_string [lindex $args 3];
891 set response_string [lindex $args 4];
892 } else {
893 set question_string "^FOOBAR$"
894 }
895
896 return [gdb_test_multiple $command $message {
897 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
898 if ![string match "" $message] then {
899 pass "$message"
900 }
901 }
902 -re "(${question_string})$" {
903 send_gdb "$response_string\n";
904 exp_continue;
905 }
906 }]
907}
a7b75dfd
JB
908
909# gdb_test_no_output COMMAND MESSAGE
910# Send a command to GDB and verify that this command generated no output.
911#
912# See gdb_test_multiple for a description of the COMMAND and MESSAGE
913# parameters. If MESSAGE is ommitted, then COMMAND will be used as
c22decce
JB
914# the message. (If MESSAGE is the empty string, then sometimes we do not
915# call pass or fail at all; I don't understand this at all.)
a7b75dfd
JB
916
917proc gdb_test_no_output { args } {
918 global gdb_prompt
919 set command [lindex $args 0]
920 if [llength $args]>1 then {
921 set message [lindex $args 1]
922 } else {
923 set message $command
924 }
925
926 set command_regex [string_to_regexp $command]
927 gdb_test_multiple $command $message {
928 -re "^$command_regex\r\n$gdb_prompt $" {
c22decce
JB
929 if ![string match "" $message] then {
930 pass "$message"
931 }
a7b75dfd
JB
932 }
933 }
934}
935
6b0ecdc2
DE
936# Send a command and then wait for a sequence of outputs.
937# This is useful when the sequence is long and contains ".*", a single
938# regexp to match the entire output can get a timeout much easier.
939#
940# COMMAND is the command to send.
941# TEST_NAME is passed to pass/fail. COMMAND is used if TEST_NAME is "".
942# EXPECTED_OUTPUT_LIST is a list of regexps of expected output, which are
943# processed in order, and all must be present in the output.
944#
945# It is unnecessary to specify ".*" at the beginning or end of any regexp,
946# there is an implicit ".*" between each element of EXPECTED_OUTPUT_LIST.
947# There is also an implicit ".*" between the last regexp and the gdb prompt.
948#
949# Like gdb_test and gdb_test_multiple, the output is expected to end with the
950# gdb prompt, which must not be specified in EXPECTED_OUTPUT_LIST.
5fa290c1
DE
951#
952# Returns:
953# 1 if the test failed,
954# 0 if the test passes,
955# -1 if there was an internal error.
6b0ecdc2
DE
956
957proc gdb_test_sequence { command test_name expected_output_list } {
958 global gdb_prompt
959 if { $test_name == "" } {
960 set test_name $command
961 }
962 lappend expected_output_list ""; # implicit ".*" before gdb prompt
963 send_gdb "$command\n"
5fa290c1 964 return [gdb_expect_list $test_name "$gdb_prompt $" $expected_output_list]
6b0ecdc2
DE
965}
966
c906108c
SS
967\f
968# Test that a command gives an error. For pass or fail, return
969# a 1 to indicate that more tests can proceed. However a timeout
970# is a serious error, generates a special fail message, and causes
971# a 0 to be returned to indicate that more tests are likely to fail
972# as well.
973
974proc test_print_reject { args } {
975 global gdb_prompt
976 global verbose
977
978 if [llength $args]==2 then {
979 set expectthis [lindex $args 1]
980 } else {
981 set expectthis "should never match this bogus string"
982 }
983 set sendthis [lindex $args 0]
984 if $verbose>2 then {
985 send_user "Sending \"$sendthis\" to gdb\n"
986 send_user "Looking to match \"$expectthis\"\n"
987 }
988 send_gdb "$sendthis\n"
989 #FIXME: Should add timeout as parameter.
990 gdb_expect {
991 -re "A .* in expression.*\\.*$gdb_prompt $" {
992 pass "reject $sendthis"
993 return 1
994 }
995 -re "Invalid syntax in expression.*$gdb_prompt $" {
996 pass "reject $sendthis"
997 return 1
998 }
999 -re "Junk after end of expression.*$gdb_prompt $" {
1000 pass "reject $sendthis"
1001 return 1
1002 }
1003 -re "Invalid number.*$gdb_prompt $" {
1004 pass "reject $sendthis"
1005 return 1
1006 }
1007 -re "Invalid character constant.*$gdb_prompt $" {
1008 pass "reject $sendthis"
1009 return 1
1010 }
1011 -re "No symbol table is loaded.*$gdb_prompt $" {
1012 pass "reject $sendthis"
1013 return 1
1014 }
1015 -re "No symbol .* in current context.*$gdb_prompt $" {
1016 pass "reject $sendthis"
1017 return 1
1018 }
c4b7bc2b
JB
1019 -re "Unmatched single quote.*$gdb_prompt $" {
1020 pass "reject $sendthis"
1021 return 1
1022 }
1023 -re "A character constant must contain at least one character.*$gdb_prompt $" {
1024 pass "reject $sendthis"
1025 return 1
1026 }
c906108c
SS
1027 -re "$expectthis.*$gdb_prompt $" {
1028 pass "reject $sendthis"
1029 return 1
1030 }
1031 -re ".*$gdb_prompt $" {
1032 fail "reject $sendthis"
1033 return 1
1034 }
1035 default {
1036 fail "reject $sendthis (eof or timeout)"
1037 return 0
1038 }
1039 }
1040}
1041\f
1042# Given an input string, adds backslashes as needed to create a
1043# regexp that will match the string.
1044
1045proc string_to_regexp {str} {
1046 set result $str
39fb8e9e 1047 regsub -all {[]*+.|()^$\[\\]} $str {\\&} result
c906108c
SS
1048 return $result
1049}
1050
1051# Same as gdb_test, but the second parameter is not a regexp,
1052# but a string that must match exactly.
1053
1054proc gdb_test_exact { args } {
1055 upvar timeout timeout
1056
1057 set command [lindex $args 0]
1058
1059 # This applies a special meaning to a null string pattern. Without
1060 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
1061 # messages from commands that should have no output except a new
1062 # prompt. With this, only results of a null string will match a null
1063 # string pattern.
1064
1065 set pattern [lindex $args 1]
1066 if [string match $pattern ""] {
1067 set pattern [string_to_regexp [lindex $args 0]]
1068 } else {
1069 set pattern [string_to_regexp [lindex $args 1]]
1070 }
1071
1072 # It is most natural to write the pattern argument with only
1073 # embedded \n's, especially if you are trying to avoid Tcl quoting
1074 # problems. But gdb_expect really wants to see \r\n in patterns. So
1075 # transform the pattern here. First transform \r\n back to \n, in
1076 # case some users of gdb_test_exact already do the right thing.
1077 regsub -all "\r\n" $pattern "\n" pattern
1078 regsub -all "\n" $pattern "\r\n" pattern
1079 if [llength $args]==3 then {
1080 set message [lindex $args 2]
1081 } else {
1082 set message $command
1083 }
1084
1085 return [gdb_test $command $pattern $message]
1086}
2dfb8c17
DE
1087
1088# Wrapper around gdb_test_multiple that looks for a list of expected
1089# output elements, but which can appear in any order.
1090# CMD is the gdb command.
1091# NAME is the name of the test.
1092# ELM_FIND_REGEXP specifies how to partition the output into elements to
1093# compare.
1094# ELM_EXTRACT_REGEXP specifies the part of ELM_FIND_REGEXP to compare.
1095# RESULT_MATCH_LIST is a list of exact matches for each expected element.
1096# All elements of RESULT_MATCH_LIST must appear for the test to pass.
1097#
1098# A typical use of ELM_FIND_REGEXP/ELM_EXTRACT_REGEXP is to extract one line
1099# of text per element and then strip trailing \r\n's.
1100# Example:
1101# gdb_test_list_exact "foo" "bar" \
eec52c44
PM
1102# "\[^\r\n\]+\[\r\n\]+" \
1103# "\[^\r\n\]+" \
2dfb8c17
DE
1104# { \
1105# {expected result 1} \
1106# {expected result 2} \
1107# }
1108
1109proc gdb_test_list_exact { cmd name elm_find_regexp elm_extract_regexp result_match_list } {
1110 global gdb_prompt
1111
1112 set matches [lsort $result_match_list]
1113 set seen {}
1114 gdb_test_multiple $cmd $name {
1115 "$cmd\[\r\n\]" { exp_continue }
1116 -re $elm_find_regexp {
1117 set str $expect_out(0,string)
1118 verbose -log "seen: $str" 3
1119 regexp -- $elm_extract_regexp $str elm_seen
1120 verbose -log "extracted: $elm_seen" 3
1121 lappend seen $elm_seen
1122 exp_continue
1123 }
1124 -re "$gdb_prompt $" {
1125 set failed ""
1126 foreach got [lsort $seen] have $matches {
1127 if {![string equal $got $have]} {
1128 set failed $have
1129 break
1130 }
1131 }
1132 if {[string length $failed] != 0} {
1133 fail "$name ($failed not found)"
1134 } else {
1135 pass $name
1136 }
1137 }
1138 }
1139}
c906108c
SS
1140\f
1141proc gdb_reinitialize_dir { subdir } {
1142 global gdb_prompt
1143
1144 if [is_remote host] {
1145 return "";
1146 }
1147 send_gdb "dir\n"
1148 gdb_expect 60 {
1149 -re "Reinitialize source path to empty.*y or n. " {
1150 send_gdb "y\n"
1151 gdb_expect 60 {
1152 -re "Source directories searched.*$gdb_prompt $" {
1153 send_gdb "dir $subdir\n"
1154 gdb_expect 60 {
1155 -re "Source directories searched.*$gdb_prompt $" {
1156 verbose "Dir set to $subdir"
1157 }
1158 -re "$gdb_prompt $" {
1159 perror "Dir \"$subdir\" failed."
1160 }
1161 }
1162 }
1163 -re "$gdb_prompt $" {
1164 perror "Dir \"$subdir\" failed."
1165 }
1166 }
1167 }
1168 -re "$gdb_prompt $" {
1169 perror "Dir \"$subdir\" failed."
1170 }
1171 }
1172}
1173
1174#
1175# gdb_exit -- exit the GDB, killing the target program if necessary
1176#
1177proc default_gdb_exit {} {
1178 global GDB
6b8ce727 1179 global INTERNAL_GDBFLAGS GDBFLAGS
c906108c
SS
1180 global verbose
1181 global gdb_spawn_id;
1182
1183 gdb_stop_suppressing_tests;
1184
1185 if ![info exists gdb_spawn_id] {
1186 return;
1187 }
1188
6b8ce727 1189 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
c906108c
SS
1190
1191 if { [is_remote host] && [board_info host exists fileid] } {
1192 send_gdb "quit\n";
1193 gdb_expect 10 {
1194 -re "y or n" {
1195 send_gdb "y\n";
1196 exp_continue;
1197 }
1198 -re "DOSEXIT code" { }
1199 default { }
1200 }
1201 }
1202
1203 if ![is_remote host] {
1204 remote_close host;
1205 }
1206 unset gdb_spawn_id
1207}
1208
3e3ffd2b 1209# Load a file into the debugger.
2db8e78e 1210# The return value is 0 for success, -1 for failure.
c906108c 1211#
2db8e78e
MC
1212# This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1213# to one of these values:
3e3ffd2b 1214#
2db8e78e
MC
1215# debug file was loaded successfully and has debug information
1216# nodebug file was loaded successfully and has no debug information
1217# fail file was not loaded
c906108c 1218#
2db8e78e
MC
1219# I tried returning this information as part of the return value,
1220# but ran into a mess because of the many re-implementations of
1221# gdb_load in config/*.exp.
3e3ffd2b 1222#
2db8e78e
MC
1223# TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
1224# this if they can get more information set.
3e3ffd2b 1225
c906108c 1226proc gdb_file_cmd { arg } {
3e3ffd2b 1227 global gdb_prompt
c906108c 1228 global verbose
c906108c 1229 global GDB
b741e217
DJ
1230 global last_loaded_file
1231
1232 set last_loaded_file $arg
c906108c 1233
2db8e78e
MC
1234 # Set whether debug info was found.
1235 # Default to "fail".
1236 global gdb_file_cmd_debug_info
1237 set gdb_file_cmd_debug_info "fail"
1238
c906108c 1239 if [is_remote host] {
3e3ffd2b 1240 set arg [remote_download host $arg]
c906108c 1241 if { $arg == "" } {
2db8e78e
MC
1242 perror "download failed"
1243 return -1
c906108c
SS
1244 }
1245 }
1246
4c42eaff
DJ
1247 # The file command used to kill the remote target. For the benefit
1248 # of the testsuite, preserve this behavior.
1249 send_gdb "kill\n"
1250 gdb_expect 120 {
1251 -re "Kill the program being debugged. .y or n. $" {
1252 send_gdb "y\n"
1253 verbose "\t\tKilling previous program being debugged"
1254 exp_continue
1255 }
1256 -re "$gdb_prompt $" {
1257 # OK.
1258 }
1259 }
1260
c906108c
SS
1261 send_gdb "file $arg\n"
1262 gdb_expect 120 {
3e3ffd2b
MC
1263 -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" {
1264 verbose "\t\tLoaded $arg into the $GDB with no debugging symbols"
2db8e78e
MC
1265 set gdb_file_cmd_debug_info "nodebug"
1266 return 0
3e3ffd2b 1267 }
c906108c
SS
1268 -re "Reading symbols from.*done.*$gdb_prompt $" {
1269 verbose "\t\tLoaded $arg into the $GDB"
2db8e78e
MC
1270 set gdb_file_cmd_debug_info "debug"
1271 return 0
c906108c 1272 }
c906108c
SS
1273 -re "Load new symbol table from \".*\".*y or n. $" {
1274 send_gdb "y\n"
1275 gdb_expect 120 {
1276 -re "Reading symbols from.*done.*$gdb_prompt $" {
1277 verbose "\t\tLoaded $arg with new symbol table into $GDB"
2db8e78e
MC
1278 set gdb_file_cmd_debug_info "debug"
1279 return 0
c906108c
SS
1280 }
1281 timeout {
2db8e78e
MC
1282 perror "(timeout) Couldn't load $arg, other program already loaded."
1283 return -1
c906108c
SS
1284 }
1285 }
1286 }
1287 -re "No such file or directory.*$gdb_prompt $" {
2db8e78e
MC
1288 perror "($arg) No such file or directory"
1289 return -1
c906108c
SS
1290 }
1291 -re "$gdb_prompt $" {
2db8e78e
MC
1292 perror "couldn't load $arg into $GDB."
1293 return -1
c906108c
SS
1294 }
1295 timeout {
2db8e78e
MC
1296 perror "couldn't load $arg into $GDB (timed out)."
1297 return -1
c906108c
SS
1298 }
1299 eof {
1300 # This is an attempt to detect a core dump, but seems not to
1301 # work. Perhaps we need to match .* followed by eof, in which
1302 # gdb_expect does not seem to have a way to do that.
2db8e78e
MC
1303 perror "couldn't load $arg into $GDB (end of file)."
1304 return -1
c906108c
SS
1305 }
1306 }
1307}
1308
1309#
1310# start gdb -- start gdb running, default procedure
1311#
1312# When running over NFS, particularly if running many simultaneous
1313# tests on different hosts all using the same server, things can
1314# get really slow. Give gdb at least 3 minutes to start up.
1315#
1316proc default_gdb_start { } {
1317 global verbose
1318 global GDB
6b8ce727 1319 global INTERNAL_GDBFLAGS GDBFLAGS
c906108c
SS
1320 global gdb_prompt
1321 global timeout
1322 global gdb_spawn_id;
1323
1324 gdb_stop_suppressing_tests;
1325
6b8ce727 1326 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
c906108c
SS
1327
1328 if [info exists gdb_spawn_id] {
1329 return 0;
1330 }
1331
1332 if ![is_remote host] {
1333 if { [which $GDB] == 0 } then {
1334 perror "$GDB does not exist."
1335 exit 1
1336 }
1337 }
6b8ce727 1338 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]"];
c906108c
SS
1339 if { $res < 0 || $res == "" } {
1340 perror "Spawning $GDB failed."
1341 return 1;
1342 }
1343 gdb_expect 360 {
1344 -re "\[\r\n\]$gdb_prompt $" {
1345 verbose "GDB initialized."
1346 }
1347 -re "$gdb_prompt $" {
1348 perror "GDB never initialized."
1349 return -1
1350 }
1351 timeout {
1352 perror "(timeout) GDB never initialized after 10 seconds."
1353 remote_close host;
1354 return -1
1355 }
1356 }
1357 set gdb_spawn_id -1;
1358 # force the height to "unlimited", so no pagers get used
1359
1360 send_gdb "set height 0\n"
1361 gdb_expect 10 {
1362 -re "$gdb_prompt $" {
1363 verbose "Setting height to 0." 2
1364 }
1365 timeout {
1366 warning "Couldn't set the height to 0"
1367 }
1368 }
1369 # force the width to "unlimited", so no wraparound occurs
1370 send_gdb "set width 0\n"
1371 gdb_expect 10 {
1372 -re "$gdb_prompt $" {
1373 verbose "Setting width to 0." 2
1374 }
1375 timeout {
1376 warning "Couldn't set the width to 0."
1377 }
1378 }
1379 return 0;
1380}
1381
ec3c07fc
NS
1382# Examine the output of compilation to determine whether compilation
1383# failed or not. If it failed determine whether it is due to missing
1384# compiler or due to compiler error. Report pass, fail or unsupported
1385# as appropriate
1386
1387proc gdb_compile_test {src output} {
1388 if { $output == "" } {
1389 pass "compilation [file tail $src]"
1390 } elseif { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output] } {
1391 unsupported "compilation [file tail $src]"
1392 } elseif { [regexp {.*: command not found[\r|\n]*$} $output] } {
1393 unsupported "compilation [file tail $src]"
6bb85cd1
DE
1394 } elseif { [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } {
1395 unsupported "compilation [file tail $src]"
ec3c07fc
NS
1396 } else {
1397 verbose -log "compilation failed: $output" 2
1398 fail "compilation [file tail $src]"
1399 }
1400}
1401
d4f3574e
SS
1402# Return a 1 for configurations for which we don't even want to try to
1403# test C++.
1404
1405proc skip_cplus_tests {} {
d4f3574e
SS
1406 if { [istarget "h8300-*-*"] } {
1407 return 1
1408 }
81d2cbae 1409
1146c7f1
SC
1410 # The C++ IO streams are too large for HC11/HC12 and are thus not
1411 # available. The gdb C++ tests use them and don't compile.
1412 if { [istarget "m6811-*-*"] } {
1413 return 1
1414 }
1415 if { [istarget "m6812-*-*"] } {
1416 return 1
1417 }
d4f3574e
SS
1418 return 0
1419}
1420
759f0f0b
PA
1421# Return a 1 for configurations for which don't have both C++ and the STL.
1422
1423proc skip_stl_tests {} {
1424 # Symbian supports the C++ language, but the STL is missing
1425 # (both headers and libraries).
1426 if { [istarget "arm*-*-symbianelf*"] } {
1427 return 1
1428 }
1429
1430 return [skip_cplus_tests]
1431}
1432
89a237cb
MC
1433# Return a 1 if I don't even want to try to test FORTRAN.
1434
1435proc skip_fortran_tests {} {
1436 return 0
1437}
1438
ec3c07fc
NS
1439# Return a 1 if I don't even want to try to test ada.
1440
1441proc skip_ada_tests {} {
1442 return 0
1443}
1444
1445# Return a 1 if I don't even want to try to test java.
1446
1447proc skip_java_tests {} {
1448 return 0
1449}
1450
f6bbabf0
PM
1451# Return a 1 for configurations that do not support Python scripting.
1452
1453proc skip_python_tests {} {
1454 global gdb_prompt
1455 gdb_test_multiple "python print 'test'" "verify python support" {
1456 -re "not supported.*$gdb_prompt $" {
1457 unsupported "Python support is disabled."
1458 return 1
1459 }
1460 -re "$gdb_prompt $" {}
1461 }
1462
1463 return 0
1464}
1465
93f02886
DJ
1466# Return a 1 if we should skip shared library tests.
1467
1468proc skip_shlib_tests {} {
1469 # Run the shared library tests on native systems.
1470 if {[isnative]} {
1471 return 0
1472 }
1473
1474 # An abbreviated list of remote targets where we should be able to
1475 # run shared library tests.
1476 if {([istarget *-*-linux*]
1477 || [istarget *-*-*bsd*]
1478 || [istarget *-*-solaris2*]
1479 || [istarget arm*-*-symbianelf*]
1480 || [istarget *-*-mingw*]
1481 || [istarget *-*-cygwin*]
1482 || [istarget *-*-pe*])} {
1483 return 0
1484 }
1485
1486 return 1
1487}
1488
e43ec454
YQ
1489# Return 1 if _Complex types are supported, otherwise, return 0.
1490
1491proc support_complex_tests {} {
1492 global support_complex_tests_saved
1493
1494 # Use the cached value, if it exists.
1495 if [info exists support_complex_tests_saved] {
1496 verbose "returning saved $support_complex_tests_saved" 2
1497 return $support_complex_tests_saved
1498 }
1499
1500 # Set up, compile, and execute a test program containing _Complex types.
1501 # Include the current process ID in the file names to prevent conflicts
1502 # with invocations for multiple testsuites.
1503 set src complex[pid].c
1504 set exe complex[pid].x
1505
1506 set f [open $src "w"]
1507 puts $f "int main() {"
1508 puts $f "_Complex float cf;"
1509 puts $f "_Complex double cd;"
1510 puts $f "_Complex long double cld;"
1511 puts $f " return 0; }"
1512 close $f
1513
1514 verbose "compiling testfile $src" 2
1515 set compile_flags {debug nowarnings quiet}
1516 set lines [gdb_compile $src $exe executable $compile_flags]
1517 file delete $src
1518 file delete $exe
1519
1520 if ![string match "" $lines] then {
1521 verbose "testfile compilation failed, returning 0" 2
1522 set support_complex_tests_saved 0
1523 } else {
1524 set support_complex_tests_saved 1
1525 }
1526
1527 return $support_complex_tests_saved
1528}
1529
ab254057
YQ
1530# Return 1 if target hardware or OS supports single stepping to signal
1531# handler, otherwise, return 0.
1532
1533proc can_single_step_to_signal_handler {} {
1534
1535 # Targets don't have hardware single step. On these targets, when
1536 # a signal is delivered during software single step, gdb is unable
1537 # to determine the next instruction addresses, because start of signal
1538 # handler is one of them.
1539 if { [istarget "arm*-*-*"] || [istarget "mips*-*-*"] } {
1540 return 0
1541 }
1542
1543 return 1
1544}
1545
add265ae
L
1546# Return 1 if target is ILP32.
1547# This cannot be decided simply from looking at the target string,
1548# as it might depend on externally passed compiler options like -m64.
1549proc is_ilp32_target {} {
1550 global is_ilp32_target_saved
1551
1552 # Use the cached value, if it exists. Cache value per "board" to handle
1553 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1554 set me "is_ilp32_target"
1555 set board [target_info name]
1556 if [info exists is_ilp32_target_saved($board)] {
1557 verbose "$me: returning saved $is_ilp32_target_saved($board)" 2
1558 return $is_ilp32_target_saved($board)
1559 }
1560
1561
1562 set src ilp32[pid].c
1563 set obj ilp32[pid].o
1564
1565 set f [open $src "w"]
1566 puts $f "int dummy\[sizeof (int) == 4"
1567 puts $f " && sizeof (void *) == 4"
1568 puts $f " && sizeof (long) == 4 ? 1 : -1\];"
1569 close $f
1570
1571 verbose "$me: compiling testfile $src" 2
1572 set lines [gdb_compile $src $obj object {quiet}]
1573 file delete $src
1574 file delete $obj
1575
1576 if ![string match "" $lines] then {
1577 verbose "$me: testfile compilation failed, returning 0" 2
1578 return [set is_ilp32_target_saved($board) 0]
1579 }
1580
1581 verbose "$me: returning 1" 2
1582 return [set is_ilp32_target_saved($board) 1]
1583}
1584
1585# Return 1 if target is LP64.
1586# This cannot be decided simply from looking at the target string,
1587# as it might depend on externally passed compiler options like -m64.
1588proc is_lp64_target {} {
1589 global is_lp64_target_saved
1590
1591 # Use the cached value, if it exists. Cache value per "board" to handle
1592 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1593 set me "is_lp64_target"
1594 set board [target_info name]
1595 if [info exists is_lp64_target_saved($board)] {
1596 verbose "$me: returning saved $is_lp64_target_saved($board)" 2
1597 return $is_lp64_target_saved($board)
1598 }
1599
1600 set src lp64[pid].c
1601 set obj lp64[pid].o
1602
1603 set f [open $src "w"]
1604 puts $f "int dummy\[sizeof (int) == 4"
1605 puts $f " && sizeof (void *) == 8"
1606 puts $f " && sizeof (long) == 8 ? 1 : -1\];"
1607 close $f
1608
1609 verbose "$me: compiling testfile $src" 2
1610 set lines [gdb_compile $src $obj object {quiet}]
1611 file delete $src
1612 file delete $obj
1613
1614 if ![string match "" $lines] then {
1615 verbose "$me: testfile compilation failed, returning 0" 2
1616 return [set is_lp64_target_saved($board) 0]
1617 }
1618
1619 verbose "$me: returning 1" 2
1620 return [set is_lp64_target_saved($board) 1]
1621}
1622
7f062217
JK
1623# Return 1 if target has x86_64 registers - either amd64 or x32.
1624# x32 target identifies as x86_64-*-linux*, therefore it cannot be determined
1625# just from the target string.
1626proc is_amd64_regs_target {} {
1627 global is_amd64_regs_target_saved
1628
1629 if {![istarget "x86_64-*-*"]} {
1630 return 0
1631 }
1632
1633 # Use the cached value, if it exists. Cache value per "board" to handle
1634 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1635 set me "is_amd64_regs_target"
1636 set board [target_info name]
1637 if [info exists is_amd64_regs_target_saved($board)] {
1638 verbose "$me: returning saved $is_amd64_regs_target_saved($board)" 2
1639 return $is_amd64_regs_target_saved($board)
1640 }
1641
1642 set src reg64[pid].s
1643 set obj reg64[pid].o
1644
1645 set f [open $src "w"]
1646 foreach reg \
1647 {rax rbx rcx rdx rsi rdi rbp rsp r8 r9 r10 r11 r12 r13 r14 r15} {
1648 puts $f "\tincq %$reg"
1649 }
1650 close $f
1651
1652 verbose "$me: compiling testfile $src" 2
1653 set lines [gdb_compile $src $obj object {quiet}]
1654 file delete $src
1655 file delete $obj
1656
1657 if ![string match "" $lines] then {
1658 verbose "$me: testfile compilation failed, returning 0" 2
1659 return [set is_amd64_regs_target_saved($board) 0]
1660 }
1661
1662 verbose "$me: returning 1" 2
1663 return [set is_amd64_regs_target_saved($board) 1]
1664}
1665
6edba76f
TT
1666# Return 1 if this target is an x86 or x86-64 with -m32.
1667proc is_x86_like_target {} {
1668 if {[istarget i?86-*]} {
1669 return 1
1670 }
1671 if {![istarget "x86_64-*-*"]} {
1672 return 0
1673 }
7f062217 1674 return [expr [is_ilp32_target] && ![is_amd64_regs_target]]
6edba76f
TT
1675}
1676
be777e08
YQ
1677# Return 1 if displaced stepping is supported on target, otherwise, return 0.
1678proc support_displaced_stepping {} {
1679
1680 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"]
1681 || [istarget "arm*-*-linux*"] || [istarget "powerpc-*-linux*"]
1682 || [istarget "powerpc64-*-linux*"] || [istarget "s390*-*-*"] } {
1683 return 1
1684 }
1685
1686 return 0
1687}
1688
3c95e6af
PG
1689# Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1690# 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1691
1692proc skip_altivec_tests {} {
1693 global skip_vmx_tests_saved
fda326dd 1694 global srcdir subdir gdb_prompt inferior_exited_re
3c95e6af
PG
1695
1696 # Use the cached value, if it exists.
1697 set me "skip_altivec_tests"
1698 if [info exists skip_vmx_tests_saved] {
1699 verbose "$me: returning saved $skip_vmx_tests_saved" 2
1700 return $skip_vmx_tests_saved
1701 }
1702
1703 # Some simulators are known to not support VMX instructions.
1704 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1705 verbose "$me: target known to not support VMX, returning 1" 2
476308bf 1706 return [set skip_vmx_tests_saved 1]
3c95e6af
PG
1707 }
1708
1709 # Make sure we have a compiler that understands altivec.
fc91c6c2 1710 set compile_flags {debug nowarnings}
3c95e6af
PG
1711 if [get_compiler_info not-used] {
1712 warning "Could not get compiler info"
1713 return 1
1714 }
1715 if [test_compiler_info gcc*] {
1716 set compile_flags "$compile_flags additional_flags=-maltivec"
1717 } elseif [test_compiler_info xlc*] {
1718 set compile_flags "$compile_flags additional_flags=-qaltivec"
1719 } else {
1720 verbose "Could not compile with altivec support, returning 1" 2
1721 return 1
1722 }
1723
1724 # Set up, compile, and execute a test program containing VMX instructions.
1725 # Include the current process ID in the file names to prevent conflicts
1726 # with invocations for multiple testsuites.
1727 set src vmx[pid].c
1728 set exe vmx[pid].x
1729
1730 set f [open $src "w"]
1731 puts $f "int main() {"
1732 puts $f "#ifdef __MACH__"
1733 puts $f " asm volatile (\"vor v0,v0,v0\");"
1734 puts $f "#else"
1735 puts $f " asm volatile (\"vor 0,0,0\");"
1736 puts $f "#endif"
1737 puts $f " return 0; }"
1738 close $f
1739
1740 verbose "$me: compiling testfile $src" 2
1741 set lines [gdb_compile $src $exe executable $compile_flags]
1742 file delete $src
1743
1744 if ![string match "" $lines] then {
1745 verbose "$me: testfile compilation failed, returning 1" 2
1746 return [set skip_vmx_tests_saved 1]
1747 }
1748
1749 # No error message, compilation succeeded so now run it via gdb.
1750
1751 gdb_exit
1752 gdb_start
1753 gdb_reinitialize_dir $srcdir/$subdir
1754 gdb_load "$exe"
1755 gdb_run_cmd
1756 gdb_expect {
1757 -re ".*Illegal instruction.*${gdb_prompt} $" {
1758 verbose -log "\n$me altivec hardware not detected"
1759 set skip_vmx_tests_saved 1
1760 }
fda326dd 1761 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3c95e6af
PG
1762 verbose -log "\n$me: altivec hardware detected"
1763 set skip_vmx_tests_saved 0
1764 }
1765 default {
1766 warning "\n$me: default case taken"
1767 set skip_vmx_tests_saved 1
1768 }
1769 }
1770 gdb_exit
1771 remote_file build delete $exe
1772
1773 verbose "$me: returning $skip_vmx_tests_saved" 2
1774 return $skip_vmx_tests_saved
1775}
1776
604c2f83
LM
1777# Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1778# 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1779
1780proc skip_vsx_tests {} {
1781 global skip_vsx_tests_saved
fda326dd 1782 global srcdir subdir gdb_prompt inferior_exited_re
604c2f83
LM
1783
1784 # Use the cached value, if it exists.
1785 set me "skip_vsx_tests"
1786 if [info exists skip_vsx_tests_saved] {
1787 verbose "$me: returning saved $skip_vsx_tests_saved" 2
1788 return $skip_vsx_tests_saved
1789 }
1790
1791 # Some simulators are known to not support Altivec instructions, so
1792 # they won't support VSX instructions as well.
1793 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1794 verbose "$me: target known to not support VSX, returning 1" 2
1795 return [set skip_vsx_tests_saved 1]
1796 }
1797
1798 # Make sure we have a compiler that understands altivec.
1799 set compile_flags {debug nowarnings quiet}
1800 if [get_compiler_info not-used] {
1801 warning "Could not get compiler info"
1802 return 1
1803 }
1804 if [test_compiler_info gcc*] {
1805 set compile_flags "$compile_flags additional_flags=-mvsx"
1806 } elseif [test_compiler_info xlc*] {
d9492458 1807 set compile_flags "$compile_flags additional_flags=-qasm=gcc"
604c2f83
LM
1808 } else {
1809 verbose "Could not compile with vsx support, returning 1" 2
1810 return 1
1811 }
1812
1813 set src vsx[pid].c
1814 set exe vsx[pid].x
1815
1816 set f [open $src "w"]
1817 puts $f "int main() {"
d9492458 1818 puts $f " double a\[2\] = { 1.0, 2.0 };"
604c2f83 1819 puts $f "#ifdef __MACH__"
d9492458 1820 puts $f " asm volatile (\"lxvd2x v0,v0,%\[addr\]\" : : \[addr\] \"r\" (a));"
604c2f83 1821 puts $f "#else"
d9492458 1822 puts $f " asm volatile (\"lxvd2x 0,0,%\[addr\]\" : : \[addr\] \"r\" (a));"
604c2f83
LM
1823 puts $f "#endif"
1824 puts $f " return 0; }"
1825 close $f
1826
1827 verbose "$me: compiling testfile $src" 2
1828 set lines [gdb_compile $src $exe executable $compile_flags]
1829 file delete $src
1830
1831 if ![string match "" $lines] then {
1832 verbose "$me: testfile compilation failed, returning 1" 2
1833 return [set skip_vsx_tests_saved 1]
1834 }
1835
1836 # No error message, compilation succeeded so now run it via gdb.
1837
1838 gdb_exit
1839 gdb_start
1840 gdb_reinitialize_dir $srcdir/$subdir
1841 gdb_load "$exe"
1842 gdb_run_cmd
1843 gdb_expect {
1844 -re ".*Illegal instruction.*${gdb_prompt} $" {
1845 verbose -log "\n$me VSX hardware not detected"
1846 set skip_vsx_tests_saved 1
1847 }
fda326dd 1848 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
604c2f83
LM
1849 verbose -log "\n$me: VSX hardware detected"
1850 set skip_vsx_tests_saved 0
1851 }
1852 default {
1853 warning "\n$me: default case taken"
1854 set skip_vsx_tests_saved 1
1855 }
1856 }
1857 gdb_exit
1858 remote_file build delete $exe
1859
1860 verbose "$me: returning $skip_vsx_tests_saved" 2
1861 return $skip_vsx_tests_saved
1862}
1863
7a292a7a
SS
1864# Skip all the tests in the file if you are not on an hppa running
1865# hpux target.
1866
1867proc skip_hp_tests {} {
1868 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
c906108c
SS
1869 verbose "Skip hp tests is $skip_hp"
1870 return $skip_hp
1871}
1872
edb3359d
DJ
1873# Return whether we should skip tests for showing inlined functions in
1874# backtraces. Requires get_compiler_info and get_debug_format.
1875
1876proc skip_inline_frame_tests {} {
1877 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
1878 if { ! [test_debug_format "DWARF 2"] } {
1879 return 1
1880 }
1881
1882 # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line.
1883 if { ([test_compiler_info "gcc-2-*"]
1884 || [test_compiler_info "gcc-3-*"]
1885 || [test_compiler_info "gcc-4-0-*"]) } {
1886 return 1
1887 }
1888
1889 return 0
1890}
1891
1892# Return whether we should skip tests for showing variables from
1893# inlined functions. Requires get_compiler_info and get_debug_format.
1894
1895proc skip_inline_var_tests {} {
1896 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
1897 if { ! [test_debug_format "DWARF 2"] } {
1898 return 1
1899 }
1900
1901 return 0
1902}
1903
b800ec70
UW
1904# Return a 1 if we should skip tests that require hardware breakpoints
1905
1906proc skip_hw_breakpoint_tests {} {
1907 # Skip tests if requested by the board (note that no_hardware_watchpoints
1908 # disables both watchpoints and breakpoints)
1909 if { [target_info exists gdb,no_hardware_watchpoints]} {
1910 return 1
1911 }
1912
1913 # These targets support hardware breakpoints natively
1914 if { [istarget "i?86-*-*"]
1915 || [istarget "x86_64-*-*"]
e3039479
UW
1916 || [istarget "ia64-*-*"]
1917 || [istarget "arm*-*-*"]} {
b800ec70
UW
1918 return 0
1919 }
1920
1921 return 1
1922}
1923
1924# Return a 1 if we should skip tests that require hardware watchpoints
1925
1926proc skip_hw_watchpoint_tests {} {
1927 # Skip tests if requested by the board
1928 if { [target_info exists gdb,no_hardware_watchpoints]} {
1929 return 1
1930 }
1931
1932 # These targets support hardware watchpoints natively
1933 if { [istarget "i?86-*-*"]
1934 || [istarget "x86_64-*-*"]
1935 || [istarget "ia64-*-*"]
e3039479 1936 || [istarget "arm*-*-*"]
b800ec70
UW
1937 || [istarget "powerpc*-*-linux*"]
1938 || [istarget "s390*-*-*"] } {
1939 return 0
1940 }
1941
1942 return 1
1943}
1944
1945# Return a 1 if we should skip tests that require *multiple* hardware
1946# watchpoints to be active at the same time
1947
1948proc skip_hw_watchpoint_multi_tests {} {
1949 if { [skip_hw_watchpoint_tests] } {
1950 return 1
1951 }
1952
1953 # These targets support just a single hardware watchpoint
e3039479
UW
1954 if { [istarget "arm*-*-*"]
1955 || [istarget "powerpc*-*-linux*"] } {
b800ec70
UW
1956 return 1
1957 }
1958
1959 return 0
1960}
1961
1962# Return a 1 if we should skip tests that require read/access watchpoints
1963
1964proc skip_hw_watchpoint_access_tests {} {
1965 if { [skip_hw_watchpoint_tests] } {
1966 return 1
1967 }
1968
1969 # These targets support just write watchpoints
1970 if { [istarget "s390*-*-*"] } {
1971 return 1
1972 }
1973
1974 return 0
1975}
1976
94b8e876
MC
1977set compiler_info "unknown"
1978set gcc_compiled 0
1979set hp_cc_compiler 0
1980set hp_aCC_compiler 0
94b8e876
MC
1981
1982# Figure out what compiler I am using.
1983#
1984# BINFILE is a "compiler information" output file. This implementation
1985# does not use BINFILE.
1986#
1987# ARGS can be empty or "C++". If empty, "C" is assumed.
1988#
1989# There are several ways to do this, with various problems.
1990#
1991# [ gdb_compile -E $ifile -o $binfile.ci ]
1992# source $binfile.ci
1993#
1994# Single Unix Spec v3 says that "-E -o ..." together are not
1995# specified. And in fact, the native compiler on hp-ux 11 (among
1996# others) does not work with "-E -o ...". Most targets used to do
1997# this, and it mostly worked, because it works with gcc.
1998#
1999# [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
2000# source $binfile.ci
2001#
2002# This avoids the problem with -E and -o together. This almost works
2003# if the build machine is the same as the host machine, which is
2004# usually true of the targets which are not gcc. But this code does
2005# not figure which compiler to call, and it always ends up using the C
2006# compiler. Not good for setting hp_aCC_compiler. Targets
2007# hppa*-*-hpux* and mips*-*-irix* used to do this.
2008#
2009# [ gdb_compile -E $ifile > $binfile.ci ]
2010# source $binfile.ci
2011#
2012# dejagnu target_compile says that it supports output redirection,
2013# but the code is completely different from the normal path and I
2014# don't want to sweep the mines from that path. So I didn't even try
2015# this.
2016#
2017# set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
2018# eval $cppout
2019#
2020# I actually do this for all targets now. gdb_compile runs the right
2021# compiler, and TCL captures the output, and I eval the output.
2022#
2023# Unfortunately, expect logs the output of the command as it goes by,
2024# and dejagnu helpfully prints a second copy of it right afterwards.
2025# So I turn off expect logging for a moment.
2026#
2027# [ gdb_compile $ifile $ciexe_file executable $args ]
2028# [ remote_exec $ciexe_file ]
2029# [ source $ci_file.out ]
2030#
2031# I could give up on -E and just do this.
2032# I didn't get desperate enough to try this.
2033#
2034# -- chastain 2004-01-06
853d6e5b 2035
c906108c 2036proc get_compiler_info {binfile args} {
94b8e876 2037 # For compiler.c and compiler.cc
c906108c 2038 global srcdir
94b8e876
MC
2039
2040 # I am going to play with the log to keep noise out.
2041 global outdir
2042 global tool
2043
2044 # These come from compiler.c or compiler.cc
853d6e5b 2045 global compiler_info
4f70a4c9
MC
2046
2047 # Legacy global data symbols.
94b8e876
MC
2048 global gcc_compiled
2049 global hp_cc_compiler
2050 global hp_aCC_compiler
c906108c 2051
94b8e876
MC
2052 # Choose which file to preprocess.
2053 set ifile "${srcdir}/lib/compiler.c"
2054 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
2055 set ifile "${srcdir}/lib/compiler.cc"
c906108c 2056 }
085dd6e6 2057
94b8e876
MC
2058 # Run $ifile through the right preprocessor.
2059 # Toggle gdb.log to keep the compiler output out of the log.
2060 log_file
e7f86de9
JM
2061 if [is_remote host] {
2062 # We have to use -E and -o together, despite the comments
2063 # above, because of how DejaGnu handles remote host testing.
2064 set ppout "$outdir/compiler.i"
2065 gdb_compile "${ifile}" "$ppout" preprocess [list "$args" quiet]
2066 set file [open $ppout r]
2067 set cppout [read $file]
2068 close $file
2069 } else {
2070 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
2071 }
94b8e876
MC
2072 log_file -a "$outdir/$tool.log"
2073
4f70a4c9
MC
2074 # Eval the output.
2075 set unknown 0
94b8e876 2076 foreach cppline [ split "$cppout" "\n" ] {
4f70a4c9
MC
2077 if { [ regexp "^#" "$cppline" ] } {
2078 # line marker
2079 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
2080 # blank line
2081 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
2082 # eval this line
2083 verbose "get_compiler_info: $cppline" 2
2084 eval "$cppline"
2085 } else {
2086 # unknown line
2087 verbose -log "get_compiler_info: $cppline"
2088 set unknown 1
94b8e876 2089 }
085dd6e6 2090 }
4f70a4c9
MC
2091
2092 # Reset to unknown compiler if any diagnostics happened.
2093 if { $unknown } {
2094 set compiler_info "unknown"
4f70a4c9
MC
2095 }
2096
2097 # Set the legacy symbols.
2098 set gcc_compiled 0
2099 set hp_cc_compiler 0
2100 set hp_aCC_compiler 0
2101 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
2102 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
2103 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
2104 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
2105 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
2106 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
2107 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
2108
2109 # Log what happened.
94b8e876 2110 verbose -log "get_compiler_info: $compiler_info"
085dd6e6
JM
2111
2112 # Most compilers will evaluate comparisons and other boolean
2113 # operations to 0 or 1.
2114 uplevel \#0 { set true 1 }
2115 uplevel \#0 { set false 0 }
2116
94b8e876
MC
2117 # Use of aCC results in boolean results being displayed as
2118 # "true" or "false"
2119 if { $hp_aCC_compiler } {
2120 uplevel \#0 { set true true }
2121 uplevel \#0 { set false false }
085dd6e6
JM
2122 }
2123
c906108c
SS
2124 return 0;
2125}
2126
9b593790 2127proc test_compiler_info { {compiler ""} } {
853d6e5b 2128 global compiler_info
6e87504d
PG
2129
2130 # if no arg, return the compiler_info string
2131
2132 if [string match "" $compiler] {
2133 if [info exists compiler_info] {
2134 return $compiler_info
2135 } else {
2136 perror "No compiler info found."
2137 }
2138 }
2139
853d6e5b
AC
2140 return [string match $compiler $compiler_info]
2141}
2142
f6838f81
DJ
2143proc current_target_name { } {
2144 global target_info
2145 if [info exists target_info(target,name)] {
2146 set answer $target_info(target,name)
2147 } else {
2148 set answer ""
2149 }
2150 return $answer
2151}
2152
f1c47eb2 2153set gdb_wrapper_initialized 0
f6838f81 2154set gdb_wrapper_target ""
f1c47eb2
MS
2155
2156proc gdb_wrapper_init { args } {
2157 global gdb_wrapper_initialized;
2158 global gdb_wrapper_file;
2159 global gdb_wrapper_flags;
f6838f81 2160 global gdb_wrapper_target
f1c47eb2
MS
2161
2162 if { $gdb_wrapper_initialized == 1 } { return; }
2163
2164 if {[target_info exists needs_status_wrapper] && \
277254ba 2165 [target_info needs_status_wrapper] != "0"} {
f1c47eb2
MS
2166 set result [build_wrapper "testglue.o"];
2167 if { $result != "" } {
2168 set gdb_wrapper_file [lindex $result 0];
2169 set gdb_wrapper_flags [lindex $result 1];
2170 } else {
2171 warning "Status wrapper failed to build."
2172 }
2173 }
2174 set gdb_wrapper_initialized 1
f6838f81 2175 set gdb_wrapper_target [current_target_name]
f1c47eb2
MS
2176}
2177
f747e0ce
PA
2178# Some targets need to always link a special object in. Save its path here.
2179global gdb_saved_set_unbuffered_mode_obj
2180set gdb_saved_set_unbuffered_mode_obj ""
2181
c906108c
SS
2182proc gdb_compile {source dest type options} {
2183 global GDB_TESTCASE_OPTIONS;
f1c47eb2
MS
2184 global gdb_wrapper_file;
2185 global gdb_wrapper_flags;
2186 global gdb_wrapper_initialized;
f747e0ce
PA
2187 global srcdir
2188 global objdir
2189 global gdb_saved_set_unbuffered_mode_obj
c906108c 2190
695e2681
MK
2191 set outdir [file dirname $dest]
2192
2193 # Add platform-specific options if a shared library was specified using
2194 # "shlib=librarypath" in OPTIONS.
2195 set new_options ""
2196 set shlib_found 0
bdf7534a 2197 set shlib_load 0
695e2681 2198 foreach opt $options {
57bf0e56
DJ
2199 if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
2200 if [test_compiler_info "xlc-*"] {
93f02886
DJ
2201 # IBM xlc compiler doesn't accept shared library named other
2202 # than .so: use "-Wl," to bypass this
2203 lappend source "-Wl,$shlib_name"
2204 } elseif { ([istarget "*-*-mingw*"]
2205 || [istarget *-*-cygwin*]
2206 || [istarget *-*-pe*])} {
2207 lappend source "${shlib_name}.a"
57bf0e56
DJ
2208 } else {
2209 lappend source $shlib_name
2210 }
0413d738 2211 if { $shlib_found == 0 } {
57bf0e56 2212 set shlib_found 1
0413d738
PA
2213 if { ([istarget "*-*-mingw*"]
2214 || [istarget *-*-cygwin*]) } {
bb61102d 2215 lappend new_options "additional_flags=-Wl,--enable-auto-import"
0413d738 2216 }
57bf0e56 2217 }
b0f4b84b 2218 } elseif { $opt == "shlib_load" } {
bdf7534a 2219 set shlib_load 1
57bf0e56
DJ
2220 } else {
2221 lappend new_options $opt
2222 }
695e2681 2223 }
bdf7534a
NF
2224
2225 # We typically link to shared libraries using an absolute path, and
2226 # that's how they are found at runtime. If we are going to
2227 # dynamically load one by basename, we must specify rpath. If we
2228 # are using a remote host, DejaGNU will link to the shared library
2229 # using a relative path, so again we must specify an rpath.
31f83dc5 2230 if { $shlib_load || ($shlib_found && [is_remote target]) } {
bdf7534a
NF
2231 if { ([istarget "*-*-mingw*"]
2232 || [istarget *-*-cygwin*]
2233 || [istarget *-*-pe*]
bdf7534a
NF
2234 || [istarget hppa*-*-hpux*])} {
2235 # Do not need anything.
2236 } elseif { [istarget *-*-openbsd*] } {
d8b34041 2237 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
759f0f0b
PA
2238 } elseif { [istarget arm*-*-symbianelf*] } {
2239 if { $shlib_load } {
2240 lappend new_options "libs=-ldl"
2241 }
bdf7534a
NF
2242 } else {
2243 if { $shlib_load } {
2244 lappend new_options "libs=-ldl"
2245 }
d8b34041 2246 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
bdf7534a
NF
2247 }
2248 }
695e2681 2249 set options $new_options
57bf0e56 2250
c906108c
SS
2251 if [target_info exists gdb_stub] {
2252 set options2 { "additional_flags=-Dusestubs" }
2253 lappend options "libs=[target_info gdb_stub]";
2254 set options [concat $options2 $options]
2255 }
2256 if [target_info exists is_vxworks] {
2257 set options2 { "additional_flags=-Dvxworks" }
2258 lappend options "libs=[target_info gdb_stub]";
2259 set options [concat $options2 $options]
2260 }
2261 if [info exists GDB_TESTCASE_OPTIONS] {
2262 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
2263 }
2264 verbose "options are $options"
2265 verbose "source is $source $dest $type $options"
2266
f1c47eb2
MS
2267 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
2268
2269 if {[target_info exists needs_status_wrapper] && \
2270 [target_info needs_status_wrapper] != "0" && \
2271 [info exists gdb_wrapper_file]} {
2272 lappend options "libs=${gdb_wrapper_file}"
2273 lappend options "ldflags=${gdb_wrapper_flags}"
2274 }
2275
fc91c6c2
PB
2276 # Replace the "nowarnings" option with the appropriate additional_flags
2277 # to disable compiler warnings.
2278 set nowarnings [lsearch -exact $options nowarnings]
2279 if {$nowarnings != -1} {
2280 if [target_info exists gdb,nowarnings_flag] {
2281 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
2282 } else {
2283 set flag "additional_flags=-w"
2284 }
2285 set options [lreplace $options $nowarnings $nowarnings $flag]
2286 }
2287
f747e0ce
PA
2288 if { $type == "executable" } {
2289 if { ([istarget "*-*-mingw*"]
56643c5e 2290 || [istarget "*-*-*djgpp"]
f747e0ce
PA
2291 || [istarget "*-*-cygwin*"])} {
2292 # Force output to unbuffered mode, by linking in an object file
2293 # with a global contructor that calls setvbuf.
2294 #
2295 # Compile the special object seperatelly for two reasons:
2296 # 1) Insulate it from $options.
2297 # 2) Avoid compiling it for every gdb_compile invocation,
2298 # which is time consuming, especially if we're remote
2299 # host testing.
2300 #
2301 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
2302 verbose "compiling gdb_saved_set_unbuffered_obj"
2303 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
2304 set unbuf_obj ${objdir}/set_unbuffered_mode.o
2305
2306 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
2307 if { $result != "" } {
2308 return $result
2309 }
2310
2311 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
2312 # Link a copy of the output object, because the
2313 # original may be automatically deleted.
2314 remote_exec host "cp -f $unbuf_obj $gdb_saved_set_unbuffered_mode_obj"
2315 } else {
2316 verbose "gdb_saved_set_unbuffered_obj already compiled"
2317 }
2318
2319 # Rely on the internal knowledge that the global ctors are ran in
2320 # reverse link order. In that case, we can use ldflags to
2321 # avoid copying the object file to the host multiple
2322 # times.
ace5c364
PM
2323 # This object can only be added if standard libraries are
2324 # used. Thus, we need to disable it if -nostdlib option is used
2325 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
2326 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
2327 }
f747e0ce
PA
2328 }
2329 }
2330
c906108c 2331 set result [target_compile $source $dest $type $options];
93f02886
DJ
2332
2333 # Prune uninteresting compiler (and linker) output.
2334 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
2335
c906108c
SS
2336 regsub "\[\r\n\]*$" "$result" "" result;
2337 regsub "^\[\r\n\]*" "$result" "" result;
ec3c07fc
NS
2338
2339 if {[lsearch $options quiet] < 0} {
2340 # We shall update this on a per language basis, to avoid
2341 # changing the entire testsuite in one go.
2342 if {[lsearch $options f77] >= 0} {
2343 gdb_compile_test $source $result
2344 } elseif { $result != "" } {
2345 clone_output "gdb compile failed, $result"
2346 }
c906108c
SS
2347 }
2348 return $result;
2349}
2350
b6ff0e81
JB
2351
2352# This is just like gdb_compile, above, except that it tries compiling
2353# against several different thread libraries, to see which one this
2354# system has.
2355proc gdb_compile_pthreads {source dest type options} {
0ae67eb3 2356 set built_binfile 0
b6ff0e81 2357 set why_msg "unrecognized error"
24486cb7 2358 foreach lib {-lpthreads -lpthread -lthread ""} {
b6ff0e81
JB
2359 # This kind of wipes out whatever libs the caller may have
2360 # set. Or maybe theirs will override ours. How infelicitous.
b5ab8ff3 2361 set options_with_lib [concat $options [list libs=$lib quiet]]
b6ff0e81
JB
2362 set ccout [gdb_compile $source $dest $type $options_with_lib]
2363 switch -regexp -- $ccout {
2364 ".*no posix threads support.*" {
2365 set why_msg "missing threads include file"
2366 break
2367 }
2368 ".*cannot open -lpthread.*" {
2369 set why_msg "missing runtime threads library"
2370 }
2371 ".*Can't find library for -lpthread.*" {
2372 set why_msg "missing runtime threads library"
2373 }
2374 {^$} {
2375 pass "successfully compiled posix threads test case"
2376 set built_binfile 1
2377 break
2378 }
2379 }
2380 }
0ae67eb3 2381 if {!$built_binfile} {
b6ff0e81
JB
2382 unsupported "Couldn't compile $source: ${why_msg}"
2383 return -1
2384 }
57bf0e56
DJ
2385}
2386
2387# Build a shared library from SOURCES. You must use get_compiler_info
2388# first.
2389
2390proc gdb_compile_shlib {sources dest options} {
2391 set obj_options $options
2392
2393 switch -glob [test_compiler_info] {
2394 "xlc-*" {
2395 lappend obj_options "additional_flags=-qpic"
2396 }
2397 "gcc-*" {
2398 if { !([istarget "powerpc*-*-aix*"]
227c54da
DJ
2399 || [istarget "rs6000*-*-aix*"]
2400 || [istarget "*-*-cygwin*"]
2401 || [istarget "*-*-mingw*"]
2402 || [istarget "*-*-pe*"]) } {
57bf0e56
DJ
2403 lappend obj_options "additional_flags=-fpic"
2404 }
2405 }
2406 default {
2407 switch -glob [istarget] {
2408 "hppa*-hp-hpux*" {
2409 lappend obj_options "additional_flags=+z"
2410 }
2411 "mips-sgi-irix*" {
2412 # Disable SGI compiler's implicit -Dsgi
2413 lappend obj_options "additional_flags=-Usgi"
2414 }
2415 default {
2416 # don't know what the compiler is...
2417 }
2418 }
2419 }
2420 }
2421
2422 set outdir [file dirname $dest]
2423 set objects ""
2424 foreach source $sources {
2425 set sourcebase [file tail $source]
2426 if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
2427 return -1
2428 }
2429 lappend objects ${outdir}/${sourcebase}.o
2430 }
2431
2432 if [istarget "hppa*-*-hpux*"] {
2433 remote_exec build "ld -b ${objects} -o ${dest}"
2434 } else {
2435 set link_options $options
2436 if [test_compiler_info "xlc-*"] {
2437 lappend link_options "additional_flags=-qmkshrobj"
2438 } else {
2439 lappend link_options "additional_flags=-shared"
93f02886
DJ
2440
2441 if { ([istarget "*-*-mingw*"]
2442 || [istarget *-*-cygwin*]
2443 || [istarget *-*-pe*])} {
2444 lappend link_options "additional_flags=-Wl,--out-implib,${dest}.a"
31f83dc5
UW
2445 } elseif [is_remote target] {
2446 # By default, we do not set the soname. This causes the linker
2447 # on ELF systems to create a DT_NEEDED entry in the executable
2448 # refering to the full path name of the library. This is a
2449 # problem in remote testing if the library is in a different
2450 # directory there. To fix this, we set a soname of just the
2451 # base filename for the library, and add an appropriate -rpath
2452 # to the main executable (in gdb_compile).
2453 set destbase [file tail $dest]
2454 lappend link_options "additional_flags=-Wl,-soname,$destbase"
2455 }
57bf0e56
DJ
2456 }
2457 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
2458 return -1
2459 }
2460 }
b6ff0e81
JB
2461}
2462
756d88a7
UW
2463# This is just like gdb_compile_shlib, above, except that it tries compiling
2464# against several different thread libraries, to see which one this
2465# system has.
2466proc gdb_compile_shlib_pthreads {sources dest options} {
2467 set built_binfile 0
2468 set why_msg "unrecognized error"
2469 foreach lib {-lpthreads -lpthread -lthread ""} {
2470 # This kind of wipes out whatever libs the caller may have
2471 # set. Or maybe theirs will override ours. How infelicitous.
2472 set options_with_lib [concat $options [list libs=$lib quiet]]
2473 set ccout [gdb_compile_shlib $sources $dest $options_with_lib]
2474 switch -regexp -- $ccout {
2475 ".*no posix threads support.*" {
2476 set why_msg "missing threads include file"
2477 break
2478 }
2479 ".*cannot open -lpthread.*" {
2480 set why_msg "missing runtime threads library"
2481 }
2482 ".*Can't find library for -lpthread.*" {
2483 set why_msg "missing runtime threads library"
2484 }
2485 {^$} {
2486 pass "successfully compiled posix threads test case"
2487 set built_binfile 1
2488 break
2489 }
2490 }
2491 }
2492 if {!$built_binfile} {
2493 unsupported "Couldn't compile $sources: ${why_msg}"
2494 return -1
2495 }
2496}
2497
130cacce
AF
2498# This is just like gdb_compile_pthreads, above, except that we always add the
2499# objc library for compiling Objective-C programs
2500proc gdb_compile_objc {source dest type options} {
2501 set built_binfile 0
2502 set why_msg "unrecognized error"
2503 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
2504 # This kind of wipes out whatever libs the caller may have
2505 # set. Or maybe theirs will override ours. How infelicitous.
2506 if { $lib == "solaris" } {
2507 set lib "-lpthread -lposix4"
2508 }
2509 if { $lib != "-lobjc" } {
2510 set lib "-lobjc $lib"
2511 }
2512 set options_with_lib [concat $options [list libs=$lib quiet]]
2513 set ccout [gdb_compile $source $dest $type $options_with_lib]
2514 switch -regexp -- $ccout {
2515 ".*no posix threads support.*" {
2516 set why_msg "missing threads include file"
2517 break
2518 }
2519 ".*cannot open -lpthread.*" {
2520 set why_msg "missing runtime threads library"
2521 }
2522 ".*Can't find library for -lpthread.*" {
2523 set why_msg "missing runtime threads library"
2524 }
2525 {^$} {
2526 pass "successfully compiled objc with posix threads test case"
2527 set built_binfile 1
2528 break
2529 }
2530 }
2531 }
2532 if {!$built_binfile} {
2533 unsupported "Couldn't compile $source: ${why_msg}"
2534 return -1
2535 }
2536}
2537
c906108c
SS
2538proc send_gdb { string } {
2539 global suppress_flag;
2540 if { $suppress_flag } {
2541 return "suppressed";
2542 }
2543 return [remote_send host "$string"];
2544}
2545
2546#
2547#
2548
2549proc gdb_expect { args } {
2550 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
2f34202f 2551 set atimeout [lindex $args 0];
c906108c
SS
2552 set expcode [list [lindex $args 1]];
2553 } else {
c906108c 2554 set expcode $args;
2f34202f
MR
2555 }
2556
2557 upvar timeout timeout;
2558
2559 if [target_info exists gdb,timeout] {
2560 if [info exists timeout] {
2561 if { $timeout < [target_info gdb,timeout] } {
c906108c 2562 set gtimeout [target_info gdb,timeout];
2f34202f
MR
2563 } else {
2564 set gtimeout $timeout;
c906108c 2565 }
2f34202f
MR
2566 } else {
2567 set gtimeout [target_info gdb,timeout];
c906108c 2568 }
2f34202f 2569 }
c906108c 2570
2f34202f
MR
2571 if ![info exists gtimeout] {
2572 global timeout;
2573 if [info exists timeout] {
2574 set gtimeout $timeout;
2575 }
2576 }
2577
2578 if [info exists atimeout] {
2579 if { ![info exists gtimeout] || $gtimeout < $atimeout } {
db16b772 2580 set gtimeout $atimeout;
2f34202f
MR
2581 }
2582 } else {
c906108c 2583 if ![info exists gtimeout] {
2f34202f
MR
2584 # Eeeeew.
2585 set gtimeout 60;
c906108c
SS
2586 }
2587 }
2f34202f 2588
c906108c
SS
2589 global suppress_flag;
2590 global remote_suppress_flag;
2591 if [info exists remote_suppress_flag] {
2592 set old_val $remote_suppress_flag;
2593 }
2594 if [info exists suppress_flag] {
2595 if { $suppress_flag } {
2596 set remote_suppress_flag 1;
2597 }
2598 }
a0b3c4fd 2599 set code [catch \
5f279fa6 2600 {uplevel remote_expect host $gtimeout $expcode} string];
c906108c
SS
2601 if [info exists old_val] {
2602 set remote_suppress_flag $old_val;
2603 } else {
2604 if [info exists remote_suppress_flag] {
2605 unset remote_suppress_flag;
2606 }
2607 }
2608
2609 if {$code == 1} {
2610 global errorInfo errorCode;
2611
2612 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
2613 } elseif {$code == 2} {
2614 return -code return $string
2615 } elseif {$code == 3} {
2616 return
2617 } elseif {$code > 4} {
2618 return -code $code $string
2619 }
2620}
2621
5fa290c1 2622# gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
085dd6e6
JM
2623#
2624# Check for long sequence of output by parts.
5fa290c1 2625# TEST: is the test message to be printed with the test success/fail.
085dd6e6
JM
2626# SENTINEL: Is the terminal pattern indicating that output has finished.
2627# LIST: is the sequence of outputs to match.
2628# If the sentinel is recognized early, it is considered an error.
2629#
11cf8741
JM
2630# Returns:
2631# 1 if the test failed,
2632# 0 if the test passes,
2633# -1 if there was an internal error.
5fa290c1 2634
c2d11a7d 2635proc gdb_expect_list {test sentinel list} {
085dd6e6 2636 global gdb_prompt
11cf8741 2637 global suppress_flag
085dd6e6 2638 set index 0
43ff13b4 2639 set ok 1
11cf8741
JM
2640 if { $suppress_flag } {
2641 set ok 0
a20ce2c3 2642 unresolved "${test}"
11cf8741 2643 }
43ff13b4 2644 while { ${index} < [llength ${list}] } {
085dd6e6
JM
2645 set pattern [lindex ${list} ${index}]
2646 set index [expr ${index} + 1]
6b0ecdc2 2647 verbose -log "gdb_expect_list pattern: /$pattern/" 2
085dd6e6 2648 if { ${index} == [llength ${list}] } {
43ff13b4
JM
2649 if { ${ok} } {
2650 gdb_expect {
c2d11a7d 2651 -re "${pattern}${sentinel}" {
a20ce2c3 2652 # pass "${test}, pattern ${index} + sentinel"
c2d11a7d
JM
2653 }
2654 -re "${sentinel}" {
a20ce2c3 2655 fail "${test} (pattern ${index} + sentinel)"
c2d11a7d 2656 set ok 0
43ff13b4 2657 }
5c5455dc
AC
2658 -re ".*A problem internal to GDB has been detected" {
2659 fail "${test} (GDB internal error)"
2660 set ok 0
2661 gdb_internal_error_resync
2662 }
43ff13b4 2663 timeout {
a20ce2c3 2664 fail "${test} (pattern ${index} + sentinel) (timeout)"
43ff13b4
JM
2665 set ok 0
2666 }
085dd6e6 2667 }
43ff13b4 2668 } else {
a20ce2c3 2669 # unresolved "${test}, pattern ${index} + sentinel"
085dd6e6
JM
2670 }
2671 } else {
43ff13b4
JM
2672 if { ${ok} } {
2673 gdb_expect {
2674 -re "${pattern}" {
a20ce2c3 2675 # pass "${test}, pattern ${index}"
43ff13b4 2676 }
c2d11a7d 2677 -re "${sentinel}" {
a20ce2c3 2678 fail "${test} (pattern ${index})"
43ff13b4
JM
2679 set ok 0
2680 }
5c5455dc
AC
2681 -re ".*A problem internal to GDB has been detected" {
2682 fail "${test} (GDB internal error)"
2683 set ok 0
2684 gdb_internal_error_resync
2685 }
43ff13b4 2686 timeout {
a20ce2c3 2687 fail "${test} (pattern ${index}) (timeout)"
43ff13b4
JM
2688 set ok 0
2689 }
085dd6e6 2690 }
43ff13b4 2691 } else {
a20ce2c3 2692 # unresolved "${test}, pattern ${index}"
085dd6e6
JM
2693 }
2694 }
2695 }
11cf8741 2696 if { ${ok} } {
a20ce2c3 2697 pass "${test}"
11cf8741
JM
2698 return 0
2699 } else {
2700 return 1
2701 }
085dd6e6
JM
2702}
2703
2704#
2705#
c906108c
SS
2706proc gdb_suppress_entire_file { reason } {
2707 global suppress_flag;
2708
2709 warning "$reason\n";
2710 set suppress_flag -1;
2711}
2712
2713#
2714# Set suppress_flag, which will cause all subsequent calls to send_gdb and
2715# gdb_expect to fail immediately (until the next call to
2716# gdb_stop_suppressing_tests).
2717#
2718proc gdb_suppress_tests { args } {
2719 global suppress_flag;
2720
2721 return; # fnf - disable pending review of results where
2722 # testsuite ran better without this
2723 incr suppress_flag;
2724
2725 if { $suppress_flag == 1 } {
2726 if { [llength $args] > 0 } {
2727 warning "[lindex $args 0]\n";
2728 } else {
2729 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
2730 }
2731 }
2732}
2733
2734#
2735# Clear suppress_flag.
2736#
2737proc gdb_stop_suppressing_tests { } {
2738 global suppress_flag;
2739
2740 if [info exists suppress_flag] {
2741 if { $suppress_flag > 0 } {
2742 set suppress_flag 0;
2743 clone_output "Tests restarted.\n";
2744 }
2745 } else {
2746 set suppress_flag 0;
2747 }
2748}
2749
2750proc gdb_clear_suppressed { } {
2751 global suppress_flag;
2752
2753 set suppress_flag 0;
2754}
2755
2756proc gdb_start { } {
2757 default_gdb_start
2758}
2759
2760proc gdb_exit { } {
2761 catch default_gdb_exit
2762}
2763
e63b55d1
NS
2764#
2765# gdb_load_cmd -- load a file into the debugger.
2766# ARGS - additional args to load command.
2767# return a -1 if anything goes wrong.
2768#
2769proc gdb_load_cmd { args } {
2770 global gdb_prompt
2771
2772 if [target_info exists gdb_load_timeout] {
2773 set loadtimeout [target_info gdb_load_timeout]
2774 } else {
2775 set loadtimeout 1600
2776 }
2777 send_gdb "load $args\n"
e91528f0 2778 verbose "Timeout is now $loadtimeout seconds" 2
e63b55d1
NS
2779 gdb_expect $loadtimeout {
2780 -re "Loading section\[^\r\]*\r\n" {
2781 exp_continue
2782 }
2783 -re "Start address\[\r\]*\r\n" {
2784 exp_continue
2785 }
2786 -re "Transfer rate\[\r\]*\r\n" {
2787 exp_continue
2788 }
2789 -re "Memory access error\[^\r\]*\r\n" {
2790 perror "Failed to load program"
2791 return -1
2792 }
2793 -re "$gdb_prompt $" {
2794 return 0
2795 }
2796 -re "(.*)\r\n$gdb_prompt " {
2797 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
2798 return -1
2799 }
2800 timeout {
c4b347c7 2801 perror "Timed out trying to load $args."
e63b55d1
NS
2802 return -1
2803 }
2804 }
2805 return -1
2806}
2807
759f0f0b
PA
2808# Return the filename to download to the target and load on the target
2809# for this shared library. Normally just LIBNAME, unless shared libraries
2810# for this target have separate link and load images.
2811
2812proc shlib_target_file { libname } {
2813 return $libname
2814}
2815
2816# Return the filename GDB will load symbols from when debugging this
2817# shared library. Normally just LIBNAME, unless shared libraries for
2818# this target have separate link and load images.
2819
2820proc shlib_symbol_file { libname } {
2821 return $libname
2822}
2823
56744f0a
JJ
2824# Return the filename to download to the target and load for this
2825# executable. Normally just BINFILE unless it is renamed to something
2826# else for this target.
2827
2828proc exec_target_file { binfile } {
2829 return $binfile
2830}
2831
2832# Return the filename GDB will load symbols from when debugging this
2833# executable. Normally just BINFILE unless executables for this target
2834# have separate files for symbols.
2835
2836proc exec_symbol_file { binfile } {
2837 return $binfile
2838}
2839
2840# Rename the executable file. Normally this is just BINFILE1 being renamed
2841# to BINFILE2, but some targets require multiple binary files.
2842proc gdb_rename_execfile { binfile1 binfile2 } {
faf067f1
JK
2843 file rename -force [exec_target_file ${binfile1}] \
2844 [exec_target_file ${binfile2}]
56744f0a 2845 if { [exec_target_file ${binfile1}] != [exec_symbol_file ${binfile1}] } {
faf067f1
JK
2846 file rename -force [exec_symbol_file ${binfile1}] \
2847 [exec_symbol_file ${binfile2}]
56744f0a
JJ
2848 }
2849}
2850
2851# "Touch" the executable file to update the date. Normally this is just
2852# BINFILE, but some targets require multiple files.
2853proc gdb_touch_execfile { binfile } {
faf067f1
JK
2854 set time [clock seconds]
2855 file mtime [exec_target_file ${binfile}] $time
56744f0a 2856 if { [exec_target_file ${binfile}] != [exec_symbol_file ${binfile}] } {
faf067f1 2857 file mtime [exec_symbol_file ${binfile}] $time
56744f0a
JJ
2858 }
2859}
2860
93f02886
DJ
2861# gdb_download
2862#
2863# Copy a file to the remote target and return its target filename.
2864# Schedule the file to be deleted at the end of this test.
2865
2866proc gdb_download { filename } {
2867 global cleanfiles
2868
2869 set destname [remote_download target $filename]
2870 lappend cleanfiles $destname
2871 return $destname
2872}
2873
2874# gdb_load_shlibs LIB...
2875#
2876# Copy the listed libraries to the target.
2877
2878proc gdb_load_shlibs { args } {
2879 if {![is_remote target]} {
2880 return
2881 }
2882
2883 foreach file $args {
759f0f0b 2884 gdb_download [shlib_target_file $file]
93f02886
DJ
2885 }
2886
2887 # Even if the target supplies full paths for shared libraries,
2888 # they may not be paths for this system.
2889 gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "" ""
2890}
2891
c906108c
SS
2892#
2893# gdb_load -- load a file into the debugger.
2db8e78e 2894# Many files in config/*.exp override this procedure.
c906108c
SS
2895#
2896proc gdb_load { arg } {
2897 return [gdb_file_cmd $arg]
2898}
2899
b741e217
DJ
2900# gdb_reload -- load a file into the target. Called before "running",
2901# either the first time or after already starting the program once,
2902# for remote targets. Most files that override gdb_load should now
2903# override this instead.
2904
2905proc gdb_reload { } {
2906 # For the benefit of existing configurations, default to gdb_load.
2907 # Specifying no file defaults to the executable currently being
2908 # debugged.
2909 return [gdb_load ""]
2910}
2911
c906108c
SS
2912proc gdb_continue { function } {
2913 global decimal
2914
2915 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
2916}
2917
2918proc default_gdb_init { args } {
277254ba 2919 global gdb_wrapper_initialized
f6838f81 2920 global gdb_wrapper_target
93f02886 2921 global cleanfiles
277254ba 2922
93f02886
DJ
2923 set cleanfiles {}
2924
c906108c
SS
2925 gdb_clear_suppressed;
2926
277254ba
MS
2927 # Make sure that the wrapper is rebuilt
2928 # with the appropriate multilib option.
f6838f81
DJ
2929 if { $gdb_wrapper_target != [current_target_name] } {
2930 set gdb_wrapper_initialized 0
2931 }
277254ba 2932
7b433602
JB
2933 # Unlike most tests, we have a small number of tests that generate
2934 # a very large amount of output. We therefore increase the expect
2935 # buffer size to be able to contain the entire test output.
2936 match_max -d 30000
8d417781
PM
2937 # Also set this value for the currently running GDB.
2938 match_max [match_max -d]
c906108c
SS
2939
2940 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
2941 if { [llength $args] > 0 } {
2942 global pf_prefix
2943
2944 set file [lindex $args 0];
2945
2946 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
2947 }
2948 global gdb_prompt;
2949 if [target_info exists gdb_prompt] {
2950 set gdb_prompt [target_info gdb_prompt];
2951 } else {
2952 set gdb_prompt "\\(gdb\\)"
2953 }
2954}
2955
7b356089
JB
2956# The default timeout used when testing GDB commands. We want to use
2957# the same timeout as the default dejagnu timeout, unless the user has
2958# already provided a specific value (probably through a site.exp file).
2959global gdb_test_timeout
2960if ![info exists gdb_test_timeout] {
2961 set gdb_test_timeout $timeout
2962}
2963
47050449
JB
2964# A list of global variables that GDB testcases should not use.
2965# We try to prevent their use by monitoring write accesses and raising
2966# an error when that happens.
2967set banned_variables { bug_id prms_id }
2968
41b2c92d
PM
2969# gdb_init is called by runtest at start, but also by several
2970# tests directly; gdb_finish is only called from within runtest after
2971# each test source execution.
2972# Placing several traces by repetitive calls to gdb_init leads
2973# to problems, as only one trace is removed in gdb_finish.
2974# To overcome this possible problem, we add a variable that records
2975# if the banned variables are traced.
2976set banned_variables_traced 0
2977
c906108c 2978proc gdb_init { args } {
7b356089
JB
2979 # Reset the timeout value to the default. This way, any testcase
2980 # that changes the timeout value without resetting it cannot affect
2981 # the timeout used in subsequent testcases.
2982 global gdb_test_timeout
2983 global timeout
2984 set timeout $gdb_test_timeout
2985
47050449
JB
2986 # Block writes to all banned variables...
2987 global banned_variables
41b2c92d
PM
2988 global banned_variables_traced
2989 if (!$banned_variables_traced) {
2990 foreach banned_var $banned_variables {
2991 global "$banned_var"
2992 trace add variable "$banned_var" write error
2993 }
2994 set banned_variables_traced 1
47050449
JB
2995 }
2996
e7ab5e63
AB
2997 # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same
2998 # messages as expected.
c6f2ac43 2999 setenv LC_ALL C
e7ab5e63 3000 setenv LC_CTYPE C
c6f2ac43
PA
3001 setenv LANG C
3002
e7ab5e63
AB
3003 # Don't let a .inputrc file or an existing setting of INPUTRC mess up
3004 # the test results. Even if /dev/null doesn't exist on the particular
3005 # platform, the readline library will use the default setting just by
3006 # failing to open the file. OTOH, opening /dev/null successfully will
3007 # also result in the default settings being used since nothing will be
3008 # read from this file.
3009 setenv INPUTRC "/dev/null"
3010
3011 # The gdb.base/readline.exp arrow key test relies on the standard VT100
3012 # bindings, so make sure that an appropriate terminal is selected.
3013 # The same bug doesn't show up if we use ^P / ^N instead.
3014 setenv TERM "vt100"
3015
3016 # Some tests (for example gdb.base/maint.exp) shell out from gdb to use
3017 # grep. Clear GREP_OPTIONS to make the behavoiur predictable,
3018 # especially having color output turned on can cause tests to fail.
3019 setenv GREP_OPTIONS ""
3020
03f2bd59
JK
3021 # Clear $gdbserver_reconnect_p.
3022 global gdbserver_reconnect_p
3023 set gdbserver_reconnect_p 1
3024 unset gdbserver_reconnect_p
3025
c906108c
SS
3026 return [eval default_gdb_init $args];
3027}
3028
3029proc gdb_finish { } {
93f02886
DJ
3030 global cleanfiles
3031
3032 # Exit first, so that the files are no longer in use.
3033 gdb_exit
3034
3035 if { [llength $cleanfiles] > 0 } {
3036 eval remote_file target delete $cleanfiles
3037 set cleanfiles {}
3038 }
47050449
JB
3039
3040 # Unblock write access to the banned variables. Dejagnu typically
3041 # resets some of them between testcases.
3042 global banned_variables
41b2c92d
PM
3043 global banned_variables_traced
3044 if ($banned_variables_traced) {
3045 foreach banned_var $banned_variables {
3046 global "$banned_var"
3047 trace remove variable "$banned_var" write error
3048 }
3049 set banned_variables_traced 0
47050449 3050 }
c906108c
SS
3051}
3052
3053global debug_format
7a292a7a 3054set debug_format "unknown"
c906108c
SS
3055
3056# Run the gdb command "info source" and extract the debugging format
3057# information from the output and save it in debug_format.
3058
3059proc get_debug_format { } {
3060 global gdb_prompt
3061 global verbose
3062 global expect_out
3063 global debug_format
3064
3065 set debug_format "unknown"
3066 send_gdb "info source\n"
3067 gdb_expect 10 {
919d772c 3068 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
c906108c
SS
3069 set debug_format $expect_out(1,string)
3070 verbose "debug format is $debug_format"
3071 return 1;
3072 }
3073 -re "No current source file.\r\n$gdb_prompt $" {
3074 perror "get_debug_format used when no current source file"
3075 return 0;
3076 }
3077 -re "$gdb_prompt $" {
3078 warning "couldn't check debug format (no valid response)."
3079 return 1;
3080 }
3081 timeout {
3082 warning "couldn't check debug format (timed out)."
3083 return 1;
3084 }
3085 }
3086}
3087
838ae6c4
JB
3088# Return true if FORMAT matches the debug format the current test was
3089# compiled with. FORMAT is a shell-style globbing pattern; it can use
3090# `*', `[...]', and so on.
3091#
3092# This function depends on variables set by `get_debug_format', above.
3093
3094proc test_debug_format {format} {
3095 global debug_format
3096
3097 return [expr [string match $format $debug_format] != 0]
3098}
3099
c906108c
SS
3100# Like setup_xfail, but takes the name of a debug format (DWARF 1,
3101# COFF, stabs, etc). If that format matches the format that the
3102# current test was compiled with, then the next test is expected to
3103# fail for any target. Returns 1 if the next test or set of tests is
3104# expected to fail, 0 otherwise (or if it is unknown). Must have
3105# previously called get_debug_format.
b55a4771 3106proc setup_xfail_format { format } {
838ae6c4 3107 set ret [test_debug_format $format];
b55a4771 3108
838ae6c4 3109 if {$ret} then {
b55a4771
MS
3110 setup_xfail "*-*-*"
3111 }
3112 return $ret;
3113}
c906108c 3114
6680506e
YQ
3115# Like setup_kfail, but only call setup_kfail conditionally if
3116# istarget[TARGET] returns true.
3117proc setup_kfail_for_target { PR target } {
3118 if { [istarget $target] } {
3119 setup_kfail $PR $target
3120 }
3121}
3122
c906108c
SS
3123proc gdb_step_for_stub { } {
3124 global gdb_prompt;
3125
3126 if ![target_info exists gdb,use_breakpoint_for_stub] {
3127 if [target_info exists gdb_stub_step_command] {
3128 set command [target_info gdb_stub_step_command];
3129 } else {
3130 set command "step";
3131 }
3132 send_gdb "${command}\n";
3133 set tries 0;
3134 gdb_expect 60 {
3135 -re "(main.* at |.*in .*start).*$gdb_prompt" {
3136 return;
3137 }
3138 -re ".*$gdb_prompt" {
3139 incr tries;
3140 if { $tries == 5 } {
3141 fail "stepping out of breakpoint function";
3142 return;
3143 }
3144 send_gdb "${command}\n";
3145 exp_continue;
3146 }
3147 default {
3148 fail "stepping out of breakpoint function";
3149 return;
3150 }
3151 }
3152 }
3153 send_gdb "where\n";
3154 gdb_expect {
3155 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
3156 set file $expect_out(1,string);
3157 set linenum [expr $expect_out(2,string) + 1];
3158 set breakplace "${file}:${linenum}";
3159 }
3160 default {}
3161 }
3162 send_gdb "break ${breakplace}\n";
3163 gdb_expect 60 {
3164 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
3165 set breakpoint $expect_out(1,string);
3166 }
3167 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
3168 set breakpoint $expect_out(1,string);
3169 }
3170 default {}
3171 }
3172 send_gdb "continue\n";
3173 gdb_expect 60 {
3174 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
3175 gdb_test "delete $breakpoint" ".*" "";
3176 return;
3177 }
3178 default {}
3179 }
3180}
3181
c6fee705
MC
3182# gdb_get_line_number TEXT [FILE]
3183#
3184# Search the source file FILE, and return the line number of the
3185# first line containing TEXT. If no match is found, return -1.
3186#
3187# TEXT is a string literal, not a regular expression.
3188#
3189# The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
3190# specified, and does not start with "/", then it is assumed to be in
3191# "$srcdir/$subdir". This is awkward, and can be fixed in the future,
3192# by changing the callers and the interface at the same time.
3193# In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
3194# gdb.base/ena-dis-br.exp.
3195#
3196# Use this function to keep your test scripts independent of the
3197# exact line numbering of the source file. Don't write:
3198#
3199# send_gdb "break 20"
3200#
3201# This means that if anyone ever edits your test's source file,
3202# your test could break. Instead, put a comment like this on the
3203# source file line you want to break at:
3204#
3205# /* breakpoint spot: frotz.exp: test name */
3206#
3207# and then write, in your test script (which we assume is named
3208# frotz.exp):
3209#
3210# send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
3211#
3212# (Yes, Tcl knows how to handle the nested quotes and brackets.
3213# Try this:
3214# $ tclsh
3215# % puts "foo [lindex "bar baz" 1]"
3216# foo baz
3217# %
3218# Tcl is quite clever, for a little stringy language.)
3219#
3220# ===
3221#
3222# The previous implementation of this procedure used the gdb search command.
3223# This version is different:
3224#
3225# . It works with MI, and it also works when gdb is not running.
3226#
3227# . It operates on the build machine, not the host machine.
3228#
3229# . For now, this implementation fakes a current directory of
3230# $srcdir/$subdir to be compatible with the old implementation.
3231# This will go away eventually and some callers will need to
3232# be changed.
3233#
3234# . The TEXT argument is literal text and matches literally,
3235# not a regular expression as it was before.
3236#
3237# . State changes in gdb, such as changing the current file
3238# and setting $_, no longer happen.
3239#
3240# After a bit of time we can forget about the differences from the
3241# old implementation.
3242#
3243# --chastain 2004-08-05
3244
3245proc gdb_get_line_number { text { file "" } } {
3246 global srcdir
3247 global subdir
3248 global srcfile
c906108c 3249
c6fee705
MC
3250 if { "$file" == "" } then {
3251 set file "$srcfile"
3252 }
3253 if { ! [regexp "^/" "$file"] } then {
3254 set file "$srcdir/$subdir/$file"
c906108c
SS
3255 }
3256
c6fee705
MC
3257 if { [ catch { set fd [open "$file"] } message ] } then {
3258 perror "$message"
3259 return -1
c906108c 3260 }
c6fee705
MC
3261
3262 set found -1
3263 for { set line 1 } { 1 } { incr line } {
3264 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
3265 perror "$message"
3266 return -1
3267 }
3268 if { $nchar < 0 } then {
3269 break
3270 }
3271 if { [string first "$text" "$body"] >= 0 } then {
3272 set found $line
3273 break
3274 }
3275 }
3276
3277 if { [ catch { close "$fd" } message ] } then {
3278 perror "$message"
3279 return -1
3280 }
3281
3282 return $found
c906108c
SS
3283}
3284
7a292a7a
SS
3285# gdb_continue_to_end:
3286# The case where the target uses stubs has to be handled specially. If a
3287# stub is used, we set a breakpoint at exit because we cannot rely on
3288# exit() behavior of a remote target.
3289#
fda326dd
TT
3290# MSSG is the error message that gets printed. If not given, a
3291# default is used.
3292# COMMAND is the command to invoke. If not given, "continue" is
3293# used.
eceb0c5f
TT
3294# ALLOW_EXTRA is a flag indicating whether the test should expect
3295# extra output between the "Continuing." line and the program
3296# exiting. By default it is zero; if nonzero, any extra output
3297# is accepted.
fda326dd 3298
eceb0c5f 3299proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} {
fda326dd 3300 global inferior_exited_re
7a292a7a 3301
fda326dd
TT
3302 if {$mssg == ""} {
3303 set text "continue until exit"
3304 } else {
3305 set text "continue until exit at $mssg"
3306 }
eceb0c5f
TT
3307 if {$allow_extra} {
3308 set extra ".*"
3309 } else {
3310 set extra ""
3311 }
7a292a7a
SS
3312 if [target_info exists use_gdb_stub] {
3313 if {![gdb_breakpoint "exit"]} {
3314 return 0
3315 }
eceb0c5f 3316 gdb_test $command "Continuing..*Breakpoint .*exit.*" \
fda326dd 3317 $text
7a292a7a
SS
3318 } else {
3319 # Continue until we exit. Should not stop again.
3320 # Don't bother to check the output of the program, that may be
3321 # extremely tough for some remote systems.
eceb0c5f
TT
3322 gdb_test $command \
3323 "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\
fda326dd 3324 $text
7a292a7a
SS
3325 }
3326}
3327
3328proc rerun_to_main {} {
3329 global gdb_prompt
3330
3331 if [target_info exists use_gdb_stub] {
3332 gdb_run_cmd
3333 gdb_expect {
3334 -re ".*Breakpoint .*main .*$gdb_prompt $"\
3335 {pass "rerun to main" ; return 0}
3336 -re "$gdb_prompt $"\
3337 {fail "rerun to main" ; return 0}
3338 timeout {fail "(timeout) rerun to main" ; return 0}
3339 }
3340 } else {
3341 send_gdb "run\n"
3342 gdb_expect {
11350d2a
CV
3343 -re "The program .* has been started already.*y or n. $" {
3344 send_gdb "y\n"
3345 exp_continue
3346 }
7a292a7a
SS
3347 -re "Starting program.*$gdb_prompt $"\
3348 {pass "rerun to main" ; return 0}
3349 -re "$gdb_prompt $"\
3350 {fail "rerun to main" ; return 0}
3351 timeout {fail "(timeout) rerun to main" ; return 0}
3352 }
3353 }
3354}
c906108c 3355
13a5e3b8
MS
3356# Print a message and return true if a test should be skipped
3357# due to lack of floating point suport.
3358
3359proc gdb_skip_float_test { msg } {
3360 if [target_info exists gdb,skip_float_tests] {
3361 verbose "Skipping test '$msg': no float tests.";
3362 return 1;
3363 }
3364 return 0;
3365}
3366
3367# Print a message and return true if a test should be skipped
3368# due to lack of stdio support.
3369
3370proc gdb_skip_stdio_test { msg } {
3371 if [target_info exists gdb,noinferiorio] {
3372 verbose "Skipping test '$msg': no inferior i/o.";
3373 return 1;
3374 }
3375 return 0;
3376}
3377
3378proc gdb_skip_bogus_test { msg } {
3379 return 0;
3380}
3381
e515b470
DJ
3382# Return true if a test should be skipped due to lack of XML support
3383# in the host GDB.
d0ef5df8 3384# NOTE: This must be called while gdb is *not* running.
e515b470
DJ
3385
3386proc gdb_skip_xml_test { } {
3387 global gdb_prompt
3388 global srcdir
3389 global xml_missing_cached
3390
3391 if {[info exists xml_missing_cached]} {
3392 return $xml_missing_cached
3393 }
3394
3395 gdb_start
3396 set xml_missing_cached 0
3397 gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" {
3398 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
3399 set xml_missing_cached 1
3400 }
3401 -re ".*$gdb_prompt $" { }
3402 }
3403 gdb_exit
3404 return $xml_missing_cached
3405}
1f8a6abb
EZ
3406
3407# Note: the procedure gdb_gnu_strip_debug will produce an executable called
3408# ${binfile}.dbglnk, which is just like the executable ($binfile) but without
3409# the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
8e1d0c49
JK
3410# the name of a debuginfo only file. This file will be stored in the same
3411# subdirectory.
1f8a6abb
EZ
3412
3413# Functions for separate debug info testing
3414
3415# starting with an executable:
3416# foo --> original executable
3417
3418# at the end of the process we have:
3419# foo.stripped --> foo w/o debug info
8e1d0c49 3420# foo.debug --> foo's debug info
1f8a6abb
EZ
3421# foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
3422
4935890f
JK
3423# Return the build-id hex string (usually 160 bits as 40 hex characters)
3424# converted to the form: .build-id/ab/cdef1234...89.debug
3425# Return "" if no build-id found.
3426proc build_id_debug_filename_get { exec } {
3427 set tmp "${exec}-tmp"
8b3fc8d8
MK
3428 set objcopy_program [transform objcopy]
3429
3430 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $exec $tmp" output]
3431 verbose "result is $result"
3432 verbose "output is $output"
3433 if {$result == 1} {
3434 return ""
3435 }
4935890f 3436 set fi [open $tmp]
b7fca990 3437 fconfigure $fi -translation binary
4935890f
JK
3438 # Skip the NOTE header.
3439 read $fi 16
3440 set data [read $fi]
3441 close $fi
3442 file delete $tmp
7020f05c 3443 if ![string compare $data ""] then {
4935890f
JK
3444 return ""
3445 }
3446 # Convert it to hex.
3447 binary scan $data H* data
061b5285 3448 regsub {^..} $data {\0/} data
4935890f
JK
3449 return ".build-id/${data}.debug";
3450}
3451
94277a38
DJ
3452# Create stripped files for DEST, replacing it. If ARGS is passed, it is a
3453# list of optional flags. The only currently supported flag is no-main,
3454# which removes the symbol entry for main from the separate debug file.
c0201579
JK
3455#
3456# Function returns zero on success. Function will return non-zero failure code
3457# on some targets not supporting separate debug info (such as i386-msdos).
1f8a6abb 3458
94277a38
DJ
3459proc gdb_gnu_strip_debug { dest args } {
3460
8e1d0c49
JK
3461 # Use the first separate debug info file location searched by GDB so the
3462 # run cannot be broken by some stale file searched with higher precedence.
3463 set debug_file "${dest}.debug"
3464
b741e217
DJ
3465 set strip_to_file_program [transform strip]
3466 set objcopy_program [transform objcopy]
1f8a6abb 3467
1f8a6abb
EZ
3468 set debug_link [file tail $debug_file]
3469 set stripped_file "${dest}.stripped"
3470
3471 # Get rid of the debug info, and store result in stripped_file
3472 # something like gdb/testsuite/gdb.base/blah.stripped.
3473 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
3474 verbose "result is $result"
3475 verbose "output is $output"
3476 if {$result == 1} {
3477 return 1
3478 }
3479
d521f563
JK
3480 # Workaround PR binutils/10802:
3481 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3482 set perm [file attributes ${dest} -permissions]
3483 file attributes ${stripped_file} -permissions $perm
3484
1f8a6abb
EZ
3485 # Get rid of everything but the debug info, and store result in debug_file
3486 # This will be in the .debug subdirectory, see above.
3487 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
3488 verbose "result is $result"
3489 verbose "output is $output"
3490 if {$result == 1} {
3491 return 1
3492 }
3493
94277a38
DJ
3494 # If no-main is passed, strip the symbol for main from the separate
3495 # file. This is to simulate the behavior of elfutils's eu-strip, which
3496 # leaves the symtab in the original file only. There's no way to get
3497 # objcopy or strip to remove the symbol table without also removing the
3498 # debugging sections, so this is as close as we can get.
3499 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
3500 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
3501 verbose "result is $result"
3502 verbose "output is $output"
3503 if {$result == 1} {
3504 return 1
3505 }
3506 file delete "${debug_file}"
3507 file rename "${debug_file}-tmp" "${debug_file}"
3508 }
3509
1f8a6abb
EZ
3510 # Link the two previous output files together, adding the .gnu_debuglink
3511 # section to the stripped_file, containing a pointer to the debug_file,
3512 # save the new file in dest.
3513 # This will be the regular executable filename, in the usual location.
3514 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
3515 verbose "result is $result"
3516 verbose "output is $output"
3517 if {$result == 1} {
3518 return 1
3519 }
3520
d521f563
JK
3521 # Workaround PR binutils/10802:
3522 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3523 set perm [file attributes ${stripped_file} -permissions]
3524 file attributes ${dest} -permissions $perm
3525
3526 return 0
1f8a6abb
EZ
3527}
3528
d8295fe9
VP
3529# Test the output of GDB_COMMAND matches the pattern obtained
3530# by concatenating all elements of EXPECTED_LINES. This makes
3531# it possible to split otherwise very long string into pieces.
3532# If third argument is not empty, it's used as the name of the
3533# test to be printed on pass/fail.
3534proc help_test_raw { gdb_command expected_lines args } {
3535 set message $gdb_command
3536 if [llength $args]>0 then {
3537 set message [lindex $args 0]
3538 }
3539 set expected_output [join $expected_lines ""]
3540 gdb_test "${gdb_command}" "${expected_output}" $message
3541}
3542
3543# Test the output of "help COMMNAD_CLASS". EXPECTED_INITIAL_LINES
3544# are regular expressions that should match the beginning of output,
3545# before the list of commands in that class. The presence of
3546# command list and standard epilogue will be tested automatically.
3547proc test_class_help { command_class expected_initial_lines args } {
3548 set l_stock_body {
3549 "List of commands\:.*\[\r\n\]+"
3550 "Type \"help\" followed by command name for full documentation\.\[\r\n\]+"
3551 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+"
3552 "Command name abbreviations are allowed if unambiguous\."
3553 }
3554 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3555
3556 eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
3557}
3558
3559# COMMAND_LIST should have either one element -- command to test, or
3560# two elements -- abbreviated command to test, and full command the first
3561# element is abbreviation of.
3562# The command must be a prefix command. EXPECTED_INITIAL_LINES
3563# are regular expressions that should match the beginning of output,
3564# before the list of subcommands. The presence of
3565# subcommand list and standard epilogue will be tested automatically.
3566proc test_prefix_command_help { command_list expected_initial_lines args } {
3567 set command [lindex $command_list 0]
3568 if {[llength $command_list]>1} {
3569 set full_command [lindex $command_list 1]
3570 } else {
3571 set full_command $command
3572 }
3573 # Use 'list' and not just {} because we want variables to
3574 # be expanded in this list.
3575 set l_stock_body [list\
3576 "List of $full_command subcommands\:.*\[\r\n\]+"\
3577 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
3578 "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
3579 "Command name abbreviations are allowed if unambiguous\."]
3580 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3581 if {[llength $args]>0} {
3582 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
3583 } else {
3584 help_test_raw "help ${command}" $l_entire_body
3585 }
3586}
dbc52822
VP
3587
3588# Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
3589# provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
3590# to pass to untested, if something is wrong. OPTIONS are passed
3591# to gdb_compile directly.
3592proc build_executable { testname executable {sources ""} {options {debug}} } {
3593
3594 global objdir
3595 global subdir
3596 global srcdir
3597 if {[llength $sources]==0} {
3598 set sources ${executable}.c
3599 }
3600
3601 set binfile ${objdir}/${subdir}/${executable}
3602
3603 set objects {}
3604 for {set i 0} "\$i<[llength $sources]" {incr i} {
3605 set s [lindex $sources $i]
3606 if { [gdb_compile "${srcdir}/${subdir}/${s}" "${binfile}${i}.o" object $options] != "" } {
3607 untested $testname
3608 return -1
3609 }
3610 lappend objects "${binfile}${i}.o"
3611 }
3612
3613 if { [gdb_compile $objects "${binfile}" executable $options] != "" } {
3614 untested $testname
3615 return -1
3616 }
3617
fd961404
DE
3618 set info_options ""
3619 if { [lsearch -exact $options "c++"] >= 0 } {
3620 set info_options "c++"
3621 }
3622 if [get_compiler_info ${binfile} ${info_options}] {
dbc52822
VP
3623 return -1
3624 }
3625 return 0
3626}
3627
3628# Starts fresh GDB binary and loads EXECUTABLE into GDB. EXECUTABLE is
3629# the name of binary in ${objdir}/${subdir}.
3630proc clean_restart { executable } {
3631 global srcdir
3632 global objdir
3633 global subdir
3634 set binfile ${objdir}/${subdir}/${executable}
3635
3636 gdb_exit
3637 gdb_start
3638 gdb_reinitialize_dir $srcdir/$subdir
3639 gdb_load ${binfile}
3640
3641 if [target_info exists gdb_stub] {
3642 gdb_step_for_stub;
3643 }
3644}
3645
3646# Prepares for testing, by calling build_executable, and then clean_restart.
3647# Please refer to build_executable for parameter description.
3648proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
3649
734a5c36 3650 if {[build_executable $testname $executable $sources $options] == -1} {
dbc52822
VP
3651 return -1
3652 }
3653 clean_restart $executable
3654
3655 return 0
3656}
7065b901
TT
3657
3658proc get_valueof { fmt exp default } {
3659 global gdb_prompt
3660
3661 set test "get valueof \"${exp}\""
3662 set val ${default}
3663 gdb_test_multiple "print${fmt} ${exp}" "$test" {
417e16e2
PM
3664 -re "\\$\[0-9\]* = (.*)\[\r\n\]*$gdb_prompt $" {
3665 set val $expect_out(1,string)
3666 pass "$test ($val)"
3667 }
3668 timeout {
3669 fail "$test (timeout)"
3670 }
3671 }
3672 return ${val}
3673}
3674
3675proc get_integer_valueof { exp default } {
3676 global gdb_prompt
3677
3678 set test "get integer valueof \"${exp}\""
3679 set val ${default}
3680 gdb_test_multiple "print /d ${exp}" "$test" {
7065b901
TT
3681 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
3682 set val $expect_out(1,string)
3683 pass "$test ($val)"
3684 }
3685 timeout {
417e16e2 3686 fail "$test (timeout)"
7065b901
TT
3687 }
3688 }
3689 return ${val}
3690}
3691
faafb047
PM
3692proc get_hexadecimal_valueof { exp default } {
3693 global gdb_prompt
3694 send_gdb "print /x ${exp}\n"
3695 set test "get hexadecimal valueof \"${exp}\""
3696 gdb_expect {
3697 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
3698 set val $expect_out(1,string)
3699 pass "$test"
3700 }
3701 timeout {
3702 set val ${default}
3703 fail "$test (timeout)"
3704 }
3705 }
3706 return ${val}
3707}
417e16e2 3708
7065b901 3709proc get_sizeof { type default } {
417e16e2 3710 return [get_integer_valueof "sizeof (${type})" $default]
7065b901
TT
3711}
3712
db863c42
MF
3713# Get the current value for remotetimeout and return it.
3714proc get_remotetimeout { } {
3715 global gdb_prompt
3716 global decimal
3717
3718 gdb_test_multiple "show remotetimeout" "" {
3719 -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" {
3720 return $expect_out(1,string);
3721 }
3722 }
3723
3724 # Pick the default that gdb uses
3725 warning "Unable to read remotetimeout"
3726 return 300
3727}
3728
3729# Set the remotetimeout to the specified timeout. Nothing is returned.
3730proc set_remotetimeout { timeout } {
3731 global gdb_prompt
3732
3733 gdb_test_multiple "set remotetimeout $timeout" "" {
3734 -re "$gdb_prompt $" {
3735 verbose "Set remotetimeout to $timeout\n"
3736 }
3737 }
3738}
3739
812f7342
TT
3740# Log gdb command line and script if requested.
3741if {[info exists TRANSCRIPT]} {
3742 rename send_gdb real_send_gdb
3743 rename remote_spawn real_remote_spawn
3744 rename remote_close real_remote_close
3745
3746 global gdb_transcript
3747 set gdb_transcript ""
3748
3749 global gdb_trans_count
3750 set gdb_trans_count 1
3751
3752 proc remote_spawn {args} {
3753 global gdb_transcript gdb_trans_count outdir
3754
3755 if {$gdb_transcript != ""} {
3756 close $gdb_transcript
3757 }
3758 set gdb_transcript [open [file join $outdir transcript.$gdb_trans_count] w]
3759 puts $gdb_transcript [lindex $args 1]
3760 incr gdb_trans_count
3761
3762 return [uplevel real_remote_spawn $args]
3763 }
3764
3765 proc remote_close {args} {
3766 global gdb_transcript
3767
3768 if {$gdb_transcript != ""} {
3769 close $gdb_transcript
3770 set gdb_transcript ""
3771 }
3772
3773 return [uplevel real_remote_close $args]
3774 }
3775
3776 proc send_gdb {args} {
3777 global gdb_transcript
3778
3779 if {$gdb_transcript != ""} {
3780 puts -nonewline $gdb_transcript [lindex $args 0]
3781 }
3782
3783 return [uplevel real_send_gdb $args]
3784 }
3785}
37aeb5df 3786
bbfba9ed 3787proc core_find {binfile {deletefiles {}} {arg ""}} {
37aeb5df
JK
3788 global objdir subdir
3789
3790 set destcore "$binfile.core"
3791 file delete $destcore
3792
3793 # Create a core file named "$destcore" rather than just "core", to
3794 # avoid problems with sys admin types that like to regularly prune all
3795 # files named "core" from the system.
3796 #
3797 # Arbitrarily try setting the core size limit to "unlimited" since
3798 # this does not hurt on systems where the command does not work and
3799 # allows us to generate a core on systems where it does.
3800 #
3801 # Some systems append "core" to the name of the program; others append
3802 # the name of the program to "core"; still others (like Linux, as of
3803 # May 2003) create cores named "core.PID". In the latter case, we
3804 # could have many core files lying around, and it may be difficult to
3805 # tell which one is ours, so let's run the program in a subdirectory.
3806 set found 0
3807 set coredir "${objdir}/${subdir}/coredir.[getpid]"
3808 file mkdir $coredir
bbfba9ed 3809 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
37aeb5df
JK
3810 # remote_exec host "${binfile}"
3811 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
3812 if [remote_file build exists $i] {
3813 remote_exec build "mv $i $destcore"
3814 set found 1
3815 }
3816 }
3817 # Check for "core.PID".
3818 if { $found == 0 } {
3819 set names [glob -nocomplain -directory $coredir core.*]
3820 if {[llength $names] == 1} {
3821 set corefile [file join $coredir [lindex $names 0]]
3822 remote_exec build "mv $corefile $destcore"
3823 set found 1
3824 }
3825 }
3826 if { $found == 0 } {
3827 # The braindamaged HPUX shell quits after the ulimit -c above
3828 # without executing ${binfile}. So we try again without the
3829 # ulimit here if we didn't find a core file above.
3830 # Oh, I should mention that any "braindamaged" non-Unix system has
3831 # the same problem. I like the cd bit too, it's really neat'n stuff.
3832 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
3833 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
3834 if [remote_file build exists $i] {
3835 remote_exec build "mv $i $destcore"
3836 set found 1
3837 }
3838 }
3839 }
3840
3841 # Try to clean up after ourselves.
3842 foreach deletefile $deletefiles {
3843 remote_file build delete [file join $coredir $deletefile]
3844 }
3845 remote_exec build "rmdir $coredir"
3846
3847 if { $found == 0 } {
3848 warning "can't generate a core file - core tests suppressed - check ulimit -c"
3849 return ""
3850 }
3851 return $destcore
3852}
ee5683ab
PM
3853
3854# gdb_target_symbol_prefix_flags returns a string that can be added
3855# to gdb_compile options to define SYMBOL_PREFIX macro value
3856# symbol_prefix_flags returns a string that can be added
3857# for targets that use underscore as symbol prefix.
3858# TODO: find out automatically if the target needs this.
3859
3860proc gdb_target_symbol_prefix_flags {} {
3861 if { [istarget "*-*-cygwin*"] || [istarget "i?86-*-mingw*"]
3862 || [istarget "*-*-msdosdjgpp*"] || [istarget "*-*-go32*"] } {
3863 return "additional_flags=-DSYMBOL_PREFIX=\"_\""
3864 } else {
3865 return ""
3866 }
3867}
3868
42159ca5
TT
3869# Always load compatibility stuff.
3870load_lib future.exp
This page took 1.568854 seconds and 4 git commands to generate.