Last active
September 29, 2016 06:58
-
-
Save sonots/5870ae8415af4c9cb66e0a11e0f98516 to your computer and use it in GitHub Desktop.
相対アドレスを /proc/[tid]/maps から取得して addr2line するやつ (systemtap 以外でも使えるだろう)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Author
sonots
commented
Sep 29, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment