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