gdb: testsuite: Support displaced stepping on LoongArch
[binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright 1992-2022 Free Software Foundation, Inc.
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
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
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.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
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
22 if {$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
28 # If GDB is built with ASAN (and because there are leaks), it will output a
29 # leak report when exiting as well as exit with a non-zero (failure) status.
30 # This can affect tests that are sensitive to what GDB prints on stderr or its
31 # exit status. Add `detect_leaks=0` to the ASAN_OPTIONS environment variable
32 # (which will affect any spawned sub-process) to avoid this.
33 append ::env(ASAN_OPTIONS) ",detect_leaks=0"
34
35 # List of procs to run in gdb_finish.
36 set gdb_finish_hooks [list]
37
38 # Variable in which we keep track of globals that are allowed to be live
39 # across test-cases.
40 array set gdb_persistent_globals {}
41
42 # Mark variable names in ARG as a persistent global, and declare them as
43 # global in the calling context. Can be used to rewrite "global var_a var_b"
44 # into "gdb_persistent_global var_a var_b".
45 proc gdb_persistent_global { args } {
46 global gdb_persistent_globals
47 foreach varname $args {
48 uplevel 1 global $varname
49 set gdb_persistent_globals($varname) 1
50 }
51 }
52
53 # Mark variable names in ARG as a persistent global.
54 proc gdb_persistent_global_no_decl { args } {
55 global gdb_persistent_globals
56 foreach varname $args {
57 set gdb_persistent_globals($varname) 1
58 }
59 }
60
61 # Override proc load_lib.
62 rename load_lib saved_load_lib
63 # Run the runtest version of load_lib, and mark all variables that were
64 # created by this call as persistent.
65 proc load_lib { file } {
66 array set known_global {}
67 foreach varname [info globals] {
68 set known_globals($varname) 1
69 }
70
71 set code [catch "saved_load_lib $file" result]
72
73 foreach varname [info globals] {
74 if { ![info exists known_globals($varname)] } {
75 gdb_persistent_global_no_decl $varname
76 }
77 }
78
79 if {$code == 1} {
80 global errorInfo errorCode
81 return -code error -errorinfo $errorInfo -errorcode $errorCode $result
82 } elseif {$code > 1} {
83 return -code $code $result
84 }
85
86 return $result
87 }
88
89 load_lib libgloss.exp
90 load_lib cache.exp
91 load_lib gdb-utils.exp
92 load_lib memory.exp
93 load_lib check-test-names.exp
94
95 # The path to the GDB binary to test.
96 global GDB
97
98 # The data directory to use for testing. If this is the empty string,
99 # then we let GDB use its own configured data directory.
100 global GDB_DATA_DIRECTORY
101
102 # The spawn ID used for I/O interaction with the inferior. For native
103 # targets, or remote targets that can do I/O through GDB
104 # (semi-hosting) this will be the same as the host/GDB's spawn ID.
105 # Otherwise, the board may set this to some other spawn ID. E.g.,
106 # when debugging with GDBserver, this is set to GDBserver's spawn ID,
107 # so input/output is done on gdbserver's tty.
108 global inferior_spawn_id
109
110 if [info exists TOOL_EXECUTABLE] {
111 set GDB $TOOL_EXECUTABLE
112 }
113 if ![info exists GDB] {
114 if ![is_remote host] {
115 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
116 } else {
117 set GDB [transform gdb]
118 }
119 } else {
120 # If the user specifies GDB on the command line, and doesn't
121 # specify GDB_DATA_DIRECTORY, then assume we're testing an
122 # installed GDB, and let it use its own configured data directory.
123 if ![info exists GDB_DATA_DIRECTORY] {
124 set GDB_DATA_DIRECTORY ""
125 }
126 }
127 verbose "using GDB = $GDB" 2
128
129 # The data directory the testing GDB will use. By default, assume
130 # we're testing a non-installed GDB in the build directory. Users may
131 # also explictly override the -data-directory from the command line.
132 if ![info exists GDB_DATA_DIRECTORY] {
133 set GDB_DATA_DIRECTORY "[pwd]/../data-directory"
134 }
135 verbose "using GDB_DATA_DIRECTORY = $GDB_DATA_DIRECTORY" 2
136
137 # GDBFLAGS is available for the user to set on the command line.
138 # E.g. make check RUNTESTFLAGS=GDBFLAGS=mumble
139 # Testcases may use it to add additional flags, but they must:
140 # - append new flags, not overwrite
141 # - restore the original value when done
142 global GDBFLAGS
143 if ![info exists GDBFLAGS] {
144 set GDBFLAGS ""
145 }
146 verbose "using GDBFLAGS = $GDBFLAGS" 2
147
148 # Append the -data-directory option to pass to GDB to CMDLINE and
149 # return the resulting string. If GDB_DATA_DIRECTORY is empty,
150 # nothing is appended.
151 proc append_gdb_data_directory_option {cmdline} {
152 global GDB_DATA_DIRECTORY
153
154 if { $GDB_DATA_DIRECTORY != "" } {
155 return "$cmdline -data-directory $GDB_DATA_DIRECTORY"
156 } else {
157 return $cmdline
158 }
159 }
160
161 # INTERNAL_GDBFLAGS contains flags that the testsuite requires.
162 # `-nw' disables any of the windowed interfaces.
163 # `-nx' disables ~/.gdbinit, so that it doesn't interfere with the tests.
164 # `-iex "set {height,width} 0"' disables pagination.
165 # `-data-directory' points to the data directory, usually in the build
166 # directory.
167 global INTERNAL_GDBFLAGS
168 if ![info exists INTERNAL_GDBFLAGS] {
169 set INTERNAL_GDBFLAGS \
170 [join [list \
171 "-nw" \
172 "-nx" \
173 {-iex "set height 0"} \
174 {-iex "set width 0"}]]
175
176 set INTERNAL_GDBFLAGS [append_gdb_data_directory_option $INTERNAL_GDBFLAGS]
177 }
178
179 # The variable gdb_prompt is a regexp which matches the gdb prompt.
180 # Set it if it is not already set. This is also set by default_gdb_init
181 # but it's not clear what removing one of them will break.
182 # See with_gdb_prompt for more details on prompt handling.
183 global gdb_prompt
184 if ![info exists gdb_prompt] then {
185 set gdb_prompt "\\(gdb\\)"
186 }
187
188 # A regexp that matches the pagination prompt.
189 set pagination_prompt \
190 "--Type <RET> for more, q to quit, c to continue without paging--"
191
192 # The variable fullname_syntax_POSIX is a regexp which matches a POSIX
193 # absolute path ie. /foo/
194 set fullname_syntax_POSIX {/[^\n]*/}
195 # The variable fullname_syntax_UNC is a regexp which matches a Windows
196 # UNC path ie. \\D\foo\
197 set fullname_syntax_UNC {\\\\[^\\]+\\[^\n]+\\}
198 # The variable fullname_syntax_DOS_CASE is a regexp which matches a
199 # particular DOS case that GDB most likely will output
200 # ie. \foo\, but don't match \\.*\
201 set fullname_syntax_DOS_CASE {\\[^\\][^\n]*\\}
202 # The variable fullname_syntax_DOS is a regexp which matches a DOS path
203 # ie. a:\foo\ && a:foo\
204 set fullname_syntax_DOS {[a-zA-Z]:[^\n]*\\}
205 # The variable fullname_syntax is a regexp which matches what GDB considers
206 # an absolute path. It is currently debatable if the Windows style paths
207 # d:foo and \abc should be considered valid as an absolute path.
208 # Also, the purpse of this regexp is not to recognize a well formed
209 # absolute path, but to say with certainty that a path is absolute.
210 set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
211
212 # Needed for some tests under Cygwin.
213 global EXEEXT
214 global env
215
216 if ![info exists env(EXEEXT)] {
217 set EXEEXT ""
218 } else {
219 set EXEEXT $env(EXEEXT)
220 }
221
222 set octal "\[0-7\]+"
223
224 set inferior_exited_re "(?:\\\[Inferior \[0-9\]+ \\(\[^\n\r\]*\\) exited)"
225
226 # A regular expression that matches a value history number.
227 # E.g., $1, $2, etc.
228 set valnum_re "\\\$$decimal"
229
230 ### Only procedures should come after this point.
231
232 #
233 # gdb_version -- extract and print the version number of GDB
234 #
235 proc default_gdb_version {} {
236 global GDB
237 global INTERNAL_GDBFLAGS GDBFLAGS
238 global gdb_prompt
239 global inotify_pid
240
241 if {[info exists inotify_pid]} {
242 eval exec kill $inotify_pid
243 }
244
245 set output [remote_exec host "$GDB $INTERNAL_GDBFLAGS --version"]
246 set tmp [lindex $output 1]
247 set version ""
248 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
249 if ![is_remote host] {
250 clone_output "[which $GDB] version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
251 } else {
252 clone_output "$GDB on remote host version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
253 }
254 }
255
256 proc gdb_version { } {
257 return [default_gdb_version]
258 }
259
260 #
261 # gdb_unload -- unload a file if one is loaded
262 # Return 0 on success, -1 on error.
263 #
264
265 proc gdb_unload {} {
266 global GDB
267 global gdb_prompt
268 send_gdb "file\n"
269 gdb_expect 60 {
270 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
271 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
272 -re "A program is being debugged already.*Are you sure you want to change the file.*y or n. $" {
273 send_gdb "y\n" answer
274 exp_continue
275 }
276 -re "Discard symbol table from .*y or n.*$" {
277 send_gdb "y\n" answer
278 exp_continue
279 }
280 -re "$gdb_prompt $" {}
281 -re "A problem internal to GDB has been detected" {
282 perror "Couldn't unload file in $GDB (GDB internal error)."
283 gdb_internal_error_resync
284 return -1
285 }
286 timeout {
287 perror "couldn't unload file in $GDB (timeout)."
288 return -1
289 }
290 }
291 return 0
292 }
293
294 # Many of the tests depend on setting breakpoints at various places and
295 # running until that breakpoint is reached. At times, we want to start
296 # with a clean-slate with respect to breakpoints, so this utility proc
297 # lets us do this without duplicating this code everywhere.
298 #
299
300 proc delete_breakpoints {} {
301 global gdb_prompt
302
303 # we need a larger timeout value here or this thing just confuses
304 # itself. May need a better implementation if possible. - guo
305 #
306 set timeout 100
307
308 set msg "delete all breakpoints in delete_breakpoints"
309 set deleted 0
310 gdb_test_multiple "delete breakpoints" "$msg" {
311 -re "Delete all breakpoints.*y or n.*$" {
312 send_gdb "y\n" answer
313 exp_continue
314 }
315 -re "$gdb_prompt $" {
316 set deleted 1
317 }
318 }
319
320 if {$deleted} {
321 # Confirm with "info breakpoints".
322 set deleted 0
323 set msg "info breakpoints"
324 gdb_test_multiple $msg $msg {
325 -re "No breakpoints or watchpoints..*$gdb_prompt $" {
326 set deleted 1
327 }
328 -re "$gdb_prompt $" {
329 }
330 }
331 }
332
333 if {!$deleted} {
334 perror "breakpoints not deleted"
335 }
336 }
337
338 # Returns true iff the target supports using the "run" command.
339
340 proc target_can_use_run_cmd {} {
341 if [target_info exists use_gdb_stub] {
342 # In this case, when we connect, the inferior is already
343 # running.
344 return 0
345 }
346
347 # Assume yes.
348 return 1
349 }
350
351 # Generic run command.
352 #
353 # Return 0 if we could start the program, -1 if we could not.
354 #
355 # The second pattern below matches up to the first newline *only*.
356 # Using ``.*$'' could swallow up output that we attempt to match
357 # elsewhere.
358 #
359 # INFERIOR_ARGS is passed as arguments to the start command, so may contain
360 # inferior arguments.
361 #
362 # N.B. This function does not wait for gdb to return to the prompt,
363 # that is the caller's responsibility.
364
365 proc gdb_run_cmd { {inferior_args {}} } {
366 global gdb_prompt use_gdb_stub
367
368 foreach command [gdb_init_commands] {
369 send_gdb "$command\n"
370 gdb_expect 30 {
371 -re "$gdb_prompt $" { }
372 default {
373 perror "gdb_init_command for target failed"
374 return
375 }
376 }
377 }
378
379 if $use_gdb_stub {
380 if [target_info exists gdb,do_reload_on_run] {
381 if { [gdb_reload $inferior_args] != 0 } {
382 return -1
383 }
384 send_gdb "continue\n"
385 gdb_expect 60 {
386 -re "Continu\[^\r\n\]*\[\r\n\]" {}
387 default {}
388 }
389 return 0
390 }
391
392 if [target_info exists gdb,start_symbol] {
393 set start [target_info gdb,start_symbol]
394 } else {
395 set start "start"
396 }
397 send_gdb "jump *$start\n"
398 set start_attempt 1
399 while { $start_attempt } {
400 # Cap (re)start attempts at three to ensure that this loop
401 # always eventually fails. Don't worry about trying to be
402 # clever and not send a command when it has failed.
403 if [expr $start_attempt > 3] {
404 perror "Jump to start() failed (retry count exceeded)"
405 return -1
406 }
407 set start_attempt [expr $start_attempt + 1]
408 gdb_expect 30 {
409 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
410 set start_attempt 0
411 }
412 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
413 perror "Can't find start symbol to run in gdb_run"
414 return -1
415 }
416 -re "No symbol \"start\" in current.*$gdb_prompt $" {
417 send_gdb "jump *_start\n"
418 }
419 -re "No symbol.*context.*$gdb_prompt $" {
420 set start_attempt 0
421 }
422 -re "Line.* Jump anyway.*y or n. $" {
423 send_gdb "y\n" answer
424 }
425 -re "The program is not being run.*$gdb_prompt $" {
426 if { [gdb_reload $inferior_args] != 0 } {
427 return -1
428 }
429 send_gdb "jump *$start\n"
430 }
431 timeout {
432 perror "Jump to start() failed (timeout)"
433 return -1
434 }
435 }
436 }
437
438 return 0
439 }
440
441 if [target_info exists gdb,do_reload_on_run] {
442 if { [gdb_reload $inferior_args] != 0 } {
443 return -1
444 }
445 }
446 send_gdb "run $inferior_args\n"
447 # This doesn't work quite right yet.
448 # Use -notransfer here so that test cases (like chng-sym.exp)
449 # may test for additional start-up messages.
450 gdb_expect 60 {
451 -re "The program .* has been started already.*y or n. $" {
452 send_gdb "y\n" answer
453 exp_continue
454 }
455 -notransfer -re "Starting program: \[^\r\n\]*" {}
456 -notransfer -re "$gdb_prompt $" {
457 # There is no more input expected.
458 }
459 -notransfer -re "A problem internal to GDB has been detected" {
460 # Let caller handle this.
461 }
462 }
463
464 return 0
465 }
466
467 # Generic start command. Return 0 if we could start the program, -1
468 # if we could not.
469 #
470 # INFERIOR_ARGS is passed as arguments to the start command, so may contain
471 # inferior arguments.
472 #
473 # N.B. This function does not wait for gdb to return to the prompt,
474 # that is the caller's responsibility.
475
476 proc gdb_start_cmd { {inferior_args {}} } {
477 global gdb_prompt use_gdb_stub
478
479 foreach command [gdb_init_commands] {
480 send_gdb "$command\n"
481 gdb_expect 30 {
482 -re "$gdb_prompt $" { }
483 default {
484 perror "gdb_init_command for target failed"
485 return -1
486 }
487 }
488 }
489
490 if $use_gdb_stub {
491 return -1
492 }
493
494 send_gdb "start $inferior_args\n"
495 # Use -notransfer here so that test cases (like chng-sym.exp)
496 # may test for additional start-up messages.
497 gdb_expect 60 {
498 -re "The program .* has been started already.*y or n. $" {
499 send_gdb "y\n" answer
500 exp_continue
501 }
502 -notransfer -re "Starting program: \[^\r\n\]*" {
503 return 0
504 }
505 }
506 return -1
507 }
508
509 # Generic starti command. Return 0 if we could start the program, -1
510 # if we could not.
511 #
512 # INFERIOR_ARGS is passed as arguments to the starti command, so may contain
513 # inferior arguments.
514 #
515 # N.B. This function does not wait for gdb to return to the prompt,
516 # that is the caller's responsibility.
517
518 proc gdb_starti_cmd { {inferior_args {}} } {
519 global gdb_prompt use_gdb_stub
520
521 foreach command [gdb_init_commands] {
522 send_gdb "$command\n"
523 gdb_expect 30 {
524 -re "$gdb_prompt $" { }
525 default {
526 perror "gdb_init_command for target failed"
527 return -1
528 }
529 }
530 }
531
532 if $use_gdb_stub {
533 return -1
534 }
535
536 send_gdb "starti $inferior_args\n"
537 gdb_expect 60 {
538 -re "The program .* has been started already.*y or n. $" {
539 send_gdb "y\n" answer
540 exp_continue
541 }
542 -re "Starting program: \[^\r\n\]*" {
543 return 0
544 }
545 }
546 return -1
547 }
548
549 # Set a breakpoint using LINESPEC.
550 #
551 # If there is an additional argument it is a list of options; the supported
552 # options are allow-pending, temporary, message, no-message and qualified.
553 #
554 # The result is 1 for success, 0 for failure.
555 #
556 # Note: The handling of message vs no-message is messed up, but it's based
557 # on historical usage. By default this function does not print passes,
558 # only fails.
559 # no-message: turns off printing of fails (and passes, but they're already off)
560 # message: turns on printing of passes (and fails, but they're already on)
561
562 proc gdb_breakpoint { linespec args } {
563 global gdb_prompt
564 global decimal
565
566 set pending_response n
567 if {[lsearch -exact $args allow-pending] != -1} {
568 set pending_response y
569 }
570
571 set break_command "break"
572 set break_message "Breakpoint"
573 if {[lsearch -exact $args temporary] != -1} {
574 set break_command "tbreak"
575 set break_message "Temporary breakpoint"
576 }
577
578 if {[lsearch -exact $args qualified] != -1} {
579 append break_command " -qualified"
580 }
581
582 set print_pass 0
583 set print_fail 1
584 set no_message_loc [lsearch -exact $args no-message]
585 set message_loc [lsearch -exact $args message]
586 # The last one to appear in args wins.
587 if { $no_message_loc > $message_loc } {
588 set print_fail 0
589 } elseif { $message_loc > $no_message_loc } {
590 set print_pass 1
591 }
592
593 set test_name "gdb_breakpoint: set breakpoint at $linespec"
594
595 send_gdb "$break_command $linespec\n"
596 # The first two regexps are what we get with -g, the third is without -g.
597 gdb_expect 30 {
598 -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
599 -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
600 -re "$break_message \[0-9\]* at .*$gdb_prompt $" {}
601 -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
602 if {$pending_response == "n"} {
603 if { $print_fail } {
604 fail $test_name
605 }
606 return 0
607 }
608 }
609 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
610 send_gdb "$pending_response\n"
611 exp_continue
612 }
613 -re "A problem internal to GDB has been detected" {
614 if { $print_fail } {
615 fail "$test_name (GDB internal error)"
616 }
617 gdb_internal_error_resync
618 return 0
619 }
620 -re "$gdb_prompt $" {
621 if { $print_fail } {
622 fail $test_name
623 }
624 return 0
625 }
626 eof {
627 perror "GDB process no longer exists"
628 global gdb_spawn_id
629 set wait_status [wait -i $gdb_spawn_id]
630 verbose -log "GDB process exited with wait status $wait_status"
631 if { $print_fail } {
632 fail "$test_name (eof)"
633 }
634 return 0
635 }
636 timeout {
637 if { $print_fail } {
638 fail "$test_name (timeout)"
639 }
640 return 0
641 }
642 }
643 if { $print_pass } {
644 pass $test_name
645 }
646 return 1
647 }
648
649 # Set breakpoint at function and run gdb until it breaks there.
650 # Since this is the only breakpoint that will be set, if it stops
651 # at a breakpoint, we will assume it is the one we want. We can't
652 # just compare to "function" because it might be a fully qualified,
653 # single quoted C++ function specifier.
654 #
655 # If there are additional arguments, pass them to gdb_breakpoint.
656 # We recognize no-message/message ourselves.
657 #
658 # no-message is messed up here, like gdb_breakpoint: to preserve
659 # historical usage fails are always printed by default.
660 # no-message: turns off printing of fails (and passes, but they're already off)
661 # message: turns on printing of passes (and fails, but they're already on)
662
663 proc runto { linespec args } {
664 global gdb_prompt
665 global decimal
666
667 delete_breakpoints
668
669 set print_pass 0
670 set print_fail 1
671 set no_message_loc [lsearch -exact $args no-message]
672 set message_loc [lsearch -exact $args message]
673 # The last one to appear in args wins.
674 if { $no_message_loc > $message_loc } {
675 set print_fail 0
676 } elseif { $message_loc > $no_message_loc } {
677 set print_pass 1
678 }
679
680 set test_name "runto: run to $linespec"
681
682 # We need to use eval here to pass our varargs args to gdb_breakpoint
683 # which is also a varargs function.
684 # But we also have to be careful because $linespec may have multiple
685 # elements, and we don't want Tcl to move the remaining elements after
686 # the first to $args. That is why $linespec is wrapped in {}.
687 if ![eval gdb_breakpoint {$linespec} $args] {
688 return 0
689 }
690
691 gdb_run_cmd
692
693 # the "at foo.c:36" output we get with -g.
694 # the "in func" output we get without -g.
695 gdb_expect 30 {
696 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
697 if { $print_pass } {
698 pass $test_name
699 }
700 return 1
701 }
702 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
703 if { $print_pass } {
704 pass $test_name
705 }
706 return 1
707 }
708 -re "The target does not support running in non-stop mode.\r\n$gdb_prompt $" {
709 if { $print_fail } {
710 unsupported "non-stop mode not supported"
711 }
712 return 0
713 }
714 -re ".*A problem internal to GDB has been detected" {
715 # Always emit a FAIL if we encounter an internal error: internal
716 # errors are never expected.
717 fail "$test_name (GDB internal error)"
718 gdb_internal_error_resync
719 return 0
720 }
721 -re "$gdb_prompt $" {
722 if { $print_fail } {
723 fail $test_name
724 }
725 return 0
726 }
727 eof {
728 if { $print_fail } {
729 fail "$test_name (eof)"
730 }
731 return 0
732 }
733 timeout {
734 if { $print_fail } {
735 fail "$test_name (timeout)"
736 }
737 return 0
738 }
739 }
740 if { $print_pass } {
741 pass $test_name
742 }
743 return 1
744 }
745
746 # Ask gdb to run until we hit a breakpoint at main.
747 #
748 # N.B. This function deletes all existing breakpoints.
749 # If you don't want that, use gdb_start_cmd.
750
751 proc runto_main { } {
752 return [runto main qualified]
753 }
754
755 ### Continue, and expect to hit a breakpoint.
756 ### Report a pass or fail, depending on whether it seems to have
757 ### worked. Use NAME as part of the test name; each call to
758 ### continue_to_breakpoint should use a NAME which is unique within
759 ### that test file.
760 proc gdb_continue_to_breakpoint {name {location_pattern .*}} {
761 global gdb_prompt
762 set full_name "continue to breakpoint: $name"
763
764 set kfail_pattern "Process record does not support instruction 0xfae64 at.*"
765 gdb_test_multiple "continue" $full_name {
766 -re "(?:Breakpoint|Temporary breakpoint) .* (at|in) $location_pattern\r\n$gdb_prompt $" {
767 pass $full_name
768 }
769 -re "\[\r\n\]*(?:$kfail_pattern)\[\r\n\]+$gdb_prompt $" {
770 kfail "gdb/25038" $full_name
771 }
772 }
773 }
774
775
776 # gdb_internal_error_resync:
777 #
778 # Answer the questions GDB asks after it reports an internal error
779 # until we get back to a GDB prompt. Decline to quit the debugging
780 # session, and decline to create a core file. Return non-zero if the
781 # resync succeeds.
782 #
783 # This procedure just answers whatever questions come up until it sees
784 # a GDB prompt; it doesn't require you to have matched the input up to
785 # any specific point. However, it only answers questions it sees in
786 # the output itself, so if you've matched a question, you had better
787 # answer it yourself before calling this.
788 #
789 # You can use this function thus:
790 #
791 # gdb_expect {
792 # ...
793 # -re ".*A problem internal to GDB has been detected" {
794 # gdb_internal_error_resync
795 # }
796 # ...
797 # }
798 #
799 proc gdb_internal_error_resync {} {
800 global gdb_prompt
801
802 verbose -log "Resyncing due to internal error."
803
804 set count 0
805 while {$count < 10} {
806 gdb_expect {
807 -re "Recursive internal problem\\." {
808 perror "Could not resync from internal error (recursive internal problem)"
809 return 0
810 }
811 -re "Quit this debugging session\\? \\(y or n\\) $" {
812 send_gdb "n\n" answer
813 incr count
814 }
815 -re "Create a core file of GDB\\? \\(y or n\\) $" {
816 send_gdb "n\n" answer
817 incr count
818 }
819 -re "$gdb_prompt $" {
820 # We're resynchronized.
821 return 1
822 }
823 timeout {
824 perror "Could not resync from internal error (timeout)"
825 return 0
826 }
827 eof {
828 perror "Could not resync from internal error (eof)"
829 return 0
830 }
831 }
832 }
833 perror "Could not resync from internal error (resync count exceeded)"
834 return 0
835 }
836
837
838 # gdb_test_multiple COMMAND MESSAGE [ -prompt PROMPT_REGEXP] [ -lbl ]
839 # EXPECT_ARGUMENTS
840 # Send a command to gdb; test the result.
841 #
842 # COMMAND is the command to execute, send to GDB with send_gdb. If
843 # this is the null string no command is sent.
844 # MESSAGE is a message to be printed with the built-in failure patterns
845 # if one of them matches. If MESSAGE is empty COMMAND will be used.
846 # -prompt PROMPT_REGEXP specifies a regexp matching the expected prompt
847 # after the command output. If empty, defaults to "$gdb_prompt $".
848 # -lbl specifies that line-by-line matching will be used.
849 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
850 # patterns. Pattern elements will be evaluated in the caller's
851 # context; action elements will be executed in the caller's context.
852 # Unlike patterns for gdb_test, these patterns should generally include
853 # the final newline and prompt.
854 #
855 # Returns:
856 # 1 if the test failed, according to a built-in failure pattern
857 # 0 if only user-supplied patterns matched
858 # -1 if there was an internal error.
859 #
860 # You can use this function thus:
861 #
862 # gdb_test_multiple "print foo" "test foo" {
863 # -re "expected output 1" {
864 # pass "test foo"
865 # }
866 # -re "expected output 2" {
867 # fail "test foo"
868 # }
869 # }
870 #
871 # Within action elements you can also make use of the variable
872 # gdb_test_name. This variable is setup automatically by
873 # gdb_test_multiple, and contains the value of MESSAGE. You can then
874 # write this, which is equivalent to the above:
875 #
876 # gdb_test_multiple "print foo" "test foo" {
877 # -re "expected output 1" {
878 # pass $gdb_test_name
879 # }
880 # -re "expected output 2" {
881 # fail $gdb_test_name
882 # }
883 # }
884 #
885 # Like with "expect", you can also specify the spawn id to match with
886 # -i "$id". Interesting spawn ids are $inferior_spawn_id and
887 # $gdb_spawn_id. The former matches inferior I/O, while the latter
888 # matches GDB I/O. E.g.:
889 #
890 # send_inferior "hello\n"
891 # gdb_test_multiple "continue" "test echo" {
892 # -i "$inferior_spawn_id" -re "^hello\r\nhello\r\n$" {
893 # pass "got echo"
894 # }
895 # -i "$gdb_spawn_id" -re "Breakpoint.*$gdb_prompt $" {
896 # fail "hit breakpoint"
897 # }
898 # }
899 #
900 # The standard patterns, such as "Inferior exited..." and "A problem
901 # ...", all being implicitly appended to that list. These are always
902 # expected from $gdb_spawn_id. IOW, callers do not need to worry
903 # about resetting "-i" back to $gdb_spawn_id explicitly.
904 #
905 # In EXPECT_ARGUMENTS we can use a -wrap pattern flag, that wraps the regexp
906 # pattern as gdb_test wraps its message argument.
907 # This allows us to rewrite:
908 # gdb_test <command> <pattern> <message>
909 # into:
910 # gdb_test_multiple <command> <message> {
911 # -re -wrap <pattern> {
912 # pass $gdb_test_name
913 # }
914 # }
915 #
916 # In EXPECT_ARGUMENTS, a pattern flag -early can be used. It makes sure the
917 # pattern is inserted before any implicit pattern added by gdb_test_multiple.
918 # Using this pattern flag, we can f.i. setup a kfail for an assertion failure
919 # <assert> during gdb_continue_to_breakpoint by the rewrite:
920 # gdb_continue_to_breakpoint <msg> <pattern>
921 # into:
922 # set breakpoint_pattern "(?:Breakpoint|Temporary breakpoint) .* (at|in)"
923 # gdb_test_multiple "continue" "continue to breakpoint: <msg>" {
924 # -early -re "internal-error: <assert>" {
925 # setup_kfail gdb/nnnnn "*-*-*"
926 # exp_continue
927 # }
928 # -re "$breakpoint_pattern <pattern>\r\n$gdb_prompt $" {
929 # pass $gdb_test_name
930 # }
931 # }
932 #
933 proc gdb_test_multiple { command message args } {
934 global verbose use_gdb_stub
935 global gdb_prompt pagination_prompt
936 global GDB
937 global gdb_spawn_id
938 global inferior_exited_re
939 upvar timeout timeout
940 upvar expect_out expect_out
941 global any_spawn_id
942
943 set line_by_line 0
944 set prompt_regexp ""
945 for {set i 0} {$i < [llength $args]} {incr i} {
946 set arg [lindex $args $i]
947 if { $arg == "-prompt" } {
948 incr i
949 set prompt_regexp [lindex $args $i]
950 } elseif { $arg == "-lbl" } {
951 set line_by_line 1
952 } else {
953 set user_code $arg
954 break
955 }
956 }
957 if { [expr $i + 1] < [llength $args] } {
958 error "Too many arguments to gdb_test_multiple"
959 } elseif { ![info exists user_code] } {
960 error "Too few arguments to gdb_test_multiple"
961 }
962
963 if { "$prompt_regexp" == "" } {
964 set prompt_regexp "$gdb_prompt $"
965 }
966
967 if { $message == "" } {
968 set message $command
969 }
970
971 if [string match "*\[\r\n\]" $command] {
972 error "Invalid trailing newline in \"$message\" test"
973 }
974
975 if [string match "*\[\r\n\]*" $message] {
976 error "Invalid newline in \"$message\" test"
977 }
978
979 if {$use_gdb_stub
980 && [regexp -nocase {^\s*(r|run|star|start|at|att|atta|attac|attach)\M} \
981 $command]} {
982 error "gdbserver does not support $command without extended-remote"
983 }
984
985 # TCL/EXPECT WART ALERT
986 # Expect does something very strange when it receives a single braced
987 # argument. It splits it along word separators and performs substitutions.
988 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
989 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
990 # double-quoted list item, "\[ab\]" is just a long way of representing
991 # "[ab]", because the backslashes will be removed by lindex.
992
993 # Unfortunately, there appears to be no easy way to duplicate the splitting
994 # that expect will do from within TCL. And many places make use of the
995 # "\[0-9\]" construct, so we need to support that; and some places make use
996 # of the "[func]" construct, so we need to support that too. In order to
997 # get this right we have to substitute quoted list elements differently
998 # from braced list elements.
999
1000 # We do this roughly the same way that Expect does it. We have to use two
1001 # lists, because if we leave unquoted newlines in the argument to uplevel
1002 # they'll be treated as command separators, and if we escape newlines
1003 # we mangle newlines inside of command blocks. This assumes that the
1004 # input doesn't contain a pattern which contains actual embedded newlines
1005 # at this point!
1006
1007 regsub -all {\n} ${user_code} { } subst_code
1008 set subst_code [uplevel list $subst_code]
1009
1010 set processed_code ""
1011 set early_processed_code ""
1012 # The variable current_list holds the name of the currently processed
1013 # list, either processed_code or early_processed_code.
1014 set current_list "processed_code"
1015 set patterns ""
1016 set expecting_action 0
1017 set expecting_arg 0
1018 set wrap_pattern 0
1019 foreach item $user_code subst_item $subst_code {
1020 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
1021 lappend $current_list $item
1022 continue
1023 }
1024 if { $item == "-indices" || $item == "-re" || $item == "-ex" } {
1025 lappend $current_list $item
1026 continue
1027 }
1028 if { $item == "-early" } {
1029 set current_list "early_processed_code"
1030 continue
1031 }
1032 if { $item == "-timeout" || $item == "-i" } {
1033 set expecting_arg 1
1034 lappend $current_list $item
1035 continue
1036 }
1037 if { $item == "-wrap" } {
1038 set wrap_pattern 1
1039 continue
1040 }
1041 if { $expecting_arg } {
1042 set expecting_arg 0
1043 lappend $current_list $subst_item
1044 continue
1045 }
1046 if { $expecting_action } {
1047 lappend $current_list "uplevel [list $item]"
1048 set expecting_action 0
1049 # Cosmetic, no effect on the list.
1050 append $current_list "\n"
1051 # End the effect of -early, it only applies to one action.
1052 set current_list "processed_code"
1053 continue
1054 }
1055 set expecting_action 1
1056 if { $wrap_pattern } {
1057 # Wrap subst_item as is done for the gdb_test PATTERN argument.
1058 lappend $current_list \
1059 "\[\r\n\]*(?:$subst_item)\[\r\n\]+$gdb_prompt $"
1060 set wrap_pattern 0
1061 } else {
1062 lappend $current_list $subst_item
1063 }
1064 if {$patterns != ""} {
1065 append patterns "; "
1066 }
1067 append patterns "\"$subst_item\""
1068 }
1069
1070 # Also purely cosmetic.
1071 regsub -all {\r} $patterns {\\r} patterns
1072 regsub -all {\n} $patterns {\\n} patterns
1073
1074 if $verbose>2 then {
1075 send_user "Sending \"$command\" to gdb\n"
1076 send_user "Looking to match \"$patterns\"\n"
1077 send_user "Message is \"$message\"\n"
1078 }
1079
1080 set result -1
1081 set string "${command}\n"
1082 if { $command != "" } {
1083 set multi_line_re "\[\r\n\] *>"
1084 while { "$string" != "" } {
1085 set foo [string first "\n" "$string"]
1086 set len [string length "$string"]
1087 if { $foo < [expr $len - 1] } {
1088 set str [string range "$string" 0 $foo]
1089 if { [send_gdb "$str"] != "" } {
1090 perror "Couldn't send $command to GDB."
1091 }
1092 # since we're checking if each line of the multi-line
1093 # command are 'accepted' by GDB here,
1094 # we need to set -notransfer expect option so that
1095 # command output is not lost for pattern matching
1096 # - guo
1097 gdb_expect 2 {
1098 -notransfer -re "$multi_line_re$" { verbose "partial: match" 3 }
1099 timeout { verbose "partial: timeout" 3 }
1100 }
1101 set string [string range "$string" [expr $foo + 1] end]
1102 set multi_line_re "$multi_line_re.*\[\r\n\] *>"
1103 } else {
1104 break
1105 }
1106 }
1107 if { "$string" != "" } {
1108 if { [send_gdb "$string"] != "" } {
1109 perror "Couldn't send $command to GDB."
1110 }
1111 }
1112 }
1113
1114 set code $early_processed_code
1115 append code {
1116 -re ".*A problem internal to GDB has been detected" {
1117 fail "$message (GDB internal error)"
1118 gdb_internal_error_resync
1119 set result -1
1120 }
1121 -re "\\*\\*\\* DOSEXIT code.*" {
1122 if { $message != "" } {
1123 fail "$message"
1124 }
1125 set result -1
1126 }
1127 }
1128 append code $processed_code
1129
1130 # Reset the spawn id, in case the processed code used -i.
1131 append code {
1132 -i "$gdb_spawn_id"
1133 }
1134
1135 append code {
1136 -re "Ending remote debugging.*$prompt_regexp" {
1137 if ![isnative] then {
1138 warning "Can`t communicate to remote target."
1139 }
1140 gdb_exit
1141 gdb_start
1142 set result -1
1143 }
1144 -re "Undefined\[a-z\]* command:.*$prompt_regexp" {
1145 perror "Undefined command \"$command\"."
1146 fail "$message"
1147 set result 1
1148 }
1149 -re "Ambiguous command.*$prompt_regexp" {
1150 perror "\"$command\" is not a unique command name."
1151 fail "$message"
1152 set result 1
1153 }
1154 -re "$inferior_exited_re with code \[0-9\]+.*$prompt_regexp" {
1155 if ![string match "" $message] then {
1156 set errmsg "$message (the program exited)"
1157 } else {
1158 set errmsg "$command (the program exited)"
1159 }
1160 fail "$errmsg"
1161 set result -1
1162 }
1163 -re "$inferior_exited_re normally.*$prompt_regexp" {
1164 if ![string match "" $message] then {
1165 set errmsg "$message (the program exited)"
1166 } else {
1167 set errmsg "$command (the program exited)"
1168 }
1169 fail "$errmsg"
1170 set result -1
1171 }
1172 -re "The program is not being run.*$prompt_regexp" {
1173 if ![string match "" $message] then {
1174 set errmsg "$message (the program is no longer running)"
1175 } else {
1176 set errmsg "$command (the program is no longer running)"
1177 }
1178 fail "$errmsg"
1179 set result -1
1180 }
1181 -re "\r\n$prompt_regexp" {
1182 if ![string match "" $message] then {
1183 fail "$message"
1184 }
1185 set result 1
1186 }
1187 -re "$pagination_prompt" {
1188 send_gdb "\n"
1189 perror "Window too small."
1190 fail "$message"
1191 set result -1
1192 }
1193 -re "\\((y or n|y or \\\[n\\\]|\\\[y\\\] or n)\\) " {
1194 send_gdb "n\n" answer
1195 gdb_expect -re "$prompt_regexp"
1196 fail "$message (got interactive prompt)"
1197 set result -1
1198 }
1199 -re "\\\[0\\\] cancel\r\n\\\[1\\\] all.*\r\n> $" {
1200 send_gdb "0\n"
1201 gdb_expect -re "$prompt_regexp"
1202 fail "$message (got breakpoint menu)"
1203 set result -1
1204 }
1205
1206 -i $gdb_spawn_id
1207 eof {
1208 perror "GDB process no longer exists"
1209 set wait_status [wait -i $gdb_spawn_id]
1210 verbose -log "GDB process exited with wait status $wait_status"
1211 if { $message != "" } {
1212 fail "$message"
1213 }
1214 return -1
1215 }
1216 }
1217
1218 if {$line_by_line} {
1219 append code {
1220 -re "\r\n\[^\r\n\]*(?=\r\n)" {
1221 exp_continue
1222 }
1223 }
1224 }
1225
1226 # Now patterns that apply to any spawn id specified.
1227 append code {
1228 -i $any_spawn_id
1229 eof {
1230 perror "Process no longer exists"
1231 if { $message != "" } {
1232 fail "$message"
1233 }
1234 return -1
1235 }
1236 full_buffer {
1237 perror "internal buffer is full."
1238 fail "$message"
1239 set result -1
1240 }
1241 timeout {
1242 if ![string match "" $message] then {
1243 fail "$message (timeout)"
1244 }
1245 set result 1
1246 }
1247 }
1248
1249 # remote_expect calls the eof section if there is an error on the
1250 # expect call. We already have eof sections above, and we don't
1251 # want them to get called in that situation. Since the last eof
1252 # section becomes the error section, here we define another eof
1253 # section, but with an empty spawn_id list, so that it won't ever
1254 # match.
1255 append code {
1256 -i "" eof {
1257 # This comment is here because the eof section must not be
1258 # the empty string, otherwise remote_expect won't realize
1259 # it exists.
1260 }
1261 }
1262
1263 # Create gdb_test_name in the parent scope. If this variable
1264 # already exists, which it might if we have nested calls to
1265 # gdb_test_multiple, then preserve the old value, otherwise,
1266 # create a new variable in the parent scope.
1267 upvar gdb_test_name gdb_test_name
1268 if { [info exists gdb_test_name] } {
1269 set gdb_test_name_old "$gdb_test_name"
1270 }
1271 set gdb_test_name "$message"
1272
1273 set result 0
1274 set code [catch {gdb_expect $code} string]
1275
1276 # Clean up the gdb_test_name variable. If we had a
1277 # previous value then restore it, otherwise, delete the variable
1278 # from the parent scope.
1279 if { [info exists gdb_test_name_old] } {
1280 set gdb_test_name "$gdb_test_name_old"
1281 } else {
1282 unset gdb_test_name
1283 }
1284
1285 if {$code == 1} {
1286 global errorInfo errorCode
1287 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1288 } elseif {$code > 1} {
1289 return -code $code $string
1290 }
1291 return $result
1292 }
1293
1294 # Usage: gdb_test_multiline NAME INPUT RESULT {INPUT RESULT} ...
1295 # Run a test named NAME, consisting of multiple lines of input.
1296 # After each input line INPUT, search for result line RESULT.
1297 # Succeed if all results are seen; fail otherwise.
1298
1299 proc gdb_test_multiline { name args } {
1300 global gdb_prompt
1301 set inputnr 0
1302 foreach {input result} $args {
1303 incr inputnr
1304 if {[gdb_test_multiple $input "$name: input $inputnr: $input" {
1305 -re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" {
1306 pass $gdb_test_name
1307 }
1308 }]} {
1309 return 1
1310 }
1311 }
1312 return 0
1313 }
1314
1315
1316 # gdb_test [-prompt PROMPT_REGEXP] [-lbl]
1317 # COMMAND [PATTERN] [MESSAGE] [QUESTION RESPONSE]
1318 # Send a command to gdb; test the result.
1319 #
1320 # COMMAND is the command to execute, send to GDB with send_gdb. If
1321 # this is the null string no command is sent.
1322 # PATTERN is the pattern to match for a PASS, and must NOT include
1323 # the \r\n sequence immediately before the gdb prompt. This argument
1324 # may be omitted to just match the prompt, ignoring whatever output
1325 # precedes it.
1326 # MESSAGE is an optional message to be printed. If this is
1327 # omitted, then the pass/fail messages use the command string as the
1328 # message. (If this is the empty string, then sometimes we don't
1329 # call pass or fail at all; I don't understand this at all.)
1330 # QUESTION is a question GDB should ask in response to COMMAND, like
1331 # "are you sure?" If this is specified, the test fails if GDB
1332 # doesn't print the question.
1333 # RESPONSE is the response to send when QUESTION appears.
1334 #
1335 # -prompt PROMPT_REGEXP specifies a regexp matching the expected prompt
1336 # after the command output. If empty, defaults to "$gdb_prompt $".
1337 # -lbl specifies that line-by-line matching will be used.
1338 #
1339 # Returns:
1340 # 1 if the test failed,
1341 # 0 if the test passes,
1342 # -1 if there was an internal error.
1343 #
1344 proc gdb_test { args } {
1345 global gdb_prompt
1346 upvar timeout timeout
1347
1348 parse_args {
1349 {prompt ""}
1350 {lbl}
1351 }
1352
1353 lassign $args command pattern message question response
1354
1355 # Can't have a question without a response.
1356 if { $question != "" && $response == "" || [llength $args] > 5 } {
1357 error "Unexpected arguments: $args"
1358 }
1359
1360 if { $message == "" } {
1361 set message $command
1362 }
1363
1364 if { $prompt == "" } {
1365 set prompt "$gdb_prompt $"
1366 }
1367
1368 set saw_question 0
1369
1370 set user_code {}
1371 lappend user_code {
1372 -re "\[\r\n\]*(?:$pattern)\[\r\n\]+$prompt" {
1373 if ![string match "" $message] then {
1374 if { $question != "" } {
1375 gdb_assert $saw_question "$message"
1376 } else {
1377 pass "$message"
1378 }
1379 }
1380 }
1381 }
1382
1383 if { $question != "" } {
1384 lappend user_code {
1385 -re "$question$" {
1386 set saw_question 1
1387 send_gdb "$response\n"
1388 exp_continue
1389 }
1390 }
1391 }
1392
1393 set user_code [join $user_code]
1394
1395 set opts {}
1396 lappend "-prompt $prompt"
1397 if {$lbl} {
1398 lappend opts "-lbl"
1399 }
1400
1401 return [gdb_test_multiple $command $message {*}$opts $user_code]
1402 }
1403
1404 # Return 1 if version MAJOR.MINOR is at least AT_LEAST_MAJOR.AT_LEAST_MINOR.
1405 proc version_at_least { major minor at_least_major at_least_minor} {
1406 if { $major > $at_least_major } {
1407 return 1
1408 } elseif { $major == $at_least_major \
1409 && $minor >= $at_least_minor } {
1410 return 1
1411 } else {
1412 return 0
1413 }
1414 }
1415
1416 # Return 1 if tcl version used is at least MAJOR.MINOR
1417 proc tcl_version_at_least { major minor } {
1418 global tcl_version
1419 regexp {^([0-9]+)\.([0-9]+)$} $tcl_version \
1420 dummy tcl_version_major tcl_version_minor
1421 return [version_at_least $tcl_version_major $tcl_version_minor \
1422 $major $minor]
1423 }
1424
1425 if { [tcl_version_at_least 8 5] == 0 } {
1426 # lrepeat was added in tcl 8.5. Only add if missing.
1427 proc lrepeat { n element } {
1428 if { [string is integer -strict $n] == 0 } {
1429 error "expected integer but got \"$n\""
1430 }
1431 if { $n < 0 } {
1432 error "bad count \"$n\": must be integer >= 0"
1433 }
1434 set res [list]
1435 for {set i 0} {$i < $n} {incr i} {
1436 lappend res $element
1437 }
1438 return $res
1439 }
1440 }
1441
1442 # gdb_test_no_output COMMAND MESSAGE
1443 # Send a command to GDB and verify that this command generated no output.
1444 #
1445 # See gdb_test_multiple for a description of the COMMAND and MESSAGE
1446 # parameters. If MESSAGE is ommitted, then COMMAND will be used as
1447 # the message. (If MESSAGE is the empty string, then sometimes we do not
1448 # call pass or fail at all; I don't understand this at all.)
1449
1450 proc gdb_test_no_output { args } {
1451 global gdb_prompt
1452 set command [lindex $args 0]
1453 if [llength $args]>1 then {
1454 set message [lindex $args 1]
1455 } else {
1456 set message $command
1457 }
1458
1459 set command_regex [string_to_regexp $command]
1460 gdb_test_multiple $command $message {
1461 -re "^$command_regex\r\n$gdb_prompt $" {
1462 if ![string match "" $message] then {
1463 pass "$message"
1464 }
1465 }
1466 }
1467 }
1468
1469 # Send a command and then wait for a sequence of outputs.
1470 # This is useful when the sequence is long and contains ".*", a single
1471 # regexp to match the entire output can get a timeout much easier.
1472 #
1473 # COMMAND is the command to execute, send to GDB with send_gdb. If
1474 # this is the null string no command is sent.
1475 # TEST_NAME is passed to pass/fail. COMMAND is used if TEST_NAME is "".
1476 # EXPECTED_OUTPUT_LIST is a list of regexps of expected output, which are
1477 # processed in order, and all must be present in the output.
1478 #
1479 # The -prompt switch can be used to override the prompt expected at the end of
1480 # the output sequence.
1481 #
1482 # It is unnecessary to specify ".*" at the beginning or end of any regexp,
1483 # there is an implicit ".*" between each element of EXPECTED_OUTPUT_LIST.
1484 # There is also an implicit ".*" between the last regexp and the gdb prompt.
1485 #
1486 # Like gdb_test and gdb_test_multiple, the output is expected to end with the
1487 # gdb prompt, which must not be specified in EXPECTED_OUTPUT_LIST.
1488 #
1489 # Returns:
1490 # 1 if the test failed,
1491 # 0 if the test passes,
1492 # -1 if there was an internal error.
1493
1494 proc gdb_test_sequence { args } {
1495 global gdb_prompt
1496
1497 parse_args {{prompt ""}}
1498
1499 if { $prompt == "" } {
1500 set prompt "$gdb_prompt $"
1501 }
1502
1503 if { [llength $args] != 3 } {
1504 error "Unexpected # of arguments, expecting: COMMAND TEST_NAME EXPECTED_OUTPUT_LIST"
1505 }
1506
1507 lassign $args command test_name expected_output_list
1508
1509 if { $test_name == "" } {
1510 set test_name $command
1511 }
1512
1513 lappend expected_output_list ""; # implicit ".*" before gdb prompt
1514
1515 if { $command != "" } {
1516 send_gdb "$command\n"
1517 }
1518
1519 return [gdb_expect_list $test_name $prompt $expected_output_list]
1520 }
1521
1522 \f
1523 # Match output of COMMAND using RE. Read output line-by-line.
1524 # Report pass/fail with MESSAGE.
1525 # For a command foo with output:
1526 # (gdb) foo^M
1527 # <line1>^M
1528 # <line2>^M
1529 # (gdb)
1530 # the portion matched using RE is:
1531 # '<line1>^M
1532 # <line2>^M
1533 # '
1534 #
1535 # Optionally, additional -re-not <regexp> arguments can be specified, to
1536 # ensure that a regexp is not match by the COMMAND output.
1537 # Such an additional argument generates an additional PASS/FAIL of the form:
1538 # PASS: test-case.exp: $message: pattern not matched: <regexp>
1539
1540 proc gdb_test_lines { command message re args } {
1541 set re_not [list]
1542
1543 for {set i 0} {$i < [llength $args]} {incr i} {
1544 set arg [lindex $args $i]
1545 if { $arg == "-re-not" } {
1546 incr i
1547 if { [llength $args] == $i } {
1548 error "Missing argument for -re-not"
1549 break
1550 }
1551 set arg [lindex $args $i]
1552 lappend re_not $arg
1553 } else {
1554 error "Unhandled argument: $arg"
1555 }
1556 }
1557
1558 if { $message == ""} {
1559 set message $command
1560 }
1561
1562 set lines ""
1563 gdb_test_multiple $command $message {
1564 -re "\r\n(\[^\r\n\]*)(?=\r\n)" {
1565 set line $expect_out(1,string)
1566 if { $lines eq "" } {
1567 append lines "$line"
1568 } else {
1569 append lines "\r\n$line"
1570 }
1571 exp_continue
1572 }
1573 -re -wrap "" {
1574 append lines "\r\n"
1575 }
1576 }
1577
1578 gdb_assert { [regexp $re $lines] } $message
1579
1580 foreach re $re_not {
1581 gdb_assert { ![regexp $re $lines] } "$message: pattern not matched: $re"
1582 }
1583 }
1584
1585 # Test that a command gives an error. For pass or fail, return
1586 # a 1 to indicate that more tests can proceed. However a timeout
1587 # is a serious error, generates a special fail message, and causes
1588 # a 0 to be returned to indicate that more tests are likely to fail
1589 # as well.
1590
1591 proc test_print_reject { args } {
1592 global gdb_prompt
1593 global verbose
1594
1595 if [llength $args]==2 then {
1596 set expectthis [lindex $args 1]
1597 } else {
1598 set expectthis "should never match this bogus string"
1599 }
1600 set sendthis [lindex $args 0]
1601 if $verbose>2 then {
1602 send_user "Sending \"$sendthis\" to gdb\n"
1603 send_user "Looking to match \"$expectthis\"\n"
1604 }
1605 send_gdb "$sendthis\n"
1606 #FIXME: Should add timeout as parameter.
1607 gdb_expect {
1608 -re "A .* in expression.*\\.*$gdb_prompt $" {
1609 pass "reject $sendthis"
1610 return 1
1611 }
1612 -re "Invalid syntax in expression.*$gdb_prompt $" {
1613 pass "reject $sendthis"
1614 return 1
1615 }
1616 -re "Junk after end of expression.*$gdb_prompt $" {
1617 pass "reject $sendthis"
1618 return 1
1619 }
1620 -re "Invalid number.*$gdb_prompt $" {
1621 pass "reject $sendthis"
1622 return 1
1623 }
1624 -re "Invalid character constant.*$gdb_prompt $" {
1625 pass "reject $sendthis"
1626 return 1
1627 }
1628 -re "No symbol table is loaded.*$gdb_prompt $" {
1629 pass "reject $sendthis"
1630 return 1
1631 }
1632 -re "No symbol .* in current context.*$gdb_prompt $" {
1633 pass "reject $sendthis"
1634 return 1
1635 }
1636 -re "Unmatched single quote.*$gdb_prompt $" {
1637 pass "reject $sendthis"
1638 return 1
1639 }
1640 -re "A character constant must contain at least one character.*$gdb_prompt $" {
1641 pass "reject $sendthis"
1642 return 1
1643 }
1644 -re "$expectthis.*$gdb_prompt $" {
1645 pass "reject $sendthis"
1646 return 1
1647 }
1648 -re ".*$gdb_prompt $" {
1649 fail "reject $sendthis"
1650 return 1
1651 }
1652 default {
1653 fail "reject $sendthis (eof or timeout)"
1654 return 0
1655 }
1656 }
1657 }
1658 \f
1659
1660 # Same as gdb_test, but the second parameter is not a regexp,
1661 # but a string that must match exactly.
1662
1663 proc gdb_test_exact { args } {
1664 upvar timeout timeout
1665
1666 set command [lindex $args 0]
1667
1668 # This applies a special meaning to a null string pattern. Without
1669 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
1670 # messages from commands that should have no output except a new
1671 # prompt. With this, only results of a null string will match a null
1672 # string pattern.
1673
1674 set pattern [lindex $args 1]
1675 if [string match $pattern ""] {
1676 set pattern [string_to_regexp [lindex $args 0]]
1677 } else {
1678 set pattern [string_to_regexp [lindex $args 1]]
1679 }
1680
1681 # It is most natural to write the pattern argument with only
1682 # embedded \n's, especially if you are trying to avoid Tcl quoting
1683 # problems. But gdb_expect really wants to see \r\n in patterns. So
1684 # transform the pattern here. First transform \r\n back to \n, in
1685 # case some users of gdb_test_exact already do the right thing.
1686 regsub -all "\r\n" $pattern "\n" pattern
1687 regsub -all "\n" $pattern "\r\n" pattern
1688 if [llength $args]==3 then {
1689 set message [lindex $args 2]
1690 return [gdb_test $command $pattern $message]
1691 }
1692
1693 return [gdb_test $command $pattern]
1694 }
1695
1696 # Wrapper around gdb_test_multiple that looks for a list of expected
1697 # output elements, but which can appear in any order.
1698 # CMD is the gdb command.
1699 # NAME is the name of the test.
1700 # ELM_FIND_REGEXP specifies how to partition the output into elements to
1701 # compare.
1702 # ELM_EXTRACT_REGEXP specifies the part of ELM_FIND_REGEXP to compare.
1703 # RESULT_MATCH_LIST is a list of exact matches for each expected element.
1704 # All elements of RESULT_MATCH_LIST must appear for the test to pass.
1705 #
1706 # A typical use of ELM_FIND_REGEXP/ELM_EXTRACT_REGEXP is to extract one line
1707 # of text per element and then strip trailing \r\n's.
1708 # Example:
1709 # gdb_test_list_exact "foo" "bar" \
1710 # "\[^\r\n\]+\[\r\n\]+" \
1711 # "\[^\r\n\]+" \
1712 # { \
1713 # {expected result 1} \
1714 # {expected result 2} \
1715 # }
1716
1717 proc gdb_test_list_exact { cmd name elm_find_regexp elm_extract_regexp result_match_list } {
1718 global gdb_prompt
1719
1720 set matches [lsort $result_match_list]
1721 set seen {}
1722 gdb_test_multiple $cmd $name {
1723 "$cmd\[\r\n\]" { exp_continue }
1724 -re $elm_find_regexp {
1725 set str $expect_out(0,string)
1726 verbose -log "seen: $str" 3
1727 regexp -- $elm_extract_regexp $str elm_seen
1728 verbose -log "extracted: $elm_seen" 3
1729 lappend seen $elm_seen
1730 exp_continue
1731 }
1732 -re "$gdb_prompt $" {
1733 set failed ""
1734 foreach got [lsort $seen] have $matches {
1735 if {![string equal $got $have]} {
1736 set failed $have
1737 break
1738 }
1739 }
1740 if {[string length $failed] != 0} {
1741 fail "$name ($failed not found)"
1742 } else {
1743 pass $name
1744 }
1745 }
1746 }
1747 }
1748
1749 # gdb_test_stdio COMMAND INFERIOR_PATTERN GDB_PATTERN MESSAGE
1750 # Send a command to gdb; expect inferior and gdb output.
1751 #
1752 # See gdb_test_multiple for a description of the COMMAND and MESSAGE
1753 # parameters.
1754 #
1755 # INFERIOR_PATTERN is the pattern to match against inferior output.
1756 #
1757 # GDB_PATTERN is the pattern to match against gdb output, and must NOT
1758 # include the \r\n sequence immediately before the gdb prompt, nor the
1759 # prompt. The default is empty.
1760 #
1761 # Both inferior and gdb patterns must match for a PASS.
1762 #
1763 # If MESSAGE is ommitted, then COMMAND will be used as the message.
1764 #
1765 # Returns:
1766 # 1 if the test failed,
1767 # 0 if the test passes,
1768 # -1 if there was an internal error.
1769 #
1770
1771 proc gdb_test_stdio {command inferior_pattern {gdb_pattern ""} {message ""}} {
1772 global inferior_spawn_id gdb_spawn_id
1773 global gdb_prompt
1774
1775 if {$message == ""} {
1776 set message $command
1777 }
1778
1779 set inferior_matched 0
1780 set gdb_matched 0
1781
1782 # Use an indirect spawn id list, and remove the inferior spawn id
1783 # from the expected output as soon as it matches, in case
1784 # $inferior_pattern happens to be a prefix of the resulting full
1785 # gdb pattern below (e.g., "\r\n").
1786 global gdb_test_stdio_spawn_id_list
1787 set gdb_test_stdio_spawn_id_list "$inferior_spawn_id"
1788
1789 # Note that if $inferior_spawn_id and $gdb_spawn_id are different,
1790 # then we may see gdb's output arriving before the inferior's
1791 # output.
1792 set res [gdb_test_multiple $command $message {
1793 -i gdb_test_stdio_spawn_id_list -re "$inferior_pattern" {
1794 set inferior_matched 1
1795 if {!$gdb_matched} {
1796 set gdb_test_stdio_spawn_id_list ""
1797 exp_continue
1798 }
1799 }
1800 -i $gdb_spawn_id -re "$gdb_pattern\r\n$gdb_prompt $" {
1801 set gdb_matched 1
1802 if {!$inferior_matched} {
1803 exp_continue
1804 }
1805 }
1806 }]
1807 if {$res == 0} {
1808 pass $message
1809 } else {
1810 verbose -log "inferior_matched=$inferior_matched, gdb_matched=$gdb_matched"
1811 }
1812 return $res
1813 }
1814
1815 # Wrapper around gdb_test_multiple to be used when testing expression
1816 # evaluation while 'set debug expression 1' is in effect.
1817 # Looks for some patterns that indicates the expression was rejected.
1818 #
1819 # CMD is the command to execute, which should include an expression
1820 # that GDB will need to parse.
1821 #
1822 # OUTPUT is the expected output pattern.
1823 #
1824 # TESTNAME is the name to be used for the test, defaults to CMD if not
1825 # given.
1826 proc gdb_test_debug_expr { cmd output {testname "" }} {
1827 global gdb_prompt
1828
1829 if { ${testname} == "" } {
1830 set testname $cmd
1831 }
1832
1833 gdb_test_multiple $cmd $testname {
1834 -re ".*Invalid expression.*\r\n$gdb_prompt $" {
1835 fail $gdb_test_name
1836 }
1837 -re ".*\[\r\n\]$output\r\n$gdb_prompt $" {
1838 pass $gdb_test_name
1839 }
1840 }
1841 }
1842
1843 # get_print_expr_at_depths EXP OUTPUTS
1844 #
1845 # Used for testing 'set print max-depth'. Prints the expression EXP
1846 # with 'set print max-depth' set to various depths. OUTPUTS is a list
1847 # of `n` different patterns to match at each of the depths from 0 to
1848 # (`n` - 1).
1849 #
1850 # This proc does one final check with the max-depth set to 'unlimited'
1851 # which is tested against the last pattern in the OUTPUTS list. The
1852 # OUTPUTS list is therefore required to match every depth from 0 to a
1853 # depth where the whole of EXP is printed with no ellipsis.
1854 #
1855 # This proc leaves the 'set print max-depth' set to 'unlimited'.
1856 proc gdb_print_expr_at_depths {exp outputs} {
1857 for { set depth 0 } { $depth <= [llength $outputs] } { incr depth } {
1858 if { $depth == [llength $outputs] } {
1859 set expected_result [lindex $outputs [expr [llength $outputs] - 1]]
1860 set depth_string "unlimited"
1861 } else {
1862 set expected_result [lindex $outputs $depth]
1863 set depth_string $depth
1864 }
1865
1866 with_test_prefix "exp='$exp': depth=${depth_string}" {
1867 gdb_test_no_output "set print max-depth ${depth_string}"
1868 gdb_test "p $exp" "$expected_result"
1869 }
1870 }
1871 }
1872
1873 \f
1874
1875 # Issue a PASS and return true if evaluating CONDITION in the caller's
1876 # frame returns true, and issue a FAIL and return false otherwise.
1877 # MESSAGE is the pass/fail message to be printed. If MESSAGE is
1878 # omitted or is empty, then the pass/fail messages use the condition
1879 # string as the message.
1880
1881 proc gdb_assert { condition {message ""} } {
1882 if { $message == ""} {
1883 set message $condition
1884 }
1885
1886 set code [catch {uplevel 1 expr $condition} res]
1887 if {$code == 1} {
1888 # If code is 1 (TCL_ERROR), it means evaluation failed and res contains
1889 # an error message. Print the error message, and set res to 0 since we
1890 # want to return a boolean.
1891 warning "While evaluating expression in gdb_assert: $res"
1892 unresolved $message
1893 set res 0
1894 } elseif { !$res } {
1895 fail $message
1896 } else {
1897 pass $message
1898 }
1899 return $res
1900 }
1901
1902 proc gdb_reinitialize_dir { subdir } {
1903 global gdb_prompt
1904
1905 if [is_remote host] {
1906 return ""
1907 }
1908 send_gdb "dir\n"
1909 gdb_expect 60 {
1910 -re "Reinitialize source path to empty.*y or n. " {
1911 send_gdb "y\n" answer
1912 gdb_expect 60 {
1913 -re "Source directories searched.*$gdb_prompt $" {
1914 send_gdb "dir $subdir\n"
1915 gdb_expect 60 {
1916 -re "Source directories searched.*$gdb_prompt $" {
1917 verbose "Dir set to $subdir"
1918 }
1919 -re "$gdb_prompt $" {
1920 perror "Dir \"$subdir\" failed."
1921 }
1922 }
1923 }
1924 -re "$gdb_prompt $" {
1925 perror "Dir \"$subdir\" failed."
1926 }
1927 }
1928 }
1929 -re "$gdb_prompt $" {
1930 perror "Dir \"$subdir\" failed."
1931 }
1932 }
1933 }
1934
1935 #
1936 # gdb_exit -- exit the GDB, killing the target program if necessary
1937 #
1938 proc default_gdb_exit {} {
1939 global GDB
1940 global INTERNAL_GDBFLAGS GDBFLAGS
1941 global gdb_spawn_id inferior_spawn_id
1942 global inotify_log_file
1943
1944 if ![info exists gdb_spawn_id] {
1945 return
1946 }
1947
1948 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1949
1950 if {[info exists inotify_log_file] && [file exists $inotify_log_file]} {
1951 set fd [open $inotify_log_file]
1952 set data [read -nonewline $fd]
1953 close $fd
1954
1955 if {[string compare $data ""] != 0} {
1956 warning "parallel-unsafe file creations noticed"
1957
1958 # Clear the log.
1959 set fd [open $inotify_log_file w]
1960 close $fd
1961 }
1962 }
1963
1964 if { [is_remote host] && [board_info host exists fileid] } {
1965 send_gdb "quit\n"
1966 gdb_expect 10 {
1967 -re "y or n" {
1968 send_gdb "y\n" answer
1969 exp_continue
1970 }
1971 -re "DOSEXIT code" { }
1972 default { }
1973 }
1974 }
1975
1976 if ![is_remote host] {
1977 remote_close host
1978 }
1979 unset gdb_spawn_id
1980 unset ::gdb_tty_name
1981 unset inferior_spawn_id
1982 }
1983
1984 # Load a file into the debugger.
1985 # The return value is 0 for success, -1 for failure.
1986 #
1987 # This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1988 # to one of these values:
1989 #
1990 # debug file was loaded successfully and has debug information
1991 # nodebug file was loaded successfully and has no debug information
1992 # lzma file was loaded, .gnu_debugdata found, but no LZMA support
1993 # compiled in
1994 # fail file was not loaded
1995 #
1996 # This procedure also set the global variable GDB_FILE_CMD_MSG to the
1997 # output of the file command in case of success.
1998 #
1999 # I tried returning this information as part of the return value,
2000 # but ran into a mess because of the many re-implementations of
2001 # gdb_load in config/*.exp.
2002 #
2003 # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
2004 # this if they can get more information set.
2005
2006 proc gdb_file_cmd { arg } {
2007 global gdb_prompt
2008 global GDB
2009 global last_loaded_file
2010
2011 # GCC for Windows target may create foo.exe given "-o foo".
2012 if { ![file exists $arg] && [file exists "$arg.exe"] } {
2013 set arg "$arg.exe"
2014 }
2015
2016 # Save this for the benefit of gdbserver-support.exp.
2017 set last_loaded_file $arg
2018
2019 # Set whether debug info was found.
2020 # Default to "fail".
2021 global gdb_file_cmd_debug_info gdb_file_cmd_msg
2022 set gdb_file_cmd_debug_info "fail"
2023
2024 if [is_remote host] {
2025 set arg [remote_download host $arg]
2026 if { $arg == "" } {
2027 perror "download failed"
2028 return -1
2029 }
2030 }
2031
2032 # The file command used to kill the remote target. For the benefit
2033 # of the testsuite, preserve this behavior. Mark as optional so it doesn't
2034 # get written to the stdin log.
2035 send_gdb "kill\n" optional
2036 gdb_expect 120 {
2037 -re "Kill the program being debugged. .y or n. $" {
2038 send_gdb "y\n" answer
2039 verbose "\t\tKilling previous program being debugged"
2040 exp_continue
2041 }
2042 -re "$gdb_prompt $" {
2043 # OK.
2044 }
2045 }
2046
2047 send_gdb "file $arg\n"
2048 set new_symbol_table 0
2049 set basename [file tail $arg]
2050 gdb_expect 120 {
2051 -re "(Reading symbols from.*LZMA support was disabled.*$gdb_prompt $)" {
2052 verbose "\t\tLoaded $arg into $GDB; .gnu_debugdata found but no LZMA available"
2053 set gdb_file_cmd_msg $expect_out(1,string)
2054 set gdb_file_cmd_debug_info "lzma"
2055 return 0
2056 }
2057 -re "(Reading symbols from.*no debugging symbols found.*$gdb_prompt $)" {
2058 verbose "\t\tLoaded $arg into $GDB with no debugging symbols"
2059 set gdb_file_cmd_msg $expect_out(1,string)
2060 set gdb_file_cmd_debug_info "nodebug"
2061 return 0
2062 }
2063 -re "(Reading symbols from.*$gdb_prompt $)" {
2064 verbose "\t\tLoaded $arg into $GDB"
2065 set gdb_file_cmd_msg $expect_out(1,string)
2066 set gdb_file_cmd_debug_info "debug"
2067 return 0
2068 }
2069 -re "Load new symbol table from \".*\".*y or n. $" {
2070 if { $new_symbol_table > 0 } {
2071 perror [join [list "Couldn't load $basename,"
2072 "interactive prompt loop detected."]]
2073 return -1
2074 }
2075 send_gdb "y\n" answer
2076 incr new_symbol_table
2077 set suffix "-- with new symbol table"
2078 set arg "$arg $suffix"
2079 set basename "$basename $suffix"
2080 exp_continue
2081 }
2082 -re "No such file or directory.*$gdb_prompt $" {
2083 perror "($basename) No such file or directory"
2084 return -1
2085 }
2086 -re "A problem internal to GDB has been detected" {
2087 perror "Couldn't load $basename into GDB (GDB internal error)."
2088 gdb_internal_error_resync
2089 return -1
2090 }
2091 -re "$gdb_prompt $" {
2092 perror "Couldn't load $basename into GDB."
2093 return -1
2094 }
2095 timeout {
2096 perror "Couldn't load $basename into GDB (timeout)."
2097 return -1
2098 }
2099 eof {
2100 # This is an attempt to detect a core dump, but seems not to
2101 # work. Perhaps we need to match .* followed by eof, in which
2102 # gdb_expect does not seem to have a way to do that.
2103 perror "Couldn't load $basename into GDB (eof)."
2104 return -1
2105 }
2106 }
2107 }
2108
2109 # The expect "spawn" function puts the tty name into the spawn_out
2110 # array; but dejagnu doesn't export this globally. So, we have to
2111 # wrap spawn with our own function and poke in the built-in spawn
2112 # so that we can capture this value.
2113 #
2114 # If available, the TTY name is saved to the LAST_SPAWN_TTY_NAME global.
2115 # Otherwise, LAST_SPAWN_TTY_NAME is unset.
2116
2117 proc spawn_capture_tty_name { args } {
2118 set result [uplevel builtin_spawn $args]
2119 upvar spawn_out spawn_out
2120 if { [info exists spawn_out(slave,name)] } {
2121 set ::last_spawn_tty_name $spawn_out(slave,name)
2122 } else {
2123 # If a process is spawned as part of a pipe line (e.g. passing
2124 # -leaveopen to the spawn proc) then the spawned process is no
2125 # assigned a tty and spawn_out(slave,name) will not be set.
2126 # In that case we want to ensure that last_spawn_tty_name is
2127 # not set.
2128 #
2129 # If the previous process spawned was also not assigned a tty
2130 # (e.g. multiple processed chained in a pipeline) then
2131 # last_spawn_tty_name will already be unset, so, if we don't
2132 # use -nocomplain here we would otherwise get an error.
2133 unset -nocomplain ::last_spawn_tty_name
2134 }
2135 return $result
2136 }
2137
2138 rename spawn builtin_spawn
2139 rename spawn_capture_tty_name spawn
2140
2141 # Default gdb_spawn procedure.
2142
2143 proc default_gdb_spawn { } {
2144 global use_gdb_stub
2145 global GDB
2146 global INTERNAL_GDBFLAGS GDBFLAGS
2147 global gdb_spawn_id
2148
2149 # Set the default value, it may be overriden later by specific testfile.
2150 #
2151 # Use `set_board_info use_gdb_stub' for the board file to flag the inferior
2152 # is already started after connecting and run/attach are not supported.
2153 # This is used for the "remote" protocol. After GDB starts you should
2154 # check global $use_gdb_stub instead of the board as the testfile may force
2155 # a specific different target protocol itself.
2156 set use_gdb_stub [target_info exists use_gdb_stub]
2157
2158 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
2159 gdb_write_cmd_file "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
2160
2161 if [info exists gdb_spawn_id] {
2162 return 0
2163 }
2164
2165 if ![is_remote host] {
2166 if { [which $GDB] == 0 } then {
2167 perror "$GDB does not exist."
2168 exit 1
2169 }
2170 }
2171
2172 # Put GDBFLAGS last so that tests can put "--args ..." in it.
2173 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS [host_info gdb_opts] $GDBFLAGS"]
2174 if { $res < 0 || $res == "" } {
2175 perror "Spawning $GDB failed."
2176 return 1
2177 }
2178
2179 set gdb_spawn_id $res
2180 set ::gdb_tty_name $::last_spawn_tty_name
2181 return 0
2182 }
2183
2184 # Default gdb_start procedure.
2185
2186 proc default_gdb_start { } {
2187 global gdb_prompt
2188 global gdb_spawn_id
2189 global inferior_spawn_id
2190
2191 if [info exists gdb_spawn_id] {
2192 return 0
2193 }
2194
2195 # Keep track of the number of times GDB has been launched.
2196 global gdb_instances
2197 incr gdb_instances
2198
2199 gdb_stdin_log_init
2200
2201 set res [gdb_spawn]
2202 if { $res != 0} {
2203 return $res
2204 }
2205
2206 # Default to assuming inferior I/O is done on GDB's terminal.
2207 if {![info exists inferior_spawn_id]} {
2208 set inferior_spawn_id $gdb_spawn_id
2209 }
2210
2211 # When running over NFS, particularly if running many simultaneous
2212 # tests on different hosts all using the same server, things can
2213 # get really slow. Give gdb at least 3 minutes to start up.
2214 gdb_expect 360 {
2215 -re "\[\r\n\]$gdb_prompt $" {
2216 verbose "GDB initialized."
2217 }
2218 -re "\[\r\n\]\033\\\[.2004h$gdb_prompt $" {
2219 # This special case detects what happens when GDB is
2220 # started with bracketed paste mode enabled. This mode is
2221 # usually forced off (see setting of INPUTRC in
2222 # default_gdb_init), but for at least one test we turn
2223 # bracketed paste mode back on, and then start GDB. In
2224 # that case, this case is hit.
2225 verbose "GDB initialized."
2226 }
2227 -re "$gdb_prompt $" {
2228 perror "GDB never initialized."
2229 unset gdb_spawn_id
2230 return -1
2231 }
2232 timeout {
2233 perror "(timeout) GDB never initialized after 10 seconds."
2234 remote_close host
2235 unset gdb_spawn_id
2236 return -1
2237 }
2238 eof {
2239 perror "(eof) GDB never initialized."
2240 unset gdb_spawn_id
2241 return -1
2242 }
2243 }
2244
2245 # force the height to "unlimited", so no pagers get used
2246
2247 send_gdb "set height 0\n"
2248 gdb_expect 10 {
2249 -re "$gdb_prompt $" {
2250 verbose "Setting height to 0." 2
2251 }
2252 timeout {
2253 warning "Couldn't set the height to 0"
2254 }
2255 }
2256 # force the width to "unlimited", so no wraparound occurs
2257 send_gdb "set width 0\n"
2258 gdb_expect 10 {
2259 -re "$gdb_prompt $" {
2260 verbose "Setting width to 0." 2
2261 }
2262 timeout {
2263 warning "Couldn't set the width to 0."
2264 }
2265 }
2266
2267 gdb_debug_init
2268 return 0
2269 }
2270
2271 # Utility procedure to give user control of the gdb prompt in a script. It is
2272 # meant to be used for debugging test cases, and should not be left in the
2273 # test cases code.
2274
2275 proc gdb_interact { } {
2276 global gdb_spawn_id
2277 set spawn_id $gdb_spawn_id
2278
2279 send_user "+------------------------------------------+\n"
2280 send_user "| Script interrupted, you can now interact |\n"
2281 send_user "| with by gdb. Type >>> to continue. |\n"
2282 send_user "+------------------------------------------+\n"
2283
2284 interact {
2285 ">>>" return
2286 }
2287 }
2288
2289 # Examine the output of compilation to determine whether compilation
2290 # failed or not. If it failed determine whether it is due to missing
2291 # compiler or due to compiler error. Report pass, fail or unsupported
2292 # as appropriate.
2293
2294 proc gdb_compile_test {src output} {
2295 set msg "compilation [file tail $src]"
2296
2297 if { $output == "" } {
2298 pass $msg
2299 return
2300 }
2301
2302 if { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output]
2303 || [regexp {.*: command not found[\r|\n]*$} $output]
2304 || [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } {
2305 unsupported "$msg (missing compiler)"
2306 return
2307 }
2308
2309 set gcc_re ".*: error: unrecognized command line option "
2310 set clang_re ".*: error: unsupported option "
2311 if { [regexp "(?:$gcc_re|$clang_re)(\[^ \t;\r\n\]*)" $output dummy option]
2312 && $option != "" } {
2313 unsupported "$msg (unsupported option $option)"
2314 return
2315 }
2316
2317 # Unclassified compilation failure, be more verbose.
2318 verbose -log "compilation failed: $output" 2
2319 fail "$msg"
2320 }
2321
2322 # Return a 1 for configurations for which we don't even want to try to
2323 # test C++.
2324
2325 proc skip_cplus_tests {} {
2326 if { [istarget "h8300-*-*"] } {
2327 return 1
2328 }
2329
2330 # The C++ IO streams are too large for HC11/HC12 and are thus not
2331 # available. The gdb C++ tests use them and don't compile.
2332 if { [istarget "m6811-*-*"] } {
2333 return 1
2334 }
2335 if { [istarget "m6812-*-*"] } {
2336 return 1
2337 }
2338 return 0
2339 }
2340
2341 # Return a 1 for configurations for which don't have both C++ and the STL.
2342
2343 proc skip_stl_tests {} {
2344 return [skip_cplus_tests]
2345 }
2346
2347 # Return a 1 if I don't even want to try to test FORTRAN.
2348
2349 proc skip_fortran_tests {} {
2350 return 0
2351 }
2352
2353 # Return a 1 if I don't even want to try to test ada.
2354
2355 proc skip_ada_tests {} {
2356 return 0
2357 }
2358
2359 # Return a 1 if I don't even want to try to test GO.
2360
2361 proc skip_go_tests {} {
2362 return 0
2363 }
2364
2365 # Return a 1 if I don't even want to try to test D.
2366
2367 proc skip_d_tests {} {
2368 return 0
2369 }
2370
2371 # Return 1 to skip Rust tests, 0 to try them.
2372 proc skip_rust_tests {} {
2373 if { ![isnative] } {
2374 return 1
2375 }
2376
2377 # The rust compiler does not support "-m32", skip.
2378 global board board_info
2379 set board [target_info name]
2380 if {[board_info $board exists multilib_flags]} {
2381 foreach flag [board_info $board multilib_flags] {
2382 if { $flag == "-m32" } {
2383 return 1
2384 }
2385 }
2386 }
2387
2388 return 0
2389 }
2390
2391 # Return a 1 for configurations that do not support Python scripting.
2392 # PROMPT_REGEXP is the expected prompt.
2393
2394 proc skip_python_tests_prompt { prompt_regexp } {
2395 gdb_test_multiple "python print ('test')" "verify python support" \
2396 -prompt "$prompt_regexp" {
2397 -re "not supported.*$prompt_regexp" {
2398 unsupported "Python support is disabled."
2399 return 1
2400 }
2401 -re "$prompt_regexp" {}
2402 }
2403
2404 return 0
2405 }
2406
2407 # Return a 1 for configurations that do not support Python scripting.
2408 # Note: This also sets various globals that specify which version of Python
2409 # is in use. See skip_python_tests_prompt.
2410
2411 proc skip_python_tests {} {
2412 global gdb_prompt
2413 return [skip_python_tests_prompt "$gdb_prompt $"]
2414 }
2415
2416 # Return a 1 if we should skip shared library tests.
2417
2418 proc skip_shlib_tests {} {
2419 # Run the shared library tests on native systems.
2420 if {[isnative]} {
2421 return 0
2422 }
2423
2424 # An abbreviated list of remote targets where we should be able to
2425 # run shared library tests.
2426 if {([istarget *-*-linux*]
2427 || [istarget *-*-*bsd*]
2428 || [istarget *-*-solaris2*]
2429 || [istarget *-*-mingw*]
2430 || [istarget *-*-cygwin*]
2431 || [istarget *-*-pe*])} {
2432 return 0
2433 }
2434
2435 return 1
2436 }
2437
2438 # Return 1 if we should skip tui related tests.
2439
2440 proc skip_tui_tests {} {
2441 global gdb_prompt
2442
2443 gdb_test_multiple "help layout" "verify tui support" {
2444 -re "Undefined command: \"layout\".*$gdb_prompt $" {
2445 return 1
2446 }
2447 -re "$gdb_prompt $" {
2448 }
2449 }
2450
2451 return 0
2452 }
2453
2454 # Test files shall make sure all the test result lines in gdb.sum are
2455 # unique in a test run, so that comparing the gdb.sum files of two
2456 # test runs gives correct results. Test files that exercise
2457 # variations of the same tests more than once, shall prefix the
2458 # different test invocations with different identifying strings in
2459 # order to make them unique.
2460 #
2461 # About test prefixes:
2462 #
2463 # $pf_prefix is the string that dejagnu prints after the result (FAIL,
2464 # PASS, etc.), and before the test message/name in gdb.sum. E.g., the
2465 # underlined substring in
2466 #
2467 # PASS: gdb.base/mytest.exp: some test
2468 # ^^^^^^^^^^^^^^^^^^^^
2469 #
2470 # is $pf_prefix.
2471 #
2472 # The easiest way to adjust the test prefix is to append a test
2473 # variation prefix to the $pf_prefix, using the with_test_prefix
2474 # procedure. E.g.,
2475 #
2476 # proc do_tests {} {
2477 # gdb_test ... ... "test foo"
2478 # gdb_test ... ... "test bar"
2479 #
2480 # with_test_prefix "subvariation a" {
2481 # gdb_test ... ... "test x"
2482 # }
2483 #
2484 # with_test_prefix "subvariation b" {
2485 # gdb_test ... ... "test x"
2486 # }
2487 # }
2488 #
2489 # with_test_prefix "variation1" {
2490 # ...do setup for variation 1...
2491 # do_tests
2492 # }
2493 #
2494 # with_test_prefix "variation2" {
2495 # ...do setup for variation 2...
2496 # do_tests
2497 # }
2498 #
2499 # Results in:
2500 #
2501 # PASS: gdb.base/mytest.exp: variation1: test foo
2502 # PASS: gdb.base/mytest.exp: variation1: test bar
2503 # PASS: gdb.base/mytest.exp: variation1: subvariation a: test x
2504 # PASS: gdb.base/mytest.exp: variation1: subvariation b: test x
2505 # PASS: gdb.base/mytest.exp: variation2: test foo
2506 # PASS: gdb.base/mytest.exp: variation2: test bar
2507 # PASS: gdb.base/mytest.exp: variation2: subvariation a: test x
2508 # PASS: gdb.base/mytest.exp: variation2: subvariation b: test x
2509 #
2510 # If for some reason more flexibility is necessary, one can also
2511 # manipulate the pf_prefix global directly, treating it as a string.
2512 # E.g.,
2513 #
2514 # global pf_prefix
2515 # set saved_pf_prefix
2516 # append pf_prefix "${foo}: bar"
2517 # ... actual tests ...
2518 # set pf_prefix $saved_pf_prefix
2519 #
2520
2521 # Run BODY in the context of the caller, with the current test prefix
2522 # (pf_prefix) appended with one space, then PREFIX, and then a colon.
2523 # Returns the result of BODY.
2524 #
2525 proc with_test_prefix { prefix body } {
2526 global pf_prefix
2527
2528 set saved $pf_prefix
2529 append pf_prefix " " $prefix ":"
2530 set code [catch {uplevel 1 $body} result]
2531 set pf_prefix $saved
2532
2533 if {$code == 1} {
2534 global errorInfo errorCode
2535 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2536 } else {
2537 return -code $code $result
2538 }
2539 }
2540
2541 # Wrapper for foreach that calls with_test_prefix on each iteration,
2542 # including the iterator's name and current value in the prefix.
2543
2544 proc foreach_with_prefix {var list body} {
2545 upvar 1 $var myvar
2546 foreach myvar $list {
2547 with_test_prefix "$var=$myvar" {
2548 set code [catch {uplevel 1 $body} result]
2549 }
2550
2551 if {$code == 1} {
2552 global errorInfo errorCode
2553 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2554 } elseif {$code == 3} {
2555 break
2556 } elseif {$code == 2} {
2557 return -code $code $result
2558 }
2559 }
2560 }
2561
2562 # Like TCL's native proc, but defines a procedure that wraps its body
2563 # within 'with_test_prefix "$proc_name" { ... }'.
2564 proc proc_with_prefix {name arguments body} {
2565 # Define the advertised proc.
2566 proc $name $arguments [list with_test_prefix $name $body]
2567 }
2568
2569 # Return an id corresponding to the test prefix stored in $pf_prefix, which
2570 # is more suitable for use in a file name.
2571 # F.i., for a pf_prefix:
2572 # gdb.dwarf2/dw2-lines.exp: \
2573 # cv=5: cdw=64: lv=5: ldw=64: string_form=line_strp:
2574 # return an id:
2575 # cv-5-cdw-32-lv-5-ldw-64-string_form-line_strp
2576
2577 proc prefix_id {} {
2578 global pf_prefix
2579 set id $pf_prefix
2580
2581 # Strip ".exp: " prefix.
2582 set id [regsub {.*\.exp: } $id {}]
2583
2584 # Strip colon suffix.
2585 set id [regsub {:$} $id {}]
2586
2587 # Strip spaces.
2588 set id [regsub -all { } $id {}]
2589
2590 # Replace colons, equal signs.
2591 set id [regsub -all \[:=\] $id -]
2592
2593 return $id
2594 }
2595
2596 # Run BODY in the context of the caller. After BODY is run, the variables
2597 # listed in VARS will be reset to the values they had before BODY was run.
2598 #
2599 # This is useful for providing a scope in which it is safe to temporarily
2600 # modify global variables, e.g.
2601 #
2602 # global INTERNAL_GDBFLAGS
2603 # global env
2604 #
2605 # set foo GDBHISTSIZE
2606 #
2607 # save_vars { INTERNAL_GDBFLAGS env($foo) env(HOME) } {
2608 # append INTERNAL_GDBFLAGS " -nx"
2609 # unset -nocomplain env(GDBHISTSIZE)
2610 # gdb_start
2611 # gdb_test ...
2612 # }
2613 #
2614 # Here, although INTERNAL_GDBFLAGS, env(GDBHISTSIZE) and env(HOME) may be
2615 # modified inside BODY, this proc guarantees that the modifications will be
2616 # undone after BODY finishes executing.
2617
2618 proc save_vars { vars body } {
2619 array set saved_scalars { }
2620 array set saved_arrays { }
2621 set unset_vars { }
2622
2623 foreach var $vars {
2624 # First evaluate VAR in the context of the caller in case the variable
2625 # name may be a not-yet-interpolated string like env($foo)
2626 set var [uplevel 1 list $var]
2627
2628 if [uplevel 1 [list info exists $var]] {
2629 if [uplevel 1 [list array exists $var]] {
2630 set saved_arrays($var) [uplevel 1 [list array get $var]]
2631 } else {
2632 set saved_scalars($var) [uplevel 1 [list set $var]]
2633 }
2634 } else {
2635 lappend unset_vars $var
2636 }
2637 }
2638
2639 set code [catch {uplevel 1 $body} result]
2640
2641 foreach {var value} [array get saved_scalars] {
2642 uplevel 1 [list set $var $value]
2643 }
2644
2645 foreach {var value} [array get saved_arrays] {
2646 uplevel 1 [list unset $var]
2647 uplevel 1 [list array set $var $value]
2648 }
2649
2650 foreach var $unset_vars {
2651 uplevel 1 [list unset -nocomplain $var]
2652 }
2653
2654 if {$code == 1} {
2655 global errorInfo errorCode
2656 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2657 } else {
2658 return -code $code $result
2659 }
2660 }
2661
2662 # As save_vars, but for variables stored in the board_info for the
2663 # target board.
2664 #
2665 # Usage example:
2666 #
2667 # save_target_board_info { multilib_flags } {
2668 # global board
2669 # set board [target_info name]
2670 # unset_board_info multilib_flags
2671 # set_board_info multilib_flags "$multilib_flags"
2672 # ...
2673 # }
2674
2675 proc save_target_board_info { vars body } {
2676 global board board_info
2677 set board [target_info name]
2678
2679 array set saved_target_board_info { }
2680 set unset_target_board_info { }
2681
2682 foreach var $vars {
2683 if { [info exists board_info($board,$var)] } {
2684 set saved_target_board_info($var) [board_info $board $var]
2685 } else {
2686 lappend unset_target_board_info $var
2687 }
2688 }
2689
2690 set code [catch {uplevel 1 $body} result]
2691
2692 foreach {var value} [array get saved_target_board_info] {
2693 unset_board_info $var
2694 set_board_info $var $value
2695 }
2696
2697 foreach var $unset_target_board_info {
2698 unset_board_info $var
2699 }
2700
2701 if {$code == 1} {
2702 global errorInfo errorCode
2703 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2704 } else {
2705 return -code $code $result
2706 }
2707 }
2708
2709 # Run tests in BODY with the current working directory (CWD) set to
2710 # DIR. When BODY is finished, restore the original CWD. Return the
2711 # result of BODY.
2712 #
2713 # This procedure doesn't check if DIR is a valid directory, so you
2714 # have to make sure of that.
2715
2716 proc with_cwd { dir body } {
2717 set saved_dir [pwd]
2718 verbose -log "Switching to directory $dir (saved CWD: $saved_dir)."
2719 cd $dir
2720
2721 set code [catch {uplevel 1 $body} result]
2722
2723 verbose -log "Switching back to $saved_dir."
2724 cd $saved_dir
2725
2726 if {$code == 1} {
2727 global errorInfo errorCode
2728 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2729 } else {
2730 return -code $code $result
2731 }
2732 }
2733
2734 # Run tests in BODY with GDB prompt and variable $gdb_prompt set to
2735 # PROMPT. When BODY is finished, restore GDB prompt and variable
2736 # $gdb_prompt.
2737 # Returns the result of BODY.
2738 #
2739 # Notes:
2740 #
2741 # 1) If you want to use, for example, "(foo)" as the prompt you must pass it
2742 # as "(foo)", and not the regexp form "\(foo\)" (expressed as "\\(foo\\)" in
2743 # TCL). PROMPT is internally converted to a suitable regexp for matching.
2744 # We do the conversion from "(foo)" to "\(foo\)" here for a few reasons:
2745 # a) It's more intuitive for callers to pass the plain text form.
2746 # b) We need two forms of the prompt:
2747 # - a regexp to use in output matching,
2748 # - a value to pass to the "set prompt" command.
2749 # c) It's easier to convert the plain text form to its regexp form.
2750 #
2751 # 2) Don't add a trailing space, we do that here.
2752
2753 proc with_gdb_prompt { prompt body } {
2754 global gdb_prompt
2755
2756 # Convert "(foo)" to "\(foo\)".
2757 # We don't use string_to_regexp because while it works today it's not
2758 # clear it will work tomorrow: the value we need must work as both a
2759 # regexp *and* as the argument to the "set prompt" command, at least until
2760 # we start recording both forms separately instead of just $gdb_prompt.
2761 # The testsuite is pretty-much hardwired to interpret $gdb_prompt as the
2762 # regexp form.
2763 regsub -all {[]*+.|()^$\[\\]} $prompt {\\&} prompt
2764
2765 set saved $gdb_prompt
2766
2767 verbose -log "Setting gdb prompt to \"$prompt \"."
2768 set gdb_prompt $prompt
2769 gdb_test_no_output "set prompt $prompt " ""
2770
2771 set code [catch {uplevel 1 $body} result]
2772
2773 verbose -log "Restoring gdb prompt to \"$saved \"."
2774 set gdb_prompt $saved
2775 gdb_test_no_output "set prompt $saved " ""
2776
2777 if {$code == 1} {
2778 global errorInfo errorCode
2779 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2780 } else {
2781 return -code $code $result
2782 }
2783 }
2784
2785 # Run tests in BODY with target-charset setting to TARGET_CHARSET. When
2786 # BODY is finished, restore target-charset.
2787
2788 proc with_target_charset { target_charset body } {
2789 global gdb_prompt
2790
2791 set saved ""
2792 gdb_test_multiple "show target-charset" "" {
2793 -re "The target character set is \".*; currently (.*)\"\..*$gdb_prompt " {
2794 set saved $expect_out(1,string)
2795 }
2796 -re "The target character set is \"(.*)\".*$gdb_prompt " {
2797 set saved $expect_out(1,string)
2798 }
2799 -re ".*$gdb_prompt " {
2800 fail "get target-charset"
2801 }
2802 }
2803
2804 gdb_test_no_output "set target-charset $target_charset" ""
2805
2806 set code [catch {uplevel 1 $body} result]
2807
2808 gdb_test_no_output "set target-charset $saved" ""
2809
2810 if {$code == 1} {
2811 global errorInfo errorCode
2812 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2813 } else {
2814 return -code $code $result
2815 }
2816 }
2817
2818 # Switch the default spawn id to SPAWN_ID, so that gdb_test,
2819 # mi_gdb_test etc. default to using it.
2820
2821 proc switch_gdb_spawn_id {spawn_id} {
2822 global gdb_spawn_id
2823 global board board_info
2824
2825 set gdb_spawn_id $spawn_id
2826 set board [host_info name]
2827 set board_info($board,fileid) $spawn_id
2828 }
2829
2830 # Clear the default spawn id.
2831
2832 proc clear_gdb_spawn_id {} {
2833 global gdb_spawn_id
2834 global board board_info
2835
2836 unset -nocomplain gdb_spawn_id
2837 set board [host_info name]
2838 unset -nocomplain board_info($board,fileid)
2839 }
2840
2841 # Run BODY with SPAWN_ID as current spawn id.
2842
2843 proc with_spawn_id { spawn_id body } {
2844 global gdb_spawn_id
2845
2846 if [info exists gdb_spawn_id] {
2847 set saved_spawn_id $gdb_spawn_id
2848 }
2849
2850 switch_gdb_spawn_id $spawn_id
2851
2852 set code [catch {uplevel 1 $body} result]
2853
2854 if [info exists saved_spawn_id] {
2855 switch_gdb_spawn_id $saved_spawn_id
2856 } else {
2857 clear_gdb_spawn_id
2858 }
2859
2860 if {$code == 1} {
2861 global errorInfo errorCode
2862 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2863 } else {
2864 return -code $code $result
2865 }
2866 }
2867
2868 # Select the largest timeout from all the timeouts:
2869 # - the local "timeout" variable of the scope two levels above,
2870 # - the global "timeout" variable,
2871 # - the board variable "gdb,timeout".
2872
2873 proc get_largest_timeout {} {
2874 upvar #0 timeout gtimeout
2875 upvar 2 timeout timeout
2876
2877 set tmt 0
2878 if [info exists timeout] {
2879 set tmt $timeout
2880 }
2881 if { [info exists gtimeout] && $gtimeout > $tmt } {
2882 set tmt $gtimeout
2883 }
2884 if { [target_info exists gdb,timeout]
2885 && [target_info gdb,timeout] > $tmt } {
2886 set tmt [target_info gdb,timeout]
2887 }
2888 if { $tmt == 0 } {
2889 # Eeeeew.
2890 set tmt 60
2891 }
2892
2893 return $tmt
2894 }
2895
2896 # Run tests in BODY with timeout increased by factor of FACTOR. When
2897 # BODY is finished, restore timeout.
2898
2899 proc with_timeout_factor { factor body } {
2900 global timeout
2901
2902 set savedtimeout $timeout
2903
2904 set timeout [expr [get_largest_timeout] * $factor]
2905 set code [catch {uplevel 1 $body} result]
2906
2907 set timeout $savedtimeout
2908 if {$code == 1} {
2909 global errorInfo errorCode
2910 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2911 } else {
2912 return -code $code $result
2913 }
2914 }
2915
2916 # Run BODY with timeout factor FACTOR if check-read1 is used.
2917
2918 proc with_read1_timeout_factor { factor body } {
2919 if { [info exists ::env(READ1)] == 1 && $::env(READ1) == 1 } {
2920 # Use timeout factor
2921 } else {
2922 # Reset timeout factor
2923 set factor 1
2924 }
2925 return [uplevel [list with_timeout_factor $factor $body]]
2926 }
2927
2928 # Return 1 if _Complex types are supported, otherwise, return 0.
2929
2930 gdb_caching_proc support_complex_tests {
2931
2932 if { [gdb_skip_float_test] } {
2933 # If floating point is not supported, _Complex is not
2934 # supported.
2935 return 0
2936 }
2937
2938 # Compile a test program containing _Complex types.
2939
2940 return [gdb_can_simple_compile complex {
2941 int main() {
2942 _Complex float cf;
2943 _Complex double cd;
2944 _Complex long double cld;
2945 return 0;
2946 }
2947 } executable]
2948 }
2949
2950 # Return 1 if compiling go is supported.
2951 gdb_caching_proc support_go_compile {
2952
2953 return [gdb_can_simple_compile go-hello {
2954 package main
2955 import "fmt"
2956 func main() {
2957 fmt.Println("hello world")
2958 }
2959 } executable go]
2960 }
2961
2962 # Return 1 if GDB can get a type for siginfo from the target, otherwise
2963 # return 0.
2964
2965 proc supports_get_siginfo_type {} {
2966 if { [istarget "*-*-linux*"] } {
2967 return 1
2968 } else {
2969 return 0
2970 }
2971 }
2972
2973 # Return 1 if memory tagging is supported at runtime, otherwise return 0.
2974
2975 gdb_caching_proc supports_memtag {
2976 global gdb_prompt
2977
2978 gdb_test_multiple "memory-tag check" "" {
2979 -re "Memory tagging not supported or disabled by the current architecture\..*$gdb_prompt $" {
2980 return 0
2981 }
2982 -re "Argument required \\(address or pointer\\).*$gdb_prompt $" {
2983 return 1
2984 }
2985 }
2986 return 0
2987 }
2988
2989 # Return 1 if the target supports hardware single stepping.
2990
2991 proc can_hardware_single_step {} {
2992
2993 if { [istarget "arm*-*-*"] || [istarget "mips*-*-*"]
2994 || [istarget "tic6x-*-*"] || [istarget "sparc*-*-linux*"]
2995 || [istarget "nios2-*-*"] || [istarget "riscv*-*-linux*"] } {
2996 return 0
2997 }
2998
2999 return 1
3000 }
3001
3002 # Return 1 if target hardware or OS supports single stepping to signal
3003 # handler, otherwise, return 0.
3004
3005 proc can_single_step_to_signal_handler {} {
3006 # Targets don't have hardware single step. On these targets, when
3007 # a signal is delivered during software single step, gdb is unable
3008 # to determine the next instruction addresses, because start of signal
3009 # handler is one of them.
3010 return [can_hardware_single_step]
3011 }
3012
3013 # Return 1 if target supports process record, otherwise return 0.
3014
3015 proc supports_process_record {} {
3016
3017 if [target_info exists gdb,use_precord] {
3018 return [target_info gdb,use_precord]
3019 }
3020
3021 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
3022 || [istarget "i\[34567\]86-*-linux*"]
3023 || [istarget "aarch64*-*-linux*"]
3024 || [istarget "powerpc*-*-linux*"]
3025 || [istarget "s390*-*-linux*"] } {
3026 return 1
3027 }
3028
3029 return 0
3030 }
3031
3032 # Return 1 if target supports reverse debugging, otherwise return 0.
3033
3034 proc supports_reverse {} {
3035
3036 if [target_info exists gdb,can_reverse] {
3037 return [target_info gdb,can_reverse]
3038 }
3039
3040 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
3041 || [istarget "i\[34567\]86-*-linux*"]
3042 || [istarget "aarch64*-*-linux*"]
3043 || [istarget "powerpc*-*-linux*"]
3044 || [istarget "s390*-*-linux*"] } {
3045 return 1
3046 }
3047
3048 return 0
3049 }
3050
3051 # Return 1 if readline library is used.
3052
3053 proc readline_is_used { } {
3054 global gdb_prompt
3055
3056 gdb_test_multiple "show editing" "" {
3057 -re ".*Editing of command lines as they are typed is on\..*$gdb_prompt $" {
3058 return 1
3059 }
3060 -re ".*$gdb_prompt $" {
3061 return 0
3062 }
3063 }
3064 }
3065
3066 # Return 1 if target is ELF.
3067 gdb_caching_proc is_elf_target {
3068 set me "is_elf_target"
3069
3070 set src { int foo () {return 0;} }
3071 if {![gdb_simple_compile elf_target $src]} {
3072 return 0
3073 }
3074
3075 set fp_obj [open $obj "r"]
3076 fconfigure $fp_obj -translation binary
3077 set data [read $fp_obj]
3078 close $fp_obj
3079
3080 file delete $obj
3081
3082 set ELFMAG "\u007FELF"
3083
3084 if {[string compare -length 4 $data $ELFMAG] != 0} {
3085 verbose "$me: returning 0" 2
3086 return 0
3087 }
3088
3089 verbose "$me: returning 1" 2
3090 return 1
3091 }
3092
3093 # Return 1 if the memory at address zero is readable.
3094
3095 gdb_caching_proc is_address_zero_readable {
3096 global gdb_prompt
3097
3098 set ret 0
3099 gdb_test_multiple "x 0" "" {
3100 -re "Cannot access memory at address 0x0.*$gdb_prompt $" {
3101 set ret 0
3102 }
3103 -re ".*$gdb_prompt $" {
3104 set ret 1
3105 }
3106 }
3107
3108 return $ret
3109 }
3110
3111 # Produce source file NAME and write SOURCES into it.
3112
3113 proc gdb_produce_source { name sources } {
3114 set index 0
3115 set f [open $name "w"]
3116
3117 puts $f $sources
3118 close $f
3119 }
3120
3121 # Return 1 if target is ILP32.
3122 # This cannot be decided simply from looking at the target string,
3123 # as it might depend on externally passed compiler options like -m64.
3124 gdb_caching_proc is_ilp32_target {
3125 return [gdb_can_simple_compile is_ilp32_target {
3126 int dummy[sizeof (int) == 4
3127 && sizeof (void *) == 4
3128 && sizeof (long) == 4 ? 1 : -1];
3129 }]
3130 }
3131
3132 # Return 1 if target is LP64.
3133 # This cannot be decided simply from looking at the target string,
3134 # as it might depend on externally passed compiler options like -m64.
3135 gdb_caching_proc is_lp64_target {
3136 return [gdb_can_simple_compile is_lp64_target {
3137 int dummy[sizeof (int) == 4
3138 && sizeof (void *) == 8
3139 && sizeof (long) == 8 ? 1 : -1];
3140 }]
3141 }
3142
3143 # Return 1 if target has 64 bit addresses.
3144 # This cannot be decided simply from looking at the target string,
3145 # as it might depend on externally passed compiler options like -m64.
3146 gdb_caching_proc is_64_target {
3147 return [gdb_can_simple_compile is_64_target {
3148 int function(void) { return 3; }
3149 int dummy[sizeof (&function) == 8 ? 1 : -1];
3150 }]
3151 }
3152
3153 # Return 1 if target has x86_64 registers - either amd64 or x32.
3154 # x32 target identifies as x86_64-*-linux*, therefore it cannot be determined
3155 # just from the target string.
3156 gdb_caching_proc is_amd64_regs_target {
3157 if {![istarget "x86_64-*-*"] && ![istarget "i?86-*"]} {
3158 return 0
3159 }
3160
3161 return [gdb_can_simple_compile is_amd64_regs_target {
3162 int main (void) {
3163 asm ("incq %rax");
3164 asm ("incq %r15");
3165
3166 return 0;
3167 }
3168 }]
3169 }
3170
3171 # Return 1 if this target is an x86 or x86-64 with -m32.
3172 proc is_x86_like_target {} {
3173 if {![istarget "x86_64-*-*"] && ![istarget i?86-*]} {
3174 return 0
3175 }
3176 return [expr [is_ilp32_target] && ![is_amd64_regs_target]]
3177 }
3178
3179 # Return 1 if this target is an arm or aarch32 on aarch64.
3180
3181 gdb_caching_proc is_aarch32_target {
3182 if { [istarget "arm*-*-*"] } {
3183 return 1
3184 }
3185
3186 if { ![istarget "aarch64*-*-*"] } {
3187 return 0
3188 }
3189
3190 set list {}
3191 foreach reg \
3192 {r0 r1 r2 r3} {
3193 lappend list "\tmov $reg, $reg"
3194 }
3195
3196 return [gdb_can_simple_compile aarch32 [join $list \n]]
3197 }
3198
3199 # Return 1 if this target is an aarch64, either lp64 or ilp32.
3200
3201 proc is_aarch64_target {} {
3202 if { ![istarget "aarch64*-*-*"] } {
3203 return 0
3204 }
3205
3206 return [expr ![is_aarch32_target]]
3207 }
3208
3209 # Return 1 if displaced stepping is supported on target, otherwise, return 0.
3210 proc support_displaced_stepping {} {
3211
3212 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"]
3213 || [istarget "arm*-*-linux*"] || [istarget "powerpc-*-linux*"]
3214 || [istarget "powerpc64-*-linux*"] || [istarget "s390*-*-*"]
3215 || [istarget "aarch64*-*-linux*"] || [istarget "loongarch*-*-linux*"] } {
3216 return 1
3217 }
3218
3219 return 0
3220 }
3221
3222 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
3223 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
3224
3225 gdb_caching_proc skip_altivec_tests {
3226 global srcdir subdir gdb_prompt inferior_exited_re
3227
3228 set me "skip_altivec_tests"
3229
3230 # Some simulators are known to not support VMX instructions.
3231 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
3232 verbose "$me: target known to not support VMX, returning 1" 2
3233 return 1
3234 }
3235
3236 # Make sure we have a compiler that understands altivec.
3237 if [get_compiler_info] {
3238 warning "Could not get compiler info"
3239 return 1
3240 }
3241 if [test_compiler_info gcc*] {
3242 set compile_flags "additional_flags=-maltivec"
3243 } elseif [test_compiler_info xlc*] {
3244 set compile_flags "additional_flags=-qaltivec"
3245 } else {
3246 verbose "Could not compile with altivec support, returning 1" 2
3247 return 1
3248 }
3249
3250 # Compile a test program containing VMX instructions.
3251 set src {
3252 int main() {
3253 #ifdef __MACH__
3254 asm volatile ("vor v0,v0,v0");
3255 #else
3256 asm volatile ("vor 0,0,0");
3257 #endif
3258 return 0;
3259 }
3260 }
3261 if {![gdb_simple_compile $me $src executable $compile_flags]} {
3262 return 1
3263 }
3264
3265 # Compilation succeeded so now run it via gdb.
3266
3267 gdb_exit
3268 gdb_start
3269 gdb_reinitialize_dir $srcdir/$subdir
3270 gdb_load "$obj"
3271 gdb_run_cmd
3272 gdb_expect {
3273 -re ".*Illegal instruction.*${gdb_prompt} $" {
3274 verbose -log "\n$me altivec hardware not detected"
3275 set skip_vmx_tests 1
3276 }
3277 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3278 verbose -log "\n$me: altivec hardware detected"
3279 set skip_vmx_tests 0
3280 }
3281 default {
3282 warning "\n$me: default case taken"
3283 set skip_vmx_tests 1
3284 }
3285 }
3286 gdb_exit
3287 remote_file build delete $obj
3288
3289 verbose "$me: returning $skip_vmx_tests" 2
3290 return $skip_vmx_tests
3291 }
3292
3293 # Run a test on the power target to see if it supports ISA 3.1 instructions
3294 gdb_caching_proc skip_power_isa_3_1_tests {
3295 global srcdir subdir gdb_prompt inferior_exited_re
3296
3297 set me "skip_power_isa_3_1_tests"
3298
3299 # Compile a test program containing ISA 3.1 instructions.
3300 set src {
3301 int main() {
3302 asm volatile ("pnop"); // marker
3303 asm volatile ("nop");
3304 return 0;
3305 }
3306 }
3307
3308 if {![gdb_simple_compile $me $src executable ]} {
3309 return 1
3310 }
3311
3312 # No error message, compilation succeeded so now run it via gdb.
3313
3314 gdb_exit
3315 gdb_start
3316 gdb_reinitialize_dir $srcdir/$subdir
3317 gdb_load "$obj"
3318 gdb_run_cmd
3319 gdb_expect {
3320 -re ".*Illegal instruction.*${gdb_prompt} $" {
3321 verbose -log "\n$me Power ISA 3.1 hardware not detected"
3322 set skip_power_isa_3_1_tests 1
3323 }
3324 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3325 verbose -log "\n$me: Power ISA 3.1 hardware detected"
3326 set skip_power_isa_3_1_tests 0
3327 }
3328 default {
3329 warning "\n$me: default case taken"
3330 set skip_power_isa_3_1_tests 1
3331 }
3332 }
3333 gdb_exit
3334 remote_file build delete $obj
3335
3336 verbose "$me: returning $skip_power_isa_3_1_tests" 2
3337 return $skip_power_isa_3_1_tests
3338 }
3339
3340 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
3341 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
3342
3343 gdb_caching_proc skip_vsx_tests {
3344 global srcdir subdir gdb_prompt inferior_exited_re
3345
3346 set me "skip_vsx_tests"
3347
3348 # Some simulators are known to not support Altivec instructions, so
3349 # they won't support VSX instructions as well.
3350 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
3351 verbose "$me: target known to not support VSX, returning 1" 2
3352 return 1
3353 }
3354
3355 # Make sure we have a compiler that understands altivec.
3356 if [get_compiler_info] {
3357 warning "Could not get compiler info"
3358 return 1
3359 }
3360 if [test_compiler_info gcc*] {
3361 set compile_flags "additional_flags=-mvsx"
3362 } elseif [test_compiler_info xlc*] {
3363 set compile_flags "additional_flags=-qasm=gcc"
3364 } else {
3365 verbose "Could not compile with vsx support, returning 1" 2
3366 return 1
3367 }
3368
3369 # Compile a test program containing VSX instructions.
3370 set src {
3371 int main() {
3372 double a[2] = { 1.0, 2.0 };
3373 #ifdef __MACH__
3374 asm volatile ("lxvd2x v0,v0,%[addr]" : : [addr] "r" (a));
3375 #else
3376 asm volatile ("lxvd2x 0,0,%[addr]" : : [addr] "r" (a));
3377 #endif
3378 return 0;
3379 }
3380 }
3381 if {![gdb_simple_compile $me $src executable $compile_flags]} {
3382 return 1
3383 }
3384
3385 # No error message, compilation succeeded so now run it via gdb.
3386
3387 gdb_exit
3388 gdb_start
3389 gdb_reinitialize_dir $srcdir/$subdir
3390 gdb_load "$obj"
3391 gdb_run_cmd
3392 gdb_expect {
3393 -re ".*Illegal instruction.*${gdb_prompt} $" {
3394 verbose -log "\n$me VSX hardware not detected"
3395 set skip_vsx_tests 1
3396 }
3397 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3398 verbose -log "\n$me: VSX hardware detected"
3399 set skip_vsx_tests 0
3400 }
3401 default {
3402 warning "\n$me: default case taken"
3403 set skip_vsx_tests 1
3404 }
3405 }
3406 gdb_exit
3407 remote_file build delete $obj
3408
3409 verbose "$me: returning $skip_vsx_tests" 2
3410 return $skip_vsx_tests
3411 }
3412
3413 # Run a test on the target to see if it supports TSX hardware. Return 0 if so,
3414 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
3415
3416 gdb_caching_proc skip_tsx_tests {
3417 global srcdir subdir gdb_prompt inferior_exited_re
3418
3419 set me "skip_tsx_tests"
3420
3421 # Compile a test program.
3422 set src {
3423 int main() {
3424 asm volatile ("xbegin .L0");
3425 asm volatile ("xend");
3426 asm volatile (".L0: nop");
3427 return 0;
3428 }
3429 }
3430 if {![gdb_simple_compile $me $src executable]} {
3431 return 1
3432 }
3433
3434 # No error message, compilation succeeded so now run it via gdb.
3435
3436 gdb_exit
3437 gdb_start
3438 gdb_reinitialize_dir $srcdir/$subdir
3439 gdb_load "$obj"
3440 gdb_run_cmd
3441 gdb_expect {
3442 -re ".*Illegal instruction.*${gdb_prompt} $" {
3443 verbose -log "$me: TSX hardware not detected."
3444 set skip_tsx_tests 1
3445 }
3446 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3447 verbose -log "$me: TSX hardware detected."
3448 set skip_tsx_tests 0
3449 }
3450 default {
3451 warning "\n$me: default case taken."
3452 set skip_tsx_tests 1
3453 }
3454 }
3455 gdb_exit
3456 remote_file build delete $obj
3457
3458 verbose "$me: returning $skip_tsx_tests" 2
3459 return $skip_tsx_tests
3460 }
3461
3462 # Run a test on the target to see if it supports avx512bf16. Return 0 if so,
3463 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
3464
3465 gdb_caching_proc skip_avx512bf16_tests {
3466 global srcdir subdir gdb_prompt inferior_exited_re
3467
3468 set me "skip_avx512bf16_tests"
3469 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
3470 verbose "$me: target does not support avx512bf16, returning 1" 2
3471 return 1
3472 }
3473
3474 # Compile a test program.
3475 set src {
3476 int main() {
3477 asm volatile ("vcvtne2ps2bf16 %xmm0, %xmm1, %xmm0");
3478 return 0;
3479 }
3480 }
3481 if {![gdb_simple_compile $me $src executable]} {
3482 return 1
3483 }
3484
3485 # No error message, compilation succeeded so now run it via gdb.
3486
3487 gdb_exit
3488 gdb_start
3489 gdb_reinitialize_dir $srcdir/$subdir
3490 gdb_load "$obj"
3491 gdb_run_cmd
3492 gdb_expect {
3493 -re ".*Illegal instruction.*${gdb_prompt} $" {
3494 verbose -log "$me: avx512bf16 hardware not detected."
3495 set skip_avx512bf16_tests 1
3496 }
3497 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3498 verbose -log "$me: avx512bf16 hardware detected."
3499 set skip_avx512bf16_tests 0
3500 }
3501 default {
3502 warning "\n$me: default case taken."
3503 set skip_avx512bf16_tests 1
3504 }
3505 }
3506 gdb_exit
3507 remote_file build delete $obj
3508
3509 verbose "$me: returning $skip_avx512bf16_tests" 2
3510 return $skip_avx512bf16_tests
3511 }
3512
3513 # Run a test on the target to see if it supports avx512fp16. Return 0 if so,
3514 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
3515
3516 gdb_caching_proc skip_avx512fp16_tests {
3517 global srcdir subdir gdb_prompt inferior_exited_re
3518
3519 set me "skip_avx512fp16_tests"
3520 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
3521 verbose "$me: target does not support avx512fp16, returning 1" 2
3522 return 1
3523 }
3524
3525 # Compile a test program.
3526 set src {
3527 int main() {
3528 asm volatile ("vcvtps2phx %xmm1, %xmm0");
3529 return 0;
3530 }
3531 }
3532 if {![gdb_simple_compile $me $src executable]} {
3533 return 1
3534 }
3535
3536 # No error message, compilation succeeded so now run it via gdb.
3537
3538 gdb_exit
3539 gdb_start
3540 gdb_reinitialize_dir $srcdir/$subdir
3541 gdb_load "$obj"
3542 gdb_run_cmd
3543 gdb_expect {
3544 -re ".*Illegal instruction.*${gdb_prompt} $" {
3545 verbose -log "$me: avx512fp16 hardware not detected."
3546 set skip_avx512fp16_tests 1
3547 }
3548 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3549 verbose -log "$me: avx512fp16 hardware detected."
3550 set skip_avx512fp16_tests 0
3551 }
3552 default {
3553 warning "\n$me: default case taken."
3554 set skip_avx512fp16_tests 1
3555 }
3556 }
3557 gdb_exit
3558 remote_file build delete $obj
3559
3560 verbose "$me: returning $skip_avx512fp16_tests" 2
3561 return $skip_avx512fp16_tests
3562 }
3563
3564 # Run a test on the target to see if it supports btrace hardware. Return 0 if so,
3565 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
3566
3567 gdb_caching_proc skip_btrace_tests {
3568 global srcdir subdir gdb_prompt inferior_exited_re
3569
3570 set me "skip_btrace_tests"
3571 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
3572 verbose "$me: target does not support btrace, returning 1" 2
3573 return 1
3574 }
3575
3576 # Compile a test program.
3577 set src { int main() { return 0; } }
3578 if {![gdb_simple_compile $me $src executable]} {
3579 return 1
3580 }
3581
3582 # No error message, compilation succeeded so now run it via gdb.
3583
3584 gdb_exit
3585 gdb_start
3586 gdb_reinitialize_dir $srcdir/$subdir
3587 gdb_load $obj
3588 if ![runto_main] {
3589 return 1
3590 }
3591 # In case of an unexpected output, we return 2 as a fail value.
3592 set skip_btrace_tests 2
3593 gdb_test_multiple "record btrace" "check btrace support" {
3594 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
3595 set skip_btrace_tests 1
3596 }
3597 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
3598 set skip_btrace_tests 1
3599 }
3600 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
3601 set skip_btrace_tests 1
3602 }
3603 -re "^record btrace\r\n$gdb_prompt $" {
3604 set skip_btrace_tests 0
3605 }
3606 }
3607 gdb_exit
3608 remote_file build delete $obj
3609
3610 verbose "$me: returning $skip_btrace_tests" 2
3611 return $skip_btrace_tests
3612 }
3613
3614 # Run a test on the target to see if it supports btrace pt hardware.
3615 # Return 0 if so, 1 if it does not. Based on 'check_vmx_hw_available'
3616 # from the GCC testsuite.
3617
3618 gdb_caching_proc skip_btrace_pt_tests {
3619 global srcdir subdir gdb_prompt inferior_exited_re
3620
3621 set me "skip_btrace_tests"
3622 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
3623 verbose "$me: target does not support btrace, returning 1" 2
3624 return 1
3625 }
3626
3627 # Compile a test program.
3628 set src { int main() { return 0; } }
3629 if {![gdb_simple_compile $me $src executable]} {
3630 return 1
3631 }
3632
3633 # No error message, compilation succeeded so now run it via gdb.
3634
3635 gdb_exit
3636 gdb_start
3637 gdb_reinitialize_dir $srcdir/$subdir
3638 gdb_load $obj
3639 if ![runto_main] {
3640 return 1
3641 }
3642 # In case of an unexpected output, we return 2 as a fail value.
3643 set skip_btrace_tests 2
3644 gdb_test_multiple "record btrace pt" "check btrace pt support" {
3645 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
3646 set skip_btrace_tests 1
3647 }
3648 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
3649 set skip_btrace_tests 1
3650 }
3651 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
3652 set skip_btrace_tests 1
3653 }
3654 -re "support was disabled at compile time.*\r\n$gdb_prompt $" {
3655 set skip_btrace_tests 1
3656 }
3657 -re "^record btrace pt\r\n$gdb_prompt $" {
3658 set skip_btrace_tests 0
3659 }
3660 }
3661 gdb_exit
3662 remote_file build delete $obj
3663
3664 verbose "$me: returning $skip_btrace_tests" 2
3665 return $skip_btrace_tests
3666 }
3667
3668 # Run a test on the target to see if it supports Aarch64 SVE hardware.
3669 # Return 0 if so, 1 if it does not. Note this causes a restart of GDB.
3670
3671 gdb_caching_proc skip_aarch64_sve_tests {
3672 global srcdir subdir gdb_prompt inferior_exited_re
3673
3674 set me "skip_aarch64_sve_tests"
3675
3676 if { ![is_aarch64_target]} {
3677 return 1
3678 }
3679
3680 set compile_flags "{additional_flags=-march=armv8-a+sve}"
3681
3682 # Compile a test program containing SVE instructions.
3683 set src {
3684 int main() {
3685 asm volatile ("ptrue p0.b");
3686 return 0;
3687 }
3688 }
3689 if {![gdb_simple_compile $me $src executable $compile_flags]} {
3690 return 1
3691 }
3692
3693 # Compilation succeeded so now run it via gdb.
3694 clean_restart $obj
3695 gdb_run_cmd
3696 gdb_expect {
3697 -re ".*Illegal instruction.*${gdb_prompt} $" {
3698 verbose -log "\n$me sve hardware not detected"
3699 set skip_sve_tests 1
3700 }
3701 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3702 verbose -log "\n$me: sve hardware detected"
3703 set skip_sve_tests 0
3704 }
3705 default {
3706 warning "\n$me: default case taken"
3707 set skip_sve_tests 1
3708 }
3709 }
3710 gdb_exit
3711 remote_file build delete $obj
3712
3713 verbose "$me: returning $skip_sve_tests" 2
3714 return $skip_sve_tests
3715 }
3716
3717
3718 # A helper that compiles a test case to see if __int128 is supported.
3719 proc gdb_int128_helper {lang} {
3720 return [gdb_can_simple_compile "i128-for-$lang" {
3721 __int128 x;
3722 int main() { return 0; }
3723 } executable $lang]
3724 }
3725
3726 # Return true if the C compiler understands the __int128 type.
3727 gdb_caching_proc has_int128_c {
3728 return [gdb_int128_helper c]
3729 }
3730
3731 # Return true if the C++ compiler understands the __int128 type.
3732 gdb_caching_proc has_int128_cxx {
3733 return [gdb_int128_helper c++]
3734 }
3735
3736 # Return true if the IFUNC feature is unsupported.
3737 gdb_caching_proc skip_ifunc_tests {
3738 if [gdb_can_simple_compile ifunc {
3739 extern void f_ ();
3740 typedef void F (void);
3741 F* g (void) { return &f_; }
3742 void f () __attribute__ ((ifunc ("g")));
3743 } object] {
3744 return 0
3745 } else {
3746 return 1
3747 }
3748 }
3749
3750 # Return whether we should skip tests for showing inlined functions in
3751 # backtraces. Requires get_compiler_info and get_debug_format.
3752
3753 proc skip_inline_frame_tests {} {
3754 # GDB only recognizes inlining information in DWARF.
3755 if { ! [test_debug_format "DWARF \[0-9\]"] } {
3756 return 1
3757 }
3758
3759 # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line.
3760 if { ([test_compiler_info "gcc-2-*"]
3761 || [test_compiler_info "gcc-3-*"]
3762 || [test_compiler_info "gcc-4-0-*"]) } {
3763 return 1
3764 }
3765
3766 return 0
3767 }
3768
3769 # Return whether we should skip tests for showing variables from
3770 # inlined functions. Requires get_compiler_info and get_debug_format.
3771
3772 proc skip_inline_var_tests {} {
3773 # GDB only recognizes inlining information in DWARF.
3774 if { ! [test_debug_format "DWARF \[0-9\]"] } {
3775 return 1
3776 }
3777
3778 return 0
3779 }
3780
3781 # Return a 1 if we should skip tests that require hardware breakpoints
3782
3783 proc skip_hw_breakpoint_tests {} {
3784 # Skip tests if requested by the board (note that no_hardware_watchpoints
3785 # disables both watchpoints and breakpoints)
3786 if { [target_info exists gdb,no_hardware_watchpoints]} {
3787 return 1
3788 }
3789
3790 # These targets support hardware breakpoints natively
3791 if { [istarget "i?86-*-*"]
3792 || [istarget "x86_64-*-*"]
3793 || [istarget "ia64-*-*"]
3794 || [istarget "arm*-*-*"]
3795 || [istarget "aarch64*-*-*"]
3796 || [istarget "s390*-*-*"] } {
3797 return 0
3798 }
3799
3800 return 1
3801 }
3802
3803 # Return a 1 if we should skip tests that require hardware watchpoints
3804
3805 proc skip_hw_watchpoint_tests {} {
3806 # Skip tests if requested by the board
3807 if { [target_info exists gdb,no_hardware_watchpoints]} {
3808 return 1
3809 }
3810
3811 # These targets support hardware watchpoints natively
3812 # Note, not all Power 9 processors support hardware watchpoints due to a HW
3813 # bug. Use has_hw_wp_support to check do a runtime check for hardware
3814 # watchpoint support on Powerpc.
3815 if { [istarget "i?86-*-*"]
3816 || [istarget "x86_64-*-*"]
3817 || [istarget "ia64-*-*"]
3818 || [istarget "arm*-*-*"]
3819 || [istarget "aarch64*-*-*"]
3820 || ([istarget "powerpc*-*-linux*"] && [has_hw_wp_support])
3821 || [istarget "s390*-*-*"] } {
3822 return 0
3823 }
3824
3825 return 1
3826 }
3827
3828 # Return a 1 if we should skip tests that require *multiple* hardware
3829 # watchpoints to be active at the same time
3830
3831 proc skip_hw_watchpoint_multi_tests {} {
3832 if { [skip_hw_watchpoint_tests] } {
3833 return 1
3834 }
3835
3836 # These targets support just a single hardware watchpoint
3837 if { [istarget "arm*-*-*"]
3838 || [istarget "powerpc*-*-linux*"] } {
3839 return 1
3840 }
3841
3842 return 0
3843 }
3844
3845 # Return a 1 if we should skip tests that require read/access watchpoints
3846
3847 proc skip_hw_watchpoint_access_tests {} {
3848 if { [skip_hw_watchpoint_tests] } {
3849 return 1
3850 }
3851
3852 # These targets support just write watchpoints
3853 if { [istarget "s390*-*-*"] } {
3854 return 1
3855 }
3856
3857 return 0
3858 }
3859
3860 # Return 1 if we should skip tests that require the runtime unwinder
3861 # hook. This must be invoked while gdb is running, after shared
3862 # libraries have been loaded. This is needed because otherwise a
3863 # shared libgcc won't be visible.
3864
3865 proc skip_unwinder_tests {} {
3866 global gdb_prompt
3867
3868 set ok 0
3869 gdb_test_multiple "print _Unwind_DebugHook" "check for unwinder hook" {
3870 -re "= .*no debug info.*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
3871 }
3872 -re "= .*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
3873 set ok 1
3874 }
3875 -re "No symbol .* in current context.\r\n$gdb_prompt $" {
3876 }
3877 }
3878 if {!$ok} {
3879 gdb_test_multiple "info probe" "check for stap probe in unwinder" {
3880 -re ".*libgcc.*unwind.*\r\n$gdb_prompt $" {
3881 set ok 1
3882 }
3883 -re "\r\n$gdb_prompt $" {
3884 }
3885 }
3886 }
3887 return $ok
3888 }
3889
3890 # Return 1 if we should skip tests that require the libstdc++ stap
3891 # probes. This must be invoked while gdb is running, after shared
3892 # libraries have been loaded. PROMPT_REGEXP is the expected prompt.
3893
3894 proc skip_libstdcxx_probe_tests_prompt { prompt_regexp } {
3895 set supported 0
3896 gdb_test_multiple "info probe" "check for stap probe in libstdc++" \
3897 -prompt "$prompt_regexp" {
3898 -re ".*libstdcxx.*catch.*\r\n$prompt_regexp" {
3899 set supported 1
3900 }
3901 -re "\r\n$prompt_regexp" {
3902 }
3903 }
3904 set skip [expr !$supported]
3905 return $skip
3906 }
3907
3908 # As skip_libstdcxx_probe_tests_prompt, with gdb_prompt.
3909
3910 proc skip_libstdcxx_probe_tests {} {
3911 global gdb_prompt
3912 return [skip_libstdcxx_probe_tests_prompt "$gdb_prompt $"]
3913 }
3914
3915 # Return 1 if we should skip tests of the "compile" feature.
3916 # This must be invoked after the inferior has been started.
3917
3918 proc skip_compile_feature_tests {} {
3919 global gdb_prompt
3920
3921 set result 0
3922 gdb_test_multiple "compile code -- ;" "check for working compile command" {
3923 "Could not load libcc1.*\r\n$gdb_prompt $" {
3924 set result 1
3925 }
3926 -re "Command not supported on this host\\..*\r\n$gdb_prompt $" {
3927 set result 1
3928 }
3929 -re "\r\n$gdb_prompt $" {
3930 }
3931 }
3932 return $result
3933 }
3934
3935 # Helper for gdb_is_target_* procs. TARGET_NAME is the name of the target
3936 # we're looking for (used to build the test name). TARGET_STACK_REGEXP
3937 # is a regexp that will match the output of "maint print target-stack" if
3938 # the target in question is currently pushed. PROMPT_REGEXP is a regexp
3939 # matching the expected prompt after the command output.
3940 #
3941 # NOTE: GDB must be running BEFORE this procedure is called!
3942
3943 proc gdb_is_target_1 { target_name target_stack_regexp prompt_regexp } {
3944 global gdb_spawn_id
3945
3946 # Throw a Tcl error if gdb isn't already started.
3947 if {![info exists gdb_spawn_id]} {
3948 error "gdb_is_target_1 called with no running gdb instance"
3949 }
3950
3951 set test "probe for target ${target_name}"
3952 gdb_test_multiple "maint print target-stack" $test \
3953 -prompt "$prompt_regexp" {
3954 -re "${target_stack_regexp}${prompt_regexp}" {
3955 pass $test
3956 return 1
3957 }
3958 -re "$prompt_regexp" {
3959 pass $test
3960 }
3961 }
3962 return 0
3963 }
3964
3965 # Helper for gdb_is_target_remote where the expected prompt is variable.
3966 #
3967 # NOTE: GDB must be running BEFORE this procedure is called!
3968
3969 proc gdb_is_target_remote_prompt { prompt_regexp } {
3970 return [gdb_is_target_1 "remote" ".*emote target using gdb-specific protocol.*" $prompt_regexp]
3971 }
3972
3973 # Check whether we're testing with the remote or extended-remote
3974 # targets.
3975 #
3976 # NOTE: GDB must be running BEFORE this procedure is called!
3977
3978 proc gdb_is_target_remote { } {
3979 global gdb_prompt
3980
3981 return [gdb_is_target_remote_prompt "$gdb_prompt $"]
3982 }
3983
3984 # Check whether we're testing with the native target.
3985 #
3986 # NOTE: GDB must be running BEFORE this procedure is called!
3987
3988 proc gdb_is_target_native { } {
3989 global gdb_prompt
3990
3991 return [gdb_is_target_1 "native" ".*native \\(Native process\\).*" "$gdb_prompt $"]
3992 }
3993
3994 # Return the effective value of use_gdb_stub.
3995 #
3996 # If the use_gdb_stub global has been set (it is set when the gdb process is
3997 # spawned), return that. Otherwise, return the value of the use_gdb_stub
3998 # property from the board file.
3999 #
4000 # This is the preferred way of checking use_gdb_stub, since it allows to check
4001 # the value before the gdb has been spawned and it will return the correct value
4002 # even when it was overriden by the test.
4003 #
4004 # Note that stub targets are not able to spawn new inferiors. Use this
4005 # check for skipping respective tests.
4006
4007 proc use_gdb_stub {} {
4008 global use_gdb_stub
4009
4010 if [info exists use_gdb_stub] {
4011 return $use_gdb_stub
4012 }
4013
4014 return [target_info exists use_gdb_stub]
4015 }
4016
4017 # Return 1 if the current remote target is an instance of our GDBserver, 0
4018 # otherwise. Return -1 if there was an error and we can't tell.
4019
4020 gdb_caching_proc target_is_gdbserver {
4021 global gdb_prompt
4022
4023 set is_gdbserver -1
4024 set test "probing for GDBserver"
4025
4026 gdb_test_multiple "monitor help" $test {
4027 -re "The following monitor commands are supported.*Quit GDBserver.*$gdb_prompt $" {
4028 set is_gdbserver 1
4029 }
4030 -re "$gdb_prompt $" {
4031 set is_gdbserver 0
4032 }
4033 }
4034
4035 if { $is_gdbserver == -1 } {
4036 verbose -log "Unable to tell whether we are using GDBserver or not."
4037 }
4038
4039 return $is_gdbserver
4040 }
4041
4042 # N.B. compiler_info is intended to be local to this file.
4043 # Call test_compiler_info with no arguments to fetch its value.
4044 # Yes, this is counterintuitive when there's get_compiler_info,
4045 # but that's the current API.
4046 if [info exists compiler_info] {
4047 unset compiler_info
4048 }
4049
4050 set gcc_compiled 0
4051
4052 # Figure out what compiler I am using.
4053 # The result is cached so only the first invocation runs the compiler.
4054 #
4055 # ARG can be empty or "C++". If empty, "C" is assumed.
4056 #
4057 # There are several ways to do this, with various problems.
4058 #
4059 # [ gdb_compile -E $ifile -o $binfile.ci ]
4060 # source $binfile.ci
4061 #
4062 # Single Unix Spec v3 says that "-E -o ..." together are not
4063 # specified. And in fact, the native compiler on hp-ux 11 (among
4064 # others) does not work with "-E -o ...". Most targets used to do
4065 # this, and it mostly worked, because it works with gcc.
4066 #
4067 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
4068 # source $binfile.ci
4069 #
4070 # This avoids the problem with -E and -o together. This almost works
4071 # if the build machine is the same as the host machine, which is
4072 # usually true of the targets which are not gcc. But this code does
4073 # not figure which compiler to call, and it always ends up using the C
4074 # compiler. Not good for setting hp_aCC_compiler. Target
4075 # hppa*-*-hpux* used to do this.
4076 #
4077 # [ gdb_compile -E $ifile > $binfile.ci ]
4078 # source $binfile.ci
4079 #
4080 # dejagnu target_compile says that it supports output redirection,
4081 # but the code is completely different from the normal path and I
4082 # don't want to sweep the mines from that path. So I didn't even try
4083 # this.
4084 #
4085 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
4086 # eval $cppout
4087 #
4088 # I actually do this for all targets now. gdb_compile runs the right
4089 # compiler, and TCL captures the output, and I eval the output.
4090 #
4091 # Unfortunately, expect logs the output of the command as it goes by,
4092 # and dejagnu helpfully prints a second copy of it right afterwards.
4093 # So I turn off expect logging for a moment.
4094 #
4095 # [ gdb_compile $ifile $ciexe_file executable $args ]
4096 # [ remote_exec $ciexe_file ]
4097 # [ source $ci_file.out ]
4098 #
4099 # I could give up on -E and just do this.
4100 # I didn't get desperate enough to try this.
4101 #
4102 # -- chastain 2004-01-06
4103
4104 proc get_compiler_info {{arg ""}} {
4105 # For compiler.c and compiler.cc
4106 global srcdir
4107
4108 # I am going to play with the log to keep noise out.
4109 global outdir
4110 global tool
4111
4112 # These come from compiler.c or compiler.cc
4113 global compiler_info
4114
4115 # Legacy global data symbols.
4116 global gcc_compiled
4117
4118 if [info exists compiler_info] {
4119 # Already computed.
4120 return 0
4121 }
4122
4123 # Choose which file to preprocess.
4124 set ifile "${srcdir}/lib/compiler.c"
4125 if { $arg == "c++" } {
4126 set ifile "${srcdir}/lib/compiler.cc"
4127 }
4128
4129 # Run $ifile through the right preprocessor.
4130 # Toggle gdb.log to keep the compiler output out of the log.
4131 set saved_log [log_file -info]
4132 log_file
4133 if [is_remote host] {
4134 # We have to use -E and -o together, despite the comments
4135 # above, because of how DejaGnu handles remote host testing.
4136 set ppout "$outdir/compiler.i"
4137 gdb_compile "${ifile}" "$ppout" preprocess [list "$arg" quiet getting_compiler_info]
4138 set file [open $ppout r]
4139 set cppout [read $file]
4140 close $file
4141 } else {
4142 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$arg" quiet getting_compiler_info] ]
4143 }
4144 eval log_file $saved_log
4145
4146 # Eval the output.
4147 set unknown 0
4148 foreach cppline [ split "$cppout" "\n" ] {
4149 if { [ regexp "^#" "$cppline" ] } {
4150 # line marker
4151 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
4152 # blank line
4153 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
4154 # eval this line
4155 verbose "get_compiler_info: $cppline" 2
4156 eval "$cppline"
4157 } else {
4158 # unknown line
4159 verbose -log "get_compiler_info: $cppline"
4160 set unknown 1
4161 }
4162 }
4163
4164 # Set to unknown if for some reason compiler_info didn't get defined.
4165 if ![info exists compiler_info] {
4166 verbose -log "get_compiler_info: compiler_info not provided"
4167 set compiler_info "unknown"
4168 }
4169 # Also set to unknown compiler if any diagnostics happened.
4170 if { $unknown } {
4171 verbose -log "get_compiler_info: got unexpected diagnostics"
4172 set compiler_info "unknown"
4173 }
4174
4175 # Set the legacy symbols.
4176 set gcc_compiled 0
4177 regexp "^gcc-(\[0-9\]+)-" "$compiler_info" matchall gcc_compiled
4178
4179 # Log what happened.
4180 verbose -log "get_compiler_info: $compiler_info"
4181
4182 # Most compilers will evaluate comparisons and other boolean
4183 # operations to 0 or 1.
4184 uplevel \#0 { set true 1 }
4185 uplevel \#0 { set false 0 }
4186
4187 return 0
4188 }
4189
4190 # Return the compiler_info string if no arg is provided.
4191 # Otherwise the argument is a glob-style expression to match against
4192 # compiler_info.
4193
4194 proc test_compiler_info { {compiler ""} } {
4195 global compiler_info
4196 get_compiler_info
4197
4198 # If no arg, return the compiler_info string.
4199 if [string match "" $compiler] {
4200 return $compiler_info
4201 }
4202
4203 return [string match $compiler $compiler_info]
4204 }
4205
4206 # Return the gcc major version, or -1.
4207 # For gcc 4.8.5, the major version is 4.8.
4208 # For gcc 7.5.0, the major version 7.
4209
4210 proc gcc_major_version { } {
4211 global compiler_info
4212 global decimal
4213 if { ![test_compiler_info "gcc-*"] } {
4214 return -1
4215 }
4216 set res [regexp gcc-($decimal)-($decimal)- $compiler_info \
4217 dummy_var major minor]
4218 if { $res != 1 } {
4219 return -1
4220 }
4221 if { $major >= 5} {
4222 return $major
4223 }
4224 return $major.$minor
4225 }
4226
4227 proc current_target_name { } {
4228 global target_info
4229 if [info exists target_info(target,name)] {
4230 set answer $target_info(target,name)
4231 } else {
4232 set answer ""
4233 }
4234 return $answer
4235 }
4236
4237 set gdb_wrapper_initialized 0
4238 set gdb_wrapper_target ""
4239 set gdb_wrapper_file ""
4240 set gdb_wrapper_flags ""
4241
4242 proc gdb_wrapper_init { args } {
4243 global gdb_wrapper_initialized
4244 global gdb_wrapper_file
4245 global gdb_wrapper_flags
4246 global gdb_wrapper_target
4247
4248 if { $gdb_wrapper_initialized == 1 } { return; }
4249
4250 if {[target_info exists needs_status_wrapper] && \
4251 [target_info needs_status_wrapper] != "0"} {
4252 set result [build_wrapper "testglue.o"]
4253 if { $result != "" } {
4254 set gdb_wrapper_file [lindex $result 0]
4255 if ![is_remote host] {
4256 set gdb_wrapper_file [file join [pwd] $gdb_wrapper_file]
4257 }
4258 set gdb_wrapper_flags [lindex $result 1]
4259 } else {
4260 warning "Status wrapper failed to build."
4261 }
4262 } else {
4263 set gdb_wrapper_file ""
4264 set gdb_wrapper_flags ""
4265 }
4266 verbose "set gdb_wrapper_file = $gdb_wrapper_file"
4267 set gdb_wrapper_initialized 1
4268 set gdb_wrapper_target [current_target_name]
4269 }
4270
4271 # Determine options that we always want to pass to the compiler.
4272 gdb_caching_proc universal_compile_options {
4273 set me "universal_compile_options"
4274 set options {}
4275
4276 set src [standard_temp_file ccopts[pid].c]
4277 set obj [standard_temp_file ccopts[pid].o]
4278
4279 gdb_produce_source $src {
4280 int foo(void) { return 0; }
4281 }
4282
4283 # Try an option for disabling colored diagnostics. Some compilers
4284 # yield colored diagnostics by default (when run from a tty) unless
4285 # such an option is specified.
4286 set opt "additional_flags=-fdiagnostics-color=never"
4287 set lines [target_compile $src $obj object [list "quiet" $opt]]
4288 if [string match "" $lines] then {
4289 # Seems to have worked; use the option.
4290 lappend options $opt
4291 }
4292 file delete $src
4293 file delete $obj
4294
4295 verbose "$me: returning $options" 2
4296 return $options
4297 }
4298
4299 # Compile the code in $code to a file based on $name, using the flags
4300 # $compile_flag as well as debug, nowarning and quiet.
4301 # Return 1 if code can be compiled
4302 # Leave the file name of the resulting object in the upvar object.
4303
4304 proc gdb_simple_compile {name code {type object} {compile_flags {}} {object obj}} {
4305 upvar $object obj
4306
4307 switch -regexp -- $type {
4308 "executable" {
4309 set postfix "x"
4310 }
4311 "object" {
4312 set postfix "o"
4313 }
4314 "preprocess" {
4315 set postfix "i"
4316 }
4317 "assembly" {
4318 set postfix "s"
4319 }
4320 }
4321 set ext "c"
4322 foreach flag $compile_flags {
4323 if { "$flag" == "go" } {
4324 set ext "go"
4325 break
4326 }
4327 }
4328 set src [standard_temp_file $name-[pid].$ext]
4329 set obj [standard_temp_file $name-[pid].$postfix]
4330 set compile_flags [concat $compile_flags {debug nowarnings quiet}]
4331
4332 gdb_produce_source $src $code
4333
4334 verbose "$name: compiling testfile $src" 2
4335 set lines [gdb_compile $src $obj $type $compile_flags]
4336
4337 file delete $src
4338
4339 if ![string match "" $lines] then {
4340 verbose "$name: compilation failed, returning 0" 2
4341 return 0
4342 }
4343 return 1
4344 }
4345
4346 # Compile the code in $code to a file based on $name, using the flags
4347 # $compile_flag as well as debug, nowarning and quiet.
4348 # Return 1 if code can be compiled
4349 # Delete all created files and objects.
4350
4351 proc gdb_can_simple_compile {name code {type object} {compile_flags ""}} {
4352 set ret [gdb_simple_compile $name $code $type $compile_flags temp_obj]
4353 file delete $temp_obj
4354 return $ret
4355 }
4356
4357 # Some targets need to always link a special object in. Save its path here.
4358 global gdb_saved_set_unbuffered_mode_obj
4359 set gdb_saved_set_unbuffered_mode_obj ""
4360
4361 # Compile source files specified by SOURCE into a binary of type TYPE at path
4362 # DEST. gdb_compile is implemented using DejaGnu's target_compile, so the type
4363 # parameter and most options are passed directly to it.
4364 #
4365 # The type can be one of the following:
4366 #
4367 # - object: Compile into an object file.
4368 # - executable: Compile and link into an executable.
4369 # - preprocess: Preprocess the source files.
4370 # - assembly: Generate assembly listing.
4371 #
4372 # The following options are understood and processed by gdb_compile:
4373 #
4374 # - shlib=so_path: Add SO_PATH to the sources, and enable some target-specific
4375 # quirks to be able to use shared libraries.
4376 # - shlib_load: Link with appropriate libraries to allow the test to
4377 # dynamically load libraries at runtime. For example, on Linux, this adds
4378 # -ldl so that the test can use dlopen.
4379 # - nowarnings: Inhibit all compiler warnings.
4380 # - pie: Force creation of PIE executables.
4381 # - nopie: Prevent creation of PIE executables.
4382 # - macros: Add the required compiler flag to include macro information in
4383 # debug information
4384 # - text_segment=addr: Tell the linker to place the text segment at ADDR.
4385 #
4386 # And here are some of the not too obscure options understood by DejaGnu that
4387 # influence the compilation:
4388 #
4389 # - additional_flags=flag: Add FLAG to the compiler flags.
4390 # - libs=library: Add LIBRARY to the libraries passed to the linker. The
4391 # argument can be a file, in which case it's added to the sources, or a
4392 # linker flag.
4393 # - ldflags=flag: Add FLAG to the linker flags.
4394 # - incdir=path: Add PATH to the searched include directories.
4395 # - libdir=path: Add PATH to the linker searched directories.
4396 # - ada, c++, f77, f90, go, rust: Compile the file as Ada, C++,
4397 # Fortran 77, Fortran 90, Go or Rust.
4398 # - debug: Build with debug information.
4399 # - optimize: Build with optimization.
4400
4401 proc gdb_compile {source dest type options} {
4402 global GDB_TESTCASE_OPTIONS
4403 global gdb_wrapper_file
4404 global gdb_wrapper_flags
4405 global srcdir
4406 global objdir
4407 global gdb_saved_set_unbuffered_mode_obj
4408
4409 set outdir [file dirname $dest]
4410
4411 # Add platform-specific options if a shared library was specified using
4412 # "shlib=librarypath" in OPTIONS.
4413 set new_options {}
4414 if {[lsearch -exact $options rust] != -1} {
4415 # -fdiagnostics-color is not a rustcc option.
4416 } else {
4417 set new_options [universal_compile_options]
4418 }
4419
4420 # Some C/C++ testcases unconditionally pass -Wno-foo as additional
4421 # options to disable some warning. That is OK with GCC, because
4422 # by design, GCC accepts any -Wno-foo option, even if it doesn't
4423 # support -Wfoo. Clang however warns about unknown -Wno-foo by
4424 # default, unless you pass -Wno-unknown-warning-option as well.
4425 # We do that here, so that individual testcases don't have to
4426 # worry about it.
4427 if {[lsearch -exact $options getting_compiler_info] == -1
4428 && [lsearch -exact $options rust] == -1
4429 && [lsearch -exact $options ada] == -1
4430 && [lsearch -exact $options f77] == -1
4431 && [lsearch -exact $options f90] == -1
4432 && [lsearch -exact $options go] == -1
4433 && [test_compiler_info "clang-*"]} {
4434 lappend new_options "additional_flags=-Wno-unknown-warning-option"
4435 }
4436
4437 # Treating .c input files as C++ is deprecated in Clang, so
4438 # explicitly force C++ language.
4439 if { [lsearch -exact $options getting_compiler_info] == -1
4440 && [lsearch -exact $options c++] != -1
4441 && [string match *.c $source] != 0 } {
4442
4443 # gdb_compile cannot handle this combination of options, the
4444 # result is a command like "clang -x c++ foo.c bar.so -o baz"
4445 # which tells Clang to treat bar.so as C++. The solution is
4446 # to call gdb_compile twice--once to compile, once to link--
4447 # either directly, or via build_executable_from_specs.
4448 if { [lsearch $options shlib=*] != -1 } {
4449 error "incompatible gdb_compile options"
4450 }
4451
4452 if {[test_compiler_info "clang-*"]} {
4453 lappend new_options early_flags=-x\ c++
4454 }
4455 }
4456
4457 # Place (and look for) Fortran `.mod` files in the output
4458 # directory for this specific test.
4459 if {[lsearch -exact $options f77] != -1 \
4460 || [lsearch -exact $options f90] != -1 } {
4461 # Fortran compile.
4462 set mod_path [standard_output_file ""]
4463 if [test_compiler_info "gcc-*"] {
4464 lappend new_options "additional_flags=-J${mod_path}"
4465 }
4466 }
4467
4468 set shlib_found 0
4469 set shlib_load 0
4470 set getting_compiler_info 0
4471 foreach opt $options {
4472 if {[regexp {^shlib=(.*)} $opt dummy_var shlib_name]
4473 && $type == "executable"} {
4474 if [test_compiler_info "xlc-*"] {
4475 # IBM xlc compiler doesn't accept shared library named other
4476 # than .so: use "-Wl," to bypass this
4477 lappend source "-Wl,$shlib_name"
4478 } elseif { ([istarget "*-*-mingw*"]
4479 || [istarget *-*-cygwin*]
4480 || [istarget *-*-pe*])} {
4481 lappend source "${shlib_name}.a"
4482 } else {
4483 lappend source $shlib_name
4484 }
4485 if { $shlib_found == 0 } {
4486 set shlib_found 1
4487 if { ([istarget "*-*-mingw*"]
4488 || [istarget *-*-cygwin*]) } {
4489 lappend new_options "additional_flags=-Wl,--enable-auto-import"
4490 }
4491 if { [test_compiler_info "gcc-*"] || [test_compiler_info "clang-*"] } {
4492 # Undo debian's change in the default.
4493 # Put it at the front to not override any user-provided
4494 # value, and to make sure it appears in front of all the
4495 # shlibs!
4496 lappend new_options "early_flags=-Wl,--no-as-needed"
4497 }
4498 }
4499 } elseif { $opt == "shlib_load" && $type == "executable" } {
4500 set shlib_load 1
4501 } elseif { $opt == "getting_compiler_info" } {
4502 # If this is set, calling test_compiler_info will cause recursion.
4503 set getting_compiler_info 1
4504 } elseif {[regexp "^text_segment=(.*)" $opt dummy_var addr]} {
4505 if { [linker_supports_Ttext_segment_flag] } {
4506 # For GNU ld.
4507 lappend new_options "ldflags=-Wl,-Ttext-segment=$addr"
4508 } elseif { [linker_supports_image_base_flag] } {
4509 # For LLVM's lld.
4510 lappend new_options "ldflags=-Wl,--image-base=$addr"
4511 } elseif { [linker_supports_Ttext_flag] } {
4512 # For old GNU gold versions.
4513 lappend new_options "ldflags=-Wl,-Ttext=$addr"
4514 } else {
4515 error "Don't know how to handle text_segment option."
4516 }
4517 } else {
4518 lappend new_options $opt
4519 }
4520 }
4521
4522 # Ensure stack protector is disabled for GCC, as this causes problems with
4523 # DWARF line numbering.
4524 # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88432
4525 # This option defaults to on for Debian/Ubuntu.
4526 if { $getting_compiler_info == 0
4527 && [test_compiler_info {gcc-*-*}]
4528 && !([test_compiler_info {gcc-[0-3]-*}]
4529 || [test_compiler_info {gcc-4-0-*}])
4530 && [lsearch -exact $options rust] == -1} {
4531 # Put it at the front to not override any user-provided value.
4532 lappend new_options "early_flags=-fno-stack-protector"
4533 }
4534
4535 # Because we link with libraries using their basename, we may need
4536 # (depending on the platform) to set a special rpath value, to allow
4537 # the executable to find the libraries it depends on.
4538 if { $shlib_load || $shlib_found } {
4539 if { ([istarget "*-*-mingw*"]
4540 || [istarget *-*-cygwin*]
4541 || [istarget *-*-pe*]) } {
4542 # Do not need anything.
4543 } elseif { [istarget *-*-freebsd*] || [istarget *-*-openbsd*] } {
4544 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
4545 } else {
4546 if { $shlib_load } {
4547 lappend new_options "libs=-ldl"
4548 }
4549 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
4550 }
4551 }
4552 set options $new_options
4553
4554 if [info exists GDB_TESTCASE_OPTIONS] {
4555 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS"
4556 }
4557 verbose "options are $options"
4558 verbose "source is $source $dest $type $options"
4559
4560 gdb_wrapper_init
4561
4562 if {[target_info exists needs_status_wrapper] && \
4563 [target_info needs_status_wrapper] != "0" && \
4564 $gdb_wrapper_file != "" } {
4565 lappend options "libs=${gdb_wrapper_file}"
4566 lappend options "ldflags=${gdb_wrapper_flags}"
4567 }
4568
4569 # Replace the "nowarnings" option with the appropriate additional_flags
4570 # to disable compiler warnings.
4571 set nowarnings [lsearch -exact $options nowarnings]
4572 if {$nowarnings != -1} {
4573 if [target_info exists gdb,nowarnings_flag] {
4574 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
4575 } else {
4576 set flag "additional_flags=-w"
4577 }
4578 set options [lreplace $options $nowarnings $nowarnings $flag]
4579 }
4580
4581 # Replace the "pie" option with the appropriate compiler and linker flags
4582 # to enable PIE executables.
4583 set pie [lsearch -exact $options pie]
4584 if {$pie != -1} {
4585 if [target_info exists gdb,pie_flag] {
4586 set flag "additional_flags=[target_info gdb,pie_flag]"
4587 } else {
4588 # For safety, use fPIE rather than fpie. On AArch64, m68k, PowerPC
4589 # and SPARC, fpie can cause compile errors due to the GOT exceeding
4590 # a maximum size. On other architectures the two flags are
4591 # identical (see the GCC manual). Note Debian9 and Ubuntu16.10
4592 # onwards default GCC to using fPIE. If you do require fpie, then
4593 # it can be set using the pie_flag.
4594 set flag "additional_flags=-fPIE"
4595 }
4596 set options [lreplace $options $pie $pie $flag]
4597
4598 if [target_info exists gdb,pie_ldflag] {
4599 set flag "ldflags=[target_info gdb,pie_ldflag]"
4600 } else {
4601 set flag "ldflags=-pie"
4602 }
4603 lappend options "$flag"
4604 }
4605
4606 # Replace the "nopie" option with the appropriate compiler and linker
4607 # flags to disable PIE executables.
4608 set nopie [lsearch -exact $options nopie]
4609 if {$nopie != -1} {
4610 if [target_info exists gdb,nopie_flag] {
4611 set flag "additional_flags=[target_info gdb,nopie_flag]"
4612 } else {
4613 set flag "additional_flags=-fno-pie"
4614 }
4615 set options [lreplace $options $nopie $nopie $flag]
4616
4617 if [target_info exists gdb,nopie_ldflag] {
4618 set flag "ldflags=[target_info gdb,nopie_ldflag]"
4619 } else {
4620 set flag "ldflags=-no-pie"
4621 }
4622 lappend options "$flag"
4623 }
4624
4625 set macros [lsearch -exact $options macros]
4626 if {$macros != -1} {
4627 if { [test_compiler_info "clang-*"] } {
4628 set flag "additional_flags=-fdebug-macro"
4629 } else {
4630 set flag "additional_flags=-g3"
4631 }
4632
4633 set options [lreplace $options $macros $macros $flag]
4634 }
4635
4636 if { $type == "executable" } {
4637 if { ([istarget "*-*-mingw*"]
4638 || [istarget "*-*-*djgpp"]
4639 || [istarget "*-*-cygwin*"])} {
4640 # Force output to unbuffered mode, by linking in an object file
4641 # with a global contructor that calls setvbuf.
4642 #
4643 # Compile the special object separately for two reasons:
4644 # 1) Insulate it from $options.
4645 # 2) Avoid compiling it for every gdb_compile invocation,
4646 # which is time consuming, especially if we're remote
4647 # host testing.
4648 #
4649 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
4650 verbose "compiling gdb_saved_set_unbuffered_obj"
4651 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
4652 set unbuf_obj ${objdir}/set_unbuffered_mode.o
4653
4654 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
4655 if { $result != "" } {
4656 return $result
4657 }
4658 if {[is_remote host]} {
4659 set gdb_saved_set_unbuffered_mode_obj set_unbuffered_mode_saved.o
4660 } else {
4661 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
4662 }
4663 # Link a copy of the output object, because the
4664 # original may be automatically deleted.
4665 remote_download host $unbuf_obj $gdb_saved_set_unbuffered_mode_obj
4666 } else {
4667 verbose "gdb_saved_set_unbuffered_obj already compiled"
4668 }
4669
4670 # Rely on the internal knowledge that the global ctors are ran in
4671 # reverse link order. In that case, we can use ldflags to
4672 # avoid copying the object file to the host multiple
4673 # times.
4674 # This object can only be added if standard libraries are
4675 # used. Thus, we need to disable it if -nostdlib option is used
4676 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
4677 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
4678 }
4679 }
4680 }
4681
4682 set result [target_compile $source $dest $type $options]
4683
4684 # Prune uninteresting compiler (and linker) output.
4685 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
4686
4687 regsub "\[\r\n\]*$" "$result" "" result
4688 regsub "^\[\r\n\]*" "$result" "" result
4689
4690 if { $type == "executable" && $result == "" \
4691 && ($nopie != -1 || $pie != -1) } {
4692 set is_pie [exec_is_pie "$dest"]
4693 if { $nopie != -1 && $is_pie == 1 } {
4694 set result "nopie failed to prevent PIE executable"
4695 } elseif { $pie != -1 && $is_pie == 0 } {
4696 set result "pie failed to generate PIE executable"
4697 }
4698 }
4699
4700 if {[lsearch $options quiet] < 0} {
4701 # We shall update this on a per language basis, to avoid
4702 # changing the entire testsuite in one go.
4703 if {[lsearch $options f77] >= 0} {
4704 gdb_compile_test $source $result
4705 } elseif { $result != "" } {
4706 clone_output "gdb compile failed, $result"
4707 }
4708 }
4709 return $result
4710 }
4711
4712
4713 # This is just like gdb_compile, above, except that it tries compiling
4714 # against several different thread libraries, to see which one this
4715 # system has.
4716 proc gdb_compile_pthreads {source dest type options} {
4717 if {$type != "executable"} {
4718 return [gdb_compile $source $dest $type $options]
4719 }
4720 set built_binfile 0
4721 set why_msg "unrecognized error"
4722 foreach lib {-lpthreads -lpthread -lthread ""} {
4723 # This kind of wipes out whatever libs the caller may have
4724 # set. Or maybe theirs will override ours. How infelicitous.
4725 set options_with_lib [concat $options [list libs=$lib quiet]]
4726 set ccout [gdb_compile $source $dest $type $options_with_lib]
4727 switch -regexp -- $ccout {
4728 ".*no posix threads support.*" {
4729 set why_msg "missing threads include file"
4730 break
4731 }
4732 ".*cannot open -lpthread.*" {
4733 set why_msg "missing runtime threads library"
4734 }
4735 ".*Can't find library for -lpthread.*" {
4736 set why_msg "missing runtime threads library"
4737 }
4738 {^$} {
4739 pass "successfully compiled posix threads test case"
4740 set built_binfile 1
4741 break
4742 }
4743 }
4744 }
4745 if {!$built_binfile} {
4746 unsupported "couldn't compile [file tail $source]: ${why_msg}"
4747 return -1
4748 }
4749 }
4750
4751 # Build a shared library from SOURCES.
4752
4753 proc gdb_compile_shlib_1 {sources dest options} {
4754 set obj_options $options
4755
4756 set ada 0
4757 if { [lsearch -exact $options "ada"] >= 0 } {
4758 set ada 1
4759 }
4760
4761 set info_options ""
4762 if { [lsearch -exact $options "c++"] >= 0 } {
4763 set info_options "c++"
4764 }
4765 if [get_compiler_info ${info_options}] {
4766 return -1
4767 }
4768
4769 switch -glob [test_compiler_info] {
4770 "xlc-*" {
4771 lappend obj_options "additional_flags=-qpic"
4772 }
4773 "clang-*" {
4774 if { [istarget "*-*-cygwin*"]
4775 || [istarget "*-*-mingw*"] } {
4776 lappend obj_options "additional_flags=-fPIC"
4777 } else {
4778 lappend obj_options "additional_flags=-fpic"
4779 }
4780 }
4781 "gcc-*" {
4782 if { [istarget "powerpc*-*-aix*"]
4783 || [istarget "rs6000*-*-aix*"]
4784 || [istarget "*-*-cygwin*"]
4785 || [istarget "*-*-mingw*"]
4786 || [istarget "*-*-pe*"] } {
4787 lappend obj_options "additional_flags=-fPIC"
4788 } else {
4789 lappend obj_options "additional_flags=-fpic"
4790 }
4791 }
4792 "icc-*" {
4793 lappend obj_options "additional_flags=-fpic"
4794 }
4795 default {
4796 # don't know what the compiler is...
4797 lappend obj_options "additional_flags=-fPIC"
4798 }
4799 }
4800
4801 set outdir [file dirname $dest]
4802 set objects ""
4803 foreach source $sources {
4804 if {[file extension $source] == ".o"} {
4805 # Already a .o file.
4806 lappend objects $source
4807 continue
4808 }
4809
4810 set sourcebase [file tail $source]
4811
4812 if { $ada } {
4813 # Gnatmake doesn't like object name foo.adb.o, use foo.o.
4814 set sourcebase [file rootname $sourcebase]
4815 }
4816 set object ${outdir}/${sourcebase}.o
4817
4818 if { $ada } {
4819 # Use gdb_compile_ada_1 instead of gdb_compile_ada to avoid the
4820 # PASS message.
4821 if {[gdb_compile_ada_1 $source $object object \
4822 $obj_options] != ""} {
4823 return -1
4824 }
4825 } else {
4826 if {[gdb_compile $source $object object \
4827 $obj_options] != ""} {
4828 return -1
4829 }
4830 }
4831
4832 lappend objects $object
4833 }
4834
4835 set link_options $options
4836 if { $ada } {
4837 # If we try to use gnatmake for the link, it will interpret the
4838 # object file as an .adb file. Remove ada from the options to
4839 # avoid it.
4840 set idx [lsearch $link_options "ada"]
4841 set link_options [lreplace $link_options $idx $idx]
4842 }
4843 if [test_compiler_info "xlc-*"] {
4844 lappend link_options "additional_flags=-qmkshrobj"
4845 } else {
4846 lappend link_options "additional_flags=-shared"
4847
4848 if { ([istarget "*-*-mingw*"]
4849 || [istarget *-*-cygwin*]
4850 || [istarget *-*-pe*]) } {
4851 if { [is_remote host] } {
4852 set name [file tail ${dest}]
4853 } else {
4854 set name ${dest}
4855 }
4856 lappend link_options "additional_flags=-Wl,--out-implib,${name}.a"
4857 } else {
4858 # Set the soname of the library. This causes the linker on ELF
4859 # systems to create the DT_NEEDED entry in the executable referring
4860 # to the soname of the library, and not its absolute path. This
4861 # (using the absolute path) would be problem when testing on a
4862 # remote target.
4863 #
4864 # In conjunction with setting the soname, we add the special
4865 # rpath=$ORIGIN value when building the executable, so that it's
4866 # able to find the library in its own directory.
4867 set destbase [file tail $dest]
4868 lappend link_options "additional_flags=-Wl,-soname,$destbase"
4869 }
4870 }
4871 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
4872 return -1
4873 }
4874 if { [is_remote host]
4875 && ([istarget "*-*-mingw*"]
4876 || [istarget *-*-cygwin*]
4877 || [istarget *-*-pe*]) } {
4878 set dest_tail_name [file tail ${dest}]
4879 remote_upload host $dest_tail_name.a ${dest}.a
4880 remote_file host delete $dest_tail_name.a
4881 }
4882
4883 return ""
4884 }
4885
4886 # Build a shared library from SOURCES. Ignore target boards PIE-related
4887 # multilib_flags.
4888
4889 proc gdb_compile_shlib {sources dest options} {
4890 global board
4891
4892 # Ignore PIE-related setting in multilib_flags.
4893 set board [target_info name]
4894 set multilib_flags_orig [board_info $board multilib_flags]
4895 set multilib_flags ""
4896 foreach op $multilib_flags_orig {
4897 if { $op == "-pie" || $op == "-no-pie" \
4898 || $op == "-fPIE" || $op == "-fno-PIE"} {
4899 } else {
4900 append multilib_flags " $op"
4901 }
4902 }
4903
4904 save_target_board_info { multilib_flags } {
4905 unset_board_info multilib_flags
4906 set_board_info multilib_flags "$multilib_flags"
4907 set result [gdb_compile_shlib_1 $sources $dest $options]
4908 }
4909
4910 return $result
4911 }
4912
4913 # This is just like gdb_compile_shlib, above, except that it tries compiling
4914 # against several different thread libraries, to see which one this
4915 # system has.
4916 proc gdb_compile_shlib_pthreads {sources dest options} {
4917 set built_binfile 0
4918 set why_msg "unrecognized error"
4919 foreach lib {-lpthreads -lpthread -lthread ""} {
4920 # This kind of wipes out whatever libs the caller may have
4921 # set. Or maybe theirs will override ours. How infelicitous.
4922 set options_with_lib [concat $options [list libs=$lib quiet]]
4923 set ccout [gdb_compile_shlib $sources $dest $options_with_lib]
4924 switch -regexp -- $ccout {
4925 ".*no posix threads support.*" {
4926 set why_msg "missing threads include file"
4927 break
4928 }
4929 ".*cannot open -lpthread.*" {
4930 set why_msg "missing runtime threads library"
4931 }
4932 ".*Can't find library for -lpthread.*" {
4933 set why_msg "missing runtime threads library"
4934 }
4935 {^$} {
4936 pass "successfully compiled posix threads shlib test case"
4937 set built_binfile 1
4938 break
4939 }
4940 }
4941 }
4942 if {!$built_binfile} {
4943 unsupported "couldn't compile $sources: ${why_msg}"
4944 return -1
4945 }
4946 }
4947
4948 # This is just like gdb_compile_pthreads, above, except that we always add the
4949 # objc library for compiling Objective-C programs
4950 proc gdb_compile_objc {source dest type options} {
4951 set built_binfile 0
4952 set why_msg "unrecognized error"
4953 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
4954 # This kind of wipes out whatever libs the caller may have
4955 # set. Or maybe theirs will override ours. How infelicitous.
4956 if { $lib == "solaris" } {
4957 set lib "-lpthread -lposix4"
4958 }
4959 if { $lib != "-lobjc" } {
4960 set lib "-lobjc $lib"
4961 }
4962 set options_with_lib [concat $options [list libs=$lib quiet]]
4963 set ccout [gdb_compile $source $dest $type $options_with_lib]
4964 switch -regexp -- $ccout {
4965 ".*no posix threads support.*" {
4966 set why_msg "missing threads include file"
4967 break
4968 }
4969 ".*cannot open -lpthread.*" {
4970 set why_msg "missing runtime threads library"
4971 }
4972 ".*Can't find library for -lpthread.*" {
4973 set why_msg "missing runtime threads library"
4974 }
4975 {^$} {
4976 pass "successfully compiled objc with posix threads test case"
4977 set built_binfile 1
4978 break
4979 }
4980 }
4981 }
4982 if {!$built_binfile} {
4983 unsupported "couldn't compile [file tail $source]: ${why_msg}"
4984 return -1
4985 }
4986 }
4987
4988 # Build an OpenMP program from SOURCE. See prefatory comment for
4989 # gdb_compile, above, for discussion of the parameters to this proc.
4990
4991 proc gdb_compile_openmp {source dest type options} {
4992 lappend options "additional_flags=-fopenmp"
4993 return [gdb_compile $source $dest $type $options]
4994 }
4995
4996 # Send a command to GDB.
4997 # For options for TYPE see gdb_stdin_log_write
4998
4999 proc send_gdb { string {type standard}} {
5000 gdb_stdin_log_write $string $type
5001 return [remote_send host "$string"]
5002 }
5003
5004 # Send STRING to the inferior's terminal.
5005
5006 proc send_inferior { string } {
5007 global inferior_spawn_id
5008
5009 if {[catch "send -i $inferior_spawn_id -- \$string" errorInfo]} {
5010 return "$errorInfo"
5011 } else {
5012 return ""
5013 }
5014 }
5015
5016 #
5017 #
5018
5019 proc gdb_expect { args } {
5020 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
5021 set atimeout [lindex $args 0]
5022 set expcode [list [lindex $args 1]]
5023 } else {
5024 set expcode $args
5025 }
5026
5027 # A timeout argument takes precedence, otherwise of all the timeouts
5028 # select the largest.
5029 if [info exists atimeout] {
5030 set tmt $atimeout
5031 } else {
5032 set tmt [get_largest_timeout]
5033 }
5034
5035 set code [catch \
5036 {uplevel remote_expect host $tmt $expcode} string]
5037
5038 if {$code == 1} {
5039 global errorInfo errorCode
5040
5041 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
5042 } else {
5043 return -code $code $string
5044 }
5045 }
5046
5047 # gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
5048 #
5049 # Check for long sequence of output by parts.
5050 # TEST: is the test message to be printed with the test success/fail.
5051 # SENTINEL: Is the terminal pattern indicating that output has finished.
5052 # LIST: is the sequence of outputs to match.
5053 # If the sentinel is recognized early, it is considered an error.
5054 #
5055 # Returns:
5056 # 1 if the test failed,
5057 # 0 if the test passes,
5058 # -1 if there was an internal error.
5059
5060 proc gdb_expect_list {test sentinel list} {
5061 global gdb_prompt
5062 set index 0
5063 set ok 1
5064
5065 while { ${index} < [llength ${list}] } {
5066 set pattern [lindex ${list} ${index}]
5067 set index [expr ${index} + 1]
5068 verbose -log "gdb_expect_list pattern: /$pattern/" 2
5069 if { ${index} == [llength ${list}] } {
5070 if { ${ok} } {
5071 gdb_expect {
5072 -re "${pattern}${sentinel}" {
5073 # pass "${test}, pattern ${index} + sentinel"
5074 }
5075 -re "${sentinel}" {
5076 fail "${test} (pattern ${index} + sentinel)"
5077 set ok 0
5078 }
5079 -re ".*A problem internal to GDB has been detected" {
5080 fail "${test} (GDB internal error)"
5081 set ok 0
5082 gdb_internal_error_resync
5083 }
5084 timeout {
5085 fail "${test} (pattern ${index} + sentinel) (timeout)"
5086 set ok 0
5087 }
5088 }
5089 } else {
5090 # unresolved "${test}, pattern ${index} + sentinel"
5091 }
5092 } else {
5093 if { ${ok} } {
5094 gdb_expect {
5095 -re "${pattern}" {
5096 # pass "${test}, pattern ${index}"
5097 }
5098 -re "${sentinel}" {
5099 fail "${test} (pattern ${index})"
5100 set ok 0
5101 }
5102 -re ".*A problem internal to GDB has been detected" {
5103 fail "${test} (GDB internal error)"
5104 set ok 0
5105 gdb_internal_error_resync
5106 }
5107 timeout {
5108 fail "${test} (pattern ${index}) (timeout)"
5109 set ok 0
5110 }
5111 }
5112 } else {
5113 # unresolved "${test}, pattern ${index}"
5114 }
5115 }
5116 }
5117 if { ${ok} } {
5118 pass "${test}"
5119 return 0
5120 } else {
5121 return 1
5122 }
5123 }
5124
5125 # Spawn the gdb process.
5126 #
5127 # This doesn't expect any output or do any other initialization,
5128 # leaving those to the caller.
5129 #
5130 # Overridable function -- you can override this function in your
5131 # baseboard file.
5132
5133 proc gdb_spawn { } {
5134 default_gdb_spawn
5135 }
5136
5137 # Spawn GDB with CMDLINE_FLAGS appended to the GDBFLAGS global.
5138
5139 proc gdb_spawn_with_cmdline_opts { cmdline_flags } {
5140 global GDBFLAGS
5141
5142 set saved_gdbflags $GDBFLAGS
5143
5144 if {$GDBFLAGS != ""} {
5145 append GDBFLAGS " "
5146 }
5147 append GDBFLAGS $cmdline_flags
5148
5149 set res [gdb_spawn]
5150
5151 set GDBFLAGS $saved_gdbflags
5152
5153 return $res
5154 }
5155
5156 # Start gdb running, wait for prompt, and disable the pagers.
5157
5158 # Overridable function -- you can override this function in your
5159 # baseboard file.
5160
5161 proc gdb_start { } {
5162 default_gdb_start
5163 }
5164
5165 proc gdb_exit { } {
5166 catch default_gdb_exit
5167 }
5168
5169 # Return true if we can spawn a program on the target and attach to
5170 # it.
5171
5172 proc can_spawn_for_attach { } {
5173 # We use exp_pid to get the inferior's pid, assuming that gives
5174 # back the pid of the program. On remote boards, that would give
5175 # us instead the PID of e.g., the ssh client, etc.
5176 if [is_remote target] then {
5177 verbose -log "can't spawn for attach (target is remote)"
5178 return 0
5179 }
5180
5181 # The "attach" command doesn't make sense when the target is
5182 # stub-like, where GDB finds the program already started on
5183 # initial connection.
5184 if {[target_info exists use_gdb_stub]} {
5185 verbose -log "can't spawn for attach (target is stub)"
5186 return 0
5187 }
5188
5189 # Assume yes.
5190 return 1
5191 }
5192
5193 # Centralize the failure checking of "attach" command.
5194 # Return 0 if attach failed, otherwise return 1.
5195
5196 proc gdb_attach { testpid args } {
5197 parse_args {
5198 {pattern ""}
5199 }
5200
5201 if { [llength $args] != 0 } {
5202 error "Unexpected arguments: $args"
5203 }
5204
5205 gdb_test_multiple "attach $testpid" "attach" {
5206 -re -wrap "Attaching to.*ptrace: Operation not permitted\\." {
5207 unsupported "$gdb_test_name (Operation not permitted)"
5208 return 0
5209 }
5210 -re -wrap "$pattern" {
5211 pass $gdb_test_name
5212 return 1
5213 }
5214 }
5215
5216 return 0
5217 }
5218
5219 # Start gdb with "--pid $TESTPID" on the command line and wait for the prompt.
5220 # Return 1 if GDB managed to start and attach to the process, 0 otherwise.
5221
5222 proc_with_prefix gdb_spawn_attach_cmdline { testpid } {
5223 if ![can_spawn_for_attach] {
5224 # The caller should have checked can_spawn_for_attach itself
5225 # before getting here.
5226 error "can't spawn for attach with this target/board"
5227 }
5228
5229 set test "start gdb with --pid"
5230 set res [gdb_spawn_with_cmdline_opts "-quiet --pid=$testpid"]
5231 if { $res != 0 } {
5232 fail $test
5233 return 0
5234 }
5235
5236 gdb_test_multiple "" "$test" {
5237 -re -wrap "ptrace: Operation not permitted\\." {
5238 unsupported "$gdb_test_name (operation not permitted)"
5239 return 0
5240 }
5241 -re -wrap "ptrace: No such process\\." {
5242 fail "$gdb_test_name (no such process)"
5243 return 0
5244 }
5245 -re -wrap "Attaching to process $testpid\r\n.*" {
5246 pass $gdb_test_name
5247 }
5248 }
5249
5250 # Check that we actually attached to a process, in case the
5251 # error message is not caught by the patterns above.
5252 gdb_test_multiple "info thread" "" {
5253 -re -wrap "No threads\\." {
5254 fail "$gdb_test_name (no thread)"
5255 }
5256 -re -wrap "Id.*" {
5257 pass $gdb_test_name
5258 return 1
5259 }
5260 }
5261
5262 return 0
5263 }
5264
5265 # Kill a progress previously started with spawn_wait_for_attach, and
5266 # reap its wait status. PROC_SPAWN_ID is the spawn id associated with
5267 # the process.
5268
5269 proc kill_wait_spawned_process { proc_spawn_id } {
5270 set pid [exp_pid -i $proc_spawn_id]
5271
5272 verbose -log "killing ${pid}"
5273 remote_exec build "kill -9 ${pid}"
5274
5275 verbose -log "closing ${proc_spawn_id}"
5276 catch "close -i $proc_spawn_id"
5277 verbose -log "waiting for ${proc_spawn_id}"
5278
5279 # If somehow GDB ends up still attached to the process here, a
5280 # blocking wait hangs until gdb is killed (or until gdb / the
5281 # ptracer reaps the exit status too, but that won't happen because
5282 # something went wrong.) Passing -nowait makes expect tell Tcl to
5283 # wait for the PID in the background. That's fine because we
5284 # don't care about the exit status. */
5285 wait -nowait -i $proc_spawn_id
5286 }
5287
5288 # Returns the process id corresponding to the given spawn id.
5289
5290 proc spawn_id_get_pid { spawn_id } {
5291 set testpid [exp_pid -i $spawn_id]
5292
5293 if { [istarget "*-*-cygwin*"] } {
5294 # testpid is the Cygwin PID, GDB uses the Windows PID, which
5295 # might be different due to the way fork/exec works.
5296 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
5297 }
5298
5299 return $testpid
5300 }
5301
5302 # Start a set of programs running and then wait for a bit, to be sure
5303 # that they can be attached to. Return a list of processes spawn IDs,
5304 # one element for each process spawned. It's a test error to call
5305 # this when [can_spawn_for_attach] is false.
5306
5307 proc spawn_wait_for_attach { executable_list } {
5308 set spawn_id_list {}
5309
5310 if ![can_spawn_for_attach] {
5311 # The caller should have checked can_spawn_for_attach itself
5312 # before getting here.
5313 error "can't spawn for attach with this target/board"
5314 }
5315
5316 foreach {executable} $executable_list {
5317 # Note we use Expect's spawn, not Tcl's exec, because with
5318 # spawn we control when to wait for/reap the process. That
5319 # allows killing the process by PID without being subject to
5320 # pid-reuse races.
5321 lappend spawn_id_list [remote_spawn target $executable]
5322 }
5323
5324 sleep 2
5325
5326 return $spawn_id_list
5327 }
5328
5329 #
5330 # gdb_load_cmd -- load a file into the debugger.
5331 # ARGS - additional args to load command.
5332 # return a -1 if anything goes wrong.
5333 #
5334 proc gdb_load_cmd { args } {
5335 global gdb_prompt
5336
5337 if [target_info exists gdb_load_timeout] {
5338 set loadtimeout [target_info gdb_load_timeout]
5339 } else {
5340 set loadtimeout 1600
5341 }
5342 send_gdb "load $args\n"
5343 verbose "Timeout is now $loadtimeout seconds" 2
5344 gdb_expect $loadtimeout {
5345 -re "Loading section\[^\r\]*\r\n" {
5346 exp_continue
5347 }
5348 -re "Start address\[\r\]*\r\n" {
5349 exp_continue
5350 }
5351 -re "Transfer rate\[\r\]*\r\n" {
5352 exp_continue
5353 }
5354 -re "Memory access error\[^\r\]*\r\n" {
5355 perror "Failed to load program"
5356 return -1
5357 }
5358 -re "$gdb_prompt $" {
5359 return 0
5360 }
5361 -re "(.*)\r\n$gdb_prompt " {
5362 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
5363 return -1
5364 }
5365 timeout {
5366 perror "Timed out trying to load $args."
5367 return -1
5368 }
5369 }
5370 return -1
5371 }
5372
5373 # Invoke "gcore". CORE is the name of the core file to write. TEST
5374 # is the name of the test case. This will return 1 if the core file
5375 # was created, 0 otherwise. If this fails to make a core file because
5376 # this configuration of gdb does not support making core files, it
5377 # will call "unsupported", not "fail". However, if this fails to make
5378 # a core file for some other reason, then it will call "fail".
5379
5380 proc gdb_gcore_cmd {core test} {
5381 global gdb_prompt
5382
5383 set result 0
5384 gdb_test_multiple "gcore $core" $test {
5385 -re "Saved corefile .*\[\r\n\]+$gdb_prompt $" {
5386 pass $test
5387 set result 1
5388 }
5389 -re "(?:Can't create a corefile|Target does not support core file generation\\.)\[\r\n\]+$gdb_prompt $" {
5390 unsupported $test
5391 }
5392 }
5393
5394 return $result
5395 }
5396
5397 # Load core file CORE. TEST is the name of the test case.
5398 # This will record a pass/fail for loading the core file.
5399 # Returns:
5400 # 1 - core file is successfully loaded
5401 # 0 - core file loaded but has a non fatal error
5402 # -1 - core file failed to load
5403
5404 proc gdb_core_cmd { core test } {
5405 global gdb_prompt
5406
5407 gdb_test_multiple "core $core" "$test" {
5408 -re "\\\[Thread debugging using \[^ \r\n\]* enabled\\\]\r\n" {
5409 exp_continue
5410 }
5411 -re " is not a core dump:.*\r\n$gdb_prompt $" {
5412 fail "$test (bad file format)"
5413 return -1
5414 }
5415 -re -wrap "[string_to_regexp $core]: No such file or directory.*" {
5416 fail "$test (file not found)"
5417 return -1
5418 }
5419 -re "Couldn't find .* registers in core file.*\r\n$gdb_prompt $" {
5420 fail "$test (incomplete note section)"
5421 return 0
5422 }
5423 -re "Core was generated by .*\r\n$gdb_prompt $" {
5424 pass "$test"
5425 return 1
5426 }
5427 -re ".*$gdb_prompt $" {
5428 fail "$test"
5429 return -1
5430 }
5431 timeout {
5432 fail "$test (timeout)"
5433 return -1
5434 }
5435 }
5436 fail "unsupported output from 'core' command"
5437 return -1
5438 }
5439
5440 # Return the filename to download to the target and load on the target
5441 # for this shared library. Normally just LIBNAME, unless shared libraries
5442 # for this target have separate link and load images.
5443
5444 proc shlib_target_file { libname } {
5445 return $libname
5446 }
5447
5448 # Return the filename GDB will load symbols from when debugging this
5449 # shared library. Normally just LIBNAME, unless shared libraries for
5450 # this target have separate link and load images.
5451
5452 proc shlib_symbol_file { libname } {
5453 return $libname
5454 }
5455
5456 # Return the filename to download to the target and load for this
5457 # executable. Normally just BINFILE unless it is renamed to something
5458 # else for this target.
5459
5460 proc exec_target_file { binfile } {
5461 return $binfile
5462 }
5463
5464 # Return the filename GDB will load symbols from when debugging this
5465 # executable. Normally just BINFILE unless executables for this target
5466 # have separate files for symbols.
5467
5468 proc exec_symbol_file { binfile } {
5469 return $binfile
5470 }
5471
5472 # Rename the executable file. Normally this is just BINFILE1 being renamed
5473 # to BINFILE2, but some targets require multiple binary files.
5474 proc gdb_rename_execfile { binfile1 binfile2 } {
5475 file rename -force [exec_target_file ${binfile1}] \
5476 [exec_target_file ${binfile2}]
5477 if { [exec_target_file ${binfile1}] != [exec_symbol_file ${binfile1}] } {
5478 file rename -force [exec_symbol_file ${binfile1}] \
5479 [exec_symbol_file ${binfile2}]
5480 }
5481 }
5482
5483 # "Touch" the executable file to update the date. Normally this is just
5484 # BINFILE, but some targets require multiple files.
5485 proc gdb_touch_execfile { binfile } {
5486 set time [clock seconds]
5487 file mtime [exec_target_file ${binfile}] $time
5488 if { [exec_target_file ${binfile}] != [exec_symbol_file ${binfile}] } {
5489 file mtime [exec_symbol_file ${binfile}] $time
5490 }
5491 }
5492
5493 # Like remote_download but provides a gdb-specific behavior.
5494 #
5495 # If the destination board is remote, the local file FROMFILE is transferred as
5496 # usual with remote_download to TOFILE on the remote board. The destination
5497 # filename is added to the CLEANFILES global, so it can be cleaned up at the
5498 # end of the test.
5499 #
5500 # If the destination board is local, the destination path TOFILE is passed
5501 # through standard_output_file, and FROMFILE is copied there.
5502 #
5503 # In both cases, if TOFILE is omitted, it defaults to the [file tail] of
5504 # FROMFILE.
5505
5506 proc gdb_remote_download {dest fromfile {tofile {}}} {
5507 # If TOFILE is not given, default to the same filename as FROMFILE.
5508 if {[string length $tofile] == 0} {
5509 set tofile [file tail $fromfile]
5510 }
5511
5512 if {[is_remote $dest]} {
5513 # When the DEST is remote, we simply send the file to DEST.
5514 global cleanfiles
5515
5516 set destname [remote_download $dest $fromfile $tofile]
5517 lappend cleanfiles $destname
5518
5519 return $destname
5520 } else {
5521 # When the DEST is local, we copy the file to the test directory (where
5522 # the executable is).
5523 #
5524 # Note that we pass TOFILE through standard_output_file, regardless of
5525 # whether it is absolute or relative, because we don't want the tests
5526 # to be able to write outside their standard output directory.
5527
5528 set tofile [standard_output_file $tofile]
5529
5530 file copy -force $fromfile $tofile
5531
5532 return $tofile
5533 }
5534 }
5535
5536 # gdb_load_shlib LIB...
5537 #
5538 # Copy the listed library to the target.
5539
5540 proc gdb_load_shlib { file } {
5541 global gdb_spawn_id
5542
5543 if ![info exists gdb_spawn_id] {
5544 perror "gdb_load_shlib: GDB is not running"
5545 }
5546
5547 set dest [gdb_remote_download target [shlib_target_file $file]]
5548
5549 if {[is_remote target]} {
5550 # If the target is remote, we need to tell gdb where to find the
5551 # libraries.
5552 #
5553 # We could set this even when not testing remotely, but a user
5554 # generally won't set it unless necessary. In order to make the tests
5555 # more like the real-life scenarios, we don't set it for local testing.
5556 gdb_test "set solib-search-path [file dirname $file]" "" ""
5557 }
5558
5559 return $dest
5560 }
5561
5562 #
5563 # gdb_load -- load a file into the debugger. Specifying no file
5564 # defaults to the executable currently being debugged.
5565 # The return value is 0 for success, -1 for failure.
5566 # Many files in config/*.exp override this procedure.
5567 #
5568 proc gdb_load { arg } {
5569 if { $arg != "" } {
5570 return [gdb_file_cmd $arg]
5571 }
5572 return 0
5573 }
5574
5575 #
5576 # with_complaints -- Execute BODY and set complaints temporary to N for the
5577 # duration.
5578 #
5579 proc with_complaints { n body } {
5580 global decimal
5581
5582 # Save current setting of complaints.
5583 set save ""
5584 set show_complaints_re \
5585 "Max number of complaints about incorrect symbols is ($decimal)\\."
5586 gdb_test_multiple "show complaints" "" {
5587 -re -wrap $show_complaints_re {
5588 set save $expect_out(1,string)
5589 }
5590 }
5591
5592 if { $save == "" } {
5593 perror "Did not manage to set complaints"
5594 } else {
5595 # Set complaints.
5596 gdb_test_no_output "set complaints $n" ""
5597 }
5598
5599 set code [catch {uplevel 1 $body} result]
5600
5601 # Restore saved setting of complaints.
5602 if { $save != "" } {
5603 gdb_test_no_output "set complaints $save" ""
5604 }
5605
5606 if {$code == 1} {
5607 global errorInfo errorCode
5608 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
5609 } else {
5610 return -code $code $result
5611 }
5612 }
5613
5614 #
5615 # gdb_load_no_complaints -- As gdb_load, but in addition verifies that
5616 # loading caused no symbol reading complaints.
5617 #
5618 proc gdb_load_no_complaints { arg } {
5619 global gdb_prompt gdb_file_cmd_msg decimal
5620
5621 # Temporarily set complaint to a small non-zero number.
5622 with_complaints 5 {
5623 gdb_load $arg
5624 }
5625
5626 # Verify that there were no complaints.
5627 set re \
5628 [multi_line \
5629 "^(Reading symbols from \[^\r\n\]*" \
5630 ")+(Expanding full symbols from \[^\r\n\]*" \
5631 ")?$gdb_prompt $"]
5632 gdb_assert {[regexp $re $gdb_file_cmd_msg]} "No complaints"
5633 }
5634
5635 # gdb_reload -- load a file into the target. Called before "running",
5636 # either the first time or after already starting the program once,
5637 # for remote targets. Most files that override gdb_load should now
5638 # override this instead.
5639 #
5640 # INFERIOR_ARGS contains the arguments to pass to the inferiors, as a
5641 # single string to get interpreted by a shell. If the target board
5642 # overriding gdb_reload is a "stub", then it should arrange things such
5643 # these arguments make their way to the inferior process.
5644
5645 proc gdb_reload { {inferior_args {}} } {
5646 # For the benefit of existing configurations, default to gdb_load.
5647 # Specifying no file defaults to the executable currently being
5648 # debugged.
5649 return [gdb_load ""]
5650 }
5651
5652 proc gdb_continue { function } {
5653 global decimal
5654
5655 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"]
5656 }
5657
5658 # Default implementation of gdb_init.
5659 proc default_gdb_init { test_file_name } {
5660 global gdb_wrapper_initialized
5661 global gdb_wrapper_target
5662 global gdb_test_file_name
5663 global cleanfiles
5664 global pf_prefix
5665
5666 # Reset the timeout value to the default. This way, any testcase
5667 # that changes the timeout value without resetting it cannot affect
5668 # the timeout used in subsequent testcases.
5669 global gdb_test_timeout
5670 global timeout
5671 set timeout $gdb_test_timeout
5672
5673 if { [regexp ".*gdb\.reverse\/.*" $test_file_name]
5674 && [target_info exists gdb_reverse_timeout] } {
5675 set timeout [target_info gdb_reverse_timeout]
5676 }
5677
5678 # If GDB_INOTIFY is given, check for writes to '.'. This is a
5679 # debugging tool to help confirm that the test suite is
5680 # parallel-safe. You need "inotifywait" from the
5681 # inotify-tools package to use this.
5682 global GDB_INOTIFY inotify_pid
5683 if {[info exists GDB_INOTIFY] && ![info exists inotify_pid]} {
5684 global outdir tool inotify_log_file
5685
5686 set exclusions {outputs temp gdb[.](log|sum) cache}
5687 set exclusion_re ([join $exclusions |])
5688
5689 set inotify_log_file [standard_temp_file inotify.out]
5690 set inotify_pid [exec inotifywait -r -m -e move,create,delete . \
5691 --exclude $exclusion_re \
5692 |& tee -a $outdir/$tool.log $inotify_log_file &]
5693
5694 # Wait for the watches; hopefully this is long enough.
5695 sleep 2
5696
5697 # Clear the log so that we don't emit a warning the first time
5698 # we check it.
5699 set fd [open $inotify_log_file w]
5700 close $fd
5701 }
5702
5703 # Block writes to all banned variables, and invocation of all
5704 # banned procedures...
5705 global banned_variables
5706 global banned_procedures
5707 global banned_traced
5708 if (!$banned_traced) {
5709 foreach banned_var $banned_variables {
5710 global "$banned_var"
5711 trace add variable "$banned_var" write error
5712 }
5713 foreach banned_proc $banned_procedures {
5714 global "$banned_proc"
5715 trace add execution "$banned_proc" enter error
5716 }
5717 set banned_traced 1
5718 }
5719
5720 # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same
5721 # messages as expected.
5722 setenv LC_ALL C
5723 setenv LC_CTYPE C
5724 setenv LANG C
5725
5726 # Don't let a .inputrc file or an existing setting of INPUTRC mess
5727 # up the test results. Certain tests (style tests and TUI tests)
5728 # want to set the terminal to a non-"dumb" value, and for those we
5729 # want to disable bracketed paste mode. Versions of Readline
5730 # before 8.0 will not understand this and will issue a warning.
5731 # We tried using a $if to guard it, but Readline 8.1 had a bug in
5732 # its version-comparison code that prevented this for working.
5733 setenv INPUTRC [cached_file inputrc "set enable-bracketed-paste off"]
5734
5735 # This disables style output, which would interfere with many
5736 # tests.
5737 setenv TERM "dumb"
5738
5739 # If DEBUGINFOD_URLS is set, gdb will try to download sources and
5740 # debug info for f.i. system libraries. Prevent this.
5741 unset -nocomplain ::env(DEBUGINFOD_URLS)
5742
5743 # Ensure that GDBHISTFILE and GDBHISTSIZE are removed from the
5744 # environment, we don't want these modifications to the history
5745 # settings.
5746 unset -nocomplain ::env(GDBHISTFILE)
5747 unset -nocomplain ::env(GDBHISTSIZE)
5748
5749 # Ensure that XDG_CONFIG_HOME is not set. Some tests setup a fake
5750 # home directory in order to test loading settings from gdbinit.
5751 # If XDG_CONFIG_HOME is set then GDB will load a gdbinit from
5752 # there (if one is present) rather than the home directory setup
5753 # in the test.
5754 unset -nocomplain ::env(XDG_CONFIG_HOME)
5755
5756 # Initialize GDB's pty with a fixed size, to make sure we avoid pagination
5757 # during startup. See "man expect" for details about stty_init.
5758 global stty_init
5759 set stty_init "rows 25 cols 80"
5760
5761 # Some tests (for example gdb.base/maint.exp) shell out from gdb to use
5762 # grep. Clear GREP_OPTIONS to make the behavior predictable,
5763 # especially having color output turned on can cause tests to fail.
5764 setenv GREP_OPTIONS ""
5765
5766 # Clear $gdbserver_reconnect_p.
5767 global gdbserver_reconnect_p
5768 set gdbserver_reconnect_p 1
5769 unset gdbserver_reconnect_p
5770
5771 # Clear $last_loaded_file
5772 global last_loaded_file
5773 unset -nocomplain last_loaded_file
5774
5775 # Reset GDB number of instances
5776 global gdb_instances
5777 set gdb_instances 0
5778
5779 set cleanfiles {}
5780
5781 set gdb_test_file_name [file rootname [file tail $test_file_name]]
5782
5783 # Make sure that the wrapper is rebuilt
5784 # with the appropriate multilib option.
5785 if { $gdb_wrapper_target != [current_target_name] } {
5786 set gdb_wrapper_initialized 0
5787 }
5788
5789 # Unlike most tests, we have a small number of tests that generate
5790 # a very large amount of output. We therefore increase the expect
5791 # buffer size to be able to contain the entire test output. This
5792 # is especially needed by gdb.base/info-macros.exp.
5793 match_max -d 65536
5794 # Also set this value for the currently running GDB.
5795 match_max [match_max -d]
5796
5797 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
5798 set pf_prefix "[file tail [file dirname $test_file_name]]/[file tail $test_file_name]:"
5799
5800 global gdb_prompt
5801 if [target_info exists gdb_prompt] {
5802 set gdb_prompt [target_info gdb_prompt]
5803 } else {
5804 set gdb_prompt "\\(gdb\\)"
5805 }
5806 global use_gdb_stub
5807 if [info exists use_gdb_stub] {
5808 unset use_gdb_stub
5809 }
5810
5811 gdb_setup_known_globals
5812
5813 if { [info procs ::gdb_tcl_unknown] != "" } {
5814 # Dejagnu overrides proc unknown. The dejagnu version may trigger in a
5815 # test-case but abort the entire test run. To fix this, we install a
5816 # local version here, which reverts dejagnu's override, and restore
5817 # dejagnu's version in gdb_finish.
5818 rename ::unknown ::dejagnu_unknown
5819 proc unknown { args } {
5820 # Use tcl's unknown.
5821 set cmd [lindex $args 0]
5822 unresolved "testcase aborted due to invalid command name: $cmd"
5823 return [uplevel 1 ::gdb_tcl_unknown $args]
5824 }
5825 }
5826 }
5827
5828 # Return a path using GDB_PARALLEL.
5829 # ARGS is a list of path elements to append to "$objdir/$GDB_PARALLEL".
5830 # GDB_PARALLEL must be defined, the caller must check.
5831 #
5832 # The default value for GDB_PARALLEL is, canonically, ".".
5833 # The catch is that tests don't expect an additional "./" in file paths so
5834 # omit any directory for the default case.
5835 # GDB_PARALLEL is written as "yes" for the default case in Makefile.in to mark
5836 # its special handling.
5837
5838 proc make_gdb_parallel_path { args } {
5839 global GDB_PARALLEL objdir
5840 set joiner [list "file" "join" $objdir]
5841 if { [info exists GDB_PARALLEL] && $GDB_PARALLEL != "yes" } {
5842 lappend joiner $GDB_PARALLEL
5843 }
5844 set joiner [concat $joiner $args]
5845 return [eval $joiner]
5846 }
5847
5848 # Turn BASENAME into a full file name in the standard output
5849 # directory. It is ok if BASENAME is the empty string; in this case
5850 # the directory is returned.
5851
5852 proc standard_output_file {basename} {
5853 global objdir subdir gdb_test_file_name
5854
5855 set dir [make_gdb_parallel_path outputs $subdir $gdb_test_file_name]
5856 file mkdir $dir
5857 # If running on MinGW, replace /c/foo with c:/foo
5858 if { [ishost *-*-mingw*] } {
5859 set dir [exec sh -c "cd ${dir} && pwd -W"]
5860 }
5861 return [file join $dir $basename]
5862 }
5863
5864 # Turn BASENAME into a full file name in the standard output directory. If
5865 # GDB has been launched more than once then append the count, starting with
5866 # a ".1" postfix.
5867
5868 proc standard_output_file_with_gdb_instance {basename} {
5869 global gdb_instances
5870 set count $gdb_instances
5871
5872 if {$count == 0} {
5873 return [standard_output_file $basename]
5874 }
5875 return [standard_output_file ${basename}.${count}]
5876 }
5877
5878 # Return the name of a file in our standard temporary directory.
5879
5880 proc standard_temp_file {basename} {
5881 # Since a particular runtest invocation is only executing a single test
5882 # file at any given time, we can use the runtest pid to build the
5883 # path of the temp directory.
5884 set dir [make_gdb_parallel_path temp [pid]]
5885 file mkdir $dir
5886 return [file join $dir $basename]
5887 }
5888
5889 # Rename file A to file B, if B does not already exists. Otherwise, leave B
5890 # as is and delete A. Return 1 if rename happened.
5891
5892 proc tentative_rename { a b } {
5893 global errorInfo errorCode
5894 set code [catch {file rename -- $a $b} result]
5895 if { $code == 1 && [lindex $errorCode 0] == "POSIX" \
5896 && [lindex $errorCode 1] == "EEXIST" } {
5897 file delete $a
5898 return 0
5899 }
5900 if {$code == 1} {
5901 return -code error -errorinfo $errorInfo -errorcode $errorCode $result
5902 } elseif {$code > 1} {
5903 return -code $code $result
5904 }
5905 return 1
5906 }
5907
5908 # Create a file with name FILENAME and contents TXT in the cache directory.
5909 # If EXECUTABLE, mark the new file for execution.
5910
5911 proc cached_file { filename txt {executable 0}} {
5912 set filename [make_gdb_parallel_path cache $filename]
5913
5914 if { [file exists $filename] } {
5915 return $filename
5916 }
5917
5918 set dir [file dirname $filename]
5919 file mkdir $dir
5920
5921 set tmp_filename $filename.[pid]
5922 set fd [open $tmp_filename w]
5923 puts $fd $txt
5924 close $fd
5925
5926 if { $executable } {
5927 exec chmod +x $tmp_filename
5928 }
5929 tentative_rename $tmp_filename $filename
5930
5931 return $filename
5932 }
5933
5934 # Set 'testfile', 'srcfile', and 'binfile'.
5935 #
5936 # ARGS is a list of source file specifications.
5937 # Without any arguments, the .exp file's base name is used to
5938 # compute the source file name. The ".c" extension is added in this case.
5939 # If ARGS is not empty, each entry is a source file specification.
5940 # If the specification starts with a "." or "-", it is treated as a suffix
5941 # to append to the .exp file's base name.
5942 # If the specification is the empty string, it is treated as if it
5943 # were ".c".
5944 # Otherwise it is a file name.
5945 # The first file in the list is used to set the 'srcfile' global.
5946 # Each subsequent name is used to set 'srcfile2', 'srcfile3', etc.
5947 #
5948 # Most tests should call this without arguments.
5949 #
5950 # If a completely different binary file name is needed, then it
5951 # should be handled in the .exp file with a suitable comment.
5952
5953 proc standard_testfile {args} {
5954 global gdb_test_file_name
5955 global subdir
5956 global gdb_test_file_last_vars
5957
5958 # Outputs.
5959 global testfile binfile
5960
5961 set testfile $gdb_test_file_name
5962 set binfile [standard_output_file ${testfile}]
5963
5964 if {[llength $args] == 0} {
5965 set args .c
5966 }
5967
5968 # Unset our previous output variables.
5969 # This can help catch hidden bugs.
5970 if {[info exists gdb_test_file_last_vars]} {
5971 foreach varname $gdb_test_file_last_vars {
5972 global $varname
5973 catch {unset $varname}
5974 }
5975 }
5976 # 'executable' is often set by tests.
5977 set gdb_test_file_last_vars {executable}
5978
5979 set suffix ""
5980 foreach arg $args {
5981 set varname srcfile$suffix
5982 global $varname
5983
5984 # Handle an extension.
5985 if {$arg == ""} {
5986 set arg $testfile.c
5987 } else {
5988 set first [string range $arg 0 0]
5989 if { $first == "." || $first == "-" } {
5990 set arg $testfile$arg
5991 }
5992 }
5993
5994 set $varname $arg
5995 lappend gdb_test_file_last_vars $varname
5996
5997 if {$suffix == ""} {
5998 set suffix 2
5999 } else {
6000 incr suffix
6001 }
6002 }
6003 }
6004
6005 # The default timeout used when testing GDB commands. We want to use
6006 # the same timeout as the default dejagnu timeout, unless the user has
6007 # already provided a specific value (probably through a site.exp file).
6008 global gdb_test_timeout
6009 if ![info exists gdb_test_timeout] {
6010 set gdb_test_timeout $timeout
6011 }
6012
6013 # A list of global variables that GDB testcases should not use.
6014 # We try to prevent their use by monitoring write accesses and raising
6015 # an error when that happens.
6016 set banned_variables { bug_id prms_id }
6017
6018 # A list of procedures that GDB testcases should not use.
6019 # We try to prevent their use by monitoring invocations and raising
6020 # an error when that happens.
6021 set banned_procedures { strace }
6022
6023 # gdb_init is called by runtest at start, but also by several
6024 # tests directly; gdb_finish is only called from within runtest after
6025 # each test source execution.
6026 # Placing several traces by repetitive calls to gdb_init leads
6027 # to problems, as only one trace is removed in gdb_finish.
6028 # To overcome this possible problem, we add a variable that records
6029 # if the banned variables and procedures are already traced.
6030 set banned_traced 0
6031
6032 # Global array that holds the name of all global variables at the time
6033 # a test script is started. After the test script has completed any
6034 # global not in this list is deleted.
6035 array set gdb_known_globals {}
6036
6037 # Setup the GDB_KNOWN_GLOBALS array with the names of all current
6038 # global variables.
6039 proc gdb_setup_known_globals {} {
6040 global gdb_known_globals
6041
6042 array set gdb_known_globals {}
6043 foreach varname [info globals] {
6044 set gdb_known_globals($varname) 1
6045 }
6046 }
6047
6048 # Cleanup the global namespace. Any global not in the
6049 # GDB_KNOWN_GLOBALS array is unset, this ensures we don't "leak"
6050 # globals from one test script to another.
6051 proc gdb_cleanup_globals {} {
6052 global gdb_known_globals gdb_persistent_globals
6053
6054 foreach varname [info globals] {
6055 if {![info exists gdb_known_globals($varname)]} {
6056 if { [info exists gdb_persistent_globals($varname)] } {
6057 continue
6058 }
6059 uplevel #0 unset $varname
6060 }
6061 }
6062 }
6063
6064 # Create gdb_tcl_unknown, a copy tcl's ::unknown, provided it's present as a
6065 # proc.
6066 set temp [interp create]
6067 if { [interp eval $temp "info procs ::unknown"] != "" } {
6068 set old_args [interp eval $temp "info args ::unknown"]
6069 set old_body [interp eval $temp "info body ::unknown"]
6070 eval proc gdb_tcl_unknown {$old_args} {$old_body}
6071 }
6072 interp delete $temp
6073 unset temp
6074
6075 # GDB implementation of ${tool}_init. Called right before executing the
6076 # test-case.
6077 # Overridable function -- you can override this function in your
6078 # baseboard file.
6079 proc gdb_init { args } {
6080 # A baseboard file overriding this proc and calling the default version
6081 # should behave the same as this proc. So, don't add code here, but to
6082 # the default version instead.
6083 return [default_gdb_init {*}$args]
6084 }
6085
6086 # GDB implementation of ${tool}_finish. Called right after executing the
6087 # test-case.
6088 proc gdb_finish { } {
6089 global gdbserver_reconnect_p
6090 global gdb_prompt
6091 global cleanfiles
6092 global known_globals
6093
6094 if { [info procs ::gdb_tcl_unknown] != "" } {
6095 # Restore dejagnu's version of proc unknown.
6096 rename ::unknown ""
6097 rename ::dejagnu_unknown ::unknown
6098 }
6099
6100 # Exit first, so that the files are no longer in use.
6101 gdb_exit
6102
6103 if { [llength $cleanfiles] > 0 } {
6104 eval remote_file target delete $cleanfiles
6105 set cleanfiles {}
6106 }
6107
6108 # Unblock write access to the banned variables. Dejagnu typically
6109 # resets some of them between testcases.
6110 global banned_variables
6111 global banned_procedures
6112 global banned_traced
6113 if ($banned_traced) {
6114 foreach banned_var $banned_variables {
6115 global "$banned_var"
6116 trace remove variable "$banned_var" write error
6117 }
6118 foreach banned_proc $banned_procedures {
6119 global "$banned_proc"
6120 trace remove execution "$banned_proc" enter error
6121 }
6122 set banned_traced 0
6123 }
6124
6125 global gdb_finish_hooks
6126 foreach gdb_finish_hook $gdb_finish_hooks {
6127 $gdb_finish_hook
6128 }
6129 set gdb_finish_hooks [list]
6130
6131 gdb_cleanup_globals
6132 }
6133
6134 global debug_format
6135 set debug_format "unknown"
6136
6137 # Run the gdb command "info source" and extract the debugging format
6138 # information from the output and save it in debug_format.
6139
6140 proc get_debug_format { } {
6141 global gdb_prompt
6142 global expect_out
6143 global debug_format
6144
6145 set debug_format "unknown"
6146 send_gdb "info source\n"
6147 gdb_expect 10 {
6148 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
6149 set debug_format $expect_out(1,string)
6150 verbose "debug format is $debug_format"
6151 return 1
6152 }
6153 -re "No current source file.\r\n$gdb_prompt $" {
6154 perror "get_debug_format used when no current source file"
6155 return 0
6156 }
6157 -re "$gdb_prompt $" {
6158 warning "couldn't check debug format (no valid response)."
6159 return 1
6160 }
6161 timeout {
6162 warning "couldn't check debug format (timeout)."
6163 return 1
6164 }
6165 }
6166 }
6167
6168 # Return true if FORMAT matches the debug format the current test was
6169 # compiled with. FORMAT is a shell-style globbing pattern; it can use
6170 # `*', `[...]', and so on.
6171 #
6172 # This function depends on variables set by `get_debug_format', above.
6173
6174 proc test_debug_format {format} {
6175 global debug_format
6176
6177 return [expr [string match $format $debug_format] != 0]
6178 }
6179
6180 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
6181 # COFF, stabs, etc). If that format matches the format that the
6182 # current test was compiled with, then the next test is expected to
6183 # fail for any target. Returns 1 if the next test or set of tests is
6184 # expected to fail, 0 otherwise (or if it is unknown). Must have
6185 # previously called get_debug_format.
6186 proc setup_xfail_format { format } {
6187 set ret [test_debug_format $format]
6188
6189 if {$ret} then {
6190 setup_xfail "*-*-*"
6191 }
6192 return $ret
6193 }
6194
6195 # gdb_get_line_number TEXT [FILE]
6196 #
6197 # Search the source file FILE, and return the line number of the
6198 # first line containing TEXT. If no match is found, an error is thrown.
6199 #
6200 # TEXT is a string literal, not a regular expression.
6201 #
6202 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
6203 # specified, and does not start with "/", then it is assumed to be in
6204 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
6205 # by changing the callers and the interface at the same time.
6206 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
6207 # gdb.base/ena-dis-br.exp.
6208 #
6209 # Use this function to keep your test scripts independent of the
6210 # exact line numbering of the source file. Don't write:
6211 #
6212 # send_gdb "break 20"
6213 #
6214 # This means that if anyone ever edits your test's source file,
6215 # your test could break. Instead, put a comment like this on the
6216 # source file line you want to break at:
6217 #
6218 # /* breakpoint spot: frotz.exp: test name */
6219 #
6220 # and then write, in your test script (which we assume is named
6221 # frotz.exp):
6222 #
6223 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
6224 #
6225 # (Yes, Tcl knows how to handle the nested quotes and brackets.
6226 # Try this:
6227 # $ tclsh
6228 # % puts "foo [lindex "bar baz" 1]"
6229 # foo baz
6230 # %
6231 # Tcl is quite clever, for a little stringy language.)
6232 #
6233 # ===
6234 #
6235 # The previous implementation of this procedure used the gdb search command.
6236 # This version is different:
6237 #
6238 # . It works with MI, and it also works when gdb is not running.
6239 #
6240 # . It operates on the build machine, not the host machine.
6241 #
6242 # . For now, this implementation fakes a current directory of
6243 # $srcdir/$subdir to be compatible with the old implementation.
6244 # This will go away eventually and some callers will need to
6245 # be changed.
6246 #
6247 # . The TEXT argument is literal text and matches literally,
6248 # not a regular expression as it was before.
6249 #
6250 # . State changes in gdb, such as changing the current file
6251 # and setting $_, no longer happen.
6252 #
6253 # After a bit of time we can forget about the differences from the
6254 # old implementation.
6255 #
6256 # --chastain 2004-08-05
6257
6258 proc gdb_get_line_number { text { file "" } } {
6259 global srcdir
6260 global subdir
6261 global srcfile
6262
6263 if { "$file" == "" } then {
6264 set file "$srcfile"
6265 }
6266 if { ! [regexp "^/" "$file"] } then {
6267 set file "$srcdir/$subdir/$file"
6268 }
6269
6270 if { [ catch { set fd [open "$file"] } message ] } then {
6271 error "$message"
6272 }
6273
6274 set found -1
6275 for { set line 1 } { 1 } { incr line } {
6276 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
6277 error "$message"
6278 }
6279 if { $nchar < 0 } then {
6280 break
6281 }
6282 if { [string first "$text" "$body"] >= 0 } then {
6283 set found $line
6284 break
6285 }
6286 }
6287
6288 if { [ catch { close "$fd" } message ] } then {
6289 error "$message"
6290 }
6291
6292 if {$found == -1} {
6293 error "undefined tag \"$text\""
6294 }
6295
6296 return $found
6297 }
6298
6299 # Continue the program until it ends.
6300 #
6301 # MSSG is the error message that gets printed. If not given, a
6302 # default is used.
6303 # COMMAND is the command to invoke. If not given, "continue" is
6304 # used.
6305 # ALLOW_EXTRA is a flag indicating whether the test should expect
6306 # extra output between the "Continuing." line and the program
6307 # exiting. By default it is zero; if nonzero, any extra output
6308 # is accepted.
6309
6310 proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} {
6311 global inferior_exited_re use_gdb_stub
6312
6313 if {$mssg == ""} {
6314 set text "continue until exit"
6315 } else {
6316 set text "continue until exit at $mssg"
6317 }
6318 if {$allow_extra} {
6319 set extra ".*"
6320 } else {
6321 set extra ""
6322 }
6323
6324 # By default, we don't rely on exit() behavior of remote stubs --
6325 # it's common for exit() to be implemented as a simple infinite
6326 # loop, or a forced crash/reset. For native targets, by default, we
6327 # assume process exit is reported as such. If a non-reliable target
6328 # is used, we set a breakpoint at exit, and continue to that.
6329 if { [target_info exists exit_is_reliable] } {
6330 set exit_is_reliable [target_info exit_is_reliable]
6331 } else {
6332 set exit_is_reliable [expr ! $use_gdb_stub]
6333 }
6334
6335 if { ! $exit_is_reliable } {
6336 if {![gdb_breakpoint "exit"]} {
6337 return 0
6338 }
6339 gdb_test $command "Continuing..*Breakpoint .*exit.*" \
6340 $text
6341 } else {
6342 # Continue until we exit. Should not stop again.
6343 # Don't bother to check the output of the program, that may be
6344 # extremely tough for some remote systems.
6345 gdb_test $command \
6346 "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\
6347 $text
6348 }
6349 }
6350
6351 proc rerun_to_main {} {
6352 global gdb_prompt use_gdb_stub
6353
6354 if $use_gdb_stub {
6355 gdb_run_cmd
6356 gdb_expect {
6357 -re ".*Breakpoint .*main .*$gdb_prompt $"\
6358 {pass "rerun to main" ; return 0}
6359 -re "$gdb_prompt $"\
6360 {fail "rerun to main" ; return 0}
6361 timeout {fail "(timeout) rerun to main" ; return 0}
6362 }
6363 } else {
6364 send_gdb "run\n"
6365 gdb_expect {
6366 -re "The program .* has been started already.*y or n. $" {
6367 send_gdb "y\n" answer
6368 exp_continue
6369 }
6370 -re "Starting program.*$gdb_prompt $"\
6371 {pass "rerun to main" ; return 0}
6372 -re "$gdb_prompt $"\
6373 {fail "rerun to main" ; return 0}
6374 timeout {fail "(timeout) rerun to main" ; return 0}
6375 }
6376 }
6377 }
6378
6379 # Return true if EXECUTABLE contains a .gdb_index or .debug_names index section.
6380
6381 proc exec_has_index_section { executable } {
6382 set readelf_program [gdb_find_readelf]
6383 set res [catch {exec $readelf_program -S $executable \
6384 | grep -E "\.gdb_index|\.debug_names" }]
6385 if { $res == 0 } {
6386 return 1
6387 }
6388 return 0
6389 }
6390
6391 # Return list with major and minor version of readelf, or an empty list.
6392 gdb_caching_proc readelf_version {
6393 set readelf_program [gdb_find_readelf]
6394 set res [catch {exec $readelf_program --version} output]
6395 if { $res != 0 } {
6396 return [list]
6397 }
6398 set lines [split $output \n]
6399 set line [lindex $lines 0]
6400 set res [regexp {[ \t]+([0-9]+)[.]([0-9]+)[^ \t]*$} \
6401 $line dummy major minor]
6402 if { $res != 1 } {
6403 return [list]
6404 }
6405 return [list $major $minor]
6406 }
6407
6408 # Return 1 if readelf prints the PIE flag, 0 if is doesn't, and -1 if unknown.
6409 proc readelf_prints_pie { } {
6410 set version [readelf_version]
6411 if { [llength $version] == 0 } {
6412 return -1
6413 }
6414 set major [lindex $version 0]
6415 set minor [lindex $version 1]
6416 # It would be better to construct a PIE executable and test if the PIE
6417 # flag is printed by readelf, but we cannot reliably construct a PIE
6418 # executable if the multilib_flags dictate otherwise
6419 # (--target_board=unix/-no-pie/-fno-PIE).
6420 return [version_at_least $major $minor 2 26]
6421 }
6422
6423 # Return 1 if EXECUTABLE is a Position Independent Executable, 0 if it is not,
6424 # and -1 if unknown.
6425
6426 proc exec_is_pie { executable } {
6427 set res [readelf_prints_pie]
6428 if { $res != 1 } {
6429 return -1
6430 }
6431 set readelf_program [gdb_find_readelf]
6432 # We're not testing readelf -d | grep "FLAGS_1.*Flags:.*PIE"
6433 # because the PIE flag is not set by all versions of gold, see PR
6434 # binutils/26039.
6435 set res [catch {exec $readelf_program -h $executable} output]
6436 if { $res != 0 } {
6437 return -1
6438 }
6439 set res [regexp -line {^[ \t]*Type:[ \t]*DYN \((Position-Independent Executable|Shared object) file\)$} \
6440 $output]
6441 if { $res == 1 } {
6442 return 1
6443 }
6444 return 0
6445 }
6446
6447 # Return true if a test should be skipped due to lack of floating
6448 # point support or GDB can't fetch the contents from floating point
6449 # registers.
6450
6451 gdb_caching_proc gdb_skip_float_test {
6452 if [target_info exists gdb,skip_float_tests] {
6453 return 1
6454 }
6455
6456 # There is an ARM kernel ptrace bug that hardware VFP registers
6457 # are not updated after GDB ptrace set VFP registers. The bug
6458 # was introduced by kernel commit 8130b9d7b9d858aa04ce67805e8951e3cb6e9b2f
6459 # in 2012 and is fixed in e2dfb4b880146bfd4b6aa8e138c0205407cebbaf
6460 # in May 2016. In other words, kernels older than 4.6.3, 4.4.14,
6461 # 4.1.27, 3.18.36, and 3.14.73 have this bug.
6462 # This kernel bug is detected by check how does GDB change the
6463 # program result by changing one VFP register.
6464 if { [istarget "arm*-*-linux*"] } {
6465
6466 set compile_flags {debug nowarnings }
6467
6468 # Set up, compile, and execute a test program having VFP
6469 # operations.
6470 set src [standard_temp_file arm_vfp[pid].c]
6471 set exe [standard_temp_file arm_vfp[pid].x]
6472
6473 gdb_produce_source $src {
6474 int main() {
6475 double d = 4.0;
6476 int ret;
6477
6478 asm ("vldr d0, [%0]" : : "r" (&d));
6479 asm ("vldr d1, [%0]" : : "r" (&d));
6480 asm (".global break_here\n"
6481 "break_here:");
6482 asm ("vcmp.f64 d0, d1\n"
6483 "vmrs APSR_nzcv, fpscr\n"
6484 "bne L_value_different\n"
6485 "movs %0, #0\n"
6486 "b L_end\n"
6487 "L_value_different:\n"
6488 "movs %0, #1\n"
6489 "L_end:\n" : "=r" (ret) :);
6490
6491 /* Return $d0 != $d1. */
6492 return ret;
6493 }
6494 }
6495
6496 verbose "compiling testfile $src" 2
6497 set lines [gdb_compile $src $exe executable $compile_flags]
6498 file delete $src
6499
6500 if ![string match "" $lines] then {
6501 verbose "testfile compilation failed, returning 1" 2
6502 return 0
6503 }
6504
6505 # No error message, compilation succeeded so now run it via gdb.
6506 # Run the test up to 5 times to detect whether ptrace can
6507 # correctly update VFP registers or not.
6508 set skip_vfp_test 0
6509 for {set i 0} {$i < 5} {incr i} {
6510 global gdb_prompt srcdir subdir
6511
6512 gdb_exit
6513 gdb_start
6514 gdb_reinitialize_dir $srcdir/$subdir
6515 gdb_load "$exe"
6516
6517 runto_main
6518 gdb_test "break *break_here"
6519 gdb_continue_to_breakpoint "break_here"
6520
6521 # Modify $d0 to a different value, so the exit code should
6522 # be 1.
6523 gdb_test "set \$d0 = 5.0"
6524
6525 set test "continue to exit"
6526 gdb_test_multiple "continue" "$test" {
6527 -re "exited with code 01.*$gdb_prompt $" {
6528 }
6529 -re "exited normally.*$gdb_prompt $" {
6530 # However, the exit code is 0. That means something
6531 # wrong in setting VFP registers.
6532 set skip_vfp_test 1
6533 break
6534 }
6535 }
6536 }
6537
6538 gdb_exit
6539 remote_file build delete $exe
6540
6541 return $skip_vfp_test
6542 }
6543 return 0
6544 }
6545
6546 # Print a message and return true if a test should be skipped
6547 # due to lack of stdio support.
6548
6549 proc gdb_skip_stdio_test { msg } {
6550 if [target_info exists gdb,noinferiorio] {
6551 verbose "Skipping test '$msg': no inferior i/o."
6552 return 1
6553 }
6554 return 0
6555 }
6556
6557 proc gdb_skip_bogus_test { msg } {
6558 return 0
6559 }
6560
6561 # Return true if a test should be skipped due to lack of XML support
6562 # in the host GDB.
6563 # NOTE: This must be called while gdb is *not* running.
6564
6565 gdb_caching_proc gdb_skip_xml_test {
6566 global gdb_spawn_id
6567 global gdb_prompt
6568 global srcdir
6569
6570 if { [info exists gdb_spawn_id] } {
6571 error "GDB must not be running in gdb_skip_xml_tests."
6572 }
6573
6574 set xml_file [gdb_remote_download host "${srcdir}/gdb.xml/trivial.xml"]
6575
6576 gdb_start
6577 set xml_missing 0
6578 gdb_test_multiple "set tdesc filename $xml_file" "" {
6579 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
6580 set xml_missing 1
6581 }
6582 -re ".*$gdb_prompt $" { }
6583 }
6584 gdb_exit
6585 return $xml_missing
6586 }
6587
6588 # Return true if argv[0] is available.
6589
6590 gdb_caching_proc gdb_has_argv0 {
6591 set result 0
6592
6593 # Compile and execute a test program to check whether argv[0] is available.
6594 gdb_simple_compile has_argv0 {
6595 int main (int argc, char **argv) {
6596 return 0;
6597 }
6598 } executable
6599
6600
6601 # Helper proc.
6602 proc gdb_has_argv0_1 { exe } {
6603 global srcdir subdir
6604 global gdb_prompt hex
6605
6606 gdb_exit
6607 gdb_start
6608 gdb_reinitialize_dir $srcdir/$subdir
6609 gdb_load "$exe"
6610
6611 # Set breakpoint on main.
6612 gdb_test_multiple "break -q main" "break -q main" {
6613 -re "Breakpoint.*${gdb_prompt} $" {
6614 }
6615 -re "${gdb_prompt} $" {
6616 return 0
6617 }
6618 }
6619
6620 # Run to main.
6621 gdb_run_cmd
6622 gdb_test_multiple "" "run to main" {
6623 -re "Breakpoint.*${gdb_prompt} $" {
6624 }
6625 -re "${gdb_prompt} $" {
6626 return 0
6627 }
6628 }
6629
6630 set old_elements "200"
6631 set test "show print elements"
6632 gdb_test_multiple $test $test {
6633 -re "Limit on string chars or array elements to print is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
6634 set old_elements $expect_out(1,string)
6635 }
6636 }
6637 set old_repeats "200"
6638 set test "show print repeats"
6639 gdb_test_multiple $test $test {
6640 -re "Threshold for repeated print elements is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
6641 set old_repeats $expect_out(1,string)
6642 }
6643 }
6644 gdb_test_no_output "set print elements unlimited" ""
6645 gdb_test_no_output "set print repeats unlimited" ""
6646
6647 set retval 0
6648 # Check whether argc is 1.
6649 gdb_test_multiple "p argc" "p argc" {
6650 -re " = 1\r\n${gdb_prompt} $" {
6651
6652 gdb_test_multiple "p argv\[0\]" "p argv\[0\]" {
6653 -re " = $hex \".*[file tail $exe]\"\r\n${gdb_prompt} $" {
6654 set retval 1
6655 }
6656 -re "${gdb_prompt} $" {
6657 }
6658 }
6659 }
6660 -re "${gdb_prompt} $" {
6661 }
6662 }
6663
6664 gdb_test_no_output "set print elements $old_elements" ""
6665 gdb_test_no_output "set print repeats $old_repeats" ""
6666
6667 return $retval
6668 }
6669
6670 set result [gdb_has_argv0_1 $obj]
6671
6672 gdb_exit
6673 file delete $obj
6674
6675 if { !$result
6676 && ([istarget *-*-linux*]
6677 || [istarget *-*-freebsd*] || [istarget *-*-kfreebsd*]
6678 || [istarget *-*-netbsd*] || [istarget *-*-knetbsd*]
6679 || [istarget *-*-openbsd*]
6680 || [istarget *-*-darwin*]
6681 || [istarget *-*-solaris*]
6682 || [istarget *-*-aix*]
6683 || [istarget *-*-gnu*]
6684 || [istarget *-*-cygwin*] || [istarget *-*-mingw32*]
6685 || [istarget *-*-*djgpp*] || [istarget *-*-go32*]
6686 || [istarget *-wince-pe] || [istarget *-*-mingw32ce*]
6687 || [istarget *-*-osf*]
6688 || [istarget *-*-dicos*]
6689 || [istarget *-*-nto*]
6690 || [istarget *-*-*vms*]
6691 || [istarget *-*-lynx*178]) } {
6692 fail "argv\[0\] should be available on this target"
6693 }
6694
6695 return $result
6696 }
6697
6698 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
6699 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
6700 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
6701 # the name of a debuginfo only file. This file will be stored in the same
6702 # subdirectory.
6703
6704 # Functions for separate debug info testing
6705
6706 # starting with an executable:
6707 # foo --> original executable
6708
6709 # at the end of the process we have:
6710 # foo.stripped --> foo w/o debug info
6711 # foo.debug --> foo's debug info
6712 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
6713
6714 # Fetch the build id from the file.
6715 # Returns "" if there is none.
6716
6717 proc get_build_id { filename } {
6718 if { ([istarget "*-*-mingw*"]
6719 || [istarget *-*-cygwin*]) } {
6720 set objdump_program [gdb_find_objdump]
6721 set result [catch {set data [exec $objdump_program -p $filename | grep signature | cut "-d " -f4]} output]
6722 verbose "result is $result"
6723 verbose "output is $output"
6724 if {$result == 1} {
6725 return ""
6726 }
6727 return $data
6728 } else {
6729 set tmp [standard_output_file "${filename}-tmp"]
6730 set objcopy_program [gdb_find_objcopy]
6731 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $filename $tmp" output]
6732 verbose "result is $result"
6733 verbose "output is $output"
6734 if {$result == 1} {
6735 return ""
6736 }
6737 set fi [open $tmp]
6738 fconfigure $fi -translation binary
6739 # Skip the NOTE header.
6740 read $fi 16
6741 set data [read $fi]
6742 close $fi
6743 file delete $tmp
6744 if ![string compare $data ""] then {
6745 return ""
6746 }
6747 # Convert it to hex.
6748 binary scan $data H* data
6749 return $data
6750 }
6751 }
6752
6753 # Return the build-id hex string (usually 160 bits as 40 hex characters)
6754 # converted to the form: .build-id/ab/cdef1234...89.debug
6755 # Return "" if no build-id found.
6756 proc build_id_debug_filename_get { filename } {
6757 set data [get_build_id $filename]
6758 if { $data == "" } {
6759 return ""
6760 }
6761 regsub {^..} $data {\0/} data
6762 return ".build-id/${data}.debug"
6763 }
6764
6765 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
6766 # list of optional flags. The only currently supported flag is no-main,
6767 # which removes the symbol entry for main from the separate debug file.
6768 #
6769 # Function returns zero on success. Function will return non-zero failure code
6770 # on some targets not supporting separate debug info (such as i386-msdos).
6771
6772 proc gdb_gnu_strip_debug { dest args } {
6773
6774 # Use the first separate debug info file location searched by GDB so the
6775 # run cannot be broken by some stale file searched with higher precedence.
6776 set debug_file "${dest}.debug"
6777
6778 set strip_to_file_program [transform strip]
6779 set objcopy_program [gdb_find_objcopy]
6780
6781 set debug_link [file tail $debug_file]
6782 set stripped_file "${dest}.stripped"
6783
6784 # Get rid of the debug info, and store result in stripped_file
6785 # something like gdb/testsuite/gdb.base/blah.stripped.
6786 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
6787 verbose "result is $result"
6788 verbose "output is $output"
6789 if {$result == 1} {
6790 return 1
6791 }
6792
6793 # Workaround PR binutils/10802:
6794 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
6795 set perm [file attributes ${dest} -permissions]
6796 file attributes ${stripped_file} -permissions $perm
6797
6798 # Get rid of everything but the debug info, and store result in debug_file
6799 # This will be in the .debug subdirectory, see above.
6800 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
6801 verbose "result is $result"
6802 verbose "output is $output"
6803 if {$result == 1} {
6804 return 1
6805 }
6806
6807 # If no-main is passed, strip the symbol for main from the separate
6808 # file. This is to simulate the behavior of elfutils's eu-strip, which
6809 # leaves the symtab in the original file only. There's no way to get
6810 # objcopy or strip to remove the symbol table without also removing the
6811 # debugging sections, so this is as close as we can get.
6812 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
6813 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
6814 verbose "result is $result"
6815 verbose "output is $output"
6816 if {$result == 1} {
6817 return 1
6818 }
6819 file delete "${debug_file}"
6820 file rename "${debug_file}-tmp" "${debug_file}"
6821 }
6822
6823 # Link the two previous output files together, adding the .gnu_debuglink
6824 # section to the stripped_file, containing a pointer to the debug_file,
6825 # save the new file in dest.
6826 # This will be the regular executable filename, in the usual location.
6827 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
6828 verbose "result is $result"
6829 verbose "output is $output"
6830 if {$result == 1} {
6831 return 1
6832 }
6833
6834 # Workaround PR binutils/10802:
6835 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
6836 set perm [file attributes ${stripped_file} -permissions]
6837 file attributes ${dest} -permissions $perm
6838
6839 return 0
6840 }
6841
6842 # Test the output of GDB_COMMAND matches the pattern obtained
6843 # by concatenating all elements of EXPECTED_LINES. This makes
6844 # it possible to split otherwise very long string into pieces.
6845 # If third argument TESTNAME is not empty, it's used as the name of the
6846 # test to be printed on pass/fail.
6847 proc help_test_raw { gdb_command expected_lines {testname {}} } {
6848 set expected_output [join $expected_lines ""]
6849 if {$testname != {}} {
6850 gdb_test "${gdb_command}" "${expected_output}" $testname
6851 return
6852 }
6853
6854 gdb_test "${gdb_command}" "${expected_output}"
6855 }
6856
6857 # A regexp that matches the end of help CLASS|PREFIX_COMMAND
6858 set help_list_trailer {
6859 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n]+"
6860 "Type \"apropos -v word\" for full documentation of commands related to \"word\"\.[\r\n]+"
6861 "Command name abbreviations are allowed if unambiguous\."
6862 }
6863
6864 # Test the output of "help COMMAND_CLASS". EXPECTED_INITIAL_LINES
6865 # are regular expressions that should match the beginning of output,
6866 # before the list of commands in that class.
6867 # LIST_OF_COMMANDS are regular expressions that should match the
6868 # list of commands in that class. If empty, the command list will be
6869 # matched automatically. The presence of standard epilogue will be tested
6870 # automatically.
6871 # If last argument TESTNAME is not empty, it's used as the name of the
6872 # test to be printed on pass/fail.
6873 # Notice that the '[' and ']' characters don't need to be escaped for strings
6874 # wrapped in {} braces.
6875 proc test_class_help { command_class expected_initial_lines {list_of_commands {}} {testname {}} } {
6876 global help_list_trailer
6877 if {[llength $list_of_commands]>0} {
6878 set l_list_of_commands {"List of commands:[\r\n]+[\r\n]+"}
6879 set l_list_of_commands [concat $l_list_of_commands $list_of_commands]
6880 set l_list_of_commands [concat $l_list_of_commands {"[\r\n]+[\r\n]+"}]
6881 } else {
6882 set l_list_of_commands {"List of commands\:.*[\r\n]+"}
6883 }
6884 set l_stock_body {
6885 "Type \"help\" followed by command name for full documentation\.[\r\n]+"
6886 }
6887 set l_entire_body [concat $expected_initial_lines $l_list_of_commands \
6888 $l_stock_body $help_list_trailer]
6889
6890 help_test_raw "help ${command_class}" $l_entire_body $testname
6891 }
6892
6893 # Like test_class_help but specialised to test "help user-defined".
6894 proc test_user_defined_class_help { {list_of_commands {}} {testname {}} } {
6895 test_class_help "user-defined" {
6896 "User-defined commands\.[\r\n]+"
6897 "The commands in this class are those defined by the user\.[\r\n]+"
6898 "Use the \"define\" command to define a command\.[\r\n]+"
6899 } $list_of_commands $testname
6900 }
6901
6902
6903 # COMMAND_LIST should have either one element -- command to test, or
6904 # two elements -- abbreviated command to test, and full command the first
6905 # element is abbreviation of.
6906 # The command must be a prefix command. EXPECTED_INITIAL_LINES
6907 # are regular expressions that should match the beginning of output,
6908 # before the list of subcommands. The presence of
6909 # subcommand list and standard epilogue will be tested automatically.
6910 proc test_prefix_command_help { command_list expected_initial_lines args } {
6911 global help_list_trailer
6912 set command [lindex $command_list 0]
6913 if {[llength $command_list]>1} {
6914 set full_command [lindex $command_list 1]
6915 } else {
6916 set full_command $command
6917 }
6918 # Use 'list' and not just {} because we want variables to
6919 # be expanded in this list.
6920 set l_stock_body [list\
6921 "List of $full_command subcommands\:.*\[\r\n\]+"\
6922 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"]
6923 set l_entire_body [concat $expected_initial_lines $l_stock_body $help_list_trailer]
6924 if {[llength $args]>0} {
6925 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
6926 } else {
6927 help_test_raw "help ${command}" $l_entire_body
6928 }
6929 }
6930
6931 # Build executable named EXECUTABLE from specifications that allow
6932 # different options to be passed to different sub-compilations.
6933 # TESTNAME is the name of the test; this is passed to 'untested' if
6934 # something fails.
6935 # OPTIONS is passed to the final link, using gdb_compile. If OPTIONS
6936 # contains the option "pthreads", then gdb_compile_pthreads is used.
6937 # ARGS is a flat list of source specifications, of the form:
6938 # { SOURCE1 OPTIONS1 [ SOURCE2 OPTIONS2 ]... }
6939 # Each SOURCE is compiled to an object file using its OPTIONS,
6940 # using gdb_compile.
6941 # Returns 0 on success, -1 on failure.
6942 proc build_executable_from_specs {testname executable options args} {
6943 global subdir
6944 global srcdir
6945
6946 set binfile [standard_output_file $executable]
6947
6948 set info_options ""
6949 if { [lsearch -exact $options "c++"] >= 0 } {
6950 set info_options "c++"
6951 }
6952 if [get_compiler_info ${info_options}] {
6953 return -1
6954 }
6955
6956 set func gdb_compile
6957 set func_index [lsearch -regexp $options {^(pthreads|shlib|shlib_pthreads|openmp)$}]
6958 if {$func_index != -1} {
6959 set func "${func}_[lindex $options $func_index]"
6960 }
6961
6962 # gdb_compile_shlib and gdb_compile_shlib_pthreads do not use the 3rd
6963 # parameter. They also requires $sources while gdb_compile and
6964 # gdb_compile_pthreads require $objects. Moreover they ignore any options.
6965 if [string match gdb_compile_shlib* $func] {
6966 set sources_path {}
6967 foreach {s local_options} $args {
6968 if { [regexp "^/" "$s"] } then {
6969 lappend sources_path "$s"
6970 } else {
6971 lappend sources_path "$srcdir/$subdir/$s"
6972 }
6973 }
6974 set ret [$func $sources_path "${binfile}" $options]
6975 } elseif {[lsearch -exact $options rust] != -1} {
6976 set sources_path {}
6977 foreach {s local_options} $args {
6978 if { [regexp "^/" "$s"] } then {
6979 lappend sources_path "$s"
6980 } else {
6981 lappend sources_path "$srcdir/$subdir/$s"
6982 }
6983 }
6984 set ret [gdb_compile_rust $sources_path "${binfile}" $options]
6985 } else {
6986 set objects {}
6987 set i 0
6988 foreach {s local_options} $args {
6989 if { ! [regexp "^/" "$s"] } then {
6990 set s "$srcdir/$subdir/$s"
6991 }
6992 if { [$func "${s}" "${binfile}${i}.o" object $local_options] != "" } {
6993 untested $testname
6994 return -1
6995 }
6996 lappend objects "${binfile}${i}.o"
6997 incr i
6998 }
6999 set ret [$func $objects "${binfile}" executable $options]
7000 }
7001 if { $ret != "" } {
7002 untested $testname
7003 return -1
7004 }
7005
7006 return 0
7007 }
7008
7009 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
7010 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
7011 # to pass to untested, if something is wrong. OPTIONS are passed
7012 # to gdb_compile directly.
7013 proc build_executable { testname executable {sources ""} {options {debug}} } {
7014 if {[llength $sources]==0} {
7015 set sources ${executable}.c
7016 }
7017
7018 set arglist [list $testname $executable $options]
7019 foreach source $sources {
7020 lappend arglist $source $options
7021 }
7022
7023 return [eval build_executable_from_specs $arglist]
7024 }
7025
7026 # Starts fresh GDB binary and loads an optional executable into GDB.
7027 # Usage: clean_restart [executable]
7028 # EXECUTABLE is the basename of the binary.
7029 # Return -1 if starting gdb or loading the executable failed.
7030
7031 proc clean_restart { args } {
7032 global srcdir
7033 global subdir
7034 global errcnt
7035 global warncnt
7036
7037 if { [llength $args] > 1 } {
7038 error "bad number of args: [llength $args]"
7039 }
7040
7041 gdb_exit
7042
7043 # This is a clean restart, so reset error and warning count.
7044 set errcnt 0
7045 set warncnt 0
7046
7047 # We'd like to do:
7048 # if { [gdb_start] == -1 } {
7049 # return -1
7050 # }
7051 # but gdb_start is a ${tool}_start proc, which doesn't have a defined
7052 # return value. So instead, we test for errcnt.
7053 gdb_start
7054 if { $errcnt > 0 } {
7055 return -1
7056 }
7057
7058 gdb_reinitialize_dir $srcdir/$subdir
7059
7060 if { [llength $args] >= 1 } {
7061 set executable [lindex $args 0]
7062 set binfile [standard_output_file ${executable}]
7063 return [gdb_load ${binfile}]
7064 }
7065
7066 return 0
7067 }
7068
7069 # Prepares for testing by calling build_executable_full, then
7070 # clean_restart.
7071 # TESTNAME is the name of the test.
7072 # Each element in ARGS is a list of the form
7073 # { EXECUTABLE OPTIONS SOURCE_SPEC... }
7074 # These are passed to build_executable_from_specs, which see.
7075 # The last EXECUTABLE is passed to clean_restart.
7076 # Returns 0 on success, non-zero on failure.
7077 proc prepare_for_testing_full {testname args} {
7078 foreach spec $args {
7079 if {[eval build_executable_from_specs [list $testname] $spec] == -1} {
7080 return -1
7081 }
7082 set executable [lindex $spec 0]
7083 }
7084 clean_restart $executable
7085 return 0
7086 }
7087
7088 # Prepares for testing, by calling build_executable, and then clean_restart.
7089 # Please refer to build_executable for parameter description.
7090 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
7091
7092 if {[build_executable $testname $executable $sources $options] == -1} {
7093 return -1
7094 }
7095 clean_restart $executable
7096
7097 return 0
7098 }
7099
7100 # Retrieve the value of EXP in the inferior, represented in format
7101 # specified in FMT (using "printFMT"). DEFAULT is used as fallback if
7102 # print fails. TEST is the test message to use. It can be omitted,
7103 # in which case a test message is built from EXP.
7104
7105 proc get_valueof { fmt exp default {test ""} } {
7106 global gdb_prompt
7107
7108 if {$test == "" } {
7109 set test "get valueof \"${exp}\""
7110 }
7111
7112 set val ${default}
7113 gdb_test_multiple "print${fmt} ${exp}" "$test" {
7114 -re "\\$\[0-9\]* = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
7115 set val $expect_out(1,string)
7116 pass "$test"
7117 }
7118 timeout {
7119 fail "$test (timeout)"
7120 }
7121 }
7122 return ${val}
7123 }
7124
7125 # Retrieve the value of local var EXP in the inferior. DEFAULT is used as
7126 # fallback if print fails. TEST is the test message to use. It can be
7127 # omitted, in which case a test message is built from EXP.
7128
7129 proc get_local_valueof { exp default {test ""} } {
7130 global gdb_prompt
7131
7132 if {$test == "" } {
7133 set test "get local valueof \"${exp}\""
7134 }
7135
7136 set val ${default}
7137 gdb_test_multiple "info locals ${exp}" "$test" {
7138 -re "$exp = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
7139 set val $expect_out(1,string)
7140 pass "$test"
7141 }
7142 timeout {
7143 fail "$test (timeout)"
7144 }
7145 }
7146 return ${val}
7147 }
7148
7149 # Retrieve the value of EXP in the inferior, as a signed decimal value
7150 # (using "print /d"). DEFAULT is used as fallback if print fails.
7151 # TEST is the test message to use. It can be omitted, in which case
7152 # a test message is built from EXP.
7153
7154 proc get_integer_valueof { exp default {test ""} } {
7155 global gdb_prompt
7156
7157 if {$test == ""} {
7158 set test "get integer valueof \"${exp}\""
7159 }
7160
7161 set val ${default}
7162 gdb_test_multiple "print /d ${exp}" "$test" {
7163 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
7164 set val $expect_out(1,string)
7165 pass "$test"
7166 }
7167 timeout {
7168 fail "$test (timeout)"
7169 }
7170 }
7171 return ${val}
7172 }
7173
7174 # Retrieve the value of EXP in the inferior, as an hexadecimal value
7175 # (using "print /x"). DEFAULT is used as fallback if print fails.
7176 # TEST is the test message to use. It can be omitted, in which case
7177 # a test message is built from EXP.
7178
7179 proc get_hexadecimal_valueof { exp default {test ""} } {
7180 global gdb_prompt
7181
7182 if {$test == ""} {
7183 set test "get hexadecimal valueof \"${exp}\""
7184 }
7185
7186 set val ${default}
7187 gdb_test_multiple "print /x ${exp}" $test {
7188 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
7189 set val $expect_out(1,string)
7190 pass "$test"
7191 }
7192 }
7193 return ${val}
7194 }
7195
7196 # Retrieve the size of TYPE in the inferior, as a decimal value. DEFAULT
7197 # is used as fallback if print fails. TEST is the test message to use.
7198 # It can be omitted, in which case a test message is 'sizeof (TYPE)'.
7199
7200 proc get_sizeof { type default {test ""} } {
7201 return [get_integer_valueof "sizeof (${type})" $default $test]
7202 }
7203
7204 proc get_target_charset { } {
7205 global gdb_prompt
7206
7207 gdb_test_multiple "show target-charset" "" {
7208 -re "The target character set is \"auto; currently (\[^\"\]*)\".*$gdb_prompt $" {
7209 return $expect_out(1,string)
7210 }
7211 -re "The target character set is \"(\[^\"\]*)\".*$gdb_prompt $" {
7212 return $expect_out(1,string)
7213 }
7214 }
7215
7216 # Pick a reasonable default.
7217 warning "Unable to read target-charset."
7218 return "UTF-8"
7219 }
7220
7221 # Get the address of VAR.
7222
7223 proc get_var_address { var } {
7224 global gdb_prompt hex
7225
7226 # Match output like:
7227 # $1 = (int *) 0x0
7228 # $5 = (int (*)()) 0
7229 # $6 = (int (*)()) 0x24 <function_bar>
7230
7231 gdb_test_multiple "print &${var}" "get address of ${var}" {
7232 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $"
7233 {
7234 pass "get address of ${var}"
7235 if { $expect_out(1,string) == "0" } {
7236 return "0x0"
7237 } else {
7238 return $expect_out(1,string)
7239 }
7240 }
7241 }
7242 return ""
7243 }
7244
7245 # Return the frame number for the currently selected frame
7246 proc get_current_frame_number {{test_name ""}} {
7247 global gdb_prompt
7248
7249 if { $test_name == "" } {
7250 set test_name "get current frame number"
7251 }
7252 set frame_num -1
7253 gdb_test_multiple "frame" $test_name {
7254 -re "#(\[0-9\]+) .*$gdb_prompt $" {
7255 set frame_num $expect_out(1,string)
7256 }
7257 }
7258 return $frame_num
7259 }
7260
7261 # Get the current value for remotetimeout and return it.
7262 proc get_remotetimeout { } {
7263 global gdb_prompt
7264 global decimal
7265
7266 gdb_test_multiple "show remotetimeout" "" {
7267 -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" {
7268 return $expect_out(1,string)
7269 }
7270 }
7271
7272 # Pick the default that gdb uses
7273 warning "Unable to read remotetimeout"
7274 return 300
7275 }
7276
7277 # Set the remotetimeout to the specified timeout. Nothing is returned.
7278 proc set_remotetimeout { timeout } {
7279 global gdb_prompt
7280
7281 gdb_test_multiple "set remotetimeout $timeout" "" {
7282 -re "$gdb_prompt $" {
7283 verbose "Set remotetimeout to $timeout\n"
7284 }
7285 }
7286 }
7287
7288 # Get the target's current endianness and return it.
7289 proc get_endianness { } {
7290 global gdb_prompt
7291
7292 gdb_test_multiple "show endian" "determine endianness" {
7293 -re ".* (little|big) endian.*\r\n$gdb_prompt $" {
7294 # Pass silently.
7295 return $expect_out(1,string)
7296 }
7297 }
7298 return "little"
7299 }
7300
7301 # Get the target's default endianness and return it.
7302 gdb_caching_proc target_endianness {
7303 global gdb_prompt
7304
7305 set me "target_endianness"
7306
7307 set src { int main() { return 0; } }
7308 if {![gdb_simple_compile $me $src executable]} {
7309 return 0
7310 }
7311
7312 clean_restart $obj
7313 if ![runto_main] {
7314 return 0
7315 }
7316 set res [get_endianness]
7317
7318 gdb_exit
7319 remote_file build delete $obj
7320
7321 return $res
7322 }
7323
7324 # ROOT and FULL are file names. Returns the relative path from ROOT
7325 # to FULL. Note that FULL must be in a subdirectory of ROOT.
7326 # For example, given ROOT = /usr/bin and FULL = /usr/bin/ls, this
7327 # will return "ls".
7328
7329 proc relative_filename {root full} {
7330 set root_split [file split $root]
7331 set full_split [file split $full]
7332
7333 set len [llength $root_split]
7334
7335 if {[eval file join $root_split]
7336 != [eval file join [lrange $full_split 0 [expr {$len - 1}]]]} {
7337 error "$full not a subdir of $root"
7338 }
7339
7340 return [eval file join [lrange $full_split $len end]]
7341 }
7342
7343 # If GDB_PARALLEL exists, then set up the parallel-mode directories.
7344 if {[info exists GDB_PARALLEL]} {
7345 if {[is_remote host]} {
7346 unset GDB_PARALLEL
7347 } else {
7348 file mkdir \
7349 [make_gdb_parallel_path outputs] \
7350 [make_gdb_parallel_path temp] \
7351 [make_gdb_parallel_path cache]
7352 }
7353 }
7354
7355 proc core_find {binfile {deletefiles {}} {arg ""}} {
7356 global objdir subdir
7357
7358 set destcore "$binfile.core"
7359 file delete $destcore
7360
7361 # Create a core file named "$destcore" rather than just "core", to
7362 # avoid problems with sys admin types that like to regularly prune all
7363 # files named "core" from the system.
7364 #
7365 # Arbitrarily try setting the core size limit to "unlimited" since
7366 # this does not hurt on systems where the command does not work and
7367 # allows us to generate a core on systems where it does.
7368 #
7369 # Some systems append "core" to the name of the program; others append
7370 # the name of the program to "core"; still others (like Linux, as of
7371 # May 2003) create cores named "core.PID". In the latter case, we
7372 # could have many core files lying around, and it may be difficult to
7373 # tell which one is ours, so let's run the program in a subdirectory.
7374 set found 0
7375 set coredir [standard_output_file coredir.[getpid]]
7376 file mkdir $coredir
7377 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
7378 # remote_exec host "${binfile}"
7379 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
7380 if [remote_file build exists $i] {
7381 remote_exec build "mv $i $destcore"
7382 set found 1
7383 }
7384 }
7385 # Check for "core.PID".
7386 if { $found == 0 } {
7387 set names [glob -nocomplain -directory $coredir core.*]
7388 if {[llength $names] == 1} {
7389 set corefile [file join $coredir [lindex $names 0]]
7390 remote_exec build "mv $corefile $destcore"
7391 set found 1
7392 }
7393 }
7394 if { $found == 0 } {
7395 # The braindamaged HPUX shell quits after the ulimit -c above
7396 # without executing ${binfile}. So we try again without the
7397 # ulimit here if we didn't find a core file above.
7398 # Oh, I should mention that any "braindamaged" non-Unix system has
7399 # the same problem. I like the cd bit too, it's really neat'n stuff.
7400 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
7401 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
7402 if [remote_file build exists $i] {
7403 remote_exec build "mv $i $destcore"
7404 set found 1
7405 }
7406 }
7407 }
7408
7409 # Try to clean up after ourselves.
7410 foreach deletefile $deletefiles {
7411 remote_file build delete [file join $coredir $deletefile]
7412 }
7413 remote_exec build "rmdir $coredir"
7414
7415 if { $found == 0 } {
7416 warning "can't generate a core file - core tests suppressed - check ulimit -c"
7417 return ""
7418 }
7419 return $destcore
7420 }
7421
7422 # gdb_target_symbol_prefix compiles a test program and then examines
7423 # the output from objdump to determine the prefix (such as underscore)
7424 # for linker symbol prefixes.
7425
7426 gdb_caching_proc gdb_target_symbol_prefix {
7427 # Compile a simple test program...
7428 set src { int main() { return 0; } }
7429 if {![gdb_simple_compile target_symbol_prefix $src executable]} {
7430 return 0
7431 }
7432
7433 set prefix ""
7434
7435 set objdump_program [gdb_find_objdump]
7436 set result [catch "exec $objdump_program --syms $obj" output]
7437
7438 if { $result == 0 \
7439 && ![regexp -lineanchor \
7440 { ([^ a-zA-Z0-9]*)main$} $output dummy prefix] } {
7441 verbose "gdb_target_symbol_prefix: Could not find main in objdump output; returning null prefix" 2
7442 }
7443
7444 file delete $obj
7445
7446 return $prefix
7447 }
7448
7449 # Return 1 if target supports scheduler locking, otherwise return 0.
7450
7451 gdb_caching_proc target_supports_scheduler_locking {
7452 global gdb_prompt
7453
7454 set me "gdb_target_supports_scheduler_locking"
7455
7456 set src { int main() { return 0; } }
7457 if {![gdb_simple_compile $me $src executable]} {
7458 return 0
7459 }
7460
7461 clean_restart $obj
7462 if ![runto_main] {
7463 return 0
7464 }
7465
7466 set supports_schedule_locking -1
7467 set current_schedule_locking_mode ""
7468
7469 set test "reading current scheduler-locking mode"
7470 gdb_test_multiple "show scheduler-locking" $test {
7471 -re "Mode for locking scheduler during execution is \"(\[\^\"\]*)\".*$gdb_prompt" {
7472 set current_schedule_locking_mode $expect_out(1,string)
7473 }
7474 -re "$gdb_prompt $" {
7475 set supports_schedule_locking 0
7476 }
7477 timeout {
7478 set supports_schedule_locking 0
7479 }
7480 }
7481
7482 if { $supports_schedule_locking == -1 } {
7483 set test "checking for scheduler-locking support"
7484 gdb_test_multiple "set scheduler-locking $current_schedule_locking_mode" $test {
7485 -re "Target '\[^'\]+' cannot support this command\..*$gdb_prompt $" {
7486 set supports_schedule_locking 0
7487 }
7488 -re "$gdb_prompt $" {
7489 set supports_schedule_locking 1
7490 }
7491 timeout {
7492 set supports_schedule_locking 0
7493 }
7494 }
7495 }
7496
7497 if { $supports_schedule_locking == -1 } {
7498 set supports_schedule_locking 0
7499 }
7500
7501 gdb_exit
7502 remote_file build delete $obj
7503 verbose "$me: returning $supports_schedule_locking" 2
7504 return $supports_schedule_locking
7505 }
7506
7507 # Return 1 if compiler supports use of nested functions. Otherwise,
7508 # return 0.
7509
7510 gdb_caching_proc support_nested_function_tests {
7511 # Compile a test program containing a nested function
7512 return [gdb_can_simple_compile nested_func {
7513 int main () {
7514 int foo () {
7515 return 0;
7516 }
7517 return foo ();
7518 }
7519 } executable]
7520 }
7521
7522 # gdb_target_symbol returns the provided symbol with the correct prefix
7523 # prepended. (See gdb_target_symbol_prefix, above.)
7524
7525 proc gdb_target_symbol { symbol } {
7526 set prefix [gdb_target_symbol_prefix]
7527 return "${prefix}${symbol}"
7528 }
7529
7530 # gdb_target_symbol_prefix_flags_asm returns a string that can be
7531 # added to gdb_compile options to define the C-preprocessor macro
7532 # SYMBOL_PREFIX with a value that can be prepended to symbols
7533 # for targets which require a prefix, such as underscore.
7534 #
7535 # This version (_asm) defines the prefix without double quotes
7536 # surrounding the prefix. It is used to define the macro
7537 # SYMBOL_PREFIX for assembly language files. Another version, below,
7538 # is used for symbols in inline assembler in C/C++ files.
7539 #
7540 # The lack of quotes in this version (_asm) makes it possible to
7541 # define supporting macros in the .S file. (The version which
7542 # uses quotes for the prefix won't work for such files since it's
7543 # impossible to define a quote-stripping macro in C.)
7544 #
7545 # It's possible to use this version (_asm) for C/C++ source files too,
7546 # but a string is usually required in such files; providing a version
7547 # (no _asm) which encloses the prefix with double quotes makes it
7548 # somewhat easier to define the supporting macros in the test case.
7549
7550 proc gdb_target_symbol_prefix_flags_asm {} {
7551 set prefix [gdb_target_symbol_prefix]
7552 if {$prefix ne ""} {
7553 return "additional_flags=-DSYMBOL_PREFIX=$prefix"
7554 } else {
7555 return "";
7556 }
7557 }
7558
7559 # gdb_target_symbol_prefix_flags returns the same string as
7560 # gdb_target_symbol_prefix_flags_asm, above, but with the prefix
7561 # enclosed in double quotes if there is a prefix.
7562 #
7563 # See the comment for gdb_target_symbol_prefix_flags_asm for an
7564 # extended discussion.
7565
7566 proc gdb_target_symbol_prefix_flags {} {
7567 set prefix [gdb_target_symbol_prefix]
7568 if {$prefix ne ""} {
7569 return "additional_flags=-DSYMBOL_PREFIX=\"$prefix\""
7570 } else {
7571 return "";
7572 }
7573 }
7574
7575 # A wrapper for 'remote_exec host' that passes or fails a test.
7576 # Returns 0 if all went well, nonzero on failure.
7577 # TEST is the name of the test, other arguments are as for remote_exec.
7578
7579 proc run_on_host { test program args } {
7580 verbose -log "run_on_host: $program $args"
7581 # remote_exec doesn't work properly if the output is set but the
7582 # input is the empty string -- so replace an empty input with
7583 # /dev/null.
7584 if {[llength $args] > 1 && [lindex $args 1] == ""} {
7585 set args [lreplace $args 1 1 "/dev/null"]
7586 }
7587 set result [eval remote_exec host [list $program] $args]
7588 verbose "result is $result"
7589 set status [lindex $result 0]
7590 set output [lindex $result 1]
7591 if {$status == 0} {
7592 pass $test
7593 return 0
7594 } else {
7595 verbose -log "run_on_host failed: $output"
7596 if { $output == "spawn failed" } {
7597 unsupported $test
7598 } else {
7599 fail $test
7600 }
7601 return -1
7602 }
7603 }
7604
7605 # Return non-zero if "board_info debug_flags" mentions Fission.
7606 # http://gcc.gnu.org/wiki/DebugFission
7607 # Fission doesn't support everything yet.
7608 # This supports working around bug 15954.
7609
7610 proc using_fission { } {
7611 set debug_flags [board_info [target_info name] debug_flags]
7612 return [regexp -- "-gsplit-dwarf" $debug_flags]
7613 }
7614
7615 # Search LISTNAME in uplevel LEVEL caller and set variables according to the
7616 # list of valid options with prefix PREFIX described by ARGSET.
7617 #
7618 # The first member of each one- or two-element list in ARGSET defines the
7619 # name of a variable that will be added to the caller's scope.
7620 #
7621 # If only one element is given to describe an option, it the value is
7622 # 0 if the option is not present in (the caller's) ARGS or 1 if
7623 # it is.
7624 #
7625 # If two elements are given, the second element is the default value of
7626 # the variable. This is then overwritten if the option exists in ARGS.
7627 # If EVAL, then subst is called on the value, which allows variables
7628 # to be used.
7629 #
7630 # Any parse_args elements in (the caller's) ARGS will be removed, leaving
7631 # any optional components.
7632 #
7633 # Example:
7634 # proc myproc {foo args} {
7635 # parse_list args 1 {{bar} {baz "abc"} {qux}} "-" false
7636 # # ...
7637 # }
7638 # myproc ABC -bar -baz DEF peanut butter
7639 # will define the following variables in myproc:
7640 # foo (=ABC), bar (=1), baz (=DEF), and qux (=0)
7641 # args will be the list {peanut butter}
7642
7643 proc parse_list { level listname argset prefix eval } {
7644 upvar $level $listname args
7645
7646 foreach argument $argset {
7647 if {[llength $argument] == 1} {
7648 # Normalize argument, strip leading/trailing whitespace.
7649 # Allows us to treat {foo} and { foo } the same.
7650 set argument [string trim $argument]
7651
7652 # No default specified, so we assume that we should set
7653 # the value to 1 if the arg is present and 0 if it's not.
7654 # It is assumed that no value is given with the argument.
7655 set pattern "$prefix$argument"
7656 set result [lsearch -exact $args $pattern]
7657
7658 if {$result != -1} then {
7659 set value 1
7660 set args [lreplace $args $result $result]
7661 } else {
7662 set value 0
7663 }
7664 uplevel $level [list set $argument $value]
7665 } elseif {[llength $argument] == 2} {
7666 # There are two items in the argument. The second is a
7667 # default value to use if the item is not present.
7668 # Otherwise, the variable is set to whatever is provided
7669 # after the item in the args.
7670 set arg [lindex $argument 0]
7671 set pattern "$prefix[lindex $arg 0]"
7672 set result [lsearch -exact $args $pattern]
7673
7674 if {$result != -1} then {
7675 set value [lindex $args [expr $result+1]]
7676 if { $eval } {
7677 set value [uplevel [expr $level + 1] [list subst $value]]
7678 }
7679 set args [lreplace $args $result [expr $result+1]]
7680 } else {
7681 set value [lindex $argument 1]
7682 if { $eval } {
7683 set value [uplevel $level [list subst $value]]
7684 }
7685 }
7686 uplevel $level [list set $arg $value]
7687 } else {
7688 error "Badly formatted argument \"$argument\" in argument set"
7689 }
7690 }
7691 }
7692
7693 # Search the caller's args variable and set variables according to the list of
7694 # valid options described by ARGSET.
7695
7696 proc parse_args { argset } {
7697 parse_list 2 args $argset "-" false
7698
7699 # The remaining args should be checked to see that they match the
7700 # number of items expected to be passed into the procedure...
7701 }
7702
7703 # Process the caller's options variable and set variables according
7704 # to the list of valid options described by OPTIONSET.
7705
7706 proc parse_options { optionset } {
7707 parse_list 2 options $optionset "" true
7708
7709 # Require no remaining options.
7710 upvar 1 options options
7711 if { [llength $options] != 0 } {
7712 error "Options left unparsed: $options"
7713 }
7714 }
7715
7716 # Capture the output of COMMAND in a string ignoring PREFIX (a regexp);
7717 # return that string.
7718
7719 proc capture_command_output { command prefix } {
7720 global gdb_prompt
7721 global expect_out
7722
7723 set output_string ""
7724 gdb_test_multiple "$command" "capture_command_output for $command" {
7725 -re "[string_to_regexp ${command}]\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $" {
7726 set output_string $expect_out(1,string)
7727 }
7728 }
7729 return $output_string
7730 }
7731
7732 # A convenience function that joins all the arguments together, with a
7733 # regexp that matches exactly one end of line in between each argument.
7734 # This function is ideal to write the expected output of a GDB command
7735 # that generates more than a couple of lines, as this allows us to write
7736 # each line as a separate string, which is easier to read by a human
7737 # being.
7738
7739 proc multi_line { args } {
7740 if { [llength $args] == 1 } {
7741 set hint "forgot {*} before list argument?"
7742 error "multi_line called with one argument ($hint)"
7743 }
7744 return [join $args "\r\n"]
7745 }
7746
7747 # Similar to the above, but while multi_line is meant to be used to
7748 # match GDB output, this one is meant to be used to build strings to
7749 # send as GDB input.
7750
7751 proc multi_line_input { args } {
7752 return [join $args "\n"]
7753 }
7754
7755 # Return the version of the DejaGnu framework.
7756 #
7757 # The return value is a list containing the major, minor and patch version
7758 # numbers. If the version does not contain a minor or patch number, they will
7759 # be set to 0. For example:
7760 #
7761 # 1.6 -> {1 6 0}
7762 # 1.6.1 -> {1 6 1}
7763 # 2 -> {2 0 0}
7764
7765 proc dejagnu_version { } {
7766 # The frame_version variable is defined by DejaGnu, in runtest.exp.
7767 global frame_version
7768
7769 verbose -log "DejaGnu version: $frame_version"
7770 verbose -log "Expect version: [exp_version]"
7771 verbose -log "Tcl version: [info tclversion]"
7772
7773 set dg_ver [split $frame_version .]
7774
7775 while { [llength $dg_ver] < 3 } {
7776 lappend dg_ver 0
7777 }
7778
7779 return $dg_ver
7780 }
7781
7782 # Define user-defined command COMMAND using the COMMAND_LIST as the
7783 # command's definition. The terminating "end" is added automatically.
7784
7785 proc gdb_define_cmd {command command_list} {
7786 global gdb_prompt
7787
7788 set input [multi_line_input {*}$command_list "end"]
7789 set test "define $command"
7790
7791 gdb_test_multiple "define $command" $test {
7792 -re "End with" {
7793 gdb_test_multiple $input $test {
7794 -re "\r\n$gdb_prompt " {
7795 }
7796 }
7797 }
7798 }
7799 }
7800
7801 # Override the 'cd' builtin with a version that ensures that the
7802 # log file keeps pointing at the same file. We need this because
7803 # unfortunately the path to the log file is recorded using an
7804 # relative path name, and, we sometimes need to close/reopen the log
7805 # after changing the current directory. See get_compiler_info.
7806
7807 rename cd builtin_cd
7808
7809 proc cd { dir } {
7810
7811 # Get the existing log file flags.
7812 set log_file_info [log_file -info]
7813
7814 # Split the flags into args and file name.
7815 set log_file_flags ""
7816 set log_file_file ""
7817 foreach arg [ split "$log_file_info" " "] {
7818 if [string match "-*" $arg] {
7819 lappend log_file_flags $arg
7820 } else {
7821 lappend log_file_file $arg
7822 }
7823 }
7824
7825 # If there was an existing file, ensure it is an absolute path, and then
7826 # reset logging.
7827 if { $log_file_file != "" } {
7828 set log_file_file [file normalize $log_file_file]
7829 log_file
7830 log_file $log_file_flags "$log_file_file"
7831 }
7832
7833 # Call the builtin version of cd.
7834 builtin_cd $dir
7835 }
7836
7837 # Return a list of all languages supported by GDB, suitable for use in
7838 # 'set language NAME'. This doesn't include either the 'local' or
7839 # 'auto' keywords.
7840 proc gdb_supported_languages {} {
7841 return [list c objective-c c++ d go fortran modula-2 asm pascal \
7842 opencl rust minimal ada]
7843 }
7844
7845 # Check if debugging is enabled for gdb.
7846
7847 proc gdb_debug_enabled { } {
7848 global gdbdebug
7849
7850 # If not already read, get the debug setting from environment or board setting.
7851 if {![info exists gdbdebug]} {
7852 global env
7853 if [info exists env(GDB_DEBUG)] {
7854 set gdbdebug $env(GDB_DEBUG)
7855 } elseif [target_info exists gdb,debug] {
7856 set gdbdebug [target_info gdb,debug]
7857 } else {
7858 return 0
7859 }
7860 }
7861
7862 # Ensure it not empty.
7863 return [expr { $gdbdebug != "" }]
7864 }
7865
7866 # Turn on debugging if enabled, or reset if already on.
7867
7868 proc gdb_debug_init { } {
7869
7870 global gdb_prompt
7871
7872 if ![gdb_debug_enabled] {
7873 return;
7874 }
7875
7876 # First ensure logging is off.
7877 send_gdb "set logging enabled off\n"
7878
7879 set debugfile [standard_output_file gdb.debug]
7880 send_gdb "set logging file $debugfile\n"
7881
7882 send_gdb "set logging debugredirect\n"
7883
7884 global gdbdebug
7885 foreach entry [split $gdbdebug ,] {
7886 send_gdb "set debug $entry 1\n"
7887 }
7888
7889 # Now that everything is set, enable logging.
7890 send_gdb "set logging enabled on\n"
7891 gdb_expect 10 {
7892 -re "Copying output to $debugfile.*Redirecting debug output to $debugfile.*$gdb_prompt $" {}
7893 timeout { warning "Couldn't set logging file" }
7894 }
7895 }
7896
7897 # Check if debugging is enabled for gdbserver.
7898
7899 proc gdbserver_debug_enabled { } {
7900 # Always disabled for GDB only setups.
7901 return 0
7902 }
7903
7904 # Open the file for logging gdb input
7905
7906 proc gdb_stdin_log_init { } {
7907 gdb_persistent_global in_file
7908
7909 if {[info exists in_file]} {
7910 # Close existing file.
7911 catch "close $in_file"
7912 }
7913
7914 set logfile [standard_output_file_with_gdb_instance gdb.in]
7915 set in_file [open $logfile w]
7916 }
7917
7918 # Write to the file for logging gdb input.
7919 # TYPE can be one of the following:
7920 # "standard" : Default. Standard message written to the log
7921 # "answer" : Answer to a question (eg "Y"). Not written the log.
7922 # "optional" : Optional message. Not written to the log.
7923
7924 proc gdb_stdin_log_write { message {type standard} } {
7925
7926 global in_file
7927 if {![info exists in_file]} {
7928 return
7929 }
7930
7931 # Check message types.
7932 switch -regexp -- $type {
7933 "answer" {
7934 return
7935 }
7936 "optional" {
7937 return
7938 }
7939 }
7940
7941 # Write to the log and make sure the output is there, even in case
7942 # of crash.
7943 puts -nonewline $in_file "$message"
7944 flush $in_file
7945 }
7946
7947 # Write the command line used to invocate gdb to the cmd file.
7948
7949 proc gdb_write_cmd_file { cmdline } {
7950 set logfile [standard_output_file_with_gdb_instance gdb.cmd]
7951 set cmd_file [open $logfile w]
7952 puts $cmd_file $cmdline
7953 catch "close $cmd_file"
7954 }
7955
7956 # Compare contents of FILE to string STR. Pass with MSG if equal, otherwise
7957 # fail with MSG.
7958
7959 proc cmp_file_string { file str msg } {
7960 if { ![file exists $file]} {
7961 fail "$msg"
7962 return
7963 }
7964
7965 set caught_error [catch {
7966 set fp [open "$file" r]
7967 set file_contents [read $fp]
7968 close $fp
7969 } error_message]
7970 if { $caught_error } then {
7971 error "$error_message"
7972 fail "$msg"
7973 return
7974 }
7975
7976 if { $file_contents == $str } {
7977 pass "$msg"
7978 } else {
7979 fail "$msg"
7980 }
7981 }
7982
7983 # Does the compiler support CTF debug output using '-gctf' compiler
7984 # flag? If not then we should skip these tests. We should also
7985 # skip them if libctf was explicitly disabled.
7986
7987 gdb_caching_proc skip_ctf_tests {
7988 global enable_libctf
7989
7990 if {$enable_libctf eq "no"} {
7991 return 1
7992 }
7993
7994 set can_ctf [gdb_can_simple_compile ctfdebug {
7995 int main () {
7996 return 0;
7997 }
7998 } executable "additional_flags=-gctf"]
7999
8000 return [expr {!$can_ctf}]
8001 }
8002
8003 # Return 1 if compiler supports -gstatement-frontiers. Otherwise,
8004 # return 0.
8005
8006 gdb_caching_proc supports_statement_frontiers {
8007 return [gdb_can_simple_compile supports_statement_frontiers {
8008 int main () {
8009 return 0;
8010 }
8011 } executable "additional_flags=-gstatement-frontiers"]
8012 }
8013
8014 # Return 1 if compiler supports -mmpx -fcheck-pointer-bounds. Otherwise,
8015 # return 0.
8016
8017 gdb_caching_proc supports_mpx_check_pointer_bounds {
8018 set flags "additional_flags=-mmpx additional_flags=-fcheck-pointer-bounds"
8019 return [gdb_can_simple_compile supports_mpx_check_pointer_bounds {
8020 int main () {
8021 return 0;
8022 }
8023 } executable $flags]
8024 }
8025
8026 # Return 1 if compiler supports -fcf-protection=. Otherwise,
8027 # return 0.
8028
8029 gdb_caching_proc supports_fcf_protection {
8030 return [gdb_can_simple_compile supports_fcf_protection {
8031 int main () {
8032 return 0;
8033 }
8034 } executable "additional_flags=-fcf-protection=full"]
8035 }
8036
8037 # Return 1 if symbols were read in using -readnow. Otherwise, return 0.
8038
8039 proc readnow { args } {
8040 if { [llength $args] == 1 } {
8041 set re [lindex $args 0]
8042 } else {
8043 set re ""
8044 }
8045
8046 set readnow_p 0
8047 # Given the listing from the following command can be very verbose, match
8048 # the patterns line-by-line. This prevents timeouts from waiting for
8049 # too much data to come at once.
8050 set cmd "maint print objfiles $re"
8051 gdb_test_multiple $cmd "" -lbl {
8052 -re "\r\n.gdb_index: faked for \"readnow\"" {
8053 # Record the we've seen the above pattern.
8054 set readnow_p 1
8055 exp_continue
8056 }
8057 -re -wrap "" {
8058 # We don't care about any other input.
8059 }
8060 }
8061
8062 return $readnow_p
8063 }
8064
8065 # Return index name if symbols were read in using an index.
8066 # Otherwise, return "".
8067
8068 proc have_index { objfile } {
8069
8070 set res ""
8071 set cmd "maint print objfiles $objfile"
8072 gdb_test_multiple $cmd "" -lbl {
8073 -re "\r\n.gdb_index: faked for \"readnow\"" {
8074 set res ""
8075 exp_continue
8076 }
8077 -re "\r\n.gdb_index:" {
8078 set res "gdb_index"
8079 exp_continue
8080 }
8081 -re "\r\n.debug_names:" {
8082 set res "debug_names"
8083 exp_continue
8084 }
8085 -re -wrap "" {
8086 # We don't care about any other input.
8087 }
8088 }
8089
8090 return $res
8091 }
8092
8093 # Return 1 if partial symbols are available. Otherwise, return 0.
8094
8095 proc psymtabs_p { } {
8096 global gdb_prompt
8097
8098 set cmd "maint info psymtab"
8099 gdb_test_multiple $cmd "" {
8100 -re "$cmd\r\n$gdb_prompt $" {
8101 return 0
8102 }
8103 -re -wrap "" {
8104 return 1
8105 }
8106 }
8107
8108 return 0
8109 }
8110
8111 # Verify that partial symtab expansion for $filename has state $readin.
8112
8113 proc verify_psymtab_expanded { filename readin } {
8114 global gdb_prompt
8115
8116 set cmd "maint info psymtab"
8117 set test "$cmd: $filename: $readin"
8118 set re [multi_line \
8119 " \{ psymtab \[^\r\n\]*$filename\[^\r\n\]*" \
8120 " readin $readin" \
8121 ".*"]
8122
8123 gdb_test_multiple $cmd $test {
8124 -re "$cmd\r\n$gdb_prompt $" {
8125 unsupported $gdb_test_name
8126 }
8127 -re -wrap $re {
8128 pass $gdb_test_name
8129 }
8130 }
8131 }
8132
8133 # Add a .gdb_index section to PROGRAM.
8134 # PROGRAM is assumed to be the output of standard_output_file.
8135 # Returns the 0 if there is a failure, otherwise 1.
8136 #
8137 # STYLE controls which style of index to add, if needed. The empty
8138 # string (the default) means .gdb_index; "-dwarf-5" means .debug_names.
8139
8140 proc add_gdb_index { program {style ""} } {
8141 global srcdir GDB env
8142 set contrib_dir "$srcdir/../contrib"
8143 set env(GDB) [append_gdb_data_directory_option $GDB]
8144 set result [catch "exec $contrib_dir/gdb-add-index.sh $style $program" output]
8145 if { $result != 0 } {
8146 verbose -log "result is $result"
8147 verbose -log "output is $output"
8148 return 0
8149 }
8150
8151 return 1
8152 }
8153
8154 # Add a .gdb_index section to PROGRAM, unless it alread has an index
8155 # (.gdb_index/.debug_names). Gdb doesn't support building an index from a
8156 # program already using one. Return 1 if a .gdb_index was added, return 0
8157 # if it already contained an index, and -1 if an error occurred.
8158 #
8159 # STYLE controls which style of index to add, if needed. The empty
8160 # string (the default) means .gdb_index; "-dwarf-5" means .debug_names.
8161
8162 proc ensure_gdb_index { binfile {style ""} } {
8163 global decimal
8164
8165 set testfile [file tail $binfile]
8166 set test "check if index present"
8167 set has_index 0
8168 set has_readnow 0
8169 gdb_test_multiple "mt print objfiles ${testfile}" $test -lbl {
8170 -re "\r\n\\.gdb_index: version ${decimal}(?=\r\n)" {
8171 set has_index 1
8172 gdb_test_lines "" $gdb_test_name ".*"
8173 }
8174 -re "\r\n\\.debug_names: exists(?=\r\n)" {
8175 set has_index 1
8176 gdb_test_lines "" $gdb_test_name ".*"
8177 }
8178 -re "\r\n(Cooked index in use|Psymtabs)(?=\r\n)" {
8179 gdb_test_lines "" $gdb_test_name ".*"
8180 }
8181 -re ".gdb_index: faked for \"readnow\"" {
8182 set has_readnow 1
8183 gdb_test_lines "" $gdb_test_name ".*"
8184 }
8185 -re -wrap "" {
8186 fail $gdb_test_name
8187 }
8188 }
8189
8190 if { $has_index } {
8191 return 0
8192 }
8193
8194 if { $has_readnow } {
8195 return -1
8196 }
8197
8198 if { [add_gdb_index $binfile $style] == "1" } {
8199 return 1
8200 }
8201
8202 return -1
8203 }
8204
8205 # Return 1 if executable contains .debug_types section. Otherwise, return 0.
8206
8207 proc debug_types { } {
8208 global hex
8209
8210 set cmd "maint info sections"
8211 gdb_test_multiple $cmd "" {
8212 -re -wrap "at $hex: .debug_types.*" {
8213 return 1
8214 }
8215 -re -wrap "" {
8216 return 0
8217 }
8218 }
8219
8220 return 0
8221 }
8222
8223 # Return the addresses in the line table for FILE for which is_stmt is true.
8224
8225 proc is_stmt_addresses { file } {
8226 global decimal
8227 global hex
8228
8229 set is_stmt [list]
8230
8231 gdb_test_multiple "maint info line-table $file" "" {
8232 -re "\r\n$decimal\[ \t\]+$decimal\[ \t\]+($hex)\[ \t\]+Y\[^\r\n\]*" {
8233 lappend is_stmt $expect_out(1,string)
8234 exp_continue
8235 }
8236 -re -wrap "" {
8237 }
8238 }
8239
8240 return $is_stmt
8241 }
8242
8243 # Return 1 if hex number VAL is an element of HEXLIST.
8244
8245 proc hex_in_list { val hexlist } {
8246 # Normalize val by removing 0x prefix, and leading zeros.
8247 set val [regsub ^0x $val ""]
8248 set val [regsub ^0+ $val "0"]
8249
8250 set re 0x0*$val
8251 set index [lsearch -regexp $hexlist $re]
8252 return [expr $index != -1]
8253 }
8254
8255 # Override proc NAME to proc OVERRIDE for the duration of the execution of
8256 # BODY.
8257
8258 proc with_override { name override body } {
8259 # Implementation note: It's possible to implement the override using
8260 # rename, like this:
8261 # rename $name save_$name
8262 # rename $override $name
8263 # set code [catch {uplevel 1 $body} result]
8264 # rename $name $override
8265 # rename save_$name $name
8266 # but there are two issues here:
8267 # - the save_$name might clash with an existing proc
8268 # - the override is no longer available under its original name during
8269 # the override
8270 # So, we use this more elaborate but cleaner mechanism.
8271
8272 # Save the old proc, if it exists.
8273 if { [info procs $name] != "" } {
8274 set old_args [info args $name]
8275 set old_body [info body $name]
8276 set existed true
8277 } else {
8278 set existed false
8279 }
8280
8281 # Install the override.
8282 set new_args [info args $override]
8283 set new_body [info body $override]
8284 eval proc $name {$new_args} {$new_body}
8285
8286 # Execute body.
8287 set code [catch {uplevel 1 $body} result]
8288
8289 # Restore old proc if it existed on entry, else delete it.
8290 if { $existed } {
8291 eval proc $name {$old_args} {$old_body}
8292 } else {
8293 rename $name ""
8294 }
8295
8296 # Return as appropriate.
8297 if { $code == 1 } {
8298 global errorInfo errorCode
8299 return -code error -errorinfo $errorInfo -errorcode $errorCode $result
8300 } elseif { $code > 1 } {
8301 return -code $code $result
8302 }
8303
8304 return $result
8305 }
8306
8307 # Setup tuiterm.exp environment. To be used in test-cases instead of
8308 # "load_lib tuiterm.exp". Calls initialization function and schedules
8309 # finalization function.
8310 proc tuiterm_env { } {
8311 load_lib tuiterm.exp
8312 }
8313
8314 # Dejagnu has a version of note, but usage is not allowed outside of dejagnu.
8315 # Define a local version.
8316 proc gdb_note { message } {
8317 verbose -- "NOTE: $message" 0
8318 }
8319
8320 # Return 1 if compiler supports -fuse-ld=gold, otherwise return 0.
8321 gdb_caching_proc have_fuse_ld_gold {
8322 set me "have_fuse_ld_gold"
8323 set flags "additional_flags=-fuse-ld=gold"
8324 set src { int main() { return 0; } }
8325 return [gdb_simple_compile $me $src executable $flags]
8326 }
8327
8328 # Return 1 if linker supports -Ttext-segment, otherwise return 0.
8329 gdb_caching_proc linker_supports_Ttext_segment_flag {
8330 set me "linker_supports_Ttext_segment_flag"
8331 set flags additional_flags="-Wl,-Ttext-segment=0x7000000"
8332 set src { int main() { return 0; } }
8333 return [gdb_simple_compile $me $src executable $flags]
8334 }
8335
8336 # Return 1 if linker supports -Ttext, otherwise return 0.
8337 gdb_caching_proc linker_supports_Ttext_flag {
8338 set me "linker_supports_Ttext_flag"
8339 set flags additional_flags="-Wl,-Ttext=0x7000000"
8340 set src { int main() { return 0; } }
8341 return [gdb_simple_compile $me $src executable $flags]
8342 }
8343
8344 # Return 1 if linker supports --image-base, otherwise 0.
8345 gdb_caching_proc linker_supports_image_base_flag {
8346 set me "linker_supports_image_base_flag"
8347 set flags additional_flags="-Wl,--image-base=0x7000000"
8348 set src { int main() { return 0; } }
8349 return [gdb_simple_compile $me $src executable $flags]
8350 }
8351
8352
8353 # Return 1 if compiler supports scalar_storage_order attribute, otherwise
8354 # return 0.
8355 gdb_caching_proc supports_scalar_storage_order_attribute {
8356 set me "supports_scalar_storage_order_attribute"
8357 set src {
8358 #include <string.h>
8359 struct sle {
8360 int v;
8361 } __attribute__((scalar_storage_order("little-endian")));
8362 struct sbe {
8363 int v;
8364 } __attribute__((scalar_storage_order("big-endian")));
8365 struct sle sle;
8366 struct sbe sbe;
8367 int main () {
8368 sle.v = sbe.v = 0x11223344;
8369 int same = memcmp (&sle, &sbe, sizeof (int)) == 0;
8370 int sso = !same;
8371 return sso;
8372 }
8373 }
8374 if { ![gdb_simple_compile $me $src executable ""] } {
8375 return 0
8376 }
8377
8378 set result [remote_exec target $obj]
8379 set status [lindex $result 0]
8380 set output [lindex $result 1]
8381 if { $output != "" } {
8382 return 0
8383 }
8384
8385 return $status
8386 }
8387
8388 # Return 1 if compiler supports __GNUC__, otherwise return 0.
8389 gdb_caching_proc supports_gnuc {
8390 set me "supports_gnuc"
8391 set src {
8392 #ifndef __GNUC__
8393 #error "No gnuc"
8394 #endif
8395 }
8396 return [gdb_simple_compile $me $src object ""]
8397 }
8398
8399 # Return 1 if target supports mpx, otherwise return 0.
8400 gdb_caching_proc have_mpx {
8401 global srcdir
8402
8403 set me "have_mpx"
8404 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
8405 verbose "$me: target does not support mpx, returning 0" 2
8406 return 0
8407 }
8408
8409 # Compile a test program.
8410 set src {
8411 #include "nat/x86-cpuid.h"
8412
8413 int main() {
8414 unsigned int eax, ebx, ecx, edx;
8415
8416 if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
8417 return 0;
8418
8419 if ((ecx & bit_OSXSAVE) == bit_OSXSAVE)
8420 {
8421 if (__get_cpuid_max (0, (void *)0) < 7)
8422 return 0;
8423
8424 __cpuid_count (7, 0, eax, ebx, ecx, edx);
8425
8426 if ((ebx & bit_MPX) == bit_MPX)
8427 return 1;
8428
8429 }
8430 return 0;
8431 }
8432 }
8433 set compile_flags "incdir=${srcdir}/.."
8434 if {![gdb_simple_compile $me $src executable $compile_flags]} {
8435 return 0
8436 }
8437
8438 set result [remote_exec target $obj]
8439 set status [lindex $result 0]
8440 set output [lindex $result 1]
8441 if { $output != "" } {
8442 set status 0
8443 }
8444
8445 remote_file build delete $obj
8446
8447 if { $status == 0 } {
8448 verbose "$me: returning $status" 2
8449 return $status
8450 }
8451
8452 # Compile program with -mmpx -fcheck-pointer-bounds, try to trigger
8453 # 'No MPX support', in other words, see if kernel supports mpx.
8454 set src { int main (void) { return 0; } }
8455 set comp_flags {}
8456 append comp_flags " additional_flags=-mmpx"
8457 append comp_flags " additional_flags=-fcheck-pointer-bounds"
8458 if {![gdb_simple_compile $me-2 $src executable $comp_flags]} {
8459 return 0
8460 }
8461
8462 set result [remote_exec target $obj]
8463 set status [lindex $result 0]
8464 set output [lindex $result 1]
8465 set status [expr ($status == 0) \
8466 && ![string equal $output "No MPX support\r\n"]]
8467
8468 remote_file build delete $obj
8469
8470 verbose "$me: returning $status" 2
8471 return $status
8472 }
8473
8474 # Return 1 if target supports avx, otherwise return 0.
8475 gdb_caching_proc have_avx {
8476 global srcdir
8477
8478 set me "have_avx"
8479 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
8480 verbose "$me: target does not support avx, returning 0" 2
8481 return 0
8482 }
8483
8484 # Compile a test program.
8485 set src {
8486 #include "nat/x86-cpuid.h"
8487
8488 int main() {
8489 unsigned int eax, ebx, ecx, edx;
8490
8491 if (!x86_cpuid (1, &eax, &ebx, &ecx, &edx))
8492 return 0;
8493
8494 if ((ecx & (bit_AVX | bit_OSXSAVE)) == (bit_AVX | bit_OSXSAVE))
8495 return 1;
8496 else
8497 return 0;
8498 }
8499 }
8500 set compile_flags "incdir=${srcdir}/.."
8501 if {![gdb_simple_compile $me $src executable $compile_flags]} {
8502 return 0
8503 }
8504
8505 set result [remote_exec target $obj]
8506 set status [lindex $result 0]
8507 set output [lindex $result 1]
8508 if { $output != "" } {
8509 set status 0
8510 }
8511
8512 remote_file build delete $obj
8513
8514 verbose "$me: returning $status" 2
8515 return $status
8516 }
8517
8518 # Called as either:
8519 # - require EXPR VAL
8520 # - require EXPR OP VAL
8521 # In the first case, OP is ==.
8522 #
8523 # Require EXPR OP VAL, where EXPR is evaluated in caller context. If not,
8524 # return in the caller's context.
8525
8526 proc require { fn arg1 {arg2 ""} } {
8527 if { $arg2 == "" } {
8528 set op ==
8529 set val $arg1
8530 } else {
8531 set op $arg1
8532 set val $arg2
8533 }
8534 set res [uplevel 1 $fn]
8535 if { [expr $res $op $val] } {
8536 return
8537 }
8538
8539 switch "$fn $op $val" {
8540 "gdb_skip_xml_test == 0" { set msg "missing xml support" }
8541 "ensure_gdb_index $binfile != -1" -
8542 "ensure_gdb_index $binfile -dwarf-5 != -1" {
8543 set msg "Couldn't ensure index in binfile"
8544 }
8545 "use_gdb_stub == 0" {
8546 set msg "Remote stub used"
8547 }
8548 default { set msg "$fn != $val" }
8549 }
8550
8551 untested $msg
8552 return -code return 0
8553 }
8554
8555 # Wait up to ::TIMEOUT seconds for file PATH to exist on the target system.
8556 # Return 1 if it does exist, 0 otherwise.
8557
8558 proc target_file_exists_with_timeout { path } {
8559 for {set i 0} {$i < $::timeout} {incr i} {
8560 if { [remote_file target exists $path] } {
8561 return 1
8562 }
8563
8564 sleep 1
8565 }
8566
8567 return 0
8568 }
8569
8570 gdb_caching_proc has_hw_wp_support {
8571 # Power 9, proc rev 2.2 does not support HW watchpoints due to HW bug.
8572 # Need to use a runtime test to determine if the Power processor has
8573 # support for HW watchpoints.
8574 global srcdir subdir gdb_prompt inferior_exited_re
8575
8576 set compile_flags {debug nowarnings quiet}
8577 set me "has_hw_wp_support"
8578
8579 # Compile a test program to test if HW watchpoints are supported
8580 set src {
8581 int main (void) {
8582 volatile int local;
8583 local = 1;
8584 if (local == 1)
8585 return 1;
8586 return 0;
8587 }
8588 }
8589
8590 if {![gdb_simple_compile $me $src executable $compile_flags]} {
8591 return 0
8592 }
8593
8594 gdb_exit
8595 gdb_start
8596 gdb_reinitialize_dir $srcdir/$subdir
8597 gdb_load "$obj"
8598
8599 if ![runto_main] {
8600 set has_hw_wp_support 0
8601 return $has_hw_wp_support
8602 }
8603
8604 # The goal is to determine if HW watchpoints are available in general.
8605 # Use "watch" and then check if gdb responds with hardware watch point.
8606 set test "watch local"
8607
8608 gdb_test_multiple $test "Check for HW watchpoint support" {
8609 -re ".*Hardware watchpoint.*" {
8610 # HW watchpoint supported by platform
8611 verbose -log "\n$me: Hardware watchpoint detected"
8612 set has_hw_wp_support 1
8613 }
8614 -re ".*$gdb_prompt $" {
8615 set has_hw_wp_support 0
8616 verbose -log "\n$me: Default, hardware watchpoint not deteced"
8617 }
8618 }
8619
8620 gdb_exit
8621 remote_file build delete $obj
8622
8623 verbose "$me: returning $has_hw_wp_support" 2
8624 return $has_hw_wp_support
8625 }
8626
8627 # Return a list of all the accepted values of the set command SET_CMD.
8628
8629 proc get_set_option_choices {set_cmd} {
8630 global gdb_prompt
8631
8632 set values {}
8633
8634 set test "complete $set_cmd"
8635 gdb_test_multiple "complete $set_cmd " "$test" {
8636 -re "$set_cmd (\[^\r\n\]+)\r\n" {
8637 lappend values $expect_out(1,string)
8638 exp_continue
8639 }
8640 -re "$gdb_prompt " {
8641 pass $test
8642 }
8643 }
8644 return $values
8645 }
8646
8647 # Always load compatibility stuff.
8648 load_lib future.exp