2004-08-27 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 # This file was written by Fred Fish. (fnf@cygnus.com)
20
21 # Generic gdb subroutines that should work for any target. If these
22 # need to be modified for any target, it can be done with a variable
23 # or by passing arguments.
24
25 load_lib libgloss.exp
26
27 global GDB
28
29 if [info exists TOOL_EXECUTABLE] {
30 set GDB $TOOL_EXECUTABLE;
31 }
32 if ![info exists GDB] {
33 if ![is_remote host] {
34 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
35 } else {
36 set GDB [transform gdb];
37 }
38 }
39 verbose "using GDB = $GDB" 2
40
41 global GDBFLAGS
42 if ![info exists GDBFLAGS] {
43 set GDBFLAGS "-nx"
44 }
45 verbose "using GDBFLAGS = $GDBFLAGS" 2
46
47 # The variable gdb_prompt is a regexp which matches the gdb prompt.
48 # Set it if it is not already set.
49 global gdb_prompt
50 if ![info exists gdb_prompt] then {
51 set gdb_prompt "\[(\]gdb\[)\]"
52 }
53
54 # Needed for some tests under Cygwin.
55 global EXEEXT
56 global env
57
58 if ![info exists env(EXEEXT)] {
59 set EXEEXT ""
60 } else {
61 set EXEEXT $env(EXEEXT)
62 }
63
64 ### Only procedures should come after this point.
65
66 #
67 # gdb_version -- extract and print the version number of GDB
68 #
69 proc default_gdb_version {} {
70 global GDB
71 global GDBFLAGS
72 global gdb_prompt
73 set fileid [open "gdb_cmd" w];
74 puts $fileid "q";
75 close $fileid;
76 set cmdfile [remote_download host "gdb_cmd"];
77 set output [remote_exec host "$GDB -nw --command $cmdfile"]
78 remote_file build delete "gdb_cmd";
79 remote_file host delete "$cmdfile";
80 set tmp [lindex $output 1];
81 set version ""
82 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
83 if ![is_remote host] {
84 clone_output "[which $GDB] version $version $GDBFLAGS\n"
85 } else {
86 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
87 }
88 }
89
90 proc gdb_version { } {
91 return [default_gdb_version];
92 }
93
94 #
95 # gdb_unload -- unload a file if one is loaded
96 #
97
98 proc gdb_unload {} {
99 global verbose
100 global GDB
101 global gdb_prompt
102 send_gdb "file\n"
103 gdb_expect 60 {
104 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
105 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
106 -re "A program is being debugged already..*Kill it.*y or n. $"\
107 { send_gdb "y\n"
108 verbose "\t\tKilling previous program being debugged"
109 exp_continue
110 }
111 -re "Discard symbol table from .*y or n.*$" {
112 send_gdb "y\n"
113 exp_continue
114 }
115 -re "$gdb_prompt $" {}
116 timeout {
117 perror "couldn't unload file in $GDB (timed out)."
118 return -1
119 }
120 }
121 }
122
123 # Many of the tests depend on setting breakpoints at various places and
124 # running until that breakpoint is reached. At times, we want to start
125 # with a clean-slate with respect to breakpoints, so this utility proc
126 # lets us do this without duplicating this code everywhere.
127 #
128
129 proc delete_breakpoints {} {
130 global gdb_prompt
131
132 # we need a larger timeout value here or this thing just confuses
133 # itself. May need a better implementation if possible. - guo
134 #
135 send_gdb "delete breakpoints\n"
136 gdb_expect 100 {
137 -re "Delete all breakpoints.*y or n.*$" {
138 send_gdb "y\n";
139 exp_continue
140 }
141 -re "$gdb_prompt $" { # This happens if there were no breakpoints
142 }
143 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
144 }
145 send_gdb "info breakpoints\n"
146 gdb_expect 100 {
147 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
148 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
149 -re "Delete all breakpoints.*or n.*$" {
150 send_gdb "y\n";
151 exp_continue
152 }
153 timeout { perror "info breakpoints (timeout)" ; return }
154 }
155 }
156
157
158 #
159 # Generic run command.
160 #
161 # The second pattern below matches up to the first newline *only*.
162 # Using ``.*$'' could swallow up output that we attempt to match
163 # elsewhere.
164 #
165 proc gdb_run_cmd {args} {
166 global gdb_prompt
167
168 if [target_info exists gdb_init_command] {
169 send_gdb "[target_info gdb_init_command]\n";
170 gdb_expect 30 {
171 -re "$gdb_prompt $" { }
172 default {
173 perror "gdb_init_command for target failed";
174 return;
175 }
176 }
177 }
178
179 if [target_info exists use_gdb_stub] {
180 if [target_info exists gdb,do_reload_on_run] {
181 # Specifying no file, defaults to the executable
182 # currently being debugged.
183 set status [gdb_load ""]
184 if { [lindex $status 0] != "" } {
185 return;
186 }
187 send_gdb "continue\n";
188 gdb_expect 60 {
189 -re "Continu\[^\r\n\]*\[\r\n\]" {}
190 default {}
191 }
192 return;
193 }
194
195 if [target_info exists gdb,start_symbol] {
196 set start [target_info gdb,start_symbol];
197 } else {
198 set start "start";
199 }
200 send_gdb "jump *$start\n"
201 set start_attempt 1;
202 while { $start_attempt } {
203 # Cap (re)start attempts at three to ensure that this loop
204 # always eventually fails. Don't worry about trying to be
205 # clever and not send a command when it has failed.
206 if [expr $start_attempt > 3] {
207 perror "Jump to start() failed (retry count exceeded)";
208 return;
209 }
210 set start_attempt [expr $start_attempt + 1];
211 gdb_expect 30 {
212 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
213 set start_attempt 0;
214 }
215 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
216 perror "Can't find start symbol to run in gdb_run";
217 return;
218 }
219 -re "No symbol \"start\" in current.*$gdb_prompt $" {
220 send_gdb "jump *_start\n";
221 }
222 -re "No symbol.*context.*$gdb_prompt $" {
223 set start_attempt 0;
224 }
225 -re "Line.* Jump anyway.*y or n. $" {
226 send_gdb "y\n"
227 }
228 -re "The program is not being run.*$gdb_prompt $" {
229 set status [gdb_load ""]
230 if { [lindex $status 0] != ""] } {
231 return;
232 }
233 send_gdb "jump *$start\n";
234 }
235 timeout {
236 perror "Jump to start() failed (timeout)";
237 return
238 }
239 }
240 }
241 if [target_info exists gdb_stub] {
242 gdb_expect 60 {
243 -re "$gdb_prompt $" {
244 send_gdb "continue\n"
245 }
246 }
247 }
248 return
249 }
250
251 if [target_info exists gdb,do_reload_on_run] {
252 set status [gdb_load ""]
253 if { [lindex $status 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 # Load a file into the debugger.
960 # The return value is a list with the following information:
961 #
962 # { message word ... }
963 #
964 # MESSAGE has the following values:
965 #
966 # "" file was loaded successfully
967 # "..." file was not loaded successfully.
968 # A perror has been generated with MESSAGE.
969 #
970 # If the MESSAGE is "", then there is an optional set of words.
971 # The words may be:
972 #
973 # nodebug this file does not contain debug information
974 #
975 # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might
976 # be able to use this if they can get more information
977 # in the return value.
978
979 proc gdb_file_cmd { arg } {
980 global gdb_prompt
981 global verbose
982 global GDB
983
984 if [is_remote host] {
985 set arg [remote_download host $arg]
986 if { $arg == "" } {
987 set message "download failed"
988 perror $message
989 return { $message }
990 }
991 }
992
993 send_gdb "file $arg\n"
994 gdb_expect 120 {
995 -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" {
996 verbose "\t\tLoaded $arg into the $GDB with no debugging symbols"
997 return { "" nodebug }
998 }
999 -re "Reading symbols from.*done.*$gdb_prompt $" {
1000 verbose "\t\tLoaded $arg into the $GDB"
1001 return { "" }
1002 }
1003 -re "A program is being debugged already.*Kill it.*y or n. $" {
1004 send_gdb "y\n"
1005 verbose "\t\tKilling previous program being debugged"
1006 exp_continue
1007 }
1008 -re "Load new symbol table from \".*\".*y or n. $" {
1009 send_gdb "y\n"
1010 gdb_expect 120 {
1011 -re "Reading symbols from.*done.*$gdb_prompt $" {
1012 verbose "\t\tLoaded $arg with new symbol table into $GDB"
1013 return { "" }
1014 }
1015 timeout {
1016 set message "(timeout) Couldn't load $arg, other program already loaded."
1017 perror $message
1018 return { $message }
1019 }
1020 }
1021 }
1022 -re "No such file or directory.*$gdb_prompt $" {
1023 set message "($arg) No such file or directory"
1024 perror $message
1025 return { $message }
1026 }
1027 -re "$gdb_prompt $" {
1028 set message "couldn't load $arg into $GDB."
1029 perror $message
1030 return { $message }
1031 }
1032 timeout {
1033 set message "couldn't load $arg into $GDB (timed out)."
1034 perror $message
1035 return { $message }
1036 }
1037 eof {
1038 # This is an attempt to detect a core dump, but seems not to
1039 # work. Perhaps we need to match .* followed by eof, in which
1040 # gdb_expect does not seem to have a way to do that.
1041 set message "couldn't load $arg into $GDB (end of file)."
1042 perror $message
1043 return { $message }
1044 }
1045 }
1046 }
1047
1048 #
1049 # start gdb -- start gdb running, default procedure
1050 #
1051 # When running over NFS, particularly if running many simultaneous
1052 # tests on different hosts all using the same server, things can
1053 # get really slow. Give gdb at least 3 minutes to start up.
1054 #
1055 proc default_gdb_start { } {
1056 global verbose
1057 global GDB
1058 global GDBFLAGS
1059 global gdb_prompt
1060 global timeout
1061 global gdb_spawn_id;
1062
1063 gdb_stop_suppressing_tests;
1064
1065 verbose "Spawning $GDB -nw $GDBFLAGS"
1066
1067 if [info exists gdb_spawn_id] {
1068 return 0;
1069 }
1070
1071 if ![is_remote host] {
1072 if { [which $GDB] == 0 } then {
1073 perror "$GDB does not exist."
1074 exit 1
1075 }
1076 }
1077 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
1078 if { $res < 0 || $res == "" } {
1079 perror "Spawning $GDB failed."
1080 return 1;
1081 }
1082 gdb_expect 360 {
1083 -re "\[\r\n\]$gdb_prompt $" {
1084 verbose "GDB initialized."
1085 }
1086 -re "$gdb_prompt $" {
1087 perror "GDB never initialized."
1088 return -1
1089 }
1090 timeout {
1091 perror "(timeout) GDB never initialized after 10 seconds."
1092 remote_close host;
1093 return -1
1094 }
1095 }
1096 set gdb_spawn_id -1;
1097 # force the height to "unlimited", so no pagers get used
1098
1099 send_gdb "set height 0\n"
1100 gdb_expect 10 {
1101 -re "$gdb_prompt $" {
1102 verbose "Setting height to 0." 2
1103 }
1104 timeout {
1105 warning "Couldn't set the height to 0"
1106 }
1107 }
1108 # force the width to "unlimited", so no wraparound occurs
1109 send_gdb "set width 0\n"
1110 gdb_expect 10 {
1111 -re "$gdb_prompt $" {
1112 verbose "Setting width to 0." 2
1113 }
1114 timeout {
1115 warning "Couldn't set the width to 0."
1116 }
1117 }
1118 return 0;
1119 }
1120
1121 # Return a 1 for configurations for which we don't even want to try to
1122 # test C++.
1123
1124 proc skip_cplus_tests {} {
1125 if { [istarget "d10v-*-*"] } {
1126 return 1
1127 }
1128 if { [istarget "h8300-*-*"] } {
1129 return 1
1130 }
1131
1132 # The C++ IO streams are too large for HC11/HC12 and are thus not
1133 # available. The gdb C++ tests use them and don't compile.
1134 if { [istarget "m6811-*-*"] } {
1135 return 1
1136 }
1137 if { [istarget "m6812-*-*"] } {
1138 return 1
1139 }
1140 return 0
1141 }
1142
1143 # Return a 1 if I don't even want to try to test FORTRAN.
1144
1145 proc skip_fortran_tests {} {
1146 return 0
1147 }
1148
1149 # Skip all the tests in the file if you are not on an hppa running
1150 # hpux target.
1151
1152 proc skip_hp_tests {} {
1153 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
1154 verbose "Skip hp tests is $skip_hp"
1155 return $skip_hp
1156 }
1157
1158 set compiler_info "unknown"
1159 set gcc_compiled 0
1160 set hp_cc_compiler 0
1161 set hp_aCC_compiler 0
1162
1163 # Figure out what compiler I am using.
1164 #
1165 # BINFILE is a "compiler information" output file. This implementation
1166 # does not use BINFILE.
1167 #
1168 # ARGS can be empty or "C++". If empty, "C" is assumed.
1169 #
1170 # There are several ways to do this, with various problems.
1171 #
1172 # [ gdb_compile -E $ifile -o $binfile.ci ]
1173 # source $binfile.ci
1174 #
1175 # Single Unix Spec v3 says that "-E -o ..." together are not
1176 # specified. And in fact, the native compiler on hp-ux 11 (among
1177 # others) does not work with "-E -o ...". Most targets used to do
1178 # this, and it mostly worked, because it works with gcc.
1179 #
1180 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1181 # source $binfile.ci
1182 #
1183 # This avoids the problem with -E and -o together. This almost works
1184 # if the build machine is the same as the host machine, which is
1185 # usually true of the targets which are not gcc. But this code does
1186 # not figure which compiler to call, and it always ends up using the C
1187 # compiler. Not good for setting hp_aCC_compiler. Targets
1188 # hppa*-*-hpux* and mips*-*-irix* used to do this.
1189 #
1190 # [ gdb_compile -E $ifile > $binfile.ci ]
1191 # source $binfile.ci
1192 #
1193 # dejagnu target_compile says that it supports output redirection,
1194 # but the code is completely different from the normal path and I
1195 # don't want to sweep the mines from that path. So I didn't even try
1196 # this.
1197 #
1198 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1199 # eval $cppout
1200 #
1201 # I actually do this for all targets now. gdb_compile runs the right
1202 # compiler, and TCL captures the output, and I eval the output.
1203 #
1204 # Unfortunately, expect logs the output of the command as it goes by,
1205 # and dejagnu helpfully prints a second copy of it right afterwards.
1206 # So I turn off expect logging for a moment.
1207 #
1208 # [ gdb_compile $ifile $ciexe_file executable $args ]
1209 # [ remote_exec $ciexe_file ]
1210 # [ source $ci_file.out ]
1211 #
1212 # I could give up on -E and just do this.
1213 # I didn't get desperate enough to try this.
1214 #
1215 # -- chastain 2004-01-06
1216
1217 proc get_compiler_info {binfile args} {
1218 # For compiler.c and compiler.cc
1219 global srcdir
1220
1221 # I am going to play with the log to keep noise out.
1222 global outdir
1223 global tool
1224
1225 # These come from compiler.c or compiler.cc
1226 global compiler_info
1227
1228 # Legacy global data symbols.
1229 global gcc_compiled
1230 global hp_cc_compiler
1231 global hp_aCC_compiler
1232
1233 # Choose which file to preprocess.
1234 set ifile "${srcdir}/lib/compiler.c"
1235 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1236 set ifile "${srcdir}/lib/compiler.cc"
1237 }
1238
1239 # Run $ifile through the right preprocessor.
1240 # Toggle gdb.log to keep the compiler output out of the log.
1241 log_file
1242 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1243 log_file -a "$outdir/$tool.log"
1244
1245 # Eval the output.
1246 set unknown 0
1247 foreach cppline [ split "$cppout" "\n" ] {
1248 if { [ regexp "^#" "$cppline" ] } {
1249 # line marker
1250 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1251 # blank line
1252 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
1253 # eval this line
1254 verbose "get_compiler_info: $cppline" 2
1255 eval "$cppline"
1256 } else {
1257 # unknown line
1258 verbose -log "get_compiler_info: $cppline"
1259 set unknown 1
1260 }
1261 }
1262
1263 # Reset to unknown compiler if any diagnostics happened.
1264 if { $unknown } {
1265 set compiler_info "unknown"
1266 }
1267
1268 # Set the legacy symbols.
1269 set gcc_compiled 0
1270 set hp_cc_compiler 0
1271 set hp_aCC_compiler 0
1272 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
1273 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
1274 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
1275 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
1276 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
1277 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
1278 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
1279
1280 # Log what happened.
1281 verbose -log "get_compiler_info: $compiler_info"
1282
1283 # Most compilers will evaluate comparisons and other boolean
1284 # operations to 0 or 1.
1285 uplevel \#0 { set true 1 }
1286 uplevel \#0 { set false 0 }
1287
1288 # Use of aCC results in boolean results being displayed as
1289 # "true" or "false"
1290 if { $hp_aCC_compiler } {
1291 uplevel \#0 { set true true }
1292 uplevel \#0 { set false false }
1293 }
1294
1295 return 0;
1296 }
1297
1298 proc test_compiler_info { compiler } {
1299 global compiler_info
1300 return [string match $compiler $compiler_info]
1301 }
1302
1303 set gdb_wrapper_initialized 0
1304
1305 proc gdb_wrapper_init { args } {
1306 global gdb_wrapper_initialized;
1307 global gdb_wrapper_file;
1308 global gdb_wrapper_flags;
1309
1310 if { $gdb_wrapper_initialized == 1 } { return; }
1311
1312 if {[target_info exists needs_status_wrapper] && \
1313 [target_info needs_status_wrapper] != "0"} {
1314 set result [build_wrapper "testglue.o"];
1315 if { $result != "" } {
1316 set gdb_wrapper_file [lindex $result 0];
1317 set gdb_wrapper_flags [lindex $result 1];
1318 } else {
1319 warning "Status wrapper failed to build."
1320 }
1321 }
1322 set gdb_wrapper_initialized 1
1323 }
1324
1325 proc gdb_compile {source dest type options} {
1326 global GDB_TESTCASE_OPTIONS;
1327 global gdb_wrapper_file;
1328 global gdb_wrapper_flags;
1329 global gdb_wrapper_initialized;
1330
1331 if [target_info exists gdb_stub] {
1332 set options2 { "additional_flags=-Dusestubs" }
1333 lappend options "libs=[target_info gdb_stub]";
1334 set options [concat $options2 $options]
1335 }
1336 if [target_info exists is_vxworks] {
1337 set options2 { "additional_flags=-Dvxworks" }
1338 lappend options "libs=[target_info gdb_stub]";
1339 set options [concat $options2 $options]
1340 }
1341 if [info exists GDB_TESTCASE_OPTIONS] {
1342 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1343 }
1344 verbose "options are $options"
1345 verbose "source is $source $dest $type $options"
1346
1347 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
1348
1349 if {[target_info exists needs_status_wrapper] && \
1350 [target_info needs_status_wrapper] != "0" && \
1351 [info exists gdb_wrapper_file]} {
1352 lappend options "libs=${gdb_wrapper_file}"
1353 lappend options "ldflags=${gdb_wrapper_flags}"
1354 }
1355
1356 set result [target_compile $source $dest $type $options];
1357 regsub "\[\r\n\]*$" "$result" "" result;
1358 regsub "^\[\r\n\]*" "$result" "" result;
1359 if { $result != "" && [lsearch $options quiet] == -1} {
1360 clone_output "gdb compile failed, $result"
1361 }
1362 return $result;
1363 }
1364
1365
1366 # This is just like gdb_compile, above, except that it tries compiling
1367 # against several different thread libraries, to see which one this
1368 # system has.
1369 proc gdb_compile_pthreads {source dest type options} {
1370 set built_binfile 0
1371 set why_msg "unrecognized error"
1372 foreach lib {-lpthreads -lpthread -lthread} {
1373 # This kind of wipes out whatever libs the caller may have
1374 # set. Or maybe theirs will override ours. How infelicitous.
1375 set options_with_lib [concat $options [list libs=$lib quiet]]
1376 set ccout [gdb_compile $source $dest $type $options_with_lib]
1377 switch -regexp -- $ccout {
1378 ".*no posix threads support.*" {
1379 set why_msg "missing threads include file"
1380 break
1381 }
1382 ".*cannot open -lpthread.*" {
1383 set why_msg "missing runtime threads library"
1384 }
1385 ".*Can't find library for -lpthread.*" {
1386 set why_msg "missing runtime threads library"
1387 }
1388 {^$} {
1389 pass "successfully compiled posix threads test case"
1390 set built_binfile 1
1391 break
1392 }
1393 }
1394 }
1395 if {!$built_binfile} {
1396 unsupported "Couldn't compile $source: ${why_msg}"
1397 return -1
1398 }
1399 }
1400
1401 # This is just like gdb_compile_pthreads, above, except that we always add the
1402 # objc library for compiling Objective-C programs
1403 proc gdb_compile_objc {source dest type options} {
1404 set built_binfile 0
1405 set why_msg "unrecognized error"
1406 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
1407 # This kind of wipes out whatever libs the caller may have
1408 # set. Or maybe theirs will override ours. How infelicitous.
1409 if { $lib == "solaris" } {
1410 set lib "-lpthread -lposix4"
1411 }
1412 if { $lib != "-lobjc" } {
1413 set lib "-lobjc $lib"
1414 }
1415 set options_with_lib [concat $options [list libs=$lib quiet]]
1416 set ccout [gdb_compile $source $dest $type $options_with_lib]
1417 switch -regexp -- $ccout {
1418 ".*no posix threads support.*" {
1419 set why_msg "missing threads include file"
1420 break
1421 }
1422 ".*cannot open -lpthread.*" {
1423 set why_msg "missing runtime threads library"
1424 }
1425 ".*Can't find library for -lpthread.*" {
1426 set why_msg "missing runtime threads library"
1427 }
1428 {^$} {
1429 pass "successfully compiled objc with posix threads test case"
1430 set built_binfile 1
1431 break
1432 }
1433 }
1434 }
1435 if {!$built_binfile} {
1436 unsupported "Couldn't compile $source: ${why_msg}"
1437 return -1
1438 }
1439 }
1440
1441 proc send_gdb { string } {
1442 global suppress_flag;
1443 if { $suppress_flag } {
1444 return "suppressed";
1445 }
1446 return [remote_send host "$string"];
1447 }
1448
1449 #
1450 #
1451
1452 proc gdb_expect { args } {
1453 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
1454 set gtimeout [lindex $args 0];
1455 set expcode [list [lindex $args 1]];
1456 } else {
1457 upvar timeout timeout;
1458
1459 set expcode $args;
1460 if [target_info exists gdb,timeout] {
1461 if [info exists timeout] {
1462 if { $timeout < [target_info gdb,timeout] } {
1463 set gtimeout [target_info gdb,timeout];
1464 } else {
1465 set gtimeout $timeout;
1466 }
1467 } else {
1468 set gtimeout [target_info gdb,timeout];
1469 }
1470 }
1471
1472 if ![info exists gtimeout] {
1473 global timeout;
1474 if [info exists timeout] {
1475 set gtimeout $timeout;
1476 } else {
1477 # Eeeeew.
1478 set gtimeout 60;
1479 }
1480 }
1481 }
1482 global suppress_flag;
1483 global remote_suppress_flag;
1484 if [info exists remote_suppress_flag] {
1485 set old_val $remote_suppress_flag;
1486 }
1487 if [info exists suppress_flag] {
1488 if { $suppress_flag } {
1489 set remote_suppress_flag 1;
1490 }
1491 }
1492 set code [catch \
1493 {uplevel remote_expect host $gtimeout $expcode} string];
1494 if [info exists old_val] {
1495 set remote_suppress_flag $old_val;
1496 } else {
1497 if [info exists remote_suppress_flag] {
1498 unset remote_suppress_flag;
1499 }
1500 }
1501
1502 if {$code == 1} {
1503 global errorInfo errorCode;
1504
1505 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1506 } elseif {$code == 2} {
1507 return -code return $string
1508 } elseif {$code == 3} {
1509 return
1510 } elseif {$code > 4} {
1511 return -code $code $string
1512 }
1513 }
1514
1515 # gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs
1516 #
1517 # Check for long sequence of output by parts.
1518 # MESSAGE: is the test message to be printed with the test success/fail.
1519 # SENTINEL: Is the terminal pattern indicating that output has finished.
1520 # LIST: is the sequence of outputs to match.
1521 # If the sentinel is recognized early, it is considered an error.
1522 #
1523 # Returns:
1524 # 1 if the test failed,
1525 # 0 if the test passes,
1526 # -1 if there was an internal error.
1527 #
1528 proc gdb_expect_list {test sentinel list} {
1529 global gdb_prompt
1530 global suppress_flag
1531 set index 0
1532 set ok 1
1533 if { $suppress_flag } {
1534 set ok 0
1535 unresolved "${test}"
1536 }
1537 while { ${index} < [llength ${list}] } {
1538 set pattern [lindex ${list} ${index}]
1539 set index [expr ${index} + 1]
1540 if { ${index} == [llength ${list}] } {
1541 if { ${ok} } {
1542 gdb_expect {
1543 -re "${pattern}${sentinel}" {
1544 # pass "${test}, pattern ${index} + sentinel"
1545 }
1546 -re "${sentinel}" {
1547 fail "${test} (pattern ${index} + sentinel)"
1548 set ok 0
1549 }
1550 -re ".*A problem internal to GDB has been detected" {
1551 fail "${test} (GDB internal error)"
1552 set ok 0
1553 gdb_internal_error_resync
1554 }
1555 timeout {
1556 fail "${test} (pattern ${index} + sentinel) (timeout)"
1557 set ok 0
1558 }
1559 }
1560 } else {
1561 # unresolved "${test}, pattern ${index} + sentinel"
1562 }
1563 } else {
1564 if { ${ok} } {
1565 gdb_expect {
1566 -re "${pattern}" {
1567 # pass "${test}, pattern ${index}"
1568 }
1569 -re "${sentinel}" {
1570 fail "${test} (pattern ${index})"
1571 set ok 0
1572 }
1573 -re ".*A problem internal to GDB has been detected" {
1574 fail "${test} (GDB internal error)"
1575 set ok 0
1576 gdb_internal_error_resync
1577 }
1578 timeout {
1579 fail "${test} (pattern ${index}) (timeout)"
1580 set ok 0
1581 }
1582 }
1583 } else {
1584 # unresolved "${test}, pattern ${index}"
1585 }
1586 }
1587 }
1588 if { ${ok} } {
1589 pass "${test}"
1590 return 0
1591 } else {
1592 return 1
1593 }
1594 }
1595
1596 #
1597 #
1598 proc gdb_suppress_entire_file { reason } {
1599 global suppress_flag;
1600
1601 warning "$reason\n";
1602 set suppress_flag -1;
1603 }
1604
1605 #
1606 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
1607 # gdb_expect to fail immediately (until the next call to
1608 # gdb_stop_suppressing_tests).
1609 #
1610 proc gdb_suppress_tests { args } {
1611 global suppress_flag;
1612
1613 return; # fnf - disable pending review of results where
1614 # testsuite ran better without this
1615 incr suppress_flag;
1616
1617 if { $suppress_flag == 1 } {
1618 if { [llength $args] > 0 } {
1619 warning "[lindex $args 0]\n";
1620 } else {
1621 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1622 }
1623 }
1624 }
1625
1626 #
1627 # Clear suppress_flag.
1628 #
1629 proc gdb_stop_suppressing_tests { } {
1630 global suppress_flag;
1631
1632 if [info exists suppress_flag] {
1633 if { $suppress_flag > 0 } {
1634 set suppress_flag 0;
1635 clone_output "Tests restarted.\n";
1636 }
1637 } else {
1638 set suppress_flag 0;
1639 }
1640 }
1641
1642 proc gdb_clear_suppressed { } {
1643 global suppress_flag;
1644
1645 set suppress_flag 0;
1646 }
1647
1648 proc gdb_start { } {
1649 default_gdb_start
1650 }
1651
1652 proc gdb_exit { } {
1653 catch default_gdb_exit
1654 }
1655
1656 #
1657 # gdb_load -- load a file into the debugger.
1658 #
1659 proc gdb_load { arg } {
1660 return [gdb_file_cmd $arg]
1661 }
1662
1663 proc gdb_continue { function } {
1664 global decimal
1665
1666 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
1667 }
1668
1669 proc default_gdb_init { args } {
1670 global gdb_wrapper_initialized
1671
1672 gdb_clear_suppressed;
1673
1674 # Make sure that the wrapper is rebuilt
1675 # with the appropriate multilib option.
1676 set gdb_wrapper_initialized 0
1677
1678 # Uh, this is lame. Really, really, really lame. But there's this *one*
1679 # testcase that will fail in random places if we don't increase this.
1680 match_max -d 20000
1681
1682 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
1683 if { [llength $args] > 0 } {
1684 global pf_prefix
1685
1686 set file [lindex $args 0];
1687
1688 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
1689 }
1690 global gdb_prompt;
1691 if [target_info exists gdb_prompt] {
1692 set gdb_prompt [target_info gdb_prompt];
1693 } else {
1694 set gdb_prompt "\\(gdb\\)"
1695 }
1696 }
1697
1698 proc gdb_init { args } {
1699 return [eval default_gdb_init $args];
1700 }
1701
1702 proc gdb_finish { } {
1703 gdb_exit;
1704 }
1705
1706 global debug_format
1707 set debug_format "unknown"
1708
1709 # Run the gdb command "info source" and extract the debugging format
1710 # information from the output and save it in debug_format.
1711
1712 proc get_debug_format { } {
1713 global gdb_prompt
1714 global verbose
1715 global expect_out
1716 global debug_format
1717
1718 set debug_format "unknown"
1719 send_gdb "info source\n"
1720 gdb_expect 10 {
1721 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
1722 set debug_format $expect_out(1,string)
1723 verbose "debug format is $debug_format"
1724 return 1;
1725 }
1726 -re "No current source file.\r\n$gdb_prompt $" {
1727 perror "get_debug_format used when no current source file"
1728 return 0;
1729 }
1730 -re "$gdb_prompt $" {
1731 warning "couldn't check debug format (no valid response)."
1732 return 1;
1733 }
1734 timeout {
1735 warning "couldn't check debug format (timed out)."
1736 return 1;
1737 }
1738 }
1739 }
1740
1741 # Return true if FORMAT matches the debug format the current test was
1742 # compiled with. FORMAT is a shell-style globbing pattern; it can use
1743 # `*', `[...]', and so on.
1744 #
1745 # This function depends on variables set by `get_debug_format', above.
1746
1747 proc test_debug_format {format} {
1748 global debug_format
1749
1750 return [expr [string match $format $debug_format] != 0]
1751 }
1752
1753 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
1754 # COFF, stabs, etc). If that format matches the format that the
1755 # current test was compiled with, then the next test is expected to
1756 # fail for any target. Returns 1 if the next test or set of tests is
1757 # expected to fail, 0 otherwise (or if it is unknown). Must have
1758 # previously called get_debug_format.
1759 proc setup_xfail_format { format } {
1760 set ret [test_debug_format $format];
1761
1762 if {$ret} then {
1763 setup_xfail "*-*-*"
1764 }
1765 return $ret;
1766 }
1767
1768 proc gdb_step_for_stub { } {
1769 global gdb_prompt;
1770
1771 if ![target_info exists gdb,use_breakpoint_for_stub] {
1772 if [target_info exists gdb_stub_step_command] {
1773 set command [target_info gdb_stub_step_command];
1774 } else {
1775 set command "step";
1776 }
1777 send_gdb "${command}\n";
1778 set tries 0;
1779 gdb_expect 60 {
1780 -re "(main.* at |.*in .*start).*$gdb_prompt" {
1781 return;
1782 }
1783 -re ".*$gdb_prompt" {
1784 incr tries;
1785 if { $tries == 5 } {
1786 fail "stepping out of breakpoint function";
1787 return;
1788 }
1789 send_gdb "${command}\n";
1790 exp_continue;
1791 }
1792 default {
1793 fail "stepping out of breakpoint function";
1794 return;
1795 }
1796 }
1797 }
1798 send_gdb "where\n";
1799 gdb_expect {
1800 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
1801 set file $expect_out(1,string);
1802 set linenum [expr $expect_out(2,string) + 1];
1803 set breakplace "${file}:${linenum}";
1804 }
1805 default {}
1806 }
1807 send_gdb "break ${breakplace}\n";
1808 gdb_expect 60 {
1809 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
1810 set breakpoint $expect_out(1,string);
1811 }
1812 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
1813 set breakpoint $expect_out(1,string);
1814 }
1815 default {}
1816 }
1817 send_gdb "continue\n";
1818 gdb_expect 60 {
1819 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
1820 gdb_test "delete $breakpoint" ".*" "";
1821 return;
1822 }
1823 default {}
1824 }
1825 }
1826
1827 # gdb_get_line_number TEXT [FILE]
1828 #
1829 # Search the source file FILE, and return the line number of the
1830 # first line containing TEXT. If no match is found, return -1.
1831 #
1832 # TEXT is a string literal, not a regular expression.
1833 #
1834 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
1835 # specified, and does not start with "/", then it is assumed to be in
1836 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
1837 # by changing the callers and the interface at the same time.
1838 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
1839 # gdb.base/ena-dis-br.exp.
1840 #
1841 # Use this function to keep your test scripts independent of the
1842 # exact line numbering of the source file. Don't write:
1843 #
1844 # send_gdb "break 20"
1845 #
1846 # This means that if anyone ever edits your test's source file,
1847 # your test could break. Instead, put a comment like this on the
1848 # source file line you want to break at:
1849 #
1850 # /* breakpoint spot: frotz.exp: test name */
1851 #
1852 # and then write, in your test script (which we assume is named
1853 # frotz.exp):
1854 #
1855 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
1856 #
1857 # (Yes, Tcl knows how to handle the nested quotes and brackets.
1858 # Try this:
1859 # $ tclsh
1860 # % puts "foo [lindex "bar baz" 1]"
1861 # foo baz
1862 # %
1863 # Tcl is quite clever, for a little stringy language.)
1864 #
1865 # ===
1866 #
1867 # The previous implementation of this procedure used the gdb search command.
1868 # This version is different:
1869 #
1870 # . It works with MI, and it also works when gdb is not running.
1871 #
1872 # . It operates on the build machine, not the host machine.
1873 #
1874 # . For now, this implementation fakes a current directory of
1875 # $srcdir/$subdir to be compatible with the old implementation.
1876 # This will go away eventually and some callers will need to
1877 # be changed.
1878 #
1879 # . The TEXT argument is literal text and matches literally,
1880 # not a regular expression as it was before.
1881 #
1882 # . State changes in gdb, such as changing the current file
1883 # and setting $_, no longer happen.
1884 #
1885 # After a bit of time we can forget about the differences from the
1886 # old implementation.
1887 #
1888 # --chastain 2004-08-05
1889
1890 proc gdb_get_line_number { text { file "" } } {
1891 global srcdir
1892 global subdir
1893 global srcfile
1894
1895 if { "$file" == "" } then {
1896 set file "$srcfile"
1897 }
1898 if { ! [regexp "^/" "$file"] } then {
1899 set file "$srcdir/$subdir/$file"
1900 }
1901
1902 if { [ catch { set fd [open "$file"] } message ] } then {
1903 perror "$message"
1904 return -1
1905 }
1906
1907 set found -1
1908 for { set line 1 } { 1 } { incr line } {
1909 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
1910 perror "$message"
1911 return -1
1912 }
1913 if { $nchar < 0 } then {
1914 break
1915 }
1916 if { [string first "$text" "$body"] >= 0 } then {
1917 set found $line
1918 break
1919 }
1920 }
1921
1922 if { [ catch { close "$fd" } message ] } then {
1923 perror "$message"
1924 return -1
1925 }
1926
1927 return $found
1928 }
1929
1930 # gdb_continue_to_end:
1931 # The case where the target uses stubs has to be handled specially. If a
1932 # stub is used, we set a breakpoint at exit because we cannot rely on
1933 # exit() behavior of a remote target.
1934 #
1935 # mssg is the error message that gets printed.
1936
1937 proc gdb_continue_to_end {mssg} {
1938 if [target_info exists use_gdb_stub] {
1939 if {![gdb_breakpoint "exit"]} {
1940 return 0
1941 }
1942 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
1943 "continue until exit at $mssg"
1944 } else {
1945 # Continue until we exit. Should not stop again.
1946 # Don't bother to check the output of the program, that may be
1947 # extremely tough for some remote systems.
1948 gdb_test "continue"\
1949 "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
1950 "continue until exit at $mssg"
1951 }
1952 }
1953
1954 proc rerun_to_main {} {
1955 global gdb_prompt
1956
1957 if [target_info exists use_gdb_stub] {
1958 gdb_run_cmd
1959 gdb_expect {
1960 -re ".*Breakpoint .*main .*$gdb_prompt $"\
1961 {pass "rerun to main" ; return 0}
1962 -re "$gdb_prompt $"\
1963 {fail "rerun to main" ; return 0}
1964 timeout {fail "(timeout) rerun to main" ; return 0}
1965 }
1966 } else {
1967 send_gdb "run\n"
1968 gdb_expect {
1969 -re "The program .* has been started already.*y or n. $" {
1970 send_gdb "y\n"
1971 exp_continue
1972 }
1973 -re "Starting program.*$gdb_prompt $"\
1974 {pass "rerun to main" ; return 0}
1975 -re "$gdb_prompt $"\
1976 {fail "rerun to main" ; return 0}
1977 timeout {fail "(timeout) rerun to main" ; return 0}
1978 }
1979 }
1980 }
1981
1982 # Print a message and return true if a test should be skipped
1983 # due to lack of floating point suport.
1984
1985 proc gdb_skip_float_test { msg } {
1986 if [target_info exists gdb,skip_float_tests] {
1987 verbose "Skipping test '$msg': no float tests.";
1988 return 1;
1989 }
1990 return 0;
1991 }
1992
1993 # Print a message and return true if a test should be skipped
1994 # due to lack of stdio support.
1995
1996 proc gdb_skip_stdio_test { msg } {
1997 if [target_info exists gdb,noinferiorio] {
1998 verbose "Skipping test '$msg': no inferior i/o.";
1999 return 1;
2000 }
2001 return 0;
2002 }
2003
2004 proc gdb_skip_bogus_test { msg } {
2005 return 0;
2006 }
2007
2008
2009 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
2010 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
2011 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
2012 # the name of a idebuginfo only file. This file will be stored in the
2013 # gdb.base/.debug subdirectory.
2014
2015 # Functions for separate debug info testing
2016
2017 # starting with an executable:
2018 # foo --> original executable
2019
2020 # at the end of the process we have:
2021 # foo.stripped --> foo w/o debug info
2022 # .debug/foo.debug --> foo's debug info
2023 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
2024
2025 # Return the name of the file in which we should stor EXEC's separated
2026 # debug info. EXEC contains the full path.
2027 proc separate_debug_filename { exec } {
2028
2029 # In a .debug subdirectory off the same directory where the testcase
2030 # executable is going to be. Something like:
2031 # <your-path>/gdb/testsuite/gdb.base/.debug/blah.debug.
2032 # This is the default location where gdb expects to findi
2033 # the debug info file.
2034
2035 set exec_dir [file dirname $exec]
2036 set exec_file [file tail $exec]
2037 set debug_dir [file join $exec_dir ".debug"]
2038 set debug_file [file join $debug_dir "${exec_file}.debug"]
2039
2040 return $debug_file
2041 }
2042
2043
2044 proc gdb_gnu_strip_debug { dest } {
2045
2046 set debug_file [separate_debug_filename $dest]
2047 set strip_to_file_program strip
2048 set objcopy_program objcopy
2049
2050 # Make sure the directory that will hold the separated debug
2051 # info actually exists.
2052 set debug_dir [file dirname $debug_file]
2053 if {! [file isdirectory $debug_dir]} {
2054 file mkdir $debug_dir
2055 }
2056
2057 set debug_link [file tail $debug_file]
2058 set stripped_file "${dest}.stripped"
2059
2060 # Get rid of the debug info, and store result in stripped_file
2061 # something like gdb/testsuite/gdb.base/blah.stripped.
2062 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
2063 verbose "result is $result"
2064 verbose "output is $output"
2065 if {$result == 1} {
2066 return 1
2067 }
2068
2069 # Get rid of everything but the debug info, and store result in debug_file
2070 # This will be in the .debug subdirectory, see above.
2071 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
2072 verbose "result is $result"
2073 verbose "output is $output"
2074 if {$result == 1} {
2075 return 1
2076 }
2077
2078 # Link the two previous output files together, adding the .gnu_debuglink
2079 # section to the stripped_file, containing a pointer to the debug_file,
2080 # save the new file in dest.
2081 # This will be the regular executable filename, in the usual location.
2082 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
2083 verbose "result is $result"
2084 verbose "output is $output"
2085 if {$result == 1} {
2086 return 1
2087 }
2088
2089 return 0
2090 }
2091
This page took 0.082167 seconds and 5 git commands to generate.