|
|
|||
File indexing completed on 2026-05-10 08:42:57
0001 //===-- DataExtractor.h -----------------------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 0009 #ifndef LLDB_UTILITY_DATAEXTRACTOR_H 0010 #define LLDB_UTILITY_DATAEXTRACTOR_H 0011 0012 #include "lldb/Utility/Endian.h" 0013 #include "lldb/lldb-defines.h" 0014 #include "lldb/lldb-enumerations.h" 0015 #include "lldb/lldb-forward.h" 0016 #include "lldb/lldb-types.h" 0017 #include "llvm/ADT/ArrayRef.h" 0018 #include "llvm/Support/DataExtractor.h" 0019 #include "llvm/Support/SwapByteOrder.h" 0020 0021 #include <cassert> 0022 #include <cstdint> 0023 #include <cstring> 0024 0025 namespace lldb_private { 0026 class Log; 0027 class Stream; 0028 } 0029 namespace llvm { 0030 template <typename T> class SmallVectorImpl; 0031 } 0032 0033 0034 namespace lldb_private { 0035 0036 /// \class DataExtractor DataExtractor.h "lldb/Core/DataExtractor.h" An data 0037 /// extractor class. 0038 /// 0039 /// DataExtractor is a class that can extract data (swapping if needed) from a 0040 /// data buffer. The data buffer can be caller owned, or can be shared data 0041 /// that can be shared between multiple DataExtractor instances. Multiple 0042 /// DataExtractor objects can share the same data, yet extract values in 0043 /// different address sizes and byte order modes. Each object can have a 0044 /// unique position in the shared data and extract data from different 0045 /// offsets. 0046 /// 0047 /// \see DataBuffer 0048 class DataExtractor { 0049 public: 0050 /// \typedef DataExtractor::Type 0051 /// Type enumerations used in the dump routines. 0052 enum Type { 0053 TypeUInt8, ///< Format output as unsigned 8 bit integers 0054 TypeChar, ///< Format output as characters 0055 TypeUInt16, ///< Format output as unsigned 16 bit integers 0056 TypeUInt32, ///< Format output as unsigned 32 bit integers 0057 TypeUInt64, ///< Format output as unsigned 64 bit integers 0058 TypePointer, ///< Format output as pointers 0059 TypeULEB128, ///< Format output as ULEB128 numbers 0060 TypeSLEB128 ///< Format output as SLEB128 numbers 0061 }; 0062 0063 /// Default constructor. 0064 /// 0065 /// Initialize all members to a default empty state. 0066 DataExtractor(); 0067 0068 /// Construct with a buffer that is owned by the caller. 0069 /// 0070 /// This constructor allows us to use data that is owned by the caller. The 0071 /// data must stay around as long as this object is valid. 0072 /// 0073 /// \param[in] data 0074 /// A pointer to caller owned data. 0075 /// 0076 /// \param[in] data_length 0077 /// The length in bytes of \a data. 0078 /// 0079 /// \param[in] byte_order 0080 /// A byte order of the data that we are extracting from. 0081 /// 0082 /// \param[in] addr_size 0083 /// A new address byte size value. 0084 /// 0085 /// \param[in] target_byte_size 0086 /// A size of a target byte in 8-bit host bytes 0087 DataExtractor(const void *data, lldb::offset_t data_length, 0088 lldb::ByteOrder byte_order, uint32_t addr_size, 0089 uint32_t target_byte_size = 1); 0090 0091 /// Construct with shared data. 0092 /// 0093 /// Copies the data shared pointer which adds a reference to the contained 0094 /// in \a data_sp. The shared data reference is reference counted to ensure 0095 /// the data lives as long as anyone still has a valid shared pointer to the 0096 /// data in \a data_sp. 0097 /// 0098 /// \param[in] data_sp 0099 /// A shared pointer to data. 0100 /// 0101 /// \param[in] byte_order 0102 /// A byte order of the data that we are extracting from. 0103 /// 0104 /// \param[in] addr_size 0105 /// A new address byte size value. 0106 /// 0107 /// \param[in] target_byte_size 0108 /// A size of a target byte in 8-bit host bytes 0109 DataExtractor(const lldb::DataBufferSP &data_sp, lldb::ByteOrder byte_order, 0110 uint32_t addr_size, uint32_t target_byte_size = 1); 0111 0112 /// Construct with a subset of \a data. 0113 /// 0114 /// Initialize this object with a subset of the data bytes in \a data. If \a 0115 /// data contains shared data, then a reference to the shared data will be 0116 /// added to ensure the shared data stays around as long as any objects have 0117 /// references to the shared data. The byte order value and the address size 0118 /// settings are copied from \a data. If \a offset is not a valid offset in 0119 /// \a data, then no reference to the shared data will be added. If there 0120 /// are not \a length bytes available in \a data starting at \a offset, the 0121 /// length will be truncated to contain as many bytes as possible. 0122 /// 0123 /// \param[in] data 0124 /// Another DataExtractor object that contains data. 0125 /// 0126 /// \param[in] offset 0127 /// The offset into \a data at which the subset starts. 0128 /// 0129 /// \param[in] length 0130 /// The length in bytes of the subset of data. 0131 /// 0132 /// \param[in] target_byte_size 0133 /// A size of a target byte in 8-bit host bytes 0134 DataExtractor(const DataExtractor &data, lldb::offset_t offset, 0135 lldb::offset_t length, uint32_t target_byte_size = 1); 0136 0137 /// Copy constructor. 0138 /// 0139 /// The copy constructor is explicit as otherwise it is easy to make 0140 /// unintended modification of a local copy instead of a caller's instance. 0141 /// Also a needless copy of the \a m_data_sp shared pointer is/ expensive. 0142 explicit DataExtractor(const DataExtractor &rhs); 0143 0144 /// Assignment operator. 0145 /// 0146 /// Copies all data, byte order and address size settings from \a rhs into 0147 /// this object. If \a rhs contains shared data, a reference to that shared 0148 /// data will be added. 0149 /// 0150 /// \param[in] rhs 0151 /// Another DataExtractor object to copy. 0152 /// 0153 /// \return 0154 /// A const reference to this object. 0155 const DataExtractor &operator=(const DataExtractor &rhs); 0156 0157 /// Move constructor and move assignment operators to complete the rule of 5. 0158 /// 0159 /// They would get deleted as we already defined those of rule of 3. 0160 DataExtractor(DataExtractor &&rhs) = default; 0161 DataExtractor &operator=(DataExtractor &&rhs) = default; 0162 0163 /// Destructor 0164 /// 0165 /// If this object contains a valid shared data reference, the reference 0166 /// count on the data will be decremented, and if zero, the data will be 0167 /// freed. 0168 virtual ~DataExtractor(); 0169 0170 uint32_t getTargetByteSize() const { return m_target_byte_size; } 0171 0172 /// Clears the object state. 0173 /// 0174 /// Clears the object contents back to a default invalid state, and release 0175 /// any references to shared data that this object may contain. 0176 void Clear(); 0177 0178 /// Dumps the binary data as \a type objects to stream \a s (or to Log() if 0179 /// \a s is nullptr) starting \a offset bytes into the data and stopping 0180 /// after dumping \a length bytes. The offset into the data is displayed at 0181 /// the beginning of each line and can be offset by base address \a 0182 /// base_addr. \a num_per_line objects will be displayed on each line. 0183 /// 0184 /// \param[in] log 0185 /// The log to dump the output to. 0186 /// 0187 /// \param[in] offset 0188 /// The offset into the data at which to start dumping. 0189 /// 0190 /// \param[in] length 0191 /// The number of bytes to dump. 0192 /// 0193 /// \param[in] base_addr 0194 /// The base address that gets added to the offset displayed on 0195 /// each line. 0196 /// 0197 /// \param[in] num_per_line 0198 /// The number of \a type objects to display on each line. 0199 /// 0200 /// \param[in] type 0201 /// The type of objects to use when dumping data from this 0202 /// object. See DataExtractor::Type. 0203 /// 0204 /// \return 0205 /// The offset at which dumping ended. 0206 lldb::offset_t PutToLog(Log *log, lldb::offset_t offset, 0207 lldb::offset_t length, uint64_t base_addr, 0208 uint32_t num_per_line, Type type) const; 0209 0210 /// Extract an arbitrary number of bytes in the specified byte order. 0211 /// 0212 /// Attemps to extract \a length bytes starting at \a offset bytes into this 0213 /// data in the requested byte order (\a dst_byte_order) and place the 0214 /// results in \a dst. \a dst must be at least \a length bytes long. 0215 /// 0216 /// \param[in] offset 0217 /// The offset in bytes into the contained data at which to 0218 /// start extracting. 0219 /// 0220 /// \param[in] length 0221 /// The number of bytes to extract. 0222 /// 0223 /// \param[in] dst_byte_order 0224 /// A byte order of the data that we want when the value in 0225 /// copied to \a dst. 0226 /// 0227 /// \param[out] dst 0228 /// The buffer that will receive the extracted value if there 0229 /// are enough bytes available in the current data. 0230 /// 0231 /// \return 0232 /// The number of bytes that were extracted which will be \a 0233 /// length when the value is successfully extracted, or zero 0234 /// if there aren't enough bytes at the specified offset. 0235 size_t ExtractBytes(lldb::offset_t offset, lldb::offset_t length, 0236 lldb::ByteOrder dst_byte_order, void *dst) const; 0237 0238 /// Extract an address from \a *offset_ptr. 0239 /// 0240 /// Extract a single address from the data and update the offset pointed to 0241 /// by \a offset_ptr. The size of the extracted address comes from the \a 0242 /// m_addr_size member variable and should be set correctly prior to 0243 /// extracting any address values. 0244 /// 0245 /// \param[in,out] offset_ptr 0246 /// A pointer to an offset within the data that will be advanced 0247 /// by the appropriate number of bytes if the value is extracted 0248 /// correctly. If the offset is out of bounds or there are not 0249 /// enough bytes to extract this value, the offset will be left 0250 /// unmodified. 0251 /// 0252 /// \return 0253 /// The extracted address value. 0254 uint64_t GetAddress(lldb::offset_t *offset_ptr) const; 0255 0256 uint64_t GetAddress_unchecked(lldb::offset_t *offset_ptr) const; 0257 0258 /// Get the current address size. 0259 /// 0260 /// Return the size in bytes of any address values this object will extract. 0261 /// 0262 /// \return 0263 /// The size in bytes of address values that will be extracted. 0264 uint32_t GetAddressByteSize() const { return m_addr_size; } 0265 0266 /// Get the number of bytes contained in this object. 0267 /// 0268 /// \return 0269 /// The total number of bytes of data this object refers to. 0270 uint64_t GetByteSize() const { return m_end - m_start; } 0271 0272 /// Extract a C string from \a *offset_ptr. 0273 /// 0274 /// Returns a pointer to a C String from the data at the offset pointed to 0275 /// by \a offset_ptr. A variable length NULL terminated C string will be 0276 /// extracted and the \a offset_ptr will be updated with the offset of the 0277 /// byte that follows the NULL terminator byte. 0278 /// 0279 /// \param[in,out] offset_ptr 0280 /// A pointer to an offset within the data that will be advanced 0281 /// by the appropriate number of bytes if the value is extracted 0282 /// correctly. If the offset is out of bounds or there are not 0283 /// enough bytes to extract this value, the offset will be left 0284 /// unmodified. 0285 /// 0286 /// \return 0287 /// A pointer to the C string value in the data. If the offset 0288 /// pointed to by \a offset_ptr is out of bounds, or if the 0289 /// offset plus the length of the C string is out of bounds, 0290 /// nullptr will be returned. 0291 const char *GetCStr(lldb::offset_t *offset_ptr) const; 0292 0293 /// Extract a C string from \a *offset_ptr with field size \a len. 0294 /// 0295 /// Returns a pointer to a C String from the data at the offset pointed to 0296 /// by \a offset_ptr, with a field length of \a len. 0297 /// A NULL terminated C string will be extracted and the \a offset_ptr 0298 /// will be updated with the offset of the byte that follows the fixed 0299 /// length field. 0300 /// 0301 /// \param[in,out] offset_ptr 0302 /// A pointer to an offset within the data that will be advanced 0303 /// by the appropriate number of bytes if the value is extracted 0304 /// correctly. If the offset is out of bounds or there are not 0305 /// enough bytes to extract this value, the offset will be left 0306 /// unmodified. 0307 /// 0308 /// \return 0309 /// A pointer to the C string value in the data. If the offset 0310 /// pointed to by \a offset_ptr is out of bounds, or if the 0311 /// offset plus the length of the field is out of bounds, or if 0312 /// the field does not contain a NULL terminator byte, nullptr will 0313 /// be returned. 0314 const char *GetCStr(lldb::offset_t *offset_ptr, lldb::offset_t len) const; 0315 0316 /// Extract \a length bytes from \a *offset_ptr. 0317 /// 0318 /// Returns a pointer to a bytes in this object's data at the offset pointed 0319 /// to by \a offset_ptr. If \a length is zero or too large, then the offset 0320 /// pointed to by \a offset_ptr will not be updated and nullptr will be 0321 /// returned. 0322 /// 0323 /// \param[in,out] offset_ptr 0324 /// A pointer to an offset within the data that will be advanced 0325 /// by the appropriate number of bytes if the value is extracted 0326 /// correctly. If the offset is out of bounds or there are not 0327 /// enough bytes to extract this value, the offset will be left 0328 /// unmodified. 0329 /// 0330 /// \param[in] length 0331 /// The optional length of a string to extract. If the value is 0332 /// zero, a NULL terminated C string will be extracted. 0333 /// 0334 /// \return 0335 /// A pointer to the bytes in this object's data if the offset 0336 /// and length are valid, or nullptr otherwise. 0337 const void *GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const { 0338 const uint8_t *ptr = PeekData(*offset_ptr, length); 0339 if (ptr) 0340 *offset_ptr += length; 0341 return ptr; 0342 } 0343 0344 /// Copy \a length bytes from \a *offset, without swapping bytes. 0345 /// 0346 /// \param[in] offset 0347 /// The offset into this data from which to start copying 0348 /// 0349 /// \param[in] length 0350 /// The length of the data to copy from this object 0351 /// 0352 /// \param[out] dst 0353 /// The buffer to place the output data. 0354 /// 0355 /// \return 0356 /// Returns the number of bytes that were copied, or zero if 0357 /// anything goes wrong. 0358 lldb::offset_t CopyData(lldb::offset_t offset, lldb::offset_t length, 0359 void *dst) const; 0360 0361 /// Copy \a dst_len bytes from \a *offset_ptr and ensure the copied data is 0362 /// treated as a value that can be swapped to match the specified byte 0363 /// order. 0364 /// 0365 /// For values that are larger than the supported integer sizes, this 0366 /// function can be used to extract data in a specified byte order. It can 0367 /// also be used to copy a smaller integer value from to a larger value. The 0368 /// extra bytes left over will be padded correctly according to the byte 0369 /// order of this object and the \a dst_byte_order. This can be very handy 0370 /// when say copying a partial data value into a register. 0371 /// 0372 /// \param[in] src_offset 0373 /// The offset into this data from which to start copying an endian 0374 /// entity 0375 /// 0376 /// \param[in] src_len 0377 /// The length of the endian data to copy from this object into the \a 0378 /// dst object 0379 /// 0380 /// \param[out] dst 0381 /// The buffer where to place the endian data. The data might need to be 0382 /// byte swapped (and appropriately padded with zeroes if \a src_len != 0383 /// \a dst_len) if \a dst_byte_order does not match the byte order in 0384 /// this object. 0385 /// 0386 /// \param[in] dst_len 0387 /// The length number of bytes that the endian value will occupy is \a 0388 /// dst. 0389 /// 0390 /// \param[in] dst_byte_order 0391 /// The byte order that the endian value should be in the \a dst buffer. 0392 /// 0393 /// \return 0394 /// Returns the number of bytes that were copied, or zero if anything 0395 /// goes wrong. 0396 lldb::offset_t CopyByteOrderedData(lldb::offset_t src_offset, 0397 lldb::offset_t src_len, void *dst, 0398 lldb::offset_t dst_len, 0399 lldb::ByteOrder dst_byte_order) const; 0400 0401 /// Get the data end pointer. 0402 /// 0403 /// \return 0404 /// Returns a pointer to the next byte contained in this 0405 /// object's data, or nullptr of there is no data in this object. 0406 const uint8_t *GetDataEnd() const { return m_end; } 0407 0408 /// Get the shared data offset. 0409 /// 0410 /// Get the offset of the first byte of data in the shared data (if any). 0411 /// 0412 /// \return 0413 /// If this object contains shared data, this function returns 0414 /// the offset in bytes into that shared data, zero otherwise. 0415 size_t GetSharedDataOffset() const; 0416 0417 /// Get the data start pointer. 0418 /// 0419 /// \return 0420 /// Returns a pointer to the first byte contained in this 0421 /// object's data, or nullptr of there is no data in this object. 0422 const uint8_t *GetDataStart() const { return m_start; } 0423 0424 /// Extract a float from \a *offset_ptr. 0425 /// 0426 /// Extract a single float value. 0427 /// 0428 /// \param[in,out] offset_ptr 0429 /// A pointer to an offset within the data that will be advanced 0430 /// by the appropriate number of bytes if the value is extracted 0431 /// correctly. If the offset is out of bounds or there are not 0432 /// enough bytes to extract this value, the offset will be left 0433 /// unmodified. 0434 /// 0435 /// \return 0436 /// The floating value that was extracted, or zero on failure. 0437 float GetFloat(lldb::offset_t *offset_ptr) const; 0438 0439 double GetDouble(lldb::offset_t *offset_ptr) const; 0440 0441 long double GetLongDouble(lldb::offset_t *offset_ptr) const; 0442 0443 /// Extract an integer of size \a byte_size from \a *offset_ptr. 0444 /// 0445 /// Extract a single integer value and update the offset pointed to by \a 0446 /// offset_ptr. The size of the extracted integer is specified by the \a 0447 /// byte_size argument. \a byte_size must have a value >= 1 and <= 4 since 0448 /// the return value is only 32 bits wide. 0449 /// 0450 /// \param[in,out] offset_ptr 0451 /// A pointer to an offset within the data that will be advanced 0452 /// by the appropriate number of bytes if the value is extracted 0453 /// correctly. If the offset is out of bounds or there are not 0454 /// enough bytes to extract this value, the offset will be left 0455 /// unmodified. 0456 /// 0457 /// \param[in] byte_size 0458 /// The size in byte of the integer to extract. 0459 /// 0460 /// \return 0461 /// The integer value that was extracted, or zero on failure. 0462 uint32_t GetMaxU32(lldb::offset_t *offset_ptr, size_t byte_size) const; 0463 0464 /// Extract an unsigned integer of size \a byte_size from \a *offset_ptr. 0465 /// 0466 /// Extract a single unsigned integer value and update the offset pointed to 0467 /// by \a offset_ptr. The size of the extracted integer is specified by the 0468 /// \a byte_size argument. \a byte_size must have a value greater than or 0469 /// equal to one and less than or equal to eight since the return value is 0470 /// 64 bits wide. 0471 /// 0472 /// \param[in,out] offset_ptr 0473 /// A pointer to an offset within the data that will be advanced 0474 /// by the appropriate number of bytes if the value is extracted 0475 /// correctly. If the offset is out of bounds or there are not 0476 /// enough bytes to extract this value, the offset will be left 0477 /// unmodified. 0478 /// 0479 /// \param[in] byte_size 0480 /// The size in byte of the integer to extract. 0481 /// 0482 /// \return 0483 /// The unsigned integer value that was extracted, or zero on 0484 /// failure. 0485 uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const; 0486 0487 uint64_t GetMaxU64_unchecked(lldb::offset_t *offset_ptr, 0488 size_t byte_size) const; 0489 0490 /// Extract an signed integer of size \a byte_size from \a *offset_ptr. 0491 /// 0492 /// Extract a single signed integer value (sign extending if required) and 0493 /// update the offset pointed to by \a offset_ptr. The size of the extracted 0494 /// integer is specified by the \a byte_size argument. \a byte_size must 0495 /// have a value greater than or equal to one and less than or equal to 0496 /// eight since the return value is 64 bits wide. 0497 /// 0498 /// \param[in,out] offset_ptr 0499 /// A pointer to an offset within the data that will be advanced 0500 /// by the appropriate number of bytes if the value is extracted 0501 /// correctly. If the offset is out of bounds or there are not 0502 /// enough bytes to extract this value, the offset will be left 0503 /// unmodified. 0504 /// 0505 /// \param[in] byte_size 0506 /// The size in byte of the integer to extract. 0507 /// 0508 /// \return 0509 /// The sign extended signed integer value that was extracted, 0510 /// or zero on failure. 0511 int64_t GetMaxS64(lldb::offset_t *offset_ptr, size_t byte_size) const; 0512 0513 /// Extract an unsigned integer of size \a byte_size from \a *offset_ptr, 0514 /// then extract the bitfield from this value if \a bitfield_bit_size is 0515 /// non-zero. 0516 /// 0517 /// Extract a single unsigned integer value and update the offset pointed to 0518 /// by \a offset_ptr. The size of the extracted integer is specified by the 0519 /// \a byte_size argument. \a byte_size must have a value greater than or 0520 /// equal to one and less than or equal to 8 since the return value is 64 0521 /// bits wide. 0522 /// 0523 /// \param[in,out] offset_ptr 0524 /// A pointer to an offset within the data that will be advanced 0525 /// by the appropriate number of bytes if the value is extracted 0526 /// correctly. If the offset is out of bounds or there are not 0527 /// enough bytes to extract this value, the offset will be left 0528 /// unmodified. 0529 /// 0530 /// \param[in] size 0531 /// The size in byte of the integer to extract. 0532 /// 0533 /// \param[in] bitfield_bit_size 0534 /// The size in bits of the bitfield value to extract, or zero 0535 /// to just extract the entire integer value. 0536 /// 0537 /// \param[in] bitfield_bit_offset 0538 /// The bit offset of the bitfield value in the extracted 0539 /// integer. For little-endian data, this is the offset of 0540 /// the LSB of the bitfield from the LSB of the integer. 0541 /// For big-endian data, this is the offset of the MSB of the 0542 /// bitfield from the MSB of the integer. 0543 /// 0544 /// \return 0545 /// The unsigned bitfield integer value that was extracted, or 0546 /// zero on failure. 0547 uint64_t GetMaxU64Bitfield(lldb::offset_t *offset_ptr, size_t size, 0548 uint32_t bitfield_bit_size, 0549 uint32_t bitfield_bit_offset) const; 0550 0551 /// Extract an signed integer of size \a size from \a *offset_ptr, then 0552 /// extract and sign-extend the bitfield from this value if \a 0553 /// bitfield_bit_size is non-zero. 0554 /// 0555 /// Extract a single signed integer value (sign-extending if required) and 0556 /// update the offset pointed to by \a offset_ptr. The size of the extracted 0557 /// integer is specified by the \a size argument. \a size must 0558 /// have a value greater than or equal to one and less than or equal to 0559 /// eight since the return value is 64 bits wide. 0560 /// 0561 /// \param[in,out] offset_ptr 0562 /// A pointer to an offset within the data that will be advanced 0563 /// by the appropriate number of bytes if the value is extracted 0564 /// correctly. If the offset is out of bounds or there are not 0565 /// enough bytes to extract this value, the offset will be left 0566 /// unmodified. 0567 /// 0568 /// \param[in] size 0569 /// The size in bytes of the integer to extract. 0570 /// 0571 /// \param[in] bitfield_bit_size 0572 /// The size in bits of the bitfield value to extract, or zero 0573 /// to just extract the entire integer value. 0574 /// 0575 /// \param[in] bitfield_bit_offset 0576 /// The bit offset of the bitfield value in the extracted 0577 /// integer. For little-endian data, this is the offset of 0578 /// the LSB of the bitfield from the LSB of the integer. 0579 /// For big-endian data, this is the offset of the MSB of the 0580 /// bitfield from the MSB of the integer. 0581 /// 0582 /// \return 0583 /// The signed bitfield integer value that was extracted, or 0584 /// zero on failure. 0585 int64_t GetMaxS64Bitfield(lldb::offset_t *offset_ptr, size_t size, 0586 uint32_t bitfield_bit_size, 0587 uint32_t bitfield_bit_offset) const; 0588 0589 /// Get the current byte order value. 0590 /// 0591 /// \return 0592 /// The current byte order value from this object's internal 0593 /// state. 0594 lldb::ByteOrder GetByteOrder() const { return m_byte_order; } 0595 0596 /// Extract a uint8_t value from \a *offset_ptr. 0597 /// 0598 /// Extract a single uint8_t from the binary data at the offset pointed to 0599 /// by \a offset_ptr, and advance the offset on success. 0600 /// 0601 /// \param[in,out] offset_ptr 0602 /// A pointer to an offset within the data that will be advanced 0603 /// by the appropriate number of bytes if the value is extracted 0604 /// correctly. If the offset is out of bounds or there are not 0605 /// enough bytes to extract this value, the offset will be left 0606 /// unmodified. 0607 /// 0608 /// \return 0609 /// The extracted uint8_t value. 0610 uint8_t GetU8(lldb::offset_t *offset_ptr) const; 0611 0612 uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const { 0613 uint8_t val = m_start[*offset_ptr]; 0614 *offset_ptr += 1; 0615 return val; 0616 } 0617 0618 uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const; 0619 0620 uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const; 0621 0622 uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const; 0623 /// Extract \a count uint8_t values from \a *offset_ptr. 0624 /// 0625 /// Extract \a count uint8_t values from the binary data at the offset 0626 /// pointed to by \a offset_ptr, and advance the offset on success. The 0627 /// extracted values are copied into \a dst. 0628 /// 0629 /// \param[in,out] offset_ptr 0630 /// A pointer to an offset within the data that will be advanced 0631 /// by the appropriate number of bytes if the value is extracted 0632 /// correctly. If the offset is out of bounds or there are not 0633 /// enough bytes to extract this value, the offset will be left 0634 /// unmodified. 0635 /// 0636 /// \param[out] dst 0637 /// A buffer to copy \a count uint8_t values into. \a dst must 0638 /// be large enough to hold all requested data. 0639 /// 0640 /// \param[in] count 0641 /// The number of uint8_t values to extract. 0642 /// 0643 /// \return 0644 /// \a dst if all values were properly extracted and copied, 0645 /// nullptr otherwise. 0646 void *GetU8(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const; 0647 0648 /// Extract a uint16_t value from \a *offset_ptr. 0649 /// 0650 /// Extract a single uint16_t from the binary data at the offset pointed to 0651 /// by \a offset_ptr, and update the offset on success. 0652 /// 0653 /// \param[in,out] offset_ptr 0654 /// A pointer to an offset within the data that will be advanced 0655 /// by the appropriate number of bytes if the value is extracted 0656 /// correctly. If the offset is out of bounds or there are not 0657 /// enough bytes to extract this value, the offset will be left 0658 /// unmodified. 0659 /// 0660 /// \return 0661 /// The extracted uint16_t value. 0662 uint16_t GetU16(lldb::offset_t *offset_ptr) const; 0663 0664 /// Extract \a count uint16_t values from \a *offset_ptr. 0665 /// 0666 /// Extract \a count uint16_t values from the binary data at the offset 0667 /// pointed to by \a offset_ptr, and advance the offset on success. The 0668 /// extracted values are copied into \a dst. 0669 /// 0670 /// \param[in,out] offset_ptr 0671 /// A pointer to an offset within the data that will be advanced 0672 /// by the appropriate number of bytes if the value is extracted 0673 /// correctly. If the offset is out of bounds or there are not 0674 /// enough bytes to extract this value, the offset will be left 0675 /// unmodified. 0676 /// 0677 /// \param[out] dst 0678 /// A buffer to copy \a count uint16_t values into. \a dst must 0679 /// be large enough to hold all requested data. 0680 /// 0681 /// \param[in] count 0682 /// The number of uint16_t values to extract. 0683 /// 0684 /// \return 0685 /// \a dst if all values were properly extracted and copied, 0686 /// nullptr otherwise. 0687 void *GetU16(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const; 0688 0689 /// Extract a uint32_t value from \a *offset_ptr. 0690 /// 0691 /// Extract a single uint32_t from the binary data at the offset pointed to 0692 /// by \a offset_ptr, and update the offset on success. 0693 /// 0694 /// \param[in,out] offset_ptr 0695 /// A pointer to an offset within the data that will be advanced 0696 /// by the appropriate number of bytes if the value is extracted 0697 /// correctly. If the offset is out of bounds or there are not 0698 /// enough bytes to extract this value, the offset will be left 0699 /// unmodified. 0700 /// 0701 /// \return 0702 /// The extracted uint32_t value. 0703 uint32_t GetU32(lldb::offset_t *offset_ptr) const; 0704 0705 /// Extract \a count uint32_t values from \a *offset_ptr. 0706 /// 0707 /// Extract \a count uint32_t values from the binary data at the offset 0708 /// pointed to by \a offset_ptr, and advance the offset on success. The 0709 /// extracted values are copied into \a dst. 0710 /// 0711 /// \param[in,out] offset_ptr 0712 /// A pointer to an offset within the data that will be advanced 0713 /// by the appropriate number of bytes if the value is extracted 0714 /// correctly. If the offset is out of bounds or there are not 0715 /// enough bytes to extract this value, the offset will be left 0716 /// unmodified. 0717 /// 0718 /// \param[out] dst 0719 /// A buffer to copy \a count uint32_t values into. \a dst must 0720 /// be large enough to hold all requested data. 0721 /// 0722 /// \param[in] count 0723 /// The number of uint32_t values to extract. 0724 /// 0725 /// \return 0726 /// \a dst if all values were properly extracted and copied, 0727 /// nullptr otherwise. 0728 void *GetU32(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const; 0729 0730 /// Extract a uint64_t value from \a *offset_ptr. 0731 /// 0732 /// Extract a single uint64_t from the binary data at the offset pointed to 0733 /// by \a offset_ptr, and update the offset on success. 0734 /// 0735 /// \param[in,out] offset_ptr 0736 /// A pointer to an offset within the data that will be advanced 0737 /// by the appropriate number of bytes if the value is extracted 0738 /// correctly. If the offset is out of bounds or there are not 0739 /// enough bytes to extract this value, the offset will be left 0740 /// unmodified. 0741 /// 0742 /// \return 0743 /// The extracted uint64_t value. 0744 uint64_t GetU64(lldb::offset_t *offset_ptr) const; 0745 0746 /// Extract \a count uint64_t values from \a *offset_ptr. 0747 /// 0748 /// Extract \a count uint64_t values from the binary data at the offset 0749 /// pointed to by \a offset_ptr, and advance the offset on success. The 0750 /// extracted values are copied into \a dst. 0751 /// 0752 /// \param[in,out] offset_ptr 0753 /// A pointer to an offset within the data that will be advanced 0754 /// by the appropriate number of bytes if the value is extracted 0755 /// correctly. If the offset is out of bounds or there are not 0756 /// enough bytes to extract this value, the offset will be left 0757 /// unmodified. 0758 /// 0759 /// \param[out] dst 0760 /// A buffer to copy \a count uint64_t values into. \a dst must 0761 /// be large enough to hold all requested data. 0762 /// 0763 /// \param[in] count 0764 /// The number of uint64_t values to extract. 0765 /// 0766 /// \return 0767 /// \a dst if all values were properly extracted and copied, 0768 /// nullptr otherwise. 0769 void *GetU64(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const; 0770 0771 /// Extract a signed LEB128 value from \a *offset_ptr. 0772 /// 0773 /// Extracts an signed LEB128 number from this object's data starting at the 0774 /// offset pointed to by \a offset_ptr. The offset pointed to by \a 0775 /// offset_ptr will be updated with the offset of the byte following the 0776 /// last extracted byte. 0777 /// 0778 /// \param[in,out] offset_ptr 0779 /// A pointer to an offset within the data that will be advanced 0780 /// by the appropriate number of bytes if the value is extracted 0781 /// correctly. If the offset is out of bounds or there are not 0782 /// enough bytes to extract this value, the offset will be left 0783 /// unmodified. 0784 /// 0785 /// \return 0786 /// The extracted signed integer value. 0787 int64_t GetSLEB128(lldb::offset_t *offset_ptr) const; 0788 0789 /// Extract a unsigned LEB128 value from \a *offset_ptr. 0790 /// 0791 /// Extracts an unsigned LEB128 number from this object's data starting at 0792 /// the offset pointed to by \a offset_ptr. The offset pointed to by \a 0793 /// offset_ptr will be updated with the offset of the byte following the 0794 /// last extracted byte. 0795 /// 0796 /// \param[in,out] offset_ptr 0797 /// A pointer to an offset within the data that will be advanced 0798 /// by the appropriate number of bytes if the value is extracted 0799 /// correctly. If the offset is out of bounds or there are not 0800 /// enough bytes to extract this value, the offset will be left 0801 /// unmodified. 0802 /// 0803 /// \return 0804 /// The extracted unsigned integer value. 0805 uint64_t GetULEB128(lldb::offset_t *offset_ptr) const; 0806 0807 lldb::DataBufferSP &GetSharedDataBuffer() { return m_data_sp; } 0808 0809 /// Peek at a C string at \a offset. 0810 /// 0811 /// Peeks at a string in the contained data. No verification is done to make 0812 /// sure the entire string lies within the bounds of this object's data, 0813 /// only \a offset is verified to be a valid offset. 0814 /// 0815 /// \param[in] offset 0816 /// An offset into the data. 0817 /// 0818 /// \return 0819 /// A non-nullptr C string pointer if \a offset is a valid offset, 0820 /// nullptr otherwise. 0821 const char *PeekCStr(lldb::offset_t offset) const; 0822 0823 /// Peek at a bytes at \a offset. 0824 /// 0825 /// Returns a pointer to \a length bytes at \a offset as long as there are 0826 /// \a length bytes available starting at \a offset. 0827 /// 0828 /// \return 0829 /// A non-nullptr data pointer if \a offset is a valid offset and 0830 /// there are \a length bytes available at that offset, nullptr 0831 /// otherwise. 0832 const uint8_t *PeekData(lldb::offset_t offset, lldb::offset_t length) const { 0833 if (ValidOffsetForDataOfSize(offset, length)) 0834 return m_start + offset; 0835 return nullptr; 0836 } 0837 0838 /// Set the address byte size. 0839 /// 0840 /// Set the size in bytes that will be used when extracting any address and 0841 /// pointer values from data contained in this object. 0842 /// 0843 /// \param[in] addr_size 0844 /// The size in bytes to use when extracting addresses. 0845 void SetAddressByteSize(uint32_t addr_size) { 0846 assert(addr_size == 2 || addr_size == 4 || addr_size == 8); 0847 m_addr_size = addr_size; 0848 } 0849 0850 /// Set data with a buffer that is caller owned. 0851 /// 0852 /// Use data that is owned by the caller when extracting values. The data 0853 /// must stay around as long as this object, or any object that copies a 0854 /// subset of this object's data, is valid. If \a bytes is nullptr, or \a 0855 /// length is zero, this object will contain no data. 0856 /// 0857 /// \param[in] bytes 0858 /// A pointer to caller owned data. 0859 /// 0860 /// \param[in] length 0861 /// The length in bytes of \a bytes. 0862 /// 0863 /// \param[in] byte_order 0864 /// A byte order of the data that we are extracting from. 0865 /// 0866 /// \return 0867 /// The number of bytes that this object now contains. 0868 lldb::offset_t SetData(const void *bytes, lldb::offset_t length, 0869 lldb::ByteOrder byte_order); 0870 0871 /// Adopt a subset of \a data. 0872 /// 0873 /// Set this object's data to be a subset of the data bytes in \a data. If 0874 /// \a data contains shared data, then a reference to the shared data will 0875 /// be added to ensure the shared data stays around as long as any objects 0876 /// have references to the shared data. The byte order and the address size 0877 /// settings are copied from \a data. If \a offset is not a valid offset in 0878 /// \a data, then no reference to the shared data will be added. If there 0879 /// are not \a length bytes available in \a data starting at \a offset, the 0880 /// length will be truncated to contains as many bytes as possible. 0881 /// 0882 /// \param[in] data 0883 /// Another DataExtractor object that contains data. 0884 /// 0885 /// \param[in] offset 0886 /// The offset into \a data at which the subset starts. 0887 /// 0888 /// \param[in] length 0889 /// The length in bytes of the subset of \a data. 0890 /// 0891 /// \return 0892 /// The number of bytes that this object now contains. 0893 lldb::offset_t SetData(const DataExtractor &data, lldb::offset_t offset, 0894 lldb::offset_t length); 0895 0896 /// Adopt a subset of shared data in \a data_sp. 0897 /// 0898 /// Copies the data shared pointer which adds a reference to the contained 0899 /// in \a data_sp. The shared data reference is reference counted to ensure 0900 /// the data lives as long as anyone still has a valid shared pointer to the 0901 /// data in \a data_sp. The byte order and address byte size settings remain 0902 /// the same. If \a offset is not a valid offset in \a data_sp, then no 0903 /// reference to the shared data will be added. If there are not \a length 0904 /// bytes available in \a data starting at \a offset, the length will be 0905 /// truncated to contains as many bytes as possible. 0906 /// 0907 /// \param[in] data_sp 0908 /// A shared pointer to data. 0909 /// 0910 /// \param[in] offset 0911 /// The offset into \a data_sp at which the subset starts. 0912 /// 0913 /// \param[in] length 0914 /// The length in bytes of the subset of \a data_sp. 0915 /// 0916 /// \return 0917 /// The number of bytes that this object now contains. 0918 lldb::offset_t SetData(const lldb::DataBufferSP &data_sp, 0919 lldb::offset_t offset = 0, 0920 lldb::offset_t length = LLDB_INVALID_OFFSET); 0921 0922 /// Set the byte_order value. 0923 /// 0924 /// Sets the byte order of the data to extract. Extracted values will be 0925 /// swapped if necessary when decoding. 0926 /// 0927 /// \param[in] byte_order 0928 /// The byte order value to use when extracting data. 0929 void SetByteOrder(lldb::ByteOrder byte_order) { m_byte_order = byte_order; } 0930 0931 /// Skip an LEB128 number at \a *offset_ptr. 0932 /// 0933 /// Skips a LEB128 number (signed or unsigned) from this object's data 0934 /// starting at the offset pointed to by \a offset_ptr. The offset pointed 0935 /// to by \a offset_ptr will be updated with the offset of the byte 0936 /// following the last extracted byte. 0937 /// 0938 /// \param[in,out] offset_ptr 0939 /// A pointer to an offset within the data that will be advanced 0940 /// by the appropriate number of bytes if the value is extracted 0941 /// correctly. If the offset is out of bounds or there are not 0942 /// enough bytes to extract this value, the offset will be left 0943 /// unmodified. 0944 /// 0945 /// \return 0946 /// The number of bytes consumed during the extraction. 0947 uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const; 0948 0949 /// Test the validity of \a offset. 0950 /// 0951 /// \return 0952 /// true if \a offset is a valid offset into the data in this object, 0953 /// false otherwise. 0954 bool ValidOffset(lldb::offset_t offset) const { 0955 return offset < GetByteSize(); 0956 } 0957 0958 /// Test the availability of \a length bytes of data from \a offset. 0959 /// 0960 /// \return 0961 /// true if \a offset is a valid offset and there are \a 0962 /// length bytes available at that offset, false otherwise. 0963 bool ValidOffsetForDataOfSize(lldb::offset_t offset, 0964 lldb::offset_t length) const { 0965 return length <= BytesLeft(offset); 0966 } 0967 0968 size_t Copy(DataExtractor &dest_data) const; 0969 0970 bool Append(DataExtractor &rhs); 0971 0972 bool Append(void *bytes, lldb::offset_t length); 0973 0974 lldb::offset_t BytesLeft(lldb::offset_t offset) const { 0975 const lldb::offset_t size = GetByteSize(); 0976 if (size > offset) 0977 return size - offset; 0978 return 0; 0979 } 0980 0981 void Checksum(llvm::SmallVectorImpl<uint8_t> &dest, uint64_t max_data = 0); 0982 0983 llvm::ArrayRef<uint8_t> GetData() const { 0984 return {GetDataStart(), size_t(GetByteSize())}; 0985 } 0986 0987 llvm::DataExtractor GetAsLLVM() const { 0988 return {GetData(), GetByteOrder() == lldb::eByteOrderLittle, 0989 uint8_t(GetAddressByteSize())}; 0990 } 0991 0992 protected: 0993 template <typename T> T Get(lldb::offset_t *offset_ptr, T fail_value) const { 0994 constexpr size_t src_size = sizeof(T); 0995 T val = fail_value; 0996 0997 const T *src = static_cast<const T *>(GetData(offset_ptr, src_size)); 0998 if (!src) 0999 return val; 1000 1001 memcpy(&val, src, src_size); 1002 if (m_byte_order != endian::InlHostByteOrder()) 1003 llvm::sys::swapByteOrder(val); 1004 1005 return val; 1006 } 1007 1008 // Member variables 1009 const uint8_t *m_start = nullptr; ///< A pointer to the first byte of data. 1010 const uint8_t *m_end = 1011 nullptr; ///< A pointer to the byte that is past the end of the data. 1012 lldb::ByteOrder 1013 m_byte_order; ///< The byte order of the data we are extracting from. 1014 uint32_t m_addr_size; ///< The address size to use when extracting addresses. 1015 /// The shared pointer to data that can be shared among multiple instances 1016 lldb::DataBufferSP m_data_sp; 1017 /// Making it const would require implementation of move assignment operator. 1018 uint32_t m_target_byte_size = 1; 1019 }; 1020 1021 } // namespace lldb_private 1022 1023 #endif // LLDB_UTILITY_DATAEXTRACTOR_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|