-
Notifications
You must be signed in to change notification settings - Fork 127
/
session.rb
2193 lines (1881 loc) · 53.9 KB
/
session.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
# skip to load debugger for bundle exec
if $0.end_with?('bin/bundle') && ARGV.first == 'exec'
trace_var(:$0) do |file|
trace_var(:$0, nil)
if /-r (#{__dir__}\S+)/ =~ ENV['RUBYOPT']
lib = $1
$LOADED_FEATURES.delete_if{|path| path.start_with?(__dir__)}
ENV['RUBY_DEBUG_INITIAL_SUSPEND_PATH'] = file
require lib
ENV['RUBY_DEBUG_INITIAL_SUSPEND_PATH'] = nil
end
end
return
end
require_relative 'frame_info'
require_relative 'config'
require_relative 'thread_client'
require_relative 'source_repository'
require_relative 'breakpoint'
require_relative 'tracer'
# To prevent loading old lib/debug.rb in Ruby 2.6 to 3.0
$LOADED_FEATURES << 'debug.rb'
$LOADED_FEATURES << File.expand_path(File.join(__dir__, '..', 'debug.rb'))
require 'debug' # invalidate the $LOADED_FEATURE cache
require 'json' if ENV['RUBY_DEBUG_TEST_MODE']
class RubyVM::InstructionSequence
def traceable_lines_norec lines
code = self.to_a[13]
line = 0
code.each{|e|
case e
when Integer
line = e
when Symbol
if /\ARUBY_EVENT_/ =~ e.to_s
lines[line] = [e, *lines[line]]
end
end
}
end
def traceable_lines_rec lines
self.each_child{|ci| ci.traceable_lines_rec(lines)}
traceable_lines_norec lines
end
def type
self.to_a[9]
end
def argc
self.to_a[4][:arg_size]
end
def locals
self.to_a[10]
end
def last_line
self.to_a[4][:code_location][2]
end
def first_line
self.to_a[4][:code_location][0]
end
end
module DEBUGGER__
PresetCommand = Struct.new(:commands, :source, :auto_continue)
class PostmortemError < RuntimeError; end
class Session
attr_reader :intercepted_sigint_cmd, :process_group
def initialize ui
@ui = ui
@sr = SourceRepository.new
@bps = {} # bp.key => bp
# [file, line] => LineBreakpoint
# "Error" => CatchBreakpoint
# "Foo#bar" => MethodBreakpoint
# [:watch, ivar] => WatchIVarBreakpoint
# [:check, expr] => CheckBreakpoint
#
@tracers = {}
@th_clients = {} # {Thread => ThreadClient}
@q_evt = Queue.new
@displays = []
@tc = nil
@tc_id = 0
@preset_command = nil
@postmortem_hook = nil
@postmortem = false
@intercept_trap_sigint = false
@intercepted_sigint_cmd = 'DEFAULT'
@process_group = ProcessGroup.new
@subsession = nil
@frame_map = {} # {id => [threadId, frame_depth]} for DAP
@var_map = {1 => [:globals], } # {id => ...} for DAP
@src_map = {} # {id => src}
@script_paths = [File.absolute_path($0)] # for CDP
@scope_map = {} # { object_id => scope } for CDP
@tp_thread_begin = nil
@tp_load_script = TracePoint.new(:script_compiled){|tp|
ThreadClient.current.on_load tp.instruction_sequence, tp.eval_script
}
@tp_load_script.enable
@thread_stopper = thread_stopper
activate
self.postmortem = CONFIG[:postmortem]
end
def active?
!@q_evt.closed?
end
def break_at? file, line
@bps.has_key? [file, line]
end
def activate on_fork: false
@tp_thread_begin&.disable
@tp_thread_begin = nil
if on_fork
@ui.activate self, on_fork: true
else
@ui.activate self, on_fork: false
end
q = Queue.new
@session_server = Thread.new do
Thread.current.name = 'DEBUGGER__::SESSION@server'
Thread.current.abort_on_exception = true
# Thread management
setup_threads
thc = get_thread_client Thread.current
thc.mark_as_management
if @ui.respond_to?(:reader_thread) && thc = get_thread_client(@ui.reader_thread)
thc.mark_as_management
end
@tp_thread_begin = TracePoint.new(:thread_begin) do |tp|
get_thread_client
end
@tp_thread_begin.enable
# session start
q << true
session_server_main
end
q.pop
end
def deactivate
get_thread_client.deactivate
@thread_stopper.disable
@tp_load_script.disable
@tp_thread_begin.disable
@bps.each_value{|bp| bp.disable}
@th_clients.each_value{|thc| thc.close}
@tracers.values.each{|t| t.disable}
@q_evt.close
@ui&.deactivate
@ui = nil
end
def reset_ui ui
@ui.deactivate
@ui = ui
end
def pop_event
@q_evt.pop
end
def session_server_main
while evt = pop_event
process_event evt
end
ensure
deactivate
end
def process_event evt
# variable `@internal_info` is only used for test
tc, output, ev, @internal_info, *ev_args = evt
output.each{|str| @ui.puts str} if ev != :suspend
case ev
when :thread_begin # special event, tc is nil
th = ev_args.shift
q = ev_args.shift
on_thread_begin th
q << true
when :init
wait_command_loop tc
when :load
iseq, src = ev_args
on_load iseq, src
@ui.event :load
tc << :continue
when :trace
trace_id, msg = ev_args
if t = @tracers.values.find{|t| t.object_id == trace_id}
t.puts msg
end
tc << :continue
when :suspend
enter_subsession if ev_args.first != :replay
output.each{|str| @ui.puts str}
case ev_args.first
when :breakpoint
bp, i = bp_index ev_args[1]
clean_bps unless bp
@ui.event :suspend_bp, i, bp, tc.id
when :trap
@ui.event :suspend_trap, sig = ev_args[1], tc.id
if sig == :SIGINT && (@intercepted_sigint_cmd.kind_of?(Proc) || @intercepted_sigint_cmd.kind_of?(String))
@ui.puts "#{@intercepted_sigint_cmd.inspect} is registered as SIGINT handler."
@ui.puts "`sigint` command execute it."
end
else
@ui.event :suspended, tc.id
end
if @displays.empty?
wait_command_loop tc
else
tc << [:eval, :display, @displays]
end
when :result
raise "[BUG] not in subsession" unless @subsession
case ev_args.first
when :try_display
failed_results = ev_args[1]
if failed_results.size > 0
i, _msg = failed_results.last
if i+1 == @displays.size
@ui.puts "canceled: #{@displays.pop}"
end
end
when :method_breakpoint, :watch_breakpoint
bp = ev_args[1]
if bp
add_bp(bp)
show_bps bp
else
# can't make a bp
end
when :trace_pass
obj_id = ev_args[1]
obj_inspect = ev_args[2]
opt = ev_args[3]
add_tracer ObjectTracer.new(@ui, obj_id, obj_inspect, **opt)
else
# ignore
end
wait_command_loop tc
when :dap_result
dap_event ev_args # server.rb
wait_command_loop tc
when :cdp_result
cdp_event ev_args
wait_command_loop tc
end
end
def add_preset_commands name, cmds, kick: true, continue: true
cs = cmds.map{|c|
c.each_line.map{|line|
line = line.strip.gsub(/\A\s*\#.*/, '').strip
line unless line.empty?
}.compact
}.flatten.compact
if @preset_command && !@preset_command.commands.empty?
@preset_command.commands += cs
else
@preset_command = PresetCommand.new(cs, name, continue)
end
ThreadClient.current.on_init name if kick
end
def source iseq
if !CONFIG[:no_color]
@sr.get_colored(iseq)
else
@sr.get(iseq)
end
end
def inspect
"DEBUGGER__::SESSION"
end
def wait_command_loop tc
@tc = tc
loop do
case wait_command
when :retry
# nothing
else
break
end
rescue Interrupt
@ui.puts "\n^C"
retry
end
end
def prompt
if @postmortem
'(rdbg:postmortem) '
elsif @process_group.multi?
"(rdbg@#{process_info}) "
else
'(rdbg) '
end
end
def wait_command
if @preset_command
if @preset_command.commands.empty?
if @preset_command.auto_continue
@preset_command = nil
leave_subsession :continue
return
else
@preset_command = nil
return :retry
end
else
line = @preset_command.commands.shift
@ui.puts "(rdbg:#{@preset_command.source}) #{line}"
end
else
@ui.puts "INTERNAL_INFO: #{JSON.generate(@internal_info)}" if ENV['RUBY_DEBUG_TEST_MODE']
line = @ui.readline prompt
end
case line
when String
process_command line
when Hash
process_protocol_request line # defined in server.rb
else
raise "unexpected input: #{line.inspect}"
end
end
def process_command line
if line.empty?
if @repl_prev_line
line = @repl_prev_line
else
return :retry
end
else
@repl_prev_line = line
end
/([^\s]+)(?:\s+(.+))?/ =~ line
cmd, arg = $1, $2
# p cmd: [cmd, *arg]
case cmd
### Control flow
# * `s[tep]`
# * Step in. Resume the program until next breakable point.
# * `s[tep] <n>`
# * Step in, resume the program at `<n>`th breakable point.
when 's', 'step'
cancel_auto_continue
check_postmortem
step_command :in, arg
# * `n[ext]`
# * Step over. Resume the program until next line.
# * `n[ext] <n>`
# * Step over, same as `step <n>`.
when 'n', 'next'
cancel_auto_continue
check_postmortem
step_command :next, arg
# * `fin[ish]`
# * Finish this frame. Resume the program until the current frame is finished.
# * `fin[ish] <n>`
# * Finish `<n>`th frames.
when 'fin', 'finish'
cancel_auto_continue
check_postmortem
if arg&.to_i == 0
raise 'finish command with 0 does not make sense.'
end
step_command :finish, arg
# * `c[ontinue]`
# * Resume the program.
when 'c', 'continue'
cancel_auto_continue
leave_subsession :continue
# * `q[uit]` or `Ctrl-D`
# * Finish debugger (with the debuggee process on non-remote debugging).
when 'q', 'quit'
if ask 'Really quit?'
@ui.quit arg.to_i
leave_subsession :continue
else
return :retry
end
# * `q[uit]!`
# * Same as q[uit] but without the confirmation prompt.
when 'q!', 'quit!'
@ui.quit arg.to_i
leave_subsession nil
# * `kill`
# * Stop the debuggee process with `Kernel#exit!`.
when 'kill'
if ask 'Really kill?'
exit! (arg || 1).to_i
else
return :retry
end
# * `kill!`
# * Same as kill but without the confirmation prompt.
when 'kill!'
exit! (arg || 1).to_i
# * `sigint`
# * Execute SIGINT handler registered by the debuggee.
# * Note that this command should be used just after stop by `SIGINT`.
when 'sigint'
begin
case cmd = @intercepted_sigint_cmd
when nil, 'IGNORE', :IGNORE, 'DEFAULT', :DEFAULT
# ignore
when String
eval(cmd)
when Proc
cmd.call
end
leave_subsession :continue
rescue Exception => e
@ui.puts "Exception: #{e}"
@ui.puts e.backtrace.map{|line| " #{e}"}
return :retry
end
### Breakpoint
# * `b[reak]`
# * Show all breakpoints.
# * `b[reak] <line>`
# * Set breakpoint on `<line>` at the current frame's file.
# * `b[reak] <file>:<line>` or `<file> <line>`
# * Set breakpoint on `<file>:<line>`.
# * `b[reak] <class>#<name>`
# * Set breakpoint on the method `<class>#<name>`.
# * `b[reak] <expr>.<name>`
# * Set breakpoint on the method `<expr>.<name>`.
# * `b[reak] ... if: <expr>`
# * break if `<expr>` is true at specified location.
# * `b[reak] ... pre: <command>`
# * break and run `<command>` before stopping.
# * `b[reak] ... do: <command>`
# * break and run `<command>`, and continue.
# * `b[reak] if: <expr>`
# * break if: `<expr>` is true at any lines.
# * Note that this feature is super slow.
when 'b', 'break'
check_postmortem
if arg == nil
show_bps
return :retry
else
case bp = repl_add_breakpoint(arg)
when :noretry
when nil
return :retry
else
show_bps bp
return :retry
end
end
# skip
when 'bv'
check_postmortem
require 'json'
h = Hash.new{|h, k| h[k] = []}
@bps.each_value{|bp|
if LineBreakpoint === bp
h[bp.path] << {lnum: bp.line}
end
}
if h.empty?
# TODO: clean?
else
open(".rdb_breakpoints.json", 'w'){|f| JSON.dump(h, f)}
end
vimsrc = File.join(__dir__, 'bp.vim')
system("vim -R -S #{vimsrc} #{@tc.location.path}")
if File.exist?(".rdb_breakpoints.json")
pp JSON.load(File.read(".rdb_breakpoints.json"))
end
return :retry
# * `catch <Error>`
# * Set breakpoint on raising `<Error>`.
# * `catch ... if: <expr>`
# * stops only if `<expr>` is true as well.
# * `catch ... pre: <command>`
# * runs `<command>` before stopping.
# * `catch ... do: <command>`
# * stops and run `<command>`, and continue.
when 'catch'
check_postmortem
if arg
bp = repl_add_catch_breakpoint arg
show_bps bp if bp
else
show_bps
end
return :retry
# * `watch @ivar`
# * Stop the execution when the result of current scope's `@ivar` is changed.
# * Note that this feature is super slow.
# * `watch ... if: <expr>`
# * stops only if `<expr>` is true as well.
# * `watch ... pre: <command>`
# * runs `<command>` before stopping.
# * `watch ... do: <command>`
# * stops and run `<command>`, and continue.
when 'wat', 'watch'
check_postmortem
if arg && arg.match?(/\A@\w+/)
repl_add_watch_breakpoint(arg)
else
show_bps
return :retry
end
# * `del[ete]`
# * delete all breakpoints.
# * `del[ete] <bpnum>`
# * delete specified breakpoint.
when 'del', 'delete'
check_postmortem
bp =
case arg
when nil
show_bps
if ask "Remove all breakpoints?", 'N'
delete_bp
end
when /\d+/
delete_bp arg.to_i
else
nil
end
@ui.puts "deleted: \##{bp[0]} #{bp[1]}" if bp
return :retry
### Information
# * `bt` or `backtrace`
# * Show backtrace (frame) information.
# * `bt <num>` or `backtrace <num>`
# * Only shows first `<num>` frames.
# * `bt /regexp/` or `backtrace /regexp/`
# * Only shows frames with method name or location info that matches `/regexp/`.
# * `bt <num> /regexp/` or `backtrace <num> /regexp/`
# * Only shows first `<num>` frames with method name or location info that matches `/regexp/`.
when 'bt', 'backtrace'
case arg
when /\A(\d+)\z/
@tc << [:show, :backtrace, arg.to_i, nil]
when /\A\/(.*)\/\z/
pattern = $1
@tc << [:show, :backtrace, nil, Regexp.compile(pattern)]
when /\A(\d+)\s+\/(.*)\/\z/
max, pattern = $1, $2
@tc << [:show, :backtrace, max.to_i, Regexp.compile(pattern)]
else
@tc << [:show, :backtrace, nil, nil]
end
# * `l[ist]`
# * Show current frame's source code.
# * Next `list` command shows the successor lines.
# * `l[ist] -`
# * Show predecessor lines as opposed to the `list` command.
# * `l[ist] <start>` or `l[ist] <start>-<end>`
# * Show current frame's source code from the line <start> to <end> if given.
when 'l', 'list'
case arg ? arg.strip : nil
when /\A(\d+)\z/
@tc << [:show, :list, {start_line: arg.to_i - 1}]
when /\A-\z/
@tc << [:show, :list, {dir: -1}]
when /\A(\d+)-(\d+)\z/
@tc << [:show, :list, {start_line: $1.to_i - 1, end_line: $2.to_i}]
when nil
@tc << [:show, :list]
else
@ui.puts "Can not handle list argument: #{arg}"
return :retry
end
# * `edit`
# * Open the current file on the editor (use `EDITOR` environment variable).
# * Note that edited file will not be reloaded.
# * `edit <file>`
# * Open <file> on the editor.
when 'edit'
if @ui.remote?
@ui.puts "not supported on the remote console."
return :retry
end
begin
arg = resolve_path(arg) if arg
rescue Errno::ENOENT
@ui.puts "not found: #{arg}"
return :retry
end
@tc << [:show, :edit, arg]
# * `i[nfo]`
# * Show information about current frame (local/instance variables and defined constants).
# * `i[nfo] l[ocal[s]]`
# * Show information about the current frame (local variables)
# * It includes `self` as `%self` and a return value as `%return`.
# * `i[nfo] i[var[s]]` or `i[nfo] instance`
# * Show information about instance variables about `self`.
# * `i[nfo] c[onst[s]]` or `i[nfo] constant[s]`
# * Show information about accessible constants except toplevel constants.
# * `i[nfo] g[lobal[s]]`
# * Show information about global variables
# * `i[nfo] ... </pattern/>`
# * Filter the output with `</pattern/>`.
# * `i[nfo] th[read[s]]`
# * Show all threads (same as `th[read]`).
when 'i', 'info'
if /\/(.+)\/\z/ =~ arg
pat = Regexp.compile($1)
sub = $~.pre_match.strip
else
sub = arg
end
case sub
when nil
@tc << [:show, :default, pat] # something useful
when 'l', /^locals?/
@tc << [:show, :locals, pat]
when 'i', /^ivars?/i, /^instance[_ ]variables?/i
@tc << [:show, :ivars, pat]
when 'c', /^consts?/i, /^constants?/i
@tc << [:show, :consts, pat]
when 'g', /^globals?/i, /^global[_ ]variables?/i
@tc << [:show, :globals, pat]
when 'th', /threads?/
thread_list
return :retry
else
@ui.puts "unrecognized argument for info command: #{arg}"
show_help 'info'
return :retry
end
# * `o[utline]` or `ls`
# * Show you available methods, constants, local variables, and instance variables in the current scope.
# * `o[utline] <expr>` or `ls <expr>`
# * Show you available methods and instance variables of the given object.
# * If the object is a class/module, it also lists its constants.
when 'outline', 'o', 'ls'
@tc << [:show, :outline, arg]
# * `display`
# * Show display setting.
# * `display <expr>`
# * Show the result of `<expr>` at every suspended timing.
when 'display'
if arg && !arg.empty?
@displays << arg
@tc << [:eval, :try_display, @displays]
else
@tc << [:eval, :display, @displays]
end
# * `undisplay`
# * Remove all display settings.
# * `undisplay <displaynum>`
# * Remove a specified display setting.
when 'undisplay'
case arg
when /(\d+)/
if @displays[n = $1.to_i]
@displays.delete_at n
end
@tc << [:eval, :display, @displays]
when nil
if ask "clear all?", 'N'
@displays.clear
end
return :retry
end
### Frame control
# * `f[rame]`
# * Show the current frame.
# * `f[rame] <framenum>`
# * Specify a current frame. Evaluation are run on specified frame.
when 'frame', 'f'
@tc << [:frame, :set, arg]
# * `up`
# * Specify the upper frame.
when 'up'
@tc << [:frame, :up]
# * `down`
# * Specify the lower frame.
when 'down'
@tc << [:frame, :down]
### Evaluate
# * `p <expr>`
# * Evaluate like `p <expr>` on the current frame.
when 'p'
@tc << [:eval, :p, arg.to_s]
# * `pp <expr>`
# * Evaluate like `pp <expr>` on the current frame.
when 'pp'
@tc << [:eval, :pp, arg.to_s]
# * `eval <expr>`
# * Evaluate `<expr>` on the current frame.
when 'eval', 'call'
if arg == nil || arg.empty?
show_help 'eval'
@ui.puts "\nTo evaluate the variable `#{cmd}`, use `pp #{cmd}` instead."
return :retry
else
@tc << [:eval, :call, arg]
end
# * `irb`
# * Invoke `irb` on the current frame.
when 'irb'
if @ui.remote?
@ui.puts "not supported on the remote console."
return :retry
end
@tc << [:eval, :irb]
# don't repeat irb command
@repl_prev_line = nil
### Trace
# * `trace`
# * Show available tracers list.
# * `trace line`
# * Add a line tracer. It indicates line events.
# * `trace call`
# * Add a call tracer. It indicate call/return events.
# * `trace exception`
# * Add an exception tracer. It indicates raising exceptions.
# * `trace object <expr>`
# * Add an object tracer. It indicates that an object by `<expr>` is passed as a parameter or a receiver on method call.
# * `trace ... </pattern/>`
# * Indicates only matched events to `</pattern/>` (RegExp).
# * `trace ... into: <file>`
# * Save trace information into: `<file>`.
# * `trace off <num>`
# * Disable tracer specified by `<num>` (use `trace` command to check the numbers).
# * `trace off [line|call|pass]`
# * Disable all tracers. If `<type>` is provided, disable specified type tracers.
when 'trace'
if (re = /\s+into:\s*(.+)/) =~ arg
into = $1
arg.sub!(re, '')
end
if (re = /\s\/(.+)\/\z/) =~ arg
pattern = $1
arg.sub!(re, '')
end
case arg
when nil
@ui.puts 'Tracers:'
@tracers.values.each_with_index{|t, i|
@ui.puts "* \##{i} #{t}"
}
@ui.puts
return :retry
when /\Aline\z/
add_tracer LineTracer.new(@ui, pattern: pattern, into: into)
return :retry
when /\Acall\z/
add_tracer CallTracer.new(@ui, pattern: pattern, into: into)
return :retry
when /\Aexception\z/
add_tracer ExceptionTracer.new(@ui, pattern: pattern, into: into)
return :retry
when /\Aobject\s+(.+)/
@tc << [:trace, :object, $1.strip, {pattern: pattern, into: into}]
when /\Aoff\s+(\d+)\z/
if t = @tracers.values[$1.to_i]
t.disable
@ui.puts "Disable #{t.to_s}"
else
@ui.puts "Unmatched: #{$1}"
end
return :retry
when /\Aoff(\s+(line|call|exception|object))?\z/
@tracers.values.each{|t|
if $2.nil? || t.type == $2
t.disable
@ui.puts "Disable #{t.to_s}"
end
}
return :retry
else
@ui.puts "Unknown trace option: #{arg.inspect}"
return :retry
end
# Record
# * `record`
# * Show recording status.
# * `record [on|off]`
# * Start/Stop recording.
# * `step back`
# * Start replay. Step back with the last execution log.
# * `s[tep]` does stepping forward with the last log.
# * `step reset`
# * Stop replay .
when 'record'
case arg
when nil, 'on', 'off'
@tc << [:record, arg&.to_sym]
else
@ui.puts "unknown command: #{arg}"
return :retry
end
### Thread control
# * `th[read]`
# * Show all threads.
# * `th[read] <thnum>`
# * Switch thread specified by `<thnum>`.
when 'th', 'thread'
case arg
when nil, 'list', 'l'
thread_list
when /(\d+)/
switch_thread $1.to_i
else
@ui.puts "unknown thread command: #{arg}"
end
return :retry
### Configuration
# * `config`
# * Show all configuration with description.
# * `config <name>`
# * Show current configuration of <name>.
# * `config set <name> <val>` or `config <name> = <val>`
# * Set <name> to <val>.
# * `config append <name> <val>` or `config <name> << <val>`
# * Append `<val>` to `<name>` if it is an array.
# * `config unset <name>`
# * Set <name> to default.
when 'config'
config_command arg
return :retry
# * `source <file>`
# * Evaluate lines in `<file>` as debug commands.
when 'source'
if arg
begin
cmds = File.readlines(path = File.expand_path(arg))
add_preset_commands path, cmds, kick: true, continue: false
rescue Errno::ENOENT
@ui.puts "File not found: #{arg}"
end
else
show_help 'source'
end
return :retry
# * `open`
# * open debuggee port on UNIX domain socket and wait for attaching.
# * Note that `open` command is EXPERIMENTAL.
# * `open [<host>:]<port>`
# * open debuggee port on TCP/IP with given `[<host>:]<port>` and wait for attaching.
# * `open vscode`
# * open debuggee port for VSCode and launch VSCode if available.
# * `open chrome`
# * open debuggee port for Chrome and wait for attaching.
when 'open'
case arg&.downcase
when '', nil
repl_open_unix
when 'vscode'
repl_open_vscode
when /\A(.+):(\d+)\z/
repl_open_tcp $1, $2.to_i
when /\A(\d+)z/
repl_open_tcp nil, $1.to_i
when 'tcp'
repl_open_tcp CONFIG[:host], (CONFIG[:port] || 0)
when 'chrome', 'cdp'
CONFIG[:open_frontend] = 'chrome'
repl_open_tcp CONFIG[:host], (CONFIG[:port] || 0)
else
raise "Unknown arg: #{arg}"
end
return :retry
### Help
# * `h[elp]`
# * Show help for all commands.
# * `h[elp] <command>`
# * Show help for the given command.
when 'h', 'help', '?'
if arg
show_help arg
else