Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 1 | """ |
| 2 | Use lldb Python API to test dynamic values in C++ |
| 3 | """ |
| 4 | |
Zachary Turner | 77db4a8 | 2015-10-22 20:06:20 +0000 | [diff] [blame] | 5 | import lldb_shared |
| 6 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 7 | import os, time |
| 8 | import re |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 9 | import lldb, lldbutil |
| 10 | from lldbtest import * |
| 11 | |
| 12 | class DynamicValueTestCase(TestBase): |
| 13 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 14 | mydir = TestBase.compute_mydir(__file__) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 15 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 16 | 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 Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 28 | @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 Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 34 | exe = os.path.join(os.getcwd(), "a.out") |
| 35 | |
| 36 | # Create a target from the debugger. |
| 37 | |
Johnny Chen | 6b2a27c | 2011-04-25 18:20:52 +0000 | [diff] [blame] | 38 | target = self.dbg.CreateTarget (exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 39 | self.assertTrue(target, VALID_TARGET) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 40 | |
| 41 | # Set up our breakpoints: |
| 42 | |
| 43 | do_something_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.do_something_line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 44 | self.assertTrue(do_something_bpt, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 45 | VALID_BREAKPOINT) |
| 46 | |
| 47 | first_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_first_call_line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 48 | self.assertTrue(first_call_bpt, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 49 | VALID_BREAKPOINT) |
| 50 | |
| 51 | second_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_second_call_line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 52 | self.assertTrue(second_call_bpt, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 53 | VALID_BREAKPOINT) |
| 54 | |
| 55 | # Now launch the process, and do not stop at the entry point. |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 56 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 57 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 58 | self.assertTrue(process.GetState() == lldb.eStateStopped, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 59 | PROCESS_STOPPED) |
| 60 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 61 | threads = lldbutil.get_threads_stopped_at_breakpoint (process, first_call_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 62 | 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 Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 70 | use_dynamic = lldb.eDynamicCanRunTarget |
| 71 | no_dynamic = lldb.eNoDynamicValues |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 72 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 73 | myB = frame.FindVariable ('myB', no_dynamic); |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 74 | self.assertTrue (myB) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 75 | myB_loc = int (myB.GetLocation(), 16) |
| 76 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 77 | otherB = frame.FindVariable('otherB', no_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 78 | self.assertTrue (otherB) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 79 | otherB_loc = int (otherB.GetLocation(), 16) |
| 80 | |
| 81 | # Okay now run to doSomething: |
| 82 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 83 | threads = lldbutil.continue_to_breakpoint (process, do_something_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 84 | self.assertTrue (len(threads) == 1) |
| 85 | thread = threads[0] |
| 86 | |
| 87 | frame = thread.GetFrameAtIndex(0) |
| 88 | |
| 89 | # Get "this" using FindVariable: |
| 90 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 91 | this_static = frame.FindVariable ('this', no_dynamic) |
| 92 | this_dynamic = frame.FindVariable ('this', use_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 93 | self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 94 | |
Jim Ingham | 60dbabb | 2011-12-08 19:44:08 +0000 | [diff] [blame] | 95 | # 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 Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 104 | # Get "this" using FindValue, make sure that works too: |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 105 | this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, no_dynamic) |
| 106 | this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, use_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 107 | self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 108 | |
| 109 | # Get "this" using the EvaluateExpression: |
Jim Ingham | de4b919 | 2011-10-07 18:02:54 +0000 | [diff] [blame] | 110 | 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 Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 113 | |
| 114 | # The "frame var" code uses another path to get into children, so let's |
| 115 | # make sure that works as well: |
| 116 | |
Enrico Granata | a0f9512 | 2013-04-11 22:55:45 +0000 | [diff] [blame] | 117 | 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 Clayton | e305594 | 2011-06-30 02:28:26 +0000 | [diff] [blame] | 118 | patterns = ['\(B \*\)']) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 119 | |
| 120 | # Now make sure we also get it right for a reference as well: |
| 121 | |
| 122 | anotherA_static = frame.FindVariable ('anotherA', False) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 123 | self.assertTrue (anotherA_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 124 | anotherA_static_addr = int (anotherA_static.GetValue(), 16) |
| 125 | |
| 126 | anotherA_dynamic = frame.FindVariable ('anotherA', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 127 | self.assertTrue (anotherA_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 128 | 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 Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 135 | self.assertTrue (anotherA_m_b_value_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 136 | 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 Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 140 | self.assertFalse (anotherA_m_b_value_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 141 | |
| 142 | # Okay, now continue again, and when we hit the second breakpoint in main |
| 143 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 144 | threads = lldbutil.continue_to_breakpoint (process, second_call_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 145 | self.assertTrue (len(threads) == 1) |
| 146 | thread = threads[0] |
| 147 | |
| 148 | frame = thread.GetFrameAtIndex(0) |
| 149 | reallyA_value = frame.FindVariable ('reallyA', False) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 150 | self.assertTrue(reallyA_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 151 | 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 Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 156 | threads = lldbutil.continue_to_breakpoint (process, do_something_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 157 | self.assertTrue(len(threads) == 1) |
| 158 | thread = threads[0] |
| 159 | |
| 160 | frame = thread.GetFrameAtIndex(0) |
| 161 | anotherA_value = frame.FindVariable ('anotherA', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 162 | self.assertTrue(anotherA_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 163 | 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 Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 167 | 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) |