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