LLDB  mainline
DataExtractor.cpp
Go to the documentation of this file.
1 //===-- DataExtractor.cpp ---------------------------------------*- C++ -*-===//
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"
12 #include "lldb/lldb-enumerations.h"
13 #include "lldb/lldb-forward.h"
14 #include "lldb/lldb-types.h"
15 
18 #include "lldb/Utility/Endian.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/Stream.h"
23 #include "lldb/Utility/UUID.h"
24 
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/MD5.h"
28 #include "llvm/Support/MathExtras.h"
29 
30 #include <algorithm>
31 #include <array>
32 #include <cassert>
33 #include <cstdint>
34 #include <string>
35 
36 #include <ctype.h>
37 #include <inttypes.h>
38 #include <string.h>
39 
40 using namespace lldb;
41 using namespace lldb_private;
42 
43 static inline uint16_t ReadInt16(const unsigned char *ptr, offset_t offset) {
44  uint16_t value;
45  memcpy(&value, ptr + offset, 2);
46  return value;
47 }
48 
49 static inline uint32_t ReadInt32(const unsigned char *ptr,
50  offset_t offset = 0) {
51  uint32_t value;
52  memcpy(&value, ptr + offset, 4);
53  return value;
54 }
55 
56 static inline uint64_t ReadInt64(const unsigned char *ptr,
57  offset_t offset = 0) {
58  uint64_t value;
59  memcpy(&value, ptr + offset, 8);
60  return value;
61 }
62 
63 static inline uint16_t ReadInt16(const void *ptr) {
64  uint16_t value;
65  memcpy(&value, ptr, 2);
66  return value;
67 }
68 
69 static inline uint16_t ReadSwapInt16(const unsigned char *ptr,
70  offset_t offset) {
71  uint16_t value;
72  memcpy(&value, ptr + offset, 2);
73  return llvm::ByteSwap_16(value);
74 }
75 
76 static inline uint32_t ReadSwapInt32(const unsigned char *ptr,
77  offset_t offset) {
78  uint32_t value;
79  memcpy(&value, ptr + offset, 4);
80  return llvm::ByteSwap_32(value);
81 }
82 
83 static inline uint64_t ReadSwapInt64(const unsigned char *ptr,
84  offset_t offset) {
85  uint64_t value;
86  memcpy(&value, ptr + offset, 8);
87  return llvm::ByteSwap_64(value);
88 }
89 
90 static inline uint16_t ReadSwapInt16(const void *ptr) {
91  uint16_t value;
92  memcpy(&value, ptr, 2);
93  return llvm::ByteSwap_16(value);
94 }
95 
96 static inline uint32_t ReadSwapInt32(const void *ptr) {
97  uint32_t value;
98  memcpy(&value, ptr, 4);
99  return llvm::ByteSwap_32(value);
100 }
101 
102 static inline uint64_t ReadSwapInt64(const void *ptr) {
103  uint64_t value;
104  memcpy(&value, ptr, 8);
105  return llvm::ByteSwap_64(value);
106 }
107 
108 static inline uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size,
109  ByteOrder byte_order) {
110  uint64_t res = 0;
111  if (byte_order == eByteOrderBig)
112  for (size_t i = 0; i < byte_size; ++i)
113  res = (res << 8) | data[i];
114  else {
115  assert(byte_order == eByteOrderLittle);
116  for (size_t i = 0; i < byte_size; ++i)
117  res = (res << 8) | data[byte_size - 1 - i];
118  }
119  return res;
120 }
121 
122 DataExtractor::DataExtractor()
123  : m_start(nullptr), m_end(nullptr),
124  m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
125  m_data_sp(), m_target_byte_size(1) {}
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.
129 DataExtractor::DataExtractor(const void *data, offset_t length,
130  ByteOrder endian, uint32_t addr_size,
131  uint32_t target_byte_size /*=1*/)
132  : m_start(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data))),
133  m_end(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data)) +
134  length),
135  m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
136  m_target_byte_size(target_byte_size) {
137  assert(addr_size == 4 || addr_size == 8);
138 }
139 
140 // Make a shared pointer reference to the shared data in "data_sp" and set the
141 // endian swapping setting to "swap", and the address size to "addr_size". The
142 // shared data reference will ensure the data lives as long as any
143 // DataExtractor objects exist that have a reference to this data.
144 DataExtractor::DataExtractor(const DataBufferSP &data_sp, ByteOrder endian,
145  uint32_t addr_size,
146  uint32_t target_byte_size /*=1*/)
147  : m_start(nullptr), m_end(nullptr), m_byte_order(endian),
148  m_addr_size(addr_size), m_data_sp(),
149  m_target_byte_size(target_byte_size) {
150  assert(addr_size == 4 || addr_size == 8);
151  SetData(data_sp);
152 }
153 
154 // Initialize this object with a subset of the data bytes in "data". If "data"
155 // contains shared data, then a reference to this shared data will added and
156 // the shared data will stay around as long as any object contains a reference
157 // to that data. The endian swap and address size settings are copied from
158 // "data".
160  offset_t length, uint32_t target_byte_size /*=1*/)
161  : m_start(nullptr), m_end(nullptr), m_byte_order(data.m_byte_order),
163  m_target_byte_size(target_byte_size) {
164  assert(m_addr_size == 4 || m_addr_size == 8);
165  if (data.ValidOffset(offset)) {
166  offset_t bytes_available = data.GetByteSize() - offset;
167  if (length > bytes_available)
168  length = bytes_available;
169  SetData(data, offset, length);
170  }
171 }
172 
174  : m_start(rhs.m_start), m_end(rhs.m_end), m_byte_order(rhs.m_byte_order),
177  assert(m_addr_size == 4 || m_addr_size == 8);
178 }
179 
180 // Assignment operator
182  if (this != &rhs) {
183  m_start = rhs.m_start;
184  m_end = rhs.m_end;
186  m_addr_size = rhs.m_addr_size;
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.
228  ByteOrder endian) {
229  m_byte_order = endian;
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 *>(reinterpret_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) {
254  m_addr_size = data.m_addr_size;
255  assert(m_addr_size == 4 || m_addr_size == 8);
256  // If "data" contains shared pointer to data, then we can use that
257  if (data.m_data_sp) {
258  m_byte_order = data.m_byte_order;
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.
284 lldb::offset_t DataExtractor::SetData(const DataBufferSP &data_sp,
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.
320 uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const {
321  const uint8_t *data = (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.
333 void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst,
334  uint32_t count) const {
335  const uint8_t *data = (const uint8_t *)GetData(offset_ptr, count);
336  if (data) {
337  // Copy the data into the buffer
338  memcpy(dst, data, count);
339  // Return a non-nullptr pointer to the converted data as an indicator of
340  // success
341  return dst;
342  }
343  return nullptr;
344 }
345 
346 // Extract a single uint16_t from the data and update the offset pointed to by
347 // "offset_ptr".
348 //
349 // RETURNS the uint16_t that was extracted, or zero on failure.
351  uint16_t val = 0;
352  const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val));
353  if (data) {
355  val = ReadSwapInt16(data);
356  else
357  val = ReadInt16(data);
358  }
359  return val;
360 }
361 
363  uint16_t val;
365  val = ReadInt16(m_start, *offset_ptr);
366  else
367  val = ReadSwapInt16(m_start, *offset_ptr);
368  *offset_ptr += sizeof(val);
369  return val;
370 }
371 
373  uint32_t val;
375  val = ReadInt32(m_start, *offset_ptr);
376  else
377  val = ReadSwapInt32(m_start, *offset_ptr);
378  *offset_ptr += sizeof(val);
379  return val;
380 }
381 
382 uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const {
383  uint64_t val;
385  val = ReadInt64(m_start, *offset_ptr);
386  else
387  val = ReadSwapInt64(m_start, *offset_ptr);
388  *offset_ptr += sizeof(val);
389  return val;
390 }
391 
392 // Extract "count" uint16_t values from the binary data and update the offset
393 // pointed to by "offset_ptr". The extracted data is copied into "dst".
394 //
395 // RETURNS the non-nullptr buffer pointer upon successful extraction of
396 // all the requested bytes, or nullptr when the data is not available in the
397 // buffer due to being out of bounds, or insufficient data.
398 void *DataExtractor::GetU16(offset_t *offset_ptr, void *void_dst,
399  uint32_t count) const {
400  const size_t src_size = sizeof(uint16_t) * count;
401  const uint16_t *src = (const uint16_t *)GetData(offset_ptr, src_size);
402  if (src) {
404  uint16_t *dst_pos = (uint16_t *)void_dst;
405  uint16_t *dst_end = dst_pos + count;
406  const uint16_t *src_pos = src;
407  while (dst_pos < dst_end) {
408  *dst_pos = ReadSwapInt16(src_pos);
409  ++dst_pos;
410  ++src_pos;
411  }
412  } else {
413  memcpy(void_dst, src, src_size);
414  }
415  // Return a non-nullptr pointer to the converted data as an indicator of
416  // success
417  return void_dst;
418  }
419  return nullptr;
420 }
421 
422 // Extract a single uint32_t from the data and update the offset pointed to by
423 // "offset_ptr".
424 //
425 // RETURNS the uint32_t that was extracted, or zero on failure.
427  uint32_t val = 0;
428  const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val));
429  if (data) {
431  val = ReadSwapInt32(data);
432  } else {
433  memcpy(&val, data, 4);
434  }
435  }
436  return val;
437 }
438 
439 // Extract "count" uint32_t values from the binary data and update the offset
440 // pointed to by "offset_ptr". The extracted data is copied into "dst".
441 //
442 // RETURNS the non-nullptr buffer pointer upon successful extraction of
443 // all the requested bytes, or nullptr when the data is not available in the
444 // buffer due to being out of bounds, or insufficient data.
445 void *DataExtractor::GetU32(offset_t *offset_ptr, void *void_dst,
446  uint32_t count) const {
447  const size_t src_size = sizeof(uint32_t) * count;
448  const uint32_t *src = (const uint32_t *)GetData(offset_ptr, src_size);
449  if (src) {
451  uint32_t *dst_pos = (uint32_t *)void_dst;
452  uint32_t *dst_end = dst_pos + count;
453  const uint32_t *src_pos = src;
454  while (dst_pos < dst_end) {
455  *dst_pos = ReadSwapInt32(src_pos);
456  ++dst_pos;
457  ++src_pos;
458  }
459  } else {
460  memcpy(void_dst, src, src_size);
461  }
462  // Return a non-nullptr pointer to the converted data as an indicator of
463  // success
464  return void_dst;
465  }
466  return nullptr;
467 }
468 
469 // Extract a single uint64_t from the data and update the offset pointed to by
470 // "offset_ptr".
471 //
472 // RETURNS the uint64_t that was extracted, or zero on failure.
473 uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const {
474  uint64_t val = 0;
475  const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val));
476  if (data) {
478  val = ReadSwapInt64(data);
479  } else {
480  memcpy(&val, data, 8);
481  }
482  }
483  return val;
484 }
485 
486 // GetU64
487 //
488 // Get multiple consecutive 64 bit values. Return true if the entire read
489 // succeeds and increment the offset pointed to by offset_ptr, else return
490 // false and leave the offset pointed to by offset_ptr unchanged.
491 void *DataExtractor::GetU64(offset_t *offset_ptr, void *void_dst,
492  uint32_t count) const {
493  const size_t src_size = sizeof(uint64_t) * count;
494  const uint64_t *src = (const uint64_t *)GetData(offset_ptr, src_size);
495  if (src) {
497  uint64_t *dst_pos = (uint64_t *)void_dst;
498  uint64_t *dst_end = dst_pos + count;
499  const uint64_t *src_pos = src;
500  while (dst_pos < dst_end) {
501  *dst_pos = ReadSwapInt64(src_pos);
502  ++dst_pos;
503  ++src_pos;
504  }
505  } else {
506  memcpy(void_dst, src, src_size);
507  }
508  // Return a non-nullptr pointer to the converted data as an indicator of
509  // success
510  return void_dst;
511  }
512  return nullptr;
513 }
514 
516  size_t byte_size) const {
517  lldbassert(byte_size > 0 && byte_size <= 4 && "GetMaxU32 invalid byte_size!");
518  return GetMaxU64(offset_ptr, byte_size);
519 }
520 
521 uint64_t DataExtractor::GetMaxU64(offset_t *offset_ptr,
522  size_t byte_size) const {
523  lldbassert(byte_size > 0 && byte_size <= 8 && "GetMaxU64 invalid byte_size!");
524  switch (byte_size) {
525  case 1:
526  return GetU8(offset_ptr);
527  case 2:
528  return GetU16(offset_ptr);
529  case 4:
530  return GetU32(offset_ptr);
531  case 8:
532  return GetU64(offset_ptr);
533  default: {
534  // General case.
535  const uint8_t *data =
536  static_cast<const uint8_t *>(GetData(offset_ptr, byte_size));
537  if (data == nullptr)
538  return 0;
539  return ReadMaxInt64(data, byte_size, m_byte_order);
540  }
541  }
542  return 0;
543 }
544 
546  size_t byte_size) const {
547  switch (byte_size) {
548  case 1:
549  return GetU8_unchecked(offset_ptr);
550  case 2:
551  return GetU16_unchecked(offset_ptr);
552  case 4:
553  return GetU32_unchecked(offset_ptr);
554  case 8:
555  return GetU64_unchecked(offset_ptr);
556  default: {
557  uint64_t res = ReadMaxInt64(&m_start[*offset_ptr], byte_size, m_byte_order);
558  *offset_ptr += byte_size;
559  return res;
560  }
561  }
562  return 0;
563 }
564 
565 int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_t byte_size) const {
566  uint64_t u64 = GetMaxU64(offset_ptr, byte_size);
567  return llvm::SignExtend64(u64, 8 * byte_size);
568 }
569 
570 uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size,
571  uint32_t bitfield_bit_size,
572  uint32_t bitfield_bit_offset) const {
573  uint64_t uval64 = GetMaxU64(offset_ptr, size);
574  if (bitfield_bit_size > 0) {
575  int32_t lsbcount = bitfield_bit_offset;
577  lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
578  if (lsbcount > 0)
579  uval64 >>= lsbcount;
580  uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
581  if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
582  return uval64;
583  uval64 &= bitfield_mask;
584  }
585  return uval64;
586 }
587 
588 int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size,
589  uint32_t bitfield_bit_size,
590  uint32_t bitfield_bit_offset) const {
591  int64_t sval64 = GetMaxS64(offset_ptr, size);
592  if (bitfield_bit_size > 0) {
593  int32_t lsbcount = bitfield_bit_offset;
595  lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
596  if (lsbcount > 0)
597  sval64 >>= lsbcount;
598  uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
599  sval64 &= bitfield_mask;
600  // sign extend if needed
601  if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
602  sval64 |= ~bitfield_mask;
603  }
604  return sval64;
605 }
606 
607 float DataExtractor::GetFloat(offset_t *offset_ptr) const {
608  typedef float float_type;
609  float_type val = 0.0;
610  const size_t src_size = sizeof(float_type);
611  const float_type *src = (const float_type *)GetData(offset_ptr, src_size);
612  if (src) {
614  const uint8_t *src_data = (const uint8_t *)src;
615  uint8_t *dst_data = (uint8_t *)&val;
616  for (size_t i = 0; i < sizeof(float_type); ++i)
617  dst_data[sizeof(float_type) - 1 - i] = src_data[i];
618  } else {
619  val = *src;
620  }
621  }
622  return val;
623 }
624 
625 double DataExtractor::GetDouble(offset_t *offset_ptr) const {
626  typedef double float_type;
627  float_type val = 0.0;
628  const size_t src_size = sizeof(float_type);
629  const float_type *src = (const float_type *)GetData(offset_ptr, src_size);
630  if (src) {
632  const uint8_t *src_data = (const uint8_t *)src;
633  uint8_t *dst_data = (uint8_t *)&val;
634  for (size_t i = 0; i < sizeof(float_type); ++i)
635  dst_data[sizeof(float_type) - 1 - i] = src_data[i];
636  } else {
637  val = *src;
638  }
639  }
640  return val;
641 }
642 
643 long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const {
644  long double val = 0.0;
645 #if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \
646  defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
647  *offset_ptr += CopyByteOrderedData(*offset_ptr, 10, &val, sizeof(val),
649 #else
650  *offset_ptr += CopyByteOrderedData(*offset_ptr, sizeof(val), &val,
651  sizeof(val), endian::InlHostByteOrder());
652 #endif
653  return val;
654 }
655 
656 // Extract a single address from the data and update the offset pointed to by
657 // "offset_ptr". The size of the extracted address comes from the
658 // "this->m_addr_size" member variable and should be set correctly prior to
659 // extracting any address values.
660 //
661 // RETURNS the address that was extracted, or zero on failure.
662 uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const {
663  assert(m_addr_size == 4 || m_addr_size == 8);
664  return GetMaxU64(offset_ptr, m_addr_size);
665 }
666 
667 uint64_t DataExtractor::GetAddress_unchecked(offset_t *offset_ptr) const {
668  assert(m_addr_size == 4 || m_addr_size == 8);
669  return GetMaxU64_unchecked(offset_ptr, m_addr_size);
670 }
671 
672 // Extract a single pointer from the data and update the offset pointed to by
673 // "offset_ptr". The size of the extracted pointer comes from the
674 // "this->m_addr_size" member variable and should be set correctly prior to
675 // extracting any pointer values.
676 //
677 // RETURNS the pointer that was extracted, or zero on failure.
678 uint64_t DataExtractor::GetPointer(offset_t *offset_ptr) const {
679  assert(m_addr_size == 4 || m_addr_size == 8);
680  return GetMaxU64(offset_ptr, m_addr_size);
681 }
682 
684  ByteOrder dst_byte_order, void *dst) const {
685  const uint8_t *src = PeekData(offset, length);
686  if (src) {
687  if (dst_byte_order != GetByteOrder()) {
688  // Validate that only a word- or register-sized dst is byte swapped
689  assert(length == 1 || length == 2 || length == 4 || length == 8 ||
690  length == 10 || length == 16 || length == 32);
691 
692  for (uint32_t i = 0; i < length; ++i)
693  ((uint8_t *)dst)[i] = src[length - i - 1];
694  } else
695  ::memcpy(dst, src, length);
696  return length;
697  }
698  return 0;
699 }
700 
701 // Extract data as it exists in target memory
703  void *dst) const {
704  const uint8_t *src = PeekData(offset, length);
705  if (src) {
706  ::memcpy(dst, src, length);
707  return length;
708  }
709  return 0;
710 }
711 
712 // Extract data and swap if needed when doing the copy
715  void *dst_void_ptr, offset_t dst_len,
716  ByteOrder dst_byte_order) const {
717  // Validate the source info
718  if (!ValidOffsetForDataOfSize(src_offset, src_len))
719  assert(ValidOffsetForDataOfSize(src_offset, src_len));
720  assert(src_len > 0);
722 
723  // Validate the destination info
724  assert(dst_void_ptr != nullptr);
725  assert(dst_len > 0);
726  assert(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
727 
728  // Validate that only a word- or register-sized dst is byte swapped
729  assert(dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
730  dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
731  dst_len == 32);
732 
733  // Must have valid byte orders set in this object and for destination
734  if (!(dst_byte_order == eByteOrderBig ||
735  dst_byte_order == eByteOrderLittle) ||
737  return 0;
738 
739  uint8_t *dst = (uint8_t *)dst_void_ptr;
740  const uint8_t *src = (const uint8_t *)PeekData(src_offset, src_len);
741  if (src) {
742  if (dst_len >= src_len) {
743  // We are copying the entire value from src into dst. Calculate how many,
744  // if any, zeroes we need for the most significant bytes if "dst_len" is
745  // greater than "src_len"...
746  const size_t num_zeroes = dst_len - src_len;
747  if (dst_byte_order == eByteOrderBig) {
748  // Big endian, so we lead with zeroes...
749  if (num_zeroes > 0)
750  ::memset(dst, 0, num_zeroes);
751  // Then either copy or swap the rest
752  if (m_byte_order == eByteOrderBig) {
753  ::memcpy(dst + num_zeroes, src, src_len);
754  } else {
755  for (uint32_t i = 0; i < src_len; ++i)
756  dst[i + num_zeroes] = src[src_len - 1 - i];
757  }
758  } else {
759  // Little endian destination, so we lead the value bytes
760  if (m_byte_order == eByteOrderBig) {
761  for (uint32_t i = 0; i < src_len; ++i)
762  dst[i] = src[src_len - 1 - i];
763  } else {
764  ::memcpy(dst, src, src_len);
765  }
766  // And zero the rest...
767  if (num_zeroes > 0)
768  ::memset(dst + src_len, 0, num_zeroes);
769  }
770  return src_len;
771  } else {
772  // We are only copying some of the value from src into dst..
773 
774  if (dst_byte_order == eByteOrderBig) {
775  // Big endian dst
776  if (m_byte_order == eByteOrderBig) {
777  // Big endian dst, with big endian src
778  ::memcpy(dst, src + (src_len - dst_len), dst_len);
779  } else {
780  // Big endian dst, with little endian src
781  for (uint32_t i = 0; i < dst_len; ++i)
782  dst[i] = src[dst_len - 1 - i];
783  }
784  } else {
785  // Little endian dst
786  if (m_byte_order == eByteOrderBig) {
787  // Little endian dst, with big endian src
788  for (uint32_t i = 0; i < dst_len; ++i)
789  dst[i] = src[src_len - 1 - i];
790  } else {
791  // Little endian dst, with big endian src
792  ::memcpy(dst, src, dst_len);
793  }
794  }
795  return dst_len;
796  }
797  }
798  return 0;
799 }
800 
801 // Extracts a variable length NULL terminated C string from the data at the
802 // offset pointed to by "offset_ptr". The "offset_ptr" will be updated with
803 // the offset of the byte that follows the NULL terminator byte.
804 //
805 // If the offset pointed to by "offset_ptr" is out of bounds, or if "length" is
806 // non-zero and there aren't enough available bytes, nullptr will be returned
807 // and "offset_ptr" will not be updated.
808 const char *DataExtractor::GetCStr(offset_t *offset_ptr) const {
809  const char *cstr = (const char *)PeekData(*offset_ptr, 1);
810  if (cstr) {
811  const char *cstr_end = cstr;
812  const char *end = (const char *)m_end;
813  while (cstr_end < end && *cstr_end)
814  ++cstr_end;
815 
816  // Now we are either at the end of the data or we point to the
817  // NULL C string terminator with cstr_end...
818  if (*cstr_end == '\0') {
819  // Advance the offset with one extra byte for the NULL terminator
820  *offset_ptr += (cstr_end - cstr + 1);
821  return cstr;
822  }
823 
824  // We reached the end of the data without finding a NULL C string
825  // terminator. Fall through and return nullptr otherwise anyone that would
826  // have used the result as a C string can wander into unknown memory...
827  }
828  return nullptr;
829 }
830 
831 // Extracts a NULL terminated C string from the fixed length field of length
832 // "len" at the offset pointed to by "offset_ptr". The "offset_ptr" will be
833 // updated with the offset of the byte that follows the fixed length field.
834 //
835 // If the offset pointed to by "offset_ptr" is out of bounds, or if the offset
836 // plus the length of the field is out of bounds, or if the field does not
837 // contain a NULL terminator byte, nullptr will be returned and "offset_ptr"
838 // will not be updated.
839 const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const {
840  const char *cstr = (const char *)PeekData(*offset_ptr, len);
841  if (cstr != nullptr) {
842  if (memchr(cstr, '\0', len) == nullptr) {
843  return nullptr;
844  }
845  *offset_ptr += len;
846  return cstr;
847  }
848  return nullptr;
849 }
850 
851 // Peeks at a string in the contained data. No verification is done to make
852 // sure the entire string lies within the bounds of this object's data, only
853 // "offset" is verified to be a valid offset.
854 //
855 // Returns a valid C string pointer if "offset" is a valid offset in this
856 // object's data, else nullptr is returned.
857 const char *DataExtractor::PeekCStr(offset_t offset) const {
858  return (const char *)PeekData(offset, 1);
859 }
860 
861 // Extracts an unsigned LEB128 number from this object's data starting at the
862 // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
863 // will be updated with the offset of the byte following the last extracted
864 // byte.
865 //
866 // Returned the extracted integer value.
867 uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const {
868  const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1);
869  if (src == nullptr)
870  return 0;
871 
872  const uint8_t *end = m_end;
873 
874  if (src < end) {
875  uint64_t result = *src++;
876  if (result >= 0x80) {
877  result &= 0x7f;
878  int shift = 7;
879  while (src < end) {
880  uint8_t byte = *src++;
881  result |= (uint64_t)(byte & 0x7f) << shift;
882  if ((byte & 0x80) == 0)
883  break;
884  shift += 7;
885  }
886  }
887  *offset_ptr = src - m_start;
888  return result;
889  }
890 
891  return 0;
892 }
893 
894 // Extracts an signed LEB128 number from this object's data starting at the
895 // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
896 // will be updated with the offset of the byte following the last extracted
897 // byte.
898 //
899 // Returned the extracted integer value.
900 int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const {
901  const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1);
902  if (src == nullptr)
903  return 0;
904 
905  const uint8_t *end = m_end;
906 
907  if (src < end) {
908  int64_t result = 0;
909  int shift = 0;
910  int size = sizeof(int64_t) * 8;
911 
912  uint8_t byte = 0;
913  int bytecount = 0;
914 
915  while (src < end) {
916  bytecount++;
917  byte = *src++;
918  result |= (int64_t)(byte & 0x7f) << shift;
919  shift += 7;
920  if ((byte & 0x80) == 0)
921  break;
922  }
923 
924  // Sign bit of byte is 2nd high order bit (0x40)
925  if (shift < size && (byte & 0x40))
926  result |= -(1 << shift);
927 
928  *offset_ptr += bytecount;
929  return result;
930  }
931  return 0;
932 }
933 
934 // Skips a ULEB128 number (signed or unsigned) from this object's data starting
935 // at the offset pointed to by "offset_ptr". The offset pointed to by
936 // "offset_ptr" will be updated with the offset of the byte following the last
937 // extracted byte.
938 //
939 // Returns the number of bytes consumed during the extraction.
941  uint32_t bytes_consumed = 0;
942  const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1);
943  if (src == nullptr)
944  return 0;
945 
946  const uint8_t *end = m_end;
947 
948  if (src < end) {
949  const uint8_t *src_pos = src;
950  while ((src_pos < end) && (*src_pos++ & 0x80))
951  ++bytes_consumed;
952  *offset_ptr += src_pos - src;
953  }
954  return bytes_consumed;
955 }
956 
957 // Dumps bytes from this object's data to the stream "s" starting
958 // "start_offset" bytes into this data, and ending with the byte before
959 // "end_offset". "base_addr" will be added to the offset into the dumped data
960 // when showing the offset into the data in the output information.
961 // "num_per_line" objects of type "type" will be dumped with the option to
962 // override the format for each object with "type_format". "type_format" is a
963 // printf style formatting string. If "type_format" is nullptr, then an
964 // appropriate format string will be used for the supplied "type". If the
965 // stream "s" is nullptr, then the output will be send to Log().
967  offset_t length, uint64_t base_addr,
968  uint32_t num_per_line,
969  DataExtractor::Type type,
970  const char *format) const {
971  if (log == nullptr)
972  return start_offset;
973 
974  offset_t offset;
975  offset_t end_offset;
976  uint32_t count;
977  StreamString sstr;
978  for (offset = start_offset, end_offset = offset + length, count = 0;
979  ValidOffset(offset) && offset < end_offset; ++count) {
980  if ((count % num_per_line) == 0) {
981  // Print out any previous string
982  if (sstr.GetSize() > 0) {
983  log->PutString(sstr.GetString());
984  sstr.Clear();
985  }
986  // Reset string offset and fill the current line string with address:
987  if (base_addr != LLDB_INVALID_ADDRESS)
988  sstr.Printf("0x%8.8" PRIx64 ":",
989  (uint64_t)(base_addr + (offset - start_offset)));
990  }
991 
992  switch (type) {
993  case TypeUInt8:
994  sstr.Printf(format ? format : " %2.2x", GetU8(&offset));
995  break;
996  case TypeChar: {
997  char ch = GetU8(&offset);
998  sstr.Printf(format ? format : " %c", isprint(ch) ? ch : ' ');
999  } break;
1000  case TypeUInt16:
1001  sstr.Printf(format ? format : " %4.4x", GetU16(&offset));
1002  break;
1003  case TypeUInt32:
1004  sstr.Printf(format ? format : " %8.8x", GetU32(&offset));
1005  break;
1006  case TypeUInt64:
1007  sstr.Printf(format ? format : " %16.16" PRIx64, GetU64(&offset));
1008  break;
1009  case TypePointer:
1010  sstr.Printf(format ? format : " 0x%" PRIx64, GetAddress(&offset));
1011  break;
1012  case TypeULEB128:
1013  sstr.Printf(format ? format : " 0x%" PRIx64, GetULEB128(&offset));
1014  break;
1015  case TypeSLEB128:
1016  sstr.Printf(format ? format : " %" PRId64, GetSLEB128(&offset));
1017  break;
1018  }
1019  }
1020 
1021  if (!sstr.Empty())
1022  log->PutString(sstr.GetString());
1023 
1024  return offset; // Return the offset at which we ended up
1025 }
1026 
1027 size_t DataExtractor::Copy(DataExtractor &dest_data) const {
1028  if (m_data_sp) {
1029  // we can pass along the SP to the data
1030  dest_data.SetData(m_data_sp);
1031  } else {
1032  const uint8_t *base_ptr = m_start;
1033  size_t data_size = GetByteSize();
1034  dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
1035  }
1036  return GetByteSize();
1037 }
1038 
1040  if (rhs.GetByteOrder() != GetByteOrder())
1041  return false;
1042 
1043  if (rhs.GetByteSize() == 0)
1044  return true;
1045 
1046  if (GetByteSize() == 0)
1047  return (rhs.Copy(*this) > 0);
1048 
1049  size_t bytes = GetByteSize() + rhs.GetByteSize();
1050 
1051  DataBufferHeap *buffer_heap_ptr = nullptr;
1052  DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
1053 
1054  if (!buffer_sp || buffer_heap_ptr == nullptr)
1055  return false;
1056 
1057  uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1058 
1059  memcpy(bytes_ptr, GetDataStart(), GetByteSize());
1060  memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
1061 
1062  SetData(buffer_sp);
1063 
1064  return true;
1065 }
1066 
1067 bool DataExtractor::Append(void *buf, offset_t length) {
1068  if (buf == nullptr)
1069  return false;
1070 
1071  if (length == 0)
1072  return true;
1073 
1074  size_t bytes = GetByteSize() + length;
1075 
1076  DataBufferHeap *buffer_heap_ptr = nullptr;
1077  DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
1078 
1079  if (!buffer_sp || buffer_heap_ptr == nullptr)
1080  return false;
1081 
1082  uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1083 
1084  if (GetByteSize() > 0)
1085  memcpy(bytes_ptr, GetDataStart(), GetByteSize());
1086 
1087  memcpy(bytes_ptr + GetByteSize(), buf, length);
1088 
1089  SetData(buffer_sp);
1090 
1091  return true;
1092 }
1093 
1095  uint64_t max_data) {
1096  if (max_data == 0)
1097  max_data = GetByteSize();
1098  else
1099  max_data = std::min(max_data, GetByteSize());
1100 
1101  llvm::MD5 md5;
1102 
1103  const llvm::ArrayRef<uint8_t> data(GetDataStart(), max_data);
1104  md5.update(data);
1105 
1106  llvm::MD5::MD5Result result;
1107  md5.final(result);
1108 
1109  dest.clear();
1110  dest.append(result.Bytes.begin(), result.Bytes.end());
1111 }
An data extractor class.
Definition: DataExtractor.h:47
static uint32_t ReadInt32(const unsigned char *ptr, offset_t offset=0)
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
static bool isprint(char32_t codepoint)
Format output as pointers.
Definition: DataExtractor.h:57
#define lldbassert(x)
Definition: LLDBAssert.h:15
const char * GetCStr(lldb::offset_t *offset_ptr) const
Extract a C string from *offset_ptr.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
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 byte_size from *offset_ptr, then extract and signe extend the bitfi...
Format output as ULEB128 numbers.
Definition: DataExtractor.h:58
Format output as unsigned 8 bit integers.
Definition: DataExtractor.h:52
static uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size, ByteOrder byte_order)
void PutString(llvm::StringRef str)
Definition: Log.cpp:110
Format output as unsigned 64 bit integers.
Definition: DataExtractor.h:56
int64_t GetSLEB128(lldb::offset_t *offset_ptr) const
Extract a signed LEB128 value from *offset_ptr.
uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const
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.
const char * PeekCStr(lldb::offset_t offset) const
Peek at a C string at offset.
const DataExtractor & operator=(const DataExtractor &rhs)
Assignment operator.
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...
float GetFloat(lldb::offset_t *offset_ptr) const
Extract a float from *offset_ptr.
A subclass of DataBuffer that stores a data buffer on the heap.
Type
Type enumerations used in the dump routines.
Definition: DataExtractor.h:51
static uint64_t ReadInt64(const unsigned char *ptr, offset_t offset=0)
static uint64_t ReadSwapInt64(const unsigned char *ptr, offset_t offset)
long double GetLongDouble(lldb::offset_t *offset_ptr) const
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
uint64_t offset_t
Definition: lldb-types.h:87
virtual ~DataExtractor()
Destructor.
void Clear()
Clears the object state.
lldb::offset_t CopyData(lldb::offset_t offset, lldb::offset_t length, void *dst) const
Copy length bytes from *offset, without swapping bytes.
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
uint64_t GetAddress_unchecked(lldb::offset_t *offset_ptr) const
size_t GetSharedDataOffset() const
Get the shared data offset.
void Checksum(llvm::SmallVectorImpl< uint8_t > &dest, uint64_t max_data=0)
uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const
llvm::StringRef GetString() const
lldb::ByteOrder m_byte_order
The byte order of the data we are extracting from.
Format output as unsigned 16 bit integers.
Definition: DataExtractor.h:54
double GetDouble(lldb::offset_t *offset_ptr) const
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
size_t Copy(DataExtractor &dest_data) const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
const uint8_t * m_end
A pointer to the byte that is past the end of the data.
int64_t GetMaxS64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an signed integer of size byte_size from *offset_ptr.
bool ValidOffset(lldb::offset_t offset) const
Test the validity of offset.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
static uint16_t ReadSwapInt16(const unsigned char *ptr, offset_t offset)
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
Format output as unsigned 32 bit integers.
Definition: DataExtractor.h:55
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...
const uint32_t m_target_byte_size
DataExtractor()
Default constructor.
uint64_t GetMaxU64_unchecked(lldb::offset_t *offset_ptr, size_t byte_size) const
static uint16_t ReadInt16(const unsigned char *ptr, offset_t offset)
const uint8_t * m_start
A pointer to the first byte of data.
static uint32_t ReadSwapInt32(const unsigned char *ptr, offset_t offset)
const uint8_t * GetDataStart() const
Get the data start pointer.
uint32_t GetMaxU32(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an integer of size byte_size from *offset_ptr.
uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const
Definition: SBAddress.h:15
uint32_t m_addr_size
The address size to use when extracting pointers or addresses.
Format output as characters.
Definition: DataExtractor.h:53
uint8_t * GetBytes() override
uint64_t GetULEB128(lldb::offset_t *offset_ptr) const
Extract a unsigned LEB128 value from *offset_ptr.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const
Skip an LEB128 number at *offset_ptr.
llvm::ArrayRef< uint8_t > GetData() 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 char *type_format=nullptr) const
Dumps the binary data as type objects to stream s (or to Log() if s is nullptr) starting offset bytes...
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
lldb::DataBufferSP m_data_sp
The shared pointer to data that can be shared among multiple instances.
uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const
uint64_t GetPointer(lldb::offset_t *offset_ptr) const
Extract an pointer from *offset_ptr.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
bool Append(DataExtractor &rhs)
Format output as SLEB128 numbers.
Definition: DataExtractor.h:59
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
const uint8_t * PeekData(lldb::offset_t offset, lldb::offset_t length) const
Peek at a bytes at offset.