Skip to content

[LLDB] Fix ValueObject::AddressOf() return value #137688

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 2 commits into from
May 5, 2025

Conversation

kuilpd
Copy link
Contributor

@kuilpd kuilpd commented Apr 28, 2025

ValueObject::AddressOf() used to return address as a value which has it's own address, allowing to do value.AddressOf().AddressOf().
This patch makes the return address a simple const value.

Copy link

github-actions bot commented Apr 28, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@kuilpd
Copy link
Contributor Author

kuilpd commented Apr 28, 2025

If I apply code formatter to the test, it breaks lines and no longer works. Should I rewrite it without using inline tests?

@jimingham
Copy link
Collaborator

What is the reason for making this change?

@kuilpd
Copy link
Contributor Author

kuilpd commented Apr 28, 2025

What is the reason for making this change?

The result value of AddressOf() shouldn't have its own address, it should be like an r-value const, and in the current implementation it just has the same address as the value it was taken from.

(lldb) script lldb.frame.FindVariable("y")
(int) y = 47
(lldb) script lldb.frame.FindVariable("y").AddressOf()
(int *) &y = 0x00007fffffffd7d0
(lldb) script lldb.frame.FindVariable("y").AddressOf().AddressOf()
(int **) &&y = 0x00007fffffffd7d0
(lldb) script lldb.frame.FindVariable("y").AddressOf().AddressOf().AddressOf()
No value

Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

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

This is at least consistent with what we do for ValueObjectConstResult::AddressOf

@labath
Copy link
Collaborator

labath commented Apr 30, 2025

If I apply code formatter to the test, it breaks lines and no longer works. Should I rewrite it without using inline tests?

You can stick a // clang-format off before the block. We don't tend to write inline tests these days, but we haven't really deprecated them either, and this looks like a good place to add the additional check.

@kuilpd kuilpd marked this pull request as ready for review May 1, 2025 12:12
@kuilpd kuilpd requested a review from JDevlieghere as a code owner May 1, 2025 12:12
@llvmbot llvmbot added the lldb label May 1, 2025
@llvmbot
Copy link
Member

llvmbot commented May 1, 2025

@llvm/pr-subscribers-lldb

Author: Ilia Kuklin (kuilpd)

Changes

ValueObject::AddressOf() used to return address as a value which has it's own address, allowing to do value.AddressOf().AddressOf().
This patch makes the return address a simple const value.


Full diff: https://github.com/llvm/llvm-project/pull/137688.diff

2 Files Affected:

  • (modified) lldb/source/ValueObject/ValueObject.cpp (+5-2)
  • (modified) lldb/test/API/python_api/sbvalue_const_addrof/main.cpp (+25-22)
diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp
index 8741cb7343166..1d8d112ccb0f4 100644
--- a/lldb/source/ValueObject/ValueObject.cpp
+++ b/lldb/source/ValueObject/ValueObject.cpp
@@ -2966,10 +2966,13 @@ ValueObjectSP ValueObject::AddressOf(Status &error) {
         std::string name(1, '&');
         name.append(m_name.AsCString(""));
         ExecutionContext exe_ctx(GetExecutionContextRef());
+
+        lldb::DataBufferSP buffer(
+            new lldb_private::DataBufferHeap(&addr, sizeof(lldb::addr_t)));
         m_addr_of_valobj_sp = ValueObjectConstResult::Create(
             exe_ctx.GetBestExecutionContextScope(),
-            compiler_type.GetPointerType(), ConstString(name.c_str()), addr,
-            eAddressTypeInvalid, m_data.GetAddressByteSize());
+            compiler_type.GetPointerType(), ConstString(name.c_str()), buffer,
+            endian::InlHostByteOrder(), exe_ctx.GetAddressByteSize());
       }
     } break;
     default:
diff --git a/lldb/test/API/python_api/sbvalue_const_addrof/main.cpp b/lldb/test/API/python_api/sbvalue_const_addrof/main.cpp
index 318a45bc21a85..ae6abc8613406 100644
--- a/lldb/test/API/python_api/sbvalue_const_addrof/main.cpp
+++ b/lldb/test/API/python_api/sbvalue_const_addrof/main.cpp
@@ -3,21 +3,21 @@
 
 struct RegisterContext
 {
-    uintptr_t r0;
-    uintptr_t r1;
-    uintptr_t r2;
-    uintptr_t r3;
-    uintptr_t r4;
-    uintptr_t pc;
-    uintptr_t fp;
-    uintptr_t sp;
+  uintptr_t r0;
+  uintptr_t r1;
+  uintptr_t r2;
+  uintptr_t r3;
+  uintptr_t r4;
+  uintptr_t pc;
+  uintptr_t fp;
+  uintptr_t sp;
 };
 
 struct ThreadInfo {
-    uint32_t tid;
-    const char *name;
-    RegisterContext regs;
-    ThreadInfo *next;
+  uint32_t tid;
+  const char *name;
+  RegisterContext regs;
+  ThreadInfo *next;
 };
 int main (int argc, char const *argv[], char const *envp[]);
 
@@ -27,14 +27,17 @@ ThreadInfo *g_thread_list_ptr = &g_thread1;
 
 int main (int argc, char const *argv[], char const *envp[])
 {
-    printf ("g_thread_list is %p\n", g_thread_list_ptr);
-    return 0; //% v = self.dbg.GetSelectedTarget().FindFirstGlobalVariable('g_thread_list_ptr')
-    //% v_gla = v.GetChildMemberWithName('regs').GetLoadAddress()
-    //% v_aof = v.GetChildMemberWithName('regs').AddressOf().GetValueAsUnsigned(lldb.LLDB_INVALID_ADDRESS)
-    //% expr = '(%s)0x%x' % (v.GetType().GetName(), v.GetValueAsUnsigned(0))
-    //% e = v.CreateValueFromExpression('e', expr)
-    //% e_gla = e.GetChildMemberWithName('regs').GetLoadAddress()
-    //% e_aof = e.GetChildMemberWithName('regs').AddressOf().GetValueAsUnsigned(lldb.LLDB_INVALID_ADDRESS)
-    //% self.assertTrue(v_gla == e_gla, "GetLoadAddress() differs")
-    //% self.assertTrue(v_aof == e_aof, "AddressOf() differs")
+  // clang-format off
+  printf ("g_thread_list is %p\n", g_thread_list_ptr);
+  return 0; //% v = self.dbg.GetSelectedTarget().FindFirstGlobalVariable('g_thread_list_ptr')
+  //% self.assertTrue(v.AddressOf().IsValid())
+  //% self.assertFalse(v.AddressOf().AddressOf().IsValid())
+  //% v_gla = v.GetChildMemberWithName('regs').GetLoadAddress()
+  //% v_aof = v.GetChildMemberWithName('regs').AddressOf().GetValueAsUnsigned(lldb.LLDB_INVALID_ADDRESS)
+  //% expr = '(%s)0x%x' % (v.GetType().GetName(), v.GetValueAsUnsigned(0))
+  //% e = v.CreateValueFromExpression('e', expr)
+  //% e_gla = e.GetChildMemberWithName('regs').GetLoadAddress()
+  //% e_aof = e.GetChildMemberWithName('regs').AddressOf().GetValueAsUnsigned(lldb.LLDB_INVALID_ADDRESS)
+  //% self.assertTrue(v_gla == e_gla, "GetLoadAddress() differs")
+  //% self.assertTrue(v_aof == e_aof, "AddressOf() differs")
 }

@kuilpd
Copy link
Contributor Author

kuilpd commented May 5, 2025

@labath
Is this okay to merge now?

Copy link
Collaborator

@labath labath left a comment

Choose a reason for hiding this comment

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

Yeah, yeah. If Jim says it's ok, that's good enough for me.

@kuilpd kuilpd merged commit 0eff410 into llvm:main May 5, 2025
13 checks passed
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
`ValueObject::AddressOf()` used to return address as a value which has
it's own address, allowing to do `value.AddressOf().AddressOf()`.
This patch makes the return address a simple const value.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
`ValueObject::AddressOf()` used to return address as a value which has
it's own address, allowing to do `value.AddressOf().AddressOf()`.
This patch makes the return address a simple const value.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
`ValueObject::AddressOf()` used to return address as a value which has
it's own address, allowing to do `value.AddressOf().AddressOf()`.
This patch makes the return address a simple const value.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
`ValueObject::AddressOf()` used to return address as a value which has
it's own address, allowing to do `value.AddressOf().AddressOf()`.
This patch makes the return address a simple const value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants