blob: 89f6d520b648c41491f1df0d5fee64bafb96461f [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 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
25 # rdar://problem/10808472 SBValue::Cast test case is failing (virtual inheritance)
26 @unittest2.expectedFailure
27 @python_api_test
28 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
34 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
35 @python_api_test
36 def test_value_cast_with_dsym_and_regular_inheritance(self):
37 """Test SBValue::Cast(SBType) API for C++ types with regular inheritance."""
38 self.buildDsym(dictionary=self.d_regular)
Johnny Chena2791f82012-02-06 21:11:17 +000039 self.setTearDownCleanup(dictionary=self.d_regular)
Johnny Chenb428b692012-02-04 02:07:33 +000040 self.do_sbvalue_cast(self.exe_name)
41
42 @python_api_test
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000043 def test_value_cast_with_dwarf_and_regular_inheritance(self):
44 """Test SBValue::Cast(SBType) API for C++ types with regular inheritance."""
45 self.buildDwarf(dictionary=self.d_regular)
Johnny Chena2791f82012-02-06 21:11:17 +000046 self.setTearDownCleanup(dictionary=self.d_regular)
Johnny Chenb428b692012-02-04 02:07:33 +000047 self.do_sbvalue_cast(self.exe_name)
48
49 def setUp(self):
50 # Call super's setUp().
51 TestBase.setUp(self)
52
53 # Find the line number to break for main.c.
54 self.source = 'sbvalue-cast.cpp';
55 self.line = line_number(self.source, '// Set breakpoint here.')
56 self.exe_name = self.testMethodName
Enrico Granata55031d22012-02-10 00:10:27 +000057 self.d_virtual = {'CXX_SOURCES': self.source, 'EXE': self.exe_name, 'CFLAGS_EXTRAS': '-DDO_VIRTUAL_INHERITANCE'}
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000058 self.d_regular = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
Johnny Chenb428b692012-02-04 02:07:33 +000059
60 def do_sbvalue_cast (self, exe_name):
61 """Test SBValue::Cast(SBType) API for C++ types."""
62 exe = os.path.join(os.getcwd(), exe_name)
63
64 # Create a target from the debugger.
65
66 target = self.dbg.CreateTarget (exe)
67 self.assertTrue(target, VALID_TARGET)
68
69 # Set up our breakpoints:
70
71 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
72 self.assertTrue(breakpoint, VALID_BREAKPOINT)
73
74 # Now launch the process, and do not stop at the entry point.
75 process = target.LaunchSimple (None, None, os.getcwd())
76
77 self.assertTrue(process.GetState() == lldb.eStateStopped,
78 PROCESS_STOPPED)
79
80 # Find DerivedA and DerivedB types.
81 typeA = target.FindFirstType('DerivedA')
82 typeB = target.FindFirstType('DerivedB')
83 self.DebugSBType(typeA)
84 self.DebugSBType(typeB)
85 self.assertTrue(typeA)
86 self.assertTrue(typeB)
87 error = lldb.SBError()
88
89 # First stop is for DerivedA instance.
90 threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint)
91 self.assertTrue (len(threads) == 1)
92 thread = threads[0]
93 frame0 = thread.GetFrameAtIndex(0)
94
95 tellerA = frame0.FindVariable('teller', lldb.eNoDynamicValues)
96 self.DebugSBValue(tellerA)
97 self.assertTrue(tellerA.GetChildMemberWithName('m_base_val').GetValueAsUnsigned(error, 0) == 20)
98
99 if self.TraceOn():
100 for child in tellerA:
101 print "child name:", child.GetName()
102 print child
103
104 # Call SBValue.Cast() to obtain instanceA.
105 instanceA = tellerA.Cast(typeA.GetPointerType())
106 self.DebugSBValue(instanceA)
107
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000108 # Iterate through all the children and print their values.
Johnny Chenb428b692012-02-04 02:07:33 +0000109 if self.TraceOn():
110 for child in instanceA:
111 print "child name:", child.GetName()
112 print child
113 a_member_val = instanceA.GetChildMemberWithName('m_a_val')
114 self.DebugSBValue(a_member_val)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000115 self.assertTrue(a_member_val.GetValueAsUnsigned(error, 0) == 10)
Johnny Chenb428b692012-02-04 02:07:33 +0000116
117 # Second stop is for DerivedB instance.
118 threads = lldbutil.continue_to_breakpoint (process, breakpoint)
119 self.assertTrue (len(threads) == 1)
120 thread = threads[0]
121 frame0 = thread.GetFrameAtIndex(0)
122
123 tellerB = frame0.FindVariable('teller', lldb.eNoDynamicValues)
124 self.DebugSBValue(tellerB)
125 self.assertTrue(tellerB.GetChildMemberWithName('m_base_val').GetValueAsUnsigned(error, 0) == 12)
126
127 if self.TraceOn():
128 for child in tellerB:
129 print "child name:", child.GetName()
130 print child
131
132 # Call SBValue.Cast() to obtain instanceB.
133 instanceB = tellerB.Cast(typeB.GetPointerType())
134 self.DebugSBValue(instanceB)
135
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000136 # Iterate through all the children and print their values.
Johnny Chenb428b692012-02-04 02:07:33 +0000137 if self.TraceOn():
138 for child in instanceB:
139 print "child name:", child.GetName()
140 print child
141 b_member_val = instanceB.GetChildMemberWithName('m_b_val')
142 self.DebugSBValue(b_member_val)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +0000143 self.assertTrue(b_member_val.GetValueAsUnsigned(error, 0) == 36)
Johnny Chenb428b692012-02-04 02:07:33 +0000144
145
146if __name__ == '__main__':
147 import atexit
148 lldb.SBDebugger.Initialize()
149 atexit.register(lambda: lldb.SBDebugger.Terminate())
150 unittest2.main()