1 //===-- Value.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/Value.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/DataExtractor.h"
17 #include "lldb/Core/DataBufferHeap.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/State.h"
20 #include "lldb/Core/Stream.h"
21 #include "lldb/Symbol/ClangASTType.h"
22 #include "lldb/Symbol/ClangASTContext.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Symbol/Type.h"
26 #include "lldb/Symbol/Variable.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/SectionLoadList.h"
30 #include "lldb/Target/Target.h"
33 using namespace lldb_private;
40 m_value_type (eValueTypeScalar),
41 m_context_type (eContextTypeInvalid),
46 Value::Value(const Scalar& scalar) :
51 m_value_type (eValueTypeScalar),
52 m_context_type (eContextTypeInvalid),
58 Value::Value(const void *bytes, int len) :
63 m_value_type (eValueTypeHostAddress),
64 m_context_type (eContextTypeInvalid),
70 Value::Value(const Value &v) :
72 m_vector (v.m_vector),
73 m_clang_type (v.m_clang_type),
74 m_context (v.m_context),
75 m_value_type (v.m_value_type),
76 m_context_type (v.m_context_type),
79 const uintptr_t rhs_value = (uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS);
80 if ((rhs_value != 0) && (rhs_value == (uintptr_t)v.m_data_buffer.GetBytes()))
82 m_data_buffer.CopyData(v.m_data_buffer.GetBytes(),
83 v.m_data_buffer.GetByteSize());
85 m_value = (uintptr_t)m_data_buffer.GetBytes();
90 Value::operator=(const Value &rhs)
94 m_value = rhs.m_value;
95 m_vector = rhs.m_vector;
96 m_clang_type = rhs.m_clang_type;
97 m_context = rhs.m_context;
98 m_value_type = rhs.m_value_type;
99 m_context_type = rhs.m_context_type;
100 const uintptr_t rhs_value = (uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS);
101 if ((rhs_value != 0) && (rhs_value == (uintptr_t)rhs.m_data_buffer.GetBytes()))
103 m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(),
104 rhs.m_data_buffer.GetByteSize());
106 m_value = (uintptr_t)m_data_buffer.GetBytes();
113 Value::SetBytes (const void *bytes, int len)
115 m_value_type = eValueTypeHostAddress;
116 m_data_buffer.CopyData(bytes, len);
117 m_value = (uintptr_t)m_data_buffer.GetBytes();
121 Value::AppendBytes (const void *bytes, int len)
123 m_value_type = eValueTypeHostAddress;
124 m_data_buffer.AppendData (bytes, len);
125 m_value = (uintptr_t)m_data_buffer.GetBytes();
129 Value::Dump (Stream* strm)
131 m_value.GetValue (strm, true);
132 strm->Printf(", value_type = %s, context = %p, context_type = %s",
133 Value::GetValueTypeAsCString(m_value_type),
135 Value::GetContextTypeAsCString(m_context_type));
139 Value::GetValueType() const
145 Value::GetValueAddressType () const
147 switch (m_value_type)
150 case eValueTypeScalar:
152 case eValueTypeLoadAddress: return eAddressTypeLoad;
153 case eValueTypeFileAddress: return eAddressTypeFile;
154 case eValueTypeHostAddress: return eAddressTypeHost;
156 return eAddressTypeInvalid;
160 Value::GetRegisterInfo() const
162 if (m_context_type == eContextTypeRegisterInfo)
163 return static_cast<RegisterInfo *> (m_context);
170 if (m_context_type == eContextTypeLLDBType)
171 return static_cast<Type *> (m_context);
176 Value::AppendDataToHostBuffer (const Value &rhs)
178 size_t curr_size = m_data_buffer.GetByteSize();
180 switch (rhs.GetValueType())
182 case eValueTypeScalar:
184 const size_t scalar_size = rhs.m_value.GetByteSize();
187 const size_t new_size = curr_size + scalar_size;
188 if (ResizeData(new_size) == new_size)
190 rhs.m_value.GetAsMemoryData (m_data_buffer.GetBytes() + curr_size,
192 lldb::endian::InlHostByteOrder(),
199 case eValueTypeVector:
201 const size_t vector_size = rhs.m_vector.length;
204 const size_t new_size = curr_size + vector_size;
205 if (ResizeData(new_size) == new_size)
207 ::memcpy (m_data_buffer.GetBytes() + curr_size,
215 case eValueTypeFileAddress:
216 case eValueTypeLoadAddress:
217 case eValueTypeHostAddress:
219 const uint8_t *src = rhs.GetBuffer().GetBytes();
220 const size_t src_len = rhs.GetBuffer().GetByteSize();
221 if (src && src_len > 0)
223 const size_t new_size = curr_size + src_len;
224 if (ResizeData(new_size) == new_size)
226 ::memcpy (m_data_buffer.GetBytes() + curr_size, src, src_len);
237 Value::ResizeData(size_t len)
239 m_value_type = eValueTypeHostAddress;
240 m_data_buffer.SetByteSize(len);
241 m_value = (uintptr_t)m_data_buffer.GetBytes();
242 return m_data_buffer.GetByteSize();
246 Value::ValueOf(ExecutionContext *exe_ctx)
248 switch (m_context_type)
250 case eContextTypeInvalid:
251 case eContextTypeRegisterInfo: // RegisterInfo *
252 case eContextTypeLLDBType: // Type *
255 case eContextTypeVariable: // Variable *
256 ResolveValue(exe_ctx);
263 Value::GetValueByteSize (Error *error_ptr)
265 uint64_t byte_size = 0;
267 switch (m_context_type)
269 case eContextTypeRegisterInfo: // RegisterInfo *
270 if (GetRegisterInfo())
271 byte_size = GetRegisterInfo()->byte_size;
274 case eContextTypeInvalid:
275 case eContextTypeLLDBType: // Type *
276 case eContextTypeVariable: // Variable *
278 const ClangASTType &ast_type = GetClangType();
279 if (ast_type.IsValid())
280 byte_size = ast_type.GetByteSize();
289 if (error_ptr->Success())
290 error_ptr->SetErrorString("Unable to determine byte size.");
301 Value::GetClangType ()
303 if (!m_clang_type.IsValid())
305 switch (m_context_type)
307 case eContextTypeInvalid:
310 case eContextTypeRegisterInfo:
311 break; // TODO: Eventually convert into a clang type?
313 case eContextTypeLLDBType:
315 Type *lldb_type = GetType();
317 m_clang_type = lldb_type->GetClangForwardType();
321 case eContextTypeVariable:
323 Variable *variable = GetVariable();
326 Type *variable_type = variable->GetType();
328 m_clang_type = variable_type->GetClangForwardType();
339 Value::SetClangType (const ClangASTType &clang_type)
341 m_clang_type = clang_type;
345 Value::GetValueDefaultFormat ()
347 switch (m_context_type)
349 case eContextTypeRegisterInfo:
350 if (GetRegisterInfo())
351 return GetRegisterInfo()->format;
354 case eContextTypeInvalid:
355 case eContextTypeLLDBType:
356 case eContextTypeVariable:
358 const ClangASTType &ast_type = GetClangType();
359 if (ast_type.IsValid())
360 return ast_type.GetFormat();
366 // Return a good default in case we can't figure anything out
371 Value::GetData (DataExtractor &data)
373 switch (m_value_type)
378 case eValueTypeScalar:
379 if (m_value.GetData (data))
383 case eValueTypeLoadAddress:
384 case eValueTypeFileAddress:
385 case eValueTypeHostAddress:
386 if (m_data_buffer.GetByteSize())
388 data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder());
399 Value::GetValueAsData (ExecutionContext *exe_ctx,
401 uint32_t data_offset,
407 lldb::addr_t address = LLDB_INVALID_ADDRESS;
408 AddressType address_type = eAddressTypeFile;
409 Address file_so_addr;
410 const ClangASTType &ast_type = GetClangType();
411 switch (m_value_type)
413 case eValueTypeVector:
414 if (ast_type.IsValid())
415 data.SetAddressByteSize (ast_type.GetPointerByteSize());
417 data.SetAddressByteSize(sizeof(void *));
418 data.SetData(m_vector.bytes, m_vector.length, m_vector.byte_order);
421 case eValueTypeScalar:
423 data.SetByteOrder (lldb::endian::InlHostByteOrder());
424 if (ast_type.IsValid())
425 data.SetAddressByteSize (ast_type.GetPointerByteSize());
427 data.SetAddressByteSize(sizeof(void *));
429 uint32_t limit_byte_size = UINT32_MAX;
431 if (ast_type.IsValid() && ast_type.IsScalarType())
433 uint64_t type_encoding_count = 0;
434 lldb::Encoding type_encoding = ast_type.GetEncoding(type_encoding_count);
436 if (type_encoding == eEncodingUint || type_encoding == eEncodingSint)
437 limit_byte_size = ast_type.GetByteSize();
440 if (m_value.GetData (data, limit_byte_size))
441 return error; // Success;
443 error.SetErrorStringWithFormat("extracting data from value failed");
446 case eValueTypeLoadAddress:
449 error.SetErrorString ("can't read load address (no execution context)");
453 Process *process = exe_ctx->GetProcessPtr();
454 if (process == NULL || !process->IsAlive())
456 Target *target = exe_ctx->GetTargetPtr();
459 // Allow expressions to run and evaluate things when the target
460 // has memory sections loaded. This allows you to use "target modules load"
461 // to load your executable and any shared libraries, then execute
462 // commands where you can look at types in data sections.
463 const SectionLoadList &target_sections = target->GetSectionLoadList();
464 if (!target_sections.IsEmpty())
466 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
467 if (target_sections.ResolveLoadAddress(address, file_so_addr))
469 address_type = eAddressTypeLoad;
470 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
471 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
474 address = LLDB_INVALID_ADDRESS;
478 // ModuleSP exe_module_sp (target->GetExecutableModule());
479 // if (exe_module_sp)
481 // address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
482 // if (address != LLDB_INVALID_ADDRESS)
484 // if (exe_module_sp->ResolveFileAddress(address, file_so_addr))
486 // data.SetByteOrder(target->GetArchitecture().GetByteOrder());
487 // data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
488 // address_type = eAddressTypeFile;
492 // address = LLDB_INVALID_ADDRESS;
500 error.SetErrorString ("can't read load address (invalid process)");
505 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
506 address_type = eAddressTypeLoad;
507 data.SetByteOrder(process->GetTarget().GetArchitecture().GetByteOrder());
508 data.SetAddressByteSize(process->GetTarget().GetArchitecture().GetAddressByteSize());
513 case eValueTypeFileAddress:
516 error.SetErrorString ("can't read file address (no execution context)");
518 else if (exe_ctx->GetTargetPtr() == NULL)
520 error.SetErrorString ("can't read file address (invalid target)");
524 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
525 if (address == LLDB_INVALID_ADDRESS)
527 error.SetErrorString ("invalid file address");
533 // The only thing we can currently lock down to a module so that
534 // we can resolve a file address, is a variable.
535 Variable *variable = GetVariable();
538 SymbolContext var_sc;
539 variable->CalculateSymbolContext(&var_sc);
540 module = var_sc.module_sp.get();
546 bool resolved = false;
547 ObjectFile *objfile = module->GetObjectFile();
550 Address so_addr(address, objfile->GetSectionList());
551 addr_t load_address = so_addr.GetLoadAddress (exe_ctx->GetTargetPtr());
552 bool process_launched_and_stopped = exe_ctx->GetProcessPtr()
553 ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(), true /* must_exist */)
555 // Don't use the load address if the process has exited.
556 if (load_address != LLDB_INVALID_ADDRESS && process_launched_and_stopped)
559 address = load_address;
560 address_type = eAddressTypeLoad;
561 data.SetByteOrder(exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder());
562 data.SetAddressByteSize(exe_ctx->GetTargetRef().GetArchitecture().GetAddressByteSize());
566 if (so_addr.IsSectionOffset())
569 file_so_addr = so_addr;
570 data.SetByteOrder(objfile->GetByteOrder());
571 data.SetAddressByteSize(objfile->GetAddressByteSize());
577 Variable *variable = GetVariable();
582 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s' in %s",
584 variable->GetName().AsCString(""),
585 module->GetFileSpec().GetPath().c_str());
587 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " in %s",
589 module->GetFileSpec().GetPath().c_str());
594 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s'",
596 variable->GetName().AsCString(""));
598 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64, address);
604 // Can't convert a file address to anything valid without more
605 // context (which Module it came from)
606 error.SetErrorString ("can't read memory from file address without more context");
612 case eValueTypeHostAddress:
613 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
614 address_type = eAddressTypeHost;
617 Target *target = exe_ctx->GetTargetPtr();
620 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
621 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
625 // fallback to host settings
626 data.SetByteOrder(lldb::endian::InlHostByteOrder());
627 data.SetAddressByteSize(sizeof(void *));
631 // Bail if we encountered any errors
635 if (address == LLDB_INVALID_ADDRESS)
637 error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load");
641 // If we got here, we need to read the value from memory
642 size_t byte_size = GetValueByteSize (&error);
644 // Bail if we encountered any errors getting the byte size
648 // Make sure we have enough room within "data", and if we don't make
649 // something large enough that does
650 if (!data.ValidOffsetForDataOfSize (data_offset, byte_size))
652 DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0'));
653 data.SetData(data_sp);
656 uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size));
659 if (address_type == eAddressTypeHost)
661 // The address is an address in this process, so just copy it.
664 error.SetErrorStringWithFormat("trying to read from host address of 0.");
667 memcpy (dst, (uint8_t*)NULL + address, byte_size);
669 else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile))
671 if (file_so_addr.IsValid())
673 // We have a file address that we were able to translate into a
674 // section offset address so we might be able to read this from
675 // the object files if we don't have a live process. Lets always
676 // try and read from the process if we have one though since we
677 // want to read the actual value by setting "prefer_file_cache"
679 const bool prefer_file_cache = false;
680 if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size)
682 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", (uint64_t)address);
687 // The execution context might have a NULL process, but it
688 // might have a valid process in the exe_ctx->target, so use
689 // the ExecutionContext::GetProcess accessor to ensure we
690 // get the process if there is one.
691 Process *process = exe_ctx->GetProcessPtr();
695 const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error);
696 if (bytes_read != byte_size)
697 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (%u of %u bytes read)",
699 (uint32_t)bytes_read,
700 (uint32_t)byte_size);
704 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (invalid process)", (uint64_t)address);
710 error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type);
715 error.SetErrorStringWithFormat ("out of memory");
722 Value::ResolveValue(ExecutionContext *exe_ctx)
724 const ClangASTType &clang_type = GetClangType();
725 if (clang_type.IsValid())
727 switch (m_value_type)
729 case eValueTypeScalar: // raw scalar value
733 case eValueTypeFileAddress:
734 case eValueTypeLoadAddress: // load address value
735 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
738 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
739 Error error (GetValueAsData (exe_ctx, data, 0, NULL));
743 if (clang_type.GetValueAsScalar (data, 0, data.GetByteSize(), scalar))
746 m_value_type = eValueTypeScalar;
750 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
753 m_value_type = eValueTypeScalar;
759 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
762 m_value_type = eValueTypeScalar;
775 if (m_context_type == eContextTypeVariable)
776 return static_cast<Variable *> (m_context);
785 m_clang_type.Clear();
786 m_value_type = eValueTypeScalar;
788 m_context_type = eContextTypeInvalid;
789 m_data_buffer.Clear();
794 Value::GetValueTypeAsCString (ValueType value_type)
798 case eValueTypeScalar: return "scalar";
799 case eValueTypeVector: return "vector";
800 case eValueTypeFileAddress: return "file address";
801 case eValueTypeLoadAddress: return "load address";
802 case eValueTypeHostAddress: return "host address";
808 Value::GetContextTypeAsCString (ContextType context_type)
810 switch (context_type)
812 case eContextTypeInvalid: return "invalid";
813 case eContextTypeRegisterInfo: return "RegisterInfo *";
814 case eContextTypeLLDBType: return "Type *";
815 case eContextTypeVariable: return "Variable *";
820 ValueList::ValueList (const ValueList &rhs)
822 m_values = rhs.m_values;
826 ValueList::operator= (const ValueList &rhs)
828 m_values = rhs.m_values;
833 ValueList::PushValue (const Value &value)
835 m_values.push_back (value);
841 return m_values.size();
845 ValueList::GetValueAtIndex (size_t idx)
849 return &(m_values[idx]);