Skip to content

Commit eab0db9

Browse files
committed
wip
1 parent 50b384c commit eab0db9

File tree

10 files changed

+97
-120
lines changed

10 files changed

+97
-120
lines changed

examples/selective_tracing.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#!/usr/bin/env ruby
22

3-
# Load the native extension only if RubyRecorder is not already available
4-
# (e.g., when running directly without the codetracer wrapper)
5-
unless defined?(CodeTracerNativeRecorder)
6-
ext_base = File.expand_path('../gems/codetracer-ruby-recorder/ext/native_tracer/target/release/libcodetracer_ruby_recorder', __dir__)
7-
require ext_base
8-
end
3+
ext_base = File.expand_path('../gems/codetracer-ruby-recorder/ext/native_tracer/target/release/libcodetracer_ruby_recorder', __dir__)
4+
require ext_base
95

10-
recorder = CodeTracerNativeRecorder.new
6+
recorder = CodeTracer::RubyRecorder.new
117

128
puts 'start trace'
139
recorder.disable_tracing

examples/selective_tracing_pure.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
#!/usr/bin/env ruby
22

3-
# Load the pure Ruby tracer library if RubyRecorder is not already defined
4-
unless defined?(CodeTracerNativeRecorder)
5-
lib_base = File.expand_path('../gems/codetracer-pure-ruby-recorder/lib/codetracer_pure_ruby_recorder', __dir__)
6-
require lib_base
7-
end
3+
lib_base = File.expand_path('../gems/codetracer-pure-ruby-recorder/lib/codetracer_pure_ruby_recorder', __dir__)
4+
require lib_base
85

9-
recorder = CodeTracerNativeRecorder.new
6+
recorder = CodeTracer::PureRubyRecorder.new
107

118
puts 'start trace'
12-
recorder.disable_tracing
9+
recorder.stop
1310
puts 'this will not be traced'
14-
recorder.enable_tracing
15-
puts 'this will be traced'
16-
recorder.disable_tracing
11+
recorder.start
12+
recorder.stop
1713
puts 'tracing disabled'
1814
recorder.flush_trace(Dir.pwd)

gems/codetracer-pure-ruby-recorder/bin/codetracer-pure-ruby-recorder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ lib_dir = File.expand_path('../lib', __dir__)
66
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
77
require 'codetracer_pure_ruby_recorder'
88

9-
exit Codetracer::PureRubyRecorder.parse_argv_and_trace_ruby_file(ARGV)
9+
exit CodeTracer::PureRubyRecorder.parse_argv_and_trace_ruby_file(ARGV)

gems/codetracer-pure-ruby-recorder/lib/codetracer/kernel_patches.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: MIT
22

3-
module Codetracer
3+
module CodeTracer
44
module KernelPatches
55
@@tracers = []
66

@@ -53,11 +53,7 @@ def self.uninstall(tracer)
5353
Kernel.module_eval do
5454
alias_method :p, :codetracer_original_p
5555
alias_method :puts, :codetracer_original_puts
56-
alias_method :print, :codetracer_original_print
57-
58-
remove_method :codetracer_original_p
59-
remove_method :codetracer_original_puts
60-
remove_method :codetracer_original_print
56+
alias_method :print, :codetracer_oirginal_print
6157
end
6258
end
6359
end

gems/codetracer-pure-ruby-recorder/lib/codetracer_pure_ruby_recorder.rb

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require_relative 'recorder'
88
require_relative 'codetracer/kernel_patches'
99

10-
module Codetracer
10+
module CodeTracer
1111
class PureRubyRecorder
1212
attr_accessor :calls_tracepoint, :return_tracepoint,
1313
:line_tracepoint, :raise_tracepoint, :tracing
@@ -61,23 +61,13 @@ def self.trace_ruby_file(program, out_dir, program_args = [])
6161
Kernel.load(program)
6262
rescue Exception => e
6363
if tracer.debug
64-
if Kernel.respond_to?(:codetracer_original_puts, true)
65-
Kernel.codetracer_original_puts ''
66-
Kernel.codetracer_original_puts '==== trace.rb error while tracing program ==='
67-
Kernel.codetracer_original_puts 'ERROR'
68-
Kernel.codetracer_original_puts e
69-
Kernel.codetracer_original_puts e.backtrace
70-
Kernel.codetracer_original_puts '====================='
71-
Kernel.codetracer_original_puts ''
72-
else
73-
puts ''
74-
puts '==== trace.rb error while tracing program ==='
75-
puts 'ERROR'
76-
puts e
77-
puts e.backtrace
78-
puts '====================='
79-
puts ''
80-
end
64+
codetracer_original_puts ''
65+
codetracer_original_puts '==== trace.rb error while tracing program ==='
66+
codetracer_original_puts 'ERROR'
67+
codetracer_original_puts e
68+
codetracer_original_puts e.backtrace
69+
codetracer_original_puts '====================='
70+
codetracer_original_puts ''
8171
end
8272
ensure
8373
# Restore original ARGV
@@ -109,27 +99,27 @@ def ignore(path)
10999

110100
def setup_tracepoints
111101
@calls_tracepoint = TracePoint.new(:call) do |tp|
112-
deactivate
102+
disable_tracepoints
113103
record_call(tp)
114-
activate
104+
enable_tracepoints
115105
end
116106

117107
@return_tracepoint = TracePoint.new(:return) do |tp|
118-
deactivate
108+
disable_tracepoints
119109
record_return(tp)
120-
activate
110+
enable_tracepoints
121111
end
122112

123113
@line_tracepoint = TracePoint.new(:line) do |tp|
124-
deactivate
114+
disable_tracepoints
125115
record_step(tp)
126-
activate
116+
enable_tracepoints
127117
end
128118

129119
@raise_tracepoint = TracePoint.new(:raise) do |tp|
130-
deactivate
120+
disable_tracepoints
131121
record_exception(tp)
132-
activate
122+
enable_tracepoints
133123
end
134124
end
135125

@@ -172,15 +162,10 @@ def record_call(tp)
172162
module_name = tp.self.class.name
173163
method_name_prefix = module_name == 'Object' ? '' : "#{module_name}#"
174164
method_name = "#{method_name_prefix}#{tp.method_id}"
175-
176-
if @debug && Kernel.respond_to?(:codetracer_original_puts, true)
177-
Kernel.codetracer_original_puts "call #{method_name} with #{tp.parameters}"
178-
elsif @debug
179-
puts "call #{method_name} with #{tp.parameters}"
165+
if @debug
166+
codetracer_original_puts "call #{method_name} with #{tp.parameters}"
180167
end
181-
182168
arg_records = prepare_args(tp)
183-
184169
@record.register_step(tp.path, tp.lineno)
185170
@record.register_call(tp.path, tp.lineno, method_name, arg_records)
186171
else
@@ -189,10 +174,8 @@ def record_call(tp)
189174

190175
def record_return(tp)
191176
if self.tracks_call?(tp)
192-
if @debug && Kernel.respond_to?(:codetracer_original_puts, true)
193-
Kernel.codetracer_original_puts 'return'
194-
elsif @debug
195-
puts 'return'
177+
if @debug
178+
codetracer_original_puts 'return'
196179
end
197180
return_value = @record.to_value(tp.return_value)
198181
@record.register_step(tp.path, tp.lineno)
@@ -237,32 +220,24 @@ def record_exception(tp)
237220
@record.events << [:Event, RecordEvent.new(EVENT_KIND_ERROR, tp.raised_exception.to_s, "")]
238221
end
239222

240-
def activate
241-
@calls_tracepoint.enable
242-
@return_tracepoint.enable
243-
@line_tracepoint.enable
244-
@raise_tracepoint.enable
245-
@tracing = true
246-
::Codetracer::KernelPatches.install(self)
223+
def start
224+
::CodeTracer::KernelPatches.install(self)
225+
enable_tracepoints
247226
end
248227

249-
def deactivate
250-
::Codetracer::KernelPatches.uninstall(self)
251-
@tracing = false
252-
@calls_tracepoint.disable
253-
@return_tracepoint.disable
254-
@line_tracepoint.disable
255-
@raise_tracepoint.disable
228+
def stop
229+
disable_tracepoints
230+
::CodeTracer::KernelPatches.uninstall(self)
256231
end
257232

258233
def trace_block(&block)
259234
raise ArgumentError, "no block given" unless block_given?
260235

261-
activate
236+
start
262237
begin
263238
yield
264239
ensure
265-
deactivate
240+
stop
266241
end
267242
end
268243

@@ -273,6 +248,26 @@ def flush_trace(out_dir)
273248

274249
private
275250

251+
def enable_tracepoints
252+
@calls_tracepoint.enable
253+
@return_tracepoint.enable
254+
@raise_tracepoint.enable
255+
@tracing = true
256+
# We intentionally enable the line tracepoint after the other tracepoints
257+
# to avoid recording the initial activation call as a line event.
258+
@line_tracepoint.enable
259+
end
260+
261+
def disable_tracepoints
262+
# We disable the line tracepoint first to avoid recording the deactivation
263+
# call as a line event.
264+
@line_tracepoint.disable
265+
@calls_tracepoint.disable
266+
@return_tracepoint.disable
267+
@raise_tracepoint.disable
268+
@tracing = false
269+
end
270+
276271
def load_variables(binding)
277272
if !binding.nil?
278273
# $stdout.write binding.local_variables

gems/codetracer-pure-ruby-recorder/lib/recorder.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ def nil_value
362362
MAX_COUNT = 5000
363363

364364
def to_value(v, depth=10)
365+
codetracer_original_p v
365366
if depth <= 0
366367
return nil_value
367368
end
@@ -394,7 +395,7 @@ def to_value(v, depth=10)
394395
end)
395396
end
396397
when Hash
397-
if v.count > MAX_COUNT
398+
if true or v.count > MAX_COUNT
398399
not_supported_value
399400
else
400401
pairs = v.map do |k, val|

gems/codetracer-ruby-recorder/lib/codetracer/kernel_patches.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: MIT
22

3-
module Codetracer
3+
module CodeTracer
44
module KernelPatches
55
@@tracers = []
66

@@ -54,10 +54,6 @@ def self.uninstall(tracer)
5454
alias_method :p, :codetracer_original_p
5555
alias_method :puts, :codetracer_original_puts
5656
alias_method :print, :codetracer_original_print
57-
58-
remove_method :codetracer_original_p
59-
remove_method :codetracer_original_puts
60-
remove_method :codetracer_original_print
6157
end
6258
end
6359
end

gems/codetracer-ruby-recorder/lib/codetracer_ruby_recorder.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def self.trace_ruby_file(program, out_dir, program_args = [])
4242

4343
ENV['CODETRACER_RUBY_RECORDER_OUT_DIR'] = out_dir
4444

45-
recorder.activate
45+
recorder.start
4646
begin
4747
# Set ARGV to contain the program arguments
4848
original_argv = ARGV.dup
@@ -55,7 +55,7 @@ def self.trace_ruby_file(program, out_dir, program_args = [])
5555
ARGV.clear
5656
ARGV.concat(original_argv)
5757

58-
recorder.deactivate
58+
recorder.stop
5959
recorder.flush_trace(out_dir)
6060
end
6161
0
@@ -72,20 +72,20 @@ def initialize
7272
load_native_recorder
7373
end
7474

75-
# Activate the recorder and install kernel patches
76-
def activate
75+
# Start the recorder and install kernel patches
76+
def start
7777
return if @active || @recorder.nil?
7878

7979
@recorder.enable_tracing
80-
Codetracer::KernelPatches.install(self)
80+
CodeTracer::KernelPatches.install(self)
8181
@active = true
8282
end
8383

84-
# Deactivate the recorder and remove kernel patches
85-
def deactivate
84+
# Stop the recorder and remove kernel patches
85+
def stop
8686
return unless @active
8787

88-
Codetracer::KernelPatches.uninstall(self)
88+
CodeTracer::KernelPatches.uninstall(self)
8989
@recorder.disable_tracing if @recorder
9090
@active = false
9191
end

0 commit comments

Comments
 (0)