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