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