Class: Necropsy::Analyzers::Dynamic::TracePointCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/necropsy/analyzers/dynamic/trace_point_collector.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, output:, sample_rate: 1.0, merge: false, run_id: nil, clock: nil, random: nil) ⇒ TracePointCollector

Returns a new instance of TracePointCollector.

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/necropsy/analyzers/dynamic/trace_point_collector.rb', line 26

def initialize(root:, output:, sample_rate: 1.0, merge: false, run_id: nil, clock: nil, random: nil)
  @root = File.expand_path(root)
  @output = output
  @sample_rate = sample_rate.to_f
  raise Error, 'sample_rate must be between 0.0 and 1.0' unless @sample_rate.between?(0.0, 1.0)

  @nodes = {}
  @node_references = {}
  @edges = {}
  @edge_references = {}
  @stacks = {}.compare_by_identity
  @lock = Mutex.new
  @merge = merge
  @run_id = run_id
  @clock = clock || -> { Clock.new.time }
  @random = random || default_random
end

Class Method Details

.install_at_exit(root:, output:, sample_rate: 1.0, merge: false, run_id: nil, clock: nil, random: nil) ⇒ Object



19
20
21
22
23
24
# File 'lib/necropsy/analyzers/dynamic/trace_point_collector.rb', line 19

def self.install_at_exit(root:, output:, sample_rate: 1.0, merge: false, run_id: nil, clock: nil, random: nil)
  new(
    root: root, output: output, sample_rate: sample_rate, merge: merge, run_id: run_id,
    clock: clock, random: random
  ).install_at_exit
end

.record(root:, output:, sample_rate: 1.0, clock: nil, random: nil) ⇒ Object



15
16
17
# File 'lib/necropsy/analyzers/dynamic/trace_point_collector.rb', line 15

def self.record(root:, output:, sample_rate: 1.0, clock: nil, random: nil, &)
  new(root: root, output: output, sample_rate: sample_rate, clock: clock, random: random).record(&)
end

Instance Method Details

#install_at_exitObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/necropsy/analyzers/dynamic/trace_point_collector.rb', line 54

def install_at_exit
  started_at = current_time
  tracer = TracePoint.new(:call, :return) { |event| capture(event) }
  tracer.enable
  at_exit do
    tracer.disable
    write_payload(started_at: started_at, finished_at: current_time)
  rescue StandardError => e
    warn "Necropsy TracePoint collector failed: #{e.message}"
  end
end

#recordObject



44
45
46
47
48
49
50
51
52
# File 'lib/necropsy/analyzers/dynamic/trace_point_collector.rb', line 44

def record
  started_at = current_time
  tracer = TracePoint.new(:call, :return) { |event| capture(event) }
  tracer.enable
  yield
ensure
  tracer&.disable
  write_payload(started_at: started_at, finished_at: current_time)
end