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