Commit | Line | Data |
---|---|---|
0fba9aa2 | 1 | # Copyright (C) 1992, 1994 Free Software Foundation, Inc. |
19fa4a0a MW |
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: | |
c79f61db | 18 | # bug-gdb@prep.ai.mit.edu |
19fa4a0a MW |
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 | ||
9bcc6c3f RS |
26 | global GDB |
27 | if ![info exists GDB] then { | |
3e304ddf | 28 | set GDB [findfile $base_dir/../gdb $base_dir/../gdb [transform gdb ]] |
9bcc6c3f RS |
29 | } |
30 | ||
31 | global GDBFLAGS | |
85174909 RS |
32 | if ![info exists GDBFLAGS] then { |
33 | set GDBFLAGS "" | |
34 | } | |
35 | ||
90fba5fa BC |
36 | # set the prompt if it doesn't exist |
37 | global prompt | |
38 | if ![info exists prompt] then { | |
39 | set prompt "\[(\]gdb\[)\]" | |
40 | } | |
41 | ||
5019a275 RS |
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] | |
85174909 RS |
50 | regexp " \[0-9\.\]+" $tmp version |
51 | clone_output "[which $GDB] version$version $GDBFLAGS\n" | |
5019a275 RS |
52 | } else { |
53 | warning "$GDB does not exist" | |
54 | } | |
55 | } | |
56 | ||
19fa4a0a MW |
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 { | |
9bcc6c3f RS |
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. $"\ | |
19fa4a0a | 70 | { send "y\n" |
85174909 | 71 | verbose "\t\tKilling previous program being debugged" |
9bcc6c3f | 72 | exp_continue |
19fa4a0a | 73 | } |
9bcc6c3f | 74 | -re "Discard symbol table from .*y or n. $" { |
19fa4a0a | 75 | send "y\n" |
9bcc6c3f | 76 | exp_continue |
19fa4a0a MW |
77 | } |
78 | -re "$prompt $" {} | |
79 | timeout { | |
85174909 | 80 | perror "couldn't unload file in $GDB (timed out)." |
c79f61db | 81 | return -1 |
19fa4a0a MW |
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 { | |
9bcc6c3f | 97 | -re "Delete all breakpoints.*y or n. $" { |
19fa4a0a | 98 | send "y\n" |
9bcc6c3f | 99 | exp_continue |
19fa4a0a MW |
100 | } |
101 | -re "y\r\n$prompt $" {} | |
31711c69 JK |
102 | -re ".*$prompt $" { # This happens if there were no breakpoints |
103 | } | |
8f07e537 | 104 | timeout { perror "Delete all breakpoints (timeout)" ; return } |
19fa4a0a MW |
105 | } |
106 | send "info breakpoints\n" | |
107 | expect { | |
108 | -re "No breakpoints or watchpoints..*$prompt $" {} | |
9bcc6c3f | 109 | -re ".*$prompt $" { perror "breakpoints not deleted" ; return } |
8f07e537 | 110 | timeout { perror "info breakpoints (timeout)" ; return } |
19fa4a0a MW |
111 | } |
112 | } | |
113 | ||
114 | ||
115 | # | |
809943cf C |
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 | ||
19fa4a0a MW |
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. | |
19fa4a0a MW |
134 | |
135 | proc runto { function } { | |
136 | global prompt | |
137 | global decimal | |
138 | ||
139 | send "delete\n" | |
140 | expect { | |
9bcc6c3f | 141 | -re "delete.*Delete all breakpoints.*y or n. $" { |
19fa4a0a MW |
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" | |
3e304ddf | 153 | # The first two regexps are what we get with -g, the third is without -g. |
19fa4a0a | 154 | expect { |
3e304ddf C |
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 $" {} | |
c79f61db | 157 | -re "Breakpoint \[0-9\]* at 0x\[0-9a-f\]*.*$prompt $" {} |
19fa4a0a MW |
158 | -re "$prompt $" { fail "setting breakpoint at $function" ; return 0 } |
159 | timeout { fail "setting breakpoint at $function (timeout)" ; return 0 } | |
160 | } | |
161 | ||
412c988b | 162 | send "run\n" |
c79f61db RS |
163 | # the "at foo.c:36" output we get with -g. |
164 | # the "in func" output we get without -g. | |
412c988b C |
165 | expect { |
166 | -re "The program .* has been started already.*y or n. $" { | |
167 | send "y\n" | |
168 | exp_continue | |
169 | } | |
c79f61db RS |
170 | -re "Starting.*Break.* at .*:$decimal.*$prompt $" { return 1 } |
171 | -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in $function.*$prompt $" { | |
172 | return 1 | |
173 | } | |
19fa4a0a MW |
174 | -re "$prompt $" { fail "running to $function" ; return 0 } |
175 | timeout { fail "running to $function (timeout)" ; return 0 } | |
176 | } | |
177 | } | |
178 | ||
179 | # | |
180 | # gdb_test -- send a command to gdb and test the result. | |
181 | # Takes three parameters. | |
182 | # Parameters: | |
183 | # First one is the command to execute, | |
184 | # Second one is the pattern to match for a PASS, | |
185 | # Third one is an optional message to be printed. If this | |
186 | # a null string "", then the pass/fail messages are not printed. | |
187 | # Returns: | |
188 | # 1 if the test failed, | |
189 | # 0 if the test passes, | |
190 | # -1 if there was an internal error. | |
191 | # | |
192 | proc gdb_test { args } { | |
193 | global verbose | |
194 | global prompt | |
195 | global GDB | |
196 | global spawn_id | |
197 | ||
198 | if [llength $args]==3 then { | |
199 | set message [lindex $args 2] | |
200 | } else { | |
201 | set message [lindex $args 0] | |
202 | } | |
203 | set command [lindex $args 0] | |
204 | set pattern [lindex $args 1] | |
205 | ||
206 | if $verbose>2 then { | |
207 | send_user "Sending \"$command\" to gdb\n" | |
208 | send_user "Looking to match \"$pattern\"\n" | |
209 | send_user "Message is \"$message\"\n" | |
210 | } | |
211 | ||
212 | set result -1 | |
213 | set errmess "" | |
214 | # trap the send so any problems don't crash things | |
215 | catch "send \"$command\n\"" errmess | |
9bcc6c3f | 216 | if [string match "write.spawn_id=\[0-9\]+.:" $errmess] then { |
85174909 | 217 | perror "sent \"$command\" got expect error \"$errmess\"" |
c79f61db RS |
218 | catch "close" |
219 | gdb_start | |
19fa4a0a MW |
220 | return -1 |
221 | } | |
222 | ||
223 | expect { | |
224 | -re ".*Ending remote debugging.*$prompt$" { | |
225 | if ![isnative] then { | |
226 | warning "Can`t communicate to remote target." | |
227 | } | |
228 | gdb_exit | |
229 | gdb_start | |
230 | set result -1 | |
231 | } | |
232 | -re "$pattern.*$prompt $" { | |
233 | if ![string match "" $message] then { | |
234 | pass "$message" | |
235 | } | |
236 | set result 0 | |
237 | } | |
238 | -re "Undefined command:.*$prompt" { | |
85174909 | 239 | perror "Undefined command \"$command\"." |
c79f61db | 240 | set result 1 |
19fa4a0a MW |
241 | } |
242 | -re "Ambiguous command.*$prompt $" { | |
85174909 | 243 | perror "\"$command\" is not a unique command name." |
c79f61db | 244 | set result 1 |
19fa4a0a MW |
245 | } |
246 | -re ".*$prompt $" { | |
247 | if ![string match "" $message] then { | |
248 | fail "$message" | |
249 | } | |
250 | set result 1 | |
251 | } | |
252 | "<return>" { | |
253 | send "\n" | |
85174909 | 254 | perror "Window too small." |
19fa4a0a | 255 | } |
9bcc6c3f | 256 | -re "\[(\]+y or n\[)\]+ " { |
19fa4a0a | 257 | send "n\n" |
85174909 | 258 | perror "Got interactive prompt." |
19fa4a0a | 259 | } |
c79f61db | 260 | eof { |
85174909 | 261 | perror "Process no longer exists" |
c79f61db RS |
262 | return -1 |
263 | } | |
19fa4a0a | 264 | buffer_full { |
85174909 | 265 | perror "internal buffer is full." |
19fa4a0a | 266 | } |
19fa4a0a | 267 | timeout { |
3e304ddf C |
268 | if ![string match "" $message] then { |
269 | fail "(timeout) $message" | |
270 | } | |
19fa4a0a MW |
271 | set result 1 |
272 | } | |
273 | } | |
274 | return $result | |
275 | } | |
276 | ||
faa15770 PB |
277 | # Given an input string, adds backslashes as needed to create a |
278 | # regexp that will match the string. | |
3e304ddf | 279 | |
faa15770 | 280 | proc string_to_regexp {str} { |
3e304ddf C |
281 | set result $str |
282 | regsub -all {[]*+.|()^$\[]} $str {\\&} result | |
faa15770 PB |
283 | return $result |
284 | } | |
285 | ||
286 | # Same as gdb_test, but the second parameter is not a regexp, | |
287 | # but a string that must match exactly. | |
288 | ||
289 | proc gdb_test_exact { args } { | |
290 | set command [lindex $args 0] | |
68361314 | 291 | set pattern [string_to_regexp [lindex $args 1]] |
faa15770 PB |
292 | if [llength $args]==3 then { |
293 | set message [lindex $args 2] | |
294 | } else { | |
295 | set message $command | |
296 | } | |
297 | return [gdb_test $command $pattern $message] | |
298 | } | |
299 | ||
19fa4a0a MW |
300 | proc gdb_reinitialize_dir { subdir } { |
301 | global prompt | |
19fa4a0a | 302 | |
85174909 | 303 | send "dir\n" |
19fa4a0a MW |
304 | expect { |
305 | -re "Reinitialize source path to empty.*" { | |
306 | send "y\n" | |
307 | expect { | |
308 | -re "Source directories searched.*$prompt $" { | |
309 | send "dir $subdir\n" | |
310 | expect { | |
311 | -re "Source directories searched.*$prompt $" { | |
85174909 | 312 | verbose "Dir set to $subdir" |
19fa4a0a MW |
313 | } |
314 | -re ".*$prompt $" { | |
85174909 | 315 | perror "Dir \"$subdir\" failed." |
19fa4a0a MW |
316 | } |
317 | } | |
318 | } | |
319 | -re ".*$prompt $" { | |
85174909 | 320 | perror "Dir \"$subdir\" failed." |
19fa4a0a MW |
321 | } |
322 | } | |
323 | } | |
324 | -re ".*$prompt $" { | |
85174909 | 325 | perror "Dir \"$subdir\" failed." |
19fa4a0a MW |
326 | } |
327 | } | |
328 | } | |
c79f61db | 329 | |
c79f61db RS |
330 | # |
331 | # gdb_exit -- exit the GDB, killing the target program if necessary | |
332 | # | |
333 | proc default_gdb_exit {} { | |
334 | global GDB | |
335 | global GDBFLAGS | |
336 | global verbose | |
337 | ||
002cc99f | 338 | verbose "Quitting $GDB $GDBFLAGS" |
c79f61db RS |
339 | |
340 | # This used to be 1 for unix-gdb.exp | |
341 | set timeout 5 | |
342 | ||
90fba5fa BC |
343 | # We used to try to send "quit" to GDB, and wait for it to die. |
344 | # Dealing with all the cases and errors got pretty hairy. Just close it, | |
345 | # that is simpler. | |
346 | close | |
347 | ||
348 | # Omitting this probably would cause strange timing-dependent failures. | |
c79f61db RS |
349 | wait |
350 | } | |
351 | ||
85174909 RS |
352 | # |
353 | # gdb_load -- load a file into the debugger. | |
354 | # return a -1 if anything goes wrong. | |
355 | # | |
356 | proc gdb_file_cmd { arg } { | |
357 | global verbose | |
358 | global loadpath | |
359 | global loadfile | |
360 | global GDB | |
361 | global prompt | |
9bcc6c3f | 362 | global spawn_id |
85174909 RS |
363 | |
364 | send "file $arg\n" | |
365 | expect { | |
366 | -re "Reading symbols from.*done.*$prompt $" { | |
367 | verbose "\t\tLoaded $arg into the $GDB" | |
368 | return 0 | |
369 | } | |
370 | -re "has no symbol-table.*$prompt $" { | |
371 | perror "$arg wasn't compiled with \"-g\"" | |
372 | return -1 | |
373 | } | |
9bcc6c3f | 374 | -re "A program is being debugged already.*Kill it.*y or n. $" { |
85174909 RS |
375 | send "y\n" |
376 | verbose "\t\tKilling previous program being debugged" | |
9bcc6c3f | 377 | exp_continue |
85174909 | 378 | } |
9bcc6c3f | 379 | -re "Load new symbol table from \".*\".*y or n. $" { |
85174909 RS |
380 | send "y\n" |
381 | expect { | |
382 | -re "Reading symbols from.*done.*$prompt $" { | |
383 | verbose "\t\tLoaded $arg with new symbol table into $GDB" | |
384 | return 0 | |
385 | } | |
386 | timeout { | |
387 | perror "(timeout) Couldn't load $arg, other program already l | |
388 | oaded." | |
389 | return -1 | |
390 | } | |
391 | } | |
392 | } | |
393 | -re ".*No such file or directory.*$prompt $" { | |
394 | perror "($arg) No such file or directory\n" | |
395 | return -1 | |
396 | } | |
397 | -re "$prompt $" { | |
398 | perror "couldn't load $arg into $GDB." | |
399 | return -1 | |
400 | } | |
401 | timeout { | |
9bcc6c3f | 402 | perror "couldn't load $arg into $GDB (timed out)." |
85174909 RS |
403 | return -1 |
404 | } | |
405 | eof { | |
406 | # This is an attempt to detect a core dump, but seems not to | |
407 | # work. Perhaps we need to match .* followed by eof, in which | |
408 | # expect does not seem to have a way to do that. | |
8f07e537 | 409 | perror "couldn't load $arg into $GDB (end of file)." |
85174909 RS |
410 | return -1 |
411 | } | |
412 | } | |
413 | } | |
c79f61db | 414 | |
0fba9aa2 SS |
415 | # |
416 | # start gdb -- start gdb running, default procedure | |
417 | # | |
418 | proc default_gdb_start { } { | |
419 | global verbose | |
420 | global GDB | |
421 | global GDBFLAGS | |
422 | global prompt | |
423 | global spawn_id | |
424 | global timeout | |
425 | verbose "Spawning $GDB $GDBFLAGS" | |
3e304ddf C |
426 | |
427 | if { [which $GDB] == 0 } then { | |
428 | perror "$GDB does not exist." | |
429 | exit 1 | |
430 | } | |
0fba9aa2 SS |
431 | |
432 | set oldtimeout $timeout | |
433 | set timeout [expr "$timeout + 60"] | |
434 | if [ llength $GDBFLAGS ] then { | |
0fba9aa2 | 435 | spawn $GDB $GDBFLAGS |
0fba9aa2 | 436 | } else { |
0fba9aa2 | 437 | spawn $GDB |
0fba9aa2 SS |
438 | } |
439 | expect { | |
440 | -re ".*\r\n$prompt $" { | |
3e304ddf | 441 | verbose "GDB initialized." |
0fba9aa2 SS |
442 | } |
443 | -re "$prompt $" { | |
444 | perror "GDB never initialized." | |
445 | return -1 | |
446 | } | |
447 | timeout { | |
448 | perror "(timeout) GDB never initialized." | |
449 | return -1 | |
450 | } | |
451 | } | |
452 | set timeout $oldtimeout | |
453 | # force the height to "unlimited", so no pagers get used | |
454 | send "set height 0\n" | |
455 | expect { | |
456 | -re ".*$prompt $" { | |
457 | verbose "Setting height to 0." 2 | |
458 | } | |
459 | timeout { | |
460 | warning "Couldn't set the height to 0." | |
461 | } | |
462 | } | |
463 | # force the width to "unlimited", so no wraparound occurs | |
464 | send "set width 0\n" | |
465 | expect { | |
466 | -re ".*$prompt $" { | |
467 | verbose "Seting width to 0." 2 | |
468 | } | |
469 | timeout { | |
470 | warning "Couldn't set the width to 0." | |
471 | } | |
472 | } | |
473 | } | |
474 | ||
9bcc6c3f RS |
475 | # |
476 | # FIXME: this is a copy of the new library procedure, but it's here too | |
477 | # till the new dejagnu gets installed everywhere. I'd hate to break the | |
478 | # gdb tests suite. | |
479 | # | |
002cc99f RS |
480 | global argv0 |
481 | if ![info exists argv0] then { | |
9bcc6c3f RS |
482 | proc exp_continue { } { |
483 | continue -expect | |
484 | } | |
485 | } | |
c79f61db RS |
486 | |
487 |