TestDynamicValue.py [plain text]
"""
Use lldb Python API to test dynamic values in C++
"""
import os, time
import re
import unittest2
import lldb, lldbutil
from lldbtest import *
class DynamicValueTestCase(TestBase):
mydir = os.path.join("lang", "cpp", "dynamic-value")
@unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
@python_api_test
@dsym_test
def test_get_dynamic_vals_with_dsym(self):
"""Test fetching C++ dynamic values from pointers & references."""
self.buildDsym()
self.do_get_dynamic_vals()
@python_api_test
@dwarf_test
def test_get_dynamic_vals_with_dwarf(self):
"""Test fetching C++ dynamic values from pointers & references."""
self.buildDwarf()
self.do_get_dynamic_vals()
def setUp(self):
TestBase.setUp(self)
self.do_something_line = line_number('pass-to-base.cpp', '// Break here in doSomething.')
self.main_first_call_line = line_number('pass-to-base.cpp',
'// Break here and get real addresses of myB and otherB.')
self.main_second_call_line = line_number('pass-to-base.cpp',
'// Break here and get real address of reallyA.')
def examine_value_object_of_this_ptr (self, this_static, this_dynamic, dynamic_location):
self.assertTrue (this_static)
this_static_loc = int (this_static.GetValue(), 16)
self.assertTrue (this_dynamic)
this_dynamic_typename = this_dynamic.GetTypeName()
self.assertTrue (this_dynamic_typename.find('B') != -1)
this_dynamic_loc = int (this_dynamic.GetValue(), 16)
self.assertTrue (this_dynamic_loc == dynamic_location)
self.assertTrue (this_static_loc > this_dynamic_loc)
use_dynamic = lldb.eDynamicCanRunTarget
no_dynamic = lldb.eNoDynamicValues
this_dynamic_m_b_value = this_dynamic.GetChildMemberWithName('m_b_value', use_dynamic)
self.assertTrue (this_dynamic_m_b_value)
m_b_value = int (this_dynamic_m_b_value.GetValue(), 0)
self.assertTrue (m_b_value == 10)
this_static_m_b_value = this_static.GetChildMemberWithName('m_b_value', no_dynamic)
self.assertFalse (this_static_m_b_value)
contained_auto_ptr = this_dynamic.GetChildMemberWithName ('m_client_A', use_dynamic)
self.assertTrue (contained_auto_ptr)
contained_b = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', use_dynamic)
self.assertTrue (contained_b)
contained_b_static = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', no_dynamic)
self.assertTrue (contained_b_static)
contained_b_addr = int (contained_b.GetValue(), 16)
contained_b_static_addr = int (contained_b_static.GetValue(), 16)
self.assertTrue (contained_b_addr < contained_b_static_addr)
def do_get_dynamic_vals(self):
"""Get argument vals for the call stack when stopped on a breakpoint."""
exe = os.path.join(os.getcwd(), "a.out")
target = self.dbg.CreateTarget (exe)
self.assertTrue(target, VALID_TARGET)
do_something_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.do_something_line)
self.assertTrue(do_something_bpt,
VALID_BREAKPOINT)
first_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_first_call_line)
self.assertTrue(first_call_bpt,
VALID_BREAKPOINT)
second_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_second_call_line)
self.assertTrue(second_call_bpt,
VALID_BREAKPOINT)
process = target.LaunchSimple (None, None, os.getcwd())
self.assertTrue(process.GetState() == lldb.eStateStopped,
PROCESS_STOPPED)
threads = lldbutil.get_threads_stopped_at_breakpoint (process, first_call_bpt)
self.assertTrue (len(threads) == 1)
thread = threads[0]
frame = thread.GetFrameAtIndex(0)
use_dynamic = lldb.eDynamicCanRunTarget
no_dynamic = lldb.eNoDynamicValues
myB = frame.FindVariable ('myB', no_dynamic);
self.assertTrue (myB)
myB_loc = int (myB.GetLocation(), 16)
otherB = frame.FindVariable('otherB', no_dynamic)
self.assertTrue (otherB)
otherB_loc = int (otherB.GetLocation(), 16)
threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
self.assertTrue (len(threads) == 1)
thread = threads[0]
frame = thread.GetFrameAtIndex(0)
this_static = frame.FindVariable ('this', no_dynamic)
this_dynamic = frame.FindVariable ('this', use_dynamic)
self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
fetched_dynamic_value = this_static.GetDynamicValue(use_dynamic)
self.examine_value_object_of_this_ptr (this_static, fetched_dynamic_value, myB_loc)
fetched_static_value = this_dynamic.GetStaticValue()
self.examine_value_object_of_this_ptr (fetched_static_value, this_dynamic, myB_loc)
this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, no_dynamic)
this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, use_dynamic)
self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
this_static = frame.EvaluateExpression ('this', False)
this_dynamic = frame.EvaluateExpression ('this', True)
self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
self.expect('frame var -d run-target anotherA.m_client_A._M_ptr', 'frame var finds its way into a child member',
patterns = ['\(B \*\)'])
anotherA_static = frame.FindVariable ('anotherA', False)
self.assertTrue (anotherA_static)
anotherA_static_addr = int (anotherA_static.GetValue(), 16)
anotherA_dynamic = frame.FindVariable ('anotherA', True)
self.assertTrue (anotherA_dynamic)
anotherA_dynamic_addr = int (anotherA_dynamic.GetValue(), 16)
anotherA_dynamic_typename = anotherA_dynamic.GetTypeName()
self.assertTrue (anotherA_dynamic_typename.find('B') != -1)
self.assertTrue(anotherA_dynamic_addr < anotherA_static_addr)
anotherA_m_b_value_dynamic = anotherA_dynamic.GetChildMemberWithName('m_b_value', True)
self.assertTrue (anotherA_m_b_value_dynamic)
anotherA_m_b_val = int (anotherA_m_b_value_dynamic.GetValue(), 10)
self.assertTrue (anotherA_m_b_val == 300)
anotherA_m_b_value_static = anotherA_static.GetChildMemberWithName('m_b_value', True)
self.assertFalse (anotherA_m_b_value_static)
threads = lldbutil.continue_to_breakpoint (process, second_call_bpt)
self.assertTrue (len(threads) == 1)
thread = threads[0]
frame = thread.GetFrameAtIndex(0)
reallyA_value = frame.FindVariable ('reallyA', False)
self.assertTrue(reallyA_value)
reallyA_loc = int (reallyA_value.GetLocation(), 16)
threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
self.assertTrue(len(threads) == 1)
thread = threads[0]
frame = thread.GetFrameAtIndex(0)
anotherA_value = frame.FindVariable ('anotherA', True)
self.assertTrue(anotherA_value)
anotherA_loc = int (anotherA_value.GetValue(), 16)
self.assertTrue (anotherA_loc == reallyA_loc)
self.assertTrue (anotherA_value.GetTypeName().find ('B') == -1)
if __name__ == '__main__':
import atexit
lldb.SBDebugger.Initialize()
atexit.register(lambda: lldb.SBDebugger.Terminate())
unittest2.main()