2004-08-17 Michael Chastain <mec.gnu@mindspring.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
2 # 2002, 2003, 2004
3 # Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 # Please email any bugs, comments, and/or additions to this file to:
20 # bug-gdb@prep.ai.mit.edu
21
22 # This file was written by Fred Fish. (fnf@cygnus.com)
23
24 # Generic gdb subroutines that should work for any target. If these
25 # need to be modified for any target, it can be done with a variable
26 # or by passing arguments.
27
28 load_lib libgloss.exp
29
30 global GDB
31
32 if [info exists TOOL_EXECUTABLE] {
33 set GDB $TOOL_EXECUTABLE;
34 }
35 if ![info exists GDB] {
36 if ![is_remote host] {
37 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
38 } else {
39 set GDB [transform gdb];
40 }
41 }
42 verbose "using GDB = $GDB" 2
43
44 global GDBFLAGS
45 if ![info exists GDBFLAGS] {
46 set GDBFLAGS "-nx"
47 }
48 verbose "using GDBFLAGS = $GDBFLAGS" 2
49
50 # The variable gdb_prompt is a regexp which matches the gdb prompt.
51 # Set it if it is not already set.
52 global gdb_prompt
53 if ![info exists gdb_prompt] then {
54 set gdb_prompt "\[(\]gdb\[)\]"
55 }
56
57 # Needed for some tests under Cygwin.
58 global EXEEXT
59 global env
60
61 if ![info exists env(EXEEXT)] {
62 set EXEEXT ""
63 } else {
64 set EXEEXT $env(EXEEXT)
65 }
66
67 ### Only procedures should come after this point.
68
69 #
70 # gdb_version -- extract and print the version number of GDB
71 #
72 proc default_gdb_version {} {
73 global GDB
74 global GDBFLAGS
75 global gdb_prompt
76 set fileid [open "gdb_cmd" w];
77 puts $fileid "q";
78 close $fileid;
79 set cmdfile [remote_download host "gdb_cmd"];
80 set output [remote_exec host "$GDB -nw --command $cmdfile"]
81 remote_file build delete "gdb_cmd";
82 remote_file host delete "$cmdfile";
83 set tmp [lindex $output 1];
84 set version ""
85 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
86 if ![is_remote host] {
87 clone_output "[which $GDB] version $version $GDBFLAGS\n"
88 } else {
89 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
90 }
91 }
92
93 proc gdb_version { } {
94 return [default_gdb_version];
95 }
96
97 #
98 # gdb_unload -- unload a file if one is loaded
99 #
100
101 proc gdb_unload {} {
102 global verbose
103 global GDB
104 global gdb_prompt
105 send_gdb "file\n"
106 gdb_expect 60 {
107 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
108 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
109 -re "A program is being debugged already..*Kill it.*y or n. $"\
110 { send_gdb "y\n"
111 verbose "\t\tKilling previous program being debugged"
112 exp_continue
113 }
114 -re "Discard symbol table from .*y or n.*$" {
115 send_gdb "y\n"
116 exp_continue
117 }
118 -re "$gdb_prompt $" {}
119 timeout {
120 perror "couldn't unload file in $GDB (timed out)."
121 return -1
122 }
123 }
124 }
125
126 # Many of the tests depend on setting breakpoints at various places and
127 # running until that breakpoint is reached. At times, we want to start
128 # with a clean-slate with respect to breakpoints, so this utility proc
129 # lets us do this without duplicating this code everywhere.
130 #
131
132 proc delete_breakpoints {} {
133 global gdb_prompt
134
135 # we need a larger timeout value here or this thing just confuses
136 # itself. May need a better implementation if possible. - guo
137 #
138 send_gdb "delete breakpoints\n"
139 gdb_expect 100 {
140 -re "Delete all breakpoints.*y or n.*$" {
141 send_gdb "y\n";
142 exp_continue
143 }
144 -re "$gdb_prompt $" { # This happens if there were no breakpoints
145 }
146 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
147 }
148 send_gdb "info breakpoints\n"
149 gdb_expect 100 {
150 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
151 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
152 -re "Delete all breakpoints.*or n.*$" {
153 send_gdb "y\n";
154 exp_continue
155 }
156 timeout { perror "info breakpoints (timeout)" ; return }
157 }
158 }
159
160
161 #
162 # Generic run command.
163 #
164 # The second pattern below matches up to the first newline *only*.
165 # Using ``.*$'' could swallow up output that we attempt to match
166 # elsewhere.
167 #
168 proc gdb_run_cmd {args} {
169 global gdb_prompt
170
171 if [target_info exists gdb_init_command] {
172 send_gdb "[target_info gdb_init_command]\n";
173 gdb_expect 30 {
174 -re "$gdb_prompt $" { }
175 default {
176 perror "gdb_init_command for target failed";
177 return;
178 }
179 }
180 }
181
182 if [target_info exists use_gdb_stub] {
183 if [target_info exists gdb,do_reload_on_run] {
184 # Specifying no file, defaults to the executable
185 # currently being debugged.
186 if { [gdb_load ""] < 0 } {
187 return;
188 }
189 send_gdb "continue\n";
190 gdb_expect 60 {
191 -re "Continu\[^\r\n\]*\[\r\n\]" {}
192 default {}
193 }
194 return;
195 }
196
197 if [target_info exists gdb,start_symbol] {
198 set start [target_info gdb,start_symbol];
199 } else {
200 set start "start";
201 }
202 send_gdb "jump *$start\n"
203 set start_attempt 1;
204 while { $start_attempt } {
205 # Cap (re)start attempts at three to ensure that this loop
206 # always eventually fails. Don't worry about trying to be
207 # clever and not send a command when it has failed.
208 if [expr $start_attempt > 3] {
209 perror "Jump to start() failed (retry count exceeded)";
210 return;
211 }
212 set start_attempt [expr $start_attempt + 1];
213 gdb_expect 30 {
214 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
215 set start_attempt 0;
216 }
217 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
218 perror "Can't find start symbol to run in gdb_run";
219 return;
220 }
221 -re "No symbol \"start\" in current.*$gdb_prompt $" {
222 send_gdb "jump *_start\n";
223 }
224 -re "No symbol.*context.*$gdb_prompt $" {
225 set start_attempt 0;
226 }
227 -re "Line.* Jump anyway.*y or n. $" {
228 send_gdb "y\n"
229 }
230 -re "The program is not being run.*$gdb_prompt $" {
231 if { [gdb_load ""] < 0 } {
232 return;
233 }
234 send_gdb "jump *$start\n";
235 }
236 timeout {
237 perror "Jump to start() failed (timeout)";
238 return
239 }
240 }
241 }
242 if [target_info exists gdb_stub] {
243 gdb_expect 60 {
244 -re "$gdb_prompt $" {
245 send_gdb "continue\n"
246 }
247 }
248 }
249 return
250 }
251
252 if [target_info exists gdb,do_reload_on_run] {
253 if { [gdb_load ""] < 0 } {
254 return;
255 }
256 }
257 send_gdb "run $args\n"
258 # This doesn't work quite right yet.
259 gdb_expect 60 {
260 -re "The program .* has been started already.*y or n. $" {
261 send_gdb "y\n"
262 exp_continue
263 }
264 -re "Starting program: \[^\r\n\]*" {}
265 }
266 }
267
268 # Set a breakpoint at FUNCTION. If there is an additional argument it is
269 # a list of options; the only currently supported option is allow-pending.
270
271 proc gdb_breakpoint { function args } {
272 global gdb_prompt
273 global decimal
274
275 set pending_response n
276 if {[lsearch -exact [lindex $args 0] allow-pending] != -1} {
277 set pending_response y
278 }
279
280 send_gdb "break $function\n"
281 # The first two regexps are what we get with -g, the third is without -g.
282 gdb_expect 30 {
283 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
284 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
285 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
286 -re "Breakpoint \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
287 if {$pending_response == "n"} {
288 fail "setting breakpoint at $function"
289 return 0
290 }
291 }
292 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
293 send_gdb "$pending_response\n"
294 exp_continue
295 }
296 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
297 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
298 }
299 return 1;
300 }
301
302 # Set breakpoint at function and run gdb until it breaks there.
303 # Since this is the only breakpoint that will be set, if it stops
304 # at a breakpoint, we will assume it is the one we want. We can't
305 # just compare to "function" because it might be a fully qualified,
306 # single quoted C++ function specifier. If there's an additional argument,
307 # pass it to gdb_breakpoint.
308
309 proc runto { function args } {
310 global gdb_prompt
311 global decimal
312
313 delete_breakpoints
314
315 if ![gdb_breakpoint $function [lindex $args 0]] {
316 return 0;
317 }
318
319 gdb_run_cmd
320
321 # the "at foo.c:36" output we get with -g.
322 # the "in func" output we get without -g.
323 gdb_expect 30 {
324 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
325 return 1
326 }
327 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
328 return 1
329 }
330 -re "$gdb_prompt $" {
331 fail "running to $function in runto"
332 return 0
333 }
334 timeout {
335 fail "running to $function in runto (timeout)"
336 return 0
337 }
338 }
339 return 1
340 }
341
342 #
343 # runto_main -- ask gdb to run until we hit a breakpoint at main.
344 # The case where the target uses stubs has to be handled
345 # specially--if it uses stubs, assuming we hit
346 # breakpoint() and just step out of the function.
347 #
348 proc runto_main { } {
349 global gdb_prompt
350 global decimal
351
352 if ![target_info exists gdb_stub] {
353 return [runto main]
354 }
355
356 delete_breakpoints
357
358 gdb_step_for_stub;
359
360 return 1
361 }
362
363
364 ### Continue, and expect to hit a breakpoint.
365 ### Report a pass or fail, depending on whether it seems to have
366 ### worked. Use NAME as part of the test name; each call to
367 ### continue_to_breakpoint should use a NAME which is unique within
368 ### that test file.
369 proc gdb_continue_to_breakpoint {name} {
370 global gdb_prompt
371 set full_name "continue to breakpoint: $name"
372
373 send_gdb "continue\n"
374 gdb_expect {
375 -re "Breakpoint .* at .*\r\n$gdb_prompt $" {
376 pass $full_name
377 }
378 -re ".*$gdb_prompt $" {
379 fail $full_name
380 }
381 timeout {
382 fail "$full_name (timeout)"
383 }
384 }
385 }
386
387
388 # gdb_internal_error_resync:
389 #
390 # Answer the questions GDB asks after it reports an internal error
391 # until we get back to a GDB prompt. Decline to quit the debugging
392 # session, and decline to create a core file. Return non-zero if the
393 # resync succeeds.
394 #
395 # This procedure just answers whatever questions come up until it sees
396 # a GDB prompt; it doesn't require you to have matched the input up to
397 # any specific point. However, it only answers questions it sees in
398 # the output itself, so if you've matched a question, you had better
399 # answer it yourself before calling this.
400 #
401 # You can use this function thus:
402 #
403 # gdb_expect {
404 # ...
405 # -re ".*A problem internal to GDB has been detected" {
406 # gdb_internal_error_resync
407 # }
408 # ...
409 # }
410 #
411 proc gdb_internal_error_resync {} {
412 global gdb_prompt
413
414 set count 0
415 while {$count < 10} {
416 gdb_expect {
417 -re "Quit this debugging session\\? \\(y or n\\) $" {
418 send_gdb "n\n"
419 incr count
420 }
421 -re "Create a core file of GDB\\? \\(y or n\\) $" {
422 send_gdb "n\n"
423 incr count
424 }
425 -re "$gdb_prompt $" {
426 # We're resynchronized.
427 return 1
428 }
429 timeout {
430 perror "Could not resync from internal error (timeout)"
431 return 0
432 }
433 }
434 }
435 perror "Could not resync from internal error (resync count exceeded)"
436 return 0
437 }
438
439
440 # gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
441 # Send a command to gdb; test the result.
442 #
443 # COMMAND is the command to execute, send to GDB with send_gdb. If
444 # this is the null string no command is sent.
445 # MESSAGE is a message to be printed with the built-in failure patterns
446 # if one of them matches. If MESSAGE is empty COMMAND will be used.
447 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
448 # patterns. Pattern elements will be evaluated in the caller's
449 # context; action elements will be executed in the caller's context.
450 # Unlike patterns for gdb_test, these patterns should generally include
451 # the final newline and prompt.
452 #
453 # Returns:
454 # 1 if the test failed, according to a built-in failure pattern
455 # 0 if only user-supplied patterns matched
456 # -1 if there was an internal error.
457 #
458 # You can use this function thus:
459 #
460 # gdb_test_multiple "print foo" "test foo" {
461 # -re "expected output 1" {
462 # pass "print foo"
463 # }
464 # -re "expected output 2" {
465 # fail "print foo"
466 # }
467 # }
468 #
469 # The standard patterns, such as "Program exited..." and "A problem
470 # ...", all being implicitly appended to that list.
471 #
472 proc gdb_test_multiple { command message user_code } {
473 global verbose
474 global gdb_prompt
475 global GDB
476 upvar timeout timeout
477 upvar expect_out expect_out
478
479 if { $message == "" } {
480 set message $command
481 }
482
483 # TCL/EXPECT WART ALERT
484 # Expect does something very strange when it receives a single braced
485 # argument. It splits it along word separators and performs substitutions.
486 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
487 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
488 # double-quoted list item, "\[ab\]" is just a long way of representing
489 # "[ab]", because the backslashes will be removed by lindex.
490
491 # Unfortunately, there appears to be no easy way to duplicate the splitting
492 # that expect will do from within TCL. And many places make use of the
493 # "\[0-9\]" construct, so we need to support that; and some places make use
494 # of the "[func]" construct, so we need to support that too. In order to
495 # get this right we have to substitute quoted list elements differently
496 # from braced list elements.
497
498 # We do this roughly the same way that Expect does it. We have to use two
499 # lists, because if we leave unquoted newlines in the argument to uplevel
500 # they'll be treated as command separators, and if we escape newlines
501 # we mangle newlines inside of command blocks. This assumes that the
502 # input doesn't contain a pattern which contains actual embedded newlines
503 # at this point!
504
505 regsub -all {\n} ${user_code} { } subst_code
506 set subst_code [uplevel list $subst_code]
507
508 set processed_code ""
509 set patterns ""
510 set expecting_action 0
511 foreach item $user_code subst_item $subst_code {
512 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
513 lappend processed_code $item
514 continue
515 }
516 if {$item == "-indices" || $item == "-re" || $item == "-ex"} {
517 lappend processed_code $item
518 continue
519 }
520 if { $expecting_action } {
521 lappend processed_code "uplevel [list $item]"
522 set expecting_action 0
523 # Cosmetic, no effect on the list.
524 append processed_code "\n"
525 continue
526 }
527 set expecting_action 1
528 lappend processed_code $subst_item
529 if {$patterns != ""} {
530 append patterns "; "
531 }
532 append patterns "\"$subst_item\""
533 }
534
535 # Also purely cosmetic.
536 regsub -all {\r} $patterns {\\r} patterns
537 regsub -all {\n} $patterns {\\n} patterns
538
539 if $verbose>2 then {
540 send_user "Sending \"$command\" to gdb\n"
541 send_user "Looking to match \"$patterns\"\n"
542 send_user "Message is \"$message\"\n"
543 }
544
545 set result -1
546 set string "${command}\n";
547 if { $command != "" } {
548 while { "$string" != "" } {
549 set foo [string first "\n" "$string"];
550 set len [string length "$string"];
551 if { $foo < [expr $len - 1] } {
552 set str [string range "$string" 0 $foo];
553 if { [send_gdb "$str"] != "" } {
554 global suppress_flag;
555
556 if { ! $suppress_flag } {
557 perror "Couldn't send $command to GDB.";
558 }
559 fail "$message";
560 return $result;
561 }
562 # since we're checking if each line of the multi-line
563 # command are 'accepted' by GDB here,
564 # we need to set -notransfer expect option so that
565 # command output is not lost for pattern matching
566 # - guo
567 gdb_expect 2 {
568 -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
569 timeout { verbose "partial: timeout" 3 }
570 }
571 set string [string range "$string" [expr $foo + 1] end];
572 } else {
573 break;
574 }
575 }
576 if { "$string" != "" } {
577 if { [send_gdb "$string"] != "" } {
578 global suppress_flag;
579
580 if { ! $suppress_flag } {
581 perror "Couldn't send $command to GDB.";
582 }
583 fail "$message";
584 return $result;
585 }
586 }
587 }
588
589 if [target_info exists gdb,timeout] {
590 set tmt [target_info gdb,timeout];
591 } else {
592 if [info exists timeout] {
593 set tmt $timeout;
594 } else {
595 global timeout;
596 if [info exists timeout] {
597 set tmt $timeout;
598 } else {
599 set tmt 60;
600 }
601 }
602 }
603
604 set code {
605 -re ".*A problem internal to GDB has been detected" {
606 fail "$message (GDB internal error)"
607 gdb_internal_error_resync
608 }
609 -re "\\*\\*\\* DOSEXIT code.*" {
610 if { $message != "" } {
611 fail "$message";
612 }
613 gdb_suppress_entire_file "GDB died";
614 set result -1;
615 }
616 -re "Ending remote debugging.*$gdb_prompt $" {
617 if ![isnative] then {
618 warning "Can`t communicate to remote target."
619 }
620 gdb_exit
621 gdb_start
622 set result -1
623 }
624 }
625 append code $processed_code
626 append code {
627 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
628 perror "Undefined command \"$command\"."
629 fail "$message"
630 set result 1
631 }
632 -re "Ambiguous command.*$gdb_prompt $" {
633 perror "\"$command\" is not a unique command name."
634 fail "$message"
635 set result 1
636 }
637 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
638 if ![string match "" $message] then {
639 set errmsg "$message (the program exited)"
640 } else {
641 set errmsg "$command (the program exited)"
642 }
643 fail "$errmsg"
644 set result -1
645 }
646 -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" {
647 if ![string match "" $message] then {
648 set errmsg "$message (the program exited)"
649 } else {
650 set errmsg "$command (the program exited)"
651 }
652 fail "$errmsg"
653 set result -1
654 }
655 -re "The program is not being run.*$gdb_prompt $" {
656 if ![string match "" $message] then {
657 set errmsg "$message (the program is no longer running)"
658 } else {
659 set errmsg "$command (the program is no longer running)"
660 }
661 fail "$errmsg"
662 set result -1
663 }
664 -re "\r\n$gdb_prompt $" {
665 if ![string match "" $message] then {
666 fail "$message"
667 }
668 set result 1
669 }
670 "<return>" {
671 send_gdb "\n"
672 perror "Window too small."
673 fail "$message"
674 set result -1
675 }
676 -re "\\(y or n\\) " {
677 send_gdb "n\n"
678 perror "Got interactive prompt."
679 fail "$message"
680 set result -1
681 }
682 eof {
683 perror "Process no longer exists"
684 if { $message != "" } {
685 fail "$message"
686 }
687 return -1
688 }
689 full_buffer {
690 perror "internal buffer is full."
691 fail "$message"
692 set result -1
693 }
694 timeout {
695 if ![string match "" $message] then {
696 fail "$message (timeout)"
697 }
698 set result 1
699 }
700 }
701
702 set result 0
703 set code [catch {gdb_expect $tmt $code} string]
704 if {$code == 1} {
705 global errorInfo errorCode;
706 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
707 } elseif {$code == 2} {
708 return -code return $string
709 } elseif {$code == 3} {
710 return
711 } elseif {$code > 4} {
712 return -code $code $string
713 }
714 return $result
715 }
716
717 # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
718 # Send a command to gdb; test the result.
719 #
720 # COMMAND is the command to execute, send to GDB with send_gdb. If
721 # this is the null string no command is sent.
722 # PATTERN is the pattern to match for a PASS, and must NOT include
723 # the \r\n sequence immediately before the gdb prompt.
724 # MESSAGE is an optional message to be printed. If this is
725 # omitted, then the pass/fail messages use the command string as the
726 # message. (If this is the empty string, then sometimes we don't
727 # call pass or fail at all; I don't understand this at all.)
728 # QUESTION is a question GDB may ask in response to COMMAND, like
729 # "are you sure?"
730 # RESPONSE is the response to send if QUESTION appears.
731 #
732 # Returns:
733 # 1 if the test failed,
734 # 0 if the test passes,
735 # -1 if there was an internal error.
736 #
737 proc gdb_test { args } {
738 global verbose
739 global gdb_prompt
740 global GDB
741 upvar timeout timeout
742
743 if [llength $args]>2 then {
744 set message [lindex $args 2]
745 } else {
746 set message [lindex $args 0]
747 }
748 set command [lindex $args 0]
749 set pattern [lindex $args 1]
750
751 if [llength $args]==5 {
752 set question_string [lindex $args 3];
753 set response_string [lindex $args 4];
754 } else {
755 set question_string "^FOOBAR$"
756 }
757
758 return [gdb_test_multiple $command $message {
759 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
760 if ![string match "" $message] then {
761 pass "$message"
762 }
763 }
764 -re "(${question_string})$" {
765 send_gdb "$response_string\n";
766 exp_continue;
767 }
768 }]
769 }
770 \f
771 # Test that a command gives an error. For pass or fail, return
772 # a 1 to indicate that more tests can proceed. However a timeout
773 # is a serious error, generates a special fail message, and causes
774 # a 0 to be returned to indicate that more tests are likely to fail
775 # as well.
776
777 proc test_print_reject { args } {
778 global gdb_prompt
779 global verbose
780
781 if [llength $args]==2 then {
782 set expectthis [lindex $args 1]
783 } else {
784 set expectthis "should never match this bogus string"
785 }
786 set sendthis [lindex $args 0]
787 if $verbose>2 then {
788 send_user "Sending \"$sendthis\" to gdb\n"
789 send_user "Looking to match \"$expectthis\"\n"
790 }
791 send_gdb "$sendthis\n"
792 #FIXME: Should add timeout as parameter.
793 gdb_expect {
794 -re "A .* in expression.*\\.*$gdb_prompt $" {
795 pass "reject $sendthis"
796 return 1
797 }
798 -re "Invalid syntax in expression.*$gdb_prompt $" {
799 pass "reject $sendthis"
800 return 1
801 }
802 -re "Junk after end of expression.*$gdb_prompt $" {
803 pass "reject $sendthis"
804 return 1
805 }
806 -re "Invalid number.*$gdb_prompt $" {
807 pass "reject $sendthis"
808 return 1
809 }
810 -re "Invalid character constant.*$gdb_prompt $" {
811 pass "reject $sendthis"
812 return 1
813 }
814 -re "No symbol table is loaded.*$gdb_prompt $" {
815 pass "reject $sendthis"
816 return 1
817 }
818 -re "No symbol .* in current context.*$gdb_prompt $" {
819 pass "reject $sendthis"
820 return 1
821 }
822 -re "Unmatched single quote.*$gdb_prompt $" {
823 pass "reject $sendthis"
824 return 1
825 }
826 -re "A character constant must contain at least one character.*$gdb_prompt $" {
827 pass "reject $sendthis"
828 return 1
829 }
830 -re "$expectthis.*$gdb_prompt $" {
831 pass "reject $sendthis"
832 return 1
833 }
834 -re ".*$gdb_prompt $" {
835 fail "reject $sendthis"
836 return 1
837 }
838 default {
839 fail "reject $sendthis (eof or timeout)"
840 return 0
841 }
842 }
843 }
844 \f
845 # Given an input string, adds backslashes as needed to create a
846 # regexp that will match the string.
847
848 proc string_to_regexp {str} {
849 set result $str
850 regsub -all {[]*+.|()^$\[]} $str {\\&} result
851 return $result
852 }
853
854 # Same as gdb_test, but the second parameter is not a regexp,
855 # but a string that must match exactly.
856
857 proc gdb_test_exact { args } {
858 upvar timeout timeout
859
860 set command [lindex $args 0]
861
862 # This applies a special meaning to a null string pattern. Without
863 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
864 # messages from commands that should have no output except a new
865 # prompt. With this, only results of a null string will match a null
866 # string pattern.
867
868 set pattern [lindex $args 1]
869 if [string match $pattern ""] {
870 set pattern [string_to_regexp [lindex $args 0]]
871 } else {
872 set pattern [string_to_regexp [lindex $args 1]]
873 }
874
875 # It is most natural to write the pattern argument with only
876 # embedded \n's, especially if you are trying to avoid Tcl quoting
877 # problems. But gdb_expect really wants to see \r\n in patterns. So
878 # transform the pattern here. First transform \r\n back to \n, in
879 # case some users of gdb_test_exact already do the right thing.
880 regsub -all "\r\n" $pattern "\n" pattern
881 regsub -all "\n" $pattern "\r\n" pattern
882 if [llength $args]==3 then {
883 set message [lindex $args 2]
884 } else {
885 set message $command
886 }
887
888 return [gdb_test $command $pattern $message]
889 }
890 \f
891 proc gdb_reinitialize_dir { subdir } {
892 global gdb_prompt
893
894 if [is_remote host] {
895 return "";
896 }
897 send_gdb "dir\n"
898 gdb_expect 60 {
899 -re "Reinitialize source path to empty.*y or n. " {
900 send_gdb "y\n"
901 gdb_expect 60 {
902 -re "Source directories searched.*$gdb_prompt $" {
903 send_gdb "dir $subdir\n"
904 gdb_expect 60 {
905 -re "Source directories searched.*$gdb_prompt $" {
906 verbose "Dir set to $subdir"
907 }
908 -re "$gdb_prompt $" {
909 perror "Dir \"$subdir\" failed."
910 }
911 }
912 }
913 -re "$gdb_prompt $" {
914 perror "Dir \"$subdir\" failed."
915 }
916 }
917 }
918 -re "$gdb_prompt $" {
919 perror "Dir \"$subdir\" failed."
920 }
921 }
922 }
923
924 #
925 # gdb_exit -- exit the GDB, killing the target program if necessary
926 #
927 proc default_gdb_exit {} {
928 global GDB
929 global GDBFLAGS
930 global verbose
931 global gdb_spawn_id;
932
933 gdb_stop_suppressing_tests;
934
935 if ![info exists gdb_spawn_id] {
936 return;
937 }
938
939 verbose "Quitting $GDB $GDBFLAGS"
940
941 if { [is_remote host] && [board_info host exists fileid] } {
942 send_gdb "quit\n";
943 gdb_expect 10 {
944 -re "y or n" {
945 send_gdb "y\n";
946 exp_continue;
947 }
948 -re "DOSEXIT code" { }
949 default { }
950 }
951 }
952
953 if ![is_remote host] {
954 remote_close host;
955 }
956 unset gdb_spawn_id
957 }
958
959 #
960 # load a file into the debugger.
961 # return a -1 if anything goes wrong.
962 #
963 proc gdb_file_cmd { arg } {
964 global verbose
965 global loadpath
966 global loadfile
967 global GDB
968 global gdb_prompt
969 upvar timeout timeout
970
971 if [is_remote host] {
972 set arg [remote_download host $arg];
973 if { $arg == "" } {
974 error "download failed"
975 return -1;
976 }
977 }
978
979 send_gdb "file $arg\n"
980 gdb_expect 120 {
981 -re "Reading symbols from.*done.*$gdb_prompt $" {
982 verbose "\t\tLoaded $arg into the $GDB"
983 return 0
984 }
985 -re "has no symbol-table.*$gdb_prompt $" {
986 perror "$arg wasn't compiled with \"-g\""
987 return -1
988 }
989 -re "A program is being debugged already.*Kill it.*y or n. $" {
990 send_gdb "y\n"
991 verbose "\t\tKilling previous program being debugged"
992 exp_continue
993 }
994 -re "Load new symbol table from \".*\".*y or n. $" {
995 send_gdb "y\n"
996 gdb_expect 120 {
997 -re "Reading symbols from.*done.*$gdb_prompt $" {
998 verbose "\t\tLoaded $arg with new symbol table into $GDB"
999 return 0
1000 }
1001 timeout {
1002 perror "(timeout) Couldn't load $arg, other program already loaded."
1003 return -1
1004 }
1005 }
1006 }
1007 -re "No such file or directory.*$gdb_prompt $" {
1008 perror "($arg) No such file or directory\n"
1009 return -1
1010 }
1011 -re "$gdb_prompt $" {
1012 perror "couldn't load $arg into $GDB."
1013 return -1
1014 }
1015 timeout {
1016 perror "couldn't load $arg into $GDB (timed out)."
1017 return -1
1018 }
1019 eof {
1020 # This is an attempt to detect a core dump, but seems not to
1021 # work. Perhaps we need to match .* followed by eof, in which
1022 # gdb_expect does not seem to have a way to do that.
1023 perror "couldn't load $arg into $GDB (end of file)."
1024 return -1
1025 }
1026 }
1027 }
1028
1029 #
1030 # start gdb -- start gdb running, default procedure
1031 #
1032 # When running over NFS, particularly if running many simultaneous
1033 # tests on different hosts all using the same server, things can
1034 # get really slow. Give gdb at least 3 minutes to start up.
1035 #
1036 proc default_gdb_start { } {
1037 global verbose
1038 global GDB
1039 global GDBFLAGS
1040 global gdb_prompt
1041 global timeout
1042 global gdb_spawn_id;
1043
1044 gdb_stop_suppressing_tests;
1045
1046 verbose "Spawning $GDB -nw $GDBFLAGS"
1047
1048 if [info exists gdb_spawn_id] {
1049 return 0;
1050 }
1051
1052 if ![is_remote host] {
1053 if { [which $GDB] == 0 } then {
1054 perror "$GDB does not exist."
1055 exit 1
1056 }
1057 }
1058 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
1059 if { $res < 0 || $res == "" } {
1060 perror "Spawning $GDB failed."
1061 return 1;
1062 }
1063 gdb_expect 360 {
1064 -re "\[\r\n\]$gdb_prompt $" {
1065 verbose "GDB initialized."
1066 }
1067 -re "$gdb_prompt $" {
1068 perror "GDB never initialized."
1069 return -1
1070 }
1071 timeout {
1072 perror "(timeout) GDB never initialized after 10 seconds."
1073 remote_close host;
1074 return -1
1075 }
1076 }
1077 set gdb_spawn_id -1;
1078 # force the height to "unlimited", so no pagers get used
1079
1080 send_gdb "set height 0\n"
1081 gdb_expect 10 {
1082 -re "$gdb_prompt $" {
1083 verbose "Setting height to 0." 2
1084 }
1085 timeout {
1086 warning "Couldn't set the height to 0"
1087 }
1088 }
1089 # force the width to "unlimited", so no wraparound occurs
1090 send_gdb "set width 0\n"
1091 gdb_expect 10 {
1092 -re "$gdb_prompt $" {
1093 verbose "Setting width to 0." 2
1094 }
1095 timeout {
1096 warning "Couldn't set the width to 0."
1097 }
1098 }
1099 return 0;
1100 }
1101
1102 # Return a 1 for configurations for which we don't even want to try to
1103 # test C++.
1104
1105 proc skip_cplus_tests {} {
1106 if { [istarget "d10v-*-*"] } {
1107 return 1
1108 }
1109 if { [istarget "h8300-*-*"] } {
1110 return 1
1111 }
1112
1113 # The C++ IO streams are too large for HC11/HC12 and are thus not
1114 # available. The gdb C++ tests use them and don't compile.
1115 if { [istarget "m6811-*-*"] } {
1116 return 1
1117 }
1118 if { [istarget "m6812-*-*"] } {
1119 return 1
1120 }
1121 return 0
1122 }
1123
1124 # Return a 1 if I don't even want to try to test FORTRAN.
1125
1126 proc skip_fortran_tests {} {
1127 return 0
1128 }
1129
1130 # Skip all the tests in the file if you are not on an hppa running
1131 # hpux target.
1132
1133 proc skip_hp_tests {} {
1134 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
1135 verbose "Skip hp tests is $skip_hp"
1136 return $skip_hp
1137 }
1138
1139 set compiler_info "unknown"
1140 set gcc_compiled 0
1141 set hp_cc_compiler 0
1142 set hp_aCC_compiler 0
1143 set signed_keyword_not_used 0
1144
1145 # Figure out what compiler I am using.
1146 #
1147 # BINFILE is a "compiler information" output file. This implementation
1148 # does not use BINFILE.
1149 #
1150 # ARGS can be empty or "C++". If empty, "C" is assumed.
1151 #
1152 # There are several ways to do this, with various problems.
1153 #
1154 # [ gdb_compile -E $ifile -o $binfile.ci ]
1155 # source $binfile.ci
1156 #
1157 # Single Unix Spec v3 says that "-E -o ..." together are not
1158 # specified. And in fact, the native compiler on hp-ux 11 (among
1159 # others) does not work with "-E -o ...". Most targets used to do
1160 # this, and it mostly worked, because it works with gcc.
1161 #
1162 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1163 # source $binfile.ci
1164 #
1165 # This avoids the problem with -E and -o together. This almost works
1166 # if the build machine is the same as the host machine, which is
1167 # usually true of the targets which are not gcc. But this code does
1168 # not figure which compiler to call, and it always ends up using the C
1169 # compiler. Not good for setting hp_aCC_compiler. Targets
1170 # hppa*-*-hpux* and mips*-*-irix* used to do this.
1171 #
1172 # [ gdb_compile -E $ifile > $binfile.ci ]
1173 # source $binfile.ci
1174 #
1175 # dejagnu target_compile says that it supports output redirection,
1176 # but the code is completely different from the normal path and I
1177 # don't want to sweep the mines from that path. So I didn't even try
1178 # this.
1179 #
1180 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1181 # eval $cppout
1182 #
1183 # I actually do this for all targets now. gdb_compile runs the right
1184 # compiler, and TCL captures the output, and I eval the output.
1185 #
1186 # Unfortunately, expect logs the output of the command as it goes by,
1187 # and dejagnu helpfully prints a second copy of it right afterwards.
1188 # So I turn off expect logging for a moment.
1189 #
1190 # [ gdb_compile $ifile $ciexe_file executable $args ]
1191 # [ remote_exec $ciexe_file ]
1192 # [ source $ci_file.out ]
1193 #
1194 # I could give up on -E and just do this.
1195 # I didn't get desperate enough to try this.
1196 #
1197 # -- chastain 2004-01-06
1198
1199 proc get_compiler_info {binfile args} {
1200 # For compiler.c and compiler.cc
1201 global srcdir
1202
1203 # I am going to play with the log to keep noise out.
1204 global outdir
1205 global tool
1206
1207 # These come from compiler.c or compiler.cc
1208 global compiler_info
1209 global signed_keyword_not_used
1210
1211 # Legacy global data symbols.
1212 global gcc_compiled
1213 global hp_cc_compiler
1214 global hp_aCC_compiler
1215
1216 # Choose which file to preprocess.
1217 set ifile "${srcdir}/lib/compiler.c"
1218 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1219 set ifile "${srcdir}/lib/compiler.cc"
1220 }
1221
1222 # Run $ifile through the right preprocessor.
1223 # Toggle gdb.log to keep the compiler output out of the log.
1224 log_file
1225 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1226 log_file -a "$outdir/$tool.log"
1227
1228 # Eval the output.
1229 set unknown 0
1230 foreach cppline [ split "$cppout" "\n" ] {
1231 if { [ regexp "^#" "$cppline" ] } {
1232 # line marker
1233 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1234 # blank line
1235 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
1236 # eval this line
1237 verbose "get_compiler_info: $cppline" 2
1238 eval "$cppline"
1239 } else {
1240 # unknown line
1241 verbose -log "get_compiler_info: $cppline"
1242 set unknown 1
1243 }
1244 }
1245
1246 # Reset to unknown compiler if any diagnostics happened.
1247 if { $unknown } {
1248 set compiler_info "unknown"
1249 set signed_keyword_not_used 0
1250 }
1251
1252 # Set the legacy symbols.
1253 set gcc_compiled 0
1254 set hp_cc_compiler 0
1255 set hp_aCC_compiler 0
1256 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
1257 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
1258 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
1259 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
1260 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
1261 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
1262 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
1263
1264 # Log what happened.
1265 verbose -log "get_compiler_info: $compiler_info"
1266
1267 # Most compilers will evaluate comparisons and other boolean
1268 # operations to 0 or 1.
1269 uplevel \#0 { set true 1 }
1270 uplevel \#0 { set false 0 }
1271
1272 # Use of aCC results in boolean results being displayed as
1273 # "true" or "false"
1274 if { $hp_aCC_compiler } {
1275 uplevel \#0 { set true true }
1276 uplevel \#0 { set false false }
1277 }
1278
1279 return 0;
1280 }
1281
1282 proc test_compiler_info { compiler } {
1283 global compiler_info
1284 return [string match $compiler $compiler_info]
1285 }
1286
1287 set gdb_wrapper_initialized 0
1288
1289 proc gdb_wrapper_init { args } {
1290 global gdb_wrapper_initialized;
1291 global gdb_wrapper_file;
1292 global gdb_wrapper_flags;
1293
1294 if { $gdb_wrapper_initialized == 1 } { return; }
1295
1296 if {[target_info exists needs_status_wrapper] && \
1297 [target_info needs_status_wrapper] != "0"} {
1298 set result [build_wrapper "testglue.o"];
1299 if { $result != "" } {
1300 set gdb_wrapper_file [lindex $result 0];
1301 set gdb_wrapper_flags [lindex $result 1];
1302 } else {
1303 warning "Status wrapper failed to build."
1304 }
1305 }
1306 set gdb_wrapper_initialized 1
1307 }
1308
1309 proc gdb_compile {source dest type options} {
1310 global GDB_TESTCASE_OPTIONS;
1311 global gdb_wrapper_file;
1312 global gdb_wrapper_flags;
1313 global gdb_wrapper_initialized;
1314
1315 if [target_info exists gdb_stub] {
1316 set options2 { "additional_flags=-Dusestubs" }
1317 lappend options "libs=[target_info gdb_stub]";
1318 set options [concat $options2 $options]
1319 }
1320 if [target_info exists is_vxworks] {
1321 set options2 { "additional_flags=-Dvxworks" }
1322 lappend options "libs=[target_info gdb_stub]";
1323 set options [concat $options2 $options]
1324 }
1325 if [info exists GDB_TESTCASE_OPTIONS] {
1326 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1327 }
1328 verbose "options are $options"
1329 verbose "source is $source $dest $type $options"
1330
1331 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
1332
1333 if {[target_info exists needs_status_wrapper] && \
1334 [target_info needs_status_wrapper] != "0" && \
1335 [info exists gdb_wrapper_file]} {
1336 lappend options "libs=${gdb_wrapper_file}"
1337 lappend options "ldflags=${gdb_wrapper_flags}"
1338 }
1339
1340 set result [target_compile $source $dest $type $options];
1341 regsub "\[\r\n\]*$" "$result" "" result;
1342 regsub "^\[\r\n\]*" "$result" "" result;
1343 if { $result != "" && [lsearch $options quiet] == -1} {
1344 clone_output "gdb compile failed, $result"
1345 }
1346 return $result;
1347 }
1348
1349
1350 # This is just like gdb_compile, above, except that it tries compiling
1351 # against several different thread libraries, to see which one this
1352 # system has.
1353 proc gdb_compile_pthreads {source dest type options} {
1354 set built_binfile 0
1355 set why_msg "unrecognized error"
1356 foreach lib {-lpthreads -lpthread -lthread} {
1357 # This kind of wipes out whatever libs the caller may have
1358 # set. Or maybe theirs will override ours. How infelicitous.
1359 set options_with_lib [concat $options [list libs=$lib quiet]]
1360 set ccout [gdb_compile $source $dest $type $options_with_lib]
1361 switch -regexp -- $ccout {
1362 ".*no posix threads support.*" {
1363 set why_msg "missing threads include file"
1364 break
1365 }
1366 ".*cannot open -lpthread.*" {
1367 set why_msg "missing runtime threads library"
1368 }
1369 ".*Can't find library for -lpthread.*" {
1370 set why_msg "missing runtime threads library"
1371 }
1372 {^$} {
1373 pass "successfully compiled posix threads test case"
1374 set built_binfile 1
1375 break
1376 }
1377 }
1378 }
1379 if {!$built_binfile} {
1380 unsupported "Couldn't compile $source: ${why_msg}"
1381 return -1
1382 }
1383 }
1384
1385 # This is just like gdb_compile_pthreads, above, except that we always add the
1386 # objc library for compiling Objective-C programs
1387 proc gdb_compile_objc {source dest type options} {
1388 set built_binfile 0
1389 set why_msg "unrecognized error"
1390 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
1391 # This kind of wipes out whatever libs the caller may have
1392 # set. Or maybe theirs will override ours. How infelicitous.
1393 if { $lib == "solaris" } {
1394 set lib "-lpthread -lposix4"
1395 }
1396 if { $lib != "-lobjc" } {
1397 set lib "-lobjc $lib"
1398 }
1399 set options_with_lib [concat $options [list libs=$lib quiet]]
1400 set ccout [gdb_compile $source $dest $type $options_with_lib]
1401 switch -regexp -- $ccout {
1402 ".*no posix threads support.*" {
1403 set why_msg "missing threads include file"
1404 break
1405 }
1406 ".*cannot open -lpthread.*" {
1407 set why_msg "missing runtime threads library"
1408 }
1409 ".*Can't find library for -lpthread.*" {
1410 set why_msg "missing runtime threads library"
1411 }
1412 {^$} {
1413 pass "successfully compiled objc with posix threads test case"
1414 set built_binfile 1
1415 break
1416 }
1417 }
1418 }
1419 if {!$built_binfile} {
1420 unsupported "Couldn't compile $source: ${why_msg}"
1421 return -1
1422 }
1423 }
1424
1425 proc send_gdb { string } {
1426 global suppress_flag;
1427 if { $suppress_flag } {
1428 return "suppressed";
1429 }
1430 return [remote_send host "$string"];
1431 }
1432
1433 #
1434 #
1435
1436 proc gdb_expect { args } {
1437 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
1438 set gtimeout [lindex $args 0];
1439 set expcode [list [lindex $args 1]];
1440 } else {
1441 upvar timeout timeout;
1442
1443 set expcode $args;
1444 if [target_info exists gdb,timeout] {
1445 if [info exists timeout] {
1446 if { $timeout < [target_info gdb,timeout] } {
1447 set gtimeout [target_info gdb,timeout];
1448 } else {
1449 set gtimeout $timeout;
1450 }
1451 } else {
1452 set gtimeout [target_info gdb,timeout];
1453 }
1454 }
1455
1456 if ![info exists gtimeout] {
1457 global timeout;
1458 if [info exists timeout] {
1459 set gtimeout $timeout;
1460 } else {
1461 # Eeeeew.
1462 set gtimeout 60;
1463 }
1464 }
1465 }
1466 global suppress_flag;
1467 global remote_suppress_flag;
1468 if [info exists remote_suppress_flag] {
1469 set old_val $remote_suppress_flag;
1470 }
1471 if [info exists suppress_flag] {
1472 if { $suppress_flag } {
1473 set remote_suppress_flag 1;
1474 }
1475 }
1476 set code [catch \
1477 {uplevel remote_expect host $gtimeout $expcode} string];
1478 if [info exists old_val] {
1479 set remote_suppress_flag $old_val;
1480 } else {
1481 if [info exists remote_suppress_flag] {
1482 unset remote_suppress_flag;
1483 }
1484 }
1485
1486 if {$code == 1} {
1487 global errorInfo errorCode;
1488
1489 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1490 } elseif {$code == 2} {
1491 return -code return $string
1492 } elseif {$code == 3} {
1493 return
1494 } elseif {$code > 4} {
1495 return -code $code $string
1496 }
1497 }
1498
1499 # gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs
1500 #
1501 # Check for long sequence of output by parts.
1502 # MESSAGE: is the test message to be printed with the test success/fail.
1503 # SENTINEL: Is the terminal pattern indicating that output has finished.
1504 # LIST: is the sequence of outputs to match.
1505 # If the sentinel is recognized early, it is considered an error.
1506 #
1507 # Returns:
1508 # 1 if the test failed,
1509 # 0 if the test passes,
1510 # -1 if there was an internal error.
1511 #
1512 proc gdb_expect_list {test sentinel list} {
1513 global gdb_prompt
1514 global suppress_flag
1515 set index 0
1516 set ok 1
1517 if { $suppress_flag } {
1518 set ok 0
1519 unresolved "${test}"
1520 }
1521 while { ${index} < [llength ${list}] } {
1522 set pattern [lindex ${list} ${index}]
1523 set index [expr ${index} + 1]
1524 if { ${index} == [llength ${list}] } {
1525 if { ${ok} } {
1526 gdb_expect {
1527 -re "${pattern}${sentinel}" {
1528 # pass "${test}, pattern ${index} + sentinel"
1529 }
1530 -re "${sentinel}" {
1531 fail "${test} (pattern ${index} + sentinel)"
1532 set ok 0
1533 }
1534 -re ".*A problem internal to GDB has been detected" {
1535 fail "${test} (GDB internal error)"
1536 set ok 0
1537 gdb_internal_error_resync
1538 }
1539 timeout {
1540 fail "${test} (pattern ${index} + sentinel) (timeout)"
1541 set ok 0
1542 }
1543 }
1544 } else {
1545 # unresolved "${test}, pattern ${index} + sentinel"
1546 }
1547 } else {
1548 if { ${ok} } {
1549 gdb_expect {
1550 -re "${pattern}" {
1551 # pass "${test}, pattern ${index}"
1552 }
1553 -re "${sentinel}" {
1554 fail "${test} (pattern ${index})"
1555 set ok 0
1556 }
1557 -re ".*A problem internal to GDB has been detected" {
1558 fail "${test} (GDB internal error)"
1559 set ok 0
1560 gdb_internal_error_resync
1561 }
1562 timeout {
1563 fail "${test} (pattern ${index}) (timeout)"
1564 set ok 0
1565 }
1566 }
1567 } else {
1568 # unresolved "${test}, pattern ${index}"
1569 }
1570 }
1571 }
1572 if { ${ok} } {
1573 pass "${test}"
1574 return 0
1575 } else {
1576 return 1
1577 }
1578 }
1579
1580 #
1581 #
1582 proc gdb_suppress_entire_file { reason } {
1583 global suppress_flag;
1584
1585 warning "$reason\n";
1586 set suppress_flag -1;
1587 }
1588
1589 #
1590 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
1591 # gdb_expect to fail immediately (until the next call to
1592 # gdb_stop_suppressing_tests).
1593 #
1594 proc gdb_suppress_tests { args } {
1595 global suppress_flag;
1596
1597 return; # fnf - disable pending review of results where
1598 # testsuite ran better without this
1599 incr suppress_flag;
1600
1601 if { $suppress_flag == 1 } {
1602 if { [llength $args] > 0 } {
1603 warning "[lindex $args 0]\n";
1604 } else {
1605 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1606 }
1607 }
1608 }
1609
1610 #
1611 # Clear suppress_flag.
1612 #
1613 proc gdb_stop_suppressing_tests { } {
1614 global suppress_flag;
1615
1616 if [info exists suppress_flag] {
1617 if { $suppress_flag > 0 } {
1618 set suppress_flag 0;
1619 clone_output "Tests restarted.\n";
1620 }
1621 } else {
1622 set suppress_flag 0;
1623 }
1624 }
1625
1626 proc gdb_clear_suppressed { } {
1627 global suppress_flag;
1628
1629 set suppress_flag 0;
1630 }
1631
1632 proc gdb_start { } {
1633 default_gdb_start
1634 }
1635
1636 proc gdb_exit { } {
1637 catch default_gdb_exit
1638 }
1639
1640 #
1641 # gdb_load -- load a file into the debugger.
1642 # return a -1 if anything goes wrong.
1643 #
1644 proc gdb_load { arg } {
1645 return [gdb_file_cmd $arg]
1646 }
1647
1648 proc gdb_continue { function } {
1649 global decimal
1650
1651 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
1652 }
1653
1654 proc default_gdb_init { args } {
1655 global gdb_wrapper_initialized
1656
1657 gdb_clear_suppressed;
1658
1659 # Make sure that the wrapper is rebuilt
1660 # with the appropriate multilib option.
1661 set gdb_wrapper_initialized 0
1662
1663 # Uh, this is lame. Really, really, really lame. But there's this *one*
1664 # testcase that will fail in random places if we don't increase this.
1665 match_max -d 20000
1666
1667 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
1668 if { [llength $args] > 0 } {
1669 global pf_prefix
1670
1671 set file [lindex $args 0];
1672
1673 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
1674 }
1675 global gdb_prompt;
1676 if [target_info exists gdb_prompt] {
1677 set gdb_prompt [target_info gdb_prompt];
1678 } else {
1679 set gdb_prompt "\\(gdb\\)"
1680 }
1681 }
1682
1683 proc gdb_init { args } {
1684 return [eval default_gdb_init $args];
1685 }
1686
1687 proc gdb_finish { } {
1688 gdb_exit;
1689 }
1690
1691 global debug_format
1692 set debug_format "unknown"
1693
1694 # Run the gdb command "info source" and extract the debugging format
1695 # information from the output and save it in debug_format.
1696
1697 proc get_debug_format { } {
1698 global gdb_prompt
1699 global verbose
1700 global expect_out
1701 global debug_format
1702
1703 set debug_format "unknown"
1704 send_gdb "info source\n"
1705 gdb_expect 10 {
1706 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
1707 set debug_format $expect_out(1,string)
1708 verbose "debug format is $debug_format"
1709 return 1;
1710 }
1711 -re "No current source file.\r\n$gdb_prompt $" {
1712 perror "get_debug_format used when no current source file"
1713 return 0;
1714 }
1715 -re "$gdb_prompt $" {
1716 warning "couldn't check debug format (no valid response)."
1717 return 1;
1718 }
1719 timeout {
1720 warning "couldn't check debug format (timed out)."
1721 return 1;
1722 }
1723 }
1724 }
1725
1726 # Return true if FORMAT matches the debug format the current test was
1727 # compiled with. FORMAT is a shell-style globbing pattern; it can use
1728 # `*', `[...]', and so on.
1729 #
1730 # This function depends on variables set by `get_debug_format', above.
1731
1732 proc test_debug_format {format} {
1733 global debug_format
1734
1735 return [expr [string match $format $debug_format] != 0]
1736 }
1737
1738 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
1739 # COFF, stabs, etc). If that format matches the format that the
1740 # current test was compiled with, then the next test is expected to
1741 # fail for any target. Returns 1 if the next test or set of tests is
1742 # expected to fail, 0 otherwise (or if it is unknown). Must have
1743 # previously called get_debug_format.
1744 proc setup_xfail_format { format } {
1745 set ret [test_debug_format $format];
1746
1747 if {$ret} then {
1748 setup_xfail "*-*-*"
1749 }
1750 return $ret;
1751 }
1752
1753 proc gdb_step_for_stub { } {
1754 global gdb_prompt;
1755
1756 if ![target_info exists gdb,use_breakpoint_for_stub] {
1757 if [target_info exists gdb_stub_step_command] {
1758 set command [target_info gdb_stub_step_command];
1759 } else {
1760 set command "step";
1761 }
1762 send_gdb "${command}\n";
1763 set tries 0;
1764 gdb_expect 60 {
1765 -re "(main.* at |.*in .*start).*$gdb_prompt" {
1766 return;
1767 }
1768 -re ".*$gdb_prompt" {
1769 incr tries;
1770 if { $tries == 5 } {
1771 fail "stepping out of breakpoint function";
1772 return;
1773 }
1774 send_gdb "${command}\n";
1775 exp_continue;
1776 }
1777 default {
1778 fail "stepping out of breakpoint function";
1779 return;
1780 }
1781 }
1782 }
1783 send_gdb "where\n";
1784 gdb_expect {
1785 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
1786 set file $expect_out(1,string);
1787 set linenum [expr $expect_out(2,string) + 1];
1788 set breakplace "${file}:${linenum}";
1789 }
1790 default {}
1791 }
1792 send_gdb "break ${breakplace}\n";
1793 gdb_expect 60 {
1794 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
1795 set breakpoint $expect_out(1,string);
1796 }
1797 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
1798 set breakpoint $expect_out(1,string);
1799 }
1800 default {}
1801 }
1802 send_gdb "continue\n";
1803 gdb_expect 60 {
1804 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
1805 gdb_test "delete $breakpoint" ".*" "";
1806 return;
1807 }
1808 default {}
1809 }
1810 }
1811
1812 # gdb_get_line_number TEXT [FILE]
1813 #
1814 # Search the source file FILE, and return the line number of the
1815 # first line containing TEXT. If no match is found, return -1.
1816 #
1817 # TEXT is a string literal, not a regular expression.
1818 #
1819 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
1820 # specified, and does not start with "/", then it is assumed to be in
1821 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
1822 # by changing the callers and the interface at the same time.
1823 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
1824 # gdb.base/ena-dis-br.exp.
1825 #
1826 # Use this function to keep your test scripts independent of the
1827 # exact line numbering of the source file. Don't write:
1828 #
1829 # send_gdb "break 20"
1830 #
1831 # This means that if anyone ever edits your test's source file,
1832 # your test could break. Instead, put a comment like this on the
1833 # source file line you want to break at:
1834 #
1835 # /* breakpoint spot: frotz.exp: test name */
1836 #
1837 # and then write, in your test script (which we assume is named
1838 # frotz.exp):
1839 #
1840 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
1841 #
1842 # (Yes, Tcl knows how to handle the nested quotes and brackets.
1843 # Try this:
1844 # $ tclsh
1845 # % puts "foo [lindex "bar baz" 1]"
1846 # foo baz
1847 # %
1848 # Tcl is quite clever, for a little stringy language.)
1849 #
1850 # ===
1851 #
1852 # The previous implementation of this procedure used the gdb search command.
1853 # This version is different:
1854 #
1855 # . It works with MI, and it also works when gdb is not running.
1856 #
1857 # . It operates on the build machine, not the host machine.
1858 #
1859 # . For now, this implementation fakes a current directory of
1860 # $srcdir/$subdir to be compatible with the old implementation.
1861 # This will go away eventually and some callers will need to
1862 # be changed.
1863 #
1864 # . The TEXT argument is literal text and matches literally,
1865 # not a regular expression as it was before.
1866 #
1867 # . State changes in gdb, such as changing the current file
1868 # and setting $_, no longer happen.
1869 #
1870 # After a bit of time we can forget about the differences from the
1871 # old implementation.
1872 #
1873 # --chastain 2004-08-05
1874
1875 proc gdb_get_line_number { text { file "" } } {
1876 global srcdir
1877 global subdir
1878 global srcfile
1879
1880 if { "$file" == "" } then {
1881 set file "$srcfile"
1882 }
1883 if { ! [regexp "^/" "$file"] } then {
1884 set file "$srcdir/$subdir/$file"
1885 }
1886
1887 if { [ catch { set fd [open "$file"] } message ] } then {
1888 perror "$message"
1889 return -1
1890 }
1891
1892 set found -1
1893 for { set line 1 } { 1 } { incr line } {
1894 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
1895 perror "$message"
1896 return -1
1897 }
1898 if { $nchar < 0 } then {
1899 break
1900 }
1901 if { [string first "$text" "$body"] >= 0 } then {
1902 set found $line
1903 break
1904 }
1905 }
1906
1907 if { [ catch { close "$fd" } message ] } then {
1908 perror "$message"
1909 return -1
1910 }
1911
1912 return $found
1913 }
1914
1915 # gdb_continue_to_end:
1916 # The case where the target uses stubs has to be handled specially. If a
1917 # stub is used, we set a breakpoint at exit because we cannot rely on
1918 # exit() behavior of a remote target.
1919 #
1920 # mssg is the error message that gets printed.
1921
1922 proc gdb_continue_to_end {mssg} {
1923 if [target_info exists use_gdb_stub] {
1924 if {![gdb_breakpoint "exit"]} {
1925 return 0
1926 }
1927 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
1928 "continue until exit at $mssg"
1929 } else {
1930 # Continue until we exit. Should not stop again.
1931 # Don't bother to check the output of the program, that may be
1932 # extremely tough for some remote systems.
1933 gdb_test "continue"\
1934 "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
1935 "continue until exit at $mssg"
1936 }
1937 }
1938
1939 proc rerun_to_main {} {
1940 global gdb_prompt
1941
1942 if [target_info exists use_gdb_stub] {
1943 gdb_run_cmd
1944 gdb_expect {
1945 -re ".*Breakpoint .*main .*$gdb_prompt $"\
1946 {pass "rerun to main" ; return 0}
1947 -re "$gdb_prompt $"\
1948 {fail "rerun to main" ; return 0}
1949 timeout {fail "(timeout) rerun to main" ; return 0}
1950 }
1951 } else {
1952 send_gdb "run\n"
1953 gdb_expect {
1954 -re "The program .* has been started already.*y or n. $" {
1955 send_gdb "y\n"
1956 exp_continue
1957 }
1958 -re "Starting program.*$gdb_prompt $"\
1959 {pass "rerun to main" ; return 0}
1960 -re "$gdb_prompt $"\
1961 {fail "rerun to main" ; return 0}
1962 timeout {fail "(timeout) rerun to main" ; return 0}
1963 }
1964 }
1965 }
1966
1967 # Print a message and return true if a test should be skipped
1968 # due to lack of floating point suport.
1969
1970 proc gdb_skip_float_test { msg } {
1971 if [target_info exists gdb,skip_float_tests] {
1972 verbose "Skipping test '$msg': no float tests.";
1973 return 1;
1974 }
1975 return 0;
1976 }
1977
1978 # Print a message and return true if a test should be skipped
1979 # due to lack of stdio support.
1980
1981 proc gdb_skip_stdio_test { msg } {
1982 if [target_info exists gdb,noinferiorio] {
1983 verbose "Skipping test '$msg': no inferior i/o.";
1984 return 1;
1985 }
1986 return 0;
1987 }
1988
1989 proc gdb_skip_bogus_test { msg } {
1990 return 0;
1991 }
1992
1993
1994 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
1995 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
1996 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
1997 # the name of a idebuginfo only file. This file will be stored in the
1998 # gdb.base/.debug subdirectory.
1999
2000 # Functions for separate debug info testing
2001
2002 # starting with an executable:
2003 # foo --> original executable
2004
2005 # at the end of the process we have:
2006 # foo.stripped --> foo w/o debug info
2007 # .debug/foo.debug --> foo's debug info
2008 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
2009
2010 # Return the name of the file in which we should stor EXEC's separated
2011 # debug info. EXEC contains the full path.
2012 proc separate_debug_filename { exec } {
2013
2014 # In a .debug subdirectory off the same directory where the testcase
2015 # executable is going to be. Something like:
2016 # <your-path>/gdb/testsuite/gdb.base/.debug/blah.debug.
2017 # This is the default location where gdb expects to findi
2018 # the debug info file.
2019
2020 set exec_dir [file dirname $exec]
2021 set exec_file [file tail $exec]
2022 set debug_dir [file join $exec_dir ".debug"]
2023 set debug_file [file join $debug_dir "${exec_file}.debug"]
2024
2025 return $debug_file
2026 }
2027
2028
2029 proc gdb_gnu_strip_debug { dest } {
2030
2031 set debug_file [separate_debug_filename $dest]
2032 set strip_to_file_program strip
2033 set objcopy_program objcopy
2034
2035 # Make sure the directory that will hold the separated debug
2036 # info actually exists.
2037 set debug_dir [file dirname $debug_file]
2038 if {! [file isdirectory $debug_dir]} {
2039 file mkdir $debug_dir
2040 }
2041
2042 set debug_link [file tail $debug_file]
2043 set stripped_file "${dest}.stripped"
2044
2045 # Get rid of the debug info, and store result in stripped_file
2046 # something like gdb/testsuite/gdb.base/blah.stripped.
2047 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
2048 verbose "result is $result"
2049 verbose "output is $output"
2050 if {$result == 1} {
2051 return 1
2052 }
2053
2054 # Get rid of everything but the debug info, and store result in debug_file
2055 # This will be in the .debug subdirectory, see above.
2056 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
2057 verbose "result is $result"
2058 verbose "output is $output"
2059 if {$result == 1} {
2060 return 1
2061 }
2062
2063 # Link the two previous output files together, adding the .gnu_debuglink
2064 # section to the stripped_file, containing a pointer to the debug_file,
2065 # save the new file in dest.
2066 # This will be the regular executable filename, in the usual location.
2067 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
2068 verbose "result is $result"
2069 verbose "output is $output"
2070 if {$result == 1} {
2071 return 1
2072 }
2073
2074 return 0
2075 }
2076
This page took 0.074119 seconds and 4 git commands to generate.