1 //===-- RegisterValue.cpp ---------------------------------------*- 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 #include "lldb/Core/RegisterValue.h"
12 #include "lldb/Core/Scalar.h"
13 #include "lldb/Utility/Args.h"
14 #include "lldb/Utility/DataExtractor.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/Utility/Stream.h"
17 #include "lldb/Utility/StreamString.h"
18 #include "lldb/lldb-defines.h" // for LLDB_INVALID_ADDRESS
19 #include "lldb/lldb-private-types.h" // for RegisterInfo, type128
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/StringRef.h"
24 #include <cstdint> // for uint8_t, uint32_t, uint64_t
25 #include <string> // for string
26 #include <tuple> // for tie, tuple
29 #include <assert.h> // for assert
30 #include <inttypes.h> // for PRIx64
31 #include <stdio.h> // for sscanf
34 using namespace lldb_private;
36 bool RegisterValue::GetData(DataExtractor &data) const {
37 return data.SetData(GetBytes(), GetByteSize(), GetByteOrder()) > 0;
40 uint32_t RegisterValue::GetAsMemoryData(const RegisterInfo *reg_info, void *dst,
42 lldb::ByteOrder dst_byte_order,
43 Status &error) const {
44 if (reg_info == nullptr) {
45 error.SetErrorString("invalid register info argument.");
49 // ReadRegister should have already been called on this object prior to
51 if (GetType() == eTypeInvalid) {
52 // No value has been read into this object...
53 error.SetErrorStringWithFormat(
54 "invalid register value type for register %s", reg_info->name);
58 if (dst_len > kMaxRegisterByteSize) {
59 error.SetErrorString("destination is too big");
63 const uint32_t src_len = reg_info->byte_size;
65 // Extract the register data into a data extractor
66 DataExtractor reg_data;
67 if (!GetData(reg_data)) {
68 error.SetErrorString("invalid register value to copy into");
72 // Prepare a memory buffer that contains some or all of the register value
73 const uint32_t bytes_copied =
74 reg_data.CopyByteOrderedData(0, // src offset
75 src_len, // src length
77 dst_len, // dst length
78 dst_byte_order); // dst byte order
79 if (bytes_copied == 0)
80 error.SetErrorStringWithFormat(
81 "failed to copy data for register write of %s", reg_info->name);
86 uint32_t RegisterValue::SetFromMemoryData(const RegisterInfo *reg_info,
87 const void *src, uint32_t src_len,
88 lldb::ByteOrder src_byte_order,
90 if (reg_info == nullptr) {
91 error.SetErrorString("invalid register info argument.");
95 // Moving from addr into a register
97 // Case 1: src_len == dst_len
99 // |AABBCCDD| Address contents
100 // |AABBCCDD| Register contents
102 // Case 2: src_len > dst_len
104 // Status! (The register should always be big enough to hold the data)
106 // Case 3: src_len < dst_len
108 // |AABB| Address contents
109 // |AABB0000| Register contents [on little-endian hardware]
110 // |0000AABB| Register contents [on big-endian hardware]
111 if (src_len > kMaxRegisterByteSize) {
112 error.SetErrorStringWithFormat(
113 "register buffer is too small to receive %u bytes of data.", src_len);
117 const uint32_t dst_len = reg_info->byte_size;
119 if (src_len > dst_len) {
120 error.SetErrorStringWithFormat(
121 "%u bytes is too big to store in register %s (%u bytes)", src_len,
122 reg_info->name, dst_len);
126 // Use a data extractor to correctly copy and pad the bytes read into the
128 DataExtractor src_data(src, src_len, src_byte_order, 4);
130 error = SetValueFromData(reg_info, src_data, 0, true);
134 // If SetValueFromData succeeded, we must have copied all of src_len
138 bool RegisterValue::GetScalarValue(Scalar &scalar) const {
143 switch (buffer.length) {
147 scalar = *(const uint8_t *)buffer.bytes;
150 scalar = *(const uint16_t *)buffer.bytes;
153 scalar = *(const uint32_t *)buffer.bytes;
156 scalar = *(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>((const uint64_t *)buffer.bytes,
179 case eTypeLongDouble:
186 void RegisterValue::Clear() { m_type = eTypeInvalid; }
188 RegisterValue::Type RegisterValue::SetType(const RegisterInfo *reg_info) {
189 // To change the type, we simply copy the data in again, using the new format
191 DataExtractor copy_data;
192 if (copy.CopyValue(*this) && copy.GetData(copy_data))
193 SetValueFromData(reg_info, copy_data, 0, true);
198 Status RegisterValue::SetValueFromData(const RegisterInfo *reg_info,
200 lldb::offset_t src_offset,
201 bool partial_data_ok) {
204 if (src.GetByteSize() == 0) {
205 error.SetErrorString("empty data.");
209 if (reg_info->byte_size == 0) {
210 error.SetErrorString("invalid register info.");
214 uint32_t src_len = src.GetByteSize() - src_offset;
216 if (!partial_data_ok && (src_len < reg_info->byte_size)) {
217 error.SetErrorString("not enough data.");
221 // Cap the data length if there is more than enough bytes for this register
223 if (src_len > reg_info->byte_size)
224 src_len = reg_info->byte_size;
226 // Zero out the value in case we get partial data...
227 memset(buffer.bytes, 0, sizeof(buffer.bytes));
231 m_type = eTypeInvalid;
232 switch (reg_info->encoding) {
233 case eEncodingInvalid:
237 if (reg_info->byte_size == 1)
238 SetUInt8(src.GetMaxU32(&src_offset, src_len));
239 else if (reg_info->byte_size <= 2)
240 SetUInt16(src.GetMaxU32(&src_offset, src_len));
241 else if (reg_info->byte_size <= 4)
242 SetUInt32(src.GetMaxU32(&src_offset, src_len));
243 else if (reg_info->byte_size <= 8)
244 SetUInt64(src.GetMaxU64(&src_offset, src_len));
245 else if (reg_info->byte_size <= 16) {
246 uint64_t data1 = src.GetU64(&src_offset);
247 uint64_t data2 = src.GetU64(&src_offset);
248 if (src.GetByteSize() == eByteOrderBig) {
255 SetUInt128(llvm::APInt(128, 2, int128.x));
258 case eEncodingIEEE754:
259 if (reg_info->byte_size == sizeof(float))
260 SetFloat(src.GetFloat(&src_offset));
261 else if (reg_info->byte_size == sizeof(double))
262 SetDouble(src.GetDouble(&src_offset));
263 else if (reg_info->byte_size == sizeof(long double))
264 SetLongDouble(src.GetLongDouble(&src_offset));
266 case eEncodingVector: {
268 buffer.length = reg_info->byte_size;
269 buffer.byte_order = src.GetByteOrder();
270 assert(buffer.length <= kMaxRegisterByteSize);
271 if (buffer.length > kMaxRegisterByteSize)
272 buffer.length = kMaxRegisterByteSize;
273 if (src.CopyByteOrderedData(
274 src_offset, // offset within "src" to start extracting data
275 src_len, // src length
276 buffer.bytes, // dst buffer
277 buffer.length, // dst length
278 buffer.byte_order) == 0) // dst byte order
280 error.SetErrorStringWithFormat(
281 "failed to copy data for register write of %s", reg_info->name);
287 if (m_type == eTypeInvalid)
288 error.SetErrorStringWithFormat(
289 "invalid register value type for register %s", reg_info->name);
293 // Helper function for RegisterValue::SetValueFromString()
294 static bool ParseVectorEncoding(const RegisterInfo *reg_info,
295 llvm::StringRef vector_str,
296 const uint32_t byte_size,
297 RegisterValue *reg_value) {
298 // Example: vector_str = "{0x2c 0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a
299 // 0x2a 0x3e 0x84 0x4f 0x2a 0x3e}".
300 vector_str = vector_str.trim();
301 vector_str.consume_front("{");
302 vector_str.consume_back("}");
303 vector_str = vector_str.trim();
307 // The first split should give us:
308 // ('0x2c', '0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f
311 llvm::StringRef cdr = vector_str;
312 std::tie(car, cdr) = vector_str.split(Sep);
313 std::vector<uint8_t> bytes;
316 // Using radix auto-sensing by passing 0 as the radix. Keep on processing the
317 // vector elements as long as the parsing succeeds and the vector size is <
319 while (!car.getAsInteger(0, byte) && bytes.size() < byte_size) {
320 bytes.push_back(byte);
321 std::tie(car, cdr) = cdr.split(Sep);
324 // Check for vector of exact byte_size elements.
325 if (bytes.size() != byte_size)
328 reg_value->SetBytes(&(bytes.front()), byte_size, eByteOrderLittle);
332 Status RegisterValue::SetValueFromString(const RegisterInfo *reg_info,
333 llvm::StringRef value_str) {
335 if (reg_info == nullptr) {
336 error.SetErrorString("Invalid register info argument.");
340 m_type = eTypeInvalid;
341 if (value_str.empty()) {
342 error.SetErrorString("Invalid c-string value string.");
345 const uint32_t byte_size = reg_info->byte_size;
351 long double ldbl_val;
352 switch (reg_info->encoding) {
353 case eEncodingInvalid:
354 error.SetErrorString("Invalid encoding.");
358 if (byte_size > sizeof(uint64_t)) {
359 error.SetErrorStringWithFormat(
360 "unsupported unsigned integer byte size: %u", byte_size);
363 if (value_str.getAsInteger(0, uval64)) {
364 error.SetErrorStringWithFormat(
365 "'%s' is not a valid unsigned integer string value",
366 value_str.str().c_str());
370 if (!Args::UInt64ValueIsValidForByteSize(uval64, byte_size)) {
371 error.SetErrorStringWithFormat(
373 " is too large to fit in a %u byte unsigned integer value",
378 if (!SetUInt(uval64, reg_info->byte_size)) {
379 error.SetErrorStringWithFormat(
380 "unsupported unsigned integer byte size: %u", byte_size);
383 // TODO: Shouldn't we be setting m_type here?
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",
414 // TODO: Shouldn't we be setting m_type here?
417 case eEncodingIEEE754: {
418 std::string value_string = value_str;
419 if (byte_size == sizeof(float)) {
420 if (::sscanf(value_string.c_str(), "%f", &flt_val) != 1) {
421 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
422 value_string.c_str());
427 } else if (byte_size == sizeof(double)) {
428 if (::sscanf(value_string.c_str(), "%lf", &dbl_val) != 1) {
429 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
430 value_string.c_str());
434 m_type = eTypeDouble;
435 } else if (byte_size == sizeof(long double)) {
436 if (::sscanf(value_string.c_str(), "%Lf", &ldbl_val) != 1) {
437 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
438 value_string.c_str());
442 m_type = eTypeLongDouble;
444 error.SetErrorStringWithFormat("unsupported float byte size: %u",
450 case eEncodingVector:
451 if (!ParseVectorEncoding(reg_info, value_str, byte_size, this))
452 error.SetErrorString("unrecognized vector encoding string value.");
459 bool RegisterValue::SignExtend(uint32_t sign_bitpos) {
469 return m_scalar.SignExtend(sign_bitpos);
472 case eTypeLongDouble:
479 bool RegisterValue::CopyValue(const RegisterValue &rhs) {
481 return rhs.m_type == eTypeInvalid ? false : true;
494 case eTypeLongDouble:
495 m_scalar = rhs.m_scalar;
498 assert(rhs.buffer.length <= kMaxRegisterByteSize);
499 ::memcpy(buffer.bytes, rhs.buffer.bytes, kMaxRegisterByteSize);
500 buffer.length = rhs.buffer.length;
501 buffer.byte_order = rhs.buffer.byte_order;
507 uint16_t RegisterValue::GetAsUInt16(uint16_t fail_value,
508 bool *success_ptr) const {
517 return m_scalar.UShort(fail_value);
519 switch (buffer.length) {
524 return *(const uint16_t *)buffer.bytes;
529 *success_ptr = false;
533 uint32_t RegisterValue::GetAsUInt32(uint32_t fail_value,
534 bool *success_ptr) const {
545 case eTypeLongDouble:
546 return m_scalar.UInt(fail_value);
548 switch (buffer.length) {
554 return *(const uint32_t *)buffer.bytes;
559 *success_ptr = false;
563 uint64_t RegisterValue::GetAsUInt64(uint64_t fail_value,
564 bool *success_ptr) const {
576 case eTypeLongDouble:
577 return m_scalar.ULongLong(fail_value);
579 switch (buffer.length) {
583 return *(const uint8_t *)buffer.bytes;
585 return *(const uint16_t *)buffer.bytes;
587 return *(const uint32_t *)buffer.bytes;
589 return *(const uint64_t *)buffer.bytes;
594 *success_ptr = false;
598 llvm::APInt RegisterValue::GetAsUInt128(const llvm::APInt &fail_value,
599 bool *success_ptr) const {
612 case eTypeLongDouble:
613 return m_scalar.UInt128(fail_value);
615 switch (buffer.length) {
623 return llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
624 ((const type128 *)buffer.bytes)->x);
629 *success_ptr = false;
633 float RegisterValue::GetAsFloat(float fail_value, bool *success_ptr) const {
644 case eTypeLongDouble:
645 return m_scalar.Float(fail_value);
648 *success_ptr = false;
652 double RegisterValue::GetAsDouble(double fail_value, bool *success_ptr) const {
664 case eTypeLongDouble:
665 return m_scalar.Double(fail_value);
668 *success_ptr = false;
672 long double RegisterValue::GetAsLongDouble(long double fail_value,
673 bool *success_ptr) const {
685 case eTypeLongDouble:
686 return m_scalar.LongDouble();
689 *success_ptr = false;
693 const void *RegisterValue::GetBytes() const {
704 case eTypeLongDouble:
705 return m_scalar.GetBytes();
712 uint32_t RegisterValue::GetByteSize() const {
725 case eTypeLongDouble:
726 return m_scalar.GetByteSize();
728 return buffer.length;
733 bool RegisterValue::SetUInt(uint64_t uint, uint32_t byte_size) {
734 if (byte_size == 0) {
736 } else if (byte_size == 1) {
738 } else if (byte_size <= 2) {
740 } else if (byte_size <= 4) {
742 } else if (byte_size <= 8) {
744 } else if (byte_size <= 16) {
745 SetUInt128(llvm::APInt(128, uint));
751 void RegisterValue::SetBytes(const void *bytes, size_t length,
752 lldb::ByteOrder byte_order) {
753 // If this assertion fires off we need to increase the size of buffer.bytes,
754 // or make it something that is allocated on the heap. Since the data buffer
755 // is in a union, we can't make it a collection class like SmallVector...
756 if (bytes && length > 0) {
757 assert(length <= sizeof(buffer.bytes) &&
758 "Storing too many bytes in a RegisterValue.");
760 buffer.length = length;
761 memcpy(buffer.bytes, bytes, length);
762 buffer.byte_order = byte_order;
764 m_type = eTypeInvalid;
769 bool RegisterValue::operator==(const RegisterValue &rhs) const {
770 if (m_type == rhs.m_type) {
781 case eTypeLongDouble:
782 return m_scalar == rhs.m_scalar;
784 if (buffer.length != rhs.buffer.length)
787 uint8_t length = buffer.length;
788 if (length > kMaxRegisterByteSize)
789 length = kMaxRegisterByteSize;
790 return memcmp(buffer.bytes, rhs.buffer.bytes, length) == 0;
798 bool RegisterValue::operator!=(const RegisterValue &rhs) const {
799 if (m_type != rhs.m_type)
811 case eTypeLongDouble:
812 return m_scalar != rhs.m_scalar;
814 if (buffer.length != rhs.buffer.length) {
817 uint8_t length = buffer.length;
818 if (length > kMaxRegisterByteSize)
819 length = kMaxRegisterByteSize;
820 return memcmp(buffer.bytes, rhs.buffer.bytes, length) != 0;
827 bool RegisterValue::ClearBit(uint32_t bit) {
837 if (bit < (GetByteSize() * 8)) {
838 return m_scalar.ClearBit(bit);
844 case eTypeLongDouble:
848 if (buffer.byte_order == eByteOrderBig ||
849 buffer.byte_order == eByteOrderLittle) {
851 if (buffer.byte_order == eByteOrderBig)
852 byte_idx = buffer.length - (bit / 8) - 1;
856 const uint32_t byte_bit = bit % 8;
857 if (byte_idx < buffer.length) {
858 buffer.bytes[byte_idx] &= ~(1u << byte_bit);
867 bool RegisterValue::SetBit(uint32_t bit) {
877 if (bit < (GetByteSize() * 8)) {
878 return m_scalar.SetBit(bit);
884 case eTypeLongDouble:
888 if (buffer.byte_order == eByteOrderBig ||
889 buffer.byte_order == eByteOrderLittle) {
891 if (buffer.byte_order == eByteOrderBig)
892 byte_idx = buffer.length - (bit / 8) - 1;
896 const uint32_t byte_bit = bit % 8;
897 if (byte_idx < buffer.length) {
898 buffer.bytes[byte_idx] |= (1u << byte_bit);