1 //===-- DataDumpExtractorTest.cpp -----------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Core/DumpDataExtractor.h" 10 #include "lldb/Utility/DataBufferHeap.h" 11 #include "lldb/Utility/DataExtractor.h" 12 #include "lldb/Utility/Endian.h" 13 #include "lldb/Utility/StreamString.h" 14 #include "gtest/gtest.h" 15 #include <complex> 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 static void TestDumpWithAddress(uint64_t base_addr, size_t item_count, 21 llvm::StringRef expected) { 22 std::vector<uint8_t> data{0x11, 0x22}; 23 StreamString result; 24 DataBufferHeap dumpbuffer(&data[0], data.size()); 25 DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(), 26 endian::InlHostByteOrder(), /*addr_size=*/4); 27 28 DumpDataExtractor(extractor, &result, 0, lldb::Format::eFormatHex, 29 /*item_byte_size=*/1, item_count, 30 /*num_per_line=*/1, base_addr, 0, 0); 31 ASSERT_EQ(expected, result.GetString()); 32 } 33 34 TEST(DumpDataExtractorTest, BaseAddress) { 35 TestDumpWithAddress(0x12341234, 1, "0x12341234: 0x11"); 36 TestDumpWithAddress(LLDB_INVALID_ADDRESS, 1, "0x11"); 37 TestDumpWithAddress(0x12341234, 2, "0x12341234: 0x11\n0x12341235: 0x22"); 38 TestDumpWithAddress(LLDB_INVALID_ADDRESS, 2, "0x11\n0x22"); 39 } 40 41 static void TestDumpWithOffset(offset_t start_offset, 42 llvm::StringRef expected) { 43 std::vector<uint8_t> data{0x11, 0x22, 0x33}; 44 StreamString result; 45 DataBufferHeap dumpbuffer(&data[0], data.size()); 46 DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(), 47 endian::InlHostByteOrder(), /*addr_size=*/4); 48 49 DumpDataExtractor(extractor, &result, start_offset, lldb::Format::eFormatHex, 50 /*item_byte_size=*/1, /*item_count=*/data.size(), 51 /*num_per_line=*/data.size(), /*base_addr=*/0, 0, 0); 52 ASSERT_EQ(expected, result.GetString()); 53 } 54 55 TEST(DumpDataExtractorTest, StartOffset) { 56 TestDumpWithOffset(0, "0x00000000: 0x11 0x22 0x33"); 57 // The offset applies to the DataExtractor, not the address used when 58 // formatting. 59 TestDumpWithOffset(1, "0x00000000: 0x22 0x33"); 60 // If the offset is outside the DataExtractor's range we do nothing. 61 TestDumpWithOffset(3, ""); 62 } 63 64 TEST(DumpDataExtractorTest, NullStream) { 65 // We don't do any work if there is no output stream. 66 uint8_t c = 0x11; 67 StreamString result; 68 DataBufferHeap dumpbuffer(&c, 0); 69 DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(), 70 endian::InlHostByteOrder(), /*addr_size=*/4); 71 72 DumpDataExtractor(extractor, nullptr, 0, lldb::Format::eFormatHex, 73 /*item_byte_size=*/1, /*item_count=*/1, 74 /*num_per_line=*/1, /*base_addr=*/0, 0, 0); 75 ASSERT_EQ("", result.GetString()); 76 } 77 78 static void TestDumpImpl(const void *data, size_t data_size, 79 size_t item_byte_size, size_t item_count, 80 size_t num_per_line, uint64_t base_addr, 81 lldb::Format format, llvm::StringRef expected) { 82 StreamString result; 83 DataBufferHeap dumpbuffer(data, data_size); 84 DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(), 85 endian::InlHostByteOrder(), 86 /*addr_size=*/4); 87 DumpDataExtractor(extractor, &result, 0, format, item_byte_size, item_count, 88 num_per_line, base_addr, 0, 0); 89 ASSERT_EQ(expected, result.GetString()); 90 } 91 92 template <typename T> 93 static void TestDump(T data, lldb::Format format, llvm::StringRef expected) { 94 TestDumpImpl(&data, sizeof(T), sizeof(T), 1, 1, LLDB_INVALID_ADDRESS, format, 95 expected); 96 } 97 98 static void TestDump(llvm::StringRef str, lldb::Format format, 99 llvm::StringRef expected) { 100 TestDumpImpl(str.bytes_begin(), 101 // +1 to include the NULL char as the last byte 102 str.size() + 1, str.size() + 1, 1, 1, LLDB_INVALID_ADDRESS, 103 format, expected); 104 } 105 106 template <typename T> 107 static void TestDump(const std::vector<T> data, lldb::Format format, 108 llvm::StringRef expected) { 109 size_t sz_bytes = data.size() * sizeof(T); 110 TestDumpImpl(&data[0], sz_bytes, sz_bytes, data.size(), 1, 111 LLDB_INVALID_ADDRESS, format, expected); 112 } 113 114 TEST(DumpDataExtractorTest, Formats) { 115 TestDump<uint8_t>(1, lldb::eFormatDefault, "0x01"); 116 TestDump<uint8_t>(1, lldb::eFormatBoolean, "true"); 117 TestDump<uint8_t>(0xAA, lldb::eFormatBinary, "0b10101010"); 118 TestDump<uint8_t>(1, lldb::eFormatBytes, "01"); 119 TestDump<uint8_t>(1, lldb::eFormatBytesWithASCII, "01 ."); 120 TestDump('?', lldb::eFormatChar, "'?'"); 121 TestDump('\x1A', lldb::eFormatCharPrintable, "."); 122 TestDump('#', lldb::eFormatCharPrintable, "#"); 123 TestDump(std::complex<float>(1.2, 3.4), lldb::eFormatComplex, "1.2 + 3.4i"); 124 TestDump(std::complex<double>(4.5, 6.7), lldb::eFormatComplex, "4.5 + 6.7i"); 125 126 // long double is not tested here because for some platforms we treat it as 10 127 // bytes when the compiler allocates 16 bytes of space for it. (see 128 // DataExtractor::GetLongDouble) Meaning that when we extract the second one, 129 // it gets the wrong value (it's 6 bytes off). You could manually construct a 130 // set of bytes to match the 10 byte format but then if the test runs on a 131 // machine where we don't use 10 it'll break. 132 133 TestDump(llvm::StringRef("aardvark"), lldb::Format::eFormatCString, 134 "\"aardvark\""); 135 TestDump<uint16_t>(99, lldb::Format::eFormatDecimal, "99"); 136 // Just prints as a signed integer. 137 TestDump(-1, lldb::Format::eFormatEnum, "-1"); 138 TestDump(0xcafef00d, lldb::Format::eFormatHex, "0xcafef00d"); 139 TestDump(0xcafef00d, lldb::Format::eFormatHexUppercase, "0xCAFEF00D"); 140 TestDump(0.456, lldb::Format::eFormatFloat, "0.456"); 141 TestDump(9, lldb::Format::eFormatOctal, "011"); 142 // Chars packed into an integer. 143 TestDump<uint32_t>(0x4C4C4442, lldb::Format::eFormatOSType, "'LLDB'"); 144 // Unicode8 doesn't have a specific formatter. 145 TestDump<uint8_t>(0x34, lldb::Format::eFormatUnicode8, "0x34"); 146 TestDump<uint16_t>(0x1122, lldb::Format::eFormatUnicode16, "U+1122"); 147 TestDump<uint32_t>(0x12345678, lldb::Format::eFormatUnicode32, 148 "U+0x12345678"); 149 TestDump<unsigned int>(654321, lldb::Format::eFormatUnsigned, "654321"); 150 // This pointer is printed based on the size of uint64_t, so the test is the 151 // same for 32/64 bit host. 152 TestDump<uint64_t>(0x4444555566667777, lldb::Format::eFormatPointer, 153 "0x4444555566667777"); 154 155 TestDump(std::vector<char>{'A', '\x01', 'C'}, 156 lldb::Format::eFormatVectorOfChar, "{A\\x01C}"); 157 TestDump(std::vector<int8_t>{0, -1, std::numeric_limits<int8_t>::max()}, 158 lldb::Format::eFormatVectorOfSInt8, "{0 -1 127}"); 159 TestDump(std::vector<uint8_t>{12, 0xFF, 34}, 160 lldb::Format::eFormatVectorOfUInt8, "{0x0c 0xff 0x22}"); 161 TestDump(std::vector<int16_t>{-1, 1234, std::numeric_limits<int16_t>::max()}, 162 lldb::Format::eFormatVectorOfSInt16, "{-1 1234 32767}"); 163 TestDump(std::vector<uint16_t>{0xffff, 0xabcd, 0x1234}, 164 lldb::Format::eFormatVectorOfUInt16, "{0xffff 0xabcd 0x1234}"); 165 TestDump(std::vector<int32_t>{0, -1, std::numeric_limits<int32_t>::max()}, 166 lldb::Format::eFormatVectorOfSInt32, "{0 -1 2147483647}"); 167 TestDump(std::vector<uint32_t>{0, 0xffffffff, 0x1234abcd}, 168 lldb::Format::eFormatVectorOfUInt32, 169 "{0x00000000 0xffffffff 0x1234abcd}"); 170 TestDump(std::vector<int64_t>{0, -1, std::numeric_limits<int64_t>::max()}, 171 lldb::Format::eFormatVectorOfSInt64, "{0 -1 9223372036854775807}"); 172 TestDump(std::vector<uint64_t>{0, 0xaaaabbbbccccdddd}, 173 lldb::Format::eFormatVectorOfUInt64, 174 "{0x0000000000000000 0xaaaabbbbccccdddd}"); 175 176 // See half2float for format details. 177 // Test zeroes. 178 TestDump(std::vector<uint16_t>{0x0000, 0x8000}, 179 lldb::Format::eFormatVectorOfFloat16, "{0 -0}"); 180 // Some subnormal numbers. 181 TestDump(std::vector<uint16_t>{0x0001, 0x8001}, 182 lldb::Format::eFormatVectorOfFloat16, "{5.96046e-08 -5.96046e-08}"); 183 // A full mantisse and empty expontent. 184 TestDump(std::vector<uint16_t>{0x83ff, 0x03ff}, 185 lldb::Format::eFormatVectorOfFloat16, "{-6.09756e-05 6.09756e-05}"); 186 // Some normal numbers. 187 TestDump(std::vector<uint16_t>{0b0100001001001000}, 188 lldb::Format::eFormatVectorOfFloat16, "{3.14062}"); 189 TestDump(std::vector<uint16_t>{0xabcd, 0x1234}, 190 lldb::Format::eFormatVectorOfFloat16, "{-0.0609436 0.000757217}"); 191 // Largest and smallest normal number. 192 TestDump(std::vector<uint16_t>{0x0400, 0x7bff}, 193 lldb::Format::eFormatVectorOfFloat16, "{6.10352e-05 65504}"); 194 // quiet/signaling NaNs. 195 TestDump(std::vector<uint16_t>{0xffff, 0xffc0, 0x7fff, 0x7fc0}, 196 lldb::Format::eFormatVectorOfFloat16, "{nan nan nan nan}"); 197 // +/-Inf. 198 TestDump(std::vector<uint16_t>{0xfc00, 0x7c00}, 199 lldb::Format::eFormatVectorOfFloat16, "{-inf inf}"); 200 201 TestDump(std::vector<float>{std::numeric_limits<float>::min(), 202 std::numeric_limits<float>::max()}, 203 lldb::Format::eFormatVectorOfFloat32, "{1.17549e-38 3.40282e+38}"); 204 TestDump(std::vector<double>{std::numeric_limits<double>::min(), 205 std::numeric_limits<double>::max()}, 206 lldb::Format::eFormatVectorOfFloat64, 207 "{2.2250738585072e-308 1.79769313486232e+308}"); 208 209 // Not sure we can rely on having uint128_t everywhere so emulate with 210 // uint64_t. 211 TestDump( 212 std::vector<uint64_t>{0x1, 0x1111222233334444, 0xaaaabbbbccccdddd, 0x0}, 213 lldb::Format::eFormatVectorOfUInt128, 214 "{0x11112222333344440000000000000001 " 215 "0x0000000000000000aaaabbbbccccdddd}"); 216 217 TestDump(std::vector<int>{2, 4}, lldb::Format::eFormatComplexInteger, 218 "2 + 4i"); 219 220 // Without an execution context this just prints the pointer on its own. 221 TestDump<uint32_t>(0x11223344, lldb::Format::eFormatAddressInfo, 222 "0x11223344"); 223 224 // Input not written in hex form because that requires C++17. 225 TestDump<float>(10, lldb::Format::eFormatHexFloat, "0x1.4p3"); 226 TestDump<double>(10, lldb::Format::eFormatHexFloat, "0x1.4p3"); 227 // long double not supported, see ItemByteSizeErrors. 228 229 // Can't disassemble without an execution context. 230 TestDump<uint32_t>(0xcafef00d, lldb::Format::eFormatInstruction, 231 "invalid target"); 232 233 // Has no special handling, intended for use elsewhere. 234 TestDump<int>(99, lldb::Format::eFormatVoid, "0x00000063"); 235 } 236 237 TEST(DumpDataExtractorTest, FormatCharArray) { 238 // Unlike the other formats, charArray isn't 1 array of N chars. 239 // It must be passed as N chars of 1 byte each. 240 // (eFormatVectorOfChar does this swap for you) 241 std::vector<char> data{'A', '\x01', '#'}; 242 StreamString result; 243 DataBufferHeap dumpbuffer(&data[0], data.size()); 244 DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(), 245 endian::InlHostByteOrder(), /*addr_size=*/4); 246 247 DumpDataExtractor(extractor, &result, 0, lldb::Format::eFormatCharArray, 248 /*item_byte_size=*/1, 249 /*item_count=*/data.size(), 250 /*num_per_line=*/data.size(), 0, 0, 0); 251 ASSERT_EQ("0x00000000: A\\x01#", result.GetString()); 252 253 result.Clear(); 254 DumpDataExtractor(extractor, &result, 0, lldb::Format::eFormatCharArray, 1, 255 data.size(), 1, 0, 0, 0); 256 // ASSERT macro thinks the split strings are multiple arguments so make a var. 257 const char *expected = "0x00000000: A\n" 258 "0x00000001: \\x01\n" 259 "0x00000002: #"; 260 ASSERT_EQ(expected, result.GetString()); 261 } 262 263 template <typename T> 264 void TestDumpMultiLine(std::vector<T> data, lldb::Format format, 265 size_t num_per_line, llvm::StringRef expected) { 266 size_t sz_bytes = data.size() * sizeof(T); 267 TestDumpImpl(&data[0], sz_bytes, data.size(), sz_bytes, num_per_line, 268 0x80000000, format, expected); 269 } 270 271 template <typename T> 272 void TestDumpMultiLine(const T *data, size_t num_items, lldb::Format format, 273 size_t num_per_line, llvm::StringRef expected) { 274 TestDumpImpl(data, sizeof(T) * num_items, sizeof(T), num_items, num_per_line, 275 0x80000000, format, expected); 276 } 277 278 TEST(DumpDataExtractorTest, MultiLine) { 279 // A vector counts as 1 item regardless of size. 280 TestDumpMultiLine(std::vector<uint8_t>{0x11}, 281 lldb::Format::eFormatVectorOfUInt8, 1, 282 "0x80000000: {0x11}"); 283 TestDumpMultiLine(std::vector<uint8_t>{0x11, 0x22}, 284 lldb::Format::eFormatVectorOfUInt8, 1, 285 "0x80000000: {0x11 0x22}"); 286 287 // If you have multiple vectors then that's multiple items. 288 // Here we say that these 2 bytes are actually 2 1 byte vectors. 289 const std::vector<uint8_t> vector_data{0x11, 0x22}; 290 TestDumpMultiLine(vector_data.data(), 2, lldb::Format::eFormatVectorOfUInt8, 291 1, "0x80000000: {0x11}\n0x80000001: {0x22}"); 292 293 // Single value formats can span multiple lines. 294 const std::vector<uint8_t> bytes{0x11, 0x22, 0x33}; 295 const char *expected_bytes_3_line = "0x80000000: 0x11\n" 296 "0x80000001: 0x22\n" 297 "0x80000002: 0x33"; 298 TestDumpMultiLine(bytes.data(), bytes.size(), lldb::Format::eFormatHex, 1, 299 expected_bytes_3_line); 300 301 // Lines may not have the full number of items. 302 TestDumpMultiLine(bytes.data(), bytes.size(), lldb::Format::eFormatHex, 4, 303 "0x80000000: 0x11 0x22 0x33"); 304 const char *expected_bytes_2_line = "0x80000000: 0x11 0x22\n" 305 "0x80000002: 0x33"; 306 TestDumpMultiLine(bytes.data(), bytes.size(), lldb::Format::eFormatHex, 2, 307 expected_bytes_2_line); 308 309 // The line address accounts for item sizes other than 1 byte. 310 const std::vector<uint16_t> shorts{0x1111, 0x2222, 0x3333}; 311 const char *expected_shorts_2_line = "0x80000000: 0x1111 0x2222\n" 312 "0x80000004: 0x3333"; 313 TestDumpMultiLine(shorts.data(), shorts.size(), lldb::Format::eFormatHex, 2, 314 expected_shorts_2_line); 315 316 // The ascii column is positioned using the maximum line length. 317 const std::vector<char> chars{'L', 'L', 'D', 'B'}; 318 const char *expected_chars_2_lines = "0x80000000: 4c 4c 44 LLD\n" 319 "0x80000003: 42 B"; 320 TestDumpMultiLine(chars.data(), chars.size(), 321 lldb::Format::eFormatBytesWithASCII, 3, 322 expected_chars_2_lines); 323 } 324 325 void TestDumpWithItemByteSize(size_t item_byte_size, lldb::Format format, 326 llvm::StringRef expected) { 327 // We won't be reading this data so anything will do. 328 uint8_t dummy = 0; 329 TestDumpImpl(&dummy, 1, item_byte_size, 1, 1, LLDB_INVALID_ADDRESS, format, 330 expected); 331 } 332 333 TEST(DumpDataExtractorTest, ItemByteSizeErrors) { 334 TestDumpWithItemByteSize( 335 16, lldb::Format::eFormatBoolean, 336 "error: unsupported byte size (16) for boolean format"); 337 TestDumpWithItemByteSize(21, lldb::Format::eFormatChar, 338 "error: unsupported byte size (21) for char format"); 339 TestDumpWithItemByteSize( 340 18, lldb::Format::eFormatComplexInteger, 341 "error: unsupported byte size (18) for complex integer format"); 342 343 // The code uses sizeof(long double) for these checks. This changes by host 344 // but we know it won't be >16. 345 TestDumpWithItemByteSize( 346 34, lldb::Format::eFormatComplex, 347 "error: unsupported byte size (34) for complex float format"); 348 TestDumpWithItemByteSize( 349 18, lldb::Format::eFormatFloat, 350 "error: unsupported byte size (18) for float format"); 351 352 // We want sizes to exactly match one of float/double. 353 TestDumpWithItemByteSize( 354 14, lldb::Format::eFormatComplex, 355 "error: unsupported byte size (14) for complex float format"); 356 TestDumpWithItemByteSize(3, lldb::Format::eFormatFloat, 357 "error: unsupported byte size (3) for float format"); 358 359 // We only allow float and double size. 360 TestDumpWithItemByteSize( 361 1, lldb::Format::eFormatHexFloat, 362 "error: unsupported byte size (1) for hex float format"); 363 TestDumpWithItemByteSize( 364 17, lldb::Format::eFormatHexFloat, 365 "error: unsupported byte size (17) for hex float format"); 366 } 367