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