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