Class: PryMoves::TraceCommand

Inherits:
Object
  • Object
show all
Includes:
TraceHelpers
Defined in:
lib/commands/trace_command.rb

Direct Known Subclasses

Debug, Finish, Goto, Iterate, Next, NextBreakpoint, Step

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TraceHelpers

#current_frame_digest, #current_frame_type, #debug_info, #frame_digest, #frame_type, #redirect_step?

Constructor Details

#initialize(command, pry_start_options, &callback) ⇒ TraceCommand

Returns a new instance of TraceCommand.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/commands/trace_command.rb', line 15

def initialize(command, pry_start_options, &callback)
  @command = command
  @pry_start_options = pry_start_options
  @pry_start_options[:pry_moves_loop] = true
  @callback = callback
  @call_depth = 0
  @c_stack_level = 0

  binding_ = @command[:binding] # =Command.target - more rich, contains required @iseq
  unless binding_.instance_variable_get('@iseq')
    binding_ = PryMoves::BindingsStack.new(@pry_start_options).initial_frame
  end
  @method = PryMoves::TracedMethod.new binding_

  if @pry_start_options.delete :exit_from_method
    @on_exit_from_method = true
    @call_depth -= 1
  end
  @pry_start_options.delete :initial_frame

  init binding_
  start_tracing
end

Class Method Details

.trace(command, pry_start_options, &callback) ⇒ Object



9
10
11
12
13
# File 'lib/commands/trace_command.rb', line 9

def self.trace(command, pry_start_options, &callback)
  cls = command[:action].to_s.split('_').collect(&:capitalize).join
  cls = Object.const_get "PryMoves::#{cls}"
  cls.new command, pry_start_options, &callback
end

Instance Method Details

#start_tracingObject



39
40
41
42
43
# File 'lib/commands/trace_command.rb', line 39

def start_tracing
  #puts "##trace_obj #{trace_obj}"
  Pry.config.disable_breakpoints = true
  trace_obj.set_trace_func method(:tracing_func).to_proc
end

#stop_tracingObject



45
46
47
48
# File 'lib/commands/trace_command.rb', line 45

def stop_tracing
  trace_obj.set_trace_func nil
  Pry.config.disable_breakpoints = false
end

#trace_objObject

You can’t call set_trace_func or Thread.current.set_trace_func recursively even in different threads 😪 But! 💡 The hack is - you can call Thread.current.set_trace_func from inside of set_trace_func! 🤗



55
56
57
58
# File 'lib/commands/trace_command.rb', line 55

def trace_obj
  Thread.current[:pry_moves_debug] ?
    Thread.current : Kernel
end

#traced_method?(file, line, method, binding_) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/commands/trace_command.rb', line 86

def traced_method?(file, line, method, binding_)
  @method.within?(file, line, method)
end

#tracing_func(event, file, line, method, binding_, klass) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/commands/trace_command.rb', line 60

def tracing_func(event, file, line, method, binding_, klass)

  # Ignore traces inside pry-moves code
  return if file && TRACE_IGNORE_FILES.include?(File.expand_path(file))
  return unless binding_ # ignore strange cases

  # for cases when currently traced method called more times recursively
  if event == "call" and traced_method?(file, line, method, binding_)
    @call_depth += 1
  elsif %w(c-call c-return).include?(event)
    # todo: может быть, c-return тоже правильнее делать после trace
    delta = event == 'c-call' ? 1 : -1
    @c_stack_level += delta
  end

  printf "👟 %8s %s:%-2d %10s %8s dep:#{@call_depth} c_st:#{@c_stack_level}\n", event, file, line, method, klass if PryMoves.trace # TRACE_MOVES=1

  if trace event, file, line, method, binding_
    @pry_start_options[:exit_from_method] = true if event == 'return'
    stop_tracing
    @callback.call binding_
  elsif event == "return" and traced_method?(file, line, method, binding_)
    @call_depth -= 1
  end
end