xref: /llvm-project/lldb/test/API/lang/cpp/dynamic-value/TestCppValueCast.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
1"""
2Test lldb Python API SBValue::Cast(SBType) for C++ types.
3"""
4
5from __future__ import print_function
6
7
8import unittest2
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class CppValueCastTestCase(TestBase):
16
17    @skipIf(bugnumber="llvm.org/PR36714")
18    @add_test_categories(['pyapi'])
19    def test_value_cast_with_virtual_inheritance(self):
20        """Test SBValue::Cast(SBType) API for C++ types with virtual inheritance."""
21        self.build(dictionary=self.d_virtual)
22        self.setTearDownCleanup(dictionary=self.d_virtual)
23        self.do_sbvalue_cast(self.exe_name)
24
25    @add_test_categories(['pyapi'])
26    def test_value_cast_with_regular_inheritance(self):
27        """Test SBValue::Cast(SBType) API for C++ types with regular inheritance."""
28        self.build(dictionary=self.d_regular)
29        self.setTearDownCleanup(dictionary=self.d_regular)
30        self.do_sbvalue_cast(self.exe_name)
31
32    def setUp(self):
33        # Call super's setUp().
34        TestBase.setUp(self)
35
36        # Find the line number to break for main.c.
37        self.source = 'sbvalue-cast.cpp'
38        self.line = line_number(self.source, '// Set breakpoint here.')
39        self.exe_name = self.testMethodName
40        self.d_virtual = {
41            'CXX_SOURCES': self.source,
42            'EXE': self.exe_name,
43            'CFLAGS_EXTRAS': '-DDO_VIRTUAL_INHERITANCE'}
44        self.d_regular = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
45
46    def do_sbvalue_cast(self, exe_name):
47        """Test SBValue::Cast(SBType) API for C++ types."""
48        exe = self.getBuildArtifact(exe_name)
49
50        # Create a target from the debugger.
51
52        target = self.dbg.CreateTarget(exe)
53        self.assertTrue(target, VALID_TARGET)
54
55        # Set up our breakpoints:
56
57        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
58        self.assertTrue(breakpoint, VALID_BREAKPOINT)
59
60        # Now launch the process, and do not stop at the entry point.
61        process = target.LaunchSimple(
62            None, None, self.get_process_working_directory())
63
64        self.assertState(process.GetState(), lldb.eStateStopped,
65                         PROCESS_STOPPED)
66
67        # Find DerivedA and DerivedB types.
68        typeA = target.FindFirstType('DerivedA')
69        typeB = target.FindFirstType('DerivedB')
70        self.DebugSBType(typeA)
71        self.DebugSBType(typeB)
72        self.assertTrue(typeA)
73        self.assertTrue(typeB)
74        error = lldb.SBError()
75
76        # First stop is for DerivedA instance.
77        threads = lldbutil.get_threads_stopped_at_breakpoint(
78            process, breakpoint)
79        self.assertEqual(len(threads), 1)
80        thread = threads[0]
81        frame0 = thread.GetFrameAtIndex(0)
82
83        tellerA = frame0.FindVariable('teller', lldb.eNoDynamicValues)
84        self.DebugSBValue(tellerA)
85        self.assertTrue(tellerA.GetChildMemberWithName(
86            'm_base_val').GetValueAsUnsigned(error, 0) == 20)
87
88        if self.TraceOn():
89            for child in tellerA:
90                print("child name:", child.GetName())
91                print(child)
92
93        # Call SBValue.Cast() to obtain instanceA.
94        instanceA = tellerA.Cast(typeA.GetPointerType())
95        self.DebugSBValue(instanceA)
96
97        # Iterate through all the children and print their values.
98        if self.TraceOn():
99            for child in instanceA:
100                print("child name:", child.GetName())
101                print(child)
102        a_member_val = instanceA.GetChildMemberWithName('m_a_val')
103        self.DebugSBValue(a_member_val)
104        self.assertEqual(a_member_val.GetValueAsUnsigned(error, 0), 10)
105
106        # Second stop is for DerivedB instance.
107        threads = lldbutil.continue_to_breakpoint(process, breakpoint)
108        self.assertEqual(len(threads), 1)
109        thread = threads[0]
110        frame0 = thread.GetFrameAtIndex(0)
111
112        tellerB = frame0.FindVariable('teller', lldb.eNoDynamicValues)
113        self.DebugSBValue(tellerB)
114        self.assertTrue(tellerB.GetChildMemberWithName(
115            'm_base_val').GetValueAsUnsigned(error, 0) == 12)
116
117        if self.TraceOn():
118            for child in tellerB:
119                print("child name:", child.GetName())
120                print(child)
121
122        # Call SBValue.Cast() to obtain instanceB.
123        instanceB = tellerB.Cast(typeB.GetPointerType())
124        self.DebugSBValue(instanceB)
125
126        # Iterate through all the children and print their values.
127        if self.TraceOn():
128            for child in instanceB:
129                print("child name:", child.GetName())
130                print(child)
131        b_member_val = instanceB.GetChildMemberWithName('m_b_val')
132        self.DebugSBValue(b_member_val)
133        self.assertEqual(b_member_val.GetValueAsUnsigned(error, 0), 36)
134