* lib/gdb.exp (gdb_unload): Change wording of perror text to be
[binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright 1992-2005, 2007-2012 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 load_lib libgloss.exp
29
30 global GDB
31
32 if [info exists TOOL_EXECUTABLE] {
33 set GDB $TOOL_EXECUTABLE;
34 }
35 if ![info exists GDB] {
36 if ![is_remote host] {
37 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
38 } else {
39 set GDB [transform gdb];
40 }
41 }
42 verbose "using GDB = $GDB" 2
43
44 # GDBFLAGS is available for the user to set on the command line.
45 # E.g. make check RUNTESTFLAGS=GDBFLAGS=mumble
46 # Testcases may use it to add additional flags, but they must:
47 # - append new flags, not overwrite
48 # - restore the original value when done
49 global GDBFLAGS
50 if ![info exists GDBFLAGS] {
51 set GDBFLAGS ""
52 }
53 verbose "using GDBFLAGS = $GDBFLAGS" 2
54
55 # Make the build data directory available to tests.
56 set BUILD_DATA_DIRECTORY "[pwd]/../data-directory"
57
58 # INTERNAL_GDBFLAGS contains flags that the testsuite requires.
59 global INTERNAL_GDBFLAGS
60 if ![info exists INTERNAL_GDBFLAGS] {
61 set INTERNAL_GDBFLAGS "-nw -nx -data-directory $BUILD_DATA_DIRECTORY"
62 }
63
64 # The variable gdb_prompt is a regexp which matches the gdb prompt.
65 # Set it if it is not already set.
66 global gdb_prompt
67 if ![info exists gdb_prompt] then {
68 set gdb_prompt "\[(\]gdb\[)\]"
69 }
70
71 # The variable fullname_syntax_POSIX is a regexp which matches a POSIX
72 # absolute path ie. /foo/
73 set fullname_syntax_POSIX {/[^\n]*/}
74 # The variable fullname_syntax_UNC is a regexp which matches a Windows
75 # UNC path ie. \\D\foo\
76 set fullname_syntax_UNC {\\\\[^\\]+\\[^\n]+\\}
77 # The variable fullname_syntax_DOS_CASE is a regexp which matches a
78 # particular DOS case that GDB most likely will output
79 # ie. \foo\, but don't match \\.*\
80 set fullname_syntax_DOS_CASE {\\[^\\][^\n]*\\}
81 # The variable fullname_syntax_DOS is a regexp which matches a DOS path
82 # ie. a:\foo\ && a:foo\
83 set fullname_syntax_DOS {[a-zA-Z]:[^\n]*\\}
84 # The variable fullname_syntax is a regexp which matches what GDB considers
85 # an absolute path. It is currently debatable if the Windows style paths
86 # d:foo and \abc should be considered valid as an absolute path.
87 # Also, the purpse of this regexp is not to recognize a well formed
88 # absolute path, but to say with certainty that a path is absolute.
89 set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
90
91 # Needed for some tests under Cygwin.
92 global EXEEXT
93 global env
94
95 if ![info exists env(EXEEXT)] {
96 set EXEEXT ""
97 } else {
98 set EXEEXT $env(EXEEXT)
99 }
100
101 set octal "\[0-7\]+"
102
103 set inferior_exited_re "(\\\[Inferior \[0-9\]+ \\(.*\\) exited)"
104
105 ### Only procedures should come after this point.
106
107 #
108 # gdb_version -- extract and print the version number of GDB
109 #
110 proc default_gdb_version {} {
111 global GDB
112 global INTERNAL_GDBFLAGS GDBFLAGS
113 global gdb_prompt
114 set output [remote_exec host "$GDB $INTERNAL_GDBFLAGS --version"]
115 set tmp [lindex $output 1];
116 set version ""
117 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
118 if ![is_remote host] {
119 clone_output "[which $GDB] version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
120 } else {
121 clone_output "$GDB on remote host version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
122 }
123 }
124
125 proc gdb_version { } {
126 return [default_gdb_version];
127 }
128
129 #
130 # gdb_unload -- unload a file if one is loaded
131 #
132
133 proc gdb_unload {} {
134 global verbose
135 global GDB
136 global gdb_prompt
137 send_gdb "file\n"
138 gdb_expect 60 {
139 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
140 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
141 -re "A program is being debugged already.*Are you sure you want to change the file.*y or n. $" {
142 send_gdb "y\n"
143 exp_continue
144 }
145 -re "Discard symbol table from .*y or n.*$" {
146 send_gdb "y\n"
147 exp_continue
148 }
149 -re "$gdb_prompt $" {}
150 timeout {
151 perror "couldn't unload file in $GDB (timeout)."
152 return -1
153 }
154 }
155 }
156
157 # Many of the tests depend on setting breakpoints at various places and
158 # running until that breakpoint is reached. At times, we want to start
159 # with a clean-slate with respect to breakpoints, so this utility proc
160 # lets us do this without duplicating this code everywhere.
161 #
162
163 proc delete_breakpoints {} {
164 global gdb_prompt
165
166 # we need a larger timeout value here or this thing just confuses
167 # itself. May need a better implementation if possible. - guo
168 #
169 send_gdb "delete breakpoints\n"
170 gdb_expect 100 {
171 -re "Delete all breakpoints.*y or n.*$" {
172 send_gdb "y\n";
173 exp_continue
174 }
175 -re "$gdb_prompt $" { # This happens if there were no breakpoints
176 }
177 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
178 }
179 send_gdb "info breakpoints\n"
180 gdb_expect 100 {
181 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
182 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
183 -re "Delete all breakpoints.*or n.*$" {
184 send_gdb "y\n";
185 exp_continue
186 }
187 timeout { perror "info breakpoints (timeout)" ; return }
188 }
189 }
190
191 # Generic run command.
192 #
193 # The second pattern below matches up to the first newline *only*.
194 # Using ``.*$'' could swallow up output that we attempt to match
195 # elsewhere.
196 #
197 # N.B. This function does not wait for gdb to return to the prompt,
198 # that is the caller's responsibility.
199
200 proc gdb_run_cmd {args} {
201 global gdb_prompt use_gdb_stub
202
203 if [target_info exists gdb_init_command] {
204 send_gdb "[target_info gdb_init_command]\n";
205 gdb_expect 30 {
206 -re "$gdb_prompt $" { }
207 default {
208 perror "gdb_init_command for target failed";
209 return;
210 }
211 }
212 }
213
214 if $use_gdb_stub {
215 if [target_info exists gdb,do_reload_on_run] {
216 if { [gdb_reload] != 0 } {
217 return;
218 }
219 send_gdb "continue\n";
220 gdb_expect 60 {
221 -re "Continu\[^\r\n\]*\[\r\n\]" {}
222 default {}
223 }
224 return;
225 }
226
227 if [target_info exists gdb,start_symbol] {
228 set start [target_info gdb,start_symbol];
229 } else {
230 set start "start";
231 }
232 send_gdb "jump *$start\n"
233 set start_attempt 1;
234 while { $start_attempt } {
235 # Cap (re)start attempts at three to ensure that this loop
236 # always eventually fails. Don't worry about trying to be
237 # clever and not send a command when it has failed.
238 if [expr $start_attempt > 3] {
239 perror "Jump to start() failed (retry count exceeded)";
240 return;
241 }
242 set start_attempt [expr $start_attempt + 1];
243 gdb_expect 30 {
244 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
245 set start_attempt 0;
246 }
247 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
248 perror "Can't find start symbol to run in gdb_run";
249 return;
250 }
251 -re "No symbol \"start\" in current.*$gdb_prompt $" {
252 send_gdb "jump *_start\n";
253 }
254 -re "No symbol.*context.*$gdb_prompt $" {
255 set start_attempt 0;
256 }
257 -re "Line.* Jump anyway.*y or n. $" {
258 send_gdb "y\n"
259 }
260 -re "The program is not being run.*$gdb_prompt $" {
261 if { [gdb_reload] != 0 } {
262 return;
263 }
264 send_gdb "jump *$start\n";
265 }
266 timeout {
267 perror "Jump to start() failed (timeout)";
268 return
269 }
270 }
271 }
272 return
273 }
274
275 if [target_info exists gdb,do_reload_on_run] {
276 if { [gdb_reload] != 0 } {
277 return;
278 }
279 }
280 send_gdb "run $args\n"
281 # This doesn't work quite right yet.
282 # Use -notransfer here so that test cases (like chng-sym.exp)
283 # may test for additional start-up messages.
284 gdb_expect 60 {
285 -re "The program .* has been started already.*y or n. $" {
286 send_gdb "y\n"
287 exp_continue
288 }
289 -notransfer -re "Starting program: \[^\r\n\]*" {}
290 -notransfer -re "$gdb_prompt $" {
291 # There is no more input expected.
292 }
293 }
294 }
295
296 # Generic start command. Return 0 if we could start the program, -1
297 # if we could not.
298 #
299 # N.B. This function does not wait for gdb to return to the prompt,
300 # that is the caller's responsibility.
301
302 proc gdb_start_cmd {args} {
303 global gdb_prompt use_gdb_stub
304
305 if [target_info exists gdb_init_command] {
306 send_gdb "[target_info gdb_init_command]\n";
307 gdb_expect 30 {
308 -re "$gdb_prompt $" { }
309 default {
310 perror "gdb_init_command for target failed";
311 return -1;
312 }
313 }
314 }
315
316 if $use_gdb_stub {
317 return -1
318 }
319
320 send_gdb "start $args\n"
321 # Use -notransfer here so that test cases (like chng-sym.exp)
322 # may test for additional start-up messages.
323 gdb_expect 60 {
324 -re "The program .* has been started already.*y or n. $" {
325 send_gdb "y\n"
326 exp_continue
327 }
328 -notransfer -re "Starting program: \[^\r\n\]*" {
329 return 0
330 }
331 }
332 return -1
333 }
334
335 # Set a breakpoint at FUNCTION. If there is an additional argument it is
336 # a list of options; the supported options are allow-pending, temporary,
337 # and no-message.
338
339 proc gdb_breakpoint { function args } {
340 global gdb_prompt
341 global decimal
342
343 set pending_response n
344 if {[lsearch -exact [lindex $args 0] allow-pending] != -1} {
345 set pending_response y
346 }
347
348 set break_command "break"
349 set break_message "Breakpoint"
350 if {[lsearch -exact [lindex $args 0] temporary] != -1} {
351 set break_command "tbreak"
352 set break_message "Temporary breakpoint"
353 }
354
355 set no_message 0
356 if {[lsearch -exact [lindex $args 0] no-message] != -1} {
357 set no_message 1
358 }
359
360 send_gdb "$break_command $function\n"
361 # The first two regexps are what we get with -g, the third is without -g.
362 gdb_expect 30 {
363 -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
364 -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
365 -re "$break_message \[0-9\]* at .*$gdb_prompt $" {}
366 -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
367 if {$pending_response == "n"} {
368 if { $no_message == 0 } {
369 fail "setting breakpoint at $function"
370 }
371 return 0
372 }
373 }
374 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
375 send_gdb "$pending_response\n"
376 exp_continue
377 }
378 -re "A problem internal to GDB has been detected" {
379 fail "setting breakpoint at $function in runto (GDB internal error)"
380 gdb_internal_error_resync
381 return 0
382 }
383 -re "$gdb_prompt $" {
384 if { $no_message == 0 } {
385 fail "setting breakpoint at $function"
386 }
387 return 0
388 }
389 timeout {
390 if { $no_message == 0 } {
391 fail "setting breakpoint at $function (timeout)"
392 }
393 return 0
394 }
395 }
396 return 1;
397 }
398
399 # Set breakpoint at function and run gdb until it breaks there.
400 # Since this is the only breakpoint that will be set, if it stops
401 # at a breakpoint, we will assume it is the one we want. We can't
402 # just compare to "function" because it might be a fully qualified,
403 # single quoted C++ function specifier. If there's an additional argument,
404 # pass it to gdb_breakpoint.
405
406 proc runto { function args } {
407 global gdb_prompt
408 global decimal
409
410 delete_breakpoints
411
412 if ![gdb_breakpoint $function [lindex $args 0]] {
413 return 0;
414 }
415
416 gdb_run_cmd
417
418 # the "at foo.c:36" output we get with -g.
419 # the "in func" output we get without -g.
420 gdb_expect 30 {
421 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
422 return 1
423 }
424 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
425 return 1
426 }
427 -re "The target does not support running in non-stop mode.\r\n$gdb_prompt $" {
428 unsupported "Non-stop mode not supported"
429 return 0
430 }
431 -re ".*A problem internal to GDB has been detected" {
432 fail "running to $function in runto (GDB internal error)"
433 gdb_internal_error_resync
434 return 0
435 }
436 -re "$gdb_prompt $" {
437 fail "running to $function in runto"
438 return 0
439 }
440 eof {
441 fail "running to $function in runto (eof)"
442 return 0
443 }
444 timeout {
445 fail "running to $function in runto (timeout)"
446 return 0
447 }
448 }
449 return 1
450 }
451
452 # Ask gdb to run until we hit a breakpoint at main.
453 #
454 # N.B. This function deletes all existing breakpoints.
455 # If you don't want that, use gdb_start_cmd.
456
457 proc runto_main { } {
458 return [runto main]
459 }
460
461 ### Continue, and expect to hit a breakpoint.
462 ### Report a pass or fail, depending on whether it seems to have
463 ### worked. Use NAME as part of the test name; each call to
464 ### continue_to_breakpoint should use a NAME which is unique within
465 ### that test file.
466 proc gdb_continue_to_breakpoint {name {location_pattern .*}} {
467 global gdb_prompt
468 set full_name "continue to breakpoint: $name"
469
470 send_gdb "continue\n"
471 gdb_expect {
472 -re "(?:Breakpoint|Temporary breakpoint) .* (at|in) $location_pattern\r\n$gdb_prompt $" {
473 pass $full_name
474 }
475 -re ".*$gdb_prompt $" {
476 fail $full_name
477 }
478 timeout {
479 fail "$full_name (timeout)"
480 }
481 }
482 }
483
484
485 # gdb_internal_error_resync:
486 #
487 # Answer the questions GDB asks after it reports an internal error
488 # until we get back to a GDB prompt. Decline to quit the debugging
489 # session, and decline to create a core file. Return non-zero if the
490 # resync succeeds.
491 #
492 # This procedure just answers whatever questions come up until it sees
493 # a GDB prompt; it doesn't require you to have matched the input up to
494 # any specific point. However, it only answers questions it sees in
495 # the output itself, so if you've matched a question, you had better
496 # answer it yourself before calling this.
497 #
498 # You can use this function thus:
499 #
500 # gdb_expect {
501 # ...
502 # -re ".*A problem internal to GDB has been detected" {
503 # gdb_internal_error_resync
504 # }
505 # ...
506 # }
507 #
508 proc gdb_internal_error_resync {} {
509 global gdb_prompt
510
511 set count 0
512 while {$count < 10} {
513 gdb_expect {
514 -re "Quit this debugging session\\? \\(y or n\\) $" {
515 send_gdb "n\n"
516 incr count
517 }
518 -re "Create a core file of GDB\\? \\(y or n\\) $" {
519 send_gdb "n\n"
520 incr count
521 }
522 -re "$gdb_prompt $" {
523 # We're resynchronized.
524 return 1
525 }
526 timeout {
527 perror "Could not resync from internal error (timeout)"
528 return 0
529 }
530 }
531 }
532 perror "Could not resync from internal error (resync count exceeded)"
533 return 0
534 }
535
536
537 # gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
538 # Send a command to gdb; test the result.
539 #
540 # COMMAND is the command to execute, send to GDB with send_gdb. If
541 # this is the null string no command is sent.
542 # MESSAGE is a message to be printed with the built-in failure patterns
543 # if one of them matches. If MESSAGE is empty COMMAND will be used.
544 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
545 # patterns. Pattern elements will be evaluated in the caller's
546 # context; action elements will be executed in the caller's context.
547 # Unlike patterns for gdb_test, these patterns should generally include
548 # the final newline and prompt.
549 #
550 # Returns:
551 # 1 if the test failed, according to a built-in failure pattern
552 # 0 if only user-supplied patterns matched
553 # -1 if there was an internal error.
554 #
555 # You can use this function thus:
556 #
557 # gdb_test_multiple "print foo" "test foo" {
558 # -re "expected output 1" {
559 # pass "print foo"
560 # }
561 # -re "expected output 2" {
562 # fail "print foo"
563 # }
564 # }
565 #
566 # The standard patterns, such as "Inferior exited..." and "A problem
567 # ...", all being implicitly appended to that list.
568 #
569 proc gdb_test_multiple { command message user_code } {
570 global verbose use_gdb_stub
571 global gdb_prompt
572 global GDB
573 global inferior_exited_re
574 upvar timeout timeout
575 upvar expect_out expect_out
576
577 if { $message == "" } {
578 set message $command
579 }
580
581 if [string match "*\[\r\n\]" $command] {
582 error "Invalid trailing newline in \"$message\" test"
583 }
584
585 if [string match "*\[\r\n\]*" $message] {
586 error "Invalid newline in \"$message\" test"
587 }
588
589 if {$use_gdb_stub
590 && [regexp -nocase {^\s*(r|run|star|start|at|att|atta|attac|attach)\M} \
591 $command]} {
592 error "gdbserver does not support $command without extended-remote"
593 }
594
595 # TCL/EXPECT WART ALERT
596 # Expect does something very strange when it receives a single braced
597 # argument. It splits it along word separators and performs substitutions.
598 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
599 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
600 # double-quoted list item, "\[ab\]" is just a long way of representing
601 # "[ab]", because the backslashes will be removed by lindex.
602
603 # Unfortunately, there appears to be no easy way to duplicate the splitting
604 # that expect will do from within TCL. And many places make use of the
605 # "\[0-9\]" construct, so we need to support that; and some places make use
606 # of the "[func]" construct, so we need to support that too. In order to
607 # get this right we have to substitute quoted list elements differently
608 # from braced list elements.
609
610 # We do this roughly the same way that Expect does it. We have to use two
611 # lists, because if we leave unquoted newlines in the argument to uplevel
612 # they'll be treated as command separators, and if we escape newlines
613 # we mangle newlines inside of command blocks. This assumes that the
614 # input doesn't contain a pattern which contains actual embedded newlines
615 # at this point!
616
617 regsub -all {\n} ${user_code} { } subst_code
618 set subst_code [uplevel list $subst_code]
619
620 set processed_code ""
621 set patterns ""
622 set expecting_action 0
623 set expecting_arg 0
624 foreach item $user_code subst_item $subst_code {
625 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
626 lappend processed_code $item
627 continue
628 }
629 if { $item == "-indices" || $item == "-re" || $item == "-ex" } {
630 lappend processed_code $item
631 continue
632 }
633 if { $item == "-timeout" } {
634 set expecting_arg 1
635 lappend processed_code $item
636 continue
637 }
638 if { $expecting_arg } {
639 set expecting_arg 0
640 lappend processed_code $item
641 continue
642 }
643 if { $expecting_action } {
644 lappend processed_code "uplevel [list $item]"
645 set expecting_action 0
646 # Cosmetic, no effect on the list.
647 append processed_code "\n"
648 continue
649 }
650 set expecting_action 1
651 lappend processed_code $subst_item
652 if {$patterns != ""} {
653 append patterns "; "
654 }
655 append patterns "\"$subst_item\""
656 }
657
658 # Also purely cosmetic.
659 regsub -all {\r} $patterns {\\r} patterns
660 regsub -all {\n} $patterns {\\n} patterns
661
662 if $verbose>2 then {
663 send_user "Sending \"$command\" to gdb\n"
664 send_user "Looking to match \"$patterns\"\n"
665 send_user "Message is \"$message\"\n"
666 }
667
668 set result -1
669 set string "${command}\n";
670 if { $command != "" } {
671 set multi_line_re "\[\r\n\] *>"
672 while { "$string" != "" } {
673 set foo [string first "\n" "$string"];
674 set len [string length "$string"];
675 if { $foo < [expr $len - 1] } {
676 set str [string range "$string" 0 $foo];
677 if { [send_gdb "$str"] != "" } {
678 global suppress_flag;
679
680 if { ! $suppress_flag } {
681 perror "Couldn't send $command to GDB.";
682 }
683 fail "$message";
684 return $result;
685 }
686 # since we're checking if each line of the multi-line
687 # command are 'accepted' by GDB here,
688 # we need to set -notransfer expect option so that
689 # command output is not lost for pattern matching
690 # - guo
691 gdb_expect 2 {
692 -notransfer -re "$multi_line_re$" { verbose "partial: match" 3 }
693 timeout { verbose "partial: timeout" 3 }
694 }
695 set string [string range "$string" [expr $foo + 1] end];
696 set multi_line_re "$multi_line_re.*\[\r\n\] *>"
697 } else {
698 break;
699 }
700 }
701 if { "$string" != "" } {
702 if { [send_gdb "$string"] != "" } {
703 global suppress_flag;
704
705 if { ! $suppress_flag } {
706 perror "Couldn't send $command to GDB.";
707 }
708 fail "$message";
709 return $result;
710 }
711 }
712 }
713
714 if [target_info exists gdb,timeout] {
715 set tmt [target_info gdb,timeout];
716 } else {
717 if [info exists timeout] {
718 set tmt $timeout;
719 } else {
720 global timeout;
721 if [info exists timeout] {
722 set tmt $timeout;
723 } else {
724 set tmt 60;
725 }
726 }
727 }
728
729 set code {
730 -re ".*A problem internal to GDB has been detected" {
731 fail "$message (GDB internal error)"
732 gdb_internal_error_resync
733 }
734 -re "\\*\\*\\* DOSEXIT code.*" {
735 if { $message != "" } {
736 fail "$message";
737 }
738 gdb_suppress_entire_file "GDB died";
739 set result -1;
740 }
741 }
742 append code $processed_code
743 append code {
744 -re "Ending remote debugging.*$gdb_prompt $" {
745 if ![isnative] then {
746 warning "Can`t communicate to remote target."
747 }
748 gdb_exit
749 gdb_start
750 set result -1
751 }
752 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
753 perror "Undefined command \"$command\"."
754 fail "$message"
755 set result 1
756 }
757 -re "Ambiguous command.*$gdb_prompt $" {
758 perror "\"$command\" is not a unique command name."
759 fail "$message"
760 set result 1
761 }
762 -re "$inferior_exited_re with code \[0-9\]+.*$gdb_prompt $" {
763 if ![string match "" $message] then {
764 set errmsg "$message (the program exited)"
765 } else {
766 set errmsg "$command (the program exited)"
767 }
768 fail "$errmsg"
769 set result -1
770 }
771 -re "$inferior_exited_re normally.*$gdb_prompt $" {
772 if ![string match "" $message] then {
773 set errmsg "$message (the program exited)"
774 } else {
775 set errmsg "$command (the program exited)"
776 }
777 fail "$errmsg"
778 set result -1
779 }
780 -re "The program is not being run.*$gdb_prompt $" {
781 if ![string match "" $message] then {
782 set errmsg "$message (the program is no longer running)"
783 } else {
784 set errmsg "$command (the program is no longer running)"
785 }
786 fail "$errmsg"
787 set result -1
788 }
789 -re "\r\n$gdb_prompt $" {
790 if ![string match "" $message] then {
791 fail "$message"
792 }
793 set result 1
794 }
795 "<return>" {
796 send_gdb "\n"
797 perror "Window too small."
798 fail "$message"
799 set result -1
800 }
801 -re "\\((y or n|y or \\\[n\\\]|\\\[y\\\] or n)\\) " {
802 send_gdb "n\n"
803 gdb_expect -re "$gdb_prompt $"
804 fail "$message (got interactive prompt)"
805 set result -1
806 }
807 -re "\\\[0\\\] cancel\r\n\\\[1\\\] all.*\r\n> $" {
808 send_gdb "0\n"
809 gdb_expect -re "$gdb_prompt $"
810 fail "$message (got breakpoint menu)"
811 set result -1
812 }
813 eof {
814 perror "Process no longer exists"
815 if { $message != "" } {
816 fail "$message"
817 }
818 return -1
819 }
820 full_buffer {
821 perror "internal buffer is full."
822 fail "$message"
823 set result -1
824 }
825 timeout {
826 if ![string match "" $message] then {
827 fail "$message (timeout)"
828 }
829 set result 1
830 }
831 }
832
833 set result 0
834 set code [catch {gdb_expect $tmt $code} string]
835 if {$code == 1} {
836 global errorInfo errorCode;
837 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
838 } elseif {$code > 1} {
839 return -code $code $string
840 }
841 return $result
842 }
843
844 # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
845 # Send a command to gdb; test the result.
846 #
847 # COMMAND is the command to execute, send to GDB with send_gdb. If
848 # this is the null string no command is sent.
849 # PATTERN is the pattern to match for a PASS, and must NOT include
850 # the \r\n sequence immediately before the gdb prompt.
851 # MESSAGE is an optional message to be printed. If this is
852 # omitted, then the pass/fail messages use the command string as the
853 # message. (If this is the empty string, then sometimes we don't
854 # call pass or fail at all; I don't understand this at all.)
855 # QUESTION is a question GDB may ask in response to COMMAND, like
856 # "are you sure?"
857 # RESPONSE is the response to send if QUESTION appears.
858 #
859 # Returns:
860 # 1 if the test failed,
861 # 0 if the test passes,
862 # -1 if there was an internal error.
863 #
864 proc gdb_test { args } {
865 global verbose
866 global gdb_prompt
867 global GDB
868 upvar timeout timeout
869
870 if [llength $args]>2 then {
871 set message [lindex $args 2]
872 } else {
873 set message [lindex $args 0]
874 }
875 set command [lindex $args 0]
876 set pattern [lindex $args 1]
877
878 if [llength $args]==5 {
879 set question_string [lindex $args 3];
880 set response_string [lindex $args 4];
881 } else {
882 set question_string "^FOOBAR$"
883 }
884
885 return [gdb_test_multiple $command $message {
886 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
887 if ![string match "" $message] then {
888 pass "$message"
889 }
890 }
891 -re "(${question_string})$" {
892 send_gdb "$response_string\n";
893 exp_continue;
894 }
895 }]
896 }
897
898 # gdb_test_no_output COMMAND MESSAGE
899 # Send a command to GDB and verify that this command generated no output.
900 #
901 # See gdb_test_multiple for a description of the COMMAND and MESSAGE
902 # parameters. If MESSAGE is ommitted, then COMMAND will be used as
903 # the message. (If MESSAGE is the empty string, then sometimes we do not
904 # call pass or fail at all; I don't understand this at all.)
905
906 proc gdb_test_no_output { args } {
907 global gdb_prompt
908 set command [lindex $args 0]
909 if [llength $args]>1 then {
910 set message [lindex $args 1]
911 } else {
912 set message $command
913 }
914
915 set command_regex [string_to_regexp $command]
916 gdb_test_multiple $command $message {
917 -re "^$command_regex\r\n$gdb_prompt $" {
918 if ![string match "" $message] then {
919 pass "$message"
920 }
921 }
922 }
923 }
924
925 # Send a command and then wait for a sequence of outputs.
926 # This is useful when the sequence is long and contains ".*", a single
927 # regexp to match the entire output can get a timeout much easier.
928 #
929 # COMMAND is the command to send.
930 # TEST_NAME is passed to pass/fail. COMMAND is used if TEST_NAME is "".
931 # EXPECTED_OUTPUT_LIST is a list of regexps of expected output, which are
932 # processed in order, and all must be present in the output.
933 #
934 # It is unnecessary to specify ".*" at the beginning or end of any regexp,
935 # there is an implicit ".*" between each element of EXPECTED_OUTPUT_LIST.
936 # There is also an implicit ".*" between the last regexp and the gdb prompt.
937 #
938 # Like gdb_test and gdb_test_multiple, the output is expected to end with the
939 # gdb prompt, which must not be specified in EXPECTED_OUTPUT_LIST.
940 #
941 # Returns:
942 # 1 if the test failed,
943 # 0 if the test passes,
944 # -1 if there was an internal error.
945
946 proc gdb_test_sequence { command test_name expected_output_list } {
947 global gdb_prompt
948 if { $test_name == "" } {
949 set test_name $command
950 }
951 lappend expected_output_list ""; # implicit ".*" before gdb prompt
952 send_gdb "$command\n"
953 return [gdb_expect_list $test_name "$gdb_prompt $" $expected_output_list]
954 }
955
956 \f
957 # Test that a command gives an error. For pass or fail, return
958 # a 1 to indicate that more tests can proceed. However a timeout
959 # is a serious error, generates a special fail message, and causes
960 # a 0 to be returned to indicate that more tests are likely to fail
961 # as well.
962
963 proc test_print_reject { args } {
964 global gdb_prompt
965 global verbose
966
967 if [llength $args]==2 then {
968 set expectthis [lindex $args 1]
969 } else {
970 set expectthis "should never match this bogus string"
971 }
972 set sendthis [lindex $args 0]
973 if $verbose>2 then {
974 send_user "Sending \"$sendthis\" to gdb\n"
975 send_user "Looking to match \"$expectthis\"\n"
976 }
977 send_gdb "$sendthis\n"
978 #FIXME: Should add timeout as parameter.
979 gdb_expect {
980 -re "A .* in expression.*\\.*$gdb_prompt $" {
981 pass "reject $sendthis"
982 return 1
983 }
984 -re "Invalid syntax in expression.*$gdb_prompt $" {
985 pass "reject $sendthis"
986 return 1
987 }
988 -re "Junk after end of expression.*$gdb_prompt $" {
989 pass "reject $sendthis"
990 return 1
991 }
992 -re "Invalid number.*$gdb_prompt $" {
993 pass "reject $sendthis"
994 return 1
995 }
996 -re "Invalid character constant.*$gdb_prompt $" {
997 pass "reject $sendthis"
998 return 1
999 }
1000 -re "No symbol table is loaded.*$gdb_prompt $" {
1001 pass "reject $sendthis"
1002 return 1
1003 }
1004 -re "No symbol .* in current context.*$gdb_prompt $" {
1005 pass "reject $sendthis"
1006 return 1
1007 }
1008 -re "Unmatched single quote.*$gdb_prompt $" {
1009 pass "reject $sendthis"
1010 return 1
1011 }
1012 -re "A character constant must contain at least one character.*$gdb_prompt $" {
1013 pass "reject $sendthis"
1014 return 1
1015 }
1016 -re "$expectthis.*$gdb_prompt $" {
1017 pass "reject $sendthis"
1018 return 1
1019 }
1020 -re ".*$gdb_prompt $" {
1021 fail "reject $sendthis"
1022 return 1
1023 }
1024 default {
1025 fail "reject $sendthis (eof or timeout)"
1026 return 0
1027 }
1028 }
1029 }
1030 \f
1031 # Given an input string, adds backslashes as needed to create a
1032 # regexp that will match the string.
1033
1034 proc string_to_regexp {str} {
1035 set result $str
1036 regsub -all {[]*+.|()^$\[\\]} $str {\\&} result
1037 return $result
1038 }
1039
1040 # Same as gdb_test, but the second parameter is not a regexp,
1041 # but a string that must match exactly.
1042
1043 proc gdb_test_exact { args } {
1044 upvar timeout timeout
1045
1046 set command [lindex $args 0]
1047
1048 # This applies a special meaning to a null string pattern. Without
1049 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
1050 # messages from commands that should have no output except a new
1051 # prompt. With this, only results of a null string will match a null
1052 # string pattern.
1053
1054 set pattern [lindex $args 1]
1055 if [string match $pattern ""] {
1056 set pattern [string_to_regexp [lindex $args 0]]
1057 } else {
1058 set pattern [string_to_regexp [lindex $args 1]]
1059 }
1060
1061 # It is most natural to write the pattern argument with only
1062 # embedded \n's, especially if you are trying to avoid Tcl quoting
1063 # problems. But gdb_expect really wants to see \r\n in patterns. So
1064 # transform the pattern here. First transform \r\n back to \n, in
1065 # case some users of gdb_test_exact already do the right thing.
1066 regsub -all "\r\n" $pattern "\n" pattern
1067 regsub -all "\n" $pattern "\r\n" pattern
1068 if [llength $args]==3 then {
1069 set message [lindex $args 2]
1070 } else {
1071 set message $command
1072 }
1073
1074 return [gdb_test $command $pattern $message]
1075 }
1076
1077 # Wrapper around gdb_test_multiple that looks for a list of expected
1078 # output elements, but which can appear in any order.
1079 # CMD is the gdb command.
1080 # NAME is the name of the test.
1081 # ELM_FIND_REGEXP specifies how to partition the output into elements to
1082 # compare.
1083 # ELM_EXTRACT_REGEXP specifies the part of ELM_FIND_REGEXP to compare.
1084 # RESULT_MATCH_LIST is a list of exact matches for each expected element.
1085 # All elements of RESULT_MATCH_LIST must appear for the test to pass.
1086 #
1087 # A typical use of ELM_FIND_REGEXP/ELM_EXTRACT_REGEXP is to extract one line
1088 # of text per element and then strip trailing \r\n's.
1089 # Example:
1090 # gdb_test_list_exact "foo" "bar" \
1091 # "\[^\r\n\]+\[\r\n\]+" \
1092 # "\[^\r\n\]+" \
1093 # { \
1094 # {expected result 1} \
1095 # {expected result 2} \
1096 # }
1097
1098 proc gdb_test_list_exact { cmd name elm_find_regexp elm_extract_regexp result_match_list } {
1099 global gdb_prompt
1100
1101 set matches [lsort $result_match_list]
1102 set seen {}
1103 gdb_test_multiple $cmd $name {
1104 "$cmd\[\r\n\]" { exp_continue }
1105 -re $elm_find_regexp {
1106 set str $expect_out(0,string)
1107 verbose -log "seen: $str" 3
1108 regexp -- $elm_extract_regexp $str elm_seen
1109 verbose -log "extracted: $elm_seen" 3
1110 lappend seen $elm_seen
1111 exp_continue
1112 }
1113 -re "$gdb_prompt $" {
1114 set failed ""
1115 foreach got [lsort $seen] have $matches {
1116 if {![string equal $got $have]} {
1117 set failed $have
1118 break
1119 }
1120 }
1121 if {[string length $failed] != 0} {
1122 fail "$name ($failed not found)"
1123 } else {
1124 pass $name
1125 }
1126 }
1127 }
1128 }
1129 \f
1130 proc gdb_reinitialize_dir { subdir } {
1131 global gdb_prompt
1132
1133 if [is_remote host] {
1134 return "";
1135 }
1136 send_gdb "dir\n"
1137 gdb_expect 60 {
1138 -re "Reinitialize source path to empty.*y or n. " {
1139 send_gdb "y\n"
1140 gdb_expect 60 {
1141 -re "Source directories searched.*$gdb_prompt $" {
1142 send_gdb "dir $subdir\n"
1143 gdb_expect 60 {
1144 -re "Source directories searched.*$gdb_prompt $" {
1145 verbose "Dir set to $subdir"
1146 }
1147 -re "$gdb_prompt $" {
1148 perror "Dir \"$subdir\" failed."
1149 }
1150 }
1151 }
1152 -re "$gdb_prompt $" {
1153 perror "Dir \"$subdir\" failed."
1154 }
1155 }
1156 }
1157 -re "$gdb_prompt $" {
1158 perror "Dir \"$subdir\" failed."
1159 }
1160 }
1161 }
1162
1163 #
1164 # gdb_exit -- exit the GDB, killing the target program if necessary
1165 #
1166 proc default_gdb_exit {} {
1167 global GDB
1168 global INTERNAL_GDBFLAGS GDBFLAGS
1169 global verbose
1170 global gdb_spawn_id;
1171
1172 gdb_stop_suppressing_tests;
1173
1174 if ![info exists gdb_spawn_id] {
1175 return;
1176 }
1177
1178 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1179
1180 if { [is_remote host] && [board_info host exists fileid] } {
1181 send_gdb "quit\n";
1182 gdb_expect 10 {
1183 -re "y or n" {
1184 send_gdb "y\n";
1185 exp_continue;
1186 }
1187 -re "DOSEXIT code" { }
1188 default { }
1189 }
1190 }
1191
1192 if ![is_remote host] {
1193 remote_close host;
1194 }
1195 unset gdb_spawn_id
1196 }
1197
1198 # Load a file into the debugger.
1199 # The return value is 0 for success, -1 for failure.
1200 #
1201 # This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1202 # to one of these values:
1203 #
1204 # debug file was loaded successfully and has debug information
1205 # nodebug file was loaded successfully and has no debug information
1206 # fail file was not loaded
1207 #
1208 # I tried returning this information as part of the return value,
1209 # but ran into a mess because of the many re-implementations of
1210 # gdb_load in config/*.exp.
1211 #
1212 # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
1213 # this if they can get more information set.
1214
1215 proc gdb_file_cmd { arg } {
1216 global gdb_prompt
1217 global verbose
1218 global GDB
1219 global last_loaded_file
1220
1221 # Save this for the benefit of gdbserver-support.exp.
1222 set last_loaded_file $arg
1223
1224 # Set whether debug info was found.
1225 # Default to "fail".
1226 global gdb_file_cmd_debug_info
1227 set gdb_file_cmd_debug_info "fail"
1228
1229 if [is_remote host] {
1230 set arg [remote_download host $arg]
1231 if { $arg == "" } {
1232 perror "download failed"
1233 return -1
1234 }
1235 }
1236
1237 # The file command used to kill the remote target. For the benefit
1238 # of the testsuite, preserve this behavior.
1239 send_gdb "kill\n"
1240 gdb_expect 120 {
1241 -re "Kill the program being debugged. .y or n. $" {
1242 send_gdb "y\n"
1243 verbose "\t\tKilling previous program being debugged"
1244 exp_continue
1245 }
1246 -re "$gdb_prompt $" {
1247 # OK.
1248 }
1249 }
1250
1251 send_gdb "file $arg\n"
1252 gdb_expect 120 {
1253 -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" {
1254 verbose "\t\tLoaded $arg into $GDB with no debugging symbols"
1255 set gdb_file_cmd_debug_info "nodebug"
1256 return 0
1257 }
1258 -re "Reading symbols from.*done.*$gdb_prompt $" {
1259 verbose "\t\tLoaded $arg into $GDB"
1260 set gdb_file_cmd_debug_info "debug"
1261 return 0
1262 }
1263 -re "Load new symbol table from \".*\".*y or n. $" {
1264 send_gdb "y\n"
1265 gdb_expect 120 {
1266 -re "Reading symbols from.*done.*$gdb_prompt $" {
1267 verbose "\t\tLoaded $arg with new symbol table into $GDB"
1268 set gdb_file_cmd_debug_info "debug"
1269 return 0
1270 }
1271 timeout {
1272 perror "Couldn't load $arg, other program already loaded (timeout)."
1273 return -1
1274 }
1275 eof {
1276 perror "Couldn't load $arg, other program already loaded (eof)."
1277 return -1
1278 }
1279 }
1280 }
1281 -re "No such file or directory.*$gdb_prompt $" {
1282 perror "($arg) No such file or directory"
1283 return -1
1284 }
1285 -re "A problem internal to GDB has been detected" {
1286 fail "($arg) GDB internal error"
1287 gdb_internal_error_resync
1288 return -1
1289 }
1290 -re "$gdb_prompt $" {
1291 perror "Couldn't load $arg into $GDB."
1292 return -1
1293 }
1294 timeout {
1295 perror "Couldn't load $arg into $GDB (timeout)."
1296 return -1
1297 }
1298 eof {
1299 # This is an attempt to detect a core dump, but seems not to
1300 # work. Perhaps we need to match .* followed by eof, in which
1301 # gdb_expect does not seem to have a way to do that.
1302 perror "Couldn't load $arg into $GDB (eof)."
1303 return -1
1304 }
1305 }
1306 }
1307
1308 #
1309 # start gdb -- start gdb running, default procedure
1310 #
1311 # When running over NFS, particularly if running many simultaneous
1312 # tests on different hosts all using the same server, things can
1313 # get really slow. Give gdb at least 3 minutes to start up.
1314 #
1315 proc default_gdb_start { } {
1316 global verbose use_gdb_stub
1317 global GDB
1318 global INTERNAL_GDBFLAGS GDBFLAGS
1319 global gdb_prompt
1320 global timeout
1321 global gdb_spawn_id;
1322
1323 gdb_stop_suppressing_tests;
1324
1325 # Set the default value, it may be overriden later by specific testfile.
1326 #
1327 # Use `set_board_info use_gdb_stub' for the board file to flag the inferior
1328 # is already started after connecting and run/attach are not supported.
1329 # This is used for the "remote" protocol. After GDB starts you should
1330 # check global $use_gdb_stub instead of the board as the testfile may force
1331 # a specific different target protocol itself.
1332 set use_gdb_stub [target_info exists use_gdb_stub]
1333
1334 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1335
1336 if [info exists gdb_spawn_id] {
1337 return 0;
1338 }
1339
1340 if ![is_remote host] {
1341 if { [which $GDB] == 0 } then {
1342 perror "$GDB does not exist."
1343 exit 1
1344 }
1345 }
1346 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]"];
1347 if { $res < 0 || $res == "" } {
1348 perror "Spawning $GDB failed."
1349 return 1;
1350 }
1351 gdb_expect 360 {
1352 -re "\[\r\n\]$gdb_prompt $" {
1353 verbose "GDB initialized."
1354 }
1355 -re "$gdb_prompt $" {
1356 perror "GDB never initialized."
1357 return -1
1358 }
1359 timeout {
1360 perror "(timeout) GDB never initialized after 10 seconds."
1361 remote_close host;
1362 return -1
1363 }
1364 }
1365 set gdb_spawn_id -1;
1366 # force the height to "unlimited", so no pagers get used
1367
1368 send_gdb "set height 0\n"
1369 gdb_expect 10 {
1370 -re "$gdb_prompt $" {
1371 verbose "Setting height to 0." 2
1372 }
1373 timeout {
1374 warning "Couldn't set the height to 0"
1375 }
1376 }
1377 # force the width to "unlimited", so no wraparound occurs
1378 send_gdb "set width 0\n"
1379 gdb_expect 10 {
1380 -re "$gdb_prompt $" {
1381 verbose "Setting width to 0." 2
1382 }
1383 timeout {
1384 warning "Couldn't set the width to 0."
1385 }
1386 }
1387 return 0;
1388 }
1389
1390 # Examine the output of compilation to determine whether compilation
1391 # failed or not. If it failed determine whether it is due to missing
1392 # compiler or due to compiler error. Report pass, fail or unsupported
1393 # as appropriate
1394
1395 proc gdb_compile_test {src output} {
1396 if { $output == "" } {
1397 pass "compilation [file tail $src]"
1398 } elseif { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output] } {
1399 unsupported "compilation [file tail $src]"
1400 } elseif { [regexp {.*: command not found[\r|\n]*$} $output] } {
1401 unsupported "compilation [file tail $src]"
1402 } elseif { [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } {
1403 unsupported "compilation [file tail $src]"
1404 } else {
1405 verbose -log "compilation failed: $output" 2
1406 fail "compilation [file tail $src]"
1407 }
1408 }
1409
1410 # Return a 1 for configurations for which we don't even want to try to
1411 # test C++.
1412
1413 proc skip_cplus_tests {} {
1414 if { [istarget "h8300-*-*"] } {
1415 return 1
1416 }
1417
1418 # The C++ IO streams are too large for HC11/HC12 and are thus not
1419 # available. The gdb C++ tests use them and don't compile.
1420 if { [istarget "m6811-*-*"] } {
1421 return 1
1422 }
1423 if { [istarget "m6812-*-*"] } {
1424 return 1
1425 }
1426 return 0
1427 }
1428
1429 # Return a 1 for configurations for which don't have both C++ and the STL.
1430
1431 proc skip_stl_tests {} {
1432 # Symbian supports the C++ language, but the STL is missing
1433 # (both headers and libraries).
1434 if { [istarget "arm*-*-symbianelf*"] } {
1435 return 1
1436 }
1437
1438 return [skip_cplus_tests]
1439 }
1440
1441 # Return a 1 if I don't even want to try to test FORTRAN.
1442
1443 proc skip_fortran_tests {} {
1444 return 0
1445 }
1446
1447 # Return a 1 if I don't even want to try to test ada.
1448
1449 proc skip_ada_tests {} {
1450 return 0
1451 }
1452
1453 # Return a 1 if I don't even want to try to test GO.
1454
1455 proc skip_go_tests {} {
1456 return 0
1457 }
1458
1459 # Return a 1 if I don't even want to try to test java.
1460
1461 proc skip_java_tests {} {
1462 return 0
1463 }
1464
1465 # Return a 1 for configurations that do not support Python scripting.
1466
1467 proc skip_python_tests {} {
1468 global gdb_prompt
1469 gdb_test_multiple "python print 'test'" "verify python support" {
1470 -re "not supported.*$gdb_prompt $" {
1471 unsupported "Python support is disabled."
1472 return 1
1473 }
1474 -re "$gdb_prompt $" {}
1475 }
1476
1477 return 0
1478 }
1479
1480 # Return a 1 if we should skip shared library tests.
1481
1482 proc skip_shlib_tests {} {
1483 # Run the shared library tests on native systems.
1484 if {[isnative]} {
1485 return 0
1486 }
1487
1488 # An abbreviated list of remote targets where we should be able to
1489 # run shared library tests.
1490 if {([istarget *-*-linux*]
1491 || [istarget *-*-*bsd*]
1492 || [istarget *-*-solaris2*]
1493 || [istarget arm*-*-symbianelf*]
1494 || [istarget *-*-mingw*]
1495 || [istarget *-*-cygwin*]
1496 || [istarget *-*-pe*])} {
1497 return 0
1498 }
1499
1500 return 1
1501 }
1502
1503 # Test files shall make sure all the test result lines in gdb.sum are
1504 # unique in a test run, so that comparing the gdb.sum files of two
1505 # test runs gives correct results. Test files that exercise
1506 # variations of the same tests more than once, shall prefix the
1507 # different test invocations with different identifying strings in
1508 # order to make them unique.
1509 #
1510 # About test prefixes:
1511 #
1512 # $pf_prefix is the string that dejagnu prints after the result (FAIL,
1513 # PASS, etc.), and before the test message/name in gdb.sum. E.g., the
1514 # underlined substring in
1515 #
1516 # PASS: gdb.base/mytest.exp: some test
1517 # ^^^^^^^^^^^^^^^^^^^^
1518 #
1519 # is $pf_prefix.
1520 #
1521 # The easiest way to adjust the test prefix is to append a test
1522 # variation prefix to the $pf_prefix, using the with_test_prefix
1523 # procedure. E.g.,
1524 #
1525 # proc do_tests {} {
1526 # gdb_test ... ... "test foo"
1527 # gdb_test ... ... "test bar"
1528 #
1529 # with_test_prefix "subvariation a" {
1530 # gdb_test ... ... "test x"
1531 # }
1532 #
1533 # with_test_prefix "subvariation b" {
1534 # gdb_test ... ... "test x"
1535 # }
1536 # }
1537 #
1538 # with_test_prefix "variation1" {
1539 # ...do setup for variation 1...
1540 # do_tests
1541 # }
1542 #
1543 # with_test_prefix "variation2" {
1544 # ...do setup for variation 2...
1545 # do_tests
1546 # }
1547 #
1548 # Results in:
1549 #
1550 # PASS: gdb.base/mytest.exp: variation1: test foo
1551 # PASS: gdb.base/mytest.exp: variation1: test bar
1552 # PASS: gdb.base/mytest.exp: variation1: subvariation a: test x
1553 # PASS: gdb.base/mytest.exp: variation1: subvariation b: test x
1554 # PASS: gdb.base/mytest.exp: variation2: test foo
1555 # PASS: gdb.base/mytest.exp: variation2: test bar
1556 # PASS: gdb.base/mytest.exp: variation2: subvariation a: test x
1557 # PASS: gdb.base/mytest.exp: variation2: subvariation b: test x
1558 #
1559 # If for some reason more flexibility is necessary, one can also
1560 # manipulate the pf_prefix global directly, treating it as a string.
1561 # E.g.,
1562 #
1563 # global pf_prefix
1564 # set saved_pf_prefix
1565 # append pf_prefix "${foo}: bar"
1566 # ... actual tests ...
1567 # set pf_prefix $saved_pf_prefix
1568 #
1569
1570 # Run BODY in the context of the caller, with the current test prefix
1571 # (pf_prefix) appended with one space, then PREFIX, and then a colon.
1572 # Returns the result of BODY.
1573 #
1574 proc with_test_prefix { prefix body } {
1575 global pf_prefix
1576
1577 set saved $pf_prefix
1578 append pf_prefix " " $prefix ":"
1579 set code [catch {uplevel 1 $body} result]
1580 set pf_prefix $saved
1581
1582 if {$code == 1} {
1583 global errorInfo errorCode
1584 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
1585 } else {
1586 return -code $code $result
1587 }
1588 }
1589
1590 # Return 1 if _Complex types are supported, otherwise, return 0.
1591
1592 proc support_complex_tests {} {
1593 global support_complex_tests_saved
1594
1595 # Use the cached value, if it exists.
1596 if [info exists support_complex_tests_saved] {
1597 verbose "returning saved $support_complex_tests_saved" 2
1598 return $support_complex_tests_saved
1599 }
1600
1601 # Set up, compile, and execute a test program containing _Complex types.
1602 # Include the current process ID in the file names to prevent conflicts
1603 # with invocations for multiple testsuites.
1604 set src complex[pid].c
1605 set exe complex[pid].x
1606
1607 set f [open $src "w"]
1608 puts $f "int main() {"
1609 puts $f "_Complex float cf;"
1610 puts $f "_Complex double cd;"
1611 puts $f "_Complex long double cld;"
1612 puts $f " return 0; }"
1613 close $f
1614
1615 verbose "compiling testfile $src" 2
1616 set compile_flags {debug nowarnings quiet}
1617 set lines [gdb_compile $src $exe executable $compile_flags]
1618 file delete $src
1619 file delete $exe
1620
1621 if ![string match "" $lines] then {
1622 verbose "testfile compilation failed, returning 0" 2
1623 set support_complex_tests_saved 0
1624 } else {
1625 set support_complex_tests_saved 1
1626 }
1627
1628 return $support_complex_tests_saved
1629 }
1630
1631 # Return 1 if target hardware or OS supports single stepping to signal
1632 # handler, otherwise, return 0.
1633
1634 proc can_single_step_to_signal_handler {} {
1635
1636 # Targets don't have hardware single step. On these targets, when
1637 # a signal is delivered during software single step, gdb is unable
1638 # to determine the next instruction addresses, because start of signal
1639 # handler is one of them.
1640 if { [istarget "arm*-*-*"] || [istarget "mips*-*-*"]
1641 || [istarget "tic6x-*-*"] || [istarget "sparc*-*-linux*"] } {
1642 return 0
1643 }
1644
1645 return 1
1646 }
1647
1648 # Return 1 if target supports process record, otherwise return 0.
1649
1650 proc supports_process_record {} {
1651
1652 if [target_info exists gdb,use_precord] {
1653 return [target_info gdb,use_precord]
1654 }
1655
1656 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"] } {
1657 return 1
1658 }
1659
1660 return 0
1661 }
1662
1663 # Return 1 if target supports reverse debugging, otherwise return 0.
1664
1665 proc supports_reverse {} {
1666
1667 if [target_info exists gdb,can_reverse] {
1668 return [target_info gdb,can_reverse]
1669 }
1670
1671 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"] } {
1672 return 1
1673 }
1674
1675 return 0
1676 }
1677
1678 # Return 1 if target is ILP32.
1679 # This cannot be decided simply from looking at the target string,
1680 # as it might depend on externally passed compiler options like -m64.
1681 proc is_ilp32_target {} {
1682 global is_ilp32_target_saved
1683
1684 # Use the cached value, if it exists. Cache value per "board" to handle
1685 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1686 set me "is_ilp32_target"
1687 set board [target_info name]
1688 if [info exists is_ilp32_target_saved($board)] {
1689 verbose "$me: returning saved $is_ilp32_target_saved($board)" 2
1690 return $is_ilp32_target_saved($board)
1691 }
1692
1693
1694 set src ilp32[pid].c
1695 set obj ilp32[pid].o
1696
1697 set f [open $src "w"]
1698 puts $f "int dummy\[sizeof (int) == 4"
1699 puts $f " && sizeof (void *) == 4"
1700 puts $f " && sizeof (long) == 4 ? 1 : -1\];"
1701 close $f
1702
1703 verbose "$me: compiling testfile $src" 2
1704 set lines [gdb_compile $src $obj object {quiet}]
1705 file delete $src
1706 file delete $obj
1707
1708 if ![string match "" $lines] then {
1709 verbose "$me: testfile compilation failed, returning 0" 2
1710 return [set is_ilp32_target_saved($board) 0]
1711 }
1712
1713 verbose "$me: returning 1" 2
1714 return [set is_ilp32_target_saved($board) 1]
1715 }
1716
1717 # Return 1 if target is LP64.
1718 # This cannot be decided simply from looking at the target string,
1719 # as it might depend on externally passed compiler options like -m64.
1720 proc is_lp64_target {} {
1721 global is_lp64_target_saved
1722
1723 # Use the cached value, if it exists. Cache value per "board" to handle
1724 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1725 set me "is_lp64_target"
1726 set board [target_info name]
1727 if [info exists is_lp64_target_saved($board)] {
1728 verbose "$me: returning saved $is_lp64_target_saved($board)" 2
1729 return $is_lp64_target_saved($board)
1730 }
1731
1732 set src lp64[pid].c
1733 set obj lp64[pid].o
1734
1735 set f [open $src "w"]
1736 puts $f "int dummy\[sizeof (int) == 4"
1737 puts $f " && sizeof (void *) == 8"
1738 puts $f " && sizeof (long) == 8 ? 1 : -1\];"
1739 close $f
1740
1741 verbose "$me: compiling testfile $src" 2
1742 set lines [gdb_compile $src $obj object {quiet}]
1743 file delete $src
1744 file delete $obj
1745
1746 if ![string match "" $lines] then {
1747 verbose "$me: testfile compilation failed, returning 0" 2
1748 return [set is_lp64_target_saved($board) 0]
1749 }
1750
1751 verbose "$me: returning 1" 2
1752 return [set is_lp64_target_saved($board) 1]
1753 }
1754
1755 # Return 1 if target has x86_64 registers - either amd64 or x32.
1756 # x32 target identifies as x86_64-*-linux*, therefore it cannot be determined
1757 # just from the target string.
1758 proc is_amd64_regs_target {} {
1759 global is_amd64_regs_target_saved
1760
1761 if {![istarget "x86_64-*-*"] && ![istarget "i?86-*"]} {
1762 return 0
1763 }
1764
1765 # Use the cached value, if it exists. Cache value per "board" to handle
1766 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1767 set me "is_amd64_regs_target"
1768 set board [target_info name]
1769 if [info exists is_amd64_regs_target_saved($board)] {
1770 verbose "$me: returning saved $is_amd64_regs_target_saved($board)" 2
1771 return $is_amd64_regs_target_saved($board)
1772 }
1773
1774 set src reg64[pid].s
1775 set obj reg64[pid].o
1776
1777 set f [open $src "w"]
1778 foreach reg \
1779 {rax rbx rcx rdx rsi rdi rbp rsp r8 r9 r10 r11 r12 r13 r14 r15} {
1780 puts $f "\tincq %$reg"
1781 }
1782 close $f
1783
1784 verbose "$me: compiling testfile $src" 2
1785 set lines [gdb_compile $src $obj object {quiet}]
1786 file delete $src
1787 file delete $obj
1788
1789 if ![string match "" $lines] then {
1790 verbose "$me: testfile compilation failed, returning 0" 2
1791 return [set is_amd64_regs_target_saved($board) 0]
1792 }
1793
1794 verbose "$me: returning 1" 2
1795 return [set is_amd64_regs_target_saved($board) 1]
1796 }
1797
1798 # Return 1 if this target is an x86 or x86-64 with -m32.
1799 proc is_x86_like_target {} {
1800 if {![istarget "x86_64-*-*"] && ![istarget i?86-*]} {
1801 return 0
1802 }
1803 return [expr [is_ilp32_target] && ![is_amd64_regs_target]]
1804 }
1805
1806 # Return 1 if displaced stepping is supported on target, otherwise, return 0.
1807 proc support_displaced_stepping {} {
1808
1809 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"]
1810 || [istarget "arm*-*-linux*"] || [istarget "powerpc-*-linux*"]
1811 || [istarget "powerpc64-*-linux*"] || [istarget "s390*-*-*"] } {
1812 return 1
1813 }
1814
1815 return 0
1816 }
1817
1818 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1819 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1820
1821 proc skip_altivec_tests {} {
1822 global skip_vmx_tests_saved
1823 global srcdir subdir gdb_prompt inferior_exited_re
1824
1825 # Use the cached value, if it exists.
1826 set me "skip_altivec_tests"
1827 if [info exists skip_vmx_tests_saved] {
1828 verbose "$me: returning saved $skip_vmx_tests_saved" 2
1829 return $skip_vmx_tests_saved
1830 }
1831
1832 # Some simulators are known to not support VMX instructions.
1833 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1834 verbose "$me: target known to not support VMX, returning 1" 2
1835 return [set skip_vmx_tests_saved 1]
1836 }
1837
1838 # Make sure we have a compiler that understands altivec.
1839 set compile_flags {debug nowarnings}
1840 if [get_compiler_info] {
1841 warning "Could not get compiler info"
1842 return 1
1843 }
1844 if [test_compiler_info gcc*] {
1845 set compile_flags "$compile_flags additional_flags=-maltivec"
1846 } elseif [test_compiler_info xlc*] {
1847 set compile_flags "$compile_flags additional_flags=-qaltivec"
1848 } else {
1849 verbose "Could not compile with altivec support, returning 1" 2
1850 return 1
1851 }
1852
1853 # Set up, compile, and execute a test program containing VMX instructions.
1854 # Include the current process ID in the file names to prevent conflicts
1855 # with invocations for multiple testsuites.
1856 set src vmx[pid].c
1857 set exe vmx[pid].x
1858
1859 set f [open $src "w"]
1860 puts $f "int main() {"
1861 puts $f "#ifdef __MACH__"
1862 puts $f " asm volatile (\"vor v0,v0,v0\");"
1863 puts $f "#else"
1864 puts $f " asm volatile (\"vor 0,0,0\");"
1865 puts $f "#endif"
1866 puts $f " return 0; }"
1867 close $f
1868
1869 verbose "$me: compiling testfile $src" 2
1870 set lines [gdb_compile $src $exe executable $compile_flags]
1871 file delete $src
1872
1873 if ![string match "" $lines] then {
1874 verbose "$me: testfile compilation failed, returning 1" 2
1875 return [set skip_vmx_tests_saved 1]
1876 }
1877
1878 # No error message, compilation succeeded so now run it via gdb.
1879
1880 gdb_exit
1881 gdb_start
1882 gdb_reinitialize_dir $srcdir/$subdir
1883 gdb_load "$exe"
1884 gdb_run_cmd
1885 gdb_expect {
1886 -re ".*Illegal instruction.*${gdb_prompt} $" {
1887 verbose -log "\n$me altivec hardware not detected"
1888 set skip_vmx_tests_saved 1
1889 }
1890 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
1891 verbose -log "\n$me: altivec hardware detected"
1892 set skip_vmx_tests_saved 0
1893 }
1894 default {
1895 warning "\n$me: default case taken"
1896 set skip_vmx_tests_saved 1
1897 }
1898 }
1899 gdb_exit
1900 remote_file build delete $exe
1901
1902 verbose "$me: returning $skip_vmx_tests_saved" 2
1903 return $skip_vmx_tests_saved
1904 }
1905
1906 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1907 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1908
1909 proc skip_vsx_tests {} {
1910 global skip_vsx_tests_saved
1911 global srcdir subdir gdb_prompt inferior_exited_re
1912
1913 # Use the cached value, if it exists.
1914 set me "skip_vsx_tests"
1915 if [info exists skip_vsx_tests_saved] {
1916 verbose "$me: returning saved $skip_vsx_tests_saved" 2
1917 return $skip_vsx_tests_saved
1918 }
1919
1920 # Some simulators are known to not support Altivec instructions, so
1921 # they won't support VSX instructions as well.
1922 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1923 verbose "$me: target known to not support VSX, returning 1" 2
1924 return [set skip_vsx_tests_saved 1]
1925 }
1926
1927 # Make sure we have a compiler that understands altivec.
1928 set compile_flags {debug nowarnings quiet}
1929 if [get_compiler_info] {
1930 warning "Could not get compiler info"
1931 return 1
1932 }
1933 if [test_compiler_info gcc*] {
1934 set compile_flags "$compile_flags additional_flags=-mvsx"
1935 } elseif [test_compiler_info xlc*] {
1936 set compile_flags "$compile_flags additional_flags=-qasm=gcc"
1937 } else {
1938 verbose "Could not compile with vsx support, returning 1" 2
1939 return 1
1940 }
1941
1942 set src vsx[pid].c
1943 set exe vsx[pid].x
1944
1945 set f [open $src "w"]
1946 puts $f "int main() {"
1947 puts $f " double a\[2\] = { 1.0, 2.0 };"
1948 puts $f "#ifdef __MACH__"
1949 puts $f " asm volatile (\"lxvd2x v0,v0,%\[addr\]\" : : \[addr\] \"r\" (a));"
1950 puts $f "#else"
1951 puts $f " asm volatile (\"lxvd2x 0,0,%\[addr\]\" : : \[addr\] \"r\" (a));"
1952 puts $f "#endif"
1953 puts $f " return 0; }"
1954 close $f
1955
1956 verbose "$me: compiling testfile $src" 2
1957 set lines [gdb_compile $src $exe executable $compile_flags]
1958 file delete $src
1959
1960 if ![string match "" $lines] then {
1961 verbose "$me: testfile compilation failed, returning 1" 2
1962 return [set skip_vsx_tests_saved 1]
1963 }
1964
1965 # No error message, compilation succeeded so now run it via gdb.
1966
1967 gdb_exit
1968 gdb_start
1969 gdb_reinitialize_dir $srcdir/$subdir
1970 gdb_load "$exe"
1971 gdb_run_cmd
1972 gdb_expect {
1973 -re ".*Illegal instruction.*${gdb_prompt} $" {
1974 verbose -log "\n$me VSX hardware not detected"
1975 set skip_vsx_tests_saved 1
1976 }
1977 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
1978 verbose -log "\n$me: VSX hardware detected"
1979 set skip_vsx_tests_saved 0
1980 }
1981 default {
1982 warning "\n$me: default case taken"
1983 set skip_vsx_tests_saved 1
1984 }
1985 }
1986 gdb_exit
1987 remote_file build delete $exe
1988
1989 verbose "$me: returning $skip_vsx_tests_saved" 2
1990 return $skip_vsx_tests_saved
1991 }
1992
1993 # Skip all the tests in the file if you are not on an hppa running
1994 # hpux target.
1995
1996 proc skip_hp_tests {} {
1997 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
1998 verbose "Skip hp tests is $skip_hp"
1999 return $skip_hp
2000 }
2001
2002 # Return whether we should skip tests for showing inlined functions in
2003 # backtraces. Requires get_compiler_info and get_debug_format.
2004
2005 proc skip_inline_frame_tests {} {
2006 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
2007 if { ! [test_debug_format "DWARF 2"] } {
2008 return 1
2009 }
2010
2011 # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line.
2012 if { ([test_compiler_info "gcc-2-*"]
2013 || [test_compiler_info "gcc-3-*"]
2014 || [test_compiler_info "gcc-4-0-*"]) } {
2015 return 1
2016 }
2017
2018 return 0
2019 }
2020
2021 # Return whether we should skip tests for showing variables from
2022 # inlined functions. Requires get_compiler_info and get_debug_format.
2023
2024 proc skip_inline_var_tests {} {
2025 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
2026 if { ! [test_debug_format "DWARF 2"] } {
2027 return 1
2028 }
2029
2030 return 0
2031 }
2032
2033 # Return a 1 if we should skip tests that require hardware breakpoints
2034
2035 proc skip_hw_breakpoint_tests {} {
2036 # Skip tests if requested by the board (note that no_hardware_watchpoints
2037 # disables both watchpoints and breakpoints)
2038 if { [target_info exists gdb,no_hardware_watchpoints]} {
2039 return 1
2040 }
2041
2042 # These targets support hardware breakpoints natively
2043 if { [istarget "i?86-*-*"]
2044 || [istarget "x86_64-*-*"]
2045 || [istarget "ia64-*-*"]
2046 || [istarget "arm*-*-*"]} {
2047 return 0
2048 }
2049
2050 return 1
2051 }
2052
2053 # Return a 1 if we should skip tests that require hardware watchpoints
2054
2055 proc skip_hw_watchpoint_tests {} {
2056 # Skip tests if requested by the board
2057 if { [target_info exists gdb,no_hardware_watchpoints]} {
2058 return 1
2059 }
2060
2061 # These targets support hardware watchpoints natively
2062 if { [istarget "i?86-*-*"]
2063 || [istarget "x86_64-*-*"]
2064 || [istarget "ia64-*-*"]
2065 || [istarget "arm*-*-*"]
2066 || [istarget "powerpc*-*-linux*"]
2067 || [istarget "s390*-*-*"] } {
2068 return 0
2069 }
2070
2071 return 1
2072 }
2073
2074 # Return a 1 if we should skip tests that require *multiple* hardware
2075 # watchpoints to be active at the same time
2076
2077 proc skip_hw_watchpoint_multi_tests {} {
2078 if { [skip_hw_watchpoint_tests] } {
2079 return 1
2080 }
2081
2082 # These targets support just a single hardware watchpoint
2083 if { [istarget "arm*-*-*"]
2084 || [istarget "powerpc*-*-linux*"] } {
2085 return 1
2086 }
2087
2088 return 0
2089 }
2090
2091 # Return a 1 if we should skip tests that require read/access watchpoints
2092
2093 proc skip_hw_watchpoint_access_tests {} {
2094 if { [skip_hw_watchpoint_tests] } {
2095 return 1
2096 }
2097
2098 # These targets support just write watchpoints
2099 if { [istarget "s390*-*-*"] } {
2100 return 1
2101 }
2102
2103 return 0
2104 }
2105
2106 # Return 1 if we should skip tests that require the runtime unwinder
2107 # hook. This must be invoked while gdb is running, after shared
2108 # libraries have been loaded. This is needed because otherwise a
2109 # shared libgcc won't be visible.
2110
2111 proc skip_unwinder_tests {} {
2112 global gdb_prompt
2113
2114 set ok 0
2115 gdb_test_multiple "print _Unwind_DebugHook" "check for unwinder hook" {
2116 -re "= .*no debug info.*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
2117 }
2118 -re "= .*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
2119 set ok 1
2120 }
2121 -re "No symbol .* in current context.\r\n$gdb_prompt $" {
2122 }
2123 }
2124 if {!$ok} {
2125 gdb_test_multiple "info probe" "check for stap probe in unwinder" {
2126 -re ".*libgcc.*unwind.*\r\n$gdb_prompt $" {
2127 set ok 1
2128 }
2129 -re "\r\n$gdb_prompt $" {
2130 }
2131 }
2132 }
2133 return $ok
2134 }
2135
2136 set compiler_info "unknown"
2137 set gcc_compiled 0
2138 set hp_cc_compiler 0
2139 set hp_aCC_compiler 0
2140
2141 # Figure out what compiler I am using.
2142 #
2143 # ARG can be empty or "C++". If empty, "C" is assumed.
2144 #
2145 # There are several ways to do this, with various problems.
2146 #
2147 # [ gdb_compile -E $ifile -o $binfile.ci ]
2148 # source $binfile.ci
2149 #
2150 # Single Unix Spec v3 says that "-E -o ..." together are not
2151 # specified. And in fact, the native compiler on hp-ux 11 (among
2152 # others) does not work with "-E -o ...". Most targets used to do
2153 # this, and it mostly worked, because it works with gcc.
2154 #
2155 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
2156 # source $binfile.ci
2157 #
2158 # This avoids the problem with -E and -o together. This almost works
2159 # if the build machine is the same as the host machine, which is
2160 # usually true of the targets which are not gcc. But this code does
2161 # not figure which compiler to call, and it always ends up using the C
2162 # compiler. Not good for setting hp_aCC_compiler. Targets
2163 # hppa*-*-hpux* and mips*-*-irix* used to do this.
2164 #
2165 # [ gdb_compile -E $ifile > $binfile.ci ]
2166 # source $binfile.ci
2167 #
2168 # dejagnu target_compile says that it supports output redirection,
2169 # but the code is completely different from the normal path and I
2170 # don't want to sweep the mines from that path. So I didn't even try
2171 # this.
2172 #
2173 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
2174 # eval $cppout
2175 #
2176 # I actually do this for all targets now. gdb_compile runs the right
2177 # compiler, and TCL captures the output, and I eval the output.
2178 #
2179 # Unfortunately, expect logs the output of the command as it goes by,
2180 # and dejagnu helpfully prints a second copy of it right afterwards.
2181 # So I turn off expect logging for a moment.
2182 #
2183 # [ gdb_compile $ifile $ciexe_file executable $args ]
2184 # [ remote_exec $ciexe_file ]
2185 # [ source $ci_file.out ]
2186 #
2187 # I could give up on -E and just do this.
2188 # I didn't get desperate enough to try this.
2189 #
2190 # -- chastain 2004-01-06
2191
2192 proc get_compiler_info {{arg ""}} {
2193 # For compiler.c and compiler.cc
2194 global srcdir
2195
2196 # I am going to play with the log to keep noise out.
2197 global outdir
2198 global tool
2199
2200 # These come from compiler.c or compiler.cc
2201 global compiler_info
2202
2203 # Legacy global data symbols.
2204 global gcc_compiled
2205 global hp_cc_compiler
2206 global hp_aCC_compiler
2207
2208 # Choose which file to preprocess.
2209 set ifile "${srcdir}/lib/compiler.c"
2210 if { $arg == "c++" } {
2211 set ifile "${srcdir}/lib/compiler.cc"
2212 }
2213
2214 # Run $ifile through the right preprocessor.
2215 # Toggle gdb.log to keep the compiler output out of the log.
2216 log_file
2217 if [is_remote host] {
2218 # We have to use -E and -o together, despite the comments
2219 # above, because of how DejaGnu handles remote host testing.
2220 set ppout "$outdir/compiler.i"
2221 gdb_compile "${ifile}" "$ppout" preprocess [list "$arg" quiet]
2222 set file [open $ppout r]
2223 set cppout [read $file]
2224 close $file
2225 } else {
2226 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$arg" quiet] ]
2227 }
2228 log_file -a "$outdir/$tool.log"
2229
2230 # Eval the output.
2231 set unknown 0
2232 foreach cppline [ split "$cppout" "\n" ] {
2233 if { [ regexp "^#" "$cppline" ] } {
2234 # line marker
2235 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
2236 # blank line
2237 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
2238 # eval this line
2239 verbose "get_compiler_info: $cppline" 2
2240 eval "$cppline"
2241 } else {
2242 # unknown line
2243 verbose -log "get_compiler_info: $cppline"
2244 set unknown 1
2245 }
2246 }
2247
2248 # Reset to unknown compiler if any diagnostics happened.
2249 if { $unknown } {
2250 set compiler_info "unknown"
2251 }
2252
2253 # Set the legacy symbols.
2254 set gcc_compiled 0
2255 set hp_cc_compiler 0
2256 set hp_aCC_compiler 0
2257 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
2258 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
2259 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
2260 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
2261 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
2262 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
2263 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
2264
2265 # Log what happened.
2266 verbose -log "get_compiler_info: $compiler_info"
2267
2268 # Most compilers will evaluate comparisons and other boolean
2269 # operations to 0 or 1.
2270 uplevel \#0 { set true 1 }
2271 uplevel \#0 { set false 0 }
2272
2273 # Use of aCC results in boolean results being displayed as
2274 # "true" or "false"
2275 if { $hp_aCC_compiler } {
2276 uplevel \#0 { set true true }
2277 uplevel \#0 { set false false }
2278 }
2279
2280 return 0;
2281 }
2282
2283 proc test_compiler_info { {compiler ""} } {
2284 global compiler_info
2285
2286 # if no arg, return the compiler_info string
2287
2288 if [string match "" $compiler] {
2289 if [info exists compiler_info] {
2290 return $compiler_info
2291 } else {
2292 perror "No compiler info found."
2293 }
2294 }
2295
2296 return [string match $compiler $compiler_info]
2297 }
2298
2299 proc current_target_name { } {
2300 global target_info
2301 if [info exists target_info(target,name)] {
2302 set answer $target_info(target,name)
2303 } else {
2304 set answer ""
2305 }
2306 return $answer
2307 }
2308
2309 set gdb_wrapper_initialized 0
2310 set gdb_wrapper_target ""
2311
2312 proc gdb_wrapper_init { args } {
2313 global gdb_wrapper_initialized;
2314 global gdb_wrapper_file;
2315 global gdb_wrapper_flags;
2316 global gdb_wrapper_target
2317
2318 if { $gdb_wrapper_initialized == 1 } { return; }
2319
2320 if {[target_info exists needs_status_wrapper] && \
2321 [target_info needs_status_wrapper] != "0"} {
2322 set result [build_wrapper "testglue.o"];
2323 if { $result != "" } {
2324 set gdb_wrapper_file [lindex $result 0];
2325 set gdb_wrapper_flags [lindex $result 1];
2326 } else {
2327 warning "Status wrapper failed to build."
2328 }
2329 }
2330 set gdb_wrapper_initialized 1
2331 set gdb_wrapper_target [current_target_name]
2332 }
2333
2334 # Some targets need to always link a special object in. Save its path here.
2335 global gdb_saved_set_unbuffered_mode_obj
2336 set gdb_saved_set_unbuffered_mode_obj ""
2337
2338 proc gdb_compile {source dest type options} {
2339 global GDB_TESTCASE_OPTIONS;
2340 global gdb_wrapper_file;
2341 global gdb_wrapper_flags;
2342 global gdb_wrapper_initialized;
2343 global srcdir
2344 global objdir
2345 global gdb_saved_set_unbuffered_mode_obj
2346
2347 set outdir [file dirname $dest]
2348
2349 # Add platform-specific options if a shared library was specified using
2350 # "shlib=librarypath" in OPTIONS.
2351 set new_options ""
2352 set shlib_found 0
2353 set shlib_load 0
2354 foreach opt $options {
2355 if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
2356 if [test_compiler_info "xlc-*"] {
2357 # IBM xlc compiler doesn't accept shared library named other
2358 # than .so: use "-Wl," to bypass this
2359 lappend source "-Wl,$shlib_name"
2360 } elseif { ([istarget "*-*-mingw*"]
2361 || [istarget *-*-cygwin*]
2362 || [istarget *-*-pe*])} {
2363 lappend source "${shlib_name}.a"
2364 } else {
2365 lappend source $shlib_name
2366 }
2367 if { $shlib_found == 0 } {
2368 set shlib_found 1
2369 if { ([istarget "*-*-mingw*"]
2370 || [istarget *-*-cygwin*]) } {
2371 lappend new_options "additional_flags=-Wl,--enable-auto-import"
2372 }
2373 }
2374 } elseif { $opt == "shlib_load" } {
2375 set shlib_load 1
2376 } else {
2377 lappend new_options $opt
2378 }
2379 }
2380
2381 # We typically link to shared libraries using an absolute path, and
2382 # that's how they are found at runtime. If we are going to
2383 # dynamically load one by basename, we must specify rpath. If we
2384 # are using a remote host, DejaGNU will link to the shared library
2385 # using a relative path, so again we must specify an rpath.
2386 if { $shlib_load || ($shlib_found && [is_remote target]) } {
2387 if { ([istarget "*-*-mingw*"]
2388 || [istarget *-*-cygwin*]
2389 || [istarget *-*-pe*]
2390 || [istarget hppa*-*-hpux*])} {
2391 # Do not need anything.
2392 } elseif { [istarget *-*-freebsd*] || [istarget *-*-openbsd*] } {
2393 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
2394 } elseif { [istarget arm*-*-symbianelf*] } {
2395 if { $shlib_load } {
2396 lappend new_options "libs=-ldl"
2397 }
2398 } else {
2399 if { $shlib_load } {
2400 lappend new_options "libs=-ldl"
2401 }
2402 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
2403 }
2404 }
2405 set options $new_options
2406
2407 if [target_info exists is_vxworks] {
2408 set options2 { "additional_flags=-Dvxworks" }
2409 set options [concat $options2 $options]
2410 }
2411 if [info exists GDB_TESTCASE_OPTIONS] {
2412 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
2413 }
2414 verbose "options are $options"
2415 verbose "source is $source $dest $type $options"
2416
2417 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
2418
2419 if {[target_info exists needs_status_wrapper] && \
2420 [target_info needs_status_wrapper] != "0" && \
2421 [info exists gdb_wrapper_file]} {
2422 lappend options "libs=${gdb_wrapper_file}"
2423 lappend options "ldflags=${gdb_wrapper_flags}"
2424 }
2425
2426 # Replace the "nowarnings" option with the appropriate additional_flags
2427 # to disable compiler warnings.
2428 set nowarnings [lsearch -exact $options nowarnings]
2429 if {$nowarnings != -1} {
2430 if [target_info exists gdb,nowarnings_flag] {
2431 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
2432 } else {
2433 set flag "additional_flags=-w"
2434 }
2435 set options [lreplace $options $nowarnings $nowarnings $flag]
2436 }
2437
2438 if { $type == "executable" } {
2439 if { ([istarget "*-*-mingw*"]
2440 || [istarget "*-*-*djgpp"]
2441 || [istarget "*-*-cygwin*"])} {
2442 # Force output to unbuffered mode, by linking in an object file
2443 # with a global contructor that calls setvbuf.
2444 #
2445 # Compile the special object seperatelly for two reasons:
2446 # 1) Insulate it from $options.
2447 # 2) Avoid compiling it for every gdb_compile invocation,
2448 # which is time consuming, especially if we're remote
2449 # host testing.
2450 #
2451 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
2452 verbose "compiling gdb_saved_set_unbuffered_obj"
2453 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
2454 set unbuf_obj ${objdir}/set_unbuffered_mode.o
2455
2456 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
2457 if { $result != "" } {
2458 return $result
2459 }
2460
2461 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
2462 # Link a copy of the output object, because the
2463 # original may be automatically deleted.
2464 remote_exec host "cp -f $unbuf_obj $gdb_saved_set_unbuffered_mode_obj"
2465 } else {
2466 verbose "gdb_saved_set_unbuffered_obj already compiled"
2467 }
2468
2469 # Rely on the internal knowledge that the global ctors are ran in
2470 # reverse link order. In that case, we can use ldflags to
2471 # avoid copying the object file to the host multiple
2472 # times.
2473 # This object can only be added if standard libraries are
2474 # used. Thus, we need to disable it if -nostdlib option is used
2475 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
2476 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
2477 }
2478 }
2479 }
2480
2481 set result [target_compile $source $dest $type $options];
2482
2483 # Prune uninteresting compiler (and linker) output.
2484 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
2485
2486 regsub "\[\r\n\]*$" "$result" "" result;
2487 regsub "^\[\r\n\]*" "$result" "" result;
2488
2489 if {[lsearch $options quiet] < 0} {
2490 # We shall update this on a per language basis, to avoid
2491 # changing the entire testsuite in one go.
2492 if {[lsearch $options f77] >= 0} {
2493 gdb_compile_test $source $result
2494 } elseif { $result != "" } {
2495 clone_output "gdb compile failed, $result"
2496 }
2497 }
2498 return $result;
2499 }
2500
2501
2502 # This is just like gdb_compile, above, except that it tries compiling
2503 # against several different thread libraries, to see which one this
2504 # system has.
2505 proc gdb_compile_pthreads {source dest type options} {
2506 set built_binfile 0
2507 set why_msg "unrecognized error"
2508 foreach lib {-lpthreads -lpthread -lthread ""} {
2509 # This kind of wipes out whatever libs the caller may have
2510 # set. Or maybe theirs will override ours. How infelicitous.
2511 set options_with_lib [concat $options [list libs=$lib quiet]]
2512 set ccout [gdb_compile $source $dest $type $options_with_lib]
2513 switch -regexp -- $ccout {
2514 ".*no posix threads support.*" {
2515 set why_msg "missing threads include file"
2516 break
2517 }
2518 ".*cannot open -lpthread.*" {
2519 set why_msg "missing runtime threads library"
2520 }
2521 ".*Can't find library for -lpthread.*" {
2522 set why_msg "missing runtime threads library"
2523 }
2524 {^$} {
2525 pass "successfully compiled posix threads test case"
2526 set built_binfile 1
2527 break
2528 }
2529 }
2530 }
2531 if {!$built_binfile} {
2532 unsupported "Couldn't compile $source: ${why_msg}"
2533 return -1
2534 }
2535 }
2536
2537 # Build a shared library from SOURCES. You must use get_compiler_info
2538 # first.
2539
2540 proc gdb_compile_shlib {sources dest options} {
2541 set obj_options $options
2542
2543 switch -glob [test_compiler_info] {
2544 "xlc-*" {
2545 lappend obj_options "additional_flags=-qpic"
2546 }
2547 "gcc-*" {
2548 if { !([istarget "powerpc*-*-aix*"]
2549 || [istarget "rs6000*-*-aix*"]
2550 || [istarget "*-*-cygwin*"]
2551 || [istarget "*-*-mingw*"]
2552 || [istarget "*-*-pe*"]) } {
2553 lappend obj_options "additional_flags=-fpic"
2554 }
2555 }
2556 default {
2557 switch -glob [istarget] {
2558 "hppa*-hp-hpux*" {
2559 lappend obj_options "additional_flags=+z"
2560 }
2561 "mips-sgi-irix*" {
2562 # Disable SGI compiler's implicit -Dsgi
2563 lappend obj_options "additional_flags=-Usgi"
2564 }
2565 default {
2566 # don't know what the compiler is...
2567 }
2568 }
2569 }
2570 }
2571
2572 set outdir [file dirname $dest]
2573 set objects ""
2574 foreach source $sources {
2575 set sourcebase [file tail $source]
2576 if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
2577 return -1
2578 }
2579 lappend objects ${outdir}/${sourcebase}.o
2580 }
2581
2582 if [istarget "hppa*-*-hpux*"] {
2583 remote_exec build "ld -b ${objects} -o ${dest}"
2584 } else {
2585 set link_options $options
2586 if [test_compiler_info "xlc-*"] {
2587 lappend link_options "additional_flags=-qmkshrobj"
2588 } else {
2589 lappend link_options "additional_flags=-shared"
2590
2591 if { ([istarget "*-*-mingw*"]
2592 || [istarget *-*-cygwin*]
2593 || [istarget *-*-pe*])} {
2594 lappend link_options "additional_flags=-Wl,--out-implib,${dest}.a"
2595 } elseif [is_remote target] {
2596 # By default, we do not set the soname. This causes the linker
2597 # on ELF systems to create a DT_NEEDED entry in the executable
2598 # refering to the full path name of the library. This is a
2599 # problem in remote testing if the library is in a different
2600 # directory there. To fix this, we set a soname of just the
2601 # base filename for the library, and add an appropriate -rpath
2602 # to the main executable (in gdb_compile).
2603 set destbase [file tail $dest]
2604 lappend link_options "additional_flags=-Wl,-soname,$destbase"
2605 }
2606 }
2607 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
2608 return -1
2609 }
2610 }
2611 }
2612
2613 # This is just like gdb_compile_shlib, above, except that it tries compiling
2614 # against several different thread libraries, to see which one this
2615 # system has.
2616 proc gdb_compile_shlib_pthreads {sources dest options} {
2617 set built_binfile 0
2618 set why_msg "unrecognized error"
2619 foreach lib {-lpthreads -lpthread -lthread ""} {
2620 # This kind of wipes out whatever libs the caller may have
2621 # set. Or maybe theirs will override ours. How infelicitous.
2622 set options_with_lib [concat $options [list libs=$lib quiet]]
2623 set ccout [gdb_compile_shlib $sources $dest $options_with_lib]
2624 switch -regexp -- $ccout {
2625 ".*no posix threads support.*" {
2626 set why_msg "missing threads include file"
2627 break
2628 }
2629 ".*cannot open -lpthread.*" {
2630 set why_msg "missing runtime threads library"
2631 }
2632 ".*Can't find library for -lpthread.*" {
2633 set why_msg "missing runtime threads library"
2634 }
2635 {^$} {
2636 pass "successfully compiled posix threads test case"
2637 set built_binfile 1
2638 break
2639 }
2640 }
2641 }
2642 if {!$built_binfile} {
2643 unsupported "Couldn't compile $sources: ${why_msg}"
2644 return -1
2645 }
2646 }
2647
2648 # This is just like gdb_compile_pthreads, above, except that we always add the
2649 # objc library for compiling Objective-C programs
2650 proc gdb_compile_objc {source dest type options} {
2651 set built_binfile 0
2652 set why_msg "unrecognized error"
2653 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
2654 # This kind of wipes out whatever libs the caller may have
2655 # set. Or maybe theirs will override ours. How infelicitous.
2656 if { $lib == "solaris" } {
2657 set lib "-lpthread -lposix4"
2658 }
2659 if { $lib != "-lobjc" } {
2660 set lib "-lobjc $lib"
2661 }
2662 set options_with_lib [concat $options [list libs=$lib quiet]]
2663 set ccout [gdb_compile $source $dest $type $options_with_lib]
2664 switch -regexp -- $ccout {
2665 ".*no posix threads support.*" {
2666 set why_msg "missing threads include file"
2667 break
2668 }
2669 ".*cannot open -lpthread.*" {
2670 set why_msg "missing runtime threads library"
2671 }
2672 ".*Can't find library for -lpthread.*" {
2673 set why_msg "missing runtime threads library"
2674 }
2675 {^$} {
2676 pass "successfully compiled objc with posix threads test case"
2677 set built_binfile 1
2678 break
2679 }
2680 }
2681 }
2682 if {!$built_binfile} {
2683 unsupported "Couldn't compile $source: ${why_msg}"
2684 return -1
2685 }
2686 }
2687
2688 proc send_gdb { string } {
2689 global suppress_flag;
2690 if { $suppress_flag } {
2691 return "suppressed";
2692 }
2693 return [remote_send host "$string"];
2694 }
2695
2696 #
2697 #
2698
2699 proc gdb_expect { args } {
2700 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
2701 set atimeout [lindex $args 0];
2702 set expcode [list [lindex $args 1]];
2703 } else {
2704 set expcode $args;
2705 }
2706
2707 upvar timeout timeout;
2708
2709 if [target_info exists gdb,timeout] {
2710 if [info exists timeout] {
2711 if { $timeout < [target_info gdb,timeout] } {
2712 set gtimeout [target_info gdb,timeout];
2713 } else {
2714 set gtimeout $timeout;
2715 }
2716 } else {
2717 set gtimeout [target_info gdb,timeout];
2718 }
2719 }
2720
2721 if ![info exists gtimeout] {
2722 global timeout;
2723 if [info exists timeout] {
2724 set gtimeout $timeout;
2725 }
2726 }
2727
2728 if [info exists atimeout] {
2729 if { ![info exists gtimeout] || $gtimeout < $atimeout } {
2730 set gtimeout $atimeout;
2731 }
2732 } else {
2733 if ![info exists gtimeout] {
2734 # Eeeeew.
2735 set gtimeout 60;
2736 }
2737 }
2738
2739 global suppress_flag;
2740 global remote_suppress_flag;
2741 if [info exists remote_suppress_flag] {
2742 set old_val $remote_suppress_flag;
2743 }
2744 if [info exists suppress_flag] {
2745 if { $suppress_flag } {
2746 set remote_suppress_flag 1;
2747 }
2748 }
2749 set code [catch \
2750 {uplevel remote_expect host $gtimeout $expcode} string];
2751 if [info exists old_val] {
2752 set remote_suppress_flag $old_val;
2753 } else {
2754 if [info exists remote_suppress_flag] {
2755 unset remote_suppress_flag;
2756 }
2757 }
2758
2759 if {$code == 1} {
2760 global errorInfo errorCode;
2761
2762 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
2763 } else {
2764 return -code $code $string
2765 }
2766 }
2767
2768 # gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
2769 #
2770 # Check for long sequence of output by parts.
2771 # TEST: is the test message to be printed with the test success/fail.
2772 # SENTINEL: Is the terminal pattern indicating that output has finished.
2773 # LIST: is the sequence of outputs to match.
2774 # If the sentinel is recognized early, it is considered an error.
2775 #
2776 # Returns:
2777 # 1 if the test failed,
2778 # 0 if the test passes,
2779 # -1 if there was an internal error.
2780
2781 proc gdb_expect_list {test sentinel list} {
2782 global gdb_prompt
2783 global suppress_flag
2784 set index 0
2785 set ok 1
2786 if { $suppress_flag } {
2787 set ok 0
2788 unresolved "${test}"
2789 }
2790 while { ${index} < [llength ${list}] } {
2791 set pattern [lindex ${list} ${index}]
2792 set index [expr ${index} + 1]
2793 verbose -log "gdb_expect_list pattern: /$pattern/" 2
2794 if { ${index} == [llength ${list}] } {
2795 if { ${ok} } {
2796 gdb_expect {
2797 -re "${pattern}${sentinel}" {
2798 # pass "${test}, pattern ${index} + sentinel"
2799 }
2800 -re "${sentinel}" {
2801 fail "${test} (pattern ${index} + sentinel)"
2802 set ok 0
2803 }
2804 -re ".*A problem internal to GDB has been detected" {
2805 fail "${test} (GDB internal error)"
2806 set ok 0
2807 gdb_internal_error_resync
2808 }
2809 timeout {
2810 fail "${test} (pattern ${index} + sentinel) (timeout)"
2811 set ok 0
2812 }
2813 }
2814 } else {
2815 # unresolved "${test}, pattern ${index} + sentinel"
2816 }
2817 } else {
2818 if { ${ok} } {
2819 gdb_expect {
2820 -re "${pattern}" {
2821 # pass "${test}, pattern ${index}"
2822 }
2823 -re "${sentinel}" {
2824 fail "${test} (pattern ${index})"
2825 set ok 0
2826 }
2827 -re ".*A problem internal to GDB has been detected" {
2828 fail "${test} (GDB internal error)"
2829 set ok 0
2830 gdb_internal_error_resync
2831 }
2832 timeout {
2833 fail "${test} (pattern ${index}) (timeout)"
2834 set ok 0
2835 }
2836 }
2837 } else {
2838 # unresolved "${test}, pattern ${index}"
2839 }
2840 }
2841 }
2842 if { ${ok} } {
2843 pass "${test}"
2844 return 0
2845 } else {
2846 return 1
2847 }
2848 }
2849
2850 #
2851 #
2852 proc gdb_suppress_entire_file { reason } {
2853 global suppress_flag;
2854
2855 warning "$reason\n";
2856 set suppress_flag -1;
2857 }
2858
2859 #
2860 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
2861 # gdb_expect to fail immediately (until the next call to
2862 # gdb_stop_suppressing_tests).
2863 #
2864 proc gdb_suppress_tests { args } {
2865 global suppress_flag;
2866
2867 return; # fnf - disable pending review of results where
2868 # testsuite ran better without this
2869 incr suppress_flag;
2870
2871 if { $suppress_flag == 1 } {
2872 if { [llength $args] > 0 } {
2873 warning "[lindex $args 0]\n";
2874 } else {
2875 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
2876 }
2877 }
2878 }
2879
2880 #
2881 # Clear suppress_flag.
2882 #
2883 proc gdb_stop_suppressing_tests { } {
2884 global suppress_flag;
2885
2886 if [info exists suppress_flag] {
2887 if { $suppress_flag > 0 } {
2888 set suppress_flag 0;
2889 clone_output "Tests restarted.\n";
2890 }
2891 } else {
2892 set suppress_flag 0;
2893 }
2894 }
2895
2896 proc gdb_clear_suppressed { } {
2897 global suppress_flag;
2898
2899 set suppress_flag 0;
2900 }
2901
2902 proc gdb_start { } {
2903 default_gdb_start
2904 }
2905
2906 proc gdb_exit { } {
2907 catch default_gdb_exit
2908 }
2909
2910 #
2911 # gdb_load_cmd -- load a file into the debugger.
2912 # ARGS - additional args to load command.
2913 # return a -1 if anything goes wrong.
2914 #
2915 proc gdb_load_cmd { args } {
2916 global gdb_prompt
2917
2918 if [target_info exists gdb_load_timeout] {
2919 set loadtimeout [target_info gdb_load_timeout]
2920 } else {
2921 set loadtimeout 1600
2922 }
2923 send_gdb "load $args\n"
2924 verbose "Timeout is now $loadtimeout seconds" 2
2925 gdb_expect $loadtimeout {
2926 -re "Loading section\[^\r\]*\r\n" {
2927 exp_continue
2928 }
2929 -re "Start address\[\r\]*\r\n" {
2930 exp_continue
2931 }
2932 -re "Transfer rate\[\r\]*\r\n" {
2933 exp_continue
2934 }
2935 -re "Memory access error\[^\r\]*\r\n" {
2936 perror "Failed to load program"
2937 return -1
2938 }
2939 -re "$gdb_prompt $" {
2940 return 0
2941 }
2942 -re "(.*)\r\n$gdb_prompt " {
2943 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
2944 return -1
2945 }
2946 timeout {
2947 perror "Timed out trying to load $args."
2948 return -1
2949 }
2950 }
2951 return -1
2952 }
2953
2954 # Return the filename to download to the target and load on the target
2955 # for this shared library. Normally just LIBNAME, unless shared libraries
2956 # for this target have separate link and load images.
2957
2958 proc shlib_target_file { libname } {
2959 return $libname
2960 }
2961
2962 # Return the filename GDB will load symbols from when debugging this
2963 # shared library. Normally just LIBNAME, unless shared libraries for
2964 # this target have separate link and load images.
2965
2966 proc shlib_symbol_file { libname } {
2967 return $libname
2968 }
2969
2970 # Return the filename to download to the target and load for this
2971 # executable. Normally just BINFILE unless it is renamed to something
2972 # else for this target.
2973
2974 proc exec_target_file { binfile } {
2975 return $binfile
2976 }
2977
2978 # Return the filename GDB will load symbols from when debugging this
2979 # executable. Normally just BINFILE unless executables for this target
2980 # have separate files for symbols.
2981
2982 proc exec_symbol_file { binfile } {
2983 return $binfile
2984 }
2985
2986 # Rename the executable file. Normally this is just BINFILE1 being renamed
2987 # to BINFILE2, but some targets require multiple binary files.
2988 proc gdb_rename_execfile { binfile1 binfile2 } {
2989 file rename -force [exec_target_file ${binfile1}] \
2990 [exec_target_file ${binfile2}]
2991 if { [exec_target_file ${binfile1}] != [exec_symbol_file ${binfile1}] } {
2992 file rename -force [exec_symbol_file ${binfile1}] \
2993 [exec_symbol_file ${binfile2}]
2994 }
2995 }
2996
2997 # "Touch" the executable file to update the date. Normally this is just
2998 # BINFILE, but some targets require multiple files.
2999 proc gdb_touch_execfile { binfile } {
3000 set time [clock seconds]
3001 file mtime [exec_target_file ${binfile}] $time
3002 if { [exec_target_file ${binfile}] != [exec_symbol_file ${binfile}] } {
3003 file mtime [exec_symbol_file ${binfile}] $time
3004 }
3005 }
3006
3007 # gdb_download
3008 #
3009 # Copy a file to the remote target and return its target filename.
3010 # Schedule the file to be deleted at the end of this test.
3011
3012 proc gdb_download { filename } {
3013 global cleanfiles
3014
3015 set destname [remote_download target $filename]
3016 lappend cleanfiles $destname
3017 return $destname
3018 }
3019
3020 # gdb_load_shlibs LIB...
3021 #
3022 # Copy the listed libraries to the target.
3023
3024 proc gdb_load_shlibs { args } {
3025 if {![is_remote target]} {
3026 return
3027 }
3028
3029 foreach file $args {
3030 gdb_download [shlib_target_file $file]
3031 }
3032
3033 # Even if the target supplies full paths for shared libraries,
3034 # they may not be paths for this system.
3035 gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "" ""
3036 }
3037
3038 #
3039 # gdb_load -- load a file into the debugger.
3040 # Many files in config/*.exp override this procedure.
3041 #
3042 proc gdb_load { arg } {
3043 return [gdb_file_cmd $arg]
3044 }
3045
3046 # gdb_reload -- load a file into the target. Called before "running",
3047 # either the first time or after already starting the program once,
3048 # for remote targets. Most files that override gdb_load should now
3049 # override this instead.
3050
3051 proc gdb_reload { } {
3052 # For the benefit of existing configurations, default to gdb_load.
3053 # Specifying no file defaults to the executable currently being
3054 # debugged.
3055 return [gdb_load ""]
3056 }
3057
3058 proc gdb_continue { function } {
3059 global decimal
3060
3061 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
3062 }
3063
3064 proc default_gdb_init { args } {
3065 global gdb_wrapper_initialized
3066 global gdb_wrapper_target
3067 global gdb_test_file_name
3068 global cleanfiles
3069
3070 set cleanfiles {}
3071
3072 gdb_clear_suppressed;
3073
3074 set gdb_test_file_name [file rootname [file tail [lindex $args 0]]]
3075
3076 # Make sure that the wrapper is rebuilt
3077 # with the appropriate multilib option.
3078 if { $gdb_wrapper_target != [current_target_name] } {
3079 set gdb_wrapper_initialized 0
3080 }
3081
3082 # Unlike most tests, we have a small number of tests that generate
3083 # a very large amount of output. We therefore increase the expect
3084 # buffer size to be able to contain the entire test output.
3085 match_max -d 30000
3086 # Also set this value for the currently running GDB.
3087 match_max [match_max -d]
3088
3089 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
3090 if { [llength $args] > 0 } {
3091 global pf_prefix
3092
3093 set file [lindex $args 0];
3094
3095 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
3096 }
3097 global gdb_prompt;
3098 if [target_info exists gdb_prompt] {
3099 set gdb_prompt [target_info gdb_prompt];
3100 } else {
3101 set gdb_prompt "\\(gdb\\)"
3102 }
3103 global use_gdb_stub
3104 if [info exists use_gdb_stub] {
3105 unset use_gdb_stub
3106 }
3107 }
3108
3109 # Turn BASENAME into a full file name in the standard output
3110 # directory. It is ok if BASENAME is the empty string; in this case
3111 # the directory is returned.
3112
3113 proc standard_output_file {basename} {
3114 global objdir subdir
3115
3116 return [file join $objdir $subdir $basename]
3117 }
3118
3119 # Set 'testfile', 'srcfile', and 'binfile'.
3120 #
3121 # ARGS is a list of source file specifications.
3122 # Without any arguments, the .exp file's base name is used to
3123 # compute the source file name. The ".c" extension is added in this case.
3124 # If ARGS is not empty, each entry is a source file specification.
3125 # If the specification starts with a ".", it is treated as a suffix
3126 # to append to the .exp file's base name.
3127 # If the specification is the empty string, it is treated as if it
3128 # were ".c".
3129 # Otherwise it is a file name.
3130 # The first file in the list is used to set the 'srcfile' global.
3131 # Each subsequent name is used to set 'srcfile2', 'srcfile3', etc.
3132 #
3133 # Most tests should call this without arguments.
3134 #
3135 # If a completely different binary file name is needed, then it
3136 # should be handled in the .exp file with a suitable comment.
3137
3138 proc standard_testfile {args} {
3139 global gdb_test_file_name
3140 global subdir
3141 global gdb_test_file_last_vars
3142
3143 # Outputs.
3144 global testfile binfile
3145
3146 set testfile $gdb_test_file_name
3147 set binfile [standard_output_file ${testfile}]
3148
3149 if {[llength $args] == 0} {
3150 set args .c
3151 }
3152
3153 # Unset our previous output variables.
3154 # This can help catch hidden bugs.
3155 if {[info exists gdb_test_file_last_vars]} {
3156 foreach varname $gdb_test_file_last_vars {
3157 global $varname
3158 catch {unset $varname}
3159 }
3160 }
3161 # 'executable' is often set by tests.
3162 set gdb_test_file_last_vars {executable}
3163
3164 set suffix ""
3165 foreach arg $args {
3166 set varname srcfile$suffix
3167 global $varname
3168
3169 # Handle an extension.
3170 if {$arg == ""} {
3171 set arg $testfile.c
3172 } elseif {[string range $arg 0 0] == "."} {
3173 set arg $testfile$arg
3174 }
3175
3176 set $varname $arg
3177 lappend gdb_test_file_last_vars $varname
3178
3179 if {$suffix == ""} {
3180 set suffix 2
3181 } else {
3182 incr suffix
3183 }
3184 }
3185 }
3186
3187 # The default timeout used when testing GDB commands. We want to use
3188 # the same timeout as the default dejagnu timeout, unless the user has
3189 # already provided a specific value (probably through a site.exp file).
3190 global gdb_test_timeout
3191 if ![info exists gdb_test_timeout] {
3192 set gdb_test_timeout $timeout
3193 }
3194
3195 # A list of global variables that GDB testcases should not use.
3196 # We try to prevent their use by monitoring write accesses and raising
3197 # an error when that happens.
3198 set banned_variables { bug_id prms_id }
3199
3200 # A list of procedures that GDB testcases should not use.
3201 # We try to prevent their use by monitoring invocations and raising
3202 # an error when that happens.
3203 set banned_procedures { strace }
3204
3205 # gdb_init is called by runtest at start, but also by several
3206 # tests directly; gdb_finish is only called from within runtest after
3207 # each test source execution.
3208 # Placing several traces by repetitive calls to gdb_init leads
3209 # to problems, as only one trace is removed in gdb_finish.
3210 # To overcome this possible problem, we add a variable that records
3211 # if the banned variables and procedures are already traced.
3212 set banned_traced 0
3213
3214 proc gdb_init { args } {
3215 # Reset the timeout value to the default. This way, any testcase
3216 # that changes the timeout value without resetting it cannot affect
3217 # the timeout used in subsequent testcases.
3218 global gdb_test_timeout
3219 global timeout
3220 set timeout $gdb_test_timeout
3221
3222 # Block writes to all banned variables, and invocation of all
3223 # banned procedures...
3224 global banned_variables
3225 global banned_procedures
3226 global banned_traced
3227 if (!$banned_traced) {
3228 foreach banned_var $banned_variables {
3229 global "$banned_var"
3230 trace add variable "$banned_var" write error
3231 }
3232 foreach banned_proc $banned_procedures {
3233 global "$banned_proc"
3234 trace add execution "$banned_proc" enter error
3235 }
3236 set banned_traced 1
3237 }
3238
3239 # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same
3240 # messages as expected.
3241 setenv LC_ALL C
3242 setenv LC_CTYPE C
3243 setenv LANG C
3244
3245 # Don't let a .inputrc file or an existing setting of INPUTRC mess up
3246 # the test results. Even if /dev/null doesn't exist on the particular
3247 # platform, the readline library will use the default setting just by
3248 # failing to open the file. OTOH, opening /dev/null successfully will
3249 # also result in the default settings being used since nothing will be
3250 # read from this file.
3251 setenv INPUTRC "/dev/null"
3252
3253 # The gdb.base/readline.exp arrow key test relies on the standard VT100
3254 # bindings, so make sure that an appropriate terminal is selected.
3255 # The same bug doesn't show up if we use ^P / ^N instead.
3256 setenv TERM "vt100"
3257
3258 # Some tests (for example gdb.base/maint.exp) shell out from gdb to use
3259 # grep. Clear GREP_OPTIONS to make the behavoiur predictable,
3260 # especially having color output turned on can cause tests to fail.
3261 setenv GREP_OPTIONS ""
3262
3263 # Clear $gdbserver_reconnect_p.
3264 global gdbserver_reconnect_p
3265 set gdbserver_reconnect_p 1
3266 unset gdbserver_reconnect_p
3267
3268 return [eval default_gdb_init $args];
3269 }
3270
3271 proc gdb_finish { } {
3272 global cleanfiles
3273
3274 # Exit first, so that the files are no longer in use.
3275 gdb_exit
3276
3277 if { [llength $cleanfiles] > 0 } {
3278 eval remote_file target delete $cleanfiles
3279 set cleanfiles {}
3280 }
3281
3282 # Unblock write access to the banned variables. Dejagnu typically
3283 # resets some of them between testcases.
3284 global banned_variables
3285 global banned_procedures
3286 global banned_traced
3287 if ($banned_traced) {
3288 foreach banned_var $banned_variables {
3289 global "$banned_var"
3290 trace remove variable "$banned_var" write error
3291 }
3292 foreach banned_proc $banned_procedures {
3293 global "$banned_proc"
3294 trace remove execution "$banned_proc" enter error
3295 }
3296 set banned_traced 0
3297 }
3298 }
3299
3300 global debug_format
3301 set debug_format "unknown"
3302
3303 # Run the gdb command "info source" and extract the debugging format
3304 # information from the output and save it in debug_format.
3305
3306 proc get_debug_format { } {
3307 global gdb_prompt
3308 global verbose
3309 global expect_out
3310 global debug_format
3311
3312 set debug_format "unknown"
3313 send_gdb "info source\n"
3314 gdb_expect 10 {
3315 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
3316 set debug_format $expect_out(1,string)
3317 verbose "debug format is $debug_format"
3318 return 1;
3319 }
3320 -re "No current source file.\r\n$gdb_prompt $" {
3321 perror "get_debug_format used when no current source file"
3322 return 0;
3323 }
3324 -re "$gdb_prompt $" {
3325 warning "couldn't check debug format (no valid response)."
3326 return 1;
3327 }
3328 timeout {
3329 warning "couldn't check debug format (timeout)."
3330 return 1;
3331 }
3332 }
3333 }
3334
3335 # Return true if FORMAT matches the debug format the current test was
3336 # compiled with. FORMAT is a shell-style globbing pattern; it can use
3337 # `*', `[...]', and so on.
3338 #
3339 # This function depends on variables set by `get_debug_format', above.
3340
3341 proc test_debug_format {format} {
3342 global debug_format
3343
3344 return [expr [string match $format $debug_format] != 0]
3345 }
3346
3347 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
3348 # COFF, stabs, etc). If that format matches the format that the
3349 # current test was compiled with, then the next test is expected to
3350 # fail for any target. Returns 1 if the next test or set of tests is
3351 # expected to fail, 0 otherwise (or if it is unknown). Must have
3352 # previously called get_debug_format.
3353 proc setup_xfail_format { format } {
3354 set ret [test_debug_format $format];
3355
3356 if {$ret} then {
3357 setup_xfail "*-*-*"
3358 }
3359 return $ret;
3360 }
3361
3362 # Like setup_kfail, but only call setup_kfail conditionally if
3363 # istarget[TARGET] returns true.
3364 proc setup_kfail_for_target { PR target } {
3365 if { [istarget $target] } {
3366 setup_kfail $PR $target
3367 }
3368 }
3369
3370 # gdb_get_line_number TEXT [FILE]
3371 #
3372 # Search the source file FILE, and return the line number of the
3373 # first line containing TEXT. If no match is found, an error is thrown.
3374 #
3375 # TEXT is a string literal, not a regular expression.
3376 #
3377 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
3378 # specified, and does not start with "/", then it is assumed to be in
3379 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
3380 # by changing the callers and the interface at the same time.
3381 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
3382 # gdb.base/ena-dis-br.exp.
3383 #
3384 # Use this function to keep your test scripts independent of the
3385 # exact line numbering of the source file. Don't write:
3386 #
3387 # send_gdb "break 20"
3388 #
3389 # This means that if anyone ever edits your test's source file,
3390 # your test could break. Instead, put a comment like this on the
3391 # source file line you want to break at:
3392 #
3393 # /* breakpoint spot: frotz.exp: test name */
3394 #
3395 # and then write, in your test script (which we assume is named
3396 # frotz.exp):
3397 #
3398 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
3399 #
3400 # (Yes, Tcl knows how to handle the nested quotes and brackets.
3401 # Try this:
3402 # $ tclsh
3403 # % puts "foo [lindex "bar baz" 1]"
3404 # foo baz
3405 # %
3406 # Tcl is quite clever, for a little stringy language.)
3407 #
3408 # ===
3409 #
3410 # The previous implementation of this procedure used the gdb search command.
3411 # This version is different:
3412 #
3413 # . It works with MI, and it also works when gdb is not running.
3414 #
3415 # . It operates on the build machine, not the host machine.
3416 #
3417 # . For now, this implementation fakes a current directory of
3418 # $srcdir/$subdir to be compatible with the old implementation.
3419 # This will go away eventually and some callers will need to
3420 # be changed.
3421 #
3422 # . The TEXT argument is literal text and matches literally,
3423 # not a regular expression as it was before.
3424 #
3425 # . State changes in gdb, such as changing the current file
3426 # and setting $_, no longer happen.
3427 #
3428 # After a bit of time we can forget about the differences from the
3429 # old implementation.
3430 #
3431 # --chastain 2004-08-05
3432
3433 proc gdb_get_line_number { text { file "" } } {
3434 global srcdir
3435 global subdir
3436 global srcfile
3437
3438 if { "$file" == "" } then {
3439 set file "$srcfile"
3440 }
3441 if { ! [regexp "^/" "$file"] } then {
3442 set file "$srcdir/$subdir/$file"
3443 }
3444
3445 if { [ catch { set fd [open "$file"] } message ] } then {
3446 error "$message"
3447 }
3448
3449 set found -1
3450 for { set line 1 } { 1 } { incr line } {
3451 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
3452 error "$message"
3453 }
3454 if { $nchar < 0 } then {
3455 break
3456 }
3457 if { [string first "$text" "$body"] >= 0 } then {
3458 set found $line
3459 break
3460 }
3461 }
3462
3463 if { [ catch { close "$fd" } message ] } then {
3464 error "$message"
3465 }
3466
3467 if {$found == -1} {
3468 error "undefined tag \"$text\""
3469 }
3470
3471 return $found
3472 }
3473
3474 # gdb_continue_to_end:
3475 # The case where the target uses stubs has to be handled specially. If a
3476 # stub is used, we set a breakpoint at exit because we cannot rely on
3477 # exit() behavior of a remote target.
3478 #
3479 # MSSG is the error message that gets printed. If not given, a
3480 # default is used.
3481 # COMMAND is the command to invoke. If not given, "continue" is
3482 # used.
3483 # ALLOW_EXTRA is a flag indicating whether the test should expect
3484 # extra output between the "Continuing." line and the program
3485 # exiting. By default it is zero; if nonzero, any extra output
3486 # is accepted.
3487
3488 proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} {
3489 global inferior_exited_re use_gdb_stub
3490
3491 if {$mssg == ""} {
3492 set text "continue until exit"
3493 } else {
3494 set text "continue until exit at $mssg"
3495 }
3496 if {$allow_extra} {
3497 set extra ".*"
3498 } else {
3499 set extra ""
3500 }
3501 if $use_gdb_stub {
3502 if {![gdb_breakpoint "exit"]} {
3503 return 0
3504 }
3505 gdb_test $command "Continuing..*Breakpoint .*exit.*" \
3506 $text
3507 } else {
3508 # Continue until we exit. Should not stop again.
3509 # Don't bother to check the output of the program, that may be
3510 # extremely tough for some remote systems.
3511 gdb_test $command \
3512 "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\
3513 $text
3514 }
3515 }
3516
3517 proc rerun_to_main {} {
3518 global gdb_prompt use_gdb_stub
3519
3520 if $use_gdb_stub {
3521 gdb_run_cmd
3522 gdb_expect {
3523 -re ".*Breakpoint .*main .*$gdb_prompt $"\
3524 {pass "rerun to main" ; return 0}
3525 -re "$gdb_prompt $"\
3526 {fail "rerun to main" ; return 0}
3527 timeout {fail "(timeout) rerun to main" ; return 0}
3528 }
3529 } else {
3530 send_gdb "run\n"
3531 gdb_expect {
3532 -re "The program .* has been started already.*y or n. $" {
3533 send_gdb "y\n"
3534 exp_continue
3535 }
3536 -re "Starting program.*$gdb_prompt $"\
3537 {pass "rerun to main" ; return 0}
3538 -re "$gdb_prompt $"\
3539 {fail "rerun to main" ; return 0}
3540 timeout {fail "(timeout) rerun to main" ; return 0}
3541 }
3542 }
3543 }
3544
3545 # Print a message and return true if a test should be skipped
3546 # due to lack of floating point suport.
3547
3548 proc gdb_skip_float_test { msg } {
3549 if [target_info exists gdb,skip_float_tests] {
3550 verbose "Skipping test '$msg': no float tests.";
3551 return 1;
3552 }
3553 return 0;
3554 }
3555
3556 # Print a message and return true if a test should be skipped
3557 # due to lack of stdio support.
3558
3559 proc gdb_skip_stdio_test { msg } {
3560 if [target_info exists gdb,noinferiorio] {
3561 verbose "Skipping test '$msg': no inferior i/o.";
3562 return 1;
3563 }
3564 return 0;
3565 }
3566
3567 proc gdb_skip_bogus_test { msg } {
3568 return 0;
3569 }
3570
3571 # Return true if a test should be skipped due to lack of XML support
3572 # in the host GDB.
3573 # NOTE: This must be called while gdb is *not* running.
3574
3575 proc gdb_skip_xml_test { } {
3576 global gdb_prompt
3577 global srcdir
3578 global xml_missing_cached
3579
3580 if {[info exists xml_missing_cached]} {
3581 return $xml_missing_cached
3582 }
3583
3584 gdb_start
3585 set xml_missing_cached 0
3586 gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" {
3587 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
3588 set xml_missing_cached 1
3589 }
3590 -re ".*$gdb_prompt $" { }
3591 }
3592 gdb_exit
3593 return $xml_missing_cached
3594 }
3595
3596 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
3597 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
3598 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
3599 # the name of a debuginfo only file. This file will be stored in the same
3600 # subdirectory.
3601
3602 # Functions for separate debug info testing
3603
3604 # starting with an executable:
3605 # foo --> original executable
3606
3607 # at the end of the process we have:
3608 # foo.stripped --> foo w/o debug info
3609 # foo.debug --> foo's debug info
3610 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
3611
3612 # Return the build-id hex string (usually 160 bits as 40 hex characters)
3613 # converted to the form: .build-id/ab/cdef1234...89.debug
3614 # Return "" if no build-id found.
3615 proc build_id_debug_filename_get { exec } {
3616 set tmp "${exec}-tmp"
3617 set objcopy_program [transform objcopy]
3618
3619 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $exec $tmp" output]
3620 verbose "result is $result"
3621 verbose "output is $output"
3622 if {$result == 1} {
3623 return ""
3624 }
3625 set fi [open $tmp]
3626 fconfigure $fi -translation binary
3627 # Skip the NOTE header.
3628 read $fi 16
3629 set data [read $fi]
3630 close $fi
3631 file delete $tmp
3632 if ![string compare $data ""] then {
3633 return ""
3634 }
3635 # Convert it to hex.
3636 binary scan $data H* data
3637 regsub {^..} $data {\0/} data
3638 return ".build-id/${data}.debug";
3639 }
3640
3641 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
3642 # list of optional flags. The only currently supported flag is no-main,
3643 # which removes the symbol entry for main from the separate debug file.
3644 #
3645 # Function returns zero on success. Function will return non-zero failure code
3646 # on some targets not supporting separate debug info (such as i386-msdos).
3647
3648 proc gdb_gnu_strip_debug { dest args } {
3649
3650 # Use the first separate debug info file location searched by GDB so the
3651 # run cannot be broken by some stale file searched with higher precedence.
3652 set debug_file "${dest}.debug"
3653
3654 set strip_to_file_program [transform strip]
3655 set objcopy_program [transform objcopy]
3656
3657 set debug_link [file tail $debug_file]
3658 set stripped_file "${dest}.stripped"
3659
3660 # Get rid of the debug info, and store result in stripped_file
3661 # something like gdb/testsuite/gdb.base/blah.stripped.
3662 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
3663 verbose "result is $result"
3664 verbose "output is $output"
3665 if {$result == 1} {
3666 return 1
3667 }
3668
3669 # Workaround PR binutils/10802:
3670 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3671 set perm [file attributes ${dest} -permissions]
3672 file attributes ${stripped_file} -permissions $perm
3673
3674 # Get rid of everything but the debug info, and store result in debug_file
3675 # This will be in the .debug subdirectory, see above.
3676 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
3677 verbose "result is $result"
3678 verbose "output is $output"
3679 if {$result == 1} {
3680 return 1
3681 }
3682
3683 # If no-main is passed, strip the symbol for main from the separate
3684 # file. This is to simulate the behavior of elfutils's eu-strip, which
3685 # leaves the symtab in the original file only. There's no way to get
3686 # objcopy or strip to remove the symbol table without also removing the
3687 # debugging sections, so this is as close as we can get.
3688 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
3689 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
3690 verbose "result is $result"
3691 verbose "output is $output"
3692 if {$result == 1} {
3693 return 1
3694 }
3695 file delete "${debug_file}"
3696 file rename "${debug_file}-tmp" "${debug_file}"
3697 }
3698
3699 # Link the two previous output files together, adding the .gnu_debuglink
3700 # section to the stripped_file, containing a pointer to the debug_file,
3701 # save the new file in dest.
3702 # This will be the regular executable filename, in the usual location.
3703 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
3704 verbose "result is $result"
3705 verbose "output is $output"
3706 if {$result == 1} {
3707 return 1
3708 }
3709
3710 # Workaround PR binutils/10802:
3711 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3712 set perm [file attributes ${stripped_file} -permissions]
3713 file attributes ${dest} -permissions $perm
3714
3715 return 0
3716 }
3717
3718 # Test the output of GDB_COMMAND matches the pattern obtained
3719 # by concatenating all elements of EXPECTED_LINES. This makes
3720 # it possible to split otherwise very long string into pieces.
3721 # If third argument is not empty, it's used as the name of the
3722 # test to be printed on pass/fail.
3723 proc help_test_raw { gdb_command expected_lines args } {
3724 set message $gdb_command
3725 if [llength $args]>0 then {
3726 set message [lindex $args 0]
3727 }
3728 set expected_output [join $expected_lines ""]
3729 gdb_test "${gdb_command}" "${expected_output}" $message
3730 }
3731
3732 # Test the output of "help COMMAND_CLASS". EXPECTED_INITIAL_LINES
3733 # are regular expressions that should match the beginning of output,
3734 # before the list of commands in that class. The presence of
3735 # command list and standard epilogue will be tested automatically.
3736 proc test_class_help { command_class expected_initial_lines args } {
3737 set l_stock_body {
3738 "List of commands\:.*\[\r\n\]+"
3739 "Type \"help\" followed by command name for full documentation\.\[\r\n\]+"
3740 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+"
3741 "Command name abbreviations are allowed if unambiguous\."
3742 }
3743 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3744
3745 eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
3746 }
3747
3748 # COMMAND_LIST should have either one element -- command to test, or
3749 # two elements -- abbreviated command to test, and full command the first
3750 # element is abbreviation of.
3751 # The command must be a prefix command. EXPECTED_INITIAL_LINES
3752 # are regular expressions that should match the beginning of output,
3753 # before the list of subcommands. The presence of
3754 # subcommand list and standard epilogue will be tested automatically.
3755 proc test_prefix_command_help { command_list expected_initial_lines args } {
3756 set command [lindex $command_list 0]
3757 if {[llength $command_list]>1} {
3758 set full_command [lindex $command_list 1]
3759 } else {
3760 set full_command $command
3761 }
3762 # Use 'list' and not just {} because we want variables to
3763 # be expanded in this list.
3764 set l_stock_body [list\
3765 "List of $full_command subcommands\:.*\[\r\n\]+"\
3766 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
3767 "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
3768 "Command name abbreviations are allowed if unambiguous\."]
3769 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3770 if {[llength $args]>0} {
3771 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
3772 } else {
3773 help_test_raw "help ${command}" $l_entire_body
3774 }
3775 }
3776
3777 # Build executable named EXECUTABLE from specifications that allow
3778 # different options to be passed to different sub-compilations.
3779 # TESTNAME is the name of the test; this is passed to 'untested' if
3780 # something fails.
3781 # OPTIONS is passed to the final link, using gdb_compile.
3782 # ARGS is a flat list of source specifications, of the form:
3783 # { SOURCE1 OPTIONS1 [ SOURCE2 OPTIONS2 ]... }
3784 # Each SOURCE is compiled to an object file using its OPTIONS,
3785 # using gdb_compile.
3786 # Returns 0 on success, -1 on failure.
3787 proc build_executable_from_specs {testname executable options args} {
3788 global subdir
3789 global srcdir
3790
3791 set binfile [standard_output_file $executable]
3792
3793 set objects {}
3794 set i 0
3795 foreach {s local_options} $args {
3796 if { [gdb_compile "${srcdir}/${subdir}/${s}" "${binfile}${i}.o" object $local_options] != "" } {
3797 untested $testname
3798 return -1
3799 }
3800 lappend objects "${binfile}${i}.o"
3801 incr i
3802 }
3803
3804 if { [gdb_compile $objects "${binfile}" executable $options] != "" } {
3805 untested $testname
3806 return -1
3807 }
3808
3809 set info_options ""
3810 if { [lsearch -exact $options "c++"] >= 0 } {
3811 set info_options "c++"
3812 }
3813 if [get_compiler_info ${info_options}] {
3814 return -1
3815 }
3816 return 0
3817 }
3818
3819 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
3820 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
3821 # to pass to untested, if something is wrong. OPTIONS are passed
3822 # to gdb_compile directly.
3823 proc build_executable { testname executable {sources ""} {options {debug}} } {
3824 if {[llength $sources]==0} {
3825 set sources ${executable}.c
3826 }
3827
3828 set arglist [list $testname $executable $options]
3829 foreach source $sources {
3830 lappend arglist $source $options
3831 }
3832
3833 return [eval build_executable_from_specs $arglist]
3834 }
3835
3836 # Starts fresh GDB binary and loads EXECUTABLE into GDB. EXECUTABLE is
3837 # the basename of the binary.
3838 proc clean_restart { executable } {
3839 global srcdir
3840 global subdir
3841 set binfile [standard_output_file ${executable}]
3842
3843 gdb_exit
3844 gdb_start
3845 gdb_reinitialize_dir $srcdir/$subdir
3846 gdb_load ${binfile}
3847 }
3848
3849 # Prepares for testing by calling build_executable_full, then
3850 # clean_restart.
3851 # TESTNAME is the name of the test.
3852 # Each element in ARGS is a list of the form
3853 # { EXECUTABLE OPTIONS SOURCE_SPEC... }
3854 # These are passed to build_executable_from_specs, which see.
3855 # The last EXECUTABLE is passed to clean_restart.
3856 # Returns 0 on success, non-zero on failure.
3857 proc prepare_for_testing_full {testname args} {
3858 foreach spec $args {
3859 if {[eval build_executable_from_specs [list $testname] $spec] == -1} {
3860 return -1
3861 }
3862 set executable [lindex $spec 0]
3863 }
3864 clean_restart $executable
3865 return 0
3866 }
3867
3868 # Prepares for testing, by calling build_executable, and then clean_restart.
3869 # Please refer to build_executable for parameter description.
3870 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
3871
3872 if {[build_executable $testname $executable $sources $options] == -1} {
3873 return -1
3874 }
3875 clean_restart $executable
3876
3877 return 0
3878 }
3879
3880 proc get_valueof { fmt exp default } {
3881 global gdb_prompt
3882
3883 set test "get valueof \"${exp}\""
3884 set val ${default}
3885 gdb_test_multiple "print${fmt} ${exp}" "$test" {
3886 -re "\\$\[0-9\]* = (.*)\[\r\n\]*$gdb_prompt $" {
3887 set val $expect_out(1,string)
3888 pass "$test ($val)"
3889 }
3890 timeout {
3891 fail "$test (timeout)"
3892 }
3893 }
3894 return ${val}
3895 }
3896
3897 proc get_integer_valueof { exp default } {
3898 global gdb_prompt
3899
3900 set test "get integer valueof \"${exp}\""
3901 set val ${default}
3902 gdb_test_multiple "print /d ${exp}" "$test" {
3903 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
3904 set val $expect_out(1,string)
3905 pass "$test ($val)"
3906 }
3907 timeout {
3908 fail "$test (timeout)"
3909 }
3910 }
3911 return ${val}
3912 }
3913
3914 proc get_hexadecimal_valueof { exp default } {
3915 global gdb_prompt
3916 send_gdb "print /x ${exp}\n"
3917 set test "get hexadecimal valueof \"${exp}\""
3918 gdb_expect {
3919 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
3920 set val $expect_out(1,string)
3921 pass "$test"
3922 }
3923 timeout {
3924 set val ${default}
3925 fail "$test (timeout)"
3926 }
3927 }
3928 return ${val}
3929 }
3930
3931 proc get_sizeof { type default } {
3932 return [get_integer_valueof "sizeof (${type})" $default]
3933 }
3934
3935 # Get the current value for remotetimeout and return it.
3936 proc get_remotetimeout { } {
3937 global gdb_prompt
3938 global decimal
3939
3940 gdb_test_multiple "show remotetimeout" "" {
3941 -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" {
3942 return $expect_out(1,string);
3943 }
3944 }
3945
3946 # Pick the default that gdb uses
3947 warning "Unable to read remotetimeout"
3948 return 300
3949 }
3950
3951 # Set the remotetimeout to the specified timeout. Nothing is returned.
3952 proc set_remotetimeout { timeout } {
3953 global gdb_prompt
3954
3955 gdb_test_multiple "set remotetimeout $timeout" "" {
3956 -re "$gdb_prompt $" {
3957 verbose "Set remotetimeout to $timeout\n"
3958 }
3959 }
3960 }
3961
3962 # Log gdb command line and script if requested.
3963 if {[info exists TRANSCRIPT]} {
3964 rename send_gdb real_send_gdb
3965 rename remote_spawn real_remote_spawn
3966 rename remote_close real_remote_close
3967
3968 global gdb_transcript
3969 set gdb_transcript ""
3970
3971 global gdb_trans_count
3972 set gdb_trans_count 1
3973
3974 proc remote_spawn {args} {
3975 global gdb_transcript gdb_trans_count outdir
3976
3977 if {$gdb_transcript != ""} {
3978 close $gdb_transcript
3979 }
3980 set gdb_transcript [open [file join $outdir transcript.$gdb_trans_count] w]
3981 puts $gdb_transcript [lindex $args 1]
3982 incr gdb_trans_count
3983
3984 return [uplevel real_remote_spawn $args]
3985 }
3986
3987 proc remote_close {args} {
3988 global gdb_transcript
3989
3990 if {$gdb_transcript != ""} {
3991 close $gdb_transcript
3992 set gdb_transcript ""
3993 }
3994
3995 return [uplevel real_remote_close $args]
3996 }
3997
3998 proc send_gdb {args} {
3999 global gdb_transcript
4000
4001 if {$gdb_transcript != ""} {
4002 puts -nonewline $gdb_transcript [lindex $args 0]
4003 }
4004
4005 return [uplevel real_send_gdb $args]
4006 }
4007 }
4008
4009 proc core_find {binfile {deletefiles {}} {arg ""}} {
4010 global objdir subdir
4011
4012 set destcore "$binfile.core"
4013 file delete $destcore
4014
4015 # Create a core file named "$destcore" rather than just "core", to
4016 # avoid problems with sys admin types that like to regularly prune all
4017 # files named "core" from the system.
4018 #
4019 # Arbitrarily try setting the core size limit to "unlimited" since
4020 # this does not hurt on systems where the command does not work and
4021 # allows us to generate a core on systems where it does.
4022 #
4023 # Some systems append "core" to the name of the program; others append
4024 # the name of the program to "core"; still others (like Linux, as of
4025 # May 2003) create cores named "core.PID". In the latter case, we
4026 # could have many core files lying around, and it may be difficult to
4027 # tell which one is ours, so let's run the program in a subdirectory.
4028 set found 0
4029 set coredir [standard_output_file coredir.[getpid]]
4030 file mkdir $coredir
4031 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
4032 # remote_exec host "${binfile}"
4033 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
4034 if [remote_file build exists $i] {
4035 remote_exec build "mv $i $destcore"
4036 set found 1
4037 }
4038 }
4039 # Check for "core.PID".
4040 if { $found == 0 } {
4041 set names [glob -nocomplain -directory $coredir core.*]
4042 if {[llength $names] == 1} {
4043 set corefile [file join $coredir [lindex $names 0]]
4044 remote_exec build "mv $corefile $destcore"
4045 set found 1
4046 }
4047 }
4048 if { $found == 0 } {
4049 # The braindamaged HPUX shell quits after the ulimit -c above
4050 # without executing ${binfile}. So we try again without the
4051 # ulimit here if we didn't find a core file above.
4052 # Oh, I should mention that any "braindamaged" non-Unix system has
4053 # the same problem. I like the cd bit too, it's really neat'n stuff.
4054 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
4055 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
4056 if [remote_file build exists $i] {
4057 remote_exec build "mv $i $destcore"
4058 set found 1
4059 }
4060 }
4061 }
4062
4063 # Try to clean up after ourselves.
4064 foreach deletefile $deletefiles {
4065 remote_file build delete [file join $coredir $deletefile]
4066 }
4067 remote_exec build "rmdir $coredir"
4068
4069 if { $found == 0 } {
4070 warning "can't generate a core file - core tests suppressed - check ulimit -c"
4071 return ""
4072 }
4073 return $destcore
4074 }
4075
4076 # gdb_target_symbol_prefix_flags returns a string that can be added
4077 # to gdb_compile options to define SYMBOL_PREFIX macro value
4078 # symbol_prefix_flags returns a string that can be added
4079 # for targets that use underscore as symbol prefix.
4080 # TODO: find out automatically if the target needs this.
4081
4082 proc gdb_target_symbol_prefix_flags {} {
4083 if { [istarget "*-*-cygwin*"] || [istarget "i?86-*-mingw*"]
4084 || [istarget "*-*-msdosdjgpp*"] || [istarget "*-*-go32*"] } {
4085 return "additional_flags=-DSYMBOL_PREFIX=\"_\""
4086 } else {
4087 return ""
4088 }
4089 }
4090
4091 # Always load compatibility stuff.
4092 load_lib future.exp