Skip to content

Commit 3d63cec

Browse files
committed
[lldb] Try to map a real address to a tagged one in resolvePointer
If we read a real process address, try to map that back as a tagged LLDBMLLDBMemoryReader address, so further reads originating from it benefit from the file-cache optimization.
1 parent 0570498 commit 3d63cec

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,70 @@ LLDBMemoryReader::resolvePointerAsSymbol(swift::remote::RemoteAddress address) {
160160
return {};
161161
}
162162

163+
swift::remote::RemoteAbsolutePointer
164+
LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
165+
uint64_t readValue) {
166+
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
167+
168+
// We may have gotten a pointer to a process address, try to map it back
169+
// to a tagged address so further memory reads originating from it benefit
170+
// from the file-cache optimization.
171+
swift::remote::RemoteAbsolutePointer process_pointer("", readValue);
172+
173+
auto &target = m_process.GetTarget();
174+
Address addr;
175+
if (!target.ResolveLoadAddress(readValue, addr)) {
176+
LLDB_LOGV(log,
177+
"[MemoryReader] Could not resolve load address of pointer {0:x} "
178+
"read from {1:x}.",
179+
readValue, address.getAddressData());
180+
return process_pointer;
181+
}
182+
183+
auto module_containing_pointer = addr.GetSection()->GetModule();
184+
185+
// Check if the module containing the pointer is registered with
186+
// LLDBMemoryReader.
187+
auto pair_iterator = std::find_if(
188+
m_range_module_map.begin(), m_range_module_map.end(), [&](auto pair) {
189+
return std::get<ModuleSP>(pair) == module_containing_pointer;
190+
});
191+
192+
// We haven't registered the image that contains the pointer.
193+
if (pair_iterator == m_range_module_map.end()) {
194+
LLDB_LOG(log,
195+
"[MemoryReader] Could not resolve find module containing pointer "
196+
"{0:x} read from {1:x}.",
197+
readValue, address.getAddressData());
198+
return process_pointer;
199+
}
200+
201+
// If the containing image is the first registered one, the image's tagged
202+
// start address for it is the first tagged address. Otherwise, the previous
203+
// pair's address is the start tagged address.
204+
uint64_t start_tagged_address = pair_iterator == m_range_module_map.begin()
205+
? LLDB_FILE_ADDRESS_BIT
206+
: std::prev(pair_iterator)->first;
207+
208+
uint64_t tagged_address = start_tagged_address + addr.GetFileAddress();
209+
210+
if (tagged_address >= std::get<uint64_t>(*pair_iterator)) {
211+
// If the tagged address invades the next image's tagged address space,
212+
// something went wrong. Log it and just return the process address.
213+
LLDB_LOG(log,
214+
"[MemoryReader] Pointer {0:x} read from {1:x} resolved to tagged "
215+
"address {2:x}, which is outside its image address space.",
216+
readValue, address.getAddressData(), tagged_address);
217+
return process_pointer;
218+
}
219+
220+
LLDB_LOGV(log,
221+
"[MemoryReader] Successfully resolved pointer {0:x} read from "
222+
"{1:x} to tagged address {2:x}.",
223+
readValue, address.getAddressData(), tagged_address);
224+
return swift::remote::RemoteAbsolutePointer("", tagged_address);
225+
}
226+
163227
bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
164228
uint8_t *dest, uint64_t size) {
165229
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
@@ -26,6 +26,10 @@ class LLDBMemoryReader : public swift::remote::MemoryReader {
2626
llvm::Optional<swift::remote::RemoteAbsolutePointer>
2727
resolvePointerAsSymbol(swift::remote::RemoteAddress address) override;
2828

29+
swift::remote::RemoteAbsolutePointer
30+
resolvePointer(swift::remote::RemoteAddress address,
31+
uint64_t readValue) override;
32+
2933
bool readBytes(swift::remote::RemoteAddress address, uint8_t *dest,
3034
uint64_t size) override;
3135

0 commit comments

Comments
 (0)