Skip to content

Instantly share code, notes, and snippets.

@KJTsanaktsidis
Created September 27, 2021 06:21
Show Gist options
  • Save KJTsanaktsidis/61a66a8856ec75e07cf0b9da7ca0c143 to your computer and use it in GitHub Desktop.
Save KJTsanaktsidis/61a66a8856ec75e07cf0b9da7ca0c143 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'open3'
@allocated_addrs = {}
@allocation_time = {}
@symbol_cache = {}
@app_pid = ARGV[0]
def resolve_symbol(addr)
return @symbol_cache[addr] if @symbol_cache[addr]
output, err, st = Open3.capture3('gdb', '-p', @app_pid, '-ex', "info line * #{addr}", stdin_data: "")
raise "fail: #{st}" unless st.success?
output.each_line do |l|
if l =~ /Line ([0-9]+) of "([^"]+)"/
ln = "#{$2}:#{$1}"
@symbol_cache[addr] = ln
return ln
end
end
ln = "Unknown <#{addr}>"
@symbol_cache[addr] = ln
return ln
end
cur_ptr = nil
STDIN.each_line do |ln|
if ln =~ /@allocated_addrs\[([0-9]+)\]:/
cur_ptr = $1.to_i
elsif ln =~ /@allocation_time\[([0-9]+)\]: ([0-9]+)/
@allocation_time[$1.to_i] = $2.to_i
elsif ln =~ /^\s*(0x[0-9a-f]*)$/
@allocated_addrs[cur_ptr] ||= []
@allocated_addrs[cur_ptr] << $1
end
end
@allocation_time.sort_by { |k, v| v}.each do |ptr, t|
stack = @allocated_addrs[ptr]
puts "Pointer at #{ptr}:"
stack&.each do |s|
puts " #{resolve_symbol s}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment