Skip to content

Commit 95fb583

Browse files
Merge pull request #7346 from adrian-prantl/cherry-pick-next
Cherry pick next
2 parents 57bbd83 + 7ef9098 commit 95fb583

8 files changed

+846
-782
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
add_lldb_library(lldbPluginSwiftLanguageRuntime PLUGIN
22
LLDBMemoryReader.cpp
3+
ReflectionContext.cpp
34
SwiftLanguageRuntime.cpp
45
SwiftLanguageRuntimeDynamicTypeResolution.cpp
56
SwiftLanguageRuntimeNames.cpp
7+
SwiftLanguageRuntimeRemoteAST.cpp
68
SwiftMetadataCache.cpp
79

810
LINK_LIBS

lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ bool LLDBMemoryReader::queryDataLayout(DataLayoutQueryType type, void *inBuffer,
2929
// disk. Setting the bit in the mask ensures it isn't accidentally cleared
3030
// by ptrauth stripping.
3131
mask_pattern |= LLDB_FILE_ADDRESS_BIT;
32-
memcpy(outBuffer, &mask_pattern, sizeof(uint64_t));
32+
memcpy(outBuffer, &mask_pattern, m_process.GetAddressByteSize());
3333
return true;
3434
}
3535
case DLQ_GetObjCReservedLowBits: {
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
//===-- ReflectionContext.cpp --------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "SwiftLanguageRuntimeImpl.h"
14+
#include "lldb/Utility/LLDBLog.h"
15+
#include "lldb/Utility/Log.h"
16+
17+
using namespace lldb;
18+
using namespace lldb_private;
19+
20+
namespace {
21+
22+
/// An implementation of the generic ReflectionContextInterface that
23+
/// is templatized on target pointer width and specialized to either
24+
/// 32-bit or 64-bit pointers, with and without ObjC interoperability.
25+
template <typename ReflectionContext>
26+
class TargetReflectionContext
27+
: public SwiftLanguageRuntimeImpl::ReflectionContextInterface {
28+
ReflectionContext m_reflection_ctx;
29+
30+
public:
31+
TargetReflectionContext(
32+
std::shared_ptr<swift::reflection::MemoryReader> reader,
33+
SwiftMetadataCache *swift_metadata_cache)
34+
: m_reflection_ctx(reader, swift_metadata_cache) {}
35+
36+
llvm::Optional<uint32_t> AddImage(
37+
llvm::function_ref<std::pair<swift::remote::RemoteRef<void>, uint64_t>(
38+
swift::ReflectionSectionKind)>
39+
find_section,
40+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names) override {
41+
return m_reflection_ctx.addImage(find_section, likely_module_names);
42+
}
43+
44+
llvm::Optional<uint32_t>
45+
AddImage(swift::remote::RemoteAddress image_start,
46+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names) override {
47+
return m_reflection_ctx.addImage(image_start, likely_module_names);
48+
}
49+
50+
llvm::Optional<uint32_t> ReadELF(
51+
swift::remote::RemoteAddress ImageStart,
52+
llvm::Optional<llvm::sys::MemoryBlock> FileBuffer,
53+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) override {
54+
return m_reflection_ctx.readELF(ImageStart, FileBuffer,
55+
likely_module_names);
56+
}
57+
58+
const swift::reflection::TypeInfo *
59+
GetTypeInfo(const swift::reflection::TypeRef *type_ref,
60+
swift::remote::TypeInfoProvider *provider) override {
61+
if (!type_ref)
62+
return nullptr;
63+
64+
Log *log(GetLog(LLDBLog::Types));
65+
if (log && log->GetVerbose()) {
66+
std::stringstream ss;
67+
type_ref->dump(ss);
68+
LLDB_LOGF(log,
69+
"[TargetReflectionContext::getTypeInfo] Getting "
70+
"type info for typeref:\n%s",
71+
ss.str().c_str());
72+
}
73+
74+
auto type_info = m_reflection_ctx.getTypeInfo(type_ref, provider);
75+
if (log && !type_info) {
76+
std::stringstream ss;
77+
type_ref->dump(ss);
78+
LLDB_LOGF(log,
79+
"[TargetReflectionContext::getTypeInfo] Could not get "
80+
"type info for typeref:\n%s",
81+
ss.str().c_str());
82+
}
83+
84+
if (type_info && log && log->GetVerbose()) {
85+
std::stringstream ss;
86+
type_info->dump(ss);
87+
log->Printf("[TargetReflectionContext::getTypeInfo] Found "
88+
"type info:\n%s",
89+
ss.str().c_str());
90+
}
91+
return type_info;
92+
}
93+
94+
swift::reflection::MemoryReader &GetReader() override {
95+
return m_reflection_ctx.getReader();
96+
}
97+
98+
bool ForEachSuperClassType(
99+
swift::remote::TypeInfoProvider *tip, lldb::addr_t pointer,
100+
std::function<bool(SwiftLanguageRuntimeImpl::SuperClassType)> fn)
101+
override {
102+
// Guard against faulty self-referential metadata.
103+
unsigned limit = 256;
104+
auto md_ptr = m_reflection_ctx.readMetadataFromInstance(pointer);
105+
if (!md_ptr)
106+
return false;
107+
108+
// Class object.
109+
while (md_ptr && *md_ptr && --limit) {
110+
// Reading metadata is potentially expensive since (in a remote
111+
// debugging scenario it may even incur network traffic) so we
112+
// just return closures that the caller can use to query details
113+
// if they need them.'
114+
auto metadata = *md_ptr;
115+
if (fn({[=]() -> const swift::reflection::RecordTypeInfo * {
116+
auto *ti = m_reflection_ctx.getMetadataTypeInfo(metadata, tip);
117+
return llvm::dyn_cast_or_null<
118+
swift::reflection::RecordTypeInfo>(ti);
119+
},
120+
[=]() -> const swift::reflection::TypeRef * {
121+
return m_reflection_ctx.readTypeFromMetadata(metadata);
122+
}}))
123+
return true;
124+
125+
// Continue with the base class.
126+
md_ptr = m_reflection_ctx.readSuperClassFromClassMetadata(metadata);
127+
}
128+
return false;
129+
}
130+
131+
llvm::Optional<std::pair<const swift::reflection::TypeRef *,
132+
swift::reflection::RemoteAddress>>
133+
ProjectExistentialAndUnwrapClass(
134+
swift::reflection::RemoteAddress existential_address,
135+
const swift::reflection::TypeRef &existential_tr) override {
136+
return m_reflection_ctx.projectExistentialAndUnwrapClass(
137+
existential_address, existential_tr);
138+
}
139+
140+
const swift::reflection::TypeRef *
141+
ReadTypeFromMetadata(lldb::addr_t metadata_address,
142+
bool skip_artificial_subclasses) override {
143+
return m_reflection_ctx.readTypeFromMetadata(metadata_address,
144+
skip_artificial_subclasses);
145+
}
146+
147+
const swift::reflection::TypeRef *
148+
ReadTypeFromInstance(lldb::addr_t instance_address,
149+
bool skip_artificial_subclasses) override {
150+
auto metadata_address =
151+
m_reflection_ctx.readMetadataFromInstance(instance_address);
152+
if (!metadata_address) {
153+
LLDB_LOGF(GetLog(LLDBLog::Types),
154+
"could not read heap metadata for object at %llu\n",
155+
instance_address);
156+
return nullptr;
157+
}
158+
159+
return m_reflection_ctx.readTypeFromMetadata(*metadata_address,
160+
skip_artificial_subclasses);
161+
}
162+
163+
swift::reflection::TypeRefBuilder &GetBuilder() override {
164+
return m_reflection_ctx.getBuilder();
165+
}
166+
167+
llvm::Optional<bool> IsValueInlinedInExistentialContainer(
168+
swift::remote::RemoteAddress existential_address) override {
169+
return m_reflection_ctx.isValueInlinedInExistentialContainer(
170+
existential_address);
171+
}
172+
173+
swift::remote::RemoteAbsolutePointer
174+
StripSignedPointer(swift::remote::RemoteAbsolutePointer pointer) override {
175+
return m_reflection_ctx.stripSignedPointer(pointer);
176+
}
177+
};
178+
179+
} // namespace
180+
181+
namespace lldb_private {
182+
std::unique_ptr<SwiftLanguageRuntimeImpl::ReflectionContextInterface>
183+
SwiftLanguageRuntimeImpl::ReflectionContextInterface::CreateReflectionContext(
184+
uint8_t ptr_size, std::shared_ptr<swift::remote::MemoryReader> reader,
185+
bool ObjCInterop, SwiftMetadataCache *swift_metadata_cache) {
186+
using ReflectionContext32ObjCInterop =
187+
TargetReflectionContext<swift::reflection::ReflectionContext<
188+
swift::External<swift::WithObjCInterop<swift::RuntimeTarget<4>>>>>;
189+
using ReflectionContext32NoObjCInterop =
190+
TargetReflectionContext<swift::reflection::ReflectionContext<
191+
swift::External<swift::NoObjCInterop<swift::RuntimeTarget<4>>>>>;
192+
using ReflectionContext64ObjCInterop =
193+
TargetReflectionContext<swift::reflection::ReflectionContext<
194+
swift::External<swift::WithObjCInterop<swift::RuntimeTarget<8>>>>>;
195+
using ReflectionContext64NoObjCInterop =
196+
TargetReflectionContext<swift::reflection::ReflectionContext<
197+
swift::External<swift::NoObjCInterop<swift::RuntimeTarget<8>>>>>;
198+
if (ptr_size == 4) {
199+
if (ObjCInterop)
200+
return std::make_unique<ReflectionContext32ObjCInterop>(
201+
reader, swift_metadata_cache);
202+
return std::make_unique<ReflectionContext32NoObjCInterop>(
203+
reader, swift_metadata_cache);
204+
}
205+
if (ptr_size == 8) {
206+
if (ObjCInterop)
207+
return std::make_unique<ReflectionContext64ObjCInterop>(
208+
reader, swift_metadata_cache);
209+
return std::make_unique<ReflectionContext64NoObjCInterop>(
210+
reader, swift_metadata_cache);
211+
}
212+
return {};
213+
}
214+
} // namespace lldb_private

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
#include "lldb/Utility/Timer.h"
4545

4646
#include "swift/AST/ASTMangler.h"
47-
#include "swift/AST/Decl.h"
48-
#include "swift/AST/Module.h"
4947
#include "swift/Demangling/Demangle.h"
5048
#include "swift/RemoteInspection/ReflectionContext.h"
5149
#include "swift/RemoteAST/RemoteAST.h"
@@ -525,22 +523,16 @@ void SwiftLanguageRuntimeImpl::SetupReflection() {
525523
objc_interop ? "with Objective-C interopability" : "Swift only";
526524

527525
auto &triple = exe_module->GetArchitecture().GetTriple();
528-
if (triple.isArch64Bit()) {
529-
LLDB_LOGF(log, "Initializing a 64-bit reflection context (%s) for \"%s\"",
530-
triple.str().c_str(), objc_interop_msg);
531-
m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext64(
532-
this->GetMemoryReader(), objc_interop, GetSwiftMetadataCache());
533-
} else if (triple.isArch32Bit()) {
534-
LLDB_LOGF(log,
535-
"Initializing a 32-bit reflection context (%s) for \"%s\"",
536-
triple.str().c_str(), objc_interop_msg);
537-
m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext32(
538-
this->GetMemoryReader(), objc_interop, GetSwiftMetadataCache());
539-
} else {
540-
LLDB_LOGF(log,
541-
"Could not initialize reflection context for \"%s\"",
542-
triple.str().c_str());
543-
}
526+
uint32_t ptr_size = m_process.GetAddressByteSize();
527+
LLDB_LOG(log, "Initializing a {0}-bit reflection context ({1}) for \"{2}\"",
528+
ptr_size * 8, triple.str(), objc_interop_msg);
529+
if (ptr_size == 4 || ptr_size == 8)
530+
m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext(
531+
ptr_size, this->GetMemoryReader(), objc_interop,
532+
GetSwiftMetadataCache());
533+
if (!m_reflection_ctx)
534+
LLDB_LOG(log, "Could not initialize reflection context for \"{0}\"",
535+
triple.str());
544536
// We set m_initialized_reflection_ctx to true here because
545537
// AddModuleToReflectionContext can potentially call into SetupReflection
546538
// again (which will early exit). This is safe to do since every other thread
@@ -685,7 +677,7 @@ bool SwiftLanguageRuntimeImpl::AddJitObjectFileToReflectionContext(
685677
if (!obj_file_format)
686678
return false;
687679

688-
auto reflection_info_id = m_reflection_ctx->addImage(
680+
auto reflection_info_id = m_reflection_ctx->AddImage(
689681
[&](swift::ReflectionSectionKind section_kind)
690682
-> std::pair<swift::remote::RemoteRef<void>, uint64_t> {
691683
auto section_name = obj_file_format->getSectionName(section_kind);
@@ -850,7 +842,7 @@ SwiftLanguageRuntimeImpl::AddObjectFileToReflectionContext(
850842
return {};
851843
};
852844

853-
return m_reflection_ctx->addImage(
845+
return m_reflection_ctx->AddImage(
854846
[&](swift::ReflectionSectionKind section_kind)
855847
-> std::pair<swift::remote::RemoteRef<void>, uint64_t> {
856848
auto pair = find_section_with_kind(segment, section_kind);
@@ -909,18 +901,18 @@ bool SwiftLanguageRuntimeImpl::AddModuleToReflectionContext(
909901
auto size = obj_file->GetData(0, obj_file->GetByteSize(), extractor);
910902
const uint8_t *file_data = extractor.GetDataStart();
911903
llvm::sys::MemoryBlock file_buffer((void *)file_data, size);
912-
info_id = m_reflection_ctx->readELF(
904+
info_id = m_reflection_ctx->ReadELF(
913905
swift::remote::RemoteAddress(load_ptr),
914906
llvm::Optional<llvm::sys::MemoryBlock>(file_buffer),
915907
likely_module_names);
916908
} else if (read_from_file_cache &&
917909
obj_file->GetPluginName().equals("mach-o")) {
918910
info_id = AddObjectFileToReflectionContext(module_sp, likely_module_names);
919911
if (!info_id)
920-
info_id = m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr),
912+
info_id = m_reflection_ctx->AddImage(swift::remote::RemoteAddress(load_ptr),
921913
likely_module_names);
922914
} else {
923-
info_id = m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr),
915+
info_id = m_reflection_ctx->AddImage(swift::remote::RemoteAddress(load_ptr),
924916
likely_module_names);
925917
}
926918

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "lldb/Core/PluginInterface.h"
2020
#include "lldb/Target/LanguageRuntime.h"
2121
#include "lldb/lldb-private.h"
22-
#include "swift/Demangling/Demangle.h"
2322

2423
#include "llvm/ADT/Optional.h"
2524
#include "llvm/ADT/StringSet.h"

0 commit comments

Comments
 (0)