llvm-project/lldb/source/Core/ValueObjectVariable.cpp

403 lines
14 KiB
C++
Raw Normal View History

//===-- ValueObjectVariable.cpp ---------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/ValueObjectVariable.h"
#include "lldb/Core/Address.h" // for Address
#include "lldb/Core/AddressRange.h" // for AddressRange
#include "lldb/Core/ArchSpec.h" // for ArchSpec
#include "lldb/Core/Module.h"
#include "lldb/Core/RegisterValue.h"
#include "lldb/Core/Scalar.h" // for Scalar, operator!=
#include "lldb/Core/Value.h"
#include "lldb/Expression/DWARFExpression.h" // for DWARFExpression
#include "lldb/Symbol/Declaration.h" // for Declaration
<rdar://problem/11757916> Make breakpoint setting by file and line much more efficient by only looking for inlined breakpoint locations if we are setting a breakpoint in anything but a source implementation file. Implementing this complex for a many reasons. Turns out that parsing compile units lazily had some issues with respect to how we need to do things with DWARF in .o files. So the fixes in the checkin for this makes these changes: - Add a new setting called "target.inline-breakpoint-strategy" which can be set to "never", "always", or "headers". "never" will never try and set any inlined breakpoints (fastest). "always" always looks for inlined breakpoint locations (slowest, but most accurate). "headers", which is the default setting, will only look for inlined breakpoint locations if the breakpoint is set in what are consudered to be header files, which is realy defined as "not in an implementation source file". - modify the breakpoint setting by file and line to check the current "target.inline-breakpoint-strategy" setting and act accordingly - Modify compile units to be able to get their language and other info lazily. This allows us to create compile units from the debug map and not have to fill all of the details in, and then lazily discover this information as we go on debuggging. This is needed to avoid parsing all .o files when setting breakpoints in implementation only files (no inlines). Otherwise we would need to parse the .o file, the object file (mach-o in our case) and the symbol file (DWARF in the object file) just to see what the compile unit was. - modify the "SymbolFileDWARFDebugMap" to subclass lldb_private::Module so that the virtual "GetObjectFile()" and "GetSymbolVendor()" functions can be intercepted when the .o file contenst are later lazilly needed. Prior to this fix, when we first instantiated the "SymbolFileDWARFDebugMap" class, we would also make modules, object files and symbol files for every .o file in the debug map because we needed to fix up the sections in the .o files with information that is in the executable debug map. Now we lazily do this in the DebugMapModule::GetObjectFile() Cleaned up header includes a bit as well. llvm-svn: 162860
2012-08-30 05:13:06 +08:00
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/SymbolContextScope.h"
#include "lldb/Symbol/Type.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/DataExtractor.h" // for DataExtractor
#include "lldb/Utility/Status.h" // for Status
#include "lldb/lldb-private-enumerations.h" // for AddressType::eAddressTy...
#include "lldb/lldb-types.h" // for addr_t
#include "llvm/ADT/StringRef.h" // for StringRef
#include <assert.h> // for assert
#include <memory> // for shared_ptr
namespace lldb_private {
class ExecutionContextScope;
}
namespace lldb_private {
class StackFrame;
}
namespace lldb_private {
struct RegisterInfo;
}
using namespace lldb_private;
lldb::ValueObjectSP
ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
const lldb::VariableSP &var_sp) {
return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
}
ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
const lldb::VariableSP &var_sp)
: ValueObject(exe_scope), m_variable_sp(var_sp) {
// Do not attempt to construct one of these objects with no variable!
assert(m_variable_sp.get() != NULL);
m_name = var_sp->GetName();
}
ValueObjectVariable::~ValueObjectVariable() {}
CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
Type *var_type = m_variable_sp->GetType();
if (var_type)
return var_type->GetForwardCompilerType();
return CompilerType();
}
ConstString ValueObjectVariable::GetTypeName() {
Type *var_type = m_variable_sp->GetType();
if (var_type)
return var_type->GetName();
return ConstString();
}
ConstString ValueObjectVariable::GetDisplayTypeName() {
Type *var_type = m_variable_sp->GetType();
if (var_type)
return var_type->GetForwardCompilerType().GetDisplayTypeName();
return ConstString();
Introduce the concept of a "display name" for types Rationale: Pretty simply, the idea is that sometimes type names are way too long and contain way too many details for the average developer to care about. For instance, a plain ol' vector of int might be shown as std::__1::vector<int, std::__1::allocator<.... rather than the much simpler std::vector<int> form, which is what most developers would actually type in their code Proposed solution: Introduce a notion of "display name" and a corresponding API GetDisplayTypeName() to return such a crafted for visual representation type name Obviously, the display name and the fully qualified (or "true") name are not necessarily the same - that's the whole point LLDB could choose to pick the "display name" as its one true notion of a type name, and if somebody really needs the fully qualified version of it, let them deal with the problem Or, LLDB could rename what it currently calls the "type name" to be the "display name", and add new APIs for the fully qualified name, making the display name the default choice The choice that I am making here is that the type name will keep meaning the same, and people who want a type name suited for display will explicitly ask for one It is the less risky/disruptive choice - and it should eventually make it fairly obvious when someone is asking for the wrong type Caveats: - for now, GetDisplayTypeName() == GetTypeName(), there is no logic to produce customized display type names yet. - while the fully-qualified type name is still the main key to the kingdom of data formatters, if we start showing custom names to people, those should match formatters llvm-svn: 209072
2014-05-18 03:14:17 +08:00
}
ConstString ValueObjectVariable::GetQualifiedTypeName() {
Type *var_type = m_variable_sp->GetType();
if (var_type)
return var_type->GetQualifiedName();
return ConstString();
}
size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
CompilerType type(GetCompilerType());
if (!type.IsValid())
return 0;
const bool omit_empty_base_classes = true;
auto child_count = type.GetNumChildren(omit_empty_base_classes);
return child_count <= max ? child_count : max;
}
uint64_t ValueObjectVariable::GetByteSize() {
ExecutionContext exe_ctx(GetExecutionContextRef());
CompilerType type(GetCompilerType());
if (!type.IsValid())
return 0;
return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
}
lldb::ValueType ValueObjectVariable::GetValueType() const {
if (m_variable_sp)
return m_variable_sp->GetScope();
return lldb::eValueTypeInvalid;
}
bool ValueObjectVariable::UpdateValue() {
SetValueIsValid(false);
m_error.Clear();
Variable *variable = m_variable_sp.get();
DWARFExpression &expr = variable->LocationExpression();
2011-05-30 08:49:24 +08:00
if (variable->GetLocationIsConstantValueData()) {
// expr doesn't contain DWARF bytes, it contains the constant variable
// value bytes themselves...
if (expr.GetExpressionData(m_data))
m_value.SetContext(Value::eContextTypeVariable, variable);
else
m_error.SetErrorString("empty constant data");
// constant bytes can't be edited - sorry
m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
} else {
lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
ExecutionContext exe_ctx(GetExecutionContextRef());
Target *target = exe_ctx.GetTargetPtr();
if (target) {
m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
}
if (expr.IsLocationList()) {
SymbolContext sc;
variable->CalculateSymbolContext(&sc);
if (sc.function)
loclist_base_load_addr =
sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
target);
}
Value old_value(m_value);
if (expr.Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, nullptr,
nullptr, m_value, &m_error)) {
m_resolved_value = m_value;
m_value.SetContext(Value::eContextTypeVariable, variable);
CompilerType compiler_type = GetCompilerType();
if (compiler_type.IsValid())
m_value.SetCompilerType(compiler_type);
Value::ValueType value_type = m_value.GetValueType();
Process *process = exe_ctx.GetProcessPtr();
const bool process_is_alive = process && process->IsAlive();
const uint32_t type_info = compiler_type.GetTypeInfo();
const bool is_pointer_or_ref =
(type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
switch (value_type) {
case Value::eValueTypeFileAddress:
// If this type is a pointer, then its children will be considered load
// addresses
// if the pointer or reference is dereferenced, but only if the process
// is alive.
//
// There could be global variables like in the following code:
// struct LinkedListNode { Foo* foo; LinkedListNode* next; };
// Foo g_foo1;
// Foo g_foo2;
// LinkedListNode g_second_node = { &g_foo2, NULL };
// LinkedListNode g_first_node = { &g_foo1, &g_second_node };
//
// When we aren't running, we should be able to look at these variables
// using
// the "target variable" command. Children of the "g_first_node" always
// will
// be of the same address type as the parent. But children of the "next"
// member of
// LinkedListNode will become load addresses if we have a live process,
// or remain
// what a file address if it what a file address.
if (process_is_alive && is_pointer_or_ref)
SetAddressTypeOfChildren(eAddressTypeLoad);
else
SetAddressTypeOfChildren(eAddressTypeFile);
break;
case Value::eValueTypeHostAddress:
// Same as above for load addresses, except children of pointer or refs
// are always
// load addresses. Host addresses are used to store freeze dried
// variables. If this
// type is a struct, the entire struct contents will be copied into the
// heap of the
// LLDB process, but we do not currrently follow any pointers.
if (is_pointer_or_ref)
SetAddressTypeOfChildren(eAddressTypeLoad);
else
SetAddressTypeOfChildren(eAddressTypeHost);
break;
case Value::eValueTypeLoadAddress:
case Value::eValueTypeScalar:
case Value::eValueTypeVector:
SetAddressTypeOfChildren(eAddressTypeLoad);
break;
}
switch (value_type) {
case Value::eValueTypeVector:
// fall through
case Value::eValueTypeScalar:
// The variable value is in the Scalar value inside the m_value.
// We can point our m_data right to it.
m_error =
m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
break;
case Value::eValueTypeFileAddress:
case Value::eValueTypeLoadAddress:
case Value::eValueTypeHostAddress:
// The DWARF expression result was an address in the inferior
// process. If this variable is an aggregate type, we just need
// the address as the main value as all child variable objects
// will rely upon this location and add an offset and then read
// their own values as needed. If this variable is a simple
// type, we read all data for it into m_data.
// Make sure this type has a value before we try and read it
// If we have a file address, convert it to a load address if we can.
if (value_type == Value::eValueTypeFileAddress && process_is_alive) {
lldb::addr_t file_addr =
m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
if (file_addr != LLDB_INVALID_ADDRESS) {
SymbolContext var_sc;
variable->CalculateSymbolContext(&var_sc);
if (var_sc.module_sp) {
ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
if (objfile) {
Address so_addr(file_addr, objfile->GetSectionList());
lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
if (load_addr != LLDB_INVALID_ADDRESS) {
m_value.SetValueType(Value::eValueTypeLoadAddress);
m_value.GetScalar() = load_addr;
2011-05-30 08:49:24 +08:00
}
}
}
}
2011-05-30 08:49:24 +08:00
}
if (!CanProvideValue()) {
// this value object represents an aggregate type whose
// children have values, but this object does not. So we
// say we are changed if our location has changed.
SetValueDidChange(value_type != old_value.GetValueType() ||
m_value.GetScalar() != old_value.GetScalar());
} else {
// Copy the Value and set the context to use our Variable
// so it can extract read its value into m_data appropriately
Value value(m_value);
value.SetContext(Value::eContextTypeVariable, variable);
m_error =
value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
SetValueDidChange(value_type != old_value.GetValueType() ||
m_value.GetScalar() != old_value.GetScalar());
}
break;
}
SetValueIsValid(m_error.Success());
} else {
// could not find location, won't allow editing
m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
}
}
return m_error.Success();
}
bool ValueObjectVariable::IsInScope() {
const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
if (exe_ctx_ref.HasFrameRef()) {
ExecutionContext exe_ctx(exe_ctx_ref);
StackFrame *frame = exe_ctx.GetFramePtr();
if (frame) {
return m_variable_sp->IsInScope(frame);
} else {
// This ValueObject had a frame at one time, but now we
// can't locate it, so return false since we probably aren't
// in scope.
return false;
}
}
// We have a variable that wasn't tied to a frame, which
// means it is a global and is always in scope.
return true;
}
lldb::ModuleSP ValueObjectVariable::GetModule() {
if (m_variable_sp) {
SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
if (sc_scope) {
return sc_scope->CalculateSymbolContextModule();
}
}
return lldb::ModuleSP();
}
SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
if (m_variable_sp)
return m_variable_sp->GetSymbolContextScope();
return NULL;
Redesign of the interaction between Python and frozen objects: - introduced two new classes ValueObjectConstResultChild and ValueObjectConstResultImpl: the first one is a ValueObjectChild obtained from a ValueObjectConstResult, the second is a common implementation backend for VOCR and VOCRCh of method calls meant to read through pointers stored in frozen objects ; now such reads transparently move from host to target as required - as a consequence of the above, removed code that made target-memory copies of expression results in several places throughout LLDB, and also removed code that enabled to recognize an expression result VO as such - introduced a new GetPointeeData() method in ValueObject that lets you read a given amount of objects of type T from a VO representing a T* or T[], and doing dereferences transparently in private layer it returns a DataExtractor ; in public layer it returns an instance of a newly created lldb::SBData - as GetPointeeData() does the right thing for both frozen and non-frozen ValueObject's, reimplemented ReadPointedString() to use it en lieu of doing the raw read itself - introduced a new GetData() method in ValueObject that lets you get a copy of the data that backs the ValueObject (for pointers, this returns the address without any previous dereferencing steps ; for arrays it actually reads the whole chunk of memory) in public layer this returns an SBData, just like GetPointeeData() - introduced a new CreateValueFromData() method in SBValue that lets you create a new SBValue from a chunk of data wrapped in an SBData the limitation to remember for this kind of SBValue is that they have no address: extracting the address-of for these objects (with any of GetAddress(), GetLoadAddress() and AddressOf()) will return invalid values - added several tests to check that "p"-ing objects (STL classes, char* and char[]) will do the right thing Solved a bug where global pointers to global variables were not dereferenced correctly for display New target setting "max-string-summary-length" gives the maximum number of characters to show in a string when summarizing it, instead of the hardcoded 128 Solved a bug where the summary for char[] and char* would not be shown if the ValueObject's were dumped via the "p" command Removed m_pointers_point_to_load_addrs from ValueObject. Introduced a new m_address_type_of_children, which each ValueObject can set to tell the address type of any pointers and/or references it creates. In the current codebase, this is load address most of the time (the only notable exception being file addresses that generate file address children UNLESS we have a live process) Updated help text for summary-string Fixed an issue in STL formatters where std::stlcontainer::iterator would match the container's synthetic children providers Edited the syntax and help for some commands to have proper argument types llvm-svn: 139160
2011-09-07 03:20:51 +08:00
}
bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
if (m_variable_sp) {
decl = m_variable_sp->GetDeclaration();
return true;
}
return false;
}
const char *ValueObjectVariable::GetLocationAsCString() {
if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
return GetLocationAsCStringImpl(m_resolved_value, m_data);
else
return ValueObject::GetLocationAsCString();
}
bool ValueObjectVariable::SetValueFromCString(const char *value_str,
Status &error) {
if (!UpdateValueIfNeeded()) {
error.SetErrorString("unable to update value before writing");
return false;
}
if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
ExecutionContext exe_ctx(GetExecutionContextRef());
RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
RegisterValue reg_value;
if (!reg_info || !reg_ctx) {
error.SetErrorString("unable to retrieve register info");
return false;
}
error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
if (error.Fail())
return false;
if (reg_ctx->WriteRegister(reg_info, reg_value)) {
SetNeedsUpdate();
return true;
} else {
error.SetErrorString("unable to write back to register");
return false;
}
} else
return ValueObject::SetValueFromCString(value_str, error);
}
bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
if (!UpdateValueIfNeeded()) {
error.SetErrorString("unable to update value before writing");
return false;
}
if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
ExecutionContext exe_ctx(GetExecutionContextRef());
RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
RegisterValue reg_value;
if (!reg_info || !reg_ctx) {
error.SetErrorString("unable to retrieve register info");
return false;
}
error = reg_value.SetValueFromData(reg_info, data, 0, true);
if (error.Fail())
return false;
if (reg_ctx->WriteRegister(reg_info, reg_value)) {
SetNeedsUpdate();
return true;
} else {
error.SetErrorString("unable to write back to register");
return false;
}
} else
return ValueObject::SetData(data, error);
}