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