diff options
Diffstat (limited to 'examples')
96 files changed, 20652 insertions, 0 deletions
diff --git a/examples/customization/bin-utils/.lldbinit b/examples/customization/bin-utils/.lldbinit new file mode 100644 index 000000000000..5a2f6feb94df --- /dev/null +++ b/examples/customization/bin-utils/.lldbinit @@ -0,0 +1,5 @@ +# So that ~/binutils.py takes precedence. +script sys.path[:0] = [os.path.expanduser('~')] +script import binutils +command script add -f binutils.itob itob +command script add -f binutils.utob utob diff --git a/examples/customization/bin-utils/README b/examples/customization/bin-utils/README new file mode 100644 index 000000000000..1352d93b2787 --- /dev/null +++ b/examples/customization/bin-utils/README @@ -0,0 +1,36 @@ +Files in this directory: + +o .lldbinit: + +An example lldb init file that imports the binutils.py module and adds the +following commands: 'itob' and 'utob'. + +o binutils.py: + +Python module which provides implementation for the 'itob' and 'utob' commands. + +o README: + +The file you are reading now. + +================================================================================ +The following terminal output shows an interaction with lldb using the .lldbinit +and the binutils.py files which are located in my HOME directory. The lldb init +file imports the utils Python module and adds the 'itob' and 'utob' commands. + +$ /Volumes/data/lldb/svn/trunk/build/Debug/lldb +(lldb) help itob +Convert the integer to print its two's complement representation. + args[0] (mandatory) is the integer to be converted + args[1] (mandatory) is the bit width of the two's complement representation + args[2] (optional) if specified, turns on verbose printing +Syntax: itob +(lldb) itob -5 4 + [1, 0, 1, 1] +(lldb) itob -5 32 v + 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1] +(lldb) utob 0xABCD 32 v + 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1] +(lldb) diff --git a/examples/customization/bin-utils/binutils.py b/examples/customization/bin-utils/binutils.py new file mode 100644 index 000000000000..313a354ec3a5 --- /dev/null +++ b/examples/customization/bin-utils/binutils.py @@ -0,0 +1,122 @@ +"Collection of tools for displaying bit representation of numbers.""" + +import StringIO + +def binary(n, width=None): + """ + Return a list of (0|1)'s for the binary representation of n where n >= 0. + If you specify a width, it must be > 0, otherwise it is ignored. The list + could be padded with 0 bits if width is specified. + """ + l = [] + if width and width <= 0: + width = None + while n > 0: + l.append(1 if n&1 else 0) + n = n >> 1 + + if width: + for i in range(width - len(l)): + l.append(0) + + l.reverse() + return l + +def twos_complement(n, width): + """ + Return a list of (0|1)'s for the binary representation of a width-bit two's + complement numeral system of an integer n which may be negative. + """ + val = 2**(width-1) + if n >= 0: + if n > (val-1): + return None + # It is safe to represent n with width-bits. + return binary(n, width) + + if n < 0: + if abs(n) > val: + return None + # It is safe to represent n (a negative int) with width-bits. + return binary(val*2 - abs(n)) + +# print binary(0xABCD) +# [1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1] +# print binary(0x1F, 8) +# [0, 0, 0, 1, 1, 1, 1, 1] +# print twos_complement(-5, 4) +# [1, 0, 1, 1] +# print twos_complement(7, 4) +# [0, 1, 1, 1] +# print binary(7) +# [1, 1, 1] +# print twos_complement(-5, 64) +# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1] + +def positions(width): + """Helper function returning a list describing the bit positions. + Bit positions greater than 99 are truncated to 2 digits, for example, + 100 -> 00 and 127 -> 27.""" + return ['{0:2}'.format(i)[-2:] for i in reversed(range(width))] + + +def utob(debugger, command_line, result, dict): + """Convert the unsigned integer to print its binary representation. + args[0] (mandatory) is the unsigned integer to be converted + args[1] (optional) is the bit width of the binary representation + args[2] (optional) if specified, turns on verbose printing""" + args = command_line.split() + try: + n = int(args[0], 0) + width = None + if len(args) > 1: + width = int(args[1], 0) + if width < 0: + width = 0 + except: + print utob.__doc__ + return + + if len(args) > 2: + verbose = True + else: + verbose = False + + bits = binary(n, width) + if not bits: + print "insufficient width value: %d" % width + return + if verbose and width > 0: + pos = positions(width) + print ' '+' '.join(pos) + print ' %s' % str(bits) + +def itob(debugger, command_line, result, dict): + """Convert the integer to print its two's complement representation. + args[0] (mandatory) is the integer to be converted + args[1] (mandatory) is the bit width of the two's complement representation + args[2] (optional) if specified, turns on verbose printing""" + args = command_line.split() + try: + n = int(args[0], 0) + width = int(args[1], 0) + if width < 0: + width = 0 + except: + print itob.__doc__ + return + + if len(args) > 2: + verbose = True + else: + verbose = False + + bits = twos_complement(n, width) + if not bits: + print "insufficient width value: %d" % width + return + if verbose and width > 0: + pos = positions(width) + print ' '+' '.join(pos) + print ' %s' % str(bits) + diff --git a/examples/customization/import-python/README b/examples/customization/import-python/README new file mode 100644 index 000000000000..9122f8f46dcf --- /dev/null +++ b/examples/customization/import-python/README @@ -0,0 +1,40 @@ +Files in this directory: + +o importcmd.py: + +Python module which provides implementation for the 'import' command. + +o README: + +The file you are reading now. + +================================================================================ +The import command defined by importcmd.py can be used in LLDB to load a Python +module given its full pathname. +The command works by extending Python's sys.path lookup to include the path to +the module to be imported when required, and then going through the language +ordinary 'import' mechanism. In this respect, modules imported from LLDB command +line should not be distinguishable from those imported using the script interpreter. +The following terminal output shows an interaction with lldb using this new command. + +Enrico-Granatas-MacBook-Pro:Debug enricogranata$ ./lldb +(lldb) script import importcmd +(lldb) command script add import -f importcmd.pyimport_cmd +(lldb) import ../demo.py +(lldb) script demo.test_function('hello world') +I am a Python function that says hello world +(lldb) quit +Enrico-Granatas-MacBook-Pro:Debug enricogranata$ + +Of course, the commands to import the importcmd.py module and to define the import +command, can be included in the .lldbinit file to make this feature available at +debugger startup + +WARNING: The import command defined by importcmd.py is now obsolete +In TOT LLDB, you can say: +(lldb) command script import ../demo.py +(lldb) script demo.test_function('hello world') +I am a Python function that says hello world +(lldb) quit + +using the native "command script import" command, which offers a superset of what the import command provided by importcmd.py does diff --git a/examples/customization/import-python/importcmd.py b/examples/customization/import-python/importcmd.py new file mode 100644 index 000000000000..576a642d5a01 --- /dev/null +++ b/examples/customization/import-python/importcmd.py @@ -0,0 +1,31 @@ +import sys,os,lldb +def check_has_dir_in_path(dirname): + return sys.path.__contains__(dirname); + +def ensure_has_dir_in_path(dirname): + dirname = os.path.abspath(dirname) + if not (check_has_dir_in_path(dirname)): + sys.path.append(dirname); + +def do_import(debugger,modname): + if (len(modname) > 4 and modname[-4:] == '.pyc'): + modname = modname[:-4] + if (len(modname) > 3 and modname[-3:] == '.py'): + modname = modname[:-3] + debugger.HandleCommand("script import " + modname) + +def pyimport_cmd(debugger, args, result, dict): + """Import a Python module given its full path""" + print 'WARNING: obsolete feature - use native command "command script import"' + if args == "": + return "no module path given"; + if not (os.sep in args): + modname = args + ensure_has_dir_in_path('.') + else: + endofdir = args.rfind(os.sep) + modname = args[endofdir+1:] + args = args[0:endofdir] + ensure_has_dir_in_path(args) + do_import(debugger,modname) + return None diff --git a/examples/customization/pwd-cd-and-system/.lldbinit b/examples/customization/pwd-cd-and-system/.lldbinit new file mode 100644 index 000000000000..f477b5797bec --- /dev/null +++ b/examples/customization/pwd-cd-and-system/.lldbinit @@ -0,0 +1,7 @@ +script import os, sys +# So that ~/utils.py takes precedence. +script sys.path[:0] = [os.path.expanduser('~')] +script import utils +command alias pwd script print os.getcwd() +command script add -f utils.chdir cd +command script add -f utils.system system diff --git a/examples/customization/pwd-cd-and-system/README b/examples/customization/pwd-cd-and-system/README new file mode 100644 index 000000000000..1b67d0b09c01 --- /dev/null +++ b/examples/customization/pwd-cd-and-system/README @@ -0,0 +1,41 @@ +Files in this directory: + +o .lldbinit: + +An example lldb init file that imports the utils.py module and adds the +following commands: 'pwd', 'cd', and 'system'. + +o utils.py: + +Python module which provides implementation for the 'cd' and 'system' commands. + +o README: + +The file you are reading now. + +================================================================================ +The following terminal output shows an interaction with lldb using the .lldbinit +and the utils.py files which are located in my HOME directory. The lldb init +file imports the utils Python module and adds the 'pwd', 'cd', and 'system' +commands. + +Johnnys-MacBook-Pro:multiple_threads johnny$ pwd +/Volumes/data/lldb/svn/trunk/test/functionalities/watchpoint/multiple_threads +Johnnys-MacBook-Pro:multiple_threads johnny$ lldb +(lldb) pwd +/Volumes/data/lldb/svn/trunk/test/functionalities/watchpoint/multiple_threads +(lldb) cd .. +Current working directory: /Volumes/data/lldb/svn/trunk/test/functionalities/watchpoint +(lldb) help system + +Execute the command (a string) in a subshell. +Syntax: system +(lldb) system ls -l +total 0 +drwxr-xr-x 7 johnny admin 238 Oct 11 17:24 hello_watchlocation +drwxr-xr-x 7 johnny admin 238 Oct 11 17:24 hello_watchpoint +drwxr-xr-x 7 johnny admin 238 Oct 11 17:24 multiple_threads +drwxr-xr-x 7 johnny admin 238 Oct 11 17:24 watchpoint_commands + +retcode: 0 +(lldb) diff --git a/examples/customization/pwd-cd-and-system/utils.py b/examples/customization/pwd-cd-and-system/utils.py new file mode 100644 index 000000000000..e975e8869773 --- /dev/null +++ b/examples/customization/pwd-cd-and-system/utils.py @@ -0,0 +1,49 @@ +"""Utility for changing directories and execution of commands in a subshell.""" + +import os, shlex, subprocess + +# Store the previous working directory for the 'cd -' command. +class Holder: + """Holds the _prev_dir_ class attribute for chdir() function.""" + _prev_dir_ = None + + @classmethod + def prev_dir(cls): + return cls._prev_dir_ + + @classmethod + def swap(cls, dir): + cls._prev_dir_ = dir + +def chdir(debugger, args, result, dict): + """Change the working directory, or cd to ${HOME}. + You can also issue 'cd -' to change to the previous working directory.""" + new_dir = args.strip() + if not new_dir: + new_dir = os.path.expanduser('~') + elif new_dir == '-': + if not Holder.prev_dir(): + # Bad directory, not changing. + print "bad directory, not changing" + return + else: + new_dir = Holder.prev_dir() + + Holder.swap(os.getcwd()) + os.chdir(new_dir) + print "Current working directory: %s" % os.getcwd() + +def system(debugger, command_line, result, dict): + """Execute the command (a string) in a subshell.""" + args = shlex.split(command_line) + process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output, error = process.communicate() + retcode = process.poll() + if output and error: + print "stdout=>\n", output + print "stderr=>\n", error + elif output: + print output + elif error: + print error + print "retcode:", retcode diff --git a/examples/darwin/heap_find/heap.py b/examples/darwin/heap_find/heap.py new file mode 100644 index 000000000000..fb3394323890 --- /dev/null +++ b/examples/darwin/heap_find/heap.py @@ -0,0 +1,1244 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# This module is designed to live inside the "lldb" python package +# in the "lldb.macosx" package. To use this in the embedded python +# interpreter using "lldb" just import it: +# +# (lldb) script import lldb.macosx.heap +#---------------------------------------------------------------------- + +import lldb +import commands +import optparse +import os +import os.path +import re +import shlex +import string +import sys +import tempfile +import lldb.utils.symbolication + +g_libheap_dylib_dir = None +g_libheap_dylib_dict = dict() + +def get_iterate_memory_expr(options, process, user_init_code, user_return_code): + expr = ''' +typedef unsigned natural_t; +typedef uintptr_t vm_size_t; +typedef uintptr_t vm_address_t; +typedef natural_t task_t; +typedef int kern_return_t; +#define KERN_SUCCESS 0 +typedef void (*range_callback_t)(task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size); +'''; + if options.search_vm_regions: + expr += ''' +typedef int vm_prot_t; +typedef unsigned int vm_inherit_t; +typedef unsigned long long memory_object_offset_t; +typedef unsigned int boolean_t; +typedef int vm_behavior_t; +typedef uint32_t vm32_object_id_t; +typedef natural_t mach_msg_type_number_t; +typedef uint64_t mach_vm_address_t; +typedef uint64_t mach_vm_offset_t; +typedef uint64_t mach_vm_size_t; +typedef uint64_t vm_map_offset_t; +typedef uint64_t vm_map_address_t; +typedef uint64_t vm_map_size_t; +#define VM_PROT_NONE ((vm_prot_t) 0x00) +#define VM_PROT_READ ((vm_prot_t) 0x01) +#define VM_PROT_WRITE ((vm_prot_t) 0x02) +#define VM_PROT_EXECUTE ((vm_prot_t) 0x04) +typedef struct vm_region_submap_short_info_data_64_t { + vm_prot_t protection; + vm_prot_t max_protection; + vm_inherit_t inheritance; + memory_object_offset_t offset; // offset into object/map + unsigned int user_tag; // user tag on map entry + unsigned int ref_count; // obj/map mappers, etc + unsigned short shadow_depth; // only for obj + unsigned char external_pager; // only for obj + unsigned char share_mode; // see enumeration + boolean_t is_submap; // submap vs obj + vm_behavior_t behavior; // access behavior hint + vm32_object_id_t object_id; // obj/map name, not a handle + unsigned short user_wired_count; +} vm_region_submap_short_info_data_64_t; +#define VM_REGION_SUBMAP_SHORT_INFO_COUNT_64 ((mach_msg_type_number_t)(sizeof(vm_region_submap_short_info_data_64_t)/sizeof(int)))'''; + if user_init_code: + expr += user_init_code; + expr += ''' +task_t task = (task_t)mach_task_self(); +mach_vm_address_t vm_region_base_addr; +mach_vm_size_t vm_region_size; +natural_t vm_region_depth; +vm_region_submap_short_info_data_64_t vm_region_info; +kern_return_t err; +for (vm_region_base_addr = 0, vm_region_size = 1; vm_region_size != 0; vm_region_base_addr += vm_region_size) +{ + mach_msg_type_number_t vm_region_info_size = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; + err = (kern_return_t)mach_vm_region_recurse (task, + &vm_region_base_addr, + &vm_region_size, + &vm_region_depth, + &vm_region_info, + &vm_region_info_size); + if (err) + break; + // Check all read + write regions. This will cover the thread stacks + // and any regions of memory like __DATA segments, that might contain + // data we are looking for + if (vm_region_info.protection & VM_PROT_WRITE && + vm_region_info.protection & VM_PROT_READ) + { + baton.callback (task, + &baton, + 64, + vm_region_base_addr, + vm_region_size); + } +}''' + else: + if options.search_stack: + expr += get_thread_stack_ranges_struct (process) + if options.search_segments: + expr += get_sections_ranges_struct (process) + if user_init_code: + expr += user_init_code + if options.search_heap: + expr += ''' +#define MALLOC_PTR_IN_USE_RANGE_TYPE 1 +typedef struct vm_range_t { + vm_address_t address; + vm_size_t size; +} vm_range_t; +typedef kern_return_t (*memory_reader_t)(task_t task, vm_address_t remote_address, vm_size_t size, void **local_memory); +typedef void (*vm_range_recorder_t)(task_t task, void *baton, unsigned type, vm_range_t *range, unsigned size); +typedef struct malloc_introspection_t { + 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 */ +} malloc_introspection_t; +typedef struct malloc_zone_t { + void *reserved1[12]; + struct malloc_introspection_t *introspect; +} malloc_zone_t; +memory_reader_t task_peek = [](task_t task, vm_address_t remote_address, vm_size_t size, void **local_memory) -> kern_return_t { + *local_memory = (void*) remote_address; + return KERN_SUCCESS; +}; +vm_address_t *zones = 0; +unsigned int num_zones = 0;task_t task = 0; +kern_return_t err = (kern_return_t)malloc_get_all_zones (task, task_peek, &zones, &num_zones); +if (KERN_SUCCESS == err) +{ + for (unsigned int i=0; i<num_zones; ++i) + { + const malloc_zone_t *zone = (const malloc_zone_t *)zones[i]; + if (zone && zone->introspect) + zone->introspect->enumerator (task, + &baton, + MALLOC_PTR_IN_USE_RANGE_TYPE, + (vm_address_t)zone, + task_peek, + [] (task_t task, void *baton, unsigned type, vm_range_t *ranges, unsigned size) -> void + { + range_callback_t callback = ((callback_baton_t *)baton)->callback; + for (unsigned i=0; i<size; ++i) + { + callback (task, baton, type, ranges[i].address, ranges[i].size); + } + }); + } +}''' + + if options.search_stack: + expr += ''' +#ifdef NUM_STACKS +// Call the callback for the thread stack ranges +for (uint32_t i=0; i<NUM_STACKS; ++i) { + range_callback(task, &baton, 8, stacks[i].base, stacks[i].size); + if (STACK_RED_ZONE_SIZE > 0) { + range_callback(task, &baton, 16, stacks[i].base - STACK_RED_ZONE_SIZE, STACK_RED_ZONE_SIZE); + } +} +#endif''' + + if options.search_segments: + expr += ''' +#ifdef NUM_SEGMENTS +// Call the callback for all segments +for (uint32_t i=0; i<NUM_SEGMENTS; ++i) + range_callback(task, &baton, 32, segments[i].base, segments[i].size); +#endif''' + + if user_return_code: + expr += "\n%s" % (user_return_code,) + + return expr + +def get_member_types_for_offset(value_type, offset, member_list): + member = value_type.GetFieldAtIndex(0) + search_bases = False + if member: + if member.GetOffsetInBytes() <= offset: + for field_idx in range (value_type.GetNumberOfFields()): + member = value_type.GetFieldAtIndex(field_idx) + member_byte_offset = member.GetOffsetInBytes() + member_end_byte_offset = member_byte_offset + member.type.size + if member_byte_offset <= offset and offset < member_end_byte_offset: + member_list.append(member) + get_member_types_for_offset (member.type, offset - member_byte_offset, member_list) + return + else: + search_bases = True + else: + search_bases = True + if search_bases: + for field_idx in range (value_type.GetNumberOfDirectBaseClasses()): + member = value_type.GetDirectBaseClassAtIndex(field_idx) + member_byte_offset = member.GetOffsetInBytes() + member_end_byte_offset = member_byte_offset + member.type.size + if member_byte_offset <= offset and offset < member_end_byte_offset: + member_list.append(member) + get_member_types_for_offset (member.type, offset - member_byte_offset, member_list) + return + for field_idx in range (value_type.GetNumberOfVirtualBaseClasses()): + member = value_type.GetVirtualBaseClassAtIndex(field_idx) + member_byte_offset = member.GetOffsetInBytes() + member_end_byte_offset = member_byte_offset + member.type.size + if member_byte_offset <= offset and offset < member_end_byte_offset: + member_list.append(member) + get_member_types_for_offset (member.type, offset - member_byte_offset, member_list) + return + +def append_regex_callback(option, opt, value, parser): + try: + ivar_regex = re.compile(value) + parser.values.ivar_regex_blacklist.append(ivar_regex) + except: + print 'error: an exception was thrown when compiling the ivar regular expression for "%s"' % value + +def add_common_options(parser): + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + parser.add_option('-t', '--type', action='store_true', dest='print_type', help='print the full value of the type for each matching malloc block', default=False) + parser.add_option('-o', '--po', action='store_true', dest='print_object_description', help='print the object descriptions for any matches', default=False) + parser.add_option('-z', '--size', action='store_true', dest='show_size', help='print the allocation size in bytes', default=False) + 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) + parser.add_option('-m', '--memory', action='store_true', dest='memory', help='dump the memory for each matching block', default=False) + parser.add_option('-f', '--format', type='string', dest='format', help='the format to use when dumping memory if --memory is specified', default=None) + 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') + 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) + 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) + 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) + 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) + parser.add_option('-M', '--max-matches', type='int', dest='max_matches', help='the maximum number of matches to print', default=32) + parser.add_option('-O', '--offset', type='int', dest='offset', help='the matching data must be at this offset', default=-1) + parser.add_option('--ignore-stack', action='store_false', dest='search_stack', help="Don't search the stack when enumerating memory", default=True) + parser.add_option('--ignore-heap', action='store_false', dest='search_heap', help="Don't search the heap allocations when enumerating memory", default=True) + parser.add_option('--ignore-segments', action='store_false', dest='search_segments', help="Don't search readable executable segments enumerating memory", default=True) + parser.add_option('-V', '--vm-regions', action='store_true', dest='search_vm_regions', help='Check all VM regions instead of searching the heap, stack and segments', default=False) + +def type_flags_to_string(type_flags): + if type_flags == 0: + type_str = 'free' + elif type_flags & 2: + type_str = 'malloc' + elif type_flags & 4: + type_str = 'free' + elif type_flags & 1: + type_str = 'generic' + elif type_flags & 8: + type_str = 'stack' + elif type_flags & 16: + type_str = 'stack (red zone)' + elif type_flags & 32: + type_str = 'segment' + elif type_flags & 64: + type_str = 'vm_region' + else: + type_str = hex(type_flags) + return type_str + +def find_variable_containing_address(verbose, frame, match_addr): + variables = frame.GetVariables(True,True,True,True) + matching_var = None + for var in variables: + var_addr = var.GetLoadAddress() + if var_addr != lldb.LLDB_INVALID_ADDRESS: + byte_size = var.GetType().GetByteSize() + if verbose: + print 'frame #%u: [%#x - %#x) %s' % (frame.GetFrameID(), var.load_addr, var.load_addr + byte_size, var.name) + if var_addr == match_addr: + if verbose: + print 'match' + return var + else: + if byte_size > 0 and var_addr <= match_addr and match_addr < (var_addr + byte_size): + if verbose: + print 'match' + return var + return None + +def find_frame_for_stack_address(process, addr): + closest_delta = sys.maxint + closest_frame = None + #print 'find_frame_for_stack_address(%#x)' % (addr) + for thread in process: + prev_sp = lldb.LLDB_INVALID_ADDRESS + for frame in thread: + cfa = frame.GetCFA() + #print 'frame #%u: cfa = %#x' % (frame.GetFrameID(), cfa) + if addr < cfa: + delta = cfa - addr + #print '%#x < %#x, delta = %i' % (addr, cfa, delta) + if delta < closest_delta: + #print 'closest' + closest_delta = delta + closest_frame = frame + # else: + # print 'delta >= closest_delta' + return closest_frame + +def type_flags_to_description(process, type_flags, ptr_addr, ptr_size, offset, match_addr): + show_offset = False + if type_flags == 0 or type_flags & 4: + type_str = 'free(%#x)' % (ptr_addr,) + elif type_flags & 2 or type_flags & 1: + type_str = 'malloc(%6u) -> %#x' % (ptr_size, ptr_addr) + show_offset = True + elif type_flags & 8: + type_str = 'stack' + frame = find_frame_for_stack_address(process, match_addr) + if frame: + type_str += ' in frame #%u of thread #%u: tid %#x' % (frame.GetFrameID(), frame.GetThread().GetIndexID(), frame.GetThread().GetThreadID()) + variables = frame.GetVariables(True,True,True,True) + matching_var = None + for var in variables: + var_addr = var.GetLoadAddress() + if var_addr != lldb.LLDB_INVALID_ADDRESS: + #print 'variable "%s" @ %#x (%#x)' % (var.name, var.load_addr, match_addr) + if var_addr == match_addr: + matching_var = var + break + else: + byte_size = var.GetType().GetByteSize() + if byte_size > 0 and var_addr <= match_addr and match_addr < (var_addr + byte_size): + matching_var = var + break + if matching_var: + type_str += ' in variable at %#x:\n %s' % (matching_var.GetLoadAddress(), matching_var) + elif type_flags & 16: + type_str = 'stack (red zone)' + elif type_flags & 32: + sb_addr = process.GetTarget().ResolveLoadAddress(ptr_addr + offset) + type_str = 'segment [%#x - %#x), %s + %u, %s' % (ptr_addr, ptr_addr + ptr_size, sb_addr.section.name, sb_addr.offset, sb_addr) + elif type_flags & 64: + sb_addr = process.GetTarget().ResolveLoadAddress(ptr_addr + offset) + type_str = 'vm_region [%#x - %#x), %s + %u, %s' % (ptr_addr, ptr_addr + ptr_size, sb_addr.section.name, sb_addr.offset, sb_addr) + else: + type_str = '%#x' % (ptr_addr,) + show_offset = True + if show_offset and offset != 0: + type_str += ' + %-6u' % (offset,) + return type_str + +def dump_stack_history_entry(options, result, stack_history_entry, idx): + address = int(stack_history_entry.address) + if address: + type_flags = int(stack_history_entry.type_flags) + symbolicator = lldb.utils.symbolication.Symbolicator() + symbolicator.target = lldb.debugger.GetSelectedTarget() + type_str = type_flags_to_string(type_flags) + result.AppendMessage('stack[%u]: addr = 0x%x, type=%s, frames:' % (idx, address, type_str)) + frame_idx = 0 + idx = 0 + pc = int(stack_history_entry.frames[idx]) + while pc != 0: + if pc >= 0x1000: + frames = symbolicator.symbolicate(pc) + if frames: + for frame in frames: + result.AppendMessage(' [%u] %s' % (frame_idx, frame)) + frame_idx += 1 + else: + result.AppendMessage(' [%u] 0x%x' % (frame_idx, pc)) + frame_idx += 1 + idx = idx + 1 + pc = int(stack_history_entry.frames[idx]) + else: + pc = 0 + if idx >= options.max_frames: + 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)) + + result.AppendMessage('') + +def dump_stack_history_entries(options, result, addr, history): + # malloc_stack_entry *get_stack_history_for_address (const void * addr) + expr_prefix = ''' +typedef int kern_return_t; +typedef struct $malloc_stack_entry { + uint64_t address; + uint64_t argument; + uint32_t type_flags; + uint32_t num_frames; + uint64_t frames[512]; + kern_return_t err; +} $malloc_stack_entry; +''' + single_expr = ''' +#define MAX_FRAMES %u +typedef unsigned task_t; +$malloc_stack_entry stack; +stack.address = 0x%x; +stack.type_flags = 2; +stack.num_frames = 0; +stack.frames[0] = 0; +uint32_t max_stack_frames = MAX_FRAMES; +stack.err = (kern_return_t)__mach_stack_logging_get_frames ( + (task_t)mach_task_self(), + stack.address, + &stack.frames[0], + max_stack_frames, + &stack.num_frames); +if (stack.num_frames < MAX_FRAMES) + stack.frames[stack.num_frames] = 0; +else + stack.frames[MAX_FRAMES-1] = 0; +stack''' % (options.max_frames, addr); + + history_expr = ''' +typedef int kern_return_t; +typedef unsigned task_t; +#define MAX_FRAMES %u +#define MAX_HISTORY %u +typedef struct mach_stack_logging_record_t { + uint32_t type_flags; + uint64_t stack_identifier; + uint64_t argument; + uint64_t address; +} mach_stack_logging_record_t; +typedef void (*enumerate_callback_t)(mach_stack_logging_record_t, void *); +typedef struct malloc_stack_entry { + uint64_t address; + uint64_t argument; + uint32_t type_flags; + uint32_t num_frames; + uint64_t frames[MAX_FRAMES]; + kern_return_t frames_err; +} malloc_stack_entry; +typedef struct $malloc_stack_history { + task_t task; + unsigned idx; + malloc_stack_entry entries[MAX_HISTORY]; +} $malloc_stack_history; +$malloc_stack_history info = { (task_t)mach_task_self(), 0 }; +uint32_t max_stack_frames = MAX_FRAMES; +enumerate_callback_t callback = [] (mach_stack_logging_record_t stack_record, void *baton) -> void { + $malloc_stack_history *info = ($malloc_stack_history *)baton; + if (info->idx < MAX_HISTORY) { + malloc_stack_entry *stack_entry = &(info->entries[info->idx]); + stack_entry->address = stack_record.address; + stack_entry->type_flags = stack_record.type_flags; + stack_entry->argument = stack_record.argument; + stack_entry->num_frames = 0; + stack_entry->frames[0] = 0; + stack_entry->frames_err = (kern_return_t)__mach_stack_logging_frames_for_uniqued_stack ( + info->task, + stack_record.stack_identifier, + stack_entry->frames, + (uint32_t)MAX_FRAMES, + &stack_entry->num_frames); + // Terminate the frames with zero if there is room + if (stack_entry->num_frames < MAX_FRAMES) + stack_entry->frames[stack_entry->num_frames] = 0; + } + ++info->idx; +}; +(kern_return_t)__mach_stack_logging_enumerate_records (info.task, (uint64_t)0x%x, callback, &info); +info''' % (options.max_frames, options.max_history, addr); + + frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() + if history: + expr = history_expr + else: + expr = single_expr + expr_options = lldb.SBExpressionOptions() + expr_options.SetIgnoreBreakpoints(True); + expr_options.SetTimeoutInMicroSeconds (5*1000*1000) # 5 second timeout + expr_options.SetTryAllThreads (True) + expr_options.SetLanguage(lldb.eLanguageTypeObjC_plus_plus) + expr_options.SetPrefix(expr_prefix) + expr_sbvalue = frame.EvaluateExpression (expr, expr_options) + if options.verbose: + print "expression:" + print expr + print "expression result:" + print expr_sbvalue + if expr_sbvalue.error.Success(): + if history: + malloc_stack_history = lldb.value(expr_sbvalue) + num_stacks = int(malloc_stack_history.idx) + if num_stacks <= options.max_history: + i_max = num_stacks + else: + i_max = options.max_history + for i in range(i_max): + stack_history_entry = malloc_stack_history.entries[i] + dump_stack_history_entry(options, result, stack_history_entry, i) + if num_stacks > options.max_history: + 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)) + else: + stack_history_entry = lldb.value(expr_sbvalue) + dump_stack_history_entry(options, result, stack_history_entry, 0) + + else: + result.AppendMessage('error: expression failed "%s" => %s' % (expr, expr_sbvalue.error)) + + +def display_match_results (process, result, options, arg_str_description, expr, print_no_matches, expr_prefix = None): + frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() + if not frame: + result.AppendMessage('error: invalid frame') + return 0 + expr_options = lldb.SBExpressionOptions() + expr_options.SetIgnoreBreakpoints(True); + expr_options.SetFetchDynamicValue(lldb.eNoDynamicValues); + expr_options.SetTimeoutInMicroSeconds (30*1000*1000) # 30 second timeout + expr_options.SetTryAllThreads (False) + expr_options.SetLanguage (lldb.eLanguageTypeObjC_plus_plus) + if expr_prefix: + expr_options.SetPrefix (expr_prefix) + expr_sbvalue = frame.EvaluateExpression (expr, expr_options) + if options.verbose: + print "expression:" + print expr + print "expression result:" + print expr_sbvalue + if expr_sbvalue.error.Success(): + match_value = lldb.value(expr_sbvalue) + i = 0 + match_idx = 0 + while 1: + print_entry = True + match_entry = match_value[i]; i += 1 + if i > options.max_matches: + result.AppendMessage('warning: the max number of matches (%u) was reached, use the --max-matches option to get more results' % (options.max_matches)) + break + malloc_addr = match_entry.addr.sbvalue.unsigned + if malloc_addr == 0: + break + malloc_size = int(match_entry.size) + offset = int(match_entry.offset) + + if options.offset >= 0 and options.offset != offset: + print_entry = False + else: + match_addr = malloc_addr + offset + type_flags = int(match_entry.type) + #result.AppendMessage (hex(malloc_addr + offset)) + if type_flags == 64: + search_stack_old = options.search_stack + search_segments_old = options.search_segments + search_heap_old = options.search_heap + search_vm_regions = options.search_vm_regions + options.search_stack = True + options.search_segments = True + options.search_heap = True + options.search_vm_regions = False + if malloc_info_impl (lldb.debugger, result, options, [hex(malloc_addr + offset)]): + print_entry = False + options.search_stack = search_stack_old + options.search_segments = search_segments_old + options.search_heap = search_heap_old + options.search_vm_regions = search_vm_regions + if print_entry: + description = '%#16.16x: %s' % (match_addr, type_flags_to_description(process, type_flags, malloc_addr, malloc_size, offset, match_addr)) + if options.show_size: + description += ' <%5u>' % (malloc_size) + if options.show_range: + description += ' [%#x - %#x)' % (malloc_addr, malloc_addr + malloc_size) + derefed_dynamic_value = None + dynamic_value = match_entry.addr.sbvalue.GetDynamicValue(lldb.eDynamicCanRunTarget) + if dynamic_value.type.name == 'void *': + if options.type == 'pointer' and malloc_size == 4096: + error = lldb.SBError() + process = expr_sbvalue.GetProcess() + target = expr_sbvalue.GetTarget() + data = bytearray(process.ReadMemory(malloc_addr, 16, error)) + if data == '\xa1\xa1\xa1\xa1AUTORELEASE!': + ptr_size = target.addr_size + thread = process.ReadUnsignedFromMemory (malloc_addr + 16 + ptr_size, ptr_size, error) + # 4 bytes 0xa1a1a1a1 + # 12 bytes 'AUTORELEASE!' + # ptr bytes autorelease insertion point + # ptr bytes pthread_t + # ptr bytes next colder page + # ptr bytes next hotter page + # 4 bytes this page's depth in the list + # 4 bytes high-water mark + description += ' AUTORELEASE! for pthread_t %#x' % (thread) + # else: + # description += 'malloc(%u)' % (malloc_size) + # else: + # description += 'malloc(%u)' % (malloc_size) + else: + derefed_dynamic_value = dynamic_value.deref + if derefed_dynamic_value: + derefed_dynamic_type = derefed_dynamic_value.type + derefed_dynamic_type_size = derefed_dynamic_type.size + derefed_dynamic_type_name = derefed_dynamic_type.name + description += ' ' + description += derefed_dynamic_type_name + if offset < derefed_dynamic_type_size: + member_list = list(); + get_member_types_for_offset (derefed_dynamic_type, offset, member_list) + if member_list: + member_path = '' + for member in member_list: + member_name = member.name + if member_name: + if member_path: + member_path += '.' + member_path += member_name + if member_path: + if options.ivar_regex_blacklist: + for ivar_regex in options.ivar_regex_blacklist: + if ivar_regex.match(member_path): + print_entry = False + description += '.%s' % (member_path) + else: + description += '%u bytes after %s' % (offset - derefed_dynamic_type_size, derefed_dynamic_type_name) + else: + # strip the "*" from the end of the name since we were unable to dereference this + description += dynamic_value.type.name[0:-1] + if print_entry: + match_idx += 1 + result_output = '' + if description: + result_output += description + if options.print_type and derefed_dynamic_value: + result_output += ' %s' % (derefed_dynamic_value) + if options.print_object_description and dynamic_value: + desc = dynamic_value.GetObjectDescription() + if desc: + result_output += '\n%s' % (desc) + if result_output: + result.AppendMessage(result_output) + if options.memory: + cmd_result = lldb.SBCommandReturnObject() + if options.format == None: + memory_command = "memory read --force 0x%x 0x%x" % (malloc_addr, malloc_addr + malloc_size) + else: + memory_command = "memory read --force -f %s 0x%x 0x%x" % (options.format, malloc_addr, malloc_addr + malloc_size) + if options.verbose: + result.AppendMessage(memory_command) + lldb.debugger.GetCommandInterpreter().HandleCommand(memory_command, cmd_result) + result.AppendMessage(cmd_result.GetOutput()) + if options.stack_history: + dump_stack_history_entries(options, result, malloc_addr, 1) + elif options.stack: + dump_stack_history_entries(options, result, malloc_addr, 0) + return i + else: + result.AppendMessage(str(expr_sbvalue.error)) + return 0 + +def get_ptr_refs_options (): + usage = "usage: %prog [options] <EXPR> [EXPR ...]" + description='''Searches all allocations on the heap for pointer values on +darwin user space programs. Any matches that were found will dump the malloc +blocks that contain the pointers and might be able to print what kind of +objects the pointers are contained in using dynamic type information in the +program.''' + parser = optparse.OptionParser(description=description, prog='ptr_refs',usage=usage) + add_common_options(parser) + return parser + +def find_variable(debugger, command, result, dict): + usage = "usage: %prog [options] <ADDR> [ADDR ...]" + description='''Searches for a local variable in all frames that contains a hex ADDR.''' + command_args = shlex.split(command) + parser = optparse.OptionParser(description=description, prog='find_variable',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + try: + (options, args) = parser.parse_args(command_args) + except: + return + + process = debugger.GetSelectedTarget().GetProcess() + if not process: + result.AppendMessage('error: invalid process') + return + + for arg in args: + var_addr = int(arg, 16) + print >>result, "Finding a variable with address %#x..." % (var_addr) + done = False + for thread in process: + for frame in thread: + var = find_variable_containing_address(options.verbose, frame, var_addr) + if var: + print var + done = True + break + if done: + break + +def ptr_refs(debugger, command, result, dict): + command_args = shlex.split(command) + parser = get_ptr_refs_options() + try: + (options, args) = parser.parse_args(command_args) + except: + return + + process = debugger.GetSelectedTarget().GetProcess() + if not process: + result.AppendMessage('error: invalid process') + return + frame = process.GetSelectedThread().GetSelectedFrame() + if not frame: + result.AppendMessage('error: invalid frame') + return + + options.type = 'pointer' + if options.format == None: + options.format = "A" # 'A' is "address" format + + if args: + # When we initialize the expression, we must define any types that + # we will need when looking at every allocation. We must also define + # a type named callback_baton_t and make an instance named "baton" + # and initialize it how ever we want to. The address of "baton" will + # be passed into our range callback. callback_baton_t must contain + # a member named "callback" whose type is "range_callback_t". This + # will be used by our zone callbacks to call the range callback for + # each malloc range. + expr_prefix = ''' +struct $malloc_match { + void *addr; + uintptr_t size; + uintptr_t offset; + uintptr_t type; +}; +''' + user_init_code_format = ''' +#define MAX_MATCHES %u +typedef struct callback_baton_t { + range_callback_t callback; + unsigned num_matches; + $malloc_match matches[MAX_MATCHES]; + void *ptr; +} callback_baton_t; +range_callback_t range_callback = [](task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size) -> void { + callback_baton_t *info = (callback_baton_t *)baton; + typedef void* T; + const unsigned size = sizeof(T); + T *array = (T*)ptr_addr; + for (unsigned idx = 0; ((idx + 1) * sizeof(T)) <= ptr_size; ++idx) { + if (array[idx] == info->ptr) { + if (info->num_matches < MAX_MATCHES) { + info->matches[info->num_matches].addr = (void*)ptr_addr; + info->matches[info->num_matches].size = ptr_size; + info->matches[info->num_matches].offset = idx*sizeof(T); + info->matches[info->num_matches].type = type; + ++info->num_matches; + } + } + } +}; +callback_baton_t baton = { range_callback, 0, {0}, (void *)%s }; +''' + # We must also define a snippet of code to be run that returns + # the result of the expression we run. + # Here we return NULL if our pointer was not found in any malloc blocks, + # and we return the address of the matches array so we can then access + # the matching results + user_return_code = '''if (baton.num_matches < MAX_MATCHES) + baton.matches[baton.num_matches].addr = 0; // Terminate the matches array +baton.matches''' + # Iterate through all of our pointer expressions and display the results + for ptr_expr in args: + user_init_code = user_init_code_format % (options.max_matches, ptr_expr) + expr = get_iterate_memory_expr(options, process, user_init_code, user_return_code) + arg_str_description = 'malloc block containing pointer %s' % ptr_expr + display_match_results (process, result, options, arg_str_description, expr, True, expr_prefix) + else: + result.AppendMessage('error: no pointer arguments were given') + +def get_cstr_refs_options(): + usage = "usage: %prog [options] <CSTR> [CSTR ...]" + description='''Searches all allocations on the heap for C string values on +darwin user space programs. Any matches that were found will dump the malloc +blocks that contain the C strings and might be able to print what kind of +objects the pointers are contained in using dynamic type information in the +program.''' + parser = optparse.OptionParser(description=description, prog='cstr_refs',usage=usage) + add_common_options(parser) + return parser + +def cstr_refs(debugger, command, result, dict): + command_args = shlex.split(command) + parser = get_cstr_refs_options(); + try: + (options, args) = parser.parse_args(command_args) + except: + return + + process = debugger.GetSelectedTarget().GetProcess() + if not process: + result.AppendMessage('error: invalid process') + return + frame = process.GetSelectedThread().GetSelectedFrame() + if not frame: + result.AppendMessage('error: invalid frame') + return + + + options.type = 'cstr' + if options.format == None: + options.format = "Y" # 'Y' is "bytes with ASCII" format + + if args: + # When we initialize the expression, we must define any types that + # we will need when looking at every allocation. We must also define + # a type named callback_baton_t and make an instance named "baton" + # and initialize it how ever we want to. The address of "baton" will + # be passed into our range callback. callback_baton_t must contain + # a member named "callback" whose type is "range_callback_t". This + # will be used by our zone callbacks to call the range callback for + # each malloc range. + expr_prefix = ''' +struct $malloc_match { + void *addr; + uintptr_t size; + uintptr_t offset; + uintptr_t type; +}; +''' + user_init_code_format = ''' +#define MAX_MATCHES %u +typedef struct callback_baton_t { + range_callback_t callback; + unsigned num_matches; + $malloc_match matches[MAX_MATCHES]; + const char *cstr; + unsigned cstr_len; +} callback_baton_t; +range_callback_t range_callback = [](task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size) -> void { + callback_baton_t *info = (callback_baton_t *)baton; + if (info->cstr_len < ptr_size) { + const char *begin = (const char *)ptr_addr; + const char *end = begin + ptr_size - info->cstr_len; + for (const char *s = begin; s < end; ++s) { + if ((int)memcmp(s, info->cstr, info->cstr_len) == 0) { + if (info->num_matches < MAX_MATCHES) { + info->matches[info->num_matches].addr = (void*)ptr_addr; + info->matches[info->num_matches].size = ptr_size; + info->matches[info->num_matches].offset = s - begin; + info->matches[info->num_matches].type = type; + ++info->num_matches; + } + } + } + } +}; +const char *cstr = "%s"; +callback_baton_t baton = { range_callback, 0, {0}, cstr, (unsigned)strlen(cstr) };''' + # We must also define a snippet of code to be run that returns + # the result of the expression we run. + # Here we return NULL if our pointer was not found in any malloc blocks, + # and we return the address of the matches array so we can then access + # the matching results + user_return_code = '''if (baton.num_matches < MAX_MATCHES) + baton.matches[baton.num_matches].addr = 0; // Terminate the matches array +baton.matches''' + # Iterate through all of our pointer expressions and display the results + for cstr in args: + user_init_code = user_init_code_format % (options.max_matches, cstr) + expr = get_iterate_memory_expr(options, process, user_init_code, user_return_code) + arg_str_description = 'malloc block containing "%s"' % cstr + display_match_results (process, result, options, arg_str_description, expr, True, expr_prefix) + else: + result.AppendMessage('error: command takes one or more C string arguments') + + +def get_malloc_info_options(): + usage = "usage: %prog [options] <EXPR> [EXPR ...]" + description='''Searches the heap a malloc block that contains the addresses +specified as one or more address expressions. Any matches that were found will +dump the malloc blocks that match or contain the specified address. The matching +blocks might be able to show what kind of objects they are using dynamic type +information in the program.''' + parser = optparse.OptionParser(description=description, prog='malloc_info',usage=usage) + add_common_options(parser) + return parser + +def malloc_info(debugger, command, result, dict): + command_args = shlex.split(command) + parser = get_malloc_info_options() + try: + (options, args) = parser.parse_args(command_args) + except: + return + malloc_info_impl (debugger, result, options, args) + +def malloc_info_impl (debugger, result, options, args): + # We are specifically looking for something on the heap only + options.type = 'malloc_info' + + process = debugger.GetSelectedTarget().GetProcess() + if not process: + result.AppendMessage('error: invalid process') + return + frame = process.GetSelectedThread().GetSelectedFrame() + if not frame: + result.AppendMessage('error: invalid frame') + return + expr_prefix = ''' +struct $malloc_match { + void *addr; + uintptr_t size; + uintptr_t offset; + uintptr_t type; +}; +''' + + user_init_code_format = ''' +typedef struct callback_baton_t { + range_callback_t callback; + unsigned num_matches; + $malloc_match matches[2]; // Two items so they can be NULL terminated + void *ptr; +} callback_baton_t; +range_callback_t range_callback = [](task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size) -> void { + callback_baton_t *info = (callback_baton_t *)baton; + if (info->num_matches == 0) { + uint8_t *p = (uint8_t *)info->ptr; + uint8_t *lo = (uint8_t *)ptr_addr; + uint8_t *hi = lo + ptr_size; + if (lo <= p && p < hi) { + info->matches[info->num_matches].addr = (void*)ptr_addr; + info->matches[info->num_matches].size = ptr_size; + info->matches[info->num_matches].offset = p - lo; + info->matches[info->num_matches].type = type; + info->num_matches = 1; + } + } +}; +callback_baton_t baton = { range_callback, 0, {0}, (void *)%s }; +baton.matches[0].addr = 0; +baton.matches[1].addr = 0;''' + if args: + total_matches = 0 + for ptr_expr in args: + user_init_code = user_init_code_format % (ptr_expr) + expr = get_iterate_memory_expr(options, process, user_init_code, 'baton.matches') + arg_str_description = 'malloc block that contains %s' % ptr_expr + total_matches += display_match_results (process, result, options, arg_str_description, expr, True, expr_prefix) + return total_matches + else: + result.AppendMessage('error: command takes one or more pointer expressions') + return 0 + +def get_thread_stack_ranges_struct (process): + '''Create code that defines a structure that represents threads stack bounds + for all threads. It returns a static sized array initialized with all of + the tid, base, size structs for all the threads.''' + stack_dicts = list() + if process: + i = 0; + for thread in process: + min_sp = thread.frame[0].sp + max_sp = min_sp + for frame in thread.frames: + sp = frame.sp + if sp < min_sp: min_sp = sp + if sp > max_sp: max_sp = sp + if min_sp < max_sp: + stack_dicts.append ({ 'tid' : thread.GetThreadID(), 'base' : min_sp , 'size' : max_sp-min_sp, 'index' : i }) + i += 1 + stack_dicts_len = len(stack_dicts) + if stack_dicts_len > 0: + result = ''' +#define NUM_STACKS %u +#define STACK_RED_ZONE_SIZE %u +typedef struct thread_stack_t { uint64_t tid, base, size; } thread_stack_t; +thread_stack_t stacks[NUM_STACKS];''' % (stack_dicts_len, process.target.GetStackRedZoneSize()) + for stack_dict in stack_dicts: + result += ''' +stacks[%(index)u].tid = 0x%(tid)x; +stacks[%(index)u].base = 0x%(base)x; +stacks[%(index)u].size = 0x%(size)x;''' % stack_dict + return result + else: + return '' + +def get_sections_ranges_struct (process): + '''Create code that defines a structure that represents all segments that + can contain data for all images in "target". It returns a static sized + array initialized with all of base, size structs for all the threads.''' + target = process.target + segment_dicts = list() + for (module_idx, module) in enumerate(target.modules): + for sect_idx in range(module.GetNumSections()): + section = module.GetSectionAtIndex(sect_idx) + if not section: + break + name = section.name + if name != '__TEXT' and name != '__LINKEDIT' and name != '__PAGEZERO': + base = section.GetLoadAddress(target) + size = section.GetByteSize() + if base != lldb.LLDB_INVALID_ADDRESS and size > 0: + segment_dicts.append ({ 'base' : base, 'size' : size }) + segment_dicts_len = len(segment_dicts) + if segment_dicts_len > 0: + result = ''' +#define NUM_SEGMENTS %u +typedef struct segment_range_t { uint64_t base; uint32_t size; } segment_range_t; +segment_range_t segments[NUM_SEGMENTS];''' % (segment_dicts_len,) + for (idx, segment_dict) in enumerate(segment_dicts): + segment_dict['index'] = idx + result += ''' +segments[%(index)u].base = 0x%(base)x; +segments[%(index)u].size = 0x%(size)x;''' % segment_dict + return result + else: + return '' + +def section_ptr_refs(debugger, command, result, dict): + command_args = shlex.split(command) + usage = "usage: %prog [options] <EXPR> [EXPR ...]" + description='''Searches section contents for pointer values in darwin user space programs.''' + parser = optparse.OptionParser(description=description, prog='section_ptr_refs',usage=usage) + add_common_options(parser) + parser.add_option('--section', action='append', type='string', dest='section_names', help='section name to search', default=list()) + try: + (options, args) = parser.parse_args(command_args) + except: + return + + options.type = 'pointer' + + sections = list() + section_modules = list() + if not options.section_names: + result.AppendMessage('error: at least one section must be specified with the --section option') + return + + target = debugger.GetSelectedTarget() + for module in target.modules: + for section_name in options.section_names: + section = module.section[section_name] + if section: + sections.append (section) + section_modules.append (module) + if sections: + dylid_load_err = load_dylib() + if dylid_load_err: + result.AppendMessage(dylid_load_err) + return + frame = target.GetProcess().GetSelectedThread().GetSelectedFrame() + for expr_str in args: + for (idx, section) in enumerate(sections): + expr = 'find_pointer_in_memory(0x%xllu, %ullu, (void *)%s)' % (section.addr.load_addr, section.size, expr_str) + arg_str_description = 'section %s.%s containing "%s"' % (section_modules[idx].file.fullpath, section.name, expr_str) + num_matches = display_match_results (target.GetProcess(), result, options, arg_str_description, expr, False) + if num_matches: + if num_matches < options.max_matches: + options.max_matches = options.max_matches - num_matches + else: + options.max_matches = 0 + if options.max_matches == 0: + return + else: + result.AppendMessage('error: no sections were found that match any of %s' % (', '.join(options.section_names))) + +def get_objc_refs_options(): + usage = "usage: %prog [options] <CLASS> [CLASS ...]" + description='''Searches all allocations on the heap for instances of +objective C classes, or any classes that inherit from the specified classes +in darwin user space programs. Any matches that were found will dump the malloc +blocks that contain the C strings and might be able to print what kind of +objects the pointers are contained in using dynamic type information in the +program.''' + parser = optparse.OptionParser(description=description, prog='objc_refs',usage=usage) + add_common_options(parser) + return parser + +def objc_refs(debugger, command, result, dict): + command_args = shlex.split(command) + parser = get_objc_refs_options() + try: + (options, args) = parser.parse_args(command_args) + except: + return + + process = debugger.GetSelectedTarget().GetProcess() + if not process: + result.AppendMessage('error: invalid process') + return + frame = process.GetSelectedThread().GetSelectedFrame() + if not frame: + result.AppendMessage('error: invalid frame') + return + + options.type = 'isa' + if options.format == None: + options.format = "A" # 'A' is "address" format + + expr_options = lldb.SBExpressionOptions() + expr_options.SetIgnoreBreakpoints(True); + expr_options.SetTimeoutInMicroSeconds (3*1000*1000) # 3 second infinite timeout + expr_options.SetTryAllThreads (True) + expr_options.SetLanguage(lldb.eLanguageTypeObjC_plus_plus) + num_objc_classes_value = frame.EvaluateExpression("(int)objc_getClassList((void *)0, (int)0)", expr_options) + if not num_objc_classes_value.error.Success(): + result.AppendMessage('error: %s' % num_objc_classes_value.error.GetCString()) + return + + num_objc_classes = num_objc_classes_value.GetValueAsUnsigned() + if num_objc_classes == 0: + result.AppendMessage('error: no objective C classes in program') + return + + if args: + # When we initialize the expression, we must define any types that + # we will need when looking at every allocation. We must also define + # a type named callback_baton_t and make an instance named "baton" + # and initialize it how ever we want to. The address of "baton" will + # be passed into our range callback. callback_baton_t must contain + # a member named "callback" whose type is "range_callback_t". This + # will be used by our zone callbacks to call the range callback for + # each malloc range. + expr_prefix = ''' +struct $malloc_match { + void *addr; + uintptr_t size; + uintptr_t offset; + uintptr_t type; +}; +''' + + user_init_code_format = ''' +#define MAX_MATCHES %u +typedef int (*compare_callback_t)(const void *a, const void *b); +typedef struct callback_baton_t { + range_callback_t callback; + compare_callback_t compare_callback; + unsigned num_matches; + $malloc_match matches[MAX_MATCHES]; + void *isa; + Class classes[%u]; +} callback_baton_t; +compare_callback_t compare_callback = [](const void *a, const void *b) -> int { + Class a_ptr = *(Class *)a; + Class b_ptr = *(Class *)b; + if (a_ptr < b_ptr) return -1; + if (a_ptr > b_ptr) return +1; + return 0; +}; +typedef Class (*class_getSuperclass_type)(void *isa); +range_callback_t range_callback = [](task_t task, void *baton, unsigned type, uintptr_t ptr_addr, uintptr_t ptr_size) -> void { + class_getSuperclass_type class_getSuperclass_impl = (class_getSuperclass_type)class_getSuperclass; + callback_baton_t *info = (callback_baton_t *)baton; + if (sizeof(Class) <= ptr_size) { + Class *curr_class_ptr = (Class *)ptr_addr; + Class *matching_class_ptr = (Class *)bsearch (curr_class_ptr, + (const void *)info->classes, + sizeof(info->classes)/sizeof(Class), + sizeof(Class), + info->compare_callback); + if (matching_class_ptr) { + bool match = false; + if (info->isa) { + Class isa = *curr_class_ptr; + if (info->isa == isa) + match = true; + else { // if (info->objc.match_superclasses) { + Class super = class_getSuperclass_impl(isa); + while (super) { + if (super == info->isa) { + match = true; + break; + } + super = class_getSuperclass_impl(super); + } + } + } + else + match = true; + if (match) { + if (info->num_matches < MAX_MATCHES) { + info->matches[info->num_matches].addr = (void*)ptr_addr; + info->matches[info->num_matches].size = ptr_size; + info->matches[info->num_matches].offset = 0; + info->matches[info->num_matches].type = type; + ++info->num_matches; + } + } + } + } +}; +callback_baton_t baton = { range_callback, compare_callback, 0, {0}, (void *)0x%x, {0} }; +int nc = (int)objc_getClassList(baton.classes, sizeof(baton.classes)/sizeof(Class)); +(void)qsort (baton.classes, sizeof(baton.classes)/sizeof(Class), sizeof(Class), compare_callback);''' + # We must also define a snippet of code to be run that returns + # the result of the expression we run. + # Here we return NULL if our pointer was not found in any malloc blocks, + # and we return the address of the matches array so we can then access + # the matching results + user_return_code = '''if (baton.num_matches < MAX_MATCHES) + baton.matches[baton.num_matches].addr = 0; // Terminate the matches array + baton.matches''' + # Iterate through all of our ObjC class name arguments + for class_name in args: + addr_expr_str = "(void *)[%s class]" % class_name + expr_options = lldb.SBExpressionOptions() + expr_options.SetIgnoreBreakpoints(True); + expr_options.SetTimeoutInMicroSeconds (1*1000*1000) # 1 second timeout + expr_options.SetTryAllThreads (True) + expr_options.SetLanguage(lldb.eLanguageTypeObjC_plus_plus) + expr_sbvalue = frame.EvaluateExpression (addr_expr_str, expr_options) + if expr_sbvalue.error.Success(): + isa = expr_sbvalue.unsigned + if isa: + options.type = 'isa' + result.AppendMessage('Searching for all instances of classes or subclasses of "%s" (isa=0x%x)' % (class_name, isa)) + user_init_code = user_init_code_format % (options.max_matches, num_objc_classes, isa) + expr = get_iterate_memory_expr(options, process, user_init_code, user_return_code) + arg_str_description = 'objective C classes with isa 0x%x' % isa + display_match_results (process, result, options, arg_str_description, expr, True, expr_prefix) + else: + result.AppendMessage('error: Can\'t find isa for an ObjC class named "%s"' % (class_name)) + else: + result.AppendMessage('error: expression error for "%s": %s' % (addr_expr_str, expr_sbvalue.error)) + else: + result.AppendMessage('error: command takes one or more C string arguments'); + +if __name__ == '__main__': + lldb.debugger = lldb.SBDebugger.Create() + +# Make the options so we can generate the help text for the new LLDB +# command line command prior to registering it with LLDB below. This way +# if clients in LLDB type "help malloc_info", they will see the exact same +# output as typing "malloc_info --help". +ptr_refs.__doc__ = get_ptr_refs_options().format_help() +cstr_refs.__doc__ = get_cstr_refs_options().format_help() +malloc_info.__doc__ = get_malloc_info_options().format_help() +objc_refs.__doc__ = get_objc_refs_options().format_help() +lldb.debugger.HandleCommand('command script add -f %s.ptr_refs ptr_refs' % __name__) +lldb.debugger.HandleCommand('command script add -f %s.cstr_refs cstr_refs' % __name__) +lldb.debugger.HandleCommand('command script add -f %s.malloc_info malloc_info' % __name__) +lldb.debugger.HandleCommand('command script add -f %s.find_variable find_variable' % __name__) +# lldb.debugger.HandleCommand('command script add -f %s.heap heap' % package_name) +# lldb.debugger.HandleCommand('command script add -f %s.section_ptr_refs section_ptr_refs' % package_name) +# lldb.debugger.HandleCommand('command script add -f %s.stack_ptr_refs stack_ptr_refs' % package_name) +lldb.debugger.HandleCommand('command script add -f %s.objc_refs objc_refs' % __name__) +print '"malloc_info", "ptr_refs", "cstr_refs", "find_variable", and "objc_refs" commands have been installed, use the "--help" options on these commands for detailed help.' + + + + diff --git a/examples/darwin/heap_find/heap/Makefile b/examples/darwin/heap_find/heap/Makefile new file mode 100644 index 000000000000..0e33dc9f8936 --- /dev/null +++ b/examples/darwin/heap_find/heap/Makefile @@ -0,0 +1,33 @@ +#---------------------------------------------------------------------- +# Fill in the source files to build +#---------------------------------------------------------------------- +# Uncomment line below for debugging shell commands +# SHELL = /bin/sh -x + +#---------------------------------------------------------------------- +# Change any build/tool options needed +#---------------------------------------------------------------------- +ARCH ?= x86_64 +CFLAGS ?=-arch $(ARCH) -gdwarf-2 -O0 +CXX ?= $(shell xcrun -find clang++) +EXE ?= libheap.dylib +DSYM ?= $(EXE).dSYM + +#---------------------------------------------------------------------- +# Compile the executable from all the objects (default rule) with no +# dsym file. +#---------------------------------------------------------------------- +$(EXE) : heap_find.cpp + $(CXX) $(CFLAGS) -install_name "@executable_path/libheap.dylib" -dynamiclib -lobjc heap_find.cpp -o "$(EXE)" + +#---------------------------------------------------------------------- +# Include all of the makefiles for each source file so we don't have +# to manually track all of the prerequisites for each source file. +#---------------------------------------------------------------------- +.PHONY: clean +all: $(EXE) +clean: + rm -rf "$(EXE)" "$(DSYM)" + + + diff --git a/examples/darwin/heap_find/heap/heap_find.cpp b/examples/darwin/heap_find/heap/heap_find.cpp new file mode 100644 index 000000000000..de896775e402 --- /dev/null +++ b/examples/darwin/heap_find/heap/heap_find.cpp @@ -0,0 +1,1071 @@ +//===-- heap_find.c ---------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file compiles into a dylib and can be used on darwin to find data that +// is contained in active malloc blocks. To use this make the project, then +// load the shared library in a debug session while you are stopped: +// +// (lldb) process load /path/to/libheap.dylib +// +// Now you can use the "find_pointer_in_heap" and "find_cstring_in_heap" +// functions in the expression parser. +// +// This will grep everything in all active allocation blocks and print and +// malloc blocks that contain the pointer 0x112233000000: +// +// (lldb) expression find_pointer_in_heap (0x112233000000) +// +// This will grep everything in all active allocation blocks and print and +// malloc blocks that contain the C string "hello" (as a substring, no +// NULL termination included): +// +// (lldb) expression find_cstring_in_heap ("hello") +// +// The results will be printed to the STDOUT of the inferior program. The +// return value of the "find_pointer_in_heap" function is the number of +// pointer references that were found. A quick example shows +// +// (lldb) expr find_pointer_in_heap(0x0000000104000410) +// (uint32_t) $5 = 0x00000002 +// 0x104000740: 0x0000000104000410 found in malloc block 0x104000730 + 16 (malloc_size = 48) +// 0x100820060: 0x0000000104000410 found in malloc block 0x100820000 + 96 (malloc_size = 4096) +// +// From the above output we see that 0x104000410 was found in the malloc block +// at 0x104000730 and 0x100820000. If we want to see what these blocks are, we +// can display the memory for this block using the "address" ("A" for short) +// format. The address format shows pointers, and if those pointers point to +// objects that have symbols or know data contents, it will display information +// about the pointers: +// +// (lldb) memory read --format address --count 1 0x104000730 +// 0x104000730: 0x0000000100002460 (void *)0x0000000100002488: MyString +// +// We can see that the first block is a "MyString" object that contains our +// pointer value at offset 16. +// +// Looking at the next pointers, are a bit more tricky: +// (lldb) memory read -fA 0x100820000 -c1 +// 0x100820000: 0x4f545541a1a1a1a1 +// (lldb) memory read 0x100820000 +// 0x100820000: a1 a1 a1 a1 41 55 54 4f 52 45 4c 45 41 53 45 21 ....AUTORELEASE! +// 0x100820010: 78 00 82 00 01 00 00 00 60 f9 e8 75 ff 7f 00 00 x.......`..u.... +// +// This is an objective C auto release pool object that contains our pointer. +// C++ classes will show up if they are virtual as something like: +// (lldb) memory read --format address --count 1 0x104008000 +// 0x104008000: 0x109008000 vtable for lldb_private::Process +// +// This is a clue that the 0x104008000 is a "lldb_private::Process *". +//===----------------------------------------------------------------------===// +// C includes +#include <assert.h> +#include <ctype.h> +#include <dlfcn.h> +#include <mach/mach.h> +#include <mach/mach_vm.h> +#include <malloc/malloc.h> +#include <objc/objc-runtime.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +// C++ includes +#include <vector> + +//---------------------------------------------------------------------- +// Redefine private types from "/usr/local/include/stack_logging.h" +//---------------------------------------------------------------------- +typedef struct { + uint32_t type_flags; + uint64_t stack_identifier; + uint64_t argument; + mach_vm_address_t address; +} mach_stack_logging_record_t; + +//---------------------------------------------------------------------- +// Redefine private defines from "/usr/local/include/stack_logging.h" +//---------------------------------------------------------------------- +#define stack_logging_type_free 0 +#define stack_logging_type_generic 1 +#define stack_logging_type_alloc 2 +#define stack_logging_type_dealloc 4 +// This bit is made up by this code +#define stack_logging_type_vm_region 8 + +//---------------------------------------------------------------------- +// Redefine private function prototypes from +// "/usr/local/include/stack_logging.h" +//---------------------------------------------------------------------- +extern "C" kern_return_t +__mach_stack_logging_set_file_path ( + task_t task, + char* file_path +); + +extern "C" kern_return_t +__mach_stack_logging_get_frames ( + task_t task, + mach_vm_address_t address, + mach_vm_address_t *stack_frames_buffer, + uint32_t max_stack_frames, + uint32_t *count +); + +extern "C" kern_return_t +__mach_stack_logging_enumerate_records ( + task_t task, + mach_vm_address_t address, + void enumerator(mach_stack_logging_record_t, void *), + void *context +); + +extern "C" kern_return_t +__mach_stack_logging_frames_for_uniqued_stack ( + task_t task, + uint64_t stack_identifier, + mach_vm_address_t *stack_frames_buffer, + uint32_t max_stack_frames, + uint32_t *count +); + +extern "C" void *gdb_class_getClass (void *objc_class); + +static void +range_info_callback (task_t task, + void *baton, + unsigned type, + uint64_t ptr_addr, + uint64_t ptr_size); + +//---------------------------------------------------------------------- +// Redefine private global variables prototypes from +// "/usr/local/include/stack_logging.h" +//---------------------------------------------------------------------- + +extern "C" int stack_logging_enable_logging; + +//---------------------------------------------------------------------- +// Local defines +//---------------------------------------------------------------------- +#define MAX_FRAMES 1024 + +//---------------------------------------------------------------------- +// Local Typedefs and Types +//---------------------------------------------------------------------- +typedef void range_callback_t (task_t task, void *baton, unsigned type, uint64_t ptr_addr, uint64_t ptr_size); +typedef void zone_callback_t (void *info, const malloc_zone_t *zone); +typedef int (*comare_function_t)(const void *, const void *); +struct range_callback_info_t +{ + zone_callback_t *zone_callback; + range_callback_t *range_callback; + void *baton; + int check_vm_regions; +}; + +enum data_type_t +{ + eDataTypeAddress, + eDataTypeContainsData, + eDataTypeObjC, + eDataTypeHeapInfo +}; + +struct aligned_data_t +{ + const uint8_t *buffer; + uint32_t size; + uint32_t align; +}; + +struct objc_data_t +{ + void *match_isa; // Set to NULL for all objective C objects + bool match_superclasses; +}; + +struct range_contains_data_callback_info_t +{ + data_type_t type; + const void *lookup_addr; + union + { + uintptr_t addr; + aligned_data_t data; + objc_data_t objc; + }; + uint32_t match_count; + bool done; + bool unique; +}; + +struct malloc_match +{ + void *addr; + intptr_t size; + intptr_t offset; + uintptr_t type; +}; + +struct malloc_stack_entry +{ + const void *address; + uint64_t argument; + uint32_t type_flags; + uint32_t num_frames; + mach_vm_address_t frames[MAX_FRAMES]; +}; + +struct malloc_block_contents +{ + union { + Class isa; + void *pointers[2]; + }; +}; + +static int +compare_void_ptr (const void *a, const void *b) +{ + Class a_ptr = *(Class *)a; + Class b_ptr = *(Class *)b; + if (a_ptr < b_ptr) return -1; + if (a_ptr > b_ptr) return +1; + return 0; +} + +class MatchResults +{ + enum { + k_max_entries = 8 * 1024 + }; +public: + MatchResults () : + m_size(0) + { + } + + void + clear() + { + m_size = 0; + bzero (&m_entries, sizeof(m_entries)); + } + + bool + empty() const + { + return m_size == 0; + } + + void + push_back (const malloc_match& m, bool unique = false) + { + if (unique) + { + // Don't add the entry if there is already a match for this address + for (uint32_t i=0; i<m_size; ++i) + { + if (((uint8_t *)m_entries[i].addr + m_entries[i].offset) == ((uint8_t *)m.addr + m.offset)) + return; // Duplicate entry + } + } + if (m_size < k_max_entries - 1) + { + m_entries[m_size] = m; + m_size++; + } + } + + malloc_match * + data () + { + // If empty, return NULL + if (empty()) + return NULL; + // In not empty, terminate and return the result + malloc_match terminator_entry = { NULL, 0, 0, 0 }; + // We always leave room for an empty entry at the end + m_entries[m_size] = terminator_entry; + return m_entries; + } + +protected: + malloc_match m_entries[k_max_entries]; + uint32_t m_size; +}; + +class MallocStackLoggingEntries +{ + enum { k_max_entries = 128 }; +public: + MallocStackLoggingEntries () : + m_size(0) + { + } + + void + clear() + { + m_size = 0; + } + + bool + empty() const + { + return m_size == 0; + } + + + malloc_stack_entry * + next () + { + if (m_size < k_max_entries - 1) + { + malloc_stack_entry * result = m_entries + m_size; + ++m_size; + return result; + } + return NULL; // Out of entries... + } + + malloc_stack_entry * + data () + { + // If empty, return NULL + if (empty()) + return NULL; + // In not empty, terminate and return the result + m_entries[m_size].address = NULL; + m_entries[m_size].argument = 0; + m_entries[m_size].type_flags = 0; + m_entries[m_size].num_frames = 0; + return m_entries; + } + +protected: + malloc_stack_entry m_entries[k_max_entries]; + uint32_t m_size; +}; + +//---------------------------------------------------------------------- +// A safe way to allocate memory and keep it from interfering with the +// malloc enumerators. +//---------------------------------------------------------------------- +void * +safe_malloc(size_t n_bytes) +{ + if (n_bytes > 0) + { + const int k_page_size = getpagesize(); + const mach_vm_size_t vm_size = ((n_bytes + k_page_size - 1)/k_page_size) * k_page_size; + vm_address_t address = 0; + kern_return_t kerr = vm_allocate (mach_task_self(), &address, vm_size, true); + if (kerr == KERN_SUCCESS) + return (void *)address; + } + return NULL; +} + + +//---------------------------------------------------------------------- +// ObjCClasses +//---------------------------------------------------------------------- +class ObjCClasses +{ +public: + ObjCClasses() : + m_objc_class_ptrs (NULL), + m_size (0) + { + } + + bool + Update() + { + // TODO: find out if class list has changed and update if needed + if (m_objc_class_ptrs == NULL) + { + m_size = objc_getClassList(NULL, 0); + if (m_size > 0) + { + // Allocate the class pointers + m_objc_class_ptrs = (Class *)safe_malloc (m_size * sizeof(Class)); + m_size = objc_getClassList(m_objc_class_ptrs, m_size); + // Sort Class pointers for quick lookup + ::qsort (m_objc_class_ptrs, m_size, sizeof(Class), compare_void_ptr); + } + else + return false; + } + return true; + } + + uint32_t + FindClassIndex (Class isa) + { + Class *matching_class = (Class *)bsearch (&isa, + m_objc_class_ptrs, + m_size, + sizeof(Class), + compare_void_ptr); + if (matching_class) + { + uint32_t idx = matching_class - m_objc_class_ptrs; + return idx; + } + return UINT32_MAX; + } + + Class + GetClassAtIndex (uint32_t idx) const + { + if (idx < m_size) + return m_objc_class_ptrs[idx]; + return NULL; + } + uint32_t + GetSize() const + { + return m_size; + } +private: + Class *m_objc_class_ptrs; + uint32_t m_size; +}; + + + +//---------------------------------------------------------------------- +// Local global variables +//---------------------------------------------------------------------- +MatchResults g_matches; +MallocStackLoggingEntries g_malloc_stack_history; +ObjCClasses g_objc_classes; + +//---------------------------------------------------------------------- +// ObjCClassInfo +//---------------------------------------------------------------------- + +enum HeapInfoSortType +{ + eSortTypeNone, + eSortTypeBytes, + eSortTypeCount +}; + +class ObjCClassInfo +{ +public: + ObjCClassInfo() : + m_entries (NULL), + m_size (0), + m_sort_type (eSortTypeNone) + { + } + + void + Update (const ObjCClasses &objc_classes) + { + m_size = objc_classes.GetSize(); + m_entries = (Entry *)safe_malloc (m_size * sizeof(Entry)); + m_sort_type = eSortTypeNone; + Reset (); + } + + bool + AddInstance (uint32_t idx, uint64_t ptr_size) + { + if (m_size == 0) + Update (g_objc_classes); + // Update the totals for the classes + if (idx < m_size) + { + m_entries[idx].bytes += ptr_size; + ++m_entries[idx].count; + return true; + } + return false; + } + + void + Reset () + { + m_sort_type = eSortTypeNone; + for (uint32_t i=0; i<m_size; ++i) + { + // In case we sort the entries after gathering the data, we will + // want to know the index into the m_objc_class_ptrs[] array. + m_entries[i].idx = i; + m_entries[i].bytes = 0; + m_entries[i].count = 0; + } + } + void + SortByTotalBytes (const ObjCClasses &objc_classes, bool print) + { + if (m_sort_type != eSortTypeBytes && m_size > 0) + { + ::qsort (m_entries, m_size, sizeof(Entry), (comare_function_t)compare_bytes); + m_sort_type = eSortTypeBytes; + } + if (print && m_size > 0) + { + puts("Objective C objects by total bytes:"); + puts("Total Bytes Class Name"); + puts("----------- -----------------------------------------------------------------"); + for (uint32_t i=0; i<m_size && m_entries[i].bytes > 0; ++i) + { + printf ("%11llu %s\n", m_entries[i].bytes, class_getName (objc_classes.GetClassAtIndex(m_entries[i].idx))); + } + } + } + void + SortByTotalCount (const ObjCClasses &objc_classes, bool print) + { + if (m_sort_type != eSortTypeCount && m_size > 0) + { + ::qsort (m_entries, m_size, sizeof(Entry), (comare_function_t)compare_count); + m_sort_type = eSortTypeCount; + } + if (print && m_size > 0) + { + puts("Objective C objects by total count:"); + puts("Count Class Name"); + puts("-------- -----------------------------------------------------------------"); + for (uint32_t i=0; i<m_size && m_entries[i].count > 0; ++i) + { + printf ("%8u %s\n", m_entries[i].count, class_getName (objc_classes.GetClassAtIndex(m_entries[i].idx))); + } + } + } +private: + struct Entry + { + uint32_t idx; // Index into the m_objc_class_ptrs[] array + uint32_t count; // Number of object instances that were found + uint64_t bytes; // Total number of bytes for each objc class + }; + + static int + compare_bytes (const Entry *a, const Entry *b) + { + // Reverse the comparison to most bytes entries end up at top of list + if (a->bytes > b->bytes) return -1; + if (a->bytes < b->bytes) return +1; + return 0; + } + + static int + compare_count (const Entry *a, const Entry *b) + { + // Reverse the comparison to most count entries end up at top of list + if (a->count > b->count) return -1; + if (a->count < b->count) return +1; + return 0; + } + + Entry *m_entries; + uint32_t m_size; + HeapInfoSortType m_sort_type; +}; + +ObjCClassInfo g_objc_class_snapshot; + +//---------------------------------------------------------------------- +// task_peek +// +// Reads memory from this tasks address space. This callback is needed +// by the code that iterates through all of the malloc blocks to read +// the memory in this process. +//---------------------------------------------------------------------- +static kern_return_t +task_peek (task_t task, vm_address_t remote_address, vm_size_t size, void **local_memory) +{ + *local_memory = (void*) remote_address; + return KERN_SUCCESS; +} + + +static const void +foreach_zone_in_this_process (range_callback_info_t *info) +{ + if (info == NULL || info->zone_callback == NULL) + return; + + vm_address_t *zones = NULL; + unsigned int num_zones = 0; + + kern_return_t err = malloc_get_all_zones (0, task_peek, &zones, &num_zones); + if (KERN_SUCCESS == err) + { + for (unsigned int i=0; i<num_zones; ++i) + { + info->zone_callback (info, (const malloc_zone_t *)zones[i]); + } + } + + if (info->check_vm_regions) + { +#if defined (VM_REGION_SUBMAP_SHORT_INFO_COUNT_64) + typedef vm_region_submap_short_info_data_64_t RegionInfo; + enum { kRegionInfoSize = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64 }; +#else + typedef vm_region_submap_info_data_64_t RegionInfo; + enum { kRegionInfoSize = VM_REGION_SUBMAP_INFO_COUNT_64 }; +#endif + task_t task = mach_task_self(); + mach_vm_address_t vm_region_base_addr; + mach_vm_size_t vm_region_size; + natural_t vm_region_depth; + RegionInfo vm_region_info; + + ((range_contains_data_callback_info_t *)info->baton)->unique = true; + + for (vm_region_base_addr = 0, vm_region_size = 1; vm_region_size != 0; vm_region_base_addr += vm_region_size) + { + mach_msg_type_number_t vm_region_info_size = kRegionInfoSize; + const kern_return_t err = mach_vm_region_recurse (task, + &vm_region_base_addr, + &vm_region_size, + &vm_region_depth, + (vm_region_recurse_info_t)&vm_region_info, + &vm_region_info_size); + if (err) + break; + // Check all read + write regions. This will cover the thread stacks + // and any regions of memory that aren't covered by the heap + if (vm_region_info.protection & VM_PROT_WRITE && + vm_region_info.protection & VM_PROT_READ) + { + //printf ("checking vm_region: [0x%16.16llx - 0x%16.16llx)\n", (uint64_t)vm_region_base_addr, (uint64_t)vm_region_base_addr + vm_region_size); + range_info_callback (task, + info->baton, + stack_logging_type_vm_region, + vm_region_base_addr, + vm_region_size); + } + } + } +} + +//---------------------------------------------------------------------- +// dump_malloc_block_callback +// +// A simple callback that will dump each malloc block and all available +// info from the enumeration callback perspective. +//---------------------------------------------------------------------- +static void +dump_malloc_block_callback (task_t task, void *baton, unsigned type, uint64_t ptr_addr, uint64_t ptr_size) +{ + printf ("task = 0x%4.4x: baton = %p, type = %u, ptr_addr = 0x%llx + 0x%llu\n", task, baton, type, ptr_addr, ptr_size); +} + +static void +ranges_callback (task_t task, void *baton, unsigned type, vm_range_t *ptrs, unsigned count) +{ + range_callback_info_t *info = (range_callback_info_t *)baton; + while(count--) { + info->range_callback (task, info->baton, type, ptrs->address, ptrs->size); + ptrs++; + } +} + +static void +enumerate_range_in_zone (void *baton, const malloc_zone_t *zone) +{ + range_callback_info_t *info = (range_callback_info_t *)baton; + + if (zone && zone->introspect) + zone->introspect->enumerator (mach_task_self(), + info, + MALLOC_PTR_IN_USE_RANGE_TYPE, + (vm_address_t)zone, + task_peek, + ranges_callback); +} + +static void +range_info_callback (task_t task, void *baton, unsigned type, uint64_t ptr_addr, uint64_t ptr_size) +{ + const uint64_t end_addr = ptr_addr + ptr_size; + + range_contains_data_callback_info_t *info = (range_contains_data_callback_info_t *)baton; + switch (info->type) + { + case eDataTypeAddress: + // Check if the current malloc block contains an address specified by "info->addr" + if (ptr_addr <= info->addr && info->addr < end_addr) + { + ++info->match_count; + malloc_match match = { (void *)ptr_addr, ptr_size, info->addr - ptr_addr, type }; + g_matches.push_back(match, info->unique); + } + break; + + case eDataTypeContainsData: + // Check if the current malloc block contains data specified in "info->data" + { + const uint32_t size = info->data.size; + if (size < ptr_size) // Make sure this block can contain this data + { + uint8_t *ptr_data = NULL; + if (task_peek (task, ptr_addr, ptr_size, (void **)&ptr_data) == KERN_SUCCESS) + { + const void *buffer = info->data.buffer; + assert (ptr_data); + const uint32_t align = info->data.align; + for (uint64_t addr = ptr_addr; + addr < end_addr && ((end_addr - addr) >= size); + addr += align, ptr_data += align) + { + if (memcmp (buffer, ptr_data, size) == 0) + { + ++info->match_count; + malloc_match match = { (void *)ptr_addr, ptr_size, addr - ptr_addr, type }; + g_matches.push_back(match, info->unique); + } + } + } + else + { + printf ("0x%llx: error: couldn't read %llu bytes\n", ptr_addr, ptr_size); + } + } + } + break; + + case eDataTypeObjC: + // Check if the current malloc block contains an objective C object + // of any sort where the first pointer in the object is an OBJC class + // pointer (an isa) + { + malloc_block_contents *block_contents = NULL; + if (task_peek (task, ptr_addr, sizeof(void *), (void **)&block_contents) == KERN_SUCCESS) + { + // We assume that g_objc_classes is up to date + // that the class list was verified to have some classes in it + // before calling this function + const uint32_t objc_class_idx = g_objc_classes.FindClassIndex (block_contents->isa); + if (objc_class_idx != UINT32_MAX) + { + bool match = false; + if (info->objc.match_isa == 0) + { + // Match any objective C object + match = true; + } + else + { + // Only match exact isa values in the current class or + // optionally in the super classes + if (info->objc.match_isa == block_contents->isa) + match = true; + else if (info->objc.match_superclasses) + { + Class super = class_getSuperclass(block_contents->isa); + while (super) + { + match = super == info->objc.match_isa; + if (match) + break; + super = class_getSuperclass(super); + } + } + } + if (match) + { + //printf (" success\n"); + ++info->match_count; + malloc_match match = { (void *)ptr_addr, ptr_size, 0, type }; + g_matches.push_back(match, info->unique); + } + else + { + //printf (" error: wrong class: %s\n", dl_info.dli_sname); + } + } + else + { + //printf ("\terror: symbol not objc class: %s\n", dl_info.dli_sname); + return; + } + } + } + break; + + case eDataTypeHeapInfo: + // Check if the current malloc block contains an objective C object + // of any sort where the first pointer in the object is an OBJC class + // pointer (an isa) + { + malloc_block_contents *block_contents = NULL; + if (task_peek (task, ptr_addr, sizeof(void *), (void **)&block_contents) == KERN_SUCCESS) + { + // We assume that g_objc_classes is up to date + // that the class list was verified to have some classes in it + // before calling this function + const uint32_t objc_class_idx = g_objc_classes.FindClassIndex (block_contents->isa); + if (objc_class_idx != UINT32_MAX) + { + // This is an objective C object + g_objc_class_snapshot.AddInstance (objc_class_idx, ptr_size); + } + else + { + // Classify other heap info + } + } + } + break; + + } +} + +static void +get_stack_for_address_enumerator(mach_stack_logging_record_t stack_record, void *task_ptr) +{ + malloc_stack_entry *stack_entry = g_malloc_stack_history.next(); + if (stack_entry) + { + stack_entry->address = (void *)stack_record.address; + stack_entry->type_flags = stack_record.type_flags; + stack_entry->argument = stack_record.argument; + stack_entry->num_frames = 0; + stack_entry->frames[0] = 0; + kern_return_t err = __mach_stack_logging_frames_for_uniqued_stack (*(task_t *)task_ptr, + stack_record.stack_identifier, + stack_entry->frames, + MAX_FRAMES, + &stack_entry->num_frames); + // Terminate the frames with zero if there is room + if (stack_entry->num_frames < MAX_FRAMES) + stack_entry->frames[stack_entry->num_frames] = 0; + } +} + +malloc_stack_entry * +get_stack_history_for_address (const void * addr, int history) +{ + if (!stack_logging_enable_logging) + return NULL; + g_malloc_stack_history.clear(); + kern_return_t err; + task_t task = mach_task_self(); + if (history) + { + err = __mach_stack_logging_enumerate_records (task, + (mach_vm_address_t)addr, + get_stack_for_address_enumerator, + &task); + } + else + { + malloc_stack_entry *stack_entry = g_malloc_stack_history.next(); + if (stack_entry) + { + stack_entry->address = addr; + stack_entry->type_flags = stack_logging_type_alloc; + stack_entry->argument = 0; + stack_entry->num_frames = 0; + stack_entry->frames[0] = 0; + err = __mach_stack_logging_get_frames(task, (mach_vm_address_t)addr, stack_entry->frames, MAX_FRAMES, &stack_entry->num_frames); + if (err == 0 && stack_entry->num_frames > 0) + { + // Terminate the frames with zero if there is room + if (stack_entry->num_frames < MAX_FRAMES) + stack_entry->frames[stack_entry->num_frames] = 0; + } + else + { + g_malloc_stack_history.clear(); + } + } + } + // Return data if there is any + return g_malloc_stack_history.data(); +} + +//---------------------------------------------------------------------- +// find_pointer_in_heap +// +// Finds a pointer value inside one or more currently valid malloc +// blocks. +//---------------------------------------------------------------------- +malloc_match * +find_pointer_in_heap (const void * addr, int check_vm_regions) +{ + g_matches.clear(); + // Setup "info" to look for a malloc block that contains data + // that is the pointer + if (addr) + { + range_contains_data_callback_info_t data_info; + data_info.type = eDataTypeContainsData; // Check each block for data + data_info.data.buffer = (uint8_t *)&addr; // What data? The pointer value passed in + data_info.data.size = sizeof(addr); // How many bytes? The byte size of a pointer + data_info.data.align = sizeof(addr); // Align to a pointer byte size + data_info.match_count = 0; // Initialize the match count to zero + data_info.done = false; // Set done to false so searching doesn't stop + data_info.unique = false; // Set to true when iterating on the vm_regions + range_callback_info_t info = { enumerate_range_in_zone, range_info_callback, &data_info, check_vm_regions }; + foreach_zone_in_this_process (&info); + + + } + return g_matches.data(); +} + +//---------------------------------------------------------------------- +// find_pointer_in_memory +// +// Finds a pointer value inside one or more currently valid malloc +// blocks. +//---------------------------------------------------------------------- +malloc_match * +find_pointer_in_memory (uint64_t memory_addr, uint64_t memory_size, const void * addr) +{ + g_matches.clear(); + // Setup "info" to look for a malloc block that contains data + // that is the pointer + range_contains_data_callback_info_t data_info; + data_info.type = eDataTypeContainsData; // Check each block for data + data_info.data.buffer = (uint8_t *)&addr; // What data? The pointer value passed in + data_info.data.size = sizeof(addr); // How many bytes? The byte size of a pointer + data_info.data.align = sizeof(addr); // Align to a pointer byte size + data_info.match_count = 0; // Initialize the match count to zero + data_info.done = false; // Set done to false so searching doesn't stop + data_info.unique = false; // Set to true when iterating on the vm_regions + range_info_callback (mach_task_self(), &data_info, stack_logging_type_generic, memory_addr, memory_size); + return g_matches.data(); +} + +//---------------------------------------------------------------------- +// find_objc_objects_in_memory +// +// Find all instances of ObjC classes 'c', or all ObjC classes if 'c' is +// NULL. If 'c' is non NULL, then also check objects to see if they +// inherit from 'c' +//---------------------------------------------------------------------- +malloc_match * +find_objc_objects_in_memory (void *isa, int check_vm_regions) +{ + g_matches.clear(); + if (g_objc_classes.Update()) + { + // Setup "info" to look for a malloc block that contains data + // that is the pointer + range_contains_data_callback_info_t data_info; + data_info.type = eDataTypeObjC; // Check each block for data + data_info.objc.match_isa = isa; + data_info.objc.match_superclasses = true; + data_info.match_count = 0; // Initialize the match count to zero + data_info.done = false; // Set done to false so searching doesn't stop + data_info.unique = false; // Set to true when iterating on the vm_regions + range_callback_info_t info = { enumerate_range_in_zone, range_info_callback, &data_info, check_vm_regions }; + foreach_zone_in_this_process (&info); + } + return g_matches.data(); +} + +//---------------------------------------------------------------------- +// get_heap_info +// +// Gather information for all allocations on the heap and report +// statistics. +//---------------------------------------------------------------------- + +void +get_heap_info (int sort_type) +{ + if (g_objc_classes.Update()) + { + // Reset all stats + g_objc_class_snapshot.Reset (); + // Setup "info" to look for a malloc block that contains data + // that is the pointer + range_contains_data_callback_info_t data_info; + data_info.type = eDataTypeHeapInfo; // Check each block for data + data_info.match_count = 0; // Initialize the match count to zero + data_info.done = false; // Set done to false so searching doesn't stop + data_info.unique = false; // Set to true when iterating on the vm_regions + const int check_vm_regions = false; + range_callback_info_t info = { enumerate_range_in_zone, range_info_callback, &data_info, check_vm_regions }; + foreach_zone_in_this_process (&info); + + // Sort and print byte total bytes + switch (sort_type) + { + case eSortTypeNone: + default: + case eSortTypeBytes: + g_objc_class_snapshot.SortByTotalBytes(g_objc_classes, true); + break; + + case eSortTypeCount: + g_objc_class_snapshot.SortByTotalCount(g_objc_classes, true); + break; + } + } + else + { + printf ("error: no objective C classes\n"); + } +} + +//---------------------------------------------------------------------- +// find_cstring_in_heap +// +// Finds a C string inside one or more currently valid malloc blocks. +//---------------------------------------------------------------------- +malloc_match * +find_cstring_in_heap (const char *s, int check_vm_regions) +{ + g_matches.clear(); + if (s == NULL || s[0] == '\0') + { + printf ("error: invalid argument (empty cstring)\n"); + return NULL; + } + // Setup "info" to look for a malloc block that contains data + // that is the C string passed in aligned on a 1 byte boundary + range_contains_data_callback_info_t data_info; + data_info.type = eDataTypeContainsData; // Check each block for data + data_info.data.buffer = (uint8_t *)s; // What data? The C string passed in + data_info.data.size = strlen(s); // How many bytes? The length of the C string + data_info.data.align = 1; // Data doesn't need to be aligned, so set the alignment to 1 + data_info.match_count = 0; // Initialize the match count to zero + data_info.done = false; // Set done to false so searching doesn't stop + data_info.unique = false; // Set to true when iterating on the vm_regions + range_callback_info_t info = { enumerate_range_in_zone, range_info_callback, &data_info, check_vm_regions }; + foreach_zone_in_this_process (&info); + return g_matches.data(); +} + +//---------------------------------------------------------------------- +// find_block_for_address +// +// Find the malloc block that whose address range contains "addr". +//---------------------------------------------------------------------- +malloc_match * +find_block_for_address (const void *addr, int check_vm_regions) +{ + g_matches.clear(); + // Setup "info" to look for a malloc block that contains data + // that is the C string passed in aligned on a 1 byte boundary + range_contains_data_callback_info_t data_info; + data_info.type = eDataTypeAddress; // Check each block to see if the block contains the address passed in + data_info.addr = (uintptr_t)addr; // What data? The C string passed in + data_info.match_count = 0; // Initialize the match count to zero + data_info.done = false; // Set done to false so searching doesn't stop + data_info.unique = false; // Set to true when iterating on the vm_regions + range_callback_info_t info = { enumerate_range_in_zone, range_info_callback, &data_info, check_vm_regions }; + foreach_zone_in_this_process (&info); + return g_matches.data(); +} diff --git a/examples/functions/Makefile b/examples/functions/Makefile new file mode 100644 index 000000000000..64ea6e75b332 --- /dev/null +++ b/examples/functions/Makefile @@ -0,0 +1,18 @@ +LEVEL = ../../test/make + +CXX_SOURCES := main.cpp + +EXE := lldb-functions +USE_LIBCPP := 1 + +MY_OS = $(shell uname -s) + +ifeq "$(MY_OS)" "Darwin" + LLDB_BUILD_DIR ?= /Applications/Xcode.app/Contents/SharedFrameworks + LD_EXTRAS ?= -framework LLDB -Wl,-rpath,"$(LLDB_BUILD_DIR)" + FRAMEWORK_INCLUDES=-F"$(LLDB_BUILD_DIR)" +else + LD_EXTRAS ?= $(LLDB_BUILD_DIR)/_lldb.so +endif + +include $(LEVEL)/Makefile.rules diff --git a/examples/functions/main.cpp b/examples/functions/main.cpp new file mode 100644 index 000000000000..4381098e3305 --- /dev/null +++ b/examples/functions/main.cpp @@ -0,0 +1,364 @@ +//===-- main.cpp ------------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include <getopt.h> +#include <stdint.h> +#include <stdlib.h> + +#if defined(__APPLE__) +#include <LLDB/LLDB.h> +#else +#include "LLDB/SBBlock.h" +#include "LLDB/SBCompileUnit.h" +#include "LLDB/SBDebugger.h" +#include "LLDB/SBFunction.h" +#include "LLDB/SBModule.h" +#include "LLDB/SBStream.h" +#include "LLDB/SBSymbol.h" +#include "LLDB/SBTarget.h" +#include "LLDB/SBThread.h" +#include "LLDB/SBProcess.h" +#endif + +#include <string> + +using namespace lldb; + +//---------------------------------------------------------------------- +// This quick sample code shows how to create a debugger instance and +// create an executable target without adding dependent shared +// libraries. It will then set a regular expression breakpoint to get +// breakpoint locations for all functions in the module, and use the +// locations to extract the symbol context for each location. Then it +// dumps all // information about the function: its name, file address +// range, the return type (if any), and all argument types. +// +// To build the program, type (while in this directory): +// +// $ make +// +// then to run this on MacOSX, specify the path to your LLDB.framework +// library using the DYLD_FRAMEWORK_PATH option and run the executable +// +// $ DYLD_FRAMEWORK_PATH=/Volumes/data/lldb/tot/build/Debug ./a.out executable_path1 [executable_path2 ...] +//---------------------------------------------------------------------- +class LLDBSentry +{ +public: + LLDBSentry() { + // Initialize LLDB + SBDebugger::Initialize(); + } + ~LLDBSentry() { + // Terminate LLDB + SBDebugger::Terminate(); + } +}; + +static struct option g_long_options[] = +{ + { "arch", required_argument, NULL, 'a' }, + { "canonical", no_argument, NULL, 'c' }, + { "extern", no_argument, NULL, 'x' }, + { "help", no_argument, NULL, 'h' }, + { "platform", required_argument, NULL, 'p' }, + { "verbose", no_argument, NULL, 'v' }, + { NULL, 0, NULL, 0 } +}; + +#define PROGRAM_NAME "lldb-functions" +void +usage () +{ + puts ( + "NAME\n" + " " PROGRAM_NAME " -- extract all function signatures from one or more binaries.\n" + "\n" + "SYNOPSIS\n" + " " PROGRAM_NAME " [[--arch=<ARCH>] [--platform=<PLATFORM>] [--verbose] [--help] [--canonical] --] <PATH> [<PATH>....]\n" + "\n" + "DESCRIPTION\n" + " Loads the executable pointed to by <PATH> and dumps complete signatures for all functions that have debug information.\n" + "\n" + "EXAMPLE\n" + " " PROGRAM_NAME " --arch=x86_64 /usr/lib/dyld\n" + ); + exit(0); +} +int +main (int argc, char const *argv[]) +{ + // Use a sentry object to properly initialize/terminate LLDB. + LLDBSentry sentry; + + SBDebugger debugger (SBDebugger::Create()); + + // Create a debugger instance so we can create a target + if (!debugger.IsValid()) + fprintf (stderr, "error: failed to create a debugger object\n"); + + bool show_usage = false; + bool verbose = false; + bool canonical = false; + bool external_only = false; + const char *arch = NULL; + const char *platform = NULL; + std::string short_options("h?"); + for (const struct option *opt = g_long_options; opt->name; ++opt) + { + if (isprint(opt->val)) + { + short_options.append(1, (char)opt->val); + switch (opt->has_arg) + { + case no_argument: + break; + case required_argument: + short_options.append(1, ':'); + break; + case optional_argument: + short_options.append(2, ':'); + break; + } + } + } +#ifdef __GLIBC__ + optind = 0; +#else + optreset = 1; + optind = 1; +#endif + char ch; + while ((ch = getopt_long_only(argc, (char * const *)argv, short_options.c_str(), g_long_options, 0)) != -1) + { + switch (ch) + { + case 0: + break; + + case 'a': + if (arch != NULL) + { + fprintf (stderr, "error: the --arch option can only be specified once\n"); + exit(1); + } + arch = optarg; + break; + + case 'c': + canonical = true; + break; + + case 'x': + external_only = true; + break; + + case 'p': + platform = optarg; + break; + + case 'v': + verbose = true; + break; + + case 'h': + case '?': + default: + show_usage = true; + break; + } + } + argc -= optind; + argv += optind; + + const bool add_dependent_libs = false; + SBError error; + for (int arg_idx = 0; arg_idx < argc; ++arg_idx) + { + // The first argument is the file path we want to look something up in + const char *exe_file_path = argv[arg_idx]; + + // Create a target using the executable. + SBTarget target = debugger.CreateTarget (exe_file_path, + arch, + platform, + add_dependent_libs, + error); + + if (error.Success()) + { + if (target.IsValid()) + { + SBFileSpec exe_file_spec (exe_file_path, true); + SBModule module (target.FindModule (exe_file_spec)); + SBFileSpecList comp_unit_list; + + if (module.IsValid()) + { + char command[1024]; + lldb::SBCommandReturnObject command_result; + snprintf (command, sizeof(command), "add-dsym --uuid %s", module.GetUUIDString()); + debugger.GetCommandInterpreter().HandleCommand (command, command_result); + if (!command_result.Succeeded()) + { + fprintf (stderr, "error: couldn't locate debug symbols for '%s'\n", exe_file_path); + exit(1); + } + + SBFileSpecList module_list; + module_list.Append(exe_file_spec); + SBBreakpoint bp = target.BreakpointCreateByRegex (".", module_list, comp_unit_list); + + const size_t num_locations = bp.GetNumLocations(); + for (uint32_t bp_loc_idx=0; bp_loc_idx<num_locations; ++bp_loc_idx) + { + SBBreakpointLocation bp_loc = bp.GetLocationAtIndex(bp_loc_idx); + SBSymbolContext sc (bp_loc.GetAddress().GetSymbolContext(eSymbolContextEverything)); + if (sc.IsValid()) + { + if (sc.GetBlock().GetContainingInlinedBlock().IsValid()) + { + // Skip inlined functions + continue; + } + SBFunction function (sc.GetFunction()); + if (function.IsValid()) + { + addr_t lo_pc = function.GetStartAddress().GetFileAddress(); + if (lo_pc == LLDB_INVALID_ADDRESS) + { + // Skip functions that don't have concrete instances in the binary + continue; + } + addr_t hi_pc = function.GetEndAddress().GetFileAddress(); + const char *func_demangled_name = function.GetName(); + const char *func_mangled_name = function.GetMangledName(); + + bool dump = true; + const bool is_objc_method = ((func_demangled_name[0] == '-') || (func_demangled_name[0] == '+')) && (func_demangled_name[1] == '['); + if (external_only) + { + // Dump all objective C methods, or external symbols + dump = is_objc_method; + if (!dump) + dump = sc.GetSymbol().IsExternal(); + } + + if (dump) + { + if (verbose) + { + printf ("\n name: %s\n", func_demangled_name); + if (func_mangled_name) + printf ("mangled: %s\n", func_mangled_name); + printf (" range: [0x%16.16llx - 0x%16.16llx)\n type: ", lo_pc, hi_pc); + } + else + { + printf ("[0x%16.16llx - 0x%16.16llx) ", lo_pc, hi_pc); + } + SBType function_type = function.GetType(); + SBType return_type = function_type.GetFunctionReturnType(); + + if (canonical) + return_type = return_type.GetCanonicalType(); + + if (func_mangled_name && + func_mangled_name[0] == '_' && + func_mangled_name[1] == 'Z') + { + printf ("%s %s\n", return_type.GetName(), func_demangled_name); + } + else + { + SBTypeList function_args = function_type.GetFunctionArgumentTypes(); + const size_t num_function_args = function_args.GetSize(); + + if (is_objc_method) + { + const char *class_name_start = func_demangled_name + 2; + + if (num_function_args == 0) + { + printf("%c(%s)[%s\n", func_demangled_name[0], return_type.GetName(), class_name_start); + } + else + { + const char *class_name_end = strchr(class_name_start,' '); + const int class_name_len = class_name_end - class_name_start; + printf ("%c(%s)[%*.*s", func_demangled_name[0], return_type.GetName(), class_name_len, class_name_len, class_name_start); + + const char *selector_pos = class_name_end + 1; + for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args; ++function_arg_idx) + { + const char *selector_end = strchr(selector_pos, ':') + 1; + const int selector_len = selector_end - selector_pos; + SBType function_arg_type = function_args.GetTypeAtIndex(function_arg_idx); + + if (canonical) + function_arg_type = function_arg_type.GetCanonicalType(); + + printf (" %*.*s", selector_len, selector_len, selector_pos); + if (function_arg_type.IsValid()) + { + printf ("(%s)", function_arg_type.GetName()); + } + else + { + printf ("(?)"); + } + selector_pos = selector_end; + } + printf ("]\n"); + } + } + else + { + printf ("%s ", return_type.GetName()); + if (strchr (func_demangled_name, '(')) + printf ("(*)("); + else + printf ("%s(", func_demangled_name); + + for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args; ++function_arg_idx) + { + SBType function_arg_type = function_args.GetTypeAtIndex(function_arg_idx); + + if (canonical) + function_arg_type = function_arg_type.GetCanonicalType(); + + if (function_arg_type.IsValid()) + { + printf ("%s%s", function_arg_idx > 0 ? ", " : "", function_arg_type.GetName()); + } + else + { + printf ("%s???", function_arg_idx > 0 ? ", " : ""); + } + } + printf (")\n"); + } + } + } + } + } + } + } + } + } + else + { + fprintf (stderr, "error: %s\n", error.GetCString()); + exit(1); + } + } + + return 0; +} + diff --git a/examples/interposing/darwin/fd_interposing/FDInterposing.cpp b/examples/interposing/darwin/fd_interposing/FDInterposing.cpp new file mode 100644 index 000000000000..e41dc34038ba --- /dev/null +++ b/examples/interposing/darwin/fd_interposing/FDInterposing.cpp @@ -0,0 +1,1152 @@ +//===-- FDInterposing.cpp ---------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file helps with catching double close calls on unix integer file +// descriptors by interposing functions for all file descriptor create and +// close operations. A stack backtrace for every create and close function is +// maintained, and every create and close operation is logged. When a double +// file descriptor close is encountered, it will be logged. +// +// To enable the interposing in a darwin program, set the DYLD_INSERT_LIBRARIES +// environment variable as follows: +// For sh: +// DYLD_INSERT_LIBRARIES=/path/to/FDInterposing.dylib /path/to/executable +// For tcsh: +// (setenv DYLD_INSERT_LIBRARIES=/path/to/FDInterposing.dylib ; /path/to/executable) +// +// Other environment variables that can alter the default actions of this +// interposing shared library include: +// +// "FileDescriptorStackLoggingNoCompact" +// +// With this environment variable set, all file descriptor create and +// delete operations will be permanantly maintained in the event map. +// The default action is to compact the create/delete events by removing +// any previous file descriptor create events that are matched with a +// corresponding file descriptor delete event when the next valid file +// descriptor create event is detected. +// +// "FileDescriptorMinimalLogging" +// +// By default every file descriptor create and delete operation is logged +// (to STDOUT by default, see the "FileDescriptorLogFile"). This can be +// suppressed to only show errors and warnings by setting this environment +// variable (the value in not important). +// +// "FileDescriptorLogFile=<path>" +// +// By default logging goes to STDOUT_FILENO, but this can be changed by +// setting FileDescriptorLogFile. The value is a path to a file that +// will be opened and used for logging. +//===----------------------------------------------------------------------===// + +#include <assert.h> +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <execinfo.h> +#include <libgen.h> +#include <mach-o/dyld.h> +#include <mach-o/dyld-interposing.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/event.h> +#include <sys/mman.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <sys/time.h> +#include <tr1/memory> // for std::tr1::shared_ptr +#include <unistd.h> +#include <string> +#include <vector> +#include <map> + +//---------------------------------------------------------------------- +/// @def DISALLOW_COPY_AND_ASSIGN(TypeName) +/// Macro definition for easily disallowing copy constructor and +/// assignment operators in C++ classes. +//---------------------------------------------------------------------- +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ +TypeName(const TypeName&); \ +const TypeName& operator=(const TypeName&) + +extern "C" { + int accept$NOCANCEL (int, struct sockaddr * __restrict, socklen_t * __restrict); + int close$NOCANCEL(int); + int open$NOCANCEL(const char *, int, ...); + int __open_extended(const char *, int, uid_t, gid_t, int, struct kauth_filesec *); +} + +namespace fd_interposing { + +//---------------------------------------------------------------------- +// String class so we can get formatted strings without having to worry +// about the memory storage since it will allocate the memory it needs. +//---------------------------------------------------------------------- +class String +{ +public: + String () : + m_str (NULL) + {} + + String (const char *format, ...) : + m_str (NULL) + { + va_list args; + va_start (args, format); + vprintf (format, args); + va_end (args); + } + + ~String() + { + reset(); + } + + void + reset (char *s = NULL) + { + if (m_str) + ::free (m_str); + m_str = s; + } + + const char * + c_str () const + { + return m_str; + } + + void + printf (const char *format, ...) + { + va_list args; + va_start (args, format); + vprintf (format, args); + va_end (args); + } + void + vprintf (const char *format, va_list args) + { + reset(); + ::vasprintf (&m_str, format, args); + } + + void + log (int log_fd) + { + if (m_str && log_fd >= 0) + { + const int len = strlen(m_str); + if (len > 0) + { + write (log_fd, m_str, len); + const char last_char = m_str[len-1]; + if (!(last_char == '\n' || last_char == '\r')) + write (log_fd, "\n", 1); + } + } + } +protected: + char *m_str; + +private: + DISALLOW_COPY_AND_ASSIGN (String); +}; + +//---------------------------------------------------------------------- +// Type definitions +//---------------------------------------------------------------------- +typedef std::vector<void *> Frames; +class FDEvent; +typedef std::vector<void *> Frames; +typedef std::tr1::shared_ptr<FDEvent> FDEventSP; +typedef std::tr1::shared_ptr<String> StringSP; + + +//---------------------------------------------------------------------- +// FDEvent +// +// A class that describes a file desciptor event. +// +// File descriptor events fall into one of two categories: create events +// and delete events. +//---------------------------------------------------------------------- +class FDEvent +{ +public: + FDEvent (int fd, int err, const StringSP &string_sp, bool is_create, const Frames& frames) : + m_string_sp (string_sp), + m_frames (frames.begin(), frames.end()), + m_fd (fd), + m_err (err), + m_is_create (is_create) + {} + + ~FDEvent () {} + + bool + IsCreateEvent() const + { + return m_is_create; + } + + bool + IsDeleteEvent() const + { + return !m_is_create; + } + + Frames & + GetFrames () + { + return m_frames; + } + + const Frames & + GetFrames () const + { + return m_frames; + } + + int + GetFD () const + { + return m_fd; + } + + int + GetError () const + { + return m_err; + } + + void + Dump (int log_fd) const; + + void + SetCreateEvent (FDEventSP &create_event_sp) + { + m_create_event_sp = create_event_sp; + } + +private: + // A shared pointer to a String that describes this event in + // detail (all args and return and error values) + StringSP m_string_sp; + // The frames for the stack backtrace for this event + Frames m_frames; + // If this is a file descriptor delete event, this might contain + // the correspoding file descriptor create event + FDEventSP m_create_event_sp; + // The file descriptor for this event + int m_fd; + // The error code (if any) for this event + int m_err; + // True if this event is a file descriptor create event, false + // if it is a file descriptor delete event + bool m_is_create; +}; + +//---------------------------------------------------------------------- +// Templatized class that will save errno only if the "value" it is +// constructed with is equal to INVALID. When the class goes out of +// scope, it will restore errno if it was saved. +//---------------------------------------------------------------------- +template <int INVALID> +class Errno +{ +public: + // Save errno only if we are supposed to + Errno (int value) : + m_saved_errno ((value == INVALID) ? errno : 0), + m_restore (value == INVALID) + { + } + + // Restore errno only if we are supposed to + ~Errno() + { + if (m_restore) + errno = m_saved_errno; + } + + // Accessor for the saved value of errno + int + get_errno() const + { + return m_saved_errno; + } + +protected: + const int m_saved_errno; + const bool m_restore; +}; + +typedef Errno<-1> InvalidFDErrno; +typedef Errno<-1> NegativeErrorErrno; +typedef std::vector<FDEventSP> FDEventArray; +typedef std::map<int, FDEventArray> FDEventMap; + +//---------------------------------------------------------------------- +// Globals +//---------------------------------------------------------------------- +// Global event map that contains all file descriptor events. As file +// descriptor create and close events come in, they will get filled +// into this map (protected by g_mutex). When a file descriptor close +// event is detected, the open event will be removed and placed into +// the close event so if something tries to double close a file +// descriptor we can show the previous close event and the file +// desctiptor event that created it. When a new file descriptor create +// event comes in, we will remove the previous one for that file +// desctiptor unless the environment variable "FileDescriptorStackLoggingNoCompact" +// is set. The file desctiptor history can be accessed using the +// get_fd_history() function. +static FDEventMap g_fd_event_map; +// A mutex to protect access to our data structures in g_fd_event_map +// and also our logging messages +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +// Log all file descriptor create and close events by default. Only log +// warnings and erros if the "FileDescriptorMinimalLogging" environment +// variable is set. +static int g_log_all_calls = 1; +// We compact the file descriptor events by default. Set the environment +// varible "FileDescriptorStackLoggingNoCompact" to keep a full history. +static int g_compact = 1; +// The current process ID +static int g_pid = -1; +static bool g_enabled = true; +//---------------------------------------------------------------------- +// Mutex class that will lock a mutex when it is constructed, and unlock +// it when is goes out of scope +//---------------------------------------------------------------------- +class Locker +{ +public: + Locker (pthread_mutex_t *mutex_ptr) : + m_mutex_ptr(mutex_ptr) + { + ::pthread_mutex_lock (m_mutex_ptr); + } + + // This allows clients to test try and acquire the mutex... + Locker (pthread_mutex_t *mutex_ptr, bool &lock_acquired) : + m_mutex_ptr(NULL) + { + lock_acquired = ::pthread_mutex_trylock(mutex_ptr) == 0; + if (lock_acquired) + m_mutex_ptr = mutex_ptr; + } + + ~Locker () + { + if (m_mutex_ptr) + ::pthread_mutex_unlock (m_mutex_ptr); + } +protected: + pthread_mutex_t *m_mutex_ptr; +}; + +static void +log (const char *format, ...) __attribute__ ((format (printf, 1, 2))); + +static void +log (int log_fd, const FDEvent *event, const char *format, ...) __attribute__ ((format (printf, 3, 4))); + +static void +backtrace_log (const char *format, ...) __attribute__ ((format (printf, 1, 2))); + +static void +backtrace_error (const char *format, ...) __attribute__ ((format (printf, 1, 2))); + +static void +log_to_fd (int log_fd, const char *format, ...) __attribute__ ((format (printf, 2, 3))); + +static inline size_t +get_backtrace (Frames &frame_buffer, size_t frames_to_remove) +{ + void *frames[2048]; + int count = ::backtrace (&frames[0], sizeof(frames)/sizeof(void*)); + if (count > frames_to_remove) + frame_buffer.assign (&frames[frames_to_remove], &frames[count]); + else + frame_buffer.assign (&frames[0], &frames[count]); + while (frame_buffer.back() < (void *)1024) + frame_buffer.pop_back(); + return frame_buffer.size(); +} + +static int g_log_fd = STDOUT_FILENO; +static int g_initialized = 0; + +const char * +get_process_fullpath (bool force = false) +{ + static char g_process_fullpath[PATH_MAX] = {0}; + if (force || g_process_fullpath[0] == '\0') + { + // If DST is NULL, then return the number of bytes needed. + uint32_t len = sizeof(g_process_fullpath); + if (_NSGetExecutablePath (g_process_fullpath, &len) != 0) + strncpy (g_process_fullpath, "<error>", sizeof(g_process_fullpath)); + } + return g_process_fullpath; +} + +// Returns the current process ID, or -1 if inserposing not enabled for +// this process +static int +get_interposed_pid() +{ + if (!g_enabled) + return -1; + + const pid_t pid = getpid(); + if (g_pid != pid) + { + if (g_pid == -1) + { + g_pid = pid; + log ("Interposing file descriptor create and delete functions for %s (pid=%i)\n", get_process_fullpath (true), pid); + } + else + { + log ("pid=%i: disabling interposing file descriptor create and delete functions for child process %s (pid=%i)\n", g_pid, get_process_fullpath (true), pid); + g_enabled = false; + return -1; + } + // Log when our process changes + } + return g_pid; +} + +static int +get_logging_fd () +{ + if (!g_enabled) + return -1; + + if (!g_initialized) + { + g_initialized = 1; + + const pid_t pid = get_interposed_pid(); + + if (g_enabled) + { + // Keep all stack info around for all fd create and delete calls. + // Otherwise we will remove the fd create call when a corresponding + // fd delete call is received + if (getenv("FileDescriptorStackLoggingNoCompact")) + g_compact = 0; + + if (getenv("FileDescriptorMinimalLogging")) + g_log_all_calls = 0; + + const char *log_path = getenv ("FileDescriptorLogFile"); + if (log_path) + g_log_fd = ::creat (log_path, 0660); + else + g_log_fd = STDOUT_FILENO; + + // Only let this interposing happen on the first time this matches + // and stop this from happening so any child processes don't also + // log their file descriptors + ::unsetenv ("DYLD_INSERT_LIBRARIES"); + } + else + { + log ("pid=%i: logging disabled\n", getpid()); + } + } + return g_log_fd; +} + +void +log_to_fd (int log_fd, const char *format, va_list args) +{ + if (format && format[0] && log_fd >= 0) + { + char buffer[PATH_MAX]; + const int count = ::vsnprintf (buffer, sizeof(buffer), format, args); + if (count > 0) + write (log_fd, buffer, count); + } +} + +void +log_to_fd (int log_fd, const char *format, ...) +{ + if (format && format[0]) + { + va_list args; + va_start (args, format); + log_to_fd (log_fd, format, args); + va_end (args); + } +} + +void +log (const char *format, va_list args) +{ + log_to_fd (get_logging_fd (), format, args); +} + +void +log (const char *format, ...) +{ + if (format && format[0]) + { + va_list args; + va_start (args, format); + log (format, args); + va_end (args); + } +} + +void +log (int log_fd, const FDEvent *event, const char *format, ...) +{ + if (format && format[0]) + { + va_list args; + va_start (args, format); + log_to_fd (log_fd, format, args); + va_end (args); + } + if (event) + event->Dump(log_fd); +} + +void +FDEvent::Dump (int log_fd) const +{ + if (log_fd >= 0) + { + log_to_fd (log_fd, "%s\n", m_string_sp->c_str()); + if (!m_frames.empty()) + ::backtrace_symbols_fd (m_frames.data(), m_frames.size(), log_fd); + + if (m_create_event_sp) + { + log_to_fd (log_fd, "\nfd=%i was created with this event:\n", m_fd); + m_create_event_sp->Dump (log_fd); + log_to_fd (log_fd, "\n"); + } + } +} + + +void +backtrace_log (const char *format, ...) +{ + const int log_fd = get_logging_fd (); + if (log_fd >= 0) + { + if (format && format[0]) + { + va_list args; + va_start (args, format); + log (format, args); + va_end (args); + } + + Frames frames; + if (get_backtrace(frames, 2)) + ::backtrace_symbols_fd (frames.data(), frames.size(), log_fd); + } + +} + +void +backtrace_error (const char *format, ...) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + const int log_fd = get_logging_fd (); + if (log_fd >= 0) + { + log ("\nerror: %s (pid=%i): ", get_process_fullpath (), pid); + + if (format && format[0]) + { + va_list args; + va_start (args, format); + log (format, args); + va_end (args); + } + + Frames frames; + if (get_backtrace(frames, 2)) + ::backtrace_symbols_fd (frames.data(), frames.size(), log_fd); + } + } +} + +void +save_backtrace (int fd, int err, const StringSP &string_sp, bool is_create) +{ + Frames frames; + get_backtrace(frames, 2); + + FDEventSP fd_event_sp (new FDEvent (fd, err, string_sp, is_create, frames)); + + FDEventMap::iterator pos = g_fd_event_map.find (fd); + + if (pos != g_fd_event_map.end()) + { + // We have history for this fd... + + FDEventArray &event_array = g_fd_event_map[fd]; + if (fd_event_sp->IsCreateEvent()) + { + // The current fd event is a function that creates + // a descriptor, check in case last event was + // a create event. + if (event_array.back()->IsCreateEvent()) + { + const int log_fd = get_logging_fd(); + // Two fd create functions in a row, we missed + // a function that closes a fd... + log (log_fd, fd_event_sp.get(), "\nwarning: unmatched file descriptor create event fd=%i (we missed a file descriptor close event):\n", fd); + } + else if (g_compact) + { + // We are compacting so we remove previous create event + // when we get the correspinding delete event + event_array.pop_back(); + } + } + else + { + // The current fd event is a function that deletes + // a descriptor, check in case last event for this + // fd was a delete event (double close!) + if (event_array.back()->IsDeleteEvent()) + { + const int log_fd = get_logging_fd(); + // Two fd delete functions in a row, we must + // have missed some function that opened a descriptor + log (log_fd, fd_event_sp.get(), "\nwarning: unmatched file descriptor close event for fd=%d (we missed the file descriptor create event):\n", fd); + } + else if (g_compact) + { + // Since this is a close event, we want to remember the open event + // that this close if for... + fd_event_sp->SetCreateEvent(event_array.back()); + // We are compacting so we remove previous create event + // when we get the correspinding delete event + event_array.pop_back(); + } + } + + event_array.push_back(fd_event_sp); + } + else + { + g_fd_event_map[fd].push_back(fd_event_sp); + } +} + +//---------------------------------------------------------------------- +// socket() interpose function +//---------------------------------------------------------------------- +extern "C" int +socket$__interposed__ (int domain, int type, int protocol) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int fd = ::socket (domain, type, protocol); + InvalidFDErrno fd_errno(fd); + StringSP description_sp(new String); + if (fd == -1) + description_sp->printf("pid=%i: socket (domain = %i, type = %i, protocol = %i) => fd=%i errno = %i", pid, domain, type, protocol, fd, fd_errno.get_errno()); + else + description_sp->printf("pid=%i: socket (domain = %i, type = %i, protocol = %i) => fd=%i", pid, domain, type, protocol, fd); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::socket (domain, type, protocol); + } +} + +//---------------------------------------------------------------------- +// socketpair() interpose function +//---------------------------------------------------------------------- +extern "C" int +socketpair$__interposed__ (int domain, int type, int protocol, int fds[2]) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + fds[0] = -1; + fds[1] = -1; + const int err = socketpair (domain, type, protocol, fds); + NegativeErrorErrno err_errno(err); + StringSP description_sp(new String ("pid=%i: socketpair (domain=%i, type=%i, protocol=%i, {fd=%i, fd=%i}) -> err=%i", pid, domain, type, protocol, fds[0], fds[1], err)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fds[0] >= 0) + save_backtrace (fds[0], err_errno.get_errno(), description_sp, true); + if (fds[1] >= 0) + save_backtrace (fds[1], err_errno.get_errno(), description_sp, true); + return err; + } + else + { + return socketpair (domain, type, protocol, fds); + } +} + +//---------------------------------------------------------------------- +// open() interpose function +//---------------------------------------------------------------------- +extern "C" int +open$__interposed__ (const char *path, int oflag, int mode) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + int fd = -2; + StringSP description_sp(new String); + if (oflag & O_CREAT) + { + fd = ::open (path, oflag, mode); + description_sp->printf("pid=%i: open (path = '%s', oflag = %i, mode = %i) -> fd=%i", pid, path, oflag, mode, fd); + } + else + { + fd = ::open (path, oflag); + description_sp->printf("pid=%i: open (path = '%s', oflag = %i) -> fd=%i", pid, path, oflag, fd); + } + + InvalidFDErrno fd_errno(fd); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::open (path, oflag, mode); + } +} + +//---------------------------------------------------------------------- +// open$NOCANCEL() interpose function +//---------------------------------------------------------------------- +extern "C" int +open$NOCANCEL$__interposed__ (const char *path, int oflag, int mode) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int fd = ::open$NOCANCEL (path, oflag, mode); + InvalidFDErrno fd_errno(fd); + StringSP description_sp(new String ("pid=%i: open$NOCANCEL (path = '%s', oflag = %i, mode = %i) -> fd=%i", pid, path, oflag, mode, fd)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::open$NOCANCEL (path, oflag, mode); + } +} + + +//---------------------------------------------------------------------- +// __open_extended() interpose function +//---------------------------------------------------------------------- +extern "C" int +__open_extended$__interposed__ (const char *path, int oflag, uid_t uid, gid_t gid, int mode, struct kauth_filesec *fsacl) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int fd = ::__open_extended (path, oflag, uid, gid, mode, fsacl); + InvalidFDErrno fd_errno(fd); + StringSP description_sp(new String ("pid=%i: __open_extended (path='%s', oflag=%i, uid=%i, gid=%i, mode=%i, fsacl=%p) -> fd=%i", pid, path, oflag, uid, gid, mode, fsacl, fd)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::__open_extended (path, oflag, uid, gid, mode, fsacl); + } +} + +//---------------------------------------------------------------------- +// kqueue() interpose function +//---------------------------------------------------------------------- +extern "C" int +kqueue$__interposed__ (void) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int fd = ::kqueue (); + InvalidFDErrno fd_errno(fd); + StringSP description_sp(new String ("pid=%i: kqueue () -> fd=%i", pid, fd)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::kqueue (); + } +} + +//---------------------------------------------------------------------- +// shm_open() interpose function +//---------------------------------------------------------------------- +extern "C" int +shm_open$__interposed__ (const char *path, int oflag, int mode) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int fd = ::shm_open (path, oflag, mode); + InvalidFDErrno fd_errno(fd); + StringSP description_sp(new String ("pid=%i: shm_open (path = '%s', oflag = %i, mode = %i) -> fd=%i", pid, path, oflag, mode, fd)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::shm_open (path, oflag, mode); + } +} + +//---------------------------------------------------------------------- +// accept() interpose function +//---------------------------------------------------------------------- +extern "C" int +accept$__interposed__ (int socket, struct sockaddr *address, socklen_t *address_len) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int fd = ::accept (socket, address, address_len); + InvalidFDErrno fd_errno(fd); + StringSP description_sp(new String ("pid=%i: accept (socket=%i, ...) -> fd=%i", pid, socket, fd)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::accept (socket, address, address_len); + } +} + + +//---------------------------------------------------------------------- +// accept$NOCANCEL() interpose function +//---------------------------------------------------------------------- +extern "C" int +accept$NOCANCEL$__interposed__ (int socket, struct sockaddr *address, socklen_t *address_len) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int fd = ::accept$NOCANCEL (socket, address, address_len); + InvalidFDErrno fd_errno(fd); + StringSP description_sp(new String ("pid=%i: accept$NOCANCEL (socket=%i, ...) -> fd=%i", pid, socket, fd)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::accept$NOCANCEL (socket, address, address_len); + } +} + +//---------------------------------------------------------------------- +// dup() interpose function +//---------------------------------------------------------------------- +extern "C" int +dup$__interposed__ (int fd2) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int fd = ::dup (fd2); + InvalidFDErrno fd_errno(fd); + StringSP description_sp(new String ("pid=%i: dup (fd2=%i) -> fd=%i", pid, fd2, fd)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::dup (fd2); + } +} + +//---------------------------------------------------------------------- +// dup2() interpose function +//---------------------------------------------------------------------- +extern "C" int +dup2$__interposed__ (int fd1, int fd2) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + // If "fd2" is already opened, it will be closed during the + // dup2 call below, so we need to see if we have fd2 in our + // open map and treat it as a close(fd2) + FDEventMap::iterator pos = g_fd_event_map.find (fd2); + StringSP dup2_close_description_sp(new String ("pid=%i: dup2 (fd1=%i, fd2=%i) -> will close (fd=%i)", pid, fd1, fd2, fd2)); + if (pos != g_fd_event_map.end() && pos->second.back()->IsCreateEvent()) + save_backtrace (fd2, 0, dup2_close_description_sp, false); + + const int fd = ::dup2(fd1, fd2); + InvalidFDErrno fd_errno(fd); + StringSP description_sp(new String ("pid=%i: dup2 (fd1=%i, fd2=%i) -> fd=%i", pid, fd1, fd2, fd)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + + if (fd >= 0) + save_backtrace (fd, fd_errno.get_errno(), description_sp, true); + return fd; + } + else + { + return ::dup2(fd1, fd2); + } +} + +//---------------------------------------------------------------------- +// close() interpose function +//---------------------------------------------------------------------- +extern "C" int +close$__interposed__ (int fd) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int err = close(fd); + NegativeErrorErrno err_errno(err); + StringSP description_sp (new String); + if (err == -1) + description_sp->printf("pid=%i: close (fd=%i) => %i errno = %i (%s))", pid, fd, err, err_errno.get_errno(), strerror(err_errno.get_errno())); + else + description_sp->printf("pid=%i: close (fd=%i) => %i", pid, fd, err); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + + if (err == 0) + { + if (fd >= 0) + save_backtrace (fd, err, description_sp, false); + } + else if (err == -1) + { + if (err_errno.get_errno() == EBADF && fd != -1) + { + backtrace_error ("close (fd=%d) resulted in EBADF:\n", fd); + + FDEventMap::iterator pos = g_fd_event_map.find (fd); + if (pos != g_fd_event_map.end()) + { + log (get_logging_fd(), pos->second.back().get(), "\nfd=%d was previously %s with this event:\n", fd, pos->second.back()->IsCreateEvent() ? "opened" : "closed"); + } + } + } + return err; + } + else + { + return close (fd); + } +} + +//---------------------------------------------------------------------- +// close$NOCANCEL() interpose function +//---------------------------------------------------------------------- +extern "C" int +close$NOCANCEL$__interposed__ (int fd) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + const int err = close$NOCANCEL(fd); + NegativeErrorErrno err_errno(err); + StringSP description_sp (new String); + if (err == -1) + description_sp->printf("pid=%i: close$NOCANCEL (fd=%i) => %i errno = %i (%s))", pid, fd, err, err_errno.get_errno(), strerror(err_errno.get_errno())); + else + description_sp->printf("pid=%i: close$NOCANCEL (fd=%i) => %i", pid, fd, err); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + + if (err == 0) + { + if (fd >= 0) + save_backtrace (fd, err, description_sp, false); + } + else if (err == -1) + { + if (err_errno.get_errno() == EBADF && fd != -1) + { + backtrace_error ("close$NOCANCEL (fd=%d) resulted in EBADF\n:", fd); + + FDEventMap::iterator pos = g_fd_event_map.find (fd); + if (pos != g_fd_event_map.end()) + { + log (get_logging_fd(), pos->second.back().get(), "\nfd=%d was previously %s with this event:\n", fd, pos->second.back()->IsCreateEvent() ? "opened" : "closed"); + } + } + } + return err; + } + else + { + return close$NOCANCEL(fd); + } +} + +//---------------------------------------------------------------------- +// pipe() interpose function +//---------------------------------------------------------------------- +extern "C" int +pipe$__interposed__ (int fds[2]) +{ + const int pid = get_interposed_pid(); + if (pid >= 0) + { + Locker locker (&g_mutex); + fds[0] = -1; + fds[1] = -1; + const int err = pipe (fds); + const int saved_errno = errno; + StringSP description_sp(new String ("pid=%i: pipe ({fd=%i, fd=%i}) -> err=%i", pid, fds[0], fds[1], err)); + if (g_log_all_calls) + description_sp->log (get_logging_fd()); + if (fds[0] >= 0) + save_backtrace (fds[0], saved_errno, description_sp, true); + if (fds[1] >= 0) + save_backtrace (fds[1], saved_errno, description_sp, true); + errno = saved_errno; + return err; + } + else + { + return pipe (fds); + } +} + +//---------------------------------------------------------------------- +// get_fd_history() +// +// This function allows runtime access to the file descriptor history. +// +// @param[in] log_fd +// The file descriptor to log to +// +// @param[in] fd +// The file descriptor whose history should be dumped +//---------------------------------------------------------------------- +extern "C" void +get_fd_history (int log_fd, int fd) +{ + // "create" below needs to be outside of the mutex locker scope + if (log_fd >= 0) + { + bool got_lock = false; + Locker locker (&g_mutex, got_lock); + if (got_lock) + { + FDEventMap::iterator pos = g_fd_event_map.find (fd); + log_to_fd (log_fd, "Dumping file descriptor history for fd=%i:\n", fd); + if (pos != g_fd_event_map.end()) + { + FDEventArray &event_array = g_fd_event_map[fd]; + const size_t num_events = event_array.size(); + for (size_t i=0; i<num_events; ++i) + event_array[i]->Dump (log_fd); + } + else + { + log_to_fd (log_fd, "error: no file descriptor events found for fd=%i\n", fd); + } + } + else + { + log_to_fd (log_fd, "error: fd event mutex is locked...\n"); + } + } +} + +//---------------------------------------------------------------------- +// Interposing +//---------------------------------------------------------------------- +// FD creation routines +DYLD_INTERPOSE(accept$__interposed__, accept); +DYLD_INTERPOSE(accept$NOCANCEL$__interposed__, accept$NOCANCEL); +DYLD_INTERPOSE(dup$__interposed__, dup); +DYLD_INTERPOSE(dup2$__interposed__, dup2); +DYLD_INTERPOSE(kqueue$__interposed__, kqueue); +DYLD_INTERPOSE(open$__interposed__, open); +DYLD_INTERPOSE(open$NOCANCEL$__interposed__, open$NOCANCEL); +DYLD_INTERPOSE(__open_extended$__interposed__, __open_extended); +DYLD_INTERPOSE(pipe$__interposed__, pipe); +DYLD_INTERPOSE(shm_open$__interposed__, shm_open); +DYLD_INTERPOSE(socket$__interposed__, socket); +DYLD_INTERPOSE(socketpair$__interposed__, socketpair); + +// FD deleting routines +DYLD_INTERPOSE(close$__interposed__, close); +DYLD_INTERPOSE(close$NOCANCEL$__interposed__, close$NOCANCEL); + +} // namespace fd_interposing + + diff --git a/examples/interposing/darwin/fd_interposing/Makefile b/examples/interposing/darwin/fd_interposing/Makefile new file mode 100644 index 000000000000..20570b1ec320 --- /dev/null +++ b/examples/interposing/darwin/fd_interposing/Makefile @@ -0,0 +1,7 @@ +LEVEL = ../../../../test/make + +DYLIB_NAME := FDInterposing +DYLIB_ONLY := YES +DYLIB_CXX_SOURCES := FDInterposing.cpp + +include $(LEVEL)/Makefile.rules diff --git a/examples/lookup/Makefile b/examples/lookup/Makefile new file mode 100644 index 000000000000..f4429b6e4d92 --- /dev/null +++ b/examples/lookup/Makefile @@ -0,0 +1,17 @@ +LEVEL = ../../test/make + +CXX_SOURCES := main.cpp +EXE := lldb-lookup +USE_LIBCPP := 1 + +MY_OS = $(shell uname -s) + +ifeq "$(MY_OS)" "Darwin" + LLDB_BUILD_DIR ?= /Applications/Xcode.app/Contents/SharedFrameworks + LD_EXTRAS ?= -framework LLDB -Wl,-rpath,"$(LLDB_BUILD_DIR)" + FRAMEWORK_INCLUDES=-F"$(LLDB_BUILD_DIR)" +else + LD_EXTRAS ?= $(LLDB_BUILD_DIR)/_lldb.so +endif + +include $(LEVEL)/Makefile.rules diff --git a/examples/lookup/main.cpp b/examples/lookup/main.cpp new file mode 100644 index 000000000000..fbb908460676 --- /dev/null +++ b/examples/lookup/main.cpp @@ -0,0 +1,235 @@ +//===-- main.cpp ------------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include <getopt.h> +#include <stdint.h> +#include <stdlib.h> + +#if defined(__APPLE__) +#include <LLDB/LLDB.h> +#else +#include "LLDB/SBBlock.h" +#include "LLDB/SBCompileUnit.h" +#include "LLDB/SBDebugger.h" +#include "LLDB/SBFunction.h" +#include "LLDB/SBModule.h" +#include "LLDB/SBStream.h" +#include "LLDB/SBSymbol.h" +#include "LLDB/SBTarget.h" +#include "LLDB/SBThread.h" +#include "LLDB/SBProcess.h" +#endif + +#include <string> + +using namespace lldb; + +//---------------------------------------------------------------------- +// This quick sample code shows how to create a debugger instance and +// create an "i386" executable target. Then we can lookup the executable +// module and resolve a file address into a section offset address, +// and find all symbol context objects (if any) for that address: +// compile unit, function, deepest block, line table entry and the +// symbol. +// +// To build the program, type (while in this directory): +// +// $ make +// +// then (for example): +// +// $ DYLD_FRAMEWORK_PATH=/Volumes/data/lldb/svn/ToT/build/Debug ./a.out executable_path file_address +//---------------------------------------------------------------------- +class LLDBSentry +{ +public: + LLDBSentry() { + // Initialize LLDB + SBDebugger::Initialize(); + } + ~LLDBSentry() { + // Terminate LLDB + SBDebugger::Terminate(); + } +}; + +static struct option g_long_options[] = +{ + { "help", no_argument, NULL, 'h' }, + { "verbose", no_argument, NULL, 'v' }, + { "arch", required_argument, NULL, 'a' }, + { "platform", required_argument, NULL, 'p' }, + { NULL, 0, NULL, 0 } +}; + +#define PROGRAM_NAME "lldb-lookup" +void +usage () +{ + puts ( + "NAME\n" + " " PROGRAM_NAME " -- symbolicate addresses using lldb.\n" + "\n" + "SYNOPSIS\n" + " " PROGRAM_NAME " [[--arch=<ARCH>] [--platform=<PLATFORM>] [--verbose] [--help] --] <PATH> <ADDRESS> [<ADDRESS>....]\n" + "\n" + "DESCRIPTION\n" + " Loads the executable pointed to by <PATH> and looks up and <ADDRESS>\n" + " arguments\n" + "\n" + "EXAMPLE\n" + " " PROGRAM_NAME " --arch=x86_64 -- /usr/lib/dyld 0x100000000\n" + ); + exit(0); +} +int +main (int argc, char const *argv[]) +{ + // Use a sentry object to properly initialize/terminate LLDB. + LLDBSentry sentry; + + SBDebugger debugger (SBDebugger::Create()); + + // Create a debugger instance so we can create a target + if (!debugger.IsValid()) + fprintf (stderr, "error: failed to create a debugger object\n"); + + bool show_usage = false; + bool verbose = false; + const char *arch = NULL; + const char *platform = NULL; + std::string short_options("h?"); + for (const struct option *opt = g_long_options; opt->name; ++opt) + { + if (isprint(opt->val)) + { + short_options.append(1, (char)opt->val); + switch (opt->has_arg) + { + case no_argument: + break; + case required_argument: + short_options.append(1, ':'); + break; + case optional_argument: + short_options.append(2, ':'); + break; + } + } + } +#ifdef __GLIBC__ + optind = 0; +#else + optreset = 1; + optind = 1; +#endif + char ch; + while ((ch = getopt_long_only(argc, (char * const *)argv, short_options.c_str(), g_long_options, 0)) != -1) + { + switch (ch) + { + case 0: + break; + + case 'a': + if (arch != NULL) + { + fprintf (stderr, "error: the --arch option can only be specified once\n"); + exit(1); + } + arch = optarg; + break; + + case 'p': + platform = optarg; + break; + + case 'v': + verbose = true; + break; + + case 'h': + case '?': + default: + show_usage = true; + break; + } + } + argc -= optind; + argv += optind; + + if (show_usage || argc < 2) + usage(); + + int arg_idx = 0; + // The first argument is the file path we want to look something up in + const char *exe_file_path = argv[arg_idx]; + const char *addr_cstr; + const bool add_dependent_libs = false; + SBError error; + SBStream strm; + strm.RedirectToFileHandle (stdout, false); + + while ((addr_cstr = argv[++arg_idx]) != NULL) + { + // The second argument in the address that we want to lookup + lldb::addr_t file_addr = strtoull (addr_cstr, NULL, 0); + + // Create a target using the executable. + SBTarget target = debugger.CreateTarget (exe_file_path, + arch, + platform, + add_dependent_libs, + error); + if (!error.Success()) + { + fprintf (stderr, "error: %s\n", error.GetCString()); + exit(1); + } + + printf ("%sLooking up 0x%llx in '%s':\n", (arg_idx > 1) ? "\n" : "", file_addr, exe_file_path); + + if (target.IsValid()) + { + // Find the executable module so we can do a lookup inside it + SBFileSpec exe_file_spec (exe_file_path, true); + SBModule module (target.FindModule (exe_file_spec)); + + // Take a file virtual address and resolve it to a section offset + // address that can be used to do a symbol lookup by address + SBAddress addr = module.ResolveFileAddress (file_addr); + bool success = addr.IsValid() && addr.GetSection().IsValid(); + if (success) + { + // We can resolve a section offset address in the module + // and only ask for what we need. You can logical or together + // bits from the SymbolContextItem enumeration found in + // lldb-enumeration.h to request only what you want. Here we + // are asking for everything. + // + // NOTE: the less you ask for, the less LLDB will parse as + // LLDB does partial parsing on just about everything. + SBSymbolContext sc (module.ResolveSymbolContextForAddress (addr, eSymbolContextEverything)); + + strm.Printf (" Address: %s + 0x%llx\n Summary: ", addr.GetSection().GetName (), addr.GetOffset()); + addr.GetDescription (strm); + strm.Printf ("\n"); + if (verbose) + sc.GetDescription (strm); + } + else + { + printf ("error: 0x%llx does not resolve to a valid file address in '%s'\n", file_addr, exe_file_path); + } + } + } + + return 0; +} + diff --git a/examples/plugins/commands/fooplugin.cpp b/examples/plugins/commands/fooplugin.cpp new file mode 100644 index 000000000000..2aaf8ff547e3 --- /dev/null +++ b/examples/plugins/commands/fooplugin.cpp @@ -0,0 +1,56 @@ +//===-- fooplugin.cpp -------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +/* +An example plugin for LLDB that provides a new foo command with a child subcommand +Compile this into a dylib foo.dylib and load by placing in appropriate locations on disk or +by typing plugin load foo.dylib at the LLDB command line +*/ + +#include <LLDB/SBCommandInterpreter.h> +#include <LLDB/SBCommandReturnObject.h> +#include <LLDB/SBDebugger.h> + +namespace lldb { + bool + PluginInitialize (lldb::SBDebugger debugger); +} + +class ChildCommand : public lldb::SBCommandPluginInterface +{ +public: + virtual bool + DoExecute (lldb::SBDebugger debugger, + char** command, + lldb::SBCommandReturnObject &result) + { + if (command) + { + const char* arg = *command; + while (arg) + { + result.Printf("%s\n",arg); + arg = *(++command); + } + return true; + } + return false; + } + +}; + +bool +lldb::PluginInitialize (lldb::SBDebugger debugger) +{ + lldb::SBCommandInterpreter interpreter = debugger.GetCommandInterpreter(); + lldb::SBCommand foo = interpreter.AddMultiwordCommand("foo",NULL); + foo.AddCommand("child",new ChildCommand(),"a child of foo"); + return true; +} + diff --git a/examples/python/cmdtemplate.py b/examples/python/cmdtemplate.py new file mode 100644 index 000000000000..ca575362f9b7 --- /dev/null +++ b/examples/python/cmdtemplate.py @@ -0,0 +1,76 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# Be sure to add the python path that points to the LLDB shared library. +# +# # To use this in the embedded python interpreter using "lldb" just +# import it with the full path using the "command script import" +# command +# (lldb) command script import /path/to/cmdtemplate.py +#---------------------------------------------------------------------- + +import lldb +import commands +import optparse +import shlex + +def create_framestats_options(): + usage = "usage: %prog [options]" + description='''This command is meant to be an example of how to make an LLDB command that +does something useful, follows best practices, and exploits the SB API. +Specifically, this command computes the aggregate and average size of the variables in the current frame +and allows you to tweak exactly which variables are to be accounted in the computation. +''' + parser = optparse.OptionParser(description=description, prog='framestats',usage=usage) + parser.add_option('-i', '--in-scope', action='store_true', dest='inscope', help='in_scope_only = True', default=False) + parser.add_option('-a', '--arguments', action='store_true', dest='arguments', help='arguments = True', default=False) + parser.add_option('-l', '--locals', action='store_true', dest='locals', help='locals = True', default=False) + parser.add_option('-s', '--statics', action='store_true', dest='statics', help='statics = True', default=False) + return parser + +def the_framestats_command(debugger, command, result, dict): + # Use the Shell Lexer to properly parse up command options just like a + # shell would + command_args = shlex.split(command) + parser = create_framestats_options() + try: + (options, args) = parser.parse_args(command_args) + except: + # if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit + # (courtesy of OptParse dealing with argument errors by throwing SystemExit) + result.SetError ("option parsing failed") + return + + # in a command - the lldb.* convenience variables are not to be used + # and their values (if any) are undefined + # this is the best practice to access those objects from within a command + target = debugger.GetSelectedTarget() + process = target.GetProcess() + thread = process.GetSelectedThread() + frame = thread.GetSelectedFrame() + if not frame.IsValid(): + return "no frame here" + # from now on, replace lldb.<thing>.whatever with <thing>.whatever + variables_list = frame.GetVariables(options.arguments, options.locals, options.statics, options.inscope) + variables_count = variables_list.GetSize() + if variables_count == 0: + print >> result, "no variables here" + return + total_size = 0 + for i in range(0,variables_count): + variable = variables_list.GetValueAtIndex(i) + variable_type = variable.GetType() + total_size = total_size + variable_type.GetByteSize() + average_size = float(total_size) / variables_count + print >>result, "Your frame has %d variables. Their total size is %d bytes. The average size is %f bytes" % (variables_count,total_size,average_size) + # not returning anything is akin to returning success + +def __lldb_init_module (debugger, dict): + # This initializer is being run from LLDB in the embedded command interpreter + # Make the options so we can generate the help text for the new LLDB + # command line command prior to registering it with LLDB below + parser = create_framestats_options() + the_framestats_command.__doc__ = parser.format_help() + # Add any commands contained in this module to LLDB + debugger.HandleCommand('command script add -f cmdtemplate.the_framestats_command framestats') + print 'The "framestats" command has been installed, type "help framestats" or "framestats --help" for detailed help.' diff --git a/examples/python/crashlog.py b/examples/python/crashlog.py new file mode 100755 index 000000000000..60a6a1f50f00 --- /dev/null +++ b/examples/python/crashlog.py @@ -0,0 +1,829 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# Be sure to add the python path that points to the LLDB shared library. +# +# To use this in the embedded python interpreter using "lldb": +# +# cd /path/containing/crashlog.py +# lldb +# (lldb) script import crashlog +# "crashlog" command installed, type "crashlog --help" for detailed help +# (lldb) crashlog ~/Library/Logs/DiagnosticReports/a.crash +# +# The benefit of running the crashlog command inside lldb in the +# embedded python interpreter is when the command completes, there +# will be a target with all of the files loaded at the locations +# described in the crash log. Only the files that have stack frames +# in the backtrace will be loaded unless the "--load-all" option +# has been specified. This allows users to explore the program in the +# state it was in right at crash time. +# +# On MacOSX csh, tcsh: +# ( setenv PYTHONPATH /path/to/LLDB.framework/Resources/Python ; ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash ) +# +# On MacOSX sh, bash: +# PYTHONPATH=/path/to/LLDB.framework/Resources/Python ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash +#---------------------------------------------------------------------- + +import commands +import cmd +import datetime +import glob +import optparse +import os +import platform +import plistlib +import pprint # pp = pprint.PrettyPrinter(indent=4); pp.pprint(command_args) +import re +import shlex +import string +import sys +import time +import uuid + +try: + # Just try for LLDB in case PYTHONPATH is already correctly setup + import lldb +except ImportError: + lldb_python_dirs = list() + # lldb is not in the PYTHONPATH, try some defaults for the current platform + platform_system = platform.system() + if platform_system == 'Darwin': + # On Darwin, try the currently selected Xcode directory + xcode_dir = commands.getoutput("xcode-select --print-path") + if xcode_dir: + lldb_python_dirs.append(os.path.realpath(xcode_dir + '/../SharedFrameworks/LLDB.framework/Resources/Python')) + lldb_python_dirs.append(xcode_dir + '/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + lldb_python_dirs.append('/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + success = False + for lldb_python_dir in lldb_python_dirs: + if os.path.exists(lldb_python_dir): + if not (sys.path.__contains__(lldb_python_dir)): + sys.path.append(lldb_python_dir) + try: + import lldb + except ImportError: + pass + else: + print 'imported lldb from: "%s"' % (lldb_python_dir) + success = True + break + if not success: + print "error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly" + sys.exit(1) + +from lldb.utils import symbolication + +PARSE_MODE_NORMAL = 0 +PARSE_MODE_THREAD = 1 +PARSE_MODE_IMAGES = 2 +PARSE_MODE_THREGS = 3 +PARSE_MODE_SYSTEM = 4 + +class CrashLog(symbolication.Symbolicator): + """Class that does parses darwin crash logs""" + parent_process_regex = re.compile('^Parent Process:\s*(.*)\[(\d+)\]'); + thread_state_regex = re.compile('^Thread ([0-9]+) crashed with') + thread_regex = re.compile('^Thread ([0-9]+)([^:]*):(.*)') + app_backtrace_regex = re.compile('^Application Specific Backtrace ([0-9]+)([^:]*):(.*)') + frame_regex = re.compile('^([0-9]+)\s+([^ ]+)\s+(0x[0-9a-fA-F]+) +(.*)') + image_regex_uuid = re.compile('(0x[0-9a-fA-F]+)[- ]+(0x[0-9a-fA-F]+) +[+]?([^ ]+) +([^<]+)<([-0-9a-fA-F]+)> (.*)'); + image_regex_no_uuid = re.compile('(0x[0-9a-fA-F]+)[- ]+(0x[0-9a-fA-F]+) +[+]?([^ ]+) +([^/]+)/(.*)'); + empty_line_regex = re.compile('^$') + + class Thread: + """Class that represents a thread in a darwin crash log""" + def __init__(self, index, app_specific_backtrace): + self.index = index + self.frames = list() + self.idents = list() + self.registers = dict() + self.reason = None + self.queue = None + self.app_specific_backtrace = app_specific_backtrace + + def dump(self, prefix): + if self.app_specific_backtrace: + print "%Application Specific Backtrace[%u] %s" % (prefix, self.index, self.reason) + else: + print "%sThread[%u] %s" % (prefix, self.index, self.reason) + if self.frames: + print "%s Frames:" % (prefix) + for frame in self.frames: + frame.dump(prefix + ' ') + if self.registers: + print "%s Registers:" % (prefix) + for reg in self.registers.keys(): + print "%s %-5s = %#16.16x" % (prefix, reg, self.registers[reg]) + + def dump_symbolicated (self, crash_log, options): + this_thread_crashed = self.app_specific_backtrace + if not this_thread_crashed: + this_thread_crashed = self.did_crash() + if options.crashed_only and this_thread_crashed == False: + return + + print "%s" % self + #prev_frame_index = -1 + display_frame_idx = -1 + for frame_idx, frame in enumerate(self.frames): + disassemble = (this_thread_crashed or options.disassemble_all_threads) and frame_idx < options.disassemble_depth; + if frame_idx == 0: + symbolicated_frame_addresses = crash_log.symbolicate (frame.pc & crash_log.addr_mask, options.verbose) + else: + # Any frame above frame zero and we have to subtract one to get the previous line entry + symbolicated_frame_addresses = crash_log.symbolicate ((frame.pc & crash_log.addr_mask) - 1, options.verbose) + + if symbolicated_frame_addresses: + symbolicated_frame_address_idx = 0 + for symbolicated_frame_address in symbolicated_frame_addresses: + display_frame_idx += 1 + print '[%3u] %s' % (frame_idx, symbolicated_frame_address) + if (options.source_all or self.did_crash()) and display_frame_idx < options.source_frames and options.source_context: + source_context = options.source_context + line_entry = symbolicated_frame_address.get_symbol_context().line_entry + if line_entry.IsValid(): + strm = lldb.SBStream() + if line_entry: + lldb.debugger.GetSourceManager().DisplaySourceLinesWithLineNumbers(line_entry.file, line_entry.line, source_context, source_context, "->", strm) + source_text = strm.GetData() + if source_text: + # Indent the source a bit + indent_str = ' ' + join_str = '\n' + indent_str + print '%s%s' % (indent_str, join_str.join(source_text.split('\n'))) + if symbolicated_frame_address_idx == 0: + if disassemble: + instructions = symbolicated_frame_address.get_instructions() + if instructions: + print + symbolication.disassemble_instructions (crash_log.get_target(), + instructions, + frame.pc, + options.disassemble_before, + options.disassemble_after, frame.index > 0) + print + symbolicated_frame_address_idx += 1 + else: + print frame + + def add_ident(self, ident): + if not ident in self.idents: + self.idents.append(ident) + + def did_crash(self): + return self.reason != None + + def __str__(self): + if self.app_specific_backtrace: + s = "Application Specific Backtrace[%u]" % self.index + else: + s = "Thread[%u]" % self.index + if self.reason: + s += ' %s' % self.reason + return s + + + class Frame: + """Class that represents a stack frame in a thread in a darwin crash log""" + def __init__(self, index, pc, description): + self.pc = pc + self.description = description + self.index = index + + def __str__(self): + if self.description: + return "[%3u] 0x%16.16x %s" % (self.index, self.pc, self.description) + else: + return "[%3u] 0x%16.16x" % (self.index, self.pc) + + def dump(self, prefix): + print "%s%s" % (prefix, str(self)) + + class DarwinImage(symbolication.Image): + """Class that represents a binary images in a darwin crash log""" + dsymForUUIDBinary = os.path.expanduser('~rc/bin/dsymForUUID') + if not os.path.exists(dsymForUUIDBinary): + dsymForUUIDBinary = commands.getoutput('which dsymForUUID') + + dwarfdump_uuid_regex = re.compile('UUID: ([-0-9a-fA-F]+) \(([^\(]+)\) .*') + + def __init__(self, text_addr_lo, text_addr_hi, identifier, version, uuid, path): + symbolication.Image.__init__(self, path, uuid); + self.add_section (symbolication.Section(text_addr_lo, text_addr_hi, "__TEXT")) + self.identifier = identifier + self.version = version + + def locate_module_and_debug_symbols(self): + # Don't load a module twice... + if self.resolved: + return True + # Mark this as resolved so we don't keep trying + self.resolved = True + uuid_str = self.get_normalized_uuid_string() + print 'Getting symbols for %s %s...' % (uuid_str, self.path), + if os.path.exists(self.dsymForUUIDBinary): + dsym_for_uuid_command = '%s %s' % (self.dsymForUUIDBinary, uuid_str) + s = commands.getoutput(dsym_for_uuid_command) + if s: + plist_root = plistlib.readPlistFromString (s) + if plist_root: + plist = plist_root[uuid_str] + if plist: + if 'DBGArchitecture' in plist: + self.arch = plist['DBGArchitecture'] + if 'DBGDSYMPath' in plist: + self.symfile = os.path.realpath(plist['DBGDSYMPath']) + if 'DBGSymbolRichExecutable' in plist: + self.path = os.path.expanduser (plist['DBGSymbolRichExecutable']) + self.resolved_path = self.path + if not self.resolved_path and os.path.exists(self.path): + dwarfdump_cmd_output = commands.getoutput('dwarfdump --uuid "%s"' % self.path) + self_uuid = self.get_uuid() + for line in dwarfdump_cmd_output.splitlines(): + match = self.dwarfdump_uuid_regex.search (line) + if match: + dwarf_uuid_str = match.group(1) + dwarf_uuid = uuid.UUID(dwarf_uuid_str) + if self_uuid == dwarf_uuid: + self.resolved_path = self.path + self.arch = match.group(2) + break; + if not self.resolved_path: + self.unavailable = True + print "error\n error: unable to locate '%s' with UUID %s" % (self.path, uuid_str) + return False + if (self.resolved_path and os.path.exists(self.resolved_path)) or (self.path and os.path.exists(self.path)): + print 'ok' + # if self.resolved_path: + # print ' exe = "%s"' % self.resolved_path + # if self.symfile: + # print ' dsym = "%s"' % self.symfile + return True + else: + self.unavailable = True + return False + + + + def __init__(self, path): + """CrashLog constructor that take a path to a darwin crash log file""" + symbolication.Symbolicator.__init__(self); + self.path = os.path.expanduser(path); + self.info_lines = list() + self.system_profile = list() + self.threads = list() + self.backtraces = list() # For application specific backtraces + self.idents = list() # A list of the required identifiers for doing all stack backtraces + self.crashed_thread_idx = -1 + self.version = -1 + self.error = None + self.target = None + # With possible initial component of ~ or ~user replaced by that user's home directory. + try: + f = open(self.path) + except IOError: + self.error = 'error: cannot open "%s"' % self.path + return + + self.file_lines = f.read().splitlines() + parse_mode = PARSE_MODE_NORMAL + thread = None + app_specific_backtrace = False + for line in self.file_lines: + # print line + line_len = len(line) + if line_len == 0: + if thread: + if parse_mode == PARSE_MODE_THREAD: + if thread.index == self.crashed_thread_idx: + thread.reason = '' + if self.thread_exception: + thread.reason += self.thread_exception + if self.thread_exception_data: + thread.reason += " (%s)" % self.thread_exception_data + if app_specific_backtrace: + self.backtraces.append(thread) + else: + self.threads.append(thread) + thread = None + else: + # only append an extra empty line if the previous line + # in the info_lines wasn't empty + if len(self.info_lines) > 0 and len(self.info_lines[-1]): + self.info_lines.append(line) + parse_mode = PARSE_MODE_NORMAL + # print 'PARSE_MODE_NORMAL' + elif parse_mode == PARSE_MODE_NORMAL: + if line.startswith ('Process:'): + (self.process_name, pid_with_brackets) = line[8:].strip().split(' [') + self.process_id = pid_with_brackets.strip('[]') + elif line.startswith ('Path:'): + self.process_path = line[5:].strip() + elif line.startswith ('Identifier:'): + self.process_identifier = line[11:].strip() + elif line.startswith ('Version:'): + version_string = line[8:].strip() + matched_pair = re.search("(.+)\((.+)\)", version_string) + if matched_pair: + self.process_version = matched_pair.group(1) + self.process_compatability_version = matched_pair.group(2) + else: + self.process = version_string + self.process_compatability_version = version_string + elif self.parent_process_regex.search(line): + parent_process_match = self.parent_process_regex.search(line) + self.parent_process_name = parent_process_match.group(1) + self.parent_process_id = parent_process_match.group(2) + elif line.startswith ('Exception Type:'): + self.thread_exception = line[15:].strip() + continue + elif line.startswith ('Exception Codes:'): + self.thread_exception_data = line[16:].strip() + continue + elif line.startswith ('Crashed Thread:'): + self.crashed_thread_idx = int(line[15:].strip().split()[0]) + continue + elif line.startswith ('Report Version:'): + self.version = int(line[15:].strip()) + continue + elif line.startswith ('System Profile:'): + parse_mode = PARSE_MODE_SYSTEM + continue + elif (line.startswith ('Interval Since Last Report:') or + line.startswith ('Crashes Since Last Report:') or + line.startswith ('Per-App Interval Since Last Report:') or + line.startswith ('Per-App Crashes Since Last Report:') or + line.startswith ('Sleep/Wake UUID:') or + line.startswith ('Anonymous UUID:')): + # ignore these + continue + elif line.startswith ('Thread'): + thread_state_match = self.thread_state_regex.search (line) + if thread_state_match: + app_specific_backtrace = False + thread_state_match = self.thread_regex.search (line) + thread_idx = int(thread_state_match.group(1)) + parse_mode = PARSE_MODE_THREGS + thread = self.threads[thread_idx] + else: + thread_match = self.thread_regex.search (line) + if thread_match: + app_specific_backtrace = False + parse_mode = PARSE_MODE_THREAD + thread_idx = int(thread_match.group(1)) + thread = CrashLog.Thread(thread_idx, False) + continue + elif line.startswith ('Binary Images:'): + parse_mode = PARSE_MODE_IMAGES + continue + elif line.startswith ('Application Specific Backtrace'): + app_backtrace_match = self.app_backtrace_regex.search (line) + if app_backtrace_match: + parse_mode = PARSE_MODE_THREAD + app_specific_backtrace = True + idx = int(app_backtrace_match.group(1)) + thread = CrashLog.Thread(idx, True) + self.info_lines.append(line.strip()) + elif parse_mode == PARSE_MODE_THREAD: + if line.startswith ('Thread'): + continue + frame_match = self.frame_regex.search(line) + if frame_match: + ident = frame_match.group(2) + thread.add_ident(ident) + if not ident in self.idents: + self.idents.append(ident) + thread.frames.append (CrashLog.Frame(int(frame_match.group(1)), int(frame_match.group(3), 0), frame_match.group(4))) + else: + print 'error: frame regex failed for line: "%s"' % line + elif parse_mode == PARSE_MODE_IMAGES: + image_match = self.image_regex_uuid.search (line) + if image_match: + image = CrashLog.DarwinImage (int(image_match.group(1),0), + int(image_match.group(2),0), + image_match.group(3).strip(), + image_match.group(4).strip(), + uuid.UUID(image_match.group(5)), + image_match.group(6)) + self.images.append (image) + else: + image_match = self.image_regex_no_uuid.search (line) + if image_match: + image = CrashLog.DarwinImage (int(image_match.group(1),0), + int(image_match.group(2),0), + image_match.group(3).strip(), + image_match.group(4).strip(), + None, + image_match.group(5)) + self.images.append (image) + else: + print "error: image regex failed for: %s" % line + + elif parse_mode == PARSE_MODE_THREGS: + stripped_line = line.strip() + # "r12: 0x00007fff6b5939c8 r13: 0x0000000007000006 r14: 0x0000000000002a03 r15: 0x0000000000000c00" + reg_values = re.findall ('([a-zA-Z0-9]+: 0[Xx][0-9a-fA-F]+) *', stripped_line); + for reg_value in reg_values: + #print 'reg_value = "%s"' % reg_value + (reg, value) = reg_value.split(': ') + #print 'reg = "%s"' % reg + #print 'value = "%s"' % value + thread.registers[reg.strip()] = int(value, 0) + elif parse_mode == PARSE_MODE_SYSTEM: + self.system_profile.append(line) + f.close() + + def dump(self): + print "Crash Log File: %s" % (self.path) + if self.backtraces: + print "\nApplication Specific Backtraces:" + for thread in self.backtraces: + thread.dump(' ') + print "\nThreads:" + for thread in self.threads: + thread.dump(' ') + print "\nImages:" + for image in self.images: + image.dump(' ') + + def find_image_with_identifier(self, identifier): + for image in self.images: + if image.identifier == identifier: + return image + regex_text = '^.*\.%s$' % (identifier) + regex = re.compile(regex_text) + for image in self.images: + if regex.match(image.identifier): + return image + return None + + def create_target(self): + #print 'crashlog.create_target()...' + if self.target is None: + self.target = symbolication.Symbolicator.create_target(self) + if self.target: + return self.target + # We weren't able to open the main executable as, but we can still symbolicate + print 'crashlog.create_target()...2' + if self.idents: + for ident in self.idents: + image = self.find_image_with_identifier (ident) + if image: + self.target = image.create_target () + if self.target: + return self.target # success + print 'crashlog.create_target()...3' + for image in self.images: + self.target = image.create_target () + if self.target: + return self.target # success + print 'crashlog.create_target()...4' + print 'error: unable to locate any executables from the crash log' + return self.target + + def get_target(self): + return self.target + +def usage(): + print "Usage: lldb-symbolicate.py [-n name] executable-image" + sys.exit(0) + +class Interactive(cmd.Cmd): + '''Interactive prompt for analyzing one or more Darwin crash logs, type "help" to see a list of supported commands.''' + image_option_parser = None + + def __init__(self, crash_logs): + cmd.Cmd.__init__(self) + self.use_rawinput = False + self.intro = 'Interactive crashlogs prompt, type "help" to see a list of supported commands.' + self.crash_logs = crash_logs + self.prompt = '% ' + + def default(self, line): + '''Catch all for unknown command, which will exit the interpreter.''' + print "uknown command: %s" % line + return True + + def do_q(self, line): + '''Quit command''' + return True + + def do_quit(self, line): + '''Quit command''' + return True + + def do_symbolicate(self, line): + description='''Symbolicate one or more darwin crash log files by index to provide source file and line information, + inlined stack frames back to the concrete functions, and disassemble the location of the crash + for the first frame of the crashed thread.''' + option_parser = CreateSymbolicateCrashLogOptions ('symbolicate', description, False) + command_args = shlex.split(line) + try: + (options, args) = option_parser.parse_args(command_args) + except: + return + + if args: + # We have arguments, they must valid be crash log file indexes + for idx_str in args: + idx = int(idx_str) + if idx < len(self.crash_logs): + SymbolicateCrashLog (self.crash_logs[idx], options) + else: + print 'error: crash log index %u is out of range' % (idx) + else: + # No arguments, symbolicate all crash logs using the options provided + for idx in range(len(self.crash_logs)): + SymbolicateCrashLog (self.crash_logs[idx], options) + + def do_list(self, line=None): + '''Dump a list of all crash logs that are currently loaded. + + USAGE: list''' + print '%u crash logs are loaded:' % len(self.crash_logs) + for (crash_log_idx, crash_log) in enumerate(self.crash_logs): + print '[%u] = %s' % (crash_log_idx, crash_log.path) + + def do_image(self, line): + '''Dump information about one or more binary images in the crash log given an image basename, or all images if no arguments are provided.''' + usage = "usage: %prog [options] <PATH> [PATH ...]" + description='''Dump information about one or more images in all crash logs. The <PATH> can be a full path, image basename, or partial path. Searches are done in this order.''' + command_args = shlex.split(line) + if not self.image_option_parser: + self.image_option_parser = optparse.OptionParser(description=description, prog='image',usage=usage) + self.image_option_parser.add_option('-a', '--all', action='store_true', help='show all images', default=False) + try: + (options, args) = self.image_option_parser.parse_args(command_args) + except: + return + + if args: + for image_path in args: + fullpath_search = image_path[0] == '/' + for (crash_log_idx, crash_log) in enumerate(self.crash_logs): + matches_found = 0 + for (image_idx, image) in enumerate(crash_log.images): + if fullpath_search: + if image.get_resolved_path() == image_path: + matches_found += 1 + print '[%u] ' % (crash_log_idx), image + else: + image_basename = image.get_resolved_path_basename() + if image_basename == image_path: + matches_found += 1 + print '[%u] ' % (crash_log_idx), image + if matches_found == 0: + for (image_idx, image) in enumerate(crash_log.images): + resolved_image_path = image.get_resolved_path() + if resolved_image_path and string.find(image.get_resolved_path(), image_path) >= 0: + print '[%u] ' % (crash_log_idx), image + else: + for crash_log in self.crash_logs: + for (image_idx, image) in enumerate(crash_log.images): + print '[%u] %s' % (image_idx, image) + return False + + +def interactive_crashlogs(options, args): + crash_log_files = list() + for arg in args: + for resolved_path in glob.glob(arg): + crash_log_files.append(resolved_path) + + crash_logs = list(); + for crash_log_file in crash_log_files: + #print 'crash_log_file = "%s"' % crash_log_file + crash_log = CrashLog(crash_log_file) + if crash_log.error: + print crash_log.error + continue + if options.debug: + crash_log.dump() + if not crash_log.images: + print 'error: no images in crash log "%s"' % (crash_log) + continue + else: + crash_logs.append(crash_log) + + interpreter = Interactive(crash_logs) + # List all crash logs that were imported + interpreter.do_list() + interpreter.cmdloop() + + +def save_crashlog(debugger, command, result, dict): + usage = "usage: %prog [options] <output-path>" + description='''Export the state of current target into a crashlog file''' + parser = optparse.OptionParser(description=description, prog='save_crashlog',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + try: + (options, args) = parser.parse_args(shlex.split(command)) + except: + result.PutCString ("error: invalid options"); + return + if len(args) != 1: + result.PutCString ("error: invalid arguments, a single output file is the only valid argument") + return + out_file = open(args[0], 'w') + if not out_file: + result.PutCString ("error: failed to open file '%s' for writing...", args[0]); + return + target = debugger.GetSelectedTarget() + if target: + identifier = target.executable.basename + if lldb.process: + pid = lldb.process.id + if pid != lldb.LLDB_INVALID_PROCESS_ID: + out_file.write('Process: %s [%u]\n' % (identifier, pid)) + out_file.write('Path: %s\n' % (target.executable.fullpath)) + out_file.write('Identifier: %s\n' % (identifier)) + out_file.write('\nDate/Time: %s\n' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) + out_file.write('OS Version: Mac OS X %s (%s)\n' % (platform.mac_ver()[0], commands.getoutput('sysctl -n kern.osversion'))); + out_file.write('Report Version: 9\n') + for thread_idx in range(lldb.process.num_threads): + thread = lldb.process.thread[thread_idx] + out_file.write('\nThread %u:\n' % (thread_idx)) + for (frame_idx, frame) in enumerate(thread.frames): + frame_pc = frame.pc + frame_offset = 0 + if frame.function: + block = frame.GetFrameBlock() + block_range = block.range[frame.addr] + if block_range: + block_start_addr = block_range[0] + frame_offset = frame_pc - block_start_addr.load_addr + else: + frame_offset = frame_pc - frame.function.addr.load_addr + elif frame.symbol: + frame_offset = frame_pc - frame.symbol.addr.load_addr + out_file.write('%-3u %-32s 0x%16.16x %s' % (frame_idx, frame.module.file.basename, frame_pc, frame.name)) + if frame_offset > 0: + out_file.write(' + %u' % (frame_offset)) + line_entry = frame.line_entry + if line_entry: + if options.verbose: + # This will output the fullpath + line + column + out_file.write(' %s' % (line_entry)) + else: + out_file.write(' %s:%u' % (line_entry.file.basename, line_entry.line)) + column = line_entry.column + if column: + out_file.write(':%u' % (column)) + out_file.write('\n') + + out_file.write('\nBinary Images:\n') + for module in target.modules: + text_segment = module.section['__TEXT'] + if text_segment: + text_segment_load_addr = text_segment.GetLoadAddress(target) + if text_segment_load_addr != lldb.LLDB_INVALID_ADDRESS: + text_segment_end_load_addr = text_segment_load_addr + text_segment.size + identifier = module.file.basename + module_version = '???' + module_version_array = module.GetVersion() + if module_version_array: + module_version = '.'.join(map(str,module_version_array)) + out_file.write (' 0x%16.16x - 0x%16.16x %s (%s - ???) <%s> %s\n' % (text_segment_load_addr, text_segment_end_load_addr, identifier, module_version, module.GetUUIDString(), module.file.fullpath)) + out_file.close() + else: + result.PutCString ("error: invalid target"); + + +def Symbolicate(debugger, command, result, dict): + try: + SymbolicateCrashLogs (shlex.split(command)) + except: + result.PutCString ("error: python exception %s" % sys.exc_info()[0]) + +def SymbolicateCrashLog(crash_log, options): + if crash_log.error: + print crash_log.error + return + if options.debug: + crash_log.dump() + if not crash_log.images: + print 'error: no images in crash log' + return + + if options.dump_image_list: + print "Binary Images:" + for image in crash_log.images: + if options.verbose: + print image.debug_dump() + else: + print image + + target = crash_log.create_target () + if not target: + return + exe_module = target.GetModuleAtIndex(0) + images_to_load = list() + loaded_images = list() + if options.load_all_images: + # --load-all option was specified, load everything up + for image in crash_log.images: + images_to_load.append(image) + else: + # Only load the images found in stack frames for the crashed threads + if options.crashed_only: + for thread in crash_log.threads: + if thread.did_crash(): + for ident in thread.idents: + images = crash_log.find_images_with_identifier (ident) + if images: + for image in images: + images_to_load.append(image) + else: + print 'error: can\'t find image for identifier "%s"' % ident + else: + for ident in crash_log.idents: + images = crash_log.find_images_with_identifier (ident) + if images: + for image in images: + images_to_load.append(image) + else: + print 'error: can\'t find image for identifier "%s"' % ident + + for image in images_to_load: + if not image in loaded_images: + err = image.add_module (target) + if err: + print err + else: + #print 'loaded %s' % image + loaded_images.append(image) + + if crash_log.backtraces: + for thread in crash_log.backtraces: + thread.dump_symbolicated (crash_log, options) + print + + for thread in crash_log.threads: + thread.dump_symbolicated (crash_log, options) + print + + +def CreateSymbolicateCrashLogOptions(command_name, description, add_interactive_options): + usage = "usage: %prog [options] <FILE> [FILE ...]" + option_parser = optparse.OptionParser(description=description, prog='crashlog',usage=usage) + option_parser.add_option('--verbose' , '-v', action='store_true', dest='verbose', help='display verbose debug info', default=False) + option_parser.add_option('--debug' , '-g', action='store_true', dest='debug', help='display verbose debug logging', default=False) + option_parser.add_option('--load-all' , '-a', action='store_true', dest='load_all_images', help='load all executable images, not just the images found in the crashed stack frames', default=False) + option_parser.add_option('--images' , action='store_true', dest='dump_image_list', help='show image list', default=False) + option_parser.add_option('--debug-delay' , type='int', dest='debug_delay', metavar='NSEC', help='pause for NSEC seconds for debugger', default=0) + option_parser.add_option('--crashed-only' , '-c', action='store_true', dest='crashed_only', help='only symbolicate the crashed thread', default=False) + option_parser.add_option('--disasm-depth' , '-d', type='int', dest='disassemble_depth', help='set the depth in stack frames that should be disassembled (default is 1)', default=1) + option_parser.add_option('--disasm-all' , '-D', action='store_true', dest='disassemble_all_threads', help='enabled disassembly of frames on all threads (not just the crashed thread)', default=False) + option_parser.add_option('--disasm-before' , '-B', type='int', dest='disassemble_before', help='the number of instructions to disassemble before the frame PC', default=4) + option_parser.add_option('--disasm-after' , '-A', type='int', dest='disassemble_after', help='the number of instructions to disassemble after the frame PC', default=4) + option_parser.add_option('--source-context', '-C', type='int', metavar='NLINES', dest='source_context', help='show NLINES source lines of source context (default = 4)', default=4) + option_parser.add_option('--source-frames' , type='int', metavar='NFRAMES', dest='source_frames', help='show source for NFRAMES (default = 4)', default=4) + option_parser.add_option('--source-all' , action='store_true', dest='source_all', help='show source for all threads, not just the crashed thread', default=False) + if add_interactive_options: + option_parser.add_option('-i', '--interactive', action='store_true', help='parse all crash logs and enter interactive mode', default=False) + return option_parser + +def SymbolicateCrashLogs(command_args): + description='''Symbolicate one or more darwin crash log files to provide source file and line information, +inlined stack frames back to the concrete functions, and disassemble the location of the crash +for the first frame of the crashed thread. +If this script is imported into the LLDB command interpreter, a "crashlog" command will be added to the interpreter +for use at the LLDB command line. After a crash log has been parsed and symbolicated, a target will have been +created that has all of the shared libraries loaded at the load addresses found in the crash log file. This allows +you to explore the program as if it were stopped at the locations described in the crash log and functions can +be disassembled and lookups can be performed using the addresses found in the crash log.''' + option_parser = CreateSymbolicateCrashLogOptions ('crashlog', description, True) + try: + (options, args) = option_parser.parse_args(command_args) + except: + return + + if options.debug: + print 'command_args = %s' % command_args + print 'options', options + print 'args', args + + if options.debug_delay > 0: + print "Waiting %u seconds for debugger to attach..." % options.debug_delay + time.sleep(options.debug_delay) + error = lldb.SBError() + + if args: + if options.interactive: + interactive_crashlogs(options, args) + else: + for crash_log_file in args: + crash_log = CrashLog(crash_log_file) + SymbolicateCrashLog (crash_log, options) +if __name__ == '__main__': + # Create a new debugger instance + lldb.debugger = lldb.SBDebugger.Create() + SymbolicateCrashLogs (sys.argv[1:]) + lldb.SBDebugger.Destroy (lldb.debugger) +elif getattr(lldb, 'debugger', None): + lldb.debugger.HandleCommand('command script add -f lldb.macosx.crashlog.Symbolicate crashlog') + lldb.debugger.HandleCommand('command script add -f lldb.macosx.crashlog.save_crashlog save_crashlog') + print '"crashlog" and "save_crashlog" command installed, use the "--help" option for detailed help' + diff --git a/examples/python/delta.py b/examples/python/delta.py new file mode 100755 index 000000000000..e470de536d85 --- /dev/null +++ b/examples/python/delta.py @@ -0,0 +1,115 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# This module will enable GDB remote packet logging when the +# 'start_gdb_log' command is called with a filename to log to. When the +# 'stop_gdb_log' command is called, it will disable the logging and +# print out statistics about how long commands took to execute and also +# will primnt ou +# Be sure to add the python path that points to the LLDB shared library. +# +# To use this in the embedded python interpreter using "lldb" just +# import it with the full path using the "command script import" +# command. This can be done from the LLDB command line: +# (lldb) command script import /path/to/gdbremote.py +# Or it can be added to your ~/.lldbinit file so this module is always +# available. +#---------------------------------------------------------------------- + +import commands +import optparse +import os +import shlex +import re +import tempfile + +def start_gdb_log(debugger, command, result, dict): + '''Start logging GDB remote packets by enabling logging with timestamps and + thread safe logging. Follow a call to this function with a call to "stop_gdb_log" + in order to dump out the commands.''' + global log_file + if log_file: + result.PutCString ('error: logging is already in progress with file "%s"', log_file) + else: + args_len = len(args) + if args_len == 0: + log_file = tempfile.mktemp() + elif len(args) == 1: + log_file = args[0] + + if log_file: + debugger.HandleCommand('log enable --threadsafe --timestamp --file "%s" gdb-remote packets' % log_file); + result.PutCString ("GDB packet logging enable with log file '%s'\nUse the 'stop_gdb_log' command to stop logging and show packet statistics." % log_file) + return + + result.PutCString ('error: invalid log file path') + result.PutCString (usage) + +def parse_time_log(debugger, command, result, dict): + # Any commands whose names might be followed by more valid C identifier + # characters must be listed here + command_args = shlex.split(command) + parse_time_log_args (command_args) + +def parse_time_log_args(command_args): + usage = "usage: parse_time_log [options] [<LOGFILEPATH>]" + description='''Parse a log file that contains timestamps and convert the timestamps to delta times between log lines.''' + parser = optparse.OptionParser(description=description, prog='parse_time_log',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + try: + (options, args) = parser.parse_args(command_args) + except: + return + for log_file in args: + parse_log_file (log_file, options) + +def parse_log_file(file, options): + '''Parse a log file that was contains timestamps. These logs are typically + generated using: + (lldb) log enable --threadsafe --timestamp --file <FILE> .... + + This log file will contain timestamps and this function will then normalize + those packets to be relative to the first value timestamp that is found and + show delta times between log lines and also keep track of how long it takes + for GDB remote commands to make a send/receive round trip. This can be + handy when trying to figure out why some operation in the debugger is taking + a long time during a preset set of debugger commands.''' + + print '#----------------------------------------------------------------------' + print "# Log file: '%s'" % file + print '#----------------------------------------------------------------------' + + timestamp_regex = re.compile('(\s*)([1-9][0-9]+\.[0-9]+)([^0-9].*)$') + + base_time = 0.0 + last_time = 0.0 + file = open(file) + lines = file.read().splitlines() + for line in lines: + match = timestamp_regex.match (line) + if match: + curr_time = float (match.group(2)) + delta = 0.0 + if base_time: + delta = curr_time - last_time + else: + base_time = curr_time + + print '%s%.6f %+.6f%s' % (match.group(1), curr_time - base_time, delta, match.group(3)) + last_time = curr_time + else: + print line + + + +if __name__ == '__main__': + import sys + parse_time_log_args (sys.argv[1:]) + +else: + import lldb + if lldb.debugger: + # This initializer is being run from LLDB in the embedded command interpreter + # Add any commands contained in this module to LLDB + lldb.debugger.HandleCommand('command script add -f delta.parse_time_log parse_time_log') + print 'The "parse_time_log" command is now installed and ready for use, type "parse_time_log --help" for more information' diff --git a/examples/python/diagnose_nsstring.py b/examples/python/diagnose_nsstring.py new file mode 100644 index 000000000000..aca5c7f220fc --- /dev/null +++ b/examples/python/diagnose_nsstring.py @@ -0,0 +1,171 @@ +# This implements the "diagnose-nsstring" command, usually installed in the debug session like +# command script import lldb.diagnose +# it is used when NSString summary formatter fails to replicate the logic that went into LLDB making the +# decisions it did and providing some useful context information that can be used for improving the formatter + +import lldb + +def read_memory(process,location,size): + data = "" + error = lldb.SBError() + for x in range(0,size-1): + byte = process.ReadUnsignedFromMemory(x+location,1,error) + if error.fail: + data = data + "err%s" % "" if x == size-2 else ":" + else: + try: + data = data + "0x%x" % byte + if byte == 0: + data = data + "(\\0)" + elif byte == 0xa: + data = data + "(\\a)" + elif byte == 0xb: + data = data + "(\\b)" + elif byte == 0xc: + data = data + "(\\c)" + elif byte == '\n': + data = data + "(\\n)" + else: + data = data + "(%s)" % chr(byte) + if x < size-2: + data = data + ":" + except Exception as e: + print e + return data + +def diagnose_nsstring_Command_Impl(debugger,command,result,internal_dict): + """ + A command to diagnose the LLDB NSString data formatter + invoke as + (lldb) diagnose-nsstring <expr returning NSString> + e.g. + (lldb) diagnose-nsstring @"Hello world" + """ + target = debugger.GetSelectedTarget() + process = target.GetProcess() + thread = process.GetSelectedThread() + frame = thread.GetSelectedFrame() + if not target.IsValid() or not process.IsValid(): + return "unable to get target/process - cannot proceed" + options = lldb.SBExpressionOptions() + options.SetFetchDynamicValue() + error = lldb.SBError() + if frame.IsValid(): + nsstring = frame.EvaluateExpression(command,options) + else: + nsstring = target.EvaluateExpression(command,options) + print >>result,str(nsstring) + nsstring_address = nsstring.GetValueAsUnsigned(0) + if nsstring_address == 0: + return "unable to obtain the string - cannot proceed" + expression = "\ +struct $__lldb__notInlineMutable {\ + char* buffer;\ + signed long length;\ + signed long capacity;\ + unsigned int hasGap:1;\ + unsigned int isFixedCapacity:1;\ + unsigned int isExternalMutable:1;\ + unsigned int capacityProvidedExternally:1;\n\ +#if __LP64__\n\ + unsigned long desiredCapacity:60;\n\ +#else\n\ + unsigned long desiredCapacity:28;\n\ +#endif\n\ + void* contentsAllocator;\ +};\ +\ +struct $__lldb__CFString {\ + void* _cfisa;\ + uint8_t _cfinfo[4];\ + uint32_t _rc;\ + union {\ + struct __inline1 {\ + signed long length;\ + } inline1;\ + struct __notInlineImmutable1 {\ + char* buffer;\ + signed long length;\ + void* contentsDeallocator;\ + } notInlineImmutable1;\ + struct __notInlineImmutable2 {\ + char* buffer;\ + void* contentsDeallocator;\ + } notInlineImmutable2;\ + struct $__lldb__notInlineMutable notInlineMutable;\ + } variants;\ +};\ +" + + expression = expression + "*(($__lldb__CFString*) %d)" % nsstring_address + # print expression + dumped = target.EvaluateExpression(expression,options) + print >>result, str(dumped) + + little_endian = (target.byte_order == lldb.eByteOrderLittle) + ptr_size = target.addr_size + + info_bits = dumped.GetChildMemberWithName("_cfinfo").GetChildAtIndex(0 if little_endian else 3).GetValueAsUnsigned(0) + is_mutable = (info_bits & 1) == 1 + is_inline = (info_bits & 0x60) == 0 + has_explicit_length = (info_bits & (1 | 4)) != 4 + is_unicode = (info_bits & 0x10) == 0x10 + is_special = (nsstring.GetDynamicValue(lldb.eDynamicCanRunTarget).GetTypeName() == "NSPathStore2") + has_null = (info_bits & 8) == 8 + + print >>result,"\nInfo=%d\nMutable=%s\nInline=%s\nExplicit=%s\nUnicode=%s\nSpecial=%s\nNull=%s\n" % \ + (info_bits, "yes" if is_mutable else "no","yes" if is_inline else "no","yes" if has_explicit_length else "no","yes" if is_unicode else "no","yes" if is_special else "no","yes" if has_null else "no") + + + explicit_length_offset = 0 + if not has_null and has_explicit_length and not is_special: + explicit_length_offset = 2*ptr_size + if is_mutable and not is_inline: + explicit_length_offset = explicit_length_offset + ptr_size + elif is_inline: + pass + elif not is_inline and not is_mutable: + explicit_length_offset = explicit_length_offset + ptr_size + else: + explicit_length_offset = 0 + + if explicit_length_offset == 0: + print >>result,"There is no explicit length marker - skipping this step\n" + else: + explicit_length_offset = nsstring_address + explicit_length_offset + explicit_length = process.ReadUnsignedFromMemory(explicit_length_offset, 4, error) + print >>result,"Explicit length location is at 0x%x - read value is %d\n" % (explicit_length_offset,explicit_length) + + if is_mutable: + location = 2 * ptr_size + nsstring_address + location = process.ReadPointerFromMemory(location,error) + elif is_inline and has_explicit_length and not is_unicode and not is_special and not is_mutable: + location = 3 * ptr_size + nsstring_address + elif is_unicode: + location = 2 * ptr_size + nsstring_address + if is_inline: + if not has_explicit_length: + print >>result,"Unicode & Inline & !Explicit is a new combo - no formula for it" + else: + location += ptr_size + else: + location = process.ReadPointerFromMemory(location,error) + elif is_special: + location = nsstring_address + ptr_size + 4 + elif is_inline: + location = 2 * ptr_size + nsstring_address + if not has_explicit_length: + location += 1 + else: + location = 2 * ptr_size + nsstring_address + location = process.ReadPointerFromMemory(location,error) + print >>result,"Expected data location: 0x%x\n" % (location) + print >>result,"1K of data around location: %s\n" % read_memory(process,location,1024) + print >>result,"5K of data around string pointer: %s\n" % read_memory(process,nsstring_address,1024*5) + +def __lldb_init_module(debugger, internal_dict): + debugger.HandleCommand("command script add -f %s.diagnose_nsstring_Command_Impl diagnose-nsstring" % __name__) + print 'The "diagnose-nsstring" command has been installed, type "help diagnose-nsstring" for detailed help.' + +__lldb_init_module(lldb.debugger,None) +__lldb_init_module = None
\ No newline at end of file diff --git a/examples/python/diagnose_unwind.py b/examples/python/diagnose_unwind.py new file mode 100644 index 000000000000..e977c4ed1b0f --- /dev/null +++ b/examples/python/diagnose_unwind.py @@ -0,0 +1,270 @@ +# This implements the "diagnose-unwind" command, usually installed +# in the debug session like +# command script import lldb.diagnose +# it is used when lldb's backtrace fails -- it collects and prints +# information about the stack frames, and tries an alternate unwind +# algorithm, that will help to understand why lldb's unwind algorithm +# did not succeed. + +import optparse +import lldb +import re +import shlex + +# Print the frame number, pc, frame pointer, module UUID and function name +# Returns the SBModule that contains the PC, if it could be found +def backtrace_print_frame (target, frame_num, addr, fp): + process = target.GetProcess() + addr_for_printing = addr + addr_width = process.GetAddressByteSize() * 2 + if frame_num > 0: + addr = addr - 1 + + sbaddr = lldb.SBAddress() + try: + sbaddr.SetLoadAddress(addr, target) + module_description = "" + if sbaddr.GetModule(): + module_filename = "" + module_uuid_str = sbaddr.GetModule().GetUUIDString() + if module_uuid_str == None: + module_uuid_str = "" + if sbaddr.GetModule().GetFileSpec(): + module_filename = sbaddr.GetModule().GetFileSpec().GetFilename() + if module_filename == None: + module_filename = "" + if module_uuid_str != "" or module_filename != "": + module_description = '%s %s' % (module_filename, module_uuid_str) + except Exception: + print '%2d: pc==0x%-*x fp==0x%-*x' % (frame_num, addr_width, addr_for_printing, addr_width, fp) + return + + sym_ctx = target.ResolveSymbolContextForAddress(sbaddr, lldb.eSymbolContextEverything) + if sym_ctx.IsValid() and sym_ctx.GetSymbol().IsValid(): + function_start = sym_ctx.GetSymbol().GetStartAddress().GetLoadAddress(target) + offset = addr - function_start + print '%2d: pc==0x%-*x fp==0x%-*x %s %s + %d' % (frame_num, addr_width, addr_for_printing, addr_width, fp, module_description, sym_ctx.GetSymbol().GetName(), offset) + else: + print '%2d: pc==0x%-*x fp==0x%-*x %s' % (frame_num, addr_width, addr_for_printing, addr_width, fp, module_description) + return sbaddr.GetModule() + +# A simple stack walk algorithm that follows the frame chain. +# Returns a two-element list; the first element is a list of modules +# seen and the second element is a list of addresses seen during the backtrace. +def simple_backtrace(debugger): + target = debugger.GetSelectedTarget() + process = target.GetProcess() + cur_thread = process.GetSelectedThread() + + initial_fp = cur_thread.GetFrameAtIndex(0).GetFP() + + # If the pseudoreg "fp" isn't recognized, on arm hardcode to r7 which is correct for Darwin programs. + if initial_fp == lldb.LLDB_INVALID_ADDRESS and target.triple[0:3] == "arm": + for reggroup in cur_thread.GetFrameAtIndex(1).registers: + if reggroup.GetName() == "General Purpose Registers": + for reg in reggroup: + if reg.GetName() == "r7": + initial_fp = int (reg.GetValue(), 16) + + module_list = [] + address_list = [cur_thread.GetFrameAtIndex(0).GetPC()] + this_module = backtrace_print_frame (target, 0, cur_thread.GetFrameAtIndex(0).GetPC(), initial_fp) + print_stack_frame (process, initial_fp) + print "" + if this_module != None: + module_list.append (this_module) + if cur_thread.GetNumFrames() < 2: + return [module_list, address_list] + + cur_fp = process.ReadPointerFromMemory (initial_fp, lldb.SBError()) + cur_pc = process.ReadPointerFromMemory (initial_fp + process.GetAddressByteSize(), lldb.SBError()) + + frame_num = 1 + + while cur_pc != 0 and cur_fp != 0 and cur_pc != lldb.LLDB_INVALID_ADDRESS and cur_fp != lldb.LLDB_INVALID_ADDRESS: + address_list.append (cur_pc) + this_module = backtrace_print_frame (target, frame_num, cur_pc, cur_fp) + print_stack_frame (process, cur_fp) + print "" + if this_module != None: + module_list.append (this_module) + frame_num = frame_num + 1 + next_pc = 0 + next_fp = 0 + if target.triple[0:6] == "x86_64" or target.triple[0:4] == "i386" or target.triple[0:3] == "arm": + error = lldb.SBError() + next_pc = process.ReadPointerFromMemory(cur_fp + process.GetAddressByteSize(), error) + if not error.Success(): + next_pc = 0 + next_fp = process.ReadPointerFromMemory(cur_fp, error) + if not error.Success(): + next_fp = 0 + # Clear the 0th bit for arm frames - this indicates it is a thumb frame + if target.triple[0:3] == "arm" and (next_pc & 1) == 1: + next_pc = next_pc & ~1 + cur_pc = next_pc + cur_fp = next_fp + this_module = backtrace_print_frame (target, frame_num, cur_pc, cur_fp) + print_stack_frame (process, cur_fp) + print "" + if this_module != None: + module_list.append (this_module) + return [module_list, address_list] + +def print_stack_frame(process, fp): + if fp == 0 or fp == lldb.LLDB_INVALID_ADDRESS or fp == 1: + return + addr_size = process.GetAddressByteSize() + addr = fp - (2 * addr_size) + i = 0 + outline = "Stack frame from $fp-%d: " % (2 * addr_size) + error = lldb.SBError() + try: + while i < 5 and error.Success(): + address = process.ReadPointerFromMemory(addr + (i * addr_size), error) + outline += " 0x%x" % address + i += 1 + print outline + except Exception: + return + +def diagnose_unwind(debugger, command, result, dict): + """ +Gather diagnostic information to help debug incorrect unwind (backtrace) +behavior in lldb. When there is a backtrace that doesn't look +correct, run this command with the correct thread selected and a +large amount of diagnostic information will be printed, it is likely +to be helpful when reporting the problem. + """ + + command_args = shlex.split(command) + parser = create_diagnose_unwind_options() + try: + (options, args) = parser.parse_args(command_args) + except: + return + target = debugger.GetSelectedTarget() + if target: + process = target.GetProcess() + if process: + thread = process.GetSelectedThread() + if thread: + lldb_versions_match = re.search(r'[lL][lL][dD][bB]-(\d+)([.](\d+))?([.](\d+))?', debugger.GetVersionString()) + lldb_version = 0 + lldb_minor = 0 + if len(lldb_versions_match.groups()) >= 1 and lldb_versions_match.groups()[0]: + lldb_major = int(lldb_versions_match.groups()[0]) + if len(lldb_versions_match.groups()) >= 5 and lldb_versions_match.groups()[4]: + lldb_minor = int(lldb_versions_match.groups()[4]) + + modules_seen = [] + addresses_seen = [] + + print 'LLDB version %s' % debugger.GetVersionString() + print 'Unwind diagnostics for thread %d' % thread.GetIndexID() + print "" + print "=============================================================================================" + print "" + print "OS plugin setting:" + debugger.HandleCommand("settings show target.process.python-os-plugin-path") + print "" + print "Live register context:" + thread.SetSelectedFrame(0) + debugger.HandleCommand("register read") + print "" + print "=============================================================================================" + print "" + print "lldb's unwind algorithm:" + print "" + frame_num = 0 + for frame in thread.frames: + if not frame.IsInlined(): + this_module = backtrace_print_frame (target, frame_num, frame.GetPC(), frame.GetFP()) + print_stack_frame (process, frame.GetFP()) + print "" + if this_module != None: + modules_seen.append (this_module) + addresses_seen.append (frame.GetPC()) + frame_num = frame_num + 1 + print "" + print "=============================================================================================" + print "" + print "Simple stack walk algorithm:" + print "" + (module_list, address_list) = simple_backtrace(debugger) + if module_list and module_list != None: + modules_seen += module_list + if address_list and address_list != None: + addresses_seen = set(addresses_seen) + addresses_seen.update(set(address_list)) + + print "" + print "=============================================================================================" + print "" + print "Modules seen in stack walks:" + print "" + modules_already_seen = set() + for module in modules_seen: + if module != None and module.GetFileSpec().GetFilename() != None: + if not module.GetFileSpec().GetFilename() in modules_already_seen: + debugger.HandleCommand('image list %s' % module.GetFileSpec().GetFilename()) + modules_already_seen.add(module.GetFileSpec().GetFilename()) + + print "" + print "=============================================================================================" + print "" + print "Disassembly ofaddresses seen in stack walks:" + print "" + additional_addresses_to_disassemble = addresses_seen + for frame in thread.frames: + if not frame.IsInlined(): + print "--------------------------------------------------------------------------------------" + print "" + print "Disassembly of %s, frame %d, address 0x%x" % (frame.GetFunctionName(), frame.GetFrameID(), frame.GetPC()) + print "" + if target.triple[0:6] == "x86_64" or target.triple[0:4] == "i386": + debugger.HandleCommand('disassemble -F att -a 0x%x' % frame.GetPC()) + else: + debugger.HandleCommand('disassemble -a 0x%x' % frame.GetPC()) + if frame.GetPC() in additional_addresses_to_disassemble: + additional_addresses_to_disassemble.remove (frame.GetPC()) + + for address in list(additional_addresses_to_disassemble): + print "--------------------------------------------------------------------------------------" + print "" + print "Disassembly of 0x%x" % address + print "" + if target.triple[0:6] == "x86_64" or target.triple[0:4] == "i386": + debugger.HandleCommand('disassemble -F att -a 0x%x' % address) + else: + debugger.HandleCommand('disassemble -a 0x%x' % address) + + print "" + print "=============================================================================================" + print "" + additional_addresses_to_show_unwind = addresses_seen + for frame in thread.frames: + if not frame.IsInlined(): + print "--------------------------------------------------------------------------------------" + print "" + print "Unwind instructions for %s, frame %d" % (frame.GetFunctionName(), frame.GetFrameID()) + print "" + debugger.HandleCommand('image show-unwind -a "0x%x"' % frame.GetPC()) + if frame.GetPC() in additional_addresses_to_show_unwind: + additional_addresses_to_show_unwind.remove (frame.GetPC()) + + for address in list(additional_addresses_to_show_unwind): + print "--------------------------------------------------------------------------------------" + print "" + print "Unwind instructions for 0x%x" % address + print "" + debugger.HandleCommand('image show-unwind -a "0x%x"' % address) + +def create_diagnose_unwind_options(): + usage = "usage: %prog" + description='''Print diagnostic information about a thread backtrace which will help to debug unwind problems''' + parser = optparse.OptionParser(description=description, prog='diagnose_unwind',usage=usage) + return parser + +lldb.debugger.HandleCommand('command script add -f %s.diagnose_unwind diagnose-unwind' % __name__) +print 'The "diagnose-unwind" command has been installed, type "help diagnose-unwind" for detailed help.' diff --git a/examples/python/dict_utils.py b/examples/python/dict_utils.py new file mode 100755 index 000000000000..7dc5e7a8b56e --- /dev/null +++ b/examples/python/dict_utils.py @@ -0,0 +1,61 @@ + +class LookupDictionary(dict): + """ + a dictionary which can lookup value by key, or keys by value + """ + def __init__(self, items=[]): + """items can be a list of pair_lists or a dictionary""" + dict.__init__(self, items) + + def get_keys_for_value(self, value, fail_value = None): + """find the key(s) as a list given a value""" + list_result = [item[0] for item in self.items() if item[1] == value] + if len(list_result) > 0: + return list_result + return fail_value + + def get_first_key_for_value(self, value, fail_value = None): + """return the first key of this dictionary given the value""" + list_result = [item[0] for item in self.items() if item[1] == value] + if len(list_result) > 0: + return list_result[0] + return fail_value + + def get_value(self, key, fail_value = None): + """find the value given a key""" + if key in self: + return self[key] + return fail_value + + +class Enum(LookupDictionary): + + def __init__(self, initial_value=0, items=[]): + """items can be a list of pair_lists or a dictionary""" + LookupDictionary.__init__(self, items) + self.value = initial_value + + def set_value(self, v): + v_typename = typeof(v).__name__ + if v_typename == 'str': + if str in self: + v = self[v] + else: + v = 0 + else: + self.value = v + + def get_enum_value(self): + return self.value + + def get_enum_name(self): + return self.__str__() + + def __str__(self): + s = self.get_first_key_for_value (self.value, None) + if s == None: + s = "%#8.8x" % self.value + return s + + def __repr__(self): + return self.__str__()
\ No newline at end of file diff --git a/examples/python/disasm-stress-test.py b/examples/python/disasm-stress-test.py new file mode 100755 index 000000000000..5aa354dc24cb --- /dev/null +++ b/examples/python/disasm-stress-test.py @@ -0,0 +1,168 @@ +#!/usr/bin/python + +import argparse, datetime, re, subprocess, sys, time + +parser = argparse.ArgumentParser(description="Run an exhaustive test of the LLDB disassembler for a specific architecture.") + +parser.add_argument('--arch', required=True, action='store', help='The architecture whose disassembler is to be tested') +parser.add_argument('--bytes', required=True, action='store', type=int, help='The byte width of instructions for that architecture') +parser.add_argument('--random', required=False, action='store_true', help='Enables non-sequential testing') +parser.add_argument('--start', required=False, action='store', type=int, help='The first instruction value to test') +parser.add_argument('--skip', required=False, action='store', type=int, help='The interval between instructions to test') +parser.add_argument('--log', required=False, action='store', help='A log file to write the most recent instruction being tested') +parser.add_argument('--time', required=False, action='store_true', help='Every 100,000 instructions, print an ETA to standard out') +parser.add_argument('--lldb', required=False, action='store', help='The path to LLDB.framework, if LLDB should be overridden') + +arguments = sys.argv[1:] + +arg_ns = parser.parse_args(arguments) + +def AddLLDBToSysPathOnMacOSX(): + def GetLLDBFrameworkPath(): + lldb_path = subprocess.check_output(["xcrun", "-find", "lldb"]) + re_result = re.match("(.*)/Developer/usr/bin/lldb", lldb_path) + if re_result == None: + return None + xcode_contents_path = re_result.group(1) + return xcode_contents_path + "/SharedFrameworks/LLDB.framework" + + lldb_framework_path = GetLLDBFrameworkPath() + + if lldb_framework_path == None: + print "Couldn't find LLDB.framework" + sys.exit(-1) + + sys.path.append(lldb_framework_path + "/Resources/Python") + +if arg_ns.lldb == None: + AddLLDBToSysPathOnMacOSX() +else: + sys.path.append(arg_ns.lldb + "/Resources/Python") + +import lldb + +debugger = lldb.SBDebugger.Create() + +if debugger.IsValid() == False: + print "Couldn't create an SBDebugger" + sys.exit(-1) + +target = debugger.CreateTargetWithFileAndArch(None, arg_ns.arch) + +if target.IsValid() == False: + print "Couldn't create an SBTarget for architecture " + arg_ns.arch + sys.exit(-1) + +def ResetLogFile(log_file): + if log_file != sys.stdout: + log_file.seek(0) + +def PrintByteArray(log_file, byte_array): + for byte in byte_array: + print >>log_file, hex(byte) + " ", + print >>log_file + +class SequentialInstructionProvider: + def __init__(self, byte_width, log_file, start=0, skip=1): + self.m_byte_width = byte_width + self.m_log_file = log_file + self.m_start = start + self.m_skip = skip + self.m_value = start + self.m_last = (1 << (byte_width * 8)) - 1 + def PrintCurrentState(self, ret): + ResetLogFile(self.m_log_file) + print >>self.m_log_file, self.m_value + PrintByteArray(self.m_log_file, ret) + def GetNextInstruction(self): + if self.m_value > self.m_last: + return None + ret = bytearray(self.m_byte_width) + for i in range(self.m_byte_width): + ret[self.m_byte_width - (i + 1)] = (self.m_value >> (i * 8)) & 255 + self.PrintCurrentState(ret) + self.m_value += self.m_skip + return ret + def GetNumInstructions(self): + return (self.m_last - self.m_start) / self.m_skip + def __iter__(self): + return self + def next(self): + ret = self.GetNextInstruction() + if ret == None: + raise StopIteration + return ret + +class RandomInstructionProvider: + def __init__(self, byte_width, log_file): + self.m_byte_width = byte_width + self.m_log_file = log_file + self.m_random_file = open("/dev/random", 'r') + def PrintCurrentState(self, ret): + ResetLogFile(self.m_log_file) + PrintByteArray(self.m_log_file, ret) + def GetNextInstruction(self): + ret = bytearray(self.m_byte_width) + for i in range(self.m_byte_width): + ret[i] = self.m_random_file.read(1) + self.PrintCurrentState(ret) + return ret + def __iter__(self): + return self + def next(self): + ret = self.GetNextInstruction() + if ret == None: + raise StopIteration + return ret + +log_file = None + +def GetProviderWithArguments(args): + global log_file + if args.log != None: + log_file = open(args.log, 'w') + else: + log_file = sys.stdout + instruction_provider = None + if args.random == True: + instruction_provider = RandomInstructionProvider(args.bytes, log_file) + else: + start = 0 + skip = 1 + if args.start != None: + start = args.start + if args.skip != None: + skip = args.skip + instruction_provider = SequentialInstructionProvider(args.bytes, log_file, start, skip) + return instruction_provider + +instruction_provider = GetProviderWithArguments(arg_ns) + +fake_address = lldb.SBAddress() + +actually_time = arg_ns.time and not arg_ns.random + +if actually_time: + num_instructions_logged = 0 + total_num_instructions = instruction_provider.GetNumInstructions() + start_time = time.time() + +for inst_bytes in instruction_provider: + if actually_time: + if (num_instructions_logged != 0) and (num_instructions_logged % 100000 == 0): + curr_time = time.time() + elapsed_time = curr_time - start_time + remaining_time = float(total_num_instructions - num_instructions_logged) * (float(elapsed_time) / float(num_instructions_logged)) + print str(datetime.timedelta(seconds=remaining_time)) + num_instructions_logged = num_instructions_logged + 1 + inst_list = target.GetInstructions(fake_address, inst_bytes) + if not inst_list.IsValid(): + print >>log_file, "Invalid instruction list" + continue + inst = inst_list.GetInstructionAtIndex(0) + if not inst.IsValid(): + print >>log_file, "Invalid instruction" + continue + instr_output_stream = lldb.SBStream() + inst.GetDescription(instr_output_stream) + print >>log_file, instr_output_stream.GetData() diff --git a/examples/python/disasm.py b/examples/python/disasm.py new file mode 100755 index 000000000000..732cf106b11d --- /dev/null +++ b/examples/python/disasm.py @@ -0,0 +1,119 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# Be sure to add the python path that points to the LLDB shared library. +# On MacOSX csh, tcsh: +# setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python +# On MacOSX sh, bash: +# export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python +#---------------------------------------------------------------------- + +import lldb +import os +import sys + +def disassemble_instructions (insts): + for i in insts: + print i + +def usage(): + print "Usage: disasm.py [-n name] executable-image" + print " By default, it breaks at and disassembles the 'main' function." + sys.exit(0) + +if len(sys.argv) == 2: + fname = 'main' + exe = sys.argv[1] +elif len(sys.argv) == 4: + if sys.argv[1] != '-n': + usage() + else: + fname = sys.argv[2] + exe = sys.argv[3] +else: + usage() + +# Create a new debugger instance +debugger = lldb.SBDebugger.Create() + +# When we step or continue, don't return from the function until the process +# stops. We do this by setting the async mode to false. +debugger.SetAsync (False) + +# Create a target from a file and arch +print "Creating a target for '%s'" % exe + +target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT) + +if target: + # If the target is valid set a breakpoint at main + main_bp = target.BreakpointCreateByName (fname, target.GetExecutable().GetFilename()); + + print main_bp + + # Launch the process. Since we specified synchronous mode, we won't return + # from this function until we hit the breakpoint at main + process = target.LaunchSimple (None, None, os.getcwd()) + + # Make sure the launch went ok + if process: + # Print some simple process info + state = process.GetState () + print process + if state == lldb.eStateStopped: + # Get the first thread + thread = process.GetThreadAtIndex (0) + if thread: + # Print some simple thread info + print thread + # Get the first frame + frame = thread.GetFrameAtIndex (0) + if frame: + # Print some simple frame info + print frame + function = frame.GetFunction() + # See if we have debug info (a function) + if function: + # We do have a function, print some info for the function + print function + # Now get all instructions for this function and print them + insts = function.GetInstructions(target) + disassemble_instructions (insts) + else: + # See if we have a symbol in the symbol table for where we stopped + symbol = frame.GetSymbol(); + if symbol: + # We do have a symbol, print some info for the symbol + print symbol + # Now get all instructions for this symbol and print them + insts = symbol.GetInstructions(target) + disassemble_instructions (insts) + + registerList = frame.GetRegisters() + print "Frame registers (size of register set = %d):" % registerList.GetSize() + for value in registerList: + #print value + print "%s (number of children = %d):" % (value.GetName(), value.GetNumChildren()) + for child in value: + print "Name: ", child.GetName(), " Value: ", child.GetValue() + + print "Hit the breakpoint at main, enter to continue and wait for program to exit or 'Ctrl-D'/'quit' to terminate the program" + next = sys.stdin.readline() + if not next or next.rstrip('\n') == 'quit': + print "Terminating the inferior process..." + process.Kill() + else: + # Now continue to the program exit + process.Continue() + # When we return from the above function we will hopefully be at the + # program exit. Print out some process info + print process + elif state == lldb.eStateExited: + print "Didn't hit the breakpoint at main, program has exited..." + else: + print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state) + process.Kill() + + + +lldb.SBDebugger.Terminate() diff --git a/examples/python/file_extract.py b/examples/python/file_extract.py new file mode 100755 index 000000000000..3afc0c3c1a0b --- /dev/null +++ b/examples/python/file_extract.py @@ -0,0 +1,221 @@ +#! /usr/bin/env python + +import string +import struct +import sys + +class FileExtract: + '''Decode binary data from a file''' + + def __init__(self, f, b = '='): + '''Initialize with an open binary file and optional byte order''' + + self.file = f + self.byte_order = b + self.offsets = list() + + def set_byte_order(self, b): + '''Set the byte order, valid values are "big", "little", "swap", "native", "<", ">", "@", "="''' + if b == 'big': + self.byte_order = '>' + elif b == 'little': + self.byte_order = '<' + elif b == 'swap': + # swap what ever the current byte order is + self.byte_order = swap_unpack_char() + elif b == 'native': + self.byte_order = '=' + elif b == '<' or b == '>' or b == '@' or b == '=': + self.byte_order = b + else: + print "error: invalid byte order specified: '%s'" % b + + def is_in_memory(self): + return False + + def seek(self, offset, whence = 0): + if self.file: + return self.file.seek(offset, whence) + raise ValueError + + def tell(self): + if self.file: + return self.file.tell() + raise ValueError + + def read_size (self, byte_size): + s = self.file.read(byte_size) + if len(s) != byte_size: + return None + return s + + def push_offset_and_seek(self, offset): + '''Push the current file offset and seek to "offset"''' + self.offsets.append(self.file.tell()) + self.file.seek(offset, 0) + + def pop_offset_and_seek(self): + '''Pop a previously pushed file offset, or do nothing if there were no previously pushed offsets''' + if len(self.offsets) > 0: + self.file.seek(self.offsets.pop()) + + def get_sint8(self, fail_value=0): + '''Extract a single int8_t from the binary file at the current file position, returns a single integer''' + s = self.read_size(1) + if s: + v, = struct.unpack(self.byte_order + 'b', s) + return v + else: + return fail_value + + def get_uint8(self, fail_value=0): + '''Extract a single uint8_t from the binary file at the current file position, returns a single integer''' + s = self.read_size(1) + if s: + v, = struct.unpack(self.byte_order + 'B', s) + return v + else: + return fail_value + + def get_sint16(self, fail_value=0): + '''Extract a single int16_t from the binary file at the current file position, returns a single integer''' + s = self.read_size(2) + if s: + v, = struct.unpack(self.byte_order + 'h', s) + return v + else: + return fail_value + + def get_uint16(self, fail_value=0): + '''Extract a single uint16_t from the binary file at the current file position, returns a single integer''' + s = self.read_size(2) + if s: + v, = struct.unpack(self.byte_order + 'H', s) + return v + else: + return fail_value + + def get_sint32(self, fail_value=0): + '''Extract a single int32_t from the binary file at the current file position, returns a single integer''' + s = self.read_size(4) + if s: + v, = struct.unpack(self.byte_order + 'i', s) + return v + else: + return fail_value + + def get_uint32(self, fail_value=0): + '''Extract a single uint32_t from the binary file at the current file position, returns a single integer''' + s = self.read_size(4) + if s: + v, = struct.unpack(self.byte_order + 'I', s) + return v + else: + return fail_value + + def get_sint64(self, fail_value=0): + '''Extract a single int64_t from the binary file at the current file position, returns a single integer''' + s = self.read_size(8) + if s: + v, = struct.unpack(self.byte_order + 'q', s) + return v + else: + return fail_value + + def get_uint64(self, fail_value=0): + '''Extract a single uint64_t from the binary file at the current file position, returns a single integer''' + s = self.read_size(8) + if s: + v, = struct.unpack(self.byte_order + 'Q', s) + return v + else: + return fail_value + + def get_fixed_length_c_string(self, n, fail_value='', isprint_only_with_space_padding=False): + '''Extract a single fixed length C string from the binary file at the current file position, returns a single C string''' + s = self.read_size(n) + if s: + cstr, = struct.unpack(self.byte_order + ("%i" % n) + 's', s) + # Strip trialing NULLs + cstr = string.strip(cstr, "\0") + if isprint_only_with_space_padding: + for c in cstr: + if c in string.printable or ord(c) == 0: + continue + return fail_value + return cstr + else: + return fail_value + + def get_c_string(self): + '''Extract a single NULL terminated C string from the binary file at the current file position, returns a single C string''' + cstr = '' + byte = self.get_uint8() + while byte != 0: + cstr += "%c" % byte + byte = self.get_uint8() + return cstr + + def get_n_sint8(self, n, fail_value=0): + '''Extract "n" int8_t integers from the binary file at the current file position, returns a list of integers''' + s = self.read_size(n) + if s: + return struct.unpack(self.byte_order + ("%u" % n) + 'b', s) + else: + return (fail_value,) * n + + def get_n_uint8(self, n, fail_value=0): + '''Extract "n" uint8_t integers from the binary file at the current file position, returns a list of integers''' + s = self.read_size(n) + if s: + return struct.unpack(self.byte_order + ("%u" % n) + 'B', s) + else: + return (fail_value,) * n + + def get_n_sint16(self, n, fail_value=0): + '''Extract "n" int16_t integers from the binary file at the current file position, returns a list of integers''' + s = self.read_size(2*n) + if s: + return struct.unpack(self.byte_order + ("%u" % n) + 'h', s) + else: + return (fail_value,) * n + + def get_n_uint16(self, n, fail_value=0): + '''Extract "n" uint16_t integers from the binary file at the current file position, returns a list of integers''' + s = self.read_size(2*n) + if s: + return struct.unpack(self.byte_order + ("%u" % n) + 'H', s) + else: + return (fail_value,) * n + + def get_n_sint32(self, n, fail_value=0): + '''Extract "n" int32_t integers from the binary file at the current file position, returns a list of integers''' + s = self.read_size(4*n) + if s: + return struct.unpack(self.byte_order + ("%u" % n) + 'i', s) + else: + return (fail_value,) * n + + def get_n_uint32(self, n, fail_value=0): + '''Extract "n" uint32_t integers from the binary file at the current file position, returns a list of integers''' + s = self.read_size(4*n) + if s: + return struct.unpack(self.byte_order + ("%u" % n) + 'I', s) + else: + return (fail_value,) * n + + def get_n_sint64(self, n, fail_value=0): + '''Extract "n" int64_t integers from the binary file at the current file position, returns a list of integers''' + s = self.read_size(8*n) + if s: + return struct.unpack(self.byte_order + ("%u" % n) + 'q', s) + else: + return (fail_value,) * n + + def get_n_uint64(self, n, fail_value=0): + '''Extract "n" uint64_t integers from the binary file at the current file position, returns a list of integers''' + s = self.read_size(8*n) + if s: + return struct.unpack(self.byte_order + ("%u" % n) + 'Q', s) + else: + return (fail_value,) * n diff --git a/examples/python/gdb_disassemble.py b/examples/python/gdb_disassemble.py new file mode 100755 index 000000000000..d9a2f212fc9a --- /dev/null +++ b/examples/python/gdb_disassemble.py @@ -0,0 +1,24 @@ +import lldb + +def disassemble(debugger, command, result, dict): + if lldb.frame.function: + instructions = lldb.frame.function.instructions + start_addr = lldb.frame.function.addr.load_addr + name = lldb.frame.function.name + elif lldb.frame.symbol: + instructions = lldb.frame.symbol.instructions + start_addr = lldb.frame.symbol.addr.load_addr + name = lldb.frame.symbol.name + + for inst in instructions: + inst_addr = inst.addr.load_addr + inst_offset = inst_addr - start_addr + comment = inst.comment + if comment: + print "<%s + %-4u> 0x%x %8s %s ; %s" % (name, inst_offset, inst_addr, inst.mnemonic, inst.operands, comment) + else: + print "<%s + %-4u> 0x%x %8s %s" % (name, inst_offset, inst_addr, inst.mnemonic, inst.operands) + +# Install the command when the module gets imported +lldb.debugger.HandleCommand('command script add -f gdb_disassemble.disassemble gdb-disassemble') +print 'Installed "gdb-disassemble" command for disassembly'
\ No newline at end of file diff --git a/examples/python/gdbremote.py b/examples/python/gdbremote.py new file mode 100755 index 000000000000..4cbfdb2ba333 --- /dev/null +++ b/examples/python/gdbremote.py @@ -0,0 +1,1362 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# This module will enable GDB remote packet logging when the +# 'start_gdb_log' command is called with a filename to log to. When the +# 'stop_gdb_log' command is called, it will disable the logging and +# print out statistics about how long commands took to execute and also +# will primnt ou +# Be sure to add the python path that points to the LLDB shared library. +# +# To use this in the embedded python interpreter using "lldb" just +# import it with the full path using the "command script import" +# command. This can be done from the LLDB command line: +# (lldb) command script import /path/to/gdbremote.py +# Or it can be added to your ~/.lldbinit file so this module is always +# available. +#---------------------------------------------------------------------- + +import binascii +import commands +import json +import math +import optparse +import os +import re +import shlex +import string +import sys +import tempfile +import xml.etree.ElementTree as ET + +#---------------------------------------------------------------------- +# Global variables +#---------------------------------------------------------------------- +g_log_file = '' +g_byte_order = 'little' +g_number_regex = re.compile('^(0x[0-9a-fA-F]+|[0-9]+)') +g_thread_id_regex = re.compile('^(-1|[0-9a-fA-F]+|0)') + +class TerminalColors: + '''Simple terminal colors class''' + def __init__(self, enabled = True): + # TODO: discover terminal type from "file" and disable if + # it can't handle the color codes + self.enabled = enabled + + def reset(self): + '''Reset all terminal colors and formatting.''' + if self.enabled: + return "\x1b[0m"; + return '' + + def bold(self, on = True): + '''Enable or disable bold depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[1m"; + else: + return "\x1b[22m"; + return '' + + def italics(self, on = True): + '''Enable or disable italics depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[3m"; + else: + return "\x1b[23m"; + return '' + + def underline(self, on = True): + '''Enable or disable underline depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[4m"; + else: + return "\x1b[24m"; + return '' + + def inverse(self, on = True): + '''Enable or disable inverse depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[7m"; + else: + return "\x1b[27m"; + return '' + + def strike(self, on = True): + '''Enable or disable strike through depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[9m"; + else: + return "\x1b[29m"; + return '' + + def black(self, fg = True): + '''Set the foreground or background color to black. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[30m"; + else: + return "\x1b[40m"; + return '' + + def red(self, fg = True): + '''Set the foreground or background color to red. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[31m"; + else: + return "\x1b[41m"; + return '' + + def green(self, fg = True): + '''Set the foreground or background color to green. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[32m"; + else: + return "\x1b[42m"; + return '' + + def yellow(self, fg = True): + '''Set the foreground or background color to yellow. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[33m"; + else: + return "\x1b[43m"; + return '' + + def blue(self, fg = True): + '''Set the foreground or background color to blue. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[34m"; + else: + return "\x1b[44m"; + return '' + + def magenta(self, fg = True): + '''Set the foreground or background color to magenta. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[35m"; + else: + return "\x1b[45m"; + return '' + + def cyan(self, fg = True): + '''Set the foreground or background color to cyan. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[36m"; + else: + return "\x1b[46m"; + return '' + + def white(self, fg = True): + '''Set the foreground or background color to white. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[37m"; + else: + return "\x1b[47m"; + return '' + + def default(self, fg = True): + '''Set the foreground or background color to the default. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[39m"; + else: + return "\x1b[49m"; + return '' + + +def start_gdb_log(debugger, command, result, dict): + '''Start logging GDB remote packets by enabling logging with timestamps and + thread safe logging. Follow a call to this function with a call to "stop_gdb_log" + in order to dump out the commands.''' + global g_log_file + command_args = shlex.split(command) + usage = "usage: start_gdb_log [options] [<LOGFILEPATH>]" + description='''The command enables GDB remote packet logging with timestamps. The packets will be logged to <LOGFILEPATH> if supplied, or a temporary file will be used. Logging stops when stop_gdb_log is called and the packet times will + be aggregated and displayed.''' + parser = optparse.OptionParser(description=description, prog='start_gdb_log',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + try: + (options, args) = parser.parse_args(command_args) + except: + return + + if g_log_file: + result.PutCString ('error: logging is already in progress with file "%s"' % g_log_file) + else: + args_len = len(args) + if args_len == 0: + g_log_file = tempfile.mktemp() + elif len(args) == 1: + g_log_file = args[0] + + if g_log_file: + debugger.HandleCommand('log enable --threadsafe --timestamp --file "%s" gdb-remote packets' % g_log_file); + result.PutCString ("GDB packet logging enable with log file '%s'\nUse the 'stop_gdb_log' command to stop logging and show packet statistics." % g_log_file) + return + + result.PutCString ('error: invalid log file path') + result.PutCString (usage) + +def stop_gdb_log(debugger, command, result, dict): + '''Stop logging GDB remote packets to the file that was specified in a call + to "start_gdb_log" and normalize the timestamps to be relative to the first + timestamp in the log file. Also print out statistics for how long each + command took to allow performance bottlenecks to be determined.''' + global g_log_file + # Any commands whose names might be followed by more valid C identifier + # characters must be listed here + command_args = shlex.split(command) + usage = "usage: stop_gdb_log [options]" + description='''The command stops a previously enabled GDB remote packet logging command. Packet logging must have been previously enabled with a call to start_gdb_log.''' + parser = optparse.OptionParser(description=description, prog='stop_gdb_log',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + parser.add_option('-q', '--quiet', action='store_true', dest='quiet', help='display verbose debug info', default=False) + parser.add_option('-C', '--color', action='store_true', dest='color', help='add terminal colors', default=False) + parser.add_option('-c', '--sort-by-count', action='store_true', dest='sort_count', help='display verbose debug info', default=False) + parser.add_option('-s', '--symbolicate', action='store_true', dest='symbolicate', help='symbolicate addresses in log using current "lldb.target"', default=False) + try: + (options, args) = parser.parse_args(command_args) + except: + return + options.colors = TerminalColors(options.color) + options.symbolicator = None + if options.symbolicate: + if lldb.target: + import lldb.utils.symbolication + options.symbolicator = lldb.utils.symbolication.Symbolicator() + options.symbolicator.target = lldb.target + else: + print "error: can't symbolicate without a target" + + if not g_log_file: + result.PutCString ('error: logging must have been previously enabled with a call to "stop_gdb_log"') + elif os.path.exists (g_log_file): + if len(args) == 0: + debugger.HandleCommand('log disable gdb-remote packets'); + result.PutCString ("GDB packet logging disabled. Logged packets are in '%s'" % g_log_file) + parse_gdb_log_file (g_log_file, options) + else: + result.PutCString (usage) + else: + print 'error: the GDB packet log file "%s" does not exist' % g_log_file + +def is_hex_byte(str): + if len(str) == 2: + return str[0] in string.hexdigits and str[1] in string.hexdigits; + return False + +# global register info list +g_register_infos = list() +g_max_register_info_name_len = 0 + +class RegisterInfo: + """Class that represents register information""" + def __init__(self, kvp): + self.info = dict() + for kv in kvp: + key = kv[0] + value = kv[1] + self.info[key] = value + def name(self): + '''Get the name of the register.''' + if self.info and 'name' in self.info: + return self.info['name'] + return None + + def bit_size(self): + '''Get the size in bits of the register.''' + if self.info and 'bitsize' in self.info: + return int(self.info['bitsize']) + return 0 + + def byte_size(self): + '''Get the size in bytes of the register.''' + return self.bit_size() / 8 + + def get_value_from_hex_string(self, hex_str): + '''Dump the register value given a native byte order encoded hex ASCII byte string.''' + encoding = self.info['encoding'] + bit_size = self.bit_size() + packet = Packet(hex_str) + if encoding == 'uint': + uval = packet.get_hex_uint(g_byte_order) + if bit_size == 8: + return '0x%2.2x' % (uval) + elif bit_size == 16: + return '0x%4.4x' % (uval) + elif bit_size == 32: + return '0x%8.8x' % (uval) + elif bit_size == 64: + return '0x%16.16x' % (uval) + bytes = list(); + uval = packet.get_hex_uint8() + while uval != None: + bytes.append(uval) + uval = packet.get_hex_uint8() + value_str = '0x' + if g_byte_order == 'little': + bytes.reverse() + for byte in bytes: + value_str += '%2.2x' % byte + return '%s' % (value_str) + + def __str__(self): + '''Dump the register info key/value pairs''' + s = '' + for key in self.info.keys(): + if s: + s += ', ' + s += "%s=%s " % (key, self.info[key]) + return s + +class Packet: + """Class that represents a packet that contains string data""" + def __init__(self, packet_str): + self.str = packet_str + + def peek_char(self): + ch = 0 + if self.str: + ch = self.str[0] + return ch + + def get_char(self): + ch = 0 + if self.str: + ch = self.str[0] + self.str = self.str[1:] + return ch + + def skip_exact_string(self, s): + if self.str and self.str.startswith(s): + self.str = self.str[len(s):] + return True + else: + return False + + def get_thread_id(self, fail_value = -1): + match = g_number_regex.match (self.str) + if match: + number_str = match.group(1) + self.str = self.str[len(number_str):] + return int(number_str, 0) + else: + return fail_value + + def get_hex_uint8(self): + if self.str and len(self.str) >= 2 and self.str[0] in string.hexdigits and self.str[1] in string.hexdigits: + uval = int(self.str[0:2], 16) + self.str = self.str[2:] + return uval + return None + + def get_hex_uint16(self, byte_order): + uval = 0 + if byte_order == 'big': + uval |= self.get_hex_uint8() << 8 + uval |= self.get_hex_uint8() + else: + uval |= self.get_hex_uint8() + uval |= self.get_hex_uint8() << 8 + return uval + + def get_hex_uint32(self, byte_order): + uval = 0 + if byte_order == 'big': + uval |= self.get_hex_uint8() << 24 + uval |= self.get_hex_uint8() << 16 + uval |= self.get_hex_uint8() << 8 + uval |= self.get_hex_uint8() + else: + uval |= self.get_hex_uint8() + uval |= self.get_hex_uint8() << 8 + uval |= self.get_hex_uint8() << 16 + uval |= self.get_hex_uint8() << 24 + return uval + + def get_hex_uint64(self, byte_order): + uval = 0 + if byte_order == 'big': + uval |= self.get_hex_uint8() << 56 + uval |= self.get_hex_uint8() << 48 + uval |= self.get_hex_uint8() << 40 + uval |= self.get_hex_uint8() << 32 + uval |= self.get_hex_uint8() << 24 + uval |= self.get_hex_uint8() << 16 + uval |= self.get_hex_uint8() << 8 + uval |= self.get_hex_uint8() + else: + uval |= self.get_hex_uint8() + uval |= self.get_hex_uint8() << 8 + uval |= self.get_hex_uint8() << 16 + uval |= self.get_hex_uint8() << 24 + uval |= self.get_hex_uint8() << 32 + uval |= self.get_hex_uint8() << 40 + uval |= self.get_hex_uint8() << 48 + uval |= self.get_hex_uint8() << 56 + return uval + + def get_number(self, fail_value=-1): + '''Get a number from the packet. The number must be in big endian format and should be parsed + according to its prefix (starts with "0x" means hex, starts with "0" means octal, starts with + [1-9] means decimal, etc)''' + match = g_number_regex.match (self.str) + if match: + number_str = match.group(1) + self.str = self.str[len(number_str):] + return int(number_str, 0) + else: + return fail_value + + + def get_hex_ascii_str(self, n=0): + hex_chars = self.get_hex_chars(n) + if hex_chars: + return binascii.unhexlify(hex_chars) + else: + return None + + def get_hex_chars(self, n = 0): + str_len = len(self.str) + if n == 0: + # n was zero, so we need to determine all hex chars and + # stop when we hit the end of the string of a non-hex character + while n < str_len and self.str[n] in string.hexdigits: + n = n + 1 + else: + if n > str_len: + return None # Not enough chars + # Verify all chars are hex if a length was specified + for i in range(n): + if self.str[i] not in string.hexdigits: + return None # Not all hex digits + if n == 0: + return None + hex_str = self.str[0:n] + self.str = self.str[n:] + return hex_str + + def get_hex_uint(self, byte_order, n = 0): + if byte_order == 'big': + hex_str = self.get_hex_chars(n) + if hex_str == None: + return None + return int(hex_str, 16) + else: + uval = self.get_hex_uint8() + if uval == None: + return None + uval_result = 0 + shift = 0 + while uval != None: + uval_result |= (uval << shift) + shift += 8 + uval = self.get_hex_uint8() + return uval_result + + def get_key_value_pairs(self): + kvp = list() + if ';' in self.str: + key_value_pairs = string.split(self.str, ';') + for key_value_pair in key_value_pairs: + if len(key_value_pair): + kvp.append(string.split(key_value_pair, ':')) + return kvp + + def split(self, ch): + return string.split(self.str, ch) + + def split_hex(self, ch, byte_order): + hex_values = list() + strings = string.split(self.str, ch) + for str in strings: + hex_values.append(Packet(str).get_hex_uint(byte_order)) + return hex_values + + def __str__(self): + return self.str + + def __len__(self): + return len(self.str) + +g_thread_suffix_regex = re.compile(';thread:([0-9a-fA-F]+);') +def get_thread_from_thread_suffix(str): + if str: + match = g_thread_suffix_regex.match (str) + if match: + return int(match.group(1), 16) + return None + +def cmd_qThreadStopInfo(options, cmd, args): + packet = Packet(args) + tid = packet.get_hex_uint('big') + print "get_thread_stop_info (tid = 0x%x)" % (tid) + +def cmd_stop_reply(options, cmd, args): + print "get_last_stop_info()" + return False + +def rsp_stop_reply(options, cmd, cmd_args, rsp): + global g_byte_order + packet = Packet(rsp) + stop_type = packet.get_char() + if stop_type == 'T' or stop_type == 'S': + signo = packet.get_hex_uint8() + key_value_pairs = packet.get_key_value_pairs() + for key_value_pair in key_value_pairs: + key = key_value_pair[0] + if is_hex_byte(key): + reg_num = Packet(key).get_hex_uint8() + if reg_num < len(g_register_infos): + reg_info = g_register_infos[reg_num] + key_value_pair[0] = reg_info.name() + key_value_pair[1] = reg_info.get_value_from_hex_string (key_value_pair[1]) + elif key == 'jthreads' or key == 'jstopinfo': + key_value_pair[1] = binascii.unhexlify(key_value_pair[1]) + key_value_pairs.insert(0, ['signal', signo]) + print 'stop_reply():' + dump_key_value_pairs (key_value_pairs) + elif stop_type == 'W': + exit_status = packet.get_hex_uint8() + print 'stop_reply(): exit (status=%i)' % exit_status + elif stop_type == 'O': + print 'stop_reply(): stdout = "%s"' % packet.str + + +def cmd_unknown_packet(options, cmd, args): + if args: + print "cmd: %s, args: %s", cmd, args + else: + print "cmd: %s", cmd + return False + +def cmd_qSymbol(options, cmd, args): + if args == ':': + print 'ready to serve symbols' + else: + packet = Packet(args) + symbol_addr = packet.get_hex_uint('big') + if symbol_addr is None: + if packet.skip_exact_string(':'): + symbol_name = packet.get_hex_ascii_str() + print 'lookup_symbol("%s") -> symbol not available yet' % (symbol_name) + else: + print 'error: bad command format' + else: + if packet.skip_exact_string(':'): + symbol_name = packet.get_hex_ascii_str() + print 'lookup_symbol("%s") -> 0x%x' % (symbol_name, symbol_addr) + else: + print 'error: bad command format' + +def rsp_qSymbol(options, cmd, cmd_args, rsp): + if len(rsp) == 0: + print "Unsupported" + else: + if rsp == "OK": + print "No more symbols to lookup" + else: + packet = Packet(rsp) + if packet.skip_exact_string("qSymbol:"): + symbol_name = packet.get_hex_ascii_str() + print 'lookup_symbol("%s")' % (symbol_name) + else: + print 'error: response string should start with "qSymbol:": respnse is "%s"' % (rsp) + +def cmd_qXfer(options, cmd, args): + # $qXfer:features:read:target.xml:0,1ffff#14 + print "read target special data %s" % (args) + return True + +def rsp_qXfer(options, cmd, cmd_args, rsp): + data = string.split(cmd_args, ':') + if data[0] == 'features': + if data[1] == 'read': + filename, extension = os.path.splitext(data[2]) + if extension == '.xml': + response = Packet(rsp) + xml_string = response.get_hex_ascii_str() + ch = xml_string[0] + if ch == 'l': + xml_string = xml_string[1:] + xml_root = ET.fromstring(xml_string) + for reg_element in xml_root.findall("./feature/reg"): + if not 'value_regnums' in reg_element.attrib: + reg_info = RegisterInfo([]) + if 'name' in reg_element.attrib: + reg_info.info['name'] = reg_element.attrib['name'] + else: + reg_info.info['name'] = 'unspecified' + if 'encoding' in reg_element.attrib: + reg_info.info['encoding'] = reg_element.attrib['encoding'] + else: + reg_info.info['encoding'] = 'uint' + if 'offset' in reg_element.attrib: + reg_info.info['offset'] = reg_element.attrib['offset'] + if 'bitsize' in reg_element.attrib: + reg_info.info['bitsize'] = reg_element.attrib['bitsize'] + g_register_infos.append(reg_info) + print 'XML for "%s":' % (data[2]) + ET.dump(xml_root) + +def cmd_A(options, cmd, args): + print 'launch process:' + packet = Packet(args) + while 1: + arg_len = packet.get_number() + if arg_len == -1: + break + if not packet.skip_exact_string(','): + break + arg_idx = packet.get_number() + if arg_idx == -1: + break + if not packet.skip_exact_string(','): + break; + arg_value = packet.get_hex_ascii_str(arg_len) + print 'argv[%u] = "%s"' % (arg_idx, arg_value) + +def cmd_qC(options, cmd, args): + print "query_current_thread_id()" + +def rsp_qC(options, cmd, cmd_args, rsp): + packet = Packet(rsp) + if packet.skip_exact_string("QC"): + tid = packet.get_thread_id() + print "current_thread_id = %#x" % (tid) + else: + print "current_thread_id = old thread ID" + +def cmd_query_packet(options, cmd, args): + if args: + print "%s%s" % (cmd, args) + else: + print "%s" % (cmd) + return False + +def rsp_ok_error(rsp): + print "rsp: ", rsp + +def rsp_ok_means_supported(options, cmd, cmd_args, rsp): + if rsp == 'OK': + print "%s%s is supported" % (cmd, cmd_args) + elif rsp == '': + print "%s%s is not supported" % (cmd, cmd_args) + else: + print "%s%s -> %s" % (cmd, cmd_args, rsp) + +def rsp_ok_means_success(options, cmd, cmd_args, rsp): + if rsp == 'OK': + print "success" + elif rsp == '': + print "%s%s is not supported" % (cmd, cmd_args) + else: + print "%s%s -> %s" % (cmd, cmd_args, rsp) + +def dump_key_value_pairs(key_value_pairs): + max_key_len = 0 + for key_value_pair in key_value_pairs: + key_len = len(key_value_pair[0]) + if max_key_len < key_len: + max_key_len = key_len + for key_value_pair in key_value_pairs: + key = key_value_pair[0] + value = key_value_pair[1] + print "%*s = %s" % (max_key_len, key, value) + +def rsp_dump_key_value_pairs(options, cmd, cmd_args, rsp): + if rsp: + print '%s response:' % (cmd) + packet = Packet(rsp) + key_value_pairs = packet.get_key_value_pairs() + dump_key_value_pairs(key_value_pairs) + else: + print "not supported" + +def cmd_c(options, cmd, args): + print "continue()" + return False + +def cmd_s(options, cmd, args): + print "step()" + return False + +def cmd_vCont(options, cmd, args): + if args == '?': + print "%s: get supported extended continue modes" % (cmd) + else: + got_other_threads = 0 + s = '' + for thread_action in string.split(args[1:], ';'): + (short_action, thread) = string.split(thread_action, ':') + tid = int(thread, 16) + if short_action == 'c': + action = 'continue' + elif short_action == 's': + action = 'step' + elif short_action[0] == 'C': + action = 'continue with signal 0x%s' % (short_action[1:]) + elif short_action == 'S': + action = 'step with signal 0x%s' % (short_action[1:]) + else: + action = short_action + if s: + s += ', ' + if tid == -1: + got_other_threads = 1 + s += 'other-threads:' + else: + s += 'thread 0x%4.4x: %s' % (tid, action) + if got_other_threads: + print "extended_continue (%s)" % (s) + else: + print "extended_continue (%s, other-threads: suspend)" % (s) + return False + +def rsp_vCont(options, cmd, cmd_args, rsp): + if cmd_args == '?': + # Skip the leading 'vCont;' + rsp = rsp[6:] + modes = string.split(rsp, ';') + s = "%s: supported extended continue modes include: " % (cmd) + + for i, mode in enumerate(modes): + if i: + s += ', ' + if mode == 'c': + s += 'continue' + elif mode == 'C': + s += 'continue with signal' + elif mode == 's': + s += 'step' + elif mode == 'S': + s += 'step with signal' + else: + s += 'unrecognized vCont mode: ', mode + print s + elif rsp: + if rsp[0] == 'T' or rsp[0] == 'S' or rsp[0] == 'W' or rsp[0] == 'X': + rsp_stop_reply (options, cmd, cmd_args, rsp) + return + if rsp[0] == 'O': + print "stdout: %s" % (rsp) + return + else: + print "not supported (cmd = '%s', args = '%s', rsp = '%s')" % (cmd, cmd_args, rsp) + +def cmd_vAttach(options, cmd, args): + (extra_command, args) = string.split(args, ';') + if extra_command: + print "%s%s(%s)" % (cmd, extra_command, args) + else: + print "attach(pid = %u)" % int(args, 16) + return False + + +def cmd_qRegisterInfo(options, cmd, args): + print 'query_register_info(reg_num=%i)' % (int(args, 16)) + return False + +def rsp_qRegisterInfo(options, cmd, cmd_args, rsp): + global g_max_register_info_name_len + print 'query_register_info(reg_num=%i):' % (int(cmd_args, 16)), + if len(rsp) == 3 and rsp[0] == 'E': + g_max_register_info_name_len = 0 + for reg_info in g_register_infos: + name_len = len(reg_info.name()) + if g_max_register_info_name_len < name_len: + g_max_register_info_name_len = name_len + print' DONE' + else: + packet = Packet(rsp) + reg_info = RegisterInfo(packet.get_key_value_pairs()) + g_register_infos.append(reg_info) + print reg_info + return False + +def cmd_qThreadInfo(options, cmd, args): + if cmd == 'qfThreadInfo': + query_type = 'first' + else: + query_type = 'subsequent' + print 'get_current_thread_list(type=%s)' % (query_type) + return False + +def rsp_qThreadInfo(options, cmd, cmd_args, rsp): + packet = Packet(rsp) + response_type = packet.get_char() + if response_type == 'm': + tids = packet.split_hex(';', 'big') + for i, tid in enumerate(tids): + if i: + print ',', + print '0x%x' % (tid), + print + elif response_type == 'l': + print 'END' + +def rsp_hex_big_endian(options, cmd, cmd_args, rsp): + packet = Packet(rsp) + uval = packet.get_hex_uint('big') + print '%s: 0x%x' % (cmd, uval) + +def cmd_read_mem_bin(options, cmd, args): + # x0x7fff5fc39200,0x200 + packet = Packet(args) + addr = packet.get_number() + comma = packet.get_char() + size = packet.get_number() + print 'binary_read_memory (addr = 0x%16.16x, size = %u)' % (addr, size) + return False + +def rsp_mem_bin_bytes(options, cmd, cmd_args, rsp): + packet = Packet(cmd_args) + addr = packet.get_number() + comma = packet.get_char() + size = packet.get_number() + print 'memory:' + if size > 0: + dump_hex_memory_buffer (addr, rsp) + +def cmd_read_memory(options, cmd, args): + packet = Packet(args) + addr = packet.get_hex_uint('big') + comma = packet.get_char() + size = packet.get_hex_uint('big') + print 'read_memory (addr = 0x%16.16x, size = %u)' % (addr, size) + return False + +def dump_hex_memory_buffer(addr, hex_byte_str): + packet = Packet(hex_byte_str) + idx = 0 + ascii = '' + uval = packet.get_hex_uint8() + while uval != None: + if ((idx % 16) == 0): + if ascii: + print ' ', ascii + ascii = '' + print '0x%x:' % (addr + idx), + print '%2.2x' % (uval), + if 0x20 <= uval and uval < 0x7f: + ascii += '%c' % uval + else: + ascii += '.' + uval = packet.get_hex_uint8() + idx = idx + 1 + if ascii: + print ' ', ascii + ascii = '' + +def cmd_write_memory(options, cmd, args): + packet = Packet(args) + addr = packet.get_hex_uint('big') + if packet.get_char() != ',': + print 'error: invalid write memory command (missing comma after address)' + return + size = packet.get_hex_uint('big') + if packet.get_char() != ':': + print 'error: invalid write memory command (missing colon after size)' + return + print 'write_memory (addr = 0x%16.16x, size = %u, data:' % (addr, size) + dump_hex_memory_buffer (addr, packet.str) + return False + +def cmd_alloc_memory(options, cmd, args): + packet = Packet(args) + byte_size = packet.get_hex_uint('big') + if packet.get_char() != ',': + print 'error: invalid allocate memory command (missing comma after address)' + return + print 'allocate_memory (byte-size = %u (0x%x), permissions = %s)' % (byte_size, byte_size, packet.str) + return False + +def rsp_alloc_memory(options, cmd, cmd_args, rsp): + packet = Packet(rsp) + addr = packet.get_hex_uint('big') + print 'addr = 0x%x' % addr + +def cmd_dealloc_memory(options, cmd, args): + packet = Packet(args) + addr = packet.get_hex_uint('big') + if packet.get_char() != ',': + print 'error: invalid allocate memory command (missing comma after address)' + else: + print 'deallocate_memory (addr = 0x%x, permissions = %s)' % (addr, packet.str) + return False +def rsp_memory_bytes(options, cmd, cmd_args, rsp): + addr = Packet(cmd_args).get_hex_uint('big') + dump_hex_memory_buffer (addr, rsp) + +def get_register_name_equal_value(options, reg_num, hex_value_str): + if reg_num < len(g_register_infos): + reg_info = g_register_infos[reg_num] + value_str = reg_info.get_value_from_hex_string (hex_value_str) + s = reg_info.name() + ' = ' + if options.symbolicator: + symbolicated_addresses = options.symbolicator.symbolicate (int(value_str, 0)) + if symbolicated_addresses: + s += options.colors.magenta() + s += '%s' % symbolicated_addresses[0] + s += options.colors.reset() + return s + s += value_str + return s + else: + reg_value = Packet(hex_value_str).get_hex_uint(g_byte_order) + return 'reg(%u) = 0x%x' % (reg_num, reg_value) + +def cmd_read_one_reg(options, cmd, args): + packet = Packet(args) + reg_num = packet.get_hex_uint('big') + tid = get_thread_from_thread_suffix (packet.str) + name = None + if reg_num < len(g_register_infos): + name = g_register_infos[reg_num].name () + if packet.str: + packet.get_char() # skip ; + thread_info = packet.get_key_value_pairs() + tid = int(thread_info[0][1], 16) + s = 'read_register (reg_num=%u' % reg_num + if name: + s += ' (%s)' % (name) + if tid != None: + s += ', tid = 0x%4.4x' % (tid) + s += ')' + print s + return False + +def rsp_read_one_reg(options, cmd, cmd_args, rsp): + packet = Packet(cmd_args) + reg_num = packet.get_hex_uint('big') + print get_register_name_equal_value (options, reg_num, rsp) + +def cmd_write_one_reg(options, cmd, args): + packet = Packet(args) + reg_num = packet.get_hex_uint('big') + if packet.get_char() != '=': + print 'error: invalid register write packet' + else: + name = None + hex_value_str = packet.get_hex_chars() + tid = get_thread_from_thread_suffix (packet.str) + s = 'write_register (reg_num=%u' % reg_num + if name: + s += ' (%s)' % (name) + s += ', value = ' + s += get_register_name_equal_value(options, reg_num, hex_value_str) + if tid != None: + s += ', tid = 0x%4.4x' % (tid) + s += ')' + print s + return False + +def dump_all_regs(packet): + for reg_info in g_register_infos: + nibble_size = reg_info.bit_size() / 4 + hex_value_str = packet.get_hex_chars(nibble_size) + if hex_value_str != None: + value = reg_info.get_value_from_hex_string (hex_value_str) + print '%*s = %s' % (g_max_register_info_name_len, reg_info.name(), value) + else: + return + +def cmd_read_all_regs(cmd, cmd_args): + packet = Packet(cmd_args) + packet.get_char() # toss the 'g' command character + tid = get_thread_from_thread_suffix (packet.str) + if tid != None: + print 'read_all_register(thread = 0x%4.4x)' % tid + else: + print 'read_all_register()' + return False + +def rsp_read_all_regs(options, cmd, cmd_args, rsp): + packet = Packet(rsp) + dump_all_regs (packet) + +def cmd_write_all_regs(options, cmd, args): + packet = Packet(args) + print 'write_all_registers()' + dump_all_regs (packet) + return False + +g_bp_types = [ "software_bp", "hardware_bp", "write_wp", "read_wp", "access_wp" ] + +def cmd_bp(options, cmd, args): + if cmd == 'Z': + s = 'set_' + else: + s = 'clear_' + packet = Packet (args) + bp_type = packet.get_hex_uint('big') + packet.get_char() # Skip , + bp_addr = packet.get_hex_uint('big') + packet.get_char() # Skip , + bp_size = packet.get_hex_uint('big') + s += g_bp_types[bp_type] + s += " (addr = 0x%x, size = %u)" % (bp_addr, bp_size) + print s + return False + +def cmd_mem_rgn_info(options, cmd, args): + packet = Packet(args) + packet.get_char() # skip ':' character + addr = packet.get_hex_uint('big') + print 'get_memory_region_info (addr=0x%x)' % (addr) + return False + +def cmd_kill(options, cmd, args): + print 'kill_process()' + return False + +def cmd_jThreadsInfo(options, cmd, args): + print 'jThreadsInfo()' + return False + +def cmd_jGetLoadedDynamicLibrariesInfos(options, cmd, args): + print 'jGetLoadedDynamicLibrariesInfos()' + return False + +def decode_packet(s, start_index = 0): + #print '\ndecode_packet("%s")' % (s[start_index:]) + index = s.find('}', start_index) + have_escapes = index != -1 + if have_escapes: + normal_s = s[start_index:index] + else: + normal_s = s[start_index:] + #print 'normal_s = "%s"' % (normal_s) + if have_escapes: + escape_char = '%c' % (ord(s[index+1]) ^ 0x20) + #print 'escape_char for "%s" = %c' % (s[index:index+2], escape_char) + return normal_s + escape_char + decode_packet(s, index+2) + else: + return normal_s + +def rsp_json(options, cmd, cmd_args, rsp): + print '%s() reply:' % (cmd) + json_tree = json.loads(rsp) + print json.dumps(json_tree, indent=4, separators=(',', ': ')) + + +def rsp_jGetLoadedDynamicLibrariesInfos(options, cmd, cmd_args, rsp): + if cmd_args: + rsp_json(options, cmd, cmd_args, rsp) + else: + rsp_ok_means_supported(options, cmd, cmd_args, rsp) + +gdb_remote_commands = { + '\\?' : { 'cmd' : cmd_stop_reply , 'rsp' : rsp_stop_reply , 'name' : "stop reply pacpket"}, + 'qThreadStopInfo' : { 'cmd' : cmd_qThreadStopInfo , 'rsp' : rsp_stop_reply , 'name' : "stop reply pacpket"}, + 'QStartNoAckMode' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_ok_means_supported , 'name' : "query if no ack mode is supported"}, + 'QThreadSuffixSupported' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_ok_means_supported , 'name' : "query if thread suffix is supported" }, + 'QListThreadsInStopReply' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_ok_means_supported , 'name' : "query if threads in stop reply packets are supported" }, + 'QSetDetachOnError' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_ok_means_success , 'name' : "set if we should detach on error" }, + 'QSetDisableASLR' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_ok_means_success , 'name' : "set if we should disable ASLR" }, + 'qLaunchSuccess' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_ok_means_success , 'name' : "check on launch success for the A packet" }, + 'A' : { 'cmd' : cmd_A , 'rsp' : rsp_ok_means_success , 'name' : "launch process" }, + 'QLaunchArch' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_ok_means_supported , 'name' : "set if we should disable ASLR" }, + 'qVAttachOrWaitSupported' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_ok_means_supported , 'name' : "set the launch architecture" }, + 'qHostInfo' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_dump_key_value_pairs, 'name' : "get host information" }, + 'qC' : { 'cmd' : cmd_qC , 'rsp' : rsp_qC , 'name' : "return the current thread ID" }, + 'vCont' : { 'cmd' : cmd_vCont , 'rsp' : rsp_vCont , 'name' : "extended continue command" }, + 'vAttach' : { 'cmd' : cmd_vAttach , 'rsp' : rsp_stop_reply , 'name' : "attach to process" }, + 'c' : { 'cmd' : cmd_c , 'rsp' : rsp_stop_reply , 'name' : "continue" }, + 's' : { 'cmd' : cmd_s , 'rsp' : rsp_stop_reply , 'name' : "step" }, + 'qRegisterInfo' : { 'cmd' : cmd_qRegisterInfo , 'rsp' : rsp_qRegisterInfo , 'name' : "query register info" }, + 'qfThreadInfo' : { 'cmd' : cmd_qThreadInfo , 'rsp' : rsp_qThreadInfo , 'name' : "get current thread list" }, + 'qsThreadInfo' : { 'cmd' : cmd_qThreadInfo , 'rsp' : rsp_qThreadInfo , 'name' : "get current thread list" }, + 'qShlibInfoAddr' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_hex_big_endian , 'name' : "get shared library info address" }, + 'qMemoryRegionInfo' : { 'cmd' : cmd_mem_rgn_info , 'rsp' : rsp_dump_key_value_pairs, 'name' : "get memory region information" }, + 'qProcessInfo' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_dump_key_value_pairs, 'name' : "get process info" }, + 'qSupported' : { 'cmd' : cmd_query_packet , 'rsp' : rsp_ok_means_supported , 'name' : "query supported" }, + 'qXfer:' : { 'cmd' : cmd_qXfer , 'rsp' : rsp_qXfer , 'name' : "qXfer" }, + 'qSymbol:' : { 'cmd' : cmd_qSymbol , 'rsp' : rsp_qSymbol , 'name' : "qSymbol" }, + 'x' : { 'cmd' : cmd_read_mem_bin , 'rsp' : rsp_mem_bin_bytes , 'name' : "read memory binary" }, + 'X' : { 'cmd' : cmd_write_memory , 'rsp' : rsp_ok_means_success , 'name' : "write memory binary" }, + 'm' : { 'cmd' : cmd_read_memory , 'rsp' : rsp_memory_bytes , 'name' : "read memory" }, + 'M' : { 'cmd' : cmd_write_memory , 'rsp' : rsp_ok_means_success , 'name' : "write memory" }, + '_M' : { 'cmd' : cmd_alloc_memory , 'rsp' : rsp_alloc_memory , 'name' : "allocate memory" }, + '_m' : { 'cmd' : cmd_dealloc_memory , 'rsp' : rsp_ok_means_success , 'name' : "deallocate memory" }, + 'p' : { 'cmd' : cmd_read_one_reg , 'rsp' : rsp_read_one_reg , 'name' : "read single register" }, + 'P' : { 'cmd' : cmd_write_one_reg , 'rsp' : rsp_ok_means_success , 'name' : "write single register" }, + 'g' : { 'cmd' : cmd_read_all_regs , 'rsp' : rsp_read_all_regs , 'name' : "read all registers" }, + 'G' : { 'cmd' : cmd_write_all_regs , 'rsp' : rsp_ok_means_success , 'name' : "write all registers" }, + 'z' : { 'cmd' : cmd_bp , 'rsp' : rsp_ok_means_success , 'name' : "clear breakpoint or watchpoint" }, + 'Z' : { 'cmd' : cmd_bp , 'rsp' : rsp_ok_means_success , 'name' : "set breakpoint or watchpoint" }, + 'k' : { 'cmd' : cmd_kill , 'rsp' : rsp_stop_reply , 'name' : "kill process" }, + 'jThreadsInfo' : { 'cmd' : cmd_jThreadsInfo , 'rsp' : rsp_json , 'name' : "JSON get all threads info" }, + 'jGetLoadedDynamicLibrariesInfos:' : { 'cmd' : cmd_jGetLoadedDynamicLibrariesInfos, 'rsp' : rsp_jGetLoadedDynamicLibrariesInfos, 'name' : 'JSON get loaded dynamic libraries' }, +} + +def calculate_mean_and_standard_deviation(floats): + sum = 0.0 + count = len(floats) + if count == 0: + return (0.0, 0.0) + for f in floats: + sum += f + mean = sum / count + accum = 0.0 + for f in floats: + delta = f - mean + accum += delta * delta + + std_dev = math.sqrt(accum / (count-1)); + return (mean, std_dev) + +def parse_gdb_log_file(path, options): + f = open(path) + parse_gdb_log(f, options) + f.close() + +def parse_gdb_log(file, options): + '''Parse a GDB log file that was generated by enabling logging with: + (lldb) log enable --threadsafe --timestamp --file <FILE> gdb-remote packets + This log file will contain timestamps and this function will then normalize + those packets to be relative to the first value timestamp that is found and + show delta times between log lines and also keep track of how long it takes + for GDB remote commands to make a send/receive round trip. This can be + handy when trying to figure out why some operation in the debugger is taking + a long time during a preset set of debugger commands.''' + + tricky_commands = [ 'qRegisterInfo' ] + timestamp_regex = re.compile('(\s*)([1-9][0-9]+\.[0-9]+)([^0-9].*)$') + packet_name_regex = re.compile('([A-Za-z_]+)[^a-z]') + packet_transmit_name_regex = re.compile('(?P<direction>send|read) packet: (?P<packet>.*)') + packet_contents_name_regex = re.compile('\$([^#]+)#[0-9a-fA-F]{2}') + packet_checksum_regex = re.compile('.*#[0-9a-fA-F]{2}$') + packet_names_regex_str = '(' + '|'.join(gdb_remote_commands.keys()) + ')(.*)'; + packet_names_regex = re.compile(packet_names_regex_str); + + base_time = 0.0 + last_time = 0.0 + packet_send_time = 0.0 + packet_total_times = {} + packet_times = [] + packet_count = {} + lines = file.read().splitlines() + last_command = None + last_command_args = None + last_command_packet = None + hide_next_response = False + num_lines = len(lines) + skip_count = 0 + for (line_index, line) in enumerate(lines): + # See if we need to skip any lines + if skip_count > 0: + skip_count -= 1 + continue + m = packet_transmit_name_regex.search(line) + is_command = False + direction = None + if m: + direction = m.group('direction') + is_command = direction == 'send' + packet = m.group('packet') + sys.stdout.write(options.colors.green()) + if not options.quiet and not hide_next_response: + print '# ', line + sys.stdout.write(options.colors.reset()) + + #print 'direction = "%s", packet = "%s"' % (direction, packet) + + if packet[0] == '+': + if is_command: + print '-->', + else: + print '<--', + if not options.quiet: print 'ACK' + continue + elif packet[0] == '-': + if is_command: + print '-->', + else: + print '<--', + if not options.quiet: print 'NACK' + continue + elif packet[0] == '$': + m = packet_contents_name_regex.match(packet) + if not m and packet[0] == '$': + multiline_packet = packet + idx = line_index + 1 + while idx < num_lines: + if not options.quiet and not hide_next_response: + print '# ', lines[idx] + multiline_packet += lines[idx] + m = packet_contents_name_regex.match(multiline_packet) + if m: + packet = multiline_packet + skip_count = idx - line_index + break + else: + idx += 1 + if m: + if is_command: + print '-->', + else: + print '<--', + contents = decode_packet(m.group(1)) + if is_command: + hide_next_response = False + m = packet_names_regex.match (contents) + if m: + last_command = m.group(1) + if last_command == '?': + last_command = '\\?' + packet_name = last_command + last_command_args = m.group(2) + last_command_packet = contents + hide_next_response = gdb_remote_commands[last_command]['cmd'](options, last_command, last_command_args) + else: + packet_match = packet_name_regex.match (contents) + if packet_match: + packet_name = packet_match.group(1) + for tricky_cmd in tricky_commands: + if packet_name.find (tricky_cmd) == 0: + packet_name = tricky_cmd + else: + packet_name = contents + last_command = None + last_command_args = None + last_command_packet = None + elif last_command: + gdb_remote_commands[last_command]['rsp'](options, last_command, last_command_args, contents) + else: + print 'error: invalid packet: "', packet, '"' + else: + print '???' + else: + print '## ', line + match = timestamp_regex.match (line) + if match: + curr_time = float (match.group(2)) + if last_time and not is_command: + delta = curr_time - last_time + packet_times.append(delta) + delta = 0.0 + if base_time: + delta = curr_time - last_time + else: + base_time = curr_time + + if is_command: + packet_send_time = curr_time + elif line.find('read packet: $') >= 0 and packet_name: + if packet_name in packet_total_times: + packet_total_times[packet_name] += delta + packet_count[packet_name] += 1 + else: + packet_total_times[packet_name] = delta + packet_count[packet_name] = 1 + packet_name = None + + if not options or not options.quiet: + print '%s%.6f %+.6f%s' % (match.group(1), curr_time - base_time, delta, match.group(3)) + last_time = curr_time + # else: + # print line + (average, std_dev) = calculate_mean_and_standard_deviation(packet_times) + if average and std_dev: + print '%u packets with average packet time of %f and standard deviation of %f' % (len(packet_times), average, std_dev) + if packet_total_times: + total_packet_time = 0.0 + total_packet_count = 0 + for key, vvv in packet_total_times.items(): + # print ' key = (%s) "%s"' % (type(key), key) + # print 'value = (%s) %s' % (type(vvv), vvv) + # if type(vvv) == 'float': + total_packet_time += vvv + for key, vvv in packet_count.items(): + total_packet_count += vvv + + print '#---------------------------------------------------' + print '# Packet timing summary:' + print '# Totals: time = %6f, count = %6d' % (total_packet_time, total_packet_count) + print '#---------------------------------------------------' + print '# Packet Time (sec) Percent Count ' + print '#------------------------- ---------- ------- ------' + if options and options.sort_count: + res = sorted(packet_count, key=packet_count.__getitem__, reverse=True) + else: + res = sorted(packet_total_times, key=packet_total_times.__getitem__, reverse=True) + + if last_time > 0.0: + for item in res: + packet_total_time = packet_total_times[item] + packet_percent = (packet_total_time / total_packet_time)*100.0 + if packet_percent >= 10.0: + print " %24s %.6f %.2f%% %6d" % (item, packet_total_time, packet_percent, packet_count[item]) + else: + print " %24s %.6f %.2f%% %6d" % (item, packet_total_time, packet_percent, packet_count[item]) + + + +if __name__ == '__main__': + usage = "usage: gdbremote [options]" + description='''The command disassembles a GDB remote packet log.''' + parser = optparse.OptionParser(description=description, prog='gdbremote',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + parser.add_option('-q', '--quiet', action='store_true', dest='quiet', help='display verbose debug info', default=False) + parser.add_option('-C', '--color', action='store_true', dest='color', help='add terminal colors', default=False) + parser.add_option('-c', '--sort-by-count', action='store_true', dest='sort_count', help='display verbose debug info', default=False) + parser.add_option('--crashlog', type='string', dest='crashlog', help='symbolicate using a darwin crash log file', default=False) + try: + (options, args) = parser.parse_args(sys.argv[1:]) + except: + print 'error: argument error' + sys.exit(1) + + options.colors = TerminalColors(options.color) + options.symbolicator = None + if options.crashlog: + import lldb + lldb.debugger = lldb.SBDebugger.Create() + import lldb.macosx.crashlog + options.symbolicator = lldb.macosx.crashlog.CrashLog(options.crashlog) + print '%s' % (options.symbolicator) + + # This script is being run from the command line, create a debugger in case we are + # going to use any debugger functions in our function. + if len(args): + for file in args: + print '#----------------------------------------------------------------------' + print "# GDB remote log file: '%s'" % file + print '#----------------------------------------------------------------------' + parse_gdb_log_file (file, options) + if options.symbolicator: + print '%s' % (options.symbolicator) + else: + parse_gdb_log(sys.stdin, options) + +else: + import lldb + if lldb.debugger: + # This initializer is being run from LLDB in the embedded command interpreter + # Add any commands contained in this module to LLDB + lldb.debugger.HandleCommand('command script add -f gdbremote.start_gdb_log start_gdb_log') + lldb.debugger.HandleCommand('command script add -f gdbremote.stop_gdb_log stop_gdb_log') + print 'The "start_gdb_log" and "stop_gdb_log" commands are now installed and ready for use, type "start_gdb_log --help" or "stop_gdb_log --help" for more information' diff --git a/examples/python/globals.py b/examples/python/globals.py new file mode 100755 index 000000000000..fb2739c8b69c --- /dev/null +++ b/examples/python/globals.py @@ -0,0 +1,72 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# For the shells csh, tcsh: +# ( setenv PYTHONPATH /Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python ; ./globals.py <path> [<path> ...]) +# +# For the shells sh, bash: +# PYTHONPATH=/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python ./globals.py <path> [<path> ...] +#---------------------------------------------------------------------- + +import lldb +import commands +import optparse +import os +import shlex +import sys + +def get_globals(raw_path, options): + error = lldb.SBError() + # Resolve the path if needed + path = os.path.expanduser(raw_path) + # Create a target using path + options + target = lldb.debugger.CreateTarget(path, options.arch, options.platform, False, error) + if target: + # Get the executable module + module = target.module[target.executable.basename] + if module: + # Keep track of which variables we have already looked up + global_names = list() + # Iterate through all symbols in the symbol table and watch for any DATA symbols + for symbol in module.symbols: + if symbol.type == lldb.eSymbolTypeData: + # The symbol is a DATA symbol, lets try and find all global variables + # that match this name and print them + global_name = symbol.name + # Make sure we don't lookup the same variable twice + if global_name not in global_names: + global_names.append(global_name) + # Find all global variables by name + global_variable_list = module.FindGlobalVariables (target, global_name, lldb.UINT32_MAX) + if global_variable_list: + # Print results for anything that matched + for global_variable in global_variable_list: + print 'name = %s' % global_variable.name # returns the global variable name as a string + print 'value = %s' % global_variable.value # Returns the variable value as a string + print 'type = %s' % global_variable.type # Returns an lldb.SBType object + print 'addr = %s' % global_variable.addr # Returns an lldb.SBAddress (section offset address) for this global + print 'file_addr = 0x%x' % global_variable.addr.file_addr # Returns the file virtual address for this global + print 'location = %s' % global_variable.location # returns the global variable value as a string + print 'size = %s' % global_variable.size # Returns the size in bytes of this global variable + print + +def globals(command_args): + '''Extract all globals from any arguments which must be paths to object files.''' + usage = "usage: %prog [options] <PATH> [PATH ...]" + description='''This command will find all globals in the specified object file and return an list() of lldb.SBValue objects (which might be empty).''' + parser = optparse.OptionParser(description=description, prog='globals',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + parser.add_option('-a', '--arch', type='string', metavar='arch', dest='arch', help='Specify an architecture (or triple) to use when extracting from a file.') + parser.add_option('-p', '--platform', type='string', metavar='platform', dest='platform', help='Specify the platform to use when creating the debug target. Valid values include "localhost", "darwin-kernel", "ios-simulator", "remote-freebsd", "remote-macosx", "remote-ios", "remote-linux".') + try: + (options, args) = parser.parse_args(command_args) + except: + return + + for path in args: + get_globals (path, options) + +if __name__ == '__main__': + lldb.debugger = lldb.SBDebugger.Create() + globals (sys.argv[1:]) + diff --git a/examples/python/jump.py b/examples/python/jump.py new file mode 100644 index 000000000000..c904009bb405 --- /dev/null +++ b/examples/python/jump.py @@ -0,0 +1,173 @@ +import lldb, re + +def parse_linespec (linespec, frame, result): + """Handles a subset of GDB-style linespecs. Specifically: + + number - A line in the current file + +offset - The line /offset/ lines after this line + -offset - The line /offset/ lines before this line + filename:number - Line /number/ in file /filename/ + function - The start of /function/ + *address - The pointer target of /address/, which must be a literal (but see `` in LLDB) + + We explicitly do not handle filename:function because it is ambiguous in Objective-C. + + This function returns a list of addresses.""" + + breakpoint = None + target = frame.GetThread().GetProcess().GetTarget() + + matched = False + + if (not matched): + mo = re.match("^([0-9]+)$", linespec) + if (mo != None): + matched = True + #print "Matched <linenum>" + line_number = int(mo.group(1)) + line_entry = frame.GetLineEntry() + if not line_entry.IsValid(): + result.AppendMessage("Specified a line in the current file, but the current frame doesn't have line table information.") + return + breakpoint = target.BreakpointCreateByLocation (line_entry.GetFileSpec(), line_number) + + if (not matched): + mo = re.match("^\+([0-9]+)$", linespec) + if (mo != None): + matched = True + #print "Matched +<count>" + line_number = int(mo.group(1)) + line_entry = frame.GetLineEntry() + if not line_entry.IsValid(): + result.AppendMessage("Specified a line in the current file, but the current frame doesn't have line table information.") + return + breakpoint = target.BreakpointCreateByLocation(line_entry.GetFileSpec(), (line_entry.GetLine() + line_number)) + + if (not matched): + mo = re.match("^\-([0-9]+)$", linespec) + if (mo != None): + matched = True + #print "Matched -<count>" + line_number = int(mo.group(1)) + line_entry = frame.GetLineEntry() + if not line_entry.IsValid(): + result.AppendMessage("Specified a line in the current file, but the current frame doesn't have line table information.") + return + breakpoint = target.BreakpointCreateByLocation(line_entry.GetFileSpec(), (line_entry.GetLine() - line_number)) + + if (not matched): + mo = re.match("^(.*):([0-9]+)$", linespec) + if (mo != None): + matched = True + #print "Matched <filename>:<linenum>" + file_name = mo.group(1) + line_number = int(mo.group(2)) + breakpoint = target.BreakpointCreateByLocation(file_name, line_number) + + if (not matched): + mo = re.match("\*((0x)?([0-9a-f]+))$", linespec) + if (mo != None): + matched = True + #print "Matched <address-expression>" + address = long(mo.group(1), base=0) + breakpoint = target.BreakpointCreateByAddress(address) + + if (not matched): + #print "Trying <function-name>" + breakpoint = target.BreakpointCreateByName(linespec) + + num_locations = breakpoint.GetNumLocations() + + if (num_locations == 0): + result.AppendMessage("The line specification provided doesn't resolve to any addresses.") + + addr_list = [] + + for location_index in range(num_locations): + location = breakpoint.GetLocationAtIndex(location_index) + addr_list.append(location.GetAddress()) + + target.BreakpointDelete(breakpoint.GetID()) + + return addr_list + +def usage_string(): + return """ Sets the program counter to a specific address. + +Syntax: jump <linespec> [<location-id>] + +Command Options Usage: + jump <linenum> + jump +<count> + jump -<count> + jump <filename>:<linenum> + jump <function-name> + jump *<address-expression> + +<location-id> serves to disambiguate when multiple locations could be meant.""" + +def jump (debugger, command, result, internal_dict): + if (command == ""): + result.AppendMessage(usage_string()) + + args = command.split() + + if not debugger.IsValid(): + result.AppendMessage("Invalid debugger!") + return + + target = debugger.GetSelectedTarget() + if not target.IsValid(): + result.AppendMessage("jump requires a valid target.") + return + + process = target.GetProcess() + if not process.IsValid(): + result.AppendMessage("jump requires a valid process.") + return + + thread = process.GetSelectedThread() + if not thread.IsValid(): + result.AppendMessage("jump requires a valid thread.") + return + + frame = thread.GetSelectedFrame() + if not frame.IsValid(): + result.AppendMessage("jump requires a valid frame.") + return + + addresses = parse_linespec(args[0], frame, result) + + stream = lldb.SBStream() + + if len(addresses) == 0: + return + + desired_address = addresses[0] + + if len(addresses) > 1: + if len(args) == 2: + desired_index = int(args[1]) + if (desired_index >= 0) and (desired_index < len(addresses)): + desired_address = addresses[desired_index] + else: + result.AppendMessage("Desired index " + args[1] + " is not one of the options.") + return + else: + index = 0 + result.AppendMessage("The specified location resolves to multiple targets."); + for address in addresses: + stream.Clear() + address.GetDescription(stream) + result.AppendMessage(" Location ID " + str(index) + ": " + stream.GetData()) + index = index + 1 + result.AppendMessage("Please type 'jump " + command + " <location-id>' to choose one.") + return + + frame.SetPC(desired_address.GetLoadAddress(target)) + +if lldb.debugger: + # Module is being run inside the LLDB interpreter + jump.__doc__ = usage_string() + lldb.debugger.HandleCommand('command script add -f jump.jump jump') + print 'The "jump" command has been installed, type "help jump" or "jump <ENTER>" for detailed help.' diff --git a/examples/python/lldb_module_utils.py b/examples/python/lldb_module_utils.py new file mode 100644 index 000000000000..37f33ba416a5 --- /dev/null +++ b/examples/python/lldb_module_utils.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +import lldb +import optparse +import shlex +import string +import sys + +def create_dump_module_line_tables_options (): + usage = "usage: dump_module_line_tables [options] MODULE1 [MODULE2 ...]" + description='''Dumps all line tables from all compile units for any modules specified as arguments. Specifying the --verbose flag will output address ranges for each line entry.''' + parser = optparse.OptionParser(description=description, prog='start_gdb_log',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='Display verbose output.', default=False) + return parser + +def dump_module_line_tables(debugger, command, result, dict): + '''Dumps all line tables from all compile units for any modules specified as arguments.''' + command_args = shlex.split(command) + + parser = create_dump_module_line_tables_options () + try: + (options, args) = parser.parse_args(command_args) + except: + return + if command_args: + target = debugger.GetSelectedTarget() + lldb.target = target + for module_name in command_args: + result.PutCString('Searching for module "%s"' % (module_name,)) + module_fspec = lldb.SBFileSpec (module_name, False) + module = target.FindModule (module_fspec); + if module: + for cu_idx in range (module.GetNumCompileUnits()): + cu = module.GetCompileUnitAtIndex(cu_idx) + result.PutCString("\n%s:" % (cu.file)) + for line_idx in range(cu.GetNumLineEntries()): + line_entry = cu.GetLineEntryAtIndex(line_idx) + start_file_addr = line_entry.addr.file_addr + end_file_addr = line_entry.end_addr.file_addr + # If the two addresses are equal, this line table entry is a termination entry + if options.verbose: + if start_file_addr != end_file_addr: + result.PutCString('[%#x - %#x): %s' % (start_file_addr, end_file_addr, line_entry)) + else: + if start_file_addr == end_file_addr: + result.PutCString('%#x: END' % (start_file_addr)) + else: + result.PutCString('%#x: %s' % (start_file_addr, line_entry)) + if start_file_addr == end_file_addr: + result.Printf("\n") + else: + result.PutCString ("no module for '%s'" % module) + else: + result.PutCString ("error: invalid target") + +parser = create_dump_module_line_tables_options () +dump_module_line_tables.__doc__ = parser.format_help() +lldb.debugger.HandleCommand('command script add -f %s.dump_module_line_tables dump_module_line_tables' % __name__) +print 'Installed "dump_module_line_tables" command'
\ No newline at end of file diff --git a/examples/python/lldbtk.py b/examples/python/lldbtk.py new file mode 100644 index 000000000000..7ada9d77b30c --- /dev/null +++ b/examples/python/lldbtk.py @@ -0,0 +1,544 @@ +#!/usr/bin/python + +import lldb +import shlex +import sys +from Tkinter import * +import ttk + +class ValueTreeItemDelegate(object): + def __init__(self, value): + self.value = value + + def get_item_dictionary(self): + name = self.value.name + if name is None: + name = '' + typename = self.value.type + if typename is None: + typename = '' + value = self.value.value + if value is None: + value = '' + summary = self.value.summary + if summary is None: + summary = '' + has_children = self.value.MightHaveChildren() + return { '#0' : name, + 'typename' : typename, + 'value' : value, + 'summary' : summary, + 'children' : has_children, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + for i in range(self.value.num_children): + item_delegate = ValueTreeItemDelegate(self.value.GetChildAtIndex(i)) + item_dicts.append(item_delegate.get_item_dictionary()) + return item_dicts + +class FrameTreeItemDelegate(object): + def __init__(self, frame): + self.frame = frame + + def get_item_dictionary(self): + id = self.frame.GetFrameID() + name = 'frame #%u' % (id); + value = '0x%16.16x' % (self.frame.GetPC()) + stream = lldb.SBStream() + self.frame.GetDescription(stream) + summary = stream.GetData().split("`")[1] + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : self.frame.GetVariables(True, True, True, True).GetSize() > 0, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + variables = self.frame.GetVariables(True, True, True, True) + n = variables.GetSize() + for i in range(n): + item_delegate = ValueTreeItemDelegate(variables[i]) + item_dicts.append(item_delegate.get_item_dictionary()) + return item_dicts + +class ThreadTreeItemDelegate(object): + def __init__(self, thread): + self.thread = thread + + def get_item_dictionary(self): + num_frames = self.thread.GetNumFrames() + name = 'thread #%u' % (self.thread.GetIndexID()) + value = '0x%x' % (self.thread.GetThreadID()) + summary = '%u frames' % (num_frames) + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : num_frames > 0, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + for frame in self.thread: + item_delegate = FrameTreeItemDelegate(frame) + item_dicts.append(item_delegate.get_item_dictionary()) + return item_dicts + +class ProcessTreeItemDelegate(object): + def __init__(self, process): + self.process = process + + def get_item_dictionary(self): + id = self.process.GetProcessID() + num_threads = self.process.GetNumThreads() + value = str(self.process.GetProcessID()) + summary = self.process.target.executable.fullpath + return { '#0' : 'process', + 'value': value, + 'summary': summary, + 'children' : num_threads > 0, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + for thread in self.process: + item_delegate = ThreadTreeItemDelegate(thread) + item_dicts.append(item_delegate.get_item_dictionary()) + return item_dicts + +class TargetTreeItemDelegate(object): + def __init__(self, target): + self.target = target + + def get_item_dictionary(self): + value = str(self.target.triple) + summary = self.target.executable.fullpath + return { '#0' : 'target', + 'value': value, + 'summary': summary, + 'children' : True, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + image_item_delegate = TargetImagesTreeItemDelegate(self.target) + item_dicts.append(image_item_delegate.get_item_dictionary()) + return item_dicts + +class TargetImagesTreeItemDelegate(object): + def __init__(self, target): + self.target = target + + def get_item_dictionary(self): + value = str(self.target.triple) + summary = self.target.executable.fullpath + num_modules = self.target.GetNumModules() + return { '#0' : 'images', + 'value': '', + 'summary': '%u images' % num_modules, + 'children' : num_modules > 0, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + for i in range(self.target.GetNumModules()): + module = self.target.GetModuleAtIndex(i) + image_item_delegate = ModuleTreeItemDelegate(self.target, module, i) + item_dicts.append(image_item_delegate.get_item_dictionary()) + return item_dicts + +class ModuleTreeItemDelegate(object): + def __init__(self, target, module, index): + self.target = target + self.module = module + self.index = index + + def get_item_dictionary(self): + name = 'module %u' % (self.index) + value = self.module.file.basename + summary = self.module.file.dirname + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : True, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + sections_item_delegate = ModuleSectionsTreeItemDelegate(self.target, self.module) + item_dicts.append(sections_item_delegate.get_item_dictionary()) + + symbols_item_delegate = ModuleSymbolsTreeItemDelegate(self.target, self.module) + item_dicts.append(symbols_item_delegate.get_item_dictionary()) + + comp_units_item_delegate = ModuleCompileUnitsTreeItemDelegate(self.target, self.module) + item_dicts.append(comp_units_item_delegate.get_item_dictionary()) + return item_dicts + +class ModuleSectionsTreeItemDelegate(object): + def __init__(self, target, module): + self.target = target + self.module = module + + def get_item_dictionary(self): + name = 'sections' + value = '' + summary = '%u sections' % (self.module.GetNumSections()) + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : True, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + num_sections = self.module.GetNumSections() + for i in range(num_sections): + section = self.module.GetSectionAtIndex(i) + image_item_delegate = SectionTreeItemDelegate(self.target, section) + item_dicts.append(image_item_delegate.get_item_dictionary()) + return item_dicts + +class SectionTreeItemDelegate(object): + def __init__(self, target, section): + self.target = target + self.section = section + + def get_item_dictionary(self): + name = self.section.name + section_load_addr = self.section.GetLoadAddress(self.target) + if section_load_addr != lldb.LLDB_INVALID_ADDRESS: + value = '0x%16.16x' % (section_load_addr) + else: + value = '0x%16.16x *' % (self.section.file_addr) + summary = '' + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : self.section.GetNumSubSections() > 0, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + num_sections = self.section.GetNumSubSections() + for i in range(num_sections): + section = self.section.GetSubSectionAtIndex(i) + image_item_delegate = SectionTreeItemDelegate(self.target, section) + item_dicts.append(image_item_delegate.get_item_dictionary()) + return item_dicts + +class ModuleCompileUnitsTreeItemDelegate(object): + def __init__(self, target, module): + self.target = target + self.module = module + + def get_item_dictionary(self): + name = 'compile units' + value = '' + summary = '%u compile units' % (self.module.GetNumSections()) + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : self.module.GetNumCompileUnits() > 0, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + num_cus = self.module.GetNumCompileUnits() + for i in range(num_cus): + cu = self.module.GetCompileUnitAtIndex(i) + image_item_delegate = CompileUnitTreeItemDelegate(self.target, cu) + item_dicts.append(image_item_delegate.get_item_dictionary()) + return item_dicts + +class CompileUnitTreeItemDelegate(object): + def __init__(self, target, cu): + self.target = target + self.cu = cu + + def get_item_dictionary(self): + name = self.cu.GetFileSpec().basename + value = '' + num_lines = self.cu.GetNumLineEntries() + summary = '' + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : num_lines > 0, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + item_delegate = LineTableTreeItemDelegate(self.target, self.cu) + item_dicts.append(item_delegate.get_item_dictionary()) + return item_dicts + +class LineTableTreeItemDelegate(object): + def __init__(self, target, cu): + self.target = target + self.cu = cu + + def get_item_dictionary(self): + name = 'line table' + value = '' + num_lines = self.cu.GetNumLineEntries() + summary = '%u line entries' % (num_lines) + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : num_lines > 0, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + num_lines = self.cu.GetNumLineEntries() + for i in range(num_lines): + line_entry = self.cu.GetLineEntryAtIndex(i) + item_delegate = LineEntryTreeItemDelegate(self.target, line_entry, i) + item_dicts.append(item_delegate.get_item_dictionary()) + return item_dicts + +class LineEntryTreeItemDelegate(object): + def __init__(self, target, line_entry, index): + self.target = target + self.line_entry = line_entry + self.index = index + + def get_item_dictionary(self): + name = str(self.index) + address = self.line_entry.GetStartAddress() + load_addr = address.GetLoadAddress(self.target) + if load_addr != lldb.LLDB_INVALID_ADDRESS: + value = '0x%16.16x' % (load_addr) + else: + value = '0x%16.16x *' % (address.file_addr) + summary = self.line_entry.GetFileSpec().fullpath + ':' + str(self.line_entry.line) + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : False, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + return item_dicts + +class InstructionTreeItemDelegate(object): + def __init__(self, target, instr): + self.target = target + self.instr = instr + + def get_item_dictionary(self): + address = self.instr.GetAddress() + load_addr = address.GetLoadAddress(self.target) + if load_addr != lldb.LLDB_INVALID_ADDRESS: + name = '0x%16.16x' % (load_addr) + else: + name = '0x%16.16x *' % (address.file_addr) + value = self.instr.GetMnemonic(self.target) + ' ' + self.instr.GetOperands(self.target) + summary = self.instr.GetComment(self.target) + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : False, + 'tree-item-delegate' : self } + +class ModuleSymbolsTreeItemDelegate(object): + def __init__(self, target, module): + self.target = target + self.module = module + + def get_item_dictionary(self): + name = 'symbols' + value = '' + summary = '%u symbols' % (self.module.GetNumSymbols()) + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : True, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + num_symbols = self.module.GetNumSymbols() + for i in range(num_symbols): + symbol = self.module.GetSymbolAtIndex(i) + image_item_delegate = SymbolTreeItemDelegate(self.target, symbol, i) + item_dicts.append(image_item_delegate.get_item_dictionary()) + return item_dicts + +class SymbolTreeItemDelegate(object): + def __init__(self, target, symbol, index): + self.target = target + self.symbol = symbol + self.index = index + + def get_item_dictionary(self): + address = self.symbol.GetStartAddress() + name = '[%u]' % self.index + symbol_load_addr = address.GetLoadAddress(self.target) + if symbol_load_addr != lldb.LLDB_INVALID_ADDRESS: + value = '0x%16.16x' % (symbol_load_addr) + else: + value = '0x%16.16x *' % (address.file_addr) + summary = self.symbol.name + return { '#0' : name, + 'value': value, + 'summary': summary, + 'children' : False, + 'tree-item-delegate' : self } + + def get_child_item_dictionaries(self): + item_dicts = list() + return item_dicts + + + +class DelegateTree(ttk.Frame): + + def __init__(self, column_dicts, delegate, title, name): + ttk.Frame.__init__(self, name=name) + self.pack(expand=Y, fill=BOTH) + self.master.title(title) + self.delegate = delegate + self.columns_dicts = column_dicts + self.item_id_to_item_dict = dict() + frame = Frame(self) + frame.pack(side=TOP, fill=BOTH, expand=Y) + self._create_treeview(frame) + self._populate_root() + + def _create_treeview(self, parent): + frame = ttk.Frame(parent) + frame.pack(side=TOP, fill=BOTH, expand=Y) + + column_ids = list() + for i in range(1,len(self.columns_dicts)): + column_ids.append(self.columns_dicts[i]['id']) + # create the tree and scrollbars + self.tree = ttk.Treeview(columns=column_ids) + + scroll_bar_v = ttk.Scrollbar(orient=VERTICAL, command= self.tree.yview) + scroll_bar_h = ttk.Scrollbar(orient=HORIZONTAL, command= self.tree.xview) + self.tree['yscroll'] = scroll_bar_v.set + self.tree['xscroll'] = scroll_bar_h.set + + # setup column headings and columns properties + for columns_dict in self.columns_dicts: + self.tree.heading(columns_dict['id'], text=columns_dict['text'], anchor=columns_dict['anchor']) + self.tree.column(columns_dict['id'], stretch=columns_dict['stretch']) + + # add tree and scrollbars to frame + self.tree.grid(in_=frame, row=0, column=0, sticky=NSEW) + scroll_bar_v.grid(in_=frame, row=0, column=1, sticky=NS) + scroll_bar_h.grid(in_=frame, row=1, column=0, sticky=EW) + + # set frame resizing priorities + frame.rowconfigure(0, weight=1) + frame.columnconfigure(0, weight=1) + + # action to perform when a node is expanded + self.tree.bind('<<TreeviewOpen>>', self._update_tree) + + def insert_items(self, parent_id, item_dicts): + for item_dict in item_dicts: + name = None + values = list() + first = True + for columns_dict in self.columns_dicts: + if first: + name = item_dict[columns_dict['id']] + first = False + else: + values.append(item_dict[columns_dict['id']]) + item_id = self.tree.insert (parent_id, # root item has an empty name + END, + text=name, + values=values) + self.item_id_to_item_dict[item_id] = item_dict + if item_dict['children']: + self.tree.insert(item_id, END, text='dummy') + + def _populate_root(self): + # use current directory as root node + self.insert_items('', self.delegate.get_child_item_dictionaries()) + + def _update_tree(self, event): + # user expanded a node - build the related directory + item_id = self.tree.focus() # the id of the expanded node + children = self.tree.get_children (item_id) + if len(children): + first_child = children[0] + # if the node only has a 'dummy' child, remove it and + # build new directory; skip if the node is already + # populated + if self.tree.item(first_child, option='text') == 'dummy': + self.tree.delete(first_child) + item_dict = self.item_id_to_item_dict[item_id] + item_dicts = item_dict['tree-item-delegate'].get_child_item_dictionaries() + self.insert_items(item_id, item_dicts) + +@lldb.command("tk-variables") +def tk_variable_display(debugger, command, result, dict): + sys.argv = ['tk-variables'] # needed for tree creation in TK library as it uses sys.argv... + target = debugger.GetSelectedTarget() + if not target: + print >>result, "invalid target" + return + process = target.GetProcess() + if not process: + print >>result, "invalid process" + return + thread = process.GetSelectedThread() + if not thread: + print >>result, "invalid thread" + return + frame = thread.GetSelectedFrame() + if not frame: + print >>result, "invalid frame" + return + # Parse command line args + command_args = shlex.split(command) + column_dicts = [{ 'id' : '#0' , 'text' : 'Name' , 'anchor' : W , 'stretch' : 0 }, + { 'id' : 'typename', 'text' : 'Type' , 'anchor' : W , 'stretch' : 0 }, + { 'id' : 'value' , 'text' : 'Value' , 'anchor' : W , 'stretch' : 0 }, + { 'id' : 'summary' , 'text' : 'Summary', 'anchor' : W , 'stretch' : 1 }] + tree = DelegateTree(column_dicts, FrameTreeItemDelegate(frame), 'Variables', 'lldb-tk-variables') + tree.mainloop() + +@lldb.command("tk-process") +def tk_process_display(debugger, command, result, dict): + sys.argv = ['tk-process'] # needed for tree creation in TK library as it uses sys.argv... + target = debugger.GetSelectedTarget() + if not target: + print >>result, "invalid target" + return + process = target.GetProcess() + if not process: + print >>result, "invalid process" + return + # Parse command line args + columnd_dicts = [{ 'id' : '#0' , 'text' : 'Name' , 'anchor' : W , 'stretch' : 0 }, + { 'id' : 'value' , 'text' : 'Value' , 'anchor' : W , 'stretch' : 0 }, + { 'id' : 'summary', 'text' : 'Summary', 'anchor' : W , 'stretch' : 1 }]; + command_args = shlex.split(command) + tree = DelegateTree(columnd_dicts, ProcessTreeItemDelegate(process), 'Process', 'lldb-tk-process') + tree.mainloop() + +@lldb.command("tk-target") +def tk_target_display(debugger, command, result, dict): + sys.argv = ['tk-target'] # needed for tree creation in TK library as it uses sys.argv... + target = debugger.GetSelectedTarget() + if not target: + print >>result, "invalid target" + return + # Parse command line args + columnd_dicts = [{ 'id' : '#0' , 'text' : 'Name' , 'anchor' : W , 'stretch' : 0 }, + { 'id' : 'value' , 'text' : 'Value' , 'anchor' : W , 'stretch' : 0 }, + { 'id' : 'summary', 'text' : 'Summary', 'anchor' : W , 'stretch' : 1 }]; + command_args = shlex.split(command) + tree = DelegateTree(columnd_dicts, TargetTreeItemDelegate(target), 'Target', 'lldb-tk-target') + tree.mainloop() + diff --git a/examples/python/mach_o.py b/examples/python/mach_o.py new file mode 100755 index 000000000000..a609b09ed0eb --- /dev/null +++ b/examples/python/mach_o.py @@ -0,0 +1,1687 @@ +#!/usr/bin/python + +import cmd +import dict_utils +import file_extract +import optparse +import re +import struct +import string +import StringIO +import sys +import uuid + +# Mach header "magic" constants +MH_MAGIC = 0xfeedface +MH_CIGAM = 0xcefaedfe +MH_MAGIC_64 = 0xfeedfacf +MH_CIGAM_64 = 0xcffaedfe +FAT_MAGIC = 0xcafebabe +FAT_CIGAM = 0xbebafeca + +# Mach haeder "filetype" constants +MH_OBJECT = 0x00000001 +MH_EXECUTE = 0x00000002 +MH_FVMLIB = 0x00000003 +MH_CORE = 0x00000004 +MH_PRELOAD = 0x00000005 +MH_DYLIB = 0x00000006 +MH_DYLINKER = 0x00000007 +MH_BUNDLE = 0x00000008 +MH_DYLIB_STUB = 0x00000009 +MH_DSYM = 0x0000000a +MH_KEXT_BUNDLE = 0x0000000b + +# Mach haeder "flag" constant bits +MH_NOUNDEFS = 0x00000001 +MH_INCRLINK = 0x00000002 +MH_DYLDLINK = 0x00000004 +MH_BINDATLOAD = 0x00000008 +MH_PREBOUND = 0x00000010 +MH_SPLIT_SEGS = 0x00000020 +MH_LAZY_INIT = 0x00000040 +MH_TWOLEVEL = 0x00000080 +MH_FORCE_FLAT = 0x00000100 +MH_NOMULTIDEFS = 0x00000200 +MH_NOFIXPREBINDING = 0x00000400 +MH_PREBINDABLE = 0x00000800 +MH_ALLMODSBOUND = 0x00001000 +MH_SUBSECTIONS_VIA_SYMBOLS = 0x00002000 +MH_CANONICAL = 0x00004000 +MH_WEAK_DEFINES = 0x00008000 +MH_BINDS_TO_WEAK = 0x00010000 +MH_ALLOW_STACK_EXECUTION = 0x00020000 +MH_ROOT_SAFE = 0x00040000 +MH_SETUID_SAFE = 0x00080000 +MH_NO_REEXPORTED_DYLIBS = 0x00100000 +MH_PIE = 0x00200000 +MH_DEAD_STRIPPABLE_DYLIB = 0x00400000 +MH_HAS_TLV_DESCRIPTORS = 0x00800000 +MH_NO_HEAP_EXECUTION = 0x01000000 + +# Mach load command constants +LC_REQ_DYLD = 0x80000000 +LC_SEGMENT = 0x00000001 +LC_SYMTAB = 0x00000002 +LC_SYMSEG = 0x00000003 +LC_THREAD = 0x00000004 +LC_UNIXTHREAD = 0x00000005 +LC_LOADFVMLIB = 0x00000006 +LC_IDFVMLIB = 0x00000007 +LC_IDENT = 0x00000008 +LC_FVMFILE = 0x00000009 +LC_PREPAGE = 0x0000000a +LC_DYSYMTAB = 0x0000000b +LC_LOAD_DYLIB = 0x0000000c +LC_ID_DYLIB = 0x0000000d +LC_LOAD_DYLINKER = 0x0000000e +LC_ID_DYLINKER = 0x0000000f +LC_PREBOUND_DYLIB = 0x00000010 +LC_ROUTINES = 0x00000011 +LC_SUB_FRAMEWORK = 0x00000012 +LC_SUB_UMBRELLA = 0x00000013 +LC_SUB_CLIENT = 0x00000014 +LC_SUB_LIBRARY = 0x00000015 +LC_TWOLEVEL_HINTS = 0x00000016 +LC_PREBIND_CKSUM = 0x00000017 +LC_LOAD_WEAK_DYLIB = 0x00000018 | LC_REQ_DYLD +LC_SEGMENT_64 = 0x00000019 +LC_ROUTINES_64 = 0x0000001a +LC_UUID = 0x0000001b +LC_RPATH = 0x0000001c | LC_REQ_DYLD +LC_CODE_SIGNATURE = 0x0000001d +LC_SEGMENT_SPLIT_INFO = 0x0000001e +LC_REEXPORT_DYLIB = 0x0000001f | LC_REQ_DYLD +LC_LAZY_LOAD_DYLIB = 0x00000020 +LC_ENCRYPTION_INFO = 0x00000021 +LC_DYLD_INFO = 0x00000022 +LC_DYLD_INFO_ONLY = 0x00000022 | LC_REQ_DYLD +LC_LOAD_UPWARD_DYLIB = 0x00000023 | LC_REQ_DYLD +LC_VERSION_MIN_MACOSX = 0x00000024 +LC_VERSION_MIN_IPHONEOS = 0x00000025 +LC_FUNCTION_STARTS = 0x00000026 +LC_DYLD_ENVIRONMENT = 0x00000027 + +# Mach CPU constants +CPU_ARCH_MASK = 0xff000000 +CPU_ARCH_ABI64 = 0x01000000 +CPU_TYPE_ANY = 0xffffffff +CPU_TYPE_VAX = 1 +CPU_TYPE_MC680x0 = 6 +CPU_TYPE_I386 = 7 +CPU_TYPE_X86_64 = CPU_TYPE_I386 | CPU_ARCH_ABI64 +CPU_TYPE_MIPS = 8 +CPU_TYPE_MC98000 = 10 +CPU_TYPE_HPPA = 11 +CPU_TYPE_ARM = 12 +CPU_TYPE_MC88000 = 13 +CPU_TYPE_SPARC = 14 +CPU_TYPE_I860 = 15 +CPU_TYPE_ALPHA = 16 +CPU_TYPE_POWERPC = 18 +CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64 + +# VM protection constants +VM_PROT_READ = 1 +VM_PROT_WRITE = 2 +VM_PROT_EXECUTE = 4 + +# VM protection constants +N_STAB = 0xe0 +N_PEXT = 0x10 +N_TYPE = 0x0e +N_EXT = 0x01 + +# Values for nlist N_TYPE bits of the "Mach.NList.type" field. +N_UNDF = 0x0 +N_ABS = 0x2 +N_SECT = 0xe +N_PBUD = 0xc +N_INDR = 0xa + +# Section indexes for the "Mach.NList.sect_idx" fields +NO_SECT = 0 +MAX_SECT = 255 + +# Stab defines +N_GSYM = 0x20 +N_FNAME = 0x22 +N_FUN = 0x24 +N_STSYM = 0x26 +N_LCSYM = 0x28 +N_BNSYM = 0x2e +N_OPT = 0x3c +N_RSYM = 0x40 +N_SLINE = 0x44 +N_ENSYM = 0x4e +N_SSYM = 0x60 +N_SO = 0x64 +N_OSO = 0x66 +N_LSYM = 0x80 +N_BINCL = 0x82 +N_SOL = 0x84 +N_PARAMS = 0x86 +N_VERSION = 0x88 +N_OLEVEL = 0x8A +N_PSYM = 0xa0 +N_EINCL = 0xa2 +N_ENTRY = 0xa4 +N_LBRAC = 0xc0 +N_EXCL = 0xc2 +N_RBRAC = 0xe0 +N_BCOMM = 0xe2 +N_ECOMM = 0xe4 +N_ECOML = 0xe8 +N_LENG = 0xfe + +vm_prot_names = [ '---', 'r--', '-w-', 'rw-', '--x', 'r-x', '-wx', 'rwx' ] + +def dump_memory(base_addr, data, hex_bytes_len, num_per_line): + hex_bytes = data.encode('hex') + if hex_bytes_len == -1: + hex_bytes_len = len(hex_bytes) + addr = base_addr + ascii_str = '' + i = 0 + while i < hex_bytes_len: + if ((i/2) % num_per_line) == 0: + if i > 0: + print ' %s' % (ascii_str) + ascii_str = '' + print '0x%8.8x:' % (addr+i), + hex_byte = hex_bytes[i:i+2] + print hex_byte, + int_byte = int (hex_byte, 16) + ascii_char = '%c' % (int_byte) + if int_byte >= 32 and int_byte < 127: + ascii_str += ascii_char + else: + ascii_str += '.' + i = i + 2 + if ascii_str: + if (i/2) % num_per_line: + padding = num_per_line - ((i/2) % num_per_line) + else: + padding = 0 + print '%*s%s' % (padding*3+1,'',ascii_str) + print + + +class TerminalColors: + '''Simple terminal colors class''' + def __init__(self, enabled = True): + # TODO: discover terminal type from "file" and disable if + # it can't handle the color codes + self.enabled = enabled + + def reset(self): + '''Reset all terminal colors and formatting.''' + if self.enabled: + return "\x1b[0m"; + return '' + + def bold(self, on = True): + '''Enable or disable bold depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[1m"; + else: + return "\x1b[22m"; + return '' + + def italics(self, on = True): + '''Enable or disable italics depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[3m"; + else: + return "\x1b[23m"; + return '' + + def underline(self, on = True): + '''Enable or disable underline depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[4m"; + else: + return "\x1b[24m"; + return '' + + def inverse(self, on = True): + '''Enable or disable inverse depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[7m"; + else: + return "\x1b[27m"; + return '' + + def strike(self, on = True): + '''Enable or disable strike through depending on the "on" parameter.''' + if self.enabled: + if on: + return "\x1b[9m"; + else: + return "\x1b[29m"; + return '' + + def black(self, fg = True): + '''Set the foreground or background color to black. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[30m"; + else: + return "\x1b[40m"; + return '' + + def red(self, fg = True): + '''Set the foreground or background color to red. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[31m"; + else: + return "\x1b[41m"; + return '' + + def green(self, fg = True): + '''Set the foreground or background color to green. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[32m"; + else: + return "\x1b[42m"; + return '' + + def yellow(self, fg = True): + '''Set the foreground or background color to yellow. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[43m"; + else: + return "\x1b[33m"; + return '' + + def blue(self, fg = True): + '''Set the foreground or background color to blue. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[34m"; + else: + return "\x1b[44m"; + return '' + + def magenta(self, fg = True): + '''Set the foreground or background color to magenta. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[35m"; + else: + return "\x1b[45m"; + return '' + + def cyan(self, fg = True): + '''Set the foreground or background color to cyan. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[36m"; + else: + return "\x1b[46m"; + return '' + + def white(self, fg = True): + '''Set the foreground or background color to white. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[37m"; + else: + return "\x1b[47m"; + return '' + + def default(self, fg = True): + '''Set the foreground or background color to the default. + The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.''' + if self.enabled: + if fg: + return "\x1b[39m"; + else: + return "\x1b[49m"; + return '' + +def swap_unpack_char(): + """Returns the unpack prefix that will for non-native endian-ness.""" + if struct.pack('H', 1).startswith("\x00"): + return '<' + return '>' + + +def dump_hex_bytes(addr, s, bytes_per_line=16): + i = 0 + line = '' + for ch in s: + if (i % bytes_per_line) == 0: + if line: + print line + line = '%#8.8x: ' % (addr + i) + line += "%02X " % ord(ch) + i += 1 + print line + +def dump_hex_byte_string_diff(addr, a, b, bytes_per_line=16): + i = 0 + line = '' + a_len = len(a) + b_len = len(b) + if a_len < b_len: + max_len = b_len + else: + max_len = a_len + tty_colors = TerminalColors (True) + for i in range(max_len): + ch = None + if i < a_len: + ch_a = a[i] + ch = ch_a + else: + ch_a = None + if i < b_len: + ch_b = b[i] + if not ch: + ch = ch_b + else: + ch_b = None + mismatch = ch_a != ch_b + if (i % bytes_per_line) == 0: + if line: + print line + line = '%#8.8x: ' % (addr + i) + if mismatch: line += tty_colors.red() + line += "%02X " % ord(ch) + if mismatch: line += tty_colors.default() + i += 1 + + print line + +class Mach: + """Class that does everything mach-o related""" + + class Arch: + """Class that implements mach-o architectures""" + + def __init__(self, c=0, s=0): + self.cpu=c + self.sub=s + + def set_cpu_type(self, c): + self.cpu=c + def set_cpu_subtype(self, s): + self.sub=s + def set_arch(self, c, s): + self.cpu=c + self.sub=s + def is_64_bit(self): + return (self.cpu & CPU_ARCH_ABI64) != 0 + + cpu_infos = [ + [ "arm" , CPU_TYPE_ARM , CPU_TYPE_ANY ], + [ "arm" , CPU_TYPE_ARM , 0 ], + [ "armv4" , CPU_TYPE_ARM , 5 ], + [ "armv6" , CPU_TYPE_ARM , 6 ], + [ "armv5" , CPU_TYPE_ARM , 7 ], + [ "xscale" , CPU_TYPE_ARM , 8 ], + [ "armv7" , CPU_TYPE_ARM , 9 ], + [ "armv7f" , CPU_TYPE_ARM , 10 ], + [ "armv7s" , CPU_TYPE_ARM , 11 ], + [ "armv7k" , CPU_TYPE_ARM , 12 ], + [ "armv7m" , CPU_TYPE_ARM , 15 ], + [ "armv7em" , CPU_TYPE_ARM , 16 ], + [ "ppc" , CPU_TYPE_POWERPC , CPU_TYPE_ANY ], + [ "ppc" , CPU_TYPE_POWERPC , 0 ], + [ "ppc601" , CPU_TYPE_POWERPC , 1 ], + [ "ppc602" , CPU_TYPE_POWERPC , 2 ], + [ "ppc603" , CPU_TYPE_POWERPC , 3 ], + [ "ppc603e" , CPU_TYPE_POWERPC , 4 ], + [ "ppc603ev" , CPU_TYPE_POWERPC , 5 ], + [ "ppc604" , CPU_TYPE_POWERPC , 6 ], + [ "ppc604e" , CPU_TYPE_POWERPC , 7 ], + [ "ppc620" , CPU_TYPE_POWERPC , 8 ], + [ "ppc750" , CPU_TYPE_POWERPC , 9 ], + [ "ppc7400" , CPU_TYPE_POWERPC , 10 ], + [ "ppc7450" , CPU_TYPE_POWERPC , 11 ], + [ "ppc970" , CPU_TYPE_POWERPC , 100 ], + [ "ppc64" , CPU_TYPE_POWERPC64 , 0 ], + [ "ppc970-64" , CPU_TYPE_POWERPC64 , 100 ], + [ "i386" , CPU_TYPE_I386 , 3 ], + [ "i486" , CPU_TYPE_I386 , 4 ], + [ "i486sx" , CPU_TYPE_I386 , 0x84 ], + [ "i386" , CPU_TYPE_I386 , CPU_TYPE_ANY ], + [ "x86_64" , CPU_TYPE_X86_64 , 3 ], + [ "x86_64" , CPU_TYPE_X86_64 , CPU_TYPE_ANY ], + ] + + def __str__(self): + for info in self.cpu_infos: + if self.cpu == info[1] and (self.sub & 0x00ffffff) == info[2]: + return info[0] + return "{0}.{1}".format(self.cpu,self.sub) + + + class Magic(dict_utils.Enum): + + enum = { + 'MH_MAGIC' : MH_MAGIC, + 'MH_CIGAM' : MH_CIGAM, + 'MH_MAGIC_64' : MH_MAGIC_64, + 'MH_CIGAM_64' : MH_CIGAM_64, + 'FAT_MAGIC' : FAT_MAGIC, + 'FAT_CIGAM' : FAT_CIGAM + } + + def __init__(self, initial_value = 0): + dict_utils.Enum.__init__(self, initial_value, self.enum) + + def is_skinny_mach_file(self): + return self.value == MH_MAGIC or self.value == MH_CIGAM or self.value == MH_MAGIC_64 or self.value == MH_CIGAM_64 + + def is_universal_mach_file(self): + return self.value == FAT_MAGIC or self.value == FAT_CIGAM + + def unpack(self, data): + data.set_byte_order('native') + self.value = data.get_uint32(); + + def get_byte_order(self): + if self.value == MH_CIGAM or self.value == MH_CIGAM_64 or self.value == FAT_CIGAM: + return swap_unpack_char() + else: + return '=' + + def is_64_bit(self): + return self.value == MH_MAGIC_64 or self.value == MH_CIGAM_64 + + def __init__(self): + self.magic = Mach.Magic() + self.content = None + self.path = None + + def extract (self, path, extractor): + self.path = path; + self.unpack(extractor) + + def parse(self, path): + self.path = path; + try: + f = open(self.path) + file_extractor = file_extract.FileExtract(f, '=') + self.unpack(file_extractor) + #f.close() + except IOError as (errno, strerror): + print "I/O error({0}): {1}".format(errno, strerror) + except ValueError: + print "Could not convert data to an integer." + except: + print "Unexpected error:", sys.exc_info()[0] + raise + + def compare(self, rhs): + self.content.compare(rhs.content) + + def dump(self, options = None): + self.content.dump(options) + + def dump_header(self, dump_description = True, options = None): + self.content.dump_header(dump_description, options) + + def dump_load_commands(self, dump_description = True, options = None): + self.content.dump_load_commands(dump_description, options) + + def dump_sections(self, dump_description = True, options = None): + self.content.dump_sections(dump_description, options) + + def dump_section_contents(self, options): + self.content.dump_section_contents(options) + + def dump_symtab(self, dump_description = True, options = None): + self.content.dump_symtab(dump_description, options) + + def dump_symbol_names_matching_regex(self, regex, file=None): + self.content.dump_symbol_names_matching_regex(regex, file) + + def description(self): + return self.content.description() + + def unpack(self, data): + self.magic.unpack(data) + if self.magic.is_skinny_mach_file(): + self.content = Mach.Skinny(self.path) + elif self.magic.is_universal_mach_file(): + self.content = Mach.Universal(self.path) + else: + self.content = None + + if self.content != None: + self.content.unpack(data, self.magic) + + def is_valid(self): + return self.content != None + + class Universal: + + def __init__(self, path): + self.path = path + self.type = 'universal' + self.file_off = 0 + self.magic = None + self.nfat_arch = 0 + self.archs = list() + + def description(self): + s = '%#8.8x: %s (' % (self.file_off, self.path) + archs_string = '' + for arch in self.archs: + if len(archs_string): + archs_string += ', ' + archs_string += '%s' % arch.arch + s += archs_string + s += ')' + return s + + def unpack(self, data, magic = None): + self.file_off = data.tell() + if magic is None: + self.magic = Mach.Magic() + self.magic.unpack(data) + else: + self.magic = magic + self.file_off = self.file_off - 4 + # Universal headers are always in big endian + data.set_byte_order('big') + self.nfat_arch = data.get_uint32() + for i in range(self.nfat_arch): + self.archs.append(Mach.Universal.ArchInfo()) + self.archs[i].unpack(data) + for i in range(self.nfat_arch): + self.archs[i].mach = Mach.Skinny(self.path) + data.seek (self.archs[i].offset, 0) + skinny_magic = Mach.Magic() + skinny_magic.unpack (data) + self.archs[i].mach.unpack(data, skinny_magic) + + def compare(self, rhs): + print 'error: comparing two universal files is not supported yet' + return False + + def dump(self, options): + if options.dump_header: + print + print "Universal Mach File: magic = %s, nfat_arch = %u" % (self.magic, self.nfat_arch) + print + if self.nfat_arch > 0: + if options.dump_header: + self.archs[0].dump_header(True, options) + for i in range(self.nfat_arch): + self.archs[i].dump_flat(options) + if options.dump_header: + print + for i in range(self.nfat_arch): + self.archs[i].mach.dump(options) + + def dump_header(self, dump_description = True, options = None): + if dump_description: + print self.description() + for i in range(self.nfat_arch): + self.archs[i].mach.dump_header(True, options) + print + + def dump_load_commands(self, dump_description = True, options = None): + if dump_description: + print self.description() + for i in range(self.nfat_arch): + self.archs[i].mach.dump_load_commands(True, options) + print + + def dump_sections(self, dump_description = True, options = None): + if dump_description: + print self.description() + for i in range(self.nfat_arch): + self.archs[i].mach.dump_sections(True, options) + print + + def dump_section_contents(self, options): + for i in range(self.nfat_arch): + self.archs[i].mach.dump_section_contents(options) + print + + def dump_symtab(self, dump_description = True, options = None): + if dump_description: + print self.description() + for i in range(self.nfat_arch): + self.archs[i].mach.dump_symtab(True, options) + print + + def dump_symbol_names_matching_regex(self, regex, file=None): + for i in range(self.nfat_arch): + self.archs[i].mach.dump_symbol_names_matching_regex(regex, file) + + class ArchInfo: + + def __init__(self): + self.arch = Mach.Arch(0,0) + self.offset = 0 + self.size = 0 + self.align = 0 + self.mach = None + + def unpack(self, data): + # Universal headers are always in big endian + data.set_byte_order('big') + self.arch.cpu, self.arch.sub, self.offset, self.size, self.align = data.get_n_uint32(5) + + def dump_header(self, dump_description = True, options = None): + if options.verbose: + print "CPU SUBTYPE OFFSET SIZE ALIGN" + print "---------- ---------- ---------- ---------- ----------" + else: + print "ARCH FILEOFFSET FILESIZE ALIGN" + print "---------- ---------- ---------- ----------" + def dump_flat(self, options): + if options.verbose: + print "%#8.8x %#8.8x %#8.8x %#8.8x %#8.8x" % (self.arch.cpu, self.arch.sub, self.offset, self.size, self.align) + else: + print "%-10s %#8.8x %#8.8x %#8.8x" % (self.arch, self.offset, self.size, self.align) + def dump(self): + print " cputype: %#8.8x" % self.arch.cpu + print "cpusubtype: %#8.8x" % self.arch.sub + print " offset: %#8.8x" % self.offset + print " size: %#8.8x" % self.size + print " align: %#8.8x" % self.align + def __str__(self): + return "Mach.Universal.ArchInfo: %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x" % (self.arch.cpu, self.arch.sub, self.offset, self.size, self.align) + def __repr__(self): + return "Mach.Universal.ArchInfo: %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x" % (self.arch.cpu, self.arch.sub, self.offset, self.size, self.align) + + class Flags: + + def __init__(self, b): + self.bits = b + + def __str__(self): + s = '' + if self.bits & MH_NOUNDEFS: + s += 'MH_NOUNDEFS | ' + if self.bits & MH_INCRLINK: + s += 'MH_INCRLINK | ' + if self.bits & MH_DYLDLINK: + s += 'MH_DYLDLINK | ' + if self.bits & MH_BINDATLOAD: + s += 'MH_BINDATLOAD | ' + if self.bits & MH_PREBOUND: + s += 'MH_PREBOUND | ' + if self.bits & MH_SPLIT_SEGS: + s += 'MH_SPLIT_SEGS | ' + if self.bits & MH_LAZY_INIT: + s += 'MH_LAZY_INIT | ' + if self.bits & MH_TWOLEVEL: + s += 'MH_TWOLEVEL | ' + if self.bits & MH_FORCE_FLAT: + s += 'MH_FORCE_FLAT | ' + if self.bits & MH_NOMULTIDEFS: + s += 'MH_NOMULTIDEFS | ' + if self.bits & MH_NOFIXPREBINDING: + s += 'MH_NOFIXPREBINDING | ' + if self.bits & MH_PREBINDABLE: + s += 'MH_PREBINDABLE | ' + if self.bits & MH_ALLMODSBOUND: + s += 'MH_ALLMODSBOUND | ' + if self.bits & MH_SUBSECTIONS_VIA_SYMBOLS: + s += 'MH_SUBSECTIONS_VIA_SYMBOLS | ' + if self.bits & MH_CANONICAL: + s += 'MH_CANONICAL | ' + if self.bits & MH_WEAK_DEFINES: + s += 'MH_WEAK_DEFINES | ' + if self.bits & MH_BINDS_TO_WEAK: + s += 'MH_BINDS_TO_WEAK | ' + if self.bits & MH_ALLOW_STACK_EXECUTION: + s += 'MH_ALLOW_STACK_EXECUTION | ' + if self.bits & MH_ROOT_SAFE: + s += 'MH_ROOT_SAFE | ' + if self.bits & MH_SETUID_SAFE: + s += 'MH_SETUID_SAFE | ' + if self.bits & MH_NO_REEXPORTED_DYLIBS: + s += 'MH_NO_REEXPORTED_DYLIBS | ' + if self.bits & MH_PIE: + s += 'MH_PIE | ' + if self.bits & MH_DEAD_STRIPPABLE_DYLIB: + s += 'MH_DEAD_STRIPPABLE_DYLIB | ' + if self.bits & MH_HAS_TLV_DESCRIPTORS: + s += 'MH_HAS_TLV_DESCRIPTORS | ' + if self.bits & MH_NO_HEAP_EXECUTION: + s += 'MH_NO_HEAP_EXECUTION | ' + # Strip the trailing " |" if we have any flags + if len(s) > 0: + s = s[0:-2] + return s + + class FileType(dict_utils.Enum): + + enum = { + 'MH_OBJECT' : MH_OBJECT , + 'MH_EXECUTE' : MH_EXECUTE , + 'MH_FVMLIB' : MH_FVMLIB , + 'MH_CORE' : MH_CORE , + 'MH_PRELOAD' : MH_PRELOAD , + 'MH_DYLIB' : MH_DYLIB , + 'MH_DYLINKER' : MH_DYLINKER , + 'MH_BUNDLE' : MH_BUNDLE , + 'MH_DYLIB_STUB' : MH_DYLIB_STUB , + 'MH_DSYM' : MH_DSYM , + 'MH_KEXT_BUNDLE' : MH_KEXT_BUNDLE + } + + def __init__(self, initial_value = 0): + dict_utils.Enum.__init__(self, initial_value, self.enum) + + class Skinny: + + def __init__(self, path): + self.path = path + self.type = 'skinny' + self.data = None + self.file_off = 0 + self.magic = 0 + self.arch = Mach.Arch(0,0) + self.filetype = Mach.FileType(0) + self.ncmds = 0 + self.sizeofcmds = 0 + self.flags = Mach.Flags(0) + self.uuid = None + self.commands = list() + self.segments = list() + self.sections = list() + self.symbols = list() + self.sections.append(Mach.Section()) + + def description(self): + return '%#8.8x: %s (%s)' % (self.file_off, self.path, self.arch) + + def unpack(self, data, magic = None): + self.data = data + self.file_off = data.tell() + if magic is None: + self.magic = Mach.Magic() + self.magic.unpack(data) + else: + self.magic = magic + self.file_off = self.file_off - 4 + data.set_byte_order(self.magic.get_byte_order()) + self.arch.cpu, self.arch.sub, self.filetype.value, self.ncmds, self.sizeofcmds, bits = data.get_n_uint32(6) + self.flags.bits = bits + + if self.is_64_bit(): + data.get_uint32() # Skip reserved word in mach_header_64 + + for i in range(0,self.ncmds): + lc = self.unpack_load_command (data) + self.commands.append (lc) + + def get_data(self): + if self.data: + self.data.set_byte_order(self.magic.get_byte_order()) + return self.data + return None + + def unpack_load_command (self, data): + lc = Mach.LoadCommand() + lc.unpack (self, data) + lc_command = lc.command.get_enum_value(); + if (lc_command == LC_SEGMENT or + lc_command == LC_SEGMENT_64): + lc = Mach.SegmentLoadCommand(lc) + lc.unpack(self, data) + elif (lc_command == LC_LOAD_DYLIB or + lc_command == LC_ID_DYLIB or + lc_command == LC_LOAD_WEAK_DYLIB or + lc_command == LC_REEXPORT_DYLIB): + lc = Mach.DylibLoadCommand(lc) + lc.unpack(self, data) + elif (lc_command == LC_LOAD_DYLINKER or + lc_command == LC_SUB_FRAMEWORK or + lc_command == LC_SUB_CLIENT or + lc_command == LC_SUB_UMBRELLA or + lc_command == LC_SUB_LIBRARY or + lc_command == LC_ID_DYLINKER or + lc_command == LC_RPATH): + lc = Mach.LoadDYLDLoadCommand(lc) + lc.unpack(self, data) + elif (lc_command == LC_DYLD_INFO_ONLY): + lc = Mach.DYLDInfoOnlyLoadCommand(lc) + lc.unpack(self, data) + elif (lc_command == LC_SYMTAB): + lc = Mach.SymtabLoadCommand(lc) + lc.unpack(self, data) + elif (lc_command == LC_DYSYMTAB): + lc = Mach.DYLDSymtabLoadCommand(lc) + lc.unpack(self, data) + elif (lc_command == LC_UUID): + lc = Mach.UUIDLoadCommand(lc) + lc.unpack(self, data) + elif (lc_command == LC_CODE_SIGNATURE or + lc_command == LC_SEGMENT_SPLIT_INFO or + lc_command == LC_FUNCTION_STARTS): + lc = Mach.DataBlobLoadCommand(lc) + lc.unpack(self, data) + elif (lc_command == LC_UNIXTHREAD): + lc = Mach.UnixThreadLoadCommand(lc) + lc.unpack(self, data) + elif (lc_command == LC_ENCRYPTION_INFO): + lc = Mach.EncryptionInfoLoadCommand(lc) + lc.unpack(self, data) + lc.skip(data) + return lc + + def compare(self, rhs): + print "\nComparing:" + print "a) %s %s" % (self.arch, self.path) + print "b) %s %s" % (rhs.arch, rhs.path) + result = True + if self.type == rhs.type: + for lhs_section in self.sections[1:]: + rhs_section = rhs.get_section_by_section(lhs_section) + if rhs_section: + print 'comparing %s.%s...' % (lhs_section.segname, lhs_section.sectname), + sys.stdout.flush() + lhs_data = lhs_section.get_contents (self) + rhs_data = rhs_section.get_contents (rhs) + if lhs_data and rhs_data: + if lhs_data == rhs_data: + print 'ok' + else: + lhs_data_len = len(lhs_data) + rhs_data_len = len(rhs_data) + # if lhs_data_len < rhs_data_len: + # if lhs_data == rhs_data[0:lhs_data_len]: + # print 'section data for %s matches the first %u bytes' % (lhs_section.sectname, lhs_data_len) + # else: + # # TODO: check padding + # result = False + # elif lhs_data_len > rhs_data_len: + # if lhs_data[0:rhs_data_len] == rhs_data: + # print 'section data for %s matches the first %u bytes' % (lhs_section.sectname, lhs_data_len) + # else: + # # TODO: check padding + # result = False + # else: + result = False + print 'error: sections differ' + #print 'a) %s' % (lhs_section) + # dump_hex_byte_string_diff(0, lhs_data, rhs_data) + #print 'b) %s' % (rhs_section) + # dump_hex_byte_string_diff(0, rhs_data, lhs_data) + elif lhs_data and not rhs_data: + print 'error: section data missing from b:' + print 'a) %s' % (lhs_section) + print 'b) %s' % (rhs_section) + result = False + elif not lhs_data and rhs_data: + print 'error: section data missing from a:' + print 'a) %s' % (lhs_section) + print 'b) %s' % (rhs_section) + result = False + elif lhs_section.offset or rhs_section.offset: + print 'error: section data missing for both a and b:' + print 'a) %s' % (lhs_section) + print 'b) %s' % (rhs_section) + result = False + else: + print 'ok' + else: + result = False + print 'error: section %s is missing in %s' % (lhs_section.sectname, rhs.path) + else: + print 'error: comaparing a %s mach-o file with a %s mach-o file is not supported' % (self.type, rhs.type) + result = False + if not result: + print 'error: mach files differ' + return result + def dump_header(self, dump_description = True, options = None): + if options.verbose: + print "MAGIC CPU SUBTYPE FILETYPE NUM CMDS SIZE CMDS FLAGS" + print "---------- ---------- ---------- ---------- -------- ---------- ----------" + else: + print "MAGIC ARCH FILETYPE NUM CMDS SIZE CMDS FLAGS" + print "------------ ---------- -------------- -------- ---------- ----------" + + def dump_flat(self, options): + if options.verbose: + print "%#8.8x %#8.8x %#8.8x %#8.8x %#8u %#8.8x %#8.8x" % (self.magic, self.arch.cpu , self.arch.sub, self.filetype.value, self.ncmds, self.sizeofcmds, self.flags.bits) + else: + print "%-12s %-10s %-14s %#8u %#8.8x %s" % (self.magic, self.arch, self.filetype, self.ncmds, self.sizeofcmds, self.flags) + + def dump(self, options): + if options.dump_header: + self.dump_header(True, options) + if options.dump_load_commands: + self.dump_load_commands(False, options) + if options.dump_sections: + self.dump_sections(False, options) + if options.section_names: + self.dump_section_contents(options) + if options.dump_symtab: + self.get_symtab() + if len(self.symbols): + self.dump_sections(False, options) + else: + print "No symbols" + if options.find_mangled: + self.dump_symbol_names_matching_regex (re.compile('^_?_Z')) + + def dump_header(self, dump_description = True, options = None): + if dump_description: + print self.description() + print "Mach Header" + print " magic: %#8.8x %s" % (self.magic.value, self.magic) + print " cputype: %#8.8x %s" % (self.arch.cpu, self.arch) + print " cpusubtype: %#8.8x" % self.arch.sub + print " filetype: %#8.8x %s" % (self.filetype.get_enum_value(), self.filetype.get_enum_name()) + print " ncmds: %#8.8x %u" % (self.ncmds, self.ncmds) + print " sizeofcmds: %#8.8x" % self.sizeofcmds + print " flags: %#8.8x %s" % (self.flags.bits, self.flags) + + def dump_load_commands(self, dump_description = True, options = None): + if dump_description: + print self.description() + for lc in self.commands: + print lc + + def get_section_by_name (self, name): + for section in self.sections: + if section.sectname and section.sectname == name: + return section + return None + + def get_section_by_section (self, other_section): + for section in self.sections: + if section.sectname == other_section.sectname and section.segname == other_section.segname: + return section + return None + + def dump_sections(self, dump_description = True, options = None): + if dump_description: + print self.description() + num_sections = len(self.sections) + if num_sections > 1: + self.sections[1].dump_header() + for sect_idx in range(1,num_sections): + print "%s" % self.sections[sect_idx] + + def dump_section_contents(self, options): + saved_section_to_disk = False + for sectname in options.section_names: + section = self.get_section_by_name(sectname) + if section: + sect_bytes = section.get_contents (self) + if options.outfile: + if not saved_section_to_disk: + outfile = open(options.outfile, 'w') + if options.extract_modules: + #print "Extracting modules from mach file..." + data = file_extract.FileExtract(StringIO.StringIO(sect_bytes), self.data.byte_order) + version = data.get_uint32() + num_modules = data.get_uint32() + #print "version = %u, num_modules = %u" % (version, num_modules) + for i in range(num_modules): + data_offset = data.get_uint64() + data_size = data.get_uint64() + name_offset = data.get_uint32() + language = data.get_uint32() + flags = data.get_uint32() + data.seek (name_offset) + module_name = data.get_c_string() + #print "module[%u] data_offset = %#16.16x, data_size = %#16.16x, name_offset = %#16.16x (%s), language = %u, flags = %#x" % (i, data_offset, data_size, name_offset, module_name, language, flags) + data.seek (data_offset) + outfile.write(data.read_size (data_size)) + else: + print "Saving section %s to '%s'" % (sectname, options.outfile) + outfile.write(sect_bytes) + outfile.close() + saved_section_to_disk = True + else: + print "error: you can only save a single section to disk at a time, skipping section '%s'" % (sectname) + else: + print 'section %s:\n' % (sectname) + section.dump_header() + print '%s\n' % (section) + dump_memory (0, sect_bytes, options.max_count, 16) + else: + print 'error: no section named "%s" was found' % (sectname) + + def get_segment(self, segname): + if len(self.segments) == 1 and self.segments[0].segname == '': + return self.segments[0] + for segment in self.segments: + if segment.segname == segname: + return segment + return None + + def get_first_load_command(self, lc_enum_value): + for lc in self.commands: + if lc.command.value == lc_enum_value: + return lc + return None + + def get_symtab(self): + if self.data and not self.symbols: + lc_symtab = self.get_first_load_command (LC_SYMTAB) + if lc_symtab: + symtab_offset = self.file_off + if self.data.is_in_memory(): + linkedit_segment = self.get_segment('__LINKEDIT') + if linkedit_segment: + linkedit_vmaddr = linkedit_segment.vmaddr + linkedit_fileoff = linkedit_segment.fileoff + symtab_offset = linkedit_vmaddr + lc_symtab.symoff - linkedit_fileoff + symtab_offset = linkedit_vmaddr + lc_symtab.stroff - linkedit_fileoff + else: + symtab_offset += lc_symtab.symoff + + self.data.seek (symtab_offset) + is_64 = self.is_64_bit() + for i in range(lc_symtab.nsyms): + nlist = Mach.NList() + nlist.unpack (self, self.data, lc_symtab) + self.symbols.append(nlist) + else: + print "no LC_SYMTAB" + + def dump_symtab(self, dump_description = True, options = None): + self.get_symtab() + if dump_description: + print self.description() + for i, symbol in enumerate(self.symbols): + print '[%5u] %s' % (i, symbol) + + def dump_symbol_names_matching_regex(self, regex, file=None): + self.get_symtab() + for symbol in self.symbols: + if symbol.name and regex.search (symbol.name): + print symbol.name + if file: + file.write('%s\n' % (symbol.name)) + + def is_64_bit(self): + return self.magic.is_64_bit() + + class LoadCommand: + class Command(dict_utils.Enum): + enum = { + 'LC_SEGMENT' : LC_SEGMENT, + 'LC_SYMTAB' : LC_SYMTAB, + 'LC_SYMSEG' : LC_SYMSEG, + 'LC_THREAD' : LC_THREAD, + 'LC_UNIXTHREAD' : LC_UNIXTHREAD, + 'LC_LOADFVMLIB' : LC_LOADFVMLIB, + 'LC_IDFVMLIB' : LC_IDFVMLIB, + 'LC_IDENT' : LC_IDENT, + 'LC_FVMFILE' : LC_FVMFILE, + 'LC_PREPAGE' : LC_PREPAGE, + 'LC_DYSYMTAB' : LC_DYSYMTAB, + 'LC_LOAD_DYLIB' : LC_LOAD_DYLIB, + 'LC_ID_DYLIB' : LC_ID_DYLIB, + 'LC_LOAD_DYLINKER' : LC_LOAD_DYLINKER, + 'LC_ID_DYLINKER' : LC_ID_DYLINKER, + 'LC_PREBOUND_DYLIB' : LC_PREBOUND_DYLIB, + 'LC_ROUTINES' : LC_ROUTINES, + 'LC_SUB_FRAMEWORK' : LC_SUB_FRAMEWORK, + 'LC_SUB_UMBRELLA' : LC_SUB_UMBRELLA, + 'LC_SUB_CLIENT' : LC_SUB_CLIENT, + 'LC_SUB_LIBRARY' : LC_SUB_LIBRARY, + 'LC_TWOLEVEL_HINTS' : LC_TWOLEVEL_HINTS, + 'LC_PREBIND_CKSUM' : LC_PREBIND_CKSUM, + 'LC_LOAD_WEAK_DYLIB' : LC_LOAD_WEAK_DYLIB, + 'LC_SEGMENT_64' : LC_SEGMENT_64, + 'LC_ROUTINES_64' : LC_ROUTINES_64, + 'LC_UUID' : LC_UUID, + 'LC_RPATH' : LC_RPATH, + 'LC_CODE_SIGNATURE' : LC_CODE_SIGNATURE, + 'LC_SEGMENT_SPLIT_INFO' : LC_SEGMENT_SPLIT_INFO, + 'LC_REEXPORT_DYLIB' : LC_REEXPORT_DYLIB, + 'LC_LAZY_LOAD_DYLIB' : LC_LAZY_LOAD_DYLIB, + 'LC_ENCRYPTION_INFO' : LC_ENCRYPTION_INFO, + 'LC_DYLD_INFO' : LC_DYLD_INFO, + 'LC_DYLD_INFO_ONLY' : LC_DYLD_INFO_ONLY, + 'LC_LOAD_UPWARD_DYLIB' : LC_LOAD_UPWARD_DYLIB, + 'LC_VERSION_MIN_MACOSX' : LC_VERSION_MIN_MACOSX, + 'LC_VERSION_MIN_IPHONEOS' : LC_VERSION_MIN_IPHONEOS, + 'LC_FUNCTION_STARTS' : LC_FUNCTION_STARTS, + 'LC_DYLD_ENVIRONMENT' : LC_DYLD_ENVIRONMENT + } + + def __init__(self, initial_value = 0): + dict_utils.Enum.__init__(self, initial_value, self.enum) + + + def __init__(self, c=None, l=0,o=0): + if c != None: + self.command = c + else: + self.command = Mach.LoadCommand.Command(0) + self.length = l + self.file_off = o + + def unpack(self, mach_file, data): + self.file_off = data.tell() + self.command.value, self.length = data.get_n_uint32(2) + + def skip(self, data): + data.seek (self.file_off + self.length, 0) + + def __str__(self): + lc_name = self.command.get_enum_name() + return '%#8.8x: <%#4.4x> %-24s' % (self.file_off, self.length, lc_name) + + class Section: + + def __init__(self): + self.index = 0 + self.is_64 = False + self.sectname = None + self.segname = None + self.addr = 0 + self.size = 0 + self.offset = 0 + self.align = 0 + self.reloff = 0 + self.nreloc = 0 + self.flags = 0 + self.reserved1 = 0 + self.reserved2 = 0 + self.reserved3 = 0 + + def unpack(self, is_64, data): + self.is_64 = is_64 + self.sectname = data.get_fixed_length_c_string (16, '', True) + self.segname = data.get_fixed_length_c_string (16, '', True) + if self.is_64: + self.addr, self.size = data.get_n_uint64(2) + self.offset, self.align, self.reloff, self.nreloc, self.flags, self.reserved1, self.reserved2, self.reserved3 = data.get_n_uint32(8) + else: + self.addr, self.size = data.get_n_uint32(2) + self.offset, self.align, self.reloff, self.nreloc, self.flags, self.reserved1, self.reserved2 = data.get_n_uint32(7) + + def dump_header(self): + if self.is_64: + print "INDEX ADDRESS SIZE OFFSET ALIGN RELOFF NRELOC FLAGS RESERVED1 RESERVED2 RESERVED3 NAME"; + print "===== ------------------ ------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------------------"; + else: + print "INDEX ADDRESS SIZE OFFSET ALIGN RELOFF NRELOC FLAGS RESERVED1 RESERVED2 NAME"; + print "===== ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------------------"; + + def __str__(self): + if self.is_64: + return "[%3u] %#16.16x %#16.16x %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %s.%s" % (self.index, self.addr, self.size, self.offset, self.align, self.reloff, self.nreloc, self.flags, self.reserved1, self.reserved2, self.reserved3, self.segname, self.sectname) + else: + return "[%3u] %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %s.%s" % (self.index, self.addr, self.size, self.offset, self.align, self.reloff, self.nreloc, self.flags, self.reserved1, self.reserved2, self.segname, self.sectname) + + def get_contents(self, mach_file): + '''Get the section contents as a python string''' + if self.size > 0 and mach_file.get_segment(self.segname).filesize > 0: + data = mach_file.get_data() + if data: + section_data_offset = mach_file.file_off + self.offset + #print '%s.%s is at offset 0x%x with size 0x%x' % (self.segname, self.sectname, section_data_offset, self.size) + data.push_offset_and_seek (section_data_offset) + bytes = data.read_size(self.size) + data.pop_offset_and_seek() + return bytes + return None + + class DylibLoadCommand(LoadCommand): + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.name = None + self.timestamp = 0 + self.current_version = 0 + self.compatibility_version = 0 + + def unpack(self, mach_file, data): + byte_order_char = mach_file.magic.get_byte_order() + name_offset, self.timestamp, self.current_version, self.compatibility_version = data.get_n_uint32(4) + data.seek(self.file_off + name_offset, 0) + self.name = data.get_fixed_length_c_string(self.length - 24) + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + s += "%#8.8x %#8.8x %#8.8x " % (self.timestamp, self.current_version, self.compatibility_version) + s += self.name + return s + + class LoadDYLDLoadCommand(LoadCommand): + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.name = None + + def unpack(self, mach_file, data): + data.get_uint32() + self.name = data.get_fixed_length_c_string(self.length - 12) + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + s += "%s" % self.name + return s + + class UnixThreadLoadCommand(LoadCommand): + class ThreadState: + def __init__(self): + self.flavor = 0 + self.count = 0 + self.register_values = list() + + def unpack(self, data): + self.flavor, self.count = data.get_n_uint32(2) + self.register_values = data.get_n_uint32(self.count) + + def __str__(self): + s = "flavor = %u, count = %u, regs =" % (self.flavor, self.count) + i = 0 + for register_value in self.register_values: + if i % 8 == 0: + s += "\n " + s += " %#8.8x" % register_value + i += 1 + return s + + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.reg_sets = list() + + def unpack(self, mach_file, data): + reg_set = Mach.UnixThreadLoadCommand.ThreadState() + reg_set.unpack (data) + self.reg_sets.append(reg_set) + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + for reg_set in self.reg_sets: + s += "%s" % reg_set + return s + + class DYLDInfoOnlyLoadCommand(LoadCommand): + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.rebase_off = 0 + self.rebase_size = 0 + self.bind_off = 0 + self.bind_size = 0 + self.weak_bind_off = 0 + self.weak_bind_size = 0 + self.lazy_bind_off = 0 + self.lazy_bind_size = 0 + self.export_off = 0 + self.export_size = 0 + + def unpack(self, mach_file, data): + byte_order_char = mach_file.magic.get_byte_order() + self.rebase_off, self.rebase_size, self.bind_off, self.bind_size, self.weak_bind_off, self.weak_bind_size, self.lazy_bind_off, self.lazy_bind_size, self.export_off, self.export_size = data.get_n_uint32(10) + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + s += "rebase_off = %#8.8x, rebase_size = %u, " % (self.rebase_off, self.rebase_size) + s += "bind_off = %#8.8x, bind_size = %u, " % (self.bind_off, self.bind_size) + s += "weak_bind_off = %#8.8x, weak_bind_size = %u, " % (self.weak_bind_off, self.weak_bind_size) + s += "lazy_bind_off = %#8.8x, lazy_bind_size = %u, " % (self.lazy_bind_off, self.lazy_bind_size) + s += "export_off = %#8.8x, export_size = %u, " % (self.export_off, self.export_size) + return s + + class DYLDSymtabLoadCommand(LoadCommand): + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.ilocalsym = 0 + self.nlocalsym = 0 + self.iextdefsym = 0 + self.nextdefsym = 0 + self.iundefsym = 0 + self.nundefsym = 0 + self.tocoff = 0 + self.ntoc = 0 + self.modtaboff = 0 + self.nmodtab = 0 + self.extrefsymoff = 0 + self.nextrefsyms = 0 + self.indirectsymoff = 0 + self.nindirectsyms = 0 + self.extreloff = 0 + self.nextrel = 0 + self.locreloff = 0 + self.nlocrel = 0 + + def unpack(self, mach_file, data): + byte_order_char = mach_file.magic.get_byte_order() + self.ilocalsym, self.nlocalsym, self.iextdefsym, self.nextdefsym, self.iundefsym, self.nundefsym, self.tocoff, self.ntoc, self.modtaboff, self.nmodtab, self.extrefsymoff, self.nextrefsyms, self.indirectsymoff, self.nindirectsyms, self.extreloff, self.nextrel, self.locreloff, self.nlocrel = data.get_n_uint32(18) + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + # s += "ilocalsym = %u, nlocalsym = %u, " % (self.ilocalsym, self.nlocalsym) + # s += "iextdefsym = %u, nextdefsym = %u, " % (self.iextdefsym, self.nextdefsym) + # s += "iundefsym %u, nundefsym = %u, " % (self.iundefsym, self.nundefsym) + # s += "tocoff = %#8.8x, ntoc = %u, " % (self.tocoff, self.ntoc) + # s += "modtaboff = %#8.8x, nmodtab = %u, " % (self.modtaboff, self.nmodtab) + # s += "extrefsymoff = %#8.8x, nextrefsyms = %u, " % (self.extrefsymoff, self.nextrefsyms) + # s += "indirectsymoff = %#8.8x, nindirectsyms = %u, " % (self.indirectsymoff, self.nindirectsyms) + # s += "extreloff = %#8.8x, nextrel = %u, " % (self.extreloff, self.nextrel) + # s += "locreloff = %#8.8x, nlocrel = %u" % (self.locreloff, self.nlocrel) + s += "ilocalsym = %-10u, nlocalsym = %u\n" % (self.ilocalsym, self.nlocalsym) + s += " iextdefsym = %-10u, nextdefsym = %u\n" % (self.iextdefsym, self.nextdefsym) + s += " iundefsym = %-10u, nundefsym = %u\n" % (self.iundefsym, self.nundefsym) + s += " tocoff = %#8.8x, ntoc = %u\n" % (self.tocoff, self.ntoc) + s += " modtaboff = %#8.8x, nmodtab = %u\n" % (self.modtaboff, self.nmodtab) + s += " extrefsymoff = %#8.8x, nextrefsyms = %u\n" % (self.extrefsymoff, self.nextrefsyms) + s += " indirectsymoff = %#8.8x, nindirectsyms = %u\n" % (self.indirectsymoff, self.nindirectsyms) + s += " extreloff = %#8.8x, nextrel = %u\n" % (self.extreloff, self.nextrel) + s += " locreloff = %#8.8x, nlocrel = %u" % (self.locreloff, self.nlocrel) + return s + + class SymtabLoadCommand(LoadCommand): + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.symoff = 0 + self.nsyms = 0 + self.stroff = 0 + self.strsize = 0 + + def unpack(self, mach_file, data): + byte_order_char = mach_file.magic.get_byte_order() + self.symoff, self.nsyms, self.stroff, self.strsize = data.get_n_uint32(4) + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + s += "symoff = %#8.8x, nsyms = %u, stroff = %#8.8x, strsize = %u" % (self.symoff, self.nsyms, self.stroff, self.strsize) + return s + + + class UUIDLoadCommand(LoadCommand): + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.uuid = None + + def unpack(self, mach_file, data): + uuid_data = data.get_n_uint8(16) + uuid_str = '' + for byte in uuid_data: + uuid_str += '%2.2x' % byte + self.uuid = uuid.UUID(uuid_str) + mach_file.uuid = self.uuid + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + s += self.uuid.__str__() + return s + + class DataBlobLoadCommand(LoadCommand): + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.dataoff = 0 + self.datasize = 0 + + def unpack(self, mach_file, data): + byte_order_char = mach_file.magic.get_byte_order() + self.dataoff, self.datasize = data.get_n_uint32(2) + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + s += "dataoff = %#8.8x, datasize = %u" % (self.dataoff, self.datasize) + return s + + class EncryptionInfoLoadCommand(LoadCommand): + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.cryptoff = 0 + self.cryptsize = 0 + self.cryptid = 0 + + def unpack(self, mach_file, data): + byte_order_char = mach_file.magic.get_byte_order() + self.cryptoff, self.cryptsize, self.cryptid = data.get_n_uint32(3) + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + s += "file-range = [%#8.8x - %#8.8x), cryptsize = %u, cryptid = %u" % (self.cryptoff, self.cryptoff + self.cryptsize, self.cryptsize, self.cryptid) + return s + + class SegmentLoadCommand(LoadCommand): + + def __init__(self, lc): + Mach.LoadCommand.__init__(self, lc.command, lc.length, lc.file_off) + self.segname = None + self.vmaddr = 0 + self.vmsize = 0 + self.fileoff = 0 + self.filesize = 0 + self.maxprot = 0 + self.initprot = 0 + self.nsects = 0 + self.flags = 0 + + def unpack(self, mach_file, data): + is_64 = self.command.get_enum_value() == LC_SEGMENT_64; + self.segname = data.get_fixed_length_c_string (16, '', True) + if is_64: + self.vmaddr, self.vmsize, self.fileoff, self.filesize = data.get_n_uint64(4) + else: + self.vmaddr, self.vmsize, self.fileoff, self.filesize = data.get_n_uint32(4) + self.maxprot, self.initprot, self.nsects, self.flags = data.get_n_uint32(4) + mach_file.segments.append(self) + for i in range(self.nsects): + section = Mach.Section() + section.unpack(is_64, data) + section.index = len (mach_file.sections) + mach_file.sections.append(section) + + + def __str__(self): + s = Mach.LoadCommand.__str__(self); + if self.command.get_enum_value() == LC_SEGMENT: + s += "%#8.8x %#8.8x %#8.8x %#8.8x " % (self.vmaddr, self.vmsize, self.fileoff, self.filesize) + else: + s += "%#16.16x %#16.16x %#16.16x %#16.16x " % (self.vmaddr, self.vmsize, self.fileoff, self.filesize) + s += "%s %s %3u %#8.8x" % (vm_prot_names[self.maxprot], vm_prot_names[self.initprot], self.nsects, self.flags) + s += ' ' + self.segname + return s + + class NList: + class Type: + class Stab(dict_utils.Enum): + enum = { + 'N_GSYM' : N_GSYM , + 'N_FNAME' : N_FNAME , + 'N_FUN' : N_FUN , + 'N_STSYM' : N_STSYM , + 'N_LCSYM' : N_LCSYM , + 'N_BNSYM' : N_BNSYM , + 'N_OPT' : N_OPT , + 'N_RSYM' : N_RSYM , + 'N_SLINE' : N_SLINE , + 'N_ENSYM' : N_ENSYM , + 'N_SSYM' : N_SSYM , + 'N_SO' : N_SO , + 'N_OSO' : N_OSO , + 'N_LSYM' : N_LSYM , + 'N_BINCL' : N_BINCL , + 'N_SOL' : N_SOL , + 'N_PARAMS' : N_PARAMS , + 'N_VERSION' : N_VERSION , + 'N_OLEVEL' : N_OLEVEL , + 'N_PSYM' : N_PSYM , + 'N_EINCL' : N_EINCL , + 'N_ENTRY' : N_ENTRY , + 'N_LBRAC' : N_LBRAC , + 'N_EXCL' : N_EXCL , + 'N_RBRAC' : N_RBRAC , + 'N_BCOMM' : N_BCOMM , + 'N_ECOMM' : N_ECOMM , + 'N_ECOML' : N_ECOML , + 'N_LENG' : N_LENG + } + + def __init__(self, magic = 0): + dict_utils.Enum.__init__(self, magic, self.enum) + + def __init__(self, t = 0): + self.value = t + + def __str__(self): + n_type = self.value + if n_type & N_STAB: + stab = Mach.NList.Type.Stab(self.value) + return '%s' % stab + else: + type = self.value & N_TYPE + type_str = '' + if type == N_UNDF: + type_str = 'N_UNDF' + elif type == N_ABS: + type_str = 'N_ABS ' + elif type == N_SECT: + type_str = 'N_SECT' + elif type == N_PBUD: + type_str = 'N_PBUD' + elif type == N_INDR: + type_str = 'N_INDR' + else: + type_str = "??? (%#2.2x)" % type + if n_type & N_PEXT: + type_str += ' | PEXT' + if n_type & N_EXT: + type_str += ' | EXT ' + return type_str + + + def __init__(self): + self.index = 0 + self.name_offset = 0 + self.name = 0 + self.type = Mach.NList.Type() + self.sect_idx = 0 + self.desc = 0 + self.value = 0 + + def unpack(self, mach_file, data, symtab_lc): + self.index = len(mach_file.symbols) + self.name_offset = data.get_uint32() + self.type.value, self.sect_idx = data.get_n_uint8(2) + self.desc = data.get_uint16() + if mach_file.is_64_bit(): + self.value = data.get_uint64() + else: + self.value = data.get_uint32() + data.push_offset_and_seek (mach_file.file_off + symtab_lc.stroff + self.name_offset) + #print "get string for symbol[%u]" % self.index + self.name = data.get_c_string() + data.pop_offset_and_seek() + + def __str__(self): + name_display = '' + if len(self.name): + name_display = ' "%s"' % self.name + return '%#8.8x %#2.2x (%-20s) %#2.2x %#4.4x %16.16x%s' % (self.name_offset, self.type.value, self.type, self.sect_idx, self.desc, self.value, name_display) + + + class Interactive(cmd.Cmd): + '''Interactive command interpreter to mach-o files.''' + + def __init__(self, mach, options): + cmd.Cmd.__init__(self) + self.intro = 'Interactive mach-o command interpreter' + self.prompt = 'mach-o: %s %% ' % mach.path + self.mach = mach + self.options = options + + def default(self, line): + '''Catch all for unknown command, which will exit the interpreter.''' + print "uknown command: %s" % line + return True + + def do_q(self, line): + '''Quit command''' + return True + + def do_quit(self, line): + '''Quit command''' + return True + + def do_header(self, line): + '''Dump mach-o file headers''' + self.mach.dump_header(True, self.options) + return False + + def do_load(self, line): + '''Dump all mach-o load commands''' + self.mach.dump_load_commands(True, self.options) + return False + + def do_sections(self, line): + '''Dump all mach-o sections''' + self.mach.dump_sections(True, self.options) + return False + + def do_symtab(self, line): + '''Dump all mach-o symbols in the symbol table''' + self.mach.dump_symtab(True, self.options) + return False + +if __name__ == '__main__': + parser = optparse.OptionParser(description='A script that parses skinny and universal mach-o files.') + parser.add_option('--arch', '-a', type='string', metavar='arch', dest='archs', action='append', help='specify one or more architectures by name') + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + parser.add_option('-H', '--header', action='store_true', dest='dump_header', help='dump the mach-o file header', default=False) + parser.add_option('-l', '--load-commands', action='store_true', dest='dump_load_commands', help='dump the mach-o load commands', default=False) + parser.add_option('-s', '--symtab', action='store_true', dest='dump_symtab', help='dump the mach-o symbol table', default=False) + parser.add_option('-S', '--sections', action='store_true', dest='dump_sections', help='dump the mach-o sections', default=False) + parser.add_option('--section', type='string', metavar='sectname', dest='section_names', action='append', help='Specify one or more section names to dump', default=[]) + parser.add_option('-o', '--out', type='string', dest='outfile', help='Used in conjunction with the --section=NAME option to save a single section\'s data to disk.', default=False) + parser.add_option('-i', '--interactive', action='store_true', dest='interactive', help='enable interactive mode', default=False) + parser.add_option('-m', '--mangled', action='store_true', dest='find_mangled', help='dump all mangled names in a mach file', default=False) + parser.add_option('-c', '--compare', action='store_true', dest='compare', help='compare two mach files', default=False) + parser.add_option('-M', '--extract-modules', action='store_true', dest='extract_modules', help='Extract modules from file', default=False) + parser.add_option('-C', '--count', type='int', dest='max_count', help='Sets the max byte count when dumping section data', default=-1) + + (options, mach_files) = parser.parse_args() + if options.extract_modules: + if options.section_names: + print "error: can't use --section option with the --extract-modules option" + exit(1) + if not options.outfile: + print "error: the --output=FILE option must be specified with the --extract-modules option" + exit(1) + options.section_names.append("__apple_ast") + if options.compare: + if len(mach_files) == 2: + mach_a = Mach() + mach_b = Mach() + mach_a.parse(mach_files[0]) + mach_b.parse(mach_files[1]) + mach_a.compare(mach_b) + else: + print 'error: --compare takes two mach files as arguments' + else: + if not (options.dump_header or options.dump_load_commands or options.dump_symtab or options.dump_sections or options.find_mangled or options.section_names): + options.dump_header = True + options.dump_load_commands = True + if options.verbose: + print 'options', options + print 'mach_files', mach_files + for path in mach_files: + mach = Mach() + mach.parse(path) + if options.interactive: + interpreter = Mach.Interactive(mach, options) + interpreter.cmdloop() + else: + mach.dump(options) + diff --git a/examples/python/memory.py b/examples/python/memory.py new file mode 100755 index 000000000000..ae78e24e2e29 --- /dev/null +++ b/examples/python/memory.py @@ -0,0 +1,181 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# Be sure to add the python path that points to the LLDB shared library. +# +# # To use this in the embedded python interpreter using "lldb" just +# import it with the full path using the "command script import" +# command +# (lldb) command script import /path/to/cmdtemplate.py +#---------------------------------------------------------------------- + +import commands +import platform +import os +import re +import sys + +try: + # Just try for LLDB in case PYTHONPATH is already correctly setup + import lldb +except ImportError: + lldb_python_dirs = list() + # lldb is not in the PYTHONPATH, try some defaults for the current platform + platform_system = platform.system() + if platform_system == 'Darwin': + # On Darwin, try the currently selected Xcode directory + xcode_dir = commands.getoutput("xcode-select --print-path") + if xcode_dir: + lldb_python_dirs.append(os.path.realpath(xcode_dir + '/../SharedFrameworks/LLDB.framework/Resources/Python')) + lldb_python_dirs.append(xcode_dir + '/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + lldb_python_dirs.append('/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + success = False + for lldb_python_dir in lldb_python_dirs: + if os.path.exists(lldb_python_dir): + if not (sys.path.__contains__(lldb_python_dir)): + sys.path.append(lldb_python_dir) + try: + import lldb + except ImportError: + pass + else: + print 'imported lldb from: "%s"' % (lldb_python_dir) + success = True + break + if not success: + print "error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly" + sys.exit(1) + +import commands +import optparse +import shlex +import string +import struct +import time + +def append_data_callback(option, opt_str, value, parser): + if opt_str == "--uint8": + int8 = int(value, 0) + parser.values.data += struct.pack('1B',int8) + if opt_str == "--uint16": + int16 = int(value, 0) + parser.values.data += struct.pack('1H',int16) + if opt_str == "--uint32": + int32 = int(value, 0) + parser.values.data += struct.pack('1I',int32) + if opt_str == "--uint64": + int64 = int(value, 0) + parser.values.data += struct.pack('1Q',int64) + if opt_str == "--int8": + int8 = int(value, 0) + parser.values.data += struct.pack('1b',int8) + if opt_str == "--int16": + int16 = int(value, 0) + parser.values.data += struct.pack('1h',int16) + if opt_str == "--int32": + int32 = int(value, 0) + parser.values.data += struct.pack('1i',int32) + if opt_str == "--int64": + int64 = int(value, 0) + parser.values.data += struct.pack('1q',int64) + +def create_memfind_options(): + usage = "usage: %prog [options] STARTADDR [ENDADDR]" + description='''This command can find data in a specified address range. +Options are used to specify the data that is to be looked for and the options +can be specified multiple times to look for longer streams of data. +''' + parser = optparse.OptionParser(description=description, prog='memfind',usage=usage) + parser.add_option('-s', '--size', type='int', metavar='BYTESIZE', dest='size', help='Specify the byte size to search.', default=0) + parser.add_option('--int8', action="callback", callback=append_data_callback, type='string', metavar='INT', dest='data', help='Specify a 8 bit signed integer value to search for in memory.', default='') + parser.add_option('--int16', action="callback", callback=append_data_callback, type='string', metavar='INT', dest='data', help='Specify a 16 bit signed integer value to search for in memory.', default='') + parser.add_option('--int32', action="callback", callback=append_data_callback, type='string', metavar='INT', dest='data', help='Specify a 32 bit signed integer value to search for in memory.', default='') + parser.add_option('--int64', action="callback", callback=append_data_callback, type='string', metavar='INT', dest='data', help='Specify a 64 bit signed integer value to search for in memory.', default='') + parser.add_option('--uint8', action="callback", callback=append_data_callback, type='string', metavar='INT', dest='data', help='Specify a 8 bit unsigned integer value to search for in memory.', default='') + parser.add_option('--uint16', action="callback", callback=append_data_callback, type='string', metavar='INT', dest='data', help='Specify a 16 bit unsigned integer value to search for in memory.', default='') + parser.add_option('--uint32', action="callback", callback=append_data_callback, type='string', metavar='INT', dest='data', help='Specify a 32 bit unsigned integer value to search for in memory.', default='') + parser.add_option('--uint64', action="callback", callback=append_data_callback, type='string', metavar='INT', dest='data', help='Specify a 64 bit unsigned integer value to search for in memory.', default='') + return parser + +def memfind_command (debugger, command, result, dict): + # Use the Shell Lexer to properly parse up command options just like a + # shell would + command_args = shlex.split(command) + parser = create_memfind_options() + (options, args) = parser.parse_args(command_args) + # try: + # (options, args) = parser.parse_args(command_args) + # except: + # # if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit + # # (courtesy of OptParse dealing with argument errors by throwing SystemExit) + # result.SetStatus (lldb.eReturnStatusFailed) + # print >>result, "error: option parsing failed" # returning a string is the same as returning an error whose description is the string + # return + memfind (debugger.GetSelectedTarget(), options, args, result) + +def print_error(str, show_usage, result): + print >>result, str + if show_usage: + print >>result, create_memfind_options().format_help() + +def memfind (target, options, args, result): + num_args = len(args) + start_addr = 0 + if num_args == 1: + if options.size > 0: + print_error ("error: --size must be specified if there is no ENDADDR argument", True, result) + return + start_addr = int(args[0], 0) + elif num_args == 2: + if options.size != 0: + print_error ("error: --size can't be specified with an ENDADDR argument", True, result) + return + start_addr = int(args[0], 0) + end_addr = int(args[1], 0) + if start_addr >= end_addr: + print_error ("error: inavlid memory range [%#x - %#x)" % (start_addr, end_addr), True, result) + return + options.size = end_addr - start_addr + else: + print_error ("error: memfind takes 1 or 2 arguments", True, result) + return + + if not options.data: + print >>result, 'error: no data specified to search for' + return + + if not target: + print >>result, 'error: invalid target' + return + process = target.process + if not process: + print >>result, 'error: invalid process' + return + + error = lldb.SBError() + bytes = process.ReadMemory (start_addr, options.size, error) + if error.Success(): + num_matches = 0 + print >>result, "Searching memory range [%#x - %#x) for" % (start_addr, end_addr), + for byte in options.data: + print >>result, '%2.2x' % ord(byte), + print >>result + + match_index = string.find(bytes, options.data) + while match_index != -1: + num_matches = num_matches + 1 + print >>result, '%#x: %#x + %u' % (start_addr + match_index, start_addr, match_index) + match_index = string.find(bytes, options.data, match_index + 1) + + if num_matches == 0: + print >>result, "error: no matches found" + else: + print >>result, 'error: %s' % (error.GetCString()) + + +if __name__ == '__main__': + print 'error: this script is designed to be used within the embedded script interpreter in LLDB' +elif getattr(lldb, 'debugger', None): + memfind_command.__doc__ = create_memfind_options().format_help() + lldb.debugger.HandleCommand('command script add -f memory.memfind_command memfind') + print '"memfind" command installed, use the "--help" option for detailed help' diff --git a/examples/python/operating_system.py b/examples/python/operating_system.py new file mode 100644 index 000000000000..49cd5ff34398 --- /dev/null +++ b/examples/python/operating_system.py @@ -0,0 +1,104 @@ +#!/usr/bin/python + +import lldb +import struct + +class OperatingSystemPlugIn(object): + """Class that provides data for an instance of a LLDB 'OperatingSystemPython' plug-in class""" + + def __init__(self, process): + '''Initialization needs a valid.SBProcess object. + + This plug-in will get created after a live process is valid and has stopped for the + first time.''' + self.process = None + self.registers = None + self.threads = None + if type(process) is lldb.SBProcess and process.IsValid(): + self.process = process + self.threads = None # Will be an dictionary containing info for each thread + + def get_target(self): + # NOTE: Don't use "lldb.target" when trying to get your target as the "lldb.target" + # tracks the current target in the LLDB command interpreter which isn't the + # correct thing to use for this plug-in. + return self.process.target + + def create_thread(self, tid, context): + if tid == 0x444444444: + thread_info = { 'tid' : tid, 'name' : 'four' , 'queue' : 'queue4', 'state' : 'stopped', 'stop_reason' : 'none' } + self.threads.append(thread_info) + return thread_info + return None + + def get_thread_info(self): + if not self.threads: + # The sample dictionary below shows the values that can be returned for a thread + # tid => thread ID (mandatory) + # name => thread name (optional key/value pair) + # queue => thread dispatch queue name (optional key/value pair) + # state => thred state (mandatory, set to 'stopped' for now) + # stop_reason => thread stop reason. (mandatory, usually set to 'none') + # Possible values include: + # 'breakpoint' if the thread is stopped at a breakpoint + # 'none' thread is just stopped because the process is stopped + # 'trace' the thread just single stepped + # The usual value for this while threads are in memory is 'none' + # register_data_addr => the address of the register data in memory (optional key/value pair) + # Specifying this key/value pair for a thread will avoid a call to get_register_data() + # and can be used when your registers are in a thread context structure that is contiguous + # in memory. Don't specify this if your register layout in memory doesn't match the layout + # described by the dictionary returned from a call to the get_register_info() method. + self.threads = [ + { 'tid' : 0x111111111, 'name' : 'one' , 'queue' : 'queue1', 'state' : 'stopped', 'stop_reason' : 'breakpoint'}, + { 'tid' : 0x222222222, 'name' : 'two' , 'queue' : 'queue2', 'state' : 'stopped', 'stop_reason' : 'none' }, + { 'tid' : 0x333333333, 'name' : 'three', 'queue' : 'queue3', 'state' : 'stopped', 'stop_reason' : 'trace' , 'register_data_addr' : 0x100000000 } + ] + return self.threads + + def get_register_info(self): + if self.registers == None: + self.registers = dict() + triple = self.process.target.triple + if triple: + arch = triple.split('-')[0] + if arch == 'x86_64': + self.registers['sets'] = ['GPR', 'FPU', 'EXC'] + self.registers['registers'] = [ + { 'name':'rax' , 'bitsize' : 64, 'offset' : 0, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 0, 'dwarf' : 0}, + { 'name':'rbx' , 'bitsize' : 64, 'offset' : 8, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 3, 'dwarf' : 3}, + { 'name':'rcx' , 'bitsize' : 64, 'offset' : 16, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 2, 'dwarf' : 2, 'generic':'arg4', 'alt-name':'arg4', }, + { 'name':'rdx' , 'bitsize' : 64, 'offset' : 24, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 1, 'dwarf' : 1, 'generic':'arg3', 'alt-name':'arg3', }, + { 'name':'rdi' , 'bitsize' : 64, 'offset' : 32, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 5, 'dwarf' : 5, 'generic':'arg1', 'alt-name':'arg1', }, + { 'name':'rsi' , 'bitsize' : 64, 'offset' : 40, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 4, 'dwarf' : 4, 'generic':'arg2', 'alt-name':'arg2', }, + { 'name':'rbp' , 'bitsize' : 64, 'offset' : 48, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 6, 'dwarf' : 6, 'generic':'fp' , 'alt-name':'fp', }, + { 'name':'rsp' , 'bitsize' : 64, 'offset' : 56, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 7, 'dwarf' : 7, 'generic':'sp' , 'alt-name':'sp', }, + { 'name':'r8' , 'bitsize' : 64, 'offset' : 64, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 8, 'dwarf' : 8, 'generic':'arg5', 'alt-name':'arg5', }, + { 'name':'r9' , 'bitsize' : 64, 'offset' : 72, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 9, 'dwarf' : 9, 'generic':'arg6', 'alt-name':'arg6', }, + { 'name':'r10' , 'bitsize' : 64, 'offset' : 80, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 10, 'dwarf' : 10}, + { 'name':'r11' , 'bitsize' : 64, 'offset' : 88, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 11, 'dwarf' : 11}, + { 'name':'r12' , 'bitsize' : 64, 'offset' : 96, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 12, 'dwarf' : 12}, + { 'name':'r13' , 'bitsize' : 64, 'offset' : 104, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 13, 'dwarf' : 13}, + { 'name':'r14' , 'bitsize' : 64, 'offset' : 112, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 14, 'dwarf' : 14}, + { 'name':'r15' , 'bitsize' : 64, 'offset' : 120, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 15, 'dwarf' : 15}, + { 'name':'rip' , 'bitsize' : 64, 'offset' : 128, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 16, 'dwarf' : 16, 'generic':'pc', 'alt-name':'pc' }, + { 'name':'rflags' , 'bitsize' : 64, 'offset' : 136, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'generic':'flags', 'alt-name':'flags' }, + { 'name':'cs' , 'bitsize' : 64, 'offset' : 144, 'encoding':'uint' , 'format':'hex' , 'set': 0 }, + { 'name':'fs' , 'bitsize' : 64, 'offset' : 152, 'encoding':'uint' , 'format':'hex' , 'set': 0 }, + { 'name':'gs' , 'bitsize' : 64, 'offset' : 160, 'encoding':'uint' , 'format':'hex' , 'set': 0 }, + ] + return self.registers + + def get_register_data(self, tid): + if tid == 0x111111111: + return struct.pack('21Q',1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21); + elif tid == 0x222222222: + return struct.pack('21Q',11,12,13,14,15,16,17,18,19,110,111,112,113,114,115,116,117,118,119,120,121); + elif tid == 0x333333333: + return struct.pack('21Q',21,22,23,24,25,26,27,28,29,210,211,212,213,214,215,216,217,218,219,220,221); + elif tid == 0x444444444: + return struct.pack('21Q',31,32,33,34,35,36,37,38,39,310,311,312,313,314,315,316,317,318,319,320,321); + else: + return struct.pack('21Q',41,42,43,44,45,46,47,48,49,410,411,412,413,414,415,416,417,418,419,420,421); + return None + diff --git a/examples/python/performance.py b/examples/python/performance.py new file mode 100755 index 000000000000..a225d7b731e0 --- /dev/null +++ b/examples/python/performance.py @@ -0,0 +1,335 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# Be sure to add the python path that points to the LLDB shared library. +# On MacOSX csh, tcsh: +# setenv PYTHONPATH /Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python +# On MacOSX sh, bash: +# export PYTHONPATH=/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python +#---------------------------------------------------------------------- + +import commands +import optparse +import os +import platform +import re +import resource +import sys +import time +import types + +#---------------------------------------------------------------------- +# Code that auto imports LLDB +#---------------------------------------------------------------------- +try: + # Just try for LLDB in case PYTHONPATH is already correctly setup + import lldb +except ImportError: + lldb_python_dirs = list() + # lldb is not in the PYTHONPATH, try some defaults for the current platform + platform_system = platform.system() + if platform_system == 'Darwin': + # On Darwin, try the currently selected Xcode directory + xcode_dir = commands.getoutput("xcode-select --print-path") + if xcode_dir: + lldb_python_dirs.append(os.path.realpath(xcode_dir + '/../SharedFrameworks/LLDB.framework/Resources/Python')) + lldb_python_dirs.append(xcode_dir + '/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + lldb_python_dirs.append('/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + success = False + for lldb_python_dir in lldb_python_dirs: + if os.path.exists(lldb_python_dir): + if not (sys.path.__contains__(lldb_python_dir)): + sys.path.append(lldb_python_dir) + try: + import lldb + except ImportError: + pass + else: + print 'imported lldb from: "%s"' % (lldb_python_dir) + success = True + break + if not success: + print "error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly" + sys.exit(1) + + +class Timer: + def __enter__(self): + self.start = time.clock() + return self + + def __exit__(self, *args): + self.end = time.clock() + self.interval = self.end - self.start + +class Action(object): + """Class that encapsulates actions to take when a thread stops for a reason.""" + def __init__(self, callback = None, callback_owner = None): + self.callback = callback + self.callback_owner = callback_owner + def ThreadStopped (self, thread): + assert False, "performance.Action.ThreadStopped(self, thread) must be overridden in a subclass" + +class PlanCompleteAction (Action): + def __init__(self, callback = None, callback_owner = None): + Action.__init__(self, callback, callback_owner) + def ThreadStopped (self, thread): + if thread.GetStopReason() == lldb.eStopReasonPlanComplete: + if self.callback: + if self.callback_owner: + self.callback (self.callback_owner, thread) + else: + self.callback (thread) + return True + return False + + +class BreakpointAction (Action): + def __init__(self, callback = None, callback_owner = None, name = None, module = None, file = None, line = None, breakpoint = None): + Action.__init__(self, callback, callback_owner) + self.modules = lldb.SBFileSpecList() + self.files = lldb.SBFileSpecList() + self.breakpoints = list() + # "module" can be a list or a string + if breakpoint: + self.breakpoints.append(breakpoint) + else: + if module: + if isinstance(module, types.ListType): + for module_path in module: + self.modules.Append(lldb.SBFileSpec(module_path, False)) + elif isinstance(module, types.StringTypes): + self.modules.Append(lldb.SBFileSpec(module, False)) + if name: + # "file" can be a list or a string + if file: + if isinstance(file, types.ListType): + self.files = lldb.SBFileSpecList() + for f in file: + self.files.Append(lldb.SBFileSpec(f, False)) + elif isinstance(file, types.StringTypes): + self.files.Append(lldb.SBFileSpec(file, False)) + self.breakpoints.append (self.target.BreakpointCreateByName(name, self.modules, self.files)) + elif file and line: + self.breakpoints.append (self.target.BreakpointCreateByLocation(file, line)) + def ThreadStopped (self, thread): + if thread.GetStopReason() == lldb.eStopReasonBreakpoint: + for bp in self.breakpoints: + if bp.GetID() == thread.GetStopReasonDataAtIndex(0): + if self.callback: + if self.callback_owner: + self.callback (self.callback_owner, thread) + else: + self.callback (thread) + return True + return False +class TestCase: + """Class that aids in running performance tests.""" + def __init__(self): + self.verbose = False + self.debugger = lldb.SBDebugger.Create() + self.target = None + self.process = None + self.thread = None + self.launch_info = None + self.done = False + self.listener = self.debugger.GetListener() + self.user_actions = list() + self.builtin_actions = list() + self.bp_id_to_dict = dict() + + def Setup(self, args): + self.launch_info = lldb.SBLaunchInfo(args) + + def Run (self, args): + assert False, "performance.TestCase.Run(self, args) must be subclassed" + + def Launch(self): + if self.target: + error = lldb.SBError() + self.process = self.target.Launch (self.launch_info, error) + if not error.Success(): + print "error: %s" % error.GetCString() + if self.process: + self.process.GetBroadcaster().AddListener(self.listener, lldb.SBProcess.eBroadcastBitStateChanged | lldb.SBProcess.eBroadcastBitInterrupt) + return True + return False + + def WaitForNextProcessEvent (self): + event = None + if self.process: + while event is None: + process_event = lldb.SBEvent() + if self.listener.WaitForEvent (lldb.UINT32_MAX, process_event): + state = lldb.SBProcess.GetStateFromEvent (process_event) + if self.verbose: + print "event = %s" % (lldb.SBDebugger.StateAsCString(state)) + if lldb.SBProcess.GetRestartedFromEvent(process_event): + continue + if state == lldb.eStateInvalid or state == lldb.eStateDetached or state == lldb.eStateCrashed or state == lldb.eStateUnloaded or state == lldb.eStateExited: + event = process_event + self.done = True + elif state == lldb.eStateConnected or state == lldb.eStateAttaching or state == lldb.eStateLaunching or state == lldb.eStateRunning or state == lldb.eStateStepping or state == lldb.eStateSuspended: + continue + elif state == lldb.eStateStopped: + event = process_event + call_test_step = True + fatal = False + selected_thread = False + for thread in self.process: + frame = thread.GetFrameAtIndex(0) + select_thread = False + + stop_reason = thread.GetStopReason() + if self.verbose: + print "tid = %#x pc = %#x " % (thread.GetThreadID(),frame.GetPC()), + if stop_reason == lldb.eStopReasonNone: + if self.verbose: + print "none" + elif stop_reason == lldb.eStopReasonTrace: + select_thread = True + if self.verbose: + print "trace" + elif stop_reason == lldb.eStopReasonPlanComplete: + select_thread = True + if self.verbose: + print "plan complete" + elif stop_reason == lldb.eStopReasonThreadExiting: + if self.verbose: + print "thread exiting" + elif stop_reason == lldb.eStopReasonExec: + if self.verbose: + print "exec" + elif stop_reason == lldb.eStopReasonInvalid: + if self.verbose: + print "invalid" + elif stop_reason == lldb.eStopReasonException: + select_thread = True + if self.verbose: + print "exception" + fatal = True + elif stop_reason == lldb.eStopReasonBreakpoint: + select_thread = True + bp_id = thread.GetStopReasonDataAtIndex(0) + bp_loc_id = thread.GetStopReasonDataAtIndex(1) + if self.verbose: + print "breakpoint id = %d.%d" % (bp_id, bp_loc_id) + elif stop_reason == lldb.eStopReasonWatchpoint: + select_thread = True + if self.verbose: + print "watchpoint id = %d" % (thread.GetStopReasonDataAtIndex(0)) + elif stop_reason == lldb.eStopReasonSignal: + select_thread = True + if self.verbose: + print "signal %d" % (thread.GetStopReasonDataAtIndex(0)) + + if select_thread and not selected_thread: + self.thread = thread + selected_thread = self.process.SetSelectedThread(thread) + + for action in self.user_actions: + action.ThreadStopped (thread) + + + if fatal: + # if self.verbose: + # Xcode.RunCommand(self.debugger,"bt all",true) + sys.exit(1) + return event + +class Measurement: + '''A class that encapsulates a measurement''' + def __init__(self): + object.__init__(self) + def Measure(self): + assert False, "performance.Measurement.Measure() must be subclassed" + +class MemoryMeasurement(Measurement): + '''A class that can measure memory statistics for a process.''' + def __init__(self, pid): + Measurement.__init__(self) + self.pid = pid + self.stats = ["rprvt","rshrd","rsize","vsize","vprvt","kprvt","kshrd","faults","cow","pageins"] + self.command = "top -l 1 -pid %u -stats %s" % (self.pid, ",".join(self.stats)) + self.value = dict() + + def Measure(self): + output = commands.getoutput(self.command).split("\n")[-1] + values = re.split('[-+\s]+', output) + for (idx, stat) in enumerate(values): + multiplier = 1 + if stat: + if stat[-1] == 'K': + multiplier = 1024 + stat = stat[:-1] + elif stat[-1] == 'M': + multiplier = 1024*1024 + stat = stat[:-1] + elif stat[-1] == 'G': + multiplier = 1024*1024*1024 + elif stat[-1] == 'T': + multiplier = 1024*1024*1024*1024 + stat = stat[:-1] + self.value[self.stats[idx]] = int (stat) * multiplier + + def __str__(self): + '''Dump the MemoryMeasurement current value''' + s = '' + for key in self.value.keys(): + if s: + s += "\n" + s += "%8s = %s" % (key, self.value[key]) + return s + + +class TesterTestCase(TestCase): + def __init__(self): + TestCase.__init__(self) + self.verbose = True + self.num_steps = 5 + + def BreakpointHit (self, thread): + bp_id = thread.GetStopReasonDataAtIndex(0) + loc_id = thread.GetStopReasonDataAtIndex(1) + print "Breakpoint %i.%i hit: %s" % (bp_id, loc_id, thread.process.target.FindBreakpointByID(bp_id)) + thread.StepOver() + + def PlanComplete (self, thread): + if self.num_steps > 0: + thread.StepOver() + self.num_steps = self.num_steps - 1 + else: + thread.process.Kill() + + def Run (self, args): + self.Setup(args) + with Timer() as total_time: + self.target = self.debugger.CreateTarget(args[0]) + if self.target: + with Timer() as breakpoint_timer: + bp = self.target.BreakpointCreateByName("main") + print('Breakpoint time = %.03f sec.' % breakpoint_timer.interval) + + self.user_actions.append (BreakpointAction(breakpoint=bp, callback=TesterTestCase.BreakpointHit, callback_owner=self)) + self.user_actions.append (PlanCompleteAction(callback=TesterTestCase.PlanComplete, callback_owner=self)) + + if self.Launch(): + while not self.done: + self.WaitForNextProcessEvent() + else: + print "error: failed to launch process" + else: + print "error: failed to create target with '%s'" % (args[0]) + print('Total time = %.03f sec.' % total_time.interval) + + +if __name__ == '__main__': + lldb.SBDebugger.Initialize() + test = TesterTestCase() + test.Run (sys.argv[1:]) + mem = MemoryMeasurement(os.getpid()) + mem.Measure() + print str(mem) + lldb.SBDebugger.Terminate() + # print "sleeeping for 100 seconds" + # time.sleep(100) diff --git a/examples/python/process_events.py b/examples/python/process_events.py new file mode 100755 index 000000000000..e8ccc5f90230 --- /dev/null +++ b/examples/python/process_events.py @@ -0,0 +1,278 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# Be sure to add the python path that points to the LLDB shared library. +# On MacOSX csh, tcsh: +# setenv PYTHONPATH /Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python +# On MacOSX sh, bash: +# export PYTHONPATH=/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python +#---------------------------------------------------------------------- + +import commands +import optparse +import os +import platform +import sys + +#---------------------------------------------------------------------- +# Code that auto imports LLDB +#---------------------------------------------------------------------- +try: + # Just try for LLDB in case PYTHONPATH is already correctly setup + import lldb +except ImportError: + lldb_python_dirs = list() + # lldb is not in the PYTHONPATH, try some defaults for the current platform + platform_system = platform.system() + if platform_system == 'Darwin': + # On Darwin, try the currently selected Xcode directory + xcode_dir = commands.getoutput("xcode-select --print-path") + if xcode_dir: + lldb_python_dirs.append(os.path.realpath(xcode_dir + '/../SharedFrameworks/LLDB.framework/Resources/Python')) + lldb_python_dirs.append(xcode_dir + '/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + lldb_python_dirs.append('/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + success = False + for lldb_python_dir in lldb_python_dirs: + if os.path.exists(lldb_python_dir): + if not (sys.path.__contains__(lldb_python_dir)): + sys.path.append(lldb_python_dir) + try: + import lldb + except ImportError: + pass + else: + print 'imported lldb from: "%s"' % (lldb_python_dir) + success = True + break + if not success: + print "error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly" + sys.exit(1) + +def print_threads(process, options): + if options.show_threads: + for thread in process: + print '%s %s' % (thread, thread.GetFrameAtIndex(0)) + +def run_commands(command_interpreter, commands): + return_obj = lldb.SBCommandReturnObject() + for command in commands: + command_interpreter.HandleCommand( command, return_obj ) + if return_obj.Succeeded(): + print return_obj.GetOutput() + else: + print return_obj + if options.stop_on_error: + break + +def main(argv): + description='''Debugs a program using the LLDB python API and uses asynchronous broadcast events to watch for process state changes.''' + epilog='''Examples: + +#---------------------------------------------------------------------- +# Run "/bin/ls" with the arguments "-lAF /tmp/", and set a breakpoint +# at "malloc" and backtrace and read all registers each time we stop +#---------------------------------------------------------------------- +% ./process_events.py --breakpoint malloc --stop-command bt --stop-command 'register read' -- /bin/ls -lAF /tmp/ + +''' + optparse.OptionParser.format_epilog = lambda self, formatter: self.epilog + parser = optparse.OptionParser(description=description, prog='process_events',usage='usage: process_events [options] program [arg1 arg2]', epilog=epilog) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help="Enable verbose logging.", default=False) + parser.add_option('-b', '--breakpoint', action='append', type='string', metavar='BPEXPR', dest='breakpoints', help='Breakpoint commands to create after the target has been created, the values will be sent to the "_regexp-break" command which supports breakpoints by name, file:line, and address.') + parser.add_option('-a', '--arch', type='string', dest='arch', help='The architecture to use when creating the debug target.', default=None) + parser.add_option('--platform', type='string', metavar='platform', dest='platform', help='Specify the platform to use when creating the debug target. Valid values include "localhost", "darwin-kernel", "ios-simulator", "remote-freebsd", "remote-macosx", "remote-ios", "remote-linux".', default=None) + parser.add_option('-l', '--launch-command', action='append', type='string', metavar='CMD', dest='launch_commands', help='LLDB command interpreter commands to run once after the process has launched. This option can be specified more than once.', default=[]) + parser.add_option('-s', '--stop-command', action='append', type='string', metavar='CMD', dest='stop_commands', help='LLDB command interpreter commands to run each time the process stops. This option can be specified more than once.', default=[]) + parser.add_option('-c', '--crash-command', action='append', type='string', metavar='CMD', dest='crash_commands', help='LLDB command interpreter commands to run in case the process crashes. This option can be specified more than once.', default=[]) + parser.add_option('-x', '--exit-command', action='append', type='string', metavar='CMD', dest='exit_commands', help='LLDB command interpreter commands to run once after the process has exited. This option can be specified more than once.', default=[]) + parser.add_option('-T', '--no-threads', action='store_false', dest='show_threads', help="Don't show threads when process stops.", default=True) + parser.add_option('--ignore-errors', action='store_false', dest='stop_on_error', help="Don't stop executing LLDB commands if the command returns an error. This applies to all of the LLDB command interpreter commands that get run for launch, stop, crash and exit.", default=True) + parser.add_option('-n', '--run-count', type='int', dest='run_count', metavar='N', help='How many times to run the process in case the process exits.', default=1) + parser.add_option('-t', '--event-timeout', type='int', dest='event_timeout', metavar='SEC', help='Specify the timeout in seconds to wait for process state change events.', default=lldb.UINT32_MAX) + parser.add_option('-e', '--environment', action='append', type='string', metavar='ENV', dest='env_vars', help='Environment variables to set in the inferior process when launching a process.') + parser.add_option('-d', '--working-dir', type='string', metavar='DIR', dest='working_dir', help='The the current working directory when launching a process.', default=None) + parser.add_option('-p', '--attach-pid', type='int', dest='attach_pid', metavar='PID', help='Specify a process to attach to by process ID.', default=-1) + parser.add_option('-P', '--attach-name', type='string', dest='attach_name', metavar='PROCESSNAME', help='Specify a process to attach to by name.', default=None) + parser.add_option('-w', '--attach-wait', action='store_true', dest='attach_wait', help='Wait for the next process to launch when attaching to a process by name.', default=False) + try: + (options, args) = parser.parse_args(argv) + except: + return + + attach_info = None + launch_info = None + exe = None + if args: + exe = args.pop(0) + launch_info = lldb.SBLaunchInfo (args) + if options.env_vars: + launch_info.SetEnvironmentEntries(options.env_vars, True) + if options.working_dir: + launch_info.SetWorkingDirectory(options.working_dir) + elif options.attach_pid != -1: + if options.run_count == 1: + attach_info = lldb.SBAttachInfo (options.attach_pid) + else: + print "error: --run-count can't be used with the --attach-pid option" + sys.exit(1) + elif not options.attach_name is None: + if options.run_count == 1: + attach_info = lldb.SBAttachInfo (options.attach_name, options.attach_wait) + else: + print "error: --run-count can't be used with the --attach-name option" + sys.exit(1) + else: + print 'error: a program path for a program to debug and its arguments are required' + sys.exit(1) + + + + # Create a new debugger instance + debugger = lldb.SBDebugger.Create() + debugger.SetAsync (True) + command_interpreter = debugger.GetCommandInterpreter() + # Create a target from a file and arch + + if exe: + print "Creating a target for '%s'" % exe + error = lldb.SBError() + target = debugger.CreateTarget (exe, options.arch, options.platform, True, error) + + if target: + + # Set any breakpoints that were specified in the args if we are launching. We use the + # command line command to take advantage of the shorthand breakpoint creation + if launch_info and options.breakpoints: + for bp in options.breakpoints: + debugger.HandleCommand( "_regexp-break %s" % (bp)) + run_commands(command_interpreter, ['breakpoint list']) + + for run_idx in range(options.run_count): + # Launch the process. Since we specified synchronous mode, we won't return + # from this function until we hit the breakpoint at main + error = lldb.SBError() + + if launch_info: + if options.run_count == 1: + print 'Launching "%s"...' % (exe) + else: + print 'Launching "%s"... (launch %u of %u)' % (exe, run_idx + 1, options.run_count) + + process = target.Launch (launch_info, error) + else: + if options.attach_pid != -1: + print 'Attaching to process %i...' % (options.attach_pid) + else: + if options.attach_wait: + print 'Waiting for next to process named "%s" to launch...' % (options.attach_name) + else: + print 'Attaching to existing process named "%s"...' % (options.attach_name) + process = target.Attach (attach_info, error) + + # Make sure the launch went ok + if process and process.GetProcessID() != lldb.LLDB_INVALID_PROCESS_ID: + + pid = process.GetProcessID() + print 'Process is %i' % (pid) + if attach_info: + # continue process if we attached as we won't get an initial event + process.Continue() + + listener = debugger.GetListener() + # sign up for process state change events + stop_idx = 0 + done = False + while not done: + event = lldb.SBEvent() + if listener.WaitForEvent (options.event_timeout, event): + if lldb.SBProcess.EventIsProcessEvent(event): + state = lldb.SBProcess.GetStateFromEvent (event) + if state == lldb.eStateInvalid: + # Not a state event + print 'process event = %s' % (event) + else: + print "process state changed event: %s" % (lldb.SBDebugger.StateAsCString(state)) + if state == lldb.eStateStopped: + if stop_idx == 0: + if launch_info: + print "process %u launched" % (pid) + run_commands(command_interpreter, ['breakpoint list']) + else: + print "attached to process %u" % (pid) + for m in target.modules: + print m + if options.breakpoints: + for bp in options.breakpoints: + debugger.HandleCommand( "_regexp-break %s" % (bp)) + run_commands(command_interpreter, ['breakpoint list']) + run_commands (command_interpreter, options.launch_commands) + else: + if options.verbose: + print "process %u stopped" % (pid) + run_commands (command_interpreter, options.stop_commands) + stop_idx += 1 + print_threads (process, options) + print "continuing process %u" % (pid) + process.Continue() + elif state == lldb.eStateExited: + exit_desc = process.GetExitDescription() + if exit_desc: + print "process %u exited with status %u: %s" % (pid, process.GetExitStatus (), exit_desc) + else: + print "process %u exited with status %u" % (pid, process.GetExitStatus ()) + run_commands (command_interpreter, options.exit_commands) + done = True + elif state == lldb.eStateCrashed: + print "process %u crashed" % (pid) + print_threads (process, options) + run_commands (command_interpreter, options.crash_commands) + done = True + elif state == lldb.eStateDetached: + print "process %u detached" % (pid) + done = True + elif state == lldb.eStateRunning: + # process is running, don't say anything, we will always get one of these after resuming + if options.verbose: + print "process %u resumed" % (pid) + elif state == lldb.eStateUnloaded: + print "process %u unloaded, this shouldn't happen" % (pid) + done = True + elif state == lldb.eStateConnected: + print "process connected" + elif state == lldb.eStateAttaching: + print "process attaching" + elif state == lldb.eStateLaunching: + print "process launching" + else: + print 'event = %s' % (event) + else: + # timeout waiting for an event + print "no process event for %u seconds, killing the process..." % (options.event_timeout) + done = True + # Now that we are done dump the stdout and stderr + process_stdout = process.GetSTDOUT(1024) + if process_stdout: + print "Process STDOUT:\n%s" % (process_stdout) + while process_stdout: + process_stdout = process.GetSTDOUT(1024) + print process_stdout + process_stderr = process.GetSTDERR(1024) + if process_stderr: + print "Process STDERR:\n%s" % (process_stderr) + while process_stderr: + process_stderr = process.GetSTDERR(1024) + print process_stderr + process.Kill() # kill the process + else: + if error: + print error + else: + if launch_info: + print 'error: launch failed' + else: + print 'error: attach failed' + + lldb.SBDebugger.Terminate() + +if __name__ == '__main__': + main(sys.argv[1:])
\ No newline at end of file diff --git a/examples/python/pytracer.py b/examples/python/pytracer.py new file mode 100644 index 000000000000..61bbceefe057 --- /dev/null +++ b/examples/python/pytracer.py @@ -0,0 +1,328 @@ +import sys +import inspect +from collections import OrderedDict + +class TracebackFancy: + def __init__(self,traceback): + self.t = traceback + + def getFrame(self): + return FrameFancy(self.t.tb_frame) + + def getLineNumber(self): + return self.t.tb_lineno if self.t != None else None + + def getNext(self): + return TracebackFancy(self.t.tb_next) + + def __str__(self): + if self.t == None: + return "" + str_self = "%s @ %s" % (self.getFrame().getName(), self.getLineNumber()) + return str_self + "\n" + self.getNext().__str__() + +class ExceptionFancy: + def __init__(self,frame): + self.etraceback = frame.f_exc_traceback + self.etype = frame.exc_type + self.evalue = frame.f_exc_value + + def __init__(self,tb,ty,va): + self.etraceback = tb + self.etype = ty + self.evalue = va + + def getTraceback(self): + return TracebackFancy(self.etraceback) + + def __nonzero__(self): + return self.etraceback != None or self.etype != None or self.evalue != None + + def getType(self): + return str(self.etype) + + def getValue(self): + return self.evalue + +class CodeFancy: + def __init__(self,code): + self.c = code + + def getArgCount(self): + return self.c.co_argcount if self.c != None else 0 + + def getFilename(self): + return self.c.co_filename if self.c != None else "" + + def getVariables(self): + return self.c.co_varnames if self.c != None else [] + + def getName(self): + return self.c.co_name if self.c != None else "" + + def getFileName(self): + return self.c.co_filename if self.c != None else "" + +class ArgsFancy: + def __init__(self,frame,arginfo): + self.f = frame + self.a = arginfo + + def __str__(self): + args, varargs, kwargs = self.getArgs(), self.getVarArgs(), self.getKWArgs() + ret = "" + count = 0 + size = len(args) + for arg in args: + ret = ret + ("%s = %s" % (arg, args[arg])) + count = count + 1 + if count < size: + ret = ret + ", " + if varargs: + if size > 0: + ret = ret + " " + ret = ret + "varargs are " + str(varargs) + if kwargs: + if size > 0: + ret = ret + " " + ret = ret + "kwargs are " + str(kwargs) + return ret + + def getNumArgs(wantVarargs = False, wantKWArgs=False): + args, varargs, keywords, values = self.a + size = len(args) + if varargs and wantVarargs: + size = size+len(self.getVarArgs()) + if keywords and wantKWArgs: + size = size+len(self.getKWArgs()) + return size + + def getArgs(self): + args, _, _, values = self.a + argWValues = OrderedDict() + for arg in args: + argWValues[arg] = values[arg] + return argWValues + + def getVarArgs(self): + _, vargs, _, _ = self.a + if vargs: + return self.f.f_locals[vargs] + return () + + def getKWArgs(self): + _, _, kwargs, _ = self.a + if kwargs: + return self.f.f_locals[kwargs] + return {} + +class FrameFancy: + def __init__(self,frame): + self.f = frame + + def getCaller(self): + return FrameFancy(self.f.f_back) + + def getLineNumber(self): + return self.f.f_lineno if self.f != None else 0 + + def getCodeInformation(self): + return CodeFancy(self.f.f_code) if self.f != None else None + + def getExceptionInfo(self): + return ExceptionFancy(self.f) if self.f != None else None + + def getName(self): + return self.getCodeInformation().getName() if self.f != None else "" + + def getFileName(self): + return self.getCodeInformation().getFileName() if self.f != None else "" + + def getLocals(self): + return self.f.f_locals if self.f != None else {} + + def getArgumentInfo(self): + return ArgsFancy(self.f,inspect.getargvalues(self.f)) if self.f != None else None + +class TracerClass: + def callEvent(self,frame): + pass + + def lineEvent(self,frame): + pass + + def returnEvent(self,frame,retval): + pass + + def exceptionEvent(self,frame,exception,value,traceback): + pass + + def cCallEvent(self,frame,cfunct): + pass + + def cReturnEvent(self,frame,cfunct): + pass + + def cExceptionEvent(self,frame,cfunct): + pass + +tracer_impl = TracerClass() + + +def the_tracer_entrypoint(frame,event,args): + if tracer_impl == None: + return None + if event == "call": + call_retval = tracer_impl.callEvent(FrameFancy(frame)) + if call_retval == False: + return None + return the_tracer_entrypoint + elif event == "line": + line_retval = tracer_impl.lineEvent(FrameFancy(frame)) + if line_retval == False: + return None + return the_tracer_entrypoint + elif event == "return": + tracer_impl.returnEvent(FrameFancy(frame),args) + elif event == "exception": + exty,exva,extb = args + exception_retval = tracer_impl.exceptionEvent(FrameFancy(frame),ExceptionFancy(extb,exty,exva)) + if exception_retval == False: + return None + return the_tracer_entrypoint + elif event == "c_call": + tracer_impl.cCallEvent(FrameFancy(frame),args) + elif event == "c_return": + tracer_impl.cReturnEvent(FrameFancy(frame),args) + elif event == "c_exception": + tracer_impl.cExceptionEvent(FrameFancy(frame),args) + return None + +def enable(t=None): + global tracer_impl + if t: + tracer_impl = t + sys.settrace(the_tracer_entrypoint) + +def disable(): + sys.settrace(None) + +class LoggingTracer: + def callEvent(self,frame): + print "call " + frame.getName() + " from " + frame.getCaller().getName() + " @ " + str(frame.getCaller().getLineNumber()) + " args are " + str(frame.getArgumentInfo()) + + def lineEvent(self,frame): + print "running " + frame.getName() + " @ " + str(frame.getLineNumber()) + " locals are " + str(frame.getLocals()) + " in " + frame.getFileName() + + def returnEvent(self,frame,retval): + print "return from " + frame.getName() + " value is " + str(retval) + " locals are " + str(frame.getLocals()) + + def exceptionEvent(self,frame,exception): + print "exception %s %s raised from %s @ %s" % (exception.getType(), str(exception.getValue()), frame.getName(), frame.getLineNumber()) + print "tb: " + str(exception.getTraceback()) + +# the same functionality as LoggingTracer, but with a little more lldb-specific smarts +class LLDBAwareTracer: + def callEvent(self,frame): + if frame.getName() == "<module>": + return + if frame.getName() == "run_one_line": + print "call run_one_line(%s)" % (frame.getArgumentInfo().getArgs()["input_string"]) + return + if "Python.framework" in frame.getFileName(): + print "call into Python at " + frame.getName() + return + if frame.getName() == "__init__" and frame.getCaller().getName() == "run_one_line" and frame.getCaller().getLineNumber() == 101: + return False + strout = "call " + frame.getName() + if (frame.getCaller().getFileName() == ""): + strout += " from LLDB - args are " + args = frame.getArgumentInfo().getArgs() + for arg in args: + if arg == "dict" or arg == "internal_dict": + continue + strout = strout + ("%s = %s " % (arg,args[arg])) + else: + strout += " from " + frame.getCaller().getName() + " @ " + str(frame.getCaller().getLineNumber()) + " args are " + str(frame.getArgumentInfo()) + print strout + + def lineEvent(self,frame): + if frame.getName() == "<module>": + return + if frame.getName() == "run_one_line": + print "running run_one_line(%s) @ %s" % (frame.getArgumentInfo().getArgs()["input_string"],frame.getLineNumber()) + return + if "Python.framework" in frame.getFileName(): + print "running into Python at " + frame.getName() + " @ " + str(frame.getLineNumber()) + return + strout = "running " + frame.getName() + " @ " + str(frame.getLineNumber()) + " locals are " + if (frame.getCaller().getFileName() == ""): + locals = frame.getLocals() + for local in locals: + if local == "dict" or local == "internal_dict": + continue + strout = strout + ("%s = %s " % (local,locals[local])) + else: + strout = strout + str(frame.getLocals()) + strout = strout + " in " + frame.getFileName() + print strout + + def returnEvent(self,frame,retval): + if frame.getName() == "<module>": + return + if frame.getName() == "run_one_line": + print "return from run_one_line(%s) return value is %s" % (frame.getArgumentInfo().getArgs()["input_string"],retval) + return + if "Python.framework" in frame.getFileName(): + print "return from Python at " + frame.getName() + " return value is " + str(retval) + return + strout = "return from " + frame.getName() + " return value is " + str(retval) + " locals are " + if (frame.getCaller().getFileName() == ""): + locals = frame.getLocals() + for local in locals: + if local == "dict" or local == "internal_dict": + continue + strout = strout + ("%s = %s " % (local,locals[local])) + else: + strout = strout + str(frame.getLocals()) + strout = strout + " in " + frame.getFileName() + print strout + + def exceptionEvent(self,frame,exception): + if frame.getName() == "<module>": + return + print "exception %s %s raised from %s @ %s" % (exception.getType(), str(exception.getValue()), frame.getName(), frame.getLineNumber()) + print "tb: " + str(exception.getTraceback()) + +def f(x,y=None): + if x > 0: + return 2 + f(x-2) + return 35 + +def g(x): + return 1.134 / x + +def print_keyword_args(**kwargs): + # kwargs is a dict of the keyword args passed to the function + for key, value in kwargs.iteritems(): + print "%s = %s" % (key, value) + +def total(initial=5, *numbers, **keywords): + count = initial + for number in numbers: + count += number + for key in keywords: + count += keywords[key] + return count + +if __name__ == "__main__": + enable(LoggingTracer()) + f(5) + f(5,1) + print_keyword_args(first_name="John", last_name="Doe") + total(10, 1, 2, 3, vegetables=50, fruits=100) + try: + g(0) + except: + pass + disable() diff --git a/examples/python/sbvalue.py b/examples/python/sbvalue.py new file mode 100755 index 000000000000..59c0b61e5528 --- /dev/null +++ b/examples/python/sbvalue.py @@ -0,0 +1,255 @@ +#!/usr/bin/python + +import lldb + +class value(object): + '''A class that wraps an lldb.SBValue object and returns an object that + can be used as an object with attribytes:\n + argv = a.value(lldb.frame.FindVariable('argv'))\n + argv.name - return the name of the value that this object contains\n + argv.type - return the lldb.SBType for this value + argv.type_name - return the name of the type + argv.size - return the byte size of this value + argv.is_in_scope - return true if this value is currently in scope + argv.is_pointer - return true if this value is a pointer + argv.format - return the current format for this value + argv.value - return the value's value as a string + argv.summary - return a summary of this value's value + argv.description - return the runtime description for this value + argv.location - return a string that represents the values location (address, register, etc) + argv.target - return the lldb.SBTarget for this value + argv.process - return the lldb.SBProcess for this value + argv.thread - return the lldb.SBThread for this value + argv.frame - return the lldb.SBFrame for this value + argv.num_children - return the number of children this value has + argv.children - return a list of sbvalue objects that represents all of the children of this value + ''' + def __init__(self, sbvalue): + self.sbvalue = sbvalue + + def __nonzero__(self): + return self.sbvalue.__nonzero__() + + def __repr__(self): + return self.sbvalue.__repr__() + + def __str__(self): + return self.sbvalue.__str__() + + def __getitem__(self, key): + if type(key) is int: + return value(self.sbvalue.GetChildAtIndex(key, lldb.eNoDynamicValues, True)) + raise TypeError + + def __getattr__(self, name): + if name == 'name': + return self.sbvalue.GetName() + if name == 'type': + return self.sbvalue.GetType() + if name == 'type_name': + return self.sbvalue.GetTypeName() + if name == 'size': + return self.sbvalue.GetByteSize() + if name == 'is_in_scope': + return self.sbvalue.IsInScope() + if name == 'is_pointer': + return self.sbvalue.TypeIsPointerType() + if name == 'format': + return self.sbvalue.GetFormat () + if name == 'value': + return self.sbvalue.GetValue () + if name == 'summary': + return self.sbvalue.GetSummary () + if name == 'description': + return self.sbvalue.GetObjectDescription () + if name == 'location': + return self.sbvalue.GetLocation () + if name == 'target': + return self.sbvalue.GetTarget() + if name == 'process': + return self.sbvalue.GetProcess() + if name == 'thread': + return self.sbvalue.GetThread() + if name == 'frame': + return self.sbvalue.GetFrame() + if name == 'num_children': + return self.sbvalue.GetNumChildren() + if name == 'children': + # Returns an array of sbvalue objects, one for each child of + # the value for the lldb.SBValue + children = [] + for i in range (self.sbvalue.GetNumChildren()): + children.append(value(self.sbvalue.GetChildAtIndex(i, lldb.eNoDynamicValues, True))) + return children + raise AttributeError + +class variable(object): + '''A class that treats a lldb.SBValue and allows it to be used just as + a variable would be in code. So if you have a Point structure variable + in your code, you would be able to do: "pt.x + pt.y"''' + def __init__(self, sbvalue): + self.sbvalue = sbvalue + + def __nonzero__(self): + return self.sbvalue.__nonzero__() + + def __repr__(self): + return self.sbvalue.__repr__() + + def __str__(self): + return self.sbvalue.__str__() + + def __getitem__(self, key): + # Allow array access if this value has children... + if type(key) is int: + return variable(self.sbvalue.GetValueForExpressionPath("[%i]" % key)) + raise TypeError + + def __getattr__(self, name): + child_sbvalue = self.sbvalue.GetChildMemberWithName (name) + if child_sbvalue: + return variable(child_sbvalue) + raise AttributeError + + def __add__(self, other): + return int(self) + int(other) + + def __sub__(self, other): + return int(self) - int(other) + + def __mul__(self, other): + return int(self) * int(other) + + def __floordiv__(self, other): + return int(self) // int(other) + + def __mod__(self, other): + return int(self) % int(other) + + def __divmod__(self, other): + return int(self) % int(other) + + def __pow__(self, other): + return int(self) ** int(other) + + def __lshift__(self, other): + return int(self) << int(other) + + def __rshift__(self, other): + return int(self) >> int(other) + + def __and__(self, other): + return int(self) & int(other) + + def __xor__(self, other): + return int(self) ^ int(other) + + def __or__(self, other): + return int(self) | int(other) + + def __div__(self, other): + return int(self) / int(other) + + def __truediv__(self, other): + return int(self) / int(other) + + def __iadd__(self, other): + result = self.__add__(other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __isub__(self, other): + result = self.__sub__(other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __imul__(self, other): + result = self.__mul__(other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __idiv__(self, other): + result = self.__div__(other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __itruediv__(self, other): + result = self.__truediv__(other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __ifloordiv__(self, other): + result = self.__floordiv__(self, other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __imod__(self, other): + result = self.__and__(self, other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __ipow__(self, other): + result = self.__pow__(self, other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __ipow__(self, other, modulo): + result = self.__pow__(self, other, modulo) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __ilshift__(self, other): + result = self.__lshift__(self, other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __irshift__(self, other): + result = self.__rshift__(self, other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __iand__(self, other): + result = self.__and__(self, other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __ixor__(self, other): + result = self.__xor__(self, other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __ior__(self, other): + result = self.__ior__(self, other) + self.sbvalue.SetValueFromCString (str(result)) + return result + + def __neg__(self): + return -int(self) + + def __pos__(self): + return +int(self) + + def __abs__(self): + return abs(int(self)) + + def __invert__(self): + return ~int(self) + + def __complex__(self): + return complex (int(self)) + + def __int__(self): + return self.sbvalue.GetValueAsSigned() + + def __long__(self): + return self.sbvalue.GetValueAsSigned() + + def __float__(self): + return float (self.sbvalue.GetValueAsSigned()) + + def __oct__(self): + return '0%o' % self.sbvalue.GetValueAsSigned() + + def __hex__(self): + return '0x%x' % self.sbvalue.GetValueAsSigned() +
\ No newline at end of file diff --git a/examples/python/scripted_step.py b/examples/python/scripted_step.py new file mode 100644 index 000000000000..8affb9e83220 --- /dev/null +++ b/examples/python/scripted_step.py @@ -0,0 +1,186 @@ +############################################################################# +# This script contains two trivial examples of simple "scripted step" classes. +# To fully understand how the lldb "Thread Plan" architecture works, read the +# comments at the beginning of ThreadPlan.h in the lldb sources. The python +# interface is a reduced version of the full internal mechanism, but captures +# most of the power with a much simpler interface. +# +# But I'll attempt a brief summary here. +# Stepping in lldb is done independently for each thread. Moreover, the stepping +# operations are stackable. So for instance if you did a "step over", and in +# the course of stepping over you hit a breakpoint, stopped and stepped again, +# the first "step-over" would be suspended, and the new step operation would +# be enqueued. Then if that step over caused the program to hit another breakpoint, +# lldb would again suspend the second step and return control to the user, so +# now there are two pending step overs. Etc. with all the other stepping +# operations. Then if you hit "continue" the bottom-most step-over would complete, +# and another continue would complete the first "step-over". +# +# lldb represents this system with a stack of "Thread Plans". Each time a new +# stepping operation is requested, a new plan is pushed on the stack. When the +# operation completes, it is pushed off the stack. +# +# The bottom-most plan in the stack is the immediate controller of stepping, +# most importantly, when the process resumes, the bottom most plan will get +# asked whether to set the program running freely, or to instruction-single-step +# the current thread. In the scripted interface, you indicate this by returning +# False or True respectively from the should_step method. +# +# Each time the process stops the thread plan stack for each thread that stopped +# "for a reason", Ii.e. a single-step completed on that thread, or a breakpoint +# was hit), is queried to determine how to proceed, starting from the most +# recently pushed plan, in two stages: +# +# 1) Each plan is asked if it "explains" the stop. The first plan to claim the +# stop wins. In scripted Thread Plans, this is done by returning True from +# the "explains_stop method. This is how, for instance, control is returned +# to the User when the "step-over" plan hits a breakpoint. The step-over +# plan doesn't explain the breakpoint stop, so it returns false, and the +# breakpoint hit is propagated up the stack to the "base" thread plan, which +# is the one that handles random breakpoint hits. +# +# 2) Then the plan that won the first round is asked if the process should stop. +# This is done in the "should_stop" method. The scripted plans actually do +# three jobs in should_stop: +# a) They determine if they have completed their job or not. If they have +# they indicate that by calling SetPlanComplete on their thread plan. +# b) They decide whether they want to return control to the user or not. +# They do this by returning True or False respectively. +# c) If they are not done, they set up whatever machinery they will use +# the next time the thread continues. +# +# Note that deciding to return control to the user, and deciding your plan +# is done, are orthgonal operations. You could set up the next phase of +# stepping, and then return True from should_stop, and when the user next +# "continued" the process your plan would resume control. Of course, the +# user might also "step-over" or some other operation that would push a +# different plan, which would take control till it was done. +# +# One other detail you should be aware of, if the plan below you on the +# stack was done, then it will be popped and the next plan will take control +# and its "should_stop" will be called. +# +# Note also, there should be another method called when your plan is popped, +# to allow you to do whatever cleanup is required. I haven't gotten to that +# yet. For now you should do that at the same time you mark your plan complete. +# +# Both examples show stepping through an address range for 20 bytes from the +# current PC. The first one does it by single stepping and checking a condition. +# It doesn't, however handle the case where you step into another frame while +# still in the current range in the starting frame. +# +# That is better handled in the second example by using the built-in StepOverRange +# thread plan. +# +# To use these stepping modes, you would do: +# +# (lldb) command script import scripted_step.py +# (lldb) thread step-scripted -C scripted_step.SimpleStep +# or +# +# (lldb) thread step-scripted -C scripted_step.StepWithPlan + +import lldb + +class SimpleStep: + def __init__ (self, thread_plan, dict): + self.thread_plan = thread_plan + self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPC() + + def explains_stop (self, event): + # We are stepping, so if we stop for any other reason, it isn't + # because of us. + if self.thread_plan.GetThread().GetStopReason()== lldb.eStopReasonTrace: + return True + else: + return False + + def should_stop (self, event): + cur_pc = self.thread_plan.GetThread().GetFrameAtIndex(0).GetPC() + + if cur_pc < self.start_address or cur_pc >= self.start_address + 20: + self.thread_plan.SetPlanComplete(True) + return True + else: + return False + + def should_step (self): + return True + +class StepWithPlan: + def __init__ (self, thread_plan, dict): + self.thread_plan = thread_plan + self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPCAddress() + self.step_thread_plan =thread_plan.QueueThreadPlanForStepOverRange(self.start_address, 20); + + def explains_stop (self, event): + # Since all I'm doing is running a plan, I will only ever get askedthis + # if myplan doesn't explain the stop, and in that caseI don'teither. + return False + + def should_stop (self, event): + if self.step_thread_plan.IsPlanComplete(): + self.thread_plan.SetPlanComplete(True) + return True + else: + return False + + def should_step (self): + return False + +# Here's another example which does "step over" through the current function, +# and when it stops at each line, it checks some condition (in this example the +# value of a variable) and stops if that condition is true. + +class StepCheckingCondition: + def __init__ (self, thread_plan, dict): + self.thread_plan = thread_plan + self.start_frame = thread_plan.GetThread().GetFrameAtIndex(0) + self.queue_next_plan() + + def queue_next_plan (self): + cur_frame = self.thread_plan.GetThread().GetFrameAtIndex(0) + cur_line_entry = cur_frame.GetLineEntry() + start_address = cur_line_entry.GetStartAddress() + end_address = cur_line_entry.GetEndAddress() + line_range = end_address.GetFileAddress() - start_address.GetFileAddress() + self.step_thread_plan = self.thread_plan.QueueThreadPlanForStepOverRange(start_address, line_range) + + def explains_stop (self, event): + # We are stepping, so if we stop for any other reason, it isn't + # because of us. + return False + + def should_stop (self, event): + if not self.step_thread_plan.IsPlanComplete(): + return False + + frame = self.thread_plan.GetThread().GetFrameAtIndex(0) + if not self.start_frame.IsEqual(frame): + self.thread_plan.SetPlanComplete(True) + return True + + # This part checks the condition. In this case we are expecting + # some integer variable called "a", and will stop when it is 20. + a_var = frame.FindVariable("a") + + if not a_var.IsValid(): + print "A was not valid." + return True + + error = lldb.SBError() + a_value = a_var.GetValueAsSigned (error) + if not error.Success(): + print "A value was not good." + return True + + if a_value == 20: + self.thread_plan.SetPlanComplete(True) + return True + else: + self.queue_next_plan() + return False + + def should_step (self): + return True + diff --git a/examples/python/sources.py b/examples/python/sources.py new file mode 100644 index 000000000000..0eb5858805be --- /dev/null +++ b/examples/python/sources.py @@ -0,0 +1,28 @@ +#!/usr/bin/python + +import lldb +import shlex + +def dump_module_sources(module, result): + if module: + print >> result, "Module: %s" % (module.file) + for compile_unit in module.compile_units: + if compile_unit.file: + print >> result, " %s" % (compile_unit.file) + +def info_sources(debugger, command, result, dict): + description='''This command will dump all compile units in any modules that are listed as arguments, or for all modules if no arguments are supplied.''' + module_names = shlex.split(command) + target = debugger.GetSelectedTarget() + if module_names: + for module_name in module_names: + dump_module_sources(target.module[module_name], result) + else: + for module in target.modules: + dump_module_sources(module, result) + + +def __lldb_init_module (debugger, dict): + # Add any commands contained in this module to LLDB + debugger.HandleCommand('command script add -f sources.info_sources info_sources') + print 'The "info_sources" command has been installed, type "help info_sources" or "info_sources --help" for detailed help.' diff --git a/examples/python/stacks.py b/examples/python/stacks.py new file mode 100755 index 000000000000..06907e159d7f --- /dev/null +++ b/examples/python/stacks.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +import lldb +import commands +import optparse +import shlex + +def stack_frames(debugger, command, result, dict): + command_args = shlex.split(command) + usage = "usage: %prog [options] <PATH> [PATH ...]" + description='''This command will enumerate all stack frames, print the stack size for each, and print an aggregation of which functions have the largest stack frame sizes at the end.''' + parser = optparse.OptionParser(description=description, prog='ls',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + try: + (options, args) = parser.parse_args(command_args) + except: + return + + target = debugger.GetSelectedTarget() + process = target.GetProcess() + + frame_info = {} + for thread in process: + last_frame = None + print "thread %u" % (thread.id) + for frame in thread.frames: + if last_frame: + frame_size = 0 + if frame.idx == 1: + if frame.fp == last_frame.fp: + # No frame one the first frame (might be right at the entry point) + first_frame_size = 0 + frame_size = frame.fp - frame.sp + else: + # First frame that has a valid size + first_frame_size = last_frame.fp - last_frame.sp + print "<%#7x> %s" % (first_frame_size, last_frame) + if first_frame_size: + name = last_frame.name + if name not in frame_info: + frame_info[name] = first_frame_size + else: + frame_info[name] += first_frame_size + else: + # Second or higher frame + frame_size = frame.fp - last_frame.fp + print "<%#7x> %s" % (frame_size, frame) + if frame_size > 0: + name = frame.name + if name not in frame_info: + frame_info[name] = frame_size + else: + frame_info[name] += frame_size + last_frame = frame + print frame_info + + +lldb.debugger.HandleCommand("command script add -f stacks.stack_frames stack_frames") +print "A new command called 'stack_frames' was added, type 'stack_frames --help' for more information."
\ No newline at end of file diff --git a/examples/python/symbolication.py b/examples/python/symbolication.py new file mode 100755 index 000000000000..2f2a274dbc41 --- /dev/null +++ b/examples/python/symbolication.py @@ -0,0 +1,640 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# Be sure to add the python path that points to the LLDB shared library. +# +# To use this in the embedded python interpreter using "lldb": +# +# cd /path/containing/crashlog.py +# lldb +# (lldb) script import crashlog +# "crashlog" command installed, type "crashlog --help" for detailed help +# (lldb) crashlog ~/Library/Logs/DiagnosticReports/a.crash +# +# The benefit of running the crashlog command inside lldb in the +# embedded python interpreter is when the command completes, there +# will be a target with all of the files loaded at the locations +# described in the crash log. Only the files that have stack frames +# in the backtrace will be loaded unless the "--load-all" option +# has been specified. This allows users to explore the program in the +# state it was in right at crash time. +# +# On MacOSX csh, tcsh: +# ( setenv PYTHONPATH /path/to/LLDB.framework/Resources/Python ; ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash ) +# +# On MacOSX sh, bash: +# PYTHONPATH=/path/to/LLDB.framework/Resources/Python ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash +#---------------------------------------------------------------------- + +import lldb +import commands +import optparse +import os +import plistlib +import re +import shlex +import sys +import time +import uuid + +class Address: + """Class that represents an address that will be symbolicated""" + def __init__(self, target, load_addr): + self.target = target + self.load_addr = load_addr # The load address that this object represents + self.so_addr = None # the resolved lldb.SBAddress (if any), named so_addr for section/offset address + self.sym_ctx = None # The cached symbol context for this address + self.description = None # Any original textual description of this address to be used as a backup in case symbolication fails + self.symbolication = None # The cached symbolicated string that describes this address + self.inlined = False + def __str__(self): + s = "%#16.16x" % (self.load_addr) + if self.symbolication: + s += " %s" % (self.symbolication) + elif self.description: + s += " %s" % (self.description) + elif self.so_addr: + s += " %s" % (self.so_addr) + return s + + def resolve_addr(self): + if self.so_addr == None: + self.so_addr = self.target.ResolveLoadAddress (self.load_addr) + return self.so_addr + + def is_inlined(self): + return self.inlined + + def get_symbol_context(self): + if self.sym_ctx == None: + sb_addr = self.resolve_addr() + if sb_addr: + self.sym_ctx = self.target.ResolveSymbolContextForAddress (sb_addr, lldb.eSymbolContextEverything) + else: + self.sym_ctx = lldb.SBSymbolContext() + return self.sym_ctx + + def get_instructions(self): + sym_ctx = self.get_symbol_context() + if sym_ctx: + function = sym_ctx.GetFunction() + if function: + return function.GetInstructions(self.target) + return sym_ctx.GetSymbol().GetInstructions(self.target) + return None + + def symbolicate(self, verbose = False): + if self.symbolication == None: + self.symbolication = '' + self.inlined = False + sym_ctx = self.get_symbol_context() + if sym_ctx: + module = sym_ctx.GetModule() + if module: + # Print full source file path in verbose mode + if verbose: + self.symbolication += str(module.GetFileSpec()) + '`' + else: + self.symbolication += module.GetFileSpec().GetFilename() + '`' + function_start_load_addr = -1 + function = sym_ctx.GetFunction() + block = sym_ctx.GetBlock() + line_entry = sym_ctx.GetLineEntry() + symbol = sym_ctx.GetSymbol() + inlined_block = block.GetContainingInlinedBlock(); + if function: + self.symbolication += function.GetName() + + if inlined_block: + self.inlined = True + self.symbolication += ' [inlined] ' + inlined_block.GetInlinedName(); + block_range_idx = inlined_block.GetRangeIndexForBlockAddress (self.so_addr) + if block_range_idx < lldb.UINT32_MAX: + block_range_start_addr = inlined_block.GetRangeStartAddress (block_range_idx) + function_start_load_addr = block_range_start_addr.GetLoadAddress (self.target) + if function_start_load_addr == -1: + function_start_load_addr = function.GetStartAddress().GetLoadAddress (self.target) + elif symbol: + self.symbolication += symbol.GetName() + function_start_load_addr = symbol.GetStartAddress().GetLoadAddress (self.target) + else: + self.symbolication = '' + return False + + # Dump the offset from the current function or symbol if it is non zero + function_offset = self.load_addr - function_start_load_addr + if function_offset > 0: + self.symbolication += " + %u" % (function_offset) + elif function_offset < 0: + self.symbolication += " %i (invalid negative offset, file a bug) " % function_offset + + # Print out any line information if any is available + if line_entry.GetFileSpec(): + # Print full source file path in verbose mode + if verbose: + self.symbolication += ' at %s' % line_entry.GetFileSpec() + else: + self.symbolication += ' at %s' % line_entry.GetFileSpec().GetFilename() + self.symbolication += ':%u' % line_entry.GetLine () + column = line_entry.GetColumn() + if column > 0: + self.symbolication += ':%u' % column + return True + return False + +class Section: + """Class that represents an load address range""" + sect_info_regex = re.compile('(?P<name>[^=]+)=(?P<range>.*)') + addr_regex = re.compile('^\s*(?P<start>0x[0-9A-Fa-f]+)\s*$') + range_regex = re.compile('^\s*(?P<start>0x[0-9A-Fa-f]+)\s*(?P<op>[-+])\s*(?P<end>0x[0-9A-Fa-f]+)\s*$') + + def __init__(self, start_addr = None, end_addr = None, name = None): + self.start_addr = start_addr + self.end_addr = end_addr + self.name = name + + @classmethod + def InitWithSBTargetAndSBSection(cls, target, section): + sect_load_addr = section.GetLoadAddress(target) + if sect_load_addr != lldb.LLDB_INVALID_ADDRESS: + obj = cls(sect_load_addr, sect_load_addr + section.size, section.name) + return obj + else: + return None + + def contains(self, addr): + return self.start_addr <= addr and addr < self.end_addr; + + def set_from_string(self, s): + match = self.sect_info_regex.match (s) + if match: + self.name = match.group('name') + range_str = match.group('range') + addr_match = self.addr_regex.match(range_str) + if addr_match: + self.start_addr = int(addr_match.group('start'), 16) + self.end_addr = None + return True + + range_match = self.range_regex.match(range_str) + if range_match: + self.start_addr = int(range_match.group('start'), 16) + self.end_addr = int(range_match.group('end'), 16) + op = range_match.group('op') + if op == '+': + self.end_addr += self.start_addr + return True + print 'error: invalid section info string "%s"' % s + print 'Valid section info formats are:' + print 'Format Example Description' + print '--------------------- -----------------------------------------------' + print '<name>=<base> __TEXT=0x123000 Section from base address only' + print '<name>=<base>-<end> __TEXT=0x123000-0x124000 Section from base address and end address' + print '<name>=<base>+<size> __TEXT=0x123000+0x1000 Section from base address and size' + return False + + def __str__(self): + if self.name: + if self.end_addr != None: + if self.start_addr != None: + return "%s=[0x%16.16x - 0x%16.16x)" % (self.name, self.start_addr, self.end_addr) + else: + if self.start_addr != None: + return "%s=0x%16.16x" % (self.name, self.start_addr) + return self.name + return "<invalid>" + +class Image: + """A class that represents an executable image and any associated data""" + + def __init__(self, path, uuid = None): + self.path = path + self.resolved_path = None + self.resolved = False + self.unavailable = False + self.uuid = uuid + self.section_infos = list() + self.identifier = None + self.version = None + self.arch = None + self.module = None + self.symfile = None + self.slide = None + + @classmethod + def InitWithSBTargetAndSBModule(cls, target, module): + '''Initialize this Image object with a module from a target.''' + obj = cls(module.file.fullpath, module.uuid) + obj.resolved_path = module.platform_file.fullpath + obj.resolved = True + obj.arch = module.triple + for section in module.sections: + symb_section = Section.InitWithSBTargetAndSBSection(target, section) + if symb_section: + obj.section_infos.append (symb_section) + obj.arch = module.triple + obj.module = module + obj.symfile = None + obj.slide = None + return obj + + def dump(self, prefix): + print "%s%s" % (prefix, self) + + def debug_dump(self): + print 'path = "%s"' % (self.path) + print 'resolved_path = "%s"' % (self.resolved_path) + print 'resolved = %i' % (self.resolved) + print 'unavailable = %i' % (self.unavailable) + print 'uuid = %s' % (self.uuid) + print 'section_infos = %s' % (self.section_infos) + print 'identifier = "%s"' % (self.identifier) + print 'version = %s' % (self.version) + print 'arch = %s' % (self.arch) + print 'module = %s' % (self.module) + print 'symfile = "%s"' % (self.symfile) + print 'slide = %i (0x%x)' % (self.slide, self.slide) + + def __str__(self): + s = '' + if self.uuid: + s += "%s " % (self.get_uuid()) + if self.arch: + s += "%s " % (self.arch) + if self.version: + s += "%s " % (self.version) + resolved_path = self.get_resolved_path() + if resolved_path: + s += "%s " % (resolved_path) + for section_info in self.section_infos: + s += ", %s" % (section_info) + if self.slide != None: + s += ', slide = 0x%16.16x' % self.slide + return s + + def add_section(self, section): + #print "added '%s' to '%s'" % (section, self.path) + self.section_infos.append (section) + + def get_section_containing_load_addr (self, load_addr): + for section_info in self.section_infos: + if section_info.contains(load_addr): + return section_info + return None + + def get_resolved_path(self): + if self.resolved_path: + return self.resolved_path + elif self.path: + return self.path + return None + + def get_resolved_path_basename(self): + path = self.get_resolved_path() + if path: + return os.path.basename(path) + return None + + def symfile_basename(self): + if self.symfile: + return os.path.basename(self.symfile) + return None + + def has_section_load_info(self): + return self.section_infos or self.slide != None + + def load_module(self, target): + if self.unavailable: + return None # We already warned that we couldn't find this module, so don't return an error string + # Load this module into "target" using the section infos to + # set the section load addresses + if self.has_section_load_info(): + if target: + if self.module: + if self.section_infos: + num_sections_loaded = 0 + for section_info in self.section_infos: + if section_info.name: + section = self.module.FindSection (section_info.name) + if section: + error = target.SetSectionLoadAddress (section, section_info.start_addr) + if error.Success(): + num_sections_loaded += 1 + else: + return 'error: %s' % error.GetCString() + else: + return 'error: unable to find the section named "%s"' % section_info.name + else: + return 'error: unable to find "%s" section in "%s"' % (range.name, self.get_resolved_path()) + if num_sections_loaded == 0: + return 'error: no sections were successfully loaded' + else: + err = target.SetModuleLoadAddress(self.module, self.slide) + if err.Fail(): + return err.GetCString() + return None + else: + return 'error: invalid module' + else: + return 'error: invalid target' + else: + return 'error: no section infos' + + def add_module(self, target): + '''Add the Image described in this object to "target" and load the sections if "load" is True.''' + if target: + # Try and find using UUID only first so that paths need not match up + uuid_str = self.get_normalized_uuid_string() + if uuid_str: + self.module = target.AddModule (None, None, uuid_str) + if not self.module: + self.locate_module_and_debug_symbols () + if self.unavailable: + return None + resolved_path = self.get_resolved_path() + self.module = target.AddModule (resolved_path, self.arch, uuid_str, self.symfile) + if not self.module: + return 'error: unable to get module for (%s) "%s"' % (self.arch, self.get_resolved_path()) + if self.has_section_load_info(): + return self.load_module(target) + else: + return None # No sections, the module was added to the target, so success + else: + return 'error: invalid target' + + def locate_module_and_debug_symbols (self): + # By default, just use the paths that were supplied in: + # self.path + # self.resolved_path + # self.module + # self.symfile + # Subclasses can inherit from this class and override this function + self.resolved = True + return True + + def get_uuid(self): + if not self.uuid and self.module: + self.uuid = uuid.UUID(self.module.GetUUIDString()) + return self.uuid + + def get_normalized_uuid_string(self): + if self.uuid: + return str(self.uuid).upper() + return None + + def create_target(self): + '''Create a target using the information in this Image object.''' + if self.unavailable: + return None + + if self.locate_module_and_debug_symbols (): + resolved_path = self.get_resolved_path(); + path_spec = lldb.SBFileSpec (resolved_path) + #result.PutCString ('plist[%s] = %s' % (uuid, self.plist)) + error = lldb.SBError() + target = lldb.debugger.CreateTarget (resolved_path, self.arch, None, False, error); + if target: + self.module = target.FindModule(path_spec) + if self.has_section_load_info(): + err = self.load_module(target) + if err: + print 'ERROR: ', err + return target + else: + print 'error: unable to create a valid target for (%s) "%s"' % (self.arch, self.path) + else: + print 'error: unable to locate main executable (%s) "%s"' % (self.arch, self.path) + return None + +class Symbolicator: + + def __init__(self): + """A class the represents the information needed to symbolicate addresses in a program""" + self.target = None + self.images = list() # a list of images to be used when symbolicating + self.addr_mask = 0xffffffffffffffff + + @classmethod + def InitWithSBTarget(cls, target): + obj = cls() + obj.target = target + obj.images = list(); + triple = target.triple + if triple: + arch = triple.split('-')[0] + if "arm" in arch: + obj.addr_mask = 0xfffffffffffffffe + + for module in target.modules: + image = Image.InitWithSBTargetAndSBModule(target, module) + obj.images.append(image) + return obj + + def __str__(self): + s = "Symbolicator:\n" + if self.target: + s += "Target = '%s'\n" % (self.target) + s += "Target modules:\n" + for m in self.target.modules: + s += str(m) + "\n" + s += "Images:\n" + for image in self.images: + s += ' %s\n' % (image) + return s + + def find_images_with_identifier(self, identifier): + images = list() + for image in self.images: + if image.identifier == identifier: + images.append(image) + if len(images) == 0: + regex_text = '^.*\.%s$' % (identifier) + regex = re.compile(regex_text) + for image in self.images: + if regex.match(image.identifier): + images.append(image) + return images + + def find_image_containing_load_addr(self, load_addr): + for image in self.images: + if image.get_section_containing_load_addr (load_addr): + return image + return None + + def create_target(self): + if self.target: + return self.target + + if self.images: + for image in self.images: + self.target = image.create_target () + if self.target: + if self.target.GetAddressByteSize() == 4: + triple = self.target.triple + if triple: + arch = triple.split('-')[0] + if "arm" in arch: + self.addr_mask = 0xfffffffffffffffe + return self.target + return None + + + def symbolicate(self, load_addr, verbose = False): + if not self.target: + self.create_target() + if self.target: + live_process = False + process = self.target.process + if process: + state = process.state + if state > lldb.eStateUnloaded and state < lldb.eStateDetached: + live_process = True + # If we don't have a live process, we can attempt to find the image + # that a load address belongs to and lazily load its module in the + # target, but we shouldn't do any of this if we have a live process + if not live_process: + image = self.find_image_containing_load_addr (load_addr) + if image: + image.add_module (self.target) + symbolicated_address = Address(self.target, load_addr) + if symbolicated_address.symbolicate (verbose): + if symbolicated_address.so_addr: + symbolicated_addresses = list() + symbolicated_addresses.append(symbolicated_address) + # See if we were able to reconstruct anything? + while 1: + inlined_parent_so_addr = lldb.SBAddress() + inlined_parent_sym_ctx = symbolicated_address.sym_ctx.GetParentOfInlinedScope (symbolicated_address.so_addr, inlined_parent_so_addr) + if not inlined_parent_sym_ctx: + break + if not inlined_parent_so_addr: + break + + symbolicated_address = Address(self.target, inlined_parent_so_addr.GetLoadAddress(self.target)) + symbolicated_address.sym_ctx = inlined_parent_sym_ctx + symbolicated_address.so_addr = inlined_parent_so_addr + symbolicated_address.symbolicate (verbose) + + # push the new frame onto the new frame stack + symbolicated_addresses.append (symbolicated_address) + + if symbolicated_addresses: + return symbolicated_addresses + else: + print 'error: no target in Symbolicator' + return None + + +def disassemble_instructions (target, instructions, pc, insts_before_pc, insts_after_pc, non_zeroeth_frame): + lines = list() + pc_index = -1 + comment_column = 50 + for inst_idx, inst in enumerate(instructions): + inst_pc = inst.GetAddress().GetLoadAddress(target); + if pc == inst_pc: + pc_index = inst_idx + mnemonic = inst.GetMnemonic (target) + operands = inst.GetOperands (target) + comment = inst.GetComment (target) + #data = inst.GetData (target) + lines.append ("%#16.16x: %8s %s" % (inst_pc, mnemonic, operands)) + if comment: + line_len = len(lines[-1]) + if line_len < comment_column: + lines[-1] += ' ' * (comment_column - line_len) + lines[-1] += "; %s" % comment + + if pc_index >= 0: + # If we are disassembling the non-zeroeth frame, we need to backup the PC by 1 + if non_zeroeth_frame and pc_index > 0: + pc_index = pc_index - 1 + if insts_before_pc == -1: + start_idx = 0 + else: + start_idx = pc_index - insts_before_pc + if start_idx < 0: + start_idx = 0 + if insts_before_pc == -1: + end_idx = inst_idx + else: + end_idx = pc_index + insts_after_pc + if end_idx > inst_idx: + end_idx = inst_idx + for i in range(start_idx, end_idx+1): + if i == pc_index: + print ' -> ', lines[i] + else: + print ' ', lines[i] + +def print_module_section_data (section): + print section + section_data = section.GetSectionData() + if section_data: + ostream = lldb.SBStream() + section_data.GetDescription (ostream, section.GetFileAddress()) + print ostream.GetData() + +def print_module_section (section, depth): + print section + if depth > 0: + num_sub_sections = section.GetNumSubSections() + for sect_idx in range(num_sub_sections): + print_module_section (section.GetSubSectionAtIndex(sect_idx), depth - 1) + +def print_module_sections (module, depth): + for sect in module.section_iter(): + print_module_section (sect, depth) + +def print_module_symbols (module): + for sym in module: + print sym + +def Symbolicate(command_args): + + usage = "usage: %prog [options] <addr1> [addr2 ...]" + description='''Symbolicate one or more addresses using LLDB's python scripting API..''' + parser = optparse.OptionParser(description=description, prog='crashlog.py',usage=usage) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) + parser.add_option('-p', '--platform', type='string', metavar='platform', dest='platform', help='Specify the platform to use when creating the debug target. Valid values include "localhost", "darwin-kernel", "ios-simulator", "remote-freebsd", "remote-macosx", "remote-ios", "remote-linux".') + parser.add_option('-f', '--file', type='string', metavar='file', dest='file', help='Specify a file to use when symbolicating') + parser.add_option('-a', '--arch', type='string', metavar='arch', dest='arch', help='Specify a architecture to use when symbolicating') + parser.add_option('-s', '--slide', type='int', metavar='slide', dest='slide', help='Specify the slide to use on the file specified with the --file option', default=None) + parser.add_option('--section', type='string', action='append', dest='section_strings', help='specify <sect-name>=<start-addr> or <sect-name>=<start-addr>-<end-addr>') + try: + (options, args) = parser.parse_args(command_args) + except: + return + symbolicator = Symbolicator() + images = list(); + if options.file: + image = Image(options.file); + image.arch = options.arch + # Add any sections that were specified with one or more --section options + if options.section_strings: + for section_str in options.section_strings: + section = Section() + if section.set_from_string (section_str): + image.add_section (section) + else: + sys.exit(1) + if options.slide != None: + image.slide = options.slide + symbolicator.images.append(image) + + target = symbolicator.create_target() + if options.verbose: + print symbolicator + if target: + for addr_str in args: + addr = int(addr_str, 0) + symbolicated_addrs = symbolicator.symbolicate(addr, options.verbose) + for symbolicated_addr in symbolicated_addrs: + print symbolicated_addr + print + else: + print 'error: no target for %s' % (symbolicator) + +if __name__ == '__main__': + # Create a new debugger instance + lldb.debugger = lldb.SBDebugger.Create() + Symbolicate (sys.argv[1:]) diff --git a/examples/python/types.py b/examples/python/types.py new file mode 100755 index 000000000000..60ea7514c13a --- /dev/null +++ b/examples/python/types.py @@ -0,0 +1,265 @@ +#!/usr/bin/python + +#---------------------------------------------------------------------- +# Be sure to add the python path that points to the LLDB shared library. +# +# # To use this in the embedded python interpreter using "lldb" just +# import it with the full path using the "command script import" +# command +# (lldb) command script import /path/to/cmdtemplate.py +#---------------------------------------------------------------------- + +import commands +import platform +import os +import re +import signal +import sys + +try: + # Just try for LLDB in case PYTHONPATH is already correctly setup + import lldb +except ImportError: + lldb_python_dirs = list() + # lldb is not in the PYTHONPATH, try some defaults for the current platform + platform_system = platform.system() + if platform_system == 'Darwin': + # On Darwin, try the currently selected Xcode directory + xcode_dir = commands.getoutput("xcode-select --print-path") + if xcode_dir: + lldb_python_dirs.append(os.path.realpath(xcode_dir + '/../SharedFrameworks/LLDB.framework/Resources/Python')) + lldb_python_dirs.append(xcode_dir + '/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + lldb_python_dirs.append('/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python') + success = False + for lldb_python_dir in lldb_python_dirs: + if os.path.exists(lldb_python_dir): + if not (sys.path.__contains__(lldb_python_dir)): + sys.path.append(lldb_python_dir) + try: + import lldb + except ImportError: + pass + else: + print 'imported lldb from: "%s"' % (lldb_python_dir) + success = True + break + if not success: + print "error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly" + sys.exit(1) + +import commands +import optparse +import shlex +import time + +def regex_option_callback(option, opt_str, value, parser): + if opt_str == "--std": + value = '^std::' + regex = re.compile(value) + parser.values.skip_type_regexes.append (regex) + +def create_types_options(for_lldb_command): + if for_lldb_command: + usage = "usage: %prog [options]" + description='''This command will help check for padding in between +base classes and members in structs and classes. It will summarize the types +and how much padding was found. If no types are specified with the --types TYPENAME +option, all structure and class types will be verified. If no modules are +specified with the --module option, only the target's main executable will be +searched. +''' + else: + usage = "usage: %prog [options] EXEPATH [EXEPATH ...]" + description='''This command will help check for padding in between +base classes and members in structures and classes. It will summarize the types +and how much padding was found. One or more paths to executable files must be +specified and targets will be created with these modules. If no types are +specified with the --types TYPENAME option, all structure and class types will +be verified in all specified modules. +''' + parser = optparse.OptionParser(description=description, prog='framestats',usage=usage) + if not for_lldb_command: + parser.add_option('-a', '--arch', type='string', dest='arch', help='The architecture to use when creating the debug target.', default=None) + parser.add_option('-p', '--platform', type='string', metavar='platform', dest='platform', help='Specify the platform to use when creating the debug target. Valid values include "localhost", "darwin-kernel", "ios-simulator", "remote-freebsd", "remote-macosx", "remote-ios", "remote-linux".') + parser.add_option('-m', '--module', action='append', type='string', metavar='MODULE', dest='modules', help='Specify one or more modules which will be used to verify the types.', default=[]) + parser.add_option('-d', '--debug', action='store_true', dest='debug', help='Pause 10 seconds to wait for a debugger to attach.', default=False) + parser.add_option('-t', '--type', action='append', type='string', metavar='TYPENAME', dest='typenames', help='Specify one or more type names which should be verified. If no type names are specified, all class and struct types will be verified.', default=[]) + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='Enable verbose logging and information.', default=False) + parser.add_option('-s', '--skip-type-regex', action="callback", callback=regex_option_callback, type='string', metavar='REGEX', dest='skip_type_regexes', help='Regular expressions that, if they match the current member typename, will cause the type to no be recursively displayed.', default=[]) + parser.add_option('--std', action="callback", callback=regex_option_callback, metavar='REGEX', dest='skip_type_regexes', help="Don't' recurse into types in the std namespace.", default=[]) + return parser + +def verify_type (target, options, type): + print type + typename = type.GetName() + # print 'type: %s' % (typename) + (end_offset, padding) = verify_type_recursive (target, options, type, None, 0, 0, 0) + byte_size = type.GetByteSize() + # if end_offset < byte_size: + # last_member_padding = byte_size - end_offset + # print '%+4u <%u> padding' % (end_offset, last_member_padding) + # padding += last_member_padding + print 'Total byte size: %u' % (byte_size) + print 'Total pad bytes: %u' % (padding) + if padding > 0: + print 'Padding percentage: %2.2f %%' % ((float(padding) / float(byte_size)) * 100.0) + print + +def verify_type_recursive (target, options, type, member_name, depth, base_offset, padding): + prev_end_offset = base_offset + typename = type.GetName() + byte_size = type.GetByteSize() + if member_name and member_name != typename: + print '%+4u <%3u> %s%s %s;' % (base_offset, byte_size, ' ' * depth, typename, member_name) + else: + print '%+4u {%3u} %s%s' % (base_offset, byte_size, ' ' * depth, typename) + + for type_regex in options.skip_type_regexes: + match = type_regex.match (typename) + if match: + return (base_offset + byte_size, padding) + + members = type.members + if members: + for member_idx, member in enumerate(members): + member_type = member.GetType() + member_canonical_type = member_type.GetCanonicalType() + member_type_class = member_canonical_type.GetTypeClass() + member_name = member.GetName() + member_offset = member.GetOffsetInBytes() + member_total_offset = member_offset + base_offset + member_byte_size = member_type.GetByteSize() + member_is_class_or_struct = False + if member_type_class == lldb.eTypeClassStruct or member_type_class == lldb.eTypeClassClass: + member_is_class_or_struct = True + if member_idx == 0 and member_offset == target.GetAddressByteSize() and type.IsPolymorphicClass(): + ptr_size = target.GetAddressByteSize() + print '%+4u <%3u> %s__vtbl_ptr_type * _vptr;' % (prev_end_offset, ptr_size, ' ' * (depth + 1)) + prev_end_offset = ptr_size + else: + if prev_end_offset < member_total_offset: + member_padding = member_total_offset - prev_end_offset + padding = padding + member_padding + print '%+4u <%3u> %s<PADDING>' % (prev_end_offset, member_padding, ' ' * (depth + 1)) + + if member_is_class_or_struct: + (prev_end_offset, padding) = verify_type_recursive (target, options, member_canonical_type, member_name, depth + 1, member_total_offset, padding) + else: + prev_end_offset = member_total_offset + member_byte_size + member_typename = member_type.GetName() + if member.IsBitfield(): + print '%+4u <%3u> %s%s:%u %s;' % (member_total_offset, member_byte_size, ' ' * (depth + 1), member_typename, member.GetBitfieldSizeInBits(), member_name) + else: + print '%+4u <%3u> %s%s %s;' % (member_total_offset, member_byte_size, ' ' * (depth + 1), member_typename, member_name) + + if prev_end_offset < byte_size: + last_member_padding = byte_size - prev_end_offset + print '%+4u <%3u> %s<PADDING>' % (prev_end_offset, last_member_padding, ' ' * (depth + 1)) + padding += last_member_padding + else: + if type.IsPolymorphicClass(): + ptr_size = target.GetAddressByteSize() + print '%+4u <%3u> %s__vtbl_ptr_type * _vptr;' % (prev_end_offset, ptr_size, ' ' * (depth + 1)) + prev_end_offset = ptr_size + prev_end_offset = base_offset + byte_size + + return (prev_end_offset, padding) + +def check_padding_command (debugger, command, result, dict): + # Use the Shell Lexer to properly parse up command options just like a + # shell would + command_args = shlex.split(command) + parser = create_types_options(True) + try: + (options, args) = parser.parse_args(command_args) + except: + # if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit + # (courtesy of OptParse dealing with argument errors by throwing SystemExit) + result.SetStatus (lldb.eReturnStatusFailed) + return "option parsing failed" # returning a string is the same as returning an error whose description is the string + verify_types(debugger.GetSelectedTarget(), options) + +@lldb.command("parse_all_struct_class_types") +def parse_all_struct_class_types (debugger, command, result, dict): + command_args = shlex.split(command) + for f in command_args: + error = lldb.SBError() + target = debugger.CreateTarget (f, None, None, False, error) + module = target.GetModuleAtIndex(0) + print "Parsing all types in '%s'" % (module) + types = module.GetTypes(lldb.eTypeClassClass | lldb.eTypeClassStruct) + for t in types: + print t + print "" + + +def verify_types (target, options): + + if not target: + print 'error: invalid target' + return + + modules = list() + if len(options.modules) == 0: + # Append just the main executable if nothing was specified + module = target.modules[0] + if module: + modules.append(module) + else: + for module_name in options.modules: + module = lldb.target.module[module_name] + if module: + modules.append(module) + + if modules: + for module in modules: + print 'module: %s' % (module.file) + if options.typenames: + for typename in options.typenames: + types = module.FindTypes(typename) + if types.GetSize(): + print 'Found %u types matching "%s" in "%s"' % (len(types), typename, module.file) + for type in types: + verify_type (target, options, type) + else: + print 'error: no type matches "%s" in "%s"' % (typename, module.file) + else: + types = module.GetTypes(lldb.eTypeClassClass | lldb.eTypeClassStruct) + print 'Found %u types in "%s"' % (len(types), module.file) + for type in types: + verify_type (target, options, type) + else: + print 'error: no modules' + +if __name__ == '__main__': + debugger = lldb.SBDebugger.Create() + parser = create_types_options(False) + + # try: + (options, args) = parser.parse_args(sys.argv[1:]) + # except: + # print "error: option parsing failed" + # sys.exit(1) + + if options.debug: + print "Waiting for debugger to attach to process %d" % os.getpid() + os.kill(os.getpid(), signal.SIGSTOP) + + for path in args: + # in a command - the lldb.* convenience variables are not to be used + # and their values (if any) are undefined + # this is the best practice to access those objects from within a command + error = lldb.SBError() + target = debugger.CreateTarget (path, + options.arch, + options.platform, + True, + error) + if error.Fail(): + print error.GetCString() + continue + verify_types (target, options) + +elif getattr(lldb, 'debugger', None): + lldb.debugger.HandleCommand('command script add -f types.check_padding_command check_padding') + print '"check_padding" command installed, use the "--help" option for detailed help'
\ No newline at end of file diff --git a/examples/python/x86_64_linux_target_definition.py b/examples/python/x86_64_linux_target_definition.py new file mode 100644 index 000000000000..06cbe4c82963 --- /dev/null +++ b/examples/python/x86_64_linux_target_definition.py @@ -0,0 +1,353 @@ +#!/usr/bin/python +#===-- x86_64_linux_target_definition.py -----------------------------*- C++ -*-===// +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +#===----------------------------------------------------------------------===// + +#---------------------------------------------------------------------- +# DESCRIPTION +# +# This file can be used with the following setting: +# plugin.process.gdb-remote.target-definition-file +# This setting should be used when you are trying to connect to a +# remote GDB server that doesn't support any of the register discovery +# packets that LLDB normally uses. +# +# Why is this necessary? LLDB doesn't require a new build of LLDB that +# targets each new architecture you will debug with. Instead, all +# architectures are supported and LLDB relies on extra GDB server +# packets to discover the target we are connecting to so that is can +# show the right registers for each target. This allows the GDB server +# to change and add new registers without requiring a new LLDB build +# just so we can see new registers. +# +# This file implements the x86_64 registers for the darwin version of +# GDB and allows you to connect to servers that use this register set. +# +# USAGE +# +# (lldb) settings set plugin.process.gdb-remote.target-definition-file /path/to/x86_64_linux_target_definition.py +# (lldb) gdb-remote other.baz.com:1234 +# +# The target definition file will get used if and only if the +# qRegisterInfo packets are not supported when connecting to a remote +# GDB server. +#---------------------------------------------------------------------- +from lldb import * + +# Compiler and DWARF register numbers +name_to_gcc_dwarf_regnum = { + 'rax' : 0 , + 'rdx' : 1 , + 'rcx' : 2 , + 'rbx' : 3 , + 'rsi' : 4 , + 'rdi' : 5 , + 'rbp' : 6 , + 'rsp' : 7 , + 'r8' : 8 , + 'r9' : 9 , + 'r10' : 10, + 'r11' : 11, + 'r12' : 12, + 'r13' : 13, + 'r14' : 14, + 'r15' : 15, + 'rip' : 16, + 'xmm0' : 17, + 'xmm1' : 18, + 'xmm2' : 19, + 'xmm3' : 20, + 'xmm4' : 21, + 'xmm5' : 22, + 'xmm6' : 23, + 'xmm7' : 24, + 'xmm8' : 25, + 'xmm9' : 26, + 'xmm10' : 27, + 'xmm11' : 28, + 'xmm12' : 29, + 'xmm13' : 30, + 'xmm14' : 31, + 'xmm15' : 32, + 'stmm0' : 33, + 'stmm1' : 34, + 'stmm2' : 35, + 'stmm3' : 36, + 'stmm4' : 37, + 'stmm5' : 38, + 'stmm6' : 39, + 'stmm7' : 30, + 'ymm0' : 41, + 'ymm1' : 42, + 'ymm2' : 43, + 'ymm3' : 44, + 'ymm4' : 45, + 'ymm5' : 46, + 'ymm6' : 47, + 'ymm7' : 48, + 'ymm8' : 49, + 'ymm9' : 40, + 'ymm10' : 41, + 'ymm11' : 42, + 'ymm12' : 43, + 'ymm13' : 44, + 'ymm14' : 45, + 'ymm15' : 46 +}; + +name_to_gdb_regnum = { + 'rax' : 0, + 'rbx' : 1, + 'rcx' : 2, + 'rdx' : 3, + 'rsi' : 4, + 'rdi' : 5, + 'rbp' : 6, + 'rsp' : 7, + 'r8' : 8, + 'r9' : 9, + 'r10' : 10, + 'r11' : 11, + 'r12' : 12, + 'r13' : 13, + 'r14' : 14, + 'r15' : 15, + 'rip' : 16, + 'rflags': 17, + 'cs' : 18, + 'ss' : 19, + 'ds' : 20, + 'es' : 21, + 'fs' : 22, + 'gs' : 23, + 'stmm0' : 24, + 'stmm1' : 25, + 'stmm2' : 26, + 'stmm3' : 27, + 'stmm4' : 28, + 'stmm5' : 29, + 'stmm6' : 30, + 'stmm7' : 31, + 'fctrl' : 32, + 'fstat' : 33, + 'ftag' : 34, + 'fiseg' : 35, + 'fioff' : 36, + 'foseg' : 37, + 'fooff' : 38, + 'fop' : 39, + 'xmm0' : 40, + 'xmm1' : 41, + 'xmm2' : 42, + 'xmm3' : 43, + 'xmm4' : 44, + 'xmm5' : 45, + 'xmm6' : 46, + 'xmm7' : 47, + 'xmm8' : 48, + 'xmm9' : 49, + 'xmm10' : 50, + 'xmm11' : 51, + 'xmm12' : 52, + 'xmm13' : 53, + 'xmm14' : 54, + 'xmm15' : 55, + 'mxcsr' : 56, + 'ymm0' : 57, + 'ymm1' : 58, + 'ymm2' : 59, + 'ymm3' : 60, + 'ymm4' : 61, + 'ymm5' : 62, + 'ymm6' : 63, + 'ymm7' : 64, + 'ymm8' : 65, + 'ymm9' : 66, + 'ymm10' : 67, + 'ymm11' : 68, + 'ymm12' : 69, + 'ymm13' : 70, + 'ymm14' : 71, + 'ymm15' : 72 +}; + +name_to_generic_regnum = { + 'rip' : LLDB_REGNUM_GENERIC_PC, + 'rsp' : LLDB_REGNUM_GENERIC_SP, + 'rbp' : LLDB_REGNUM_GENERIC_FP, + 'rdi' : LLDB_REGNUM_GENERIC_ARG1, + 'rsi' : LLDB_REGNUM_GENERIC_ARG2, + 'rdx' : LLDB_REGNUM_GENERIC_ARG3, + 'rcx' : LLDB_REGNUM_GENERIC_ARG4, + 'r8' : LLDB_REGNUM_GENERIC_ARG5, + 'r9' : LLDB_REGNUM_GENERIC_ARG6 +}; + +def get_reg_num (reg_num_dict, reg_name): + if reg_name in reg_num_dict: + return reg_num_dict[reg_name] + return LLDB_INVALID_REGNUM + +x86_64_register_infos = [ +{ 'name':'rax' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'rbx' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'rcx' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg4' }, +{ 'name':'rdx' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg3' }, +{ 'name':'rsi' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg2' }, +{ 'name':'rdi' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg1' }, +{ 'name':'rbp' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'fp' }, +{ 'name':'rsp' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'sp' }, +{ 'name':'r8' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg5' }, +{ 'name':'r9' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg6' }, +{ 'name':'r10' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r11' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r12' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r13' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r14' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r15' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'rip' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'pc' }, +{ 'name':'rflags', 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'cs' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'ss' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'ds' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'es' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fs' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'gs' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'stmm0' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm1' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm2' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm3' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm4' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm5' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm6' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm7' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'fctrl' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fstat' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'ftag' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fiseg' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fioff' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'foseg' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fooff' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fop' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'xmm0' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm1' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm2' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm3' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm4' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm5' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm6' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm7' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm8' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm9' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm10' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm11' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm12' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm13' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm14' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm15' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'mxcsr' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'orig_rax' , 'set':1, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatHex }, +# Registers that are contained in or composed of one of more other registers +{ 'name':'eax' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[31:0]' }, +{ 'name':'ebx' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[31:0]' }, +{ 'name':'ecx' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[31:0]' }, +{ 'name':'edx' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[31:0]' }, +{ 'name':'edi' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdi[31:0]' }, +{ 'name':'esi' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsi[31:0]' }, +{ 'name':'ebp' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbp[31:0]' }, +{ 'name':'esp' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsp[31:0]' }, +{ 'name':'r8d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r8[31:0]' }, +{ 'name':'r9d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r9[31:0]' }, +{ 'name':'r10d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r10[31:0]' }, +{ 'name':'r11d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r11[31:0]' }, +{ 'name':'r12d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r12[31:0]' }, +{ 'name':'r13d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r13[31:0]' }, +{ 'name':'r14d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r14[31:0]' }, +{ 'name':'r15d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r15[31:0]' }, + +{ 'name':'ax' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[15:0]' }, +{ 'name':'bx' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[15:0]' }, +{ 'name':'cx' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[15:0]' }, +{ 'name':'dx' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[15:0]' }, +{ 'name':'di' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdi[15:0]' }, +{ 'name':'si' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsi[15:0]' }, +{ 'name':'bp' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbp[15:0]' }, +{ 'name':'sp' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsp[15:0]' }, +{ 'name':'r8w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r8[15:0]' }, +{ 'name':'r9w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r9[15:0]' }, +{ 'name':'r10w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r10[15:0]' }, +{ 'name':'r11w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r11[15:0]' }, +{ 'name':'r12w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r12[15:0]' }, +{ 'name':'r13w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r13[15:0]' }, +{ 'name':'r14w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r14[15:0]' }, +{ 'name':'r15w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r15[15:0]' }, + +{ 'name':'ah' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[15:8]' }, +{ 'name':'bh' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[15:8]' }, +{ 'name':'ch' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[15:8]' }, +{ 'name':'dh' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[15:8]' }, + +{ 'name':'al' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[7:0]' }, +{ 'name':'bl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[7:0]' }, +{ 'name':'cl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[7:0]' }, +{ 'name':'dl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[7:0]' }, +{ 'name':'dil' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdi[7:0]' }, +{ 'name':'sil' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsi[7:0]' }, +{ 'name':'bpl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbp[7:0]' }, +{ 'name':'spl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsp[7:0]' }, +{ 'name':'r8l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r8[7:0]' }, +{ 'name':'r9l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r9[7:0]' }, +{ 'name':'r10l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r10[7:0]' }, +{ 'name':'r11l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r11[7:0]' }, +{ 'name':'r12l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r12[7:0]' }, +{ 'name':'r13l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r13[7:0]' }, +{ 'name':'r14l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r14[7:0]' }, +{ 'name':'r15l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r15[7:0]' }, +]; + +g_target_definition = None + +def get_target_definition (): + global g_target_definition + if g_target_definition == None: + g_target_definition = {} + offset = 0 + for reg_info in x86_64_register_infos: + reg_name = reg_info['name'] + + # Only fill in the offset if there is no 'slice' in the register info + if 'slice' not in reg_info and 'composite' not in reg_info: + reg_info['offset'] = offset + offset += reg_info['bitsize']/8 + + # Set the GCC/DWARF register number for this register if it has one + reg_num = get_reg_num(name_to_gcc_dwarf_regnum, reg_name) + if reg_num != LLDB_INVALID_REGNUM: + reg_info['gcc'] = reg_num + reg_info['dwarf'] = reg_num + + # Set the generic register number for this register if it has one + reg_num = get_reg_num(name_to_generic_regnum, reg_name) + if reg_num != LLDB_INVALID_REGNUM: + reg_info['generic'] = reg_num + + # Set the GDB register number for this register if it has one + reg_num = get_reg_num(name_to_gdb_regnum, reg_name) + if reg_num != LLDB_INVALID_REGNUM: + reg_info['gdb'] = reg_num + + g_target_definition['sets'] = ['General Purpose Registers', 'Floating Point Registers'] + g_target_definition['registers'] = x86_64_register_infos + g_target_definition['host-info'] = { 'triple' : 'x86_64-*-linux', 'endian': eByteOrderLittle } + g_target_definition['g-packet-size'] = offset + g_target_definition['breakpoint-pc-offset'] = -1 + return g_target_definition + +def get_dynamic_setting(target, setting_name): + if setting_name == 'gdb-server-target-definition': + return get_target_definition()
\ No newline at end of file diff --git a/examples/python/x86_64_qemu_target_definition.py b/examples/python/x86_64_qemu_target_definition.py new file mode 100644 index 000000000000..7b246896d8bf --- /dev/null +++ b/examples/python/x86_64_qemu_target_definition.py @@ -0,0 +1,352 @@ +#!/usr/bin/python +#===-- x86_64_qemu_target_definition.py -----------------------------*- C++ -*-===// +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +#===----------------------------------------------------------------------===// + +#---------------------------------------------------------------------- +# DESCRIPTION +# +# This file can be used with the following setting: +# plugin.process.gdb-remote.target-definition-file +# This setting should be used when you are trying to connect to a +# remote GDB server that doesn't support any of the register discovery +# packets that LLDB normally uses. +# +# Why is this necessary? LLDB doesn't require a new build of LLDB that +# targets each new architecture you will debug with. Instead, all +# architectures are supported and LLDB relies on extra GDB server +# packets to discover the target we are connecting to so that is can +# show the right registers for each target. This allows the remote stub +# to change and add new registers without requiring a new LLDB build +# just so we can see new registers. +# +# This file implements the x86_64 registers for the user mode qemu on linux. +# The only difference with the Linux file is the absense of orig_rax register. +# +# USAGE +# +# (lldb) settings set plugin.process.gdb-remote.target-definition-file /path/to/x86_64_qemu_target_definition.py +# (lldb) gdb-remote other.baz.com:1234 +# +# The target definition file will get used if and only if the +# qRegisterInfo packets are not supported when connecting to a remote +# GDB stub. +#---------------------------------------------------------------------- +from lldb import * + +# Compiler and DWARF register numbers +name_to_gcc_dwarf_regnum = { + 'rax' : 0 , + 'rdx' : 1 , + 'rcx' : 2 , + 'rbx' : 3 , + 'rsi' : 4 , + 'rdi' : 5 , + 'rbp' : 6 , + 'rsp' : 7 , + 'r8' : 8 , + 'r9' : 9 , + 'r10' : 10, + 'r11' : 11, + 'r12' : 12, + 'r13' : 13, + 'r14' : 14, + 'r15' : 15, + 'rip' : 16, + 'xmm0' : 17, + 'xmm1' : 18, + 'xmm2' : 19, + 'xmm3' : 20, + 'xmm4' : 21, + 'xmm5' : 22, + 'xmm6' : 23, + 'xmm7' : 24, + 'xmm8' : 25, + 'xmm9' : 26, + 'xmm10' : 27, + 'xmm11' : 28, + 'xmm12' : 29, + 'xmm13' : 30, + 'xmm14' : 31, + 'xmm15' : 32, + 'stmm0' : 33, + 'stmm1' : 34, + 'stmm2' : 35, + 'stmm3' : 36, + 'stmm4' : 37, + 'stmm5' : 38, + 'stmm6' : 39, + 'stmm7' : 30, + 'ymm0' : 41, + 'ymm1' : 42, + 'ymm2' : 43, + 'ymm3' : 44, + 'ymm4' : 45, + 'ymm5' : 46, + 'ymm6' : 47, + 'ymm7' : 48, + 'ymm8' : 49, + 'ymm9' : 40, + 'ymm10' : 41, + 'ymm11' : 42, + 'ymm12' : 43, + 'ymm13' : 44, + 'ymm14' : 45, + 'ymm15' : 46 +}; + +name_to_gdb_regnum = { + 'rax' : 0, + 'rbx' : 1, + 'rcx' : 2, + 'rdx' : 3, + 'rsi' : 4, + 'rdi' : 5, + 'rbp' : 6, + 'rsp' : 7, + 'r8' : 8, + 'r9' : 9, + 'r10' : 10, + 'r11' : 11, + 'r12' : 12, + 'r13' : 13, + 'r14' : 14, + 'r15' : 15, + 'rip' : 16, + 'rflags': 17, + 'cs' : 18, + 'ss' : 19, + 'ds' : 20, + 'es' : 21, + 'fs' : 22, + 'gs' : 23, + 'stmm0' : 24, + 'stmm1' : 25, + 'stmm2' : 26, + 'stmm3' : 27, + 'stmm4' : 28, + 'stmm5' : 29, + 'stmm6' : 30, + 'stmm7' : 31, + 'fctrl' : 32, + 'fstat' : 33, + 'ftag' : 34, + 'fiseg' : 35, + 'fioff' : 36, + 'foseg' : 37, + 'fooff' : 38, + 'fop' : 39, + 'xmm0' : 40, + 'xmm1' : 41, + 'xmm2' : 42, + 'xmm3' : 43, + 'xmm4' : 44, + 'xmm5' : 45, + 'xmm6' : 46, + 'xmm7' : 47, + 'xmm8' : 48, + 'xmm9' : 49, + 'xmm10' : 50, + 'xmm11' : 51, + 'xmm12' : 52, + 'xmm13' : 53, + 'xmm14' : 54, + 'xmm15' : 55, + 'mxcsr' : 56, + 'ymm0' : 57, + 'ymm1' : 58, + 'ymm2' : 59, + 'ymm3' : 60, + 'ymm4' : 61, + 'ymm5' : 62, + 'ymm6' : 63, + 'ymm7' : 64, + 'ymm8' : 65, + 'ymm9' : 66, + 'ymm10' : 67, + 'ymm11' : 68, + 'ymm12' : 69, + 'ymm13' : 70, + 'ymm14' : 71, + 'ymm15' : 72 +}; + +name_to_generic_regnum = { + 'rip' : LLDB_REGNUM_GENERIC_PC, + 'rsp' : LLDB_REGNUM_GENERIC_SP, + 'rbp' : LLDB_REGNUM_GENERIC_FP, + 'rdi' : LLDB_REGNUM_GENERIC_ARG1, + 'rsi' : LLDB_REGNUM_GENERIC_ARG2, + 'rdx' : LLDB_REGNUM_GENERIC_ARG3, + 'rcx' : LLDB_REGNUM_GENERIC_ARG4, + 'r8' : LLDB_REGNUM_GENERIC_ARG5, + 'r9' : LLDB_REGNUM_GENERIC_ARG6 +}; + +def get_reg_num (reg_num_dict, reg_name): + if reg_name in reg_num_dict: + return reg_num_dict[reg_name] + return LLDB_INVALID_REGNUM + +x86_64_register_infos = [ +{ 'name':'rax' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'rbx' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'rcx' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg4' }, +{ 'name':'rdx' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg3' }, +{ 'name':'rsi' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg2' }, +{ 'name':'rdi' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg1' }, +{ 'name':'rbp' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'fp' }, +{ 'name':'rsp' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'sp' }, +{ 'name':'r8' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg5' }, +{ 'name':'r9' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg6' }, +{ 'name':'r10' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r11' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r12' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r13' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r14' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r15' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'rip' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'pc' }, +{ 'name':'rflags', 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'cs' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'ss' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'ds' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'es' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fs' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'gs' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'stmm0' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm1' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm2' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm3' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm4' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm5' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm6' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm7' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'fctrl' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fstat' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'ftag' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fiseg' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fioff' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'foseg' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fooff' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fop' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'xmm0' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm1' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm2' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm3' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm4' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm5' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm6' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm7' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm8' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm9' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm10' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm11' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm12' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm13' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm14' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm15' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'mxcsr' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +# Registers that are contained in or composed of one of more other registers +{ 'name':'eax' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[31:0]' }, +{ 'name':'ebx' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[31:0]' }, +{ 'name':'ecx' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[31:0]' }, +{ 'name':'edx' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[31:0]' }, +{ 'name':'edi' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdi[31:0]' }, +{ 'name':'esi' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsi[31:0]' }, +{ 'name':'ebp' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbp[31:0]' }, +{ 'name':'esp' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsp[31:0]' }, +{ 'name':'r8d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r8[31:0]' }, +{ 'name':'r9d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r9[31:0]' }, +{ 'name':'r10d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r10[31:0]' }, +{ 'name':'r11d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r11[31:0]' }, +{ 'name':'r12d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r12[31:0]' }, +{ 'name':'r13d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r13[31:0]' }, +{ 'name':'r14d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r14[31:0]' }, +{ 'name':'r15d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r15[31:0]' }, + +{ 'name':'ax' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[15:0]' }, +{ 'name':'bx' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[15:0]' }, +{ 'name':'cx' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[15:0]' }, +{ 'name':'dx' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[15:0]' }, +{ 'name':'di' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdi[15:0]' }, +{ 'name':'si' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsi[15:0]' }, +{ 'name':'bp' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbp[15:0]' }, +{ 'name':'sp' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsp[15:0]' }, +{ 'name':'r8w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r8[15:0]' }, +{ 'name':'r9w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r9[15:0]' }, +{ 'name':'r10w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r10[15:0]' }, +{ 'name':'r11w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r11[15:0]' }, +{ 'name':'r12w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r12[15:0]' }, +{ 'name':'r13w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r13[15:0]' }, +{ 'name':'r14w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r14[15:0]' }, +{ 'name':'r15w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r15[15:0]' }, + +{ 'name':'ah' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[15:8]' }, +{ 'name':'bh' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[15:8]' }, +{ 'name':'ch' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[15:8]' }, +{ 'name':'dh' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[15:8]' }, + +{ 'name':'al' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[7:0]' }, +{ 'name':'bl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[7:0]' }, +{ 'name':'cl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[7:0]' }, +{ 'name':'dl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[7:0]' }, +{ 'name':'dil' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdi[7:0]' }, +{ 'name':'sil' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsi[7:0]' }, +{ 'name':'bpl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbp[7:0]' }, +{ 'name':'spl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsp[7:0]' }, +{ 'name':'r8l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r8[7:0]' }, +{ 'name':'r9l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r9[7:0]' }, +{ 'name':'r10l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r10[7:0]' }, +{ 'name':'r11l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r11[7:0]' }, +{ 'name':'r12l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r12[7:0]' }, +{ 'name':'r13l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r13[7:0]' }, +{ 'name':'r14l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r14[7:0]' }, +{ 'name':'r15l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r15[7:0]' }, +]; + +g_target_definition = None + +def get_target_definition (): + global g_target_definition + if g_target_definition == None: + g_target_definition = {} + offset = 0 + for reg_info in x86_64_register_infos: + reg_name = reg_info['name'] + + # Only fill in the offset if there is no 'slice' in the register info + if 'slice' not in reg_info and 'composite' not in reg_info: + reg_info['offset'] = offset + offset += reg_info['bitsize']/8 + + # Set the GCC/DWARF register number for this register if it has one + reg_num = get_reg_num(name_to_gcc_dwarf_regnum, reg_name) + if reg_num != LLDB_INVALID_REGNUM: + reg_info['gcc'] = reg_num + reg_info['dwarf'] = reg_num + + # Set the generic register number for this register if it has one + reg_num = get_reg_num(name_to_generic_regnum, reg_name) + if reg_num != LLDB_INVALID_REGNUM: + reg_info['generic'] = reg_num + + # Set the GDB register number for this register if it has one + reg_num = get_reg_num(name_to_gdb_regnum, reg_name) + if reg_num != LLDB_INVALID_REGNUM: + reg_info['gdb'] = reg_num + + g_target_definition['sets'] = ['General Purpose Registers', 'Floating Point Registers'] + g_target_definition['registers'] = x86_64_register_infos + g_target_definition['host-info'] = { 'triple' : 'x86_64-*-linux', 'endian': eByteOrderLittle } + g_target_definition['g-packet-size'] = offset + g_target_definition['breakpoint-pc-offset'] = -1 + return g_target_definition + +def get_dynamic_setting(target, setting_name): + if setting_name == 'gdb-server-target-definition': + return get_target_definition() diff --git a/examples/python/x86_64_target_definition.py b/examples/python/x86_64_target_definition.py new file mode 100644 index 000000000000..3a1290b62f80 --- /dev/null +++ b/examples/python/x86_64_target_definition.py @@ -0,0 +1,357 @@ +#!/usr/bin/python +#===-- x86_64_target_definition.py -----------------------------*- C++ -*-===// +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +#===----------------------------------------------------------------------===// + +#---------------------------------------------------------------------- +# DESCRIPTION +# +# This file can be used with the following setting: +# plugin.process.gdb-remote.target-definition-file +# This setting should be used when you are trying to connect to a +# remote GDB server that doesn't support any of the register discovery +# packets that LLDB normally uses. +# +# Why is this necessary? LLDB doesn't require a new build of LLDB that +# targets each new architecture you will debug with. Instead, all +# architectures are supported and LLDB relies on extra GDB server +# packets to discover the target we are connecting to so that is can +# show the right registers for each target. This allows the GDB server +# to change and add new registers without requiring a new LLDB build +# just so we can see new registers. +# +# This file implements the x86_64 registers for the darwin version of +# GDB and allows you to connect to servers that use this register set. +# +# USAGE +# +# (lldb) settings set plugin.process.gdb-remote.target-definition-file /path/to/x86_64_target_definition.py +# (lldb) gdb-remote other.baz.com:1234 +# +# The target definition file will get used if and only if the +# qRegisterInfo packets are not supported when connecting to a remote +# GDB server. +#---------------------------------------------------------------------- +from lldb import * + +# Compiler and DWARF register numbers +name_to_gcc_dwarf_regnum = { + 'rax' : 0 , + 'rdx' : 1 , + 'rcx' : 2 , + 'rbx' : 3 , + 'rsi' : 4 , + 'rdi' : 5 , + 'rbp' : 6 , + 'rsp' : 7 , + 'r8' : 8 , + 'r9' : 9 , + 'r10' : 10, + 'r11' : 11, + 'r12' : 12, + 'r13' : 13, + 'r14' : 14, + 'r15' : 15, + 'rip' : 16, + 'xmm0' : 17, + 'xmm1' : 18, + 'xmm2' : 19, + 'xmm3' : 20, + 'xmm4' : 21, + 'xmm5' : 22, + 'xmm6' : 23, + 'xmm7' : 24, + 'xmm8' : 25, + 'xmm9' : 26, + 'xmm10' : 27, + 'xmm11' : 28, + 'xmm12' : 29, + 'xmm13' : 30, + 'xmm14' : 31, + 'xmm15' : 32, + 'stmm0' : 33, + 'stmm1' : 34, + 'stmm2' : 35, + 'stmm3' : 36, + 'stmm4' : 37, + 'stmm5' : 38, + 'stmm6' : 39, + 'stmm7' : 30, + 'ymm0' : 41, + 'ymm1' : 42, + 'ymm2' : 43, + 'ymm3' : 44, + 'ymm4' : 45, + 'ymm5' : 46, + 'ymm6' : 47, + 'ymm7' : 48, + 'ymm8' : 49, + 'ymm9' : 40, + 'ymm10' : 41, + 'ymm11' : 42, + 'ymm12' : 43, + 'ymm13' : 44, + 'ymm14' : 45, + 'ymm15' : 46 +}; + +name_to_gdb_regnum = { + 'rax' : 0, + 'rbx' : 1, + 'rcx' : 2, + 'rdx' : 3, + 'rsi' : 4, + 'rdi' : 5, + 'rbp' : 6, + 'rsp' : 7, + 'r8' : 8, + 'r9' : 9, + 'r10' : 10, + 'r11' : 11, + 'r12' : 12, + 'r13' : 13, + 'r14' : 14, + 'r15' : 15, + 'rip' : 16, + 'rflags': 17, + 'cs' : 18, + 'ss' : 19, + 'ds' : 20, + 'es' : 21, + 'fs' : 22, + 'gs' : 23, + 'stmm0' : 24, + 'stmm1' : 25, + 'stmm2' : 26, + 'stmm3' : 27, + 'stmm4' : 28, + 'stmm5' : 29, + 'stmm6' : 30, + 'stmm7' : 31, + 'fctrl' : 32, + 'fstat' : 33, + 'ftag' : 34, + 'fiseg' : 35, + 'fioff' : 36, + 'foseg' : 37, + 'fooff' : 38, + 'fop' : 39, + 'xmm0' : 40, + 'xmm1' : 41, + 'xmm2' : 42, + 'xmm3' : 43, + 'xmm4' : 44, + 'xmm5' : 45, + 'xmm6' : 46, + 'xmm7' : 47, + 'xmm8' : 48, + 'xmm9' : 49, + 'xmm10' : 50, + 'xmm11' : 51, + 'xmm12' : 52, + 'xmm13' : 53, + 'xmm14' : 54, + 'xmm15' : 55, + 'mxcsr' : 56, + 'ymm0' : 57, + 'ymm1' : 58, + 'ymm2' : 59, + 'ymm3' : 60, + 'ymm4' : 61, + 'ymm5' : 62, + 'ymm6' : 63, + 'ymm7' : 64, + 'ymm8' : 65, + 'ymm9' : 66, + 'ymm10' : 67, + 'ymm11' : 68, + 'ymm12' : 69, + 'ymm13' : 70, + 'ymm14' : 71, + 'ymm15' : 72 +}; + +name_to_generic_regnum = { + 'rip' : LLDB_REGNUM_GENERIC_PC, + 'rsp' : LLDB_REGNUM_GENERIC_SP, + 'rbp' : LLDB_REGNUM_GENERIC_FP, + 'rdi' : LLDB_REGNUM_GENERIC_ARG1, + 'rsi' : LLDB_REGNUM_GENERIC_ARG2, + 'rdx' : LLDB_REGNUM_GENERIC_ARG3, + 'rcx' : LLDB_REGNUM_GENERIC_ARG4, + 'r8' : LLDB_REGNUM_GENERIC_ARG5, + 'r9' : LLDB_REGNUM_GENERIC_ARG6 +}; + + +def get_reg_num (reg_num_dict, reg_name): + if reg_name in reg_num_dict: + return reg_num_dict[reg_name] + return LLDB_INVALID_REGNUM + +def get_reg_num (reg_num_dict, reg_name): + if reg_name in reg_num_dict: + return reg_num_dict[reg_name] + return LLDB_INVALID_REGNUM + +x86_64_register_infos = [ +{ 'name':'rax' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'rbx' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'rcx' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg4' }, +{ 'name':'rdx' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg3' }, +{ 'name':'rsi' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg2' }, +{ 'name':'rdi' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg1' }, +{ 'name':'rbp' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'fp' }, +{ 'name':'rsp' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'sp' }, +{ 'name':'r8' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg5' }, +{ 'name':'r9' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'arg6' }, +{ 'name':'r10' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r11' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r12' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r13' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r14' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'r15' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo }, +{ 'name':'rip' , 'set':0, 'bitsize':64 , 'encoding':eEncodingUint , 'format':eFormatAddressInfo, 'alt-name':'pc' }, +{ 'name':'rflags', 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'cs' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'ss' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'ds' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'es' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fs' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'gs' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'stmm0' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm1' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm2' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm3' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm4' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm5' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm6' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'stmm7' , 'set':1, 'bitsize':80 , 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'fctrl' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fstat' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'ftag' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fiseg' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fioff' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'foseg' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fooff' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'fop' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +{ 'name':'xmm0' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm1' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm2' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm3' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm4' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm5' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm6' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm7' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm8' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm9' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm10' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm11' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm12' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm13' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm14' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'xmm15' , 'set':1, 'bitsize':128, 'encoding':eEncodingVector, 'format':eFormatVectorOfUInt8 }, +{ 'name':'mxcsr' , 'set':1, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex }, +# Registers that are contained in or composed of one of more other registers +{ 'name':'eax' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[31:0]' }, +{ 'name':'ebx' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[31:0]' }, +{ 'name':'ecx' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[31:0]' }, +{ 'name':'edx' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[31:0]' }, +{ 'name':'edi' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdi[31:0]' }, +{ 'name':'esi' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsi[31:0]' }, +{ 'name':'ebp' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbp[31:0]' }, +{ 'name':'esp' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsp[31:0]' }, +{ 'name':'r8d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r8[31:0]' }, +{ 'name':'r9d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r9[31:0]' }, +{ 'name':'r10d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r10[31:0]' }, +{ 'name':'r11d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r11[31:0]' }, +{ 'name':'r12d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r12[31:0]' }, +{ 'name':'r13d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r13[31:0]' }, +{ 'name':'r14d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r14[31:0]' }, +{ 'name':'r15d' , 'set':0, 'bitsize':32 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r15[31:0]' }, + +{ 'name':'ax' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[15:0]' }, +{ 'name':'bx' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[15:0]' }, +{ 'name':'cx' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[15:0]' }, +{ 'name':'dx' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[15:0]' }, +{ 'name':'di' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdi[15:0]' }, +{ 'name':'si' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsi[15:0]' }, +{ 'name':'bp' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbp[15:0]' }, +{ 'name':'sp' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsp[15:0]' }, +{ 'name':'r8w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r8[15:0]' }, +{ 'name':'r9w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r9[15:0]' }, +{ 'name':'r10w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r10[15:0]' }, +{ 'name':'r11w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r11[15:0]' }, +{ 'name':'r12w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r12[15:0]' }, +{ 'name':'r13w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r13[15:0]' }, +{ 'name':'r14w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r14[15:0]' }, +{ 'name':'r15w' , 'set':0, 'bitsize':16 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r15[15:0]' }, + +{ 'name':'ah' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[15:8]' }, +{ 'name':'bh' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[15:8]' }, +{ 'name':'ch' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[15:8]' }, +{ 'name':'dh' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[15:8]' }, + +{ 'name':'al' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rax[7:0]' }, +{ 'name':'bl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbx[7:0]' }, +{ 'name':'cl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rcx[7:0]' }, +{ 'name':'dl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdx[7:0]' }, +{ 'name':'dil' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rdi[7:0]' }, +{ 'name':'sil' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsi[7:0]' }, +{ 'name':'bpl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rbp[7:0]' }, +{ 'name':'spl' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'rsp[7:0]' }, +{ 'name':'r8l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r8[7:0]' }, +{ 'name':'r9l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r9[7:0]' }, +{ 'name':'r10l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r10[7:0]' }, +{ 'name':'r11l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r11[7:0]' }, +{ 'name':'r12l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r12[7:0]' }, +{ 'name':'r13l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r13[7:0]' }, +{ 'name':'r14l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r14[7:0]' }, +{ 'name':'r15l' , 'set':0, 'bitsize':8 , 'encoding':eEncodingUint , 'format':eFormatHex , 'slice': 'r15[7:0]' }, +]; + +g_target_definition = None + +def get_target_definition (): + global g_target_definition + if g_target_definition == None: + g_target_definition = {} + offset = 0 + for reg_info in x86_64_register_infos: + reg_name = reg_info['name'] + + # Only fill in the offset if there is no 'slice' in the register info + if 'slice' not in reg_info and 'composite' not in reg_info: + reg_info['offset'] = offset + offset += reg_info['bitsize']/8 + + # Set the GCC/DWARF register number for this register if it has one + reg_num = get_reg_num(name_to_gcc_dwarf_regnum, reg_name) + if reg_num != LLDB_INVALID_REGNUM: + reg_info['gcc'] = reg_num + reg_info['dwarf'] = reg_num + + # Set the generic register number for this register if it has one + reg_num = get_reg_num(name_to_generic_regnum, reg_name) + if reg_num != LLDB_INVALID_REGNUM: + reg_info['generic'] = reg_num + + # Set the GDB register number for this register if it has one + reg_num = get_reg_num(name_to_gdb_regnum, reg_name) + if reg_num != LLDB_INVALID_REGNUM: + reg_info['gdb'] = reg_num + + g_target_definition['sets'] = ['General Purpose Registers', 'Floating Point Registers'] + g_target_definition['registers'] = x86_64_register_infos + g_target_definition['host-info'] = { 'triple' : 'x86_64-apple-macosx', 'endian': eByteOrderLittle } + g_target_definition['g-packet-size'] = offset + return g_target_definition + +def get_dynamic_setting(target, setting_name): + if setting_name == 'gdb-server-target-definition': + return get_target_definition()
\ No newline at end of file diff --git a/examples/scripting/dictionary.c b/examples/scripting/dictionary.c new file mode 100644 index 000000000000..a7e1390ccebd --- /dev/null +++ b/examples/scripting/dictionary.c @@ -0,0 +1,200 @@ +//===-- dictionary.c ---------------------------------------------*- C -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +#include <stdlib.h> +#include <stdio.h> +#include <ctype.h> +#include <string.h> + +typedef struct tree_node +{ + const char *word; + struct tree_node *left; + struct tree_node *right; +} tree_node; + +/* Given a char*, returns a substring that starts at the first + alphabet character and ends at the last alphabet character, i.e. it + strips off beginning or ending quotes, punctuation, etc. */ + +char * +strip (char **word) +{ + char *start = *word; + int len = strlen (start); + char *end = start + len - 1; + + while ((start < end) && (!isalpha (start[0]))) + start++; + + while ((end > start) && (!isalpha (end[0]))) + end--; + + if (start > end) + return NULL; + + end[1] = '\0'; + *word = start; + + return start; +} + +/* Given a binary search tree (sorted alphabetically by the word at + each node), and a new word, inserts the word at the appropriate + place in the tree. */ + +void +insert (tree_node *root, char *word) +{ + if (root == NULL) + return; + + int compare_value = strcmp (word, root->word); + + if (compare_value == 0) + return; + + if (compare_value < 0) + { + if (root->left != NULL) + insert (root->left, word); + else + { + tree_node *new_node = (tree_node *) malloc (sizeof (tree_node)); + new_node->word = strdup (word); + new_node->left = NULL; + new_node->right = NULL; + root->left = new_node; + } + } + else + { + if (root->right != NULL) + insert (root->right, word); + else + { + tree_node *new_node = (tree_node *) malloc (sizeof (tree_node)); + new_node->word = strdup (word); + new_node->left = NULL; + new_node->right = NULL; + root->right = new_node; + } + } +} + +/* Read in a text file and storea all the words from the file in a + binary search tree. */ + +void +populate_dictionary (tree_node **dictionary, char *filename) +{ + FILE *in_file; + char word[1024]; + + in_file = fopen (filename, "r"); + if (in_file) + { + while (fscanf (in_file, "%s", word) == 1) + { + char *new_word = (strdup (word)); + new_word = strip (&new_word); + if (*dictionary == NULL) + { + tree_node *new_node = (tree_node *) malloc (sizeof (tree_node)); + new_node->word = new_word; + new_node->left = NULL; + new_node->right = NULL; + *dictionary = new_node; + } + else + insert (*dictionary, new_word); + } + } +} + +/* Given a binary search tree and a word, search for the word + in the binary search tree. */ + +int +find_word (tree_node *dictionary, char *word) +{ + if (!word || !dictionary) + return 0; + + int compare_value = strcmp (word, dictionary->word); + + if (compare_value == 0) + return 1; + else if (compare_value < 0) + return find_word (dictionary->left, word); + else + return find_word (dictionary->right, word); +} + +/* Print out the words in the binary search tree, in sorted order. */ + +void +print_tree (tree_node *dictionary) +{ + if (!dictionary) + return; + + if (dictionary->left) + print_tree (dictionary->left); + + printf ("%s\n", dictionary->word); + + + if (dictionary->right) + print_tree (dictionary->right); +} + + +int +main (int argc, char **argv) +{ + tree_node *dictionary = NULL; + char buffer[1024]; + char *filename; + int done = 0; + + if (argc == 2) + filename = argv[1]; + + if (!filename) + return -1; + + populate_dictionary (&dictionary, filename); + fprintf (stdout, "Dictionary loaded.\nEnter search word: "); + while (!done && fgets (buffer, sizeof(buffer), stdin)) + { + char *word = buffer; + int len = strlen (word); + int i; + + for (i = 0; i < len; ++i) + word[i] = tolower (word[i]); + + if ((len > 0) && (word[len-1] == '\n')) + { + word[len-1] = '\0'; + len = len - 1; + } + + if (find_word (dictionary, word)) + fprintf (stdout, "Yes!\n"); + else + fprintf (stdout, "No!\n"); + + fprintf (stdout, "Enter search word: "); + } + + fprintf (stdout, "\n"); + return 0; +} + diff --git a/examples/scripting/tree_utils.py b/examples/scripting/tree_utils.py new file mode 100755 index 000000000000..e83f516ab580 --- /dev/null +++ b/examples/scripting/tree_utils.py @@ -0,0 +1,118 @@ +""" +# ===-- tree_utils.py ---------------------------------------*- Python -*-===// +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +# ===---------------------------------------------------------------------===// + +tree_utils.py - A set of functions for examining binary +search trees, based on the example search tree defined in +dictionary.c. These functions contain calls to LLDB API +functions, and assume that the LLDB Python module has been +imported. + +For a thorough explanation of how the DFS function works, and +for more information about dictionary.c go to +http://lldb.llvm.org/scripting.html +""" + + +def DFS (root, word, cur_path): + """ + Recursively traverse a binary search tree containing + words sorted alphabetically, searching for a particular + word in the tree. Also maintains a string representing + the path from the root of the tree to the current node. + If the word is found in the tree, return the path string. + Otherwise return an empty string. + + This function assumes the binary search tree is + the one defined in dictionary.c It uses LLDB API + functions to examine and traverse the tree nodes. + """ + + # Get pointer field values out of node 'root' + + root_word_ptr = root.GetChildMemberWithName ("word") + left_child_ptr = root.GetChildMemberWithName ("left") + right_child_ptr = root.GetChildMemberWithName ("right") + + # Get the word out of the word pointer and strip off + # surrounding quotes (added by call to GetSummary). + + root_word = root_word_ptr.GetSummary() + end = len (root_word) - 1 + if root_word[0] == '"' and root_word[end] == '"': + root_word = root_word[1:end] + end = len (root_word) - 1 + if root_word[0] == '\'' and root_word[end] == '\'': + root_word = root_word[1:end] + + # Main depth first search + + if root_word == word: + return cur_path + elif word < root_word: + + # Check to see if left child is NULL + + if left_child_ptr.GetValue() == None: + return "" + else: + cur_path = cur_path + "L" + return DFS (left_child_ptr, word, cur_path) + else: + + # Check to see if right child is NULL + + if right_child_ptr.GetValue() == None: + return "" + else: + cur_path = cur_path + "R" + return DFS (right_child_ptr, word, cur_path) + + +def tree_size (root): + """ + Recursively traverse a binary search tree, counting + the nodes in the tree. Returns the final count. + + This function assumes the binary search tree is + the one defined in dictionary.c It uses LLDB API + functions to examine and traverse the tree nodes. + """ + if (root.GetValue == None): + return 0 + + if (int (root.GetValue(), 16) == 0): + return 0 + + left_size = tree_size (root.GetChildAtIndex(1)); + right_size = tree_size (root.GetChildAtIndex(2)); + + total_size = left_size + right_size + 1 + return total_size + + +def print_tree (root): + """ + Recursively traverse a binary search tree, printing out + the words at the nodes in alphabetical order (the + search order for the binary tree). + + This function assumes the binary search tree is + the one defined in dictionary.c It uses LLDB API + functions to examine and traverse the tree nodes. + """ + if (root.GetChildAtIndex(1).GetValue() != None) and (int (root.GetChildAtIndex(1).GetValue(), 16) != 0): + print_tree (root.GetChildAtIndex(1)) + + print root.GetChildAtIndex(0).GetSummary() + + if (root.GetChildAtIndex(2).GetValue() != None) and (int (root.GetChildAtIndex(2).GetValue(), 16) != 0): + print_tree (root.GetChildAtIndex(2)) + + diff --git a/examples/summaries/cocoa/CFArray.py b/examples/summaries/cocoa/CFArray.py new file mode 100644 index 000000000000..5068875b5b38 --- /dev/null +++ b/examples/summaries/cocoa/CFArray.py @@ -0,0 +1,204 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for NSArray +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# much less functional than the other two cases below +# just runs code to get to the count and then returns +# no children +class NSArrayKVC_SynthProvider: + + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, dict, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + num_children_vo = self.valobj.CreateValueFromExpression("count","(int)[" + stream.GetData() + " count]"); + if num_children_vo.IsValid(): + return num_children_vo.GetValueAsUnsigned(0) + return "<variable is not NSArray>" + +# much less functional than the other two cases below +# just runs code to get to the count and then returns +# no children +class NSArrayCF_SynthProvider: + + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, dict, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not (self.sys_params.types_cache.ulong): + self.sys_params.types_cache.ulong = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + num_children_vo = self.valobj.CreateChildAtOffset("count", + self.sys_params.cfruntime_size, + self.sys_params.types_cache.ulong) + return num_children_vo.GetValueAsUnsigned(0) + +class NSArrayI_SynthProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, dict, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.long): + self.sys_params.types_cache.long = self.valobj.GetType().GetBasicType(lldb.eBasicTypeLong) + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # skip the isa pointer and get at the size + def num_children(self): + logger = lldb.formatters.Logger.Logger() + count = self.valobj.CreateChildAtOffset("count", + self.sys_params.pointer_size, + self.sys_params.types_cache.long); + return count.GetValueAsUnsigned(0) + +class NSArrayM_SynthProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, dict, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.long): + self.sys_params.types_cache.long = self.valobj.GetType().GetBasicType(lldb.eBasicTypeLong) + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # skip the isa pointer and get at the size + def num_children(self): + logger = lldb.formatters.Logger.Logger() + count = self.valobj.CreateChildAtOffset("count", + self.sys_params.pointer_size, + self.sys_params.types_cache.long); + return count.GetValueAsUnsigned(0) + +# this is the actual synth provider, but is just a wrapper that checks +# whether valobj is an instance of __NSArrayI or __NSArrayM and sets up an +# appropriate backend layer to do the computations +class NSArray_SynthProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, dict): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.adjust_for_architecture() + self.error = False + self.wrapper = self.make_wrapper() + self.invalid = (self.wrapper == None) + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + if self.wrapper == None: + return 0; + return self.wrapper.num_children() + + def update(self): + logger = lldb.formatters.Logger.Logger() + if self.wrapper == None: + return + self.wrapper.update() + + # this code acts as our defense against NULL and uninitialized + # NSArray pointers, which makes it much longer than it would be otherwise + def make_wrapper(self): + logger = lldb.formatters.Logger.Logger() + if self.valobj.GetValueAsUnsigned() == 0: + self.error = True + return lldb.runtime.objc.objc_runtime.InvalidPointer_Description(True) + else: + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(self.valobj,statistics) + if wrapper: + self.error = True + return wrapper + + name_string = class_data.class_name() + + logger >> "Class name is " + str(name_string) + + if name_string == '__NSArrayI': + wrapper = NSArrayI_SynthProvider(self.valobj, dict, class_data.sys_params) + statistics.metric_hit('code_notrun',self.valobj.GetName()) + elif name_string == '__NSArrayM': + wrapper = NSArrayM_SynthProvider(self.valobj, dict, class_data.sys_params) + statistics.metric_hit('code_notrun',self.valobj.GetName()) + elif name_string == '__NSCFArray': + wrapper = NSArrayCF_SynthProvider(self.valobj, dict, class_data.sys_params) + statistics.metric_hit('code_notrun',self.valobj.GetName()) + else: + wrapper = NSArrayKVC_SynthProvider(self.valobj, dict, class_data.sys_params) + statistics.metric_hit('unknown_class',str(self.valobj.GetName()) + " seen as " + name_string) + return wrapper; + +def CFArray_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = NSArray_SynthProvider(valobj,dict); + if provider.invalid == False: + if provider.error == True: + return provider.wrapper.message() + try: + summary = int(provider.num_children()); + except: + summary = None + logger >> "provider gave me " + str(summary) + if summary == None: + summary = '<variable is not NSArray>' + elif isinstance(summary,basestring): + pass + else: + # we format it like it were a CFString to make it look the same as the summary from Xcode + summary = '@"' + str(summary) + (" objects" if summary != 1 else " object") + '"' + return summary + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F CFArray.CFArray_SummaryProvider NSArray CFArrayRef CFMutableArrayRef") diff --git a/examples/summaries/cocoa/CFBag.py b/examples/summaries/cocoa/CFBag.py new file mode 100644 index 000000000000..37d14a432d52 --- /dev/null +++ b/examples/summaries/cocoa/CFBag.py @@ -0,0 +1,146 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for CFBag +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but the length for an CFBag, so they need not +# obey the interface specification for synthetic children providers +class CFBagRef_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # 12 bytes on i386 + # 20 bytes on x64 + # most probably 2 pointers and 4 bytes of data + def offset(self): + logger = lldb.formatters.Logger.Logger() + if self.sys_params.is_64_bit: + return 20 + else: + return 12 + + def length(self): + logger = lldb.formatters.Logger.Logger() + size = self.valobj.CreateChildAtOffset("count", + self.offset(), + self.sys_params.types_cache.NSUInteger) + return size.GetValueAsUnsigned(0) + + +class CFBagUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def length(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + num_children_vo = self.valobj.CreateValueFromExpression("count","(int)CFBagGetCount(" + stream.GetData() + " )") + if num_children_vo.IsValid(): + return num_children_vo.GetValueAsUnsigned(0) + return "<variable is not CFBag>" + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + actual_name = name_string + + logger >> "name string got was " + str(name_string) + " but actual name is " + str(actual_name) + + if class_data.is_cftype(): + # CFBag does not expose an actual NSWrapper type, so we have to check that this is + # an NSCFType and then check we are a pointer-to __CFBag + valobj_type = valobj.GetType() + if valobj_type.IsValid() and valobj_type.IsPointerType(): + valobj_type = valobj_type.GetPointeeType() + if valobj_type.IsValid(): + actual_name = valobj_type.GetName() + if actual_name == '__CFBag' or \ + actual_name == 'const struct __CFBag': + wrapper = CFBagRef_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + return wrapper + wrapper = CFBagUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + actual_name) + return wrapper; + +def CFBag_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.length(); + except: + summary = None + logger >> "summary got from provider: " + str(summary) + # for some reason, one needs to clear some bits for the count + # to be correct when using CF(Mutable)BagRef on x64 + # the bit mask was derived through experimentation + # (if counts start looking weird, then most probably + # the mask needs to be changed) + if summary == None: + summary = '<variable is not CFBag>' + elif isinstance(summary,basestring): + pass + else: + if provider.sys_params.is_64_bit: + summary = summary & ~0x1fff000000000000 + if summary == 1: + summary = '@"1 value"' + else: + summary = '@"' + str(summary) + ' values"' + return summary + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F CFBag.CFBag_SummaryProvider CFBagRef CFMutableBagRef") diff --git a/examples/summaries/cocoa/CFBinaryHeap.py b/examples/summaries/cocoa/CFBinaryHeap.py new file mode 100644 index 000000000000..2348a8971815 --- /dev/null +++ b/examples/summaries/cocoa/CFBinaryHeap.py @@ -0,0 +1,142 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for CFBinaryHeap +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but the length for an CFBinaryHeap, so they need not +# obey the interface specification for synthetic children providers +class CFBinaryHeapRef_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # 8 bytes on i386 + # 16 bytes on x64 + # most probably 2 pointers + def offset(self): + logger = lldb.formatters.Logger.Logger() + return 2 * self.sys_params.pointer_size + + def length(self): + logger = lldb.formatters.Logger.Logger() + size = self.valobj.CreateChildAtOffset("count", + self.offset(), + self.sys_params.types_cache.NSUInteger) + return size.GetValueAsUnsigned(0) + + +class CFBinaryHeapUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def length(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + num_children_vo = self.valobj.CreateValueFromExpression("count","(int)CFBinaryHeapGetCount(" + stream.GetData() + " )"); + if num_children_vo.IsValid(): + return num_children_vo.GetValueAsUnsigned(0) + return '<variable is not CFBinaryHeap>' + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + actual_name = class_data.class_name() + + logger >> "name string got was " + str(name_string) + " but actual name is " + str(actual_name) + + if class_data.is_cftype(): + # CFBinaryHeap does not expose an actual NSWrapper type, so we have to check that this is + # an NSCFType and then check we are a pointer-to CFBinaryHeap + valobj_type = valobj.GetType() + if valobj_type.IsValid() and valobj_type.IsPointerType(): + valobj_type = valobj_type.GetPointeeType() + if valobj_type.IsValid(): + actual_name = valobj_type.GetName() + if actual_name == '__CFBinaryHeap': + wrapper = CFBinaryHeapRef_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + return wrapper + wrapper = CFBinaryHeapUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + +def CFBinaryHeap_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.length(); + except: + summary = None + logger >> "summary got from provider: " + str(summary) + # for some reason, one needs to clear some bits for the count + # to be correct when using CF(Mutable)BagRef on x64 + # the bit mask was derived through experimentation + # (if counts start looking weird, then most probably + # the mask needs to be changed) + if summary == None: + summary = '<variable is not CFBinaryHeap>' + elif isinstance(summary,basestring): + pass + else: + if provider.sys_params.is_64_bit: + summary = summary & ~0x1fff000000000000 + if summary == 1: + return '@"1 item"' + else: + summary = '@"' + str(summary) + ' items"' + return summary + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F CFBinaryHeap.CFBinaryHeap_SummaryProvider CFBinaryHeapRef") diff --git a/examples/summaries/cocoa/CFBitVector.py b/examples/summaries/cocoa/CFBitVector.py new file mode 100644 index 000000000000..b0c9e7912106 --- /dev/null +++ b/examples/summaries/cocoa/CFBitVector.py @@ -0,0 +1,175 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# summary provider for CF(Mutable)BitVector +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import lldb.formatters.Logger + +# first define some utility functions +def byte_index(abs_pos): + logger = lldb.formatters.Logger.Logger() + return abs_pos/8 + +def bit_index(abs_pos): + logger = lldb.formatters.Logger.Logger() + return abs_pos & 7 + +def get_bit(byte,index): + logger = lldb.formatters.Logger.Logger() + if index < 0 or index > 7: + return None + return (byte >> (7-index)) & 1 + +def grab_array_item_data(pointer,index): + logger = lldb.formatters.Logger.Logger() + return pointer.GetPointeeData(index,1) + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but a summary for a CF*BitVector, so they need not +# obey the interface specification for synthetic children providers +class CFBitVectorKnown_SummaryProvider: + def adjust_for_architecture(self): + logger = lldb.formatters.Logger.Logger() + self.uiint_size = self.sys_params.types_cache.NSUInteger.GetByteSize() + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + if not(self.sys_params.types_cache.charptr): + self.sys_params.types_cache.charptr = self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar).GetPointerType() + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # we skip the CFRuntimeBase + # then the next CFIndex is the count + # then we skip another CFIndex and then we get at a byte array + # that wraps the individual bits + + def contents(self): + logger = lldb.formatters.Logger.Logger() + count_vo = self.valobj.CreateChildAtOffset("count",self.sys_params.cfruntime_size, + self.sys_params.types_cache.NSUInteger) + count = count_vo.GetValueAsUnsigned(0) + if count == 0: + return '(empty)' + + array_vo = self.valobj.CreateChildAtOffset("data", + self.sys_params.cfruntime_size+2*self.uiint_size, + self.sys_params.types_cache.charptr) + + data_list = [] + cur_byte_pos = None + for i in range(0,count): + if cur_byte_pos == None: + cur_byte_pos = byte_index(i) + cur_byte = grab_array_item_data(array_vo,cur_byte_pos) + cur_byte_val = cur_byte.uint8[0] + else: + byte_pos = byte_index(i) + # do not fetch the pointee data every single time through + if byte_pos != cur_byte_pos: + cur_byte_pos = byte_pos + cur_byte = grab_array_item_data(array_vo,cur_byte_pos) + cur_byte_val = cur_byte.uint8[0] + bit = get_bit(cur_byte_val,bit_index(i)) + if (i % 4) == 0: + data_list.append(' ') + if bit == 1: + data_list.append('1') + else: + data_list.append('0') + return ''.join(data_list) + + +class CFBitVectorUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def contents(self): + logger = lldb.formatters.Logger.Logger() + return '<unable to summarize this CFBitVector>' + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + actual_name = name_string + + logger >> "name string got was " + str(name_string) + " but actual name is " + str(actual_name) + + if class_data.is_cftype(): + # CFBitVectorRef does not expose an actual NSWrapper type, so we have to check that this is + # an NSCFType and then check we are a pointer-to CFBitVectorRef + valobj_type = valobj.GetType() + if valobj_type.IsValid() and valobj_type.IsPointerType(): + valobj_type = valobj_type.GetPointeeType() + if valobj_type.IsValid(): + actual_name = valobj_type.GetName() + if actual_name == '__CFBitVector' or actual_name == '__CFMutableBitVector': + wrapper = CFBitVectorKnown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = CFBitVectorUnknown_SummaryProvider(valobj, class_data.sys_params) + print actual_name + else: + wrapper = CFBitVectorUnknown_SummaryProvider(valobj, class_data.sys_params) + print name_string + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + +def CFBitVector_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.contents(); + except: + summary = None + logger >> "summary got from provider: " + str(summary) + if summary == None or summary == '': + summary = '<variable is not CFBitVector>' + return summary + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F CFBitVector.CFBitVector_SummaryProvider CFBitVectorRef CFMutableBitVectorRef") diff --git a/examples/summaries/cocoa/CFDictionary.py b/examples/summaries/cocoa/CFDictionary.py new file mode 100644 index 000000000000..061f5c56f9d6 --- /dev/null +++ b/examples/summaries/cocoa/CFDictionary.py @@ -0,0 +1,234 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for NSDictionary +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but the count for an NSDictionary, so they need not +# obey the interface specification for synthetic children providers +class NSCFDictionary_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # empirically determined on both 32 and 64bit desktop Mac OS X + # probably boils down to 2 pointers and 4 bytes of data, but + # the description of __CFDictionary is not readily available so most + # of this is guesswork, plain and simple + def offset(self): + logger = lldb.formatters.Logger.Logger() + if self.sys_params.is_64_bit: + return 20 + else: + return 12 + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + num_children_vo = self.valobj.CreateChildAtOffset("count", + self.offset(), + self.sys_params.types_cache.NSUInteger) + return num_children_vo.GetValueAsUnsigned(0) + + +class NSDictionaryI_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # we just need to skip the ISA and the count immediately follows + def offset(self): + logger = lldb.formatters.Logger.Logger() + return self.sys_params.pointer_size + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + num_children_vo = self.valobj.CreateChildAtOffset("count", + self.offset(), + self.sys_params.types_cache.NSUInteger) + value = num_children_vo.GetValueAsUnsigned(0) + if value != None: + # the MS6bits on immutable dictionaries seem to be taken by the LSB of capacity + # not sure if it is a bug or some weird sort of feature, but masking that out + # gets the count right + if self.sys_params.is_64_bit: + value = value & ~0xFC00000000000000 + else: + value = value & ~0xFC000000 + return value + +class NSDictionaryM_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # we just need to skip the ISA and the count immediately follows + def offset(self): + return self.sys_params.pointer_size + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + num_children_vo = self.valobj.CreateChildAtOffset("count", + self.offset(), + self.sys_params.types_cache.NSUInteger) + value = num_children_vo.GetValueAsUnsigned(0) + if value != None: + # the MS6bits on immutable dictionaries seem to be taken by the LSB of capacity + # not sure if it is a bug or some weird sort of feature, but masking that out + # gets the count right + if self.sys_params.is_64_bit: + value = value & ~0xFC00000000000000 + else: + value = value & ~0xFC000000 + return value + +class NSDictionaryUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + num_children_vo = self.valobj.CreateValueFromExpression("count","(int)[" + stream.GetData() + " count]"); + if num_children_vo.IsValid(): + return num_children_vo.GetValueAsUnsigned(0) + return '<variable is not NSDictionary>' + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + + logger >> "class name is: " + str(name_string) + + if name_string == '__NSCFDictionary': + wrapper = NSCFDictionary_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + elif name_string == '__NSDictionaryI': + wrapper = NSDictionaryI_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + elif name_string == '__NSDictionaryM': + wrapper = NSDictionaryM_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSDictionaryUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + +def CFDictionary_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.num_children(); + except: + summary = None + logger >> "got summary " + str(summary) + if summary == None: + return '<variable is not NSDictionary>' + if isinstance(summary,basestring): + return summary + return str(summary) + (" key/value pairs" if summary != 1 else " key/value pair") + return 'Summary Unavailable' + +def CFDictionary_SummaryProvider2 (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.num_children(); + except: + summary = None + logger >> "got summary " + str(summary) + if summary == None: + summary = '<variable is not CFDictionary>' + if isinstance(summary,basestring): + return summary + else: + # needed on OSX Mountain Lion + if provider.sys_params.is_64_bit: + summary = summary & ~0x0f1f000000000000 + summary = '@"' + str(summary) + (' entries"' if summary != 1 else ' entry"') + return summary + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F CFDictionary.CFDictionary_SummaryProvider NSDictionary") + debugger.HandleCommand("type summary add -F CFDictionary.CFDictionary_SummaryProvider2 CFDictionaryRef CFMutableDictionaryRef") diff --git a/examples/summaries/cocoa/CFString.py b/examples/summaries/cocoa/CFString.py new file mode 100644 index 000000000000..570fd8280e0c --- /dev/null +++ b/examples/summaries/cocoa/CFString.py @@ -0,0 +1,325 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example synthetic children and summary provider for CFString (and related NSString class) +# the real code is part of the LLDB core +import lldb +import lldb.runtime.objc.objc_runtime +import lldb.formatters.Logger + +def CFString_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = CFStringSynthProvider(valobj,dict); + if provider.invalid == False: + try: + summary = provider.get_child_at_index(provider.get_child_index("content")) + if type(summary) == lldb.SBValue: + summary = summary.GetSummary() + else: + summary = '"' + summary + '"' + except: + summary = None + if summary == None: + summary = '<variable is not NSString>' + return '@'+summary + return '' + +def CFAttributedString_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + offset = valobj.GetTarget().GetProcess().GetAddressByteSize() + pointee = valobj.GetValueAsUnsigned(0) + summary = '<variable is not NSAttributedString>' + if pointee != None and pointee != 0: + pointee = pointee + offset + child_ptr = valobj.CreateValueFromAddress("string_ptr",pointee,valobj.GetType()) + child = child_ptr.CreateValueFromAddress("string_data",child_ptr.GetValueAsUnsigned(),valobj.GetType()).AddressOf() + provider = CFStringSynthProvider(child,dict); + if provider.invalid == False: + try: + summary = provider.get_child_at_index(provider.get_child_index("content")).GetSummary(); + except: + summary = '<variable is not NSAttributedString>' + if summary == None: + summary = '<variable is not NSAttributedString>' + return '@'+summary + + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F CFString.CFString_SummaryProvider NSString CFStringRef CFMutableStringRef") + debugger.HandleCommand("type summary add -F CFString.CFAttributedString_SummaryProvider NSAttributedString") + +class CFStringSynthProvider: + def __init__(self,valobj,dict): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.update() + + # children other than "content" are for debugging only and must not be used in production code + def num_children(self): + logger = lldb.formatters.Logger.Logger() + if self.invalid: + return 0; + return 6; + + def read_unicode(self, pointer,max_len=2048): + logger = lldb.formatters.Logger.Logger() + process = self.valobj.GetTarget().GetProcess() + error = lldb.SBError() + pystr = u'' + # cannot do the read at once because the length value has + # a weird encoding. better play it safe here + while max_len > 0: + content = process.ReadMemory(pointer, 2, error) + new_bytes = bytearray(content) + b0 = new_bytes[0] + b1 = new_bytes[1] + pointer = pointer + 2 + if b0 == 0 and b1 == 0: + break + # rearrange bytes depending on endianness + # (do we really need this or is Cocoa going to + # use Windows-compatible little-endian even + # if the target is big endian?) + if self.is_little: + value = b1 * 256 + b0 + else: + value = b0 * 256 + b1 + pystr = pystr + unichr(value) + # read max_len unicode values, not max_len bytes + max_len = max_len - 1 + return pystr + + # handle the special case strings + # only use the custom code for the tested LP64 case + def handle_special(self): + logger = lldb.formatters.Logger.Logger() + if self.is_64_bit == False: + # for 32bit targets, use safe ObjC code + return self.handle_unicode_string_safe() + offset = 12 + pointer = self.valobj.GetValueAsUnsigned(0) + offset + pystr = self.read_unicode(pointer) + return self.valobj.CreateValueFromExpression("content", + "(char*)\"" + pystr.encode('utf-8') + "\"") + + # last resort call, use ObjC code to read; the final aim is to + # be able to strip this call away entirely and only do the read + # ourselves + def handle_unicode_string_safe(self): + return self.valobj.CreateValueFromExpression("content", + "(char*)\"" + self.valobj.GetObjectDescription() + "\""); + + def handle_unicode_string(self): + logger = lldb.formatters.Logger.Logger() + # step 1: find offset + if self.inline: + pointer = self.valobj.GetValueAsUnsigned(0) + self.size_of_cfruntime_base(); + if self.explicit == False: + # untested, use the safe code path + return self.handle_unicode_string_safe(); + else: + # a full pointer is skipped here before getting to the live data + pointer = pointer + self.pointer_size + else: + pointer = self.valobj.GetValueAsUnsigned(0) + self.size_of_cfruntime_base() + # read 8 bytes here and make an address out of them + try: + char_type = self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar).GetPointerType() + vopointer = self.valobj.CreateValueFromAddress("dummy",pointer,char_type); + pointer = vopointer.GetValueAsUnsigned(0) + except: + return self.valobj.CreateValueFromExpression("content", + '(char*)"@\"invalid NSString\""') + # step 2: read Unicode data at pointer + pystr = self.read_unicode(pointer) + # step 3: return it + return pystr.encode('utf-8') + + def handle_inline_explicit(self): + logger = lldb.formatters.Logger.Logger() + offset = 3*self.pointer_size + offset = offset + self.valobj.GetValueAsUnsigned(0) + return self.valobj.CreateValueFromExpression("content", + "(char*)(" + str(offset) + ")") + + def handle_mutable_string(self): + logger = lldb.formatters.Logger.Logger() + offset = 2 * self.pointer_size + data = self.valobj.CreateChildAtOffset("content", + offset, self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar).GetPointerType()); + data_value = data.GetValueAsUnsigned(0) + if self.explicit and self.unicode: + return self.read_unicode(data_value).encode('utf-8') + else: + data_value = data_value + 1 + return self.valobj.CreateValueFromExpression("content", "(char*)(" + str(data_value) + ")") + + def handle_UTF8_inline(self): + logger = lldb.formatters.Logger.Logger() + offset = self.valobj.GetValueAsUnsigned(0) + self.size_of_cfruntime_base(); + if self.explicit == False: + offset = offset + 1; + return self.valobj.CreateValueFromAddress("content", + offset, self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar)).AddressOf(); + + def handle_UTF8_not_inline(self): + logger = lldb.formatters.Logger.Logger() + offset = self.size_of_cfruntime_base(); + return self.valobj.CreateChildAtOffset("content", + offset,self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar).GetPointerType()); + + def get_child_at_index(self,index): + logger = lldb.formatters.Logger.Logger() + logger >> "Querying for child [" + str(index) + "]" + if index == 0: + return self.valobj.CreateValueFromExpression("mutable", + str(int(self.mutable))); + if index == 1: + return self.valobj.CreateValueFromExpression("inline", + str(int(self.inline))); + if index == 2: + return self.valobj.CreateValueFromExpression("explicit", + str(int(self.explicit))); + if index == 3: + return self.valobj.CreateValueFromExpression("unicode", + str(int(self.unicode))); + if index == 4: + return self.valobj.CreateValueFromExpression("special", + str(int(self.special))); + if index == 5: + # we are handling the several possible combinations of flags. + # for each known combination we have a function that knows how to + # go fetch the data from memory instead of running code. if a string is not + # correctly displayed, one should start by finding a combination of flags that + # makes it different from these known cases, and provide a new reader function + # if this is not possible, a new flag might have to be made up (like the "special" flag + # below, which is not a real flag in CFString), or alternatively one might need to use + # the ObjC runtime helper to detect the new class and deal with it accordingly + #print 'mutable = ' + str(self.mutable) + #print 'inline = ' + str(self.inline) + #print 'explicit = ' + str(self.explicit) + #print 'unicode = ' + str(self.unicode) + #print 'special = ' + str(self.special) + if self.mutable == True: + return self.handle_mutable_string() + elif self.inline == True and self.explicit == True and \ + self.unicode == False and self.special == False and \ + self.mutable == False: + return self.handle_inline_explicit() + elif self.unicode == True: + return self.handle_unicode_string(); + elif self.special == True: + return self.handle_special(); + elif self.inline == True: + return self.handle_UTF8_inline(); + else: + return self.handle_UTF8_not_inline(); + + def get_child_index(self,name): + logger = lldb.formatters.Logger.Logger() + logger >> "Querying for child ['" + str(name) + "']" + if name == "content": + return self.num_children() - 1; + if name == "mutable": + return 0; + if name == "inline": + return 1; + if name == "explicit": + return 2; + if name == "unicode": + return 3; + if name == "special": + return 4; + + # CFRuntimeBase is defined as having an additional + # 4 bytes (padding?) on LP64 architectures + # to get its size we add up sizeof(pointer)+4 + # and then add 4 more bytes if we are on a 64bit system + def size_of_cfruntime_base(self): + logger = lldb.formatters.Logger.Logger() + return self.pointer_size+4+(4 if self.is_64_bit else 0) + + # the info bits are part of the CFRuntimeBase structure + # to get at them we have to skip a uintptr_t and then get + # at the least-significant byte of a 4 byte array. If we are + # on big-endian this means going to byte 3, if we are on + # little endian (OSX & iOS), this means reading byte 0 + def offset_of_info_bits(self): + logger = lldb.formatters.Logger.Logger() + offset = self.pointer_size + if self.is_little == False: + offset = offset + 3; + return offset; + + def read_info_bits(self): + logger = lldb.formatters.Logger.Logger() + cfinfo = self.valobj.CreateChildAtOffset("cfinfo", + self.offset_of_info_bits(), + self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar)); + cfinfo.SetFormat(11) + info = cfinfo.GetValue(); + if info != None: + self.invalid = False; + return int(info,0); + else: + self.invalid = True; + return None; + + # calculating internal flag bits of the CFString object + # this stuff is defined and discussed in CFString.c + def is_mutable(self): + logger = lldb.formatters.Logger.Logger() + return (self.info_bits & 1) == 1; + + def is_inline(self): + logger = lldb.formatters.Logger.Logger() + return (self.info_bits & 0x60) == 0; + + # this flag's name is ambiguous, it turns out + # we must skip a length byte to get at the data + # when this flag is False + def has_explicit_length(self): + logger = lldb.formatters.Logger.Logger() + return (self.info_bits & (1 | 4)) != 4; + + # probably a subclass of NSString. obtained this from [str pathExtension] + # here info_bits = 0 and Unicode data at the start of the padding word + # in the long run using the isa value might be safer as a way to identify this + # instead of reading the info_bits + def is_special_case(self): + logger = lldb.formatters.Logger.Logger() + return self.info_bits == 0; + + def is_unicode(self): + logger = lldb.formatters.Logger.Logger() + return (self.info_bits & 0x10) == 0x10; + + # preparing ourselves to read into memory + # by adjusting architecture-specific info + def adjust_for_architecture(self): + logger = lldb.formatters.Logger.Logger() + self.pointer_size = self.valobj.GetTarget().GetProcess().GetAddressByteSize() + self.is_64_bit = self.pointer_size == 8 + self.is_little = self.valobj.GetTarget().GetProcess().GetByteOrder() == lldb.eByteOrderLittle + + # reading info bits out of the CFString and computing + # useful values to get at the real data + def compute_flags(self): + logger = lldb.formatters.Logger.Logger() + self.info_bits = self.read_info_bits(); + if self.info_bits == None: + return; + self.mutable = self.is_mutable(); + self.inline = self.is_inline(); + self.explicit = self.has_explicit_length(); + self.unicode = self.is_unicode(); + self.special = self.is_special_case(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + self.compute_flags(); diff --git a/examples/summaries/cocoa/Class.py b/examples/summaries/cocoa/Class.py new file mode 100644 index 000000000000..9c9dda858ac1 --- /dev/null +++ b/examples/summaries/cocoa/Class.py @@ -0,0 +1,21 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +import lldb +import lldb.runtime.objc.objc_runtime +import lldb.formatters.Logger + +def Class_Summary(valobj,dict): + logger = lldb.formatters.Logger.Logger() + runtime =lldb.runtime.objc.objc_runtime.ObjCRuntime.runtime_from_isa(valobj) + if runtime == None or not runtime.is_valid(): + return '<error: unknown Class>' + class_data = runtime.read_class_data() + if class_data == None or not class_data.is_valid(): + return '<error: unknown Class>' + return class_data.class_name() + diff --git a/examples/summaries/cocoa/Logger.py b/examples/summaries/cocoa/Logger.py new file mode 100644 index 000000000000..91d503c31210 --- /dev/null +++ b/examples/summaries/cocoa/Logger.py @@ -0,0 +1,122 @@ +from __future__ import print_function +import sys +import os.path +import inspect + +class NopLogger: + def __init__(self): + pass + + def write(self,data): + pass + + def flush(self): + pass + + def close(self): + pass + + +class StdoutLogger: + def __init__(self): + pass + + def write(self,data): + print(data) + + def flush(self): + pass + + def close(self): + pass + +class FileLogger: + def __init__(self, name): + self.file = None + try: + name = os.path.abspath(name) + self.file = open(name,'a') + except: + try: + self.file = open('formatters.log','a') + except: + pass + + def write(self,data): + if self.file != None: + print(data,file=self.file) + else: + print(data) + + def flush(self): + if self.file != None: + self.file.flush() + + def close(self): + if self.file != None: + self.file.close() + self.file = None + +# to enable logging: +# define lldb.formatters.Logger._lldb_formatters_debug_level to any number greater than 0 +# if you define it to any value greater than 1, the log will be automatically flushed after each write (slower but should make sure most of the stuff makes it to the log even if we crash) +# if you define it to any value greater than 2, the calling function's details will automatically be logged (even slower, but provides additional details) +# if you need the log to go to a file instead of on screen, define lldb.formatters.Logger._lldb_formatters_debug_filename to a valid filename +class Logger: + def __init__(self,autoflush=False,logcaller=False): + global _lldb_formatters_debug_level + global _lldb_formatters_debug_filename + self.autoflush = autoflush + want_log = False + try: + want_log = (_lldb_formatters_debug_level > 0) + except: + pass + if not (want_log): + self.impl = NopLogger() + return + want_file = False + try: + want_file = (_lldb_formatters_debug_filename != None and _lldb_formatters_debug_filename != '' and _lldb_formatters_debug_filename != 0) + except: + pass + if want_file: + self.impl = FileLogger(_lldb_formatters_debug_filename) + else: + self.impl = StdoutLogger() + try: + self.autoflush = (_lldb_formatters_debug_level > 1) + except: + self.autoflush = autoflush + want_caller_info = False + try: + want_caller_info = (_lldb_formatters_debug_level > 2) + except: + pass + if want_caller_info: + self._log_caller() + + def _log_caller(self): + caller = inspect.stack()[2] + try: + if caller != None and len(caller) > 3: + self.write('Logging from function ' + str(caller)) + else: + self.write('Caller info not available - Required caller logging not possible') + finally: + del caller # needed per Python docs to avoid keeping objects alive longer than we care + + def write(self,data): + self.impl.write(data) + if self.autoflush: + self.flush() + + def __rshift__(self,data): + self.write(data) + + def flush(self): + self.impl.flush() + + def close(self): + self.impl.close() + diff --git a/examples/summaries/cocoa/NSBundle.py b/examples/summaries/cocoa/NSBundle.py new file mode 100644 index 000000000000..5fd83f8e89f1 --- /dev/null +++ b/examples/summaries/cocoa/NSBundle.py @@ -0,0 +1,127 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for NSBundle +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import NSURL +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but a summary for an NSURL, so they need not +# obey the interface specification for synthetic children providers +class NSBundleKnown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSString): + self.sys_params.types_cache.NSString = self.valobj.GetTarget().FindFirstType('NSString').GetPointerType() + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # we need to skip the ISA, plus four other values + # that are luckily each a pointer in size + # which makes our computation trivial :-) + def offset(self): + logger = lldb.formatters.Logger.Logger() + return 5 * self.sys_params.pointer_size + + def url_text(self): + logger = lldb.formatters.Logger.Logger() + global statistics + text = self.valobj.CreateChildAtOffset("text", + self.offset(), + self.sys_params.types_cache.NSString) + my_string = text.GetSummary() + if (my_string == None) or (my_string == ''): + statistics.metric_hit('unknown_class',str(self.valobj.GetName()) + " triggered unknown pointer location") + return NSBundleUnknown_SummaryProvider(self.valobj, self.sys_params).url_text() + else: + statistics.metric_hit('code_notrun',self.valobj) + return my_string + + +class NSBundleUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def url_text(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + expr = "(NSString*)[" + stream.GetData() + " bundlePath]" + url_text_vo = self.valobj.CreateValueFromExpression("path",expr); + if url_text_vo.IsValid(): + return url_text_vo.GetSummary() + return '<variable is not NSBundle>' + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + logger >> "class name is: " + str(name_string) + + if name_string == 'NSBundle': + wrapper = NSBundleKnown_SummaryProvider(valobj, class_data.sys_params) + # [NSBundle mainBundle] does return an object that is + # not correctly filled out for our purposes, so we still + # end up having to run code in that case + #statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSBundleUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + +def NSBundle_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.url_text(); + except: + summary = None + logger >> "got summary " + str(summary) + if summary == None or summary == '': + summary = '<variable is not NSBundle>' + return summary + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSBundle.NSBundle_SummaryProvider NSBundle") diff --git a/examples/summaries/cocoa/NSData.py b/examples/summaries/cocoa/NSData.py new file mode 100644 index 000000000000..3aa30b29f54b --- /dev/null +++ b/examples/summaries/cocoa/NSData.py @@ -0,0 +1,163 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for NSData +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but the length for an NSData, so they need not +# obey the interface specification for synthetic children providers +class NSConcreteData_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + logger >> "NSConcreteData_SummaryProvider __init__" + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + self.adjust_for_architecture(); + + # one pointer is the ISA + # then there are 32 bit worth of flags and other data + # however, on 64bit systems these are padded to be a full + # machine word long, which means we actually have two pointers + # worth of data to skip + def offset(self): + return 2 * self.sys_params.pointer_size + + def length(self): + logger = lldb.formatters.Logger.Logger() + logger >> "NSConcreteData_SummaryProvider length" + size = self.valobj.CreateChildAtOffset("count", + self.offset(), + self.sys_params.types_cache.NSUInteger) + logger >> str(size) + logger >> str(size.GetValueAsUnsigned(0)) + return size.GetValueAsUnsigned(0) + + +class NSDataUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + logger >> "NSDataUnknown_SummaryProvider __init__" + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + self.adjust_for_architecture(); + + def length(self): + logger = lldb.formatters.Logger.Logger() + logger >> "NSDataUnknown_SummaryProvider length" + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + logger >> stream.GetData() + num_children_vo = self.valobj.CreateValueFromExpression("count","(int)[" + stream.GetData() + " length]"); + logger >> "still in after expression: " + str(num_children_vo) + if num_children_vo.IsValid(): + logger >> "wow - expr output is valid: " + str(num_children_vo.GetValueAsUnsigned()) + return num_children_vo.GetValueAsUnsigned(0) + logger >> "invalid expr output - too bad" + return '<variable is not NSData>' + + +def GetSummary_Impl(valobj): + global statistics + logger = lldb.formatters.Logger.Logger() + logger >> "NSData GetSummary_Impl" + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + logger >> "got a wrapper summary - using it" + return wrapper + + name_string = class_data.class_name() + logger >> "class name: " + name_string + if name_string == 'NSConcreteData' or \ + name_string == 'NSConcreteMutableData' or \ + name_string == '__NSCFData': + wrapper = NSConcreteData_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSDataUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + +def NSData_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + logger >> "NSData_SummaryProvider" + provider = GetSummary_Impl(valobj); + logger >> "found a summary provider, it is: " + str(provider) + if provider != None: + try: + summary = provider.length(); + except: + summary = None + logger >> "got a summary: it is " + str(summary) + if summary == None: + summary = '<variable is not NSData>' + elif isinstance(summary,basestring): + pass + else: + if summary == 1: + summary = '1 byte' + else: + summary = str(summary) + ' bytes' + return summary + return 'Summary Unavailable' + +def NSData_SummaryProvider2 (valobj,dict): + logger = lldb.formatters.Logger.Logger() + logger >> "NSData_SummaryProvider2" + provider = GetSummary_Impl(valobj); + logger >> "found a summary provider, it is: " + str(provider) + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.length(); + except: + summary = None + logger >> "got a summary: it is " + str(summary) + if summary == None: + summary = '<variable is not CFData>' + elif isinstance(summary,basestring): + pass + else: + if summary == 1: + summary = '@"1 byte"' + else: + summary = '@"' + str(summary) + ' bytes"' + return summary + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSData.NSData_SummaryProvider NSData") + debugger.HandleCommand("type summary add -F NSData.NSData_SummaryProvider2 CFDataRef CFMutableDataRef") diff --git a/examples/summaries/cocoa/NSDate.py b/examples/summaries/cocoa/NSDate.py new file mode 100644 index 000000000000..4dd63b4a5c32 --- /dev/null +++ b/examples/summaries/cocoa/NSDate.py @@ -0,0 +1,269 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for NSDate +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import struct +import time +import datetime +import CFString +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# Python promises to start counting time at midnight on Jan 1st on the epoch year +# hence, all we need to know is the epoch year +python_epoch = time.gmtime(0).tm_year + +osx_epoch = datetime.date(2001,1,1).timetuple() + +def mkgmtime(t): + logger = lldb.formatters.Logger.Logger() + return time.mktime(t)-time.timezone + +osx_epoch = mkgmtime(osx_epoch) + +def osx_to_python_time(osx): + logger = lldb.formatters.Logger.Logger() + if python_epoch <= 2001: + return osx + osx_epoch + else: + return osx - osx_epoch + +# represent a struct_time as a string in the format used by Xcode +def xcode_format_time(X): + logger = lldb.formatters.Logger.Logger() + return time.strftime('%Y-%m-%d %H:%M:%S %Z',X) + +# represent a count-since-epoch as a string in the format used by Xcode +def xcode_format_count(X): + logger = lldb.formatters.Logger.Logger() + return xcode_format_time(time.localtime(X)) + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but the summary for NSDate, so they need not +# obey the interface specification for synthetic children providers +class NSTaggedDate_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, info_bits, data, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + # NSDate is not using its info_bits for info like NSNumber is + # so we need to regroup info_bits and data + self.data = ((data << 8) | (info_bits << 4)) + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def value(self): + logger = lldb.formatters.Logger.Logger() + # the value of the date-time object is wrapped into the pointer value + # unfortunately, it is made as a time-delta after Jan 1 2001 midnight GMT + # while all Python knows about is the "epoch", which is a platform-dependent + # year (1970 of *nix) whose Jan 1 at midnight is taken as reference + value_double = struct.unpack('d', struct.pack('Q', self.data))[0] + if value_double == -63114076800.0: + return '0001-12-30 00:00:00 +0000' + return xcode_format_count(osx_to_python_time(value_double)) + + +class NSUntaggedDate_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not (self.sys_params.types_cache.double): + self.sys_params.types_cache.double = self.valobj.GetType().GetBasicType(lldb.eBasicTypeDouble) + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def offset(self): + logger = lldb.formatters.Logger.Logger() + return self.sys_params.pointer_size + + def value(self): + logger = lldb.formatters.Logger.Logger() + value = self.valobj.CreateChildAtOffset("value", + self.offset(), + self.sys_params.types_cache.double) + value_double = struct.unpack('d', struct.pack('Q', value.GetData().uint64[0]))[0] + if value_double == -63114076800.0: + return '0001-12-30 00:00:00 +0000' + return xcode_format_count(osx_to_python_time(value_double)) + +class NSCalendarDate_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not (self.sys_params.types_cache.double): + self.sys_params.types_cache.double = self.valobj.GetType().GetBasicType(lldb.eBasicTypeDouble) + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def offset(self): + logger = lldb.formatters.Logger.Logger() + return 2*self.sys_params.pointer_size + + def value(self): + logger = lldb.formatters.Logger.Logger() + value = self.valobj.CreateChildAtOffset("value", + self.offset(), + self.sys_params.types_cache.double) + value_double = struct.unpack('d', struct.pack('Q', value.GetData().uint64[0]))[0] + return xcode_format_count(osx_to_python_time(value_double)) + +class NSTimeZoneClass_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not (self.sys_params.types_cache.voidptr): + self.sys_params.types_cache.voidptr = self.valobj.GetType().GetBasicType(lldb.eBasicTypeVoid).GetPointerType() + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def offset(self): + logger = lldb.formatters.Logger.Logger() + return self.sys_params.pointer_size + + def timezone(self): + logger = lldb.formatters.Logger.Logger() + tz_string = self.valobj.CreateChildAtOffset("tz_name", + self.offset(), + self.sys_params.types_cache.voidptr) + return CFString.CFString_SummaryProvider(tz_string,None) + +class NSUnknownDate_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def value(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + expr = "(NSString*)[" + stream.GetData() + " description]" + num_children_vo = self.valobj.CreateValueFromExpression("str",expr); + if num_children_vo.IsValid(): + return num_children_vo.GetSummary() + return '<variable is not NSDate>' + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + logger >> "class name is: " + str(name_string) + + if name_string == 'NSDate' or name_string == '__NSDate' or name_string == '__NSTaggedDate': + if class_data.is_tagged(): + wrapper = NSTaggedDate_SummaryProvider(valobj,class_data.info_bits(),class_data.value(), class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSUntaggedDate_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + elif name_string == 'NSCalendarDate': + wrapper = NSCalendarDate_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + elif name_string == '__NSTimeZone': + wrapper = NSTimeZoneClass_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSUnknownDate_SummaryProvider(valobj) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + + +def NSDate_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.value(); + except: + summary = None + if summary == None: + summary = '<variable is not NSDate>' + return str(summary) + return 'Summary Unavailable' + +def NSTimeZone_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.timezone(); + except: + summary = None + logger >> "got summary " + str(summary) + if summary == None: + summary = '<variable is not NSTimeZone>' + return str(summary) + return 'Summary Unavailable' + + +def CFAbsoluteTime_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + try: + value_double = struct.unpack('d', struct.pack('Q', valobj.GetData().uint64[0]))[0] + return xcode_format_count(osx_to_python_time(value_double)) + except: + return 'Summary Unavailable' + + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSDate.NSDate_SummaryProvider NSDate") + debugger.HandleCommand("type summary add -F NSDate.CFAbsoluteTime_SummaryProvider CFAbsoluteTime") + debugger.HandleCommand("type summary add -F NSDate.NSTimeZone_SummaryProvider NSTimeZone CFTimeZoneRef") + diff --git a/examples/summaries/cocoa/NSException.py b/examples/summaries/cocoa/NSException.py new file mode 100644 index 000000000000..72bf895bdbc3 --- /dev/null +++ b/examples/summaries/cocoa/NSException.py @@ -0,0 +1,114 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# summary provider for class NSException +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import CFString +import lldb +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +class NSKnownException_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not (self.sys_params.types_cache.id): + self.sys_params.types_cache.id = self.valobj.GetType().GetBasicType(lldb.eBasicTypeObjCID) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def offset_name(self): + logger = lldb.formatters.Logger.Logger() + return self.sys_params.pointer_size + def offset_reason(self): + logger = lldb.formatters.Logger.Logger() + return 2*self.sys_params.pointer_size + + def description(self): + logger = lldb.formatters.Logger.Logger() + name_ptr = self.valobj.CreateChildAtOffset("name", + self.offset_name(), + self.sys_params.types_cache.id) + reason_ptr = self.valobj.CreateChildAtOffset("reason", + self.offset_reason(), + self.sys_params.types_cache.id) + return 'name:' + CFString.CFString_SummaryProvider(name_ptr,None) + ' reason:' + CFString.CFString_SummaryProvider(reason_ptr,None) + +class NSUnknownException_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def description(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + name_vo = self.valobj.CreateValueFromExpression("name","(NSString*)[" + stream.GetData() + " name]"); + reason_vo = self.valobj.CreateValueFromExpression("reason","(NSString*)[" + stream.GetData() + " reason]"); + if name_vo.IsValid() and reason_vo.IsValid(): + return CFString.CFString_SummaryProvider(name_vo,None) + ' ' + CFString.CFString_SummaryProvider(reason_vo,None) + return '<variable is not NSException>' + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + logger >> "class name is: " + str(name_string) + + if name_string == 'NSException': + wrapper = NSKnownException_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSUnknownException_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + +def NSException_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.description(); + except: + summary = None + logger >> "got summary " + str(summary) + if summary == None: + summary = '<variable is not NSException>' + return str(summary) + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSException.NSException_SummaryProvider NSException") diff --git a/examples/summaries/cocoa/NSIndexSet.py b/examples/summaries/cocoa/NSIndexSet.py new file mode 100644 index 000000000000..011d58dd773d --- /dev/null +++ b/examples/summaries/cocoa/NSIndexSet.py @@ -0,0 +1,150 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for NS(Mutable)IndexSet +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but the count of values for an NSIndexSet, so they need not +# obey the interface specification for synthetic children providers +class NSIndexSetClass_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + self.sys_params.types_cache.uint32 = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.sys_params.types_cache.uint32 = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + if not(self.sys_params.types_cache.uint32): + self.sys_params.types_cache.uint32 = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # NS(Mutable)IndexSet works in one of two modes: when having a compact block of data (e.g. a Range) + # the count is stored in the set itself, 3 pointers into it + # otherwise, it will store a pointer to an additional data structure (2 pointers into itself) and this + # additional structure will contain the count two pointers deep + # a bunch of flags allow us to detect an empty set, vs. a one-range set, vs. a multi-range set + def count(self): + logger = lldb.formatters.Logger.Logger() + mode_chooser_vo = self.valobj.CreateChildAtOffset("mode_chooser", + self.sys_params.pointer_size, + self.sys_params.types_cache.uint32) + mode_chooser = mode_chooser_vo.GetValueAsUnsigned(0) + if self.sys_params.is_64_bit: + mode_chooser = mode_chooser & 0x00000000FFFFFFFF + # empty set + if mode_chooser & 0x01 == 1: + return 0 + # single range + if mode_chooser & 0x02 == 2: + mode = 1 + # multi range + else: + mode = 2 + if mode == 1: + count_vo = self.valobj.CreateChildAtOffset("count", + 3*self.sys_params.pointer_size, + self.sys_params.types_cache.NSUInteger) + else: + count_ptr = self.valobj.CreateChildAtOffset("count_ptr", + 2*self.sys_params.pointer_size, + self.sys_params.types_cache.NSUInteger) + count_vo = self.valobj.CreateValueFromAddress("count", + count_ptr.GetValueAsUnsigned()+2*self.sys_params.pointer_size, + self.sys_params.types_cache.NSUInteger) + return count_vo.GetValueAsUnsigned(0) + + +class NSIndexSetUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def count(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + expr = "(int)[" + stream.GetData() + " count]" + num_children_vo = self.valobj.CreateValueFromExpression("count",expr) + if num_children_vo.IsValid(): + return num_children_vo.GetValueAsUnsigned(0) + return '<variable is not NSIndexSet>' + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + logger >> "class name is: " + str(name_string) + + if name_string == 'NSIndexSet' or name_string == 'NSMutableIndexSet': + wrapper = NSIndexSetClass_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSIndexSetUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + + +def NSIndexSet_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.count(); + except: + summary = None + logger >> "got summary " + str(summary) + if summary == None: + summary = '<variable is not NSIndexSet>' + if isinstance(summary, basestring): + return summary + else: + summary = str(summary) + (' indexes' if summary != 1 else ' index') + return summary + return 'Summary Unavailable' + + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSIndexSet.NSIndexSet_SummaryProvider NSIndexSet NSMutableIndexSet") diff --git a/examples/summaries/cocoa/NSMachPort.py b/examples/summaries/cocoa/NSMachPort.py new file mode 100644 index 000000000000..554d2ca7785a --- /dev/null +++ b/examples/summaries/cocoa/NSMachPort.py @@ -0,0 +1,123 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for NSMachPort +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but the port number of an NSMachPort, so they need not +# obey the interface specification for synthetic children providers +class NSMachPortKnown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # one pointer is the ISA + # then we have one other internal pointer, plus + # 4 bytes worth of flags. hence, these values + def offset(self): + logger = lldb.formatters.Logger.Logger() + if self.sys_params.is_64_bit: + return 20 + else: + return 12 + + def port(self): + logger = lldb.formatters.Logger.Logger() + vport = self.valobj.CreateChildAtOffset("port", + self.offset(), + self.sys_params.types_cache.NSUInteger) + return vport.GetValueAsUnsigned(0) + + +class NSMachPortUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def port(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + num_children_vo = self.valobj.CreateValueFromExpression("port","(int)[" + stream.GetData() + " machPort]") + if num_children_vo.IsValid(): + return num_children_vo.GetValueAsUnsigned(0) + return '<variable is not NSMachPort>' + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + logger >> "class name is: " + str(name_string) + + if name_string == 'NSMachPort': + wrapper = NSMachPortKnown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSMachPortUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + +def NSMachPort_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.port(); + except: + summary = None + logger >> "got summary " + str(summary) + if summary == None: + summary = '<variable is not NSMachPort>' + if isinstance(summary, basestring): + return summay + return 'mach port: ' + str(summary) + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSMachPort.NSMachPort_SummaryProvider NSMachPort") diff --git a/examples/summaries/cocoa/NSNotification.py b/examples/summaries/cocoa/NSNotification.py new file mode 100644 index 000000000000..33c20065346f --- /dev/null +++ b/examples/summaries/cocoa/NSNotification.py @@ -0,0 +1,110 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for NSNotification +# the real summary is now C++ code built into LLDB +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import CFString +import lldb +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +class NSConcreteNotification_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not (self.sys_params.types_cache.id): + self.sys_params.types_cache.id = self.valobj.GetType().GetBasicType(lldb.eBasicTypeObjCID) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # skip the ISA and go to the name pointer + def offset(self): + logger = lldb.formatters.Logger.Logger() + return self.sys_params.pointer_size + + def name(self): + logger = lldb.formatters.Logger.Logger() + string_ptr = self.valobj.CreateChildAtOffset("name", + self.offset(), + self.sys_params.types_cache.id) + return CFString.CFString_SummaryProvider(string_ptr,None) + + +class NSNotificationUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def name(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + name_vo = self.valobj.CreateValueFromExpression("name","(NSString*)[" + stream.GetData() + " name]") + if name_vo.IsValid(): + return CFString.CFString_SummaryProvider(name_vo,None) + return '<variable is not NSNotification>' + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + logger >> "class name is: " + str(name_string) + + if name_string == 'NSConcreteNotification': + wrapper = NSConcreteNotification_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSNotificationUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + +def NSNotification_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.name(); + except: + summary = None + logger >> "got summary " + str(summary) + if summary == None: + summary = '<variable is not NSNotification>' + return str(summary) + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSNotification.NSNotification_SummaryProvider NSNotification") diff --git a/examples/summaries/cocoa/NSNumber.py b/examples/summaries/cocoa/NSNumber.py new file mode 100644 index 000000000000..7edd33803f9c --- /dev/null +++ b/examples/summaries/cocoa/NSNumber.py @@ -0,0 +1,235 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# example summary provider for NSNumber +# the real summary is now C++ code built into LLDB +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import struct +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but the port number of an NSNumber, so they need not +# obey the interface specification for synthetic children providers +class NSTaggedNumber_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, info_bits, data, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.info_bits = info_bits + self.data = data + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def value(self): + logger = lldb.formatters.Logger.Logger() + # in spite of the plenty of types made available by the public NSNumber API + # only a bunch of these are actually used in the internal implementation + # unfortunately, the original type information appears to be lost + # so we try to at least recover the proper magnitude of the data + if self.info_bits == 0: + return '(char)' + str(ord(ctypes.c_char(chr(self.data % 256)).value)) + if self.info_bits == 4: + return '(short)' + str(ctypes.c_short(self.data % (256*256)).value) + if self.info_bits == 8: + return '(int)' + str(ctypes.c_int(self.data % (256*256*256*256)).value) + if self.info_bits == 12: + return '(long)' + str(ctypes.c_long(self.data).value) + else: + return 'unexpected value:(info=' + str(self.info_bits) + ", value = " + str(self.data) + ')' + + +class NSUntaggedNumber_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.char): + self.sys_params.types_cache.char = self.valobj.GetType().GetBasicType(lldb.eBasicTypeChar) + if not(self.sys_params.types_cache.short): + self.sys_params.types_cache.short = self.valobj.GetType().GetBasicType(lldb.eBasicTypeShort) + if not(self.sys_params.types_cache.ushort): + self.sys_params.types_cache.ushort = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedShort) + if not(self.sys_params.types_cache.int): + self.sys_params.types_cache.int = self.valobj.GetType().GetBasicType(lldb.eBasicTypeInt) + if not(self.sys_params.types_cache.long): + self.sys_params.types_cache.long = self.valobj.GetType().GetBasicType(lldb.eBasicTypeLong) + if not(self.sys_params.types_cache.ulong): + self.sys_params.types_cache.ulong = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + if not(self.sys_params.types_cache.longlong): + self.sys_params.types_cache.longlong = self.valobj.GetType().GetBasicType(lldb.eBasicTypeLongLong) + if not(self.sys_params.types_cache.ulonglong): + self.sys_params.types_cache.ulonglong = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLongLong) + if not(self.sys_params.types_cache.float): + self.sys_params.types_cache.float = self.valobj.GetType().GetBasicType(lldb.eBasicTypeFloat) + if not(self.sys_params.types_cache.double): + self.sys_params.types_cache.double = self.valobj.GetType().GetBasicType(lldb.eBasicTypeDouble) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def value(self): + logger = lldb.formatters.Logger.Logger() + global statistics + # we need to skip the ISA, then the next byte tells us what to read + # we then skip one other full pointer worth of data and then fetch the contents + # if we are fetching an int64 value, one more pointer must be skipped to get at our data + data_type_vo = self.valobj.CreateChildAtOffset("dt", + self.sys_params.pointer_size, + self.sys_params.types_cache.char) + data_type = ((data_type_vo.GetValueAsUnsigned(0) % 256) & 0x1F) + data_offset = 2 * self.sys_params.pointer_size + if data_type == 0B00001: + data_vo = self.valobj.CreateChildAtOffset("data", + data_offset, + self.sys_params.types_cache.char) + statistics.metric_hit('code_notrun',self.valobj) + return '(char)' + str(ord(ctypes.c_char(chr(data_vo.GetValueAsUnsigned(0))).value)) + elif data_type == 0B0010: + data_vo = self.valobj.CreateChildAtOffset("data", + data_offset, + self.sys_params.types_cache.short) + statistics.metric_hit('code_notrun',self.valobj) + return '(short)' + str(ctypes.c_short(data_vo.GetValueAsUnsigned(0) % (256*256)).value) + # IF tagged pointers are possible on 32bit+v2 runtime + # (of which the only existing instance should be iOS) + # then values of this type might be tagged + elif data_type == 0B0011: + data_vo = self.valobj.CreateChildAtOffset("data", + data_offset, + self.sys_params.types_cache.int) + statistics.metric_hit('code_notrun',self.valobj) + return '(int)' + str(ctypes.c_int(data_vo.GetValueAsUnsigned(0)% (256*256*256*256)).value) + # apparently, on is_64_bit architectures, these are the only values that will ever + # be represented by a non tagged pointers + elif data_type == 0B10001: + data_offset = data_offset + 8 # 8 is needed even if we are on 32bit + data_vo = self.valobj.CreateChildAtOffset("data", + data_offset, + self.sys_params.types_cache.longlong) + statistics.metric_hit('code_notrun',self.valobj) + return '(long)' + str(ctypes.c_long(data_vo.GetValueAsUnsigned(0)).value) + elif data_type == 0B0100: + if self.sys_params.is_64_bit: + data_offset = data_offset + self.sys_params.pointer_size + data_vo = self.valobj.CreateChildAtOffset("data", + data_offset, + self.sys_params.types_cache.longlong) + statistics.metric_hit('code_notrun',self.valobj) + return '(long)' + str(ctypes.c_long(data_vo.GetValueAsUnsigned(0)).value) + elif data_type == 0B0101: + data_vo = self.valobj.CreateChildAtOffset("data", + data_offset, + self.sys_params.types_cache.longlong) + data_plain = int(str(data_vo.GetValueAsUnsigned(0) & 0x00000000FFFFFFFF)) + packed = struct.pack('I', data_plain) + data_float = struct.unpack('f', packed)[0] + statistics.metric_hit('code_notrun',self.valobj) + return '(float)' + str(data_float) + elif data_type == 0B0110: + data_vo = self.valobj.CreateChildAtOffset("data", + data_offset, + self.sys_params.types_cache.longlong) + data_plain = data_vo.GetValueAsUnsigned(0) + data_double = struct.unpack('d', struct.pack('Q', data_plain))[0] + statistics.metric_hit('code_notrun',self.valobj) + return '(double)' + str(data_double) + statistics.metric_hit('unknown_class',str(valobj.GetName()) + " had unknown data_type " + str(data_type)) + return 'unexpected: dt = ' + str(data_type) + + +class NSUnknownNumber_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def value(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + expr = "(NSString*)[" + stream.GetData() + " stringValue]" + num_children_vo = self.valobj.CreateValueFromExpression("str",expr) + if num_children_vo.IsValid(): + return num_children_vo.GetSummary() + return '<variable is not NSNumber>' + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + logger >> "class name is: " + str(name_string) + + if name_string == 'NSNumber' or name_string == '__NSCFNumber': + if class_data.is_tagged(): + wrapper = NSTaggedNumber_SummaryProvider(valobj,class_data.info_bits(),class_data.value(), class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + # the wrapper might be unable to decipher what is into the NSNumber + # and then have to run code on it + wrapper = NSUntaggedNumber_SummaryProvider(valobj, class_data.sys_params) + else: + wrapper = NSUnknownNumber_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + + +def NSNumber_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.value(); + except Exception as foo: + print foo +# except: + summary = None + logger >> "got summary " + str(summary) + if summary == None: + summary = '<variable is not NSNumber>' + return str(summary) + return 'Summary Unavailable' + + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSNumber.NSNumber_SummaryProvider NSNumber") + debugger.HandleCommand("type summary add -F NSNumber.NSNumber_SummaryProvider __NSCFBoolean") + debugger.HandleCommand("type summary add -F NSNumber.NSNumber_SummaryProvider __NSCFNumber") + diff --git a/examples/summaries/cocoa/NSSet.py b/examples/summaries/cocoa/NSSet.py new file mode 100644 index 000000000000..71665dbc9f79 --- /dev/null +++ b/examples/summaries/cocoa/NSSet.py @@ -0,0 +1,263 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# summary provider for NSSet +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import CFBag +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but the port number of an NSMachPort, so they need not +# obey the interface specification for synthetic children providers +class NSCFSet_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # one pointer is the ISA + # then we have one other internal pointer, plus + # 4 bytes worth of flags. hence, these values + def offset(self): + logger = lldb.formatters.Logger.Logger() + if self.sys_params.is_64_bit: + return 20 + else: + return 12 + + def count(self): + logger = lldb.formatters.Logger.Logger() + vcount = self.valobj.CreateChildAtOffset("count", + self.offset(), + self.sys_params.types_cache.NSUInteger) + return vcount.GetValueAsUnsigned(0) + + +class NSSetUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def count(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + expr = "(int)[" + stream.GetData() + " count]" + num_children_vo = self.valobj.CreateValueFromExpression("count",expr) + if num_children_vo.IsValid(): + return num_children_vo.GetValueAsUnsigned(0) + return '<variable is not NSSet>' + +class NSSetI_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # we just need to skip the ISA and the count immediately follows + def offset(self): + logger = lldb.formatters.Logger.Logger() + return self.sys_params.pointer_size + + def count(self): + logger = lldb.formatters.Logger.Logger() + num_children_vo = self.valobj.CreateChildAtOffset("count", + self.offset(), + self.sys_params.types_cache.NSUInteger) + value = num_children_vo.GetValueAsUnsigned(0) + if value != None: + # the MSB on immutable sets seems to be taken by some other data + # not sure if it is a bug or some weird sort of feature, but masking it out + # gets the count right (unless, of course, someone's dictionaries grow + # too large - but I have not tested this) + if self.sys_params.is_64_bit: + value = value & ~0xFF00000000000000 + else: + value = value & ~0xFF000000 + return value + +class NSSetM_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSUInteger): + if self.sys_params.is_64_bit: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + else: + self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # we just need to skip the ISA and the count immediately follows + def offset(self): + logger = lldb.formatters.Logger.Logger() + return self.sys_params.pointer_size + + def count(self): + logger = lldb.formatters.Logger.Logger() + num_children_vo = self.valobj.CreateChildAtOffset("count", + self.offset(), + self.sys_params.types_cache.NSUInteger) + return num_children_vo.GetValueAsUnsigned(0) + + +class NSCountedSet_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not (self.sys_params.types_cache.voidptr): + self.sys_params.types_cache.voidptr = self.valobj.GetType().GetBasicType(lldb.eBasicTypeVoid).GetPointerType() + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # an NSCountedSet is implemented using a CFBag whose pointer just follows the ISA + def offset(self): + logger = lldb.formatters.Logger.Logger() + return self.sys_params.pointer_size + + def count(self): + logger = lldb.formatters.Logger.Logger() + cfbag_vo = self.valobj.CreateChildAtOffset("bag_impl", + self.offset(), + self.sys_params.types_cache.voidptr) + return CFBag.CFBagRef_SummaryProvider(cfbag_vo,self.sys_params).length() + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + logger >> "class name is: " + str(name_string) + + if name_string == '__NSCFSet': + wrapper = NSCFSet_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + elif name_string == '__NSSetI': + wrapper = NSSetI_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + elif name_string == '__NSSetM': + wrapper = NSSetM_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + elif name_string == 'NSCountedSet': + wrapper = NSCountedSet_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSSetUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + + +def NSSet_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + try: + summary = provider.count(); + except: + summary = None + if summary == None: + summary = '<variable is not NSSet>' + if isinstance(summary, basestring): + return summary + else: + summary = str(summary) + (' objects' if summary != 1 else ' object') + return summary + return 'Summary Unavailable' + +def NSSet_SummaryProvider2 (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.count(); + except: + summary = None + logger >> "got summary " + str(summary) + # for some reason, one needs to clear some bits for the count returned + # to be correct when using directly CF*SetRef as compared to NS*Set + # this only happens on 64bit, and the bit mask was derived through + # experimentation (if counts start looking weird, then most probably + # the mask needs to be changed) + if summary == None: + summary = '<variable is not CFSet>' + if isinstance(summary, basestring): + return summary + else: + if provider.sys_params.is_64_bit: + summary = summary & ~0x1fff000000000000 + summary = '@"' + str(summary) + (' values"' if summary != 1 else ' value"') + return summary + return 'Summary Unavailable' + + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSSet.NSSet_SummaryProvider NSSet") + debugger.HandleCommand("type summary add -F NSSet.NSSet_SummaryProvider2 CFSetRef CFMutableSetRef") diff --git a/examples/summaries/cocoa/NSURL.py b/examples/summaries/cocoa/NSURL.py new file mode 100644 index 000000000000..ac47be365e5a --- /dev/null +++ b/examples/summaries/cocoa/NSURL.py @@ -0,0 +1,137 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +# summary provider for NSURL +import lldb +import ctypes +import lldb.runtime.objc.objc_runtime +import lldb.formatters.metrics +import CFString +import lldb.formatters.Logger + +statistics = lldb.formatters.metrics.Metrics() +statistics.add_metric('invalid_isa') +statistics.add_metric('invalid_pointer') +statistics.add_metric('unknown_class') +statistics.add_metric('code_notrun') + +# despite the similary to synthetic children providers, these classes are not +# trying to provide anything but a summary for an NSURL, so they need not +# obey the interface specification for synthetic children providers +class NSURLKnown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + if not(self.sys_params.types_cache.NSString): + self.sys_params.types_cache.NSString = self.valobj.GetTarget().FindFirstType('NSString').GetPointerType() + if not(self.sys_params.types_cache.NSURL): + self.sys_params.types_cache.NSURL = self.valobj.GetTarget().FindFirstType('NSURL').GetPointerType() + self.update(); + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + # one pointer is the ISA + # then there is one more pointer and 8 bytes of plain data + # (which are also present on a 32-bit system) + # then there is a pointer to an NSString which is the url text + # optionally, the next pointer is another NSURL which is the "base" + # of this one when doing NSURLs composition (incidentally, NSURLs can + # recurse the base+text mechanism to any desired depth) + def offset_text(self): + logger = lldb.formatters.Logger.Logger() + return 24 if self.sys_params.is_64_bit else 16 + def offset_base(self): + logger = lldb.formatters.Logger.Logger() + return self.offset_text()+self.sys_params.pointer_size + + def url_text(self): + logger = lldb.formatters.Logger.Logger() + text = self.valobj.CreateChildAtOffset("text", + self.offset_text(), + self.sys_params.types_cache.NSString) + base = self.valobj.CreateChildAtOffset("base", + self.offset_base(), + self.sys_params.types_cache.NSURL) + my_string = CFString.CFString_SummaryProvider(text,None) + if len(my_string) > 0 and base.GetValueAsUnsigned(0) != 0: + # remove final " from myself + my_string = my_string[0:len(my_string)-1] + my_string = my_string + ' -- ' + my_base_string = NSURL_SummaryProvider(base,None) + if len(my_base_string) > 2: + # remove @" marker from base URL string + my_base_string = my_base_string[2:] + my_string = my_string + my_base_string + return my_string + + +class NSURLUnknown_SummaryProvider: + def adjust_for_architecture(self): + pass + + def __init__(self, valobj, params): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.sys_params = params + self.update() + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(); + + def url_text(self): + logger = lldb.formatters.Logger.Logger() + stream = lldb.SBStream() + self.valobj.GetExpressionPath(stream) + url_text_vo = self.valobj.CreateValueFromExpression("url","(NSString*)[" + stream.GetData() + " description]") + if url_text_vo.IsValid(): + return CFString.CFString_SummaryProvider(url_text_vo,None) + return '<variable is not NSURL>' + + +def GetSummary_Impl(valobj): + logger = lldb.formatters.Logger.Logger() + global statistics + class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(valobj,statistics) + if wrapper: + return wrapper + + name_string = class_data.class_name() + logger >> "class name is: " + str(name_string) + + if name_string == 'NSURL': + wrapper = NSURLKnown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('code_notrun',valobj) + else: + wrapper = NSURLUnknown_SummaryProvider(valobj, class_data.sys_params) + statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string) + return wrapper; + +def NSURL_SummaryProvider (valobj,dict): + logger = lldb.formatters.Logger.Logger() + provider = GetSummary_Impl(valobj); + if provider != None: + if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description): + return provider.message() + try: + summary = provider.url_text(); + except: + summary = None + logger >> "got summary " + str(summary) + if summary == None or summary == '': + summary = '<variable is not NSURL>' + return summary + return 'Summary Unavailable' + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand("type summary add -F NSURL.NSURL_SummaryProvider NSURL CFURLRef") diff --git a/examples/summaries/cocoa/Selector.py b/examples/summaries/cocoa/Selector.py new file mode 100644 index 000000000000..d0505204bf22 --- /dev/null +++ b/examples/summaries/cocoa/Selector.py @@ -0,0 +1,14 @@ +""" +LLDB AppKit formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +import lldb + +def SEL_Summary(valobj,dict): + return valobj.Cast(valobj.GetType().GetBasicType(lldb.eBasicTypeChar).GetPointerType()).GetSummary() + +def SELPointer_Summary(valobj,dict): + return valobj.CreateValueFromAddress('text',valobj.GetValueAsUnsigned(0),valobj.GetType().GetBasicType(lldb.eBasicTypeChar)).AddressOf().GetSummary() diff --git a/examples/summaries/cocoa/attrib_fromdict.py b/examples/summaries/cocoa/attrib_fromdict.py new file mode 100644 index 000000000000..86964d602b5b --- /dev/null +++ b/examples/summaries/cocoa/attrib_fromdict.py @@ -0,0 +1,38 @@ +""" +Objective-C runtime wrapper for use by LLDB Python formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +class AttributesDictionary: + def __init__(self, allow_reset = True): + self.__dict__['_dictionary'] = {} # need to do it this way to prevent endless recursion + self.__dict__['_allow_reset'] = allow_reset + + def __getattr__(self,name): + if not self._check_exists(name): + return None + value = self._dictionary[name] + return value + + def _set_impl(self,name,value): + self._dictionary[name] = value + + def _check_exists(self,name): + return name in self._dictionary + + def __setattr__(self,name,value): + if self._allow_reset: + self._set_impl(name,value) + else: + self.set_if_necessary(name,value) + + def set_if_necessary(self,name,value): + if not self._check_exists(name): + self._set_impl(name,value) + return True + return False + + def __len__(self): + return len(self._dictionary)
\ No newline at end of file diff --git a/examples/summaries/cocoa/cache.py b/examples/summaries/cocoa/cache.py new file mode 100644 index 000000000000..066829d80d4f --- /dev/null +++ b/examples/summaries/cocoa/cache.py @@ -0,0 +1,35 @@ +""" +Objective-C runtime wrapper for use by LLDB Python formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +import lldb.formatters.metrics + +class Cache: + def __init__(self): + self.data = {} + self.statistics = lldb.formatters.metrics.Metrics() + self.statistics.add_metric('hit') + self.statistics.add_metric('miss') + + def look_for_key(self,key): + if key in self.data: + return True + return False + + def add_item(self,key,value,ok_to_replace=True): + if not(ok_to_replace) and self.look_for_key(key): + return False + self.data[key] = value + return True + + def get_value(self,key,default=None): + if self.look_for_key(key): + self.statistics.metric_hit('hit',key) + return self.data[key] + else: + self.statistics.metric_hit('miss',key) + return default + diff --git a/examples/summaries/cocoa/metrics.py b/examples/summaries/cocoa/metrics.py new file mode 100644 index 000000000000..6b82ff3b3015 --- /dev/null +++ b/examples/summaries/cocoa/metrics.py @@ -0,0 +1,94 @@ +""" +Objective-C runtime wrapper for use by LLDB Python formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +import lldb +import time, datetime +import inspect + +class TimeMetrics: + @staticmethod + def generate(label=None): + return TimeMetrics(label) + + def __init__(self,lbl=None): + self.label = "" if lbl is None else lbl + pass + + def __enter__(self): + caller = inspect.stack()[1] + self.function = str(caller) + self.enter_time = time.clock() + + def __exit__(self, a,b,c): + self.exit_time = time.clock() + print("It took " + str(self.exit_time - self.enter_time) + " time units to run through " + self.function + self.label) + return False + +class Counter: + def __init__(self): + self.count = 0 + self.list = [] + def update(self,name): + self.count = self.count + 1 + # avoid getting the full dump of this ValueObject just to save its metrics + if isinstance(name,lldb.SBValue): + self.list.append(name.GetName()) + else: + self.list.append(str(name)) + def __str__(self): + return str(self.count) + " times, for items [" + str(self.list) + "]" + +class MetricsPrinter_Verbose: + def __init__(self,metrics): + self.metrics = metrics + def __str__(self): + string = "" + for key,value in self.metrics.metrics.items(): + string = string + "metric " + str(key) + ": " + str(value) + "\n" + return string + +class MetricsPrinter_Compact: + def __init__(self,metrics): + self.metrics = metrics + def __str__(self): + string = "" + for key,value in self.metrics.metrics.items(): + string = string + "metric " + str(key) + " was hit " + str(value.count) + " times\n" + return string + +class Metrics: + def __init__(self): + self.metrics = {} + + def add_metric(self,name): + self.metrics[name] = Counter() + + def metric_hit(self,metric,trigger): + self.metrics[metric].update(trigger) + + def __getitem__(self,key): + return self.metrics[key] + + def __getattr__(self,name): + if name == 'compact': + return MetricsPrinter_Compact(self) + if name == 'verbose': + return MetricsPrinter_Verbose(self) + raise AttributeError("%r object has no attribute %r" % + (type(self).__name__, name)) + + def __str__(self): + return str(self.verbose) + + def metric_success(self,metric): + total_count = 0 + metric_count = self[metric].count + for key,value in self.metrics.items(): + total_count = total_count + value.count + if total_count > 0: + return metric_count / float(total_count) + return 0 diff --git a/examples/summaries/cocoa/objc_runtime.py b/examples/summaries/cocoa/objc_runtime.py new file mode 100644 index 000000000000..8b5debccb824 --- /dev/null +++ b/examples/summaries/cocoa/objc_runtime.py @@ -0,0 +1,781 @@ +""" +Objective-C runtime wrapper for use by LLDB Python formatters + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" +import lldb +import lldb.formatters.cache +import lldb.formatters.attrib_fromdict +import functools +import lldb.formatters.Logger + +class Utilities: + @staticmethod + def read_ascii(process, pointer,max_len=128): + logger = lldb.formatters.Logger.Logger() + error = lldb.SBError() + content = None + try: + content = process.ReadCStringFromMemory(pointer,max_len,error) + except: + pass + if content is None or len(content) == 0 or error.fail: + return None + return content + + @staticmethod + def is_valid_pointer(pointer, pointer_size, allow_tagged=0, allow_NULL=0): + logger = lldb.formatters.Logger.Logger() + if pointer is None: + return 0 + if pointer == 0: + return allow_NULL + if allow_tagged and (pointer % 2) == 1: + return 1 + return ((pointer % pointer_size) == 0) + + # Objective-C runtime has a rule that pointers in a class_t will only have bits 0 thru 46 set + # so if any pointer has bits 47 thru 63 high we know that this is not a valid isa + @staticmethod + def is_allowed_pointer(pointer): + logger = lldb.formatters.Logger.Logger() + if pointer is None: + return 0 + return ((pointer & 0xFFFF800000000000) == 0) + + @staticmethod + def read_child_of(valobj,offset,type): + logger = lldb.formatters.Logger.Logger() + if offset == 0 and type.GetByteSize() == valobj.GetByteSize(): + return valobj.GetValueAsUnsigned() + child = valobj.CreateChildAtOffset("childUNK",offset,type) + if child is None or child.IsValid() == 0: + return None; + return child.GetValueAsUnsigned() + + @staticmethod + def is_valid_identifier(name): + logger = lldb.formatters.Logger.Logger() + if name is None: + return None + if len(name) == 0: + return None + # technically, the ObjC runtime does not enforce any rules about what name a class can have + # in practice, the commonly used byte values for a class name are the letters, digits and some + # symbols: $, %, -, _, . + # WARNING: this means that you cannot use this runtime implementation if you need to deal + # with class names that use anything but what is allowed here + ok_values = dict.fromkeys("$%_.-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890") + return all(c in ok_values for c in name) + + @staticmethod + def check_is_osx_lion(target): + logger = lldb.formatters.Logger.Logger() + # assume the only thing that has a Foundation.framework is a Mac + # assume anything < Lion does not even exist + try: + mod = target.module['Foundation'] + except: + mod = None + if mod is None or mod.IsValid() == 0: + return None + ver = mod.GetVersion() + if ver is None or ver == []: + return None + return (ver[0] < 900) + + # a utility method that factors out code common to almost all the formatters + # takes in an SBValue and a metrics object + # returns a class_data and a wrapper (or None, if the runtime alone can't decide on a wrapper) + @staticmethod + def prepare_class_detection(valobj,statistics): + logger = lldb.formatters.Logger.Logger() + class_data = ObjCRuntime(valobj) + if class_data.is_valid() == 0: + statistics.metric_hit('invalid_pointer',valobj) + wrapper = InvalidPointer_Description(valobj.GetValueAsUnsigned(0) == 0) + return class_data,wrapper + class_data = class_data.read_class_data() + if class_data.is_valid() == 0: + statistics.metric_hit('invalid_isa',valobj) + wrapper = InvalidISA_Description() + return class_data,wrapper + if class_data.is_kvo(): + class_data = class_data.get_superclass() + if class_data.class_name() == '_NSZombie_OriginalClass': + wrapper = ThisIsZombie_Description() + return class_data,wrapper + return class_data,None + + +class RoT_Data: + def __init__(self,rot_pointer,params): + logger = lldb.formatters.Logger.Logger() + if (Utilities.is_valid_pointer(rot_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=0)): + self.sys_params = params + self.valobj = rot_pointer + #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t) + #self.instanceStart = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t) + self.instanceSize = None # lazy fetching + offset = 24 if self.sys_params.is_64_bit else 16 + #self.ivarLayoutPtr = Utilities.read_child_of(self.valobj,offset,self.sys_params.addr_ptr_type) + self.namePointer = Utilities.read_child_of(self.valobj,offset,self.sys_params.types_cache.addr_ptr_type) + self.valid = 1 # self.check_valid() + else: + logger >> "Marking as invalid - rot is invalid" + self.valid = 0 + if self.valid: + self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer) + if not(Utilities.is_valid_identifier(self.name)): + logger >> "Marking as invalid - name is invalid" + self.valid = 0 + + # perform sanity checks on the contents of this class_ro_t + def check_valid(self): + self.valid = 1 + # misaligned pointers seem to be possible for this field + #if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=0)): + # self.valid = 0 + # pass + + def __str__(self): + logger = lldb.formatters.Logger.Logger() + return \ + "instanceSize = " + hex(self.instance_size()) + "\n" + \ + "namePointer = " + hex(self.namePointer) + " --> " + self.name + + def is_valid(self): + return self.valid + + def instance_size(self,align=0): + logger = lldb.formatters.Logger.Logger() + if self.is_valid() == 0: + return None + if self.instanceSize is None: + self.instanceSize = Utilities.read_child_of(self.valobj,8,self.sys_params.types_cache.uint32_t) + if align: + unalign = self.instance_size(0) + if self.sys_params.is_64_bit: + return ((unalign + 7) & ~7) % 0x100000000 + else: + return ((unalign + 3) & ~3) % 0x100000000 + else: + return self.instanceSize + +class RwT_Data: + def __init__(self,rwt_pointer,params): + logger = lldb.formatters.Logger.Logger() + if (Utilities.is_valid_pointer(rwt_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=0)): + self.sys_params = params + self.valobj = rwt_pointer + #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t) + #self.version = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t) + self.roPointer = Utilities.read_child_of(self.valobj,8,self.sys_params.types_cache.addr_ptr_type) + self.check_valid() + else: + logger >> "Marking as invalid - rwt is invald" + self.valid = 0 + if self.valid: + self.rot = self.valobj.CreateValueFromData("rot",lldb.SBData.CreateDataFromUInt64Array(self.sys_params.endianness, self.sys_params.pointer_size, [self.roPointer]),self.sys_params.types_cache.addr_ptr_type) +# self.rot = self.valobj.CreateValueFromAddress("rot",self.roPointer,self.sys_params.types_cache.addr_ptr_type).AddressOf() + self.data = RoT_Data(self.rot,self.sys_params) + + # perform sanity checks on the contents of this class_rw_t + def check_valid(self): + logger = lldb.formatters.Logger.Logger() + self.valid = 1 + if not(Utilities.is_valid_pointer(self.roPointer,self.sys_params.pointer_size,allow_tagged=0)): + logger >> "Marking as invalid - ropointer is invalid" + self.valid = 0 + + def __str__(self): + logger = lldb.formatters.Logger.Logger() + return \ + "roPointer = " + hex(self.roPointer) + + def is_valid(self): + logger = lldb.formatters.Logger.Logger() + if self.valid: + return self.data.is_valid() + return 0 + +class Class_Data_V2: + def __init__(self,isa_pointer,params): + logger = lldb.formatters.Logger.Logger() + if (isa_pointer != None) and (Utilities.is_valid_pointer(isa_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=0)): + self.sys_params = params + self.valobj = isa_pointer + self.check_valid() + else: + logger >> "Marking as invalid - isa is invalid or None" + self.valid = 0 + if self.valid: + self.rwt = self.valobj.CreateValueFromData("rwt",lldb.SBData.CreateDataFromUInt64Array(self.sys_params.endianness, self.sys_params.pointer_size, [self.dataPointer]),self.sys_params.types_cache.addr_ptr_type) +# self.rwt = self.valobj.CreateValueFromAddress("rwt",self.dataPointer,self.sys_params.types_cache.addr_ptr_type).AddressOf() + self.data = RwT_Data(self.rwt,self.sys_params) + + # perform sanity checks on the contents of this class_t + # this call tries to minimize the amount of data fetched- as soon as we have "proven" + # that we have an invalid object, we stop reading + def check_valid(self): + logger = lldb.formatters.Logger.Logger() + self.valid = 1 + + self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.types_cache.addr_ptr_type) + if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=0)): + logger >> "Marking as invalid - isaPointer is invalid" + self.valid = 0 + return + if not(Utilities.is_allowed_pointer(self.isaPointer)): + logger >> "Marking as invalid - isaPointer is not allowed" + self.valid = 0 + return + + self.cachePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) + if not(Utilities.is_valid_pointer(self.cachePointer,self.sys_params.pointer_size,allow_tagged=0)): + logger >> "Marking as invalid - cachePointer is invalid" + self.valid = 0 + return + if not(Utilities.is_allowed_pointer(self.cachePointer)): + logger >> "Marking as invalid - cachePointer is not allowed" + self.valid = 0 + return + self.dataPointer = Utilities.read_child_of(self.valobj,4*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) + if not(Utilities.is_valid_pointer(self.dataPointer,self.sys_params.pointer_size,allow_tagged=0)): + logger >> "Marking as invalid - dataPointer is invalid" + self.valid = 0 + return + if not(Utilities.is_allowed_pointer(self.dataPointer)): + logger >> "Marking as invalid - dataPointer is not allowed" + self.valid = 0 + return + + self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) + if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=0, allow_NULL=1)): + logger >> "Marking as invalid - superclassIsa is invalid" + self.valid = 0 + return + if not(Utilities.is_allowed_pointer(self.superclassIsaPointer)): + logger >> "Marking as invalid - superclassIsa is not allowed" + self.valid = 0 + return + + # in general, KVO is implemented by transparently subclassing + # however, there could be exceptions where a class does something else + # internally to implement the feature - this method will have no clue that a class + # has been KVO'ed unless the standard implementation technique is used + def is_kvo(self): + logger = lldb.formatters.Logger.Logger() + if self.is_valid(): + if self.class_name().startswith("NSKVONotifying_"): + return 1 + return 0 + + # some CF classes have a valid ObjC isa in their CFRuntimeBase + # but instead of being class-specific this isa points to a match-'em-all class + # which is __NSCFType (the versions without __ also exists and we are matching to it + # just to be on the safe side) + def is_cftype(self): + logger = lldb.formatters.Logger.Logger() + if self.is_valid(): + return self.class_name() == '__NSCFType' or self.class_name() == 'NSCFType' + + def get_superclass(self): + logger = lldb.formatters.Logger.Logger() + if self.is_valid(): + parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa", + self.sys_params.pointer_size, + self.sys_params.addr_ptr_type) + return Class_Data_V2(parent_isa_pointer,self.sys_params) + else: + return None + + def class_name(self): + logger = lldb.formatters.Logger.Logger() + if self.is_valid(): + return self.data.data.name + else: + return None + + def is_valid(self): + logger = lldb.formatters.Logger.Logger() + if self.valid: + return self.data.is_valid() + return 0 + + def __str__(self): + logger = lldb.formatters.Logger.Logger() + return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \ + "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \ + "cachePointer = " + hex(self.cachePointer) + "\n" + \ + "data = " + hex(self.dataPointer) + + def is_tagged(self): + return 0 + + def instance_size(self,align=0): + logger = lldb.formatters.Logger.Logger() + if self.is_valid() == 0: + return None + return self.rwt.rot.instance_size(align) + +# runtime v1 is much less intricate than v2 and stores relevant information directly in the class_t object +class Class_Data_V1: + def __init__(self,isa_pointer,params): + logger = lldb.formatters.Logger.Logger() + if (isa_pointer != None) and (Utilities.is_valid_pointer(isa_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=0)): + self.valid = 1 + self.sys_params = params + self.valobj = isa_pointer + self.check_valid() + else: + logger >> "Marking as invalid - isaPointer is invalid or None" + self.valid = 0 + if self.valid: + self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer) + if not(Utilities.is_valid_identifier(self.name)): + logger >> "Marking as invalid - name is not valid" + self.valid = 0 + + # perform sanity checks on the contents of this class_t + def check_valid(self): + logger = lldb.formatters.Logger.Logger() + self.valid = 1 + + self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.types_cache.addr_ptr_type) + if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=0)): + logger >> "Marking as invalid - isaPointer is invalid" + self.valid = 0 + return + + self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) + if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=0,allow_NULL=1)): + logger >> "Marking as invalid - superclassIsa is invalid" + self.valid = 0 + return + + self.namePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) + #if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=0,allow_NULL=0)): + # self.valid = 0 + # return + + # in general, KVO is implemented by transparently subclassing + # however, there could be exceptions where a class does something else + # internally to implement the feature - this method will have no clue that a class + # has been KVO'ed unless the standard implementation technique is used + def is_kvo(self): + logger = lldb.formatters.Logger.Logger() + if self.is_valid(): + if self.class_name().startswith("NSKVONotifying_"): + return 1 + return 0 + + # some CF classes have a valid ObjC isa in their CFRuntimeBase + # but instead of being class-specific this isa points to a match-'em-all class + # which is __NSCFType (the versions without __ also exists and we are matching to it + # just to be on the safe side) + def is_cftype(self): + logger = lldb.formatters.Logger.Logger() + if self.is_valid(): + return self.class_name() == '__NSCFType' or self.class_name() == 'NSCFType' + + def get_superclass(self): + logger = lldb.formatters.Logger.Logger() + if self.is_valid(): + parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa", + self.sys_params.pointer_size, + self.sys_params.addr_ptr_type) + return Class_Data_V1(parent_isa_pointer,self.sys_params) + else: + return None + + def class_name(self): + logger = lldb.formatters.Logger.Logger() + if self.is_valid(): + return self.name + else: + return None + + def is_valid(self): + return self.valid + + def __str__(self): + logger = lldb.formatters.Logger.Logger() + return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \ + "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \ + "namePointer = " + hex(self.namePointer) + " --> " + self.name + \ + "instanceSize = " + hex(self.instanceSize()) + "\n" + + def is_tagged(self): + return 0 + + def instance_size(self,align=0): + logger = lldb.formatters.Logger.Logger() + if self.is_valid() == 0: + return None + if self.instanceSize is None: + self.instanceSize = Utilities.read_child_of(self.valobj,5*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) + if align: + unalign = self.instance_size(0) + if self.sys_params.is_64_bit: + return ((unalign + 7) & ~7) % 0x100000000 + else: + return ((unalign + 3) & ~3) % 0x100000000 + else: + return self.instanceSize + +# these are the only tagged pointers values for current versions +# of OSX - they might change in future OS releases, and no-one is +# advised to rely on these values, or any of the bitmasking formulas +# in TaggedClass_Data. doing otherwise is at your own risk +TaggedClass_Values_Lion = {1 : 'NSNumber', \ + 5: 'NSManagedObject', \ + 6: 'NSDate', \ + 7: 'NSDateTS' }; +TaggedClass_Values_NMOS = {0: 'NSAtom', \ + 3 : 'NSNumber', \ + 4: 'NSDateTS', \ + 5: 'NSManagedObject', \ + 6: 'NSDate' }; + +class TaggedClass_Data: + def __init__(self,pointer,params): + logger = lldb.formatters.Logger.Logger() + global TaggedClass_Values_Lion,TaggedClass_Values_NMOS + self.valid = 1 + self.name = None + self.sys_params = params + self.valobj = pointer + self.val = (pointer & ~0x0000000000000000FF) >> 8 + self.class_bits = (pointer & 0xE) >> 1 + self.i_bits = (pointer & 0xF0) >> 4 + + if self.sys_params.is_lion: + if self.class_bits in TaggedClass_Values_Lion: + self.name = TaggedClass_Values_Lion[self.class_bits] + else: + logger >> "Marking as invalid - not a good tagged pointer for Lion" + self.valid = 0 + else: + if self.class_bits in TaggedClass_Values_NMOS: + self.name = TaggedClass_Values_NMOS[self.class_bits] + else: + logger >> "Marking as invalid - not a good tagged pointer for NMOS" + self.valid = 0 + + + def is_valid(self): + return self.valid + + def class_name(self): + logger = lldb.formatters.Logger.Logger() + if self.is_valid(): + return self.name + else: + return 0 + + def value(self): + return self.val if self.is_valid() else None + + def info_bits(self): + return self.i_bits if self.is_valid() else None + + def is_kvo(self): + return 0 + + def is_cftype(self): + return 0 + + # we would need to go around looking for the superclass or ask the runtime + # for now, we seem not to require support for this operation so we will merrily + # pretend to be at a root point in the hierarchy + def get_superclass(self): + return None + + # anything that is handled here is tagged + def is_tagged(self): + return 1 + + # it seems reasonable to say that a tagged pointer is the size of a pointer + def instance_size(self,align=0): + logger = lldb.formatters.Logger.Logger() + if self.is_valid() == 0: + return None + return self.sys_params.pointer_size + + +class InvalidClass_Data: + def __init__(self): + pass + def is_valid(self): + return 0 + + +class Version: + def __init__(self, major, minor, release, build_string): + self._major = major + self._minor = minor + self._release = release + self._build_string = build_string + + def get_major(self): + return self._major + def get_minor(self): + return self._minor + def get_release(self): + return self._release + def get_build_string(self): + return self._build_string + + major = property(get_major,None) + minor = property(get_minor,None) + release = property(get_release,None) + build_string = property(get_build_string,None) + + def __lt__(self,other): + if (self.major < other.major): + return 1 + if (self.minor < other.minor): + return 1 + if (self.release < other.release): + return 1 + # build strings are not compared since they are heavily platform-dependent and might not always + # be available + return 0 + + def __eq__(self,other): + return (self.major == other.major) and \ + (self.minor == other.minor) and \ + (self.release == other.release) and \ + (self.build_string == other.build_string) + + # Python 2.6 doesn't have functools.total_ordering, so we have to implement + # other comparators + def __gt__(self, other): + return other < self + + def __le__(self, other): + return not other < self + + def __ge__(self, other): + return not self < other + + +runtime_version = lldb.formatters.cache.Cache() +os_version = lldb.formatters.cache.Cache() +types_caches = lldb.formatters.cache.Cache() +isa_caches = lldb.formatters.cache.Cache() + +class SystemParameters: + def __init__(self,valobj): + logger = lldb.formatters.Logger.Logger() + self.adjust_for_architecture(valobj) + self.adjust_for_process(valobj) + + def adjust_for_process(self, valobj): + logger = lldb.formatters.Logger.Logger() + global runtime_version + global os_version + global types_caches + global isa_caches + + process = valobj.GetTarget().GetProcess() + self.pid = process.GetUniqueID() # using the unique ID for added guarantees (see svn revision 172628 for further details) + + if runtime_version.look_for_key(self.pid): + self.runtime_version = runtime_version.get_value(self.pid) + else: + self.runtime_version = ObjCRuntime.runtime_version(process) + runtime_version.add_item(self.pid,self.runtime_version) + + if os_version.look_for_key(self.pid): + self.is_lion = os_version.get_value(self.pid) + else: + self.is_lion = Utilities.check_is_osx_lion(valobj.GetTarget()) + os_version.add_item(self.pid,self.is_lion) + + if types_caches.look_for_key(self.pid): + self.types_cache = types_caches.get_value(self.pid) + else: + self.types_cache = lldb.formatters.attrib_fromdict.AttributesDictionary(allow_reset=0) + self.types_cache.addr_type = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + self.types_cache.addr_ptr_type = self.types_cache.addr_type.GetPointerType() + self.types_cache.uint32_t = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) + types_caches.add_item(self.pid,self.types_cache) + + if isa_caches.look_for_key(self.pid): + self.isa_cache = isa_caches.get_value(self.pid) + else: + self.isa_cache = lldb.formatters.cache.Cache() + isa_caches.add_item(self.pid,self.isa_cache) + + def adjust_for_architecture(self,valobj): + process = valobj.GetTarget().GetProcess() + self.pointer_size = process.GetAddressByteSize() + self.is_64_bit = (self.pointer_size == 8) + self.endianness = process.GetByteOrder() + self.is_little = (self.endianness == lldb.eByteOrderLittle) + self.cfruntime_size = 16 if self.is_64_bit else 8 + + # a simple helper function that makes it more explicit that one is calculating + # an offset that is made up of X pointers and Y bytes of additional data + # taking into account pointer size - if you know there is going to be some padding + # you can pass that in and it will be taken into account (since padding may be different between + # 32 and 64 bit versions, you can pass padding value for both, the right one will be used) + def calculate_offset(self, num_pointers = 0, bytes_count = 0, padding32 = 0, padding64 = 0): + value = bytes_count + num_pointers*self.pointer_size + return value + padding64 if self.is_64_bit else value + padding32 + +class ObjCRuntime: + + # the ObjC runtime has no explicit "version" field that we can use + # instead, we discriminate v1 from v2 by looking for the presence + # of a well-known section only present in v1 + @staticmethod + def runtime_version(process): + logger = lldb.formatters.Logger.Logger() + if process.IsValid() == 0: + logger >> "No process - bailing out" + return None + target = process.GetTarget() + num_modules = target.GetNumModules() + module_objc = None + for idx in range(num_modules): + module = target.GetModuleAtIndex(idx) + if module.GetFileSpec().GetFilename() == 'libobjc.A.dylib': + module_objc = module + break + if module_objc is None or module_objc.IsValid() == 0: + logger >> "no libobjc - bailing out" + return None + num_sections = module.GetNumSections() + section_objc = None + for idx in range(num_sections): + section = module.GetSectionAtIndex(idx) + if section.GetName() == '__OBJC': + section_objc = section + break + if section_objc != None and section_objc.IsValid(): + logger >> "found __OBJC: v1" + return 1 + logger >> "no __OBJC: v2" + return 2 + + @staticmethod + def runtime_from_isa(isa): + logger = lldb.formatters.Logger.Logger() + runtime = ObjCRuntime(isa) + runtime.isa = isa + return runtime + + def __init__(self,valobj): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj + self.adjust_for_architecture() + self.sys_params = SystemParameters(self.valobj) + self.unsigned_value = self.valobj.GetValueAsUnsigned() + self.isa_value = None + + def adjust_for_architecture(self): + pass + +# an ObjC pointer can either be tagged or must be aligned + def is_tagged(self): + logger = lldb.formatters.Logger.Logger() + if self.valobj is None: + return 0 + return (Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=1) and \ + not(Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=0))) + + def is_valid(self): + logger = lldb.formatters.Logger.Logger() + if self.valobj is None: + return 0 + if self.valobj.IsInScope() == 0: + return 0 + return Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=1) + + def is_nil(self): + return self.unsigned_value == 0 + + def read_isa(self): + logger = lldb.formatters.Logger.Logger() + if self.isa_value != None: + logger >> "using cached isa" + return self.isa_value + self.isa_pointer = self.valobj.CreateChildAtOffset("cfisa", + 0, + self.sys_params.types_cache.addr_ptr_type) + if self.isa_pointer is None or self.isa_pointer.IsValid() == 0: + logger >> "invalid isa - bailing out" + return None; + self.isa_value = self.isa_pointer.GetValueAsUnsigned(1) + if self.isa_value == 1: + logger >> "invalid isa value - bailing out" + return None; + return Ellipsis + + def read_class_data(self): + logger = lldb.formatters.Logger.Logger() + global isa_cache + if self.is_tagged(): + # tagged pointers only exist in ObjC v2 + if self.sys_params.runtime_version == 2: + logger >> "on v2 and tagged - maybe" + # not every odd-valued pointer is actually tagged. most are just plain wrong + # we could try and predetect this before even creating a TaggedClass_Data object + # but unless performance requires it, this seems a cleaner way to tackle the task + tentative_tagged = TaggedClass_Data(self.unsigned_value,self.sys_params) + if tentative_tagged.is_valid(): + logger >> "truly tagged" + return tentative_tagged + else: + logger >> "not tagged - error" + return InvalidClass_Data() + else: + logger >> "on v1 and tagged - error" + return InvalidClass_Data() + if self.is_valid() == 0 or self.read_isa() is None: + return InvalidClass_Data() + data = self.sys_params.isa_cache.get_value(self.isa_value,default=None) + if data != None: + return data + if self.sys_params.runtime_version == 2: + data = Class_Data_V2(self.isa_pointer,self.sys_params) + else: + data = Class_Data_V1(self.isa_pointer,self.sys_params) + if data is None: + return InvalidClass_Data() + if data.is_valid(): + self.sys_params.isa_cache.add_item(self.isa_value,data,ok_to_replace=1) + return data + +# these classes below can be used by the data formatters to provide a consistent message that describes a given runtime-generated situation +class SpecialSituation_Description: + def message(self): + return '' + +class InvalidPointer_Description(SpecialSituation_Description): + + def __init__(self,nil): + self.is_nil = nil + + def message(self): + if self.is_nil: + return '@"<nil>"' + else: + return '<invalid pointer>' + +class InvalidISA_Description(SpecialSituation_Description): + + def __init__(self): + pass + + def message(self): + return '<not an Objective-C object>' + +class ThisIsZombie_Description(SpecialSituation_Description): + def message(self): + return '<freed object>'
\ No newline at end of file diff --git a/examples/summaries/essentials b/examples/summaries/essentials new file mode 100644 index 000000000000..85e87e23457e --- /dev/null +++ b/examples/summaries/essentials @@ -0,0 +1,5 @@ +type summary add -s "${var._M_dataplus._M_p}" std::string std::basic_string<char> "std::basic_string<char,std::char_traits<char>,std::allocator<char> >" +type summary add -s "\"${var%@}\"" "NSString *" +type summary add -s "${svar%#} items" -e -x std::map< +type summary add -s "${svar%#} items" -e -x std::vector< +type summary add -s "${svar%#} items" -e -x std::list< diff --git a/examples/summaries/lldb b/examples/summaries/lldb new file mode 100644 index 000000000000..b6b2bf3d41c5 --- /dev/null +++ b/examples/summaries/lldb @@ -0,0 +1,28 @@ +type summary add -w lldb lldb_private::Error -s "Type: ${var.m_type%E}, Code: ${var.m_code}, Message: ${var.m_string}" +type summary add -w lldb lldb_private::ConstString -s "${var.m_string}" +type summary add -w lldb lldb_private::Language -s "${var.m_language%E}" +type summary add -w lldb lldb_private::RegularExpression -s "${var.m_re}" +type summary add -w lldb lldb_private::UserID -s "UserID(${var.m_uid})" +type summary add -w lldb lldb_private::ValueObject -s "${var.m_name}" +type summary add -w lldb lldb_private::ValueObjectSP -s "${var.ptr_.m_name}" +type summary add -w lldb lldb_private::ValueObjectRegister -s "${var.m_reg_info.name}" +type summary add -w lldb lldb_private::ClangExpression -s "{${var.m_expr_text}}" +type summary add -w lldb lldb_private::CommandObject -s "Command name: ${var.m_cmd_name}" +type summary add -w lldb lldb_private::Variable -s "${var.m_type.m_name} ${var.m_name}" +type summary add -w lldb lldb_private::StopInfo -s "ID: ${var.m_stop_id}, ${var.m_description}" +type summary add -w lldb lldb_private::FileSpec -s "file: ${var.m_filename%S} dir: ${var.m_directory%S}" +type summary add -w -v lldb lldb::ConnectionStatus -s "[enum=${var%E} val=${var%i}]" +# Where '-v' tells type summary not to show the value itself, but just use the summary format. + +type summary add -w lldb "lldb_private::ThreadSafeValue<lldb::StateType>" -s "${var.m_value}" +type summary add -w lldb lldb_private::CompileUnit -s "file: ${var.m_filename%S} dir: ${var.m_directory%S}" +type summary add -w lldb "lldb_private::Module" -s "${var.m_file%S}" +type summary add -w lldb "lldb_private::ModuleSpec" -s "${var.m_file%S}" +type summary add -w lldb "lldb_private::ModuleList" -s "${var.m_modules%S}" +type summary add -w lldb "lldb::ModuleSP" -s "${var._M_ptr%S}" +type summary add -w lldb "lldb_private::Process" -s "Public: ${var.m_public_state%S} Private: ${var.m_private_state%S}" +type summary add -w lldb "DynamicLoaderMacOSXDYLD::DYLDImageInfo" -s "${var.file_spec%S}" + +type format add -f x lldb::addr_t + +type category enable lldb diff --git a/examples/summaries/objc.py b/examples/summaries/objc.py new file mode 100644 index 000000000000..75a4572add7a --- /dev/null +++ b/examples/summaries/objc.py @@ -0,0 +1,16 @@ +# Summaries for common ObjC types that require Python scripting +# to be generated fit into this file + +def BOOL_SummaryProvider (valobj,dict): + if not (valobj.IsValid()): + return "<invalid>" + if valobj.GetValueAsUnsigned() == 0: + return "NO" + else: + return "YES" + +def BOOLRef_SummaryProvider (valobj, dict): + return BOOL_SummaryProvider (valobj.GetChildAtIndex(0),dict) + +def BOOLPtr_SummaryProvider (valobj,dict): + return BOOL_SummaryProvider (valobj.Dereference(),dict) diff --git a/examples/summaries/pysummary.py b/examples/summaries/pysummary.py new file mode 100644 index 000000000000..71414fdaeb78 --- /dev/null +++ b/examples/summaries/pysummary.py @@ -0,0 +1,18 @@ +import lldb + +def pyobj_summary (value,unused): + if value == None or value.IsValid() == False or value.GetValueAsUnsigned(0) == 0: + return "<invalid>" + refcnt = value.GetChildMemberWithName("ob_refcnt") + expr = "(char*)PyString_AsString( (PyObject*)PyObject_Str( (PyObject*)0x%x) )" % (value.GetValueAsUnsigned(0)) + expr_summary = value.target.EvaluateExpression(expr,lldb.SBExpressionOptions()).GetSummary() + refcnt_value = "rc = %d" % (refcnt.GetValueAsUnsigned(0)) + return "%s (%s)" % (expr_summary,refcnt_value) + +def __lldb_init_module(debugger, unused): + debugger.HandleCommand("type summary add PyObject --python-function pysummary.pyobj_summary") + debugger.HandleCommand("type summary add lldb_private::PythonObject -s ${var.m_py_obj%S}") + debugger.HandleCommand("type summary add lldb_private::PythonDictionary -s ${var.m_py_obj%S}") + debugger.HandleCommand("type summary add lldb_private::PythonString -s ${var.m_py_obj%S}") + + diff --git a/examples/summaries/sp_cp.py b/examples/summaries/sp_cp.py new file mode 100644 index 000000000000..9aa0a2f4fb93 --- /dev/null +++ b/examples/summaries/sp_cp.py @@ -0,0 +1,61 @@ +""" +Summary and synthetic providers for LLDB-specific shared pointers + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" + +class SharedPtr_SyntheticChildrenProvider: + def __init__(self,valobj,dict): + self.valobj = valobj + self.update() + def update(self): + pass + def num_children(self): + return 1 + def get_child_index(self,name): + if name == "ptr": + return 0 + if name == "count": + return 1 + return None + def get_child_at_index(self,index): + if index == 0: + return self.valobj.GetChildMemberWithName('_M_ptr') + if index == 1: + return self.valobj.GetChildMemberWithName('_M_refcount').GetChildMemberWithName('_M_pi').GetChildMemberWithName('_M_use_count') + return None + +def SharedPtr_SummaryProvider (valobj,dict): + return 'use = ' + str(valobj.GetChildMemberWithName("count").GetValueAsUnsigned()) + +class ValueObjectSP_SyntheticChildrenProvider: + def __init__(self,valobj,dict): + self.valobj = valobj + self.update() + def update(self): + pass + def num_children(self): + return 1 + def get_child_index(self,name): + if name == "ptr": + return 0 + if name == "count": + return 1 + return None + def get_child_at_index(self,index): + if index == 0: + return self.valobj.GetChildMemberWithName('ptr_') + if index == 1: + return self.valobj.GetChildMemberWithName('cntrl_').GetChildMemberWithName('shared_owners_') + return None + +def ValueObjectSP_SummaryProvider (valobj,dict): + return 'use = ' + str(1 + valobj.GetChildMemberWithName("count").GetValueAsUnsigned()) + +def __lldb_init_module(debugger, dict): + debugger.HandleCommand('type summary add -x ".*ValueObjectSP" --expand -F sp_cp.ValueObjectSP_SummaryProvider') + debugger.HandleCommand('type synthetic add -x ".*ValueObjectSP" -l sp_cp.ValueObjectSP_SyntheticChildrenProvider') + debugger.HandleCommand('type summary add -x ".*SP" --expand -F sp_cp.SharedPtr_SummaryProvider') + debugger.HandleCommand('type synthetic add -x ".*SP" -l sp_cp.SharedPtr_SyntheticChildrenProvider') diff --git a/examples/summaries/unicode_strings.py b/examples/summaries/unicode_strings.py new file mode 100644 index 000000000000..319433ff3c15 --- /dev/null +++ b/examples/summaries/unicode_strings.py @@ -0,0 +1,48 @@ +""" +Example data formatters for strings represented as (pointer,length) pairs +encoded in UTF8/16/32 for use with the LLDB debugger + +To use in your projects, tweak the children names as appropriate for your data structures +and use as summaries for your data types + +part of The LLVM Compiler Infrastructure +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. +""" + +import lldb +def utf8_summary(value,unused): + pointer = value.GetChildMemberWithName("first").GetValueAsUnsigned(0) + length = value.GetChildMemberWithName("second").GetValueAsUnsigned(0) + if pointer == 0: + return False + if length == 0: + return '""' + error = lldb.SBError() + string_data = value.process.ReadMemory(pointer, length, error) + return '"%s"' % (string_data) # utf8 is safe to emit as-is on OSX + +def utf16_summary(value,unused): + pointer = value.GetChildMemberWithName("first").GetValueAsUnsigned(0) + length = value.GetChildMemberWithName("second").GetValueAsUnsigned(0) + # assume length is in bytes - if in UTF16 chars, just multiply by 2 + if pointer == 0: + return False + if length == 0: + return '""' + error = lldb.SBError() + string_data = value.process.ReadMemory(pointer, length, error) + return '"%s"' % (string_data.decode('utf-16').encode('utf-8')) # utf8 is safe to emit as-is on OSX + +def utf32_summary(value,unused): + pointer = value.GetChildMemberWithName("first").GetValueAsUnsigned(0) + length = value.GetChildMemberWithName("second").GetValueAsUnsigned(0) + # assume length is in bytes - if in UTF32 chars, just multiply by 4 + if pointer == 0: + return False + if length == 0: + return '""' + error = lldb.SBError() + string_data = value.process.ReadMemory(pointer, length, error) + return '"%s"' % (string_data.decode('utf-32').encode('utf-8')) # utf8 is safe to emit as-is on OSX + diff --git a/examples/synthetic/bitfield/example.py b/examples/synthetic/bitfield/example.py new file mode 100644 index 000000000000..7995919a4904 --- /dev/null +++ b/examples/synthetic/bitfield/example.py @@ -0,0 +1,100 @@ +# Synthetic children provider example for class MaskedData +# to use me: +# command script import ./example.py --allow-reload +# type synthetic add MaskedData --python-class example.MaskedData_SyntheticChildrenProvider +class MaskedData_SyntheticChildrenProvider: + def __init__(self, valobj, dict): + self.valobj = valobj # remember the SBValue since you will not have another chance to get it :-) + + def num_children(self): + # you could perform calculations involving the SBValue and/or its children to determine this value + # here, we have an hardcoded value - but since you have stored the SBValue you could use it to + # help figure out the correct thing to return here. if you return a number N, you should be prepared to + # answer questions about N children + return 4 + + def has_children(self): + # we simply say True here because we know we have 4 children + # in general, you want to make this calculation as simple as possible + # and return True if in doubt (you can always return num_children == 0 later) + return True + + def get_child_index(self,name): + # given a name, return its index + # you can return None if you don't know the answer for a given name + if name == "value": + return 0 + # here, we are using a reserved C++ keyword as a child name - we could not do that in the source code + # but we are free to use the names we like best in the synthetic children provider class + # we are also not respecting the order of declaration in the C++ class itself - as long as + # we are consistent, we can do that freely + if name == "operator": + return 1 + if name == "mask": + return 2 + # this member does not exist in the original class - we will compute its value and show it to the user + # when returning synthetic children, there is no need to only stick to what already exists in memory + if name == "apply()": + return 3 + return None # no clue, just say none + + def get_child_at_index(self,index): + # precautionary measures + if index < 0: + return None + if index > self.num_children(): + return None + if self.valobj.IsValid() == False: + return None + if index == 0: + return self.valobj.GetChildMemberWithName("value") + if index == 1: + # fetch the value of the operator + op_chosen = self.valobj.GetChildMemberWithName("oper").GetValueAsUnsigned() + # if it is a known value, return a descriptive string for it + # we are not doing this in the most efficient possible way, but the code is very readable + # and easy to maintain - if you change the values on the C++ side, the same changes must be made here + if op_chosen == 0: + return self.valobj.CreateValueFromExpression("operator",'(const char*)"none"') + elif op_chosen == 1: + return self.valobj.CreateValueFromExpression("operator",'(const char*)"AND"') + elif op_chosen == 2: + return self.valobj.CreateValueFromExpression("operator",'(const char*)"OR"') + elif op_chosen == 3: + return self.valobj.CreateValueFromExpression("operator",'(const char*)"XOR"') + elif op_chosen == 4: + return self.valobj.CreateValueFromExpression("operator",'(const char*)"NAND"') + elif op_chosen == 5: + return self.valobj.CreateValueFromExpression("operator",'(const char*)"NOR"') + else: + return self.valobj.CreateValueFromExpression("operator",'(const char*)"unknown"') # something else + if index == 2: + return self.valobj.GetChildMemberWithName("mask") + if index == 3: + # for this, we must fetch all the other elements + # in an efficient implementation, we would be caching this data for efficiency + value = self.valobj.GetChildMemberWithName("value").GetValueAsUnsigned() + operator = self.valobj.GetChildMemberWithName("oper").GetValueAsUnsigned() + mask = self.valobj.GetChildMemberWithName("mask").GetValueAsUnsigned() + # compute the masked value according to the operator + if operator == 1: + value = value & mask + elif operator == 2: + value = value | mask + elif operator == 3: + value = value ^ mask + elif operator == 4: + value = ~(value & mask) + elif operator == 5: + value = ~(value | mask) + else: + pass + value &= 0xFFFFFFFF # make sure Python does not extend our values to 64-bits + # return it - again, not the most efficient possible way. we should actually be pushing the computed value + # into an SBData, and using the SBData to create an SBValue - this has the advantage of readability + return self.valobj.CreateValueFromExpression("apply()",'(uint32_t)(' + str(value) + ')') + + def update(self): + # we do not do anything special in update - but this would be the right place to lookup + # the data we use in get_child_at_index and cache it + pass diff --git a/examples/synthetic/bitfield/program.cpp b/examples/synthetic/bitfield/program.cpp new file mode 100644 index 000000000000..5276824a2fb4 --- /dev/null +++ b/examples/synthetic/bitfield/program.cpp @@ -0,0 +1,74 @@ +typedef unsigned int uint32_t; + +enum MaskingOperator +{ + eMaskingOperatorDefault = 0, + eMaskingOperatorAnd = 1, + eMaskingOperatorOr = 2, + eMaskingOperatorXor = 3, + eMaskingOperatorNand = 4, + eMaskingOperatorNor = 5 +}; + +class MaskedData +{ +private: + uint32_t value; + uint32_t mask; + MaskingOperator oper; +public: + MaskedData( uint32_t V = 0, + uint32_t M = 0, + MaskingOperator P = eMaskingOperatorDefault) : + value(V), + mask(M), + oper(P) + { + } + + uint32_t apply() + { + switch(oper) + { + case eMaskingOperatorAnd: + return value & mask; + case eMaskingOperatorOr: + return value | mask; + case eMaskingOperatorXor: + return value ^ mask; + case eMaskingOperatorNand: + return ~(value & mask); + case eMaskingOperatorNor: + return ~(value | mask); + case eMaskingOperatorDefault: // fall through + default: + return value; + } + } + + void setValue(uint32_t V) + { + value = V; + } + + void setMask (uint32_t M) + { + mask = M; + } + + void setOperator(MaskingOperator P) + { + oper = P; + } +}; + +int main() +{ + MaskedData data_1(0xFF0F,0xA01F,eMaskingOperatorAnd); + MaskedData data_2(data_1.apply(),0x1AFC,eMaskingOperatorXor); + MaskedData data_3(data_2.apply(),0xFFCF,eMaskingOperatorOr); + MaskedData data_4(data_3.apply(),0xAABC,eMaskingOperatorAnd); + MaskedData data_5(data_4.apply(),0xFFAC,eMaskingOperatorNor); + MaskedData data_6(data_5.apply(),0x0000BEEF,eMaskingOperatorAnd); + return data_6.apply(); // <-- what comes out of here? +}
\ No newline at end of file diff --git a/examples/synthetic/gnu_libstdcpp.py b/examples/synthetic/gnu_libstdcpp.py new file mode 100644 index 000000000000..b6bf42235acd --- /dev/null +++ b/examples/synthetic/gnu_libstdcpp.py @@ -0,0 +1,451 @@ +import re +import lldb.formatters.Logger + +# C++ STL formatters for LLDB +# These formatters are based upon the version of the GNU libstdc++ +# as it ships with Mac OS X 10.6.8 thru 10.8.0 +# You are encouraged to look at the STL implementation for your platform +# before relying on these formatters to do the right thing for your setup + +class StdListSynthProvider: + + def __init__(self, valobj, dict): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj + self.count = None + logger >> "Providing synthetic children for a list named " + str(valobj.GetName()) + + def next_node(self,node): + logger = lldb.formatters.Logger.Logger() + return node.GetChildMemberWithName('_M_next') + + def is_valid(self,node): + logger = lldb.formatters.Logger.Logger() + valid = self.value(self.next_node(node)) != self.node_address + if valid: + logger >> "%s is valid" % str(self.valobj.GetName()) + else: + logger >> "synthetic value is not valid" + return valid + + def value(self,node): + logger = lldb.formatters.Logger.Logger() + value = node.GetValueAsUnsigned() + logger >> "synthetic value for {}: {}".format(str(self.valobj.GetName()), value) + return value + + # Floyd's cycle-finding algorithm + # try to detect if this list has a loop + def has_loop(self): + global _list_uses_loop_detector + logger = lldb.formatters.Logger.Logger() + if _list_uses_loop_detector == False: + logger >> "Asked not to use loop detection" + return False + slow = self.next + fast1 = self.next + fast2 = self.next + while self.is_valid(slow): + slow_value = self.value(slow) + fast1 = self.next_node(fast2) + fast2 = self.next_node(fast1) + if self.value(fast1) == slow_value or self.value(fast2) == slow_value: + return True + slow = self.next_node(slow) + return False + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + if self.count is None: + # libstdc++ 6.0.21 added dedicated count field. + count_child = self.node.GetChildMemberWithName('_M_data') + if count_child and count_child.IsValid(): + self.count = count_child.GetValueAsUnsigned(0) + if self.count is None: + self.count = self.num_children_impl() + return self.count + + def num_children_impl(self): + logger = lldb.formatters.Logger.Logger() + try: + next_val = self.next.GetValueAsUnsigned(0) + prev_val = self.prev.GetValueAsUnsigned(0) + # After a std::list has been initialized, both next and prev will be non-NULL + if next_val == 0 or prev_val == 0: + return 0 + if next_val == self.node_address: + return 0 + if next_val == prev_val: + return 1 + if self.has_loop(): + return 0 + size = 2 + current = self.next + while current.GetChildMemberWithName('_M_next').GetValueAsUnsigned(0) != self.node_address: + size = size + 1 + current = current.GetChildMemberWithName('_M_next') + return (size - 1) + except: + return 0; + + def get_child_index(self,name): + logger = lldb.formatters.Logger.Logger() + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self,index): + logger = lldb.formatters.Logger.Logger() + logger >> "Fetching child " + str(index) + if index < 0: + return None; + if index >= self.num_children(): + return None; + try: + offset = index + current = self.next + while offset > 0: + current = current.GetChildMemberWithName('_M_next') + offset = offset - 1 + return current.CreateChildAtOffset('['+str(index)+']',2*current.GetType().GetByteSize(),self.data_type) + except: + return None + + def extract_type(self): + logger = lldb.formatters.Logger.Logger() + list_type = self.valobj.GetType().GetUnqualifiedType() + if list_type.IsReferenceType(): + list_type = list_type.GetDereferencedType() + if list_type.GetNumberOfTemplateArguments() > 0: + data_type = list_type.GetTemplateArgumentType(0) + else: + data_type = None + return data_type + + def update(self): + logger = lldb.formatters.Logger.Logger() + # preemptively setting this to None - we might end up changing our mind later + self.count = None + try: + impl = self.valobj.GetChildMemberWithName('_M_impl') + self.node = impl.GetChildMemberWithName('_M_node') + self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0) + self.next = self.node.GetChildMemberWithName('_M_next') + self.prev = self.node.GetChildMemberWithName('_M_prev') + self.data_type = self.extract_type() + self.data_size = self.data_type.GetByteSize() + except: + pass + + def has_children(self): + return True + +class StdVectorSynthProvider: + + class StdVectorImplementation(object): + def __init__(self, valobj): + self.valobj = valobj + self.count = None + + def num_children(self): + if self.count == None: + self.count = self.num_children_impl() + return self.count + + def num_children_impl(self): + try: + start_val = self.start.GetValueAsUnsigned(0) + finish_val = self.finish.GetValueAsUnsigned(0) + end_val = self.end.GetValueAsUnsigned(0) + # Before a vector has been constructed, it will contain bad values + # so we really need to be careful about the length we return since + # uninitialized data can cause us to return a huge number. We need + # to also check for any of the start, finish or end of storage values + # being zero (NULL). If any are, then this vector has not been + # initialized yet and we should return zero + + # Make sure nothing is NULL + if start_val == 0 or finish_val == 0 or end_val == 0: + return 0 + # Make sure start is less than finish + if start_val >= finish_val: + return 0 + # Make sure finish is less than or equal to end of storage + if finish_val > end_val: + return 0 + + # if we have a struct (or other data type that the compiler pads to native word size) + # this check might fail, unless the sizeof() we get is itself incremented to take the + # padding bytes into account - on current clang it looks like this is the case + num_children = (finish_val-start_val) + if (num_children % self.data_size) != 0: + return 0 + else: + num_children = num_children/self.data_size + return num_children + except: + return 0; + + def get_child_at_index(self, index): + logger = lldb.formatters.Logger.Logger() + logger >> "Retrieving child " + str(index) + if index < 0: + return None; + if index >= self.num_children(): + return None; + try: + offset = index * self.data_size + return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type) + except: + return None + + def update(self): + # preemptively setting this to None - we might end up changing our mind later + self.count = None + try: + impl = self.valobj.GetChildMemberWithName('_M_impl') + self.start = impl.GetChildMemberWithName('_M_start') + self.finish = impl.GetChildMemberWithName('_M_finish') + self.end = impl.GetChildMemberWithName('_M_end_of_storage') + self.data_type = self.start.GetType().GetPointeeType() + self.data_size = self.data_type.GetByteSize() + # if any of these objects is invalid, it means there is no point in trying to fetch anything + if self.start.IsValid() and self.finish.IsValid() and self.end.IsValid() and self.data_type.IsValid(): + self.count = None + else: + self.count = 0 + except: + pass + return True + + class StdVBoolImplementation(object): + def __init__(self, valobj, bool_type): + self.valobj = valobj + self.bool_type = bool_type + self.valid = False + + def num_children(self): + if self.valid: + start = self.start_p.GetValueAsUnsigned(0) + finish = self.finish_p.GetValueAsUnsigned(0) + offset = self.offset.GetValueAsUnsigned(0) + if finish >= start: + return (finish - start) * 8 + offset + return 0 + + def get_child_at_index(self, index): + if index >= self.num_children(): + return None + byte_offset = index / 8 + bit_offset = index % 8 + element_size = self.start_p.GetType().GetPointeeType().GetByteSize() + data = self.start_p.GetPointeeData(byte_offset / element_size) + bit = data.GetUnsignedInt8(lldb.SBError(), byte_offset % element_size) & (1 << bit_offset) + if bit != 0: + value_expr = "(bool)true" + else: + value_expr = "(bool)false" + return self.valobj.CreateValueFromExpression("[%d]" % index, value_expr) + + def update(self): + try: + m_impl = self.valobj.GetChildMemberWithName('_M_impl') + self.m_start = m_impl.GetChildMemberWithName('_M_start') + self.m_finish = m_impl.GetChildMemberWithName('_M_finish') + self.start_p = self.m_start.GetChildMemberWithName('_M_p') + self.finish_p = self.m_finish.GetChildMemberWithName('_M_p') + self.offset = self.m_finish.GetChildMemberWithName('_M_offset') + self.valid = True + except: + self.valid = False + return True + + def __init__(self, valobj, dict): + logger = lldb.formatters.Logger.Logger() + first_template_arg_type = valobj.GetType().GetTemplateArgumentType(0) + if str(first_template_arg_type.GetName()) == "bool": + self.impl = self.StdVBoolImplementation(valobj, first_template_arg_type) + else: + self.impl = self.StdVectorImplementation(valobj) + logger >> "Providing synthetic children for a vector named " + str(valobj.GetName()) + + def num_children(self): + return self.impl.num_children() + + def get_child_index(self,name): + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self, index): + return self.impl.get_child_at_index(index) + + def update(self): + return self.impl.update() + + def has_children(self): + return True + + +class StdMapSynthProvider: + + def __init__(self, valobj, dict): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.count = None + logger >> "Providing synthetic children for a map named " + str(valobj.GetName()) + + # we need this function as a temporary workaround for rdar://problem/10801549 + # which prevents us from extracting the std::pair<K,V> SBType out of the template + # arguments for _Rep_Type _M_t in the map itself - because we have to make up the + # typename and then find it, we may hit the situation were std::string has multiple + # names but only one is actually referenced in the debug information. hence, we need + # to replace the longer versions of std::string with the shorter one in order to be able + # to find the type name + def fixup_class_name(self, class_name): + logger = lldb.formatters.Logger.Logger() + if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >': + return 'std::basic_string<char>',True + if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >': + return 'std::basic_string<char>',True + if class_name == 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >': + return 'std::basic_string<char>',True + if class_name == 'basic_string<char, std::char_traits<char>, std::allocator<char> >': + return 'std::basic_string<char>',True + return class_name,False + + def update(self): + logger = lldb.formatters.Logger.Logger() + # preemptively setting this to None - we might end up changing our mind later + self.count = None + try: + # we will set this to True if we find out that discovering a node in the map takes more steps than the overall size of the RB tree + # if this gets set to True, then we will merrily return None for any child from that moment on + self.garbage = False + self.Mt = self.valobj.GetChildMemberWithName('_M_t') + self.Mimpl = self.Mt.GetChildMemberWithName('_M_impl') + self.Mheader = self.Mimpl.GetChildMemberWithName('_M_header') + + map_type = self.valobj.GetType() + if map_type.IsReferenceType(): + logger >> "Dereferencing type" + map_type = map_type.GetDereferencedType() + + # Get the type of std::pair<key, value>. It is the first template + # argument type of the 4th template argument to std::map. + allocator_type = map_type.GetTemplateArgumentType(3) + self.data_type = allocator_type.GetTemplateArgumentType(0) + if not self.data_type: + # GCC does not emit DW_TAG_template_type_parameter for + # std::allocator<...>. For such a case, get the type of + # std::pair from a member of std::map. + rep_type = self.valobj.GetChildMemberWithName('_M_t').GetType() + self.data_type = rep_type.GetTypedefedType().GetTemplateArgumentType(1) + + # from libstdc++ implementation of _M_root for rbtree + self.Mroot = self.Mheader.GetChildMemberWithName('_M_parent') + self.data_size = self.data_type.GetByteSize() + self.skip_size = self.Mheader.GetType().GetByteSize() + except: + pass + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + if self.count == None: + self.count = self.num_children_impl() + return self.count + + def num_children_impl(self): + logger = lldb.formatters.Logger.Logger() + try: + root_ptr_val = self.node_ptr_value(self.Mroot) + if root_ptr_val == 0: + return 0; + count = self.Mimpl.GetChildMemberWithName('_M_node_count').GetValueAsUnsigned(0) + logger >> "I have " + str(count) + " children available" + return count + except: + return 0; + + def get_child_index(self,name): + logger = lldb.formatters.Logger.Logger() + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self,index): + logger = lldb.formatters.Logger.Logger() + logger >> "Being asked to fetch child[" + str(index) + "]" + if index < 0: + return None + if index >= self.num_children(): + return None; + if self.garbage: + logger >> "Returning None since we are a garbage tree" + return None + try: + offset = index + current = self.left(self.Mheader); + while offset > 0: + current = self.increment_node(current) + offset = offset - 1; + # skip all the base stuff and get at the data + return current.CreateChildAtOffset('['+str(index)+']',self.skip_size,self.data_type) + except: + return None + + # utility functions + def node_ptr_value(self,node): + logger = lldb.formatters.Logger.Logger() + return node.GetValueAsUnsigned(0) + + def right(self,node): + logger = lldb.formatters.Logger.Logger() + return node.GetChildMemberWithName("_M_right"); + + def left(self,node): + logger = lldb.formatters.Logger.Logger() + return node.GetChildMemberWithName("_M_left"); + + def parent(self,node): + logger = lldb.formatters.Logger.Logger() + return node.GetChildMemberWithName("_M_parent"); + + # from libstdc++ implementation of iterator for rbtree + def increment_node(self,node): + logger = lldb.formatters.Logger.Logger() + max_steps = self.num_children() + if self.node_ptr_value(self.right(node)) != 0: + x = self.right(node); + max_steps -= 1 + while self.node_ptr_value(self.left(x)) != 0: + x = self.left(x); + max_steps -= 1 + logger >> str(max_steps) + " more to go before giving up" + if max_steps <= 0: + self.garbage = True + return None + return x; + else: + x = node; + y = self.parent(x) + max_steps -= 1 + while(self.node_ptr_value(x) == self.node_ptr_value(self.right(y))): + x = y; + y = self.parent(y); + max_steps -= 1 + logger >> str(max_steps) + " more to go before giving up" + if max_steps <= 0: + self.garbage = True + return None + if self.node_ptr_value(self.right(x)) != self.node_ptr_value(y): + x = y; + return x; + + def has_children(self): + return True + +_list_uses_loop_detector = True diff --git a/examples/synthetic/libcxx.py b/examples/synthetic/libcxx.py new file mode 100644 index 000000000000..6623fea097c4 --- /dev/null +++ b/examples/synthetic/libcxx.py @@ -0,0 +1,787 @@ +import lldb +import lldb.formatters.Logger + +# libcxx STL formatters for LLDB +# These formatters are based upon the implementation of libc++ that +# ships with current releases of OS X - They will not work for other implementations +# of the standard C++ library - and they are bound to use the libc++-specific namespace + +# the std::string summary is just an example for your convenience +# the actual summary that LLDB uses is C++ code inside the debugger's own core + +# this could probably be made more efficient but since it only reads a handful of bytes at a time +# we probably don't need to worry too much about this for the time being +def make_string(F,L): + strval = '' + G = F.GetData().uint8 + for X in range(L): + V = G[X] + if V == 0: + break + strval = strval + chr(V % 256) + return '"' + strval + '"' + +# if we ever care about big-endian, these two functions might need to change +def is_short_string(value): + return True if (value & 1) == 0 else False +def extract_short_size(value): + return ((value >> 1) % 256) + +# some of the members of libc++ std::string are anonymous or have internal names that convey +# no external significance - we access them by index since this saves a name lookup that would add +# no information for readers of the code, but when possible try to use meaningful variable names +def stdstring_SummaryProvider(valobj,dict): + logger = lldb.formatters.Logger.Logger() + r = valobj.GetChildAtIndex(0) + B = r.GetChildAtIndex(0) + first = B.GetChildAtIndex(0) + D = first.GetChildAtIndex(0) + l = D.GetChildAtIndex(0) + s = D.GetChildAtIndex(1) + D20 = s.GetChildAtIndex(0) + size_mode = D20.GetChildAtIndex(0).GetValueAsUnsigned(0) + if is_short_string(size_mode): + size = extract_short_size(size_mode) + return make_string(s.GetChildAtIndex(1),size) + else: + data_ptr = l.GetChildAtIndex(2) + size_vo = l.GetChildAtIndex(1) + size = size_vo.GetValueAsUnsigned(0)+1 # the NULL terminator must be accounted for + if size <= 1 or size == None: # should never be the case + return '""' + try: + data = data_ptr.GetPointeeData(0,size) + except: + return '""' + error = lldb.SBError() + strval = data.GetString(error,0) + if error.Fail(): + return '<error:' + error.GetCString() + '>' + else: + return '"' + strval + '"' + +class stdvector_SynthProvider: + + def __init__(self, valobj, dict): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + + def num_children(self): + logger = lldb.formatters.Logger.Logger() + try: + start_val = self.start.GetValueAsUnsigned(0) + finish_val = self.finish.GetValueAsUnsigned(0) + # Before a vector has been constructed, it will contain bad values + # so we really need to be careful about the length we return since + # uninitialized data can cause us to return a huge number. We need + # to also check for any of the start, finish or end of storage values + # being zero (NULL). If any are, then this vector has not been + # initialized yet and we should return zero + + # Make sure nothing is NULL + if start_val == 0 or finish_val == 0: + return 0 + # Make sure start is less than finish + if start_val >= finish_val: + return 0 + + num_children = (finish_val-start_val) + if (num_children % self.data_size) != 0: + return 0 + else: + num_children = num_children/self.data_size + return num_children + except: + return 0; + + def get_child_index(self,name): + logger = lldb.formatters.Logger.Logger() + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self,index): + logger = lldb.formatters.Logger.Logger() + logger >> "Retrieving child " + str(index) + if index < 0: + return None; + if index >= self.num_children(): + return None; + try: + offset = index * self.data_size + return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type) + except: + return None + + def update(self): + logger = lldb.formatters.Logger.Logger() + try: + self.start = self.valobj.GetChildMemberWithName('__begin_') + self.finish = self.valobj.GetChildMemberWithName('__end_') + # the purpose of this field is unclear, but it is the only field whose type is clearly T* for a vector<T> + # if this ends up not being correct, we can use the APIs to get at template arguments + data_type_finder = self.valobj.GetChildMemberWithName('__end_cap_').GetChildMemberWithName('__first_') + self.data_type = data_type_finder.GetType().GetPointeeType() + self.data_size = self.data_type.GetByteSize() + except: + pass + + def has_children(self): + return True + +# Just an example: the actual summary is produced by a summary string: size=${svar%#} +def stdvector_SummaryProvider(valobj,dict): + prov = stdvector_SynthProvider(valobj,None) + return 'size=' + str(prov.num_children()) + +class stdlist_entry: + + def __init__(self,entry): + logger = lldb.formatters.Logger.Logger() + self.entry = entry + + def _next_impl(self): + logger = lldb.formatters.Logger.Logger() + return stdlist_entry(self.entry.GetChildMemberWithName('__next_')) + + def _prev_impl(self): + logger = lldb.formatters.Logger.Logger() + return stdlist_entry(self.entry.GetChildMemberWithName('__prev_')) + + def _value_impl(self): + logger = lldb.formatters.Logger.Logger() + return self.entry.GetValueAsUnsigned(0) + + def _isnull_impl(self): + logger = lldb.formatters.Logger.Logger() + return self._value_impl() == 0 + + def _sbvalue_impl(self): + logger = lldb.formatters.Logger.Logger() + return self.entry + + next = property(_next_impl,None) + value = property(_value_impl,None) + is_null = property(_isnull_impl,None) + sbvalue = property(_sbvalue_impl,None) + +class stdlist_iterator: + + def increment_node(self,node): + logger = lldb.formatters.Logger.Logger() + if node.is_null: + return None + return node.next + + def __init__(self,node): + logger = lldb.formatters.Logger.Logger() + self.node = stdlist_entry(node) # we convert the SBValue to an internal node object on entry + + def value(self): + logger = lldb.formatters.Logger.Logger() + return self.node.sbvalue # and return the SBValue back on exit + + def next(self): + logger = lldb.formatters.Logger.Logger() + node = self.increment_node(self.node) + if node != None and node.sbvalue.IsValid() and not(node.is_null): + self.node = node + return self.value() + else: + return None + + def advance(self,N): + logger = lldb.formatters.Logger.Logger() + if N < 0: + return None + if N == 0: + return self.value() + if N == 1: + return self.next() + while N > 0: + self.next() + N = N - 1 + return self.value() + + +class stdlist_SynthProvider: + def __init__(self, valobj, dict): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj + self.count = None + + def next_node(self,node): + logger = lldb.formatters.Logger.Logger() + return node.GetChildMemberWithName('__next_') + + def value(self,node): + logger = lldb.formatters.Logger.Logger() + return node.GetValueAsUnsigned() + + # Floyd's cycle-finding algorithm + # try to detect if this list has a loop + def has_loop(self): + global _list_uses_loop_detector + logger = lldb.formatters.Logger.Logger() + if _list_uses_loop_detector == False: + logger >> "Asked not to use loop detection" + return False + slow = stdlist_entry(self.head) + fast1 = stdlist_entry(self.head) + fast2 = stdlist_entry(self.head) + while slow.next.value != self.node_address: + slow_value = slow.value + fast1 = fast2.next + fast2 = fast1.next + if fast1.value == slow_value or fast2.value == slow_value: + return True + slow = slow.next + return False + + def num_children(self): + global _list_capping_size + logger = lldb.formatters.Logger.Logger() + if self.count == None: + self.count = self.num_children_impl() + if self.count > _list_capping_size: + self.count = _list_capping_size + return self.count + + def num_children_impl(self): + global _list_capping_size + logger = lldb.formatters.Logger.Logger() + try: + next_val = self.head.GetValueAsUnsigned(0) + prev_val = self.tail.GetValueAsUnsigned(0) + # After a std::list has been initialized, both next and prev will be non-NULL + if next_val == 0 or prev_val == 0: + return 0 + if next_val == self.node_address: + return 0 + if next_val == prev_val: + return 1 + if self.has_loop(): + return 0 + size = 2 + current = stdlist_entry(self.head) + while current.next.value != self.node_address: + size = size + 1 + current = current.next + if size > _list_capping_size: + return _list_capping_size + return (size - 1) + except: + return 0; + + def get_child_index(self,name): + logger = lldb.formatters.Logger.Logger() + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self,index): + logger = lldb.formatters.Logger.Logger() + logger >> "Fetching child " + str(index) + if index < 0: + return None; + if index >= self.num_children(): + return None; + try: + current = stdlist_iterator(self.head) + current = current.advance(index) + # we do not return __value_ because then all our children would be named __value_ + # we need to make a copy of __value__ with the right name - unfortunate + obj = current.GetChildMemberWithName('__value_') + obj_data = obj.GetData() + return self.valobj.CreateValueFromData('[' + str(index) + ']',obj_data,self.data_type) + except: + return None + + def extract_type(self): + logger = lldb.formatters.Logger.Logger() + list_type = self.valobj.GetType().GetUnqualifiedType() + if list_type.IsReferenceType(): + list_type = list_type.GetDereferencedType() + if list_type.GetNumberOfTemplateArguments() > 0: + data_type = list_type.GetTemplateArgumentType(0) + else: + data_type = None + return data_type + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.count = None + try: + impl = self.valobj.GetChildMemberWithName('__end_') + self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0) + self.head = impl.GetChildMemberWithName('__next_') + self.tail = impl.GetChildMemberWithName('__prev_') + self.data_type = self.extract_type() + self.data_size = self.data_type.GetByteSize() + except: + pass + + def has_children(self): + return True + + +# Just an example: the actual summary is produced by a summary string: size=${svar%#} +def stdlist_SummaryProvider(valobj,dict): + prov = stdlist_SynthProvider(valobj,None) + return 'size=' + str(prov.num_children()) + +# a tree node - this class makes the syntax in the actual iterator nicer to read and maintain +class stdmap_iterator_node: + def _left_impl(self): + logger = lldb.formatters.Logger.Logger() + return stdmap_iterator_node(self.node.GetChildMemberWithName("__left_")) + + def _right_impl(self): + logger = lldb.formatters.Logger.Logger() + return stdmap_iterator_node(self.node.GetChildMemberWithName("__right_")) + + def _parent_impl(self): + logger = lldb.formatters.Logger.Logger() + return stdmap_iterator_node(self.node.GetChildMemberWithName("__parent_")) + + def _value_impl(self): + logger = lldb.formatters.Logger.Logger() + return self.node.GetValueAsUnsigned(0) + + def _sbvalue_impl(self): + logger = lldb.formatters.Logger.Logger() + return self.node + + def _null_impl(self): + logger = lldb.formatters.Logger.Logger() + return self.value == 0 + + def __init__(self,node): + logger = lldb.formatters.Logger.Logger() + self.node = node + + left = property(_left_impl,None) + right = property(_right_impl,None) + parent = property(_parent_impl,None) + value = property(_value_impl,None) + is_null = property(_null_impl,None) + sbvalue = property(_sbvalue_impl,None) + +# a Python implementation of the tree iterator used by libc++ +class stdmap_iterator: + + def tree_min(self,x): + logger = lldb.formatters.Logger.Logger() + steps = 0 + if x.is_null: + return None + while (not x.left.is_null): + x = x.left + steps += 1 + if steps > self.max_count: + logger >> "Returning None - we overflowed" + return None + return x + + def tree_max(self,x): + logger = lldb.formatters.Logger.Logger() + if x.is_null: + return None + while (not x.right.is_null): + x = x.right + return x + + def tree_is_left_child(self,x): + logger = lldb.formatters.Logger.Logger() + if x.is_null: + return None + return True if x.value == x.parent.left.value else False + + def increment_node(self,node): + logger = lldb.formatters.Logger.Logger() + if node.is_null: + return None + if not node.right.is_null: + return self.tree_min(node.right) + steps = 0 + while (not self.tree_is_left_child(node)): + steps += 1 + if steps > self.max_count: + logger >> "Returning None - we overflowed" + return None + node = node.parent + return node.parent + + def __init__(self,node,max_count=0): + logger = lldb.formatters.Logger.Logger() + self.node = stdmap_iterator_node(node) # we convert the SBValue to an internal node object on entry + self.max_count = max_count + + def value(self): + logger = lldb.formatters.Logger.Logger() + return self.node.sbvalue # and return the SBValue back on exit + + def next(self): + logger = lldb.formatters.Logger.Logger() + node = self.increment_node(self.node) + if node != None and node.sbvalue.IsValid() and not(node.is_null): + self.node = node + return self.value() + else: + return None + + def advance(self,N): + logger = lldb.formatters.Logger.Logger() + if N < 0: + return None + if N == 0: + return self.value() + if N == 1: + return self.next() + while N > 0: + if self.next() == None: + return None + N = N - 1 + return self.value() + +class stdmap_SynthProvider: + + def __init__(self, valobj, dict): + logger = lldb.formatters.Logger.Logger() + self.valobj = valobj; + self.pointer_size = self.valobj.GetProcess().GetAddressByteSize() + self.count = None + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.count = None + try: + # we will set this to True if we find out that discovering a node in the map takes more steps than the overall size of the RB tree + # if this gets set to True, then we will merrily return None for any child from that moment on + self.garbage = False + self.tree = self.valobj.GetChildMemberWithName('__tree_') + self.root_node = self.tree.GetChildMemberWithName('__begin_node_') + # this data is either lazily-calculated, or cannot be inferred at this moment + # we still need to mark it as None, meaning "please set me ASAP" + self.data_type = None + self.data_size = None + self.skip_size = None + except: + pass + + def num_children(self): + global _map_capping_size + logger = lldb.formatters.Logger.Logger() + if self.count == None: + self.count = self.num_children_impl() + if self.count > _map_capping_size: + self.count = _map_capping_size + return self.count + + def num_children_impl(self): + logger = lldb.formatters.Logger.Logger() + try: + return self.valobj.GetChildMemberWithName('__tree_').GetChildMemberWithName('__pair3_').GetChildMemberWithName('__first_').GetValueAsUnsigned() + except: + return 0; + + def has_children(self): + return True + + def get_data_type(self): + logger = lldb.formatters.Logger.Logger() + if self.data_type == None or self.data_size == None: + if self.num_children() == 0: + return False + deref = self.root_node.Dereference() + if not(deref.IsValid()): + return False + value = deref.GetChildMemberWithName('__value_') + if not(value.IsValid()): + return False + self.data_type = value.GetType() + self.data_size = self.data_type.GetByteSize() + self.skip_size = None + return True + else: + return True + + def get_value_offset(self,node): + logger = lldb.formatters.Logger.Logger() + if self.skip_size == None: + node_type = node.GetType() + fields_count = node_type.GetNumberOfFields() + for i in range(fields_count): + field = node_type.GetFieldAtIndex(i) + if field.GetName() == '__value_': + self.skip_size = field.GetOffsetInBytes() + break + return (self.skip_size != None) + + def get_child_index(self,name): + logger = lldb.formatters.Logger.Logger() + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self,index): + logger = lldb.formatters.Logger.Logger() + logger >> "Retrieving child " + str(index) + if index < 0: + return None + if index >= self.num_children(): + return None; + if self.garbage: + logger >> "Returning None since this tree is garbage" + return None + try: + iterator = stdmap_iterator(self.root_node,max_count=self.num_children()) + # the debug info for libc++ std::map is such that __begin_node_ has a very nice and useful type + # out of which we can grab the information we need - every other node has a less informative + # type which omits all value information and only contains housekeeping information for the RB tree + # hence, we need to know if we are at a node != 0, so that we can still get at the data + need_to_skip = (index > 0) + current = iterator.advance(index) + if current == None: + logger >> "Tree is garbage - returning None" + self.garbage = True + return None + if self.get_data_type(): + if not(need_to_skip): + current = current.Dereference() + obj = current.GetChildMemberWithName('__value_') + obj_data = obj.GetData() + self.get_value_offset(current) # make sure we have a valid offset for the next items + # we do not return __value_ because then we would end up with a child named + # __value_ instead of [0] + return self.valobj.CreateValueFromData('[' + str(index) + ']',obj_data,self.data_type) + else: + # FIXME we need to have accessed item 0 before accessing any other item! + if self.skip_size == None: + logger >> "You asked for item > 0 before asking for item == 0, I will fetch 0 now then retry" + if self.get_child_at_index(0): + return self.get_child_at_index(index) + else: + logger >> "item == 0 could not be found. sorry, nothing can be done here." + return None + return current.CreateChildAtOffset('[' + str(index) + ']',self.skip_size,self.data_type) + else: + logger >> "Unable to infer data-type - returning None (should mark tree as garbage here?)" + return None + except Exception as err: + logger >> "Hit an exception: " + str(err) + return None + +# Just an example: the actual summary is produced by a summary string: size=${svar%#} +def stdmap_SummaryProvider(valobj,dict): + prov = stdmap_SynthProvider(valobj,None) + return 'size=' + str(prov.num_children()) + +class stddeque_SynthProvider: + def __init__(self, valobj, d): + logger = lldb.formatters.Logger.Logger() + logger.write("init") + self.valobj = valobj + self.pointer_size = self.valobj.GetProcess().GetAddressByteSize() + self.count = None + try: + self.find_block_size() + except: + self.block_size = -1 + self.element_size = -1 + logger.write("block_size=%d, element_size=%d" % (self.block_size, self.element_size)) + + def find_block_size(self): + # in order to use the deque we must have the block size, or else + # it's impossible to know what memory addresses are valid + self.element_type = self.valobj.GetType().GetTemplateArgumentType(0) + self.element_size = self.element_type.GetByteSize() + # The code says this, but there must be a better way: + # template <class _Tp, class _Allocator> + # class __deque_base { + # static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16; + # } + if self.element_size < 256: + self.block_size = 4096 / self.element_size + else: + self.block_size = 16 + + def num_children(self): + global _deque_capping_size + logger = lldb.formatters.Logger.Logger() + if self.count is None: + return 0 + return min(self.count, _deque_capping_size) + + def has_children(self): + return True + + def get_child_index(self,name): + logger = lldb.formatters.Logger.Logger() + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self,index): + logger = lldb.formatters.Logger.Logger() + logger.write("Fetching child " + str(index)) + if index < 0 or self.count is None: + return None; + if index >= self.num_children(): + return None; + try: + i, j = divmod(self.start+index, self.block_size) + return self.first.CreateValueFromExpression('[' + str(index) + ']', + '*(*(%s + %d) + %d)' % (self.first.get_expr_path(), i, j)) + except: + return None + + def update(self): + logger = lldb.formatters.Logger.Logger() + try: + # A deque is effectively a two-dim array, with fixed width. + # 'map' contains pointers to the rows of this array. The + # full memory area allocated by the deque is delimited + # by 'first' and 'end_cap'. However, only a subset of this + # memory contains valid data since a deque may have some slack + # at the front and back in order to have O(1) insertion at + # both ends. The rows in active use are delimited by + # 'begin' and 'end'. + # + # To find the elements that are actually constructed, the 'start' + # variable tells which element in this NxM array is the 0th + # one, and the 'size' element gives the number of elements + # in the deque. + count = self.valobj.GetChildMemberWithName('__size_').GetChildMemberWithName('__first_').GetValueAsUnsigned(0) + # give up now if we cant access memory reliably + if self.block_size < 0: + logger.write("block_size < 0") + return + map_ = self.valobj.GetChildMemberWithName('__map_') + start = self.valobj.GetChildMemberWithName('__start_').GetValueAsUnsigned(0) + first = map_.GetChildMemberWithName('__first_') + map_first = first.GetValueAsUnsigned(0) + map_begin = map_.GetChildMemberWithName('__begin_').GetValueAsUnsigned(0) + map_end = map_.GetChildMemberWithName('__end_').GetValueAsUnsigned(0) + map_endcap= map_.GetChildMemberWithName('__end_cap_').GetChildMemberWithName('__first_').GetValueAsUnsigned(0) + # check consistency + if not map_first <= map_begin <= map_end <= map_endcap: + logger.write("map pointers are not monotonic") + return + total_rows, junk = divmod(map_endcap - map_first, self.pointer_size) + if junk: + logger.write("endcap-first doesnt align correctly") + return + active_rows, junk = divmod(map_end - map_begin, self.pointer_size) + if junk: + logger.write("end-begin doesnt align correctly") + return + start_row, junk = divmod(map_begin - map_first, self.pointer_size) + if junk: + logger.write("begin-first doesnt align correctly") + return + if not start_row*self.block_size <= start < (start_row+1)*self.block_size: + logger.write("0th element must be in the 'begin' row") + return + end_row = start_row + active_rows + if not count: + if active_rows: + logger.write("empty deque but begin!=end") + return + elif not (end_row-1)*self.block_size <= start+count < end_row*self.block_size: + logger.write("nth element must be before the 'end' row") + return + logger.write("update success: count=%r, start=%r, first=%r" % (count,start,first)) + # if consistent, save all we really need: + self.count = count + self.start = start + self.first = first + except: + self.count = None + self.start = None + self.map_first = None + self.map_begin = None + +class stdsharedptr_SynthProvider: + def __init__(self, valobj, d): + logger = lldb.formatters.Logger.Logger() + logger.write("init") + self.valobj = valobj + #self.element_ptr_type = self.valobj.GetType().GetTemplateArgumentType(0).GetPointerType() + self.ptr = None + self.cntrl = None + process = valobj.GetProcess() + self.endianness = process.GetByteOrder() + self.pointer_size = process.GetAddressByteSize() + self.count_type = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) + + def num_children(self): + return 1 + + def has_children(self): + return True + + def get_child_index(self,name): + if name=="__ptr_": + return 0 + if name=="count": + return 1 + if name=="weak_count": + return 2 + return -1 + + def get_child_at_index(self,index): + if index == 0: + return self.ptr + if index == 1: + if self.cntrl == None: + count = 0 + else: + count = 1 + self.cntrl.GetChildMemberWithName('__shared_owners_').GetValueAsSigned() + return self.valobj.CreateValueFromData("count", + lldb.SBData.CreateDataFromUInt64Array(self.endianness, self.pointer_size, [count]), + self.count_type) + if index == 2: + if self.cntrl == None: + count = 0 + else: + count = 1 + self.cntrl.GetChildMemberWithName('__shared_weak_owners_').GetValueAsSigned() + return self.valobj.CreateValueFromData("weak_count", + lldb.SBData.CreateDataFromUInt64Array(self.endianness, self.pointer_size, [count]), + self.count_type) + return None + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.ptr = self.valobj.GetChildMemberWithName('__ptr_')#.Cast(self.element_ptr_type) + cntrl = self.valobj.GetChildMemberWithName('__cntrl_') + if cntrl.GetValueAsUnsigned(0): + self.cntrl = cntrl.Dereference() + else: + self.cntrl = None + +# we can use two different categories for old and new formatters - type names are different enough that we should make no confusion +# talking with libc++ developer: "std::__1::class_name is set in stone until we decide to change the ABI. That shouldn't happen within a 5 year time frame" +def __lldb_init_module(debugger,dict): + debugger.HandleCommand('type summary add -F libcxx.stdstring_SummaryProvider "std::__1::string" -w libcxx') + debugger.HandleCommand('type summary add -F libcxx.stdstring_SummaryProvider "std::__1::basic_string<char, class std::__1::char_traits<char>, class std::__1::allocator<char> >" -w libcxx') + debugger.HandleCommand('type synthetic add -l libcxx.stdvector_SynthProvider -x "^(std::__1::)vector<.+>$" -w libcxx') + debugger.HandleCommand('type summary add -F libcxx.stdvector_SummaryProvider -e -x "^(std::__1::)vector<.+>$" -w libcxx') + debugger.HandleCommand('type synthetic add -l libcxx.stdlist_SynthProvider -x "^(std::__1::)list<.+>$" -w libcxx') + debugger.HandleCommand('type summary add -F libcxx.stdlist_SummaryProvider -e -x "^(std::__1::)list<.+>$" -w libcxx') + debugger.HandleCommand('type synthetic add -l libcxx.stdmap_SynthProvider -x "^(std::__1::)map<.+> >$" -w libcxx') + debugger.HandleCommand('type summary add -F libcxx.stdmap_SummaryProvider -e -x "^(std::__1::)map<.+> >$" -w libcxx') + debugger.HandleCommand("type category enable libcxx") + debugger.HandleCommand('type synthetic add -l libcxx.stddeque_SynthProvider -x "^(std::__1::)deque<.+>$" -w libcxx') + debugger.HandleCommand('type synthetic add -l libcxx.stdsharedptr_SynthProvider -x "^(std::__1::)shared_ptr<.+>$" -w libcxx') + # turns out the structs look the same, so weak_ptr can be handled the same! + debugger.HandleCommand('type synthetic add -l libcxx.stdsharedptr_SynthProvider -x "^(std::__1::)weak_ptr<.+>$" -w libcxx') + +_map_capping_size = 255 +_list_capping_size = 255 +_list_uses_loop_detector = True +_deque_capping_size = 255 diff --git a/examples/synthetic/unordered_multi.py b/examples/synthetic/unordered_multi.py new file mode 100644 index 000000000000..3389a01aea30 --- /dev/null +++ b/examples/synthetic/unordered_multi.py @@ -0,0 +1,110 @@ +import lldb + +_map_capping_size = 255 + +class libcxx_hash_table_SynthProvider: + def __init__(self, valobj, dict): + self.valobj = valobj + self.num_elements = None + self.next_element = None + self.bucket_count = None + + def update(self): + logger = lldb.formatters.Logger.Logger() + self.num_elements = None + self.next_element = None + self.bucket_count = None + try: + # unordered_map is made up of a hash_map, which has 4 pieces in it: + # bucket list : + # array of buckets + # p1 (pair): + # first - pointer to first loaded element + # p2 (pair): + # first - number of elements + # second - hash function + # p3 (pair): + # first - max_load_factor + # second - equality operator function + # + # For display, we actually don't need to go inside the buckets, since 'p1' has a way to iterate over all + # the elements directly. + # + # We will calculate other values about the map because they will be useful for the summary. + # + table = self.valobj.GetChildMemberWithName('__table_') + + bl_ptr = table.GetChildMemberWithName('__bucket_list_').GetChildMemberWithName('__ptr_') + self.bucket_array_ptr = bl_ptr.GetChildMemberWithName('__first_').GetValueAsUnsigned(0) + self.bucket_count = bl_ptr.GetChildMemberWithName('__second_').GetChildMemberWithName('__data_').GetChildMemberWithName('__first_').GetValueAsUnsigned(0) + logger >> "Bucket count = %r" % self.bucket_count + + self.begin_ptr = table.GetChildMemberWithName('__p1_').GetChildMemberWithName('__first_').GetChildMemberWithName('__next_') + + self.num_elements = table.GetChildMemberWithName('__p2_').GetChildMemberWithName('__first_').GetValueAsUnsigned(0) + self.max_load_factor = table.GetChildMemberWithName('__p3_').GetChildMemberWithName('__first_').GetValueAsUnsigned(0) + logger >> "Num elements = %r" % self.num_elements + + # save the pointers as we get them + # -- don't access this first element if num_element==0! + self.elements_cache = [] + if self.num_elements: + self.next_element = self.begin_ptr + else: + self.next_element = None + except Exception as e: + logger >> "Caught exception: %r" % e + pass + + def num_children(self): + global _map_capping_size + num_elements = self.num_elements + if num_elements is not None: + if num_elements > _map_capping_size: + num_elements = _map_capping_size + return num_elements + + def has_children(self): + return True + + def get_child_index(self,name): + logger = lldb.formatters.Logger.Logger() + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self,index): + logger = lldb.formatters.Logger.Logger() + logger >> "Retrieving child " + str(index) + if index < 0: + return None + if index >= self.num_children(): + return None + + # extend + logger >> " : cache size starts with %d elements" % len(self.elements_cache) + while index >= len(self.elements_cache): + # if we hit the end before we get the index, give up: + if not self.next_element: + logger >> " : hit end of list" + return None + + node = self.next_element.Dereference() + + value = node.GetChildMemberWithName('__value_') + hash_value = node.GetChildMemberWithName('__hash_').GetValueAsUnsigned() + self.elements_cache.append((value, hash_value)) + + self.next_element = node.GetChildMemberWithName('__next_') + if not self.next_element.GetValueAsUnsigned(0): + self.next_element = None + + # hit the index! so we have the value + logger >> " : cache size ends with %d elements" % len(self.elements_cache) + value, hash_value = self.elements_cache[index] + return self.valobj.CreateValueFromData('[%d] <hash %d>'%(index,hash_value), value.GetData(), value.GetType()) + + +def __lldb_init_module(debugger,dict): + debugger.HandleCommand('type synthetic add -l unordered_multi.libcxx_hash_table_SynthProvider -x "^(std::__1::)unordered_(multi)?(map|set)<.+> >$" -w libcxx') diff --git a/examples/test/.lldb-loggings b/examples/test/.lldb-loggings new file mode 100644 index 000000000000..9c92bd958479 --- /dev/null +++ b/examples/test/.lldb-loggings @@ -0,0 +1,20 @@ +def pre_flight(self): + import os + import lldb + import lldbtest + + dname = os.path.join(os.environ["LLDB_TEST"], + os.environ["LLDB_SESSION_DIRNAME"]) + if not os.path.isdir(dname): + os.mkdir(dname) + dest = os.path.join(dname, "lldb_log-%s-%s-%s.txt" % (self.getArchitecture(), self.getCompiler(), self.id())) + print "\nEnabling lldb logging for test case:", self + print "with log destination:", dest + self.runCmd("log enable -f %s gdb-remote packets process" % dest) + +#def post_flight(test): +# __import__("lldb") +# __import__("lldbtest") +# print "\nRunning post-flight function:" +# print "for test case:", test + diff --git a/examples/test/.lldb-pre-post-flight b/examples/test/.lldb-pre-post-flight new file mode 100644 index 000000000000..c1568a7295a7 --- /dev/null +++ b/examples/test/.lldb-pre-post-flight @@ -0,0 +1,12 @@ +def pre_flight(test): + __import__("lldb") + __import__("lldbtest") + print "\nRunning pre-flight function:" + print "for test case:", test + +def post_flight(test): + __import__("lldb") + __import__("lldbtest") + print "\nRunning post-flight function:" + print "for test case:", test + diff --git a/examples/test/.lldb-pre-post-flight.bad b/examples/test/.lldb-pre-post-flight.bad new file mode 100644 index 000000000000..0e17f3cdc95b --- /dev/null +++ b/examples/test/.lldb-pre-post-flight.bad @@ -0,0 +1,8 @@ +pre_flight = "I am not callable" + +def post_flight(test): + __import__("lldb") + __import__("lldbtest") + print "\nRunning post-flight function:" + print "for test case:", test + diff --git a/examples/test/.lldbtest-config b/examples/test/.lldbtest-config new file mode 100644 index 000000000000..31b489207778 --- /dev/null +++ b/examples/test/.lldbtest-config @@ -0,0 +1,6 @@ +sys.stderr = open("/tmp/lldbtest-stderr", "w") +sys.stdout = open("/tmp/lldbtest-stdout", "w") +compilers = ["gcc", "llvm-gcc"] +archs = ["x86_64", "i386"] +split_stderr = True # This will split the stderr into configuration-specific file +split_stdout = True # This will split the stdout into configuration-specific file diff --git a/examples/test/.lldbtest-config2 b/examples/test/.lldbtest-config2 new file mode 100644 index 000000000000..bf44726fd7f2 --- /dev/null +++ b/examples/test/.lldbtest-config2 @@ -0,0 +1,19 @@ +# Example config file for running the test suite for both 64 and 32-bit +# architectures. +# +# I use the following command to invoke the test driver: +# +# /Volumes/data/lldb/svn/trunk/test $ ./dotest.py -r /Volumes/data/lldb-test/archs -s session -c ../examples/test/.lldbtest-config2 -v -w . 2> ~/Developer/Log/lldbtest.log +# +# The '-r' option tells the driver to relocate the test execution to +# /Volumes/data/lldb-test/archs which must not exist before the run. +# +# Test failures/errors will be recorded into the 'session' directory due to the +# '-s' option, e.g., /Volumes/data/lldb-test/archs.arch=i386/test/session could +# contain the following three session info files: +# +# -rw-r--r-- 1 johnny admin 1737 Oct 25 13:25 TestArrayTypes.ArrayTypesTestCase.test_with_dwarf_and_run_command.log +# -rw-r--r-- 1 johnny admin 1733 Oct 25 13:25 TestClassTypes.ClassTypesTestCase.test_with_dwarf_and_run_command.log +# -rw-r--r-- 1 johnny admin 4677 Oct 25 13:26 TestObjCMethods.FoundationTestCase.test_data_type_and_expr_with_dsym.log + +archs = ["x86_64", "i386"] diff --git a/examples/test/lldbtest-stderr b/examples/test/lldbtest-stderr new file mode 100644 index 000000000000..7934d92835cc --- /dev/null +++ b/examples/test/lldbtest-stderr @@ -0,0 +1,39 @@ +---------------------------------------------------------------------- +Collected 1 test + + +Configuration: arch=x86_64 compiler=gcc +test_persistent_variables (TestPersistentVariables.PersistentVariablesTestCase) +Test that lldb persistent variables works correctly. ... ok + +---------------------------------------------------------------------- +Ran 1 test in 1.397s + +OK + +Configuration: arch=x86_64 compiler=llvm-gcc +test_persistent_variables (TestPersistentVariables.PersistentVariablesTestCase) +Test that lldb persistent variables works correctly. ... ok + +---------------------------------------------------------------------- +Ran 1 test in 1.282s + +OK + +Configuration: arch=i386 compiler=gcc +test_persistent_variables (TestPersistentVariables.PersistentVariablesTestCase) +Test that lldb persistent variables works correctly. ... ok + +---------------------------------------------------------------------- +Ran 1 test in 1.297s + +OK + +Configuration: arch=i386 compiler=llvm-gcc +test_persistent_variables (TestPersistentVariables.PersistentVariablesTestCase) +Test that lldb persistent variables works correctly. ... ok + +---------------------------------------------------------------------- +Ran 1 test in 1.269s + +OK diff --git a/examples/test/lldbtest-stdout b/examples/test/lldbtest-stdout new file mode 100644 index 000000000000..e69de29bb2d1 --- /dev/null +++ b/examples/test/lldbtest-stdout diff --git a/examples/test/tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dsym.txt b/examples/test/tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dsym.txt new file mode 100644 index 000000000000..c1448cd844c2 --- /dev/null +++ b/examples/test/tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dsym.txt @@ -0,0 +1,55 @@ +com.apple.main-thread /Volumes/data/lldb/svn/ToT/build/Debug/LLDB.framework/Versions/A/Resources/debugserver arguments: +argv[0]="/Volumes/data/lldb/svn/ToT/build/Debug/LLDB.framework/Versions/A/Resources/debugserver" +argv[1]="localhost:14953" +argv[2]="--native-regs" +argv[3]="--setsid" +argv[4]=NULL + + +com.apple.main-thread Host::LaunchProcess (launch_info) => pid=55237, path='/Volumes/data/lldb/svn/ToT/build/Debug/LLDB.framework/Versions/A/Resources/debugserver' err = 0x00000000 +com.apple.main-thread ProcessGDBRemote::StartAsyncThread () +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc1883400, pid = 0) thread starting... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc1883400, pid = 0) listener.WaitForEvent (NULL, event_sp)... +com.apple.main-thread < 1> send packet: + +com.apple.main-thread history[1] tid=0x1307 < 1> send packet: + +com.apple.main-thread < 19> send packet: $QStartNoAckMode#b0 +com.apple.main-thread < 1> read packet: + +com.apple.main-thread < 6> read packet: $OK#9a +com.apple.main-thread < 1> send packet: + +com.apple.main-thread < 26> send packet: $QThreadSuffixSupported#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread < 27> send packet: $QListThreadsInStopReply#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread < 13> send packet: $qHostInfo#00 +com.apple.main-thread < 122> read packet: $cputype:16777223;cpusubtype:3;ostype:macosx;watchpoint_exceptions_received:after;vendor:apple;endian:little;ptrsize:8;#00 +com.apple.main-thread < 10> send packet: $vCont?#00 +com.apple.main-thread < 17> read packet: $vCont;c;C;s;S#00 +com.apple.main-thread < 27> send packet: $qVAttachOrWaitSupported#00 +com.apple.main-thread < 6> read packet: $OK#00 + +... + +com.apple.main-thread ProcessGDBRemote::DoDestroy() +com.apple.main-thread < 5> send packet: $k#00 +com.apple.main-thread error: failed to get response for 'k' +com.apple.main-thread ProcessGDBRemote::DoDestroy - failed to send k packet +com.apple.main-thread ProcessGDBRemote::StopAsyncThread () + ProcessGDBRemote::AsyncThread (arg = 0x7fabc185e200, pid = 55239) Got an event of type: 2... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc185e200, pid = 55239) got eBroadcastBitAsyncThreadShouldExit... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc185e200, pid = 55239) thread exiting... +com.apple.root.default-priority ProcessGDBRemote::MonitorDebugserverProcess (baton=0x7fabc185e200, pid=55240, signo=2 (0x2), exit_status=-1) +com.apple.main-thread < 18> send packet: $z0,100000e37,1#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread < 21> send packet: $z0,7fff5fc0d6e5,1#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread < 21> send packet: $z0,7fff8b132187,1#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread ProcessGDBRemote::DoDestroy() +com.apple.main-thread < 5> send packet: $k#00 +com.apple.main-thread error: failed to get response for 'k' +com.apple.main-thread ProcessGDBRemote::DoDestroy - failed to send k packet +com.apple.main-thread ProcessGDBRemote::StopAsyncThread () +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc18f8600, pid = 55243) Got an event of type: 2... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc18f8600, pid = 55243) got eBroadcastBitAsyncThreadShouldExit... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc18f8600, pid = 55243) thread exiting... +com.apple.root.default-priority ProcessGDBRemote::MonitorDebugserverProcess (baton=0x7fabc18f8600, pid=55244, signo=2 (0x2), exit_status=-1) diff --git a/examples/test/tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dwarf.txt b/examples/test/tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dwarf.txt new file mode 100644 index 000000000000..87cfddb29329 --- /dev/null +++ b/examples/test/tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dwarf.txt @@ -0,0 +1,55 @@ +com.apple.main-thread /Volumes/data/lldb/svn/ToT/build/Debug/LLDB.framework/Versions/A/Resources/debugserver arguments: +argv[0]="/Volumes/data/lldb/svn/ToT/build/Debug/LLDB.framework/Versions/A/Resources/debugserver" +argv[1]="localhost:33231" +argv[2]="--native-regs" +argv[3]="--setsid" +argv[4]=NULL + + +com.apple.main-thread Host::LaunchProcess (launch_info) => pid=55287, path='/Volumes/data/lldb/svn/ToT/build/Debug/LLDB.framework/Versions/A/Resources/debugserver' err = 0x00000000 +com.apple.main-thread ProcessGDBRemote::StartAsyncThread () +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc0997600, pid = 0) thread starting... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc0997600, pid = 0) listener.WaitForEvent (NULL, event_sp)... +com.apple.main-thread < 1> send packet: + +com.apple.main-thread history[1] tid=0x1307 < 1> send packet: + +com.apple.main-thread < 19> send packet: $QStartNoAckMode#b0 +com.apple.main-thread < 1> read packet: + +com.apple.main-thread < 6> read packet: $OK#9a +com.apple.main-thread < 1> send packet: + +com.apple.main-thread < 26> send packet: $QThreadSuffixSupported#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread < 27> send packet: $QListThreadsInStopReply#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread < 13> send packet: $qHostInfo#00 +com.apple.main-thread < 122> read packet: $cputype:16777223;cpusubtype:3;ostype:macosx;watchpoint_exceptions_received:after;vendor:apple;endian:little;ptrsize:8;#00 +com.apple.main-thread < 10> send packet: $vCont?#00 +com.apple.main-thread < 17> read packet: $vCont;c;C;s;S#00 +com.apple.main-thread < 27> send packet: $qVAttachOrWaitSupported#00 +com.apple.main-thread < 6> read packet: $OK#00 + +... + +com.apple.main-thread ProcessGDBRemote::DoDestroy() +com.apple.main-thread < 5> send packet: $k#00 +com.apple.main-thread error: failed to get response for 'k' +com.apple.main-thread ProcessGDBRemote::DoDestroy - failed to send k packet +com.apple.main-thread ProcessGDBRemote::StopAsyncThread () +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc1882000, pid = 55289) Got an event of type: 2... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc1882000, pid = 55289) got eBroadcastBitAsyncThreadShouldExit... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc1882000, pid = 55289) thread exiting... +com.apple.root.default-priority ProcessGDBRemote::MonitorDebugserverProcess (baton=0x7fabc1882000, pid=55290, signo=2 (0x2), exit_status=-1) +com.apple.main-thread < 18> send packet: $z0,100000e37,1#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread < 21> send packet: $z0,7fff5fc0d6e5,1#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread < 21> send packet: $z0,7fff8b132187,1#00 +com.apple.main-thread < 6> read packet: $OK#00 +com.apple.main-thread ProcessGDBRemote::DoDestroy() +com.apple.main-thread < 5> send packet: $k#00 +com.apple.main-thread error: failed to get response for 'k' +com.apple.main-thread ProcessGDBRemote::DoDestroy - failed to send k packet +com.apple.main-thread ProcessGDBRemote::StopAsyncThread () +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc0bed200, pid = 55292) Got an event of type: 2... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc0bed200, pid = 55292) got eBroadcastBitAsyncThreadShouldExit... +<lldb.process.gdb-remote.async> ProcessGDBRemote::AsyncThread (arg = 0x7fabc0bed200, pid = 55292) thread exiting... +com.apple.root.default-priority ProcessGDBRemote::MonitorDebugserverProcess (baton=0x7fabc0bed200, pid=55293, signo=2 (0x2), exit_status=-1) diff --git a/examples/test/usage-config b/examples/test/usage-config new file mode 100644 index 000000000000..4f3d3b222f17 --- /dev/null +++ b/examples/test/usage-config @@ -0,0 +1,10 @@ +# This is an example of using the "-c" option to source a config file to +# reassign the system stderr and stdout and to exercise different combinations +# of architectures and compilers. +# +# The config file is checked in as .lldbtest-config and the redirected stderr +# and stdout are checked in as lldbtest-stderr and lldbtest-stdout, all in the +# the same directory as this file. + +[15:36:32] johnny:/Volumes/data/lldb/svn/trunk/test $ ./dotest.py -v -c ~/.lldbtest-config persistent_variables +[15:40:55] johnny:/Volumes/data/lldb/svn/trunk/test $ diff --git a/examples/test/usage-lldb-loggings b/examples/test/usage-lldb-loggings new file mode 100644 index 000000000000..b7d7e2e58fc1 --- /dev/null +++ b/examples/test/usage-lldb-loggings @@ -0,0 +1,125 @@ +# +# The following example shows how to utilize the pre-flight config file to route the lldb gdb-remote log messages +# into individual log destinations. +# +# See also .lldb-loggings in this directory as well as the tmp dir which contains the two log files abridged due +# to their log sizes. +# + +[11:31:34] johnny:/Volumes/data/lldb/svn/ToT/test $ ./dotest.py -A x86_64 -v -c ../examples/test/.lldb-loggings functionalities/breakpoint/breakpoint_command +config: {'pre_flight': <function pre_flight at 0x107042140>} +LLDB build dir: /Volumes/data/lldb/svn/ToT/build/Debug +LLDB-165 +Path: /Volumes/data/lldb/svn/ToT +URL: https://johnny@llvm.org/svn/llvm-project/lldb/trunk +Repository Root: https://johnny@llvm.org/svn/llvm-project +Repository UUID: 91177308-0d34-0410-b5e6-96231b3b80d8 +Revision: 162231 +Node Kind: directory +Schedule: normal +Last Changed Author: johnny +Last Changed Rev: 162228 +Last Changed Date: 2012-08-20 14:16:02 -0700 (Mon, 20 Aug 2012) + + +lldb.pre_flight: def pre_flight(self): + import os + import lldb + import lldbtest + + dest = os.path.join("/tmp", "lldb_log-%s-%s-%s.txt" % (self.getArchitecture(), self.getCompiler(), self.id())) + print "\nEnabling lldb logging for test case:", self + print "with log destination:", dest + self.runCmd("log enable -f %s gdb-remote packets process" % dest) + +lldb.post_flight: None + +Session logs for test failures/errors/unexpected successes will go into directory '2012-08-22-11_36_37' +Command invoked: python ./dotest.py -A x86_64 -v -c ../examples/test/.lldb-loggings functionalities/breakpoint/breakpoint_command +compilers=['clang'] + +Configuration: arch=x86_64 compiler=clang +---------------------------------------------------------------------- +Collected 2 tests + +1: test_with_dsym (TestBreakpointCommand.BreakpointCommandTestCase) + Test a sequence of breakpoint command add, list, and delete. ... +Enabling lldb logging for test case: test_with_dsym (TestBreakpointCommand.BreakpointCommandTestCase) +with log destination: /tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dsym.txt +ok +2: test_with_dwarf (TestBreakpointCommand.BreakpointCommandTestCase) + Test a sequence of breakpoint command add, list, and delete. ... +Enabling lldb logging for test case: test_with_dwarf (TestBreakpointCommand.BreakpointCommandTestCase) +with log destination: /tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dwarf.txt +ok + +---------------------------------------------------------------------- +Ran 2 tests in 7.826s + +OK +[11:36:44] johnny:/Volumes/data/lldb/svn/ToT/test $ ls -l /tmp/lldb_log* +-rw-r----- 1 johnny wheel 614614 Aug 22 11:36 /tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dsym.txt +-rw-r----- 1 johnny wheel 614614 Aug 22 11:36 /tmp/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dwarf.txt +[11:37:09] johnny:/Volumes/data/lldb/svn/ToT/test $ + +# +# And this shows the log files go into the session directory. +# Note that the .lldb-loggings file is modified to get the session directory now. +# + +[11:37:09] johnny:/Volumes/data/lldb/svn/ToT/test $ ./dotest.py -A x86_64 -v -c ../examples/test/.lldb-loggings functionalities/breakpoint/breakpoint_command +config: {'pre_flight': <function pre_flight at 0x10ca5c1b8>} +LLDB build dir: /Volumes/data/lldb/svn/ToT/build/Debug +LLDB-165 +Path: /Volumes/data/lldb/svn/ToT +URL: https://johnny@llvm.org/svn/llvm-project/lldb/trunk +Repository Root: https://johnny@llvm.org/svn/llvm-project +Repository UUID: 91177308-0d34-0410-b5e6-96231b3b80d8 +Revision: 162231 +Node Kind: directory +Schedule: normal +Last Changed Author: johnny +Last Changed Rev: 162228 +Last Changed Date: 2012-08-20 14:16:02 -0700 (Mon, 20 Aug 2012) + + +lldb.pre_flight: def pre_flight(self): + import os + import lldb + import lldbtest + + dname = os.path.join(os.environ["LLDB_TEST"], + os.environ["LLDB_SESSION_DIRNAME"]) + if not os.path.isdir(dname): + os.mkdir(dname) + dest = os.path.join(dname, "lldb_log-%s-%s-%s.txt" % (self.getArchitecture(), self.getCompiler(), self.id())) + print "\nEnabling lldb logging for test case:", self + print "with log destination:", dest + self.runCmd("log enable -f %s gdb-remote packets process" % dest) + +lldb.post_flight: None + +Session logs for test failures/errors/unexpected successes will go into directory '2012-08-22-13_21_46' +Command invoked: python ./dotest.py -A x86_64 -v -c ../examples/test/.lldb-loggings functionalities/breakpoint/breakpoint_command +compilers=['clang'] + +Configuration: arch=x86_64 compiler=clang +---------------------------------------------------------------------- +Collected 2 tests + +1: test_with_dsym (TestBreakpointCommand.BreakpointCommandTestCase) + Test a sequence of breakpoint command add, list, and delete. ... +Enabling lldb logging for test case: test_with_dsym (TestBreakpointCommand.BreakpointCommandTestCase) +with log destination: /Volumes/data/lldb/svn/ToT/test/2012-08-22-13_21_46/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dsym.txt +ok +2: test_with_dwarf (TestBreakpointCommand.BreakpointCommandTestCase) + Test a sequence of breakpoint command add, list, and delete. ... +Enabling lldb logging for test case: test_with_dwarf (TestBreakpointCommand.BreakpointCommandTestCase) +with log destination: /Volumes/data/lldb/svn/ToT/test/2012-08-22-13_21_46/lldb_log-x86_64-clang-TestBreakpointCommand.BreakpointCommandTestCase.test_with_dwarf.txt +ok + +---------------------------------------------------------------------- +Ran 2 tests in 8.575s + +OK +[13:21:55] johnny:/Volumes/data/lldb/svn/ToT/test $ diff --git a/examples/test/usage-pre-post-flight b/examples/test/usage-pre-post-flight new file mode 100644 index 000000000000..da6860724258 --- /dev/null +++ b/examples/test/usage-pre-post-flight @@ -0,0 +1,65 @@ +# +# The following examples first show a bad pre/post flight config file followed by a good pre/post config file. +# + +[11:31:19] johnny:/Volumes/data/lldb/svn/ToT/test $ ./dotest.py -A x86_64 -v -c ../examples/test/.lldb-pre-post-flight.bad functionalities/watchpoint/hello_watchpoint +config: {'pre_flight': 'I am not callable', 'post_flight': <function post_flight at 0x1071871b8>} +fatal error: pre_flight is not callable, exiting. +[11:32:48] johnny:/Volumes/data/lldb/svn/ToT/test $ ./dotest.py -A x86_64 -v -c ../examples/test/.lldb-pre-post-flight functionalities/watchpoint/hello_watchpoint +config: {'pre_flight': <function pre_flight at 0x1098541b8>, 'post_flight': <function post_flight at 0x109854230>} +LLDB build dir: /Volumes/data/lldb/svn/ToT/build/Debug +LLDB-139 +Path: /Volumes/data/lldb/svn/ToT +URL: https://johnny@llvm.org/svn/llvm-project/lldb/trunk +Repository Root: https://johnny@llvm.org/svn/llvm-project +Repository UUID: 91177308-0d34-0410-b5e6-96231b3b80d8 +Revision: 154753 +Node Kind: directory +Schedule: normal +Last Changed Author: gclayton +Last Changed Rev: 154730 +Last Changed Date: 2012-04-13 18:42:46 -0700 (Fri, 13 Apr 2012) + + +lldb.pre_flight: def pre_flight(test): + __import__("lldb") + __import__("lldbtest") + print "\nRunning pre-flight function:" + print "for test case:", test + +lldb.post_flight: def post_flight(test): + __import__("lldb") + __import__("lldbtest") + print "\nRunning post-flight function:" + print "for test case:", test + + +Session logs for test failures/errors/unexpected successes will go into directory '2012-04-16-11_34_08' +Command invoked: python ./dotest.py -A x86_64 -v -c ../examples/test/.lldb-pre-post-flight functionalities/watchpoint/hello_watchpoint +compilers=['clang'] + +Configuration: arch=x86_64 compiler=clang +---------------------------------------------------------------------- +Collected 2 tests + +1: test_hello_watchpoint_with_dsym_using_watchpoint_set (TestMyFirstWatchpoint.HelloWatchpointTestCase) + Test a simple sequence of watchpoint creation and watchpoint hit. ... +Running pre-flight function: +for test case: test_hello_watchpoint_with_dsym_using_watchpoint_set (TestMyFirstWatchpoint.HelloWatchpointTestCase) + +Running post-flight function: +for test case: test_hello_watchpoint_with_dsym_using_watchpoint_set (TestMyFirstWatchpoint.HelloWatchpointTestCase) +ok +2: test_hello_watchpoint_with_dwarf_using_watchpoint_set (TestMyFirstWatchpoint.HelloWatchpointTestCase) + Test a simple sequence of watchpoint creation and watchpoint hit. ... +Running pre-flight function: +for test case: test_hello_watchpoint_with_dwarf_using_watchpoint_set (TestMyFirstWatchpoint.HelloWatchpointTestCase) + +Running post-flight function: +for test case: test_hello_watchpoint_with_dwarf_using_watchpoint_set (TestMyFirstWatchpoint.HelloWatchpointTestCase) +ok + +---------------------------------------------------------------------- +Ran 2 tests in 1.584s + +OK
\ No newline at end of file |