1 //===-- RegisterValue.cpp -------------------------------------------------===//
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/DataExtractor.h"
12 #include "lldb/Utility/Scalar.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/Stream.h"
15 #include "lldb/Utility/StreamString.h"
16 #include "lldb/lldb-defines.h"
17 #include "lldb/lldb-private-types.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.h"
32 using namespace lldb_private
;
34 bool RegisterValue::GetData(DataExtractor
&data
) const {
35 return data
.SetData(GetBytes(), GetByteSize(), GetByteOrder()) > 0;
38 uint32_t RegisterValue::GetAsMemoryData(const RegisterInfo
®_info
, void *dst
,
40 lldb::ByteOrder dst_byte_order
,
41 Status
&error
) const {
42 // ReadRegister should have already been called on this object prior to
44 if (GetType() == eTypeInvalid
) {
45 // No value has been read into this object...
46 error
= Status::FromErrorStringWithFormatv(
47 "invalid register value type for register {0}", reg_info
.name
);
51 const uint32_t src_len
= reg_info
.byte_size
;
53 // Extract the register data into a data extractor
54 DataExtractor reg_data
;
55 if (!GetData(reg_data
)) {
56 error
= Status::FromErrorString("invalid register value to copy into");
60 // Prepare a memory buffer that contains some or all of the register value
61 const uint32_t bytes_copied
=
62 reg_data
.CopyByteOrderedData(0, // src offset
63 src_len
, // src length
65 dst_len
, // dst length
66 dst_byte_order
); // dst byte order
67 if (bytes_copied
== 0)
68 error
= Status::FromErrorStringWithFormat(
69 "failed to copy data for register write of %s", reg_info
.name
);
74 uint32_t RegisterValue::SetFromMemoryData(const RegisterInfo
®_info
,
75 const void *src
, uint32_t src_len
,
76 lldb::ByteOrder src_byte_order
,
78 // Moving from addr into a register
80 // Case 1: src_len == dst_len
82 // |AABBCCDD| Address contents
83 // |AABBCCDD| Register contents
85 // Case 2: src_len > dst_len
87 // Status! (The register should always be big enough to hold the data)
89 // Case 3: src_len < dst_len
91 // |AABB| Address contents
92 // |AABB0000| Register contents [on little-endian hardware]
93 // |0000AABB| Register contents [on big-endian hardware]
94 const uint32_t dst_len
= reg_info
.byte_size
;
96 if (src_len
> dst_len
) {
97 error
= Status::FromErrorStringWithFormat(
98 "%u bytes is too big to store in register %s (%u bytes)", src_len
,
99 reg_info
.name
, dst_len
);
103 // Use a data extractor to correctly copy and pad the bytes read into the
105 DataExtractor
src_data(src
, src_len
, src_byte_order
, 4);
107 error
= SetValueFromData(reg_info
, src_data
, 0, true);
111 // If SetValueFromData succeeded, we must have copied all of src_len
115 bool RegisterValue::GetScalarValue(Scalar
&scalar
) const {
120 DataExtractor
data(buffer
.bytes
.data(), buffer
.bytes
.size(),
121 buffer
.byte_order
, 1);
122 if (scalar
.SetValueFromData(data
, lldb::eEncodingUint
, buffer
.bytes
.size())
133 case eTypeLongDouble
:
140 void RegisterValue::Clear() { m_type
= eTypeInvalid
; }
142 RegisterValue::Type
RegisterValue::SetType(const RegisterInfo
®_info
) {
143 // To change the type, we simply copy the data in again, using the new format
145 DataExtractor copy_data
;
146 if (copy
.CopyValue(*this) && copy
.GetData(copy_data
)) {
147 Status error
= SetValueFromData(reg_info
, copy_data
, 0, true);
148 assert(error
.Success() && "Expected SetValueFromData to succeed.");
149 UNUSED_IF_ASSERT_DISABLED(error
);
155 Status
RegisterValue::SetValueFromData(const RegisterInfo
®_info
,
157 lldb::offset_t src_offset
,
158 bool partial_data_ok
) {
161 if (src
.GetByteSize() == 0) {
162 error
= Status::FromErrorString("empty data.");
166 if (reg_info
.byte_size
== 0) {
167 error
= Status::FromErrorString("invalid register info.");
171 uint32_t src_len
= src
.GetByteSize() - src_offset
;
173 if (!partial_data_ok
&& (src_len
< reg_info
.byte_size
)) {
174 error
= Status::FromErrorString("not enough data.");
178 // Cap the data length if there is more than enough bytes for this register
180 if (src_len
> reg_info
.byte_size
)
181 src_len
= reg_info
.byte_size
;
185 m_type
= eTypeInvalid
;
186 switch (reg_info
.encoding
) {
187 case eEncodingInvalid
:
191 if (reg_info
.byte_size
== 1)
192 SetUInt8(src
.GetMaxU32(&src_offset
, src_len
));
193 else if (reg_info
.byte_size
<= 2)
194 SetUInt16(src
.GetMaxU32(&src_offset
, src_len
));
195 else if (reg_info
.byte_size
<= 4)
196 SetUInt32(src
.GetMaxU32(&src_offset
, src_len
));
197 else if (reg_info
.byte_size
<= 8)
198 SetUInt64(src
.GetMaxU64(&src_offset
, src_len
));
199 else if (reg_info
.byte_size
<= 16) {
200 uint64_t data1
= src
.GetU64(&src_offset
);
201 uint64_t data2
= src
.GetU64(&src_offset
);
202 if (src
.GetByteOrder() == eByteOrderBig
) {
209 SetUInt128(llvm::APInt(128, 2, int128
.x
));
212 case eEncodingIEEE754
:
213 if (reg_info
.byte_size
== sizeof(float))
214 SetFloat(src
.GetFloat(&src_offset
));
215 else if (reg_info
.byte_size
== sizeof(double))
216 SetDouble(src
.GetDouble(&src_offset
));
217 else if (reg_info
.byte_size
== sizeof(long double))
218 SetLongDouble(src
.GetLongDouble(&src_offset
));
220 case eEncodingVector
: {
222 assert(reg_info
.byte_size
<= kMaxRegisterByteSize
);
223 buffer
.bytes
.resize(reg_info
.byte_size
);
224 buffer
.byte_order
= src
.GetByteOrder();
225 if (src
.CopyByteOrderedData(
226 src_offset
, // offset within "src" to start extracting data
227 src_len
, // src length
228 buffer
.bytes
.data(), // dst buffer
229 buffer
.bytes
.size(), // dst length
230 buffer
.byte_order
) == 0) // dst byte order
232 error
= Status::FromErrorStringWithFormat(
233 "failed to copy data for register write of %s", reg_info
.name
);
239 if (m_type
== eTypeInvalid
)
240 error
= Status::FromErrorStringWithFormat(
241 "invalid register value type for register %s", reg_info
.name
);
245 // Helper function for RegisterValue::SetValueFromString()
246 static bool ParseVectorEncoding(const RegisterInfo
*reg_info
,
247 llvm::StringRef vector_str
,
248 const uint32_t byte_size
,
249 RegisterValue
*reg_value
) {
250 // Example: vector_str = "{0x2c 0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a
251 // 0x2a 0x3e 0x84 0x4f 0x2a 0x3e}".
252 vector_str
= vector_str
.trim();
253 vector_str
.consume_front("{");
254 vector_str
.consume_back("}");
255 vector_str
= vector_str
.trim();
259 // The first split should give us:
260 // ('0x2c', '0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f
263 llvm::StringRef cdr
= vector_str
;
264 std::tie(car
, cdr
) = vector_str
.split(Sep
);
265 std::vector
<uint8_t> bytes
;
268 // Using radix auto-sensing by passing 0 as the radix. Keep on processing the
269 // vector elements as long as the parsing succeeds and the vector size is <
271 while (!car
.getAsInteger(0, byte
) && bytes
.size() < byte_size
) {
272 bytes
.push_back(byte
);
273 std::tie(car
, cdr
) = cdr
.split(Sep
);
276 // Check for vector of exact byte_size elements.
277 if (bytes
.size() != byte_size
)
280 reg_value
->SetBytes(&(bytes
.front()), byte_size
, eByteOrderLittle
);
284 static bool UInt64ValueIsValidForByteSize(uint64_t uval64
,
285 size_t total_byte_size
) {
286 if (total_byte_size
> 8)
289 if (total_byte_size
== 8)
293 (static_cast<uint64_t>(1) << static_cast<uint64_t>(total_byte_size
* 8)) -
295 return uval64
<= max
;
298 static bool SInt64ValueIsValidForByteSize(int64_t sval64
,
299 size_t total_byte_size
) {
300 if (total_byte_size
> 8)
303 if (total_byte_size
== 8)
306 const int64_t max
= (static_cast<int64_t>(1)
307 << static_cast<uint64_t>(total_byte_size
* 8 - 1)) -
309 const int64_t min
= ~(max
);
310 return min
<= sval64
&& sval64
<= max
;
313 Status
RegisterValue::SetValueFromString(const RegisterInfo
*reg_info
,
314 llvm::StringRef value_str
) {
316 if (reg_info
== nullptr) {
317 error
= Status::FromErrorString("Invalid register info argument.");
321 m_type
= eTypeInvalid
;
322 if (value_str
.empty()) {
323 error
= Status::FromErrorString("Invalid c-string value string.");
326 const uint32_t byte_size
= reg_info
->byte_size
;
332 long double ldbl_val
;
333 switch (reg_info
->encoding
) {
334 case eEncodingInvalid
:
335 error
= Status::FromErrorString("Invalid encoding.");
339 if (byte_size
> sizeof(uint64_t)) {
340 error
= Status::FromErrorStringWithFormat(
341 "unsupported unsigned integer byte size: %u", byte_size
);
344 if (value_str
.getAsInteger(0, uval64
)) {
345 error
= Status::FromErrorStringWithFormatv(
346 "'{0}' is not a valid unsigned integer string value", value_str
);
350 if (!UInt64ValueIsValidForByteSize(uval64
, byte_size
)) {
351 error
= Status::FromErrorStringWithFormat(
353 " is too large to fit in a %u byte unsigned integer value",
358 if (!SetUInt(uval64
, reg_info
->byte_size
)) {
359 error
= Status::FromErrorStringWithFormat(
360 "unsupported unsigned integer byte size: %u", byte_size
);
366 if (byte_size
> sizeof(long long)) {
367 error
= Status::FromErrorStringWithFormat(
368 "unsupported signed integer byte size: %u", byte_size
);
372 if (value_str
.getAsInteger(0, ival64
)) {
373 error
= Status::FromErrorStringWithFormatv(
374 "'{0}' is not a valid signed integer string value", value_str
);
378 if (!SInt64ValueIsValidForByteSize(ival64
, byte_size
)) {
379 error
= Status::FromErrorStringWithFormat(
381 " is too large to fit in a %u byte signed integer value",
386 if (!SetUInt(ival64
, reg_info
->byte_size
)) {
387 error
= Status::FromErrorStringWithFormat(
388 "unsupported signed integer byte size: %u", byte_size
);
393 case eEncodingIEEE754
: {
394 std::string value_string
= std::string(value_str
);
395 if (byte_size
== sizeof(float)) {
396 if (::sscanf(value_string
.c_str(), "%f", &flt_val
) != 1) {
397 error
= Status::FromErrorStringWithFormat(
398 "'%s' is not a valid float string value", value_string
.c_str());
403 } else if (byte_size
== sizeof(double)) {
404 if (::sscanf(value_string
.c_str(), "%lf", &dbl_val
) != 1) {
405 error
= Status::FromErrorStringWithFormat(
406 "'%s' is not a valid float string value", value_string
.c_str());
410 m_type
= eTypeDouble
;
411 } else if (byte_size
== sizeof(long double)) {
412 if (::sscanf(value_string
.c_str(), "%Lf", &ldbl_val
) != 1) {
413 error
= Status::FromErrorStringWithFormat(
414 "'%s' is not a valid float string value", value_string
.c_str());
418 m_type
= eTypeLongDouble
;
420 error
= Status::FromErrorStringWithFormat(
421 "unsupported float byte size: %u", byte_size
);
426 case eEncodingVector
:
427 if (!ParseVectorEncoding(reg_info
, value_str
, byte_size
, this))
429 Status::FromErrorString("unrecognized vector encoding string value.");
436 bool RegisterValue::SignExtend(uint32_t sign_bitpos
) {
446 return m_scalar
.SignExtend(sign_bitpos
);
449 case eTypeLongDouble
:
456 bool RegisterValue::CopyValue(const RegisterValue
&rhs
) {
458 return rhs
.m_type
!= eTypeInvalid
;
471 case eTypeLongDouble
:
472 m_scalar
= rhs
.m_scalar
;
475 buffer
.bytes
= rhs
.buffer
.bytes
;
476 buffer
.byte_order
= rhs
.buffer
.byte_order
;
482 uint16_t RegisterValue::GetAsUInt16(uint16_t fail_value
,
483 bool *success_ptr
) const {
492 return m_scalar
.UShort(fail_value
);
494 switch (buffer
.bytes
.size()) {
499 return *reinterpret_cast<const uint16_t *>(buffer
.bytes
.data());
504 *success_ptr
= false;
508 uint32_t RegisterValue::GetAsUInt32(uint32_t fail_value
,
509 bool *success_ptr
) const {
520 case eTypeLongDouble
:
521 return m_scalar
.UInt(fail_value
);
523 switch (buffer
.bytes
.size()) {
529 return *reinterpret_cast<const uint32_t *>(buffer
.bytes
.data());
534 *success_ptr
= false;
538 uint64_t RegisterValue::GetAsUInt64(uint64_t fail_value
,
539 bool *success_ptr
) const {
551 case eTypeLongDouble
:
552 return m_scalar
.ULongLong(fail_value
);
554 switch (buffer
.bytes
.size()) {
558 return *(const uint8_t *)buffer
.bytes
.data();
560 return *reinterpret_cast<const uint16_t *>(buffer
.bytes
.data());
562 return *reinterpret_cast<const uint32_t *>(buffer
.bytes
.data());
564 return *reinterpret_cast<const uint64_t *>(buffer
.bytes
.data());
569 *success_ptr
= false;
573 llvm::APInt
RegisterValue::GetAsUInt128(const llvm::APInt
&fail_value
,
574 bool *success_ptr
) const {
587 case eTypeLongDouble
:
588 return m_scalar
.UInt128(fail_value
);
590 switch (buffer
.bytes
.size()) {
599 BITWIDTH_INT128
, NUM_OF_WORDS_INT128
,
600 (reinterpret_cast<const type128
*>(buffer
.bytes
.data()))->x
);
605 *success_ptr
= false;
609 float RegisterValue::GetAsFloat(float fail_value
, bool *success_ptr
) const {
620 case eTypeLongDouble
:
621 return m_scalar
.Float(fail_value
);
624 *success_ptr
= false;
628 double RegisterValue::GetAsDouble(double fail_value
, bool *success_ptr
) const {
640 case eTypeLongDouble
:
641 return m_scalar
.Double(fail_value
);
644 *success_ptr
= false;
648 long double RegisterValue::GetAsLongDouble(long double fail_value
,
649 bool *success_ptr
) const {
661 case eTypeLongDouble
:
662 return m_scalar
.LongDouble();
665 *success_ptr
= false;
669 const void *RegisterValue::GetBytes() const {
680 case eTypeLongDouble
:
681 m_scalar
.GetBytes(buffer
.bytes
);
682 return buffer
.bytes
.data();
684 return buffer
.bytes
.data();
689 uint32_t RegisterValue::GetByteSize() const {
702 case eTypeLongDouble
:
703 return m_scalar
.GetByteSize();
705 return buffer
.bytes
.size();
710 bool RegisterValue::SetUInt(uint64_t uint
, uint32_t byte_size
) {
711 if (byte_size
== 0) {
713 } else if (byte_size
== 1) {
715 } else if (byte_size
<= 2) {
717 } else if (byte_size
<= 4) {
719 } else if (byte_size
<= 8) {
721 } else if (byte_size
<= 16) {
722 SetUInt128(llvm::APInt(128, uint
));
728 void RegisterValue::SetBytes(const void *bytes
, size_t length
,
729 lldb::ByteOrder byte_order
) {
730 if (bytes
&& length
> 0) {
732 buffer
.bytes
.resize(length
);
733 memcpy(buffer
.bytes
.data(), bytes
, length
);
734 buffer
.byte_order
= byte_order
;
736 m_type
= eTypeInvalid
;
737 buffer
.bytes
.resize(0);
741 bool RegisterValue::operator==(const RegisterValue
&rhs
) const {
742 if (m_type
== rhs
.m_type
) {
753 case eTypeLongDouble
:
754 return m_scalar
== rhs
.m_scalar
;
756 return buffer
.bytes
== rhs
.buffer
.bytes
;
762 bool RegisterValue::operator!=(const RegisterValue
&rhs
) const {
763 return !(*this == rhs
);
766 bool RegisterValue::ClearBit(uint32_t bit
) {
776 if (bit
< (GetByteSize() * 8)) {
777 return m_scalar
.ClearBit(bit
);
783 case eTypeLongDouble
:
787 if (buffer
.byte_order
== eByteOrderBig
||
788 buffer
.byte_order
== eByteOrderLittle
) {
790 if (buffer
.byte_order
== eByteOrderBig
)
791 byte_idx
= buffer
.bytes
.size() - (bit
/ 8) - 1;
795 const uint32_t byte_bit
= bit
% 8;
796 if (byte_idx
< buffer
.bytes
.size()) {
797 buffer
.bytes
[byte_idx
] &= ~(1u << byte_bit
);
806 bool RegisterValue::SetBit(uint32_t bit
) {
816 if (bit
< (GetByteSize() * 8)) {
817 return m_scalar
.SetBit(bit
);
823 case eTypeLongDouble
:
827 if (buffer
.byte_order
== eByteOrderBig
||
828 buffer
.byte_order
== eByteOrderLittle
) {
830 if (buffer
.byte_order
== eByteOrderBig
)
831 byte_idx
= buffer
.bytes
.size() - (bit
/ 8) - 1;
835 const uint32_t byte_bit
= bit
% 8;
836 if (byte_idx
< buffer
.bytes
.size()) {
837 buffer
.bytes
[byte_idx
] |= (1u << byte_bit
);