llvm-capstone/lldb/source/Core/ValueObjectDynamicValue.cpp
Greg Clayton 84db9105d2 <rdar://problem/11113279>
Fixed type lookups to "do the right thing". Prior to this fix, looking up a type using "foo::bar" would result in a type list that contains all types that had "bar" as a basename unless the symbol file was able to match fully qualified names (which our DWARF parser does not). 

This fix will allow type matches to be made based on the basename and then have the types that don't match filtered out. Types by name can be fully qualified, or partially qualified with the new "bool exact_match" parameter to the Module::FindTypes() method.

This fixes some issue that we discovered with dynamic type resolution as well as improves the overall type lookups in LLDB.

llvm-svn: 153482
2012-03-26 23:03:23 +00:00

362 lines
11 KiB
C++

//===-- ValueObjectDynamicValue.cpp ---------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/ValueObjectDynamicValue.h"
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Module.h"
#include "lldb/Core/ValueObjectList.h"
#include "lldb/Core/Value.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/Type.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/LanguageRuntime.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
using namespace lldb_private;
lldb::ValueObjectSP
ValueObjectCast::Create (ValueObject &parent,
const ConstString &name,
const ClangASTType &cast_type)
{
ValueObjectCast *cast_valobj_ptr = new ValueObjectCast (parent, name, cast_type);
return cast_valobj_ptr->GetSP();
}
ValueObjectCast::ValueObjectCast
(
ValueObject &parent,
const ConstString &name,
const ClangASTType &cast_type
) :
ValueObject(parent),
m_cast_type (cast_type)
{
SetName (name);
m_value.SetContext (Value::eContextTypeClangType, cast_type.GetOpaqueQualType());
}
ValueObjectCast::~ValueObjectCast()
{
}
lldb::clang_type_t
ValueObjectCast::GetClangTypeImpl ()
{
return m_cast_type.GetOpaqueQualType();
}
uint32_t
ValueObjectCast::CalculateNumChildren()
{
return ClangASTContext::GetNumChildren (GetClangAST (), GetClangType(), true);
}
clang::ASTContext *
ValueObjectCast::GetClangASTImpl ()
{
return m_cast_type.GetASTContext();
}
size_t
ValueObjectCast::GetByteSize()
{
return m_value.GetValueByteSize(GetClangAST(), NULL);
}
lldb::ValueType
ValueObjectCast::GetValueType() const
{
// Let our parent answer global, local, argument, etc...
return m_parent->GetValueType();
}
bool
ValueObjectCast::UpdateValue ()
{
SetValueIsValid (false);
m_error.Clear();
if (m_parent->UpdateValueIfNeeded(false))
{
Value old_value(m_value);
m_update_point.SetUpdated();
m_value = m_parent->GetValue();
m_value.SetContext (Value::eContextTypeClangType, GetClangType());
SetAddressTypeOfChildren(m_parent->GetAddressTypeOfChildren());
if (ClangASTContext::IsAggregateType (GetClangType()))
{
// this value object represents an aggregate type whose
// children have values, but this object does not. So we
// say we are changed if our location has changed.
SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
}
ExecutionContext exe_ctx (GetExecutionContextRef());
m_error = m_value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
SetValueDidChange (m_parent->GetValueDidChange());
return true;
}
// The dynamic value failed to get an error, pass the error along
if (m_error.Success() && m_parent->GetError().Fail())
m_error = m_parent->GetError();
SetValueIsValid (false);
return false;
}
bool
ValueObjectCast::IsInScope ()
{
return m_parent->IsInScope();
}
//----------------------------------------------------------------------
ValueObjectDynamicValue::ValueObjectDynamicValue (ValueObject &parent, lldb::DynamicValueType use_dynamic) :
ValueObject(parent),
m_address (),
m_type_sp(),
m_use_dynamic (use_dynamic)
{
m_last_format_mgr_dynamic = use_dynamic;
SetName (parent.GetName());
}
ValueObjectDynamicValue::~ValueObjectDynamicValue()
{
m_owning_valobj_sp.reset();
}
lldb::clang_type_t
ValueObjectDynamicValue::GetClangTypeImpl ()
{
if (m_type_sp)
return m_value.GetClangType();
else
return m_parent->GetClangType();
}
ConstString
ValueObjectDynamicValue::GetTypeName()
{
const bool success = UpdateValueIfNeeded(false);
if (success && m_type_sp)
return ClangASTType::GetConstTypeName (GetClangAST(), GetClangType());
else
return m_parent->GetTypeName();
}
uint32_t
ValueObjectDynamicValue::CalculateNumChildren()
{
const bool success = UpdateValueIfNeeded(false);
if (success && m_type_sp)
return ClangASTContext::GetNumChildren (GetClangAST (), GetClangType(), true);
else
return m_parent->GetNumChildren();
}
clang::ASTContext *
ValueObjectDynamicValue::GetClangASTImpl ()
{
const bool success = UpdateValueIfNeeded(false);
if (success && m_type_sp)
return m_type_sp->GetClangAST();
else
return m_parent->GetClangAST ();
}
size_t
ValueObjectDynamicValue::GetByteSize()
{
const bool success = UpdateValueIfNeeded(false);
if (success && m_type_sp)
return m_value.GetValueByteSize(GetClangAST(), NULL);
else
return m_parent->GetByteSize();
}
lldb::ValueType
ValueObjectDynamicValue::GetValueType() const
{
return m_parent->GetValueType();
}
bool
ValueObjectDynamicValue::UpdateValue ()
{
SetValueIsValid (false);
m_error.Clear();
if (!m_parent->UpdateValueIfNeeded(false))
{
// The dynamic value failed to get an error, pass the error along
if (m_error.Success() && m_parent->GetError().Fail())
m_error = m_parent->GetError();
return false;
}
// Setting our type_sp to NULL will route everything back through our
// parent which is equivalent to not using dynamic values.
if (m_use_dynamic == lldb::eNoDynamicValues)
{
m_type_sp.reset();
return true;
}
ExecutionContext exe_ctx (GetExecutionContextRef());
Target *target = exe_ctx.GetTargetPtr();
if (target)
{
m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
}
// First make sure our Type and/or Address haven't changed:
Process *process = exe_ctx.GetProcessPtr();
if (!process)
return false;
TypeAndOrName class_type_or_name;
Address dynamic_address;
bool found_dynamic_type = false;
lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage();
if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
{
LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
if (runtime)
found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
}
else
{
LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
if (cpp_runtime)
found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
if (!found_dynamic_type)
{
LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
if (objc_runtime)
found_dynamic_type = objc_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
}
}
lldb::TypeSP dynamic_type_sp = class_type_or_name.GetTypeSP();
// Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really
// don't...
m_update_point.SetUpdated();
// If we don't have a dynamic type, then make ourselves just a echo of our parent.
// Or we could return false, and make ourselves an echo of our parent?
if (!found_dynamic_type)
{
if (m_type_sp)
SetValueDidChange(true);
m_value = m_parent->GetValue();
m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
return m_error.Success();
}
Value old_value(m_value);
if (!m_type_sp)
{
m_type_sp = dynamic_type_sp;
}
else if (dynamic_type_sp != m_type_sp)
{
// We are another type, we need to tear down our children...
m_type_sp = dynamic_type_sp;
SetValueDidChange (true);
}
if (!m_address.IsValid() || m_address != dynamic_address)
{
if (m_address.IsValid())
SetValueDidChange (true);
// We've moved, so we should be fine...
m_address = dynamic_address;
lldb::TargetSP target_sp (GetTargetSP());
lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
m_value.GetScalar() = load_address;
}
// The type will always be the type of the dynamic object. If our parent's type was a pointer,
// then our type should be a pointer to the type of the dynamic object. If a reference, then the original type
// should be okay...
lldb::clang_type_t orig_type = m_type_sp->GetClangForwardType();
lldb::clang_type_t corrected_type = orig_type;
if (m_parent->IsPointerType())
corrected_type = ClangASTContext::CreatePointerType (m_type_sp->GetClangAST(), orig_type);
else if (m_parent->IsPointerOrReferenceType())
corrected_type = ClangASTContext::CreateLValueReferenceType (m_type_sp->GetClangAST(), orig_type);
m_value.SetContext (Value::eContextTypeClangType, corrected_type);
// Our address is the location of the dynamic type stored in memory. It isn't a load address,
// because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us...
m_value.SetValueType(Value::eValueTypeScalar);
if (m_address.IsValid() && m_type_sp)
{
// The variable value is in the Scalar value inside the m_value.
// We can point our m_data right to it.
m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
if (m_error.Success())
{
if (ClangASTContext::IsAggregateType (GetClangType()))
{
// this value object represents an aggregate type whose
// children have values, but this object does not. So we
// say we are changed if our location has changed.
SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
}
SetValueIsValid (true);
return true;
}
}
// We get here if we've failed above...
SetValueIsValid (false);
return false;
}
bool
ValueObjectDynamicValue::IsInScope ()
{
return m_parent->IsInScope();
}