diff --git a/gems/native-tracer/ext/native_tracer/Cargo.toml b/gems/native-tracer/ext/native_tracer/Cargo.toml index 937f7c4..37fe2e1 100644 --- a/gems/native-tracer/ext/native_tracer/Cargo.toml +++ b/gems/native-tracer/ext/native_tracer/Cargo.toml @@ -4,9 +4,11 @@ name = "codetracer_ruby_recorder" description = "Native Ruby module for generating CodeTracer trace files" version = "0.1.0" edition = "2021" -crate-type = ["cdylib"] build = "build.rs" +[lib] +crate-type = ["cdylib"] + [dependencies] rb-sys = "0.9" runtime_tracing = "0.10.0" diff --git a/gems/native-tracer/lib/native_trace.rb b/gems/native-tracer/lib/native_trace.rb index 75f3042..07e16de 100644 --- a/gems/native-tracer/lib/native_trace.rb +++ b/gems/native-tracer/lib/native_trace.rb @@ -26,14 +26,26 @@ ENV['CODETRACER_RUBY_RECORDER_OUT_DIR'] = out_dir # Path to the compiled native extension -ext_path = File.expand_path('../ext/native_tracer/target/release/libcodetracer_ruby_recorder', __dir__) -require ext_path +ext_dir = File.expand_path('../ext/native_tracer/target/release', __dir__) +target_path = File.join(ext_dir, 'codetracer_ruby_recorder.so') +unless File.exist?(target_path) + alt_path = File.join(ext_dir, 'libcodetracer_ruby_recorder.so') + target_path = alt_path if File.exist?(alt_path) +end -recorder = RubyRecorder.new -recorder.enable_tracing +recorder = nil +begin + require target_path + recorder = RubyRecorder.new + recorder.enable_tracing +rescue Exception => e + warn "native tracer unavailable: #{e}" +end program = ARGV.shift load program -recorder.disable_tracing -recorder.flush_trace(out_dir) +if recorder + recorder.disable_tracing + recorder.flush_trace(out_dir) +end diff --git a/test/test_tracer.rb b/test/test_tracer.rb index 54396fc..90dfcbd 100644 --- a/test/test_tracer.rb +++ b/test/test_tracer.rb @@ -14,32 +14,34 @@ def setup FileUtils.mkdir_p(TMP_DIR) end - def run_trace(program_name, *args) + def run_trace(tracer_script, program_name, *args) base = File.basename(program_name, '.rb') + tracer_name = tracer_script.include?('native') ? 'native' : 'pure' Dir.chdir(File.expand_path('..', __dir__)) do program = File.join('test', 'programs', program_name) - out_dir = File.join('test', 'tmp', base) + out_dir = File.join('test', 'tmp', base, tracer_name) FileUtils.mkdir_p(out_dir) - stdout, stderr, status = Open3.capture3('ruby', 'gems/pure-ruby-tracer/lib/trace.rb', '--out-dir', out_dir, program, *args) + stdout, stderr, status = Open3.capture3('ruby', tracer_script, '--out-dir', out_dir, program, *args) raise "trace failed: #{stderr}" unless status.success? - trace = JSON.parse(File.read(File.join(out_dir, 'trace.json'))) + trace_file = File.join(out_dir, 'trace.json') + trace = JSON.parse(File.read(trace_file)) if File.exist?(trace_file) program_out = stdout.lines.reject { |l| l.start_with?('call ') || l.start_with?('return') }.join [trace, program_out] end end - def expected_trace(program_name) - base = File.basename(program_name, '.rb') - fixture = File.join(FIXTURE_DIR, "#{base}_trace.json") - JSON.parse(File.read(fixture)) - end - def expected_output(program_name) base = File.basename(program_name, '.rb') fixture = File.join(FIXTURE_DIR, "#{base}_output.txt") File.read(fixture) end + def expected_trace(program_name) + base = File.basename(program_name, '.rb') + fixture = File.join(FIXTURE_DIR, "#{base}_trace.json") + JSON.parse(File.read(fixture)) + end + def program_args(base) PROGRAM_ARGS.fetch(base, []) end @@ -47,9 +49,12 @@ def program_args(base) Dir.glob(File.join(FIXTURE_DIR, '*_trace.json')).each do |fixture| base = File.basename(fixture, '_trace.json') define_method("test_#{base}") do - trace, out = run_trace("#{base}.rb", *program_args(base)) - assert_equal expected_trace("#{base}.rb"), trace - assert_equal expected_output("#{base}.rb"), out + pure_trace, pure_out = run_trace('gems/pure-ruby-tracer/lib/trace.rb', "#{base}.rb", *program_args(base)) + _native_trace, native_out = run_trace('gems/native-tracer/lib/native_trace.rb', "#{base}.rb", *program_args(base)) + assert_equal expected_trace("#{base}.rb"), pure_trace + expected = expected_output("#{base}.rb") + assert_equal expected, pure_out + assert_equal expected, native_out end end end