[Lldb-commits] [lldb] [lldb][Commands] Fix memory find for Swift expressions (PR #143860)
Michael Buch via lldb-commits
lldb-commits at lists.llvm.org
Thu Jun 12 09:04:55 PDT 2025
https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/143860
>From 953c71de0b3d21309a4ebbe71cab8618c16dd699 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 11 Jun 2025 12:26:51 +0100
Subject: [PATCH 1/2] [lldb][Commands] Fix memory find for Swift expressions
There were two issues previously preventing `memory find -e` expressions
to succeed when stopped in Swift frames:
1. We weren't getting the dynamic type of the result `ValueObject`.
For Swift this would fail when we tried to produce a scalar value
out of it because the static VO wasn't sufficient to get to the
integer value. Hence we add a call to `GetQualifiedRepresentationIfAvailable`
(which is what we do for expressions in `OptionArgParser::ToAddress` too).
2. We weren't passing an `ExecutionContextScope` to `GetByteSize`, which
Swift relied on to get the size of the result type.
My plan is to add an API test for this on the Apple `swiftlang/llvm-project` fork.
I considered an alternative where we use `OptionArgParser::ToAddress`
for `memory find -e` expressions, but it got a bit icky when trying to
figure out how many bytes we should copy out of the result into the
`DataBufferHeap` (currently we rely on the size of the result variable
type). This gets even trickier when we were to pass an expression that
was actually a hex digit or a number into `ToAddress`.
rdar://152113525
---
lldb/source/Commands/CommandObjectMemory.cpp | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/lldb/source/Commands/CommandObjectMemory.cpp b/lldb/source/Commands/CommandObjectMemory.cpp
index 85ae9f8f9e8cb..2bcbc4291f7dd 100644
--- a/lldb/source/Commands/CommandObjectMemory.cpp
+++ b/lldb/source/Commands/CommandObjectMemory.cpp
@@ -886,9 +886,10 @@ class CommandObjectMemoryRead : public CommandObjectParsed {
#include "CommandOptions.inc"
static llvm::Error CopyExpressionResult(ValueObject &result,
- DataBufferHeap &buffer) {
+ DataBufferHeap &buffer,
+ ExecutionContextScope *scope) {
uint64_t value = result.GetValueAsUnsigned(0);
- auto size_or_err = result.GetCompilerType().GetByteSize(nullptr);
+ auto size_or_err = result.GetCompilerType().GetByteSize(scope);
if (!size_or_err)
return size_or_err.takeError();
@@ -928,6 +929,11 @@ EvaluateExpression(llvm::StringRef expression, StackFrame &frame,
return llvm::createStringError(
"expression evaluation failed. pass a string instead");
+ result_sp = result_sp->GetQualifiedRepresentationIfAvailable(
+ result_sp->GetDynamicValueType(), /*synthValue=*/true);
+ if (!result_sp)
+ return llvm::createStringError("failed to unwrap expression result type");
+
return result_sp;
}
@@ -1082,7 +1088,8 @@ class CommandObjectMemoryFind : public CommandObjectParsed {
ValueObjectSP result_sp = *result_or_err;
- if (auto err = CopyExpressionResult(*result_sp, buffer)) {
+ if (auto err = CopyExpressionResult(*result_sp, buffer,
+ m_exe_ctx.GetFramePtr())) {
result.AppendError(llvm::toString(std::move(err)));
return;
}
>From 566b9576266d2f50e1cc69a6d99f4b737654b030 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Thu, 12 Jun 2025 17:04:48 +0100
Subject: [PATCH 2/2] fixup! improve error message
---
lldb/source/Commands/CommandObjectMemory.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lldb/source/Commands/CommandObjectMemory.cpp b/lldb/source/Commands/CommandObjectMemory.cpp
index 2bcbc4291f7dd..ccb06d8ff4d59 100644
--- a/lldb/source/Commands/CommandObjectMemory.cpp
+++ b/lldb/source/Commands/CommandObjectMemory.cpp
@@ -932,7 +932,7 @@ EvaluateExpression(llvm::StringRef expression, StackFrame &frame,
result_sp = result_sp->GetQualifiedRepresentationIfAvailable(
result_sp->GetDynamicValueType(), /*synthValue=*/true);
if (!result_sp)
- return llvm::createStringError("failed to unwrap expression result type");
+ return llvm::createStringError("failed to get dynamic result type");
return result_sp;
}
More information about the lldb-commits
mailing list