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 | |
| 13 | mydir = os.path.join("cpp", "dynamic-value") |
| 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 | |
| 44 | self.assertTrue (this_static.IsValid()) |
| 45 | this_static_loc = int (this_static.GetValue(), 16) |
| 46 | |
| 47 | # Get "this" as its dynamic value |
| 48 | |
| 49 | self.assertTrue (this_dynamic.IsValid()) |
| 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 | |
| 64 | this_dynamic_m_b_value = this_dynamic.GetChildMemberWithName('m_b_value', True) |
| 65 | self.assertTrue (this_dynamic_m_b_value.IsValid()) |
| 66 | |
| 67 | m_b_value = int (this_dynamic_m_b_value.GetValue(), 0) |
| 68 | self.assertTrue (m_b_value == 10) |
| 69 | |
| 70 | # Make sure it is not in the static version |
| 71 | |
| 72 | this_static_m_b_value = this_static.GetChildMemberWithName('m_b_value', False) |
| 73 | self.assertTrue (this_static_m_b_value.IsValid() == False) |
| 74 | |
| 75 | # Okay, now let's make sure that we can get the dynamic type of a child element: |
| 76 | |
| 77 | contained_auto_ptr = this_dynamic.GetChildMemberWithName ('m_client_A', True) |
| 78 | self.assertTrue (contained_auto_ptr.IsValid()) |
| 79 | contained_b = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', True) |
| 80 | self.assertTrue (contained_b.IsValid()) |
| 81 | |
| 82 | contained_b_static = contained_auto_ptr.GetChildMemberWithName ('_M_ptr', False) |
| 83 | self.assertTrue (contained_b_static.IsValid()) |
| 84 | |
| 85 | contained_b_addr = int (contained_b.GetValue(), 16) |
| 86 | contained_b_static_addr = int (contained_b_static.GetValue(), 16) |
| 87 | |
| 88 | self.assertTrue (contained_b_addr < contained_b_static_addr) |
| 89 | |
| 90 | def do_get_dynamic_vals(self): |
| 91 | """Get argument vals for the call stack when stopped on a breakpoint.""" |
| 92 | exe = os.path.join(os.getcwd(), "a.out") |
| 93 | |
| 94 | # Create a target from the debugger. |
| 95 | |
| 96 | target = self.dbg.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT) |
| 97 | self.assertTrue(target.IsValid(), VALID_TARGET) |
| 98 | |
| 99 | # Set up our breakpoints: |
| 100 | |
| 101 | do_something_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.do_something_line) |
| 102 | self.assertTrue(do_something_bpt.IsValid() and |
| 103 | do_something_bpt.GetNumLocations() == 1, |
| 104 | VALID_BREAKPOINT) |
| 105 | |
| 106 | first_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_first_call_line) |
| 107 | self.assertTrue(first_call_bpt.IsValid() and |
| 108 | first_call_bpt.GetNumLocations() == 1, |
| 109 | VALID_BREAKPOINT) |
| 110 | |
| 111 | second_call_bpt = target.BreakpointCreateByLocation('pass-to-base.cpp', self.main_second_call_line) |
| 112 | self.assertTrue(second_call_bpt.IsValid() and |
| 113 | second_call_bpt.GetNumLocations() == 1, |
| 114 | VALID_BREAKPOINT) |
| 115 | |
| 116 | # Now launch the process, and do not stop at the entry point. |
| 117 | |
| 118 | error = lldb.SBError() |
| 119 | self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error) |
| 120 | |
| 121 | self.assertTrue(self.process.GetState() == lldb.eStateStopped, |
| 122 | PROCESS_STOPPED) |
| 123 | |
| 124 | threads = lldbutil.GetThreadsStoppedAtBreakpoint (self.process, first_call_bpt) |
| 125 | self.assertTrue (len(threads) == 1) |
| 126 | thread = threads[0] |
| 127 | |
| 128 | frame = thread.GetFrameAtIndex(0) |
| 129 | |
| 130 | # Now find the dynamic addresses of myB and otherB so we can compare them |
| 131 | # with the dynamic values we get in doSomething: |
| 132 | |
| 133 | noDynamic = False |
| 134 | useDynamic = True |
| 135 | |
| 136 | myB = frame.FindVariable ('myB', noDynamic); |
| 137 | self.assertTrue (myB.IsValid()) |
| 138 | myB_loc = int (myB.GetLocation(), 16) |
| 139 | |
| 140 | otherB = frame.FindVariable('otherB', noDynamic) |
| 141 | self.assertTrue (otherB.IsValid()) |
| 142 | otherB_loc = int (otherB.GetLocation(), 16) |
| 143 | |
| 144 | # Okay now run to doSomething: |
| 145 | |
| 146 | threads = lldbutil.ContinueToBreakpoint (self.process, do_something_bpt) |
| 147 | self.assertTrue (len(threads) == 1) |
| 148 | thread = threads[0] |
| 149 | |
| 150 | frame = thread.GetFrameAtIndex(0) |
| 151 | |
| 152 | # Get "this" using FindVariable: |
| 153 | |
| 154 | this_static = frame.FindVariable ('this', noDynamic) |
| 155 | this_dynamic = frame.FindVariable ('this', useDynamic) |
| 156 | self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 157 | |
| 158 | # Get "this" using FindValue, make sure that works too: |
| 159 | this_static = frame.FindValue ('this', lldb.eValueTypeVariableArgument, noDynamic) |
| 160 | this_dynamic = frame.FindValue ('this', lldb.eValueTypeVariableArgument, useDynamic) |
| 161 | self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 162 | |
| 163 | # Get "this" using the EvaluateExpression: |
| 164 | # These tests fail for now because EvaluateExpression doesn't currently support dynamic typing... |
| 165 | #this_static = frame.EvaluateExpression ('this', False) |
| 166 | #this_dynamic = frame.EvaluateExpression ('this', True) |
| 167 | #self.examine_value_object_of_this_ptr (this_static, this_dynamic, myB_loc) |
| 168 | |
| 169 | # The "frame var" code uses another path to get into children, so let's |
| 170 | # make sure that works as well: |
| 171 | |
| 172 | self.expect('frame var -d 1 anotherA.m_client_A._M_ptr', 'frame var finds its way into a child member', |
| 173 | patterns = ['\(.* B \*\)']) |
| 174 | |
| 175 | # Now make sure we also get it right for a reference as well: |
| 176 | |
| 177 | anotherA_static = frame.FindVariable ('anotherA', False) |
| 178 | self.assertTrue (anotherA_static.IsValid()) |
| 179 | anotherA_static_addr = int (anotherA_static.GetValue(), 16) |
| 180 | |
| 181 | anotherA_dynamic = frame.FindVariable ('anotherA', True) |
| 182 | self.assertTrue (anotherA_dynamic.IsValid()) |
| 183 | anotherA_dynamic_addr = int (anotherA_dynamic.GetValue(), 16) |
| 184 | anotherA_dynamic_typename = anotherA_dynamic.GetTypeName() |
| 185 | self.assertTrue (anotherA_dynamic_typename.find('B') != -1) |
| 186 | |
| 187 | self.assertTrue(anotherA_dynamic_addr < anotherA_static_addr) |
| 188 | |
| 189 | anotherA_m_b_value_dynamic = anotherA_dynamic.GetChildMemberWithName('m_b_value', True) |
| 190 | self.assertTrue (anotherA_m_b_value_dynamic.IsValid()) |
| 191 | anotherA_m_b_val = int (anotherA_m_b_value_dynamic.GetValue(), 10) |
| 192 | self.assertTrue (anotherA_m_b_val == 300) |
| 193 | |
| 194 | anotherA_m_b_value_static = anotherA_static.GetChildMemberWithName('m_b_value', True) |
| 195 | self.assertTrue (anotherA_m_b_value_static.IsValid() == False) |
| 196 | |
| 197 | # Okay, now continue again, and when we hit the second breakpoint in main |
| 198 | |
| 199 | threads = lldbutil.ContinueToBreakpoint (self.process, second_call_bpt) |
| 200 | self.assertTrue (len(threads) == 1) |
| 201 | thread = threads[0] |
| 202 | |
| 203 | frame = thread.GetFrameAtIndex(0) |
| 204 | reallyA_value = frame.FindVariable ('reallyA', False) |
| 205 | self.assertTrue(reallyA_value.IsValid()) |
| 206 | reallyA_loc = int (reallyA_value.GetLocation(), 16) |
| 207 | |
| 208 | # Finally continue to doSomething again, and make sure we get the right value for anotherA, |
| 209 | # which this time around is just an "A". |
| 210 | |
| 211 | threads = lldbutil.ContinueToBreakpoint (self.process, do_something_bpt) |
| 212 | self.assertTrue(len(threads) == 1) |
| 213 | thread = threads[0] |
| 214 | |
| 215 | frame = thread.GetFrameAtIndex(0) |
| 216 | anotherA_value = frame.FindVariable ('anotherA', True) |
| 217 | self.assertTrue(anotherA_value.IsValid()) |
| 218 | anotherA_loc = int (anotherA_value.GetValue(), 16) |
| 219 | self.assertTrue (anotherA_loc == reallyA_loc) |
| 220 | self.assertTrue (anotherA_value.GetTypeName().find ('B') == -1) |
| 221 | |
| 222 | if __name__ == '__main__': |
| 223 | import atexit |
| 224 | lldb.SBDebugger.Initialize() |
| 225 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 226 | unittest2.main() |