Skip to content

Instantly share code, notes, and snippets.

@sonots
Last active September 29, 2016 06:58
Show Gist options
  • Save sonots/5870ae8415af4c9cb66e0a11e0f98516 to your computer and use it in GitHub Desktop.
Save sonots/5870ae8415af4c9cb66e0a11e0f98516 to your computer and use it in GitHub Desktop.
相対アドレスを /proc/[tid]/maps から取得して addr2line するやつ (systemtap 以外でも使えるだろう)
#!/usr/bin/env ruby
#
# systemtap_addr2line.rb
#
# Show source files and line numers of given address
#
# This script works only on Linux.
# https://gist.github.com/sonots/5870ae8415af4c9cb66e0a11e0f98516
#
# ref. https://gist.github.com/nurse/0619b6af90df140508c2
# Get [executable or library path, relative_address]
def search_maps(tid, addr)
IO.foreach("/proc/#{tid}/maps") do |line|
address, perms, offset, dev, inode, pathname = line.split
range = Range.new(*address.split('-').map{|x|x.hex})
if range.include?(addr)
reladdr = addr - range.first
return [pathname, reladdr]
end
end
end
# http://man7.org/linux/man-pages/man1/addr2line.1.html
def addr2line(path, addr)
system('addr2line', '-fie', path, addr.to_s(16))
end
def usage(msg = nil)
puts "#$0: <tid> <addr>"
puts msg if msg
exit 1
end
def main
begin
unless tid = Integer(ARGV.shift)
usage
end
unless addr = Integer(ARGV.shift)
usage
end
rescue ArgumentError => e
usage e.message
end
executable, reladdr = search_maps(tid, addr)
puts "reladdr: %x -> %x" % [addr, reladdr]
addr2line(executable, reladdr)
end
main
@sonots
Copy link
Author

sonots commented Sep 29, 2016

stap -d /path/to/executable -e 'probe vm.write_shared_copy {
  printf("%s(pid:%d,tid:%d) write_to_shared_page(%p)\n", execname(), pid(), tid(), address)
  print_ubacktrace()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment