Skip to content

Factor out all code that depends on RemoteAST into a single file. (NFC) #7343

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
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
2 changes: 2 additions & 0 deletions lldb/source/Plugins/LanguageRuntime/Swift/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
add_lldb_library(lldbPluginSwiftLanguageRuntime PLUGIN
LLDBMemoryReader.cpp
ReflectionContext.cpp
SwiftLanguageRuntime.cpp
SwiftLanguageRuntimeDynamicTypeResolution.cpp
SwiftLanguageRuntimeNames.cpp
SwiftLanguageRuntimeRemoteAST.cpp
SwiftMetadataCache.cpp

LINK_LIBS
Expand Down
214 changes: 214 additions & 0 deletions lldb/source/Plugins/LanguageRuntime/Swift/ReflectionContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
//===-- ReflectionContext.cpp --------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "SwiftLanguageRuntimeImpl.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"

using namespace lldb;
using namespace lldb_private;

namespace {

/// An implementation of the generic ReflectionContextInterface that
/// is templatized on target pointer width and specialized to either
/// 32-bit or 64-bit pointers, with and without ObjC interoperability.
template <typename ReflectionContext>
class TargetReflectionContext
: public SwiftLanguageRuntimeImpl::ReflectionContextInterface {
ReflectionContext m_reflection_ctx;

public:
TargetReflectionContext(
std::shared_ptr<swift::reflection::MemoryReader> reader,
SwiftMetadataCache *swift_metadata_cache)
: m_reflection_ctx(reader, swift_metadata_cache) {}

llvm::Optional<uint32_t> AddImage(
llvm::function_ref<std::pair<swift::remote::RemoteRef<void>, uint64_t>(
swift::ReflectionSectionKind)>
find_section,
llvm::SmallVector<llvm::StringRef, 1> likely_module_names) override {
return m_reflection_ctx.addImage(find_section, likely_module_names);
}

llvm::Optional<uint32_t>
AddImage(swift::remote::RemoteAddress image_start,
llvm::SmallVector<llvm::StringRef, 1> likely_module_names) override {
return m_reflection_ctx.addImage(image_start, likely_module_names);
}

llvm::Optional<uint32_t> ReadELF(
swift::remote::RemoteAddress ImageStart,
llvm::Optional<llvm::sys::MemoryBlock> FileBuffer,
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) override {
return m_reflection_ctx.readELF(ImageStart, FileBuffer,
likely_module_names);
}

const swift::reflection::TypeInfo *
GetTypeInfo(const swift::reflection::TypeRef *type_ref,
swift::remote::TypeInfoProvider *provider) override {
if (!type_ref)
return nullptr;

Log *log(GetLog(LLDBLog::Types));
if (log && log->GetVerbose()) {
std::stringstream ss;
type_ref->dump(ss);
LLDB_LOGF(log,
"[TargetReflectionContext::getTypeInfo] Getting "
"type info for typeref:\n%s",
ss.str().c_str());
}

auto type_info = m_reflection_ctx.getTypeInfo(type_ref, provider);
if (log && !type_info) {
std::stringstream ss;
type_ref->dump(ss);
LLDB_LOGF(log,
"[TargetReflectionContext::getTypeInfo] Could not get "
"type info for typeref:\n%s",
ss.str().c_str());
}

if (type_info && log && log->GetVerbose()) {
std::stringstream ss;
type_info->dump(ss);
log->Printf("[TargetReflectionContext::getTypeInfo] Found "
"type info:\n%s",
ss.str().c_str());
}
return type_info;
}

swift::reflection::MemoryReader &GetReader() override {
return m_reflection_ctx.getReader();
}

bool ForEachSuperClassType(
swift::remote::TypeInfoProvider *tip, lldb::addr_t pointer,
std::function<bool(SwiftLanguageRuntimeImpl::SuperClassType)> fn)
override {
// Guard against faulty self-referential metadata.
unsigned limit = 256;
auto md_ptr = m_reflection_ctx.readMetadataFromInstance(pointer);
if (!md_ptr)
return false;

// Class object.
while (md_ptr && *md_ptr && --limit) {
// Reading metadata is potentially expensive since (in a remote
// debugging scenario it may even incur network traffic) so we
// just return closures that the caller can use to query details
// if they need them.'
auto metadata = *md_ptr;
if (fn({[=]() -> const swift::reflection::RecordTypeInfo * {
auto *ti = m_reflection_ctx.getMetadataTypeInfo(metadata, tip);
return llvm::dyn_cast_or_null<
swift::reflection::RecordTypeInfo>(ti);
},
[=]() -> const swift::reflection::TypeRef * {
return m_reflection_ctx.readTypeFromMetadata(metadata);
}}))
return true;

// Continue with the base class.
md_ptr = m_reflection_ctx.readSuperClassFromClassMetadata(metadata);
}
return false;
}

llvm::Optional<std::pair<const swift::reflection::TypeRef *,
swift::reflection::RemoteAddress>>
ProjectExistentialAndUnwrapClass(
swift::reflection::RemoteAddress existential_address,
const swift::reflection::TypeRef &existential_tr) override {
return m_reflection_ctx.projectExistentialAndUnwrapClass(
existential_address, existential_tr);
}

const swift::reflection::TypeRef *
ReadTypeFromMetadata(lldb::addr_t metadata_address,
bool skip_artificial_subclasses) override {
return m_reflection_ctx.readTypeFromMetadata(metadata_address,
skip_artificial_subclasses);
}

const swift::reflection::TypeRef *
ReadTypeFromInstance(lldb::addr_t instance_address,
bool skip_artificial_subclasses) override {
auto metadata_address =
m_reflection_ctx.readMetadataFromInstance(instance_address);
if (!metadata_address) {
LLDB_LOGF(GetLog(LLDBLog::Types),
"could not read heap metadata for object at %llu\n",
instance_address);
return nullptr;
}

return m_reflection_ctx.readTypeFromMetadata(*metadata_address,
skip_artificial_subclasses);
}

swift::reflection::TypeRefBuilder &GetBuilder() override {
return m_reflection_ctx.getBuilder();
}

llvm::Optional<bool> IsValueInlinedInExistentialContainer(
swift::remote::RemoteAddress existential_address) override {
return m_reflection_ctx.isValueInlinedInExistentialContainer(
existential_address);
}

swift::remote::RemoteAbsolutePointer
StripSignedPointer(swift::remote::RemoteAbsolutePointer pointer) override {
return m_reflection_ctx.stripSignedPointer(pointer);
}
};

} // namespace

namespace lldb_private {
std::unique_ptr<SwiftLanguageRuntimeImpl::ReflectionContextInterface>
SwiftLanguageRuntimeImpl::ReflectionContextInterface::CreateReflectionContext(
uint8_t ptr_size, std::shared_ptr<swift::remote::MemoryReader> reader,
bool ObjCInterop, SwiftMetadataCache *swift_metadata_cache) {
using ReflectionContext32ObjCInterop =
TargetReflectionContext<swift::reflection::ReflectionContext<
swift::External<swift::WithObjCInterop<swift::RuntimeTarget<4>>>>>;
using ReflectionContext32NoObjCInterop =
TargetReflectionContext<swift::reflection::ReflectionContext<
swift::External<swift::NoObjCInterop<swift::RuntimeTarget<4>>>>>;
using ReflectionContext64ObjCInterop =
TargetReflectionContext<swift::reflection::ReflectionContext<
swift::External<swift::WithObjCInterop<swift::RuntimeTarget<8>>>>>;
using ReflectionContext64NoObjCInterop =
TargetReflectionContext<swift::reflection::ReflectionContext<
swift::External<swift::NoObjCInterop<swift::RuntimeTarget<8>>>>>;
if (ptr_size == 4) {
if (ObjCInterop)
return std::make_unique<ReflectionContext32ObjCInterop>(
reader, swift_metadata_cache);
return std::make_unique<ReflectionContext32NoObjCInterop>(
reader, swift_metadata_cache);
}
if (ptr_size == 8) {
if (ObjCInterop)
return std::make_unique<ReflectionContext64ObjCInterop>(
reader, swift_metadata_cache);
return std::make_unique<ReflectionContext64NoObjCInterop>(
reader, swift_metadata_cache);
}
return {};
}
} // namespace lldb_private
39 changes: 15 additions & 24 deletions lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
#include "lldb/Utility/Timer.h"

#include "swift/AST/ASTMangler.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Module.h"
#include "swift/Demangling/Demangle.h"
#include "swift/RemoteInspection/ReflectionContext.h"
#include "swift/RemoteAST/RemoteAST.h"
Expand Down Expand Up @@ -519,23 +517,16 @@ void SwiftLanguageRuntimeImpl::SetupReflection() {
objc_interop ? "with Objective-C interopability" : "Swift only";

auto &triple = exe_module->GetArchitecture().GetTriple();
auto byte_size = m_process.GetAddressByteSize();
if (byte_size == 8) {
LLDB_LOGF(log, "Initializing a 64-bit reflection context (%s) for \"%s\"",
triple.str().c_str(), objc_interop_msg);
m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext64(
this->GetMemoryReader(), objc_interop, GetSwiftMetadataCache());
} else if (byte_size == 4) {
LLDB_LOGF(log,
"Initializing a 32-bit reflection context (%s) for \"%s\"",
triple.str().c_str(), objc_interop_msg);
m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext32(
this->GetMemoryReader(), objc_interop, GetSwiftMetadataCache());
} else {
LLDB_LOGF(log,
"Could not initialize reflection context for \"%s\"",
triple.str().c_str());
}
uint32_t ptr_size = m_process.GetAddressByteSize();
LLDB_LOG(log, "Initializing a {0}-bit reflection context ({1}) for \"{2}\"",
ptr_size * 8, triple.str(), objc_interop_msg);
if (ptr_size == 4 || ptr_size == 8)
m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext(
ptr_size, this->GetMemoryReader(), objc_interop,
GetSwiftMetadataCache());
if (!m_reflection_ctx)
LLDB_LOG(log, "Could not initialize reflection context for \"{0}\"",
triple.str());
// We set m_initialized_reflection_ctx to true here because
// AddModuleToReflectionContext can potentially call into SetupReflection
// again (which will early exit). This is safe to do since every other thread
Expand Down Expand Up @@ -680,7 +671,7 @@ bool SwiftLanguageRuntimeImpl::AddJitObjectFileToReflectionContext(
if (!obj_file_format)
return false;

auto reflection_info_id = m_reflection_ctx->addImage(
auto reflection_info_id = m_reflection_ctx->AddImage(
[&](swift::ReflectionSectionKind section_kind)
-> std::pair<swift::remote::RemoteRef<void>, uint64_t> {
auto section_name = obj_file_format->getSectionName(section_kind);
Expand Down Expand Up @@ -845,7 +836,7 @@ SwiftLanguageRuntimeImpl::AddObjectFileToReflectionContext(
return {};
};

return m_reflection_ctx->addImage(
return m_reflection_ctx->AddImage(
[&](swift::ReflectionSectionKind section_kind)
-> std::pair<swift::remote::RemoteRef<void>, uint64_t> {
auto pair = find_section_with_kind(segment, section_kind);
Expand Down Expand Up @@ -904,18 +895,18 @@ bool SwiftLanguageRuntimeImpl::AddModuleToReflectionContext(
auto size = obj_file->GetData(0, obj_file->GetByteSize(), extractor);
const uint8_t *file_data = extractor.GetDataStart();
llvm::sys::MemoryBlock file_buffer((void *)file_data, size);
info_id = m_reflection_ctx->readELF(
info_id = m_reflection_ctx->ReadELF(
swift::remote::RemoteAddress(load_ptr),
llvm::Optional<llvm::sys::MemoryBlock>(file_buffer),
likely_module_names);
} else if (read_from_file_cache &&
obj_file->GetPluginName().equals("mach-o")) {
info_id = AddObjectFileToReflectionContext(module_sp, likely_module_names);
if (!info_id)
info_id = m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr),
info_id = m_reflection_ctx->AddImage(swift::remote::RemoteAddress(load_ptr),
likely_module_names);
} else {
info_id = m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr),
info_id = m_reflection_ctx->AddImage(swift::remote::RemoteAddress(load_ptr),
likely_module_names);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "lldb/Core/PluginInterface.h"
#include "lldb/Target/LanguageRuntime.h"
#include "lldb/lldb-private.h"
#include "swift/Demangling/Demangle.h"

#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringSet.h"
Expand Down
Loading