blob: 23a9fc503b87bf8ad5a89072362b3a5744b6d4d9 [file] [log] [blame]
Johnny Chenb428b692012-02-04 02:07:33 +00001"""
2Test lldb Python API SBValue::Cast(SBType) for C++ types.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class CppValueCastTestCase(TestBase):
12
Greg Clayton4570d3e2013-12-10 23:19:29 +000013 mydir = TestBase.compute_mydir(__file__)
Johnny Chenb428b692012-02-04 02:07:33 +000014
Enrico Granata83e7f682014-10-16 22:27:17 +000015 @unittest2.expectedFailure("rdar://problem/10808472 SBValue::Cast test case is failing (virtual inheritance)")
Robert Flack13c7ad92015-03-30 14:12:17 +000016 @skipUnlessDarwin
Johnny Chenb428b692012-02-04 02:07:33 +000017 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000018 @dsym_test
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000019 def test_value_cast_with_dsym_and_virtual_inheritance(self):
20 """Test SBValue::Cast(SBType) API for C++ types with virtual inheritance."""
21 self.buildDsym(dictionary=self.d_virtual)
Johnny Chena2791f82012-02-06 21:11:17 +000022 self.setTearDownCleanup(dictionary=self.d_virtual)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000023 self.do_sbvalue_cast(self.exe_name)
24
Enrico Granata83e7f682014-10-16 22:27:17 +000025 @unittest2.expectedFailure("rdar://problem/10808472 SBValue::Cast test case is failing (virtual inheritance)")
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000026 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000027 @dwarf_test
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000028 def test_value_cast_with_dwarf_and_virtual_inheritance(self):
29 """Test SBValue::Cast(SBType) API for C++ types with virtual inheritance."""
30 self.buildDwarf(dictionary=self.d_virtual)
Johnny Chena2791f82012-02-06 21:11:17 +000031 self.setTearDownCleanup(dictionary=self.d_virtual)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000032 self.do_sbvalue_cast(self.exe_name)
33
Robert Flack13c7ad92015-03-30 14:12:17 +000034 @skipUnlessDarwin
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000035 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000036 @dsym_test
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000037 def test_value_cast_with_dsym_and_regular_inheritance(self):
38 """Test SBValue::Cast(SBType) API for C++ types with regular inheritance."""
39 self.buildDsym(dictionary=self.d_regular)
Johnny Chena2791f82012-02-06 21:11:17 +000040 self.setTearDownCleanup(dictionary=self.d_regular)
Johnny Chenb428b692012-02-04 02:07:33 +000041 self.do_sbvalue_cast(self.exe_name)
42
43 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000044 @dwarf_test
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000045 def test_value_cast_with_dwarf_and_regular_inheritance(self):
46 """Test SBValue::Cast(SBType) API for C++ types with regular inheritance."""
47 self.buildDwarf(dictionary=self.d_regular)
Johnny Chena2791f82012-02-06 21:11:17 +000048 self.setTearDownCleanup(dictionary=self.d_regular)
Johnny Chenb428b692012-02-04 02:07:33 +000049 self.do_sbvalue_cast(self.exe_name)
50
51 def setUp(self):
52 # Call super's setUp().
53 TestBase.setUp(self)
54
55 # Find the line number to break for main.c.
56 self.source = 'sbvalue-cast.cpp';
57 self.line = line_number(self.source, '// Set breakpoint here.')
58 self.exe_name = self.testMethodName
Enrico Granata55031d22012-02-10 00:10:27 +000059 self.d_virtual = {'CXX_SOURCES': self.source, 'EXE': self.exe_name, 'CFLAGS_EXTRAS': '-DDO_VIRTUAL_INHERITANCE'}
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000060 self.d_regular = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
Johnny Chenb428b692012-02-04 02:07:33 +000061
62 def do_sbvalue_cast (self, exe_name):
63 """Test SBValue::Cast(SBType) API for C++ types."""
64 exe = os.path.join(os.getcwd(), exe_name)
65
66 # Create a target from the debugger.
67
68 target = self.dbg.CreateTarget (exe)
69 self.assertTrue(target, VALID_TARGET)
70
71 # Set up our breakpoints:
72
73 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
74 self.assertTrue(breakpoint, VALID_BREAKPOINT)
75
76 # Now launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000077 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chenb428b692012-02-04 02:07:33 +000078
79 self.assertTrue(process.GetState() == lldb.eStateStopped,
80 PROCESS_STOPPED)
81
82 # Find DerivedA and DerivedB types.
83 typeA = target.FindFirstType('DerivedA')
84 typeB = target.FindFirstType('DerivedB')
85 self.DebugSBType(typeA)
86 self.DebugSBType(typeB)
87 self.assertTrue(typeA)
88 self.assertTrue(typeB)
89 error = lldb.SBError()
90
91 # First stop is for DerivedA instance.
92 threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint)
93 self.assertTrue (len(threads) == 1)
94 thread = threads[0]
95 frame0 = thread.GetFrameAtIndex(0)
96
97 tellerA = frame0.FindVariable('teller', lldb.eNoDynamicValues)
98 self.DebugSBValue(tellerA)
99 self.assertTrue(tellerA.GetChildMemberWithName('m_base_val').GetValueAsUnsigned(error, 0) == 20)
100
101 if self.TraceOn():
102 for child in tellerA:
103 print "child name:", child.GetName()
104 print child
105
106 # Call SBValue.Cast() to obtain instanceA.
107 instanceA = tellerA.Cast(typeA.GetPointerType())
108 self.DebugSBValue(instanceA)
109
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000110 # Iterate through all the children and print their values.
Johnny Chenb428b692012-02-04 02:07:33 +0000111 if self.TraceOn():
112 for child in instanceA:
113 print "child name:", child.GetName()
114 print child
115 a_member_val = instanceA.GetChildMemberWithName('m_a_val')
116 self.DebugSBValue(a_member_val)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000117 self.assertTrue(a_member_val.GetValueAsUnsigned(error, 0) == 10)
Johnny Chenb428b692012-02-04 02:07:33 +0000118
119 # Second stop is for DerivedB instance.
120 threads = lldbutil.continue_to_breakpoint (process, breakpoint)
121 self.assertTrue (len(threads) == 1)
122 thread = threads[0]
123 frame0 = thread.GetFrameAtIndex(0)
124
125 tellerB = frame0.FindVariable('teller', lldb.eNoDynamicValues)
126 self.DebugSBValue(tellerB)
127 self.assertTrue(tellerB.GetChildMemberWithName('m_base_val').GetValueAsUnsigned(error, 0) == 12)
128
129 if self.TraceOn():
130 for child in tellerB:
131 print "child name:", child.GetName()
132 print child
133
134 # Call SBValue.Cast() to obtain instanceB.
135 instanceB = tellerB.Cast(typeB.GetPointerType())
136 self.DebugSBValue(instanceB)
137
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000138 # Iterate through all the children and print their values.
Johnny Chenb428b692012-02-04 02:07:33 +0000139 if self.TraceOn():
140 for child in instanceB:
141 print "child name:", child.GetName()
142 print child
143 b_member_val = instanceB.GetChildMemberWithName('m_b_val')
144 self.DebugSBValue(b_member_val)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000145 self.assertTrue(b_member_val.GetValueAsUnsigned(error, 0) == 36)
Johnny Chenb428b692012-02-04 02:07:33 +0000146
147
148if __name__ == '__main__':
149 import atexit
150 lldb.SBDebugger.Initialize()
151 atexit.register(lambda: lldb.SBDebugger.Terminate())
152 unittest2.main()