Skip to content

[lldb] Add summary formatter for DefaultActorStorage #10388

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
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
65 changes: 51 additions & 14 deletions lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "lldb/lldb-enumerations.h"
#include "swift/ABI/Task.h"
#include "swift/AST/Types.h"
#include "swift/Concurrency/Actor.h"
#include "swift/Demangling/Demangle.h"
#include "swift/Demangling/ManglingMacros.h"
#include "llvm/ADT/STLExtras.h"
Expand Down Expand Up @@ -1321,6 +1322,25 @@ class TaskGroupSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
std::vector<ValueObjectSP> m_children;
};

/// Offset of ActiveActorStatus from _$defaultActor_.
///
/// DefaultActorImpl has the following (labeled) layout.
///
/// DefaultActorImpl:
/// 0: HeapObject
/// $defaultActor:
/// 16/0: isDistributedRemoteActor
/// 17/1: <alignment padding>
/// 32/16: StatusStorage
///
/// As shown, the $defaultActor field does not point to the start of the
/// DefaultActorImpl.
///
/// The StatusStorage is at offset of +32 from the start of DefaultActorImpl, or
/// at +16 relative to $defaultActor. The formatters are based on $defaultActor,
/// and as such use the relative offset.
static constexpr offset_t ActiveActorStatusOffset = 16;

class ActorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
public:
ActorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Expand Down Expand Up @@ -1458,20 +1478,6 @@ class ActorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
ProcessSP process_sp;
addr_t addr;

// `$defaultActor`'s offset within the actor object.
//
// The $defaultActor field does not point to the start of DefaultActorImpl,
// it has as an address that points past the HeapObject layout.
static constexpr offset_t DefaultActorFieldOffset = 16;
// ActiveActorStatus's offset within DefaultActorImpl.
//
// ActiveActorStatus is declared alignas(2*sizeof(void*)). The layout of
// DefaultActorImpl puts the status record after HeapObject (size 16), and
// its first field (bool size 1), an offset of +32 from the start of the
// actor. This offset is relative to DefaultActorImpl, but this code needs
// an offset relative to the $defaultActor field, and is adjusted as such.
static constexpr offset_t ActiveActorStatusOffset =
32 - DefaultActorFieldOffset;
// FirstJob's offset within ActiveActorStatus.
static constexpr offset_t FirstJobOffset = ActiveActorStatusOffset + 8;

Expand Down Expand Up @@ -1865,6 +1871,37 @@ bool lldb_private::formatters::swift::Task_SummaryProvider(
return true;
}

bool lldb_private::formatters::swift::Actor_SummaryProvider(
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
static constexpr offset_t FlagsOffset = ActiveActorStatusOffset;
auto addr = valobj.GetLoadAddress();
if (addr == LLDB_INVALID_ADDRESS)
return false;

auto flags_addr = addr + FlagsOffset;
Status status;
uint64_t flags = 0;
if (auto process_sp = valobj.GetProcessSP())
flags = process_sp->ReadUnsignedIntegerFromMemory(flags_addr, 4, 0, status);

if (status.Fail()) {
stream.PutCString("<could not read actor state>");
return true;
}

using namespace ::swift::concurrency::ActorFlagConstants;
uint8_t state = flags & ActorStateMask;
static_assert(Zombie_ReadyForDeallocation == 3);
if (state > Zombie_ReadyForDeallocation) {
stream << "<unknown actor state: " << Twine(state).str() << ">";
return true;
}

static const StringRef states[] = {"idle", "scheduled", "running", "zombie"};
stream.PutCString(states[state]);
return true;
}

namespace {

/// Enumerate the kinds of SIMD elements.
Expand Down
3 changes: 3 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,9 @@ bool TaskPriority_SummaryProvider(ValueObject &valobj, Stream &stream,
bool Task_SummaryProvider(ValueObject &valobj, Stream &stream,
const TypeSummaryOptions &options);

bool Actor_SummaryProvider(ValueObject &valobj, Stream &stream,
const TypeSummaryOptions &options);

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

Expand Down
4 changes: 4 additions & 0 deletions lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ static void LoadSwiftFormatters(lldb::TypeCategoryImplSP swift_category_sp) {
lldb_private::formatters::swift::Task_SummaryProvider,
"Swift UnsafeCurrentTask summary provider",
"Swift.UnsafeCurrentTask", task_summary_flags);
AddCXXSummary(swift_category_sp,
lldb_private::formatters::swift::Actor_SummaryProvider,
"Swift Actor summary provider", "Builtin.DefaultActorStorage",
task_summary_flags);
}

summary_flags.SetSkipPointers(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def test_actor_unprioritised_jobs(self):
self, "break here", lldb.SBFileSpec("main.swift")
)
frame = thread.GetSelectedFrame()
unprioritised_jobs = frame.var("a.$defaultActor.unprioritised_jobs")
defaultActor = frame.var("a.$defaultActor")
self.assertEqual(defaultActor.summary, "running")
unprioritised_jobs = defaultActor.GetChildMemberWithName("unprioritised_jobs")
# There are 4 child tasks (async let), the first one occupies the actor
# with a sleep, the next 3 go on to the queue.
self.assertEqual(unprioritised_jobs.num_children, 3)
Expand Down