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/Log.h" 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 23 #include "lldb/Symbol/ClangASTType.h" 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 36 using namespace lldb_private; 37 38 ValueObjectDynamicValue::ValueObjectDynamicValue (ValueObject &parent, lldb::DynamicValueType use_dynamic) : 39 ValueObject(parent), 40 m_address (), 41 m_dynamic_type_info(), 42 m_use_dynamic (use_dynamic) 43 { 44 SetName (parent.GetName()); 45 } 46 47 ValueObjectDynamicValue::~ValueObjectDynamicValue() 48 { 49 m_owning_valobj_sp.reset(); 50 } 51 52 ClangASTType 53 ValueObjectDynamicValue::GetClangTypeImpl () 54 { 55 const bool success = UpdateValueIfNeeded(false); 56 if (success) 57 { 58 if (m_dynamic_type_info.HasType()) 59 return m_value.GetClangType(); 60 else 61 return m_parent->GetClangType(); 62 } 63 return m_parent->GetClangType(); 64 } 65 66 ConstString 67 ValueObjectDynamicValue::GetTypeName() 68 { 69 const bool success = UpdateValueIfNeeded(false); 70 if (success) 71 { 72 if (m_dynamic_type_info.HasType()) 73 return GetClangType().GetConstTypeName(); 74 if (m_dynamic_type_info.HasName()) 75 return m_dynamic_type_info.GetName(); 76 } 77 return m_parent->GetTypeName(); 78 } 79 80 TypeImpl 81 ValueObjectDynamicValue::GetTypeImpl () 82 { 83 const bool success = UpdateValueIfNeeded(false); 84 if (success && m_type_impl.IsValid()) 85 { 86 return m_type_impl; 87 } 88 return m_parent->GetTypeImpl(); 89 } 90 91 ConstString 92 ValueObjectDynamicValue::GetQualifiedTypeName() 93 { 94 const bool success = UpdateValueIfNeeded(false); 95 if (success) 96 { 97 if (m_dynamic_type_info.HasType()) 98 return GetClangType().GetConstQualifiedTypeName (); 99 if (m_dynamic_type_info.HasName()) 100 return m_dynamic_type_info.GetName(); 101 } 102 return m_parent->GetTypeName(); 103 } 104 105 size_t 106 ValueObjectDynamicValue::CalculateNumChildren() 107 { 108 const bool success = UpdateValueIfNeeded(false); 109 if (success && m_dynamic_type_info.HasType()) 110 return GetClangType().GetNumChildren (true); 111 else 112 return m_parent->GetNumChildren(); 113 } 114 115 uint64_t 116 ValueObjectDynamicValue::GetByteSize() 117 { 118 const bool success = UpdateValueIfNeeded(false); 119 if (success && m_dynamic_type_info.HasType()) 120 return m_value.GetValueByteSize(NULL); 121 else 122 return m_parent->GetByteSize(); 123 } 124 125 lldb::ValueType 126 ValueObjectDynamicValue::GetValueType() const 127 { 128 return m_parent->GetValueType(); 129 } 130 131 132 static TypeAndOrName 133 FixupTypeAndOrName (const TypeAndOrName& type_andor_name, 134 ValueObject& parent) 135 { 136 TypeAndOrName ret(type_andor_name); 137 if (type_andor_name.HasType()) 138 { 139 // The type will always be the type of the dynamic object. If our parent's type was a pointer, 140 // then our type should be a pointer to the type of the dynamic object. If a reference, then the original type 141 // should be okay... 142 ClangASTType orig_type = type_andor_name.GetClangASTType(); 143 ClangASTType corrected_type = orig_type; 144 if (parent.IsPointerType()) 145 corrected_type = orig_type.GetPointerType (); 146 else if (parent.IsPointerOrReferenceType()) 147 corrected_type = orig_type.GetLValueReferenceType (); 148 ret.SetClangASTType(corrected_type); 149 } 150 else /*if (m_dynamic_type_info.HasName())*/ 151 { 152 // If we are here we need to adjust our dynamic type name to include the correct & or * symbol 153 std::string corrected_name (type_andor_name.GetName().GetCString()); 154 if (parent.IsPointerType()) 155 corrected_name.append(" *"); 156 else if (parent.IsPointerOrReferenceType()) 157 corrected_name.append(" &"); 158 ret.SetName(corrected_name.c_str()); 159 } 160 return ret; 161 } 162 163 bool 164 ValueObjectDynamicValue::UpdateValue () 165 { 166 SetValueIsValid (false); 167 m_error.Clear(); 168 169 if (!m_parent->UpdateValueIfNeeded(false)) 170 { 171 // The dynamic value failed to get an error, pass the error along 172 if (m_error.Success() && m_parent->GetError().Fail()) 173 m_error = m_parent->GetError(); 174 return false; 175 } 176 177 // Setting our type_sp to NULL will route everything back through our 178 // parent which is equivalent to not using dynamic values. 179 if (m_use_dynamic == lldb::eNoDynamicValues) 180 { 181 m_dynamic_type_info.Clear(); 182 return true; 183 } 184 185 ExecutionContext exe_ctx (GetExecutionContextRef()); 186 Target *target = exe_ctx.GetTargetPtr(); 187 if (target) 188 { 189 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder()); 190 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); 191 } 192 193 // First make sure our Type and/or Address haven't changed: 194 Process *process = exe_ctx.GetProcessPtr(); 195 if (!process) 196 return false; 197 198 TypeAndOrName class_type_or_name; 199 Address dynamic_address; 200 bool found_dynamic_type = false; 201 202 lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage(); 203 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC) 204 { 205 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type); 206 if (runtime) 207 found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address); 208 } 209 else 210 { 211 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus); 212 if (cpp_runtime) 213 found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address); 214 215 if (!found_dynamic_type) 216 { 217 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC); 218 if (objc_runtime) 219 found_dynamic_type = objc_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address); 220 } 221 } 222 223 // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really 224 // don't... 225 226 m_update_point.SetUpdated(); 227 228 if (found_dynamic_type) 229 { 230 if (class_type_or_name.HasType()) 231 { 232 // TypeSP are always generated from debug info 233 if (!class_type_or_name.HasTypeSP() && class_type_or_name.GetClangASTType().IsRuntimeGeneratedType()) 234 { 235 m_type_impl = TypeImpl(m_parent->GetClangType(),FixupTypeAndOrName(class_type_or_name, *m_parent).GetClangASTType()); 236 class_type_or_name.SetClangASTType(ClangASTType()); 237 } 238 else 239 { 240 m_type_impl = TypeImpl(FixupTypeAndOrName(class_type_or_name, *m_parent).GetClangASTType()); 241 } 242 } 243 else 244 { 245 m_type_impl.Clear(); 246 } 247 } 248 else 249 { 250 m_type_impl.Clear(); 251 } 252 253 // If we don't have a dynamic type, then make ourselves just a echo of our parent. 254 // Or we could return false, and make ourselves an echo of our parent? 255 if (!found_dynamic_type) 256 { 257 if (m_dynamic_type_info) 258 SetValueDidChange(true); 259 ClearDynamicTypeInformation(); 260 m_dynamic_type_info.Clear(); 261 m_value = m_parent->GetValue(); 262 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get()); 263 return m_error.Success(); 264 } 265 266 Value old_value(m_value); 267 268 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 269 270 bool has_changed_type = false; 271 272 if (!m_dynamic_type_info) 273 { 274 m_dynamic_type_info = class_type_or_name; 275 has_changed_type = true; 276 } 277 else if (class_type_or_name != m_dynamic_type_info) 278 { 279 // We are another type, we need to tear down our children... 280 m_dynamic_type_info = class_type_or_name; 281 SetValueDidChange (true); 282 has_changed_type = true; 283 } 284 285 if (has_changed_type) 286 ClearDynamicTypeInformation (); 287 288 if (!m_address.IsValid() || m_address != dynamic_address) 289 { 290 if (m_address.IsValid()) 291 SetValueDidChange (true); 292 293 // We've moved, so we should be fine... 294 m_address = dynamic_address; 295 lldb::TargetSP target_sp (GetTargetSP()); 296 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get()); 297 m_value.GetScalar() = load_address; 298 } 299 300 m_dynamic_type_info = FixupTypeAndOrName(m_dynamic_type_info, *m_parent); 301 302 //m_value.SetContext (Value::eContextTypeClangType, corrected_type); 303 m_value.SetClangType (m_dynamic_type_info.GetClangASTType()); 304 305 // Our address is the location of the dynamic type stored in memory. It isn't a load address, 306 // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us... 307 m_value.SetValueType(Value::eValueTypeScalar); 308 309 if (has_changed_type && log) 310 log->Printf("[%s %p] has a new dynamic type %s", 311 GetName().GetCString(), 312 this, 313 GetTypeName().GetCString()); 314 315 if (m_address.IsValid() && m_dynamic_type_info) 316 { 317 // The variable value is in the Scalar value inside the m_value. 318 // We can point our m_data right to it. 319 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get()); 320 if (m_error.Success()) 321 { 322 if (GetClangType().IsAggregateType ()) 323 { 324 // this value object represents an aggregate type whose 325 // children have values, but this object does not. So we 326 // say we are changed if our location has changed. 327 SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar()); 328 } 329 330 SetValueIsValid (true); 331 return true; 332 } 333 } 334 335 // We get here if we've failed above... 336 SetValueIsValid (false); 337 return false; 338 } 339 340 341 342 bool 343 ValueObjectDynamicValue::IsInScope () 344 { 345 return m_parent->IsInScope(); 346 } 347 348 bool 349 ValueObjectDynamicValue::SetValueFromCString (const char *value_str, Error& error) 350 { 351 if (!UpdateValueIfNeeded(false)) 352 { 353 error.SetErrorString("unable to read value"); 354 return false; 355 } 356 357 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX); 358 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX); 359 360 if (my_value == UINT64_MAX || parent_value == UINT64_MAX) 361 { 362 error.SetErrorString("unable to read value"); 363 return false; 364 } 365 366 // if we are at an offset from our parent, in order to set ourselves correctly we would need 367 // to change the new value so that it refers to the correct dynamic type. we choose not to deal 368 // with that - if anything more than a value overwrite is required, you should be using the 369 // expression parser instead of the value editing facility 370 if (my_value != parent_value) 371 { 372 // but NULL'ing out a value should always be allowed 373 if (strcmp(value_str,"0")) 374 { 375 error.SetErrorString("unable to modify dynamic value, use 'expression' command"); 376 return false; 377 } 378 } 379 380 bool ret_val = m_parent->SetValueFromCString(value_str,error); 381 SetNeedsUpdate(); 382 return ret_val; 383 } 384 385 bool 386 ValueObjectDynamicValue::SetData (DataExtractor &data, Error &error) 387 { 388 if (!UpdateValueIfNeeded(false)) 389 { 390 error.SetErrorString("unable to read value"); 391 return false; 392 } 393 394 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX); 395 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX); 396 397 if (my_value == UINT64_MAX || parent_value == UINT64_MAX) 398 { 399 error.SetErrorString("unable to read value"); 400 return false; 401 } 402 403 // if we are at an offset from our parent, in order to set ourselves correctly we would need 404 // to change the new value so that it refers to the correct dynamic type. we choose not to deal 405 // with that - if anything more than a value overwrite is required, you should be using the 406 // expression parser instead of the value editing facility 407 if (my_value != parent_value) 408 { 409 // but NULL'ing out a value should always be allowed 410 lldb::offset_t offset = 0; 411 412 if (data.GetPointer(&offset) != 0) 413 { 414 error.SetErrorString("unable to modify dynamic value, use 'expression' command"); 415 return false; 416 } 417 } 418 419 bool ret_val = m_parent->SetData(data, error); 420 SetNeedsUpdate(); 421 return ret_val; 422 } 423