* linespec.c (minsym_found): Advance to the next line if possible.
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2 # 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 # This file was written by Fred Fish. (fnf@cygnus.com)
19
20 # Generic gdb subroutines that should work for any target. If these
21 # need to be modified for any target, it can be done with a variable
22 # or by passing arguments.
23
24 if {$tool == ""} {
25 # Tests would fail, logs on get_compiler_info() would be missing.
26 send_error "`site.exp' not found, run `make site.exp'!\n"
27 exit 2
28 }
29
30 load_lib libgloss.exp
31
32 global GDB
33
34 if [info exists TOOL_EXECUTABLE] {
35 set GDB $TOOL_EXECUTABLE;
36 }
37 if ![info exists GDB] {
38 if ![is_remote host] {
39 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
40 } else {
41 set GDB [transform gdb];
42 }
43 }
44 verbose "using GDB = $GDB" 2
45
46 global GDBFLAGS
47 if ![info exists GDBFLAGS] {
48 set GDBFLAGS "-nx"
49 }
50 verbose "using GDBFLAGS = $GDBFLAGS" 2
51
52 # The variable gdb_prompt is a regexp which matches the gdb prompt.
53 # Set it if it is not already set.
54 global gdb_prompt
55 if ![info exists gdb_prompt] then {
56 set gdb_prompt "\[(\]gdb\[)\]"
57 }
58
59 # The variable fullname_syntax_POSIX is a regexp which matches a POSIX
60 # absolute path ie. /foo/
61 set fullname_syntax_POSIX "/.*/"
62 # The variable fullname_syntax_UNC is a regexp which matches a Windows
63 # UNC path ie. \\D\foo\
64 set fullname_syntax_UNC {\\\\[^\\]+\\.+\\}
65 # The variable fullname_syntax_DOS_CASE is a regexp which matches a
66 # particular DOS case that GDB most likely will output
67 # ie. \foo\, but don't match \\.*\
68 set fullname_syntax_DOS_CASE {\\[^\\].*\\}
69 # The variable fullname_syntax_DOS is a regexp which matches a DOS path
70 # ie. a:\foo\ && a:foo\
71 set fullname_syntax_DOS {[a-zA-Z]:.*\\}
72 # The variable fullname_syntax is a regexp which matches what GDB considers
73 # an absolute path. It is currently debatable if the Windows style paths
74 # d:foo and \abc should be considered valid as an absolute path.
75 # Also, the purpse of this regexp is not to recognize a well formed
76 # absolute path, but to say with certainty that a path is absolute.
77 set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
78
79 # Needed for some tests under Cygwin.
80 global EXEEXT
81 global env
82
83 if ![info exists env(EXEEXT)] {
84 set EXEEXT ""
85 } else {
86 set EXEEXT $env(EXEEXT)
87 }
88
89 ### Only procedures should come after this point.
90
91 #
92 # gdb_version -- extract and print the version number of GDB
93 #
94 proc default_gdb_version {} {
95 global GDB
96 global GDBFLAGS
97 global gdb_prompt
98 set fileid [open "gdb_cmd" w];
99 puts $fileid "q";
100 close $fileid;
101 set cmdfile [remote_download host "gdb_cmd"];
102 set output [remote_exec host "$GDB -nw --command $cmdfile"]
103 remote_file build delete "gdb_cmd";
104 remote_file host delete "$cmdfile";
105 set tmp [lindex $output 1];
106 set version ""
107 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
108 if ![is_remote host] {
109 clone_output "[which $GDB] version $version $GDBFLAGS\n"
110 } else {
111 clone_output "$GDB on remote host version $version $GDBFLAGS\n"
112 }
113 }
114
115 proc gdb_version { } {
116 return [default_gdb_version];
117 }
118
119 #
120 # gdb_unload -- unload a file if one is loaded
121 #
122
123 proc gdb_unload {} {
124 global verbose
125 global GDB
126 global gdb_prompt
127 send_gdb "file\n"
128 gdb_expect 60 {
129 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
130 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
131 -re "A program is being debugged already..*Kill it.*y or n. $"\
132 { send_gdb "y\n"
133 verbose "\t\tKilling previous program being debugged"
134 exp_continue
135 }
136 -re "Discard symbol table from .*y or n.*$" {
137 send_gdb "y\n"
138 exp_continue
139 }
140 -re "$gdb_prompt $" {}
141 timeout {
142 perror "couldn't unload file in $GDB (timed out)."
143 return -1
144 }
145 }
146 }
147
148 # Many of the tests depend on setting breakpoints at various places and
149 # running until that breakpoint is reached. At times, we want to start
150 # with a clean-slate with respect to breakpoints, so this utility proc
151 # lets us do this without duplicating this code everywhere.
152 #
153
154 proc delete_breakpoints {} {
155 global gdb_prompt
156
157 # we need a larger timeout value here or this thing just confuses
158 # itself. May need a better implementation if possible. - guo
159 #
160 send_gdb "delete breakpoints\n"
161 gdb_expect 100 {
162 -re "Delete all breakpoints.*y or n.*$" {
163 send_gdb "y\n";
164 exp_continue
165 }
166 -re "$gdb_prompt $" { # This happens if there were no breakpoints
167 }
168 timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
169 }
170 send_gdb "info breakpoints\n"
171 gdb_expect 100 {
172 -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
173 -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
174 -re "Delete all breakpoints.*or n.*$" {
175 send_gdb "y\n";
176 exp_continue
177 }
178 timeout { perror "info breakpoints (timeout)" ; return }
179 }
180 }
181
182
183 #
184 # Generic run command.
185 #
186 # The second pattern below matches up to the first newline *only*.
187 # Using ``.*$'' could swallow up output that we attempt to match
188 # elsewhere.
189 #
190 proc gdb_run_cmd {args} {
191 global gdb_prompt
192
193 if [target_info exists gdb_init_command] {
194 send_gdb "[target_info gdb_init_command]\n";
195 gdb_expect 30 {
196 -re "$gdb_prompt $" { }
197 default {
198 perror "gdb_init_command for target failed";
199 return;
200 }
201 }
202 }
203
204 if [target_info exists use_gdb_stub] {
205 if [target_info exists gdb,do_reload_on_run] {
206 if { [gdb_reload] != 0 } {
207 return;
208 }
209 send_gdb "continue\n";
210 gdb_expect 60 {
211 -re "Continu\[^\r\n\]*\[\r\n\]" {}
212 default {}
213 }
214 return;
215 }
216
217 if [target_info exists gdb,start_symbol] {
218 set start [target_info gdb,start_symbol];
219 } else {
220 set start "start";
221 }
222 send_gdb "jump *$start\n"
223 set start_attempt 1;
224 while { $start_attempt } {
225 # Cap (re)start attempts at three to ensure that this loop
226 # always eventually fails. Don't worry about trying to be
227 # clever and not send a command when it has failed.
228 if [expr $start_attempt > 3] {
229 perror "Jump to start() failed (retry count exceeded)";
230 return;
231 }
232 set start_attempt [expr $start_attempt + 1];
233 gdb_expect 30 {
234 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
235 set start_attempt 0;
236 }
237 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
238 perror "Can't find start symbol to run in gdb_run";
239 return;
240 }
241 -re "No symbol \"start\" in current.*$gdb_prompt $" {
242 send_gdb "jump *_start\n";
243 }
244 -re "No symbol.*context.*$gdb_prompt $" {
245 set start_attempt 0;
246 }
247 -re "Line.* Jump anyway.*y or n. $" {
248 send_gdb "y\n"
249 }
250 -re "The program is not being run.*$gdb_prompt $" {
251 if { [gdb_reload] != 0 } {
252 return;
253 }
254 send_gdb "jump *$start\n";
255 }
256 timeout {
257 perror "Jump to start() failed (timeout)";
258 return
259 }
260 }
261 }
262 if [target_info exists gdb_stub] {
263 gdb_expect 60 {
264 -re "$gdb_prompt $" {
265 send_gdb "continue\n"
266 }
267 }
268 }
269 return
270 }
271
272 if [target_info exists gdb,do_reload_on_run] {
273 if { [gdb_reload] != 0 } {
274 return;
275 }
276 }
277 send_gdb "run $args\n"
278 # This doesn't work quite right yet.
279 gdb_expect 60 {
280 -re "The program .* has been started already.*y or n. $" {
281 send_gdb "y\n"
282 exp_continue
283 }
284 # Use -notransfer here so that test cases (like chng-sym.exp)
285 # may test for additional start-up messages.
286 -notransfer -re "Starting program: \[^\r\n\]*" {}
287 }
288 }
289
290 # Generic start command. Return 0 if we could start the program, -1
291 # if we could not.
292
293 proc gdb_start_cmd {args} {
294 global gdb_prompt
295
296 if [target_info exists gdb_init_command] {
297 send_gdb "[target_info gdb_init_command]\n";
298 gdb_expect 30 {
299 -re "$gdb_prompt $" { }
300 default {
301 perror "gdb_init_command for target failed";
302 return;
303 }
304 }
305 }
306
307 if [target_info exists use_gdb_stub] {
308 return -1
309 }
310
311 send_gdb "start $args\n"
312 gdb_expect 60 {
313 -re "The program .* has been started already.*y or n. $" {
314 send_gdb "y\n"
315 exp_continue
316 }
317 # Use -notransfer here so that test cases (like chng-sym.exp)
318 # may test for additional start-up messages.
319 -notransfer -re "Starting program: \[^\r\n\]*" {
320 return 0
321 }
322 }
323 return -1
324 }
325
326 # Set a breakpoint at FUNCTION. If there is an additional argument it is
327 # a list of options; the supported options are allow-pending and temporary.
328
329 proc gdb_breakpoint { function args } {
330 global gdb_prompt
331 global decimal
332
333 set pending_response n
334 if {[lsearch -exact [lindex $args 0] allow-pending] != -1} {
335 set pending_response y
336 }
337
338 set break_command "break"
339 if {[lsearch -exact [lindex $args 0] temporary] != -1} {
340 set break_command "tbreak"
341 }
342
343 send_gdb "$break_command $function\n"
344 # The first two regexps are what we get with -g, the third is without -g.
345 gdb_expect 30 {
346 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
347 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
348 -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
349 -re "Breakpoint \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
350 if {$pending_response == "n"} {
351 fail "setting breakpoint at $function"
352 return 0
353 }
354 }
355 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
356 send_gdb "$pending_response\n"
357 exp_continue
358 }
359 -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
360 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
361 }
362 return 1;
363 }
364
365 # Set breakpoint at function and run gdb until it breaks there.
366 # Since this is the only breakpoint that will be set, if it stops
367 # at a breakpoint, we will assume it is the one we want. We can't
368 # just compare to "function" because it might be a fully qualified,
369 # single quoted C++ function specifier. If there's an additional argument,
370 # pass it to gdb_breakpoint.
371
372 proc runto { function args } {
373 global gdb_prompt
374 global decimal
375
376 delete_breakpoints
377
378 if ![gdb_breakpoint $function [lindex $args 0]] {
379 return 0;
380 }
381
382 gdb_run_cmd
383
384 # the "at foo.c:36" output we get with -g.
385 # the "in func" output we get without -g.
386 gdb_expect 30 {
387 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
388 return 1
389 }
390 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
391 return 1
392 }
393 -re "$gdb_prompt $" {
394 fail "running to $function in runto"
395 return 0
396 }
397 timeout {
398 fail "running to $function in runto (timeout)"
399 return 0
400 }
401 }
402 return 1
403 }
404
405 #
406 # runto_main -- ask gdb to run until we hit a breakpoint at main.
407 # The case where the target uses stubs has to be handled
408 # specially--if it uses stubs, assuming we hit
409 # breakpoint() and just step out of the function.
410 #
411 proc runto_main { } {
412 global gdb_prompt
413 global decimal
414
415 if ![target_info exists gdb_stub] {
416 return [runto main]
417 }
418
419 delete_breakpoints
420
421 gdb_step_for_stub;
422
423 return 1
424 }
425
426
427 ### Continue, and expect to hit a breakpoint.
428 ### Report a pass or fail, depending on whether it seems to have
429 ### worked. Use NAME as part of the test name; each call to
430 ### continue_to_breakpoint should use a NAME which is unique within
431 ### that test file.
432 proc gdb_continue_to_breakpoint {name} {
433 global gdb_prompt
434 set full_name "continue to breakpoint: $name"
435
436 send_gdb "continue\n"
437 gdb_expect {
438 -re "Breakpoint .* at .*\r\n$gdb_prompt $" {
439 pass $full_name
440 }
441 -re ".*$gdb_prompt $" {
442 fail $full_name
443 }
444 timeout {
445 fail "$full_name (timeout)"
446 }
447 }
448 }
449
450
451 # gdb_internal_error_resync:
452 #
453 # Answer the questions GDB asks after it reports an internal error
454 # until we get back to a GDB prompt. Decline to quit the debugging
455 # session, and decline to create a core file. Return non-zero if the
456 # resync succeeds.
457 #
458 # This procedure just answers whatever questions come up until it sees
459 # a GDB prompt; it doesn't require you to have matched the input up to
460 # any specific point. However, it only answers questions it sees in
461 # the output itself, so if you've matched a question, you had better
462 # answer it yourself before calling this.
463 #
464 # You can use this function thus:
465 #
466 # gdb_expect {
467 # ...
468 # -re ".*A problem internal to GDB has been detected" {
469 # gdb_internal_error_resync
470 # }
471 # ...
472 # }
473 #
474 proc gdb_internal_error_resync {} {
475 global gdb_prompt
476
477 set count 0
478 while {$count < 10} {
479 gdb_expect {
480 -re "Quit this debugging session\\? \\(y or n\\) $" {
481 send_gdb "n\n"
482 incr count
483 }
484 -re "Create a core file of GDB\\? \\(y or n\\) $" {
485 send_gdb "n\n"
486 incr count
487 }
488 -re "$gdb_prompt $" {
489 # We're resynchronized.
490 return 1
491 }
492 timeout {
493 perror "Could not resync from internal error (timeout)"
494 return 0
495 }
496 }
497 }
498 perror "Could not resync from internal error (resync count exceeded)"
499 return 0
500 }
501
502
503 # gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
504 # Send a command to gdb; test the result.
505 #
506 # COMMAND is the command to execute, send to GDB with send_gdb. If
507 # this is the null string no command is sent.
508 # MESSAGE is a message to be printed with the built-in failure patterns
509 # if one of them matches. If MESSAGE is empty COMMAND will be used.
510 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
511 # patterns. Pattern elements will be evaluated in the caller's
512 # context; action elements will be executed in the caller's context.
513 # Unlike patterns for gdb_test, these patterns should generally include
514 # the final newline and prompt.
515 #
516 # Returns:
517 # 1 if the test failed, according to a built-in failure pattern
518 # 0 if only user-supplied patterns matched
519 # -1 if there was an internal error.
520 #
521 # You can use this function thus:
522 #
523 # gdb_test_multiple "print foo" "test foo" {
524 # -re "expected output 1" {
525 # pass "print foo"
526 # }
527 # -re "expected output 2" {
528 # fail "print foo"
529 # }
530 # }
531 #
532 # The standard patterns, such as "Program exited..." and "A problem
533 # ...", all being implicitly appended to that list.
534 #
535 proc gdb_test_multiple { command message user_code } {
536 global verbose
537 global gdb_prompt
538 global GDB
539 upvar timeout timeout
540 upvar expect_out expect_out
541
542 if { $message == "" } {
543 set message $command
544 }
545
546 # TCL/EXPECT WART ALERT
547 # Expect does something very strange when it receives a single braced
548 # argument. It splits it along word separators and performs substitutions.
549 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
550 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
551 # double-quoted list item, "\[ab\]" is just a long way of representing
552 # "[ab]", because the backslashes will be removed by lindex.
553
554 # Unfortunately, there appears to be no easy way to duplicate the splitting
555 # that expect will do from within TCL. And many places make use of the
556 # "\[0-9\]" construct, so we need to support that; and some places make use
557 # of the "[func]" construct, so we need to support that too. In order to
558 # get this right we have to substitute quoted list elements differently
559 # from braced list elements.
560
561 # We do this roughly the same way that Expect does it. We have to use two
562 # lists, because if we leave unquoted newlines in the argument to uplevel
563 # they'll be treated as command separators, and if we escape newlines
564 # we mangle newlines inside of command blocks. This assumes that the
565 # input doesn't contain a pattern which contains actual embedded newlines
566 # at this point!
567
568 regsub -all {\n} ${user_code} { } subst_code
569 set subst_code [uplevel list $subst_code]
570
571 set processed_code ""
572 set patterns ""
573 set expecting_action 0
574 foreach item $user_code subst_item $subst_code {
575 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
576 lappend processed_code $item
577 continue
578 }
579 if {$item == "-indices" || $item == "-re" || $item == "-ex"} {
580 lappend processed_code $item
581 continue
582 }
583 if { $expecting_action } {
584 lappend processed_code "uplevel [list $item]"
585 set expecting_action 0
586 # Cosmetic, no effect on the list.
587 append processed_code "\n"
588 continue
589 }
590 set expecting_action 1
591 lappend processed_code $subst_item
592 if {$patterns != ""} {
593 append patterns "; "
594 }
595 append patterns "\"$subst_item\""
596 }
597
598 # Also purely cosmetic.
599 regsub -all {\r} $patterns {\\r} patterns
600 regsub -all {\n} $patterns {\\n} patterns
601
602 if $verbose>2 then {
603 send_user "Sending \"$command\" to gdb\n"
604 send_user "Looking to match \"$patterns\"\n"
605 send_user "Message is \"$message\"\n"
606 }
607
608 set result -1
609 set string "${command}\n";
610 if { $command != "" } {
611 while { "$string" != "" } {
612 set foo [string first "\n" "$string"];
613 set len [string length "$string"];
614 if { $foo < [expr $len - 1] } {
615 set str [string range "$string" 0 $foo];
616 if { [send_gdb "$str"] != "" } {
617 global suppress_flag;
618
619 if { ! $suppress_flag } {
620 perror "Couldn't send $command to GDB.";
621 }
622 fail "$message";
623 return $result;
624 }
625 # since we're checking if each line of the multi-line
626 # command are 'accepted' by GDB here,
627 # we need to set -notransfer expect option so that
628 # command output is not lost for pattern matching
629 # - guo
630 gdb_expect 2 {
631 -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
632 timeout { verbose "partial: timeout" 3 }
633 }
634 set string [string range "$string" [expr $foo + 1] end];
635 } else {
636 break;
637 }
638 }
639 if { "$string" != "" } {
640 if { [send_gdb "$string"] != "" } {
641 global suppress_flag;
642
643 if { ! $suppress_flag } {
644 perror "Couldn't send $command to GDB.";
645 }
646 fail "$message";
647 return $result;
648 }
649 }
650 }
651
652 if [target_info exists gdb,timeout] {
653 set tmt [target_info gdb,timeout];
654 } else {
655 if [info exists timeout] {
656 set tmt $timeout;
657 } else {
658 global timeout;
659 if [info exists timeout] {
660 set tmt $timeout;
661 } else {
662 set tmt 60;
663 }
664 }
665 }
666
667 set code {
668 -re ".*A problem internal to GDB has been detected" {
669 fail "$message (GDB internal error)"
670 gdb_internal_error_resync
671 }
672 -re "\\*\\*\\* DOSEXIT code.*" {
673 if { $message != "" } {
674 fail "$message";
675 }
676 gdb_suppress_entire_file "GDB died";
677 set result -1;
678 }
679 }
680 append code $processed_code
681 append code {
682 -re "Ending remote debugging.*$gdb_prompt $" {
683 if ![isnative] then {
684 warning "Can`t communicate to remote target."
685 }
686 gdb_exit
687 gdb_start
688 set result -1
689 }
690 -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
691 perror "Undefined command \"$command\"."
692 fail "$message"
693 set result 1
694 }
695 -re "Ambiguous command.*$gdb_prompt $" {
696 perror "\"$command\" is not a unique command name."
697 fail "$message"
698 set result 1
699 }
700 -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
701 if ![string match "" $message] then {
702 set errmsg "$message (the program exited)"
703 } else {
704 set errmsg "$command (the program exited)"
705 }
706 fail "$errmsg"
707 set result -1
708 }
709 -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" {
710 if ![string match "" $message] then {
711 set errmsg "$message (the program exited)"
712 } else {
713 set errmsg "$command (the program exited)"
714 }
715 fail "$errmsg"
716 set result -1
717 }
718 -re "The program is not being run.*$gdb_prompt $" {
719 if ![string match "" $message] then {
720 set errmsg "$message (the program is no longer running)"
721 } else {
722 set errmsg "$command (the program is no longer running)"
723 }
724 fail "$errmsg"
725 set result -1
726 }
727 -re "\r\n$gdb_prompt $" {
728 if ![string match "" $message] then {
729 fail "$message"
730 }
731 set result 1
732 }
733 "<return>" {
734 send_gdb "\n"
735 perror "Window too small."
736 fail "$message"
737 set result -1
738 }
739 -re "\\(y or n\\) " {
740 send_gdb "n\n"
741 perror "Got interactive prompt."
742 fail "$message"
743 set result -1
744 }
745 eof {
746 perror "Process no longer exists"
747 if { $message != "" } {
748 fail "$message"
749 }
750 return -1
751 }
752 full_buffer {
753 perror "internal buffer is full."
754 fail "$message"
755 set result -1
756 }
757 timeout {
758 if ![string match "" $message] then {
759 fail "$message (timeout)"
760 }
761 set result 1
762 }
763 }
764
765 set result 0
766 set code [catch {gdb_expect $tmt $code} string]
767 if {$code == 1} {
768 global errorInfo errorCode;
769 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
770 } elseif {$code == 2} {
771 return -code return $string
772 } elseif {$code == 3} {
773 return
774 } elseif {$code > 4} {
775 return -code $code $string
776 }
777 return $result
778 }
779
780 # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
781 # Send a command to gdb; test the result.
782 #
783 # COMMAND is the command to execute, send to GDB with send_gdb. If
784 # this is the null string no command is sent.
785 # PATTERN is the pattern to match for a PASS, and must NOT include
786 # the \r\n sequence immediately before the gdb prompt.
787 # MESSAGE is an optional message to be printed. If this is
788 # omitted, then the pass/fail messages use the command string as the
789 # message. (If this is the empty string, then sometimes we don't
790 # call pass or fail at all; I don't understand this at all.)
791 # QUESTION is a question GDB may ask in response to COMMAND, like
792 # "are you sure?"
793 # RESPONSE is the response to send if QUESTION appears.
794 #
795 # Returns:
796 # 1 if the test failed,
797 # 0 if the test passes,
798 # -1 if there was an internal error.
799 #
800 proc gdb_test { args } {
801 global verbose
802 global gdb_prompt
803 global GDB
804 upvar timeout timeout
805
806 if [llength $args]>2 then {
807 set message [lindex $args 2]
808 } else {
809 set message [lindex $args 0]
810 }
811 set command [lindex $args 0]
812 set pattern [lindex $args 1]
813
814 if [llength $args]==5 {
815 set question_string [lindex $args 3];
816 set response_string [lindex $args 4];
817 } else {
818 set question_string "^FOOBAR$"
819 }
820
821 return [gdb_test_multiple $command $message {
822 -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
823 if ![string match "" $message] then {
824 pass "$message"
825 }
826 }
827 -re "(${question_string})$" {
828 send_gdb "$response_string\n";
829 exp_continue;
830 }
831 }]
832 }
833 \f
834 # Test that a command gives an error. For pass or fail, return
835 # a 1 to indicate that more tests can proceed. However a timeout
836 # is a serious error, generates a special fail message, and causes
837 # a 0 to be returned to indicate that more tests are likely to fail
838 # as well.
839
840 proc test_print_reject { args } {
841 global gdb_prompt
842 global verbose
843
844 if [llength $args]==2 then {
845 set expectthis [lindex $args 1]
846 } else {
847 set expectthis "should never match this bogus string"
848 }
849 set sendthis [lindex $args 0]
850 if $verbose>2 then {
851 send_user "Sending \"$sendthis\" to gdb\n"
852 send_user "Looking to match \"$expectthis\"\n"
853 }
854 send_gdb "$sendthis\n"
855 #FIXME: Should add timeout as parameter.
856 gdb_expect {
857 -re "A .* in expression.*\\.*$gdb_prompt $" {
858 pass "reject $sendthis"
859 return 1
860 }
861 -re "Invalid syntax in expression.*$gdb_prompt $" {
862 pass "reject $sendthis"
863 return 1
864 }
865 -re "Junk after end of expression.*$gdb_prompt $" {
866 pass "reject $sendthis"
867 return 1
868 }
869 -re "Invalid number.*$gdb_prompt $" {
870 pass "reject $sendthis"
871 return 1
872 }
873 -re "Invalid character constant.*$gdb_prompt $" {
874 pass "reject $sendthis"
875 return 1
876 }
877 -re "No symbol table is loaded.*$gdb_prompt $" {
878 pass "reject $sendthis"
879 return 1
880 }
881 -re "No symbol .* in current context.*$gdb_prompt $" {
882 pass "reject $sendthis"
883 return 1
884 }
885 -re "Unmatched single quote.*$gdb_prompt $" {
886 pass "reject $sendthis"
887 return 1
888 }
889 -re "A character constant must contain at least one character.*$gdb_prompt $" {
890 pass "reject $sendthis"
891 return 1
892 }
893 -re "$expectthis.*$gdb_prompt $" {
894 pass "reject $sendthis"
895 return 1
896 }
897 -re ".*$gdb_prompt $" {
898 fail "reject $sendthis"
899 return 1
900 }
901 default {
902 fail "reject $sendthis (eof or timeout)"
903 return 0
904 }
905 }
906 }
907 \f
908 # Given an input string, adds backslashes as needed to create a
909 # regexp that will match the string.
910
911 proc string_to_regexp {str} {
912 set result $str
913 regsub -all {[]*+.|()^$\[\\]} $str {\\&} result
914 return $result
915 }
916
917 # Same as gdb_test, but the second parameter is not a regexp,
918 # but a string that must match exactly.
919
920 proc gdb_test_exact { args } {
921 upvar timeout timeout
922
923 set command [lindex $args 0]
924
925 # This applies a special meaning to a null string pattern. Without
926 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
927 # messages from commands that should have no output except a new
928 # prompt. With this, only results of a null string will match a null
929 # string pattern.
930
931 set pattern [lindex $args 1]
932 if [string match $pattern ""] {
933 set pattern [string_to_regexp [lindex $args 0]]
934 } else {
935 set pattern [string_to_regexp [lindex $args 1]]
936 }
937
938 # It is most natural to write the pattern argument with only
939 # embedded \n's, especially if you are trying to avoid Tcl quoting
940 # problems. But gdb_expect really wants to see \r\n in patterns. So
941 # transform the pattern here. First transform \r\n back to \n, in
942 # case some users of gdb_test_exact already do the right thing.
943 regsub -all "\r\n" $pattern "\n" pattern
944 regsub -all "\n" $pattern "\r\n" pattern
945 if [llength $args]==3 then {
946 set message [lindex $args 2]
947 } else {
948 set message $command
949 }
950
951 return [gdb_test $command $pattern $message]
952 }
953 \f
954 proc gdb_reinitialize_dir { subdir } {
955 global gdb_prompt
956
957 if [is_remote host] {
958 return "";
959 }
960 send_gdb "dir\n"
961 gdb_expect 60 {
962 -re "Reinitialize source path to empty.*y or n. " {
963 send_gdb "y\n"
964 gdb_expect 60 {
965 -re "Source directories searched.*$gdb_prompt $" {
966 send_gdb "dir $subdir\n"
967 gdb_expect 60 {
968 -re "Source directories searched.*$gdb_prompt $" {
969 verbose "Dir set to $subdir"
970 }
971 -re "$gdb_prompt $" {
972 perror "Dir \"$subdir\" failed."
973 }
974 }
975 }
976 -re "$gdb_prompt $" {
977 perror "Dir \"$subdir\" failed."
978 }
979 }
980 }
981 -re "$gdb_prompt $" {
982 perror "Dir \"$subdir\" failed."
983 }
984 }
985 }
986
987 #
988 # gdb_exit -- exit the GDB, killing the target program if necessary
989 #
990 proc default_gdb_exit {} {
991 global GDB
992 global GDBFLAGS
993 global verbose
994 global gdb_spawn_id;
995
996 gdb_stop_suppressing_tests;
997
998 if ![info exists gdb_spawn_id] {
999 return;
1000 }
1001
1002 verbose "Quitting $GDB $GDBFLAGS"
1003
1004 if { [is_remote host] && [board_info host exists fileid] } {
1005 send_gdb "quit\n";
1006 gdb_expect 10 {
1007 -re "y or n" {
1008 send_gdb "y\n";
1009 exp_continue;
1010 }
1011 -re "DOSEXIT code" { }
1012 default { }
1013 }
1014 }
1015
1016 if ![is_remote host] {
1017 remote_close host;
1018 }
1019 unset gdb_spawn_id
1020 }
1021
1022 # Load a file into the debugger.
1023 # The return value is 0 for success, -1 for failure.
1024 #
1025 # This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1026 # to one of these values:
1027 #
1028 # debug file was loaded successfully and has debug information
1029 # nodebug file was loaded successfully and has no debug information
1030 # fail file was not loaded
1031 #
1032 # I tried returning this information as part of the return value,
1033 # but ran into a mess because of the many re-implementations of
1034 # gdb_load in config/*.exp.
1035 #
1036 # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
1037 # this if they can get more information set.
1038
1039 proc gdb_file_cmd { arg } {
1040 global gdb_prompt
1041 global verbose
1042 global GDB
1043 global last_loaded_file
1044
1045 set last_loaded_file $arg
1046
1047 # Set whether debug info was found.
1048 # Default to "fail".
1049 global gdb_file_cmd_debug_info
1050 set gdb_file_cmd_debug_info "fail"
1051
1052 if [is_remote host] {
1053 set arg [remote_download host $arg]
1054 if { $arg == "" } {
1055 perror "download failed"
1056 return -1
1057 }
1058 }
1059
1060 # The file command used to kill the remote target. For the benefit
1061 # of the testsuite, preserve this behavior.
1062 send_gdb "kill\n"
1063 gdb_expect 120 {
1064 -re "Kill the program being debugged. .y or n. $" {
1065 send_gdb "y\n"
1066 verbose "\t\tKilling previous program being debugged"
1067 exp_continue
1068 }
1069 -re "$gdb_prompt $" {
1070 # OK.
1071 }
1072 }
1073
1074 send_gdb "file $arg\n"
1075 gdb_expect 120 {
1076 -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" {
1077 verbose "\t\tLoaded $arg into the $GDB with no debugging symbols"
1078 set gdb_file_cmd_debug_info "nodebug"
1079 return 0
1080 }
1081 -re "Reading symbols from.*done.*$gdb_prompt $" {
1082 verbose "\t\tLoaded $arg into the $GDB"
1083 set gdb_file_cmd_debug_info "debug"
1084 return 0
1085 }
1086 -re "Load new symbol table from \".*\".*y or n. $" {
1087 send_gdb "y\n"
1088 gdb_expect 120 {
1089 -re "Reading symbols from.*done.*$gdb_prompt $" {
1090 verbose "\t\tLoaded $arg with new symbol table into $GDB"
1091 set gdb_file_cmd_debug_info "debug"
1092 return 0
1093 }
1094 timeout {
1095 perror "(timeout) Couldn't load $arg, other program already loaded."
1096 return -1
1097 }
1098 }
1099 }
1100 -re "No such file or directory.*$gdb_prompt $" {
1101 perror "($arg) No such file or directory"
1102 return -1
1103 }
1104 -re "$gdb_prompt $" {
1105 perror "couldn't load $arg into $GDB."
1106 return -1
1107 }
1108 timeout {
1109 perror "couldn't load $arg into $GDB (timed out)."
1110 return -1
1111 }
1112 eof {
1113 # This is an attempt to detect a core dump, but seems not to
1114 # work. Perhaps we need to match .* followed by eof, in which
1115 # gdb_expect does not seem to have a way to do that.
1116 perror "couldn't load $arg into $GDB (end of file)."
1117 return -1
1118 }
1119 }
1120 }
1121
1122 #
1123 # start gdb -- start gdb running, default procedure
1124 #
1125 # When running over NFS, particularly if running many simultaneous
1126 # tests on different hosts all using the same server, things can
1127 # get really slow. Give gdb at least 3 minutes to start up.
1128 #
1129 proc default_gdb_start { } {
1130 global verbose
1131 global GDB
1132 global GDBFLAGS
1133 global gdb_prompt
1134 global timeout
1135 global gdb_spawn_id;
1136
1137 gdb_stop_suppressing_tests;
1138
1139 verbose "Spawning $GDB -nw $GDBFLAGS"
1140
1141 if [info exists gdb_spawn_id] {
1142 return 0;
1143 }
1144
1145 if ![is_remote host] {
1146 if { [which $GDB] == 0 } then {
1147 perror "$GDB does not exist."
1148 exit 1
1149 }
1150 }
1151 set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
1152 if { $res < 0 || $res == "" } {
1153 perror "Spawning $GDB failed."
1154 return 1;
1155 }
1156 gdb_expect 360 {
1157 -re "\[\r\n\]$gdb_prompt $" {
1158 verbose "GDB initialized."
1159 }
1160 -re "$gdb_prompt $" {
1161 perror "GDB never initialized."
1162 return -1
1163 }
1164 timeout {
1165 perror "(timeout) GDB never initialized after 10 seconds."
1166 remote_close host;
1167 return -1
1168 }
1169 }
1170 set gdb_spawn_id -1;
1171 # force the height to "unlimited", so no pagers get used
1172
1173 send_gdb "set height 0\n"
1174 gdb_expect 10 {
1175 -re "$gdb_prompt $" {
1176 verbose "Setting height to 0." 2
1177 }
1178 timeout {
1179 warning "Couldn't set the height to 0"
1180 }
1181 }
1182 # force the width to "unlimited", so no wraparound occurs
1183 send_gdb "set width 0\n"
1184 gdb_expect 10 {
1185 -re "$gdb_prompt $" {
1186 verbose "Setting width to 0." 2
1187 }
1188 timeout {
1189 warning "Couldn't set the width to 0."
1190 }
1191 }
1192 return 0;
1193 }
1194
1195 # Return a 1 for configurations for which we don't even want to try to
1196 # test C++.
1197
1198 proc skip_cplus_tests {} {
1199 if { [istarget "h8300-*-*"] } {
1200 return 1
1201 }
1202
1203 # The C++ IO streams are too large for HC11/HC12 and are thus not
1204 # available. The gdb C++ tests use them and don't compile.
1205 if { [istarget "m6811-*-*"] } {
1206 return 1
1207 }
1208 if { [istarget "m6812-*-*"] } {
1209 return 1
1210 }
1211 return 0
1212 }
1213
1214 # Return a 1 if I don't even want to try to test FORTRAN.
1215
1216 proc skip_fortran_tests {} {
1217 return 0
1218 }
1219
1220 # Return a 1 if we should skip shared library tests.
1221
1222 proc skip_shlib_tests {} {
1223 # Run the shared library tests on native systems.
1224 if {[isnative]} {
1225 return 0
1226 }
1227
1228 # An abbreviated list of remote targets where we should be able to
1229 # run shared library tests.
1230 if {([istarget *-*-linux*]
1231 || [istarget *-*-*bsd*]
1232 || [istarget *-*-solaris2*]
1233 || [istarget arm*-*-symbianelf*]
1234 || [istarget *-*-mingw*]
1235 || [istarget *-*-cygwin*]
1236 || [istarget *-*-pe*])} {
1237 return 0
1238 }
1239
1240 return 1
1241 }
1242
1243 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
1244 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
1245
1246 proc skip_altivec_tests {} {
1247 global skip_vmx_tests_saved
1248 global srcdir subdir gdb_prompt
1249
1250 # Use the cached value, if it exists.
1251 set me "skip_altivec_tests"
1252 if [info exists skip_vmx_tests_saved] {
1253 verbose "$me: returning saved $skip_vmx_tests_saved" 2
1254 return $skip_vmx_tests_saved
1255 }
1256
1257 # Some simulators are known to not support VMX instructions.
1258 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1259 verbose "$me: target known to not support VMX, returning 1" 2
1260 return [set skip_vmx_tests_saved 1]
1261 }
1262
1263 # Make sure we have a compiler that understands altivec.
1264 set compile_flags {debug nowarnings}
1265 if [get_compiler_info not-used] {
1266 warning "Could not get compiler info"
1267 return 1
1268 }
1269 if [test_compiler_info gcc*] {
1270 set compile_flags "$compile_flags additional_flags=-maltivec"
1271 } elseif [test_compiler_info xlc*] {
1272 set compile_flags "$compile_flags additional_flags=-qaltivec"
1273 } else {
1274 verbose "Could not compile with altivec support, returning 1" 2
1275 return 1
1276 }
1277
1278 # Set up, compile, and execute a test program containing VMX instructions.
1279 # Include the current process ID in the file names to prevent conflicts
1280 # with invocations for multiple testsuites.
1281 set src vmx[pid].c
1282 set exe vmx[pid].x
1283
1284 set f [open $src "w"]
1285 puts $f "int main() {"
1286 puts $f "#ifdef __MACH__"
1287 puts $f " asm volatile (\"vor v0,v0,v0\");"
1288 puts $f "#else"
1289 puts $f " asm volatile (\"vor 0,0,0\");"
1290 puts $f "#endif"
1291 puts $f " return 0; }"
1292 close $f
1293
1294 verbose "$me: compiling testfile $src" 2
1295 set lines [gdb_compile $src $exe executable $compile_flags]
1296 file delete $src
1297
1298 if ![string match "" $lines] then {
1299 verbose "$me: testfile compilation failed, returning 1" 2
1300 return [set skip_vmx_tests_saved 1]
1301 }
1302
1303 # No error message, compilation succeeded so now run it via gdb.
1304
1305 gdb_exit
1306 gdb_start
1307 gdb_reinitialize_dir $srcdir/$subdir
1308 gdb_load "$exe"
1309 gdb_run_cmd
1310 gdb_expect {
1311 -re ".*Illegal instruction.*${gdb_prompt} $" {
1312 verbose -log "\n$me altivec hardware not detected"
1313 set skip_vmx_tests_saved 1
1314 }
1315 -re ".*Program exited normally.*${gdb_prompt} $" {
1316 verbose -log "\n$me: altivec hardware detected"
1317 set skip_vmx_tests_saved 0
1318 }
1319 default {
1320 warning "\n$me: default case taken"
1321 set skip_vmx_tests_saved 1
1322 }
1323 }
1324 gdb_exit
1325 remote_file build delete $exe
1326
1327 verbose "$me: returning $skip_vmx_tests_saved" 2
1328 return $skip_vmx_tests_saved
1329 }
1330
1331 # Skip all the tests in the file if you are not on an hppa running
1332 # hpux target.
1333
1334 proc skip_hp_tests {} {
1335 eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
1336 verbose "Skip hp tests is $skip_hp"
1337 return $skip_hp
1338 }
1339
1340 set compiler_info "unknown"
1341 set gcc_compiled 0
1342 set hp_cc_compiler 0
1343 set hp_aCC_compiler 0
1344
1345 # Figure out what compiler I am using.
1346 #
1347 # BINFILE is a "compiler information" output file. This implementation
1348 # does not use BINFILE.
1349 #
1350 # ARGS can be empty or "C++". If empty, "C" is assumed.
1351 #
1352 # There are several ways to do this, with various problems.
1353 #
1354 # [ gdb_compile -E $ifile -o $binfile.ci ]
1355 # source $binfile.ci
1356 #
1357 # Single Unix Spec v3 says that "-E -o ..." together are not
1358 # specified. And in fact, the native compiler on hp-ux 11 (among
1359 # others) does not work with "-E -o ...". Most targets used to do
1360 # this, and it mostly worked, because it works with gcc.
1361 #
1362 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1363 # source $binfile.ci
1364 #
1365 # This avoids the problem with -E and -o together. This almost works
1366 # if the build machine is the same as the host machine, which is
1367 # usually true of the targets which are not gcc. But this code does
1368 # not figure which compiler to call, and it always ends up using the C
1369 # compiler. Not good for setting hp_aCC_compiler. Targets
1370 # hppa*-*-hpux* and mips*-*-irix* used to do this.
1371 #
1372 # [ gdb_compile -E $ifile > $binfile.ci ]
1373 # source $binfile.ci
1374 #
1375 # dejagnu target_compile says that it supports output redirection,
1376 # but the code is completely different from the normal path and I
1377 # don't want to sweep the mines from that path. So I didn't even try
1378 # this.
1379 #
1380 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1381 # eval $cppout
1382 #
1383 # I actually do this for all targets now. gdb_compile runs the right
1384 # compiler, and TCL captures the output, and I eval the output.
1385 #
1386 # Unfortunately, expect logs the output of the command as it goes by,
1387 # and dejagnu helpfully prints a second copy of it right afterwards.
1388 # So I turn off expect logging for a moment.
1389 #
1390 # [ gdb_compile $ifile $ciexe_file executable $args ]
1391 # [ remote_exec $ciexe_file ]
1392 # [ source $ci_file.out ]
1393 #
1394 # I could give up on -E and just do this.
1395 # I didn't get desperate enough to try this.
1396 #
1397 # -- chastain 2004-01-06
1398
1399 proc get_compiler_info {binfile args} {
1400 # For compiler.c and compiler.cc
1401 global srcdir
1402
1403 # I am going to play with the log to keep noise out.
1404 global outdir
1405 global tool
1406
1407 # These come from compiler.c or compiler.cc
1408 global compiler_info
1409
1410 # Legacy global data symbols.
1411 global gcc_compiled
1412 global hp_cc_compiler
1413 global hp_aCC_compiler
1414
1415 # Choose which file to preprocess.
1416 set ifile "${srcdir}/lib/compiler.c"
1417 if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1418 set ifile "${srcdir}/lib/compiler.cc"
1419 }
1420
1421 # Run $ifile through the right preprocessor.
1422 # Toggle gdb.log to keep the compiler output out of the log.
1423 log_file
1424 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1425 log_file -a "$outdir/$tool.log"
1426
1427 # Eval the output.
1428 set unknown 0
1429 foreach cppline [ split "$cppout" "\n" ] {
1430 if { [ regexp "^#" "$cppline" ] } {
1431 # line marker
1432 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1433 # blank line
1434 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
1435 # eval this line
1436 verbose "get_compiler_info: $cppline" 2
1437 eval "$cppline"
1438 } else {
1439 # unknown line
1440 verbose -log "get_compiler_info: $cppline"
1441 set unknown 1
1442 }
1443 }
1444
1445 # Reset to unknown compiler if any diagnostics happened.
1446 if { $unknown } {
1447 set compiler_info "unknown"
1448 }
1449
1450 # Set the legacy symbols.
1451 set gcc_compiled 0
1452 set hp_cc_compiler 0
1453 set hp_aCC_compiler 0
1454 if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
1455 if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
1456 if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
1457 if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
1458 if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
1459 if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 }
1460 if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
1461
1462 # Log what happened.
1463 verbose -log "get_compiler_info: $compiler_info"
1464
1465 # Most compilers will evaluate comparisons and other boolean
1466 # operations to 0 or 1.
1467 uplevel \#0 { set true 1 }
1468 uplevel \#0 { set false 0 }
1469
1470 # Use of aCC results in boolean results being displayed as
1471 # "true" or "false"
1472 if { $hp_aCC_compiler } {
1473 uplevel \#0 { set true true }
1474 uplevel \#0 { set false false }
1475 }
1476
1477 return 0;
1478 }
1479
1480 proc test_compiler_info { {compiler ""} } {
1481 global compiler_info
1482
1483 # if no arg, return the compiler_info string
1484
1485 if [string match "" $compiler] {
1486 if [info exists compiler_info] {
1487 return $compiler_info
1488 } else {
1489 perror "No compiler info found."
1490 }
1491 }
1492
1493 return [string match $compiler $compiler_info]
1494 }
1495
1496 set gdb_wrapper_initialized 0
1497
1498 proc gdb_wrapper_init { args } {
1499 global gdb_wrapper_initialized;
1500 global gdb_wrapper_file;
1501 global gdb_wrapper_flags;
1502
1503 if { $gdb_wrapper_initialized == 1 } { return; }
1504
1505 if {[target_info exists needs_status_wrapper] && \
1506 [target_info needs_status_wrapper] != "0"} {
1507 set result [build_wrapper "testglue.o"];
1508 if { $result != "" } {
1509 set gdb_wrapper_file [lindex $result 0];
1510 set gdb_wrapper_flags [lindex $result 1];
1511 } else {
1512 warning "Status wrapper failed to build."
1513 }
1514 }
1515 set gdb_wrapper_initialized 1
1516 }
1517
1518 proc gdb_compile {source dest type options} {
1519 global GDB_TESTCASE_OPTIONS;
1520 global gdb_wrapper_file;
1521 global gdb_wrapper_flags;
1522 global gdb_wrapper_initialized;
1523
1524 # Add platform-specific options if a shared library was specified using
1525 # "shlib=librarypath" in OPTIONS.
1526 set new_options ""
1527 set shlib_found 0
1528 foreach opt $options {
1529 if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
1530 if [test_compiler_info "xlc-*"] {
1531 # IBM xlc compiler doesn't accept shared library named other
1532 # than .so: use "-Wl," to bypass this
1533 lappend source "-Wl,$shlib_name"
1534 } elseif { ([istarget "*-*-mingw*"]
1535 || [istarget *-*-cygwin*]
1536 || [istarget *-*-pe*])} {
1537 lappend source "${shlib_name}.a"
1538 } else {
1539 lappend source $shlib_name
1540 }
1541 if {$shlib_found == 0} {
1542 set shlib_found 1
1543 if { ([test_compiler_info "gcc-*"]
1544 && ([istarget "powerpc*-*-aix*"]
1545 || [istarget "rs6000*-*-aix*"] )) } {
1546 lappend options "additional_flags=-L${objdir}/${subdir}"
1547 } elseif { [istarget "mips-sgi-irix*"] } {
1548 lappend options "additional_flags=-rpath ${objdir}/${subdir}"
1549 }
1550 }
1551 } elseif { $opt == "shlib_load" } {
1552 if { ([istarget "*-*-mingw*"]
1553 || [istarget *-*-cygwin*]
1554 || [istarget *-*-pe*]
1555 || [istarget arm*-*-symbianelf*]
1556 || [istarget hppa*-*-hpux*])} {
1557 # Do not need anything.
1558 } else {
1559 lappend new_options "libs=-ldl"
1560 lappend new_options "additional_flags=-Wl,-rpath,\\\$ORIGIN"
1561 }
1562 } else {
1563 lappend new_options $opt
1564 }
1565 }
1566 set options $new_options
1567
1568 if [target_info exists gdb_stub] {
1569 set options2 { "additional_flags=-Dusestubs" }
1570 lappend options "libs=[target_info gdb_stub]";
1571 set options [concat $options2 $options]
1572 }
1573 if [target_info exists is_vxworks] {
1574 set options2 { "additional_flags=-Dvxworks" }
1575 lappend options "libs=[target_info gdb_stub]";
1576 set options [concat $options2 $options]
1577 }
1578 if [info exists GDB_TESTCASE_OPTIONS] {
1579 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1580 }
1581 verbose "options are $options"
1582 verbose "source is $source $dest $type $options"
1583
1584 if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
1585
1586 if {[target_info exists needs_status_wrapper] && \
1587 [target_info needs_status_wrapper] != "0" && \
1588 [info exists gdb_wrapper_file]} {
1589 lappend options "libs=${gdb_wrapper_file}"
1590 lappend options "ldflags=${gdb_wrapper_flags}"
1591 }
1592
1593 # Replace the "nowarnings" option with the appropriate additional_flags
1594 # to disable compiler warnings.
1595 set nowarnings [lsearch -exact $options nowarnings]
1596 if {$nowarnings != -1} {
1597 if [target_info exists gdb,nowarnings_flag] {
1598 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
1599 } else {
1600 set flag "additional_flags=-w"
1601 }
1602 set options [lreplace $options $nowarnings $nowarnings $flag]
1603 }
1604
1605 set result [target_compile $source $dest $type $options];
1606
1607 # Prune uninteresting compiler (and linker) output.
1608 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
1609
1610 regsub "\[\r\n\]*$" "$result" "" result;
1611 regsub "^\[\r\n\]*" "$result" "" result;
1612
1613 if { $result != "" && [lsearch $options quiet] == -1} {
1614 clone_output "gdb compile failed, $result"
1615 }
1616 return $result;
1617 }
1618
1619
1620 # This is just like gdb_compile, above, except that it tries compiling
1621 # against several different thread libraries, to see which one this
1622 # system has.
1623 proc gdb_compile_pthreads {source dest type options} {
1624 set built_binfile 0
1625 set why_msg "unrecognized error"
1626 foreach lib {-lpthreads -lpthread -lthread} {
1627 # This kind of wipes out whatever libs the caller may have
1628 # set. Or maybe theirs will override ours. How infelicitous.
1629 set options_with_lib [concat $options [list libs=$lib quiet]]
1630 set ccout [gdb_compile $source $dest $type $options_with_lib]
1631 switch -regexp -- $ccout {
1632 ".*no posix threads support.*" {
1633 set why_msg "missing threads include file"
1634 break
1635 }
1636 ".*cannot open -lpthread.*" {
1637 set why_msg "missing runtime threads library"
1638 }
1639 ".*Can't find library for -lpthread.*" {
1640 set why_msg "missing runtime threads library"
1641 }
1642 {^$} {
1643 pass "successfully compiled posix threads test case"
1644 set built_binfile 1
1645 break
1646 }
1647 }
1648 }
1649 if {!$built_binfile} {
1650 unsupported "Couldn't compile $source: ${why_msg}"
1651 return -1
1652 }
1653 }
1654
1655 # Build a shared library from SOURCES. You must use get_compiler_info
1656 # first.
1657
1658 proc gdb_compile_shlib {sources dest options} {
1659 set obj_options $options
1660
1661 switch -glob [test_compiler_info] {
1662 "xlc-*" {
1663 lappend obj_options "additional_flags=-qpic"
1664 }
1665 "gcc-*" {
1666 if { !([istarget "powerpc*-*-aix*"]
1667 || [istarget "rs6000*-*-aix*"]
1668 || [istarget "*-*-cygwin*"]
1669 || [istarget "*-*-mingw*"]
1670 || [istarget "*-*-pe*"]) } {
1671 lappend obj_options "additional_flags=-fpic"
1672 }
1673 }
1674 default {
1675 switch -glob [istarget] {
1676 "hppa*-hp-hpux*" {
1677 lappend obj_options "additional_flags=+z"
1678 }
1679 "mips-sgi-irix*" {
1680 # Disable SGI compiler's implicit -Dsgi
1681 lappend obj_options "additional_flags=-Usgi"
1682 }
1683 default {
1684 # don't know what the compiler is...
1685 }
1686 }
1687 }
1688 }
1689
1690 set outdir [file dirname $dest]
1691 set objects ""
1692 foreach source $sources {
1693 set sourcebase [file tail $source]
1694 if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
1695 return -1
1696 }
1697 lappend objects ${outdir}/${sourcebase}.o
1698 }
1699
1700 if [istarget "hppa*-*-hpux*"] {
1701 remote_exec build "ld -b ${objects} -o ${dest}"
1702 } else {
1703 set link_options $options
1704 if [test_compiler_info "xlc-*"] {
1705 lappend link_options "additional_flags=-qmkshrobj"
1706 } else {
1707 lappend link_options "additional_flags=-shared"
1708
1709 if { ([istarget "*-*-mingw*"]
1710 || [istarget *-*-cygwin*]
1711 || [istarget *-*-pe*])} {
1712 lappend link_options "additional_flags=-Wl,--out-implib,${dest}.a"
1713 }
1714 }
1715 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
1716 return -1
1717 }
1718 }
1719 }
1720
1721 # This is just like gdb_compile_pthreads, above, except that we always add the
1722 # objc library for compiling Objective-C programs
1723 proc gdb_compile_objc {source dest type options} {
1724 set built_binfile 0
1725 set why_msg "unrecognized error"
1726 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
1727 # This kind of wipes out whatever libs the caller may have
1728 # set. Or maybe theirs will override ours. How infelicitous.
1729 if { $lib == "solaris" } {
1730 set lib "-lpthread -lposix4"
1731 }
1732 if { $lib != "-lobjc" } {
1733 set lib "-lobjc $lib"
1734 }
1735 set options_with_lib [concat $options [list libs=$lib quiet]]
1736 set ccout [gdb_compile $source $dest $type $options_with_lib]
1737 switch -regexp -- $ccout {
1738 ".*no posix threads support.*" {
1739 set why_msg "missing threads include file"
1740 break
1741 }
1742 ".*cannot open -lpthread.*" {
1743 set why_msg "missing runtime threads library"
1744 }
1745 ".*Can't find library for -lpthread.*" {
1746 set why_msg "missing runtime threads library"
1747 }
1748 {^$} {
1749 pass "successfully compiled objc with posix threads test case"
1750 set built_binfile 1
1751 break
1752 }
1753 }
1754 }
1755 if {!$built_binfile} {
1756 unsupported "Couldn't compile $source: ${why_msg}"
1757 return -1
1758 }
1759 }
1760
1761 proc send_gdb { string } {
1762 global suppress_flag;
1763 if { $suppress_flag } {
1764 return "suppressed";
1765 }
1766 return [remote_send host "$string"];
1767 }
1768
1769 #
1770 #
1771
1772 proc gdb_expect { args } {
1773 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
1774 set gtimeout [lindex $args 0];
1775 set expcode [list [lindex $args 1]];
1776 } else {
1777 upvar timeout timeout;
1778
1779 set expcode $args;
1780 if [target_info exists gdb,timeout] {
1781 if [info exists timeout] {
1782 if { $timeout < [target_info gdb,timeout] } {
1783 set gtimeout [target_info gdb,timeout];
1784 } else {
1785 set gtimeout $timeout;
1786 }
1787 } else {
1788 set gtimeout [target_info gdb,timeout];
1789 }
1790 }
1791
1792 if ![info exists gtimeout] {
1793 global timeout;
1794 if [info exists timeout] {
1795 set gtimeout $timeout;
1796 } else {
1797 # Eeeeew.
1798 set gtimeout 60;
1799 }
1800 }
1801 }
1802 global suppress_flag;
1803 global remote_suppress_flag;
1804 if [info exists remote_suppress_flag] {
1805 set old_val $remote_suppress_flag;
1806 }
1807 if [info exists suppress_flag] {
1808 if { $suppress_flag } {
1809 set remote_suppress_flag 1;
1810 }
1811 }
1812 set code [catch \
1813 {uplevel remote_expect host $gtimeout $expcode} string];
1814 if [info exists old_val] {
1815 set remote_suppress_flag $old_val;
1816 } else {
1817 if [info exists remote_suppress_flag] {
1818 unset remote_suppress_flag;
1819 }
1820 }
1821
1822 if {$code == 1} {
1823 global errorInfo errorCode;
1824
1825 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1826 } elseif {$code == 2} {
1827 return -code return $string
1828 } elseif {$code == 3} {
1829 return
1830 } elseif {$code > 4} {
1831 return -code $code $string
1832 }
1833 }
1834
1835 # gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs
1836 #
1837 # Check for long sequence of output by parts.
1838 # MESSAGE: is the test message to be printed with the test success/fail.
1839 # SENTINEL: Is the terminal pattern indicating that output has finished.
1840 # LIST: is the sequence of outputs to match.
1841 # If the sentinel is recognized early, it is considered an error.
1842 #
1843 # Returns:
1844 # 1 if the test failed,
1845 # 0 if the test passes,
1846 # -1 if there was an internal error.
1847 #
1848 proc gdb_expect_list {test sentinel list} {
1849 global gdb_prompt
1850 global suppress_flag
1851 set index 0
1852 set ok 1
1853 if { $suppress_flag } {
1854 set ok 0
1855 unresolved "${test}"
1856 }
1857 while { ${index} < [llength ${list}] } {
1858 set pattern [lindex ${list} ${index}]
1859 set index [expr ${index} + 1]
1860 if { ${index} == [llength ${list}] } {
1861 if { ${ok} } {
1862 gdb_expect {
1863 -re "${pattern}${sentinel}" {
1864 # pass "${test}, pattern ${index} + sentinel"
1865 }
1866 -re "${sentinel}" {
1867 fail "${test} (pattern ${index} + sentinel)"
1868 set ok 0
1869 }
1870 -re ".*A problem internal to GDB has been detected" {
1871 fail "${test} (GDB internal error)"
1872 set ok 0
1873 gdb_internal_error_resync
1874 }
1875 timeout {
1876 fail "${test} (pattern ${index} + sentinel) (timeout)"
1877 set ok 0
1878 }
1879 }
1880 } else {
1881 # unresolved "${test}, pattern ${index} + sentinel"
1882 }
1883 } else {
1884 if { ${ok} } {
1885 gdb_expect {
1886 -re "${pattern}" {
1887 # pass "${test}, pattern ${index}"
1888 }
1889 -re "${sentinel}" {
1890 fail "${test} (pattern ${index})"
1891 set ok 0
1892 }
1893 -re ".*A problem internal to GDB has been detected" {
1894 fail "${test} (GDB internal error)"
1895 set ok 0
1896 gdb_internal_error_resync
1897 }
1898 timeout {
1899 fail "${test} (pattern ${index}) (timeout)"
1900 set ok 0
1901 }
1902 }
1903 } else {
1904 # unresolved "${test}, pattern ${index}"
1905 }
1906 }
1907 }
1908 if { ${ok} } {
1909 pass "${test}"
1910 return 0
1911 } else {
1912 return 1
1913 }
1914 }
1915
1916 #
1917 #
1918 proc gdb_suppress_entire_file { reason } {
1919 global suppress_flag;
1920
1921 warning "$reason\n";
1922 set suppress_flag -1;
1923 }
1924
1925 #
1926 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
1927 # gdb_expect to fail immediately (until the next call to
1928 # gdb_stop_suppressing_tests).
1929 #
1930 proc gdb_suppress_tests { args } {
1931 global suppress_flag;
1932
1933 return; # fnf - disable pending review of results where
1934 # testsuite ran better without this
1935 incr suppress_flag;
1936
1937 if { $suppress_flag == 1 } {
1938 if { [llength $args] > 0 } {
1939 warning "[lindex $args 0]\n";
1940 } else {
1941 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1942 }
1943 }
1944 }
1945
1946 #
1947 # Clear suppress_flag.
1948 #
1949 proc gdb_stop_suppressing_tests { } {
1950 global suppress_flag;
1951
1952 if [info exists suppress_flag] {
1953 if { $suppress_flag > 0 } {
1954 set suppress_flag 0;
1955 clone_output "Tests restarted.\n";
1956 }
1957 } else {
1958 set suppress_flag 0;
1959 }
1960 }
1961
1962 proc gdb_clear_suppressed { } {
1963 global suppress_flag;
1964
1965 set suppress_flag 0;
1966 }
1967
1968 proc gdb_start { } {
1969 default_gdb_start
1970 }
1971
1972 proc gdb_exit { } {
1973 catch default_gdb_exit
1974 }
1975
1976 #
1977 # gdb_load_cmd -- load a file into the debugger.
1978 # ARGS - additional args to load command.
1979 # return a -1 if anything goes wrong.
1980 #
1981 proc gdb_load_cmd { args } {
1982 global gdb_prompt
1983
1984 if [target_info exists gdb_load_timeout] {
1985 set loadtimeout [target_info gdb_load_timeout]
1986 } else {
1987 set loadtimeout 1600
1988 }
1989 send_gdb "load $args\n"
1990 verbose "Timeout is now $timeout seconds" 2
1991 gdb_expect $loadtimeout {
1992 -re "Loading section\[^\r\]*\r\n" {
1993 exp_continue
1994 }
1995 -re "Start address\[\r\]*\r\n" {
1996 exp_continue
1997 }
1998 -re "Transfer rate\[\r\]*\r\n" {
1999 exp_continue
2000 }
2001 -re "Memory access error\[^\r\]*\r\n" {
2002 perror "Failed to load program"
2003 return -1
2004 }
2005 -re "$gdb_prompt $" {
2006 return 0
2007 }
2008 -re "(.*)\r\n$gdb_prompt " {
2009 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
2010 return -1
2011 }
2012 timeout {
2013 perror "Timed out trying to load $arg."
2014 return -1
2015 }
2016 }
2017 return -1
2018 }
2019
2020 # gdb_download
2021 #
2022 # Copy a file to the remote target and return its target filename.
2023 # Schedule the file to be deleted at the end of this test.
2024
2025 proc gdb_download { filename } {
2026 global cleanfiles
2027
2028 set destname [remote_download target $filename]
2029 lappend cleanfiles $destname
2030 return $destname
2031 }
2032
2033 # gdb_load_shlibs LIB...
2034 #
2035 # Copy the listed libraries to the target.
2036
2037 proc gdb_load_shlibs { args } {
2038 if {![is_remote target]} {
2039 return
2040 }
2041
2042 foreach file $args {
2043 gdb_download $file
2044 }
2045
2046 # Even if the target supplies full paths for shared libraries,
2047 # they may not be paths for this system.
2048 gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "" ""
2049 }
2050
2051 #
2052 # gdb_load -- load a file into the debugger.
2053 # Many files in config/*.exp override this procedure.
2054 #
2055 proc gdb_load { arg } {
2056 return [gdb_file_cmd $arg]
2057 }
2058
2059 # gdb_reload -- load a file into the target. Called before "running",
2060 # either the first time or after already starting the program once,
2061 # for remote targets. Most files that override gdb_load should now
2062 # override this instead.
2063
2064 proc gdb_reload { } {
2065 # For the benefit of existing configurations, default to gdb_load.
2066 # Specifying no file defaults to the executable currently being
2067 # debugged.
2068 return [gdb_load ""]
2069 }
2070
2071 proc gdb_continue { function } {
2072 global decimal
2073
2074 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
2075 }
2076
2077 proc default_gdb_init { args } {
2078 global gdb_wrapper_initialized
2079 global cleanfiles
2080
2081 set cleanfiles {}
2082
2083 gdb_clear_suppressed;
2084
2085 # Make sure that the wrapper is rebuilt
2086 # with the appropriate multilib option.
2087 set gdb_wrapper_initialized 0
2088
2089 # Uh, this is lame. Really, really, really lame. But there's this *one*
2090 # testcase that will fail in random places if we don't increase this.
2091 match_max -d 20000
2092
2093 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
2094 if { [llength $args] > 0 } {
2095 global pf_prefix
2096
2097 set file [lindex $args 0];
2098
2099 set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
2100 }
2101 global gdb_prompt;
2102 if [target_info exists gdb_prompt] {
2103 set gdb_prompt [target_info gdb_prompt];
2104 } else {
2105 set gdb_prompt "\\(gdb\\)"
2106 }
2107 }
2108
2109 proc gdb_init { args } {
2110 return [eval default_gdb_init $args];
2111 }
2112
2113 proc gdb_finish { } {
2114 global cleanfiles
2115
2116 # Exit first, so that the files are no longer in use.
2117 gdb_exit
2118
2119 if { [llength $cleanfiles] > 0 } {
2120 eval remote_file target delete $cleanfiles
2121 set cleanfiles {}
2122 }
2123 }
2124
2125 global debug_format
2126 set debug_format "unknown"
2127
2128 # Run the gdb command "info source" and extract the debugging format
2129 # information from the output and save it in debug_format.
2130
2131 proc get_debug_format { } {
2132 global gdb_prompt
2133 global verbose
2134 global expect_out
2135 global debug_format
2136
2137 set debug_format "unknown"
2138 send_gdb "info source\n"
2139 gdb_expect 10 {
2140 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
2141 set debug_format $expect_out(1,string)
2142 verbose "debug format is $debug_format"
2143 return 1;
2144 }
2145 -re "No current source file.\r\n$gdb_prompt $" {
2146 perror "get_debug_format used when no current source file"
2147 return 0;
2148 }
2149 -re "$gdb_prompt $" {
2150 warning "couldn't check debug format (no valid response)."
2151 return 1;
2152 }
2153 timeout {
2154 warning "couldn't check debug format (timed out)."
2155 return 1;
2156 }
2157 }
2158 }
2159
2160 # Return true if FORMAT matches the debug format the current test was
2161 # compiled with. FORMAT is a shell-style globbing pattern; it can use
2162 # `*', `[...]', and so on.
2163 #
2164 # This function depends on variables set by `get_debug_format', above.
2165
2166 proc test_debug_format {format} {
2167 global debug_format
2168
2169 return [expr [string match $format $debug_format] != 0]
2170 }
2171
2172 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
2173 # COFF, stabs, etc). If that format matches the format that the
2174 # current test was compiled with, then the next test is expected to
2175 # fail for any target. Returns 1 if the next test or set of tests is
2176 # expected to fail, 0 otherwise (or if it is unknown). Must have
2177 # previously called get_debug_format.
2178 proc setup_xfail_format { format } {
2179 set ret [test_debug_format $format];
2180
2181 if {$ret} then {
2182 setup_xfail "*-*-*"
2183 }
2184 return $ret;
2185 }
2186
2187 proc gdb_step_for_stub { } {
2188 global gdb_prompt;
2189
2190 if ![target_info exists gdb,use_breakpoint_for_stub] {
2191 if [target_info exists gdb_stub_step_command] {
2192 set command [target_info gdb_stub_step_command];
2193 } else {
2194 set command "step";
2195 }
2196 send_gdb "${command}\n";
2197 set tries 0;
2198 gdb_expect 60 {
2199 -re "(main.* at |.*in .*start).*$gdb_prompt" {
2200 return;
2201 }
2202 -re ".*$gdb_prompt" {
2203 incr tries;
2204 if { $tries == 5 } {
2205 fail "stepping out of breakpoint function";
2206 return;
2207 }
2208 send_gdb "${command}\n";
2209 exp_continue;
2210 }
2211 default {
2212 fail "stepping out of breakpoint function";
2213 return;
2214 }
2215 }
2216 }
2217 send_gdb "where\n";
2218 gdb_expect {
2219 -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
2220 set file $expect_out(1,string);
2221 set linenum [expr $expect_out(2,string) + 1];
2222 set breakplace "${file}:${linenum}";
2223 }
2224 default {}
2225 }
2226 send_gdb "break ${breakplace}\n";
2227 gdb_expect 60 {
2228 -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
2229 set breakpoint $expect_out(1,string);
2230 }
2231 -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
2232 set breakpoint $expect_out(1,string);
2233 }
2234 default {}
2235 }
2236 send_gdb "continue\n";
2237 gdb_expect 60 {
2238 -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
2239 gdb_test "delete $breakpoint" ".*" "";
2240 return;
2241 }
2242 default {}
2243 }
2244 }
2245
2246 # gdb_get_line_number TEXT [FILE]
2247 #
2248 # Search the source file FILE, and return the line number of the
2249 # first line containing TEXT. If no match is found, return -1.
2250 #
2251 # TEXT is a string literal, not a regular expression.
2252 #
2253 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
2254 # specified, and does not start with "/", then it is assumed to be in
2255 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
2256 # by changing the callers and the interface at the same time.
2257 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
2258 # gdb.base/ena-dis-br.exp.
2259 #
2260 # Use this function to keep your test scripts independent of the
2261 # exact line numbering of the source file. Don't write:
2262 #
2263 # send_gdb "break 20"
2264 #
2265 # This means that if anyone ever edits your test's source file,
2266 # your test could break. Instead, put a comment like this on the
2267 # source file line you want to break at:
2268 #
2269 # /* breakpoint spot: frotz.exp: test name */
2270 #
2271 # and then write, in your test script (which we assume is named
2272 # frotz.exp):
2273 #
2274 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
2275 #
2276 # (Yes, Tcl knows how to handle the nested quotes and brackets.
2277 # Try this:
2278 # $ tclsh
2279 # % puts "foo [lindex "bar baz" 1]"
2280 # foo baz
2281 # %
2282 # Tcl is quite clever, for a little stringy language.)
2283 #
2284 # ===
2285 #
2286 # The previous implementation of this procedure used the gdb search command.
2287 # This version is different:
2288 #
2289 # . It works with MI, and it also works when gdb is not running.
2290 #
2291 # . It operates on the build machine, not the host machine.
2292 #
2293 # . For now, this implementation fakes a current directory of
2294 # $srcdir/$subdir to be compatible with the old implementation.
2295 # This will go away eventually and some callers will need to
2296 # be changed.
2297 #
2298 # . The TEXT argument is literal text and matches literally,
2299 # not a regular expression as it was before.
2300 #
2301 # . State changes in gdb, such as changing the current file
2302 # and setting $_, no longer happen.
2303 #
2304 # After a bit of time we can forget about the differences from the
2305 # old implementation.
2306 #
2307 # --chastain 2004-08-05
2308
2309 proc gdb_get_line_number { text { file "" } } {
2310 global srcdir
2311 global subdir
2312 global srcfile
2313
2314 if { "$file" == "" } then {
2315 set file "$srcfile"
2316 }
2317 if { ! [regexp "^/" "$file"] } then {
2318 set file "$srcdir/$subdir/$file"
2319 }
2320
2321 if { [ catch { set fd [open "$file"] } message ] } then {
2322 perror "$message"
2323 return -1
2324 }
2325
2326 set found -1
2327 for { set line 1 } { 1 } { incr line } {
2328 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
2329 perror "$message"
2330 return -1
2331 }
2332 if { $nchar < 0 } then {
2333 break
2334 }
2335 if { [string first "$text" "$body"] >= 0 } then {
2336 set found $line
2337 break
2338 }
2339 }
2340
2341 if { [ catch { close "$fd" } message ] } then {
2342 perror "$message"
2343 return -1
2344 }
2345
2346 return $found
2347 }
2348
2349 # gdb_continue_to_end:
2350 # The case where the target uses stubs has to be handled specially. If a
2351 # stub is used, we set a breakpoint at exit because we cannot rely on
2352 # exit() behavior of a remote target.
2353 #
2354 # mssg is the error message that gets printed.
2355
2356 proc gdb_continue_to_end {mssg} {
2357 if [target_info exists use_gdb_stub] {
2358 if {![gdb_breakpoint "exit"]} {
2359 return 0
2360 }
2361 gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
2362 "continue until exit at $mssg"
2363 } else {
2364 # Continue until we exit. Should not stop again.
2365 # Don't bother to check the output of the program, that may be
2366 # extremely tough for some remote systems.
2367 gdb_test "continue"\
2368 "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
2369 "continue until exit at $mssg"
2370 }
2371 }
2372
2373 proc rerun_to_main {} {
2374 global gdb_prompt
2375
2376 if [target_info exists use_gdb_stub] {
2377 gdb_run_cmd
2378 gdb_expect {
2379 -re ".*Breakpoint .*main .*$gdb_prompt $"\
2380 {pass "rerun to main" ; return 0}
2381 -re "$gdb_prompt $"\
2382 {fail "rerun to main" ; return 0}
2383 timeout {fail "(timeout) rerun to main" ; return 0}
2384 }
2385 } else {
2386 send_gdb "run\n"
2387 gdb_expect {
2388 -re "The program .* has been started already.*y or n. $" {
2389 send_gdb "y\n"
2390 exp_continue
2391 }
2392 -re "Starting program.*$gdb_prompt $"\
2393 {pass "rerun to main" ; return 0}
2394 -re "$gdb_prompt $"\
2395 {fail "rerun to main" ; return 0}
2396 timeout {fail "(timeout) rerun to main" ; return 0}
2397 }
2398 }
2399 }
2400
2401 # Print a message and return true if a test should be skipped
2402 # due to lack of floating point suport.
2403
2404 proc gdb_skip_float_test { msg } {
2405 if [target_info exists gdb,skip_float_tests] {
2406 verbose "Skipping test '$msg': no float tests.";
2407 return 1;
2408 }
2409 return 0;
2410 }
2411
2412 # Print a message and return true if a test should be skipped
2413 # due to lack of stdio support.
2414
2415 proc gdb_skip_stdio_test { msg } {
2416 if [target_info exists gdb,noinferiorio] {
2417 verbose "Skipping test '$msg': no inferior i/o.";
2418 return 1;
2419 }
2420 return 0;
2421 }
2422
2423 proc gdb_skip_bogus_test { msg } {
2424 return 0;
2425 }
2426
2427 # Return true if a test should be skipped due to lack of XML support
2428 # in the host GDB.
2429
2430 proc gdb_skip_xml_test { } {
2431 global gdb_prompt
2432 global srcdir
2433 global xml_missing_cached
2434
2435 if {[info exists xml_missing_cached]} {
2436 return $xml_missing_cached
2437 }
2438
2439 gdb_start
2440 set xml_missing_cached 0
2441 gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" {
2442 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
2443 set xml_missing_cached 1
2444 }
2445 -re ".*$gdb_prompt $" { }
2446 }
2447 gdb_exit
2448 return $xml_missing_cached
2449 }
2450
2451 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
2452 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
2453 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
2454 # the name of a debuginfo only file. This file will be stored in the
2455 # gdb.base/.debug subdirectory.
2456
2457 # Functions for separate debug info testing
2458
2459 # starting with an executable:
2460 # foo --> original executable
2461
2462 # at the end of the process we have:
2463 # foo.stripped --> foo w/o debug info
2464 # .debug/foo.debug --> foo's debug info
2465 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
2466
2467 # Return the name of the file in which we should stor EXEC's separated
2468 # debug info. EXEC contains the full path.
2469 proc separate_debug_filename { exec } {
2470
2471 # In a .debug subdirectory off the same directory where the testcase
2472 # executable is going to be. Something like:
2473 # <your-path>/gdb/testsuite/gdb.base/.debug/blah.debug.
2474 # This is the default location where gdb expects to findi
2475 # the debug info file.
2476
2477 set exec_dir [file dirname $exec]
2478 set exec_file [file tail $exec]
2479 set debug_dir [file join $exec_dir ".debug"]
2480 set debug_file [file join $debug_dir "${exec_file}.debug"]
2481
2482 return $debug_file
2483 }
2484
2485 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
2486 # list of optional flags. The only currently supported flag is no-main,
2487 # which removes the symbol entry for main from the separate debug file.
2488
2489 proc gdb_gnu_strip_debug { dest args } {
2490
2491 # First, make sure that we can do this. This is nasty. We need to
2492 # check for the stabs debug format. To do this we must run gdb on
2493 # the unstripped executable, list 'main' (as to have a default
2494 # source file), use get_debug_format (which does 'info source')
2495 # and then see if the debug info is stabs. If so, we bail out. We
2496 # cannot do this any other way because get_debug_format finds out
2497 # the debug format using gdb itself, and in case of stabs we get
2498 # an error loading the program if it is already stripped. An
2499 # alternative would be to find out the debug info from the flags
2500 # passed to dejagnu when the test is run.
2501
2502 gdb_exit
2503 gdb_start
2504 gdb_load ${dest}
2505 gdb_test "list main" "" ""
2506 get_debug_format
2507 if { [test_debug_format "stabs"] } then {
2508 # The separate debug info feature doesn't work well in
2509 # binutils with stabs. It produces a corrupted debug info
2510 # only file, and gdb chokes on it. It is almost impossible to
2511 # capture the failing message out of gdb, because it happens
2512 # inside gdb_load. At that point any error message is
2513 # intercepted by dejagnu itself, and, because of the error
2514 # threshold, any faulty test result is changed into an
2515 # UNRESOLVED. (see dejagnu/lib/framework.exp)
2516 unsupported "no separate debug info handling with stabs"
2517 return -1
2518 } elseif { [test_debug_format "unknown"] } then {
2519 # gdb doesn't know what the debug format is. We are out of luck here.
2520 unsupported "unknown debugging format"
2521 return -1
2522 }
2523 gdb_exit
2524
2525 set debug_file [separate_debug_filename $dest]
2526 set strip_to_file_program [transform strip]
2527 set objcopy_program [transform objcopy]
2528
2529 # Make sure the directory that will hold the separated debug
2530 # info actually exists.
2531 set debug_dir [file dirname $debug_file]
2532 if {! [file isdirectory $debug_dir]} {
2533 file mkdir $debug_dir
2534 }
2535
2536 set debug_link [file tail $debug_file]
2537 set stripped_file "${dest}.stripped"
2538
2539 # Get rid of the debug info, and store result in stripped_file
2540 # something like gdb/testsuite/gdb.base/blah.stripped.
2541 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
2542 verbose "result is $result"
2543 verbose "output is $output"
2544 if {$result == 1} {
2545 return 1
2546 }
2547
2548 # Get rid of everything but the debug info, and store result in debug_file
2549 # This will be in the .debug subdirectory, see above.
2550 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
2551 verbose "result is $result"
2552 verbose "output is $output"
2553 if {$result == 1} {
2554 return 1
2555 }
2556
2557 # If no-main is passed, strip the symbol for main from the separate
2558 # file. This is to simulate the behavior of elfutils's eu-strip, which
2559 # leaves the symtab in the original file only. There's no way to get
2560 # objcopy or strip to remove the symbol table without also removing the
2561 # debugging sections, so this is as close as we can get.
2562 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
2563 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
2564 verbose "result is $result"
2565 verbose "output is $output"
2566 if {$result == 1} {
2567 return 1
2568 }
2569 file delete "${debug_file}"
2570 file rename "${debug_file}-tmp" "${debug_file}"
2571 }
2572
2573 # Link the two previous output files together, adding the .gnu_debuglink
2574 # section to the stripped_file, containing a pointer to the debug_file,
2575 # save the new file in dest.
2576 # This will be the regular executable filename, in the usual location.
2577 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
2578 verbose "result is $result"
2579 verbose "output is $output"
2580 if {$result == 1} {
2581 return 1
2582 }
2583
2584 return 0
2585 }
2586
2587 # Test the output of GDB_COMMAND matches the pattern obtained
2588 # by concatenating all elements of EXPECTED_LINES. This makes
2589 # it possible to split otherwise very long string into pieces.
2590 # If third argument is not empty, it's used as the name of the
2591 # test to be printed on pass/fail.
2592 proc help_test_raw { gdb_command expected_lines args } {
2593 set message $gdb_command
2594 if [llength $args]>0 then {
2595 set message [lindex $args 0]
2596 }
2597 set expected_output [join $expected_lines ""]
2598 gdb_test "${gdb_command}" "${expected_output}" $message
2599 }
2600
2601 # Test the output of "help COMMNAD_CLASS". EXPECTED_INITIAL_LINES
2602 # are regular expressions that should match the beginning of output,
2603 # before the list of commands in that class. The presence of
2604 # command list and standard epilogue will be tested automatically.
2605 proc test_class_help { command_class expected_initial_lines args } {
2606 set l_stock_body {
2607 "List of commands\:.*\[\r\n\]+"
2608 "Type \"help\" followed by command name for full documentation\.\[\r\n\]+"
2609 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+"
2610 "Command name abbreviations are allowed if unambiguous\."
2611 }
2612 set l_entire_body [concat $expected_initial_lines $l_stock_body]
2613
2614 eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
2615 }
2616
2617 # COMMAND_LIST should have either one element -- command to test, or
2618 # two elements -- abbreviated command to test, and full command the first
2619 # element is abbreviation of.
2620 # The command must be a prefix command. EXPECTED_INITIAL_LINES
2621 # are regular expressions that should match the beginning of output,
2622 # before the list of subcommands. The presence of
2623 # subcommand list and standard epilogue will be tested automatically.
2624 proc test_prefix_command_help { command_list expected_initial_lines args } {
2625 set command [lindex $command_list 0]
2626 if {[llength $command_list]>1} {
2627 set full_command [lindex $command_list 1]
2628 } else {
2629 set full_command $command
2630 }
2631 # Use 'list' and not just {} because we want variables to
2632 # be expanded in this list.
2633 set l_stock_body [list\
2634 "List of $full_command subcommands\:.*\[\r\n\]+"\
2635 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
2636 "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
2637 "Command name abbreviations are allowed if unambiguous\."]
2638 set l_entire_body [concat $expected_initial_lines $l_stock_body]
2639 if {[llength $args]>0} {
2640 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
2641 } else {
2642 help_test_raw "help ${command}" $l_entire_body
2643 }
2644 }
This page took 0.080711 seconds and 5 git commands to generate.