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 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 37 | ValueObjectDynamicValue::ValueObjectDynamicValue (ValueObject &parent, lldb::DynamicValueType use_dynamic) : |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 38 | ValueObject(parent), |
| 39 | m_address (), |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 40 | m_type_sp(), |
| 41 | m_use_dynamic (use_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 42 | { |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 43 | SetName (parent.GetName().AsCString()); |
| 44 | } |
| 45 | |
| 46 | ValueObjectDynamicValue::~ValueObjectDynamicValue() |
| 47 | { |
| 48 | m_owning_valobj_sp.reset(); |
| 49 | } |
| 50 | |
| 51 | lldb::clang_type_t |
| 52 | ValueObjectDynamicValue::GetClangType () |
| 53 | { |
| 54 | if (m_type_sp) |
| 55 | return m_value.GetClangType(); |
| 56 | else |
| 57 | return m_parent->GetClangType(); |
| 58 | } |
| 59 | |
| 60 | ConstString |
| 61 | ValueObjectDynamicValue::GetTypeName() |
| 62 | { |
| 63 | // FIXME: Maybe cache the name, but have to clear it out if the type changes... |
| 64 | if (!UpdateValueIfNeeded()) |
| 65 | return ConstString("<unknown type>"); |
| 66 | |
| 67 | if (m_type_sp) |
| 68 | return ClangASTType::GetClangTypeName (GetClangType()); |
| 69 | else |
| 70 | return m_parent->GetTypeName(); |
| 71 | } |
| 72 | |
| 73 | uint32_t |
| 74 | ValueObjectDynamicValue::CalculateNumChildren() |
| 75 | { |
| 76 | if (!UpdateValueIfNeeded()) |
| 77 | return 0; |
| 78 | |
| 79 | if (m_type_sp) |
| 80 | return ClangASTContext::GetNumChildren (GetClangAST (), GetClangType(), true); |
| 81 | else |
| 82 | return m_parent->GetNumChildren(); |
| 83 | } |
| 84 | |
| 85 | clang::ASTContext * |
| 86 | ValueObjectDynamicValue::GetClangAST () |
| 87 | { |
| 88 | if (!UpdateValueIfNeeded()) |
| 89 | return NULL; |
| 90 | |
| 91 | if (m_type_sp) |
| 92 | return m_type_sp->GetClangAST(); |
| 93 | else |
| 94 | return m_parent->GetClangAST (); |
| 95 | } |
| 96 | |
| 97 | size_t |
| 98 | ValueObjectDynamicValue::GetByteSize() |
| 99 | { |
| 100 | if (!UpdateValueIfNeeded()) |
| 101 | return 0; |
| 102 | |
| 103 | if (m_type_sp) |
| 104 | return m_value.GetValueByteSize(GetClangAST(), NULL); |
| 105 | else |
| 106 | return m_parent->GetByteSize(); |
| 107 | } |
| 108 | |
| 109 | lldb::ValueType |
| 110 | ValueObjectDynamicValue::GetValueType() const |
| 111 | { |
| 112 | return m_parent->GetValueType(); |
| 113 | } |
| 114 | |
| 115 | bool |
| 116 | ValueObjectDynamicValue::UpdateValue () |
| 117 | { |
| 118 | SetValueIsValid (false); |
| 119 | m_error.Clear(); |
| 120 | |
| 121 | if (!m_parent->UpdateValueIfNeeded()) |
| 122 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame] | 123 | // The dynamic value failed to get an error, pass the error along |
| 124 | if (m_error.Success() && m_parent->GetError().Fail()) |
| 125 | m_error = m_parent->GetError(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 129 | // Setting our type_sp to NULL will route everything back through our |
| 130 | // parent which is equivalent to not using dynamic values. |
| 131 | if (m_use_dynamic == lldb::eNoDynamicValues) |
| 132 | { |
| 133 | m_type_sp.reset(); |
| 134 | return true; |
| 135 | } |
| 136 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 137 | ExecutionContext exe_ctx (GetExecutionContextScope()); |
| 138 | |
| 139 | if (exe_ctx.target) |
| 140 | { |
| 141 | m_data.SetByteOrder(exe_ctx.target->GetArchitecture().GetByteOrder()); |
| 142 | m_data.SetAddressByteSize(exe_ctx.target->GetArchitecture().GetAddressByteSize()); |
| 143 | } |
| 144 | |
| 145 | // First make sure our Type and/or Address haven't changed: |
| 146 | Process *process = m_update_point.GetProcess(); |
| 147 | if (!process) |
| 148 | return false; |
| 149 | |
Jim Ingham | 61be090 | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 150 | TypeAndOrName class_type_or_name; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 151 | Address dynamic_address; |
| 152 | bool found_dynamic_type = false; |
| 153 | |
| 154 | lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage(); |
| 155 | if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC) |
| 156 | { |
| 157 | LanguageRuntime *runtime = process->GetLanguageRuntime (known_type); |
| 158 | if (runtime) |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 159 | found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 160 | } |
| 161 | else |
| 162 | { |
| 163 | LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus); |
| 164 | if (cpp_runtime) |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 165 | found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 166 | |
| 167 | if (!found_dynamic_type) |
| 168 | { |
| 169 | LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC); |
| 170 | if (objc_runtime) |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 171 | found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Jim Ingham | 61be090 | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 175 | lldb::TypeSP dynamic_type_sp = class_type_or_name.GetTypeSP(); |
| 176 | |
| 177 | // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really |
| 178 | // don't... |
| 179 | |
| 180 | m_update_point.SetUpdated(); |
| 181 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 182 | // If we don't have a dynamic type, then make ourselves just a echo of our parent. |
| 183 | // Or we could return false, and make ourselves an echo of our parent? |
| 184 | if (!found_dynamic_type) |
| 185 | { |
| 186 | if (m_type_sp) |
| 187 | SetValueDidChange(true); |
| 188 | m_value = m_parent->GetValue(); |
| 189 | m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0); |
| 190 | return m_error.Success(); |
| 191 | } |
| 192 | |
| 193 | Value old_value(m_value); |
| 194 | |
| 195 | if (!m_type_sp) |
| 196 | { |
| 197 | m_type_sp = dynamic_type_sp; |
| 198 | } |
| 199 | else if (dynamic_type_sp != m_type_sp) |
| 200 | { |
| 201 | // We are another type, we need to tear down our children... |
| 202 | m_type_sp = dynamic_type_sp; |
| 203 | SetValueDidChange (true); |
| 204 | } |
| 205 | |
| 206 | if (!m_address.IsValid() || m_address != dynamic_address) |
| 207 | { |
| 208 | if (m_address.IsValid()) |
| 209 | SetValueDidChange (true); |
| 210 | |
| 211 | // We've moved, so we should be fine... |
| 212 | m_address = dynamic_address; |
| 213 | lldb::addr_t load_address = m_address.GetLoadAddress(m_update_point.GetTarget()); |
| 214 | m_value.GetScalar() = load_address; |
| 215 | } |
| 216 | |
| 217 | // The type will always be the type of the dynamic object. If our parent's type was a pointer, |
| 218 | // then our type should be a pointer to the type of the dynamic object. If a reference, then the original type |
| 219 | // should be okay... |
| 220 | lldb::clang_type_t orig_type = m_type_sp->GetClangForwardType(); |
| 221 | lldb::clang_type_t corrected_type = orig_type; |
| 222 | if (m_parent->IsPointerType()) |
| 223 | corrected_type = ClangASTContext::CreatePointerType (m_type_sp->GetClangAST(), orig_type); |
| 224 | else if (m_parent->IsPointerOrReferenceType()) |
| 225 | corrected_type = ClangASTContext::CreateLValueReferenceType (m_type_sp->GetClangAST(), orig_type); |
| 226 | |
| 227 | m_value.SetContext (Value::eContextTypeClangType, corrected_type); |
| 228 | |
| 229 | // Our address is the location of the dynamic type stored in memory. It isn't a load address, |
| 230 | // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us... |
| 231 | m_value.SetValueType(Value::eValueTypeScalar); |
| 232 | |
| 233 | if (m_address.IsValid() && m_type_sp) |
| 234 | { |
| 235 | // The variable value is in the Scalar value inside the m_value. |
| 236 | // We can point our m_data right to it. |
| 237 | m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0); |
| 238 | if (m_error.Success()) |
| 239 | { |
| 240 | if (ClangASTContext::IsAggregateType (GetClangType())) |
| 241 | { |
| 242 | // this value object represents an aggregate type whose |
| 243 | // children have values, but this object does not. So we |
| 244 | // say we are changed if our location has changed. |
| 245 | SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar()); |
| 246 | } |
| 247 | |
| 248 | SetValueIsValid (true); |
| 249 | return true; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // We get here if we've failed above... |
| 254 | SetValueIsValid (false); |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | |
| 260 | bool |
| 261 | ValueObjectDynamicValue::IsInScope () |
| 262 | { |
| 263 | return m_parent->IsInScope(); |
| 264 | } |
| 265 | |