1 //===-- RegisterValue.cpp ---------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/RegisterValue.h"
11 #include "lldb/Utility/Args.h"
12 #include "lldb/Utility/DataExtractor.h"
13 #include "lldb/Utility/Scalar.h"
14 #include "lldb/Utility/Status.h"
15 #include "lldb/Utility/Stream.h"
16 #include "lldb/Utility/StreamString.h"
17 #include "lldb/lldb-defines.h"
18 #include "lldb/lldb-private-types.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/StringRef.h"
33 using namespace lldb_private;
35 bool RegisterValue::GetData(DataExtractor &data) const {
36 return data.SetData(GetBytes(), GetByteSize(), GetByteOrder()) > 0;
39 uint32_t RegisterValue::GetAsMemoryData(const RegisterInfo *reg_info, void *dst,
41 lldb::ByteOrder dst_byte_order,
42 Status &error) const {
43 if (reg_info == nullptr) {
44 error.SetErrorString("invalid register info argument.");
48 // ReadRegister should have already been called on this object prior to
50 if (GetType() == eTypeInvalid) {
51 // No value has been read into this object...
52 error.SetErrorStringWithFormat(
53 "invalid register value type for register %s", reg_info->name);
57 if (dst_len > kMaxRegisterByteSize) {
58 error.SetErrorString("destination is too big");
62 const uint32_t src_len = reg_info->byte_size;
64 // Extract the register data into a data extractor
65 DataExtractor reg_data;
66 if (!GetData(reg_data)) {
67 error.SetErrorString("invalid register value to copy into");
71 // Prepare a memory buffer that contains some or all of the register value
72 const uint32_t bytes_copied =
73 reg_data.CopyByteOrderedData(0, // src offset
74 src_len, // src length
76 dst_len, // dst length
77 dst_byte_order); // dst byte order
78 if (bytes_copied == 0)
79 error.SetErrorStringWithFormat(
80 "failed to copy data for register write of %s", reg_info->name);
85 uint32_t RegisterValue::SetFromMemoryData(const RegisterInfo *reg_info,
86 const void *src, uint32_t src_len,
87 lldb::ByteOrder src_byte_order,
89 if (reg_info == nullptr) {
90 error.SetErrorString("invalid register info argument.");
94 // Moving from addr into a register
96 // Case 1: src_len == dst_len
98 // |AABBCCDD| Address contents
99 // |AABBCCDD| Register contents
101 // Case 2: src_len > dst_len
103 // Status! (The register should always be big enough to hold the data)
105 // Case 3: src_len < dst_len
107 // |AABB| Address contents
108 // |AABB0000| Register contents [on little-endian hardware]
109 // |0000AABB| Register contents [on big-endian hardware]
110 if (src_len > kMaxRegisterByteSize) {
111 error.SetErrorStringWithFormat(
112 "register buffer is too small to receive %u bytes of data.", src_len);
116 const uint32_t dst_len = reg_info->byte_size;
118 if (src_len > dst_len) {
119 error.SetErrorStringWithFormat(
120 "%u bytes is too big to store in register %s (%u bytes)", src_len,
121 reg_info->name, dst_len);
125 // Use a data extractor to correctly copy and pad the bytes read into the
127 DataExtractor src_data(src, src_len, src_byte_order, 4);
129 error = SetValueFromData(reg_info, src_data, 0, true);
133 // If SetValueFromData succeeded, we must have copied all of src_len
137 bool RegisterValue::GetScalarValue(Scalar &scalar) const {
142 switch (buffer.length) {
146 scalar = *(const uint8_t *)buffer.bytes;
149 scalar = *reinterpret_cast<const uint16_t *>(buffer.bytes);
152 scalar = *reinterpret_cast<const uint32_t *>(buffer.bytes);
155 scalar = *reinterpret_cast<const uint64_t *>(buffer.bytes);
160 if (buffer.length % sizeof(uint64_t) == 0) {
161 const auto length_in_bits = buffer.length * 8;
162 const auto length_in_uint64 = buffer.length / sizeof(uint64_t);
164 llvm::APInt(length_in_bits,
165 llvm::ArrayRef<uint64_t>(
166 reinterpret_cast<const uint64_t *>(buffer.bytes),
180 case eTypeLongDouble:
187 void RegisterValue::Clear() { m_type = eTypeInvalid; }
189 RegisterValue::Type RegisterValue::SetType(const RegisterInfo *reg_info) {
190 // To change the type, we simply copy the data in again, using the new format
192 DataExtractor copy_data;
193 if (copy.CopyValue(*this) && copy.GetData(copy_data))
194 SetValueFromData(reg_info, copy_data, 0, true);
199 Status RegisterValue::SetValueFromData(const RegisterInfo *reg_info,
201 lldb::offset_t src_offset,
202 bool partial_data_ok) {
205 if (src.GetByteSize() == 0) {
206 error.SetErrorString("empty data.");
210 if (reg_info->byte_size == 0) {
211 error.SetErrorString("invalid register info.");
215 uint32_t src_len = src.GetByteSize() - src_offset;
217 if (!partial_data_ok && (src_len < reg_info->byte_size)) {
218 error.SetErrorString("not enough data.");
222 // Cap the data length if there is more than enough bytes for this register
224 if (src_len > reg_info->byte_size)
225 src_len = reg_info->byte_size;
227 // Zero out the value in case we get partial data...
228 memset(buffer.bytes, 0, sizeof(buffer.bytes));
232 m_type = eTypeInvalid;
233 switch (reg_info->encoding) {
234 case eEncodingInvalid:
238 if (reg_info->byte_size == 1)
239 SetUInt8(src.GetMaxU32(&src_offset, src_len));
240 else if (reg_info->byte_size <= 2)
241 SetUInt16(src.GetMaxU32(&src_offset, src_len));
242 else if (reg_info->byte_size <= 4)
243 SetUInt32(src.GetMaxU32(&src_offset, src_len));
244 else if (reg_info->byte_size <= 8)
245 SetUInt64(src.GetMaxU64(&src_offset, src_len));
246 else if (reg_info->byte_size <= 16) {
247 uint64_t data1 = src.GetU64(&src_offset);
248 uint64_t data2 = src.GetU64(&src_offset);
249 if (src.GetByteSize() == eByteOrderBig) {
256 SetUInt128(llvm::APInt(128, 2, int128.x));
259 case eEncodingIEEE754:
260 if (reg_info->byte_size == sizeof(float))
261 SetFloat(src.GetFloat(&src_offset));
262 else if (reg_info->byte_size == sizeof(double))
263 SetDouble(src.GetDouble(&src_offset));
264 else if (reg_info->byte_size == sizeof(long double))
265 SetLongDouble(src.GetLongDouble(&src_offset));
267 case eEncodingVector: {
269 buffer.length = reg_info->byte_size;
270 buffer.byte_order = src.GetByteOrder();
271 assert(buffer.length <= kMaxRegisterByteSize);
272 if (buffer.length > kMaxRegisterByteSize)
273 buffer.length = kMaxRegisterByteSize;
274 if (src.CopyByteOrderedData(
275 src_offset, // offset within "src" to start extracting data
276 src_len, // src length
277 buffer.bytes, // dst buffer
278 buffer.length, // dst length
279 buffer.byte_order) == 0) // dst byte order
281 error.SetErrorStringWithFormat(
282 "failed to copy data for register write of %s", reg_info->name);
288 if (m_type == eTypeInvalid)
289 error.SetErrorStringWithFormat(
290 "invalid register value type for register %s", reg_info->name);
294 // Helper function for RegisterValue::SetValueFromString()
295 static bool ParseVectorEncoding(const RegisterInfo *reg_info,
296 llvm::StringRef vector_str,
297 const uint32_t byte_size,
298 RegisterValue *reg_value) {
299 // Example: vector_str = "{0x2c 0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a
300 // 0x2a 0x3e 0x84 0x4f 0x2a 0x3e}".
301 vector_str = vector_str.trim();
302 vector_str.consume_front("{");
303 vector_str.consume_back("}");
304 vector_str = vector_str.trim();
308 // The first split should give us:
309 // ('0x2c', '0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f
312 llvm::StringRef cdr = vector_str;
313 std::tie(car, cdr) = vector_str.split(Sep);
314 std::vector<uint8_t> bytes;
317 // Using radix auto-sensing by passing 0 as the radix. Keep on processing the
318 // vector elements as long as the parsing succeeds and the vector size is <
320 while (!car.getAsInteger(0, byte) && bytes.size() < byte_size) {
321 bytes.push_back(byte);
322 std::tie(car, cdr) = cdr.split(Sep);
325 // Check for vector of exact byte_size elements.
326 if (bytes.size() != byte_size)
329 reg_value->SetBytes(&(bytes.front()), byte_size, eByteOrderLittle);
333 Status RegisterValue::SetValueFromString(const RegisterInfo *reg_info,
334 llvm::StringRef value_str) {
336 if (reg_info == nullptr) {
337 error.SetErrorString("Invalid register info argument.");
341 m_type = eTypeInvalid;
342 if (value_str.empty()) {
343 error.SetErrorString("Invalid c-string value string.");
346 const uint32_t byte_size = reg_info->byte_size;
352 long double ldbl_val;
353 switch (reg_info->encoding) {
354 case eEncodingInvalid:
355 error.SetErrorString("Invalid encoding.");
359 if (byte_size > sizeof(uint64_t)) {
360 error.SetErrorStringWithFormat(
361 "unsupported unsigned integer byte size: %u", byte_size);
364 if (value_str.getAsInteger(0, uval64)) {
365 error.SetErrorStringWithFormat(
366 "'%s' is not a valid unsigned integer string value",
367 value_str.str().c_str());
371 if (!Args::UInt64ValueIsValidForByteSize(uval64, byte_size)) {
372 error.SetErrorStringWithFormat(
374 " is too large to fit in a %u byte unsigned integer value",
379 if (!SetUInt(uval64, reg_info->byte_size)) {
380 error.SetErrorStringWithFormat(
381 "unsupported unsigned integer byte size: %u", byte_size);
387 if (byte_size > sizeof(long long)) {
388 error.SetErrorStringWithFormat("unsupported signed integer byte size: %u",
393 if (value_str.getAsInteger(0, ival64)) {
394 error.SetErrorStringWithFormat(
395 "'%s' is not a valid signed integer string value",
396 value_str.str().c_str());
400 if (!Args::SInt64ValueIsValidForByteSize(ival64, byte_size)) {
401 error.SetErrorStringWithFormat(
403 " is too large to fit in a %u byte signed integer value",
408 if (!SetUInt(ival64, reg_info->byte_size)) {
409 error.SetErrorStringWithFormat("unsupported signed integer byte size: %u",
415 case eEncodingIEEE754: {
416 std::string value_string = value_str;
417 if (byte_size == sizeof(float)) {
418 if (::sscanf(value_string.c_str(), "%f", &flt_val) != 1) {
419 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
420 value_string.c_str());
425 } else if (byte_size == sizeof(double)) {
426 if (::sscanf(value_string.c_str(), "%lf", &dbl_val) != 1) {
427 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
428 value_string.c_str());
432 m_type = eTypeDouble;
433 } else if (byte_size == sizeof(long double)) {
434 if (::sscanf(value_string.c_str(), "%Lf", &ldbl_val) != 1) {
435 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
436 value_string.c_str());
440 m_type = eTypeLongDouble;
442 error.SetErrorStringWithFormat("unsupported float byte size: %u",
448 case eEncodingVector:
449 if (!ParseVectorEncoding(reg_info, value_str, byte_size, this))
450 error.SetErrorString("unrecognized vector encoding string value.");
457 bool RegisterValue::SignExtend(uint32_t sign_bitpos) {
467 return m_scalar.SignExtend(sign_bitpos);
470 case eTypeLongDouble:
477 bool RegisterValue::CopyValue(const RegisterValue &rhs) {
479 return rhs.m_type != eTypeInvalid;
492 case eTypeLongDouble:
493 m_scalar = rhs.m_scalar;
496 assert(rhs.buffer.length <= kMaxRegisterByteSize);
497 ::memcpy(buffer.bytes, rhs.buffer.bytes, kMaxRegisterByteSize);
498 buffer.length = rhs.buffer.length;
499 buffer.byte_order = rhs.buffer.byte_order;
505 uint16_t RegisterValue::GetAsUInt16(uint16_t fail_value,
506 bool *success_ptr) const {
515 return m_scalar.UShort(fail_value);
517 switch (buffer.length) {
522 return *reinterpret_cast<const uint16_t *>(buffer.bytes);
527 *success_ptr = false;
531 uint32_t RegisterValue::GetAsUInt32(uint32_t fail_value,
532 bool *success_ptr) const {
543 case eTypeLongDouble:
544 return m_scalar.UInt(fail_value);
546 switch (buffer.length) {
552 return *reinterpret_cast<const uint32_t *>(buffer.bytes);
557 *success_ptr = false;
561 uint64_t RegisterValue::GetAsUInt64(uint64_t fail_value,
562 bool *success_ptr) const {
574 case eTypeLongDouble:
575 return m_scalar.ULongLong(fail_value);
577 switch (buffer.length) {
581 return *(const uint8_t *)buffer.bytes;
583 return *reinterpret_cast<const uint16_t *>(buffer.bytes);
585 return *reinterpret_cast<const uint32_t *>(buffer.bytes);
587 return *reinterpret_cast<const uint64_t *>(buffer.bytes);
592 *success_ptr = false;
596 llvm::APInt RegisterValue::GetAsUInt128(const llvm::APInt &fail_value,
597 bool *success_ptr) const {
610 case eTypeLongDouble:
611 return m_scalar.UInt128(fail_value);
613 switch (buffer.length) {
621 return llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
622 (reinterpret_cast<const type128 *>(buffer.bytes))->x);
627 *success_ptr = false;
631 float RegisterValue::GetAsFloat(float fail_value, bool *success_ptr) const {
642 case eTypeLongDouble:
643 return m_scalar.Float(fail_value);
646 *success_ptr = false;
650 double RegisterValue::GetAsDouble(double fail_value, bool *success_ptr) const {
662 case eTypeLongDouble:
663 return m_scalar.Double(fail_value);
666 *success_ptr = false;
670 long double RegisterValue::GetAsLongDouble(long double fail_value,
671 bool *success_ptr) const {
683 case eTypeLongDouble:
684 return m_scalar.LongDouble();
687 *success_ptr = false;
691 const void *RegisterValue::GetBytes() const {
702 case eTypeLongDouble:
703 return m_scalar.GetBytes();
710 uint32_t RegisterValue::GetByteSize() const {
723 case eTypeLongDouble:
724 return m_scalar.GetByteSize();
726 return buffer.length;
731 bool RegisterValue::SetUInt(uint64_t uint, uint32_t byte_size) {
732 if (byte_size == 0) {
734 } else if (byte_size == 1) {
736 } else if (byte_size <= 2) {
738 } else if (byte_size <= 4) {
740 } else if (byte_size <= 8) {
742 } else if (byte_size <= 16) {
743 SetUInt128(llvm::APInt(128, uint));
749 void RegisterValue::SetBytes(const void *bytes, size_t length,
750 lldb::ByteOrder byte_order) {
751 // If this assertion fires off we need to increase the size of buffer.bytes,
752 // or make it something that is allocated on the heap. Since the data buffer
753 // is in a union, we can't make it a collection class like SmallVector...
754 if (bytes && length > 0) {
755 assert(length <= sizeof(buffer.bytes) &&
756 "Storing too many bytes in a RegisterValue.");
758 buffer.length = length;
759 memcpy(buffer.bytes, bytes, length);
760 buffer.byte_order = byte_order;
762 m_type = eTypeInvalid;
767 bool RegisterValue::operator==(const RegisterValue &rhs) const {
768 if (m_type == rhs.m_type) {
779 case eTypeLongDouble:
780 return m_scalar == rhs.m_scalar;
782 if (buffer.length != rhs.buffer.length)
785 uint8_t length = buffer.length;
786 if (length > kMaxRegisterByteSize)
787 length = kMaxRegisterByteSize;
788 return memcmp(buffer.bytes, rhs.buffer.bytes, length) == 0;
796 bool RegisterValue::operator!=(const RegisterValue &rhs) const {
797 return !(*this == rhs);
800 bool RegisterValue::ClearBit(uint32_t bit) {
810 if (bit < (GetByteSize() * 8)) {
811 return m_scalar.ClearBit(bit);
817 case eTypeLongDouble:
821 if (buffer.byte_order == eByteOrderBig ||
822 buffer.byte_order == eByteOrderLittle) {
824 if (buffer.byte_order == eByteOrderBig)
825 byte_idx = buffer.length - (bit / 8) - 1;
829 const uint32_t byte_bit = bit % 8;
830 if (byte_idx < buffer.length) {
831 buffer.bytes[byte_idx] &= ~(1u << byte_bit);
840 bool RegisterValue::SetBit(uint32_t bit) {
850 if (bit < (GetByteSize() * 8)) {
851 return m_scalar.SetBit(bit);
857 case eTypeLongDouble:
861 if (buffer.byte_order == eByteOrderBig ||
862 buffer.byte_order == eByteOrderLittle) {
864 if (buffer.byte_order == eByteOrderBig)
865 byte_idx = buffer.length - (bit / 8) - 1;
869 const uint32_t byte_bit = bit % 8;
870 if (byte_idx < buffer.length) {
871 buffer.bytes[byte_idx] |= (1u << byte_bit);