* lib/gdb.exp: add proc runto_main, for targets that use stubs, this
[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 global usestubs
47 if [istarget "sparclite-*-*"] then {
48 set usestubs 1
49 } else {
50 set usestubs 0
51 }
52
53 if ![info exists noargs] then {
54 set noargs 0
55 }
56
57 #
58 # gdb_version -- extract and print the version number of GDB
59 #
60 proc default_gdb_version {} {
61 global GDB
62 global GDBFLAGS
63 if {[which $GDB] != 0} then {
64 set tmp [exec echo "q" | $GDB -nw $GDBFLAGS]
65 regexp " \[0-9\.\]+" $tmp version
66 clone_output "[which $GDB] version$version -nw $GDBFLAGS \n"
67 } else {
68 warning "$GDB does not exist"
69 }
70 }
71
72 #
73 # gdb_unload -- unload a file if one is loaded
74 #
75
76 proc gdb_unload {} {
77 global verbose
78 global GDB
79 global prompt
80 send "file\n"
81 expect {
82 -re "No exec file now.*\r" { exp_continue }
83 -re "No symbol file now.*\r" { exp_continue }
84 -re "A program is being debugged already..*Kill it.*y or n. $"\
85 { send "y\n"
86 verbose "\t\tKilling previous program being debugged"
87 exp_continue
88 }
89 -re "Discard symbol table from .*y or n. $" {
90 send "y\n"
91 exp_continue
92 }
93 -re "$prompt $" {}
94 timeout {
95 perror "couldn't unload file in $GDB (timed out)."
96 return -1
97 }
98 }
99 }
100
101 # Many of the tests depend on setting breakpoints at various places and
102 # running until that breakpoint is reached. At times, we want to start
103 # with a clean-slate with respect to breakpoints, so this utility proc
104 # lets us do this without duplicating this code everywhere.
105 #
106
107 proc delete_breakpoints {} {
108 global prompt
109
110 send "delete breakpoints\n"
111 expect {
112 -re "Delete all breakpoints.*y or n. $" {
113 send "y\n"
114 exp_continue
115 }
116 -re "y\r\n$prompt $" {}
117 -re ".*$prompt $" { # This happens if there were no breakpoints
118 }
119 timeout { perror "Delete all breakpoints (timeout)" ; return }
120 }
121 send "info breakpoints\n"
122 expect {
123 -re "No breakpoints or watchpoints..*$prompt $" {}
124 -re ".*$prompt $" { perror "breakpoints not deleted" ; return }
125 timeout { perror "info breakpoints (timeout)" ; return }
126 }
127 }
128
129
130 #
131 # Generic run command.
132 #
133 # The second pattern below matches up to the first newline *only*.
134 # Using ``.*$'' could swallow up output that we attempt to match
135 # elsewhere.
136 #
137 proc gdb_run_cmd {} {
138 send "run\n"
139 expect {
140 -re "The program .* has been started already.*y or n. $" {
141 send "y\n"
142 exp_continue
143 }
144 -re "Starting program: \[^\n\]*" {}
145 }
146 }
147
148
149 # Set breakpoint at function and run gdb until it breaks there.
150 # Since this is the only breakpoint that will be set, if it stops
151 # at a breakpoint, we will assume it is the one we want. We can't
152 # just compare to "function" because it might be a fully qualified,
153 # single quoted C++ function specifier.
154
155 proc runto { function } {
156 global prompt
157 global decimal
158
159 send "delete\n"
160 expect {
161 -re "delete.*Delete all breakpoints.*y or n. $" {
162 send "y\n"
163 expect {
164 -re "$prompt $" {}
165 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
166 }
167 }
168 -re ".*$prompt $" {}
169 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
170 }
171
172 send "break $function\n"
173 # The first two regexps are what we get with -g, the third is without -g.
174 expect {
175 -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$prompt $" {}
176 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
177 -re "Breakpoint \[0-9\]* at .*$prompt $" {}
178 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
179 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
180 }
181
182 gdb_run_cmd
183
184 # the "at foo.c:36" output we get with -g.
185 # the "in func" output we get without -g.
186 expect {
187 -re "Break.* at .*:$decimal.*$prompt $" {
188 return 1
189 }
190 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
191 return 1
192 }
193 -re "$prompt $" {
194 fail "running to $function"
195 return 0
196 }
197 timeout {
198 fail "running to $function (timeout)"
199 return 0
200 }
201 }
202 }
203
204 #
205 # runto_main -- ask gdb to run and untill hit break point at main.
206 # if it uses stubs, assuming we hit breakpoint() and just
207 # step out of the function.
208 #
209 proc runto_main {} {
210 global prompt
211 global decimal
212 global usestubs
213
214 if $usestubs==0 {
215 runto main
216 return 1
217 }
218
219 send "delete\n"
220 expect {
221 -re "delete.*Delete all breakpoints.*y or n. $" {
222 send "y\n"
223 expect {
224 -re "$prompt $" {}
225 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
226 }
227 }
228 -re ".*$prompt $" {}
229 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
230 }
231
232 send "step\n"
233 # if use stubs step out of the breakpoint() function.
234 expect {
235 -re "* at .*$prompt $" {}
236 timeout { fail "single step at breakpoint() (timeout)" ; return 0 }
237 }
238 }
239
240 #
241 # gdb_test -- send a command to gdb and test the result.
242 # Takes three parameters.
243 # Parameters:
244 # First one is the command to execute,
245 # Second one is the pattern to match for a PASS,
246 # Third one is an optional message to be printed. If this
247 # a null string "", then the pass/fail messages are not printed.
248 # Returns:
249 # 1 if the test failed,
250 # 0 if the test passes,
251 # -1 if there was an internal error.
252 #
253 proc gdb_test { args } {
254 global verbose
255 global prompt
256 global GDB
257 global spawn_id
258
259 if [llength $args]==3 then {
260 set message [lindex $args 2]
261 } else {
262 set message [lindex $args 0]
263 }
264 set command [lindex $args 0]
265 set pattern [lindex $args 1]
266
267 if $verbose>2 then {
268 send_user "Sending \"$command\" to gdb\n"
269 send_user "Looking to match \"$pattern\"\n"
270 send_user "Message is \"$message\"\n"
271 }
272
273 set result -1
274 if ![string match $command ""] {
275 send "$command\n"
276 }
277
278 expect {
279 -re ".*Ending remote debugging.*$prompt$" {
280 if ![isnative] then {
281 warning "Can`t communicate to remote target."
282 }
283 gdb_exit
284 gdb_start
285 set result -1
286 }
287 -re "$pattern\r\n$prompt $" {
288 if ![string match "" $message] then {
289 pass "$message"
290 }
291 set result 0
292 }
293 -re "Undefined command:.*$prompt" {
294 perror "Undefined command \"$command\"."
295 set result 1
296 }
297 -re "Ambiguous command.*$prompt $" {
298 perror "\"$command\" is not a unique command name."
299 set result 1
300 }
301 -re ".*$prompt $" {
302 if ![string match "" $message] then {
303 fail "$message"
304 }
305 set result 1
306 }
307 "<return>" {
308 send "\n"
309 perror "Window too small."
310 }
311 -re "\[(\]+y or n\[)\]+ " {
312 send "n\n"
313 perror "Got interactive prompt."
314 }
315 eof {
316 perror "Process no longer exists"
317 return -1
318 }
319 buffer_full {
320 perror "internal buffer is full."
321 }
322 timeout {
323 if ![string match "" $message] then {
324 fail "(timeout) $message"
325 }
326 set result 1
327 }
328 }
329 return $result
330 }
331 \f
332 # Test that a command gives an error. For pass or fail, return
333 # a 1 to indicate that more tests can proceed. However a timeout
334 # is a serious error, generates a special fail message, and causes
335 # a 0 to be returned to indicate that more tests are likely to fail
336 # as well.
337
338 proc test_print_reject { args } {
339 global prompt
340 global verbose
341
342 if [llength $args]==2 then {
343 set expectthis [lindex $args 1]
344 } else {
345 set expectthis "should never match this bogus string"
346 }
347 set sendthis [lindex $args 0]
348 if $verbose>2 then {
349 send_user "Sending \"$sendthis\" to gdb\n"
350 send_user "Looking to match \"$expectthis\"\n"
351 }
352 send "$sendthis\n"
353 expect {
354 -re ".*A .* in expression.*\\.*$prompt $" {
355 pass "reject $sendthis"
356 return 1
357 }
358 -re ".*Invalid syntax in expression.*$prompt $" {
359 pass "reject $sendthis"
360 return 1
361 }
362 -re ".*Junk after end of expression.*$prompt $" {
363 pass "reject $sendthis"
364 return 1
365 }
366 -re ".*Invalid number.*$prompt $" {
367 pass "reject $sendthis"
368 return 1
369 }
370 -re ".*Invalid character constant.*$prompt $" {
371 pass "reject $sendthis"
372 return 1
373 }
374 -re ".*No symbol table is loaded.*$prompt $" {
375 pass "reject $sendthis"
376 return 1
377 }
378 -re ".*No symbol .* in current context.*$prompt $" {
379 pass "reject $sendthis"
380 return 1
381 }
382 -re ".*$expectthis.*$prompt $" {
383 pass "reject $sendthis"
384 return 1
385 }
386 -re ".*$prompt $" {
387 fail "reject $sendthis"
388 return 1
389 }
390 default {
391 fail "reject $sendthis (eof or timeout)"
392 return 0
393 }
394 }
395 }
396 \f
397 # Given an input string, adds backslashes as needed to create a
398 # regexp that will match the string.
399
400 proc string_to_regexp {str} {
401 set result $str
402 regsub -all {[]*+.|()^$\[]} $str {\\&} result
403 return $result
404 }
405
406 # Same as gdb_test, but the second parameter is not a regexp,
407 # but a string that must match exactly.
408
409 proc gdb_test_exact { args } {
410 set command [lindex $args 0]
411 set pattern [string_to_regexp [lindex $args 1]]
412 if [llength $args]==3 then {
413 set message [lindex $args 2]
414 } else {
415 set message $command
416 }
417 return [gdb_test $command $pattern $message]
418 }
419 \f
420 proc gdb_reinitialize_dir { subdir } {
421 global prompt
422
423 send "dir\n"
424 expect {
425 -re "Reinitialize source path to empty.*" {
426 send "y\n"
427 expect {
428 -re "Source directories searched.*$prompt $" {
429 send "dir $subdir\n"
430 expect {
431 -re "Source directories searched.*$prompt $" {
432 verbose "Dir set to $subdir"
433 }
434 -re ".*$prompt $" {
435 perror "Dir \"$subdir\" failed."
436 }
437 }
438 }
439 -re ".*$prompt $" {
440 perror "Dir \"$subdir\" failed."
441 }
442 }
443 }
444 -re ".*$prompt $" {
445 perror "Dir \"$subdir\" failed."
446 }
447 }
448 }
449
450 #
451 # gdb_exit -- exit the GDB, killing the target program if necessary
452 #
453 proc default_gdb_exit {} {
454 global GDB
455 global GDBFLAGS
456 global verbose
457
458 verbose "Quitting $GDB $GDBFLAGS"
459
460 # This used to be 1 for unix-gdb.exp
461 set timeout 5
462
463 # We used to try to send "quit" to GDB, and wait for it to die.
464 # Dealing with all the cases and errors got pretty hairy. Just close it,
465 # that is simpler.
466 close
467
468 # Omitting this probably would cause strange timing-dependent failures.
469 wait
470 }
471
472 #
473 # gdb_load -- load a file into the debugger.
474 # return a -1 if anything goes wrong.
475 #
476 proc gdb_file_cmd { arg } {
477 global verbose
478 global loadpath
479 global loadfile
480 global GDB
481 global prompt
482 global spawn_id
483
484 send "file $arg\n"
485 expect {
486 -re "Reading symbols from.*done.*$prompt $" {
487 verbose "\t\tLoaded $arg into the $GDB"
488 return 0
489 }
490 -re "has no symbol-table.*$prompt $" {
491 perror "$arg wasn't compiled with \"-g\""
492 return -1
493 }
494 -re "A program is being debugged already.*Kill it.*y or n. $" {
495 send "y\n"
496 verbose "\t\tKilling previous program being debugged"
497 exp_continue
498 }
499 -re "Load new symbol table from \".*\".*y or n. $" {
500 send "y\n"
501 expect {
502 -re "Reading symbols from.*done.*$prompt $" {
503 verbose "\t\tLoaded $arg with new symbol table into $GDB"
504 return 0
505 }
506 timeout {
507 perror "(timeout) Couldn't load $arg, other program already l
508 oaded."
509 return -1
510 }
511 }
512 }
513 -re ".*No such file or directory.*$prompt $" {
514 perror "($arg) No such file or directory\n"
515 return -1
516 }
517 -re "$prompt $" {
518 perror "couldn't load $arg into $GDB."
519 return -1
520 }
521 timeout {
522 perror "couldn't load $arg into $GDB (timed out)."
523 return -1
524 }
525 eof {
526 # This is an attempt to detect a core dump, but seems not to
527 # work. Perhaps we need to match .* followed by eof, in which
528 # expect does not seem to have a way to do that.
529 perror "couldn't load $arg into $GDB (end of file)."
530 return -1
531 }
532 }
533 }
534
535 #
536 # start gdb -- start gdb running, default procedure
537 #
538 proc default_gdb_start { } {
539 global verbose
540 global GDB
541 global GDBFLAGS
542 global prompt
543 global spawn_id
544 global timeout
545 verbose "Spawning $GDB -nw $GDBFLAGS"
546
547 if { [which $GDB] == 0 } then {
548 perror "$GDB does not exist."
549 exit 1
550 }
551
552 set oldtimeout $timeout
553 set timeout [expr "$timeout + 60"]
554 eval "spawn $GDB -nw $GDBFLAGS"
555 expect {
556 -re ".*\r\n$prompt $" {
557 verbose "GDB initialized."
558 }
559 -re "$prompt $" {
560 perror "GDB never initialized."
561 return -1
562 }
563 timeout {
564 perror "(timeout) GDB never initialized."
565 return -1
566 }
567 }
568 set timeout $oldtimeout
569 # force the height to "unlimited", so no pagers get used
570 send "set height 0\n"
571 expect {
572 -re ".*$prompt $" {
573 verbose "Setting height to 0." 2
574 }
575 timeout {
576 warning "Couldn't set the height to 0."
577 }
578 }
579 # force the width to "unlimited", so no wraparound occurs
580 send "set width 0\n"
581 expect {
582 -re ".*$prompt $" {
583 verbose "Seting width to 0." 2
584 }
585 timeout {
586 warning "Couldn't set the width to 0."
587 }
588 }
589 }
590
591 #
592 # FIXME: this is a copy of the new library procedure, but it's here too
593 # till the new dejagnu gets installed everywhere. I'd hate to break the
594 # gdb tests suite.
595 #
596 global argv0
597 if ![info exists argv0] then {
598 proc exp_continue { } {
599 continue -expect
600 }
601 }
602
603 proc skip_chill_tests {} {
604 # For crosses, the CHILL runtime doesn't build because it can't find
605 # setjmp.h, stdio.h, etc.
606 # For AIX (as of 16 Mar 95), (a) there is no language code for
607 # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
608 # does not get along with AIX's too-clever linker.
609 # On Solaris, static constructors are broken.
610 return {![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-solaris2*"]}
611 }
This page took 0.045367 seconds and 5 git commands to generate.