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
70/// Read a byte-swapped \c T from \a ptr + \a offset, which need not be aligned
71/// for \c T.
72template <typename T>
73static inline T ReadSwap(const uint8_t *ptr, offset_t offset = 0) {
74 T value;
75 memcpy(&value, ptr + offset, sizeof(T));
76 return llvm::byteswap<T>(value);
77}
78
79static inline uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size,
80 ByteOrder byte_order) {
81 uint64_t res = 0;
82 if (byte_order == eByteOrderBig)
83 for (size_t i = 0; i < byte_size; ++i)
84 res = (res << 8) | data[i];
85 else {
86 assert(byte_order == eByteOrderLittle);
87 for (size_t i = 0; i < byte_size; ++i)
88 res = (res << 8) | data[byte_size - 1 - i];
89 }
90 return res;
91}
92
93/// Implements DataExtractor::GetU16/GetU32/GetU64(offset_ptr, dst, count); see
94/// DataExtractor.h for the contract.
95///
96/// \a data's buffer and \a dst are only byte-aligned, so every value is
97/// transferred with memcpy: loading or storing a \c T through a pointer that is
98/// not sizeof(T)-aligned is undefined behavior, and trips UBSan.
99template <typename T>
100static void *GetUInt(const DataExtractor &data, offset_t *offset_ptr, void *dst,
101 uint32_t count) {
102 const size_t src_size = sizeof(T) * count;
103 const uint8_t *src =
104 static_cast<const uint8_t *>(data.GetData(offset_ptr, src_size));
105 if (!src)
106 return nullptr;
107
108 if (data.GetByteOrder() == endian::InlHostByteOrder()) {
109 memcpy(dst, src, src_size);
110 } else {
111 uint8_t *dst_bytes = static_cast<uint8_t *>(dst);
112 for (uint32_t i = 0; i < count; ++i) {
113 T value = ReadSwap<T>(src, i * sizeof(T));
114 memcpy(dst_bytes + i * sizeof(T), &value, sizeof(T));
115 }
116 }
117 return dst;
118}
119
121 : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
122 m_data_sp() {}
123
124// This constructor allows us to use data that is owned by someone else. The
125// data must stay around as long as this object is valid.
126DataExtractor::DataExtractor(const void *data, offset_t length,
127 ByteOrder endian, uint32_t addr_size)
128 : m_start(const_cast<uint8_t *>(static_cast<const uint8_t *>(data))),
129 m_end(const_cast<uint8_t *>(static_cast<const uint8_t *>(data)) + length),
130 m_byte_order(endian), m_addr_size(addr_size), m_data_sp() {
131 assert(addr_size >= 1 && addr_size <= 8);
132}
133
134// Make a shared pointer reference to the shared data in "data_sp" and set the
135// endian swapping setting to "swap", and the address size to "addr_size". The
136// shared data reference will ensure the data lives as long as any
137// DataExtractor objects exist that have a reference to this data.
139 uint32_t addr_size)
140 : m_byte_order(endian), m_addr_size(addr_size), m_data_sp() {
141 assert(addr_size >= 1 && addr_size <= 8);
142 SetData(data_sp);
143}
144
145// Make a shared pointer reference to the shared data in "data_sp".
147 : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
148 m_data_sp(data_sp) {
149 if (data_sp)
150 SetData(data_sp);
151}
152
153// Initialize this object with a subset of the data bytes in "data". If "data"
154// contains shared data, then a reference to this shared data will added and
155// the shared data will stay around as long as any object contains a reference
156// to that data. The endian swap and address size settings are copied from
157// "data".
159 offset_t length)
161 m_data_sp() {
162 assert(m_addr_size >= 1 && m_addr_size <= 8);
163 if (data.ValidOffset(offset)) {
164 offset_t bytes_available = data.GetByteSize() - offset;
165 if (length > bytes_available)
166 length = bytes_available;
167 SetData(data, offset, length);
168 }
169}
170
176
177// Assignment operator
179 if (this != &rhs) {
180 m_start = rhs.m_start;
181 m_end = rhs.m_end;
184 m_data_sp = rhs.m_data_sp;
185 }
186 return *this;
187}
188
190
191// Clears the object contents back to a default invalid state, and release any
192// references to shared data that this object may contain.
194 m_start = nullptr;
195 m_end = nullptr;
197 m_addr_size = sizeof(void *);
198 m_data_sp.reset();
199}
200
201// If this object contains shared data, this function returns the offset into
202// that shared data. Else zero is returned.
204 if (m_start != nullptr) {
205 const DataBuffer *data = m_data_sp.get();
206 if (data != nullptr) {
207 const uint8_t *data_bytes = data->GetBytes();
208 if (data_bytes != nullptr) {
209 assert(m_start >= data_bytes);
210 return m_start - data_bytes;
211 }
212 }
213 }
214 return 0;
215}
216
217// Set the data with which this object will extract from to data starting at
218// BYTES and set the length of the data to LENGTH bytes long. The data is
219// externally owned must be around at least as long as this object points to
220// the data. No copy of the data is made, this object just refers to this data
221// and can extract from it. If this object refers to any shared data upon
222// entry, the reference to that data will be released. Is SWAP is set to true,
223// any data extracted will be endian swapped.
227 m_data_sp.reset();
228 if (bytes == nullptr || length == 0) {
229 m_start = nullptr;
230 m_end = nullptr;
231 } else {
232 m_start = const_cast<uint8_t *>(static_cast<const uint8_t *>(bytes));
233 m_end = m_start + length;
234 }
235 return GetByteSize();
236}
237
238// Assign the data for this object to be a subrange in "data" starting
239// "data_offset" bytes into "data" and ending "data_length" bytes later. If
240// "data_offset" is not a valid offset into "data", then this object will
241// contain no bytes. If "data_offset" is within "data" yet "data_length" is too
242// large, the length will be capped at the number of bytes remaining in "data".
243// If "data" contains a shared pointer to other data, then a ref counted
244// pointer to that data will be made in this object. If "data" doesn't contain
245// a shared pointer to data, then the bytes referred to in "data" will need to
246// exist at least as long as this object refers to those bytes. The address
247// size and endian swap settings are copied from the current values in "data".
249 offset_t data_offset,
250 offset_t data_length) {
252 assert(m_addr_size >= 1 && m_addr_size <= 8);
253 // If "data" contains shared pointer to data, then we can use that
254 if (data.m_data_sp) {
256 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset,
257 data_length);
258 }
259
260 // We have a DataExtractor object that just has a pointer to bytes
261 if (data.ValidOffset(data_offset)) {
262 if (data_length > data.GetByteSize() - data_offset)
263 data_length = data.GetByteSize() - data_offset;
264 return SetData(data.GetDataStart() + data_offset, data_length,
265 data.GetByteOrder());
266 }
267 return 0;
268}
269
270// Assign the data for this object to be a subrange of the shared data in
271// "data_sp" starting "data_offset" bytes into "data_sp" and ending
272// "data_length" bytes later. If "data_offset" is not a valid offset into
273// "data_sp", then this object will contain no bytes. If "data_offset" is
274// within "data_sp" yet "data_length" is too large, the length will be capped
275// at the number of bytes remaining in "data_sp". A ref counted pointer to the
276// data in "data_sp" will be made in this object IF the number of bytes this
277// object refers to in greater than zero (if at least one byte was available
278// starting at "data_offset") to ensure the data stays around as long as it is
279// needed. The address size and endian swap settings will remain unchanged from
280// their current settings.
282 offset_t data_offset,
283 offset_t data_length) {
284 m_start = m_end = nullptr;
285
286 if (data_length > 0) {
287 m_data_sp = data_sp;
288 if (data_sp) {
289 const size_t data_size = data_sp->GetByteSize();
290 if (data_offset < data_size) {
291 m_start = data_sp->GetBytes() + data_offset;
292 const size_t bytes_left = data_size - data_offset;
293 // Cap the length of we asked for too many
294 if (data_length <= bytes_left)
295 m_end = m_start + data_length; // We got all the bytes we wanted
296 else
297 m_end = m_start + bytes_left; // Not all the bytes requested were
298 // available in the shared data
299 }
300 }
301 }
302
303 size_t new_size = GetByteSize();
304
305 // Don't hold a shared pointer to the data buffer if we don't share any valid
306 // bytes in the shared buffer.
307 if (new_size == 0)
308 m_data_sp.reset();
309
310 return new_size;
311}
312
313// Extract a single unsigned char from the binary data and update the offset
314// pointed to by "offset_ptr".
315//
316// RETURNS the byte that was extracted, or zero on failure.
317uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const {
318 const uint8_t *data = static_cast<const uint8_t *>(GetData(offset_ptr, 1));
319 if (data)
320 return *data;
321 return 0;
322}
323
324// Extract "count" unsigned chars from the binary data and update the offset
325// pointed to by "offset_ptr". The extracted data is copied into "dst".
326//
327// RETURNS the non-nullptr buffer pointer upon successful extraction of
328// all the requested bytes, or nullptr when the data is not available in the
329// buffer due to being out of bounds, or insufficient data.
330void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst,
331 uint32_t count) const {
332 const uint8_t *data =
333 static_cast<const uint8_t *>(GetData(offset_ptr, count));
334 if (data) {
335 // Copy the data into the buffer
336 memcpy(dst, data, count);
337 // Return a non-nullptr pointer to the converted data as an indicator of
338 // success
339 return dst;
340 }
341 return nullptr;
342}
343
344// Extract a single uint16_t from the data and update the offset pointed to by
345// "offset_ptr".
346//
347// RETURNS the uint16_t that was extracted, or zero on failure.
348uint16_t DataExtractor::GetU16(offset_t *offset_ptr) const {
349 uint16_t val = 0;
350 const uint8_t *data =
351 static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
352 if (data) {
354 val = ReadSwap<uint16_t>(data);
355 else
356 val = ReadInt16(data);
357 }
358 return val;
359}
360
361uint16_t DataExtractor::GetU16_unchecked(offset_t *offset_ptr) const {
362 uint16_t val;
364 val = ReadInt16(m_start, *offset_ptr);
365 else
366 val = ReadSwap<uint16_t>(m_start, *offset_ptr);
367 *offset_ptr += sizeof(val);
368 return val;
369}
370
371uint32_t DataExtractor::GetU32_unchecked(offset_t *offset_ptr) const {
372 uint32_t val;
374 val = ReadInt32(m_start, *offset_ptr);
375 else
376 val = ReadSwap<uint32_t>(m_start, *offset_ptr);
377 *offset_ptr += sizeof(val);
378 return val;
379}
380
381uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const {
382 uint64_t val;
384 val = ReadInt64(m_start, *offset_ptr);
385 else
386 val = ReadSwap<uint64_t>(m_start, *offset_ptr);
387 *offset_ptr += sizeof(val);
388 return val;
389}
390
391void *DataExtractor::GetU16(offset_t *offset_ptr, void *dst,
392 uint32_t count) const {
393 return GetUInt<uint16_t>(*this, offset_ptr, dst, count);
394}
395
396// Extract a single uint32_t from the data and update the offset pointed to by
397// "offset_ptr".
398//
399// RETURNS the uint32_t that was extracted, or zero on failure.
400uint32_t DataExtractor::GetU32(offset_t *offset_ptr) const {
401 uint32_t val = 0;
402 const uint8_t *data =
403 static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
404 if (data) {
406 val = ReadSwap<uint32_t>(data);
407 } else {
408 memcpy(&val, data, 4);
409 }
410 }
411 return val;
412}
413
414void *DataExtractor::GetU32(offset_t *offset_ptr, void *dst,
415 uint32_t count) const {
416 return GetUInt<uint32_t>(*this, offset_ptr, dst, count);
417}
418
419// Extract a single uint64_t from the data and update the offset pointed to by
420// "offset_ptr".
421//
422// RETURNS the uint64_t that was extracted, or zero on failure.
423uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const {
424 uint64_t val = 0;
425 const uint8_t *data =
426 static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val)));
427 if (data) {
429 val = ReadSwap<uint64_t>(data);
430 } else {
431 memcpy(&val, data, 8);
432 }
433 }
434 return val;
435}
436
437void *DataExtractor::GetU64(offset_t *offset_ptr, void *dst,
438 uint32_t count) const {
439 return GetUInt<uint64_t>(*this, offset_ptr, dst, count);
440}
441
443 size_t byte_size) const {
444 lldbassert(byte_size > 0 && byte_size <= 4 && "GetMaxU32 invalid byte_size!");
445 return GetMaxU64(offset_ptr, byte_size);
446}
447
449 size_t byte_size) const {
450 lldbassert(byte_size > 0 && byte_size <= 8 && "GetMaxU64 invalid byte_size!");
451 switch (byte_size) {
452 case 1:
453 return GetU8(offset_ptr);
454 case 2:
455 return GetU16(offset_ptr);
456 case 4:
457 return GetU32(offset_ptr);
458 case 8:
459 return GetU64(offset_ptr);
460 default: {
461 // General case.
462 const uint8_t *data =
463 static_cast<const uint8_t *>(GetData(offset_ptr, byte_size));
464 if (data == nullptr)
465 return 0;
466 return ReadMaxInt64(data, byte_size, m_byte_order);
467 }
468 }
469 return 0;
470}
471
473 size_t byte_size) const {
474 switch (byte_size) {
475 case 1:
476 return GetU8_unchecked(offset_ptr);
477 case 2:
478 return GetU16_unchecked(offset_ptr);
479 case 4:
480 return GetU32_unchecked(offset_ptr);
481 case 8:
482 return GetU64_unchecked(offset_ptr);
483 default: {
484 uint64_t res = ReadMaxInt64(&m_start[*offset_ptr], byte_size, m_byte_order);
485 *offset_ptr += byte_size;
486 return res;
487 }
488 }
489 return 0;
490}
491
492int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_t byte_size) const {
493 uint64_t u64 = GetMaxU64(offset_ptr, byte_size);
494 return llvm::SignExtend64(u64, 8 * byte_size);
495}
496
497uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size,
498 uint32_t bitfield_bit_size,
499 uint32_t bitfield_bit_offset) const {
500 assert(bitfield_bit_size <= 64);
501 uint64_t uval64 = GetMaxU64(offset_ptr, size);
502
503 if (bitfield_bit_size == 0)
504 return uval64;
505
506 int32_t lsbcount = bitfield_bit_offset;
508 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
509
510 if (lsbcount > 0)
511 uval64 >>= lsbcount;
512
513 uint64_t bitfield_mask =
514 (bitfield_bit_size == 64
515 ? std::numeric_limits<uint64_t>::max()
516 : ((static_cast<uint64_t>(1) << bitfield_bit_size) - 1));
517 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
518 return uval64;
519
520 uval64 &= bitfield_mask;
521
522 return uval64;
523}
524
525int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size,
526 uint32_t bitfield_bit_size,
527 uint32_t bitfield_bit_offset) const {
528 assert(size >= 1 && "GetMaxS64Bitfield size must be >= 1");
529 assert(size <= 8 && "GetMaxS64Bitfield size must be <= 8");
530 int64_t sval64 = GetMaxS64(offset_ptr, size);
531 if (bitfield_bit_size == 0)
532 return sval64;
533 int32_t lsbcount = bitfield_bit_offset;
535 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
536 if (lsbcount > 0)
537 sval64 >>= lsbcount;
538 uint64_t bitfield_mask = llvm::maskTrailingOnes<uint64_t>(bitfield_bit_size);
539 sval64 &= bitfield_mask;
540 // sign extend if needed
541 if (sval64 & ((static_cast<uint64_t>(1)) << (bitfield_bit_size - 1)))
542 sval64 |= ~bitfield_mask;
543 return sval64;
544}
545
546float DataExtractor::GetFloat(offset_t *offset_ptr) const {
547 return Get<float>(offset_ptr, 0.0f);
548}
549
550double DataExtractor::GetDouble(offset_t *offset_ptr) const {
551 return Get<double>(offset_ptr, 0.0);
552}
553
554long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const {
555 long double val = 0.0;
556#if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \
557 defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
558 *offset_ptr += CopyByteOrderedData(*offset_ptr, 10, &val, sizeof(val),
560#else
561 *offset_ptr += CopyByteOrderedData(*offset_ptr, sizeof(val), &val,
562 sizeof(val), endian::InlHostByteOrder());
563#endif
564 return val;
565}
566
567// Extract a single address from the data and update the offset pointed to by
568// "offset_ptr". The size of the extracted address comes from the
569// "this->m_addr_size" member variable and should be set correctly prior to
570// extracting any address values.
571//
572// RETURNS the address that was extracted, or zero on failure.
573uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const {
574 assert(m_addr_size >= 1 && m_addr_size <= 8);
575 return GetMaxU64(offset_ptr, m_addr_size);
576}
577
579 assert(m_addr_size >= 1 && m_addr_size <= 8);
580 return GetMaxU64_unchecked(offset_ptr, m_addr_size);
581}
582
584 ByteOrder dst_byte_order, void *dst) const {
585 const uint8_t *src = PeekData(offset, length);
586 if (src) {
587 if (dst_byte_order != GetByteOrder()) {
588 for (uint32_t i = 0; i < length; ++i)
589 (static_cast<uint8_t *>(dst))[i] = src[length - i - 1];
590 } else
591 ::memcpy(dst, src, length);
592 return length;
593 }
594 return 0;
595}
596
597// Extract data as it exists in target memory
599 void *dst) const {
600 const uint8_t *src = PeekData(offset, length);
601 if (src) {
602 ::memcpy(dst, src, length);
603 return length;
604 }
605 return 0;
606}
607
608// Extract data and swap if needed when doing the copy
611 void *dst_void_ptr, offset_t dst_len,
612 ByteOrder dst_byte_order) const {
613 // Validate the source info
614 if (!ValidOffsetForDataOfSize(src_offset, src_len))
615 assert(ValidOffsetForDataOfSize(src_offset, src_len));
616 assert(src_len > 0);
618
619 // Validate the destination info
620 assert(dst_void_ptr != nullptr);
621 assert(dst_len > 0);
622 assert(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
623
624 // Validate that only a word- or register-sized dst is byte swapped
625 assert(dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
626 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
627 dst_len == 32);
628
629 // Must have valid byte orders set in this object and for destination
630 if (!(dst_byte_order == eByteOrderBig ||
631 dst_byte_order == eByteOrderLittle) ||
633 return 0;
634
635 uint8_t *dst = static_cast<uint8_t *>(dst_void_ptr);
636 const uint8_t *src = PeekData(src_offset, src_len);
637 if (src) {
638 if (dst_len >= src_len) {
639 // We are copying the entire value from src into dst. Calculate how many,
640 // if any, zeroes we need for the most significant bytes if "dst_len" is
641 // greater than "src_len"...
642 const size_t num_zeroes = dst_len - src_len;
643 if (dst_byte_order == eByteOrderBig) {
644 // Big endian, so we lead with zeroes...
645 if (num_zeroes > 0)
646 ::memset(dst, 0, num_zeroes);
647 // Then either copy or swap the rest
649 ::memcpy(dst + num_zeroes, src, src_len);
650 } else {
651 for (uint32_t i = 0; i < src_len; ++i)
652 dst[i + num_zeroes] = src[src_len - 1 - i];
653 }
654 } else {
655 // Little endian destination, so we lead the value bytes
657 for (uint32_t i = 0; i < src_len; ++i)
658 dst[i] = src[src_len - 1 - i];
659 } else {
660 ::memcpy(dst, src, src_len);
661 }
662 // And zero the rest...
663 if (num_zeroes > 0)
664 ::memset(dst + src_len, 0, num_zeroes);
665 }
666 return src_len;
667 } else {
668 // We are only copying some of the value from src into dst..
669
670 if (dst_byte_order == eByteOrderBig) {
671 // Big endian dst
673 // Big endian dst, with big endian src
674 ::memcpy(dst, src + (src_len - dst_len), dst_len);
675 } else {
676 // Big endian dst, with little endian src
677 for (uint32_t i = 0; i < dst_len; ++i)
678 dst[i] = src[dst_len - 1 - i];
679 }
680 } else {
681 // Little endian dst
683 // Little endian dst, with big endian src
684 for (uint32_t i = 0; i < dst_len; ++i)
685 dst[i] = src[src_len - 1 - i];
686 } else {
687 // Little endian dst, with big endian src
688 ::memcpy(dst, src, dst_len);
689 }
690 }
691 return dst_len;
692 }
693 }
694 return 0;
695}
696
697// Extracts a variable length NULL terminated C string from the data at the
698// offset pointed to by "offset_ptr". The "offset_ptr" will be updated with
699// the offset of the byte that follows the NULL terminator byte.
700//
701// If the offset pointed to by "offset_ptr" is out of bounds, or if "length" is
702// non-zero and there aren't enough available bytes, nullptr will be returned
703// and "offset_ptr" will not be updated.
704const char *DataExtractor::GetCStr(offset_t *offset_ptr) const {
705 const char *start = reinterpret_cast<const char *>(PeekData(*offset_ptr, 1));
706 // Already at the end of the data.
707 if (!start)
708 return nullptr;
709
710 const char *end = reinterpret_cast<const char *>(m_end);
711
712 // Check all bytes for a null terminator that terminates a C string.
713 const char *terminator_or_end = std::find(start, end, '\0');
714
715 // We didn't find a null terminator, so return nullptr to indicate that there
716 // is no valid C string at that offset.
717 if (terminator_or_end == end)
718 return nullptr;
719
720 // Update offset_ptr for the caller to point to the data behind the
721 // terminator (which is 1 byte long).
722 *offset_ptr += (terminator_or_end - start + 1UL);
723 return start;
724}
725
726// Extracts a NULL terminated C string from the fixed length field of length
727// "len" at the offset pointed to by "offset_ptr". The "offset_ptr" will be
728// updated with the offset of the byte that follows the fixed length field.
729//
730// If the offset pointed to by "offset_ptr" is out of bounds, or if the offset
731// plus the length of the field is out of bounds, or if the field does not
732// contain a NULL terminator byte, nullptr will be returned and "offset_ptr"
733// will not be updated.
734const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const {
735 const char *cstr = reinterpret_cast<const char *>(PeekData(*offset_ptr, len));
736 if (cstr != nullptr) {
737 if (memchr(cstr, '\0', len) == nullptr) {
738 return nullptr;
739 }
740 *offset_ptr += len;
741 return cstr;
742 }
743 return nullptr;
744}
745
746// Peeks at a string in the contained data. No verification is done to make
747// sure the entire string lies within the bounds of this object's data, only
748// "offset" is verified to be a valid offset.
749//
750// Returns a valid C string pointer if "offset" is a valid offset in this
751// object's data, else nullptr is returned.
752const char *DataExtractor::PeekCStr(offset_t offset) const {
753 return reinterpret_cast<const char *>(PeekData(offset, 1));
754}
755
756// Extracts an unsigned LEB128 number from this object's data starting at the
757// offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
758// will be updated with the offset of the byte following the last extracted
759// byte.
760//
761// Returned the extracted integer value.
762uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const {
763 const uint8_t *src = PeekData(*offset_ptr, 1);
764 if (src == nullptr)
765 return 0;
766
767 unsigned byte_count = 0;
768 uint64_t result = llvm::decodeULEB128(src, &byte_count, m_end);
769 *offset_ptr += byte_count;
770 return result;
771}
772
773// Extracts an signed LEB128 number from this object's data starting at the
774// offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
775// will be updated with the offset of the byte following the last extracted
776// byte.
777//
778// Returned the extracted integer value.
779int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const {
780 const uint8_t *src = PeekData(*offset_ptr, 1);
781 if (src == nullptr)
782 return 0;
783
784 unsigned byte_count = 0;
785 int64_t result = llvm::decodeSLEB128(src, &byte_count, m_end);
786 *offset_ptr += byte_count;
787 return result;
788}
789
790// Skips a ULEB128 number (signed or unsigned) from this object's data starting
791// at the offset pointed to by "offset_ptr". The offset pointed to by
792// "offset_ptr" will be updated with the offset of the byte following the last
793// extracted byte.
794//
795// Returns the number of bytes consumed during the extraction.
796uint32_t DataExtractor::Skip_LEB128(offset_t *offset_ptr) const {
797 uint32_t bytes_consumed = 0;
798 const uint8_t *src = PeekData(*offset_ptr, 1);
799 if (src == nullptr)
800 return 0;
801
802 const uint8_t *end = m_end;
803
804 if (src < end) {
805 const uint8_t *src_pos = src;
806 while ((src_pos < end) && (*src_pos++ & 0x80))
807 ++bytes_consumed;
808 *offset_ptr += src_pos - src;
809 }
810 return bytes_consumed;
811}
812
813// Dumps bytes from this object's data to the stream "s" starting
814// "start_offset" bytes into this data, and ending with the byte before
815// "end_offset". "base_addr" will be added to the offset into the dumped data
816// when showing the offset into the data in the output information.
817// "num_per_line" objects of type "type" will be dumped with the option to
818// override the format for each object with "type_format". "type_format" is a
819// printf style formatting string. If "type_format" is nullptr, then an
820// appropriate format string will be used for the supplied "type". If the
821// stream "s" is nullptr, then the output will be send to Log().
823 offset_t length, uint64_t base_addr,
824 uint32_t num_per_line,
825 DataExtractor::Type type) const {
826 if (log == nullptr)
827 return start_offset;
828
829 offset_t offset;
830 offset_t end_offset;
831 uint32_t count;
832 StreamString sstr;
833 for (offset = start_offset, end_offset = offset + length, count = 0;
834 ValidOffset(offset) && offset < end_offset; ++count) {
835 if ((count % num_per_line) == 0) {
836 // Print out any previous string
837 if (sstr.GetSize() > 0) {
838 log->PutString(sstr.GetString());
839 sstr.Clear();
840 }
841 // Reset string offset and fill the current line string with address:
842 if (base_addr != LLDB_INVALID_ADDRESS)
843 sstr.Printf("0x%8.8" PRIx64 ":",
844 static_cast<uint64_t>(base_addr + (offset - start_offset)));
845 }
846
847 switch (type) {
848 case TypeUInt8:
849 sstr.Printf(" %2.2x", GetU8(&offset));
850 break;
851 case TypeChar: {
852 char ch = GetU8(&offset);
853 sstr.Printf(" %c", llvm::isPrint(ch) ? ch : ' ');
854 } break;
855 case TypeUInt16:
856 sstr.Printf(" %4.4x", GetU16(&offset));
857 break;
858 case TypeUInt32:
859 sstr.Printf(" %8.8x", GetU32(&offset));
860 break;
861 case TypeUInt64:
862 sstr.Printf(" %16.16" PRIx64, GetU64(&offset));
863 break;
864 case TypePointer:
865 sstr.Printf(" 0x%" PRIx64, GetAddress(&offset));
866 break;
867 case TypeULEB128:
868 sstr.Printf(" 0x%" PRIx64, GetULEB128(&offset));
869 break;
870 case TypeSLEB128:
871 sstr.Printf(" %" PRId64, GetSLEB128(&offset));
872 break;
873 }
874 }
875
876 if (!sstr.Empty())
877 log->PutString(sstr.GetString());
878
879 return offset; // Return the offset at which we ended up
880}
881
882size_t DataExtractor::Copy(DataExtractor &dest_data) const {
883 if (m_data_sp) {
884 // we can pass along the SP to the data
885 dest_data.SetData(m_data_sp);
886 } else {
887 const uint8_t *base_ptr = m_start;
888 size_t data_size = GetByteSize();
889 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
890 }
891 return GetByteSize();
892}
893
895 if (rhs.GetByteOrder() != GetByteOrder())
896 return false;
897
898 if (rhs.GetByteSize() == 0)
899 return true;
900
901 if (GetByteSize() == 0)
902 return (rhs.Copy(*this) > 0);
903
904 size_t bytes = GetByteSize() + rhs.GetByteSize();
905
906 DataBufferHeap *buffer_heap_ptr = nullptr;
907 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
908
909 if (!buffer_sp || buffer_heap_ptr == nullptr)
910 return false;
911
912 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
913
914 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
915 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
916
917 SetData(buffer_sp);
918
919 return true;
920}
921
922bool DataExtractor::Append(void *buf, offset_t length) {
923 if (buf == nullptr)
924 return false;
925
926 if (length == 0)
927 return true;
928
929 size_t bytes = GetByteSize() + length;
930
931 DataBufferHeap *buffer_heap_ptr = nullptr;
932 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
933
934 if (!buffer_sp || buffer_heap_ptr == nullptr)
935 return false;
936
937 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
938
939 if (GetByteSize() > 0)
940 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
941
942 memcpy(bytes_ptr + GetByteSize(), buf, length);
943
944 SetData(buffer_sp);
945
946 return true;
947}
948
950 uint64_t max_data) {
951 if (max_data == 0)
952 max_data = GetByteSize();
953 else
954 max_data = std::min(max_data, GetByteSize());
955
956 llvm::MD5 md5;
957
958 const llvm::ArrayRef<uint8_t> data(GetDataStart(), max_data);
959 md5.update(data);
960
961 llvm::MD5::MD5Result result;
962 md5.final(result);
963
964 dest.clear();
965 dest.append(result.begin(), result.end());
966}
967
969 offset_t length) {
970 DataExtractorSP new_sp = std::make_shared<DataExtractor>(
972 new_sp->SetData(GetSharedDataBuffer(), GetSharedDataOffset() + offset,
973 length);
974 return new_sp;
975}
976
static T ReadSwap(const uint8_t *ptr, offset_t offset=0)
Read a byte-swapped T from ptr + offset, which need not be aligned for T.
static void * GetUInt(const DataExtractor &data, offset_t *offset_ptr, void *dst, uint32_t count)
Implements DataExtractor::GetU16/GetU32/GetU64(offset_ptr, dst, count); see DataExtractor....
static uint64_t ReadInt64(const unsigned char *ptr, offset_t offset=0)
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)
#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
An data extractor class.
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.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes 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:164
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:86
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP