[Lldb-commits] [lldb] r278368 - [InstrumentationRuntime] Refactor the API (Part 2/N) (NFCI)

Vedant Kumar via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 11 10:28:37 PDT 2016


Author: vedantk
Date: Thu Aug 11 12:28:37 2016
New Revision: 278368

URL: http://llvm.org/viewvc/llvm-project?rev=278368&view=rev
Log:
[InstrumentationRuntime] Refactor the API (Part 2/N) (NFCI)

Factor out some common logic used to find the runtime library in a list
of modules.

Differential Revision: https://reviews.llvm.org/D23150

Modified:
    lldb/trunk/include/lldb/Target/InstrumentationRuntime.h
    lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp
    lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h
    lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp
    lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h
    lldb/trunk/source/Target/InstrumentationRuntime.cpp

Modified: lldb/trunk/include/lldb/Target/InstrumentationRuntime.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/InstrumentationRuntime.h?rev=278368&r1=278367&r2=278368&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/InstrumentationRuntime.h (original)
+++ lldb/trunk/include/lldb/Target/InstrumentationRuntime.h Thu Aug 11 12:28:37 2016
@@ -87,6 +87,19 @@ protected:
         m_is_active = IsActive;
     }
 
+    /// Return a regular expression which can be used to identify a valid version of the runtime library.
+    virtual const RegularExpression &
+    GetPatternForRuntimeLibrary() = 0;
+
+    /// Check whether \p module_sp corresponds to a valid runtime library.
+    virtual bool
+    CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) = 0;
+
+    /// Register a breakpoint in the runtime library and perform any other necessary initialization. The runtime library
+    /// is guaranteed to be loaded.
+    virtual void
+    Activate() = 0;
+
 public:
     
     static void
@@ -94,8 +107,8 @@ public:
 
     /// Look for the instrumentation runtime in \p module_list. Register and activate the runtime if this hasn't already
     /// been done.
-    virtual void
-    ModulesDidLoad(lldb_private::ModuleList &module_list) = 0;
+    void
+    ModulesDidLoad(lldb_private::ModuleList &module_list);
 
     bool
     IsActive() const

Modified: lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp?rev=278368&r1=278367&r2=278368&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp (original)
+++ lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp Thu Aug 11 12:28:37 2016
@@ -12,7 +12,6 @@
 #include "lldb/Breakpoint/StoppointCallbackContext.h"
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/Module.h"
-#include "lldb/Core/ModuleList.h"
 #include "lldb/Core/RegularExpression.h"
 #include "lldb/Core/PluginInterface.h"
 #include "lldb/Core/PluginManager.h"
@@ -68,46 +67,21 @@ AddressSanitizerRuntime::~AddressSanitiz
     Deactivate();
 }
 
-bool ModuleContainsASanRuntime(Module * module)
+const RegularExpression &
+AddressSanitizerRuntime::GetPatternForRuntimeLibrary()
 {
-    const Symbol* symbol = module->FindFirstSymbolWithNameAndType(
-            ConstString("__asan_get_alloc_stack"),
-            lldb::eSymbolTypeAny);
-
-    return symbol != nullptr;
+    // FIXME: This shouldn't include the "dylib" suffix.
+    static RegularExpression regex("libclang_rt.asan_(.*)_dynamic\\.dylib");
+    return regex;
 }
 
-void
-AddressSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
+bool
+AddressSanitizerRuntime::CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp)
 {
-    if (IsActive())
-        return;
-    
-    if (GetRuntimeModuleSP()) {
-        Activate();
-        return;
-    }
-
-    std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
-    const size_t num_modules = module_list.GetSize();
-    for (size_t i = 0; i < num_modules; ++i)
-    {
-        Module *module_pointer = module_list.GetModulePointerAtIndexUnlocked(i);
-        const FileSpec & file_spec = module_pointer->GetFileSpec();
-        if (! file_spec)
-            continue;
-        
-        static RegularExpression g_asan_runtime_regex("libclang_rt.asan_(.*)_dynamic\\.dylib");
-        if (g_asan_runtime_regex.Execute (file_spec.GetFilename().GetCString()) || module_pointer->IsExecutable())
-        {
-            if (ModuleContainsASanRuntime(module_pointer))
-            {
-                SetRuntimeModuleSP(module_pointer->shared_from_this());
-                Activate();
-                return;
-            }
-        }
-    }
+    const Symbol *symbol =
+        module_sp->FindFirstSymbolWithNameAndType(ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);
+
+    return symbol != nullptr;
 }
 
 #define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000

Modified: lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h?rev=278368&r1=278367&r2=278368&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h (original)
+++ lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h Thu Aug 11 12:28:37 2016
@@ -56,15 +56,18 @@ public:
         return 1;
     }
     
-    void
-    ModulesDidLoad(lldb_private::ModuleList &module_list) override;
-    
 private:
     AddressSanitizerRuntime(const lldb::ProcessSP &process_sp) : lldb_private::InstrumentationRuntime(process_sp) {}
 
+    const RegularExpression &
+    GetPatternForRuntimeLibrary() override;
+
+    bool
+    CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override;
+
     void
-    Activate();
-    
+    Activate() override;
+
     void
     Deactivate();
     

Modified: lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp?rev=278368&r1=278367&r2=278368&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp (original)
+++ lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp Thu Aug 11 12:28:37 2016
@@ -12,7 +12,6 @@
 #include "lldb/Breakpoint/StoppointCallbackContext.h"
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/Module.h"
-#include "lldb/Core/ModuleList.h"
 #include "lldb/Core/RegularExpression.h"
 #include "lldb/Core/PluginInterface.h"
 #include "lldb/Core/PluginManager.h"
@@ -73,46 +72,6 @@ ThreadSanitizerRuntime::~ThreadSanitizer
     Deactivate();
 }
 
-static bool
-ModuleContainsTSanRuntime(ModuleSP module_sp)
-{
-    static ConstString g_tsan_get_current_report("__tsan_get_current_report");
-    const Symbol* symbol = module_sp->FindFirstSymbolWithNameAndType(g_tsan_get_current_report, lldb::eSymbolTypeAny);
-    return symbol != nullptr;
-}
-
-void
-ThreadSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
-{
-    if (IsActive())
-        return;
-    
-    if (GetRuntimeModuleSP()) {
-        Activate();
-        return;
-    }
-    
-    module_list.ForEach ([this](const lldb::ModuleSP module_sp) -> bool
-    {
-        const FileSpec & file_spec = module_sp->GetFileSpec();
-        if (! file_spec)
-            return true; // Keep iterating through modules
-        
-        llvm::StringRef module_basename(file_spec.GetFilename().GetStringRef());
-        if (module_sp->IsExecutable() || module_basename.startswith("libclang_rt.tsan_"))
-        {
-            if (ModuleContainsTSanRuntime(module_sp))
-            {
-                SetRuntimeModuleSP(module_sp);
-                Activate();
-                return false; // Stop iterating
-            }
-        }
-
-        return true; // Keep iterating through modules
-    });
-}
-
 #define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000
 
 const char *
@@ -713,6 +672,20 @@ ThreadSanitizerRuntime::NotifyBreakpoint
         return false;   // Let target run
 }
 
+const RegularExpression &
+ThreadSanitizerRuntime::GetPatternForRuntimeLibrary() {
+  static RegularExpression regex("libclang_rt.tsan_");
+  return regex;
+}
+
+bool
+ThreadSanitizerRuntime::CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp)
+{
+    static ConstString g_tsan_get_current_report("__tsan_get_current_report");
+    const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(g_tsan_get_current_report, lldb::eSymbolTypeAny);
+    return symbol != nullptr;
+}
+
 void
 ThreadSanitizerRuntime::Activate()
 {
@@ -768,7 +741,6 @@ ThreadSanitizerRuntime::Deactivate()
     }
     SetActive(false);
 }
-
 static std::string
 GenerateThreadName(const std::string &path, StructuredData::Object *o, StructuredData::ObjectSP main_info) {
     std::string result = "additional information";

Modified: lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h?rev=278368&r1=278367&r2=278368&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h (original)
+++ lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h Thu Aug 11 12:28:37 2016
@@ -56,17 +56,20 @@ public:
         return 1;
     }
     
-    void
-    ModulesDidLoad(lldb_private::ModuleList &module_list) override;
-    
     lldb::ThreadCollectionSP
     GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override;
     
 private:
     ThreadSanitizerRuntime(const lldb::ProcessSP &process_sp) : lldb_private::InstrumentationRuntime(process_sp) {}
 
+    const RegularExpression &
+    GetPatternForRuntimeLibrary() override;
+
+    bool
+    CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override;
+
     void
-    Activate();
+    Activate() override;
     
     void
     Deactivate();

Modified: lldb/trunk/source/Target/InstrumentationRuntime.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/InstrumentationRuntime.cpp?rev=278368&r1=278367&r2=278368&view=diff
==============================================================================
--- lldb/trunk/source/Target/InstrumentationRuntime.cpp (original)
+++ lldb/trunk/source/Target/InstrumentationRuntime.cpp Thu Aug 11 12:28:37 2016
@@ -13,7 +13,10 @@
 // Project includes
 #include "lldb/lldb-private.h"
 #include "lldb/Target/Process.h"
+#include "lldb/Core/Module.h"
+#include "lldb/Core/ModuleList.h"
 #include "lldb/Core/PluginManager.h"
+#include "lldb/Core/RegularExpression.h"
 #include "lldb/Target/InstrumentationRuntime.h"
 
 using namespace lldb;
@@ -40,6 +43,38 @@ InstrumentationRuntime::ModulesDidLoad(l
     }
 }
 
+void
+InstrumentationRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
+{
+    if (IsActive())
+        return;
+
+    if (GetRuntimeModuleSP())
+    {
+        Activate();
+        return;
+    }
+
+    module_list.ForEach([this](const lldb::ModuleSP module_sp) -> bool {
+        const FileSpec &file_spec = module_sp->GetFileSpec();
+        if (!file_spec)
+            return true; // Keep iterating.
+
+        const RegularExpression &runtime_regex = GetPatternForRuntimeLibrary();
+        if (runtime_regex.Execute(file_spec.GetFilename().GetCString()) || module_sp->IsExecutable())
+        {
+            if (CheckIfRuntimeIsValid(module_sp))
+            {
+                SetRuntimeModuleSP(module_sp);
+                Activate();
+                return false; // Stop iterating, we're done.
+            }
+        }
+
+        return true;
+    });
+}
+
 lldb::ThreadCollectionSP
 InstrumentationRuntime::GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info)
 {




More information about the lldb-commits mailing list