[Lldb-commits] [lldb] r277088 - Move the code which knows how to get information about the shared

Jason Molenda via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 28 17:18:40 PDT 2016


Author: jmolenda
Date: Thu Jul 28 19:18:39 2016
New Revision: 277088

URL: http://llvm.org/viewvc/llvm-project?rev=277088&view=rev
Log:
Move the code which knows how to get information about the shared
cache from ObjectFileMachO (very wrong place) to the DynamicLoader
plugins (better place).  Not much change to the code itself, although
the old ObjectFileMachO method would try both the new dyld SPI and
reading the dyld_all_image_infos structure.  In the new methods,
I've separated those into the appropriate DynamicLoader plugins.


Modified:
    lldb/trunk/include/lldb/Target/DynamicLoader.h
    lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp
    lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.h
    lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
    lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h
    lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Modified: lldb/trunk/include/lldb/Target/DynamicLoader.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/DynamicLoader.h?rev=277088&r1=277087&r2=277088&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/DynamicLoader.h (original)
+++ lldb/trunk/include/lldb/Target/DynamicLoader.h Thu Jul 28 19:18:39 2016
@@ -14,6 +14,7 @@
 #include "lldb/lldb-private.h"
 #include "lldb/Core/Error.h"
 #include "lldb/Core/PluginInterface.h"
+#include "lldb/Core/UUID.h"
 
 namespace lldb_private {
 
@@ -251,6 +252,55 @@ public:
                         lldb::addr_t base_addr,
                         bool base_addr_is_offset);
 
+    //------------------------------------------------------------------
+    /// Get information about the shared cache for a process, if possible.
+    ///
+    /// On some systems (e.g. Darwin based systems), a set of libraries 
+    /// that are common to most processes may be put in a single region
+    /// of memory and mapped into every process, this is called the
+    /// shared cache, as a performance optimization.
+    ///
+    /// Many targets will not have the concept of a shared cache.
+    ///
+    /// Depending on how the DynamicLoader gathers information about the
+    /// shared cache, it may be able to only return basic information - 
+    /// like the UUID of the cache - or it may be able to return additional
+    /// information about the cache.  
+    ///
+    /// @param[out] base_address
+    ///     The base address (load address) of the shared cache.
+    ///     LLDB_INVALID_ADDRESS if it cannot be determined.
+    ///
+    /// @param[out] uuid
+    ///     The UUID of the shared cache, if it can be determined.
+    ///     If the UUID cannot be fetched, IsValid() will be false.
+    ///
+    /// @param[out] using_shared_cache
+    ///     If this process is using a shared cache.
+    ///     If unknown, eLazyBoolCalculate is returned.
+    ///
+    /// @param[out] private_shared_cache
+    ///     A LazyBool indicating whether this process is using a
+    ///     private shared cache.
+    ///     If this information cannot be fetched, eLazyBoolCalculate.
+    ///
+    /// @return
+    ///     Returns false if this DynamicLoader cannot gather information
+    ///     about the shared cache / has no concept of a shared cache.
+    //------------------------------------------------------------------
+    virtual bool
+    GetSharedCacheInformation (lldb::addr_t &base_address, 
+                               UUID &uuid,
+                               LazyBool &using_shared_cache,
+                               LazyBool &private_shared_cache)
+    {
+        base_address = LLDB_INVALID_ADDRESS;
+        uuid.Clear();
+        using_shared_cache = eLazyBoolCalculate;
+        private_shared_cache = eLazyBoolCalculate;
+        return false;
+    }
+
 protected:
     //------------------------------------------------------------------
     // Utility methods for derived classes

Modified: lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp?rev=277088&r1=277087&r2=277088&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp Thu Jul 28 19:18:39 2016
@@ -490,6 +490,52 @@ DynamicLoaderMacOS::CanLoadImage ()
     return error;
 }
 
+bool
+DynamicLoaderMacOS::GetSharedCacheInformation (lldb::addr_t &base_address,
+                                               UUID &uuid,
+                                               LazyBool &using_shared_cache,
+                                               LazyBool &private_shared_cache)
+{
+    base_address = LLDB_INVALID_ADDRESS;
+    uuid.Clear();
+    using_shared_cache = eLazyBoolCalculate;
+    private_shared_cache = eLazyBoolCalculate;
+
+    if (m_process)
+    {
+        StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
+        StructuredData::Dictionary *info_dict = nullptr;
+        if (info.get() && info->GetAsDictionary())
+        {
+            info_dict = info->GetAsDictionary();
+        }
+
+        // {"shared_cache_base_address":140735683125248,"shared_cache_uuid":"DDB8D70C-C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false}
+
+        if (info_dict
+            && info_dict->HasKey("shared_cache_uuid")
+            && info_dict->HasKey("no_shared_cache")
+            && info_dict->HasKey("shared_cache_base_address"))
+        {
+            base_address = info_dict->GetValueForKey("shared_cache_base_address")->GetIntegerValue(LLDB_INVALID_ADDRESS);
+            std::string uuid_str = info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue().c_str();
+            if (!uuid_str.empty())
+                uuid.SetFromCString (uuid_str.c_str());
+            if (info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue() == false)
+                using_shared_cache = eLazyBoolYes;
+            else
+                using_shared_cache = eLazyBoolNo;
+            if (info_dict->GetValueForKey("shared_cache_private_cache")->GetBooleanValue())
+                private_shared_cache = eLazyBoolYes;
+            else
+                private_shared_cache = eLazyBoolNo;
+
+            return true;
+        }
+    }
+    return false;
+}
+
 void
 DynamicLoaderMacOS::Initialize()
 {

Modified: lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.h?rev=277088&r1=277087&r2=277088&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.h (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.h Thu Jul 28 19:18:39 2016
@@ -70,6 +70,12 @@ public:
     lldb_private::Error
     CanLoadImage() override;
 
+    bool
+    GetSharedCacheInformation (lldb::addr_t &base_address,
+                               lldb_private::UUID &uuid,
+                               lldb_private::LazyBool &using_shared_cache,
+                               lldb_private::LazyBool &private_shared_cache) override;
+
     //------------------------------------------------------------------
     // PluginInterface protocol
     //------------------------------------------------------------------

Modified: lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp?rev=277088&r1=277087&r2=277088&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp Thu Jul 28 19:18:39 2016
@@ -1153,6 +1153,79 @@ DynamicLoaderMacOSXDYLD::CanLoadImage ()
     return error;
 }
 
+bool
+DynamicLoaderMacOSXDYLD::GetSharedCacheInformation (lldb::addr_t &base_address,
+                                               UUID &uuid,
+                                               LazyBool &using_shared_cache,
+                                               LazyBool &private_shared_cache)
+{
+    base_address = LLDB_INVALID_ADDRESS;
+    uuid.Clear();
+    using_shared_cache = eLazyBoolCalculate;
+    private_shared_cache = eLazyBoolCalculate;
+
+    if (m_process)
+    {
+        addr_t all_image_infos = m_process->GetImageInfoAddress();
+
+        // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
+        // or it may be the address of the dyld_all_image_infos structure (want).  The first four
+        // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
+        // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
+
+        Error err;
+        uint32_t version_or_magic = m_process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
+        if (version_or_magic != static_cast<uint32_t>(-1)
+            && version_or_magic != llvm::MachO::MH_MAGIC
+            && version_or_magic != llvm::MachO::MH_CIGAM
+            && version_or_magic != llvm::MachO::MH_MAGIC_64
+            && version_or_magic != llvm::MachO::MH_CIGAM_64
+            && version_or_magic >= 13)
+        {
+            addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
+            int wordsize = m_process->GetAddressByteSize();
+            if (wordsize == 8)
+            {
+                sharedCacheUUID_address = all_image_infos + 160;  // sharedCacheUUID <mach-o/dyld_images.h>
+            }
+            if (wordsize == 4)
+            {
+                sharedCacheUUID_address = all_image_infos + 84;   // sharedCacheUUID <mach-o/dyld_images.h>
+            }
+            if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
+            {
+                uuid_t shared_cache_uuid;
+                if (m_process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
+                {
+                    uuid.SetBytes (shared_cache_uuid);
+                    if (uuid.IsValid ())
+                    {
+                        using_shared_cache = eLazyBoolYes;
+                    }
+                }
+
+                if (version_or_magic >= 15)
+                {
+                    // The sharedCacheBaseAddress field is the next one in the dyld_all_image_infos struct.
+                    addr_t sharedCacheBaseAddr_address = sharedCacheUUID_address + 16;
+                    Error error;
+                    base_address = m_process->ReadUnsignedIntegerFromMemory (sharedCacheBaseAddr_address, wordsize, LLDB_INVALID_ADDRESS, error);
+                    if (error.Fail())
+                        base_address = LLDB_INVALID_ADDRESS;
+                }
+
+                return true;
+            }
+
+            //
+            // add
+            // NB: sharedCacheBaseAddress is the next field in dyld_all_image_infos after
+            // sharedCacheUUID -- that is, 16 bytes after it, if we wanted to fetch it.
+        }
+    }
+    return false;
+}
+
 void
 DynamicLoaderMacOSXDYLD::Initialize()
 {

Modified: lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h?rev=277088&r1=277087&r2=277088&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h Thu Jul 28 19:18:39 2016
@@ -74,6 +74,12 @@ public:
     lldb_private::Error
     CanLoadImage() override;
 
+    bool
+    GetSharedCacheInformation (lldb::addr_t &base_address,
+                               lldb_private::UUID &uuid,
+                               lldb_private::LazyBool &using_shared_cache,
+                               lldb_private::LazyBool &private_shared_cache) override;
+
     //------------------------------------------------------------------
     // PluginInterface protocol
     //------------------------------------------------------------------

Modified: lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp?rev=277088&r1=277087&r2=277088&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp (original)
+++ lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp Thu Jul 28 19:18:39 2016
@@ -32,6 +32,7 @@
 #include "lldb/Host/FileSpec.h"
 #include "lldb/Symbol/DWARFCallFrameInfo.h"
 #include "lldb/Symbol/ObjectFile.h"
+#include "lldb/Target/DynamicLoader.h"
 #include "lldb/Target/MemoryRegionInfo.h"
 #include "lldb/Target/Platform.h"
 #include "lldb/Target/Process.h"
@@ -5449,70 +5450,13 @@ UUID
 ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
 {
     UUID uuid;
-
-    // First see if we can get the shared cache details from debugserver
-    if (process)
-    {
-        StructuredData::ObjectSP info = process->GetSharedCacheInfo();
-        StructuredData::Dictionary *info_dict = nullptr;
-        if (info.get() && info->GetAsDictionary())
-        {
-            info_dict = info->GetAsDictionary();
-        }
-
-        // {"shared_cache_base_address":140735683125248,"shared_cache_uuid":"DDB8D70C-C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false}
-
-        if (info_dict
-            && info_dict->HasKey("shared_cache_uuid")
-            && info_dict->HasKey("no_shared_cache")
-            && info_dict->HasKey("shared_cache_base_address"))
-        {
-            bool process_using_shared_cache = info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue() == false;
-            std::string uuid_str = info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue();
-
-            if (process_using_shared_cache && !uuid_str.empty() && uuid.SetFromCString (uuid_str.c_str()) == 0)
-                return uuid;
-        }
-    }
-
-    // Fall back to trying to read the shared cache info out of dyld's internal data structures
-    if (process)
+    if (process && process->GetDynamicLoader())
     {
-        addr_t all_image_infos = process->GetImageInfoAddress();
-
-        // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
-        // or it may be the address of the dyld_all_image_infos structure (want).  The first four
-        // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
-        // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
-
-        Error err;
-        uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
-        if (version_or_magic != static_cast<uint32_t>(-1)
-            && version_or_magic != MH_MAGIC
-            && version_or_magic != MH_CIGAM
-            && version_or_magic != MH_MAGIC_64
-            && version_or_magic != MH_CIGAM_64
-            && version_or_magic >= 13)
-        {
-            addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
-            int wordsize = process->GetAddressByteSize();
-            if (wordsize == 8)
-            {
-                sharedCacheUUID_address = all_image_infos + 160;  // sharedCacheUUID <mach-o/dyld_images.h>
-            }
-            if (wordsize == 4)
-            {
-                sharedCacheUUID_address = all_image_infos + 84;   // sharedCacheUUID <mach-o/dyld_images.h>
-            }
-            if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
-            {
-                uuid_t shared_cache_uuid;
-                if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
-                {
-                    uuid.SetBytes (shared_cache_uuid);
-                }
-            }
-        }
+        DynamicLoader *dl = process->GetDynamicLoader();
+        addr_t load_address;
+        LazyBool using_shared_cache;
+        LazyBool private_shared_cache;
+        dl->GetSharedCacheInformation (load_address, uuid, using_shared_cache, private_shared_cache);
     }
     return uuid;
 }




More information about the lldb-commits mailing list