2010-09-09 06:54:46 +08:00
|
|
|
"""
|
2010-10-08 05:38:28 +08:00
|
|
|
This LLDB module contains miscellaneous utilities.
|
2010-09-09 06:54:46 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import lldb
|
2010-10-08 02:52:48 +08:00
|
|
|
import sys
|
2010-10-16 07:33:18 +08:00
|
|
|
import StringIO
|
2010-09-09 06:54:46 +08:00
|
|
|
|
2010-10-15 09:18:29 +08:00
|
|
|
################################################
|
|
|
|
# #
|
|
|
|
# Iterator for lldb aggregate data structures. #
|
|
|
|
# #
|
|
|
|
################################################
|
|
|
|
|
2010-10-11 04:25:10 +08:00
|
|
|
def lldb_iter(obj, getsize, getelem):
|
2010-10-09 09:31:09 +08:00
|
|
|
"""
|
|
|
|
A generator adaptor for lldb aggregate data structures.
|
|
|
|
|
2010-10-11 04:25:10 +08:00
|
|
|
API clients pass in the aggregate object, the name of the method to get the
|
|
|
|
size of the object, and the name of the method to get the element given an
|
|
|
|
index.
|
2010-10-09 09:31:09 +08:00
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
def disassemble_instructions (insts):
|
2010-10-11 04:25:10 +08:00
|
|
|
from lldbutil import lldb_iter
|
|
|
|
for i in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'):
|
2010-10-09 09:31:09 +08:00
|
|
|
print i
|
|
|
|
"""
|
2010-10-11 04:25:10 +08:00
|
|
|
size = getattr(obj, getsize)
|
|
|
|
elem = getattr(obj, getelem)
|
|
|
|
for i in range(size()):
|
|
|
|
yield elem(i)
|
2010-10-09 09:31:09 +08:00
|
|
|
|
|
|
|
|
2010-10-08 06:15:58 +08:00
|
|
|
########################################################
|
|
|
|
# #
|
|
|
|
# Convert some enum value to its string's counterpart. #
|
|
|
|
# #
|
|
|
|
########################################################
|
|
|
|
|
|
|
|
def StateTypeString(enum):
|
|
|
|
"""Returns the stateType string given an enum."""
|
|
|
|
if enum == lldb.eStateInvalid:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "invalid"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateUnloaded:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "unloaded"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateAttaching:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "attaching"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateLaunching:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "launching"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateStopped:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "stopped"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateRunning:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "running"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateStepping:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "stepping"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateCrashed:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "crashed"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateDetached:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "detached"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateExited:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "exited"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStateSuspended:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "suspended"
|
2010-10-08 06:15:58 +08:00
|
|
|
else:
|
|
|
|
raise Exception("Unknown stopReason enum")
|
|
|
|
|
|
|
|
def StopReasonString(enum):
|
|
|
|
"""Returns the stopReason string given an enum."""
|
|
|
|
if enum == lldb.eStopReasonInvalid:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "invalid"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStopReasonNone:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "none"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStopReasonTrace:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "trace"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStopReasonBreakpoint:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "breakpoint"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStopReasonWatchpoint:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "watchpoint"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStopReasonSignal:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "signal"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStopReasonException:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "exception"
|
2010-10-08 06:15:58 +08:00
|
|
|
elif enum == lldb.eStopReasonPlanComplete:
|
2010-10-18 23:46:54 +08:00
|
|
|
return "plancomplete"
|
2010-10-08 06:15:58 +08:00
|
|
|
else:
|
|
|
|
raise Exception("Unknown stopReason enum")
|
|
|
|
|
|
|
|
|
|
|
|
#######################################################
|
|
|
|
# #
|
|
|
|
# Utility functions related to Threads and Processes. #
|
|
|
|
# #
|
|
|
|
#######################################################
|
|
|
|
|
2010-09-09 06:54:46 +08:00
|
|
|
def GetFunctionNames(thread):
|
|
|
|
"""
|
|
|
|
Returns a sequence of function names from the stack frames of this thread.
|
|
|
|
"""
|
|
|
|
def GetFuncName(i):
|
|
|
|
return thread.GetFrameAtIndex(i).GetFunction().GetName()
|
|
|
|
|
|
|
|
return map(GetFuncName, range(thread.GetNumFrames()))
|
|
|
|
|
|
|
|
|
2010-10-08 05:38:28 +08:00
|
|
|
def GetSymbolNames(thread):
|
|
|
|
"""
|
|
|
|
Returns a sequence of symbols for this thread.
|
|
|
|
"""
|
|
|
|
def GetSymbol(i):
|
|
|
|
return thread.GetFrameAtIndex(i).GetSymbol().GetName()
|
|
|
|
|
|
|
|
return map(GetSymbol, range(thread.GetNumFrames()))
|
|
|
|
|
|
|
|
|
|
|
|
def GetPCAddresses(thread):
|
|
|
|
"""
|
|
|
|
Returns a sequence of pc addresses for this thread.
|
|
|
|
"""
|
|
|
|
def GetPCAddress(i):
|
|
|
|
return thread.GetFrameAtIndex(i).GetPCAddress()
|
|
|
|
|
|
|
|
return map(GetPCAddress, range(thread.GetNumFrames()))
|
|
|
|
|
|
|
|
|
2010-09-09 06:54:46 +08:00
|
|
|
def GetFilenames(thread):
|
|
|
|
"""
|
|
|
|
Returns a sequence of file names from the stack frames of this thread.
|
|
|
|
"""
|
|
|
|
def GetFilename(i):
|
|
|
|
return thread.GetFrameAtIndex(i).GetLineEntry().GetFileSpec().GetFilename()
|
|
|
|
|
|
|
|
return map(GetFilename, range(thread.GetNumFrames()))
|
|
|
|
|
|
|
|
|
|
|
|
def GetLineNumbers(thread):
|
|
|
|
"""
|
|
|
|
Returns a sequence of line numbers from the stack frames of this thread.
|
|
|
|
"""
|
|
|
|
def GetLineNumber(i):
|
|
|
|
return thread.GetFrameAtIndex(i).GetLineEntry().GetLine()
|
|
|
|
|
|
|
|
return map(GetLineNumber, range(thread.GetNumFrames()))
|
|
|
|
|
|
|
|
|
|
|
|
def GetModuleNames(thread):
|
|
|
|
"""
|
|
|
|
Returns a sequence of module names from the stack frames of this thread.
|
|
|
|
"""
|
|
|
|
def GetModuleName(i):
|
|
|
|
return thread.GetFrameAtIndex(i).GetModule().GetFileSpec().GetFilename()
|
|
|
|
|
|
|
|
return map(GetModuleName, range(thread.GetNumFrames()))
|
|
|
|
|
|
|
|
|
2010-09-09 08:55:07 +08:00
|
|
|
def GetStackFrames(thread):
|
|
|
|
"""
|
|
|
|
Returns a sequence of stack frames for this thread.
|
|
|
|
"""
|
|
|
|
def GetStackFrame(i):
|
|
|
|
return thread.GetFrameAtIndex(i)
|
|
|
|
|
|
|
|
return map(GetStackFrame, range(thread.GetNumFrames()))
|
|
|
|
|
|
|
|
|
2010-10-08 02:52:48 +08:00
|
|
|
def PrintStackTrace(thread, string_buffer = False):
|
2010-09-09 06:54:46 +08:00
|
|
|
"""Prints a simple stack trace of this thread."""
|
2010-10-08 02:52:48 +08:00
|
|
|
|
2010-10-16 07:33:18 +08:00
|
|
|
output = StringIO.StringIO() if string_buffer else sys.stdout
|
2010-10-08 05:38:28 +08:00
|
|
|
target = thread.GetProcess().GetTarget()
|
|
|
|
|
2010-09-09 06:54:46 +08:00
|
|
|
depth = thread.GetNumFrames()
|
|
|
|
|
|
|
|
mods = GetModuleNames(thread)
|
|
|
|
funcs = GetFunctionNames(thread)
|
2010-10-08 05:38:28 +08:00
|
|
|
symbols = GetSymbolNames(thread)
|
2010-09-09 06:54:46 +08:00
|
|
|
files = GetFilenames(thread)
|
|
|
|
lines = GetLineNumbers(thread)
|
2010-10-08 05:38:28 +08:00
|
|
|
addrs = GetPCAddresses(thread)
|
2010-10-08 02:52:48 +08:00
|
|
|
|
|
|
|
print >> output, "Stack trace for thread id={0:#x} name={1} queue={2}:".format(
|
2010-09-09 06:54:46 +08:00
|
|
|
thread.GetThreadID(), thread.GetName(), thread.GetQueueName())
|
|
|
|
|
2010-10-08 05:38:28 +08:00
|
|
|
for i in range(depth):
|
|
|
|
frame = thread.GetFrameAtIndex(i)
|
|
|
|
function = frame.GetFunction()
|
|
|
|
|
|
|
|
load_addr = addrs[i].GetLoadAddress(target)
|
|
|
|
if not function.IsValid():
|
|
|
|
file_addr = addrs[i].GetFileAddress()
|
|
|
|
print >> output, " frame #{num}: {addr:#016x} {mod}`{symbol} + ????".format(
|
|
|
|
num=i, addr=load_addr, mod=mods[i], symbol=symbols[i])
|
|
|
|
else:
|
|
|
|
print >> output, " frame #{num}: {addr:#016x} {mod}`{func} at {file}:{line}".format(
|
|
|
|
num=i, addr=load_addr, mod=mods[i], func=funcs[i], file=files[i], line=lines[i])
|
|
|
|
|
|
|
|
if string_buffer:
|
2010-10-16 07:33:18 +08:00
|
|
|
return output.getvalue()
|
2010-10-08 05:38:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
def PrintStackTraces(process, string_buffer = False):
|
|
|
|
"""Prints the stack traces of all the threads."""
|
|
|
|
|
2010-10-16 07:33:18 +08:00
|
|
|
output = StringIO.StringIO() if string_buffer else sys.stdout
|
2010-10-08 05:38:28 +08:00
|
|
|
|
|
|
|
print >> output, "Stack traces for " + repr(process)
|
2010-09-09 06:54:46 +08:00
|
|
|
|
2010-10-08 05:38:28 +08:00
|
|
|
for i in range(process.GetNumThreads()):
|
|
|
|
print >> output, PrintStackTrace(process.GetThreadAtIndex(i), string_buffer=True)
|
2010-10-08 02:52:48 +08:00
|
|
|
|
|
|
|
if string_buffer:
|
2010-10-16 07:33:18 +08:00
|
|
|
return output.getvalue()
|