1 //===-- DumpDataExtractor.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/DumpDataExtractor.h"
12 #include "lldb/lldb-defines.h" // for LLDB_INVALID_ADDRESS
13 #include "lldb/lldb-forward.h" // for TargetSP, DisassemblerSP
15 #include "lldb/Core/Address.h" // for Address
16 #include "lldb/Core/Disassembler.h"
17 #include "lldb/Core/ModuleList.h" // for ModuleList
18 #include "lldb/Symbol/ClangASTContext.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Target/ExecutionContextScope.h"
21 #include "lldb/Target/SectionLoadList.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/DataExtractor.h"
24 #include "lldb/Utility/Stream.h"
26 #include "clang/AST/ASTContext.h" // for ASTContext
27 #include "clang/AST/CanonicalType.h" // for CanQualType
29 #include "llvm/ADT/APFloat.h" // for APFloat, APFloatBase:...
30 #include "llvm/ADT/APInt.h" // for APInt
31 #include "llvm/ADT/ArrayRef.h" // for ArrayRef
32 #include "llvm/ADT/SmallVector.h" // for SmallVector
34 #include <limits> // for numeric_limits, numer...
35 #include <memory> // for shared_ptr
36 #include <string> // for string, basic_string
38 #include <assert.h> // for assert
39 #include <ctype.h> // for isprint
40 #include <inttypes.h> // for PRIu64, PRIx64, PRIX64
41 #include <math.h> // for ldexpf
46 using namespace lldb_private;
49 #define NON_PRINTABLE_CHAR '.'
51 static float half2float(uint16_t half) {
56 int32_t v = (int16_t)half;
58 if (0 == (v & 0x7c00)) {
59 u.u = v & 0x80007FFFU;
60 return u.f * ldexpf(1, 125);
64 u.u = v | 0x70000000U;
65 return u.f * ldexpf(1, -112);
68 static bool GetAPInt(const DataExtractor &data, lldb::offset_t *offset_ptr,
69 lldb::offset_t byte_size, llvm::APInt &result) {
70 llvm::SmallVector<uint64_t, 2> uint64_array;
71 lldb::offset_t bytes_left = byte_size;
73 const lldb::ByteOrder byte_order = data.GetByteOrder();
74 if (byte_order == lldb::eByteOrderLittle) {
75 while (bytes_left > 0) {
76 if (bytes_left >= 8) {
77 u64 = data.GetU64(offset_ptr);
80 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
83 uint64_array.push_back(u64);
85 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
87 } else if (byte_order == lldb::eByteOrderBig) {
88 lldb::offset_t be_offset = *offset_ptr + byte_size;
89 lldb::offset_t temp_offset;
90 while (bytes_left > 0) {
91 if (bytes_left >= 8) {
93 temp_offset = be_offset;
94 u64 = data.GetU64(&temp_offset);
97 be_offset -= bytes_left;
98 temp_offset = be_offset;
99 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
102 uint64_array.push_back(u64);
104 *offset_ptr += byte_size;
105 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
111 static lldb::offset_t DumpAPInt(Stream *s, const DataExtractor &data,
112 lldb::offset_t offset, lldb::offset_t byte_size,
113 bool is_signed, unsigned radix) {
115 if (GetAPInt(data, &offset, byte_size, apint)) {
116 std::string apint_str(apint.toString(radix, is_signed));
127 s->Write(apint_str.c_str(), apint_str.size());
132 lldb::offset_t lldb_private::DumpDataExtractor(
133 const DataExtractor &DE, Stream *s, offset_t start_offset,
134 lldb::Format item_format, size_t item_byte_size, size_t item_count,
135 size_t num_per_line, uint64_t base_addr,
136 uint32_t item_bit_size, // If zero, this is not a bitfield value, if
137 // non-zero, the value is a bitfield
138 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the
139 // shift amount to apply to a bitfield
140 ExecutionContextScope *exe_scope) {
144 if (item_format == eFormatPointer) {
145 if (item_byte_size != 4 && item_byte_size != 8)
146 item_byte_size = s->GetAddressByteSize();
149 offset_t offset = start_offset;
151 if (item_format == eFormatInstruction) {
154 target_sp = exe_scope->CalculateTarget();
156 DisassemblerSP disassembler_sp(Disassembler::FindPlugin(
157 target_sp->GetArchitecture(),
158 target_sp->GetDisassemblyFlavor(), nullptr));
159 if (disassembler_sp) {
160 lldb::addr_t addr = base_addr + start_offset;
161 lldb_private::Address so_addr;
162 bool data_from_file = true;
163 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) {
164 data_from_file = false;
166 if (target_sp->GetSectionLoadList().IsEmpty() ||
167 !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
168 so_addr.SetRawAddress(addr);
171 size_t bytes_consumed = disassembler_sp->DecodeInstructions(
172 so_addr, DE, start_offset, item_count, false, data_from_file);
174 if (bytes_consumed) {
175 offset += bytes_consumed;
176 const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
177 const bool show_bytes = true;
178 ExecutionContext exe_ctx;
179 exe_scope->CalculateExecutionContext(exe_ctx);
180 disassembler_sp->GetInstructionList().Dump(s, show_address,
181 show_bytes, &exe_ctx);
185 s->Printf("invalid target");
190 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) &&
192 item_format = eFormatHex;
194 lldb::offset_t line_start_offset = start_offset;
195 for (uint32_t count = 0; DE.ValidOffset(offset) && count < item_count;
197 if ((count % num_per_line) == 0) {
199 if (item_format == eFormatBytesWithASCII &&
200 offset > line_start_offset) {
203 (num_per_line - (offset - line_start_offset)) * 3 + 2),
205 DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
206 offset - line_start_offset, SIZE_MAX,
207 LLDB_INVALID_ADDRESS, 0, 0);
211 if (base_addr != LLDB_INVALID_ADDRESS)
212 s->Printf("0x%8.8" PRIx64 ": ",
213 (uint64_t)(base_addr +
214 (offset - start_offset) / DE.getTargetByteSize()));
216 line_start_offset = offset;
217 } else if (item_format != eFormatChar &&
218 item_format != eFormatCharPrintable &&
219 item_format != eFormatCharArray && count > 0) {
223 switch (item_format) {
225 if (item_byte_size <= 8)
226 s->Printf("%s", DE.GetMaxU64Bitfield(&offset, item_byte_size,
227 item_bit_size, item_bit_offset)
231 s->Printf("error: unsupported byte size (%" PRIu64
232 ") for boolean format",
233 (uint64_t)item_byte_size);
239 if (item_byte_size <= 8) {
240 uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
241 item_bit_size, item_bit_offset);
242 // Avoid std::bitset<64>::to_string() since it is missing in
243 // earlier C++ libraries
244 std::string binary_value(64, '0');
245 std::bitset<64> bits(uval64);
246 for (uint32_t i = 0; i < 64; ++i)
248 binary_value[64 - 1 - i] = '1';
249 if (item_bit_size > 0)
250 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
251 else if (item_byte_size > 0 && item_byte_size <= 8)
252 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
254 const bool is_signed = false;
255 const unsigned radix = 2;
256 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
261 case eFormatBytesWithASCII:
262 for (uint32_t i = 0; i < item_byte_size; ++i) {
263 s->Printf("%2.2x", DE.GetU8(&offset));
266 // Put an extra space between the groups of bytes if more than one
267 // is being dumped in a group (item_byte_size is more than 1).
268 if (item_byte_size > 1)
273 case eFormatCharPrintable:
274 case eFormatCharArray: {
275 // Reject invalid item_byte_size.
276 if (item_byte_size > 8) {
277 s->Printf("error: unsupported byte size (%" PRIu64 ") for char format",
278 (uint64_t)item_byte_size);
282 // If we are only printing one character surround it with single
284 if (item_count == 1 && item_format == eFormatChar)
287 const uint64_t ch = DE.GetMaxU64Bitfield(&offset, item_byte_size,
288 item_bit_size, item_bit_offset);
290 s->Printf("%c", (char)ch);
291 else if (item_format != eFormatCharPrintable) {
321 if (item_byte_size == 1)
322 s->Printf("\\x%2.2x", (uint8_t)ch);
324 s->Printf("%" PRIu64, ch);
328 s->PutChar(NON_PRINTABLE_CHAR);
331 // If we are only printing one character surround it with single quotes
332 if (item_count == 1 && item_format == eFormatChar)
336 case eFormatEnum: // Print enum value as a signed integer when we don't get
339 if (item_byte_size <= 8)
340 s->Printf("%" PRId64,
341 DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
344 const bool is_signed = true;
345 const unsigned radix = 10;
346 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
350 case eFormatUnsigned:
351 if (item_byte_size <= 8)
352 s->Printf("%" PRIu64,
353 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
356 const bool is_signed = false;
357 const unsigned radix = 10;
358 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
363 if (item_byte_size <= 8)
364 s->Printf("0%" PRIo64,
365 DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
368 const bool is_signed = false;
369 const unsigned radix = 8;
370 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
374 case eFormatOSType: {
375 uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
376 item_bit_size, item_bit_offset);
378 for (uint32_t i = 0; i < item_byte_size; ++i) {
379 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
412 s->Printf("\\x%2.2x", ch);
420 case eFormatCString: {
421 const char *cstr = DE.GetCStr(&offset);
425 offset = LLDB_INVALID_OFFSET;
429 while (const char c = *cstr) {
459 s->Printf("\\x%2.2x", c);
472 s->Address(DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
477 case eFormatComplexInteger: {
478 size_t complex_int_byte_size = item_byte_size / 2;
480 if (complex_int_byte_size > 0 && complex_int_byte_size <= 8) {
481 s->Printf("%" PRIu64,
482 DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
483 s->Printf(" + %" PRIu64 "i",
484 DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
486 s->Printf("error: unsupported byte size (%" PRIu64
487 ") for complex integer format",
488 (uint64_t)item_byte_size);
494 if (sizeof(float) * 2 == item_byte_size) {
495 float f32_1 = DE.GetFloat(&offset);
496 float f32_2 = DE.GetFloat(&offset);
498 s->Printf("%g + %gi", f32_1, f32_2);
500 } else if (sizeof(double) * 2 == item_byte_size) {
501 double d64_1 = DE.GetDouble(&offset);
502 double d64_2 = DE.GetDouble(&offset);
504 s->Printf("%lg + %lgi", d64_1, d64_2);
506 } else if (sizeof(long double) * 2 == item_byte_size) {
507 long double ld64_1 = DE.GetLongDouble(&offset);
508 long double ld64_2 = DE.GetLongDouble(&offset);
509 s->Printf("%Lg + %Lgi", ld64_1, ld64_2);
512 s->Printf("error: unsupported byte size (%" PRIu64
513 ") for complex float format",
514 (uint64_t)item_byte_size);
522 case eFormatHexUppercase: {
523 bool wantsuppercase = (item_format == eFormatHexUppercase);
524 switch (item_byte_size) {
529 s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64,
530 (int)(2 * item_byte_size), (int)(2 * item_byte_size),
531 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
535 assert(item_bit_size == 0 && item_bit_offset == 0);
536 const uint8_t *bytes =
537 (const uint8_t *)DE.GetData(&offset, item_byte_size);
541 if (DE.GetByteOrder() == eByteOrderBig) {
542 for (idx = 0; idx < item_byte_size; ++idx)
543 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
545 for (idx = 0; idx < item_byte_size; ++idx)
546 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x",
547 bytes[item_byte_size - 1 - idx]);
556 bool used_apfloat = false;
558 target_sp = exe_scope->CalculateTarget();
560 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
562 clang::ASTContext *ast = clang_ast->getASTContext();
564 llvm::SmallVector<char, 256> sv;
565 // Show full precision when printing float values
566 const unsigned format_precision = 0;
567 const unsigned format_max_padding = 100;
568 size_t item_bit_size = item_byte_size * 8;
570 if (item_bit_size == ast->getTypeSize(ast->FloatTy)) {
571 llvm::APInt apint(item_bit_size,
572 DE.GetMaxU64(&offset, item_byte_size));
573 llvm::APFloat apfloat(ast->getFloatTypeSemantics(ast->FloatTy),
575 apfloat.toString(sv, format_precision, format_max_padding);
576 } else if (item_bit_size == ast->getTypeSize(ast->DoubleTy)) {
578 if (GetAPInt(DE, &offset, item_byte_size, apint)) {
579 llvm::APFloat apfloat(ast->getFloatTypeSemantics(ast->DoubleTy),
581 apfloat.toString(sv, format_precision, format_max_padding);
583 } else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy)) {
584 const auto &semantics =
585 ast->getFloatTypeSemantics(ast->LongDoubleTy);
586 const auto byte_size =
587 (llvm::APFloat::getSizeInBits(semantics) + 7) / 8;
590 if (GetAPInt(DE, &offset, byte_size, apint)) {
591 llvm::APFloat apfloat(semantics, apint);
592 apfloat.toString(sv, format_precision, format_max_padding);
594 } else if (item_bit_size == ast->getTypeSize(ast->HalfTy)) {
595 llvm::APInt apint(item_bit_size, DE.GetU16(&offset));
596 llvm::APFloat apfloat(ast->getFloatTypeSemantics(ast->HalfTy),
598 apfloat.toString(sv, format_precision, format_max_padding);
602 s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
610 std::ostringstream ss;
611 if (item_byte_size == sizeof(float) || item_byte_size == 2) {
613 if (item_byte_size == 2) {
614 uint16_t half = DE.GetU16(&offset);
615 f = half2float(half);
617 f = DE.GetFloat(&offset);
619 ss.precision(std::numeric_limits<float>::digits10);
621 } else if (item_byte_size == sizeof(double)) {
622 ss.precision(std::numeric_limits<double>::digits10);
623 ss << DE.GetDouble(&offset);
624 } else if (item_byte_size == sizeof(long double) ||
625 item_byte_size == 10) {
626 ss.precision(std::numeric_limits<long double>::digits10);
627 ss << DE.GetLongDouble(&offset);
629 s->Printf("error: unsupported byte size (%" PRIu64
630 ") for float format",
631 (uint64_t)item_byte_size);
635 s->Printf("%s", ss.str().c_str());
639 case eFormatUnicode16:
640 s->Printf("U+%4.4x", DE.GetU16(&offset));
643 case eFormatUnicode32:
644 s->Printf("U+0x%8.8x", DE.GetU32(&offset));
647 case eFormatAddressInfo: {
648 addr_t addr = DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
650 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size),
651 (int)(2 * item_byte_size), addr);
653 TargetSP target_sp(exe_scope->CalculateTarget());
654 lldb_private::Address so_addr;
656 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr,
659 so_addr.Dump(s, exe_scope, Address::DumpStyleResolvedDescription,
660 Address::DumpStyleModuleWithFileAddress);
662 so_addr.SetOffset(addr);
663 so_addr.Dump(s, exe_scope,
664 Address::DumpStyleResolvedPointerDescription);
670 case eFormatHexFloat:
671 if (sizeof(float) == item_byte_size) {
672 char float_cstr[256];
673 llvm::APFloat ap_float(DE.GetFloat(&offset));
674 ap_float.convertToHexString(float_cstr, 0, false,
675 llvm::APFloat::rmNearestTiesToEven);
676 s->Printf("%s", float_cstr);
678 } else if (sizeof(double) == item_byte_size) {
679 char float_cstr[256];
680 llvm::APFloat ap_float(DE.GetDouble(&offset));
681 ap_float.convertToHexString(float_cstr, 0, false,
682 llvm::APFloat::rmNearestTiesToEven);
683 s->Printf("%s", float_cstr);
686 s->Printf("error: unsupported byte size (%" PRIu64
687 ") for hex float format",
688 (uint64_t)item_byte_size);
693 // please keep the single-item formats below in sync with
694 // FormatManager::GetSingleItemFormat
695 // if you fail to do so, users will start getting different outputs
696 // depending on internal
697 // implementation details they should not care about ||
698 case eFormatVectorOfChar: // ||
699 s->PutChar('{'); // \/
701 DumpDataExtractor(DE, s, offset, eFormatCharArray, 1, item_byte_size,
702 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
706 case eFormatVectorOfSInt8:
709 DumpDataExtractor(DE, s, offset, eFormatDecimal, 1, item_byte_size,
710 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
714 case eFormatVectorOfUInt8:
716 offset = DumpDataExtractor(DE, s, offset, eFormatHex, 1, item_byte_size,
717 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
721 case eFormatVectorOfSInt16:
723 offset = DumpDataExtractor(
724 DE, s, offset, eFormatDecimal, sizeof(uint16_t),
725 item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t),
726 LLDB_INVALID_ADDRESS, 0, 0);
730 case eFormatVectorOfUInt16:
732 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint16_t),
733 item_byte_size / sizeof(uint16_t),
734 item_byte_size / sizeof(uint16_t),
735 LLDB_INVALID_ADDRESS, 0, 0);
739 case eFormatVectorOfSInt32:
741 offset = DumpDataExtractor(
742 DE, s, offset, eFormatDecimal, sizeof(uint32_t),
743 item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t),
744 LLDB_INVALID_ADDRESS, 0, 0);
748 case eFormatVectorOfUInt32:
750 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint32_t),
751 item_byte_size / sizeof(uint32_t),
752 item_byte_size / sizeof(uint32_t),
753 LLDB_INVALID_ADDRESS, 0, 0);
757 case eFormatVectorOfSInt64:
759 offset = DumpDataExtractor(
760 DE, s, offset, eFormatDecimal, sizeof(uint64_t),
761 item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t),
762 LLDB_INVALID_ADDRESS, 0, 0);
766 case eFormatVectorOfUInt64:
768 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint64_t),
769 item_byte_size / sizeof(uint64_t),
770 item_byte_size / sizeof(uint64_t),
771 LLDB_INVALID_ADDRESS, 0, 0);
775 case eFormatVectorOfFloat16:
778 DumpDataExtractor(DE, s, offset, eFormatFloat, 2, item_byte_size / 2,
779 item_byte_size / 2, LLDB_INVALID_ADDRESS, 0, 0);
783 case eFormatVectorOfFloat32:
786 DumpDataExtractor(DE, s, offset, eFormatFloat, 4, item_byte_size / 4,
787 item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
791 case eFormatVectorOfFloat64:
794 DumpDataExtractor(DE, s, offset, eFormatFloat, 8, item_byte_size / 8,
795 item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
799 case eFormatVectorOfUInt128:
802 DumpDataExtractor(DE, s, offset, eFormatHex, 16, item_byte_size / 16,
803 item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
809 if (item_format == eFormatBytesWithASCII && offset > line_start_offset) {
810 s->Printf("%*s", static_cast<int>(
811 (num_per_line - (offset - line_start_offset)) * 3 + 2),
813 DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
814 offset - line_start_offset, SIZE_MAX,
815 LLDB_INVALID_ADDRESS, 0, 0);
817 return offset; // Return the offset at which we ended up
820 void lldb_private::DumpHexBytes(Stream *s, const void *src, size_t src_len,
821 uint32_t bytes_per_line,
822 lldb::addr_t base_addr) {
823 DataExtractor data(src, src_len, lldb::eByteOrderLittle, 4);
824 DumpDataExtractor(data, s,
825 0, // Offset into "src"
826 lldb::eFormatBytes, // Dump as hex bytes
827 1, // Size of each item is 1 for single bytes
828 src_len, // Number of bytes
829 bytes_per_line, // Num bytes per line
830 base_addr, // Base address
831 0, 0); // Bitfield info