diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/test/dtrace/helper.rb |
Fresh start
Diffstat (limited to 'jni/ruby/test/dtrace/helper.rb')
-rw-r--r-- | jni/ruby/test/dtrace/helper.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/jni/ruby/test/dtrace/helper.rb b/jni/ruby/test/dtrace/helper.rb new file mode 100644 index 0000000..ccc7081 --- /dev/null +++ b/jni/ruby/test/dtrace/helper.rb @@ -0,0 +1,50 @@ +# -*- coding: us-ascii -*- +require 'test/unit' +require 'tempfile' + +if Process.euid == 0 + ok = true +elsif (sudo = ENV["SUDO"]) and !sudo.empty? and (`#{sudo} echo ok` rescue false) + ok = true +else + ok = false +end +ok &= (`dtrace -V` rescue false) +module DTrace + class TestCase < Test::Unit::TestCase + INCLUDE = File.expand_path('..', File.dirname(__FILE__)) + + def trap_probe d_program, ruby_program + d = Tempfile.new(%w'probe .d') + d.write d_program + d.flush + + rb = Tempfile.new(%w'probed .rb') + rb.write ruby_program + rb.flush + + d_path = d.path + rb_path = rb.path + + cmd = ["dtrace", "-q", "-s", d_path, "-c", "#{EnvUtil.rubybin} -I#{INCLUDE} #{rb_path}"] + if sudo = @@sudo + [RbConfig::CONFIG["LIBPATHENV"], "RUBY", "RUBYOPT"].each do |name| + if name and val = ENV[name] + cmd.unshift("#{name}=#{val}") + end + end + cmd.unshift(sudo) + end + probes = IO.popen(cmd, err: [:child, :out]) do |io| + io.readlines + end + d.close(true) + rb.close(true) + yield(d_path, rb_path, probes) + end + end +end if ok + +if ok + DTrace::TestCase.class_variable_set(:@@sudo, sudo) +end |