cb54c954bb84eefdf144ba7587bb70df9f1cb8a7
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright (C) 1992, 1994 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 gcc
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]
50 regexp " \[0-9\.\]+" $tmp version
51 clone_output "[which $GDB] version$version $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 0x\[0-9a-f\]*: file .*, line $decimal.\r\n$prompt $" {}
161 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
162 -re "Breakpoint \[0-9\]* at 0x\[0-9a-f\]*.*$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
289 # Given an input string, adds backslashes as needed to create a
290 # regexp that will match the string.
291
292 proc string_to_regexp {str} {
293 set result $str
294 regsub -all {[]*+.|()^$\[]} $str {\\&} result
295 return $result
296 }
297
298 # Same as gdb_test, but the second parameter is not a regexp,
299 # but a string that must match exactly.
300
301 proc gdb_test_exact { args } {
302 set command [lindex $args 0]
303 set pattern [string_to_regexp [lindex $args 1]]
304 if [llength $args]==3 then {
305 set message [lindex $args 2]
306 } else {
307 set message $command
308 }
309 return [gdb_test $command $pattern $message]
310 }
311
312 proc gdb_reinitialize_dir { subdir } {
313 global prompt
314
315 send "dir\n"
316 expect {
317 -re "Reinitialize source path to empty.*" {
318 send "y\n"
319 expect {
320 -re "Source directories searched.*$prompt $" {
321 send "dir $subdir\n"
322 expect {
323 -re "Source directories searched.*$prompt $" {
324 verbose "Dir set to $subdir"
325 }
326 -re ".*$prompt $" {
327 perror "Dir \"$subdir\" failed."
328 }
329 }
330 }
331 -re ".*$prompt $" {
332 perror "Dir \"$subdir\" failed."
333 }
334 }
335 }
336 -re ".*$prompt $" {
337 perror "Dir \"$subdir\" failed."
338 }
339 }
340 }
341
342 #
343 # gdb_exit -- exit the GDB, killing the target program if necessary
344 #
345 proc default_gdb_exit {} {
346 global GDB
347 global GDBFLAGS
348 global verbose
349
350 verbose "Quitting $GDB $GDBFLAGS"
351
352 # This used to be 1 for unix-gdb.exp
353 set timeout 5
354
355 # We used to try to send "quit" to GDB, and wait for it to die.
356 # Dealing with all the cases and errors got pretty hairy. Just close it,
357 # that is simpler.
358 close
359
360 # Omitting this probably would cause strange timing-dependent failures.
361 wait
362 }
363
364 #
365 # gdb_load -- load a file into the debugger.
366 # return a -1 if anything goes wrong.
367 #
368 proc gdb_file_cmd { arg } {
369 global verbose
370 global loadpath
371 global loadfile
372 global GDB
373 global prompt
374 global spawn_id
375
376 send "file $arg\n"
377 expect {
378 -re "Reading symbols from.*done.*$prompt $" {
379 verbose "\t\tLoaded $arg into the $GDB"
380 return 0
381 }
382 -re "has no symbol-table.*$prompt $" {
383 perror "$arg wasn't compiled with \"-g\""
384 return -1
385 }
386 -re "A program is being debugged already.*Kill it.*y or n. $" {
387 send "y\n"
388 verbose "\t\tKilling previous program being debugged"
389 exp_continue
390 }
391 -re "Load new symbol table from \".*\".*y or n. $" {
392 send "y\n"
393 expect {
394 -re "Reading symbols from.*done.*$prompt $" {
395 verbose "\t\tLoaded $arg with new symbol table into $GDB"
396 return 0
397 }
398 timeout {
399 perror "(timeout) Couldn't load $arg, other program already l
400 oaded."
401 return -1
402 }
403 }
404 }
405 -re ".*No such file or directory.*$prompt $" {
406 perror "($arg) No such file or directory\n"
407 return -1
408 }
409 -re "$prompt $" {
410 perror "couldn't load $arg into $GDB."
411 return -1
412 }
413 timeout {
414 perror "couldn't load $arg into $GDB (timed out)."
415 return -1
416 }
417 eof {
418 # This is an attempt to detect a core dump, but seems not to
419 # work. Perhaps we need to match .* followed by eof, in which
420 # expect does not seem to have a way to do that.
421 perror "couldn't load $arg into $GDB (end of file)."
422 return -1
423 }
424 }
425 }
426
427 #
428 # start gdb -- start gdb running, default procedure
429 #
430 proc default_gdb_start { } {
431 global verbose
432 global GDB
433 global GDBFLAGS
434 global prompt
435 global spawn_id
436 global timeout
437 verbose "Spawning $GDB $GDBFLAGS"
438
439 if { [which $GDB] == 0 } then {
440 perror "$GDB does not exist."
441 exit 1
442 }
443
444 set oldtimeout $timeout
445 set timeout [expr "$timeout + 60"]
446 if [ llength $GDBFLAGS ] then {
447 spawn $GDB $GDBFLAGS
448 } else {
449 spawn $GDB
450 }
451 expect {
452 -re ".*\r\n$prompt $" {
453 verbose "GDB initialized."
454 }
455 -re "$prompt $" {
456 perror "GDB never initialized."
457 return -1
458 }
459 timeout {
460 perror "(timeout) GDB never initialized."
461 return -1
462 }
463 }
464 set timeout $oldtimeout
465 # force the height to "unlimited", so no pagers get used
466 send "set height 0\n"
467 expect {
468 -re ".*$prompt $" {
469 verbose "Setting height to 0." 2
470 }
471 timeout {
472 warning "Couldn't set the height to 0."
473 }
474 }
475 # force the width to "unlimited", so no wraparound occurs
476 send "set width 0\n"
477 expect {
478 -re ".*$prompt $" {
479 verbose "Seting width to 0." 2
480 }
481 timeout {
482 warning "Couldn't set the width to 0."
483 }
484 }
485 }
486
487 #
488 # FIXME: this is a copy of the new library procedure, but it's here too
489 # till the new dejagnu gets installed everywhere. I'd hate to break the
490 # gdb tests suite.
491 #
492 global argv0
493 if ![info exists argv0] then {
494 proc exp_continue { } {
495 continue -expect
496 }
497 }
498
499
This page took 0.040935 seconds and 3 git commands to generate.