]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/lldb/source/Core/DataExtractor.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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/Target.h"
41
42 using namespace lldb;
43 using namespace lldb_private;
44
45 static inline uint16_t 
46 ReadInt16(const unsigned char* ptr, offset_t offset)
47 {
48     return *(uint16_t *)(ptr + offset);
49 }
50 static inline uint32_t
51 ReadInt32 (const unsigned char* ptr, offset_t offset)
52 {
53     return *(uint32_t *)(ptr + offset);
54 }
55
56 static inline uint64_t 
57 ReadInt64(const unsigned char* ptr, offset_t offset)
58 {
59     return *(uint64_t *)(ptr + offset);
60 }
61
62 static inline uint16_t
63 ReadInt16(const void* ptr)
64 {
65     return *(uint16_t *)(ptr);
66 }
67 static inline uint32_t
68 ReadInt32 (const void* ptr)
69 {
70     return *(uint32_t *)(ptr);
71 }
72
73 static inline uint64_t
74 ReadInt64(const void* ptr)
75 {
76     return *(uint64_t *)(ptr);
77 }
78
79 static inline uint16_t
80 ReadSwapInt16(const unsigned char* ptr, offset_t offset)
81 {
82     return llvm::ByteSwap_16(*(uint16_t *)(ptr + offset));
83 }
84
85 static inline uint32_t
86 ReadSwapInt32 (const unsigned char* ptr, offset_t offset)
87 {
88     return llvm::ByteSwap_32(*(uint32_t *)(ptr + offset));
89 }
90 static inline uint64_t 
91 ReadSwapInt64(const unsigned char* ptr, offset_t offset) 
92 {
93   return llvm::ByteSwap_64(*(uint64_t *)(ptr + offset));
94 }
95
96 static inline uint16_t
97 ReadSwapInt16(const void* ptr)
98 {
99     return llvm::ByteSwap_16(*(uint16_t *)(ptr));
100 }
101
102 static inline uint32_t
103 ReadSwapInt32 (const void* ptr)
104 {
105     return llvm::ByteSwap_32(*(uint32_t *)(ptr));
106 }
107 static inline uint64_t
108 ReadSwapInt64(const void* ptr)
109 {
110     return llvm::ByteSwap_64(*(uint64_t *)(ptr));
111 }
112
113 #define NON_PRINTABLE_CHAR '.'
114 //----------------------------------------------------------------------
115 // Default constructor.
116 //----------------------------------------------------------------------
117 DataExtractor::DataExtractor () :
118     m_start     (NULL),
119     m_end       (NULL),
120     m_byte_order(lldb::endian::InlHostByteOrder()),
121     m_addr_size (4),
122     m_data_sp   ()
123 {
124 }
125
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),
135     m_data_sp   ()
136 {
137 }
138
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
144 // this data.
145 //----------------------------------------------------------------------
146 DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint32_t addr_size) :
147     m_start     (NULL),
148     m_end       (NULL),
149     m_byte_order(endian),
150     m_addr_size (addr_size),
151     m_data_sp   ()
152 {
153     SetData (data_sp);
154 }
155
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) :
164     m_start(NULL),
165     m_end(NULL),
166     m_byte_order(data.m_byte_order),
167     m_addr_size(data.m_addr_size),
168     m_data_sp()
169 {
170     if (data.ValidOffset(offset))
171     {
172         offset_t bytes_available = data.GetByteSize() - offset;
173         if (length > bytes_available)
174             length = bytes_available;
175         SetData(data, offset, length);
176     }
177 }
178
179 DataExtractor::DataExtractor (const DataExtractor& rhs) :
180     m_start (rhs.m_start),
181     m_end (rhs.m_end),
182     m_byte_order (rhs.m_byte_order),
183     m_addr_size (rhs.m_addr_size),
184     m_data_sp (rhs.m_data_sp)
185 {
186 }
187
188 //----------------------------------------------------------------------
189 // Assignment operator
190 //----------------------------------------------------------------------
191 const DataExtractor&
192 DataExtractor::operator= (const DataExtractor& rhs)
193 {
194     if (this != &rhs)
195     {
196         m_start = rhs.m_start;
197         m_end = rhs.m_end;
198         m_byte_order = rhs.m_byte_order;
199         m_addr_size = rhs.m_addr_size;
200         m_data_sp = rhs.m_data_sp;
201     }
202     return *this;
203 }
204
205 //----------------------------------------------------------------------
206 // Destructor
207 //----------------------------------------------------------------------
208 DataExtractor::~DataExtractor ()
209 {
210 }
211
212 //------------------------------------------------------------------
213 // Clears the object contents back to a default invalid state, and
214 // release any references to shared data that this object may
215 // contain.
216 //------------------------------------------------------------------
217 void
218 DataExtractor::Clear ()
219 {
220     m_start = NULL;
221     m_end = NULL;
222     m_byte_order = lldb::endian::InlHostByteOrder();
223     m_addr_size = 4;
224     m_data_sp.reset();
225 }
226
227 //------------------------------------------------------------------
228 // If this object contains shared data, this function returns the
229 // offset into that shared data. Else zero is returned.
230 //------------------------------------------------------------------
231 size_t
232 DataExtractor::GetSharedDataOffset () const
233 {
234     if (m_start != NULL)
235     {
236         const DataBuffer * data = m_data_sp.get();
237         if (data != NULL)
238         {
239             const uint8_t * data_bytes = data->GetBytes();
240             if (data_bytes != NULL)
241             {
242                 assert(m_start >= data_bytes);
243                 return m_start - data_bytes;
244             }
245         }
246     }
247     return 0;
248 }
249
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 //----------------------------------------------------------------------
260 lldb::offset_t
261 DataExtractor::SetData (const void *bytes, offset_t length, ByteOrder endian)
262 {
263     m_byte_order = endian;
264     m_data_sp.reset();
265     if (bytes == NULL || length == 0)
266     {
267         m_start = NULL;
268         m_end = NULL;
269     }
270     else
271     {
272         m_start = (uint8_t *)bytes;
273         m_end = m_start + length;
274     }
275     return GetByteSize();
276 }
277
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 //----------------------------------------------------------------------
292 lldb::offset_t
293 DataExtractor::SetData (const DataExtractor& data, offset_t data_offset, offset_t data_length)
294 {
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())
298     {
299         m_byte_order = data.m_byte_order;
300         return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length);
301     }
302
303     // We have a DataExtractor object that just has a pointer to bytes
304     if (data.ValidOffset(data_offset))
305     {
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());
309     }
310     return 0;
311 }
312
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 //----------------------------------------------------------------------
327 lldb::offset_t
328 DataExtractor::SetData (const DataBufferSP& data_sp, offset_t data_offset, offset_t data_length)
329 {
330     m_start = m_end = NULL;
331
332     if (data_length > 0)
333     {
334         m_data_sp = data_sp;
335         if (data_sp.get())
336         {
337             const size_t data_size = data_sp->GetByteSize();
338             if (data_offset < data_size)
339             {
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
345                 else
346                     m_end = m_start + bytes_left;   // Not all the bytes requested were available in the shared data
347             }
348         }
349     }
350
351     size_t new_size = GetByteSize();
352
353     // Don't hold a shared pointer to the data buffer if we don't share
354     // any valid bytes in the shared buffer.
355     if (new_size == 0)
356         m_data_sp.reset();
357
358     return new_size;
359 }
360
361 //----------------------------------------------------------------------
362 // Extract a single unsigned char from the binary data and update
363 // the offset pointed to by "offset_ptr".
364 //
365 // RETURNS the byte that was extracted, or zero on failure.
366 //----------------------------------------------------------------------
367 uint8_t
368 DataExtractor::GetU8 (offset_t *offset_ptr) const
369 {
370     const uint8_t *data = (const uint8_t *)GetData (offset_ptr, 1);
371     if (data)
372         return *data;
373     return 0;
374 }
375
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
379 // "dst".
380 //
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 //----------------------------------------------------------------------
385 void *
386 DataExtractor::GetU8 (offset_t *offset_ptr, void *dst, uint32_t count) const
387 {
388     const uint8_t *data = (const uint8_t *)GetData (offset_ptr, count);
389     if (data)
390     {
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
394         return dst;
395     }
396     return NULL;
397 }
398
399 //----------------------------------------------------------------------
400 // Extract a single uint16_t from the data and update the offset
401 // pointed to by "offset_ptr".
402 //
403 // RETURNS the uint16_t that was extracted, or zero on failure.
404 //----------------------------------------------------------------------
405 uint16_t
406 DataExtractor::GetU16 (offset_t *offset_ptr) const
407 {
408     uint16_t val = 0;
409     const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
410     if (data)
411     {
412         if (m_byte_order != lldb::endian::InlHostByteOrder())
413             val = ReadSwapInt16(data);
414         else
415             val = ReadInt16 (data);
416     }
417     return val;
418 }
419
420 uint16_t
421 DataExtractor::GetU16_unchecked (offset_t *offset_ptr) const
422 {
423     uint16_t val;
424     if (m_byte_order == lldb::endian::InlHostByteOrder())
425         val = ReadInt16 (m_start, *offset_ptr);
426     else
427         val = ReadSwapInt16(m_start, *offset_ptr);
428     *offset_ptr += sizeof(val);
429     return val;
430 }
431
432 uint32_t
433 DataExtractor::GetU32_unchecked (offset_t *offset_ptr) const
434 {
435     uint32_t val;
436     if (m_byte_order == lldb::endian::InlHostByteOrder())
437         val = ReadInt32 (m_start, *offset_ptr);
438     else
439         val =  ReadSwapInt32 (m_start, *offset_ptr);
440     *offset_ptr += sizeof(val);
441     return val;
442 }
443
444 uint64_t
445 DataExtractor::GetU64_unchecked (offset_t *offset_ptr) const
446 {
447     uint64_t val;
448     if (m_byte_order == lldb::endian::InlHostByteOrder())
449         val = ReadInt64 (m_start, *offset_ptr);
450     else
451         val = ReadSwapInt64 (m_start, *offset_ptr);
452     *offset_ptr += sizeof(val);
453     return val;
454 }
455
456
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".
461 //
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 //----------------------------------------------------------------------
466 void *
467 DataExtractor::GetU16 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
468 {
469     const size_t src_size = sizeof(uint16_t) * count;
470     const uint16_t *src = (const uint16_t *)GetData (offset_ptr, src_size);
471     if (src)
472     {
473         if (m_byte_order != lldb::endian::InlHostByteOrder())
474         {
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)
479             {
480                 *dst_pos = ReadSwapInt16 (src_pos);
481                 ++dst_pos;
482                 ++src_pos;
483             }
484         }
485         else
486         {
487             memcpy (void_dst, src, src_size);
488         }
489         // Return a non-NULL pointer to the converted data as an indicator of success
490         return void_dst;
491     }
492     return NULL;
493 }
494
495 //----------------------------------------------------------------------
496 // Extract a single uint32_t from the data and update the offset
497 // pointed to by "offset_ptr".
498 //
499 // RETURNS the uint32_t that was extracted, or zero on failure.
500 //----------------------------------------------------------------------
501 uint32_t
502 DataExtractor::GetU32 (offset_t *offset_ptr) const
503 {
504     uint32_t val = 0;
505     const uint32_t *data = (const uint32_t *)GetData (offset_ptr, sizeof(val));
506     if (data)
507     {
508         if (m_byte_order != lldb::endian::InlHostByteOrder())
509             val = ReadSwapInt32 (data);
510         else
511             val = *data;
512     }
513     return val;
514 }
515
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".
520 //
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 //----------------------------------------------------------------------
525 void *
526 DataExtractor::GetU32 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
527 {
528     const size_t src_size = sizeof(uint32_t) * count;
529     const uint32_t *src = (const uint32_t *)GetData (offset_ptr, src_size);
530     if (src)
531     {
532         if (m_byte_order != lldb::endian::InlHostByteOrder())
533         {
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)
538             {
539                 *dst_pos = ReadSwapInt32 (src_pos);
540                 ++dst_pos;
541                 ++src_pos;
542             }
543         }
544         else
545         {
546             memcpy (void_dst, src, src_size);
547         }
548         // Return a non-NULL pointer to the converted data as an indicator of success
549         return void_dst;
550     }
551     return NULL;
552 }
553
554 //----------------------------------------------------------------------
555 // Extract a single uint64_t from the data and update the offset
556 // pointed to by "offset_ptr".
557 //
558 // RETURNS the uint64_t that was extracted, or zero on failure.
559 //----------------------------------------------------------------------
560 uint64_t
561 DataExtractor::GetU64 (offset_t *offset_ptr) const
562 {
563     uint64_t val = 0;
564     const uint64_t *data = (const uint64_t *)GetData (offset_ptr, sizeof(val));
565     if (data)
566     {
567         if (m_byte_order != lldb::endian::InlHostByteOrder())
568             val = ReadSwapInt64 (data);
569         else
570             val = *data;
571     }
572     return val;
573 }
574
575 //----------------------------------------------------------------------
576 // GetU64
577 //
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 //----------------------------------------------------------------------
582 void *
583 DataExtractor::GetU64 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
584 {
585     const size_t src_size = sizeof(uint64_t) * count;
586     const uint64_t *src = (const uint64_t *)GetData (offset_ptr, src_size);
587     if (src)
588     {
589         if (m_byte_order != lldb::endian::InlHostByteOrder())
590         {
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)
595             {
596                 *dst_pos = ReadSwapInt64 (src_pos);
597                 ++dst_pos;
598                 ++src_pos;
599             }
600         }
601         else
602         {
603             memcpy (void_dst, src, src_size);
604         }
605         // Return a non-NULL pointer to the converted data as an indicator of success
606         return void_dst;
607     }
608     return NULL;
609 }
610
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.
618 //
619 // RETURNS the integer value that was extracted, or zero on failure.
620 //----------------------------------------------------------------------
621 uint32_t
622 DataExtractor::GetMaxU32 (offset_t *offset_ptr, size_t byte_size) const
623 {
624     switch (byte_size)
625     {
626     case 1: return GetU8 (offset_ptr); break;
627     case 2: return GetU16(offset_ptr); break;
628     case 4: return GetU32(offset_ptr); break;
629     default:
630         assert("GetMaxU32 unhandled case!" == NULL);
631         break;
632     }
633     return 0;
634 }
635
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.
643 //
644 // RETURNS the integer value that was extracted, or zero on failure.
645 //----------------------------------------------------------------------
646 uint64_t
647 DataExtractor::GetMaxU64 (offset_t *offset_ptr, size_t size) const
648 {
649     switch (size)
650     {
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;
655     default:
656         assert("GetMax64 unhandled case!" == NULL);
657         break;
658     }
659     return 0;
660 }
661
662 uint64_t
663 DataExtractor::GetMaxU64_unchecked (offset_t *offset_ptr, size_t size) const
664 {
665     switch (size)
666     {
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;
671         default:
672             assert("GetMax64 unhandled case!" == NULL);
673             break;
674     }
675     return 0;
676 }
677
678 int64_t
679 DataExtractor::GetMaxS64 (offset_t *offset_ptr, size_t size) const
680 {
681     switch (size)
682     {
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;
687     default:
688         assert("GetMax64 unhandled case!" == NULL);
689         break;
690     }
691     return 0;
692 }
693
694 uint64_t
695 DataExtractor::GetMaxU64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
696 {
697     uint64_t uval64 = GetMaxU64 (offset_ptr, size);
698     if (bitfield_bit_size > 0)
699     {
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)
704             return uval64;
705         uval64 &= bitfield_mask;
706     }
707     return uval64;
708 }
709
710 int64_t
711 DataExtractor::GetMaxS64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
712 {
713     int64_t sval64 = GetMaxS64 (offset_ptr, size);
714     if (bitfield_bit_size > 0)
715     {
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;
723     }
724     return sval64;
725 }
726
727
728 float
729 DataExtractor::GetFloat (offset_t *offset_ptr) const
730 {
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);
735     if (src)
736     {
737         if (m_byte_order != lldb::endian::InlHostByteOrder())
738         {
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];
743         }
744         else
745         {
746             val = *src;
747         }
748     }
749     return val;
750 }
751
752 double
753 DataExtractor::GetDouble (offset_t *offset_ptr) const
754 {
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);
759     if (src)
760     {
761         if (m_byte_order != lldb::endian::InlHostByteOrder())
762         {
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];
767         }
768         else
769         {
770             val = *src;
771         }
772     }
773     return val;
774 }
775
776
777 long double
778 DataExtractor::GetLongDouble (offset_t *offset_ptr) const
779 {
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());
783 #else
784     *offset_ptr += CopyByteOrderedData (*offset_ptr, sizeof(val), &val, sizeof(val), lldb::endian::InlHostByteOrder());
785 #endif
786     return val;
787 }
788
789
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.
795 //
796 // RETURNS the address that was extracted, or zero on failure.
797 //------------------------------------------------------------------
798 uint64_t
799 DataExtractor::GetAddress (offset_t *offset_ptr) const
800 {
801     return GetMaxU64 (offset_ptr, m_addr_size);
802 }
803
804 uint64_t
805 DataExtractor::GetAddress_unchecked (offset_t *offset_ptr) const
806 {
807     return GetMaxU64_unchecked (offset_ptr, m_addr_size);
808 }
809
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.
815 //
816 // RETURNS the pointer that was extracted, or zero on failure.
817 //------------------------------------------------------------------
818 uint64_t
819 DataExtractor::GetPointer (offset_t *offset_ptr) const
820 {
821     return GetMaxU64 (offset_ptr, m_addr_size);
822 }
823
824 //----------------------------------------------------------------------
825 // GetDwarfEHPtr
826 //
827 // Used for calls when the value type is specified by a DWARF EH Frame
828 // pointer encoding.
829 //----------------------------------------------------------------------
830
831 uint64_t
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
833 {
834     if (eh_ptr_enc == DW_EH_PE_omit)
835         return ULLONG_MAX;  // Value isn't in the buffer...
836
837     uint64_t baseAddress = 0;
838     uint64_t addressValue = 0;
839     const uint32_t addr_size = GetAddressByteSize();
840
841     bool signExtendValue = false;
842     // Decode the base part or adjust our offset
843     switch (eh_ptr_enc & 0x70)
844     {
845     case DW_EH_PE_pcrel:
846         signExtendValue = true;
847         baseAddress = *offset_ptr;
848         if (pc_rel_addr != LLDB_INVALID_ADDRESS)
849             baseAddress += pc_rel_addr;
850 //      else
851 //          Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address.");
852         break;
853
854     case DW_EH_PE_textrel:
855         signExtendValue = true;
856         if (text_addr != LLDB_INVALID_ADDRESS)
857             baseAddress = text_addr;
858 //      else
859 //          Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero.");
860         break;
861
862     case DW_EH_PE_datarel:
863         signExtendValue = true;
864         if (data_addr != LLDB_INVALID_ADDRESS)
865             baseAddress = data_addr;
866 //      else
867 //          Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero.");
868         break;
869
870     case DW_EH_PE_funcrel:
871         signExtendValue = true;
872         break;
873
874     case DW_EH_PE_aligned:
875         {
876             // SetPointerSize should be called prior to extracting these so the
877             // pointer size is cached
878             assert(addr_size != 0);
879             if (addr_size)
880             {
881                 // Align to a address size boundary first
882                 uint32_t alignOffset = *offset_ptr % addr_size;
883                 if (alignOffset)
884                     offset_ptr += addr_size - alignOffset;
885             }
886         }
887         break;
888
889     default:
890     break;
891     }
892
893     // Decode the value part
894     switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING)
895     {
896     case DW_EH_PE_absptr    :
897         {
898             addressValue = GetAddress (offset_ptr);
899 //          if (data_relocs)
900 //              addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue);
901         }
902         break;
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;
911     default:
912     // Unhandled encoding type
913     assert(eh_ptr_enc);
914     break;
915     }
916
917     // Since we promote everything to 64 bit, we may need to sign extend
918     if (signExtendValue && addr_size < sizeof(baseAddress))
919     {
920         uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
921         if (sign_bit & addressValue)
922         {
923             uint64_t mask = ~sign_bit + 1;
924             addressValue |= mask;
925         }
926     }
927     return baseAddress + addressValue;
928 }
929
930 size_t
931 DataExtractor::ExtractBytes (offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const
932 {
933     const uint8_t *src = PeekData (offset, length);
934     if (src)
935     {
936         if (dst_byte_order != GetByteOrder())
937         {
938             for (uint32_t i=0; i<length; ++i)
939                 ((uint8_t*)dst)[i] = src[length - i - 1];
940         }
941         else
942             ::memcpy (dst, src, length);
943         return length;
944     }
945     return 0;
946 }
947
948 // Extract data and swap if needed when doing the copy
949 lldb::offset_t
950 DataExtractor::CopyByteOrderedData (offset_t src_offset,
951                                     offset_t src_len,
952                                     void *dst_void_ptr, 
953                                     offset_t dst_len, 
954                                     ByteOrder dst_byte_order) const
955 {
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);
961
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);
966     
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))
970         return 0;
971
972     uint32_t i;
973     uint8_t* dst = (uint8_t*)dst_void_ptr;
974     const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len);
975     if (src)
976     {
977         if (dst_len >= src_len)
978         {
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)
984             {
985                 // Big endian, so we lead with zeroes...
986                 if (num_zeroes > 0)
987                     ::memset (dst, 0, num_zeroes);
988                 // Then either copy or swap the rest
989                 if (m_byte_order == eByteOrderBig)
990                 {
991                     ::memcpy (dst + num_zeroes, src, src_len);
992                 }
993                 else
994                 {
995                     for (i=0; i<src_len; ++i)
996                         dst[i+num_zeroes] = src[src_len - 1 - i];
997                 }
998             }
999             else
1000             {
1001                 // Little endian destination, so we lead the value bytes
1002                 if (m_byte_order == eByteOrderBig)
1003                 {
1004                     for (i=0; i<src_len; ++i)
1005                         dst[i] = src[src_len - 1 - i];
1006                 }
1007                 else
1008                 {
1009                     ::memcpy (dst, src, src_len);
1010                 }
1011                 // And zero the rest...
1012                 if (num_zeroes > 0)
1013                     ::memset (dst + src_len, 0, num_zeroes);
1014             }
1015             return src_len;
1016         }
1017         else
1018         {
1019             // We are only copying some of the value from src into dst..
1020
1021             if (dst_byte_order == eByteOrderBig)
1022             {
1023                 // Big endian dst
1024                 if (m_byte_order == eByteOrderBig)
1025                 {
1026                     // Big endian dst, with big endian src
1027                     ::memcpy (dst, src + (src_len - dst_len), dst_len);
1028                 }
1029                 else
1030                 {
1031                     // Big endian dst, with little endian src
1032                     for (i=0; i<dst_len; ++i)
1033                         dst[i] = src[dst_len - 1 - i];
1034                 }
1035             }
1036             else
1037             {
1038                 // Little endian dst
1039                 if (m_byte_order == eByteOrderBig)
1040                 {
1041                     // Little endian dst, with big endian src
1042                     for (i=0; i<dst_len; ++i)
1043                         dst[i] = src[src_len - 1 - i];
1044                 }
1045                 else
1046                 {
1047                     // Little endian dst, with big endian src
1048                     ::memcpy (dst, src, dst_len);
1049                 }
1050             }
1051             return dst_len;
1052         }            
1053
1054     }
1055     return 0;
1056 }
1057
1058
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.
1064 //
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
1068 // updated.
1069 //----------------------------------------------------------------------
1070 const char*
1071 DataExtractor::GetCStr (offset_t *offset_ptr) const
1072 {
1073     const char *cstr = (const char *)PeekData (*offset_ptr, 1);
1074     if (cstr)
1075     {
1076         const char *cstr_end = cstr;
1077         const char *end = (const char *)m_end;
1078         while (cstr_end < end && *cstr_end)
1079             ++cstr_end;
1080
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')
1084         {
1085             // Advance the offset with one extra byte for the NULL terminator
1086             *offset_ptr += (cstr_end - cstr + 1);
1087             return cstr;
1088         }
1089         
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...
1094     }
1095     return NULL;
1096 }
1097
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.
1103 //
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 //----------------------------------------------------------------------
1109 const char*
1110 DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const
1111 {
1112     const char *cstr = (const char *)PeekData (*offset_ptr, len);
1113     if (cstr)
1114     {
1115         if (memchr (cstr, '\0', len) == NULL)
1116         {
1117             return NULL;
1118         }
1119         *offset_ptr += len;
1120         return cstr;
1121     }
1122     return NULL;
1123 }
1124
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.
1129 //
1130 // Returns a valid C string pointer if "offset" is a valid offset in
1131 // this object's data, else NULL is returned.
1132 //------------------------------------------------------------------
1133 const char *
1134 DataExtractor::PeekCStr (offset_t offset) const
1135 {
1136     return (const char *)PeekData (offset, 1);
1137 }
1138
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.
1144 //
1145 // Returned the extracted integer value.
1146 //----------------------------------------------------------------------
1147 uint64_t
1148 DataExtractor::GetULEB128 (offset_t *offset_ptr) const
1149 {
1150     const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1151     if (src == NULL)
1152         return 0;
1153     
1154     const uint8_t *end = m_end;
1155     
1156     if (src < end)
1157     {
1158         uint64_t result = *src++;
1159         if (result >= 0x80)
1160         {
1161             result &= 0x7f;
1162             int shift = 7;
1163             while (src < end)
1164             {
1165                 uint8_t byte = *src++;
1166                 result |= (byte & 0x7f) << shift;
1167                 if ((byte & 0x80) == 0)
1168                     break;
1169                 shift += 7;
1170             }
1171         }
1172         *offset_ptr = src - m_start;
1173         return result;
1174     }
1175     
1176     return 0;
1177 }
1178
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.
1184 //
1185 // Returned the extracted integer value.
1186 //----------------------------------------------------------------------
1187 int64_t
1188 DataExtractor::GetSLEB128 (offset_t *offset_ptr) const
1189 {
1190     const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1191     if (src == NULL)
1192         return 0;
1193     
1194     const uint8_t *end = m_end;
1195     
1196     if (src < end)
1197     {
1198         int64_t result = 0;
1199         int shift = 0;
1200         int size = sizeof (int64_t) * 8;
1201
1202         uint8_t byte = 0;
1203         int bytecount = 0;
1204
1205         while (src < end)
1206         {
1207             bytecount++;
1208             byte = *src++;
1209             result |= (byte & 0x7f) << shift;
1210             shift += 7;
1211             if ((byte & 0x80) == 0)
1212                 break;
1213         }
1214
1215         // Sign bit of byte is 2nd high order bit (0x40)
1216         if (shift < size && (byte & 0x40))
1217             result |= - (1 << shift);
1218
1219         *offset_ptr += bytecount;
1220         return result;
1221     }
1222     return 0;
1223 }
1224
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.
1230 //
1231 // Returns the number of bytes consumed during the extraction.
1232 //----------------------------------------------------------------------
1233 uint32_t
1234 DataExtractor::Skip_LEB128 (offset_t *offset_ptr) const
1235 {
1236     uint32_t bytes_consumed = 0;
1237     const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
1238     if (src == NULL)
1239         return 0;
1240         
1241     const uint8_t *end = m_end;
1242     
1243     if (src < end)
1244     {
1245         const uint8_t *src_pos = src;
1246         while ((src_pos < end) && (*src_pos++ & 0x80))
1247             ++bytes_consumed;
1248         *offset_ptr += src_pos - src;
1249     }
1250     return bytes_consumed;
1251 }
1252
1253 static bool
1254 GetAPInt (const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size, llvm::APInt &result)
1255 {
1256     llvm::SmallVector<uint64_t, 2> uint64_array;
1257     lldb::offset_t bytes_left = byte_size;
1258     uint64_t u64;
1259     const lldb::ByteOrder byte_order = data.GetByteOrder();
1260     if (byte_order == lldb::eByteOrderLittle)
1261     {
1262         while (bytes_left > 0)
1263         {
1264             if (bytes_left >= 8)
1265             {
1266                 u64 = data.GetU64(offset_ptr);
1267                 bytes_left -= 8;
1268             }
1269             else
1270             {
1271                 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
1272                 bytes_left = 0;
1273             }
1274             uint64_array.push_back(u64);
1275         }
1276         result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1277         return true;
1278     }
1279     else if (byte_order == lldb::eByteOrderBig)
1280     {
1281         lldb::offset_t be_offset = *offset_ptr + byte_size;
1282         lldb::offset_t temp_offset;
1283         while (bytes_left > 0)
1284         {
1285             if (bytes_left >= 8)
1286             {
1287                 be_offset -= 8;
1288                 temp_offset = be_offset;
1289                 u64 = data.GetU64(&temp_offset);
1290                 bytes_left -= 8;
1291             }
1292             else
1293             {
1294                 be_offset -= bytes_left;
1295                 temp_offset = be_offset;
1296                 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
1297                 bytes_left = 0;
1298             }
1299             uint64_array.push_back(u64);
1300         }
1301         *offset_ptr += byte_size;
1302         result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
1303         return true;
1304     }
1305     return false;
1306 }
1307
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)
1310 {
1311     llvm::APInt apint;
1312     if (GetAPInt (data, &offset, byte_size, apint))
1313     {
1314         std::string apint_str(apint.toString(radix, is_signed));
1315         switch (radix)
1316         {
1317             case 2:
1318                 s->Write ("0b", 2);
1319                 break;
1320             case 8:
1321                 s->Write ("0", 1);
1322                 break;
1323             case 10:
1324                 break;
1325         }
1326         s->Write(apint_str.c_str(), apint_str.size());
1327     }
1328     return offset;
1329 }
1330
1331 static float half2float (uint16_t half)
1332 {
1333     union{ float       f; uint32_t    u;}u;
1334     int32_t v = (int16_t) half;
1335     
1336     if( 0 == (v & 0x7c00))
1337     {
1338         u.u = v & 0x80007FFFU;
1339         return u.f * 0x1.0p125f;
1340     }
1341     
1342     v <<= 13;
1343     u.u = v | 0x70000000U;
1344     return u.f * 0x1.0p-112f;
1345 }
1346
1347 lldb::offset_t
1348 DataExtractor::Dump (Stream *s,
1349                      offset_t start_offset,
1350                      lldb::Format item_format,
1351                      size_t item_byte_size,
1352                      size_t item_count,
1353                      size_t num_per_line,
1354                      uint64_t base_addr,
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
1358 {
1359     if (s == NULL)
1360         return start_offset;
1361
1362     if (item_format == eFormatPointer)
1363     {
1364         if (item_byte_size != 4 && item_byte_size != 8)
1365             item_byte_size = s->GetAddressByteSize();
1366     }
1367     
1368     offset_t offset = start_offset;
1369
1370     if (item_format == eFormatInstruction)
1371     {
1372         TargetSP target_sp;
1373         if (exe_scope)
1374             target_sp = exe_scope->CalculateTarget();
1375         if (target_sp)
1376         {
1377             DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL,  NULL));
1378             if (disassembler_sp)
1379             {
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))
1384                 {
1385                     data_from_file = false;
1386                 }
1387                 else
1388                 {
1389                     if (target_sp->GetSectionLoadList().IsEmpty() || !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
1390                         so_addr.SetRawAddress(addr);
1391                 }
1392
1393                 size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false, data_from_file);
1394                 
1395                 if (bytes_consumed)
1396                 {
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);
1403                     
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();
1407                 }
1408             }
1409         }
1410         else
1411             s->Printf ("invalid target");
1412
1413         return offset;
1414     }
1415
1416     if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8)
1417         item_format = eFormatHex;
1418
1419     lldb::offset_t line_start_offset = start_offset;
1420     for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count)
1421     {
1422         if ((count % num_per_line) == 0)
1423         {
1424             if (count > 0)
1425             {
1426                 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1427                 {
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);
1430                 }
1431                 s->EOL();
1432             }
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;
1436         }
1437         else
1438         if (item_format != eFormatChar &&
1439             item_format != eFormatCharPrintable &&
1440             item_format != eFormatCharArray &&
1441             count > 0)
1442         {
1443             s->PutChar(' ');
1444         }
1445
1446         uint32_t i;
1447         switch (item_format)
1448         {
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");
1452             else
1453             {
1454                 s->Printf("error: unsupported byte size (%zu) for boolean format", item_byte_size);
1455                 return offset;
1456             }
1457             break;
1458
1459         case eFormatBinary:
1460             if (item_byte_size <= 8)
1461             {
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)
1468                     if (bits[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);
1474             }
1475             else
1476             {
1477                 const bool is_signed = false;
1478                 const unsigned radix = 2;
1479                 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1480             }
1481             break;
1482
1483         case eFormatBytes:
1484         case eFormatBytesWithASCII:
1485             for (i=0; i<item_byte_size; ++i)
1486             {
1487                 s->Printf ("%2.2x", GetU8(&offset));
1488             }
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)
1492                 s->PutChar(' ');
1493             break;
1494
1495         case eFormatChar:
1496         case eFormatCharPrintable:
1497         case eFormatCharArray:
1498             {
1499                 // If we are only printing one character surround it with single
1500                 // quotes
1501                 if (item_count == 1 && item_format == eFormatChar)
1502                     s->PutChar('\'');
1503
1504                 const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1505                 if (isprint(ch))
1506                     s->Printf ("%c", (char)ch);
1507                 else if (item_format != eFormatCharPrintable)
1508                 {
1509                     switch (ch)
1510                     {
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;
1520                     default:   
1521                         if (item_byte_size == 1)
1522                             s->Printf ("\\x%2.2x", (uint8_t)ch); 
1523                         else
1524                             s->Printf ("%" PRIu64, ch);
1525                         break;
1526                     }
1527                 }
1528                 else
1529                 {
1530                     s->PutChar(NON_PRINTABLE_CHAR);
1531                 }
1532
1533                 // If we are only printing one character surround it with single quotes
1534                 if (item_count == 1 && item_format == eFormatChar)
1535                     s->PutChar('\'');
1536             }
1537             break;
1538
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));
1543             else
1544             {
1545                 const bool is_signed = true;
1546                 const unsigned radix = 10;
1547                 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1548             }
1549             break;
1550
1551         case eFormatUnsigned:
1552             if (item_byte_size <= 8)
1553                 s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1554             else
1555             {
1556                 const bool is_signed = false;
1557                 const unsigned radix = 10;
1558                 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1559             }
1560             break;
1561
1562         case eFormatOctal:
1563             if (item_byte_size <= 8)
1564                 s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
1565             else
1566             {
1567                 const bool is_signed = false;
1568                 const unsigned radix = 8;
1569                 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1570             }
1571             break;
1572
1573         case eFormatOSType:
1574             {
1575                 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1576                 s->PutChar('\'');
1577                 for (i=0; i<item_byte_size; ++i)
1578                 {
1579                     uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
1580                     if (isprint(ch))
1581                         s->Printf ("%c", ch);
1582                     else
1583                     {
1584                         switch (ch)
1585                         {
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;
1596                         }
1597                     }
1598                 }
1599                 s->PutChar('\'');
1600             }
1601             break;
1602             
1603         case eFormatCString:
1604             {
1605                 const char *cstr = GetCStr(&offset);
1606                 
1607                 if (!cstr)
1608                 {
1609                     s->Printf("NULL");
1610                     offset = LLDB_INVALID_OFFSET;
1611                 }
1612                 else
1613                 {
1614                     s->PutChar('\"');
1615                     
1616                     while (const char c = *cstr)
1617                     {                    
1618                         if (isprint(c))
1619                         {
1620                             s->PutChar(c);
1621                         }
1622                         else
1623                         {
1624                             switch (c)
1625                             {
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;
1635                             }
1636                         }
1637                         
1638                         ++cstr;
1639                     }
1640                     
1641                     s->PutChar('\"');
1642                 }
1643             }
1644             break;
1645
1646
1647         case eFormatPointer:
1648             s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t));
1649             break;
1650
1651
1652         case eFormatComplexInteger:
1653             {
1654                 size_t complex_int_byte_size = item_byte_size / 2;
1655                 
1656                 if (complex_int_byte_size <= 8)
1657                 {
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));
1660                 }
1661                 else
1662                 {
1663                     s->Printf("error: unsupported byte size (%zu) for complex integer format", item_byte_size);
1664                     return offset;
1665                 }
1666             }
1667             break;
1668
1669         case eFormatComplex:
1670             if (sizeof(float) * 2 == item_byte_size)
1671             {
1672                 float f32_1 = GetFloat (&offset);
1673                 float f32_2 = GetFloat (&offset);
1674
1675                 s->Printf ("%g + %gi", f32_1, f32_2);
1676                 break;
1677             }
1678             else if (sizeof(double) * 2 == item_byte_size)
1679             {
1680                 double d64_1 = GetDouble (&offset);
1681                 double d64_2 = GetDouble (&offset);
1682
1683                 s->Printf ("%lg + %lgi", d64_1, d64_2);
1684                 break;
1685             }
1686             else if (sizeof(long double) * 2 == item_byte_size)
1687             {
1688                 long double ld64_1 = GetLongDouble (&offset);
1689                 long double ld64_2 = GetLongDouble (&offset);
1690                 s->Printf ("%Lg + %Lgi", ld64_1, ld64_2);
1691                 break;
1692             }
1693             else
1694             {
1695                 s->Printf("error: unsupported byte size (%zu) for complex float format", item_byte_size);
1696                 return offset;
1697             }
1698             break;
1699
1700         default:
1701         case eFormatDefault:
1702         case eFormatHex:
1703         case eFormatHexUppercase:
1704             {
1705                 bool wantsuppercase  = (item_format == eFormatHexUppercase);
1706                 if (item_byte_size <= 8)
1707                 {
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));
1709                 }
1710                 else
1711                 {
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);
1715                     if (bytes)
1716                     {
1717                         uint32_t idx;
1718                         if (m_byte_order == eByteOrderBig)
1719                         {
1720                             for (idx = 0; idx < item_byte_size; ++idx)
1721                                 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
1722                         }
1723                         else
1724                         {
1725                             for (idx = 0; idx < item_byte_size; ++idx)
1726                                 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]);
1727                         }
1728                     }
1729                 }
1730             }
1731             break;
1732
1733         case eFormatFloat:
1734             {
1735                 TargetSP target_sp;
1736                 bool used_apfloat = false;
1737                 if (exe_scope)
1738                     target_sp = exe_scope->CalculateTarget();
1739                 if (target_sp)
1740                 {
1741                     ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1742                     if (clang_ast)
1743                     {
1744                         clang::ASTContext *ast = clang_ast->getASTContext();
1745                         if (ast)
1746                         {
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;
1752                             
1753                             if (item_bit_size == ast->getTypeSize(ast->FloatTy))
1754                             {
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);
1758                             }
1759                             else if (item_bit_size == ast->getTypeSize(ast->DoubleTy))
1760                             {
1761                                 llvm::APInt apint;
1762                                 if (GetAPInt (*this, &offset, item_byte_size, apint))
1763                                 {
1764                                     llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->DoubleTy), apint);
1765                                     apfloat.toString(sv, format_precision, format_max_padding);
1766                                 }
1767                             }
1768                             else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy))
1769                             {
1770                                 llvm::APInt apint;
1771                                 switch (target_sp->GetArchitecture().GetCore())
1772                                 {
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))
1779                                         {
1780                                             llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1781                                             apfloat.toString(sv, format_precision, format_max_padding);
1782                                         }
1783                                         break;
1784                                         
1785                                     default:
1786                                         if (GetAPInt (*this, &offset, item_byte_size, apint))
1787                                         {
1788                                             llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint);
1789                                             apfloat.toString(sv, format_precision, format_max_padding);
1790                                         }
1791                                         break;
1792                                 }
1793                             }
1794                             else if (item_bit_size == ast->getTypeSize(ast->HalfTy))
1795                             {
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);
1799                             }
1800
1801                             if (!sv.empty())
1802                             {
1803                                 s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
1804                                 used_apfloat = true;
1805                             }
1806                         }
1807                     }
1808                 }
1809                 
1810                 if (!used_apfloat)
1811                 {
1812                     std::ostringstream ss;
1813                     if (item_byte_size == sizeof(float) || item_byte_size == 2)
1814                     {
1815                         float f;
1816                         if (item_byte_size == 2)
1817                         {
1818                             uint16_t half = this->GetU16(&offset);
1819                             f = half2float(half);
1820                         }
1821                         else
1822                         {
1823                             f = GetFloat (&offset);
1824                         }
1825                         ss.precision(std::numeric_limits<float>::digits10);
1826                         ss << f;
1827                     } 
1828                     else if (item_byte_size == sizeof(double))
1829                     {
1830                         ss.precision(std::numeric_limits<double>::digits10);
1831                         ss << GetDouble(&offset);
1832                     }
1833                     else if (item_byte_size == sizeof(long double) || item_byte_size == 10)
1834                     {
1835                         ss.precision(std::numeric_limits<long double>::digits10);
1836                         ss << GetLongDouble(&offset);
1837                     }
1838                     else
1839                     {
1840                         s->Printf("error: unsupported byte size (%zu) for float format", item_byte_size);
1841                         return offset;
1842                     }
1843                     ss.flush();
1844                     s->Printf("%s", ss.str().c_str());
1845                 }
1846             }
1847             break;
1848
1849         case eFormatUnicode16:
1850             s->Printf("U+%4.4x", GetU16 (&offset));
1851             break;
1852
1853         case eFormatUnicode32:
1854             s->Printf("U+0x%8.8x", GetU32 (&offset));
1855             break;
1856
1857         case eFormatAddressInfo:
1858             {
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);
1861                 if (exe_scope)
1862                 {
1863                     TargetSP target_sp (exe_scope->CalculateTarget());
1864                     lldb_private::Address so_addr;
1865                     if (target_sp)
1866                     {
1867                         if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1868                         {
1869                             s->PutChar(' ');
1870                             so_addr.Dump (s,
1871                                           exe_scope,
1872                                           Address::DumpStyleResolvedDescription,
1873                                           Address::DumpStyleModuleWithFileAddress);
1874                         }
1875                         else
1876                         {
1877                             so_addr.SetOffset(addr);
1878                             so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription);
1879                         }
1880                     }
1881                 }
1882             }
1883             break;
1884
1885         case eFormatHexFloat:
1886             if (sizeof(float) == item_byte_size)
1887             {
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);
1892                 break;
1893             }
1894             else if (sizeof(double) == item_byte_size)
1895             {
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);
1900                 break;
1901             }
1902             else
1903             {
1904                 s->Printf("error: unsupported byte size (%zu) for hex float format", item_byte_size);
1905                 return offset;
1906             }
1907             break;
1908
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);
1915             s->PutChar('}');
1916             break;
1917
1918         case eFormatVectorOfSInt8:
1919             s->PutChar('{');
1920             offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1921             s->PutChar('}');
1922             break;
1923
1924         case eFormatVectorOfUInt8:
1925             s->PutChar('{');
1926             offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
1927             s->PutChar('}');
1928             break;
1929
1930         case eFormatVectorOfSInt16:
1931             s->PutChar('{');
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);
1933             s->PutChar('}');
1934             break;
1935
1936         case eFormatVectorOfUInt16:
1937             s->PutChar('{');
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);
1939             s->PutChar('}');
1940             break;
1941
1942         case eFormatVectorOfSInt32:
1943             s->PutChar('{');
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);
1945             s->PutChar('}');
1946             break;
1947
1948         case eFormatVectorOfUInt32:
1949             s->PutChar('{');
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);
1951             s->PutChar('}');
1952             break;
1953
1954         case eFormatVectorOfSInt64:
1955             s->PutChar('{');
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);
1957             s->PutChar('}');
1958             break;
1959
1960         case eFormatVectorOfUInt64:
1961             s->PutChar('{');
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);
1963             s->PutChar('}');
1964             break;
1965
1966         case eFormatVectorOfFloat32:
1967             s->PutChar('{');
1968             offset = Dump (s, offset, eFormatFloat,       4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
1969             s->PutChar('}');
1970             break;
1971
1972         case eFormatVectorOfFloat64:
1973             s->PutChar('{');
1974             offset = Dump (s, offset, eFormatFloat,       8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
1975             s->PutChar('}');
1976             break;
1977
1978         case eFormatVectorOfUInt128:
1979             s->PutChar('{');
1980             offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
1981             s->PutChar('}');
1982             break;
1983         }
1984     }
1985
1986     if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1987     {
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);
1990     }
1991     return offset;  // Return the offset at which we ended up
1992 }
1993
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 //----------------------------------------------------------------------
2006 lldb::offset_t
2007 DataExtractor::PutToLog
2008 (
2009     Log *log,
2010     offset_t start_offset,
2011     offset_t length,
2012     uint64_t base_addr,
2013     uint32_t num_per_line,
2014     DataExtractor::Type type,
2015     const char *format
2016 ) const
2017 {
2018     if (log == NULL)
2019         return start_offset;
2020
2021     offset_t offset;
2022     offset_t end_offset;
2023     uint32_t count;
2024     StreamString sstr;
2025     for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count)
2026     {
2027         if ((count % num_per_line) == 0)
2028         {
2029             // Print out any previous string
2030             if (sstr.GetSize() > 0)
2031             {
2032                 log->Printf("%s", sstr.GetData());
2033                 sstr.Clear();
2034             }
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)));
2038         }
2039
2040         switch (type)
2041         {
2042             case TypeUInt8:   sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break;
2043             case TypeChar:
2044                 {
2045                     char ch = GetU8(&offset);
2046                     sstr.Printf (format ? format : " %c",    isprint(ch) ? ch : ' ');
2047                 }
2048                 break;
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;
2055         }
2056     }
2057
2058     if (sstr.GetSize() > 0)
2059         log->Printf("%s", sstr.GetData());
2060
2061     return offset;  // Return the offset at which we ended up
2062 }
2063
2064 //----------------------------------------------------------------------
2065 // DumpUUID
2066 //
2067 // Dump out a UUID starting at 'offset' bytes into the buffer
2068 //----------------------------------------------------------------------
2069 void
2070 DataExtractor::DumpUUID (Stream *s, offset_t offset) const
2071 {
2072     if (s)
2073     {
2074         const uint8_t *uuid_data = PeekData(offset, 16);
2075         if ( uuid_data )
2076         {
2077             lldb_private::UUID uuid(uuid_data, 16);
2078             uuid.Dump(s);
2079         }
2080         else
2081         {
2082             s->Printf("<not enough data for UUID at offset 0x%8.8" PRIx64 ">", offset);
2083         }
2084     }
2085 }
2086
2087 void
2088 DataExtractor::DumpHexBytes (Stream *s, 
2089                              const void *src, 
2090                              size_t src_len, 
2091                              uint32_t bytes_per_line,
2092                              addr_t base_addr)
2093 {
2094     DataExtractor data (src, src_len, eByteOrderLittle, 4);
2095     data.Dump (s, 
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
2103 }
2104
2105 size_t
2106 DataExtractor::Copy (DataExtractor &dest_data) const
2107 {
2108     if (m_data_sp.get())
2109     {
2110         // we can pass along the SP to the data
2111         dest_data.SetData(m_data_sp);
2112     }
2113     else
2114     {
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)));
2118     }
2119     return GetByteSize();
2120 }
2121
2122 bool
2123 DataExtractor::Append(DataExtractor& rhs)
2124 {
2125     if (rhs.GetByteOrder() != GetByteOrder())
2126         return false;
2127     
2128     if (rhs.GetByteSize() == 0)
2129         return true;
2130     
2131     if (GetByteSize() == 0)
2132         return (rhs.Copy(*this) > 0);
2133     
2134     size_t bytes = GetByteSize() + rhs.GetByteSize();
2135
2136     DataBufferHeap *buffer_heap_ptr = NULL;
2137     DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2138     
2139     if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2140         return false;
2141     
2142     uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2143     
2144     memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2145     memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
2146     
2147     SetData(buffer_sp);
2148     
2149     return true;
2150 }
2151
2152 bool
2153 DataExtractor::Append(void* buf, offset_t length)
2154 {
2155     if (buf == NULL)
2156         return false;
2157     
2158     if (length == 0)
2159         return true;
2160     
2161     size_t bytes = GetByteSize() + length;
2162     
2163     DataBufferHeap *buffer_heap_ptr = NULL;
2164     DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2165     
2166     if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2167         return false;
2168     
2169     uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2170     
2171     if (GetByteSize() > 0)
2172         memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2173
2174     memcpy(bytes_ptr + GetByteSize(), buf, length);
2175     
2176     SetData(buffer_sp);
2177     
2178     return true;
2179 }