Class: Ladybug::Debugger

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/ladybug/debugger.rb

Defined Under Namespace

Classes: InvalidBreakpointLocationError, InvalidExpressionError

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
35
36
# 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 = {}

  @break = nil

  # Todo: consider thread safety of mutating this hash
  Thread.new do
    preload_paths.each do |preload_path|
      parse(preload_path)
    end
  end
end

Instance Method Details

#debugObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ladybug/debugger.rb', line 46

def debug
  RubyVM::InstructionSequence.compile_option = {
    trace_instruction: true
  }
  Thread.current.set_trace_func trace_func

  yield
ensure
  RubyVM::InstructionSequence.compile_option = {
    trace_instruction: false
  }
  Thread.current.set_trace_func nil
end

#evaluate(expression) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ladybug/debugger.rb', line 84

def evaluate(expression)
  raise InvalidExpressionError if !parseable?(expression)

  @to_main_thread.push({
    command: 'eval',
    arguments: {
      expression: expression
    }
  })

  # Block on eval, returns result
  @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)



143
144
145
# File 'lib/ladybug/debugger.rb', line 143

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



60
61
62
# File 'lib/ladybug/debugger.rb', line 60

def on_pause(&block)
  @on_pause = block
end

#on_resume(&block) ⇒ Object



64
65
66
# File 'lib/ladybug/debugger.rb', line 64

def on_resume(&block)
  @on_resume = block
end

#remove_breakpoint(breakpoint_id) ⇒ Object



128
129
130
131
132
133
# File 'lib/ladybug/debugger.rb', line 128

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

#resumeObject



68
69
70
# File 'lib/ladybug/debugger.rb', line 68

def resume
  @to_main_thread.push({ command: 'continue' })
end

#set_breakpoint(filename:, line_number:) ⇒ Object

Sets a breakpoint and returns a breakpoint object.



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
# File 'lib/ladybug/debugger.rb', line 99

def set_breakpoint(filename:, line_number:)
  possible_lines = line_numbers_with_code(filename)

  # It's acceptable for a caller to request setting a breakpoint
  # on a location where it's not possible to set a breakpoint.
  # In this case, we set a breakpoint on the next possible location.
  if !possible_lines.include?(line_number)
    line_number = possible_lines.sort.find { |ln| ln > line_number }
  end

  # Sometimes the above check will fail and there's no possible breakpoint.
  if line_number.nil?
    raise InvalidBreakpointLocationError,
          "invalid breakpoint line: #{filename}:#{line_number}"
  end

  breakpoint = {
    # We need to use the absolute path here
    # because set_trace_func ends up checking against that
    filename: File.absolute_path(filename),
    line_number: line_number,
    id: "#{filename}:#{line_number}"
  }

  @breakpoints.push(breakpoint)

  breakpoint
end

#startObject



38
39
40
41
42
43
44
# File 'lib/ladybug/debugger.rb', line 38

def start
  RubyVM::InstructionSequence.compile_option = {
    trace_instruction: true
  }

  set_trace_func trace_func
end

#step_intoObject



76
77
78
# File 'lib/ladybug/debugger.rb', line 76

def step_into
  @to_main_thread.push({ command: 'step_into' })
end

#step_outObject



80
81
82
# File 'lib/ladybug/debugger.rb', line 80

def step_out
  @to_main_thread.push({ command: 'step_out' })
end

#step_overObject



72
73
74
# File 'lib/ladybug/debugger.rb', line 72

def step_over
  @to_main_thread.push({ command: 'step_over' })
end