[Lldb-commits] [lldb] [lldb-dap] Support inspecting memory (PR #104317)

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 15 18:36:53 PDT 2024


================
@@ -1085,6 +1084,17 @@ std::string VariableDescription::GetResult(llvm::StringRef context) {
   return description.trim().str();
 }
 
+std::optional<lldb::addr_t> GetMemoryReference(lldb::SBValue v) {
+  if (!v.GetType().IsPointerType() && !v.GetType().IsArrayType())
+    return std::nullopt;
+
----------------
clayborg wrote:

This means that we only return a valid memory reference if we have a pointer or an array. Do we really only want a memory reference for pointers and arrays? I would think we would want a valid memory reference for any variable that lives in memory. For example:
```
void foo() {
  int x = 12;
}
```
Will create a local variable, and with no optimizations with `-O0`, the variable `x` will live on the stack. We can return a value memory reference for this even though it isn't a pointer or an array. So my suggestion for this function was:
```
std::optional<lldb::addr_t> GetMemoryReference(lldb::SBValue v) {
  SBType v_type = v.GetType();
  if (v_type.IsPointerType() || v_type.IsReferenceType())
    v = v.Dereference();
  lldb::addr_t load_addr = deref.GetLoadAddress();
  if (load_addr != LLDB_INVALID_ADDRESS)
    return load_addr;
  return std::nullopt;
}
```
This will transparently look through both pointers and references to get the pointee type's location, and for all other types, including arrays, it will get the addresss of the data for the value.

https://github.com/llvm/llvm-project/pull/104317


More information about the lldb-commits mailing list