1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# Copyright (C) 1988, 1990, 1991, 1992 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
# Please email any bugs, comments, and/or additions to this file to:
# bug-gdb@prep.ai.mit.edu
# This file was written by Rob Savoye. (rob@cygnus.com)
# variables that need to set up
#
if ![info exists prompt] then {
set prompt "\(gdb\)"
}
# some convenience abbreviations
#
if ![info exists hex] then {
set hex "0x\[0-9A-Fa-f\]+"
}
if ![info exists decimal] then {
set decimal "\[0-9\]+"
}
#
# gdb_version -- extract and print the version number of gcc
#
proc gdb_version {} {
global GDB
global GDBFLAGS
if {[which $GDB] != 0} then {
set tmp [exec echo "q" | $GDB]
set version "[lindex $tmp [lsearch $tmp "\[0-9\]*"]]"
set version "[string range $version 0 [expr [string length $version]-2]]"
clone_output "[which $GDB] version $version $GDBFLAGS\n"
} else {
warning "$GDB does not exist"
}
}
#
# gdb_unload -- unload a file if one is loaded
#
proc gdb_unload {} {
global verbose
global GDB
global prompt
send "file\n"
expect {
-re "No exec file now\.\r" { continue -expect }
-re "No symbol file now\.\r" { continue -expect }
-re "A program is being debugged already..*Kill it\? \(y or n\) $"\
{ send "y\n"
if $verbose>1 then {
send_user "\t\tKilling previous program being debugged\n"
}
continue -expect
}
-re "Discard symbol table from .*\? \(y or n\) $" {
send "y\n"
continue -expect
}
-re "$prompt $" {}
timeout {
error "Couldn't unload file in $GDB (timed out)."
return -1
}
}
}
#
# gdb_load -- load a file into the debugger.
# return a -1 if anything goes wrong.
#
proc gdb_load { arg } {
global verbose
global loadpath
global loadfile
global GDB
global prompt
set loadfile [file tail $arg]
set loadpath [file dirname $arg]
send "file $arg\n"
expect {
-re "Reading symbols from.*done.*$prompt $" {
if $verbose>1 then {
send_user "\t\tLoaded $arg into the $GDB\n"
}
return 0
}
-re "has no symbol-table.*$prompt $" {
error "$arg wasn't compiled with \"-g\""
return -1
}
-re "A program is being debugged already..*Kill it\? \(y or n\) $" {
send "y\n"
if $verbose>1 then {
send_user "\t\tKilling previous program being debugged\n"
}
continue -expect
}
-re "Load new symbol table from.*\? \(y or n\) $" {
send "y\n"
expect {
-re "Reading symbols from.*done.*$prompt $" {
if $verbose>1 then {
send_user "\t\tLoaded $arg with new symbol table into $GDB\n"
}
return 0
}
timeout {
error "(timeout) Couldn't load $arg, other program already loaded."
return -1
}
}
}
-re ".*No such file or directory.*$prompt $" {
error "($arg) No such file or directory\n"
return -1
}
-re "$prompt $" {
error "couldn't load $arg into $GDB."
return -1
}
timeout {
error "couldn't load $arg into $GDB (timed out)."
return -1
}
eof {
# This is an attempt to detect a core dump, but seems not to
# work. Perhaps we need to match .* followed by eof, in which
# expect does not seem to have a way to do that.
error "couldn't load $arg into $GDB (end of file)."
return -1
}
}
}
#
# gdb_exit -- kills the gdb process currently running
#
# This overrides the generic version of gdb_exit in lib/gdb.exp, since
# it's doing a lot of wierd stuff that lib/gdb.exp isn't.
# FIXME, fold it in, or abandon this version.
proc gdb_exit {} {
global verbose
global GDB
global GDBFLAGS
set timeout 1
if $verbose>1 then {
send_user "Quitting $GDB $GDBFLAGS\n"
}
catch "send \"quit\n\"" result
if [string match "write\(spawn_id=\[0-9\]+\):" $result] then {
catch "close"
return -1
}
catch {
expect {
eof {
if $verbose>1 then {
send_user "Got EOF from $GDB\n"
}
}
timeout {
if $verbose>1 then {
send_user "Got TIMEOUT from $GDB\n"
}
close
}
-re "The program is running. Quit anyway.*? (y or n) $" {
send "y\n"
if $verbose>1 then {
send_user "\t\tKilling program being debugged\n"
}
close
}
}
}
catch "close"
# Before this was here sometimes "uit" would get sent to the next GDB
# (assuming this is immediately followed by gdb_start), which would
# cause a loss of syncronization (i.e. all the stuff that swallows a
# prompt would swallow the wrong one).
wait
if $verbose>1 then {
send_user "Quitting $GDB $GDBFLAGS\n"
}
}
#
# start gdb -- start gdb running
#
proc gdb_start {} {
global verbose
global GDB
global GDBFLAGS
global prompt
global spawn_id
global timeout
if $verbose>1 then {
send_user "Spawning $GDB $GDBFLAGS\n"
}
set oldtimeout $timeout
set timeout [expr "$timeout + 60"]
if [ llength $GDBFLAGS ] then {
if {[which $GDB] != 0} then {
spawn $GDB $GDBFLAGS
} else {
error "$GDB does not exist."
exit 1
}
} else {
IF {[WHICH $GDB] != 0} then {
spawn $GDB
} else {
error "$GDB does not exist."
exit 1
}
}
expect {
-re ".*$prompt $" {
if $verbose>1 then {
send_user "GDB initialized for native mode\n"
}
}
-re "$prompt $" {
error "GDB never initialized."
return -1
}
timeout {
error "(timeout) GDB never initialized."
return -1
}
}
set timeout $oldtimeout
# force the height to "unlimited", so no pagers get used
send "set height 0\n"
expect {
-re ".*$prompt $" {
if $verbose>2 then {
send_user "Seting height to 0.\n"
}
}
timeout {
warning "Couldn't set the height to 0."
}
}
# force the width to "unlimited", so no wraparound occurs
send "set width 0\n"
expect {
-re ".*$prompt $" {
if $verbose>2 then {
send_user "Seting width to 0.\n"
}
}
timeout {
warning "Couldn't set the width to 0."
}
}
}
# These only need to be uncommented for debugging test cases. They exist
# mainly to catch programming errors
#expect_after {
# "<return>" { send "\n"; error "Window too small." }
# -re "\(y or n\) " { send "n\n"; error "Got interactive prompt." }
# buffer_full { error "internal buffer is full." }
# eof { error "eof -- there is no child process" ; cleanup ; exit $exit_status}
# timeout { error "timeout." }
# "virtual memory exhausted" { error "virtual memory exhausted." }
# "Undefined command" { error "send string probably wrong." }
#}
load_lib gdb.exp
set binpath /s1/users/rob/vxworks/bin/somewhere-bogus-that-needs-configuring
set bin $GDB
gdb_start
|