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