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