2010-10-06 11:53:16 +08:00
|
|
|
#!/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
|
2011-05-26 06:01:16 +08:00
|
|
|
import signal
|
2010-10-06 11:53:16 +08:00
|
|
|
|
|
|
|
def disassemble_instructions (insts):
|
2011-04-29 07:26:17 +08:00
|
|
|
for i in insts:
|
|
|
|
print i
|
2010-10-06 11:53:16 +08:00
|
|
|
|
|
|
|
# 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
|
2010-10-07 12:19:01 +08:00
|
|
|
print "Creating a target for '%s'" % sys.argv[1]
|
|
|
|
|
|
|
|
target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
|
2010-10-06 11:53:16 +08:00
|
|
|
|
2011-05-26 04:48:29 +08:00
|
|
|
if target:
|
2010-10-06 11:53:16 +08:00
|
|
|
# If the target is valid set a breakpoint at main
|
2011-03-30 09:55:23 +08:00
|
|
|
main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename());
|
2010-10-07 12:19:01 +08:00
|
|
|
|
|
|
|
print main_bp
|
|
|
|
|
2010-10-06 11:53:16 +08:00
|
|
|
# Launch the process. Since we specified synchronous mode, we won't return
|
|
|
|
# from this function until we hit the breakpoint at main
|
2011-05-26 04:56:32 +08:00
|
|
|
process = target.LaunchSimple (None, None, os.getcwd())
|
2010-10-06 11:53:16 +08:00
|
|
|
|
|
|
|
# Make sure the launch went ok
|
2011-05-26 04:48:29 +08:00
|
|
|
if process:
|
2010-10-06 11:53:16 +08:00
|
|
|
# Print some simple process info
|
2010-10-07 12:19:01 +08:00
|
|
|
state = process.GetState ()
|
|
|
|
print process
|
|
|
|
if state == lldb.eStateStopped:
|
|
|
|
# Get the first thread
|
|
|
|
thread = process.GetThreadAtIndex (0)
|
2011-05-26 04:48:29 +08:00
|
|
|
if thread:
|
2010-10-07 12:19:01 +08:00
|
|
|
# Print some simple thread info
|
|
|
|
print thread
|
|
|
|
# Get the first frame
|
|
|
|
frame = thread.GetFrameAtIndex (0)
|
2011-05-26 04:48:29 +08:00
|
|
|
if frame:
|
2010-10-07 12:19:01 +08:00
|
|
|
# Print some simple frame info
|
|
|
|
print frame
|
|
|
|
function = frame.GetFunction()
|
|
|
|
# See if we have debug info (a function)
|
2011-05-26 04:48:29 +08:00
|
|
|
if function:
|
2010-10-07 12:19:01 +08:00
|
|
|
# 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)
|
2010-10-06 11:53:16 +08:00
|
|
|
disassemble_instructions (insts)
|
2010-10-07 12:19:01 +08:00
|
|
|
else:
|
|
|
|
# See if we have a symbol in the symbol table for where we stopped
|
|
|
|
symbol = frame.GetSymbol();
|
2011-05-26 04:48:29 +08:00
|
|
|
if symbol:
|
2010-10-07 12:19:01 +08:00
|
|
|
# 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)
|
2011-03-30 09:55:23 +08:00
|
|
|
|
|
|
|
registerList = frame.GetRegisters()
|
2011-04-29 07:26:17 +08:00
|
|
|
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(frame)
|
2011-03-30 09:55:23 +08:00
|
|
|
|
2011-05-26 06:01:16 +08:00
|
|
|
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
|
2010-10-07 12:19:01 +08:00
|
|
|
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()
|
|
|
|
|
2010-10-06 11:53:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
lldb.SBDebugger.Terminate()
|