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)") |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 16 | @python_api_test |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 17 | def test_value_cast_with_virtual_inheritance(self): |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 18 | """Test SBValue::Cast(SBType) API for C++ types with virtual inheritance.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 19 | self.build(dictionary=self.d_virtual) |
Johnny Chen | a2791f8 | 2012-02-06 21:11:17 +0000 | [diff] [blame] | 20 | self.setTearDownCleanup(dictionary=self.d_virtual) |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 21 | self.do_sbvalue_cast(self.exe_name) |
| 22 | |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 23 | @python_api_test |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 24 | def test_value_cast_with_regular_inheritance(self): |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 25 | """Test SBValue::Cast(SBType) API for C++ types with regular inheritance.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 26 | self.build(dictionary=self.d_regular) |
Johnny Chen | a2791f8 | 2012-02-06 21:11:17 +0000 | [diff] [blame] | 27 | self.setTearDownCleanup(dictionary=self.d_regular) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 28 | self.do_sbvalue_cast(self.exe_name) |
| 29 | |
| 30 | def setUp(self): |
| 31 | # Call super's setUp(). |
| 32 | TestBase.setUp(self) |
| 33 | |
| 34 | # Find the line number to break for main.c. |
| 35 | self.source = 'sbvalue-cast.cpp'; |
| 36 | self.line = line_number(self.source, '// Set breakpoint here.') |
| 37 | self.exe_name = self.testMethodName |
Enrico Granata | 55031d2 | 2012-02-10 00:10:27 +0000 | [diff] [blame] | 38 | 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] | 39 | self.d_regular = {'CXX_SOURCES': self.source, 'EXE': self.exe_name} |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 40 | |
| 41 | def do_sbvalue_cast (self, exe_name): |
| 42 | """Test SBValue::Cast(SBType) API for C++ types.""" |
| 43 | exe = os.path.join(os.getcwd(), exe_name) |
| 44 | |
| 45 | # Create a target from the debugger. |
| 46 | |
| 47 | target = self.dbg.CreateTarget (exe) |
| 48 | self.assertTrue(target, VALID_TARGET) |
| 49 | |
| 50 | # Set up our breakpoints: |
| 51 | |
| 52 | breakpoint = target.BreakpointCreateByLocation(self.source, self.line) |
| 53 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
| 54 | |
| 55 | # Now launch the process, and do not stop at the entry point. |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 56 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 57 | |
| 58 | self.assertTrue(process.GetState() == lldb.eStateStopped, |
| 59 | PROCESS_STOPPED) |
| 60 | |
| 61 | # Find DerivedA and DerivedB types. |
| 62 | typeA = target.FindFirstType('DerivedA') |
| 63 | typeB = target.FindFirstType('DerivedB') |
| 64 | self.DebugSBType(typeA) |
| 65 | self.DebugSBType(typeB) |
| 66 | self.assertTrue(typeA) |
| 67 | self.assertTrue(typeB) |
| 68 | error = lldb.SBError() |
| 69 | |
| 70 | # First stop is for DerivedA instance. |
| 71 | threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint) |
| 72 | self.assertTrue (len(threads) == 1) |
| 73 | thread = threads[0] |
| 74 | frame0 = thread.GetFrameAtIndex(0) |
| 75 | |
| 76 | tellerA = frame0.FindVariable('teller', lldb.eNoDynamicValues) |
| 77 | self.DebugSBValue(tellerA) |
| 78 | self.assertTrue(tellerA.GetChildMemberWithName('m_base_val').GetValueAsUnsigned(error, 0) == 20) |
| 79 | |
| 80 | if self.TraceOn(): |
| 81 | for child in tellerA: |
| 82 | print "child name:", child.GetName() |
| 83 | print child |
| 84 | |
| 85 | # Call SBValue.Cast() to obtain instanceA. |
| 86 | instanceA = tellerA.Cast(typeA.GetPointerType()) |
| 87 | self.DebugSBValue(instanceA) |
| 88 | |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 89 | # Iterate through all the children and print their values. |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 90 | if self.TraceOn(): |
| 91 | for child in instanceA: |
| 92 | print "child name:", child.GetName() |
| 93 | print child |
| 94 | a_member_val = instanceA.GetChildMemberWithName('m_a_val') |
| 95 | self.DebugSBValue(a_member_val) |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 96 | self.assertTrue(a_member_val.GetValueAsUnsigned(error, 0) == 10) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 97 | |
| 98 | # Second stop is for DerivedB instance. |
| 99 | threads = lldbutil.continue_to_breakpoint (process, breakpoint) |
| 100 | self.assertTrue (len(threads) == 1) |
| 101 | thread = threads[0] |
| 102 | frame0 = thread.GetFrameAtIndex(0) |
| 103 | |
| 104 | tellerB = frame0.FindVariable('teller', lldb.eNoDynamicValues) |
| 105 | self.DebugSBValue(tellerB) |
| 106 | self.assertTrue(tellerB.GetChildMemberWithName('m_base_val').GetValueAsUnsigned(error, 0) == 12) |
| 107 | |
| 108 | if self.TraceOn(): |
| 109 | for child in tellerB: |
| 110 | print "child name:", child.GetName() |
| 111 | print child |
| 112 | |
| 113 | # Call SBValue.Cast() to obtain instanceB. |
| 114 | instanceB = tellerB.Cast(typeB.GetPointerType()) |
| 115 | self.DebugSBValue(instanceB) |
| 116 | |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 117 | # Iterate through all the children and print their values. |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 118 | if self.TraceOn(): |
| 119 | for child in instanceB: |
| 120 | print "child name:", child.GetName() |
| 121 | print child |
| 122 | b_member_val = instanceB.GetChildMemberWithName('m_b_val') |
| 123 | self.DebugSBValue(b_member_val) |
Johnny Chen | 2eb6c3d | 2012-02-06 19:14:44 +0000 | [diff] [blame] | 124 | self.assertTrue(b_member_val.GetValueAsUnsigned(error, 0) == 36) |
Johnny Chen | b428b69 | 2012-02-04 02:07:33 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | if __name__ == '__main__': |
| 128 | import atexit |
| 129 | lldb.SBDebugger.Initialize() |
| 130 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 131 | unittest2.main() |