llvm.org GIT mirror llvm / a206940
Added the ability to dump hex bytes easily into a raw_ostream. Unit tests were added to verify this functionality keeps working correctly. Example output for raw hex bytes: llvm::ArrayRef<uint8_t> Bytes = ...; llvm::outs() << format_hex_bytes(Bytes); 554889e5 4881ec70 04000048 8d051002 00004c8d 05fd0100 004c8b0d d0020000 Example output for raw hex bytes with offsets: llvm::outs() << format_hex_bytes(Bytes, 0x100000d10); 0x0000000100000d10: 554889e5 4881ec70 04000048 8d051002 0x0000000100000d20: 00004c8d 05fd0100 004c8b0d d0020000 Example output for raw hex bytes with ASCII with offsets: llvm::outs() << format_hex_bytes_with_ascii(Bytes, 0x100000d10); 0x0000000100000d10: 554889e5 4881ec70 04000048 8d051002 |UH.?H.?p...H....| 0x0000000100000d20: 00004c8d 05fd0100 004c8b0d d0020000 |..L..?...L..?...| The default groups bytes into 4 byte groups, but this can be changed to 1 byte: llvm::outs() << format_hex_bytes(Bytes, 0x100000d10, 16 /*NumPerLine*/, 1 /*ByteGroupSize*/); 0x0000000100000d10: 55 48 89 e5 48 81 ec 70 04 00 00 48 8d 05 10 02 0x0000000100000d20: 00 00 4c 8d 05 fd 01 00 00 4c 8b 0d d0 02 00 00 llvm::outs() << format_hex_bytes(Bytes, 0x100000d10, 16 /*NumPerLine*/, 2 /*ByteGroupSize*/); 0x0000000100000d10: 5548 89e5 4881 ec70 0400 0048 8d05 1002 0x0000000100000d20: 0000 4c8d 05fd 0100 004c 8b0d d002 0000 llvm::outs() << format_hex_bytes(Bytes, 0x100000d10, 8 /*NumPerLine*/, 1 /*ByteGroupSize*/); 0x0000000100000d10: 55 48 89 e5 48 81 ec 70 0x0000000100000d18: 04 00 00 48 8d 05 10 02 0x0000000100000d20: 00 00 4c 8d 05 fd 01 00 0x0000000100000d28: 00 4c 8b 0d d0 02 00 00 https://reviews.llvm.org/D26405 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286316 91177308-0d34-0410-b5e6-96231b3b80d8 Greg Clayton 3 years ago
4 changed file(s) with 247 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
2222 #ifndef LLVM_SUPPORT_FORMAT_H
2323 #define LLVM_SUPPORT_FORMAT_H
2424
25 #include "llvm/ADT/ArrayRef.h"
2526 #include "llvm/ADT/STLExtras.h"
2627 #include "llvm/ADT/StringRef.h"
2728 #include "llvm/Support/DataTypes.h"
201202 return FormattedNumber(0, N, Width, false, false, false);
202203 }
203204
205 class FormattedHexBytes {
206 llvm::ArrayRef Bytes;
207 // Display offsets for each line if FirstByteOffset has a value.
208 llvm::Optional FirstByteOffset;
209 uint32_t NumPerLine; // Number of bytes to show per line.
210 uint8_t ByteGroupSize; // How many hex bytes are grouped without spaces
211 bool Upper; // Show offset and hex bytes as upper case.
212 bool ASCII; // Show the ASCII bytes for the hex bytes to the right.
213 friend class raw_ostream;
214
215 public:
216 FormattedHexBytes(llvm::ArrayRef B, llvm::Optional O,
217 uint32_t NPL, uint8_t BGS, bool U, bool A)
218 : Bytes(B), FirstByteOffset(O), NumPerLine(NPL), ByteGroupSize(BGS),
219 Upper(U), ASCII(A) {}
220 };
221
222 inline FormattedHexBytes format_hex_bytes(
223 llvm::ArrayRef Bytes,
224 llvm::Optional FirstByteOffset = llvm::Optional(),
225 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) {
226 return FormattedHexBytes(Bytes, FirstByteOffset, NumPerLine, ByteGroupSize,
227 false /*Upper*/, false /*ASCII*/);
228 }
229
230 inline FormattedHexBytes format_hex_bytes_with_ascii(
231 llvm::ArrayRef Bytes,
232 llvm::Optional FirstByteOffset = llvm::Optional(),
233 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) {
234 return FormattedHexBytes(Bytes, FirstByteOffset, NumPerLine, ByteGroupSize,
235 false /*Upper*/, true /*ASCII*/);
236 }
237
204238 } // end namespace llvm
205239
206240 #endif
2222 class format_object_base;
2323 class FormattedString;
2424 class FormattedNumber;
25 class FormattedHexBytes;
2526 template class SmallVectorImpl;
2627
2728 namespace sys {
220221
221222 // Formatted output, see the formatHex() function in Support/Format.h.
222223 raw_ostream &operator<<(const FormattedNumber &);
224
225 // Formatted output, see the formatHex() function in Support/Format.h.
226 raw_ostream &operator<<(const FormattedHexBytes &);
223227
224228 /// indent - Insert 'NumSpaces' spaces.
225229 raw_ostream &indent(unsigned NumSpaces);
351351 return *this;
352352 }
353353
354 raw_ostream &raw_ostream::operator<<(const FormattedHexBytes &FB) {
355 size_t LineIndex = 0;
356 const size_t Size = FB.Bytes.size();
357 HexPrintStyle OffsetStyle =
358 FB.Upper ? HexPrintStyle::PrefixUpper : HexPrintStyle::PrefixLower;
359 HexPrintStyle ByteStyle =
360 FB.Upper ? HexPrintStyle::Upper : HexPrintStyle::Lower;
361 while (LineIndex < Size) {
362 if (FB.FirstByteOffset.hasValue()) {
363 uint64_t Offset = FB.FirstByteOffset.getValue();
364 llvm::write_hex(*this, Offset + LineIndex, OffsetStyle,
365 sizeof(Offset) * 2 + 2);
366 *this << ": ";
367 }
368 // Print the hex bytes for this line
369 uint32_t I = 0;
370 for (I = 0; I < FB.NumPerLine; ++I) {
371 size_t Index = LineIndex + I;
372 if (Index >= Size)
373 break;
374 if (I && (I % FB.ByteGroupSize) == 0)
375 *this << " ";
376 llvm::write_hex(*this, FB.Bytes[Index], ByteStyle, 2);
377 }
378 uint32_t BytesDisplayed = I;
379 if (FB.ASCII) {
380 // Print any spaces needed for any bytes that we didn't print on this
381 // line so that the ASCII bytes are correctly aligned.
382 for (; I < FB.NumPerLine; ++I) {
383 if (I && (I % FB.ByteGroupSize) == 0)
384 indent(3);
385 else
386 indent(2);
387 }
388 *this << " |";
389 // Print the ASCII char values for each byte on this line
390 for (I = 0; I < FB.NumPerLine; ++I) {
391 size_t Index = LineIndex + I;
392 if (Index >= Size)
393 break;
394 char ch = (char)FB.Bytes[Index];
395 if (isprint(ch))
396 *this << ch;
397 else
398 *this << '.';
399 }
400 *this << '|';
401 }
402 LineIndex += BytesDisplayed;
403 if (LineIndex < Size)
404 *this << '\n';
405 }
406 return *this;
407 }
408
354409 /// indent - Insert 'NumSpaces' spaces.
355410 raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
356411 static const char Spaces[] = " "
180180 printToString(format_decimal(INT64_MIN, 21), 21));
181181 }
182182
183
184 }
183 std::string
184 format_hex_bytes(const void *P, size_t N,
185 llvm::Optional Offset = llvm::Optional(),
186 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) {
187 std::string S;
188 if (P && N) {
189 llvm::raw_string_ostream Str(S);
190 Str << llvm::format_hex_bytes(
191 llvm::ArrayRef(static_cast(P), N), Offset,
192 NumPerLine, ByteGroupSize);
193 Str.flush();
194 }
195 return S;
196 }
197
198 std::string format_hex_bytes_with_ascii(
199 const void *P, size_t N,
200 llvm::Optional Offset = llvm::Optional(),
201 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) {
202 std::string S;
203 if (P && N) {
204 llvm::raw_string_ostream Str(S);
205 Str << llvm::format_hex_bytes_with_ascii(
206 llvm::ArrayRef(static_cast(P), N), Offset,
207 NumPerLine, ByteGroupSize);
208 Str.flush();
209 }
210 return S;
211 }
212
213 TEST(raw_ostreamTest, FormattedHexBytes) {
214 char b[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
215 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
216 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
217 // Test raw bytes
218 const llvm::Optional InvalidOffset;
219 // Test invalid input.
220 EXPECT_EQ("", format_hex_bytes(nullptr, 0));
221 EXPECT_EQ("", format_hex_bytes(b, 0));
222 EXPECT_EQ("", format_hex_bytes(nullptr, 16));
223 //----------------------------------------------------------------------
224 // Test hex byte output with the default 4 byte groups
225 //----------------------------------------------------------------------
226 EXPECT_EQ("61", format_hex_bytes(b, 1));
227 EXPECT_EQ("61626364 65", format_hex_bytes(b, 5));
228 // Test that 16 bytes get written to a line correctly.
229 EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70", format_hex_bytes(b, 16));
230 // Test raw bytes with default 16 bytes per line wrapping.
231 EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70\n71", format_hex_bytes(b, 17));
232 // Test raw bytes with 1 bytes per line wrapping.
233 EXPECT_EQ("61\n62\n63\n64\n65\n66", format_hex_bytes(b, 6, InvalidOffset, 1));
234 // Test raw bytes with 7 bytes per line wrapping.
235 EXPECT_EQ("61626364 656667\n68696a6b 6c6d6e\n6f7071",
236 format_hex_bytes(b, 17, InvalidOffset, 7));
237 // Test raw bytes with 8 bytes per line wrapping.
238 EXPECT_EQ("61626364 65666768\n696a6b6c 6d6e6f70\n71",
239 format_hex_bytes(b, 17, InvalidOffset, 8));
240 //----------------------------------------------------------------------
241 // Test hex byte output with the 1 byte groups
242 //----------------------------------------------------------------------
243 EXPECT_EQ("61 62 63 64 65", format_hex_bytes(b, 5, InvalidOffset, 16, 1));
244 // Test that 16 bytes get written to a line correctly.
245 EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70",
246 format_hex_bytes(b, 16, InvalidOffset, 16, 1));
247 // Test raw bytes with default 16 bytes per line wrapping.
248 EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70\n71",
249 format_hex_bytes(b, 17, InvalidOffset, 16, 1));
250 // Test raw bytes with 7 bytes per line wrapping.
251 EXPECT_EQ("61 62 63 64 65 66 67\n68 69 6a 6b 6c 6d 6e\n6f 70 71",
252 format_hex_bytes(b, 17, InvalidOffset, 7, 1));
253 // Test raw bytes with 8 bytes per line wrapping.
254 EXPECT_EQ("61 62 63 64 65 66 67 68\n69 6a 6b 6c 6d 6e 6f 70\n71",
255 format_hex_bytes(b, 17, InvalidOffset, 8, 1));
256
257 //----------------------------------------------------------------------
258 // Test hex byte output with the 2 byte groups
259 //----------------------------------------------------------------------
260 EXPECT_EQ("6162 6364 65", format_hex_bytes(b, 5, InvalidOffset, 16, 2));
261 // Test that 16 bytes get written to a line correctly.
262 EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70",
263 format_hex_bytes(b, 16, InvalidOffset, 16, 2));
264 // Test raw bytes with default 16 bytes per line wrapping.
265 EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70\n71",
266 format_hex_bytes(b, 17, InvalidOffset, 16, 2));
267 // Test raw bytes with 7 bytes per line wrapping.
268 EXPECT_EQ("6162 6364 6566 67\n6869 6a6b 6c6d 6e\n6f70 71",
269 format_hex_bytes(b, 17, InvalidOffset, 7, 2));
270 // Test raw bytes with 8 bytes per line wrapping.
271 EXPECT_EQ("6162 6364 6566 6768\n696a 6b6c 6d6e 6f70\n71",
272 format_hex_bytes(b, 17, InvalidOffset, 8, 2));
273
274 //----------------------------------------------------------------------
275 // Test hex bytes with offset with the default 4 byte groups.
276 //----------------------------------------------------------------------
277 EXPECT_EQ("0x0000000000000000: 61", format_hex_bytes(b, 1, 0x0));
278 EXPECT_EQ("0x0000000000001000: 61", format_hex_bytes(b, 1, 0x1000));
279 EXPECT_EQ("0x0000000000001000: 61\n0x0000000000001001: 62",
280 format_hex_bytes(b, 2, 0x1000, 1));
281 //----------------------------------------------------------------------
282 // Test hex bytes with ASCII with the default 4 byte groups.
283 //----------------------------------------------------------------------
284 EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70 |abcdefghijklmnop|",
285 format_hex_bytes_with_ascii(b, 16));
286 EXPECT_EQ("61626364 65666768 |abcdefgh|\n"
287 "696a6b6c 6d6e6f70 |ijklmnop|",
288 format_hex_bytes_with_ascii(b, 16, InvalidOffset, 8));
289 EXPECT_EQ("61626364 65666768 |abcdefgh|\n696a6b6c |ijkl|",
290 format_hex_bytes_with_ascii(b, 12, InvalidOffset, 8));
291 char unprintable[] = {'a', '\x1e', 'b', '\x1f'};
292 // Make sure the ASCII is still lined up correctly when fewer bytes than 16
293 // bytes per line are available. The ASCII should still be aligned as if 16
294 // bytes of hex might be displayed.
295 EXPECT_EQ("611e621f |a.b.|",
296 format_hex_bytes_with_ascii(unprintable, 4));
297 //----------------------------------------------------------------------
298 // Test hex bytes with ASCII with offsets with the default 4 byte groups.
299 //----------------------------------------------------------------------
300 EXPECT_EQ("0x0000000000000000: 61626364 65666768 "
301 "696a6b6c 6d6e6f70 |abcdefghijklmnop|",
302 format_hex_bytes_with_ascii(b, 16, 0));
303 EXPECT_EQ("0x0000000000000000: 61626364 65666768 |abcdefgh|\n"
304 "0x0000000000000008: 696a6b6c 6d6e6f70 |ijklmnop|",
305 format_hex_bytes_with_ascii(b, 16, 0, 8));
306 EXPECT_EQ("0x0000000000000000: 61626364 656667 |abcdefg|\n"
307 "0x0000000000000007: 68696a6b 6c |hijkl|",
308 format_hex_bytes_with_ascii(b, 12, 0, 7));
309
310 //----------------------------------------------------------------------
311 // Test hex bytes with ASCII with offsets with the default 2 byte groups.
312 //----------------------------------------------------------------------
313 EXPECT_EQ("0x0000000000000000: 6162 6364 6566 6768 "
314 "696a 6b6c 6d6e 6f70 |abcdefghijklmnop|",
315 format_hex_bytes_with_ascii(b, 16, 0, 16, 2));
316 EXPECT_EQ("0x0000000000000000: 6162 6364 6566 6768 |abcdefgh|\n"
317 "0x0000000000000008: 696a 6b6c 6d6e 6f70 |ijklmnop|",
318 format_hex_bytes_with_ascii(b, 16, 0, 8, 2));
319 EXPECT_EQ("0x0000000000000000: 6162 6364 6566 67 |abcdefg|\n"
320 "0x0000000000000007: 6869 6a6b 6c |hijkl|",
321 format_hex_bytes_with_ascii(b, 12, 0, 7, 2));
322
323 //----------------------------------------------------------------------
324 // Test hex bytes with ASCII with offsets with the default 1 byte groups.
325 //----------------------------------------------------------------------
326 EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 68 "
327 "69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|",
328 format_hex_bytes_with_ascii(b, 16, 0, 16, 1));
329 EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 68 |abcdefgh|\n"
330 "0x0000000000000008: 69 6a 6b 6c 6d 6e 6f 70 |ijklmnop|",
331 format_hex_bytes_with_ascii(b, 16, 0, 8, 1));
332 EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 |abcdefg|\n"
333 "0x0000000000000007: 68 69 6a 6b 6c |hijkl|",
334 format_hex_bytes_with_ascii(b, 12, 0, 7, 1));
335 }
336 }