forked from yugui/vm_probes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extconf.rb
108 lines (96 loc) · 2.69 KB
/
extconf.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require 'mkmf'
autoload :Tempfile, 'tempfile'
autoload :ERB, 'erb'
DTRACE = 'dtrace'
ARCH=$configure_args['with-arch']
def choose_mechanism
case mechanism = $configure_args['tracing-mechanism']
when 'dtrace', nil
mechanism = 'dtrace'
begin
$objs = %w[ vm_probes.o ]
$objs << 'dtrace.o' if dtrace_needs_postprocessor?(ARCH)
rescue
message "something wrong in checking dtrace. see mkmf.log\n"
message $!.message
exit false
end
create_dtrace_d
else
message "unknown tracing mechanism #{mechanism}"
exit false
end
return mechanism
end
def create_dtrace_d
bitsize_of_value = try_constant("SIZEOF_VALUE * CHAR_BIT", %w[ limits.h ruby/ruby.h ])
unless bitsize_of_value.kind_of?(Integer)
message 'failed to check SIZEOF_VALUE'
exit false
end
template = ERB.new(File.read( File.expand_path("../dtrace.d.erb", __FILE__) ))
File.open("dtrace.d", "wb"){|f| f.write template.result(binding) }
end
def dtrace_needs_postprocessor?(arch)
return $dtrace_needs_postprocessor unless $dtrace_needs_postprocessor.nil?
arch_arg = "--arch=#{arch}" if arch
Tempfile.open("conftest.d"){|f|
f.puts <<-EOS
provider conftest {
probe fire();
};
EOS
f.close
unless xsystem("#{DTRACE} #{arch_arg} -h -o dtrace_conftest.h -s #{f.path}")
message "failed to run dtrace -h"
exit false
end
unless try_compile(<<-EOS, "$(COUTFLAG)conftest.$(OBJEXT)")
#include "dtrace_conftest.h"
int main() {
return 0;
}
EOS
message "failed to compile a test program"
exit false
end
unless xsystem(RbConfig.expand("#{DTRACE} #{arch_arg} -G -o dtrace.o conftest.$(OBJEXT)"))
return $dtrace_needs_postprocessor = false
end
unless try_compile(<<-EOS, "$(COUTFLAG)conftest.$(OBJEXT)")
#include "dtrace_conftest.h"
int main() {
if (CONFTEST_FIRE_ENABLED()) {
CONFTEST_FIRE();
}
return 0;
}
EOS
message "failed to compile a test program"
exit false
end
unless xsystem(RbConfig.expand("#{DTRACE} #{arch_arg} -G -o dtrace.o conftest.$(OBJEXT)"))
message "dtrace USDT is broken"
exit false
end
return $dtrace_needs_postprocessor = true
}
raise "never reached"
ensure
rm_f 'dtrace_conftest.h'
rm_f 'dtrace.o'
end
def prepend_variables(mechanism)
makefile = File.read("Makefile")
File.open("Makefile", "wt"){|f|
f.puts "DTRACE=#{DTRACE}"
f.puts "TRACING_MECHANISM=#{mechanism}"
f.puts "ARCH_FLAGS=--arch #{ARCH}" if ARCH
orig_objs = $objs.select{|x| x != "#{mechanism}.o"}
f.puts "ORIG_OBJS=#{orig_objs.join(' ')}"
f.puts(makefile)
}
end
mechanism = choose_mechanism
create_makefile('vm_probes')
prepend_variables(mechanism)