1 //===-- DataExtractor.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_DataExtractor_h_
11 #define liblldb_DataExtractor_h_
19 // Other libraries and framework includes
20 #include "llvm/ADT/SmallVector.h"
23 #include "lldb/lldb-private.h"
25 namespace lldb_private {
27 //----------------------------------------------------------------------
28 /// @class DataExtractor DataExtractor.h "lldb/Core/DataExtractor.h"
29 /// @brief An data extractor class.
31 /// DataExtractor is a class that can extract data (swapping if needed)
32 /// from a data buffer. The data buffer can be caller owned, or can be
33 /// shared data that can be shared between multiple DataExtractor
34 /// instances. Multiple DataExtractor objects can share the same data,
35 /// yet extract values in different address sizes and byte order modes.
36 /// Each object can have a unique position in the shared data and extract
37 /// data from different offsets.
40 //----------------------------------------------------------------------
43 //------------------------------------------------------------------
44 /// @typedef DataExtractor::Type
45 /// @brief Type enumerations used in the dump routines.
46 /// @see DataExtractor::Dump()
47 /// @see DataExtractor::DumpRawHexBytes()
48 //------------------------------------------------------------------
50 TypeUInt8, ///< Format output as unsigned 8 bit integers
51 TypeChar, ///< Format output as characters
52 TypeUInt16, ///< Format output as unsigned 16 bit integers
53 TypeUInt32, ///< Format output as unsigned 32 bit integers
54 TypeUInt64, ///< Format output as unsigned 64 bit integers
55 TypePointer, ///< Format output as pointers
56 TypeULEB128, ///< Format output as ULEB128 numbers
57 TypeSLEB128 ///< Format output as SLEB128 numbers
60 static void DumpHexBytes(Stream *s, const void *src, size_t src_len,
61 uint32_t bytes_per_line,
62 lldb::addr_t base_addr); // Pass LLDB_INVALID_ADDRESS
63 // to not show address at
66 //------------------------------------------------------------------
67 /// Default constructor.
69 /// Initialize all members to a default empty state.
70 //------------------------------------------------------------------
73 //------------------------------------------------------------------
74 /// Construct with a buffer that is owned by the caller.
76 /// This constructor allows us to use data that is owned by the
77 /// caller. The data must stay around as long as this object is
81 /// A pointer to caller owned data.
83 /// @param[in] data_length
84 /// The length in bytes of \a data.
86 /// @param[in] byte_order
87 /// A byte order of the data that we are extracting from.
89 /// @param[in] addr_size
90 /// A new address byte size value.
92 /// @param[in] target_byte_size
93 /// A size of a target byte in 8-bit host bytes
94 //------------------------------------------------------------------
95 DataExtractor(const void *data, lldb::offset_t data_length,
96 lldb::ByteOrder byte_order, uint32_t addr_size,
97 uint32_t target_byte_size = 1);
99 //------------------------------------------------------------------
100 /// Construct with shared data.
102 /// Copies the data shared pointer which adds a reference to the
103 /// contained in \a data_sp. The shared data reference is reference
104 /// counted to ensure the data lives as long as anyone still has a
105 /// valid shared pointer to the data in \a data_sp.
107 /// @param[in] data_sp
108 /// A shared pointer to data.
110 /// @param[in] byte_order
111 /// A byte order of the data that we are extracting from.
113 /// @param[in] addr_size
114 /// A new address byte size value.
116 /// @param[in] target_byte_size
117 /// A size of a target byte in 8-bit host bytes
118 //------------------------------------------------------------------
119 DataExtractor(const lldb::DataBufferSP &data_sp, lldb::ByteOrder byte_order,
120 uint32_t addr_size, uint32_t target_byte_size = 1);
122 //------------------------------------------------------------------
123 /// Construct with a subset of \a data.
125 /// Initialize this object with a subset of the data bytes in \a
126 /// data. If \a data contains shared data, then a reference to the
127 /// shared data will be added to ensure the shared data stays around
128 /// as long as any objects have references to the shared data. The
129 /// byte order value and the address size settings are copied from \a
130 /// data. If \a offset is not a valid offset in \a data, then no
131 /// reference to the shared data will be added. If there are not
132 /// \a length bytes available in \a data starting at \a offset,
133 /// the length will be truncated to contain as many bytes as
137 /// Another DataExtractor object that contains data.
139 /// @param[in] offset
140 /// The offset into \a data at which the subset starts.
142 /// @param[in] length
143 /// The length in bytes of the subset of data.
145 /// @param[in] target_byte_size
146 /// A size of a target byte in 8-bit host bytes
147 //------------------------------------------------------------------
148 DataExtractor(const DataExtractor &data, lldb::offset_t offset,
149 lldb::offset_t length, uint32_t target_byte_size = 1);
151 DataExtractor(const DataExtractor &rhs);
153 //------------------------------------------------------------------
154 /// Assignment operator.
156 /// Copies all data, byte order and address size settings from \a rhs into
157 /// this object. If \a rhs contains shared data, a reference to that
158 /// shared data will be added.
161 /// Another DataExtractor object to copy.
164 /// A const reference to this object.
165 //------------------------------------------------------------------
166 const DataExtractor &operator=(const DataExtractor &rhs);
168 //------------------------------------------------------------------
171 /// If this object contains a valid shared data reference, the
172 /// reference count on the data will be decremented, and if zero,
173 /// the data will be freed.
174 //------------------------------------------------------------------
177 //------------------------------------------------------------------
178 /// Clears the object state.
180 /// Clears the object contents back to a default invalid state, and
181 /// release any references to shared data that this object may
183 //------------------------------------------------------------------
186 //------------------------------------------------------------------
187 /// Dumps the binary data as \a type objects to stream \a s (or to
188 /// Log() if \a s is nullptr) starting \a offset bytes into the data
189 /// and stopping after dumping \a length bytes. The offset into the
190 /// data is displayed at the beginning of each line and can be
191 /// offset by base address \a base_addr. \a num_per_line objects
192 /// will be displayed on each line.
195 /// The stream to dump the output to. If nullptr the output will
196 /// be dumped to Log().
198 /// @param[in] offset
199 /// The offset into the data at which to start dumping.
201 /// @param[in] length
202 /// The number of bytes to dump.
204 /// @param[in] base_addr
205 /// The base address that gets added to the offset displayed on
208 /// @param[in] num_per_line
209 /// The number of \a type objects to display on each line.
212 /// The type of objects to use when dumping data from this
213 /// object. See DataExtractor::Type.
215 /// @param[in] type_format
216 /// The optional format to use for the \a type objects. If this
217 /// is nullptr, the default format for the \a type will be used.
220 /// The offset at which dumping ended.
221 //------------------------------------------------------------------
222 lldb::offset_t PutToLog(Log *log, lldb::offset_t offset,
223 lldb::offset_t length, uint64_t base_addr,
224 uint32_t num_per_line, Type type,
225 const char *type_format = nullptr) const;
227 //------------------------------------------------------------------
228 /// Dumps \a item_count objects into the stream \a s.
230 /// Dumps \a item_count objects using \a item_format, each of which
231 /// are \a item_byte_size bytes long starting at offset \a offset
232 /// bytes into the contained data, into the stream \a s. \a
233 /// num_per_line objects will be dumped on each line before a new
234 /// line will be output. If \a base_addr is a valid address, then
235 /// each new line of output will be preceded by the address value
236 /// plus appropriate offset, and a colon and space. Bitfield values
237 /// can be dumped by calling this function multiple times with the
238 /// same start offset, format and size, yet differing \a
239 /// item_bit_size and \a item_bit_offset values.
242 /// The stream to dump the output to. This value can not be nullptr.
244 /// @param[in] offset
245 /// The offset into the data at which to start dumping.
247 /// @param[in] item_format
248 /// The format to use when dumping each item.
250 /// @param[in] item_byte_size
251 /// The byte size of each item.
253 /// @param[in] item_count
254 /// The number of items to dump.
256 /// @param[in] num_per_line
257 /// The number of items to display on each line.
259 /// @param[in] base_addr
260 /// The base address that gets added to the offset displayed on
261 /// each line if the value is valid. Is \a base_addr is
262 /// LLDB_INVALID_ADDRESS then no address values will be prepended
265 /// @param[in] item_bit_size
266 /// If the value to display is a bitfield, this value should
267 /// be the number of bits that the bitfield item has within the
268 /// item's byte size value. This function will need to be called
269 /// multiple times with identical \a offset and \a item_byte_size
270 /// values in order to display multiple bitfield values that
271 /// exist within the same integer value. If the items being
272 /// displayed are not bitfields, this value should be zero.
274 /// @param[in] item_bit_offset
275 /// If the value to display is a bitfield, this value should
276 /// be the offset in bits, or shift right amount, that the
277 /// bitfield item occupies within the item's byte size value.
278 /// This function will need to be called multiple times with
279 /// identical \a offset and \a item_byte_size values in order
280 /// to display multiple bitfield values that exist within the
281 /// same integer value. If the items being displayed are not
282 /// bitfields, this value should be zero.
285 /// The offset at which dumping ended.
286 //------------------------------------------------------------------
287 lldb::offset_t Dump(Stream *s, lldb::offset_t offset,
288 lldb::Format item_format, size_t item_byte_size,
289 size_t item_count, size_t num_per_line,
290 uint64_t base_addr, uint32_t item_bit_size,
291 uint32_t item_bit_offset,
292 ExecutionContextScope *exe_scope = nullptr) const;
294 //------------------------------------------------------------------
295 /// Dump a UUID value at \a offset.
297 /// Dump a UUID starting at \a offset bytes into this object's data.
298 /// If the stream \a s is nullptr, the output will be sent to Log().
301 /// The stream to dump the output to. If nullptr the output will
302 /// be dumped to Log().
304 /// @param[in] offset
305 /// The offset into the data at which to extract and dump a
307 //------------------------------------------------------------------
308 void DumpUUID(Stream *s, lldb::offset_t offset) const;
310 //------------------------------------------------------------------
311 /// Extract an arbitrary number of bytes in the specified byte
314 /// Attemps to extract \a length bytes starting at \a offset bytes
315 /// into this data in the requested byte order (\a dst_byte_order)
316 /// and place the results in \a dst. \a dst must be at least \a
317 /// length bytes long.
319 /// @param[in] offset
320 /// The offset in bytes into the contained data at which to
321 /// start extracting.
323 /// @param[in] length
324 /// The number of bytes to extract.
326 /// @param[in] dst_byte_order
327 /// A byte order of the data that we want when the value in
328 /// copied to \a dst.
331 /// The buffer that will receive the extracted value if there
332 /// are enough bytes available in the current data.
335 /// The number of bytes that were extracted which will be \a
336 /// length when the value is successfully extracted, or zero
337 /// if there aren't enough bytes at the specified offset.
338 //------------------------------------------------------------------
339 size_t ExtractBytes(lldb::offset_t offset, lldb::offset_t length,
340 lldb::ByteOrder dst_byte_order, void *dst) const;
342 //------------------------------------------------------------------
343 /// Extract an address from \a *offset_ptr.
345 /// Extract a single address from the data and update the offset
346 /// pointed to by \a offset_ptr. The size of the extracted address
347 /// comes from the \a m_addr_size member variable and should be
348 /// set correctly prior to extracting any address values.
350 /// @param[in,out] offset_ptr
351 /// A pointer to an offset within the data that will be advanced
352 /// by the appropriate number of bytes if the value is extracted
353 /// correctly. If the offset is out of bounds or there are not
354 /// enough bytes to extract this value, the offset will be left
358 /// The extracted address value.
359 //------------------------------------------------------------------
360 uint64_t GetAddress(lldb::offset_t *offset_ptr) const;
362 uint64_t GetAddress_unchecked(lldb::offset_t *offset_ptr) const;
364 //------------------------------------------------------------------
365 /// Get the current address size.
367 /// Return the size in bytes of any address values this object will
371 /// The size in bytes of address values that will be extracted.
372 //------------------------------------------------------------------
373 uint32_t GetAddressByteSize() const { return m_addr_size; }
375 //------------------------------------------------------------------
376 /// Get the number of bytes contained in this object.
379 /// The total number of bytes of data this object refers to.
380 //------------------------------------------------------------------
381 uint64_t GetByteSize() const { return m_end - m_start; }
383 //------------------------------------------------------------------
384 /// Extract a C string from \a *offset_ptr.
386 /// Returns a pointer to a C String from the data at the offset
387 /// pointed to by \a offset_ptr. A variable length NULL terminated C
388 /// string will be extracted and the \a offset_ptr will be
389 /// updated with the offset of the byte that follows the NULL
392 /// @param[in,out] offset_ptr
393 /// A pointer to an offset within the data that will be advanced
394 /// by the appropriate number of bytes if the value is extracted
395 /// correctly. If the offset is out of bounds or there are not
396 /// enough bytes to extract this value, the offset will be left
400 /// A pointer to the C string value in the data. If the offset
401 /// pointed to by \a offset_ptr is out of bounds, or if the
402 /// offset plus the length of the C string is out of bounds,
403 /// nullptr will be returned.
404 //------------------------------------------------------------------
405 const char *GetCStr(lldb::offset_t *offset_ptr) const;
407 //------------------------------------------------------------------
408 /// Extract a C string from \a *offset_ptr with field size \a len.
410 /// Returns a pointer to a C String from the data at the offset
411 /// pointed to by \a offset_ptr, with a field length of \a len.
412 /// A NULL terminated C string will be extracted and the \a offset_ptr
413 /// will be updated with the offset of the byte that follows the fixed
416 /// @param[in,out] offset_ptr
417 /// A pointer to an offset within the data that will be advanced
418 /// by the appropriate number of bytes if the value is extracted
419 /// correctly. If the offset is out of bounds or there are not
420 /// enough bytes to extract this value, the offset will be left
424 /// A pointer to the C string value in the data. If the offset
425 /// pointed to by \a offset_ptr is out of bounds, or if the
426 /// offset plus the length of the field is out of bounds, or if
427 /// the field does not contain a NULL terminator byte, nullptr will
429 const char *GetCStr(lldb::offset_t *offset_ptr, lldb::offset_t len) const;
431 //------------------------------------------------------------------
432 /// Extract \a length bytes from \a *offset_ptr.
434 /// Returns a pointer to a bytes in this object's data at the offset
435 /// pointed to by \a offset_ptr. If \a length is zero or too large,
436 /// then the offset pointed to by \a offset_ptr will not be updated
437 /// and nullptr will be returned.
439 /// @param[in,out] offset_ptr
440 /// A pointer to an offset within the data that will be advanced
441 /// by the appropriate number of bytes if the value is extracted
442 /// correctly. If the offset is out of bounds or there are not
443 /// enough bytes to extract this value, the offset will be left
446 /// @param[in] length
447 /// The optional length of a string to extract. If the value is
448 /// zero, a NULL terminated C string will be extracted.
451 /// A pointer to the bytes in this object's data if the offset
452 /// and length are valid, or nullptr otherwise.
453 //------------------------------------------------------------------
454 const void *GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const {
455 const uint8_t *ptr = PeekData(*offset_ptr, length);
457 *offset_ptr += length;
461 //------------------------------------------------------------------
462 /// Copy \a length bytes from \a *offset, without swapping bytes.
464 /// @param[in] offset
465 /// The offset into this data from which to start copying
467 /// @param[in] length
468 /// The length of the data to copy from this object
471 /// The buffer to place the output data.
474 /// Returns the number of bytes that were copied, or zero if
475 /// anything goes wrong.
476 //------------------------------------------------------------------
477 lldb::offset_t CopyData(lldb::offset_t offset, lldb::offset_t length,
480 //------------------------------------------------------------------
481 /// Copy \a dst_len bytes from \a *offset_ptr and ensure the copied
482 /// data is treated as a value that can be swapped to match the
483 /// specified byte order.
485 /// For values that are larger than the supported integer sizes,
486 /// this function can be used to extract data in a specified byte
487 /// order. It can also be used to copy a smaller integer value from
488 /// to a larger value. The extra bytes left over will be padded
489 /// correctly according to the byte order of this object and the
490 /// \a dst_byte_order. This can be very handy when say copying a
491 /// partial data value into a register.
493 /// @param[in] src_offset
494 /// The offset into this data from which to start copying an
497 /// @param[in] src_len
498 /// The length of the endian data to copy from this object
499 /// into the \a dst object
502 /// The buffer where to place the endian data. The data might
503 /// need to be byte swapped (and appropriately padded with
504 /// zeroes if \a src_len != \a dst_len) if \a dst_byte_order
505 /// does not match the byte order in this object.
507 /// @param[in] dst_len
508 /// The length number of bytes that the endian value will
509 /// occupy is \a dst.
511 /// @param[in] byte_order
512 /// The byte order that the endian value should be in the \a dst
516 /// Returns the number of bytes that were copied, or zero if
517 /// anything goes wrong.
518 //------------------------------------------------------------------
519 lldb::offset_t CopyByteOrderedData(lldb::offset_t src_offset,
520 lldb::offset_t src_len, void *dst,
521 lldb::offset_t dst_len,
522 lldb::ByteOrder dst_byte_order) const;
524 //------------------------------------------------------------------
525 /// Get the data end pointer.
528 /// Returns a pointer to the next byte contained in this
529 /// object's data, or nullptr of there is no data in this object.
530 //------------------------------------------------------------------
531 const uint8_t *GetDataEnd() const { return m_end; }
533 //------------------------------------------------------------------
534 /// Get the shared data offset.
536 /// Get the offset of the first byte of data in the shared data (if
540 /// If this object contains shared data, this function returns
541 /// the offset in bytes into that shared data, zero otherwise.
542 //------------------------------------------------------------------
543 size_t GetSharedDataOffset() const;
545 //------------------------------------------------------------------
546 /// Get the data start pointer.
549 /// Returns a pointer to the first byte contained in this
550 /// object's data, or nullptr of there is no data in this object.
551 //------------------------------------------------------------------
552 const uint8_t *GetDataStart() const { return m_start; }
554 //------------------------------------------------------------------
555 /// Extract a float from \a *offset_ptr.
557 /// Extract a single float value.
559 /// @param[in,out] offset_ptr
560 /// A pointer to an offset within the data that will be advanced
561 /// by the appropriate number of bytes if the value is extracted
562 /// correctly. If the offset is out of bounds or there are not
563 /// enough bytes to extract this value, the offset will be left
567 /// The floating value that was extracted, or zero on failure.
568 //------------------------------------------------------------------
569 float GetFloat(lldb::offset_t *offset_ptr) const;
571 double GetDouble(lldb::offset_t *offset_ptr) const;
573 long double GetLongDouble(lldb::offset_t *offset_ptr) const;
575 //------------------------------------------------------------------
576 /// Extract a GNU encoded pointer value from \a *offset_ptr.
578 /// @param[in,out] offset_ptr
579 /// A pointer to an offset within the data that will be advanced
580 /// by the appropriate number of bytes if the value is extracted
581 /// correctly. If the offset is out of bounds or there are not
582 /// enough bytes to extract this value, the offset will be left
585 /// @param[in] eh_ptr_enc
586 /// The GNU pointer encoding type.
588 /// @param[in] pc_rel_addr
589 /// The PC relative address to use when the encoding is
590 /// \c DW_GNU_EH_PE_pcrel.
592 /// @param[in] text_addr
593 /// The text (code) relative address to use when the encoding is
594 /// \c DW_GNU_EH_PE_textrel.
596 /// @param[in] data_addr
597 /// The data relative address to use when the encoding is
598 /// \c DW_GNU_EH_PE_datarel.
601 /// The extracted GNU encoded pointer value.
602 //------------------------------------------------------------------
603 uint64_t GetGNUEHPointer(lldb::offset_t *offset_ptr, uint32_t eh_ptr_enc,
604 lldb::addr_t pc_rel_addr, lldb::addr_t text_addr,
605 lldb::addr_t data_addr);
607 //------------------------------------------------------------------
608 /// Extract an integer of size \a byte_size from \a *offset_ptr.
610 /// Extract a single integer value and update the offset pointed to
611 /// by \a offset_ptr. The size of the extracted integer is specified
612 /// by the \a byte_size argument. \a byte_size should have a value
613 /// >= 1 and <= 4 since the return value is only 32 bits wide. Any
614 /// \a byte_size values less than 1 or greater than 4 will result in
615 /// nothing being extracted, and zero being returned.
617 /// @param[in,out] offset_ptr
618 /// A pointer to an offset within the data that will be advanced
619 /// by the appropriate number of bytes if the value is extracted
620 /// correctly. If the offset is out of bounds or there are not
621 /// enough bytes to extract this value, the offset will be left
624 /// @param[in] byte_size
625 /// The size in byte of the integer to extract.
628 /// The integer value that was extracted, or zero on failure.
629 //------------------------------------------------------------------
630 uint32_t GetMaxU32(lldb::offset_t *offset_ptr, size_t byte_size) const;
632 //------------------------------------------------------------------
633 /// Extract an unsigned integer of size \a byte_size from \a
636 /// Extract a single unsigned integer value and update the offset
637 /// pointed to by \a offset_ptr. The size of the extracted integer
638 /// is specified by the \a byte_size argument. \a byte_size should
639 /// have a value greater than or equal to one and less than or equal
640 /// to eight since the return value is 64 bits wide. Any
641 /// \a byte_size values less than 1 or greater than 8 will result in
642 /// nothing being extracted, and zero being returned.
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
651 /// @param[in] byte_size
652 /// The size in byte of the integer to extract.
655 /// The unsigned integer value that was extracted, or zero on
657 //------------------------------------------------------------------
658 uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const;
660 uint64_t GetMaxU64_unchecked(lldb::offset_t *offset_ptr,
661 size_t byte_size) const;
663 //------------------------------------------------------------------
664 /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
666 /// Extract a single signed integer value (sign extending if required)
667 /// and update the offset pointed to by \a offset_ptr. The size of
668 /// the extracted integer is specified by the \a byte_size argument.
669 /// \a byte_size should have a value greater than or equal to one
670 /// and less than or equal to eight since the return value is 64
671 /// bits wide. Any \a byte_size values less than 1 or greater than
672 /// 8 will result in nothing being extracted, and zero being returned.
674 /// @param[in,out] offset_ptr
675 /// A pointer to an offset within the data that will be advanced
676 /// by the appropriate number of bytes if the value is extracted
677 /// correctly. If the offset is out of bounds or there are not
678 /// enough bytes to extract this value, the offset will be left
681 /// @param[in] byte_size
682 /// The size in byte of the integer to extract.
685 /// The sign extended signed integer value that was extracted,
686 /// or zero on failure.
687 //------------------------------------------------------------------
688 int64_t GetMaxS64(lldb::offset_t *offset_ptr, size_t size) const;
690 //------------------------------------------------------------------
691 /// Extract an unsigned integer of size \a byte_size from \a
692 /// *offset_ptr, then extract the bitfield from this value if
693 /// \a bitfield_bit_size is non-zero.
695 /// Extract a single unsigned integer value and update the offset
696 /// pointed to by \a offset_ptr. The size of the extracted integer
697 /// is specified by the \a byte_size argument. \a byte_size should
698 /// have a value greater than or equal to one and less than or equal
699 /// to 8 since the return value is 64 bits wide. Any
700 /// \a byte_size values less than 1 or greater than 8 will result in
701 /// nothing being extracted, and zero being returned.
703 /// @param[in,out] offset_ptr
704 /// A pointer to an offset within the data that will be advanced
705 /// by the appropriate number of bytes if the value is extracted
706 /// correctly. If the offset is out of bounds or there are not
707 /// enough bytes to extract this value, the offset will be left
710 /// @param[in] byte_size
711 /// The size in byte of the integer to extract.
713 /// @param[in] bitfield_bit_size
714 /// The size in bits of the bitfield value to extract, or zero
715 /// to just extract the entire integer value.
717 /// @param[in] bitfield_bit_offset
718 /// The bit offset of the bitfield value in the extracted
719 /// integer. For little-endian data, this is the offset of
720 /// the LSB of the bitfield from the LSB of the integer.
721 /// For big-endian data, this is the offset of the MSB of the
722 /// bitfield from the MSB of the integer.
725 /// The unsigned bitfield integer value that was extracted, or
727 //------------------------------------------------------------------
728 uint64_t GetMaxU64Bitfield(lldb::offset_t *offset_ptr, size_t size,
729 uint32_t bitfield_bit_size,
730 uint32_t bitfield_bit_offset) const;
732 //------------------------------------------------------------------
733 /// Extract an signed integer of size \a byte_size from \a
734 /// *offset_ptr, then extract and signe extend the bitfield from
735 /// this value if \a bitfield_bit_size is non-zero.
737 /// Extract a single signed integer value (sign extending if required)
738 /// and update the offset pointed to by \a offset_ptr. The size of
739 /// the extracted integer is specified by the \a byte_size argument.
740 /// \a byte_size should have a value greater than or equal to one
741 /// and less than or equal to eight since the return value is 64
742 /// bits wide. Any \a byte_size values less than 1 or greater than
743 /// 8 will result in nothing being extracted, and zero being returned.
745 /// @param[in,out] offset_ptr
746 /// A pointer to an offset within the data that will be advanced
747 /// by the appropriate number of bytes if the value is extracted
748 /// correctly. If the offset is out of bounds or there are not
749 /// enough bytes to extract this value, the offset will be left
752 /// @param[in] byte_size
753 /// The size in bytes of the integer to extract.
755 /// @param[in] bitfield_bit_size
756 /// The size in bits of the bitfield value to extract, or zero
757 /// to just extract the entire integer value.
759 /// @param[in] bitfield_bit_offset
760 /// The bit offset of the bitfield value in the extracted
761 /// integer. For little-endian data, this is the offset of
762 /// the LSB of the bitfield from the LSB of the integer.
763 /// For big-endian data, this is the offset of the MSB of the
764 /// bitfield from the MSB of the integer.
767 /// The signed bitfield integer value that was extracted, or
769 //------------------------------------------------------------------
770 int64_t GetMaxS64Bitfield(lldb::offset_t *offset_ptr, size_t size,
771 uint32_t bitfield_bit_size,
772 uint32_t bitfield_bit_offset) const;
774 //------------------------------------------------------------------
775 /// Extract an pointer from \a *offset_ptr.
777 /// Extract a single pointer from the data and update the offset
778 /// pointed to by \a offset_ptr. The size of the extracted pointer
779 /// comes from the \a m_addr_size member variable and should be
780 /// set correctly prior to extracting any pointer values.
782 /// @param[in,out] offset_ptr
783 /// A pointer to an offset within the data that will be advanced
784 /// by the appropriate number of bytes if the value is extracted
785 /// correctly. If the offset is out of bounds or there are not
786 /// enough bytes to extract this value, the offset will be left
790 /// The extracted pointer value as a 64 integer.
791 //------------------------------------------------------------------
792 uint64_t GetPointer(lldb::offset_t *offset_ptr) const;
794 //------------------------------------------------------------------
795 /// Get the current byte order value.
798 /// The current byte order value from this object's internal
800 //------------------------------------------------------------------
801 lldb::ByteOrder GetByteOrder() const { return m_byte_order; }
803 //------------------------------------------------------------------
804 /// Extract a uint8_t value from \a *offset_ptr.
806 /// Extract a single uint8_t from the binary data at the offset
807 /// pointed to by \a offset_ptr, and advance the offset on success.
809 /// @param[in,out] offset_ptr
810 /// A pointer to an offset within the data that will be advanced
811 /// by the appropriate number of bytes if the value is extracted
812 /// correctly. If the offset is out of bounds or there are not
813 /// enough bytes to extract this value, the offset will be left
817 /// The extracted uint8_t value.
818 //------------------------------------------------------------------
819 uint8_t GetU8(lldb::offset_t *offset_ptr) const;
821 uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const {
822 uint8_t val = m_start[*offset_ptr];
827 uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const;
829 uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const;
831 uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const;
832 //------------------------------------------------------------------
833 /// Extract \a count uint8_t values from \a *offset_ptr.
835 /// Extract \a count uint8_t values from the binary data at the
836 /// offset pointed to by \a offset_ptr, and advance the offset on
837 /// success. The extracted values are copied into \a dst.
839 /// @param[in,out] offset_ptr
840 /// A pointer to an offset within the data that will be advanced
841 /// by the appropriate number of bytes if the value is extracted
842 /// correctly. If the offset is out of bounds or there are not
843 /// enough bytes to extract this value, the offset will be left
847 /// A buffer to copy \a count uint8_t values into. \a dst must
848 /// be large enough to hold all requested data.
851 /// The number of uint8_t values to extract.
854 /// \a dst if all values were properly extracted and copied,
855 /// nullptr otherwise.
856 //------------------------------------------------------------------
857 void *GetU8(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
859 //------------------------------------------------------------------
860 /// Extract a uint16_t value from \a *offset_ptr.
862 /// Extract a single uint16_t from the binary data at the offset
863 /// pointed to by \a offset_ptr, and update the offset on success.
865 /// @param[in,out] offset_ptr
866 /// A pointer to an offset within the data that will be advanced
867 /// by the appropriate number of bytes if the value is extracted
868 /// correctly. If the offset is out of bounds or there are not
869 /// enough bytes to extract this value, the offset will be left
873 /// The extracted uint16_t value.
874 //------------------------------------------------------------------
875 uint16_t GetU16(lldb::offset_t *offset_ptr) const;
877 //------------------------------------------------------------------
878 /// Extract \a count uint16_t values from \a *offset_ptr.
880 /// Extract \a count uint16_t values from the binary data at the
881 /// offset pointed to by \a offset_ptr, and advance the offset on
882 /// success. The extracted values are copied into \a dst.
884 /// @param[in,out] offset_ptr
885 /// A pointer to an offset within the data that will be advanced
886 /// by the appropriate number of bytes if the value is extracted
887 /// correctly. If the offset is out of bounds or there are not
888 /// enough bytes to extract this value, the offset will be left
892 /// A buffer to copy \a count uint16_t values into. \a dst must
893 /// be large enough to hold all requested data.
896 /// The number of uint16_t values to extract.
899 /// \a dst if all values were properly extracted and copied,
900 /// nullptr otherwise.
901 //------------------------------------------------------------------
902 void *GetU16(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
904 //------------------------------------------------------------------
905 /// Extract a uint32_t value from \a *offset_ptr.
907 /// Extract a single uint32_t from the binary data at the offset
908 /// pointed to by \a offset_ptr, and update the offset on success.
910 /// @param[in,out] offset_ptr
911 /// A pointer to an offset within the data that will be advanced
912 /// by the appropriate number of bytes if the value is extracted
913 /// correctly. If the offset is out of bounds or there are not
914 /// enough bytes to extract this value, the offset will be left
918 /// The extracted uint32_t value.
919 //------------------------------------------------------------------
920 uint32_t GetU32(lldb::offset_t *offset_ptr) const;
922 //------------------------------------------------------------------
923 /// Extract \a count uint32_t values from \a *offset_ptr.
925 /// Extract \a count uint32_t values from the binary data at the
926 /// offset pointed to by \a offset_ptr, and advance the offset on
927 /// success. The extracted values are copied into \a dst.
929 /// @param[in,out] offset_ptr
930 /// A pointer to an offset within the data that will be advanced
931 /// by the appropriate number of bytes if the value is extracted
932 /// correctly. If the offset is out of bounds or there are not
933 /// enough bytes to extract this value, the offset will be left
937 /// A buffer to copy \a count uint32_t values into. \a dst must
938 /// be large enough to hold all requested data.
941 /// The number of uint32_t values to extract.
944 /// \a dst if all values were properly extracted and copied,
945 /// nullptr otherwise.
946 //------------------------------------------------------------------
947 void *GetU32(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
949 //------------------------------------------------------------------
950 /// Extract a uint64_t value from \a *offset_ptr.
952 /// Extract a single uint64_t from the binary data at the offset
953 /// pointed to by \a offset_ptr, and update the offset on success.
955 /// @param[in,out] offset_ptr
956 /// A pointer to an offset within the data that will be advanced
957 /// by the appropriate number of bytes if the value is extracted
958 /// correctly. If the offset is out of bounds or there are not
959 /// enough bytes to extract this value, the offset will be left
963 /// The extracted uint64_t value.
964 //------------------------------------------------------------------
965 uint64_t GetU64(lldb::offset_t *offset_ptr) const;
967 //------------------------------------------------------------------
968 /// Extract \a count uint64_t values from \a *offset_ptr.
970 /// Extract \a count uint64_t values from the binary data at the
971 /// offset pointed to by \a offset_ptr, and advance the offset on
972 /// success. The extracted values are copied into \a dst.
974 /// @param[in,out] offset_ptr
975 /// A pointer to an offset within the data that will be advanced
976 /// by the appropriate number of bytes if the value is extracted
977 /// correctly. If the offset is out of bounds or there are not
978 /// enough bytes to extract this value, the offset will be left
982 /// A buffer to copy \a count uint64_t values into. \a dst must
983 /// be large enough to hold all requested data.
986 /// The number of uint64_t values to extract.
989 /// \a dst if all values were properly extracted and copied,
990 /// nullptr otherwise.
991 //------------------------------------------------------------------
992 void *GetU64(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
994 //------------------------------------------------------------------
995 /// Extract a signed LEB128 value from \a *offset_ptr.
997 /// Extracts an signed LEB128 number from this object's data
998 /// starting at the offset pointed to by \a offset_ptr. The offset
999 /// pointed to by \a offset_ptr will be updated with the offset of
1000 /// the byte following the last extracted byte.
1002 /// @param[in,out] offset_ptr
1003 /// A pointer to an offset within the data that will be advanced
1004 /// by the appropriate number of bytes if the value is extracted
1005 /// correctly. If the offset is out of bounds or there are not
1006 /// enough bytes to extract this value, the offset will be left
1010 /// The extracted signed integer value.
1011 //------------------------------------------------------------------
1012 int64_t GetSLEB128(lldb::offset_t *offset_ptr) const;
1014 //------------------------------------------------------------------
1015 /// Extract a unsigned LEB128 value from \a *offset_ptr.
1017 /// Extracts an unsigned LEB128 number from this object's data
1018 /// starting at the offset pointed to by \a offset_ptr. The offset
1019 /// pointed to by \a offset_ptr will be updated with the offset of
1020 /// the byte following the last extracted byte.
1022 /// @param[in,out] offset_ptr
1023 /// A pointer to an offset within the data that will be advanced
1024 /// by the appropriate number of bytes if the value is extracted
1025 /// correctly. If the offset is out of bounds or there are not
1026 /// enough bytes to extract this value, the offset will be left
1030 /// The extracted unsigned integer value.
1031 //------------------------------------------------------------------
1032 uint64_t GetULEB128(lldb::offset_t *offset_ptr) const;
1034 lldb::DataBufferSP &GetSharedDataBuffer() { return m_data_sp; }
1036 //------------------------------------------------------------------
1037 /// Peek at a C string at \a offset.
1039 /// Peeks at a string in the contained data. No verification is done
1040 /// to make sure the entire string lies within the bounds of this
1041 /// object's data, only \a offset is verified to be a valid offset.
1043 /// @param[in] offset
1044 /// An offset into the data.
1047 /// A non-nullptr C string pointer if \a offset is a valid offset,
1048 /// nullptr otherwise.
1049 //------------------------------------------------------------------
1050 const char *PeekCStr(lldb::offset_t offset) const;
1052 //------------------------------------------------------------------
1053 /// Peek at a bytes at \a offset.
1055 /// Returns a pointer to \a length bytes at \a offset as long as
1056 /// there are \a length bytes available starting at \a offset.
1059 /// A non-nullptr data pointer if \a offset is a valid offset and
1060 /// there are \a length bytes available at that offset, nullptr
1062 //------------------------------------------------------------------
1063 const uint8_t *PeekData(lldb::offset_t offset, lldb::offset_t length) const {
1064 if (ValidOffsetForDataOfSize(offset, length))
1065 return m_start + offset;
1069 //------------------------------------------------------------------
1070 /// Set the address byte size.
1072 /// Set the size in bytes that will be used when extracting any
1073 /// address and pointer values from data contained in this object.
1075 /// @param[in] addr_size
1076 /// The size in bytes to use when extracting addresses.
1077 //------------------------------------------------------------------
1078 void SetAddressByteSize(uint32_t addr_size) {
1079 #ifdef LLDB_CONFIGURATION_DEBUG
1080 assert(addr_size == 4 || addr_size == 8);
1082 m_addr_size = addr_size;
1085 //------------------------------------------------------------------
1086 /// Set data with a buffer that is caller owned.
1088 /// Use data that is owned by the caller when extracting values.
1089 /// The data must stay around as long as this object, or any object
1090 /// that copies a subset of this object's data, is valid. If \a
1091 /// bytes is nullptr, or \a length is zero, this object will contain
1094 /// @param[in] bytes
1095 /// A pointer to caller owned data.
1097 /// @param[in] length
1098 /// The length in bytes of \a bytes.
1100 /// @param[in] byte_order
1101 /// A byte order of the data that we are extracting from.
1104 /// The number of bytes that this object now contains.
1105 //------------------------------------------------------------------
1106 lldb::offset_t SetData(const void *bytes, lldb::offset_t length,
1107 lldb::ByteOrder byte_order);
1109 //------------------------------------------------------------------
1110 /// Adopt a subset of \a data.
1112 /// Set this object's data to be a subset of the data bytes in \a
1113 /// data. If \a data contains shared data, then a reference to the
1114 /// shared data will be added to ensure the shared data stays around
1115 /// as long as any objects have references to the shared data. The
1116 /// byte order and the address size settings are copied from \a
1117 /// data. If \a offset is not a valid offset in \a data, then no
1118 /// reference to the shared data will be added. If there are not
1119 /// \a length bytes available in \a data starting at \a offset,
1120 /// the length will be truncated to contains as many bytes as
1124 /// Another DataExtractor object that contains data.
1126 /// @param[in] offset
1127 /// The offset into \a data at which the subset starts.
1129 /// @param[in] length
1130 /// The length in bytes of the subset of \a data.
1133 /// The number of bytes that this object now contains.
1134 //------------------------------------------------------------------
1135 lldb::offset_t SetData(const DataExtractor &data, lldb::offset_t offset,
1136 lldb::offset_t length);
1138 //------------------------------------------------------------------
1139 /// Adopt a subset of shared data in \a data_sp.
1141 /// Copies the data shared pointer which adds a reference to the
1142 /// contained in \a data_sp. The shared data reference is reference
1143 /// counted to ensure the data lives as long as anyone still has a
1144 /// valid shared pointer to the data in \a data_sp. The byte order
1145 /// and address byte size settings remain the same. If
1146 /// \a offset is not a valid offset in \a data_sp, then no reference
1147 /// to the shared data will be added. If there are not \a length
1148 /// bytes available in \a data starting at \a offset, the length
1149 /// will be truncated to contains as many bytes as possible.
1151 /// @param[in] data_sp
1152 /// A shared pointer to data.
1154 /// @param[in] offset
1155 /// The offset into \a data_sp at which the subset starts.
1157 /// @param[in] length
1158 /// The length in bytes of the subset of \a data_sp.
1161 /// The number of bytes that this object now contains.
1162 //------------------------------------------------------------------
1163 lldb::offset_t SetData(const lldb::DataBufferSP &data_sp,
1164 lldb::offset_t offset = 0,
1165 lldb::offset_t length = LLDB_INVALID_OFFSET);
1167 //------------------------------------------------------------------
1168 /// Set the byte_order value.
1170 /// Sets the byte order of the data to extract. Extracted values
1171 /// will be swapped if necessary when decoding.
1173 /// @param[in] byte_order
1174 /// The byte order value to use when extracting data.
1175 //------------------------------------------------------------------
1176 void SetByteOrder(lldb::ByteOrder byte_order) { m_byte_order = byte_order; }
1178 //------------------------------------------------------------------
1179 /// Skip an LEB128 number at \a *offset_ptr.
1181 /// Skips a LEB128 number (signed or unsigned) from this object's
1182 /// data starting at the offset pointed to by \a offset_ptr. The
1183 /// offset pointed to by \a offset_ptr will be updated with the
1184 /// offset of the byte following the last extracted byte.
1186 /// @param[in,out] offset_ptr
1187 /// A pointer to an offset within the data that will be advanced
1188 /// by the appropriate number of bytes if the value is extracted
1189 /// correctly. If the offset is out of bounds or there are not
1190 /// enough bytes to extract this value, the offset will be left
1194 // The number of bytes consumed during the extraction.
1195 //------------------------------------------------------------------
1196 uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const;
1198 //------------------------------------------------------------------
1199 /// Test the validity of \a offset.
1202 /// \b true if \a offset is a valid offset into the data in this
1203 /// object, \b false otherwise.
1204 //------------------------------------------------------------------
1205 bool ValidOffset(lldb::offset_t offset) const {
1206 return offset < GetByteSize();
1209 //------------------------------------------------------------------
1210 /// Test the availability of \a length bytes of data from \a offset.
1213 /// \b true if \a offset is a valid offset and there are \a
1214 /// length bytes available at that offset, \b false otherwise.
1215 //------------------------------------------------------------------
1216 bool ValidOffsetForDataOfSize(lldb::offset_t offset,
1217 lldb::offset_t length) const {
1218 return length <= BytesLeft(offset);
1221 size_t Copy(DataExtractor &dest_data) const;
1223 bool Append(DataExtractor &rhs);
1225 bool Append(void *bytes, lldb::offset_t length);
1227 lldb::offset_t BytesLeft(lldb::offset_t offset) const {
1228 const lldb::offset_t size = GetByteSize();
1230 return size - offset;
1234 void Checksum(llvm::SmallVectorImpl<uint8_t> &dest, uint64_t max_data = 0);
1237 //------------------------------------------------------------------
1239 //------------------------------------------------------------------
1240 const uint8_t *m_start; ///< A pointer to the first byte of data.
1242 *m_end; ///< A pointer to the byte that is past the end of the data.
1244 m_byte_order; ///< The byte order of the data we are extracting from.
1245 uint32_t m_addr_size; ///< The address size to use when extracting pointers or
1247 mutable lldb::DataBufferSP m_data_sp; ///< The shared pointer to data that can
1248 ///be shared among multiple instances
1249 const uint32_t m_target_byte_size;
1252 } // namespace lldb_private
1254 #endif // liblldb_DataExtractor_h_