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