ChangeLog:
[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 || [istarget "arm*-*-*"]} {
1800 return 0
1801 }
1802
1803 return 1
1804 }
1805
1806 # Return a 1 if we should skip tests that require hardware watchpoints
1807
1808 proc skip_hw_watchpoint_tests {} {
1809 # Skip tests if requested by the board
1810 if { [target_info exists gdb,no_hardware_watchpoints]} {
1811 return 1
1812 }
1813
1814 # These targets support hardware watchpoints natively
1815 if { [istarget "i?86-*-*"]
1816 || [istarget "x86_64-*-*"]
1817 || [istarget "ia64-*-*"]
1818 || [istarget "arm*-*-*"]
1819 || [istarget "powerpc*-*-linux*"]
1820 || [istarget "s390*-*-*"] } {
1821 return 0
1822 }
1823
1824 return 1
1825 }
1826
1827 # Return a 1 if we should skip tests that require *multiple* hardware
1828 # watchpoints to be active at the same time
1829
1830 proc skip_hw_watchpoint_multi_tests {} {
1831 if { [skip_hw_watchpoint_tests] } {
1832 return 1
1833 }
1834
1835 # These targets support just a single hardware watchpoint
1836 if { [istarget "arm*-*-*"]
1837 || [istarget "powerpc*-*-linux*"] } {
1838 return 1
1839 }
1840
1841 return 0
1842 }
1843
1844 # Return a 1 if we should skip tests that require read/access watchpoints
1845
1846 proc skip_hw_watchpoint_access_tests {} {
1847 if { [skip_hw_watchpoint_tests] } {
1848 return 1
1849 }
1850
1851 # These targets support just write watchpoints
1852 if { [istarget "s390*-*-*"] } {
1853 return 1
1854 }
1855
1856 return 0
1857 }
1858
1859 set compiler_info "unknown"
1860 set gcc_compiled 0
1861 set hp_cc_compiler 0
1862 set hp_aCC_compiler 0
1863
1864 # Figure out what compiler I am using.
1865 #
1866 # BINFILE is a "compiler information" output file. This implementation
1867 # does not use BINFILE.
1868 #
1869 # ARGS can be empty or "C++". If empty, "C" is assumed.
1870 #
1871 # There are several ways to do this, with various problems.
1872 #
1873 # [ gdb_compile -E $ifile -o $binfile.ci ]
1874 # source $binfile.ci
1875 #
1876 # Single Unix Spec v3 says that "-E -o ..." together are not
1877 # specified. And in fact, the native compiler on hp-ux 11 (among
1878 # others) does not work with "-E -o ...". Most targets used to do
1879 # this, and it mostly worked, because it works with gcc.
1880 #
1881 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1882 # source $binfile.ci
1883 #
1884 # This avoids the problem with -E and -o together. This almost works
1885 # if the build machine is the same as the host machine, which is
1886 # usually true of the targets which are not gcc. But this code does
1887 # not figure which compiler to call, and it always ends up using the C
1888 # compiler. Not good for setting hp_aCC_compiler. Targets
1889 # hppa*-*-hpux* and mips*-*-irix* used to do this.
1890 #
1891 # [ gdb_compile -E $ifile > $binfile.ci ]
1892 # source $binfile.ci
1893 #
1894 # dejagnu target_compile says that it supports output redirection,
1895 # but the code is completely different from the normal path and I
1896 # don't want to sweep the mines from that path. So I didn't even try
1897 # this.
1898 #
1899 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1900 # eval $cppout
1901 #
1902 # I actually do this for all targets now. gdb_compile runs the right
1903 # compiler, and TCL captures the output, and I eval the output.
1904 #
1905 # Unfortunately, expect logs the output of the command as it goes by,
1906 # and dejagnu helpfully prints a second copy of it right afterwards.
1907 # So I turn off expect logging for a moment.
1908 #
1909 # [ gdb_compile $ifile $ciexe_file executable $args ]
1910 # [ remote_exec $ciexe_file ]
1911 # [ source $ci_file.out ]
1912 #
1913 # I could give up on -E and just do this.
1914 # I didn't get desperate enough to try this.
1915 #
1916 # -- chastain 2004-01-06
1917
1918 proc get_compiler_info {binfile args} {
1919 # For compiler.c and compiler.cc
1920 global srcdir
1921
1922 # I am going to play with the log to keep noise out.
1923 global outdir
1924 global tool
1925
1926 # These come from compiler.c or compiler.cc
1927 global compiler_info
1928
1929 # Legacy global data symbols.
1930 global gcc_compiled
1931 global hp_cc_compiler
1932 global hp_aCC_compiler
1933
1934 # Choose which file to preprocess.
1935 set ifile "${srcdir}/lib/compiler.c"
1936 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1937 set ifile "${srcdir}/lib/compiler.cc"
1938 }
1939
1940 # Run $ifile through the right preprocessor.
1941 # Toggle gdb.log to keep the compiler output out of the log.
1942 log_file
1943 if [is_remote host] {
1944 # We have to use -E and -o together, despite the comments
1945 # above, because of how DejaGnu handles remote host testing.
1946 set ppout "$outdir/compiler.i"
1947 gdb_compile "${ifile}" "$ppout" preprocess [list "$args" quiet]
1948 set file [open $ppout r]
1949 set cppout [read $file]
1950 close $file
1951 } else {
1952 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1953 }
1954 log_file -a "$outdir/$tool.log"
1955
1956 # Eval the output.
1957 set unknown 0
1958 foreach cppline [ split "$cppout" "\n" ] {
1959 if { [ regexp "^#" "$cppline" ] } {
1960 # line marker
1961 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1962 # blank line
1963 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
1964 # eval this line
1965 verbose "get_compiler_info: $cppline" 2
1966 eval "$cppline"
1967 } else {
1968 # unknown line
1969 verbose -log "get_compiler_info: $cppline"
1970 set unknown 1
1971 }
1972 }
1973
1974 # Reset to unknown compiler if any diagnostics happened.
1975 if { $unknown } {
1976 set compiler_info "unknown"
1977 }
1978
1979 # Set the legacy symbols.
1980 set gcc_compiled 0
1981 set hp_cc_compiler 0
1982 set hp_aCC_compiler 0
1983 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
1984 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
1985 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
1986 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
1987 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
1988 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
1989 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
1990
1991 # Log what happened.
1992 verbose -log "get_compiler_info: $compiler_info"
1993
1994 # Most compilers will evaluate comparisons and other boolean
1995 # operations to 0 or 1.
1996 uplevel \#0 { set true 1 }
1997 uplevel \#0 { set false 0 }
1998
1999 # Use of aCC results in boolean results being displayed as
2000 # "true" or "false"
2001 if { $hp_aCC_compiler } {
2002 uplevel \#0 { set true true }
2003 uplevel \#0 { set false false }
2004 }
2005
2006 return 0;
2007 }
2008
2009 proc test_compiler_info { {compiler ""} } {
2010 global compiler_info
2011
2012 # if no arg, return the compiler_info string
2013
2014 if [string match "" $compiler] {
2015 if [info exists compiler_info] {
2016 return $compiler_info
2017 } else {
2018 perror "No compiler info found."
2019 }
2020 }
2021
2022 return [string match $compiler $compiler_info]
2023 }
2024
2025 proc current_target_name { } {
2026 global target_info
2027 if [info exists target_info(target,name)] {
2028 set answer $target_info(target,name)
2029 } else {
2030 set answer ""
2031 }
2032 return $answer
2033 }
2034
2035 set gdb_wrapper_initialized 0
2036 set gdb_wrapper_target ""
2037
2038 proc gdb_wrapper_init { args } {
2039 global gdb_wrapper_initialized;
2040 global gdb_wrapper_file;
2041 global gdb_wrapper_flags;
2042 global gdb_wrapper_target
2043
2044 if { $gdb_wrapper_initialized == 1 } { return; }
2045
2046 if {[target_info exists needs_status_wrapper] && \
2047 [target_info needs_status_wrapper] != "0"} {
2048 set result [build_wrapper "testglue.o"];
2049 if { $result != "" } {
2050 set gdb_wrapper_file [lindex $result 0];
2051 set gdb_wrapper_flags [lindex $result 1];
2052 } else {
2053 warning "Status wrapper failed to build."
2054 }
2055 }
2056 set gdb_wrapper_initialized 1
2057 set gdb_wrapper_target [current_target_name]
2058 }
2059
2060 # Some targets need to always link a special object in. Save its path here.
2061 global gdb_saved_set_unbuffered_mode_obj
2062 set gdb_saved_set_unbuffered_mode_obj ""
2063
2064 proc gdb_compile {source dest type options} {
2065 global GDB_TESTCASE_OPTIONS;
2066 global gdb_wrapper_file;
2067 global gdb_wrapper_flags;
2068 global gdb_wrapper_initialized;
2069 global srcdir
2070 global objdir
2071 global gdb_saved_set_unbuffered_mode_obj
2072
2073 set outdir [file dirname $dest]
2074
2075 # Add platform-specific options if a shared library was specified using
2076 # "shlib=librarypath" in OPTIONS.
2077 set new_options ""
2078 set shlib_found 0
2079 set shlib_load 0
2080 foreach opt $options {
2081 if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
2082 if [test_compiler_info "xlc-*"] {
2083 # IBM xlc compiler doesn't accept shared library named other
2084 # than .so: use "-Wl," to bypass this
2085 lappend source "-Wl,$shlib_name"
2086 } elseif { ([istarget "*-*-mingw*"]
2087 || [istarget *-*-cygwin*]
2088 || [istarget *-*-pe*])} {
2089 lappend source "${shlib_name}.a"
2090 } else {
2091 lappend source $shlib_name
2092 }
2093 if { $shlib_found == 0 } {
2094 set shlib_found 1
2095 if { ([istarget "*-*-mingw*"]
2096 || [istarget *-*-cygwin*]) } {
2097 lappend new_options "additional_flags=-Wl,--enable-auto-import"
2098 }
2099 }
2100 } elseif { $opt == "shlib_load" } {
2101 set shlib_load 1
2102 } else {
2103 lappend new_options $opt
2104 }
2105 }
2106
2107 # We typically link to shared libraries using an absolute path, and
2108 # that's how they are found at runtime. If we are going to
2109 # dynamically load one by basename, we must specify rpath. If we
2110 # are using a remote host, DejaGNU will link to the shared library
2111 # using a relative path, so again we must specify an rpath.
2112 if { $shlib_load || ($shlib_found && [is_remote host]) } {
2113 if { ([istarget "*-*-mingw*"]
2114 || [istarget *-*-cygwin*]
2115 || [istarget *-*-pe*]
2116 || [istarget hppa*-*-hpux*])} {
2117 # Do not need anything.
2118 } elseif { [istarget *-*-openbsd*] } {
2119 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
2120 } elseif { [istarget arm*-*-symbianelf*] } {
2121 if { $shlib_load } {
2122 lappend new_options "libs=-ldl"
2123 }
2124 } else {
2125 if { $shlib_load } {
2126 lappend new_options "libs=-ldl"
2127 }
2128 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
2129 }
2130 }
2131 set options $new_options
2132
2133 if [target_info exists gdb_stub] {
2134 set options2 { "additional_flags=-Dusestubs" }
2135 lappend options "libs=[target_info gdb_stub]";
2136 set options [concat $options2 $options]
2137 }
2138 if [target_info exists is_vxworks] {
2139 set options2 { "additional_flags=-Dvxworks" }
2140 lappend options "libs=[target_info gdb_stub]";
2141 set options [concat $options2 $options]
2142 }
2143 if [info exists GDB_TESTCASE_OPTIONS] {
2144 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
2145 }
2146 verbose "options are $options"
2147 verbose "source is $source $dest $type $options"
2148
2149 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
2150
2151 if {[target_info exists needs_status_wrapper] && \
2152 [target_info needs_status_wrapper] != "0" && \
2153 [info exists gdb_wrapper_file]} {
2154 lappend options "libs=${gdb_wrapper_file}"
2155 lappend options "ldflags=${gdb_wrapper_flags}"
2156 }
2157
2158 # Replace the "nowarnings" option with the appropriate additional_flags
2159 # to disable compiler warnings.
2160 set nowarnings [lsearch -exact $options nowarnings]
2161 if {$nowarnings != -1} {
2162 if [target_info exists gdb,nowarnings_flag] {
2163 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
2164 } else {
2165 set flag "additional_flags=-w"
2166 }
2167 set options [lreplace $options $nowarnings $nowarnings $flag]
2168 }
2169
2170 if { $type == "executable" } {
2171 if { ([istarget "*-*-mingw*"]
2172 || [istarget "*-*-*djgpp"]
2173 || [istarget "*-*-cygwin*"])} {
2174 # Force output to unbuffered mode, by linking in an object file
2175 # with a global contructor that calls setvbuf.
2176 #
2177 # Compile the special object seperatelly for two reasons:
2178 # 1) Insulate it from $options.
2179 # 2) Avoid compiling it for every gdb_compile invocation,
2180 # which is time consuming, especially if we're remote
2181 # host testing.
2182 #
2183 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
2184 verbose "compiling gdb_saved_set_unbuffered_obj"
2185 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
2186 set unbuf_obj ${objdir}/set_unbuffered_mode.o
2187
2188 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
2189 if { $result != "" } {
2190 return $result
2191 }
2192
2193 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
2194 # Link a copy of the output object, because the
2195 # original may be automatically deleted.
2196 remote_exec host "cp -f $unbuf_obj $gdb_saved_set_unbuffered_mode_obj"
2197 } else {
2198 verbose "gdb_saved_set_unbuffered_obj already compiled"
2199 }
2200
2201 # Rely on the internal knowledge that the global ctors are ran in
2202 # reverse link order. In that case, we can use ldflags to
2203 # avoid copying the object file to the host multiple
2204 # times.
2205 # This object can only be added if standard libraries are
2206 # used. Thus, we need to disable it if -nostdlib option is used
2207 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
2208 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
2209 }
2210 }
2211 }
2212
2213 set result [target_compile $source $dest $type $options];
2214
2215 # Prune uninteresting compiler (and linker) output.
2216 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
2217
2218 regsub "\[\r\n\]*$" "$result" "" result;
2219 regsub "^\[\r\n\]*" "$result" "" result;
2220
2221 if {[lsearch $options quiet] < 0} {
2222 # We shall update this on a per language basis, to avoid
2223 # changing the entire testsuite in one go.
2224 if {[lsearch $options f77] >= 0} {
2225 gdb_compile_test $source $result
2226 } elseif { $result != "" } {
2227 clone_output "gdb compile failed, $result"
2228 }
2229 }
2230 return $result;
2231 }
2232
2233
2234 # This is just like gdb_compile, above, except that it tries compiling
2235 # against several different thread libraries, to see which one this
2236 # system has.
2237 proc gdb_compile_pthreads {source dest type options} {
2238 set built_binfile 0
2239 set why_msg "unrecognized error"
2240 foreach lib {-lpthreads -lpthread -lthread ""} {
2241 # This kind of wipes out whatever libs the caller may have
2242 # set. Or maybe theirs will override ours. How infelicitous.
2243 set options_with_lib [concat $options [list libs=$lib quiet]]
2244 set ccout [gdb_compile $source $dest $type $options_with_lib]
2245 switch -regexp -- $ccout {
2246 ".*no posix threads support.*" {
2247 set why_msg "missing threads include file"
2248 break
2249 }
2250 ".*cannot open -lpthread.*" {
2251 set why_msg "missing runtime threads library"
2252 }
2253 ".*Can't find library for -lpthread.*" {
2254 set why_msg "missing runtime threads library"
2255 }
2256 {^$} {
2257 pass "successfully compiled posix threads test case"
2258 set built_binfile 1
2259 break
2260 }
2261 }
2262 }
2263 if {!$built_binfile} {
2264 unsupported "Couldn't compile $source: ${why_msg}"
2265 return -1
2266 }
2267 }
2268
2269 # Build a shared library from SOURCES. You must use get_compiler_info
2270 # first.
2271
2272 proc gdb_compile_shlib {sources dest options} {
2273 set obj_options $options
2274
2275 switch -glob [test_compiler_info] {
2276 "xlc-*" {
2277 lappend obj_options "additional_flags=-qpic"
2278 }
2279 "gcc-*" {
2280 if { !([istarget "powerpc*-*-aix*"]
2281 || [istarget "rs6000*-*-aix*"]
2282 || [istarget "*-*-cygwin*"]
2283 || [istarget "*-*-mingw*"]
2284 || [istarget "*-*-pe*"]) } {
2285 lappend obj_options "additional_flags=-fpic"
2286 }
2287 }
2288 default {
2289 switch -glob [istarget] {
2290 "hppa*-hp-hpux*" {
2291 lappend obj_options "additional_flags=+z"
2292 }
2293 "mips-sgi-irix*" {
2294 # Disable SGI compiler's implicit -Dsgi
2295 lappend obj_options "additional_flags=-Usgi"
2296 }
2297 default {
2298 # don't know what the compiler is...
2299 }
2300 }
2301 }
2302 }
2303
2304 set outdir [file dirname $dest]
2305 set objects ""
2306 foreach source $sources {
2307 set sourcebase [file tail $source]
2308 if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
2309 return -1
2310 }
2311 lappend objects ${outdir}/${sourcebase}.o
2312 }
2313
2314 if [istarget "hppa*-*-hpux*"] {
2315 remote_exec build "ld -b ${objects} -o ${dest}"
2316 } else {
2317 set link_options $options
2318 if [test_compiler_info "xlc-*"] {
2319 lappend link_options "additional_flags=-qmkshrobj"
2320 } else {
2321 lappend link_options "additional_flags=-shared"
2322
2323 if { ([istarget "*-*-mingw*"]
2324 || [istarget *-*-cygwin*]
2325 || [istarget *-*-pe*])} {
2326 lappend link_options "additional_flags=-Wl,--out-implib,${dest}.a"
2327 }
2328 }
2329 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
2330 return -1
2331 }
2332 }
2333 }
2334
2335 # This is just like gdb_compile_pthreads, above, except that we always add the
2336 # objc library for compiling Objective-C programs
2337 proc gdb_compile_objc {source dest type options} {
2338 set built_binfile 0
2339 set why_msg "unrecognized error"
2340 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
2341 # This kind of wipes out whatever libs the caller may have
2342 # set. Or maybe theirs will override ours. How infelicitous.
2343 if { $lib == "solaris" } {
2344 set lib "-lpthread -lposix4"
2345 }
2346 if { $lib != "-lobjc" } {
2347 set lib "-lobjc $lib"
2348 }
2349 set options_with_lib [concat $options [list libs=$lib quiet]]
2350 set ccout [gdb_compile $source $dest $type $options_with_lib]
2351 switch -regexp -- $ccout {
2352 ".*no posix threads support.*" {
2353 set why_msg "missing threads include file"
2354 break
2355 }
2356 ".*cannot open -lpthread.*" {
2357 set why_msg "missing runtime threads library"
2358 }
2359 ".*Can't find library for -lpthread.*" {
2360 set why_msg "missing runtime threads library"
2361 }
2362 {^$} {
2363 pass "successfully compiled objc with posix threads test case"
2364 set built_binfile 1
2365 break
2366 }
2367 }
2368 }
2369 if {!$built_binfile} {
2370 unsupported "Couldn't compile $source: ${why_msg}"
2371 return -1
2372 }
2373 }
2374
2375 proc send_gdb { string } {
2376 global suppress_flag;
2377 if { $suppress_flag } {
2378 return "suppressed";
2379 }
2380 return [remote_send host "$string"];
2381 }
2382
2383 #
2384 #
2385
2386 proc gdb_expect { args } {
2387 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
2388 set atimeout [lindex $args 0];
2389 set expcode [list [lindex $args 1]];
2390 } else {
2391 set expcode $args;
2392 }
2393
2394 upvar timeout timeout;
2395
2396 if [target_info exists gdb,timeout] {
2397 if [info exists timeout] {
2398 if { $timeout < [target_info gdb,timeout] } {
2399 set gtimeout [target_info gdb,timeout];
2400 } else {
2401 set gtimeout $timeout;
2402 }
2403 } else {
2404 set gtimeout [target_info gdb,timeout];
2405 }
2406 }
2407
2408 if ![info exists gtimeout] {
2409 global timeout;
2410 if [info exists timeout] {
2411 set gtimeout $timeout;
2412 }
2413 }
2414
2415 if [info exists atimeout] {
2416 if { ![info exists gtimeout] || $gtimeout < $atimeout } {
2417 set gtimeout $atimeout;
2418 }
2419 } else {
2420 if ![info exists gtimeout] {
2421 # Eeeeew.
2422 set gtimeout 60;
2423 }
2424 }
2425
2426 global suppress_flag;
2427 global remote_suppress_flag;
2428 if [info exists remote_suppress_flag] {
2429 set old_val $remote_suppress_flag;
2430 }
2431 if [info exists suppress_flag] {
2432 if { $suppress_flag } {
2433 set remote_suppress_flag 1;
2434 }
2435 }
2436 set code [catch \
2437 {uplevel remote_expect host $gtimeout $expcode} string];
2438 if [info exists old_val] {
2439 set remote_suppress_flag $old_val;
2440 } else {
2441 if [info exists remote_suppress_flag] {
2442 unset remote_suppress_flag;
2443 }
2444 }
2445
2446 if {$code == 1} {
2447 global errorInfo errorCode;
2448
2449 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
2450 } elseif {$code == 2} {
2451 return -code return $string
2452 } elseif {$code == 3} {
2453 return
2454 } elseif {$code > 4} {
2455 return -code $code $string
2456 }
2457 }
2458
2459 # gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
2460 #
2461 # Check for long sequence of output by parts.
2462 # TEST: is the test message to be printed with the test success/fail.
2463 # SENTINEL: Is the terminal pattern indicating that output has finished.
2464 # LIST: is the sequence of outputs to match.
2465 # If the sentinel is recognized early, it is considered an error.
2466 #
2467 # Returns:
2468 # 1 if the test failed,
2469 # 0 if the test passes,
2470 # -1 if there was an internal error.
2471
2472 proc gdb_expect_list {test sentinel list} {
2473 global gdb_prompt
2474 global suppress_flag
2475 set index 0
2476 set ok 1
2477 if { $suppress_flag } {
2478 set ok 0
2479 unresolved "${test}"
2480 }
2481 while { ${index} < [llength ${list}] } {
2482 set pattern [lindex ${list} ${index}]
2483 set index [expr ${index} + 1]
2484 verbose -log "gdb_expect_list pattern: /$pattern/" 2
2485 if { ${index} == [llength ${list}] } {
2486 if { ${ok} } {
2487 gdb_expect {
2488 -re "${pattern}${sentinel}" {
2489 # pass "${test}, pattern ${index} + sentinel"
2490 }
2491 -re "${sentinel}" {
2492 fail "${test} (pattern ${index} + sentinel)"
2493 set ok 0
2494 }
2495 -re ".*A problem internal to GDB has been detected" {
2496 fail "${test} (GDB internal error)"
2497 set ok 0
2498 gdb_internal_error_resync
2499 }
2500 timeout {
2501 fail "${test} (pattern ${index} + sentinel) (timeout)"
2502 set ok 0
2503 }
2504 }
2505 } else {
2506 # unresolved "${test}, pattern ${index} + sentinel"
2507 }
2508 } else {
2509 if { ${ok} } {
2510 gdb_expect {
2511 -re "${pattern}" {
2512 # pass "${test}, pattern ${index}"
2513 }
2514 -re "${sentinel}" {
2515 fail "${test} (pattern ${index})"
2516 set ok 0
2517 }
2518 -re ".*A problem internal to GDB has been detected" {
2519 fail "${test} (GDB internal error)"
2520 set ok 0
2521 gdb_internal_error_resync
2522 }
2523 timeout {
2524 fail "${test} (pattern ${index}) (timeout)"
2525 set ok 0
2526 }
2527 }
2528 } else {
2529 # unresolved "${test}, pattern ${index}"
2530 }
2531 }
2532 }
2533 if { ${ok} } {
2534 pass "${test}"
2535 return 0
2536 } else {
2537 return 1
2538 }
2539 }
2540
2541 #
2542 #
2543 proc gdb_suppress_entire_file { reason } {
2544 global suppress_flag;
2545
2546 warning "$reason\n";
2547 set suppress_flag -1;
2548 }
2549
2550 #
2551 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
2552 # gdb_expect to fail immediately (until the next call to
2553 # gdb_stop_suppressing_tests).
2554 #
2555 proc gdb_suppress_tests { args } {
2556 global suppress_flag;
2557
2558 return; # fnf - disable pending review of results where
2559 # testsuite ran better without this
2560 incr suppress_flag;
2561
2562 if { $suppress_flag == 1 } {
2563 if { [llength $args] > 0 } {
2564 warning "[lindex $args 0]\n";
2565 } else {
2566 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
2567 }
2568 }
2569 }
2570
2571 #
2572 # Clear suppress_flag.
2573 #
2574 proc gdb_stop_suppressing_tests { } {
2575 global suppress_flag;
2576
2577 if [info exists suppress_flag] {
2578 if { $suppress_flag > 0 } {
2579 set suppress_flag 0;
2580 clone_output "Tests restarted.\n";
2581 }
2582 } else {
2583 set suppress_flag 0;
2584 }
2585 }
2586
2587 proc gdb_clear_suppressed { } {
2588 global suppress_flag;
2589
2590 set suppress_flag 0;
2591 }
2592
2593 proc gdb_start { } {
2594 default_gdb_start
2595 }
2596
2597 proc gdb_exit { } {
2598 catch default_gdb_exit
2599 }
2600
2601 #
2602 # gdb_load_cmd -- load a file into the debugger.
2603 # ARGS - additional args to load command.
2604 # return a -1 if anything goes wrong.
2605 #
2606 proc gdb_load_cmd { args } {
2607 global gdb_prompt
2608
2609 if [target_info exists gdb_load_timeout] {
2610 set loadtimeout [target_info gdb_load_timeout]
2611 } else {
2612 set loadtimeout 1600
2613 }
2614 send_gdb "load $args\n"
2615 verbose "Timeout is now $loadtimeout seconds" 2
2616 gdb_expect $loadtimeout {
2617 -re "Loading section\[^\r\]*\r\n" {
2618 exp_continue
2619 }
2620 -re "Start address\[\r\]*\r\n" {
2621 exp_continue
2622 }
2623 -re "Transfer rate\[\r\]*\r\n" {
2624 exp_continue
2625 }
2626 -re "Memory access error\[^\r\]*\r\n" {
2627 perror "Failed to load program"
2628 return -1
2629 }
2630 -re "$gdb_prompt $" {
2631 return 0
2632 }
2633 -re "(.*)\r\n$gdb_prompt " {
2634 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
2635 return -1
2636 }
2637 timeout {
2638 perror "Timed out trying to load $args."
2639 return -1
2640 }
2641 }
2642 return -1
2643 }
2644
2645 # Return the filename to download to the target and load on the target
2646 # for this shared library. Normally just LIBNAME, unless shared libraries
2647 # for this target have separate link and load images.
2648
2649 proc shlib_target_file { libname } {
2650 return $libname
2651 }
2652
2653 # Return the filename GDB will load symbols from when debugging this
2654 # shared library. Normally just LIBNAME, unless shared libraries for
2655 # this target have separate link and load images.
2656
2657 proc shlib_symbol_file { libname } {
2658 return $libname
2659 }
2660
2661 # gdb_download
2662 #
2663 # Copy a file to the remote target and return its target filename.
2664 # Schedule the file to be deleted at the end of this test.
2665
2666 proc gdb_download { filename } {
2667 global cleanfiles
2668
2669 set destname [remote_download target $filename]
2670 lappend cleanfiles $destname
2671 return $destname
2672 }
2673
2674 # gdb_load_shlibs LIB...
2675 #
2676 # Copy the listed libraries to the target.
2677
2678 proc gdb_load_shlibs { args } {
2679 if {![is_remote target]} {
2680 return
2681 }
2682
2683 foreach file $args {
2684 gdb_download [shlib_target_file $file]
2685 }
2686
2687 # Even if the target supplies full paths for shared libraries,
2688 # they may not be paths for this system.
2689 gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "" ""
2690 }
2691
2692 #
2693 # gdb_load -- load a file into the debugger.
2694 # Many files in config/*.exp override this procedure.
2695 #
2696 proc gdb_load { arg } {
2697 return [gdb_file_cmd $arg]
2698 }
2699
2700 # gdb_reload -- load a file into the target. Called before "running",
2701 # either the first time or after already starting the program once,
2702 # for remote targets. Most files that override gdb_load should now
2703 # override this instead.
2704
2705 proc gdb_reload { } {
2706 # For the benefit of existing configurations, default to gdb_load.
2707 # Specifying no file defaults to the executable currently being
2708 # debugged.
2709 return [gdb_load ""]
2710 }
2711
2712 proc gdb_continue { function } {
2713 global decimal
2714
2715 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
2716 }
2717
2718 proc default_gdb_init { args } {
2719 global gdb_wrapper_initialized
2720 global gdb_wrapper_target
2721 global cleanfiles
2722
2723 set cleanfiles {}
2724
2725 gdb_clear_suppressed;
2726
2727 # Make sure that the wrapper is rebuilt
2728 # with the appropriate multilib option.
2729 if { $gdb_wrapper_target != [current_target_name] } {
2730 set gdb_wrapper_initialized 0
2731 }
2732
2733 # Unlike most tests, we have a small number of tests that generate
2734 # a very large amount of output. We therefore increase the expect
2735 # buffer size to be able to contain the entire test output.
2736 match_max -d 30000
2737 # Also set this value for the currently running GDB.
2738 match_max [match_max -d]
2739
2740 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
2741 if { [llength $args] > 0 } {
2742 global pf_prefix
2743
2744 set file [lindex $args 0];
2745
2746 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
2747 }
2748 global gdb_prompt;
2749 if [target_info exists gdb_prompt] {
2750 set gdb_prompt [target_info gdb_prompt];
2751 } else {
2752 set gdb_prompt "\\(gdb\\)"
2753 }
2754 }
2755
2756 # The default timeout used when testing GDB commands. We want to use
2757 # the same timeout as the default dejagnu timeout, unless the user has
2758 # already provided a specific value (probably through a site.exp file).
2759 global gdb_test_timeout
2760 if ![info exists gdb_test_timeout] {
2761 set gdb_test_timeout $timeout
2762 }
2763
2764 # A list of global variables that GDB testcases should not use.
2765 # We try to prevent their use by monitoring write accesses and raising
2766 # an error when that happens.
2767 set banned_variables { bug_id prms_id }
2768
2769 # gdb_init is called by runtest at start, but also by several
2770 # tests directly; gdb_finish is only called from within runtest after
2771 # each test source execution.
2772 # Placing several traces by repetitive calls to gdb_init leads
2773 # to problems, as only one trace is removed in gdb_finish.
2774 # To overcome this possible problem, we add a variable that records
2775 # if the banned variables are traced.
2776 set banned_variables_traced 0
2777
2778 proc gdb_init { args } {
2779 # Reset the timeout value to the default. This way, any testcase
2780 # that changes the timeout value without resetting it cannot affect
2781 # the timeout used in subsequent testcases.
2782 global gdb_test_timeout
2783 global timeout
2784 set timeout $gdb_test_timeout
2785
2786 # Block writes to all banned variables...
2787 global banned_variables
2788 global banned_variables_traced
2789 if (!$banned_variables_traced) {
2790 foreach banned_var $banned_variables {
2791 global "$banned_var"
2792 trace add variable "$banned_var" write error
2793 }
2794 set banned_variables_traced 1
2795 }
2796
2797 # We set LC_ALL and LANG to C so that we get the same messages as
2798 # expected.
2799 setenv LC_ALL C
2800 setenv LANG C
2801
2802 return [eval default_gdb_init $args];
2803 }
2804
2805 proc gdb_finish { } {
2806 global cleanfiles
2807
2808 # Exit first, so that the files are no longer in use.
2809 gdb_exit
2810
2811 if { [llength $cleanfiles] > 0 } {
2812 eval remote_file target delete $cleanfiles
2813 set cleanfiles {}
2814 }
2815
2816 # Unblock write access to the banned variables. Dejagnu typically
2817 # resets some of them between testcases.
2818 global banned_variables
2819 global banned_variables_traced
2820 if ($banned_variables_traced) {
2821 foreach banned_var $banned_variables {
2822 global "$banned_var"
2823 trace remove variable "$banned_var" write error
2824 }
2825 set banned_variables_traced 0
2826 }
2827 }
2828
2829 global debug_format
2830 set debug_format "unknown"
2831
2832 # Run the gdb command "info source" and extract the debugging format
2833 # information from the output and save it in debug_format.
2834
2835 proc get_debug_format { } {
2836 global gdb_prompt
2837 global verbose
2838 global expect_out
2839 global debug_format
2840
2841 set debug_format "unknown"
2842 send_gdb "info source\n"
2843 gdb_expect 10 {
2844 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
2845 set debug_format $expect_out(1,string)
2846 verbose "debug format is $debug_format"
2847 return 1;
2848 }
2849 -re "No current source file.\r\n$gdb_prompt $" {
2850 perror "get_debug_format used when no current source file"
2851 return 0;
2852 }
2853 -re "$gdb_prompt $" {
2854 warning "couldn't check debug format (no valid response)."
2855 return 1;
2856 }
2857 timeout {
2858 warning "couldn't check debug format (timed out)."
2859 return 1;
2860 }
2861 }
2862 }
2863
2864 # Return true if FORMAT matches the debug format the current test was
2865 # compiled with. FORMAT is a shell-style globbing pattern; it can use
2866 # `*', `[...]', and so on.
2867 #
2868 # This function depends on variables set by `get_debug_format', above.
2869
2870 proc test_debug_format {format} {
2871 global debug_format
2872
2873 return [expr [string match $format $debug_format] != 0]
2874 }
2875
2876 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
2877 # COFF, stabs, etc). If that format matches the format that the
2878 # current test was compiled with, then the next test is expected to
2879 # fail for any target. Returns 1 if the next test or set of tests is
2880 # expected to fail, 0 otherwise (or if it is unknown). Must have
2881 # previously called get_debug_format.
2882 proc setup_xfail_format { format } {
2883 set ret [test_debug_format $format];
2884
2885 if {$ret} then {
2886 setup_xfail "*-*-*"
2887 }
2888 return $ret;
2889 }
2890
2891 proc gdb_step_for_stub { } {
2892 global gdb_prompt;
2893
2894 if ![target_info exists gdb,use_breakpoint_for_stub] {
2895 if [target_info exists gdb_stub_step_command] {
2896 set command [target_info gdb_stub_step_command];
2897 } else {
2898 set command "step";
2899 }
2900 send_gdb "${command}\n";
2901 set tries 0;
2902 gdb_expect 60 {
2903 -re "(main.* at |.*in .*start).*$gdb_prompt" {
2904 return;
2905 }
2906 -re ".*$gdb_prompt" {
2907 incr tries;
2908 if { $tries == 5 } {
2909 fail "stepping out of breakpoint function";
2910 return;
2911 }
2912 send_gdb "${command}\n";
2913 exp_continue;
2914 }
2915 default {
2916 fail "stepping out of breakpoint function";
2917 return;
2918 }
2919 }
2920 }
2921 send_gdb "where\n";
2922 gdb_expect {
2923 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
2924 set file $expect_out(1,string);
2925 set linenum [expr $expect_out(2,string) + 1];
2926 set breakplace "${file}:${linenum}";
2927 }
2928 default {}
2929 }
2930 send_gdb "break ${breakplace}\n";
2931 gdb_expect 60 {
2932 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
2933 set breakpoint $expect_out(1,string);
2934 }
2935 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
2936 set breakpoint $expect_out(1,string);
2937 }
2938 default {}
2939 }
2940 send_gdb "continue\n";
2941 gdb_expect 60 {
2942 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
2943 gdb_test "delete $breakpoint" ".*" "";
2944 return;
2945 }
2946 default {}
2947 }
2948 }
2949
2950 # gdb_get_line_number TEXT [FILE]
2951 #
2952 # Search the source file FILE, and return the line number of the
2953 # first line containing TEXT. If no match is found, return -1.
2954 #
2955 # TEXT is a string literal, not a regular expression.
2956 #
2957 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
2958 # specified, and does not start with "/", then it is assumed to be in
2959 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
2960 # by changing the callers and the interface at the same time.
2961 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
2962 # gdb.base/ena-dis-br.exp.
2963 #
2964 # Use this function to keep your test scripts independent of the
2965 # exact line numbering of the source file. Don't write:
2966 #
2967 # send_gdb "break 20"
2968 #
2969 # This means that if anyone ever edits your test's source file,
2970 # your test could break. Instead, put a comment like this on the
2971 # source file line you want to break at:
2972 #
2973 # /* breakpoint spot: frotz.exp: test name */
2974 #
2975 # and then write, in your test script (which we assume is named
2976 # frotz.exp):
2977 #
2978 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
2979 #
2980 # (Yes, Tcl knows how to handle the nested quotes and brackets.
2981 # Try this:
2982 # $ tclsh
2983 # % puts "foo [lindex "bar baz" 1]"
2984 # foo baz
2985 # %
2986 # Tcl is quite clever, for a little stringy language.)
2987 #
2988 # ===
2989 #
2990 # The previous implementation of this procedure used the gdb search command.
2991 # This version is different:
2992 #
2993 # . It works with MI, and it also works when gdb is not running.
2994 #
2995 # . It operates on the build machine, not the host machine.
2996 #
2997 # . For now, this implementation fakes a current directory of
2998 # $srcdir/$subdir to be compatible with the old implementation.
2999 # This will go away eventually and some callers will need to
3000 # be changed.
3001 #
3002 # . The TEXT argument is literal text and matches literally,
3003 # not a regular expression as it was before.
3004 #
3005 # . State changes in gdb, such as changing the current file
3006 # and setting $_, no longer happen.
3007 #
3008 # After a bit of time we can forget about the differences from the
3009 # old implementation.
3010 #
3011 # --chastain 2004-08-05
3012
3013 proc gdb_get_line_number { text { file "" } } {
3014 global srcdir
3015 global subdir
3016 global srcfile
3017
3018 if { "$file" == "" } then {
3019 set file "$srcfile"
3020 }
3021 if { ! [regexp "^/" "$file"] } then {
3022 set file "$srcdir/$subdir/$file"
3023 }
3024
3025 if { [ catch { set fd [open "$file"] } message ] } then {
3026 perror "$message"
3027 return -1
3028 }
3029
3030 set found -1
3031 for { set line 1 } { 1 } { incr line } {
3032 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
3033 perror "$message"
3034 return -1
3035 }
3036 if { $nchar < 0 } then {
3037 break
3038 }
3039 if { [string first "$text" "$body"] >= 0 } then {
3040 set found $line
3041 break
3042 }
3043 }
3044
3045 if { [ catch { close "$fd" } message ] } then {
3046 perror "$message"
3047 return -1
3048 }
3049
3050 return $found
3051 }
3052
3053 # gdb_continue_to_end:
3054 # The case where the target uses stubs has to be handled specially. If a
3055 # stub is used, we set a breakpoint at exit because we cannot rely on
3056 # exit() behavior of a remote target.
3057 #
3058 # mssg is the error message that gets printed.
3059
3060 proc gdb_continue_to_end {mssg} {
3061 if [target_info exists use_gdb_stub] {
3062 if {![gdb_breakpoint "exit"]} {
3063 return 0
3064 }
3065 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
3066 "continue until exit at $mssg"
3067 } else {
3068 # Continue until we exit. Should not stop again.
3069 # Don't bother to check the output of the program, that may be
3070 # extremely tough for some remote systems.
3071 gdb_test "continue"\
3072 "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
3073 "continue until exit at $mssg"
3074 }
3075 }
3076
3077 proc rerun_to_main {} {
3078 global gdb_prompt
3079
3080 if [target_info exists use_gdb_stub] {
3081 gdb_run_cmd
3082 gdb_expect {
3083 -re ".*Breakpoint .*main .*$gdb_prompt $"\
3084 {pass "rerun to main" ; return 0}
3085 -re "$gdb_prompt $"\
3086 {fail "rerun to main" ; return 0}
3087 timeout {fail "(timeout) rerun to main" ; return 0}
3088 }
3089 } else {
3090 send_gdb "run\n"
3091 gdb_expect {
3092 -re "The program .* has been started already.*y or n. $" {
3093 send_gdb "y\n"
3094 exp_continue
3095 }
3096 -re "Starting program.*$gdb_prompt $"\
3097 {pass "rerun to main" ; return 0}
3098 -re "$gdb_prompt $"\
3099 {fail "rerun to main" ; return 0}
3100 timeout {fail "(timeout) rerun to main" ; return 0}
3101 }
3102 }
3103 }
3104
3105 # Print a message and return true if a test should be skipped
3106 # due to lack of floating point suport.
3107
3108 proc gdb_skip_float_test { msg } {
3109 if [target_info exists gdb,skip_float_tests] {
3110 verbose "Skipping test '$msg': no float tests.";
3111 return 1;
3112 }
3113 return 0;
3114 }
3115
3116 # Print a message and return true if a test should be skipped
3117 # due to lack of stdio support.
3118
3119 proc gdb_skip_stdio_test { msg } {
3120 if [target_info exists gdb,noinferiorio] {
3121 verbose "Skipping test '$msg': no inferior i/o.";
3122 return 1;
3123 }
3124 return 0;
3125 }
3126
3127 proc gdb_skip_bogus_test { msg } {
3128 return 0;
3129 }
3130
3131 # Return true if a test should be skipped due to lack of XML support
3132 # in the host GDB.
3133 # NOTE: This must be called while gdb is *not* running.
3134
3135 proc gdb_skip_xml_test { } {
3136 global gdb_prompt
3137 global srcdir
3138 global xml_missing_cached
3139
3140 if {[info exists xml_missing_cached]} {
3141 return $xml_missing_cached
3142 }
3143
3144 gdb_start
3145 set xml_missing_cached 0
3146 gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" {
3147 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
3148 set xml_missing_cached 1
3149 }
3150 -re ".*$gdb_prompt $" { }
3151 }
3152 gdb_exit
3153 return $xml_missing_cached
3154 }
3155
3156 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
3157 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
3158 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
3159 # the name of a debuginfo only file. This file will be stored in the same
3160 # subdirectory.
3161
3162 # Functions for separate debug info testing
3163
3164 # starting with an executable:
3165 # foo --> original executable
3166
3167 # at the end of the process we have:
3168 # foo.stripped --> foo w/o debug info
3169 # foo.debug --> foo's debug info
3170 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
3171
3172 # Return the build-id hex string (usually 160 bits as 40 hex characters)
3173 # converted to the form: .build-id/ab/cdef1234...89.debug
3174 # Return "" if no build-id found.
3175 proc build_id_debug_filename_get { exec } {
3176 set tmp "${exec}-tmp"
3177 set objcopy_program [transform objcopy]
3178
3179 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $exec $tmp" output]
3180 verbose "result is $result"
3181 verbose "output is $output"
3182 if {$result == 1} {
3183 return ""
3184 }
3185 set fi [open $tmp]
3186 fconfigure $fi -translation binary
3187 # Skip the NOTE header.
3188 read $fi 16
3189 set data [read $fi]
3190 close $fi
3191 file delete $tmp
3192 if ![string compare $data ""] then {
3193 return ""
3194 }
3195 # Convert it to hex.
3196 binary scan $data H* data
3197 regsub {^..} $data {\0/} data
3198 return ".build-id/${data}.debug";
3199 }
3200
3201 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
3202 # list of optional flags. The only currently supported flag is no-main,
3203 # which removes the symbol entry for main from the separate debug file.
3204 #
3205 # Function returns zero on success. Function will return non-zero failure code
3206 # on some targets not supporting separate debug info (such as i386-msdos).
3207
3208 proc gdb_gnu_strip_debug { dest args } {
3209
3210 # Use the first separate debug info file location searched by GDB so the
3211 # run cannot be broken by some stale file searched with higher precedence.
3212 set debug_file "${dest}.debug"
3213
3214 set strip_to_file_program [transform strip]
3215 set objcopy_program [transform objcopy]
3216
3217 set debug_link [file tail $debug_file]
3218 set stripped_file "${dest}.stripped"
3219
3220 # Get rid of the debug info, and store result in stripped_file
3221 # something like gdb/testsuite/gdb.base/blah.stripped.
3222 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
3223 verbose "result is $result"
3224 verbose "output is $output"
3225 if {$result == 1} {
3226 return 1
3227 }
3228
3229 # Workaround PR binutils/10802:
3230 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3231 set perm [file attributes ${dest} -permissions]
3232 file attributes ${stripped_file} -permissions $perm
3233
3234 # Get rid of everything but the debug info, and store result in debug_file
3235 # This will be in the .debug subdirectory, see above.
3236 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
3237 verbose "result is $result"
3238 verbose "output is $output"
3239 if {$result == 1} {
3240 return 1
3241 }
3242
3243 # If no-main is passed, strip the symbol for main from the separate
3244 # file. This is to simulate the behavior of elfutils's eu-strip, which
3245 # leaves the symtab in the original file only. There's no way to get
3246 # objcopy or strip to remove the symbol table without also removing the
3247 # debugging sections, so this is as close as we can get.
3248 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
3249 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
3250 verbose "result is $result"
3251 verbose "output is $output"
3252 if {$result == 1} {
3253 return 1
3254 }
3255 file delete "${debug_file}"
3256 file rename "${debug_file}-tmp" "${debug_file}"
3257 }
3258
3259 # Link the two previous output files together, adding the .gnu_debuglink
3260 # section to the stripped_file, containing a pointer to the debug_file,
3261 # save the new file in dest.
3262 # This will be the regular executable filename, in the usual location.
3263 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
3264 verbose "result is $result"
3265 verbose "output is $output"
3266 if {$result == 1} {
3267 return 1
3268 }
3269
3270 # Workaround PR binutils/10802:
3271 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
3272 set perm [file attributes ${stripped_file} -permissions]
3273 file attributes ${dest} -permissions $perm
3274
3275 return 0
3276 }
3277
3278 # Test the output of GDB_COMMAND matches the pattern obtained
3279 # by concatenating all elements of EXPECTED_LINES. This makes
3280 # it possible to split otherwise very long string into pieces.
3281 # If third argument is not empty, it's used as the name of the
3282 # test to be printed on pass/fail.
3283 proc help_test_raw { gdb_command expected_lines args } {
3284 set message $gdb_command
3285 if [llength $args]>0 then {
3286 set message [lindex $args 0]
3287 }
3288 set expected_output [join $expected_lines ""]
3289 gdb_test "${gdb_command}" "${expected_output}" $message
3290 }
3291
3292 # Test the output of "help COMMNAD_CLASS". EXPECTED_INITIAL_LINES
3293 # are regular expressions that should match the beginning of output,
3294 # before the list of commands in that class. The presence of
3295 # command list and standard epilogue will be tested automatically.
3296 proc test_class_help { command_class expected_initial_lines args } {
3297 set l_stock_body {
3298 "List of commands\:.*\[\r\n\]+"
3299 "Type \"help\" followed by command name for full documentation\.\[\r\n\]+"
3300 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+"
3301 "Command name abbreviations are allowed if unambiguous\."
3302 }
3303 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3304
3305 eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
3306 }
3307
3308 # COMMAND_LIST should have either one element -- command to test, or
3309 # two elements -- abbreviated command to test, and full command the first
3310 # element is abbreviation of.
3311 # The command must be a prefix command. EXPECTED_INITIAL_LINES
3312 # are regular expressions that should match the beginning of output,
3313 # before the list of subcommands. The presence of
3314 # subcommand list and standard epilogue will be tested automatically.
3315 proc test_prefix_command_help { command_list expected_initial_lines args } {
3316 set command [lindex $command_list 0]
3317 if {[llength $command_list]>1} {
3318 set full_command [lindex $command_list 1]
3319 } else {
3320 set full_command $command
3321 }
3322 # Use 'list' and not just {} because we want variables to
3323 # be expanded in this list.
3324 set l_stock_body [list\
3325 "List of $full_command subcommands\:.*\[\r\n\]+"\
3326 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
3327 "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
3328 "Command name abbreviations are allowed if unambiguous\."]
3329 set l_entire_body [concat $expected_initial_lines $l_stock_body]
3330 if {[llength $args]>0} {
3331 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
3332 } else {
3333 help_test_raw "help ${command}" $l_entire_body
3334 }
3335 }
3336
3337 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
3338 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
3339 # to pass to untested, if something is wrong. OPTIONS are passed
3340 # to gdb_compile directly.
3341 proc build_executable { testname executable {sources ""} {options {debug}} } {
3342
3343 global objdir
3344 global subdir
3345 global srcdir
3346 if {[llength $sources]==0} {
3347 set sources ${executable}.c
3348 }
3349
3350 set binfile ${objdir}/${subdir}/${executable}
3351
3352 set objects {}
3353 for {set i 0} "\$i<[llength $sources]" {incr i} {
3354 set s [lindex $sources $i]
3355 if { [gdb_compile "${srcdir}/${subdir}/${s}" "${binfile}${i}.o" object $options] != "" } {
3356 untested $testname
3357 return -1
3358 }
3359 lappend objects "${binfile}${i}.o"
3360 }
3361
3362 if { [gdb_compile $objects "${binfile}" executable $options] != "" } {
3363 untested $testname
3364 return -1
3365 }
3366
3367 set info_options ""
3368 if { [lsearch -exact $options "c++"] >= 0 } {
3369 set info_options "c++"
3370 }
3371 if [get_compiler_info ${binfile} ${info_options}] {
3372 return -1
3373 }
3374 return 0
3375 }
3376
3377 # Starts fresh GDB binary and loads EXECUTABLE into GDB. EXECUTABLE is
3378 # the name of binary in ${objdir}/${subdir}.
3379 proc clean_restart { executable } {
3380 global srcdir
3381 global objdir
3382 global subdir
3383 set binfile ${objdir}/${subdir}/${executable}
3384
3385 gdb_exit
3386 gdb_start
3387 gdb_reinitialize_dir $srcdir/$subdir
3388 gdb_load ${binfile}
3389
3390 if [target_info exists gdb_stub] {
3391 gdb_step_for_stub;
3392 }
3393 }
3394
3395 # Prepares for testing, by calling build_executable, and then clean_restart.
3396 # Please refer to build_executable for parameter description.
3397 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
3398
3399 if {[build_executable $testname $executable $sources $options] == -1} {
3400 return -1
3401 }
3402 clean_restart $executable
3403
3404 return 0
3405 }
3406
3407 proc get_valueof { fmt exp default } {
3408 global gdb_prompt
3409
3410 set test "get valueof \"${exp}\""
3411 set val ${default}
3412 gdb_test_multiple "print${fmt} ${exp}" "$test" {
3413 -re "\\$\[0-9\]* = (.*)\[\r\n\]*$gdb_prompt $" {
3414 set val $expect_out(1,string)
3415 pass "$test ($val)"
3416 }
3417 timeout {
3418 fail "$test (timeout)"
3419 }
3420 }
3421 return ${val}
3422 }
3423
3424 proc get_integer_valueof { exp default } {
3425 global gdb_prompt
3426
3427 set test "get integer valueof \"${exp}\""
3428 set val ${default}
3429 gdb_test_multiple "print /d ${exp}" "$test" {
3430 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
3431 set val $expect_out(1,string)
3432 pass "$test ($val)"
3433 }
3434 timeout {
3435 fail "$test (timeout)"
3436 }
3437 }
3438 return ${val}
3439 }
3440
3441 proc get_hexadecimal_valueof { exp default } {
3442 global gdb_prompt
3443 send_gdb "print /x ${exp}\n"
3444 set test "get hexadecimal valueof \"${exp}\""
3445 gdb_expect {
3446 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
3447 set val $expect_out(1,string)
3448 pass "$test"
3449 }
3450 timeout {
3451 set val ${default}
3452 fail "$test (timeout)"
3453 }
3454 }
3455 return ${val}
3456 }
3457
3458 proc get_sizeof { type default } {
3459 return [get_integer_valueof "sizeof (${type})" $default]
3460 }
3461
3462 # Log gdb command line and script if requested.
3463 if {[info exists TRANSCRIPT]} {
3464 rename send_gdb real_send_gdb
3465 rename remote_spawn real_remote_spawn
3466 rename remote_close real_remote_close
3467
3468 global gdb_transcript
3469 set gdb_transcript ""
3470
3471 global gdb_trans_count
3472 set gdb_trans_count 1
3473
3474 proc remote_spawn {args} {
3475 global gdb_transcript gdb_trans_count outdir
3476
3477 if {$gdb_transcript != ""} {
3478 close $gdb_transcript
3479 }
3480 set gdb_transcript [open [file join $outdir transcript.$gdb_trans_count] w]
3481 puts $gdb_transcript [lindex $args 1]
3482 incr gdb_trans_count
3483
3484 return [uplevel real_remote_spawn $args]
3485 }
3486
3487 proc remote_close {args} {
3488 global gdb_transcript
3489
3490 if {$gdb_transcript != ""} {
3491 close $gdb_transcript
3492 set gdb_transcript ""
3493 }
3494
3495 return [uplevel real_remote_close $args]
3496 }
3497
3498 proc send_gdb {args} {
3499 global gdb_transcript
3500
3501 if {$gdb_transcript != ""} {
3502 puts -nonewline $gdb_transcript [lindex $args 0]
3503 }
3504
3505 return [uplevel real_send_gdb $args]
3506 }
3507 }
3508
3509 proc core_find {binfile {deletefiles {}} {arg ""}} {
3510 global objdir subdir
3511
3512 set destcore "$binfile.core"
3513 file delete $destcore
3514
3515 # Create a core file named "$destcore" rather than just "core", to
3516 # avoid problems with sys admin types that like to regularly prune all
3517 # files named "core" from the system.
3518 #
3519 # Arbitrarily try setting the core size limit to "unlimited" since
3520 # this does not hurt on systems where the command does not work and
3521 # allows us to generate a core on systems where it does.
3522 #
3523 # Some systems append "core" to the name of the program; others append
3524 # the name of the program to "core"; still others (like Linux, as of
3525 # May 2003) create cores named "core.PID". In the latter case, we
3526 # could have many core files lying around, and it may be difficult to
3527 # tell which one is ours, so let's run the program in a subdirectory.
3528 set found 0
3529 set coredir "${objdir}/${subdir}/coredir.[getpid]"
3530 file mkdir $coredir
3531 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
3532 # remote_exec host "${binfile}"
3533 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
3534 if [remote_file build exists $i] {
3535 remote_exec build "mv $i $destcore"
3536 set found 1
3537 }
3538 }
3539 # Check for "core.PID".
3540 if { $found == 0 } {
3541 set names [glob -nocomplain -directory $coredir core.*]
3542 if {[llength $names] == 1} {
3543 set corefile [file join $coredir [lindex $names 0]]
3544 remote_exec build "mv $corefile $destcore"
3545 set found 1
3546 }
3547 }
3548 if { $found == 0 } {
3549 # The braindamaged HPUX shell quits after the ulimit -c above
3550 # without executing ${binfile}. So we try again without the
3551 # ulimit here if we didn't find a core file above.
3552 # Oh, I should mention that any "braindamaged" non-Unix system has
3553 # the same problem. I like the cd bit too, it's really neat'n stuff.
3554 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
3555 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
3556 if [remote_file build exists $i] {
3557 remote_exec build "mv $i $destcore"
3558 set found 1
3559 }
3560 }
3561 }
3562
3563 # Try to clean up after ourselves.
3564 foreach deletefile $deletefiles {
3565 remote_file build delete [file join $coredir $deletefile]
3566 }
3567 remote_exec build "rmdir $coredir"
3568
3569 if { $found == 0 } {
3570 warning "can't generate a core file - core tests suppressed - check ulimit -c"
3571 return ""
3572 }
3573 return $destcore
3574 }
This page took 0.109383 seconds and 4 git commands to generate.