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