New implementation of gdb_run_cmd which fixes some subtle bugs.
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
CommitLineData
0fba9aa2 1# Copyright (C) 1992, 1994 Free Software Foundation, Inc.
19fa4a0a
MW
2
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.
7#
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.
12#
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. */
16
17# Please email any bugs, comments, and/or additions to this file to:
c79f61db 18# bug-gdb@prep.ai.mit.edu
19fa4a0a
MW
19
20# This file was written by Fred Fish. (fnf@cygnus.com)
21
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.
25
9bcc6c3f
RS
26global GDB
27if ![info exists GDB] then {
3e304ddf 28 set GDB [findfile $base_dir/../gdb $base_dir/../gdb [transform gdb ]]
9bcc6c3f
RS
29}
30
31global GDBFLAGS
85174909
RS
32if ![info exists GDBFLAGS] then {
33 set GDBFLAGS ""
34}
35
90fba5fa
BC
36# set the prompt if it doesn't exist
37global prompt
38if ![info exists prompt] then {
39 set prompt "\[(\]gdb\[)\]"
40}
41
5019a275
RS
42#
43# gdb_version -- extract and print the version number of gcc
44#
45proc default_gdb_version {} {
46 global GDB
47 global GDBFLAGS
48 if {[which $GDB] != 0} then {
49 set tmp [exec echo "q" | $GDB]
85174909
RS
50 regexp " \[0-9\.\]+" $tmp version
51 clone_output "[which $GDB] version$version $GDBFLAGS\n"
5019a275
RS
52 } else {
53 warning "$GDB does not exist"
54 }
55}
56
19fa4a0a
MW
57#
58# gdb_unload -- unload a file if one is loaded
59#
60
61proc gdb_unload {} {
62 global verbose
63 global GDB
64 global prompt
65 send "file\n"
66 expect {
9bcc6c3f
RS
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. $"\
19fa4a0a 70 { send "y\n"
85174909 71 verbose "\t\tKilling previous program being debugged"
9bcc6c3f 72 exp_continue
19fa4a0a 73 }
9bcc6c3f 74 -re "Discard symbol table from .*y or n. $" {
19fa4a0a 75 send "y\n"
9bcc6c3f 76 exp_continue
19fa4a0a
MW
77 }
78 -re "$prompt $" {}
79 timeout {
85174909 80 perror "couldn't unload file in $GDB (timed out)."
c79f61db 81 return -1
19fa4a0a
MW
82 }
83 }
84}
85
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.
90#
91
92proc delete_breakpoints {} {
93 global prompt
94
95 send "delete breakpoints\n"
96 expect {
9bcc6c3f 97 -re "Delete all breakpoints.*y or n. $" {
19fa4a0a 98 send "y\n"
9bcc6c3f 99 exp_continue
19fa4a0a
MW
100 }
101 -re "y\r\n$prompt $" {}
31711c69
JK
102 -re ".*$prompt $" { # This happens if there were no breakpoints
103 }
8f07e537 104 timeout { perror "Delete all breakpoints (timeout)" ; return }
19fa4a0a
MW
105 }
106 send "info breakpoints\n"
107 expect {
108 -re "No breakpoints or watchpoints..*$prompt $" {}
9bcc6c3f 109 -re ".*$prompt $" { perror "breakpoints not deleted" ; return }
8f07e537 110 timeout { perror "info breakpoints (timeout)" ; return }
19fa4a0a
MW
111 }
112}
113
114
115#
809943cf
C
116#
117#
118proc gdb_run_cmd {} {
119 send "run\n"
120 expect {
121 -re "The program .* has been started already.*y or n. $" {
122 send "y\n"
123 exp_continue
124 }
4f2ec2ee
C
125
126 -re "Starting program: .*$" {}
809943cf
C
127 }
128}
129
130
19fa4a0a
MW
131# Set breakpoint at function and run gdb until it breaks there.
132# Since this is the only breakpoint that will be set, if it stops
133# at a breakpoint, we will assume it is the one we want. We can't
134# just compare to "function" because it might be a fully qualified,
135# single quoted C++ function specifier.
19fa4a0a
MW
136
137proc runto { function } {
138 global prompt
139 global decimal
140
141 send "delete\n"
142 expect {
9bcc6c3f 143 -re "delete.*Delete all breakpoints.*y or n. $" {
19fa4a0a
MW
144 send "y\n"
145 expect {
146 -re "$prompt $" {}
147 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
148 }
149 }
150 -re ".*$prompt $" {}
151 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
152 }
153
154 send "break $function\n"
3e304ddf 155 # The first two regexps are what we get with -g, the third is without -g.
19fa4a0a 156 expect {
3e304ddf
C
157 -re "Breakpoint \[0-9\]* at 0x\[0-9a-f\]*: file .*, line $decimal.\r\n$prompt $" {}
158 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
c79f61db 159 -re "Breakpoint \[0-9\]* at 0x\[0-9a-f\]*.*$prompt $" {}
19fa4a0a
MW
160 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
161 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
162 }
163
4f2ec2ee
C
164 gdb_run_cmd
165
c79f61db
RS
166 # the "at foo.c:36" output we get with -g.
167 # the "in func" output we get without -g.
412c988b 168 expect {
4f2ec2ee
C
169 -re "Break.* at .*:$decimal.*$prompt $" {
170 return 1
412c988b 171 }
c79f61db
RS
172 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
173 return 1
174 }
4f2ec2ee
C
175 -re "$prompt $" {
176 fail "running to $function"
177 return 0
178 }
179 timeout {
180 fail "running to $function (timeout)"
181 return 0
182 }
19fa4a0a
MW
183 }
184}
185
186#
187# gdb_test -- send a command to gdb and test the result.
188# Takes three parameters.
189# Parameters:
190# First one is the command to execute,
191# Second one is the pattern to match for a PASS,
192# Third one is an optional message to be printed. If this
193# a null string "", then the pass/fail messages are not printed.
194# Returns:
195# 1 if the test failed,
196# 0 if the test passes,
197# -1 if there was an internal error.
198#
199proc gdb_test { args } {
200 global verbose
201 global prompt
202 global GDB
203 global spawn_id
204
205 if [llength $args]==3 then {
206 set message [lindex $args 2]
207 } else {
208 set message [lindex $args 0]
209 }
210 set command [lindex $args 0]
211 set pattern [lindex $args 1]
212
213 if $verbose>2 then {
214 send_user "Sending \"$command\" to gdb\n"
215 send_user "Looking to match \"$pattern\"\n"
216 send_user "Message is \"$message\"\n"
217 }
218
219 set result -1
220 set errmess ""
221 # trap the send so any problems don't crash things
222 catch "send \"$command\n\"" errmess
9bcc6c3f 223 if [string match "write.spawn_id=\[0-9\]+.:" $errmess] then {
85174909 224 perror "sent \"$command\" got expect error \"$errmess\""
c79f61db
RS
225 catch "close"
226 gdb_start
19fa4a0a
MW
227 return -1
228 }
229
230 expect {
231 -re ".*Ending remote debugging.*$prompt$" {
232 if ![isnative] then {
233 warning "Can`t communicate to remote target."
234 }
235 gdb_exit
236 gdb_start
237 set result -1
238 }
239 -re "$pattern.*$prompt $" {
240 if ![string match "" $message] then {
241 pass "$message"
242 }
243 set result 0
244 }
245 -re "Undefined command:.*$prompt" {
85174909 246 perror "Undefined command \"$command\"."
c79f61db 247 set result 1
19fa4a0a
MW
248 }
249 -re "Ambiguous command.*$prompt $" {
85174909 250 perror "\"$command\" is not a unique command name."
c79f61db 251 set result 1
19fa4a0a
MW
252 }
253 -re ".*$prompt $" {
254 if ![string match "" $message] then {
255 fail "$message"
256 }
257 set result 1
258 }
259 "<return>" {
260 send "\n"
85174909 261 perror "Window too small."
19fa4a0a 262 }
9bcc6c3f 263 -re "\[(\]+y or n\[)\]+ " {
19fa4a0a 264 send "n\n"
85174909 265 perror "Got interactive prompt."
19fa4a0a 266 }
c79f61db 267 eof {
85174909 268 perror "Process no longer exists"
c79f61db
RS
269 return -1
270 }
19fa4a0a 271 buffer_full {
85174909 272 perror "internal buffer is full."
19fa4a0a 273 }
19fa4a0a 274 timeout {
3e304ddf
C
275 if ![string match "" $message] then {
276 fail "(timeout) $message"
277 }
19fa4a0a
MW
278 set result 1
279 }
280 }
281 return $result
282}
283
faa15770
PB
284# Given an input string, adds backslashes as needed to create a
285# regexp that will match the string.
3e304ddf 286
faa15770 287proc string_to_regexp {str} {
3e304ddf
C
288 set result $str
289 regsub -all {[]*+.|()^$\[]} $str {\\&} result
faa15770
PB
290 return $result
291}
292
293# Same as gdb_test, but the second parameter is not a regexp,
294# but a string that must match exactly.
295
296proc gdb_test_exact { args } {
297 set command [lindex $args 0]
68361314 298 set pattern [string_to_regexp [lindex $args 1]]
faa15770
PB
299 if [llength $args]==3 then {
300 set message [lindex $args 2]
301 } else {
302 set message $command
303 }
304 return [gdb_test $command $pattern $message]
305}
306
19fa4a0a
MW
307proc gdb_reinitialize_dir { subdir } {
308 global prompt
19fa4a0a 309
85174909 310 send "dir\n"
19fa4a0a
MW
311 expect {
312 -re "Reinitialize source path to empty.*" {
313 send "y\n"
314 expect {
315 -re "Source directories searched.*$prompt $" {
316 send "dir $subdir\n"
317 expect {
318 -re "Source directories searched.*$prompt $" {
85174909 319 verbose "Dir set to $subdir"
19fa4a0a
MW
320 }
321 -re ".*$prompt $" {
85174909 322 perror "Dir \"$subdir\" failed."
19fa4a0a
MW
323 }
324 }
325 }
326 -re ".*$prompt $" {
85174909 327 perror "Dir \"$subdir\" failed."
19fa4a0a
MW
328 }
329 }
330 }
331 -re ".*$prompt $" {
85174909 332 perror "Dir \"$subdir\" failed."
19fa4a0a
MW
333 }
334 }
335}
c79f61db 336
c79f61db
RS
337#
338# gdb_exit -- exit the GDB, killing the target program if necessary
339#
340proc default_gdb_exit {} {
341 global GDB
342 global GDBFLAGS
343 global verbose
344
002cc99f 345 verbose "Quitting $GDB $GDBFLAGS"
c79f61db
RS
346
347 # This used to be 1 for unix-gdb.exp
348 set timeout 5
349
90fba5fa
BC
350 # We used to try to send "quit" to GDB, and wait for it to die.
351 # Dealing with all the cases and errors got pretty hairy. Just close it,
352 # that is simpler.
353 close
354
355 # Omitting this probably would cause strange timing-dependent failures.
c79f61db
RS
356 wait
357}
358
85174909
RS
359#
360# gdb_load -- load a file into the debugger.
361# return a -1 if anything goes wrong.
362#
363proc gdb_file_cmd { arg } {
364 global verbose
365 global loadpath
366 global loadfile
367 global GDB
368 global prompt
9bcc6c3f 369 global spawn_id
85174909
RS
370
371 send "file $arg\n"
372 expect {
373 -re "Reading symbols from.*done.*$prompt $" {
374 verbose "\t\tLoaded $arg into the $GDB"
375 return 0
376 }
377 -re "has no symbol-table.*$prompt $" {
378 perror "$arg wasn't compiled with \"-g\""
379 return -1
380 }
9bcc6c3f 381 -re "A program is being debugged already.*Kill it.*y or n. $" {
85174909
RS
382 send "y\n"
383 verbose "\t\tKilling previous program being debugged"
9bcc6c3f 384 exp_continue
85174909 385 }
9bcc6c3f 386 -re "Load new symbol table from \".*\".*y or n. $" {
85174909
RS
387 send "y\n"
388 expect {
389 -re "Reading symbols from.*done.*$prompt $" {
390 verbose "\t\tLoaded $arg with new symbol table into $GDB"
391 return 0
392 }
393 timeout {
394 perror "(timeout) Couldn't load $arg, other program already l
395oaded."
396 return -1
397 }
398 }
399 }
400 -re ".*No such file or directory.*$prompt $" {
401 perror "($arg) No such file or directory\n"
402 return -1
403 }
404 -re "$prompt $" {
405 perror "couldn't load $arg into $GDB."
406 return -1
407 }
408 timeout {
9bcc6c3f 409 perror "couldn't load $arg into $GDB (timed out)."
85174909
RS
410 return -1
411 }
412 eof {
413 # This is an attempt to detect a core dump, but seems not to
414 # work. Perhaps we need to match .* followed by eof, in which
415 # expect does not seem to have a way to do that.
8f07e537 416 perror "couldn't load $arg into $GDB (end of file)."
85174909
RS
417 return -1
418 }
419 }
420}
c79f61db 421
0fba9aa2
SS
422#
423# start gdb -- start gdb running, default procedure
424#
425proc default_gdb_start { } {
426 global verbose
427 global GDB
428 global GDBFLAGS
429 global prompt
430 global spawn_id
431 global timeout
432 verbose "Spawning $GDB $GDBFLAGS"
3e304ddf
C
433
434 if { [which $GDB] == 0 } then {
435 perror "$GDB does not exist."
436 exit 1
437 }
0fba9aa2
SS
438
439 set oldtimeout $timeout
440 set timeout [expr "$timeout + 60"]
441 if [ llength $GDBFLAGS ] then {
0fba9aa2 442 spawn $GDB $GDBFLAGS
0fba9aa2 443 } else {
0fba9aa2 444 spawn $GDB
0fba9aa2
SS
445 }
446 expect {
447 -re ".*\r\n$prompt $" {
3e304ddf 448 verbose "GDB initialized."
0fba9aa2
SS
449 }
450 -re "$prompt $" {
451 perror "GDB never initialized."
452 return -1
453 }
454 timeout {
455 perror "(timeout) GDB never initialized."
456 return -1
457 }
458 }
459 set timeout $oldtimeout
460 # force the height to "unlimited", so no pagers get used
461 send "set height 0\n"
462 expect {
463 -re ".*$prompt $" {
464 verbose "Setting height to 0." 2
465 }
466 timeout {
467 warning "Couldn't set the height to 0."
468 }
469 }
470 # force the width to "unlimited", so no wraparound occurs
471 send "set width 0\n"
472 expect {
473 -re ".*$prompt $" {
474 verbose "Seting width to 0." 2
475 }
476 timeout {
477 warning "Couldn't set the width to 0."
478 }
479 }
480}
481
9bcc6c3f
RS
482#
483# FIXME: this is a copy of the new library procedure, but it's here too
484# till the new dejagnu gets installed everywhere. I'd hate to break the
485# gdb tests suite.
486#
002cc99f
RS
487global argv0
488if ![info exists argv0] then {
9bcc6c3f
RS
489 proc exp_continue { } {
490 continue -expect
491 }
492}
c79f61db
RS
493
494
This page took 0.148852 seconds and 4 git commands to generate.