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()
debugger = lldb.SBDebugger.Create()
debugger.SetAsync (False)
print "Creating a target for '%s'" % exe
target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT)
if target:
main_bp = target.BreakpointCreateByName (fname, target.GetExecutable().GetFilename());
print main_bp
process = target.LaunchSimple (None, None, os.getcwd())
if process:
state = process.GetState ()
print process
if state == lldb.eStateStopped:
thread = process.GetThreadAtIndex (0)
if thread:
print thread
frame = thread.GetFrameAtIndex (0)
if frame:
print frame
function = frame.GetFunction()
if function:
print function
insts = function.GetInstructions(target)
disassemble_instructions (insts)
else:
symbol = frame.GetSymbol();
if symbol:
print symbol
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 "%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:
process.Continue()
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()