blob: 431f19f7ae4a019c5eaeb062ceb147f3135404fa [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)")
Johnny Chenb428b692012-02-04 02:07:33 +000016 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000017 def test_value_cast_with_virtual_inheritance(self):
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000018 """Test SBValue::Cast(SBType) API for C++ types with virtual inheritance."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000019 self.build(dictionary=self.d_virtual)
Johnny Chena2791f82012-02-06 21:11:17 +000020 self.setTearDownCleanup(dictionary=self.d_virtual)
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000021 self.do_sbvalue_cast(self.exe_name)
22
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000023 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000024 def test_value_cast_with_regular_inheritance(self):
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000025 """Test SBValue::Cast(SBType) API for C++ types with regular inheritance."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000026 self.build(dictionary=self.d_regular)
Johnny Chena2791f82012-02-06 21:11:17 +000027 self.setTearDownCleanup(dictionary=self.d_regular)
Johnny Chenb428b692012-02-04 02:07:33 +000028 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 Granata55031d22012-02-10 00:10:27 +000038 self.d_virtual = {'CXX_SOURCES': self.source, 'EXE': self.exe_name, 'CFLAGS_EXTRAS': '-DDO_VIRTUAL_INHERITANCE'}
Johnny Chen2eb6c3d2012-02-06 19:14:44 +000039 self.d_regular = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
Johnny Chenb428b692012-02-04 02:07:33 +000040
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 Claytonc6947512013-12-13 19:18:59 +000056 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chenb428b692012-02-04 02:07:33 +000057
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 Chen2eb6c3d2012-02-06 19:14:44 +000089 # Iterate through all the children and print their values.
Johnny Chenb428b692012-02-04 02:07:33 +000090 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 Chen2eb6c3d2012-02-06 19:14:44 +000096 self.assertTrue(a_member_val.GetValueAsUnsigned(error, 0) == 10)
Johnny Chenb428b692012-02-04 02:07:33 +000097
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 Chen2eb6c3d2012-02-06 19:14:44 +0000117 # Iterate through all the children and print their values.
Johnny Chenb428b692012-02-04 02:07:33 +0000118 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 Chen2eb6c3d2012-02-06 19:14:44 +0000124 self.assertTrue(b_member_val.GetValueAsUnsigned(error, 0) == 36)
Johnny Chenb428b692012-02-04 02:07:33 +0000125
126
127if __name__ == '__main__':
128 import atexit
129 lldb.SBDebugger.Initialize()
130 atexit.register(lambda: lldb.SBDebugger.Terminate())
131 unittest2.main()