199451b44SJordan Rupprecht""" 299451b44SJordan RupprechtTest lldb Python API SBValue::Cast(SBType) for C++ types. 399451b44SJordan Rupprecht""" 499451b44SJordan Rupprecht 599451b44SJordan Rupprechtimport lldb 699451b44SJordan Rupprechtfrom lldbsuite.test.decorators import * 799451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import * 899451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil 999451b44SJordan Rupprecht 1099451b44SJordan Rupprecht 1199451b44SJordan Rupprechtclass CppValueCastTestCase(TestBase): 1299451b44SJordan Rupprecht @skipIf(bugnumber="llvm.org/PR36714") 13*2238dcc3SJonas Devlieghere @add_test_categories(["pyapi"]) 1499451b44SJordan Rupprecht def test_value_cast_with_virtual_inheritance(self): 1599451b44SJordan Rupprecht """Test SBValue::Cast(SBType) API for C++ types with virtual inheritance.""" 1699451b44SJordan Rupprecht self.build(dictionary=self.d_virtual) 1799451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=self.d_virtual) 1899451b44SJordan Rupprecht self.do_sbvalue_cast(self.exe_name) 1999451b44SJordan Rupprecht 20*2238dcc3SJonas Devlieghere @add_test_categories(["pyapi"]) 2199451b44SJordan Rupprecht def test_value_cast_with_regular_inheritance(self): 2299451b44SJordan Rupprecht """Test SBValue::Cast(SBType) API for C++ types with regular inheritance.""" 2399451b44SJordan Rupprecht self.build(dictionary=self.d_regular) 2499451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=self.d_regular) 2599451b44SJordan Rupprecht self.do_sbvalue_cast(self.exe_name) 2699451b44SJordan Rupprecht 2799451b44SJordan Rupprecht def setUp(self): 2899451b44SJordan Rupprecht # Call super's setUp(). 2999451b44SJordan Rupprecht TestBase.setUp(self) 3099451b44SJordan Rupprecht 3199451b44SJordan Rupprecht # Find the line number to break for main.c. 32*2238dcc3SJonas Devlieghere self.source = "sbvalue-cast.cpp" 33*2238dcc3SJonas Devlieghere self.line = line_number(self.source, "// Set breakpoint here.") 3499451b44SJordan Rupprecht self.exe_name = self.testMethodName 3599451b44SJordan Rupprecht self.d_virtual = { 36*2238dcc3SJonas Devlieghere "CXX_SOURCES": self.source, 37*2238dcc3SJonas Devlieghere "EXE": self.exe_name, 38*2238dcc3SJonas Devlieghere "CFLAGS_EXTRAS": "-DDO_VIRTUAL_INHERITANCE", 39*2238dcc3SJonas Devlieghere } 40*2238dcc3SJonas Devlieghere self.d_regular = {"CXX_SOURCES": self.source, "EXE": self.exe_name} 4199451b44SJordan Rupprecht 4299451b44SJordan Rupprecht def do_sbvalue_cast(self, exe_name): 4399451b44SJordan Rupprecht """Test SBValue::Cast(SBType) API for C++ types.""" 4499451b44SJordan Rupprecht exe = self.getBuildArtifact(exe_name) 4599451b44SJordan Rupprecht 4699451b44SJordan Rupprecht # Create a target from the debugger. 4799451b44SJordan Rupprecht 4899451b44SJordan Rupprecht target = self.dbg.CreateTarget(exe) 4999451b44SJordan Rupprecht self.assertTrue(target, VALID_TARGET) 5099451b44SJordan Rupprecht 5199451b44SJordan Rupprecht # Set up our breakpoints: 5299451b44SJordan Rupprecht 5399451b44SJordan Rupprecht breakpoint = target.BreakpointCreateByLocation(self.source, self.line) 5499451b44SJordan Rupprecht self.assertTrue(breakpoint, VALID_BREAKPOINT) 5599451b44SJordan Rupprecht 5699451b44SJordan Rupprecht # Now launch the process, and do not stop at the entry point. 57*2238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 5899451b44SJordan Rupprecht 59*2238dcc3SJonas Devlieghere self.assertState(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED) 6099451b44SJordan Rupprecht 6199451b44SJordan Rupprecht # Find DerivedA and DerivedB types. 62*2238dcc3SJonas Devlieghere typeA = target.FindFirstType("DerivedA") 63*2238dcc3SJonas Devlieghere typeB = target.FindFirstType("DerivedB") 6499451b44SJordan Rupprecht self.DebugSBType(typeA) 6599451b44SJordan Rupprecht self.DebugSBType(typeB) 6699451b44SJordan Rupprecht self.assertTrue(typeA) 6799451b44SJordan Rupprecht self.assertTrue(typeB) 6899451b44SJordan Rupprecht error = lldb.SBError() 6999451b44SJordan Rupprecht 7099451b44SJordan Rupprecht # First stop is for DerivedA instance. 71*2238dcc3SJonas Devlieghere threads = lldbutil.get_threads_stopped_at_breakpoint(process, breakpoint) 72619e2e09SDave Lee self.assertEqual(len(threads), 1) 7399451b44SJordan Rupprecht thread = threads[0] 7499451b44SJordan Rupprecht frame0 = thread.GetFrameAtIndex(0) 7599451b44SJordan Rupprecht 76*2238dcc3SJonas Devlieghere tellerA = frame0.FindVariable("teller", lldb.eNoDynamicValues) 7799451b44SJordan Rupprecht self.DebugSBValue(tellerA) 781fb5c7a2SDave Lee self.assertEqual( 79*2238dcc3SJonas Devlieghere tellerA.GetChildMemberWithName("m_base_val").GetValueAsUnsigned(error, 0), 80*2238dcc3SJonas Devlieghere 20, 81*2238dcc3SJonas Devlieghere ) 8299451b44SJordan Rupprecht 8399451b44SJordan Rupprecht if self.TraceOn(): 8499451b44SJordan Rupprecht for child in tellerA: 8599451b44SJordan Rupprecht print("child name:", child.GetName()) 8699451b44SJordan Rupprecht print(child) 8799451b44SJordan Rupprecht 8899451b44SJordan Rupprecht # Call SBValue.Cast() to obtain instanceA. 8999451b44SJordan Rupprecht instanceA = tellerA.Cast(typeA.GetPointerType()) 9099451b44SJordan Rupprecht self.DebugSBValue(instanceA) 9199451b44SJordan Rupprecht 9299451b44SJordan Rupprecht # Iterate through all the children and print their values. 9399451b44SJordan Rupprecht if self.TraceOn(): 9499451b44SJordan Rupprecht for child in instanceA: 9599451b44SJordan Rupprecht print("child name:", child.GetName()) 9699451b44SJordan Rupprecht print(child) 97*2238dcc3SJonas Devlieghere a_member_val = instanceA.GetChildMemberWithName("m_a_val") 9899451b44SJordan Rupprecht self.DebugSBValue(a_member_val) 99619e2e09SDave Lee self.assertEqual(a_member_val.GetValueAsUnsigned(error, 0), 10) 10099451b44SJordan Rupprecht 10199451b44SJordan Rupprecht # Second stop is for DerivedB instance. 10299451b44SJordan Rupprecht threads = lldbutil.continue_to_breakpoint(process, breakpoint) 103619e2e09SDave Lee self.assertEqual(len(threads), 1) 10499451b44SJordan Rupprecht thread = threads[0] 10599451b44SJordan Rupprecht frame0 = thread.GetFrameAtIndex(0) 10699451b44SJordan Rupprecht 107*2238dcc3SJonas Devlieghere tellerB = frame0.FindVariable("teller", lldb.eNoDynamicValues) 10899451b44SJordan Rupprecht self.DebugSBValue(tellerB) 1091fb5c7a2SDave Lee self.assertEqual( 110*2238dcc3SJonas Devlieghere tellerB.GetChildMemberWithName("m_base_val").GetValueAsUnsigned(error, 0), 111*2238dcc3SJonas Devlieghere 12, 112*2238dcc3SJonas Devlieghere ) 11399451b44SJordan Rupprecht 11499451b44SJordan Rupprecht if self.TraceOn(): 11599451b44SJordan Rupprecht for child in tellerB: 11699451b44SJordan Rupprecht print("child name:", child.GetName()) 11799451b44SJordan Rupprecht print(child) 11899451b44SJordan Rupprecht 11999451b44SJordan Rupprecht # Call SBValue.Cast() to obtain instanceB. 12099451b44SJordan Rupprecht instanceB = tellerB.Cast(typeB.GetPointerType()) 12199451b44SJordan Rupprecht self.DebugSBValue(instanceB) 12299451b44SJordan Rupprecht 12399451b44SJordan Rupprecht # Iterate through all the children and print their values. 12499451b44SJordan Rupprecht if self.TraceOn(): 12599451b44SJordan Rupprecht for child in instanceB: 12699451b44SJordan Rupprecht print("child name:", child.GetName()) 12799451b44SJordan Rupprecht print(child) 128*2238dcc3SJonas Devlieghere b_member_val = instanceB.GetChildMemberWithName("m_b_val") 12999451b44SJordan Rupprecht self.DebugSBValue(b_member_val) 130619e2e09SDave Lee self.assertEqual(b_member_val.GetValueAsUnsigned(error, 0), 36) 131