summaryrefslogtreecommitdiffstats
path: root/lldb/examples/darwin/heap_find/heap.py
diff options
context:
space:
mode:
authorGreg Clayton <gclayton@apple.com>2012-04-12 18:57:36 +0000
committerGreg Clayton <gclayton@apple.com>2012-04-12 18:57:36 +0000
commit7767716d0f3e3d717f238d0969029e302ee73a6f (patch)
tree357aedeba0adc768414eee9f2df3145ccb47b51e /lldb/examples/darwin/heap_find/heap.py
parentb1e2e848f3308d38c19f6d8d321df4f636be5f7c (diff)
downloadbcm5719-llvm-7767716d0f3e3d717f238d0969029e302ee73a6f.tar.gz
bcm5719-llvm-7767716d0f3e3d717f238d0969029e302ee73a6f.zip
A few tweaks done to the heap.py in me free time where we now have:
(lldb) command script import heap.py Find all malloc blocks that contains a pointer value of 0x1234000: (lldb) ptr_refs 0x1234000 Find all malloc blocks that contain a C string: (lldb) cstr_refs "hello" Get info on a malloc block that starts at or contains 0x12340000 (lldb) malloc_info 0x12340000 llvm-svn: 154602
Diffstat (limited to 'lldb/examples/darwin/heap_find/heap.py')
-rw-r--r--lldb/examples/darwin/heap_find/heap.py145
1 files changed, 93 insertions, 52 deletions
diff --git a/lldb/examples/darwin/heap_find/heap.py b/lldb/examples/darwin/heap_find/heap.py
index bb2a63961ab..982165e90ba 100644
--- a/lldb/examples/darwin/heap_find/heap.py
+++ b/lldb/examples/darwin/heap_find/heap.py
@@ -23,11 +23,16 @@ import shlex
def heap_search(options, arg_str):
expr = None
+ arg_str_description = arg_str
if options.type == 'pointer':
- ptr = int(arg_str, 0)
- expr = 'find_pointer_in_heap(0x%x)' % ptr
+ expr = 'find_pointer_in_heap(%s)' % arg_str
+ arg_str_description = 'malloc block containing pointer %s' % arg_str
elif options.type == 'cstr':
expr = 'find_cstring_in_heap("%s")' % arg_str
+ arg_str_description = 'malloc block containing "%s"' % arg_str
+ elif options.type == 'addr':
+ expr = 'find_block_for_address(%s)' % arg_str
+ arg_str_description = 'malloc block for %s' % arg_str
else:
print 'error: invalid type "%s"\nvalid values are "pointer", "cstr"' % options.type
return
@@ -48,73 +53,81 @@ def heap_search(options, arg_str):
# If the type is still 'void *' then we weren't able to figure
# out a dynamic type for the malloc_addr
type_name = dynamic_value.type.name
+ description = '[%u] %s: addr = 0x%x' % (i, arg_str_description, malloc_addr)
+ if offset != 0:
+ description += ' + %u' % (offset)
+ description += ', size = %u' % (malloc_size)
if type_name == 'void *':
if options.type == 'pointer' and malloc_size == 4096:
error = lldb.SBError()
data = bytearray(lldb.process.ReadMemory(malloc_addr, 16, error))
if data == '\xa1\xa1\xa1\xa1AUTORELEASE!':
- print 'found %s %s: block = 0x%x, size = %u, offset = %u, type = (autorelease object pool)' % (options.type, arg_str, malloc_addr, malloc_size, offset)
+ description += ', type = (AUTORELEASE!)'
+ print description
continue
-
- print 'found %s %s: block = 0x%x, size = %u, offset = %u, type = \'%s\'' % (options.type, arg_str, malloc_addr, malloc_size, offset, type_name),
- derefed_dynamic_value = dynamic_value.deref
- ivar_member = None
- if derefed_dynamic_value:
- derefed_dynamic_type = derefed_dynamic_value.type
- member = derefed_dynamic_type.GetFieldAtIndex(0)
- search_bases = False
- if member:
- if member.GetOffsetInBytes() <= offset:
- for field_idx in range (derefed_dynamic_type.GetNumberOfFields()):
- member = derefed_dynamic_type.GetFieldAtIndex(field_idx)
- member_byte_offset = member.GetOffsetInBytes()
- if member_byte_offset == offset:
- ivar_member = member
- break
+ else:
+ description += ', type = %s' % (type_name)
+ derefed_dynamic_value = dynamic_value.deref
+ ivar_member = None
+ if derefed_dynamic_value:
+ derefed_dynamic_type = derefed_dynamic_value.type
+ member = derefed_dynamic_type.GetFieldAtIndex(0)
+ search_bases = False
+ if member:
+ if member.GetOffsetInBytes() <= offset:
+ for field_idx in range (derefed_dynamic_type.GetNumberOfFields()):
+ member = derefed_dynamic_type.GetFieldAtIndex(field_idx)
+ member_byte_offset = member.GetOffsetInBytes()
+ if member_byte_offset == offset:
+ ivar_member = member
+ break
+ else:
+ search_bases = True
else:
search_bases = True
- else:
- search_bases = True
-
- if not ivar_member and search_bases:
- for field_idx in range (derefed_dynamic_type.GetNumberOfDirectBaseClasses()):
- member = derefed_dynamic_type.GetDirectBaseClassAtIndex(field_idx)
- member_byte_offset = member.GetOffsetInBytes()
- if member_byte_offset == offset:
- ivar_member = member
- break
- if not ivar_member:
- for field_idx in range (derefed_dynamic_type.GetNumberOfVirtualBaseClasses()):
- member = derefed_dynamic_type.GetVirtualBaseClassAtIndex(field_idx)
+
+ if not ivar_member and search_bases:
+ for field_idx in range (derefed_dynamic_type.GetNumberOfDirectBaseClasses()):
+ member = derefed_dynamic_type.GetDirectBaseClassAtIndex(field_idx)
member_byte_offset = member.GetOffsetInBytes()
if member_byte_offset == offset:
ivar_member = member
break
+ if not ivar_member:
+ for field_idx in range (derefed_dynamic_type.GetNumberOfVirtualBaseClasses()):
+ member = derefed_dynamic_type.GetVirtualBaseClassAtIndex(field_idx)
+ member_byte_offset = member.GetOffsetInBytes()
+ if member_byte_offset == offset:
+ ivar_member = member
+ break
if ivar_member:
- print ", ivar = %s" % ivar_member.name,
- print "\n", dynamic_value.deref
- else:
- print
- if options.print_object_description:
- desc = dynamic_value.GetObjectDescription()
- if desc:
- print ' (%s) 0x%x %s\n' % (type_name, malloc_addr, desc)
+ description +=', ivar = %s' % (ivar_member.name)
+
+ print description
+ if derefed_dynamic_value:
+ print derefed_dynamic_value
+ if options.print_object_description:
+ desc = dynamic_value.GetObjectDescription()
+ if desc:
+ print ' (%s) 0x%x %s\n' % (type_name, malloc_addr, desc)
else:
print '%s %s was not found in any malloc blocks' % (options.type, arg_str)
else:
- print expr_sbvalue.error
+ print expr_sbvalue.error
+ print
-def heap_ptr_refs(debugger, command, result, dict):
+def ptr_refs(debugger, command, result, dict):
command_args = shlex.split(command)
- usage = "usage: %prog [options] <PATH> [PATH ...]"
+ usage = "usage: %prog [options] <PTR> [PTR ...]"
description='''Searches the heap for pointer references on darwin user space programs.
Any matches that were found will dump the malloc blocks that contain the pointers
and might be able to print what kind of objects the pointers are contained in using
- dynamic type information from the program.'''
- parser = optparse.OptionParser(description=description, prog='heap_ptr_refs',usage=usage)
+ dynamic type information in the program.'''
+ parser = optparse.OptionParser(description=description, prog='ptr_refs',usage=usage)
parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False)
parser.add_option('-o', '--po', action='store_true', dest='print_object_description', help='print the object descriptions for any matches', default=False)
+ parser.add_option('-m', '--memory', action='store_true', dest='show_memory', help='dump the memory for each matching block', default=False)
try:
(options, args) = parser.parse_args(command_args)
except:
@@ -129,17 +142,18 @@ def heap_ptr_refs(debugger, command, result, dict):
else:
print 'error: no pointer arguments were given'
-def heap_cstr_refs(debugger, command, result, dict):
+def cstr_refs(debugger, command, result, dict):
command_args = shlex.split(command)
- usage = "usage: %prog [options] <PATH> [PATH ...]"
+ usage = "usage: %prog [options] <CSTR> [CSTR ...]"
description='''Searches the heap for C string references on darwin user space programs.
Any matches that were found will dump the malloc blocks that contain the C strings
and might be able to print what kind of objects the pointers are contained in using
- dynamic type information from the program.'''
- parser = optparse.OptionParser(description=description, prog='heap_cstr_refs',usage=usage)
+ dynamic type information in the program.'''
+ parser = optparse.OptionParser(description=description, prog='cstr_refs',usage=usage)
parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False)
parser.add_option('-o', '--po', action='store_true', dest='print_object_description', help='print the object descriptions for any matches', default=False)
+ parser.add_option('-m', '--memory', action='store_true', dest='show_memory', help='dump the memory for each matching block', default=False)
try:
(options, args) = parser.parse_args(command_args)
except:
@@ -154,14 +168,41 @@ def heap_cstr_refs(debugger, command, result, dict):
else:
print 'error: no c string arguments were given to search for'
+def malloc_info(debugger, command, result, dict):
+ command_args = shlex.split(command)
+ usage = "usage: %prog [options] <ADDR> [ADDR ...]"
+ description='''Searches the heap a malloc block that contains the addresses specified as arguments.
+
+ Any matches that were found will dump the malloc blocks that match or contain
+ the specified address. The matching blocks might be able to show what kind
+ of objects they are using dynamic type information in the program.'''
+ parser = optparse.OptionParser(description=description, prog='cstr_refs',usage=usage)
+ parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False)
+ parser.add_option('-o', '--po', action='store_true', dest='print_object_description', help='print the object descriptions for any matches', default=False)
+ parser.add_option('-m', '--memory', action='store_true', dest='show_memory', help='dump the memory for each matching block', default=False)
+ try:
+ (options, args) = parser.parse_args(command_args)
+ except:
+ return
+
+ options.type = 'addr'
+
+ if args:
+
+ for data in args:
+ heap_search (options, data)
+ else:
+ print 'error: no c string arguments were given to search for'
+
def __lldb_init_module (debugger, dict):
# This initializer is being run from LLDB in the embedded command interpreter
# Add any commands contained in this module to LLDB
libheap_dylib_path = os.path.dirname(__file__) + '/libheap.dylib'
debugger.HandleCommand('process load "%s"' % libheap_dylib_path)
- debugger.HandleCommand('command script add -f heap.heap_ptr_refs heap_ptr_refs')
- debugger.HandleCommand('command script add -f heap.heap_cstr_refs heap_cstr_refs')
- print '"heap_ptr_refs" and "heap_cstr_refs" commands have been installed, use the "--help" options on these commands for detailed help.'
+ debugger.HandleCommand('command script add -f heap.ptr_refs ptr_refs')
+ debugger.HandleCommand('command script add -f heap.cstr_refs cstr_refs')
+ debugger.HandleCommand('command script add -f heap.malloc_info malloc_info')
+ print '"ptr_refs", "cstr_refs", and "malloc_info" commands have been installed, use the "--help" options on these commands for detailed help.'
OpenPOWER on IntegriCloud