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"
16 // Other libraries and framework includes
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
21 #include "lldb/Core/DataExtractor.h"
22 #include "lldb/Core/Error.h"
23 #include "lldb/Core/Scalar.h"
24 #include "lldb/Core/Stream.h"
25 #include "lldb/Core/StreamString.h"
26 #include "lldb/Host/StringConvert.h"
27 #include "lldb/Interpreter/Args.h"
30 using namespace lldb_private;
32 bool RegisterValue::Dump(Stream *s, const RegisterInfo *reg_info,
33 bool prefix_with_name, bool prefix_with_alt_name,
35 uint32_t reg_name_right_align_at) const {
38 bool name_printed = false;
39 // For simplicity, alignment of the register name printing applies only
40 // in the most common case where:
42 // prefix_with_name^prefix_with_alt_name is true
44 StreamString format_string;
45 if (reg_name_right_align_at && (prefix_with_name ^ prefix_with_alt_name))
46 format_string.Printf("%%%us", reg_name_right_align_at);
48 format_string.Printf("%%s");
49 std::string fmt = format_string.GetString();
50 if (prefix_with_name) {
52 s->Printf(fmt.c_str(), reg_info->name);
54 } else if (reg_info->alt_name) {
55 s->Printf(fmt.c_str(), reg_info->alt_name);
56 prefix_with_alt_name = false;
60 if (prefix_with_alt_name) {
63 if (reg_info->alt_name) {
64 s->Printf(fmt.c_str(), reg_info->alt_name);
66 } else if (!name_printed) {
67 // No alternate name but we were asked to display a name, so show the
69 s->Printf(fmt.c_str(), reg_info->name);
76 if (format == eFormatDefault)
77 format = reg_info->format;
80 0, // Offset in "data"
81 format, // Format to use when dumping
82 reg_info->byte_size, // item_byte_size
84 UINT32_MAX, // num_per_line
85 LLDB_INVALID_ADDRESS, // base_addr
87 0); // item_bit_offset
93 bool RegisterValue::GetData(DataExtractor &data) const {
94 return data.SetData(GetBytes(), GetByteSize(), GetByteOrder()) > 0;
97 uint32_t RegisterValue::GetAsMemoryData(const RegisterInfo *reg_info, void *dst,
99 lldb::ByteOrder dst_byte_order,
100 Error &error) const {
101 if (reg_info == nullptr) {
102 error.SetErrorString("invalid register info argument.");
106 // ReadRegister should have already been called on this object prior to
108 if (GetType() == eTypeInvalid) {
109 // No value has been read into this object...
110 error.SetErrorStringWithFormat(
111 "invalid register value type for register %s", reg_info->name);
115 if (dst_len > kMaxRegisterByteSize) {
116 error.SetErrorString("destination is too big");
120 const uint32_t src_len = reg_info->byte_size;
122 // Extract the register data into a data extractor
123 DataExtractor reg_data;
124 if (!GetData(reg_data)) {
125 error.SetErrorString("invalid register value to copy into");
129 // Prepare a memory buffer that contains some or all of the register value
130 const uint32_t bytes_copied =
131 reg_data.CopyByteOrderedData(0, // src offset
132 src_len, // src length
134 dst_len, // dst length
135 dst_byte_order); // dst byte order
136 if (bytes_copied == 0)
137 error.SetErrorStringWithFormat(
138 "failed to copy data for register write of %s", reg_info->name);
143 uint32_t RegisterValue::SetFromMemoryData(const RegisterInfo *reg_info,
144 const void *src, uint32_t src_len,
145 lldb::ByteOrder src_byte_order,
147 if (reg_info == nullptr) {
148 error.SetErrorString("invalid register info argument.");
152 // Moving from addr into a register
154 // Case 1: src_len == dst_len
156 // |AABBCCDD| Address contents
157 // |AABBCCDD| Register contents
159 // Case 2: src_len > dst_len
161 // Error! (The register should always be big enough to hold the data)
163 // Case 3: src_len < dst_len
165 // |AABB| Address contents
166 // |AABB0000| Register contents [on little-endian hardware]
167 // |0000AABB| Register contents [on big-endian hardware]
168 if (src_len > kMaxRegisterByteSize) {
169 error.SetErrorStringWithFormat(
170 "register buffer is too small to receive %u bytes of data.", src_len);
174 const uint32_t dst_len = reg_info->byte_size;
176 if (src_len > dst_len) {
177 error.SetErrorStringWithFormat(
178 "%u bytes is too big to store in register %s (%u bytes)", src_len,
179 reg_info->name, dst_len);
183 // Use a data extractor to correctly copy and pad the bytes read into the
185 DataExtractor src_data(src, src_len, src_byte_order, 4);
187 error = SetValueFromData(reg_info, src_data, 0, true);
191 // If SetValueFromData succeeded, we must have copied all of src_len
195 bool RegisterValue::GetScalarValue(Scalar &scalar) const {
200 switch (buffer.length) {
204 scalar = *(const uint8_t *)buffer.bytes;
207 scalar = *(const uint16_t *)buffer.bytes;
210 scalar = *(const uint32_t *)buffer.bytes;
213 scalar = *(const uint64_t *)buffer.bytes;
217 if (buffer.length % sizeof(uint64_t) == 0) {
218 const auto length_in_bits = buffer.length * 8;
219 const auto length_in_uint64 = buffer.length / sizeof(uint64_t);
221 llvm::APInt(length_in_bits,
222 llvm::ArrayRef<uint64_t>((const uint64_t *)buffer.bytes,
236 case eTypeLongDouble:
243 void RegisterValue::Clear() { m_type = eTypeInvalid; }
245 RegisterValue::Type RegisterValue::SetType(const RegisterInfo *reg_info) {
246 // To change the type, we simply copy the data in again, using the new format
248 DataExtractor copy_data;
249 if (copy.CopyValue(*this) && copy.GetData(copy_data))
250 SetValueFromData(reg_info, copy_data, 0, true);
255 Error RegisterValue::SetValueFromData(const RegisterInfo *reg_info,
257 lldb::offset_t src_offset,
258 bool partial_data_ok) {
261 if (src.GetByteSize() == 0) {
262 error.SetErrorString("empty data.");
266 if (reg_info->byte_size == 0) {
267 error.SetErrorString("invalid register info.");
271 uint32_t src_len = src.GetByteSize() - src_offset;
273 if (!partial_data_ok && (src_len < reg_info->byte_size)) {
274 error.SetErrorString("not enough data.");
278 // Cap the data length if there is more than enough bytes for this register
280 if (src_len > reg_info->byte_size)
281 src_len = reg_info->byte_size;
283 // Zero out the value in case we get partial data...
284 memset(buffer.bytes, 0, sizeof(buffer.bytes));
288 m_type = eTypeInvalid;
289 switch (reg_info->encoding) {
290 case eEncodingInvalid:
294 if (reg_info->byte_size == 1)
295 SetUInt8(src.GetMaxU32(&src_offset, src_len));
296 else if (reg_info->byte_size <= 2)
297 SetUInt16(src.GetMaxU32(&src_offset, src_len));
298 else if (reg_info->byte_size <= 4)
299 SetUInt32(src.GetMaxU32(&src_offset, src_len));
300 else if (reg_info->byte_size <= 8)
301 SetUInt64(src.GetMaxU64(&src_offset, src_len));
302 else if (reg_info->byte_size <= 16) {
303 uint64_t data1 = src.GetU64(&src_offset);
304 uint64_t data2 = src.GetU64(&src_offset);
305 if (src.GetByteSize() == eByteOrderBig) {
312 SetUInt128(llvm::APInt(128, 2, int128.x));
315 case eEncodingIEEE754:
316 if (reg_info->byte_size == sizeof(float))
317 SetFloat(src.GetFloat(&src_offset));
318 else if (reg_info->byte_size == sizeof(double))
319 SetDouble(src.GetDouble(&src_offset));
320 else if (reg_info->byte_size == sizeof(long double))
321 SetLongDouble(src.GetLongDouble(&src_offset));
323 case eEncodingVector: {
325 buffer.length = reg_info->byte_size;
326 buffer.byte_order = src.GetByteOrder();
327 assert(buffer.length <= kMaxRegisterByteSize);
328 if (buffer.length > kMaxRegisterByteSize)
329 buffer.length = kMaxRegisterByteSize;
330 if (src.CopyByteOrderedData(
331 src_offset, // offset within "src" to start extracting data
332 src_len, // src length
333 buffer.bytes, // dst buffer
334 buffer.length, // dst length
335 buffer.byte_order) == 0) // dst byte order
337 error.SetErrorStringWithFormat(
338 "failed to copy data for register write of %s", reg_info->name);
344 if (m_type == eTypeInvalid)
345 error.SetErrorStringWithFormat(
346 "invalid register value type for register %s", reg_info->name);
350 // Helper function for RegisterValue::SetValueFromString()
351 static bool ParseVectorEncoding(const RegisterInfo *reg_info,
352 llvm::StringRef vector_str,
353 const uint32_t byte_size,
354 RegisterValue *reg_value) {
355 // Example: vector_str = "{0x2c 0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a
356 // 0x2a 0x3e 0x84 0x4f 0x2a 0x3e}".
357 vector_str = vector_str.trim();
358 vector_str.consume_front("{");
359 vector_str.consume_back("}");
360 vector_str = vector_str.trim();
364 // The first split should give us:
365 // ('0x2c', '0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f
368 llvm::StringRef cdr = vector_str;
369 std::tie(car, cdr) = vector_str.split(Sep);
370 std::vector<uint8_t> bytes;
373 // Using radix auto-sensing by passing 0 as the radix.
374 // Keep on processing the vector elements as long as the parsing succeeds and
375 // the vector size is < byte_size.
376 while (!car.getAsInteger(0, byte) && bytes.size() < byte_size) {
377 bytes.push_back(byte);
378 std::tie(car, cdr) = cdr.split(Sep);
381 // Check for vector of exact byte_size elements.
382 if (bytes.size() != byte_size)
385 reg_value->SetBytes(&(bytes.front()), byte_size, eByteOrderLittle);
389 Error RegisterValue::SetValueFromString(const RegisterInfo *reg_info,
390 llvm::StringRef value_str) {
392 if (reg_info == nullptr) {
393 error.SetErrorString("Invalid register info argument.");
397 m_type = eTypeInvalid;
398 if (value_str.empty()) {
399 error.SetErrorString("Invalid c-string value string.");
402 const uint32_t byte_size = reg_info->byte_size;
408 long double ldbl_val;
409 switch (reg_info->encoding) {
410 case eEncodingInvalid:
411 error.SetErrorString("Invalid encoding.");
415 if (byte_size > sizeof(uint64_t)) {
416 error.SetErrorStringWithFormat(
417 "unsupported unsigned integer byte size: %u", byte_size);
420 if (value_str.getAsInteger(0, uval64)) {
421 error.SetErrorStringWithFormat(
422 "'%s' is not a valid unsigned integer string value",
423 value_str.str().c_str());
427 if (!Args::UInt64ValueIsValidForByteSize(uval64, byte_size)) {
428 error.SetErrorStringWithFormat(
430 " is too large to fit in a %u byte unsigned integer value",
435 if (!SetUInt(uval64, reg_info->byte_size)) {
436 error.SetErrorStringWithFormat(
437 "unsupported unsigned integer byte size: %u", byte_size);
440 // TODO: Shouldn't we be setting m_type here?
444 if (byte_size > sizeof(long long)) {
445 error.SetErrorStringWithFormat("unsupported signed integer byte size: %u",
450 if (value_str.getAsInteger(0, ival64)) {
451 error.SetErrorStringWithFormat(
452 "'%s' is not a valid signed integer string value",
453 value_str.str().c_str());
457 if (!Args::SInt64ValueIsValidForByteSize(ival64, byte_size)) {
458 error.SetErrorStringWithFormat(
460 " is too large to fit in a %u byte signed integer value",
465 if (!SetUInt(ival64, reg_info->byte_size)) {
466 error.SetErrorStringWithFormat("unsupported signed integer byte size: %u",
471 // TODO: Shouldn't we be setting m_type here?
474 case eEncodingIEEE754: {
475 std::string value_string = value_str;
476 if (byte_size == sizeof(float)) {
477 if (::sscanf(value_string.c_str(), "%f", &flt_val) != 1) {
478 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
479 value_string.c_str());
484 } else if (byte_size == sizeof(double)) {
485 if (::sscanf(value_string.c_str(), "%lf", &dbl_val) != 1) {
486 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
487 value_string.c_str());
491 m_type = eTypeDouble;
492 } else if (byte_size == sizeof(long double)) {
493 if (::sscanf(value_string.c_str(), "%Lf", &ldbl_val) != 1) {
494 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
495 value_string.c_str());
499 m_type = eTypeLongDouble;
501 error.SetErrorStringWithFormat("unsupported float byte size: %u",
507 case eEncodingVector:
508 if (!ParseVectorEncoding(reg_info, value_str, byte_size, this))
509 error.SetErrorString("unrecognized vector encoding string value.");
516 bool RegisterValue::SignExtend(uint32_t sign_bitpos) {
526 return m_scalar.SignExtend(sign_bitpos);
529 case eTypeLongDouble:
536 bool RegisterValue::CopyValue(const RegisterValue &rhs) {
548 case eTypeLongDouble:
549 m_scalar = rhs.m_scalar;
552 assert(rhs.buffer.length <= kMaxRegisterByteSize);
553 ::memcpy(buffer.bytes, rhs.buffer.bytes, kMaxRegisterByteSize);
554 buffer.length = rhs.buffer.length;
555 buffer.byte_order = rhs.buffer.byte_order;
561 uint16_t RegisterValue::GetAsUInt16(uint16_t fail_value,
562 bool *success_ptr) const {
571 return m_scalar.UShort(fail_value);
573 switch (buffer.length) {
578 return *(const uint16_t *)buffer.bytes;
583 *success_ptr = false;
587 uint32_t RegisterValue::GetAsUInt32(uint32_t fail_value,
588 bool *success_ptr) const {
599 case eTypeLongDouble:
600 return m_scalar.UInt(fail_value);
602 switch (buffer.length) {
608 return *(const uint32_t *)buffer.bytes;
613 *success_ptr = false;
617 uint64_t RegisterValue::GetAsUInt64(uint64_t fail_value,
618 bool *success_ptr) const {
630 case eTypeLongDouble:
631 return m_scalar.ULongLong(fail_value);
633 switch (buffer.length) {
637 return *(const uint8_t *)buffer.bytes;
639 return *(const uint16_t *)buffer.bytes;
641 return *(const uint32_t *)buffer.bytes;
643 return *(const uint64_t *)buffer.bytes;
648 *success_ptr = false;
652 llvm::APInt RegisterValue::GetAsUInt128(const llvm::APInt &fail_value,
653 bool *success_ptr) const {
666 case eTypeLongDouble:
667 return m_scalar.UInt128(fail_value);
669 switch (buffer.length) {
677 return llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
678 ((const type128 *)buffer.bytes)->x);
683 *success_ptr = false;
687 float RegisterValue::GetAsFloat(float fail_value, bool *success_ptr) const {
698 case eTypeLongDouble:
699 return m_scalar.Float(fail_value);
702 *success_ptr = false;
706 double RegisterValue::GetAsDouble(double fail_value, bool *success_ptr) const {
718 case eTypeLongDouble:
719 return m_scalar.Double(fail_value);
722 *success_ptr = false;
726 long double RegisterValue::GetAsLongDouble(long double fail_value,
727 bool *success_ptr) const {
739 case eTypeLongDouble:
740 return m_scalar.LongDouble();
743 *success_ptr = false;
747 const void *RegisterValue::GetBytes() const {
758 case eTypeLongDouble:
759 return m_scalar.GetBytes();
766 uint32_t RegisterValue::GetByteSize() const {
779 case eTypeLongDouble:
780 return m_scalar.GetByteSize();
782 return buffer.length;
787 bool RegisterValue::SetUInt(uint64_t uint, uint32_t byte_size) {
788 if (byte_size == 0) {
790 } else if (byte_size == 1) {
792 } else if (byte_size <= 2) {
794 } else if (byte_size <= 4) {
796 } else if (byte_size <= 8) {
798 } else if (byte_size <= 16) {
799 SetUInt128(llvm::APInt(128, uint));
805 void RegisterValue::SetBytes(const void *bytes, size_t length,
806 lldb::ByteOrder byte_order) {
807 // If this assertion fires off we need to increase the size of
808 // buffer.bytes, or make it something that is allocated on
809 // the heap. Since the data buffer is in a union, we can't make it
810 // a collection class like SmallVector...
811 if (bytes && length > 0) {
812 assert(length <= sizeof(buffer.bytes) &&
813 "Storing too many bytes in a RegisterValue.");
815 buffer.length = length;
816 memcpy(buffer.bytes, bytes, length);
817 buffer.byte_order = byte_order;
819 m_type = eTypeInvalid;
824 bool RegisterValue::operator==(const RegisterValue &rhs) const {
825 if (m_type == rhs.m_type) {
836 case eTypeLongDouble:
837 return m_scalar == rhs.m_scalar;
839 if (buffer.length != rhs.buffer.length)
842 uint8_t length = buffer.length;
843 if (length > kMaxRegisterByteSize)
844 length = kMaxRegisterByteSize;
845 return memcmp(buffer.bytes, rhs.buffer.bytes, length) == 0;
853 bool RegisterValue::operator!=(const RegisterValue &rhs) const {
854 if (m_type != rhs.m_type)
866 case eTypeLongDouble:
867 return m_scalar != rhs.m_scalar;
869 if (buffer.length != rhs.buffer.length) {
872 uint8_t length = buffer.length;
873 if (length > kMaxRegisterByteSize)
874 length = kMaxRegisterByteSize;
875 return memcmp(buffer.bytes, rhs.buffer.bytes, length) != 0;
882 bool RegisterValue::ClearBit(uint32_t bit) {
892 if (bit < (GetByteSize() * 8)) {
893 return m_scalar.ClearBit(bit);
899 case eTypeLongDouble:
903 if (buffer.byte_order == eByteOrderBig ||
904 buffer.byte_order == eByteOrderLittle) {
906 if (buffer.byte_order == eByteOrderBig)
907 byte_idx = buffer.length - (bit / 8) - 1;
911 const uint32_t byte_bit = bit % 8;
912 if (byte_idx < buffer.length) {
913 buffer.bytes[byte_idx] &= ~(1u << byte_bit);
922 bool RegisterValue::SetBit(uint32_t bit) {
932 if (bit < (GetByteSize() * 8)) {
933 return m_scalar.SetBit(bit);
939 case eTypeLongDouble:
943 if (buffer.byte_order == eByteOrderBig ||
944 buffer.byte_order == eByteOrderLittle) {
946 if (buffer.byte_order == eByteOrderBig)
947 byte_idx = buffer.length - (bit / 8) - 1;
951 const uint32_t byte_bit = bit % 8;
952 if (byte_idx < buffer.length) {
953 buffer.bytes[byte_idx] |= (1u << byte_bit);