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 |
Enrico Granata | d228483 | 2012-10-17 22:23:56 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Log.h" |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Module.h" |
| 19 | #include "lldb/Core/ValueObjectList.h" |
| 20 | #include "lldb/Core/Value.h" |
| 21 | #include "lldb/Core/ValueObject.h" |
| 22 | |
Enrico Granata | 21fd13f | 2012-10-27 02:05:48 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/ClangASTType.h" |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/ObjectFile.h" |
| 25 | #include "lldb/Symbol/SymbolContext.h" |
| 26 | #include "lldb/Symbol/Type.h" |
| 27 | #include "lldb/Symbol/Variable.h" |
| 28 | |
| 29 | #include "lldb/Target/ExecutionContext.h" |
| 30 | #include "lldb/Target/LanguageRuntime.h" |
| 31 | #include "lldb/Target/Process.h" |
| 32 | #include "lldb/Target/RegisterContext.h" |
| 33 | #include "lldb/Target/Target.h" |
| 34 | #include "lldb/Target/Thread.h" |
| 35 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 36 | using namespace lldb_private; |
| 37 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 38 | ValueObjectDynamicValue::ValueObjectDynamicValue (ValueObject &parent, lldb::DynamicValueType use_dynamic) : |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 39 | ValueObject(parent), |
| 40 | m_address (), |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 41 | m_type_sp(), |
| 42 | m_use_dynamic (use_dynamic) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 43 | { |
Enrico Granata | d8b5fce | 2011-08-02 23:12:24 +0000 | [diff] [blame] | 44 | m_last_format_mgr_dynamic = use_dynamic; |
Enrico Granata | 6f3533f | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 45 | SetName (parent.GetName()); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ValueObjectDynamicValue::~ValueObjectDynamicValue() |
| 49 | { |
| 50 | m_owning_valobj_sp.reset(); |
| 51 | } |
| 52 | |
| 53 | lldb::clang_type_t |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 54 | ValueObjectDynamicValue::GetClangTypeImpl () |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 55 | { |
| 56 | if (m_type_sp) |
| 57 | return m_value.GetClangType(); |
| 58 | else |
| 59 | return m_parent->GetClangType(); |
| 60 | } |
| 61 | |
| 62 | ConstString |
| 63 | ValueObjectDynamicValue::GetTypeName() |
| 64 | { |
Enrico Granata | c3e320a | 2011-08-02 17:27:39 +0000 | [diff] [blame] | 65 | const bool success = UpdateValueIfNeeded(false); |
Greg Clayton | 5ad6394 | 2011-05-31 20:18:39 +0000 | [diff] [blame] | 66 | if (success && m_type_sp) |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 67 | return ClangASTType::GetConstTypeName (GetClangAST(), GetClangType()); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 68 | else |
| 69 | return m_parent->GetTypeName(); |
| 70 | } |
| 71 | |
| 72 | uint32_t |
| 73 | ValueObjectDynamicValue::CalculateNumChildren() |
| 74 | { |
Enrico Granata | c3e320a | 2011-08-02 17:27:39 +0000 | [diff] [blame] | 75 | const bool success = UpdateValueIfNeeded(false); |
Greg Clayton | 5ad6394 | 2011-05-31 20:18:39 +0000 | [diff] [blame] | 76 | if (success && m_type_sp) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 77 | return ClangASTContext::GetNumChildren (GetClangAST (), GetClangType(), true); |
| 78 | else |
| 79 | return m_parent->GetNumChildren(); |
| 80 | } |
| 81 | |
| 82 | clang::ASTContext * |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 83 | ValueObjectDynamicValue::GetClangASTImpl () |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 84 | { |
Enrico Granata | 0a3958e | 2011-07-02 00:25:22 +0000 | [diff] [blame] | 85 | const bool success = UpdateValueIfNeeded(false); |
Greg Clayton | 5ad6394 | 2011-05-31 20:18:39 +0000 | [diff] [blame] | 86 | if (success && m_type_sp) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 87 | return m_type_sp->GetClangAST(); |
| 88 | else |
| 89 | return m_parent->GetClangAST (); |
| 90 | } |
| 91 | |
| 92 | size_t |
| 93 | ValueObjectDynamicValue::GetByteSize() |
| 94 | { |
Enrico Granata | c3e320a | 2011-08-02 17:27:39 +0000 | [diff] [blame] | 95 | const bool success = UpdateValueIfNeeded(false); |
Greg Clayton | 5ad6394 | 2011-05-31 20:18:39 +0000 | [diff] [blame] | 96 | if (success && m_type_sp) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 97 | return m_value.GetValueByteSize(GetClangAST(), NULL); |
| 98 | else |
| 99 | return m_parent->GetByteSize(); |
| 100 | } |
| 101 | |
| 102 | lldb::ValueType |
| 103 | ValueObjectDynamicValue::GetValueType() const |
| 104 | { |
| 105 | return m_parent->GetValueType(); |
| 106 | } |
| 107 | |
| 108 | bool |
| 109 | ValueObjectDynamicValue::UpdateValue () |
| 110 | { |
| 111 | SetValueIsValid (false); |
| 112 | m_error.Clear(); |
| 113 | |
Enrico Granata | c3e320a | 2011-08-02 17:27:39 +0000 | [diff] [blame] | 114 | if (!m_parent->UpdateValueIfNeeded(false)) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 115 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame] | 116 | // The dynamic value failed to get an error, pass the error along |
| 117 | if (m_error.Success() && m_parent->GetError().Fail()) |
| 118 | m_error = m_parent->GetError(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 122 | // Setting our type_sp to NULL will route everything back through our |
| 123 | // parent which is equivalent to not using dynamic values. |
| 124 | if (m_use_dynamic == lldb::eNoDynamicValues) |
| 125 | { |
| 126 | m_type_sp.reset(); |
| 127 | return true; |
| 128 | } |
| 129 | |
Greg Clayton | cc4d014 | 2012-02-17 07:49:44 +0000 | [diff] [blame] | 130 | ExecutionContext exe_ctx (GetExecutionContextRef()); |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 131 | Target *target = exe_ctx.GetTargetPtr(); |
| 132 | if (target) |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 133 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 134 | m_data.SetByteOrder(target->GetArchitecture().GetByteOrder()); |
| 135 | m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // First make sure our Type and/or Address haven't changed: |
Greg Clayton | cc4d014 | 2012-02-17 07:49:44 +0000 | [diff] [blame] | 139 | Process *process = exe_ctx.GetProcessPtr(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 140 | if (!process) |
| 141 | return false; |
| 142 | |
Jim Ingham | 61be090 | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 143 | TypeAndOrName class_type_or_name; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 144 | Address dynamic_address; |
| 145 | bool found_dynamic_type = false; |
| 146 | |
| 147 | lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage(); |
| 148 | if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC) |
| 149 | { |
| 150 | LanguageRuntime *runtime = process->GetLanguageRuntime (known_type); |
| 151 | if (runtime) |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 152 | 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] | 153 | } |
| 154 | else |
| 155 | { |
| 156 | LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus); |
| 157 | if (cpp_runtime) |
Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 158 | 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] | 159 | |
| 160 | if (!found_dynamic_type) |
| 161 | { |
| 162 | LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC); |
| 163 | if (objc_runtime) |
Enrico Granata | 9910bc8 | 2011-08-03 02:18:51 +0000 | [diff] [blame] | 164 | found_dynamic_type = objc_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] | 165 | } |
| 166 | } |
| 167 | |
Jim Ingham | 61be090 | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 168 | lldb::TypeSP dynamic_type_sp = class_type_or_name.GetTypeSP(); |
| 169 | |
| 170 | // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really |
| 171 | // don't... |
| 172 | |
| 173 | m_update_point.SetUpdated(); |
| 174 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 175 | // If we don't have a dynamic type, then make ourselves just a echo of our parent. |
| 176 | // Or we could return false, and make ourselves an echo of our parent? |
| 177 | if (!found_dynamic_type) |
| 178 | { |
Enrico Granata | bd83b87 | 2012-11-27 23:28:32 +0000 | [diff] [blame^] | 179 | ClearDynamicTypeInformation(); |
| 180 | m_type_sp.reset(); |
| 181 | SetValueDidChange(true); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 182 | m_value = m_parent->GetValue(); |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 183 | m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get()); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 184 | return m_error.Success(); |
| 185 | } |
| 186 | |
| 187 | Value old_value(m_value); |
| 188 | |
Enrico Granata | d228483 | 2012-10-17 22:23:56 +0000 | [diff] [blame] | 189 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); |
| 190 | |
Enrico Granata | e3e9151 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 191 | bool has_changed_type = false; |
| 192 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 193 | if (!m_type_sp) |
| 194 | { |
| 195 | m_type_sp = dynamic_type_sp; |
Enrico Granata | e3e9151 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 196 | has_changed_type = true; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 197 | } |
| 198 | else if (dynamic_type_sp != m_type_sp) |
| 199 | { |
| 200 | // We are another type, we need to tear down our children... |
| 201 | m_type_sp = dynamic_type_sp; |
| 202 | SetValueDidChange (true); |
Enrico Granata | e3e9151 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 203 | has_changed_type = true; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Enrico Granata | e3e9151 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 206 | if (has_changed_type) |
| 207 | ClearDynamicTypeInformation (); |
| 208 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 209 | if (!m_address.IsValid() || m_address != dynamic_address) |
| 210 | { |
| 211 | if (m_address.IsValid()) |
| 212 | SetValueDidChange (true); |
| 213 | |
| 214 | // We've moved, so we should be fine... |
| 215 | m_address = dynamic_address; |
Greg Clayton | cc4d014 | 2012-02-17 07:49:44 +0000 | [diff] [blame] | 216 | lldb::TargetSP target_sp (GetTargetSP()); |
| 217 | lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get()); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 218 | m_value.GetScalar() = load_address; |
| 219 | } |
| 220 | |
| 221 | // The type will always be the type of the dynamic object. If our parent's type was a pointer, |
| 222 | // then our type should be a pointer to the type of the dynamic object. If a reference, then the original type |
| 223 | // should be okay... |
| 224 | lldb::clang_type_t orig_type = m_type_sp->GetClangForwardType(); |
| 225 | lldb::clang_type_t corrected_type = orig_type; |
| 226 | if (m_parent->IsPointerType()) |
| 227 | corrected_type = ClangASTContext::CreatePointerType (m_type_sp->GetClangAST(), orig_type); |
| 228 | else if (m_parent->IsPointerOrReferenceType()) |
| 229 | corrected_type = ClangASTContext::CreateLValueReferenceType (m_type_sp->GetClangAST(), orig_type); |
| 230 | |
| 231 | m_value.SetContext (Value::eContextTypeClangType, corrected_type); |
| 232 | |
| 233 | // Our address is the location of the dynamic type stored in memory. It isn't a load address, |
| 234 | // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us... |
| 235 | m_value.SetValueType(Value::eValueTypeScalar); |
| 236 | |
Enrico Granata | e3e9151 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 237 | if (has_changed_type && log) |
| 238 | log->Printf("[%s %p] has a new dynamic type %s", |
| 239 | GetName().GetCString(), |
| 240 | this, |
| 241 | GetTypeName().GetCString()); |
| 242 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 243 | if (m_address.IsValid() && m_type_sp) |
| 244 | { |
| 245 | // The variable value is in the Scalar value inside the m_value. |
| 246 | // We can point our m_data right to it. |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 247 | m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get()); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 248 | if (m_error.Success()) |
| 249 | { |
| 250 | if (ClangASTContext::IsAggregateType (GetClangType())) |
| 251 | { |
| 252 | // this value object represents an aggregate type whose |
| 253 | // children have values, but this object does not. So we |
| 254 | // say we are changed if our location has changed. |
| 255 | SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar()); |
| 256 | } |
| 257 | |
| 258 | SetValueIsValid (true); |
| 259 | return true; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // We get here if we've failed above... |
| 264 | SetValueIsValid (false); |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | |
| 270 | bool |
| 271 | ValueObjectDynamicValue::IsInScope () |
| 272 | { |
| 273 | return m_parent->IsInScope(); |
| 274 | } |
| 275 | |
Enrico Granata | 07a4ac2 | 2012-05-08 21:25:06 +0000 | [diff] [blame] | 276 | bool |
| 277 | ValueObjectDynamicValue::SetValueFromCString (const char *value_str, Error& error) |
| 278 | { |
| 279 | if (!UpdateValueIfNeeded(false)) |
| 280 | { |
| 281 | error.SetErrorString("unable to read value"); |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | uint64_t my_value = GetValueAsUnsigned(UINT64_MAX); |
| 286 | uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX); |
| 287 | |
| 288 | if (my_value == UINT64_MAX || parent_value == UINT64_MAX) |
| 289 | { |
| 290 | error.SetErrorString("unable to read value"); |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | // if we are at an offset from our parent, in order to set ourselves correctly we would need |
| 295 | // to change the new value so that it refers to the correct dynamic type. we choose not to deal |
| 296 | // with that - if anything more than a value overwrite is required, you should be using the |
| 297 | // expression parser instead of the value editing facility |
| 298 | if (my_value != parent_value) |
| 299 | { |
| 300 | // but NULL'ing out a value should always be allowed |
| 301 | if (strcmp(value_str,"0")) |
| 302 | { |
| 303 | error.SetErrorString("unable to modify dynamic value, use 'expression' command"); |
| 304 | return false; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | bool ret_val = m_parent->SetValueFromCString(value_str,error); |
| 309 | SetNeedsUpdate(); |
| 310 | return ret_val; |
| 311 | } |