jasonmolenda created this revision.
jasonmolenda added reviewers: bulbazord, JDevlieghere.
jasonmolenda added a project: LLDB.
Herald added a project: All.
jasonmolenda requested review of this revision.
Herald added a subscriber: lldb-commits.

We have some corefiles which are intended to debug a standalone binary, and 
have an LC_NOTE mach-o load command with the UUID and/or address of that 
binary.  If the search for that binary fails for any reason, we don't load the 
intended binary.  ProcessMachCore then falls back to doing an exhaustive scan 
of the corefile memory pages looking for a darwin kernel or a userland dyld 
binary.  And the case where this comes up most often, there is a darwin kernel. 
 lldb starts doing a darwin kernel debug session and that's not at all what the 
user is intending to do.

This patch changes ProcessMachCore::LoadBinariesViaMetadata to return if any 
metadata record was found, even if we didn't find the binary, and 
ProcessMachCore::LoadBinariesAndSetDYLD will only start an exhaustive search if 
we had no metadata.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157168

Files:
  lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
  lldb/source/Plugins/Process/mach-core/ProcessMachCore.h

Index: lldb/source/Plugins/Process/mach-core/ProcessMachCore.h
===================================================================
--- lldb/source/Plugins/Process/mach-core/ProcessMachCore.h
+++ lldb/source/Plugins/Process/mach-core/ProcessMachCore.h
@@ -86,7 +86,7 @@
 
 private:
   void CreateMemoryRegions();
-  void LoadBinariesViaMetadata();
+  bool LoadBinariesViaMetadata();
   void LoadBinariesViaExhaustiveSearch();
   void LoadBinariesAndSetDYLD();
   void CleanupMemoryRegionPermissions();
Index: lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
===================================================================
--- lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
+++ lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
@@ -223,16 +223,20 @@
   }
 }
 
-void ProcessMachCore::LoadBinariesViaMetadata() {
+bool ProcessMachCore::LoadBinariesViaMetadata() {
   Log *log(GetLog(LLDBLog::DynamicLoader | LLDBLog::Process));
   ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
-  bool found_main_binary_definitively = false;
 
   addr_t objfile_binary_value;
   bool objfile_binary_value_is_offset;
   UUID objfile_binary_uuid;
   ObjectFile::BinaryType type;
 
+  // This will be set to true if we had a metadata hint
+  // specifying a UUID or address -- and we should not fall back
+  // to doing an exhaustive search.
+  bool found_binary_spec_in_metadata = false;
+
   if (core_objfile->GetCorefileMainBinaryInfo(objfile_binary_value,
                                               objfile_binary_value_is_offset,
                                               objfile_binary_uuid, type)) {
@@ -244,6 +248,7 @@
                   objfile_binary_uuid.GetAsString().c_str(),
                   objfile_binary_value, objfile_binary_value_is_offset, type);
     }
+    found_binary_spec_in_metadata = true;
 
     // If this is the xnu kernel, don't load it now.  Note the correct
     // DynamicLoader plugin to use, and the address of the kernel, and
@@ -251,7 +256,6 @@
     if (type == ObjectFile::eBinaryTypeKernel) {
       m_mach_kernel_addr = objfile_binary_value;
       m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
-      found_main_binary_definitively = true;
     } else if (type == ObjectFile::eBinaryTypeUser) {
       m_dyld_addr = objfile_binary_value;
       m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic();
@@ -263,7 +267,6 @@
               this, llvm::StringRef(), objfile_binary_uuid,
               objfile_binary_value, objfile_binary_value_is_offset,
               force_symbol_search, notify, set_address_in_target)) {
-        found_main_binary_definitively = true;
         m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
       }
     }
@@ -274,7 +277,6 @@
   // load command is present, let's use the contents.
   UUID ident_uuid;
   addr_t ident_binary_addr = LLDB_INVALID_ADDRESS;
-  if (!found_main_binary_definitively) {
     std::string corefile_identifier = core_objfile->GetIdentifierString();
 
     // Search for UUID= and stext= strings in the identifier str.
@@ -285,6 +287,7 @@
       if (log)
         log->Printf("Got a UUID from LC_IDENT/kern ver str LC_NOTE: %s",
                     ident_uuid.GetAsString().c_str());
+      found_binary_spec_in_metadata = true;
     }
     if (corefile_identifier.find("stext=") != std::string::npos) {
       size_t p = corefile_identifier.find("stext=") + strlen("stext=");
@@ -295,6 +298,7 @@
           log->Printf("Got a load address from LC_IDENT/kern ver str "
                       "LC_NOTE: 0x%" PRIx64,
                       ident_binary_addr);
+        found_binary_spec_in_metadata = true;
       }
     }
 
@@ -307,7 +311,7 @@
             "ProcessMachCore::LoadBinariesViaMetadata: Found kernel binary via "
             "LC_IDENT/kern ver str LC_NOTE");
       m_mach_kernel_addr = ident_binary_addr;
-      found_main_binary_definitively = true;
+      found_binary_spec_in_metadata = true;
     } else if (ident_uuid.IsValid()) {
       // We have no address specified, only a UUID.  Load it at the file
       // address.
@@ -319,16 +323,14 @@
               this, llvm::StringRef(), ident_uuid, ident_binary_addr,
               value_is_offset, force_symbol_search, notify,
               set_address_in_target)) {
-        found_main_binary_definitively = true;
+        found_binary_spec_in_metadata = true;
         m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
       }
     }
-  }
 
   // Finally, load any binaries noted by "load binary" LC_NOTEs in the
   // corefile
   if (core_objfile->LoadCoreFileImages(*this)) {
-    found_main_binary_definitively = true;
     m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
   }
 
@@ -338,6 +340,8 @@
   // un-set it later.
   if (m_dyld_up)
     m_dyld_plugin_name = GetDynamicLoader()->GetPluginName();
+
+  return found_binary_spec_in_metadata;
 }
 
 void ProcessMachCore::LoadBinariesViaExhaustiveSearch() {
@@ -414,8 +418,8 @@
 void ProcessMachCore::LoadBinariesAndSetDYLD() {
   Log *log(GetLog(LLDBLog::DynamicLoader | LLDBLog::Process));
 
-  LoadBinariesViaMetadata();
-  if (m_dyld_plugin_name.empty())
+  bool found_binary_spec_in_metadata = LoadBinariesViaMetadata();
+  if (!found_binary_spec_in_metadata)
     LoadBinariesViaExhaustiveSearch();
 
   if (m_dyld_plugin_name.empty()) {
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to