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