1# -*- coding: us-ascii -*-
2require 'minitest/autorun'
3require 'tempfile'
4require_relative '../ruby/envutil'
5
6if Process.euid == 0
7  ok = true
8elsif (sudo = ENV["SUDO"]) and (`#{sudo} echo ok` rescue false)
9  ok = true
10else
11  ok = false
12end
13ok &= (`dtrace -V` rescue false)
14module DTrace
15  class TestCase < MiniTest::Unit::TestCase
16    INCLUDE = File.expand_path(File.join(File.dirname(__FILE__), '..'))
17
18    def trap_probe d_program, ruby_program
19      d = Tempfile.new('probe.d')
20      d.write d_program
21      d.flush
22
23      rb = Tempfile.new('probed.rb')
24      rb.write ruby_program
25      rb.flush
26
27      d_path  = d.path
28      rb_path = rb.path
29
30      cmd = ["dtrace", "-q", "-s", d_path, "-c", "#{EnvUtil.rubybin} -I#{INCLUDE} #{rb_path}"]
31      sudo = ENV["SUDO"] and cmd.unshift(sudo)
32      probes = IO.popen(cmd) do |io|
33        io.readlines
34      end
35      d.close(true)
36      rb.close(true)
37      yield(d_path, rb_path, probes)
38    end
39  end
40end if ok
41