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