1 //===-- ValueObjectVariable.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/ValueObjectVariable.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/RegisterValue.h" 19 #include "lldb/Core/ValueObjectList.h" 20 #include "lldb/Core/Value.h" 21 22 #include "lldb/Symbol/Function.h" 23 #include "lldb/Symbol/ObjectFile.h" 24 #include "lldb/Symbol/SymbolContext.h" 25 #include "lldb/Symbol/SymbolContextScope.h" 26 #include "lldb/Symbol/Type.h" 27 #include "lldb/Symbol/Variable.h" 28 29 #include "lldb/Target/ExecutionContext.h" 30 #include "lldb/Target/Process.h" 31 #include "lldb/Target/RegisterContext.h" 32 #include "lldb/Target/Target.h" 33 #include "lldb/Target/Thread.h" 34 35 36 using namespace lldb_private; 37 38 lldb::ValueObjectSP 39 ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) 40 { 41 return (new ValueObjectVariable (exe_scope, var_sp))->GetSP(); 42 } 43 44 ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) : 45 ValueObject(exe_scope), 46 m_variable_sp(var_sp) 47 { 48 // Do not attempt to construct one of these objects with no variable! 49 assert (m_variable_sp.get() != NULL); 50 m_name = var_sp->GetName(); 51 } 52 53 ValueObjectVariable::~ValueObjectVariable() 54 { 55 } 56 57 ClangASTType 58 ValueObjectVariable::GetClangTypeImpl () 59 { 60 Type *var_type = m_variable_sp->GetType(); 61 if (var_type) 62 return var_type->GetClangForwardType(); 63 return ClangASTType(); 64 } 65 66 ConstString 67 ValueObjectVariable::GetTypeName() 68 { 69 Type * var_type = m_variable_sp->GetType(); 70 if (var_type) 71 return var_type->GetName(); 72 return ConstString(); 73 } 74 75 ConstString 76 ValueObjectVariable::GetDisplayTypeName() 77 { 78 Type * var_type = m_variable_sp->GetType(); 79 if (var_type) 80 return var_type->GetClangForwardType().GetDisplayTypeName(); 81 return ConstString(); 82 } 83 84 ConstString 85 ValueObjectVariable::GetQualifiedTypeName() 86 { 87 Type * var_type = m_variable_sp->GetType(); 88 if (var_type) 89 return var_type->GetQualifiedName(); 90 return ConstString(); 91 } 92 93 size_t 94 ValueObjectVariable::CalculateNumChildren() 95 { 96 ClangASTType type(GetClangType()); 97 98 if (!type.IsValid()) 99 return 0; 100 101 const bool omit_empty_base_classes = true; 102 return type.GetNumChildren(omit_empty_base_classes); 103 } 104 105 uint64_t 106 ValueObjectVariable::GetByteSize() 107 { 108 ExecutionContext exe_ctx(GetExecutionContextRef()); 109 110 ClangASTType type(GetClangType()); 111 112 if (!type.IsValid()) 113 return 0; 114 115 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); 116 } 117 118 lldb::ValueType 119 ValueObjectVariable::GetValueType() const 120 { 121 if (m_variable_sp) 122 return m_variable_sp->GetScope(); 123 return lldb::eValueTypeInvalid; 124 } 125 126 bool 127 ValueObjectVariable::UpdateValue () 128 { 129 SetValueIsValid (false); 130 m_error.Clear(); 131 132 Variable *variable = m_variable_sp.get(); 133 DWARFExpression &expr = variable->LocationExpression(); 134 135 if (variable->GetLocationIsConstantValueData()) 136 { 137 // expr doesn't contain DWARF bytes, it contains the constant variable 138 // value bytes themselves... 139 if (expr.GetExpressionData(m_data)) 140 m_value.SetContext(Value::eContextTypeVariable, variable); 141 else 142 m_error.SetErrorString ("empty constant data"); 143 // constant bytes can't be edited - sorry 144 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL); 145 } 146 else 147 { 148 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS; 149 ExecutionContext exe_ctx (GetExecutionContextRef()); 150 151 Target *target = exe_ctx.GetTargetPtr(); 152 if (target) 153 { 154 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder()); 155 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); 156 } 157 158 if (expr.IsLocationList()) 159 { 160 SymbolContext sc; 161 variable->CalculateSymbolContext (&sc); 162 if (sc.function) 163 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target); 164 } 165 Value old_value(m_value); 166 if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error)) 167 { 168 m_resolved_value = m_value; 169 m_value.SetContext(Value::eContextTypeVariable, variable); 170 171 ClangASTType clang_type = GetClangType(); 172 if (clang_type.IsValid()) 173 m_value.SetClangType(clang_type); 174 175 Value::ValueType value_type = m_value.GetValueType(); 176 177 Process *process = exe_ctx.GetProcessPtr(); 178 const bool process_is_alive = process && process->IsAlive(); 179 const uint32_t type_info = clang_type.GetTypeInfo(); 180 const bool is_pointer_or_ref = (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0; 181 182 switch (value_type) 183 { 184 case Value::eValueTypeFileAddress: 185 // If this type is a pointer, then its children will be considered load addresses 186 // if the pointer or reference is dereferenced, but only if the process is alive. 187 // 188 // There could be global variables like in the following code: 189 // struct LinkedListNode { Foo* foo; LinkedListNode* next; }; 190 // Foo g_foo1; 191 // Foo g_foo2; 192 // LinkedListNode g_second_node = { &g_foo2, NULL }; 193 // LinkedListNode g_first_node = { &g_foo1, &g_second_node }; 194 // 195 // When we aren't running, we should be able to look at these variables using 196 // the "target variable" command. Children of the "g_first_node" always will 197 // be of the same address type as the parent. But children of the "next" member of 198 // LinkedListNode will become load addresses if we have a live process, or remain 199 // what a file address if it what a file address. 200 if (process_is_alive && is_pointer_or_ref) 201 SetAddressTypeOfChildren(eAddressTypeLoad); 202 else 203 SetAddressTypeOfChildren(eAddressTypeFile); 204 break; 205 case Value::eValueTypeHostAddress: 206 // Same as above for load addresses, except children of pointer or refs are always 207 // load addresses. Host addresses are used to store freeze dried variables. If this 208 // type is a struct, the entire struct contents will be copied into the heap of the 209 // LLDB process, but we do not currrently follow any pointers. 210 if (is_pointer_or_ref) 211 SetAddressTypeOfChildren(eAddressTypeLoad); 212 else 213 SetAddressTypeOfChildren(eAddressTypeHost); 214 break; 215 case Value::eValueTypeLoadAddress: 216 case Value::eValueTypeScalar: 217 case Value::eValueTypeVector: 218 SetAddressTypeOfChildren(eAddressTypeLoad); 219 break; 220 } 221 222 switch (value_type) 223 { 224 case Value::eValueTypeVector: 225 // fall through 226 case Value::eValueTypeScalar: 227 // The variable value is in the Scalar value inside the m_value. 228 // We can point our m_data right to it. 229 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get()); 230 break; 231 232 case Value::eValueTypeFileAddress: 233 case Value::eValueTypeLoadAddress: 234 case Value::eValueTypeHostAddress: 235 // The DWARF expression result was an address in the inferior 236 // process. If this variable is an aggregate type, we just need 237 // the address as the main value as all child variable objects 238 // will rely upon this location and add an offset and then read 239 // their own values as needed. If this variable is a simple 240 // type, we read all data for it into m_data. 241 // Make sure this type has a value before we try and read it 242 243 // If we have a file address, convert it to a load address if we can. 244 if (value_type == Value::eValueTypeFileAddress && process_is_alive) 245 { 246 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 247 if (file_addr != LLDB_INVALID_ADDRESS) 248 { 249 SymbolContext var_sc; 250 variable->CalculateSymbolContext(&var_sc); 251 if (var_sc.module_sp) 252 { 253 ObjectFile *objfile = var_sc.module_sp->GetObjectFile(); 254 if (objfile) 255 { 256 Address so_addr(file_addr, objfile->GetSectionList()); 257 lldb::addr_t load_addr = so_addr.GetLoadAddress (target); 258 if (load_addr != LLDB_INVALID_ADDRESS) 259 { 260 m_value.SetValueType(Value::eValueTypeLoadAddress); 261 m_value.GetScalar() = load_addr; 262 } 263 } 264 } 265 } 266 } 267 268 if (!CanProvideValue()) 269 { 270 // this value object represents an aggregate type whose 271 // children have values, but this object does not. So we 272 // say we are changed if our location has changed. 273 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar()); 274 } 275 else 276 { 277 // Copy the Value and set the context to use our Variable 278 // so it can extract read its value into m_data appropriately 279 Value value(m_value); 280 value.SetContext(Value::eContextTypeVariable, variable); 281 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get()); 282 283 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar()); 284 } 285 break; 286 } 287 288 SetValueIsValid (m_error.Success()); 289 } 290 else 291 { 292 // could not find location, won't allow editing 293 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL); 294 } 295 } 296 return m_error.Success(); 297 } 298 299 300 301 bool 302 ValueObjectVariable::IsInScope () 303 { 304 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef(); 305 if (exe_ctx_ref.HasFrameRef()) 306 { 307 ExecutionContext exe_ctx (exe_ctx_ref); 308 StackFrame *frame = exe_ctx.GetFramePtr(); 309 if (frame) 310 { 311 return m_variable_sp->IsInScope (frame); 312 } 313 else 314 { 315 // This ValueObject had a frame at one time, but now we 316 // can't locate it, so return false since we probably aren't 317 // in scope. 318 return false; 319 } 320 } 321 // We have a variable that wasn't tied to a frame, which 322 // means it is a global and is always in scope. 323 return true; 324 325 } 326 327 lldb::ModuleSP 328 ValueObjectVariable::GetModule() 329 { 330 if (m_variable_sp) 331 { 332 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope(); 333 if (sc_scope) 334 { 335 return sc_scope->CalculateSymbolContextModule(); 336 } 337 } 338 return lldb::ModuleSP(); 339 } 340 341 SymbolContextScope * 342 ValueObjectVariable::GetSymbolContextScope() 343 { 344 if (m_variable_sp) 345 return m_variable_sp->GetSymbolContextScope(); 346 return NULL; 347 } 348 349 bool 350 ValueObjectVariable::GetDeclaration (Declaration &decl) 351 { 352 if (m_variable_sp) 353 { 354 decl = m_variable_sp->GetDeclaration(); 355 return true; 356 } 357 return false; 358 } 359 360 const char * 361 ValueObjectVariable::GetLocationAsCString () 362 { 363 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) 364 return GetLocationAsCStringImpl(m_resolved_value, 365 m_data); 366 else 367 return ValueObject::GetLocationAsCString(); 368 } 369 370 bool 371 ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error) 372 { 373 if (!UpdateValueIfNeeded()) 374 { 375 error.SetErrorString("unable to update value before writing"); 376 return false; 377 } 378 379 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) 380 { 381 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); 382 ExecutionContext exe_ctx(GetExecutionContextRef()); 383 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); 384 RegisterValue reg_value; 385 if (!reg_info || !reg_ctx) 386 { 387 error.SetErrorString("unable to retrieve register info"); 388 return false; 389 } 390 error = reg_value.SetValueFromCString(reg_info, value_str); 391 if (error.Fail()) 392 return false; 393 if (reg_ctx->WriteRegister (reg_info, reg_value)) 394 { 395 SetNeedsUpdate(); 396 return true; 397 } 398 else 399 { 400 error.SetErrorString("unable to write back to register"); 401 return false; 402 } 403 } 404 else 405 return ValueObject::SetValueFromCString(value_str, error); 406 } 407 408 bool 409 ValueObjectVariable::SetData (DataExtractor &data, Error &error) 410 { 411 if (!UpdateValueIfNeeded()) 412 { 413 error.SetErrorString("unable to update value before writing"); 414 return false; 415 } 416 417 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) 418 { 419 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); 420 ExecutionContext exe_ctx(GetExecutionContextRef()); 421 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); 422 RegisterValue reg_value; 423 if (!reg_info || !reg_ctx) 424 { 425 error.SetErrorString("unable to retrieve register info"); 426 return false; 427 } 428 error = reg_value.SetValueFromData(reg_info, data, 0, true); 429 if (error.Fail()) 430 return false; 431 if (reg_ctx->WriteRegister (reg_info, reg_value)) 432 { 433 SetNeedsUpdate(); 434 return true; 435 } 436 else 437 { 438 error.SetErrorString("unable to write back to register"); 439 return false; 440 } 441 } 442 else 443 return ValueObject::SetData(data, error); 444 } 445