55require 'json'
66require_relative 'recorder'
77
8- if ARGV [ 0 ] . nil?
9- $stderr. puts ( "ruby trace.rb <program> [<args>]" )
10- exit ( 1 )
11- end
12-
13- program = ARGV [ 0 ]
14-
158# Warning:
169# probably related to our development env:
1710# if we hit an `incompatible library version` error, like
@@ -82,6 +75,7 @@ def initialize(record)
8275 @trace_stopped = false
8376 @record = record
8477 @ignore_list = [ ]
78+ setup_tracepoints
8579 end
8680
8781 def stop_tracing
@@ -97,6 +91,32 @@ def ignore(path)
9791 @ignore_list << path
9892 end
9993
94+ def setup_tracepoints
95+ @calls_tracepoint = TracePoint . new ( :call ) do |tp |
96+ deactivate
97+ record_call ( tp )
98+ activate
99+ end
100+
101+ @return_tracepoint = TracePoint . new ( :return ) do |tp |
102+ deactivate
103+ record_return ( tp )
104+ activate
105+ end
106+
107+ @line_tracepoint = TracePoint . new ( :line ) do |tp |
108+ deactivate
109+ record_step ( tp )
110+ activate
111+ end
112+
113+ @raise_tracepoint = TracePoint . new ( :raise ) do |tp |
114+ deactivate
115+ record_exception ( tp )
116+ activate
117+ end
118+ end
119+
100120 def prepare_args ( tp )
101121 args_after_self = tp . parameters . map do |( kind , name ) |
102122 value = if tp . binding . nil? || name . nil?
@@ -229,64 +249,38 @@ def load_variables(binding)
229249
230250$tracer = Tracer . new ( $codetracer_record)
231251
232- # also possible :c_call, :b_call: for now record ruby calls (:call)
233- # c_call: c lang
234- # b_call: block entry
235- # https://rubyapi.org/3.4/o/tracepoint
236- $tracer. calls_tracepoint = TracePoint . new ( :call ) do |tp |
237- $tracer. deactivate
238- $tracer. record_call ( tp )
239- $tracer. activate
240- end
252+ if __FILE__ == $PROGRAM_NAME
253+ if ARGV [ 0 ] . nil?
254+ $stderr. puts ( 'ruby trace.rb <program> [<args>]' )
255+ exit ( 1 )
256+ end
241257
242- $tracer. return_tracepoint = TracePoint . new ( :return ) do |tp |
243- $tracer. deactivate
244- $tracer. record_return ( tp )
245- $tracer. activate
246- end
258+ program = ARGV [ 0 ]
247259
248- $tracer. line_tracepoint = TracePoint . new ( :line ) do |tp |
249- $tracer. deactivate
250- $tracer. record_step ( tp )
251- $tracer. activate
252- end
260+ $tracer. record . register_call ( '' , 1 , '<top-level>' , [ ] )
261+ $tracer. ignore ( 'lib/ruby' )
262+ $tracer. ignore ( 'trace.rb' )
263+ $tracer. ignore ( 'recorder.rb' )
264+ $tracer. ignore ( '<internal:' )
265+ $tracer. ignore ( 'gems/' )
253266
254- $tracer. raise_tracepoint = TracePoint . new ( :raise ) do |tp |
255- $tracer. deactivate
256- $tracer. record_exception ( tp )
267+ trace_args = ARGV
268+ ARGV = ARGV [ 1 ..-1 ]
257269 $tracer. activate
258- end
270+ begin
271+ Kernel . load ( program )
272+ rescue Exception => e
273+ old_puts ''
274+ old_puts '==== trace.rb error while tracing program ==='
275+ old_puts 'ERROR'
276+ old_puts e
277+ old_puts e . backtrace
278+ old_puts '====================='
279+ old_puts ''
280+ end
281+ ARGV = trace_args
259282
260- $tracer. record . register_call ( "" , 1 , "<top-level>" , [ ] )
261- $tracer. ignore ( 'lib/ruby' )
262- $tracer. ignore ( 'trace.rb' )
263- $tracer. ignore ( 'recorder.rb' )
264- $tracer. ignore ( '<internal:' )
265- $tracer. ignore ( 'gems/' )
266-
283+ $tracer. stop_tracing
267284
268- trace_args = ARGV
269- ARGV = ARGV [ 1 ..-1 ]
270- $tracer. activate
271- begin
272- Kernel . load ( program )
273- rescue Exception => e
274- # important: rescue Exception,
275- # not just rescue as we originally did
276- # because a simple `rescue` doesn't catch some errors
277- # like SystemExit and others
278- # (when we call `exit` in the trace program and others)
279- # https://stackoverflow.com/questions/5118745/is-systemexit-a-special-kind-of-exception
280- old_puts ""
281- old_puts "==== trace.rb error while tracing program ==="
282- old_puts "ERROR"
283- old_puts e
284- old_puts e . backtrace
285- old_puts "====================="
286- old_puts ""
285+ $tracer. record . serialize ( program )
287286end
288- ARGV = trace_args
289-
290- $tracer. stop_tracing
291-
292- $tracer. record . serialize ( program )
0 commit comments