-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.rb
More file actions
executable file
·87 lines (70 loc) · 1.75 KB
/
trace.rb
File metadata and controls
executable file
·87 lines (70 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env ruby
require 'optparse'
require 'pp'
require 'addr2sym'
mapfile = nil
target = nil
opt = OptionParser.new
opt.on('-m mapfile') { |v| mapfile = v }
opt.on('-t target') { |v| target = v }
opt.parse!
unless mapfile.nil? ^ target.nil?
puts("Either mapfile or target must be given.")
exit
end
# read mapfile and construct address-to-binary map
# maps: Range -> String
a2s = nil
unless mapfile.nil?
File.open(mapfile) do |file|
a2s = Addr2Sym.new file
end
end
filename = ARGV[0]
puts "reading '#{filename}' ..."
allocation = Struct.new(:address, :size, :callstack)
arr = []
File.open(filename) do |f|
f.each_line do |line|
line.chomp!
type, address, size, *callstack = line.split(' ')
if type == 'm' # malloc
arr.push(allocation.new(address, size, callstack))
elsif type == 'f' # free
idx = arr.rindex { |alloc| alloc.address == address}
if idx.nil?
puts "orphaned free found, but continue processing..."
next
end
arr.delete_at idx
else # unknown
puts "Unknown operation type found: #{type}."
end
end
end
if arr.empty?
puts "no memory leaks found."
exit
end
size = arr.size
puts "#{size} leaks found..."
puts
arr.each_index do |idx|
puts "memory leak #{idx}:"
alloc = arr[idx]
puts " address : #{alloc.address}"
puts " size : #{alloc.size.hex}"
puts " callstack:"
alloc.callstack.each do |caller|
if target
output = `addr2line -Cife #{target} #{caller}`
func, line = output.split
puts " #{caller} #{func} #{line}"
else
sym = a2s.translate caller.hex
offset = sym.offset.nil? ? '?' : sym.offset.to_s(16)
puts " #{caller} #{sym.funcname}+0x#{offset} in #{sym.pathname}"
end
end
puts
end