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