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