Skip to content

Commit bfc7cce

Browse files
committed
[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
1 parent 94a2cae commit bfc7cce

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lldb/source/Commands/CommandObjectMemory.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,10 @@ class CommandObjectMemoryRead : public CommandObjectParsed {
886886
#include "CommandOptions.inc"
887887

888888
static llvm::Error CopyExpressionResult(ValueObject &result,
889-
DataBufferHeap &buffer) {
889+
DataBufferHeap &buffer,
890+
ExecutionContextScope *scope) {
890891
uint64_t value = result.GetValueAsUnsigned(0);
891-
auto size_or_err = result.GetCompilerType().GetByteSize(nullptr);
892+
auto size_or_err = result.GetCompilerType().GetByteSize(scope);
892893
if (!size_or_err)
893894
return size_or_err.takeError();
894895

@@ -931,6 +932,11 @@ EvaluateExpression(llvm::StringRef expression, StackFrame &frame,
931932
return llvm::createStringError(
932933
"expression evaluation failed. pass a string instead");
933934

935+
result_sp = result_sp->GetQualifiedRepresentationIfAvailable(
936+
result_sp->GetDynamicValueType(), /*synthValue=*/true);
937+
if (!result_sp)
938+
return llvm::createStringError("failed to unwrap expression result type");
939+
934940
return result_sp;
935941
}
936942

@@ -1085,7 +1091,8 @@ class CommandObjectMemoryFind : public CommandObjectParsed {
10851091

10861092
ValueObjectSP result_sp = *result_or_err;
10871093

1088-
if (auto err = CopyExpressionResult(*result_sp, buffer)) {
1094+
if (auto err = CopyExpressionResult(*result_sp, buffer,
1095+
m_exe_ctx.GetFramePtr())) {
10891096
result.AppendError(llvm::toString(std::move(err)));
10901097
return;
10911098
}

lldb/test/API/functionalities/memory/find/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include <stdio.h>
22
#include <stdint.h>
33

4-
template<size_t T>
5-
struct [[gnu::packed]] Payload {
4+
template <size_t T> struct [[gnu::packed]] Payload {
65
uint8_t data[T];
76
};
87

0 commit comments

Comments
 (0)