1""" 2Tests the builtin formats of LLDB. 3""" 4 5import lldb 6from lldbsuite.test.decorators import * 7from lldbsuite.test.lldbtest import * 8from lldbsuite.test import lldbutil 9 10class TestCase(TestBase): 11 12 def getFormatted(self, format, expr): 13 """ 14 Evaluates the expression and formats the result with the given format. 15 """ 16 result = lldb.SBCommandReturnObject() 17 full_expr = "expr --format '" + format + "' -- " + expr 18 self.dbg.GetCommandInterpreter().HandleCommand(full_expr, result) 19 self.assertTrue(result.Succeeded(), result.GetError()) 20 return result.GetOutput() 21 22 @no_debug_info_test 23 @skipIfWindows 24 # uint128_t not available on arm. 25 @skipIf(archs=['arm']) 26 def test(self): 27 self.build() 28 lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.cpp")) 29 30 # void 31 self.assertEqual("", self.getFormatted("void", "1")) 32 33 # boolean 34 self.assertIn("= false\n", self.getFormatted("boolean", "0")) 35 self.assertIn("= true\n", self.getFormatted("boolean", "1")) 36 self.assertIn("= true\n", self.getFormatted("boolean", "2")) 37 self.assertIn("= error: unsupported byte size (16) for boolean format\n", self.getFormatted("boolean", "(__uint128_t)0")) 38 39 # float 40 self.assertIn("= 0\n", self.getFormatted("float", "0")) 41 self.assertIn("= 2\n", self.getFormatted("float", "0x40000000")) 42 self.assertIn("= NaN\n", self.getFormatted("float", "-1")) 43 # Checks the float16 code. 44 self.assertIn("= 2\n", self.getFormatted("float", "(__UINT16_TYPE__)0x4000")) 45 self.assertIn("= error: unsupported byte size (1) for float format\n", self.getFormatted("float", "'a'")) 46 47 # enumeration 48 self.assertIn("= 0\n", self.getFormatted("enumeration", "0")) 49 self.assertIn("= 1234567\n", self.getFormatted("enumeration", "1234567")) 50 self.assertIn("= -1234567\n", self.getFormatted("enumeration", "-1234567")) 51 52 # dec 53 self.assertIn("= 1234567\n", self.getFormatted("dec", "1234567")) 54 self.assertIn("= 123456789\n", self.getFormatted("dec", "(__uint128_t)123456789")) 55 56 # unsigned decimal 57 self.assertIn("= 1234567\n", self.getFormatted("unsigned decimal", "1234567")) 58 self.assertIn("= 4293732729\n", self.getFormatted("unsigned decimal", "-1234567")) 59 self.assertIn("= 123456789\n", self.getFormatted("unsigned decimal", "(__uint128_t)123456789")) 60 61 # octal 62 self.assertIn("= 04553207\n", self.getFormatted("octal", "1234567")) 63 self.assertIn("= 0221505317046536757\n", self.getFormatted("octal", "(__uint128_t)0x123456789ABDEFull")) 64 65 # complex float 66 self.assertIn("= error: unsupported byte size (1) for complex float format\n", self.getFormatted("complex float", "'a'")) 67 68 # complex integer 69 self.assertIn("= error: unsupported byte size (1) for complex integer format\n", self.getFormatted("complex integer", "'a'")) 70 71 # hex 72 self.assertIn("= 0x00abc123\n", self.getFormatted("hex", "0xABC123")) 73 self.assertIn("= 0x000000000000000000123456789abdef\n", self.getFormatted("hex", "(__uint128_t)0x123456789ABDEFull")) 74 75 # hex float 76 self.assertIn("= 0x1p1\n", self.getFormatted("hex float", "2.0f")) 77 self.assertIn("= 0x1p1\n", self.getFormatted("hex float", "2.0")) 78 # FIXME: long double not supported. 79 self.assertIn("= error: unsupported byte size (16) for hex float format\n", self.getFormatted("hex float", "2.0l")) 80 81 # uppercase hex 82 self.assertIn("= 0x00ABC123\n", self.getFormatted("uppercase hex", "0xABC123")) 83 84 # binary 85 self.assertIn("= 0b00000000000000000000000000000010\n", self.getFormatted("binary", "2")) 86 self.assertIn("= 0b01100001\n", self.getFormatted("binary", "'a'")) 87 self.assertIn(" = 0b10010001101000101011001111000\n", self.getFormatted("binary", "(__uint128_t)0x12345678ll")) 88 89 # Different character arrays. 90 # FIXME: Passing a 'const char *' will ignore any given format, 91 self.assertIn(r'= " \U0000001b\a\b\f\n\r\t\vaA09"', self.getFormatted("character array", "cstring")) 92 self.assertIn(r'= " \U0000001b\a\b\f\n\r\t\vaA09"', self.getFormatted("c-string", "cstring")) 93 self.assertIn(' = " \\e\\a\\b\\f\\n\\r\\t\\vaA09" " \\U0000001b\\a\\b\\f\\n\\r\\t\\vaA09"\n', 94 self.getFormatted("c-string", "(char *)cstring")) 95 self.assertIn('=\n', self.getFormatted("c-string", "(__UINT64_TYPE__)0")) 96 97 # Build a uint128_t that contains a series of characters in each byte. 98 # First 8 byte of the uint128_t. 99 cstring_chars1 = " \a\b\f\n\r\t\v" 100 # Last 8 byte of the uint128_t. 101 cstring_chars2 = "AZaz09\033\0" 102 103 # Build a uint128_t value with the hex encoded characters. 104 string_expr = "((__uint128_t)0x" 105 for c in cstring_chars1: 106 string_expr += format(ord(c), "x").zfill(2) 107 string_expr += "ull << 64) | (__uint128_t)0x" 108 for c in cstring_chars2: 109 string_expr += format(ord(c), "x").zfill(2) 110 string_expr += "ull" 111 112 # Try to print that uint128_t with the different char formatters. 113 self.assertIn('= \\0\\e90zaZA\\v\\t\\r\\n\\f\\b\\a \n', self.getFormatted("character array", string_expr)) 114 self.assertIn('= \\0\\e90zaZA\\v\\t\\r\\n\\f\\b\\a \n', self.getFormatted("character", string_expr)) 115 self.assertIn('= ..90zaZA....... \n', self.getFormatted("printable character", string_expr)) 116 self.assertIn('= 0x00 0x1b 0x39 0x30 0x7a 0x61 0x5a 0x41 0x0b 0x09 0x0d 0x0a 0x0c 0x08 0x07 0x20\n', self.getFormatted("unicode8", string_expr)) 117 118 # OSType 119 ostype_expr = "(__UINT64_TYPE__)0x" 120 for c in cstring_chars1: 121 ostype_expr += format(ord(c), "x").zfill(2) 122 self.assertIn("= ' \\a\\b\\f\\n\\r\\t\\v'\n", self.getFormatted("OSType", ostype_expr)) 123 124 ostype_expr = "(__UINT64_TYPE__)0x" 125 for c in cstring_chars2: 126 ostype_expr += format(ord(c), "x").zfill(2) 127 self.assertIn("= 'AZaz09\\e\\0'\n", self.getFormatted("OSType", ostype_expr)) 128 129 self.assertIn('= 0x2007080c0a0d090b415a617a30391b00\n', self.getFormatted("OSType", string_expr)) 130 131 # bytes 132 self.assertIn(r'= " \U0000001b\a\b\f\n\r\t\vaA09"', self.getFormatted("bytes", "cstring")) 133 134 # bytes with ASCII 135 self.assertIn(r'= " \U0000001b\a\b\f\n\r\t\vaA09"', self.getFormatted("bytes with ASCII", "cstring")) 136 137 # unicode8 138 self.assertIn('= 0x78 0x56 0x34 0x12\n', self.getFormatted("unicode8", "0x12345678")) 139 140 # unicode16 141 self.assertIn('= U+5678 U+1234\n', self.getFormatted("unicode16", "0x12345678")) 142 143 # unicode32 144 self.assertIn('= U+0x89abcdef U+0x01234567\n', self.getFormatted("unicode32", "(__UINT64_TYPE__)0x123456789ABCDEFll")) 145 146 # address 147 self.assertIn("= 0x00000012\n", self.getFormatted("address", "0x12")) 148 self.assertIn("= 0x00000000\n", self.getFormatted("address", "0")) 149 150 # Different fixed-width integer type arrays (e.g. 'uint8_t[]'). 151 self.assertIn("= {0xf8 0x56 0x34 0x12}\n", self.getFormatted("uint8_t[]", "0x123456f8")) 152 self.assertIn("= {-8 86 52 18}\n", self.getFormatted("int8_t[]", "0x123456f8")) 153 154 self.assertIn("= {0x56f8 0x1234}\n", self.getFormatted("uint16_t[]", "0x123456f8")) 155 self.assertIn("= {-2312 4660}\n", self.getFormatted("int16_t[]", "0x1234F6f8")) 156 157 self.assertIn("= {0x89abcdef 0x01234567}\n", self.getFormatted("uint32_t[]", "(__UINT64_TYPE__)0x123456789ABCDEFll")) 158 self.assertIn("= {-1985229329 19088743}\n", self.getFormatted("int32_t[]", "(__UINT64_TYPE__)0x123456789ABCDEFll")) 159 160 self.assertIn("= {0x89abcdef 0x01234567 0x00000000 0x00000000}\n", self.getFormatted("uint32_t[]", "__uint128_t i = 0x123456789ABCDEF; i")) 161 self.assertIn("= {-1985229329 19088743 0 0}\n", self.getFormatted("int32_t[]", "__uint128_t i = 0x123456789ABCDEF; i")) 162 163 self.assertIn("= {0x0123456789abcdef 0x0000000000000000}\n", self.getFormatted("uint64_t[]", "__uint128_t i = 0x123456789ABCDEF; i")) 164 self.assertIn("= {-994074541749903617 0}\n", self.getFormatted("int64_t[]", "__uint128_t i = 0xF23456789ABCDEFFll; i")) 165 166 # There is not int128_t[] style, so this only tests uint128_t[]. 167 self.assertIn("= {0x00000000000000000123456789abcdef}\n", self.getFormatted("uint128_t[]", "__uint128_t i = 0x123456789ABCDEF; i")) 168 169 # Different fixed-width float type arrays. 170 self.assertIn("{2 2}\n", self.getFormatted("float16[]", "0x40004000")) 171 self.assertIn("{2 2}\n", self.getFormatted("float32[]", "0x4000000040000000ll")) 172 self.assertIn("{2 0}\n", self.getFormatted("float64[]", "__uint128_t i = 0x4000000000000000ll; i")) 173 174 # Invalid format string 175 self.expect("expr --format invalid_format_string -- 1", error=True, 176 substrs=["error: Invalid format character or name 'invalid_format_string'. Valid values are:"]) 177 178 # Extends to host target pointer width. 179 @skipIf(archs=no_match(['x86_64'])) 180 @no_debug_info_test 181 def test_pointer(self): 182 # pointer 183 self.assertIn("= 0x000000000012d687\n", self.getFormatted("pointer", "1234567")) 184 self.assertIn("= 0x0000000000000000\n", self.getFormatted("pointer", "0")) 185 # FIXME: Just ignores the input value as it's not pointer sized. 186 self.assertIn("= 0x0000000000000000\n", self.getFormatted("pointer", "'a'")) 187 188 # Depends on the host target for decoding. 189 @skipIf(archs=no_match(['x86_64'])) 190 @no_debug_info_test 191 def test_instruction(self): 192 self.assertIn(" addq 0xa(%rdi), %r8\n", self.getFormatted("instruction", "0x0a47034c")) 193