Class: CommandServer

Inherits:
Messaging::Server show all
Defined in:
lib/rdb/debugger.rb

Instance Method Summary collapse

Methods inherited from Messaging::Server

#broadcast, inherited, #listen

Constructor Details

#initializeCommandServer

Returns a new instance of CommandServer.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rdb/debugger.rb', line 5

def initialize
  super

  @debug_thread_group = nil

  @access = Mutex.new

  @waiting = false
  @mutex = Mutex.new
  @resource = ConditionVariable.new

  @command_queue = []
end

Instance Method Details

#add_breakpoint(file: nil, line: nil) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/rdb/debugger.rb', line 138

def add_breakpoint(file: nil, line: nil)
  @access.synchronize {
    breakpoint = Byebug::Breakpoint.add(file, line)
    broadcast.breakpoint_created
    breakpoint.id
  }
end

#breakpointsObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rdb/debugger.rb', line 125

def breakpoints
  @access.synchronize {
    Byebug.breakpoints.map do |bp|
      {
        id: bp.id,
        line: bp.pos,
        path: bp.source
      }
    end
  }
end

#eval(expr: nil, frame: nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rdb/debugger.rb', line 108

def eval(expr: nil, frame: nil)
  @access.synchronize {
    while !@waiting
    end

    context = Byebug.thread_context(Thread.main)
    begin
      binding = context.frame_binding(frame)
      value = Inspector.inspect(binding.eval(expr))
      return { success: true, value: value }
    rescue Exception => e
      return { success: false, class: e.class.name, message: e.message }
    end
  }
end

#localsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rdb/debugger.rb', line 90

def locals
  @access.synchronize {
    while !@waiting
    end

    vars = {}
    context = Byebug.thread_context(Thread.main)
    syms = context.frame_binding.eval('local_variables')
    syms.each do |sym|
      name = sym.to_s
      vars[name] = context.frame_binding.eval(name).inspect
    end

    vars
  }
end

#next_commandObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rdb/debugger.rb', line 228

def next_command
  if @command_queue.empty?
    @mutex.synchronize {
      @waiting = true
      # TODO: Fix this synchronization. Using a condition variable here
      # triggers Ruby's deadlock detection, so for now, we use a limited
      # sleep with repeated checking.
      while @command_queue.empty?
        sleep 0.1
      end
      @waiting = false
    }
  end

  return @command_queue.shift
end

#pauseObject



156
157
158
159
160
161
162
163
# File 'lib/rdb/debugger.rb', line 156

def pause
  @access.synchronize {
    context = Byebug.thread_context(Thread.main)
    context.interrupt

    false
  }
end

#processObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rdb/debugger.rb', line 77

def process
  @access.synchronize {
    {
      id: Process.pid,
      argv: ARGV,
      script: $0,
      env: ENV.to_h,
      config: RbConfig::CONFIG
    }
  }
end

#remove_breakpoint(id: nil) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/rdb/debugger.rb', line 147

def remove_breakpoint(id: nil)
  @access.synchronize {
    breakpoint = Byebug::Breakpoint.remove(id)
    broadcast.breakpoint_deleted
    true
  }
end

#resumeObject



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rdb/debugger.rb', line 166

def resume
  @access.synchronize {
    @command_queue << proc { |context|
      true
    }

    @mutex.synchronize {
      @resource.signal
    }

    true
  }
end

#running=(running) ⇒ Object



252
253
254
# File 'lib/rdb/debugger.rb', line 252

def running=(running)
  @running = running
end

#running?Boolean

Returns:

  • (Boolean)


246
247
248
249
250
# File 'lib/rdb/debugger.rb', line 246

def running?
  @access.synchronize {
    @running
  }
end

#startObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/rdb/debugger.rb', line 19

def start
  @debug_thread_group = group = ThreadGroup.new

  thread = Byebug::DebugThread.new do
    listen('0.0.0.0', 4444)
  end
  
  group.add(thread)
  group.enclose
end

#step_inObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rdb/debugger.rb', line 181

def step_in
  @access.synchronize {
    @command_queue << proc { |context|
      context.step_into(1)
      true
    }

    @mutex.synchronize {
      @resource.signal
    }

    true
  }
end

#step_outObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rdb/debugger.rb', line 213

def step_out
  @access.synchronize {
    @command_queue << proc { |context|
      context.step_out(1)
      true
    }

    @mutex.synchronize {
      @resource.signal
    }

    true
  }
end

#step_overObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rdb/debugger.rb', line 197

def step_over
  @access.synchronize {
    @command_queue << proc { |context|
      context.step_over(1)
      true
    }

    @mutex.synchronize {
      @resource.signal
    }

    true
  }
end

#threadsObject



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

def threads
  @access.synchronize {
    # TODO: Better sync
    while !@waiting
    end

    threads = []
    Thread.list.each do |t|
      next if ignored? t
      
      backtrace = []

      context = Byebug.thread_context(t)
      stack_size = context.calced_stack_size

      (0...stack_size).each do |i|
        path = File.expand_path(context.frame_file(i))
        line = context.frame_line(i)
        klass = context.frame_class(i).to_s
        klass = nil if klass.empty?
        method = context.frame_method(i)

        backtrace << {
          path: path,
          line: line,
          class: klass,
          method: method,
        }
      end

      threads << {
        id: context.thnum,
        main: t == Thread.main,
        status: t.status,
        alive: t.alive?,
        priority: t.priority,
        safe_level: t.safe_level,
        backtrace: backtrace
      }
    end

    threads
  }
end