blob: a40479cf39733819abce8bce54d17e2a5e958e30 [file] [log] [blame]
Jim Ingham78a685a2011-04-16 00:01:13 +00001"""
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
Johnny Chen91b6b772011-06-25 20:19:47 +000013 mydir = os.path.join("lang", "cpp", "dynamic-value")
Jim Ingham78a685a2011-04-16 00:01:13 +000014
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000017 @dsym_test
Jim Ingham78a685a2011-04-16 00:01:13 +000018 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
Johnny Chen24086bc2012-04-06 19:54:10 +000024 @dwarf_test
Jim Ingham78a685a2011-04-16 00:01:13 +000025 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
Johnny Chen4ebd0192011-05-24 18:22:45 +000046 self.assertTrue (this_static)
Jim Ingham78a685a2011-04-16 00:01:13 +000047 this_static_loc = int (this_static.GetValue(), 16)
48
49 # Get "this" as its dynamic value
50
Johnny Chen4ebd0192011-05-24 18:22:45 +000051 self.assertTrue (this_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +000052 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
Jim Ingham2837b762011-05-04 03:43:18 +000066 use_dynamic = lldb.eDynamicCanRunTarget
67 no_dynamic = lldb.eNoDynamicValues
68
69 this_dynamic_m_b_value = this_dynamic.GetChildMemberWithName('m_b_value', use_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000070 self.assertTrue (this_dynamic_m_b_value)
Jim Ingham78a685a2011-04-16 00:01:13 +000071
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
Jim Ingham2837b762011-05-04 03:43:18 +000077 this_static_m_b_value = this_static.GetChildMemberWithName('m_b_value', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000078 self.assertFalse (this_static_m_b_value)
Jim Ingham78a685a2011-04-16 00:01:13 +000079
80 # Okay, now let's make sure that we can get the dynamic type of a child element:
81
Jim Ingham2837b762011-05-04 03:43:18 +000082 contained_auto_ptr = this_dynamic.GetChildMemberWithName ('m_client_A', use_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000083 self.assertTrue (contained_auto_ptr)
Jim Ingham2837b762011-05-04 03:43:18 +000084 contained_b = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', use_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000085 self.assertTrue (contained_b)
Jim Ingham78a685a2011-04-16 00:01:13 +000086
Jim Ingham2837b762011-05-04 03:43:18 +000087 contained_b_static = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000088 self.assertTrue (contained_b_static)
Jim Ingham78a685a2011-04-16 00:01:13 +000089
90 contained_b_addr = int (contained_b.GetValue(), 16)
91 contained_b_static_addr = int (contained_b_static.GetValue(), 16)
92
93 self.assertTrue (contained_b_addr < contained_b_static_addr)
94
95 def do_get_dynamic_vals(self):
96 """Get argument vals for the call stack when stopped on a breakpoint."""
97 exe = os.path.join(os.getcwd(), "a.out")
98
99 # Create a target from the debugger.
100
Johnny Chen6b2a27c2011-04-25 18:20:52 +0000101 target = self.dbg.CreateTarget (exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000102 self.assertTrue(target, VALID_TARGET)
Jim Ingham78a685a2011-04-16 00:01:13 +0000103
104 # Set up our breakpoints:
105
106 do_something_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.do_something_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000107 self.assertTrue(do_something_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +0000108 VALID_BREAKPOINT)
109
110 first_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_first_call_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000111 self.assertTrue(first_call_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +0000112 VALID_BREAKPOINT)
113
114 second_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_second_call_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000115 self.assertTrue(second_call_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +0000116 VALID_BREAKPOINT)
117
118 # Now launch the process, and do not stop at the entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000119 process = target.LaunchSimple (None, None, os.getcwd())
Jim Ingham78a685a2011-04-16 00:01:13 +0000120
Johnny Chen5a0bee72011-06-15 22:14:12 +0000121 self.assertTrue(process.GetState() == lldb.eStateStopped,
Jim Ingham78a685a2011-04-16 00:01:13 +0000122 PROCESS_STOPPED)
123
Johnny Chen5a0bee72011-06-15 22:14:12 +0000124 threads = lldbutil.get_threads_stopped_at_breakpoint (process, first_call_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000125 self.assertTrue (len(threads) == 1)
126 thread = threads[0]
127
128 frame = thread.GetFrameAtIndex(0)
129
130 # Now find the dynamic addresses of myB and otherB so we can compare them
131 # with the dynamic values we get in doSomething:
132
Jim Ingham2837b762011-05-04 03:43:18 +0000133 use_dynamic = lldb.eDynamicCanRunTarget
134 no_dynamic = lldb.eNoDynamicValues
Jim Ingham78a685a2011-04-16 00:01:13 +0000135
Jim Ingham2837b762011-05-04 03:43:18 +0000136 myB = frame.FindVariable ('myB', no_dynamic);
Johnny Chen4ebd0192011-05-24 18:22:45 +0000137 self.assertTrue (myB)
Jim Ingham78a685a2011-04-16 00:01:13 +0000138 myB_loc = int (myB.GetLocation(), 16)
139
Jim Ingham2837b762011-05-04 03:43:18 +0000140 otherB = frame.FindVariable('otherB', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000141 self.assertTrue (otherB)
Jim Ingham78a685a2011-04-16 00:01:13 +0000142 otherB_loc = int (otherB.GetLocation(), 16)
143
144 # Okay now run to doSomething:
145
Johnny Chen5a0bee72011-06-15 22:14:12 +0000146 threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000147 self.assertTrue (len(threads) == 1)
148 thread = threads[0]
149
150 frame = thread.GetFrameAtIndex(0)
151
152 # Get "this" using FindVariable:
153
Jim Ingham2837b762011-05-04 03:43:18 +0000154 this_static = frame.FindVariable ('this', no_dynamic)
155 this_dynamic = frame.FindVariable ('this', use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000156 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
157
Jim Ingham60dbabb2011-12-08 19:44:08 +0000158 # Now make sure that the "GetDynamicValue" works:
159 # This doesn't work currently because we can't get dynamic values from ConstResult objects.
160 fetched_dynamic_value = this_static.GetDynamicValue(use_dynamic)
161 self.examine_value_object_of_this_ptr (this_static, fetched_dynamic_value, myB_loc)
162
163 # And conversely that the GetDynamicValue() interface also works:
164 fetched_static_value = this_dynamic.GetStaticValue()
165 self.examine_value_object_of_this_ptr (fetched_static_value, this_dynamic, myB_loc)
166
Jim Ingham78a685a2011-04-16 00:01:13 +0000167 # Get "this" using FindValue, make sure that works too:
Jim Ingham2837b762011-05-04 03:43:18 +0000168 this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, no_dynamic)
169 this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000170 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
171
172 # Get "this" using the EvaluateExpression:
Jim Inghamde4b9192011-10-07 18:02:54 +0000173 this_static = frame.EvaluateExpression ('this', False)
174 this_dynamic = frame.EvaluateExpression ('this', True)
175 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
Jim Ingham78a685a2011-04-16 00:01:13 +0000176
177 # The "frame var" code uses another path to get into children, so let's
178 # make sure that works as well:
179
Jim Ingham2837b762011-05-04 03:43:18 +0000180 self.expect('frame var -d run-target anotherA.m_client_A._M_ptr', 'frame var finds its way into a child member',
Greg Claytone3055942011-06-30 02:28:26 +0000181 patterns = ['\(B \*\)'])
Jim Ingham78a685a2011-04-16 00:01:13 +0000182
183 # Now make sure we also get it right for a reference as well:
184
185 anotherA_static = frame.FindVariable ('anotherA', False)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000186 self.assertTrue (anotherA_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000187 anotherA_static_addr = int (anotherA_static.GetValue(), 16)
188
189 anotherA_dynamic = frame.FindVariable ('anotherA', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000190 self.assertTrue (anotherA_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000191 anotherA_dynamic_addr = int (anotherA_dynamic.GetValue(), 16)
192 anotherA_dynamic_typename = anotherA_dynamic.GetTypeName()
193 self.assertTrue (anotherA_dynamic_typename.find('B') != -1)
194
195 self.assertTrue(anotherA_dynamic_addr < anotherA_static_addr)
196
197 anotherA_m_b_value_dynamic = anotherA_dynamic.GetChildMemberWithName('m_b_value', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000198 self.assertTrue (anotherA_m_b_value_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000199 anotherA_m_b_val = int (anotherA_m_b_value_dynamic.GetValue(), 10)
200 self.assertTrue (anotherA_m_b_val == 300)
201
202 anotherA_m_b_value_static = anotherA_static.GetChildMemberWithName('m_b_value', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000203 self.assertFalse (anotherA_m_b_value_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000204
205 # Okay, now continue again, and when we hit the second breakpoint in main
206
Johnny Chen5a0bee72011-06-15 22:14:12 +0000207 threads = lldbutil.continue_to_breakpoint (process, second_call_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000208 self.assertTrue (len(threads) == 1)
209 thread = threads[0]
210
211 frame = thread.GetFrameAtIndex(0)
212 reallyA_value = frame.FindVariable ('reallyA', False)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000213 self.assertTrue(reallyA_value)
Jim Ingham78a685a2011-04-16 00:01:13 +0000214 reallyA_loc = int (reallyA_value.GetLocation(), 16)
215
216 # Finally continue to doSomething again, and make sure we get the right value for anotherA,
217 # which this time around is just an "A".
218
Johnny Chen5a0bee72011-06-15 22:14:12 +0000219 threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000220 self.assertTrue(len(threads) == 1)
221 thread = threads[0]
222
223 frame = thread.GetFrameAtIndex(0)
224 anotherA_value = frame.FindVariable ('anotherA', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000225 self.assertTrue(anotherA_value)
Jim Ingham78a685a2011-04-16 00:01:13 +0000226 anotherA_loc = int (anotherA_value.GetValue(), 16)
227 self.assertTrue (anotherA_loc == reallyA_loc)
228 self.assertTrue (anotherA_value.GetTypeName().find ('B') == -1)
229
230if __name__ == '__main__':
231 import atexit
232 lldb.SBDebugger.Initialize()
233 atexit.register(lambda: lldb.SBDebugger.Terminate())
234 unittest2.main()