[Lldb-commits] [lldb] r154671 - /lldb/trunk/examples/darwin/heap_find/heap.py

Greg Clayton gclayton at apple.com
Fri Apr 13 09:24:09 PDT 2012


Author: gclayton
Date: Fri Apr 13 11:24:09 2012
New Revision: 154671

URL: http://llvm.org/viewvc/llvm-project?rev=154671&view=rev
Log:
Added a --memory option to allow dumping the matching malloc block memory with a default format that makes sense, or that format can be overridden with the --format option.


Modified:
    lldb/trunk/examples/darwin/heap_find/heap.py

Modified: lldb/trunk/examples/darwin/heap_find/heap.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/examples/darwin/heap_find/heap.py?rev=154671&r1=154670&r2=154671&view=diff
==============================================================================
--- lldb/trunk/examples/darwin/heap_find/heap.py (original)
+++ lldb/trunk/examples/darwin/heap_find/heap.py Fri Apr 13 11:24:09 2012
@@ -21,12 +21,22 @@
 import os
 import shlex
 
+def add_common_options(parser):
+    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='memory', help='dump the memory for each matching block', default=False)
+    parser.add_option('-f', '--format', type='string', dest='format', help='the format to use when dumping memory if --memory is specified', default=None)
+    
 def heap_search(options, arg_str):
     expr = None
     arg_str_description = arg_str
+    default_memory_format = "Y" # 'Y' is "bytes with ASCII" format
+    #memory_chunk_size = 1
     if options.type == 'pointer':
         expr = 'find_pointer_in_heap((void *)%s)' % arg_str
         arg_str_description = 'malloc block containing pointer %s' % arg_str
+        default_memory_format = "A" # 'A' is "address" format
+        #memory_chunk_size = lldb.process.GetAddressByteSize()
     elif options.type == 'cstr':
         expr = 'find_cstring_in_heap("%s")' % arg_str
         arg_str_description = 'malloc block containing "%s"' % arg_str
@@ -63,8 +73,7 @@
                         data = bytearray(lldb.process.ReadMemory(malloc_addr, 16, error))
                         if data == '\xa1\xa1\xa1\xa1AUTORELEASE!':
                             description += ', type = (AUTORELEASE!)'
-                            print description
-                            continue
+                    print description
                 else:
                     description += ', type = %s' % (type_name)
                     derefed_dynamic_value = dynamic_value.deref
@@ -110,6 +119,15 @@
                         desc = dynamic_value.GetObjectDescription()
                         if desc:
                             print '  (%s) 0x%x %s\n' % (type_name, malloc_addr, desc)
+                if options.memory:
+                    memory_format = options.format
+                    if not memory_format:
+                        memory_format = default_memory_format
+                    cmd_result = lldb.SBCommandReturnObject()
+                    #count = malloc_size / memory_chunk_size
+                    memory_command = "memory read -f %s 0x%x 0x%x" % (memory_format, malloc_addr, malloc_addr + malloc_size)
+                    lldb.debugger.GetCommandInterpreter().HandleCommand(memory_command, cmd_result)
+                    print cmd_result.GetOutput()
         else:
             print '%s %s was not found in any malloc blocks' % (options.type, arg_str)
     else:
@@ -125,9 +143,7 @@
     and might be able to print what kind of objects the pointers are contained in using 
     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)
+    add_common_options(parser)
     try:
         (options, args) = parser.parse_args(command_args)
     except:
@@ -151,9 +167,7 @@
     and might be able to print what kind of objects the pointers are contained in 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)
+    add_common_options(parser)
     try:
         (options, args) = parser.parse_args(command_args)
     except:
@@ -177,9 +191,7 @@
     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)
+    add_common_options(parser)
     try:
         (options, args) = parser.parse_args(command_args)
     except:





More information about the lldb-commits mailing list