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