Skip to content

Commit 201ddc5

Browse files
committed
Merge pull request #3848 from apple/lldb-Implement-LLDBMemoryReader-resolvePointer
[lldb] Implement LLDBMemoryReader::resolvePointer (cherry picked from commit bb7505d)
1 parent 17355e5 commit 201ddc5

File tree

14 files changed

+131
-0
lines changed

14 files changed

+131
-0
lines changed

lldb/include/lldb/Core/Section.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ class Section : public std::enable_shared_from_this<Section>,
195195
ObjectFile *GetObjectFile() { return m_obj_file; }
196196
const ObjectFile *GetObjectFile() const { return m_obj_file; }
197197

198+
bool CanContainSwiftReflectionData() const;
199+
198200
/// Read the section data from the object file that the section
199201
/// resides in.
200202
///

lldb/include/lldb/Symbol/ObjectFile.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,12 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
683683
virtual llvm::StringRef
684684
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);
685685

686+
#ifdef LLDB_ENABLE_SWIFT
687+
virtual bool CanContainSwiftReflectionData(const Section &section) {
688+
return false;
689+
}
690+
#endif // LLDB_ENABLE_SWIFT
691+
686692
/// Load binaries listed in a corefile
687693
///
688694
/// A corefile may have metadata listing binaries that can be loaded,

lldb/include/lldb/Target/Target.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ class TargetProperties : public Properties {
169169

170170
bool GetSwiftCreateModuleContextsInParallel() const;
171171

172+
bool GetSwiftUseReflectionSymbols() const;
173+
172174
bool GetEnableAutoImportClangModules() const;
173175

174176
bool GetUseAllCompilerFlags() const;

lldb/source/Core/Section.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ void Section::SetPermissions(uint32_t permissions) {
390390
m_executable = (permissions & ePermissionsExecutable) != 0;
391391
}
392392

393+
bool Section::CanContainSwiftReflectionData() const {
394+
#ifdef LLDB_ENABLE_SWIFT
395+
return m_obj_file->CanContainSwiftReflectionData(*this);
396+
#else
397+
return false;
398+
#endif // LLDB_ENABLE_SWIFT
399+
}
400+
393401
lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
394402
lldb::offset_t offset) {
395403
if (m_obj_file)

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "LLDBMemoryReader.h"
2+
#include "lldb/Core/Address.h"
23
#include "lldb/Core/Section.h"
34
#include "lldb/Utility/Log.h"
45
#include "lldb/Utility/Logging.h"
6+
#include "swift/Demangling/Demangle.h"
57

68
using namespace lldb;
79
using namespace lldb_private;
@@ -115,6 +117,50 @@ LLDBMemoryReader::getSymbolAddress(const std::string &name) {
115117
return swift::remote::RemoteAddress(load_addr);
116118
}
117119

120+
swift::remote::RemoteAbsolutePointer
121+
LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
122+
uint64_t readValue) {
123+
// If an address has a symbol, that symbol provides additional useful data to
124+
// MetadataReader. Without the symbol, MetadataReader can derive the symbol
125+
// by loading other parts of reflection metadata, but that work has a cost.
126+
// For lldb, that data loading can be a significant performance hit. Providing
127+
// a symbol greatly reduces memory read traffic to the process.
128+
auto pointer = swift::remote::RemoteAbsolutePointer("", readValue);
129+
130+
auto &target = m_process.GetTarget();
131+
if (!target.GetSwiftUseReflectionSymbols())
132+
return pointer;
133+
134+
llvm::Optional<Address> maybeAddr =
135+
resolveRemoteAddress(address.getAddressData());
136+
// This is not an assert, but should never happen.
137+
if (!maybeAddr)
138+
return pointer;
139+
140+
Address addr;
141+
if (maybeAddr->IsSectionOffset()) {
142+
// `address` was tagged, and then successfully mapped (resolved).
143+
addr = *maybeAddr;
144+
} else {
145+
// `address` is a real load address.
146+
if (!target.ResolveLoadAddress(address.getAddressData(), addr))
147+
return pointer;
148+
}
149+
150+
if (!addr.GetSection()->CanContainSwiftReflectionData())
151+
return pointer;
152+
153+
if (auto *symbol = addr.CalculateSymbolContextSymbol()) {
154+
auto mangledName = symbol->GetMangled().GetMangledName().GetStringRef();
155+
// MemoryReader requires this to be a Swift symbol. LLDB can also be
156+
// aware of local symbols, so avoid returning those.
157+
if (swift::Demangle::isSwiftSymbol(mangledName))
158+
return {mangledName, 0};
159+
}
160+
161+
return pointer;
162+
}
163+
118164
bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
119165
uint8_t *dest, uint64_t size) {
120166
if (m_local_buffer) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class LLDBMemoryReader : public swift::remote::MemoryReader {
2323
swift::remote::RemoteAddress
2424
getSymbolAddress(const std::string &name) override;
2525

26+
swift::remote::RemoteAbsolutePointer
27+
resolvePointer(swift::remote::RemoteAddress address,
28+
uint64_t readValue) override;
29+
2630
bool readBytes(swift::remote::RemoteAddress address, uint8_t *dest,
2731
uint64_t size) override;
2832

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3436,3 +3436,11 @@ llvm::StringRef ObjectFileELF::GetReflectionSectionIdentifier(
34363436
llvm_unreachable("Swift support disabled");
34373437
#endif //LLDB_ENABLE_SWIFT
34383438
}
3439+
3440+
#ifdef LLDB_ENABLE_SWIFT
3441+
bool ObjectFileELF::CanContainSwiftReflectionData(const Section &section) {
3442+
swift::SwiftObjectFileFormatELF file_format;
3443+
return file_format.sectionContainsReflectionData(
3444+
section.GetName().GetStringRef());
3445+
}
3446+
#endif // LLDB_ENABLE_SWIFT

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ class ObjectFileELF : public lldb_private::ObjectFile {
395395

396396
llvm::StringRef
397397
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section) override;
398+
399+
#ifdef LLDB_ENABLE_SWIFT
400+
bool
401+
CanContainSwiftReflectionData(const lldb_private::Section &section) override;
402+
#endif // LLDB_ENABLE_SWIFT
398403
};
399404

400405
#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6998,6 +6998,23 @@ llvm::StringRef ObjectFileMachO::GetReflectionSectionIdentifier(
69986998
#endif //LLDB_ENABLE_SWIFT
69996999
}
70007000

7001+
#ifdef LLDB_ENABLE_SWIFT
7002+
bool ObjectFileMachO::CanContainSwiftReflectionData(const Section &section) {
7003+
swift::SwiftObjectFileFormatMachO file_format;
7004+
if (file_format.sectionContainsReflectionData(
7005+
section.GetName().GetStringRef()))
7006+
return true;
7007+
// Some section names are not unique, such as `__const`, which could be in
7008+
// `__TEXT` or `__DATA`. For these, also check the segment,section name.
7009+
if (auto segment = section.GetParent()) {
7010+
std::string segmentSectionName =
7011+
llvm::formatv("{0},{1}", segment->GetName(), section.GetName()).str();
7012+
return file_format.sectionContainsReflectionData(segmentSectionName);
7013+
}
7014+
return false;
7015+
}
7016+
#endif // LLDB_ENABLE_SWIFT
7017+
70017018
ObjectFileMachO::MachOCorefileAllImageInfos
70027019
ObjectFileMachO::GetCorefileAllImageInfos() {
70037020
MachOCorefileAllImageInfos image_infos;

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ class ObjectFileMachO : public lldb_private::ObjectFile {
220220
llvm::StringRef
221221
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section) override;
222222

223+
#ifdef LLDB_ENABLE_SWIFT
224+
bool
225+
CanContainSwiftReflectionData(const lldb_private::Section &section) override;
226+
#endif // LLDB_ENABLE_SWIFT
227+
223228
/// A corefile may include metadata about all of the binaries that were
224229
/// present in the process when the corefile was taken. This is only
225230
/// implemented for Mach-O files for now; we'll generalize it when we

lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,3 +1225,11 @@ llvm::StringRef ObjectFilePECOFF::GetReflectionSectionIdentifier(
12251225
llvm_unreachable("Swift support disabled");
12261226
#endif //LLDB_ENABLE_SWIFT
12271227
}
1228+
1229+
#ifdef LLDB_ENABLE_SWIFT
1230+
bool ObjectFilePECOFF::CanContainSwiftReflectionData(const Section &section) {
1231+
swift::SwiftObjectFileFormatCOFF file_format;
1232+
return file_format.sectionContainsReflectionData(
1233+
section.GetName().GetStringRef());
1234+
}
1235+
#endif // LLDB_ENABLE_SWIFT

lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ class ObjectFilePECOFF : public lldb_private::ObjectFile {
290290
llvm::StringRef
291291
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section) override;
292292

293+
#ifdef LLDB_ENABLE_SWIFT
294+
bool
295+
CanContainSwiftReflectionData(const lldb_private::Section &section) override;
296+
#endif // LLDB_ENABLE_SWIFT
297+
293298
typedef std::vector<section_header_t> SectionHeaderColl;
294299
typedef SectionHeaderColl::iterator SectionHeaderCollIter;
295300
typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;

lldb/source/Target/Target.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,6 +4180,18 @@ bool TargetProperties::GetSwiftCreateModuleContextsInParallel() const {
41804180
return true;
41814181
}
41824182

4183+
bool TargetProperties::GetSwiftUseReflectionSymbols() const {
4184+
const Property *exp_property = m_collection_sp->GetPropertyAtIndex(
4185+
nullptr, false, ePropertyExperimental);
4186+
OptionValueProperties *exp_values =
4187+
exp_property->GetValue()->GetAsProperties();
4188+
if (exp_values)
4189+
return exp_values->GetPropertyAtIndexAsBoolean(
4190+
nullptr, ePropertySwiftUseReflectionSymbols, true);
4191+
else
4192+
return true;
4193+
}
4194+
41834195
ArchSpec TargetProperties::GetDefaultArchitecture() const {
41844196
OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(
41854197
nullptr, ePropertyDefaultArch);

lldb/source/Target/TargetProperties.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ let Definition = "target_experimental" in {
77
def SwiftCreateModuleContextsInParallel : Property<"swift-create-module-contexts-in-parallel", "Boolean">,
88
DefaultTrue,
99
Desc<"Create the per-module Swift AST contexts in parallel.">;
10+
def SwiftUseReflectionSymbols : Property<"swift-use-reflection-symbols", "Boolean">,
11+
Global, DefaultTrue,
12+
Desc<"if true, optimize the loading of Swift reflection metadata by making use of available symbols.">;
1013
}
1114

1215
let Definition = "target" in {

0 commit comments

Comments
 (0)