* gdb.c++/classes.exp, gdb.c++/cplusfuncs.exp,
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
CommitLineData
f34c8766 1# Copyright (C) 1992, 1994, 1995 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 42#
f34c8766 43# gdb_version -- extract and print the version number of GDB
5019a275
RS
44#
45proc default_gdb_version {} {
46 global GDB
47 global GDBFLAGS
48 if {[which $GDB] != 0} then {
f34c8766 49 set tmp [exec echo "q" | $GDB -nw]
85174909 50 regexp " \[0-9\.\]+" $tmp version
f34c8766 51 clone_output "[which $GDB] version$version -nw $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#
70bcd4bc 116# Generic run command.
809943cf 117#
70bcd4bc
SS
118# The second pattern below matches up to the first newline *only*.
119# Using ``.*$'' could swallow up output that we attempt to match
120# elsewhere.
809943cf
C
121#
122proc gdb_run_cmd {} {
123 send "run\n"
124 expect {
125 -re "The program .* has been started already.*y or n. $" {
126 send "y\n"
127 exp_continue
128 }
959fea03 129 -re "Starting program: \[^\n\]*" {}
809943cf
C
130 }
131}
132
133
19fa4a0a
MW
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.
19fa4a0a
MW
139
140proc runto { function } {
141 global prompt
142 global decimal
143
144 send "delete\n"
145 expect {
9bcc6c3f 146 -re "delete.*Delete all breakpoints.*y or n. $" {
19fa4a0a
MW
147 send "y\n"
148 expect {
149 -re "$prompt $" {}
150 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
151 }
152 }
153 -re ".*$prompt $" {}
154 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
155 }
156
157 send "break $function\n"
3e304ddf 158 # The first two regexps are what we get with -g, the third is without -g.
19fa4a0a 159 expect {
f34c8766 160 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$prompt $" {}
3e304ddf 161 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
f34c8766 162 -re "Breakpoint \[0-9\]* at .*$prompt $" {}
19fa4a0a
MW
163 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
164 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
165 }
166
4f2ec2ee
C
167 gdb_run_cmd
168
c79f61db
RS
169 # the "at foo.c:36" output we get with -g.
170 # the "in func" output we get without -g.
412c988b 171 expect {
4f2ec2ee
C
172 -re "Break.* at .*:$decimal.*$prompt $" {
173 return 1
412c988b 174 }
c79f61db
RS
175 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
176 return 1
177 }
4f2ec2ee
C
178 -re "$prompt $" {
179 fail "running to $function"
180 return 0
181 }
182 timeout {
183 fail "running to $function (timeout)"
184 return 0
185 }
19fa4a0a
MW
186 }
187}
188
189#
190# gdb_test -- send a command to gdb and test the result.
191# Takes three parameters.
192# 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.
197# Returns:
198# 1 if the test failed,
199# 0 if the test passes,
200# -1 if there was an internal error.
201#
202proc gdb_test { args } {
203 global verbose
204 global prompt
205 global GDB
206 global spawn_id
207
208 if [llength $args]==3 then {
209 set message [lindex $args 2]
210 } else {
211 set message [lindex $args 0]
212 }
213 set command [lindex $args 0]
214 set pattern [lindex $args 1]
215
216 if $verbose>2 then {
217 send_user "Sending \"$command\" to gdb\n"
218 send_user "Looking to match \"$pattern\"\n"
219 send_user "Message is \"$message\"\n"
220 }
221
222 set result -1
223 set errmess ""
70bcd4bc
SS
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\""
229 catch "close"
230 gdb_start
231 return -1
232 }
19fa4a0a
MW
233 }
234
235 expect {
236 -re ".*Ending remote debugging.*$prompt$" {
237 if ![isnative] then {
238 warning "Can`t communicate to remote target."
239 }
240 gdb_exit
241 gdb_start
242 set result -1
243 }
244 -re "$pattern.*$prompt $" {
245 if ![string match "" $message] then {
246 pass "$message"
247 }
248 set result 0
249 }
250 -re "Undefined command:.*$prompt" {
85174909 251 perror "Undefined command \"$command\"."
c79f61db 252 set result 1
19fa4a0a
MW
253 }
254 -re "Ambiguous command.*$prompt $" {
85174909 255 perror "\"$command\" is not a unique command name."
c79f61db 256 set result 1
19fa4a0a
MW
257 }
258 -re ".*$prompt $" {
259 if ![string match "" $message] then {
260 fail "$message"
261 }
262 set result 1
263 }
264 "<return>" {
265 send "\n"
85174909 266 perror "Window too small."
19fa4a0a 267 }
9bcc6c3f 268 -re "\[(\]+y or n\[)\]+ " {
19fa4a0a 269 send "n\n"
85174909 270 perror "Got interactive prompt."
19fa4a0a 271 }
c79f61db 272 eof {
85174909 273 perror "Process no longer exists"
c79f61db
RS
274 return -1
275 }
19fa4a0a 276 buffer_full {
85174909 277 perror "internal buffer is full."
19fa4a0a 278 }
19fa4a0a 279 timeout {
3e304ddf
C
280 if ![string match "" $message] then {
281 fail "(timeout) $message"
282 }
19fa4a0a
MW
283 set result 1
284 }
285 }
286 return $result
287}
f34c8766
JK
288\f
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
293# as well.
294#
295# Args are:
296#
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
300#
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).
19fa4a0a 306
f34c8766
JK
307proc test_print_accept { args } {
308 global prompt
309 global verbose
310
311 if [llength $args]==3 then {
312 set message [lindex $args 2]
313 } else {
314 set message [lindex $args 0]
315 }
316 set sendthis [lindex $args 0]
317 set expectthis [lindex $args 1]
318 if $verbose>2 then {
319 send_user "Sending \"$sendthis\" to gdb\n"
320 send_user "Looking to match \"$expectthis\"\n"
321 send_user "Message is \"$message\"\n"
322 }
323 send "$sendthis\n"
324 expect {
325 -re ".* = $expectthis\r\n$prompt $" {
326 if ![string match "" $message] then {
327 pass "$sendthis ($message)"
328 } else {
329 pass "$sendthis"
330 }
331 return 1
332 }
333 -re ".*$prompt $" {
334 if ![string match "" $message] then {
335 fail "$sendthis ($message)"
336 } else {
337 fail "$sendthis"
338 }
339 return 1
340 }
341 timeout {
342 fail "$sendthis (timeout)"
343 return 0
344 }
345 }
346}
347
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
352# as well.
353
354proc test_print_reject { args } {
355 global prompt
356 global verbose
357
358 if [llength $args]==2 then {
359 set expectthis [lindex $args 1]
360 } else {
361 set expectthis "should never match this bogus string"
362 }
363 set sendthis [lindex $args 0]
364 if $verbose>2 then {
365 send_user "Sending \"$sendthis\" to gdb\n"
366 send_user "Looking to match \"$expectthis\"\n"
367 }
368 send "$sendthis\n"
369 expect {
370 -re ".*A .* in expression.*\\.*$prompt $" {
371 pass "reject $sendthis"
372 return 1
373 }
374 -re ".*Invalid syntax in expression.*$prompt $" {
375 pass "reject $sendthis"
376 return 1
377 }
378 -re ".*Junk after end of expression.*$prompt $" {
379 pass "reject $sendthis"
380 return 1
381 }
382 -re ".*Invalid number.*$prompt $" {
383 pass "reject $sendthis"
384 return 1
385 }
386 -re ".*Invalid character constant.*$prompt $" {
387 pass "reject $sendthis"
388 return 1
389 }
390 -re ".*No symbol table is loaded.*$prompt $" {
391 pass "reject $sendthis"
392 return 1
393 }
394 -re ".*No symbol .* in current context.*$prompt $" {
395 pass "reject $sendthis"
396 return 1
397 }
398 -re ".*$expectthis.*$prompt $" {
399 pass "reject $sendthis"
400 return 1
401 }
402 -re ".*$prompt $" {
403 fail "reject $sendthis"
404 return 1
405 }
406 default {
407 fail "reject $sendthis (eof or timeout)"
408 return 0
409 }
410 }
411}
412\f
faa15770
PB
413# Given an input string, adds backslashes as needed to create a
414# regexp that will match the string.
3e304ddf 415
faa15770 416proc string_to_regexp {str} {
3e304ddf
C
417 set result $str
418 regsub -all {[]*+.|()^$\[]} $str {\\&} result
faa15770
PB
419 return $result
420}
421
422# Same as gdb_test, but the second parameter is not a regexp,
423# but a string that must match exactly.
424
425proc gdb_test_exact { args } {
426 set command [lindex $args 0]
68361314 427 set pattern [string_to_regexp [lindex $args 1]]
faa15770
PB
428 if [llength $args]==3 then {
429 set message [lindex $args 2]
430 } else {
431 set message $command
432 }
433 return [gdb_test $command $pattern $message]
434}
f34c8766 435\f
19fa4a0a
MW
436proc gdb_reinitialize_dir { subdir } {
437 global prompt
19fa4a0a 438
85174909 439 send "dir\n"
19fa4a0a
MW
440 expect {
441 -re "Reinitialize source path to empty.*" {
442 send "y\n"
443 expect {
444 -re "Source directories searched.*$prompt $" {
445 send "dir $subdir\n"
446 expect {
447 -re "Source directories searched.*$prompt $" {
85174909 448 verbose "Dir set to $subdir"
19fa4a0a
MW
449 }
450 -re ".*$prompt $" {
85174909 451 perror "Dir \"$subdir\" failed."
19fa4a0a
MW
452 }
453 }
454 }
455 -re ".*$prompt $" {
85174909 456 perror "Dir \"$subdir\" failed."
19fa4a0a
MW
457 }
458 }
459 }
460 -re ".*$prompt $" {
85174909 461 perror "Dir \"$subdir\" failed."
19fa4a0a
MW
462 }
463 }
464}
c79f61db 465
c79f61db
RS
466#
467# gdb_exit -- exit the GDB, killing the target program if necessary
468#
469proc default_gdb_exit {} {
470 global GDB
471 global GDBFLAGS
472 global verbose
473
002cc99f 474 verbose "Quitting $GDB $GDBFLAGS"
c79f61db
RS
475
476 # This used to be 1 for unix-gdb.exp
477 set timeout 5
478
90fba5fa
BC
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,
481 # that is simpler.
482 close
483
484 # Omitting this probably would cause strange timing-dependent failures.
c79f61db
RS
485 wait
486}
487
85174909
RS
488#
489# gdb_load -- load a file into the debugger.
490# return a -1 if anything goes wrong.
491#
492proc gdb_file_cmd { arg } {
493 global verbose
494 global loadpath
495 global loadfile
496 global GDB
497 global prompt
9bcc6c3f 498 global spawn_id
85174909
RS
499
500 send "file $arg\n"
501 expect {
502 -re "Reading symbols from.*done.*$prompt $" {
503 verbose "\t\tLoaded $arg into the $GDB"
504 return 0
505 }
506 -re "has no symbol-table.*$prompt $" {
507 perror "$arg wasn't compiled with \"-g\""
508 return -1
509 }
9bcc6c3f 510 -re "A program is being debugged already.*Kill it.*y or n. $" {
85174909
RS
511 send "y\n"
512 verbose "\t\tKilling previous program being debugged"
9bcc6c3f 513 exp_continue
85174909 514 }
9bcc6c3f 515 -re "Load new symbol table from \".*\".*y or n. $" {
85174909
RS
516 send "y\n"
517 expect {
518 -re "Reading symbols from.*done.*$prompt $" {
519 verbose "\t\tLoaded $arg with new symbol table into $GDB"
520 return 0
521 }
522 timeout {
523 perror "(timeout) Couldn't load $arg, other program already l
524oaded."
525 return -1
526 }
527 }
528 }
529 -re ".*No such file or directory.*$prompt $" {
530 perror "($arg) No such file or directory\n"
531 return -1
532 }
533 -re "$prompt $" {
534 perror "couldn't load $arg into $GDB."
535 return -1
536 }
537 timeout {
9bcc6c3f 538 perror "couldn't load $arg into $GDB (timed out)."
85174909
RS
539 return -1
540 }
541 eof {
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.
8f07e537 545 perror "couldn't load $arg into $GDB (end of file)."
85174909
RS
546 return -1
547 }
548 }
549}
c79f61db 550
0fba9aa2
SS
551#
552# start gdb -- start gdb running, default procedure
553#
554proc default_gdb_start { } {
555 global verbose
556 global GDB
557 global GDBFLAGS
558 global prompt
559 global spawn_id
560 global timeout
f34c8766 561 verbose "Spawning $GDB -nw $GDBFLAGS"
3e304ddf
C
562
563 if { [which $GDB] == 0 } then {
564 perror "$GDB does not exist."
565 exit 1
566 }
0fba9aa2
SS
567
568 set oldtimeout $timeout
569 set timeout [expr "$timeout + 60"]
f34c8766 570 eval "spawn $GDB -nw $GDBFLAGS"
0fba9aa2
SS
571 expect {
572 -re ".*\r\n$prompt $" {
3e304ddf 573 verbose "GDB initialized."
0fba9aa2
SS
574 }
575 -re "$prompt $" {
576 perror "GDB never initialized."
577 return -1
578 }
579 timeout {
580 perror "(timeout) GDB never initialized."
581 return -1
582 }
583 }
584 set timeout $oldtimeout
585 # force the height to "unlimited", so no pagers get used
586 send "set height 0\n"
587 expect {
588 -re ".*$prompt $" {
589 verbose "Setting height to 0." 2
590 }
591 timeout {
592 warning "Couldn't set the height to 0."
593 }
594 }
595 # force the width to "unlimited", so no wraparound occurs
596 send "set width 0\n"
597 expect {
598 -re ".*$prompt $" {
599 verbose "Seting width to 0." 2
600 }
601 timeout {
602 warning "Couldn't set the width to 0."
603 }
604 }
605}
606
9bcc6c3f
RS
607#
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
610# gdb tests suite.
611#
002cc99f
RS
612global argv0
613if ![info exists argv0] then {
9bcc6c3f
RS
614 proc exp_continue { } {
615 continue -expect
616 }
617}
c79f61db
RS
618
619
This page took 0.18573 seconds and 4 git commands to generate.