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/Target.h"
43 using namespace lldb_private;
45 static inline uint16_t
46 ReadInt16(const unsigned char* ptr, offset_t offset)
48 return *(uint16_t *)(ptr + offset);
50 static inline uint32_t
51 ReadInt32 (const unsigned char* ptr, offset_t offset)
53 return *(uint32_t *)(ptr + offset);
56 static inline uint64_t
57 ReadInt64(const unsigned char* ptr, offset_t offset)
59 return *(uint64_t *)(ptr + offset);
62 static inline uint16_t
63 ReadInt16(const void* ptr)
65 return *(uint16_t *)(ptr);
67 static inline uint32_t
68 ReadInt32 (const void* ptr)
70 return *(uint32_t *)(ptr);
73 static inline uint64_t
74 ReadInt64(const void* ptr)
76 return *(uint64_t *)(ptr);
79 static inline uint16_t
80 ReadSwapInt16(const unsigned char* ptr, offset_t offset)
82 return llvm::ByteSwap_16(*(uint16_t *)(ptr + offset));
85 static inline uint32_t
86 ReadSwapInt32 (const unsigned char* ptr, offset_t offset)
88 return llvm::ByteSwap_32(*(uint32_t *)(ptr + offset));
90 static inline uint64_t
91 ReadSwapInt64(const unsigned char* ptr, offset_t offset)
93 return llvm::ByteSwap_64(*(uint64_t *)(ptr + offset));
96 static inline uint16_t
97 ReadSwapInt16(const void* ptr)
99 return llvm::ByteSwap_16(*(uint16_t *)(ptr));
102 static inline uint32_t
103 ReadSwapInt32 (const void* ptr)
105 return llvm::ByteSwap_32(*(uint32_t *)(ptr));
107 static inline uint64_t
108 ReadSwapInt64(const void* ptr)
110 return llvm::ByteSwap_64(*(uint64_t *)(ptr));
113 #define NON_PRINTABLE_CHAR '.'
114 //----------------------------------------------------------------------
115 // Default constructor.
116 //----------------------------------------------------------------------
117 DataExtractor::DataExtractor () :
120 m_byte_order(lldb::endian::InlHostByteOrder()),
126 //----------------------------------------------------------------------
127 // This constructor allows us to use data that is owned by someone else.
128 // The data must stay around as long as this object is valid.
129 //----------------------------------------------------------------------
130 DataExtractor::DataExtractor (const void* data, offset_t length, ByteOrder endian, uint32_t addr_size) :
131 m_start ((uint8_t*)data),
132 m_end ((uint8_t*)data + length),
133 m_byte_order(endian),
134 m_addr_size (addr_size),
139 //----------------------------------------------------------------------
140 // Make a shared pointer reference to the shared data in "data_sp" and
141 // set the endian swapping setting to "swap", and the address size to
142 // "addr_size". The shared data reference will ensure the data lives
143 // as long as any DataExtractor objects exist that have a reference to
145 //----------------------------------------------------------------------
146 DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint32_t addr_size) :
149 m_byte_order(endian),
150 m_addr_size (addr_size),
156 //----------------------------------------------------------------------
157 // Initialize this object with a subset of the data bytes in "data".
158 // If "data" contains shared data, then a reference to this shared
159 // data will added and the shared data will stay around as long
160 // as any object contains a reference to that data. The endian
161 // swap and address size settings are copied from "data".
162 //----------------------------------------------------------------------
163 DataExtractor::DataExtractor (const DataExtractor& data, offset_t offset, offset_t length) :
166 m_byte_order(data.m_byte_order),
167 m_addr_size(data.m_addr_size),
170 if (data.ValidOffset(offset))
172 offset_t bytes_available = data.GetByteSize() - offset;
173 if (length > bytes_available)
174 length = bytes_available;
175 SetData(data, offset, length);
179 DataExtractor::DataExtractor (const DataExtractor& rhs) :
180 m_start (rhs.m_start),
182 m_byte_order (rhs.m_byte_order),
183 m_addr_size (rhs.m_addr_size),
184 m_data_sp (rhs.m_data_sp)
188 //----------------------------------------------------------------------
189 // Assignment operator
190 //----------------------------------------------------------------------
192 DataExtractor::operator= (const DataExtractor& rhs)
196 m_start = rhs.m_start;
198 m_byte_order = rhs.m_byte_order;
199 m_addr_size = rhs.m_addr_size;
200 m_data_sp = rhs.m_data_sp;
205 //----------------------------------------------------------------------
207 //----------------------------------------------------------------------
208 DataExtractor::~DataExtractor ()
212 //------------------------------------------------------------------
213 // Clears the object contents back to a default invalid state, and
214 // release any references to shared data that this object may
216 //------------------------------------------------------------------
218 DataExtractor::Clear ()
222 m_byte_order = lldb::endian::InlHostByteOrder();
227 //------------------------------------------------------------------
228 // If this object contains shared data, this function returns the
229 // offset into that shared data. Else zero is returned.
230 //------------------------------------------------------------------
232 DataExtractor::GetSharedDataOffset () const
236 const DataBuffer * data = m_data_sp.get();
239 const uint8_t * data_bytes = data->GetBytes();
240 if (data_bytes != NULL)
242 assert(m_start >= data_bytes);
243 return m_start - data_bytes;
250 //----------------------------------------------------------------------
251 // Set the data with which this object will extract from to data
252 // starting at BYTES and set the length of the data to LENGTH bytes
253 // long. The data is externally owned must be around at least as
254 // long as this object points to the data. No copy of the data is
255 // made, this object just refers to this data and can extract from
256 // it. If this object refers to any shared data upon entry, the
257 // reference to that data will be released. Is SWAP is set to true,
258 // any data extracted will be endian swapped.
259 //----------------------------------------------------------------------
261 DataExtractor::SetData (const void *bytes, offset_t length, ByteOrder endian)
263 m_byte_order = endian;
265 if (bytes == NULL || length == 0)
272 m_start = (uint8_t *)bytes;
273 m_end = m_start + length;
275 return GetByteSize();
278 //----------------------------------------------------------------------
279 // Assign the data for this object to be a subrange in "data"
280 // starting "data_offset" bytes into "data" and ending "data_length"
281 // bytes later. If "data_offset" is not a valid offset into "data",
282 // then this object will contain no bytes. If "data_offset" is
283 // within "data" yet "data_length" is too large, the length will be
284 // capped at the number of bytes remaining in "data". If "data"
285 // contains a shared pointer to other data, then a ref counted
286 // pointer to that data will be made in this object. If "data"
287 // doesn't contain a shared pointer to data, then the bytes referred
288 // to in "data" will need to exist at least as long as this object
289 // refers to those bytes. The address size and endian swap settings
290 // are copied from the current values in "data".
291 //----------------------------------------------------------------------
293 DataExtractor::SetData (const DataExtractor& data, offset_t data_offset, offset_t data_length)
295 m_addr_size = data.m_addr_size;
296 // If "data" contains shared pointer to data, then we can use that
297 if (data.m_data_sp.get())
299 m_byte_order = data.m_byte_order;
300 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length);
303 // We have a DataExtractor object that just has a pointer to bytes
304 if (data.ValidOffset(data_offset))
306 if (data_length > data.GetByteSize() - data_offset)
307 data_length = data.GetByteSize() - data_offset;
308 return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder());
313 //----------------------------------------------------------------------
314 // Assign the data for this object to be a subrange of the shared
315 // data in "data_sp" starting "data_offset" bytes into "data_sp"
316 // and ending "data_length" bytes later. If "data_offset" is not
317 // a valid offset into "data_sp", then this object will contain no
318 // bytes. If "data_offset" is within "data_sp" yet "data_length" is
319 // too large, the length will be capped at the number of bytes
320 // remaining in "data_sp". A ref counted pointer to the data in
321 // "data_sp" will be made in this object IF the number of bytes this
322 // object refers to in greater than zero (if at least one byte was
323 // available starting at "data_offset") to ensure the data stays
324 // around as long as it is needed. The address size and endian swap
325 // settings will remain unchanged from their current settings.
326 //----------------------------------------------------------------------
328 DataExtractor::SetData (const DataBufferSP& data_sp, offset_t data_offset, offset_t data_length)
330 m_start = m_end = NULL;
337 const size_t data_size = data_sp->GetByteSize();
338 if (data_offset < data_size)
340 m_start = data_sp->GetBytes() + data_offset;
341 const size_t bytes_left = data_size - data_offset;
342 // Cap the length of we asked for too many
343 if (data_length <= bytes_left)
344 m_end = m_start + data_length; // We got all the bytes we wanted
346 m_end = m_start + bytes_left; // Not all the bytes requested were available in the shared data
351 size_t new_size = GetByteSize();
353 // Don't hold a shared pointer to the data buffer if we don't share
354 // any valid bytes in the shared buffer.
361 //----------------------------------------------------------------------
362 // Extract a single unsigned char from the binary data and update
363 // the offset pointed to by "offset_ptr".
365 // RETURNS the byte that was extracted, or zero on failure.
366 //----------------------------------------------------------------------
368 DataExtractor::GetU8 (offset_t *offset_ptr) const
370 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, 1);
376 //----------------------------------------------------------------------
377 // Extract "count" unsigned chars from the binary data and update the
378 // offset pointed to by "offset_ptr". The extracted data is copied into
381 // RETURNS the non-NULL buffer pointer upon successful extraction of
382 // all the requested bytes, or NULL when the data is not available in
383 // the buffer due to being out of bounds, or unsufficient data.
384 //----------------------------------------------------------------------
386 DataExtractor::GetU8 (offset_t *offset_ptr, void *dst, uint32_t count) const
388 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, count);
391 // Copy the data into the buffer
392 memcpy (dst, data, count);
393 // Return a non-NULL pointer to the converted data as an indicator of success
399 //----------------------------------------------------------------------
400 // Extract a single uint16_t from the data and update the offset
401 // pointed to by "offset_ptr".
403 // RETURNS the uint16_t that was extracted, or zero on failure.
404 //----------------------------------------------------------------------
406 DataExtractor::GetU16 (offset_t *offset_ptr) const
409 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
412 if (m_byte_order != lldb::endian::InlHostByteOrder())
413 val = ReadSwapInt16(data);
415 val = ReadInt16 (data);
421 DataExtractor::GetU16_unchecked (offset_t *offset_ptr) const
424 if (m_byte_order == lldb::endian::InlHostByteOrder())
425 val = ReadInt16 (m_start, *offset_ptr);
427 val = ReadSwapInt16(m_start, *offset_ptr);
428 *offset_ptr += sizeof(val);
433 DataExtractor::GetU32_unchecked (offset_t *offset_ptr) const
436 if (m_byte_order == lldb::endian::InlHostByteOrder())
437 val = ReadInt32 (m_start, *offset_ptr);
439 val = ReadSwapInt32 (m_start, *offset_ptr);
440 *offset_ptr += sizeof(val);
445 DataExtractor::GetU64_unchecked (offset_t *offset_ptr) const
448 if (m_byte_order == lldb::endian::InlHostByteOrder())
449 val = ReadInt64 (m_start, *offset_ptr);
451 val = ReadSwapInt64 (m_start, *offset_ptr);
452 *offset_ptr += sizeof(val);
457 //----------------------------------------------------------------------
458 // Extract "count" uint16_t values from the binary data and update
459 // the offset pointed to by "offset_ptr". The extracted data is
460 // copied into "dst".
462 // RETURNS the non-NULL buffer pointer upon successful extraction of
463 // all the requested bytes, or NULL when the data is not available
464 // in the buffer due to being out of bounds, or unsufficient data.
465 //----------------------------------------------------------------------
467 DataExtractor::GetU16 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
469 const size_t src_size = sizeof(uint16_t) * count;
470 const uint16_t *src = (const uint16_t *)GetData (offset_ptr, src_size);
473 if (m_byte_order != lldb::endian::InlHostByteOrder())
475 uint16_t *dst_pos = (uint16_t *)void_dst;
476 uint16_t *dst_end = dst_pos + count;
477 const uint16_t *src_pos = src;
478 while (dst_pos < dst_end)
480 *dst_pos = ReadSwapInt16 (src_pos);
487 memcpy (void_dst, src, src_size);
489 // Return a non-NULL pointer to the converted data as an indicator of success
495 //----------------------------------------------------------------------
496 // Extract a single uint32_t from the data and update the offset
497 // pointed to by "offset_ptr".
499 // RETURNS the uint32_t that was extracted, or zero on failure.
500 //----------------------------------------------------------------------
502 DataExtractor::GetU32 (offset_t *offset_ptr) const
505 const uint32_t *data = (const uint32_t *)GetData (offset_ptr, sizeof(val));
508 if (m_byte_order != lldb::endian::InlHostByteOrder())
509 val = ReadSwapInt32 (data);
516 //----------------------------------------------------------------------
517 // Extract "count" uint32_t values from the binary data and update
518 // the offset pointed to by "offset_ptr". The extracted data is
519 // copied into "dst".
521 // RETURNS the non-NULL buffer pointer upon successful extraction of
522 // all the requested bytes, or NULL when the data is not available
523 // in the buffer due to being out of bounds, or unsufficient data.
524 //----------------------------------------------------------------------
526 DataExtractor::GetU32 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
528 const size_t src_size = sizeof(uint32_t) * count;
529 const uint32_t *src = (const uint32_t *)GetData (offset_ptr, src_size);
532 if (m_byte_order != lldb::endian::InlHostByteOrder())
534 uint32_t *dst_pos = (uint32_t *)void_dst;
535 uint32_t *dst_end = dst_pos + count;
536 const uint32_t *src_pos = src;
537 while (dst_pos < dst_end)
539 *dst_pos = ReadSwapInt32 (src_pos);
546 memcpy (void_dst, src, src_size);
548 // Return a non-NULL pointer to the converted data as an indicator of success
554 //----------------------------------------------------------------------
555 // Extract a single uint64_t from the data and update the offset
556 // pointed to by "offset_ptr".
558 // RETURNS the uint64_t that was extracted, or zero on failure.
559 //----------------------------------------------------------------------
561 DataExtractor::GetU64 (offset_t *offset_ptr) const
564 const uint64_t *data = (const uint64_t *)GetData (offset_ptr, sizeof(val));
567 if (m_byte_order != lldb::endian::InlHostByteOrder())
568 val = ReadSwapInt64 (data);
575 //----------------------------------------------------------------------
578 // Get multiple consecutive 64 bit values. Return true if the entire
579 // read succeeds and increment the offset pointed to by offset_ptr, else
580 // return false and leave the offset pointed to by offset_ptr unchanged.
581 //----------------------------------------------------------------------
583 DataExtractor::GetU64 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
585 const size_t src_size = sizeof(uint64_t) * count;
586 const uint64_t *src = (const uint64_t *)GetData (offset_ptr, src_size);
589 if (m_byte_order != lldb::endian::InlHostByteOrder())
591 uint64_t *dst_pos = (uint64_t *)void_dst;
592 uint64_t *dst_end = dst_pos + count;
593 const uint64_t *src_pos = src;
594 while (dst_pos < dst_end)
596 *dst_pos = ReadSwapInt64 (src_pos);
603 memcpy (void_dst, src, src_size);
605 // Return a non-NULL pointer to the converted data as an indicator of success
611 //----------------------------------------------------------------------
612 // Extract a single integer value from the data and update the offset
613 // pointed to by "offset_ptr". The size of the extracted integer
614 // is specified by the "byte_size" argument. "byte_size" should have
615 // a value between 1 and 4 since the return value is only 32 bits
616 // wide. Any "byte_size" values less than 1 or greater than 4 will
617 // result in nothing being extracted, and zero being returned.
619 // RETURNS the integer value that was extracted, or zero on failure.
620 //----------------------------------------------------------------------
622 DataExtractor::GetMaxU32 (offset_t *offset_ptr, size_t byte_size) const
626 case 1: return GetU8 (offset_ptr); break;
627 case 2: return GetU16(offset_ptr); break;
628 case 4: return GetU32(offset_ptr); break;
630 assert("GetMaxU32 unhandled case!" == NULL);
636 //----------------------------------------------------------------------
637 // Extract a single integer value from the data and update the offset
638 // pointed to by "offset_ptr". The size of the extracted integer
639 // is specified by the "byte_size" argument. "byte_size" should have
640 // a value >= 1 and <= 8 since the return value is only 64 bits
641 // wide. Any "byte_size" values less than 1 or greater than 8 will
642 // result in nothing being extracted, and zero being returned.
644 // RETURNS the integer value that was extracted, or zero on failure.
645 //----------------------------------------------------------------------
647 DataExtractor::GetMaxU64 (offset_t *offset_ptr, size_t size) const
651 case 1: return GetU8 (offset_ptr); break;
652 case 2: return GetU16(offset_ptr); break;
653 case 4: return GetU32(offset_ptr); break;
654 case 8: return GetU64(offset_ptr); break;
656 assert("GetMax64 unhandled case!" == NULL);
663 DataExtractor::GetMaxU64_unchecked (offset_t *offset_ptr, size_t size) const
667 case 1: return GetU8_unchecked (offset_ptr); break;
668 case 2: return GetU16_unchecked (offset_ptr); break;
669 case 4: return GetU32_unchecked (offset_ptr); break;
670 case 8: return GetU64_unchecked (offset_ptr); break;
672 assert("GetMax64 unhandled case!" == NULL);
679 DataExtractor::GetMaxS64 (offset_t *offset_ptr, size_t size) const
683 case 1: return (int8_t)GetU8 (offset_ptr); break;
684 case 2: return (int16_t)GetU16(offset_ptr); break;
685 case 4: return (int32_t)GetU32(offset_ptr); break;
686 case 8: return (int64_t)GetU64(offset_ptr); break;
688 assert("GetMax64 unhandled case!" == NULL);
695 DataExtractor::GetMaxU64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
697 uint64_t uval64 = GetMaxU64 (offset_ptr, size);
698 if (bitfield_bit_size > 0)
700 if (bitfield_bit_offset > 0)
701 uval64 >>= bitfield_bit_offset;
702 uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
703 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
705 uval64 &= bitfield_mask;
711 DataExtractor::GetMaxS64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
713 int64_t sval64 = GetMaxS64 (offset_ptr, size);
714 if (bitfield_bit_size > 0)
716 if (bitfield_bit_offset > 0)
717 sval64 >>= bitfield_bit_offset;
718 uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
719 sval64 &= bitfield_mask;
720 // sign extend if needed
721 if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
722 sval64 |= ~bitfield_mask;
729 DataExtractor::GetFloat (offset_t *offset_ptr) const
731 typedef float float_type;
732 float_type val = 0.0;
733 const size_t src_size = sizeof(float_type);
734 const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
737 if (m_byte_order != lldb::endian::InlHostByteOrder())
739 const uint8_t *src_data = (const uint8_t *)src;
740 uint8_t *dst_data = (uint8_t *)&val;
741 for (size_t i=0; i<sizeof(float_type); ++i)
742 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
753 DataExtractor::GetDouble (offset_t *offset_ptr) const
755 typedef double float_type;
756 float_type val = 0.0;
757 const size_t src_size = sizeof(float_type);
758 const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
761 if (m_byte_order != lldb::endian::InlHostByteOrder())
763 const uint8_t *src_data = (const uint8_t *)src;
764 uint8_t *dst_data = (uint8_t *)&val;
765 for (size_t i=0; i<sizeof(float_type); ++i)
766 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
778 DataExtractor::GetLongDouble (offset_t *offset_ptr) const
780 long double val = 0.0;
781 #if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
782 *offset_ptr += CopyByteOrderedData (*offset_ptr, 10, &val, sizeof(val), lldb::endian::InlHostByteOrder());
784 *offset_ptr += CopyByteOrderedData (*offset_ptr, sizeof(val), &val, sizeof(val), lldb::endian::InlHostByteOrder());
790 //------------------------------------------------------------------
791 // Extract a single address from the data and update the offset
792 // pointed to by "offset_ptr". The size of the extracted address
793 // comes from the "this->m_addr_size" member variable and should be
794 // set correctly prior to extracting any address values.
796 // RETURNS the address that was extracted, or zero on failure.
797 //------------------------------------------------------------------
799 DataExtractor::GetAddress (offset_t *offset_ptr) const
801 return GetMaxU64 (offset_ptr, m_addr_size);
805 DataExtractor::GetAddress_unchecked (offset_t *offset_ptr) const
807 return GetMaxU64_unchecked (offset_ptr, m_addr_size);
810 //------------------------------------------------------------------
811 // Extract a single pointer from the data and update the offset
812 // pointed to by "offset_ptr". The size of the extracted pointer
813 // comes from the "this->m_addr_size" member variable and should be
814 // set correctly prior to extracting any pointer values.
816 // RETURNS the pointer that was extracted, or zero on failure.
817 //------------------------------------------------------------------
819 DataExtractor::GetPointer (offset_t *offset_ptr) const
821 return GetMaxU64 (offset_ptr, m_addr_size);
824 //----------------------------------------------------------------------
827 // Used for calls when the value type is specified by a DWARF EH Frame
829 //----------------------------------------------------------------------
832 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
834 if (eh_ptr_enc == DW_EH_PE_omit)
835 return ULLONG_MAX; // Value isn't in the buffer...
837 uint64_t baseAddress = 0;
838 uint64_t addressValue = 0;
839 const uint32_t addr_size = GetAddressByteSize();
841 bool signExtendValue = false;
842 // Decode the base part or adjust our offset
843 switch (eh_ptr_enc & 0x70)
846 signExtendValue = true;
847 baseAddress = *offset_ptr;
848 if (pc_rel_addr != LLDB_INVALID_ADDRESS)
849 baseAddress += pc_rel_addr;
851 // Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address.");
854 case DW_EH_PE_textrel:
855 signExtendValue = true;
856 if (text_addr != LLDB_INVALID_ADDRESS)
857 baseAddress = text_addr;
859 // Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero.");
862 case DW_EH_PE_datarel:
863 signExtendValue = true;
864 if (data_addr != LLDB_INVALID_ADDRESS)
865 baseAddress = data_addr;
867 // Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero.");
870 case DW_EH_PE_funcrel:
871 signExtendValue = true;
874 case DW_EH_PE_aligned:
876 // SetPointerSize should be called prior to extracting these so the
877 // pointer size is cached
878 assert(addr_size != 0);
881 // Align to a address size boundary first
882 uint32_t alignOffset = *offset_ptr % addr_size;
884 offset_ptr += addr_size - alignOffset;
893 // Decode the value part
894 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING)
896 case DW_EH_PE_absptr :
898 addressValue = GetAddress (offset_ptr);
900 // addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue);
903 case DW_EH_PE_uleb128 : addressValue = GetULEB128(offset_ptr); break;
904 case DW_EH_PE_udata2 : addressValue = GetU16(offset_ptr); break;
905 case DW_EH_PE_udata4 : addressValue = GetU32(offset_ptr); break;
906 case DW_EH_PE_udata8 : addressValue = GetU64(offset_ptr); break;
907 case DW_EH_PE_sleb128 : addressValue = GetSLEB128(offset_ptr); break;
908 case DW_EH_PE_sdata2 : addressValue = (int16_t)GetU16(offset_ptr); break;
909 case DW_EH_PE_sdata4 : addressValue = (int32_t)GetU32(offset_ptr); break;
910 case DW_EH_PE_sdata8 : addressValue = (int64_t)GetU64(offset_ptr); break;
912 // Unhandled encoding type
917 // Since we promote everything to 64 bit, we may need to sign extend
918 if (signExtendValue && addr_size < sizeof(baseAddress))
920 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
921 if (sign_bit & addressValue)
923 uint64_t mask = ~sign_bit + 1;
924 addressValue |= mask;
927 return baseAddress + addressValue;
931 DataExtractor::ExtractBytes (offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const
933 const uint8_t *src = PeekData (offset, length);
936 if (dst_byte_order != GetByteOrder())
938 for (uint32_t i=0; i<length; ++i)
939 ((uint8_t*)dst)[i] = src[length - i - 1];
942 ::memcpy (dst, src, length);
948 // Extract data and swap if needed when doing the copy
950 DataExtractor::CopyByteOrderedData (offset_t src_offset,
954 ByteOrder dst_byte_order) const
956 // Validate the source info
957 if (!ValidOffsetForDataOfSize(src_offset, src_len))
958 assert (ValidOffsetForDataOfSize(src_offset, src_len));
959 assert (src_len > 0);
960 assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
962 // Validate the destination info
963 assert (dst_void_ptr != NULL);
964 assert (dst_len > 0);
965 assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
967 // Must have valid byte orders set in this object and for destination
968 if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) ||
969 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
973 uint8_t* dst = (uint8_t*)dst_void_ptr;
974 const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len);
977 if (dst_len >= src_len)
979 // We are copying the entire value from src into dst.
980 // Calculate how many, if any, zeroes we need for the most
981 // significant bytes if "dst_len" is greater than "src_len"...
982 const size_t num_zeroes = dst_len - src_len;
983 if (dst_byte_order == eByteOrderBig)
985 // Big endian, so we lead with zeroes...
987 ::memset (dst, 0, num_zeroes);
988 // Then either copy or swap the rest
989 if (m_byte_order == eByteOrderBig)
991 ::memcpy (dst + num_zeroes, src, src_len);
995 for (i=0; i<src_len; ++i)
996 dst[i+num_zeroes] = src[src_len - 1 - i];
1001 // Little endian destination, so we lead the value bytes
1002 if (m_byte_order == eByteOrderBig)
1004 for (i=0; i<src_len; ++i)
1005 dst[i] = src[src_len - 1 - i];
1009 ::memcpy (dst, src, src_len);
1011 // And zero the rest...
1013 ::memset (dst + src_len, 0, num_zeroes);
1019 // We are only copying some of the value from src into dst..
1021 if (dst_byte_order == eByteOrderBig)
1024 if (m_byte_order == eByteOrderBig)
1026 // Big endian dst, with big endian src
1027 ::memcpy (dst, src + (src_len - dst_len), dst_len);
1031 // Big endian dst, with little endian src
1032 for (i=0; i<dst_len; ++i)
1033 dst[i] = src[dst_len - 1 - i];
1038 // Little endian dst
1039 if (m_byte_order == eByteOrderBig)
1041 // Little endian dst, with big endian src
1042 for (i=0; i<dst_len; ++i)
1043 dst[i] = src[src_len - 1 - i];
1047 // Little endian dst, with big endian src
1048 ::memcpy (dst, src, dst_len);
1059 //----------------------------------------------------------------------
1060 // Extracts a variable length NULL terminated C string from
1061 // the data at the offset pointed to by "offset_ptr". The
1062 // "offset_ptr" will be updated with the offset of the byte that
1063 // follows the NULL terminator byte.
1065 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1066 // "length" is non-zero and there aren't enough avaialable
1067 // bytes, NULL will be returned and "offset_ptr" will not be
1069 //----------------------------------------------------------------------
1071 DataExtractor::GetCStr (offset_t *offset_ptr) const
1073 const char *cstr = (const char *)PeekData (*offset_ptr, 1);
1076 const char *cstr_end = cstr;
1077 const char *end = (const char *)m_end;
1078 while (cstr_end < end && *cstr_end)
1081 // Now we are either at the end of the data or we point to the
1082 // NULL C string terminator with cstr_end...
1083 if (*cstr_end == '\0')
1085 // Advance the offset with one extra byte for the NULL terminator
1086 *offset_ptr += (cstr_end - cstr + 1);
1090 // We reached the end of the data without finding a NULL C string
1091 // terminator. Fall through and return NULL otherwise anyone that
1092 // would have used the result as a C string can wonder into
1093 // unknown memory...
1098 //----------------------------------------------------------------------
1099 // Extracts a NULL terminated C string from the fixed length field of
1100 // length "len" at the offset pointed to by "offset_ptr".
1101 // The "offset_ptr" will be updated with the offset of the byte that
1102 // follows the fixed length field.
1104 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1105 // the offset plus the length of the field is out of bounds, or if the
1106 // field does not contain a NULL terminator byte, NULL will be returned
1107 // and "offset_ptr" will not be updated.
1108 //----------------------------------------------------------------------
1110 DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const
1112 const char *cstr = (const char *)PeekData (*offset_ptr, len);
1115 if (memchr (cstr, '\0', len) == NULL)
1125 //------------------------------------------------------------------
1126 // Peeks at a string in the contained data. No verification is done
1127 // to make sure the entire string lies within the bounds of this
1128 // object's data, only "offset" is verified to be a valid offset.
1130 // Returns a valid C string pointer if "offset" is a valid offset in
1131 // this object's data, else NULL is returned.
1132 //------------------------------------------------------------------
1134 DataExtractor::PeekCStr (offset_t offset) const
1136 return (const char *)PeekData (offset, 1);
1139 //----------------------------------------------------------------------
1140 // Extracts an unsigned LEB128 number from this object's data
1141 // starting at the offset pointed to by "offset_ptr". The offset
1142 // pointed to by "offset_ptr" will be updated with the offset of the
1143 // byte following the last extracted byte.
1145 // Returned the extracted integer value.
1146 //----------------------------------------------------------------------
1148 DataExtractor::GetULEB128 (offset_t *offset_ptr) const
1150 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1154 const uint8_t *end = m_end;
1158 uint64_t result = *src++;
1165 uint8_t byte = *src++;
1166 result |= (byte & 0x7f) << shift;
1167 if ((byte & 0x80) == 0)
1172 *offset_ptr = src - m_start;
1179 //----------------------------------------------------------------------
1180 // Extracts an signed LEB128 number from this object's data
1181 // starting at the offset pointed to by "offset_ptr". The offset
1182 // pointed to by "offset_ptr" will be updated with the offset of the
1183 // byte following the last extracted byte.
1185 // Returned the extracted integer value.
1186 //----------------------------------------------------------------------
1188 DataExtractor::GetSLEB128 (offset_t *offset_ptr) const
1190 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1194 const uint8_t *end = m_end;
1200 int size = sizeof (int64_t) * 8;
1209 result |= (byte & 0x7f) << shift;
1211 if ((byte & 0x80) == 0)
1215 // Sign bit of byte is 2nd high order bit (0x40)
1216 if (shift < size && (byte & 0x40))
1217 result |= - (1 << shift);
1219 *offset_ptr += bytecount;
1225 //----------------------------------------------------------------------
1226 // Skips a ULEB128 number (signed or unsigned) from this object's
1227 // data starting at the offset pointed to by "offset_ptr". The
1228 // offset pointed to by "offset_ptr" will be updated with the offset
1229 // of the byte following the last extracted byte.
1231 // Returns the number of bytes consumed during the extraction.
1232 //----------------------------------------------------------------------
1234 DataExtractor::Skip_LEB128 (offset_t *offset_ptr) const
1236 uint32_t bytes_consumed = 0;
1237 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1241 const uint8_t *end = m_end;
1245 const uint8_t *src_pos = src;
1246 while ((src_pos < end) && (*src_pos++ & 0x80))
1248 *offset_ptr += src_pos - src;
1250 return bytes_consumed;
1254 GetAPInt (const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size, llvm::APInt &result)
1256 llvm::SmallVector<uint64_t, 2> uint64_array;
1257 lldb::offset_t bytes_left = byte_size;
1259 const lldb::ByteOrder byte_order = data.GetByteOrder();
1260 if (byte_order == lldb::eByteOrderLittle)
1262 while (bytes_left > 0)
1264 if (bytes_left >= 8)
1266 u64 = data.GetU64(offset_ptr);
1271 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
1274 uint64_array.push_back(u64);
1276 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1279 else if (byte_order == lldb::eByteOrderBig)
1281 lldb::offset_t be_offset = *offset_ptr + byte_size;
1282 lldb::offset_t temp_offset;
1283 while (bytes_left > 0)
1285 if (bytes_left >= 8)
1288 temp_offset = be_offset;
1289 u64 = data.GetU64(&temp_offset);
1294 be_offset -= bytes_left;
1295 temp_offset = be_offset;
1296 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
1299 uint64_array.push_back(u64);
1301 *offset_ptr += byte_size;
1302 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1308 static lldb::offset_t
1309 DumpAPInt (Stream *s, const DataExtractor &data, lldb::offset_t offset, lldb::offset_t byte_size, bool is_signed, unsigned radix)
1312 if (GetAPInt (data, &offset, byte_size, apint))
1314 std::string apint_str(apint.toString(radix, is_signed));
1326 s->Write(apint_str.c_str(), apint_str.size());
1331 static float half2float (uint16_t half)
1333 union{ float f; uint32_t u;}u;
1334 int32_t v = (int16_t) half;
1336 if( 0 == (v & 0x7c00))
1338 u.u = v & 0x80007FFFU;
1339 return u.f * 0x1.0p125f;
1343 u.u = v | 0x70000000U;
1344 return u.f * 0x1.0p-112f;
1348 DataExtractor::Dump (Stream *s,
1349 offset_t start_offset,
1350 lldb::Format item_format,
1351 size_t item_byte_size,
1353 size_t num_per_line,
1355 uint32_t item_bit_size, // If zero, this is not a bitfield value, if non-zero, the value is a bitfield
1356 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the shift amount to apply to a bitfield
1357 ExecutionContextScope *exe_scope) const
1360 return start_offset;
1362 if (item_format == eFormatPointer)
1364 if (item_byte_size != 4 && item_byte_size != 8)
1365 item_byte_size = s->GetAddressByteSize();
1368 offset_t offset = start_offset;
1370 if (item_format == eFormatInstruction)
1374 target_sp = exe_scope->CalculateTarget();
1377 DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL, NULL));
1378 if (disassembler_sp)
1380 lldb::addr_t addr = base_addr + start_offset;
1381 lldb_private::Address so_addr;
1382 bool data_from_file = true;
1383 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1385 data_from_file = false;
1389 if (target_sp->GetSectionLoadList().IsEmpty() || !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
1390 so_addr.SetRawAddress(addr);
1393 size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false, data_from_file);
1397 offset += bytes_consumed;
1398 const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
1399 const bool show_bytes = true;
1400 ExecutionContext exe_ctx;
1401 exe_scope->CalculateExecutionContext(exe_ctx);
1402 disassembler_sp->GetInstructionList().Dump (s, show_address, show_bytes, &exe_ctx);
1404 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
1405 // I'll fix that but for now, just clear the list and it will go away nicely.
1406 disassembler_sp->GetInstructionList().Clear();
1411 s->Printf ("invalid target");
1416 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8)
1417 item_format = eFormatHex;
1419 lldb::offset_t line_start_offset = start_offset;
1420 for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count)
1422 if ((count % num_per_line) == 0)
1426 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1428 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
1429 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, LLDB_INVALID_OFFSET, LLDB_INVALID_ADDRESS, 0, 0);
1433 if (base_addr != LLDB_INVALID_ADDRESS)
1434 s->Printf ("0x%8.8" PRIx64 ": ", (uint64_t)(base_addr + (offset - start_offset)));
1435 line_start_offset = offset;
1438 if (item_format != eFormatChar &&
1439 item_format != eFormatCharPrintable &&
1440 item_format != eFormatCharArray &&
1447 switch (item_format)
1449 case eFormatBoolean:
1450 if (item_byte_size <= 8)
1451 s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false");
1454 s->Printf("error: unsupported byte size (%zu) for boolean format", item_byte_size);
1460 if (item_byte_size <= 8)
1462 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1463 // Avoid std::bitset<64>::to_string() since it is missing in
1464 // earlier C++ libraries
1465 std::string binary_value(64, '0');
1466 std::bitset<64> bits(uval64);
1467 for (i = 0; i < 64; ++i)
1469 binary_value[64 - 1 - i] = '1';
1470 if (item_bit_size > 0)
1471 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
1472 else if (item_byte_size > 0 && item_byte_size <= 8)
1473 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
1477 const bool is_signed = false;
1478 const unsigned radix = 2;
1479 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1484 case eFormatBytesWithASCII:
1485 for (i=0; i<item_byte_size; ++i)
1487 s->Printf ("%2.2x", GetU8(&offset));
1489 // Put an extra space between the groups of bytes if more than one
1490 // is being dumped in a group (item_byte_size is more than 1).
1491 if (item_byte_size > 1)
1496 case eFormatCharPrintable:
1497 case eFormatCharArray:
1499 // If we are only printing one character surround it with single
1501 if (item_count == 1 && item_format == eFormatChar)
1504 const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1506 s->Printf ("%c", (char)ch);
1507 else if (item_format != eFormatCharPrintable)
1511 case '\033': s->Printf ("\\e"); break;
1512 case '\a': s->Printf ("\\a"); break;
1513 case '\b': s->Printf ("\\b"); break;
1514 case '\f': s->Printf ("\\f"); break;
1515 case '\n': s->Printf ("\\n"); break;
1516 case '\r': s->Printf ("\\r"); break;
1517 case '\t': s->Printf ("\\t"); break;
1518 case '\v': s->Printf ("\\v"); break;
1519 case '\0': s->Printf ("\\0"); break;
1521 if (item_byte_size == 1)
1522 s->Printf ("\\x%2.2x", (uint8_t)ch);
1524 s->Printf ("%" PRIu64, ch);
1530 s->PutChar(NON_PRINTABLE_CHAR);
1533 // If we are only printing one character surround it with single quotes
1534 if (item_count == 1 && item_format == eFormatChar)
1539 case eFormatEnum: // Print enum value as a signed integer when we don't get the enum type
1540 case eFormatDecimal:
1541 if (item_byte_size <= 8)
1542 s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1545 const bool is_signed = true;
1546 const unsigned radix = 10;
1547 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1551 case eFormatUnsigned:
1552 if (item_byte_size <= 8)
1553 s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1556 const bool is_signed = false;
1557 const unsigned radix = 10;
1558 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1563 if (item_byte_size <= 8)
1564 s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1567 const bool is_signed = false;
1568 const unsigned radix = 8;
1569 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1575 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1577 for (i=0; i<item_byte_size; ++i)
1579 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
1581 s->Printf ("%c", ch);
1586 case '\033': s->Printf ("\\e"); break;
1587 case '\a': s->Printf ("\\a"); break;
1588 case '\b': s->Printf ("\\b"); break;
1589 case '\f': s->Printf ("\\f"); break;
1590 case '\n': s->Printf ("\\n"); break;
1591 case '\r': s->Printf ("\\r"); break;
1592 case '\t': s->Printf ("\\t"); break;
1593 case '\v': s->Printf ("\\v"); break;
1594 case '\0': s->Printf ("\\0"); break;
1595 default: s->Printf ("\\x%2.2x", ch); break;
1603 case eFormatCString:
1605 const char *cstr = GetCStr(&offset);
1610 offset = LLDB_INVALID_OFFSET;
1616 while (const char c = *cstr)
1626 case '\033': s->Printf ("\\e"); break;
1627 case '\a': s->Printf ("\\a"); break;
1628 case '\b': s->Printf ("\\b"); break;
1629 case '\f': s->Printf ("\\f"); break;
1630 case '\n': s->Printf ("\\n"); break;
1631 case '\r': s->Printf ("\\r"); break;
1632 case '\t': s->Printf ("\\t"); break;
1633 case '\v': s->Printf ("\\v"); break;
1634 default: s->Printf ("\\x%2.2x", c); break;
1647 case eFormatPointer:
1648 s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t));
1652 case eFormatComplexInteger:
1654 size_t complex_int_byte_size = item_byte_size / 2;
1656 if (complex_int_byte_size <= 8)
1658 s->Printf("%" PRIu64, GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1659 s->Printf(" + %" PRIu64 "i", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1663 s->Printf("error: unsupported byte size (%zu) for complex integer format", item_byte_size);
1669 case eFormatComplex:
1670 if (sizeof(float) * 2 == item_byte_size)
1672 float f32_1 = GetFloat (&offset);
1673 float f32_2 = GetFloat (&offset);
1675 s->Printf ("%g + %gi", f32_1, f32_2);
1678 else if (sizeof(double) * 2 == item_byte_size)
1680 double d64_1 = GetDouble (&offset);
1681 double d64_2 = GetDouble (&offset);
1683 s->Printf ("%lg + %lgi", d64_1, d64_2);
1686 else if (sizeof(long double) * 2 == item_byte_size)
1688 long double ld64_1 = GetLongDouble (&offset);
1689 long double ld64_2 = GetLongDouble (&offset);
1690 s->Printf ("%Lg + %Lgi", ld64_1, ld64_2);
1695 s->Printf("error: unsupported byte size (%zu) for complex float format", item_byte_size);
1701 case eFormatDefault:
1703 case eFormatHexUppercase:
1705 bool wantsuppercase = (item_format == eFormatHexUppercase);
1706 if (item_byte_size <= 8)
1708 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));
1712 assert (item_bit_size == 0 && item_bit_offset == 0);
1713 s->PutCString("0x");
1714 const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size);
1718 if (m_byte_order == eByteOrderBig)
1720 for (idx = 0; idx < item_byte_size; ++idx)
1721 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
1725 for (idx = 0; idx < item_byte_size; ++idx)
1726 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]);
1736 bool used_apfloat = false;
1738 target_sp = exe_scope->CalculateTarget();
1741 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1744 clang::ASTContext *ast = clang_ast->getASTContext();
1747 llvm::SmallVector<char, 256> sv;
1748 // Show full precision when printing float values
1749 const unsigned format_precision = 0;
1750 const unsigned format_max_padding = 100;
1751 size_t item_bit_size = item_byte_size * 8;
1753 if (item_bit_size == ast->getTypeSize(ast->FloatTy))
1755 llvm::APInt apint(item_bit_size, this->GetMaxU64(&offset, item_byte_size));
1756 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->FloatTy), apint);
1757 apfloat.toString(sv, format_precision, format_max_padding);
1759 else if (item_bit_size == ast->getTypeSize(ast->DoubleTy))
1762 if (GetAPInt (*this, &offset, item_byte_size, apint))
1764 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->DoubleTy), apint);
1765 apfloat.toString(sv, format_precision, format_max_padding);
1768 else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy))
1771 switch (target_sp->GetArchitecture().GetCore())
1773 case ArchSpec::eCore_x86_32_i386:
1774 case ArchSpec::eCore_x86_32_i486:
1775 case ArchSpec::eCore_x86_32_i486sx:
1776 case ArchSpec::eCore_x86_64_x86_64:
1777 // clang will assert when contructing the apfloat if we use a 16 byte integer value
1778 if (GetAPInt (*this, &offset, 10, apint))
1780 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1781 apfloat.toString(sv, format_precision, format_max_padding);
1786 if (GetAPInt (*this, &offset, item_byte_size, apint))
1788 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1789 apfloat.toString(sv, format_precision, format_max_padding);
1794 else if (item_bit_size == ast->getTypeSize(ast->HalfTy))
1796 llvm::APInt apint(item_bit_size, this->GetU16(&offset));
1797 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->HalfTy), apint);
1798 apfloat.toString(sv, format_precision, format_max_padding);
1803 s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
1804 used_apfloat = true;
1812 std::ostringstream ss;
1813 if (item_byte_size == sizeof(float) || item_byte_size == 2)
1816 if (item_byte_size == 2)
1818 uint16_t half = this->GetU16(&offset);
1819 f = half2float(half);
1823 f = GetFloat (&offset);
1825 ss.precision(std::numeric_limits<float>::digits10);
1828 else if (item_byte_size == sizeof(double))
1830 ss.precision(std::numeric_limits<double>::digits10);
1831 ss << GetDouble(&offset);
1833 else if (item_byte_size == sizeof(long double) || item_byte_size == 10)
1835 ss.precision(std::numeric_limits<long double>::digits10);
1836 ss << GetLongDouble(&offset);
1840 s->Printf("error: unsupported byte size (%zu) for float format", item_byte_size);
1844 s->Printf("%s", ss.str().c_str());
1849 case eFormatUnicode16:
1850 s->Printf("U+%4.4x", GetU16 (&offset));
1853 case eFormatUnicode32:
1854 s->Printf("U+0x%8.8x", GetU32 (&offset));
1857 case eFormatAddressInfo:
1859 addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1860 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), addr);
1863 TargetSP target_sp (exe_scope->CalculateTarget());
1864 lldb_private::Address so_addr;
1867 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1872 Address::DumpStyleResolvedDescription,
1873 Address::DumpStyleModuleWithFileAddress);
1877 so_addr.SetOffset(addr);
1878 so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription);
1885 case eFormatHexFloat:
1886 if (sizeof(float) == item_byte_size)
1888 char float_cstr[256];
1889 llvm::APFloat ap_float (GetFloat (&offset));
1890 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1891 s->Printf ("%s", float_cstr);
1894 else if (sizeof(double) == item_byte_size)
1896 char float_cstr[256];
1897 llvm::APFloat ap_float (GetDouble (&offset));
1898 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1899 s->Printf ("%s", float_cstr);
1904 s->Printf("error: unsupported byte size (%zu) for hex float format", item_byte_size);
1909 // please keep the single-item formats below in sync with FormatManager::GetSingleItemFormat
1910 // if you fail to do so, users will start getting different outputs depending on internal
1911 // implementation details they should not care about ||
1912 case eFormatVectorOfChar: // ||
1913 s->PutChar('{'); // \/
1914 offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1918 case eFormatVectorOfSInt8:
1920 offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1924 case eFormatVectorOfUInt8:
1926 offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1930 case eFormatVectorOfSInt16:
1932 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);
1936 case eFormatVectorOfUInt16:
1938 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);
1942 case eFormatVectorOfSInt32:
1944 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);
1948 case eFormatVectorOfUInt32:
1950 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);
1954 case eFormatVectorOfSInt64:
1956 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);
1960 case eFormatVectorOfUInt64:
1962 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);
1966 case eFormatVectorOfFloat32:
1968 offset = Dump (s, offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
1972 case eFormatVectorOfFloat64:
1974 offset = Dump (s, offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
1978 case eFormatVectorOfUInt128:
1980 offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
1986 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1988 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
1989 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, LLDB_INVALID_OFFSET, LLDB_INVALID_ADDRESS, 0, 0);
1991 return offset; // Return the offset at which we ended up
1994 //----------------------------------------------------------------------
1995 // Dumps bytes from this object's data to the stream "s" starting
1996 // "start_offset" bytes into this data, and ending with the byte
1997 // before "end_offset". "base_addr" will be added to the offset
1998 // into the dumped data when showing the offset into the data in the
1999 // output information. "num_per_line" objects of type "type" will
2000 // be dumped with the option to override the format for each object
2001 // with "type_format". "type_format" is a printf style formatting
2002 // string. If "type_format" is NULL, then an appropriate format
2003 // string will be used for the supplied "type". If the stream "s"
2004 // is NULL, then the output will be send to Log().
2005 //----------------------------------------------------------------------
2007 DataExtractor::PutToLog
2010 offset_t start_offset,
2013 uint32_t num_per_line,
2014 DataExtractor::Type type,
2019 return start_offset;
2022 offset_t end_offset;
2025 for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count)
2027 if ((count % num_per_line) == 0)
2029 // Print out any previous string
2030 if (sstr.GetSize() > 0)
2032 log->Printf("%s", sstr.GetData());
2035 // Reset string offset and fill the current line string with address:
2036 if (base_addr != LLDB_INVALID_ADDRESS)
2037 sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset)));
2042 case TypeUInt8: sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break;
2045 char ch = GetU8(&offset);
2046 sstr.Printf (format ? format : " %c", isprint(ch) ? ch : ' ');
2049 case TypeUInt16: sstr.Printf (format ? format : " %4.4x", GetU16(&offset)); break;
2050 case TypeUInt32: sstr.Printf (format ? format : " %8.8x", GetU32(&offset)); break;
2051 case TypeUInt64: sstr.Printf (format ? format : " %16.16" PRIx64, GetU64(&offset)); break;
2052 case TypePointer: sstr.Printf (format ? format : " 0x%" PRIx64, GetAddress(&offset)); break;
2053 case TypeULEB128: sstr.Printf (format ? format : " 0x%" PRIx64, GetULEB128(&offset)); break;
2054 case TypeSLEB128: sstr.Printf (format ? format : " %" PRId64, GetSLEB128(&offset)); break;
2058 if (sstr.GetSize() > 0)
2059 log->Printf("%s", sstr.GetData());
2061 return offset; // Return the offset at which we ended up
2064 //----------------------------------------------------------------------
2067 // Dump out a UUID starting at 'offset' bytes into the buffer
2068 //----------------------------------------------------------------------
2070 DataExtractor::DumpUUID (Stream *s, offset_t offset) const
2074 const uint8_t *uuid_data = PeekData(offset, 16);
2077 lldb_private::UUID uuid(uuid_data, 16);
2082 s->Printf("<not enough data for UUID at offset 0x%8.8" PRIx64 ">", offset);
2088 DataExtractor::DumpHexBytes (Stream *s,
2091 uint32_t bytes_per_line,
2094 DataExtractor data (src, src_len, eByteOrderLittle, 4);
2096 0, // Offset into "src"
2097 eFormatBytes, // Dump as hex bytes
2098 1, // Size of each item is 1 for single bytes
2099 src_len, // Number of bytes
2100 bytes_per_line, // Num bytes per line
2101 base_addr, // Base address
2102 0, 0); // Bitfield info
2106 DataExtractor::Copy (DataExtractor &dest_data) const
2108 if (m_data_sp.get())
2110 // we can pass along the SP to the data
2111 dest_data.SetData(m_data_sp);
2115 const uint8_t *base_ptr = m_start;
2116 size_t data_size = GetByteSize();
2117 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
2119 return GetByteSize();
2123 DataExtractor::Append(DataExtractor& rhs)
2125 if (rhs.GetByteOrder() != GetByteOrder())
2128 if (rhs.GetByteSize() == 0)
2131 if (GetByteSize() == 0)
2132 return (rhs.Copy(*this) > 0);
2134 size_t bytes = GetByteSize() + rhs.GetByteSize();
2136 DataBufferHeap *buffer_heap_ptr = NULL;
2137 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2139 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2142 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2144 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2145 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
2153 DataExtractor::Append(void* buf, offset_t length)
2161 size_t bytes = GetByteSize() + length;
2163 DataBufferHeap *buffer_heap_ptr = NULL;
2164 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2166 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2169 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2171 if (GetByteSize() > 0)
2172 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2174 memcpy(bytes_ptr + GetByteSize(), buf, length);