Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | #---------------------------------------------------------------------- |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 4 | # This module is designed to live inside the "lldb" python package |
| 5 | # in the "lldb.macosx" package. To use this in the embedded python |
| 6 | # interpreter using "lldb" just import it: |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 7 | # |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 8 | # (lldb) script import lldb.macosx.heap |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 9 | #---------------------------------------------------------------------- |
| 10 | |
| 11 | import lldb |
| 12 | import commands |
| 13 | import optparse |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 14 | import os |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 15 | import os.path |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 16 | import re |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 17 | import shlex |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 18 | import string |
| 19 | import tempfile |
Greg Clayton | 6f2f0ab | 2012-04-25 01:49:50 +0000 | [diff] [blame] | 20 | import lldb.utils.symbolication |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 21 | |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 22 | g_libheap_dylib_dir = None |
| 23 | g_libheap_dylib_dict = dict() |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 24 | g_verbose = False |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 25 | |
Greg Clayton | 9098fee | 2012-04-21 00:11:26 +0000 | [diff] [blame] | 26 | def load_dylib(): |
| 27 | if lldb.target: |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 28 | global g_libheap_dylib_dir |
| 29 | global g_libheap_dylib_dict |
| 30 | triple = lldb.target.triple |
Greg Clayton | 3e33979 | 2012-04-25 21:23:07 +0000 | [diff] [blame] | 31 | if triple in g_libheap_dylib_dict: |
| 32 | libheap_dylib_path = g_libheap_dylib_dict[triple] |
| 33 | else: |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 34 | if not g_libheap_dylib_dir: |
Greg Clayton | 3e33979 | 2012-04-25 21:23:07 +0000 | [diff] [blame] | 35 | g_libheap_dylib_dir = tempfile.gettempdir() + '/lldb-dylibs' |
| 36 | triple_dir = g_libheap_dylib_dir + '/' + triple + '/' + __name__ |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 37 | if not os.path.exists(triple_dir): |
Greg Clayton | 3e33979 | 2012-04-25 21:23:07 +0000 | [diff] [blame] | 38 | os.makedirs(triple_dir) |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 39 | libheap_dylib_path = triple_dir + '/libheap.dylib' |
| 40 | g_libheap_dylib_dict[triple] = libheap_dylib_path |
Greg Clayton | 3e33979 | 2012-04-25 21:23:07 +0000 | [diff] [blame] | 41 | heap_code_directory = os.path.dirname(__file__) + '/heap' |
| 42 | heap_source_file = heap_code_directory + '/heap_find.cpp' |
| 43 | # Check if the dylib doesn't exist, or if "heap_find.cpp" is newer than the dylib |
| 44 | if not os.path.exists(libheap_dylib_path) or os.stat(heap_source_file).st_mtime > os.stat(libheap_dylib_path).st_mtime: |
| 45 | # Remake the dylib |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 46 | make_command = '(cd "%s" ; make EXE="%s" ARCH=%s)' % (heap_code_directory, libheap_dylib_path, string.split(triple, '-')[0]) |
Greg Clayton | 3e33979 | 2012-04-25 21:23:07 +0000 | [diff] [blame] | 47 | # print make_command |
Greg Clayton | 0cae063 | 2012-06-27 19:57:59 +0000 | [diff] [blame] | 48 | (make_exit_status, make_output) = commands.getstatusoutput(make_command) |
| 49 | if make_exit_status != 0: |
| 50 | print make_output |
Greg Clayton | 9098fee | 2012-04-21 00:11:26 +0000 | [diff] [blame] | 51 | if os.path.exists(libheap_dylib_path): |
| 52 | libheap_dylib_spec = lldb.SBFileSpec(libheap_dylib_path) |
| 53 | if lldb.target.FindModule(libheap_dylib_spec): |
| 54 | return None # success, 'libheap.dylib' already loaded |
| 55 | if lldb.process: |
| 56 | state = lldb.process.state |
| 57 | if state == lldb.eStateStopped: |
| 58 | (libheap_dylib_path) |
| 59 | error = lldb.SBError() |
| 60 | image_idx = lldb.process.LoadImage(libheap_dylib_spec, error) |
| 61 | if error.Success(): |
| 62 | return None |
| 63 | else: |
| 64 | if error: |
| 65 | return 'error: %s' % error |
| 66 | else: |
| 67 | return 'error: "process load \'%s\'" failed' % libheap_dylib_spec |
| 68 | else: |
| 69 | return 'error: process is not stopped' |
| 70 | else: |
| 71 | return 'error: invalid process' |
| 72 | else: |
| 73 | return 'error: file does not exist "%s"' % libheap_dylib_path |
| 74 | else: |
| 75 | return 'error: invalid target' |
| 76 | |
| 77 | debugger.HandleCommand('process load "%s"' % libheap_dylib_path) |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 78 | if lldb.target.FindModule(libheap_dylib_spec): |
| 79 | return None # success, 'libheap.dylib' already loaded |
| 80 | return 'error: failed to load "%s"' % libheap_dylib_path |
Greg Clayton | 0d0f56d | 2012-07-07 01:22:45 +0000 | [diff] [blame] | 81 | |
| 82 | def get_member_types_for_offset(value_type, offset, member_list): |
| 83 | member = value_type.GetFieldAtIndex(0) |
| 84 | search_bases = False |
| 85 | if member: |
| 86 | if member.GetOffsetInBytes() <= offset: |
| 87 | for field_idx in range (value_type.GetNumberOfFields()): |
| 88 | member = value_type.GetFieldAtIndex(field_idx) |
| 89 | member_byte_offset = member.GetOffsetInBytes() |
| 90 | member_end_byte_offset = member_byte_offset + member.type.size |
| 91 | if member_byte_offset <= offset and offset < member_end_byte_offset: |
| 92 | member_list.append(member) |
| 93 | get_member_types_for_offset (member.type, offset - member_byte_offset, member_list) |
| 94 | return |
| 95 | else: |
| 96 | search_bases = True |
| 97 | else: |
| 98 | search_bases = True |
| 99 | if search_bases: |
| 100 | for field_idx in range (value_type.GetNumberOfDirectBaseClasses()): |
| 101 | member = value_type.GetDirectBaseClassAtIndex(field_idx) |
| 102 | member_byte_offset = member.GetOffsetInBytes() |
| 103 | member_end_byte_offset = member_byte_offset + member.type.size |
| 104 | if member_byte_offset <= offset and offset < member_end_byte_offset: |
| 105 | member_list.append(member) |
| 106 | get_member_types_for_offset (member.type, offset - member_byte_offset, member_list) |
| 107 | return |
| 108 | for field_idx in range (value_type.GetNumberOfVirtualBaseClasses()): |
| 109 | member = value_type.GetVirtualBaseClassAtIndex(field_idx) |
| 110 | member_byte_offset = member.GetOffsetInBytes() |
| 111 | member_end_byte_offset = member_byte_offset + member.type.size |
| 112 | if member_byte_offset <= offset and offset < member_end_byte_offset: |
| 113 | member_list.append(member) |
| 114 | get_member_types_for_offset (member.type, offset - member_byte_offset, member_list) |
| 115 | return |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 116 | |
| 117 | def append_regex_callback(option, opt, value, parser): |
| 118 | try: |
| 119 | ivar_regex = re.compile(value) |
| 120 | parser.values.ivar_regex_blacklist.append(ivar_regex) |
| 121 | except: |
| 122 | print 'error: an exception was thrown when compiling the ivar regular expression for "%s"' % value |
Greg Clayton | 9098fee | 2012-04-21 00:11:26 +0000 | [diff] [blame] | 123 | |
Greg Clayton | 93e5ba5 | 2012-04-13 16:24:09 +0000 | [diff] [blame] | 124 | def add_common_options(parser): |
| 125 | parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 126 | parser.add_option('-t', '--type', action='store_true', dest='print_type', help='print the full value of the type for each matching malloc block', default=False) |
Greg Clayton | 93e5ba5 | 2012-04-13 16:24:09 +0000 | [diff] [blame] | 127 | parser.add_option('-o', '--po', action='store_true', dest='print_object_description', help='print the object descriptions for any matches', default=False) |
| 128 | parser.add_option('-m', '--memory', action='store_true', dest='memory', help='dump the memory for each matching block', default=False) |
| 129 | parser.add_option('-f', '--format', type='string', dest='format', help='the format to use when dumping memory if --memory is specified', default=None) |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 130 | parser.add_option('-I', '--omit-ivar-regex', type='string', action='callback', callback=append_regex_callback, dest='ivar_regex_blacklist', default=[], help='specify one or more regular expressions used to backlist any matches that are in ivars') |
Greg Clayton | 9098fee | 2012-04-21 00:11:26 +0000 | [diff] [blame] | 131 | parser.add_option('-s', '--stack', action='store_true', dest='stack', help='gets the stack that allocated each malloc block if MallocStackLogging is enabled', default=False) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 132 | parser.add_option('-S', '--stack-history', action='store_true', dest='stack_history', help='gets the stack history for all allocations whose start address matches each malloc block if MallocStackLogging is enabled', default=False) |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 133 | parser.add_option('-M', '--max-matches', type='int', dest='max_matches', help='the maximum number of matches to print', default=256) |
Greg Clayton | ab20f29 | 2012-08-11 02:26:26 +0000 | [diff] [blame^] | 134 | parser.add_option('-O', '--offset', type='int', dest='offset', help='the matching data must be at this offset', default=-1) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 135 | |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 136 | def dump_stack_history_entry(stack_history_entry, idx): |
Greg Clayton | 6f446f3 | 2012-05-10 23:37:52 +0000 | [diff] [blame] | 137 | address = int(stack_history_entry.address) |
| 138 | if address: |
| 139 | type_flags = int(stack_history_entry.type_flags) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 140 | symbolicator = lldb.utils.symbolication.Symbolicator() |
| 141 | symbolicator.target = lldb.target |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 142 | type_str = '' |
| 143 | if type_flags == 0: |
| 144 | type_str = 'free' |
| 145 | else: |
| 146 | if type_flags & 2: |
| 147 | type_str = 'alloc' |
| 148 | elif type_flags & 4: |
| 149 | type_str = 'free' |
| 150 | elif type_flags & 1: |
| 151 | type_str = 'generic' |
| 152 | else: |
| 153 | type_str = hex(type_flags) |
| 154 | print 'stack[%u]: addr = 0x%x, type=%s, frames:' % (idx, address, type_str) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 155 | frame_idx = 0 |
Greg Clayton | 6f446f3 | 2012-05-10 23:37:52 +0000 | [diff] [blame] | 156 | idx = 0 |
| 157 | pc = int(stack_history_entry.frames[idx]) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 158 | while pc != 0: |
| 159 | if pc >= 0x1000: |
| 160 | frames = symbolicator.symbolicate(pc) |
| 161 | if frames: |
| 162 | for frame in frames: |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 163 | print ' [%u] %s' % (frame_idx, frame) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 164 | frame_idx += 1 |
| 165 | else: |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 166 | print ' [%u] 0x%x' % (frame_idx, pc) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 167 | frame_idx += 1 |
Greg Clayton | 6f446f3 | 2012-05-10 23:37:52 +0000 | [diff] [blame] | 168 | idx = idx + 1 |
| 169 | pc = int(stack_history_entry.frames[idx]) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 170 | else: |
| 171 | pc = 0 |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 172 | print |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 173 | |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 174 | def dump_stack_history_entries(addr, history): |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 175 | # malloc_stack_entry *get_stack_history_for_address (const void * addr) |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 176 | expr = 'get_stack_history_for_address((void *)0x%x, %u)' % (addr, history) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 177 | expr_sbvalue = lldb.frame.EvaluateExpression (expr) |
| 178 | if expr_sbvalue.error.Success(): |
| 179 | if expr_sbvalue.unsigned: |
| 180 | expr_value = lldb.value(expr_sbvalue) |
| 181 | idx = 0; |
| 182 | stack_history_entry = expr_value[idx] |
Greg Clayton | 6f446f3 | 2012-05-10 23:37:52 +0000 | [diff] [blame] | 183 | while int(stack_history_entry.address) != 0: |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 184 | dump_stack_history_entry(stack_history_entry, idx) |
| 185 | idx = idx + 1 |
| 186 | stack_history_entry = expr_value[idx] |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 187 | else: |
| 188 | print 'error: expression failed "%s" => %s' % (expr, expr_sbvalue.error) |
Greg Clayton | 7a24576 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 189 | |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 190 | |
| 191 | def display_match_results (options, arg_str_description, expr_sbvalue, print_no_matches = True): |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 192 | if expr_sbvalue.error.Success(): |
| 193 | if expr_sbvalue.unsigned: |
| 194 | match_value = lldb.value(expr_sbvalue) |
| 195 | i = 0 |
Greg Clayton | ab20f29 | 2012-08-11 02:26:26 +0000 | [diff] [blame^] | 196 | match_idx = 0 |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 197 | while 1: |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 198 | print_entry = True |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 199 | match_entry = match_value[i]; i += 1 |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 200 | if i >= options.max_matches: |
| 201 | print 'error: the max number of matches (%u) was reached, use the --max-matches option to get more results' % (options.max_matches) |
| 202 | break |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 203 | malloc_addr = match_entry.addr.sbvalue.unsigned |
| 204 | if malloc_addr == 0: |
| 205 | break |
| 206 | malloc_size = int(match_entry.size) |
| 207 | offset = int(match_entry.offset) |
Greg Clayton | ab20f29 | 2012-08-11 02:26:26 +0000 | [diff] [blame^] | 208 | |
| 209 | if options.offset >= 0 and options.offset != offset: |
| 210 | print_entry = False |
| 211 | else: |
| 212 | match_addr = malloc_addr + offset |
| 213 | dynamic_value = match_entry.addr.sbvalue.GetDynamicValue(lldb.eDynamicCanRunTarget) |
| 214 | description = '[%u] %s: addr = 0x%x' % (match_idx, arg_str_description, malloc_addr) |
| 215 | if offset != 0: |
| 216 | description += ' + %u' % (offset) |
| 217 | description += ', size = %u' % (malloc_size) |
| 218 | derefed_dynamic_value = None |
| 219 | if dynamic_value.type.name == 'void *': |
| 220 | if options.type == 'pointer' and malloc_size == 4096: |
| 221 | error = lldb.SBError() |
| 222 | data = bytearray(lldb.process.ReadMemory(malloc_addr, 16, error)) |
| 223 | if data == '\xa1\xa1\xa1\xa1AUTORELEASE!': |
| 224 | description += ', type = (AUTORELEASE!)' |
| 225 | else: |
| 226 | derefed_dynamic_value = dynamic_value.deref |
| 227 | if derefed_dynamic_value: |
| 228 | derefed_dynamic_type = derefed_dynamic_value.type |
| 229 | derefed_dynamic_type_size = derefed_dynamic_type.size |
| 230 | derefed_dynamic_type_name = derefed_dynamic_type.name |
| 231 | description += ', type = %s <%u>' % (derefed_dynamic_type_name, derefed_dynamic_type_size) |
| 232 | if offset < derefed_dynamic_type_size: |
| 233 | member_list = list(); |
| 234 | get_member_types_for_offset (derefed_dynamic_type, offset, member_list) |
| 235 | if member_list: |
| 236 | member_path = '' |
| 237 | for member in member_list: |
| 238 | member_name = member.name |
| 239 | if member_name: |
| 240 | if member_path: |
| 241 | member_path += '.' |
| 242 | member_path += member_name |
| 243 | if member_path: |
| 244 | if options.ivar_regex_blacklist: |
| 245 | for ivar_regex in options.ivar_regex_blacklist: |
| 246 | if ivar_regex.match(member_path): |
| 247 | print_entry = False |
| 248 | description += ', ivar = %s' % (member_path) |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 249 | if print_entry: |
Greg Clayton | ab20f29 | 2012-08-11 02:26:26 +0000 | [diff] [blame^] | 250 | match_idx += 1 |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 251 | if description: |
| 252 | print description |
| 253 | if options.print_type and derefed_dynamic_value: |
| 254 | print derefed_dynamic_value |
| 255 | if options.print_object_description and dynamic_value: |
| 256 | desc = dynamic_value.GetObjectDescription() |
| 257 | if desc: |
Greg Clayton | ab20f29 | 2012-08-11 02:26:26 +0000 | [diff] [blame^] | 258 | print ', po=%s\n' % (desc) |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 259 | if options.memory: |
| 260 | cmd_result = lldb.SBCommandReturnObject() |
| 261 | memory_command = "memory read -f %s 0x%x 0x%x" % (options.format, malloc_addr, malloc_addr + malloc_size) |
| 262 | lldb.debugger.GetCommandInterpreter().HandleCommand(memory_command, cmd_result) |
| 263 | print cmd_result.GetOutput() |
| 264 | if options.stack_history: |
| 265 | dump_stack_history_entries(malloc_addr, 1) |
| 266 | elif options.stack: |
| 267 | dump_stack_history_entries(malloc_addr, 0) |
| 268 | return i |
| 269 | elif print_no_matches: |
| 270 | print 'no matches found for %s' % (arg_str_description) |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 271 | else: |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 272 | print expr_sbvalue.error |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 273 | return 0 |
| 274 | |
| 275 | def heap_search(options, arg_str): |
| 276 | dylid_load_err = load_dylib() |
| 277 | if dylid_load_err: |
| 278 | print dylid_load_err |
| 279 | return |
| 280 | expr = None |
| 281 | arg_str_description = arg_str |
| 282 | if options.format == None: |
| 283 | options.format = "Y" # 'Y' is "bytes with ASCII" format |
| 284 | if options.type == 'pointer': |
| 285 | expr = 'find_pointer_in_heap((void *)%s)' % (arg_str) |
| 286 | arg_str_description = 'malloc block containing pointer %s' % arg_str |
| 287 | if options.format == None: |
| 288 | options.format = "A" # 'A' is "address" format |
Greg Clayton | ab20f29 | 2012-08-11 02:26:26 +0000 | [diff] [blame^] | 289 | elif options.type == 'isa': |
| 290 | expr = 'find_pointer_in_heap((void *)%s)' % (arg_str) |
| 291 | arg_str_description = 'objective C classes with isa %s' % arg_str |
| 292 | options.offset = 0 |
| 293 | if options.format == None: |
| 294 | options.format = "A" # 'A' is "address" format |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 295 | elif options.type == 'cstr': |
| 296 | expr = 'find_cstring_in_heap("%s")' % arg_str |
| 297 | arg_str_description = 'malloc block containing "%s"' % arg_str |
| 298 | elif options.type == 'addr': |
| 299 | expr = 'find_block_for_address((void *)%s)' % arg_str |
| 300 | arg_str_description = 'malloc block for %s' % arg_str |
| 301 | else: |
| 302 | print 'error: invalid type "%s"\nvalid values are "pointer", "cstr"' % options.type |
| 303 | return |
| 304 | |
| 305 | display_match_results (options, arg_str_description, lldb.frame.EvaluateExpression (expr)) |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 306 | |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 307 | def ptr_refs(debugger, command, result, dict): |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 308 | command_args = shlex.split(command) |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 309 | usage = "usage: %prog [options] <EXPR> [EXPR ...]" |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 310 | description='''Searches the heap for pointer references on darwin user space programs. |
| 311 | |
| 312 | Any matches that were found will dump the malloc blocks that contain the pointers |
| 313 | and might be able to print what kind of objects the pointers are contained in using |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 314 | dynamic type information in the program.''' |
| 315 | parser = optparse.OptionParser(description=description, prog='ptr_refs',usage=usage) |
Greg Clayton | 93e5ba5 | 2012-04-13 16:24:09 +0000 | [diff] [blame] | 316 | add_common_options(parser) |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 317 | try: |
| 318 | (options, args) = parser.parse_args(command_args) |
| 319 | except: |
| 320 | return |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 321 | |
| 322 | options.type = 'pointer' |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 323 | |
| 324 | if args: |
| 325 | |
| 326 | for data in args: |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 327 | heap_search (options, data) |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 328 | else: |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 329 | print 'error: no pointer arguments were given' |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 330 | |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 331 | def cstr_refs(debugger, command, result, dict): |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 332 | command_args = shlex.split(command) |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 333 | usage = "usage: %prog [options] <CSTR> [CSTR ...]" |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 334 | description='''Searches the heap for C string references on darwin user space programs. |
| 335 | |
| 336 | Any matches that were found will dump the malloc blocks that contain the C strings |
| 337 | and might be able to print what kind of objects the pointers are contained in using |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 338 | dynamic type information in the program.''' |
| 339 | parser = optparse.OptionParser(description=description, prog='cstr_refs',usage=usage) |
Greg Clayton | 93e5ba5 | 2012-04-13 16:24:09 +0000 | [diff] [blame] | 340 | add_common_options(parser) |
Greg Clayton | 9666644 | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 341 | try: |
| 342 | (options, args) = parser.parse_args(command_args) |
| 343 | except: |
| 344 | return |
| 345 | |
| 346 | options.type = 'cstr' |
| 347 | |
| 348 | if args: |
| 349 | |
| 350 | for data in args: |
| 351 | heap_search (options, data) |
| 352 | else: |
| 353 | print 'error: no c string arguments were given to search for' |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 354 | |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 355 | def malloc_info(debugger, command, result, dict): |
| 356 | command_args = shlex.split(command) |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 357 | usage = "usage: %prog [options] <EXPR> [EXPR ...]" |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 358 | description='''Searches the heap a malloc block that contains the addresses specified as arguments. |
| 359 | |
| 360 | Any matches that were found will dump the malloc blocks that match or contain |
| 361 | the specified address. The matching blocks might be able to show what kind |
| 362 | of objects they are using dynamic type information in the program.''' |
| 363 | parser = optparse.OptionParser(description=description, prog='cstr_refs',usage=usage) |
Greg Clayton | 93e5ba5 | 2012-04-13 16:24:09 +0000 | [diff] [blame] | 364 | add_common_options(parser) |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 365 | try: |
| 366 | (options, args) = parser.parse_args(command_args) |
| 367 | except: |
| 368 | return |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 369 | options.type = 'addr' |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 370 | if args: |
Greg Clayton | bff7841 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 371 | for data in args: |
| 372 | heap_search (options, data) |
| 373 | else: |
| 374 | print 'error: no c string arguments were given to search for' |
| 375 | |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 376 | def malloc_history(debugger, command, result, dict): |
| 377 | command_args = shlex.split(command) |
| 378 | usage = "usage: %prog [options] <EXPR> [EXPR ...]" |
| 379 | description='''Gets the allocation history for an expression whose result is an address. |
| 380 | |
| 381 | Programs should set the MallocStackLoggingNoCompact=1 in the environment to enable stack history. This can be done |
| 382 | with "process launch -v MallocStackLoggingNoCompact=1 -- [arg1 ...]"''' |
| 383 | |
| 384 | dylid_load_err = load_dylib() |
| 385 | if dylid_load_err: |
| 386 | print dylid_load_err |
| 387 | else: |
| 388 | if command_args: |
| 389 | for addr_expr_str in command_args: |
| 390 | expr_sbvalue = lldb.frame.EvaluateExpression (addr_expr_str) |
| 391 | if expr_sbvalue.error.Success(): |
| 392 | addr = expr_sbvalue.unsigned |
| 393 | if addr != 0: |
| 394 | dump_stack_history_entries (addr, 1) |
| 395 | else: |
| 396 | print 'error: expression error for "%s": %s' % (addr_expr_str, expr_sbvalue.error) |
| 397 | else: |
| 398 | print 'error: no address expressions were specified' |
| 399 | |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 400 | def section_ptr_refs(debugger, command, result, dict): |
| 401 | command_args = shlex.split(command) |
| 402 | usage = "usage: %prog [options] <EXPR> [EXPR ...]" |
| 403 | description='''Searches section contents for pointer values in darwin user space programs.''' |
| 404 | parser = optparse.OptionParser(description=description, prog='section_ptr_refs',usage=usage) |
| 405 | add_common_options(parser) |
| 406 | parser.add_option('--section', action='append', type='string', dest='section_names', help='section name to search', default=list()) |
| 407 | try: |
| 408 | (options, args) = parser.parse_args(command_args) |
| 409 | except: |
| 410 | return |
| 411 | |
| 412 | options.type = 'pointer' |
| 413 | |
| 414 | sections = list() |
| 415 | section_modules = list() |
| 416 | if not options.section_names: |
| 417 | print 'error: at least one section must be specified with the --section option' |
| 418 | return |
| 419 | |
| 420 | for module in lldb.target.modules: |
| 421 | for section_name in options.section_names: |
| 422 | section = module.section[section_name] |
| 423 | if section: |
| 424 | sections.append (section) |
| 425 | section_modules.append (module) |
| 426 | if sections: |
| 427 | dylid_load_err = load_dylib() |
| 428 | if dylid_load_err: |
| 429 | print dylid_load_err |
| 430 | return |
| 431 | for expr_str in args: |
| 432 | for (idx, section) in enumerate(sections): |
| 433 | expr = 'find_pointer_in_memory(0x%xllu, %ullu, (void *)%s)' % (section.addr.load_addr, section.size, expr_str) |
| 434 | arg_str_description = 'section %s.%s containing "%s"' % (section_modules[idx].file.fullpath, section.name, expr_str) |
| 435 | num_matches = display_match_results (options, arg_str_description, lldb.frame.EvaluateExpression (expr), False) |
| 436 | if num_matches: |
| 437 | if num_matches < options.max_matches: |
| 438 | options.max_matches = options.max_matches - num_matches |
| 439 | else: |
| 440 | options.max_matches = 0 |
| 441 | if options.max_matches == 0: |
| 442 | return |
| 443 | else: |
| 444 | print 'error: no sections were found that match any of %s' % (', '.join(options.section_names)) |
| 445 | |
Greg Clayton | ab20f29 | 2012-08-11 02:26:26 +0000 | [diff] [blame^] | 446 | def objc_refs(debugger, command, result, dict): |
| 447 | command_args = shlex.split(command) |
| 448 | usage = "usage: %prog [options] <EXPR> [EXPR ...]" |
| 449 | description='''Find all heap allocations given one or more objective C class names.''' |
| 450 | parser = optparse.OptionParser(description=description, prog='object_refs',usage=usage) |
| 451 | add_common_options(parser) |
| 452 | try: |
| 453 | (options, args) = parser.parse_args(command_args) |
| 454 | except: |
| 455 | return |
| 456 | |
| 457 | dylid_load_err = load_dylib() |
| 458 | if dylid_load_err: |
| 459 | print dylid_load_err |
| 460 | else: |
| 461 | if args: |
| 462 | for class_name in args: |
| 463 | addr_expr_str = "(void *)[%s class]" % class_name |
| 464 | expr_sbvalue = lldb.frame.EvaluateExpression (addr_expr_str) |
| 465 | if expr_sbvalue.error.Success(): |
| 466 | isa = expr_sbvalue.unsigned |
| 467 | if isa: |
| 468 | options.type = 'isa' |
| 469 | heap_search (options, '0x%x' % isa) |
| 470 | else: |
| 471 | print 'error: Can\'t find isa for an ObjC class named "%s"' % (class_name) |
| 472 | else: |
| 473 | print 'error: expression error for "%s": %s' % (addr_expr_str, expr_sbvalue.error) |
| 474 | else: |
| 475 | print 'error: no address expressions were specified' |
| 476 | |
Greg Clayton | 1dae6f3 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 477 | if __name__ == '__main__': |
| 478 | lldb.debugger = lldb.SBDebugger.Create() |
| 479 | |
| 480 | # This initializer is being run from LLDB in the embedded command interpreter |
| 481 | # Add any commands contained in this module to LLDB |
| 482 | lldb.debugger.HandleCommand('command script add -f lldb.macosx.heap.ptr_refs ptr_refs') |
| 483 | lldb.debugger.HandleCommand('command script add -f lldb.macosx.heap.cstr_refs cstr_refs') |
| 484 | lldb.debugger.HandleCommand('command script add -f lldb.macosx.heap.malloc_info malloc_info') |
Greg Clayton | bf47965 | 2012-05-11 02:42:36 +0000 | [diff] [blame] | 485 | lldb.debugger.HandleCommand('command script add -f lldb.macosx.heap.malloc_history malloc_history') |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 486 | lldb.debugger.HandleCommand('command script add -f lldb.macosx.heap.section_ptr_refs section_ptr_refs') |
Greg Clayton | ab20f29 | 2012-08-11 02:26:26 +0000 | [diff] [blame^] | 487 | lldb.debugger.HandleCommand('command script add -f lldb.macosx.heap.objc_refs objc_refs') |
Greg Clayton | 4c5c429 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 488 | print '"ptr_refs", "cstr_refs", "malloc_info", "malloc_history" and "section_ptr_refs" commands have been installed, use the "--help" options on these commands for detailed help.' |
Greg Clayton | e93e24f | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 489 | |
| 490 | |
| 491 | |
| 492 | |