1 //===-- Value.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/Core/Value.h"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Symbol/CompilerType.h"
14 #include "lldb/Symbol/ObjectFile.h"
15 #include "lldb/Symbol/SymbolContext.h"
16 #include "lldb/Symbol/Type.h"
17 #include "lldb/Symbol/Variable.h"
18 #include "lldb/Target/ExecutionContext.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/SectionLoadList.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/ConstString.h"
23 #include "lldb/Utility/DataBufferHeap.h"
24 #include "lldb/Utility/DataExtractor.h"
25 #include "lldb/Utility/Endian.h"
26 #include "lldb/Utility/FileSpec.h"
27 #include "lldb/Utility/State.h"
28 #include "lldb/Utility/Stream.h"
29 #include "lldb/lldb-defines.h"
30 #include "lldb/lldb-forward.h"
31 #include "lldb/lldb-types.h"
39 using namespace lldb_private;
42 : m_value(), m_vector(), m_compiler_type(), m_context(nullptr),
43 m_value_type(eValueTypeScalar), m_context_type(eContextTypeInvalid),
46 Value::Value(const Scalar &scalar)
47 : m_value(scalar), m_vector(), m_compiler_type(), m_context(nullptr),
48 m_value_type(eValueTypeScalar), m_context_type(eContextTypeInvalid),
51 Value::Value(const void *bytes, int len)
52 : m_value(), m_vector(), m_compiler_type(), m_context(nullptr),
53 m_value_type(eValueTypeHostAddress), m_context_type(eContextTypeInvalid),
58 Value::Value(const Value &v)
59 : m_value(v.m_value), m_vector(v.m_vector),
60 m_compiler_type(v.m_compiler_type), m_context(v.m_context),
61 m_value_type(v.m_value_type), m_context_type(v.m_context_type),
63 const uintptr_t rhs_value =
64 (uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS);
65 if ((rhs_value != 0) &&
66 (rhs_value == (uintptr_t)v.m_data_buffer.GetBytes())) {
67 m_data_buffer.CopyData(v.m_data_buffer.GetBytes(),
68 v.m_data_buffer.GetByteSize());
70 m_value = (uintptr_t)m_data_buffer.GetBytes();
74 Value &Value::operator=(const Value &rhs) {
76 m_value = rhs.m_value;
77 m_vector = rhs.m_vector;
78 m_compiler_type = rhs.m_compiler_type;
79 m_context = rhs.m_context;
80 m_value_type = rhs.m_value_type;
81 m_context_type = rhs.m_context_type;
82 const uintptr_t rhs_value =
83 (uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS);
84 if ((rhs_value != 0) &&
85 (rhs_value == (uintptr_t)rhs.m_data_buffer.GetBytes())) {
86 m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(),
87 rhs.m_data_buffer.GetByteSize());
89 m_value = (uintptr_t)m_data_buffer.GetBytes();
95 void Value::SetBytes(const void *bytes, int len) {
96 m_value_type = eValueTypeHostAddress;
97 m_data_buffer.CopyData(bytes, len);
98 m_value = (uintptr_t)m_data_buffer.GetBytes();
101 void Value::AppendBytes(const void *bytes, int len) {
102 m_value_type = eValueTypeHostAddress;
103 m_data_buffer.AppendData(bytes, len);
104 m_value = (uintptr_t)m_data_buffer.GetBytes();
107 void Value::Dump(Stream *strm) {
108 m_value.GetValue(strm, true);
109 strm->Printf(", value_type = %s, context = %p, context_type = %s",
110 Value::GetValueTypeAsCString(m_value_type), m_context,
111 Value::GetContextTypeAsCString(m_context_type));
114 Value::ValueType Value::GetValueType() const { return m_value_type; }
116 AddressType Value::GetValueAddressType() const {
117 switch (m_value_type) {
119 case eValueTypeScalar:
121 case eValueTypeLoadAddress:
122 return eAddressTypeLoad;
123 case eValueTypeFileAddress:
124 return eAddressTypeFile;
125 case eValueTypeHostAddress:
126 return eAddressTypeHost;
128 return eAddressTypeInvalid;
131 RegisterInfo *Value::GetRegisterInfo() const {
132 if (m_context_type == eContextTypeRegisterInfo)
133 return static_cast<RegisterInfo *>(m_context);
137 Type *Value::GetType() {
138 if (m_context_type == eContextTypeLLDBType)
139 return static_cast<Type *>(m_context);
143 size_t Value::AppendDataToHostBuffer(const Value &rhs) {
147 size_t curr_size = m_data_buffer.GetByteSize();
149 switch (rhs.GetValueType()) {
150 case eValueTypeScalar: {
151 const size_t scalar_size = rhs.m_value.GetByteSize();
152 if (scalar_size > 0) {
153 const size_t new_size = curr_size + scalar_size;
154 if (ResizeData(new_size) == new_size) {
155 rhs.m_value.GetAsMemoryData(m_data_buffer.GetBytes() + curr_size,
156 scalar_size, endian::InlHostByteOrder(),
162 case eValueTypeVector: {
163 const size_t vector_size = rhs.m_vector.length;
164 if (vector_size > 0) {
165 const size_t new_size = curr_size + vector_size;
166 if (ResizeData(new_size) == new_size) {
167 ::memcpy(m_data_buffer.GetBytes() + curr_size, rhs.m_vector.bytes,
173 case eValueTypeFileAddress:
174 case eValueTypeLoadAddress:
175 case eValueTypeHostAddress: {
176 const uint8_t *src = rhs.GetBuffer().GetBytes();
177 const size_t src_len = rhs.GetBuffer().GetByteSize();
178 if (src && src_len > 0) {
179 const size_t new_size = curr_size + src_len;
180 if (ResizeData(new_size) == new_size) {
181 ::memcpy(m_data_buffer.GetBytes() + curr_size, src, src_len);
190 size_t Value::ResizeData(size_t len) {
191 m_value_type = eValueTypeHostAddress;
192 m_data_buffer.SetByteSize(len);
193 m_value = (uintptr_t)m_data_buffer.GetBytes();
194 return m_data_buffer.GetByteSize();
197 bool Value::ValueOf(ExecutionContext *exe_ctx) {
198 switch (m_context_type) {
199 case eContextTypeInvalid:
200 case eContextTypeRegisterInfo: // RegisterInfo *
201 case eContextTypeLLDBType: // Type *
204 case eContextTypeVariable: // Variable *
205 ResolveValue(exe_ctx);
211 uint64_t Value::GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx) {
212 switch (m_context_type) {
213 case eContextTypeRegisterInfo: // RegisterInfo *
214 if (GetRegisterInfo()) {
217 return GetRegisterInfo()->byte_size;
221 case eContextTypeInvalid:
222 case eContextTypeLLDBType: // Type *
223 case eContextTypeVariable: // Variable *
225 auto *scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
226 if (llvm::Optional<uint64_t> size = GetCompilerType().GetByteSize(scope)) {
234 if (error_ptr && error_ptr->Success())
235 error_ptr->SetErrorString("Unable to determine byte size.");
239 const CompilerType &Value::GetCompilerType() {
240 if (!m_compiler_type.IsValid()) {
241 switch (m_context_type) {
242 case eContextTypeInvalid:
245 case eContextTypeRegisterInfo:
246 break; // TODO: Eventually convert into a compiler type?
248 case eContextTypeLLDBType: {
249 Type *lldb_type = GetType();
251 m_compiler_type = lldb_type->GetForwardCompilerType();
254 case eContextTypeVariable: {
255 Variable *variable = GetVariable();
257 Type *variable_type = variable->GetType();
259 m_compiler_type = variable_type->GetForwardCompilerType();
265 return m_compiler_type;
268 void Value::SetCompilerType(const CompilerType &compiler_type) {
269 m_compiler_type = compiler_type;
272 lldb::Format Value::GetValueDefaultFormat() {
273 switch (m_context_type) {
274 case eContextTypeRegisterInfo:
275 if (GetRegisterInfo())
276 return GetRegisterInfo()->format;
279 case eContextTypeInvalid:
280 case eContextTypeLLDBType:
281 case eContextTypeVariable: {
282 const CompilerType &ast_type = GetCompilerType();
283 if (ast_type.IsValid())
284 return ast_type.GetFormat();
288 // Return a good default in case we can't figure anything out
292 bool Value::GetData(DataExtractor &data) {
293 switch (m_value_type) {
297 case eValueTypeScalar:
298 if (m_value.GetData(data))
302 case eValueTypeLoadAddress:
303 case eValueTypeFileAddress:
304 case eValueTypeHostAddress:
305 if (m_data_buffer.GetByteSize()) {
306 data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(),
307 data.GetByteOrder());
316 Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
317 uint32_t data_offset, Module *module) {
321 lldb::addr_t address = LLDB_INVALID_ADDRESS;
322 AddressType address_type = eAddressTypeFile;
323 Address file_so_addr;
324 const CompilerType &ast_type = GetCompilerType();
325 switch (m_value_type) {
326 case eValueTypeVector:
327 if (ast_type.IsValid())
328 data.SetAddressByteSize(ast_type.GetPointerByteSize());
330 data.SetAddressByteSize(sizeof(void *));
331 data.SetData(m_vector.bytes, m_vector.length, m_vector.byte_order);
334 case eValueTypeScalar: {
335 data.SetByteOrder(endian::InlHostByteOrder());
336 if (ast_type.IsValid())
337 data.SetAddressByteSize(ast_type.GetPointerByteSize());
339 data.SetAddressByteSize(sizeof(void *));
341 uint32_t limit_byte_size = UINT32_MAX;
343 if (llvm::Optional<uint64_t> size = ast_type.GetByteSize(
344 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr))
345 limit_byte_size = *size;
347 if (limit_byte_size <= m_value.GetByteSize()) {
348 if (m_value.GetData(data, limit_byte_size))
349 return error; // Success;
352 error.SetErrorStringWithFormat("extracting data from value failed");
355 case eValueTypeLoadAddress:
356 if (exe_ctx == nullptr) {
357 error.SetErrorString("can't read load address (no execution context)");
359 Process *process = exe_ctx->GetProcessPtr();
360 if (process == nullptr || !process->IsAlive()) {
361 Target *target = exe_ctx->GetTargetPtr();
363 // Allow expressions to run and evaluate things when the target has
364 // memory sections loaded. This allows you to use "target modules
365 // load" to load your executable and any shared libraries, then
366 // execute commands where you can look at types in data sections.
367 const SectionLoadList &target_sections = target->GetSectionLoadList();
368 if (!target_sections.IsEmpty()) {
369 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
370 if (target_sections.ResolveLoadAddress(address, file_so_addr)) {
371 address_type = eAddressTypeLoad;
372 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
373 data.SetAddressByteSize(
374 target->GetArchitecture().GetAddressByteSize());
376 address = LLDB_INVALID_ADDRESS;
379 error.SetErrorString("can't read load address (invalid process)");
382 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
383 address_type = eAddressTypeLoad;
385 process->GetTarget().GetArchitecture().GetByteOrder());
386 data.SetAddressByteSize(
387 process->GetTarget().GetArchitecture().GetAddressByteSize());
392 case eValueTypeFileAddress:
393 if (exe_ctx == nullptr) {
394 error.SetErrorString("can't read file address (no execution context)");
395 } else if (exe_ctx->GetTargetPtr() == nullptr) {
396 error.SetErrorString("can't read file address (invalid target)");
398 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
399 if (address == LLDB_INVALID_ADDRESS) {
400 error.SetErrorString("invalid file address");
402 if (module == nullptr) {
403 // The only thing we can currently lock down to a module so that we
404 // can resolve a file address, is a variable.
405 Variable *variable = GetVariable();
407 SymbolContext var_sc;
408 variable->CalculateSymbolContext(&var_sc);
409 module = var_sc.module_sp.get();
414 bool resolved = false;
415 ObjectFile *objfile = module->GetObjectFile();
417 Address so_addr(address, objfile->GetSectionList());
418 addr_t load_address =
419 so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
420 bool process_launched_and_stopped =
421 exe_ctx->GetProcessPtr()
422 ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(),
423 true /* must_exist */)
425 // Don't use the load address if the process has exited.
426 if (load_address != LLDB_INVALID_ADDRESS &&
427 process_launched_and_stopped) {
429 address = load_address;
430 address_type = eAddressTypeLoad;
432 exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder());
433 data.SetAddressByteSize(exe_ctx->GetTargetRef()
435 .GetAddressByteSize());
437 if (so_addr.IsSectionOffset()) {
439 file_so_addr = so_addr;
440 data.SetByteOrder(objfile->GetByteOrder());
441 data.SetAddressByteSize(objfile->GetAddressByteSize());
446 Variable *variable = GetVariable();
450 error.SetErrorStringWithFormat(
451 "unable to resolve the module for file address 0x%" PRIx64
452 " for variable '%s' in %s",
453 address, variable->GetName().AsCString(""),
454 module->GetFileSpec().GetPath().c_str());
456 error.SetErrorStringWithFormat(
457 "unable to resolve the module for file address 0x%" PRIx64
459 address, module->GetFileSpec().GetPath().c_str());
462 error.SetErrorStringWithFormat(
463 "unable to resolve the module for file address 0x%" PRIx64
464 " for variable '%s'",
465 address, variable->GetName().AsCString(""));
467 error.SetErrorStringWithFormat(
468 "unable to resolve the module for file address 0x%" PRIx64,
473 // Can't convert a file address to anything valid without more
474 // context (which Module it came from)
475 error.SetErrorString(
476 "can't read memory from file address without more context");
482 case eValueTypeHostAddress:
483 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
484 address_type = eAddressTypeHost;
486 Target *target = exe_ctx->GetTargetPtr();
488 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
489 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
493 // fallback to host settings
494 data.SetByteOrder(endian::InlHostByteOrder());
495 data.SetAddressByteSize(sizeof(void *));
499 // Bail if we encountered any errors
503 if (address == LLDB_INVALID_ADDRESS) {
504 error.SetErrorStringWithFormat("invalid %s address",
505 address_type == eAddressTypeHost ? "host"
510 // If we got here, we need to read the value from memory
511 size_t byte_size = GetValueByteSize(&error, exe_ctx);
513 // Bail if we encountered any errors getting the byte size
517 // No memory to read for zero-sized types.
521 // Make sure we have enough room within "data", and if we don't make
522 // something large enough that does
523 if (!data.ValidOffsetForDataOfSize(data_offset, byte_size)) {
525 std::make_shared<DataBufferHeap>(data_offset + byte_size, '\0');
526 data.SetData(data_sp);
529 uint8_t *dst = const_cast<uint8_t *>(data.PeekData(data_offset, byte_size));
530 if (dst != nullptr) {
531 if (address_type == eAddressTypeHost) {
532 // The address is an address in this process, so just copy it.
534 error.SetErrorStringWithFormat(
535 "trying to read from host address of 0.");
538 memcpy(dst, reinterpret_cast<uint8_t *>(address), byte_size);
539 } else if ((address_type == eAddressTypeLoad) ||
540 (address_type == eAddressTypeFile)) {
541 if (file_so_addr.IsValid()) {
542 // We have a file address that we were able to translate into a section
543 // offset address so we might be able to read this from the object
544 // files if we don't have a live process. Lets always try and read from
545 // the process if we have one though since we want to read the actual
546 // value by setting "prefer_file_cache" to false.
547 const bool prefer_file_cache = false;
548 if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache,
550 error) != byte_size) {
551 error.SetErrorStringWithFormat(
552 "read memory from 0x%" PRIx64 " failed", (uint64_t)address);
555 // The execution context might have a NULL process, but it might have a
556 // valid process in the exe_ctx->target, so use the
557 // ExecutionContext::GetProcess accessor to ensure we get the process
559 Process *process = exe_ctx->GetProcessPtr();
562 const size_t bytes_read =
563 process->ReadMemory(address, dst, byte_size, error);
564 if (bytes_read != byte_size)
565 error.SetErrorStringWithFormat(
566 "read memory from 0x%" PRIx64 " failed (%u of %u bytes read)",
567 (uint64_t)address, (uint32_t)bytes_read, (uint32_t)byte_size);
569 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64
570 " failed (invalid process)",
575 error.SetErrorStringWithFormat("unsupported AddressType value (%i)",
579 error.SetErrorStringWithFormat("out of memory");
585 Scalar &Value::ResolveValue(ExecutionContext *exe_ctx) {
586 const CompilerType &compiler_type = GetCompilerType();
587 if (compiler_type.IsValid()) {
588 switch (m_value_type) {
589 case eValueTypeScalar: // raw scalar value
593 case eValueTypeFileAddress:
594 case eValueTypeLoadAddress: // load address value
595 case eValueTypeHostAddress: // host address value (for memory in the process
596 // that is using liblldb)
599 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
600 Status error(GetValueAsData(exe_ctx, data, 0, nullptr));
601 if (error.Success()) {
603 if (compiler_type.GetValueAsScalar(data, 0, data.GetByteSize(),
606 m_value_type = eValueTypeScalar;
608 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) {
610 m_value_type = eValueTypeScalar;
614 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) {
616 m_value_type = eValueTypeScalar;
625 Variable *Value::GetVariable() {
626 if (m_context_type == eContextTypeVariable)
627 return static_cast<Variable *>(m_context);
631 void Value::Clear() {
634 m_compiler_type.Clear();
635 m_value_type = eValueTypeScalar;
637 m_context_type = eContextTypeInvalid;
638 m_data_buffer.Clear();
641 const char *Value::GetValueTypeAsCString(ValueType value_type) {
642 switch (value_type) {
643 case eValueTypeScalar:
645 case eValueTypeVector:
647 case eValueTypeFileAddress:
648 return "file address";
649 case eValueTypeLoadAddress:
650 return "load address";
651 case eValueTypeHostAddress:
652 return "host address";
657 const char *Value::GetContextTypeAsCString(ContextType context_type) {
658 switch (context_type) {
659 case eContextTypeInvalid:
661 case eContextTypeRegisterInfo:
662 return "RegisterInfo *";
663 case eContextTypeLLDBType:
665 case eContextTypeVariable:
671 void Value::ConvertToLoadAddress(Module *module, Target *target) {
672 if (!module || !target || (GetValueType() != eValueTypeFileAddress))
675 lldb::addr_t file_addr = GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
676 if (file_addr == LLDB_INVALID_ADDRESS)
680 if (!module->ResolveFileAddress(file_addr, so_addr))
682 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
683 if (load_addr == LLDB_INVALID_ADDRESS)
686 SetValueType(Value::eValueTypeLoadAddress);
687 GetScalar() = load_addr;
690 ValueList::ValueList(const ValueList &rhs) { m_values = rhs.m_values; }
692 const ValueList &ValueList::operator=(const ValueList &rhs) {
693 m_values = rhs.m_values;
697 void ValueList::PushValue(const Value &value) { m_values.push_back(value); }
699 size_t ValueList::GetSize() { return m_values.size(); }
701 Value *ValueList::GetValueAtIndex(size_t idx) {
702 if (idx < GetSize()) {
703 return &(m_values[idx]);
708 void ValueList::Clear() { m_values.clear(); }