blob: c11a351071258315e9a9c40f4f36556471e68a0b [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
13 mydir = os.path.join("lang", "cpp", "dynamic-value")
14
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000015 # rdar://problem/10808472 SBValue::Cast test case is failing (virtual inheritance)
16 @unittest2.expectedFailure
Johnny Chenb428b692012-02-04 02:07:33 +000017 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
18 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000019 @dsym_test
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000020 def test_value_cast_with_dsym_and_virtual_inheritance(self):
21 """Test SBValue::Cast(SBType) API for C++ types with virtual inheritance."""
22 self.buildDsym(dictionary=self.d_virtual)
Johnny Chena2791f82012-02-06 21:11:17 +000023 self.setTearDownCleanup(dictionary=self.d_virtual)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000024 self.do_sbvalue_cast(self.exe_name)
25
26 # rdar://problem/10808472 SBValue::Cast test case is failing (virtual inheritance)
27 @unittest2.expectedFailure
28 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000029 @dwarf_test
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000030 def test_value_cast_with_dwarf_and_virtual_inheritance(self):
31 """Test SBValue::Cast(SBType) API for C++ types with virtual inheritance."""
32 self.buildDwarf(dictionary=self.d_virtual)
Johnny Chena2791f82012-02-06 21:11:17 +000033 self.setTearDownCleanup(dictionary=self.d_virtual)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000034 self.do_sbvalue_cast(self.exe_name)
35
36 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
37 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000038 @dsym_test
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000039 def test_value_cast_with_dsym_and_regular_inheritance(self):
40 """Test SBValue::Cast(SBType) API for C++ types with regular inheritance."""
41 self.buildDsym(dictionary=self.d_regular)
Johnny Chena2791f82012-02-06 21:11:17 +000042 self.setTearDownCleanup(dictionary=self.d_regular)
Johnny Chenb428b692012-02-04 02:07:33 +000043 self.do_sbvalue_cast(self.exe_name)
44
45 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000046 @dwarf_test
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000047 def test_value_cast_with_dwarf_and_regular_inheritance(self):
48 """Test SBValue::Cast(SBType) API for C++ types with regular inheritance."""
49 self.buildDwarf(dictionary=self.d_regular)
Johnny Chena2791f82012-02-06 21:11:17 +000050 self.setTearDownCleanup(dictionary=self.d_regular)
Johnny Chenb428b692012-02-04 02:07:33 +000051 self.do_sbvalue_cast(self.exe_name)
52
53 def setUp(self):
54 # Call super's setUp().
55 TestBase.setUp(self)
56
57 # Find the line number to break for main.c.
58 self.source = 'sbvalue-cast.cpp';
59 self.line = line_number(self.source, '// Set breakpoint here.')
60 self.exe_name = self.testMethodName
Enrico Granata55031d22012-02-10 00:10:27 +000061 self.d_virtual = {'CXX_SOURCES': self.source, 'EXE': self.exe_name, 'CFLAGS_EXTRAS': '-DDO_VIRTUAL_INHERITANCE'}
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000062 self.d_regular = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
Johnny Chenb428b692012-02-04 02:07:33 +000063
64 def do_sbvalue_cast (self, exe_name):
65 """Test SBValue::Cast(SBType) API for C++ types."""
66 exe = os.path.join(os.getcwd(), exe_name)
67
68 # Create a target from the debugger.
69
70 target = self.dbg.CreateTarget (exe)
71 self.assertTrue(target, VALID_TARGET)
72
73 # Set up our breakpoints:
74
75 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
76 self.assertTrue(breakpoint, VALID_BREAKPOINT)
77
78 # Now launch the process, and do not stop at the entry point.
79 process = target.LaunchSimple (None, None, os.getcwd())
80
81 self.assertTrue(process.GetState() == lldb.eStateStopped,
82 PROCESS_STOPPED)
83
84 # Find DerivedA and DerivedB types.
85 typeA = target.FindFirstType('DerivedA')
86 typeB = target.FindFirstType('DerivedB')
87 self.DebugSBType(typeA)
88 self.DebugSBType(typeB)
89 self.assertTrue(typeA)
90 self.assertTrue(typeB)
91 error = lldb.SBError()
92
93 # First stop is for DerivedA instance.
94 threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint)
95 self.assertTrue (len(threads) == 1)
96 thread = threads[0]
97 frame0 = thread.GetFrameAtIndex(0)
98
99 tellerA = frame0.FindVariable('teller', lldb.eNoDynamicValues)
100 self.DebugSBValue(tellerA)
101 self.assertTrue(tellerA.GetChildMemberWithName('m_base_val').GetValueAsUnsigned(error, 0) == 20)
102
103 if self.TraceOn():
104 for child in tellerA:
105 print "child name:", child.GetName()
106 print child
107
108 # Call SBValue.Cast() to obtain instanceA.
109 instanceA = tellerA.Cast(typeA.GetPointerType())
110 self.DebugSBValue(instanceA)
111
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000112 # Iterate through all the children and print their values.
Johnny Chenb428b692012-02-04 02:07:33 +0000113 if self.TraceOn():
114 for child in instanceA:
115 print "child name:", child.GetName()
116 print child
117 a_member_val = instanceA.GetChildMemberWithName('m_a_val')
118 self.DebugSBValue(a_member_val)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000119 self.assertTrue(a_member_val.GetValueAsUnsigned(error, 0) == 10)
Johnny Chenb428b692012-02-04 02:07:33 +0000120
121 # Second stop is for DerivedB instance.
122 threads = lldbutil.continue_to_breakpoint (process, breakpoint)
123 self.assertTrue (len(threads) == 1)
124 thread = threads[0]
125 frame0 = thread.GetFrameAtIndex(0)
126
127 tellerB = frame0.FindVariable('teller', lldb.eNoDynamicValues)
128 self.DebugSBValue(tellerB)
129 self.assertTrue(tellerB.GetChildMemberWithName('m_base_val').GetValueAsUnsigned(error, 0) == 12)
130
131 if self.TraceOn():
132 for child in tellerB:
133 print "child name:", child.GetName()
134 print child
135
136 # Call SBValue.Cast() to obtain instanceB.
137 instanceB = tellerB.Cast(typeB.GetPointerType())
138 self.DebugSBValue(instanceB)
139
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000140 # Iterate through all the children and print their values.
Johnny Chenb428b692012-02-04 02:07:33 +0000141 if self.TraceOn():
142 for child in instanceB:
143 print "child name:", child.GetName()
144 print child
145 b_member_val = instanceB.GetChildMemberWithName('m_b_val')
146 self.DebugSBValue(b_member_val)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000147 self.assertTrue(b_member_val.GetValueAsUnsigned(error, 0) == 36)
Johnny Chenb428b692012-02-04 02:07:33 +0000148
149
150if __name__ == '__main__':
151 import atexit
152 lldb.SBDebugger.Initialize()
153 atexit.register(lambda: lldb.SBDebugger.Terminate())
154 unittest2.main()