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 # gdb_version -- extract and print the version number of GDB
49 proc default_gdb_version {} {
52 if {[which $GDB] != 0} then {
53 set tmp [exec echo "q" | $GDB -nw $GDBFLAGS]
54 regexp " \[0-9\.\]+" $tmp version
55 clone_output "[which $GDB] version$version -nw $GDBFLAGS \n"
57 warning "$GDB does not exist"
62 # gdb_unload -- unload a file if one is loaded
71 -re "No exec file now.*\r" { exp_continue }
72 -re "No symbol file now.*\r" { exp_continue }
73 -re "A program is being debugged already..*Kill it.*y or n. $"\
75 verbose "\t\tKilling previous program being debugged"
78 -re "Discard symbol table from .*y or n. $" {
84 perror "couldn't unload file in $GDB (timed out)."
90 # Many of the tests depend on setting breakpoints at various places and
91 # running until that breakpoint is reached. At times, we want to start
92 # with a clean-slate with respect to breakpoints, so this utility proc
93 # lets us do this without duplicating this code everywhere.
96 proc delete_breakpoints {} {
99 send "delete breakpoints\n"
101 -re "Delete all breakpoints.*y or n. $" {
105 -re "y\r\n$prompt $" {}
106 -re ".*$prompt $" { # This happens if there were no breakpoints
108 timeout { perror "Delete all breakpoints (timeout)" ; return }
110 send "info breakpoints\n"
112 -re "No breakpoints or watchpoints..*$prompt $" {}
113 -re ".*$prompt $" { perror "breakpoints not deleted" ; return }
114 timeout { perror "info breakpoints (timeout)" ; return }
120 # Generic run command.
122 # The second pattern below matches up to the first newline *only*.
123 # Using ``.*$'' could swallow up output that we attempt to match
126 proc gdb_run_cmd {} {
129 -re "The program .* has been started already.*y or n. $" {
133 -re "Starting program: \[^\n\]*" {}
138 # Set breakpoint at function and run gdb until it breaks there.
139 # Since this is the only breakpoint that will be set, if it stops
140 # at a breakpoint, we will assume it is the one we want. We can't
141 # just compare to "function" because it might be a fully qualified,
142 # single quoted C++ function specifier.
144 proc runto { function } {
150 -re "delete.*Delete all breakpoints.*y or n. $" {
154 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
158 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
161 send "break $function\n"
162 # The first two regexps are what we get with -g, the third is without -g.
164 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$prompt $" {}
165 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
166 -re "Breakpoint \[0-9\]* at .*$prompt $" {}
167 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
168 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
173 # the "at foo.c:36" output we get with -g.
174 # the "in func" output we get without -g.
176 -re "Break.* at .*:$decimal.*$prompt $" {
179 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
183 fail "running to $function"
187 fail "running to $function (timeout)"
194 # gdb_test -- send a command to gdb and test the result.
195 # Takes three parameters.
197 # First one is the command to execute,
198 # Second one is the pattern to match for a PASS,
199 # Third one is an optional message to be printed. If this
200 # a null string "", then the pass/fail messages are not printed.
202 # 1 if the test failed,
203 # 0 if the test passes,
204 # -1 if there was an internal error.
206 proc gdb_test { args } {
212 if [llength $args]==3 then {
213 set message [lindex $args 2]
215 set message [lindex $args 0]
217 set command [lindex $args 0]
218 set pattern [lindex $args 1]
221 send_user "Sending \"$command\" to gdb\n"
222 send_user "Looking to match \"$pattern\"\n"
223 send_user "Message is \"$message\"\n"
227 if ![string match $command ""] {
232 -re ".*Ending remote debugging.*$prompt$" {
233 if ![isnative] then {
234 warning "Can`t communicate to remote target."
240 -re "$pattern\r\n$prompt $" {
241 if ![string match "" $message] then {
246 -re "Undefined command:.*$prompt" {
247 perror "Undefined command \"$command\"."
250 -re "Ambiguous command.*$prompt $" {
251 perror "\"$command\" is not a unique command name."
255 if ![string match "" $message] then {
262 perror "Window too small."
264 -re "\[(\]+y or n\[)\]+ " {
266 perror "Got interactive prompt."
269 perror "Process no longer exists"
273 perror "internal buffer is full."
276 if ![string match "" $message] then {
277 fail "(timeout) $message"
285 # Testing printing of a specific value. For passes and fails, return
286 # a 1 to indicate that more tests can proceed. However a timeout
287 # is a serious error, generates a special fail message, and causes
288 # a 0 to be returned to indicate that more tests are likely to fail
293 # First one is string to send to gdb
294 # Second one is string to match gdb result to
295 # Third one is an optional message to be printed
297 # This differs from gdb_test in a few ways: (1) handling of empty
298 # sendthis (I suspect test_print_accept callers never use this), (2)
299 # it tests for the " =" (that could easily be moved to the callers.
301 proc test_print_accept { args } {
305 if [llength $args]==3 then {
306 set message [lindex $args 2]
308 set message [lindex $args 0]
310 set sendthis [lindex $args 0]
311 set expectthis [lindex $args 1]
313 send_user "Sending \"$sendthis\" to gdb\n"
314 send_user "Looking to match \"$expectthis\"\n"
315 send_user "Message is \"$message\"\n"
319 -re ".* = $expectthis\r\n$prompt $" {
320 if ![string match "" $message] then {
321 pass "$sendthis ($message)"
328 if ![string match "" $message] then {
329 fail "$sendthis ($message)"
336 fail "$sendthis (timeout)"
342 # Testing printing of a specific value. For pass or fail, return
343 # a 1 to indicate that more tests can proceed. However a timeout
344 # is a serious error, generates a special fail message, and causes
345 # a 0 to be returned to indicate that more tests are likely to fail
348 proc test_print_reject { args } {
352 if [llength $args]==2 then {
353 set expectthis [lindex $args 1]
355 set expectthis "should never match this bogus string"
357 set sendthis [lindex $args 0]
359 send_user "Sending \"$sendthis\" to gdb\n"
360 send_user "Looking to match \"$expectthis\"\n"
364 -re ".*A .* in expression.*\\.*$prompt $" {
365 pass "reject $sendthis"
368 -re ".*Invalid syntax in expression.*$prompt $" {
369 pass "reject $sendthis"
372 -re ".*Junk after end of expression.*$prompt $" {
373 pass "reject $sendthis"
376 -re ".*Invalid number.*$prompt $" {
377 pass "reject $sendthis"
380 -re ".*Invalid character constant.*$prompt $" {
381 pass "reject $sendthis"
384 -re ".*No symbol table is loaded.*$prompt $" {
385 pass "reject $sendthis"
388 -re ".*No symbol .* in current context.*$prompt $" {
389 pass "reject $sendthis"
392 -re ".*$expectthis.*$prompt $" {
393 pass "reject $sendthis"
397 fail "reject $sendthis"
401 fail "reject $sendthis (eof or timeout)"
407 # Given an input string, adds backslashes as needed to create a
408 # regexp that will match the string.
410 proc string_to_regexp {str} {
412 regsub -all {[]*+.|()^$\[]} $str {\\&} result
416 # Same as gdb_test, but the second parameter is not a regexp,
417 # but a string that must match exactly.
419 proc gdb_test_exact { args } {
420 set command [lindex $args 0]
421 set pattern [string_to_regexp [lindex $args 1]]
422 if [llength $args]==3 then {
423 set message [lindex $args 2]
427 return [gdb_test $command $pattern $message]
430 proc gdb_reinitialize_dir { subdir } {
435 -re "Reinitialize source path to empty.*" {
438 -re "Source directories searched.*$prompt $" {
441 -re "Source directories searched.*$prompt $" {
442 verbose "Dir set to $subdir"
445 perror "Dir \"$subdir\" failed."
450 perror "Dir \"$subdir\" failed."
455 perror "Dir \"$subdir\" failed."
461 # gdb_exit -- exit the GDB, killing the target program if necessary
463 proc default_gdb_exit {} {
468 verbose "Quitting $GDB $GDBFLAGS"
470 # This used to be 1 for unix-gdb.exp
473 # We used to try to send "quit" to GDB, and wait for it to die.
474 # Dealing with all the cases and errors got pretty hairy. Just close it,
478 # Omitting this probably would cause strange timing-dependent failures.
483 # gdb_load -- load a file into the debugger.
484 # return a -1 if anything goes wrong.
486 proc gdb_file_cmd { arg } {
496 -re "Reading symbols from.*done.*$prompt $" {
497 verbose "\t\tLoaded $arg into the $GDB"
500 -re "has no symbol-table.*$prompt $" {
501 perror "$arg wasn't compiled with \"-g\""
504 -re "A program is being debugged already.*Kill it.*y or n. $" {
506 verbose "\t\tKilling previous program being debugged"
509 -re "Load new symbol table from \".*\".*y or n. $" {
512 -re "Reading symbols from.*done.*$prompt $" {
513 verbose "\t\tLoaded $arg with new symbol table into $GDB"
517 perror "(timeout) Couldn't load $arg, other program already l
523 -re ".*No such file or directory.*$prompt $" {
524 perror "($arg) No such file or directory\n"
528 perror "couldn't load $arg into $GDB."
532 perror "couldn't load $arg into $GDB (timed out)."
536 # This is an attempt to detect a core dump, but seems not to
537 # work. Perhaps we need to match .* followed by eof, in which
538 # expect does not seem to have a way to do that.
539 perror "couldn't load $arg into $GDB (end of file)."
546 # start gdb -- start gdb running, default procedure
548 proc default_gdb_start { } {
555 verbose "Spawning $GDB -nw $GDBFLAGS"
557 if { [which $GDB] == 0 } then {
558 perror "$GDB does not exist."
562 set oldtimeout $timeout
563 set timeout [expr "$timeout + 60"]
564 eval "spawn $GDB -nw $GDBFLAGS"
566 -re ".*\r\n$prompt $" {
567 verbose "GDB initialized."
570 perror "GDB never initialized."
574 perror "(timeout) GDB never initialized."
578 set timeout $oldtimeout
579 # force the height to "unlimited", so no pagers get used
580 send "set height 0\n"
583 verbose "Setting height to 0." 2
586 warning "Couldn't set the height to 0."
589 # force the width to "unlimited", so no wraparound occurs
593 verbose "Seting width to 0." 2
596 warning "Couldn't set the width to 0."
602 # FIXME: this is a copy of the new library procedure, but it's here too
603 # till the new dejagnu gets installed everywhere. I'd hate to break the
607 if ![info exists argv0] then {
608 proc exp_continue { } {
613 proc skip_chill_tests {} {