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