blob: 34ecbd3d3dd92fd5ff7f27b14a59930444d74b8d [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
Andrew Kaylor70ce6d22013-10-31 22:07:11 +000023 @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 +000024 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000025 @dwarf_test
Jim Ingham78a685a2011-04-16 00:01:13 +000026 def test_get_dynamic_vals_with_dwarf(self):
27 """Test fetching C++ dynamic values from pointers & references."""
Andrew Kaylor7268e6e2013-10-31 19:42:35 +000028 self.buildDwarf(dictionary=self.getBuildFlags())
Jim Ingham78a685a2011-04-16 00:01:13 +000029 self.do_get_dynamic_vals()
30
31 def setUp(self):
32 # Call super's setUp().
33 TestBase.setUp(self)
34
35 # Find the line number to break for main.c.
36
37 self.do_something_line = line_number('pass-to-base.cpp', '// Break here in doSomething.')
38 self.main_first_call_line = line_number('pass-to-base.cpp',
39 '// Break here and get real addresses of myB and otherB.')
40 self.main_second_call_line = line_number('pass-to-base.cpp',
41 '// Break here and get real address of reallyA.')
42
Enrico Granata38c54632013-10-30 00:04:29 +000043 self.main_third_call_line = line_number('pass-to-base.cpp',
44 '// Break here and check b has 0 children')
45 self.main_fourth_call_line = line_number('pass-to-base.cpp',
46 '// Break here and check b still has 0 children')
47 self.main_fifth_call_line = line_number('pass-to-base.cpp',
48 '// Break here and check b has one child now')
49
50
51
52
Jim Ingham78a685a2011-04-16 00:01:13 +000053 def examine_value_object_of_this_ptr (self, this_static, this_dynamic, dynamic_location):
54
55 # Get "this" as its static value
56
Johnny Chen4ebd0192011-05-24 18:22:45 +000057 self.assertTrue (this_static)
Jim Ingham78a685a2011-04-16 00:01:13 +000058 this_static_loc = int (this_static.GetValue(), 16)
59
60 # Get "this" as its dynamic value
61
Johnny Chen4ebd0192011-05-24 18:22:45 +000062 self.assertTrue (this_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +000063 this_dynamic_typename = this_dynamic.GetTypeName()
64 self.assertTrue (this_dynamic_typename.find('B') != -1)
65 this_dynamic_loc = int (this_dynamic.GetValue(), 16)
66
67 # Make sure we got the right address for "this"
68
69 self.assertTrue (this_dynamic_loc == dynamic_location)
70
71 # And that the static address is greater than the dynamic one
72
73 self.assertTrue (this_static_loc > this_dynamic_loc)
74
75 # Now read m_b_value which is only in the dynamic value:
76
Jim Ingham2837b762011-05-04 03:43:18 +000077 use_dynamic = lldb.eDynamicCanRunTarget
78 no_dynamic = lldb.eNoDynamicValues
79
80 this_dynamic_m_b_value = this_dynamic.GetChildMemberWithName('m_b_value', use_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000081 self.assertTrue (this_dynamic_m_b_value)
Jim Ingham78a685a2011-04-16 00:01:13 +000082
83 m_b_value = int (this_dynamic_m_b_value.GetValue(), 0)
84 self.assertTrue (m_b_value == 10)
85
86 # Make sure it is not in the static version
87
Jim Ingham2837b762011-05-04 03:43:18 +000088 this_static_m_b_value = this_static.GetChildMemberWithName('m_b_value', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000089 self.assertFalse (this_static_m_b_value)
Jim Ingham78a685a2011-04-16 00:01:13 +000090
91 # Okay, now let's make sure that we can get the dynamic type of a child element:
92
Jim Ingham2837b762011-05-04 03:43:18 +000093 contained_auto_ptr = this_dynamic.GetChildMemberWithName ('m_client_A', use_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000094 self.assertTrue (contained_auto_ptr)
Jim Ingham2837b762011-05-04 03:43:18 +000095 contained_b = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', use_dynamic)
Enrico Granataa0f95122013-04-11 22:55:45 +000096 if not contained_b:
97 contained_b = contained_auto_ptr.GetChildMemberWithName ('__ptr_', use_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +000098 self.assertTrue (contained_b)
Jim Ingham78a685a2011-04-16 00:01:13 +000099
Jim Ingham2837b762011-05-04 03:43:18 +0000100 contained_b_static = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', no_dynamic)
Enrico Granataa0f95122013-04-11 22:55:45 +0000101 if not contained_b_static:
102 contained_b_static = contained_auto_ptr.GetChildMemberWithName ('__ptr_', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000103 self.assertTrue (contained_b_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000104
105 contained_b_addr = int (contained_b.GetValue(), 16)
106 contained_b_static_addr = int (contained_b_static.GetValue(), 16)
107
108 self.assertTrue (contained_b_addr < contained_b_static_addr)
109
110 def do_get_dynamic_vals(self):
111 """Get argument vals for the call stack when stopped on a breakpoint."""
112 exe = os.path.join(os.getcwd(), "a.out")
113
114 # Create a target from the debugger.
115
Johnny Chen6b2a27c2011-04-25 18:20:52 +0000116 target = self.dbg.CreateTarget (exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000117 self.assertTrue(target, VALID_TARGET)
Jim Ingham78a685a2011-04-16 00:01:13 +0000118
119 # Set up our breakpoints:
120
121 do_something_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.do_something_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000122 self.assertTrue(do_something_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +0000123 VALID_BREAKPOINT)
124
125 first_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_first_call_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000126 self.assertTrue(first_call_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +0000127 VALID_BREAKPOINT)
128
129 second_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_second_call_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000130 self.assertTrue(second_call_bpt,
Jim Ingham78a685a2011-04-16 00:01:13 +0000131 VALID_BREAKPOINT)
Enrico Granata38c54632013-10-30 00:04:29 +0000132 third_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_third_call_line)
133 self.assertTrue(third_call_bpt,
134 VALID_BREAKPOINT)
135 fourth_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_fourth_call_line)
136 self.assertTrue(fourth_call_bpt,
137 VALID_BREAKPOINT)
138 fifth_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_fifth_call_line)
139 self.assertTrue(fifth_call_bpt,
140 VALID_BREAKPOINT)
Jim Ingham78a685a2011-04-16 00:01:13 +0000141
142 # Now launch the process, and do not stop at the entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000143 process = target.LaunchSimple (None, None, os.getcwd())
Jim Ingham78a685a2011-04-16 00:01:13 +0000144
Johnny Chen5a0bee72011-06-15 22:14:12 +0000145 self.assertTrue(process.GetState() == lldb.eStateStopped,
Jim Ingham78a685a2011-04-16 00:01:13 +0000146 PROCESS_STOPPED)
147
Johnny Chen5a0bee72011-06-15 22:14:12 +0000148 threads = lldbutil.get_threads_stopped_at_breakpoint (process, first_call_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000149 self.assertTrue (len(threads) == 1)
150 thread = threads[0]
151
152 frame = thread.GetFrameAtIndex(0)
153
154 # Now find the dynamic addresses of myB and otherB so we can compare them
155 # with the dynamic values we get in doSomething:
156
Jim Ingham2837b762011-05-04 03:43:18 +0000157 use_dynamic = lldb.eDynamicCanRunTarget
158 no_dynamic = lldb.eNoDynamicValues
Jim Ingham78a685a2011-04-16 00:01:13 +0000159
Jim Ingham2837b762011-05-04 03:43:18 +0000160 myB = frame.FindVariable ('myB', no_dynamic);
Johnny Chen4ebd0192011-05-24 18:22:45 +0000161 self.assertTrue (myB)
Jim Ingham78a685a2011-04-16 00:01:13 +0000162 myB_loc = int (myB.GetLocation(), 16)
163
Jim Ingham2837b762011-05-04 03:43:18 +0000164 otherB = frame.FindVariable('otherB', no_dynamic)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000165 self.assertTrue (otherB)
Jim Ingham78a685a2011-04-16 00:01:13 +0000166 otherB_loc = int (otherB.GetLocation(), 16)
167
168 # Okay now run to doSomething:
169
Johnny Chen5a0bee72011-06-15 22:14:12 +0000170 threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000171 self.assertTrue (len(threads) == 1)
172 thread = threads[0]
173
174 frame = thread.GetFrameAtIndex(0)
175
176 # Get "this" using FindVariable:
177
Jim Ingham2837b762011-05-04 03:43:18 +0000178 this_static = frame.FindVariable ('this', no_dynamic)
179 this_dynamic = frame.FindVariable ('this', use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000180 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
181
Jim Ingham60dbabb2011-12-08 19:44:08 +0000182 # Now make sure that the "GetDynamicValue" works:
183 # This doesn't work currently because we can't get dynamic values from ConstResult objects.
184 fetched_dynamic_value = this_static.GetDynamicValue(use_dynamic)
185 self.examine_value_object_of_this_ptr (this_static, fetched_dynamic_value, myB_loc)
186
187 # And conversely that the GetDynamicValue() interface also works:
188 fetched_static_value = this_dynamic.GetStaticValue()
189 self.examine_value_object_of_this_ptr (fetched_static_value, this_dynamic, myB_loc)
190
Jim Ingham78a685a2011-04-16 00:01:13 +0000191 # Get "this" using FindValue, make sure that works too:
Jim Ingham2837b762011-05-04 03:43:18 +0000192 this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, no_dynamic)
193 this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000194 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
195
196 # Get "this" using the EvaluateExpression:
Jim Inghamde4b9192011-10-07 18:02:54 +0000197 this_static = frame.EvaluateExpression ('this', False)
198 this_dynamic = frame.EvaluateExpression ('this', True)
199 self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc)
Jim Ingham78a685a2011-04-16 00:01:13 +0000200
201 # The "frame var" code uses another path to get into children, so let's
202 # make sure that works as well:
203
Enrico Granataa0f95122013-04-11 22:55:45 +0000204 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 +0000205 patterns = ['\(B \*\)'])
Jim Ingham78a685a2011-04-16 00:01:13 +0000206
207 # Now make sure we also get it right for a reference as well:
208
209 anotherA_static = frame.FindVariable ('anotherA', False)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000210 self.assertTrue (anotherA_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000211 anotherA_static_addr = int (anotherA_static.GetValue(), 16)
212
213 anotherA_dynamic = frame.FindVariable ('anotherA', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000214 self.assertTrue (anotherA_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000215 anotherA_dynamic_addr = int (anotherA_dynamic.GetValue(), 16)
216 anotherA_dynamic_typename = anotherA_dynamic.GetTypeName()
217 self.assertTrue (anotherA_dynamic_typename.find('B') != -1)
218
219 self.assertTrue(anotherA_dynamic_addr < anotherA_static_addr)
220
221 anotherA_m_b_value_dynamic = anotherA_dynamic.GetChildMemberWithName('m_b_value', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000222 self.assertTrue (anotherA_m_b_value_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000223 anotherA_m_b_val = int (anotherA_m_b_value_dynamic.GetValue(), 10)
224 self.assertTrue (anotherA_m_b_val == 300)
225
226 anotherA_m_b_value_static = anotherA_static.GetChildMemberWithName('m_b_value', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000227 self.assertFalse (anotherA_m_b_value_static)
Jim Ingham78a685a2011-04-16 00:01:13 +0000228
229 # Okay, now continue again, and when we hit the second breakpoint in main
230
Johnny Chen5a0bee72011-06-15 22:14:12 +0000231 threads = lldbutil.continue_to_breakpoint (process, second_call_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000232 self.assertTrue (len(threads) == 1)
233 thread = threads[0]
234
235 frame = thread.GetFrameAtIndex(0)
236 reallyA_value = frame.FindVariable ('reallyA', False)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000237 self.assertTrue(reallyA_value)
Jim Ingham78a685a2011-04-16 00:01:13 +0000238 reallyA_loc = int (reallyA_value.GetLocation(), 16)
239
240 # Finally continue to doSomething again, and make sure we get the right value for anotherA,
241 # which this time around is just an "A".
242
Johnny Chen5a0bee72011-06-15 22:14:12 +0000243 threads = lldbutil.continue_to_breakpoint (process, do_something_bpt)
Jim Ingham78a685a2011-04-16 00:01:13 +0000244 self.assertTrue(len(threads) == 1)
245 thread = threads[0]
246
247 frame = thread.GetFrameAtIndex(0)
248 anotherA_value = frame.FindVariable ('anotherA', True)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000249 self.assertTrue(anotherA_value)
Jim Ingham78a685a2011-04-16 00:01:13 +0000250 anotherA_loc = int (anotherA_value.GetValue(), 16)
251 self.assertTrue (anotherA_loc == reallyA_loc)
252 self.assertTrue (anotherA_value.GetTypeName().find ('B') == -1)
253
Enrico Granata38c54632013-10-30 00:04:29 +0000254 self.runCmd("continue")
Enrico Granata0ede1092013-10-31 22:49:31 +0000255# self.runCmd("frame select 0")
256# self.runCmd("frame variable")
Enrico Granata38c54632013-10-30 00:04:29 +0000257 b = self.frame().FindVariable("b").GetDynamicValue(lldb.eDynamicCanRunTarget)
258 self.assertTrue(b.GetNumChildren() == 0, "b has 0 children")
Enrico Granata0ede1092013-10-31 22:49:31 +0000259 self.runCmd("next")
260# self.runCmd("frame select 0")
261# self.runCmd("frame variable")
Enrico Granata38c54632013-10-30 00:04:29 +0000262 self.assertTrue(b.GetNumChildren() == 0, "b still has 0 children")
263 self.runCmd("continue")
Enrico Granata0ede1092013-10-31 22:49:31 +0000264# self.runCmd("frame select 0")
265# self.runCmd("frame variable")
266 self.assertTrue(b.GetNumChildren() != 0, "b now has 1 child")
Enrico Granata38c54632013-10-30 00:04:29 +0000267
Jim Ingham78a685a2011-04-16 00:01:13 +0000268if __name__ == '__main__':
269 import atexit
270 lldb.SBDebugger.Initialize()
271 atexit.register(lambda: lldb.SBDebugger.Terminate())
272 unittest2.main()