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_
12 #if defined (__cplusplus)
15 #include "lldb/lldb-private.h"
17 #include "llvm/ADT/SmallVector.h"
24 namespace lldb_private {
26 //----------------------------------------------------------------------
27 /// @class DataExtractor DataExtractor.h "lldb/Core/DataExtractor.h"
28 /// @brief An data extractor class.
30 /// DataExtractor is a class that can extract data (swapping if needed)
31 /// from a data buffer. The data buffer can be caller owned, or can be
32 /// shared data that can be shared between multiple DataExtractor
33 /// instances. Multiple DataExtractor objects can share the same data,
34 /// yet extract values in different address sizes and byte order modes.
35 /// Each object can have a unique position in the shared data and extract
36 /// data from different offsets.
39 //----------------------------------------------------------------------
43 //------------------------------------------------------------------
44 /// @typedef DataExtractor::Type
45 /// @brief Type enumerations used in the dump routines.
46 /// @see DataExtractor::Dump()
47 /// @see DataExtractor::DumpRawHexBytes()
48 //------------------------------------------------------------------
51 TypeUInt8, ///< Format output as unsigned 8 bit integers
52 TypeChar, ///< Format output as characters
53 TypeUInt16, ///< Format output as unsigned 16 bit integers
54 TypeUInt32, ///< Format output as unsigned 32 bit integers
55 TypeUInt64, ///< Format output as unsigned 64 bit integers
56 TypePointer, ///< Format output as pointers
57 TypeULEB128, ///< Format output as ULEB128 numbers
58 TypeSLEB128 ///< Format output as SLEB128 numbers
62 DumpHexBytes (Stream *s,
65 uint32_t bytes_per_line,
66 lldb::addr_t base_addr); // Pass LLDB_INVALID_ADDRESS to not show address at start of line
67 //------------------------------------------------------------------
68 /// Default constructor.
70 /// Initialize all members to a default empty state.
71 //------------------------------------------------------------------
74 //------------------------------------------------------------------
75 /// Construct with a buffer that is owned by the caller.
77 /// This constructor allows us to use data that is owned by the
78 /// caller. The data must stay around as long as this object is
82 /// A pointer to caller owned data.
84 /// @param[in] data_length
85 /// The length in bytes of \a data.
87 /// @param[in] byte_order
88 /// A byte order of the data that we are extracting from.
90 /// @param[in] addr_size
91 /// A new address byte size value.
93 /// @param[in] target_byte_size
94 /// A size of a target byte in 8-bit host bytes
95 //------------------------------------------------------------------
96 DataExtractor (const void* data, lldb::offset_t data_length, lldb::ByteOrder byte_order, uint32_t addr_size, uint32_t target_byte_size = 1);
98 //------------------------------------------------------------------
99 /// Construct with shared data.
101 /// Copies the data shared pointer which adds a reference to the
102 /// contained in \a data_sp. The shared data reference is reference
103 /// counted to ensure the data lives as long as anyone still has a
104 /// valid shared pointer to the data in \a data_sp.
106 /// @param[in] data_sp
107 /// A shared pointer to data.
109 /// @param[in] byte_order
110 /// A byte order of the data that we are extracting from.
112 /// @param[in] addr_size
113 /// A new address byte size value.
115 /// @param[in] target_byte_size
116 /// A size of a target byte in 8-bit host bytes
117 //------------------------------------------------------------------
118 DataExtractor (const lldb::DataBufferSP& data_sp, lldb::ByteOrder byte_order, uint32_t addr_size, uint32_t target_byte_size = 1);
120 //------------------------------------------------------------------
121 /// Construct with a subset of \a data.
123 /// Initialize this object with a subset of the data bytes in \a
124 /// data. If \a data contains shared data, then a reference to the
125 /// shared data will be added to ensure the shared data stays around
126 /// as long as any objects have references to the shared data. The
127 /// byte order value and the address size settings are copied from \a
128 /// data. If \a offset is not a valid offset in \a data, then no
129 /// reference to the shared data will be added. If there are not
130 /// \a length bytes available in \a data starting at \a offset,
131 /// the length will be truncated to contain as many bytes as
135 /// Another DataExtractor object that contains data.
137 /// @param[in] offset
138 /// The offset into \a data at which the subset starts.
140 /// @param[in] length
141 /// The length in bytes of the subset of data.
143 /// @param[in] target_byte_size
144 /// A size of a target byte in 8-bit host bytes
145 //------------------------------------------------------------------
146 DataExtractor (const DataExtractor& data, lldb::offset_t offset, lldb::offset_t length, uint32_t target_byte_size = 1);
148 DataExtractor (const DataExtractor& rhs);
149 //------------------------------------------------------------------
150 /// Assignment operator.
152 /// Copies all data, byte order and address size settings from \a rhs into
153 /// this object. If \a rhs contains shared data, a reference to that
154 /// shared data will be added.
157 /// Another DataExtractor object to copy.
160 /// A const reference to this object.
161 //------------------------------------------------------------------
163 operator= (const DataExtractor& rhs);
165 //------------------------------------------------------------------
168 /// If this object contains a valid shared data reference, the
169 /// reference count on the data will be decremented, and if zero,
170 /// the data will be freed.
171 //------------------------------------------------------------------
174 //------------------------------------------------------------------
175 /// Clears the object state.
177 /// Clears the object contents back to a default invalid state, and
178 /// release any references to shared data that this object may
180 //------------------------------------------------------------------
184 //------------------------------------------------------------------
185 /// Dumps the binary data as \a type objects to stream \a s (or to
186 /// Log() if \a s is NULL) starting \a offset bytes into the data
187 /// and stopping after dumping \a length bytes. The offset into the
188 /// data is displayed at the beginning of each line and can be
189 /// offset by base address \a base_addr. \a num_per_line objects
190 /// will be displayed on each line.
193 /// The stream to dump the output to. If NULL the output will
194 /// be dumped to Log().
196 /// @param[in] offset
197 /// The offset into the data at which to start dumping.
199 /// @param[in] length
200 /// The number of bytes to dump.
202 /// @param[in] base_addr
203 /// The base address that gets added to the offset displayed on
206 /// @param[in] num_per_line
207 /// The number of \a type objects to display on each line.
210 /// The type of objects to use when dumping data from this
211 /// object. See DataExtractor::Type.
213 /// @param[in] type_format
214 /// The optional format to use for the \a type objects. If this
215 /// is NULL, the default format for the \a type will be used.
218 /// The offset at which dumping ended.
219 //------------------------------------------------------------------
222 lldb::offset_t offset,
223 lldb::offset_t length,
225 uint32_t num_per_line,
227 const char *type_format = NULL) const;
229 //------------------------------------------------------------------
230 /// Dumps \a item_count objects into the stream \a s.
232 /// Dumps \a item_count objects using \a item_format, each of which
233 /// are \a item_byte_size bytes long starting at offset \a offset
234 /// bytes into the contained data, into the stream \a s. \a
235 /// num_per_line objects will be dumped on each line before a new
236 /// line will be output. If \a base_addr is a valid address, then
237 /// each new line of output will be prededed by the address value
238 /// plus appropriate offset, and a colon and space. Bitfield values
239 /// can be dumped by calling this function multiple times with the
240 /// same start offset, format and size, yet differing \a
241 /// item_bit_size and \a item_bit_offset values.
244 /// The stream to dump the output to. This value can not be NULL.
246 /// @param[in] offset
247 /// The offset into the data at which to start dumping.
249 /// @param[in] item_format
250 /// The format to use when dumping each item.
252 /// @param[in] item_byte_size
253 /// The byte size of each item.
255 /// @param[in] item_count
256 /// The number of items to dump.
258 /// @param[in] num_per_line
259 /// The number of items to display on each line.
261 /// @param[in] base_addr
262 /// The base address that gets added to the offset displayed on
263 /// each line if the value is valid. Is \a base_addr is
264 /// LLDB_INVALID_ADDRESS then no address values will be prepended
267 /// @param[in] item_bit_size
268 /// If the value to display is a bitfield, this value should
269 /// be the number of bits that the bitfield item has within the
270 /// item's byte size value. This function will need to be called
271 /// multiple times with identical \a offset and \a item_byte_size
272 /// values in order to display multiple bitfield values that
273 /// exist within the same integer value. If the items being
274 /// displayed are not bitfields, this value should be zero.
276 /// @param[in] item_bit_offset
277 /// If the value to display is a bitfield, this value should
278 /// be the offset in bits, or shift right amount, that the
279 /// bitfield item occupies within the item's byte size value.
280 /// This function will need to be called multiple times with
281 /// identical \a offset and \a item_byte_size values in order
282 /// to display multiple bitfield values that exist within the
283 /// same integer value. If the items being displayed are not
284 /// bitfields, this value should be zero.
287 /// The offset at which dumping ended.
288 //------------------------------------------------------------------
291 lldb::offset_t offset,
292 lldb::Format item_format,
293 size_t item_byte_size,
297 uint32_t item_bit_size,
298 uint32_t item_bit_offset,
299 ExecutionContextScope *exe_scope = NULL) const;
301 //------------------------------------------------------------------
302 /// Dump a UUID value at \a offset.
304 /// Dump a UUID starting at \a offset bytes into this object's data.
305 /// If the stream \a s is NULL, the output will be sent to Log().
308 /// The stream to dump the output to. If NULL the output will
309 /// be dumped to Log().
311 /// @param[in] offset
312 /// The offset into the data at which to extract and dump a
314 //------------------------------------------------------------------
316 DumpUUID (Stream *s, lldb::offset_t offset) const;
318 //------------------------------------------------------------------
319 /// Extract an arbitrary number of bytes in the specified byte
322 /// Attemps to extract \a length bytes starting at \a offset bytes
323 /// into this data in the requested byte order (\a dst_byte_order)
324 /// and place the results in \a dst. \a dst must be at least \a
325 /// length bytes long.
327 /// @param[in] offset
328 /// The offset in bytes into the contained data at which to
329 /// start extracting.
331 /// @param[in] length
332 /// The number of bytes to extract.
334 /// @param[in] dst_byte_order
335 /// A byte order of the data that we want when the value in
336 /// copied to \a dst.
339 /// The buffer that will receive the extracted value if there
340 /// are enough bytes available in the current data.
343 /// The number of bytes that were extracted which will be \a
344 /// length when the value is successfully extracted, or zero
345 /// if there aren't enough bytes at the specified offset.
346 //------------------------------------------------------------------
348 ExtractBytes (lldb::offset_t offset, lldb::offset_t length, lldb::ByteOrder dst_byte_order, void *dst) const;
350 //------------------------------------------------------------------
351 /// Extract an address from \a *offset_ptr.
353 /// Extract a single address from the data and update the offset
354 /// pointed to by \a offset_ptr. The size of the extracted address
355 /// comes from the \a m_addr_size member variable and should be
356 /// set correctly prior to extracting any address values.
358 /// @param[in,out] offset_ptr
359 /// A pointer to an offset within the data that will be advanced
360 /// by the appropriate number of bytes if the value is extracted
361 /// correctly. If the offset is out of bounds or there are not
362 /// enough bytes to extract this value, the offset will be left
366 /// The extracted address value.
367 //------------------------------------------------------------------
369 GetAddress (lldb::offset_t *offset_ptr) const;
372 GetAddress_unchecked (lldb::offset_t *offset_ptr) const;
374 //------------------------------------------------------------------
375 /// Get the current address size.
377 /// Return the size in bytes of any address values this object will
381 /// The size in bytes of address values that will be extracted.
382 //------------------------------------------------------------------
384 GetAddressByteSize () const
389 //------------------------------------------------------------------
390 /// Get the number of bytes contained in this object.
393 /// The total number of bytes of data this object refers to.
394 //------------------------------------------------------------------
398 return m_end - m_start;
401 //------------------------------------------------------------------
402 /// Extract a C string from \a *offset_ptr.
404 /// Returns a pointer to a C String from the data at the offset
405 /// pointed to by \a offset_ptr. A variable length NULL terminated C
406 /// string will be extracted and the \a offset_ptr will be
407 /// updated with the offset of the byte that follows the NULL
410 /// @param[in,out] offset_ptr
411 /// A pointer to an offset within the data that will be advanced
412 /// by the appropriate number of bytes if the value is extracted
413 /// correctly. If the offset is out of bounds or there are not
414 /// enough bytes to extract this value, the offset will be left
418 /// A pointer to the C string value in the data. If the offset
419 /// pointed to by \a offset_ptr is out of bounds, or if the
420 /// offset plus the length of the C string is out of bounds,
421 /// NULL will be returned.
422 //------------------------------------------------------------------
424 GetCStr (lldb::offset_t *offset_ptr) const;
426 //------------------------------------------------------------------
427 /// Extract a C string from \a *offset_ptr with field size \a len.
429 /// Returns a pointer to a C String from the data at the offset
430 /// pointed to by \a offset_ptr, with a field length of \a len.
431 /// A NULL terminated C string will be extracted and the \a offset_ptr
432 /// will be updated with the offset of the byte that follows the fixed
435 /// @param[in,out] offset_ptr
436 /// A pointer to an offset within the data that will be advanced
437 /// by the appropriate number of bytes if the value is extracted
438 /// correctly. If the offset is out of bounds or there are not
439 /// enough bytes to extract this value, the offset will be left
443 /// A pointer to the C string value in the data. If the offset
444 /// pointed to by \a offset_ptr is out of bounds, or if the
445 /// offset plus the length of the field is out of bounds, or if
446 /// the field does not contain a NULL terminator byte, NULL will
449 GetCStr (lldb::offset_t *offset_ptr, lldb::offset_t len) const;
451 //------------------------------------------------------------------
452 /// Extract \a length bytes from \a *offset_ptr.
454 /// Returns a pointer to a bytes in this object's data at the offset
455 /// pointed to by \a offset_ptr. If \a length is zero or too large,
456 /// then the offset pointed to by \a offset_ptr will not be updated
457 /// and NULL will be returned.
459 /// @param[in,out] offset_ptr
460 /// A pointer to an offset within the data that will be advanced
461 /// by the appropriate number of bytes if the value is extracted
462 /// correctly. If the offset is out of bounds or there are not
463 /// enough bytes to extract this value, the offset will be left
466 /// @param[in] length
467 /// The optional length of a string to extract. If the value is
468 /// zero, a NULL terminated C string will be extracted.
471 /// A pointer to the bytes in this object's data if the offset
472 /// and length are valid, or NULL otherwise.
473 //------------------------------------------------------------------
475 GetData (lldb::offset_t *offset_ptr, lldb::offset_t length) const
477 const uint8_t *ptr = PeekData (*offset_ptr, length);
479 *offset_ptr += length;
483 //------------------------------------------------------------------
484 /// Copy \a length bytes from \a *offset, without swapping bytes.
486 /// @param[in] offset
487 /// The offset into this data from which to start copying
489 /// @param[in] length
490 /// The length of the data to copy from this object
493 /// The buffer to place the output data.
496 /// Returns the number of bytes that were copied, or zero if
497 /// anything goes wrong.
498 //------------------------------------------------------------------
500 CopyData (lldb::offset_t offset,
501 lldb::offset_t length,
504 //------------------------------------------------------------------
505 /// Copy \a dst_len bytes from \a *offset_ptr and ensure the copied
506 /// data is treated as a value that can be swapped to match the
507 /// specified byte order.
509 /// For values that are larger than the supported integer sizes,
510 /// this function can be used to extract data in a specified byte
511 /// order. It can also be used to copy a smaller integer value from
512 /// to a larger value. The extra bytes left over will be padded
513 /// correctly according to the byte order of this object and the
514 /// \a dst_byte_order. This can be very handy when say copying a
515 /// partial data value into a register.
517 /// @param[in] src_offset
518 /// The offset into this data from which to start copying an
521 /// @param[in] src_len
522 /// The length of the endian data to copy from this object
523 /// into the \a dst object
526 /// The buffer where to place the endian data. The data might
527 /// need to be byte swapped (and appropriately padded with
528 /// zeroes if \a src_len != \a dst_len) if \a dst_byte_order
529 /// does not match the byte order in this object.
531 /// @param[in] dst_len
532 /// The length number of bytes that the endian value will
533 /// occupy is \a dst.
535 /// @param[in] byte_order
536 /// The byte order that the endian value should be in the \a dst
540 /// Returns the number of bytes that were copied, or zero if
541 /// anything goes wrong.
542 //------------------------------------------------------------------
544 CopyByteOrderedData (lldb::offset_t src_offset,
545 lldb::offset_t src_len,
547 lldb::offset_t dst_len,
548 lldb::ByteOrder dst_byte_order) const;
550 //------------------------------------------------------------------
551 /// Get the data end pointer.
554 /// Returns a pointer to the next byte contained in this
555 /// object's data, or NULL of there is no data in this object.
556 //------------------------------------------------------------------
563 //------------------------------------------------------------------
564 /// Get the shared data offset.
566 /// Get the offset of the first byte of data in the shared data (if
570 /// If this object contains shared data, this function returns
571 /// the offset in bytes into that shared data, zero otherwise.
572 //------------------------------------------------------------------
574 GetSharedDataOffset () const;
576 //------------------------------------------------------------------
577 /// Get a the data start pointer.
580 /// Returns a pointer to the first byte contained in this
581 /// object's data, or NULL of there is no data in this object.
582 //------------------------------------------------------------------
584 GetDataStart () const
590 //------------------------------------------------------------------
591 /// Extract a float from \a *offset_ptr.
593 /// Extract a single float value.
595 /// @param[in,out] offset_ptr
596 /// A pointer to an offset within the data that will be advanced
597 /// by the appropriate number of bytes if the value is extracted
598 /// correctly. If the offset is out of bounds or there are not
599 /// enough bytes to extract this value, the offset will be left
603 /// The floating value that was extracted, or zero on failure.
604 //------------------------------------------------------------------
606 GetFloat (lldb::offset_t *offset_ptr) const;
609 GetDouble (lldb::offset_t *offset_ptr) const;
612 GetLongDouble (lldb::offset_t *offset_ptr) const;
614 //------------------------------------------------------------------
615 /// Extract a GNU encoded pointer value from \a *offset_ptr.
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] eh_ptr_enc
625 /// The GNU pointer encoding type.
627 /// @param[in] pc_rel_addr
628 /// The PC relative address to use when the encoding is
629 /// \c DW_GNU_EH_PE_pcrel.
631 /// @param[in] text_addr
632 /// The text (code) relative address to use when the encoding is
633 /// \c DW_GNU_EH_PE_textrel.
635 /// @param[in] data_addr
636 /// The data relative address to use when the encoding is
637 /// \c DW_GNU_EH_PE_datarel.
640 /// The extracted GNU encoded pointer value.
641 //------------------------------------------------------------------
643 GetGNUEHPointer (lldb::offset_t *offset_ptr,
645 lldb::addr_t pc_rel_addr,
646 lldb::addr_t text_addr,
647 lldb::addr_t data_addr);
649 //------------------------------------------------------------------
650 /// Extract an integer of size \a byte_size from \a *offset_ptr.
652 /// Extract a single integer value and update the offset pointed to
653 /// by \a offset_ptr. The size of the extracted integer is specified
654 /// by the \a byte_size argument. \a byte_size should have a value
655 /// >= 1 and <= 4 since the return value is only 32 bits wide. Any
656 /// \a byte_size values less than 1 or greater than 4 will result in
657 /// nothing being extracted, and zero being returned.
659 /// @param[in,out] offset_ptr
660 /// A pointer to an offset within the data that will be advanced
661 /// by the appropriate number of bytes if the value is extracted
662 /// correctly. If the offset is out of bounds or there are not
663 /// enough bytes to extract this value, the offset will be left
666 /// @param[in] byte_size
667 /// The size in byte of the integer to extract.
670 /// The integer value that was extracted, or zero on failure.
671 //------------------------------------------------------------------
673 GetMaxU32 (lldb::offset_t *offset_ptr, size_t byte_size) const;
675 //------------------------------------------------------------------
676 /// Extract an unsigned integer of size \a byte_size from \a
679 /// Extract a single unsigned integer value and update the offset
680 /// pointed to by \a offset_ptr. The size of the extracted integer
681 /// is specified by the \a byte_size argument. \a byte_size should
682 /// have a value greater than or equal to one and less than or equal
683 /// to eight since the return value is 64 bits wide. Any
684 /// \a byte_size values less than 1 or greater than 8 will result in
685 /// nothing being extracted, and zero being returned.
687 /// @param[in,out] offset_ptr
688 /// A pointer to an offset within the data that will be advanced
689 /// by the appropriate number of bytes if the value is extracted
690 /// correctly. If the offset is out of bounds or there are not
691 /// enough bytes to extract this value, the offset will be left
694 /// @param[in] byte_size
695 /// The size in byte of the integer to extract.
698 /// The unsigned integer value that was extracted, or zero on
700 //------------------------------------------------------------------
702 GetMaxU64 (lldb::offset_t *offset_ptr, size_t byte_size) const;
705 GetMaxU64_unchecked (lldb::offset_t *offset_ptr, size_t byte_size) const;
707 //------------------------------------------------------------------
708 /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
710 /// Extract a single signed integer value (sign extending if required)
711 /// and update the offset pointed to by \a offset_ptr. The size of
712 /// the extracted integer is specified by the \a byte_size argument.
713 /// \a byte_size should have a value greater than or equal to one
714 /// and less than or equal to eight since the return value is 64
715 /// bits wide. Any \a byte_size values less than 1 or greater than
716 /// 8 will result in nothing being extracted, and zero being returned.
718 /// @param[in,out] offset_ptr
719 /// A pointer to an offset within the data that will be advanced
720 /// by the appropriate number of bytes if the value is extracted
721 /// correctly. If the offset is out of bounds or there are not
722 /// enough bytes to extract this value, the offset will be left
725 /// @param[in] byte_size
726 /// The size in byte of the integer to extract.
729 /// The sign extended signed integer value that was extracted,
730 /// or zero on failure.
731 //------------------------------------------------------------------
733 GetMaxS64 (lldb::offset_t *offset_ptr, size_t size) const;
735 //------------------------------------------------------------------
736 /// Extract an unsigned integer of size \a byte_size from \a
737 /// *offset_ptr, then extract the bitfield from this value if
738 /// \a bitfield_bit_size is non-zero.
740 /// Extract a single unsigned integer value and update the offset
741 /// pointed to by \a offset_ptr. The size of the extracted integer
742 /// is specified by the \a byte_size argument. \a byte_size should
743 /// have a value greater than or equal to one and less than or equal
744 /// to 8 since the return value is 64 bits wide. Any
745 /// \a byte_size values less than 1 or greater than 8 will result in
746 /// nothing being extracted, and zero being returned.
748 /// @param[in,out] offset_ptr
749 /// A pointer to an offset within the data that will be advanced
750 /// by the appropriate number of bytes if the value is extracted
751 /// correctly. If the offset is out of bounds or there are not
752 /// enough bytes to extract this value, the offset will be left
755 /// @param[in] byte_size
756 /// The size in byte of the integer to extract.
758 /// @param[in] bitfield_bit_size
759 /// The size in bits of the bitfield value to extract, or zero
760 /// to just extract the entire integer value.
762 /// @param[in] bitfield_bit_offset
763 /// The bit offset of the bitfield value in the extracted
764 /// integer (the number of bits to shift the integer to the
768 /// The unsigned bitfield integer value that was extracted, or
770 //------------------------------------------------------------------
772 GetMaxU64Bitfield (lldb::offset_t *offset_ptr,
774 uint32_t bitfield_bit_size,
775 uint32_t bitfield_bit_offset) const;
777 //------------------------------------------------------------------
778 /// Extract an signed integer of size \a byte_size from \a
779 /// *offset_ptr, then extract and signe extend the bitfield from
780 /// this value if \a bitfield_bit_size is non-zero.
782 /// Extract a single signed integer value (sign extending if required)
783 /// and update the offset pointed to by \a offset_ptr. The size of
784 /// the extracted integer is specified by the \a byte_size argument.
785 /// \a byte_size should have a value greater than or equal to one
786 /// and less than or equal to eight since the return value is 64
787 /// bits wide. Any \a byte_size values less than 1 or greater than
788 /// 8 will result in nothing being extracted, and zero being returned.
790 /// @param[in,out] offset_ptr
791 /// A pointer to an offset within the data that will be advanced
792 /// by the appropriate number of bytes if the value is extracted
793 /// correctly. If the offset is out of bounds or there are not
794 /// enough bytes to extract this value, the offset will be left
797 /// @param[in] byte_size
798 /// The size in bytes of the integer to extract.
800 /// @param[in] bitfield_bit_size
801 /// The size in bits of the bitfield value to extract, or zero
802 /// to just extract the entire integer value.
804 /// @param[in] bitfield_bit_offset
805 /// The bit offset of the bitfield value in the extracted
806 /// integer (the number of bits to shift the integer to the
810 /// The signed bitfield integer value that was extracted, or
812 //------------------------------------------------------------------
814 GetMaxS64Bitfield (lldb::offset_t *offset_ptr,
816 uint32_t bitfield_bit_size,
817 uint32_t bitfield_bit_offset) const;
819 //------------------------------------------------------------------
820 /// Extract an pointer from \a *offset_ptr.
822 /// Extract a single pointer from the data and update the offset
823 /// pointed to by \a offset_ptr. The size of the extracted pointer
824 /// comes from the \a m_addr_size member variable and should be
825 /// set correctly prior to extracting any pointer values.
827 /// @param[in,out] offset_ptr
828 /// A pointer to an offset within the data that will be advanced
829 /// by the appropriate number of bytes if the value is extracted
830 /// correctly. If the offset is out of bounds or there are not
831 /// enough bytes to extract this value, the offset will be left
835 /// The extracted pointer value as a 64 integer.
836 //------------------------------------------------------------------
838 GetPointer (lldb::offset_t *offset_ptr) const;
840 //------------------------------------------------------------------
841 /// Get the current byte order value.
844 /// The current byte order value from this object's internal
846 //------------------------------------------------------------------
853 //------------------------------------------------------------------
854 /// Extract a uint8_t value from \a *offset_ptr.
856 /// Extract a single uint8_t from the binary data at the offset
857 /// pointed to by \a offset_ptr, and advance the offset on success.
859 /// @param[in,out] offset_ptr
860 /// A pointer to an offset within the data that will be advanced
861 /// by the appropriate number of bytes if the value is extracted
862 /// correctly. If the offset is out of bounds or there are not
863 /// enough bytes to extract this value, the offset will be left
867 /// The extracted uint8_t value.
868 //------------------------------------------------------------------
870 GetU8 ( lldb::offset_t *offset_ptr) const;
873 GetU8_unchecked (lldb::offset_t *offset_ptr) const
875 uint8_t val = m_start[*offset_ptr];
881 GetU16_unchecked (lldb::offset_t *offset_ptr) const;
884 GetU32_unchecked (lldb::offset_t *offset_ptr) const;
887 GetU64_unchecked (lldb::offset_t *offset_ptr) const;
888 //------------------------------------------------------------------
889 /// Extract \a count uint8_t values from \a *offset_ptr.
891 /// Extract \a count uint8_t values from the binary data at the
892 /// offset pointed to by \a offset_ptr, and advance the offset on
893 /// success. The extracted values are copied into \a dst.
895 /// @param[in,out] offset_ptr
896 /// A pointer to an offset within the data that will be advanced
897 /// by the appropriate number of bytes if the value is extracted
898 /// correctly. If the offset is out of bounds or there are not
899 /// enough bytes to extract this value, the offset will be left
903 /// A buffer to copy \a count uint8_t values into. \a dst must
904 /// be large enough to hold all requested data.
907 /// The number of uint8_t values to extract.
910 /// \a dst if all values were properly extracted and copied,
912 //------------------------------------------------------------------
914 GetU8 (lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
916 //------------------------------------------------------------------
917 /// Extract a uint16_t value from \a *offset_ptr.
919 /// Extract a single uint16_t from the binary data at the offset
920 /// pointed to by \a offset_ptr, and update the offset on success.
922 /// @param[in,out] offset_ptr
923 /// A pointer to an offset within the data that will be advanced
924 /// by the appropriate number of bytes if the value is extracted
925 /// correctly. If the offset is out of bounds or there are not
926 /// enough bytes to extract this value, the offset will be left
930 /// The extracted uint16_t value.
931 //------------------------------------------------------------------
933 GetU16 (lldb::offset_t *offset_ptr) const;
935 //------------------------------------------------------------------
936 /// Extract \a count uint16_t values from \a *offset_ptr.
938 /// Extract \a count uint16_t values from the binary data at the
939 /// offset pointed to by \a offset_ptr, and advance the offset on
940 /// success. The extracted values are copied into \a dst.
942 /// @param[in,out] offset_ptr
943 /// A pointer to an offset within the data that will be advanced
944 /// by the appropriate number of bytes if the value is extracted
945 /// correctly. If the offset is out of bounds or there are not
946 /// enough bytes to extract this value, the offset will be left
950 /// A buffer to copy \a count uint16_t values into. \a dst must
951 /// be large enough to hold all requested data.
954 /// The number of uint16_t values to extract.
957 /// \a dst if all values were properly extracted and copied,
959 //------------------------------------------------------------------
961 GetU16 (lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
963 //------------------------------------------------------------------
964 /// Extract a uint32_t value from \a *offset_ptr.
966 /// Extract a single uint32_t from the binary data at the offset
967 /// pointed to by \a offset_ptr, and update the offset on success.
969 /// @param[in,out] offset_ptr
970 /// A pointer to an offset within the data that will be advanced
971 /// by the appropriate number of bytes if the value is extracted
972 /// correctly. If the offset is out of bounds or there are not
973 /// enough bytes to extract this value, the offset will be left
977 /// The extracted uint32_t value.
978 //------------------------------------------------------------------
980 GetU32 (lldb::offset_t *offset_ptr) const;
982 //------------------------------------------------------------------
983 /// Extract \a count uint32_t values from \a *offset_ptr.
985 /// Extract \a count uint32_t values from the binary data at the
986 /// offset pointed to by \a offset_ptr, and advance the offset on
987 /// success. The extracted values are copied into \a dst.
989 /// @param[in,out] offset_ptr
990 /// A pointer to an offset within the data that will be advanced
991 /// by the appropriate number of bytes if the value is extracted
992 /// correctly. If the offset is out of bounds or there are not
993 /// enough bytes to extract this value, the offset will be left
997 /// A buffer to copy \a count uint32_t values into. \a dst must
998 /// be large enough to hold all requested data.
1000 /// @param[in] count
1001 /// The number of uint32_t values to extract.
1004 /// \a dst if all values were properly extracted and copied,
1006 //------------------------------------------------------------------
1008 GetU32 (lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
1010 //------------------------------------------------------------------
1011 /// Extract a uint64_t value from \a *offset_ptr.
1013 /// Extract a single uint64_t from the binary data at the offset
1014 /// pointed to by \a offset_ptr, and update the offset on success.
1016 /// @param[in,out] offset_ptr
1017 /// A pointer to an offset within the data that will be advanced
1018 /// by the appropriate number of bytes if the value is extracted
1019 /// correctly. If the offset is out of bounds or there are not
1020 /// enough bytes to extract this value, the offset will be left
1024 /// The extracted uint64_t value.
1025 //------------------------------------------------------------------
1027 GetU64 (lldb::offset_t *offset_ptr) const;
1029 //------------------------------------------------------------------
1030 /// Extract \a count uint64_t values from \a *offset_ptr.
1032 /// Extract \a count uint64_t values from the binary data at the
1033 /// offset pointed to by \a offset_ptr, and advance the offset on
1034 /// success. The extracted values are copied into \a dst.
1036 /// @param[in,out] offset_ptr
1037 /// A pointer to an offset within the data that will be advanced
1038 /// by the appropriate number of bytes if the value is extracted
1039 /// correctly. If the offset is out of bounds or there are not
1040 /// enough bytes to extract this value, the offset will be left
1044 /// A buffer to copy \a count uint64_t values into. \a dst must
1045 /// be large enough to hold all requested data.
1047 /// @param[in] count
1048 /// The number of uint64_t values to extract.
1051 /// \a dst if all values were properly extracted and copied,
1053 //------------------------------------------------------------------
1055 GetU64 ( lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
1057 //------------------------------------------------------------------
1058 /// Extract a signed LEB128 value from \a *offset_ptr.
1060 /// Extracts an signed LEB128 number from this object's data
1061 /// starting at the offset pointed to by \a offset_ptr. The offset
1062 /// pointed to by \a offset_ptr will be updated with the offset of
1063 /// the byte following the last extracted byte.
1065 /// @param[in,out] offset_ptr
1066 /// A pointer to an offset within the data that will be advanced
1067 /// by the appropriate number of bytes if the value is extracted
1068 /// correctly. If the offset is out of bounds or there are not
1069 /// enough bytes to extract this value, the offset will be left
1073 /// The extracted signed integer value.
1074 //------------------------------------------------------------------
1076 GetSLEB128 (lldb::offset_t *offset_ptr) const;
1078 //------------------------------------------------------------------
1079 /// Extract a unsigned LEB128 value from \a *offset_ptr.
1081 /// Extracts an unsigned LEB128 number from this object's data
1082 /// starting at the offset pointed to by \a offset_ptr. The offset
1083 /// pointed to by \a offset_ptr will be updated with the offset of
1084 /// the byte following the last extracted byte.
1086 /// @param[in,out] offset_ptr
1087 /// A pointer to an offset within the data that will be advanced
1088 /// by the appropriate number of bytes if the value is extracted
1089 /// correctly. If the offset is out of bounds or there are not
1090 /// enough bytes to extract this value, the offset will be left
1094 /// The extracted unsigned integer value.
1095 //------------------------------------------------------------------
1097 GetULEB128 (lldb::offset_t *offset_ptr) const;
1099 lldb::DataBufferSP &
1100 GetSharedDataBuffer ()
1105 //------------------------------------------------------------------
1106 /// Peek at a C string at \a offset.
1108 /// Peeks at a string in the contained data. No verification is done
1109 /// to make sure the entire string lies within the bounds of this
1110 /// object's data, only \a offset is verified to be a valid offset.
1112 /// @param[in] offset
1113 /// An offset into the data.
1116 /// A non-NULL C string pointer if \a offset is a valid offset,
1118 //------------------------------------------------------------------
1120 PeekCStr (lldb::offset_t offset) const;
1122 //------------------------------------------------------------------
1123 /// Peek at a bytes at \a offset.
1125 /// Returns a pointer to \a length bytes at \a offset as long as
1126 /// there are \a length bytes available starting at \a offset.
1129 /// A non-NULL data pointer if \a offset is a valid offset and
1130 /// there are \a length bytes available at that offset, NULL
1132 //------------------------------------------------------------------
1134 PeekData (lldb::offset_t offset, lldb::offset_t length) const
1136 if (length > 0 && ValidOffsetForDataOfSize(offset, length))
1137 return m_start + offset;
1141 //------------------------------------------------------------------
1142 /// Set the address byte size.
1144 /// Set the size in bytes that will be used when extracting any
1145 /// address and pointer values from data contained in this object.
1147 /// @param[in] addr_size
1148 /// The size in bytes to use when extracting addresses.
1149 //------------------------------------------------------------------
1151 SetAddressByteSize (uint32_t addr_size)
1153 m_addr_size = addr_size;
1156 //------------------------------------------------------------------
1157 /// Set data with a buffer that is caller owned.
1159 /// Use data that is owned by the caller when extracting values.
1160 /// The data must stay around as long as this object, or any object
1161 /// that copies a subset of this object's data, is valid. If \a
1162 /// bytes is NULL, or \a length is zero, this object will contain
1165 /// @param[in] bytes
1166 /// A pointer to caller owned data.
1168 /// @param[in] length
1169 /// The length in bytes of \a bytes.
1171 /// @param[in] byte_order
1172 /// A byte order of the data that we are extracting from.
1175 /// The number of bytes that this object now contains.
1176 //------------------------------------------------------------------
1178 SetData (const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order);
1180 //------------------------------------------------------------------
1181 /// Adopt a subset of \a data.
1183 /// Set this object's data to be a subset of the data bytes in \a
1184 /// data. If \a data contains shared data, then a reference to the
1185 /// shared data will be added to ensure the shared data stays around
1186 /// as long as any objects have references to the shared data. The
1187 /// byte order and the address size settings are copied from \a
1188 /// data. If \a offset is not a valid offset in \a data, then no
1189 /// reference to the shared data will be added. If there are not
1190 /// \a length bytes available in \a data starting at \a offset,
1191 /// the length will be truncated to contains as many bytes as
1195 /// Another DataExtractor object that contains data.
1197 /// @param[in] offset
1198 /// The offset into \a data at which the subset starts.
1200 /// @param[in] length
1201 /// The length in bytes of the subset of \a data.
1204 /// The number of bytes that this object now contains.
1205 //------------------------------------------------------------------
1207 SetData (const DataExtractor& data, lldb::offset_t offset, lldb::offset_t length);
1209 //------------------------------------------------------------------
1210 /// Adopt a subset of shared data in \a data_sp.
1212 /// Copies the data shared pointer which adds a reference to the
1213 /// contained in \a data_sp. The shared data reference is reference
1214 /// counted to ensure the data lives as long as anyone still has a
1215 /// valid shared pointer to the data in \a data_sp. The byte order
1216 /// and address byte size settings remain the same. If
1217 /// \a offset is not a valid offset in \a data_sp, then no reference
1218 /// to the shared data will be added. If there are not \a length
1219 /// bytes available in \a data starting at \a offset, the length
1220 /// will be truncated to contains as many bytes as possible.
1222 /// @param[in] data_sp
1223 /// A shared pointer to data.
1225 /// @param[in] offset
1226 /// The offset into \a data_sp at which the subset starts.
1228 /// @param[in] length
1229 /// The length in bytes of the subset of \a data_sp.
1232 /// The number of bytes that this object now contains.
1233 //------------------------------------------------------------------
1235 SetData (const lldb::DataBufferSP& data_sp, lldb::offset_t offset = 0, lldb::offset_t length = LLDB_INVALID_OFFSET);
1237 //------------------------------------------------------------------
1238 /// Set the byte_order value.
1240 /// Sets the byte order of the data to extract. Extracted values
1241 /// will be swapped if necessary when decoding.
1243 /// @param[in] byte_order
1244 /// The byte order value to use when extracting data.
1245 //------------------------------------------------------------------
1247 SetByteOrder (lldb::ByteOrder byte_order)
1249 m_byte_order = byte_order;
1252 //------------------------------------------------------------------
1253 /// Skip an LEB128 number at \a *offset_ptr.
1255 /// Skips a LEB128 number (signed or unsigned) from this object's
1256 /// data starting at the offset pointed to by \a offset_ptr. The
1257 /// offset pointed to by \a offset_ptr will be updated with the
1258 /// offset of the byte following the last extracted byte.
1260 /// @param[in,out] offset_ptr
1261 /// A pointer to an offset within the data that will be advanced
1262 /// by the appropriate number of bytes if the value is extracted
1263 /// correctly. If the offset is out of bounds or there are not
1264 /// enough bytes to extract this value, the offset will be left
1268 // The number of bytes consumed during the extraction.
1269 //------------------------------------------------------------------
1271 Skip_LEB128 (lldb::offset_t *offset_ptr) const;
1273 //------------------------------------------------------------------
1274 /// Test the validity of \a offset.
1277 /// \b true if \a offset is a valid offset into the data in this
1278 /// object, \b false otherwise.
1279 //------------------------------------------------------------------
1281 ValidOffset (lldb::offset_t offset) const
1283 return offset < GetByteSize();
1286 //------------------------------------------------------------------
1287 /// Test the availability of \a length bytes of data from \a offset.
1290 /// \b true if \a offset is a valid offset and there are \a
1291 /// length bytes available at that offset, \b false otherwise.
1292 //------------------------------------------------------------------
1294 ValidOffsetForDataOfSize (lldb::offset_t offset, lldb::offset_t length) const
1296 return length <= BytesLeft (offset);
1300 Copy (DataExtractor& dest_data) const;
1303 Append (DataExtractor& rhs);
1306 Append (void* bytes, lldb::offset_t length);
1309 BytesLeft (lldb::offset_t offset) const
1311 const lldb::offset_t size = GetByteSize();
1313 return size - offset;
1318 Checksum (llvm::SmallVectorImpl<uint8_t> &dest,
1319 uint64_t max_data = 0);
1324 //------------------------------------------------------------------
1326 //------------------------------------------------------------------
1327 const uint8_t * m_start; ///< A pointer to the first byte of data.
1328 const uint8_t * m_end; ///< A pointer to the byte that is past the end of the data.
1329 lldb::ByteOrder m_byte_order; ///< The byte order of the data we are extracting from.
1330 uint32_t m_addr_size; ///< The address size to use when extracting pointers or addresses
1331 mutable lldb::DataBufferSP m_data_sp; ///< The shared pointer to data that can be shared among multilple instances
1332 const uint32_t m_target_byte_size;
1335 } // namespace lldb_private
1337 #endif // #if defined (__cplusplus)
1338 #endif // #ifndef liblldb_DataExtractor_h_