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 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| 8 | import lldb, lldbutil |
| 9 | from lldbtest import * |
| 10 | |
| 11 | class DynamicValueTestCase(TestBase): |
| 12 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 13 | mydir = TestBase.compute_mydir(__file__) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 14 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 15 | @skipUnlessDarwin |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 16 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 17 | @dsym_test |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 18 | def test_get_dynamic_vals_with_dsym(self): |
| 19 | """Test fetching C++ dynamic values from pointers & references.""" |
Andrew Kaylor | 7268e6e | 2013-10-31 19:42:35 +0000 | [diff] [blame] | 20 | self.buildDsym(dictionary=self.getBuildFlags()) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 21 | self.do_get_dynamic_vals() |
| 22 | |
Ed Maste | e63b512 | 2014-04-07 13:42:05 +0000 | [diff] [blame] | 23 | @expectedFailureFreeBSD # FIXME: This needs to be root-caused. |
Andrew Kaylor | 70ce6d2 | 2013-10-31 22:07:11 +0000 | [diff] [blame] | 24 | @expectedFailureLinux # FIXME: This needs to be root-caused. It looks like the DWARF info is anticipating the derived class assignment. |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 25 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 26 | @dwarf_test |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 27 | def test_get_dynamic_vals_with_dwarf(self): |
| 28 | """Test fetching C++ dynamic values from pointers & references.""" |
Andrew Kaylor | 7268e6e | 2013-10-31 19:42:35 +0000 | [diff] [blame] | 29 | self.buildDwarf(dictionary=self.getBuildFlags()) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 30 | 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 Granata | 38c5463 | 2013-10-30 00:04:29 +0000 | [diff] [blame] | 44 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 45 | def examine_value_object_of_this_ptr (self, this_static, this_dynamic, dynamic_location): |
| 46 | |
| 47 | # Get "this" as its static value |
| 48 | |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 49 | self.assertTrue (this_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 50 | this_static_loc = int (this_static.GetValue(), 16) |
| 51 | |
| 52 | # Get "this" as its dynamic value |
| 53 | |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 54 | self.assertTrue (this_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 55 | this_dynamic_typename = this_dynamic.GetTypeName() |
| 56 | self.assertTrue (this_dynamic_typename.find('B') != -1) |
| 57 | this_dynamic_loc = int (this_dynamic.GetValue(), 16) |
| 58 | |
| 59 | # Make sure we got the right address for "this" |
| 60 | |
| 61 | self.assertTrue (this_dynamic_loc == dynamic_location) |
| 62 | |
| 63 | # And that the static address is greater than the dynamic one |
| 64 | |
| 65 | self.assertTrue (this_static_loc > this_dynamic_loc) |
| 66 | |
| 67 | # Now read m_b_value which is only in the dynamic value: |
| 68 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 69 | use_dynamic = lldb.eDynamicCanRunTarget |
| 70 | no_dynamic = lldb.eNoDynamicValues |
| 71 | |
| 72 | this_dynamic_m_b_value = this_dynamic.GetChildMemberWithName('m_b_value', use_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 73 | self.assertTrue (this_dynamic_m_b_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 74 | |
| 75 | m_b_value = int (this_dynamic_m_b_value.GetValue(), 0) |
| 76 | self.assertTrue (m_b_value == 10) |
| 77 | |
| 78 | # Make sure it is not in the static version |
| 79 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 80 | this_static_m_b_value = this_static.GetChildMemberWithName('m_b_value', no_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 81 | self.assertFalse (this_static_m_b_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 82 | |
| 83 | # Okay, now let's make sure that we can get the dynamic type of a child element: |
| 84 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 85 | contained_auto_ptr = this_dynamic.GetChildMemberWithName ('m_client_A', use_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 86 | self.assertTrue (contained_auto_ptr) |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 87 | contained_b = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', use_dynamic) |
Enrico Granata | a0f9512 | 2013-04-11 22:55:45 +0000 | [diff] [blame] | 88 | if not contained_b: |
| 89 | contained_b = contained_auto_ptr.GetChildMemberWithName ('__ptr_', use_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 90 | self.assertTrue (contained_b) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 91 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 92 | contained_b_static = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', no_dynamic) |
Enrico Granata | a0f9512 | 2013-04-11 22:55:45 +0000 | [diff] [blame] | 93 | if not contained_b_static: |
| 94 | contained_b_static = contained_auto_ptr.GetChildMemberWithName ('__ptr_', no_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 95 | self.assertTrue (contained_b_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 96 | |
| 97 | contained_b_addr = int (contained_b.GetValue(), 16) |
| 98 | contained_b_static_addr = int (contained_b_static.GetValue(), 16) |
| 99 | |
| 100 | self.assertTrue (contained_b_addr < contained_b_static_addr) |
| 101 | |
| 102 | def do_get_dynamic_vals(self): |
| 103 | """Get argument vals for the call stack when stopped on a breakpoint.""" |
| 104 | exe = os.path.join(os.getcwd(), "a.out") |
| 105 | |
| 106 | # Create a target from the debugger. |
| 107 | |
Johnny Chen | 6b2a27c | 2011-04-25 18:20:52 +0000 | [diff] [blame] | 108 | target = self.dbg.CreateTarget (exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 109 | self.assertTrue(target, VALID_TARGET) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 110 | |
| 111 | # Set up our breakpoints: |
| 112 | |
| 113 | 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] | 114 | self.assertTrue(do_something_bpt, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 115 | VALID_BREAKPOINT) |
| 116 | |
| 117 | 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] | 118 | self.assertTrue(first_call_bpt, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 119 | VALID_BREAKPOINT) |
| 120 | |
| 121 | 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] | 122 | self.assertTrue(second_call_bpt, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 123 | VALID_BREAKPOINT) |
| 124 | |
| 125 | # Now launch the process, and do not stop at the entry point. |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 126 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 127 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 128 | self.assertTrue(process.GetState() == lldb.eStateStopped, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 129 | PROCESS_STOPPED) |
| 130 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 131 | threads = lldbutil.get_threads_stopped_at_breakpoint (process, first_call_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 132 | self.assertTrue (len(threads) == 1) |
| 133 | thread = threads[0] |
| 134 | |
| 135 | frame = thread.GetFrameAtIndex(0) |
| 136 | |
| 137 | # Now find the dynamic addresses of myB and otherB so we can compare them |
| 138 | # with the dynamic values we get in doSomething: |
| 139 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 140 | use_dynamic = lldb.eDynamicCanRunTarget |
| 141 | no_dynamic = lldb.eNoDynamicValues |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 142 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 143 | myB = frame.FindVariable ('myB', no_dynamic); |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 144 | self.assertTrue (myB) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 145 | myB_loc = int (myB.GetLocation(), 16) |
| 146 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 147 | otherB = frame.FindVariable('otherB', no_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 148 | self.assertTrue (otherB) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 149 | otherB_loc = int (otherB.GetLocation(), 16) |
| 150 | |
| 151 | # Okay now run to doSomething: |
| 152 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 153 | threads = lldbutil.continue_to_breakpoint (process, do_something_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 154 | self.assertTrue (len(threads) == 1) |
| 155 | thread = threads[0] |
| 156 | |
| 157 | frame = thread.GetFrameAtIndex(0) |
| 158 | |
| 159 | # Get "this" using FindVariable: |
| 160 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 161 | this_static = frame.FindVariable ('this', no_dynamic) |
| 162 | this_dynamic = frame.FindVariable ('this', use_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 163 | self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 164 | |
Jim Ingham | 60dbabb | 2011-12-08 19:44:08 +0000 | [diff] [blame] | 165 | # Now make sure that the "GetDynamicValue" works: |
| 166 | # This doesn't work currently because we can't get dynamic values from ConstResult objects. |
| 167 | fetched_dynamic_value = this_static.GetDynamicValue(use_dynamic) |
| 168 | self.examine_value_object_of_this_ptr (this_static, fetched_dynamic_value, myB_loc) |
| 169 | |
| 170 | # And conversely that the GetDynamicValue() interface also works: |
| 171 | fetched_static_value = this_dynamic.GetStaticValue() |
| 172 | self.examine_value_object_of_this_ptr (fetched_static_value, this_dynamic, myB_loc) |
| 173 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 174 | # Get "this" using FindValue, make sure that works too: |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 175 | this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, no_dynamic) |
| 176 | this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, use_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 177 | self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 178 | |
| 179 | # Get "this" using the EvaluateExpression: |
Jim Ingham | de4b919 | 2011-10-07 18:02:54 +0000 | [diff] [blame] | 180 | this_static = frame.EvaluateExpression ('this', False) |
| 181 | this_dynamic = frame.EvaluateExpression ('this', True) |
| 182 | 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] | 183 | |
| 184 | # The "frame var" code uses another path to get into children, so let's |
| 185 | # make sure that works as well: |
| 186 | |
Enrico Granata | a0f9512 | 2013-04-11 22:55:45 +0000 | [diff] [blame] | 187 | 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] | 188 | patterns = ['\(B \*\)']) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 189 | |
| 190 | # Now make sure we also get it right for a reference as well: |
| 191 | |
| 192 | anotherA_static = frame.FindVariable ('anotherA', False) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 193 | self.assertTrue (anotherA_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 194 | anotherA_static_addr = int (anotherA_static.GetValue(), 16) |
| 195 | |
| 196 | anotherA_dynamic = frame.FindVariable ('anotherA', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 197 | self.assertTrue (anotherA_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 198 | anotherA_dynamic_addr = int (anotherA_dynamic.GetValue(), 16) |
| 199 | anotherA_dynamic_typename = anotherA_dynamic.GetTypeName() |
| 200 | self.assertTrue (anotherA_dynamic_typename.find('B') != -1) |
| 201 | |
| 202 | self.assertTrue(anotherA_dynamic_addr < anotherA_static_addr) |
| 203 | |
| 204 | anotherA_m_b_value_dynamic = anotherA_dynamic.GetChildMemberWithName('m_b_value', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 205 | self.assertTrue (anotherA_m_b_value_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 206 | anotherA_m_b_val = int (anotherA_m_b_value_dynamic.GetValue(), 10) |
| 207 | self.assertTrue (anotherA_m_b_val == 300) |
| 208 | |
| 209 | anotherA_m_b_value_static = anotherA_static.GetChildMemberWithName('m_b_value', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 210 | self.assertFalse (anotherA_m_b_value_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 211 | |
| 212 | # Okay, now continue again, and when we hit the second breakpoint in main |
| 213 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 214 | threads = lldbutil.continue_to_breakpoint (process, second_call_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 215 | self.assertTrue (len(threads) == 1) |
| 216 | thread = threads[0] |
| 217 | |
| 218 | frame = thread.GetFrameAtIndex(0) |
| 219 | reallyA_value = frame.FindVariable ('reallyA', False) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 220 | self.assertTrue(reallyA_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 221 | reallyA_loc = int (reallyA_value.GetLocation(), 16) |
| 222 | |
| 223 | # Finally continue to doSomething again, and make sure we get the right value for anotherA, |
| 224 | # which this time around is just an "A". |
| 225 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 226 | threads = lldbutil.continue_to_breakpoint (process, do_something_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 227 | self.assertTrue(len(threads) == 1) |
| 228 | thread = threads[0] |
| 229 | |
| 230 | frame = thread.GetFrameAtIndex(0) |
| 231 | anotherA_value = frame.FindVariable ('anotherA', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 232 | self.assertTrue(anotherA_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 233 | anotherA_loc = int (anotherA_value.GetValue(), 16) |
| 234 | self.assertTrue (anotherA_loc == reallyA_loc) |
| 235 | self.assertTrue (anotherA_value.GetTypeName().find ('B') == -1) |
| 236 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 237 | if __name__ == '__main__': |
| 238 | import atexit |
| 239 | lldb.SBDebugger.Initialize() |
| 240 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 241 | unittest2.main() |