[Lldb-commits] [lldb] c9cf394 - [lldb] Replace NativeProcess delegate list with a single delegate

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Tue Apr 13 00:50:00 PDT 2021


Author: Pavel Labath
Date: 2021-04-13T09:49:38+02:00
New Revision: c9cf394f796e181b9ee3bc9b69d84fbbba2fe45c

URL: https://github.com/llvm/llvm-project/commit/c9cf394f796e181b9ee3bc9b69d84fbbba2fe45c
DIFF: https://github.com/llvm/llvm-project/commit/c9cf394f796e181b9ee3bc9b69d84fbbba2fe45c.diff

LOG: [lldb] Replace NativeProcess delegate list with a single delegate

In all this time, we've never used more than one delegate. The logic to
support multiple delegates is therefore untested, and becomes
particularly unwieldy once we need to support multiple processes.

Just remove it.

Added: 
    

Modified: 
    lldb/include/lldb/Host/common/NativeProcessProtocol.h
    lldb/source/Host/common/NativeProcessProtocol.cpp
    lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
    lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
    lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Host/common/NativeProcessProtocol.h b/lldb/include/lldb/Host/common/NativeProcessProtocol.h
index 36b22d72b1962..72ac05509936d 100644
--- a/lldb/include/lldb/Host/common/NativeProcessProtocol.h
+++ b/lldb/include/lldb/Host/common/NativeProcessProtocol.h
@@ -222,36 +222,6 @@ class NativeProcessProtocol {
     virtual void DidExec(NativeProcessProtocol *process) = 0;
   };
 
-  /// Register a native delegate.
-  ///
-  /// Clients can register nofication callbacks by passing in a
-  /// NativeDelegate impl and passing it into this function.
-  ///
-  /// Note: it is required that the lifetime of the
-  /// native_delegate outlive the NativeProcessProtocol.
-  ///
-  /// \param[in] native_delegate
-  ///     A NativeDelegate impl to be called when certain events
-  ///     happen within the NativeProcessProtocol or related threads.
-  ///
-  /// \return
-  ///     true if the delegate was registered successfully;
-  ///     false if the delegate was already registered.
-  ///
-  /// \see NativeProcessProtocol::NativeDelegate.
-  bool RegisterNativeDelegate(NativeDelegate &native_delegate);
-
-  /// Unregister a native delegate previously registered.
-  ///
-  /// \param[in] native_delegate
-  ///     A NativeDelegate impl previously registered with this process.
-  ///
-  /// \return Returns \b true if the NativeDelegate was
-  /// successfully removed from the process, \b false otherwise.
-  ///
-  /// \see NativeProcessProtocol::NativeDelegate
-  bool UnregisterNativeDelegate(NativeDelegate &native_delegate);
-
   virtual Status GetLoadedModuleFileSpec(const char *module_path,
                                          FileSpec &file_spec) = 0;
 
@@ -377,8 +347,7 @@ class NativeProcessProtocol {
 
   llvm::Optional<WaitStatus> m_exit_status;
 
-  std::recursive_mutex m_delegates_mutex;
-  std::vector<NativeDelegate *> m_delegates;
+  NativeDelegate &m_delegate;
   NativeWatchpointList m_watchpoint_list;
   HardwareBreakpointMap m_hw_breakpoints_map;
   int m_terminal_fd;

diff  --git a/lldb/source/Host/common/NativeProcessProtocol.cpp b/lldb/source/Host/common/NativeProcessProtocol.cpp
index 070fda664678a..d15bb21e8e6db 100644
--- a/lldb/source/Host/common/NativeProcessProtocol.cpp
+++ b/lldb/source/Host/common/NativeProcessProtocol.cpp
@@ -25,10 +25,8 @@ using namespace lldb_private;
 
 NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd,
                                              NativeDelegate &delegate)
-    : m_pid(pid), m_terminal_fd(terminal_fd) {
-  bool registered = RegisterNativeDelegate(delegate);
-  assert(registered);
-  (void)registered;
+    : m_pid(pid), m_delegate(delegate), m_terminal_fd(terminal_fd) {
+  delegate.InitializeDelegate(this);
 }
 
 lldb_private::Status NativeProcessProtocol::Interrupt() {
@@ -295,64 +293,21 @@ Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) {
   return error;
 }
 
-bool NativeProcessProtocol::RegisterNativeDelegate(
-    NativeDelegate &native_delegate) {
-  std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
-  if (llvm::is_contained(m_delegates, &native_delegate))
-    return false;
-
-  m_delegates.push_back(&native_delegate);
-  native_delegate.InitializeDelegate(this);
-  return true;
-}
-
-bool NativeProcessProtocol::UnregisterNativeDelegate(
-    NativeDelegate &native_delegate) {
-  std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
-
-  const auto initial_size = m_delegates.size();
-  m_delegates.erase(
-      remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
-      m_delegates.end());
-
-  // We removed the delegate if the count of delegates shrank after removing
-  // all copies of the given native_delegate from the vector.
-  return m_delegates.size() < initial_size;
-}
-
 void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
     lldb::StateType state) {
   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
 
-  std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
-  for (auto native_delegate : m_delegates)
-    native_delegate->ProcessStateChanged(this, state);
+  m_delegate.ProcessStateChanged(this, state);
 
-  if (log) {
-    if (!m_delegates.empty()) {
-      LLDB_LOGF(log,
-                "NativeProcessProtocol::%s: sent state notification [%s] "
-                "from process %" PRIu64,
-                __FUNCTION__, lldb_private::StateAsCString(state), GetID());
-    } else {
-      LLDB_LOGF(log,
-                "NativeProcessProtocol::%s: would send state notification "
-                "[%s] from process %" PRIu64 ", but no delegates",
-                __FUNCTION__, lldb_private::StateAsCString(state), GetID());
-    }
-  }
+  LLDB_LOG(log, "sent state notification [{0}] from process {1}", state,
+           GetID());
 }
 
 void NativeProcessProtocol::NotifyDidExec() {
   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
-  LLDB_LOGF(log, "NativeProcessProtocol::%s - preparing to call delegates",
-            __FUNCTION__);
+  LLDB_LOG(log, "process {0} exec()ed", GetID());
 
-  {
-    std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
-    for (auto native_delegate : m_delegates)
-      native_delegate->DidExec(this);
-  }
+  m_delegate.DidExec(this);
 }
 
 Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,

diff  --git a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
index c55a66474e66c..5e371672eaf27 100644
--- a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
@@ -957,7 +957,7 @@ void NativeProcessFreeBSD::MonitorClone(::pid_t child_pid) {
 
   MainLoop unused_loop;
   NativeProcessFreeBSD child_process{static_cast<::pid_t>(child_pid),
-                                     m_terminal_fd, *m_delegates[0], m_arch,
+                                     m_terminal_fd, m_delegate, m_arch,
                                      unused_loop};
   child_process.ReinitializeThreads();
   auto *child_thread =

diff  --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
index 418a7a39be2a1..4880d4dc5d81f 100644
--- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
+++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
@@ -917,7 +917,7 @@ bool NativeProcessLinux::MonitorClone(
     MainLoop unused_loop;
     NativeProcessLinux child_process{static_cast<::pid_t>(child_pid),
                                      m_terminal_fd,
-                                     *m_delegates[0],
+                                     m_delegate,
                                      m_arch,
                                      unused_loop,
                                      {static_cast<::pid_t>(child_pid)}};

diff  --git a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
index 957682a2d266d..1f357e3e96d79 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
+++ b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
@@ -1018,7 +1018,7 @@ void NativeProcessNetBSD::MonitorClone(::pid_t child_pid) {
 
   MainLoop unused_loop;
   NativeProcessNetBSD child_process{static_cast<::pid_t>(child_pid),
-                                    m_terminal_fd, *m_delegates[0], m_arch,
+                                    m_terminal_fd, m_delegate, m_arch,
                                     unused_loop};
   child_process.Detach();
   Status pt_error =


        


More information about the lldb-commits mailing list