Skip to content

Instantly share code, notes, and snippets.

@chyh1990
Created October 20, 2015 02:21
Show Gist options
  • Select an option

  • Save chyh1990/c0107596a3257a000771 to your computer and use it in GitHub Desktop.

Select an option

Save chyh1990/c0107596a3257a000771 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'nokogiri'
require 'colorize'
require 'tempfile'
# valgrind --xml=yes --xml-file=leak.xml --leak-check=full --error-limit=no \
# --num-callers=20 --partial-loads-ok=yes --undef-value-errors=no \
# ruby -Ilib test/image_test.rb
def print_node node
puts node.css('kind').text
node.xpath('stack/frame').each do |f|
fn = File.basename f.css('obj').text
line = " #{fn}:#{f.css('fn').text} @ #{f.css('file').text}:#{f.css('line').text}"
if fn =~ /libruby\.so/ || fn =~ /preload_memcheck/
puts line
else
puts line.green
end
end
puts "=================="
end
BLACK_LIST_METHODS = %w(
rb_add_method
rb_define_singleton_method
rb_class_boot
rb_define_class_id_under
rb_define_module_function
)
unless ARGV.include? '--'
$stderr.puts "Usage: #{$0} [keyword] -- cmdline"
exit 1
end
if ARGV[0] == '--'
kw = nil
else
kw = ARGV.shift
end
ARGV.shift # '--'
tmpout = Tempfile.new ['val', '.xml']
cmd = "valgrind --xml=yes --xml-file=#{tmpout.path} --leak-check=full \
--error-limit=no --num-callers=20 --partial-loads-ok=yes --undef-value-errors=no \
#{ARGV.join ' '}"
$stderr.puts "running: #{cmd}"
system cmd
fail 'fail to run valgrind' unless $?.to_i == 0
$stderr.puts "valgrind done"
# tmpout.close
doc = Nokogiri::XML File.open(tmpout.path)
doc.css('error').each do |e|
objs = e.xpath('stack/frame/obj').map(&:text)
methods = e.xpath('stack/frame/fn').map(&:text)
if kw
next unless objs.grep(/#{kw}/).size > 0
end
next if methods.any?{|e| BLACK_LIST_METHODS.include? e}
print_node e
# exit
end
# tmpout.unlink
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment