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