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