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