Class: Ladybug::Debugger
- Inherits:
-
Object
show all
- Extended by:
- Memoist
- Defined in:
- lib/ladybug/debugger.rb
Defined Under Namespace
Classes: InvalidBreakpointLocationError
Instance Method Summary
collapse
Constructor Details
#initialize(preload_paths: []) ⇒ Debugger
preload_paths (optional):
paths to pre-parse into Ruby code so that
later we can quickly respond to requests for breakpoint locations
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/ladybug/debugger.rb', line 17
def initialize(preload_paths: [])
@breakpoints = []
@to_main_thread = Queue.new
@from_main_thread = Queue.new
@on_pause = -> {}
@on_resume = -> {}
@parsed_files = {}
Thread.new do
preload_paths.each do |preload_path|
parse(preload_path)
end
end
end
|
Instance Method Details
#evaluate(expression) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/ladybug/debugger.rb', line 68
def evaluate(expression)
@to_main_thread.push({
command: 'eval',
arguments: {
expression: expression
}
})
@from_main_thread.pop
end
|
#get_possible_breakpoints(path:, start_num:, end_num:) ⇒ Object
Given a filename line number range of a requested breakpoint, give the line numbers of possible breakpoints.
In practice, start and end number tend to be the same when Chrome devtools is the client.
A breakpoint can be set at the beginning of any Ruby statement. (more details in #line_numbers_with_code)
125
126
127
|
# File 'lib/ladybug/debugger.rb', line 125
def get_possible_breakpoints(path:, start_num:, end_num:)
(start_num..end_num).to_a & line_numbers_with_code(path)
end
|
#on_pause(&block) ⇒ Object
44
45
46
|
# File 'lib/ladybug/debugger.rb', line 44
def on_pause(&block)
@on_pause = block
end
|
#on_resume(&block) ⇒ Object
48
49
50
|
# File 'lib/ladybug/debugger.rb', line 48
def on_resume(&block)
@on_resume = block
end
|
#remove_breakpoint(breakpoint_id) ⇒ Object
110
111
112
113
114
115
|
# File 'lib/ladybug/debugger.rb', line 110
def remove_breakpoint(breakpoint_id)
filename, line_number = breakpoint_id.split(":")
line_number = line_number.to_i
@breakpoints.delete_if { |bp| bp[:id] == breakpoint_id }
end
|
#resume ⇒ Object
52
53
54
|
# File 'lib/ladybug/debugger.rb', line 52
def resume
@to_main_thread.push({ command: 'continue' })
end
|
#set_breakpoint(filename:, line_number:) ⇒ Object
Sets a breakpoint and returns a breakpoint object.
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
|
# File 'lib/ladybug/debugger.rb', line 81
def set_breakpoint(filename:, line_number:)
possible_lines = line_numbers_with_code(filename)
if !possible_lines.include?(line_number)
line_number = possible_lines.sort.find { |ln| ln > line_number }
end
if line_number.nil?
raise InvalidBreakpointLocationError,
"invalid breakpoint line: #{filename}:#{line_number}"
end
breakpoint = {
filename: File.absolute_path(filename),
line_number: line_number,
id: "#{filename}:#{line_number}"
}
@breakpoints.push(breakpoint)
breakpoint
end
|
#start ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/ladybug/debugger.rb', line 36
def start
RubyVM::InstructionSequence.compile_option = {
trace_instruction: true
}
set_trace_func trace_func
end
|
#step_into ⇒ Object
60
61
62
|
# File 'lib/ladybug/debugger.rb', line 60
def step_into
@to_main_thread.push({ command: 'step_into' })
end
|
#step_out ⇒ Object
64
65
66
|
# File 'lib/ladybug/debugger.rb', line 64
def step_out
@to_main_thread.push({ command: 'step_out' })
end
|
#step_over ⇒ Object
56
57
58
|
# File 'lib/ladybug/debugger.rb', line 56
def step_over
@to_main_thread.push({ command: 'step_over' })
end
|