forked from ruby/debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakpoint.rb
556 lines (458 loc) · 12.3 KB
/
breakpoint.rb
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# frozen_string_literal: true
require_relative 'color'
module DEBUGGER__
class Breakpoint
include SkipPathHelper
attr_reader :key, :skip_src
def initialize cond, command, path, do_enable: true
@deleted = false
@cond = cond
@command = command
@path = path
setup
enable if do_enable
end
def safe_eval b, expr
b.eval(expr)
rescue Exception => e
puts "[EVAL ERROR]"
puts " expr: #{expr}"
puts " err: #{e} (#{e.class})"
puts "Error caused by #{self}."
nil
end
def oneshot?
defined?(@oneshot) && @oneshot
end
def setup
raise "not implemented..."
end
def enable
@tp.enable
end
def disable
@tp&.disable
end
def enabled?
@tp.enabled?
end
def delete
disable
@deleted = true
end
def deleted?
@deleted
end
def suspend
if @command
provider, pre_cmds, do_cmds = @command
nonstop = true if do_cmds
cmds = [*pre_cmds&.split(';;'), *do_cmds&.split(';;')]
SESSION.add_preset_commands provider, cmds, kick: false, continue: nonstop
end
ThreadClient.current.on_breakpoint @tp, self
end
def to_s
s = ''.dup
s << " if: #{@cond}" if defined?(@cond) && @cond
s << " pre: #{@command[1]}" if defined?(@command) && @command && @command[1]
s << " do: #{@command[2]}" if defined?(@command) && @command && @command[2]
s
end
def description
to_s
end
def duplicable?
false
end
def skip_path?(path)
case @path
when Regexp
!path.match?(@path)
when String
!path.include?(@path)
else
super
end
end
include Color
def generate_label(name)
colorize(" BP - #{name} ", [:YELLOW, :BOLD, :REVERSE])
end
end
if RUBY_VERSION.to_f <= 2.7
# workaround for https://bugs.ruby-lang.org/issues/17302
TracePoint.new(:line){}.enable{}
end
class ISeqBreakpoint < Breakpoint
def initialize iseq, events, oneshot: false
@events = events
@iseq = iseq
@oneshot = oneshot
@key = [:iseq, @iseq.path, @iseq.first_lineno].freeze
super(nil, nil, nil)
end
def setup
@tp = TracePoint.new(*@events) do |tp|
delete if @oneshot
suspend
end
end
def enable
@tp.enable(target: @iseq)
end
end
class LineBreakpoint < Breakpoint
attr_reader :path, :line, :iseq, :cond, :oneshot, :hook_call, :command
def self.copy bp, root_iseq
nbp = LineBreakpoint.new bp.path, bp.line,
cond: bp.cond, oneshot: bp.oneshot, hook_call: bp.hook_call,
command: bp.command, skip_activate: true
nbp.try_activate root_iseq
nbp
end
def initialize path, line, cond: nil, oneshot: false, hook_call: true, command: nil, skip_activate: false, skip_src: false
@line = line
@oneshot = oneshot
@hook_call = hook_call
@skip_src = skip_src
@pending = false
@iseq = nil
@type = nil
@key = [path, @line].freeze
super(cond, command, path)
try_activate unless skip_activate
@pending = !@iseq
end
def setup
return unless @type
@tp = TracePoint.new(@type) do |tp|
if @cond
next unless safe_eval tp.binding, @cond
end
delete if @oneshot
suspend
end
end
def enable
return unless @iseq
if @type == :line
@tp.enable(target: @iseq, target_line: @line)
else
@tp.enable(target: @iseq)
end
rescue ArgumentError
puts @iseq.disasm # for debug
raise
end
def activate iseq, event, line
@iseq = iseq
@type = event
@line = line
@path = iseq.absolute_path
@key = [@path, @line].freeze
SESSION.rehash_bps
setup
enable
if @pending && !@oneshot
DEBUGGER__.info "#{self} is activated."
end
@pending = false
end
def activate_exact iseq, events, line
case
when events.include?(:RUBY_EVENT_CALL)
# "def foo" line set bp on the beginning of method foo
activate(iseq, :call, line)
when events.include?(:RUBY_EVENT_LINE)
activate(iseq, :line, line)
when events.include?(:RUBY_EVENT_RETURN)
activate(iseq, :return, line)
when events.include?(:RUBY_EVENT_B_RETURN)
activate(iseq, :b_return, line)
when events.include?(:RUBY_EVENT_END)
activate(iseq, :end, line)
else
# not activated
end
end
def duplicable?
@oneshot
end
NearestISeq = Struct.new(:iseq, :line, :events)
def iterate_iseq root_iseq
if root_iseq
is = [root_iseq]
while iseq = is.pop
yield iseq
iseq.each_child do |child_iseq|
is << child_iseq
end
end
else
ObjectSpace.each_iseq do |iseq|
if DEBUGGER__.compare_path((iseq.absolute_path || iseq.path), self.path) &&
iseq.first_lineno <= self.line &&
iseq.type != :ensure # ensure iseq is copied (duplicated)
yield iseq
end
end
end
end
def try_activate root_iseq = nil
nearest = nil # NearestISeq
iterate_iseq root_iseq do |iseq|
iseq.traceable_lines_norec(line_events = {})
lines = line_events.keys.sort
if !lines.empty? && lines.last >= line
nline = lines.bsearch{|l| line <= l}
events = line_events[nline]
next if events == [:RUBY_EVENT_B_CALL]
if @hook_call &&
events.include?(:RUBY_EVENT_CALL) &&
self.line == iseq.first_lineno
nline = iseq.first_lineno
end
if !nearest || ((line - nline).abs < (line - nearest.line).abs)
nearest = NearestISeq.new(iseq, nline, events)
elsif @hook_call &&
nearest.line == iseq.first_line &&
events.include?(:RUBY_EVENT_CALL)
nearest = NearestISeq.new(iseq, nline, events)
end
end
end
if nearest
activate_exact nearest.iseq, nearest.events, nearest.line
end
end
def to_s
oneshot = @oneshot ? " (oneshot)" : ""
if @iseq
"#{generate_label("Line")} #{@path}:#{@line} (#{@type})#{oneshot}" + super
else
"#{generate_label("Line (pending)")} #{@path}:#{@line}#{oneshot}" + super
end
end
def inspect
"<#{self.class.name} #{self.to_s}>"
end
def path_is? path
DEBUGGER__.compare_path(@path, path)
end
end
class CatchBreakpoint < Breakpoint
attr_reader :last_exc
def initialize pat, cond: nil, command: nil, path: nil
@pat = pat.freeze
@key = [:catch, @pat].freeze
@last_exc = nil
super(cond, command, path)
end
def setup
@tp = TracePoint.new(:raise){|tp|
exc = tp.raised_exception
next if SystemExit === exc
next if skip_path?(tp.path)
next if !safe_eval(tp.binding, @cond) if @cond
should_suspend = false
exc.class.ancestors.each{|cls|
if @pat === cls.name
should_suspend = true
@last_exc = exc
break
end
}
suspend if should_suspend
}
end
def to_s
"#{generate_label("Catch")} #{@pat.inspect}"
end
def description
"#{@last_exc.inspect} is raised."
end
end
class CheckBreakpoint < Breakpoint
def initialize cond:, command: nil, path: nil
@key = [:check, cond].freeze
super(cond, command, path)
end
def setup
@tp = TracePoint.new(:line){|tp|
next if SESSION.in_subsession? # TODO: Ractor support
next if ThreadClient.current.management?
next if skip_path?(tp.path)
if need_suspend? safe_eval(tp.binding, @cond)
suspend
end
}
end
private def need_suspend? cond_result
map = ThreadClient.current.check_bp_fulfillment_map
if cond_result
if map[self]
false
else
map[self] = true
end
else
map[self] = false
end
end
def to_s
s = "#{generate_label("Check")}"
s += super
s
end
end
class WatchIVarBreakpoint < Breakpoint
def initialize ivar, object, current, cond: nil, command: nil, path: nil
@ivar = ivar.to_sym
@object = object
@key = [:watch, object.object_id, @ivar].freeze
@current = current
super(cond, command, path)
end
def watch_eval(tp)
result = @object.instance_variable_get(@ivar)
if result != @current
begin
@prev = @current
@current = result
if (@cond.nil? || @object.instance_eval(@cond)) && !skip_path?(tp.path)
suspend
end
ensure
remove_instance_variable(:@prev)
end
end
rescue Exception
false
end
def setup
@tp = TracePoint.new(:line, :return, :b_return){|tp|
watch_eval(tp)
}
end
def to_s
value_str =
if defined?(@prev)
"#{@prev} -> #{@current}"
else
"#{@current}"
end
"#{generate_label("Watch")} #{@object} #{@ivar} = #{value_str}"
end
end
class MethodBreakpoint < Breakpoint
attr_reader :sig_method_name, :method, :klass
def initialize b, klass_name, op, method_name, cond: nil, command: nil, path: nil
@sig_klass_name = klass_name
@sig_op = op
@sig_method_name = method_name
@klass_eval_binding = b
@override_method = false
@klass = nil
@method = nil
@cond_class = nil
@key = "#{klass_name}#{op}#{method_name}".freeze
super(cond, command, path, do_enable: false)
end
def setup
@tp = TracePoint.new(:call){|tp|
next if !safe_eval(tp.binding, @cond) if @cond
next if @cond_class && !tp.self.kind_of?(@cond_class)
caller_location = caller_locations(2, 1).first.to_s
next if skip_path?(caller_location)
suspend
}
end
def eval_class_name
return @klass if @klass
@klass = @klass_eval_binding.eval(@sig_klass_name)
@klass_eval_binding = nil
@klass
end
def search_method
case @sig_op
when '.'
@method = @klass.method(@sig_method_name)
when '#'
@method = @klass.instance_method(@sig_method_name)
else
raise "Unknown op: #{@sig_op}"
end
end
def enable
try_enable
end
if RUBY_VERSION.to_f <= 2.6
def override klass
sig_method_name = @sig_method_name
klass.prepend Module.new{
define_method(sig_method_name) do |*args, &block|
super(*args, &block)
end
}
end
else
def override klass
sig_method_name = @sig_method_name
klass.prepend Module.new{
define_method(sig_method_name) do |*args, **kw, &block|
super(*args, **kw, &block)
end
}
end
end
def try_enable added: false
eval_class_name
search_method
begin
retried = false
@tp.enable(target: @method)
DEBUGGER__.info "#{self} is activated." if added
if @sig_op == '#'
@cond_class = @klass if @method.owner != @klass
else # '.'
begin
@cond_class = @klass.singleton_class if @method.owner != @klass.singleton_class
rescue TypeError
end
end
rescue ArgumentError
raise if retried
retried = true
# maybe C method
case @sig_op
when '.'
begin
override @klass.singleton_class
rescue TypeError
override @klass.class
end
when '#'
override @klass
end
# re-collect the method object after the above patch
search_method
@override_method = true if @method
retry
end
rescue Exception
raise unless added
end
def sig
@key
end
def to_s
if @method
loc = @method.source_location || []
"#{generate_label("Method")} #{sig} at #{loc.join(':')}"
else
"#{generate_label("Method (pending)")} #{sig}"
end + super
end
end
end