Class: Google::Cloud::Debugger::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/debugger/tracer.rb

Overview

Tracer

When active breakpoints are set for the debugger, the tracer monitors the running Ruby application and triggers evaluation when the code is executed at the breakpoint locations.

The tracer tracks the running application using several Ruby TracePoints and C level Ruby debugging API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#agentGoogle::Cloud::Debugger::Agent (readonly)

The debugger agent this tracer belongs to



35
36
37
# File 'lib/google/cloud/debugger/tracer.rb', line 35

def agent
  @agent
end

Instance Method Details

#breakpoints_hit(breakpoints, call_stack_bindings) ⇒ Object

Callback function when a set of breakpoints are hit. Handover the hit breakpoint to breakpoint_manager to be evaluated.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/google/cloud/debugger/tracer.rb', line 96

def breakpoints_hit breakpoints, call_stack_bindings
  breakpoints.each do |breakpoint|
    # Stop evaluating breakpoints if we have quotas and the quotas are
    # met.
    break if agent.quota_manager && !agent.quota_manager.more?

    next if breakpoint.nil? || breakpoint.complete?

    time_begin = Time.now

    agent.breakpoint_manager.breakpoint_hit breakpoint,
                                            call_stack_bindings

    # Report time and resource consumption to quota manager
    if agent.quota_manager.respond_to? :consume
      agent.quota_manager.consume time: Time.now - time_begin
    end
  end

  update_breakpoints_cache

  # Disable all trace points and tracing if all breakpoints are complete
  disable_traces if @breakpoints_cache.empty?
end

#startObject

Get the sync the breakpoints cache with BreakpointManager. Start tracing and monitoring if there are any breakpoints.



124
125
126
127
# File 'lib/google/cloud/debugger/tracer.rb', line 124

def start
  update_breakpoints_cache
  enable_traces unless breakpoints_cache.empty?
end

#stopObject

Stops all tracing.



131
132
133
# File 'lib/google/cloud/debugger/tracer.rb', line 131

def stop
  disable_traces
end

#update_breakpoints_cacheObject

Update tracer's private breakpoints cache with the list of active breakpoints from BreakpointManager.

This methood is atomic for thread safety purpose.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/google/cloud/debugger/tracer.rb', line 74

def update_breakpoints_cache
  active_breakpoints = agent.breakpoint_manager.active_breakpoints.dup
  breakpoints_hash = {}

  active_breakpoints.each do |active_breakpoint|
    breakpoint_line = active_breakpoint.line
    breakpoint_path = active_breakpoint.full_path
    breakpoints_hash[breakpoint_path] ||= {}
    breakpoints_hash[breakpoint_path][breakpoint_line] ||= []
    breakpoints_hash[breakpoint_path][breakpoint_line].push(
      active_breakpoint
    )
  end

  # Tracer is explicitly designed to not have a lock. This should be the
  # only place writing @breakpoints_cache to ensure thread safety.
  @breakpoints_cache = breakpoints_hash
end