Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test lldb Python API SBValue::Cast(SBType) for C++ types. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| 8 | import lldb, lldbutil |
| 9 | from lldbtest import * |
| 10 | |
| 11 | class CppValueCastTestCase(TestBase): |
| 12 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 13 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 14 | |
Enrico Granata | 83e7f68 | 2014-10-16 22:27:17 +0000 | [diff] [blame] | 15 | @unittest2.expectedFailure("rdar://problem/10808472 SBValue::Cast test case is failing (virtual inheritance)") |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 16 | @skipUnlessDarwin |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 17 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 18 | @dsym_test |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 19 | 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 Chen | a2791f8 | 2012-02-06 21:11:17 +0000 | [diff] [blame] | 22 | self.setTearDownCleanup(dictionary=self.d_virtual) |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 23 | self.do_sbvalue_cast(self.exe_name) |
| 24 | |
Enrico Granata | 83e7f68 | 2014-10-16 22:27:17 +0000 | [diff] [blame] | 25 | @unittest2.expectedFailure("rdar://problem/10808472 SBValue::Cast test case is failing (virtual inheritance)") |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 26 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 27 | @dwarf_test |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 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 Chen | a2791f8 | 2012-02-06 21:11:17 +0000 | [diff] [blame] | 31 | self.setTearDownCleanup(dictionary=self.d_virtual) |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 32 | self.do_sbvalue_cast(self.exe_name) |
| 33 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 34 | @skipUnlessDarwin |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 35 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 36 | @dsym_test |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 37 | 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 Chen | a2791f8 | 2012-02-06 21:11:17 +0000 | [diff] [blame] | 40 | self.setTearDownCleanup(dictionary=self.d_regular) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 41 | self.do_sbvalue_cast(self.exe_name) |
| 42 | |
| 43 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 44 | @dwarf_test |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 45 | 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 Chen | a2791f8 | 2012-02-06 21:11:17 +0000 | [diff] [blame] | 48 | self.setTearDownCleanup(dictionary=self.d_regular) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 49 | 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 Granata | 55031d2 | 2012-02-10 00:10:27 +0000 | [diff] [blame] | 59 | self.d_virtual = {'CXX_SOURCES': self.source, 'EXE': self.exe_name, 'CFLAGS_EXTRAS': '-DDO_VIRTUAL_INHERITANCE'} |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 60 | self.d_regular = {'CXX_SOURCES': self.source, 'EXE': self.exe_name} |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 61 | |
| 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 Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 77 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 78 | |
| 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 Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 110 | # Iterate through all the children and print their values. |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 111 | 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 Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 117 | self.assertTrue(a_member_val.GetValueAsUnsigned(error, 0) == 10) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 118 | |
| 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 Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 138 | # Iterate through all the children and print their values. |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 139 | 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 Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 145 | self.assertTrue(b_member_val.GetValueAsUnsigned(error, 0) == 36) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 146 | |
| 147 | |
| 148 | if __name__ == '__main__': |
| 149 | import atexit |
| 150 | lldb.SBDebugger.Initialize() |
| 151 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 152 | unittest2.main() |