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