2011-03-29 06:40:32 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
Run lldb to disassemble all the available functions for an executable image.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
def setupSysPath():
|
|
|
|
"""
|
2011-03-29 06:48:25 +08:00
|
|
|
Add LLDB.framework/Resources/Python and the test dir to the sys.path.
|
2011-03-29 06:40:32 +08:00
|
|
|
"""
|
|
|
|
# Get the directory containing the current script.
|
|
|
|
scriptPath = sys.path[0]
|
|
|
|
if not scriptPath.endswith(os.path.join('utils', 'test')):
|
|
|
|
print "This script expects to reside in lldb's utils/test directory."
|
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
# This is our base name component.
|
|
|
|
base = os.path.abspath(os.path.join(scriptPath, os.pardir, os.pardir))
|
|
|
|
|
|
|
|
# This is for the goodies in the test directory under base.
|
|
|
|
sys.path.append(os.path.join(base,'test'))
|
|
|
|
|
|
|
|
# These are for xcode build directories.
|
|
|
|
xcode3_build_dir = ['build']
|
|
|
|
xcode4_build_dir = ['build', 'lldb', 'Build', 'Products']
|
|
|
|
dbg = ['Debug']
|
|
|
|
rel = ['Release']
|
|
|
|
bai = ['BuildAndIntegration']
|
|
|
|
python_resource_dir = ['LLDB.framework', 'Resources', 'Python']
|
|
|
|
|
|
|
|
dbgPath = os.path.join(base, *(xcode3_build_dir + dbg + python_resource_dir))
|
|
|
|
dbgPath2 = os.path.join(base, *(xcode4_build_dir + dbg + python_resource_dir))
|
|
|
|
relPath = os.path.join(base, *(xcode3_build_dir + rel + python_resource_dir))
|
|
|
|
relPath2 = os.path.join(base, *(xcode4_build_dir + rel + python_resource_dir))
|
|
|
|
baiPath = os.path.join(base, *(xcode3_build_dir + bai + python_resource_dir))
|
|
|
|
baiPath2 = os.path.join(base, *(xcode4_build_dir + bai + python_resource_dir))
|
|
|
|
|
|
|
|
lldbPath = None
|
|
|
|
if os.path.isfile(os.path.join(dbgPath, 'lldb.py')):
|
|
|
|
lldbPath = dbgPath
|
|
|
|
elif os.path.isfile(os.path.join(dbgPath2, 'lldb.py')):
|
|
|
|
lldbPath = dbgPath2
|
|
|
|
elif os.path.isfile(os.path.join(relPath, 'lldb.py')):
|
|
|
|
lldbPath = relPath
|
|
|
|
elif os.path.isfile(os.path.join(relPath2, 'lldb.py')):
|
|
|
|
lldbPath = relPath2
|
|
|
|
elif os.path.isfile(os.path.join(baiPath, 'lldb.py')):
|
|
|
|
lldbPath = baiPath
|
|
|
|
elif os.path.isfile(os.path.join(baiPath2, 'lldb.py')):
|
|
|
|
lldbPath = baiPath2
|
|
|
|
|
|
|
|
if not lldbPath:
|
|
|
|
print 'This script requires lldb.py to be in either ' + dbgPath + ',',
|
|
|
|
print relPath + ', or ' + baiPath
|
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
# This is to locate the lldb.py module. Insert it right after sys.path[0].
|
|
|
|
sys.path[1:1] = [lldbPath]
|
|
|
|
print "sys.path:", sys.path
|
|
|
|
|
|
|
|
|
2011-03-29 09:07:00 +08:00
|
|
|
def run_command(ci, cmd, res, echoInput=True, echoOutput=True):
|
|
|
|
if echoInput:
|
|
|
|
print "run command:", cmd
|
2011-03-29 06:40:32 +08:00
|
|
|
ci.HandleCommand(cmd, res)
|
|
|
|
if res.Succeeded():
|
2011-03-29 09:07:00 +08:00
|
|
|
if echoOutput:
|
|
|
|
print "run_command output:", res.GetOutput()
|
2011-03-29 06:40:32 +08:00
|
|
|
else:
|
2011-03-29 09:07:00 +08:00
|
|
|
if echoOutput:
|
|
|
|
print "run command failed!"
|
|
|
|
print "run_command error:", res.GetError()
|
2011-03-29 06:40:32 +08:00
|
|
|
|
2011-08-19 06:04:27 +08:00
|
|
|
def do_lldb_disassembly(lldb_commands, exe, disassemble_options, num_symbols, symbols_to_disassemble, quiet_disassembly):
|
2011-03-31 09:06:28 +08:00
|
|
|
import lldb, atexit, re
|
2011-03-29 06:40:32 +08:00
|
|
|
|
|
|
|
# Create the debugger instance now.
|
|
|
|
dbg = lldb.SBDebugger.Create()
|
2011-05-26 04:47:27 +08:00
|
|
|
if not dbg:
|
2011-03-29 06:40:32 +08:00
|
|
|
raise Exception('Invalid debugger instance')
|
|
|
|
|
|
|
|
# Register an exit callback.
|
|
|
|
atexit.register(lambda: lldb.SBDebugger.Terminate())
|
|
|
|
|
|
|
|
# We want our debugger to be synchronous.
|
|
|
|
dbg.SetAsync(False)
|
|
|
|
|
|
|
|
# Get the command interpreter from the debugger.
|
|
|
|
ci = dbg.GetCommandInterpreter()
|
|
|
|
if not ci:
|
|
|
|
raise Exception('Could not get the command interpreter')
|
|
|
|
|
|
|
|
# And the associated result object.
|
|
|
|
res = lldb.SBCommandReturnObject()
|
|
|
|
|
|
|
|
# See if there any extra command(s) to execute before we issue the file command.
|
|
|
|
for cmd in lldb_commands:
|
|
|
|
run_command(ci, cmd, res)
|
|
|
|
|
2011-03-31 09:34:55 +08:00
|
|
|
# Now issue the file command.
|
|
|
|
run_command(ci, 'file %s' % exe, res)
|
|
|
|
|
2011-03-31 09:06:28 +08:00
|
|
|
# Create a target.
|
2011-03-31 09:34:55 +08:00
|
|
|
#target = dbg.CreateTarget(exe)
|
|
|
|
target = dbg.GetSelectedTarget()
|
2011-03-31 09:06:28 +08:00
|
|
|
stream = lldb.SBStream()
|
|
|
|
|
2011-03-31 09:34:55 +08:00
|
|
|
def IsCodeType(symbol):
|
|
|
|
"""Check whether an SBSymbol represents code."""
|
|
|
|
return symbol.GetType() == lldb.eSymbolTypeCode
|
|
|
|
|
2011-03-31 09:06:28 +08:00
|
|
|
# Define a generator for the symbols to disassemble.
|
2011-08-19 06:04:27 +08:00
|
|
|
def symbol_iter(num, symbols, target, verbose):
|
2011-03-31 09:06:28 +08:00
|
|
|
# If we specify the symbols to disassemble, ignore symbol table dump.
|
|
|
|
if symbols:
|
|
|
|
for i in range(len(symbols)):
|
|
|
|
print "symbol:", symbols[i]
|
|
|
|
yield symbols[i]
|
|
|
|
else:
|
|
|
|
limited = True if num != -1 else False
|
|
|
|
if limited:
|
|
|
|
count = 0
|
|
|
|
stream = lldb.SBStream()
|
2011-04-29 07:34:58 +08:00
|
|
|
for m in target.module_iter():
|
2011-03-31 09:06:28 +08:00
|
|
|
print "module:", m
|
2011-04-29 07:34:58 +08:00
|
|
|
for s in m:
|
2011-03-31 09:06:28 +08:00
|
|
|
if limited and count >= num:
|
|
|
|
return
|
|
|
|
print "symbol:", s.GetName()
|
|
|
|
if IsCodeType(s):
|
|
|
|
if limited:
|
|
|
|
count = count + 1
|
2011-08-19 06:04:27 +08:00
|
|
|
if verbose:
|
|
|
|
print "returning symbol:", s.GetName()
|
2011-03-31 09:06:28 +08:00
|
|
|
yield s.GetName()
|
2011-08-19 06:04:27 +08:00
|
|
|
if verbose:
|
|
|
|
print "start address:", s.GetStartAddress()
|
|
|
|
print "end address:", s.GetEndAddress()
|
|
|
|
s.GetDescription(stream)
|
|
|
|
print "symbol description:", stream.GetData()
|
|
|
|
stream.Clear()
|
2011-03-31 09:06:28 +08:00
|
|
|
|
2011-03-31 02:47:54 +08:00
|
|
|
# Disassembly time.
|
2011-08-19 06:04:27 +08:00
|
|
|
for symbol in symbol_iter(num_symbols, symbols_to_disassemble, target, not quiet_disassembly):
|
2011-03-31 02:47:54 +08:00
|
|
|
cmd = "disassemble %s '%s'" % (disassemble_options, symbol)
|
2011-08-19 06:04:27 +08:00
|
|
|
run_command(ci, cmd, res, True, not quiet_disassembly)
|
2011-03-29 09:07:00 +08:00
|
|
|
|
2011-03-29 06:40:32 +08:00
|
|
|
|
|
|
|
def main():
|
|
|
|
# This is to set up the Python path to include the pexpect-2.4 dir.
|
|
|
|
# Remember to update this when/if things change.
|
|
|
|
scriptPath = sys.path[0]
|
|
|
|
sys.path.append(os.path.join(scriptPath, os.pardir, os.pardir, 'test', 'pexpect-2.4'))
|
|
|
|
|
|
|
|
parser = OptionParser(usage="""\
|
|
|
|
Run lldb to disassemble all the available functions for an executable image.
|
|
|
|
|
|
|
|
Usage: %prog [options]
|
|
|
|
""")
|
|
|
|
parser.add_option('-C', '--lldb-command',
|
|
|
|
type='string', action='append', metavar='COMMAND',
|
|
|
|
default=[], dest='lldb_commands',
|
|
|
|
help='Command(s) lldb executes after starting up (can be empty)')
|
|
|
|
parser.add_option('-e', '--executable',
|
|
|
|
type='string', action='store',
|
|
|
|
dest='executable',
|
2011-03-29 09:07:00 +08:00
|
|
|
help="""Mandatory: the executable to do disassembly on.""")
|
|
|
|
parser.add_option('-o', '--options',
|
|
|
|
type='string', action='store',
|
|
|
|
dest='disassemble_options',
|
|
|
|
help="""Mandatory: the options passed to lldb's 'disassemble' command.""")
|
|
|
|
parser.add_option('-n', '--num-symbols',
|
2011-03-31 02:47:54 +08:00
|
|
|
type='int', action='store', default=-1,
|
2011-03-29 09:07:00 +08:00
|
|
|
dest='num_symbols',
|
|
|
|
help="""The number of symbols to disassemble, if specified.""")
|
2011-08-19 06:04:27 +08:00
|
|
|
parser.add_option('-q', '--quiet-disassembly',
|
|
|
|
action='store_true', default=False,
|
|
|
|
dest='quiet_disassembly',
|
|
|
|
help="""The symbol(s) to invoke lldb's 'disassemble' command on, if specified.""")
|
2011-03-31 02:47:54 +08:00
|
|
|
parser.add_option('-s', '--symbol',
|
|
|
|
type='string', action='append', metavar='SYMBOL', default=[],
|
|
|
|
dest='symbols_to_disassemble',
|
|
|
|
help="""The symbol(s) to invoke lldb's 'disassemble' command on, if specified.""")
|
|
|
|
|
2011-03-29 06:40:32 +08:00
|
|
|
opts, args = parser.parse_args()
|
|
|
|
|
|
|
|
lldb_commands = opts.lldb_commands
|
|
|
|
|
2011-03-29 09:07:00 +08:00
|
|
|
if not opts.executable or not opts.disassemble_options:
|
2011-03-29 06:40:32 +08:00
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|
2011-03-29 09:07:00 +08:00
|
|
|
|
2011-03-29 06:40:32 +08:00
|
|
|
executable = opts.executable
|
2011-03-29 09:07:00 +08:00
|
|
|
disassemble_options = opts.disassemble_options
|
2011-03-31 02:47:54 +08:00
|
|
|
num_symbols = opts.num_symbols
|
2011-08-19 06:04:27 +08:00
|
|
|
quiet_disassembly = opts.quiet_disassembly
|
2011-03-31 02:47:54 +08:00
|
|
|
symbols_to_disassemble = opts.symbols_to_disassemble
|
2011-03-29 06:40:32 +08:00
|
|
|
|
|
|
|
# We have parsed the options.
|
|
|
|
print "lldb commands:", lldb_commands
|
|
|
|
print "executable:", executable
|
2011-03-29 09:07:00 +08:00
|
|
|
print "disassemble options:", disassemble_options
|
|
|
|
print "num of symbols to disassemble:", num_symbols
|
2011-08-19 06:04:27 +08:00
|
|
|
print "quiet disassembly output:", quiet_disassembly
|
2011-03-31 02:47:54 +08:00
|
|
|
print "symbols to disassemble:", symbols_to_disassemble
|
2011-03-29 06:40:32 +08:00
|
|
|
|
|
|
|
setupSysPath()
|
2011-08-19 06:04:27 +08:00
|
|
|
do_lldb_disassembly(lldb_commands, executable, disassemble_options,
|
|
|
|
num_symbols,
|
|
|
|
symbols_to_disassemble,
|
|
|
|
quiet_disassembly)
|
2011-03-29 06:40:32 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|