* lib/gdb.exp (gdb_test): Between $pattern and $prompt, expect
[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 if [file exists $base_dir/../gdb] then {
29 set GDB $base_dir/../gdb
30 } else {
31 set GDB [transform gdb]
32 }
33 }
34
35 global GDBFLAGS
36 if ![info exists GDBFLAGS] then {
37 set GDBFLAGS "-nx"
38 }
39
40 # set the prompt if it doesn't exist
41 global prompt
42 if ![info exists prompt] then {
43 set prompt "\[(\]gdb\[)\]"
44 }
45
46 #
47 # gdb_version -- extract and print the version number of GDB
48 #
49 proc default_gdb_version {} {
50 global GDB
51 global GDBFLAGS
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"
56 } else {
57 warning "$GDB does not exist"
58 }
59 }
60
61 #
62 # gdb_unload -- unload a file if one is loaded
63 #
64
65 proc gdb_unload {} {
66 global verbose
67 global GDB
68 global prompt
69 send "file\n"
70 expect {
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. $"\
74 { send "y\n"
75 verbose "\t\tKilling previous program being debugged"
76 exp_continue
77 }
78 -re "Discard symbol table from .*y or n. $" {
79 send "y\n"
80 exp_continue
81 }
82 -re "$prompt $" {}
83 timeout {
84 perror "couldn't unload file in $GDB (timed out)."
85 return -1
86 }
87 }
88 }
89
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.
94 #
95
96 proc delete_breakpoints {} {
97 global prompt
98
99 send "delete breakpoints\n"
100 expect {
101 -re "Delete all breakpoints.*y or n. $" {
102 send "y\n"
103 exp_continue
104 }
105 -re "y\r\n$prompt $" {}
106 -re ".*$prompt $" { # This happens if there were no breakpoints
107 }
108 timeout { perror "Delete all breakpoints (timeout)" ; return }
109 }
110 send "info breakpoints\n"
111 expect {
112 -re "No breakpoints or watchpoints..*$prompt $" {}
113 -re ".*$prompt $" { perror "breakpoints not deleted" ; return }
114 timeout { perror "info breakpoints (timeout)" ; return }
115 }
116 }
117
118
119 #
120 # Generic run command.
121 #
122 # The second pattern below matches up to the first newline *only*.
123 # Using ``.*$'' could swallow up output that we attempt to match
124 # elsewhere.
125 #
126 proc gdb_run_cmd {} {
127 send "run\n"
128 expect {
129 -re "The program .* has been started already.*y or n. $" {
130 send "y\n"
131 exp_continue
132 }
133 -re "Starting program: \[^\n\]*" {}
134 }
135 }
136
137
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.
143
144 proc runto { function } {
145 global prompt
146 global decimal
147
148 send "delete\n"
149 expect {
150 -re "delete.*Delete all breakpoints.*y or n. $" {
151 send "y\n"
152 expect {
153 -re "$prompt $" {}
154 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
155 }
156 }
157 -re ".*$prompt $" {}
158 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
159 }
160
161 send "break $function\n"
162 # The first two regexps are what we get with -g, the third is without -g.
163 expect {
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 }
169 }
170
171 gdb_run_cmd
172
173 # the "at foo.c:36" output we get with -g.
174 # the "in func" output we get without -g.
175 expect {
176 -re "Break.* at .*:$decimal.*$prompt $" {
177 return 1
178 }
179 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
180 return 1
181 }
182 -re "$prompt $" {
183 fail "running to $function"
184 return 0
185 }
186 timeout {
187 fail "running to $function (timeout)"
188 return 0
189 }
190 }
191 }
192
193 #
194 # gdb_test -- send a command to gdb and test the result.
195 # Takes three parameters.
196 # 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.
201 # Returns:
202 # 1 if the test failed,
203 # 0 if the test passes,
204 # -1 if there was an internal error.
205 #
206 proc gdb_test { args } {
207 global verbose
208 global prompt
209 global GDB
210 global spawn_id
211
212 if [llength $args]==3 then {
213 set message [lindex $args 2]
214 } else {
215 set message [lindex $args 0]
216 }
217 set command [lindex $args 0]
218 set pattern [lindex $args 1]
219
220 if $verbose>2 then {
221 send_user "Sending \"$command\" to gdb\n"
222 send_user "Looking to match \"$pattern\"\n"
223 send_user "Message is \"$message\"\n"
224 }
225
226 set result -1
227 if ![string match $command ""] {
228 send "$command\n"
229 }
230
231 expect {
232 -re ".*Ending remote debugging.*$prompt$" {
233 if ![isnative] then {
234 warning "Can`t communicate to remote target."
235 }
236 gdb_exit
237 gdb_start
238 set result -1
239 }
240 -re "$pattern\r\n$prompt $" {
241 if ![string match "" $message] then {
242 pass "$message"
243 }
244 set result 0
245 }
246 -re "Undefined command:.*$prompt" {
247 perror "Undefined command \"$command\"."
248 set result 1
249 }
250 -re "Ambiguous command.*$prompt $" {
251 perror "\"$command\" is not a unique command name."
252 set result 1
253 }
254 -re ".*$prompt $" {
255 if ![string match "" $message] then {
256 fail "$message"
257 }
258 set result 1
259 }
260 "<return>" {
261 send "\n"
262 perror "Window too small."
263 }
264 -re "\[(\]+y or n\[)\]+ " {
265 send "n\n"
266 perror "Got interactive prompt."
267 }
268 eof {
269 perror "Process no longer exists"
270 return -1
271 }
272 buffer_full {
273 perror "internal buffer is full."
274 }
275 timeout {
276 if ![string match "" $message] then {
277 fail "(timeout) $message"
278 }
279 set result 1
280 }
281 }
282 return $result
283 }
284 \f
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
289 # as well.
290 #
291 # Args are:
292 #
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
296 #
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.
300
301 proc test_print_accept { args } {
302 global prompt
303 global verbose
304
305 if [llength $args]==3 then {
306 set message [lindex $args 2]
307 } else {
308 set message [lindex $args 0]
309 }
310 set sendthis [lindex $args 0]
311 set expectthis [lindex $args 1]
312 if $verbose>2 then {
313 send_user "Sending \"$sendthis\" to gdb\n"
314 send_user "Looking to match \"$expectthis\"\n"
315 send_user "Message is \"$message\"\n"
316 }
317 send "$sendthis\n"
318 expect {
319 -re ".* = $expectthis\r\n$prompt $" {
320 if ![string match "" $message] then {
321 pass "$sendthis ($message)"
322 } else {
323 pass "$sendthis"
324 }
325 return 1
326 }
327 -re ".*$prompt $" {
328 if ![string match "" $message] then {
329 fail "$sendthis ($message)"
330 } else {
331 fail "$sendthis"
332 }
333 return 1
334 }
335 timeout {
336 fail "$sendthis (timeout)"
337 return 0
338 }
339 }
340 }
341
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
346 # as well.
347
348 proc test_print_reject { args } {
349 global prompt
350 global verbose
351
352 if [llength $args]==2 then {
353 set expectthis [lindex $args 1]
354 } else {
355 set expectthis "should never match this bogus string"
356 }
357 set sendthis [lindex $args 0]
358 if $verbose>2 then {
359 send_user "Sending \"$sendthis\" to gdb\n"
360 send_user "Looking to match \"$expectthis\"\n"
361 }
362 send "$sendthis\n"
363 expect {
364 -re ".*A .* in expression.*\\.*$prompt $" {
365 pass "reject $sendthis"
366 return 1
367 }
368 -re ".*Invalid syntax in expression.*$prompt $" {
369 pass "reject $sendthis"
370 return 1
371 }
372 -re ".*Junk after end of expression.*$prompt $" {
373 pass "reject $sendthis"
374 return 1
375 }
376 -re ".*Invalid number.*$prompt $" {
377 pass "reject $sendthis"
378 return 1
379 }
380 -re ".*Invalid character constant.*$prompt $" {
381 pass "reject $sendthis"
382 return 1
383 }
384 -re ".*No symbol table is loaded.*$prompt $" {
385 pass "reject $sendthis"
386 return 1
387 }
388 -re ".*No symbol .* in current context.*$prompt $" {
389 pass "reject $sendthis"
390 return 1
391 }
392 -re ".*$expectthis.*$prompt $" {
393 pass "reject $sendthis"
394 return 1
395 }
396 -re ".*$prompt $" {
397 fail "reject $sendthis"
398 return 1
399 }
400 default {
401 fail "reject $sendthis (eof or timeout)"
402 return 0
403 }
404 }
405 }
406 \f
407 # Given an input string, adds backslashes as needed to create a
408 # regexp that will match the string.
409
410 proc string_to_regexp {str} {
411 set result $str
412 regsub -all {[]*+.|()^$\[]} $str {\\&} result
413 return $result
414 }
415
416 # Same as gdb_test, but the second parameter is not a regexp,
417 # but a string that must match exactly.
418
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]
424 } else {
425 set message $command
426 }
427 return [gdb_test $command $pattern $message]
428 }
429 \f
430 proc gdb_reinitialize_dir { subdir } {
431 global prompt
432
433 send "dir\n"
434 expect {
435 -re "Reinitialize source path to empty.*" {
436 send "y\n"
437 expect {
438 -re "Source directories searched.*$prompt $" {
439 send "dir $subdir\n"
440 expect {
441 -re "Source directories searched.*$prompt $" {
442 verbose "Dir set to $subdir"
443 }
444 -re ".*$prompt $" {
445 perror "Dir \"$subdir\" failed."
446 }
447 }
448 }
449 -re ".*$prompt $" {
450 perror "Dir \"$subdir\" failed."
451 }
452 }
453 }
454 -re ".*$prompt $" {
455 perror "Dir \"$subdir\" failed."
456 }
457 }
458 }
459
460 #
461 # gdb_exit -- exit the GDB, killing the target program if necessary
462 #
463 proc default_gdb_exit {} {
464 global GDB
465 global GDBFLAGS
466 global verbose
467
468 verbose "Quitting $GDB $GDBFLAGS"
469
470 # This used to be 1 for unix-gdb.exp
471 set timeout 5
472
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,
475 # that is simpler.
476 close
477
478 # Omitting this probably would cause strange timing-dependent failures.
479 wait
480 }
481
482 #
483 # gdb_load -- load a file into the debugger.
484 # return a -1 if anything goes wrong.
485 #
486 proc gdb_file_cmd { arg } {
487 global verbose
488 global loadpath
489 global loadfile
490 global GDB
491 global prompt
492 global spawn_id
493
494 send "file $arg\n"
495 expect {
496 -re "Reading symbols from.*done.*$prompt $" {
497 verbose "\t\tLoaded $arg into the $GDB"
498 return 0
499 }
500 -re "has no symbol-table.*$prompt $" {
501 perror "$arg wasn't compiled with \"-g\""
502 return -1
503 }
504 -re "A program is being debugged already.*Kill it.*y or n. $" {
505 send "y\n"
506 verbose "\t\tKilling previous program being debugged"
507 exp_continue
508 }
509 -re "Load new symbol table from \".*\".*y or n. $" {
510 send "y\n"
511 expect {
512 -re "Reading symbols from.*done.*$prompt $" {
513 verbose "\t\tLoaded $arg with new symbol table into $GDB"
514 return 0
515 }
516 timeout {
517 perror "(timeout) Couldn't load $arg, other program already l
518 oaded."
519 return -1
520 }
521 }
522 }
523 -re ".*No such file or directory.*$prompt $" {
524 perror "($arg) No such file or directory\n"
525 return -1
526 }
527 -re "$prompt $" {
528 perror "couldn't load $arg into $GDB."
529 return -1
530 }
531 timeout {
532 perror "couldn't load $arg into $GDB (timed out)."
533 return -1
534 }
535 eof {
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)."
540 return -1
541 }
542 }
543 }
544
545 #
546 # start gdb -- start gdb running, default procedure
547 #
548 proc default_gdb_start { } {
549 global verbose
550 global GDB
551 global GDBFLAGS
552 global prompt
553 global spawn_id
554 global timeout
555 verbose "Spawning $GDB -nw $GDBFLAGS"
556
557 if { [which $GDB] == 0 } then {
558 perror "$GDB does not exist."
559 exit 1
560 }
561
562 set oldtimeout $timeout
563 set timeout [expr "$timeout + 60"]
564 eval "spawn $GDB -nw $GDBFLAGS"
565 expect {
566 -re ".*\r\n$prompt $" {
567 verbose "GDB initialized."
568 }
569 -re "$prompt $" {
570 perror "GDB never initialized."
571 return -1
572 }
573 timeout {
574 perror "(timeout) GDB never initialized."
575 return -1
576 }
577 }
578 set timeout $oldtimeout
579 # force the height to "unlimited", so no pagers get used
580 send "set height 0\n"
581 expect {
582 -re ".*$prompt $" {
583 verbose "Setting height to 0." 2
584 }
585 timeout {
586 warning "Couldn't set the height to 0."
587 }
588 }
589 # force the width to "unlimited", so no wraparound occurs
590 send "set width 0\n"
591 expect {
592 -re ".*$prompt $" {
593 verbose "Seting width to 0." 2
594 }
595 timeout {
596 warning "Couldn't set the width to 0."
597 }
598 }
599 }
600
601 #
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
604 # gdb tests suite.
605 #
606 global argv0
607 if ![info exists argv0] then {
608 proc exp_continue { } {
609 continue -expect
610 }
611 }
612
613 proc skip_chill_tests {} {
614 return ![isnative]
615 }
This page took 0.042173 seconds and 5 git commands to generate.