Modify the test suite and lldbutil.py to utilize the Python iteration pattern now that

the lldb iteration protocol has been added to lldb.py module.

llvm-svn: 130452
This commit is contained in:
Johnny Chen 2011-04-28 22:57:01 +00:00
parent 8409bce4ac
commit e69c748328
6 changed files with 8 additions and 92 deletions

View File

@ -101,8 +101,7 @@ class StaticVariableTestCase(TestBase):
# in_scope_only => False
valList = frame.GetVariables(False, False, True, False)
from lldbutil import lldb_iter
for val in lldb_iter(valList, 'GetSize', 'GetValueAtIndex'):
for val in valList:
self.DebugSBValue(frame, val)
self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal)
name = val.GetName()

View File

@ -103,9 +103,8 @@ class IterateFrameAndDisassembleTestCase(TestBase):
if function.IsValid():
# Get all instructions for this function and print them out.
insts = function.GetInstructions(target)
from lldbutil import lldb_iter
for inst in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'):
# It could simply be 'print inst' to print out the disassembly.
for inst in insts:
# We could simply do 'print inst' to print out the disassembly.
# But we want to print to stdout only if self.TraceOn() is True.
disasm = str(inst)
if self.TraceOn():

View File

@ -60,8 +60,7 @@ class FoundationSymtabTestCase(TestBase):
# Create the set of known symbols. As we iterate through the symbol
# table, remove the symbol from the set if it is a known symbol.
expected_symbols = set(self.symbols_list)
from lldbutil import lldb_iter
for symbol in lldb_iter(module, 'GetNumSymbols', 'GetSymbolAtIndex'):
for symbol in module:
self.assertTrue(symbol.IsValid(), VALID_SYMBOL)
#print "symbol:", symbol
name = symbol.GetName()

View File

@ -27,85 +27,6 @@ def which(program):
return exe_file
return None
# ===========================================
# 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()
...
"""
#import traceback
#traceback.print_stack()
size = getattr(obj, getsize)
elem = getattr(obj, getelem)
for i in range(size()):
yield elem(i)
def smart_iter(obj):
"""Returns an iterator for eligible lldb objects, or None otherwise.
An example of eligible lldb container object is SBModule, which contains
SBSymbols. While SBTarget contains SBModules and SBBreakpoints, because it
is ambiguous which containee type to iterate on, the best we can do is to
return None. API clients can use lldb_iter() to clarify their intentions.
SBSymbol does not have the notion of containee objects and is not eligible
for smart iterator.
Example usage:
from lldb_util import smart_iter
for thread in smart_iter(process):
ID = thread.GetThreadID()
if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
stopped_due_to_breakpoint = True
for frame in smart_iter(thread):
self.assertTrue(frame.GetThread().GetThreadID() == ID)
...
"""
d = { lldb.SBBreakpoint: ('GetNumLocations', 'GetLocationAtIndex'),
lldb.SBCompileUnit: ('GetNumLineEntries', 'GetLineEntryAtIndex'),
lldb.SBDebugger: ('GetNumTargets', 'GetTargetAtIndex'),
lldb.SBModule: ('GetNumSymbols', 'GetSymbolAtIndex'),
lldb.SBProcess: ('GetNumThreads', 'GetThreadAtIndex'),
lldb.SBThread: ('GetNumFrames', 'GetFrameAtIndex'),
lldb.SBInstructionList: ('GetSize', 'GetInstructionAtIndex'),
lldb.SBStringList: ('GetSize', 'GetStringAtIndex',),
lldb.SBSymbolContextList: ('GetSize', 'GetContextAtIndex'),
lldb.SBValueList: ('GetSize', 'GetValueAtIndex'),
lldb.SBType: ('GetNumberChildren', 'GetChildAtIndex'),
lldb.SBValue: ('GetNumChildren', 'GetChildAtIndex')
}
if obj.__class__ in d:
val = d.get(obj.__class__)
return lldb_iter(obj, val[0], val[1])
else:
return None
# ===================================================
# Disassembly for an SBFunction or an SBSymbol object
# ===================================================
@ -117,7 +38,7 @@ def disassemble(target, function_or_symbol):
"""
buf = StringIO.StringIO()
insts = function_or_symbol.GetInstructions(target)
for i in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'):
for i in insts:
print >> buf, i
return buf.getvalue()
@ -289,7 +210,7 @@ def value_type_to_str(enum):
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'):
for t in process:
if t.GetStopReason() == reason:
threads.append(t)
return threads

View File

@ -80,8 +80,7 @@ class FrameAPITestCase(TestBase):
# in_scope_only => True
valList = frame.GetVariables(True, False, False, True)
argList = []
from lldbutil import lldb_iter
for val in lldb_iter(valList, 'GetSize', 'GetValueAtIndex'):
for val in valList:
#self.DebugSBValue(frame, val)
argList.append("(%s)%s=%s" % (val.GetTypeName(),
val.GetName(),

View File

@ -1,6 +1,5 @@
"""
Test lldb_iter/smart_iter() which returns an iterator object for lldb container
objects.
Test the iteration protocol for some lldb container objects.
"""
import os, time