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