Class: Ladybug::Debugger

Inherits:
Object
  • 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 = {}

  # 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

#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
    }
  })

  # 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)



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

#resumeObject



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)

  # 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



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_intoObject



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

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

#step_outObject



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

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

#step_overObject



56
57
58
# File 'lib/ladybug/debugger.rb', line 56

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