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