]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/lib/gdb.exp
run copyright.sh for 2011.
[thirdparty/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2 # 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
3 # Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 # This file was written by Fred Fish. (fnf@cygnus.com)
19
20 # Generic gdb subroutines that should work for any target. If these
21 # need to be modified for any target, it can be done with a variable
22 # or by passing arguments.
23
24 if {$tool == ""} {
25 # Tests would fail, logs on get_compiler_info() would be missing.
26 send_error "`site.exp' not found, run `make site.exp'!\n"
27 exit 2
28 }
29
30 load_lib libgloss.exp
31
32 global GDB
33
34 if [info exists TOOL_EXECUTABLE] {
35 set GDB $TOOL_EXECUTABLE;
36 }
37 if ![info exists GDB] {
38 if ![is_remote host] {
39 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
40 } else {
41 set GDB [transform gdb];
42 }
43 }
44 verbose "using GDB = $GDB" 2
45
46 # GDBFLAGS is available for the user to set on the command line.
47 # E.g. make check RUNTESTFLAGS=GDBFLAGS=mumble
48 # Testcases may use it to add additional flags, but they must:
49 # - append new flags, not overwrite
50 # - restore the original value when done
51 global GDBFLAGS
52 if ![info exists GDBFLAGS] {
53 set GDBFLAGS ""
54 }
55 verbose "using GDBFLAGS = $GDBFLAGS" 2
56
57 # INTERNAL_GDBFLAGS contains flags that the testsuite requires.
58 global INTERNAL_GDBFLAGS
59 if ![info exists INTERNAL_GDBFLAGS] {
60 set INTERNAL_GDBFLAGS "-nw -nx -data-directory [pwd]/../data-directory"
61 }
62
63 # The variable gdb_prompt is a regexp which matches the gdb prompt.
64 # Set it if it is not already set.
65 global gdb_prompt
66 if ![info exists gdb_prompt] then {
67 set gdb_prompt "\[(\]gdb\[)\]"
68 }
69
70 # The variable fullname_syntax_POSIX is a regexp which matches a POSIX
71 # absolute path ie. /foo/
72 set fullname_syntax_POSIX {/[^\n]*/}
73 # The variable fullname_syntax_UNC is a regexp which matches a Windows
74 # UNC path ie. \\D\foo\
75 set fullname_syntax_UNC {\\\\[^\\]+\\[^\n]+\\}
76 # The variable fullname_syntax_DOS_CASE is a regexp which matches a
77 # particular DOS case that GDB most likely will output
78 # ie. \foo\, but don't match \\.*\
79 set fullname_syntax_DOS_CASE {\\[^\\][^\n]*\\}
80 # The variable fullname_syntax_DOS is a regexp which matches a DOS path
81 # ie. a:\foo\ && a:foo\
82 set fullname_syntax_DOS {[a-zA-Z]:[^\n]*\\}
83 # The variable fullname_syntax is a regexp which matches what GDB considers
84 # an absolute path. It is currently debatable if the Windows style paths
85 # d:foo and \abc should be considered valid as an absolute path.
86 # Also, the purpse of this regexp is not to recognize a well formed
87 # absolute path, but to say with certainty that a path is absolute.
88 set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
89
90 # Needed for some tests under Cygwin.
91 global EXEEXT
92 global env
93
94 if ![info exists env(EXEEXT)] {
95 set EXEEXT ""
96 } else {
97 set EXEEXT $env(EXEEXT)
98 }
99
100 set octal "\[0-7\]+"
101
102 ### Only procedures should come after this point.
103
104 #
105 # gdb_version -- extract and print the version number of GDB
106 #
107 proc default_gdb_version {} {
108 global GDB
109 global INTERNAL_GDBFLAGS GDBFLAGS
110 global gdb_prompt
111 set output [remote_exec host "$GDB $INTERNAL_GDBFLAGS --version"]
112 set tmp [lindex $output 1];
113 set version ""
114 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
115 if ![is_remote host] {
116 clone_output "[which $GDB] version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
117 } else {
118 clone_output "$GDB on remote host version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
119 }
120 }
121
122 proc gdb_version { } {
123 return [default_gdb_version];
124 }
125
126 #
127 # gdb_unload -- unload a file if one is loaded
128 #
129
130 proc gdb_unload {} {
131 global verbose
132 global GDB
133 global gdb_prompt
134 send_gdb "file\n"
135 gdb_expect 60 {
136 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
137 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
138 -re "A program is being debugged already..*Kill it.*y or n. $"\
139 { send_gdb "y\n"
140 verbose "\t\tKilling previous program being debugged"
141 exp_continue
142 }
143 -re "Discard symbol table from .*y or n.*$" {
144 send_gdb "y\n"
145 exp_continue
146 }
147 -re "$gdb_prompt $" {}
148 timeout {
149 perror "couldn't unload file in $GDB (timed out)."
150 return -1
151 }
152 }
153 }
154
155 # Many of the tests depend on setting breakpoints at various places and
156 # running until that breakpoint is reached. At times, we want to start
157 # with a clean-slate with respect to breakpoints, so this utility proc
158 # lets us do this without duplicating this code everywhere.
159 #
160
161 proc delete_breakpoints {} {
162 global gdb_prompt
163
164 # we need a larger timeout value here or this thing just confuses
165 # itself. May need a better implementation if possible. - guo
166 #
167 send_gdb "delete breakpoints\n"
168 gdb_expect 100 {
169 -re "Delete all breakpoints.*y or n.*$" {
170 send_gdb "y\n";
171 exp_continue
172 }
173 -re "$gdb_prompt $" { # This happens if there were no breakpoints
174 }
175 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
176 }
177 send_gdb "info breakpoints\n"
178 gdb_expect 100 {
179 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
180 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
181 -re "Delete all breakpoints.*or n.*$" {
182 send_gdb "y\n";
183 exp_continue
184 }
185 timeout { perror "info breakpoints (timeout)" ; return }
186 }
187 }
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 proc gdb_run_cmd {args} {
198 global gdb_prompt
199
200 if [target_info exists gdb_init_command] {
201 send_gdb "[target_info gdb_init_command]\n";
202 gdb_expect 30 {
203 -re "$gdb_prompt $" { }
204 default {
205 perror "gdb_init_command for target failed";
206 return;
207 }
208 }
209 }
210
211 if [target_info exists use_gdb_stub] {
212 if [target_info exists gdb,do_reload_on_run] {
213 if { [gdb_reload] != 0 } {
214 return;
215 }
216 send_gdb "continue\n";
217 gdb_expect 60 {
218 -re "Continu\[^\r\n\]*\[\r\n\]" {}
219 default {}
220 }
221 return;
222 }
223
224 if [target_info exists gdb,start_symbol] {
225 set start [target_info gdb,start_symbol];
226 } else {
227 set start "start";
228 }
229 send_gdb "jump *$start\n"
230 set start_attempt 1;
231 while { $start_attempt } {
232 # Cap (re)start attempts at three to ensure that this loop
233 # always eventually fails. Don't worry about trying to be
234 # clever and not send a command when it has failed.
235 if [expr $start_attempt > 3] {
236 perror "Jump to start() failed (retry count exceeded)";
237 return;
238 }
239 set start_attempt [expr $start_attempt + 1];
240 gdb_expect 30 {
241 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
242 set start_attempt 0;
243 }
244 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
245 perror "Can't find start symbol to run in gdb_run";
246 return;
247 }
248 -re "No symbol \"start\" in current.*$gdb_prompt $" {
249 send_gdb "jump *_start\n";
250 }
251 -re "No symbol.*context.*$gdb_prompt $" {
252 set start_attempt 0;
253 }
254 -re "Line.* Jump anyway.*y or n. $" {
255 send_gdb "y\n"
256 }
257 -re "The program is not being run.*$gdb_prompt $" {
258 if { [gdb_reload] != 0 } {
259 return;
260 }
261 send_gdb "jump *$start\n";
262 }
263 timeout {
264 perror "Jump to start() failed (timeout)";
265 return
266 }
267 }
268 }
269 if [target_info exists gdb_stub] {
270 gdb_expect 60 {
271 -re "$gdb_prompt $" {
272 send_gdb "continue\n"
273 }
274 }
275 }
276 return
277 }
278
279 if [target_info exists gdb,do_reload_on_run] {
280 if { [gdb_reload] != 0 } {
281 return;
282 }
283 }
284 send_gdb "run $args\n"
285 # This doesn't work quite right yet.
286 # Use -notransfer here so that test cases (like chng-sym.exp)
287 # may test for additional start-up messages.
288 gdb_expect 60 {
289 -re "The program .* has been started already.*y or n. $" {
290 send_gdb "y\n"
291 exp_continue
292 }
293 -notransfer -re "Starting program: \[^\r\n\]*" {}
294 -notransfer -re "$gdb_prompt $" {
295 # There is no more input expected.
296 }
297 }
298 }
299
300 # Generic start command. Return 0 if we could start the program, -1
301 # if we could not.
302
303 proc gdb_start_cmd {args} {
304 global gdb_prompt
305
306 if [target_info exists gdb_init_command] {
307 send_gdb "[target_info gdb_init_command]\n";
308 gdb_expect 30 {
309 -re "$gdb_prompt $" { }
310 default {
311 perror "gdb_init_command for target failed";
312 return;
313 }
314 }
315 }
316
317 if [target_info exists use_gdb_stub] {
318 return -1
319 }
320
321 send_gdb "start $args\n"
322 # Use -notransfer here so that test cases (like chng-sym.exp)
323 # may test for additional start-up messages.
324 gdb_expect 60 {
325 -re "The program .* has been started already.*y or n. $" {
326 send_gdb "y\n"
327 exp_continue
328 }
329 -notransfer -re "Starting program: \[^\r\n\]*" {
330 return 0
331 }
332 }
333 return -1
334 }
335
336 # Set a breakpoint at FUNCTION. If there is an additional argument it is
337 # a list of options; the supported options are allow-pending, temporary,
338 # and no-message.
339
340 proc gdb_breakpoint { function args } {
341 global gdb_prompt
342 global decimal
343
344 set pending_response n
345 if {[lsearch -exact [lindex $args 0] allow-pending] != -1} {
346 set pending_response y
347 }
348
349 set break_command "break"
350 set break_message "Breakpoint"
351 if {[lsearch -exact [lindex $args 0] temporary] != -1} {
352 set break_command "tbreak"
353 set break_message "Temporary breakpoint"
354 }
355
356 set no_message 0
357 if {[lsearch -exact [lindex $args 0] no-message] != -1} {
358 set no_message 1
359 }
360
361 send_gdb "$break_command $function\n"
362 # The first two regexps are what we get with -g, the third is without -g.
363 gdb_expect 30 {
364 -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
365 -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
366 -re "$break_message \[0-9\]* at .*$gdb_prompt $" {}
367 -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
368 if {$pending_response == "n"} {
369 if { $no_message == 0 } {
370 fail "setting breakpoint at $function"
371 }
372 return 0
373 }
374 }
375 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
376 send_gdb "$pending_response\n"
377 exp_continue
378 }
379 -re "$gdb_prompt $" {
380 if { $no_message == 0 } {
381 fail "setting breakpoint at $function"
382 }
383 return 0
384 }
385 timeout {
386 if { $no_message == 0 } {
387 fail "setting breakpoint at $function (timeout)"
388 }
389 return 0
390 }
391 }
392 return 1;
393 }
394
395 # Set breakpoint at function and run gdb until it breaks there.
396 # Since this is the only breakpoint that will be set, if it stops
397 # at a breakpoint, we will assume it is the one we want. We can't
398 # just compare to "function" because it might be a fully qualified,
399 # single quoted C++ function specifier. If there's an additional argument,
400 # pass it to gdb_breakpoint.
401
402 proc runto { function args } {
403 global gdb_prompt
404 global decimal
405
406 delete_breakpoints
407
408 if ![gdb_breakpoint $function [lindex $args 0]] {
409 return 0;
410 }
411
412 gdb_run_cmd
413
414 # the "at foo.c:36" output we get with -g.
415 # the "in func" output we get without -g.
416 gdb_expect 30 {
417 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
418 return 1
419 }
420 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
421 return 1
422 }
423 -re "The target does not support running in non-stop mode.\r\n$gdb_prompt $" {
424 unsupported "Non-stop mode not supported"
425 return 0
426 }
427 -re ".*A problem internal to GDB has been detected" {
428 fail "running to $function in runto (GDB internal error)"
429 gdb_internal_error_resync
430 return 0
431 }
432 -re "$gdb_prompt $" {
433 fail "running to $function in runto"
434 return 0
435 }
436 eof {
437 fail "running to $function in runto (end of file)"
438 return 0
439 }
440 timeout {
441 fail "running to $function in runto (timeout)"
442 return 0
443 }
444 }
445 return 1
446 }
447
448 #
449 # runto_main -- ask gdb to run until we hit a breakpoint at main.
450 # The case where the target uses stubs has to be handled
451 # specially--if it uses stubs, assuming we hit
452 # breakpoint() and just step out of the function.
453 #
454 proc runto_main { } {
455 global gdb_prompt
456 global decimal
457
458 if ![target_info exists gdb_stub] {
459 return [runto main]
460 }
461
462 delete_breakpoints
463
464 gdb_step_for_stub;
465
466 return 1
467 }
468
469
470 ### Continue, and expect to hit a breakpoint.
471 ### Report a pass or fail, depending on whether it seems to have
472 ### worked. Use NAME as part of the test name; each call to
473 ### continue_to_breakpoint should use a NAME which is unique within
474 ### that test file.
475 proc gdb_continue_to_breakpoint {name {location_pattern .*}} {
476 global gdb_prompt
477 set full_name "continue to breakpoint: $name"
478
479 send_gdb "continue\n"
480 gdb_expect {
481 -re "Breakpoint .* (at|in) $location_pattern\r\n$gdb_prompt $" {
482 pass $full_name
483 }
484 -re ".*$gdb_prompt $" {
485 fail $full_name
486 }
487 timeout {
488 fail "$full_name (timeout)"
489 }
490 }
491 }
492
493
494 # gdb_internal_error_resync:
495 #
496 # Answer the questions GDB asks after it reports an internal error
497 # until we get back to a GDB prompt. Decline to quit the debugging
498 # session, and decline to create a core file. Return non-zero if the
499 # resync succeeds.
500 #
501 # This procedure just answers whatever questions come up until it sees
502 # a GDB prompt; it doesn't require you to have matched the input up to
503 # any specific point. However, it only answers questions it sees in
504 # the output itself, so if you've matched a question, you had better
505 # answer it yourself before calling this.
506 #
507 # You can use this function thus:
508 #
509 # gdb_expect {
510 # ...
511 # -re ".*A problem internal to GDB has been detected" {
512 # gdb_internal_error_resync
513 # }
514 # ...
515 # }
516 #
517 proc gdb_internal_error_resync {} {
518 global gdb_prompt
519
520 set count 0
521 while {$count < 10} {
522 gdb_expect {
523 -re "Quit this debugging session\\? \\(y or n\\) $" {
524 send_gdb "n\n"
525 incr count
526 }
527 -re "Create a core file of GDB\\? \\(y or n\\) $" {
528 send_gdb "n\n"
529 incr count
530 }
531 -re "$gdb_prompt $" {
532 # We're resynchronized.
533 return 1
534 }
535 timeout {
536 perror "Could not resync from internal error (timeout)"
537 return 0
538 }
539 }
540 }
541 perror "Could not resync from internal error (resync count exceeded)"
542 return 0
543 }
544
545
546 # gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
547 # Send a command to gdb; test the result.
548 #
549 # COMMAND is the command to execute, send to GDB with send_gdb. If
550 # this is the null string no command is sent.
551 # MESSAGE is a message to be printed with the built-in failure patterns
552 # if one of them matches. If MESSAGE is empty COMMAND will be used.
553 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
554 # patterns. Pattern elements will be evaluated in the caller's
555 # context; action elements will be executed in the caller's context.
556 # Unlike patterns for gdb_test, these patterns should generally include
557 # the final newline and prompt.
558 #
559 # Returns:
560 # 1 if the test failed, according to a built-in failure pattern
561 # 0 if only user-supplied patterns matched
562 # -1 if there was an internal error.
563 #
564 # You can use this function thus:
565 #
566 # gdb_test_multiple "print foo" "test foo" {
567 # -re "expected output 1" {
568 # pass "print foo"
569 # }
570 # -re "expected output 2" {
571 # fail "print foo"
572 # }
573 # }
574 #
575 # The standard patterns, such as "Program exited..." and "A problem
576 # ...", all being implicitly appended to that list.
577 #
578 proc gdb_test_multiple { command message user_code } {
579 global verbose
580 global gdb_prompt
581 global GDB
582 upvar timeout timeout
583 upvar expect_out expect_out
584
585 if { $message == "" } {
586 set message $command
587 }
588
589 if [string match "*\[\r\n\]" $command] {
590 error "Invalid trailing newline in \"$message\" test"
591 }
592
593 # TCL/EXPECT WART ALERT
594 # Expect does something very strange when it receives a single braced
595 # argument. It splits it along word separators and performs substitutions.
596 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
597 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
598 # double-quoted list item, "\[ab\]" is just a long way of representing
599 # "[ab]", because the backslashes will be removed by lindex.
600
601 # Unfortunately, there appears to be no easy way to duplicate the splitting
602 # that expect will do from within TCL. And many places make use of the
603 # "\[0-9\]" construct, so we need to support that; and some places make use
604 # of the "[func]" construct, so we need to support that too. In order to
605 # get this right we have to substitute quoted list elements differently
606 # from braced list elements.
607
608 # We do this roughly the same way that Expect does it. We have to use two
609 # lists, because if we leave unquoted newlines in the argument to uplevel
610 # they'll be treated as command separators, and if we escape newlines
611 # we mangle newlines inside of command blocks. This assumes that the
612 # input doesn't contain a pattern which contains actual embedded newlines
613 # at this point!
614
615 regsub -all {\n} ${user_code} { } subst_code
616 set subst_code [uplevel list $subst_code]
617
618 set processed_code ""
619 set patterns ""
620 set expecting_action 0
621 set expecting_arg 0
622 foreach item $user_code subst_item $subst_code {
623 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
624 lappend processed_code $item
625 continue
626 }
627 if { $item == "-indices" || $item == "-re" || $item == "-ex" } {
628 lappend processed_code $item
629 continue
630 }
631 if { $item == "-timeout" } {
632 set expecting_arg 1
633 lappend processed_code $item
634 continue
635 }
636 if { $expecting_arg } {
637 set expecting_arg 0
638 lappend processed_code $item
639 continue
640 }
641 if { $expecting_action } {
642 lappend processed_code "uplevel [list $item]"
643 set expecting_action 0
644 # Cosmetic, no effect on the list.
645 append processed_code "\n"
646 continue
647 }
648 set expecting_action 1
649 lappend processed_code $subst_item
650 if {$patterns != ""} {
651 append patterns "; "
652 }
653 append patterns "\"$subst_item\""
654 }
655
656 # Also purely cosmetic.
657 regsub -all {\r} $patterns {\\r} patterns
658 regsub -all {\n} $patterns {\\n} patterns
659
660 if $verbose>2 then {
661 send_user "Sending \"$command\" to gdb\n"
662 send_user "Looking to match \"$patterns\"\n"
663 send_user "Message is \"$message\"\n"
664 }
665
666 set result -1
667 set string "${command}\n";
668 if { $command != "" } {
669 while { "$string" != "" } {
670 set foo [string first "\n" "$string"];
671 set len [string length "$string"];
672 if { $foo < [expr $len - 1] } {
673 set str [string range "$string" 0 $foo];
674 if { [send_gdb "$str"] != "" } {
675 global suppress_flag;
676
677 if { ! $suppress_flag } {
678 perror "Couldn't send $command to GDB.";
679 }
680 fail "$message";
681 return $result;
682 }
683 # since we're checking if each line of the multi-line
684 # command are 'accepted' by GDB here,
685 # we need to set -notransfer expect option so that
686 # command output is not lost for pattern matching
687 # - guo
688 gdb_expect 2 {
689 -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
690 timeout { verbose "partial: timeout" 3 }
691 }
692 set string [string range "$string" [expr $foo + 1] end];
693 } else {
694 break;
695 }
696 }
697 if { "$string" != "" } {
698 if { [send_gdb "$string"] != "" } {
699 global suppress_flag;
700
701 if { ! $suppress_flag } {
702 perror "Couldn't send $command to GDB.";
703 }
704 fail "$message";
705 return $result;
706 }
707 }
708 }
709
710 if [target_info exists gdb,timeout] {
711 set tmt [target_info gdb,timeout];
712 } else {
713 if [info exists timeout] {
714 set tmt $timeout;
715 } else {
716 global timeout;
717 if [info exists timeout] {
718 set tmt $timeout;
719 } else {
720 set tmt 60;
721 }
722 }
723 }
724
725 set code {
726 -re ".*A problem internal to GDB has been detected" {
727 fail "$message (GDB internal error)"
728 gdb_internal_error_resync
729 }
730 -re "\\*\\*\\* DOSEXIT code.*" {
731 if { $message != "" } {
732 fail "$message";
733 }
734 gdb_suppress_entire_file "GDB died";
735 set result -1;
736 }
737 }
738 append code $processed_code
739 append code {
740 -re "Ending remote debugging.*$gdb_prompt $" {
741 if ![isnative] then {
742 warning "Can`t communicate to remote target."
743 }
744 gdb_exit
745 gdb_start
746 set result -1
747 }
748 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
749 perror "Undefined command \"$command\"."
750 fail "$message"
751 set result 1
752 }
753 -re "Ambiguous command.*$gdb_prompt $" {
754 perror "\"$command\" is not a unique command name."
755 fail "$message"
756 set result 1
757 }
758 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
759 if ![string match "" $message] then {
760 set errmsg "$message (the program exited)"
761 } else {
762 set errmsg "$command (the program exited)"
763 }
764 fail "$errmsg"
765 set result -1
766 }
767 -re "Program exited normally.*$gdb_prompt $" {
768 if ![string match "" $message] then {
769 set errmsg "$message (the program exited)"
770 } else {
771 set errmsg "$command (the program exited)"
772 }
773 fail "$errmsg"
774 set result -1
775 }
776 -re "The program is not being run.*$gdb_prompt $" {
777 if ![string match "" $message] then {
778 set errmsg "$message (the program is no longer running)"
779 } else {
780 set errmsg "$command (the program is no longer running)"
781 }
782 fail "$errmsg"
783 set result -1
784 }
785 -re "\r\n$gdb_prompt $" {
786 if ![string match "" $message] then {
787 fail "$message"
788 }
789 set result 1
790 }
791 "<return>" {
792 send_gdb "\n"
793 perror "Window too small."
794 fail "$message"
795 set result -1
796 }
797 -re "\\((y or n|y or \\\[n\\\]|\\\[y\\\] or n)\\) " {
798 send_gdb "n\n"
799 gdb_expect -re "$gdb_prompt $"
800 fail "$message (got interactive prompt)"
801 set result -1
802 }
803 -re "\\\[0\\\] cancel\r\n\\\[1\\\] all.*\r\n> $" {
804 send_gdb "0\n"
805 gdb_expect -re "$gdb_prompt $"
806 fail "$message (got breakpoint menu)"
807 set result -1
808 }
809 eof {
810 perror "Process no longer exists"
811 if { $message != "" } {
812 fail "$message"
813 }
814 return -1
815 }
816 full_buffer {
817 perror "internal buffer is full."
818 fail "$message"
819 set result -1
820 }
821 timeout {
822 if ![string match "" $message] then {
823 fail "$message (timeout)"
824 }
825 set result 1
826 }
827 }
828
829 set result 0
830 set code [catch {gdb_expect $tmt $code} string]
831 if {$code == 1} {
832 global errorInfo errorCode;
833 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
834 } elseif {$code == 2} {
835 return -code return $string
836 } elseif {$code == 3} {
837 return
838 } elseif {$code > 4} {
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
1307 global GDB
1308 global INTERNAL_GDBFLAGS GDBFLAGS
1309 global gdb_prompt
1310 global timeout
1311 global gdb_spawn_id;
1312 global env
1313
1314 gdb_stop_suppressing_tests;
1315
1316 set env(LC_CTYPE) C
1317
1318 # Don't let a .inputrc file or an existing setting of INPUTRC mess up
1319 # the test results. Even if /dev/null doesn't exist on the particular
1320 # platform, the readline library will use the default setting just by
1321 # failing to open the file. OTOH, opening /dev/null successfully will
1322 # also result in the default settings being used since nothing will be
1323 # read from this file.
1324 set env(INPUTRC) "/dev/null"
1325
1326 # The gdb.base/readline.exp arrow key test relies on the standard VT100
1327 # bindings, so make sure that an appropriate terminal is selected.
1328 # The same bug doesn't show up if we use ^P / ^N instead.
1329 set env(TERM) "vt100"
1330
1331 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1332
1333 if [info exists gdb_spawn_id] {
1334 return 0;
1335 }
1336
1337 if ![is_remote host] {
1338 if { [which $GDB] == 0 } then {
1339 perror "$GDB does not exist."
1340 exit 1
1341 }
1342 }
1343 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]"];
1344 if { $res < 0 || $res == "" } {
1345 perror "Spawning $GDB failed."
1346 return 1;
1347 }
1348 gdb_expect 360 {
1349 -re "\[\r\n\]$gdb_prompt $" {
1350 verbose "GDB initialized."
1351 }
1352 -re "$gdb_prompt $" {
1353 perror "GDB never initialized."
1354 return -1
1355 }
1356 timeout {
1357 perror "(timeout) GDB never initialized after 10 seconds."
1358 remote_close host;
1359 return -1
1360 }
1361 }
1362 set gdb_spawn_id -1;
1363 # force the height to "unlimited", so no pagers get used
1364
1365 send_gdb "set height 0\n"
1366 gdb_expect 10 {
1367 -re "$gdb_prompt $" {
1368 verbose "Setting height to 0." 2
1369 }
1370 timeout {
1371 warning "Couldn't set the height to 0"
1372 }
1373 }
1374 # force the width to "unlimited", so no wraparound occurs
1375 send_gdb "set width 0\n"
1376 gdb_expect 10 {
1377 -re "$gdb_prompt $" {
1378 verbose "Setting width to 0." 2
1379 }
1380 timeout {
1381 warning "Couldn't set the width to 0."
1382 }
1383 }
1384 return 0;
1385 }
1386
1387 # Examine the output of compilation to determine whether compilation
1388 # failed or not. If it failed determine whether it is due to missing
1389 # compiler or due to compiler error. Report pass, fail or unsupported
1390 # as appropriate
1391
1392 proc gdb_compile_test {src output} {
1393 if { $output == "" } {
1394 pass "compilation [file tail $src]"
1395 } elseif { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output] } {
1396 unsupported "compilation [file tail $src]"
1397 } elseif { [regexp {.*: command not found[\r|\n]*$} $output] } {
1398 unsupported "compilation [file tail $src]"
1399 } elseif { [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } {
1400 unsupported "compilation [file tail $src]"
1401 } else {
1402 verbose -log "compilation failed: $output" 2
1403 fail "compilation [file tail $src]"
1404 }
1405 }
1406
1407 # Return a 1 for configurations for which we don't even want to try to
1408 # test C++.
1409
1410 proc skip_cplus_tests {} {
1411 if { [istarget "h8300-*-*"] } {
1412 return 1
1413 }
1414
1415 # The C++ IO streams are too large for HC11/HC12 and are thus not
1416 # available. The gdb C++ tests use them and don't compile.
1417 if { [istarget "m6811-*-*"] } {
1418 return 1
1419 }
1420 if { [istarget "m6812-*-*"] } {
1421 return 1
1422 }
1423 return 0
1424 }
1425
1426 # Return a 1 for configurations for which don't have both C++ and the STL.
1427
1428 proc skip_stl_tests {} {
1429 # Symbian supports the C++ language, but the STL is missing
1430 # (both headers and libraries).
1431 if { [istarget "arm*-*-symbianelf*"] } {
1432 return 1
1433 }
1434
1435 return [skip_cplus_tests]
1436 }
1437
1438 # Return a 1 if I don't even want to try to test FORTRAN.
1439
1440 proc skip_fortran_tests {} {
1441 return 0
1442 }
1443
1444 # Return a 1 if I don't even want to try to test ada.
1445
1446 proc skip_ada_tests {} {
1447 return 0
1448 }
1449
1450 # Return a 1 if I don't even want to try to test java.
1451
1452 proc skip_java_tests {} {
1453 return 0
1454 }
1455
1456 # Return a 1 for configurations that do not support Python scripting.
1457
1458 proc skip_python_tests {} {
1459 global gdb_prompt
1460 gdb_test_multiple "python print 'test'" "verify python support" {
1461 -re "not supported.*$gdb_prompt $" {
1462 unsupported "Python support is disabled."
1463 return 1
1464 }
1465 -re "$gdb_prompt $" {}
1466 }
1467
1468 return 0
1469 }
1470
1471 # Return a 1 if we should skip shared library tests.
1472
1473 proc skip_shlib_tests {} {
1474 # Run the shared library tests on native systems.
1475 if {[isnative]} {
1476 return 0
1477 }
1478
1479 # An abbreviated list of remote targets where we should be able to
1480 # run shared library tests.
1481 if {([istarget *-*-linux*]
1482 || [istarget *-*-*bsd*]
1483 || [istarget *-*-solaris2*]
1484 || [istarget arm*-*-symbianelf*]
1485 || [istarget *-*-mingw*]
1486 || [istarget *-*-cygwin*]
1487 || [istarget *-*-pe*])} {
1488 return 0
1489 }
1490
1491 return 1
1492 }
1493
1494 # Return 1 if target is ILP32.
1495 # This cannot be decided simply from looking at the target string,
1496 # as it might depend on externally passed compiler options like -m64.
1497 proc is_ilp32_target {} {
1498 global is_ilp32_target_saved
1499
1500 # Use the cached value, if it exists. Cache value per "board" to handle
1501 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1502 set me "is_ilp32_target"
1503 set board [target_info name]
1504 if [info exists is_ilp32_target_saved($board)] {
1505 verbose "$me: returning saved $is_ilp32_target_saved($board)" 2
1506 return $is_ilp32_target_saved($board)
1507 }
1508
1509
1510 set src ilp32[pid].c
1511 set obj ilp32[pid].o
1512
1513 set f [open $src "w"]
1514 puts $f "int dummy\[sizeof (int) == 4"
1515 puts $f " && sizeof (void *) == 4"
1516 puts $f " && sizeof (long) == 4 ? 1 : -1\];"
1517 close $f
1518
1519 verbose "$me: compiling testfile $src" 2
1520 set lines [gdb_compile $src $obj object {quiet}]
1521 file delete $src
1522 file delete $obj
1523
1524 if ![string match "" $lines] then {
1525 verbose "$me: testfile compilation failed, returning 0" 2
1526 return [set is_ilp32_target_saved($board) 0]
1527 }
1528
1529 verbose "$me: returning 1" 2
1530 return [set is_ilp32_target_saved($board) 1]
1531 }
1532
1533 # Return 1 if target is LP64.
1534 # This cannot be decided simply from looking at the target string,
1535 # as it might depend on externally passed compiler options like -m64.
1536 proc is_lp64_target {} {
1537 global is_lp64_target_saved
1538
1539 # Use the cached value, if it exists. Cache value per "board" to handle
1540 # runs with multiple options (e.g. unix/{-m32,-64}) correctly.
1541 set me "is_lp64_target"
1542 set board [target_info name]
1543 if [info exists is_lp64_target_saved($board)] {
1544 verbose "$me: returning saved $is_lp64_target_saved($board)" 2
1545 return $is_lp64_target_saved($board)
1546 }
1547
1548 set src lp64[pid].c
1549 set obj lp64[pid].o
1550
1551 set f [open $src "w"]
1552 puts $f "int dummy\[sizeof (int) == 4"
1553 puts $f " && sizeof (void *) == 8"
1554 puts $f " && sizeof (long) == 8 ? 1 : -1\];"
1555 close $f
1556
1557 verbose "$me: compiling testfile $src" 2
1558 set lines [gdb_compile $src $obj object {quiet}]
1559 file delete $src
1560 file delete $obj
1561
1562 if ![string match "" $lines] then {
1563 verbose "$me: testfile compilation failed, returning 0" 2
1564 return [set is_lp64_target_saved($board) 0]
1565 }
1566
1567 verbose "$me: returning 1" 2
1568 return [set is_lp64_target_saved($board) 1]
1569 }
1570
1571 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1572 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1573
1574 proc skip_altivec_tests {} {
1575 global skip_vmx_tests_saved
1576 global srcdir subdir gdb_prompt
1577
1578 # Use the cached value, if it exists.
1579 set me "skip_altivec_tests"
1580 if [info exists skip_vmx_tests_saved] {
1581 verbose "$me: returning saved $skip_vmx_tests_saved" 2
1582 return $skip_vmx_tests_saved
1583 }
1584
1585 # Some simulators are known to not support VMX instructions.
1586 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1587 verbose "$me: target known to not support VMX, returning 1" 2
1588 return [set skip_vmx_tests_saved 1]
1589 }
1590
1591 # Make sure we have a compiler that understands altivec.
1592 set compile_flags {debug nowarnings}
1593 if [get_compiler_info not-used] {
1594 warning "Could not get compiler info"
1595 return 1
1596 }
1597 if [test_compiler_info gcc*] {
1598 set compile_flags "$compile_flags additional_flags=-maltivec"
1599 } elseif [test_compiler_info xlc*] {
1600 set compile_flags "$compile_flags additional_flags=-qaltivec"
1601 } else {
1602 verbose "Could not compile with altivec support, returning 1" 2
1603 return 1
1604 }
1605
1606 # Set up, compile, and execute a test program containing VMX instructions.
1607 # Include the current process ID in the file names to prevent conflicts
1608 # with invocations for multiple testsuites.
1609 set src vmx[pid].c
1610 set exe vmx[pid].x
1611
1612 set f [open $src "w"]
1613 puts $f "int main() {"
1614 puts $f "#ifdef __MACH__"
1615 puts $f " asm volatile (\"vor v0,v0,v0\");"
1616 puts $f "#else"
1617 puts $f " asm volatile (\"vor 0,0,0\");"
1618 puts $f "#endif"
1619 puts $f " return 0; }"
1620 close $f
1621
1622 verbose "$me: compiling testfile $src" 2
1623 set lines [gdb_compile $src $exe executable $compile_flags]
1624 file delete $src
1625
1626 if ![string match "" $lines] then {
1627 verbose "$me: testfile compilation failed, returning 1" 2
1628 return [set skip_vmx_tests_saved 1]
1629 }
1630
1631 # No error message, compilation succeeded so now run it via gdb.
1632
1633 gdb_exit
1634 gdb_start
1635 gdb_reinitialize_dir $srcdir/$subdir
1636 gdb_load "$exe"
1637 gdb_run_cmd
1638 gdb_expect {
1639 -re ".*Illegal instruction.*${gdb_prompt} $" {
1640 verbose -log "\n$me altivec hardware not detected"
1641 set skip_vmx_tests_saved 1
1642 }
1643 -re ".*Program exited normally.*${gdb_prompt} $" {
1644 verbose -log "\n$me: altivec hardware detected"
1645 set skip_vmx_tests_saved 0
1646 }
1647 default {
1648 warning "\n$me: default case taken"
1649 set skip_vmx_tests_saved 1
1650 }
1651 }
1652 gdb_exit
1653 remote_file build delete $exe
1654
1655 verbose "$me: returning $skip_vmx_tests_saved" 2
1656 return $skip_vmx_tests_saved
1657 }
1658
1659 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1660 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1661
1662 proc skip_vsx_tests {} {
1663 global skip_vsx_tests_saved
1664 global srcdir subdir gdb_prompt
1665
1666 # Use the cached value, if it exists.
1667 set me "skip_vsx_tests"
1668 if [info exists skip_vsx_tests_saved] {
1669 verbose "$me: returning saved $skip_vsx_tests_saved" 2
1670 return $skip_vsx_tests_saved
1671 }
1672
1673 # Some simulators are known to not support Altivec instructions, so
1674 # they won't support VSX instructions as well.
1675 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1676 verbose "$me: target known to not support VSX, returning 1" 2
1677 return [set skip_vsx_tests_saved 1]
1678 }
1679
1680 # Make sure we have a compiler that understands altivec.
1681 set compile_flags {debug nowarnings quiet}
1682 if [get_compiler_info not-used] {
1683 warning "Could not get compiler info"
1684 return 1
1685 }
1686 if [test_compiler_info gcc*] {
1687 set compile_flags "$compile_flags additional_flags=-mvsx"
1688 } elseif [test_compiler_info xlc*] {
1689 set compile_flags "$compile_flags additional_flags=-qvsx"
1690 } else {
1691 verbose "Could not compile with vsx support, returning 1" 2
1692 return 1
1693 }
1694
1695 set src vsx[pid].c
1696 set exe vsx[pid].x
1697
1698 set f [open $src "w"]
1699 puts $f "int main() {"
1700 puts $f "#ifdef __MACH__"
1701 puts $f " asm volatile (\"lxvd2x v0,v0,v0\");"
1702 puts $f "#else"
1703 puts $f " asm volatile (\"lxvd2x 0,0,0\");"
1704 puts $f "#endif"
1705 puts $f " return 0; }"
1706 close $f
1707
1708 verbose "$me: compiling testfile $src" 2
1709 set lines [gdb_compile $src $exe executable $compile_flags]
1710 file delete $src
1711
1712 if ![string match "" $lines] then {
1713 verbose "$me: testfile compilation failed, returning 1" 2
1714 return [set skip_vsx_tests_saved 1]
1715 }
1716
1717 # No error message, compilation succeeded so now run it via gdb.
1718
1719 gdb_exit
1720 gdb_start
1721 gdb_reinitialize_dir $srcdir/$subdir
1722 gdb_load "$exe"
1723 gdb_run_cmd
1724 gdb_expect {
1725 -re ".*Illegal instruction.*${gdb_prompt} $" {
1726 verbose -log "\n$me VSX hardware not detected"
1727 set skip_vsx_tests_saved 1
1728 }
1729 -re ".*Program exited normally.*${gdb_prompt} $" {
1730 verbose -log "\n$me: VSX hardware detected"
1731 set skip_vsx_tests_saved 0
1732 }
1733 default {
1734 warning "\n$me: default case taken"
1735 set skip_vsx_tests_saved 1
1736 }
1737 }
1738 gdb_exit
1739 remote_file build delete $exe
1740
1741 verbose "$me: returning $skip_vsx_tests_saved" 2
1742 return $skip_vsx_tests_saved
1743 }
1744
1745 # Skip all the tests in the file if you are not on an hppa running
1746 # hpux target.
1747
1748 proc skip_hp_tests {} {
1749 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
1750 verbose "Skip hp tests is $skip_hp"
1751 return $skip_hp
1752 }
1753
1754 # Return whether we should skip tests for showing inlined functions in
1755 # backtraces. Requires get_compiler_info and get_debug_format.
1756
1757 proc skip_inline_frame_tests {} {
1758 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
1759 if { ! [test_debug_format "DWARF 2"] } {
1760 return 1
1761 }
1762
1763 # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line.
1764 if { ([test_compiler_info "gcc-2-*"]
1765 || [test_compiler_info "gcc-3-*"]
1766 || [test_compiler_info "gcc-4-0-*"]) } {
1767 return 1
1768 }
1769
1770 return 0
1771 }
1772
1773 # Return whether we should skip tests for showing variables from
1774 # inlined functions. Requires get_compiler_info and get_debug_format.
1775
1776 proc skip_inline_var_tests {} {
1777 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
1778 if { ! [test_debug_format "DWARF 2"] } {
1779 return 1
1780 }
1781
1782 return 0
1783 }
1784
1785 set compiler_info "unknown"
1786 set gcc_compiled 0
1787 set hp_cc_compiler 0
1788 set hp_aCC_compiler 0
1789
1790 # Figure out what compiler I am using.
1791 #
1792 # BINFILE is a "compiler information" output file. This implementation
1793 # does not use BINFILE.
1794 #
1795 # ARGS can be empty or "C++". If empty, "C" is assumed.
1796 #
1797 # There are several ways to do this, with various problems.
1798 #
1799 # [ gdb_compile -E $ifile -o $binfile.ci ]
1800 # source $binfile.ci
1801 #
1802 # Single Unix Spec v3 says that "-E -o ..." together are not
1803 # specified. And in fact, the native compiler on hp-ux 11 (among
1804 # others) does not work with "-E -o ...". Most targets used to do
1805 # this, and it mostly worked, because it works with gcc.
1806 #
1807 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1808 # source $binfile.ci
1809 #
1810 # This avoids the problem with -E and -o together. This almost works
1811 # if the build machine is the same as the host machine, which is
1812 # usually true of the targets which are not gcc. But this code does
1813 # not figure which compiler to call, and it always ends up using the C
1814 # compiler. Not good for setting hp_aCC_compiler. Targets
1815 # hppa*-*-hpux* and mips*-*-irix* used to do this.
1816 #
1817 # [ gdb_compile -E $ifile > $binfile.ci ]
1818 # source $binfile.ci
1819 #
1820 # dejagnu target_compile says that it supports output redirection,
1821 # but the code is completely different from the normal path and I
1822 # don't want to sweep the mines from that path. So I didn't even try
1823 # this.
1824 #
1825 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1826 # eval $cppout
1827 #
1828 # I actually do this for all targets now. gdb_compile runs the right
1829 # compiler, and TCL captures the output, and I eval the output.
1830 #
1831 # Unfortunately, expect logs the output of the command as it goes by,
1832 # and dejagnu helpfully prints a second copy of it right afterwards.
1833 # So I turn off expect logging for a moment.
1834 #
1835 # [ gdb_compile $ifile $ciexe_file executable $args ]
1836 # [ remote_exec $ciexe_file ]
1837 # [ source $ci_file.out ]
1838 #
1839 # I could give up on -E and just do this.
1840 # I didn't get desperate enough to try this.
1841 #
1842 # -- chastain 2004-01-06
1843
1844 proc get_compiler_info {binfile args} {
1845 # For compiler.c and compiler.cc
1846 global srcdir
1847
1848 # I am going to play with the log to keep noise out.
1849 global outdir
1850 global tool
1851
1852 # These come from compiler.c or compiler.cc
1853 global compiler_info
1854
1855 # Legacy global data symbols.
1856 global gcc_compiled
1857 global hp_cc_compiler
1858 global hp_aCC_compiler
1859
1860 # Choose which file to preprocess.
1861 set ifile "${srcdir}/lib/compiler.c"
1862 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1863 set ifile "${srcdir}/lib/compiler.cc"
1864 }
1865
1866 # Run $ifile through the right preprocessor.
1867 # Toggle gdb.log to keep the compiler output out of the log.
1868 log_file
1869 if [is_remote host] {
1870 # We have to use -E and -o together, despite the comments
1871 # above, because of how DejaGnu handles remote host testing.
1872 set ppout "$outdir/compiler.i"
1873 gdb_compile "${ifile}" "$ppout" preprocess [list "$args" quiet]
1874 set file [open $ppout r]
1875 set cppout [read $file]
1876 close $file
1877 } else {
1878 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1879 }
1880 log_file -a "$outdir/$tool.log"
1881
1882 # Eval the output.
1883 set unknown 0
1884 foreach cppline [ split "$cppout" "\n" ] {
1885 if { [ regexp "^#" "$cppline" ] } {
1886 # line marker
1887 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1888 # blank line
1889 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
1890 # eval this line
1891 verbose "get_compiler_info: $cppline" 2
1892 eval "$cppline"
1893 } else {
1894 # unknown line
1895 verbose -log "get_compiler_info: $cppline"
1896 set unknown 1
1897 }
1898 }
1899
1900 # Reset to unknown compiler if any diagnostics happened.
1901 if { $unknown } {
1902 set compiler_info "unknown"
1903 }
1904
1905 # Set the legacy symbols.
1906 set gcc_compiled 0
1907 set hp_cc_compiler 0
1908 set hp_aCC_compiler 0
1909 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
1910 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
1911 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
1912 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
1913 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
1914 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
1915 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
1916
1917 # Log what happened.
1918 verbose -log "get_compiler_info: $compiler_info"
1919
1920 # Most compilers will evaluate comparisons and other boolean
1921 # operations to 0 or 1.
1922 uplevel \#0 { set true 1 }
1923 uplevel \#0 { set false 0 }
1924
1925 # Use of aCC results in boolean results being displayed as
1926 # "true" or "false"
1927 if { $hp_aCC_compiler } {
1928 uplevel \#0 { set true true }
1929 uplevel \#0 { set false false }
1930 }
1931
1932 return 0;
1933 }
1934
1935 proc test_compiler_info { {compiler ""} } {
1936 global compiler_info
1937
1938 # if no arg, return the compiler_info string
1939
1940 if [string match "" $compiler] {
1941 if [info exists compiler_info] {
1942 return $compiler_info
1943 } else {
1944 perror "No compiler info found."
1945 }
1946 }
1947
1948 return [string match $compiler $compiler_info]
1949 }
1950
1951 proc current_target_name { } {
1952 global target_info
1953 if [info exists target_info(target,name)] {
1954 set answer $target_info(target,name)
1955 } else {
1956 set answer ""
1957 }
1958 return $answer
1959 }
1960
1961 set gdb_wrapper_initialized 0
1962 set gdb_wrapper_target ""
1963
1964 proc gdb_wrapper_init { args } {
1965 global gdb_wrapper_initialized;
1966 global gdb_wrapper_file;
1967 global gdb_wrapper_flags;
1968 global gdb_wrapper_target
1969
1970 if { $gdb_wrapper_initialized == 1 } { return; }
1971
1972 if {[target_info exists needs_status_wrapper] && \
1973 [target_info needs_status_wrapper] != "0"} {
1974 set result [build_wrapper "testglue.o"];
1975 if { $result != "" } {
1976 set gdb_wrapper_file [lindex $result 0];
1977 set gdb_wrapper_flags [lindex $result 1];
1978 } else {
1979 warning "Status wrapper failed to build."
1980 }
1981 }
1982 set gdb_wrapper_initialized 1
1983 set gdb_wrapper_target [current_target_name]
1984 }
1985
1986 # Some targets need to always link a special object in. Save its path here.
1987 global gdb_saved_set_unbuffered_mode_obj
1988 set gdb_saved_set_unbuffered_mode_obj ""
1989
1990 proc gdb_compile {source dest type options} {
1991 global GDB_TESTCASE_OPTIONS;
1992 global gdb_wrapper_file;
1993 global gdb_wrapper_flags;
1994 global gdb_wrapper_initialized;
1995 global srcdir
1996 global objdir
1997 global gdb_saved_set_unbuffered_mode_obj
1998
1999 set outdir [file dirname $dest]
2000
2001 # Add platform-specific options if a shared library was specified using
2002 # "shlib=librarypath" in OPTIONS.
2003 set new_options ""
2004 set shlib_found 0
2005 set shlib_load 0
2006 foreach opt $options {
2007 if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
2008 if [test_compiler_info "xlc-*"] {
2009 # IBM xlc compiler doesn't accept shared library named other
2010 # than .so: use "-Wl," to bypass this
2011 lappend source "-Wl,$shlib_name"
2012 } elseif { ([istarget "*-*-mingw*"]
2013 || [istarget *-*-cygwin*]
2014 || [istarget *-*-pe*])} {
2015 lappend source "${shlib_name}.a"
2016 } else {
2017 lappend source $shlib_name
2018 }
2019 if { $shlib_found == 0 } {
2020 set shlib_found 1
2021 if { ([istarget "*-*-mingw*"]
2022 || [istarget *-*-cygwin*]) } {
2023 lappend new_options "additional_flags=-Wl,--enable-auto-import"
2024 }
2025 }
2026 } elseif { $opt == "shlib_load" } {
2027 set shlib_load 1
2028 } else {
2029 lappend new_options $opt
2030 }
2031 }
2032
2033 # We typically link to shared libraries using an absolute path, and
2034 # that's how they are found at runtime. If we are going to
2035 # dynamically load one by basename, we must specify rpath. If we
2036 # are using a remote host, DejaGNU will link to the shared library
2037 # using a relative path, so again we must specify an rpath.
2038 if { $shlib_load || ($shlib_found && [is_remote host]) } {
2039 if { ([istarget "*-*-mingw*"]
2040 || [istarget *-*-cygwin*]
2041 || [istarget *-*-pe*]
2042 || [istarget hppa*-*-hpux*])} {
2043 # Do not need anything.
2044 } elseif { [istarget *-*-openbsd*] } {
2045 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
2046 } elseif { [istarget arm*-*-symbianelf*] } {
2047 if { $shlib_load } {
2048 lappend new_options "libs=-ldl"
2049 }
2050 } else {
2051 if { $shlib_load } {
2052 lappend new_options "libs=-ldl"
2053 }
2054 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
2055 }
2056 }
2057 set options $new_options
2058
2059 if [target_info exists gdb_stub] {
2060 set options2 { "additional_flags=-Dusestubs" }
2061 lappend options "libs=[target_info gdb_stub]";
2062 set options [concat $options2 $options]
2063 }
2064 if [target_info exists is_vxworks] {
2065 set options2 { "additional_flags=-Dvxworks" }
2066 lappend options "libs=[target_info gdb_stub]";
2067 set options [concat $options2 $options]
2068 }
2069 if [info exists GDB_TESTCASE_OPTIONS] {
2070 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
2071 }
2072 verbose "options are $options"
2073 verbose "source is $source $dest $type $options"
2074
2075 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
2076
2077 if {[target_info exists needs_status_wrapper] && \
2078 [target_info needs_status_wrapper] != "0" && \
2079 [info exists gdb_wrapper_file]} {
2080 lappend options "libs=${gdb_wrapper_file}"
2081 lappend options "ldflags=${gdb_wrapper_flags}"
2082 }
2083
2084 # Replace the "nowarnings" option with the appropriate additional_flags
2085 # to disable compiler warnings.
2086 set nowarnings [lsearch -exact $options nowarnings]
2087 if {$nowarnings != -1} {
2088 if [target_info exists gdb,nowarnings_flag] {
2089 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
2090 } else {
2091 set flag "additional_flags=-w"
2092 }
2093 set options [lreplace $options $nowarnings $nowarnings $flag]
2094 }
2095
2096 if { $type == "executable" } {
2097 if { ([istarget "*-*-mingw*"]
2098 || [istarget "*-*-*djgpp"]
2099 || [istarget "*-*-cygwin*"])} {
2100 # Force output to unbuffered mode, by linking in an object file
2101 # with a global contructor that calls setvbuf.
2102 #
2103 # Compile the special object seperatelly for two reasons:
2104 # 1) Insulate it from $options.
2105 # 2) Avoid compiling it for every gdb_compile invocation,
2106 # which is time consuming, especially if we're remote
2107 # host testing.
2108 #
2109 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
2110 verbose "compiling gdb_saved_set_unbuffered_obj"
2111 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
2112 set unbuf_obj ${objdir}/set_unbuffered_mode.o
2113
2114 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
2115 if { $result != "" } {
2116 return $result
2117 }
2118
2119 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
2120 # Link a copy of the output object, because the
2121 # original may be automatically deleted.
2122 remote_exec host "cp -f $unbuf_obj $gdb_saved_set_unbuffered_mode_obj"
2123 } else {
2124 verbose "gdb_saved_set_unbuffered_obj already compiled"
2125 }
2126
2127 # Rely on the internal knowledge that the global ctors are ran in
2128 # reverse link order. In that case, we can use ldflags to
2129 # avoid copying the object file to the host multiple
2130 # times.
2131 # This object can only be added if standard libraries are
2132 # used. Thus, we need to disable it if -nostdlib option is used
2133 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
2134 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
2135 }
2136 }
2137 }
2138
2139 set result [target_compile $source $dest $type $options];
2140
2141 # Prune uninteresting compiler (and linker) output.
2142 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
2143
2144 regsub "\[\r\n\]*$" "$result" "" result;
2145 regsub "^\[\r\n\]*" "$result" "" result;
2146
2147 if {[lsearch $options quiet] < 0} {
2148 # We shall update this on a per language basis, to avoid
2149 # changing the entire testsuite in one go.
2150 if {[lsearch $options f77] >= 0} {
2151 gdb_compile_test $source $result
2152 } elseif { $result != "" } {
2153 clone_output "gdb compile failed, $result"
2154 }
2155 }
2156 return $result;
2157 }
2158
2159
2160 # This is just like gdb_compile, above, except that it tries compiling
2161 # against several different thread libraries, to see which one this
2162 # system has.
2163 proc gdb_compile_pthreads {source dest type options} {
2164 set built_binfile 0
2165 set why_msg "unrecognized error"
2166 foreach lib {-lpthreads -lpthread -lthread ""} {
2167 # This kind of wipes out whatever libs the caller may have
2168 # set. Or maybe theirs will override ours. How infelicitous.
2169 set options_with_lib [concat $options [list libs=$lib quiet]]
2170 set ccout [gdb_compile $source $dest $type $options_with_lib]
2171 switch -regexp -- $ccout {
2172 ".*no posix threads support.*" {
2173 set why_msg "missing threads include file"
2174 break
2175 }
2176 ".*cannot open -lpthread.*" {
2177 set why_msg "missing runtime threads library"
2178 }
2179 ".*Can't find library for -lpthread.*" {
2180 set why_msg "missing runtime threads library"
2181 }
2182 {^$} {
2183 pass "successfully compiled posix threads test case"
2184 set built_binfile 1
2185 break
2186 }
2187 }
2188 }
2189 if {!$built_binfile} {
2190 unsupported "Couldn't compile $source: ${why_msg}"
2191 return -1
2192 }
2193 }
2194
2195 # Build a shared library from SOURCES. You must use get_compiler_info
2196 # first.
2197
2198 proc gdb_compile_shlib {sources dest options} {
2199 set obj_options $options
2200
2201 switch -glob [test_compiler_info] {
2202 "xlc-*" {
2203 lappend obj_options "additional_flags=-qpic"
2204 }
2205 "gcc-*" {
2206 if { !([istarget "powerpc*-*-aix*"]
2207 || [istarget "rs6000*-*-aix*"]
2208 || [istarget "*-*-cygwin*"]
2209 || [istarget "*-*-mingw*"]
2210 || [istarget "*-*-pe*"]) } {
2211 lappend obj_options "additional_flags=-fpic"
2212 }
2213 }
2214 default {
2215 switch -glob [istarget] {
2216 "hppa*-hp-hpux*" {
2217 lappend obj_options "additional_flags=+z"
2218 }
2219 "mips-sgi-irix*" {
2220 # Disable SGI compiler's implicit -Dsgi
2221 lappend obj_options "additional_flags=-Usgi"
2222 }
2223 default {
2224 # don't know what the compiler is...
2225 }
2226 }
2227 }
2228 }
2229
2230 set outdir [file dirname $dest]
2231 set objects ""
2232 foreach source $sources {
2233 set sourcebase [file tail $source]
2234 if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
2235 return -1
2236 }
2237 lappend objects ${outdir}/${sourcebase}.o
2238 }
2239
2240 if [istarget "hppa*-*-hpux*"] {
2241 remote_exec build "ld -b ${objects} -o ${dest}"
2242 } else {
2243 set link_options $options
2244 if [test_compiler_info "xlc-*"] {
2245 lappend link_options "additional_flags=-qmkshrobj"
2246 } else {
2247 lappend link_options "additional_flags=-shared"
2248
2249 if { ([istarget "*-*-mingw*"]
2250 || [istarget *-*-cygwin*]
2251 || [istarget *-*-pe*])} {
2252 lappend link_options "additional_flags=-Wl,--out-implib,${dest}.a"
2253 }
2254 }
2255 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
2256 return -1
2257 }
2258 }
2259 }
2260
2261 # This is just like gdb_compile_pthreads, above, except that we always add the
2262 # objc library for compiling Objective-C programs
2263 proc gdb_compile_objc {source dest type options} {
2264 set built_binfile 0
2265 set why_msg "unrecognized error"
2266 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
2267 # This kind of wipes out whatever libs the caller may have
2268 # set. Or maybe theirs will override ours. How infelicitous.
2269 if { $lib == "solaris" } {
2270 set lib "-lpthread -lposix4"
2271 }
2272 if { $lib != "-lobjc" } {
2273 set lib "-lobjc $lib"
2274 }
2275 set options_with_lib [concat $options [list libs=$lib quiet]]
2276 set ccout [gdb_compile $source $dest $type $options_with_lib]
2277 switch -regexp -- $ccout {
2278 ".*no posix threads support.*" {
2279 set why_msg "missing threads include file"
2280 break
2281 }
2282 ".*cannot open -lpthread.*" {
2283 set why_msg "missing runtime threads library"
2284 }
2285 ".*Can't find library for -lpthread.*" {
2286 set why_msg "missing runtime threads library"
2287 }
2288 {^$} {
2289 pass "successfully compiled objc with posix threads test case"
2290 set built_binfile 1
2291 break
2292 }
2293 }
2294 }
2295 if {!$built_binfile} {
2296 unsupported "Couldn't compile $source: ${why_msg}"
2297 return -1
2298 }
2299 }
2300
2301 proc send_gdb { string } {
2302 global suppress_flag;
2303 if { $suppress_flag } {
2304 return "suppressed";
2305 }
2306 return [remote_send host "$string"];
2307 }
2308
2309 #
2310 #
2311
2312 proc gdb_expect { args } {
2313 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
2314 set atimeout [lindex $args 0];
2315 set expcode [list [lindex $args 1]];
2316 } else {
2317 set expcode $args;
2318 }
2319
2320 upvar timeout timeout;
2321
2322 if [target_info exists gdb,timeout] {
2323 if [info exists timeout] {
2324 if { $timeout < [target_info gdb,timeout] } {
2325 set gtimeout [target_info gdb,timeout];
2326 } else {
2327 set gtimeout $timeout;
2328 }
2329 } else {
2330 set gtimeout [target_info gdb,timeout];
2331 }
2332 }
2333
2334 if ![info exists gtimeout] {
2335 global timeout;
2336 if [info exists timeout] {
2337 set gtimeout $timeout;
2338 }
2339 }
2340
2341 if [info exists atimeout] {
2342 if { ![info exists gtimeout] || $gtimeout < $atimeout } {
2343 set gtimeout $atimeout;
2344 }
2345 } else {
2346 if ![info exists gtimeout] {
2347 # Eeeeew.
2348 set gtimeout 60;
2349 }
2350 }
2351
2352 global suppress_flag;
2353 global remote_suppress_flag;
2354 if [info exists remote_suppress_flag] {
2355 set old_val $remote_suppress_flag;
2356 }
2357 if [info exists suppress_flag] {
2358 if { $suppress_flag } {
2359 set remote_suppress_flag 1;
2360 }
2361 }
2362 set code [catch \
2363 {uplevel remote_expect host $gtimeout $expcode} string];
2364 if [info exists old_val] {
2365 set remote_suppress_flag $old_val;
2366 } else {
2367 if [info exists remote_suppress_flag] {
2368 unset remote_suppress_flag;
2369 }
2370 }
2371
2372 if {$code == 1} {
2373 global errorInfo errorCode;
2374
2375 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
2376 } elseif {$code == 2} {
2377 return -code return $string
2378 } elseif {$code == 3} {
2379 return
2380 } elseif {$code > 4} {
2381 return -code $code $string
2382 }
2383 }
2384
2385 # gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
2386 #
2387 # Check for long sequence of output by parts.
2388 # TEST: is the test message to be printed with the test success/fail.
2389 # SENTINEL: Is the terminal pattern indicating that output has finished.
2390 # LIST: is the sequence of outputs to match.
2391 # If the sentinel is recognized early, it is considered an error.
2392 #
2393 # Returns:
2394 # 1 if the test failed,
2395 # 0 if the test passes,
2396 # -1 if there was an internal error.
2397
2398 proc gdb_expect_list {test sentinel list} {
2399 global gdb_prompt
2400 global suppress_flag
2401 set index 0
2402 set ok 1
2403 if { $suppress_flag } {
2404 set ok 0
2405 unresolved "${test}"
2406 }
2407 while { ${index} < [llength ${list}] } {
2408 set pattern [lindex ${list} ${index}]
2409 set index [expr ${index} + 1]
2410 verbose -log "gdb_expect_list pattern: /$pattern/" 2
2411 if { ${index} == [llength ${list}] } {
2412 if { ${ok} } {
2413 gdb_expect {
2414 -re "${pattern}${sentinel}" {
2415 # pass "${test}, pattern ${index} + sentinel"
2416 }
2417 -re "${sentinel}" {
2418 fail "${test} (pattern ${index} + sentinel)"
2419 set ok 0
2420 }
2421 -re ".*A problem internal to GDB has been detected" {
2422 fail "${test} (GDB internal error)"
2423 set ok 0
2424 gdb_internal_error_resync
2425 }
2426 timeout {
2427 fail "${test} (pattern ${index} + sentinel) (timeout)"
2428 set ok 0
2429 }
2430 }
2431 } else {
2432 # unresolved "${test}, pattern ${index} + sentinel"
2433 }
2434 } else {
2435 if { ${ok} } {
2436 gdb_expect {
2437 -re "${pattern}" {
2438 # pass "${test}, pattern ${index}"
2439 }
2440 -re "${sentinel}" {
2441 fail "${test} (pattern ${index})"
2442 set ok 0
2443 }
2444 -re ".*A problem internal to GDB has been detected" {
2445 fail "${test} (GDB internal error)"
2446 set ok 0
2447 gdb_internal_error_resync
2448 }
2449 timeout {
2450 fail "${test} (pattern ${index}) (timeout)"
2451 set ok 0
2452 }
2453 }
2454 } else {
2455 # unresolved "${test}, pattern ${index}"
2456 }
2457 }
2458 }
2459 if { ${ok} } {
2460 pass "${test}"
2461 return 0
2462 } else {
2463 return 1
2464 }
2465 }
2466
2467 #
2468 #
2469 proc gdb_suppress_entire_file { reason } {
2470 global suppress_flag;
2471
2472 warning "$reason\n";
2473 set suppress_flag -1;
2474 }
2475
2476 #
2477 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
2478 # gdb_expect to fail immediately (until the next call to
2479 # gdb_stop_suppressing_tests).
2480 #
2481 proc gdb_suppress_tests { args } {
2482 global suppress_flag;
2483
2484 return; # fnf - disable pending review of results where
2485 # testsuite ran better without this
2486 incr suppress_flag;
2487
2488 if { $suppress_flag == 1 } {
2489 if { [llength $args] > 0 } {
2490 warning "[lindex $args 0]\n";
2491 } else {
2492 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
2493 }
2494 }
2495 }
2496
2497 #
2498 # Clear suppress_flag.
2499 #
2500 proc gdb_stop_suppressing_tests { } {
2501 global suppress_flag;
2502
2503 if [info exists suppress_flag] {
2504 if { $suppress_flag > 0 } {
2505 set suppress_flag 0;
2506 clone_output "Tests restarted.\n";
2507 }
2508 } else {
2509 set suppress_flag 0;
2510 }
2511 }
2512
2513 proc gdb_clear_suppressed { } {
2514 global suppress_flag;
2515
2516 set suppress_flag 0;
2517 }
2518
2519 proc gdb_start { } {
2520 default_gdb_start
2521 }
2522
2523 proc gdb_exit { } {
2524 catch default_gdb_exit
2525 }
2526
2527 #
2528 # gdb_load_cmd -- load a file into the debugger.
2529 # ARGS - additional args to load command.
2530 # return a -1 if anything goes wrong.
2531 #
2532 proc gdb_load_cmd { args } {
2533 global gdb_prompt
2534
2535 if [target_info exists gdb_load_timeout] {
2536 set loadtimeout [target_info gdb_load_timeout]
2537 } else {
2538 set loadtimeout 1600
2539 }
2540 send_gdb "load $args\n"
2541 verbose "Timeout is now $loadtimeout seconds" 2
2542 gdb_expect $loadtimeout {
2543 -re "Loading section\[^\r\]*\r\n" {
2544 exp_continue
2545 }
2546 -re "Start address\[\r\]*\r\n" {
2547 exp_continue
2548 }
2549 -re "Transfer rate\[\r\]*\r\n" {
2550 exp_continue
2551 }
2552 -re "Memory access error\[^\r\]*\r\n" {
2553 perror "Failed to load program"
2554 return -1
2555 }
2556 -re "$gdb_prompt $" {
2557 return 0
2558 }
2559 -re "(.*)\r\n$gdb_prompt " {
2560 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
2561 return -1
2562 }
2563 timeout {
2564 perror "Timed out trying to load $args."
2565 return -1
2566 }
2567 }
2568 return -1
2569 }
2570
2571 # Return the filename to download to the target and load on the target
2572 # for this shared library. Normally just LIBNAME, unless shared libraries
2573 # for this target have separate link and load images.
2574
2575 proc shlib_target_file { libname } {
2576 return $libname
2577 }
2578
2579 # Return the filename GDB will load symbols from when debugging this
2580 # shared library. Normally just LIBNAME, unless shared libraries for
2581 # this target have separate link and load images.
2582
2583 proc shlib_symbol_file { libname } {
2584 return $libname
2585 }
2586
2587 # gdb_download
2588 #
2589 # Copy a file to the remote target and return its target filename.
2590 # Schedule the file to be deleted at the end of this test.
2591
2592 proc gdb_download { filename } {
2593 global cleanfiles
2594
2595 set destname [remote_download target $filename]
2596 lappend cleanfiles $destname
2597 return $destname
2598 }
2599
2600 # gdb_load_shlibs LIB...
2601 #
2602 # Copy the listed libraries to the target.
2603
2604 proc gdb_load_shlibs { args } {
2605 if {![is_remote target]} {
2606 return
2607 }
2608
2609 foreach file $args {
2610 gdb_download [shlib_target_file $file]
2611 }
2612
2613 # Even if the target supplies full paths for shared libraries,
2614 # they may not be paths for this system.
2615 gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "" ""
2616 }
2617
2618 #
2619 # gdb_load -- load a file into the debugger.
2620 # Many files in config/*.exp override this procedure.
2621 #
2622 proc gdb_load { arg } {
2623 return [gdb_file_cmd $arg]
2624 }
2625
2626 # gdb_reload -- load a file into the target. Called before "running",
2627 # either the first time or after already starting the program once,
2628 # for remote targets. Most files that override gdb_load should now
2629 # override this instead.
2630
2631 proc gdb_reload { } {
2632 # For the benefit of existing configurations, default to gdb_load.
2633 # Specifying no file defaults to the executable currently being
2634 # debugged.
2635 return [gdb_load ""]
2636 }
2637
2638 proc gdb_continue { function } {
2639 global decimal
2640
2641 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
2642 }
2643
2644 proc default_gdb_init { args } {
2645 global gdb_wrapper_initialized
2646 global gdb_wrapper_target
2647 global cleanfiles
2648
2649 set cleanfiles {}
2650
2651 gdb_clear_suppressed;
2652
2653 # Make sure that the wrapper is rebuilt
2654 # with the appropriate multilib option.
2655 if { $gdb_wrapper_target != [current_target_name] } {
2656 set gdb_wrapper_initialized 0
2657 }
2658
2659 # Unlike most tests, we have a small number of tests that generate
2660 # a very large amount of output. We therefore increase the expect
2661 # buffer size to be able to contain the entire test output.
2662 match_max -d 30000
2663 # Also set this value for the currently running GDB.
2664 match_max [match_max -d]
2665
2666 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
2667 if { [llength $args] > 0 } {
2668 global pf_prefix
2669
2670 set file [lindex $args 0];
2671
2672 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
2673 }
2674 global gdb_prompt;
2675 if [target_info exists gdb_prompt] {
2676 set gdb_prompt [target_info gdb_prompt];
2677 } else {
2678 set gdb_prompt "\\(gdb\\)"
2679 }
2680 }
2681
2682 # The default timeout used when testing GDB commands. We want to use
2683 # the same timeout as the default dejagnu timeout, unless the user has
2684 # already provided a specific value (probably through a site.exp file).
2685 global gdb_test_timeout
2686 if ![info exists gdb_test_timeout] {
2687 set gdb_test_timeout $timeout
2688 }
2689
2690 # A list of global variables that GDB testcases should not use.
2691 # We try to prevent their use by monitoring write accesses and raising
2692 # an error when that happens.
2693 set banned_variables { bug_id prms_id }
2694
2695 # gdb_init is called by runtest at start, but also by several
2696 # tests directly; gdb_finish is only called from within runtest after
2697 # each test source execution.
2698 # Placing several traces by repetitive calls to gdb_init leads
2699 # to problems, as only one trace is removed in gdb_finish.
2700 # To overcome this possible problem, we add a variable that records
2701 # if the banned variables are traced.
2702 set banned_variables_traced 0
2703
2704 proc gdb_init { args } {
2705 # Reset the timeout value to the default. This way, any testcase
2706 # that changes the timeout value without resetting it cannot affect
2707 # the timeout used in subsequent testcases.
2708 global gdb_test_timeout
2709 global timeout
2710 set timeout $gdb_test_timeout
2711
2712 # Block writes to all banned variables...
2713 global banned_variables
2714 global banned_variables_traced
2715 if (!$banned_variables_traced) {
2716 foreach banned_var $banned_variables {
2717 global "$banned_var"
2718 trace add variable "$banned_var" write error
2719 }
2720 set banned_variables_traced 1
2721 }
2722
2723 # We set LC_ALL and LANG to C so that we get the same messages as
2724 # expected.
2725 setenv LC_ALL C
2726 setenv LANG C
2727
2728 return [eval default_gdb_init $args];
2729 }
2730
2731 proc gdb_finish { } {
2732 global cleanfiles
2733
2734 # Exit first, so that the files are no longer in use.
2735 gdb_exit
2736
2737 if { [llength $cleanfiles] > 0 } {
2738 eval remote_file target delete $cleanfiles
2739 set cleanfiles {}
2740 }
2741
2742 # Unblock write access to the banned variables. Dejagnu typically
2743 # resets some of them between testcases.
2744 global banned_variables
2745 global banned_variables_traced
2746 if ($banned_variables_traced) {
2747 foreach banned_var $banned_variables {
2748 global "$banned_var"
2749 trace remove variable "$banned_var" write error
2750 }
2751 set banned_variables_traced 0
2752 }
2753 }
2754
2755 global debug_format
2756 set debug_format "unknown"
2757
2758 # Run the gdb command "info source" and extract the debugging format
2759 # information from the output and save it in debug_format.
2760
2761 proc get_debug_format { } {
2762 global gdb_prompt
2763 global verbose
2764 global expect_out
2765 global debug_format
2766
2767 set debug_format "unknown"
2768 send_gdb "info source\n"
2769 gdb_expect 10 {
2770 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
2771 set debug_format $expect_out(1,string)
2772 verbose "debug format is $debug_format"
2773 return 1;
2774 }
2775 -re "No current source file.\r\n$gdb_prompt $" {
2776 perror "get_debug_format used when no current source file"
2777 return 0;
2778 }
2779 -re "$gdb_prompt $" {
2780 warning "couldn't check debug format (no valid response)."
2781 return 1;
2782 }
2783 timeout {
2784 warning "couldn't check debug format (timed out)."
2785 return 1;
2786 }
2787 }
2788 }
2789
2790 # Return true if FORMAT matches the debug format the current test was
2791 # compiled with. FORMAT is a shell-style globbing pattern; it can use
2792 # `*', `[...]', and so on.
2793 #
2794 # This function depends on variables set by `get_debug_format', above.
2795
2796 proc test_debug_format {format} {
2797 global debug_format
2798
2799 return [expr [string match $format $debug_format] != 0]
2800 }
2801
2802 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
2803 # COFF, stabs, etc). If that format matches the format that the
2804 # current test was compiled with, then the next test is expected to
2805 # fail for any target. Returns 1 if the next test or set of tests is
2806 # expected to fail, 0 otherwise (or if it is unknown). Must have
2807 # previously called get_debug_format.
2808 proc setup_xfail_format { format } {
2809 set ret [test_debug_format $format];
2810
2811 if {$ret} then {
2812 setup_xfail "*-*-*"
2813 }
2814 return $ret;
2815 }
2816
2817 proc gdb_step_for_stub { } {
2818 global gdb_prompt;
2819
2820 if ![target_info exists gdb,use_breakpoint_for_stub] {
2821 if [target_info exists gdb_stub_step_command] {
2822 set command [target_info gdb_stub_step_command];
2823 } else {
2824 set command "step";
2825 }
2826 send_gdb "${command}\n";
2827 set tries 0;
2828 gdb_expect 60 {
2829 -re "(main.* at |.*in .*start).*$gdb_prompt" {
2830 return;
2831 }
2832 -re ".*$gdb_prompt" {
2833 incr tries;
2834 if { $tries == 5 } {
2835 fail "stepping out of breakpoint function";
2836 return;
2837 }
2838 send_gdb "${command}\n";
2839 exp_continue;
2840 }
2841 default {
2842 fail "stepping out of breakpoint function";
2843 return;
2844 }
2845 }
2846 }
2847 send_gdb "where\n";
2848 gdb_expect {
2849 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
2850 set file $expect_out(1,string);
2851 set linenum [expr $expect_out(2,string) + 1];
2852 set breakplace "${file}:${linenum}";
2853 }
2854 default {}
2855 }
2856 send_gdb "break ${breakplace}\n";
2857 gdb_expect 60 {
2858 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
2859 set breakpoint $expect_out(1,string);
2860 }
2861 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
2862 set breakpoint $expect_out(1,string);
2863 }
2864 default {}
2865 }
2866 send_gdb "continue\n";
2867 gdb_expect 60 {
2868 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
2869 gdb_test "delete $breakpoint" ".*" "";
2870 return;
2871 }
2872 default {}
2873 }
2874 }
2875
2876 # gdb_get_line_number TEXT [FILE]
2877 #
2878 # Search the source file FILE, and return the line number of the
2879 # first line containing TEXT. If no match is found, return -1.
2880 #
2881 # TEXT is a string literal, not a regular expression.
2882 #
2883 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
2884 # specified, and does not start with "/", then it is assumed to be in
2885 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
2886 # by changing the callers and the interface at the same time.
2887 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
2888 # gdb.base/ena-dis-br.exp.
2889 #
2890 # Use this function to keep your test scripts independent of the
2891 # exact line numbering of the source file. Don't write:
2892 #
2893 # send_gdb "break 20"
2894 #
2895 # This means that if anyone ever edits your test's source file,
2896 # your test could break. Instead, put a comment like this on the
2897 # source file line you want to break at:
2898 #
2899 # /* breakpoint spot: frotz.exp: test name */
2900 #
2901 # and then write, in your test script (which we assume is named
2902 # frotz.exp):
2903 #
2904 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
2905 #
2906 # (Yes, Tcl knows how to handle the nested quotes and brackets.
2907 # Try this:
2908 # $ tclsh
2909 # % puts "foo [lindex "bar baz" 1]"
2910 # foo baz
2911 # %
2912 # Tcl is quite clever, for a little stringy language.)
2913 #
2914 # ===
2915 #
2916 # The previous implementation of this procedure used the gdb search command.
2917 # This version is different:
2918 #
2919 # . It works with MI, and it also works when gdb is not running.
2920 #
2921 # . It operates on the build machine, not the host machine.
2922 #
2923 # . For now, this implementation fakes a current directory of
2924 # $srcdir/$subdir to be compatible with the old implementation.
2925 # This will go away eventually and some callers will need to
2926 # be changed.
2927 #
2928 # . The TEXT argument is literal text and matches literally,
2929 # not a regular expression as it was before.
2930 #
2931 # . State changes in gdb, such as changing the current file
2932 # and setting $_, no longer happen.
2933 #
2934 # After a bit of time we can forget about the differences from the
2935 # old implementation.
2936 #
2937 # --chastain 2004-08-05
2938
2939 proc gdb_get_line_number { text { file "" } } {
2940 global srcdir
2941 global subdir
2942 global srcfile
2943
2944 if { "$file" == "" } then {
2945 set file "$srcfile"
2946 }
2947 if { ! [regexp "^/" "$file"] } then {
2948 set file "$srcdir/$subdir/$file"
2949 }
2950
2951 if { [ catch { set fd [open "$file"] } message ] } then {
2952 perror "$message"
2953 return -1
2954 }
2955
2956 set found -1
2957 for { set line 1 } { 1 } { incr line } {
2958 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
2959 perror "$message"
2960 return -1
2961 }
2962 if { $nchar < 0 } then {
2963 break
2964 }
2965 if { [string first "$text" "$body"] >= 0 } then {
2966 set found $line
2967 break
2968 }
2969 }
2970
2971 if { [ catch { close "$fd" } message ] } then {
2972 perror "$message"
2973 return -1
2974 }
2975
2976 return $found
2977 }
2978
2979 # gdb_continue_to_end:
2980 # The case where the target uses stubs has to be handled specially. If a
2981 # stub is used, we set a breakpoint at exit because we cannot rely on
2982 # exit() behavior of a remote target.
2983 #
2984 # mssg is the error message that gets printed.
2985
2986 proc gdb_continue_to_end {mssg} {
2987 if [target_info exists use_gdb_stub] {
2988 if {![gdb_breakpoint "exit"]} {
2989 return 0
2990 }
2991 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
2992 "continue until exit at $mssg"
2993 } else {
2994 # Continue until we exit. Should not stop again.
2995 # Don't bother to check the output of the program, that may be
2996 # extremely tough for some remote systems.
2997 gdb_test "continue"\
2998 "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
2999 "continue until exit at $mssg"
3000 }
3001 }
3002
3003 proc rerun_to_main {} {
3004 global gdb_prompt
3005
3006 if [target_info exists use_gdb_stub] {
3007 gdb_run_cmd
3008 gdb_expect {
3009 -re ".*Breakpoint .*main .*$gdb_prompt $"\
3010 {pass "rerun to main" ; return 0}
3011 -re "$gdb_prompt $"\
3012 {fail "rerun to main" ; return 0}
3013 timeout {fail "(timeout) rerun to main" ; return 0}
3014 }
3015 } else {
3016 send_gdb "run\n"
3017 gdb_expect {
3018 -re "The program .* has been started already.*y or n. $" {
3019 send_gdb "y\n"
3020 exp_continue
3021 }
3022 -re "Starting program.*$gdb_prompt $"\
3023 {pass "rerun to main" ; return 0}
3024 -re "$gdb_prompt $"\
3025 {fail "rerun to main" ; return 0}
3026 timeout {fail "(timeout) rerun to main" ; return 0}
3027 }
3028 }
3029 }
3030
3031 # Print a message and return true if a test should be skipped
3032 # due to lack of floating point suport.
3033
3034 proc gdb_skip_float_test { msg } {
3035 if [target_info exists gdb,skip_float_tests] {
3036 verbose "Skipping test '$msg': no float tests.";
3037 return 1;
3038 }
3039 return 0;
3040 }
3041
3042 # Print a message and return true if a test should be skipped
3043 # due to lack of stdio support.
3044
3045 proc gdb_skip_stdio_test { msg } {
3046 if [target_info exists gdb,noinferiorio] {
3047 verbose "Skipping test '$msg': no inferior i/o.";
3048 return 1;
3049 }
3050 return 0;
3051 }
3052
3053 proc gdb_skip_bogus_test { msg } {
3054 return 0;
3055 }
3056
3057 # Return true if a test should be skipped due to lack of XML support
3058 # in the host GDB.
3059 # NOTE: This must be called while gdb is *not* running.
3060
3061 proc gdb_skip_xml_test { } {
3062 global gdb_prompt
3063 global srcdir
3064 global xml_missing_cached
3065
3066 if {[info exists xml_missing_cached]} {
3067 return $xml_missing_cached
3068 }
3069
3070 gdb_start
3071 set xml_missing_cached 0
3072 gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" {
3073 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
3074 set xml_missing_cached 1
3075 }
3076 -re ".*$gdb_prompt $" { }
3077 }
3078 gdb_exit
3079 return $xml_missing_cached
3080 }
3081
3082 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
3083 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
3084 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
3085 # the name of a debuginfo only file. This file will be stored in the same
3086 # subdirectory.
3087
3088 # Functions for separate debug info testing
3089
3090 # starting with an executable:
3091 # foo --> original executable
3092
3093 # at the end of the process we have:
3094 # foo.stripped --> foo w/o debug info
3095 # foo.debug --> foo's debug info
3096 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
3097
3098 # Return the build-id hex string (usually 160 bits as 40 hex characters)
3099 # converted to the form: .build-id/ab/cdef1234...89.debug
3100 # Return "" if no build-id found.
3101 proc build_id_debug_filename_get { exec } {
3102 set tmp "${exec}-tmp"
3103 set objcopy_program [transform objcopy]
3104
3105 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $exec $tmp" output]
3106 verbose "result is $result"
3107 verbose "output is $output"
3108 if {$result == 1} {
3109 return ""
3110 }
3111 set fi [open $tmp]
3112 fconfigure $fi -translation binary
3113 # Skip the NOTE header.
3114 read $fi 16
3115 set data [read $fi]
3116 close $fi
3117 file delete $tmp
3118 if ![string compare $data ""] then {
3119 return ""
3120 }
3121 # Convert it to hex.
3122 binary scan $data H* data
3123 regsub {^..} $data {\0/} data
3124 return ".build-id/${data}.debug";
3125 }
3126
3127 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
3128 # list of optional flags. The only currently supported flag is no-main,
3129 # which removes the symbol entry for main from the separate debug file.
3130 #
3131 # Function returns zero on success. Function will return non-zero failure code
3132 # on some targets not supporting separate debug info (such as i386-msdos).
3133
3134 proc gdb_gnu_strip_debug { dest args } {
3135
3136 # Use the first separate debug info file location searched by GDB so the
3137 # run cannot be broken by some stale file searched with higher precedence.
3138 set debug_file "${dest}.debug"
3139
3140 set strip_to_file_program [transform strip]
3141 set objcopy_program [transform objcopy]
3142
3143 set debug_link [file tail $debug_file]
3144 set stripped_file "${dest}.stripped"
3145
3146 # Get rid of the debug info, and store result in stripped_file
3147 # something like gdb/testsuite/gdb.base/blah.stripped.
3148 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
3149 verbose "result is $result"
3150 verbose "output is $output"
3151 if {$result == 1} {
3152 return 1
3153 }
3154
3155 # Workaround PR binutils/10802:
3156 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3157 set perm [file attributes ${dest} -permissions]
3158 file attributes ${stripped_file} -permissions $perm
3159
3160 # Get rid of everything but the debug info, and store result in debug_file
3161 # This will be in the .debug subdirectory, see above.
3162 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
3163 verbose "result is $result"
3164 verbose "output is $output"
3165 if {$result == 1} {
3166 return 1
3167 }
3168
3169 # If no-main is passed, strip the symbol for main from the separate
3170 # file. This is to simulate the behavior of elfutils's eu-strip, which
3171 # leaves the symtab in the original file only. There's no way to get
3172 # objcopy or strip to remove the symbol table without also removing the
3173 # debugging sections, so this is as close as we can get.
3174 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
3175 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
3176 verbose "result is $result"
3177 verbose "output is $output"
3178 if {$result == 1} {
3179 return 1
3180 }
3181 file delete "${debug_file}"
3182 file rename "${debug_file}-tmp" "${debug_file}"
3183 }
3184
3185 # Link the two previous output files together, adding the .gnu_debuglink
3186 # section to the stripped_file, containing a pointer to the debug_file,
3187 # save the new file in dest.
3188 # This will be the regular executable filename, in the usual location.
3189 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
3190 verbose "result is $result"
3191 verbose "output is $output"
3192 if {$result == 1} {
3193 return 1
3194 }
3195
3196 # Workaround PR binutils/10802:
3197 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3198 set perm [file attributes ${stripped_file} -permissions]
3199 file attributes ${dest} -permissions $perm
3200
3201 return 0
3202 }
3203
3204 # Test the output of GDB_COMMAND matches the pattern obtained
3205 # by concatenating all elements of EXPECTED_LINES. This makes
3206 # it possible to split otherwise very long string into pieces.
3207 # If third argument is not empty, it's used as the name of the
3208 # test to be printed on pass/fail.
3209 proc help_test_raw { gdb_command expected_lines args } {
3210 set message $gdb_command
3211 if [llength $args]>0 then {
3212 set message [lindex $args 0]
3213 }
3214 set expected_output [join $expected_lines ""]
3215 gdb_test "${gdb_command}" "${expected_output}" $message
3216 }
3217
3218 # Test the output of "help COMMNAD_CLASS". EXPECTED_INITIAL_LINES
3219 # are regular expressions that should match the beginning of output,
3220 # before the list of commands in that class. The presence of
3221 # command list and standard epilogue will be tested automatically.
3222 proc test_class_help { command_class expected_initial_lines args } {
3223 set l_stock_body {
3224 "List of commands\:.*\[\r\n\]+"
3225 "Type \"help\" followed by command name for full documentation\.\[\r\n\]+"
3226 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+"
3227 "Command name abbreviations are allowed if unambiguous\."
3228 }
3229 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3230
3231 eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
3232 }
3233
3234 # COMMAND_LIST should have either one element -- command to test, or
3235 # two elements -- abbreviated command to test, and full command the first
3236 # element is abbreviation of.
3237 # The command must be a prefix command. EXPECTED_INITIAL_LINES
3238 # are regular expressions that should match the beginning of output,
3239 # before the list of subcommands. The presence of
3240 # subcommand list and standard epilogue will be tested automatically.
3241 proc test_prefix_command_help { command_list expected_initial_lines args } {
3242 set command [lindex $command_list 0]
3243 if {[llength $command_list]>1} {
3244 set full_command [lindex $command_list 1]
3245 } else {
3246 set full_command $command
3247 }
3248 # Use 'list' and not just {} because we want variables to
3249 # be expanded in this list.
3250 set l_stock_body [list\
3251 "List of $full_command subcommands\:.*\[\r\n\]+"\
3252 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
3253 "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
3254 "Command name abbreviations are allowed if unambiguous\."]
3255 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3256 if {[llength $args]>0} {
3257 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
3258 } else {
3259 help_test_raw "help ${command}" $l_entire_body
3260 }
3261 }
3262
3263 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
3264 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
3265 # to pass to untested, if something is wrong. OPTIONS are passed
3266 # to gdb_compile directly.
3267 proc build_executable { testname executable {sources ""} {options {debug}} } {
3268
3269 global objdir
3270 global subdir
3271 global srcdir
3272 if {[llength $sources]==0} {
3273 set sources ${executable}.c
3274 }
3275
3276 set binfile ${objdir}/${subdir}/${executable}
3277
3278 set objects {}
3279 for {set i 0} "\$i<[llength $sources]" {incr i} {
3280 set s [lindex $sources $i]
3281 if { [gdb_compile "${srcdir}/${subdir}/${s}" "${binfile}${i}.o" object $options] != "" } {
3282 untested $testname
3283 return -1
3284 }
3285 lappend objects "${binfile}${i}.o"
3286 }
3287
3288 if { [gdb_compile $objects "${binfile}" executable $options] != "" } {
3289 untested $testname
3290 return -1
3291 }
3292
3293 set info_options ""
3294 if { [lsearch -exact $options "c++"] >= 0 } {
3295 set info_options "c++"
3296 }
3297 if [get_compiler_info ${binfile} ${info_options}] {
3298 return -1
3299 }
3300 return 0
3301 }
3302
3303 # Starts fresh GDB binary and loads EXECUTABLE into GDB. EXECUTABLE is
3304 # the name of binary in ${objdir}/${subdir}.
3305 proc clean_restart { executable } {
3306 global srcdir
3307 global objdir
3308 global subdir
3309 set binfile ${objdir}/${subdir}/${executable}
3310
3311 gdb_exit
3312 gdb_start
3313 gdb_reinitialize_dir $srcdir/$subdir
3314 gdb_load ${binfile}
3315
3316 if [target_info exists gdb_stub] {
3317 gdb_step_for_stub;
3318 }
3319 }
3320
3321 # Prepares for testing, by calling build_executable, and then clean_restart.
3322 # Please refer to build_executable for parameter description.
3323 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
3324
3325 if {[build_executable $testname $executable $sources $options] == -1} {
3326 return -1
3327 }
3328 clean_restart $executable
3329
3330 return 0
3331 }
3332
3333 proc get_valueof { fmt exp default } {
3334 global gdb_prompt
3335
3336 set test "get valueof \"${exp}\""
3337 set val ${default}
3338 gdb_test_multiple "print${fmt} ${exp}" "$test" {
3339 -re "\\$\[0-9\]* = (.*)\[\r\n\]*$gdb_prompt $" {
3340 set val $expect_out(1,string)
3341 pass "$test ($val)"
3342 }
3343 timeout {
3344 fail "$test (timeout)"
3345 }
3346 }
3347 return ${val}
3348 }
3349
3350 proc get_integer_valueof { exp default } {
3351 global gdb_prompt
3352
3353 set test "get integer valueof \"${exp}\""
3354 set val ${default}
3355 gdb_test_multiple "print /d ${exp}" "$test" {
3356 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
3357 set val $expect_out(1,string)
3358 pass "$test ($val)"
3359 }
3360 timeout {
3361 fail "$test (timeout)"
3362 }
3363 }
3364 return ${val}
3365 }
3366
3367 proc get_hexadecimal_valueof { exp default } {
3368 global gdb_prompt
3369 send_gdb "print /x ${exp}\n"
3370 set test "get hexadecimal valueof \"${exp}\""
3371 gdb_expect {
3372 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
3373 set val $expect_out(1,string)
3374 pass "$test"
3375 }
3376 timeout {
3377 set val ${default}
3378 fail "$test (timeout)"
3379 }
3380 }
3381 return ${val}
3382 }
3383
3384 proc get_sizeof { type default } {
3385 return [get_integer_valueof "sizeof (${type})" $default]
3386 }
3387
3388 # Log gdb command line and script if requested.
3389 if {[info exists TRANSCRIPT]} {
3390 rename send_gdb real_send_gdb
3391 rename remote_spawn real_remote_spawn
3392 rename remote_close real_remote_close
3393
3394 global gdb_transcript
3395 set gdb_transcript ""
3396
3397 global gdb_trans_count
3398 set gdb_trans_count 1
3399
3400 proc remote_spawn {args} {
3401 global gdb_transcript gdb_trans_count outdir
3402
3403 if {$gdb_transcript != ""} {
3404 close $gdb_transcript
3405 }
3406 set gdb_transcript [open [file join $outdir transcript.$gdb_trans_count] w]
3407 puts $gdb_transcript [lindex $args 1]
3408 incr gdb_trans_count
3409
3410 return [uplevel real_remote_spawn $args]
3411 }
3412
3413 proc remote_close {args} {
3414 global gdb_transcript
3415
3416 if {$gdb_transcript != ""} {
3417 close $gdb_transcript
3418 set gdb_transcript ""
3419 }
3420
3421 return [uplevel real_remote_close $args]
3422 }
3423
3424 proc send_gdb {args} {
3425 global gdb_transcript
3426
3427 if {$gdb_transcript != ""} {
3428 puts -nonewline $gdb_transcript [lindex $args 0]
3429 }
3430
3431 return [uplevel real_send_gdb $args]
3432 }
3433 }
3434
3435 proc core_find {binfile {deletefiles {}} {arg ""}} {
3436 global objdir subdir
3437
3438 set destcore "$binfile.core"
3439 file delete $destcore
3440
3441 # Create a core file named "$destcore" rather than just "core", to
3442 # avoid problems with sys admin types that like to regularly prune all
3443 # files named "core" from the system.
3444 #
3445 # Arbitrarily try setting the core size limit to "unlimited" since
3446 # this does not hurt on systems where the command does not work and
3447 # allows us to generate a core on systems where it does.
3448 #
3449 # Some systems append "core" to the name of the program; others append
3450 # the name of the program to "core"; still others (like Linux, as of
3451 # May 2003) create cores named "core.PID". In the latter case, we
3452 # could have many core files lying around, and it may be difficult to
3453 # tell which one is ours, so let's run the program in a subdirectory.
3454 set found 0
3455 set coredir "${objdir}/${subdir}/coredir.[getpid]"
3456 file mkdir $coredir
3457 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
3458 # remote_exec host "${binfile}"
3459 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
3460 if [remote_file build exists $i] {
3461 remote_exec build "mv $i $destcore"
3462 set found 1
3463 }
3464 }
3465 # Check for "core.PID".
3466 if { $found == 0 } {
3467 set names [glob -nocomplain -directory $coredir core.*]
3468 if {[llength $names] == 1} {
3469 set corefile [file join $coredir [lindex $names 0]]
3470 remote_exec build "mv $corefile $destcore"
3471 set found 1
3472 }
3473 }
3474 if { $found == 0 } {
3475 # The braindamaged HPUX shell quits after the ulimit -c above
3476 # without executing ${binfile}. So we try again without the
3477 # ulimit here if we didn't find a core file above.
3478 # Oh, I should mention that any "braindamaged" non-Unix system has
3479 # the same problem. I like the cd bit too, it's really neat'n stuff.
3480 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
3481 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
3482 if [remote_file build exists $i] {
3483 remote_exec build "mv $i $destcore"
3484 set found 1
3485 }
3486 }
3487 }
3488
3489 # Try to clean up after ourselves.
3490 foreach deletefile $deletefiles {
3491 remote_file build delete [file join $coredir $deletefile]
3492 }
3493 remote_exec build "rmdir $coredir"
3494
3495 if { $found == 0 } {
3496 warning "can't generate a core file - core tests suppressed - check ulimit -c"
3497 return ""
3498 }
3499 return $destcore
3500 }