664aff284c43f8a475826423613ff53c75cb6063
[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 #
117 #
118 proc gdb_run_cmd {} {
119 send "run\n"
120 expect {
121 -re "The program .* has been started already.*y or n. $" {
122 send "y\n"
123 exp_continue
124 }
125 }
126 }
127
128
129 # Set breakpoint at function and run gdb until it breaks there.
130 # Since this is the only breakpoint that will be set, if it stops
131 # at a breakpoint, we will assume it is the one we want. We can't
132 # just compare to "function" because it might be a fully qualified,
133 # single quoted C++ function specifier.
134
135 proc runto { function } {
136 global prompt
137 global decimal
138
139 send "delete\n"
140 expect {
141 -re "delete.*Delete all breakpoints.*y or n. $" {
142 send "y\n"
143 expect {
144 -re "$prompt $" {}
145 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
146 }
147 }
148 -re ".*$prompt $" {}
149 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
150 }
151
152 send "break $function\n"
153 # The first two regexps are what we get with -g, the third is without -g.
154 expect {
155 -re "Breakpoint \[0-9\]* at 0x\[0-9a-f\]*: file .*, line $decimal.\r\n$prompt $" {}
156 -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$prompt $" {}
157 -re "Breakpoint \[0-9\]* at 0x\[0-9a-f\]*.*$prompt $" {}
158 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
159 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
160 }
161
162 # the "at foo.c:36" output we get with -g.
163 # the "in func" output we get without -g.
164 expect_after {
165 -re "Starting.*Break.* at .*:$decimal.*$prompt $" { return 1 }
166 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
167 return 1
168 }
169 -re "$prompt $" { fail "running to $function" ; return 0 }
170 timeout { fail "running to $function (timeout)" ; return 0 }
171 }
172 gdb_run_cmd
173 expect_after;
174 }
175
176 #
177 # gdb_test -- send a command to gdb and test the result.
178 # Takes three parameters.
179 # Parameters:
180 # First one is the command to execute,
181 # Second one is the pattern to match for a PASS,
182 # Third one is an optional message to be printed. If this
183 # a null string "", then the pass/fail messages are not printed.
184 # Returns:
185 # 1 if the test failed,
186 # 0 if the test passes,
187 # -1 if there was an internal error.
188 #
189 proc gdb_test { args } {
190 global verbose
191 global prompt
192 global GDB
193 global spawn_id
194
195 if [llength $args]==3 then {
196 set message [lindex $args 2]
197 } else {
198 set message [lindex $args 0]
199 }
200 set command [lindex $args 0]
201 set pattern [lindex $args 1]
202
203 if $verbose>2 then {
204 send_user "Sending \"$command\" to gdb\n"
205 send_user "Looking to match \"$pattern\"\n"
206 send_user "Message is \"$message\"\n"
207 }
208
209 set result -1
210 set errmess ""
211 # trap the send so any problems don't crash things
212 catch "send \"$command\n\"" errmess
213 if [string match "write.spawn_id=\[0-9\]+.:" $errmess] then {
214 perror "sent \"$command\" got expect error \"$errmess\""
215 catch "close"
216 gdb_start
217 return -1
218 }
219
220 expect {
221 -re ".*Ending remote debugging.*$prompt$" {
222 if ![isnative] then {
223 warning "Can`t communicate to remote target."
224 }
225 gdb_exit
226 gdb_start
227 set result -1
228 }
229 -re "$pattern.*$prompt $" {
230 if ![string match "" $message] then {
231 pass "$message"
232 }
233 set result 0
234 }
235 -re "Undefined command:.*$prompt" {
236 perror "Undefined command \"$command\"."
237 set result 1
238 }
239 -re "Ambiguous command.*$prompt $" {
240 perror "\"$command\" is not a unique command name."
241 set result 1
242 }
243 -re ".*$prompt $" {
244 if ![string match "" $message] then {
245 fail "$message"
246 }
247 set result 1
248 }
249 "<return>" {
250 send "\n"
251 perror "Window too small."
252 }
253 -re "\[(\]+y or n\[)\]+ " {
254 send "n\n"
255 perror "Got interactive prompt."
256 }
257 eof {
258 perror "Process no longer exists"
259 return -1
260 }
261 buffer_full {
262 perror "internal buffer is full."
263 }
264 timeout {
265 if ![string match "" $message] then {
266 fail "(timeout) $message"
267 }
268 set result 1
269 }
270 }
271 return $result
272 }
273
274 # Given an input string, adds backslashes as needed to create a
275 # regexp that will match the string.
276
277 proc string_to_regexp {str} {
278 set result $str
279 regsub -all {[]*+.|()^$\[]} $str {\\&} result
280 return $result
281 }
282
283 # Same as gdb_test, but the second parameter is not a regexp,
284 # but a string that must match exactly.
285
286 proc gdb_test_exact { args } {
287 set command [lindex $args 0]
288 set pattern [string_to_regexp [lindex $args 1]]
289 if [llength $args]==3 then {
290 set message [lindex $args 2]
291 } else {
292 set message $command
293 }
294 return [gdb_test $command $pattern $message]
295 }
296
297 proc gdb_reinitialize_dir { subdir } {
298 global prompt
299
300 send "dir\n"
301 expect {
302 -re "Reinitialize source path to empty.*" {
303 send "y\n"
304 expect {
305 -re "Source directories searched.*$prompt $" {
306 send "dir $subdir\n"
307 expect {
308 -re "Source directories searched.*$prompt $" {
309 verbose "Dir set to $subdir"
310 }
311 -re ".*$prompt $" {
312 perror "Dir \"$subdir\" failed."
313 }
314 }
315 }
316 -re ".*$prompt $" {
317 perror "Dir \"$subdir\" failed."
318 }
319 }
320 }
321 -re ".*$prompt $" {
322 perror "Dir \"$subdir\" failed."
323 }
324 }
325 }
326
327 #
328 # gdb_exit -- exit the GDB, killing the target program if necessary
329 #
330 proc default_gdb_exit {} {
331 global GDB
332 global GDBFLAGS
333 global verbose
334
335 verbose "Quitting $GDB $GDBFLAGS"
336
337 # This used to be 1 for unix-gdb.exp
338 set timeout 5
339
340 # We used to try to send "quit" to GDB, and wait for it to die.
341 # Dealing with all the cases and errors got pretty hairy. Just close it,
342 # that is simpler.
343 close
344
345 # Omitting this probably would cause strange timing-dependent failures.
346 wait
347 }
348
349 #
350 # gdb_load -- load a file into the debugger.
351 # return a -1 if anything goes wrong.
352 #
353 proc gdb_file_cmd { arg } {
354 global verbose
355 global loadpath
356 global loadfile
357 global GDB
358 global prompt
359 global spawn_id
360
361 send "file $arg\n"
362 expect {
363 -re "Reading symbols from.*done.*$prompt $" {
364 verbose "\t\tLoaded $arg into the $GDB"
365 return 0
366 }
367 -re "has no symbol-table.*$prompt $" {
368 perror "$arg wasn't compiled with \"-g\""
369 return -1
370 }
371 -re "A program is being debugged already.*Kill it.*y or n. $" {
372 send "y\n"
373 verbose "\t\tKilling previous program being debugged"
374 exp_continue
375 }
376 -re "Load new symbol table from \".*\".*y or n. $" {
377 send "y\n"
378 expect {
379 -re "Reading symbols from.*done.*$prompt $" {
380 verbose "\t\tLoaded $arg with new symbol table into $GDB"
381 return 0
382 }
383 timeout {
384 perror "(timeout) Couldn't load $arg, other program already l
385 oaded."
386 return -1
387 }
388 }
389 }
390 -re ".*No such file or directory.*$prompt $" {
391 perror "($arg) No such file or directory\n"
392 return -1
393 }
394 -re "$prompt $" {
395 perror "couldn't load $arg into $GDB."
396 return -1
397 }
398 timeout {
399 perror "couldn't load $arg into $GDB (timed out)."
400 return -1
401 }
402 eof {
403 # This is an attempt to detect a core dump, but seems not to
404 # work. Perhaps we need to match .* followed by eof, in which
405 # expect does not seem to have a way to do that.
406 perror "couldn't load $arg into $GDB (end of file)."
407 return -1
408 }
409 }
410 }
411
412 #
413 # start gdb -- start gdb running, default procedure
414 #
415 proc default_gdb_start { } {
416 global verbose
417 global GDB
418 global GDBFLAGS
419 global prompt
420 global spawn_id
421 global timeout
422 verbose "Spawning $GDB $GDBFLAGS"
423
424 if { [which $GDB] == 0 } then {
425 perror "$GDB does not exist."
426 exit 1
427 }
428
429 set oldtimeout $timeout
430 set timeout [expr "$timeout + 60"]
431 if [ llength $GDBFLAGS ] then {
432 spawn $GDB $GDBFLAGS
433 } else {
434 spawn $GDB
435 }
436 expect {
437 -re ".*\r\n$prompt $" {
438 verbose "GDB initialized."
439 }
440 -re "$prompt $" {
441 perror "GDB never initialized."
442 return -1
443 }
444 timeout {
445 perror "(timeout) GDB never initialized."
446 return -1
447 }
448 }
449 set timeout $oldtimeout
450 # force the height to "unlimited", so no pagers get used
451 send "set height 0\n"
452 expect {
453 -re ".*$prompt $" {
454 verbose "Setting height to 0." 2
455 }
456 timeout {
457 warning "Couldn't set the height to 0."
458 }
459 }
460 # force the width to "unlimited", so no wraparound occurs
461 send "set width 0\n"
462 expect {
463 -re ".*$prompt $" {
464 verbose "Seting width to 0." 2
465 }
466 timeout {
467 warning "Couldn't set the width to 0."
468 }
469 }
470 }
471
472 #
473 # FIXME: this is a copy of the new library procedure, but it's here too
474 # till the new dejagnu gets installed everywhere. I'd hate to break the
475 # gdb tests suite.
476 #
477 global argv0
478 if ![info exists argv0] then {
479 proc exp_continue { } {
480 continue -expect
481 }
482 }
483
484
This page took 0.041094 seconds and 3 git commands to generate.