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