Added a test case to bitfields which uses the Python APIs from lldb.py.

Added a utility method to TestBase class to debug print an SBValue object.

llvm-svn: 112247
This commit is contained in:
Johnny Chen 2010-08-27 00:15:48 +00:00
parent f5bae22db7
commit 827edffafd
3 changed files with 81 additions and 0 deletions

View File

@ -77,34 +77,42 @@ class TestArrayTypes(TestBase):
# Lookup the "strings" string array variable.
frame = thread.GetFrameAtIndex(0)
variable = frame.LookupVar("strings")
self.DebugSBValue(frame, variable)
self.assertTrue(variable.GetNumChildren() == 4,
"Variable 'strings' should have 4 children")
child3 = variable.GetChildAtIndex(3)
self.DebugSBValue(frame, child3)
self.assertTrue(child3.GetSummary(frame) == '"Guten Tag"',
'strings[3] == "Guten Tag"')
# Lookup the "char_16" char array variable.
variable = frame.LookupVar("char_16")
self.DebugSBValue(frame, variable)
self.assertTrue(variable.GetNumChildren() == 16,
"Variable 'char_16' should have 16 children")
# Lookup the "ushort_matrix" ushort[] array variable.
variable = frame.LookupVar("ushort_matrix")
self.DebugSBValue(frame, variable)
self.assertTrue(variable.GetNumChildren() == 2,
"Variable 'ushort_matrix' should have 2 children")
child0 = variable.GetChildAtIndex(0)
self.DebugSBValue(frame, child0)
self.assertTrue(child0.GetNumChildren() == 3,
"Variable 'ushort_matrix[0]' should have 3 children")
child0_2 = child0.GetChildAtIndex(2)
self.DebugSBValue(frame, child0_2)
self.assertTrue(int(child0_2.GetValue(frame), 16) == 3,
"ushort_matrix[0][2] == 3")
# Lookup the "long_6" char array variable.
variable = frame.LookupVar("long_6")
self.DebugSBValue(frame, variable)
self.assertTrue(variable.GetNumChildren() == 6,
"Variable 'long_6' should have 6 children")
child5 = variable.GetChildAtIndex(5)
self.DebugSBValue(frame, child5)
self.assertTrue(long(child5.GetValue(frame)) == 6,
"long_6[5] == 6")

View File

@ -53,6 +53,61 @@ class TestBitfields(TestBase):
'(uint32_t:7) b7 = 0x0000007f,',
'(uint32_t:4) four = 0x0000000f'])
def test_bitfields_variable_python(self):
"""Use Python APIs to inspect a bitfields variable."""
exe = os.path.join(os.getcwd(), "a.out")
target = self.dbg.CreateTarget(exe)
self.assertTrue(target.IsValid(), VALID_TARGET)
breakpoint = target.BreakpointCreateByLocation("main.c", 42)
self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
self.runCmd("run", RUN_STOPPED)
# This does not work, and results in the process stopped at dyld_start?
#process = target.LaunchProcess([''], [''], os.ctermid(), False)
# The stop reason of the thread should be breakpoint.
thread = target.GetProcess().GetThreadAtIndex(0)
self.assertTrue(thread.GetStopReason() == Enum("Breakpoint"),
STOPPED_DUE_TO_BREAKPOINT)
# The breakpoint should have a hit count of 1.
self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE)
# Lookup the "bits" variable which contains 8 bitfields.
frame = thread.GetFrameAtIndex(0)
bits = frame.LookupVar("bits")
self.DebugSBValue(frame, bits)
self.assertTrue(bits.GetTypeName() == "Bits" and
bits.GetNumChildren() == 8 and
bits.GetByteSize() == 4,
"(Bits)bits with byte size of 4 and 8 children")
b1 = bits.GetChildAtIndex(0)
self.DebugSBValue(frame, b1)
self.assertTrue(b1.GetName() == "b1" and
b1.GetTypeName() == "uint32_t:1" and
b1.IsInScope(frame) and
int(b1.GetValue(frame), 16) == 0x01,
'bits.b1 has type uint32_t:1, is in scope, and == 0x01')
b7 = bits.GetChildAtIndex(6)
self.DebugSBValue(frame, b7)
self.assertTrue(b7.GetName() == "b7" and
b7.GetTypeName() == "uint32_t:7" and
b7.IsInScope(frame) and
int(b7.GetValue(frame), 16) == 0x7f,
'bits.b7 has type uint32_t:7, is in scope, and == 0x7f')
four = bits.GetChildAtIndex(7)
self.DebugSBValue(frame, four)
self.assertTrue(four.GetName() == "four" and
four.GetTypeName() == "uint32_t:4" and
four.IsInScope(frame) and
int(four.GetValue(frame), 16) == 0x0f,
'bits.four has type uint32_t:4, is in scope, and == 0x0f')
if __name__ == '__main__':
import atexit

View File

@ -177,6 +177,7 @@ def Enum(stopReason):
def EnvArray():
return map(lambda k,v: k+"="+v, os.environ.keys(), os.environ.values())
class TestBase(unittest2.TestCase):
"""This LLDB abstract base class is meant to be subclassed."""
@ -337,3 +338,20 @@ class TestBase(unittest2.TestCase):
if self.traceAlways:
print str(method) + ":", result
return result
def DebugSBValue(self, frame, val):
"""Debug print a SBValue object, if self.traceAlways is True."""
if not self.traceAlways:
return
err = sys.stderr
err.write(val.GetName() + ":\n")
err.write('\t' + "TypeName -> " + val.GetTypeName() + '\n')
err.write('\t' + "ByteSize -> " + str(val.GetByteSize()) + '\n')
err.write('\t' + "NumChildren -> " + str(val.GetNumChildren()) + '\n')
err.write('\t' + "Value -> " + str(val.GetValue(frame)) + '\n')
err.write('\t' + "Summary -> " + str(val.GetSummary(frame)) + '\n')
err.write('\t' + "IsPtrType -> " + str(val.TypeIsPtrType()) + '\n')
err.write('\t' + "Location -> " + val.GetLocation(frame) + '\n')