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