* lib/gdb.exp: Transform tool name.
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright (C) 1992 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 if ![info exists GDB] then {
27 set GDB [transform gdb]
28 }
29 if ![info exists GDBFLAGS] then {
30 set GDBFLAGS ""
31 }
32
33 #
34 # gdb_version -- extract and print the version number of gcc
35 #
36 proc default_gdb_version {} {
37 global GDB
38 global GDBFLAGS
39 if {[which $GDB] != 0} then {
40 set tmp [exec echo "q" | $GDB]
41 regexp " \[0-9\.\]+" $tmp version
42 clone_output "[which $GDB] version$version $GDBFLAGS\n"
43 } else {
44 warning "$GDB does not exist"
45 }
46 }
47
48 #
49 # gdb_unload -- unload a file if one is loaded
50 #
51
52 proc gdb_unload {} {
53 global verbose
54 global GDB
55 global prompt
56 send "file\n"
57 expect {
58 -re "No exec file now\.\r" { continue -expect }
59 -re "No symbol file now\.\r" { continue -expect }
60 -re "A program is being debugged already..*Kill it\? \(y or n\) $"\
61 { send "y\n"
62 verbose "\t\tKilling previous program being debugged"
63 continue -expect
64 }
65 -re "Discard symbol table from .*\? \(y or n\) $" {
66 send "y\n"
67 continue -expect
68 }
69 -re "$prompt $" {}
70 timeout {
71 perror "couldn't unload file in $GDB (timed out)."
72 return -1
73 }
74 }
75 }
76
77 # Many of the tests depend on setting breakpoints at various places and
78 # running until that breakpoint is reached. At times, we want to start
79 # with a clean-slate with respect to breakpoints, so this utility proc
80 # lets us do this without duplicating this code everywhere.
81 #
82
83 proc delete_breakpoints {} {
84 global prompt
85
86 send "delete breakpoints\n"
87 expect {
88 -re "Delete all breakpoints\? \(y or n\) $" {
89 send "y\n"
90 continue -expect
91 }
92 -re "y\r\n$prompt $" {}
93 -re ".*$prompt $" { fail "Delete all breakpoints" ; return }
94 timeout { fail "Delete all breakpoints (timeout)" ; return }
95 }
96 send "info breakpoints\n"
97 expect {
98 -re "No breakpoints or watchpoints..*$prompt $" {}
99 -re ".*$prompt $" { fail "breakpoints not deleted" ; return }
100 timeout { fail "info breakpoints (timeout)" ; return }
101 }
102 }
103
104
105 #
106 # Set breakpoint at function and run gdb until it breaks there.
107 # Since this is the only breakpoint that will be set, if it stops
108 # at a breakpoint, we will assume it is the one we want. We can't
109 # just compare to "function" because it might be a fully qualified,
110 # single quoted C++ function specifier.
111 #
112
113 proc runto { function } {
114 global prompt
115 global decimal
116
117 send "delete\n"
118 expect {
119 -re "Delete all breakpoints\? \(y or n\) $" {
120 send "y\n"
121 expect {
122 -re "$prompt $" {}
123 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
124 }
125 }
126 -re ".*$prompt $" {}
127 timeout { fail "deleting breakpoints (timeout)" ; return 0 }
128 }
129
130 send "break $function\n"
131 # The first regexp is what we get with -g, the second without -g.
132 expect {
133 -re "Break.* at .*: file .*, line $decimal.\r\n$prompt $" {}
134 -re "Breakpoint \[0-9\]* at 0x\[0-9a-f\]*.*$prompt $" {}
135 -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 }
136 timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
137 }
138
139 send "run\n"
140 # the "at foo.c:36" output we get with -g.
141 # the "in func" output we get without -g.
142 expect {
143 -re "The program .* has been started already.* \(y or n\) $" {
144 send "y\n"
145 continue -expect
146 }
147 -re "Starting.*Break.* at .*:$decimal.*$prompt $" { return 1 }
148 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" {
149 return 1
150 }
151 -re "$prompt $" { fail "running to $function" ; return 0 }
152 timeout { fail "running to $function (timeout)" ; return 0 }
153 }
154 }
155
156 #
157 # gdb_test -- send a command to gdb and test the result.
158 # Takes three parameters.
159 # Parameters:
160 # First one is the command to execute,
161 # Second one is the pattern to match for a PASS,
162 # Third one is an optional message to be printed. If this
163 # a null string "", then the pass/fail messages are not printed.
164 # Returns:
165 # 1 if the test failed,
166 # 0 if the test passes,
167 # -1 if there was an internal error.
168 #
169 proc gdb_test { args } {
170 global verbose
171 global prompt
172 global GDB
173 global spawn_id
174
175 if [llength $args]==3 then {
176 set message [lindex $args 2]
177 } else {
178 set message [lindex $args 0]
179 }
180 set command [lindex $args 0]
181 set pattern [lindex $args 1]
182
183 if $verbose>2 then {
184 send_user "Sending \"$command\" to gdb\n"
185 send_user "Looking to match \"$pattern\"\n"
186 send_user "Message is \"$message\"\n"
187 }
188
189 set result -1
190 set errmess ""
191 # trap the send so any problems don't crash things
192 catch "send \"$command\n\"" errmess
193 if [string match "write\(spawn_id=\[0-9\]+\):" $errmess] then {
194 perror "sent \"$command\" got expect error \"$errmess\""
195 catch "close"
196 gdb_start
197 return -1
198 }
199
200 expect {
201 -re ".*Ending remote debugging.*$prompt$" {
202 if ![isnative] then {
203 warning "Can`t communicate to remote target."
204 }
205 gdb_exit
206 gdb_start
207 set result -1
208 }
209 -re "$pattern.*$prompt $" {
210 if ![string match "" $message] then {
211 pass "$message"
212 }
213 set result 0
214 }
215 -re "Undefined command:.*$prompt" {
216 perror "Undefined command \"$command\"."
217 set result 1
218 }
219 -re "Ambiguous command.*$prompt $" {
220 perror "\"$command\" is not a unique command name."
221 set result 1
222 }
223 -re ".*$prompt $" {
224 if ![string match "" $message] then {
225 fail "$message"
226 }
227 set result 1
228 }
229 "<return>" {
230 send "\n"
231 perror "Window too small."
232 }
233 -re "\(y or n\) " {
234 send "n\n"
235 perror "Got interactive prompt."
236 }
237 eof {
238 perror "Process no longer exists"
239 return -1
240 }
241 buffer_full {
242 perror "internal buffer is full."
243 }
244 timeout {
245 fail "(timeout) $message"
246 set result 1
247 }
248 }
249 return $result
250 }
251
252 proc gdb_reinitialize_dir { subdir } {
253 global prompt
254
255 send "dir\n"
256 expect {
257 -re "Reinitialize source path to empty.*" {
258 send "y\n"
259 expect {
260 -re "Source directories searched.*$prompt $" {
261 send "dir $subdir\n"
262 expect {
263 -re "Source directories searched.*$prompt $" {
264 verbose "Dir set to $subdir"
265 }
266 -re ".*$prompt $" {
267 perror "Dir \"$subdir\" failed."
268 }
269 }
270 }
271 -re ".*$prompt $" {
272 perror "Dir \"$subdir\" failed."
273 }
274 }
275 }
276 -re ".*$prompt $" {
277 perror "Dir \"$subdir\" failed."
278 }
279 }
280 }
281
282 #
283 # gdb_exit -- exit the GDB, killing the target program if necessary
284 #
285 proc default_gdb_exit {} {
286 global GDB
287 global GDBFLAGS
288 global verbose
289
290 verbose "Quitting $GDB $GDBFLAGS" 1
291
292 # This used to be 1 for unix-gdb.exp
293 set timeout 5
294
295 catch "send \"quit\n\"" result
296 # If the process has gone away (e.g. gdb dumped core), deal with it.
297 if [string match "write\(spawn_id=\[0-9\]+\):" $result] then {
298 catch "close"
299 # FIXME: Shouldn't we call "wait" too?
300 return -1
301 }
302 # FIXME: What is this catch statement doing here? Won't it prevent us
303 # from getting errors that we'd rather see?
304 catch {
305 expect {
306 eof {
307 verbose "Got EOF from $GDB" 2
308 }
309 timeout {
310 verbose "Got TIMEOUT from $GDB" 2
311 }
312 -re "The program is running. Quit anyway.*(y or n) $" {
313 send "y\n"
314 verbose "Killing program being debugged" 2
315 }
316 }
317 }
318
319 # Before this was here sometimes "uit" would get sent to the next GDB
320 # (assuming this is immediately followed by gdb_start), which would
321 # cause a loss of syncronization (i.e. all the stuff that swallows a
322 # prompt would swallow the wrong one).
323 wait
324 }
325
326 #
327 # gdb_load -- load a file into the debugger.
328 # return a -1 if anything goes wrong.
329 #
330 proc gdb_file_cmd { arg } {
331 global verbose
332 global loadpath
333 global loadfile
334 global GDB
335 global prompt
336
337 send "file $arg\n"
338 expect {
339 -re "Reading symbols from.*done.*$prompt $" {
340 verbose "\t\tLoaded $arg into the $GDB"
341 return 0
342 }
343 -re "has no symbol-table.*$prompt $" {
344 perror "$arg wasn't compiled with \"-g\""
345 return -1
346 }
347 -re "A program is being debugged already..*Kill it\? \(y or n\) $" {
348 send "y\n"
349 verbose "\t\tKilling previous program being debugged"
350 continue -expect
351 }
352 -re "Load new symbol table from.*\? \(y or n\) $" {
353 send "y\n"
354 expect {
355 -re "Reading symbols from.*done.*$prompt $" {
356 verbose "\t\tLoaded $arg with new symbol table into $GDB"
357 return 0
358 }
359 timeout {
360 perror "(timeout) Couldn't load $arg, other program already l
361 oaded."
362 return -1
363 }
364 }
365 }
366 -re ".*No such file or directory.*$prompt $" {
367 perror "($arg) No such file or directory\n"
368 return -1
369 }
370 -re "$prompt $" {
371 perror "couldn't load $arg into $GDB."
372 return -1
373 }
374 timeout {
375 error "couldn't load $arg into $GDB (timed out)."
376 return -1
377 }
378 eof {
379 # This is an attempt to detect a core dump, but seems not to
380 # work. Perhaps we need to match .* followed by eof, in which
381 # expect does not seem to have a way to do that.
382 error "couldn't load $arg into $GDB (end of file)."
383 return -1
384 }
385 }
386 }
387
388
389
This page took 0.040659 seconds and 5 git commands to generate.