Skip to content

[lldb] Add synthetic formatter for Swift.CheckedContinuation #10172

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

Closed
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
168 changes: 168 additions & 0 deletions lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,158 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
ValueObjectSP m_is_running_sp;
};

class UnsafeContinuationSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
public:
UnsafeContinuationSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
: SyntheticChildrenFrontEnd(*valobj_sp.get()) {
if (auto target_sp = m_backend.GetTargetSP()) {
if (auto ts_or_err =
target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeSwift)) {
if (auto *ts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(
ts_or_err->get()))
// TypeMangling for "Swift.UnsafeCurrentTask"
m_task_type = ts->GetTypeFromMangledTypename(ConstString("$sSctD"));
} else {
LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters | LLDBLog::Types),
ts_or_err.takeError(),
"could not get Swift type system for UnsafeContinuation "
"synthetic provider: {0}");
}
}
}

llvm::Expected<uint32_t> CalculateNumChildren() override {
if (!m_task_sp)
return m_backend.GetNumChildren();

return 1;
}

bool MightHaveChildren() override { return true; }

lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
if (!m_task_sp)
return m_backend.GetChildAtIndex(idx);

if (idx == 0)
return m_task_sp;

return {};
}

size_t GetIndexOfChildWithName(ConstString name) override {
if (!m_task_sp)
return m_backend.GetIndexOfChildWithName(name);

if (name == "task")
return 0;

return UINT32_MAX;
}

lldb::ChildCacheState Update() override {
if (auto context_sp = m_backend.GetChildMemberWithName("context"))
if (addr_t task_addr = context_sp->GetValueAsUnsigned(0)) {
m_task_sp = ValueObject::CreateValueObjectFromAddress(
"task", task_addr, m_backend.GetExecutionContextRef(), m_task_type,
false);
if (auto synthetic_sp = m_task_sp->GetSyntheticValue())
m_task_sp = synthetic_sp;
}
return ChildCacheState::eRefetch;
}

private:
CompilerType m_task_type;
ValueObjectSP m_task_sp;
};

class CheckedContinuationSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
public:
CheckedContinuationSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
: SyntheticChildrenFrontEnd(*valobj_sp.get()) {
bool is_64bit = false;
if (auto target_sp = m_backend.GetTargetSP())
is_64bit = target_sp->GetArchitecture().GetTriple().isArch64Bit();

std::optional<uint32_t> concurrency_version;
if (auto process_sp = m_backend.GetProcessSP())
concurrency_version =
SwiftLanguageRuntime::FindConcurrencyDebugVersion(*process_sp);

bool is_supported_target = is_64bit && concurrency_version.value_or(0) == 1;
if (!is_supported_target)
return;

if (auto target_sp = m_backend.GetTargetSP()) {
if (auto ts_or_err =
target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeSwift)) {
if (auto *ts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(
ts_or_err->get()))
// TypeMangling for "Swift.UnsafeCurrentTask"
m_task_type = ts->GetTypeFromMangledTypename(ConstString("$sSctD"));
} else {
LLDB_LOG_ERROR(
GetLog(LLDBLog::DataFormatters | LLDBLog::Types),
ts_or_err.takeError(),
"could not get Swift type system for CheckedContinuation "
"synthetic provider: {0}");
}
}
}

llvm::Expected<uint32_t> CalculateNumChildren() override {
if (!m_task_sp)
return m_backend.GetNumChildren();

return 1;
}

bool MightHaveChildren() override { return true; }

lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
if (!m_task_sp)
return m_backend.GetChildAtIndex(idx);

if (idx == 0)
return m_task_sp;

return {};
}

size_t GetIndexOfChildWithName(ConstString name) override {
if (!m_task_sp)
return m_backend.GetIndexOfChildWithName(name);

if (name == "task")
return 0;

return UINT32_MAX;
}

lldb::ChildCacheState Update() override {
if (!m_task_type)
return ChildCacheState::eReuse;

size_t canary_task_offset = 0x10;
Status status;
if (auto canary_sp = m_backend.GetChildMemberWithName("canary"))
if (addr_t canary_addr = canary_sp->GetValueAsUnsigned(0))
if (addr_t task_addr = m_backend.GetProcessSP()->ReadPointerFromMemory(
canary_addr + canary_task_offset, status))
m_task_sp = ValueObject::CreateValueObjectFromAddress(
"task", task_addr, m_backend.GetExecutionContextRef(),
m_task_type, false);
if (auto synthetic_sp = m_task_sp->GetSyntheticValue())
m_task_sp = synthetic_sp;
return ChildCacheState::eRefetch;
}

private:
CompilerType m_task_type;
ValueObjectSP m_task_sp;
};

class TaskGroupSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
public:
TaskGroupSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Expand Down Expand Up @@ -1202,6 +1354,22 @@ lldb_private::formatters::swift::TaskSyntheticFrontEndCreator(
return new TaskSyntheticFrontEnd(valobj_sp);
}

SyntheticChildrenFrontEnd *
lldb_private::formatters::swift::UnsafeContinuationSyntheticFrontEndCreator(
CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
if (!valobj_sp)
return nullptr;
return new UnsafeContinuationSyntheticFrontEnd(valobj_sp);
}

SyntheticChildrenFrontEnd *
lldb_private::formatters::swift::CheckedContinuationSyntheticFrontEndCreator(
CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
if (!valobj_sp)
return nullptr;
return new CheckedContinuationSyntheticFrontEnd(valobj_sp);
}

SyntheticChildrenFrontEnd *
lldb_private::formatters::swift::TaskGroupSyntheticFrontEndCreator(
CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
Expand Down
8 changes: 8 additions & 0 deletions lldb/source/Plugins/Language/Swift/SwiftFormatters.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ SyntheticChildrenFrontEnd *EnumSyntheticFrontEndCreator(CXXSyntheticChildren *,
SyntheticChildrenFrontEnd *TaskSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP);

SyntheticChildrenFrontEnd *
UnsafeContinuationSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP);

SyntheticChildrenFrontEnd *
CheckedContinuationSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP);

SyntheticChildrenFrontEnd *
TaskGroupSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP);
}
Expand Down
12 changes: 12 additions & 0 deletions lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@ static void LoadSwiftFormatters(lldb::TypeCategoryImplSP swift_category_sp) {
lldb_private::formatters::swift::TaskSyntheticFrontEndCreator,
"Swift.UnsafeCurrentTask synthetic children",
ConstString("Swift.UnsafeCurrentTask"), synth_flags);
AddCXXSynthetic(swift_category_sp,
lldb_private::formatters::swift::
UnsafeContinuationSyntheticFrontEndCreator,
"Swift.UnsafeContinuation synthetic children",
ConstString("^Swift\\.UnsafeContinuation<.+,.+>"),
synth_flags, true);
AddCXXSynthetic(swift_category_sp,
lldb_private::formatters::swift::
CheckedContinuationSyntheticFrontEndCreator,
"Swift.CheckedContinuation synthetic children",
ConstString("^Swift\\.CheckedContinuation<.+,.+>"),
synth_flags, true);
AddCXXSynthetic(
swift_category_sp,
lldb_private::formatters::swift::TaskGroupSyntheticFrontEndCreator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3671,7 +3671,8 @@ lldb::Encoding TypeSystemSwiftTypeRef::GetEncoding(opaque_compiler_type_t type,
if (node->getText() == swift::BUILTIN_TYPE_NAME_RAWPOINTER ||
node->getText() == swift::BUILTIN_TYPE_NAME_NATIVEOBJECT ||
node->getText() == swift::BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER ||
node->getText() == swift::BUILTIN_TYPE_NAME_BRIDGEOBJECT)
node->getText() == swift::BUILTIN_TYPE_NAME_BRIDGEOBJECT ||
node->getText() == swift::BUILTIN_TYPE_NAME_RAWUNSAFECONTINUATION)
return lldb::eEncodingUint;
if (node->getText().starts_with(swift::BUILTIN_TYPE_NAME_VEC)) {
count = 0;
Expand Down
3 changes: 3 additions & 0 deletions lldb/test/API/lang/swift/async/continuations/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SWIFT_SOURCES := main.swift
SWIFTFLAGS_EXTRAS := -parse-as-library
include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class TestCase(TestBase):

@swiftTest
def test_unsafe_continuation_printing(self):
"""Print an UnsafeContinuation and verify its children."""
self.build()
lldbutil.run_to_source_breakpoint(
self, "break unsafe continuation", lldb.SBFileSpec("main.swift")
)
self.expect(
"v cont",
substrs=[
"(UnsafeContinuation<Void, Never>) cont = {",
"task = {",
"isFuture = true",
],
)

@swiftTest
def test_checked_continuation_printing(self):
"""Print an CheckedContinuation and verify its children."""
self.build()
lldbutil.run_to_source_breakpoint(
self, "break checked continuation", lldb.SBFileSpec("main.swift")
)
self.expect(
"v cont",
substrs=[
"(CheckedContinuation<Int, Never>) cont = {",
"task = {",
"isFuture = true",
],
)
13 changes: 13 additions & 0 deletions lldb/test/API/lang/swift/async/continuations/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@main struct Main {
static func main() async {
await withUnsafeContinuation { (cont: UnsafeContinuation<Void, Never>) in
print("break unsafe continuation")
cont.resume()
}

_ = await withCheckedContinuation { (cont: CheckedContinuation<Int, Never>) in
print("break checked continuation")
cont.resume(returning: 15)
}
}
}