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 set GDB [findfile $base_dir/../gdb $base_dir/../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 GDB
45 proc default_gdb_version {} {
48 if {[which $GDB] != 0} then {
49 set tmp [exec echo "q" | $GDB -nw]
50 regexp " \[0-9\.\]+" $tmp version
51 clone_output "[which $GDB] version$version -nw $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 # Generic run command.
118 # The second pattern below matches up to the first newline *only*.
119 # Using ``.*$'' could swallow up output that we attempt to match
122 proc gdb_run_cmd {} {
125 -re "The program .* has been started already.*y or n. $" {
129 -re "Starting program: \[^\n\]*" {}
134 # Set breakpoint at function and run gdb until it breaks there.
135 # Since this is the only breakpoint that will be set, if it stops
136 # at a breakpoint, we will assume it is the one we want. We can't
137 # just compare to "function" because it might be a fully qualified,
138 # single quoted C++ function specifier.
140 proc runto { function } {
146 -re "delete.*Delete all breakpoints.*y or n. $" {
150 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
154 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
157 send "break $function\n"
158 # The first two regexps are what we get with -g, the third is without -g.
160 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$prompt $" {}
161 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
162 -re "Breakpoint \[0-9\]* at .*$prompt $" {}
163 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
164 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
169 # the "at foo.c:36" output we get with -g.
170 # the "in func" output we get without -g.
172 -re "Break.* at .*:$decimal.*$prompt $" {
175 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
179 fail "running to $function"
183 fail "running to $function (timeout)"
190 # gdb_test -- send a command to gdb and test the result.
191 # Takes three parameters.
193 # First one is the command to execute,
194 # Second one is the pattern to match for a PASS,
195 # Third one is an optional message to be printed. If this
196 # a null string "", then the pass/fail messages are not printed.
198 # 1 if the test failed,
199 # 0 if the test passes,
200 # -1 if there was an internal error.
202 proc gdb_test { args } {
208 if [llength $args]==3 then {
209 set message [lindex $args 2]
211 set message [lindex $args 0]
213 set command [lindex $args 0]
214 set pattern [lindex $args 1]
217 send_user "Sending \"$command\" to gdb\n"
218 send_user "Looking to match \"$pattern\"\n"
219 send_user "Message is \"$message\"\n"
224 if ![string match $command ""] {
225 # trap the send so any problems don't crash things
226 catch "send \"$command\n\"" errmess
227 if [string match "write.spawn_id=\[0-9\]+.:" $errmess] then {
228 perror "sent \"$command\" got expect error \"$errmess\""
236 -re ".*Ending remote debugging.*$prompt$" {
237 if ![isnative] then {
238 warning "Can`t communicate to remote target."
244 -re "$pattern.*$prompt $" {
245 if ![string match "" $message] then {
250 -re "Undefined command:.*$prompt" {
251 perror "Undefined command \"$command\"."
254 -re "Ambiguous command.*$prompt $" {
255 perror "\"$command\" is not a unique command name."
259 if ![string match "" $message] then {
266 perror "Window too small."
268 -re "\[(\]+y or n\[)\]+ " {
270 perror "Got interactive prompt."
273 perror "Process no longer exists"
277 perror "internal buffer is full."
280 if ![string match "" $message] then {
281 fail "(timeout) $message"
289 # Testing printing of a specific value. For passes and fails, return
290 # a 1 to indicate that more tests can proceed. However a timeout
291 # is a serious error, generates a special fail message, and causes
292 # a 0 to be returned to indicate that more tests are likely to fail
297 # First one is string to send to gdb
298 # Second one is string to match gdb result to
299 # Third one is an optional message to be printed
301 # This differs from gdb_test in a few ways: (1) no catch on the send (there is
302 # no reason for this to be different from gdb_test but I think the lack of
303 # catch is correct), (2) it tests for the " =" (that could easily be moved
304 # to the callers, (3) the pattern must be followed by \r\n and the prompt,
305 # not other garbage as in gdb_test (this feature seems kind of worthwhile).
307 proc test_print_accept { args } {
311 if [llength $args]==3 then {
312 set message [lindex $args 2]
314 set message [lindex $args 0]
316 set sendthis [lindex $args 0]
317 set expectthis [lindex $args 1]
319 send_user "Sending \"$sendthis\" to gdb\n"
320 send_user "Looking to match \"$expectthis\"\n"
321 send_user "Message is \"$message\"\n"
325 -re ".* = $expectthis\r\n$prompt $" {
326 if ![string match "" $message] then {
327 pass "$sendthis ($message)"
334 if ![string match "" $message] then {
335 fail "$sendthis ($message)"
342 fail "$sendthis (timeout)"
348 # Testing printing of a specific value. For pass or fail, return
349 # a 1 to indicate that more tests can proceed. However a timeout
350 # is a serious error, generates a special fail message, and causes
351 # a 0 to be returned to indicate that more tests are likely to fail
354 proc test_print_reject { args } {
358 if [llength $args]==2 then {
359 set expectthis [lindex $args 1]
361 set expectthis "should never match this bogus string"
363 set sendthis [lindex $args 0]
365 send_user "Sending \"$sendthis\" to gdb\n"
366 send_user "Looking to match \"$expectthis\"\n"
370 -re ".*A .* in expression.*\\.*$prompt $" {
371 pass "reject $sendthis"
374 -re ".*Invalid syntax in expression.*$prompt $" {
375 pass "reject $sendthis"
378 -re ".*Junk after end of expression.*$prompt $" {
379 pass "reject $sendthis"
382 -re ".*Invalid number.*$prompt $" {
383 pass "reject $sendthis"
386 -re ".*Invalid character constant.*$prompt $" {
387 pass "reject $sendthis"
390 -re ".*No symbol table is loaded.*$prompt $" {
391 pass "reject $sendthis"
394 -re ".*No symbol .* in current context.*$prompt $" {
395 pass "reject $sendthis"
398 -re ".*$expectthis.*$prompt $" {
399 pass "reject $sendthis"
403 fail "reject $sendthis"
407 fail "reject $sendthis (eof or timeout)"
413 # Given an input string, adds backslashes as needed to create a
414 # regexp that will match the string.
416 proc string_to_regexp {str} {
418 regsub -all {[]*+.|()^$\[]} $str {\\&} result
422 # Same as gdb_test, but the second parameter is not a regexp,
423 # but a string that must match exactly.
425 proc gdb_test_exact { args } {
426 set command [lindex $args 0]
427 set pattern [string_to_regexp [lindex $args 1]]
428 if [llength $args]==3 then {
429 set message [lindex $args 2]
433 return [gdb_test $command $pattern $message]
436 proc gdb_reinitialize_dir { subdir } {
441 -re "Reinitialize source path to empty.*" {
444 -re "Source directories searched.*$prompt $" {
447 -re "Source directories searched.*$prompt $" {
448 verbose "Dir set to $subdir"
451 perror "Dir \"$subdir\" failed."
456 perror "Dir \"$subdir\" failed."
461 perror "Dir \"$subdir\" failed."
467 # gdb_exit -- exit the GDB, killing the target program if necessary
469 proc default_gdb_exit {} {
474 verbose "Quitting $GDB $GDBFLAGS"
476 # This used to be 1 for unix-gdb.exp
479 # We used to try to send "quit" to GDB, and wait for it to die.
480 # Dealing with all the cases and errors got pretty hairy. Just close it,
484 # Omitting this probably would cause strange timing-dependent failures.
489 # gdb_load -- load a file into the debugger.
490 # return a -1 if anything goes wrong.
492 proc gdb_file_cmd { arg } {
502 -re "Reading symbols from.*done.*$prompt $" {
503 verbose "\t\tLoaded $arg into the $GDB"
506 -re "has no symbol-table.*$prompt $" {
507 perror "$arg wasn't compiled with \"-g\""
510 -re "A program is being debugged already.*Kill it.*y or n. $" {
512 verbose "\t\tKilling previous program being debugged"
515 -re "Load new symbol table from \".*\".*y or n. $" {
518 -re "Reading symbols from.*done.*$prompt $" {
519 verbose "\t\tLoaded $arg with new symbol table into $GDB"
523 perror "(timeout) Couldn't load $arg, other program already l
529 -re ".*No such file or directory.*$prompt $" {
530 perror "($arg) No such file or directory\n"
534 perror "couldn't load $arg into $GDB."
538 perror "couldn't load $arg into $GDB (timed out)."
542 # This is an attempt to detect a core dump, but seems not to
543 # work. Perhaps we need to match .* followed by eof, in which
544 # expect does not seem to have a way to do that.
545 perror "couldn't load $arg into $GDB (end of file)."
552 # start gdb -- start gdb running, default procedure
554 proc default_gdb_start { } {
561 verbose "Spawning $GDB -nw $GDBFLAGS"
563 if { [which $GDB] == 0 } then {
564 perror "$GDB does not exist."
568 set oldtimeout $timeout
569 set timeout [expr "$timeout + 60"]
570 eval "spawn $GDB -nw $GDBFLAGS"
572 -re ".*\r\n$prompt $" {
573 verbose "GDB initialized."
576 perror "GDB never initialized."
580 perror "(timeout) GDB never initialized."
584 set timeout $oldtimeout
585 # force the height to "unlimited", so no pagers get used
586 send "set height 0\n"
589 verbose "Setting height to 0." 2
592 warning "Couldn't set the height to 0."
595 # force the width to "unlimited", so no wraparound occurs
599 verbose "Seting width to 0." 2
602 warning "Couldn't set the width to 0."
608 # FIXME: this is a copy of the new library procedure, but it's here too
609 # till the new dejagnu gets installed everywhere. I'd hate to break the
613 if ![info exists argv0] then {
614 proc exp_continue { } {