Skip to content

Commit 0d1d33e

Browse files
authored
Merge pull request #5283 from jasonmolenda/r98754861-platform-binary-support-in-corefiles
Add mach-o corefile support for platform binaries
2 parents 1b62ca7 + b53c562 commit 0d1d33e

File tree

6 files changed

+271
-187
lines changed

6 files changed

+271
-187
lines changed

lldb/include/lldb/Target/DynamicLoader.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ class DynamicLoader : public PluginInterface {
226226
/// \param[in] process
227227
/// The process to add this binary to.
228228
///
229+
/// \param[in] name
230+
/// Name of the binary, if available. If this method cannot find a
231+
/// matching binary on the debug host, it may create a memory module
232+
/// out of live memory, and the provided name will be used. If an
233+
/// empty StringRef is provided, a name will be constructed for the module
234+
/// based on the address it is loaded at.
235+
///
229236
/// \param[in] uuid
230237
/// UUID of the binary to be loaded. UUID may be empty, and if a
231238
/// load address is supplied, will read the binary from memory, get
@@ -251,10 +258,9 @@ class DynamicLoader : public PluginInterface {
251258
///
252259
/// \return
253260
/// Returns a shared pointer for the Module that has been added.
254-
static lldb::ModuleSP
255-
LoadBinaryWithUUIDAndAddress(Process *process, UUID uuid, lldb::addr_t value,
256-
bool value_is_offset, bool force_symbol_search,
257-
bool notify);
261+
static lldb::ModuleSP LoadBinaryWithUUIDAndAddress(
262+
Process *process, llvm::StringRef name, UUID uuid, lldb::addr_t value,
263+
bool value_is_offset, bool force_symbol_search, bool notify);
258264

259265
/// Get information about the shared cache for a process, if possible.
260266
///

lldb/source/Core/DynamicLoader.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,19 @@ ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file,
175175
return nullptr;
176176
}
177177

178-
static ModuleSP ReadUnnamedMemoryModule(Process *process, addr_t addr) {
178+
static ModuleSP ReadUnnamedMemoryModule(Process *process, addr_t addr,
179+
llvm::StringRef name) {
179180
char namebuf[80];
180-
snprintf(namebuf, sizeof(namebuf), "memory-image-0x%" PRIx64, addr);
181-
return process->ReadModuleFromMemory(FileSpec(namebuf), addr);
181+
if (name.empty()) {
182+
snprintf(namebuf, sizeof(namebuf), "memory-image-0x%" PRIx64, addr);
183+
name = namebuf;
184+
}
185+
return process->ReadModuleFromMemory(FileSpec(name), addr);
182186
}
183187

184-
ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(Process *process,
185-
UUID uuid, addr_t value,
186-
bool value_is_offset,
187-
bool force_symbol_search,
188-
bool notify) {
188+
ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
189+
Process *process, llvm::StringRef name, UUID uuid, addr_t value,
190+
bool value_is_offset, bool force_symbol_search, bool notify) {
189191
ModuleSP memory_module_sp;
190192
ModuleSP module_sp;
191193
PlatformSP platform_sp = process->GetTarget().GetPlatform();
@@ -195,7 +197,7 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(Process *process,
195197
module_spec.GetUUID() = uuid;
196198

197199
if (!uuid.IsValid() && !value_is_offset) {
198-
memory_module_sp = ReadUnnamedMemoryModule(process, value);
200+
memory_module_sp = ReadUnnamedMemoryModule(process, value, name);
199201

200202
if (memory_module_sp)
201203
uuid = memory_module_sp->GetUUID();
@@ -223,7 +225,7 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(Process *process,
223225
// read it out of memory.
224226
if (!module_sp.get() && value != LLDB_INVALID_ADDRESS && !value_is_offset) {
225227
if (!memory_module_sp)
226-
memory_module_sp = ReadUnnamedMemoryModule(process, value);
228+
memory_module_sp = ReadUnnamedMemoryModule(process, value, name);
227229
if (memory_module_sp)
228230
module_sp = memory_module_sp;
229231
}

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

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7012,44 +7012,81 @@ ObjectFileMachO::GetCorefileAllImageInfos() {
70127012
bool ObjectFileMachO::LoadCoreFileImages(lldb_private::Process &process) {
70137013
MachOCorefileAllImageInfos image_infos = GetCorefileAllImageInfos();
70147014
Log *log = GetLog(LLDBLog::DynamicLoader);
7015+
Status error;
70157016

7017+
bool found_platform_binary = false;
70167018
ModuleList added_modules;
7017-
for (const MachOCorefileImageEntry &image : image_infos.all_image_infos) {
7018-
ModuleSP module_sp;
7019+
for (MachOCorefileImageEntry &image : image_infos.all_image_infos) {
7020+
ModuleSP module_sp, local_filesystem_module_sp;
7021+
7022+
// If this is a platform binary, it has been loaded (or registered with
7023+
// the DynamicLoader to be loaded), we don't need to do any further
7024+
// processing. We're not going to call ModulesDidLoad on this in this
7025+
// method, so notify==true.
7026+
if (process.GetTarget()
7027+
.GetDebugger()
7028+
.GetPlatformList()
7029+
.LoadPlatformBinaryAndSetup(&process, image.load_address,
7030+
true /* notify */)) {
7031+
LLDB_LOGF(log,
7032+
"ObjectFileMachO::%s binary at 0x%" PRIx64
7033+
" is a platform binary, has been handled by a Platform plugin.",
7034+
__FUNCTION__, image.load_address);
7035+
continue;
7036+
}
70197037

7020-
if (!image.filename.empty()) {
7021-
Status error;
7038+
// If this binary is currently executing, we want to force a
7039+
// possibly expensive search for the binary and its dSYM.
7040+
if (image.currently_executing && image.uuid.IsValid()) {
70227041
ModuleSpec module_spec;
70237042
module_spec.GetUUID() = image.uuid;
7024-
module_spec.GetFileSpec() = FileSpec(image.filename.c_str());
7025-
if (image.currently_executing) {
7026-
Symbols::DownloadObjectAndSymbolFile(module_spec, error, true);
7027-
if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
7028-
process.GetTarget().GetOrCreateModule(module_spec, false);
7029-
}
7043+
Symbols::DownloadObjectAndSymbolFile(module_spec, error, true);
7044+
if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
7045+
module_sp = process.GetTarget().GetOrCreateModule(module_spec, false);
7046+
process.GetTarget().GetImages().AppendIfNeeded(module_sp,
7047+
false /* notify */);
70307048
}
7049+
}
7050+
7051+
// We have an address, that's the best way to discover the binary.
7052+
if (!module_sp && image.load_address != LLDB_INVALID_ADDRESS) {
7053+
module_sp = DynamicLoader::LoadBinaryWithUUIDAndAddress(
7054+
&process, image.filename, image.uuid, image.load_address,
7055+
false /* value_is_offset */, image.currently_executing,
7056+
false /* notify */);
7057+
}
7058+
7059+
// If we have a slide, we need to find the original binary
7060+
// by UUID, then we can apply the slide value.
7061+
if (!module_sp && image.uuid.IsValid() &&
7062+
image.slide != LLDB_INVALID_ADDRESS) {
7063+
module_sp = DynamicLoader::LoadBinaryWithUUIDAndAddress(
7064+
&process, image.filename, image.uuid, image.slide,
7065+
true /* value_is_offset */, image.currently_executing,
7066+
false /* notify */);
7067+
}
7068+
7069+
// Try to find the binary by UUID or filename on the local
7070+
// filesystem or in lldb's global module cache.
7071+
if (!module_sp) {
7072+
Status error;
7073+
ModuleSpec module_spec;
7074+
if (image.uuid.IsValid())
7075+
module_spec.GetUUID() = image.uuid;
7076+
if (!image.filename.empty())
7077+
module_spec.GetFileSpec() = FileSpec(image.filename.c_str());
70317078
module_sp =
70327079
process.GetTarget().GetOrCreateModule(module_spec, false, &error);
70337080
process.GetTarget().GetImages().AppendIfNeeded(module_sp,
70347081
false /* notify */);
7035-
} else {
7036-
if (image.load_address != LLDB_INVALID_ADDRESS) {
7037-
module_sp = DynamicLoader::LoadBinaryWithUUIDAndAddress(
7038-
&process, image.uuid, image.load_address,
7039-
false /* value_is_offset */, image.currently_executing,
7040-
false /* notify */);
7041-
} else if (image.slide != LLDB_INVALID_ADDRESS) {
7042-
module_sp = DynamicLoader::LoadBinaryWithUUIDAndAddress(
7043-
&process, image.uuid, image.slide, true /* value_is_offset */,
7044-
image.currently_executing, false /* notify */);
7045-
}
70467082
}
70477083

7048-
if (module_sp.get()) {
7049-
// Will call ModulesDidLoad with all modules once they've all
7050-
// been added to the Target with load addresses. Don't notify
7051-
// here, before the load address is set.
7084+
// We have a ModuleSP to load in the Target. Load it at the
7085+
// correct address/slide and notify/load scripting resources.
7086+
if (module_sp) {
70527087
added_modules.Append(module_sp, false /* notify */);
7088+
7089+
// We have a list of segment load address
70537090
if (image.segment_load_addresses.size() > 0) {
70547091
if (log) {
70557092
std::string uuidstr = image.uuid.GetAsString();
@@ -7110,5 +7147,11 @@ bool ObjectFileMachO::LoadCoreFileImages(lldb_private::Process &process) {
71107147
process.Flush();
71117148
return true;
71127149
}
7150+
// Return true if the only binary we found was the platform binary,
7151+
// and it was loaded outside the scope of this method.
7152+
if (found_platform_binary)
7153+
return true;
7154+
7155+
// No binaries.
71137156
return false;
71147157
}

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ Status ProcessGDBRemote::DoConnectRemote(llvm::StringRef remote_url) {
584584
const bool force_symbol_search = true;
585585
const bool notify = true;
586586
DynamicLoader::LoadBinaryWithUUIDAndAddress(
587-
this, standalone_uuid, standalone_value,
587+
this, llvm::StringRef(), standalone_uuid, standalone_value,
588588
standalone_value_is_offset, force_symbol_search, notify);
589589
}
590590
}
@@ -616,7 +616,8 @@ Status ProcessGDBRemote::DoConnectRemote(llvm::StringRef remote_url) {
616616
const bool force_symbol_search = true;
617617
// Second manually load this binary into the Target.
618618
DynamicLoader::LoadBinaryWithUUIDAndAddress(
619-
this, uuid, addr, value_is_slide, force_symbol_search, notify);
619+
this, llvm::StringRef(), uuid, addr, value_is_slide,
620+
force_symbol_search, notify);
620621
}
621622
}
622623

0 commit comments

Comments
 (0)