1 # Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
20 # This file was written by Fred Fish. (fnf@cygnus.com)
22 # Generic gdb subroutines that should work for any target. If these
23 # need to be modified for any target, it can be done with a variable
24 # or by passing arguments.
27 if ![info exists GDB] then {
28 if [file exists $base_dir/../gdb] then {
29 set GDB $base_dir/../gdb
31 set GDB [transform gdb]
36 if ![info exists GDBFLAGS] then {
40 # set the prompt if it doesn't exist
42 if ![info exists prompt] then {
43 set prompt "\[(\]gdb\[)\]"
47 if [istarget "sparclite-*-*"] then {
53 if ![info exists noargs] then {
58 # gdb_version -- extract and print the version number of GDB
60 proc default_gdb_version {} {
63 if {[which $GDB] != 0} then {
64 set tmp [exec echo "q" | $GDB -nw $GDBFLAGS]
65 regexp " \[0-9\.\]+" $tmp version
66 clone_output "[which $GDB] version$version -nw $GDBFLAGS \n"
68 warning "$GDB does not exist"
73 # gdb_unload -- unload a file if one is loaded
82 -re "No exec file now.*\r" { exp_continue }
83 -re "No symbol file now.*\r" { exp_continue }
84 -re "A program is being debugged already..*Kill it.*y or n. $"\
86 verbose "\t\tKilling previous program being debugged"
89 -re "Discard symbol table from .*y or n. $" {
95 perror "couldn't unload file in $GDB (timed out)."
101 # Many of the tests depend on setting breakpoints at various places and
102 # running until that breakpoint is reached. At times, we want to start
103 # with a clean-slate with respect to breakpoints, so this utility proc
104 # lets us do this without duplicating this code everywhere.
107 proc delete_breakpoints {} {
110 send "delete breakpoints\n"
112 -re "Delete all breakpoints.*y or n. $" {
116 -re "y\r\n$prompt $" {}
117 -re ".*$prompt $" { # This happens if there were no breakpoints
119 timeout { perror "Delete all breakpoints (timeout)" ; return }
121 send "info breakpoints\n"
123 -re "No breakpoints or watchpoints..*$prompt $" {}
124 -re ".*$prompt $" { perror "breakpoints not deleted" ; return }
125 timeout { perror "info breakpoints (timeout)" ; return }
131 # Generic run command.
133 # The second pattern below matches up to the first newline *only*.
134 # Using ``.*$'' could swallow up output that we attempt to match
137 proc gdb_run_cmd {} {
140 -re "The program .* has been started already.*y or n. $" {
144 -re "Starting program: \[^\n\]*" {}
149 # Set breakpoint at function and run gdb until it breaks there.
150 # Since this is the only breakpoint that will be set, if it stops
151 # at a breakpoint, we will assume it is the one we want. We can't
152 # just compare to "function" because it might be a fully qualified,
153 # single quoted C++ function specifier.
155 proc runto { function } {
161 -re "delete.*Delete all breakpoints.*y or n. $" {
165 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
169 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
172 send "break $function\n"
173 # The first two regexps are what we get with -g, the third is without -g.
175 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$prompt $" {}
176 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
177 -re "Breakpoint \[0-9\]* at .*$prompt $" {}
178 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
179 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
184 # the "at foo.c:36" output we get with -g.
185 # the "in func" output we get without -g.
187 -re "Break.* at .*:$decimal.*$prompt $" {
190 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
194 fail "running to $function"
198 fail "running to $function (timeout)"
205 # runto_main -- ask gdb to run and untill hit break point at main.
206 # if it uses stubs, assuming we hit breakpoint() and just
207 # step out of the function.
221 -re "delete.*Delete all breakpoints.*y or n. $" {
225 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
229 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
233 # if use stubs step out of the breakpoint() function.
235 -re "* at .*$prompt $" {}
236 timeout { fail "single step at breakpoint() (timeout)" ; return 0 }
241 # gdb_test -- send a command to gdb and test the result.
242 # Takes three parameters.
244 # First one is the command to execute,
245 # Second one is the pattern to match for a PASS,
246 # Third one is an optional message to be printed. If this
247 # a null string "", then the pass/fail messages are not printed.
249 # 1 if the test failed,
250 # 0 if the test passes,
251 # -1 if there was an internal error.
253 proc gdb_test { args } {
259 if [llength $args]==3 then {
260 set message [lindex $args 2]
262 set message [lindex $args 0]
264 set command [lindex $args 0]
265 set pattern [lindex $args 1]
268 send_user "Sending \"$command\" to gdb\n"
269 send_user "Looking to match \"$pattern\"\n"
270 send_user "Message is \"$message\"\n"
274 if ![string match $command ""] {
279 -re ".*Ending remote debugging.*$prompt$" {
280 if ![isnative] then {
281 warning "Can`t communicate to remote target."
287 -re "$pattern\r\n$prompt $" {
288 if ![string match "" $message] then {
293 -re "Undefined command:.*$prompt" {
294 perror "Undefined command \"$command\"."
297 -re "Ambiguous command.*$prompt $" {
298 perror "\"$command\" is not a unique command name."
302 if ![string match "" $message] then {
309 perror "Window too small."
311 -re "\[(\]+y or n\[)\]+ " {
313 perror "Got interactive prompt."
316 perror "Process no longer exists"
320 perror "internal buffer is full."
323 if ![string match "" $message] then {
324 fail "(timeout) $message"
332 # Test that a command gives an error. For pass or fail, return
333 # a 1 to indicate that more tests can proceed. However a timeout
334 # is a serious error, generates a special fail message, and causes
335 # a 0 to be returned to indicate that more tests are likely to fail
338 proc test_print_reject { args } {
342 if [llength $args]==2 then {
343 set expectthis [lindex $args 1]
345 set expectthis "should never match this bogus string"
347 set sendthis [lindex $args 0]
349 send_user "Sending \"$sendthis\" to gdb\n"
350 send_user "Looking to match \"$expectthis\"\n"
354 -re ".*A .* in expression.*\\.*$prompt $" {
355 pass "reject $sendthis"
358 -re ".*Invalid syntax in expression.*$prompt $" {
359 pass "reject $sendthis"
362 -re ".*Junk after end of expression.*$prompt $" {
363 pass "reject $sendthis"
366 -re ".*Invalid number.*$prompt $" {
367 pass "reject $sendthis"
370 -re ".*Invalid character constant.*$prompt $" {
371 pass "reject $sendthis"
374 -re ".*No symbol table is loaded.*$prompt $" {
375 pass "reject $sendthis"
378 -re ".*No symbol .* in current context.*$prompt $" {
379 pass "reject $sendthis"
382 -re ".*$expectthis.*$prompt $" {
383 pass "reject $sendthis"
387 fail "reject $sendthis"
391 fail "reject $sendthis (eof or timeout)"
397 # Given an input string, adds backslashes as needed to create a
398 # regexp that will match the string.
400 proc string_to_regexp {str} {
402 regsub -all {[]*+.|()^$\[]} $str {\\&} result
406 # Same as gdb_test, but the second parameter is not a regexp,
407 # but a string that must match exactly.
409 proc gdb_test_exact { args } {
410 set command [lindex $args 0]
411 set pattern [string_to_regexp [lindex $args 1]]
412 if [llength $args]==3 then {
413 set message [lindex $args 2]
417 return [gdb_test $command $pattern $message]
420 proc gdb_reinitialize_dir { subdir } {
425 -re "Reinitialize source path to empty.*" {
428 -re "Source directories searched.*$prompt $" {
431 -re "Source directories searched.*$prompt $" {
432 verbose "Dir set to $subdir"
435 perror "Dir \"$subdir\" failed."
440 perror "Dir \"$subdir\" failed."
445 perror "Dir \"$subdir\" failed."
451 # gdb_exit -- exit the GDB, killing the target program if necessary
453 proc default_gdb_exit {} {
458 verbose "Quitting $GDB $GDBFLAGS"
460 # This used to be 1 for unix-gdb.exp
463 # We used to try to send "quit" to GDB, and wait for it to die.
464 # Dealing with all the cases and errors got pretty hairy. Just close it,
468 # Omitting this probably would cause strange timing-dependent failures.
473 # gdb_load -- load a file into the debugger.
474 # return a -1 if anything goes wrong.
476 proc gdb_file_cmd { arg } {
486 -re "Reading symbols from.*done.*$prompt $" {
487 verbose "\t\tLoaded $arg into the $GDB"
490 -re "has no symbol-table.*$prompt $" {
491 perror "$arg wasn't compiled with \"-g\""
494 -re "A program is being debugged already.*Kill it.*y or n. $" {
496 verbose "\t\tKilling previous program being debugged"
499 -re "Load new symbol table from \".*\".*y or n. $" {
502 -re "Reading symbols from.*done.*$prompt $" {
503 verbose "\t\tLoaded $arg with new symbol table into $GDB"
507 perror "(timeout) Couldn't load $arg, other program already l
513 -re ".*No such file or directory.*$prompt $" {
514 perror "($arg) No such file or directory\n"
518 perror "couldn't load $arg into $GDB."
522 perror "couldn't load $arg into $GDB (timed out)."
526 # This is an attempt to detect a core dump, but seems not to
527 # work. Perhaps we need to match .* followed by eof, in which
528 # expect does not seem to have a way to do that.
529 perror "couldn't load $arg into $GDB (end of file)."
536 # start gdb -- start gdb running, default procedure
538 proc default_gdb_start { } {
545 verbose "Spawning $GDB -nw $GDBFLAGS"
547 if { [which $GDB] == 0 } then {
548 perror "$GDB does not exist."
552 set oldtimeout $timeout
553 set timeout [expr "$timeout + 60"]
554 eval "spawn $GDB -nw $GDBFLAGS"
556 -re ".*\r\n$prompt $" {
557 verbose "GDB initialized."
560 perror "GDB never initialized."
564 perror "(timeout) GDB never initialized."
568 set timeout $oldtimeout
569 # force the height to "unlimited", so no pagers get used
570 send "set height 0\n"
573 verbose "Setting height to 0." 2
576 warning "Couldn't set the height to 0."
579 # force the width to "unlimited", so no wraparound occurs
583 verbose "Seting width to 0." 2
586 warning "Couldn't set the width to 0."
592 # FIXME: this is a copy of the new library procedure, but it's here too
593 # till the new dejagnu gets installed everywhere. I'd hate to break the
597 if ![info exists argv0] then {
598 proc exp_continue { } {
603 proc skip_chill_tests {} {
604 # For crosses, the CHILL runtime doesn't build because it can't find
605 # setjmp.h, stdio.h, etc.
606 # For AIX (as of 16 Mar 95), (a) there is no language code for
607 # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
608 # does not get along with AIX's too-clever linker.
609 # On Solaris, static constructors are broken.
610 return {![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-solaris2*"]}