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