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