blob: 3ef4193a91786051fd97059c5b34006eb1e631ca [file] [log] [blame]
Jim Ingham78a685a2011-04-16 00:01:13 +00001"""
2Use lldb Python API to test dynamic values in C++
3"""
4
Zachary Turner77db4a82015-10-22 20:06:20 +00005import lldb_shared
6
Jim Ingham78a685a2011-04-16 00:01:13 +00007import os, time
8import re
Jim Ingham78a685a2011-04-16 00:01:13 +00009import lldb, lldbutil
10from lldbtest import *
11
12class DynamicValueTestCase(TestBase):
13
Greg Clayton4570d3e2013-12-10 23:19:29 +000014 mydir = TestBase.compute_mydir(__file__)
Jim Ingham78a685a2011-04-16 00:01:13 +000015
Jim Ingham78a685a2011-04-16 00:01:13 +000016 def setUp(self):
17 # Call super's setUp().
18 TestBase.setUp(self)
19
20 # Find the line number to break for main.c.
21
22 self.do_something_line = line_number('pass-to-base.cpp', '// Break here in doSomething.')
23 self.main_first_call_line = line_number('pass-to-base.cpp',
24 '// Break here and get real addresses of myB and otherB.')
25 self.main_second_call_line = line_number('pass-to-base.cpp',
26 '// Break here and get real address of reallyA.')
27
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000028 @expectedFailureFreeBSD # FIXME: This needs to be root-caused.
29 @expectedFailureWindows("llvm.org/pr24663")
30 @python_api_test
31 def test_get_dynamic_vals(self):
32 """Test fetching C++ dynamic values from pointers & references."""
33 self.build(dictionary=self.getBuildFlags())
Jim Ingham78a685a2011-04-16 00:01:13 +000034 exe = os.path.join(os.getcwd(), "a.out")
35
36 # Create a target from the debugger.
37
Johnny Chen6b2a27c2011-04-25 18:20:52 +000038 target = self.dbg.CreateTarget (exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000039 self.assertTrue(target, VALID_TARGET)
Jim Ingham78a685a2011-04-16 00:01:13 +000040
41 # Set up our breakpoints:
42
43 do_something_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.do_something_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000044 self.assertTrue(do_something_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +000045 VALID_BREAKPOINT)
46
47 first_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_first_call_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000048 self.assertTrue(first_call_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +000049 VALID_BREAKPOINT)
50
51 second_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_second_call_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000052 self.assertTrue(second_call_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +000053 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())
Jim Ingham78a685a2011-04-16 00:01:13 +000057
Johnny Chen5a0bee72011-06-15 22:14:12 +000058 self.assertTrue(process.GetState() == lldb.eStateStopped,
Jim Ingham78a685a2011-04-16 00:01:13 +000059 PROCESS_STOPPED)
60
Johnny Chen5a0bee72011-06-15 22:14:12 +000061 threads = lldbutil.get_threads_stopped_at_breakpoint (process, first_call_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +000062 self.assertTrue (len(threads) == 1)
63 thread = threads[0]
64
65 frame = thread.GetFrameAtIndex(0)
66
67 # Now find the dynamic addresses of myB and otherB so we can compare them
68 # with the dynamic values we get in doSomething:
69
Jim Ingham2837b762011-05-04 03:43:18 +000070 use_dynamic = lldb.eDynamicCanRunTarget
71 no_dynamic = lldb.eNoDynamicValues
Jim Ingham78a685a2011-04-16 00:01:13 +000072
Jim Ingham2837b762011-05-04 03:43:18 +000073 myB = frame.FindVariable ('myB', no_dynamic);
Johnny Chen4ebd0192011-05-24 18:22:45 +000074 self.assertTrue (myB)
Jim Ingham78a685a2011-04-16 00:01:13 +000075 myB_loc = int (myB.GetLocation(), 16)
76
Jim Ingham2837b762011-05-04 03:43:18 +000077 otherB = frame.FindVariable('otherB', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000078 self.assertTrue (otherB)
Jim Ingham78a685a2011-04-16 00:01:13 +000079 otherB_loc = int (otherB.GetLocation(), 16)
80
81 # Okay now run to doSomething:
82
Johnny Chen5a0bee72011-06-15 22:14:12 +000083 threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +000084 self.assertTrue (len(threads) == 1)
85 thread = threads[0]
86
87 frame = thread.GetFrameAtIndex(0)
88
89 # Get "this" using FindVariable:
90
Jim Ingham2837b762011-05-04 03:43:18 +000091 this_static = frame.FindVariable ('this', no_dynamic)
92 this_dynamic = frame.FindVariable ('this', use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +000093 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
94
Jim Ingham60dbabb2011-12-08 19:44:08 +000095 # Now make sure that the "GetDynamicValue" works:
96 # This doesn't work currently because we can't get dynamic values from ConstResult objects.
97 fetched_dynamic_value = this_static.GetDynamicValue(use_dynamic)
98 self.examine_value_object_of_this_ptr (this_static, fetched_dynamic_value, myB_loc)
99
100 # And conversely that the GetDynamicValue() interface also works:
101 fetched_static_value = this_dynamic.GetStaticValue()
102 self.examine_value_object_of_this_ptr (fetched_static_value, this_dynamic, myB_loc)
103
Jim Ingham78a685a2011-04-16 00:01:13 +0000104 # Get "this" using FindValue, make sure that works too:
Jim Ingham2837b762011-05-04 03:43:18 +0000105 this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, no_dynamic)
106 this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000107 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
108
109 # Get "this" using the EvaluateExpression:
Jim Inghamde4b9192011-10-07 18:02:54 +0000110 this_static = frame.EvaluateExpression ('this', False)
111 this_dynamic = frame.EvaluateExpression ('this', True)
112 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
Jim Ingham78a685a2011-04-16 00:01:13 +0000113
114 # The "frame var" code uses another path to get into children, so let's
115 # make sure that works as well:
116
Enrico Granataa0f95122013-04-11 22:55:45 +0000117 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',
Greg Claytone3055942011-06-30 02:28:26 +0000118 patterns = ['\(B \*\)'])
Jim Ingham78a685a2011-04-16 00:01:13 +0000119
120 # Now make sure we also get it right for a reference as well:
121
122 anotherA_static = frame.FindVariable ('anotherA', False)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000123 self.assertTrue (anotherA_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000124 anotherA_static_addr = int (anotherA_static.GetValue(), 16)
125
126 anotherA_dynamic = frame.FindVariable ('anotherA', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000127 self.assertTrue (anotherA_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000128 anotherA_dynamic_addr = int (anotherA_dynamic.GetValue(), 16)
129 anotherA_dynamic_typename = anotherA_dynamic.GetTypeName()
130 self.assertTrue (anotherA_dynamic_typename.find('B') != -1)
131
132 self.assertTrue(anotherA_dynamic_addr < anotherA_static_addr)
133
134 anotherA_m_b_value_dynamic = anotherA_dynamic.GetChildMemberWithName('m_b_value', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000135 self.assertTrue (anotherA_m_b_value_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000136 anotherA_m_b_val = int (anotherA_m_b_value_dynamic.GetValue(), 10)
137 self.assertTrue (anotherA_m_b_val == 300)
138
139 anotherA_m_b_value_static = anotherA_static.GetChildMemberWithName('m_b_value', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000140 self.assertFalse (anotherA_m_b_value_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000141
142 # Okay, now continue again, and when we hit the second breakpoint in main
143
Johnny Chen5a0bee72011-06-15 22:14:12 +0000144 threads = lldbutil.continue_to_breakpoint (process, second_call_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000145 self.assertTrue (len(threads) == 1)
146 thread = threads[0]
147
148 frame = thread.GetFrameAtIndex(0)
149 reallyA_value = frame.FindVariable ('reallyA', False)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000150 self.assertTrue(reallyA_value)
Jim Ingham78a685a2011-04-16 00:01:13 +0000151 reallyA_loc = int (reallyA_value.GetLocation(), 16)
152
153 # Finally continue to doSomething again, and make sure we get the right value for anotherA,
154 # which this time around is just an "A".
155
Johnny Chen5a0bee72011-06-15 22:14:12 +0000156 threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000157 self.assertTrue(len(threads) == 1)
158 thread = threads[0]
159
160 frame = thread.GetFrameAtIndex(0)
161 anotherA_value = frame.FindVariable ('anotherA', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000162 self.assertTrue(anotherA_value)
Jim Ingham78a685a2011-04-16 00:01:13 +0000163 anotherA_loc = int (anotherA_value.GetValue(), 16)
164 self.assertTrue (anotherA_loc == reallyA_loc)
165 self.assertTrue (anotherA_value.GetTypeName().find ('B') == -1)
166
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000167 def examine_value_object_of_this_ptr (self, this_static, this_dynamic, dynamic_location):
168 # Get "this" as its static value
169 self.assertTrue (this_static)
170 this_static_loc = int (this_static.GetValue(), 16)
171
172 # Get "this" as its dynamic value
173
174 self.assertTrue (this_dynamic)
175 this_dynamic_typename = this_dynamic.GetTypeName()
176 self.assertTrue (this_dynamic_typename.find('B') != -1)
177 this_dynamic_loc = int (this_dynamic.GetValue(), 16)
178
179 # Make sure we got the right address for "this"
180
181 self.assertTrue (this_dynamic_loc == dynamic_location)
182
183 # And that the static address is greater than the dynamic one
184
185 self.assertTrue (this_static_loc > this_dynamic_loc)
186
187 # Now read m_b_value which is only in the dynamic value:
188
189 use_dynamic = lldb.eDynamicCanRunTarget
190 no_dynamic = lldb.eNoDynamicValues
191
192 this_dynamic_m_b_value = this_dynamic.GetChildMemberWithName('m_b_value', use_dynamic)
193 self.assertTrue (this_dynamic_m_b_value)
194
195 m_b_value = int (this_dynamic_m_b_value.GetValue(), 0)
196 self.assertTrue (m_b_value == 10)
197
198 # Make sure it is not in the static version
199
200 this_static_m_b_value = this_static.GetChildMemberWithName('m_b_value', no_dynamic)
201 self.assertFalse (this_static_m_b_value)
202
203 # Okay, now let's make sure that we can get the dynamic type of a child element:
204
205 contained_auto_ptr = this_dynamic.GetChildMemberWithName ('m_client_A', use_dynamic)
206 self.assertTrue (contained_auto_ptr)
207 contained_b = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', use_dynamic)
208 if not contained_b:
209 contained_b = contained_auto_ptr.GetChildMemberWithName ('__ptr_', use_dynamic)
210 self.assertTrue (contained_b)
211
212 contained_b_static = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', no_dynamic)
213 if not contained_b_static:
214 contained_b_static = contained_auto_ptr.GetChildMemberWithName ('__ptr_', no_dynamic)
215 self.assertTrue (contained_b_static)
216
217 contained_b_addr = int (contained_b.GetValue(), 16)
218 contained_b_static_addr = int (contained_b_static.GetValue(), 16)
219
220 self.assertTrue (contained_b_addr < contained_b_static_addr)