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/Target.h"
32 using namespace lldb_private;
39 m_value_type (eValueTypeScalar),
40 m_context_type (eContextTypeInvalid),
45 Value::Value(const Scalar& scalar) :
50 m_value_type (eValueTypeScalar),
51 m_context_type (eContextTypeInvalid),
57 Value::Value(const uint8_t *bytes, int len) :
62 m_value_type (eValueTypeHostAddress),
63 m_context_type (eContextTypeInvalid),
66 m_data_buffer.CopyData(bytes, len);
67 m_value = (uintptr_t)m_data_buffer.GetBytes();
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 if ((uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)v.m_data_buffer.GetBytes())
81 m_data_buffer.CopyData(v.m_data_buffer.GetBytes(),
82 v.m_data_buffer.GetByteSize());
84 m_value = (uintptr_t)m_data_buffer.GetBytes();
89 Value::operator=(const Value &rhs)
93 m_value = rhs.m_value;
94 m_vector = rhs.m_vector;
95 m_clang_type = rhs.m_clang_type;
96 m_context = rhs.m_context;
97 m_value_type = rhs.m_value_type;
98 m_context_type = rhs.m_context_type;
99 if ((uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs.m_data_buffer.GetBytes())
101 m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(),
102 rhs.m_data_buffer.GetByteSize());
104 m_value = (uintptr_t)m_data_buffer.GetBytes();
111 Value::Dump (Stream* strm)
113 m_value.GetValue (strm, true);
114 strm->Printf(", value_type = %s, context = %p, context_type = %s",
115 Value::GetValueTypeAsCString(m_value_type),
117 Value::GetContextTypeAsCString(m_context_type));
121 Value::GetValueType() const
127 Value::GetValueAddressType () const
129 switch (m_value_type)
132 case eValueTypeScalar:
134 case eValueTypeLoadAddress: return eAddressTypeLoad;
135 case eValueTypeFileAddress: return eAddressTypeFile;
136 case eValueTypeHostAddress: return eAddressTypeHost;
138 return eAddressTypeInvalid;
142 Value::GetRegisterInfo() const
144 if (m_context_type == eContextTypeRegisterInfo)
145 return static_cast<RegisterInfo *> (m_context);
152 if (m_context_type == eContextTypeLLDBType)
153 return static_cast<Type *> (m_context);
158 Value::ResizeData(size_t len)
160 m_value_type = eValueTypeHostAddress;
161 m_data_buffer.SetByteSize(len);
162 m_value = (uintptr_t)m_data_buffer.GetBytes();
166 Value::ValueOf(ExecutionContext *exe_ctx)
168 switch (m_context_type)
170 case eContextTypeInvalid:
171 case eContextTypeRegisterInfo: // RegisterInfo *
172 case eContextTypeLLDBType: // Type *
175 case eContextTypeVariable: // Variable *
176 ResolveValue(exe_ctx);
183 Value::GetValueByteSize (Error *error_ptr)
185 uint64_t byte_size = 0;
187 switch (m_context_type)
189 case eContextTypeRegisterInfo: // RegisterInfo *
190 if (GetRegisterInfo())
191 byte_size = GetRegisterInfo()->byte_size;
194 case eContextTypeInvalid:
195 case eContextTypeLLDBType: // Type *
196 case eContextTypeVariable: // Variable *
198 const ClangASTType &ast_type = GetClangType();
199 if (ast_type.IsValid())
200 byte_size = ast_type.GetByteSize();
209 if (error_ptr->Success())
210 error_ptr->SetErrorString("Unable to determine byte size.");
221 Value::GetClangType ()
223 if (!m_clang_type.IsValid())
225 switch (m_context_type)
227 case eContextTypeInvalid:
230 case eContextTypeRegisterInfo:
231 break; // TODO: Eventually convert into a clang type?
233 case eContextTypeLLDBType:
235 Type *lldb_type = GetType();
237 m_clang_type = lldb_type->GetClangForwardType();
241 case eContextTypeVariable:
243 Variable *variable = GetVariable();
246 Type *variable_type = variable->GetType();
248 m_clang_type = variable_type->GetClangForwardType();
259 Value::SetClangType (const ClangASTType &clang_type)
261 m_clang_type = clang_type;
265 Value::GetValueDefaultFormat ()
267 switch (m_context_type)
269 case eContextTypeRegisterInfo:
270 if (GetRegisterInfo())
271 return GetRegisterInfo()->format;
274 case eContextTypeInvalid:
275 case eContextTypeLLDBType:
276 case eContextTypeVariable:
278 const ClangASTType &ast_type = GetClangType();
279 if (ast_type.IsValid())
280 return ast_type.GetFormat();
286 // Return a good default in case we can't figure anything out
291 Value::GetData (DataExtractor &data)
293 switch (m_value_type)
298 case eValueTypeScalar:
299 if (m_value.GetData (data))
303 case eValueTypeLoadAddress:
304 case eValueTypeFileAddress:
305 case eValueTypeHostAddress:
306 if (m_data_buffer.GetByteSize())
308 data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder());
319 Value::GetValueAsData (ExecutionContext *exe_ctx,
321 uint32_t data_offset,
327 lldb::addr_t address = LLDB_INVALID_ADDRESS;
328 AddressType address_type = eAddressTypeFile;
329 Address file_so_addr;
330 const ClangASTType &ast_type = GetClangType();
331 switch (m_value_type)
333 case eValueTypeVector:
334 if (ast_type.IsValid())
335 data.SetAddressByteSize (ast_type.GetPointerByteSize());
337 data.SetAddressByteSize(sizeof(void *));
338 data.SetData(m_vector.bytes, m_vector.length, m_vector.byte_order);
341 case eValueTypeScalar:
342 data.SetByteOrder (lldb::endian::InlHostByteOrder());
343 if (ast_type.IsValid())
344 data.SetAddressByteSize (ast_type.GetPointerByteSize());
346 data.SetAddressByteSize(sizeof(void *));
347 if (m_value.GetData (data))
348 return error; // Success;
349 error.SetErrorStringWithFormat("extracting data from value failed");
352 case eValueTypeLoadAddress:
355 error.SetErrorString ("can't read load address (no execution context)");
359 Process *process = exe_ctx->GetProcessPtr();
360 if (process == NULL || !process->IsAlive())
362 Target *target = exe_ctx->GetTargetPtr();
365 // Allow expressions to run and evaluate things when the target
366 // has memory sections loaded. This allows you to use "target modules load"
367 // to load your executable and any shared libraries, then execute
368 // commands where you can look at types in data sections.
369 const SectionLoadList &target_sections = target->GetSectionLoadList();
370 if (!target_sections.IsEmpty())
372 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
373 if (target_sections.ResolveLoadAddress(address, file_so_addr))
375 address_type = eAddressTypeLoad;
376 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
377 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
380 address = LLDB_INVALID_ADDRESS;
384 // ModuleSP exe_module_sp (target->GetExecutableModule());
385 // if (exe_module_sp)
387 // address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
388 // if (address != LLDB_INVALID_ADDRESS)
390 // if (exe_module_sp->ResolveFileAddress(address, file_so_addr))
392 // data.SetByteOrder(target->GetArchitecture().GetByteOrder());
393 // data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
394 // address_type = eAddressTypeFile;
398 // address = LLDB_INVALID_ADDRESS;
406 error.SetErrorString ("can't read load address (invalid process)");
411 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
412 address_type = eAddressTypeLoad;
413 data.SetByteOrder(process->GetTarget().GetArchitecture().GetByteOrder());
414 data.SetAddressByteSize(process->GetTarget().GetArchitecture().GetAddressByteSize());
419 case eValueTypeFileAddress:
422 error.SetErrorString ("can't read file address (no execution context)");
424 else if (exe_ctx->GetTargetPtr() == NULL)
426 error.SetErrorString ("can't read file address (invalid target)");
430 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
431 if (address == LLDB_INVALID_ADDRESS)
433 error.SetErrorString ("invalid file address");
439 // The only thing we can currently lock down to a module so that
440 // we can resolve a file address, is a variable.
441 Variable *variable = GetVariable();
444 SymbolContext var_sc;
445 variable->CalculateSymbolContext(&var_sc);
446 module = var_sc.module_sp.get();
452 bool resolved = false;
453 ObjectFile *objfile = module->GetObjectFile();
456 Address so_addr(address, objfile->GetSectionList());
457 addr_t load_address = so_addr.GetLoadAddress (exe_ctx->GetTargetPtr());
458 bool process_launched_and_stopped = exe_ctx->GetProcessPtr()
459 ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(), true /* must_exist */)
461 // Don't use the load address if the process has exited.
462 if (load_address != LLDB_INVALID_ADDRESS && process_launched_and_stopped)
465 address = load_address;
466 address_type = eAddressTypeLoad;
467 data.SetByteOrder(exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder());
468 data.SetAddressByteSize(exe_ctx->GetTargetRef().GetArchitecture().GetAddressByteSize());
472 if (so_addr.IsSectionOffset())
475 file_so_addr = so_addr;
476 data.SetByteOrder(objfile->GetByteOrder());
477 data.SetAddressByteSize(objfile->GetAddressByteSize());
483 Variable *variable = GetVariable();
488 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s' in %s",
490 variable->GetName().AsCString(""),
491 module->GetFileSpec().GetPath().c_str());
493 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " in %s",
495 module->GetFileSpec().GetPath().c_str());
500 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s'",
502 variable->GetName().AsCString(""));
504 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64, address);
510 // Can't convert a file address to anything valid without more
511 // context (which Module it came from)
512 error.SetErrorString ("can't read memory from file address without more context");
518 case eValueTypeHostAddress:
519 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
520 address_type = eAddressTypeHost;
523 Target *target = exe_ctx->GetTargetPtr();
526 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
527 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
531 // fallback to host settings
532 data.SetByteOrder(lldb::endian::InlHostByteOrder());
533 data.SetAddressByteSize(sizeof(void *));
537 // Bail if we encountered any errors
541 if (address == LLDB_INVALID_ADDRESS)
543 error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load");
547 // If we got here, we need to read the value from memory
548 size_t byte_size = GetValueByteSize (&error);
550 // Bail if we encountered any errors getting the byte size
554 // Make sure we have enough room within "data", and if we don't make
555 // something large enough that does
556 if (!data.ValidOffsetForDataOfSize (data_offset, byte_size))
558 DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0'));
559 data.SetData(data_sp);
562 uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size));
565 if (address_type == eAddressTypeHost)
567 // The address is an address in this process, so just copy it
568 memcpy (dst, (uint8_t*)NULL + address, byte_size);
570 else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile))
572 if (file_so_addr.IsValid())
574 // We have a file address that we were able to translate into a
575 // section offset address so we might be able to read this from
576 // the object files if we don't have a live process. Lets always
577 // try and read from the process if we have one though since we
578 // want to read the actual value by setting "prefer_file_cache"
580 const bool prefer_file_cache = false;
581 if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size)
583 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", (uint64_t)address);
588 // The execution context might have a NULL process, but it
589 // might have a valid process in the exe_ctx->target, so use
590 // the ExecutionContext::GetProcess accessor to ensure we
591 // get the process if there is one.
592 Process *process = exe_ctx->GetProcessPtr();
596 const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error);
597 if (bytes_read != byte_size)
598 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (%u of %u bytes read)",
600 (uint32_t)bytes_read,
601 (uint32_t)byte_size);
605 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (invalid process)", (uint64_t)address);
611 error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type);
616 error.SetErrorStringWithFormat ("out of memory");
623 Value::ResolveValue(ExecutionContext *exe_ctx)
625 const ClangASTType &clang_type = GetClangType();
626 if (clang_type.IsValid())
628 switch (m_value_type)
630 case eValueTypeScalar: // raw scalar value
634 case eValueTypeFileAddress:
635 case eValueTypeLoadAddress: // load address value
636 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
639 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
640 Error error (GetValueAsData (exe_ctx, data, 0, NULL));
644 if (clang_type.GetValueAsScalar (data, 0, data.GetByteSize(), scalar))
647 m_value_type = eValueTypeScalar;
651 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
654 m_value_type = eValueTypeScalar;
660 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
663 m_value_type = eValueTypeScalar;
676 if (m_context_type == eContextTypeVariable)
677 return static_cast<Variable *> (m_context);
686 m_clang_type.Clear();
687 m_value_type = eValueTypeScalar;
689 m_context_type = eContextTypeInvalid;
690 m_data_buffer.Clear();
695 Value::GetValueTypeAsCString (ValueType value_type)
699 case eValueTypeScalar: return "scalar";
700 case eValueTypeVector: return "vector";
701 case eValueTypeFileAddress: return "file address";
702 case eValueTypeLoadAddress: return "load address";
703 case eValueTypeHostAddress: return "host address";
709 Value::GetContextTypeAsCString (ContextType context_type)
711 switch (context_type)
713 case eContextTypeInvalid: return "invalid";
714 case eContextTypeRegisterInfo: return "RegisterInfo *";
715 case eContextTypeLLDBType: return "Type *";
716 case eContextTypeVariable: return "Variable *";
721 ValueList::ValueList (const ValueList &rhs)
723 m_values = rhs.m_values;
727 ValueList::operator= (const ValueList &rhs)
729 m_values = rhs.m_values;
734 ValueList::PushValue (const Value &value)
736 m_values.push_back (value);
742 return m_values.size();
746 ValueList::GetValueAtIndex (size_t idx)
750 return &(m_values[idx]);