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