Cleanup ILLDBServices2 interface.
authorMike McLaughlin <mikem@microsoft.com>
Sun, 17 Feb 2019 19:57:52 +0000 (11:57 -0800)
committerMike McLaughlin <mikem@microsoft.com>
Sun, 17 Feb 2019 19:57:52 +0000 (11:57 -0800)
Add runtineOnly option to LoadNativeSymbols to use to get DAC/DBI module name.

Use LoadNativeSymbols(true) to get the DAC/DBI modules when don't exist locally.

src/SOS/SOS.NETCore/SymbolReader.cs
src/SOS/Strike/hostcoreclr.cpp
src/SOS/Strike/hostcoreclr.h
src/SOS/lldbplugin/inc/lldbservices.h
src/SOS/lldbplugin/services.cpp
src/SOS/lldbplugin/services.h

index 06ce2ca197d15157bce4111e6313ad3e8fa63523..3a098e507739ac99e85317e2304385fa895faa37 100644 (file)
@@ -158,10 +158,7 @@ namespace SOS
         /// </summary>
         /// <param name="moduleFileName">module file name</param>
         /// <param name="symbolFileName">symbol file name and path</param>
-        internal delegate void SymbolFileCallback(
-            IntPtr parameter,
-            [MarshalAs(UnmanagedType.LPStr)] string moduleFileName, 
-            [MarshalAs(UnmanagedType.LPStr)] string symbolFileName);
+        public delegate void SymbolFileCallback(IntPtr parameter, [MarshalAs(UnmanagedType.LPStr)] string moduleFileName, [MarshalAs(UnmanagedType.LPStr)] string symbolFileName);
 
         static SymbolStore s_symbolStore = null;
         static bool s_symbolCacheAdded = false;
@@ -242,18 +239,15 @@ namespace SOS
         /// </summary>
         /// <param name="callback">called back for each symbol file loaded</param>
         /// <param name="parameter">callback parameter</param>
-        /// <param name="moduleDirectory">module path</param>
-        /// <param name="moduleFileName">module file name</param>
+        /// <param name="moduleFilePath">module path</param>
         /// <param name="address">module base address</param>
         /// <param name="size">module size</param>
         /// <param name="readMemory">read memory callback delegate</param>
-        internal static void LoadNativeSymbols(SymbolFileCallback callback, IntPtr parameter, string tempDirectory, string moduleDirectory, string moduleFileName, 
-            ulong address, int size, ReadMemoryDelegate readMemory)
+        public static void LoadNativeSymbols(SymbolFileCallback callback, IntPtr parameter, string tempDirectory, string moduleFilePath, ulong address, int size, ReadMemoryDelegate readMemory)
         {
             if (s_symbolStore != null)
             {
                 Debug.Assert(s_tracer != null);
-                string path = Path.Combine(moduleDirectory, moduleFileName);
                 Stream stream = new TargetStream(address, size, readMemory);
                 KeyTypeFlags flags = KeyTypeFlags.SymbolKey | KeyTypeFlags.ClrKeys;
                 KeyGenerator generator = null;
@@ -261,12 +255,12 @@ namespace SOS
                 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                 {
                     var elfFile = new ELFFile(new StreamAddressSpace(stream), 0, true);
-                    generator = new ELFFileKeyGenerator(s_tracer, elfFile, path);
+                    generator = new ELFFileKeyGenerator(s_tracer, elfFile, moduleFilePath);
                 }
                 else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                 {
                     var machOFile = new MachOFile(new StreamAddressSpace(stream), 0, true);
-                    generator = new MachOFileKeyGenerator(s_tracer, machOFile, path);
+                    generator = new MachOFileKeyGenerator(s_tracer, machOFile, moduleFilePath);
                 }
                 else
                 {
@@ -278,11 +272,11 @@ namespace SOS
                     IEnumerable<SymbolStoreKey> keys = generator.GetKeys(flags);
                     foreach (SymbolStoreKey key in keys)
                     {
-                        string symbolFileName = Path.GetFileName(key.FullPathName);
+                        string moduleFileName = Path.GetFileName(key.FullPathName);
                         s_tracer.Verbose("{0} {1}", key.FullPathName, key.Index);
 
                         // Don't download the sos binaries that come with the runtime
-                        if (symbolFileName != "SOS.NETCore.dll" && !symbolFileName.StartsWith("libsos."))
+                        if (moduleFileName != "SOS.NETCore.dll" && !moduleFileName.StartsWith("libsos."))
                         {
                             using (SymbolStoreFile file = GetSymbolStoreFile(key))
                             {
@@ -290,20 +284,20 @@ namespace SOS
                                 {
                                     try
                                     {
-                                        string downloadFileName = file.FileName;
+                                        string downloadFilePath = file.FileName;
 
                                         // If the downloaded doesn't already exists on disk in the cache, then write it to a temporary location.
-                                        if (!File.Exists(downloadFileName))
+                                        if (!File.Exists(downloadFilePath))
                                         {
-                                            downloadFileName = Path.Combine(tempDirectory, symbolFileName);
+                                            downloadFilePath = Path.Combine(tempDirectory, moduleFileName);
 
-                                            using (Stream destinationStream = File.OpenWrite(downloadFileName)) {
+                                            using (Stream destinationStream = File.OpenWrite(downloadFilePath)) {
                                                 file.Stream.CopyTo(destinationStream);
                                             }
                                             s_tracer.WriteLine("Downloaded symbol file {0}", key.FullPathName);
                                         }
-                                        s_tracer.Information("{0}: {1}", symbolFileName, downloadFileName);
-                                        callback(parameter, symbolFileName, downloadFileName);
+                                        s_tracer.Information("{0}: {1}", moduleFileName, downloadFilePath);
+                                        callback(parameter, moduleFileName, downloadFilePath);
                                     }
                                     catch (Exception ex) when (ex is UnauthorizedAccessException || ex is DirectoryNotFoundException)
                                     {
@@ -316,7 +310,7 @@ namespace SOS
                 }
                 catch (Exception ex) when (ex is BadInputFormatException || ex is InvalidVirtualAddressException)
                 {
-                    s_tracer.Error("Exception: {0}/{1}: {2:X16}", moduleDirectory, moduleFileName, address);
+                    s_tracer.Error("Exception: {0}/{1}: {2:X16}", moduleFilePath, address);
                 }
             }
         }
index 4ff608ac248f50685b133c9f5f47dc6b075e4140..df6e99006061610e4e51c3dda6136ed78f27af2c 100644 (file)
@@ -237,7 +237,6 @@ HRESULT GetCoreClrDirectory(std::string& coreClrDirectory)
     }
     if (!GetAbsolutePath(directory, coreClrDirectory))
     {
-        ExtErr("Error: Failed to get coreclr absolute path\n");
         return E_FAIL;
     }
 #else
@@ -410,7 +409,7 @@ static LPCSTR GetTempDirectory()
             strcat_s(tmpPath, MAX_LONGPATH, DIRECTORY_SEPARATOR_STR_A);
         }
         char pidstr[128];
-        sprintf_s(pidstr, _countof(pidstr), "%d", GetCurrentProcessId());
+        sprintf_s(pidstr, _countof(pidstr), "sos%d", GetCurrentProcessId());
         strcat_s(tmpPath, MAX_LONGPATH, pidstr);
         strcat_s(tmpPath, MAX_LONGPATH, DIRECTORY_SEPARATOR_STR_A);
 
@@ -503,6 +502,12 @@ LPCSTR GetDacFilePath()
                 g_dacFilePath = _strdup(dacModulePath.c_str());
             }
         }
+
+        if (g_dacFilePath == nullptr)
+        {
+            // Attempt to only load the DAC/DBI modules
+            LoadNativeSymbols(true);
+        }
     }
     return g_dacFilePath;
 }
@@ -524,7 +529,15 @@ LPCSTR GetDbiFilePath()
             // if DBI file exists
             if (access(dbiModulePath.c_str(), F_OK) == 0)
 #endif
+            {
                 g_dbiFilePath = _strdup(dbiModulePath.c_str());
+            }
+        }
+
+        if (g_dbiFilePath == nullptr)
+        {
+            // Attempt to only load the DAC/DBI modules
+            LoadNativeSymbols(true);
         }
     }
     return g_dbiFilePath;
@@ -697,20 +710,20 @@ static int ReadMemoryForSymbols(ULONG64 address, uint8_t *buffer, int cb)
 //
 // Symbol downloader callback
 //
-static void SymbolFileCallback(void* param, const char* moduleFileName, const char* symbolFileName)
+static void SymbolFileCallback(void* param, const char* moduleFileName, const char* symbolFilePath)
 {
     if (strcmp(moduleFileName, MAIN_CLR_DLL_NAME_A) == 0) {
         return;
     }
     if (strcmp(moduleFileName, MAKEDLLNAME_A("mscordaccore")) == 0) {
         if (g_dacFilePath == nullptr) {
-            g_dacFilePath = _strdup(symbolFileName);
+            g_dacFilePath = _strdup(symbolFilePath);
         }
         return;
     }
     if (strcmp(moduleFileName, MAKEDLLNAME_A("mscordbi")) == 0) {
         if (g_dbiFilePath == nullptr) {
-            g_dbiFilePath = _strdup(symbolFileName);
+            g_dbiFilePath = _strdup(symbolFilePath);
         }
         return;
     }
@@ -718,18 +731,18 @@ static void SymbolFileCallback(void* param, const char* moduleFileName, const ch
     HRESULT Status = g_ExtServices->QueryInterface(__uuidof(ILLDBServices2), (void**)&services2);
     if (SUCCEEDED(Status))
     {
-        services2->AddModuleSymbol(param, symbolFileName);
+        services2->AddModuleSymbol(param, symbolFilePath);
     }
 }
 
 //
 // Enumerate native module callback
 //
-static void LoadNativeSymbolsCallback(void* param, const char* moduleDirectory, const char* moduleFileName, ULONG64 moduleAddress, int moduleSize)
+static void LoadNativeSymbolsCallback(void* param, const char* moduleFilePath, ULONG64 moduleAddress, int moduleSize)
 {
     _ASSERTE(g_hostingInitialized);
     _ASSERTE(g_SOSNetCoreCallbacks.LoadNativeSymbolsDelegate != nullptr);
-    g_SOSNetCoreCallbacks.LoadNativeSymbolsDelegate(SymbolFileCallback, param, GetTempDirectory(), moduleDirectory, moduleFileName, moduleAddress, moduleSize, ReadMemoryForSymbols);
+    g_SOSNetCoreCallbacks.LoadNativeSymbolsDelegate(SymbolFileCallback, param, GetTempDirectory(), moduleFilePath, moduleAddress, moduleSize, ReadMemoryForSymbols);
 }
 
 #endif
@@ -757,7 +770,7 @@ HRESULT InitializeSymbolStore(BOOL logging, BOOL msdl, BOOL symweb, const char*
  * for them. Depends on the lldb callback to enumerate modules. Not
  * necessary on dbgeng because it already downloads native symbols.
 \**********************************************************************/
-HRESULT LoadNativeSymbols()
+HRESULT LoadNativeSymbols(bool runtimeOnly)
 {
     HRESULT Status = S_OK;
 #ifdef FEATURE_PAL
@@ -767,7 +780,7 @@ HRESULT LoadNativeSymbols()
         Status = g_ExtServices->QueryInterface(__uuidof(ILLDBServices2), (void**)&services2);
         if (SUCCEEDED(Status))
         {
-            Status = services2->LoadNativeSymbols(LoadNativeSymbolsCallback);
+            Status = services2->LoadNativeSymbols(runtimeOnly, LoadNativeSymbolsCallback);
         }
     }
 #endif
index e757830f5d78fa6926e9fce641a3cfa20e590146..f6b6dfa3f490897ba0afdbcaeb148dc842f8e548 100644 (file)
@@ -49,7 +49,7 @@ extern LPCSTR GetDbiFilePath();
 extern BOOL IsHostingInitialized();
 extern HRESULT InitializeHosting();
 extern HRESULT InitializeSymbolStore(BOOL logging, BOOL msdl, BOOL symweb, const char* symbolServer, const char* cacheDirectory);
-extern HRESULT LoadNativeSymbols();
+extern HRESULT LoadNativeSymbols(bool runtimeOnly = false);
 extern void DisplaySymbolStore();
 extern void DisableSymbolStore();
 
index 754e6c812aa69cb11925c711b5b8083b1955ce27..69c78e62f7d0a353283d157425479aed92f3de87 100644 (file)
@@ -542,7 +542,7 @@ public:
         PULONG64 offset) = 0;
 };
 
-typedef void (*PFN_MODULE_LOAD_CALLBACK)(void* param, const char* moduleName, const char* moduleDirectory, ULONG64 moduleAddress, int moduleSize);
+typedef void (*PFN_MODULE_LOAD_CALLBACK)(void* param, const char* moduleFilePath, ULONG64 moduleAddress, int moduleSize);
 
 MIDL_INTERFACE("012F32F0-33BA-4E8E-BC01-037D382D8A5E")
 ILLDBServices2: public IUnknown
@@ -553,11 +553,12 @@ public:
     //----------------------------------------------------------------------------
 
     virtual HRESULT LoadNativeSymbols(
+        bool runtimeOnly,
         PFN_MODULE_LOAD_CALLBACK callback) = 0;
 
     virtual HRESULT AddModuleSymbol(
         void* param, 
-        const char* symbolFileName) = 0;
+        const char* symbolFilePath) = 0;
 };
 
 #ifdef __cplusplus
index 3f9081c1d63545bf4ff1f3996da41164693e9d67..2fe56dcdd0bcfe259a8498fefc403aedcc4a0fd3 100644 (file)
@@ -1720,49 +1720,79 @@ LLDBServices::GetFrameOffset(
 // ILLDBServices2
 //----------------------------------------------------------------------------
 
+void
+LLDBServices::LoadNativeSymbols(
+    lldb::SBTarget target,
+    lldb::SBModule module,
+    PFN_MODULE_LOAD_CALLBACK callback)
+{
+    if (module.IsValid())
+    {
+        const char* directory = nullptr;
+        const char* filename = nullptr;
+
+        lldb::SBFileSpec symbolFileSpec = module.GetSymbolFileSpec();
+        if (symbolFileSpec.IsValid())
+        {
+            directory = symbolFileSpec.GetDirectory();
+            filename = symbolFileSpec.GetFilename();
+        }
+        else {
+            lldb::SBFileSpec fileSpec = module.GetFileSpec();
+            if (fileSpec.IsValid())
+            {
+                directory = fileSpec.GetDirectory();
+                filename = fileSpec.GetFilename();
+            }
+        }
+
+        if (directory != nullptr && filename != nullptr)
+        {
+            ULONG64 moduleAddress = GetModuleBase(target, module);
+            int moduleSize = INT32_MAX;
+            if (moduleAddress != UINT64_MAX)
+            {
+                std::string path(directory);
+                path.append("/");
+                path.append(filename);
+
+                callback(&module, path.c_str(), moduleAddress, moduleSize);
+            }
+        }
+    }
+}
+
 HRESULT 
 LLDBServices::LoadNativeSymbols(
+    bool runtimeOnly,
     PFN_MODULE_LOAD_CALLBACK callback)
 {
-    uint32_t numTargets = m_debugger.GetNumTargets();
-    for (int ti = 0; ti < numTargets; ti++)
+    if (runtimeOnly)
     {
-        lldb::SBTarget target = m_debugger.GetTargetAtIndex(ti);
+        lldb::SBTarget target = m_debugger.GetSelectedTarget();
         if (target.IsValid())
         {
-            uint32_t numModules = target.GetNumModules();
-            for (int mi = 0; mi < numModules; mi++)
+            const char *coreclrModule = MAKEDLLNAME_A("coreclr");
+            lldb::SBFileSpec fileSpec;
+            fileSpec.SetFilename(coreclrModule);
+
+            lldb::SBModule module = target.FindModule(fileSpec);
+            LoadNativeSymbols(target, module, callback);
+        }
+    }
+    else 
+    {
+        uint32_t numTargets = m_debugger.GetNumTargets();
+        for (int ti = 0; ti < numTargets; ti++)
+        {
+            lldb::SBTarget target = m_debugger.GetTargetAtIndex(ti);
+            if (target.IsValid())
             {
-                lldb::SBModule module = target.GetModuleAtIndex(mi);
-                if (module.IsValid())
+                uint32_t numModules = target.GetNumModules();
+                for (int mi = 0; mi < numModules; mi++)
                 {
-                    const char* directory = nullptr;
-                    const char* filename = nullptr;
-
-                    lldb::SBFileSpec symbolFileSpec = module.GetSymbolFileSpec();
-                    if (symbolFileSpec.IsValid())
-                    {
-                        directory = symbolFileSpec.GetDirectory();
-                        filename = symbolFileSpec.GetFilename();
-                    }
-                    else {
-                        lldb::SBFileSpec fileSpec = module.GetFileSpec();
-                        if (fileSpec.IsValid())
-                        {
-                            directory = fileSpec.GetDirectory();
-                            filename = fileSpec.GetFilename();
-                        }
-                    }
-
-                    if (directory != nullptr && filename != nullptr)
-                    {
-                        ULONG64 moduleAddress = GetModuleBase(target, module);
-                        int moduleSize = INT32_MAX;
-                        if (moduleAddress != UINT64_MAX)
-                        {
-                            callback(&module, directory, filename, moduleAddress, moduleSize);
-                        }
-                    }
+                    lldb::SBModule module = target.GetModuleAtIndex(mi);
+                    LoadNativeSymbols(target, module, callback);
                 }
             }
         }
index e6d60b72156bda7a4f0088b6d5171008ec6bfd24..a02b2ba0d40765b8971067d5f94ce3c5154556db 100644 (file)
@@ -270,6 +270,7 @@ public:
     //----------------------------------------------------------------------------
 
     HRESULT LoadNativeSymbols(
+        bool runtimeOnly,
         PFN_MODULE_LOAD_CALLBACK callback);
 
     HRESULT AddModuleSymbol(
@@ -282,8 +283,10 @@ public:
 
     PCSTR GetModuleDirectory(
         PCSTR name);
+    void LoadNativeSymbols(
+        lldb::SBTarget target,
+        lldb::SBModule module,
+        PFN_MODULE_LOAD_CALLBACK callback);
 
     PCSTR GetPluginModuleDirectory();
-
-    int StartListenerThread();
 };