Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | #---------------------------------------------------------------------- |
Greg Clayton | d712ef0 | 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 | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 7 | # |
Greg Clayton | d712ef0 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 8 | # (lldb) script import lldb.macosx.heap |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 9 | #---------------------------------------------------------------------- |
| 10 | |
| 11 | import lldb |
| 12 | import commands |
| 13 | import optparse |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 14 | import os |
Greg Clayton | d712ef0 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 15 | import os.path |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 16 | import re |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 17 | import shlex |
Greg Clayton | d712ef0 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 18 | import string |
| 19 | import tempfile |
Greg Clayton | ed3eee6 | 2012-04-25 01:49:50 +0000 | [diff] [blame] | 20 | import lldb.utils.symbolication |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 21 | |
Greg Clayton | d712ef0 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 22 | g_libheap_dylib_dir = None |
| 23 | g_libheap_dylib_dict = dict() |
| 24 | |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 25 | def get_iterate_memory_expr(process, user_init_code, user_return_code): |
| 26 | return ''' |
| 27 | typedef unsigned natural_t; |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 28 | typedef uintptr_t vm_size_t; |
| 29 | typedef uintptr_t vm_address_t; |
| 30 | typedef natural_t task_t; |
| 31 | typedef int kern_return_t; |
| 32 | #define KERN_SUCCESS 0 |
| 33 | #define MALLOC_PTR_IN_USE_RANGE_TYPE 1 |
| 34 | typedef struct vm_range_t { |
| 35 | vm_address_t address; |
| 36 | vm_size_t size; |
| 37 | } vm_range_t; |
| 38 | typedef kern_return_t (*memory_reader_t)(task_t task, vm_address_t remote_address, vm_size_t size, void **local_memory); |
| 39 | typedef void (*vm_range_recorder_t)(task_t task, void *baton, unsigned type, vm_range_t *range, unsigned size); |
| 40 | typedef void (*range_callback_t)(task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size); |
| 41 | typedef struct malloc_introspection_t { |
| 42 | kern_return_t (*enumerator)(task_t task, void *, unsigned type_mask, vm_address_t zone_address, memory_reader_t reader, vm_range_recorder_t recorder); /* enumerates all the malloc pointers in use */ |
| 43 | } malloc_introspection_t; |
| 44 | typedef struct malloc_zone_t { |
| 45 | void *reserved1[12]; |
| 46 | struct malloc_introspection_t *introspect; |
| 47 | } malloc_zone_t; |
| 48 | memory_reader_t task_peek = [](task_t task, vm_address_t remote_address, vm_size_t size, void **local_memory) -> kern_return_t { |
| 49 | *local_memory = (void*) remote_address; |
| 50 | return KERN_SUCCESS; |
| 51 | }; |
| 52 | vm_address_t *zones = 0; |
| 53 | unsigned int num_zones = 0; |
| 54 | %s |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 55 | %s |
| 56 | %s |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 57 | task_t task = 0; |
| 58 | kern_return_t err = (kern_return_t)malloc_get_all_zones (task, task_peek, &zones, &num_zones); |
| 59 | if (KERN_SUCCESS == err) |
| 60 | { |
| 61 | for (unsigned int i=0; i<num_zones; ++i) |
| 62 | { |
| 63 | const malloc_zone_t *zone = (const malloc_zone_t *)zones[i]; |
| 64 | if (zone && zone->introspect) |
| 65 | zone->introspect->enumerator (task, |
| 66 | &baton, |
| 67 | MALLOC_PTR_IN_USE_RANGE_TYPE, |
| 68 | (vm_address_t)zone, |
| 69 | task_peek, |
| 70 | [] (task_t task, void *baton, unsigned type, vm_range_t *ranges, unsigned size) -> void |
| 71 | { |
| 72 | range_callback_t callback = ((callback_baton_t *)baton)->callback; |
| 73 | for (unsigned i=0; i<size; ++i) |
| 74 | { |
| 75 | callback (task, baton, type, ranges[i].address, ranges[i].size); |
| 76 | } |
| 77 | }); |
| 78 | } |
| 79 | } |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 80 | // Call the callback for the thread stack ranges |
| 81 | for (uint32_t i=0; i<NUM_STACKS; ++i) |
| 82 | range_callback(task, &baton, 8, stacks[i].base, stacks[i].size); |
| 83 | // Call the callback for all segments |
| 84 | for (uint32_t i=0; i<NUM_SEGMENTS; ++i) |
| 85 | range_callback(task, &baton, 16, segments[i].base, segments[i].size); |
| 86 | %s''' % (get_thread_stack_ranges_struct (process), get_sections_ranges_struct(process), user_init_code, user_return_code) |
Greg Clayton | 9407302 | 2012-07-07 01:22:45 +0000 | [diff] [blame] | 87 | |
| 88 | def get_member_types_for_offset(value_type, offset, member_list): |
| 89 | member = value_type.GetFieldAtIndex(0) |
| 90 | search_bases = False |
| 91 | if member: |
| 92 | if member.GetOffsetInBytes() <= offset: |
| 93 | for field_idx in range (value_type.GetNumberOfFields()): |
| 94 | member = value_type.GetFieldAtIndex(field_idx) |
| 95 | member_byte_offset = member.GetOffsetInBytes() |
| 96 | member_end_byte_offset = member_byte_offset + member.type.size |
| 97 | if member_byte_offset <= offset and offset < member_end_byte_offset: |
| 98 | member_list.append(member) |
| 99 | get_member_types_for_offset (member.type, offset - member_byte_offset, member_list) |
| 100 | return |
| 101 | else: |
| 102 | search_bases = True |
| 103 | else: |
| 104 | search_bases = True |
| 105 | if search_bases: |
| 106 | for field_idx in range (value_type.GetNumberOfDirectBaseClasses()): |
| 107 | member = value_type.GetDirectBaseClassAtIndex(field_idx) |
| 108 | member_byte_offset = member.GetOffsetInBytes() |
| 109 | member_end_byte_offset = member_byte_offset + member.type.size |
| 110 | if member_byte_offset <= offset and offset < member_end_byte_offset: |
| 111 | member_list.append(member) |
| 112 | get_member_types_for_offset (member.type, offset - member_byte_offset, member_list) |
| 113 | return |
| 114 | for field_idx in range (value_type.GetNumberOfVirtualBaseClasses()): |
| 115 | member = value_type.GetVirtualBaseClassAtIndex(field_idx) |
| 116 | member_byte_offset = member.GetOffsetInBytes() |
| 117 | member_end_byte_offset = member_byte_offset + member.type.size |
| 118 | if member_byte_offset <= offset and offset < member_end_byte_offset: |
| 119 | member_list.append(member) |
| 120 | get_member_types_for_offset (member.type, offset - member_byte_offset, member_list) |
| 121 | return |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 122 | |
| 123 | def append_regex_callback(option, opt, value, parser): |
| 124 | try: |
| 125 | ivar_regex = re.compile(value) |
| 126 | parser.values.ivar_regex_blacklist.append(ivar_regex) |
| 127 | except: |
| 128 | print 'error: an exception was thrown when compiling the ivar regular expression for "%s"' % value |
Greg Clayton | b403a15 | 2012-04-21 00:11:26 +0000 | [diff] [blame] | 129 | |
Greg Clayton | d84bb48 | 2012-04-13 16:24:09 +0000 | [diff] [blame] | 130 | def add_common_options(parser): |
| 131 | parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 132 | 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 | d84bb48 | 2012-04-13 16:24:09 +0000 | [diff] [blame] | 133 | parser.add_option('-o', '--po', action='store_true', dest='print_object_description', help='print the object descriptions for any matches', default=False) |
Greg Clayton | a3606ad | 2012-09-12 02:02:32 +0000 | [diff] [blame] | 134 | parser.add_option('-z', '--size', action='store_true', dest='show_size', help='print the allocation size in bytes', default=False) |
| 135 | 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 | d84bb48 | 2012-04-13 16:24:09 +0000 | [diff] [blame] | 136 | parser.add_option('-m', '--memory', action='store_true', dest='memory', help='dump the memory for each matching block', default=False) |
| 137 | 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 | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 138 | 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 | b403a15 | 2012-04-21 00:11:26 +0000 | [diff] [blame] | 139 | 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 | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 140 | 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 | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 141 | parser.add_option('-F', '--max-frames', type='int', dest='max_frames', help='the maximum number of stack frames to print when using the --stack or --stack-history options (default=128)', default=128) |
| 142 | parser.add_option('-H', '--max-history', type='int', dest='max_history', help='the maximum number of stack history backtraces to print for each allocation when using the --stack-history option (default=16)', default=16) |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 143 | parser.add_option('-M', '--max-matches', type='int', dest='max_matches', help='the maximum number of matches to print', default=32) |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 144 | parser.add_option('-O', '--offset', type='int', dest='offset', help='the matching data must be at this offset', default=-1) |
Greg Clayton | ae23ed3 | 2012-10-08 22:39:38 +0000 | [diff] [blame] | 145 | parser.add_option('-V', '--vm-regions', action='store_true', dest='check_vm_regions', help='Also check the VM regions', default=False) |
Greg Clayton | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 146 | |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 147 | def type_flags_to_string(type_flags): |
| 148 | if type_flags == 0: |
| 149 | type_str = 'free' |
| 150 | elif type_flags & 2: |
| 151 | type_str = 'malloc' |
| 152 | elif type_flags & 4: |
| 153 | type_str = 'free' |
| 154 | elif type_flags & 1: |
| 155 | type_str = 'generic' |
| 156 | elif type_flags & 8: |
| 157 | type_str = 'stack' |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 158 | elif type_flags & 16: |
| 159 | type_str = 'segment' |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 160 | else: |
| 161 | type_str = hex(type_flags) |
| 162 | return type_str |
| 163 | |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 164 | def type_flags_to_description(type_flags, ptr_addr, ptr_size, offset): |
| 165 | show_offset = False |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 166 | if type_flags == 0 or type_flags & 4: |
| 167 | type_str = 'free(%#x)' % (ptr_addr,) |
| 168 | elif type_flags & 2 or type_flags & 1: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 169 | type_str = 'malloc(%6u) -> %#x' % (ptr_size, ptr_addr) |
| 170 | show_offset = True |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 171 | elif type_flags & 8: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 172 | type_str = 'stack' |
| 173 | elif type_flags & 16: |
| 174 | sb_addr = lldb.debugger.GetSelectedTarget().ResolveLoadAddress(ptr_addr + offset) |
| 175 | type_str = 'segment [%#x - %#x), %s + %u, %s' % (ptr_addr, ptr_addr + ptr_size, sb_addr.section.name, sb_addr.offset, sb_addr) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 176 | else: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 177 | type_str = '%#x' % (ptr_addr,) |
| 178 | show_offset = True |
| 179 | if show_offset and offset != 0: |
| 180 | type_str += ' + %-6u' % (offset,) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 181 | return type_str |
| 182 | |
| 183 | def dump_stack_history_entry(options, result, stack_history_entry, idx): |
Greg Clayton | 7200ed1 | 2012-05-10 23:37:52 +0000 | [diff] [blame] | 184 | address = int(stack_history_entry.address) |
| 185 | if address: |
| 186 | type_flags = int(stack_history_entry.type_flags) |
Greg Clayton | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 187 | symbolicator = lldb.utils.symbolication.Symbolicator() |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 188 | symbolicator.target = lldb.debugger.GetSelectedTarget() |
| 189 | type_str = type_flags_to_string(type_flags) |
Greg Clayton | c373ca5 | 2012-09-01 00:34:35 +0000 | [diff] [blame] | 190 | result.AppendMessage('stack[%u]: addr = 0x%x, type=%s, frames:' % (idx, address, type_str)) |
Greg Clayton | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 191 | frame_idx = 0 |
Greg Clayton | 7200ed1 | 2012-05-10 23:37:52 +0000 | [diff] [blame] | 192 | idx = 0 |
| 193 | pc = int(stack_history_entry.frames[idx]) |
Greg Clayton | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 194 | while pc != 0: |
| 195 | if pc >= 0x1000: |
| 196 | frames = symbolicator.symbolicate(pc) |
| 197 | if frames: |
| 198 | for frame in frames: |
Greg Clayton | c373ca5 | 2012-09-01 00:34:35 +0000 | [diff] [blame] | 199 | result.AppendMessage(' [%u] %s' % (frame_idx, frame)) |
Greg Clayton | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 200 | frame_idx += 1 |
| 201 | else: |
Greg Clayton | c373ca5 | 2012-09-01 00:34:35 +0000 | [diff] [blame] | 202 | result.AppendMessage(' [%u] 0x%x' % (frame_idx, pc)) |
Greg Clayton | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 203 | frame_idx += 1 |
Greg Clayton | 7200ed1 | 2012-05-10 23:37:52 +0000 | [diff] [blame] | 204 | idx = idx + 1 |
| 205 | pc = int(stack_history_entry.frames[idx]) |
Greg Clayton | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 206 | else: |
| 207 | pc = 0 |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 208 | if idx >= options.max_frames: |
| 209 | result.AppendMessage('warning: the max number of stack frames (%u) was reached, use the "--max-frames=<COUNT>" option to see more frames' % (options.max_frames)) |
| 210 | |
Greg Clayton | c373ca5 | 2012-09-01 00:34:35 +0000 | [diff] [blame] | 211 | result.AppendMessage('') |
Greg Clayton | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 212 | |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 213 | def dump_stack_history_entries(options, result, addr, history): |
Greg Clayton | d9fc535 | 2012-05-10 23:17:28 +0000 | [diff] [blame] | 214 | # malloc_stack_entry *get_stack_history_for_address (const void * addr) |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 215 | single_expr = ''' |
| 216 | typedef int kern_return_t; |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 217 | #define MAX_FRAMES %u |
| 218 | typedef struct $malloc_stack_entry { |
| 219 | uint64_t address; |
| 220 | uint64_t argument; |
| 221 | uint32_t type_flags; |
| 222 | uint32_t num_frames; |
| 223 | uint64_t frames[512]; |
| 224 | kern_return_t err; |
| 225 | } $malloc_stack_entry; |
| 226 | typedef unsigned task_t; |
| 227 | $malloc_stack_entry stack; |
| 228 | stack.address = 0x%x; |
| 229 | stack.type_flags = 2; |
| 230 | stack.num_frames = 0; |
| 231 | stack.frames[0] = 0; |
| 232 | uint32_t max_stack_frames = MAX_FRAMES; |
| 233 | stack.err = (kern_return_t)__mach_stack_logging_get_frames ( |
| 234 | (task_t)mach_task_self(), |
| 235 | stack.address, |
| 236 | &stack.frames[0], |
| 237 | max_stack_frames, |
| 238 | &stack.num_frames); |
| 239 | if (stack.num_frames < MAX_FRAMES) |
| 240 | stack.frames[stack.num_frames] = 0; |
| 241 | else |
| 242 | stack.frames[MAX_FRAMES-1] = 0; |
| 243 | stack''' % (options.max_frames, addr); |
| 244 | |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 245 | history_expr = ''' |
| 246 | typedef int kern_return_t; |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 247 | typedef unsigned task_t; |
| 248 | #define MAX_FRAMES %u |
| 249 | #define MAX_HISTORY %u |
| 250 | typedef struct mach_stack_logging_record_t { |
| 251 | uint32_t type_flags; |
| 252 | uint64_t stack_identifier; |
| 253 | uint64_t argument; |
| 254 | uint64_t address; |
| 255 | } mach_stack_logging_record_t; |
| 256 | typedef void (*enumerate_callback_t)(mach_stack_logging_record_t, void *); |
| 257 | typedef struct malloc_stack_entry { |
| 258 | uint64_t address; |
| 259 | uint64_t argument; |
| 260 | uint32_t type_flags; |
| 261 | uint32_t num_frames; |
| 262 | uint64_t frames[MAX_FRAMES]; |
| 263 | kern_return_t frames_err; |
| 264 | } malloc_stack_entry; |
| 265 | typedef struct $malloc_stack_history { |
| 266 | task_t task; |
| 267 | unsigned idx; |
| 268 | malloc_stack_entry entries[MAX_HISTORY]; |
| 269 | } $malloc_stack_history; |
| 270 | $malloc_stack_history info = { (task_t)mach_task_self(), 0 }; |
| 271 | uint32_t max_stack_frames = MAX_FRAMES; |
| 272 | enumerate_callback_t callback = [] (mach_stack_logging_record_t stack_record, void *baton) -> void { |
| 273 | $malloc_stack_history *info = ($malloc_stack_history *)baton; |
| 274 | if (info->idx < MAX_HISTORY) { |
| 275 | malloc_stack_entry *stack_entry = &(info->entries[info->idx]); |
| 276 | stack_entry->address = stack_record.address; |
| 277 | stack_entry->type_flags = stack_record.type_flags; |
| 278 | stack_entry->argument = stack_record.argument; |
| 279 | stack_entry->num_frames = 0; |
| 280 | stack_entry->frames[0] = 0; |
| 281 | stack_entry->frames_err = (kern_return_t)__mach_stack_logging_frames_for_uniqued_stack ( |
| 282 | info->task, |
| 283 | stack_record.stack_identifier, |
| 284 | stack_entry->frames, |
| 285 | (uint32_t)MAX_FRAMES, |
| 286 | &stack_entry->num_frames); |
| 287 | // Terminate the frames with zero if there is room |
| 288 | if (stack_entry->num_frames < MAX_FRAMES) |
| 289 | stack_entry->frames[stack_entry->num_frames] = 0; |
| 290 | } |
| 291 | ++info->idx; |
| 292 | }; |
| 293 | (kern_return_t)__mach_stack_logging_enumerate_records (info.task, (uint64_t)0x%x, callback, &info); |
| 294 | info''' % (options.max_frames, options.max_history, addr); |
| 295 | |
| 296 | frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() |
| 297 | if history: |
| 298 | expr = history_expr |
| 299 | else: |
| 300 | expr = single_expr |
| 301 | expr_sbvalue = frame.EvaluateExpression (expr) |
| 302 | if options.verbose: |
| 303 | print "expression:" |
| 304 | print expr |
| 305 | print "expression result:" |
| 306 | print expr_sbvalue |
| 307 | if expr_sbvalue.error.Success(): |
| 308 | if history: |
| 309 | malloc_stack_history = lldb.value(expr_sbvalue) |
| 310 | num_stacks = int(malloc_stack_history.idx) |
| 311 | if num_stacks <= options.max_history: |
| 312 | i_max = num_stacks |
| 313 | else: |
| 314 | i_max = options.max_history |
| 315 | for i in range(i_max): |
| 316 | stack_history_entry = malloc_stack_history.entries[i] |
| 317 | dump_stack_history_entry(options, result, stack_history_entry, i) |
| 318 | if num_stacks > options.max_history: |
| 319 | result.AppendMessage('warning: the max number of stacks (%u) was reached, use the "--max-history=%u" option to see all of the stacks' % (options.max_history, num_stacks)) |
| 320 | else: |
| 321 | stack_history_entry = lldb.value(expr_sbvalue) |
| 322 | dump_stack_history_entry(options, result, stack_history_entry, 0) |
| 323 | |
| 324 | else: |
| 325 | result.AppendMessage('error: expression failed "%s" => %s' % (expr, expr_sbvalue.error)) |
| 326 | |
| 327 | |
| 328 | def display_match_results (result, options, arg_str_description, expr, print_no_matches = True): |
| 329 | frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() |
| 330 | if not frame: |
| 331 | result.AppendMessage('error: invalid frame') |
| 332 | return 0 |
| 333 | expr_sbvalue = frame.EvaluateExpression (expr) |
| 334 | if options.verbose: |
| 335 | print "expression:" |
| 336 | print expr |
| 337 | print "expression result:" |
| 338 | print expr_sbvalue |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 339 | if expr_sbvalue.error.Success(): |
| 340 | if expr_sbvalue.unsigned: |
| 341 | match_value = lldb.value(expr_sbvalue) |
| 342 | i = 0 |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 343 | match_idx = 0 |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 344 | while 1: |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 345 | print_entry = True |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 346 | match_entry = match_value[i]; i += 1 |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 347 | if i > options.max_matches: |
| 348 | result.AppendMessage('warning: the max number of matches (%u) was reached, use the --max-matches option to get more results' % (options.max_matches)) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 349 | break |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 350 | malloc_addr = match_entry.addr.sbvalue.unsigned |
| 351 | if malloc_addr == 0: |
| 352 | break |
| 353 | malloc_size = int(match_entry.size) |
| 354 | offset = int(match_entry.offset) |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 355 | |
| 356 | if options.offset >= 0 and options.offset != offset: |
| 357 | print_entry = False |
| 358 | else: |
| 359 | match_addr = malloc_addr + offset |
| 360 | dynamic_value = match_entry.addr.sbvalue.GetDynamicValue(lldb.eDynamicCanRunTarget) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 361 | type_flags = int(match_entry.type) |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 362 | description = '%#16.16x: %s' % (match_addr, type_flags_to_description(type_flags, malloc_addr, malloc_size, offset)) |
Greg Clayton | a3606ad | 2012-09-12 02:02:32 +0000 | [diff] [blame] | 363 | if options.show_size: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 364 | description += ' <%5u>' % (malloc_size) |
Greg Clayton | a3606ad | 2012-09-12 02:02:32 +0000 | [diff] [blame] | 365 | if options.show_range: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 366 | description += ' [%#x - %#x)' % (malloc_addr, malloc_addr + malloc_size) |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 367 | derefed_dynamic_value = None |
| 368 | if dynamic_value.type.name == 'void *': |
| 369 | if options.type == 'pointer' and malloc_size == 4096: |
| 370 | error = lldb.SBError() |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 371 | process = expr_sbvalue.GetProcess() |
| 372 | target = expr_sbvalue.GetTarget() |
| 373 | data = bytearray(process.ReadMemory(malloc_addr, 16, error)) |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 374 | if data == '\xa1\xa1\xa1\xa1AUTORELEASE!': |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 375 | ptr_size = target.addr_size |
| 376 | thread = process.ReadUnsignedFromMemory (malloc_addr + 16 + ptr_size, ptr_size, error) |
Greg Clayton | a3606ad | 2012-09-12 02:02:32 +0000 | [diff] [blame] | 377 | # 4 bytes 0xa1a1a1a1 |
| 378 | # 12 bytes 'AUTORELEASE!' |
| 379 | # ptr bytes autorelease insertion point |
| 380 | # ptr bytes pthread_t |
| 381 | # ptr bytes next colder page |
| 382 | # ptr bytes next hotter page |
| 383 | # 4 bytes this page's depth in the list |
| 384 | # 4 bytes high-water mark |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 385 | description += ' AUTORELEASE! for pthread_t %#x' % (thread) |
| 386 | # else: |
| 387 | # description += 'malloc(%u)' % (malloc_size) |
| 388 | # else: |
| 389 | # description += 'malloc(%u)' % (malloc_size) |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 390 | else: |
| 391 | derefed_dynamic_value = dynamic_value.deref |
| 392 | if derefed_dynamic_value: |
| 393 | derefed_dynamic_type = derefed_dynamic_value.type |
| 394 | derefed_dynamic_type_size = derefed_dynamic_type.size |
| 395 | derefed_dynamic_type_name = derefed_dynamic_type.name |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 396 | description += ' ' |
Greg Clayton | a3606ad | 2012-09-12 02:02:32 +0000 | [diff] [blame] | 397 | description += derefed_dynamic_type_name |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 398 | if offset < derefed_dynamic_type_size: |
| 399 | member_list = list(); |
| 400 | get_member_types_for_offset (derefed_dynamic_type, offset, member_list) |
| 401 | if member_list: |
| 402 | member_path = '' |
| 403 | for member in member_list: |
| 404 | member_name = member.name |
| 405 | if member_name: |
| 406 | if member_path: |
| 407 | member_path += '.' |
| 408 | member_path += member_name |
| 409 | if member_path: |
| 410 | if options.ivar_regex_blacklist: |
| 411 | for ivar_regex in options.ivar_regex_blacklist: |
| 412 | if ivar_regex.match(member_path): |
| 413 | print_entry = False |
Greg Clayton | a3606ad | 2012-09-12 02:02:32 +0000 | [diff] [blame] | 414 | description += '.%s' % (member_path) |
| 415 | else: |
| 416 | description += '%u bytes after %s' % (offset - derefed_dynamic_type_size, derefed_dynamic_type_name) |
| 417 | else: |
| 418 | # strip the "*" from the end of the name since we were unable to dereference this |
| 419 | description += dynamic_value.type.name[0:-1] |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 420 | if print_entry: |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 421 | match_idx += 1 |
Greg Clayton | 7143f00 | 2012-09-04 14:09:21 +0000 | [diff] [blame] | 422 | result_output = '' |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 423 | if description: |
Greg Clayton | 7143f00 | 2012-09-04 14:09:21 +0000 | [diff] [blame] | 424 | result_output += description |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 425 | if options.print_type and derefed_dynamic_value: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 426 | result_output += ' %s' % (derefed_dynamic_value) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 427 | if options.print_object_description and dynamic_value: |
| 428 | desc = dynamic_value.GetObjectDescription() |
| 429 | if desc: |
Greg Clayton | a3606ad | 2012-09-12 02:02:32 +0000 | [diff] [blame] | 430 | result_output += '\n%s' % (desc) |
Greg Clayton | 7143f00 | 2012-09-04 14:09:21 +0000 | [diff] [blame] | 431 | if result_output: |
| 432 | result.AppendMessage(result_output) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 433 | if options.memory: |
| 434 | cmd_result = lldb.SBCommandReturnObject() |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 435 | if options.format == None: |
| 436 | memory_command = "memory read --force 0x%x 0x%x" % (malloc_addr, malloc_addr + malloc_size) |
| 437 | else: |
| 438 | memory_command = "memory read --force -f %s 0x%x 0x%x" % (options.format, malloc_addr, malloc_addr + malloc_size) |
| 439 | if options.verbose: |
| 440 | result.AppendMessage(memory_command) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 441 | lldb.debugger.GetCommandInterpreter().HandleCommand(memory_command, cmd_result) |
Greg Clayton | c373ca5 | 2012-09-01 00:34:35 +0000 | [diff] [blame] | 442 | result.AppendMessage(cmd_result.GetOutput()) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 443 | if options.stack_history: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 444 | dump_stack_history_entries(options, result, malloc_addr, 1) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 445 | elif options.stack: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 446 | dump_stack_history_entries(options, result, malloc_addr, 0) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 447 | return i |
| 448 | elif print_no_matches: |
Greg Clayton | c373ca5 | 2012-09-01 00:34:35 +0000 | [diff] [blame] | 449 | result.AppendMessage('no matches found for %s' % (arg_str_description)) |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 450 | else: |
Greg Clayton | 7143f00 | 2012-09-04 14:09:21 +0000 | [diff] [blame] | 451 | result.AppendMessage(str(expr_sbvalue.error)) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 452 | return 0 |
| 453 | |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 454 | def get_ptr_refs_options (): |
| 455 | usage = "usage: %prog [options] <EXPR> [EXPR ...]" |
| 456 | description='''Searches all allocations on the heap for pointer values on |
| 457 | darwin user space programs. Any matches that were found will dump the malloc |
| 458 | blocks that contain the pointers and might be able to print what kind of |
| 459 | objects the pointers are contained in using dynamic type information in the |
| 460 | program.''' |
| 461 | parser = optparse.OptionParser(description=description, prog='ptr_refs',usage=usage) |
| 462 | add_common_options(parser) |
| 463 | return parser |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 464 | |
Greg Clayton | 7767716 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 465 | def ptr_refs(debugger, command, result, dict): |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 466 | command_args = shlex.split(command) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 467 | parser = get_ptr_refs_options() |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 468 | try: |
| 469 | (options, args) = parser.parse_args(command_args) |
| 470 | except: |
| 471 | return |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 472 | |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 473 | process = lldb.debugger.GetSelectedTarget().GetProcess() |
| 474 | if not process: |
| 475 | result.AppendMessage('error: invalid process') |
| 476 | return |
| 477 | frame = process.GetSelectedThread().GetSelectedFrame() |
| 478 | if not frame: |
| 479 | result.AppendMessage('error: invalid frame') |
| 480 | return |
| 481 | |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 482 | options.type = 'pointer' |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 483 | if options.format == None: |
| 484 | options.format = "A" # 'A' is "address" format |
| 485 | |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 486 | if args: |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 487 | # When we initialize the expression, we must define any types that |
| 488 | # we will need when looking at every allocation. We must also define |
| 489 | # a type named callback_baton_t and make an instance named "baton" |
| 490 | # and initialize it how ever we want to. The address of "baton" will |
| 491 | # be passed into our range callback. callback_baton_t must contain |
| 492 | # a member named "callback" whose type is "range_callback_t". This |
| 493 | # will be used by our zone callbacks to call the range callback for |
| 494 | # each malloc range. |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 495 | user_init_code_format = ''' |
| 496 | #define MAX_MATCHES %u |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 497 | struct $malloc_match { |
| 498 | void *addr; |
| 499 | uintptr_t size; |
| 500 | uintptr_t offset; |
| 501 | uintptr_t type; |
| 502 | }; |
| 503 | typedef struct callback_baton_t { |
| 504 | range_callback_t callback; |
| 505 | unsigned num_matches; |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 506 | $malloc_match matches[MAX_MATCHES]; |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 507 | void *ptr; |
| 508 | } callback_baton_t; |
| 509 | range_callback_t range_callback = [](task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size) -> void { |
| 510 | callback_baton_t *info = (callback_baton_t *)baton; |
| 511 | typedef void* T; |
| 512 | const unsigned size = sizeof(T); |
| 513 | T *array = (T*)ptr_addr; |
| 514 | for (unsigned idx = 0; ((idx + 1) * sizeof(T)) <= ptr_size; ++idx) { |
| 515 | if (array[idx] == info->ptr) { |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 516 | if (info->num_matches < MAX_MATCHES) { |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 517 | info->matches[info->num_matches].addr = (void*)ptr_addr; |
| 518 | info->matches[info->num_matches].size = ptr_size; |
| 519 | info->matches[info->num_matches].offset = idx*sizeof(T); |
| 520 | info->matches[info->num_matches].type = type; |
| 521 | ++info->num_matches; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | }; |
| 526 | callback_baton_t baton = { range_callback, 0, {0}, (void *)%s }; |
| 527 | ''' |
| 528 | # We must also define a snippet of code to be run that returns |
| 529 | # the result of the expression we run. |
| 530 | # Here we return NULL if our pointer was not found in any malloc blocks, |
| 531 | # and we return the address of the matches array so we can then access |
| 532 | # the matching results |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 533 | user_return_code = '''if (baton.num_matches < MAX_MATCHES) |
| 534 | baton.matches[baton.num_matches].addr = 0; // Terminate the matches array |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 535 | baton.matches''' |
| 536 | # Iterate through all of our pointer expressions and display the results |
| 537 | for ptr_expr in args: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 538 | user_init_code = user_init_code_format % (options.max_matches, ptr_expr) |
| 539 | expr = get_iterate_memory_expr(process, user_init_code, user_return_code) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 540 | arg_str_description = 'malloc block containing pointer %s' % ptr_expr |
| 541 | display_match_results (result, options, arg_str_description, expr) |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 542 | else: |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 543 | result.AppendMessage('error: no pointer arguments were given') |
| 544 | |
| 545 | def get_cstr_refs_options(): |
| 546 | usage = "usage: %prog [options] <CSTR> [CSTR ...]" |
| 547 | description='''Searches all allocations on the heap for C string values on |
| 548 | darwin user space programs. Any matches that were found will dump the malloc |
| 549 | blocks that contain the C strings and might be able to print what kind of |
| 550 | objects the pointers are contained in using dynamic type information in the |
| 551 | program.''' |
| 552 | parser = optparse.OptionParser(description=description, prog='cstr_refs',usage=usage) |
| 553 | add_common_options(parser) |
| 554 | return parser |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 555 | |
Greg Clayton | 7767716 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 556 | def cstr_refs(debugger, command, result, dict): |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 557 | command_args = shlex.split(command) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 558 | parser = get_cstr_refs_options(); |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 559 | try: |
| 560 | (options, args) = parser.parse_args(command_args) |
| 561 | except: |
| 562 | return |
| 563 | |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 564 | process = lldb.debugger.GetSelectedTarget().GetProcess() |
| 565 | if not process: |
| 566 | result.AppendMessage('error: invalid process') |
| 567 | return |
| 568 | frame = process.GetSelectedThread().GetSelectedFrame() |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 569 | if not frame: |
| 570 | result.AppendMessage('error: invalid frame') |
| 571 | return |
| 572 | |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 573 | |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 574 | options.type = 'cstr' |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 575 | if options.format == None: |
| 576 | options.format = "Y" # 'Y' is "bytes with ASCII" format |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 577 | |
| 578 | if args: |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 579 | # When we initialize the expression, we must define any types that |
| 580 | # we will need when looking at every allocation. We must also define |
| 581 | # a type named callback_baton_t and make an instance named "baton" |
| 582 | # and initialize it how ever we want to. The address of "baton" will |
| 583 | # be passed into our range callback. callback_baton_t must contain |
| 584 | # a member named "callback" whose type is "range_callback_t". This |
| 585 | # will be used by our zone callbacks to call the range callback for |
| 586 | # each malloc range. |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 587 | user_init_code_format = ''' |
| 588 | #define MAX_MATCHES %u |
| 589 | struct $malloc_match { |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 590 | void *addr; |
| 591 | uintptr_t size; |
| 592 | uintptr_t offset; |
| 593 | uintptr_t type; |
| 594 | }; |
| 595 | typedef struct callback_baton_t { |
| 596 | range_callback_t callback; |
| 597 | unsigned num_matches; |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 598 | $malloc_match matches[MAX_MATCHES]; |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 599 | const char *cstr; |
| 600 | unsigned cstr_len; |
| 601 | } callback_baton_t; |
| 602 | range_callback_t range_callback = [](task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size) -> void { |
| 603 | callback_baton_t *info = (callback_baton_t *)baton; |
| 604 | if (info->cstr_len < ptr_size) { |
| 605 | const char *begin = (const char *)ptr_addr; |
| 606 | const char *end = begin + ptr_size - info->cstr_len; |
| 607 | for (const char *s = begin; s < end; ++s) { |
| 608 | if ((int)memcmp(s, info->cstr, info->cstr_len) == 0) { |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 609 | if (info->num_matches < MAX_MATCHES) { |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 610 | info->matches[info->num_matches].addr = (void*)ptr_addr; |
| 611 | info->matches[info->num_matches].size = ptr_size; |
| 612 | info->matches[info->num_matches].offset = s - begin; |
| 613 | info->matches[info->num_matches].type = type; |
| 614 | ++info->num_matches; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | }; |
| 620 | const char *cstr = "%s"; |
| 621 | callback_baton_t baton = { range_callback, 0, {0}, cstr, (unsigned)strlen(cstr) };''' |
| 622 | # We must also define a snippet of code to be run that returns |
| 623 | # the result of the expression we run. |
| 624 | # Here we return NULL if our pointer was not found in any malloc blocks, |
| 625 | # and we return the address of the matches array so we can then access |
| 626 | # the matching results |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 627 | user_return_code = '''if (baton.num_matches < MAX_MATCHES) |
| 628 | baton.matches[baton.num_matches].addr = 0; // Terminate the matches array |
| 629 | baton.matches''' |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 630 | # Iterate through all of our pointer expressions and display the results |
| 631 | for cstr in args: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 632 | user_init_code = user_init_code_format % (options.max_matches, cstr) |
| 633 | expr = get_iterate_memory_expr(process, user_init_code, user_return_code) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 634 | arg_str_description = 'malloc block containing "%s"' % cstr |
| 635 | display_match_results (result, options, arg_str_description, expr) |
Greg Clayton | 7fb671b | 2012-04-11 18:30:53 +0000 | [diff] [blame] | 636 | else: |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 637 | result.AppendMessage('error: command takes one or more C string arguments') |
| 638 | |
| 639 | |
| 640 | def get_malloc_info_options(): |
| 641 | usage = "usage: %prog [options] <EXPR> [EXPR ...]" |
| 642 | description='''Searches the heap a malloc block that contains the addresses |
| 643 | specified as one or more address expressions. Any matches that were found will |
| 644 | dump the malloc blocks that match or contain the specified address. The matching |
| 645 | blocks might be able to show what kind of objects they are using dynamic type |
| 646 | information in the program.''' |
| 647 | parser = optparse.OptionParser(description=description, prog='malloc_info',usage=usage) |
| 648 | add_common_options(parser) |
| 649 | return parser |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 650 | |
Greg Clayton | 7767716 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 651 | def malloc_info(debugger, command, result, dict): |
| 652 | command_args = shlex.split(command) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 653 | parser = get_malloc_info_options() |
Greg Clayton | 7767716 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 654 | try: |
| 655 | (options, args) = parser.parse_args(command_args) |
| 656 | except: |
| 657 | return |
Greg Clayton | 7767716 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 658 | options.type = 'addr' |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 659 | |
| 660 | process = lldb.debugger.GetSelectedTarget().GetProcess() |
| 661 | if not process: |
| 662 | result.AppendMessage('error: invalid process') |
| 663 | return |
| 664 | frame = process.GetSelectedThread().GetSelectedFrame() |
| 665 | if not frame: |
| 666 | result.AppendMessage('error: invalid frame') |
| 667 | return |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 668 | |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 669 | user_init_code_format = ''' |
| 670 | struct $malloc_match { |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 671 | void *addr; |
| 672 | uintptr_t size; |
| 673 | uintptr_t offset; |
| 674 | uintptr_t type; |
| 675 | }; |
| 676 | typedef struct callback_baton_t { |
| 677 | range_callback_t callback; |
| 678 | unsigned num_matches; |
| 679 | $malloc_match matches[2]; // Two items so they can be NULL terminated |
| 680 | void *ptr; |
| 681 | } callback_baton_t; |
| 682 | range_callback_t range_callback = [](task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size) -> void { |
| 683 | callback_baton_t *info = (callback_baton_t *)baton; |
| 684 | if (info->num_matches == 0) { |
| 685 | uint8_t *p = (uint8_t *)info->ptr; |
| 686 | uint8_t *lo = (uint8_t *)ptr_addr; |
| 687 | uint8_t *hi = lo + ptr_size; |
| 688 | if (lo <= p && p < hi) { |
| 689 | info->matches[info->num_matches].addr = (void*)ptr_addr; |
| 690 | info->matches[info->num_matches].size = ptr_size; |
| 691 | info->matches[info->num_matches].offset = p - lo; |
| 692 | info->matches[info->num_matches].type = type; |
| 693 | info->num_matches = 1; |
| 694 | } |
| 695 | } |
| 696 | }; |
| 697 | callback_baton_t baton = { range_callback, 0, {0}, (void *)%s }; |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 698 | baton.matches[0].addr = 0; |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 699 | baton.matches[1].addr = 0;''' |
Greg Clayton | 7767716 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 700 | if args: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 701 | for ptr_expr in args: |
| 702 | user_init_code = user_init_code_format % (ptr_expr) |
| 703 | expr = get_iterate_memory_expr(process, user_init_code, 'baton.matches') |
| 704 | arg_str_description = 'malloc block that contains %s' % ptr_expr |
| 705 | display_match_results (result, options, arg_str_description, expr) |
Greg Clayton | 7767716 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 706 | else: |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 707 | result.AppendMessage('error: command takes one or more pointer expressions') |
Greg Clayton | 7767716 | 2012-04-12 18:57:36 +0000 | [diff] [blame] | 708 | |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 709 | def get_thread_stack_ranges_struct (process): |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 710 | '''Create code that defines a structure that represents threads stack bounds |
| 711 | for all threads. It returns a static sized array initialized with all of |
| 712 | the tid, base, size structs for all the threads.''' |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 713 | stack_dicts = list() |
| 714 | if process: |
| 715 | i = 0; |
| 716 | for thread in process: |
| 717 | min_sp = thread.frame[0].sp |
| 718 | max_sp = min_sp |
| 719 | for frame in thread.frames: |
| 720 | sp = frame.sp |
| 721 | if sp < min_sp: min_sp = sp |
| 722 | if sp > max_sp: max_sp = sp |
| 723 | if min_sp < max_sp: |
| 724 | stack_dicts.append ({ 'tid' : thread.GetThreadID(), 'base' : min_sp , 'size' : max_sp-min_sp, 'index' : i }) |
| 725 | i += 1 |
| 726 | stack_dicts_len = len(stack_dicts) |
| 727 | if stack_dicts_len > 0: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 728 | result = ''' |
| 729 | #define NUM_STACKS %u |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 730 | typedef struct thread_stack_t { uint64_t tid, base, size; } thread_stack_t; |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 731 | thread_stack_t stacks[NUM_STACKS];''' % (stack_dicts_len,) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 732 | for stack_dict in stack_dicts: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 733 | result += ''' |
| 734 | stacks[%(index)u].tid = 0x%(tid)x; |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 735 | stacks[%(index)u].base = 0x%(base)x; |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 736 | stacks[%(index)u].size = 0x%(size)x;''' % stack_dict |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 737 | return result |
| 738 | else: |
| 739 | return None |
Greg Clayton | ae23ed3 | 2012-10-08 22:39:38 +0000 | [diff] [blame] | 740 | |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 741 | def get_sections_ranges_struct (process): |
| 742 | '''Create code that defines a structure that represents all segments that |
| 743 | can contain data for all images in "target". It returns a static sized |
| 744 | array initialized with all of base, size structs for all the threads.''' |
| 745 | target = process.target |
| 746 | segment_dicts = list() |
| 747 | for (module_idx, module) in enumerate(target.modules): |
| 748 | for sect_idx in range(module.GetNumSections()): |
| 749 | section = module.GetSectionAtIndex(sect_idx) |
| 750 | if not section: |
| 751 | break |
| 752 | name = section.name |
| 753 | if name != '__TEXT' and name != '__LINKEDIT' and name != '__PAGEZERO': |
| 754 | base = section.GetLoadAddress(target) |
| 755 | size = section.GetByteSize() |
| 756 | if base != lldb.LLDB_INVALID_ADDRESS and size > 0: |
| 757 | segment_dicts.append ({ 'base' : base, 'size' : size }) |
| 758 | segment_dicts_len = len(segment_dicts) |
| 759 | if segment_dicts_len > 0: |
| 760 | result = ''' |
| 761 | #define NUM_SEGMENTS %u |
| 762 | typedef struct segment_range_t { uint64_t base; uint32_t size; } segment_range_t; |
| 763 | segment_range_t segments[NUM_SEGMENTS];''' % (segment_dicts_len,) |
| 764 | for (idx, segment_dict) in enumerate(segment_dicts): |
| 765 | segment_dict['index'] = idx |
| 766 | result += ''' |
| 767 | segments[%(index)u].base = 0x%(base)x; |
| 768 | segments[%(index)u].size = 0x%(size)x;''' % segment_dict |
| 769 | return result |
Greg Clayton | ae23ed3 | 2012-10-08 22:39:38 +0000 | [diff] [blame] | 770 | else: |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 771 | return None |
Greg Clayton | ae23ed3 | 2012-10-08 22:39:38 +0000 | [diff] [blame] | 772 | |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 773 | def section_ptr_refs(debugger, command, result, dict): |
| 774 | command_args = shlex.split(command) |
| 775 | usage = "usage: %prog [options] <EXPR> [EXPR ...]" |
| 776 | description='''Searches section contents for pointer values in darwin user space programs.''' |
| 777 | parser = optparse.OptionParser(description=description, prog='section_ptr_refs',usage=usage) |
| 778 | add_common_options(parser) |
| 779 | parser.add_option('--section', action='append', type='string', dest='section_names', help='section name to search', default=list()) |
| 780 | try: |
| 781 | (options, args) = parser.parse_args(command_args) |
| 782 | except: |
| 783 | return |
| 784 | |
| 785 | options.type = 'pointer' |
Greg Clayton | ae23ed3 | 2012-10-08 22:39:38 +0000 | [diff] [blame] | 786 | |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 787 | sections = list() |
| 788 | section_modules = list() |
| 789 | if not options.section_names: |
Greg Clayton | c373ca5 | 2012-09-01 00:34:35 +0000 | [diff] [blame] | 790 | result.AppendMessage('error: at least one section must be specified with the --section option') |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 791 | return |
| 792 | |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 793 | target = lldb.debugger.GetSelectedTarget() |
| 794 | for module in target.modules: |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 795 | for section_name in options.section_names: |
| 796 | section = module.section[section_name] |
| 797 | if section: |
| 798 | sections.append (section) |
| 799 | section_modules.append (module) |
| 800 | if sections: |
| 801 | dylid_load_err = load_dylib() |
| 802 | if dylid_load_err: |
Greg Clayton | c373ca5 | 2012-09-01 00:34:35 +0000 | [diff] [blame] | 803 | result.AppendMessage(dylid_load_err) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 804 | return |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 805 | frame = target.GetProcess().GetSelectedThread().GetSelectedFrame() |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 806 | for expr_str in args: |
| 807 | for (idx, section) in enumerate(sections): |
| 808 | expr = 'find_pointer_in_memory(0x%xllu, %ullu, (void *)%s)' % (section.addr.load_addr, section.size, expr_str) |
| 809 | arg_str_description = 'section %s.%s containing "%s"' % (section_modules[idx].file.fullpath, section.name, expr_str) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 810 | num_matches = display_match_results (result, options, arg_str_description, expr, False) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 811 | if num_matches: |
| 812 | if num_matches < options.max_matches: |
| 813 | options.max_matches = options.max_matches - num_matches |
| 814 | else: |
| 815 | options.max_matches = 0 |
| 816 | if options.max_matches == 0: |
| 817 | return |
| 818 | else: |
Greg Clayton | c373ca5 | 2012-09-01 00:34:35 +0000 | [diff] [blame] | 819 | result.AppendMessage('error: no sections were found that match any of %s' % (', '.join(options.section_names))) |
Greg Clayton | e04cfd2 | 2012-07-11 22:13:18 +0000 | [diff] [blame] | 820 | |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 821 | def get_objc_refs_options(): |
| 822 | usage = "usage: %prog [options] <CLASS> [CLASS ...]" |
| 823 | description='''Searches all allocations on the heap for instances of |
| 824 | objective C classes, or any classes that inherit from the specified classes |
| 825 | in darwin user space programs. Any matches that were found will dump the malloc |
| 826 | blocks that contain the C strings and might be able to print what kind of |
| 827 | objects the pointers are contained in using dynamic type information in the |
| 828 | program.''' |
| 829 | parser = optparse.OptionParser(description=description, prog='objc_refs',usage=usage) |
| 830 | add_common_options(parser) |
| 831 | return parser |
| 832 | |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 833 | def objc_refs(debugger, command, result, dict): |
| 834 | command_args = shlex.split(command) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 835 | parser = get_objc_refs_options() |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 836 | try: |
| 837 | (options, args) = parser.parse_args(command_args) |
| 838 | except: |
| 839 | return |
| 840 | |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 841 | process = lldb.debugger.GetSelectedTarget().GetProcess() |
| 842 | if not process: |
| 843 | result.AppendMessage('error: invalid process') |
| 844 | return |
| 845 | frame = process.GetSelectedThread().GetSelectedFrame() |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 846 | if not frame: |
| 847 | result.AppendMessage('error: invalid frame') |
| 848 | return |
| 849 | |
| 850 | options.type = 'isa' |
| 851 | if options.format == None: |
| 852 | options.format = "A" # 'A' is "address" format |
| 853 | |
| 854 | num_objc_classes_value = frame.EvaluateExpression("(int)objc_getClassList((void *)0, (int)0)") |
| 855 | if not num_objc_classes_value.error.Success(): |
| 856 | result.AppendMessage('error: %s' % num_objc_classes_value.error.GetCString()) |
| 857 | return |
| 858 | |
| 859 | num_objc_classes = num_objc_classes_value.GetValueAsUnsigned() |
| 860 | if num_objc_classes == 0: |
| 861 | result.AppendMessage('error: no objective C classes in program') |
| 862 | return |
| 863 | |
| 864 | if args: |
| 865 | # When we initialize the expression, we must define any types that |
| 866 | # we will need when looking at every allocation. We must also define |
| 867 | # a type named callback_baton_t and make an instance named "baton" |
| 868 | # and initialize it how ever we want to. The address of "baton" will |
| 869 | # be passed into our range callback. callback_baton_t must contain |
| 870 | # a member named "callback" whose type is "range_callback_t". This |
| 871 | # will be used by our zone callbacks to call the range callback for |
| 872 | # each malloc range. |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 873 | user_init_code_format = '''#define MAX_MATCHES %u |
| 874 | struct $malloc_match { |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 875 | void *addr; |
| 876 | uintptr_t size; |
| 877 | uintptr_t offset; |
| 878 | uintptr_t type; |
| 879 | }; |
| 880 | typedef int (*compare_callback_t)(const void *a, const void *b); |
| 881 | typedef struct callback_baton_t { |
| 882 | range_callback_t callback; |
| 883 | compare_callback_t compare_callback; |
| 884 | unsigned num_matches; |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 885 | $malloc_match matches[MAX_MATCHES]; |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 886 | void *isa; |
| 887 | Class classes[%u]; |
| 888 | } callback_baton_t; |
| 889 | compare_callback_t compare_callback = [](const void *a, const void *b) -> int { |
| 890 | Class a_ptr = *(Class *)a; |
| 891 | Class b_ptr = *(Class *)b; |
| 892 | if (a_ptr < b_ptr) return -1; |
| 893 | if (a_ptr > b_ptr) return +1; |
| 894 | return 0; |
| 895 | }; |
| 896 | range_callback_t range_callback = [](task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size) -> void { |
| 897 | callback_baton_t *info = (callback_baton_t *)baton; |
| 898 | if (sizeof(Class) <= ptr_size) { |
| 899 | Class *curr_class_ptr = (Class *)ptr_addr; |
| 900 | Class *matching_class_ptr = (Class *)bsearch (curr_class_ptr, |
| 901 | (const void *)info->classes, |
| 902 | sizeof(info->classes)/sizeof(Class), |
| 903 | sizeof(Class), |
| 904 | info->compare_callback); |
| 905 | if (matching_class_ptr) { |
| 906 | bool match = false; |
| 907 | if (info->isa) { |
| 908 | Class isa = *curr_class_ptr; |
| 909 | if (info->isa == isa) |
| 910 | match = true; |
| 911 | else { // if (info->objc.match_superclasses) { |
| 912 | Class super = (Class)class_getSuperclass(isa); |
| 913 | while (super) { |
| 914 | if (super == info->isa) { |
| 915 | match = true; |
| 916 | break; |
| 917 | } |
| 918 | super = (Class)class_getSuperclass(super); |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | else |
| 923 | match = true; |
| 924 | if (match) { |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 925 | if (info->num_matches < MAX_MATCHES) { |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 926 | info->matches[info->num_matches].addr = (void*)ptr_addr; |
| 927 | info->matches[info->num_matches].size = ptr_size; |
| 928 | info->matches[info->num_matches].offset = 0; |
| 929 | info->matches[info->num_matches].type = type; |
| 930 | ++info->num_matches; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | }; |
| 936 | callback_baton_t baton = { range_callback, compare_callback, 0, {0}, (void *)0x%x, {0} }; |
| 937 | int nc = (int)objc_getClassList(baton.classes, sizeof(baton.classes)/sizeof(Class)); |
| 938 | (void)qsort (baton.classes, sizeof(baton.classes)/sizeof(Class), sizeof(Class), compare_callback);''' |
| 939 | # We must also define a snippet of code to be run that returns |
| 940 | # the result of the expression we run. |
| 941 | # Here we return NULL if our pointer was not found in any malloc blocks, |
| 942 | # and we return the address of the matches array so we can then access |
| 943 | # the matching results |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 944 | user_return_code = '''if (baton.num_matches < MAX_MATCHES) |
| 945 | baton.matches[baton.num_matches].addr = 0; // Terminate the matches array |
| 946 | baton.matches''' |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 947 | # Iterate through all of our ObjC class name arguments |
| 948 | for class_name in args: |
| 949 | addr_expr_str = "(void *)[%s class]" % class_name |
| 950 | expr_sbvalue = frame.EvaluateExpression (addr_expr_str) |
| 951 | if expr_sbvalue.error.Success(): |
| 952 | isa = expr_sbvalue.unsigned |
| 953 | if isa: |
| 954 | options.type = 'isa' |
Greg Clayton | e7fc9cc | 2013-01-31 06:38:09 +0000 | [diff] [blame^] | 955 | result.AppendMessage('Searching for all instances of classes or subclasses of "%s" (isa=0x%x)' % (class_name, isa)) |
| 956 | user_init_code = user_init_code_format % (options.max_matches, num_objc_classes, isa) |
| 957 | expr = get_iterate_memory_expr(process, user_init_code, user_return_code) |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 958 | arg_str_description = 'objective C classes with isa 0x%x' % isa |
| 959 | display_match_results (result, options, arg_str_description, expr) |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 960 | else: |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 961 | result.AppendMessage('error: Can\'t find isa for an ObjC class named "%s"' % (class_name)) |
| 962 | else: |
| 963 | result.AppendMessage('error: expression error for "%s": %s' % (addr_expr_str, expr_sbvalue.error)) |
| 964 | else: |
| 965 | result.AppendMessage('error: command takes one or more C string arguments'); |
Greg Clayton | 774ebdf | 2012-08-11 02:26:26 +0000 | [diff] [blame] | 966 | |
Greg Clayton | d712ef0 | 2012-04-25 18:40:20 +0000 | [diff] [blame] | 967 | if __name__ == '__main__': |
| 968 | lldb.debugger = lldb.SBDebugger.Create() |
| 969 | |
| 970 | # This initializer is being run from LLDB in the embedded command interpreter |
| 971 | # Add any commands contained in this module to LLDB |
Greg Clayton | 85e62ec | 2013-01-30 22:57:34 +0000 | [diff] [blame] | 972 | if __package__: |
| 973 | package_name = __package__ + '.' + __name__ |
| 974 | else: |
| 975 | package_name = __name__ |
| 976 | |
| 977 | # Make the options so we can generate the help text for the new LLDB |
| 978 | # command line command prior to registering it with LLDB below. This way |
| 979 | # if clients in LLDB type "help malloc_info", they will see the exact same |
| 980 | # output as typing "malloc_info --help". |
| 981 | ptr_refs.__doc__ = get_ptr_refs_options().format_help() |
| 982 | cstr_refs.__doc__ = get_cstr_refs_options().format_help() |
| 983 | malloc_info.__doc__ = get_malloc_info_options().format_help() |
| 984 | objc_refs.__doc__ = get_objc_refs_options().format_help() |
| 985 | lldb.debugger.HandleCommand('command script add -f %s.ptr_refs ptr_refs' % package_name) |
| 986 | lldb.debugger.HandleCommand('command script add -f %s.cstr_refs cstr_refs' % package_name) |
| 987 | lldb.debugger.HandleCommand('command script add -f %s.malloc_info malloc_info' % package_name) |
| 988 | # lldb.debugger.HandleCommand('command script add -f %s.heap heap' % package_name) |
| 989 | # lldb.debugger.HandleCommand('command script add -f %s.section_ptr_refs section_ptr_refs' % package_name) |
| 990 | # lldb.debugger.HandleCommand('command script add -f %s.stack_ptr_refs stack_ptr_refs' % package_name) |
| 991 | lldb.debugger.HandleCommand('command script add -f %s.objc_refs objc_refs' % package_name) |
| 992 | print '"malloc_info", "ptr_refs", "cstr_refs", and "objc_refs" commands have been installed, use the "--help" options on these commands for detailed help.' |
Greg Clayton | 804de01 | 2012-04-11 16:27:06 +0000 | [diff] [blame] | 993 | |
| 994 | |
| 995 | |
| 996 | |