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