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