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 = 0)
58 memcpy (&value, ptr + offset, 4);
62 static inline uint64_t
63 ReadInt64(const unsigned char* ptr, offset_t offset = 0)
66 memcpy (&value, ptr + offset, 8);
70 static inline uint16_t
71 ReadInt16(const void* ptr)
74 memcpy (&value, ptr, 2);
78 static inline uint16_t
79 ReadSwapInt16(const unsigned char* ptr, offset_t offset)
82 memcpy (&value, ptr + offset, 2);
83 return llvm::ByteSwap_16(value);
86 static inline uint32_t
87 ReadSwapInt32 (const unsigned char* ptr, offset_t offset)
90 memcpy (&value, ptr + offset, 4);
91 return llvm::ByteSwap_32(value);
94 static inline uint64_t
95 ReadSwapInt64(const unsigned char* ptr, offset_t offset)
98 memcpy (&value, ptr + offset, 8);
99 return llvm::ByteSwap_64(value);
102 static inline uint16_t
103 ReadSwapInt16(const void* ptr)
106 memcpy (&value, ptr, 2);
107 return llvm::ByteSwap_16(value);
110 static inline uint32_t
111 ReadSwapInt32 (const void* ptr)
114 memcpy (&value, ptr, 4);
115 return llvm::ByteSwap_32(value);
118 static inline uint64_t
119 ReadSwapInt64(const void* ptr)
122 memcpy (&value, ptr, 8);
123 return llvm::ByteSwap_64(value);
126 #define NON_PRINTABLE_CHAR '.'
127 //----------------------------------------------------------------------
128 // Default constructor.
129 //----------------------------------------------------------------------
130 DataExtractor::DataExtractor () :
133 m_byte_order(lldb::endian::InlHostByteOrder()),
139 //----------------------------------------------------------------------
140 // This constructor allows us to use data that is owned by someone else.
141 // The data must stay around as long as this object is valid.
142 //----------------------------------------------------------------------
143 DataExtractor::DataExtractor (const void* data, offset_t length, ByteOrder endian, uint32_t addr_size) :
144 m_start ((uint8_t*)data),
145 m_end ((uint8_t*)data + length),
146 m_byte_order(endian),
147 m_addr_size (addr_size),
152 //----------------------------------------------------------------------
153 // Make a shared pointer reference to the shared data in "data_sp" and
154 // set the endian swapping setting to "swap", and the address size to
155 // "addr_size". The shared data reference will ensure the data lives
156 // as long as any DataExtractor objects exist that have a reference to
158 //----------------------------------------------------------------------
159 DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint32_t addr_size) :
162 m_byte_order(endian),
163 m_addr_size (addr_size),
169 //----------------------------------------------------------------------
170 // Initialize this object with a subset of the data bytes in "data".
171 // If "data" contains shared data, then a reference to this shared
172 // data will added and the shared data will stay around as long
173 // as any object contains a reference to that data. The endian
174 // swap and address size settings are copied from "data".
175 //----------------------------------------------------------------------
176 DataExtractor::DataExtractor (const DataExtractor& data, offset_t offset, offset_t length) :
179 m_byte_order(data.m_byte_order),
180 m_addr_size(data.m_addr_size),
183 if (data.ValidOffset(offset))
185 offset_t bytes_available = data.GetByteSize() - offset;
186 if (length > bytes_available)
187 length = bytes_available;
188 SetData(data, offset, length);
192 DataExtractor::DataExtractor (const DataExtractor& rhs) :
193 m_start (rhs.m_start),
195 m_byte_order (rhs.m_byte_order),
196 m_addr_size (rhs.m_addr_size),
197 m_data_sp (rhs.m_data_sp)
201 //----------------------------------------------------------------------
202 // Assignment operator
203 //----------------------------------------------------------------------
205 DataExtractor::operator= (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;
218 //----------------------------------------------------------------------
220 //----------------------------------------------------------------------
221 DataExtractor::~DataExtractor ()
225 //------------------------------------------------------------------
226 // Clears the object contents back to a default invalid state, and
227 // release any references to shared data that this object may
229 //------------------------------------------------------------------
231 DataExtractor::Clear ()
235 m_byte_order = lldb::endian::InlHostByteOrder();
240 //------------------------------------------------------------------
241 // If this object contains shared data, this function returns the
242 // offset into that shared data. Else zero is returned.
243 //------------------------------------------------------------------
245 DataExtractor::GetSharedDataOffset () const
249 const DataBuffer * data = m_data_sp.get();
252 const uint8_t * data_bytes = data->GetBytes();
253 if (data_bytes != NULL)
255 assert(m_start >= data_bytes);
256 return m_start - data_bytes;
263 //----------------------------------------------------------------------
264 // Set the data with which this object will extract from to data
265 // starting at BYTES and set the length of the data to LENGTH bytes
266 // long. The data is externally owned must be around at least as
267 // long as this object points to the data. No copy of the data is
268 // made, this object just refers to this data and can extract from
269 // it. If this object refers to any shared data upon entry, the
270 // reference to that data will be released. Is SWAP is set to true,
271 // any data extracted will be endian swapped.
272 //----------------------------------------------------------------------
274 DataExtractor::SetData (const void *bytes, offset_t length, ByteOrder endian)
276 m_byte_order = endian;
278 if (bytes == NULL || length == 0)
285 m_start = (uint8_t *)bytes;
286 m_end = m_start + length;
288 return GetByteSize();
291 //----------------------------------------------------------------------
292 // Assign the data for this object to be a subrange in "data"
293 // starting "data_offset" bytes into "data" and ending "data_length"
294 // bytes later. If "data_offset" is not a valid offset into "data",
295 // then this object will contain no bytes. If "data_offset" is
296 // within "data" yet "data_length" is too large, the length will be
297 // capped at the number of bytes remaining in "data". If "data"
298 // contains a shared pointer to other data, then a ref counted
299 // pointer to that data will be made in this object. If "data"
300 // doesn't contain a shared pointer to data, then the bytes referred
301 // to in "data" will need to exist at least as long as this object
302 // refers to those bytes. The address size and endian swap settings
303 // are copied from the current values in "data".
304 //----------------------------------------------------------------------
306 DataExtractor::SetData (const DataExtractor& data, offset_t data_offset, offset_t data_length)
308 m_addr_size = data.m_addr_size;
309 // If "data" contains shared pointer to data, then we can use that
310 if (data.m_data_sp.get())
312 m_byte_order = data.m_byte_order;
313 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length);
316 // We have a DataExtractor object that just has a pointer to bytes
317 if (data.ValidOffset(data_offset))
319 if (data_length > data.GetByteSize() - data_offset)
320 data_length = data.GetByteSize() - data_offset;
321 return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder());
326 //----------------------------------------------------------------------
327 // Assign the data for this object to be a subrange of the shared
328 // data in "data_sp" starting "data_offset" bytes into "data_sp"
329 // and ending "data_length" bytes later. If "data_offset" is not
330 // a valid offset into "data_sp", then this object will contain no
331 // bytes. If "data_offset" is within "data_sp" yet "data_length" is
332 // too large, the length will be capped at the number of bytes
333 // remaining in "data_sp". A ref counted pointer to the data in
334 // "data_sp" will be made in this object IF the number of bytes this
335 // object refers to in greater than zero (if at least one byte was
336 // available starting at "data_offset") to ensure the data stays
337 // around as long as it is needed. The address size and endian swap
338 // settings will remain unchanged from their current settings.
339 //----------------------------------------------------------------------
341 DataExtractor::SetData (const DataBufferSP& data_sp, offset_t data_offset, offset_t data_length)
343 m_start = m_end = NULL;
350 const size_t data_size = data_sp->GetByteSize();
351 if (data_offset < data_size)
353 m_start = data_sp->GetBytes() + data_offset;
354 const size_t bytes_left = data_size - data_offset;
355 // Cap the length of we asked for too many
356 if (data_length <= bytes_left)
357 m_end = m_start + data_length; // We got all the bytes we wanted
359 m_end = m_start + bytes_left; // Not all the bytes requested were available in the shared data
364 size_t new_size = GetByteSize();
366 // Don't hold a shared pointer to the data buffer if we don't share
367 // any valid bytes in the shared buffer.
374 //----------------------------------------------------------------------
375 // Extract a single unsigned char from the binary data and update
376 // the offset pointed to by "offset_ptr".
378 // RETURNS the byte that was extracted, or zero on failure.
379 //----------------------------------------------------------------------
381 DataExtractor::GetU8 (offset_t *offset_ptr) const
383 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, 1);
389 //----------------------------------------------------------------------
390 // Extract "count" unsigned chars from the binary data and update the
391 // offset pointed to by "offset_ptr". The extracted data is copied into
394 // RETURNS the non-NULL buffer pointer upon successful extraction of
395 // all the requested bytes, or NULL when the data is not available in
396 // the buffer due to being out of bounds, or insufficient data.
397 //----------------------------------------------------------------------
399 DataExtractor::GetU8 (offset_t *offset_ptr, void *dst, uint32_t count) const
401 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, count);
404 // Copy the data into the buffer
405 memcpy (dst, data, count);
406 // Return a non-NULL pointer to the converted data as an indicator of success
412 //----------------------------------------------------------------------
413 // Extract a single uint16_t from the data and update the offset
414 // pointed to by "offset_ptr".
416 // RETURNS the uint16_t that was extracted, or zero on failure.
417 //----------------------------------------------------------------------
419 DataExtractor::GetU16 (offset_t *offset_ptr) const
422 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
425 if (m_byte_order != lldb::endian::InlHostByteOrder())
426 val = ReadSwapInt16(data);
428 val = ReadInt16 (data);
434 DataExtractor::GetU16_unchecked (offset_t *offset_ptr) const
437 if (m_byte_order == lldb::endian::InlHostByteOrder())
438 val = ReadInt16 (m_start, *offset_ptr);
440 val = ReadSwapInt16(m_start, *offset_ptr);
441 *offset_ptr += sizeof(val);
446 DataExtractor::GetU32_unchecked (offset_t *offset_ptr) const
449 if (m_byte_order == lldb::endian::InlHostByteOrder())
450 val = ReadInt32 (m_start, *offset_ptr);
452 val = ReadSwapInt32 (m_start, *offset_ptr);
453 *offset_ptr += sizeof(val);
458 DataExtractor::GetU64_unchecked (offset_t *offset_ptr) const
461 if (m_byte_order == lldb::endian::InlHostByteOrder())
462 val = ReadInt64 (m_start, *offset_ptr);
464 val = ReadSwapInt64 (m_start, *offset_ptr);
465 *offset_ptr += sizeof(val);
470 //----------------------------------------------------------------------
471 // Extract "count" uint16_t values from the binary data and update
472 // the offset pointed to by "offset_ptr". The extracted data is
473 // copied into "dst".
475 // RETURNS the non-NULL buffer pointer upon successful extraction of
476 // all the requested bytes, or NULL when the data is not available
477 // in the buffer due to being out of bounds, or insufficient data.
478 //----------------------------------------------------------------------
480 DataExtractor::GetU16 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
482 const size_t src_size = sizeof(uint16_t) * count;
483 const uint16_t *src = (const uint16_t *)GetData (offset_ptr, src_size);
486 if (m_byte_order != lldb::endian::InlHostByteOrder())
488 uint16_t *dst_pos = (uint16_t *)void_dst;
489 uint16_t *dst_end = dst_pos + count;
490 const uint16_t *src_pos = src;
491 while (dst_pos < dst_end)
493 *dst_pos = ReadSwapInt16 (src_pos);
500 memcpy (void_dst, src, src_size);
502 // Return a non-NULL pointer to the converted data as an indicator of success
508 //----------------------------------------------------------------------
509 // Extract a single uint32_t from the data and update the offset
510 // pointed to by "offset_ptr".
512 // RETURNS the uint32_t that was extracted, or zero on failure.
513 //----------------------------------------------------------------------
515 DataExtractor::GetU32 (offset_t *offset_ptr) const
518 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
521 if (m_byte_order != lldb::endian::InlHostByteOrder())
523 val = ReadSwapInt32 (data);
527 memcpy (&val, data, 4);
533 //----------------------------------------------------------------------
534 // Extract "count" uint32_t values from the binary data and update
535 // the offset pointed to by "offset_ptr". The extracted data is
536 // copied into "dst".
538 // RETURNS the non-NULL buffer pointer upon successful extraction of
539 // all the requested bytes, or NULL when the data is not available
540 // in the buffer due to being out of bounds, or insufficient data.
541 //----------------------------------------------------------------------
543 DataExtractor::GetU32 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
545 const size_t src_size = sizeof(uint32_t) * count;
546 const uint32_t *src = (const uint32_t *)GetData (offset_ptr, src_size);
549 if (m_byte_order != lldb::endian::InlHostByteOrder())
551 uint32_t *dst_pos = (uint32_t *)void_dst;
552 uint32_t *dst_end = dst_pos + count;
553 const uint32_t *src_pos = src;
554 while (dst_pos < dst_end)
556 *dst_pos = ReadSwapInt32 (src_pos);
563 memcpy (void_dst, src, src_size);
565 // Return a non-NULL pointer to the converted data as an indicator of success
571 //----------------------------------------------------------------------
572 // Extract a single uint64_t from the data and update the offset
573 // pointed to by "offset_ptr".
575 // RETURNS the uint64_t that was extracted, or zero on failure.
576 //----------------------------------------------------------------------
578 DataExtractor::GetU64 (offset_t *offset_ptr) const
581 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
584 if (m_byte_order != lldb::endian::InlHostByteOrder())
586 val = ReadSwapInt64 (data);
590 memcpy (&val, data, 8);
596 //----------------------------------------------------------------------
599 // Get multiple consecutive 64 bit values. Return true if the entire
600 // read succeeds and increment the offset pointed to by offset_ptr, else
601 // return false and leave the offset pointed to by offset_ptr unchanged.
602 //----------------------------------------------------------------------
604 DataExtractor::GetU64 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
606 const size_t src_size = sizeof(uint64_t) * count;
607 const uint64_t *src = (const uint64_t *)GetData (offset_ptr, src_size);
610 if (m_byte_order != lldb::endian::InlHostByteOrder())
612 uint64_t *dst_pos = (uint64_t *)void_dst;
613 uint64_t *dst_end = dst_pos + count;
614 const uint64_t *src_pos = src;
615 while (dst_pos < dst_end)
617 *dst_pos = ReadSwapInt64 (src_pos);
624 memcpy (void_dst, src, src_size);
626 // Return a non-NULL pointer to the converted data as an indicator of success
632 //----------------------------------------------------------------------
633 // Extract a single integer value from the data and update the offset
634 // pointed to by "offset_ptr". The size of the extracted integer
635 // is specified by the "byte_size" argument. "byte_size" should have
636 // a value between 1 and 4 since the return value is only 32 bits
637 // wide. Any "byte_size" values less than 1 or greater than 4 will
638 // result in nothing being extracted, and zero being returned.
640 // RETURNS the integer value that was extracted, or zero on failure.
641 //----------------------------------------------------------------------
643 DataExtractor::GetMaxU32 (offset_t *offset_ptr, size_t byte_size) const
647 case 1: return GetU8 (offset_ptr); break;
648 case 2: return GetU16(offset_ptr); break;
649 case 4: return GetU32(offset_ptr); break;
651 assert("GetMaxU32 unhandled case!" == NULL);
657 //----------------------------------------------------------------------
658 // Extract a single integer value from the data and update the offset
659 // pointed to by "offset_ptr". The size of the extracted integer
660 // is specified by the "byte_size" argument. "byte_size" should have
661 // a value >= 1 and <= 8 since the return value is only 64 bits
662 // wide. Any "byte_size" values less than 1 or greater than 8 will
663 // result in nothing being extracted, and zero being returned.
665 // RETURNS the integer value that was extracted, or zero on failure.
666 //----------------------------------------------------------------------
668 DataExtractor::GetMaxU64 (offset_t *offset_ptr, size_t size) const
672 case 1: return GetU8 (offset_ptr); break;
673 case 2: return GetU16(offset_ptr); break;
674 case 4: return GetU32(offset_ptr); break;
675 case 8: return GetU64(offset_ptr); break;
677 assert("GetMax64 unhandled case!" == NULL);
684 DataExtractor::GetMaxU64_unchecked (offset_t *offset_ptr, size_t size) const
688 case 1: return GetU8_unchecked (offset_ptr); break;
689 case 2: return GetU16_unchecked (offset_ptr); break;
690 case 4: return GetU32_unchecked (offset_ptr); break;
691 case 8: return GetU64_unchecked (offset_ptr); break;
693 assert("GetMax64 unhandled case!" == NULL);
700 DataExtractor::GetMaxS64 (offset_t *offset_ptr, size_t size) const
704 case 1: return (int8_t)GetU8 (offset_ptr); break;
705 case 2: return (int16_t)GetU16(offset_ptr); break;
706 case 4: return (int32_t)GetU32(offset_ptr); break;
707 case 8: return (int64_t)GetU64(offset_ptr); break;
709 assert("GetMax64 unhandled case!" == NULL);
716 DataExtractor::GetMaxU64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
718 uint64_t uval64 = GetMaxU64 (offset_ptr, size);
719 if (bitfield_bit_size > 0)
721 if (bitfield_bit_offset > 0)
722 uval64 >>= bitfield_bit_offset;
723 uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
724 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
726 uval64 &= bitfield_mask;
732 DataExtractor::GetMaxS64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
734 int64_t sval64 = GetMaxS64 (offset_ptr, size);
735 if (bitfield_bit_size > 0)
737 if (bitfield_bit_offset > 0)
738 sval64 >>= bitfield_bit_offset;
739 uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
740 sval64 &= bitfield_mask;
741 // sign extend if needed
742 if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
743 sval64 |= ~bitfield_mask;
750 DataExtractor::GetFloat (offset_t *offset_ptr) const
752 typedef float float_type;
753 float_type val = 0.0;
754 const size_t src_size = sizeof(float_type);
755 const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
758 if (m_byte_order != lldb::endian::InlHostByteOrder())
760 const uint8_t *src_data = (const uint8_t *)src;
761 uint8_t *dst_data = (uint8_t *)&val;
762 for (size_t i=0; i<sizeof(float_type); ++i)
763 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
774 DataExtractor::GetDouble (offset_t *offset_ptr) const
776 typedef double float_type;
777 float_type val = 0.0;
778 const size_t src_size = sizeof(float_type);
779 const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
782 if (m_byte_order != lldb::endian::InlHostByteOrder())
784 const uint8_t *src_data = (const uint8_t *)src;
785 uint8_t *dst_data = (uint8_t *)&val;
786 for (size_t i=0; i<sizeof(float_type); ++i)
787 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
799 DataExtractor::GetLongDouble (offset_t *offset_ptr) const
801 long double val = 0.0;
802 #if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
803 *offset_ptr += CopyByteOrderedData (*offset_ptr, 10, &val, sizeof(val), lldb::endian::InlHostByteOrder());
805 *offset_ptr += CopyByteOrderedData (*offset_ptr, sizeof(val), &val, sizeof(val), lldb::endian::InlHostByteOrder());
811 //------------------------------------------------------------------
812 // Extract a single address from the data and update the offset
813 // pointed to by "offset_ptr". The size of the extracted address
814 // comes from the "this->m_addr_size" member variable and should be
815 // set correctly prior to extracting any address values.
817 // RETURNS the address that was extracted, or zero on failure.
818 //------------------------------------------------------------------
820 DataExtractor::GetAddress (offset_t *offset_ptr) const
822 return GetMaxU64 (offset_ptr, m_addr_size);
826 DataExtractor::GetAddress_unchecked (offset_t *offset_ptr) const
828 return GetMaxU64_unchecked (offset_ptr, m_addr_size);
831 //------------------------------------------------------------------
832 // Extract a single pointer from the data and update the offset
833 // pointed to by "offset_ptr". The size of the extracted pointer
834 // comes from the "this->m_addr_size" member variable and should be
835 // set correctly prior to extracting any pointer values.
837 // RETURNS the pointer that was extracted, or zero on failure.
838 //------------------------------------------------------------------
840 DataExtractor::GetPointer (offset_t *offset_ptr) const
842 return GetMaxU64 (offset_ptr, m_addr_size);
845 //----------------------------------------------------------------------
848 // Used for calls when the value type is specified by a DWARF EH Frame
850 //----------------------------------------------------------------------
853 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
855 if (eh_ptr_enc == DW_EH_PE_omit)
856 return ULLONG_MAX; // Value isn't in the buffer...
858 uint64_t baseAddress = 0;
859 uint64_t addressValue = 0;
860 const uint32_t addr_size = GetAddressByteSize();
862 bool signExtendValue = false;
863 // Decode the base part or adjust our offset
864 switch (eh_ptr_enc & 0x70)
867 signExtendValue = true;
868 baseAddress = *offset_ptr;
869 if (pc_rel_addr != LLDB_INVALID_ADDRESS)
870 baseAddress += pc_rel_addr;
872 // Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address.");
875 case DW_EH_PE_textrel:
876 signExtendValue = true;
877 if (text_addr != LLDB_INVALID_ADDRESS)
878 baseAddress = text_addr;
880 // Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero.");
883 case DW_EH_PE_datarel:
884 signExtendValue = true;
885 if (data_addr != LLDB_INVALID_ADDRESS)
886 baseAddress = data_addr;
888 // Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero.");
891 case DW_EH_PE_funcrel:
892 signExtendValue = true;
895 case DW_EH_PE_aligned:
897 // SetPointerSize should be called prior to extracting these so the
898 // pointer size is cached
899 assert(addr_size != 0);
902 // Align to a address size boundary first
903 uint32_t alignOffset = *offset_ptr % addr_size;
905 offset_ptr += addr_size - alignOffset;
914 // Decode the value part
915 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING)
917 case DW_EH_PE_absptr :
919 addressValue = GetAddress (offset_ptr);
921 // addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue);
924 case DW_EH_PE_uleb128 : addressValue = GetULEB128(offset_ptr); break;
925 case DW_EH_PE_udata2 : addressValue = GetU16(offset_ptr); break;
926 case DW_EH_PE_udata4 : addressValue = GetU32(offset_ptr); break;
927 case DW_EH_PE_udata8 : addressValue = GetU64(offset_ptr); break;
928 case DW_EH_PE_sleb128 : addressValue = GetSLEB128(offset_ptr); break;
929 case DW_EH_PE_sdata2 : addressValue = (int16_t)GetU16(offset_ptr); break;
930 case DW_EH_PE_sdata4 : addressValue = (int32_t)GetU32(offset_ptr); break;
931 case DW_EH_PE_sdata8 : addressValue = (int64_t)GetU64(offset_ptr); break;
933 // Unhandled encoding type
938 // Since we promote everything to 64 bit, we may need to sign extend
939 if (signExtendValue && addr_size < sizeof(baseAddress))
941 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
942 if (sign_bit & addressValue)
944 uint64_t mask = ~sign_bit + 1;
945 addressValue |= mask;
948 return baseAddress + addressValue;
952 DataExtractor::ExtractBytes (offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const
954 const uint8_t *src = PeekData (offset, length);
957 if (dst_byte_order != GetByteOrder())
959 // Validate that only a word- or register-sized dst is byte swapped
960 assert (length == 1 || length == 2 || length == 4 || length == 8 ||
961 length == 10 || length == 16 || length == 32);
963 for (uint32_t i=0; i<length; ++i)
964 ((uint8_t*)dst)[i] = src[length - i - 1];
967 ::memcpy (dst, src, length);
973 // Extract data as it exists in target memory
975 DataExtractor::CopyData (offset_t offset,
979 const uint8_t *src = PeekData (offset, length);
982 ::memcpy (dst, src, length);
988 // Extract data and swap if needed when doing the copy
990 DataExtractor::CopyByteOrderedData (offset_t src_offset,
994 ByteOrder dst_byte_order) const
996 // Validate the source info
997 if (!ValidOffsetForDataOfSize(src_offset, src_len))
998 assert (ValidOffsetForDataOfSize(src_offset, src_len));
999 assert (src_len > 0);
1000 assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
1002 // Validate the destination info
1003 assert (dst_void_ptr != NULL);
1004 assert (dst_len > 0);
1005 assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
1007 // Validate that only a word- or register-sized dst is byte swapped
1008 assert (dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
1009 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
1012 // Must have valid byte orders set in this object and for destination
1013 if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) ||
1014 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
1018 uint8_t* dst = (uint8_t*)dst_void_ptr;
1019 const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len);
1022 if (dst_len >= src_len)
1024 // We are copying the entire value from src into dst.
1025 // Calculate how many, if any, zeroes we need for the most
1026 // significant bytes if "dst_len" is greater than "src_len"...
1027 const size_t num_zeroes = dst_len - src_len;
1028 if (dst_byte_order == eByteOrderBig)
1030 // Big endian, so we lead with zeroes...
1032 ::memset (dst, 0, num_zeroes);
1033 // Then either copy or swap the rest
1034 if (m_byte_order == eByteOrderBig)
1036 ::memcpy (dst + num_zeroes, src, src_len);
1040 for (i=0; i<src_len; ++i)
1041 dst[i+num_zeroes] = src[src_len - 1 - i];
1046 // Little endian destination, so we lead the value bytes
1047 if (m_byte_order == eByteOrderBig)
1049 for (i=0; i<src_len; ++i)
1050 dst[i] = src[src_len - 1 - i];
1054 ::memcpy (dst, src, src_len);
1056 // And zero the rest...
1058 ::memset (dst + src_len, 0, num_zeroes);
1064 // We are only copying some of the value from src into dst..
1066 if (dst_byte_order == eByteOrderBig)
1069 if (m_byte_order == eByteOrderBig)
1071 // Big endian dst, with big endian src
1072 ::memcpy (dst, src + (src_len - dst_len), dst_len);
1076 // Big endian dst, with little endian src
1077 for (i=0; i<dst_len; ++i)
1078 dst[i] = src[dst_len - 1 - i];
1083 // Little endian dst
1084 if (m_byte_order == eByteOrderBig)
1086 // Little endian dst, with big endian src
1087 for (i=0; i<dst_len; ++i)
1088 dst[i] = src[src_len - 1 - i];
1092 // Little endian dst, with big endian src
1093 ::memcpy (dst, src, dst_len);
1104 //----------------------------------------------------------------------
1105 // Extracts a variable length NULL terminated C string from
1106 // the data at the offset pointed to by "offset_ptr". The
1107 // "offset_ptr" will be updated with the offset of the byte that
1108 // follows the NULL terminator byte.
1110 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1111 // "length" is non-zero and there aren't enough available
1112 // bytes, NULL will be returned and "offset_ptr" will not be
1114 //----------------------------------------------------------------------
1116 DataExtractor::GetCStr (offset_t *offset_ptr) const
1118 const char *cstr = (const char *)PeekData (*offset_ptr, 1);
1121 const char *cstr_end = cstr;
1122 const char *end = (const char *)m_end;
1123 while (cstr_end < end && *cstr_end)
1126 // Now we are either at the end of the data or we point to the
1127 // NULL C string terminator with cstr_end...
1128 if (*cstr_end == '\0')
1130 // Advance the offset with one extra byte for the NULL terminator
1131 *offset_ptr += (cstr_end - cstr + 1);
1135 // We reached the end of the data without finding a NULL C string
1136 // terminator. Fall through and return NULL otherwise anyone that
1137 // would have used the result as a C string can wander into
1138 // unknown memory...
1143 //----------------------------------------------------------------------
1144 // Extracts a NULL terminated C string from the fixed length field of
1145 // length "len" at the offset pointed to by "offset_ptr".
1146 // The "offset_ptr" will be updated with the offset of the byte that
1147 // follows the fixed length field.
1149 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1150 // the offset plus the length of the field is out of bounds, or if the
1151 // field does not contain a NULL terminator byte, NULL will be returned
1152 // and "offset_ptr" will not be updated.
1153 //----------------------------------------------------------------------
1155 DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const
1157 const char *cstr = (const char *)PeekData (*offset_ptr, len);
1160 if (memchr (cstr, '\0', len) == NULL)
1170 //------------------------------------------------------------------
1171 // Peeks at a string in the contained data. No verification is done
1172 // to make sure the entire string lies within the bounds of this
1173 // object's data, only "offset" is verified to be a valid offset.
1175 // Returns a valid C string pointer if "offset" is a valid offset in
1176 // this object's data, else NULL is returned.
1177 //------------------------------------------------------------------
1179 DataExtractor::PeekCStr (offset_t offset) const
1181 return (const char *)PeekData (offset, 1);
1184 //----------------------------------------------------------------------
1185 // Extracts an unsigned LEB128 number from this object's data
1186 // starting at the offset pointed to by "offset_ptr". The offset
1187 // pointed to by "offset_ptr" will be updated with the offset of the
1188 // byte following the last extracted byte.
1190 // Returned the extracted integer value.
1191 //----------------------------------------------------------------------
1193 DataExtractor::GetULEB128 (offset_t *offset_ptr) const
1195 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1199 const uint8_t *end = m_end;
1203 uint64_t result = *src++;
1210 uint8_t byte = *src++;
1211 result |= (byte & 0x7f) << shift;
1212 if ((byte & 0x80) == 0)
1217 *offset_ptr = src - m_start;
1224 //----------------------------------------------------------------------
1225 // Extracts an signed LEB128 number from this object's data
1226 // starting at the offset pointed to by "offset_ptr". The offset
1227 // pointed to by "offset_ptr" will be updated with the offset of the
1228 // byte following the last extracted byte.
1230 // Returned the extracted integer value.
1231 //----------------------------------------------------------------------
1233 DataExtractor::GetSLEB128 (offset_t *offset_ptr) const
1235 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1239 const uint8_t *end = m_end;
1245 int size = sizeof (int64_t) * 8;
1254 result |= (byte & 0x7f) << shift;
1256 if ((byte & 0x80) == 0)
1260 // Sign bit of byte is 2nd high order bit (0x40)
1261 if (shift < size && (byte & 0x40))
1262 result |= - (1 << shift);
1264 *offset_ptr += bytecount;
1270 //----------------------------------------------------------------------
1271 // Skips a ULEB128 number (signed or unsigned) from this object's
1272 // data starting at the offset pointed to by "offset_ptr". The
1273 // offset pointed to by "offset_ptr" will be updated with the offset
1274 // of the byte following the last extracted byte.
1276 // Returns the number of bytes consumed during the extraction.
1277 //----------------------------------------------------------------------
1279 DataExtractor::Skip_LEB128 (offset_t *offset_ptr) const
1281 uint32_t bytes_consumed = 0;
1282 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1286 const uint8_t *end = m_end;
1290 const uint8_t *src_pos = src;
1291 while ((src_pos < end) && (*src_pos++ & 0x80))
1293 *offset_ptr += src_pos - src;
1295 return bytes_consumed;
1299 GetAPInt (const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size, llvm::APInt &result)
1301 llvm::SmallVector<uint64_t, 2> uint64_array;
1302 lldb::offset_t bytes_left = byte_size;
1304 const lldb::ByteOrder byte_order = data.GetByteOrder();
1305 if (byte_order == lldb::eByteOrderLittle)
1307 while (bytes_left > 0)
1309 if (bytes_left >= 8)
1311 u64 = data.GetU64(offset_ptr);
1316 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
1319 uint64_array.push_back(u64);
1321 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1324 else if (byte_order == lldb::eByteOrderBig)
1326 lldb::offset_t be_offset = *offset_ptr + byte_size;
1327 lldb::offset_t temp_offset;
1328 while (bytes_left > 0)
1330 if (bytes_left >= 8)
1333 temp_offset = be_offset;
1334 u64 = data.GetU64(&temp_offset);
1339 be_offset -= bytes_left;
1340 temp_offset = be_offset;
1341 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
1344 uint64_array.push_back(u64);
1346 *offset_ptr += byte_size;
1347 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1353 static lldb::offset_t
1354 DumpAPInt (Stream *s, const DataExtractor &data, lldb::offset_t offset, lldb::offset_t byte_size, bool is_signed, unsigned radix)
1357 if (GetAPInt (data, &offset, byte_size, apint))
1359 std::string apint_str(apint.toString(radix, is_signed));
1371 s->Write(apint_str.c_str(), apint_str.size());
1376 static float half2float (uint16_t half)
1379 llvm_unreachable("half2float not implemented for MSVC");
1381 union{ float f; uint32_t u;}u;
1382 int32_t v = (int16_t) half;
1384 if( 0 == (v & 0x7c00))
1386 u.u = v & 0x80007FFFU;
1387 return u.f * ldexpf(1, 125);
1391 u.u = v | 0x70000000U;
1392 return u.f * ldexpf(1, -112);
1397 DataExtractor::Dump (Stream *s,
1398 offset_t start_offset,
1399 lldb::Format item_format,
1400 size_t item_byte_size,
1402 size_t num_per_line,
1404 uint32_t item_bit_size, // If zero, this is not a bitfield value, if non-zero, the value is a bitfield
1405 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the shift amount to apply to a bitfield
1406 ExecutionContextScope *exe_scope) const
1409 return start_offset;
1411 if (item_format == eFormatPointer)
1413 if (item_byte_size != 4 && item_byte_size != 8)
1414 item_byte_size = s->GetAddressByteSize();
1417 offset_t offset = start_offset;
1419 if (item_format == eFormatInstruction)
1423 target_sp = exe_scope->CalculateTarget();
1426 DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL, NULL));
1427 if (disassembler_sp)
1429 lldb::addr_t addr = base_addr + start_offset;
1430 lldb_private::Address so_addr;
1431 bool data_from_file = true;
1432 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1434 data_from_file = false;
1438 if (target_sp->GetSectionLoadList().IsEmpty() || !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
1439 so_addr.SetRawAddress(addr);
1442 size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false, data_from_file);
1446 offset += bytes_consumed;
1447 const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
1448 const bool show_bytes = true;
1449 ExecutionContext exe_ctx;
1450 exe_scope->CalculateExecutionContext(exe_ctx);
1451 disassembler_sp->GetInstructionList().Dump (s, show_address, show_bytes, &exe_ctx);
1453 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
1454 // I'll fix that but for now, just clear the list and it will go away nicely.
1455 disassembler_sp->GetInstructionList().Clear();
1460 s->Printf ("invalid target");
1465 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8)
1466 item_format = eFormatHex;
1468 lldb::offset_t line_start_offset = start_offset;
1469 for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count)
1471 if ((count % num_per_line) == 0)
1475 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1477 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
1478 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0);
1482 if (base_addr != LLDB_INVALID_ADDRESS)
1483 s->Printf ("0x%8.8" PRIx64 ": ", (uint64_t)(base_addr + (offset - start_offset)));
1484 line_start_offset = offset;
1487 if (item_format != eFormatChar &&
1488 item_format != eFormatCharPrintable &&
1489 item_format != eFormatCharArray &&
1496 switch (item_format)
1498 case eFormatBoolean:
1499 if (item_byte_size <= 8)
1500 s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false");
1503 s->Printf("error: unsupported byte size (%" PRIu64 ") for boolean format", (uint64_t)item_byte_size);
1509 if (item_byte_size <= 8)
1511 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1512 // Avoid std::bitset<64>::to_string() since it is missing in
1513 // earlier C++ libraries
1514 std::string binary_value(64, '0');
1515 std::bitset<64> bits(uval64);
1516 for (i = 0; i < 64; ++i)
1518 binary_value[64 - 1 - i] = '1';
1519 if (item_bit_size > 0)
1520 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
1521 else if (item_byte_size > 0 && item_byte_size <= 8)
1522 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
1526 const bool is_signed = false;
1527 const unsigned radix = 2;
1528 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1533 case eFormatBytesWithASCII:
1534 for (i=0; i<item_byte_size; ++i)
1536 s->Printf ("%2.2x", GetU8(&offset));
1538 // Put an extra space between the groups of bytes if more than one
1539 // is being dumped in a group (item_byte_size is more than 1).
1540 if (item_byte_size > 1)
1545 case eFormatCharPrintable:
1546 case eFormatCharArray:
1548 // If we are only printing one character surround it with single
1550 if (item_count == 1 && item_format == eFormatChar)
1553 const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1555 s->Printf ("%c", (char)ch);
1556 else if (item_format != eFormatCharPrintable)
1560 case '\033': s->Printf ("\\e"); break;
1561 case '\a': s->Printf ("\\a"); break;
1562 case '\b': s->Printf ("\\b"); break;
1563 case '\f': s->Printf ("\\f"); break;
1564 case '\n': s->Printf ("\\n"); break;
1565 case '\r': s->Printf ("\\r"); break;
1566 case '\t': s->Printf ("\\t"); break;
1567 case '\v': s->Printf ("\\v"); break;
1568 case '\0': s->Printf ("\\0"); break;
1570 if (item_byte_size == 1)
1571 s->Printf ("\\x%2.2x", (uint8_t)ch);
1573 s->Printf ("%" PRIu64, ch);
1579 s->PutChar(NON_PRINTABLE_CHAR);
1582 // If we are only printing one character surround it with single quotes
1583 if (item_count == 1 && item_format == eFormatChar)
1588 case eFormatEnum: // Print enum value as a signed integer when we don't get the enum type
1589 case eFormatDecimal:
1590 if (item_byte_size <= 8)
1591 s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1594 const bool is_signed = true;
1595 const unsigned radix = 10;
1596 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1600 case eFormatUnsigned:
1601 if (item_byte_size <= 8)
1602 s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1605 const bool is_signed = false;
1606 const unsigned radix = 10;
1607 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1612 if (item_byte_size <= 8)
1613 s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1616 const bool is_signed = false;
1617 const unsigned radix = 8;
1618 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1624 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1626 for (i=0; i<item_byte_size; ++i)
1628 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
1630 s->Printf ("%c", ch);
1635 case '\033': s->Printf ("\\e"); break;
1636 case '\a': s->Printf ("\\a"); break;
1637 case '\b': s->Printf ("\\b"); break;
1638 case '\f': s->Printf ("\\f"); break;
1639 case '\n': s->Printf ("\\n"); break;
1640 case '\r': s->Printf ("\\r"); break;
1641 case '\t': s->Printf ("\\t"); break;
1642 case '\v': s->Printf ("\\v"); break;
1643 case '\0': s->Printf ("\\0"); break;
1644 default: s->Printf ("\\x%2.2x", ch); break;
1652 case eFormatCString:
1654 const char *cstr = GetCStr(&offset);
1659 offset = LLDB_INVALID_OFFSET;
1665 while (const char c = *cstr)
1675 case '\033': s->Printf ("\\e"); break;
1676 case '\a': s->Printf ("\\a"); break;
1677 case '\b': s->Printf ("\\b"); break;
1678 case '\f': s->Printf ("\\f"); break;
1679 case '\n': s->Printf ("\\n"); break;
1680 case '\r': s->Printf ("\\r"); break;
1681 case '\t': s->Printf ("\\t"); break;
1682 case '\v': s->Printf ("\\v"); break;
1683 default: s->Printf ("\\x%2.2x", c); break;
1696 case eFormatPointer:
1697 s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t));
1701 case eFormatComplexInteger:
1703 size_t complex_int_byte_size = item_byte_size / 2;
1705 if (complex_int_byte_size <= 8)
1707 s->Printf("%" PRIu64, GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1708 s->Printf(" + %" PRIu64 "i", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1712 s->Printf("error: unsupported byte size (%" PRIu64 ") for complex integer format", (uint64_t)item_byte_size);
1718 case eFormatComplex:
1719 if (sizeof(float) * 2 == item_byte_size)
1721 float f32_1 = GetFloat (&offset);
1722 float f32_2 = GetFloat (&offset);
1724 s->Printf ("%g + %gi", f32_1, f32_2);
1727 else if (sizeof(double) * 2 == item_byte_size)
1729 double d64_1 = GetDouble (&offset);
1730 double d64_2 = GetDouble (&offset);
1732 s->Printf ("%lg + %lgi", d64_1, d64_2);
1735 else if (sizeof(long double) * 2 == item_byte_size)
1737 long double ld64_1 = GetLongDouble (&offset);
1738 long double ld64_2 = GetLongDouble (&offset);
1739 s->Printf ("%Lg + %Lgi", ld64_1, ld64_2);
1744 s->Printf("error: unsupported byte size (%" PRIu64 ") for complex float format", (uint64_t)item_byte_size);
1750 case eFormatDefault:
1752 case eFormatHexUppercase:
1754 bool wantsuppercase = (item_format == eFormatHexUppercase);
1755 switch (item_byte_size)
1761 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));
1765 assert (item_bit_size == 0 && item_bit_offset == 0);
1766 const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size);
1769 s->PutCString("0x");
1771 if (m_byte_order == eByteOrderBig)
1773 for (idx = 0; idx < item_byte_size; ++idx)
1774 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
1778 for (idx = 0; idx < item_byte_size; ++idx)
1779 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]);
1791 bool used_apfloat = false;
1793 target_sp = exe_scope->CalculateTarget();
1796 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1799 clang::ASTContext *ast = clang_ast->getASTContext();
1802 llvm::SmallVector<char, 256> sv;
1803 // Show full precision when printing float values
1804 const unsigned format_precision = 0;
1805 const unsigned format_max_padding = 100;
1806 size_t item_bit_size = item_byte_size * 8;
1808 if (item_bit_size == ast->getTypeSize(ast->FloatTy))
1810 llvm::APInt apint(item_bit_size, this->GetMaxU64(&offset, item_byte_size));
1811 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->FloatTy), apint);
1812 apfloat.toString(sv, format_precision, format_max_padding);
1814 else if (item_bit_size == ast->getTypeSize(ast->DoubleTy))
1817 if (GetAPInt (*this, &offset, item_byte_size, apint))
1819 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->DoubleTy), apint);
1820 apfloat.toString(sv, format_precision, format_max_padding);
1823 else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy))
1826 switch (target_sp->GetArchitecture().GetMachine())
1828 case llvm::Triple::x86:
1829 case llvm::Triple::x86_64:
1830 // clang will assert when constructing the apfloat if we use a 16 byte integer value
1831 if (GetAPInt (*this, &offset, 10, apint))
1833 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1834 apfloat.toString(sv, format_precision, format_max_padding);
1839 if (GetAPInt (*this, &offset, item_byte_size, apint))
1841 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1842 apfloat.toString(sv, format_precision, format_max_padding);
1847 else if (item_bit_size == ast->getTypeSize(ast->HalfTy))
1849 llvm::APInt apint(item_bit_size, this->GetU16(&offset));
1850 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->HalfTy), apint);
1851 apfloat.toString(sv, format_precision, format_max_padding);
1856 s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
1857 used_apfloat = true;
1865 std::ostringstream ss;
1866 if (item_byte_size == sizeof(float) || item_byte_size == 2)
1869 if (item_byte_size == 2)
1871 uint16_t half = this->GetU16(&offset);
1872 f = half2float(half);
1876 f = GetFloat (&offset);
1878 ss.precision(std::numeric_limits<float>::digits10);
1881 else if (item_byte_size == sizeof(double))
1883 ss.precision(std::numeric_limits<double>::digits10);
1884 ss << GetDouble(&offset);
1886 else if (item_byte_size == sizeof(long double) || item_byte_size == 10)
1888 ss.precision(std::numeric_limits<long double>::digits10);
1889 ss << GetLongDouble(&offset);
1893 s->Printf("error: unsupported byte size (%" PRIu64 ") for float format", (uint64_t)item_byte_size);
1897 s->Printf("%s", ss.str().c_str());
1902 case eFormatUnicode16:
1903 s->Printf("U+%4.4x", GetU16 (&offset));
1906 case eFormatUnicode32:
1907 s->Printf("U+0x%8.8x", GetU32 (&offset));
1910 case eFormatAddressInfo:
1912 addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1913 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), addr);
1916 TargetSP target_sp (exe_scope->CalculateTarget());
1917 lldb_private::Address so_addr;
1920 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1925 Address::DumpStyleResolvedDescription,
1926 Address::DumpStyleModuleWithFileAddress);
1930 so_addr.SetOffset(addr);
1931 so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription);
1938 case eFormatHexFloat:
1939 if (sizeof(float) == item_byte_size)
1941 char float_cstr[256];
1942 llvm::APFloat ap_float (GetFloat (&offset));
1943 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1944 s->Printf ("%s", float_cstr);
1947 else if (sizeof(double) == item_byte_size)
1949 char float_cstr[256];
1950 llvm::APFloat ap_float (GetDouble (&offset));
1951 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1952 s->Printf ("%s", float_cstr);
1957 s->Printf("error: unsupported byte size (%" PRIu64 ") for hex float format", (uint64_t)item_byte_size);
1962 // please keep the single-item formats below in sync with FormatManager::GetSingleItemFormat
1963 // if you fail to do so, users will start getting different outputs depending on internal
1964 // implementation details they should not care about ||
1965 case eFormatVectorOfChar: // ||
1966 s->PutChar('{'); // \/
1967 offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1971 case eFormatVectorOfSInt8:
1973 offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1977 case eFormatVectorOfUInt8:
1979 offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1983 case eFormatVectorOfSInt16:
1985 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);
1989 case eFormatVectorOfUInt16:
1991 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);
1995 case eFormatVectorOfSInt32:
1997 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);
2001 case eFormatVectorOfUInt32:
2003 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);
2007 case eFormatVectorOfSInt64:
2009 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);
2013 case eFormatVectorOfUInt64:
2015 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);
2019 case eFormatVectorOfFloat32:
2021 offset = Dump (s, offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
2025 case eFormatVectorOfFloat64:
2027 offset = Dump (s, offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
2031 case eFormatVectorOfUInt128:
2033 offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
2039 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
2041 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
2042 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0);
2044 return offset; // Return the offset at which we ended up
2047 //----------------------------------------------------------------------
2048 // Dumps bytes from this object's data to the stream "s" starting
2049 // "start_offset" bytes into this data, and ending with the byte
2050 // before "end_offset". "base_addr" will be added to the offset
2051 // into the dumped data when showing the offset into the data in the
2052 // output information. "num_per_line" objects of type "type" will
2053 // be dumped with the option to override the format for each object
2054 // with "type_format". "type_format" is a printf style formatting
2055 // string. If "type_format" is NULL, then an appropriate format
2056 // string will be used for the supplied "type". If the stream "s"
2057 // is NULL, then the output will be send to Log().
2058 //----------------------------------------------------------------------
2060 DataExtractor::PutToLog
2063 offset_t start_offset,
2066 uint32_t num_per_line,
2067 DataExtractor::Type type,
2072 return start_offset;
2075 offset_t end_offset;
2078 for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count)
2080 if ((count % num_per_line) == 0)
2082 // Print out any previous string
2083 if (sstr.GetSize() > 0)
2085 log->Printf("%s", sstr.GetData());
2088 // Reset string offset and fill the current line string with address:
2089 if (base_addr != LLDB_INVALID_ADDRESS)
2090 sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset)));
2095 case TypeUInt8: sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break;
2098 char ch = GetU8(&offset);
2099 sstr.Printf (format ? format : " %c", isprint(ch) ? ch : ' ');
2102 case TypeUInt16: sstr.Printf (format ? format : " %4.4x", GetU16(&offset)); break;
2103 case TypeUInt32: sstr.Printf (format ? format : " %8.8x", GetU32(&offset)); break;
2104 case TypeUInt64: sstr.Printf (format ? format : " %16.16" PRIx64, GetU64(&offset)); break;
2105 case TypePointer: sstr.Printf (format ? format : " 0x%" PRIx64, GetAddress(&offset)); break;
2106 case TypeULEB128: sstr.Printf (format ? format : " 0x%" PRIx64, GetULEB128(&offset)); break;
2107 case TypeSLEB128: sstr.Printf (format ? format : " %" PRId64, GetSLEB128(&offset)); break;
2111 if (sstr.GetSize() > 0)
2112 log->Printf("%s", sstr.GetData());
2114 return offset; // Return the offset at which we ended up
2117 //----------------------------------------------------------------------
2120 // Dump out a UUID starting at 'offset' bytes into the buffer
2121 //----------------------------------------------------------------------
2123 DataExtractor::DumpUUID (Stream *s, offset_t offset) const
2127 const uint8_t *uuid_data = PeekData(offset, 16);
2130 lldb_private::UUID uuid(uuid_data, 16);
2135 s->Printf("<not enough data for UUID at offset 0x%8.8" PRIx64 ">", offset);
2141 DataExtractor::DumpHexBytes (Stream *s,
2144 uint32_t bytes_per_line,
2147 DataExtractor data (src, src_len, eByteOrderLittle, 4);
2149 0, // Offset into "src"
2150 eFormatBytes, // Dump as hex bytes
2151 1, // Size of each item is 1 for single bytes
2152 src_len, // Number of bytes
2153 bytes_per_line, // Num bytes per line
2154 base_addr, // Base address
2155 0, 0); // Bitfield info
2159 DataExtractor::Copy (DataExtractor &dest_data) const
2161 if (m_data_sp.get())
2163 // we can pass along the SP to the data
2164 dest_data.SetData(m_data_sp);
2168 const uint8_t *base_ptr = m_start;
2169 size_t data_size = GetByteSize();
2170 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
2172 return GetByteSize();
2176 DataExtractor::Append(DataExtractor& rhs)
2178 if (rhs.GetByteOrder() != GetByteOrder())
2181 if (rhs.GetByteSize() == 0)
2184 if (GetByteSize() == 0)
2185 return (rhs.Copy(*this) > 0);
2187 size_t bytes = GetByteSize() + rhs.GetByteSize();
2189 DataBufferHeap *buffer_heap_ptr = NULL;
2190 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2192 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2195 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2197 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2198 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
2206 DataExtractor::Append(void* buf, offset_t length)
2214 size_t bytes = GetByteSize() + length;
2216 DataBufferHeap *buffer_heap_ptr = NULL;
2217 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2219 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2222 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2224 if (GetByteSize() > 0)
2225 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2227 memcpy(bytes_ptr + GetByteSize(), buf, length);