LLDB mainline
DataExtractor.cpp
Go to the documentation of this file.
1//===-- DataExtractor.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "lldb/lldb-defines.h"
13#include "lldb/lldb-forward.h"
14#include "lldb/lldb-types.h"
15
19#include "lldb/Utility/Log.h"
20#include "lldb/Utility/Stream.h"
22#include "lldb/Utility/UUID.h"
23
24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringExtras.h"
27#include "llvm/Support/LEB128.h"
28#include "llvm/Support/MD5.h"
29#include "llvm/Support/MathExtras.h"
30
31#include <algorithm>
32#include <array>
33#include <cassert>
34#include <cstdint>
35#include <string>
36
37#include <cctype>
38#include <cinttypes>
39#include <cstring>
40
41using namespace lldb;
42using namespace lldb_private;
43
44static inline uint16_t ReadInt16(const unsigned char *ptr, offset_t offset) {
45 uint16_t value;
46 memcpy(&value, ptr + offset, 2);
47 return value;
48}
49
50static inline uint32_t ReadInt32(const unsigned char *ptr,
51 offset_t offset = 0) {
52 uint32_t value;
53 memcpy(&value, ptr + offset, 4);
54 return value;
55}
56
57static inline uint64_t ReadInt64(const unsigned char *ptr,
58 offset_t offset = 0) {
59 uint64_t value;
60 memcpy(&value, ptr + offset, 8);
61 return value;
62}
63
64static inline uint16_t ReadInt16(const void *ptr) {
65 uint16_t value;
66 memcpy(&value, ptr, 2);
67 return value;
68}
69
70static inline uint16_t ReadSwapInt16(const unsigned char *ptr,
71 offset_t offset) {
72 uint16_t value;
73 memcpy(&value, ptr + offset, 2);
74 return llvm::byteswap<uint16_t>(value);
75}
76
77static inline uint32_t ReadSwapInt32(const unsigned char *ptr,
78 offset_t offset) {
79 uint32_t value;
80 memcpy(&value, ptr + offset, 4);
81 return llvm::byteswap<uint32_t>(value);
82}
83
84static inline uint64_t ReadSwapInt64(const unsigned char *ptr,
85 offset_t offset) {
86 uint64_t value;
87 memcpy(&value, ptr + offset, 8);
88 return llvm::byteswap<uint64_t>(value);
89}
90
91static inline uint16_t ReadSwapInt16(const void *ptr) {
92 uint16_t value;
93 memcpy(&value, ptr, 2);
94 return llvm::byteswap<uint16_t>(value);
95}
96
97static inline uint32_t ReadSwapInt32(const void *ptr) {
98 uint32_t value;
99 memcpy(&value, ptr, 4);
100 return llvm::byteswap<uint32_t>(value);
101}
102
103static inline uint64_t ReadSwapInt64(const void *ptr) {
104 uint64_t value;
105 memcpy(&value, ptr, 8);
106 return llvm::byteswap<uint64_t>(value);
107}
108
109static inline uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size,
110 ByteOrder byte_order) {
111 uint64_t res = 0;
112 if (byte_order == eByteOrderBig)
113 for (size_t i = 0; i < byte_size; ++i)
114 res = (res << 8) | data[i];
115 else {
116 assert(byte_order == eByteOrderLittle);
117 for (size_t i = 0; i < byte_size; ++i)
118 res = (res << 8) | data[byte_size - 1 - i];
119 }
120 return res;
121}
122
124 : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
125 m_data_sp() {}
126
127// This constructor allows us to use data that is owned by someone else. The
128// data must stay around as long as this object is valid.
129DataExtractor::DataExtractor(const void *data, offset_t length,
130 ByteOrder endian, uint32_t addr_size)
131 : m_start(const_cast<uint8_t *>(static_cast<const uint8_t *>(data))),
132 m_end(const_cast<uint8_t *>(static_cast<const uint8_t *>(data)) + length),
133 m_byte_order(endian), m_addr_size(addr_size), m_data_sp() {
134 assert(addr_size >= 1 && addr_size <= 8);
135}
136
137// Make a shared pointer reference to the shared data in "data_sp" and set the
138// endian swapping setting to "swap", and the address size to "addr_size". The
139// shared data reference will ensure the data lives as long as any
140// DataExtractor objects exist that have a reference to this data.
142 uint32_t addr_size)
143 : m_byte_order(endian), m_addr_size(addr_size), m_data_sp() {
144 assert(addr_size >= 1 && addr_size <= 8);
145 SetData(data_sp);
146}
147
148// Make a shared pointer reference to the shared data in "data_sp".
150 : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
151 m_data_sp(data_sp) {
152 if (data_sp)
153 SetData(data_sp);
154}
155
156// Initialize this object with a subset of the data bytes in "data". If "data"
157// contains shared data, then a reference to this shared data will added and
158// the shared data will stay around as long as any object contains a reference
159// to that data. The endian swap and address size settings are copied from
160// "data".
162 offset_t length)
164 m_data_sp() {
165 assert(m_addr_size >= 1 && m_addr_size <= 8);
166 if (data.ValidOffset(offset)) {
167 offset_t bytes_available = data.GetByteSize() - offset;
168 if (length > bytes_available)
169 length = bytes_available;
170 SetData(data, offset, length);
171 }
172}
173
179
180// Assignment operator
182 if (this != &rhs) {
183 m_start = rhs.m_start;
184 m_end = rhs.m_end;
187 m_data_sp = rhs.m_data_sp;
188 }
189 return *this;
190}
191
193
194// Clears the object contents back to a default invalid state, and release any
195// references to shared data that this object may contain.
197 m_start = nullptr;
198 m_end = nullptr;
200 m_addr_size = sizeof(void *);
201 m_data_sp.reset();
202}
203
204// If this object contains shared data, this function returns the offset into
205// that shared data. Else zero is returned.
207 if (m_start != nullptr) {
208 const DataBuffer *data = m_data_sp.get();
209 if (data != nullptr) {
210 const uint8_t *data_bytes = data->GetBytes();
211 if (data_bytes != nullptr) {
212 assert(m_start >= data_bytes);
213 return m_start - data_bytes;
214 }
215 }
216 }
217 return 0;
218}
219
220// Set the data with which this object will extract from to data starting at
221// BYTES and set the length of the data to LENGTH bytes long. The data is
222// externally owned must be around at least as long as this object points to
223// the data. No copy of the data is made, this object just refers to this data
224// and can extract from it. If this object refers to any shared data upon
225// entry, the reference to that data will be released. Is SWAP is set to true,
226// any data extracted will be endian swapped.
230 m_data_sp.reset();
231 if (bytes == nullptr || length == 0) {
232 m_start = nullptr;
233 m_end = nullptr;
234 } else {
235 m_start = const_cast<uint8_t *>(static_cast<const uint8_t *>(bytes));
236 m_end = m_start + length;
237 }
238 return GetByteSize();
239}
240
241// Assign the data for this object to be a subrange in "data" starting
242// "data_offset" bytes into "data" and ending "data_length" bytes later. If
243// "data_offset" is not a valid offset into "data", then this object will
244// contain no bytes. If "data_offset" is within "data" yet "data_length" is too
245// large, the length will be capped at the number of bytes remaining in "data".
246// If "data" contains a shared pointer to other data, then a ref counted
247// pointer to that data will be made in this object. If "data" doesn't contain
248// a shared pointer to data, then the bytes referred to in "data" will need to
249// exist at least as long as this object refers to those bytes. The address
250// size and endian swap settings are copied from the current values in "data".
252 offset_t data_offset,
253 offset_t data_length) {
255 assert(m_addr_size >= 1 && m_addr_size <= 8);
256 // If "data" contains shared pointer to data, then we can use that
257 if (data.m_data_sp) {
259 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset,
260 data_length);
261 }
262
263 // We have a DataExtractor object that just has a pointer to bytes
264 if (data.ValidOffset(data_offset)) {
265 if (data_length > data.GetByteSize() - data_offset)
266 data_length = data.GetByteSize() - data_offset;
267 return SetData(data.GetDataStart() + data_offset, data_length,
268 data.GetByteOrder());
269 }
270 return 0;
271}
272
273// Assign the data for this object to be a subrange of the shared data in
274// "data_sp" starting "data_offset" bytes into "data_sp" and ending
275// "data_length" bytes later. If "data_offset" is not a valid offset into
276// "data_sp", then this object will contain no bytes. If "data_offset" is
277// within "data_sp" yet "data_length" is too large, the length will be capped
278// at the number of bytes remaining in "data_sp". A ref counted pointer to the
279// data in "data_sp" will be made in this object IF the number of bytes this
280// object refers to in greater than zero (if at least one byte was available
281// starting at "data_offset") to ensure the data stays around as long as it is
282// needed. The address size and endian swap settings will remain unchanged from
283// their current settings.
285 offset_t data_offset,
286 offset_t data_length) {
287 m_start = m_end = nullptr;
288
289 if (data_length > 0) {
290 m_data_sp = data_sp;
291 if (data_sp) {
292 const size_t data_size = data_sp->GetByteSize();
293 if (data_offset < data_size) {
294 m_start = data_sp->GetBytes() + data_offset;
295 const size_t bytes_left = data_size - data_offset;
296 // Cap the length of we asked for too many
297 if (data_length <= bytes_left)
298 m_end = m_start + data_length; // We got all the bytes we wanted
299 else
300 m_end = m_start + bytes_left; // Not all the bytes requested were
301 // available in the shared data
302 }
303 }
304 }
305
306 size_t new_size = GetByteSize();
307
308 // Don't hold a shared pointer to the data buffer if we don't share any valid
309 // bytes in the shared buffer.
310 if (new_size == 0)
311 m_data_sp.reset();
312
313 return new_size;
314}
315
316// Extract a single unsigned char from the binary data and update the offset
317// pointed to by "offset_ptr".
318//
319// RETURNS the byte that was extracted, or zero on failure.
320uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const {
321 const uint8_t *data = static_cast<const uint8_t *>(GetData(offset_ptr, 1));
322 if (data)
323 return *data;
324 return 0;
325}
326
327// Extract "count" unsigned chars from the binary data and update the offset
328// pointed to by "offset_ptr". The extracted data is copied into "dst".
329//
330// RETURNS the non-nullptr buffer pointer upon successful extraction of
331// all the requested bytes, or nullptr when the data is not available in the
332// buffer due to being out of bounds, or insufficient data.
333void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst,
334 uint32_t count) const {
335 const uint8_t *data =
336 static_cast<const uint8_t *>(GetData(offset_ptr, count));
337 if (data) {
338 // Copy the data into the buffer
339 memcpy(dst, data, count);
340 // Return a non-nullptr pointer to the converted data as an indicator of
341 // success
342 return dst;
343 }
344 return nullptr;
345}
346
347// Extract a single uint16_t from the data and update the offset pointed to by
348// "offset_ptr".
349//
350// RETURNS the uint16_t that was extracted, or zero on failure.
351uint16_t DataExtractor::GetU16(offset_t *offset_ptr) const {
352 uint16_t val = 0;
353 const uint8_t *data =
354 static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
355 if (data) {
357 val = ReadSwapInt16(data);
358 else
359 val = ReadInt16(data);
360 }
361 return val;
362}
363
364uint16_t DataExtractor::GetU16_unchecked(offset_t *offset_ptr) const {
365 uint16_t val;
367 val = ReadInt16(m_start, *offset_ptr);
368 else
369 val = ReadSwapInt16(m_start, *offset_ptr);
370 *offset_ptr += sizeof(val);
371 return val;
372}
373
374uint32_t DataExtractor::GetU32_unchecked(offset_t *offset_ptr) const {
375 uint32_t val;
377 val = ReadInt32(m_start, *offset_ptr);
378 else
379 val = ReadSwapInt32(m_start, *offset_ptr);
380 *offset_ptr += sizeof(val);
381 return val;
382}
383
384uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const {
385 uint64_t val;
387 val = ReadInt64(m_start, *offset_ptr);
388 else
389 val = ReadSwapInt64(m_start, *offset_ptr);
390 *offset_ptr += sizeof(val);
391 return val;
392}
393
394// Extract "count" uint16_t values from the binary data and update the offset
395// pointed to by "offset_ptr". The extracted data is copied into "dst".
396//
397// RETURNS the non-nullptr buffer pointer upon successful extraction of
398// all the requested bytes, or nullptr when the data is not available in the
399// buffer due to being out of bounds, or insufficient data.
400void *DataExtractor::GetU16(offset_t *offset_ptr, void *void_dst,
401 uint32_t count) const {
402 const size_t src_size = sizeof(uint16_t) * count;
403 const uint16_t *src =
404 static_cast<const uint16_t *>(GetData(offset_ptr, src_size));
405 if (src) {
407 uint16_t *dst_pos = static_cast<uint16_t *>(void_dst);
408 uint16_t *dst_end = dst_pos + count;
409 const uint16_t *src_pos = src;
410 while (dst_pos < dst_end) {
411 *dst_pos = ReadSwapInt16(src_pos);
412 ++dst_pos;
413 ++src_pos;
414 }
415 } else {
416 memcpy(void_dst, src, src_size);
417 }
418 // Return a non-nullptr pointer to the converted data as an indicator of
419 // success
420 return void_dst;
421 }
422 return nullptr;
423}
424
425// Extract a single uint32_t from the data and update the offset pointed to by
426// "offset_ptr".
427//
428// RETURNS the uint32_t that was extracted, or zero on failure.
429uint32_t DataExtractor::GetU32(offset_t *offset_ptr) const {
430 uint32_t val = 0;
431 const uint8_t *data =
432 static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
433 if (data) {
435 val = ReadSwapInt32(data);
436 } else {
437 memcpy(&val, data, 4);
438 }
439 }
440 return val;
441}
442
443// Extract "count" uint32_t values from the binary data and update the offset
444// pointed to by "offset_ptr". The extracted data is copied into "dst".
445//
446// RETURNS the non-nullptr buffer pointer upon successful extraction of
447// all the requested bytes, or nullptr when the data is not available in the
448// buffer due to being out of bounds, or insufficient data.
449void *DataExtractor::GetU32(offset_t *offset_ptr, void *void_dst,
450 uint32_t count) const {
451 const size_t src_size = sizeof(uint32_t) * count;
452 const uint32_t *src =
453 static_cast<const uint32_t *>(GetData(offset_ptr, src_size));
454 if (src) {
456 uint32_t *dst_pos = static_cast<uint32_t *>(void_dst);
457 uint32_t *dst_end = dst_pos + count;
458 const uint32_t *src_pos = src;
459 while (dst_pos < dst_end) {
460 *dst_pos = ReadSwapInt32(src_pos);
461 ++dst_pos;
462 ++src_pos;
463 }
464 } else {
465 memcpy(void_dst, src, src_size);
466 }
467 // Return a non-nullptr pointer to the converted data as an indicator of
468 // success
469 return void_dst;
470 }
471 return nullptr;
472}
473
474// Extract a single uint64_t from the data and update the offset pointed to by
475// "offset_ptr".
476//
477// RETURNS the uint64_t that was extracted, or zero on failure.
478uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const {
479 uint64_t val = 0;
480 const uint8_t *data =
481 static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
482 if (data) {
484 val = ReadSwapInt64(data);
485 } else {
486 memcpy(&val, data, 8);
487 }
488 }
489 return val;
490}
491
492// GetU64
493//
494// Get multiple consecutive 64 bit values. Return true if the entire read
495// succeeds and increment the offset pointed to by offset_ptr, else return
496// false and leave the offset pointed to by offset_ptr unchanged.
497void *DataExtractor::GetU64(offset_t *offset_ptr, void *void_dst,
498 uint32_t count) const {
499 const size_t src_size = sizeof(uint64_t) * count;
500 const uint64_t *src =
501 static_cast<const uint64_t *>(GetData(offset_ptr, src_size));
502 if (src) {
504 uint64_t *dst_pos = static_cast<uint64_t *>(void_dst);
505 uint64_t *dst_end = dst_pos + count;
506 const uint64_t *src_pos = src;
507 while (dst_pos < dst_end) {
508 *dst_pos = ReadSwapInt64(src_pos);
509 ++dst_pos;
510 ++src_pos;
511 }
512 } else {
513 memcpy(void_dst, src, src_size);
514 }
515 // Return a non-nullptr pointer to the converted data as an indicator of
516 // success
517 return void_dst;
518 }
519 return nullptr;
520}
521
523 size_t byte_size) const {
524 lldbassert(byte_size > 0 && byte_size <= 4 && "GetMaxU32 invalid byte_size!");
525 return GetMaxU64(offset_ptr, byte_size);
526}
527
529 size_t byte_size) const {
530 lldbassert(byte_size > 0 && byte_size <= 8 && "GetMaxU64 invalid byte_size!");
531 switch (byte_size) {
532 case 1:
533 return GetU8(offset_ptr);
534 case 2:
535 return GetU16(offset_ptr);
536 case 4:
537 return GetU32(offset_ptr);
538 case 8:
539 return GetU64(offset_ptr);
540 default: {
541 // General case.
542 const uint8_t *data =
543 static_cast<const uint8_t *>(GetData(offset_ptr, byte_size));
544 if (data == nullptr)
545 return 0;
546 return ReadMaxInt64(data, byte_size, m_byte_order);
547 }
548 }
549 return 0;
550}
551
553 size_t byte_size) const {
554 switch (byte_size) {
555 case 1:
556 return GetU8_unchecked(offset_ptr);
557 case 2:
558 return GetU16_unchecked(offset_ptr);
559 case 4:
560 return GetU32_unchecked(offset_ptr);
561 case 8:
562 return GetU64_unchecked(offset_ptr);
563 default: {
564 uint64_t res = ReadMaxInt64(&m_start[*offset_ptr], byte_size, m_byte_order);
565 *offset_ptr += byte_size;
566 return res;
567 }
568 }
569 return 0;
570}
571
572int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_t byte_size) const {
573 uint64_t u64 = GetMaxU64(offset_ptr, byte_size);
574 return llvm::SignExtend64(u64, 8 * byte_size);
575}
576
577uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size,
578 uint32_t bitfield_bit_size,
579 uint32_t bitfield_bit_offset) const {
580 assert(bitfield_bit_size <= 64);
581 uint64_t uval64 = GetMaxU64(offset_ptr, size);
582
583 if (bitfield_bit_size == 0)
584 return uval64;
585
586 int32_t lsbcount = bitfield_bit_offset;
588 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
589
590 if (lsbcount > 0)
591 uval64 >>= lsbcount;
592
593 uint64_t bitfield_mask =
594 (bitfield_bit_size == 64
595 ? std::numeric_limits<uint64_t>::max()
596 : ((static_cast<uint64_t>(1) << bitfield_bit_size) - 1));
597 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
598 return uval64;
599
600 uval64 &= bitfield_mask;
601
602 return uval64;
603}
604
605int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size,
606 uint32_t bitfield_bit_size,
607 uint32_t bitfield_bit_offset) const {
608 assert(size >= 1 && "GetMaxS64Bitfield size must be >= 1");
609 assert(size <= 8 && "GetMaxS64Bitfield size must be <= 8");
610 int64_t sval64 = GetMaxS64(offset_ptr, size);
611 if (bitfield_bit_size == 0)
612 return sval64;
613 int32_t lsbcount = bitfield_bit_offset;
615 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
616 if (lsbcount > 0)
617 sval64 >>= lsbcount;
618 uint64_t bitfield_mask = llvm::maskTrailingOnes<uint64_t>(bitfield_bit_size);
619 sval64 &= bitfield_mask;
620 // sign extend if needed
621 if (sval64 & ((static_cast<uint64_t>(1)) << (bitfield_bit_size - 1)))
622 sval64 |= ~bitfield_mask;
623 return sval64;
624}
625
626float DataExtractor::GetFloat(offset_t *offset_ptr) const {
627 return Get<float>(offset_ptr, 0.0f);
628}
629
630double DataExtractor::GetDouble(offset_t *offset_ptr) const {
631 return Get<double>(offset_ptr, 0.0);
632}
633
634long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const {
635 long double val = 0.0;
636#if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \
637 defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
638 *offset_ptr += CopyByteOrderedData(*offset_ptr, 10, &val, sizeof(val),
640#else
641 *offset_ptr += CopyByteOrderedData(*offset_ptr, sizeof(val), &val,
642 sizeof(val), endian::InlHostByteOrder());
643#endif
644 return val;
645}
646
647// Extract a single address from the data and update the offset pointed to by
648// "offset_ptr". The size of the extracted address comes from the
649// "this->m_addr_size" member variable and should be set correctly prior to
650// extracting any address values.
651//
652// RETURNS the address that was extracted, or zero on failure.
653uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const {
654 assert(m_addr_size >= 1 && m_addr_size <= 8);
655 return GetMaxU64(offset_ptr, m_addr_size);
656}
657
659 assert(m_addr_size >= 1 && m_addr_size <= 8);
660 return GetMaxU64_unchecked(offset_ptr, m_addr_size);
661}
662
664 ByteOrder dst_byte_order, void *dst) const {
665 const uint8_t *src = PeekData(offset, length);
666 if (src) {
667 if (dst_byte_order != GetByteOrder()) {
668 for (uint32_t i = 0; i < length; ++i)
669 (static_cast<uint8_t *>(dst))[i] = src[length - i - 1];
670 } else
671 ::memcpy(dst, src, length);
672 return length;
673 }
674 return 0;
675}
676
677// Extract data as it exists in target memory
679 void *dst) const {
680 const uint8_t *src = PeekData(offset, length);
681 if (src) {
682 ::memcpy(dst, src, length);
683 return length;
684 }
685 return 0;
686}
687
688// Extract data and swap if needed when doing the copy
691 void *dst_void_ptr, offset_t dst_len,
692 ByteOrder dst_byte_order) const {
693 // Validate the source info
694 if (!ValidOffsetForDataOfSize(src_offset, src_len))
695 assert(ValidOffsetForDataOfSize(src_offset, src_len));
696 assert(src_len > 0);
698
699 // Validate the destination info
700 assert(dst_void_ptr != nullptr);
701 assert(dst_len > 0);
702 assert(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
703
704 // Validate that only a word- or register-sized dst is byte swapped
705 assert(dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
706 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
707 dst_len == 32);
708
709 // Must have valid byte orders set in this object and for destination
710 if (!(dst_byte_order == eByteOrderBig ||
711 dst_byte_order == eByteOrderLittle) ||
713 return 0;
714
715 uint8_t *dst = static_cast<uint8_t *>(dst_void_ptr);
716 const uint8_t *src = PeekData(src_offset, src_len);
717 if (src) {
718 if (dst_len >= src_len) {
719 // We are copying the entire value from src into dst. Calculate how many,
720 // if any, zeroes we need for the most significant bytes if "dst_len" is
721 // greater than "src_len"...
722 const size_t num_zeroes = dst_len - src_len;
723 if (dst_byte_order == eByteOrderBig) {
724 // Big endian, so we lead with zeroes...
725 if (num_zeroes > 0)
726 ::memset(dst, 0, num_zeroes);
727 // Then either copy or swap the rest
729 ::memcpy(dst + num_zeroes, src, src_len);
730 } else {
731 for (uint32_t i = 0; i < src_len; ++i)
732 dst[i + num_zeroes] = src[src_len - 1 - i];
733 }
734 } else {
735 // Little endian destination, so we lead the value bytes
737 for (uint32_t i = 0; i < src_len; ++i)
738 dst[i] = src[src_len - 1 - i];
739 } else {
740 ::memcpy(dst, src, src_len);
741 }
742 // And zero the rest...
743 if (num_zeroes > 0)
744 ::memset(dst + src_len, 0, num_zeroes);
745 }
746 return src_len;
747 } else {
748 // We are only copying some of the value from src into dst..
749
750 if (dst_byte_order == eByteOrderBig) {
751 // Big endian dst
753 // Big endian dst, with big endian src
754 ::memcpy(dst, src + (src_len - dst_len), dst_len);
755 } else {
756 // Big endian dst, with little endian src
757 for (uint32_t i = 0; i < dst_len; ++i)
758 dst[i] = src[dst_len - 1 - i];
759 }
760 } else {
761 // Little endian dst
763 // Little endian dst, with big endian src
764 for (uint32_t i = 0; i < dst_len; ++i)
765 dst[i] = src[src_len - 1 - i];
766 } else {
767 // Little endian dst, with big endian src
768 ::memcpy(dst, src, dst_len);
769 }
770 }
771 return dst_len;
772 }
773 }
774 return 0;
775}
776
777// Extracts a variable length NULL terminated C string from the data at the
778// offset pointed to by "offset_ptr". The "offset_ptr" will be updated with
779// the offset of the byte that follows the NULL terminator byte.
780//
781// If the offset pointed to by "offset_ptr" is out of bounds, or if "length" is
782// non-zero and there aren't enough available bytes, nullptr will be returned
783// and "offset_ptr" will not be updated.
784const char *DataExtractor::GetCStr(offset_t *offset_ptr) const {
785 const char *start = reinterpret_cast<const char *>(PeekData(*offset_ptr, 1));
786 // Already at the end of the data.
787 if (!start)
788 return nullptr;
789
790 const char *end = reinterpret_cast<const char *>(m_end);
791
792 // Check all bytes for a null terminator that terminates a C string.
793 const char *terminator_or_end = std::find(start, end, '\0');
794
795 // We didn't find a null terminator, so return nullptr to indicate that there
796 // is no valid C string at that offset.
797 if (terminator_or_end == end)
798 return nullptr;
799
800 // Update offset_ptr for the caller to point to the data behind the
801 // terminator (which is 1 byte long).
802 *offset_ptr += (terminator_or_end - start + 1UL);
803 return start;
804}
805
806// Extracts a NULL terminated C string from the fixed length field of length
807// "len" at the offset pointed to by "offset_ptr". The "offset_ptr" will be
808// updated with the offset of the byte that follows the fixed length field.
809//
810// If the offset pointed to by "offset_ptr" is out of bounds, or if the offset
811// plus the length of the field is out of bounds, or if the field does not
812// contain a NULL terminator byte, nullptr will be returned and "offset_ptr"
813// will not be updated.
814const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const {
815 const char *cstr = reinterpret_cast<const char *>(PeekData(*offset_ptr, len));
816 if (cstr != nullptr) {
817 if (memchr(cstr, '\0', len) == nullptr) {
818 return nullptr;
819 }
820 *offset_ptr += len;
821 return cstr;
822 }
823 return nullptr;
824}
825
826// Peeks at a string in the contained data. No verification is done to make
827// sure the entire string lies within the bounds of this object's data, only
828// "offset" is verified to be a valid offset.
829//
830// Returns a valid C string pointer if "offset" is a valid offset in this
831// object's data, else nullptr is returned.
832const char *DataExtractor::PeekCStr(offset_t offset) const {
833 return reinterpret_cast<const char *>(PeekData(offset, 1));
834}
835
836// Extracts an unsigned LEB128 number from this object's data starting at the
837// offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
838// will be updated with the offset of the byte following the last extracted
839// byte.
840//
841// Returned the extracted integer value.
842uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const {
843 const uint8_t *src = PeekData(*offset_ptr, 1);
844 if (src == nullptr)
845 return 0;
846
847 unsigned byte_count = 0;
848 uint64_t result = llvm::decodeULEB128(src, &byte_count, m_end);
849 *offset_ptr += byte_count;
850 return result;
851}
852
853// Extracts an signed LEB128 number from this object's data starting at the
854// offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
855// will be updated with the offset of the byte following the last extracted
856// byte.
857//
858// Returned the extracted integer value.
859int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const {
860 const uint8_t *src = PeekData(*offset_ptr, 1);
861 if (src == nullptr)
862 return 0;
863
864 unsigned byte_count = 0;
865 int64_t result = llvm::decodeSLEB128(src, &byte_count, m_end);
866 *offset_ptr += byte_count;
867 return result;
868}
869
870// Skips a ULEB128 number (signed or unsigned) from this object's data starting
871// at the offset pointed to by "offset_ptr". The offset pointed to by
872// "offset_ptr" will be updated with the offset of the byte following the last
873// extracted byte.
874//
875// Returns the number of bytes consumed during the extraction.
876uint32_t DataExtractor::Skip_LEB128(offset_t *offset_ptr) const {
877 uint32_t bytes_consumed = 0;
878 const uint8_t *src = PeekData(*offset_ptr, 1);
879 if (src == nullptr)
880 return 0;
881
882 const uint8_t *end = m_end;
883
884 if (src < end) {
885 const uint8_t *src_pos = src;
886 while ((src_pos < end) && (*src_pos++ & 0x80))
887 ++bytes_consumed;
888 *offset_ptr += src_pos - src;
889 }
890 return bytes_consumed;
891}
892
893// Dumps bytes from this object's data to the stream "s" starting
894// "start_offset" bytes into this data, and ending with the byte before
895// "end_offset". "base_addr" will be added to the offset into the dumped data
896// when showing the offset into the data in the output information.
897// "num_per_line" objects of type "type" will be dumped with the option to
898// override the format for each object with "type_format". "type_format" is a
899// printf style formatting string. If "type_format" is nullptr, then an
900// appropriate format string will be used for the supplied "type". If the
901// stream "s" is nullptr, then the output will be send to Log().
903 offset_t length, uint64_t base_addr,
904 uint32_t num_per_line,
905 DataExtractor::Type type) const {
906 if (log == nullptr)
907 return start_offset;
908
909 offset_t offset;
910 offset_t end_offset;
911 uint32_t count;
912 StreamString sstr;
913 for (offset = start_offset, end_offset = offset + length, count = 0;
914 ValidOffset(offset) && offset < end_offset; ++count) {
915 if ((count % num_per_line) == 0) {
916 // Print out any previous string
917 if (sstr.GetSize() > 0) {
918 log->PutString(sstr.GetString());
919 sstr.Clear();
920 }
921 // Reset string offset and fill the current line string with address:
922 if (base_addr != LLDB_INVALID_ADDRESS)
923 sstr.Printf("0x%8.8" PRIx64 ":",
924 static_cast<uint64_t>(base_addr + (offset - start_offset)));
925 }
926
927 switch (type) {
928 case TypeUInt8:
929 sstr.Printf(" %2.2x", GetU8(&offset));
930 break;
931 case TypeChar: {
932 char ch = GetU8(&offset);
933 sstr.Printf(" %c", llvm::isPrint(ch) ? ch : ' ');
934 } break;
935 case TypeUInt16:
936 sstr.Printf(" %4.4x", GetU16(&offset));
937 break;
938 case TypeUInt32:
939 sstr.Printf(" %8.8x", GetU32(&offset));
940 break;
941 case TypeUInt64:
942 sstr.Printf(" %16.16" PRIx64, GetU64(&offset));
943 break;
944 case TypePointer:
945 sstr.Printf(" 0x%" PRIx64, GetAddress(&offset));
946 break;
947 case TypeULEB128:
948 sstr.Printf(" 0x%" PRIx64, GetULEB128(&offset));
949 break;
950 case TypeSLEB128:
951 sstr.Printf(" %" PRId64, GetSLEB128(&offset));
952 break;
953 }
954 }
955
956 if (!sstr.Empty())
957 log->PutString(sstr.GetString());
958
959 return offset; // Return the offset at which we ended up
960}
961
962size_t DataExtractor::Copy(DataExtractor &dest_data) const {
963 if (m_data_sp) {
964 // we can pass along the SP to the data
965 dest_data.SetData(m_data_sp);
966 } else {
967 const uint8_t *base_ptr = m_start;
968 size_t data_size = GetByteSize();
969 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
970 }
971 return GetByteSize();
972}
973
975 if (rhs.GetByteOrder() != GetByteOrder())
976 return false;
977
978 if (rhs.GetByteSize() == 0)
979 return true;
980
981 if (GetByteSize() == 0)
982 return (rhs.Copy(*this) > 0);
983
984 size_t bytes = GetByteSize() + rhs.GetByteSize();
985
986 DataBufferHeap *buffer_heap_ptr = nullptr;
987 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
988
989 if (!buffer_sp || buffer_heap_ptr == nullptr)
990 return false;
991
992 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
993
994 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
995 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
996
997 SetData(buffer_sp);
998
999 return true;
1000}
1001
1002bool DataExtractor::Append(void *buf, offset_t length) {
1003 if (buf == nullptr)
1004 return false;
1005
1006 if (length == 0)
1007 return true;
1008
1009 size_t bytes = GetByteSize() + length;
1010
1011 DataBufferHeap *buffer_heap_ptr = nullptr;
1012 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
1013
1014 if (!buffer_sp || buffer_heap_ptr == nullptr)
1015 return false;
1016
1017 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1018
1019 if (GetByteSize() > 0)
1020 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
1021
1022 memcpy(bytes_ptr + GetByteSize(), buf, length);
1023
1024 SetData(buffer_sp);
1025
1026 return true;
1027}
1028
1030 uint64_t max_data) {
1031 if (max_data == 0)
1032 max_data = GetByteSize();
1033 else
1034 max_data = std::min(max_data, GetByteSize());
1035
1036 llvm::MD5 md5;
1037
1038 const llvm::ArrayRef<uint8_t> data(GetDataStart(), max_data);
1039 md5.update(data);
1040
1041 llvm::MD5::MD5Result result;
1042 md5.final(result);
1043
1044 dest.clear();
1045 dest.append(result.begin(), result.end());
1046}
1047
1049 offset_t length) {
1050 DataExtractorSP new_sp = std::make_shared<DataExtractor>(
1052 new_sp->SetData(GetSharedDataBuffer(), GetSharedDataOffset() + offset,
1053 length);
1054 return new_sp;
1055}
1056
static uint64_t ReadSwapInt64(const unsigned char *ptr, offset_t offset)
static uint64_t ReadInt64(const unsigned char *ptr, offset_t offset=0)
static uint16_t ReadSwapInt16(const unsigned char *ptr, offset_t offset)
static uint16_t ReadInt16(const unsigned char *ptr, offset_t offset)
static uint32_t ReadInt32(const unsigned char *ptr, offset_t offset=0)
static uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size, ByteOrder byte_order)
static uint32_t ReadSwapInt32(const unsigned char *ptr, offset_t offset)
#define lldbassert(x)
Definition LLDBAssert.h:16
A subclass of DataBuffer that stores a data buffer on the heap.
A pure virtual protocol class for abstracted read only data buffers.
Definition DataBuffer.h:42
const uint8_t * GetBytes() const
Get a const pointer to the data.
Definition DataBuffer.h:57
uint64_t GetULEB128(lldb::offset_t *offset_ptr) const
Extract a unsigned LEB128 value from *offset_ptr.
size_t GetSharedDataOffset() const
Get the shared data offset.
float GetFloat(lldb::offset_t *offset_ptr) const
Extract a float from *offset_ptr.
virtual uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const
const char * GetCStr(lldb::offset_t *offset_ptr) const
Extract a C string from *offset_ptr.
size_t Copy(DataExtractor &dest_data) const
T Get(lldb::offset_t *offset_ptr, T fail_value) const
int64_t GetMaxS64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an signed integer of size byte_size from *offset_ptr.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
long double GetLongDouble(lldb::offset_t *offset_ptr) const
void Clear()
Clears the object state.
const uint8_t * m_start
A pointer to the first byte of data.
lldb::DataBufferSP m_data_sp
The shared pointer to data that can be shared among multiple instances.
uint32_t GetMaxU32(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an integer of size byte_size from *offset_ptr.
virtual const uint8_t * PeekData(lldb::offset_t offset, lldb::offset_t length) const
Peek at a bytes at offset.
virtual lldb::DataExtractorSP GetSubsetExtractorSP(lldb::offset_t offset, lldb::offset_t length)
Return a new DataExtractor which represents a subset of an existing data extractor's bytes,...
virtual uint64_t GetByteSize() const
Get the number of bytes contained in this object.
uint64_t GetAddress_unchecked(lldb::offset_t *offset_ptr) const
const DataExtractor & operator=(const DataExtractor &rhs)
Assignment operator.
lldb::offset_t CopyData(lldb::offset_t offset, lldb::offset_t length, void *dst) const
Copy length bytes from *offset, without swapping bytes.
uint64_t GetMaxU64_unchecked(lldb::offset_t *offset_ptr, size_t byte_size) const
uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const
Skip an LEB128 number at *offset_ptr.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
const uint8_t * m_end
A pointer to the byte that is past the end of the data.
DataExtractor()
Default constructor.
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
Type
Type enumerations used in the dump routines.
@ TypeUInt32
Format output as unsigned 32 bit integers.
@ TypeSLEB128
Format output as SLEB128 numbers.
@ TypeUInt8
Format output as unsigned 8 bit integers.
@ TypeUInt64
Format output as unsigned 64 bit integers.
@ TypeULEB128
Format output as ULEB128 numbers.
@ TypePointer
Format output as pointers.
@ TypeUInt16
Format output as unsigned 16 bit integers.
@ TypeChar
Format output as characters.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
uint64_t GetMaxU64Bitfield(lldb::offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
Extract an unsigned integer of size byte_size from *offset_ptr, then extract the bitfield from this v...
lldb::ByteOrder m_byte_order
The byte order of the data we are extracting from.
void Checksum(llvm::SmallVectorImpl< uint8_t > &dest, uint64_t max_data=0)
bool Append(DataExtractor &rhs)
const uint8_t * GetDataStart() const
Get the data start pointer.
bool ValidOffset(lldb::offset_t offset) const
Test the validity of offset.
virtual lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
uint32_t GetAddressByteSize() const
Get the current address size.
uint32_t m_addr_size
The address size to use when extracting addresses.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
virtual uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const
int64_t GetSLEB128(lldb::offset_t *offset_ptr) const
Extract a signed LEB128 value from *offset_ptr.
virtual uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const
int64_t GetMaxS64Bitfield(lldb::offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
Extract an signed integer of size size from *offset_ptr, then extract and sign-extend the bitfield fr...
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
virtual uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const
lldb::offset_t PutToLog(Log *log, lldb::offset_t offset, lldb::offset_t length, uint64_t base_addr, uint32_t num_per_line, Type type) const
Dumps the binary data as type objects to stream s (or to Log() if s is nullptr) starting offset bytes...
lldb::offset_t CopyByteOrderedData(lldb::offset_t src_offset, lldb::offset_t src_len, void *dst, lldb::offset_t dst_len, lldb::ByteOrder dst_byte_order) const
Copy dst_len bytes from *offset_ptr and ensure the copied data is treated as a value that can be swap...
lldb::DataBufferSP GetSharedDataBuffer() const
double GetDouble(lldb::offset_t *offset_ptr) const
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
const char * PeekCStr(lldb::offset_t offset) const
Peek at a C string at offset.
virtual ~DataExtractor()
Destructor.
size_t ExtractBytes(lldb::offset_t offset, lldb::offset_t length, lldb::ByteOrder dst_byte_order, void *dst) const
Extract an arbitrary number of bytes in the specified byte order.
virtual llvm::ArrayRef< uint8_t > GetData() const
void PutString(llvm::StringRef str)
Definition Log.cpp:147
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define LLDB_INVALID_ADDRESS
lldb::ByteOrder InlHostByteOrder()
Definition Endian.h:25
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:85
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP