Debian Codegraph @

llvm-project/lldb/unittests/Core/DumpDataExtractorTest.cpp

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