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