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