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 | |
Johnny Chen | 91b6b77 | 2011-06-25 20:19:47 +0000 | [diff] [blame^] | 13 | mydir = os.path.join("lang", "cpp", "dynamic-value") |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 14 | |
| 15 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 16 | @python_api_test |
| 17 | def test_get_dynamic_vals_with_dsym(self): |
| 18 | """Test fetching C++ dynamic values from pointers & references.""" |
| 19 | self.buildDsym() |
| 20 | self.do_get_dynamic_vals() |
| 21 | |
| 22 | @python_api_test |
| 23 | def test_get_dynamic_vals_with_dwarf(self): |
| 24 | """Test fetching C++ dynamic values from pointers & references.""" |
| 25 | self.buildDwarf() |
| 26 | self.do_get_dynamic_vals() |
| 27 | |
| 28 | def setUp(self): |
| 29 | # Call super's setUp(). |
| 30 | TestBase.setUp(self) |
| 31 | |
| 32 | # Find the line number to break for main.c. |
| 33 | |
| 34 | self.do_something_line = line_number('pass-to-base.cpp', '// Break here in doSomething.') |
| 35 | self.main_first_call_line = line_number('pass-to-base.cpp', |
| 36 | '// Break here and get real addresses of myB and otherB.') |
| 37 | self.main_second_call_line = line_number('pass-to-base.cpp', |
| 38 | '// Break here and get real address of reallyA.') |
| 39 | |
| 40 | def examine_value_object_of_this_ptr (self, this_static, this_dynamic, dynamic_location): |
| 41 | |
| 42 | # Get "this" as its static value |
| 43 | |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 44 | self.assertTrue (this_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 45 | this_static_loc = int (this_static.GetValue(), 16) |
| 46 | |
| 47 | # Get "this" as its dynamic value |
| 48 | |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 49 | self.assertTrue (this_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 50 | this_dynamic_typename = this_dynamic.GetTypeName() |
| 51 | self.assertTrue (this_dynamic_typename.find('B') != -1) |
| 52 | this_dynamic_loc = int (this_dynamic.GetValue(), 16) |
| 53 | |
| 54 | # Make sure we got the right address for "this" |
| 55 | |
| 56 | self.assertTrue (this_dynamic_loc == dynamic_location) |
| 57 | |
| 58 | # And that the static address is greater than the dynamic one |
| 59 | |
| 60 | self.assertTrue (this_static_loc > this_dynamic_loc) |
| 61 | |
| 62 | # Now read m_b_value which is only in the dynamic value: |
| 63 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 64 | use_dynamic = lldb.eDynamicCanRunTarget |
| 65 | no_dynamic = lldb.eNoDynamicValues |
| 66 | |
| 67 | 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] | 68 | self.assertTrue (this_dynamic_m_b_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 69 | |
| 70 | m_b_value = int (this_dynamic_m_b_value.GetValue(), 0) |
| 71 | self.assertTrue (m_b_value == 10) |
| 72 | |
| 73 | # Make sure it is not in the static version |
| 74 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 75 | 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] | 76 | self.assertFalse (this_static_m_b_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 77 | |
| 78 | # Okay, now let's make sure that we can get the dynamic type of a child element: |
| 79 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 80 | contained_auto_ptr = this_dynamic.GetChildMemberWithName ('m_client_A', use_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 81 | self.assertTrue (contained_auto_ptr) |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 82 | contained_b = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', use_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 83 | self.assertTrue (contained_b) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 84 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 85 | contained_b_static = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', no_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 86 | self.assertTrue (contained_b_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 87 | |
| 88 | contained_b_addr = int (contained_b.GetValue(), 16) |
| 89 | contained_b_static_addr = int (contained_b_static.GetValue(), 16) |
| 90 | |
| 91 | self.assertTrue (contained_b_addr < contained_b_static_addr) |
| 92 | |
| 93 | def do_get_dynamic_vals(self): |
| 94 | """Get argument vals for the call stack when stopped on a breakpoint.""" |
| 95 | exe = os.path.join(os.getcwd(), "a.out") |
| 96 | |
| 97 | # Create a target from the debugger. |
| 98 | |
Johnny Chen | 6b2a27c | 2011-04-25 18:20:52 +0000 | [diff] [blame] | 99 | target = self.dbg.CreateTarget (exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 100 | self.assertTrue(target, VALID_TARGET) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 101 | |
| 102 | # Set up our breakpoints: |
| 103 | |
| 104 | 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] | 105 | self.assertTrue(do_something_bpt, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 106 | VALID_BREAKPOINT) |
| 107 | |
| 108 | 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] | 109 | self.assertTrue(first_call_bpt, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 110 | VALID_BREAKPOINT) |
| 111 | |
| 112 | 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] | 113 | self.assertTrue(second_call_bpt, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 114 | VALID_BREAKPOINT) |
| 115 | |
| 116 | # Now launch the process, and do not stop at the entry point. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 117 | process = target.LaunchSimple (None, None, os.getcwd()) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 118 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 119 | self.assertTrue(process.GetState() == lldb.eStateStopped, |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 120 | PROCESS_STOPPED) |
| 121 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 122 | threads = lldbutil.get_threads_stopped_at_breakpoint (process, first_call_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 123 | self.assertTrue (len(threads) == 1) |
| 124 | thread = threads[0] |
| 125 | |
| 126 | frame = thread.GetFrameAtIndex(0) |
| 127 | |
| 128 | # Now find the dynamic addresses of myB and otherB so we can compare them |
| 129 | # with the dynamic values we get in doSomething: |
| 130 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 131 | use_dynamic = lldb.eDynamicCanRunTarget |
| 132 | no_dynamic = lldb.eNoDynamicValues |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 133 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 134 | myB = frame.FindVariable ('myB', no_dynamic); |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 135 | self.assertTrue (myB) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 136 | myB_loc = int (myB.GetLocation(), 16) |
| 137 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 138 | otherB = frame.FindVariable('otherB', no_dynamic) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 139 | self.assertTrue (otherB) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 140 | otherB_loc = int (otherB.GetLocation(), 16) |
| 141 | |
| 142 | # Okay now run to doSomething: |
| 143 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 144 | threads = lldbutil.continue_to_breakpoint (process, do_something_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 | |
| 150 | # Get "this" using FindVariable: |
| 151 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 152 | this_static = frame.FindVariable ('this', no_dynamic) |
| 153 | this_dynamic = frame.FindVariable ('this', use_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 154 | self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 155 | |
| 156 | # Get "this" using FindValue, make sure that works too: |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 157 | this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, no_dynamic) |
| 158 | this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, use_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 159 | self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 160 | |
| 161 | # Get "this" using the EvaluateExpression: |
| 162 | # These tests fail for now because EvaluateExpression doesn't currently support dynamic typing... |
| 163 | #this_static = frame.EvaluateExpression ('this', False) |
| 164 | #this_dynamic = frame.EvaluateExpression ('this', True) |
| 165 | #self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 166 | |
| 167 | # The "frame var" code uses another path to get into children, so let's |
| 168 | # make sure that works as well: |
| 169 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 170 | self.expect('frame var -d run-target anotherA.m_client_A._M_ptr', 'frame var finds its way into a child member', |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 171 | patterns = ['\(.* B \*\)']) |
| 172 | |
| 173 | # Now make sure we also get it right for a reference as well: |
| 174 | |
| 175 | anotherA_static = frame.FindVariable ('anotherA', False) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 176 | self.assertTrue (anotherA_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 177 | anotherA_static_addr = int (anotherA_static.GetValue(), 16) |
| 178 | |
| 179 | anotherA_dynamic = frame.FindVariable ('anotherA', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 180 | self.assertTrue (anotherA_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 181 | anotherA_dynamic_addr = int (anotherA_dynamic.GetValue(), 16) |
| 182 | anotherA_dynamic_typename = anotherA_dynamic.GetTypeName() |
| 183 | self.assertTrue (anotherA_dynamic_typename.find('B') != -1) |
| 184 | |
| 185 | self.assertTrue(anotherA_dynamic_addr < anotherA_static_addr) |
| 186 | |
| 187 | anotherA_m_b_value_dynamic = anotherA_dynamic.GetChildMemberWithName('m_b_value', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 188 | self.assertTrue (anotherA_m_b_value_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 189 | anotherA_m_b_val = int (anotherA_m_b_value_dynamic.GetValue(), 10) |
| 190 | self.assertTrue (anotherA_m_b_val == 300) |
| 191 | |
| 192 | anotherA_m_b_value_static = anotherA_static.GetChildMemberWithName('m_b_value', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 193 | self.assertFalse (anotherA_m_b_value_static) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 194 | |
| 195 | # Okay, now continue again, and when we hit the second breakpoint in main |
| 196 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 197 | threads = lldbutil.continue_to_breakpoint (process, second_call_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 198 | self.assertTrue (len(threads) == 1) |
| 199 | thread = threads[0] |
| 200 | |
| 201 | frame = thread.GetFrameAtIndex(0) |
| 202 | reallyA_value = frame.FindVariable ('reallyA', False) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 203 | self.assertTrue(reallyA_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 204 | reallyA_loc = int (reallyA_value.GetLocation(), 16) |
| 205 | |
| 206 | # Finally continue to doSomething again, and make sure we get the right value for anotherA, |
| 207 | # which this time around is just an "A". |
| 208 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 209 | threads = lldbutil.continue_to_breakpoint (process, do_something_bpt) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 210 | self.assertTrue(len(threads) == 1) |
| 211 | thread = threads[0] |
| 212 | |
| 213 | frame = thread.GetFrameAtIndex(0) |
| 214 | anotherA_value = frame.FindVariable ('anotherA', True) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 215 | self.assertTrue(anotherA_value) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 216 | anotherA_loc = int (anotherA_value.GetValue(), 16) |
| 217 | self.assertTrue (anotherA_loc == reallyA_loc) |
| 218 | self.assertTrue (anotherA_value.GetTypeName().find ('B') == -1) |
| 219 | |
| 220 | if __name__ == '__main__': |
| 221 | import atexit |
| 222 | lldb.SBDebugger.Initialize() |
| 223 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 224 | unittest2.main() |