-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcommands.py
435 lines (379 loc) · 14 KB
/
commands.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# Copyright (C) 2010 David Hugh Malcolm
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import gdb
import re
import sys
from heap.glibc import glibc_arenas
from heap.history import history, Snapshot, Diff
from heap import lazily_get_usage_list, \
fmt_size, fmt_addr, \
categorize, categorize_usage_list, Usage, \
hexdump_as_bytes, \
Table, \
MissingDebuginfo
def need_debuginfo(f):
def g(self, args, from_tty):
try:
return f(self, args, from_tty)
except MissingDebuginfo as e:
print('Missing debuginfo for %s' % e.module)
print('Suggested fix:')
print(' debuginfo-install %s' % e.module)
return g
class Heap(gdb.Command):
'Print a report on memory usage, by category'
def __init__(self):
gdb.Command.__init__ (self,
"heap",
gdb.COMMAND_DATA,
prefix=True)
@need_debuginfo
def invoke(self, args, from_tty):
total_by_category = {}
count_by_category = {}
total_size = 0
total_count = 0
try:
usage_list = list(lazily_get_usage_list())
for u in usage_list:
u.ensure_category()
total_size += u.size
if u.category in total_by_category:
total_by_category[u.category] += u.size
else:
total_by_category[u.category] = u.size
total_count += 1
if u.category in count_by_category:
count_by_category[u.category] += 1
else:
count_by_category[u.category] = 1
except KeyboardInterrupt:
pass # FIXME
t = Table(['Domain', 'Kind', 'Detail', 'Count', 'Allocated size'])
for category in sorted(total_by_category.keys(),
key=total_by_category.get,
reverse=True):
detail = category.detail
if not detail:
detail = ''
t.add_row([category.domain,
category.kind,
detail,
fmt_size(count_by_category[category]),
fmt_size(total_by_category[category]),
])
t.add_row(['', '', 'TOTAL', fmt_size(total_count), fmt_size(total_size)])
t.write(sys.stdout)
print()
class HeapSizes(gdb.Command):
'Print a report on memory usage, by sizes'
def __init__(self):
gdb.Command.__init__ (self,
"heap sizes",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
ms = glibc_arenas.get_ms()
chunks_by_size = {}
num_chunks = 0
total_size = 0
try:
for chunk in ms.iter_chunks():
if not chunk.is_inuse():
continue
size = int(chunk.chunksize())
num_chunks += 1
total_size += size
if size in chunks_by_size:
chunks_by_size[size] += 1
else:
chunks_by_size[size] = 1
except KeyboardInterrupt:
pass # FIXME
t = Table(['Chunk size', 'Num chunks', 'Allocated size'])
for size in sorted(chunks_by_size.keys(),
lambda s1, s2: chunks_by_size[s2] * s2 - chunks_by_size[s1] * s1):
t.add_row([fmt_size(size),
chunks_by_size[size],
fmt_size(chunks_by_size[size] * size)])
t.add_row(['TOTALS', num_chunks, fmt_size(total_size)])
t.write(sys.stdout)
print()
class HeapUsed(gdb.Command):
'Print used heap chunks'
def __init__(self):
gdb.Command.__init__ (self,
"heap used",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
print('Used chunks of memory on heap')
print('-----------------------------')
ms = glibc_arenas.get_ms()
for i, chunk in enumerate(ms.iter_chunks()):
if not chunk.is_inuse():
continue
size = chunk.chunksize()
mem = chunk.as_mem()
u = Usage(mem, size)
category = categorize(u, None)
hd = hexdump_as_bytes(mem, 32)
print ('%6i: %s -> %s %8i bytes %20s |%s'
% (i,
fmt_addr(chunk.as_mem()),
fmt_addr(chunk.as_mem()+size-1),
size, category, hd))
print()
class HeapFree(gdb.Command):
'Print free heap chunks'
def __init__(self):
gdb.Command.__init__ (self,
"heap free",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
print('Free chunks of memory on heap')
print('-----------------------------')
ms = glibc_arenas.get_ms()
total_size = 0
for i, chunk in enumerate(ms.iter_free_chunks()):
size = chunk.chunksize()
total_size += size
mem = chunk.as_mem()
u = Usage(mem, size)
category = categorize(u, None)
hd = hexdump_as_bytes(mem, 32)
print ('%6i: %s -> %s %8i bytes %20s |%s'
% (i,
fmt_addr(chunk.as_mem()),
fmt_addr(chunk.as_mem()+size-1),
size, category, hd))
print("Total size: %s" % total_size)
class HeapAll(gdb.Command):
'Print all heap chunks'
def __init__(self):
gdb.Command.__init__ (self,
"heap all",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
print('All chunks of memory on heap (both used and free)')
print('-------------------------------------------------')
ms = glibc_arenas.get_ms()
for i, chunk in enumerate(ms.iter_chunks()):
size = chunk.chunksize()
if chunk.is_inuse():
kind = ' inuse'
else:
kind = ' free'
print ('%i: %s -> %s %s: %i bytes (%s)'
% (i,
fmt_addr(chunk.as_address()),
fmt_addr(chunk.as_address()+size-1),
kind, size, chunk))
print()
class HeapLog(gdb.Command):
'Print a log of recorded heap states'
def __init__(self):
gdb.Command.__init__ (self,
"heap log",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
h = history
if len(h.snapshots) == 0:
print('(no history)')
return
for i in range(len(h.snapshots), 0, -1):
s = h.snapshots[i-1]
print('Label %i "%s" at %s' % (i, s.name, s.time))
print(' ', s.summary())
if i > 1:
prev = h.snapshots[i-2]
d = Diff(prev, s)
print()
print(' ', d.stats())
print()
class HeapLabel(gdb.Command):
'Record the current state of the heap for later comparison'
def __init__(self):
gdb.Command.__init__ (self,
"heap label",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
s = history.add(args)
print(s.summary())
class HeapDiff(gdb.Command):
'Compare two states of the heap'
def __init__(self):
gdb.Command.__init__ (self,
"heap diff",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
h = history
if len(h.snapshots) == 0:
print('(no history)')
return
prev = h.snapshots[-1]
curr = Snapshot.current('current')
d = Diff(prev, curr)
print('Changes from %s to %s' % (prev.name, curr.name))
print(' ', d.stats())
print()
print('\n'.join([' ' + line for line in d.as_changes().splitlines()]))
class HeapSelect(gdb.Command):
'Query used heap chunks'
def __init__(self):
gdb.Command.__init__ (self,
"heap select",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
from heap.query import do_query
from heap.parser import ParserError
try:
do_query(args)
except ParserError as e:
print(e)
class HeapRange(gdb.Command):
'''Print all non-empty merged memory ranges sorted by start address
Each line contains a start and end address; the end address is the
first not valid address.
'''
def __init__(self):
gdb.Command.__init__ (self,
"heap range",
gdb.COMMAND_DATA,
prefix = True)
def invoke(self, args, from_tty):
import ranges
for r in ranges.merged_ranges():
print('\t%s' % r)
class HeapRangeRun(gdb.Command):
'''Run a command for each range from `heap range`
Use $range_start, $range_last (end - 1) and $range_end variables in the
command.
Example: heap range run find /g $range_start, $range_last, some_value
'''
def __init__(self):
gdb.Command.__init__ (self,
"heap range run",
gdb.COMMAND_DATA,
gdb.COMPLETE_COMMAND)
def invoke(self, args, from_tty):
import ranges
for r in ranges.merged_ranges():
gdb.execute("set $range_start=%s" % fmt_addr(r.start))
gdb.execute("set $range_last=%s" % fmt_addr(r.last))
gdb.execute("set $range_end=%s" % fmt_addr(r.end))
gdb.execute(args, from_tty = True)
class HeapFind(gdb.Command):
'''Find stuff anywhere; supplies ranges to the find command
Example: heap find /g some_callback_function
'''
def __init__(self):
gdb.Command.__init__ (self,
"heap find",
gdb.COMMAND_DATA)
def invoke(self, args, from_tty):
import ranges
import re
from heap.compat import execute
FIND_SWITCHES = re.compile('^((?:\s*/\w+)*)(.*)$')
(switches, args) = FIND_SWITCHES.match(args).groups()
while len(args) > 0 and args[0][0] == '/':
switches.append(args.pop(0))
sumfound = 0
for r in ranges.merged_ranges():
result = execute('find%s %s, %s, %s' % (switches, fmt_addr(r.start), fmt_addr(r.last), args))
numfound = int(gdb.parse_and_eval('$numfound'))
sumfound += numfound
if numfound > 0:
result = result.splitlines()
result.pop() # no summary
for l in result:
print(l)
if sumfound > 0:
print ('%s patterns found' % sumfound)
else:
print ('Pattern not found')
class Hexdump(gdb.Command):
'Print a hexdump, starting at the specific region of memory'
def __init__(self):
gdb.Command.__init__ (self,
"hexdump",
gdb.COMMAND_DATA)
def invoke(self, args, from_tty):
print(repr(args))
arg_list = gdb.string_to_argv(args)
chars_only = True
if len(arg_list) == 2:
addr_arg = arg_list[0]
chars_only = True if args[1] == '-c' else False
else:
addr_arg = args
if addr_arg.startswith('0x'):
addr = int(addr_arg, 16)
else:
addr = int(addr_arg)
# assume that paging will cut in and the user will quit at some point:
size = 32
while True:
hd = hexdump_as_bytes(addr, size, chars_only=chars_only)
print ('%s -> %s %s' % (fmt_addr(addr), fmt_addr(addr + size -1), hd))
addr += size
class HeapArenas(gdb.Command):
'Display heap arenas available'
def __init__(self):
gdb.Command.__init__ (self,
"heap arenas",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
for n, arena in enumerate(glibc_arenas.arenas):
print("Arena #%d: %s" % (n, arena.address))
class HeapArenaSelect(gdb.Command):
'Select heap arena'
def __init__(self):
gdb.Command.__init__ (self,
"heap arena",
gdb.COMMAND_DATA)
@need_debuginfo
def invoke(self, args, from_tty):
arena_num = int(args)
glibc_arenas.cur_arena = glibc_arenas.arenas[arena_num]
print("Arena set to %s" % glibc_arenas.cur_arena.address)
def register_commands():
# Register the commands with gdb
Heap()
HeapSizes()
HeapUsed()
HeapFree()
HeapAll()
HeapLog()
HeapLabel()
HeapDiff()
HeapSelect()
HeapArenas()
HeapArenaSelect()
HeapRange()
HeapRangeRun()
HeapFind()
Hexdump()
from heap.cpython import register_commands as register_cpython_commands
register_cpython_commands()