1 """ 2 Test using LLDB data formatters with frozen objects coming from the expression parser. 3 """ 4 5 import unittest2 6 import lldb 7 import lldbutil 8 from lldbtest import * 9 10 class ExprFormattersTestCase(TestBase): 11 12 mydir = os.path.join("expression_command", "formatters") 13 14 def setUp(self): 15 # Call super's setUp(). 16 TestBase.setUp(self) 17 # Find the line number to break for main.cpp. 18 self.line = line_number('main.cpp', 19 '// Stop here') 20 21 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 22 @dsym_test 23 def test_with_dsym(self): 24 """Test expr + formatters for good interoperability.""" 25 self.buildDsym() 26 self.do_my_test() 27 28 @expectedFailureFreeBSD('llvm.org/pr16697') # Expression fails with 'there is no JIT compiled function' 29 @dwarf_test 30 def test_with_dwarf(self): 31 """Test expr + formatters for good interoperability.""" 32 self.buildDwarf() 33 self.do_my_test() 34 35 def do_my_test(self): 36 37 # This is the function to remove the custom formats in order to have a 38 # clean slate for the next test case. 39 def cleanup(): 40 self.runCmd('type summary clear', check=False) 41 self.runCmd('type synthetic clear', check=False) 42 43 # Execute the cleanup function during test case tear down. 44 self.addTearDownHook(cleanup) 45 46 """Test expr + formatters for good interoperability.""" 47 self.runCmd("file a.out", CURRENT_EXECUTABLE_SET) 48 49 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, loc_exact=True) 50 51 self.runCmd("run", RUN_SUCCEEDED) 52 self.runCmd("script import formatters") 53 self.runCmd("script import foosynth") 54 55 self.runCmd("frame variable foo1 --show-types") 56 self.runCmd("frame variable foo1.b --show-types") 57 self.runCmd("frame variable foo1.b.b_ref --show-types") 58 59 self.expect("expression --show-types -- *(new foo(47))", 60 substrs = ['(int) a = 47', '(bar) b = {', '(int) i = 94', '(baz) b = {', '(int) k = 99']) 61 62 self.runCmd("type summary add -F formatters.foo_SummaryProvider foo") 63 64 self.expect("expression new int(12)", 65 substrs = ['(int *) $', ' = 0x']) 66 67 self.runCmd("type summary add -s \"${var%pointer} -> ${*var%decimal}\" \"int *\"") 68 69 self.expect("expression new int(12)", 70 substrs = ['(int *) $', '= 0x', ' -> 12']) 71 72 self.expect("expression foo1.a_ptr", 73 substrs = ['(int *) $', '= 0x', ' -> 13']) 74 75 self.expect("expression foo1", 76 substrs = ['(foo) $', ' = a = 12, a_ptr = ', ' -> 13, i = 24, i_ptr = ', ' -> 25']) 77 78 self.expect("expression new foo(47)", 79 substrs = ['(foo *) $', ' a = 47, a_ptr = ', ' -> 48, i = 94, i_ptr = ', ' -> 95']) 80 81 self.expect("expression foo2", 82 substrs = ['(foo) $', ' = a = 121, a_ptr = ', ' -> 122, i = 242, i_ptr = ', ' -> 243']) 83 84 object_name = self.res.GetOutput() 85 object_name = object_name[7:] 86 object_name = object_name[0:object_name.find(' =')] 87 88 self.expect("frame variable foo2", 89 substrs = ['(foo)', 'foo2', ' = a = 121, a_ptr = ', ' -> 122, i = 242, i_ptr = ', ' -> 243']) 90 91 self.expect("expression $" + object_name, 92 substrs = ['(foo) $', ' = a = 121, a_ptr = ', ' -> 122, i = 242, i_ptr = ', ' -> 243', ', h = 245 , k = 247']) 93 94 self.runCmd("type summary delete foo") 95 self.runCmd("type synthetic add --python-class foosynth.FooSyntheticProvider foo") 96 97 self.expect("expression --show-types -- $" + object_name, 98 substrs = ['(foo) $', ' = {', '(int) *i_ptr = 243']) 99 100 self.runCmd("n") 101 self.runCmd("n") 102 103 self.runCmd("type synthetic delete foo") 104 self.runCmd("type summary add -F formatters.foo_SummaryProvider foo") 105 106 self.expect("expression foo2", 107 substrs = ['(foo) $', ' = a = 7777, a_ptr = ', ' -> 122, i = 242, i_ptr = ', ' -> 8888']) 108 109 self.expect("expression $" + object_name + '.a', 110 substrs = ['7777']) 111 112 self.expect("expression *$" + object_name + '.b.i_ptr', 113 substrs = ['8888']) 114 115 self.expect("expression $" + object_name, 116 substrs = ['(foo) $', ' = a = 121, a_ptr = ', ' -> 122, i = 242, i_ptr = ', ' -> 8888', 'h = 245 , k = 247']) 117 118 self.runCmd("type summary delete foo") 119 self.runCmd("type synthetic add --python-class foosynth.FooSyntheticProvider foo") 120 121 self.expect("expression --show-types -- $" + object_name, 122 substrs = ['(foo) $', ' = {', '(int) *i_ptr = 8888']) 123 124 self.runCmd("n") 125 126 self.runCmd("type synthetic delete foo") 127 self.runCmd("type summary add -F formatters.foo_SummaryProvider foo") 128 129 self.expect("expression $" + object_name, 130 substrs = ['(foo) $', ' = a = 121, a_ptr = ', ' -> 122, i = 242, i_ptr = ', ' -> 8888', 'h = 9999 , k = 247']) 131 132 process = self.dbg.GetSelectedTarget().GetProcess() 133 thread = process.GetThreadAtIndex(0) 134 frame = thread.GetSelectedFrame() 135 136 frozen = frame.EvaluateExpression("$" + object_name + ".a_ptr") 137 138 a_data = frozen.GetPointeeData() 139 140 error = lldb.SBError() 141 self.assertTrue(a_data.GetUnsignedInt32(error, 0) == 122, '*a_ptr = 122') 142 143 self.runCmd("n");self.runCmd("n");self.runCmd("n"); 144 145 self.expect("frame variable numbers", 146 substrs = ['1','2','3','4','5']) 147 148 self.expect("expression numbers", 149 substrs = ['1','2','3','4','5']) 150 151 frozen = frame.EvaluateExpression("&numbers") 152 153 a_data = frozen.GetPointeeData(0, 1) 154 155 self.assertTrue(a_data.GetUnsignedInt32(error, 0) == 1, 'numbers[0] == 1') 156 self.assertTrue(a_data.GetUnsignedInt32(error, 4) == 2, 'numbers[1] == 2') 157 self.assertTrue(a_data.GetUnsignedInt32(error, 8) == 3, 'numbers[2] == 3') 158 self.assertTrue(a_data.GetUnsignedInt32(error, 12) == 4, 'numbers[3] == 4') 159 self.assertTrue(a_data.GetUnsignedInt32(error, 16) == 5, 'numbers[4] == 5') 160 161 frozen = frame.EvaluateExpression("numbers") 162 163 a_data = frozen.GetData() 164 165 self.assertTrue(a_data.GetUnsignedInt32(error, 0) == 1, 'numbers[0] == 1') 166 self.assertTrue(a_data.GetUnsignedInt32(error, 4) == 2, 'numbers[1] == 2') 167 self.assertTrue(a_data.GetUnsignedInt32(error, 8) == 3, 'numbers[2] == 3') 168 self.assertTrue(a_data.GetUnsignedInt32(error, 12) == 4, 'numbers[3] == 4') 169 self.assertTrue(a_data.GetUnsignedInt32(error, 16) == 5, 'numbers[4] == 5') 170 171 if __name__ == '__main__': 172 import atexit 173 lldb.SBDebugger.Initialize() 174 atexit.register(lambda: lldb.SBDebugger.Terminate()) 175 unittest2.main() 176