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