Newer
Older
Johnny Chen
committed
"""
This LLDB module contains miscellaneous utilities.
Johnny Chen
committed
"""
import lldb
import StringIO
Johnny Chen
committed
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
# Find the full path to a program, or return None.
def which(program):
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# ===========================================
# Iterator for lldb aggregate data structures
# ===========================================
def lldb_iter(obj, getsize, getelem):
"""A generator adaptor for lldb aggregate data structures.
API clients pass in an aggregate object or a container of it, the name of
the method to get the size of the aggregate, and the name of the method to
get the element by index.
Example usages:
1. Pass an aggregate as the first argument:
def disassemble_instructions (insts):
from lldbutil import lldb_iter
for i in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'):
print i
2. Pass a container of aggregate which provides APIs to get to the size and
the element of the aggregate:
# Module is a container of symbol table
module = target.FindModule(filespec)
for symbol in lldb_iter(module, 'GetNumSymbols', 'GetSymbolAtIndex'):
name = symbol.GetName()
...
"""
size = getattr(obj, getsize)
elem = getattr(obj, getelem)
for i in range(size()):
yield elem(i)
Johnny Chen
committed
# ===================================================
# Disassembly for an SBFunction or an SBSymbol object
# ===================================================
def disassemble(target, function_or_symbol):
"""Disassemble the function or symbol given a target.
It returns the disassembly content in a string object.
"""
buf = StringIO.StringIO()
insts = function_or_symbol.GetInstructions(target)
for i in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'):
print >> buf, i
return buf.getvalue()
# ==========================================================
# Integer (byte size 1, 2, 4, and 8) to bytearray conversion
# ==========================================================
def int_to_bytearray(val, bytesize):
"""Utility function to convert an integer into a bytearray.
It returns the bytearray in the little endian format. It is easy to get the
big endian format, just do ba.reverse() on the returned object.
"""
from struct import *
if bytesize == 1:
return bytearray([val])
# Little endian followed by a format character.
template = "<%c"
if bytesize == 2:
fmt = template % 'h'
elif bytesize == 4:
fmt = template % 'i'
elif bytesize == 4:
fmt = template % 'q'
else:
return None
packed = pack(fmt, val)
return bytearray(map(ord, packed))
def bytearray_to_int(bytes, bytesize):
"""Utility function to convert a bytearray into an integer.
It interprets the bytearray in the little endian format. For a big endian
bytearray, just do ba.reverse() on the object before passing it in.
"""
from struct import *
if bytesize == 1:
return ba[0]
# Little endian followed by a format character.
template = "<%c"
if bytesize == 2:
fmt = template % 'h'
elif bytesize == 4:
fmt = template % 'i'
elif bytesize == 4:
fmt = template % 'q'
else:
return None
unpacked = unpack(fmt, str(bytes))
return unpacked[0]
# ===========================================================
# Returns the list of stopped thread(s) given an lldb process
# ===========================================================
def get_stopped_threads(process, reason):
"""Returns the thread(s) with the specified stop reason in a list."""
threads = []
for t in lldb_iter(process, 'GetNumThreads', 'GetThreadAtIndex'):
if t.GetStopReason() == reason:
threads.append(t)
return threads
Johnny Chen
committed
def get_stopped_thread(process, reason):
"""A convenience function which returns the first thread with the given stop
reason or None.
Johnny Chen
committed
Johnny Chen
committed
Example usages:
Johnny Chen
committed
1. Get the stopped thread due to a breakpoint condition
Johnny Chen
committed
...
from lldbutil import get_stopped_thread
thread = get_stopped_thread(self.process, lldb.eStopReasonPlanComplete)
self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
...
Johnny Chen
committed
Johnny Chen
committed
...
from lldbutil import get_stopped_thread
thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
...
Johnny Chen
committed
"""
threads = get_stopped_threads(process, reason)
if len(threads) == 0:
return None
return threads[0]
Johnny Chen
committed
# =================================================
# Convert some enum value to its string counterpart
# =================================================
def StateTypeString(enum):
"""Returns the stateType string given an enum."""
if enum == lldb.eStateInvalid:
Johnny Chen
committed
return "invalid"
elif enum == lldb.eStateUnloaded:
Johnny Chen
committed
return "unloaded"
Johnny Chen
committed
elif enum == lldb.eStateConnected:
return "connected"
elif enum == lldb.eStateAttaching:
Johnny Chen
committed
return "attaching"
elif enum == lldb.eStateLaunching:
Johnny Chen
committed
return "launching"
elif enum == lldb.eStateStopped:
Johnny Chen
committed
return "stopped"
elif enum == lldb.eStateRunning:
Johnny Chen
committed
return "running"
elif enum == lldb.eStateStepping:
Johnny Chen
committed
return "stepping"
elif enum == lldb.eStateCrashed:
Johnny Chen
committed
return "crashed"
elif enum == lldb.eStateDetached:
Johnny Chen
committed
return "detached"
elif enum == lldb.eStateExited:
Johnny Chen
committed
return "exited"
elif enum == lldb.eStateSuspended:
Johnny Chen
committed
return "suspended"
else:
Johnny Chen
committed
raise Exception("Unknown StateType enum")
def StopReasonString(enum):
"""Returns the stopReason string given an enum."""
if enum == lldb.eStopReasonInvalid:
Johnny Chen
committed
return "invalid"
elif enum == lldb.eStopReasonNone:
Johnny Chen
committed
return "none"
elif enum == lldb.eStopReasonTrace:
Johnny Chen
committed
return "trace"
elif enum == lldb.eStopReasonBreakpoint:
Johnny Chen
committed
return "breakpoint"
elif enum == lldb.eStopReasonWatchpoint:
Johnny Chen
committed
return "watchpoint"
elif enum == lldb.eStopReasonSignal:
Johnny Chen
committed
return "signal"
elif enum == lldb.eStopReasonException:
Johnny Chen
committed
return "exception"
elif enum == lldb.eStopReasonPlanComplete:
Johnny Chen
committed
return "plancomplete"
else:
Johnny Chen
committed
raise Exception("Unknown StopReason enum")
Johnny Chen
committed
def ValueTypeString(enum):
"""Returns the valueType string given an enum."""
if enum == lldb.eValueTypeInvalid:
return "invalid"
elif enum == lldb.eValueTypeVariableGlobal:
return "global_variable"
elif enum == lldb.eValueTypeVariableStatic:
return "static_variable"
elif enum == lldb.eValueTypeVariableArgument:
return "argument_variable"
elif enum == lldb.eValueTypeVariableLocal:
return "local_variable"
elif enum == lldb.eValueTypeRegister:
return "register"
elif enum == lldb.eValueTypeRegisterSet:
return "register_set"
elif enum == lldb.eValueTypeConstResult:
return "constant_result"
else:
Johnny Chen
committed
raise Exception("Unknown ValueType enum")
Johnny Chen
committed
# ==================================================
# Utility functions related to Threads and Processes
# ==================================================
Johnny Chen
committed
def get_caller_symbol(thread):
"""
Returns the symbol name for the call site of the leaf function.
"""
depth = thread.GetNumFrames()
if depth <= 1:
return None
caller = thread.GetFrameAtIndex(1).GetSymbol()
if caller:
return caller.GetName()
else:
return None
Johnny Chen
committed
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()))
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()))
Johnny Chen
committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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()))
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()))
def PrintStackTrace(thread, string_buffer = False):
Johnny Chen
committed
"""Prints a simple stack trace of this thread."""
output = StringIO.StringIO() if string_buffer else sys.stdout
target = thread.GetProcess().GetTarget()
Johnny Chen
committed
depth = thread.GetNumFrames()
mods = GetModuleNames(thread)
funcs = GetFunctionNames(thread)
symbols = GetSymbolNames(thread)
Johnny Chen
committed
files = GetFilenames(thread)
lines = GetLineNumbers(thread)
addrs = GetPCAddresses(thread)
if thread.GetStopReason() != lldb.eStopReasonInvalid:
desc = "stop reason=" + StopReasonString(thread.GetStopReason())
else:
desc = ""
print >> output, "Stack trace for thread id={0:#x} name={1} queue={2} ".format(
thread.GetThreadID(), thread.GetName(), thread.GetQueueName()) + desc
Johnny Chen
committed
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:
return output.getvalue()
def PrintStackTraces(process, string_buffer = False):
"""Prints the stack traces of all the threads."""
output = StringIO.StringIO() if string_buffer else sys.stdout
print >> output, "Stack traces for " + repr(process)
Johnny Chen
committed
for i in range(process.GetNumThreads()):
print >> output, PrintStackTrace(process.GetThreadAtIndex(i), string_buffer=True)
if string_buffer:
return output.getvalue()