1"""
2Use lldb Python API to test dynamic values in C++
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class DynamicValueTestCase(TestBase):
12
13    mydir = os.path.join("lang", "cpp", "dynamic-value")
14
15    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16    @python_api_test
17    @dsym_test
18    def test_get_dynamic_vals_with_dsym(self):
19        """Test fetching C++ dynamic values from pointers & references."""
20        self.buildDsym()
21        self.do_get_dynamic_vals()
22
23    @python_api_test
24    @dwarf_test
25    def test_get_dynamic_vals_with_dwarf(self):
26        """Test fetching C++ dynamic values from pointers & references."""
27        self.buildDwarf()
28        self.do_get_dynamic_vals()
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
36        self.do_something_line = line_number('pass-to-base.cpp', '// Break here in doSomething.')
37        self.main_first_call_line = line_number('pass-to-base.cpp',
38                                                 '// Break here and get real addresses of myB and otherB.')
39        self.main_second_call_line = line_number('pass-to-base.cpp',
40                                                       '// Break here and get real address of reallyA.')
41
42    def examine_value_object_of_this_ptr (self, this_static, this_dynamic, dynamic_location):
43
44        # Get "this" as its static value
45
46        self.assertTrue (this_static)
47        this_static_loc = int (this_static.GetValue(), 16)
48
49        # Get "this" as its dynamic value
50
51        self.assertTrue (this_dynamic)
52        this_dynamic_typename = this_dynamic.GetTypeName()
53        self.assertTrue (this_dynamic_typename.find('B') != -1)
54        this_dynamic_loc = int (this_dynamic.GetValue(), 16)
55
56        # Make sure we got the right address for "this"
57
58        self.assertTrue (this_dynamic_loc == dynamic_location)
59
60        # And that the static address is greater than the dynamic one
61
62        self.assertTrue (this_static_loc > this_dynamic_loc)
63
64        # Now read m_b_value which is only in the dynamic value:
65
66        use_dynamic = lldb.eDynamicCanRunTarget
67        no_dynamic  = lldb.eNoDynamicValues
68
69        this_dynamic_m_b_value = this_dynamic.GetChildMemberWithName('m_b_value', use_dynamic)
70        self.assertTrue (this_dynamic_m_b_value)
71
72        m_b_value = int (this_dynamic_m_b_value.GetValue(), 0)
73        self.assertTrue (m_b_value == 10)
74
75        # Make sure it is not in the static version
76
77        this_static_m_b_value = this_static.GetChildMemberWithName('m_b_value', no_dynamic)
78        self.assertFalse (this_static_m_b_value)
79
80        # Okay, now let's make sure that we can get the dynamic type of a child element:
81
82        contained_auto_ptr = this_dynamic.GetChildMemberWithName ('m_client_A', use_dynamic)
83        self.assertTrue (contained_auto_ptr)
84        contained_b = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', use_dynamic)
85        if not contained_b:
86                contained_b = contained_auto_ptr.GetChildMemberWithName ('__ptr_', use_dynamic)
87        self.assertTrue (contained_b)
88
89        contained_b_static = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', no_dynamic)
90        if not contained_b_static:
91                contained_b_static = contained_auto_ptr.GetChildMemberWithName ('__ptr_', no_dynamic)
92        self.assertTrue (contained_b_static)
93
94        contained_b_addr = int (contained_b.GetValue(), 16)
95        contained_b_static_addr = int (contained_b_static.GetValue(), 16)
96
97        self.assertTrue (contained_b_addr < contained_b_static_addr)
98
99    def do_get_dynamic_vals(self):
100        """Get argument vals for the call stack when stopped on a breakpoint."""
101        exe = os.path.join(os.getcwd(), "a.out")
102
103        # Create a target from the debugger.
104
105        target = self.dbg.CreateTarget (exe)
106        self.assertTrue(target, VALID_TARGET)
107
108        # Set up our breakpoints:
109
110        do_something_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.do_something_line)
111        self.assertTrue(do_something_bpt,
112                        VALID_BREAKPOINT)
113
114        first_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_first_call_line)
115        self.assertTrue(first_call_bpt,
116                        VALID_BREAKPOINT)
117
118        second_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_second_call_line)
119        self.assertTrue(second_call_bpt,
120                        VALID_BREAKPOINT)
121
122        # Now launch the process, and do not stop at the entry point.
123        process = target.LaunchSimple (None, None, os.getcwd())
124
125        self.assertTrue(process.GetState() == lldb.eStateStopped,
126                        PROCESS_STOPPED)
127
128        threads = lldbutil.get_threads_stopped_at_breakpoint (process, first_call_bpt)
129        self.assertTrue (len(threads) == 1)
130        thread = threads[0]
131
132        frame = thread.GetFrameAtIndex(0)
133
134        # Now find the dynamic addresses of myB and otherB so we can compare them
135        # with the dynamic values we get in doSomething:
136
137        use_dynamic = lldb.eDynamicCanRunTarget
138        no_dynamic  = lldb.eNoDynamicValues
139
140        myB = frame.FindVariable ('myB', no_dynamic);
141        self.assertTrue (myB)
142        myB_loc = int (myB.GetLocation(), 16)
143
144        otherB = frame.FindVariable('otherB', no_dynamic)
145        self.assertTrue (otherB)
146        otherB_loc = int (otherB.GetLocation(), 16)
147
148        # Okay now run to doSomething:
149
150        threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
151        self.assertTrue (len(threads) == 1)
152        thread = threads[0]
153
154        frame = thread.GetFrameAtIndex(0)
155
156        # Get "this" using FindVariable:
157
158        this_static = frame.FindVariable ('this', no_dynamic)
159        this_dynamic = frame.FindVariable ('this', use_dynamic)
160        self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
161
162        # Now make sure that the "GetDynamicValue" works:
163        # This doesn't work currently because we can't get dynamic values from ConstResult objects.
164        fetched_dynamic_value = this_static.GetDynamicValue(use_dynamic)
165        self.examine_value_object_of_this_ptr (this_static, fetched_dynamic_value, myB_loc)
166
167        # And conversely that the GetDynamicValue() interface also works:
168        fetched_static_value = this_dynamic.GetStaticValue()
169        self.examine_value_object_of_this_ptr (fetched_static_value, this_dynamic, myB_loc)
170
171        # Get "this" using FindValue, make sure that works too:
172        this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, no_dynamic)
173        this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, use_dynamic)
174        self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
175
176        # Get "this" using the EvaluateExpression:
177        this_static = frame.EvaluateExpression ('this', False)
178        this_dynamic = frame.EvaluateExpression ('this', True)
179        self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
180
181        # The "frame var" code uses another path to get into children, so let's
182        # make sure that works as well:
183
184        self.expect('frame var -d run-target --ptr-depth=2 --show-types anotherA.m_client_A', 'frame var finds its way into a child member',
185            patterns = ['\(B \*\)'])
186
187        # Now make sure we also get it right for a reference as well:
188
189        anotherA_static = frame.FindVariable ('anotherA', False)
190        self.assertTrue (anotherA_static)
191        anotherA_static_addr = int (anotherA_static.GetValue(), 16)
192
193        anotherA_dynamic = frame.FindVariable ('anotherA', True)
194        self.assertTrue (anotherA_dynamic)
195        anotherA_dynamic_addr = int (anotherA_dynamic.GetValue(), 16)
196        anotherA_dynamic_typename = anotherA_dynamic.GetTypeName()
197        self.assertTrue (anotherA_dynamic_typename.find('B') != -1)
198
199        self.assertTrue(anotherA_dynamic_addr < anotherA_static_addr)
200
201        anotherA_m_b_value_dynamic = anotherA_dynamic.GetChildMemberWithName('m_b_value', True)
202        self.assertTrue (anotherA_m_b_value_dynamic)
203        anotherA_m_b_val = int (anotherA_m_b_value_dynamic.GetValue(), 10)
204        self.assertTrue (anotherA_m_b_val == 300)
205
206        anotherA_m_b_value_static = anotherA_static.GetChildMemberWithName('m_b_value', True)
207        self.assertFalse (anotherA_m_b_value_static)
208
209        # Okay, now continue again, and when we hit the second breakpoint in main
210
211        threads = lldbutil.continue_to_breakpoint (process, second_call_bpt)
212        self.assertTrue (len(threads) == 1)
213        thread = threads[0]
214
215        frame = thread.GetFrameAtIndex(0)
216        reallyA_value = frame.FindVariable ('reallyA', False)
217        self.assertTrue(reallyA_value)
218        reallyA_loc = int (reallyA_value.GetLocation(), 16)
219
220        # Finally continue to doSomething again, and make sure we get the right value for anotherA,
221        # which this time around is just an "A".
222
223        threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
224        self.assertTrue(len(threads) == 1)
225        thread = threads[0]
226
227        frame = thread.GetFrameAtIndex(0)
228        anotherA_value = frame.FindVariable ('anotherA', True)
229        self.assertTrue(anotherA_value)
230        anotherA_loc = int (anotherA_value.GetValue(), 16)
231        self.assertTrue (anotherA_loc == reallyA_loc)
232        self.assertTrue (anotherA_value.GetTypeName().find ('B') == -1)
233
234if __name__ == '__main__':
235    import atexit
236    lldb.SBDebugger.Initialize()
237    atexit.register(lambda: lldb.SBDebugger.Terminate())
238    unittest2.main()
239