|
| 1 | +""" |
| 2 | +Tests the builtin formats of LLDB. |
| 3 | +""" |
| 4 | + |
| 5 | +import lldb |
| 6 | +from lldbsuite.test.decorators import * |
| 7 | +from lldbsuite.test.lldbtest import * |
| 8 | +from lldbsuite.test import lldbutil |
| 9 | + |
| 10 | +class TestCase(TestBase): |
| 11 | + |
| 12 | + mydir = TestBase.compute_mydir(__file__) |
| 13 | + |
| 14 | + def getFormatted(self, format, expr): |
| 15 | + """ |
| 16 | + Evaluates the expression and formats the result with the given format. |
| 17 | + """ |
| 18 | + result = lldb.SBCommandReturnObject() |
| 19 | + full_expr = "expr --format '" + format + "' -- " + expr |
| 20 | + self.dbg.GetCommandInterpreter().HandleCommand(full_expr, result) |
| 21 | + self.assertTrue(result.Succeeded(), result.GetError()) |
| 22 | + return result.GetOutput() |
| 23 | + |
| 24 | + @no_debug_info_test |
| 25 | + def test(self): |
| 26 | + self.build() |
| 27 | + lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.cpp")) |
| 28 | + |
| 29 | + # void |
| 30 | + self.assertEqual("", self.getFormatted("void", "1")) |
| 31 | + |
| 32 | + # boolean |
| 33 | + self.assertIn("= false\n", self.getFormatted("boolean", "0")) |
| 34 | + self.assertIn("= true\n", self.getFormatted("boolean", "1")) |
| 35 | + self.assertIn("= true\n", self.getFormatted("boolean", "2")) |
| 36 | + self.assertIn("= error: unsupported byte size (16) for boolean format\n", self.getFormatted("boolean", "(__uint128_t)0")) |
| 37 | + |
| 38 | + # float |
| 39 | + self.assertIn("= 0\n", self.getFormatted("float", "0")) |
| 40 | + self.assertIn("= 2\n", self.getFormatted("float", "0x40000000")) |
| 41 | + self.assertIn("= NaN\n", self.getFormatted("float", "-1")) |
| 42 | + |
| 43 | + # enumeration |
| 44 | + self.assertIn("= 0\n", self.getFormatted("enumeration", "0")) |
| 45 | + self.assertIn("= 1234567\n", self.getFormatted("enumeration", "1234567")) |
| 46 | + self.assertIn("= -1234567\n", self.getFormatted("enumeration", "-1234567")) |
| 47 | + |
| 48 | + # dec |
| 49 | + self.assertIn("= 1234567\n", self.getFormatted("dec", "1234567")) |
| 50 | + self.assertIn("= 123456789\n", self.getFormatted("dec", "(__uint128_t)123456789")) |
| 51 | + |
| 52 | + # unsigned decimal |
| 53 | + self.assertIn("= 1234567\n", self.getFormatted("unsigned decimal", "1234567")) |
| 54 | + self.assertIn("= 4293732729\n", self.getFormatted("unsigned decimal", "-1234567")) |
| 55 | + self.assertIn("= 123456789\n", self.getFormatted("unsigned decimal", "(__uint128_t)123456789")) |
| 56 | + |
| 57 | + # octal |
| 58 | + self.assertIn("= 04553207\n", self.getFormatted("octal", "1234567")) |
| 59 | + |
| 60 | + # hex |
| 61 | + self.assertIn("= 0x00abc123\n", self.getFormatted("hex", "0xABC123")) |
| 62 | + self.assertIn("= 0x000000000000000000123456789abdef\n", self.getFormatted("hex", "(__uint128_t)0x123456789ABDEFull")) |
| 63 | + |
| 64 | + # hex float |
| 65 | + self.assertIn("= 0x1p1\n", self.getFormatted("hex float", "2.0f")) |
| 66 | + self.assertIn("= 0x1p1\n", self.getFormatted("hex float", "2.0")) |
| 67 | + # FIXME: long double not supported. |
| 68 | + self.assertIn("= error: unsupported byte size (16) for hex float format\n", self.getFormatted("hex float", "2.0l")) |
| 69 | + |
| 70 | + # uppercase hex |
| 71 | + self.assertIn("= 0x00ABC123\n", self.getFormatted("uppercase hex", "0xABC123")) |
| 72 | + |
| 73 | + # binary |
| 74 | + self.assertIn("= 0b00000000000000000000000000000010\n", self.getFormatted("binary", "2")) |
| 75 | + self.assertIn("= 0b01100001\n", self.getFormatted("binary", "'a'")) |
| 76 | + self.assertIn(" = 0b10010001101000101011001111000\n", self.getFormatted("binary", "(__uint128_t)0x12345678ll")) |
| 77 | + |
| 78 | + # Different character arrays. |
| 79 | + self.assertIn('= " \\U0000001b\\a\\b\\f\\n\\r\\t\\vaA09"\n', self.getFormatted("character array", "cstring")) |
| 80 | + self.assertIn('= " \\U0000001b\\a\\b\\f\\n\\r\\t\\vaA09"\n', self.getFormatted("character", "cstring")) |
| 81 | + self.assertIn('= " \\U0000001b\\a\\b\\f\\n\\r\\t\\vaA09"\n', self.getFormatted("c-string", "cstring")) |
| 82 | + self.assertIn('= " \\U0000001b\\a\\b\\f\\n\\r\\t\\vaA09"\n', self.getFormatted("printable character", "cstring")) |
| 83 | + self.assertIn('= " \\U0000001b\\a\\b\\f\\n\\r\\t\\vaA09"\n', self.getFormatted("OSType", "cstring")) |
| 84 | + self.assertIn('= " \\U0000001b\\a\\b\\f\\n\\r\\t\\vaA09"\n', self.getFormatted("unicode8", "cstring")) |
| 85 | + |
| 86 | + self.assertIn('= \\0\\0\\0\\0\\0\\0\\0\\0\n', self.getFormatted("character array", "(__UINT64_TYPE__)0")) |
| 87 | + self.assertIn('= \\0\\0\\0\\0\\0\\0\\0\\0\n', self.getFormatted("character", "(__UINT64_TYPE__)0")) |
| 88 | + self.assertIn('=\n', self.getFormatted("c-string", "(__UINT64_TYPE__)0")) |
| 89 | + self.assertIn('= ........\n', self.getFormatted("printable character", "(__UINT64_TYPE__)0")) |
| 90 | + self.assertIn('= \'\\0\\0\\0\\0\\0\\0\\0\\0\'\n', self.getFormatted("OSType", "(__UINT64_TYPE__)0")) |
| 91 | + # FIXME: This seems wrong. |
| 92 | + self.assertIn('= 0x0000000000000000\n', self.getFormatted("unicode8", "(__UINT64_TYPE__)0")) |
| 93 | + |
| 94 | + self.assertIn('= xV4\\x12\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\n', self.getFormatted("character array", "(__uint128_t)0x12345678ll")) |
| 95 | + self.assertIn('= xV4\\x12\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\n', self.getFormatted("character", "(__uint128_t)0x12345678ll")) |
| 96 | + self.assertIn('=\n', self.getFormatted("c-string", "(__uint128_t)0x12345678ll")) |
| 97 | + self.assertIn('= xV4.............\n', self.getFormatted("printable character", "(__uint128_t)0x12345678ll")) |
| 98 | + self.assertIn('= 0x00000000000000000000000012345678\n', self.getFormatted("OSType", "(__uint128_t)0x12345678ll")) |
| 99 | + # FIXME: This seems wrong. |
| 100 | + self.assertIn('= 0x00000000000000000000000012345678\n', self.getFormatted("unicode8", "(__uint128_t)0x12345678ll")) |
| 101 | + |
| 102 | + # bytes |
| 103 | + self.assertIn('= " \\U0000001b\\a\\b\\f\\n\\r\\t\\vaA09"\n', self.getFormatted("bytes", "cstring")) |
| 104 | + |
| 105 | + # bytes with ASCII |
| 106 | + self.assertIn('= " \\U0000001b\\a\\b\\f\\n\\r\\t\\vaA09"\n', self.getFormatted("bytes with ASCII", "cstring")) |
| 107 | + |
| 108 | + # unicode16 |
| 109 | + self.assertIn('= U+5678 U+1234\n', self.getFormatted("unicode16", "0x12345678")) |
| 110 | + |
| 111 | + # unicode32 |
| 112 | + self.assertIn('= U+0x89abcdef U+0x01234567\n', self.getFormatted("unicode32", "(__UINT64_TYPE__)0x123456789ABCDEFll")) |
| 113 | + |
| 114 | + # address |
| 115 | + self.assertIn("= 0x00000012\n", self.getFormatted("address", "0x12")) |
| 116 | + self.assertIn("= 0x00000000\n", self.getFormatted("address", "0")) |
| 117 | + |
| 118 | + # Different fixed-width integer type arrays (e.g. 'uint8_t[]'). |
| 119 | + self.assertIn("= {0xf8 0x56 0x34 0x12}\n", self.getFormatted("uint8_t[]", "0x123456f8")) |
| 120 | + self.assertIn("= {-8 86 52 18}\n", self.getFormatted("int8_t[]", "0x123456f8")) |
| 121 | + |
| 122 | + self.assertIn("= {0x56f8 0x1234}\n", self.getFormatted("uint16_t[]", "0x123456f8")) |
| 123 | + self.assertIn("= {-2312 4660}\n", self.getFormatted("int16_t[]", "0x1234F6f8")) |
| 124 | + |
| 125 | + self.assertIn("= {0x89abcdef 0x01234567}\n", self.getFormatted("uint32_t[]", "(__UINT64_TYPE__)0x123456789ABCDEFll")) |
| 126 | + self.assertIn("= {-1985229329 19088743}\n", self.getFormatted("int32_t[]", "(__UINT64_TYPE__)0x123456789ABCDEFll")) |
| 127 | + |
| 128 | + self.assertIn("= {0x89abcdef 0x01234567 0x00000000 0x00000000}\n", self.getFormatted("uint32_t[]", "__uint128_t i = 0x123456789ABCDEF; i")) |
| 129 | + self.assertIn("= {-1985229329 19088743 0 0}\n", self.getFormatted("int32_t[]", "__uint128_t i = 0x123456789ABCDEF; i")) |
| 130 | + |
| 131 | + self.assertIn("= {0x0123456789abcdef 0x0000000000000000}\n", self.getFormatted("uint64_t[]", "__uint128_t i = 0x123456789ABCDEF; i")) |
| 132 | + self.assertIn("= {-994074541749903617 0}\n", self.getFormatted("int64_t[]", "__uint128_t i = 0xF23456789ABCDEFFll; i")) |
| 133 | + |
| 134 | + # There is not int128_t[] style, so this only tests uint128_t[]. |
| 135 | + self.assertIn("= {0x00000000000000000123456789abcdef}\n", self.getFormatted("uint128_t[]", "__uint128_t i = 0x123456789ABCDEF; i")) |
| 136 | + |
| 137 | + # Invalid format string |
| 138 | + self.expect("expr --format invalid_format_string -- 1", error=True, |
| 139 | + substrs=["error: Invalid format character or name 'invalid_format_string'. Valid values are:"]) |
| 140 | + |
| 141 | + # Extends to host target pointer width. |
| 142 | + @skipIf(archs=no_match(['x86_64'])) |
| 143 | + @no_debug_info_test |
| 144 | + def test_instruction(self): |
| 145 | + # pointer |
| 146 | + self.assertIn("= 0x000000000012d687\n", self.getFormatted("pointer", "1234567")) |
| 147 | + self.assertIn("= 0x0000000000000000\n", self.getFormatted("pointer", "0")) |
| 148 | + |
| 149 | + # Depends on the host target for decoding. |
| 150 | + @skipIf(archs=no_match(['x86_64'])) |
| 151 | + @no_debug_info_test |
| 152 | + def test_instruction(self): |
| 153 | + self.assertIn(" addq 0xa(%rdi), %r8\n", self.getFormatted("instruction", "0x0a47034c")) |
0 commit comments