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