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