Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame^] | 1 | //===-- ValueObjectDynamicValue.cpp ---------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
| 11 | #include "lldb/Core/ValueObjectDynamicValue.h" |
| 12 | |
| 13 | // C Includes |
| 14 | // C++ Includes |
| 15 | // Other libraries and framework includes |
| 16 | // Project includes |
| 17 | #include "lldb/Core/Module.h" |
| 18 | #include "lldb/Core/ValueObjectList.h" |
| 19 | #include "lldb/Core/Value.h" |
| 20 | #include "lldb/Core/ValueObject.h" |
| 21 | |
| 22 | #include "lldb/Symbol/ObjectFile.h" |
| 23 | #include "lldb/Symbol/SymbolContext.h" |
| 24 | #include "lldb/Symbol/Type.h" |
| 25 | #include "lldb/Symbol/Variable.h" |
| 26 | |
| 27 | #include "lldb/Target/ExecutionContext.h" |
| 28 | #include "lldb/Target/LanguageRuntime.h" |
| 29 | #include "lldb/Target/Process.h" |
| 30 | #include "lldb/Target/RegisterContext.h" |
| 31 | #include "lldb/Target/Target.h" |
| 32 | #include "lldb/Target/Thread.h" |
| 33 | |
| 34 | |
| 35 | using namespace lldb_private; |
| 36 | |
| 37 | ValueObjectDynamicValue::ValueObjectDynamicValue (ValueObject &parent) : |
| 38 | ValueObject(parent), |
| 39 | m_address (), |
| 40 | m_type_sp() |
| 41 | { |
| 42 | // THINK ABOUT: It looks ugly to doctor up the name like this. But if |
| 43 | // people find it confusing to tell the difference, we may want to do something... |
| 44 | |
| 45 | // std::string dynamic_name ("<dynamic value for \""); |
| 46 | // dynamic_name.append(parent.GetName().AsCString()); |
| 47 | // dynamic_name.append("\">"); |
| 48 | // |
| 49 | // SetName (dynamic_name.c_str()); |
| 50 | SetName (parent.GetName().AsCString()); |
| 51 | } |
| 52 | |
| 53 | ValueObjectDynamicValue::~ValueObjectDynamicValue() |
| 54 | { |
| 55 | m_owning_valobj_sp.reset(); |
| 56 | } |
| 57 | |
| 58 | lldb::clang_type_t |
| 59 | ValueObjectDynamicValue::GetClangType () |
| 60 | { |
| 61 | if (m_type_sp) |
| 62 | return m_value.GetClangType(); |
| 63 | else |
| 64 | return m_parent->GetClangType(); |
| 65 | } |
| 66 | |
| 67 | ConstString |
| 68 | ValueObjectDynamicValue::GetTypeName() |
| 69 | { |
| 70 | // FIXME: Maybe cache the name, but have to clear it out if the type changes... |
| 71 | if (!UpdateValueIfNeeded()) |
| 72 | return ConstString("<unknown type>"); |
| 73 | |
| 74 | if (m_type_sp) |
| 75 | return ClangASTType::GetClangTypeName (GetClangType()); |
| 76 | else |
| 77 | return m_parent->GetTypeName(); |
| 78 | } |
| 79 | |
| 80 | uint32_t |
| 81 | ValueObjectDynamicValue::CalculateNumChildren() |
| 82 | { |
| 83 | if (!UpdateValueIfNeeded()) |
| 84 | return 0; |
| 85 | |
| 86 | if (m_type_sp) |
| 87 | return ClangASTContext::GetNumChildren (GetClangAST (), GetClangType(), true); |
| 88 | else |
| 89 | return m_parent->GetNumChildren(); |
| 90 | } |
| 91 | |
| 92 | clang::ASTContext * |
| 93 | ValueObjectDynamicValue::GetClangAST () |
| 94 | { |
| 95 | if (!UpdateValueIfNeeded()) |
| 96 | return NULL; |
| 97 | |
| 98 | if (m_type_sp) |
| 99 | return m_type_sp->GetClangAST(); |
| 100 | else |
| 101 | return m_parent->GetClangAST (); |
| 102 | } |
| 103 | |
| 104 | size_t |
| 105 | ValueObjectDynamicValue::GetByteSize() |
| 106 | { |
| 107 | if (!UpdateValueIfNeeded()) |
| 108 | return 0; |
| 109 | |
| 110 | if (m_type_sp) |
| 111 | return m_value.GetValueByteSize(GetClangAST(), NULL); |
| 112 | else |
| 113 | return m_parent->GetByteSize(); |
| 114 | } |
| 115 | |
| 116 | lldb::ValueType |
| 117 | ValueObjectDynamicValue::GetValueType() const |
| 118 | { |
| 119 | return m_parent->GetValueType(); |
| 120 | } |
| 121 | |
| 122 | bool |
| 123 | ValueObjectDynamicValue::UpdateValue () |
| 124 | { |
| 125 | SetValueIsValid (false); |
| 126 | m_error.Clear(); |
| 127 | |
| 128 | if (!m_parent->UpdateValueIfNeeded()) |
| 129 | { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | ExecutionContext exe_ctx (GetExecutionContextScope()); |
| 134 | |
| 135 | if (exe_ctx.target) |
| 136 | { |
| 137 | m_data.SetByteOrder(exe_ctx.target->GetArchitecture().GetByteOrder()); |
| 138 | m_data.SetAddressByteSize(exe_ctx.target->GetArchitecture().GetAddressByteSize()); |
| 139 | } |
| 140 | |
| 141 | // First make sure our Type and/or Address haven't changed: |
| 142 | Process *process = m_update_point.GetProcess(); |
| 143 | if (!process) |
| 144 | return false; |
| 145 | |
| 146 | lldb::TypeSP dynamic_type_sp; |
| 147 | Address dynamic_address; |
| 148 | bool found_dynamic_type = false; |
| 149 | |
| 150 | lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage(); |
| 151 | if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC) |
| 152 | { |
| 153 | LanguageRuntime *runtime = process->GetLanguageRuntime (known_type); |
| 154 | if (runtime) |
| 155 | found_dynamic_type = runtime->GetDynamicValue(*m_parent, dynamic_type_sp, dynamic_address); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus); |
| 160 | if (cpp_runtime) |
| 161 | found_dynamic_type = cpp_runtime->GetDynamicValue(*m_parent, dynamic_type_sp, dynamic_address); |
| 162 | |
| 163 | if (!found_dynamic_type) |
| 164 | { |
| 165 | LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC); |
| 166 | if (objc_runtime) |
| 167 | found_dynamic_type = cpp_runtime->GetDynamicValue(*m_parent, dynamic_type_sp, dynamic_address); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // If we don't have a dynamic type, then make ourselves just a echo of our parent. |
| 172 | // Or we could return false, and make ourselves an echo of our parent? |
| 173 | if (!found_dynamic_type) |
| 174 | { |
| 175 | if (m_type_sp) |
| 176 | SetValueDidChange(true); |
| 177 | m_value = m_parent->GetValue(); |
| 178 | m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0); |
| 179 | return m_error.Success(); |
| 180 | } |
| 181 | |
| 182 | Value old_value(m_value); |
| 183 | |
| 184 | if (!m_type_sp) |
| 185 | { |
| 186 | m_type_sp = dynamic_type_sp; |
| 187 | } |
| 188 | else if (dynamic_type_sp != m_type_sp) |
| 189 | { |
| 190 | // We are another type, we need to tear down our children... |
| 191 | m_type_sp = dynamic_type_sp; |
| 192 | SetValueDidChange (true); |
| 193 | } |
| 194 | |
| 195 | if (!m_address.IsValid() || m_address != dynamic_address) |
| 196 | { |
| 197 | if (m_address.IsValid()) |
| 198 | SetValueDidChange (true); |
| 199 | |
| 200 | // We've moved, so we should be fine... |
| 201 | m_address = dynamic_address; |
| 202 | lldb::addr_t load_address = m_address.GetLoadAddress(m_update_point.GetTarget()); |
| 203 | m_value.GetScalar() = load_address; |
| 204 | } |
| 205 | |
| 206 | // The type will always be the type of the dynamic object. If our parent's type was a pointer, |
| 207 | // then our type should be a pointer to the type of the dynamic object. If a reference, then the original type |
| 208 | // should be okay... |
| 209 | lldb::clang_type_t orig_type = m_type_sp->GetClangForwardType(); |
| 210 | lldb::clang_type_t corrected_type = orig_type; |
| 211 | if (m_parent->IsPointerType()) |
| 212 | corrected_type = ClangASTContext::CreatePointerType (m_type_sp->GetClangAST(), orig_type); |
| 213 | else if (m_parent->IsPointerOrReferenceType()) |
| 214 | corrected_type = ClangASTContext::CreateLValueReferenceType (m_type_sp->GetClangAST(), orig_type); |
| 215 | |
| 216 | m_value.SetContext (Value::eContextTypeClangType, corrected_type); |
| 217 | |
| 218 | // Our address is the location of the dynamic type stored in memory. It isn't a load address, |
| 219 | // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us... |
| 220 | m_value.SetValueType(Value::eValueTypeScalar); |
| 221 | |
| 222 | if (m_address.IsValid() && m_type_sp) |
| 223 | { |
| 224 | // The variable value is in the Scalar value inside the m_value. |
| 225 | // We can point our m_data right to it. |
| 226 | m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0); |
| 227 | if (m_error.Success()) |
| 228 | { |
| 229 | if (ClangASTContext::IsAggregateType (GetClangType())) |
| 230 | { |
| 231 | // this value object represents an aggregate type whose |
| 232 | // children have values, but this object does not. So we |
| 233 | // say we are changed if our location has changed. |
| 234 | SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar()); |
| 235 | } |
| 236 | |
| 237 | SetValueIsValid (true); |
| 238 | return true; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // We get here if we've failed above... |
| 243 | SetValueIsValid (false); |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
| 249 | bool |
| 250 | ValueObjectDynamicValue::IsInScope () |
| 251 | { |
| 252 | return m_parent->IsInScope(); |
| 253 | } |
| 254 | |