]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/DataExtractor.cpp
MFV r276568:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / DataExtractor.cpp
1 //===-- DataExtractor.cpp ---------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include <assert.h>
11 #include <stddef.h>
12
13 #include <bitset>
14 #include <limits>
15 #include <sstream>
16 #include <string>
17
18 #include "clang/AST/ASTContext.h"
19
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
26
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"
42
43 using namespace lldb;
44 using namespace lldb_private;
45
46 static inline uint16_t 
47 ReadInt16(const unsigned char* ptr, offset_t offset)
48 {
49     uint16_t value;
50     memcpy (&value, ptr + offset, 2);
51     return value;
52 }
53
54 static inline uint32_t
55 ReadInt32 (const unsigned char* ptr, offset_t offset = 0)
56 {
57     uint32_t value;
58     memcpy (&value, ptr + offset, 4);
59     return value;
60 }
61
62 static inline uint64_t 
63 ReadInt64(const unsigned char* ptr, offset_t offset = 0)
64 {
65     uint64_t value;
66     memcpy (&value, ptr + offset, 8);
67     return value;
68 }
69
70 static inline uint16_t
71 ReadInt16(const void* ptr)
72 {
73     uint16_t value;
74     memcpy (&value, ptr, 2);
75     return value;
76 }
77
78 static inline uint16_t
79 ReadSwapInt16(const unsigned char* ptr, offset_t offset)
80 {
81     uint16_t value;
82     memcpy (&value, ptr + offset, 2);
83     return llvm::ByteSwap_16(value);
84 }
85
86 static inline uint32_t
87 ReadSwapInt32 (const unsigned char* ptr, offset_t offset)
88 {
89     uint32_t value;
90     memcpy (&value, ptr + offset, 4);
91     return llvm::ByteSwap_32(value);
92 }
93
94 static inline uint64_t 
95 ReadSwapInt64(const unsigned char* ptr, offset_t offset) 
96 {
97     uint64_t value;
98     memcpy (&value, ptr + offset, 8);
99     return llvm::ByteSwap_64(value);
100 }
101
102 static inline uint16_t
103 ReadSwapInt16(const void* ptr)
104 {
105     uint16_t value;
106     memcpy (&value, ptr, 2);
107     return llvm::ByteSwap_16(value);
108 }
109
110 static inline uint32_t
111 ReadSwapInt32 (const void* ptr)
112 {
113     uint32_t value;
114     memcpy (&value, ptr, 4);
115     return llvm::ByteSwap_32(value);
116 }
117
118 static inline uint64_t
119 ReadSwapInt64(const void* ptr)
120 {
121     uint64_t value;
122     memcpy (&value, ptr, 8);
123     return llvm::ByteSwap_64(value);
124 }
125
126 #define NON_PRINTABLE_CHAR '.'
127 //----------------------------------------------------------------------
128 // Default constructor.
129 //----------------------------------------------------------------------
130 DataExtractor::DataExtractor () :
131     m_start     (NULL),
132     m_end       (NULL),
133     m_byte_order(lldb::endian::InlHostByteOrder()),
134     m_addr_size (4),
135     m_data_sp   ()
136 {
137 }
138
139 //----------------------------------------------------------------------
140 // This constructor allows us to use data that is owned by someone else.
141 // The data must stay around as long as this object is valid.
142 //----------------------------------------------------------------------
143 DataExtractor::DataExtractor (const void* data, offset_t length, ByteOrder endian, uint32_t addr_size) :
144     m_start     ((uint8_t*)data),
145     m_end       ((uint8_t*)data + length),
146     m_byte_order(endian),
147     m_addr_size (addr_size),
148     m_data_sp   ()
149 {
150 }
151
152 //----------------------------------------------------------------------
153 // Make a shared pointer reference to the shared data in "data_sp" and
154 // set the endian swapping setting to "swap", and the address size to
155 // "addr_size". The shared data reference will ensure the data lives
156 // as long as any DataExtractor objects exist that have a reference to
157 // this data.
158 //----------------------------------------------------------------------
159 DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint32_t addr_size) :
160     m_start     (NULL),
161     m_end       (NULL),
162     m_byte_order(endian),
163     m_addr_size (addr_size),
164     m_data_sp   ()
165 {
166     SetData (data_sp);
167 }
168
169 //----------------------------------------------------------------------
170 // Initialize this object with a subset of the data bytes in "data".
171 // If "data" contains shared data, then a reference to this shared
172 // data will added and the shared data will stay around as long
173 // as any object contains a reference to that data. The endian
174 // swap and address size settings are copied from "data".
175 //----------------------------------------------------------------------
176 DataExtractor::DataExtractor (const DataExtractor& data, offset_t offset, offset_t length) :
177     m_start(NULL),
178     m_end(NULL),
179     m_byte_order(data.m_byte_order),
180     m_addr_size(data.m_addr_size),
181     m_data_sp()
182 {
183     if (data.ValidOffset(offset))
184     {
185         offset_t bytes_available = data.GetByteSize() - offset;
186         if (length > bytes_available)
187             length = bytes_available;
188         SetData(data, offset, length);
189     }
190 }
191
192 DataExtractor::DataExtractor (const DataExtractor& rhs) :
193     m_start (rhs.m_start),
194     m_end (rhs.m_end),
195     m_byte_order (rhs.m_byte_order),
196     m_addr_size (rhs.m_addr_size),
197     m_data_sp (rhs.m_data_sp)
198 {
199 }
200
201 //----------------------------------------------------------------------
202 // Assignment operator
203 //----------------------------------------------------------------------
204 const DataExtractor&
205 DataExtractor::operator= (const DataExtractor& rhs)
206 {
207     if (this != &rhs)
208     {
209         m_start = rhs.m_start;
210         m_end = rhs.m_end;
211         m_byte_order = rhs.m_byte_order;
212         m_addr_size = rhs.m_addr_size;
213         m_data_sp = rhs.m_data_sp;
214     }
215     return *this;
216 }
217
218 //----------------------------------------------------------------------
219 // Destructor
220 //----------------------------------------------------------------------
221 DataExtractor::~DataExtractor ()
222 {
223 }
224
225 //------------------------------------------------------------------
226 // Clears the object contents back to a default invalid state, and
227 // release any references to shared data that this object may
228 // contain.
229 //------------------------------------------------------------------
230 void
231 DataExtractor::Clear ()
232 {
233     m_start = NULL;
234     m_end = NULL;
235     m_byte_order = lldb::endian::InlHostByteOrder();
236     m_addr_size = 4;
237     m_data_sp.reset();
238 }
239
240 //------------------------------------------------------------------
241 // If this object contains shared data, this function returns the
242 // offset into that shared data. Else zero is returned.
243 //------------------------------------------------------------------
244 size_t
245 DataExtractor::GetSharedDataOffset () const
246 {
247     if (m_start != NULL)
248     {
249         const DataBuffer * data = m_data_sp.get();
250         if (data != NULL)
251         {
252             const uint8_t * data_bytes = data->GetBytes();
253             if (data_bytes != NULL)
254             {
255                 assert(m_start >= data_bytes);
256                 return m_start - data_bytes;
257             }
258         }
259     }
260     return 0;
261 }
262
263 //----------------------------------------------------------------------
264 // Set the data with which this object will extract from to data
265 // starting at BYTES and set the length of the data to LENGTH bytes
266 // long. The data is externally owned must be around at least as
267 // long as this object points to the data. No copy of the data is
268 // made, this object just refers to this data and can extract from
269 // it. If this object refers to any shared data upon entry, the
270 // reference to that data will be released. Is SWAP is set to true,
271 // any data extracted will be endian swapped.
272 //----------------------------------------------------------------------
273 lldb::offset_t
274 DataExtractor::SetData (const void *bytes, offset_t length, ByteOrder endian)
275 {
276     m_byte_order = endian;
277     m_data_sp.reset();
278     if (bytes == NULL || length == 0)
279     {
280         m_start = NULL;
281         m_end = NULL;
282     }
283     else
284     {
285         m_start = (uint8_t *)bytes;
286         m_end = m_start + length;
287     }
288     return GetByteSize();
289 }
290
291 //----------------------------------------------------------------------
292 // Assign the data for this object to be a subrange in "data"
293 // starting "data_offset" bytes into "data" and ending "data_length"
294 // bytes later. If "data_offset" is not a valid offset into "data",
295 // then this object will contain no bytes. If "data_offset" is
296 // within "data" yet "data_length" is too large, the length will be
297 // capped at the number of bytes remaining in "data". If "data"
298 // contains a shared pointer to other data, then a ref counted
299 // pointer to that data will be made in this object. If "data"
300 // doesn't contain a shared pointer to data, then the bytes referred
301 // to in "data" will need to exist at least as long as this object
302 // refers to those bytes. The address size and endian swap settings
303 // are copied from the current values in "data".
304 //----------------------------------------------------------------------
305 lldb::offset_t
306 DataExtractor::SetData (const DataExtractor& data, offset_t data_offset, offset_t data_length)
307 {
308     m_addr_size = data.m_addr_size;
309     // If "data" contains shared pointer to data, then we can use that
310     if (data.m_data_sp.get())
311     {
312         m_byte_order = data.m_byte_order;
313         return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length);
314     }
315
316     // We have a DataExtractor object that just has a pointer to bytes
317     if (data.ValidOffset(data_offset))
318     {
319         if (data_length > data.GetByteSize() - data_offset)
320             data_length = data.GetByteSize() - data_offset;
321         return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder());
322     }
323     return 0;
324 }
325
326 //----------------------------------------------------------------------
327 // Assign the data for this object to be a subrange of the shared
328 // data in "data_sp" starting "data_offset" bytes into "data_sp"
329 // and ending "data_length" bytes later. If "data_offset" is not
330 // a valid offset into "data_sp", then this object will contain no
331 // bytes. If "data_offset" is within "data_sp" yet "data_length" is
332 // too large, the length will be capped at the number of bytes
333 // remaining in "data_sp". A ref counted pointer to the data in
334 // "data_sp" will be made in this object IF the number of bytes this
335 // object refers to in greater than zero (if at least one byte was
336 // available starting at "data_offset") to ensure the data stays
337 // around as long as it is needed. The address size and endian swap
338 // settings will remain unchanged from their current settings.
339 //----------------------------------------------------------------------
340 lldb::offset_t
341 DataExtractor::SetData (const DataBufferSP& data_sp, offset_t data_offset, offset_t data_length)
342 {
343     m_start = m_end = NULL;
344
345     if (data_length > 0)
346     {
347         m_data_sp = data_sp;
348         if (data_sp.get())
349         {
350             const size_t data_size = data_sp->GetByteSize();
351             if (data_offset < data_size)
352             {
353                 m_start = data_sp->GetBytes() + data_offset;
354                 const size_t bytes_left = data_size - data_offset;
355                 // Cap the length of we asked for too many
356                 if (data_length <= bytes_left)
357                     m_end = m_start + data_length;  // We got all the bytes we wanted
358                 else
359                     m_end = m_start + bytes_left;   // Not all the bytes requested were available in the shared data
360             }
361         }
362     }
363
364     size_t new_size = GetByteSize();
365
366     // Don't hold a shared pointer to the data buffer if we don't share
367     // any valid bytes in the shared buffer.
368     if (new_size == 0)
369         m_data_sp.reset();
370
371     return new_size;
372 }
373
374 //----------------------------------------------------------------------
375 // Extract a single unsigned char from the binary data and update
376 // the offset pointed to by "offset_ptr".
377 //
378 // RETURNS the byte that was extracted, or zero on failure.
379 //----------------------------------------------------------------------
380 uint8_t
381 DataExtractor::GetU8 (offset_t *offset_ptr) const
382 {
383     const uint8_t *data = (const uint8_t *)GetData (offset_ptr, 1);
384     if (data)
385         return *data;
386     return 0;
387 }
388
389 //----------------------------------------------------------------------
390 // Extract "count" unsigned chars from the binary data and update the
391 // offset pointed to by "offset_ptr". The extracted data is copied into
392 // "dst".
393 //
394 // RETURNS the non-NULL buffer pointer upon successful extraction of
395 // all the requested bytes, or NULL when the data is not available in
396 // the buffer due to being out of bounds, or insufficient data.
397 //----------------------------------------------------------------------
398 void *
399 DataExtractor::GetU8 (offset_t *offset_ptr, void *dst, uint32_t count) const
400 {
401     const uint8_t *data = (const uint8_t *)GetData (offset_ptr, count);
402     if (data)
403     {
404         // Copy the data into the buffer
405         memcpy (dst, data, count);
406         // Return a non-NULL pointer to the converted data as an indicator of success
407         return dst;
408     }
409     return NULL;
410 }
411
412 //----------------------------------------------------------------------
413 // Extract a single uint16_t from the data and update the offset
414 // pointed to by "offset_ptr".
415 //
416 // RETURNS the uint16_t that was extracted, or zero on failure.
417 //----------------------------------------------------------------------
418 uint16_t
419 DataExtractor::GetU16 (offset_t *offset_ptr) const
420 {
421     uint16_t val = 0;
422     const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
423     if (data)
424     {
425         if (m_byte_order != lldb::endian::InlHostByteOrder())
426             val = ReadSwapInt16(data);
427         else
428             val = ReadInt16 (data);
429     }
430     return val;
431 }
432
433 uint16_t
434 DataExtractor::GetU16_unchecked (offset_t *offset_ptr) const
435 {
436     uint16_t val;
437     if (m_byte_order == lldb::endian::InlHostByteOrder())
438         val = ReadInt16 (m_start, *offset_ptr);
439     else
440         val = ReadSwapInt16(m_start, *offset_ptr);
441     *offset_ptr += sizeof(val);
442     return val;
443 }
444
445 uint32_t
446 DataExtractor::GetU32_unchecked (offset_t *offset_ptr) const
447 {
448     uint32_t val;
449     if (m_byte_order == lldb::endian::InlHostByteOrder())
450         val = ReadInt32 (m_start, *offset_ptr);
451     else
452         val =  ReadSwapInt32 (m_start, *offset_ptr);
453     *offset_ptr += sizeof(val);
454     return val;
455 }
456
457 uint64_t
458 DataExtractor::GetU64_unchecked (offset_t *offset_ptr) const
459 {
460     uint64_t val;
461     if (m_byte_order == lldb::endian::InlHostByteOrder())
462         val = ReadInt64 (m_start, *offset_ptr);
463     else
464         val = ReadSwapInt64 (m_start, *offset_ptr);
465     *offset_ptr += sizeof(val);
466     return val;
467 }
468
469
470 //----------------------------------------------------------------------
471 // Extract "count" uint16_t values from the binary data and update
472 // the offset pointed to by "offset_ptr". The extracted data is
473 // copied into "dst".
474 //
475 // RETURNS the non-NULL buffer pointer upon successful extraction of
476 // all the requested bytes, or NULL when the data is not available
477 // in the buffer due to being out of bounds, or insufficient data.
478 //----------------------------------------------------------------------
479 void *
480 DataExtractor::GetU16 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
481 {
482     const size_t src_size = sizeof(uint16_t) * count;
483     const uint16_t *src = (const uint16_t *)GetData (offset_ptr, src_size);
484     if (src)
485     {
486         if (m_byte_order != lldb::endian::InlHostByteOrder())
487         {
488             uint16_t *dst_pos = (uint16_t *)void_dst;
489             uint16_t *dst_end = dst_pos + count;
490             const uint16_t *src_pos = src;
491             while (dst_pos < dst_end)
492             {
493                 *dst_pos = ReadSwapInt16 (src_pos);
494                 ++dst_pos;
495                 ++src_pos;
496             }
497         }
498         else
499         {
500             memcpy (void_dst, src, src_size);
501         }
502         // Return a non-NULL pointer to the converted data as an indicator of success
503         return void_dst;
504     }
505     return NULL;
506 }
507
508 //----------------------------------------------------------------------
509 // Extract a single uint32_t from the data and update the offset
510 // pointed to by "offset_ptr".
511 //
512 // RETURNS the uint32_t that was extracted, or zero on failure.
513 //----------------------------------------------------------------------
514 uint32_t
515 DataExtractor::GetU32 (offset_t *offset_ptr) const
516 {
517     uint32_t val = 0;
518     const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
519     if (data)
520     {
521         if (m_byte_order != lldb::endian::InlHostByteOrder())
522         {
523             val = ReadSwapInt32 (data);
524         }
525         else
526         {
527             memcpy (&val, data, 4);
528         }
529     }
530     return val;
531 }
532
533 //----------------------------------------------------------------------
534 // Extract "count" uint32_t values from the binary data and update
535 // the offset pointed to by "offset_ptr". The extracted data is
536 // copied into "dst".
537 //
538 // RETURNS the non-NULL buffer pointer upon successful extraction of
539 // all the requested bytes, or NULL when the data is not available
540 // in the buffer due to being out of bounds, or insufficient data.
541 //----------------------------------------------------------------------
542 void *
543 DataExtractor::GetU32 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
544 {
545     const size_t src_size = sizeof(uint32_t) * count;
546     const uint32_t *src = (const uint32_t *)GetData (offset_ptr, src_size);
547     if (src)
548     {
549         if (m_byte_order != lldb::endian::InlHostByteOrder())
550         {
551             uint32_t *dst_pos = (uint32_t *)void_dst;
552             uint32_t *dst_end = dst_pos + count;
553             const uint32_t *src_pos = src;
554             while (dst_pos < dst_end)
555             {
556                 *dst_pos = ReadSwapInt32 (src_pos);
557                 ++dst_pos;
558                 ++src_pos;
559             }
560         }
561         else
562         {
563             memcpy (void_dst, src, src_size);
564         }
565         // Return a non-NULL pointer to the converted data as an indicator of success
566         return void_dst;
567     }
568     return NULL;
569 }
570
571 //----------------------------------------------------------------------
572 // Extract a single uint64_t from the data and update the offset
573 // pointed to by "offset_ptr".
574 //
575 // RETURNS the uint64_t that was extracted, or zero on failure.
576 //----------------------------------------------------------------------
577 uint64_t
578 DataExtractor::GetU64 (offset_t *offset_ptr) const
579 {
580     uint64_t val = 0;
581     const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
582     if (data)
583     {
584         if (m_byte_order != lldb::endian::InlHostByteOrder())
585         {
586             val = ReadSwapInt64 (data);
587         }
588         else
589         {
590             memcpy (&val, data, 8);
591         }
592     }
593     return val;
594 }
595
596 //----------------------------------------------------------------------
597 // GetU64
598 //
599 // Get multiple consecutive 64 bit values. Return true if the entire
600 // read succeeds and increment the offset pointed to by offset_ptr, else
601 // return false and leave the offset pointed to by offset_ptr unchanged.
602 //----------------------------------------------------------------------
603 void *
604 DataExtractor::GetU64 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
605 {
606     const size_t src_size = sizeof(uint64_t) * count;
607     const uint64_t *src = (const uint64_t *)GetData (offset_ptr, src_size);
608     if (src)
609     {
610         if (m_byte_order != lldb::endian::InlHostByteOrder())
611         {
612             uint64_t *dst_pos = (uint64_t *)void_dst;
613             uint64_t *dst_end = dst_pos + count;
614             const uint64_t *src_pos = src;
615             while (dst_pos < dst_end)
616             {
617                 *dst_pos = ReadSwapInt64 (src_pos);
618                 ++dst_pos;
619                 ++src_pos;
620             }
621         }
622         else
623         {
624             memcpy (void_dst, src, src_size);
625         }
626         // Return a non-NULL pointer to the converted data as an indicator of success
627         return void_dst;
628     }
629     return NULL;
630 }
631
632 //----------------------------------------------------------------------
633 // Extract a single integer value from the data and update the offset
634 // pointed to by "offset_ptr". The size of the extracted integer
635 // is specified by the "byte_size" argument. "byte_size" should have
636 // a value between 1 and 4 since the return value is only 32 bits
637 // wide. Any "byte_size" values less than 1 or greater than 4 will
638 // result in nothing being extracted, and zero being returned.
639 //
640 // RETURNS the integer value that was extracted, or zero on failure.
641 //----------------------------------------------------------------------
642 uint32_t
643 DataExtractor::GetMaxU32 (offset_t *offset_ptr, size_t byte_size) const
644 {
645     switch (byte_size)
646     {
647     case 1: return GetU8 (offset_ptr); break;
648     case 2: return GetU16(offset_ptr); break;
649     case 4: return GetU32(offset_ptr); break;
650     default:
651         assert("GetMaxU32 unhandled case!" == NULL);
652         break;
653     }
654     return 0;
655 }
656
657 //----------------------------------------------------------------------
658 // Extract a single integer value from the data and update the offset
659 // pointed to by "offset_ptr". The size of the extracted integer
660 // is specified by the "byte_size" argument. "byte_size" should have
661 // a value >= 1 and <= 8 since the return value is only 64 bits
662 // wide. Any "byte_size" values less than 1 or greater than 8 will
663 // result in nothing being extracted, and zero being returned.
664 //
665 // RETURNS the integer value that was extracted, or zero on failure.
666 //----------------------------------------------------------------------
667 uint64_t
668 DataExtractor::GetMaxU64 (offset_t *offset_ptr, size_t size) const
669 {
670     switch (size)
671     {
672     case 1: return GetU8 (offset_ptr); break;
673     case 2: return GetU16(offset_ptr); break;
674     case 4: return GetU32(offset_ptr); break;
675     case 8: return GetU64(offset_ptr); break;
676     default:
677         assert("GetMax64 unhandled case!" == NULL);
678         break;
679     }
680     return 0;
681 }
682
683 uint64_t
684 DataExtractor::GetMaxU64_unchecked (offset_t *offset_ptr, size_t size) const
685 {
686     switch (size)
687     {
688         case 1: return GetU8_unchecked  (offset_ptr); break;
689         case 2: return GetU16_unchecked (offset_ptr); break;
690         case 4: return GetU32_unchecked (offset_ptr); break;
691         case 8: return GetU64_unchecked (offset_ptr); break;
692         default:
693             assert("GetMax64 unhandled case!" == NULL);
694             break;
695     }
696     return 0;
697 }
698
699 int64_t
700 DataExtractor::GetMaxS64 (offset_t *offset_ptr, size_t size) const
701 {
702     switch (size)
703     {
704     case 1: return (int8_t)GetU8 (offset_ptr); break;
705     case 2: return (int16_t)GetU16(offset_ptr); break;
706     case 4: return (int32_t)GetU32(offset_ptr); break;
707     case 8: return (int64_t)GetU64(offset_ptr); break;
708     default:
709         assert("GetMax64 unhandled case!" == NULL);
710         break;
711     }
712     return 0;
713 }
714
715 uint64_t
716 DataExtractor::GetMaxU64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
717 {
718     uint64_t uval64 = GetMaxU64 (offset_ptr, size);
719     if (bitfield_bit_size > 0)
720     {
721         if (bitfield_bit_offset > 0)
722             uval64 >>= bitfield_bit_offset;
723         uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
724         if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
725             return uval64;
726         uval64 &= bitfield_mask;
727     }
728     return uval64;
729 }
730
731 int64_t
732 DataExtractor::GetMaxS64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
733 {
734     int64_t sval64 = GetMaxS64 (offset_ptr, size);
735     if (bitfield_bit_size > 0)
736     {
737         if (bitfield_bit_offset > 0)
738             sval64 >>= bitfield_bit_offset;
739         uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
740         sval64 &= bitfield_mask;
741         // sign extend if needed
742         if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
743             sval64 |= ~bitfield_mask;
744     }
745     return sval64;
746 }
747
748
749 float
750 DataExtractor::GetFloat (offset_t *offset_ptr) const
751 {
752     typedef float float_type;
753     float_type val = 0.0;
754     const size_t src_size = sizeof(float_type);
755     const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
756     if (src)
757     {
758         if (m_byte_order != lldb::endian::InlHostByteOrder())
759         {
760             const uint8_t *src_data = (const uint8_t *)src;
761             uint8_t *dst_data = (uint8_t *)&val;
762             for (size_t i=0; i<sizeof(float_type); ++i)
763                 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
764         }
765         else
766         {
767             val = *src;
768         }
769     }
770     return val;
771 }
772
773 double
774 DataExtractor::GetDouble (offset_t *offset_ptr) const
775 {
776     typedef double float_type;
777     float_type val = 0.0;
778     const size_t src_size = sizeof(float_type);
779     const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
780     if (src)
781     {
782         if (m_byte_order != lldb::endian::InlHostByteOrder())
783         {
784             const uint8_t *src_data = (const uint8_t *)src;
785             uint8_t *dst_data = (uint8_t *)&val;
786             for (size_t i=0; i<sizeof(float_type); ++i)
787                 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
788         }
789         else
790         {
791             val = *src;
792         }
793     }
794     return val;
795 }
796
797
798 long double
799 DataExtractor::GetLongDouble (offset_t *offset_ptr) const
800 {
801     long double val = 0.0;
802 #if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
803     *offset_ptr += CopyByteOrderedData (*offset_ptr, 10, &val, sizeof(val), lldb::endian::InlHostByteOrder());
804 #else
805     *offset_ptr += CopyByteOrderedData (*offset_ptr, sizeof(val), &val, sizeof(val), lldb::endian::InlHostByteOrder());
806 #endif
807     return val;
808 }
809
810
811 //------------------------------------------------------------------
812 // Extract a single address from the data and update the offset
813 // pointed to by "offset_ptr". The size of the extracted address
814 // comes from the "this->m_addr_size" member variable and should be
815 // set correctly prior to extracting any address values.
816 //
817 // RETURNS the address that was extracted, or zero on failure.
818 //------------------------------------------------------------------
819 uint64_t
820 DataExtractor::GetAddress (offset_t *offset_ptr) const
821 {
822     return GetMaxU64 (offset_ptr, m_addr_size);
823 }
824
825 uint64_t
826 DataExtractor::GetAddress_unchecked (offset_t *offset_ptr) const
827 {
828     return GetMaxU64_unchecked (offset_ptr, m_addr_size);
829 }
830
831 //------------------------------------------------------------------
832 // Extract a single pointer from the data and update the offset
833 // pointed to by "offset_ptr". The size of the extracted pointer
834 // comes from the "this->m_addr_size" member variable and should be
835 // set correctly prior to extracting any pointer values.
836 //
837 // RETURNS the pointer that was extracted, or zero on failure.
838 //------------------------------------------------------------------
839 uint64_t
840 DataExtractor::GetPointer (offset_t *offset_ptr) const
841 {
842     return GetMaxU64 (offset_ptr, m_addr_size);
843 }
844
845 //----------------------------------------------------------------------
846 // GetDwarfEHPtr
847 //
848 // Used for calls when the value type is specified by a DWARF EH Frame
849 // pointer encoding.
850 //----------------------------------------------------------------------
851
852 uint64_t
853 DataExtractor::GetGNUEHPointer (offset_t *offset_ptr, uint32_t eh_ptr_enc, lldb::addr_t pc_rel_addr, lldb::addr_t text_addr, lldb::addr_t data_addr)//, BSDRelocs *data_relocs) const
854 {
855     if (eh_ptr_enc == DW_EH_PE_omit)
856         return ULLONG_MAX;  // Value isn't in the buffer...
857
858     uint64_t baseAddress = 0;
859     uint64_t addressValue = 0;
860     const uint32_t addr_size = GetAddressByteSize();
861
862     bool signExtendValue = false;
863     // Decode the base part or adjust our offset
864     switch (eh_ptr_enc & 0x70)
865     {
866     case DW_EH_PE_pcrel:
867         signExtendValue = true;
868         baseAddress = *offset_ptr;
869         if (pc_rel_addr != LLDB_INVALID_ADDRESS)
870             baseAddress += pc_rel_addr;
871 //      else
872 //          Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address.");
873         break;
874
875     case DW_EH_PE_textrel:
876         signExtendValue = true;
877         if (text_addr != LLDB_INVALID_ADDRESS)
878             baseAddress = text_addr;
879 //      else
880 //          Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero.");
881         break;
882
883     case DW_EH_PE_datarel:
884         signExtendValue = true;
885         if (data_addr != LLDB_INVALID_ADDRESS)
886             baseAddress = data_addr;
887 //      else
888 //          Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero.");
889         break;
890
891     case DW_EH_PE_funcrel:
892         signExtendValue = true;
893         break;
894
895     case DW_EH_PE_aligned:
896         {
897             // SetPointerSize should be called prior to extracting these so the
898             // pointer size is cached
899             assert(addr_size != 0);
900             if (addr_size)
901             {
902                 // Align to a address size boundary first
903                 uint32_t alignOffset = *offset_ptr % addr_size;
904                 if (alignOffset)
905                     offset_ptr += addr_size - alignOffset;
906             }
907         }
908         break;
909
910     default:
911     break;
912     }
913
914     // Decode the value part
915     switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING)
916     {
917     case DW_EH_PE_absptr    :
918         {
919             addressValue = GetAddress (offset_ptr);
920 //          if (data_relocs)
921 //              addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue);
922         }
923         break;
924     case DW_EH_PE_uleb128   : addressValue = GetULEB128(offset_ptr);        break;
925     case DW_EH_PE_udata2    : addressValue = GetU16(offset_ptr);            break;
926     case DW_EH_PE_udata4    : addressValue = GetU32(offset_ptr);            break;
927     case DW_EH_PE_udata8    : addressValue = GetU64(offset_ptr);            break;
928     case DW_EH_PE_sleb128   : addressValue = GetSLEB128(offset_ptr);        break;
929     case DW_EH_PE_sdata2    : addressValue = (int16_t)GetU16(offset_ptr);   break;
930     case DW_EH_PE_sdata4    : addressValue = (int32_t)GetU32(offset_ptr);   break;
931     case DW_EH_PE_sdata8    : addressValue = (int64_t)GetU64(offset_ptr);   break;
932     default:
933     // Unhandled encoding type
934     assert(eh_ptr_enc);
935     break;
936     }
937
938     // Since we promote everything to 64 bit, we may need to sign extend
939     if (signExtendValue && addr_size < sizeof(baseAddress))
940     {
941         uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
942         if (sign_bit & addressValue)
943         {
944             uint64_t mask = ~sign_bit + 1;
945             addressValue |= mask;
946         }
947     }
948     return baseAddress + addressValue;
949 }
950
951 size_t
952 DataExtractor::ExtractBytes (offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const
953 {
954     const uint8_t *src = PeekData (offset, length);
955     if (src)
956     {
957         if (dst_byte_order != GetByteOrder())
958         {
959             // Validate that only a word- or register-sized dst is byte swapped
960             assert (length == 1 || length == 2 || length == 4 || length == 8 ||
961                     length == 10 || length == 16 || length == 32);
962
963             for (uint32_t i=0; i<length; ++i)
964                 ((uint8_t*)dst)[i] = src[length - i - 1];
965         }
966         else
967             ::memcpy (dst, src, length);
968         return length;
969     }
970     return 0;
971 }
972
973 // Extract data as it exists in target memory
974 lldb::offset_t
975 DataExtractor::CopyData (offset_t offset,
976                          offset_t length,
977                          void *dst) const
978 {
979     const uint8_t *src = PeekData (offset, length);
980     if (src)
981     {
982         ::memcpy (dst, src, length);
983         return length;
984     }
985     return 0;
986 }
987
988 // Extract data and swap if needed when doing the copy
989 lldb::offset_t
990 DataExtractor::CopyByteOrderedData (offset_t src_offset,
991                                     offset_t src_len,
992                                     void *dst_void_ptr, 
993                                     offset_t dst_len, 
994                                     ByteOrder dst_byte_order) const
995 {
996     // Validate the source info
997     if (!ValidOffsetForDataOfSize(src_offset, src_len))
998         assert (ValidOffsetForDataOfSize(src_offset, src_len));
999     assert (src_len > 0);
1000     assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
1001
1002     // Validate the destination info
1003     assert (dst_void_ptr != NULL);
1004     assert (dst_len > 0);
1005     assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
1006
1007     // Validate that only a word- or register-sized dst is byte swapped
1008     assert (dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
1009             dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
1010             dst_len == 32);
1011
1012     // Must have valid byte orders set in this object and for destination
1013     if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) ||
1014         !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
1015         return 0;
1016
1017     uint32_t i;
1018     uint8_t* dst = (uint8_t*)dst_void_ptr;
1019     const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len);
1020     if (src)
1021     {
1022         if (dst_len >= src_len)
1023         {
1024             // We are copying the entire value from src into dst.
1025             // Calculate how many, if any, zeroes we need for the most 
1026             // significant bytes if "dst_len" is greater than "src_len"...
1027             const size_t num_zeroes = dst_len - src_len;
1028             if (dst_byte_order == eByteOrderBig)
1029             {
1030                 // Big endian, so we lead with zeroes...
1031                 if (num_zeroes > 0)
1032                     ::memset (dst, 0, num_zeroes);
1033                 // Then either copy or swap the rest
1034                 if (m_byte_order == eByteOrderBig)
1035                 {
1036                     ::memcpy (dst + num_zeroes, src, src_len);
1037                 }
1038                 else
1039                 {
1040                     for (i=0; i<src_len; ++i)
1041                         dst[i+num_zeroes] = src[src_len - 1 - i];
1042                 }
1043             }
1044             else
1045             {
1046                 // Little endian destination, so we lead the value bytes
1047                 if (m_byte_order == eByteOrderBig)
1048                 {
1049                     for (i=0; i<src_len; ++i)
1050                         dst[i] = src[src_len - 1 - i];
1051                 }
1052                 else
1053                 {
1054                     ::memcpy (dst, src, src_len);
1055                 }
1056                 // And zero the rest...
1057                 if (num_zeroes > 0)
1058                     ::memset (dst + src_len, 0, num_zeroes);
1059             }
1060             return src_len;
1061         }
1062         else
1063         {
1064             // We are only copying some of the value from src into dst..
1065
1066             if (dst_byte_order == eByteOrderBig)
1067             {
1068                 // Big endian dst
1069                 if (m_byte_order == eByteOrderBig)
1070                 {
1071                     // Big endian dst, with big endian src
1072                     ::memcpy (dst, src + (src_len - dst_len), dst_len);
1073                 }
1074                 else
1075                 {
1076                     // Big endian dst, with little endian src
1077                     for (i=0; i<dst_len; ++i)
1078                         dst[i] = src[dst_len - 1 - i];
1079                 }
1080             }
1081             else
1082             {
1083                 // Little endian dst
1084                 if (m_byte_order == eByteOrderBig)
1085                 {
1086                     // Little endian dst, with big endian src
1087                     for (i=0; i<dst_len; ++i)
1088                         dst[i] = src[src_len - 1 - i];
1089                 }
1090                 else
1091                 {
1092                     // Little endian dst, with big endian src
1093                     ::memcpy (dst, src, dst_len);
1094                 }
1095             }
1096             return dst_len;
1097         }            
1098
1099     }
1100     return 0;
1101 }
1102
1103
1104 //----------------------------------------------------------------------
1105 // Extracts a variable length NULL terminated C string from
1106 // the data at the offset pointed to by "offset_ptr".  The
1107 // "offset_ptr" will be updated with the offset of the byte that
1108 // follows the NULL terminator byte.
1109 //
1110 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1111 // "length" is non-zero and there aren't enough available
1112 // bytes, NULL will be returned and "offset_ptr" will not be
1113 // updated.
1114 //----------------------------------------------------------------------
1115 const char*
1116 DataExtractor::GetCStr (offset_t *offset_ptr) const
1117 {
1118     const char *cstr = (const char *)PeekData (*offset_ptr, 1);
1119     if (cstr)
1120     {
1121         const char *cstr_end = cstr;
1122         const char *end = (const char *)m_end;
1123         while (cstr_end < end && *cstr_end)
1124             ++cstr_end;
1125
1126         // Now we are either at the end of the data or we point to the
1127         // NULL C string terminator with cstr_end...
1128         if (*cstr_end == '\0')
1129         {
1130             // Advance the offset with one extra byte for the NULL terminator
1131             *offset_ptr += (cstr_end - cstr + 1);
1132             return cstr;
1133         }
1134         
1135         // We reached the end of the data without finding a NULL C string
1136         // terminator. Fall through and return NULL otherwise anyone that
1137         // would have used the result as a C string can wander into
1138         // unknown memory...
1139     }
1140     return NULL;
1141 }
1142
1143 //----------------------------------------------------------------------
1144 // Extracts a NULL terminated C string from the fixed length field of
1145 // length "len" at the offset pointed to by "offset_ptr".
1146 // The "offset_ptr" will be updated with the offset of the byte that
1147 // follows the fixed length field.
1148 //
1149 // If the offset pointed to by "offset_ptr" is out of bounds, or if
1150 // the offset plus the length of the field is out of bounds, or if the
1151 // field does not contain a NULL terminator byte, NULL will be returned
1152 // and "offset_ptr" will not be updated.
1153 //----------------------------------------------------------------------
1154 const char*
1155 DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const
1156 {
1157     const char *cstr = (const char *)PeekData (*offset_ptr, len);
1158     if (cstr)
1159     {
1160         if (memchr (cstr, '\0', len) == NULL)
1161         {
1162             return NULL;
1163         }
1164         *offset_ptr += len;
1165         return cstr;
1166     }
1167     return NULL;
1168 }
1169
1170 //------------------------------------------------------------------
1171 // Peeks at a string in the contained data. No verification is done
1172 // to make sure the entire string lies within the bounds of this
1173 // object's data, only "offset" is verified to be a valid offset.
1174 //
1175 // Returns a valid C string pointer if "offset" is a valid offset in
1176 // this object's data, else NULL is returned.
1177 //------------------------------------------------------------------
1178 const char *
1179 DataExtractor::PeekCStr (offset_t offset) const
1180 {
1181     return (const char *)PeekData (offset, 1);
1182 }
1183
1184 //----------------------------------------------------------------------
1185 // Extracts an unsigned LEB128 number from this object's data
1186 // starting at the offset pointed to by "offset_ptr". The offset
1187 // pointed to by "offset_ptr" will be updated with the offset of the
1188 // byte following the last extracted byte.
1189 //
1190 // Returned the extracted integer value.
1191 //----------------------------------------------------------------------
1192 uint64_t
1193 DataExtractor::GetULEB128 (offset_t *offset_ptr) const
1194 {
1195     const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1196     if (src == NULL)
1197         return 0;
1198     
1199     const uint8_t *end = m_end;
1200     
1201     if (src < end)
1202     {
1203         uint64_t result = *src++;
1204         if (result >= 0x80)
1205         {
1206             result &= 0x7f;
1207             int shift = 7;
1208             while (src < end)
1209             {
1210                 uint8_t byte = *src++;
1211                 result |= (byte & 0x7f) << shift;
1212                 if ((byte & 0x80) == 0)
1213                     break;
1214                 shift += 7;
1215             }
1216         }
1217         *offset_ptr = src - m_start;
1218         return result;
1219     }
1220     
1221     return 0;
1222 }
1223
1224 //----------------------------------------------------------------------
1225 // Extracts an signed LEB128 number from this object's data
1226 // starting at the offset pointed to by "offset_ptr". The offset
1227 // pointed to by "offset_ptr" will be updated with the offset of the
1228 // byte following the last extracted byte.
1229 //
1230 // Returned the extracted integer value.
1231 //----------------------------------------------------------------------
1232 int64_t
1233 DataExtractor::GetSLEB128 (offset_t *offset_ptr) const
1234 {
1235     const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1236     if (src == NULL)
1237         return 0;
1238     
1239     const uint8_t *end = m_end;
1240     
1241     if (src < end)
1242     {
1243         int64_t result = 0;
1244         int shift = 0;
1245         int size = sizeof (int64_t) * 8;
1246
1247         uint8_t byte = 0;
1248         int bytecount = 0;
1249
1250         while (src < end)
1251         {
1252             bytecount++;
1253             byte = *src++;
1254             result |= (byte & 0x7f) << shift;
1255             shift += 7;
1256             if ((byte & 0x80) == 0)
1257                 break;
1258         }
1259
1260         // Sign bit of byte is 2nd high order bit (0x40)
1261         if (shift < size && (byte & 0x40))
1262             result |= - (1 << shift);
1263
1264         *offset_ptr += bytecount;
1265         return result;
1266     }
1267     return 0;
1268 }
1269
1270 //----------------------------------------------------------------------
1271 // Skips a ULEB128 number (signed or unsigned) from this object's
1272 // data starting at the offset pointed to by "offset_ptr". The
1273 // offset pointed to by "offset_ptr" will be updated with the offset
1274 // of the byte following the last extracted byte.
1275 //
1276 // Returns the number of bytes consumed during the extraction.
1277 //----------------------------------------------------------------------
1278 uint32_t
1279 DataExtractor::Skip_LEB128 (offset_t *offset_ptr) const
1280 {
1281     uint32_t bytes_consumed = 0;
1282     const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1283     if (src == NULL)
1284         return 0;
1285         
1286     const uint8_t *end = m_end;
1287     
1288     if (src < end)
1289     {
1290         const uint8_t *src_pos = src;
1291         while ((src_pos < end) && (*src_pos++ & 0x80))
1292             ++bytes_consumed;
1293         *offset_ptr += src_pos - src;
1294     }
1295     return bytes_consumed;
1296 }
1297
1298 static bool
1299 GetAPInt (const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size, llvm::APInt &result)
1300 {
1301     llvm::SmallVector<uint64_t, 2> uint64_array;
1302     lldb::offset_t bytes_left = byte_size;
1303     uint64_t u64;
1304     const lldb::ByteOrder byte_order = data.GetByteOrder();
1305     if (byte_order == lldb::eByteOrderLittle)
1306     {
1307         while (bytes_left > 0)
1308         {
1309             if (bytes_left >= 8)
1310             {
1311                 u64 = data.GetU64(offset_ptr);
1312                 bytes_left -= 8;
1313             }
1314             else
1315             {
1316                 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
1317                 bytes_left = 0;
1318             }
1319             uint64_array.push_back(u64);
1320         }
1321         result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1322         return true;
1323     }
1324     else if (byte_order == lldb::eByteOrderBig)
1325     {
1326         lldb::offset_t be_offset = *offset_ptr + byte_size;
1327         lldb::offset_t temp_offset;
1328         while (bytes_left > 0)
1329         {
1330             if (bytes_left >= 8)
1331             {
1332                 be_offset -= 8;
1333                 temp_offset = be_offset;
1334                 u64 = data.GetU64(&temp_offset);
1335                 bytes_left -= 8;
1336             }
1337             else
1338             {
1339                 be_offset -= bytes_left;
1340                 temp_offset = be_offset;
1341                 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
1342                 bytes_left = 0;
1343             }
1344             uint64_array.push_back(u64);
1345         }
1346         *offset_ptr += byte_size;
1347         result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1348         return true;
1349     }
1350     return false;
1351 }
1352
1353 static lldb::offset_t
1354 DumpAPInt (Stream *s, const DataExtractor &data, lldb::offset_t offset, lldb::offset_t byte_size, bool is_signed, unsigned radix)
1355 {
1356     llvm::APInt apint;
1357     if (GetAPInt (data, &offset, byte_size, apint))
1358     {
1359         std::string apint_str(apint.toString(radix, is_signed));
1360         switch (radix)
1361         {
1362             case 2:
1363                 s->Write ("0b", 2);
1364                 break;
1365             case 8:
1366                 s->Write ("0", 1);
1367                 break;
1368             case 10:
1369                 break;
1370         }
1371         s->Write(apint_str.c_str(), apint_str.size());
1372     }
1373     return offset;
1374 }
1375
1376 static float half2float (uint16_t half)
1377 {
1378 #ifdef _MSC_VER
1379     llvm_unreachable("half2float not implemented for MSVC");
1380 #else
1381     union{ float       f; uint32_t    u;}u;
1382     int32_t v = (int16_t) half;
1383     
1384     if( 0 == (v & 0x7c00))
1385     {
1386         u.u = v & 0x80007FFFU;
1387         return u.f * ldexpf(1, 125);
1388     }
1389     
1390     v <<= 13;
1391     u.u = v | 0x70000000U;
1392     return u.f * ldexpf(1, -112);
1393 #endif
1394 }
1395
1396 lldb::offset_t
1397 DataExtractor::Dump (Stream *s,
1398                      offset_t start_offset,
1399                      lldb::Format item_format,
1400                      size_t item_byte_size,
1401                      size_t item_count,
1402                      size_t num_per_line,
1403                      uint64_t base_addr,
1404                      uint32_t item_bit_size,     // If zero, this is not a bitfield value, if non-zero, the value is a bitfield
1405                      uint32_t item_bit_offset,    // If "item_bit_size" is non-zero, this is the shift amount to apply to a bitfield
1406                      ExecutionContextScope *exe_scope) const
1407 {
1408     if (s == NULL)
1409         return start_offset;
1410
1411     if (item_format == eFormatPointer)
1412     {
1413         if (item_byte_size != 4 && item_byte_size != 8)
1414             item_byte_size = s->GetAddressByteSize();
1415     }
1416     
1417     offset_t offset = start_offset;
1418
1419     if (item_format == eFormatInstruction)
1420     {
1421         TargetSP target_sp;
1422         if (exe_scope)
1423             target_sp = exe_scope->CalculateTarget();
1424         if (target_sp)
1425         {
1426             DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL,  NULL));
1427             if (disassembler_sp)
1428             {
1429                 lldb::addr_t addr = base_addr + start_offset;
1430                 lldb_private::Address so_addr;
1431                                 bool data_from_file = true;
1432                 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1433                 {
1434                     data_from_file = false;
1435                 }
1436                 else
1437                 {
1438                     if (target_sp->GetSectionLoadList().IsEmpty() || !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
1439                         so_addr.SetRawAddress(addr);
1440                 }
1441
1442                 size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false, data_from_file);
1443                 
1444                 if (bytes_consumed)
1445                 {
1446                     offset += bytes_consumed;
1447                     const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
1448                     const bool show_bytes = true;
1449                     ExecutionContext exe_ctx;
1450                     exe_scope->CalculateExecutionContext(exe_ctx);
1451                     disassembler_sp->GetInstructionList().Dump (s,  show_address, show_bytes, &exe_ctx);
1452                     
1453                     // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
1454                     // I'll fix that but for now, just clear the list and it will go away nicely.
1455                     disassembler_sp->GetInstructionList().Clear();
1456                 }
1457             }
1458         }
1459         else
1460             s->Printf ("invalid target");
1461
1462         return offset;
1463     }
1464
1465     if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8)
1466         item_format = eFormatHex;
1467
1468     lldb::offset_t line_start_offset = start_offset;
1469     for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count)
1470     {
1471         if ((count % num_per_line) == 0)
1472         {
1473             if (count > 0)
1474             {
1475                 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1476                 {
1477                     s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
1478                     Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0);
1479                 }
1480                 s->EOL();
1481             }
1482             if (base_addr != LLDB_INVALID_ADDRESS)
1483                 s->Printf ("0x%8.8" PRIx64 ": ", (uint64_t)(base_addr + (offset - start_offset)));
1484             line_start_offset = offset;
1485         }
1486         else
1487         if (item_format != eFormatChar &&
1488             item_format != eFormatCharPrintable &&
1489             item_format != eFormatCharArray &&
1490             count > 0)
1491         {
1492             s->PutChar(' ');
1493         }
1494
1495         uint32_t i;
1496         switch (item_format)
1497         {
1498         case eFormatBoolean:
1499             if (item_byte_size <= 8)
1500                 s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false");
1501             else
1502             {
1503                 s->Printf("error: unsupported byte size (%" PRIu64 ") for boolean format", (uint64_t)item_byte_size);
1504                 return offset;
1505             }
1506             break;
1507
1508         case eFormatBinary:
1509             if (item_byte_size <= 8)
1510             {
1511                 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1512                 // Avoid std::bitset<64>::to_string() since it is missing in
1513                 // earlier C++ libraries
1514                 std::string binary_value(64, '0');
1515                 std::bitset<64> bits(uval64);
1516                 for (i = 0; i < 64; ++i)
1517                     if (bits[i])
1518                         binary_value[64 - 1 - i] = '1';
1519                 if (item_bit_size > 0)
1520                     s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
1521                 else if (item_byte_size > 0 && item_byte_size <= 8)
1522                     s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
1523             }
1524             else
1525             {
1526                 const bool is_signed = false;
1527                 const unsigned radix = 2;
1528                 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1529             }
1530             break;
1531
1532         case eFormatBytes:
1533         case eFormatBytesWithASCII:
1534             for (i=0; i<item_byte_size; ++i)
1535             {
1536                 s->Printf ("%2.2x", GetU8(&offset));
1537             }
1538             // Put an extra space between the groups of bytes if more than one
1539             // is being dumped in a group (item_byte_size is more than 1).
1540             if (item_byte_size > 1)
1541                 s->PutChar(' ');
1542             break;
1543
1544         case eFormatChar:
1545         case eFormatCharPrintable:
1546         case eFormatCharArray:
1547             {
1548                 // If we are only printing one character surround it with single
1549                 // quotes
1550                 if (item_count == 1 && item_format == eFormatChar)
1551                     s->PutChar('\'');
1552
1553                 const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1554                 if (isprint(ch))
1555                     s->Printf ("%c", (char)ch);
1556                 else if (item_format != eFormatCharPrintable)
1557                 {
1558                     switch (ch)
1559                     {
1560                     case '\033': s->Printf ("\\e"); break;
1561                     case '\a': s->Printf ("\\a"); break;
1562                     case '\b': s->Printf ("\\b"); break;
1563                     case '\f': s->Printf ("\\f"); break;
1564                     case '\n': s->Printf ("\\n"); break;
1565                     case '\r': s->Printf ("\\r"); break;
1566                     case '\t': s->Printf ("\\t"); break;
1567                     case '\v': s->Printf ("\\v"); break;
1568                     case '\0': s->Printf ("\\0"); break;
1569                     default:   
1570                         if (item_byte_size == 1)
1571                             s->Printf ("\\x%2.2x", (uint8_t)ch); 
1572                         else
1573                             s->Printf ("%" PRIu64, ch);
1574                         break;
1575                     }
1576                 }
1577                 else
1578                 {
1579                     s->PutChar(NON_PRINTABLE_CHAR);
1580                 }
1581
1582                 // If we are only printing one character surround it with single quotes
1583                 if (item_count == 1 && item_format == eFormatChar)
1584                     s->PutChar('\'');
1585             }
1586             break;
1587
1588         case eFormatEnum:       // Print enum value as a signed integer when we don't get the enum type
1589         case eFormatDecimal:
1590             if (item_byte_size <= 8)
1591                 s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1592             else
1593             {
1594                 const bool is_signed = true;
1595                 const unsigned radix = 10;
1596                 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1597             }
1598             break;
1599
1600         case eFormatUnsigned:
1601             if (item_byte_size <= 8)
1602                 s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1603             else
1604             {
1605                 const bool is_signed = false;
1606                 const unsigned radix = 10;
1607                 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1608             }
1609             break;
1610
1611         case eFormatOctal:
1612             if (item_byte_size <= 8)
1613                 s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1614             else
1615             {
1616                 const bool is_signed = false;
1617                 const unsigned radix = 8;
1618                 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1619             }
1620             break;
1621
1622         case eFormatOSType:
1623             {
1624                 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1625                 s->PutChar('\'');
1626                 for (i=0; i<item_byte_size; ++i)
1627                 {
1628                     uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
1629                     if (isprint(ch))
1630                         s->Printf ("%c", ch);
1631                     else
1632                     {
1633                         switch (ch)
1634                         {
1635                         case '\033': s->Printf ("\\e"); break;
1636                         case '\a': s->Printf ("\\a"); break;
1637                         case '\b': s->Printf ("\\b"); break;
1638                         case '\f': s->Printf ("\\f"); break;
1639                         case '\n': s->Printf ("\\n"); break;
1640                         case '\r': s->Printf ("\\r"); break;
1641                         case '\t': s->Printf ("\\t"); break;
1642                         case '\v': s->Printf ("\\v"); break;
1643                         case '\0': s->Printf ("\\0"); break;
1644                         default:   s->Printf ("\\x%2.2x", ch); break;
1645                         }
1646                     }
1647                 }
1648                 s->PutChar('\'');
1649             }
1650             break;
1651             
1652         case eFormatCString:
1653             {
1654                 const char *cstr = GetCStr(&offset);
1655                 
1656                 if (!cstr)
1657                 {
1658                     s->Printf("NULL");
1659                     offset = LLDB_INVALID_OFFSET;
1660                 }
1661                 else
1662                 {
1663                     s->PutChar('\"');
1664                     
1665                     while (const char c = *cstr)
1666                     {                    
1667                         if (isprint(c))
1668                         {
1669                             s->PutChar(c);
1670                         }
1671                         else
1672                         {
1673                             switch (c)
1674                             {
1675                             case '\033': s->Printf ("\\e"); break;
1676                             case '\a': s->Printf ("\\a"); break;
1677                             case '\b': s->Printf ("\\b"); break;
1678                             case '\f': s->Printf ("\\f"); break;
1679                             case '\n': s->Printf ("\\n"); break;
1680                             case '\r': s->Printf ("\\r"); break;
1681                             case '\t': s->Printf ("\\t"); break;
1682                             case '\v': s->Printf ("\\v"); break;
1683                             default:   s->Printf ("\\x%2.2x", c); break;
1684                             }
1685                         }
1686                         
1687                         ++cstr;
1688                     }
1689                     
1690                     s->PutChar('\"');
1691                 }
1692             }
1693             break;
1694
1695
1696         case eFormatPointer:
1697             s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t));
1698             break;
1699
1700
1701         case eFormatComplexInteger:
1702             {
1703                 size_t complex_int_byte_size = item_byte_size / 2;
1704                 
1705                 if (complex_int_byte_size <= 8)
1706                 {
1707                     s->Printf("%" PRIu64, GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1708                     s->Printf(" + %" PRIu64 "i", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1709                 }
1710                 else
1711                 {
1712                     s->Printf("error: unsupported byte size (%" PRIu64 ") for complex integer format", (uint64_t)item_byte_size);
1713                     return offset;
1714                 }
1715             }
1716             break;
1717
1718         case eFormatComplex:
1719             if (sizeof(float) * 2 == item_byte_size)
1720             {
1721                 float f32_1 = GetFloat (&offset);
1722                 float f32_2 = GetFloat (&offset);
1723
1724                 s->Printf ("%g + %gi", f32_1, f32_2);
1725                 break;
1726             }
1727             else if (sizeof(double) * 2 == item_byte_size)
1728             {
1729                 double d64_1 = GetDouble (&offset);
1730                 double d64_2 = GetDouble (&offset);
1731
1732                 s->Printf ("%lg + %lgi", d64_1, d64_2);
1733                 break;
1734             }
1735             else if (sizeof(long double) * 2 == item_byte_size)
1736             {
1737                 long double ld64_1 = GetLongDouble (&offset);
1738                 long double ld64_2 = GetLongDouble (&offset);
1739                 s->Printf ("%Lg + %Lgi", ld64_1, ld64_2);
1740                 break;
1741             }
1742             else
1743             {
1744                 s->Printf("error: unsupported byte size (%" PRIu64 ") for complex float format", (uint64_t)item_byte_size);
1745                 return offset;
1746             }
1747             break;
1748
1749         default:
1750         case eFormatDefault:
1751         case eFormatHex:
1752         case eFormatHexUppercase:
1753             {
1754                 bool wantsuppercase  = (item_format == eFormatHexUppercase);
1755                 switch (item_byte_size)
1756                 {
1757                 case 1:
1758                 case 2:
1759                 case 4:
1760                 case 8:
1761                     s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1762                     break;
1763                 default:
1764                     {
1765                         assert (item_bit_size == 0 && item_bit_offset == 0);
1766                         const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size);
1767                         if (bytes)
1768                         {
1769                             s->PutCString("0x");
1770                             uint32_t idx;
1771                                 if (m_byte_order == eByteOrderBig)
1772                             {
1773                                 for (idx = 0; idx < item_byte_size; ++idx)
1774                                     s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
1775                             }
1776                             else
1777                             {
1778                                 for (idx = 0; idx < item_byte_size; ++idx)
1779                                     s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]);
1780                             }
1781                         }
1782                     }
1783                     break;
1784                 }
1785             }
1786             break;
1787
1788         case eFormatFloat:
1789             {
1790                 TargetSP target_sp;
1791                 bool used_apfloat = false;
1792                 if (exe_scope)
1793                     target_sp = exe_scope->CalculateTarget();
1794                 if (target_sp)
1795                 {
1796                     ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1797                     if (clang_ast)
1798                     {
1799                         clang::ASTContext *ast = clang_ast->getASTContext();
1800                         if (ast)
1801                         {
1802                             llvm::SmallVector<char, 256> sv;
1803                             // Show full precision when printing float values
1804                             const unsigned format_precision = 0;
1805                             const unsigned format_max_padding = 100;
1806                             size_t item_bit_size = item_byte_size * 8;
1807                             
1808                             if (item_bit_size == ast->getTypeSize(ast->FloatTy))
1809                             {
1810                                 llvm::APInt apint(item_bit_size, this->GetMaxU64(&offset, item_byte_size));
1811                                 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->FloatTy), apint);
1812                                 apfloat.toString(sv, format_precision, format_max_padding);
1813                             }
1814                             else if (item_bit_size == ast->getTypeSize(ast->DoubleTy))
1815                             {
1816                                 llvm::APInt apint;
1817                                 if (GetAPInt (*this, &offset, item_byte_size, apint))
1818                                 {
1819                                     llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->DoubleTy), apint);
1820                                     apfloat.toString(sv, format_precision, format_max_padding);
1821                                 }
1822                             }
1823                             else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy))
1824                             {
1825                                 llvm::APInt apint;
1826                                 switch (target_sp->GetArchitecture().GetMachine())
1827                                 {
1828                                     case llvm::Triple::x86:
1829                                     case llvm::Triple::x86_64:
1830                                         // clang will assert when constructing the apfloat if we use a 16 byte integer value
1831                                         if (GetAPInt (*this, &offset, 10, apint))
1832                                         {
1833                                             llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1834                                             apfloat.toString(sv, format_precision, format_max_padding);
1835                                         }
1836                                         break;
1837                                         
1838                                     default:
1839                                         if (GetAPInt (*this, &offset, item_byte_size, apint))
1840                                         {
1841                                             llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1842                                             apfloat.toString(sv, format_precision, format_max_padding);
1843                                         }
1844                                         break;
1845                                 }
1846                             }
1847                             else if (item_bit_size == ast->getTypeSize(ast->HalfTy))
1848                             {
1849                                 llvm::APInt apint(item_bit_size, this->GetU16(&offset));
1850                                 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->HalfTy), apint);
1851                                 apfloat.toString(sv, format_precision, format_max_padding);
1852                             }
1853
1854                             if (!sv.empty())
1855                             {
1856                                 s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
1857                                 used_apfloat = true;
1858                             }
1859                         }
1860                     }
1861                 }
1862                 
1863                 if (!used_apfloat)
1864                 {
1865                     std::ostringstream ss;
1866                     if (item_byte_size == sizeof(float) || item_byte_size == 2)
1867                     {
1868                         float f;
1869                         if (item_byte_size == 2)
1870                         {
1871                             uint16_t half = this->GetU16(&offset);
1872                             f = half2float(half);
1873                         }
1874                         else
1875                         {
1876                             f = GetFloat (&offset);
1877                         }
1878                         ss.precision(std::numeric_limits<float>::digits10);
1879                         ss << f;
1880                     } 
1881                     else if (item_byte_size == sizeof(double))
1882                     {
1883                         ss.precision(std::numeric_limits<double>::digits10);
1884                         ss << GetDouble(&offset);
1885                     }
1886                     else if (item_byte_size == sizeof(long double) || item_byte_size == 10)
1887                     {
1888                         ss.precision(std::numeric_limits<long double>::digits10);
1889                         ss << GetLongDouble(&offset);
1890                     }
1891                     else
1892                     {
1893                         s->Printf("error: unsupported byte size (%" PRIu64 ") for float format", (uint64_t)item_byte_size);
1894                         return offset;
1895                     }
1896                     ss.flush();
1897                     s->Printf("%s", ss.str().c_str());
1898                 }
1899             }
1900             break;
1901
1902         case eFormatUnicode16:
1903             s->Printf("U+%4.4x", GetU16 (&offset));
1904             break;
1905
1906         case eFormatUnicode32:
1907             s->Printf("U+0x%8.8x", GetU32 (&offset));
1908             break;
1909
1910         case eFormatAddressInfo:
1911             {
1912                 addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1913                 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), addr);
1914                 if (exe_scope)
1915                 {
1916                     TargetSP target_sp (exe_scope->CalculateTarget());
1917                     lldb_private::Address so_addr;
1918                     if (target_sp)
1919                     {
1920                         if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1921                         {
1922                             s->PutChar(' ');
1923                             so_addr.Dump (s,
1924                                           exe_scope,
1925                                           Address::DumpStyleResolvedDescription,
1926                                           Address::DumpStyleModuleWithFileAddress);
1927                         }
1928                         else
1929                         {
1930                             so_addr.SetOffset(addr);
1931                             so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription);
1932                         }
1933                     }
1934                 }
1935             }
1936             break;
1937
1938         case eFormatHexFloat:
1939             if (sizeof(float) == item_byte_size)
1940             {
1941                 char float_cstr[256];
1942                 llvm::APFloat ap_float (GetFloat (&offset));
1943                 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1944                 s->Printf ("%s", float_cstr);
1945                 break;
1946             }
1947             else if (sizeof(double) == item_byte_size)
1948             {
1949                 char float_cstr[256];
1950                 llvm::APFloat ap_float (GetDouble (&offset));
1951                 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1952                 s->Printf ("%s", float_cstr);
1953                 break;
1954             }
1955             else
1956             {
1957                 s->Printf("error: unsupported byte size (%" PRIu64 ") for hex float format", (uint64_t)item_byte_size);
1958                 return offset;
1959             }
1960             break;
1961
1962 // please keep the single-item formats below in sync with FormatManager::GetSingleItemFormat
1963 // if you fail to do so, users will start getting different outputs depending on internal
1964 // implementation details they should not care about ||
1965         case eFormatVectorOfChar:               //   ||
1966             s->PutChar('{');                    //   \/   
1967             offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1968             s->PutChar('}');
1969             break;
1970
1971         case eFormatVectorOfSInt8:
1972             s->PutChar('{');
1973             offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1974             s->PutChar('}');
1975             break;
1976
1977         case eFormatVectorOfUInt8:
1978             s->PutChar('{');
1979             offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1980             s->PutChar('}');
1981             break;
1982
1983         case eFormatVectorOfSInt16:
1984             s->PutChar('{');
1985             offset = Dump (s, offset, eFormatDecimal, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
1986             s->PutChar('}');
1987             break;
1988
1989         case eFormatVectorOfUInt16:
1990             s->PutChar('{');
1991             offset = Dump (s, offset, eFormatHex,     sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
1992             s->PutChar('}');
1993             break;
1994
1995         case eFormatVectorOfSInt32:
1996             s->PutChar('{');
1997             offset = Dump (s, offset, eFormatDecimal, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
1998             s->PutChar('}');
1999             break;
2000
2001         case eFormatVectorOfUInt32:
2002             s->PutChar('{');
2003             offset = Dump (s, offset, eFormatHex,     sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
2004             s->PutChar('}');
2005             break;
2006
2007         case eFormatVectorOfSInt64:
2008             s->PutChar('{');
2009             offset = Dump (s, offset, eFormatDecimal, sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0);
2010             s->PutChar('}');
2011             break;
2012
2013         case eFormatVectorOfUInt64:
2014             s->PutChar('{');
2015             offset = Dump (s, offset, eFormatHex,     sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0);
2016             s->PutChar('}');
2017             break;
2018
2019         case eFormatVectorOfFloat32:
2020             s->PutChar('{');
2021             offset = Dump (s, offset, eFormatFloat,       4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
2022             s->PutChar('}');
2023             break;
2024
2025         case eFormatVectorOfFloat64:
2026             s->PutChar('{');
2027             offset = Dump (s, offset, eFormatFloat,       8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
2028             s->PutChar('}');
2029             break;
2030
2031         case eFormatVectorOfUInt128:
2032             s->PutChar('{');
2033             offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
2034             s->PutChar('}');
2035             break;
2036         }
2037     }
2038
2039     if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
2040     {
2041         s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
2042         Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0);
2043     }
2044     return offset;  // Return the offset at which we ended up
2045 }
2046
2047 //----------------------------------------------------------------------
2048 // Dumps bytes from this object's data to the stream "s" starting
2049 // "start_offset" bytes into this data, and ending with the byte
2050 // before "end_offset". "base_addr" will be added to the offset
2051 // into the dumped data when showing the offset into the data in the
2052 // output information. "num_per_line" objects of type "type" will
2053 // be dumped with the option to override the format for each object
2054 // with "type_format". "type_format" is a printf style formatting
2055 // string. If "type_format" is NULL, then an appropriate format
2056 // string will be used for the supplied "type". If the stream "s"
2057 // is NULL, then the output will be send to Log().
2058 //----------------------------------------------------------------------
2059 lldb::offset_t
2060 DataExtractor::PutToLog
2061 (
2062     Log *log,
2063     offset_t start_offset,
2064     offset_t length,
2065     uint64_t base_addr,
2066     uint32_t num_per_line,
2067     DataExtractor::Type type,
2068     const char *format
2069 ) const
2070 {
2071     if (log == NULL)
2072         return start_offset;
2073
2074     offset_t offset;
2075     offset_t end_offset;
2076     uint32_t count;
2077     StreamString sstr;
2078     for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count)
2079     {
2080         if ((count % num_per_line) == 0)
2081         {
2082             // Print out any previous string
2083             if (sstr.GetSize() > 0)
2084             {
2085                 log->Printf("%s", sstr.GetData());
2086                 sstr.Clear();
2087             }
2088             // Reset string offset and fill the current line string with address:
2089             if (base_addr != LLDB_INVALID_ADDRESS)
2090                 sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset)));
2091         }
2092
2093         switch (type)
2094         {
2095             case TypeUInt8:   sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break;
2096             case TypeChar:
2097                 {
2098                     char ch = GetU8(&offset);
2099                     sstr.Printf (format ? format : " %c",    isprint(ch) ? ch : ' ');
2100                 }
2101                 break;
2102             case TypeUInt16:  sstr.Printf (format ? format : " %4.4x",       GetU16(&offset)); break;
2103             case TypeUInt32:  sstr.Printf (format ? format : " %8.8x",       GetU32(&offset)); break;
2104             case TypeUInt64:  sstr.Printf (format ? format : " %16.16" PRIx64,   GetU64(&offset)); break;
2105             case TypePointer: sstr.Printf (format ? format : " 0x%" PRIx64,      GetAddress(&offset)); break;
2106             case TypeULEB128: sstr.Printf (format ? format : " 0x%" PRIx64,      GetULEB128(&offset)); break;
2107             case TypeSLEB128: sstr.Printf (format ? format : " %" PRId64,        GetSLEB128(&offset)); break;
2108         }
2109     }
2110
2111     if (sstr.GetSize() > 0)
2112         log->Printf("%s", sstr.GetData());
2113
2114     return offset;  // Return the offset at which we ended up
2115 }
2116
2117 //----------------------------------------------------------------------
2118 // DumpUUID
2119 //
2120 // Dump out a UUID starting at 'offset' bytes into the buffer
2121 //----------------------------------------------------------------------
2122 void
2123 DataExtractor::DumpUUID (Stream *s, offset_t offset) const
2124 {
2125     if (s)
2126     {
2127         const uint8_t *uuid_data = PeekData(offset, 16);
2128         if ( uuid_data )
2129         {
2130             lldb_private::UUID uuid(uuid_data, 16);
2131             uuid.Dump(s);
2132         }
2133         else
2134         {
2135             s->Printf("<not enough data for UUID at offset 0x%8.8" PRIx64 ">", offset);
2136         }
2137     }
2138 }
2139
2140 void
2141 DataExtractor::DumpHexBytes (Stream *s, 
2142                              const void *src, 
2143                              size_t src_len, 
2144                              uint32_t bytes_per_line,
2145                              addr_t base_addr)
2146 {
2147     DataExtractor data (src, src_len, eByteOrderLittle, 4);
2148     data.Dump (s, 
2149                0,               // Offset into "src"
2150                eFormatBytes,    // Dump as hex bytes
2151                1,               // Size of each item is 1 for single bytes
2152                src_len,         // Number of bytes
2153                bytes_per_line,  // Num bytes per line
2154                base_addr,       // Base address
2155                0, 0);           // Bitfield info
2156 }
2157
2158 size_t
2159 DataExtractor::Copy (DataExtractor &dest_data) const
2160 {
2161     if (m_data_sp.get())
2162     {
2163         // we can pass along the SP to the data
2164         dest_data.SetData(m_data_sp);
2165     }
2166     else
2167     {
2168         const uint8_t *base_ptr = m_start;
2169         size_t data_size = GetByteSize();
2170         dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
2171     }
2172     return GetByteSize();
2173 }
2174
2175 bool
2176 DataExtractor::Append(DataExtractor& rhs)
2177 {
2178     if (rhs.GetByteOrder() != GetByteOrder())
2179         return false;
2180     
2181     if (rhs.GetByteSize() == 0)
2182         return true;
2183     
2184     if (GetByteSize() == 0)
2185         return (rhs.Copy(*this) > 0);
2186     
2187     size_t bytes = GetByteSize() + rhs.GetByteSize();
2188
2189     DataBufferHeap *buffer_heap_ptr = NULL;
2190     DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2191     
2192     if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2193         return false;
2194     
2195     uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2196     
2197     memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2198     memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
2199     
2200     SetData(buffer_sp);
2201     
2202     return true;
2203 }
2204
2205 bool
2206 DataExtractor::Append(void* buf, offset_t length)
2207 {
2208     if (buf == NULL)
2209         return false;
2210     
2211     if (length == 0)
2212         return true;
2213     
2214     size_t bytes = GetByteSize() + length;
2215     
2216     DataBufferHeap *buffer_heap_ptr = NULL;
2217     DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2218     
2219     if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2220         return false;
2221     
2222     uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2223     
2224     if (GetByteSize() > 0)
2225         memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2226
2227     memcpy(bytes_ptr + GetByteSize(), buf, length);
2228     
2229     SetData(buffer_sp);
2230     
2231     return true;
2232 }