Skip to content

[LLDB] Add more helper functions to ValueObject class. #87197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion lldb/include/lldb/Core/ValueObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,30 @@ class ValueObject {

virtual int64_t GetValueAsSigned(int64_t fail_value, bool *success = nullptr);

/// If the current ValueObject is of an appropriate type, convert the
/// value to an APSInt and return that. Otherwise return an error.
llvm::Expected<llvm::APSInt> GetValueAsAPSInt();

/// If the current ValueObject is of an appropriate type, convert the
/// value to an APFloat and return that. Otherwise return an error.
llvm::Expected<llvm::APFloat> GetValueAsAPFloat();

/// If the current ValueObject is of an appropriate type, convert the
/// value to a boolean and return that. Otherwise return an error.
llvm::Expected<bool> GetValueAsBool();

/// Update an existing integer ValueObject with a new integer value. This
/// is only intended to be used with 'temporary' ValueObjects, i.e. ones that
/// are not associated with program variables. It does not update program
/// memory, registers, stack, etc.
void SetValueFromInteger(const llvm::APInt &value, Status &error);

/// Update an existing integer ValueObject with an integer value created
/// frome 'new_val_sp'. This is only intended to be used with 'temporary'
/// ValueObjects, i.e. ones that are not associated with program variables.
/// It does not update program memory, registers, stack, etc.
void SetValueFromInteger(lldb::ValueObjectSP new_val_sp, Status &error);

virtual bool SetValueFromCString(const char *value_str, Status &error);

/// Return the module associated with this value object in case the value is
Expand Down Expand Up @@ -618,6 +642,32 @@ class ValueObject {
virtual lldb::ValueObjectSP CastPointerType(const char *name,
lldb::TypeSP &type_sp);

/// Return the target load address associated with this value object.
lldb::addr_t GetLoadAddress();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function just a copy of the contents of lldb::addr_t SBValue::GetLoadAddress()? If so, we should also fix that function to call this function. Might be nice to have this return std::optional<lldb::addr_t> for internal use as if the ValueObject is a variable that is in a register, this will need to return LLDB_INVALID_ADDRESS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is based on SBValue::GetLoadAddress; I'll update that one to call this one. As written, it already does return LLDB_INVALID_ADDRESS in certain cases...I'm not sure what you want me to do differently (probably better to discuss this at the implementation rather than the declaration).


/// Take a ValueObject whose type is an inherited class, and cast it to
/// 'type', which should be one of its base classes. 'base_type_indices'
/// contains the indices of direct base classes on the path from the
/// ValueObject's current type to 'type'
llvm::Expected<lldb::ValueObjectSP>
CastDerivedToBaseType(CompilerType type,
const llvm::ArrayRef<uint32_t> &base_type_indices);

/// Take a ValueObject whose type is a base class, and cast it to 'type',
/// which should be one of its derived classes. 'base_type_indices'
/// contains the indices of direct base classes on the path from the
/// ValueObject's current type to 'type'
llvm::Expected<lldb::ValueObjectSP> CastBaseToDerivedType(CompilerType type,
uint64_t offset);

// Take a ValueObject that contains a scalar, enum or pointer type, and
// cast it to a "basic" type (integer, float or boolean).
lldb::ValueObjectSP CastToBasicType(CompilerType type);

// Take a ValueObject that contain an integer, float or enum, and cast it
// to an enum.
lldb::ValueObjectSP CastToEnumType(CompilerType type);

/// If this object represents a C++ class with a vtable, return an object
/// that represents the virtual function table. If the object isn't a class
/// with a vtable, return a valid ValueObject with the error set correctly.
Expand Down Expand Up @@ -659,15 +709,41 @@ class ValueObject {
const ExecutionContext &exe_ctx,
const EvaluateExpressionOptions &options);

/// Given an address either create a value object containing the value at
/// that address, or create a value object containing the address itself
/// (pointer value), depending on whether the parameter 'do_deref' is true or
/// false.
static lldb::ValueObjectSP
CreateValueObjectFromAddress(llvm::StringRef name, uint64_t address,
const ExecutionContext &exe_ctx,
CompilerType type);
CompilerType type, bool do_deref = true);

static lldb::ValueObjectSP
CreateValueObjectFromData(llvm::StringRef name, const DataExtractor &data,
const ExecutionContext &exe_ctx, CompilerType type);

/// Create a value object containing the given APInt value.
static lldb::ValueObjectSP CreateValueObjectFromAPInt(lldb::TargetSP target,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto for adding a name parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const llvm::APInt &v,
CompilerType type,
llvm::StringRef name);

/// Create a value object containing the given APFloat value.
static lldb::ValueObjectSP
CreateValueObjectFromAPFloat(lldb::TargetSP target, const llvm::APFloat &v,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto for adding a name parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

CompilerType type, llvm::StringRef name);

/// Create a value object containing the given boolean value.
static lldb::ValueObjectSP CreateValueObjectFromBool(lldb::TargetSP target,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

bool value,
llvm::StringRef name);

/// Create a nullptr value object with the specified type (must be a
/// nullptr type).
static lldb::ValueObjectSP CreateValueObjectFromNullptr(lldb::TargetSP target,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

CompilerType type,
llvm::StringRef name);

lldb::ValueObjectSP Persist();

/// Returns true if this is a char* or a char[] if it is a char* and
Expand Down Expand Up @@ -719,6 +795,10 @@ class ValueObject {
ClearUserVisibleData(eClearUserVisibleDataItemsSummary);
}

void SetDerefValobj(ValueObject *deref) { m_deref_valobj = deref; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Synthetic child providers also have a way to provide the deref ValueObject dynamically. How do these two ways of doing that job work together?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the synthetic child providers way of getting the deref ValueObject to get the value I use for my call to SetDerefValobj (when I'm converting a ValueObject containing a SmartPointer into a ValueObject containing the pointer the SmartPointer was referencing).


ValueObject *GetDerefValobj() { return m_deref_valobj; }

void SetValueFormat(lldb::TypeFormatImplSP format) {
m_type_format_sp = std::move(format);
ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Expand Down
4 changes: 4 additions & 0 deletions lldb/include/lldb/Utility/Scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ class Scalar {

long double LongDouble(long double fail_value = 0.0) const;

llvm::APSInt GetAPSInt() const { return m_integer; }

llvm::APFloat GetAPFloat() const { return m_float; }

Status SetValueFromCString(const char *s, lldb::Encoding encoding,
size_t byte_size);

Expand Down
22 changes: 2 additions & 20 deletions lldb/source/API/SBValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1281,26 +1281,8 @@ lldb::addr_t SBValue::GetLoadAddress() {
lldb::addr_t value = LLDB_INVALID_ADDRESS;
ValueLocker locker;
lldb::ValueObjectSP value_sp(GetSP(locker));
if (value_sp) {
TargetSP target_sp(value_sp->GetTargetSP());
if (target_sp) {
const bool scalar_is_load_address = true;
AddressType addr_type;
value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type);
if (addr_type == eAddressTypeFile) {
ModuleSP module_sp(value_sp->GetModule());
if (!module_sp)
value = LLDB_INVALID_ADDRESS;
else {
Address addr;
module_sp->ResolveFileAddress(value, addr);
value = addr.GetLoadAddress(target_sp.get());
}
} else if (addr_type == eAddressTypeHost ||
addr_type == eAddressTypeInvalid)
value = LLDB_INVALID_ADDRESS;
}
}
if (value_sp)
return value_sp->GetLoadAddress();

return value;
}
Expand Down
Loading
Loading