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