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