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