97e79af2cd49169ea46906058f5d496427462796
[deliverable/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 (timed out)."
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 (end of file)"
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 set last_loaded_file $arg
1222
1223 # Set whether debug info was found.
1224 # Default to "fail".
1225 global gdb_file_cmd_debug_info
1226 set gdb_file_cmd_debug_info "fail"
1227
1228 if [is_remote host] {
1229 set arg [remote_download host $arg]
1230 if { $arg == "" } {
1231 perror "download failed"
1232 return -1
1233 }
1234 }
1235
1236 # The file command used to kill the remote target. For the benefit
1237 # of the testsuite, preserve this behavior.
1238 send_gdb "kill\n"
1239 gdb_expect 120 {
1240 -re "Kill the program being debugged. .y or n. $" {
1241 send_gdb "y\n"
1242 verbose "\t\tKilling previous program being debugged"
1243 exp_continue
1244 }
1245 -re "$gdb_prompt $" {
1246 # OK.
1247 }
1248 }
1249
1250 send_gdb "file $arg\n"
1251 gdb_expect 120 {
1252 -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" {
1253 verbose "\t\tLoaded $arg into the $GDB with no debugging symbols"
1254 set gdb_file_cmd_debug_info "nodebug"
1255 return 0
1256 }
1257 -re "Reading symbols from.*done.*$gdb_prompt $" {
1258 verbose "\t\tLoaded $arg into the $GDB"
1259 set gdb_file_cmd_debug_info "debug"
1260 return 0
1261 }
1262 -re "Load new symbol table from \".*\".*y or n. $" {
1263 send_gdb "y\n"
1264 gdb_expect 120 {
1265 -re "Reading symbols from.*done.*$gdb_prompt $" {
1266 verbose "\t\tLoaded $arg with new symbol table into $GDB"
1267 set gdb_file_cmd_debug_info "debug"
1268 return 0
1269 }
1270 timeout {
1271 perror "(timeout) Couldn't load $arg, other program already loaded."
1272 return -1
1273 }
1274 }
1275 }
1276 -re "No such file or directory.*$gdb_prompt $" {
1277 perror "($arg) No such file or directory"
1278 return -1
1279 }
1280 -re "$gdb_prompt $" {
1281 perror "couldn't load $arg into $GDB."
1282 return -1
1283 }
1284 timeout {
1285 perror "couldn't load $arg into $GDB (timed out)."
1286 return -1
1287 }
1288 eof {
1289 # This is an attempt to detect a core dump, but seems not to
1290 # work. Perhaps we need to match .* followed by eof, in which
1291 # gdb_expect does not seem to have a way to do that.
1292 perror "couldn't load $arg into $GDB (end of file)."
1293 return -1
1294 }
1295 }
1296 }
1297
1298 #
1299 # start gdb -- start gdb running, default procedure
1300 #
1301 # When running over NFS, particularly if running many simultaneous
1302 # tests on different hosts all using the same server, things can
1303 # get really slow. Give gdb at least 3 minutes to start up.
1304 #
1305 proc default_gdb_start { } {
1306 global verbose use_gdb_stub
1307 global GDB
1308 global INTERNAL_GDBFLAGS GDBFLAGS
1309 global gdb_prompt
1310 global timeout
1311 global gdb_spawn_id;
1312
1313 gdb_stop_suppressing_tests;
1314
1315 # Set the default value, it may be overriden later by specific testfile.
1316 #
1317 # Use `set_board_info use_gdb_stub' for the board file to flag the inferior
1318 # is already started after connecting and run/attach are not supported.
1319 # This is used for the "remote" protocol. After GDB starts you should
1320 # check global $use_gdb_stub instead of the board as the testfile may force
1321 # a specific different target protocol itself.
1322 set use_gdb_stub [target_info exists use_gdb_stub]
1323
1324 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1325
1326 if [info exists gdb_spawn_id] {
1327 return 0;
1328 }
1329
1330 if ![is_remote host] {
1331 if { [which $GDB] == 0 } then {
1332 perror "$GDB does not exist."
1333 exit 1
1334 }
1335 }
1336 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]"];
1337 if { $res < 0 || $res == "" } {
1338 perror "Spawning $GDB failed."
1339 return 1;
1340 }
1341 gdb_expect 360 {
1342 -re "\[\r\n\]$gdb_prompt $" {
1343 verbose "GDB initialized."
1344 }
1345 -re "$gdb_prompt $" {
1346 perror "GDB never initialized."
1347 return -1
1348 }
1349 timeout {
1350 perror "(timeout) GDB never initialized after 10 seconds."
1351 remote_close host;
1352 return -1
1353 }
1354 }
1355 set gdb_spawn_id -1;
1356 # force the height to "unlimited", so no pagers get used
1357
1358 send_gdb "set height 0\n"
1359 gdb_expect 10 {
1360 -re "$gdb_prompt $" {
1361 verbose "Setting height to 0." 2
1362 }
1363 timeout {
1364 warning "Couldn't set the height to 0"
1365 }
1366 }
1367 # force the width to "unlimited", so no wraparound occurs
1368 send_gdb "set width 0\n"
1369 gdb_expect 10 {
1370 -re "$gdb_prompt $" {
1371 verbose "Setting width to 0." 2
1372 }
1373 timeout {
1374 warning "Couldn't set the width to 0."
1375 }
1376 }
1377 return 0;
1378 }
1379
1380 # Examine the output of compilation to determine whether compilation
1381 # failed or not. If it failed determine whether it is due to missing
1382 # compiler or due to compiler error. Report pass, fail or unsupported
1383 # as appropriate
1384
1385 proc gdb_compile_test {src output} {
1386 if { $output == "" } {
1387 pass "compilation [file tail $src]"
1388 } elseif { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output] } {
1389 unsupported "compilation [file tail $src]"
1390 } elseif { [regexp {.*: command not found[\r|\n]*$} $output] } {
1391 unsupported "compilation [file tail $src]"
1392 } elseif { [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } {
1393 unsupported "compilation [file tail $src]"
1394 } else {
1395 verbose -log "compilation failed: $output" 2
1396 fail "compilation [file tail $src]"
1397 }
1398 }
1399
1400 # Return a 1 for configurations for which we don't even want to try to
1401 # test C++.
1402
1403 proc skip_cplus_tests {} {
1404 if { [istarget "h8300-*-*"] } {
1405 return 1
1406 }
1407
1408 # The C++ IO streams are too large for HC11/HC12 and are thus not
1409 # available. The gdb C++ tests use them and don't compile.
1410 if { [istarget "m6811-*-*"] } {
1411 return 1
1412 }
1413 if { [istarget "m6812-*-*"] } {
1414 return 1
1415 }
1416 return 0
1417 }
1418
1419 # Return a 1 for configurations for which don't have both C++ and the STL.
1420
1421 proc skip_stl_tests {} {
1422 # Symbian supports the C++ language, but the STL is missing
1423 # (both headers and libraries).
1424 if { [istarget "arm*-*-symbianelf*"] } {
1425 return 1
1426 }
1427
1428 return [skip_cplus_tests]
1429 }
1430
1431 # Return a 1 if I don't even want to try to test FORTRAN.
1432
1433 proc skip_fortran_tests {} {
1434 return 0
1435 }
1436
1437 # Return a 1 if I don't even want to try to test ada.
1438
1439 proc skip_ada_tests {} {
1440 return 0
1441 }
1442
1443 # Return a 1 if I don't even want to try to test GO.
1444
1445 proc skip_go_tests {} {
1446 return 0
1447 }
1448
1449 # Return a 1 if I don't even want to try to test java.
1450
1451 proc skip_java_tests {} {
1452 return 0
1453 }
1454
1455 # Return a 1 for configurations that do not support Python scripting.
1456
1457 proc skip_python_tests {} {
1458 global gdb_prompt
1459 gdb_test_multiple "python print 'test'" "verify python support" {
1460 -re "not supported.*$gdb_prompt $" {
1461 unsupported "Python support is disabled."
1462 return 1
1463 }
1464 -re "$gdb_prompt $" {}
1465 }
1466
1467 return 0
1468 }
1469
1470 # Return a 1 if we should skip shared library tests.
1471
1472 proc skip_shlib_tests {} {
1473 # Run the shared library tests on native systems.
1474 if {[isnative]} {
1475 return 0
1476 }
1477
1478 # An abbreviated list of remote targets where we should be able to
1479 # run shared library tests.
1480 if {([istarget *-*-linux*]
1481 || [istarget *-*-*bsd*]
1482 || [istarget *-*-solaris2*]
1483 || [istarget arm*-*-symbianelf*]
1484 || [istarget *-*-mingw*]
1485 || [istarget *-*-cygwin*]
1486 || [istarget *-*-pe*])} {
1487 return 0
1488 }
1489
1490 return 1
1491 }
1492
1493 # Test files shall make sure all the test result lines in gdb.sum are
1494 # unique in a test run, so that comparing the gdb.sum files of two
1495 # test runs gives correct results. Test files that exercise
1496 # variations of the same tests more than once, shall prefix the
1497 # different test invocations with different identifying strings in
1498 # order to make them unique.
1499 #
1500 # About test prefixes:
1501 #
1502 # $pf_prefix is the string that dejagnu prints after the result (FAIL,
1503 # PASS, etc.), and before the test message/name in gdb.sum. E.g., the
1504 # underlined substring in
1505 #
1506 # PASS: gdb.base/mytest.exp: some test
1507 # ^^^^^^^^^^^^^^^^^^^^
1508 #
1509 # is $pf_prefix.
1510 #
1511 # The easiest way to adjust the test prefix is to append a test
1512 # variation prefix to the $pf_prefix, using the with_test_prefix
1513 # procedure. E.g.,
1514 #
1515 # proc do_tests {} {
1516 # gdb_test ... ... "test foo"
1517 # gdb_test ... ... "test bar"
1518 #
1519 # with_test_prefix "subvariation a" {
1520 # gdb_test ... ... "test x"
1521 # }
1522 #
1523 # with_test_prefix "subvariation b" {
1524 # gdb_test ... ... "test x"
1525 # }
1526 # }
1527 #
1528 # with_test_prefix "variation1" {
1529 # ...do setup for variation 1...
1530 # do_tests
1531 # }
1532 #
1533 # with_test_prefix "variation2" {
1534 # ...do setup for variation 2...
1535 # do_tests
1536 # }
1537 #
1538 # Results in:
1539 #
1540 # PASS: gdb.base/mytest.exp: variation1: test foo
1541 # PASS: gdb.base/mytest.exp: variation1: test bar
1542 # PASS: gdb.base/mytest.exp: variation1: subvariation a: test x
1543 # PASS: gdb.base/mytest.exp: variation1: subvariation b: test x
1544 # PASS: gdb.base/mytest.exp: variation2: test foo
1545 # PASS: gdb.base/mytest.exp: variation2: test bar
1546 # PASS: gdb.base/mytest.exp: variation2: subvariation a: test x
1547 # PASS: gdb.base/mytest.exp: variation2: subvariation b: test x
1548 #
1549 # If for some reason more flexibility is necessary, one can also
1550 # manipulate the pf_prefix global directly, treating it as a string.
1551 # E.g.,
1552 #
1553 # global pf_prefix
1554 # set saved_pf_prefix
1555 # append pf_prefix "${foo}: bar"
1556 # ... actual tests ...
1557 # set pf_prefix $saved_pf_prefix
1558 #
1559
1560 # Run BODY in the context of the caller, with the current test prefix
1561 # (pf_prefix) appended with one space, then PREFIX, and then a colon.
1562 # Returns the result of BODY.
1563 #
1564 proc with_test_prefix { prefix body } {
1565 global pf_prefix
1566
1567 set saved $pf_prefix
1568 append pf_prefix " " $prefix ":"
1569 set code [catch {uplevel 1 $body} result]
1570 set pf_prefix $saved
1571
1572 if {$code == 1} {
1573 global errorInfo errorCode
1574 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
1575 } else {
1576 return -code $code $result
1577 }
1578 }
1579
1580 # Return 1 if _Complex types are supported, otherwise, return 0.
1581
1582 proc support_complex_tests {} {
1583 global support_complex_tests_saved
1584
1585 # Use the cached value, if it exists.
1586 if [info exists support_complex_tests_saved] {
1587 verbose "returning saved $support_complex_tests_saved" 2
1588 return $support_complex_tests_saved
1589 }
1590
1591 # Set up, compile, and execute a test program containing _Complex types.
1592 # Include the current process ID in the file names to prevent conflicts
1593 # with invocations for multiple testsuites.
1594 set src complex[pid].c
1595 set exe complex[pid].x
1596
1597 set f [open $src "w"]
1598 puts $f "int main() {"
1599 puts $f "_Complex float cf;"
1600 puts $f "_Complex double cd;"
1601 puts $f "_Complex long double cld;"
1602 puts $f " return 0; }"
1603 close $f
1604
1605 verbose "compiling testfile $src" 2
1606 set compile_flags {debug nowarnings quiet}
1607 set lines [gdb_compile $src $exe executable $compile_flags]
1608 file delete $src
1609 file delete $exe
1610
1611 if ![string match "" $lines] then {
1612 verbose "testfile compilation failed, returning 0" 2
1613 set support_complex_tests_saved 0
1614 } else {
1615 set support_complex_tests_saved 1
1616 }
1617
1618 return $support_complex_tests_saved
1619 }
1620
1621 # Return 1 if target hardware or OS supports single stepping to signal
1622 # handler, otherwise, return 0.
1623
1624 proc can_single_step_to_signal_handler {} {
1625
1626 # Targets don't have hardware single step. On these targets, when
1627 # a signal is delivered during software single step, gdb is unable
1628 # to determine the next instruction addresses, because start of signal
1629 # handler is one of them.
1630 if { [istarget "arm*-*-*"] || [istarget "mips*-*-*"]
1631 || [istarget "tic6x-*-*"] || [istarget "sparc*-*-linux*"] } {
1632 return 0
1633 }
1634
1635 return 1
1636 }
1637
1638 # Return 1 if target supports process record, otherwise return 0.
1639
1640 proc supports_process_record {} {
1641
1642 if [target_info exists gdb,use_precord] {
1643 return [target_info gdb,use_precord]
1644 }
1645
1646 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"] } {
1647 return 1
1648 }
1649
1650 return 0
1651 }
1652
1653 # Return 1 if target supports reverse debugging, otherwise return 0.
1654
1655 proc supports_reverse {} {
1656
1657 if [target_info exists gdb,can_reverse] {
1658 return [target_info gdb,can_reverse]
1659 }
1660
1661 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"] } {
1662 return 1
1663 }
1664
1665 return 0
1666 }
1667
1668 # Return 1 if target is ILP32.
1669 # This cannot be decided simply from looking at the target string,
1670 # as it might depend on externally passed compiler options like -m64.
1671 proc is_ilp32_target {} {
1672 global is_ilp32_target_saved
1673
1674 # Use the cached value, if it exists. Cache value per "board" to handle
1675 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1676 set me "is_ilp32_target"
1677 set board [target_info name]
1678 if [info exists is_ilp32_target_saved($board)] {
1679 verbose "$me: returning saved $is_ilp32_target_saved($board)" 2
1680 return $is_ilp32_target_saved($board)
1681 }
1682
1683
1684 set src ilp32[pid].c
1685 set obj ilp32[pid].o
1686
1687 set f [open $src "w"]
1688 puts $f "int dummy\[sizeof (int) == 4"
1689 puts $f " && sizeof (void *) == 4"
1690 puts $f " && sizeof (long) == 4 ? 1 : -1\];"
1691 close $f
1692
1693 verbose "$me: compiling testfile $src" 2
1694 set lines [gdb_compile $src $obj object {quiet}]
1695 file delete $src
1696 file delete $obj
1697
1698 if ![string match "" $lines] then {
1699 verbose "$me: testfile compilation failed, returning 0" 2
1700 return [set is_ilp32_target_saved($board) 0]
1701 }
1702
1703 verbose "$me: returning 1" 2
1704 return [set is_ilp32_target_saved($board) 1]
1705 }
1706
1707 # Return 1 if target is LP64.
1708 # This cannot be decided simply from looking at the target string,
1709 # as it might depend on externally passed compiler options like -m64.
1710 proc is_lp64_target {} {
1711 global is_lp64_target_saved
1712
1713 # Use the cached value, if it exists. Cache value per "board" to handle
1714 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1715 set me "is_lp64_target"
1716 set board [target_info name]
1717 if [info exists is_lp64_target_saved($board)] {
1718 verbose "$me: returning saved $is_lp64_target_saved($board)" 2
1719 return $is_lp64_target_saved($board)
1720 }
1721
1722 set src lp64[pid].c
1723 set obj lp64[pid].o
1724
1725 set f [open $src "w"]
1726 puts $f "int dummy\[sizeof (int) == 4"
1727 puts $f " && sizeof (void *) == 8"
1728 puts $f " && sizeof (long) == 8 ? 1 : -1\];"
1729 close $f
1730
1731 verbose "$me: compiling testfile $src" 2
1732 set lines [gdb_compile $src $obj object {quiet}]
1733 file delete $src
1734 file delete $obj
1735
1736 if ![string match "" $lines] then {
1737 verbose "$me: testfile compilation failed, returning 0" 2
1738 return [set is_lp64_target_saved($board) 0]
1739 }
1740
1741 verbose "$me: returning 1" 2
1742 return [set is_lp64_target_saved($board) 1]
1743 }
1744
1745 # Return 1 if target has x86_64 registers - either amd64 or x32.
1746 # x32 target identifies as x86_64-*-linux*, therefore it cannot be determined
1747 # just from the target string.
1748 proc is_amd64_regs_target {} {
1749 global is_amd64_regs_target_saved
1750
1751 if {![istarget "x86_64-*-*"] && ![istarget "i?86-*"]} {
1752 return 0
1753 }
1754
1755 # Use the cached value, if it exists. Cache value per "board" to handle
1756 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1757 set me "is_amd64_regs_target"
1758 set board [target_info name]
1759 if [info exists is_amd64_regs_target_saved($board)] {
1760 verbose "$me: returning saved $is_amd64_regs_target_saved($board)" 2
1761 return $is_amd64_regs_target_saved($board)
1762 }
1763
1764 set src reg64[pid].s
1765 set obj reg64[pid].o
1766
1767 set f [open $src "w"]
1768 foreach reg \
1769 {rax rbx rcx rdx rsi rdi rbp rsp r8 r9 r10 r11 r12 r13 r14 r15} {
1770 puts $f "\tincq %$reg"
1771 }
1772 close $f
1773
1774 verbose "$me: compiling testfile $src" 2
1775 set lines [gdb_compile $src $obj object {quiet}]
1776 file delete $src
1777 file delete $obj
1778
1779 if ![string match "" $lines] then {
1780 verbose "$me: testfile compilation failed, returning 0" 2
1781 return [set is_amd64_regs_target_saved($board) 0]
1782 }
1783
1784 verbose "$me: returning 1" 2
1785 return [set is_amd64_regs_target_saved($board) 1]
1786 }
1787
1788 # Return 1 if this target is an x86 or x86-64 with -m32.
1789 proc is_x86_like_target {} {
1790 if {![istarget "x86_64-*-*"] && ![istarget i?86-*]} {
1791 return 0
1792 }
1793 return [expr [is_ilp32_target] && ![is_amd64_regs_target]]
1794 }
1795
1796 # Return 1 if displaced stepping is supported on target, otherwise, return 0.
1797 proc support_displaced_stepping {} {
1798
1799 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"]
1800 || [istarget "arm*-*-linux*"] || [istarget "powerpc-*-linux*"]
1801 || [istarget "powerpc64-*-linux*"] || [istarget "s390*-*-*"] } {
1802 return 1
1803 }
1804
1805 return 0
1806 }
1807
1808 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1809 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1810
1811 proc skip_altivec_tests {} {
1812 global skip_vmx_tests_saved
1813 global srcdir subdir gdb_prompt inferior_exited_re
1814
1815 # Use the cached value, if it exists.
1816 set me "skip_altivec_tests"
1817 if [info exists skip_vmx_tests_saved] {
1818 verbose "$me: returning saved $skip_vmx_tests_saved" 2
1819 return $skip_vmx_tests_saved
1820 }
1821
1822 # Some simulators are known to not support VMX instructions.
1823 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1824 verbose "$me: target known to not support VMX, returning 1" 2
1825 return [set skip_vmx_tests_saved 1]
1826 }
1827
1828 # Make sure we have a compiler that understands altivec.
1829 set compile_flags {debug nowarnings}
1830 if [get_compiler_info] {
1831 warning "Could not get compiler info"
1832 return 1
1833 }
1834 if [test_compiler_info gcc*] {
1835 set compile_flags "$compile_flags additional_flags=-maltivec"
1836 } elseif [test_compiler_info xlc*] {
1837 set compile_flags "$compile_flags additional_flags=-qaltivec"
1838 } else {
1839 verbose "Could not compile with altivec support, returning 1" 2
1840 return 1
1841 }
1842
1843 # Set up, compile, and execute a test program containing VMX instructions.
1844 # Include the current process ID in the file names to prevent conflicts
1845 # with invocations for multiple testsuites.
1846 set src vmx[pid].c
1847 set exe vmx[pid].x
1848
1849 set f [open $src "w"]
1850 puts $f "int main() {"
1851 puts $f "#ifdef __MACH__"
1852 puts $f " asm volatile (\"vor v0,v0,v0\");"
1853 puts $f "#else"
1854 puts $f " asm volatile (\"vor 0,0,0\");"
1855 puts $f "#endif"
1856 puts $f " return 0; }"
1857 close $f
1858
1859 verbose "$me: compiling testfile $src" 2
1860 set lines [gdb_compile $src $exe executable $compile_flags]
1861 file delete $src
1862
1863 if ![string match "" $lines] then {
1864 verbose "$me: testfile compilation failed, returning 1" 2
1865 return [set skip_vmx_tests_saved 1]
1866 }
1867
1868 # No error message, compilation succeeded so now run it via gdb.
1869
1870 gdb_exit
1871 gdb_start
1872 gdb_reinitialize_dir $srcdir/$subdir
1873 gdb_load "$exe"
1874 gdb_run_cmd
1875 gdb_expect {
1876 -re ".*Illegal instruction.*${gdb_prompt} $" {
1877 verbose -log "\n$me altivec hardware not detected"
1878 set skip_vmx_tests_saved 1
1879 }
1880 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
1881 verbose -log "\n$me: altivec hardware detected"
1882 set skip_vmx_tests_saved 0
1883 }
1884 default {
1885 warning "\n$me: default case taken"
1886 set skip_vmx_tests_saved 1
1887 }
1888 }
1889 gdb_exit
1890 remote_file build delete $exe
1891
1892 verbose "$me: returning $skip_vmx_tests_saved" 2
1893 return $skip_vmx_tests_saved
1894 }
1895
1896 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1897 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1898
1899 proc skip_vsx_tests {} {
1900 global skip_vsx_tests_saved
1901 global srcdir subdir gdb_prompt inferior_exited_re
1902
1903 # Use the cached value, if it exists.
1904 set me "skip_vsx_tests"
1905 if [info exists skip_vsx_tests_saved] {
1906 verbose "$me: returning saved $skip_vsx_tests_saved" 2
1907 return $skip_vsx_tests_saved
1908 }
1909
1910 # Some simulators are known to not support Altivec instructions, so
1911 # they won't support VSX instructions as well.
1912 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1913 verbose "$me: target known to not support VSX, returning 1" 2
1914 return [set skip_vsx_tests_saved 1]
1915 }
1916
1917 # Make sure we have a compiler that understands altivec.
1918 set compile_flags {debug nowarnings quiet}
1919 if [get_compiler_info] {
1920 warning "Could not get compiler info"
1921 return 1
1922 }
1923 if [test_compiler_info gcc*] {
1924 set compile_flags "$compile_flags additional_flags=-mvsx"
1925 } elseif [test_compiler_info xlc*] {
1926 set compile_flags "$compile_flags additional_flags=-qasm=gcc"
1927 } else {
1928 verbose "Could not compile with vsx support, returning 1" 2
1929 return 1
1930 }
1931
1932 set src vsx[pid].c
1933 set exe vsx[pid].x
1934
1935 set f [open $src "w"]
1936 puts $f "int main() {"
1937 puts $f " double a\[2\] = { 1.0, 2.0 };"
1938 puts $f "#ifdef __MACH__"
1939 puts $f " asm volatile (\"lxvd2x v0,v0,%\[addr\]\" : : \[addr\] \"r\" (a));"
1940 puts $f "#else"
1941 puts $f " asm volatile (\"lxvd2x 0,0,%\[addr\]\" : : \[addr\] \"r\" (a));"
1942 puts $f "#endif"
1943 puts $f " return 0; }"
1944 close $f
1945
1946 verbose "$me: compiling testfile $src" 2
1947 set lines [gdb_compile $src $exe executable $compile_flags]
1948 file delete $src
1949
1950 if ![string match "" $lines] then {
1951 verbose "$me: testfile compilation failed, returning 1" 2
1952 return [set skip_vsx_tests_saved 1]
1953 }
1954
1955 # No error message, compilation succeeded so now run it via gdb.
1956
1957 gdb_exit
1958 gdb_start
1959 gdb_reinitialize_dir $srcdir/$subdir
1960 gdb_load "$exe"
1961 gdb_run_cmd
1962 gdb_expect {
1963 -re ".*Illegal instruction.*${gdb_prompt} $" {
1964 verbose -log "\n$me VSX hardware not detected"
1965 set skip_vsx_tests_saved 1
1966 }
1967 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
1968 verbose -log "\n$me: VSX hardware detected"
1969 set skip_vsx_tests_saved 0
1970 }
1971 default {
1972 warning "\n$me: default case taken"
1973 set skip_vsx_tests_saved 1
1974 }
1975 }
1976 gdb_exit
1977 remote_file build delete $exe
1978
1979 verbose "$me: returning $skip_vsx_tests_saved" 2
1980 return $skip_vsx_tests_saved
1981 }
1982
1983 # Skip all the tests in the file if you are not on an hppa running
1984 # hpux target.
1985
1986 proc skip_hp_tests {} {
1987 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
1988 verbose "Skip hp tests is $skip_hp"
1989 return $skip_hp
1990 }
1991
1992 # Return whether we should skip tests for showing inlined functions in
1993 # backtraces. Requires get_compiler_info and get_debug_format.
1994
1995 proc skip_inline_frame_tests {} {
1996 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
1997 if { ! [test_debug_format "DWARF 2"] } {
1998 return 1
1999 }
2000
2001 # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line.
2002 if { ([test_compiler_info "gcc-2-*"]
2003 || [test_compiler_info "gcc-3-*"]
2004 || [test_compiler_info "gcc-4-0-*"]) } {
2005 return 1
2006 }
2007
2008 return 0
2009 }
2010
2011 # Return whether we should skip tests for showing variables from
2012 # inlined functions. Requires get_compiler_info and get_debug_format.
2013
2014 proc skip_inline_var_tests {} {
2015 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
2016 if { ! [test_debug_format "DWARF 2"] } {
2017 return 1
2018 }
2019
2020 return 0
2021 }
2022
2023 # Return a 1 if we should skip tests that require hardware breakpoints
2024
2025 proc skip_hw_breakpoint_tests {} {
2026 # Skip tests if requested by the board (note that no_hardware_watchpoints
2027 # disables both watchpoints and breakpoints)
2028 if { [target_info exists gdb,no_hardware_watchpoints]} {
2029 return 1
2030 }
2031
2032 # These targets support hardware breakpoints natively
2033 if { [istarget "i?86-*-*"]
2034 || [istarget "x86_64-*-*"]
2035 || [istarget "ia64-*-*"]
2036 || [istarget "arm*-*-*"]} {
2037 return 0
2038 }
2039
2040 return 1
2041 }
2042
2043 # Return a 1 if we should skip tests that require hardware watchpoints
2044
2045 proc skip_hw_watchpoint_tests {} {
2046 # Skip tests if requested by the board
2047 if { [target_info exists gdb,no_hardware_watchpoints]} {
2048 return 1
2049 }
2050
2051 # These targets support hardware watchpoints natively
2052 if { [istarget "i?86-*-*"]
2053 || [istarget "x86_64-*-*"]
2054 || [istarget "ia64-*-*"]
2055 || [istarget "arm*-*-*"]
2056 || [istarget "powerpc*-*-linux*"]
2057 || [istarget "s390*-*-*"] } {
2058 return 0
2059 }
2060
2061 return 1
2062 }
2063
2064 # Return a 1 if we should skip tests that require *multiple* hardware
2065 # watchpoints to be active at the same time
2066
2067 proc skip_hw_watchpoint_multi_tests {} {
2068 if { [skip_hw_watchpoint_tests] } {
2069 return 1
2070 }
2071
2072 # These targets support just a single hardware watchpoint
2073 if { [istarget "arm*-*-*"]
2074 || [istarget "powerpc*-*-linux*"] } {
2075 return 1
2076 }
2077
2078 return 0
2079 }
2080
2081 # Return a 1 if we should skip tests that require read/access watchpoints
2082
2083 proc skip_hw_watchpoint_access_tests {} {
2084 if { [skip_hw_watchpoint_tests] } {
2085 return 1
2086 }
2087
2088 # These targets support just write watchpoints
2089 if { [istarget "s390*-*-*"] } {
2090 return 1
2091 }
2092
2093 return 0
2094 }
2095
2096 # Return 1 if we should skip tests that require the runtime unwinder
2097 # hook. This must be invoked while gdb is running, after shared
2098 # libraries have been loaded. This is needed because otherwise a
2099 # shared libgcc won't be visible.
2100
2101 proc skip_unwinder_tests {} {
2102 global gdb_prompt
2103
2104 set ok 1
2105 gdb_test_multiple "print _Unwind_DebugHook" "check for unwinder hook" {
2106 -re "= .*no debug info.*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
2107 set ok 0
2108 }
2109 -re "= .*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
2110 }
2111 -re "No symbol .* in current context.\r\n$gdb_prompt $" {
2112 set ok 0
2113 }
2114 }
2115 if {!$ok} {
2116 gdb_test_multiple "info probe" "check for stap probe in unwinder" {
2117 -re ".*libgcc.*unwind.*\r\n$gdb_prompt $" {
2118 set ok 1
2119 }
2120 -re "\r\n$gdb_prompt $" {
2121 }
2122 }
2123 }
2124 return $ok
2125 }
2126
2127 set compiler_info "unknown"
2128 set gcc_compiled 0
2129 set hp_cc_compiler 0
2130 set hp_aCC_compiler 0
2131
2132 # Figure out what compiler I am using.
2133 #
2134 # ARG can be empty or "C++". If empty, "C" is assumed.
2135 #
2136 # There are several ways to do this, with various problems.
2137 #
2138 # [ gdb_compile -E $ifile -o $binfile.ci ]
2139 # source $binfile.ci
2140 #
2141 # Single Unix Spec v3 says that "-E -o ..." together are not
2142 # specified. And in fact, the native compiler on hp-ux 11 (among
2143 # others) does not work with "-E -o ...". Most targets used to do
2144 # this, and it mostly worked, because it works with gcc.
2145 #
2146 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
2147 # source $binfile.ci
2148 #
2149 # This avoids the problem with -E and -o together. This almost works
2150 # if the build machine is the same as the host machine, which is
2151 # usually true of the targets which are not gcc. But this code does
2152 # not figure which compiler to call, and it always ends up using the C
2153 # compiler. Not good for setting hp_aCC_compiler. Targets
2154 # hppa*-*-hpux* and mips*-*-irix* used to do this.
2155 #
2156 # [ gdb_compile -E $ifile > $binfile.ci ]
2157 # source $binfile.ci
2158 #
2159 # dejagnu target_compile says that it supports output redirection,
2160 # but the code is completely different from the normal path and I
2161 # don't want to sweep the mines from that path. So I didn't even try
2162 # this.
2163 #
2164 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
2165 # eval $cppout
2166 #
2167 # I actually do this for all targets now. gdb_compile runs the right
2168 # compiler, and TCL captures the output, and I eval the output.
2169 #
2170 # Unfortunately, expect logs the output of the command as it goes by,
2171 # and dejagnu helpfully prints a second copy of it right afterwards.
2172 # So I turn off expect logging for a moment.
2173 #
2174 # [ gdb_compile $ifile $ciexe_file executable $args ]
2175 # [ remote_exec $ciexe_file ]
2176 # [ source $ci_file.out ]
2177 #
2178 # I could give up on -E and just do this.
2179 # I didn't get desperate enough to try this.
2180 #
2181 # -- chastain 2004-01-06
2182
2183 proc get_compiler_info {{arg ""}} {
2184 # For compiler.c and compiler.cc
2185 global srcdir
2186
2187 # I am going to play with the log to keep noise out.
2188 global outdir
2189 global tool
2190
2191 # These come from compiler.c or compiler.cc
2192 global compiler_info
2193
2194 # Legacy global data symbols.
2195 global gcc_compiled
2196 global hp_cc_compiler
2197 global hp_aCC_compiler
2198
2199 # Choose which file to preprocess.
2200 set ifile "${srcdir}/lib/compiler.c"
2201 if { $arg == "c++" } {
2202 set ifile "${srcdir}/lib/compiler.cc"
2203 }
2204
2205 # Run $ifile through the right preprocessor.
2206 # Toggle gdb.log to keep the compiler output out of the log.
2207 log_file
2208 if [is_remote host] {
2209 # We have to use -E and -o together, despite the comments
2210 # above, because of how DejaGnu handles remote host testing.
2211 set ppout "$outdir/compiler.i"
2212 gdb_compile "${ifile}" "$ppout" preprocess [list "$arg" quiet]
2213 set file [open $ppout r]
2214 set cppout [read $file]
2215 close $file
2216 } else {
2217 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$arg" quiet] ]
2218 }
2219 log_file -a "$outdir/$tool.log"
2220
2221 # Eval the output.
2222 set unknown 0
2223 foreach cppline [ split "$cppout" "\n" ] {
2224 if { [ regexp "^#" "$cppline" ] } {
2225 # line marker
2226 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
2227 # blank line
2228 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
2229 # eval this line
2230 verbose "get_compiler_info: $cppline" 2
2231 eval "$cppline"
2232 } else {
2233 # unknown line
2234 verbose -log "get_compiler_info: $cppline"
2235 set unknown 1
2236 }
2237 }
2238
2239 # Reset to unknown compiler if any diagnostics happened.
2240 if { $unknown } {
2241 set compiler_info "unknown"
2242 }
2243
2244 # Set the legacy symbols.
2245 set gcc_compiled 0
2246 set hp_cc_compiler 0
2247 set hp_aCC_compiler 0
2248 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
2249 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
2250 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
2251 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
2252 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
2253 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
2254 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
2255
2256 # Log what happened.
2257 verbose -log "get_compiler_info: $compiler_info"
2258
2259 # Most compilers will evaluate comparisons and other boolean
2260 # operations to 0 or 1.
2261 uplevel \#0 { set true 1 }
2262 uplevel \#0 { set false 0 }
2263
2264 # Use of aCC results in boolean results being displayed as
2265 # "true" or "false"
2266 if { $hp_aCC_compiler } {
2267 uplevel \#0 { set true true }
2268 uplevel \#0 { set false false }
2269 }
2270
2271 return 0;
2272 }
2273
2274 proc test_compiler_info { {compiler ""} } {
2275 global compiler_info
2276
2277 # if no arg, return the compiler_info string
2278
2279 if [string match "" $compiler] {
2280 if [info exists compiler_info] {
2281 return $compiler_info
2282 } else {
2283 perror "No compiler info found."
2284 }
2285 }
2286
2287 return [string match $compiler $compiler_info]
2288 }
2289
2290 proc current_target_name { } {
2291 global target_info
2292 if [info exists target_info(target,name)] {
2293 set answer $target_info(target,name)
2294 } else {
2295 set answer ""
2296 }
2297 return $answer
2298 }
2299
2300 set gdb_wrapper_initialized 0
2301 set gdb_wrapper_target ""
2302
2303 proc gdb_wrapper_init { args } {
2304 global gdb_wrapper_initialized;
2305 global gdb_wrapper_file;
2306 global gdb_wrapper_flags;
2307 global gdb_wrapper_target
2308
2309 if { $gdb_wrapper_initialized == 1 } { return; }
2310
2311 if {[target_info exists needs_status_wrapper] && \
2312 [target_info needs_status_wrapper] != "0"} {
2313 set result [build_wrapper "testglue.o"];
2314 if { $result != "" } {
2315 set gdb_wrapper_file [lindex $result 0];
2316 set gdb_wrapper_flags [lindex $result 1];
2317 } else {
2318 warning "Status wrapper failed to build."
2319 }
2320 }
2321 set gdb_wrapper_initialized 1
2322 set gdb_wrapper_target [current_target_name]
2323 }
2324
2325 # Some targets need to always link a special object in. Save its path here.
2326 global gdb_saved_set_unbuffered_mode_obj
2327 set gdb_saved_set_unbuffered_mode_obj ""
2328
2329 proc gdb_compile {source dest type options} {
2330 global GDB_TESTCASE_OPTIONS;
2331 global gdb_wrapper_file;
2332 global gdb_wrapper_flags;
2333 global gdb_wrapper_initialized;
2334 global srcdir
2335 global objdir
2336 global gdb_saved_set_unbuffered_mode_obj
2337
2338 set outdir [file dirname $dest]
2339
2340 # Add platform-specific options if a shared library was specified using
2341 # "shlib=librarypath" in OPTIONS.
2342 set new_options ""
2343 set shlib_found 0
2344 set shlib_load 0
2345 foreach opt $options {
2346 if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
2347 if [test_compiler_info "xlc-*"] {
2348 # IBM xlc compiler doesn't accept shared library named other
2349 # than .so: use "-Wl," to bypass this
2350 lappend source "-Wl,$shlib_name"
2351 } elseif { ([istarget "*-*-mingw*"]
2352 || [istarget *-*-cygwin*]
2353 || [istarget *-*-pe*])} {
2354 lappend source "${shlib_name}.a"
2355 } else {
2356 lappend source $shlib_name
2357 }
2358 if { $shlib_found == 0 } {
2359 set shlib_found 1
2360 if { ([istarget "*-*-mingw*"]
2361 || [istarget *-*-cygwin*]) } {
2362 lappend new_options "additional_flags=-Wl,--enable-auto-import"
2363 }
2364 }
2365 } elseif { $opt == "shlib_load" } {
2366 set shlib_load 1
2367 } else {
2368 lappend new_options $opt
2369 }
2370 }
2371
2372 # We typically link to shared libraries using an absolute path, and
2373 # that's how they are found at runtime. If we are going to
2374 # dynamically load one by basename, we must specify rpath. If we
2375 # are using a remote host, DejaGNU will link to the shared library
2376 # using a relative path, so again we must specify an rpath.
2377 if { $shlib_load || ($shlib_found && [is_remote target]) } {
2378 if { ([istarget "*-*-mingw*"]
2379 || [istarget *-*-cygwin*]
2380 || [istarget *-*-pe*]
2381 || [istarget hppa*-*-hpux*])} {
2382 # Do not need anything.
2383 } elseif { [istarget *-*-freebsd*] || [istarget *-*-openbsd*] } {
2384 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
2385 } elseif { [istarget arm*-*-symbianelf*] } {
2386 if { $shlib_load } {
2387 lappend new_options "libs=-ldl"
2388 }
2389 } else {
2390 if { $shlib_load } {
2391 lappend new_options "libs=-ldl"
2392 }
2393 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
2394 }
2395 }
2396 set options $new_options
2397
2398 if [target_info exists is_vxworks] {
2399 set options2 { "additional_flags=-Dvxworks" }
2400 set options [concat $options2 $options]
2401 }
2402 if [info exists GDB_TESTCASE_OPTIONS] {
2403 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
2404 }
2405 verbose "options are $options"
2406 verbose "source is $source $dest $type $options"
2407
2408 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
2409
2410 if {[target_info exists needs_status_wrapper] && \
2411 [target_info needs_status_wrapper] != "0" && \
2412 [info exists gdb_wrapper_file]} {
2413 lappend options "libs=${gdb_wrapper_file}"
2414 lappend options "ldflags=${gdb_wrapper_flags}"
2415 }
2416
2417 # Replace the "nowarnings" option with the appropriate additional_flags
2418 # to disable compiler warnings.
2419 set nowarnings [lsearch -exact $options nowarnings]
2420 if {$nowarnings != -1} {
2421 if [target_info exists gdb,nowarnings_flag] {
2422 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
2423 } else {
2424 set flag "additional_flags=-w"
2425 }
2426 set options [lreplace $options $nowarnings $nowarnings $flag]
2427 }
2428
2429 if { $type == "executable" } {
2430 if { ([istarget "*-*-mingw*"]
2431 || [istarget "*-*-*djgpp"]
2432 || [istarget "*-*-cygwin*"])} {
2433 # Force output to unbuffered mode, by linking in an object file
2434 # with a global contructor that calls setvbuf.
2435 #
2436 # Compile the special object seperatelly for two reasons:
2437 # 1) Insulate it from $options.
2438 # 2) Avoid compiling it for every gdb_compile invocation,
2439 # which is time consuming, especially if we're remote
2440 # host testing.
2441 #
2442 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
2443 verbose "compiling gdb_saved_set_unbuffered_obj"
2444 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
2445 set unbuf_obj ${objdir}/set_unbuffered_mode.o
2446
2447 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
2448 if { $result != "" } {
2449 return $result
2450 }
2451
2452 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
2453 # Link a copy of the output object, because the
2454 # original may be automatically deleted.
2455 remote_exec host "cp -f $unbuf_obj $gdb_saved_set_unbuffered_mode_obj"
2456 } else {
2457 verbose "gdb_saved_set_unbuffered_obj already compiled"
2458 }
2459
2460 # Rely on the internal knowledge that the global ctors are ran in
2461 # reverse link order. In that case, we can use ldflags to
2462 # avoid copying the object file to the host multiple
2463 # times.
2464 # This object can only be added if standard libraries are
2465 # used. Thus, we need to disable it if -nostdlib option is used
2466 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
2467 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
2468 }
2469 }
2470 }
2471
2472 set result [target_compile $source $dest $type $options];
2473
2474 # Prune uninteresting compiler (and linker) output.
2475 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
2476
2477 regsub "\[\r\n\]*$" "$result" "" result;
2478 regsub "^\[\r\n\]*" "$result" "" result;
2479
2480 if {[lsearch $options quiet] < 0} {
2481 # We shall update this on a per language basis, to avoid
2482 # changing the entire testsuite in one go.
2483 if {[lsearch $options f77] >= 0} {
2484 gdb_compile_test $source $result
2485 } elseif { $result != "" } {
2486 clone_output "gdb compile failed, $result"
2487 }
2488 }
2489 return $result;
2490 }
2491
2492
2493 # This is just like gdb_compile, above, except that it tries compiling
2494 # against several different thread libraries, to see which one this
2495 # system has.
2496 proc gdb_compile_pthreads {source dest type options} {
2497 set built_binfile 0
2498 set why_msg "unrecognized error"
2499 foreach lib {-lpthreads -lpthread -lthread ""} {
2500 # This kind of wipes out whatever libs the caller may have
2501 # set. Or maybe theirs will override ours. How infelicitous.
2502 set options_with_lib [concat $options [list libs=$lib quiet]]
2503 set ccout [gdb_compile $source $dest $type $options_with_lib]
2504 switch -regexp -- $ccout {
2505 ".*no posix threads support.*" {
2506 set why_msg "missing threads include file"
2507 break
2508 }
2509 ".*cannot open -lpthread.*" {
2510 set why_msg "missing runtime threads library"
2511 }
2512 ".*Can't find library for -lpthread.*" {
2513 set why_msg "missing runtime threads library"
2514 }
2515 {^$} {
2516 pass "successfully compiled posix threads test case"
2517 set built_binfile 1
2518 break
2519 }
2520 }
2521 }
2522 if {!$built_binfile} {
2523 unsupported "Couldn't compile $source: ${why_msg}"
2524 return -1
2525 }
2526 }
2527
2528 # Build a shared library from SOURCES. You must use get_compiler_info
2529 # first.
2530
2531 proc gdb_compile_shlib {sources dest options} {
2532 set obj_options $options
2533
2534 switch -glob [test_compiler_info] {
2535 "xlc-*" {
2536 lappend obj_options "additional_flags=-qpic"
2537 }
2538 "gcc-*" {
2539 if { !([istarget "powerpc*-*-aix*"]
2540 || [istarget "rs6000*-*-aix*"]
2541 || [istarget "*-*-cygwin*"]
2542 || [istarget "*-*-mingw*"]
2543 || [istarget "*-*-pe*"]) } {
2544 lappend obj_options "additional_flags=-fpic"
2545 }
2546 }
2547 default {
2548 switch -glob [istarget] {
2549 "hppa*-hp-hpux*" {
2550 lappend obj_options "additional_flags=+z"
2551 }
2552 "mips-sgi-irix*" {
2553 # Disable SGI compiler's implicit -Dsgi
2554 lappend obj_options "additional_flags=-Usgi"
2555 }
2556 default {
2557 # don't know what the compiler is...
2558 }
2559 }
2560 }
2561 }
2562
2563 set outdir [file dirname $dest]
2564 set objects ""
2565 foreach source $sources {
2566 set sourcebase [file tail $source]
2567 if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
2568 return -1
2569 }
2570 lappend objects ${outdir}/${sourcebase}.o
2571 }
2572
2573 if [istarget "hppa*-*-hpux*"] {
2574 remote_exec build "ld -b ${objects} -o ${dest}"
2575 } else {
2576 set link_options $options
2577 if [test_compiler_info "xlc-*"] {
2578 lappend link_options "additional_flags=-qmkshrobj"
2579 } else {
2580 lappend link_options "additional_flags=-shared"
2581
2582 if { ([istarget "*-*-mingw*"]
2583 || [istarget *-*-cygwin*]
2584 || [istarget *-*-pe*])} {
2585 lappend link_options "additional_flags=-Wl,--out-implib,${dest}.a"
2586 } elseif [is_remote target] {
2587 # By default, we do not set the soname. This causes the linker
2588 # on ELF systems to create a DT_NEEDED entry in the executable
2589 # refering to the full path name of the library. This is a
2590 # problem in remote testing if the library is in a different
2591 # directory there. To fix this, we set a soname of just the
2592 # base filename for the library, and add an appropriate -rpath
2593 # to the main executable (in gdb_compile).
2594 set destbase [file tail $dest]
2595 lappend link_options "additional_flags=-Wl,-soname,$destbase"
2596 }
2597 }
2598 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
2599 return -1
2600 }
2601 }
2602 }
2603
2604 # This is just like gdb_compile_shlib, above, except that it tries compiling
2605 # against several different thread libraries, to see which one this
2606 # system has.
2607 proc gdb_compile_shlib_pthreads {sources dest options} {
2608 set built_binfile 0
2609 set why_msg "unrecognized error"
2610 foreach lib {-lpthreads -lpthread -lthread ""} {
2611 # This kind of wipes out whatever libs the caller may have
2612 # set. Or maybe theirs will override ours. How infelicitous.
2613 set options_with_lib [concat $options [list libs=$lib quiet]]
2614 set ccout [gdb_compile_shlib $sources $dest $options_with_lib]
2615 switch -regexp -- $ccout {
2616 ".*no posix threads support.*" {
2617 set why_msg "missing threads include file"
2618 break
2619 }
2620 ".*cannot open -lpthread.*" {
2621 set why_msg "missing runtime threads library"
2622 }
2623 ".*Can't find library for -lpthread.*" {
2624 set why_msg "missing runtime threads library"
2625 }
2626 {^$} {
2627 pass "successfully compiled posix threads test case"
2628 set built_binfile 1
2629 break
2630 }
2631 }
2632 }
2633 if {!$built_binfile} {
2634 unsupported "Couldn't compile $sources: ${why_msg}"
2635 return -1
2636 }
2637 }
2638
2639 # This is just like gdb_compile_pthreads, above, except that we always add the
2640 # objc library for compiling Objective-C programs
2641 proc gdb_compile_objc {source dest type options} {
2642 set built_binfile 0
2643 set why_msg "unrecognized error"
2644 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
2645 # This kind of wipes out whatever libs the caller may have
2646 # set. Or maybe theirs will override ours. How infelicitous.
2647 if { $lib == "solaris" } {
2648 set lib "-lpthread -lposix4"
2649 }
2650 if { $lib != "-lobjc" } {
2651 set lib "-lobjc $lib"
2652 }
2653 set options_with_lib [concat $options [list libs=$lib quiet]]
2654 set ccout [gdb_compile $source $dest $type $options_with_lib]
2655 switch -regexp -- $ccout {
2656 ".*no posix threads support.*" {
2657 set why_msg "missing threads include file"
2658 break
2659 }
2660 ".*cannot open -lpthread.*" {
2661 set why_msg "missing runtime threads library"
2662 }
2663 ".*Can't find library for -lpthread.*" {
2664 set why_msg "missing runtime threads library"
2665 }
2666 {^$} {
2667 pass "successfully compiled objc with posix threads test case"
2668 set built_binfile 1
2669 break
2670 }
2671 }
2672 }
2673 if {!$built_binfile} {
2674 unsupported "Couldn't compile $source: ${why_msg}"
2675 return -1
2676 }
2677 }
2678
2679 proc send_gdb { string } {
2680 global suppress_flag;
2681 if { $suppress_flag } {
2682 return "suppressed";
2683 }
2684 return [remote_send host "$string"];
2685 }
2686
2687 #
2688 #
2689
2690 proc gdb_expect { args } {
2691 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
2692 set atimeout [lindex $args 0];
2693 set expcode [list [lindex $args 1]];
2694 } else {
2695 set expcode $args;
2696 }
2697
2698 upvar timeout timeout;
2699
2700 if [target_info exists gdb,timeout] {
2701 if [info exists timeout] {
2702 if { $timeout < [target_info gdb,timeout] } {
2703 set gtimeout [target_info gdb,timeout];
2704 } else {
2705 set gtimeout $timeout;
2706 }
2707 } else {
2708 set gtimeout [target_info gdb,timeout];
2709 }
2710 }
2711
2712 if ![info exists gtimeout] {
2713 global timeout;
2714 if [info exists timeout] {
2715 set gtimeout $timeout;
2716 }
2717 }
2718
2719 if [info exists atimeout] {
2720 if { ![info exists gtimeout] || $gtimeout < $atimeout } {
2721 set gtimeout $atimeout;
2722 }
2723 } else {
2724 if ![info exists gtimeout] {
2725 # Eeeeew.
2726 set gtimeout 60;
2727 }
2728 }
2729
2730 global suppress_flag;
2731 global remote_suppress_flag;
2732 if [info exists remote_suppress_flag] {
2733 set old_val $remote_suppress_flag;
2734 }
2735 if [info exists suppress_flag] {
2736 if { $suppress_flag } {
2737 set remote_suppress_flag 1;
2738 }
2739 }
2740 set code [catch \
2741 {uplevel remote_expect host $gtimeout $expcode} string];
2742 if [info exists old_val] {
2743 set remote_suppress_flag $old_val;
2744 } else {
2745 if [info exists remote_suppress_flag] {
2746 unset remote_suppress_flag;
2747 }
2748 }
2749
2750 if {$code == 1} {
2751 global errorInfo errorCode;
2752
2753 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
2754 } else {
2755 return -code $code $string
2756 }
2757 }
2758
2759 # gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
2760 #
2761 # Check for long sequence of output by parts.
2762 # TEST: is the test message to be printed with the test success/fail.
2763 # SENTINEL: Is the terminal pattern indicating that output has finished.
2764 # LIST: is the sequence of outputs to match.
2765 # If the sentinel is recognized early, it is considered an error.
2766 #
2767 # Returns:
2768 # 1 if the test failed,
2769 # 0 if the test passes,
2770 # -1 if there was an internal error.
2771
2772 proc gdb_expect_list {test sentinel list} {
2773 global gdb_prompt
2774 global suppress_flag
2775 set index 0
2776 set ok 1
2777 if { $suppress_flag } {
2778 set ok 0
2779 unresolved "${test}"
2780 }
2781 while { ${index} < [llength ${list}] } {
2782 set pattern [lindex ${list} ${index}]
2783 set index [expr ${index} + 1]
2784 verbose -log "gdb_expect_list pattern: /$pattern/" 2
2785 if { ${index} == [llength ${list}] } {
2786 if { ${ok} } {
2787 gdb_expect {
2788 -re "${pattern}${sentinel}" {
2789 # pass "${test}, pattern ${index} + sentinel"
2790 }
2791 -re "${sentinel}" {
2792 fail "${test} (pattern ${index} + sentinel)"
2793 set ok 0
2794 }
2795 -re ".*A problem internal to GDB has been detected" {
2796 fail "${test} (GDB internal error)"
2797 set ok 0
2798 gdb_internal_error_resync
2799 }
2800 timeout {
2801 fail "${test} (pattern ${index} + sentinel) (timeout)"
2802 set ok 0
2803 }
2804 }
2805 } else {
2806 # unresolved "${test}, pattern ${index} + sentinel"
2807 }
2808 } else {
2809 if { ${ok} } {
2810 gdb_expect {
2811 -re "${pattern}" {
2812 # pass "${test}, pattern ${index}"
2813 }
2814 -re "${sentinel}" {
2815 fail "${test} (pattern ${index})"
2816 set ok 0
2817 }
2818 -re ".*A problem internal to GDB has been detected" {
2819 fail "${test} (GDB internal error)"
2820 set ok 0
2821 gdb_internal_error_resync
2822 }
2823 timeout {
2824 fail "${test} (pattern ${index}) (timeout)"
2825 set ok 0
2826 }
2827 }
2828 } else {
2829 # unresolved "${test}, pattern ${index}"
2830 }
2831 }
2832 }
2833 if { ${ok} } {
2834 pass "${test}"
2835 return 0
2836 } else {
2837 return 1
2838 }
2839 }
2840
2841 #
2842 #
2843 proc gdb_suppress_entire_file { reason } {
2844 global suppress_flag;
2845
2846 warning "$reason\n";
2847 set suppress_flag -1;
2848 }
2849
2850 #
2851 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
2852 # gdb_expect to fail immediately (until the next call to
2853 # gdb_stop_suppressing_tests).
2854 #
2855 proc gdb_suppress_tests { args } {
2856 global suppress_flag;
2857
2858 return; # fnf - disable pending review of results where
2859 # testsuite ran better without this
2860 incr suppress_flag;
2861
2862 if { $suppress_flag == 1 } {
2863 if { [llength $args] > 0 } {
2864 warning "[lindex $args 0]\n";
2865 } else {
2866 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
2867 }
2868 }
2869 }
2870
2871 #
2872 # Clear suppress_flag.
2873 #
2874 proc gdb_stop_suppressing_tests { } {
2875 global suppress_flag;
2876
2877 if [info exists suppress_flag] {
2878 if { $suppress_flag > 0 } {
2879 set suppress_flag 0;
2880 clone_output "Tests restarted.\n";
2881 }
2882 } else {
2883 set suppress_flag 0;
2884 }
2885 }
2886
2887 proc gdb_clear_suppressed { } {
2888 global suppress_flag;
2889
2890 set suppress_flag 0;
2891 }
2892
2893 proc gdb_start { } {
2894 default_gdb_start
2895 }
2896
2897 proc gdb_exit { } {
2898 catch default_gdb_exit
2899 }
2900
2901 #
2902 # gdb_load_cmd -- load a file into the debugger.
2903 # ARGS - additional args to load command.
2904 # return a -1 if anything goes wrong.
2905 #
2906 proc gdb_load_cmd { args } {
2907 global gdb_prompt
2908
2909 if [target_info exists gdb_load_timeout] {
2910 set loadtimeout [target_info gdb_load_timeout]
2911 } else {
2912 set loadtimeout 1600
2913 }
2914 send_gdb "load $args\n"
2915 verbose "Timeout is now $loadtimeout seconds" 2
2916 gdb_expect $loadtimeout {
2917 -re "Loading section\[^\r\]*\r\n" {
2918 exp_continue
2919 }
2920 -re "Start address\[\r\]*\r\n" {
2921 exp_continue
2922 }
2923 -re "Transfer rate\[\r\]*\r\n" {
2924 exp_continue
2925 }
2926 -re "Memory access error\[^\r\]*\r\n" {
2927 perror "Failed to load program"
2928 return -1
2929 }
2930 -re "$gdb_prompt $" {
2931 return 0
2932 }
2933 -re "(.*)\r\n$gdb_prompt " {
2934 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
2935 return -1
2936 }
2937 timeout {
2938 perror "Timed out trying to load $args."
2939 return -1
2940 }
2941 }
2942 return -1
2943 }
2944
2945 # Return the filename to download to the target and load on the target
2946 # for this shared library. Normally just LIBNAME, unless shared libraries
2947 # for this target have separate link and load images.
2948
2949 proc shlib_target_file { libname } {
2950 return $libname
2951 }
2952
2953 # Return the filename GDB will load symbols from when debugging this
2954 # shared library. Normally just LIBNAME, unless shared libraries for
2955 # this target have separate link and load images.
2956
2957 proc shlib_symbol_file { libname } {
2958 return $libname
2959 }
2960
2961 # Return the filename to download to the target and load for this
2962 # executable. Normally just BINFILE unless it is renamed to something
2963 # else for this target.
2964
2965 proc exec_target_file { binfile } {
2966 return $binfile
2967 }
2968
2969 # Return the filename GDB will load symbols from when debugging this
2970 # executable. Normally just BINFILE unless executables for this target
2971 # have separate files for symbols.
2972
2973 proc exec_symbol_file { binfile } {
2974 return $binfile
2975 }
2976
2977 # Rename the executable file. Normally this is just BINFILE1 being renamed
2978 # to BINFILE2, but some targets require multiple binary files.
2979 proc gdb_rename_execfile { binfile1 binfile2 } {
2980 file rename -force [exec_target_file ${binfile1}] \
2981 [exec_target_file ${binfile2}]
2982 if { [exec_target_file ${binfile1}] != [exec_symbol_file ${binfile1}] } {
2983 file rename -force [exec_symbol_file ${binfile1}] \
2984 [exec_symbol_file ${binfile2}]
2985 }
2986 }
2987
2988 # "Touch" the executable file to update the date. Normally this is just
2989 # BINFILE, but some targets require multiple files.
2990 proc gdb_touch_execfile { binfile } {
2991 set time [clock seconds]
2992 file mtime [exec_target_file ${binfile}] $time
2993 if { [exec_target_file ${binfile}] != [exec_symbol_file ${binfile}] } {
2994 file mtime [exec_symbol_file ${binfile}] $time
2995 }
2996 }
2997
2998 # gdb_download
2999 #
3000 # Copy a file to the remote target and return its target filename.
3001 # Schedule the file to be deleted at the end of this test.
3002
3003 proc gdb_download { filename } {
3004 global cleanfiles
3005
3006 set destname [remote_download target $filename]
3007 lappend cleanfiles $destname
3008 return $destname
3009 }
3010
3011 # gdb_load_shlibs LIB...
3012 #
3013 # Copy the listed libraries to the target.
3014
3015 proc gdb_load_shlibs { args } {
3016 if {![is_remote target]} {
3017 return
3018 }
3019
3020 foreach file $args {
3021 gdb_download [shlib_target_file $file]
3022 }
3023
3024 # Even if the target supplies full paths for shared libraries,
3025 # they may not be paths for this system.
3026 gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "" ""
3027 }
3028
3029 #
3030 # gdb_load -- load a file into the debugger.
3031 # Many files in config/*.exp override this procedure.
3032 #
3033 proc gdb_load { arg } {
3034 return [gdb_file_cmd $arg]
3035 }
3036
3037 # gdb_reload -- load a file into the target. Called before "running",
3038 # either the first time or after already starting the program once,
3039 # for remote targets. Most files that override gdb_load should now
3040 # override this instead.
3041
3042 proc gdb_reload { } {
3043 # For the benefit of existing configurations, default to gdb_load.
3044 # Specifying no file defaults to the executable currently being
3045 # debugged.
3046 return [gdb_load ""]
3047 }
3048
3049 proc gdb_continue { function } {
3050 global decimal
3051
3052 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
3053 }
3054
3055 proc default_gdb_init { args } {
3056 global gdb_wrapper_initialized
3057 global gdb_wrapper_target
3058 global gdb_test_file_name
3059 global cleanfiles
3060
3061 set cleanfiles {}
3062
3063 gdb_clear_suppressed;
3064
3065 set gdb_test_file_name [file rootname [file tail [lindex $args 0]]]
3066
3067 # Make sure that the wrapper is rebuilt
3068 # with the appropriate multilib option.
3069 if { $gdb_wrapper_target != [current_target_name] } {
3070 set gdb_wrapper_initialized 0
3071 }
3072
3073 # Unlike most tests, we have a small number of tests that generate
3074 # a very large amount of output. We therefore increase the expect
3075 # buffer size to be able to contain the entire test output.
3076 match_max -d 30000
3077 # Also set this value for the currently running GDB.
3078 match_max [match_max -d]
3079
3080 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
3081 if { [llength $args] > 0 } {
3082 global pf_prefix
3083
3084 set file [lindex $args 0];
3085
3086 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
3087 }
3088 global gdb_prompt;
3089 if [target_info exists gdb_prompt] {
3090 set gdb_prompt [target_info gdb_prompt];
3091 } else {
3092 set gdb_prompt "\\(gdb\\)"
3093 }
3094 global use_gdb_stub
3095 if [info exists use_gdb_stub] {
3096 unset use_gdb_stub
3097 }
3098 }
3099
3100 # Turn BASENAME into a full file name in the standard output
3101 # directory. It is ok if BASENAME is the empty string; in this case
3102 # the directory is returned.
3103
3104 proc standard_output_file {basename} {
3105 global objdir subdir
3106
3107 return [file join $objdir $subdir $basename]
3108 }
3109
3110 # Set 'testfile', 'srcfile', and 'binfile'.
3111 #
3112 # ARGS is a list of source file specifications.
3113 # Without any arguments, the .exp file's base name is used to
3114 # compute the source file name. The ".c" extension is added in this case.
3115 # If ARGS is not empty, each entry is a source file specification.
3116 # If the specification starts with a ".", it is treated as a suffix
3117 # to append to the .exp file's base name.
3118 # If the specification is the empty string, it is treated as if it
3119 # were ".c".
3120 # Otherwise it is a file name.
3121 # The first file in the list is used to set the 'srcfile' global.
3122 # Each subsequent name is used to set 'srcfile2', 'srcfile3', etc.
3123 #
3124 # Most tests should call this without arguments.
3125 #
3126 # If a completely different binary file name is needed, then it
3127 # should be handled in the .exp file with a suitable comment.
3128
3129 proc standard_testfile {args} {
3130 global gdb_test_file_name
3131 global subdir
3132 global gdb_test_file_last_vars
3133
3134 # Outputs.
3135 global testfile binfile
3136
3137 set testfile $gdb_test_file_name
3138 set binfile [standard_output_file ${testfile}]
3139
3140 if {[llength $args] == 0} {
3141 set args .c
3142 }
3143
3144 # Unset our previous output variables.
3145 # This can help catch hidden bugs.
3146 if {[info exists gdb_test_file_last_vars]} {
3147 foreach varname $gdb_test_file_last_vars {
3148 global $varname
3149 catch {unset $varname}
3150 }
3151 }
3152 # 'executable' is often set by tests.
3153 set gdb_test_file_last_vars {executable}
3154
3155 set suffix ""
3156 foreach arg $args {
3157 set varname srcfile$suffix
3158 global $varname
3159
3160 # Handle an extension.
3161 if {$arg == ""} {
3162 set arg $testfile.c
3163 } elseif {[string range $arg 0 0] == "."} {
3164 set arg $testfile$arg
3165 }
3166
3167 set $varname $arg
3168 lappend gdb_test_file_last_vars $varname
3169
3170 if {$suffix == ""} {
3171 set suffix 2
3172 } else {
3173 incr suffix
3174 }
3175 }
3176 }
3177
3178 # The default timeout used when testing GDB commands. We want to use
3179 # the same timeout as the default dejagnu timeout, unless the user has
3180 # already provided a specific value (probably through a site.exp file).
3181 global gdb_test_timeout
3182 if ![info exists gdb_test_timeout] {
3183 set gdb_test_timeout $timeout
3184 }
3185
3186 # A list of global variables that GDB testcases should not use.
3187 # We try to prevent their use by monitoring write accesses and raising
3188 # an error when that happens.
3189 set banned_variables { bug_id prms_id }
3190
3191 # A list of procedures that GDB testcases should not use.
3192 # We try to prevent their use by monitoring invocations and raising
3193 # an error when that happens.
3194 set banned_procedures { strace }
3195
3196 # gdb_init is called by runtest at start, but also by several
3197 # tests directly; gdb_finish is only called from within runtest after
3198 # each test source execution.
3199 # Placing several traces by repetitive calls to gdb_init leads
3200 # to problems, as only one trace is removed in gdb_finish.
3201 # To overcome this possible problem, we add a variable that records
3202 # if the banned variables and procedures are already traced.
3203 set banned_traced 0
3204
3205 proc gdb_init { args } {
3206 # Reset the timeout value to the default. This way, any testcase
3207 # that changes the timeout value without resetting it cannot affect
3208 # the timeout used in subsequent testcases.
3209 global gdb_test_timeout
3210 global timeout
3211 set timeout $gdb_test_timeout
3212
3213 # Block writes to all banned variables, and invocation of all
3214 # banned procedures...
3215 global banned_variables
3216 global banned_procedures
3217 global banned_traced
3218 if (!$banned_traced) {
3219 foreach banned_var $banned_variables {
3220 global "$banned_var"
3221 trace add variable "$banned_var" write error
3222 }
3223 foreach banned_proc $banned_procedures {
3224 global "$banned_proc"
3225 trace add execution "$banned_proc" enter error
3226 }
3227 set banned_traced 1
3228 }
3229
3230 # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same
3231 # messages as expected.
3232 setenv LC_ALL C
3233 setenv LC_CTYPE C
3234 setenv LANG C
3235
3236 # Don't let a .inputrc file or an existing setting of INPUTRC mess up
3237 # the test results. Even if /dev/null doesn't exist on the particular
3238 # platform, the readline library will use the default setting just by
3239 # failing to open the file. OTOH, opening /dev/null successfully will
3240 # also result in the default settings being used since nothing will be
3241 # read from this file.
3242 setenv INPUTRC "/dev/null"
3243
3244 # The gdb.base/readline.exp arrow key test relies on the standard VT100
3245 # bindings, so make sure that an appropriate terminal is selected.
3246 # The same bug doesn't show up if we use ^P / ^N instead.
3247 setenv TERM "vt100"
3248
3249 # Some tests (for example gdb.base/maint.exp) shell out from gdb to use
3250 # grep. Clear GREP_OPTIONS to make the behavoiur predictable,
3251 # especially having color output turned on can cause tests to fail.
3252 setenv GREP_OPTIONS ""
3253
3254 # Clear $gdbserver_reconnect_p.
3255 global gdbserver_reconnect_p
3256 set gdbserver_reconnect_p 1
3257 unset gdbserver_reconnect_p
3258
3259 return [eval default_gdb_init $args];
3260 }
3261
3262 proc gdb_finish { } {
3263 global cleanfiles
3264
3265 # Exit first, so that the files are no longer in use.
3266 gdb_exit
3267
3268 if { [llength $cleanfiles] > 0 } {
3269 eval remote_file target delete $cleanfiles
3270 set cleanfiles {}
3271 }
3272
3273 # Unblock write access to the banned variables. Dejagnu typically
3274 # resets some of them between testcases.
3275 global banned_variables
3276 global banned_procedures
3277 global banned_traced
3278 if ($banned_traced) {
3279 foreach banned_var $banned_variables {
3280 global "$banned_var"
3281 trace remove variable "$banned_var" write error
3282 }
3283 foreach banned_proc $banned_procedures {
3284 global "$banned_proc"
3285 trace remove execution "$banned_proc" enter error
3286 }
3287 set banned_traced 0
3288 }
3289 }
3290
3291 global debug_format
3292 set debug_format "unknown"
3293
3294 # Run the gdb command "info source" and extract the debugging format
3295 # information from the output and save it in debug_format.
3296
3297 proc get_debug_format { } {
3298 global gdb_prompt
3299 global verbose
3300 global expect_out
3301 global debug_format
3302
3303 set debug_format "unknown"
3304 send_gdb "info source\n"
3305 gdb_expect 10 {
3306 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
3307 set debug_format $expect_out(1,string)
3308 verbose "debug format is $debug_format"
3309 return 1;
3310 }
3311 -re "No current source file.\r\n$gdb_prompt $" {
3312 perror "get_debug_format used when no current source file"
3313 return 0;
3314 }
3315 -re "$gdb_prompt $" {
3316 warning "couldn't check debug format (no valid response)."
3317 return 1;
3318 }
3319 timeout {
3320 warning "couldn't check debug format (timed out)."
3321 return 1;
3322 }
3323 }
3324 }
3325
3326 # Return true if FORMAT matches the debug format the current test was
3327 # compiled with. FORMAT is a shell-style globbing pattern; it can use
3328 # `*', `[...]', and so on.
3329 #
3330 # This function depends on variables set by `get_debug_format', above.
3331
3332 proc test_debug_format {format} {
3333 global debug_format
3334
3335 return [expr [string match $format $debug_format] != 0]
3336 }
3337
3338 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
3339 # COFF, stabs, etc). If that format matches the format that the
3340 # current test was compiled with, then the next test is expected to
3341 # fail for any target. Returns 1 if the next test or set of tests is
3342 # expected to fail, 0 otherwise (or if it is unknown). Must have
3343 # previously called get_debug_format.
3344 proc setup_xfail_format { format } {
3345 set ret [test_debug_format $format];
3346
3347 if {$ret} then {
3348 setup_xfail "*-*-*"
3349 }
3350 return $ret;
3351 }
3352
3353 # Like setup_kfail, but only call setup_kfail conditionally if
3354 # istarget[TARGET] returns true.
3355 proc setup_kfail_for_target { PR target } {
3356 if { [istarget $target] } {
3357 setup_kfail $PR $target
3358 }
3359 }
3360
3361 # gdb_get_line_number TEXT [FILE]
3362 #
3363 # Search the source file FILE, and return the line number of the
3364 # first line containing TEXT. If no match is found, an error is thrown.
3365 #
3366 # TEXT is a string literal, not a regular expression.
3367 #
3368 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
3369 # specified, and does not start with "/", then it is assumed to be in
3370 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
3371 # by changing the callers and the interface at the same time.
3372 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
3373 # gdb.base/ena-dis-br.exp.
3374 #
3375 # Use this function to keep your test scripts independent of the
3376 # exact line numbering of the source file. Don't write:
3377 #
3378 # send_gdb "break 20"
3379 #
3380 # This means that if anyone ever edits your test's source file,
3381 # your test could break. Instead, put a comment like this on the
3382 # source file line you want to break at:
3383 #
3384 # /* breakpoint spot: frotz.exp: test name */
3385 #
3386 # and then write, in your test script (which we assume is named
3387 # frotz.exp):
3388 #
3389 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
3390 #
3391 # (Yes, Tcl knows how to handle the nested quotes and brackets.
3392 # Try this:
3393 # $ tclsh
3394 # % puts "foo [lindex "bar baz" 1]"
3395 # foo baz
3396 # %
3397 # Tcl is quite clever, for a little stringy language.)
3398 #
3399 # ===
3400 #
3401 # The previous implementation of this procedure used the gdb search command.
3402 # This version is different:
3403 #
3404 # . It works with MI, and it also works when gdb is not running.
3405 #
3406 # . It operates on the build machine, not the host machine.
3407 #
3408 # . For now, this implementation fakes a current directory of
3409 # $srcdir/$subdir to be compatible with the old implementation.
3410 # This will go away eventually and some callers will need to
3411 # be changed.
3412 #
3413 # . The TEXT argument is literal text and matches literally,
3414 # not a regular expression as it was before.
3415 #
3416 # . State changes in gdb, such as changing the current file
3417 # and setting $_, no longer happen.
3418 #
3419 # After a bit of time we can forget about the differences from the
3420 # old implementation.
3421 #
3422 # --chastain 2004-08-05
3423
3424 proc gdb_get_line_number { text { file "" } } {
3425 global srcdir
3426 global subdir
3427 global srcfile
3428
3429 if { "$file" == "" } then {
3430 set file "$srcfile"
3431 }
3432 if { ! [regexp "^/" "$file"] } then {
3433 set file "$srcdir/$subdir/$file"
3434 }
3435
3436 if { [ catch { set fd [open "$file"] } message ] } then {
3437 error "$message"
3438 }
3439
3440 set found -1
3441 for { set line 1 } { 1 } { incr line } {
3442 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
3443 error "$message"
3444 }
3445 if { $nchar < 0 } then {
3446 break
3447 }
3448 if { [string first "$text" "$body"] >= 0 } then {
3449 set found $line
3450 break
3451 }
3452 }
3453
3454 if { [ catch { close "$fd" } message ] } then {
3455 error "$message"
3456 }
3457
3458 if {$found == -1} {
3459 error "undefined tag \"$text\""
3460 }
3461
3462 return $found
3463 }
3464
3465 # gdb_continue_to_end:
3466 # The case where the target uses stubs has to be handled specially. If a
3467 # stub is used, we set a breakpoint at exit because we cannot rely on
3468 # exit() behavior of a remote target.
3469 #
3470 # MSSG is the error message that gets printed. If not given, a
3471 # default is used.
3472 # COMMAND is the command to invoke. If not given, "continue" is
3473 # used.
3474 # ALLOW_EXTRA is a flag indicating whether the test should expect
3475 # extra output between the "Continuing." line and the program
3476 # exiting. By default it is zero; if nonzero, any extra output
3477 # is accepted.
3478
3479 proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} {
3480 global inferior_exited_re use_gdb_stub
3481
3482 if {$mssg == ""} {
3483 set text "continue until exit"
3484 } else {
3485 set text "continue until exit at $mssg"
3486 }
3487 if {$allow_extra} {
3488 set extra ".*"
3489 } else {
3490 set extra ""
3491 }
3492 if $use_gdb_stub {
3493 if {![gdb_breakpoint "exit"]} {
3494 return 0
3495 }
3496 gdb_test $command "Continuing..*Breakpoint .*exit.*" \
3497 $text
3498 } else {
3499 # Continue until we exit. Should not stop again.
3500 # Don't bother to check the output of the program, that may be
3501 # extremely tough for some remote systems.
3502 gdb_test $command \
3503 "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\
3504 $text
3505 }
3506 }
3507
3508 proc rerun_to_main {} {
3509 global gdb_prompt use_gdb_stub
3510
3511 if $use_gdb_stub {
3512 gdb_run_cmd
3513 gdb_expect {
3514 -re ".*Breakpoint .*main .*$gdb_prompt $"\
3515 {pass "rerun to main" ; return 0}
3516 -re "$gdb_prompt $"\
3517 {fail "rerun to main" ; return 0}
3518 timeout {fail "(timeout) rerun to main" ; return 0}
3519 }
3520 } else {
3521 send_gdb "run\n"
3522 gdb_expect {
3523 -re "The program .* has been started already.*y or n. $" {
3524 send_gdb "y\n"
3525 exp_continue
3526 }
3527 -re "Starting program.*$gdb_prompt $"\
3528 {pass "rerun to main" ; return 0}
3529 -re "$gdb_prompt $"\
3530 {fail "rerun to main" ; return 0}
3531 timeout {fail "(timeout) rerun to main" ; return 0}
3532 }
3533 }
3534 }
3535
3536 # Print a message and return true if a test should be skipped
3537 # due to lack of floating point suport.
3538
3539 proc gdb_skip_float_test { msg } {
3540 if [target_info exists gdb,skip_float_tests] {
3541 verbose "Skipping test '$msg': no float tests.";
3542 return 1;
3543 }
3544 return 0;
3545 }
3546
3547 # Print a message and return true if a test should be skipped
3548 # due to lack of stdio support.
3549
3550 proc gdb_skip_stdio_test { msg } {
3551 if [target_info exists gdb,noinferiorio] {
3552 verbose "Skipping test '$msg': no inferior i/o.";
3553 return 1;
3554 }
3555 return 0;
3556 }
3557
3558 proc gdb_skip_bogus_test { msg } {
3559 return 0;
3560 }
3561
3562 # Return true if a test should be skipped due to lack of XML support
3563 # in the host GDB.
3564 # NOTE: This must be called while gdb is *not* running.
3565
3566 proc gdb_skip_xml_test { } {
3567 global gdb_prompt
3568 global srcdir
3569 global xml_missing_cached
3570
3571 if {[info exists xml_missing_cached]} {
3572 return $xml_missing_cached
3573 }
3574
3575 gdb_start
3576 set xml_missing_cached 0
3577 gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" {
3578 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
3579 set xml_missing_cached 1
3580 }
3581 -re ".*$gdb_prompt $" { }
3582 }
3583 gdb_exit
3584 return $xml_missing_cached
3585 }
3586
3587 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
3588 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
3589 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
3590 # the name of a debuginfo only file. This file will be stored in the same
3591 # subdirectory.
3592
3593 # Functions for separate debug info testing
3594
3595 # starting with an executable:
3596 # foo --> original executable
3597
3598 # at the end of the process we have:
3599 # foo.stripped --> foo w/o debug info
3600 # foo.debug --> foo's debug info
3601 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
3602
3603 # Return the build-id hex string (usually 160 bits as 40 hex characters)
3604 # converted to the form: .build-id/ab/cdef1234...89.debug
3605 # Return "" if no build-id found.
3606 proc build_id_debug_filename_get { exec } {
3607 set tmp "${exec}-tmp"
3608 set objcopy_program [transform objcopy]
3609
3610 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $exec $tmp" output]
3611 verbose "result is $result"
3612 verbose "output is $output"
3613 if {$result == 1} {
3614 return ""
3615 }
3616 set fi [open $tmp]
3617 fconfigure $fi -translation binary
3618 # Skip the NOTE header.
3619 read $fi 16
3620 set data [read $fi]
3621 close $fi
3622 file delete $tmp
3623 if ![string compare $data ""] then {
3624 return ""
3625 }
3626 # Convert it to hex.
3627 binary scan $data H* data
3628 regsub {^..} $data {\0/} data
3629 return ".build-id/${data}.debug";
3630 }
3631
3632 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
3633 # list of optional flags. The only currently supported flag is no-main,
3634 # which removes the symbol entry for main from the separate debug file.
3635 #
3636 # Function returns zero on success. Function will return non-zero failure code
3637 # on some targets not supporting separate debug info (such as i386-msdos).
3638
3639 proc gdb_gnu_strip_debug { dest args } {
3640
3641 # Use the first separate debug info file location searched by GDB so the
3642 # run cannot be broken by some stale file searched with higher precedence.
3643 set debug_file "${dest}.debug"
3644
3645 set strip_to_file_program [transform strip]
3646 set objcopy_program [transform objcopy]
3647
3648 set debug_link [file tail $debug_file]
3649 set stripped_file "${dest}.stripped"
3650
3651 # Get rid of the debug info, and store result in stripped_file
3652 # something like gdb/testsuite/gdb.base/blah.stripped.
3653 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
3654 verbose "result is $result"
3655 verbose "output is $output"
3656 if {$result == 1} {
3657 return 1
3658 }
3659
3660 # Workaround PR binutils/10802:
3661 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3662 set perm [file attributes ${dest} -permissions]
3663 file attributes ${stripped_file} -permissions $perm
3664
3665 # Get rid of everything but the debug info, and store result in debug_file
3666 # This will be in the .debug subdirectory, see above.
3667 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
3668 verbose "result is $result"
3669 verbose "output is $output"
3670 if {$result == 1} {
3671 return 1
3672 }
3673
3674 # If no-main is passed, strip the symbol for main from the separate
3675 # file. This is to simulate the behavior of elfutils's eu-strip, which
3676 # leaves the symtab in the original file only. There's no way to get
3677 # objcopy or strip to remove the symbol table without also removing the
3678 # debugging sections, so this is as close as we can get.
3679 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
3680 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
3681 verbose "result is $result"
3682 verbose "output is $output"
3683 if {$result == 1} {
3684 return 1
3685 }
3686 file delete "${debug_file}"
3687 file rename "${debug_file}-tmp" "${debug_file}"
3688 }
3689
3690 # Link the two previous output files together, adding the .gnu_debuglink
3691 # section to the stripped_file, containing a pointer to the debug_file,
3692 # save the new file in dest.
3693 # This will be the regular executable filename, in the usual location.
3694 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
3695 verbose "result is $result"
3696 verbose "output is $output"
3697 if {$result == 1} {
3698 return 1
3699 }
3700
3701 # Workaround PR binutils/10802:
3702 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3703 set perm [file attributes ${stripped_file} -permissions]
3704 file attributes ${dest} -permissions $perm
3705
3706 return 0
3707 }
3708
3709 # Test the output of GDB_COMMAND matches the pattern obtained
3710 # by concatenating all elements of EXPECTED_LINES. This makes
3711 # it possible to split otherwise very long string into pieces.
3712 # If third argument is not empty, it's used as the name of the
3713 # test to be printed on pass/fail.
3714 proc help_test_raw { gdb_command expected_lines args } {
3715 set message $gdb_command
3716 if [llength $args]>0 then {
3717 set message [lindex $args 0]
3718 }
3719 set expected_output [join $expected_lines ""]
3720 gdb_test "${gdb_command}" "${expected_output}" $message
3721 }
3722
3723 # Test the output of "help COMMAND_CLASS". EXPECTED_INITIAL_LINES
3724 # are regular expressions that should match the beginning of output,
3725 # before the list of commands in that class. The presence of
3726 # command list and standard epilogue will be tested automatically.
3727 proc test_class_help { command_class expected_initial_lines args } {
3728 set l_stock_body {
3729 "List of commands\:.*\[\r\n\]+"
3730 "Type \"help\" followed by command name for full documentation\.\[\r\n\]+"
3731 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+"
3732 "Command name abbreviations are allowed if unambiguous\."
3733 }
3734 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3735
3736 eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
3737 }
3738
3739 # COMMAND_LIST should have either one element -- command to test, or
3740 # two elements -- abbreviated command to test, and full command the first
3741 # element is abbreviation of.
3742 # The command must be a prefix command. EXPECTED_INITIAL_LINES
3743 # are regular expressions that should match the beginning of output,
3744 # before the list of subcommands. The presence of
3745 # subcommand list and standard epilogue will be tested automatically.
3746 proc test_prefix_command_help { command_list expected_initial_lines args } {
3747 set command [lindex $command_list 0]
3748 if {[llength $command_list]>1} {
3749 set full_command [lindex $command_list 1]
3750 } else {
3751 set full_command $command
3752 }
3753 # Use 'list' and not just {} because we want variables to
3754 # be expanded in this list.
3755 set l_stock_body [list\
3756 "List of $full_command subcommands\:.*\[\r\n\]+"\
3757 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
3758 "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
3759 "Command name abbreviations are allowed if unambiguous\."]
3760 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3761 if {[llength $args]>0} {
3762 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
3763 } else {
3764 help_test_raw "help ${command}" $l_entire_body
3765 }
3766 }
3767
3768 # Build executable named EXECUTABLE from specifications that allow
3769 # different options to be passed to different sub-compilations.
3770 # TESTNAME is the name of the test; this is passed to 'untested' if
3771 # something fails.
3772 # OPTIONS is passed to the final link, using gdb_compile.
3773 # ARGS is a flat list of source specifications, of the form:
3774 # { SOURCE1 OPTIONS1 [ SOURCE2 OPTIONS2 ]... }
3775 # Each SOURCE is compiled to an object file using its OPTIONS,
3776 # using gdb_compile.
3777 # Returns 0 on success, -1 on failure.
3778 proc build_executable_from_specs {testname executable options args} {
3779 global subdir
3780 global srcdir
3781
3782 set binfile [standard_output_file $executable]
3783
3784 set objects {}
3785 set i 0
3786 foreach {s local_options} $args {
3787 if { [gdb_compile "${srcdir}/${subdir}/${s}" "${binfile}${i}.o" object $local_options] != "" } {
3788 untested $testname
3789 return -1
3790 }
3791 lappend objects "${binfile}${i}.o"
3792 incr i
3793 }
3794
3795 if { [gdb_compile $objects "${binfile}" executable $options] != "" } {
3796 untested $testname
3797 return -1
3798 }
3799
3800 set info_options ""
3801 if { [lsearch -exact $options "c++"] >= 0 } {
3802 set info_options "c++"
3803 }
3804 if [get_compiler_info ${info_options}] {
3805 return -1
3806 }
3807 return 0
3808 }
3809
3810 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
3811 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
3812 # to pass to untested, if something is wrong. OPTIONS are passed
3813 # to gdb_compile directly.
3814 proc build_executable { testname executable {sources ""} {options {debug}} } {
3815 if {[llength $sources]==0} {
3816 set sources ${executable}.c
3817 }
3818
3819 set arglist [list $testname $executable $options]
3820 foreach source $sources {
3821 lappend arglist $source $options
3822 }
3823
3824 return [eval build_executable_from_specs $arglist]
3825 }
3826
3827 # Starts fresh GDB binary and loads EXECUTABLE into GDB. EXECUTABLE is
3828 # the basename of the binary.
3829 proc clean_restart { executable } {
3830 global srcdir
3831 global subdir
3832 set binfile [standard_output_file ${executable}]
3833
3834 gdb_exit
3835 gdb_start
3836 gdb_reinitialize_dir $srcdir/$subdir
3837 gdb_load ${binfile}
3838 }
3839
3840 # Prepares for testing by calling build_executable_full, then
3841 # clean_restart.
3842 # TESTNAME is the name of the test.
3843 # Each element in ARGS is a list of the form
3844 # { EXECUTABLE OPTIONS SOURCE_SPEC... }
3845 # These are passed to build_executable_from_specs, which see.
3846 # The last EXECUTABLE is passed to clean_restart.
3847 # Returns 0 on success, non-zero on failure.
3848 proc prepare_for_testing_full {testname args} {
3849 foreach spec $args {
3850 if {[eval build_executable_from_specs [list $testname] $spec] == -1} {
3851 return -1
3852 }
3853 set executable [lindex $spec 0]
3854 }
3855 clean_restart $executable
3856 return 0
3857 }
3858
3859 # Prepares for testing, by calling build_executable, and then clean_restart.
3860 # Please refer to build_executable for parameter description.
3861 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
3862
3863 if {[build_executable $testname $executable $sources $options] == -1} {
3864 return -1
3865 }
3866 clean_restart $executable
3867
3868 return 0
3869 }
3870
3871 proc get_valueof { fmt exp default } {
3872 global gdb_prompt
3873
3874 set test "get valueof \"${exp}\""
3875 set val ${default}
3876 gdb_test_multiple "print${fmt} ${exp}" "$test" {
3877 -re "\\$\[0-9\]* = (.*)\[\r\n\]*$gdb_prompt $" {
3878 set val $expect_out(1,string)
3879 pass "$test ($val)"
3880 }
3881 timeout {
3882 fail "$test (timeout)"
3883 }
3884 }
3885 return ${val}
3886 }
3887
3888 proc get_integer_valueof { exp default } {
3889 global gdb_prompt
3890
3891 set test "get integer valueof \"${exp}\""
3892 set val ${default}
3893 gdb_test_multiple "print /d ${exp}" "$test" {
3894 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
3895 set val $expect_out(1,string)
3896 pass "$test ($val)"
3897 }
3898 timeout {
3899 fail "$test (timeout)"
3900 }
3901 }
3902 return ${val}
3903 }
3904
3905 proc get_hexadecimal_valueof { exp default } {
3906 global gdb_prompt
3907 send_gdb "print /x ${exp}\n"
3908 set test "get hexadecimal valueof \"${exp}\""
3909 gdb_expect {
3910 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
3911 set val $expect_out(1,string)
3912 pass "$test"
3913 }
3914 timeout {
3915 set val ${default}
3916 fail "$test (timeout)"
3917 }
3918 }
3919 return ${val}
3920 }
3921
3922 proc get_sizeof { type default } {
3923 return [get_integer_valueof "sizeof (${type})" $default]
3924 }
3925
3926 # Get the current value for remotetimeout and return it.
3927 proc get_remotetimeout { } {
3928 global gdb_prompt
3929 global decimal
3930
3931 gdb_test_multiple "show remotetimeout" "" {
3932 -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" {
3933 return $expect_out(1,string);
3934 }
3935 }
3936
3937 # Pick the default that gdb uses
3938 warning "Unable to read remotetimeout"
3939 return 300
3940 }
3941
3942 # Set the remotetimeout to the specified timeout. Nothing is returned.
3943 proc set_remotetimeout { timeout } {
3944 global gdb_prompt
3945
3946 gdb_test_multiple "set remotetimeout $timeout" "" {
3947 -re "$gdb_prompt $" {
3948 verbose "Set remotetimeout to $timeout\n"
3949 }
3950 }
3951 }
3952
3953 # Log gdb command line and script if requested.
3954 if {[info exists TRANSCRIPT]} {
3955 rename send_gdb real_send_gdb
3956 rename remote_spawn real_remote_spawn
3957 rename remote_close real_remote_close
3958
3959 global gdb_transcript
3960 set gdb_transcript ""
3961
3962 global gdb_trans_count
3963 set gdb_trans_count 1
3964
3965 proc remote_spawn {args} {
3966 global gdb_transcript gdb_trans_count outdir
3967
3968 if {$gdb_transcript != ""} {
3969 close $gdb_transcript
3970 }
3971 set gdb_transcript [open [file join $outdir transcript.$gdb_trans_count] w]
3972 puts $gdb_transcript [lindex $args 1]
3973 incr gdb_trans_count
3974
3975 return [uplevel real_remote_spawn $args]
3976 }
3977
3978 proc remote_close {args} {
3979 global gdb_transcript
3980
3981 if {$gdb_transcript != ""} {
3982 close $gdb_transcript
3983 set gdb_transcript ""
3984 }
3985
3986 return [uplevel real_remote_close $args]
3987 }
3988
3989 proc send_gdb {args} {
3990 global gdb_transcript
3991
3992 if {$gdb_transcript != ""} {
3993 puts -nonewline $gdb_transcript [lindex $args 0]
3994 }
3995
3996 return [uplevel real_send_gdb $args]
3997 }
3998 }
3999
4000 proc core_find {binfile {deletefiles {}} {arg ""}} {
4001 global objdir subdir
4002
4003 set destcore "$binfile.core"
4004 file delete $destcore
4005
4006 # Create a core file named "$destcore" rather than just "core", to
4007 # avoid problems with sys admin types that like to regularly prune all
4008 # files named "core" from the system.
4009 #
4010 # Arbitrarily try setting the core size limit to "unlimited" since
4011 # this does not hurt on systems where the command does not work and
4012 # allows us to generate a core on systems where it does.
4013 #
4014 # Some systems append "core" to the name of the program; others append
4015 # the name of the program to "core"; still others (like Linux, as of
4016 # May 2003) create cores named "core.PID". In the latter case, we
4017 # could have many core files lying around, and it may be difficult to
4018 # tell which one is ours, so let's run the program in a subdirectory.
4019 set found 0
4020 set coredir [standard_output_file coredir.[getpid]]
4021 file mkdir $coredir
4022 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
4023 # remote_exec host "${binfile}"
4024 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
4025 if [remote_file build exists $i] {
4026 remote_exec build "mv $i $destcore"
4027 set found 1
4028 }
4029 }
4030 # Check for "core.PID".
4031 if { $found == 0 } {
4032 set names [glob -nocomplain -directory $coredir core.*]
4033 if {[llength $names] == 1} {
4034 set corefile [file join $coredir [lindex $names 0]]
4035 remote_exec build "mv $corefile $destcore"
4036 set found 1
4037 }
4038 }
4039 if { $found == 0 } {
4040 # The braindamaged HPUX shell quits after the ulimit -c above
4041 # without executing ${binfile}. So we try again without the
4042 # ulimit here if we didn't find a core file above.
4043 # Oh, I should mention that any "braindamaged" non-Unix system has
4044 # the same problem. I like the cd bit too, it's really neat'n stuff.
4045 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
4046 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
4047 if [remote_file build exists $i] {
4048 remote_exec build "mv $i $destcore"
4049 set found 1
4050 }
4051 }
4052 }
4053
4054 # Try to clean up after ourselves.
4055 foreach deletefile $deletefiles {
4056 remote_file build delete [file join $coredir $deletefile]
4057 }
4058 remote_exec build "rmdir $coredir"
4059
4060 if { $found == 0 } {
4061 warning "can't generate a core file - core tests suppressed - check ulimit -c"
4062 return ""
4063 }
4064 return $destcore
4065 }
4066
4067 # gdb_target_symbol_prefix_flags returns a string that can be added
4068 # to gdb_compile options to define SYMBOL_PREFIX macro value
4069 # symbol_prefix_flags returns a string that can be added
4070 # for targets that use underscore as symbol prefix.
4071 # TODO: find out automatically if the target needs this.
4072
4073 proc gdb_target_symbol_prefix_flags {} {
4074 if { [istarget "*-*-cygwin*"] || [istarget "i?86-*-mingw*"]
4075 || [istarget "*-*-msdosdjgpp*"] || [istarget "*-*-go32*"] } {
4076 return "additional_flags=-DSYMBOL_PREFIX=\"_\""
4077 } else {
4078 return ""
4079 }
4080 }
4081
4082 # Always load compatibility stuff.
4083 load_lib future.exp
This page took 0.119053 seconds and 4 git commands to generate.