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"
25 #include "llvm/Support/MD5.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()),
136 m_target_byte_size(1)
140 //----------------------------------------------------------------------
141 // This constructor allows us to use data that is owned by someone else.
142 // The data must stay around as long as this object is valid.
143 //----------------------------------------------------------------------
144 DataExtractor::DataExtractor (const void* data, offset_t length, ByteOrder endian, uint32_t addr_size, uint32_t target_byte_size/*=1*/) :
145 m_start ((uint8_t*)data),
146 m_end ((uint8_t*)data + length),
147 m_byte_order(endian),
148 m_addr_size (addr_size),
150 m_target_byte_size(target_byte_size)
154 //----------------------------------------------------------------------
155 // Make a shared pointer reference to the shared data in "data_sp" and
156 // set the endian swapping setting to "swap", and the address size to
157 // "addr_size". The shared data reference will ensure the data lives
158 // as long as any DataExtractor objects exist that have a reference to
160 //----------------------------------------------------------------------
161 DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint32_t addr_size, uint32_t target_byte_size/*=1*/) :
164 m_byte_order(endian),
165 m_addr_size (addr_size),
167 m_target_byte_size(target_byte_size)
172 //----------------------------------------------------------------------
173 // Initialize this object with a subset of the data bytes in "data".
174 // If "data" contains shared data, then a reference to this shared
175 // data will added and the shared data will stay around as long
176 // as any object contains a reference to that data. The endian
177 // swap and address size settings are copied from "data".
178 //----------------------------------------------------------------------
179 DataExtractor::DataExtractor (const DataExtractor& data, offset_t offset, offset_t length, uint32_t target_byte_size/*=1*/) :
182 m_byte_order(data.m_byte_order),
183 m_addr_size(data.m_addr_size),
185 m_target_byte_size(target_byte_size)
187 if (data.ValidOffset(offset))
189 offset_t bytes_available = data.GetByteSize() - offset;
190 if (length > bytes_available)
191 length = bytes_available;
192 SetData(data, offset, length);
196 DataExtractor::DataExtractor (const DataExtractor& rhs) :
197 m_start (rhs.m_start),
199 m_byte_order (rhs.m_byte_order),
200 m_addr_size (rhs.m_addr_size),
201 m_data_sp (rhs.m_data_sp),
202 m_target_byte_size(rhs.m_target_byte_size)
206 //----------------------------------------------------------------------
207 // Assignment operator
208 //----------------------------------------------------------------------
210 DataExtractor::operator= (const DataExtractor& rhs)
214 m_start = rhs.m_start;
216 m_byte_order = rhs.m_byte_order;
217 m_addr_size = rhs.m_addr_size;
218 m_data_sp = rhs.m_data_sp;
223 //----------------------------------------------------------------------
225 //----------------------------------------------------------------------
226 DataExtractor::~DataExtractor ()
230 //------------------------------------------------------------------
231 // Clears the object contents back to a default invalid state, and
232 // release any references to shared data that this object may
234 //------------------------------------------------------------------
236 DataExtractor::Clear ()
240 m_byte_order = lldb::endian::InlHostByteOrder();
245 //------------------------------------------------------------------
246 // If this object contains shared data, this function returns the
247 // offset into that shared data. Else zero is returned.
248 //------------------------------------------------------------------
250 DataExtractor::GetSharedDataOffset () const
254 const DataBuffer * data = m_data_sp.get();
257 const uint8_t * data_bytes = data->GetBytes();
258 if (data_bytes != NULL)
260 assert(m_start >= data_bytes);
261 return m_start - data_bytes;
268 //----------------------------------------------------------------------
269 // Set the data with which this object will extract from to data
270 // starting at BYTES and set the length of the data to LENGTH bytes
271 // long. The data is externally owned must be around at least as
272 // long as this object points to the data. No copy of the data is
273 // made, this object just refers to this data and can extract from
274 // it. If this object refers to any shared data upon entry, the
275 // reference to that data will be released. Is SWAP is set to true,
276 // any data extracted will be endian swapped.
277 //----------------------------------------------------------------------
279 DataExtractor::SetData (const void *bytes, offset_t length, ByteOrder endian)
281 m_byte_order = endian;
283 if (bytes == NULL || length == 0)
290 m_start = (uint8_t *)bytes;
291 m_end = m_start + length;
293 return GetByteSize();
296 //----------------------------------------------------------------------
297 // Assign the data for this object to be a subrange in "data"
298 // starting "data_offset" bytes into "data" and ending "data_length"
299 // bytes later. If "data_offset" is not a valid offset into "data",
300 // then this object will contain no bytes. If "data_offset" is
301 // within "data" yet "data_length" is too large, the length will be
302 // capped at the number of bytes remaining in "data". If "data"
303 // contains a shared pointer to other data, then a ref counted
304 // pointer to that data will be made in this object. If "data"
305 // doesn't contain a shared pointer to data, then the bytes referred
306 // to in "data" will need to exist at least as long as this object
307 // refers to those bytes. The address size and endian swap settings
308 // are copied from the current values in "data".
309 //----------------------------------------------------------------------
311 DataExtractor::SetData (const DataExtractor& data, offset_t data_offset, offset_t data_length)
313 m_addr_size = data.m_addr_size;
314 // If "data" contains shared pointer to data, then we can use that
315 if (data.m_data_sp.get())
317 m_byte_order = data.m_byte_order;
318 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length);
321 // We have a DataExtractor object that just has a pointer to bytes
322 if (data.ValidOffset(data_offset))
324 if (data_length > data.GetByteSize() - data_offset)
325 data_length = data.GetByteSize() - data_offset;
326 return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder());
331 //----------------------------------------------------------------------
332 // Assign the data for this object to be a subrange of the shared
333 // data in "data_sp" starting "data_offset" bytes into "data_sp"
334 // and ending "data_length" bytes later. If "data_offset" is not
335 // a valid offset into "data_sp", then this object will contain no
336 // bytes. If "data_offset" is within "data_sp" yet "data_length" is
337 // too large, the length will be capped at the number of bytes
338 // remaining in "data_sp". A ref counted pointer to the data in
339 // "data_sp" will be made in this object IF the number of bytes this
340 // object refers to in greater than zero (if at least one byte was
341 // available starting at "data_offset") to ensure the data stays
342 // around as long as it is needed. The address size and endian swap
343 // settings will remain unchanged from their current settings.
344 //----------------------------------------------------------------------
346 DataExtractor::SetData (const DataBufferSP& data_sp, offset_t data_offset, offset_t data_length)
348 m_start = m_end = NULL;
355 const size_t data_size = data_sp->GetByteSize();
356 if (data_offset < data_size)
358 m_start = data_sp->GetBytes() + data_offset;
359 const size_t bytes_left = data_size - data_offset;
360 // Cap the length of we asked for too many
361 if (data_length <= bytes_left)
362 m_end = m_start + data_length; // We got all the bytes we wanted
364 m_end = m_start + bytes_left; // Not all the bytes requested were available in the shared data
369 size_t new_size = GetByteSize();
371 // Don't hold a shared pointer to the data buffer if we don't share
372 // any valid bytes in the shared buffer.
379 //----------------------------------------------------------------------
380 // Extract a single unsigned char from the binary data and update
381 // the offset pointed to by "offset_ptr".
383 // RETURNS the byte that was extracted, or zero on failure.
384 //----------------------------------------------------------------------
386 DataExtractor::GetU8 (offset_t *offset_ptr) const
388 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, 1);
394 //----------------------------------------------------------------------
395 // Extract "count" unsigned chars from the binary data and update the
396 // offset pointed to by "offset_ptr". The extracted data is copied into
399 // RETURNS the non-NULL buffer pointer upon successful extraction of
400 // all the requested bytes, or NULL when the data is not available in
401 // the buffer due to being out of bounds, or insufficient data.
402 //----------------------------------------------------------------------
404 DataExtractor::GetU8 (offset_t *offset_ptr, void *dst, uint32_t count) const
406 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, count);
409 // Copy the data into the buffer
410 memcpy (dst, data, count);
411 // Return a non-NULL pointer to the converted data as an indicator of success
417 //----------------------------------------------------------------------
418 // Extract a single uint16_t from the data and update the offset
419 // pointed to by "offset_ptr".
421 // RETURNS the uint16_t that was extracted, or zero on failure.
422 //----------------------------------------------------------------------
424 DataExtractor::GetU16 (offset_t *offset_ptr) const
427 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
430 if (m_byte_order != lldb::endian::InlHostByteOrder())
431 val = ReadSwapInt16(data);
433 val = ReadInt16 (data);
439 DataExtractor::GetU16_unchecked (offset_t *offset_ptr) const
442 if (m_byte_order == lldb::endian::InlHostByteOrder())
443 val = ReadInt16 (m_start, *offset_ptr);
445 val = ReadSwapInt16(m_start, *offset_ptr);
446 *offset_ptr += sizeof(val);
451 DataExtractor::GetU32_unchecked (offset_t *offset_ptr) const
454 if (m_byte_order == lldb::endian::InlHostByteOrder())
455 val = ReadInt32 (m_start, *offset_ptr);
457 val = ReadSwapInt32 (m_start, *offset_ptr);
458 *offset_ptr += sizeof(val);
463 DataExtractor::GetU64_unchecked (offset_t *offset_ptr) const
466 if (m_byte_order == lldb::endian::InlHostByteOrder())
467 val = ReadInt64 (m_start, *offset_ptr);
469 val = ReadSwapInt64 (m_start, *offset_ptr);
470 *offset_ptr += sizeof(val);
475 //----------------------------------------------------------------------
476 // Extract "count" uint16_t values from the binary data and update
477 // the offset pointed to by "offset_ptr". The extracted data is
478 // copied into "dst".
480 // RETURNS the non-NULL buffer pointer upon successful extraction of
481 // all the requested bytes, or NULL when the data is not available
482 // in the buffer due to being out of bounds, or insufficient data.
483 //----------------------------------------------------------------------
485 DataExtractor::GetU16 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
487 const size_t src_size = sizeof(uint16_t) * count;
488 const uint16_t *src = (const uint16_t *)GetData (offset_ptr, src_size);
491 if (m_byte_order != lldb::endian::InlHostByteOrder())
493 uint16_t *dst_pos = (uint16_t *)void_dst;
494 uint16_t *dst_end = dst_pos + count;
495 const uint16_t *src_pos = src;
496 while (dst_pos < dst_end)
498 *dst_pos = ReadSwapInt16 (src_pos);
505 memcpy (void_dst, src, src_size);
507 // Return a non-NULL pointer to the converted data as an indicator of success
513 //----------------------------------------------------------------------
514 // Extract a single uint32_t from the data and update the offset
515 // pointed to by "offset_ptr".
517 // RETURNS the uint32_t that was extracted, or zero on failure.
518 //----------------------------------------------------------------------
520 DataExtractor::GetU32 (offset_t *offset_ptr) const
523 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
526 if (m_byte_order != lldb::endian::InlHostByteOrder())
528 val = ReadSwapInt32 (data);
532 memcpy (&val, data, 4);
538 //----------------------------------------------------------------------
539 // Extract "count" uint32_t values from the binary data and update
540 // the offset pointed to by "offset_ptr". The extracted data is
541 // copied into "dst".
543 // RETURNS the non-NULL buffer pointer upon successful extraction of
544 // all the requested bytes, or NULL when the data is not available
545 // in the buffer due to being out of bounds, or insufficient data.
546 //----------------------------------------------------------------------
548 DataExtractor::GetU32 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
550 const size_t src_size = sizeof(uint32_t) * count;
551 const uint32_t *src = (const uint32_t *)GetData (offset_ptr, src_size);
554 if (m_byte_order != lldb::endian::InlHostByteOrder())
556 uint32_t *dst_pos = (uint32_t *)void_dst;
557 uint32_t *dst_end = dst_pos + count;
558 const uint32_t *src_pos = src;
559 while (dst_pos < dst_end)
561 *dst_pos = ReadSwapInt32 (src_pos);
568 memcpy (void_dst, src, src_size);
570 // Return a non-NULL pointer to the converted data as an indicator of success
576 //----------------------------------------------------------------------
577 // Extract a single uint64_t from the data and update the offset
578 // pointed to by "offset_ptr".
580 // RETURNS the uint64_t that was extracted, or zero on failure.
581 //----------------------------------------------------------------------
583 DataExtractor::GetU64 (offset_t *offset_ptr) const
586 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
589 if (m_byte_order != lldb::endian::InlHostByteOrder())
591 val = ReadSwapInt64 (data);
595 memcpy (&val, data, 8);
601 //----------------------------------------------------------------------
604 // Get multiple consecutive 64 bit values. Return true if the entire
605 // read succeeds and increment the offset pointed to by offset_ptr, else
606 // return false and leave the offset pointed to by offset_ptr unchanged.
607 //----------------------------------------------------------------------
609 DataExtractor::GetU64 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
611 const size_t src_size = sizeof(uint64_t) * count;
612 const uint64_t *src = (const uint64_t *)GetData (offset_ptr, src_size);
615 if (m_byte_order != lldb::endian::InlHostByteOrder())
617 uint64_t *dst_pos = (uint64_t *)void_dst;
618 uint64_t *dst_end = dst_pos + count;
619 const uint64_t *src_pos = src;
620 while (dst_pos < dst_end)
622 *dst_pos = ReadSwapInt64 (src_pos);
629 memcpy (void_dst, src, src_size);
631 // Return a non-NULL pointer to the converted data as an indicator of success
637 //----------------------------------------------------------------------
638 // Extract a single integer value from the data and update the offset
639 // pointed to by "offset_ptr". The size of the extracted integer
640 // is specified by the "byte_size" argument. "byte_size" should have
641 // a value between 1 and 4 since the return value is only 32 bits
642 // wide. Any "byte_size" values less than 1 or greater than 4 will
643 // result in nothing being extracted, and zero being returned.
645 // RETURNS the integer value that was extracted, or zero on failure.
646 //----------------------------------------------------------------------
648 DataExtractor::GetMaxU32 (offset_t *offset_ptr, size_t byte_size) const
652 case 1: return GetU8 (offset_ptr); break;
653 case 2: return GetU16(offset_ptr); break;
654 case 4: return GetU32(offset_ptr); break;
656 assert("GetMaxU32 unhandled case!" == NULL);
662 //----------------------------------------------------------------------
663 // Extract a single integer value from the data and update the offset
664 // pointed to by "offset_ptr". The size of the extracted integer
665 // is specified by the "byte_size" argument. "byte_size" should have
666 // a value >= 1 and <= 8 since the return value is only 64 bits
667 // wide. Any "byte_size" values less than 1 or greater than 8 will
668 // result in nothing being extracted, and zero being returned.
670 // RETURNS the integer value that was extracted, or zero on failure.
671 //----------------------------------------------------------------------
673 DataExtractor::GetMaxU64 (offset_t *offset_ptr, size_t size) const
677 case 1: return GetU8 (offset_ptr); break;
678 case 2: return GetU16(offset_ptr); break;
679 case 4: return GetU32(offset_ptr); break;
680 case 8: return GetU64(offset_ptr); break;
682 assert("GetMax64 unhandled case!" == NULL);
689 DataExtractor::GetMaxU64_unchecked (offset_t *offset_ptr, size_t size) const
693 case 1: return GetU8_unchecked (offset_ptr); break;
694 case 2: return GetU16_unchecked (offset_ptr); break;
695 case 4: return GetU32_unchecked (offset_ptr); break;
696 case 8: return GetU64_unchecked (offset_ptr); break;
698 assert("GetMax64 unhandled case!" == NULL);
705 DataExtractor::GetMaxS64 (offset_t *offset_ptr, size_t size) const
709 case 1: return (int8_t)GetU8 (offset_ptr); break;
710 case 2: return (int16_t)GetU16(offset_ptr); break;
711 case 4: return (int32_t)GetU32(offset_ptr); break;
712 case 8: return (int64_t)GetU64(offset_ptr); break;
714 assert("GetMax64 unhandled case!" == NULL);
721 DataExtractor::GetMaxU64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
723 uint64_t uval64 = GetMaxU64 (offset_ptr, size);
724 if (bitfield_bit_size > 0)
726 if (bitfield_bit_offset > 0)
727 uval64 >>= bitfield_bit_offset;
728 uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
729 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
731 uval64 &= bitfield_mask;
737 DataExtractor::GetMaxS64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
739 int64_t sval64 = GetMaxS64 (offset_ptr, size);
740 if (bitfield_bit_size > 0)
742 if (bitfield_bit_offset > 0)
743 sval64 >>= bitfield_bit_offset;
744 uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
745 sval64 &= bitfield_mask;
746 // sign extend if needed
747 if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
748 sval64 |= ~bitfield_mask;
755 DataExtractor::GetFloat (offset_t *offset_ptr) const
757 typedef float float_type;
758 float_type val = 0.0;
759 const size_t src_size = sizeof(float_type);
760 const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
763 if (m_byte_order != lldb::endian::InlHostByteOrder())
765 const uint8_t *src_data = (const uint8_t *)src;
766 uint8_t *dst_data = (uint8_t *)&val;
767 for (size_t i=0; i<sizeof(float_type); ++i)
768 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
779 DataExtractor::GetDouble (offset_t *offset_ptr) const
781 typedef double float_type;
782 float_type val = 0.0;
783 const size_t src_size = sizeof(float_type);
784 const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
787 if (m_byte_order != lldb::endian::InlHostByteOrder())
789 const uint8_t *src_data = (const uint8_t *)src;
790 uint8_t *dst_data = (uint8_t *)&val;
791 for (size_t i=0; i<sizeof(float_type); ++i)
792 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
804 DataExtractor::GetLongDouble (offset_t *offset_ptr) const
806 long double val = 0.0;
807 #if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
808 *offset_ptr += CopyByteOrderedData (*offset_ptr, 10, &val, sizeof(val), lldb::endian::InlHostByteOrder());
810 *offset_ptr += CopyByteOrderedData (*offset_ptr, sizeof(val), &val, sizeof(val), lldb::endian::InlHostByteOrder());
816 //------------------------------------------------------------------
817 // Extract a single address from the data and update the offset
818 // pointed to by "offset_ptr". The size of the extracted address
819 // comes from the "this->m_addr_size" member variable and should be
820 // set correctly prior to extracting any address values.
822 // RETURNS the address that was extracted, or zero on failure.
823 //------------------------------------------------------------------
825 DataExtractor::GetAddress (offset_t *offset_ptr) const
827 return GetMaxU64 (offset_ptr, m_addr_size);
831 DataExtractor::GetAddress_unchecked (offset_t *offset_ptr) const
833 return GetMaxU64_unchecked (offset_ptr, m_addr_size);
836 //------------------------------------------------------------------
837 // Extract a single pointer from the data and update the offset
838 // pointed to by "offset_ptr". The size of the extracted pointer
839 // comes from the "this->m_addr_size" member variable and should be
840 // set correctly prior to extracting any pointer values.
842 // RETURNS the pointer that was extracted, or zero on failure.
843 //------------------------------------------------------------------
845 DataExtractor::GetPointer (offset_t *offset_ptr) const
847 return GetMaxU64 (offset_ptr, m_addr_size);
850 //----------------------------------------------------------------------
853 // Used for calls when the value type is specified by a DWARF EH Frame
855 //----------------------------------------------------------------------
858 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
860 if (eh_ptr_enc == DW_EH_PE_omit)
861 return ULLONG_MAX; // Value isn't in the buffer...
863 uint64_t baseAddress = 0;
864 uint64_t addressValue = 0;
865 const uint32_t addr_size = GetAddressByteSize();
867 bool signExtendValue = false;
868 // Decode the base part or adjust our offset
869 switch (eh_ptr_enc & 0x70)
872 signExtendValue = true;
873 baseAddress = *offset_ptr;
874 if (pc_rel_addr != LLDB_INVALID_ADDRESS)
875 baseAddress += pc_rel_addr;
877 // Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address.");
880 case DW_EH_PE_textrel:
881 signExtendValue = true;
882 if (text_addr != LLDB_INVALID_ADDRESS)
883 baseAddress = text_addr;
885 // Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero.");
888 case DW_EH_PE_datarel:
889 signExtendValue = true;
890 if (data_addr != LLDB_INVALID_ADDRESS)
891 baseAddress = data_addr;
893 // Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero.");
896 case DW_EH_PE_funcrel:
897 signExtendValue = true;
900 case DW_EH_PE_aligned:
902 // SetPointerSize should be called prior to extracting these so the
903 // pointer size is cached
904 assert(addr_size != 0);
907 // Align to a address size boundary first
908 uint32_t alignOffset = *offset_ptr % addr_size;
910 offset_ptr += addr_size - alignOffset;
919 // Decode the value part
920 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING)
922 case DW_EH_PE_absptr :
924 addressValue = GetAddress (offset_ptr);
926 // addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue);
929 case DW_EH_PE_uleb128 : addressValue = GetULEB128(offset_ptr); break;
930 case DW_EH_PE_udata2 : addressValue = GetU16(offset_ptr); break;
931 case DW_EH_PE_udata4 : addressValue = GetU32(offset_ptr); break;
932 case DW_EH_PE_udata8 : addressValue = GetU64(offset_ptr); break;
933 case DW_EH_PE_sleb128 : addressValue = GetSLEB128(offset_ptr); break;
934 case DW_EH_PE_sdata2 : addressValue = (int16_t)GetU16(offset_ptr); break;
935 case DW_EH_PE_sdata4 : addressValue = (int32_t)GetU32(offset_ptr); break;
936 case DW_EH_PE_sdata8 : addressValue = (int64_t)GetU64(offset_ptr); break;
938 // Unhandled encoding type
943 // Since we promote everything to 64 bit, we may need to sign extend
944 if (signExtendValue && addr_size < sizeof(baseAddress))
946 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
947 if (sign_bit & addressValue)
949 uint64_t mask = ~sign_bit + 1;
950 addressValue |= mask;
953 return baseAddress + addressValue;
957 DataExtractor::ExtractBytes (offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const
959 const uint8_t *src = PeekData (offset, length);
962 if (dst_byte_order != GetByteOrder())
964 // Validate that only a word- or register-sized dst is byte swapped
965 assert (length == 1 || length == 2 || length == 4 || length == 8 ||
966 length == 10 || length == 16 || length == 32);
968 for (uint32_t i=0; i<length; ++i)
969 ((uint8_t*)dst)[i] = src[length - i - 1];
972 ::memcpy (dst, src, length);
978 // Extract data as it exists in target memory
980 DataExtractor::CopyData (offset_t offset,
984 const uint8_t *src = PeekData (offset, length);
987 ::memcpy (dst, src, length);
993 // Extract data and swap if needed when doing the copy
995 DataExtractor::CopyByteOrderedData (offset_t src_offset,
999 ByteOrder dst_byte_order) const
1001 // Validate the source info
1002 if (!ValidOffsetForDataOfSize(src_offset, src_len))
1003 assert (ValidOffsetForDataOfSize(src_offset, src_len));
1004 assert (src_len > 0);
1005 assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
1007 // Validate the destination info
1008 assert (dst_void_ptr != NULL);
1009 assert (dst_len > 0);
1010 assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
1012 // Validate that only a word- or register-sized dst is byte swapped
1013 assert (dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
1014 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
1017 // Must have valid byte orders set in this object and for destination
1018 if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) ||
1019 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
1023 uint8_t* dst = (uint8_t*)dst_void_ptr;
1024 const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len);
1027 if (dst_len >= src_len)
1029 // We are copying the entire value from src into dst.
1030 // Calculate how many, if any, zeroes we need for the most
1031 // significant bytes if "dst_len" is greater than "src_len"...
1032 const size_t num_zeroes = dst_len - src_len;
1033 if (dst_byte_order == eByteOrderBig)
1035 // Big endian, so we lead with zeroes...
1037 ::memset (dst, 0, num_zeroes);
1038 // Then either copy or swap the rest
1039 if (m_byte_order == eByteOrderBig)
1041 ::memcpy (dst + num_zeroes, src, src_len);
1045 for (i=0; i<src_len; ++i)
1046 dst[i+num_zeroes] = src[src_len - 1 - i];
1051 // Little endian destination, so we lead the value bytes
1052 if (m_byte_order == eByteOrderBig)
1054 for (i=0; i<src_len; ++i)
1055 dst[i] = src[src_len - 1 - i];
1059 ::memcpy (dst, src, src_len);
1061 // And zero the rest...
1063 ::memset (dst + src_len, 0, num_zeroes);
1069 // We are only copying some of the value from src into dst..
1071 if (dst_byte_order == eByteOrderBig)
1074 if (m_byte_order == eByteOrderBig)
1076 // Big endian dst, with big endian src
1077 ::memcpy (dst, src + (src_len - dst_len), dst_len);
1081 // Big endian dst, with little endian src
1082 for (i=0; i<dst_len; ++i)
1083 dst[i] = src[dst_len - 1 - i];
1088 // Little endian dst
1089 if (m_byte_order == eByteOrderBig)
1091 // Little endian dst, with big endian src
1092 for (i=0; i<dst_len; ++i)
1093 dst[i] = src[src_len - 1 - i];
1097 // Little endian dst, with big endian src
1098 ::memcpy (dst, src, dst_len);
1109 //----------------------------------------------------------------------
1110 // Extracts a variable length NULL terminated C string from
1111 // the data at the offset pointed to by "offset_ptr". The
1112 // "offset_ptr" will be updated with the offset of the byte that
1113 // follows the NULL terminator byte.
1115 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1116 // "length" is non-zero and there aren't enough available
1117 // bytes, NULL will be returned and "offset_ptr" will not be
1119 //----------------------------------------------------------------------
1121 DataExtractor::GetCStr (offset_t *offset_ptr) const
1123 const char *cstr = (const char *)PeekData (*offset_ptr, 1);
1126 const char *cstr_end = cstr;
1127 const char *end = (const char *)m_end;
1128 while (cstr_end < end && *cstr_end)
1131 // Now we are either at the end of the data or we point to the
1132 // NULL C string terminator with cstr_end...
1133 if (*cstr_end == '\0')
1135 // Advance the offset with one extra byte for the NULL terminator
1136 *offset_ptr += (cstr_end - cstr + 1);
1140 // We reached the end of the data without finding a NULL C string
1141 // terminator. Fall through and return NULL otherwise anyone that
1142 // would have used the result as a C string can wander into
1143 // unknown memory...
1148 //----------------------------------------------------------------------
1149 // Extracts a NULL terminated C string from the fixed length field of
1150 // length "len" at the offset pointed to by "offset_ptr".
1151 // The "offset_ptr" will be updated with the offset of the byte that
1152 // follows the fixed length field.
1154 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1155 // the offset plus the length of the field is out of bounds, or if the
1156 // field does not contain a NULL terminator byte, NULL will be returned
1157 // and "offset_ptr" will not be updated.
1158 //----------------------------------------------------------------------
1160 DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const
1162 const char *cstr = (const char *)PeekData (*offset_ptr, len);
1165 if (memchr (cstr, '\0', len) == NULL)
1175 //------------------------------------------------------------------
1176 // Peeks at a string in the contained data. No verification is done
1177 // to make sure the entire string lies within the bounds of this
1178 // object's data, only "offset" is verified to be a valid offset.
1180 // Returns a valid C string pointer if "offset" is a valid offset in
1181 // this object's data, else NULL is returned.
1182 //------------------------------------------------------------------
1184 DataExtractor::PeekCStr (offset_t offset) const
1186 return (const char *)PeekData (offset, 1);
1189 //----------------------------------------------------------------------
1190 // Extracts an unsigned LEB128 number from this object's data
1191 // starting at the offset pointed to by "offset_ptr". The offset
1192 // pointed to by "offset_ptr" will be updated with the offset of the
1193 // byte following the last extracted byte.
1195 // Returned the extracted integer value.
1196 //----------------------------------------------------------------------
1198 DataExtractor::GetULEB128 (offset_t *offset_ptr) const
1200 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1204 const uint8_t *end = m_end;
1208 uint64_t result = *src++;
1215 uint8_t byte = *src++;
1216 result |= (byte & 0x7f) << shift;
1217 if ((byte & 0x80) == 0)
1222 *offset_ptr = src - m_start;
1229 //----------------------------------------------------------------------
1230 // Extracts an signed LEB128 number from this object's data
1231 // starting at the offset pointed to by "offset_ptr". The offset
1232 // pointed to by "offset_ptr" will be updated with the offset of the
1233 // byte following the last extracted byte.
1235 // Returned the extracted integer value.
1236 //----------------------------------------------------------------------
1238 DataExtractor::GetSLEB128 (offset_t *offset_ptr) const
1240 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1244 const uint8_t *end = m_end;
1250 int size = sizeof (int64_t) * 8;
1259 result |= (byte & 0x7f) << shift;
1261 if ((byte & 0x80) == 0)
1265 // Sign bit of byte is 2nd high order bit (0x40)
1266 if (shift < size && (byte & 0x40))
1267 result |= - (1 << shift);
1269 *offset_ptr += bytecount;
1275 //----------------------------------------------------------------------
1276 // Skips a ULEB128 number (signed or unsigned) from this object's
1277 // data starting at the offset pointed to by "offset_ptr". The
1278 // offset pointed to by "offset_ptr" will be updated with the offset
1279 // of the byte following the last extracted byte.
1281 // Returns the number of bytes consumed during the extraction.
1282 //----------------------------------------------------------------------
1284 DataExtractor::Skip_LEB128 (offset_t *offset_ptr) const
1286 uint32_t bytes_consumed = 0;
1287 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1291 const uint8_t *end = m_end;
1295 const uint8_t *src_pos = src;
1296 while ((src_pos < end) && (*src_pos++ & 0x80))
1298 *offset_ptr += src_pos - src;
1300 return bytes_consumed;
1304 GetAPInt (const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size, llvm::APInt &result)
1306 llvm::SmallVector<uint64_t, 2> uint64_array;
1307 lldb::offset_t bytes_left = byte_size;
1309 const lldb::ByteOrder byte_order = data.GetByteOrder();
1310 if (byte_order == lldb::eByteOrderLittle)
1312 while (bytes_left > 0)
1314 if (bytes_left >= 8)
1316 u64 = data.GetU64(offset_ptr);
1321 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
1324 uint64_array.push_back(u64);
1326 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1329 else if (byte_order == lldb::eByteOrderBig)
1331 lldb::offset_t be_offset = *offset_ptr + byte_size;
1332 lldb::offset_t temp_offset;
1333 while (bytes_left > 0)
1335 if (bytes_left >= 8)
1338 temp_offset = be_offset;
1339 u64 = data.GetU64(&temp_offset);
1344 be_offset -= bytes_left;
1345 temp_offset = be_offset;
1346 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
1349 uint64_array.push_back(u64);
1351 *offset_ptr += byte_size;
1352 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1358 static lldb::offset_t
1359 DumpAPInt (Stream *s, const DataExtractor &data, lldb::offset_t offset, lldb::offset_t byte_size, bool is_signed, unsigned radix)
1362 if (GetAPInt (data, &offset, byte_size, apint))
1364 std::string apint_str(apint.toString(radix, is_signed));
1376 s->Write(apint_str.c_str(), apint_str.size());
1381 static float half2float (uint16_t half)
1384 llvm_unreachable("half2float not implemented for MSVC");
1386 union{ float f; uint32_t u;}u;
1387 int32_t v = (int16_t) half;
1389 if( 0 == (v & 0x7c00))
1391 u.u = v & 0x80007FFFU;
1392 return u.f * ldexpf(1, 125);
1396 u.u = v | 0x70000000U;
1397 return u.f * ldexpf(1, -112);
1402 DataExtractor::Dump (Stream *s,
1403 offset_t start_offset,
1404 lldb::Format item_format,
1405 size_t item_byte_size,
1407 size_t num_per_line,
1409 uint32_t item_bit_size, // If zero, this is not a bitfield value, if non-zero, the value is a bitfield
1410 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the shift amount to apply to a bitfield
1411 ExecutionContextScope *exe_scope) const
1414 return start_offset;
1416 if (item_format == eFormatPointer)
1418 if (item_byte_size != 4 && item_byte_size != 8)
1419 item_byte_size = s->GetAddressByteSize();
1422 offset_t offset = start_offset;
1424 if (item_format == eFormatInstruction)
1428 target_sp = exe_scope->CalculateTarget();
1431 DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL, NULL));
1432 if (disassembler_sp)
1434 lldb::addr_t addr = base_addr + start_offset;
1435 lldb_private::Address so_addr;
1436 bool data_from_file = true;
1437 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1439 data_from_file = false;
1443 if (target_sp->GetSectionLoadList().IsEmpty() || !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
1444 so_addr.SetRawAddress(addr);
1447 size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false, data_from_file);
1451 offset += bytes_consumed;
1452 const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
1453 const bool show_bytes = true;
1454 ExecutionContext exe_ctx;
1455 exe_scope->CalculateExecutionContext(exe_ctx);
1456 disassembler_sp->GetInstructionList().Dump (s, show_address, show_bytes, &exe_ctx);
1458 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
1459 // I'll fix that but for now, just clear the list and it will go away nicely.
1460 disassembler_sp->GetInstructionList().Clear();
1465 s->Printf ("invalid target");
1470 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8)
1471 item_format = eFormatHex;
1473 lldb::offset_t line_start_offset = start_offset;
1474 for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count)
1476 if ((count % num_per_line) == 0)
1480 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1482 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
1483 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0);
1487 if (base_addr != LLDB_INVALID_ADDRESS)
1488 s->Printf ("0x%8.8" PRIx64 ": ",
1489 (uint64_t)(base_addr + (offset - start_offset)/m_target_byte_size ));
1491 line_start_offset = offset;
1494 if (item_format != eFormatChar &&
1495 item_format != eFormatCharPrintable &&
1496 item_format != eFormatCharArray &&
1503 switch (item_format)
1505 case eFormatBoolean:
1506 if (item_byte_size <= 8)
1507 s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false");
1510 s->Printf("error: unsupported byte size (%" PRIu64 ") for boolean format", (uint64_t)item_byte_size);
1516 if (item_byte_size <= 8)
1518 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1519 // Avoid std::bitset<64>::to_string() since it is missing in
1520 // earlier C++ libraries
1521 std::string binary_value(64, '0');
1522 std::bitset<64> bits(uval64);
1523 for (i = 0; i < 64; ++i)
1525 binary_value[64 - 1 - i] = '1';
1526 if (item_bit_size > 0)
1527 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
1528 else if (item_byte_size > 0 && item_byte_size <= 8)
1529 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
1533 const bool is_signed = false;
1534 const unsigned radix = 2;
1535 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1540 case eFormatBytesWithASCII:
1541 for (i=0; i<item_byte_size; ++i)
1543 s->Printf ("%2.2x", GetU8(&offset));
1546 // Put an extra space between the groups of bytes if more than one
1547 // is being dumped in a group (item_byte_size is more than 1).
1548 if (item_byte_size > 1)
1553 case eFormatCharPrintable:
1554 case eFormatCharArray:
1556 // If we are only printing one character surround it with single
1558 if (item_count == 1 && item_format == eFormatChar)
1561 const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1563 s->Printf ("%c", (char)ch);
1564 else if (item_format != eFormatCharPrintable)
1568 case '\033': s->Printf ("\\e"); break;
1569 case '\a': s->Printf ("\\a"); break;
1570 case '\b': s->Printf ("\\b"); break;
1571 case '\f': s->Printf ("\\f"); break;
1572 case '\n': s->Printf ("\\n"); break;
1573 case '\r': s->Printf ("\\r"); break;
1574 case '\t': s->Printf ("\\t"); break;
1575 case '\v': s->Printf ("\\v"); break;
1576 case '\0': s->Printf ("\\0"); break;
1578 if (item_byte_size == 1)
1579 s->Printf ("\\x%2.2x", (uint8_t)ch);
1581 s->Printf ("%" PRIu64, ch);
1587 s->PutChar(NON_PRINTABLE_CHAR);
1590 // If we are only printing one character surround it with single quotes
1591 if (item_count == 1 && item_format == eFormatChar)
1596 case eFormatEnum: // Print enum value as a signed integer when we don't get the enum type
1597 case eFormatDecimal:
1598 if (item_byte_size <= 8)
1599 s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1602 const bool is_signed = true;
1603 const unsigned radix = 10;
1604 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1608 case eFormatUnsigned:
1609 if (item_byte_size <= 8)
1610 s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1613 const bool is_signed = false;
1614 const unsigned radix = 10;
1615 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1620 if (item_byte_size <= 8)
1621 s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1624 const bool is_signed = false;
1625 const unsigned radix = 8;
1626 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1632 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1634 for (i=0; i<item_byte_size; ++i)
1636 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
1638 s->Printf ("%c", ch);
1643 case '\033': s->Printf ("\\e"); break;
1644 case '\a': s->Printf ("\\a"); break;
1645 case '\b': s->Printf ("\\b"); break;
1646 case '\f': s->Printf ("\\f"); break;
1647 case '\n': s->Printf ("\\n"); break;
1648 case '\r': s->Printf ("\\r"); break;
1649 case '\t': s->Printf ("\\t"); break;
1650 case '\v': s->Printf ("\\v"); break;
1651 case '\0': s->Printf ("\\0"); break;
1652 default: s->Printf ("\\x%2.2x", ch); break;
1660 case eFormatCString:
1662 const char *cstr = GetCStr(&offset);
1667 offset = LLDB_INVALID_OFFSET;
1673 while (const char c = *cstr)
1683 case '\033': s->Printf ("\\e"); break;
1684 case '\a': s->Printf ("\\a"); break;
1685 case '\b': s->Printf ("\\b"); break;
1686 case '\f': s->Printf ("\\f"); break;
1687 case '\n': s->Printf ("\\n"); break;
1688 case '\r': s->Printf ("\\r"); break;
1689 case '\t': s->Printf ("\\t"); break;
1690 case '\v': s->Printf ("\\v"); break;
1691 default: s->Printf ("\\x%2.2x", c); break;
1704 case eFormatPointer:
1705 s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t));
1709 case eFormatComplexInteger:
1711 size_t complex_int_byte_size = item_byte_size / 2;
1713 if (complex_int_byte_size <= 8)
1715 s->Printf("%" PRIu64, GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1716 s->Printf(" + %" PRIu64 "i", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1720 s->Printf("error: unsupported byte size (%" PRIu64 ") for complex integer format", (uint64_t)item_byte_size);
1726 case eFormatComplex:
1727 if (sizeof(float) * 2 == item_byte_size)
1729 float f32_1 = GetFloat (&offset);
1730 float f32_2 = GetFloat (&offset);
1732 s->Printf ("%g + %gi", f32_1, f32_2);
1735 else if (sizeof(double) * 2 == item_byte_size)
1737 double d64_1 = GetDouble (&offset);
1738 double d64_2 = GetDouble (&offset);
1740 s->Printf ("%lg + %lgi", d64_1, d64_2);
1743 else if (sizeof(long double) * 2 == item_byte_size)
1745 long double ld64_1 = GetLongDouble (&offset);
1746 long double ld64_2 = GetLongDouble (&offset);
1747 s->Printf ("%Lg + %Lgi", ld64_1, ld64_2);
1752 s->Printf("error: unsupported byte size (%" PRIu64 ") for complex float format", (uint64_t)item_byte_size);
1758 case eFormatDefault:
1760 case eFormatHexUppercase:
1762 bool wantsuppercase = (item_format == eFormatHexUppercase);
1763 switch (item_byte_size)
1769 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));
1773 assert (item_bit_size == 0 && item_bit_offset == 0);
1774 const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size);
1777 s->PutCString("0x");
1779 if (m_byte_order == eByteOrderBig)
1781 for (idx = 0; idx < item_byte_size; ++idx)
1782 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
1786 for (idx = 0; idx < item_byte_size; ++idx)
1787 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]);
1799 bool used_apfloat = false;
1801 target_sp = exe_scope->CalculateTarget();
1804 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1807 clang::ASTContext *ast = clang_ast->getASTContext();
1810 llvm::SmallVector<char, 256> sv;
1811 // Show full precision when printing float values
1812 const unsigned format_precision = 0;
1813 const unsigned format_max_padding = 100;
1814 size_t item_bit_size = item_byte_size * 8;
1816 if (item_bit_size == ast->getTypeSize(ast->FloatTy))
1818 llvm::APInt apint(item_bit_size, this->GetMaxU64(&offset, item_byte_size));
1819 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->FloatTy), apint);
1820 apfloat.toString(sv, format_precision, format_max_padding);
1822 else if (item_bit_size == ast->getTypeSize(ast->DoubleTy))
1825 if (GetAPInt (*this, &offset, item_byte_size, apint))
1827 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->DoubleTy), apint);
1828 apfloat.toString(sv, format_precision, format_max_padding);
1831 else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy))
1834 switch (target_sp->GetArchitecture().GetMachine())
1836 case llvm::Triple::x86:
1837 case llvm::Triple::x86_64:
1838 // clang will assert when constructing the apfloat if we use a 16 byte integer value
1839 if (GetAPInt (*this, &offset, 10, apint))
1841 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1842 apfloat.toString(sv, format_precision, format_max_padding);
1847 if (GetAPInt (*this, &offset, item_byte_size, apint))
1849 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1850 apfloat.toString(sv, format_precision, format_max_padding);
1855 else if (item_bit_size == ast->getTypeSize(ast->HalfTy))
1857 llvm::APInt apint(item_bit_size, this->GetU16(&offset));
1858 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->HalfTy), apint);
1859 apfloat.toString(sv, format_precision, format_max_padding);
1864 s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
1865 used_apfloat = true;
1873 std::ostringstream ss;
1874 if (item_byte_size == sizeof(float) || item_byte_size == 2)
1877 if (item_byte_size == 2)
1879 uint16_t half = this->GetU16(&offset);
1880 f = half2float(half);
1884 f = GetFloat (&offset);
1886 ss.precision(std::numeric_limits<float>::digits10);
1889 else if (item_byte_size == sizeof(double))
1891 ss.precision(std::numeric_limits<double>::digits10);
1892 ss << GetDouble(&offset);
1894 else if (item_byte_size == sizeof(long double) || item_byte_size == 10)
1896 ss.precision(std::numeric_limits<long double>::digits10);
1897 ss << GetLongDouble(&offset);
1901 s->Printf("error: unsupported byte size (%" PRIu64 ") for float format", (uint64_t)item_byte_size);
1905 s->Printf("%s", ss.str().c_str());
1910 case eFormatUnicode16:
1911 s->Printf("U+%4.4x", GetU16 (&offset));
1914 case eFormatUnicode32:
1915 s->Printf("U+0x%8.8x", GetU32 (&offset));
1918 case eFormatAddressInfo:
1920 addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1921 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), addr);
1924 TargetSP target_sp (exe_scope->CalculateTarget());
1925 lldb_private::Address so_addr;
1928 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1933 Address::DumpStyleResolvedDescription,
1934 Address::DumpStyleModuleWithFileAddress);
1938 so_addr.SetOffset(addr);
1939 so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription);
1946 case eFormatHexFloat:
1947 if (sizeof(float) == item_byte_size)
1949 char float_cstr[256];
1950 llvm::APFloat ap_float (GetFloat (&offset));
1951 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1952 s->Printf ("%s", float_cstr);
1955 else if (sizeof(double) == item_byte_size)
1957 char float_cstr[256];
1958 llvm::APFloat ap_float (GetDouble (&offset));
1959 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1960 s->Printf ("%s", float_cstr);
1965 s->Printf("error: unsupported byte size (%" PRIu64 ") for hex float format", (uint64_t)item_byte_size);
1970 // please keep the single-item formats below in sync with FormatManager::GetSingleItemFormat
1971 // if you fail to do so, users will start getting different outputs depending on internal
1972 // implementation details they should not care about ||
1973 case eFormatVectorOfChar: // ||
1974 s->PutChar('{'); // \/
1975 offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1979 case eFormatVectorOfSInt8:
1981 offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1985 case eFormatVectorOfUInt8:
1987 offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1991 case eFormatVectorOfSInt16:
1993 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);
1997 case eFormatVectorOfUInt16:
1999 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);
2003 case eFormatVectorOfSInt32:
2005 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);
2009 case eFormatVectorOfUInt32:
2011 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);
2015 case eFormatVectorOfSInt64:
2017 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);
2021 case eFormatVectorOfUInt64:
2023 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);
2027 case eFormatVectorOfFloat32:
2029 offset = Dump (s, offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
2033 case eFormatVectorOfFloat64:
2035 offset = Dump (s, offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
2039 case eFormatVectorOfUInt128:
2041 offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
2047 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
2049 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
2050 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0);
2052 return offset; // Return the offset at which we ended up
2055 //----------------------------------------------------------------------
2056 // Dumps bytes from this object's data to the stream "s" starting
2057 // "start_offset" bytes into this data, and ending with the byte
2058 // before "end_offset". "base_addr" will be added to the offset
2059 // into the dumped data when showing the offset into the data in the
2060 // output information. "num_per_line" objects of type "type" will
2061 // be dumped with the option to override the format for each object
2062 // with "type_format". "type_format" is a printf style formatting
2063 // string. If "type_format" is NULL, then an appropriate format
2064 // string will be used for the supplied "type". If the stream "s"
2065 // is NULL, then the output will be send to Log().
2066 //----------------------------------------------------------------------
2068 DataExtractor::PutToLog
2071 offset_t start_offset,
2074 uint32_t num_per_line,
2075 DataExtractor::Type type,
2080 return start_offset;
2083 offset_t end_offset;
2086 for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count)
2088 if ((count % num_per_line) == 0)
2090 // Print out any previous string
2091 if (sstr.GetSize() > 0)
2093 log->Printf("%s", sstr.GetData());
2096 // Reset string offset and fill the current line string with address:
2097 if (base_addr != LLDB_INVALID_ADDRESS)
2098 sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset)));
2103 case TypeUInt8: sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break;
2106 char ch = GetU8(&offset);
2107 sstr.Printf (format ? format : " %c", isprint(ch) ? ch : ' ');
2110 case TypeUInt16: sstr.Printf (format ? format : " %4.4x", GetU16(&offset)); break;
2111 case TypeUInt32: sstr.Printf (format ? format : " %8.8x", GetU32(&offset)); break;
2112 case TypeUInt64: sstr.Printf (format ? format : " %16.16" PRIx64, GetU64(&offset)); break;
2113 case TypePointer: sstr.Printf (format ? format : " 0x%" PRIx64, GetAddress(&offset)); break;
2114 case TypeULEB128: sstr.Printf (format ? format : " 0x%" PRIx64, GetULEB128(&offset)); break;
2115 case TypeSLEB128: sstr.Printf (format ? format : " %" PRId64, GetSLEB128(&offset)); break;
2119 if (sstr.GetSize() > 0)
2120 log->Printf("%s", sstr.GetData());
2122 return offset; // Return the offset at which we ended up
2125 //----------------------------------------------------------------------
2128 // Dump out a UUID starting at 'offset' bytes into the buffer
2129 //----------------------------------------------------------------------
2131 DataExtractor::DumpUUID (Stream *s, offset_t offset) const
2135 const uint8_t *uuid_data = PeekData(offset, 16);
2138 lldb_private::UUID uuid(uuid_data, 16);
2143 s->Printf("<not enough data for UUID at offset 0x%8.8" PRIx64 ">", offset);
2149 DataExtractor::DumpHexBytes (Stream *s,
2152 uint32_t bytes_per_line,
2155 DataExtractor data (src, src_len, eByteOrderLittle, 4);
2157 0, // Offset into "src"
2158 eFormatBytes, // Dump as hex bytes
2159 1, // Size of each item is 1 for single bytes
2160 src_len, // Number of bytes
2161 bytes_per_line, // Num bytes per line
2162 base_addr, // Base address
2163 0, 0); // Bitfield info
2167 DataExtractor::Copy (DataExtractor &dest_data) const
2169 if (m_data_sp.get())
2171 // we can pass along the SP to the data
2172 dest_data.SetData(m_data_sp);
2176 const uint8_t *base_ptr = m_start;
2177 size_t data_size = GetByteSize();
2178 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
2180 return GetByteSize();
2184 DataExtractor::Append(DataExtractor& rhs)
2186 if (rhs.GetByteOrder() != GetByteOrder())
2189 if (rhs.GetByteSize() == 0)
2192 if (GetByteSize() == 0)
2193 return (rhs.Copy(*this) > 0);
2195 size_t bytes = GetByteSize() + rhs.GetByteSize();
2197 DataBufferHeap *buffer_heap_ptr = NULL;
2198 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2200 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2203 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2205 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2206 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
2214 DataExtractor::Append(void* buf, offset_t length)
2222 size_t bytes = GetByteSize() + length;
2224 DataBufferHeap *buffer_heap_ptr = NULL;
2225 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2227 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2230 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2232 if (GetByteSize() > 0)
2233 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2235 memcpy(bytes_ptr + GetByteSize(), buf, length);
2243 DataExtractor::Checksum (llvm::SmallVectorImpl<uint8_t> &dest,
2247 max_data = GetByteSize();
2249 max_data = std::min(max_data, GetByteSize());
2253 const llvm::ArrayRef<uint8_t> data(GetDataStart(),max_data);
2256 llvm::MD5::MD5Result result;