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