blob: 804b17fa9d56f0e44526eba51888cc378dffe228 [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."""
Andrew Kaylor7268e6e2013-10-31 19:42:35 +000020 self.buildDsym(dictionary=self.getBuildFlags())
Jim Ingham78a685a2011-04-16 00:01:13 +000021 self.do_get_dynamic_vals()
22
Ed Maste34bdbbd2013-09-13 15:34:59 +000023 @expectedFailureFreeBSD('llvm.org/pr17225')
Andrew Kaylor70ce6d22013-10-31 22:07:11 +000024 @expectedFailureLinux # FIXME: This needs to be root-caused. It looks like the DWARF info is anticipating the derived class assignment.
Jim Ingham78a685a2011-04-16 00:01:13 +000025 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000026 @dwarf_test
Jim Ingham78a685a2011-04-16 00:01:13 +000027 def test_get_dynamic_vals_with_dwarf(self):
28 """Test fetching C++ dynamic values from pointers & references."""
Andrew Kaylor7268e6e2013-10-31 19:42:35 +000029 self.buildDwarf(dictionary=self.getBuildFlags())
Jim Ingham78a685a2011-04-16 00:01:13 +000030 self.do_get_dynamic_vals()
31
32 def setUp(self):
33 # Call super's setUp().
34 TestBase.setUp(self)
35
36 # Find the line number to break for main.c.
37
38 self.do_something_line = line_number('pass-to-base.cpp', '// Break here in doSomething.')
39 self.main_first_call_line = line_number('pass-to-base.cpp',
40 '// Break here and get real addresses of myB and otherB.')
41 self.main_second_call_line = line_number('pass-to-base.cpp',
42 '// Break here and get real address of reallyA.')
43
Enrico Granata38c54632013-10-30 00:04:29 +000044 self.main_third_call_line = line_number('pass-to-base.cpp',
45 '// Break here and check b has 0 children')
46 self.main_fourth_call_line = line_number('pass-to-base.cpp',
47 '// Break here and check b still has 0 children')
48 self.main_fifth_call_line = line_number('pass-to-base.cpp',
49 '// Break here and check b has one child now')
50
51
52
53
Jim Ingham78a685a2011-04-16 00:01:13 +000054 def examine_value_object_of_this_ptr (self, this_static, this_dynamic, dynamic_location):
55
56 # Get "this" as its static value
57
Johnny Chen4ebd0192011-05-24 18:22:45 +000058 self.assertTrue (this_static)
Jim Ingham78a685a2011-04-16 00:01:13 +000059 this_static_loc = int (this_static.GetValue(), 16)
60
61 # Get "this" as its dynamic value
62
Johnny Chen4ebd0192011-05-24 18:22:45 +000063 self.assertTrue (this_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +000064 this_dynamic_typename = this_dynamic.GetTypeName()
65 self.assertTrue (this_dynamic_typename.find('B') != -1)
66 this_dynamic_loc = int (this_dynamic.GetValue(), 16)
67
68 # Make sure we got the right address for "this"
69
70 self.assertTrue (this_dynamic_loc == dynamic_location)
71
72 # And that the static address is greater than the dynamic one
73
74 self.assertTrue (this_static_loc > this_dynamic_loc)
75
76 # Now read m_b_value which is only in the dynamic value:
77
Jim Ingham2837b762011-05-04 03:43:18 +000078 use_dynamic = lldb.eDynamicCanRunTarget
79 no_dynamic = lldb.eNoDynamicValues
80
81 this_dynamic_m_b_value = this_dynamic.GetChildMemberWithName('m_b_value', use_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000082 self.assertTrue (this_dynamic_m_b_value)
Jim Ingham78a685a2011-04-16 00:01:13 +000083
84 m_b_value = int (this_dynamic_m_b_value.GetValue(), 0)
85 self.assertTrue (m_b_value == 10)
86
87 # Make sure it is not in the static version
88
Jim Ingham2837b762011-05-04 03:43:18 +000089 this_static_m_b_value = this_static.GetChildMemberWithName('m_b_value', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000090 self.assertFalse (this_static_m_b_value)
Jim Ingham78a685a2011-04-16 00:01:13 +000091
92 # Okay, now let's make sure that we can get the dynamic type of a child element:
93
Jim Ingham2837b762011-05-04 03:43:18 +000094 contained_auto_ptr = this_dynamic.GetChildMemberWithName ('m_client_A', use_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000095 self.assertTrue (contained_auto_ptr)
Jim Ingham2837b762011-05-04 03:43:18 +000096 contained_b = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', use_dynamic)
Enrico Granataa0f95122013-04-11 22:55:45 +000097 if not contained_b:
98 contained_b = contained_auto_ptr.GetChildMemberWithName ('__ptr_', use_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000099 self.assertTrue (contained_b)
Jim Ingham78a685a2011-04-16 00:01:13 +0000100
Jim Ingham2837b762011-05-04 03:43:18 +0000101 contained_b_static = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', no_dynamic)
Enrico Granataa0f95122013-04-11 22:55:45 +0000102 if not contained_b_static:
103 contained_b_static = contained_auto_ptr.GetChildMemberWithName ('__ptr_', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000104 self.assertTrue (contained_b_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000105
106 contained_b_addr = int (contained_b.GetValue(), 16)
107 contained_b_static_addr = int (contained_b_static.GetValue(), 16)
108
109 self.assertTrue (contained_b_addr < contained_b_static_addr)
110
111 def do_get_dynamic_vals(self):
112 """Get argument vals for the call stack when stopped on a breakpoint."""
113 exe = os.path.join(os.getcwd(), "a.out")
114
115 # Create a target from the debugger.
116
Johnny Chen6b2a27c2011-04-25 18:20:52 +0000117 target = self.dbg.CreateTarget (exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000118 self.assertTrue(target, VALID_TARGET)
Jim Ingham78a685a2011-04-16 00:01:13 +0000119
120 # Set up our breakpoints:
121
122 do_something_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.do_something_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000123 self.assertTrue(do_something_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +0000124 VALID_BREAKPOINT)
125
126 first_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_first_call_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000127 self.assertTrue(first_call_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +0000128 VALID_BREAKPOINT)
129
130 second_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_second_call_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000131 self.assertTrue(second_call_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +0000132 VALID_BREAKPOINT)
Enrico Granata38c54632013-10-30 00:04:29 +0000133 third_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_third_call_line)
134 self.assertTrue(third_call_bpt,
135 VALID_BREAKPOINT)
136 fourth_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_fourth_call_line)
137 self.assertTrue(fourth_call_bpt,
138 VALID_BREAKPOINT)
139 fifth_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_fifth_call_line)
140 self.assertTrue(fifth_call_bpt,
141 VALID_BREAKPOINT)
Jim Ingham78a685a2011-04-16 00:01:13 +0000142
143 # Now launch the process, and do not stop at the entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000144 process = target.LaunchSimple (None, None, os.getcwd())
Jim Ingham78a685a2011-04-16 00:01:13 +0000145
Johnny Chen5a0bee72011-06-15 22:14:12 +0000146 self.assertTrue(process.GetState() == lldb.eStateStopped,
Jim Ingham78a685a2011-04-16 00:01:13 +0000147 PROCESS_STOPPED)
148
Johnny Chen5a0bee72011-06-15 22:14:12 +0000149 threads = lldbutil.get_threads_stopped_at_breakpoint (process, first_call_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000150 self.assertTrue (len(threads) == 1)
151 thread = threads[0]
152
153 frame = thread.GetFrameAtIndex(0)
154
155 # Now find the dynamic addresses of myB and otherB so we can compare them
156 # with the dynamic values we get in doSomething:
157
Jim Ingham2837b762011-05-04 03:43:18 +0000158 use_dynamic = lldb.eDynamicCanRunTarget
159 no_dynamic = lldb.eNoDynamicValues
Jim Ingham78a685a2011-04-16 00:01:13 +0000160
Jim Ingham2837b762011-05-04 03:43:18 +0000161 myB = frame.FindVariable ('myB', no_dynamic);
Johnny Chen4ebd0192011-05-24 18:22:45 +0000162 self.assertTrue (myB)
Jim Ingham78a685a2011-04-16 00:01:13 +0000163 myB_loc = int (myB.GetLocation(), 16)
164
Jim Ingham2837b762011-05-04 03:43:18 +0000165 otherB = frame.FindVariable('otherB', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000166 self.assertTrue (otherB)
Jim Ingham78a685a2011-04-16 00:01:13 +0000167 otherB_loc = int (otherB.GetLocation(), 16)
168
169 # Okay now run to doSomething:
170
Johnny Chen5a0bee72011-06-15 22:14:12 +0000171 threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000172 self.assertTrue (len(threads) == 1)
173 thread = threads[0]
174
175 frame = thread.GetFrameAtIndex(0)
176
177 # Get "this" using FindVariable:
178
Jim Ingham2837b762011-05-04 03:43:18 +0000179 this_static = frame.FindVariable ('this', no_dynamic)
180 this_dynamic = frame.FindVariable ('this', use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000181 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
182
Jim Ingham60dbabb2011-12-08 19:44:08 +0000183 # Now make sure that the "GetDynamicValue" works:
184 # This doesn't work currently because we can't get dynamic values from ConstResult objects.
185 fetched_dynamic_value = this_static.GetDynamicValue(use_dynamic)
186 self.examine_value_object_of_this_ptr (this_static, fetched_dynamic_value, myB_loc)
187
188 # And conversely that the GetDynamicValue() interface also works:
189 fetched_static_value = this_dynamic.GetStaticValue()
190 self.examine_value_object_of_this_ptr (fetched_static_value, this_dynamic, myB_loc)
191
Jim Ingham78a685a2011-04-16 00:01:13 +0000192 # Get "this" using FindValue, make sure that works too:
Jim Ingham2837b762011-05-04 03:43:18 +0000193 this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, no_dynamic)
194 this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000195 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
196
197 # Get "this" using the EvaluateExpression:
Jim Inghamde4b9192011-10-07 18:02:54 +0000198 this_static = frame.EvaluateExpression ('this', False)
199 this_dynamic = frame.EvaluateExpression ('this', True)
200 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
Jim Ingham78a685a2011-04-16 00:01:13 +0000201
202 # The "frame var" code uses another path to get into children, so let's
203 # make sure that works as well:
204
Enrico Granataa0f95122013-04-11 22:55:45 +0000205 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 +0000206 patterns = ['\(B \*\)'])
Jim Ingham78a685a2011-04-16 00:01:13 +0000207
208 # Now make sure we also get it right for a reference as well:
209
210 anotherA_static = frame.FindVariable ('anotherA', False)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000211 self.assertTrue (anotherA_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000212 anotherA_static_addr = int (anotherA_static.GetValue(), 16)
213
214 anotherA_dynamic = frame.FindVariable ('anotherA', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000215 self.assertTrue (anotherA_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000216 anotherA_dynamic_addr = int (anotherA_dynamic.GetValue(), 16)
217 anotherA_dynamic_typename = anotherA_dynamic.GetTypeName()
218 self.assertTrue (anotherA_dynamic_typename.find('B') != -1)
219
220 self.assertTrue(anotherA_dynamic_addr < anotherA_static_addr)
221
222 anotherA_m_b_value_dynamic = anotherA_dynamic.GetChildMemberWithName('m_b_value', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000223 self.assertTrue (anotherA_m_b_value_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000224 anotherA_m_b_val = int (anotherA_m_b_value_dynamic.GetValue(), 10)
225 self.assertTrue (anotherA_m_b_val == 300)
226
227 anotherA_m_b_value_static = anotherA_static.GetChildMemberWithName('m_b_value', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000228 self.assertFalse (anotherA_m_b_value_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000229
230 # Okay, now continue again, and when we hit the second breakpoint in main
231
Johnny Chen5a0bee72011-06-15 22:14:12 +0000232 threads = lldbutil.continue_to_breakpoint (process, second_call_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000233 self.assertTrue (len(threads) == 1)
234 thread = threads[0]
235
236 frame = thread.GetFrameAtIndex(0)
237 reallyA_value = frame.FindVariable ('reallyA', False)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000238 self.assertTrue(reallyA_value)
Jim Ingham78a685a2011-04-16 00:01:13 +0000239 reallyA_loc = int (reallyA_value.GetLocation(), 16)
240
241 # Finally continue to doSomething again, and make sure we get the right value for anotherA,
242 # which this time around is just an "A".
243
Johnny Chen5a0bee72011-06-15 22:14:12 +0000244 threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000245 self.assertTrue(len(threads) == 1)
246 thread = threads[0]
247
248 frame = thread.GetFrameAtIndex(0)
249 anotherA_value = frame.FindVariable ('anotherA', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000250 self.assertTrue(anotherA_value)
Jim Ingham78a685a2011-04-16 00:01:13 +0000251 anotherA_loc = int (anotherA_value.GetValue(), 16)
252 self.assertTrue (anotherA_loc == reallyA_loc)
253 self.assertTrue (anotherA_value.GetTypeName().find ('B') == -1)
254
Enrico Granata38c54632013-10-30 00:04:29 +0000255 self.runCmd("continue")
Enrico Granata0ede1092013-10-31 22:49:31 +0000256# self.runCmd("frame select 0")
257# self.runCmd("frame variable")
Enrico Granata38c54632013-10-30 00:04:29 +0000258 b = self.frame().FindVariable("b").GetDynamicValue(lldb.eDynamicCanRunTarget)
259 self.assertTrue(b.GetNumChildren() == 0, "b has 0 children")
Enrico Granata0ede1092013-10-31 22:49:31 +0000260 self.runCmd("next")
261# self.runCmd("frame select 0")
262# self.runCmd("frame variable")
Enrico Granata38c54632013-10-30 00:04:29 +0000263 self.assertTrue(b.GetNumChildren() == 0, "b still has 0 children")
264 self.runCmd("continue")
Enrico Granata0ede1092013-10-31 22:49:31 +0000265# self.runCmd("frame select 0")
266# self.runCmd("frame variable")
267 self.assertTrue(b.GetNumChildren() != 0, "b now has 1 child")
Enrico Granata38c54632013-10-30 00:04:29 +0000268
Jim Ingham78a685a2011-04-16 00:01:13 +0000269if __name__ == '__main__':
270 import atexit
271 lldb.SBDebugger.Initialize()
272 atexit.register(lambda: lldb.SBDebugger.Terminate())
273 unittest2.main()