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