1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/dtrace/helper.rb
akr fb2008a73a * test/lib/envutil.rb: Moved from test/ruby/.
* test/lib/find_executable.rb: Ditto.

* test/lib/memory_status.rb: Ditto.

* test/lib/test/unit.rb: require envutil.

* test/: Don't require envutil in test files.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48409 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-11-13 16:05:37 +00:00

50 lines
1.2 KiB
Ruby

# -*- 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