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