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