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 uint8_t *bytes, int len) :
63 m_value_type (eValueTypeHostAddress),
64 m_context_type (eContextTypeInvalid),
67 m_data_buffer.CopyData(bytes, len);
68 m_value = (uintptr_t)m_data_buffer.GetBytes();
71 Value::Value(const Value &v) :
73 m_vector (v.m_vector),
74 m_clang_type (v.m_clang_type),
75 m_context (v.m_context),
76 m_value_type (v.m_value_type),
77 m_context_type (v.m_context_type),
80 if ((uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (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 if ((uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs.m_data_buffer.GetBytes())
102 m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(),
103 rhs.m_data_buffer.GetByteSize());
105 m_value = (uintptr_t)m_data_buffer.GetBytes();
112 Value::Dump (Stream* strm)
114 m_value.GetValue (strm, true);
115 strm->Printf(", value_type = %s, context = %p, context_type = %s",
116 Value::GetValueTypeAsCString(m_value_type),
118 Value::GetContextTypeAsCString(m_context_type));
122 Value::GetValueType() const
128 Value::GetValueAddressType () const
130 switch (m_value_type)
133 case eValueTypeScalar:
135 case eValueTypeLoadAddress: return eAddressTypeLoad;
136 case eValueTypeFileAddress: return eAddressTypeFile;
137 case eValueTypeHostAddress: return eAddressTypeHost;
139 return eAddressTypeInvalid;
143 Value::GetRegisterInfo() const
145 if (m_context_type == eContextTypeRegisterInfo)
146 return static_cast<RegisterInfo *> (m_context);
153 if (m_context_type == eContextTypeLLDBType)
154 return static_cast<Type *> (m_context);
159 Value::ResizeData(size_t len)
161 m_value_type = eValueTypeHostAddress;
162 m_data_buffer.SetByteSize(len);
163 m_value = (uintptr_t)m_data_buffer.GetBytes();
167 Value::ValueOf(ExecutionContext *exe_ctx)
169 switch (m_context_type)
171 case eContextTypeInvalid:
172 case eContextTypeRegisterInfo: // RegisterInfo *
173 case eContextTypeLLDBType: // Type *
176 case eContextTypeVariable: // Variable *
177 ResolveValue(exe_ctx);
184 Value::GetValueByteSize (Error *error_ptr)
186 uint64_t byte_size = 0;
188 switch (m_context_type)
190 case eContextTypeRegisterInfo: // RegisterInfo *
191 if (GetRegisterInfo())
192 byte_size = GetRegisterInfo()->byte_size;
195 case eContextTypeInvalid:
196 case eContextTypeLLDBType: // Type *
197 case eContextTypeVariable: // Variable *
199 const ClangASTType &ast_type = GetClangType();
200 if (ast_type.IsValid())
201 byte_size = ast_type.GetByteSize();
210 if (error_ptr->Success())
211 error_ptr->SetErrorString("Unable to determine byte size.");
222 Value::GetClangType ()
224 if (!m_clang_type.IsValid())
226 switch (m_context_type)
228 case eContextTypeInvalid:
231 case eContextTypeRegisterInfo:
232 break; // TODO: Eventually convert into a clang type?
234 case eContextTypeLLDBType:
236 Type *lldb_type = GetType();
238 m_clang_type = lldb_type->GetClangForwardType();
242 case eContextTypeVariable:
244 Variable *variable = GetVariable();
247 Type *variable_type = variable->GetType();
249 m_clang_type = variable_type->GetClangForwardType();
260 Value::SetClangType (const ClangASTType &clang_type)
262 m_clang_type = clang_type;
266 Value::GetValueDefaultFormat ()
268 switch (m_context_type)
270 case eContextTypeRegisterInfo:
271 if (GetRegisterInfo())
272 return GetRegisterInfo()->format;
275 case eContextTypeInvalid:
276 case eContextTypeLLDBType:
277 case eContextTypeVariable:
279 const ClangASTType &ast_type = GetClangType();
280 if (ast_type.IsValid())
281 return ast_type.GetFormat();
287 // Return a good default in case we can't figure anything out
292 Value::GetData (DataExtractor &data)
294 switch (m_value_type)
299 case eValueTypeScalar:
300 if (m_value.GetData (data))
304 case eValueTypeLoadAddress:
305 case eValueTypeFileAddress:
306 case eValueTypeHostAddress:
307 if (m_data_buffer.GetByteSize())
309 data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder());
320 Value::GetValueAsData (ExecutionContext *exe_ctx,
322 uint32_t data_offset,
328 lldb::addr_t address = LLDB_INVALID_ADDRESS;
329 AddressType address_type = eAddressTypeFile;
330 Address file_so_addr;
331 const ClangASTType &ast_type = GetClangType();
332 switch (m_value_type)
334 case eValueTypeVector:
335 if (ast_type.IsValid())
336 data.SetAddressByteSize (ast_type.GetPointerByteSize());
338 data.SetAddressByteSize(sizeof(void *));
339 data.SetData(m_vector.bytes, m_vector.length, m_vector.byte_order);
342 case eValueTypeScalar:
344 data.SetByteOrder (lldb::endian::InlHostByteOrder());
345 if (ast_type.IsValid())
346 data.SetAddressByteSize (ast_type.GetPointerByteSize());
348 data.SetAddressByteSize(sizeof(void *));
350 uint32_t limit_byte_size = UINT32_MAX;
352 if (ast_type.IsValid() && ast_type.IsScalarType())
354 uint64_t type_encoding_count = 0;
355 lldb::Encoding type_encoding = ast_type.GetEncoding(type_encoding_count);
357 if (type_encoding == eEncodingUint || type_encoding == eEncodingSint)
358 limit_byte_size = ast_type.GetByteSize();
361 if (m_value.GetData (data, limit_byte_size))
362 return error; // Success;
364 error.SetErrorStringWithFormat("extracting data from value failed");
367 case eValueTypeLoadAddress:
370 error.SetErrorString ("can't read load address (no execution context)");
374 Process *process = exe_ctx->GetProcessPtr();
375 if (process == NULL || !process->IsAlive())
377 Target *target = exe_ctx->GetTargetPtr();
380 // Allow expressions to run and evaluate things when the target
381 // has memory sections loaded. This allows you to use "target modules load"
382 // to load your executable and any shared libraries, then execute
383 // commands where you can look at types in data sections.
384 const SectionLoadList &target_sections = target->GetSectionLoadList();
385 if (!target_sections.IsEmpty())
387 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
388 if (target_sections.ResolveLoadAddress(address, file_so_addr))
390 address_type = eAddressTypeLoad;
391 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
392 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
395 address = LLDB_INVALID_ADDRESS;
399 // ModuleSP exe_module_sp (target->GetExecutableModule());
400 // if (exe_module_sp)
402 // address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
403 // if (address != LLDB_INVALID_ADDRESS)
405 // if (exe_module_sp->ResolveFileAddress(address, file_so_addr))
407 // data.SetByteOrder(target->GetArchitecture().GetByteOrder());
408 // data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
409 // address_type = eAddressTypeFile;
413 // address = LLDB_INVALID_ADDRESS;
421 error.SetErrorString ("can't read load address (invalid process)");
426 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
427 address_type = eAddressTypeLoad;
428 data.SetByteOrder(process->GetTarget().GetArchitecture().GetByteOrder());
429 data.SetAddressByteSize(process->GetTarget().GetArchitecture().GetAddressByteSize());
434 case eValueTypeFileAddress:
437 error.SetErrorString ("can't read file address (no execution context)");
439 else if (exe_ctx->GetTargetPtr() == NULL)
441 error.SetErrorString ("can't read file address (invalid target)");
445 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
446 if (address == LLDB_INVALID_ADDRESS)
448 error.SetErrorString ("invalid file address");
454 // The only thing we can currently lock down to a module so that
455 // we can resolve a file address, is a variable.
456 Variable *variable = GetVariable();
459 SymbolContext var_sc;
460 variable->CalculateSymbolContext(&var_sc);
461 module = var_sc.module_sp.get();
467 bool resolved = false;
468 ObjectFile *objfile = module->GetObjectFile();
471 Address so_addr(address, objfile->GetSectionList());
472 addr_t load_address = so_addr.GetLoadAddress (exe_ctx->GetTargetPtr());
473 bool process_launched_and_stopped = exe_ctx->GetProcessPtr()
474 ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(), true /* must_exist */)
476 // Don't use the load address if the process has exited.
477 if (load_address != LLDB_INVALID_ADDRESS && process_launched_and_stopped)
480 address = load_address;
481 address_type = eAddressTypeLoad;
482 data.SetByteOrder(exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder());
483 data.SetAddressByteSize(exe_ctx->GetTargetRef().GetArchitecture().GetAddressByteSize());
487 if (so_addr.IsSectionOffset())
490 file_so_addr = so_addr;
491 data.SetByteOrder(objfile->GetByteOrder());
492 data.SetAddressByteSize(objfile->GetAddressByteSize());
498 Variable *variable = GetVariable();
503 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s' in %s",
505 variable->GetName().AsCString(""),
506 module->GetFileSpec().GetPath().c_str());
508 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " in %s",
510 module->GetFileSpec().GetPath().c_str());
515 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s'",
517 variable->GetName().AsCString(""));
519 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64, address);
525 // Can't convert a file address to anything valid without more
526 // context (which Module it came from)
527 error.SetErrorString ("can't read memory from file address without more context");
533 case eValueTypeHostAddress:
534 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
535 address_type = eAddressTypeHost;
538 Target *target = exe_ctx->GetTargetPtr();
541 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
542 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
546 // fallback to host settings
547 data.SetByteOrder(lldb::endian::InlHostByteOrder());
548 data.SetAddressByteSize(sizeof(void *));
552 // Bail if we encountered any errors
556 if (address == LLDB_INVALID_ADDRESS)
558 error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load");
562 // If we got here, we need to read the value from memory
563 size_t byte_size = GetValueByteSize (&error);
565 // Bail if we encountered any errors getting the byte size
569 // Make sure we have enough room within "data", and if we don't make
570 // something large enough that does
571 if (!data.ValidOffsetForDataOfSize (data_offset, byte_size))
573 DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0'));
574 data.SetData(data_sp);
577 uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size));
580 if (address_type == eAddressTypeHost)
582 // The address is an address in this process, so just copy it
583 memcpy (dst, (uint8_t*)NULL + address, byte_size);
585 else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile))
587 if (file_so_addr.IsValid())
589 // We have a file address that we were able to translate into a
590 // section offset address so we might be able to read this from
591 // the object files if we don't have a live process. Lets always
592 // try and read from the process if we have one though since we
593 // want to read the actual value by setting "prefer_file_cache"
595 const bool prefer_file_cache = false;
596 if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size)
598 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", (uint64_t)address);
603 // The execution context might have a NULL process, but it
604 // might have a valid process in the exe_ctx->target, so use
605 // the ExecutionContext::GetProcess accessor to ensure we
606 // get the process if there is one.
607 Process *process = exe_ctx->GetProcessPtr();
611 const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error);
612 if (bytes_read != byte_size)
613 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (%u of %u bytes read)",
615 (uint32_t)bytes_read,
616 (uint32_t)byte_size);
620 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (invalid process)", (uint64_t)address);
626 error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type);
631 error.SetErrorStringWithFormat ("out of memory");
638 Value::ResolveValue(ExecutionContext *exe_ctx)
640 const ClangASTType &clang_type = GetClangType();
641 if (clang_type.IsValid())
643 switch (m_value_type)
645 case eValueTypeScalar: // raw scalar value
649 case eValueTypeFileAddress:
650 case eValueTypeLoadAddress: // load address value
651 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
654 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
655 Error error (GetValueAsData (exe_ctx, data, 0, NULL));
659 if (clang_type.GetValueAsScalar (data, 0, data.GetByteSize(), scalar))
662 m_value_type = eValueTypeScalar;
666 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
669 m_value_type = eValueTypeScalar;
675 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
678 m_value_type = eValueTypeScalar;
691 if (m_context_type == eContextTypeVariable)
692 return static_cast<Variable *> (m_context);
701 m_clang_type.Clear();
702 m_value_type = eValueTypeScalar;
704 m_context_type = eContextTypeInvalid;
705 m_data_buffer.Clear();
710 Value::GetValueTypeAsCString (ValueType value_type)
714 case eValueTypeScalar: return "scalar";
715 case eValueTypeVector: return "vector";
716 case eValueTypeFileAddress: return "file address";
717 case eValueTypeLoadAddress: return "load address";
718 case eValueTypeHostAddress: return "host address";
724 Value::GetContextTypeAsCString (ContextType context_type)
726 switch (context_type)
728 case eContextTypeInvalid: return "invalid";
729 case eContextTypeRegisterInfo: return "RegisterInfo *";
730 case eContextTypeLLDBType: return "Type *";
731 case eContextTypeVariable: return "Variable *";
736 ValueList::ValueList (const ValueList &rhs)
738 m_values = rhs.m_values;
742 ValueList::operator= (const ValueList &rhs)
744 m_values = rhs.m_values;
749 ValueList::PushValue (const Value &value)
751 m_values.push_back (value);
757 return m_values.size();
761 ValueList::GetValueAtIndex (size_t idx)
765 return &(m_values[idx]);