-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrdump.py
executable file
·163 lines (134 loc) · 5.65 KB
/
grdump.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python
#
# Copyright 2021, Xcalar Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: Eric D. Cohen
import argparse
from collections import Counter
import csv
import subprocess as sp
import re
import sys
import os
argParser = argparse.ArgumentParser()
argParser.add_argument('-b', dest='binary', required=False,
help='Path to binary run under guard rails')
argParser.add_argument('-c', dest='sortct', required=False, action='store_true',
help='Sort by leak count instead total memory leaked')
argParser.add_argument('-f', dest='fin', required=True,
help='GuardRails leak dump CSV file')
argParser.add_argument('-t', dest='top', required=False, type=int, default=sys.maxsize,
help='Only show up to this many top contexts')
args = argParser.parse_args()
class grDump(object):
def __init__(self):
self.data = []
self.leaks = []
# XXX: Needs to be an array of shared lib bases...
self.baseAddr = 0
self.myDir = os.path.dirname(os.path.realpath(__file__))
try:
with open(self.myDir + '/blist.txt', 'r') as fh:
self.blist = fh.read().splitlines()
except IOError:
self.blist = []
def loadData(self):
with open(args.fin) as fh:
self.data = fh.read().splitlines()
self.baseAddr = int(self.data.pop(0).split('-')[0], 16)
print("Program base address {:#x}".format(self.baseAddr))
def resolveSym(self, addr):
addrProc = sp.Popen("addr2line -Cfse " + args.binary + " " + str(addr), shell=True, stdout=sp.PIPE)
return filter(lambda x: x, addrProc.stdout.read().split('\n'))
def resolveSyms(self, addrs):
# addr2line is surprisingly slow; resolves about 6 backtraces/sec
addrProc = sp.Popen("addr2line -Capfse " + args.binary + " " + str(addrs), shell=True, stdout=sp.PIPE)
return addrProc.stdout.read()
def parseLeaks(self):
ctr = Counter(self.data)
leakFreq = ctr.items()
leakFreq.sort(key=lambda x: x[1], reverse=True)
leakFreq = [str(x[1]) + "," + x[0] for x in leakFreq]
csvReader = csv.reader(leakFreq, delimiter=',')
skipped = 0
while True:
try:
row = csvReader.next()
except csv.Error:
skipped += 1
continue
except StopIteration:
break
leak = filter(lambda x: x, row)
count = int(leak[0])
elmBytes = int(leak[1])
totBytes = count * elmBytes
self.leaks.append({'count': count, 'elmBytes': elmBytes, 'totBytes': totBytes, 'backtrace': leak[2:]})
if skipped:
# Error rows are likely due to either a known bug in libunwind or
# GuardRail's current naughty use of SIGUSR2. They are rare enough
# it shouldn't really matter for leak tracking purposes...
print("Skipped %d erroneous leak record" % skipped)
def printLeaks(self):
self.parseLeaks()
totalBytesLeaked = 0
totalLeakCount = 0
numContexts = len(self.leaks)
for leak in self.leaks:
totalBytesLeaked += leak['totBytes']
totalLeakCount += leak['count']
print "Leaked total of {:,d} bytes across {:,d} leaks from {:,d} contexts"\
.format(totalBytesLeaked, totalLeakCount, numContexts)
if args.sortct:
self.leaks.sort(key=lambda x: x['count'], reverse=True)
else:
self.leaks.sort(key=lambda x: x['totBytes'], reverse=True)
context = 0
outStr = ""
for leak in self.leaks:
leakStr = "================================ Context {:>6,d} / {:,d} ================================\n"\
.format(context, numContexts)
leakStr += "Leaked {:,d} bytes across {:,d} allocations of {:,d} bytes each:\n"\
.format(leak['totBytes'], leak['count'], leak['elmBytes'])
leakNum = 0
if args.binary:
# XXX: Need to pull in shared lib bases here.
abs_addrs = [hex(int(x, 16) - self.baseAddr) for x in leak['backtrace']]
syms = self.resolveSyms(' '.join(abs_addrs))
skipLeak = False
for sym in syms.split('\n'):
if not sym:
continue
shortSym = re.sub(r'\(.*?\)', r'', sym)
for b in self.blist:
if re.search(b, shortSym):
skipLeak = True
leakStr += "#{: <2} {}\n".format(leakNum, shortSym)
leakNum += 1
if skipLeak:
continue
else:
outStr += leakStr
else:
for addr in leak['backtrace']:
outStr += "#{: <2} {} (No symbols, see -b option)\n".format(leakNum, addr)
leakNum += 1
context += 1
if context >= args.top:
break
print outStr
dumper = grDump()
dumper.loadData()
dumper.printLeaks()