* gdb.c++/classes.exp, gdb.c++/cplusfuncs.exp,
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
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:
18 # bug-gdb@prep.ai.mit.edu
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
26 global GDB
27 if ![info exists GDB] then {
28 set GDB [findfile $base_dir/../gdb $base_dir/../gdb [transform gdb ]]
29 }
30
31 global GDBFLAGS
32 if ![info exists GDBFLAGS] then {
33 set GDBFLAGS ""
34 }
35
36 # set the prompt if it doesn't exist
37 global prompt
38 if ![info exists prompt] then {
39 set prompt "\[(\]gdb\[)\]"
40 }
41
42 #
43 # gdb_version -- extract and print the version number of GDB
44 #
45 proc default_gdb_version {} {
46 global GDB
47 global GDBFLAGS
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"
52 } else {
53 warning "$GDB does not exist"
54 }
55 }
56
57 #
58 # gdb_unload -- unload a file if one is loaded
59 #
60
61 proc gdb_unload {} {
62 global verbose
63 global GDB
64 global prompt
65 send "file\n"
66 expect {
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. $"\
70 { send "y\n"
71 verbose "\t\tKilling previous program being debugged"
72 exp_continue
73 }
74 -re "Discard symbol table from .*y or n. $" {
75 send "y\n"
76 exp_continue
77 }
78 -re "$prompt $" {}
79 timeout {
80 perror "couldn't unload file in $GDB (timed out)."
81 return -1
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
92 proc delete_breakpoints {} {
93 global prompt
94
95 send "delete breakpoints\n"
96 expect {
97 -re "Delete all breakpoints.*y or n. $" {
98 send "y\n"
99 exp_continue
100 }
101 -re "y\r\n$prompt $" {}
102 -re ".*$prompt $" { # This happens if there were no breakpoints
103 }
104 timeout { perror "Delete all breakpoints (timeout)" ; return }
105 }
106 send "info breakpoints\n"
107 expect {
108 -re "No breakpoints or watchpoints..*$prompt $" {}
109 -re ".*$prompt $" { perror "breakpoints not deleted" ; return }
110 timeout { perror "info breakpoints (timeout)" ; return }
111 }
112 }
113
114
115 #
116 # Generic run command.
117 #
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.
121 #
122 proc 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 }
129 -re "Starting program: \[^\n\]*" {}
130 }
131 }
132
133
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.
139
140 proc runto { function } {
141 global prompt
142 global decimal
143
144 send "delete\n"
145 expect {
146 -re "delete.*Delete all breakpoints.*y or n. $" {
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"
158 # The first two regexps are what we get with -g, the third is without -g.
159 expect {
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 }
165 }
166
167 gdb_run_cmd
168
169 # the "at foo.c:36" output we get with -g.
170 # the "in func" output we get without -g.
171 expect {
172 -re "Break.* at .*:$decimal.*$prompt $" {
173 return 1
174 }
175 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
176 return 1
177 }
178 -re "$prompt $" {
179 fail "running to $function"
180 return 0
181 }
182 timeout {
183 fail "running to $function (timeout)"
184 return 0
185 }
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 #
202 proc 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 ""
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 }
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" {
251 perror "Undefined command \"$command\"."
252 set result 1
253 }
254 -re "Ambiguous command.*$prompt $" {
255 perror "\"$command\" is not a unique command name."
256 set result 1
257 }
258 -re ".*$prompt $" {
259 if ![string match "" $message] then {
260 fail "$message"
261 }
262 set result 1
263 }
264 "<return>" {
265 send "\n"
266 perror "Window too small."
267 }
268 -re "\[(\]+y or n\[)\]+ " {
269 send "n\n"
270 perror "Got interactive prompt."
271 }
272 eof {
273 perror "Process no longer exists"
274 return -1
275 }
276 buffer_full {
277 perror "internal buffer is full."
278 }
279 timeout {
280 if ![string match "" $message] then {
281 fail "(timeout) $message"
282 }
283 set result 1
284 }
285 }
286 return $result
287 }
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).
306
307 proc 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
354 proc 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
413 # Given an input string, adds backslashes as needed to create a
414 # regexp that will match the string.
415
416 proc string_to_regexp {str} {
417 set result $str
418 regsub -all {[]*+.|()^$\[]} $str {\\&} result
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
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]
430 } else {
431 set message $command
432 }
433 return [gdb_test $command $pattern $message]
434 }
435 \f
436 proc gdb_reinitialize_dir { subdir } {
437 global prompt
438
439 send "dir\n"
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 $" {
448 verbose "Dir set to $subdir"
449 }
450 -re ".*$prompt $" {
451 perror "Dir \"$subdir\" failed."
452 }
453 }
454 }
455 -re ".*$prompt $" {
456 perror "Dir \"$subdir\" failed."
457 }
458 }
459 }
460 -re ".*$prompt $" {
461 perror "Dir \"$subdir\" failed."
462 }
463 }
464 }
465
466 #
467 # gdb_exit -- exit the GDB, killing the target program if necessary
468 #
469 proc default_gdb_exit {} {
470 global GDB
471 global GDBFLAGS
472 global verbose
473
474 verbose "Quitting $GDB $GDBFLAGS"
475
476 # This used to be 1 for unix-gdb.exp
477 set timeout 5
478
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.
485 wait
486 }
487
488 #
489 # gdb_load -- load a file into the debugger.
490 # return a -1 if anything goes wrong.
491 #
492 proc gdb_file_cmd { arg } {
493 global verbose
494 global loadpath
495 global loadfile
496 global GDB
497 global prompt
498 global spawn_id
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 }
510 -re "A program is being debugged already.*Kill it.*y or n. $" {
511 send "y\n"
512 verbose "\t\tKilling previous program being debugged"
513 exp_continue
514 }
515 -re "Load new symbol table from \".*\".*y or n. $" {
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
524 oaded."
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 {
538 perror "couldn't load $arg into $GDB (timed out)."
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.
545 perror "couldn't load $arg into $GDB (end of file)."
546 return -1
547 }
548 }
549 }
550
551 #
552 # start gdb -- start gdb running, default procedure
553 #
554 proc default_gdb_start { } {
555 global verbose
556 global GDB
557 global GDBFLAGS
558 global prompt
559 global spawn_id
560 global timeout
561 verbose "Spawning $GDB -nw $GDBFLAGS"
562
563 if { [which $GDB] == 0 } then {
564 perror "$GDB does not exist."
565 exit 1
566 }
567
568 set oldtimeout $timeout
569 set timeout [expr "$timeout + 60"]
570 eval "spawn $GDB -nw $GDBFLAGS"
571 expect {
572 -re ".*\r\n$prompt $" {
573 verbose "GDB initialized."
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
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 #
612 global argv0
613 if ![info exists argv0] then {
614 proc exp_continue { } {
615 continue -expect
616 }
617 }
618
619
This page took 0.044328 seconds and 4 git commands to generate.