1 //===-- DataExtractor.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 //===----------------------------------------------------------------------===//
18 #include "clang/AST/ASTContext.h"
20 #include "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/APInt.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/MathExtras.h"
27 #include "lldb/Core/DataBufferHeap.h"
28 #include "lldb/Core/DataExtractor.h"
29 #include "lldb/Core/DataBuffer.h"
30 #include "lldb/Core/Disassembler.h"
31 #include "lldb/Core/Log.h"
32 #include "lldb/Core/Stream.h"
33 #include "lldb/Core/StreamString.h"
34 #include "lldb/Core/UUID.h"
35 #include "lldb/Core/dwarf.h"
36 #include "lldb/Host/Endian.h"
37 #include "lldb/Symbol/ClangASTContext.h"
38 #include "lldb/Target/ExecutionContext.h"
39 #include "lldb/Target/ExecutionContextScope.h"
40 #include "lldb/Target/SectionLoadList.h"
41 #include "lldb/Target/Target.h"
44 using namespace lldb_private;
46 static inline uint16_t
47 ReadInt16(const unsigned char* ptr, offset_t offset)
50 memcpy (&value, ptr + offset, 2);
54 static inline uint32_t
55 ReadInt32 (const unsigned char* ptr, offset_t offset)
58 memcpy (&value, ptr + offset, 4);
62 static inline uint64_t
63 ReadInt64(const unsigned char* ptr, offset_t offset)
66 memcpy (&value, ptr + offset, 8);
70 static inline uint16_t
71 ReadInt16(const void* ptr)
74 memcpy (&value, ptr, 2);
78 static inline uint32_t
79 ReadInt32 (const void* ptr)
82 memcpy (&value, ptr, 4);
86 static inline uint64_t
87 ReadInt64(const void* ptr)
90 memcpy (&value, ptr, 8);
94 static inline uint16_t
95 ReadSwapInt16(const unsigned char* ptr, offset_t offset)
98 memcpy (&value, ptr + offset, 2);
99 return llvm::ByteSwap_16(value);
102 static inline uint32_t
103 ReadSwapInt32 (const unsigned char* ptr, offset_t offset)
106 memcpy (&value, ptr + offset, 4);
107 return llvm::ByteSwap_32(value);
110 static inline uint64_t
111 ReadSwapInt64(const unsigned char* ptr, offset_t offset)
114 memcpy (&value, ptr + offset, 8);
115 return llvm::ByteSwap_64(value);
118 static inline uint16_t
119 ReadSwapInt16(const void* ptr)
122 memcpy (&value, ptr, 2);
123 return llvm::ByteSwap_16(value);
126 static inline uint32_t
127 ReadSwapInt32 (const void* ptr)
130 memcpy (&value, ptr, 4);
131 return llvm::ByteSwap_32(value);
134 static inline uint64_t
135 ReadSwapInt64(const void* ptr)
138 memcpy (&value, ptr, 8);
139 return llvm::ByteSwap_64(value);
142 #define NON_PRINTABLE_CHAR '.'
143 //----------------------------------------------------------------------
144 // Default constructor.
145 //----------------------------------------------------------------------
146 DataExtractor::DataExtractor () :
149 m_byte_order(lldb::endian::InlHostByteOrder()),
155 //----------------------------------------------------------------------
156 // This constructor allows us to use data that is owned by someone else.
157 // The data must stay around as long as this object is valid.
158 //----------------------------------------------------------------------
159 DataExtractor::DataExtractor (const void* data, offset_t length, ByteOrder endian, uint32_t addr_size) :
160 m_start ((uint8_t*)data),
161 m_end ((uint8_t*)data + length),
162 m_byte_order(endian),
163 m_addr_size (addr_size),
168 //----------------------------------------------------------------------
169 // Make a shared pointer reference to the shared data in "data_sp" and
170 // set the endian swapping setting to "swap", and the address size to
171 // "addr_size". The shared data reference will ensure the data lives
172 // as long as any DataExtractor objects exist that have a reference to
174 //----------------------------------------------------------------------
175 DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint32_t addr_size) :
178 m_byte_order(endian),
179 m_addr_size (addr_size),
185 //----------------------------------------------------------------------
186 // Initialize this object with a subset of the data bytes in "data".
187 // If "data" contains shared data, then a reference to this shared
188 // data will added and the shared data will stay around as long
189 // as any object contains a reference to that data. The endian
190 // swap and address size settings are copied from "data".
191 //----------------------------------------------------------------------
192 DataExtractor::DataExtractor (const DataExtractor& data, offset_t offset, offset_t length) :
195 m_byte_order(data.m_byte_order),
196 m_addr_size(data.m_addr_size),
199 if (data.ValidOffset(offset))
201 offset_t bytes_available = data.GetByteSize() - offset;
202 if (length > bytes_available)
203 length = bytes_available;
204 SetData(data, offset, length);
208 DataExtractor::DataExtractor (const DataExtractor& rhs) :
209 m_start (rhs.m_start),
211 m_byte_order (rhs.m_byte_order),
212 m_addr_size (rhs.m_addr_size),
213 m_data_sp (rhs.m_data_sp)
217 //----------------------------------------------------------------------
218 // Assignment operator
219 //----------------------------------------------------------------------
221 DataExtractor::operator= (const DataExtractor& rhs)
225 m_start = rhs.m_start;
227 m_byte_order = rhs.m_byte_order;
228 m_addr_size = rhs.m_addr_size;
229 m_data_sp = rhs.m_data_sp;
234 //----------------------------------------------------------------------
236 //----------------------------------------------------------------------
237 DataExtractor::~DataExtractor ()
241 //------------------------------------------------------------------
242 // Clears the object contents back to a default invalid state, and
243 // release any references to shared data that this object may
245 //------------------------------------------------------------------
247 DataExtractor::Clear ()
251 m_byte_order = lldb::endian::InlHostByteOrder();
256 //------------------------------------------------------------------
257 // If this object contains shared data, this function returns the
258 // offset into that shared data. Else zero is returned.
259 //------------------------------------------------------------------
261 DataExtractor::GetSharedDataOffset () const
265 const DataBuffer * data = m_data_sp.get();
268 const uint8_t * data_bytes = data->GetBytes();
269 if (data_bytes != NULL)
271 assert(m_start >= data_bytes);
272 return m_start - data_bytes;
279 //----------------------------------------------------------------------
280 // Set the data with which this object will extract from to data
281 // starting at BYTES and set the length of the data to LENGTH bytes
282 // long. The data is externally owned must be around at least as
283 // long as this object points to the data. No copy of the data is
284 // made, this object just refers to this data and can extract from
285 // it. If this object refers to any shared data upon entry, the
286 // reference to that data will be released. Is SWAP is set to true,
287 // any data extracted will be endian swapped.
288 //----------------------------------------------------------------------
290 DataExtractor::SetData (const void *bytes, offset_t length, ByteOrder endian)
292 m_byte_order = endian;
294 if (bytes == NULL || length == 0)
301 m_start = (uint8_t *)bytes;
302 m_end = m_start + length;
304 return GetByteSize();
307 //----------------------------------------------------------------------
308 // Assign the data for this object to be a subrange in "data"
309 // starting "data_offset" bytes into "data" and ending "data_length"
310 // bytes later. If "data_offset" is not a valid offset into "data",
311 // then this object will contain no bytes. If "data_offset" is
312 // within "data" yet "data_length" is too large, the length will be
313 // capped at the number of bytes remaining in "data". If "data"
314 // contains a shared pointer to other data, then a ref counted
315 // pointer to that data will be made in this object. If "data"
316 // doesn't contain a shared pointer to data, then the bytes referred
317 // to in "data" will need to exist at least as long as this object
318 // refers to those bytes. The address size and endian swap settings
319 // are copied from the current values in "data".
320 //----------------------------------------------------------------------
322 DataExtractor::SetData (const DataExtractor& data, offset_t data_offset, offset_t data_length)
324 m_addr_size = data.m_addr_size;
325 // If "data" contains shared pointer to data, then we can use that
326 if (data.m_data_sp.get())
328 m_byte_order = data.m_byte_order;
329 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length);
332 // We have a DataExtractor object that just has a pointer to bytes
333 if (data.ValidOffset(data_offset))
335 if (data_length > data.GetByteSize() - data_offset)
336 data_length = data.GetByteSize() - data_offset;
337 return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder());
342 //----------------------------------------------------------------------
343 // Assign the data for this object to be a subrange of the shared
344 // data in "data_sp" starting "data_offset" bytes into "data_sp"
345 // and ending "data_length" bytes later. If "data_offset" is not
346 // a valid offset into "data_sp", then this object will contain no
347 // bytes. If "data_offset" is within "data_sp" yet "data_length" is
348 // too large, the length will be capped at the number of bytes
349 // remaining in "data_sp". A ref counted pointer to the data in
350 // "data_sp" will be made in this object IF the number of bytes this
351 // object refers to in greater than zero (if at least one byte was
352 // available starting at "data_offset") to ensure the data stays
353 // around as long as it is needed. The address size and endian swap
354 // settings will remain unchanged from their current settings.
355 //----------------------------------------------------------------------
357 DataExtractor::SetData (const DataBufferSP& data_sp, offset_t data_offset, offset_t data_length)
359 m_start = m_end = NULL;
366 const size_t data_size = data_sp->GetByteSize();
367 if (data_offset < data_size)
369 m_start = data_sp->GetBytes() + data_offset;
370 const size_t bytes_left = data_size - data_offset;
371 // Cap the length of we asked for too many
372 if (data_length <= bytes_left)
373 m_end = m_start + data_length; // We got all the bytes we wanted
375 m_end = m_start + bytes_left; // Not all the bytes requested were available in the shared data
380 size_t new_size = GetByteSize();
382 // Don't hold a shared pointer to the data buffer if we don't share
383 // any valid bytes in the shared buffer.
390 //----------------------------------------------------------------------
391 // Extract a single unsigned char from the binary data and update
392 // the offset pointed to by "offset_ptr".
394 // RETURNS the byte that was extracted, or zero on failure.
395 //----------------------------------------------------------------------
397 DataExtractor::GetU8 (offset_t *offset_ptr) const
399 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, 1);
405 //----------------------------------------------------------------------
406 // Extract "count" unsigned chars from the binary data and update the
407 // offset pointed to by "offset_ptr". The extracted data is copied into
410 // RETURNS the non-NULL buffer pointer upon successful extraction of
411 // all the requested bytes, or NULL when the data is not available in
412 // the buffer due to being out of bounds, or unsufficient data.
413 //----------------------------------------------------------------------
415 DataExtractor::GetU8 (offset_t *offset_ptr, void *dst, uint32_t count) const
417 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, count);
420 // Copy the data into the buffer
421 memcpy (dst, data, count);
422 // Return a non-NULL pointer to the converted data as an indicator of success
428 //----------------------------------------------------------------------
429 // Extract a single uint16_t from the data and update the offset
430 // pointed to by "offset_ptr".
432 // RETURNS the uint16_t that was extracted, or zero on failure.
433 //----------------------------------------------------------------------
435 DataExtractor::GetU16 (offset_t *offset_ptr) const
438 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
441 if (m_byte_order != lldb::endian::InlHostByteOrder())
442 val = ReadSwapInt16(data);
444 val = ReadInt16 (data);
450 DataExtractor::GetU16_unchecked (offset_t *offset_ptr) const
453 if (m_byte_order == lldb::endian::InlHostByteOrder())
454 val = ReadInt16 (m_start, *offset_ptr);
456 val = ReadSwapInt16(m_start, *offset_ptr);
457 *offset_ptr += sizeof(val);
462 DataExtractor::GetU32_unchecked (offset_t *offset_ptr) const
465 if (m_byte_order == lldb::endian::InlHostByteOrder())
466 val = ReadInt32 (m_start, *offset_ptr);
468 val = ReadSwapInt32 (m_start, *offset_ptr);
469 *offset_ptr += sizeof(val);
474 DataExtractor::GetU64_unchecked (offset_t *offset_ptr) const
477 if (m_byte_order == lldb::endian::InlHostByteOrder())
478 val = ReadInt64 (m_start, *offset_ptr);
480 val = ReadSwapInt64 (m_start, *offset_ptr);
481 *offset_ptr += sizeof(val);
486 //----------------------------------------------------------------------
487 // Extract "count" uint16_t values from the binary data and update
488 // the offset pointed to by "offset_ptr". The extracted data is
489 // copied into "dst".
491 // RETURNS the non-NULL buffer pointer upon successful extraction of
492 // all the requested bytes, or NULL when the data is not available
493 // in the buffer due to being out of bounds, or unsufficient data.
494 //----------------------------------------------------------------------
496 DataExtractor::GetU16 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
498 const size_t src_size = sizeof(uint16_t) * count;
499 const uint16_t *src = (const uint16_t *)GetData (offset_ptr, src_size);
502 if (m_byte_order != lldb::endian::InlHostByteOrder())
504 uint16_t *dst_pos = (uint16_t *)void_dst;
505 uint16_t *dst_end = dst_pos + count;
506 const uint16_t *src_pos = src;
507 while (dst_pos < dst_end)
509 *dst_pos = ReadSwapInt16 (src_pos);
516 memcpy (void_dst, src, src_size);
518 // Return a non-NULL pointer to the converted data as an indicator of success
524 //----------------------------------------------------------------------
525 // Extract a single uint32_t from the data and update the offset
526 // pointed to by "offset_ptr".
528 // RETURNS the uint32_t that was extracted, or zero on failure.
529 //----------------------------------------------------------------------
531 DataExtractor::GetU32 (offset_t *offset_ptr) const
534 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
537 if (m_byte_order != lldb::endian::InlHostByteOrder())
539 val = ReadSwapInt32 (data);
543 memcpy (&val, data, 4);
549 //----------------------------------------------------------------------
550 // Extract "count" uint32_t values from the binary data and update
551 // the offset pointed to by "offset_ptr". The extracted data is
552 // copied into "dst".
554 // RETURNS the non-NULL buffer pointer upon successful extraction of
555 // all the requested bytes, or NULL when the data is not available
556 // in the buffer due to being out of bounds, or unsufficient data.
557 //----------------------------------------------------------------------
559 DataExtractor::GetU32 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
561 const size_t src_size = sizeof(uint32_t) * count;
562 const uint32_t *src = (const uint32_t *)GetData (offset_ptr, src_size);
565 if (m_byte_order != lldb::endian::InlHostByteOrder())
567 uint32_t *dst_pos = (uint32_t *)void_dst;
568 uint32_t *dst_end = dst_pos + count;
569 const uint32_t *src_pos = src;
570 while (dst_pos < dst_end)
572 *dst_pos = ReadSwapInt32 (src_pos);
579 memcpy (void_dst, src, src_size);
581 // Return a non-NULL pointer to the converted data as an indicator of success
587 //----------------------------------------------------------------------
588 // Extract a single uint64_t from the data and update the offset
589 // pointed to by "offset_ptr".
591 // RETURNS the uint64_t that was extracted, or zero on failure.
592 //----------------------------------------------------------------------
594 DataExtractor::GetU64 (offset_t *offset_ptr) const
597 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
600 if (m_byte_order != lldb::endian::InlHostByteOrder())
602 val = ReadSwapInt64 (data);
606 memcpy (&val, data, 8);
612 //----------------------------------------------------------------------
615 // Get multiple consecutive 64 bit values. Return true if the entire
616 // read succeeds and increment the offset pointed to by offset_ptr, else
617 // return false and leave the offset pointed to by offset_ptr unchanged.
618 //----------------------------------------------------------------------
620 DataExtractor::GetU64 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
622 const size_t src_size = sizeof(uint64_t) * count;
623 const uint64_t *src = (const uint64_t *)GetData (offset_ptr, src_size);
626 if (m_byte_order != lldb::endian::InlHostByteOrder())
628 uint64_t *dst_pos = (uint64_t *)void_dst;
629 uint64_t *dst_end = dst_pos + count;
630 const uint64_t *src_pos = src;
631 while (dst_pos < dst_end)
633 *dst_pos = ReadSwapInt64 (src_pos);
640 memcpy (void_dst, src, src_size);
642 // Return a non-NULL pointer to the converted data as an indicator of success
648 //----------------------------------------------------------------------
649 // Extract a single integer value from the data and update the offset
650 // pointed to by "offset_ptr". The size of the extracted integer
651 // is specified by the "byte_size" argument. "byte_size" should have
652 // a value between 1 and 4 since the return value is only 32 bits
653 // wide. Any "byte_size" values less than 1 or greater than 4 will
654 // result in nothing being extracted, and zero being returned.
656 // RETURNS the integer value that was extracted, or zero on failure.
657 //----------------------------------------------------------------------
659 DataExtractor::GetMaxU32 (offset_t *offset_ptr, size_t byte_size) const
663 case 1: return GetU8 (offset_ptr); break;
664 case 2: return GetU16(offset_ptr); break;
665 case 4: return GetU32(offset_ptr); break;
667 assert("GetMaxU32 unhandled case!" == NULL);
673 //----------------------------------------------------------------------
674 // Extract a single integer value from the data and update the offset
675 // pointed to by "offset_ptr". The size of the extracted integer
676 // is specified by the "byte_size" argument. "byte_size" should have
677 // a value >= 1 and <= 8 since the return value is only 64 bits
678 // wide. Any "byte_size" values less than 1 or greater than 8 will
679 // result in nothing being extracted, and zero being returned.
681 // RETURNS the integer value that was extracted, or zero on failure.
682 //----------------------------------------------------------------------
684 DataExtractor::GetMaxU64 (offset_t *offset_ptr, size_t size) const
688 case 1: return GetU8 (offset_ptr); break;
689 case 2: return GetU16(offset_ptr); break;
690 case 4: return GetU32(offset_ptr); break;
691 case 8: return GetU64(offset_ptr); break;
693 assert("GetMax64 unhandled case!" == NULL);
700 DataExtractor::GetMaxU64_unchecked (offset_t *offset_ptr, size_t size) const
704 case 1: return GetU8_unchecked (offset_ptr); break;
705 case 2: return GetU16_unchecked (offset_ptr); break;
706 case 4: return GetU32_unchecked (offset_ptr); break;
707 case 8: return GetU64_unchecked (offset_ptr); break;
709 assert("GetMax64 unhandled case!" == NULL);
716 DataExtractor::GetMaxS64 (offset_t *offset_ptr, size_t size) const
720 case 1: return (int8_t)GetU8 (offset_ptr); break;
721 case 2: return (int16_t)GetU16(offset_ptr); break;
722 case 4: return (int32_t)GetU32(offset_ptr); break;
723 case 8: return (int64_t)GetU64(offset_ptr); break;
725 assert("GetMax64 unhandled case!" == NULL);
732 DataExtractor::GetMaxU64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
734 uint64_t uval64 = GetMaxU64 (offset_ptr, size);
735 if (bitfield_bit_size > 0)
737 if (bitfield_bit_offset > 0)
738 uval64 >>= bitfield_bit_offset;
739 uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
740 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
742 uval64 &= bitfield_mask;
748 DataExtractor::GetMaxS64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
750 int64_t sval64 = GetMaxS64 (offset_ptr, size);
751 if (bitfield_bit_size > 0)
753 if (bitfield_bit_offset > 0)
754 sval64 >>= bitfield_bit_offset;
755 uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
756 sval64 &= bitfield_mask;
757 // sign extend if needed
758 if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
759 sval64 |= ~bitfield_mask;
766 DataExtractor::GetFloat (offset_t *offset_ptr) const
768 typedef float float_type;
769 float_type val = 0.0;
770 const size_t src_size = sizeof(float_type);
771 const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
774 if (m_byte_order != lldb::endian::InlHostByteOrder())
776 const uint8_t *src_data = (const uint8_t *)src;
777 uint8_t *dst_data = (uint8_t *)&val;
778 for (size_t i=0; i<sizeof(float_type); ++i)
779 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
790 DataExtractor::GetDouble (offset_t *offset_ptr) const
792 typedef double float_type;
793 float_type val = 0.0;
794 const size_t src_size = sizeof(float_type);
795 const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
798 if (m_byte_order != lldb::endian::InlHostByteOrder())
800 const uint8_t *src_data = (const uint8_t *)src;
801 uint8_t *dst_data = (uint8_t *)&val;
802 for (size_t i=0; i<sizeof(float_type); ++i)
803 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
815 DataExtractor::GetLongDouble (offset_t *offset_ptr) const
817 long double val = 0.0;
818 #if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
819 *offset_ptr += CopyByteOrderedData (*offset_ptr, 10, &val, sizeof(val), lldb::endian::InlHostByteOrder());
821 *offset_ptr += CopyByteOrderedData (*offset_ptr, sizeof(val), &val, sizeof(val), lldb::endian::InlHostByteOrder());
827 //------------------------------------------------------------------
828 // Extract a single address from the data and update the offset
829 // pointed to by "offset_ptr". The size of the extracted address
830 // comes from the "this->m_addr_size" member variable and should be
831 // set correctly prior to extracting any address values.
833 // RETURNS the address that was extracted, or zero on failure.
834 //------------------------------------------------------------------
836 DataExtractor::GetAddress (offset_t *offset_ptr) const
838 return GetMaxU64 (offset_ptr, m_addr_size);
842 DataExtractor::GetAddress_unchecked (offset_t *offset_ptr) const
844 return GetMaxU64_unchecked (offset_ptr, m_addr_size);
847 //------------------------------------------------------------------
848 // Extract a single pointer from the data and update the offset
849 // pointed to by "offset_ptr". The size of the extracted pointer
850 // comes from the "this->m_addr_size" member variable and should be
851 // set correctly prior to extracting any pointer values.
853 // RETURNS the pointer that was extracted, or zero on failure.
854 //------------------------------------------------------------------
856 DataExtractor::GetPointer (offset_t *offset_ptr) const
858 return GetMaxU64 (offset_ptr, m_addr_size);
861 //----------------------------------------------------------------------
864 // Used for calls when the value type is specified by a DWARF EH Frame
866 //----------------------------------------------------------------------
869 DataExtractor::GetGNUEHPointer (offset_t *offset_ptr, uint32_t eh_ptr_enc, lldb::addr_t pc_rel_addr, lldb::addr_t text_addr, lldb::addr_t data_addr)//, BSDRelocs *data_relocs) const
871 if (eh_ptr_enc == DW_EH_PE_omit)
872 return ULLONG_MAX; // Value isn't in the buffer...
874 uint64_t baseAddress = 0;
875 uint64_t addressValue = 0;
876 const uint32_t addr_size = GetAddressByteSize();
878 bool signExtendValue = false;
879 // Decode the base part or adjust our offset
880 switch (eh_ptr_enc & 0x70)
883 signExtendValue = true;
884 baseAddress = *offset_ptr;
885 if (pc_rel_addr != LLDB_INVALID_ADDRESS)
886 baseAddress += pc_rel_addr;
888 // Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address.");
891 case DW_EH_PE_textrel:
892 signExtendValue = true;
893 if (text_addr != LLDB_INVALID_ADDRESS)
894 baseAddress = text_addr;
896 // Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero.");
899 case DW_EH_PE_datarel:
900 signExtendValue = true;
901 if (data_addr != LLDB_INVALID_ADDRESS)
902 baseAddress = data_addr;
904 // Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero.");
907 case DW_EH_PE_funcrel:
908 signExtendValue = true;
911 case DW_EH_PE_aligned:
913 // SetPointerSize should be called prior to extracting these so the
914 // pointer size is cached
915 assert(addr_size != 0);
918 // Align to a address size boundary first
919 uint32_t alignOffset = *offset_ptr % addr_size;
921 offset_ptr += addr_size - alignOffset;
930 // Decode the value part
931 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING)
933 case DW_EH_PE_absptr :
935 addressValue = GetAddress (offset_ptr);
937 // addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue);
940 case DW_EH_PE_uleb128 : addressValue = GetULEB128(offset_ptr); break;
941 case DW_EH_PE_udata2 : addressValue = GetU16(offset_ptr); break;
942 case DW_EH_PE_udata4 : addressValue = GetU32(offset_ptr); break;
943 case DW_EH_PE_udata8 : addressValue = GetU64(offset_ptr); break;
944 case DW_EH_PE_sleb128 : addressValue = GetSLEB128(offset_ptr); break;
945 case DW_EH_PE_sdata2 : addressValue = (int16_t)GetU16(offset_ptr); break;
946 case DW_EH_PE_sdata4 : addressValue = (int32_t)GetU32(offset_ptr); break;
947 case DW_EH_PE_sdata8 : addressValue = (int64_t)GetU64(offset_ptr); break;
949 // Unhandled encoding type
954 // Since we promote everything to 64 bit, we may need to sign extend
955 if (signExtendValue && addr_size < sizeof(baseAddress))
957 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
958 if (sign_bit & addressValue)
960 uint64_t mask = ~sign_bit + 1;
961 addressValue |= mask;
964 return baseAddress + addressValue;
968 DataExtractor::ExtractBytes (offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const
970 const uint8_t *src = PeekData (offset, length);
973 if (dst_byte_order != GetByteOrder())
975 // Validate that only a word- or register-sized dst is byte swapped
976 assert (length == 1 || length == 2 || length == 4 || length == 8 ||
977 length == 10 || length == 16 || length == 32);
979 for (uint32_t i=0; i<length; ++i)
980 ((uint8_t*)dst)[i] = src[length - i - 1];
983 ::memcpy (dst, src, length);
989 // Extract data as it exists in target memory
991 DataExtractor::CopyData (offset_t offset,
995 const uint8_t *src = PeekData (offset, length);
998 ::memcpy (dst, src, length);
1004 // Extract data and swap if needed when doing the copy
1006 DataExtractor::CopyByteOrderedData (offset_t src_offset,
1010 ByteOrder dst_byte_order) const
1012 // Validate the source info
1013 if (!ValidOffsetForDataOfSize(src_offset, src_len))
1014 assert (ValidOffsetForDataOfSize(src_offset, src_len));
1015 assert (src_len > 0);
1016 assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
1018 // Validate the destination info
1019 assert (dst_void_ptr != NULL);
1020 assert (dst_len > 0);
1021 assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
1023 // Validate that only a word- or register-sized dst is byte swapped
1024 assert (dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
1025 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
1028 // Must have valid byte orders set in this object and for destination
1029 if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) ||
1030 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
1034 uint8_t* dst = (uint8_t*)dst_void_ptr;
1035 const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len);
1038 if (dst_len >= src_len)
1040 // We are copying the entire value from src into dst.
1041 // Calculate how many, if any, zeroes we need for the most
1042 // significant bytes if "dst_len" is greater than "src_len"...
1043 const size_t num_zeroes = dst_len - src_len;
1044 if (dst_byte_order == eByteOrderBig)
1046 // Big endian, so we lead with zeroes...
1048 ::memset (dst, 0, num_zeroes);
1049 // Then either copy or swap the rest
1050 if (m_byte_order == eByteOrderBig)
1052 ::memcpy (dst + num_zeroes, src, src_len);
1056 for (i=0; i<src_len; ++i)
1057 dst[i+num_zeroes] = src[src_len - 1 - i];
1062 // Little endian destination, so we lead the value bytes
1063 if (m_byte_order == eByteOrderBig)
1065 for (i=0; i<src_len; ++i)
1066 dst[i] = src[src_len - 1 - i];
1070 ::memcpy (dst, src, src_len);
1072 // And zero the rest...
1074 ::memset (dst + src_len, 0, num_zeroes);
1080 // We are only copying some of the value from src into dst..
1082 if (dst_byte_order == eByteOrderBig)
1085 if (m_byte_order == eByteOrderBig)
1087 // Big endian dst, with big endian src
1088 ::memcpy (dst, src + (src_len - dst_len), dst_len);
1092 // Big endian dst, with little endian src
1093 for (i=0; i<dst_len; ++i)
1094 dst[i] = src[dst_len - 1 - i];
1099 // Little endian dst
1100 if (m_byte_order == eByteOrderBig)
1102 // Little endian dst, with big endian src
1103 for (i=0; i<dst_len; ++i)
1104 dst[i] = src[src_len - 1 - i];
1108 // Little endian dst, with big endian src
1109 ::memcpy (dst, src, dst_len);
1120 //----------------------------------------------------------------------
1121 // Extracts a variable length NULL terminated C string from
1122 // the data at the offset pointed to by "offset_ptr". The
1123 // "offset_ptr" will be updated with the offset of the byte that
1124 // follows the NULL terminator byte.
1126 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1127 // "length" is non-zero and there aren't enough avaialable
1128 // bytes, NULL will be returned and "offset_ptr" will not be
1130 //----------------------------------------------------------------------
1132 DataExtractor::GetCStr (offset_t *offset_ptr) const
1134 const char *cstr = (const char *)PeekData (*offset_ptr, 1);
1137 const char *cstr_end = cstr;
1138 const char *end = (const char *)m_end;
1139 while (cstr_end < end && *cstr_end)
1142 // Now we are either at the end of the data or we point to the
1143 // NULL C string terminator with cstr_end...
1144 if (*cstr_end == '\0')
1146 // Advance the offset with one extra byte for the NULL terminator
1147 *offset_ptr += (cstr_end - cstr + 1);
1151 // We reached the end of the data without finding a NULL C string
1152 // terminator. Fall through and return NULL otherwise anyone that
1153 // would have used the result as a C string can wonder into
1154 // unknown memory...
1159 //----------------------------------------------------------------------
1160 // Extracts a NULL terminated C string from the fixed length field of
1161 // length "len" at the offset pointed to by "offset_ptr".
1162 // The "offset_ptr" will be updated with the offset of the byte that
1163 // follows the fixed length field.
1165 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1166 // the offset plus the length of the field is out of bounds, or if the
1167 // field does not contain a NULL terminator byte, NULL will be returned
1168 // and "offset_ptr" will not be updated.
1169 //----------------------------------------------------------------------
1171 DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const
1173 const char *cstr = (const char *)PeekData (*offset_ptr, len);
1176 if (memchr (cstr, '\0', len) == NULL)
1186 //------------------------------------------------------------------
1187 // Peeks at a string in the contained data. No verification is done
1188 // to make sure the entire string lies within the bounds of this
1189 // object's data, only "offset" is verified to be a valid offset.
1191 // Returns a valid C string pointer if "offset" is a valid offset in
1192 // this object's data, else NULL is returned.
1193 //------------------------------------------------------------------
1195 DataExtractor::PeekCStr (offset_t offset) const
1197 return (const char *)PeekData (offset, 1);
1200 //----------------------------------------------------------------------
1201 // Extracts an unsigned LEB128 number from this object's data
1202 // starting at the offset pointed to by "offset_ptr". The offset
1203 // pointed to by "offset_ptr" will be updated with the offset of the
1204 // byte following the last extracted byte.
1206 // Returned the extracted integer value.
1207 //----------------------------------------------------------------------
1209 DataExtractor::GetULEB128 (offset_t *offset_ptr) const
1211 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1215 const uint8_t *end = m_end;
1219 uint64_t result = *src++;
1226 uint8_t byte = *src++;
1227 result |= (byte & 0x7f) << shift;
1228 if ((byte & 0x80) == 0)
1233 *offset_ptr = src - m_start;
1240 //----------------------------------------------------------------------
1241 // Extracts an signed LEB128 number from this object's data
1242 // starting at the offset pointed to by "offset_ptr". The offset
1243 // pointed to by "offset_ptr" will be updated with the offset of the
1244 // byte following the last extracted byte.
1246 // Returned the extracted integer value.
1247 //----------------------------------------------------------------------
1249 DataExtractor::GetSLEB128 (offset_t *offset_ptr) const
1251 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1255 const uint8_t *end = m_end;
1261 int size = sizeof (int64_t) * 8;
1270 result |= (byte & 0x7f) << shift;
1272 if ((byte & 0x80) == 0)
1276 // Sign bit of byte is 2nd high order bit (0x40)
1277 if (shift < size && (byte & 0x40))
1278 result |= - (1 << shift);
1280 *offset_ptr += bytecount;
1286 //----------------------------------------------------------------------
1287 // Skips a ULEB128 number (signed or unsigned) from this object's
1288 // data starting at the offset pointed to by "offset_ptr". The
1289 // offset pointed to by "offset_ptr" will be updated with the offset
1290 // of the byte following the last extracted byte.
1292 // Returns the number of bytes consumed during the extraction.
1293 //----------------------------------------------------------------------
1295 DataExtractor::Skip_LEB128 (offset_t *offset_ptr) const
1297 uint32_t bytes_consumed = 0;
1298 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1302 const uint8_t *end = m_end;
1306 const uint8_t *src_pos = src;
1307 while ((src_pos < end) && (*src_pos++ & 0x80))
1309 *offset_ptr += src_pos - src;
1311 return bytes_consumed;
1315 GetAPInt (const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size, llvm::APInt &result)
1317 llvm::SmallVector<uint64_t, 2> uint64_array;
1318 lldb::offset_t bytes_left = byte_size;
1320 const lldb::ByteOrder byte_order = data.GetByteOrder();
1321 if (byte_order == lldb::eByteOrderLittle)
1323 while (bytes_left > 0)
1325 if (bytes_left >= 8)
1327 u64 = data.GetU64(offset_ptr);
1332 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
1335 uint64_array.push_back(u64);
1337 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1340 else if (byte_order == lldb::eByteOrderBig)
1342 lldb::offset_t be_offset = *offset_ptr + byte_size;
1343 lldb::offset_t temp_offset;
1344 while (bytes_left > 0)
1346 if (bytes_left >= 8)
1349 temp_offset = be_offset;
1350 u64 = data.GetU64(&temp_offset);
1355 be_offset -= bytes_left;
1356 temp_offset = be_offset;
1357 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
1360 uint64_array.push_back(u64);
1362 *offset_ptr += byte_size;
1363 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1369 static lldb::offset_t
1370 DumpAPInt (Stream *s, const DataExtractor &data, lldb::offset_t offset, lldb::offset_t byte_size, bool is_signed, unsigned radix)
1373 if (GetAPInt (data, &offset, byte_size, apint))
1375 std::string apint_str(apint.toString(radix, is_signed));
1387 s->Write(apint_str.c_str(), apint_str.size());
1392 static float half2float (uint16_t half)
1395 llvm_unreachable("half2float not implemented for MSVC");
1397 union{ float f; uint32_t u;}u;
1398 int32_t v = (int16_t) half;
1400 if( 0 == (v & 0x7c00))
1402 u.u = v & 0x80007FFFU;
1403 return u.f * ldexpf(1, 125);
1407 u.u = v | 0x70000000U;
1408 return u.f * ldexpf(1, -112);
1413 DataExtractor::Dump (Stream *s,
1414 offset_t start_offset,
1415 lldb::Format item_format,
1416 size_t item_byte_size,
1418 size_t num_per_line,
1420 uint32_t item_bit_size, // If zero, this is not a bitfield value, if non-zero, the value is a bitfield
1421 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the shift amount to apply to a bitfield
1422 ExecutionContextScope *exe_scope) const
1425 return start_offset;
1427 if (item_format == eFormatPointer)
1429 if (item_byte_size != 4 && item_byte_size != 8)
1430 item_byte_size = s->GetAddressByteSize();
1433 offset_t offset = start_offset;
1435 if (item_format == eFormatInstruction)
1439 target_sp = exe_scope->CalculateTarget();
1442 DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL, NULL));
1443 if (disassembler_sp)
1445 lldb::addr_t addr = base_addr + start_offset;
1446 lldb_private::Address so_addr;
1447 bool data_from_file = true;
1448 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1450 data_from_file = false;
1454 if (target_sp->GetSectionLoadList().IsEmpty() || !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
1455 so_addr.SetRawAddress(addr);
1458 size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false, data_from_file);
1462 offset += bytes_consumed;
1463 const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
1464 const bool show_bytes = true;
1465 ExecutionContext exe_ctx;
1466 exe_scope->CalculateExecutionContext(exe_ctx);
1467 disassembler_sp->GetInstructionList().Dump (s, show_address, show_bytes, &exe_ctx);
1469 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
1470 // I'll fix that but for now, just clear the list and it will go away nicely.
1471 disassembler_sp->GetInstructionList().Clear();
1476 s->Printf ("invalid target");
1481 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8)
1482 item_format = eFormatHex;
1484 lldb::offset_t line_start_offset = start_offset;
1485 for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count)
1487 if ((count % num_per_line) == 0)
1491 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1493 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
1494 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, LLDB_INVALID_OFFSET, LLDB_INVALID_ADDRESS, 0, 0);
1498 if (base_addr != LLDB_INVALID_ADDRESS)
1499 s->Printf ("0x%8.8" PRIx64 ": ", (uint64_t)(base_addr + (offset - start_offset)));
1500 line_start_offset = offset;
1503 if (item_format != eFormatChar &&
1504 item_format != eFormatCharPrintable &&
1505 item_format != eFormatCharArray &&
1512 switch (item_format)
1514 case eFormatBoolean:
1515 if (item_byte_size <= 8)
1516 s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false");
1519 s->Printf("error: unsupported byte size (%zu) for boolean format", item_byte_size);
1525 if (item_byte_size <= 8)
1527 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1528 // Avoid std::bitset<64>::to_string() since it is missing in
1529 // earlier C++ libraries
1530 std::string binary_value(64, '0');
1531 std::bitset<64> bits(uval64);
1532 for (i = 0; i < 64; ++i)
1534 binary_value[64 - 1 - i] = '1';
1535 if (item_bit_size > 0)
1536 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
1537 else if (item_byte_size > 0 && item_byte_size <= 8)
1538 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
1542 const bool is_signed = false;
1543 const unsigned radix = 2;
1544 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1549 case eFormatBytesWithASCII:
1550 for (i=0; i<item_byte_size; ++i)
1552 s->Printf ("%2.2x", GetU8(&offset));
1554 // Put an extra space between the groups of bytes if more than one
1555 // is being dumped in a group (item_byte_size is more than 1).
1556 if (item_byte_size > 1)
1561 case eFormatCharPrintable:
1562 case eFormatCharArray:
1564 // If we are only printing one character surround it with single
1566 if (item_count == 1 && item_format == eFormatChar)
1569 const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1571 s->Printf ("%c", (char)ch);
1572 else if (item_format != eFormatCharPrintable)
1576 case '\033': s->Printf ("\\e"); break;
1577 case '\a': s->Printf ("\\a"); break;
1578 case '\b': s->Printf ("\\b"); break;
1579 case '\f': s->Printf ("\\f"); break;
1580 case '\n': s->Printf ("\\n"); break;
1581 case '\r': s->Printf ("\\r"); break;
1582 case '\t': s->Printf ("\\t"); break;
1583 case '\v': s->Printf ("\\v"); break;
1584 case '\0': s->Printf ("\\0"); break;
1586 if (item_byte_size == 1)
1587 s->Printf ("\\x%2.2x", (uint8_t)ch);
1589 s->Printf ("%" PRIu64, ch);
1595 s->PutChar(NON_PRINTABLE_CHAR);
1598 // If we are only printing one character surround it with single quotes
1599 if (item_count == 1 && item_format == eFormatChar)
1604 case eFormatEnum: // Print enum value as a signed integer when we don't get the enum type
1605 case eFormatDecimal:
1606 if (item_byte_size <= 8)
1607 s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1610 const bool is_signed = true;
1611 const unsigned radix = 10;
1612 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1616 case eFormatUnsigned:
1617 if (item_byte_size <= 8)
1618 s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1621 const bool is_signed = false;
1622 const unsigned radix = 10;
1623 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1628 if (item_byte_size <= 8)
1629 s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1632 const bool is_signed = false;
1633 const unsigned radix = 8;
1634 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1640 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1642 for (i=0; i<item_byte_size; ++i)
1644 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
1646 s->Printf ("%c", ch);
1651 case '\033': s->Printf ("\\e"); break;
1652 case '\a': s->Printf ("\\a"); break;
1653 case '\b': s->Printf ("\\b"); break;
1654 case '\f': s->Printf ("\\f"); break;
1655 case '\n': s->Printf ("\\n"); break;
1656 case '\r': s->Printf ("\\r"); break;
1657 case '\t': s->Printf ("\\t"); break;
1658 case '\v': s->Printf ("\\v"); break;
1659 case '\0': s->Printf ("\\0"); break;
1660 default: s->Printf ("\\x%2.2x", ch); break;
1668 case eFormatCString:
1670 const char *cstr = GetCStr(&offset);
1675 offset = LLDB_INVALID_OFFSET;
1681 while (const char c = *cstr)
1691 case '\033': s->Printf ("\\e"); break;
1692 case '\a': s->Printf ("\\a"); break;
1693 case '\b': s->Printf ("\\b"); break;
1694 case '\f': s->Printf ("\\f"); break;
1695 case '\n': s->Printf ("\\n"); break;
1696 case '\r': s->Printf ("\\r"); break;
1697 case '\t': s->Printf ("\\t"); break;
1698 case '\v': s->Printf ("\\v"); break;
1699 default: s->Printf ("\\x%2.2x", c); break;
1712 case eFormatPointer:
1713 s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t));
1717 case eFormatComplexInteger:
1719 size_t complex_int_byte_size = item_byte_size / 2;
1721 if (complex_int_byte_size <= 8)
1723 s->Printf("%" PRIu64, GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1724 s->Printf(" + %" PRIu64 "i", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1728 s->Printf("error: unsupported byte size (%zu) for complex integer format", item_byte_size);
1734 case eFormatComplex:
1735 if (sizeof(float) * 2 == item_byte_size)
1737 float f32_1 = GetFloat (&offset);
1738 float f32_2 = GetFloat (&offset);
1740 s->Printf ("%g + %gi", f32_1, f32_2);
1743 else if (sizeof(double) * 2 == item_byte_size)
1745 double d64_1 = GetDouble (&offset);
1746 double d64_2 = GetDouble (&offset);
1748 s->Printf ("%lg + %lgi", d64_1, d64_2);
1751 else if (sizeof(long double) * 2 == item_byte_size)
1753 long double ld64_1 = GetLongDouble (&offset);
1754 long double ld64_2 = GetLongDouble (&offset);
1755 s->Printf ("%Lg + %Lgi", ld64_1, ld64_2);
1760 s->Printf("error: unsupported byte size (%zu) for complex float format", item_byte_size);
1766 case eFormatDefault:
1768 case eFormatHexUppercase:
1770 bool wantsuppercase = (item_format == eFormatHexUppercase);
1771 switch (item_byte_size)
1777 s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1781 assert (item_bit_size == 0 && item_bit_offset == 0);
1782 const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size);
1785 s->PutCString("0x");
1787 if (m_byte_order == eByteOrderBig)
1789 for (idx = 0; idx < item_byte_size; ++idx)
1790 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
1794 for (idx = 0; idx < item_byte_size; ++idx)
1795 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]);
1807 bool used_apfloat = false;
1809 target_sp = exe_scope->CalculateTarget();
1812 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1815 clang::ASTContext *ast = clang_ast->getASTContext();
1818 llvm::SmallVector<char, 256> sv;
1819 // Show full precision when printing float values
1820 const unsigned format_precision = 0;
1821 const unsigned format_max_padding = 100;
1822 size_t item_bit_size = item_byte_size * 8;
1824 if (item_bit_size == ast->getTypeSize(ast->FloatTy))
1826 llvm::APInt apint(item_bit_size, this->GetMaxU64(&offset, item_byte_size));
1827 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->FloatTy), apint);
1828 apfloat.toString(sv, format_precision, format_max_padding);
1830 else if (item_bit_size == ast->getTypeSize(ast->DoubleTy))
1833 if (GetAPInt (*this, &offset, item_byte_size, apint))
1835 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->DoubleTy), apint);
1836 apfloat.toString(sv, format_precision, format_max_padding);
1839 else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy))
1842 switch (target_sp->GetArchitecture().GetCore())
1844 case ArchSpec::eCore_x86_32_i386:
1845 case ArchSpec::eCore_x86_32_i486:
1846 case ArchSpec::eCore_x86_32_i486sx:
1847 case ArchSpec::eCore_x86_64_x86_64:
1848 case ArchSpec::eCore_x86_64_x86_64h:
1849 // clang will assert when contructing the apfloat if we use a 16 byte integer value
1850 if (GetAPInt (*this, &offset, 10, apint))
1852 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1853 apfloat.toString(sv, format_precision, format_max_padding);
1858 if (GetAPInt (*this, &offset, item_byte_size, apint))
1860 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1861 apfloat.toString(sv, format_precision, format_max_padding);
1866 else if (item_bit_size == ast->getTypeSize(ast->HalfTy))
1868 llvm::APInt apint(item_bit_size, this->GetU16(&offset));
1869 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->HalfTy), apint);
1870 apfloat.toString(sv, format_precision, format_max_padding);
1875 s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
1876 used_apfloat = true;
1884 std::ostringstream ss;
1885 if (item_byte_size == sizeof(float) || item_byte_size == 2)
1888 if (item_byte_size == 2)
1890 uint16_t half = this->GetU16(&offset);
1891 f = half2float(half);
1895 f = GetFloat (&offset);
1897 ss.precision(std::numeric_limits<float>::digits10);
1900 else if (item_byte_size == sizeof(double))
1902 ss.precision(std::numeric_limits<double>::digits10);
1903 ss << GetDouble(&offset);
1905 else if (item_byte_size == sizeof(long double) || item_byte_size == 10)
1907 ss.precision(std::numeric_limits<long double>::digits10);
1908 ss << GetLongDouble(&offset);
1912 s->Printf("error: unsupported byte size (%zu) for float format", item_byte_size);
1916 s->Printf("%s", ss.str().c_str());
1921 case eFormatUnicode16:
1922 s->Printf("U+%4.4x", GetU16 (&offset));
1925 case eFormatUnicode32:
1926 s->Printf("U+0x%8.8x", GetU32 (&offset));
1929 case eFormatAddressInfo:
1931 addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1932 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), addr);
1935 TargetSP target_sp (exe_scope->CalculateTarget());
1936 lldb_private::Address so_addr;
1939 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1944 Address::DumpStyleResolvedDescription,
1945 Address::DumpStyleModuleWithFileAddress);
1949 so_addr.SetOffset(addr);
1950 so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription);
1957 case eFormatHexFloat:
1958 if (sizeof(float) == item_byte_size)
1960 char float_cstr[256];
1961 llvm::APFloat ap_float (GetFloat (&offset));
1962 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1963 s->Printf ("%s", float_cstr);
1966 else if (sizeof(double) == item_byte_size)
1968 char float_cstr[256];
1969 llvm::APFloat ap_float (GetDouble (&offset));
1970 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1971 s->Printf ("%s", float_cstr);
1976 s->Printf("error: unsupported byte size (%zu) for hex float format", item_byte_size);
1981 // please keep the single-item formats below in sync with FormatManager::GetSingleItemFormat
1982 // if you fail to do so, users will start getting different outputs depending on internal
1983 // implementation details they should not care about ||
1984 case eFormatVectorOfChar: // ||
1985 s->PutChar('{'); // \/
1986 offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1990 case eFormatVectorOfSInt8:
1992 offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1996 case eFormatVectorOfUInt8:
1998 offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
2002 case eFormatVectorOfSInt16:
2004 offset = Dump (s, offset, eFormatDecimal, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
2008 case eFormatVectorOfUInt16:
2010 offset = Dump (s, offset, eFormatHex, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
2014 case eFormatVectorOfSInt32:
2016 offset = Dump (s, offset, eFormatDecimal, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
2020 case eFormatVectorOfUInt32:
2022 offset = Dump (s, offset, eFormatHex, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
2026 case eFormatVectorOfSInt64:
2028 offset = Dump (s, offset, eFormatDecimal, sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0);
2032 case eFormatVectorOfUInt64:
2034 offset = Dump (s, offset, eFormatHex, sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0);
2038 case eFormatVectorOfFloat32:
2040 offset = Dump (s, offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
2044 case eFormatVectorOfFloat64:
2046 offset = Dump (s, offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
2050 case eFormatVectorOfUInt128:
2052 offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
2058 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
2060 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
2061 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, LLDB_INVALID_OFFSET, LLDB_INVALID_ADDRESS, 0, 0);
2063 return offset; // Return the offset at which we ended up
2066 //----------------------------------------------------------------------
2067 // Dumps bytes from this object's data to the stream "s" starting
2068 // "start_offset" bytes into this data, and ending with the byte
2069 // before "end_offset". "base_addr" will be added to the offset
2070 // into the dumped data when showing the offset into the data in the
2071 // output information. "num_per_line" objects of type "type" will
2072 // be dumped with the option to override the format for each object
2073 // with "type_format". "type_format" is a printf style formatting
2074 // string. If "type_format" is NULL, then an appropriate format
2075 // string will be used for the supplied "type". If the stream "s"
2076 // is NULL, then the output will be send to Log().
2077 //----------------------------------------------------------------------
2079 DataExtractor::PutToLog
2082 offset_t start_offset,
2085 uint32_t num_per_line,
2086 DataExtractor::Type type,
2091 return start_offset;
2094 offset_t end_offset;
2097 for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count)
2099 if ((count % num_per_line) == 0)
2101 // Print out any previous string
2102 if (sstr.GetSize() > 0)
2104 log->Printf("%s", sstr.GetData());
2107 // Reset string offset and fill the current line string with address:
2108 if (base_addr != LLDB_INVALID_ADDRESS)
2109 sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset)));
2114 case TypeUInt8: sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break;
2117 char ch = GetU8(&offset);
2118 sstr.Printf (format ? format : " %c", isprint(ch) ? ch : ' ');
2121 case TypeUInt16: sstr.Printf (format ? format : " %4.4x", GetU16(&offset)); break;
2122 case TypeUInt32: sstr.Printf (format ? format : " %8.8x", GetU32(&offset)); break;
2123 case TypeUInt64: sstr.Printf (format ? format : " %16.16" PRIx64, GetU64(&offset)); break;
2124 case TypePointer: sstr.Printf (format ? format : " 0x%" PRIx64, GetAddress(&offset)); break;
2125 case TypeULEB128: sstr.Printf (format ? format : " 0x%" PRIx64, GetULEB128(&offset)); break;
2126 case TypeSLEB128: sstr.Printf (format ? format : " %" PRId64, GetSLEB128(&offset)); break;
2130 if (sstr.GetSize() > 0)
2131 log->Printf("%s", sstr.GetData());
2133 return offset; // Return the offset at which we ended up
2136 //----------------------------------------------------------------------
2139 // Dump out a UUID starting at 'offset' bytes into the buffer
2140 //----------------------------------------------------------------------
2142 DataExtractor::DumpUUID (Stream *s, offset_t offset) const
2146 const uint8_t *uuid_data = PeekData(offset, 16);
2149 lldb_private::UUID uuid(uuid_data, 16);
2154 s->Printf("<not enough data for UUID at offset 0x%8.8" PRIx64 ">", offset);
2160 DataExtractor::DumpHexBytes (Stream *s,
2163 uint32_t bytes_per_line,
2166 DataExtractor data (src, src_len, eByteOrderLittle, 4);
2168 0, // Offset into "src"
2169 eFormatBytes, // Dump as hex bytes
2170 1, // Size of each item is 1 for single bytes
2171 src_len, // Number of bytes
2172 bytes_per_line, // Num bytes per line
2173 base_addr, // Base address
2174 0, 0); // Bitfield info
2178 DataExtractor::Copy (DataExtractor &dest_data) const
2180 if (m_data_sp.get())
2182 // we can pass along the SP to the data
2183 dest_data.SetData(m_data_sp);
2187 const uint8_t *base_ptr = m_start;
2188 size_t data_size = GetByteSize();
2189 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
2191 return GetByteSize();
2195 DataExtractor::Append(DataExtractor& rhs)
2197 if (rhs.GetByteOrder() != GetByteOrder())
2200 if (rhs.GetByteSize() == 0)
2203 if (GetByteSize() == 0)
2204 return (rhs.Copy(*this) > 0);
2206 size_t bytes = GetByteSize() + rhs.GetByteSize();
2208 DataBufferHeap *buffer_heap_ptr = NULL;
2209 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2211 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2214 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2216 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2217 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
2225 DataExtractor::Append(void* buf, offset_t length)
2233 size_t bytes = GetByteSize() + length;
2235 DataBufferHeap *buffer_heap_ptr = NULL;
2236 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2238 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2241 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2243 if (GetByteSize() > 0)
2244 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2246 memcpy(bytes_ptr + GetByteSize(), buf, length);