Skip to content

Commit 34fc61f

Browse files
author
matz
committed
new debug.rb
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_3@505 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 9d62daa commit 34fc61f

File tree

1 file changed

+116
-43
lines changed

1 file changed

+116
-43
lines changed

lib/debug.rb

Lines changed: 116 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
class DEBUGGER__
2+
begin
3+
require 'readline'
4+
def readline(prompt)
5+
Readline::readline(prompt, true)
6+
end
7+
rescue LoadError
8+
def readline(prompt)
9+
STDOUT.print prompt
10+
STDOUT.flush
11+
line = STDIN.gets
12+
line.chomp!
13+
line
14+
end
15+
USE_READLINE = false
16+
end
17+
218
trap("INT") { DEBUGGER__::CONTEXT.interrupt }
319
$DEBUG = true
420
def initialize
521
@break_points = []
22+
@display = []
623
@stop_next = 1
724
@frames = [nil]
825
@last_file = nil
@@ -42,10 +59,8 @@ def debug_command(file, line, id, binding)
4259
line_at(binding_file, binding_line)
4360
end
4461
@frames[0] = binding
45-
STDOUT.print "(rdb:-) "
46-
STDOUT.flush
47-
while input = STDIN.gets
48-
input.chop!
62+
display_expressions(binding)
63+
while input = readline("(rdb:-) ")
4964
if input == ""
5065
input = DEBUG_LAST_CMD[0]
5166
else
@@ -55,55 +70,98 @@ def debug_command(file, line, id, binding)
5570
case input
5671
when /^b(?:reak)?\s+((?:[^:\n]+:)?.+)/
5772
pos = $1
58-
if pos.index ":"
73+
if pos.index(":")
5974
file, pos = pos.split(":")
6075
end
6176
file = File.basename(file)
6277
if pos =~ /^\d+$/
6378
pname = pos
64-
pos = Integer(pos)
79+
pos = pos.to_i
6580
else
6681
pname = pos = pos.intern.id2name
6782
end
83+
@break_points.push [true, 0, file, pos]
6884
STDOUT.printf "Set breakpoint %d at %s:%s\n", @break_points.size, file,
6985
pname
70-
@break_points.push [file, pos]
7186

72-
when /^b(?:reak)?$/, /^i(?:nfo) b(?:reak)?$/
73-
n = 0
74-
for f, p in @break_points
75-
STDOUT.printf "%d %s:%s\n", n, f, p
87+
when /^wat(?:ch)?\s+((?:[^:\n]+:)?.+)$/
88+
exp = $1
89+
@break_points.push [true, 1, exp]
90+
STDOUT.printf "Set watchpoint %d\n", @break_points.size, exp
91+
92+
when /^b(?:reak)?$/, /^info b(?:reak)?$/
93+
n = 1
94+
STDOUT.print "breakpoints:\n"
95+
for b in @break_points
96+
if b[0] and (b[1] == 0)
97+
STDOUT.printf " %d %s:%s\n", n, b[2], b[3]
98+
end
7699
n += 1
77100
end
101+
n = 1
102+
STDOUT.print "\n"
103+
STDOUT.print "watchpoints:\n"
104+
for b in @break_points
105+
if b[0] and (b[1] == 1)
106+
STDOUT.printf " %d %s\n", n, b[2]
107+
end
108+
n += 1
109+
end
110+
STDOUT.print "\n"
78111

79112
when /^del(?:ete)?(?:\s+(\d+))?$/
80113
pos = $1
81114
unless pos
82115
STDOUT.print "clear all breakpoints? (y/n) "
83116
STDOUT.flush
84-
input = STDIN.gets.chop!
117+
input = readline
85118
if input == "y"
86-
for n in @break_points.indexes
87-
@break_points[n] = nil
119+
for b in @break_points
120+
b[0] = false
88121
end
89122
end
90123
else
91-
pos = Integer(pos)
92-
if @break_points[pos]
93-
bp = @break_points[pos]
94-
STDOUT.printf "Clear breakpoint %d at %s:%s\n", pos, bp[0], bp[1]
95-
@break_points[pos] = nil
124+
pos = pos.to_i
125+
if @break_points[pos-1]
126+
@break_points[pos-1][0] = false
96127
else
97128
STDOUT.printf "Breakpoint %d is not defined\n", pos
98129
end
99130
end
100131

101-
when /^c(?:ont)?$/
132+
when /^disp(?:lay)?\s+(.+)$/
133+
exp = $1
134+
@display.push.push [true, exp]
135+
STDOUT.printf " %d: %s = %s\n", @display.size, exp,
136+
debug_eval(exp, binding).to_s
137+
138+
when /^disp(?:lay)?$/, /^info disp(?:lay)?$/
139+
display_expressions(binding)
140+
141+
when /^undisp(?:lay)?(?:\s+(\d+))?$/
142+
pos = $1
143+
unless pos
144+
input = readline("clear all expressions? (y/n) ")
145+
if input == "y"
146+
for d in @display
147+
d[0] = false
148+
end
149+
end
150+
else
151+
pos = pos.to_i
152+
if @display[pos-1]
153+
@display[pos-1][0] = false
154+
else
155+
STDOUT.printf "display expression %d is not defined\n", pos
156+
end
157+
end
158+
159+
when /^co(?:nt)?$/
102160
return
103161

104162
when /^s(?:tep)?\s*(\d+)?$/
105163
if $1
106-
lev = Integer($1)
164+
lev = $1.to_i
107165
else
108166
lev = 1
109167
end
@@ -112,7 +170,7 @@ def debug_command(file, line, id, binding)
112170

113171
when /^n(?:ext)?\s*(\d+)?$/
114172
if $1
115-
lev = Integer($1)
173+
lev = $1.to_i
116174
else
117175
lev = 1
118176
end
@@ -122,16 +180,16 @@ def debug_command(file, line, id, binding)
122180

123181
when /^w(?:here)?$/, /^f(?:rame)?$/
124182
at = caller(0)
125-
0.upto( @frames.size - 1 ) do |n|
126-
if ( frame_pos == n )
183+
0.upto(@frames.size - 1) do |n|
184+
if frame_pos == n
127185
STDOUT.printf "--> #%d %s\n", n, at[-(@frames.size - n)]
128186
else
129187
STDOUT.printf " #%d %s\n", n, at[-(@frames.size - n)]
130188
end
131189
end
132190

133191
when /^l(?:ist)?(?:\s+(.+))?$/
134-
if !$1
192+
if not $1
135193
b = previus_line ? previus_line + 10 : binding_line - 5
136194
e = b + 9
137195
elsif $1 == '-'
@@ -140,10 +198,10 @@ def debug_command(file, line, id, binding)
140198
else
141199
b, e = $1.split(/[-,]/)
142200
if e
143-
b = Integer(b)
144-
e = Integer(e)
201+
b = b.to_i
202+
e = e.to_i
145203
else
146-
b = Integer(b) - 5
204+
b = b.to_i - 5
147205
e = b + 9
148206
end
149207
end
@@ -154,7 +212,7 @@ def debug_command(file, line, id, binding)
154212
n = 0
155213
b.upto(e) do |n|
156214
if n > 0 && lines[n-1]
157-
if ( n == binding_line )
215+
if n == binding_line
158216
STDOUT.printf "=> %d %s\n", n, lines[n-1].chomp
159217
else
160218
STDOUT.printf " %d %s\n", n, lines[n-1].chomp
@@ -168,7 +226,7 @@ def debug_command(file, line, id, binding)
168226
when /^up\s*(\d+)?$/
169227
previus_line = nil
170228
if $1
171-
lev = Integer($1)
229+
lev = $1.to_i
172230
else
173231
lev = 1
174232
end
@@ -184,7 +242,7 @@ def debug_command(file, line, id, binding)
184242
when /^down\s*(\d+)?$/
185243
previus_line = nil
186244
if $1
187-
lev = Integer($1)
245+
lev = $1.to_i
188246
else
189247
lev = 1
190248
end
@@ -203,26 +261,32 @@ def debug_command(file, line, id, binding)
203261
return
204262

205263
when /^q(?:uit)?$/
206-
STDOUT.print "really quit? (y/n) "
207-
STDOUT.flush
208-
input = STDIN.gets.chop!
264+
input = readline("really quit? (y/n) ")
209265
exit if input == "y"
210266

211267
when /^p\s+/
212268
p debug_eval($', binding)
213269

214270
else
215271
v = debug_eval(input, binding)
216-
p v unless v == nil
272+
p v unless (v == nil)
217273
end
218-
STDOUT.print "(rdb:-) "
219-
STDOUT.flush
220274
end
221275
end
222276

277+
def display_expressions(binding)
278+
n = 1
279+
for d in @display
280+
if d[0]
281+
STDOUT.printf "%d: %s = %s\n", n, d[1], debug_eval(d[1], binding).to_s
282+
end
283+
n += 1
284+
end
285+
end
286+
223287
def frame_info(pos = 0)
224288
info = caller(0)[-(@frames.size - pos)]
225-
info.sub( /:in `.*'$/, '' ) =~ /^(.*):(\d+)$/ #`
289+
info.sub(/:in `.*'$/, "") =~ /^(.*):(\d+)$/ #`
226290
[info, $1, $2.to_i]
227291
end
228292

@@ -259,11 +323,20 @@ def debug_funcname(id)
259323

260324
def check_break_points(file, pos, binding, id)
261325
file = File.basename(file)
262-
if @break_points.include? [file, pos]
263-
index = @break_points.index([file, pos])
264-
STDOUT.printf "Breakpoint %d, %s at %s:%s\n",
265-
index, debug_funcname(id), file, pos
266-
return true
326+
n = 1
327+
for b in @break_points
328+
if b[0]
329+
if b[1] == 0 and b[2] == file and b[3] == pos
330+
STDOUT.printf "breakpoint %d, %s at %s:%s\n", n, debug_funcname(id),
331+
file, pos
332+
return true
333+
elsif b[1] == 1 and debug_eval(b[2], binding)
334+
STDOUT.printf "watchpoint %d, %s at %s:%s\n", n, debug_funcname(id),
335+
file, pos
336+
return true
337+
end
338+
end
339+
n += 1
267340
end
268341
return false
269342
end

0 commit comments

Comments
 (0)