Class: Rubinius::SetTrace

Inherits:
Object
  • Object
show all
Defined in:
lib/set_trace.rb,
app/breakpoint.rb,
app/stepping.rb,
app/frame.rb

Overview

A Rubinius implementation of set_trace_func.

This code is wired into the debugging APIs provided by Rubinius. It tries to isolate the complicated stepping logic as well as simulate Ruby’s set_trace_func.

Defined Under Namespace

Classes: BreakPoint, Frame

Constant Summary collapse

VERSION =
'0.0.4'
DEFAULT_SET_TRACE_FUNC_OPTS =
{
  :callback_style => :classic,
  :step_to_parent => :true
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSetTrace

Create a new object. Stepping starts up a thread which is where the callback executes from. Other threads are told that their debugging thread is the stepping thread, and this control of execution is handled.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/set_trace.rb', line 30

def initialize()
  @file_lines = Hash.new do |hash, path|
    if File.exists? path
      hash[path] = File.readlines(path)
    else
      ab_path = File.join(@root_dir, path)
      if File.exists? ab_path
        hash[path] = File.readlines(ab_path)
      else
        hash[path] = []
      end
    end
  end

  @thread = nil
  @frames = []
  @user_variables = 0
  @breakpoints = []
end

Instance Attribute Details

#breakpointsObject (readonly)

Returns the value of attribute breakpoints.



50
51
52
# File 'lib/set_trace.rb', line 50

def breakpoints
  @breakpoints
end

#current_frameObject (readonly)

Returns the value of attribute current_frame.



50
51
52
# File 'lib/set_trace.rb', line 50

def current_frame
  @current_frame
end

#user_variablesObject (readonly)

Returns the value of attribute user_variables.



50
51
52
# File 'lib/set_trace.rb', line 50

def user_variables
  @user_variables
end

#variablesObject (readonly)

Returns the value of attribute variables.



50
51
52
# File 'lib/set_trace.rb', line 50

def variables
  @variables
end

#vm_locationsObject (readonly)

Returns the value of attribute vm_locations.



51
52
53
# File 'lib/set_trace.rb', line 51

def vm_locations
  @vm_locations
end

Class Method Details

.global(opts = {}) ⇒ Object



53
54
55
# File 'lib/set_trace.rb', line 53

def self.global(opts={})
  @global ||= new
end

.set_trace_func(callback_method, opts = {}) ⇒ Object



57
58
59
60
61
# File 'lib/set_trace.rb', line 57

def self.set_trace_func(callback_method, opts={})
  opts = Rubinius::SetTrace::DEFAULT_SET_TRACE_FUNC_OPTS.merge(opts)
  opts[:call_offset] ||= 1
  global.set_trace_func(callback_method, opts)
end

Instance Method Details

#call_callbackObject

call callback



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/set_trace.rb', line 135

def call_callback
  case @opts[:callback_style]
  when :classic
    line = @current_frame.line
    file = @current_frame.file
    meth = @current_frame.method
    binding = @current_frame.binding
    id = nil
    classname = nil
    @callback_method.call(@event, file, line, id, binding, classname)
  else
    @callback_method.call(@event, @vm_locations)
  end
  if @tracing
    if @opts[:step_to_parent] 
      @opts[:step_to_parent] = false
      step_to_parent 
    else 
      step_over_by(1)
    end
  end
  listen
end

#decode_oneObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/set_trace.rb', line 210

def decode_one
  ip = @current_frame.ip

  meth = @current_frame.method
  decoder = Rubinius::InstructionDecoder.new(meth.iseq)
  partial = decoder.decode_between(ip, ip+1)

  partial.each do |ins|
    op = ins.shift

    ins.each_index do |i|
      case op.args[i]
      when :literal
        ins[i] = meth.literals[ins[i]].inspect
      when :local
        if meth.local_names
          ins[i] = meth.local_names[ins[i]]
        end
      end
    end

    display "ip #{ip} = #{op.opcode} #{ins.join(', ')}"
  end
end

#delete_breakpoint(i) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/set_trace.rb', line 163

def delete_breakpoint(i)
  bp = @breakpoints[i-1]

  unless bp
    STDERR.puts "Unknown breakpoint '#{i}'"
    return
  end

  bp.delete!

  @breakpoints[i-1] = nil
end

#frame(num) ⇒ Object



159
160
161
# File 'lib/set_trace.rb', line 159

def frame(num)
  @frames[num] ||= Frame.new(self, num, @vm_locations[num])
end

#goto_between(exec, start, fin) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/stepping.rb', line 5

def goto_between(exec, start, fin)
  goto = Rubinius::InstructionSet.opcodes_map[:goto]
  git  = Rubinius::InstructionSet.opcodes_map[:goto_if_true]
  gif  = Rubinius::InstructionSet.opcodes_map[:goto_if_false]
  
  iseq = exec.iseq
  
  i = start
  while i < fin
    op = iseq[i]
    case op
    when goto
      return next_interesting(exec, iseq[i + 1]) # goto target
    when git, gif
      return [next_interesting(exec, iseq[i + 1]),
              next_interesting(exec, i + 2)] # target and next ip
    else
      op = Rubinius::InstructionSet[op]
      i += (op.arg_count + 1)
    end
  end
  
  return next_interesting(exec, fin)
end

#listen(step_into = false) ⇒ Object

Stop and wait for a debuggee thread to send us info about stopping at a breakpoint.



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

def listen(step_into=false)
  while true
    if @channel
      if step_into
        @channel << :step
      else
        @channel << true
      end
    end

    # Wait for someone to stop
    @breakpoint, @debugee_thread, @channel, @vm_locations = 
      @local_channel.receive

    # Uncache all frames since we stopped at a new place
    @frames = []

    @current_frame = frame(0)

    if @breakpoint
      @event = @breakpoint.event
      # Only break out if the hit was valid
      break if @breakpoint.hit!(@vm_locations.first)
    else
      @event = 'call'
      break
    end
  end

  # puts
  # puts "Breakpoint: #{@current_frame.describe}"
  # show_code

end

#next_interesting(exec, ip) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'app/stepping.rb', line 30

def next_interesting(exec, ip)
  pop = Rubinius::InstructionSet.opcodes_map[:pop]
  
  if exec.iseq[ip] == pop
    return ip + 1
  end
  
  return ip
end

#send_between(exec, start, fin) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/set_trace.rb', line 176

def send_between(exec, start, fin)
  ss = Rubinius::InstructionSet.opcodes_map[:send_stack]
  sm = Rubinius::InstructionSet.opcodes_map[:send_method]
  sb = Rubinius::InstructionSet.opcodes_map[:send_stack_with_block]

  iseq = exec.iseq

  fin = iseq.size if fin < 0

  i = start
  while i < fin
    op = iseq[i]
    case op
    when ss, sm, sb
      return exec.literals[iseq[i + 1]]
    else
      op = Rubinius::InstructionSet[op]
      i += (op.arg_count + 1)
    end
  end

  return nil
end

#set_breakpoints_between(exec, start_ip, fin_ip) ⇒ Object



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
# File 'app/stepping.rb', line 40

def set_breakpoints_between(exec, start_ip, fin_ip)
  ips = goto_between(exec, start_ip, fin_ip)
  if ips.kind_of? Fixnum
    ip = ips
  else
    one, two = ips
    bp1 = BreakPoint.for_ip(exec, one)
    bp2 = BreakPoint.for_ip(exec, two)
    
    bp1.paired_with(bp2)
    bp2.paired_with(bp1)
    
    bp1.for_step!(current_frame.variables)
    bp2.for_step!(current_frame.variables)
    
    bp1.activate
    bp2.activate
    
    return bp1
  end
  
  if ip == -1
    STDERR.puts "No place to step to"
    return nil
  end
  
  bp = BreakPoint.for_ip(exec, ip)
  bp.for_step!(current_frame.variables)
  bp.activate
  
  return bp
end

#set_trace_func(callback_method, opts = {}) ⇒ Object

Startup the stepping, skipping back opts[:call_offset] frames. This lets you start stepping straight into caller’s method.



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
# File 'lib/set_trace.rb', line 67

def set_trace_func(callback_method, opts={})
  if callback_method
    @opts = opts
    call_offset = @opts[:call_offset] || 0
    @callback_method = callback_method
    @tracing  = true
    spinup_thread
    
    # Feed info to the stepping call-back thread!
    @vm_locations = Rubinius::VM.backtrace(call_offset + 1, true)
    
    method = Rubinius::CompiledMethod.of_sender
    
    bp = BreakPoint.new "<start>", method, 0, 0
    channel = Rubinius::Channel.new
    
    @local_channel.send Rubinius::Tuple[bp, Thread.current, channel, 
                                        @vm_locations]
    
    # wait for the callback to release us
    channel.receive
    
    Thread.current.set_debugger_thread @thread
    self
  else
    @tracing = false
  end
end

#show_bytecode(line = @current_frame.line) ⇒ Object



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
# File 'lib/set_trace.rb', line 235

def show_bytecode(line=@current_frame.line)
  meth = @current_frame.method
  start = meth.first_ip_on_line(line)
  fin = meth.first_ip_on_line(line+1)

  if fin == -1
    fin = meth.iseq.size
  end

  puts "Bytecode between #{start} and #{fin-1} for line #{line}"

  decoder = Rubinius::InstructionDecoder.new(meth.iseq)
  partial = decoder.decode_between(start, fin)

  ip = start

  partial.each do |ins|
    op = ins.shift

    ins.each_index do |i|
      case op.args[i]
      when :literal
        ins[i] = meth.literals[ins[i]].inspect
      when :local
        if meth.local_names
          ins[i] = meth.local_names[ins[i]]
        end
      end
    end

    puts " %4d: #{op.opcode} #{ins.join(', ')}" % ip

    ip += (ins.size + 1)
  end
end

#show_code(line = @current_frame.line) ⇒ Object



200
201
202
203
204
205
206
207
208
# File 'lib/set_trace.rb', line 200

def show_code(line=@current_frame.line)
  path = @current_frame.method.active_path

  if str = @file_lines[path][line - 1]
    puts "#{line}: #{str}"
  else
    show_bytecode(line)
  end
end

#step_over_by(step) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/stepping.rb', line 73

def step_over_by(step)
  f = current_frame
  
  ip = -1
  
  exec = f.method
  possible_line = f.line + step
  fin_ip = exec.first_ip_on_line possible_line, f.ip
  
  if fin_ip == -1
    return step_to_parent
  end
  
  set_breakpoints_between(exec, f.ip, fin_ip)
end

#step_to_parentObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/stepping.rb', line 89

def step_to_parent
  f = frame(current_frame.number + 1)
  unless f
    STDERR.puts "Unable to find frame to step to next"
    return
  end
  
  exec = f.method
  ip = f.ip
  
  bp = BreakPoint.for_ip(exec, ip, :anon, 'return')
  bp.for_step!(f.variables)
  bp.activate
  
  return bp
end