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