/
thread_client.rb
1299 lines (1084 loc) · 32.3 KB
/
thread_client.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require 'objspace'
require 'pp'
require_relative 'color'
module DEBUGGER__
M_INSTANCE_VARIABLES = method(:instance_variables).unbind
M_INSTANCE_VARIABLE_GET = method(:instance_variable_get).unbind
M_CLASS = method(:class).unbind
M_SINGLETON_CLASS = method(:singleton_class).unbind
M_KIND_OF_P = method(:kind_of?).unbind
M_RESPOND_TO_P = method(:respond_to?).unbind
M_METHOD = method(:method).unbind
M_OBJECT_ID = method(:object_id).unbind
module SkipPathHelper
def skip_path?(path)
!path ||
CONFIG.skip? ||
ThreadClient.current.management? ||
skip_internal_path?(path) ||
skip_config_skip_path?(path)
end
def skip_config_skip_path?(path)
(skip_paths = CONFIG[:skip_path]) && skip_paths.any?{|skip_path| path.match?(skip_path)}
end
def skip_internal_path?(path)
path.start_with?(__dir__) || path.start_with?('<internal:')
end
def skip_location?(loc)
loc_path = loc.absolute_path || "!eval:#{loc.path}"
skip_path?(loc_path)
end
end
class ThreadClient
def self.current
if thc = Thread.current[:DEBUGGER__ThreadClient]
thc
else
thc = SESSION.get_thread_client
Thread.current[:DEBUGGER__ThreadClient] = thc
end
end
include Color
include SkipPathHelper
attr_reader :thread, :id, :recorder, :check_bp_fulfillment_map
def location
current_frame&.location
end
def assemble_arguments(args)
args.map do |arg|
"#{colorize_cyan(arg[:name])}=#{arg[:value]}"
end.join(", ")
end
def default_frame_formatter frame
call_identifier_str =
case frame.frame_type
when :block
level, block_loc = frame.block_identifier
args = frame.parameters_info
if !args.empty?
args_str = " {|#{assemble_arguments(args)}|}"
end
"#{colorize_blue("block")}#{args_str} in #{colorize_blue(block_loc + level)}"
when :method
ci = frame.method_identifier
args = frame.parameters_info
if !args.empty?
args_str = "(#{assemble_arguments(args)})"
end
"#{colorize_blue(ci)}#{args_str}"
when :c
colorize_blue(frame.c_identifier)
when :other
colorize_blue(frame.other_identifier)
end
location_str = colorize(frame.location_str, [:GREEN])
result = "#{call_identifier_str} at #{location_str}"
if return_str = frame.return_str
result += " #=> #{colorize_magenta(return_str)}"
end
result
end
def initialize id, q_evt, q_cmd, thr = Thread.current
@is_management = false
@id = id
@thread = thr
@target_frames = nil
@q_evt = q_evt
@q_cmd = q_cmd
@step_tp = nil
@output = []
@frame_formatter = method(:default_frame_formatter)
@var_map = {} # { thread_local_var_id => obj } for DAP
@obj_map = {} # { object_id => obj } for CDP
@recorder = nil
@mode = :waiting
@current_frame_index = 0
# every thread should maintain its own CheckBreakpoint fulfillment state
@check_bp_fulfillment_map = {} # { check_bp => boolean }
set_mode :running
thr.instance_variable_set(:@__thread_client_id, id)
::DEBUGGER__.info("Thread \##{@id} is created.")
end
def deactivate
@step_tp.disable if @step_tp
end
def management?
@is_management
end
def mark_as_management
@is_management = true
end
def set_mode mode
debug_mode(@mode, mode)
# STDERR.puts "#{@mode} => #{mode} @ #{caller.inspect}"
# pp caller
# mode transition check
case mode
when :running
raise "#{mode} is given, but #{mode}" unless self.waiting?
when :waiting
# TODO: there is waiting -> waiting
# raise "#{mode} is given, but #{mode}" unless self.running?
else
raise "unknown mode: #{mode}"
end
# DEBUGGER__.warn "#{@mode} => #{mode} @ #{self.inspect}"
@mode = mode
end
def running?
@mode == :running
end
def waiting?
@mode == :waiting
end
def name
"##{@id} #{@thread.name || @thread.backtrace.last}"
end
def close
@q_cmd.close
end
def inspect
if bt = @thread.backtrace
"#<DBG:TC #{self.id}:#{@mode}@#{bt[-1]}>"
else # bt can be nil
"#<DBG:TC #{self.id}:#{@mode}>"
end
end
def to_s
str = "(#{@thread.name || @thread.status})@#{current_frame&.location || @thread.to_s}"
str += " (not under control)" unless self.waiting?
str
end
def puts str = ''
if @recorder&.replaying?
prefix = colorize_dim("[replay] ")
end
case str
when nil
@output << "\n"
when Array
str.each{|s| puts s}
else
@output << "#{prefix}#{str.chomp}\n"
end
end
def << req
debug_cmd(req)
@q_cmd << req
end
def generate_info
return unless current_frame
{ location: current_frame.location_str, line: current_frame.location.lineno }
end
def event! ev, *args
debug_event(ev, args)
@q_evt << [self, @output, ev, generate_info, *args]
@output = []
end
## events
def wait_reply event_arg
return if management?
set_mode :waiting
event!(*event_arg)
wait_next_action
end
def on_load iseq, eval_src
wait_reply [:load, iseq, eval_src]
end
def on_init name
wait_reply [:init, name]
end
def on_trace trace_id, msg
wait_reply [:trace, trace_id, msg]
end
def on_breakpoint tp, bp
suspend tp.event, tp, bp: bp
end
def on_trap sig
if waiting?
# raise Interrupt
else
suspend :trap, sig: sig
end
end
def on_pause
suspend :pause
end
def suspend event, tp = nil, bp: nil, sig: nil, postmortem_frames: nil, replay_frames: nil, postmortem_exc: nil
return if management?
debug_suspend(event)
@current_frame_index = 0
case
when postmortem_frames
@target_frames = postmortem_frames
@postmortem = true
when replay_frames
@target_frames = replay_frames
else
@target_frames = DEBUGGER__.capture_frames(__dir__)
end
cf = @target_frames.first
if cf
case event
when :return, :b_return, :c_return
cf.has_return_value = true
cf.return_value = tp.return_value
end
if CatchBreakpoint === bp
cf.has_raised_exception = true
cf.raised_exception = bp.last_exc
end
if postmortem_exc
cf.has_raised_exception = true
cf.raised_exception = postmortem_exc
end
end
if event != :pause
show_src
show_frames CONFIG[:show_frames]
set_mode :waiting
if bp
event! :suspend, :breakpoint, bp.key
elsif sig
event! :suspend, :trap, sig
else
event! :suspend, event
end
else
set_mode :waiting
end
wait_next_action
end
def replay_suspend
# @recorder.current_position
suspend :replay, replay_frames: @recorder.current_frame
end
## control all
begin
TracePoint.new(:raise){}.enable(target_thread: Thread.current)
SUPPORT_TARGET_THREAD = true
rescue ArgumentError
SUPPORT_TARGET_THREAD = false
end
def step_tp iter, events = [:line, :b_return, :return]
@step_tp.disable if @step_tp
thread = Thread.current
if SUPPORT_TARGET_THREAD
@step_tp = TracePoint.new(*events){|tp|
next if SESSION.break_at? tp.path, tp.lineno
next if !yield(tp.event)
next if tp.path.start_with?(__dir__)
next if tp.path.start_with?('<internal:trace_point>')
next unless File.exist?(tp.path) if CONFIG[:skip_nosrc]
loc = caller_locations(1, 1).first
next if skip_location?(loc)
next if iter && (iter -= 1) > 0
tp.disable
suspend tp.event, tp
}
@step_tp.enable(target_thread: thread)
else
@step_tp = TracePoint.new(*events){|tp|
next if thread != Thread.current
next if SESSION.break_at? tp.path, tp.lineno
next if !yield(tp.event)
next if tp.path.start_with?(__dir__)
next if tp.path.start_with?('<internal:trace_point>')
next unless File.exist?(tp.path) if CONFIG[:skip_nosrc]
loc = caller_locations(1, 1).first
next if skip_location?(loc)
next if iter && (iter -= 1) > 0
tp.disable
suspend tp.event, tp
}
@step_tp.enable
end
end
## cmd helpers
if TracePoint.respond_to? :allow_reentry
def tp_allow_reentry
TracePoint.allow_reentry do
yield
end
rescue RuntimeError => e
# on the postmortem mode, it is not stopped in TracePoint
if e.message == 'No need to allow reentrance.'
yield
else
raise
end
end
else
def tp_allow_reentry
yield
end
end
def frame_eval_core src, b
saved_target_frames = @target_frames
saved_current_frame_index = @current_frame_index
if b
f, _l = b.source_location
tp_allow_reentry do
b.eval(src, "(rdbg)/#{f}")
end
else
frame_self = current_frame.self
tp_allow_reentry do
frame_self.instance_eval(src)
end
end
ensure
@target_frames = saved_target_frames
@current_frame_index = saved_current_frame_index
end
SPECIAL_LOCAL_VARS = [
[:raised_exception, "_raised"],
[:return_value, "_return"],
]
def frame_eval src, re_raise: false
@success_last_eval = false
b = current_frame.eval_binding
special_local_variables current_frame do |name, var|
b.local_variable_set(name, var) if /\%/ !~ name
end
result = frame_eval_core(src, b)
@success_last_eval = true
result
rescue SystemExit
raise
rescue Exception => e
return yield(e) if block_given?
puts "eval error: #{e}"
e.backtrace_locations&.each do |loc|
break if loc.path == __FILE__
puts " #{loc}"
end
raise if re_raise
end
def get_src(frame,
max_lines:,
start_line: nil,
end_line: nil,
dir: +1)
if file_lines = frame.file_lines
frame_line = frame.location.lineno - 1
lines = file_lines.map.with_index do |e, i|
cur = i == frame_line ? '=>' : ' '
line = colorize_dim('%4d|' % (i+1))
"#{cur}#{line} #{e}"
end
unless start_line
if frame.show_line
if dir > 0
start_line = frame.show_line
else
end_line = frame.show_line - max_lines
start_line = [end_line - max_lines, 0].max
end
else
start_line = [frame_line - max_lines/2, 0].max
end
end
unless end_line
end_line = [start_line + max_lines, lines.size].min
end
if start_line != end_line && max_lines
[start_line, end_line, lines]
end
else # no file lines
nil
end
rescue Exception => e
p e
pp e.backtrace
exit!
end
def show_src(frame_index: @current_frame_index, update_line: false, max_lines: CONFIG[:show_src_lines], **options)
if frame = get_frame(frame_index)
start_line, end_line, lines = *get_src(frame, max_lines: max_lines, **options)
if start_line
if update_line
frame.show_line = end_line
end
puts "[#{start_line+1}, #{end_line}] in #{frame.pretty_path}" if !update_line && max_lines != 1
puts lines[start_line...end_line]
else
puts "# No sourcefile available for #{frame.path}"
end
end
end
def current_frame
get_frame(@current_frame_index)
end
def get_frame(index)
if @target_frames
@target_frames[index]
else
nil
end
end
def collect_locals(frame)
locals = []
if s = frame&.self
locals << ["%self", s]
end
special_local_variables frame do |name, val|
locals << [name, val]
end
if vars = frame&.local_variables
vars.each{|var, val|
locals << [var, val]
}
end
locals
end
## cmd: show
def special_local_variables frame
SPECIAL_LOCAL_VARS.each do |mid, name|
next unless frame&.send("has_#{mid}")
name = name.sub('_', '%') if frame.eval_binding.local_variable_defined?(name)
yield name, frame.send(mid)
end
end
def show_locals pat
collect_locals(current_frame).each do |var, val|
puts_variable_info(var, val, pat)
end
end
def show_ivars pat
if s = current_frame&.self
M_INSTANCE_VARIABLES.bind_call(s).sort.each{|iv|
value = M_INSTANCE_VARIABLE_GET.bind_call(s, iv)
puts_variable_info iv, value, pat
}
end
end
def show_consts pat, only_self: false
if s = current_frame&.self
cs = {}
if M_KIND_OF_P.bind_call(s, Module)
cs[s] = :self
else
s = M_CLASS.bind_call(s)
cs[s] = :self unless only_self
end
unless only_self
s.ancestors.each{|c| break if c == Object; cs[c] = :ancestors}
if b = current_frame&.binding
b.eval('::Module.nesting').each{|c| cs[c] = :nesting unless cs.has_key? c}
end
end
names = {}
cs.each{|c, _|
c.constants(false).sort.each{|name|
next if names.has_key? name
names[name] = nil
value = c.const_get(name)
puts_variable_info name, value, pat
}
}
end
end
SKIP_GLOBAL_LIST = %i[$= $KCODE $-K $SAFE].freeze
def show_globals pat
global_variables.sort.each{|name|
next if SKIP_GLOBAL_LIST.include? name
value = eval(name.to_s)
puts_variable_info name, value, pat
}
end
def puts_variable_info label, obj, pat
return if pat && pat !~ label
begin
inspected = DEBUGGER__.safe_inspect(obj)
rescue Exception => e
inspected = e.inspect
end
mono_info = "#{label} = #{inspected}"
w = SESSION::width
if mono_info.length >= w
maximum_value_width = w - "#{label} = ".length
valstr = truncate(inspected, width: maximum_value_width)
else
valstr = colored_inspect(obj, width: 2 ** 30)
valstr = inspected if valstr.lines.size > 1
end
info = "#{colorize_cyan(label)} = #{valstr}"
puts info
end
def truncate(string, width:)
if string.start_with?("#<")
string[0 .. (width-5)] + '...>'
else
string[0 .. (width-4)] + '...'
end
end
### cmd: show edit
def show_by_editor path = nil
unless path
if current_frame
path = current_frame.path
else
return # can't get path
end
end
if File.exist?(path)
if editor = (ENV['RUBY_DEBUG_EDITOR'] || ENV['EDITOR'])
puts "command: #{editor}"
puts " path: #{path}"
system(editor, path)
else
puts "can not find editor setting: ENV['RUBY_DEBUG_EDITOR'] or ENV['EDITOR']"
end
else
puts "Can not find file: #{path}"
end
end
### cmd: show frames
def show_frames max = nil, pattern = nil
if @target_frames && (max ||= @target_frames.size) > 0
frames = []
@target_frames.each_with_index{|f, i|
# we need to use FrameInfo#matchable_location because #location_str is for display
# and it may change based on configs (e.g. use_short_path)
next if pattern && !(f.name.match?(pattern) || f.matchable_location.match?(pattern))
# avoid using skip_path? because we still want to display internal frames
next if skip_config_skip_path?(f.matchable_location)
frames << [i, f]
}
size = frames.size
max.times{|i|
break unless frames[i]
index, frame = frames[i]
puts frame_str(index, frame: frame)
}
puts " # and #{size - max} frames (use `bt' command for all frames)" if max < size
end
end
def show_frame i=0
puts frame_str(i)
end
def frame_str(i, frame: @target_frames[i])
cur_str = (@current_frame_index == i ? '=>' : ' ')
prefix = "#{cur_str}##{i}"
frame_string = @frame_formatter.call(frame)
"#{prefix}\t#{frame_string}"
end
### cmd: show outline
def show_outline expr
begin
obj = frame_eval(expr, re_raise: true)
rescue Exception
# ignore
else
o = Output.new(@output)
locals = current_frame&.local_variables
klass = M_CLASS.bind_call(obj)
klass = obj if Class == klass || Module == klass
o.dump("constants", obj.constants) if M_RESPOND_TO_P.bind_call(obj, :constants)
outline_method(o, klass, obj)
o.dump("instance variables", M_INSTANCE_VARIABLES.bind_call(obj))
o.dump("class variables", klass.class_variables)
o.dump("locals", locals.keys) if locals
end
end
def outline_method(o, klass, obj)
begin
singleton_class = M_SINGLETON_CLASS.bind_call(obj)
rescue TypeError
singleton_class = nil
end
maps = class_method_map((singleton_class || klass).ancestors)
maps.each do |mod, methods|
name = mod == singleton_class ? "#{klass}.methods" : "#{mod}#methods"
o.dump(name, methods)
end
end
def class_method_map(classes)
dumped = Array.new
classes.reject { |mod| mod >= Object }.map do |mod|
methods = mod.public_instance_methods(false).select do |m|
dumped.push(m) unless dumped.include?(m)
end
[mod, methods]
end.reverse
end
## cmd: breakpoint
# TODO: support non-ASCII Constant name
def constant_name? name
case name
when /\A::\b/
constant_name? $~.post_match
when /\A[A-Z]\w*/
post = $~.post_match
if post.empty?
true
else
constant_name? post
end
else
false
end
end
def make_breakpoint args
case args.first
when :method
klass_name, op, method_name, cond, cmd, path = args[1..]
bp = MethodBreakpoint.new(current_frame.eval_binding, klass_name, op, method_name, cond: cond, command: cmd, path: path)
begin
bp.enable
rescue NameError => e
if bp.klass
puts "Unknown method name: \"#{e.name}\""
else
# klass_name can not be evaluated
if constant_name? klass_name
puts "Unknown constant name: \"#{e.name}\""
else
# only Class name is allowed
puts "Not a constant name: \"#{klass_name}\""
bp = nil
end
end
Session.activate_method_added_trackers if bp
rescue Exception => e
puts e.inspect
bp = nil
end
bp
when :watch
ivar, object, result, cond, command, path = args[1..]
WatchIVarBreakpoint.new(ivar, object, result, cond: cond, command: command, path: path)
else
raise "unknown breakpoint: #{args}"
end
end
class SuspendReplay < Exception
end
def wait_next_action
wait_next_action_
rescue SuspendReplay
replay_suspend
end
def wait_next_action_
# assertions
raise "@mode is #{@mode}" if !waiting?
unless SESSION.active?
pp caller
set_mode :running
return
end
while true
begin
set_mode :waiting if !waiting?
cmds = @q_cmd.pop
# pp [self, cmds: cmds]
break unless cmds
ensure
set_mode :running
end
cmd, *args = *cmds
case cmd
when :continue
break
when :step
step_type = args[0]
iter = args[1]
case step_type
when :in
if @recorder&.replaying?
@recorder.step_forward
raise SuspendReplay
else
step_tp iter do
true
end
break
end
when :next
frame = @target_frames.first
path = frame.location.absolute_path || "!eval:#{frame.path}"
line = frame.location.lineno
if frame.iseq
frame.iseq.traceable_lines_norec(lines = {})
next_line = lines.keys.bsearch{|e| e > line}
if !next_line && (last_line = frame.iseq.last_line) > line
next_line = last_line
end
end
depth = @target_frames.first.frame_depth
step_tp iter do
loc = caller_locations(2, 1).first
loc_path = loc.absolute_path || "!eval:#{loc.path}"
# same stack depth
(DEBUGGER__.frame_depth - 3 <= depth) ||
# different frame
(next_line && loc_path == path &&
(loc_lineno = loc.lineno) > line &&
loc_lineno <= next_line)
end
break
when :finish
finish_frames = (iter || 1) - 1
goal_depth = @target_frames.first.frame_depth - finish_frames
step_tp nil, [:return, :b_return] do
DEBUGGER__.frame_depth - 3 <= goal_depth ? true : false
end
break
when :back
if @recorder&.can_step_back?
unless @recorder.backup_frames
@recorder.backup_frames = @target_frames
end
@recorder.step_back
raise SuspendReplay
else
puts "Can not step back more."
event! :result, nil
end
when :reset
if @recorder&.replaying?
@recorder.step_reset
raise SuspendReplay
end
else
raise "unknown: #{type}"
end
when :eval
eval_type, eval_src = *args
result_type = nil
case eval_type
when :p
result = frame_eval(eval_src)
puts "=> " + color_pp(result, 2 ** 30)
if alloc_path = ObjectSpace.allocation_sourcefile(result)
puts "allocated at #{alloc_path}:#{ObjectSpace.allocation_sourceline(result)}"
end
when :pp
result = frame_eval(eval_src)
puts color_pp(result, SESSION.width)
if alloc_path = ObjectSpace.allocation_sourcefile(result)
puts "allocated at #{alloc_path}:#{ObjectSpace.allocation_sourceline(result)}"
end
when :call
result = frame_eval(eval_src)
when :irb
begin
result = frame_eval('binding.irb')
ensure
# workaround: https://github.com/ruby/debug/issues/308
Reline.prompt_proc = nil if defined? Reline
end
when :display, :try_display
failed_results = []
eval_src.each_with_index{|src, i|
result = frame_eval(src){|e|
failed_results << [i, e.message]
"<error: #{e.message}>"
}
puts "#{i}: #{src} = #{result}"
}
result_type = eval_type
result = failed_results
else
raise "unknown error option: #{args.inspect}"
end
event! :result, result_type, result
when :frame
type, arg = *args
case type
when :up
if @current_frame_index + 1 < @target_frames.size
@current_frame_index += 1
show_src max_lines: 1
show_frame(@current_frame_index)
end
when :down
if @current_frame_index > 0
@current_frame_index -= 1
show_src max_lines: 1
show_frame(@current_frame_index)
end
when :set
if arg
index = arg.to_i
if index >= 0 && index < @target_frames.size
@current_frame_index = index
else
puts "out of frame index: #{index}"
end
end
show_src max_lines: 1
show_frame(@current_frame_index)
else
raise "unsupported frame operation: #{arg.inspect}"
end
event! :result, nil
when :show
type = args.shift
case type
when :backtrace
max_lines, pattern = *args
show_frames max_lines, pattern
when :list
show_src(update_line: true, **(args.first || {}))
when :edit
show_by_editor(args.first)
when :default
pat = args.shift
show_locals pat
show_ivars pat
show_consts pat, only_self: true
when :locals