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