Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@ -0,0 +1,122 @@
#!/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
class FrameStatCommand:
def create_options(self):
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.
'''
# Pass add_help_option = False, since this keeps the command in line with lldb commands,
# and we wire up "help command" to work by providing the long & short help methods below.
self.parser = optparse.OptionParser(
description = description,
prog = 'framestats',
usage = usage,
add_help_option = False)
self.parser.add_option(
'-i',
'--in-scope',
action = 'store_true',
dest = 'inscope',
help = 'in_scope_only = True',
default = True)
self.parser.add_option(
'-a',
'--arguments',
action = 'store_true',
dest = 'arguments',
help = 'arguments = True',
default = True)
self.parser.add_option(
'-l',
'--locals',
action = 'store_true',
dest = 'locals',
help = 'locals = True',
default = True)
self.parser.add_option(
'-s',
'--statics',
action = 'store_true',
dest = 'statics',
help = 'statics = True',
default = True)
def get_short_help(self):
return "Example command for use in debugging"
def get_long_help(self):
return self.help_string
def __init__(self, debugger, unused):
self.create_options()
self.help_string = self.parser.format_help()
def __call__(self, debugger, command, exe_ctx, result):
# Use the Shell Lexer to properly parse up command options just like a
# shell would
command_args = shlex.split(command)
try:
(options, args) = self.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
# Always get program state from the SBExecutionContext passed in as exe_ctx
frame = exe_ctx.GetFrame()
if not frame.IsValid():
result.SetError("invalid frame")
return
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
# Add any commands contained in this module to LLDB
debugger.HandleCommand(
'command script add -c cmdtemplate.FrameStatCommand framestats')
print 'The "framestats" command has been installed, type "help framestats" for detailed help.'

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,134 @@
#!/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'

View File

@ -0,0 +1,183 @@
# 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

View File

@ -0,0 +1,313 @@
# 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 is None:
module_uuid_str = ""
if sbaddr.GetModule().GetFileSpec():
module_filename = sbaddr.GetModule().GetFileSpec().GetFilename()
if module_filename is 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 is not 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 is not 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 is not 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 is not 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 is not None:
modules_seen += module_list
if address_list and address_list is not 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 is not None and module.GetFileSpec().GetFilename() is not 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.'

View File

@ -0,0 +1,62 @@
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 is None:
s = "%#8.8x" % self.value
return s
def __repr__(self):
return self.__str__()

View File

@ -0,0 +1,230 @@
#!/usr/bin/python
import argparse
import datetime
import re
import subprocess
import sys
import 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 is 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 is None:
print "Couldn't find LLDB.framework"
sys.exit(-1)
sys.path.append(lldb_framework_path + "/Resources/Python")
if arg_ns.lldb is 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 is 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 is None:
raise StopIteration
return ret
log_file = None
def GetProviderWithArguments(args):
global log_file
if args.log is not None:
log_file = open(args.log, 'w')
else:
log_file = sys.stdout
instruction_provider = None
if args.random:
instruction_provider = RandomInstructionProvider(args.bytes, log_file)
else:
start = 0
skip = 1
if args.start is not None:
start = args.start
if args.skip is not 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()

View File

@ -0,0 +1,126 @@
#!/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()

View File

@ -0,0 +1,48 @@
""" Adds the 'toggle-disassembly' command to switch you into a disassembly only mode """
import lldb
class DisassemblyMode:
def __init__(self, debugger, unused):
self.dbg = debugger
self.interp = debugger.GetCommandInterpreter()
self.store_state()
self.mode_off = True
def store_state(self):
self.dis_count = self.get_string_value("stop-disassembly-count")
self.dis_display = self.get_string_value("stop-disassembly-display")
self.before_count = self.get_string_value("stop-line-count-before")
self.after_count = self.get_string_value("stop-line-count-after")
def get_string_value(self, setting):
result = lldb.SBCommandReturnObject()
self.interp.HandleCommand("settings show " + setting, result)
value = result.GetOutput().split(" = ")[1].rstrip("\n")
return value
def set_value(self, setting, value):
result = lldb.SBCommandReturnObject()
self.interp.HandleCommand("settings set " + setting + " " + value, result)
def __call__(self, debugger, command, exe_ctx, result):
if self.mode_off:
self.mode_off = False
self.store_state()
self.set_value("stop-disassembly-display","always")
self.set_value("stop-disassembly-count", "8")
self.set_value("stop-line-count-before", "0")
self.set_value("stop-line-count-after", "0")
result.AppendMessage("Disassembly mode on.")
else:
self.mode_off = True
self.set_value("stop-disassembly-display",self.dis_display)
self.set_value("stop-disassembly-count", self.dis_count)
self.set_value("stop-line-count-before", self.before_count)
self.set_value("stop-line-count-after", self.after_count)
result.AppendMessage("Disassembly mode off.")
def get_short_help(self):
return "Toggles between a disassembly only mode and normal source mode\n"
def __lldb_init_module(debugger, unused):
debugger.HandleCommand("command script add -c disassembly_mode.DisassemblyMode toggle-disassembly")

View File

@ -0,0 +1,226 @@
#! /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

View File

@ -0,0 +1,26 @@
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'

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,106 @@
#!/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:
# returns the global variable name as a string
print 'name = %s' % global_variable.name
# Returns the variable value as a string
print 'value = %s' % global_variable.value
print 'type = %s' % global_variable.type # Returns an lldb.SBType object
# Returns an lldb.SBAddress (section offset
# address) for this global
print 'addr = %s' % global_variable.addr
# Returns the file virtual address for this
# global
print 'file_addr = 0x%x' % global_variable.addr.file_addr
# returns the global variable value as a string
print 'location = %s' % global_variable.location
# Returns the size in bytes of this global
# variable
print 'size = %s' % global_variable.size
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:])

View File

@ -0,0 +1,196 @@
import lldb
import 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 is not 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 is not 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 is not 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 is not 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 is not 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.'

View File

@ -0,0 +1,191 @@
#!/usr/bin/python
import lldb
import optparse
import shlex
import string
import sys
class DumpLineTables:
command_name = "dump-line-tables"
short_decription = "Dumps full paths to compile unit files and optionally all line table files."
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.'
usage = "usage: %prog [options] MODULE1 [MODULE2 ...]"
def create_options(self):
self.parser = optparse.OptionParser(
description=self.description,
prog=self.command_name,
usage=self.usage)
self.parser.add_option(
'-v',
'--verbose',
action='store_true',
dest='verbose',
help='Display verbose output.',
default=False)
def get_short_help(self):
return self.short_decription
def get_long_help(self):
return self.help_string
def __init__(self, debugger, unused):
self.create_options()
self.help_string = self.parser.format_help()
def __call__(self, debugger, command, exe_ctx, result):
# Use the Shell Lexer to properly parse up command options just like a
# shell would
command_args = shlex.split(command)
try:
(options, args) = self.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
# Always get program state from the SBExecutionContext passed in as exe_ctx
target = exe_ctx.GetTarget()
if not target.IsValid():
result.SetError("invalid target")
return
for module_path in args:
module = target.module[module_path]
if not module:
result.SetError('no module found that matches "%s".' % (module_path))
return
num_cus = module.GetNumCompileUnits()
print >>result, 'Module: "%s"' % (module.file.fullpath),
if num_cus == 0:
print >>result, 'no debug info.'
continue
print >>result, 'has %u compile units:' % (num_cus)
for cu_idx in range(num_cus):
cu = module.GetCompileUnitAtIndex(cu_idx)
print >>result, ' Compile Unit: %s' % (cu.file.fullpath)
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.PutCString("\n")
class DumpFiles:
command_name = "dump-files"
short_description = "Dumps full paths to compile unit files and optionally all line table files."
usage = "usage: %prog [options] MODULE1 [MODULE2 ...]"
description = '''This class adds a dump-files command to the LLDB interpreter.
This command will dump all compile unit file paths found for each source file
for the binaries specified as arguments in the current target. Specify the
--support-files or -s option to see all file paths that a compile unit uses in
its lines tables. This is handy for troubleshooting why breakpoints aren't
working in IDEs that specify full paths to source files when setting file and
line breakpoints. Sometimes symlinks cause the debug info to contain the symlink
path and an IDE will resolve the path to the actual file and use the resolved
path when setting breakpoints.
'''
def create_options(self):
# Pass add_help_option = False, since this keeps the command in line with lldb commands,
# and we wire up "help command" to work by providing the long & short help methods below.
self.parser = optparse.OptionParser(
description = self.description,
prog = self.command_name,
usage = self.usage,
add_help_option = False)
self.parser.add_option(
'-s',
'--support-files',
action = 'store_true',
dest = 'support_files',
help = 'Dumps full paths to all files used in a compile unit.',
default = False)
def get_short_help(self):
return self.short_description
def get_long_help(self):
return self.help_string
def __init__(self, debugger, unused):
self.create_options()
self.help_string = self.parser.format_help()
def __call__(self, debugger, command, exe_ctx, result):
# Use the Shell Lexer to properly parse up command options just like a
# shell would
command_args = shlex.split(command)
try:
(options, args) = self.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
# Always get program state from the SBExecutionContext passed in as exe_ctx
target = exe_ctx.GetTarget()
if not target.IsValid():
result.SetError("invalid target")
return
if len(args) == 0:
result.SetError("one or more executable paths must be specified")
return
for module_path in args:
module = target.module[module_path]
if not module:
result.SetError('no module found that matches "%s".' % (module_path))
return
num_cus = module.GetNumCompileUnits()
print >>result, 'Module: "%s"' % (module.file.fullpath),
if num_cus == 0:
print >>result, 'no debug info.'
continue
print >>result, 'has %u compile units:' % (num_cus)
for i in range(num_cus):
cu = module.GetCompileUnitAtIndex(i)
print >>result, ' Compile Unit: %s' % (cu.file.fullpath)
if options.support_files:
num_support_files = cu.GetNumSupportFiles()
for j in range(num_support_files):
path = cu.GetSupportFileAtIndex(j).fullpath
print >>result, ' file[%u]: %s' % (j, path)
def __lldb_init_module(debugger, dict):
# This initializer is being run from LLDB in the embedded command interpreter
# Add any commands contained in this module to LLDB
debugger.HandleCommand(
'command script add -c %s.DumpLineTables %s' % (__name__,
DumpLineTables.command_name))
debugger.HandleCommand(
'command script add -c %s.DumpFiles %s' % (__name__, DumpFiles.command_name))
print 'The "%s" and "%s" commands have been installed.' % (DumpLineTables.command_name,
DumpFiles.command_name)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,276 @@
#!/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'

View File

@ -0,0 +1,231 @@
#!/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 isinstance(process, 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 is 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

View File

@ -0,0 +1,392 @@
#!/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)

Some files were not shown because too many files have changed in this diff Show More