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