1 # Copyright (C) 1992, 1994 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 set GDB [findfile $base_dir/../gdb "gdb" [transform gdb ]]
32 if ![info exists GDBFLAGS] then {
36 # set the prompt if it doesn't exist
38 if ![info exists prompt] then {
39 set prompt "\[(\]gdb\[)\]"
43 # gdb_version -- extract and print the version number of gcc
45 proc default_gdb_version {} {
48 if {[which $GDB] != 0} then {
49 set tmp [exec echo "q" | $GDB]
50 regexp " \[0-9\.\]+" $tmp version
51 clone_output "[which $GDB] version$version $GDBFLAGS\n"
53 warning "$GDB does not exist"
58 # gdb_unload -- unload a file if one is loaded
67 -re "No exec file now.*\r" { exp_continue }
68 -re "No symbol file now.*\r" { exp_continue }
69 -re "A program is being debugged already..*Kill it.*y or n. $"\
71 verbose "\t\tKilling previous program being debugged"
74 -re "Discard symbol table from .*y or n. $" {
80 perror "couldn't unload file in $GDB (timed out)."
86 # Many of the tests depend on setting breakpoints at various places and
87 # running until that breakpoint is reached. At times, we want to start
88 # with a clean-slate with respect to breakpoints, so this utility proc
89 # lets us do this without duplicating this code everywhere.
92 proc delete_breakpoints {} {
95 send "delete breakpoints\n"
97 -re "Delete all breakpoints.*y or n. $" {
101 -re "y\r\n$prompt $" {}
102 -re ".*$prompt $" { # This happens if there were no breakpoints
104 timeout { perror "Delete all breakpoints (timeout)" ; return }
106 send "info breakpoints\n"
108 -re "No breakpoints or watchpoints..*$prompt $" {}
109 -re ".*$prompt $" { perror "breakpoints not deleted" ; return }
110 timeout { perror "info breakpoints (timeout)" ; return }
116 # Set breakpoint at function and run gdb until it breaks there.
117 # Since this is the only breakpoint that will be set, if it stops
118 # at a breakpoint, we will assume it is the one we want. We can't
119 # just compare to "function" because it might be a fully qualified,
120 # single quoted C++ function specifier.
123 proc runto { function } {
129 -re "delete.*Delete all breakpoints.*y or n. $" {
133 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
137 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
140 send "break $function\n"
141 # The first regexp is what we get with -g, the second without -g.
143 -re "Break.* at .*: file .*, line $decimal.\r\n$prompt $" {}
144 -re "Breakpoint \[0-9\]* at 0x\[0-9a-f\]*.*$prompt $" {}
145 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
146 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
150 # the "at foo.c:36" output we get with -g.
151 # the "in func" output we get without -g.
153 -re "The program .* has been started already.*y or n. $" {
157 -re "Starting.*Break.* at .*:$decimal.*$prompt $" { return 1 }
158 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
161 -re "$prompt $" { fail "running to $function" ; return 0 }
162 timeout { fail "running to $function (timeout)" ; return 0 }
167 # gdb_test -- send a command to gdb and test the result.
168 # Takes three parameters.
170 # First one is the command to execute,
171 # Second one is the pattern to match for a PASS,
172 # Third one is an optional message to be printed. If this
173 # a null string "", then the pass/fail messages are not printed.
175 # 1 if the test failed,
176 # 0 if the test passes,
177 # -1 if there was an internal error.
179 proc gdb_test { args } {
185 if [llength $args]==3 then {
186 set message [lindex $args 2]
188 set message [lindex $args 0]
190 set command [lindex $args 0]
191 set pattern [lindex $args 1]
194 send_user "Sending \"$command\" to gdb\n"
195 send_user "Looking to match \"$pattern\"\n"
196 send_user "Message is \"$message\"\n"
201 # trap the send so any problems don't crash things
202 catch "send \"$command\n\"" errmess
203 if [string match "write.spawn_id=\[0-9\]+.:" $errmess] then {
204 perror "sent \"$command\" got expect error \"$errmess\""
211 -re ".*Ending remote debugging.*$prompt$" {
212 if ![isnative] then {
213 warning "Can`t communicate to remote target."
219 -re "$pattern.*$prompt $" {
220 if ![string match "" $message] then {
225 -re "Undefined command:.*$prompt" {
226 perror "Undefined command \"$command\"."
229 -re "Ambiguous command.*$prompt $" {
230 perror "\"$command\" is not a unique command name."
234 if ![string match "" $message] then {
241 perror "Window too small."
243 -re "\[(\]+y or n\[)\]+ " {
245 perror "Got interactive prompt."
248 perror "Process no longer exists"
252 perror "internal buffer is full."
255 fail "(timeout) $message"
262 # Given an input string, adds backslashes as needed to create a
263 # regexp that will match the string.
264 proc string_to_regexp {str} {
265 regsub -all {[]*+.|()^$[]} $str {\\&} result
269 # Same as gdb_test, but the second parameter is not a regexp,
270 # but a string that must match exactly.
272 proc gdb_test_exact { args } {
273 set command [lindex $args 0]
274 set pattern [string_to_regexp [lindex $args 1]]
275 if [llength $args]==3 then {
276 set message [lindex $args 2]
280 return [gdb_test $command $pattern $message]
283 proc gdb_reinitialize_dir { subdir } {
288 -re "Reinitialize source path to empty.*" {
291 -re "Source directories searched.*$prompt $" {
294 -re "Source directories searched.*$prompt $" {
295 verbose "Dir set to $subdir"
298 perror "Dir \"$subdir\" failed."
303 perror "Dir \"$subdir\" failed."
308 perror "Dir \"$subdir\" failed."
314 # gdb_exit -- exit the GDB, killing the target program if necessary
316 proc default_gdb_exit {} {
321 verbose "Quitting $GDB $GDBFLAGS"
323 # This used to be 1 for unix-gdb.exp
326 # We used to try to send "quit" to GDB, and wait for it to die.
327 # Dealing with all the cases and errors got pretty hairy. Just close it,
331 # Omitting this probably would cause strange timing-dependent failures.
336 # gdb_load -- load a file into the debugger.
337 # return a -1 if anything goes wrong.
339 proc gdb_file_cmd { arg } {
349 -re "Reading symbols from.*done.*$prompt $" {
350 verbose "\t\tLoaded $arg into the $GDB"
353 -re "has no symbol-table.*$prompt $" {
354 perror "$arg wasn't compiled with \"-g\""
357 -re "A program is being debugged already.*Kill it.*y or n. $" {
359 verbose "\t\tKilling previous program being debugged"
362 -re "Load new symbol table from \".*\".*y or n. $" {
365 -re "Reading symbols from.*done.*$prompt $" {
366 verbose "\t\tLoaded $arg with new symbol table into $GDB"
370 perror "(timeout) Couldn't load $arg, other program already l
376 -re ".*No such file or directory.*$prompt $" {
377 perror "($arg) No such file or directory\n"
381 perror "couldn't load $arg into $GDB."
385 perror "couldn't load $arg into $GDB (timed out)."
389 # This is an attempt to detect a core dump, but seems not to
390 # work. Perhaps we need to match .* followed by eof, in which
391 # expect does not seem to have a way to do that.
392 perror "couldn't load $arg into $GDB (end of file)."
399 # start gdb -- start gdb running, default procedure
401 proc default_gdb_start { } {
408 verbose "Spawning $GDB $GDBFLAGS"
410 set oldtimeout $timeout
411 set timeout [expr "$timeout + 60"]
412 if [ llength $GDBFLAGS ] then {
413 if {[which $GDB] != 0} then {
416 perror "$GDB does not exist."
420 if {[which $GDB] != 0} then {
423 perror "$GDB does not exist."
428 -re ".*\r\n$prompt $" {
429 verbose "GDB initialized for native mode"
432 perror "GDB never initialized."
436 perror "(timeout) GDB never initialized."
440 set timeout $oldtimeout
441 # force the height to "unlimited", so no pagers get used
442 send "set height 0\n"
445 verbose "Setting height to 0." 2
448 warning "Couldn't set the height to 0."
451 # force the width to "unlimited", so no wraparound occurs
455 verbose "Seting width to 0." 2
458 warning "Couldn't set the width to 0."
464 # FIXME: this is a copy of the new library procedure, but it's here too
465 # till the new dejagnu gets installed everywhere. I'd hate to break the
469 if ![info exists argv0] then {
470 proc exp_continue { } {