PowerPC warning on symbol+offset@got
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
CommitLineData
42a4f53d 1# Copyright 1992-2019 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
e309aa65 31load_lib memory.exp
c906108c
SS
32
33global GDB
c906108c 34
f71c18e7
PA
35# The spawn ID used for I/O interaction with the inferior. For native
36# targets, or remote targets that can do I/O through GDB
37# (semi-hosting) this will be the same as the host/GDB's spawn ID.
38# Otherwise, the board may set this to some other spawn ID. E.g.,
39# when debugging with GDBserver, this is set to GDBserver's spawn ID,
40# so input/output is done on gdbserver's tty.
41global inferior_spawn_id
42
c906108c 43if [info exists TOOL_EXECUTABLE] {
4ec70201 44 set GDB $TOOL_EXECUTABLE
c906108c
SS
45}
46if ![info exists GDB] {
47 if ![is_remote host] {
48 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
49 } else {
4ec70201 50 set GDB [transform gdb]
c906108c
SS
51 }
52}
53verbose "using GDB = $GDB" 2
54
6b8ce727
DE
55# GDBFLAGS is available for the user to set on the command line.
56# E.g. make check RUNTESTFLAGS=GDBFLAGS=mumble
57# Testcases may use it to add additional flags, but they must:
58# - append new flags, not overwrite
59# - restore the original value when done
c906108c
SS
60global GDBFLAGS
61if ![info exists GDBFLAGS] {
6b8ce727 62 set GDBFLAGS ""
c906108c
SS
63}
64verbose "using GDBFLAGS = $GDBFLAGS" 2
65
2f4e0a80
DE
66# Make the build data directory available to tests.
67set BUILD_DATA_DIRECTORY "[pwd]/../data-directory"
68
6b8ce727 69# INTERNAL_GDBFLAGS contains flags that the testsuite requires.
1be00882
DE
70global INTERNAL_GDBFLAGS
71if ![info exists INTERNAL_GDBFLAGS] {
2f4e0a80 72 set INTERNAL_GDBFLAGS "-nw -nx -data-directory $BUILD_DATA_DIRECTORY"
1be00882 73}
6b8ce727 74
9e0b60a8 75# The variable gdb_prompt is a regexp which matches the gdb prompt.
3714cea7
DE
76# Set it if it is not already set. This is also set by default_gdb_init
77# but it's not clear what removing one of them will break.
78# See with_gdb_prompt for more details on prompt handling.
c906108c 79global gdb_prompt
9e0b60a8 80if ![info exists gdb_prompt] then {
3714cea7 81 set gdb_prompt "\\(gdb\\)"
c906108c
SS
82}
83
94696ad3 84# A regexp that matches the pagination prompt.
eb6af809
TT
85set pagination_prompt \
86 "--Type <RET> for more, q to quit, c to continue without paging--"
94696ad3 87
6006a3a1
BR
88# The variable fullname_syntax_POSIX is a regexp which matches a POSIX
89# absolute path ie. /foo/
d0b76dc6 90set fullname_syntax_POSIX {/[^\n]*/}
6006a3a1
BR
91# The variable fullname_syntax_UNC is a regexp which matches a Windows
92# UNC path ie. \\D\foo\
d0b76dc6 93set fullname_syntax_UNC {\\\\[^\\]+\\[^\n]+\\}
6006a3a1
BR
94# The variable fullname_syntax_DOS_CASE is a regexp which matches a
95# particular DOS case that GDB most likely will output
96# ie. \foo\, but don't match \\.*\
d0b76dc6 97set fullname_syntax_DOS_CASE {\\[^\\][^\n]*\\}
6006a3a1
BR
98# The variable fullname_syntax_DOS is a regexp which matches a DOS path
99# ie. a:\foo\ && a:foo\
d0b76dc6 100set fullname_syntax_DOS {[a-zA-Z]:[^\n]*\\}
6006a3a1
BR
101# The variable fullname_syntax is a regexp which matches what GDB considers
102# an absolute path. It is currently debatable if the Windows style paths
103# d:foo and \abc should be considered valid as an absolute path.
104# Also, the purpse of this regexp is not to recognize a well formed
105# absolute path, but to say with certainty that a path is absolute.
106set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
107
93076499
ND
108# Needed for some tests under Cygwin.
109global EXEEXT
110global env
111
112if ![info exists env(EXEEXT)] {
113 set EXEEXT ""
114} else {
115 set EXEEXT $env(EXEEXT)
116}
117
bb2bed55
NR
118set octal "\[0-7\]+"
119
eceb0c5f 120set inferior_exited_re "(\\\[Inferior \[0-9\]+ \\(.*\\) exited)"
fda326dd 121
fad0c9fb
PA
122# A regular expression that matches a value history number.
123# E.g., $1, $2, etc.
124set valnum_re "\\\$$decimal"
125
085dd6e6
JM
126### Only procedures should come after this point.
127
c906108c
SS
128#
129# gdb_version -- extract and print the version number of GDB
130#
131proc default_gdb_version {} {
132 global GDB
6b8ce727 133 global INTERNAL_GDBFLAGS GDBFLAGS
c906108c 134 global gdb_prompt
5e92f71a
TT
135 global inotify_pid
136
137 if {[info exists inotify_pid]} {
138 eval exec kill $inotify_pid
139 }
140
fa335448 141 set output [remote_exec host "$GDB $INTERNAL_GDBFLAGS --version"]
4ec70201 142 set tmp [lindex $output 1]
c906108c
SS
143 set version ""
144 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
145 if ![is_remote host] {
6b8ce727 146 clone_output "[which $GDB] version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
c906108c 147 } else {
6b8ce727 148 clone_output "$GDB on remote host version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
c906108c
SS
149 }
150}
151
152proc gdb_version { } {
ae59b1da 153 return [default_gdb_version]
c906108c
SS
154}
155
156#
157# gdb_unload -- unload a file if one is loaded
608e2dbb 158# Return 0 on success, -1 on error.
c906108c
SS
159#
160
161proc gdb_unload {} {
162 global verbose
163 global GDB
164 global gdb_prompt
165 send_gdb "file\n"
166 gdb_expect 60 {
167 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
168 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
959e7469 169 -re "A program is being debugged already.*Are you sure you want to change the file.*y or n. $" {
f9e2e39d 170 send_gdb "y\n" answer
c906108c
SS
171 exp_continue
172 }
173 -re "Discard symbol table from .*y or n.*$" {
f9e2e39d 174 send_gdb "y\n" answer
c906108c
SS
175 exp_continue
176 }
177 -re "$gdb_prompt $" {}
178 timeout {
975531db 179 perror "couldn't unload file in $GDB (timeout)."
c906108c
SS
180 return -1
181 }
182 }
608e2dbb 183 return 0
c906108c
SS
184}
185
186# Many of the tests depend on setting breakpoints at various places and
187# running until that breakpoint is reached. At times, we want to start
188# with a clean-slate with respect to breakpoints, so this utility proc
189# lets us do this without duplicating this code everywhere.
190#
191
192proc delete_breakpoints {} {
193 global gdb_prompt
194
a0b3c4fd
JM
195 # we need a larger timeout value here or this thing just confuses
196 # itself. May need a better implementation if possible. - guo
197 #
d8b901ed
PA
198 set timeout 100
199
200 set msg "delete all breakpoints in delete_breakpoints"
201 set deleted 0
202 gdb_test_multiple "delete breakpoints" "$msg" {
203 -re "Delete all breakpoints.*y or n.*$" {
f9e2e39d 204 send_gdb "y\n" answer
c906108c
SS
205 exp_continue
206 }
d8b901ed
PA
207 -re "$gdb_prompt $" {
208 set deleted 1
209 }
c906108c 210 }
d8b901ed
PA
211
212 if {$deleted} {
213 # Confirm with "info breakpoints".
214 set deleted 0
215 set msg "info breakpoints"
216 gdb_test_multiple $msg $msg {
217 -re "No breakpoints or watchpoints..*$gdb_prompt $" {
218 set deleted 1
219 }
220 -re "$gdb_prompt $" {
221 }
c906108c 222 }
d8b901ed
PA
223 }
224
225 if {!$deleted} {
226 perror "breakpoints not deleted"
c906108c
SS
227 }
228}
229
300b6685
PA
230# Returns true iff the target supports using the "run" command.
231
232proc target_can_use_run_cmd {} {
233 if [target_info exists use_gdb_stub] {
234 # In this case, when we connect, the inferior is already
235 # running.
236 return 0
237 }
238
239 # Assume yes.
240 return 1
241}
242
c906108c
SS
243# Generic run command.
244#
245# The second pattern below matches up to the first newline *only*.
246# Using ``.*$'' could swallow up output that we attempt to match
247# elsewhere.
248#
1d41d75c
DE
249# N.B. This function does not wait for gdb to return to the prompt,
250# that is the caller's responsibility.
251
c906108c 252proc gdb_run_cmd {args} {
e11ac3a3 253 global gdb_prompt use_gdb_stub
c906108c 254
a25eb028
MR
255 foreach command [gdb_init_commands] {
256 send_gdb "$command\n"
c906108c
SS
257 gdb_expect 30 {
258 -re "$gdb_prompt $" { }
259 default {
4ec70201
PA
260 perror "gdb_init_command for target failed"
261 return
c906108c
SS
262 }
263 }
264 }
265
e11ac3a3 266 if $use_gdb_stub {
c906108c 267 if [target_info exists gdb,do_reload_on_run] {
b741e217 268 if { [gdb_reload] != 0 } {
4ec70201 269 return
917317f4 270 }
4ec70201 271 send_gdb "continue\n"
c906108c
SS
272 gdb_expect 60 {
273 -re "Continu\[^\r\n\]*\[\r\n\]" {}
274 default {}
275 }
4ec70201 276 return
c906108c
SS
277 }
278
279 if [target_info exists gdb,start_symbol] {
4ec70201 280 set start [target_info gdb,start_symbol]
c906108c 281 } else {
4ec70201 282 set start "start"
c906108c
SS
283 }
284 send_gdb "jump *$start\n"
4ec70201 285 set start_attempt 1
917317f4
JM
286 while { $start_attempt } {
287 # Cap (re)start attempts at three to ensure that this loop
288 # always eventually fails. Don't worry about trying to be
289 # clever and not send a command when it has failed.
290 if [expr $start_attempt > 3] {
4ec70201
PA
291 perror "Jump to start() failed (retry count exceeded)"
292 return
c906108c 293 }
4ec70201 294 set start_attempt [expr $start_attempt + 1]
917317f4
JM
295 gdb_expect 30 {
296 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
4ec70201 297 set start_attempt 0
917317f4
JM
298 }
299 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
4ec70201
PA
300 perror "Can't find start symbol to run in gdb_run"
301 return
917317f4
JM
302 }
303 -re "No symbol \"start\" in current.*$gdb_prompt $" {
4ec70201 304 send_gdb "jump *_start\n"
917317f4
JM
305 }
306 -re "No symbol.*context.*$gdb_prompt $" {
4ec70201 307 set start_attempt 0
917317f4
JM
308 }
309 -re "Line.* Jump anyway.*y or n. $" {
f9e2e39d 310 send_gdb "y\n" answer
917317f4
JM
311 }
312 -re "The program is not being run.*$gdb_prompt $" {
b741e217 313 if { [gdb_reload] != 0 } {
4ec70201 314 return
917317f4 315 }
4ec70201 316 send_gdb "jump *$start\n"
917317f4
JM
317 }
318 timeout {
4ec70201 319 perror "Jump to start() failed (timeout)"
917317f4
JM
320 return
321 }
c906108c 322 }
c906108c 323 }
c906108c
SS
324 return
325 }
83f66e8f
DJ
326
327 if [target_info exists gdb,do_reload_on_run] {
b741e217 328 if { [gdb_reload] != 0 } {
4ec70201 329 return
83f66e8f
DJ
330 }
331 }
c906108c
SS
332 send_gdb "run $args\n"
333# This doesn't work quite right yet.
5aa7ddc2
PM
334# Use -notransfer here so that test cases (like chng-sym.exp)
335# may test for additional start-up messages.
336 gdb_expect 60 {
c906108c 337 -re "The program .* has been started already.*y or n. $" {
f9e2e39d 338 send_gdb "y\n" answer
c906108c
SS
339 exp_continue
340 }
bbb88ebf 341 -notransfer -re "Starting program: \[^\r\n\]*" {}
8e46892c
JK
342 -notransfer -re "$gdb_prompt $" {
343 # There is no more input expected.
344 }
c906108c
SS
345 }
346}
347
b741e217
DJ
348# Generic start command. Return 0 if we could start the program, -1
349# if we could not.
1d41d75c
DE
350#
351# N.B. This function does not wait for gdb to return to the prompt,
352# that is the caller's responsibility.
b741e217
DJ
353
354proc gdb_start_cmd {args} {
e11ac3a3 355 global gdb_prompt use_gdb_stub
b741e217 356
a25eb028
MR
357 foreach command [gdb_init_commands] {
358 send_gdb "$command\n"
b741e217
DJ
359 gdb_expect 30 {
360 -re "$gdb_prompt $" { }
361 default {
4ec70201 362 perror "gdb_init_command for target failed"
ae59b1da 363 return -1
b741e217
DJ
364 }
365 }
366 }
367
e11ac3a3 368 if $use_gdb_stub {
b741e217
DJ
369 return -1
370 }
371
372 send_gdb "start $args\n"
2de75e71
JB
373 # Use -notransfer here so that test cases (like chng-sym.exp)
374 # may test for additional start-up messages.
b741e217
DJ
375 gdb_expect 60 {
376 -re "The program .* has been started already.*y or n. $" {
f9e2e39d 377 send_gdb "y\n" answer
b741e217
DJ
378 exp_continue
379 }
b741e217
DJ
380 -notransfer -re "Starting program: \[^\r\n\]*" {
381 return 0
382 }
383 }
384 return -1
385}
386
4e5a4f58
JB
387# Generic starti command. Return 0 if we could start the program, -1
388# if we could not.
389#
390# N.B. This function does not wait for gdb to return to the prompt,
391# that is the caller's responsibility.
392
393proc gdb_starti_cmd {args} {
394 global gdb_prompt use_gdb_stub
395
396 foreach command [gdb_init_commands] {
397 send_gdb "$command\n"
398 gdb_expect 30 {
399 -re "$gdb_prompt $" { }
400 default {
401 perror "gdb_init_command for target failed"
402 return -1
403 }
404 }
405 }
406
407 if $use_gdb_stub {
408 return -1
409 }
410
411 send_gdb "starti $args\n"
412 gdb_expect 60 {
413 -re "The program .* has been started already.*y or n. $" {
f9e2e39d 414 send_gdb "y\n" answer
4e5a4f58
JB
415 exp_continue
416 }
417 -re "Starting program: \[^\r\n\]*" {
418 return 0
419 }
420 }
421 return -1
422}
423
78a1a894 424# Set a breakpoint at FUNCTION. If there is an additional argument it is
55cd6f92 425# a list of options; the supported options are allow-pending, temporary,
a20714ff 426# message, no-message, passfail and qualified.
5b7d0050
DE
427# The result is 1 for success, 0 for failure.
428#
429# Note: The handling of message vs no-message is messed up, but it's based
430# on historical usage. By default this function does not print passes,
431# only fails.
432# no-message: turns off printing of fails (and passes, but they're already off)
433# message: turns on printing of passes (and fails, but they're already on)
78a1a894
DJ
434
435proc gdb_breakpoint { function args } {
c906108c
SS
436 global gdb_prompt
437 global decimal
438
78a1a894 439 set pending_response n
5b7d0050 440 if {[lsearch -exact $args allow-pending] != -1} {
78a1a894
DJ
441 set pending_response y
442 }
443
e48883f7 444 set break_command "break"
18ac113b 445 set break_message "Breakpoint"
5b7d0050 446 if {[lsearch -exact $args temporary] != -1} {
e48883f7 447 set break_command "tbreak"
18ac113b 448 set break_message "Temporary breakpoint"
e48883f7
DJ
449 }
450
a20714ff
PA
451 if {[lsearch -exact $args qualified] != -1} {
452 append break_command " -qualified"
453 }
454
5b7d0050
DE
455 set print_pass 0
456 set print_fail 1
457 set no_message_loc [lsearch -exact $args no-message]
458 set message_loc [lsearch -exact $args message]
459 # The last one to appear in args wins.
460 if { $no_message_loc > $message_loc } {
461 set print_fail 0
462 } elseif { $message_loc > $no_message_loc } {
463 set print_pass 1
55cd6f92
DJ
464 }
465
5b7d0050
DE
466 set test_name "setting breakpoint at $function"
467
e48883f7 468 send_gdb "$break_command $function\n"
c906108c
SS
469 # The first two regexps are what we get with -g, the third is without -g.
470 gdb_expect 30 {
18ac113b
AR
471 -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
472 -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
473 -re "$break_message \[0-9\]* at .*$gdb_prompt $" {}
474 -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
78a1a894 475 if {$pending_response == "n"} {
5b7d0050
DE
476 if { $print_fail } {
477 fail $test_name
55cd6f92 478 }
78a1a894
DJ
479 return 0
480 }
481 }
9f27c604 482 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
78a1a894 483 send_gdb "$pending_response\n"
14b1a056 484 exp_continue
18fe2033 485 }
28781456 486 -re "A problem internal to GDB has been detected" {
5b7d0050
DE
487 if { $print_fail } {
488 fail "$test_name (GDB internal error)"
489 }
28781456
JK
490 gdb_internal_error_resync
491 return 0
492 }
55cd6f92 493 -re "$gdb_prompt $" {
5b7d0050
DE
494 if { $print_fail } {
495 fail $test_name
496 }
497 return 0
498 }
499 eof {
500 if { $print_fail } {
501 fail "$test_name (eof)"
55cd6f92
DJ
502 }
503 return 0
504 }
505 timeout {
5b7d0050
DE
506 if { $print_fail } {
507 fail "$test_name (timeout)"
55cd6f92
DJ
508 }
509 return 0
510 }
c906108c 511 }
5b7d0050
DE
512 if { $print_pass } {
513 pass $test_name
514 }
ae59b1da 515 return 1
c906108c
SS
516}
517
518# Set breakpoint at function and run gdb until it breaks there.
519# Since this is the only breakpoint that will be set, if it stops
520# at a breakpoint, we will assume it is the one we want. We can't
521# just compare to "function" because it might be a fully qualified,
5b7d0050
DE
522# single quoted C++ function specifier.
523#
524# If there are additional arguments, pass them to gdb_breakpoint.
525# We recognize no-message/message ourselves.
526# The default is no-message.
527# no-message is messed up here, like gdb_breakpoint: to preserve
528# historical usage fails are always printed by default.
529# no-message: turns off printing of fails (and passes, but they're already off)
530# message: turns on printing of passes (and fails, but they're already on)
c906108c 531
78a1a894 532proc runto { function args } {
c906108c
SS
533 global gdb_prompt
534 global decimal
535
536 delete_breakpoints
537
5b7d0050
DE
538 # Default to "no-message".
539 set args "no-message $args"
540
541 set print_pass 0
542 set print_fail 1
543 set no_message_loc [lsearch -exact $args no-message]
544 set message_loc [lsearch -exact $args message]
545 # The last one to appear in args wins.
546 if { $no_message_loc > $message_loc } {
547 set print_fail 0
548 } elseif { $message_loc > $no_message_loc } {
549 set print_pass 1
550 }
551
552 set test_name "running to $function in runto"
553
554 # We need to use eval here to pass our varargs args to gdb_breakpoint
555 # which is also a varargs function.
2c47921e
DE
556 # But we also have to be careful because $function may have multiple
557 # elements, and we don't want Tcl to move the remaining elements after
558 # the first to $args. That is why $function is wrapped in {}.
559 if ![eval gdb_breakpoint {$function} $args] {
ae59b1da 560 return 0
c906108c
SS
561 }
562
563 gdb_run_cmd
564
565 # the "at foo.c:36" output we get with -g.
566 # the "in func" output we get without -g.
567 gdb_expect 30 {
568 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
5b7d0050
DE
569 if { $print_pass } {
570 pass $test_name
571 }
c906108c
SS
572 return 1
573 }
574 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
5b7d0050
DE
575 if { $print_pass } {
576 pass $test_name
577 }
c906108c
SS
578 return 1
579 }
8e46892c 580 -re "The target does not support running in non-stop mode.\r\n$gdb_prompt $" {
5b7d0050 581 if { $print_fail } {
bc6c7af4 582 unsupported "non-stop mode not supported"
5b7d0050 583 }
8e46892c
JK
584 return 0
585 }
569b05a5 586 -re ".*A problem internal to GDB has been detected" {
5b7d0050
DE
587 if { $print_fail } {
588 fail "$test_name (GDB internal error)"
589 }
569b05a5
JK
590 gdb_internal_error_resync
591 return 0
592 }
c906108c 593 -re "$gdb_prompt $" {
5b7d0050
DE
594 if { $print_fail } {
595 fail $test_name
596 }
c906108c
SS
597 return 0
598 }
72c63395 599 eof {
5b7d0050
DE
600 if { $print_fail } {
601 fail "$test_name (eof)"
602 }
72c63395
JK
603 return 0
604 }
c906108c 605 timeout {
5b7d0050
DE
606 if { $print_fail } {
607 fail "$test_name (timeout)"
608 }
c906108c
SS
609 return 0
610 }
611 }
5b7d0050
DE
612 if { $print_pass } {
613 pass $test_name
614 }
c906108c
SS
615 return 1
616}
617
1d41d75c 618# Ask gdb to run until we hit a breakpoint at main.
c906108c 619#
1d41d75c
DE
620# N.B. This function deletes all existing breakpoints.
621# If you don't want that, use gdb_start_cmd.
622
c906108c 623proc runto_main { } {
5b7d0050 624 return [runto main no-message]
c906108c
SS
625}
626
4ce44c66
JM
627### Continue, and expect to hit a breakpoint.
628### Report a pass or fail, depending on whether it seems to have
629### worked. Use NAME as part of the test name; each call to
630### continue_to_breakpoint should use a NAME which is unique within
631### that test file.
74960c60 632proc gdb_continue_to_breakpoint {name {location_pattern .*}} {
4ce44c66
JM
633 global gdb_prompt
634 set full_name "continue to breakpoint: $name"
635
06d97543 636 gdb_test_multiple "continue" $full_name {
a1624241 637 -re "(?:Breakpoint|Temporary breakpoint) .* (at|in) $location_pattern\r\n$gdb_prompt $" {
4ce44c66
JM
638 pass $full_name
639 }
4ce44c66
JM
640 }
641}
642
643
039cf96d
AC
644# gdb_internal_error_resync:
645#
646# Answer the questions GDB asks after it reports an internal error
647# until we get back to a GDB prompt. Decline to quit the debugging
648# session, and decline to create a core file. Return non-zero if the
649# resync succeeds.
650#
651# This procedure just answers whatever questions come up until it sees
652# a GDB prompt; it doesn't require you to have matched the input up to
653# any specific point. However, it only answers questions it sees in
654# the output itself, so if you've matched a question, you had better
655# answer it yourself before calling this.
656#
657# You can use this function thus:
658#
659# gdb_expect {
660# ...
661# -re ".*A problem internal to GDB has been detected" {
662# gdb_internal_error_resync
663# }
664# ...
665# }
666#
667proc gdb_internal_error_resync {} {
668 global gdb_prompt
669
5b7d0050
DE
670 verbose -log "Resyncing due to internal error."
671
039cf96d
AC
672 set count 0
673 while {$count < 10} {
674 gdb_expect {
675 -re "Quit this debugging session\\? \\(y or n\\) $" {
f9e2e39d 676 send_gdb "n\n" answer
039cf96d
AC
677 incr count
678 }
679 -re "Create a core file of GDB\\? \\(y or n\\) $" {
f9e2e39d 680 send_gdb "n\n" answer
039cf96d
AC
681 incr count
682 }
683 -re "$gdb_prompt $" {
684 # We're resynchronized.
685 return 1
686 }
687 timeout {
688 perror "Could not resync from internal error (timeout)"
689 return 0
690 }
691 }
692 }
2b211c59
AC
693 perror "Could not resync from internal error (resync count exceeded)"
694 return 0
039cf96d
AC
695}
696
4ce44c66 697
d17725d7 698# gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS PROMPT_REGEXP
8dbfb380 699# Send a command to gdb; test the result.
c906108c
SS
700#
701# COMMAND is the command to execute, send to GDB with send_gdb. If
702# this is the null string no command is sent.
2307bd6a
DJ
703# MESSAGE is a message to be printed with the built-in failure patterns
704# if one of them matches. If MESSAGE is empty COMMAND will be used.
705# EXPECT_ARGUMENTS will be fed to expect in addition to the standard
706# patterns. Pattern elements will be evaluated in the caller's
707# context; action elements will be executed in the caller's context.
708# Unlike patterns for gdb_test, these patterns should generally include
709# the final newline and prompt.
d17725d7
TV
710# PROMPT_REGEXP is a regexp matching the expected prompt after the command
711# output. If empty, defaults to "$gdb_prompt $"
c906108c
SS
712#
713# Returns:
2307bd6a
DJ
714# 1 if the test failed, according to a built-in failure pattern
715# 0 if only user-supplied patterns matched
c906108c
SS
716# -1 if there was an internal error.
717#
d422fe19
AC
718# You can use this function thus:
719#
720# gdb_test_multiple "print foo" "test foo" {
721# -re "expected output 1" {
722# pass "print foo"
723# }
724# -re "expected output 2" {
725# fail "print foo"
726# }
727# }
728#
f71c18e7
PA
729# Like with "expect", you can also specify the spawn id to match with
730# -i "$id". Interesting spawn ids are $inferior_spawn_id and
731# $gdb_spawn_id. The former matches inferior I/O, while the latter
732# matches GDB I/O. E.g.:
733#
734# send_inferior "hello\n"
735# gdb_test_multiple "continue" "test echo" {
736# -i "$inferior_spawn_id" -re "^hello\r\nhello\r\n$" {
737# pass "got echo"
738# }
739# -i "$gdb_spawn_id" -re "Breakpoint.*$gdb_prompt $" {
740# fail "hit breakpoint"
741# }
742# }
743#
fda326dd 744# The standard patterns, such as "Inferior exited..." and "A problem
f71c18e7
PA
745# ...", all being implicitly appended to that list. These are always
746# expected from $gdb_spawn_id. IOW, callers do not need to worry
747# about resetting "-i" back to $gdb_spawn_id explicitly.
d422fe19 748#
d17725d7 749proc gdb_test_multiple { command message user_code { prompt_regexp "" } } {
e11ac3a3 750 global verbose use_gdb_stub
c3f814a1 751 global gdb_prompt pagination_prompt
c906108c 752 global GDB
f71c18e7 753 global gdb_spawn_id
fda326dd 754 global inferior_exited_re
c906108c 755 upvar timeout timeout
c47cebdb 756 upvar expect_out expect_out
749ef8f8 757 global any_spawn_id
c906108c 758
d17725d7
TV
759 if { "$prompt_regexp" == "" } {
760 set prompt_regexp "$gdb_prompt $"
761 }
762
2307bd6a
DJ
763 if { $message == "" } {
764 set message $command
c906108c 765 }
c906108c 766
824cc8dd
JK
767 if [string match "*\[\r\n\]" $command] {
768 error "Invalid trailing newline in \"$message\" test"
769 }
770
8344e389
JK
771 if [string match "*\[\r\n\]*" $message] {
772 error "Invalid newline in \"$message\" test"
773 }
774
e11ac3a3 775 if {$use_gdb_stub
9bfee719 776 && [regexp -nocase {^\s*(r|run|star|start|at|att|atta|attac|attach)\M} \
e11ac3a3
JK
777 $command]} {
778 error "gdbserver does not support $command without extended-remote"
779 }
780
2307bd6a
DJ
781 # TCL/EXPECT WART ALERT
782 # Expect does something very strange when it receives a single braced
783 # argument. It splits it along word separators and performs substitutions.
784 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
785 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
786 # double-quoted list item, "\[ab\]" is just a long way of representing
787 # "[ab]", because the backslashes will be removed by lindex.
788
789 # Unfortunately, there appears to be no easy way to duplicate the splitting
790 # that expect will do from within TCL. And many places make use of the
791 # "\[0-9\]" construct, so we need to support that; and some places make use
792 # of the "[func]" construct, so we need to support that too. In order to
793 # get this right we have to substitute quoted list elements differently
794 # from braced list elements.
795
796 # We do this roughly the same way that Expect does it. We have to use two
797 # lists, because if we leave unquoted newlines in the argument to uplevel
798 # they'll be treated as command separators, and if we escape newlines
799 # we mangle newlines inside of command blocks. This assumes that the
800 # input doesn't contain a pattern which contains actual embedded newlines
801 # at this point!
802
803 regsub -all {\n} ${user_code} { } subst_code
804 set subst_code [uplevel list $subst_code]
805
806 set processed_code ""
807 set patterns ""
808 set expecting_action 0
21e24d21 809 set expecting_arg 0
2307bd6a
DJ
810 foreach item $user_code subst_item $subst_code {
811 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
812 lappend processed_code $item
813 continue
814 }
21e24d21
PA
815 if { $item == "-indices" || $item == "-re" || $item == "-ex" } {
816 lappend processed_code $item
817 continue
818 }
f71c18e7 819 if { $item == "-timeout" || $item == "-i" } {
21e24d21
PA
820 set expecting_arg 1
821 lappend processed_code $item
822 continue
823 }
824 if { $expecting_arg } {
825 set expecting_arg 0
71c0ee8c 826 lappend processed_code $subst_item
2307bd6a
DJ
827 continue
828 }
829 if { $expecting_action } {
830 lappend processed_code "uplevel [list $item]"
831 set expecting_action 0
832 # Cosmetic, no effect on the list.
833 append processed_code "\n"
834 continue
835 }
836 set expecting_action 1
837 lappend processed_code $subst_item
838 if {$patterns != ""} {
839 append patterns "; "
840 }
841 append patterns "\"$subst_item\""
c906108c
SS
842 }
843
2307bd6a
DJ
844 # Also purely cosmetic.
845 regsub -all {\r} $patterns {\\r} patterns
846 regsub -all {\n} $patterns {\\n} patterns
847
c906108c
SS
848 if $verbose>2 then {
849 send_user "Sending \"$command\" to gdb\n"
2307bd6a 850 send_user "Looking to match \"$patterns\"\n"
c906108c
SS
851 send_user "Message is \"$message\"\n"
852 }
853
854 set result -1
4ec70201 855 set string "${command}\n"
c906108c 856 if { $command != "" } {
543a9323 857 set multi_line_re "\[\r\n\] *>"
c906108c 858 while { "$string" != "" } {
4ec70201
PA
859 set foo [string first "\n" "$string"]
860 set len [string length "$string"]
c906108c 861 if { $foo < [expr $len - 1] } {
4ec70201 862 set str [string range "$string" 0 $foo]
c906108c 863 if { [send_gdb "$str"] != "" } {
4ec70201 864 global suppress_flag
c906108c
SS
865
866 if { ! $suppress_flag } {
4ec70201 867 perror "Couldn't send $command to GDB."
c906108c 868 }
4ec70201 869 fail "$message"
ae59b1da 870 return $result
c906108c 871 }
a0b3c4fd
JM
872 # since we're checking if each line of the multi-line
873 # command are 'accepted' by GDB here,
874 # we need to set -notransfer expect option so that
875 # command output is not lost for pattern matching
876 # - guo
5f279fa6 877 gdb_expect 2 {
543a9323 878 -notransfer -re "$multi_line_re$" { verbose "partial: match" 3 }
5f279fa6 879 timeout { verbose "partial: timeout" 3 }
c906108c 880 }
4ec70201 881 set string [string range "$string" [expr $foo + 1] end]
543a9323 882 set multi_line_re "$multi_line_re.*\[\r\n\] *>"
c906108c 883 } else {
4ec70201 884 break
c906108c
SS
885 }
886 }
887 if { "$string" != "" } {
888 if { [send_gdb "$string"] != "" } {
4ec70201 889 global suppress_flag
c906108c
SS
890
891 if { ! $suppress_flag } {
4ec70201 892 perror "Couldn't send $command to GDB."
c906108c 893 }
4ec70201 894 fail "$message"
ae59b1da 895 return $result
c906108c
SS
896 }
897 }
898 }
899
2307bd6a 900 set code {
9bfee719
MR
901 -re ".*A problem internal to GDB has been detected" {
902 fail "$message (GDB internal error)"
903 gdb_internal_error_resync
28054d69 904 set result -1
9bfee719
MR
905 }
906 -re "\\*\\*\\* DOSEXIT code.*" {
907 if { $message != "" } {
4ec70201 908 fail "$message"
9bfee719 909 }
4ec70201
PA
910 gdb_suppress_entire_file "GDB died"
911 set result -1
9bfee719 912 }
b0f4b84b
DJ
913 }
914 append code $processed_code
9a93502f
PA
915
916 # Reset the spawn id, in case the processed code used -i.
b0f4b84b 917 append code {
f71c18e7 918 -i "$gdb_spawn_id"
9a93502f 919 }
f71c18e7 920
9a93502f 921 append code {
d17725d7 922 -re "Ending remote debugging.*$prompt_regexp" {
c906108c
SS
923 if ![isnative] then {
924 warning "Can`t communicate to remote target."
925 }
926 gdb_exit
927 gdb_start
928 set result -1
929 }
d17725d7 930 -re "Undefined\[a-z\]* command:.*$prompt_regexp" {
c906108c 931 perror "Undefined command \"$command\"."
9bfee719 932 fail "$message"
c906108c
SS
933 set result 1
934 }
d17725d7 935 -re "Ambiguous command.*$prompt_regexp" {
c906108c 936 perror "\"$command\" is not a unique command name."
9bfee719 937 fail "$message"
c906108c
SS
938 set result 1
939 }
d17725d7 940 -re "$inferior_exited_re with code \[0-9\]+.*$prompt_regexp" {
c906108c 941 if ![string match "" $message] then {
ed4c619a 942 set errmsg "$message (the program exited)"
c906108c 943 } else {
ed4c619a 944 set errmsg "$command (the program exited)"
c906108c
SS
945 }
946 fail "$errmsg"
2307bd6a 947 set result -1
cb9a9d3e 948 }
d17725d7 949 -re "$inferior_exited_re normally.*$prompt_regexp" {
cb9a9d3e 950 if ![string match "" $message] then {
ed4c619a 951 set errmsg "$message (the program exited)"
cb9a9d3e 952 } else {
ed4c619a 953 set errmsg "$command (the program exited)"
cb9a9d3e
MS
954 }
955 fail "$errmsg"
2307bd6a 956 set result -1
c906108c 957 }
d17725d7 958 -re "The program is not being run.*$prompt_regexp" {
c906108c 959 if ![string match "" $message] then {
ed4c619a 960 set errmsg "$message (the program is no longer running)"
c906108c 961 } else {
ed4c619a 962 set errmsg "$command (the program is no longer running)"
c906108c
SS
963 }
964 fail "$errmsg"
2307bd6a 965 set result -1
c906108c 966 }
d17725d7 967 -re "\r\n$prompt_regexp" {
c906108c
SS
968 if ![string match "" $message] then {
969 fail "$message"
970 }
971 set result 1
972 }
c3f814a1 973 -re "$pagination_prompt" {
c906108c
SS
974 send_gdb "\n"
975 perror "Window too small."
9bfee719 976 fail "$message"
2307bd6a 977 set result -1
c906108c 978 }
b598bfda 979 -re "\\((y or n|y or \\\[n\\\]|\\\[y\\\] or n)\\) " {
f9e2e39d 980 send_gdb "n\n" answer
d17725d7 981 gdb_expect -re "$prompt_regexp"
b598bfda
DJ
982 fail "$message (got interactive prompt)"
983 set result -1
984 }
985 -re "\\\[0\\\] cancel\r\n\\\[1\\\] all.*\r\n> $" {
986 send_gdb "0\n"
d17725d7 987 gdb_expect -re "$prompt_regexp"
b598bfda 988 fail "$message (got breakpoint menu)"
2307bd6a 989 set result -1
c906108c 990 }
749ef8f8 991
fe1a5cad
TV
992 -i $gdb_spawn_id
993 eof {
994 perror "GDB process no longer exists"
995 set wait_status [wait -i $gdb_spawn_id]
996 verbose -log "GDB process exited with wait status $wait_status"
997 if { $message != "" } {
998 fail "$message"
999 }
1000 return -1
1001 }
9a93502f 1002 }
fe1a5cad 1003
9a93502f
PA
1004 # Now patterns that apply to any spawn id specified.
1005 append code {
749ef8f8 1006 -i $any_spawn_id
9bfee719
MR
1007 eof {
1008 perror "Process no longer exists"
1009 if { $message != "" } {
1010 fail "$message"
1011 }
1012 return -1
c906108c 1013 }
9bfee719 1014 full_buffer {
c906108c 1015 perror "internal buffer is full."
9bfee719 1016 fail "$message"
2307bd6a 1017 set result -1
c906108c
SS
1018 }
1019 timeout {
1020 if ![string match "" $message] then {
1021 fail "$message (timeout)"
1022 }
1023 set result 1
1024 }
1025 }
2307bd6a 1026
9a93502f
PA
1027 # remote_expect calls the eof section if there is an error on the
1028 # expect call. We already have eof sections above, and we don't
1029 # want them to get called in that situation. Since the last eof
1030 # section becomes the error section, here we define another eof
1031 # section, but with an empty spawn_id list, so that it won't ever
1032 # match.
1033 append code {
1034 -i "" eof {
1035 # This comment is here because the eof section must not be
1036 # the empty string, otherwise remote_expect won't realize
1037 # it exists.
1038 }
1039 }
1040
2307bd6a 1041 set result 0
4a40f85a 1042 set code [catch {gdb_expect $code} string]
04f6ecf2 1043 if {$code == 1} {
4ec70201 1044 global errorInfo errorCode
04f6ecf2 1045 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
d6d7a51a 1046 } elseif {$code > 1} {
04f6ecf2
DJ
1047 return -code $code $string
1048 }
c906108c
SS
1049 return $result
1050}
2307bd6a
DJ
1051
1052# gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
1053# Send a command to gdb; test the result.
1054#
1055# COMMAND is the command to execute, send to GDB with send_gdb. If
1056# this is the null string no command is sent.
1057# PATTERN is the pattern to match for a PASS, and must NOT include
79fad5b8
SL
1058# the \r\n sequence immediately before the gdb prompt. This argument
1059# may be omitted to just match the prompt, ignoring whatever output
1060# precedes it.
2307bd6a
DJ
1061# MESSAGE is an optional message to be printed. If this is
1062# omitted, then the pass/fail messages use the command string as the
1063# message. (If this is the empty string, then sometimes we don't
1064# call pass or fail at all; I don't understand this at all.)
1065# QUESTION is a question GDB may ask in response to COMMAND, like
1066# "are you sure?"
1067# RESPONSE is the response to send if QUESTION appears.
1068#
1069# Returns:
1070# 1 if the test failed,
1071# 0 if the test passes,
1072# -1 if there was an internal error.
1073#
1074proc gdb_test { args } {
2307bd6a 1075 global gdb_prompt
2307bd6a
DJ
1076 upvar timeout timeout
1077
1078 if [llength $args]>2 then {
1079 set message [lindex $args 2]
1080 } else {
1081 set message [lindex $args 0]
1082 }
1083 set command [lindex $args 0]
1084 set pattern [lindex $args 1]
1085
1086 if [llength $args]==5 {
4ec70201
PA
1087 set question_string [lindex $args 3]
1088 set response_string [lindex $args 4]
2307bd6a
DJ
1089 } else {
1090 set question_string "^FOOBAR$"
1091 }
1092
1093 return [gdb_test_multiple $command $message {
75312ae3 1094 -re "\[\r\n\]*(?:$pattern)\[\r\n\]+$gdb_prompt $" {
2307bd6a
DJ
1095 if ![string match "" $message] then {
1096 pass "$message"
1097 }
1098 }
1099 -re "(${question_string})$" {
4ec70201
PA
1100 send_gdb "$response_string\n"
1101 exp_continue
2307bd6a
DJ
1102 }
1103 }]
1104}
a7b75dfd 1105
a80cf5d8
TV
1106# Return 1 if version MAJOR.MINOR is at least AT_LEAST_MAJOR.AT_LEAST_MINOR.
1107proc version_at_least { major minor at_least_major at_least_minor} {
1108 if { $major > $at_least_major } {
2a3ad588 1109 return 1
a80cf5d8
TV
1110 } elseif { $major == $at_least_major \
1111 && $minor >= $at_least_minor } {
2a3ad588
TV
1112 return 1
1113 } else {
1114 return 0
1115 }
1116}
1117
a80cf5d8
TV
1118# Return 1 if tcl version used is at least MAJOR.MINOR
1119proc tcl_version_at_least { major minor } {
1120 global tcl_version
1121 regexp {^([0-9]+)\.([0-9]+)$} $tcl_version \
1122 dummy tcl_version_major tcl_version_minor
1123 return [version_at_least $tcl_version_major $tcl_version_minor \
1124 $major $minor]
1125}
1126
2a3ad588
TV
1127if { [tcl_version_at_least 8 5] == 0 } {
1128 # lrepeat was added in tcl 8.5. Only add if missing.
1129 proc lrepeat { n element } {
1130 if { [string is integer -strict $n] == 0 } {
1131 error "expected integer but got \"$n\""
1132 }
1133 if { $n < 0 } {
1134 error "bad count \"$n\": must be integer >= 0"
1135 }
1136 set res [list]
1137 for {set i 0} {$i < $n} {incr i} {
1138 lappend res $element
1139 }
1140 return $res
1141 }
1142}
1143
a7b75dfd
JB
1144# gdb_test_no_output COMMAND MESSAGE
1145# Send a command to GDB and verify that this command generated no output.
1146#
1147# See gdb_test_multiple for a description of the COMMAND and MESSAGE
1148# parameters. If MESSAGE is ommitted, then COMMAND will be used as
c22decce
JB
1149# the message. (If MESSAGE is the empty string, then sometimes we do not
1150# call pass or fail at all; I don't understand this at all.)
a7b75dfd
JB
1151
1152proc gdb_test_no_output { args } {
1153 global gdb_prompt
1154 set command [lindex $args 0]
1155 if [llength $args]>1 then {
1156 set message [lindex $args 1]
1157 } else {
1158 set message $command
1159 }
1160
1161 set command_regex [string_to_regexp $command]
1162 gdb_test_multiple $command $message {
1163 -re "^$command_regex\r\n$gdb_prompt $" {
c22decce
JB
1164 if ![string match "" $message] then {
1165 pass "$message"
1166 }
a7b75dfd
JB
1167 }
1168 }
1169}
1170
6b0ecdc2
DE
1171# Send a command and then wait for a sequence of outputs.
1172# This is useful when the sequence is long and contains ".*", a single
1173# regexp to match the entire output can get a timeout much easier.
1174#
968a13f8
PA
1175# COMMAND is the command to execute, send to GDB with send_gdb. If
1176# this is the null string no command is sent.
6b0ecdc2
DE
1177# TEST_NAME is passed to pass/fail. COMMAND is used if TEST_NAME is "".
1178# EXPECTED_OUTPUT_LIST is a list of regexps of expected output, which are
1179# processed in order, and all must be present in the output.
1180#
1181# It is unnecessary to specify ".*" at the beginning or end of any regexp,
1182# there is an implicit ".*" between each element of EXPECTED_OUTPUT_LIST.
1183# There is also an implicit ".*" between the last regexp and the gdb prompt.
1184#
1185# Like gdb_test and gdb_test_multiple, the output is expected to end with the
1186# gdb prompt, which must not be specified in EXPECTED_OUTPUT_LIST.
5fa290c1
DE
1187#
1188# Returns:
1189# 1 if the test failed,
1190# 0 if the test passes,
1191# -1 if there was an internal error.
6b0ecdc2
DE
1192
1193proc gdb_test_sequence { command test_name expected_output_list } {
1194 global gdb_prompt
1195 if { $test_name == "" } {
1196 set test_name $command
1197 }
1198 lappend expected_output_list ""; # implicit ".*" before gdb prompt
968a13f8
PA
1199 if { $command != "" } {
1200 send_gdb "$command\n"
1201 }
5fa290c1 1202 return [gdb_expect_list $test_name "$gdb_prompt $" $expected_output_list]
6b0ecdc2
DE
1203}
1204
c906108c
SS
1205\f
1206# Test that a command gives an error. For pass or fail, return
1207# a 1 to indicate that more tests can proceed. However a timeout
1208# is a serious error, generates a special fail message, and causes
1209# a 0 to be returned to indicate that more tests are likely to fail
1210# as well.
1211
1212proc test_print_reject { args } {
1213 global gdb_prompt
1214 global verbose
1215
1216 if [llength $args]==2 then {
1217 set expectthis [lindex $args 1]
1218 } else {
1219 set expectthis "should never match this bogus string"
1220 }
1221 set sendthis [lindex $args 0]
1222 if $verbose>2 then {
1223 send_user "Sending \"$sendthis\" to gdb\n"
1224 send_user "Looking to match \"$expectthis\"\n"
1225 }
1226 send_gdb "$sendthis\n"
1227 #FIXME: Should add timeout as parameter.
1228 gdb_expect {
1229 -re "A .* in expression.*\\.*$gdb_prompt $" {
1230 pass "reject $sendthis"
1231 return 1
1232 }
1233 -re "Invalid syntax in expression.*$gdb_prompt $" {
1234 pass "reject $sendthis"
1235 return 1
1236 }
1237 -re "Junk after end of expression.*$gdb_prompt $" {
1238 pass "reject $sendthis"
1239 return 1
1240 }
1241 -re "Invalid number.*$gdb_prompt $" {
1242 pass "reject $sendthis"
1243 return 1
1244 }
1245 -re "Invalid character constant.*$gdb_prompt $" {
1246 pass "reject $sendthis"
1247 return 1
1248 }
1249 -re "No symbol table is loaded.*$gdb_prompt $" {
1250 pass "reject $sendthis"
1251 return 1
1252 }
1253 -re "No symbol .* in current context.*$gdb_prompt $" {
1254 pass "reject $sendthis"
1255 return 1
1256 }
c4b7bc2b
JB
1257 -re "Unmatched single quote.*$gdb_prompt $" {
1258 pass "reject $sendthis"
1259 return 1
1260 }
1261 -re "A character constant must contain at least one character.*$gdb_prompt $" {
1262 pass "reject $sendthis"
1263 return 1
1264 }
c906108c
SS
1265 -re "$expectthis.*$gdb_prompt $" {
1266 pass "reject $sendthis"
1267 return 1
1268 }
1269 -re ".*$gdb_prompt $" {
1270 fail "reject $sendthis"
1271 return 1
1272 }
1273 default {
1274 fail "reject $sendthis (eof or timeout)"
1275 return 0
1276 }
1277 }
1278}
1279\f
c906108c
SS
1280
1281# Same as gdb_test, but the second parameter is not a regexp,
1282# but a string that must match exactly.
1283
1284proc gdb_test_exact { args } {
1285 upvar timeout timeout
1286
1287 set command [lindex $args 0]
1288
1289 # This applies a special meaning to a null string pattern. Without
1290 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
1291 # messages from commands that should have no output except a new
1292 # prompt. With this, only results of a null string will match a null
1293 # string pattern.
1294
1295 set pattern [lindex $args 1]
1296 if [string match $pattern ""] {
1297 set pattern [string_to_regexp [lindex $args 0]]
1298 } else {
1299 set pattern [string_to_regexp [lindex $args 1]]
1300 }
1301
1302 # It is most natural to write the pattern argument with only
1303 # embedded \n's, especially if you are trying to avoid Tcl quoting
1304 # problems. But gdb_expect really wants to see \r\n in patterns. So
1305 # transform the pattern here. First transform \r\n back to \n, in
1306 # case some users of gdb_test_exact already do the right thing.
1307 regsub -all "\r\n" $pattern "\n" pattern
1308 regsub -all "\n" $pattern "\r\n" pattern
1309 if [llength $args]==3 then {
1310 set message [lindex $args 2]
1311 } else {
1312 set message $command
1313 }
1314
1315 return [gdb_test $command $pattern $message]
1316}
2dfb8c17
DE
1317
1318# Wrapper around gdb_test_multiple that looks for a list of expected
1319# output elements, but which can appear in any order.
1320# CMD is the gdb command.
1321# NAME is the name of the test.
1322# ELM_FIND_REGEXP specifies how to partition the output into elements to
1323# compare.
1324# ELM_EXTRACT_REGEXP specifies the part of ELM_FIND_REGEXP to compare.
1325# RESULT_MATCH_LIST is a list of exact matches for each expected element.
1326# All elements of RESULT_MATCH_LIST must appear for the test to pass.
1327#
1328# A typical use of ELM_FIND_REGEXP/ELM_EXTRACT_REGEXP is to extract one line
1329# of text per element and then strip trailing \r\n's.
1330# Example:
1331# gdb_test_list_exact "foo" "bar" \
eec52c44
PM
1332# "\[^\r\n\]+\[\r\n\]+" \
1333# "\[^\r\n\]+" \
2dfb8c17
DE
1334# { \
1335# {expected result 1} \
1336# {expected result 2} \
1337# }
1338
1339proc gdb_test_list_exact { cmd name elm_find_regexp elm_extract_regexp result_match_list } {
1340 global gdb_prompt
1341
1342 set matches [lsort $result_match_list]
1343 set seen {}
1344 gdb_test_multiple $cmd $name {
1345 "$cmd\[\r\n\]" { exp_continue }
1346 -re $elm_find_regexp {
1347 set str $expect_out(0,string)
1348 verbose -log "seen: $str" 3
1349 regexp -- $elm_extract_regexp $str elm_seen
1350 verbose -log "extracted: $elm_seen" 3
1351 lappend seen $elm_seen
1352 exp_continue
1353 }
1354 -re "$gdb_prompt $" {
1355 set failed ""
1356 foreach got [lsort $seen] have $matches {
1357 if {![string equal $got $have]} {
1358 set failed $have
1359 break
1360 }
1361 }
1362 if {[string length $failed] != 0} {
1363 fail "$name ($failed not found)"
1364 } else {
1365 pass $name
1366 }
1367 }
1368 }
1369}
188a61b4
PA
1370
1371# gdb_test_stdio COMMAND INFERIOR_PATTERN GDB_PATTERN MESSAGE
1372# Send a command to gdb; expect inferior and gdb output.
1373#
1374# See gdb_test_multiple for a description of the COMMAND and MESSAGE
1375# parameters.
1376#
1377# INFERIOR_PATTERN is the pattern to match against inferior output.
1378#
1379# GDB_PATTERN is the pattern to match against gdb output, and must NOT
1380# include the \r\n sequence immediately before the gdb prompt, nor the
1381# prompt. The default is empty.
1382#
1383# Both inferior and gdb patterns must match for a PASS.
1384#
1385# If MESSAGE is ommitted, then COMMAND will be used as the message.
1386#
1387# Returns:
1388# 1 if the test failed,
1389# 0 if the test passes,
1390# -1 if there was an internal error.
1391#
1392
1393proc gdb_test_stdio {command inferior_pattern {gdb_pattern ""} {message ""}} {
1394 global inferior_spawn_id gdb_spawn_id
1395 global gdb_prompt
1396
1397 if {$message == ""} {
1398 set message $command
1399 }
1400
1401 set inferior_matched 0
1402 set gdb_matched 0
1403
1404 # Use an indirect spawn id list, and remove the inferior spawn id
1405 # from the expected output as soon as it matches, in case
1406 # $inferior_pattern happens to be a prefix of the resulting full
1407 # gdb pattern below (e.g., "\r\n").
1408 global gdb_test_stdio_spawn_id_list
1409 set gdb_test_stdio_spawn_id_list "$inferior_spawn_id"
1410
1411 # Note that if $inferior_spawn_id and $gdb_spawn_id are different,
1412 # then we may see gdb's output arriving before the inferior's
1413 # output.
1414 set res [gdb_test_multiple $command $message {
1415 -i gdb_test_stdio_spawn_id_list -re "$inferior_pattern" {
1416 set inferior_matched 1
1417 if {!$gdb_matched} {
1418 set gdb_test_stdio_spawn_id_list ""
1419 exp_continue
1420 }
1421 }
1422 -i $gdb_spawn_id -re "$gdb_pattern\r\n$gdb_prompt $" {
1423 set gdb_matched 1
1424 if {!$inferior_matched} {
1425 exp_continue
1426 }
1427 }
1428 }]
1429 if {$res == 0} {
1430 pass $message
1431 } else {
1432 verbose -log "inferior_matched=$inferior_matched, gdb_matched=$gdb_matched"
1433 }
1434 return $res
1435}
1436
2e62ab40
AB
1437# get_print_expr_at_depths EXP OUTPUTS
1438#
1439# Used for testing 'set print max-depth'. Prints the expression EXP
1440# with 'set print max-depth' set to various depths. OUTPUTS is a list
1441# of `n` different patterns to match at each of the depths from 0 to
1442# (`n` - 1).
1443#
1444# This proc does one final check with the max-depth set to 'unlimited'
1445# which is tested against the last pattern in the OUTPUTS list. The
1446# OUTPUTS list is therefore required to match every depth from 0 to a
1447# depth where the whole of EXP is printed with no ellipsis.
1448#
1449# This proc leaves the 'set print max-depth' set to 'unlimited'.
1450proc gdb_print_expr_at_depths {exp outputs} {
1451 for { set depth 0 } { $depth <= [llength $outputs] } { incr depth } {
1452 if { $depth == [llength $outputs] } {
1453 set expected_result [lindex $outputs [expr [llength $outputs] - 1]]
1454 set depth_string "unlimited"
1455 } else {
1456 set expected_result [lindex $outputs $depth]
1457 set depth_string $depth
1458 }
1459
1460 with_test_prefix "exp='$exp': depth=${depth_string}" {
1461 gdb_test_no_output "set print max-depth ${depth_string}"
1462 gdb_test "p $exp" "$expected_result"
1463 }
1464 }
1465}
1466
c906108c 1467\f
bd293940
PA
1468
1469# Issue a PASS and return true if evaluating CONDITION in the caller's
1470# frame returns true, and issue a FAIL and return false otherwise.
1471# MESSAGE is the pass/fail message to be printed. If MESSAGE is
1472# omitted or is empty, then the pass/fail messages use the condition
1473# string as the message.
1474
1475proc gdb_assert { condition {message ""} } {
1476 if { $message == ""} {
1477 set message $condition
1478 }
1479
1480 set res [uplevel 1 expr $condition]
1481 if {!$res} {
1482 fail $message
1483 } else {
1484 pass $message
1485 }
1486 return $res
1487}
1488
c906108c
SS
1489proc gdb_reinitialize_dir { subdir } {
1490 global gdb_prompt
1491
1492 if [is_remote host] {
ae59b1da 1493 return ""
c906108c
SS
1494 }
1495 send_gdb "dir\n"
1496 gdb_expect 60 {
1497 -re "Reinitialize source path to empty.*y or n. " {
f9e2e39d 1498 send_gdb "y\n" answer
c906108c
SS
1499 gdb_expect 60 {
1500 -re "Source directories searched.*$gdb_prompt $" {
1501 send_gdb "dir $subdir\n"
1502 gdb_expect 60 {
1503 -re "Source directories searched.*$gdb_prompt $" {
1504 verbose "Dir set to $subdir"
1505 }
1506 -re "$gdb_prompt $" {
1507 perror "Dir \"$subdir\" failed."
1508 }
1509 }
1510 }
1511 -re "$gdb_prompt $" {
1512 perror "Dir \"$subdir\" failed."
1513 }
1514 }
1515 }
1516 -re "$gdb_prompt $" {
1517 perror "Dir \"$subdir\" failed."
1518 }
1519 }
1520}
1521
1522#
1523# gdb_exit -- exit the GDB, killing the target program if necessary
1524#
1525proc default_gdb_exit {} {
1526 global GDB
6b8ce727 1527 global INTERNAL_GDBFLAGS GDBFLAGS
c906108c 1528 global verbose
51f77c37 1529 global gdb_spawn_id inferior_spawn_id
5e92f71a 1530 global inotify_log_file
c906108c 1531
4ec70201 1532 gdb_stop_suppressing_tests
c906108c
SS
1533
1534 if ![info exists gdb_spawn_id] {
4ec70201 1535 return
c906108c
SS
1536 }
1537
6b8ce727 1538 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
c906108c 1539
5e92f71a
TT
1540 if {[info exists inotify_log_file] && [file exists $inotify_log_file]} {
1541 set fd [open $inotify_log_file]
1542 set data [read -nonewline $fd]
1543 close $fd
1544
1545 if {[string compare $data ""] != 0} {
1546 warning "parallel-unsafe file creations noticed"
1547
1548 # Clear the log.
1549 set fd [open $inotify_log_file w]
1550 close $fd
1551 }
1552 }
1553
c906108c 1554 if { [is_remote host] && [board_info host exists fileid] } {
4ec70201 1555 send_gdb "quit\n"
c906108c
SS
1556 gdb_expect 10 {
1557 -re "y or n" {
f9e2e39d 1558 send_gdb "y\n" answer
4ec70201 1559 exp_continue
c906108c
SS
1560 }
1561 -re "DOSEXIT code" { }
1562 default { }
1563 }
1564 }
1565
1566 if ![is_remote host] {
4ec70201 1567 remote_close host
c906108c
SS
1568 }
1569 unset gdb_spawn_id
51f77c37 1570 unset inferior_spawn_id
c906108c
SS
1571}
1572
3e3ffd2b 1573# Load a file into the debugger.
2db8e78e 1574# The return value is 0 for success, -1 for failure.
c906108c 1575#
2db8e78e
MC
1576# This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1577# to one of these values:
3e3ffd2b 1578#
2db8e78e
MC
1579# debug file was loaded successfully and has debug information
1580# nodebug file was loaded successfully and has no debug information
608e2dbb
TT
1581# lzma file was loaded, .gnu_debugdata found, but no LZMA support
1582# compiled in
2db8e78e 1583# fail file was not loaded
c906108c 1584#
2db8e78e
MC
1585# I tried returning this information as part of the return value,
1586# but ran into a mess because of the many re-implementations of
1587# gdb_load in config/*.exp.
3e3ffd2b 1588#
2db8e78e
MC
1589# TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
1590# this if they can get more information set.
3e3ffd2b 1591
c906108c 1592proc gdb_file_cmd { arg } {
3e3ffd2b 1593 global gdb_prompt
c906108c 1594 global verbose
c906108c 1595 global GDB
b741e217
DJ
1596 global last_loaded_file
1597
975531db 1598 # Save this for the benefit of gdbserver-support.exp.
b741e217 1599 set last_loaded_file $arg
c906108c 1600
2db8e78e
MC
1601 # Set whether debug info was found.
1602 # Default to "fail".
1603 global gdb_file_cmd_debug_info
1604 set gdb_file_cmd_debug_info "fail"
1605
c906108c 1606 if [is_remote host] {
3e3ffd2b 1607 set arg [remote_download host $arg]
c906108c 1608 if { $arg == "" } {
2db8e78e
MC
1609 perror "download failed"
1610 return -1
c906108c
SS
1611 }
1612 }
1613
4c42eaff 1614 # The file command used to kill the remote target. For the benefit
f9e2e39d
AH
1615 # of the testsuite, preserve this behavior. Mark as optional so it doesn't
1616 # get written to the stdin log.
1617 send_gdb "kill\n" optional
4c42eaff
DJ
1618 gdb_expect 120 {
1619 -re "Kill the program being debugged. .y or n. $" {
f9e2e39d 1620 send_gdb "y\n" answer
4c42eaff
DJ
1621 verbose "\t\tKilling previous program being debugged"
1622 exp_continue
1623 }
1624 -re "$gdb_prompt $" {
1625 # OK.
1626 }
1627 }
1628
c906108c
SS
1629 send_gdb "file $arg\n"
1630 gdb_expect 120 {
3453e7e4 1631 -re "Reading symbols from.*LZMA support was disabled.*$gdb_prompt $" {
608e2dbb
TT
1632 verbose "\t\tLoaded $arg into $GDB; .gnu_debugdata found but no LZMA available"
1633 set gdb_file_cmd_debug_info "lzma"
1634 return 0
1635 }
3453e7e4 1636 -re "Reading symbols from.*no debugging symbols found.*$gdb_prompt $" {
975531db 1637 verbose "\t\tLoaded $arg into $GDB with no debugging symbols"
2db8e78e
MC
1638 set gdb_file_cmd_debug_info "nodebug"
1639 return 0
3e3ffd2b 1640 }
3453e7e4 1641 -re "Reading symbols from.*$gdb_prompt $" {
975531db 1642 verbose "\t\tLoaded $arg into $GDB"
2db8e78e
MC
1643 set gdb_file_cmd_debug_info "debug"
1644 return 0
c906108c 1645 }
c906108c 1646 -re "Load new symbol table from \".*\".*y or n. $" {
f9e2e39d 1647 send_gdb "y\n" answer
c906108c 1648 gdb_expect 120 {
3453e7e4 1649 -re "Reading symbols from.*$gdb_prompt $" {
c906108c 1650 verbose "\t\tLoaded $arg with new symbol table into $GDB"
2db8e78e
MC
1651 set gdb_file_cmd_debug_info "debug"
1652 return 0
c906108c
SS
1653 }
1654 timeout {
975531db 1655 perror "Couldn't load $arg, other program already loaded (timeout)."
2db8e78e 1656 return -1
c906108c 1657 }
975531db
DE
1658 eof {
1659 perror "Couldn't load $arg, other program already loaded (eof)."
1660 return -1
1661 }
c906108c
SS
1662 }
1663 }
1664 -re "No such file or directory.*$gdb_prompt $" {
2db8e78e
MC
1665 perror "($arg) No such file or directory"
1666 return -1
c906108c 1667 }
04e7407c 1668 -re "A problem internal to GDB has been detected" {
5b7d0050 1669 fail "($arg) (GDB internal error)"
04e7407c
JK
1670 gdb_internal_error_resync
1671 return -1
1672 }
c906108c 1673 -re "$gdb_prompt $" {
975531db 1674 perror "Couldn't load $arg into $GDB."
2db8e78e 1675 return -1
c906108c
SS
1676 }
1677 timeout {
975531db 1678 perror "Couldn't load $arg into $GDB (timeout)."
2db8e78e 1679 return -1
c906108c
SS
1680 }
1681 eof {
1682 # This is an attempt to detect a core dump, but seems not to
1683 # work. Perhaps we need to match .* followed by eof, in which
1684 # gdb_expect does not seem to have a way to do that.
975531db 1685 perror "Couldn't load $arg into $GDB (eof)."
2db8e78e 1686 return -1
c906108c
SS
1687 }
1688 }
1689}
1690
94696ad3
PA
1691# Default gdb_spawn procedure.
1692
1693proc default_gdb_spawn { } {
1694 global use_gdb_stub
c906108c 1695 global GDB
6b8ce727 1696 global INTERNAL_GDBFLAGS GDBFLAGS
4ec70201 1697 global gdb_spawn_id
c906108c 1698
4ec70201 1699 gdb_stop_suppressing_tests
c906108c 1700
e11ac3a3
JK
1701 # Set the default value, it may be overriden later by specific testfile.
1702 #
1703 # Use `set_board_info use_gdb_stub' for the board file to flag the inferior
1704 # is already started after connecting and run/attach are not supported.
1705 # This is used for the "remote" protocol. After GDB starts you should
1706 # check global $use_gdb_stub instead of the board as the testfile may force
1707 # a specific different target protocol itself.
1708 set use_gdb_stub [target_info exists use_gdb_stub]
1709
6b8ce727 1710 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
408e9b8b 1711 gdb_write_cmd_file "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
c906108c
SS
1712
1713 if [info exists gdb_spawn_id] {
ae59b1da 1714 return 0
c906108c
SS
1715 }
1716
1717 if ![is_remote host] {
1718 if { [which $GDB] == 0 } then {
1719 perror "$GDB does not exist."
1720 exit 1
1721 }
1722 }
4ec70201 1723 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]"]
c906108c
SS
1724 if { $res < 0 || $res == "" } {
1725 perror "Spawning $GDB failed."
ae59b1da 1726 return 1
c906108c 1727 }
717cf30c
AG
1728
1729 set gdb_spawn_id $res
94696ad3
PA
1730 return 0
1731}
1732
1733# Default gdb_start procedure.
1734
1735proc default_gdb_start { } {
bd447abb 1736 global gdb_prompt
94696ad3 1737 global gdb_spawn_id
f71c18e7 1738 global inferior_spawn_id
94696ad3
PA
1739
1740 if [info exists gdb_spawn_id] {
1741 return 0
1742 }
1743
f9e2e39d
AH
1744 # Keep track of the number of times GDB has been launched.
1745 global gdb_instances
1746 incr gdb_instances
1747
1748 gdb_stdin_log_init
1749
94696ad3
PA
1750 set res [gdb_spawn]
1751 if { $res != 0} {
1752 return $res
1753 }
1754
f71c18e7
PA
1755 # Default to assuming inferior I/O is done on GDB's terminal.
1756 if {![info exists inferior_spawn_id]} {
1757 set inferior_spawn_id $gdb_spawn_id
1758 }
1759
94696ad3
PA
1760 # When running over NFS, particularly if running many simultaneous
1761 # tests on different hosts all using the same server, things can
1762 # get really slow. Give gdb at least 3 minutes to start up.
bd447abb
SM
1763 gdb_expect 360 {
1764 -re "\[\r\n\]$gdb_prompt $" {
1765 verbose "GDB initialized."
1766 }
1767 -re "$gdb_prompt $" {
1768 perror "GDB never initialized."
1769 unset gdb_spawn_id
1770 return -1
1771 }
1772 timeout {
1773 perror "(timeout) GDB never initialized after 10 seconds."
1774 remote_close host
1775 unset gdb_spawn_id
1776 return -1
c906108c
SS
1777 }
1778 }
94696ad3 1779
c906108c
SS
1780 # force the height to "unlimited", so no pagers get used
1781
1782 send_gdb "set height 0\n"
1783 gdb_expect 10 {
1784 -re "$gdb_prompt $" {
1785 verbose "Setting height to 0." 2
1786 }
1787 timeout {
1788 warning "Couldn't set the height to 0"
1789 }
1790 }
1791 # force the width to "unlimited", so no wraparound occurs
1792 send_gdb "set width 0\n"
1793 gdb_expect 10 {
1794 -re "$gdb_prompt $" {
1795 verbose "Setting width to 0." 2
1796 }
1797 timeout {
1798 warning "Couldn't set the width to 0."
1799 }
1800 }
29b52314
AH
1801
1802 gdb_debug_init
ae59b1da 1803 return 0
c906108c
SS
1804}
1805
717cf30c
AG
1806# Utility procedure to give user control of the gdb prompt in a script. It is
1807# meant to be used for debugging test cases, and should not be left in the
1808# test cases code.
1809
1810proc gdb_interact { } {
1811 global gdb_spawn_id
1812 set spawn_id $gdb_spawn_id
1813
1814 send_user "+------------------------------------------+\n"
1815 send_user "| Script interrupted, you can now interact |\n"
1816 send_user "| with by gdb. Type >>> to continue. |\n"
1817 send_user "+------------------------------------------+\n"
1818
1819 interact {
1820 ">>>" return
1821 }
1822}
1823
ec3c07fc
NS
1824# Examine the output of compilation to determine whether compilation
1825# failed or not. If it failed determine whether it is due to missing
1826# compiler or due to compiler error. Report pass, fail or unsupported
1827# as appropriate
1828
1829proc gdb_compile_test {src output} {
1830 if { $output == "" } {
1831 pass "compilation [file tail $src]"
1832 } elseif { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output] } {
1833 unsupported "compilation [file tail $src]"
1834 } elseif { [regexp {.*: command not found[\r|\n]*$} $output] } {
1835 unsupported "compilation [file tail $src]"
6bb85cd1
DE
1836 } elseif { [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } {
1837 unsupported "compilation [file tail $src]"
ec3c07fc
NS
1838 } else {
1839 verbose -log "compilation failed: $output" 2
1840 fail "compilation [file tail $src]"
1841 }
1842}
1843
d4f3574e
SS
1844# Return a 1 for configurations for which we don't even want to try to
1845# test C++.
1846
1847proc skip_cplus_tests {} {
d4f3574e
SS
1848 if { [istarget "h8300-*-*"] } {
1849 return 1
1850 }
81d2cbae 1851
1146c7f1
SC
1852 # The C++ IO streams are too large for HC11/HC12 and are thus not
1853 # available. The gdb C++ tests use them and don't compile.
1854 if { [istarget "m6811-*-*"] } {
1855 return 1
1856 }
1857 if { [istarget "m6812-*-*"] } {
1858 return 1
1859 }
d4f3574e
SS
1860 return 0
1861}
1862
759f0f0b
PA
1863# Return a 1 for configurations for which don't have both C++ and the STL.
1864
1865proc skip_stl_tests {} {
1866 # Symbian supports the C++ language, but the STL is missing
1867 # (both headers and libraries).
1868 if { [istarget "arm*-*-symbianelf*"] } {
1869 return 1
1870 }
1871
1872 return [skip_cplus_tests]
1873}
1874
89a237cb
MC
1875# Return a 1 if I don't even want to try to test FORTRAN.
1876
1877proc skip_fortran_tests {} {
1878 return 0
1879}
1880
ec3c07fc
NS
1881# Return a 1 if I don't even want to try to test ada.
1882
1883proc skip_ada_tests {} {
1884 return 0
1885}
1886
a766d390
DE
1887# Return a 1 if I don't even want to try to test GO.
1888
1889proc skip_go_tests {} {
1890 return 0
1891}
1892
7f420862
IB
1893# Return a 1 if I don't even want to try to test D.
1894
1895proc skip_d_tests {} {
1896 return 0
1897}
1898
67218854
TT
1899# Return 1 to skip Rust tests, 0 to try them.
1900proc skip_rust_tests {} {
1901 return [expr {![isnative]}]
1902}
1903
f6bbabf0 1904# Return a 1 for configurations that do not support Python scripting.
4d6cceb4 1905# PROMPT_REGEXP is the expected prompt.
f6bbabf0 1906
4d6cceb4 1907proc skip_python_tests_prompt { prompt_regexp } {
9325cb04 1908 global gdb_py_is_py3k
9325cb04
PK
1909
1910 gdb_test_multiple "python print ('test')" "verify python support" {
4d6cceb4 1911 -re "not supported.*$prompt_regexp" {
f6bbabf0
PM
1912 unsupported "Python support is disabled."
1913 return 1
1914 }
4d6cceb4 1915 -re "$prompt_regexp" {}
d17725d7 1916 } "$prompt_regexp"
f6bbabf0 1917
9325cb04 1918 gdb_test_multiple "python print (sys.version_info\[0\])" "check if python 3" {
4d6cceb4 1919 -re "3.*$prompt_regexp" {
9325cb04
PK
1920 set gdb_py_is_py3k 1
1921 }
4d6cceb4 1922 -re ".*$prompt_regexp" {
9325cb04
PK
1923 set gdb_py_is_py3k 0
1924 }
d17725d7 1925 } "$prompt_regexp"
9325cb04 1926
f6bbabf0
PM
1927 return 0
1928}
1929
4d6cceb4
DE
1930# Return a 1 for configurations that do not support Python scripting.
1931# Note: This also sets various globals that specify which version of Python
1932# is in use. See skip_python_tests_prompt.
1933
1934proc skip_python_tests {} {
1935 global gdb_prompt
1936 return [skip_python_tests_prompt "$gdb_prompt $"]
1937}
1938
93f02886
DJ
1939# Return a 1 if we should skip shared library tests.
1940
1941proc skip_shlib_tests {} {
1942 # Run the shared library tests on native systems.
1943 if {[isnative]} {
1944 return 0
1945 }
1946
1947 # An abbreviated list of remote targets where we should be able to
1948 # run shared library tests.
1949 if {([istarget *-*-linux*]
1950 || [istarget *-*-*bsd*]
1951 || [istarget *-*-solaris2*]
1952 || [istarget arm*-*-symbianelf*]
1953 || [istarget *-*-mingw*]
1954 || [istarget *-*-cygwin*]
1955 || [istarget *-*-pe*])} {
1956 return 0
1957 }
1958
1959 return 1
1960}
1961
ebe3b578
AB
1962# Return 1 if we should skip tui related tests.
1963
1964proc skip_tui_tests {} {
1965 global gdb_prompt
1966
1967 gdb_test_multiple "help layout" "verify tui support" {
1968 -re "Undefined command: \"layout\".*$gdb_prompt $" {
1969 return 1
1970 }
1971 -re "$gdb_prompt $" {
1972 }
1973 }
1974
1975 return 0
1976}
1977
6a5870ce
PA
1978# Test files shall make sure all the test result lines in gdb.sum are
1979# unique in a test run, so that comparing the gdb.sum files of two
1980# test runs gives correct results. Test files that exercise
1981# variations of the same tests more than once, shall prefix the
1982# different test invocations with different identifying strings in
1983# order to make them unique.
1984#
1985# About test prefixes:
1986#
1987# $pf_prefix is the string that dejagnu prints after the result (FAIL,
1988# PASS, etc.), and before the test message/name in gdb.sum. E.g., the
1989# underlined substring in
1990#
1991# PASS: gdb.base/mytest.exp: some test
1992# ^^^^^^^^^^^^^^^^^^^^
1993#
1994# is $pf_prefix.
1995#
1996# The easiest way to adjust the test prefix is to append a test
1997# variation prefix to the $pf_prefix, using the with_test_prefix
1998# procedure. E.g.,
1999#
2000# proc do_tests {} {
2001# gdb_test ... ... "test foo"
2002# gdb_test ... ... "test bar"
2003#
0f4d39d5 2004# with_test_prefix "subvariation a" {
6a5870ce
PA
2005# gdb_test ... ... "test x"
2006# }
2007#
0f4d39d5 2008# with_test_prefix "subvariation b" {
6a5870ce
PA
2009# gdb_test ... ... "test x"
2010# }
2011# }
2012#
0f4d39d5 2013# with_test_prefix "variation1" {
6a5870ce
PA
2014# ...do setup for variation 1...
2015# do_tests
2016# }
2017#
0f4d39d5 2018# with_test_prefix "variation2" {
6a5870ce
PA
2019# ...do setup for variation 2...
2020# do_tests
2021# }
2022#
2023# Results in:
2024#
2025# PASS: gdb.base/mytest.exp: variation1: test foo
2026# PASS: gdb.base/mytest.exp: variation1: test bar
2027# PASS: gdb.base/mytest.exp: variation1: subvariation a: test x
2028# PASS: gdb.base/mytest.exp: variation1: subvariation b: test x
2029# PASS: gdb.base/mytest.exp: variation2: test foo
2030# PASS: gdb.base/mytest.exp: variation2: test bar
2031# PASS: gdb.base/mytest.exp: variation2: subvariation a: test x
2032# PASS: gdb.base/mytest.exp: variation2: subvariation b: test x
2033#
2034# If for some reason more flexibility is necessary, one can also
2035# manipulate the pf_prefix global directly, treating it as a string.
2036# E.g.,
2037#
2038# global pf_prefix
2039# set saved_pf_prefix
0f4d39d5 2040# append pf_prefix "${foo}: bar"
6a5870ce
PA
2041# ... actual tests ...
2042# set pf_prefix $saved_pf_prefix
2043#
2044
2045# Run BODY in the context of the caller, with the current test prefix
0f4d39d5
PA
2046# (pf_prefix) appended with one space, then PREFIX, and then a colon.
2047# Returns the result of BODY.
6a5870ce
PA
2048#
2049proc with_test_prefix { prefix body } {
2050 global pf_prefix
2051
2052 set saved $pf_prefix
0f4d39d5 2053 append pf_prefix " " $prefix ":"
6a5870ce
PA
2054 set code [catch {uplevel 1 $body} result]
2055 set pf_prefix $saved
2056
2057 if {$code == 1} {
2058 global errorInfo errorCode
2059 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2060 } else {
2061 return -code $code $result
2062 }
2063}
2064
f1da4b11
PA
2065# Wrapper for foreach that calls with_test_prefix on each iteration,
2066# including the iterator's name and current value in the prefix.
2067
2068proc foreach_with_prefix {var list body} {
2069 upvar 1 $var myvar
2070 foreach myvar $list {
2071 with_test_prefix "$var=$myvar" {
a26c8de0
PA
2072 set code [catch {uplevel 1 $body} result]
2073 }
2074
2075 if {$code == 1} {
2076 global errorInfo errorCode
2077 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
213fd9fa
PA
2078 } elseif {$code == 3} {
2079 break
2080 } elseif {$code == 2} {
a26c8de0 2081 return -code $code $result
f1da4b11
PA
2082 }
2083 }
2084}
2085
64f367a2
PA
2086# Like TCL's native proc, but defines a procedure that wraps its body
2087# within 'with_test_prefix "$proc_name" { ... }'.
2088proc proc_with_prefix {name arguments body} {
2089 # Define the advertised proc.
2090 proc $name $arguments [list with_test_prefix $name $body]
2091}
2092
2093
abe8e607
PP
2094# Run BODY in the context of the caller. After BODY is run, the variables
2095# listed in VARS will be reset to the values they had before BODY was run.
2096#
2097# This is useful for providing a scope in which it is safe to temporarily
2098# modify global variables, e.g.
2099#
2100# global INTERNAL_GDBFLAGS
2101# global env
2102#
2103# set foo GDBHISTSIZE
2104#
2105# save_vars { INTERNAL_GDBFLAGS env($foo) env(HOME) } {
2106# append INTERNAL_GDBFLAGS " -nx"
2107# unset -nocomplain env(GDBHISTSIZE)
2108# gdb_start
2109# gdb_test ...
2110# }
2111#
2112# Here, although INTERNAL_GDBFLAGS, env(GDBHISTSIZE) and env(HOME) may be
2113# modified inside BODY, this proc guarantees that the modifications will be
2114# undone after BODY finishes executing.
2115
2116proc save_vars { vars body } {
2117 array set saved_scalars { }
2118 array set saved_arrays { }
2119 set unset_vars { }
2120
2121 foreach var $vars {
2122 # First evaluate VAR in the context of the caller in case the variable
2123 # name may be a not-yet-interpolated string like env($foo)
2124 set var [uplevel 1 list $var]
2125
2126 if [uplevel 1 [list info exists $var]] {
2127 if [uplevel 1 [list array exists $var]] {
2128 set saved_arrays($var) [uplevel 1 [list array get $var]]
2129 } else {
2130 set saved_scalars($var) [uplevel 1 [list set $var]]
2131 }
2132 } else {
2133 lappend unset_vars $var
2134 }
2135 }
2136
2137 set code [catch {uplevel 1 $body} result]
2138
2139 foreach {var value} [array get saved_scalars] {
2140 uplevel 1 [list set $var $value]
2141 }
2142
2143 foreach {var value} [array get saved_arrays] {
2144 uplevel 1 [list unset $var]
2145 uplevel 1 [list array set $var $value]
2146 }
2147
2148 foreach var $unset_vars {
2149 uplevel 1 [list unset -nocomplain $var]
2150 }
2151
2152 if {$code == 1} {
2153 global errorInfo errorCode
2154 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2155 } else {
2156 return -code $code $result
2157 }
2158}
2159
25e3c82c
SDJ
2160# Run tests in BODY with the current working directory (CWD) set to
2161# DIR. When BODY is finished, restore the original CWD. Return the
2162# result of BODY.
2163#
2164# This procedure doesn't check if DIR is a valid directory, so you
2165# have to make sure of that.
2166
2167proc with_cwd { dir body } {
2168 set saved_dir [pwd]
2169 verbose -log "Switching to directory $dir (saved CWD: $saved_dir)."
2170 cd $dir
2171
2172 set code [catch {uplevel 1 $body} result]
2173
2174 verbose -log "Switching back to $saved_dir."
2175 cd $saved_dir
2176
2177 if {$code == 1} {
2178 global errorInfo errorCode
2179 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2180 } else {
2181 return -code $code $result
2182 }
2183}
abe8e607 2184
8b5e6dc2
YQ
2185# Run tests in BODY with GDB prompt and variable $gdb_prompt set to
2186# PROMPT. When BODY is finished, restore GDB prompt and variable
2187# $gdb_prompt.
2188# Returns the result of BODY.
3714cea7
DE
2189#
2190# Notes:
2191#
2192# 1) If you want to use, for example, "(foo)" as the prompt you must pass it
2193# as "(foo)", and not the regexp form "\(foo\)" (expressed as "\\(foo\\)" in
2194# TCL). PROMPT is internally converted to a suitable regexp for matching.
2195# We do the conversion from "(foo)" to "\(foo\)" here for a few reasons:
2196# a) It's more intuitive for callers to pass the plain text form.
2197# b) We need two forms of the prompt:
2198# - a regexp to use in output matching,
2199# - a value to pass to the "set prompt" command.
2200# c) It's easier to convert the plain text form to its regexp form.
2201#
2202# 2) Don't add a trailing space, we do that here.
8b5e6dc2
YQ
2203
2204proc with_gdb_prompt { prompt body } {
2205 global gdb_prompt
2206
3714cea7
DE
2207 # Convert "(foo)" to "\(foo\)".
2208 # We don't use string_to_regexp because while it works today it's not
2209 # clear it will work tomorrow: the value we need must work as both a
2210 # regexp *and* as the argument to the "set prompt" command, at least until
2211 # we start recording both forms separately instead of just $gdb_prompt.
2212 # The testsuite is pretty-much hardwired to interpret $gdb_prompt as the
2213 # regexp form.
2214 regsub -all {[]*+.|()^$\[\\]} $prompt {\\&} prompt
2215
8b5e6dc2
YQ
2216 set saved $gdb_prompt
2217
3714cea7 2218 verbose -log "Setting gdb prompt to \"$prompt \"."
8b5e6dc2
YQ
2219 set gdb_prompt $prompt
2220 gdb_test_no_output "set prompt $prompt " ""
2221
2222 set code [catch {uplevel 1 $body} result]
2223
3714cea7 2224 verbose -log "Restoring gdb prompt to \"$saved \"."
8b5e6dc2
YQ
2225 set gdb_prompt $saved
2226 gdb_test_no_output "set prompt $saved " ""
2227
2228 if {$code == 1} {
2229 global errorInfo errorCode
2230 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2231 } else {
2232 return -code $code $result
2233 }
2234}
2235
389b98f7
YQ
2236# Run tests in BODY with target-charset setting to TARGET_CHARSET. When
2237# BODY is finished, restore target-charset.
2238
2239proc with_target_charset { target_charset body } {
2240 global gdb_prompt
2241
2242 set saved ""
2243 gdb_test_multiple "show target-charset" "" {
2244 -re "The target character set is \".*; currently (.*)\"\..*$gdb_prompt " {
2245 set saved $expect_out(1,string)
2246 }
2247 -re "The target character set is \"(.*)\".*$gdb_prompt " {
2248 set saved $expect_out(1,string)
2249 }
2250 -re ".*$gdb_prompt " {
2251 fail "get target-charset"
2252 }
2253 }
2254
2255 gdb_test_no_output "set target-charset $target_charset" ""
2256
2257 set code [catch {uplevel 1 $body} result]
2258
2259 gdb_test_no_output "set target-charset $saved" ""
2260
2261 if {$code == 1} {
2262 global errorInfo errorCode
2263 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2264 } else {
2265 return -code $code $result
2266 }
2267}
2268
ac69f786
PA
2269# Switch the default spawn id to SPAWN_ID, so that gdb_test,
2270# mi_gdb_test etc. default to using it.
2271
2272proc switch_gdb_spawn_id {spawn_id} {
2273 global gdb_spawn_id
2274 global board board_info
2275
2276 set gdb_spawn_id $spawn_id
2277 set board [host_info name]
2278 set board_info($board,fileid) $spawn_id
2279}
2280
4295e285
PA
2281# Clear the default spawn id.
2282
2283proc clear_gdb_spawn_id {} {
2284 global gdb_spawn_id
2285 global board board_info
2286
2287 unset -nocomplain gdb_spawn_id
2288 set board [host_info name]
2289 unset -nocomplain board_info($board,fileid)
2290}
2291
ac69f786
PA
2292# Run BODY with SPAWN_ID as current spawn id.
2293
2294proc with_spawn_id { spawn_id body } {
2295 global gdb_spawn_id
2296
4295e285
PA
2297 if [info exists gdb_spawn_id] {
2298 set saved_spawn_id $gdb_spawn_id
2299 }
2300
ac69f786
PA
2301 switch_gdb_spawn_id $spawn_id
2302
2303 set code [catch {uplevel 1 $body} result]
2304
4295e285
PA
2305 if [info exists saved_spawn_id] {
2306 switch_gdb_spawn_id $saved_spawn_id
2307 } else {
2308 clear_gdb_spawn_id
2309 }
ac69f786
PA
2310
2311 if {$code == 1} {
2312 global errorInfo errorCode
2313 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2314 } else {
2315 return -code $code $result
2316 }
2317}
2318
45fd756c
YQ
2319# Select the largest timeout from all the timeouts:
2320# - the local "timeout" variable of the scope two levels above,
2321# - the global "timeout" variable,
2322# - the board variable "gdb,timeout".
2323
2324proc get_largest_timeout {} {
2325 upvar #0 timeout gtimeout
2326 upvar 2 timeout timeout
2327
2328 set tmt 0
2329 if [info exists timeout] {
2330 set tmt $timeout
2331 }
2332 if { [info exists gtimeout] && $gtimeout > $tmt } {
2333 set tmt $gtimeout
2334 }
2335 if { [target_info exists gdb,timeout]
2336 && [target_info gdb,timeout] > $tmt } {
2337 set tmt [target_info gdb,timeout]
2338 }
2339 if { $tmt == 0 } {
2340 # Eeeeew.
2341 set tmt 60
2342 }
2343
2344 return $tmt
2345}
2346
2347# Run tests in BODY with timeout increased by factor of FACTOR. When
2348# BODY is finished, restore timeout.
2349
2350proc with_timeout_factor { factor body } {
2351 global timeout
2352
2353 set savedtimeout $timeout
2354
2355 set timeout [expr [get_largest_timeout] * $factor]
2356 set code [catch {uplevel 1 $body} result]
2357
2358 set timeout $savedtimeout
2359 if {$code == 1} {
2360 global errorInfo errorCode
2361 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2362 } else {
2363 return -code $code $result
2364 }
2365}
2366
d86bd7cb
TV
2367# Run BODY with timeout factor FACTOR if check-read1 is used.
2368
2369proc with_read1_timeout_factor { factor body } {
2370 if { [info exists ::env(READ1)] == 1 && $::env(READ1) == 1 } {
2371 # Use timeout factor
2372 } else {
2373 # Reset timeout factor
2374 set factor 1
2375 }
2376 return [uplevel [list with_timeout_factor $factor $body]]
2377}
2378
e43ec454
YQ
2379# Return 1 if _Complex types are supported, otherwise, return 0.
2380
17e1c970 2381gdb_caching_proc support_complex_tests {
fdebf1a4
YQ
2382
2383 if { [gdb_skip_float_test] } {
2384 # If floating point is not supported, _Complex is not
2385 # supported.
2386 return 0
2387 }
2388
c221b2f7 2389 # Compile a test program containing _Complex types.
e43ec454 2390
c221b2f7 2391 return [gdb_can_simple_compile complex {
11ec5965
YQ
2392 int main() {
2393 _Complex float cf;
2394 _Complex double cd;
2395 _Complex long double cld;
2396 return 0;
2397 }
c221b2f7 2398 } executable]
e43ec454
YQ
2399}
2400
4d7be007
YQ
2401# Return 1 if GDB can get a type for siginfo from the target, otherwise
2402# return 0.
2403
2404proc supports_get_siginfo_type {} {
5cd867b4 2405 if { [istarget "*-*-linux*"] } {
4d7be007
YQ
2406 return 1
2407 } else {
2408 return 0
2409 }
2410}
2411
1ed415e2 2412# Return 1 if the target supports hardware single stepping.
ab254057 2413
1ed415e2 2414proc can_hardware_single_step {} {
ab254057 2415
b0221781 2416 if { [istarget "arm*-*-*"] || [istarget "mips*-*-*"]
b5bee914
YQ
2417 || [istarget "tic6x-*-*"] || [istarget "sparc*-*-linux*"]
2418 || [istarget "nios2-*-*"] } {
ab254057
YQ
2419 return 0
2420 }
2421
2422 return 1
2423}
2424
1ed415e2
PA
2425# Return 1 if target hardware or OS supports single stepping to signal
2426# handler, otherwise, return 0.
2427
2428proc can_single_step_to_signal_handler {} {
2429 # Targets don't have hardware single step. On these targets, when
2430 # a signal is delivered during software single step, gdb is unable
2431 # to determine the next instruction addresses, because start of signal
2432 # handler is one of them.
2433 return [can_hardware_single_step]
2434}
2435
d3895d7d
YQ
2436# Return 1 if target supports process record, otherwise return 0.
2437
2438proc supports_process_record {} {
2439
2440 if [target_info exists gdb,use_precord] {
2441 return [target_info gdb,use_precord]
2442 }
2443
596662fa 2444 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
b4cdae6f 2445 || [istarget "i\[34567\]86-*-linux*"]
a81bfbd0 2446 || [istarget "aarch64*-*-linux*"]
566c56c9
MK
2447 || [istarget "powerpc*-*-linux*"]
2448 || [istarget "s390*-*-linux*"] } {
d3895d7d
YQ
2449 return 1
2450 }
2451
2452 return 0
2453}
2454
2455# Return 1 if target supports reverse debugging, otherwise return 0.
2456
2457proc supports_reverse {} {
2458
2459 if [target_info exists gdb,can_reverse] {
2460 return [target_info gdb,can_reverse]
2461 }
2462
596662fa 2463 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
b4cdae6f 2464 || [istarget "i\[34567\]86-*-linux*"]
a81bfbd0 2465 || [istarget "aarch64*-*-linux*"]
566c56c9
MK
2466 || [istarget "powerpc*-*-linux*"]
2467 || [istarget "s390*-*-linux*"] } {
d3895d7d
YQ
2468 return 1
2469 }
2470
2471 return 0
2472}
2473
0d4d0e77
YQ
2474# Return 1 if readline library is used.
2475
2476proc readline_is_used { } {
2477 global gdb_prompt
2478
2479 gdb_test_multiple "show editing" "" {
2480 -re ".*Editing of command lines as they are typed is on\..*$gdb_prompt $" {
2481 return 1
2482 }
2483 -re ".*$gdb_prompt $" {
2484 return 0
2485 }
2486 }
2487}
2488
e9f0e62e
NB
2489# Return 1 if target is ELF.
2490gdb_caching_proc is_elf_target {
2491 set me "is_elf_target"
2492
bf326452
AH
2493 set src { int foo () {return 0;} }
2494 if {![gdb_simple_compile elf_target $src]} {
2495 return 0
e9f0e62e
NB
2496 }
2497
2498 set fp_obj [open $obj "r"]
2499 fconfigure $fp_obj -translation binary
2500 set data [read $fp_obj]
2501 close $fp_obj
2502
2503 file delete $obj
2504
2505 set ELFMAG "\u007FELF"
2506
2507 if {[string compare -length 4 $data $ELFMAG] != 0} {
2508 verbose "$me: returning 0" 2
2509 return 0
2510 }
2511
2512 verbose "$me: returning 1" 2
2513 return 1
2514}
2515
20c6f1e1
YQ
2516# Return 1 if the memory at address zero is readable.
2517
2518gdb_caching_proc is_address_zero_readable {
2519 global gdb_prompt
2520
2521 set ret 0
2522 gdb_test_multiple "x 0" "" {
2523 -re "Cannot access memory at address 0x0.*$gdb_prompt $" {
2524 set ret 0
2525 }
2526 -re ".*$gdb_prompt $" {
2527 set ret 1
2528 }
2529 }
2530
2531 return $ret
2532}
2533
6dbb6798
YQ
2534# Produce source file NAME and write SOURCES into it.
2535
2536proc gdb_produce_source { name sources } {
2537 set index 0
2538 set f [open $name "w"]
2539
2540 puts $f $sources
2541 close $f
2542}
2543
add265ae
L
2544# Return 1 if target is ILP32.
2545# This cannot be decided simply from looking at the target string,
2546# as it might depend on externally passed compiler options like -m64.
17e1c970 2547gdb_caching_proc is_ilp32_target {
c221b2f7 2548 return [gdb_can_simple_compile is_ilp32_target {
11ec5965
YQ
2549 int dummy[sizeof (int) == 4
2550 && sizeof (void *) == 4
2551 && sizeof (long) == 4 ? 1 : -1];
c221b2f7 2552 }]
add265ae
L
2553}
2554
2555# Return 1 if target is LP64.
2556# This cannot be decided simply from looking at the target string,
2557# as it might depend on externally passed compiler options like -m64.
17e1c970 2558gdb_caching_proc is_lp64_target {
c221b2f7 2559 return [gdb_can_simple_compile is_lp64_target {
11ec5965
YQ
2560 int dummy[sizeof (int) == 4
2561 && sizeof (void *) == 8
2562 && sizeof (long) == 8 ? 1 : -1];
c221b2f7 2563 }]
add265ae
L
2564}
2565
e630b974
TT
2566# Return 1 if target has 64 bit addresses.
2567# This cannot be decided simply from looking at the target string,
2568# as it might depend on externally passed compiler options like -m64.
2569gdb_caching_proc is_64_target {
c221b2f7 2570 return [gdb_can_simple_compile is_64_target {
11ec5965
YQ
2571 int function(void) { return 3; }
2572 int dummy[sizeof (&function) == 8 ? 1 : -1];
c221b2f7 2573 }]
e630b974
TT
2574}
2575
7f062217
JK
2576# Return 1 if target has x86_64 registers - either amd64 or x32.
2577# x32 target identifies as x86_64-*-linux*, therefore it cannot be determined
2578# just from the target string.
17e1c970 2579gdb_caching_proc is_amd64_regs_target {
68fb0ec0 2580 if {![istarget "x86_64-*-*"] && ![istarget "i?86-*"]} {
7f062217
JK
2581 return 0
2582 }
2583
224d30d3
MM
2584 return [gdb_can_simple_compile is_amd64_regs_target {
2585 int main (void) {
2586 asm ("incq %rax");
2587 asm ("incq %r15");
7f062217 2588
224d30d3
MM
2589 return 0;
2590 }
2591 }]
7f062217
JK
2592}
2593
6edba76f
TT
2594# Return 1 if this target is an x86 or x86-64 with -m32.
2595proc is_x86_like_target {} {
68fb0ec0 2596 if {![istarget "x86_64-*-*"] && ![istarget i?86-*]} {
6edba76f
TT
2597 return 0
2598 }
7f062217 2599 return [expr [is_ilp32_target] && ![is_amd64_regs_target]]
6edba76f
TT
2600}
2601
9fcf688e
YQ
2602# Return 1 if this target is an arm or aarch32 on aarch64.
2603
2604gdb_caching_proc is_aarch32_target {
2605 if { [istarget "arm*-*-*"] } {
2606 return 1
2607 }
2608
2609 if { ![istarget "aarch64*-*-*"] } {
2610 return 0
2611 }
2612
9fcf688e
YQ
2613 set list {}
2614 foreach reg \
2615 {r0 r1 r2 r3} {
2616 lappend list "\tmov $reg, $reg"
2617 }
9fcf688e 2618
c221b2f7 2619 return [gdb_can_simple_compile aarch32 [join $list \n]]
9fcf688e
YQ
2620}
2621
4931af25
YQ
2622# Return 1 if this target is an aarch64, either lp64 or ilp32.
2623
2624proc is_aarch64_target {} {
2625 if { ![istarget "aarch64*-*-*"] } {
2626 return 0
2627 }
2628
2629 return [expr ![is_aarch32_target]]
2630}
2631
be777e08
YQ
2632# Return 1 if displaced stepping is supported on target, otherwise, return 0.
2633proc support_displaced_stepping {} {
2634
2635 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"]
2636 || [istarget "arm*-*-linux*"] || [istarget "powerpc-*-linux*"]
34240514
YQ
2637 || [istarget "powerpc64-*-linux*"] || [istarget "s390*-*-*"]
2638 || [istarget "aarch64*-*-linux*"] } {
be777e08
YQ
2639 return 1
2640 }
2641
2642 return 0
2643}
2644
3c95e6af
PG
2645# Run a test on the target to see if it supports vmx hardware. Return 0 if so,
2646# 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2647
17e1c970 2648gdb_caching_proc skip_altivec_tests {
fda326dd 2649 global srcdir subdir gdb_prompt inferior_exited_re
3c95e6af 2650
3c95e6af 2651 set me "skip_altivec_tests"
3c95e6af
PG
2652
2653 # Some simulators are known to not support VMX instructions.
2654 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
2655 verbose "$me: target known to not support VMX, returning 1" 2
17e1c970 2656 return 1
3c95e6af
PG
2657 }
2658
2659 # Make sure we have a compiler that understands altivec.
4c93b1db 2660 if [get_compiler_info] {
3c95e6af
PG
2661 warning "Could not get compiler info"
2662 return 1
2663 }
2664 if [test_compiler_info gcc*] {
bf326452 2665 set compile_flags "additional_flags=-maltivec"
3c95e6af 2666 } elseif [test_compiler_info xlc*] {
bf326452 2667 set compile_flags "additional_flags=-qaltivec"
3c95e6af
PG
2668 } else {
2669 verbose "Could not compile with altivec support, returning 1" 2
2670 return 1
2671 }
2672
bf326452
AH
2673 # Compile a test program containing VMX instructions.
2674 set src {
11ec5965
YQ
2675 int main() {
2676 #ifdef __MACH__
2677 asm volatile ("vor v0,v0,v0");
2678 #else
2679 asm volatile ("vor 0,0,0");
2680 #endif
2681 return 0;
2682 }
2683 }
bf326452 2684 if {![gdb_simple_compile $me $src executable $compile_flags]} {
17e1c970 2685 return 1
3c95e6af
PG
2686 }
2687
bf326452 2688 # Compilation succeeded so now run it via gdb.
3c95e6af
PG
2689
2690 gdb_exit
2691 gdb_start
2692 gdb_reinitialize_dir $srcdir/$subdir
bf326452 2693 gdb_load "$obj"
3c95e6af
PG
2694 gdb_run_cmd
2695 gdb_expect {
2696 -re ".*Illegal instruction.*${gdb_prompt} $" {
2697 verbose -log "\n$me altivec hardware not detected"
17e1c970 2698 set skip_vmx_tests 1
3c95e6af 2699 }
fda326dd 2700 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3c95e6af 2701 verbose -log "\n$me: altivec hardware detected"
17e1c970 2702 set skip_vmx_tests 0
3c95e6af
PG
2703 }
2704 default {
2705 warning "\n$me: default case taken"
17e1c970 2706 set skip_vmx_tests 1
3c95e6af
PG
2707 }
2708 }
2709 gdb_exit
bf326452 2710 remote_file build delete $obj
3c95e6af 2711
17e1c970
TT
2712 verbose "$me: returning $skip_vmx_tests" 2
2713 return $skip_vmx_tests
3c95e6af
PG
2714}
2715
604c2f83
LM
2716# Run a test on the target to see if it supports vmx hardware. Return 0 if so,
2717# 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2718
17e1c970 2719gdb_caching_proc skip_vsx_tests {
fda326dd 2720 global srcdir subdir gdb_prompt inferior_exited_re
604c2f83 2721
604c2f83 2722 set me "skip_vsx_tests"
604c2f83
LM
2723
2724 # Some simulators are known to not support Altivec instructions, so
2725 # they won't support VSX instructions as well.
2726 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
2727 verbose "$me: target known to not support VSX, returning 1" 2
17e1c970 2728 return 1
604c2f83
LM
2729 }
2730
2731 # Make sure we have a compiler that understands altivec.
4c93b1db 2732 if [get_compiler_info] {
604c2f83
LM
2733 warning "Could not get compiler info"
2734 return 1
2735 }
2736 if [test_compiler_info gcc*] {
bf326452 2737 set compile_flags "additional_flags=-mvsx"
604c2f83 2738 } elseif [test_compiler_info xlc*] {
bf326452 2739 set compile_flags "additional_flags=-qasm=gcc"
604c2f83
LM
2740 } else {
2741 verbose "Could not compile with vsx support, returning 1" 2
2742 return 1
2743 }
2744
bf326452
AH
2745 # Compile a test program containing VSX instructions.
2746 set src {
11ec5965
YQ
2747 int main() {
2748 double a[2] = { 1.0, 2.0 };
2749 #ifdef __MACH__
2750 asm volatile ("lxvd2x v0,v0,%[addr]" : : [addr] "r" (a));
2751 #else
2752 asm volatile ("lxvd2x 0,0,%[addr]" : : [addr] "r" (a));
2753 #endif
2754 return 0;
2755 }
2756 }
bf326452 2757 if {![gdb_simple_compile $me $src executable $compile_flags]} {
17e1c970 2758 return 1
604c2f83
LM
2759 }
2760
2761 # No error message, compilation succeeded so now run it via gdb.
2762
2763 gdb_exit
2764 gdb_start
2765 gdb_reinitialize_dir $srcdir/$subdir
bf326452 2766 gdb_load "$obj"
604c2f83
LM
2767 gdb_run_cmd
2768 gdb_expect {
2769 -re ".*Illegal instruction.*${gdb_prompt} $" {
2770 verbose -log "\n$me VSX hardware not detected"
17e1c970 2771 set skip_vsx_tests 1
604c2f83 2772 }
fda326dd 2773 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
604c2f83 2774 verbose -log "\n$me: VSX hardware detected"
17e1c970 2775 set skip_vsx_tests 0
604c2f83
LM
2776 }
2777 default {
2778 warning "\n$me: default case taken"
17e1c970 2779 set skip_vsx_tests 1
604c2f83
LM
2780 }
2781 }
2782 gdb_exit
bf326452 2783 remote_file build delete $obj
604c2f83 2784
17e1c970
TT
2785 verbose "$me: returning $skip_vsx_tests" 2
2786 return $skip_vsx_tests
604c2f83
LM
2787}
2788
da8c46d2
MM
2789# Run a test on the target to see if it supports TSX hardware. Return 0 if so,
2790# 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2791
2792gdb_caching_proc skip_tsx_tests {
2793 global srcdir subdir gdb_prompt inferior_exited_re
2794
2795 set me "skip_tsx_tests"
2796
bf326452
AH
2797 # Compile a test program.
2798 set src {
2799 int main() {
2800 asm volatile ("xbegin .L0");
2801 asm volatile ("xend");
2802 asm volatile (".L0: nop");
2803 return 0;
2804 }
da8c46d2 2805 }
bf326452 2806 if {![gdb_simple_compile $me $src executable]} {
da8c46d2
MM
2807 return 1
2808 }
2809
2810 # No error message, compilation succeeded so now run it via gdb.
2811
2812 gdb_exit
2813 gdb_start
2814 gdb_reinitialize_dir $srcdir/$subdir
bf326452 2815 gdb_load "$obj"
da8c46d2
MM
2816 gdb_run_cmd
2817 gdb_expect {
2818 -re ".*Illegal instruction.*${gdb_prompt} $" {
2819 verbose -log "$me: TSX hardware not detected."
2820 set skip_tsx_tests 1
2821 }
2822 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
2823 verbose -log "$me: TSX hardware detected."
2824 set skip_tsx_tests 0
2825 }
2826 default {
2827 warning "\n$me: default case taken."
2828 set skip_tsx_tests 1
2829 }
2830 }
2831 gdb_exit
bf326452 2832 remote_file build delete $obj
da8c46d2
MM
2833
2834 verbose "$me: returning $skip_tsx_tests" 2
2835 return $skip_tsx_tests
2836}
2837
2f1d9bdd
MM
2838# Run a test on the target to see if it supports btrace hardware. Return 0 if so,
2839# 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2840
f3a76454 2841gdb_caching_proc skip_btrace_tests {
2f1d9bdd
MM
2842 global srcdir subdir gdb_prompt inferior_exited_re
2843
2f1d9bdd 2844 set me "skip_btrace_tests"
2f1d9bdd
MM
2845 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
2846 verbose "$me: target does not support btrace, returning 1" 2
f3a76454 2847 return 1
2f1d9bdd
MM
2848 }
2849
bf326452
AH
2850 # Compile a test program.
2851 set src { int main() { return 0; } }
2852 if {![gdb_simple_compile $me $src executable]} {
2853 return 0
2f1d9bdd
MM
2854 }
2855
2856 # No error message, compilation succeeded so now run it via gdb.
2857
f3a76454
TT
2858 gdb_exit
2859 gdb_start
2860 gdb_reinitialize_dir $srcdir/$subdir
bf326452 2861 gdb_load $obj
2f1d9bdd 2862 if ![runto_main] {
f3a76454 2863 return 1
2f1d9bdd
MM
2864 }
2865 # In case of an unexpected output, we return 2 as a fail value.
f3a76454 2866 set skip_btrace_tests 2
2f1d9bdd
MM
2867 gdb_test_multiple "record btrace" "check btrace support" {
2868 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
f3a76454 2869 set skip_btrace_tests 1
2f1d9bdd
MM
2870 }
2871 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
f3a76454 2872 set skip_btrace_tests 1
2f1d9bdd
MM
2873 }
2874 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
f3a76454 2875 set skip_btrace_tests 1
2f1d9bdd
MM
2876 }
2877 -re "^record btrace\r\n$gdb_prompt $" {
f3a76454 2878 set skip_btrace_tests 0
2f1d9bdd
MM
2879 }
2880 }
2881 gdb_exit
bf326452 2882 remote_file build delete $obj
2f1d9bdd 2883
f3a76454
TT
2884 verbose "$me: returning $skip_btrace_tests" 2
2885 return $skip_btrace_tests
2f1d9bdd
MM
2886}
2887
da8c46d2
MM
2888# Run a test on the target to see if it supports btrace pt hardware.
2889# Return 0 if so, 1 if it does not. Based on 'check_vmx_hw_available'
2890# from the GCC testsuite.
2891
2892gdb_caching_proc skip_btrace_pt_tests {
2893 global srcdir subdir gdb_prompt inferior_exited_re
2894
2895 set me "skip_btrace_tests"
2896 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
2897 verbose "$me: target does not support btrace, returning 1" 2
2898 return 1
2899 }
2900
bf326452
AH
2901 # Compile a test program.
2902 set src { int main() { return 0; } }
2903 if {![gdb_simple_compile $me $src executable]} {
2904 return 0
da8c46d2
MM
2905 }
2906
2907 # No error message, compilation succeeded so now run it via gdb.
2908
2909 gdb_exit
2910 gdb_start
2911 gdb_reinitialize_dir $srcdir/$subdir
bf326452 2912 gdb_load $obj
da8c46d2 2913 if ![runto_main] {
da8c46d2
MM
2914 return 1
2915 }
da8c46d2
MM
2916 # In case of an unexpected output, we return 2 as a fail value.
2917 set skip_btrace_tests 2
c4e12631 2918 gdb_test_multiple "record btrace pt" "check btrace pt support" {
da8c46d2
MM
2919 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
2920 set skip_btrace_tests 1
2921 }
2922 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
2923 set skip_btrace_tests 1
2924 }
2925 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
2926 set skip_btrace_tests 1
2927 }
c4e12631 2928 -re "support was disabled at compile time.*\r\n$gdb_prompt $" {
46a3515b
MM
2929 set skip_btrace_tests 1
2930 }
da8c46d2
MM
2931 -re "^record btrace pt\r\n$gdb_prompt $" {
2932 set skip_btrace_tests 0
2933 }
2934 }
2935 gdb_exit
bf326452 2936 remote_file build delete $obj
da8c46d2
MM
2937
2938 verbose "$me: returning $skip_btrace_tests" 2
2939 return $skip_btrace_tests
2940}
2941
6bb8890e
AH
2942# Run a test on the target to see if it supports Aarch64 SVE hardware.
2943# Return 0 if so, 1 if it does not. Note this causes a restart of GDB.
2944
2945gdb_caching_proc skip_aarch64_sve_tests {
2946 global srcdir subdir gdb_prompt inferior_exited_re
2947
2948 set me "skip_aarch64_sve_tests"
2949
2950 if { ![is_aarch64_target]} {
2951 return 1
2952 }
2953
2954 set compile_flags "{additional_flags=-march=armv8-a+sve}"
2955
2956 # Compile a test program containing SVE instructions.
2957 set src {
2958 int main() {
2959 asm volatile ("ptrue p0.b");
2960 return 0;
2961 }
2962 }
2963 if {![gdb_simple_compile $me $src executable $compile_flags]} {
2964 return 1
2965 }
2966
2967 # Compilation succeeded so now run it via gdb.
2968 clean_restart $obj
2969 gdb_run_cmd
2970 gdb_expect {
2971 -re ".*Illegal instruction.*${gdb_prompt} $" {
2972 verbose -log "\n$me sve hardware not detected"
2973 set skip_sve_tests 1
2974 }
2975 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
2976 verbose -log "\n$me: sve hardware detected"
2977 set skip_sve_tests 0
2978 }
2979 default {
2980 warning "\n$me: default case taken"
2981 set skip_sve_tests 1
2982 }
2983 }
2984 gdb_exit
2985 remote_file build delete $obj
2986
2987 verbose "$me: returning $skip_sve_tests" 2
2988 return $skip_sve_tests
2989}
2990
2991
007e1530
TT
2992# A helper that compiles a test case to see if __int128 is supported.
2993proc gdb_int128_helper {lang} {
c221b2f7 2994 return [gdb_can_simple_compile "i128-for-$lang" {
007e1530
TT
2995 __int128 x;
2996 int main() { return 0; }
c221b2f7 2997 } executable $lang]
007e1530
TT
2998}
2999
3000# Return true if the C compiler understands the __int128 type.
3001gdb_caching_proc has_int128_c {
3002 return [gdb_int128_helper c]
3003}
3004
3005# Return true if the C++ compiler understands the __int128 type.
3006gdb_caching_proc has_int128_cxx {
3007 return [gdb_int128_helper c++]
3008}
3009
ca98345e
SL
3010# Return true if the IFUNC feature is unsupported.
3011gdb_caching_proc skip_ifunc_tests {
3012 if [gdb_can_simple_compile ifunc {
3013 extern void f_ ();
3014 typedef void F (void);
3015 F* g (void) { return &f_; }
3016 void f () __attribute__ ((ifunc ("g")));
3017 } object] {
3018 return 0
3019 } else {
3020 return 1
3021 }
3022}
3023
edb3359d
DJ
3024# Return whether we should skip tests for showing inlined functions in
3025# backtraces. Requires get_compiler_info and get_debug_format.
3026
3027proc skip_inline_frame_tests {} {
3028 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
3029 if { ! [test_debug_format "DWARF 2"] } {
3030 return 1
3031 }
3032
3033 # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line.
3034 if { ([test_compiler_info "gcc-2-*"]
3035 || [test_compiler_info "gcc-3-*"]
3036 || [test_compiler_info "gcc-4-0-*"]) } {
3037 return 1
3038 }
3039
3040 return 0
3041}
3042
3043# Return whether we should skip tests for showing variables from
3044# inlined functions. Requires get_compiler_info and get_debug_format.
3045
3046proc skip_inline_var_tests {} {
3047 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
3048 if { ! [test_debug_format "DWARF 2"] } {
3049 return 1
3050 }
3051
3052 return 0
3053}
3054
b800ec70
UW
3055# Return a 1 if we should skip tests that require hardware breakpoints
3056
3057proc skip_hw_breakpoint_tests {} {
3058 # Skip tests if requested by the board (note that no_hardware_watchpoints
3059 # disables both watchpoints and breakpoints)
3060 if { [target_info exists gdb,no_hardware_watchpoints]} {
3061 return 1
3062 }
3063
3064 # These targets support hardware breakpoints natively
3065 if { [istarget "i?86-*-*"]
3066 || [istarget "x86_64-*-*"]
e3039479 3067 || [istarget "ia64-*-*"]
52042a00 3068 || [istarget "arm*-*-*"]
8193adea
AA
3069 || [istarget "aarch64*-*-*"]
3070 || [istarget "s390*-*-*"] } {
b800ec70
UW
3071 return 0
3072 }
3073
3074 return 1
3075}
3076
3077# Return a 1 if we should skip tests that require hardware watchpoints
3078
3079proc skip_hw_watchpoint_tests {} {
3080 # Skip tests if requested by the board
3081 if { [target_info exists gdb,no_hardware_watchpoints]} {
3082 return 1
3083 }
3084
3085 # These targets support hardware watchpoints natively
3086 if { [istarget "i?86-*-*"]
3087 || [istarget "x86_64-*-*"]
3088 || [istarget "ia64-*-*"]
e3039479 3089 || [istarget "arm*-*-*"]
52042a00 3090 || [istarget "aarch64*-*-*"]
b800ec70
UW
3091 || [istarget "powerpc*-*-linux*"]
3092 || [istarget "s390*-*-*"] } {
3093 return 0
3094 }
3095
3096 return 1
3097}
3098
3099# Return a 1 if we should skip tests that require *multiple* hardware
3100# watchpoints to be active at the same time
3101
3102proc skip_hw_watchpoint_multi_tests {} {
3103 if { [skip_hw_watchpoint_tests] } {
3104 return 1
3105 }
3106
3107 # These targets support just a single hardware watchpoint
e3039479
UW
3108 if { [istarget "arm*-*-*"]
3109 || [istarget "powerpc*-*-linux*"] } {
b800ec70
UW
3110 return 1
3111 }
3112
3113 return 0
3114}
3115
3116# Return a 1 if we should skip tests that require read/access watchpoints
3117
3118proc skip_hw_watchpoint_access_tests {} {
3119 if { [skip_hw_watchpoint_tests] } {
3120 return 1
3121 }
3122
3123 # These targets support just write watchpoints
3124 if { [istarget "s390*-*-*"] } {
3125 return 1
3126 }
3127
3128 return 0
3129}
3130
b4893d48
TT
3131# Return 1 if we should skip tests that require the runtime unwinder
3132# hook. This must be invoked while gdb is running, after shared
3133# libraries have been loaded. This is needed because otherwise a
3134# shared libgcc won't be visible.
3135
3136proc skip_unwinder_tests {} {
3137 global gdb_prompt
3138
4442ada7 3139 set ok 0
b4893d48
TT
3140 gdb_test_multiple "print _Unwind_DebugHook" "check for unwinder hook" {
3141 -re "= .*no debug info.*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
b4893d48
TT
3142 }
3143 -re "= .*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
4442ada7 3144 set ok 1
b4893d48
TT
3145 }
3146 -re "No symbol .* in current context.\r\n$gdb_prompt $" {
b4893d48
TT
3147 }
3148 }
3149 if {!$ok} {
3150 gdb_test_multiple "info probe" "check for stap probe in unwinder" {
3151 -re ".*libgcc.*unwind.*\r\n$gdb_prompt $" {
b4893d48
TT
3152 set ok 1
3153 }
3154 -re "\r\n$gdb_prompt $" {
3155 }
3156 }
3157 }
3158 return $ok
3159}
3160
b694989f 3161# Return 1 if we should skip tests that require the libstdc++ stap
72f1fe8a 3162# probes. This must be invoked while gdb is running, after shared
297989a1 3163# libraries have been loaded. PROMPT_REGEXP is the expected prompt.
72f1fe8a 3164
297989a1 3165proc skip_libstdcxx_probe_tests_prompt { prompt_regexp } {
b694989f 3166 set supported 0
72f1fe8a 3167 gdb_test_multiple "info probe" "check for stap probe in libstdc++" {
297989a1 3168 -re ".*libstdcxx.*catch.*\r\n$prompt_regexp" {
b694989f 3169 set supported 1
72f1fe8a 3170 }
297989a1 3171 -re "\r\n$prompt_regexp" {
72f1fe8a 3172 }
2d274232 3173 } "$prompt_regexp"
b694989f
TV
3174 set skip [expr !$supported]
3175 return $skip
72f1fe8a
TT
3176}
3177
297989a1
TV
3178# As skip_libstdcxx_probe_tests_prompt, with gdb_prompt.
3179
3180proc skip_libstdcxx_probe_tests {} {
3181 global gdb_prompt
3182 return [skip_libstdcxx_probe_tests_prompt "$gdb_prompt $"]
3183}
3184
bb2ec1b3
TT
3185# Return 1 if we should skip tests of the "compile" feature.
3186# This must be invoked after the inferior has been started.
3187
3188proc skip_compile_feature_tests {} {
3189 global gdb_prompt
3190
3191 set result 0
3192 gdb_test_multiple "compile code -- ;" "check for working compile command" {
3193 "Could not load libcc1.*\r\n$gdb_prompt $" {
3194 set result 1
3195 }
1bc1068a
JK
3196 -re "Command not supported on this host\\..*\r\n$gdb_prompt $" {
3197 set result 1
3198 }
bb2ec1b3
TT
3199 -re "\r\n$gdb_prompt $" {
3200 }
3201 }
3202 return $result
3203}
3204
3275ef47
SM
3205# Helper for gdb_is_target_* procs. TARGET_NAME is the name of the target
3206# we're looking for (used to build the test name). TARGET_STACK_REGEXP
3207# is a regexp that will match the output of "maint print target-stack" if
3083294d
SM
3208# the target in question is currently pushed. PROMPT_REGEXP is a regexp
3209# matching the expected prompt after the command output.
076855f9 3210
3083294d 3211proc gdb_is_target_1 { target_name target_stack_regexp prompt_regexp } {
3275ef47 3212 set test "probe for target ${target_name}"
076855f9 3213 gdb_test_multiple "maint print target-stack" $test {
3275ef47 3214 -re "${target_stack_regexp}${prompt_regexp}" {
076855f9
PA
3215 pass $test
3216 return 1
3217 }
f015c27b 3218 -re "$prompt_regexp" {
076855f9
PA
3219 pass $test
3220 }
2d274232 3221 } "$prompt_regexp"
076855f9
PA
3222 return 0
3223}
3224
3083294d
SM
3225# Helper for gdb_is_target_remote where the expected prompt is variable.
3226
3227proc gdb_is_target_remote_prompt { prompt_regexp } {
3228 return [gdb_is_target_1 "remote" ".*emote serial target in gdb-specific protocol.*" $prompt_regexp]
3229}
3230
f015c27b
PA
3231# Check whether we're testing with the remote or extended-remote
3232# targets.
3233
3275ef47 3234proc gdb_is_target_remote { } {
3083294d
SM
3235 global gdb_prompt
3236
3237 return [gdb_is_target_remote_prompt "$gdb_prompt $"]
3275ef47
SM
3238}
3239
3240# Check whether we're testing with the native target.
f015c27b 3241
3275ef47 3242proc gdb_is_target_native { } {
3083294d
SM
3243 global gdb_prompt
3244
3245 return [gdb_is_target_1 "native" ".*native \\(Native process\\).*" "$gdb_prompt $"]
f015c27b
PA
3246}
3247
8929ad8b
SM
3248# Return the effective value of use_gdb_stub.
3249#
3250# If the use_gdb_stub global has been set (it is set when the gdb process is
3251# spawned), return that. Otherwise, return the value of the use_gdb_stub
3252# property from the board file.
3253#
3254# This is the preferred way of checking use_gdb_stub, since it allows to check
3255# the value before the gdb has been spawned and it will return the correct value
3256# even when it was overriden by the test.
3257
3258proc use_gdb_stub {} {
3259 global use_gdb_stub
3260
3261 if [info exists use_gdb_stub] {
3262 return $use_gdb_stub
3263 }
3264
3265 return [target_info exists use_gdb_stub]
3266}
3267
0a46d518
SM
3268# Return 1 if the current remote target is an instance of our GDBserver, 0
3269# otherwise. Return -1 if there was an error and we can't tell.
3270
3271gdb_caching_proc target_is_gdbserver {
3272 global gdb_prompt
3273
3274 set is_gdbserver -1
bc6c7af4 3275 set test "probing for GDBserver"
0a46d518
SM
3276
3277 gdb_test_multiple "monitor help" $test {
3278 -re "The following monitor commands are supported.*Quit GDBserver.*$gdb_prompt $" {
3279 set is_gdbserver 1
3280 }
3281 -re "$gdb_prompt $" {
3282 set is_gdbserver 0
3283 }
3284 }
3285
3286 if { $is_gdbserver == -1 } {
3287 verbose -log "Unable to tell whether we are using GDBserver or not."
3288 }
3289
3290 return $is_gdbserver
3291}
3292
a97b16b8
DE
3293# N.B. compiler_info is intended to be local to this file.
3294# Call test_compiler_info with no arguments to fetch its value.
3295# Yes, this is counterintuitive when there's get_compiler_info,
3296# but that's the current API.
3297if [info exists compiler_info] {
3298 unset compiler_info
3299}
3300
94b8e876 3301set gcc_compiled 0
94b8e876
MC
3302
3303# Figure out what compiler I am using.
a97b16b8 3304# The result is cached so only the first invocation runs the compiler.
94b8e876 3305#
4c93b1db 3306# ARG can be empty or "C++". If empty, "C" is assumed.
94b8e876
MC
3307#
3308# There are several ways to do this, with various problems.
3309#
3310# [ gdb_compile -E $ifile -o $binfile.ci ]
3311# source $binfile.ci
3312#
3313# Single Unix Spec v3 says that "-E -o ..." together are not
3314# specified. And in fact, the native compiler on hp-ux 11 (among
3315# others) does not work with "-E -o ...". Most targets used to do
3316# this, and it mostly worked, because it works with gcc.
3317#
3318# [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
3319# source $binfile.ci
3320#
3321# This avoids the problem with -E and -o together. This almost works
3322# if the build machine is the same as the host machine, which is
3323# usually true of the targets which are not gcc. But this code does
3324# not figure which compiler to call, and it always ends up using the C
3831839c
PA
3325# compiler. Not good for setting hp_aCC_compiler. Target
3326# hppa*-*-hpux* used to do this.
94b8e876
MC
3327#
3328# [ gdb_compile -E $ifile > $binfile.ci ]
3329# source $binfile.ci
3330#
3331# dejagnu target_compile says that it supports output redirection,
3332# but the code is completely different from the normal path and I
3333# don't want to sweep the mines from that path. So I didn't even try
3334# this.
3335#
3336# set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
3337# eval $cppout
3338#
3339# I actually do this for all targets now. gdb_compile runs the right
3340# compiler, and TCL captures the output, and I eval the output.
3341#
3342# Unfortunately, expect logs the output of the command as it goes by,
3343# and dejagnu helpfully prints a second copy of it right afterwards.
3344# So I turn off expect logging for a moment.
3345#
3346# [ gdb_compile $ifile $ciexe_file executable $args ]
3347# [ remote_exec $ciexe_file ]
3348# [ source $ci_file.out ]
3349#
3350# I could give up on -E and just do this.
3351# I didn't get desperate enough to try this.
3352#
3353# -- chastain 2004-01-06
853d6e5b 3354
4c93b1db 3355proc get_compiler_info {{arg ""}} {
94b8e876 3356 # For compiler.c and compiler.cc
c906108c 3357 global srcdir
94b8e876
MC
3358
3359 # I am going to play with the log to keep noise out.
3360 global outdir
3361 global tool
3362
3363 # These come from compiler.c or compiler.cc
853d6e5b 3364 global compiler_info
4f70a4c9
MC
3365
3366 # Legacy global data symbols.
94b8e876 3367 global gcc_compiled
c906108c 3368
a97b16b8
DE
3369 if [info exists compiler_info] {
3370 # Already computed.
3371 return 0
3372 }
3373
94b8e876
MC
3374 # Choose which file to preprocess.
3375 set ifile "${srcdir}/lib/compiler.c"
4c93b1db 3376 if { $arg == "c++" } {
94b8e876 3377 set ifile "${srcdir}/lib/compiler.cc"
c906108c 3378 }
085dd6e6 3379
94b8e876
MC
3380 # Run $ifile through the right preprocessor.
3381 # Toggle gdb.log to keep the compiler output out of the log.
95d7853e 3382 set saved_log [log_file -info]
94b8e876 3383 log_file
e7f86de9
JM
3384 if [is_remote host] {
3385 # We have to use -E and -o together, despite the comments
3386 # above, because of how DejaGnu handles remote host testing.
3387 set ppout "$outdir/compiler.i"
fc65c7db 3388 gdb_compile "${ifile}" "$ppout" preprocess [list "$arg" quiet getting_compiler_info]
e7f86de9
JM
3389 set file [open $ppout r]
3390 set cppout [read $file]
3391 close $file
3392 } else {
fc65c7db 3393 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$arg" quiet getting_compiler_info] ]
e7f86de9 3394 }
95d7853e 3395 eval log_file $saved_log
94b8e876 3396
4f70a4c9
MC
3397 # Eval the output.
3398 set unknown 0
94b8e876 3399 foreach cppline [ split "$cppout" "\n" ] {
4f70a4c9
MC
3400 if { [ regexp "^#" "$cppline" ] } {
3401 # line marker
3402 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
3403 # blank line
3404 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
3405 # eval this line
3406 verbose "get_compiler_info: $cppline" 2
3407 eval "$cppline"
3408 } else {
3409 # unknown line
3410 verbose -log "get_compiler_info: $cppline"
3411 set unknown 1
94b8e876 3412 }
085dd6e6 3413 }
4f70a4c9 3414
a97b16b8
DE
3415 # Set to unknown if for some reason compiler_info didn't get defined.
3416 if ![info exists compiler_info] {
3417 verbose -log "get_compiler_info: compiler_info not provided"
3418 set compiler_info "unknown"
3419 }
3420 # Also set to unknown compiler if any diagnostics happened.
4f70a4c9 3421 if { $unknown } {
a97b16b8 3422 verbose -log "get_compiler_info: got unexpected diagnostics"
4f70a4c9 3423 set compiler_info "unknown"
4f70a4c9
MC
3424 }
3425
3426 # Set the legacy symbols.
f90fd8c2
JK
3427 set gcc_compiled 0
3428 regexp "^gcc-(\[0-9\]+)-" "$compiler_info" matchall gcc_compiled
4f70a4c9
MC
3429
3430 # Log what happened.
94b8e876 3431 verbose -log "get_compiler_info: $compiler_info"
085dd6e6
JM
3432
3433 # Most compilers will evaluate comparisons and other boolean
3434 # operations to 0 or 1.
3435 uplevel \#0 { set true 1 }
3436 uplevel \#0 { set false 0 }
3437
ae59b1da 3438 return 0
c906108c
SS
3439}
3440
a97b16b8
DE
3441# Return the compiler_info string if no arg is provided.
3442# Otherwise the argument is a glob-style expression to match against
3443# compiler_info.
3444
9b593790 3445proc test_compiler_info { {compiler ""} } {
853d6e5b 3446 global compiler_info
a97b16b8 3447 get_compiler_info
6e87504d 3448
a97b16b8
DE
3449 # If no arg, return the compiler_info string.
3450 if [string match "" $compiler] {
3451 return $compiler_info
3452 }
6e87504d 3453
853d6e5b
AC
3454 return [string match $compiler $compiler_info]
3455}
3456
f6838f81
DJ
3457proc current_target_name { } {
3458 global target_info
3459 if [info exists target_info(target,name)] {
3460 set answer $target_info(target,name)
3461 } else {
3462 set answer ""
3463 }
3464 return $answer
3465}
3466
f1c47eb2 3467set gdb_wrapper_initialized 0
f6838f81 3468set gdb_wrapper_target ""
f1c47eb2
MS
3469
3470proc gdb_wrapper_init { args } {
4ec70201
PA
3471 global gdb_wrapper_initialized
3472 global gdb_wrapper_file
3473 global gdb_wrapper_flags
f6838f81 3474 global gdb_wrapper_target
f1c47eb2
MS
3475
3476 if { $gdb_wrapper_initialized == 1 } { return; }
3477
3478 if {[target_info exists needs_status_wrapper] && \
277254ba 3479 [target_info needs_status_wrapper] != "0"} {
4ec70201 3480 set result [build_wrapper "testglue.o"]
f1c47eb2 3481 if { $result != "" } {
4ec70201
PA
3482 set gdb_wrapper_file [lindex $result 0]
3483 set gdb_wrapper_flags [lindex $result 1]
f1c47eb2
MS
3484 } else {
3485 warning "Status wrapper failed to build."
3486 }
3487 }
3488 set gdb_wrapper_initialized 1
f6838f81 3489 set gdb_wrapper_target [current_target_name]
f1c47eb2
MS
3490}
3491
bf0ec4c2
AA
3492# Determine options that we always want to pass to the compiler.
3493gdb_caching_proc universal_compile_options {
3494 set me "universal_compile_options"
3495 set options {}
3496
3497 set src [standard_temp_file ccopts[pid].c]
3498 set obj [standard_temp_file ccopts[pid].o]
3499
3500 gdb_produce_source $src {
3501 int foo(void) { return 0; }
3502 }
3503
3504 # Try an option for disabling colored diagnostics. Some compilers
3505 # yield colored diagnostics by default (when run from a tty) unless
3506 # such an option is specified.
3507 set opt "additional_flags=-fdiagnostics-color=never"
3508 set lines [target_compile $src $obj object [list "quiet" $opt]]
3509 if [string match "" $lines] then {
3510 # Seems to have worked; use the option.
3511 lappend options $opt
3512 }
3513 file delete $src
3514 file delete $obj
3515
3516 verbose "$me: returning $options" 2
3517 return $options
3518}
3519
c221b2f7
AH
3520# Compile the code in $code to a file based on $name, using the flags
3521# $compile_flag as well as debug, nowarning and quiet.
3522# Return 1 if code can be compiled
bf326452 3523# Leave the file name of the resulting object in the upvar object.
c221b2f7 3524
bf326452
AH
3525proc gdb_simple_compile {name code {type object} {compile_flags {}} {object obj}} {
3526 upvar $object obj
c221b2f7
AH
3527
3528 switch -regexp -- $type {
3529 "executable" {
3530 set postfix "x"
3531 }
3532 "object" {
3533 set postfix "o"
3534 }
3535 "preprocess" {
3536 set postfix "i"
3537 }
3538 "assembly" {
3539 set postfix "s"
3540 }
3541 }
3542 set src [standard_temp_file $name-[pid].c]
3543 set obj [standard_temp_file $name-[pid].$postfix]
3544 set compile_flags [concat $compile_flags {debug nowarnings quiet}]
3545
3546 gdb_produce_source $src $code
3547
3548 verbose "$name: compiling testfile $src" 2
3549 set lines [gdb_compile $src $obj $type $compile_flags]
3550
3551 file delete $src
c221b2f7
AH
3552
3553 if ![string match "" $lines] then {
3554 verbose "$name: compilation failed, returning 0" 2
3555 return 0
3556 }
3557 return 1
3558}
3559
bf326452
AH
3560# Compile the code in $code to a file based on $name, using the flags
3561# $compile_flag as well as debug, nowarning and quiet.
3562# Return 1 if code can be compiled
3563# Delete all created files and objects.
3564
3565proc gdb_can_simple_compile {name code {type object} {compile_flags ""}} {
3566 set ret [gdb_simple_compile $name $code $type $compile_flags temp_obj]
3567 file delete $temp_obj
3568 return $ret
3569}
3570
f747e0ce
PA
3571# Some targets need to always link a special object in. Save its path here.
3572global gdb_saved_set_unbuffered_mode_obj
3573set gdb_saved_set_unbuffered_mode_obj ""
3574
aff9c0f8
SM
3575# Compile source files specified by SOURCE into a binary of type TYPE at path
3576# DEST. gdb_compile is implemented using DejaGnu's target_compile, so the type
3577# parameter and most options are passed directly to it.
3578#
3579# The type can be one of the following:
3580#
3581# - object: Compile into an object file.
3582# - executable: Compile and link into an executable.
3583# - preprocess: Preprocess the source files.
3584# - assembly: Generate assembly listing.
3585#
3586# The following options are understood and processed by gdb_compile:
3587#
3588# - shlib=so_path: Add SO_PATH to the sources, and enable some target-specific
3589# quirks to be able to use shared libraries.
3590# - shlib_load: Link with appropriate libraries to allow the test to
3591# dynamically load libraries at runtime. For example, on Linux, this adds
3592# -ldl so that the test can use dlopen.
3593# - nowarnings: Inhibit all compiler warnings.
968aa7ae 3594# - pie: Force creation of PIE executables.
6e8b1ab2 3595# - nopie: Prevent creation of PIE executables.
aff9c0f8
SM
3596#
3597# And here are some of the not too obscure options understood by DejaGnu that
3598# influence the compilation:
3599#
3600# - additional_flags=flag: Add FLAG to the compiler flags.
3601# - libs=library: Add LIBRARY to the libraries passed to the linker. The
3602# argument can be a file, in which case it's added to the sources, or a
3603# linker flag.
3604# - ldflags=flag: Add FLAG to the linker flags.
3605# - incdir=path: Add PATH to the searched include directories.
3606# - libdir=path: Add PATH to the linker searched directories.
3607# - ada, c++, f77: Compile the file as Ada, C++ or Fortran.
3608# - debug: Build with debug information.
3609# - optimize: Build with optimization.
3610
c906108c 3611proc gdb_compile {source dest type options} {
4ec70201
PA
3612 global GDB_TESTCASE_OPTIONS
3613 global gdb_wrapper_file
3614 global gdb_wrapper_flags
3615 global gdb_wrapper_initialized
f747e0ce
PA
3616 global srcdir
3617 global objdir
3618 global gdb_saved_set_unbuffered_mode_obj
c906108c 3619
695e2681
MK
3620 set outdir [file dirname $dest]
3621
3622 # Add platform-specific options if a shared library was specified using
3623 # "shlib=librarypath" in OPTIONS.
dcc06925 3624 set new_options {}
5eb5f850
TT
3625 if {[lsearch -exact $options rust] != -1} {
3626 # -fdiagnostics-color is not a rustcc option.
3627 } else {
3628 set new_options [universal_compile_options]
3629 }
695e2681 3630 set shlib_found 0
bdf7534a 3631 set shlib_load 0
fc65c7db 3632 set getting_compiler_info 0
695e2681 3633 foreach opt $options {
6181e9c2
SM
3634 if {[regexp {^shlib=(.*)} $opt dummy_var shlib_name]
3635 && $type == "executable"} {
57bf0e56 3636 if [test_compiler_info "xlc-*"] {
93f02886
DJ
3637 # IBM xlc compiler doesn't accept shared library named other
3638 # than .so: use "-Wl," to bypass this
3639 lappend source "-Wl,$shlib_name"
3640 } elseif { ([istarget "*-*-mingw*"]
3641 || [istarget *-*-cygwin*]
3642 || [istarget *-*-pe*])} {
3643 lappend source "${shlib_name}.a"
57bf0e56
DJ
3644 } else {
3645 lappend source $shlib_name
3646 }
0413d738 3647 if { $shlib_found == 0 } {
57bf0e56 3648 set shlib_found 1
0413d738
PA
3649 if { ([istarget "*-*-mingw*"]
3650 || [istarget *-*-cygwin*]) } {
bb61102d 3651 lappend new_options "additional_flags=-Wl,--enable-auto-import"
0413d738 3652 }
6ebea266
DE
3653 if { [test_compiler_info "gcc-*"] || [test_compiler_info "clang-*"] } {
3654 # Undo debian's change in the default.
3655 # Put it at the front to not override any user-provided
3656 # value, and to make sure it appears in front of all the
3657 # shlibs!
3658 lappend new_options "early_flags=-Wl,--no-as-needed"
3659 }
57bf0e56 3660 }
6181e9c2 3661 } elseif { $opt == "shlib_load" && $type == "executable" } {
bdf7534a 3662 set shlib_load 1
fc65c7db
AH
3663 } elseif { $opt == "getting_compiler_info" } {
3664 # If this is set, calling test_compiler_info will cause recursion.
3665 set getting_compiler_info 1
57bf0e56
DJ
3666 } else {
3667 lappend new_options $opt
3668 }
695e2681 3669 }
bdf7534a 3670
fc65c7db
AH
3671 # Ensure stack protector is disabled for GCC, as this causes problems with
3672 # DWARF line numbering.
3673 # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88432
3674 # This option defaults to on for Debian/Ubuntu.
3675 if { $getting_compiler_info == 0
3676 && [test_compiler_info {gcc-*-*}]
3677 && !([test_compiler_info {gcc-[0-3]-*}]
1670072e
TT
3678 || [test_compiler_info {gcc-4-0-*}])
3679 && [lsearch -exact $options rust] == -1} {
fc65c7db
AH
3680 # Put it at the front to not override any user-provided value.
3681 lappend new_options "early_flags=-fno-stack-protector"
3682 }
3683
6e774b13
SM
3684 # Because we link with libraries using their basename, we may need
3685 # (depending on the platform) to set a special rpath value, to allow
3686 # the executable to find the libraries it depends on.
3687 if { $shlib_load || $shlib_found } {
bdf7534a
NF
3688 if { ([istarget "*-*-mingw*"]
3689 || [istarget *-*-cygwin*]
3ca22649 3690 || [istarget *-*-pe*]) } {
bdf7534a 3691 # Do not need anything.
b2a6bdeb 3692 } elseif { [istarget *-*-freebsd*] || [istarget *-*-openbsd*] } {
d8b34041 3693 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
759f0f0b
PA
3694 } elseif { [istarget arm*-*-symbianelf*] } {
3695 if { $shlib_load } {
3696 lappend new_options "libs=-ldl"
3697 }
bdf7534a
NF
3698 } else {
3699 if { $shlib_load } {
3700 lappend new_options "libs=-ldl"
3701 }
d8b34041 3702 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
bdf7534a
NF
3703 }
3704 }
695e2681 3705 set options $new_options
57bf0e56 3706
c906108c 3707 if [info exists GDB_TESTCASE_OPTIONS] {
4ec70201 3708 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS"
c906108c
SS
3709 }
3710 verbose "options are $options"
3711 verbose "source is $source $dest $type $options"
3712
f1c47eb2
MS
3713 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
3714
3715 if {[target_info exists needs_status_wrapper] && \
3716 [target_info needs_status_wrapper] != "0" && \
3717 [info exists gdb_wrapper_file]} {
3718 lappend options "libs=${gdb_wrapper_file}"
3719 lappend options "ldflags=${gdb_wrapper_flags}"
3720 }
3721
fc91c6c2
PB
3722 # Replace the "nowarnings" option with the appropriate additional_flags
3723 # to disable compiler warnings.
3724 set nowarnings [lsearch -exact $options nowarnings]
3725 if {$nowarnings != -1} {
3726 if [target_info exists gdb,nowarnings_flag] {
3727 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
3728 } else {
3729 set flag "additional_flags=-w"
3730 }
3731 set options [lreplace $options $nowarnings $nowarnings $flag]
3732 }
3733
968aa7ae
AH
3734 # Replace the "pie" option with the appropriate compiler and linker flags
3735 # to enable PIE executables.
3736 set pie [lsearch -exact $options pie]
3737 if {$pie != -1} {
3738 if [target_info exists gdb,pie_flag] {
3739 set flag "additional_flags=[target_info gdb,pie_flag]"
3740 } else {
3741 # For safety, use fPIE rather than fpie. On AArch64, m68k, PowerPC
3742 # and SPARC, fpie can cause compile errors due to the GOT exceeding
3743 # a maximum size. On other architectures the two flags are
3744 # identical (see the GCC manual). Note Debian9 and Ubuntu16.10
3745 # onwards default GCC to using fPIE. If you do require fpie, then
3746 # it can be set using the pie_flag.
3747 set flag "additional_flags=-fPIE"
3748 }
3749 set options [lreplace $options $pie $pie $flag]
3750
3751 if [target_info exists gdb,pie_ldflag] {
3752 set flag "ldflags=[target_info gdb,pie_ldflag]"
3753 } else {
3754 set flag "ldflags=-pie"
3755 }
3756 lappend options "$flag"
3757 }
3758
3759 # Replace the "nopie" option with the appropriate linker flag to disable
3760 # PIE executables. There are no compiler flags for this option.
6e8b1ab2
JV
3761 set nopie [lsearch -exact $options nopie]
3762 if {$nopie != -1} {
3763 if [target_info exists gdb,nopie_flag] {
3764 set flag "ldflags=[target_info gdb,nopie_flag]"
3765 } else {
3766 set flag "ldflags=-no-pie"
3767 }
3768 set options [lreplace $options $nopie $nopie $flag]
3769 }
3770
f747e0ce
PA
3771 if { $type == "executable" } {
3772 if { ([istarget "*-*-mingw*"]
56643c5e 3773 || [istarget "*-*-*djgpp"]
f747e0ce
PA
3774 || [istarget "*-*-cygwin*"])} {
3775 # Force output to unbuffered mode, by linking in an object file
3776 # with a global contructor that calls setvbuf.
3777 #
3778 # Compile the special object seperatelly for two reasons:
3779 # 1) Insulate it from $options.
3780 # 2) Avoid compiling it for every gdb_compile invocation,
3781 # which is time consuming, especially if we're remote
3782 # host testing.
3783 #
3784 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
3785 verbose "compiling gdb_saved_set_unbuffered_obj"
3786 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
3787 set unbuf_obj ${objdir}/set_unbuffered_mode.o
3788
3789 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
3790 if { $result != "" } {
3791 return $result
3792 }
f6dc277e
YQ
3793 if {[is_remote host]} {
3794 set gdb_saved_set_unbuffered_mode_obj set_unbuffered_mode_saved.o
3795 } else {
3796 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
3797 }
f747e0ce
PA
3798 # Link a copy of the output object, because the
3799 # original may be automatically deleted.
f6dc277e 3800 remote_download host $unbuf_obj $gdb_saved_set_unbuffered_mode_obj
f747e0ce
PA
3801 } else {
3802 verbose "gdb_saved_set_unbuffered_obj already compiled"
3803 }
3804
3805 # Rely on the internal knowledge that the global ctors are ran in
3806 # reverse link order. In that case, we can use ldflags to
3807 # avoid copying the object file to the host multiple
3808 # times.
ace5c364
PM
3809 # This object can only be added if standard libraries are
3810 # used. Thus, we need to disable it if -nostdlib option is used
3811 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
3812 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
3813 }
f747e0ce
PA
3814 }
3815 }
3816
4ec70201 3817 set result [target_compile $source $dest $type $options]
93f02886
DJ
3818
3819 # Prune uninteresting compiler (and linker) output.
3820 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
3821
4ec70201
PA
3822 regsub "\[\r\n\]*$" "$result" "" result
3823 regsub "^\[\r\n\]*" "$result" "" result
ec3c07fc 3824
a80cf5d8
TV
3825 if { $type == "executable" && $result == "" \
3826 && ($nopie != -1 || $pie != -1) } {
3827 set is_pie [exec_is_pie "$dest"]
3828 if { $nopie != -1 && $is_pie == 1 } {
b13057d9 3829 set result "nopie failed to prevent PIE executable"
a80cf5d8
TV
3830 } elseif { $pie != -1 && $is_pie == 0 } {
3831 set result "pie failed to generate PIE executable"
b13057d9
TV
3832 }
3833 }
3834
ec3c07fc
NS
3835 if {[lsearch $options quiet] < 0} {
3836 # We shall update this on a per language basis, to avoid
3837 # changing the entire testsuite in one go.
3838 if {[lsearch $options f77] >= 0} {
3839 gdb_compile_test $source $result
3840 } elseif { $result != "" } {
3841 clone_output "gdb compile failed, $result"
3842 }
c906108c 3843 }
ae59b1da 3844 return $result
c906108c
SS
3845}
3846
b6ff0e81
JB
3847
3848# This is just like gdb_compile, above, except that it tries compiling
3849# against several different thread libraries, to see which one this
3850# system has.
3851proc gdb_compile_pthreads {source dest type options} {
0ae67eb3 3852 set built_binfile 0
b6ff0e81 3853 set why_msg "unrecognized error"
24486cb7 3854 foreach lib {-lpthreads -lpthread -lthread ""} {
b6ff0e81
JB
3855 # This kind of wipes out whatever libs the caller may have
3856 # set. Or maybe theirs will override ours. How infelicitous.
b5ab8ff3 3857 set options_with_lib [concat $options [list libs=$lib quiet]]
b6ff0e81
JB
3858 set ccout [gdb_compile $source $dest $type $options_with_lib]
3859 switch -regexp -- $ccout {
3860 ".*no posix threads support.*" {
3861 set why_msg "missing threads include file"
3862 break
3863 }
3864 ".*cannot open -lpthread.*" {
3865 set why_msg "missing runtime threads library"
3866 }
3867 ".*Can't find library for -lpthread.*" {
3868 set why_msg "missing runtime threads library"
3869 }
3870 {^$} {
3871 pass "successfully compiled posix threads test case"
3872 set built_binfile 1
3873 break
3874 }
3875 }
3876 }
0ae67eb3 3877 if {!$built_binfile} {
bc6c7af4 3878 unsupported "couldn't compile [file tail $source]: ${why_msg}"
b6ff0e81
JB
3879 return -1
3880 }
57bf0e56
DJ
3881}
3882
409d8f48 3883# Build a shared library from SOURCES.
57bf0e56
DJ
3884
3885proc gdb_compile_shlib {sources dest options} {
3886 set obj_options $options
3887
409d8f48
AB
3888 set info_options ""
3889 if { [lsearch -exact $options "c++"] >= 0 } {
3890 set info_options "c++"
3891 }
3892 if [get_compiler_info ${info_options}] {
3893 return -1
3894 }
3895
57bf0e56
DJ
3896 switch -glob [test_compiler_info] {
3897 "xlc-*" {
3898 lappend obj_options "additional_flags=-qpic"
3899 }
ee92b0dd
DE
3900 "clang-*" {
3901 if { !([istarget "*-*-cygwin*"]
3902 || [istarget "*-*-mingw*"]) } {
3903 lappend obj_options "additional_flags=-fpic"
3904 }
3905 }
57bf0e56
DJ
3906 "gcc-*" {
3907 if { !([istarget "powerpc*-*-aix*"]
227c54da
DJ
3908 || [istarget "rs6000*-*-aix*"]
3909 || [istarget "*-*-cygwin*"]
3910 || [istarget "*-*-mingw*"]
3911 || [istarget "*-*-pe*"]) } {
57bf0e56
DJ
3912 lappend obj_options "additional_flags=-fpic"
3913 }
3914 }
9b9b09e9
BH
3915 "icc-*" {
3916 lappend obj_options "additional_flags=-fpic"
3917 }
57bf0e56 3918 default {
3ca22649 3919 # don't know what the compiler is...
57bf0e56
DJ
3920 }
3921 }
3922
3923 set outdir [file dirname $dest]
3924 set objects ""
3925 foreach source $sources {
2ff0a947
TT
3926 set sourcebase [file tail $source]
3927 if {[file extension $source] == ".o"} {
3928 # Already a .o file.
3929 lappend objects $source
3930 } elseif {[gdb_compile $source "${outdir}/${sourcebase}.o" object \
3931 $obj_options] != ""} {
3932 return -1
3933 } else {
3934 lappend objects ${outdir}/${sourcebase}.o
3935 }
57bf0e56
DJ
3936 }
3937
3ca22649
SM
3938 set link_options $options
3939 if [test_compiler_info "xlc-*"] {
3940 lappend link_options "additional_flags=-qmkshrobj"
57bf0e56 3941 } else {
3ca22649
SM
3942 lappend link_options "additional_flags=-shared"
3943
3944 if { ([istarget "*-*-mingw*"]
3945 || [istarget *-*-cygwin*]
3946 || [istarget *-*-pe*]) } {
3947 if { [is_remote host] } {
3948 set name [file tail ${dest}]
3949 } else {
3950 set name ${dest}
3951 }
3952 lappend link_options "additional_flags=-Wl,--out-implib,${name}.a"
6e774b13
SM
3953 } else {
3954 # Set the soname of the library. This causes the linker on ELF
3955 # systems to create the DT_NEEDED entry in the executable referring
3956 # to the soname of the library, and not its absolute path. This
3957 # (using the absolute path) would be problem when testing on a
3958 # remote target.
3959 #
3960 # In conjunction with setting the soname, we add the special
3961 # rpath=$ORIGIN value when building the executable, so that it's
3962 # able to find the library in its own directory.
3ca22649
SM
3963 set destbase [file tail $dest]
3964 lappend link_options "additional_flags=-Wl,-soname,$destbase"
3965 }
3966 }
3967 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
3968 return -1
57bf0e56 3969 }
3ca22649
SM
3970 if { [is_remote host]
3971 && ([istarget "*-*-mingw*"]
3972 || [istarget *-*-cygwin*]
3973 || [istarget *-*-pe*]) } {
3974 set dest_tail_name [file tail ${dest}]
3975 remote_upload host $dest_tail_name.a ${dest}.a
3976 remote_file host delete $dest_tail_name.a
3977 }
3978
3979 return ""
b6ff0e81
JB
3980}
3981
756d88a7
UW
3982# This is just like gdb_compile_shlib, above, except that it tries compiling
3983# against several different thread libraries, to see which one this
3984# system has.
3985proc gdb_compile_shlib_pthreads {sources dest options} {
3986 set built_binfile 0
3987 set why_msg "unrecognized error"
3988 foreach lib {-lpthreads -lpthread -lthread ""} {
3989 # This kind of wipes out whatever libs the caller may have
3990 # set. Or maybe theirs will override ours. How infelicitous.
3991 set options_with_lib [concat $options [list libs=$lib quiet]]
3992 set ccout [gdb_compile_shlib $sources $dest $options_with_lib]
3993 switch -regexp -- $ccout {
3994 ".*no posix threads support.*" {
3995 set why_msg "missing threads include file"
3996 break
3997 }
3998 ".*cannot open -lpthread.*" {
3999 set why_msg "missing runtime threads library"
4000 }
4001 ".*Can't find library for -lpthread.*" {
4002 set why_msg "missing runtime threads library"
4003 }
4004 {^$} {
4005 pass "successfully compiled posix threads test case"
4006 set built_binfile 1
4007 break
4008 }
4009 }
4010 }
4011 if {!$built_binfile} {
bc6c7af4 4012 unsupported "couldn't compile $sources: ${why_msg}"
756d88a7
UW
4013 return -1
4014 }
4015}
4016
130cacce
AF
4017# This is just like gdb_compile_pthreads, above, except that we always add the
4018# objc library for compiling Objective-C programs
4019proc gdb_compile_objc {source dest type options} {
4020 set built_binfile 0
4021 set why_msg "unrecognized error"
4022 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
4023 # This kind of wipes out whatever libs the caller may have
4024 # set. Or maybe theirs will override ours. How infelicitous.
4025 if { $lib == "solaris" } {
4026 set lib "-lpthread -lposix4"
4027 }
4028 if { $lib != "-lobjc" } {
4029 set lib "-lobjc $lib"
4030 }
4031 set options_with_lib [concat $options [list libs=$lib quiet]]
4032 set ccout [gdb_compile $source $dest $type $options_with_lib]
4033 switch -regexp -- $ccout {
4034 ".*no posix threads support.*" {
4035 set why_msg "missing threads include file"
4036 break
4037 }
4038 ".*cannot open -lpthread.*" {
4039 set why_msg "missing runtime threads library"
4040 }
4041 ".*Can't find library for -lpthread.*" {
4042 set why_msg "missing runtime threads library"
4043 }
4044 {^$} {
4045 pass "successfully compiled objc with posix threads test case"
4046 set built_binfile 1
4047 break
4048 }
4049 }
4050 }
4051 if {!$built_binfile} {
bc6c7af4 4052 unsupported "couldn't compile [file tail $source]: ${why_msg}"
130cacce
AF
4053 return -1
4054 }
4055}
4056
f9e2e39d
AH
4057# Send a command to GDB.
4058# For options for TYPE see gdb_stdin_log_write
4059
4060proc send_gdb { string {type standard}} {
4ec70201 4061 global suppress_flag
c906108c 4062 if { $suppress_flag } {
ae59b1da 4063 return "suppressed"
c906108c 4064 }
f9e2e39d 4065 gdb_stdin_log_write $string $type
ae59b1da 4066 return [remote_send host "$string"]
c906108c
SS
4067}
4068
f71c18e7
PA
4069# Send STRING to the inferior's terminal.
4070
4071proc send_inferior { string } {
4072 global inferior_spawn_id
4073
4074 if {[catch "send -i $inferior_spawn_id -- \$string" errorInfo]} {
4075 return "$errorInfo"
4076 } else {
4077 return ""
4078 }
4079}
4080
c906108c
SS
4081#
4082#
4083
4084proc gdb_expect { args } {
4085 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
4ec70201
PA
4086 set atimeout [lindex $args 0]
4087 set expcode [list [lindex $args 1]]
c906108c 4088 } else {
4ec70201 4089 set expcode $args
2f34202f
MR
4090 }
4091
4a40f85a
MR
4092 # A timeout argument takes precedence, otherwise of all the timeouts
4093 # select the largest.
4a40f85a
MR
4094 if [info exists atimeout] {
4095 set tmt $atimeout
4096 } else {
45fd756c 4097 set tmt [get_largest_timeout]
c906108c 4098 }
2f34202f 4099
4ec70201
PA
4100 global suppress_flag
4101 global remote_suppress_flag
c906108c 4102 if [info exists remote_suppress_flag] {
4ec70201 4103 set old_val $remote_suppress_flag
c906108c
SS
4104 }
4105 if [info exists suppress_flag] {
4106 if { $suppress_flag } {
4ec70201 4107 set remote_suppress_flag 1
c906108c
SS
4108 }
4109 }
a0b3c4fd 4110 set code [catch \
4a40f85a 4111 {uplevel remote_expect host $tmt $expcode} string]
c906108c 4112 if [info exists old_val] {
4ec70201 4113 set remote_suppress_flag $old_val
c906108c
SS
4114 } else {
4115 if [info exists remote_suppress_flag] {
4ec70201 4116 unset remote_suppress_flag
c906108c
SS
4117 }
4118 }
4119
4120 if {$code == 1} {
4ec70201 4121 global errorInfo errorCode
c906108c
SS
4122
4123 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
d6d7a51a 4124 } else {
c906108c
SS
4125 return -code $code $string
4126 }
4127}
4128
5fa290c1 4129# gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
085dd6e6
JM
4130#
4131# Check for long sequence of output by parts.
5fa290c1 4132# TEST: is the test message to be printed with the test success/fail.
085dd6e6
JM
4133# SENTINEL: Is the terminal pattern indicating that output has finished.
4134# LIST: is the sequence of outputs to match.
4135# If the sentinel is recognized early, it is considered an error.
4136#
11cf8741
JM
4137# Returns:
4138# 1 if the test failed,
4139# 0 if the test passes,
4140# -1 if there was an internal error.
5fa290c1 4141
c2d11a7d 4142proc gdb_expect_list {test sentinel list} {
085dd6e6 4143 global gdb_prompt
11cf8741 4144 global suppress_flag
085dd6e6 4145 set index 0
43ff13b4 4146 set ok 1
11cf8741
JM
4147 if { $suppress_flag } {
4148 set ok 0
a20ce2c3 4149 unresolved "${test}"
11cf8741 4150 }
43ff13b4 4151 while { ${index} < [llength ${list}] } {
085dd6e6
JM
4152 set pattern [lindex ${list} ${index}]
4153 set index [expr ${index} + 1]
6b0ecdc2 4154 verbose -log "gdb_expect_list pattern: /$pattern/" 2
085dd6e6 4155 if { ${index} == [llength ${list}] } {
43ff13b4
JM
4156 if { ${ok} } {
4157 gdb_expect {
c2d11a7d 4158 -re "${pattern}${sentinel}" {
a20ce2c3 4159 # pass "${test}, pattern ${index} + sentinel"
c2d11a7d
JM
4160 }
4161 -re "${sentinel}" {
a20ce2c3 4162 fail "${test} (pattern ${index} + sentinel)"
c2d11a7d 4163 set ok 0
43ff13b4 4164 }
5c5455dc
AC
4165 -re ".*A problem internal to GDB has been detected" {
4166 fail "${test} (GDB internal error)"
4167 set ok 0
4168 gdb_internal_error_resync
4169 }
43ff13b4 4170 timeout {
a20ce2c3 4171 fail "${test} (pattern ${index} + sentinel) (timeout)"
43ff13b4
JM
4172 set ok 0
4173 }
085dd6e6 4174 }
43ff13b4 4175 } else {
a20ce2c3 4176 # unresolved "${test}, pattern ${index} + sentinel"
085dd6e6
JM
4177 }
4178 } else {
43ff13b4
JM
4179 if { ${ok} } {
4180 gdb_expect {
4181 -re "${pattern}" {
a20ce2c3 4182 # pass "${test}, pattern ${index}"
43ff13b4 4183 }
c2d11a7d 4184 -re "${sentinel}" {
a20ce2c3 4185 fail "${test} (pattern ${index})"
43ff13b4
JM
4186 set ok 0
4187 }
5c5455dc
AC
4188 -re ".*A problem internal to GDB has been detected" {
4189 fail "${test} (GDB internal error)"
4190 set ok 0
4191 gdb_internal_error_resync
4192 }
43ff13b4 4193 timeout {
a20ce2c3 4194 fail "${test} (pattern ${index}) (timeout)"
43ff13b4
JM
4195 set ok 0
4196 }
085dd6e6 4197 }
43ff13b4 4198 } else {
a20ce2c3 4199 # unresolved "${test}, pattern ${index}"
085dd6e6
JM
4200 }
4201 }
4202 }
11cf8741 4203 if { ${ok} } {
a20ce2c3 4204 pass "${test}"
11cf8741
JM
4205 return 0
4206 } else {
4207 return 1
4208 }
085dd6e6
JM
4209}
4210
4211#
4212#
c906108c 4213proc gdb_suppress_entire_file { reason } {
4ec70201 4214 global suppress_flag
c906108c 4215
4ec70201
PA
4216 warning "$reason\n"
4217 set suppress_flag -1
c906108c
SS
4218}
4219
4220#
4221# Set suppress_flag, which will cause all subsequent calls to send_gdb and
4222# gdb_expect to fail immediately (until the next call to
4223# gdb_stop_suppressing_tests).
4224#
4225proc gdb_suppress_tests { args } {
4ec70201 4226 global suppress_flag
c906108c
SS
4227
4228 return; # fnf - disable pending review of results where
4229 # testsuite ran better without this
4ec70201 4230 incr suppress_flag
c906108c
SS
4231
4232 if { $suppress_flag == 1 } {
4233 if { [llength $args] > 0 } {
4ec70201 4234 warning "[lindex $args 0]\n"
c906108c 4235 } else {
4ec70201 4236 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n"
c906108c
SS
4237 }
4238 }
4239}
4240
4241#
4242# Clear suppress_flag.
4243#
4244proc gdb_stop_suppressing_tests { } {
4ec70201 4245 global suppress_flag
c906108c
SS
4246
4247 if [info exists suppress_flag] {
4248 if { $suppress_flag > 0 } {
4ec70201
PA
4249 set suppress_flag 0
4250 clone_output "Tests restarted.\n"
c906108c
SS
4251 }
4252 } else {
4ec70201 4253 set suppress_flag 0
c906108c
SS
4254 }
4255}
4256
4257proc gdb_clear_suppressed { } {
4ec70201 4258 global suppress_flag
c906108c 4259
4ec70201 4260 set suppress_flag 0
c906108c
SS
4261}
4262
94696ad3
PA
4263# Spawn the gdb process.
4264#
4265# This doesn't expect any output or do any other initialization,
4266# leaving those to the caller.
4267#
4268# Overridable function -- you can override this function in your
4269# baseboard file.
4270
4271proc gdb_spawn { } {
4272 default_gdb_spawn
4273}
4274
98880d46
PA
4275# Spawn GDB with CMDLINE_FLAGS appended to the GDBFLAGS global.
4276
4277proc gdb_spawn_with_cmdline_opts { cmdline_flags } {
4278 global GDBFLAGS
4279
4280 set saved_gdbflags $GDBFLAGS
4281
0bbeccb1
PA
4282 if {$GDBFLAGS != ""} {
4283 append GDBFLAGS " "
4284 }
98880d46
PA
4285 append GDBFLAGS $cmdline_flags
4286
4287 set res [gdb_spawn]
4288
4289 set GDBFLAGS $saved_gdbflags
4290
4291 return $res
4292}
4293
94696ad3
PA
4294# Start gdb running, wait for prompt, and disable the pagers.
4295
4296# Overridable function -- you can override this function in your
4297# baseboard file.
4298
c906108c
SS
4299proc gdb_start { } {
4300 default_gdb_start
4301}
4302
4303proc gdb_exit { } {
4304 catch default_gdb_exit
4305}
4306
60b3033e
PA
4307# Return true if we can spawn a program on the target and attach to
4308# it.
4309
4310proc can_spawn_for_attach { } {
2c8c5d37
PA
4311 # We use exp_pid to get the inferior's pid, assuming that gives
4312 # back the pid of the program. On remote boards, that would give
4313 # us instead the PID of e.g., the ssh client, etc.
60b3033e
PA
4314 if [is_remote target] then {
4315 return 0
4316 }
4317
4318 # The "attach" command doesn't make sense when the target is
4319 # stub-like, where GDB finds the program already started on
4320 # initial connection.
4321 if {[target_info exists use_gdb_stub]} {
4322 return 0
4323 }
4324
4325 # Assume yes.
4326 return 1
4327}
4328
2c8c5d37
PA
4329# Kill a progress previously started with spawn_wait_for_attach, and
4330# reap its wait status. PROC_SPAWN_ID is the spawn id associated with
4331# the process.
4332
4333proc kill_wait_spawned_process { proc_spawn_id } {
4334 set pid [exp_pid -i $proc_spawn_id]
4335
4336 verbose -log "killing ${pid}"
4337 remote_exec build "kill -9 ${pid}"
4338
4339 verbose -log "closing ${proc_spawn_id}"
4340 catch "close -i $proc_spawn_id"
4341 verbose -log "waiting for ${proc_spawn_id}"
4342
4343 # If somehow GDB ends up still attached to the process here, a
4344 # blocking wait hangs until gdb is killed (or until gdb / the
4345 # ptracer reaps the exit status too, but that won't happen because
4346 # something went wrong.) Passing -nowait makes expect tell Tcl to
4347 # wait for the PID in the background. That's fine because we
4348 # don't care about the exit status. */
4349 wait -nowait -i $proc_spawn_id
4350}
4351
4352# Returns the process id corresponding to the given spawn id.
4353
4354proc spawn_id_get_pid { spawn_id } {
4355 set testpid [exp_pid -i $spawn_id]
4356
4357 if { [istarget "*-*-cygwin*"] } {
4358 # testpid is the Cygwin PID, GDB uses the Windows PID, which
4359 # might be different due to the way fork/exec works.
4360 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
4361 }
4362
4363 return $testpid
4364}
4365
4c92ff2c 4366# Start a set of programs running and then wait for a bit, to be sure
2c8c5d37
PA
4367# that they can be attached to. Return a list of processes spawn IDs,
4368# one element for each process spawned. It's a test error to call
4369# this when [can_spawn_for_attach] is false.
4c92ff2c
PA
4370
4371proc spawn_wait_for_attach { executable_list } {
2c8c5d37 4372 set spawn_id_list {}
4c92ff2c 4373
60b3033e
PA
4374 if ![can_spawn_for_attach] {
4375 # The caller should have checked can_spawn_for_attach itself
4376 # before getting here.
4377 error "can't spawn for attach with this target/board"
4378 }
4379
4c92ff2c 4380 foreach {executable} $executable_list {
2c8c5d37
PA
4381 # Note we use Expect's spawn, not Tcl's exec, because with
4382 # spawn we control when to wait for/reap the process. That
4383 # allows killing the process by PID without being subject to
4384 # pid-reuse races.
4385 lappend spawn_id_list [remote_spawn target $executable]
4c92ff2c
PA
4386 }
4387
4388 sleep 2
4389
2c8c5d37 4390 return $spawn_id_list
4c92ff2c
PA
4391}
4392
e63b55d1
NS
4393#
4394# gdb_load_cmd -- load a file into the debugger.
4395# ARGS - additional args to load command.
4396# return a -1 if anything goes wrong.
4397#
4398proc gdb_load_cmd { args } {
4399 global gdb_prompt
4400
4401 if [target_info exists gdb_load_timeout] {
4402 set loadtimeout [target_info gdb_load_timeout]
4403 } else {
4404 set loadtimeout 1600
4405 }
4406 send_gdb "load $args\n"
e91528f0 4407 verbose "Timeout is now $loadtimeout seconds" 2
e63b55d1
NS
4408 gdb_expect $loadtimeout {
4409 -re "Loading section\[^\r\]*\r\n" {
4410 exp_continue
4411 }
4412 -re "Start address\[\r\]*\r\n" {
4413 exp_continue
4414 }
4415 -re "Transfer rate\[\r\]*\r\n" {
4416 exp_continue
4417 }
4418 -re "Memory access error\[^\r\]*\r\n" {
4419 perror "Failed to load program"
4420 return -1
4421 }
4422 -re "$gdb_prompt $" {
4423 return 0
4424 }
4425 -re "(.*)\r\n$gdb_prompt " {
4426 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
4427 return -1
4428 }
4429 timeout {
c4b347c7 4430 perror "Timed out trying to load $args."
e63b55d1
NS
4431 return -1
4432 }
4433 }
4434 return -1
4435}
4436
2d338fa9
TT
4437# Invoke "gcore". CORE is the name of the core file to write. TEST
4438# is the name of the test case. This will return 1 if the core file
4439# was created, 0 otherwise. If this fails to make a core file because
4440# this configuration of gdb does not support making core files, it
4441# will call "unsupported", not "fail". However, if this fails to make
4442# a core file for some other reason, then it will call "fail".
4443
4444proc gdb_gcore_cmd {core test} {
4445 global gdb_prompt
4446
4447 set result 0
4448 gdb_test_multiple "gcore $core" $test {
4449 -re "Saved corefile .*\[\r\n\]+$gdb_prompt $" {
4450 pass $test
4451 set result 1
4452 }
bbe769cc 4453 -re "(?:Can't create a corefile|Target does not support core file generation\\.)\[\r\n\]+$gdb_prompt $" {
2d338fa9
TT
4454 unsupported $test
4455 }
4456 }
4457
4458 return $result
4459}
4460
fac51dd9
DE
4461# Load core file CORE. TEST is the name of the test case.
4462# This will record a pass/fail for loading the core file.
4463# Returns:
4464# 1 - core file is successfully loaded
4465# 0 - core file loaded but has a non fatal error
4466# -1 - core file failed to load
4467
4468proc gdb_core_cmd { core test } {
4469 global gdb_prompt
4470
4f424bb1 4471 gdb_test_multiple "core $core" "$test" {
fac51dd9
DE
4472 -re "\\\[Thread debugging using \[^ \r\n\]* enabled\\\]\r\n" {
4473 exp_continue
4474 }
4475 -re " is not a core dump:.*\r\n$gdb_prompt $" {
4f424bb1 4476 fail "$test (bad file format)"
fac51dd9
DE
4477 return -1
4478 }
4479 -re ": No such file or directory.*\r\n$gdb_prompt $" {
4f424bb1 4480 fail "$test (file not found)"
fac51dd9
DE
4481 return -1
4482 }
4483 -re "Couldn't find .* registers in core file.*\r\n$gdb_prompt $" {
4f424bb1 4484 fail "$test (incomplete note section)"
fac51dd9
DE
4485 return 0
4486 }
4487 -re "Core was generated by .*\r\n$gdb_prompt $" {
4f424bb1 4488 pass "$test"
fac51dd9
DE
4489 return 1
4490 }
4491 -re ".*$gdb_prompt $" {
4f424bb1 4492 fail "$test"
fac51dd9
DE
4493 return -1
4494 }
4495 timeout {
4f424bb1 4496 fail "$test (timeout)"
fac51dd9
DE
4497 return -1
4498 }
4499 }
4500 fail "unsupported output from 'core' command"
4501 return -1
4502}
4503
759f0f0b
PA
4504# Return the filename to download to the target and load on the target
4505# for this shared library. Normally just LIBNAME, unless shared libraries
4506# for this target have separate link and load images.
4507
4508proc shlib_target_file { libname } {
4509 return $libname
4510}
4511
4512# Return the filename GDB will load symbols from when debugging this
4513# shared library. Normally just LIBNAME, unless shared libraries for
4514# this target have separate link and load images.
4515
4516proc shlib_symbol_file { libname } {
4517 return $libname
4518}
4519
56744f0a
JJ
4520# Return the filename to download to the target and load for this
4521# executable. Normally just BINFILE unless it is renamed to something
4522# else for this target.
4523
4524proc exec_target_file { binfile } {
4525 return $binfile
4526}
4527
4528# Return the filename GDB will load symbols from when debugging this
4529# executable. Normally just BINFILE unless executables for this target
4530# have separate files for symbols.
4531
4532proc exec_symbol_file { binfile } {
4533 return $binfile
4534}
4535
4536# Rename the executable file. Normally this is just BINFILE1 being renamed
4537# to BINFILE2, but some targets require multiple binary files.
4538proc gdb_rename_execfile { binfile1 binfile2 } {
faf067f1
JK
4539 file rename -force [exec_target_file ${binfile1}] \
4540 [exec_target_file ${binfile2}]
56744f0a 4541 if { [exec_target_file ${binfile1}] != [exec_symbol_file ${binfile1}] } {
faf067f1
JK
4542 file rename -force [exec_symbol_file ${binfile1}] \
4543 [exec_symbol_file ${binfile2}]
56744f0a
JJ
4544 }
4545}
4546
4547# "Touch" the executable file to update the date. Normally this is just
4548# BINFILE, but some targets require multiple files.
4549proc gdb_touch_execfile { binfile } {
faf067f1
JK
4550 set time [clock seconds]
4551 file mtime [exec_target_file ${binfile}] $time
56744f0a 4552 if { [exec_target_file ${binfile}] != [exec_symbol_file ${binfile}] } {
faf067f1 4553 file mtime [exec_symbol_file ${binfile}] $time
56744f0a
JJ
4554 }
4555}
4556
7817ea46
SM
4557# Like remote_download but provides a gdb-specific behavior.
4558#
4559# If the destination board is remote, the local file FROMFILE is transferred as
4560# usual with remote_download to TOFILE on the remote board. The destination
4561# filename is added to the CLEANFILES global, so it can be cleaned up at the
4562# end of the test.
4563#
4564# If the destination board is local, the destination path TOFILE is passed
4565# through standard_output_file, and FROMFILE is copied there.
4566#
4567# In both cases, if TOFILE is omitted, it defaults to the [file tail] of
4568# FROMFILE.
44ee8174
TT
4569
4570proc gdb_remote_download {dest fromfile {tofile {}}} {
7817ea46
SM
4571 # If TOFILE is not given, default to the same filename as FROMFILE.
4572 if {[string length $tofile] == 0} {
4573 set tofile [file tail $fromfile]
44ee8174 4574 }
ce4ea2bb 4575
7817ea46
SM
4576 if {[is_remote $dest]} {
4577 # When the DEST is remote, we simply send the file to DEST.
4578 global cleanfiles
44ee8174 4579
7817ea46
SM
4580 set destname [remote_download $dest $fromfile $tofile]
4581 lappend cleanfiles $destname
93f02886 4582
7817ea46
SM
4583 return $destname
4584 } else {
8392fa22
SM
4585 # When the DEST is local, we copy the file to the test directory (where
4586 # the executable is).
4587 #
4588 # Note that we pass TOFILE through standard_output_file, regardless of
4589 # whether it is absolute or relative, because we don't want the tests
4590 # to be able to write outside their standard output directory.
4591
7817ea46 4592 set tofile [standard_output_file $tofile]
93f02886 4593
7817ea46
SM
4594 file copy -force $fromfile $tofile
4595
4596 return $tofile
4597 }
93f02886
DJ
4598}
4599
d9019901 4600# gdb_load_shlib LIB...
93f02886 4601#
fca4cfd9 4602# Copy the listed library to the target.
93f02886 4603
d9019901 4604proc gdb_load_shlib { file } {
c708f4d2
AB
4605 global gdb_spawn_id
4606
4607 if ![info exists gdb_spawn_id] {
4608 perror "gdb_load_shlib: GDB is not running"
4609 }
4610
fca4cfd9 4611 set dest [gdb_remote_download target [shlib_target_file $file]]
93f02886 4612
6e774b13
SM
4613 if {[is_remote target]} {
4614 # If the target is remote, we need to tell gdb where to find the
4615 # libraries.
4616 #
4617 # We could set this even when not testing remotely, but a user
4618 # generally won't set it unless necessary. In order to make the tests
4619 # more like the real-life scenarios, we don't set it for local testing.
fca4cfd9 4620 gdb_test "set solib-search-path [file dirname $file]" "" ""
6e774b13 4621 }
fca4cfd9
SM
4622
4623 return $dest
93f02886
DJ
4624}
4625
c906108c 4626#
5b80f00d
PA
4627# gdb_load -- load a file into the debugger. Specifying no file
4628# defaults to the executable currently being debugged.
7e60a48e 4629# The return value is 0 for success, -1 for failure.
2db8e78e 4630# Many files in config/*.exp override this procedure.
c906108c
SS
4631#
4632proc gdb_load { arg } {
5b80f00d
PA
4633 if { $arg != "" } {
4634 return [gdb_file_cmd $arg]
4635 }
7e60a48e 4636 return 0
c906108c
SS
4637}
4638
b741e217
DJ
4639# gdb_reload -- load a file into the target. Called before "running",
4640# either the first time or after already starting the program once,
4641# for remote targets. Most files that override gdb_load should now
4642# override this instead.
4643
4644proc gdb_reload { } {
4645 # For the benefit of existing configurations, default to gdb_load.
4646 # Specifying no file defaults to the executable currently being
4647 # debugged.
4648 return [gdb_load ""]
4649}
4650
c906108c
SS
4651proc gdb_continue { function } {
4652 global decimal
4653
ae59b1da 4654 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"]
c906108c
SS
4655}
4656
73c9764f 4657proc default_gdb_init { test_file_name } {
277254ba 4658 global gdb_wrapper_initialized
f6838f81 4659 global gdb_wrapper_target
0a6d0306 4660 global gdb_test_file_name
93f02886 4661 global cleanfiles
73c9764f 4662 global pf_prefix
277254ba 4663
93f02886
DJ
4664 set cleanfiles {}
4665
4ec70201 4666 gdb_clear_suppressed
c906108c 4667
73c9764f 4668 set gdb_test_file_name [file rootname [file tail $test_file_name]]
0a6d0306 4669
277254ba
MS
4670 # Make sure that the wrapper is rebuilt
4671 # with the appropriate multilib option.
f6838f81
DJ
4672 if { $gdb_wrapper_target != [current_target_name] } {
4673 set gdb_wrapper_initialized 0
4674 }
277254ba 4675
7b433602
JB
4676 # Unlike most tests, we have a small number of tests that generate
4677 # a very large amount of output. We therefore increase the expect
ff604a67
MR
4678 # buffer size to be able to contain the entire test output. This
4679 # is especially needed by gdb.base/info-macros.exp.
4680 match_max -d 65536
8d417781
PM
4681 # Also set this value for the currently running GDB.
4682 match_max [match_max -d]
c906108c
SS
4683
4684 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
73c9764f 4685 set pf_prefix "[file tail [file dirname $test_file_name]]/[file tail $test_file_name]:"
c906108c 4686
4ec70201 4687 global gdb_prompt
c906108c 4688 if [target_info exists gdb_prompt] {
4ec70201 4689 set gdb_prompt [target_info gdb_prompt]
c906108c
SS
4690 } else {
4691 set gdb_prompt "\\(gdb\\)"
4692 }
e11ac3a3
JK
4693 global use_gdb_stub
4694 if [info exists use_gdb_stub] {
4695 unset use_gdb_stub
4696 }
c906108c
SS
4697}
4698
3d338901
DE
4699# Return a path using GDB_PARALLEL.
4700# ARGS is a list of path elements to append to "$objdir/$GDB_PARALLEL".
4701# GDB_PARALLEL must be defined, the caller must check.
4702#
4703# The default value for GDB_PARALLEL is, canonically, ".".
4704# The catch is that tests don't expect an additional "./" in file paths so
4705# omit any directory for the default case.
4706# GDB_PARALLEL is written as "yes" for the default case in Makefile.in to mark
4707# its special handling.
4708
4709proc make_gdb_parallel_path { args } {
4710 global GDB_PARALLEL objdir
4711 set joiner [list "file" "join" $objdir]
2151ccc5 4712 if { [info exists GDB_PARALLEL] && $GDB_PARALLEL != "yes" } {
3d338901
DE
4713 lappend joiner $GDB_PARALLEL
4714 }
4715 set joiner [concat $joiner $args]
4716 return [eval $joiner]
4717}
4718
0a6d0306 4719# Turn BASENAME into a full file name in the standard output
8a3e1f8d
TT
4720# directory. It is ok if BASENAME is the empty string; in this case
4721# the directory is returned.
0a6d0306
TT
4722
4723proc standard_output_file {basename} {
2151ccc5 4724 global objdir subdir gdb_test_file_name
0a6d0306 4725
2151ccc5
SM
4726 set dir [make_gdb_parallel_path outputs $subdir $gdb_test_file_name]
4727 file mkdir $dir
97dd8e07
CB
4728 # If running on MinGW, replace /c/foo with c:/foo
4729 if { [ishost *-*-mingw*] } {
4730 set dir [regsub {^/([a-z])/} $dir {\1:/}]
4731 }
2151ccc5 4732 return [file join $dir $basename]
0a6d0306
TT
4733}
4734
f9e2e39d
AH
4735# Turn BASENAME into a full file name in the standard output directory. If
4736# GDB has been launched more than once then append the count, starting with
4737# a ".1" postfix.
4738
4739proc standard_output_file_with_gdb_instance {basename} {
4740 global gdb_instances
4741 set count [expr $gdb_instances - 1 ]
4742
4743 if {$count == 0} {
4744 return [standard_output_file $basename]
4745 }
4746 return [standard_output_file ${basename}.${count}]
4747}
4748
4e234898
TT
4749# Return the name of a file in our standard temporary directory.
4750
4751proc standard_temp_file {basename} {
c4ef31bf
SM
4752 # Since a particular runtest invocation is only executing a single test
4753 # file at any given time, we can use the runtest pid to build the
4754 # path of the temp directory.
4755 set dir [make_gdb_parallel_path temp [pid]]
4756 file mkdir $dir
4757 return [file join $dir $basename]
4e234898
TT
4758}
4759
0a6d0306
TT
4760# Set 'testfile', 'srcfile', and 'binfile'.
4761#
4762# ARGS is a list of source file specifications.
4763# Without any arguments, the .exp file's base name is used to
4764# compute the source file name. The ".c" extension is added in this case.
4765# If ARGS is not empty, each entry is a source file specification.
4766# If the specification starts with a ".", it is treated as a suffix
4767# to append to the .exp file's base name.
4768# If the specification is the empty string, it is treated as if it
4769# were ".c".
4770# Otherwise it is a file name.
4771# The first file in the list is used to set the 'srcfile' global.
4772# Each subsequent name is used to set 'srcfile2', 'srcfile3', etc.
4773#
4774# Most tests should call this without arguments.
4775#
4776# If a completely different binary file name is needed, then it
4777# should be handled in the .exp file with a suitable comment.
4778
4779proc standard_testfile {args} {
4780 global gdb_test_file_name
93c0ef37 4781 global subdir
686f09d0 4782 global gdb_test_file_last_vars
0a6d0306
TT
4783
4784 # Outputs.
4785 global testfile binfile
4786
4787 set testfile $gdb_test_file_name
4788 set binfile [standard_output_file ${testfile}]
4789
4790 if {[llength $args] == 0} {
4791 set args .c
4792 }
4793
686f09d0
TT
4794 # Unset our previous output variables.
4795 # This can help catch hidden bugs.
4796 if {[info exists gdb_test_file_last_vars]} {
4797 foreach varname $gdb_test_file_last_vars {
4798 global $varname
4799 catch {unset $varname}
4800 }
4801 }
4802 # 'executable' is often set by tests.
4803 set gdb_test_file_last_vars {executable}
4804
0a6d0306
TT
4805 set suffix ""
4806 foreach arg $args {
4807 set varname srcfile$suffix
4808 global $varname
4809
4810 # Handle an extension.
4811 if {$arg == ""} {
4812 set arg $testfile.c
4813 } elseif {[string range $arg 0 0] == "."} {
4814 set arg $testfile$arg
4815 }
4816
4817 set $varname $arg
686f09d0 4818 lappend gdb_test_file_last_vars $varname
0a6d0306
TT
4819
4820 if {$suffix == ""} {
4821 set suffix 2
4822 } else {
4823 incr suffix
4824 }
4825 }
4826}
4827
7b356089
JB
4828# The default timeout used when testing GDB commands. We want to use
4829# the same timeout as the default dejagnu timeout, unless the user has
4830# already provided a specific value (probably through a site.exp file).
4831global gdb_test_timeout
4832if ![info exists gdb_test_timeout] {
4833 set gdb_test_timeout $timeout
4834}
4835
47050449
JB
4836# A list of global variables that GDB testcases should not use.
4837# We try to prevent their use by monitoring write accesses and raising
4838# an error when that happens.
4839set banned_variables { bug_id prms_id }
4840
abcc4978
PA
4841# A list of procedures that GDB testcases should not use.
4842# We try to prevent their use by monitoring invocations and raising
4843# an error when that happens.
4844set banned_procedures { strace }
4845
41b2c92d
PM
4846# gdb_init is called by runtest at start, but also by several
4847# tests directly; gdb_finish is only called from within runtest after
4848# each test source execution.
4849# Placing several traces by repetitive calls to gdb_init leads
4850# to problems, as only one trace is removed in gdb_finish.
4851# To overcome this possible problem, we add a variable that records
abcc4978
PA
4852# if the banned variables and procedures are already traced.
4853set banned_traced 0
41b2c92d 4854
73c9764f 4855proc gdb_init { test_file_name } {
7b356089
JB
4856 # Reset the timeout value to the default. This way, any testcase
4857 # that changes the timeout value without resetting it cannot affect
4858 # the timeout used in subsequent testcases.
4859 global gdb_test_timeout
4860 global timeout
4861 set timeout $gdb_test_timeout
4862
8b696e31
YQ
4863 if { [regexp ".*gdb\.reverse\/.*" $test_file_name]
4864 && [target_info exists gdb_reverse_timeout] } {
4865 set timeout [target_info gdb_reverse_timeout]
4866 }
4867
5e92f71a
TT
4868 # If GDB_INOTIFY is given, check for writes to '.'. This is a
4869 # debugging tool to help confirm that the test suite is
4870 # parallel-safe. You need "inotifywait" from the
4871 # inotify-tools package to use this.
4872 global GDB_INOTIFY inotify_pid
4873 if {[info exists GDB_INOTIFY] && ![info exists inotify_pid]} {
4874 global outdir tool inotify_log_file
4875
4876 set exclusions {outputs temp gdb[.](log|sum) cache}
4877 set exclusion_re ([join $exclusions |])
4878
4879 set inotify_log_file [standard_temp_file inotify.out]
4880 set inotify_pid [exec inotifywait -r -m -e move,create,delete . \
4881 --exclude $exclusion_re \
4882 |& tee -a $outdir/$tool.log $inotify_log_file &]
4883
4884 # Wait for the watches; hopefully this is long enough.
4885 sleep 2
4886
4887 # Clear the log so that we don't emit a warning the first time
4888 # we check it.
4889 set fd [open $inotify_log_file w]
4890 close $fd
4891 }
4892
abcc4978
PA
4893 # Block writes to all banned variables, and invocation of all
4894 # banned procedures...
47050449 4895 global banned_variables
abcc4978
PA
4896 global banned_procedures
4897 global banned_traced
4898 if (!$banned_traced) {
41b2c92d
PM
4899 foreach banned_var $banned_variables {
4900 global "$banned_var"
4901 trace add variable "$banned_var" write error
4902 }
abcc4978
PA
4903 foreach banned_proc $banned_procedures {
4904 global "$banned_proc"
4905 trace add execution "$banned_proc" enter error
4906 }
4907 set banned_traced 1
47050449
JB
4908 }
4909
e7ab5e63
AB
4910 # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same
4911 # messages as expected.
c6f2ac43 4912 setenv LC_ALL C
e7ab5e63 4913 setenv LC_CTYPE C
c6f2ac43
PA
4914 setenv LANG C
4915
e7ab5e63
AB
4916 # Don't let a .inputrc file or an existing setting of INPUTRC mess up
4917 # the test results. Even if /dev/null doesn't exist on the particular
4918 # platform, the readline library will use the default setting just by
4919 # failing to open the file. OTOH, opening /dev/null successfully will
4920 # also result in the default settings being used since nothing will be
4921 # read from this file.
4922 setenv INPUTRC "/dev/null"
4923
9162a27c
TT
4924 # This disables style output, which would interfere with many
4925 # tests.
4926 setenv TERM "dumb"
e7ab5e63 4927
bd447abb
SM
4928 # Initialize GDB's pty with a fixed size, to make sure we avoid pagination
4929 # during startup. See "man expect" for details about stty_init.
4930 global stty_init
4931 set stty_init "rows 25 cols 80"
4932
e7ab5e63 4933 # Some tests (for example gdb.base/maint.exp) shell out from gdb to use
e4b8388f 4934 # grep. Clear GREP_OPTIONS to make the behavior predictable,
e7ab5e63
AB
4935 # especially having color output turned on can cause tests to fail.
4936 setenv GREP_OPTIONS ""
4937
03f2bd59
JK
4938 # Clear $gdbserver_reconnect_p.
4939 global gdbserver_reconnect_p
4940 set gdbserver_reconnect_p 1
4941 unset gdbserver_reconnect_p
4942
f9e2e39d
AH
4943 # Reset GDB number of instances
4944 global gdb_instances
4945 set gdb_instances 0
4946
73c9764f 4947 return [default_gdb_init $test_file_name]
c906108c
SS
4948}
4949
4950proc gdb_finish { } {
a35cfb40
MR
4951 global gdbserver_reconnect_p
4952 global gdb_prompt
93f02886
DJ
4953 global cleanfiles
4954
4955 # Exit first, so that the files are no longer in use.
4956 gdb_exit
4957
4958 if { [llength $cleanfiles] > 0 } {
4959 eval remote_file target delete $cleanfiles
4960 set cleanfiles {}
4961 }
47050449
JB
4962
4963 # Unblock write access to the banned variables. Dejagnu typically
4964 # resets some of them between testcases.
4965 global banned_variables
abcc4978
PA
4966 global banned_procedures
4967 global banned_traced
4968 if ($banned_traced) {
41b2c92d
PM
4969 foreach banned_var $banned_variables {
4970 global "$banned_var"
4971 trace remove variable "$banned_var" write error
4972 }
abcc4978
PA
4973 foreach banned_proc $banned_procedures {
4974 global "$banned_proc"
4975 trace remove execution "$banned_proc" enter error
4976 }
4977 set banned_traced 0
47050449 4978 }
c906108c
SS
4979}
4980
4981global debug_format
7a292a7a 4982set debug_format "unknown"
c906108c
SS
4983
4984# Run the gdb command "info source" and extract the debugging format
4985# information from the output and save it in debug_format.
4986
4987proc get_debug_format { } {
4988 global gdb_prompt
4989 global verbose
4990 global expect_out
4991 global debug_format
4992
4993 set debug_format "unknown"
4994 send_gdb "info source\n"
4995 gdb_expect 10 {
919d772c 4996 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
c906108c
SS
4997 set debug_format $expect_out(1,string)
4998 verbose "debug format is $debug_format"
ae59b1da 4999 return 1
c906108c
SS
5000 }
5001 -re "No current source file.\r\n$gdb_prompt $" {
5002 perror "get_debug_format used when no current source file"
ae59b1da 5003 return 0
c906108c
SS
5004 }
5005 -re "$gdb_prompt $" {
5006 warning "couldn't check debug format (no valid response)."
ae59b1da 5007 return 1
c906108c
SS
5008 }
5009 timeout {
975531db 5010 warning "couldn't check debug format (timeout)."
ae59b1da 5011 return 1
c906108c
SS
5012 }
5013 }
5014}
5015
838ae6c4
JB
5016# Return true if FORMAT matches the debug format the current test was
5017# compiled with. FORMAT is a shell-style globbing pattern; it can use
5018# `*', `[...]', and so on.
5019#
5020# This function depends on variables set by `get_debug_format', above.
5021
5022proc test_debug_format {format} {
5023 global debug_format
5024
5025 return [expr [string match $format $debug_format] != 0]
5026}
5027
c906108c
SS
5028# Like setup_xfail, but takes the name of a debug format (DWARF 1,
5029# COFF, stabs, etc). If that format matches the format that the
5030# current test was compiled with, then the next test is expected to
5031# fail for any target. Returns 1 if the next test or set of tests is
5032# expected to fail, 0 otherwise (or if it is unknown). Must have
5033# previously called get_debug_format.
b55a4771 5034proc setup_xfail_format { format } {
4ec70201 5035 set ret [test_debug_format $format]
b55a4771 5036
838ae6c4 5037 if {$ret} then {
b55a4771
MS
5038 setup_xfail "*-*-*"
5039 }
ae59b1da 5040 return $ret
b55a4771 5041}
c906108c 5042
c6fee705
MC
5043# gdb_get_line_number TEXT [FILE]
5044#
5045# Search the source file FILE, and return the line number of the
0d7941a9 5046# first line containing TEXT. If no match is found, an error is thrown.
c6fee705
MC
5047#
5048# TEXT is a string literal, not a regular expression.
5049#
5050# The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
5051# specified, and does not start with "/", then it is assumed to be in
5052# "$srcdir/$subdir". This is awkward, and can be fixed in the future,
5053# by changing the callers and the interface at the same time.
5054# In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
5055# gdb.base/ena-dis-br.exp.
5056#
5057# Use this function to keep your test scripts independent of the
5058# exact line numbering of the source file. Don't write:
5059#
5060# send_gdb "break 20"
5061#
5062# This means that if anyone ever edits your test's source file,
5063# your test could break. Instead, put a comment like this on the
5064# source file line you want to break at:
5065#
5066# /* breakpoint spot: frotz.exp: test name */
5067#
5068# and then write, in your test script (which we assume is named
5069# frotz.exp):
5070#
5071# send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
5072#
5073# (Yes, Tcl knows how to handle the nested quotes and brackets.
5074# Try this:
5075# $ tclsh
5076# % puts "foo [lindex "bar baz" 1]"
5077# foo baz
5078# %
5079# Tcl is quite clever, for a little stringy language.)
5080#
5081# ===
5082#
5083# The previous implementation of this procedure used the gdb search command.
5084# This version is different:
5085#
5086# . It works with MI, and it also works when gdb is not running.
5087#
5088# . It operates on the build machine, not the host machine.
5089#
5090# . For now, this implementation fakes a current directory of
5091# $srcdir/$subdir to be compatible with the old implementation.
5092# This will go away eventually and some callers will need to
5093# be changed.
5094#
5095# . The TEXT argument is literal text and matches literally,
5096# not a regular expression as it was before.
5097#
5098# . State changes in gdb, such as changing the current file
5099# and setting $_, no longer happen.
5100#
5101# After a bit of time we can forget about the differences from the
5102# old implementation.
5103#
5104# --chastain 2004-08-05
5105
5106proc gdb_get_line_number { text { file "" } } {
5107 global srcdir
5108 global subdir
5109 global srcfile
c906108c 5110
c6fee705
MC
5111 if { "$file" == "" } then {
5112 set file "$srcfile"
5113 }
5114 if { ! [regexp "^/" "$file"] } then {
5115 set file "$srcdir/$subdir/$file"
c906108c
SS
5116 }
5117
c6fee705 5118 if { [ catch { set fd [open "$file"] } message ] } then {
0d7941a9 5119 error "$message"
c906108c 5120 }
c6fee705
MC
5121
5122 set found -1
5123 for { set line 1 } { 1 } { incr line } {
5124 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
0d7941a9 5125 error "$message"
c6fee705
MC
5126 }
5127 if { $nchar < 0 } then {
5128 break
5129 }
5130 if { [string first "$text" "$body"] >= 0 } then {
5131 set found $line
5132 break
5133 }
5134 }
5135
5136 if { [ catch { close "$fd" } message ] } then {
0d7941a9
KS
5137 error "$message"
5138 }
5139
5140 if {$found == -1} {
5141 error "undefined tag \"$text\""
c6fee705
MC
5142 }
5143
5144 return $found
c906108c
SS
5145}
5146
b477a5e6
PA
5147# Continue the program until it ends.
5148#
fda326dd
TT
5149# MSSG is the error message that gets printed. If not given, a
5150# default is used.
5151# COMMAND is the command to invoke. If not given, "continue" is
5152# used.
eceb0c5f
TT
5153# ALLOW_EXTRA is a flag indicating whether the test should expect
5154# extra output between the "Continuing." line and the program
5155# exiting. By default it is zero; if nonzero, any extra output
5156# is accepted.
fda326dd 5157
eceb0c5f 5158proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} {
e11ac3a3 5159 global inferior_exited_re use_gdb_stub
7a292a7a 5160
fda326dd
TT
5161 if {$mssg == ""} {
5162 set text "continue until exit"
5163 } else {
5164 set text "continue until exit at $mssg"
5165 }
eceb0c5f
TT
5166 if {$allow_extra} {
5167 set extra ".*"
5168 } else {
5169 set extra ""
5170 }
b477a5e6
PA
5171
5172 # By default, we don't rely on exit() behavior of remote stubs --
5173 # it's common for exit() to be implemented as a simple infinite
5174 # loop, or a forced crash/reset. For native targets, by default, we
5175 # assume process exit is reported as such. If a non-reliable target
5176 # is used, we set a breakpoint at exit, and continue to that.
5177 if { [target_info exists exit_is_reliable] } {
5178 set exit_is_reliable [target_info exit_is_reliable]
5179 } else {
5180 set exit_is_reliable [expr ! $use_gdb_stub]
5181 }
5182
5183 if { ! $exit_is_reliable } {
7a292a7a
SS
5184 if {![gdb_breakpoint "exit"]} {
5185 return 0
5186 }
eceb0c5f 5187 gdb_test $command "Continuing..*Breakpoint .*exit.*" \
fda326dd 5188 $text
7a292a7a
SS
5189 } else {
5190 # Continue until we exit. Should not stop again.
5191 # Don't bother to check the output of the program, that may be
5192 # extremely tough for some remote systems.
eceb0c5f
TT
5193 gdb_test $command \
5194 "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\
fda326dd 5195 $text
7a292a7a
SS
5196 }
5197}
5198
5199proc rerun_to_main {} {
e11ac3a3 5200 global gdb_prompt use_gdb_stub
7a292a7a 5201
e11ac3a3 5202 if $use_gdb_stub {
7a292a7a
SS
5203 gdb_run_cmd
5204 gdb_expect {
5205 -re ".*Breakpoint .*main .*$gdb_prompt $"\
5206 {pass "rerun to main" ; return 0}
5207 -re "$gdb_prompt $"\
5208 {fail "rerun to main" ; return 0}
5209 timeout {fail "(timeout) rerun to main" ; return 0}
5210 }
5211 } else {
5212 send_gdb "run\n"
5213 gdb_expect {
11350d2a 5214 -re "The program .* has been started already.*y or n. $" {
f9e2e39d 5215 send_gdb "y\n" answer
11350d2a
CV
5216 exp_continue
5217 }
7a292a7a
SS
5218 -re "Starting program.*$gdb_prompt $"\
5219 {pass "rerun to main" ; return 0}
5220 -re "$gdb_prompt $"\
5221 {fail "rerun to main" ; return 0}
5222 timeout {fail "(timeout) rerun to main" ; return 0}
5223 }
5224 }
5225}
c906108c 5226
5a56d6a6
TV
5227# Return true if EXECUTABLE contains a .gdb_index or .debug_names index section.
5228
5229proc exec_has_index_section { executable } {
5230 set readelf_program [gdb_find_readelf]
5231 set res [catch {exec $readelf_program -S $executable \
5232 | grep -E "\.gdb_index|\.debug_names" }]
5233 if { $res == 0 } {
5234 return 1
5235 }
5236 return 0
5237}
5238
a80cf5d8
TV
5239# Return list with major and minor version of readelf, or an empty list.
5240gdb_caching_proc readelf_version {
5241 set readelf_program [gdb_find_readelf]
5242 set res [catch {exec $readelf_program --version} output]
5243 if { $res != 0 } {
5244 return [list]
5245 }
5246 set lines [split $output \n]
5247 set line [lindex $lines 0]
5248 set res [regexp {[ \t]+([0-9]+)[.]([0-9]+)[^ \t]*$} \
5249 $line dummy major minor]
5250 if { $res != 1 } {
5251 return [list]
5252 }
5253 return [list $major $minor]
5254}
5255
5256# Return 1 if readelf prints the PIE flag, 0 if is doesn't, and -1 if unknown.
5257proc readelf_prints_pie { } {
5258 set version [readelf_version]
5259 if { [llength $version] == 0 } {
5260 return -1
5261 }
5262 set major [lindex $version 0]
5263 set minor [lindex $version 1]
5264 # It would be better to construct a PIE executable and test if the PIE
5265 # flag is printed by readelf, but we cannot reliably construct a PIE
5266 # executable if the multilib_flags dictate otherwise
5267 # (--target_board=unix/-no-pie/-fno-PIE).
5268 return [version_at_least $major $minor 2 26]
5269}
5270
5271# Return 1 if EXECUTABLE is a Position Independent Executable, 0 if it is not,
5272# and -1 if unknown.
b13057d9
TV
5273
5274proc exec_is_pie { executable } {
a80cf5d8
TV
5275 set res [readelf_prints_pie]
5276 if { $res != 1 } {
5277 return -1
5278 }
b13057d9 5279 set readelf_program [gdb_find_readelf]
a80cf5d8
TV
5280 set res [catch {exec $readelf_program -d $executable} output]
5281 if { $res != 0 } {
5282 return -1
5283 }
5284 set res [regexp -line {\(FLAGS_1\).*Flags:.* PIE($| )} $output]
5285 if { $res == 1 } {
b13057d9
TV
5286 return 1
5287 }
5288 return 0
5289}
5290
27aba047
YQ
5291# Return true if a test should be skipped due to lack of floating
5292# point support or GDB can't fetch the contents from floating point
5293# registers.
13a5e3b8 5294
27aba047 5295gdb_caching_proc gdb_skip_float_test {
13a5e3b8 5296 if [target_info exists gdb,skip_float_tests] {
ae59b1da 5297 return 1
13a5e3b8 5298 }
27aba047
YQ
5299
5300 # There is an ARM kernel ptrace bug that hardware VFP registers
5301 # are not updated after GDB ptrace set VFP registers. The bug
5302 # was introduced by kernel commit 8130b9d7b9d858aa04ce67805e8951e3cb6e9b2f
5303 # in 2012 and is fixed in e2dfb4b880146bfd4b6aa8e138c0205407cebbaf
5304 # in May 2016. In other words, kernels older than 4.6.3, 4.4.14,
5305 # 4.1.27, 3.18.36, and 3.14.73 have this bug.
5306 # This kernel bug is detected by check how does GDB change the
5307 # program result by changing one VFP register.
5308 if { [istarget "arm*-*-linux*"] } {
5309
5310 set compile_flags {debug nowarnings }
5311
5312 # Set up, compile, and execute a test program having VFP
5313 # operations.
5314 set src [standard_temp_file arm_vfp[pid].c]
5315 set exe [standard_temp_file arm_vfp[pid].x]
5316
5317 gdb_produce_source $src {
5318 int main() {
5319 double d = 4.0;
5320 int ret;
5321
5322 asm ("vldr d0, [%0]" : : "r" (&d));
5323 asm ("vldr d1, [%0]" : : "r" (&d));
5324 asm (".global break_here\n"
5325 "break_here:");
5326 asm ("vcmp.f64 d0, d1\n"
5327 "vmrs APSR_nzcv, fpscr\n"
5328 "bne L_value_different\n"
5329 "movs %0, #0\n"
5330 "b L_end\n"
5331 "L_value_different:\n"
5332 "movs %0, #1\n"
5333 "L_end:\n" : "=r" (ret) :);
5334
5335 /* Return $d0 != $d1. */
5336 return ret;
5337 }
5338 }
5339
5340 verbose "compiling testfile $src" 2
5341 set lines [gdb_compile $src $exe executable $compile_flags]
5342 file delete $src
5343
5344 if ![string match "" $lines] then {
5345 verbose "testfile compilation failed, returning 1" 2
5346 return 0
5347 }
5348
5349 # No error message, compilation succeeded so now run it via gdb.
5350 # Run the test up to 5 times to detect whether ptrace can
5351 # correctly update VFP registers or not.
5352 set skip_vfp_test 0
5353 for {set i 0} {$i < 5} {incr i} {
5354 global gdb_prompt srcdir subdir
5355
5356 gdb_exit
5357 gdb_start
5358 gdb_reinitialize_dir $srcdir/$subdir
5359 gdb_load "$exe"
5360
5361 runto_main
5362 gdb_test "break *break_here"
5363 gdb_continue_to_breakpoint "break_here"
5364
5365 # Modify $d0 to a different value, so the exit code should
5366 # be 1.
5367 gdb_test "set \$d0 = 5.0"
5368
5369 set test "continue to exit"
5370 gdb_test_multiple "continue" "$test" {
5371 -re "exited with code 01.*$gdb_prompt $" {
5372 }
5373 -re "exited normally.*$gdb_prompt $" {
5374 # However, the exit code is 0. That means something
5375 # wrong in setting VFP registers.
5376 set skip_vfp_test 1
5377 break
5378 }
5379 }
5380 }
5381
5382 gdb_exit
5383 remote_file build delete $exe
5384
5385 return $skip_vfp_test
5386 }
ae59b1da 5387 return 0
13a5e3b8
MS
5388}
5389
5390# Print a message and return true if a test should be skipped
5391# due to lack of stdio support.
5392
5393proc gdb_skip_stdio_test { msg } {
5394 if [target_info exists gdb,noinferiorio] {
4ec70201 5395 verbose "Skipping test '$msg': no inferior i/o."
ae59b1da 5396 return 1
13a5e3b8 5397 }
ae59b1da 5398 return 0
13a5e3b8
MS
5399}
5400
5401proc gdb_skip_bogus_test { msg } {
ae59b1da 5402 return 0
13a5e3b8
MS
5403}
5404
e515b470
DJ
5405# Return true if a test should be skipped due to lack of XML support
5406# in the host GDB.
d0ef5df8 5407# NOTE: This must be called while gdb is *not* running.
e515b470 5408
17e1c970 5409gdb_caching_proc gdb_skip_xml_test {
787f0025 5410 global gdb_spawn_id
e515b470
DJ
5411 global gdb_prompt
5412 global srcdir
e515b470 5413
787f0025
MM
5414 if { [info exists gdb_spawn_id] } {
5415 error "GDB must not be running in gdb_skip_xml_tests."
5416 }
5417
b22089ab
YQ
5418 set xml_file [gdb_remote_download host "${srcdir}/gdb.xml/trivial.xml"]
5419
e515b470 5420 gdb_start
17e1c970 5421 set xml_missing 0
b22089ab 5422 gdb_test_multiple "set tdesc filename $xml_file" "" {
e515b470 5423 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
17e1c970 5424 set xml_missing 1
e515b470
DJ
5425 }
5426 -re ".*$gdb_prompt $" { }
5427 }
5428 gdb_exit
17e1c970 5429 return $xml_missing
e515b470 5430}
1f8a6abb 5431
673dc4a0
YQ
5432# Return true if argv[0] is available.
5433
5434gdb_caching_proc gdb_has_argv0 {
5435 set result 0
5436
bf326452
AH
5437 # Compile and execute a test program to check whether argv[0] is available.
5438 gdb_simple_compile has_argv0 {
673dc4a0
YQ
5439 int main (int argc, char **argv) {
5440 return 0;
5441 }
bf326452 5442 } executable
673dc4a0 5443
673dc4a0
YQ
5444
5445 # Helper proc.
5446 proc gdb_has_argv0_1 { exe } {
5447 global srcdir subdir
5448 global gdb_prompt hex
5449
5450 gdb_exit
5451 gdb_start
5452 gdb_reinitialize_dir $srcdir/$subdir
5453 gdb_load "$exe"
5454
5455 # Set breakpoint on main.
5456 gdb_test_multiple "break main" "break main" {
5457 -re "Breakpoint.*${gdb_prompt} $" {
5458 }
5459 -re "${gdb_prompt} $" {
5460 return 0
5461 }
5462 }
5463
5464 # Run to main.
5465 gdb_run_cmd
5466 gdb_test_multiple "" "run to main" {
5467 -re "Breakpoint.*${gdb_prompt} $" {
5468 }
5469 -re "${gdb_prompt} $" {
5470 return 0
5471 }
5472 }
5473
c0ecb95f
JK
5474 set old_elements "200"
5475 set test "show print elements"
5476 gdb_test_multiple $test $test {
5477 -re "Limit on string chars or array elements to print is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
5478 set old_elements $expect_out(1,string)
5479 }
5480 }
5481 set old_repeats "200"
5482 set test "show print repeats"
5483 gdb_test_multiple $test $test {
5484 -re "Threshold for repeated print elements is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
5485 set old_repeats $expect_out(1,string)
5486 }
5487 }
5488 gdb_test_no_output "set print elements unlimited" ""
5489 gdb_test_no_output "set print repeats unlimited" ""
5490
5491 set retval 0
673dc4a0
YQ
5492 # Check whether argc is 1.
5493 gdb_test_multiple "p argc" "p argc" {
5494 -re " = 1\r\n${gdb_prompt} $" {
5495
5496 gdb_test_multiple "p argv\[0\]" "p argv\[0\]" {
5497 -re " = $hex \".*[file tail $exe]\"\r\n${gdb_prompt} $" {
c0ecb95f 5498 set retval 1
673dc4a0
YQ
5499 }
5500 -re "${gdb_prompt} $" {
673dc4a0
YQ
5501 }
5502 }
5503 }
5504 -re "${gdb_prompt} $" {
673dc4a0
YQ
5505 }
5506 }
c0ecb95f
JK
5507
5508 gdb_test_no_output "set print elements $old_elements" ""
5509 gdb_test_no_output "set print repeats $old_repeats" ""
5510
5511 return $retval
673dc4a0
YQ
5512 }
5513
bf326452 5514 set result [gdb_has_argv0_1 $obj]
673dc4a0
YQ
5515
5516 gdb_exit
bf326452 5517 file delete $obj
673dc4a0
YQ
5518
5519 if { !$result
5520 && ([istarget *-*-linux*]
5521 || [istarget *-*-freebsd*] || [istarget *-*-kfreebsd*]
5522 || [istarget *-*-netbsd*] || [istarget *-*-knetbsd*]
5523 || [istarget *-*-openbsd*]
5524 || [istarget *-*-darwin*]
5525 || [istarget *-*-solaris*]
5526 || [istarget *-*-aix*]
5527 || [istarget *-*-gnu*]
5528 || [istarget *-*-cygwin*] || [istarget *-*-mingw32*]
5529 || [istarget *-*-*djgpp*] || [istarget *-*-go32*]
5530 || [istarget *-wince-pe] || [istarget *-*-mingw32ce*]
5531 || [istarget *-*-symbianelf*]
5532 || [istarget *-*-osf*]
673dc4a0
YQ
5533 || [istarget *-*-dicos*]
5534 || [istarget *-*-nto*]
5535 || [istarget *-*-*vms*]
5536 || [istarget *-*-lynx*178]) } {
5537 fail "argv\[0\] should be available on this target"
5538 }
5539
5540 return $result
5541}
5542
1f8a6abb
EZ
5543# Note: the procedure gdb_gnu_strip_debug will produce an executable called
5544# ${binfile}.dbglnk, which is just like the executable ($binfile) but without
5545# the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
8e1d0c49
JK
5546# the name of a debuginfo only file. This file will be stored in the same
5547# subdirectory.
1f8a6abb
EZ
5548
5549# Functions for separate debug info testing
5550
5551# starting with an executable:
5552# foo --> original executable
5553
5554# at the end of the process we have:
5555# foo.stripped --> foo w/o debug info
8e1d0c49 5556# foo.debug --> foo's debug info
1f8a6abb
EZ
5557# foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
5558
7c50a931
DE
5559# Fetch the build id from the file.
5560# Returns "" if there is none.
5561
5562proc get_build_id { filename } {
c74f7d1c
JT
5563 if { ([istarget "*-*-mingw*"]
5564 || [istarget *-*-cygwin*]) } {
5565 set objdump_program [gdb_find_objdump]
5566 set result [catch {set data [exec $objdump_program -p $filename | grep signature | cut "-d " -f4]} output]
5567 verbose "result is $result"
5568 verbose "output is $output"
5569 if {$result == 1} {
5570 return ""
5571 }
5572 return $data
92046791 5573 } else {
c74f7d1c
JT
5574 set tmp [standard_output_file "${filename}-tmp"]
5575 set objcopy_program [gdb_find_objcopy]
5576 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $filename $tmp" output]
5577 verbose "result is $result"
5578 verbose "output is $output"
5579 if {$result == 1} {
5580 return ""
5581 }
5582 set fi [open $tmp]
5583 fconfigure $fi -translation binary
5584 # Skip the NOTE header.
5585 read $fi 16
5586 set data [read $fi]
5587 close $fi
5588 file delete $tmp
5589 if ![string compare $data ""] then {
5590 return ""
5591 }
5592 # Convert it to hex.
5593 binary scan $data H* data
5594 return $data
4935890f 5595 }
7c50a931
DE
5596}
5597
5598# Return the build-id hex string (usually 160 bits as 40 hex characters)
5599# converted to the form: .build-id/ab/cdef1234...89.debug
5600# Return "" if no build-id found.
5601proc build_id_debug_filename_get { filename } {
5602 set data [get_build_id $filename]
5603 if { $data == "" } {
5604 return ""
5605 }
061b5285 5606 regsub {^..} $data {\0/} data
ae59b1da 5607 return ".build-id/${data}.debug"
4935890f
JK
5608}
5609
94277a38
DJ
5610# Create stripped files for DEST, replacing it. If ARGS is passed, it is a
5611# list of optional flags. The only currently supported flag is no-main,
5612# which removes the symbol entry for main from the separate debug file.
c0201579
JK
5613#
5614# Function returns zero on success. Function will return non-zero failure code
5615# on some targets not supporting separate debug info (such as i386-msdos).
1f8a6abb 5616
94277a38
DJ
5617proc gdb_gnu_strip_debug { dest args } {
5618
8e1d0c49
JK
5619 # Use the first separate debug info file location searched by GDB so the
5620 # run cannot be broken by some stale file searched with higher precedence.
5621 set debug_file "${dest}.debug"
5622
b741e217 5623 set strip_to_file_program [transform strip]
4fa7d390 5624 set objcopy_program [gdb_find_objcopy]
1f8a6abb 5625
1f8a6abb
EZ
5626 set debug_link [file tail $debug_file]
5627 set stripped_file "${dest}.stripped"
5628
5629 # Get rid of the debug info, and store result in stripped_file
5630 # something like gdb/testsuite/gdb.base/blah.stripped.
5631 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
5632 verbose "result is $result"
5633 verbose "output is $output"
5634 if {$result == 1} {
5635 return 1
5636 }
5637
d521f563
JK
5638 # Workaround PR binutils/10802:
5639 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
5640 set perm [file attributes ${dest} -permissions]
5641 file attributes ${stripped_file} -permissions $perm
5642
1f8a6abb
EZ
5643 # Get rid of everything but the debug info, and store result in debug_file
5644 # This will be in the .debug subdirectory, see above.
5645 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
5646 verbose "result is $result"
5647 verbose "output is $output"
5648 if {$result == 1} {
5649 return 1
5650 }
5651
94277a38
DJ
5652 # If no-main is passed, strip the symbol for main from the separate
5653 # file. This is to simulate the behavior of elfutils's eu-strip, which
5654 # leaves the symtab in the original file only. There's no way to get
5655 # objcopy or strip to remove the symbol table without also removing the
5656 # debugging sections, so this is as close as we can get.
5657 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
5658 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
5659 verbose "result is $result"
5660 verbose "output is $output"
5661 if {$result == 1} {
5662 return 1
5663 }
5664 file delete "${debug_file}"
5665 file rename "${debug_file}-tmp" "${debug_file}"
5666 }
5667
1f8a6abb
EZ
5668 # Link the two previous output files together, adding the .gnu_debuglink
5669 # section to the stripped_file, containing a pointer to the debug_file,
5670 # save the new file in dest.
5671 # This will be the regular executable filename, in the usual location.
5672 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
5673 verbose "result is $result"
5674 verbose "output is $output"
5675 if {$result == 1} {
5676 return 1
5677 }
5678
d521f563
JK
5679 # Workaround PR binutils/10802:
5680 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
5681 set perm [file attributes ${stripped_file} -permissions]
5682 file attributes ${dest} -permissions $perm
5683
5684 return 0
1f8a6abb
EZ
5685}
5686
d8295fe9
VP
5687# Test the output of GDB_COMMAND matches the pattern obtained
5688# by concatenating all elements of EXPECTED_LINES. This makes
5689# it possible to split otherwise very long string into pieces.
206584bd 5690# If third argument TESTNAME is not empty, it's used as the name of the
d8295fe9 5691# test to be printed on pass/fail.
206584bd
PW
5692proc help_test_raw { gdb_command expected_lines {testname {}} } {
5693 if {$testname == {}} {
5694 set message $gdb_command
5695 } else {
5696 set message $testname
5697 }
d8295fe9
VP
5698 set expected_output [join $expected_lines ""]
5699 gdb_test "${gdb_command}" "${expected_output}" $message
5700}
5701
206584bd
PW
5702# A regexp that matches the end of help CLASS|PREFIX_COMMAND
5703set help_list_trailer {
5704 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n]+"
5705 "Type \"apropos -v word\" for full documentation of commands related to \"word\"\.[\r\n]+"
5706 "Command name abbreviations are allowed if unambiguous\."
5707}
5708
5709# Test the output of "help COMMAND_CLASS". EXPECTED_INITIAL_LINES
d8295fe9 5710# are regular expressions that should match the beginning of output,
206584bd
PW
5711# before the list of commands in that class.
5712# LIST_OF_COMMANDS are regular expressions that should match the
5713# list of commands in that class. If empty, the command list will be
5714# matched automatically. The presence of standard epilogue will be tested
5715# automatically.
5716# If last argument TESTNAME is not empty, it's used as the name of the
5717# test to be printed on pass/fail.
06f810bd
MG
5718# Notice that the '[' and ']' characters don't need to be escaped for strings
5719# wrapped in {} braces.
206584bd
PW
5720proc test_class_help { command_class expected_initial_lines {list_of_commands {}} {testname {}} } {
5721 global help_list_trailer
5722 if {[llength $list_of_commands]>0} {
5723 set l_list_of_commands {"List of commands:[\r\n]+[\r\n]+"}
5724 set l_list_of_commands [concat $l_list_of_commands $list_of_commands]
5725 set l_list_of_commands [concat $l_list_of_commands {"[\r\n]+[\r\n]+"}]
5726 } else {
5727 set l_list_of_commands {"List of commands\:.*[\r\n]+"}
5728 }
d8295fe9 5729 set l_stock_body {
06f810bd 5730 "Type \"help\" followed by command name for full documentation\.[\r\n]+"
d8295fe9 5731 }
206584bd
PW
5732 set l_entire_body [concat $expected_initial_lines $l_list_of_commands \
5733 $l_stock_body $help_list_trailer]
d8295fe9 5734
206584bd 5735 help_test_raw "help ${command_class}" $l_entire_body $testname
d8295fe9
VP
5736}
5737
206584bd
PW
5738# Like test_class_help but specialised to test "help user-defined".
5739proc test_user_defined_class_help { {list_of_commands {}} {testname {}} } {
5740 test_class_help "user-defined" {
5741 "User-defined commands\.[\r\n]+"
5742 "The commands in this class are those defined by the user\.[\r\n]+"
5743 "Use the \"define\" command to define a command\.[\r\n]+"
5744 } $list_of_commands $testname
5745}
5746
5747
d8295fe9
VP
5748# COMMAND_LIST should have either one element -- command to test, or
5749# two elements -- abbreviated command to test, and full command the first
5750# element is abbreviation of.
5751# The command must be a prefix command. EXPECTED_INITIAL_LINES
5752# are regular expressions that should match the beginning of output,
5753# before the list of subcommands. The presence of
5754# subcommand list and standard epilogue will be tested automatically.
5755proc test_prefix_command_help { command_list expected_initial_lines args } {
206584bd 5756 global help_list_trailer
d8295fe9
VP
5757 set command [lindex $command_list 0]
5758 if {[llength $command_list]>1} {
5759 set full_command [lindex $command_list 1]
5760 } else {
5761 set full_command $command
5762 }
5763 # Use 'list' and not just {} because we want variables to
5764 # be expanded in this list.
5765 set l_stock_body [list\
5766 "List of $full_command subcommands\:.*\[\r\n\]+"\
206584bd
PW
5767 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"]
5768 set l_entire_body [concat $expected_initial_lines $l_stock_body $help_list_trailer]
d8295fe9
VP
5769 if {[llength $args]>0} {
5770 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
5771 } else {
5772 help_test_raw "help ${command}" $l_entire_body
5773 }
5774}
dbc52822 5775
85b4440a
TT
5776# Build executable named EXECUTABLE from specifications that allow
5777# different options to be passed to different sub-compilations.
5778# TESTNAME is the name of the test; this is passed to 'untested' if
5779# something fails.
a0d3f2f5
SCR
5780# OPTIONS is passed to the final link, using gdb_compile. If OPTIONS
5781# contains the option "pthreads", then gdb_compile_pthreads is used.
85b4440a
TT
5782# ARGS is a flat list of source specifications, of the form:
5783# { SOURCE1 OPTIONS1 [ SOURCE2 OPTIONS2 ]... }
5784# Each SOURCE is compiled to an object file using its OPTIONS,
5785# using gdb_compile.
5786# Returns 0 on success, -1 on failure.
5787proc build_executable_from_specs {testname executable options args} {
dbc52822
VP
5788 global subdir
5789 global srcdir
dbc52822 5790
0a6d0306 5791 set binfile [standard_output_file $executable]
dbc52822 5792
fd961404
DE
5793 set info_options ""
5794 if { [lsearch -exact $options "c++"] >= 0 } {
5795 set info_options "c++"
5796 }
4c93b1db 5797 if [get_compiler_info ${info_options}] {
dbc52822
VP
5798 return -1
5799 }
a29a3fb7 5800
a29a3fb7
GB
5801 set func gdb_compile
5802 set func_index [lsearch -regexp $options {^(pthreads|shlib|shlib_pthreads)$}]
5803 if {$func_index != -1} {
5804 set func "${func}_[lindex $options $func_index]"
5805 }
5806
5807 # gdb_compile_shlib and gdb_compile_shlib_pthreads do not use the 3rd
5808 # parameter. They also requires $sources while gdb_compile and
5809 # gdb_compile_pthreads require $objects. Moreover they ignore any options.
5810 if [string match gdb_compile_shlib* $func] {
5811 set sources_path {}
5812 foreach {s local_options} $args {
0e5c4555
AA
5813 if { [regexp "^/" "$s"] } then {
5814 lappend sources_path "$s"
5815 } else {
5816 lappend sources_path "$srcdir/$subdir/$s"
5817 }
a29a3fb7
GB
5818 }
5819 set ret [$func $sources_path "${binfile}" $options]
67218854
TT
5820 } elseif {[lsearch -exact $options rust] != -1} {
5821 set sources_path {}
5822 foreach {s local_options} $args {
5823 if { [regexp "^/" "$s"] } then {
5824 lappend sources_path "$s"
5825 } else {
5826 lappend sources_path "$srcdir/$subdir/$s"
5827 }
5828 }
5829 set ret [gdb_compile_rust $sources_path "${binfile}" $options]
a29a3fb7
GB
5830 } else {
5831 set objects {}
5832 set i 0
5833 foreach {s local_options} $args {
0e5c4555
AA
5834 if { ! [regexp "^/" "$s"] } then {
5835 set s "$srcdir/$subdir/$s"
5836 }
5837 if { [gdb_compile "${s}" "${binfile}${i}.o" object $local_options] != "" } {
a29a3fb7
GB
5838 untested $testname
5839 return -1
5840 }
5841 lappend objects "${binfile}${i}.o"
5842 incr i
5843 }
5844 set ret [$func $objects "${binfile}" executable $options]
5845 }
5846 if { $ret != "" } {
5847 untested $testname
5848 return -1
5849 }
5850
dbc52822
VP
5851 return 0
5852}
5853
85b4440a
TT
5854# Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
5855# provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
5856# to pass to untested, if something is wrong. OPTIONS are passed
5857# to gdb_compile directly.
5858proc build_executable { testname executable {sources ""} {options {debug}} } {
5859 if {[llength $sources]==0} {
5860 set sources ${executable}.c
5861 }
5862
5863 set arglist [list $testname $executable $options]
5864 foreach source $sources {
5865 lappend arglist $source $options
5866 }
5867
5868 return [eval build_executable_from_specs $arglist]
5869}
5870
7b606f95
DE
5871# Starts fresh GDB binary and loads an optional executable into GDB.
5872# Usage: clean_restart [executable]
5873# EXECUTABLE is the basename of the binary.
5874
5875proc clean_restart { args } {
dbc52822 5876 global srcdir
dbc52822 5877 global subdir
7b606f95
DE
5878
5879 if { [llength $args] > 1 } {
5880 error "bad number of args: [llength $args]"
5881 }
dbc52822
VP
5882
5883 gdb_exit
5884 gdb_start
5885 gdb_reinitialize_dir $srcdir/$subdir
7b606f95
DE
5886
5887 if { [llength $args] >= 1 } {
5888 set executable [lindex $args 0]
5889 set binfile [standard_output_file ${executable}]
5890 gdb_load ${binfile}
5891 }
dbc52822
VP
5892}
5893
85b4440a
TT
5894# Prepares for testing by calling build_executable_full, then
5895# clean_restart.
5896# TESTNAME is the name of the test.
5897# Each element in ARGS is a list of the form
5898# { EXECUTABLE OPTIONS SOURCE_SPEC... }
5899# These are passed to build_executable_from_specs, which see.
5900# The last EXECUTABLE is passed to clean_restart.
5901# Returns 0 on success, non-zero on failure.
5902proc prepare_for_testing_full {testname args} {
5903 foreach spec $args {
5904 if {[eval build_executable_from_specs [list $testname] $spec] == -1} {
5905 return -1
5906 }
5907 set executable [lindex $spec 0]
5908 }
5909 clean_restart $executable
5910 return 0
5911}
5912
dbc52822
VP
5913# Prepares for testing, by calling build_executable, and then clean_restart.
5914# Please refer to build_executable for parameter description.
5915proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
5916
734a5c36 5917 if {[build_executable $testname $executable $sources $options] == -1} {
dbc52822
VP
5918 return -1
5919 }
5920 clean_restart $executable
5921
5922 return 0
5923}
7065b901 5924
0efcde63
AK
5925# Retrieve the value of EXP in the inferior, represented in format
5926# specified in FMT (using "printFMT"). DEFAULT is used as fallback if
5927# print fails. TEST is the test message to use. It can be omitted,
5928# in which case a test message is built from EXP.
5929
5930proc get_valueof { fmt exp default {test ""} } {
7065b901
TT
5931 global gdb_prompt
5932
0efcde63
AK
5933 if {$test == "" } {
5934 set test "get valueof \"${exp}\""
5935 }
5936
7065b901
TT
5937 set val ${default}
5938 gdb_test_multiple "print${fmt} ${exp}" "$test" {
c2c2dd9f 5939 -re "\\$\[0-9\]* = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
417e16e2 5940 set val $expect_out(1,string)
1443936e 5941 pass "$test"
417e16e2
PM
5942 }
5943 timeout {
5944 fail "$test (timeout)"
5945 }
5946 }
5947 return ${val}
5948}
5949
0efcde63
AK
5950# Retrieve the value of EXP in the inferior, as a signed decimal value
5951# (using "print /d"). DEFAULT is used as fallback if print fails.
5952# TEST is the test message to use. It can be omitted, in which case
5953# a test message is built from EXP.
5954
5955proc get_integer_valueof { exp default {test ""} } {
417e16e2
PM
5956 global gdb_prompt
5957
0efcde63
AK
5958 if {$test == ""} {
5959 set test "get integer valueof \"${exp}\""
5960 }
5961
417e16e2
PM
5962 set val ${default}
5963 gdb_test_multiple "print /d ${exp}" "$test" {
7065b901
TT
5964 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
5965 set val $expect_out(1,string)
2f20e312 5966 pass "$test"
7065b901
TT
5967 }
5968 timeout {
417e16e2 5969 fail "$test (timeout)"
7065b901
TT
5970 }
5971 }
5972 return ${val}
5973}
5974
20aa2c60
PA
5975# Retrieve the value of EXP in the inferior, as an hexadecimal value
5976# (using "print /x"). DEFAULT is used as fallback if print fails.
0efcde63 5977# TEST is the test message to use. It can be omitted, in which case
20aa2c60
PA
5978# a test message is built from EXP.
5979
5980proc get_hexadecimal_valueof { exp default {test ""} } {
faafb047 5981 global gdb_prompt
20aa2c60
PA
5982
5983 if {$test == ""} {
5984 set test "get hexadecimal valueof \"${exp}\""
5985 }
5986
5987 set val ${default}
5988 gdb_test_multiple "print /x ${exp}" $test {
faafb047
PM
5989 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
5990 set val $expect_out(1,string)
5991 pass "$test"
5992 }
faafb047
PM
5993 }
5994 return ${val}
5995}
417e16e2 5996
0efcde63
AK
5997# Retrieve the size of TYPE in the inferior, as a decimal value. DEFAULT
5998# is used as fallback if print fails. TEST is the test message to use.
5999# It can be omitted, in which case a test message is 'sizeof (TYPE)'.
6000
6001proc get_sizeof { type default {test ""} } {
6002 return [get_integer_valueof "sizeof (${type})" $default $test]
7065b901
TT
6003}
6004
ed3ef339
DE
6005proc get_target_charset { } {
6006 global gdb_prompt
6007
6008 gdb_test_multiple "show target-charset" "" {
6009 -re "The target character set is \"auto; currently (\[^\"\]*)\".*$gdb_prompt $" {
6010 return $expect_out(1,string)
6011 }
6012 -re "The target character set is \"(\[^\"\]*)\".*$gdb_prompt $" {
6013 return $expect_out(1,string)
6014 }
6015 }
6016
6017 # Pick a reasonable default.
6018 warning "Unable to read target-charset."
6019 return "UTF-8"
6020}
6021
5ad9dba7
YQ
6022# Get the address of VAR.
6023
6024proc get_var_address { var } {
6025 global gdb_prompt hex
6026
6027 # Match output like:
6028 # $1 = (int *) 0x0
6029 # $5 = (int (*)()) 0
6030 # $6 = (int (*)()) 0x24 <function_bar>
6031
6032 gdb_test_multiple "print &${var}" "get address of ${var}" {
6033 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $"
6034 {
6035 pass "get address of ${var}"
6036 if { $expect_out(1,string) == "0" } {
6037 return "0x0"
6038 } else {
6039 return $expect_out(1,string)
6040 }
6041 }
6042 }
6043 return ""
6044}
6045
45f25d6c
AB
6046# Return the frame number for the currently selected frame
6047proc get_current_frame_number {{test_name ""}} {
6048 global gdb_prompt
6049
6050 if { $test_name == "" } {
6051 set test_name "get current frame number"
6052 }
6053 set frame_num -1
6054 gdb_test_multiple "frame" $test_name {
6055 -re "#(\[0-9\]+) .*$gdb_prompt $" {
6056 set frame_num $expect_out(1,string)
6057 }
6058 }
6059 return $frame_num
6060}
6061
db863c42
MF
6062# Get the current value for remotetimeout and return it.
6063proc get_remotetimeout { } {
6064 global gdb_prompt
6065 global decimal
6066
6067 gdb_test_multiple "show remotetimeout" "" {
6068 -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" {
ae59b1da 6069 return $expect_out(1,string)
db863c42
MF
6070 }
6071 }
6072
6073 # Pick the default that gdb uses
6074 warning "Unable to read remotetimeout"
6075 return 300
6076}
6077
6078# Set the remotetimeout to the specified timeout. Nothing is returned.
6079proc set_remotetimeout { timeout } {
6080 global gdb_prompt
6081
6082 gdb_test_multiple "set remotetimeout $timeout" "" {
6083 -re "$gdb_prompt $" {
6084 verbose "Set remotetimeout to $timeout\n"
6085 }
6086 }
6087}
6088
805acca0
AA
6089# Get the target's current endianness and return it.
6090proc get_endianness { } {
6091 global gdb_prompt
6092
6093 gdb_test_multiple "show endian" "determine endianness" {
6094 -re ".* (little|big) endian.*\r\n$gdb_prompt $" {
6095 # Pass silently.
6096 return $expect_out(1,string)
6097 }
6098 }
6099 return "little"
6100}
6101
1e537771
TT
6102# ROOT and FULL are file names. Returns the relative path from ROOT
6103# to FULL. Note that FULL must be in a subdirectory of ROOT.
6104# For example, given ROOT = /usr/bin and FULL = /usr/bin/ls, this
6105# will return "ls".
6106
6107proc relative_filename {root full} {
6108 set root_split [file split $root]
6109 set full_split [file split $full]
6110
6111 set len [llength $root_split]
6112
6113 if {[eval file join $root_split]
6114 != [eval file join [lrange $full_split 0 [expr {$len - 1}]]]} {
6115 error "$full not a subdir of $root"
6116 }
6117
6118 return [eval file join [lrange $full_split $len end]]
6119}
6120
5e92f71a
TT
6121# If GDB_PARALLEL exists, then set up the parallel-mode directories.
6122if {[info exists GDB_PARALLEL]} {
6123 if {[is_remote host]} {
6124 unset GDB_PARALLEL
6125 } else {
3d338901
DE
6126 file mkdir \
6127 [make_gdb_parallel_path outputs] \
6128 [make_gdb_parallel_path temp] \
6129 [make_gdb_parallel_path cache]
5e92f71a
TT
6130 }
6131}
6132
bbfba9ed 6133proc core_find {binfile {deletefiles {}} {arg ""}} {
37aeb5df
JK
6134 global objdir subdir
6135
6136 set destcore "$binfile.core"
6137 file delete $destcore
6138
6139 # Create a core file named "$destcore" rather than just "core", to
6140 # avoid problems with sys admin types that like to regularly prune all
6141 # files named "core" from the system.
6142 #
6143 # Arbitrarily try setting the core size limit to "unlimited" since
6144 # this does not hurt on systems where the command does not work and
6145 # allows us to generate a core on systems where it does.
6146 #
6147 # Some systems append "core" to the name of the program; others append
6148 # the name of the program to "core"; still others (like Linux, as of
6149 # May 2003) create cores named "core.PID". In the latter case, we
6150 # could have many core files lying around, and it may be difficult to
6151 # tell which one is ours, so let's run the program in a subdirectory.
6152 set found 0
93c0ef37 6153 set coredir [standard_output_file coredir.[getpid]]
37aeb5df 6154 file mkdir $coredir
bbfba9ed 6155 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
37aeb5df
JK
6156 # remote_exec host "${binfile}"
6157 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
6158 if [remote_file build exists $i] {
6159 remote_exec build "mv $i $destcore"
6160 set found 1
6161 }
6162 }
6163 # Check for "core.PID".
6164 if { $found == 0 } {
6165 set names [glob -nocomplain -directory $coredir core.*]
6166 if {[llength $names] == 1} {
6167 set corefile [file join $coredir [lindex $names 0]]
6168 remote_exec build "mv $corefile $destcore"
6169 set found 1
6170 }
6171 }
6172 if { $found == 0 } {
6173 # The braindamaged HPUX shell quits after the ulimit -c above
6174 # without executing ${binfile}. So we try again without the
6175 # ulimit here if we didn't find a core file above.
6176 # Oh, I should mention that any "braindamaged" non-Unix system has
6177 # the same problem. I like the cd bit too, it's really neat'n stuff.
6178 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
6179 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
6180 if [remote_file build exists $i] {
6181 remote_exec build "mv $i $destcore"
6182 set found 1
6183 }
6184 }
6185 }
6186
6187 # Try to clean up after ourselves.
6188 foreach deletefile $deletefiles {
6189 remote_file build delete [file join $coredir $deletefile]
6190 }
6191 remote_exec build "rmdir $coredir"
6192
6193 if { $found == 0 } {
6194 warning "can't generate a core file - core tests suppressed - check ulimit -c"
6195 return ""
6196 }
6197 return $destcore
6198}
ee5683ab 6199
2223449a
KB
6200# gdb_target_symbol_prefix compiles a test program and then examines
6201# the output from objdump to determine the prefix (such as underscore)
6202# for linker symbol prefixes.
6203
6204gdb_caching_proc gdb_target_symbol_prefix {
bf326452
AH
6205 # Compile a simple test program...
6206 set src { int main() { return 0; } }
6207 if {![gdb_simple_compile target_symbol_prefix $src executable]} {
6208 return 0
2223449a
KB
6209 }
6210
2223449a
KB
6211 set prefix ""
6212
bf326452
AH
6213 set objdump_program [gdb_find_objdump]
6214 set result [catch "exec $objdump_program --syms $obj" output]
2223449a 6215
bf326452
AH
6216 if { $result == 0 \
6217 && ![regexp -lineanchor \
6218 { ([^ a-zA-Z0-9]*)main$} $output dummy prefix] } {
6219 verbose "gdb_target_symbol_prefix: Could not find main in objdump output; returning null prefix" 2
2223449a
KB
6220 }
6221
bf326452 6222 file delete $obj
2223449a
KB
6223
6224 return $prefix
6225}
6226
5bd18990
AB
6227# Return 1 if target supports scheduler locking, otherwise return 0.
6228
6229gdb_caching_proc target_supports_scheduler_locking {
6230 global gdb_prompt
6231
6232 set me "gdb_target_supports_scheduler_locking"
6233
bf326452
AH
6234 set src { int main() { return 0; } }
6235 if {![gdb_simple_compile $me $src executable]} {
5bd18990
AB
6236 return 0
6237 }
6238
bf326452 6239 clean_restart $obj
58bbcd02
TV
6240 if ![runto_main] {
6241 return 0
6242 }
5bd18990
AB
6243
6244 set supports_schedule_locking -1
6245 set current_schedule_locking_mode ""
6246
6247 set test "reading current scheduler-locking mode"
6248 gdb_test_multiple "show scheduler-locking" $test {
6249 -re "Mode for locking scheduler during execution is \"(\[\^\"\]*)\".*$gdb_prompt" {
6250 set current_schedule_locking_mode $expect_out(1,string)
6251 }
6252 -re "$gdb_prompt $" {
6253 set supports_schedule_locking 0
6254 }
6255 timeout {
6256 set supports_schedule_locking 0
6257 }
6258 }
6259
6260 if { $supports_schedule_locking == -1 } {
6261 set test "checking for scheduler-locking support"
6262 gdb_test_multiple "set scheduler-locking $current_schedule_locking_mode" $test {
6263 -re "Target '\[^'\]+' cannot support this command\..*$gdb_prompt $" {
6264 set supports_schedule_locking 0
6265 }
6266 -re "$gdb_prompt $" {
6267 set supports_schedule_locking 1
6268 }
6269 timeout {
6270 set supports_schedule_locking 0
6271 }
6272 }
6273 }
6274
6275 if { $supports_schedule_locking == -1 } {
6276 set supports_schedule_locking 0
6277 }
6278
6279 gdb_exit
bf326452 6280 remote_file build delete $obj
5bd18990
AB
6281 verbose "$me: returning $supports_schedule_locking" 2
6282 return $supports_schedule_locking
6283}
6284
2223449a
KB
6285# gdb_target_symbol returns the provided symbol with the correct prefix
6286# prepended. (See gdb_target_symbol_prefix, above.)
6287
6288proc gdb_target_symbol { symbol } {
6289 set prefix [gdb_target_symbol_prefix]
6290 return "${prefix}${symbol}"
6291}
6292
f01dcfd9
KB
6293# gdb_target_symbol_prefix_flags_asm returns a string that can be
6294# added to gdb_compile options to define the C-preprocessor macro
6295# SYMBOL_PREFIX with a value that can be prepended to symbols
6296# for targets which require a prefix, such as underscore.
6297#
6298# This version (_asm) defines the prefix without double quotes
6299# surrounding the prefix. It is used to define the macro
6300# SYMBOL_PREFIX for assembly language files. Another version, below,
6301# is used for symbols in inline assembler in C/C++ files.
6302#
6303# The lack of quotes in this version (_asm) makes it possible to
6304# define supporting macros in the .S file. (The version which
6305# uses quotes for the prefix won't work for such files since it's
6306# impossible to define a quote-stripping macro in C.)
6307#
6308# It's possible to use this version (_asm) for C/C++ source files too,
6309# but a string is usually required in such files; providing a version
6310# (no _asm) which encloses the prefix with double quotes makes it
6311# somewhat easier to define the supporting macros in the test case.
6312
6313proc gdb_target_symbol_prefix_flags_asm {} {
6314 set prefix [gdb_target_symbol_prefix]
6315 if {$prefix ne ""} {
6316 return "additional_flags=-DSYMBOL_PREFIX=$prefix"
6317 } else {
6318 return "";
6319 }
6320}
6321
6322# gdb_target_symbol_prefix_flags returns the same string as
6323# gdb_target_symbol_prefix_flags_asm, above, but with the prefix
6324# enclosed in double quotes if there is a prefix.
6325#
6326# See the comment for gdb_target_symbol_prefix_flags_asm for an
6327# extended discussion.
ee5683ab
PM
6328
6329proc gdb_target_symbol_prefix_flags {} {
f01dcfd9
KB
6330 set prefix [gdb_target_symbol_prefix]
6331 if {$prefix ne ""} {
6332 return "additional_flags=-DSYMBOL_PREFIX=\"$prefix\""
ee5683ab 6333 } else {
f01dcfd9 6334 return "";
ee5683ab
PM
6335 }
6336}
6337
6e45f158
DE
6338# A wrapper for 'remote_exec host' that passes or fails a test.
6339# Returns 0 if all went well, nonzero on failure.
6340# TEST is the name of the test, other arguments are as for remote_exec.
6341
6342proc run_on_host { test program args } {
6343 verbose -log "run_on_host: $program $args"
6344 # remote_exec doesn't work properly if the output is set but the
6345 # input is the empty string -- so replace an empty input with
6346 # /dev/null.
6347 if {[llength $args] > 1 && [lindex $args 1] == ""} {
6348 set args [lreplace $args 1 1 "/dev/null"]
6349 }
6350 set result [eval remote_exec host [list $program] $args]
6351 verbose "result is $result"
6352 set status [lindex $result 0]
6353 set output [lindex $result 1]
6354 if {$status == 0} {
6355 pass $test
6356 return 0
6357 } else {
50cc37c8 6358 verbose -log "run_on_host failed: $output"
6e45f158
DE
6359 fail $test
6360 return -1
6361 }
6362}
6363
a587b477
DE
6364# Return non-zero if "board_info debug_flags" mentions Fission.
6365# http://gcc.gnu.org/wiki/DebugFission
6366# Fission doesn't support everything yet.
6367# This supports working around bug 15954.
6368
6369proc using_fission { } {
6370 set debug_flags [board_info [target_info name] debug_flags]
6371 return [regexp -- "-gsplit-dwarf" $debug_flags]
6372}
6373
4b48d439
KS
6374# Search the caller's ARGS list and set variables according to the list of
6375# valid options described by ARGSET.
6376#
6377# The first member of each one- or two-element list in ARGSET defines the
6378# name of a variable that will be added to the caller's scope.
6379#
6380# If only one element is given to describe an option, it the value is
6381# 0 if the option is not present in (the caller's) ARGS or 1 if
6382# it is.
6383#
6384# If two elements are given, the second element is the default value of
6385# the variable. This is then overwritten if the option exists in ARGS.
6386#
6387# Any parse_args elements in (the caller's) ARGS will be removed, leaving
6388# any optional components.
6389
6390# Example:
6391# proc myproc {foo args} {
6392# parse_args {{bar} {baz "abc"} {qux}}
6393# # ...
6394# }
6395# myproc ABC -bar -baz DEF peanut butter
6396# will define the following variables in myproc:
6397# foo (=ABC), bar (=1), baz (=DEF), and qux (=0)
6398# args will be the list {peanut butter}
6399
6400proc parse_args { argset } {
6401 upvar args args
6402
6403 foreach argument $argset {
6404 if {[llength $argument] == 1} {
6405 # No default specified, so we assume that we should set
6406 # the value to 1 if the arg is present and 0 if it's not.
6407 # It is assumed that no value is given with the argument.
6408 set result [lsearch -exact $args "-$argument"]
6409 if {$result != -1} then {
6410 uplevel 1 [list set $argument 1]
6411 set args [lreplace $args $result $result]
6412 } else {
6413 uplevel 1 [list set $argument 0]
6414 }
6415 } elseif {[llength $argument] == 2} {
6416 # There are two items in the argument. The second is a
6417 # default value to use if the item is not present.
6418 # Otherwise, the variable is set to whatever is provided
6419 # after the item in the args.
6420 set arg [lindex $argument 0]
6421 set result [lsearch -exact $args "-[lindex $arg 0]"]
6422 if {$result != -1} then {
6423 uplevel 1 [list set $arg [lindex $args [expr $result+1]]]
6424 set args [lreplace $args $result [expr $result+1]]
6425 } else {
6426 uplevel 1 [list set $arg [lindex $argument 1]]
6427 }
6428 } else {
6429 error "Badly formatted argument \"$argument\" in argument set"
6430 }
6431 }
6432
6433 # The remaining args should be checked to see that they match the
6434 # number of items expected to be passed into the procedure...
6435}
6436
87f0e720
KS
6437# Capture the output of COMMAND in a string ignoring PREFIX (a regexp);
6438# return that string.
6439
e9089e05
MM
6440proc capture_command_output { command prefix } {
6441 global gdb_prompt
6442 global expect_out
6443
6444 set output_string ""
6445 gdb_test_multiple "$command" "capture_command_output for $command" {
87f0e720 6446 -re "[string_to_regexp ${command}]\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $" {
e9089e05
MM
6447 set output_string $expect_out(1,string)
6448 }
6449 }
6450 return $output_string
6451}
6452
3c724c8c
PMR
6453# A convenience function that joins all the arguments together, with a
6454# regexp that matches exactly one end of line in between each argument.
6455# This function is ideal to write the expected output of a GDB command
6456# that generates more than a couple of lines, as this allows us to write
6457# each line as a separate string, which is easier to read by a human
6458# being.
6459
6460proc multi_line { args } {
6461 return [join $args "\r\n"]
6462}
6463
fad0c9fb
PA
6464# Similar to the above, but while multi_line is meant to be used to
6465# match GDB output, this one is meant to be used to build strings to
6466# send as GDB input.
6467
6468proc multi_line_input { args } {
6469 return [join $args "\n"]
6470}
6471
896c0c1e
SM
6472# Return the version of the DejaGnu framework.
6473#
6474# The return value is a list containing the major, minor and patch version
6475# numbers. If the version does not contain a minor or patch number, they will
6476# be set to 0. For example:
6477#
6478# 1.6 -> {1 6 0}
6479# 1.6.1 -> {1 6 1}
6480# 2 -> {2 0 0}
6481
6482proc dejagnu_version { } {
6483 # The frame_version variable is defined by DejaGnu, in runtest.exp.
6484 global frame_version
6485
6486 verbose -log "DejaGnu version: $frame_version"
6487 verbose -log "Expect version: [exp_version]"
6488 verbose -log "Tcl version: [info tclversion]"
6489
6490 set dg_ver [split $frame_version .]
6491
6492 while { [llength $dg_ver] < 3 } {
6493 lappend dg_ver 0
6494 }
6495
6496 return $dg_ver
6497}
fad0c9fb 6498
3a3fd0fd
PA
6499# Define user-defined command COMMAND using the COMMAND_LIST as the
6500# command's definition. The terminating "end" is added automatically.
6501
6502proc gdb_define_cmd {command command_list} {
6503 global gdb_prompt
6504
6505 set input [multi_line_input {*}$command_list "end"]
6506 set test "define $command"
6507
6508 gdb_test_multiple "define $command" $test {
6509 -re "End with" {
6510 gdb_test_multiple $input $test {
6511 -re "\r\n$gdb_prompt " {
6512 }
6513 }
6514 }
6515 }
6516}
6517
c3734e09
AH
6518# Override the 'cd' builtin with a version that ensures that the
6519# log file keeps pointing at the same file. We need this because
6520# unfortunately the path to the log file is recorded using an
6521# relative path name, and, we sometimes need to close/reopen the log
6522# after changing the current directory. See get_compiler_info.
6523
6524rename cd builtin_cd
6525
6526proc cd { dir } {
6527
6528 # Get the existing log file flags.
6529 set log_file_info [log_file -info]
6530
6531 # Split the flags into args and file name.
6532 set log_file_flags ""
6533 set log_file_file ""
6534 foreach arg [ split "$log_file_info" " "] {
6535 if [string match "-*" $arg] {
6536 lappend log_file_flags $arg
6537 } else {
6538 lappend log_file_file $arg
6539 }
6540 }
6541
6542 # If there was an existing file, ensure it is an absolute path, and then
6543 # reset logging.
6544 if { $log_file_file != "" } {
6545 set log_file_file [file normalize $log_file_file]
6546 log_file
6547 log_file $log_file_flags "$log_file_file"
6548 }
6549
6550 # Call the builtin version of cd.
6551 builtin_cd $dir
6552}
6553
d7df6549
AB
6554# Return a list of all languages supported by GDB, suitable for use in
6555# 'set language NAME'. This doesn't include either the 'local' or
6556# 'auto' keywords.
6557proc gdb_supported_languages {} {
6558 return [list c objective-c c++ d go fortran modula-2 asm pascal \
6559 opencl rust minimal ada]
6560}
6561
29b52314
AH
6562# Check if debugging is enabled for gdb.
6563
6564proc gdb_debug_enabled { } {
6565 global gdbdebug
6566
6567 # If not already read, get the debug setting from environment or board setting.
6568 if {![info exists gdbdebug]} {
6569 global env
6570 if [info exists env(GDB_DEBUG)] {
6571 set gdbdebug $env(GDB_DEBUG)
6572 } elseif [target_info exists gdb,debug] {
6573 set gdbdebug [target_info gdb,debug]
6574 } else {
6575 return 0
6576 }
6577 }
6578
6579 # Ensure it not empty.
6580 return [expr { $gdbdebug != "" }]
6581}
6582
6583# Turn on debugging if enabled, or reset if already on.
6584
6585proc gdb_debug_init { } {
6586
6587 global gdb_prompt
6588
6589 if ![gdb_debug_enabled] {
6590 return;
6591 }
6592
6593 # First ensure logging is off.
6594 send_gdb "set logging off\n"
6595
6596 set debugfile [standard_output_file gdb.debug]
6597 send_gdb "set logging file $debugfile\n"
6598
6599 send_gdb "set logging debugredirect\n"
6600
6601 global gdbdebug
6602 foreach entry [split $gdbdebug ,] {
6603 send_gdb "set debug $entry 1\n"
6604 }
6605
6606 # Now that everything is set, enable logging.
6607 send_gdb "set logging on\n"
6608 gdb_expect 10 {
6609 -re "Copying output to $debugfile.*Redirecting debug output to $debugfile.*$gdb_prompt $" {}
6610 timeout { warning "Couldn't set logging file" }
6611 }
6612}
6613
dd06d4d6
AH
6614# Check if debugging is enabled for gdbserver.
6615
6616proc gdbserver_debug_enabled { } {
6617 # Always disabled for GDB only setups.
6618 return 0
6619}
6620
f9e2e39d
AH
6621# Open the file for logging gdb input
6622
6623proc gdb_stdin_log_init { } {
6624 global in_file
6625
6626 if {[info exists in_file]} {
6627 # Close existing file.
6628 catch "close $in_file"
6629 }
6630
6631 set logfile [standard_output_file_with_gdb_instance gdb.in]
6632 set in_file [open $logfile w]
6633}
6634
6635# Write to the file for logging gdb input.
6636# TYPE can be one of the following:
6637# "standard" : Default. Standard message written to the log
6638# "answer" : Answer to a question (eg "Y"). Not written the log.
6639# "optional" : Optional message. Not written to the log.
6640
6641proc gdb_stdin_log_write { message {type standard} } {
6642
6643 global in_file
6644 if {![info exists in_file]} {
6645 return
6646 }
6647
6648 # Check message types.
6649 switch -regexp -- $type {
6650 "answer" {
6651 return
6652 }
6653 "optional" {
6654 return
6655 }
6656 }
6657
6658 #Write to the log
6659 puts -nonewline $in_file "$message"
6660}
6661
408e9b8b
AH
6662# Write the command line used to invocate gdb to the cmd file.
6663
6664proc gdb_write_cmd_file { cmdline } {
6665 set logfile [standard_output_file_with_gdb_instance gdb.cmd]
6666 set cmd_file [open $logfile w]
6667 puts $cmd_file $cmdline
6668 catch "close $cmd_file"
6669}
6670
42159ca5
TT
6671# Always load compatibility stuff.
6672load_lib future.exp
This page took 2.717265 seconds and 4 git commands to generate.