f8d165
require 'set'
f8d165
f8d165
LIBRUBY_SO = 'libruby.so'
f8d165
PROBES_D = 'probes.d'
f8d165
f8d165
# These probes are excluded by VM_COLLECT_USAGE_DETAILS ifdef.
f8d165
EXCLUDE_PROBES = Set.new %w(insn insn__operand)
f8d165
f8d165
## Detect SystemTap section headers presence
f8d165
f8d165
stap_headers = [
f8d165
  '\.stapsdt\.base',
f8d165
  '\.note\.stapsdt'
f8d165
]
f8d165
f8d165
header_regexp = %r{ (#{stap_headers.join('|')}) }
f8d165
f8d165
section_headers = `readelf -S "#{LIBRUBY_SO}"`
f8d165
detected_stap_headers = section_headers.scan(header_regexp).flatten
f8d165
f8d165
# Assume there are both headers until this is proven wrong ;)
f8d165
unless detected_stap_headers.size == 2
f8d165
  puts 'ERROR: SystemTap (DTrace) headers were not detected in resulting library.'
f8d165
  exit false
f8d165
end
f8d165
f8d165
## Find if every declared probe is propagated to resulting library
f8d165
f8d165
# Colect probes specified in probes.d file.
f8d165
probes_declared = []
f8d165
f8d165
File.open(PROBES_D) do |file|
f8d165
  file.each_line do |line|
f8d165
    if probe = line[/probe (\S+)\(.*\);/, 1]
f8d165
      probes_declared << probe
f8d165
    end
f8d165
  end
f8d165
end
f8d165
f8d165
probes_declared = Set.new probes_declared
f8d165
f8d165
unless EXCLUDE_PROBES.subset? probes_declared
f8d165
  puts 'ERROR: Change in SystemTap (DTrace) probes definition file detected.'
f8d165
  exit false
f8d165
end
f8d165
f8d165
probes_declared -= EXCLUDE_PROBES
f8d165
f8d165
# Detect probes in resulting library.
f8d165
get_probes_detected = %r{
f8d165
^\s*Provider:\s+ruby,\s+Name:\s+(\S+),\s+.*$
f8d165
}
f8d165
f8d165
probes_detected = `eu-readelf -n "#{LIBRUBY_SO}"`
f8d165
f8d165
probes_detected = Set.new probes_detected.scan(get_probes_detected).flatten
f8d165
f8d165
# Both sets must be equal, otherwise something is wrong.
f8d165
unless probes_declared == probes_detected
f8d165
  puts 'ERROR: SystemTap (DTrace) probes were not correctly propagated into resulting library.'
f8d165
  puts "       Undetected probes: #{(probes_declared - probes_detected).sort.join(', ')}\n",
f8d165
       "       Additional detected probes: #{(probes_detected - probes_declared).sort.join(', ')}"
f8d165
f8d165
  exit false
f8d165
end