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/DumpDataExtractor.h"
13 #include "lldb/Core/Scalar.h"
14 #include "lldb/Interpreter/Args.h"
15 #include "lldb/Utility/DataExtractor.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/Utility/Stream.h"
18 #include "lldb/Utility/StreamString.h"
19 #include "lldb/lldb-defines.h" // for LLDB_INVALID_ADDRESS
20 #include "lldb/lldb-private-types.h" // for RegisterInfo, type128
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/StringRef.h"
25 #include <cstdint> // for uint8_t, uint32_t, uint64_t
26 #include <string> // for string
27 #include <tuple> // for tie, tuple
30 #include <assert.h> // for assert
31 #include <inttypes.h> // for PRIx64
32 #include <stdio.h> // for sscanf
35 using namespace lldb_private;
37 bool RegisterValue::Dump(Stream *s, const RegisterInfo *reg_info,
38 bool prefix_with_name, bool prefix_with_alt_name,
40 uint32_t reg_name_right_align_at) const {
43 bool name_printed = false;
44 // For simplicity, alignment of the register name printing applies only
45 // in the most common case where:
47 // prefix_with_name^prefix_with_alt_name is true
49 StreamString format_string;
50 if (reg_name_right_align_at && (prefix_with_name ^ prefix_with_alt_name))
51 format_string.Printf("%%%us", reg_name_right_align_at);
53 format_string.Printf("%%s");
54 std::string fmt = format_string.GetString();
55 if (prefix_with_name) {
57 s->Printf(fmt.c_str(), reg_info->name);
59 } else if (reg_info->alt_name) {
60 s->Printf(fmt.c_str(), reg_info->alt_name);
61 prefix_with_alt_name = false;
65 if (prefix_with_alt_name) {
68 if (reg_info->alt_name) {
69 s->Printf(fmt.c_str(), reg_info->alt_name);
71 } else if (!name_printed) {
72 // No alternate name but we were asked to display a name, so show the
74 s->Printf(fmt.c_str(), reg_info->name);
81 if (format == eFormatDefault)
82 format = reg_info->format;
84 DumpDataExtractor(data, s,
85 0, // Offset in "data"
86 format, // Format to use when dumping
87 reg_info->byte_size, // item_byte_size
89 UINT32_MAX, // num_per_line
90 LLDB_INVALID_ADDRESS, // base_addr
92 0); // item_bit_offset
98 bool RegisterValue::GetData(DataExtractor &data) const {
99 return data.SetData(GetBytes(), GetByteSize(), GetByteOrder()) > 0;
102 uint32_t RegisterValue::GetAsMemoryData(const RegisterInfo *reg_info, void *dst,
104 lldb::ByteOrder dst_byte_order,
105 Status &error) const {
106 if (reg_info == nullptr) {
107 error.SetErrorString("invalid register info argument.");
111 // ReadRegister should have already been called on this object prior to
113 if (GetType() == eTypeInvalid) {
114 // No value has been read into this object...
115 error.SetErrorStringWithFormat(
116 "invalid register value type for register %s", reg_info->name);
120 if (dst_len > kMaxRegisterByteSize) {
121 error.SetErrorString("destination is too big");
125 const uint32_t src_len = reg_info->byte_size;
127 // Extract the register data into a data extractor
128 DataExtractor reg_data;
129 if (!GetData(reg_data)) {
130 error.SetErrorString("invalid register value to copy into");
134 // Prepare a memory buffer that contains some or all of the register value
135 const uint32_t bytes_copied =
136 reg_data.CopyByteOrderedData(0, // src offset
137 src_len, // src length
139 dst_len, // dst length
140 dst_byte_order); // dst byte order
141 if (bytes_copied == 0)
142 error.SetErrorStringWithFormat(
143 "failed to copy data for register write of %s", reg_info->name);
148 uint32_t RegisterValue::SetFromMemoryData(const RegisterInfo *reg_info,
149 const void *src, uint32_t src_len,
150 lldb::ByteOrder src_byte_order,
152 if (reg_info == nullptr) {
153 error.SetErrorString("invalid register info argument.");
157 // Moving from addr into a register
159 // Case 1: src_len == dst_len
161 // |AABBCCDD| Address contents
162 // |AABBCCDD| Register contents
164 // Case 2: src_len > dst_len
166 // Status! (The register should always be big enough to hold the data)
168 // Case 3: src_len < dst_len
170 // |AABB| Address contents
171 // |AABB0000| Register contents [on little-endian hardware]
172 // |0000AABB| Register contents [on big-endian hardware]
173 if (src_len > kMaxRegisterByteSize) {
174 error.SetErrorStringWithFormat(
175 "register buffer is too small to receive %u bytes of data.", src_len);
179 const uint32_t dst_len = reg_info->byte_size;
181 if (src_len > dst_len) {
182 error.SetErrorStringWithFormat(
183 "%u bytes is too big to store in register %s (%u bytes)", src_len,
184 reg_info->name, dst_len);
188 // Use a data extractor to correctly copy and pad the bytes read into the
190 DataExtractor src_data(src, src_len, src_byte_order, 4);
192 error = SetValueFromData(reg_info, src_data, 0, true);
196 // If SetValueFromData succeeded, we must have copied all of src_len
200 bool RegisterValue::GetScalarValue(Scalar &scalar) const {
205 switch (buffer.length) {
209 scalar = *(const uint8_t *)buffer.bytes;
212 scalar = *(const uint16_t *)buffer.bytes;
215 scalar = *(const uint32_t *)buffer.bytes;
218 scalar = *(const uint64_t *)buffer.bytes;
222 if (buffer.length % sizeof(uint64_t) == 0) {
223 const auto length_in_bits = buffer.length * 8;
224 const auto length_in_uint64 = buffer.length / sizeof(uint64_t);
226 llvm::APInt(length_in_bits,
227 llvm::ArrayRef<uint64_t>((const uint64_t *)buffer.bytes,
241 case eTypeLongDouble:
248 void RegisterValue::Clear() { m_type = eTypeInvalid; }
250 RegisterValue::Type RegisterValue::SetType(const RegisterInfo *reg_info) {
251 // To change the type, we simply copy the data in again, using the new format
253 DataExtractor copy_data;
254 if (copy.CopyValue(*this) && copy.GetData(copy_data))
255 SetValueFromData(reg_info, copy_data, 0, true);
260 Status RegisterValue::SetValueFromData(const RegisterInfo *reg_info,
262 lldb::offset_t src_offset,
263 bool partial_data_ok) {
266 if (src.GetByteSize() == 0) {
267 error.SetErrorString("empty data.");
271 if (reg_info->byte_size == 0) {
272 error.SetErrorString("invalid register info.");
276 uint32_t src_len = src.GetByteSize() - src_offset;
278 if (!partial_data_ok && (src_len < reg_info->byte_size)) {
279 error.SetErrorString("not enough data.");
283 // Cap the data length if there is more than enough bytes for this register
285 if (src_len > reg_info->byte_size)
286 src_len = reg_info->byte_size;
288 // Zero out the value in case we get partial data...
289 memset(buffer.bytes, 0, sizeof(buffer.bytes));
293 m_type = eTypeInvalid;
294 switch (reg_info->encoding) {
295 case eEncodingInvalid:
299 if (reg_info->byte_size == 1)
300 SetUInt8(src.GetMaxU32(&src_offset, src_len));
301 else if (reg_info->byte_size <= 2)
302 SetUInt16(src.GetMaxU32(&src_offset, src_len));
303 else if (reg_info->byte_size <= 4)
304 SetUInt32(src.GetMaxU32(&src_offset, src_len));
305 else if (reg_info->byte_size <= 8)
306 SetUInt64(src.GetMaxU64(&src_offset, src_len));
307 else if (reg_info->byte_size <= 16) {
308 uint64_t data1 = src.GetU64(&src_offset);
309 uint64_t data2 = src.GetU64(&src_offset);
310 if (src.GetByteSize() == eByteOrderBig) {
317 SetUInt128(llvm::APInt(128, 2, int128.x));
320 case eEncodingIEEE754:
321 if (reg_info->byte_size == sizeof(float))
322 SetFloat(src.GetFloat(&src_offset));
323 else if (reg_info->byte_size == sizeof(double))
324 SetDouble(src.GetDouble(&src_offset));
325 else if (reg_info->byte_size == sizeof(long double))
326 SetLongDouble(src.GetLongDouble(&src_offset));
328 case eEncodingVector: {
330 buffer.length = reg_info->byte_size;
331 buffer.byte_order = src.GetByteOrder();
332 assert(buffer.length <= kMaxRegisterByteSize);
333 if (buffer.length > kMaxRegisterByteSize)
334 buffer.length = kMaxRegisterByteSize;
335 if (src.CopyByteOrderedData(
336 src_offset, // offset within "src" to start extracting data
337 src_len, // src length
338 buffer.bytes, // dst buffer
339 buffer.length, // dst length
340 buffer.byte_order) == 0) // dst byte order
342 error.SetErrorStringWithFormat(
343 "failed to copy data for register write of %s", reg_info->name);
349 if (m_type == eTypeInvalid)
350 error.SetErrorStringWithFormat(
351 "invalid register value type for register %s", reg_info->name);
355 // Helper function for RegisterValue::SetValueFromString()
356 static bool ParseVectorEncoding(const RegisterInfo *reg_info,
357 llvm::StringRef vector_str,
358 const uint32_t byte_size,
359 RegisterValue *reg_value) {
360 // Example: vector_str = "{0x2c 0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a
361 // 0x2a 0x3e 0x84 0x4f 0x2a 0x3e}".
362 vector_str = vector_str.trim();
363 vector_str.consume_front("{");
364 vector_str.consume_back("}");
365 vector_str = vector_str.trim();
369 // The first split should give us:
370 // ('0x2c', '0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f
373 llvm::StringRef cdr = vector_str;
374 std::tie(car, cdr) = vector_str.split(Sep);
375 std::vector<uint8_t> bytes;
378 // Using radix auto-sensing by passing 0 as the radix.
379 // Keep on processing the vector elements as long as the parsing succeeds and
380 // the vector size is < byte_size.
381 while (!car.getAsInteger(0, byte) && bytes.size() < byte_size) {
382 bytes.push_back(byte);
383 std::tie(car, cdr) = cdr.split(Sep);
386 // Check for vector of exact byte_size elements.
387 if (bytes.size() != byte_size)
390 reg_value->SetBytes(&(bytes.front()), byte_size, eByteOrderLittle);
394 Status RegisterValue::SetValueFromString(const RegisterInfo *reg_info,
395 llvm::StringRef value_str) {
397 if (reg_info == nullptr) {
398 error.SetErrorString("Invalid register info argument.");
402 m_type = eTypeInvalid;
403 if (value_str.empty()) {
404 error.SetErrorString("Invalid c-string value string.");
407 const uint32_t byte_size = reg_info->byte_size;
413 long double ldbl_val;
414 switch (reg_info->encoding) {
415 case eEncodingInvalid:
416 error.SetErrorString("Invalid encoding.");
420 if (byte_size > sizeof(uint64_t)) {
421 error.SetErrorStringWithFormat(
422 "unsupported unsigned integer byte size: %u", byte_size);
425 if (value_str.getAsInteger(0, uval64)) {
426 error.SetErrorStringWithFormat(
427 "'%s' is not a valid unsigned integer string value",
428 value_str.str().c_str());
432 if (!Args::UInt64ValueIsValidForByteSize(uval64, byte_size)) {
433 error.SetErrorStringWithFormat(
435 " is too large to fit in a %u byte unsigned integer value",
440 if (!SetUInt(uval64, reg_info->byte_size)) {
441 error.SetErrorStringWithFormat(
442 "unsupported unsigned integer byte size: %u", byte_size);
445 // TODO: Shouldn't we be setting m_type here?
449 if (byte_size > sizeof(long long)) {
450 error.SetErrorStringWithFormat("unsupported signed integer byte size: %u",
455 if (value_str.getAsInteger(0, ival64)) {
456 error.SetErrorStringWithFormat(
457 "'%s' is not a valid signed integer string value",
458 value_str.str().c_str());
462 if (!Args::SInt64ValueIsValidForByteSize(ival64, byte_size)) {
463 error.SetErrorStringWithFormat(
465 " is too large to fit in a %u byte signed integer value",
470 if (!SetUInt(ival64, reg_info->byte_size)) {
471 error.SetErrorStringWithFormat("unsupported signed integer byte size: %u",
476 // TODO: Shouldn't we be setting m_type here?
479 case eEncodingIEEE754: {
480 std::string value_string = value_str;
481 if (byte_size == sizeof(float)) {
482 if (::sscanf(value_string.c_str(), "%f", &flt_val) != 1) {
483 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
484 value_string.c_str());
489 } else if (byte_size == sizeof(double)) {
490 if (::sscanf(value_string.c_str(), "%lf", &dbl_val) != 1) {
491 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
492 value_string.c_str());
496 m_type = eTypeDouble;
497 } else if (byte_size == sizeof(long double)) {
498 if (::sscanf(value_string.c_str(), "%Lf", &ldbl_val) != 1) {
499 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
500 value_string.c_str());
504 m_type = eTypeLongDouble;
506 error.SetErrorStringWithFormat("unsupported float byte size: %u",
512 case eEncodingVector:
513 if (!ParseVectorEncoding(reg_info, value_str, byte_size, this))
514 error.SetErrorString("unrecognized vector encoding string value.");
521 bool RegisterValue::SignExtend(uint32_t sign_bitpos) {
531 return m_scalar.SignExtend(sign_bitpos);
534 case eTypeLongDouble:
541 bool RegisterValue::CopyValue(const RegisterValue &rhs) {
543 return rhs.m_type == eTypeInvalid ? false : true;
556 case eTypeLongDouble:
557 m_scalar = rhs.m_scalar;
560 assert(rhs.buffer.length <= kMaxRegisterByteSize);
561 ::memcpy(buffer.bytes, rhs.buffer.bytes, kMaxRegisterByteSize);
562 buffer.length = rhs.buffer.length;
563 buffer.byte_order = rhs.buffer.byte_order;
569 uint16_t RegisterValue::GetAsUInt16(uint16_t fail_value,
570 bool *success_ptr) const {
579 return m_scalar.UShort(fail_value);
581 switch (buffer.length) {
586 return *(const uint16_t *)buffer.bytes;
591 *success_ptr = false;
595 uint32_t RegisterValue::GetAsUInt32(uint32_t fail_value,
596 bool *success_ptr) const {
607 case eTypeLongDouble:
608 return m_scalar.UInt(fail_value);
610 switch (buffer.length) {
616 return *(const uint32_t *)buffer.bytes;
621 *success_ptr = false;
625 uint64_t RegisterValue::GetAsUInt64(uint64_t fail_value,
626 bool *success_ptr) const {
638 case eTypeLongDouble:
639 return m_scalar.ULongLong(fail_value);
641 switch (buffer.length) {
645 return *(const uint8_t *)buffer.bytes;
647 return *(const uint16_t *)buffer.bytes;
649 return *(const uint32_t *)buffer.bytes;
651 return *(const uint64_t *)buffer.bytes;
656 *success_ptr = false;
660 llvm::APInt RegisterValue::GetAsUInt128(const llvm::APInt &fail_value,
661 bool *success_ptr) const {
674 case eTypeLongDouble:
675 return m_scalar.UInt128(fail_value);
677 switch (buffer.length) {
685 return llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
686 ((const type128 *)buffer.bytes)->x);
691 *success_ptr = false;
695 float RegisterValue::GetAsFloat(float fail_value, bool *success_ptr) const {
706 case eTypeLongDouble:
707 return m_scalar.Float(fail_value);
710 *success_ptr = false;
714 double RegisterValue::GetAsDouble(double fail_value, bool *success_ptr) const {
726 case eTypeLongDouble:
727 return m_scalar.Double(fail_value);
730 *success_ptr = false;
734 long double RegisterValue::GetAsLongDouble(long double fail_value,
735 bool *success_ptr) const {
747 case eTypeLongDouble:
748 return m_scalar.LongDouble();
751 *success_ptr = false;
755 const void *RegisterValue::GetBytes() const {
766 case eTypeLongDouble:
767 return m_scalar.GetBytes();
774 uint32_t RegisterValue::GetByteSize() const {
787 case eTypeLongDouble:
788 return m_scalar.GetByteSize();
790 return buffer.length;
795 bool RegisterValue::SetUInt(uint64_t uint, uint32_t byte_size) {
796 if (byte_size == 0) {
798 } else if (byte_size == 1) {
800 } else if (byte_size <= 2) {
802 } else if (byte_size <= 4) {
804 } else if (byte_size <= 8) {
806 } else if (byte_size <= 16) {
807 SetUInt128(llvm::APInt(128, uint));
813 void RegisterValue::SetBytes(const void *bytes, size_t length,
814 lldb::ByteOrder byte_order) {
815 // If this assertion fires off we need to increase the size of
816 // buffer.bytes, or make it something that is allocated on
817 // the heap. Since the data buffer is in a union, we can't make it
818 // a collection class like SmallVector...
819 if (bytes && length > 0) {
820 assert(length <= sizeof(buffer.bytes) &&
821 "Storing too many bytes in a RegisterValue.");
823 buffer.length = length;
824 memcpy(buffer.bytes, bytes, length);
825 buffer.byte_order = byte_order;
827 m_type = eTypeInvalid;
832 bool RegisterValue::operator==(const RegisterValue &rhs) const {
833 if (m_type == rhs.m_type) {
844 case eTypeLongDouble:
845 return m_scalar == rhs.m_scalar;
847 if (buffer.length != rhs.buffer.length)
850 uint8_t length = buffer.length;
851 if (length > kMaxRegisterByteSize)
852 length = kMaxRegisterByteSize;
853 return memcmp(buffer.bytes, rhs.buffer.bytes, length) == 0;
861 bool RegisterValue::operator!=(const RegisterValue &rhs) const {
862 if (m_type != rhs.m_type)
874 case eTypeLongDouble:
875 return m_scalar != rhs.m_scalar;
877 if (buffer.length != rhs.buffer.length) {
880 uint8_t length = buffer.length;
881 if (length > kMaxRegisterByteSize)
882 length = kMaxRegisterByteSize;
883 return memcmp(buffer.bytes, rhs.buffer.bytes, length) != 0;
890 bool RegisterValue::ClearBit(uint32_t bit) {
900 if (bit < (GetByteSize() * 8)) {
901 return m_scalar.ClearBit(bit);
907 case eTypeLongDouble:
911 if (buffer.byte_order == eByteOrderBig ||
912 buffer.byte_order == eByteOrderLittle) {
914 if (buffer.byte_order == eByteOrderBig)
915 byte_idx = buffer.length - (bit / 8) - 1;
919 const uint32_t byte_bit = bit % 8;
920 if (byte_idx < buffer.length) {
921 buffer.bytes[byte_idx] &= ~(1u << byte_bit);
930 bool RegisterValue::SetBit(uint32_t bit) {
940 if (bit < (GetByteSize() * 8)) {
941 return m_scalar.SetBit(bit);
947 case eTypeLongDouble:
951 if (buffer.byte_order == eByteOrderBig ||
952 buffer.byte_order == eByteOrderLittle) {
954 if (buffer.byte_order == eByteOrderBig)
955 byte_idx = buffer.length - (bit / 8) - 1;
959 const uint32_t byte_bit = bit % 8;
960 if (byte_idx < buffer.length) {
961 buffer.bytes[byte_idx] |= (1u << byte_bit);