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