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