LLDB mainline
DataExtractor.h
Go to the documentation of this file.
1//===-- DataExtractor.h -----------------------------------------*- 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
9#ifndef LLDB_UTILITY_DATAEXTRACTOR_H
10#define LLDB_UTILITY_DATAEXTRACTOR_H
11
12#include "lldb/Utility/Endian.h"
13#include "lldb/lldb-defines.h"
15#include "lldb/lldb-forward.h"
16#include "lldb/lldb-types.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/Support/DataExtractor.h"
19#include "llvm/Support/SwapByteOrder.h"
20
21#include <cassert>
22#include <cstdint>
23#include <cstring>
24
25namespace lldb_private {
26class Log;
27class Stream;
28}
29namespace llvm {
30template <typename T> class SmallVectorImpl;
31}
32
33
34namespace lldb_private {
35
36/// \class DataExtractor DataExtractor.h "lldb/Core/DataExtractor.h" An data
37/// extractor class.
38///
39/// DataExtractor is a class that can extract data (swapping if needed) from a
40/// data buffer. The data buffer can be caller owned, or can be shared data
41/// that can be shared between multiple DataExtractor instances. Multiple
42/// DataExtractor objects can share the same data, yet extract values in
43/// different address sizes and byte order modes. Each object can have a
44/// unique position in the shared data and extract data from different
45/// offsets.
46///
47/// \see DataBuffer
49public:
50 /// \typedef DataExtractor::Type
51 /// Type enumerations used in the dump routines.
52 enum Type {
53 TypeUInt8, ///< Format output as unsigned 8 bit integers
54 TypeChar, ///< Format output as characters
55 TypeUInt16, ///< Format output as unsigned 16 bit integers
56 TypeUInt32, ///< Format output as unsigned 32 bit integers
57 TypeUInt64, ///< Format output as unsigned 64 bit integers
58 TypePointer, ///< Format output as pointers
59 TypeULEB128, ///< Format output as ULEB128 numbers
60 TypeSLEB128 ///< Format output as SLEB128 numbers
61 };
62
63 /// Default constructor.
64 ///
65 /// Initialize all members to a default empty state.
67
68 /// Construct with a buffer that is owned by the caller.
69 ///
70 /// This constructor allows us to use data that is owned by the caller. The
71 /// data must stay around as long as this object is valid.
72 ///
73 /// \param[in] data
74 /// A pointer to caller owned data.
75 ///
76 /// \param[in] data_length
77 /// The length in bytes of \a data.
78 ///
79 /// \param[in] byte_order
80 /// A byte order of the data that we are extracting from.
81 ///
82 /// \param[in] addr_size
83 /// A new address byte size value.
84 ///
85 /// \param[in] target_byte_size
86 /// A size of a target byte in 8-bit host bytes
87 DataExtractor(const void *data, lldb::offset_t data_length,
88 lldb::ByteOrder byte_order, uint32_t addr_size,
89 uint32_t target_byte_size = 1);
90
91 /// Construct with shared data.
92 ///
93 /// Copies the data shared pointer which adds a reference to the contained
94 /// in \a data_sp. The shared data reference is reference counted to ensure
95 /// the data lives as long as anyone still has a valid shared pointer to the
96 /// data in \a data_sp.
97 ///
98 /// \param[in] data_sp
99 /// A shared pointer to data.
100 ///
101 /// \param[in] byte_order
102 /// A byte order of the data that we are extracting from.
103 ///
104 /// \param[in] addr_size
105 /// A new address byte size value.
106 ///
107 /// \param[in] target_byte_size
108 /// A size of a target byte in 8-bit host bytes
109 DataExtractor(const lldb::DataBufferSP &data_sp, lldb::ByteOrder byte_order,
110 uint32_t addr_size, uint32_t target_byte_size = 1);
111
112 /// Construct with a subset of \a data.
113 ///
114 /// Initialize this object with a subset of the data bytes in \a data. If \a
115 /// data contains shared data, then a reference to the shared data will be
116 /// added to ensure the shared data stays around as long as any objects have
117 /// references to the shared data. The byte order value and the address size
118 /// settings are copied from \a data. If \a offset is not a valid offset in
119 /// \a data, then no reference to the shared data will be added. If there
120 /// are not \a length bytes available in \a data starting at \a offset, the
121 /// length will be truncated to contain as many bytes as possible.
122 ///
123 /// \param[in] data
124 /// Another DataExtractor object that contains data.
125 ///
126 /// \param[in] offset
127 /// The offset into \a data at which the subset starts.
128 ///
129 /// \param[in] length
130 /// The length in bytes of the subset of data.
131 ///
132 /// \param[in] target_byte_size
133 /// A size of a target byte in 8-bit host bytes
134 DataExtractor(const DataExtractor &data, lldb::offset_t offset,
135 lldb::offset_t length, uint32_t target_byte_size = 1);
136
137 /// Copy constructor.
138 ///
139 /// The copy constructor is explicit as otherwise it is easy to make
140 /// unintended modification of a local copy instead of a caller's instance.
141 /// Also a needless copy of the \a m_data_sp shared pointer is/ expensive.
142 explicit DataExtractor(const DataExtractor &rhs);
143
144 /// Assignment operator.
145 ///
146 /// Copies all data, byte order and address size settings from \a rhs into
147 /// this object. If \a rhs contains shared data, a reference to that shared
148 /// data will be added.
149 ///
150 /// \param[in] rhs
151 /// Another DataExtractor object to copy.
152 ///
153 /// \return
154 /// A const reference to this object.
155 const DataExtractor &operator=(const DataExtractor &rhs);
156
157 /// Move constructor and move assignment operators to complete the rule of 5.
158 ///
159 /// They would get deleted as we already defined those of rule of 3.
160 DataExtractor(DataExtractor &&rhs) = default;
162
163 /// Destructor
164 ///
165 /// If this object contains a valid shared data reference, the reference
166 /// count on the data will be decremented, and if zero, the data will be
167 /// freed.
168 virtual ~DataExtractor();
169
170 uint32_t getTargetByteSize() const { return m_target_byte_size; }
171
172 /// Clears the object state.
173 ///
174 /// Clears the object contents back to a default invalid state, and release
175 /// any references to shared data that this object may contain.
176 void Clear();
177
178 /// Dumps the binary data as \a type objects to stream \a s (or to Log() if
179 /// \a s is nullptr) starting \a offset bytes into the data and stopping
180 /// after dumping \a length bytes. The offset into the data is displayed at
181 /// the beginning of each line and can be offset by base address \a
182 /// base_addr. \a num_per_line objects will be displayed on each line.
183 ///
184 /// \param[in] log
185 /// The log to dump the output to.
186 ///
187 /// \param[in] offset
188 /// The offset into the data at which to start dumping.
189 ///
190 /// \param[in] length
191 /// The number of bytes to dump.
192 ///
193 /// \param[in] base_addr
194 /// The base address that gets added to the offset displayed on
195 /// each line.
196 ///
197 /// \param[in] num_per_line
198 /// The number of \a type objects to display on each line.
199 ///
200 /// \param[in] type
201 /// The type of objects to use when dumping data from this
202 /// object. See DataExtractor::Type.
203 ///
204 /// \return
205 /// The offset at which dumping ended.
207 lldb::offset_t length, uint64_t base_addr,
208 uint32_t num_per_line, Type type) const;
209
210 /// Extract an arbitrary number of bytes in the specified byte order.
211 ///
212 /// Attemps to extract \a length bytes starting at \a offset bytes into this
213 /// data in the requested byte order (\a dst_byte_order) and place the
214 /// results in \a dst. \a dst must be at least \a length bytes long.
215 ///
216 /// \param[in] offset
217 /// The offset in bytes into the contained data at which to
218 /// start extracting.
219 ///
220 /// \param[in] length
221 /// The number of bytes to extract.
222 ///
223 /// \param[in] dst_byte_order
224 /// A byte order of the data that we want when the value in
225 /// copied to \a dst.
226 ///
227 /// \param[out] dst
228 /// The buffer that will receive the extracted value if there
229 /// are enough bytes available in the current data.
230 ///
231 /// \return
232 /// The number of bytes that were extracted which will be \a
233 /// length when the value is successfully extracted, or zero
234 /// if there aren't enough bytes at the specified offset.
235 size_t ExtractBytes(lldb::offset_t offset, lldb::offset_t length,
236 lldb::ByteOrder dst_byte_order, void *dst) const;
237
238 /// Extract an address from \a *offset_ptr.
239 ///
240 /// Extract a single address from the data and update the offset pointed to
241 /// by \a offset_ptr. The size of the extracted address comes from the \a
242 /// m_addr_size member variable and should be set correctly prior to
243 /// extracting any address values.
244 ///
245 /// \param[in,out] offset_ptr
246 /// A pointer to an offset within the data that will be advanced
247 /// by the appropriate number of bytes if the value is extracted
248 /// correctly. If the offset is out of bounds or there are not
249 /// enough bytes to extract this value, the offset will be left
250 /// unmodified.
251 ///
252 /// \return
253 /// The extracted address value.
254 uint64_t GetAddress(lldb::offset_t *offset_ptr) const;
255
256 uint64_t GetAddress_unchecked(lldb::offset_t *offset_ptr) const;
257
258 /// Get the current address size.
259 ///
260 /// Return the size in bytes of any address values this object will extract.
261 ///
262 /// \return
263 /// The size in bytes of address values that will be extracted.
264 uint32_t GetAddressByteSize() const { return m_addr_size; }
265
266 /// Get the number of bytes contained in this object.
267 ///
268 /// \return
269 /// The total number of bytes of data this object refers to.
270 uint64_t GetByteSize() const { return m_end - m_start; }
271
272 /// Extract a C string from \a *offset_ptr.
273 ///
274 /// Returns a pointer to a C String from the data at the offset pointed to
275 /// by \a offset_ptr. A variable length NULL terminated C string will be
276 /// extracted and the \a offset_ptr will be updated with the offset of the
277 /// byte that follows the NULL terminator byte.
278 ///
279 /// \param[in,out] offset_ptr
280 /// A pointer to an offset within the data that will be advanced
281 /// by the appropriate number of bytes if the value is extracted
282 /// correctly. If the offset is out of bounds or there are not
283 /// enough bytes to extract this value, the offset will be left
284 /// unmodified.
285 ///
286 /// \return
287 /// A pointer to the C string value in the data. If the offset
288 /// pointed to by \a offset_ptr is out of bounds, or if the
289 /// offset plus the length of the C string is out of bounds,
290 /// nullptr will be returned.
291 const char *GetCStr(lldb::offset_t *offset_ptr) const;
292
293 /// Extract a C string from \a *offset_ptr with field size \a len.
294 ///
295 /// Returns a pointer to a C String from the data at the offset pointed to
296 /// by \a offset_ptr, with a field length of \a len.
297 /// A NULL terminated C string will be extracted and the \a offset_ptr
298 /// will be updated with the offset of the byte that follows the fixed
299 /// length field.
300 ///
301 /// \param[in,out] offset_ptr
302 /// A pointer to an offset within the data that will be advanced
303 /// by the appropriate number of bytes if the value is extracted
304 /// correctly. If the offset is out of bounds or there are not
305 /// enough bytes to extract this value, the offset will be left
306 /// unmodified.
307 ///
308 /// \return
309 /// A pointer to the C string value in the data. If the offset
310 /// pointed to by \a offset_ptr is out of bounds, or if the
311 /// offset plus the length of the field is out of bounds, or if
312 /// the field does not contain a NULL terminator byte, nullptr will
313 /// be returned.
314 const char *GetCStr(lldb::offset_t *offset_ptr, lldb::offset_t len) const;
315
316 /// Extract \a length bytes from \a *offset_ptr.
317 ///
318 /// Returns a pointer to a bytes in this object's data at the offset pointed
319 /// to by \a offset_ptr. If \a length is zero or too large, then the offset
320 /// pointed to by \a offset_ptr will not be updated and nullptr will be
321 /// returned.
322 ///
323 /// \param[in,out] offset_ptr
324 /// A pointer to an offset within the data that will be advanced
325 /// by the appropriate number of bytes if the value is extracted
326 /// correctly. If the offset is out of bounds or there are not
327 /// enough bytes to extract this value, the offset will be left
328 /// unmodified.
329 ///
330 /// \param[in] length
331 /// The optional length of a string to extract. If the value is
332 /// zero, a NULL terminated C string will be extracted.
333 ///
334 /// \return
335 /// A pointer to the bytes in this object's data if the offset
336 /// and length are valid, or nullptr otherwise.
337 virtual const void *GetData(lldb::offset_t *offset_ptr,
338 lldb::offset_t length) const {
339 const uint8_t *ptr = PeekData(*offset_ptr, length);
340 if (ptr)
341 *offset_ptr += length;
342 return ptr;
343 }
344
345 /// Copy \a length bytes from \a *offset, without swapping bytes.
346 ///
347 /// \param[in] offset
348 /// The offset into this data from which to start copying
349 ///
350 /// \param[in] length
351 /// The length of the data to copy from this object
352 ///
353 /// \param[out] dst
354 /// The buffer to place the output data.
355 ///
356 /// \return
357 /// Returns the number of bytes that were copied, or zero if
358 /// anything goes wrong.
360 void *dst) const;
361
362 /// Copy \a dst_len bytes from \a *offset_ptr and ensure the copied data is
363 /// treated as a value that can be swapped to match the specified byte
364 /// order.
365 ///
366 /// For values that are larger than the supported integer sizes, this
367 /// function can be used to extract data in a specified byte order. It can
368 /// also be used to copy a smaller integer value from to a larger value. The
369 /// extra bytes left over will be padded correctly according to the byte
370 /// order of this object and the \a dst_byte_order. This can be very handy
371 /// when say copying a partial data value into a register.
372 ///
373 /// \param[in] src_offset
374 /// The offset into this data from which to start copying an endian
375 /// entity
376 ///
377 /// \param[in] src_len
378 /// The length of the endian data to copy from this object into the \a
379 /// dst object
380 ///
381 /// \param[out] dst
382 /// The buffer where to place the endian data. The data might need to be
383 /// byte swapped (and appropriately padded with zeroes if \a src_len !=
384 /// \a dst_len) if \a dst_byte_order does not match the byte order in
385 /// this object.
386 ///
387 /// \param[in] dst_len
388 /// The length number of bytes that the endian value will occupy is \a
389 /// dst.
390 ///
391 /// \param[in] dst_byte_order
392 /// The byte order that the endian value should be in the \a dst buffer.
393 ///
394 /// \return
395 /// Returns the number of bytes that were copied, or zero if anything
396 /// goes wrong.
398 lldb::offset_t src_len, void *dst,
399 lldb::offset_t dst_len,
400 lldb::ByteOrder dst_byte_order) const;
401
402 /// Get the data end pointer.
403 ///
404 /// \return
405 /// Returns a pointer to the next byte contained in this
406 /// object's data, or nullptr of there is no data in this object.
407 const uint8_t *GetDataEnd() const { return m_end; }
408
409 /// Get the shared data offset.
410 ///
411 /// Get the offset of the first byte of data in the shared data (if any).
412 ///
413 /// \return
414 /// If this object contains shared data, this function returns
415 /// the offset in bytes into that shared data, zero otherwise.
416 size_t GetSharedDataOffset() const;
417
418 /// Get the data start pointer.
419 ///
420 /// \return
421 /// Returns a pointer to the first byte contained in this
422 /// object's data, or nullptr of there is no data in this object.
423 const uint8_t *GetDataStart() const { return m_start; }
424
425 /// Extract a float from \a *offset_ptr.
426 ///
427 /// Extract a single float value.
428 ///
429 /// \param[in,out] offset_ptr
430 /// A pointer to an offset within the data that will be advanced
431 /// by the appropriate number of bytes if the value is extracted
432 /// correctly. If the offset is out of bounds or there are not
433 /// enough bytes to extract this value, the offset will be left
434 /// unmodified.
435 ///
436 /// \return
437 /// The floating value that was extracted, or zero on failure.
438 float GetFloat(lldb::offset_t *offset_ptr) const;
439
440 double GetDouble(lldb::offset_t *offset_ptr) const;
441
442 long double GetLongDouble(lldb::offset_t *offset_ptr) const;
443
444 /// Extract an integer of size \a byte_size from \a *offset_ptr.
445 ///
446 /// Extract a single integer value and update the offset pointed to by \a
447 /// offset_ptr. The size of the extracted integer is specified by the \a
448 /// byte_size argument. \a byte_size must have a value >= 1 and <= 4 since
449 /// the return value is only 32 bits wide.
450 ///
451 /// \param[in,out] offset_ptr
452 /// A pointer to an offset within the data that will be advanced
453 /// by the appropriate number of bytes if the value is extracted
454 /// correctly. If the offset is out of bounds or there are not
455 /// enough bytes to extract this value, the offset will be left
456 /// unmodified.
457 ///
458 /// \param[in] byte_size
459 /// The size in byte of the integer to extract.
460 ///
461 /// \return
462 /// The integer value that was extracted, or zero on failure.
463 uint32_t GetMaxU32(lldb::offset_t *offset_ptr, size_t byte_size) const;
464
465 /// Extract an unsigned integer of size \a byte_size from \a *offset_ptr.
466 ///
467 /// Extract a single unsigned integer value and update the offset pointed to
468 /// by \a offset_ptr. The size of the extracted integer is specified by the
469 /// \a byte_size argument. \a byte_size must have a value greater than or
470 /// equal to one and less than or equal to eight since the return value is
471 /// 64 bits wide.
472 ///
473 /// \param[in,out] offset_ptr
474 /// A pointer to an offset within the data that will be advanced
475 /// by the appropriate number of bytes if the value is extracted
476 /// correctly. If the offset is out of bounds or there are not
477 /// enough bytes to extract this value, the offset will be left
478 /// unmodified.
479 ///
480 /// \param[in] byte_size
481 /// The size in byte of the integer to extract.
482 ///
483 /// \return
484 /// The unsigned integer value that was extracted, or zero on
485 /// failure.
486 uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const;
487
488 uint64_t GetMaxU64_unchecked(lldb::offset_t *offset_ptr,
489 size_t byte_size) const;
490
491 /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
492 ///
493 /// Extract a single signed integer value (sign extending if required) and
494 /// update the offset pointed to by \a offset_ptr. The size of the extracted
495 /// integer is specified by the \a byte_size argument. \a byte_size must
496 /// have a value greater than or equal to one and less than or equal to
497 /// eight since the return value is 64 bits wide.
498 ///
499 /// \param[in,out] offset_ptr
500 /// A pointer to an offset within the data that will be advanced
501 /// by the appropriate number of bytes if the value is extracted
502 /// correctly. If the offset is out of bounds or there are not
503 /// enough bytes to extract this value, the offset will be left
504 /// unmodified.
505 ///
506 /// \param[in] byte_size
507 /// The size in byte of the integer to extract.
508 ///
509 /// \return
510 /// The sign extended signed integer value that was extracted,
511 /// or zero on failure.
512 int64_t GetMaxS64(lldb::offset_t *offset_ptr, size_t byte_size) const;
513
514 /// Extract an unsigned integer of size \a byte_size from \a *offset_ptr,
515 /// then extract the bitfield from this value if \a bitfield_bit_size is
516 /// non-zero.
517 ///
518 /// Extract a single unsigned integer value and update the offset pointed to
519 /// by \a offset_ptr. The size of the extracted integer is specified by the
520 /// \a byte_size argument. \a byte_size must have a value greater than or
521 /// equal to one and less than or equal to 8 since the return value is 64
522 /// bits wide.
523 ///
524 /// \param[in,out] offset_ptr
525 /// A pointer to an offset within the data that will be advanced
526 /// by the appropriate number of bytes if the value is extracted
527 /// correctly. If the offset is out of bounds or there are not
528 /// enough bytes to extract this value, the offset will be left
529 /// unmodified.
530 ///
531 /// \param[in] size
532 /// The size in byte of the integer to extract.
533 ///
534 /// \param[in] bitfield_bit_size
535 /// The size in bits of the bitfield value to extract, or zero
536 /// to just extract the entire integer value.
537 ///
538 /// \param[in] bitfield_bit_offset
539 /// The bit offset of the bitfield value in the extracted
540 /// integer. For little-endian data, this is the offset of
541 /// the LSB of the bitfield from the LSB of the integer.
542 /// For big-endian data, this is the offset of the MSB of the
543 /// bitfield from the MSB of the integer.
544 ///
545 /// \return
546 /// The unsigned bitfield integer value that was extracted, or
547 /// zero on failure.
548 uint64_t GetMaxU64Bitfield(lldb::offset_t *offset_ptr, size_t size,
549 uint32_t bitfield_bit_size,
550 uint32_t bitfield_bit_offset) const;
551
552 /// Extract an signed integer of size \a size from \a *offset_ptr, then
553 /// extract and sign-extend the bitfield from this value if \a
554 /// bitfield_bit_size is non-zero.
555 ///
556 /// Extract a single signed integer value (sign-extending if required) and
557 /// update the offset pointed to by \a offset_ptr. The size of the extracted
558 /// integer is specified by the \a size argument. \a size must
559 /// have a value greater than or equal to one and less than or equal to
560 /// eight since the return value is 64 bits wide.
561 ///
562 /// \param[in,out] offset_ptr
563 /// A pointer to an offset within the data that will be advanced
564 /// by the appropriate number of bytes if the value is extracted
565 /// correctly. If the offset is out of bounds or there are not
566 /// enough bytes to extract this value, the offset will be left
567 /// unmodified.
568 ///
569 /// \param[in] size
570 /// The size in bytes of the integer to extract.
571 ///
572 /// \param[in] bitfield_bit_size
573 /// The size in bits of the bitfield value to extract, or zero
574 /// to just extract the entire integer value.
575 ///
576 /// \param[in] bitfield_bit_offset
577 /// The bit offset of the bitfield value in the extracted
578 /// integer. For little-endian data, this is the offset of
579 /// the LSB of the bitfield from the LSB of the integer.
580 /// For big-endian data, this is the offset of the MSB of the
581 /// bitfield from the MSB of the integer.
582 ///
583 /// \return
584 /// The signed bitfield integer value that was extracted, or
585 /// zero on failure.
586 int64_t GetMaxS64Bitfield(lldb::offset_t *offset_ptr, size_t size,
587 uint32_t bitfield_bit_size,
588 uint32_t bitfield_bit_offset) const;
589
590 /// Get the current byte order value.
591 ///
592 /// \return
593 /// The current byte order value from this object's internal
594 /// state.
596
597 /// Extract a uint8_t value from \a *offset_ptr.
598 ///
599 /// Extract a single uint8_t from the binary data at the offset pointed to
600 /// by \a offset_ptr, and advance the offset on success.
601 ///
602 /// \param[in,out] offset_ptr
603 /// A pointer to an offset within the data that will be advanced
604 /// by the appropriate number of bytes if the value is extracted
605 /// correctly. If the offset is out of bounds or there are not
606 /// enough bytes to extract this value, the offset will be left
607 /// unmodified.
608 ///
609 /// \return
610 /// The extracted uint8_t value.
611 uint8_t GetU8(lldb::offset_t *offset_ptr) const;
612
613 virtual uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const {
614 uint8_t val = m_start[*offset_ptr];
615 *offset_ptr += 1;
616 return val;
617 }
618
619 virtual uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const;
620
621 virtual uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const;
622
623 virtual uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const;
624 /// Extract \a count uint8_t values from \a *offset_ptr.
625 ///
626 /// Extract \a count uint8_t values from the binary data at the offset
627 /// pointed to by \a offset_ptr, and advance the offset on success. The
628 /// extracted values are copied into \a dst.
629 ///
630 /// \param[in,out] offset_ptr
631 /// A pointer to an offset within the data that will be advanced
632 /// by the appropriate number of bytes if the value is extracted
633 /// correctly. If the offset is out of bounds or there are not
634 /// enough bytes to extract this value, the offset will be left
635 /// unmodified.
636 ///
637 /// \param[out] dst
638 /// A buffer to copy \a count uint8_t values into. \a dst must
639 /// be large enough to hold all requested data.
640 ///
641 /// \param[in] count
642 /// The number of uint8_t values to extract.
643 ///
644 /// \return
645 /// \a dst if all values were properly extracted and copied,
646 /// nullptr otherwise.
647 void *GetU8(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
648
649 /// Extract a uint16_t value from \a *offset_ptr.
650 ///
651 /// Extract a single uint16_t from the binary data at the offset pointed to
652 /// by \a offset_ptr, and update the offset on success.
653 ///
654 /// \param[in,out] offset_ptr
655 /// A pointer to an offset within the data that will be advanced
656 /// by the appropriate number of bytes if the value is extracted
657 /// correctly. If the offset is out of bounds or there are not
658 /// enough bytes to extract this value, the offset will be left
659 /// unmodified.
660 ///
661 /// \return
662 /// The extracted uint16_t value.
663 uint16_t GetU16(lldb::offset_t *offset_ptr) const;
664
665 /// Extract \a count uint16_t values from \a *offset_ptr.
666 ///
667 /// Extract \a count uint16_t values from the binary data at the offset
668 /// pointed to by \a offset_ptr, and advance the offset on success. The
669 /// extracted values are copied into \a dst.
670 ///
671 /// \param[in,out] offset_ptr
672 /// A pointer to an offset within the data that will be advanced
673 /// by the appropriate number of bytes if the value is extracted
674 /// correctly. If the offset is out of bounds or there are not
675 /// enough bytes to extract this value, the offset will be left
676 /// unmodified.
677 ///
678 /// \param[out] dst
679 /// A buffer to copy \a count uint16_t values into. \a dst must
680 /// be large enough to hold all requested data.
681 ///
682 /// \param[in] count
683 /// The number of uint16_t values to extract.
684 ///
685 /// \return
686 /// \a dst if all values were properly extracted and copied,
687 /// nullptr otherwise.
688 void *GetU16(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
689
690 /// Extract a uint32_t value from \a *offset_ptr.
691 ///
692 /// Extract a single uint32_t from the binary data at the offset pointed to
693 /// by \a offset_ptr, and update the offset on success.
694 ///
695 /// \param[in,out] offset_ptr
696 /// A pointer to an offset within the data that will be advanced
697 /// by the appropriate number of bytes if the value is extracted
698 /// correctly. If the offset is out of bounds or there are not
699 /// enough bytes to extract this value, the offset will be left
700 /// unmodified.
701 ///
702 /// \return
703 /// The extracted uint32_t value.
704 uint32_t GetU32(lldb::offset_t *offset_ptr) const;
705
706 /// Extract \a count uint32_t values from \a *offset_ptr.
707 ///
708 /// Extract \a count uint32_t values from the binary data at the offset
709 /// pointed to by \a offset_ptr, and advance the offset on success. The
710 /// extracted values are copied into \a dst.
711 ///
712 /// \param[in,out] offset_ptr
713 /// A pointer to an offset within the data that will be advanced
714 /// by the appropriate number of bytes if the value is extracted
715 /// correctly. If the offset is out of bounds or there are not
716 /// enough bytes to extract this value, the offset will be left
717 /// unmodified.
718 ///
719 /// \param[out] dst
720 /// A buffer to copy \a count uint32_t values into. \a dst must
721 /// be large enough to hold all requested data.
722 ///
723 /// \param[in] count
724 /// The number of uint32_t values to extract.
725 ///
726 /// \return
727 /// \a dst if all values were properly extracted and copied,
728 /// nullptr otherwise.
729 void *GetU32(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
730
731 /// Extract a uint64_t value from \a *offset_ptr.
732 ///
733 /// Extract a single uint64_t from the binary data at the offset pointed to
734 /// by \a offset_ptr, and update the offset on success.
735 ///
736 /// \param[in,out] offset_ptr
737 /// A pointer to an offset within the data that will be advanced
738 /// by the appropriate number of bytes if the value is extracted
739 /// correctly. If the offset is out of bounds or there are not
740 /// enough bytes to extract this value, the offset will be left
741 /// unmodified.
742 ///
743 /// \return
744 /// The extracted uint64_t value.
745 uint64_t GetU64(lldb::offset_t *offset_ptr) const;
746
747 /// Extract \a count uint64_t values from \a *offset_ptr.
748 ///
749 /// Extract \a count uint64_t values from the binary data at the offset
750 /// pointed to by \a offset_ptr, and advance the offset on success. The
751 /// extracted values are copied into \a dst.
752 ///
753 /// \param[in,out] offset_ptr
754 /// A pointer to an offset within the data that will be advanced
755 /// by the appropriate number of bytes if the value is extracted
756 /// correctly. If the offset is out of bounds or there are not
757 /// enough bytes to extract this value, the offset will be left
758 /// unmodified.
759 ///
760 /// \param[out] dst
761 /// A buffer to copy \a count uint64_t values into. \a dst must
762 /// be large enough to hold all requested data.
763 ///
764 /// \param[in] count
765 /// The number of uint64_t values to extract.
766 ///
767 /// \return
768 /// \a dst if all values were properly extracted and copied,
769 /// nullptr otherwise.
770 void *GetU64(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
771
772 /// Extract a signed LEB128 value from \a *offset_ptr.
773 ///
774 /// Extracts an signed LEB128 number from this object's data starting at the
775 /// offset pointed to by \a offset_ptr. The offset pointed to by \a
776 /// offset_ptr will be updated with the offset of the byte following the
777 /// last extracted byte.
778 ///
779 /// \param[in,out] offset_ptr
780 /// A pointer to an offset within the data that will be advanced
781 /// by the appropriate number of bytes if the value is extracted
782 /// correctly. If the offset is out of bounds or there are not
783 /// enough bytes to extract this value, the offset will be left
784 /// unmodified.
785 ///
786 /// \return
787 /// The extracted signed integer value.
788 int64_t GetSLEB128(lldb::offset_t *offset_ptr) const;
789
790 /// Extract a unsigned LEB128 value from \a *offset_ptr.
791 ///
792 /// Extracts an unsigned LEB128 number from this object's data starting at
793 /// the offset pointed to by \a offset_ptr. The offset pointed to by \a
794 /// offset_ptr will be updated with the offset of the byte following the
795 /// last extracted byte.
796 ///
797 /// \param[in,out] offset_ptr
798 /// A pointer to an offset within the data that will be advanced
799 /// by the appropriate number of bytes if the value is extracted
800 /// correctly. If the offset is out of bounds or there are not
801 /// enough bytes to extract this value, the offset will be left
802 /// unmodified.
803 ///
804 /// \return
805 /// The extracted unsigned integer value.
806 uint64_t GetULEB128(lldb::offset_t *offset_ptr) const;
807
809
810 /// Peek at a C string at \a offset.
811 ///
812 /// Peeks at a string in the contained data. No verification is done to make
813 /// sure the entire string lies within the bounds of this object's data,
814 /// only \a offset is verified to be a valid offset.
815 ///
816 /// \param[in] offset
817 /// An offset into the data.
818 ///
819 /// \return
820 /// A non-nullptr C string pointer if \a offset is a valid offset,
821 /// nullptr otherwise.
822 const char *PeekCStr(lldb::offset_t offset) const;
823
824 /// Peek at a bytes at \a offset.
825 ///
826 /// Returns a pointer to \a length bytes at \a offset as long as there are
827 /// \a length bytes available starting at \a offset.
828 ///
829 /// \return
830 /// A non-nullptr data pointer if \a offset is a valid offset and
831 /// there are \a length bytes available at that offset, nullptr
832 /// otherwise.
833 virtual const uint8_t *PeekData(lldb::offset_t offset,
834 lldb::offset_t length) const {
835 if (ValidOffsetForDataOfSize(offset, length))
836 return m_start + offset;
837 return nullptr;
838 }
839
840 /// Set the address byte size.
841 ///
842 /// Set the size in bytes that will be used when extracting any address and
843 /// pointer values from data contained in this object.
844 ///
845 /// \param[in] addr_size
846 /// The size in bytes to use when extracting addresses.
847 void SetAddressByteSize(uint32_t addr_size) {
848 assert(addr_size == 2 || addr_size == 4 || addr_size == 8);
849 m_addr_size = addr_size;
850 }
851
852 /// Set data with a buffer that is caller owned.
853 ///
854 /// Use data that is owned by the caller when extracting values. The data
855 /// must stay around as long as this object, or any object that copies a
856 /// subset of this object's data, is valid. If \a bytes is nullptr, or \a
857 /// length is zero, this object will contain no data.
858 ///
859 /// \param[in] bytes
860 /// A pointer to caller owned data.
861 ///
862 /// \param[in] length
863 /// The length in bytes of \a bytes.
864 ///
865 /// \param[in] byte_order
866 /// A byte order of the data that we are extracting from.
867 ///
868 /// \return
869 /// The number of bytes that this object now contains.
870 lldb::offset_t SetData(const void *bytes, lldb::offset_t length,
871 lldb::ByteOrder byte_order);
872
873 /// Adopt a subset of \a data.
874 ///
875 /// Set this object's data to be a subset of the data bytes in \a data. If
876 /// \a data contains shared data, then a reference to the shared data will
877 /// be added to ensure the shared data stays around as long as any objects
878 /// have references to the shared data. The byte order and the address size
879 /// settings are copied from \a data. If \a offset is not a valid offset in
880 /// \a data, then no reference to the shared data will be added. If there
881 /// are not \a length bytes available in \a data starting at \a offset, the
882 /// length will be truncated to contains as many bytes as possible.
883 ///
884 /// \param[in] data
885 /// Another DataExtractor object that contains data.
886 ///
887 /// \param[in] offset
888 /// The offset into \a data at which the subset starts.
889 ///
890 /// \param[in] length
891 /// The length in bytes of the subset of \a data.
892 ///
893 /// \return
894 /// The number of bytes that this object now contains.
896 lldb::offset_t length);
897
898 /// Adopt a subset of shared data in \a data_sp.
899 ///
900 /// Copies the data shared pointer which adds a reference to the contained
901 /// in \a data_sp. The shared data reference is reference counted to ensure
902 /// the data lives as long as anyone still has a valid shared pointer to the
903 /// data in \a data_sp. The byte order and address byte size settings remain
904 /// the same. If \a offset is not a valid offset in \a data_sp, then no
905 /// reference to the shared data will be added. If there are not \a length
906 /// bytes available in \a data starting at \a offset, the length will be
907 /// truncated to contains as many bytes as possible.
908 ///
909 /// \param[in] data_sp
910 /// A shared pointer to data.
911 ///
912 /// \param[in] offset
913 /// The offset into \a data_sp at which the subset starts.
914 ///
915 /// \param[in] length
916 /// The length in bytes of the subset of \a data_sp.
917 ///
918 /// \return
919 /// The number of bytes that this object now contains.
921 lldb::offset_t offset = 0,
923
924 /// Set the byte_order value.
925 ///
926 /// Sets the byte order of the data to extract. Extracted values will be
927 /// swapped if necessary when decoding.
928 ///
929 /// \param[in] byte_order
930 /// The byte order value to use when extracting data.
931 void SetByteOrder(lldb::ByteOrder byte_order) { m_byte_order = byte_order; }
932
933 /// Skip an LEB128 number at \a *offset_ptr.
934 ///
935 /// Skips a LEB128 number (signed or unsigned) from this object's data
936 /// starting at the offset pointed to by \a offset_ptr. The offset pointed
937 /// to by \a offset_ptr will be updated with the offset of the byte
938 /// following the last extracted byte.
939 ///
940 /// \param[in,out] offset_ptr
941 /// A pointer to an offset within the data that will be advanced
942 /// by the appropriate number of bytes if the value is extracted
943 /// correctly. If the offset is out of bounds or there are not
944 /// enough bytes to extract this value, the offset will be left
945 /// unmodified.
946 ///
947 /// \return
948 /// The number of bytes consumed during the extraction.
949 uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const;
950
951 /// Test the validity of \a offset.
952 ///
953 /// \return
954 /// true if \a offset is a valid offset into the data in this object,
955 /// false otherwise.
956 bool ValidOffset(lldb::offset_t offset) const {
957 return offset < GetByteSize();
958 }
959
960 /// Test the availability of \a length bytes of data from \a offset.
961 ///
962 /// \return
963 /// true if \a offset is a valid offset and there are \a
964 /// length bytes available at that offset, false otherwise.
966 lldb::offset_t length) const {
967 return length <= BytesLeft(offset);
968 }
969
970 size_t Copy(DataExtractor &dest_data) const;
971
972 bool Append(DataExtractor &rhs);
973
974 bool Append(void *bytes, lldb::offset_t length);
975
977 const lldb::offset_t size = GetByteSize();
978 if (size > offset)
979 return size - offset;
980 return 0;
981 }
982
983 void Checksum(llvm::SmallVectorImpl<uint8_t> &dest, uint64_t max_data = 0);
984
985 llvm::ArrayRef<uint8_t> GetData() const {
986 return {GetDataStart(), size_t(GetByteSize())};
987 }
988
989 llvm::DataExtractor GetAsLLVM() const {
991 uint8_t(GetAddressByteSize())};
992 }
993
994protected:
995 template <typename T> T Get(lldb::offset_t *offset_ptr, T fail_value) const {
996 constexpr size_t src_size = sizeof(T);
997 T val = fail_value;
998
999 const void *src = GetData(offset_ptr, src_size);
1000 if (!src)
1001 return val;
1002
1003 memcpy(&val, src, src_size);
1005 llvm::sys::swapByteOrder(val);
1006
1007 return val;
1008 }
1009
1010 // Member variables
1011 const uint8_t *m_start = nullptr; ///< A pointer to the first byte of data.
1012 const uint8_t *m_end =
1013 nullptr; ///< A pointer to the byte that is past the end of the data.
1015 m_byte_order; ///< The byte order of the data we are extracting from.
1016 uint32_t m_addr_size; ///< The address size to use when extracting addresses.
1017 /// The shared pointer to data that can be shared among multiple instances
1019 /// Making it const would require implementation of move assignment operator.
1021};
1022
1023} // namespace lldb_private
1024
1025#endif // LLDB_UTILITY_DATAEXTRACTOR_H
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.
uint32_t getTargetByteSize() const
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
lldb::DataBufferSP & GetSharedDataBuffer()
llvm::DataExtractor GetAsLLVM() const
uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const
Skip an LEB128 number at *offset_ptr.
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
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.
uint32_t m_target_byte_size
Making it const would require implementation of move assignment operator.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
DataExtractor & operator=(DataExtractor &&rhs)=default
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.
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
const uint8_t * GetDataEnd() const
Get the data end pointer.
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
llvm::ArrayRef< uint8_t > GetData() const
lldb::offset_t BytesLeft(lldb::offset_t offset) 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.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
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...
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.
DataExtractor(DataExtractor &&rhs)=default
Move constructor and move assignment operators to complete the rule of 5.
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.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
#define LLDB_INVALID_OFFSET
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