[Lldb-commits] [lldb] r288241 - Convert most of the Process class to Timeout<>

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 30 03:56:32 PST 2016


Author: labath
Date: Wed Nov 30 05:56:32 2016
New Revision: 288241

URL: http://llvm.org/viewvc/llvm-project?rev=288241&view=rev
Log:
Convert most of the Process class to Timeout<>

This changes most of the class to use the new Timeout class. The one function
left is RunThreadPlan, which I left for a separate change as the function is
massive. A couple of things to call out:
- I've renamed the affected functions to match the listener interface names. This
  should also help catch any places I did not convert at compile time.
- I've deleted the WaitForState function as it was unused.

Modified:
    lldb/trunk/include/lldb/Target/Process.h
    lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp
    lldb/trunk/source/Target/Process.cpp
    lldb/trunk/source/Target/Target.cpp

Modified: lldb/trunk/include/lldb/Target/Process.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Process.h?rev=288241&r1=288240&r2=288241&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Process.h (original)
+++ lldb/trunk/include/lldb/Target/Process.h Wed Nov 30 05:56:32 2016
@@ -2415,7 +2415,7 @@ public:
   // false
   // will avoid this behavior.
   lldb::StateType
-  WaitForProcessToStop(const std::chrono::microseconds &timeout,
+  WaitForProcessToStop(const Timeout<std::micro> &timeout,
                        lldb::EventSP *event_sp_ptr = nullptr,
                        bool wait_always = true,
                        lldb::ListenerSP hijack_listener = lldb::ListenerSP(),
@@ -2436,8 +2436,8 @@ public:
   //--------------------------------------------------------------------------------------
   void SyncIOHandler(uint32_t iohandler_id, uint64_t timeout_msec);
 
-  lldb::StateType WaitForStateChangedEvents(
-      const std::chrono::microseconds &timeout, lldb::EventSP &event_sp,
+  lldb::StateType GetStateChangedEvents(
+      lldb::EventSP &event_sp, const Timeout<std::micro> &timeout,
       lldb::ListenerSP
           hijack_listener); // Pass an empty ListenerSP to use builtin listener
 
@@ -3105,24 +3105,19 @@ protected:
 
   Error HaltPrivate();
 
-  lldb::StateType
-  WaitForProcessStopPrivate(const std::chrono::microseconds &timeout,
-                            lldb::EventSP &event_sp);
+  lldb::StateType WaitForProcessStopPrivate(lldb::EventSP &event_sp,
+                                            const Timeout<std::micro> &timeout);
 
   // This waits for both the state change broadcaster, and the control
   // broadcaster.
   // If control_only, it only waits for the control broadcaster.
 
-  bool WaitForEventsPrivate(const std::chrono::microseconds &timeout,
-                            lldb::EventSP &event_sp, bool control_only);
+  bool GetEventsPrivate(lldb::EventSP &event_sp,
+                        const Timeout<std::micro> &timeout, bool control_only);
 
   lldb::StateType
-  WaitForStateChangedEventsPrivate(const std::chrono::microseconds &timeout,
-                                   lldb::EventSP &event_sp);
-
-  lldb::StateType WaitForState(const std::chrono::microseconds &timeout,
-                               const lldb::StateType *match_states,
-                               const uint32_t num_match_states);
+  GetStateChangedEventsPrivate(lldb::EventSP &event_sp,
+                               const Timeout<std::micro> &timeout);
 
   size_t WriteMemoryPrivate(lldb::addr_t addr, const void *buf, size_t size,
                             Error &error);

Modified: lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp?rev=288241&r1=288240&r2=288241&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp Wed Nov 30 05:56:32 2016
@@ -644,7 +644,7 @@ PlatformLinux::DebugProcess(ProcessLaunc
     // Handle the hijacking of process events.
     if (listener_sp) {
       const StateType state = process_sp->WaitForProcessToStop(
-          std::chrono::microseconds(0), NULL, false, listener_sp);
+          llvm::None, NULL, false, listener_sp);
 
       if (state == eStateStopped) {
         if (log)

Modified: lldb/trunk/source/Target/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Process.cpp?rev=288241&r1=288240&r2=288241&view=diff
==============================================================================
--- lldb/trunk/source/Target/Process.cpp (original)
+++ lldb/trunk/source/Target/Process.cpp Wed Nov 30 05:56:32 2016
@@ -67,6 +67,7 @@
 
 using namespace lldb;
 using namespace lldb_private;
+using namespace std::chrono;
 
 // A temporary function to convert between old representations of timeouts (0
 // means infinite wait) and new Timeout class (0 means "poll").
@@ -971,11 +972,10 @@ void Process::SyncIOHandler(uint32_t ioh
                 __FUNCTION__, iohandler_id, new_iohandler_id);
 }
 
-StateType
-Process::WaitForProcessToStop(const std::chrono::microseconds &timeout,
-                              EventSP *event_sp_ptr, bool wait_always,
-                              ListenerSP hijack_listener_sp, Stream *stream,
-                              bool use_run_lock) {
+StateType Process::WaitForProcessToStop(const Timeout<std::micro> &timeout,
+                                        EventSP *event_sp_ptr, bool wait_always,
+                                        ListenerSP hijack_listener_sp,
+                                        Stream *stream, bool use_run_lock) {
   // We can't just wait for a "stopped" event, because the stopped event may
   // have restarted the target.
   // We have to actually check each event, and in the case of a stopped event
@@ -991,8 +991,9 @@ Process::WaitForProcessToStop(const std:
 
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
   if (log)
-    log->Printf("Process::%s (timeout = %llu)", __FUNCTION__,
-                static_cast<unsigned long long>(timeout.count()));
+    log->Printf(
+        "Process::%s (timeout = %llu)", __FUNCTION__,
+        static_cast<unsigned long long>(timeout ? timeout->count() : -1));
 
   if (!wait_always && StateIsStoppedState(state, true) &&
       StateIsStoppedState(GetPrivateState(), true)) {
@@ -1009,7 +1010,7 @@ Process::WaitForProcessToStop(const std:
 
   while (state != eStateInvalid) {
     EventSP event_sp;
-    state = WaitForStateChangedEvents(timeout, event_sp, hijack_listener_sp);
+    state = GetStateChangedEvents(event_sp, timeout, hijack_listener_sp);
     if (event_sp_ptr && event_sp)
       *event_sp_ptr = event_sp;
 
@@ -1254,27 +1255,6 @@ bool Process::HandleProcessStateChangedE
   return true;
 }
 
-StateType Process::WaitForState(const std::chrono::microseconds &timeout,
-                                const StateType *match_states,
-                                const uint32_t num_match_states) {
-  EventSP event_sp;
-  StateType state = GetState();
-  while (state != eStateInvalid) {
-    // If we are exited or detached, we won't ever get back to any
-    // other valid state...
-    if (state == eStateDetached || state == eStateExited)
-      return state;
-
-    state = WaitForStateChangedEvents(timeout, event_sp, nullptr);
-
-    for (uint32_t i = 0; i < num_match_states; ++i) {
-      if (match_states[i] == state)
-        return state;
-    }
-  }
-  return state;
-}
-
 bool Process::HijackProcessEvents(ListenerSP listener_sp) {
   if (listener_sp) {
     return HijackBroadcaster(listener_sp, eBroadcastBitStateChanged |
@@ -1285,15 +1265,15 @@ bool Process::HijackProcessEvents(Listen
 
 void Process::RestoreProcessEvents() { RestoreBroadcaster(); }
 
-StateType
-Process::WaitForStateChangedEvents(const std::chrono::microseconds &timeout,
-                                   EventSP &event_sp,
-                                   ListenerSP hijack_listener_sp) {
+StateType Process::GetStateChangedEvents(EventSP &event_sp,
+                                         const Timeout<std::micro> &timeout,
+                                         ListenerSP hijack_listener_sp) {
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
 
   if (log)
-    log->Printf("Process::%s (timeout = %llu, event_sp)...", __FUNCTION__,
-                static_cast<unsigned long long>(timeout.count()));
+    log->Printf(
+        "Process::%s (timeout = %llu, event_sp)...", __FUNCTION__,
+        static_cast<unsigned long long>(timeout ? timeout->count() : -1));
 
   ListenerSP listener_sp = hijack_listener_sp;
   if (!listener_sp)
@@ -1302,7 +1282,7 @@ Process::WaitForStateChangedEvents(const
   StateType state = eStateInvalid;
   if (listener_sp->GetEventForBroadcasterWithType(
           this, eBroadcastBitStateChanged | eBroadcastBitInterrupt, event_sp,
-          ConvertTimeout(timeout))) {
+          timeout)) {
     if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
       state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
     else if (log)
@@ -1310,9 +1290,10 @@ Process::WaitForStateChangedEvents(const
   }
 
   if (log)
-    log->Printf("Process::%s (timeout = %llu, event_sp) => %s", __FUNCTION__,
-                static_cast<unsigned long long>(timeout.count()),
-                StateAsCString(state));
+    log->Printf(
+        "Process::%s (timeout = %llu, event_sp) => %s", __FUNCTION__,
+        static_cast<unsigned long long>(timeout ? timeout->count() : -1),
+        StateAsCString(state));
   return state;
 }
 
@@ -1337,19 +1318,21 @@ Event *Process::PeekAtStateChangedEvents
   return event_ptr;
 }
 
-StateType Process::WaitForStateChangedEventsPrivate(
-    const std::chrono::microseconds &timeout, EventSP &event_sp) {
+StateType
+Process::GetStateChangedEventsPrivate(EventSP &event_sp,
+                                      const Timeout<std::micro> &timeout) {
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
 
   if (log)
-    log->Printf("Process::%s (timeout = %llu, event_sp)...", __FUNCTION__,
-                static_cast<unsigned long long>(timeout.count()));
+    log->Printf(
+        "Process::%s (timeout = %llu, event_sp)...", __FUNCTION__,
+        static_cast<unsigned long long>(timeout ? timeout->count() : -1));
 
   StateType state = eStateInvalid;
   if (m_private_state_listener_sp->GetEventForBroadcasterWithType(
           &m_private_state_broadcaster,
           eBroadcastBitStateChanged | eBroadcastBitInterrupt, event_sp,
-          ConvertTimeout(timeout)))
+          timeout))
     if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
       state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
 
@@ -1357,27 +1340,28 @@ StateType Process::WaitForStateChangedEv
   // to the command-line, and that could disable the log, which would render the
   // log we got above invalid.
   if (log)
-    log->Printf("Process::%s (timeout = %llu, event_sp) => %s", __FUNCTION__,
-                static_cast<unsigned long long>(timeout.count()),
-                state == eStateInvalid ? "TIMEOUT" : StateAsCString(state));
+    log->Printf(
+        "Process::%s (timeout = %llu, event_sp) => %s", __FUNCTION__,
+        static_cast<unsigned long long>(timeout ? timeout->count() : -1),
+        state == eStateInvalid ? "TIMEOUT" : StateAsCString(state));
   return state;
 }
 
-bool Process::WaitForEventsPrivate(const std::chrono::microseconds &timeout,
-                                   EventSP &event_sp, bool control_only) {
+bool Process::GetEventsPrivate(EventSP &event_sp,
+                               const Timeout<std::micro> &timeout,
+                               bool control_only) {
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
 
   if (log)
-    log->Printf("Process::%s (timeout = %llu, event_sp)...", __FUNCTION__,
-                static_cast<unsigned long long>(timeout.count()));
+    log->Printf(
+        "Process::%s (timeout = %llu, event_sp)...", __FUNCTION__,
+        static_cast<unsigned long long>(timeout ? timeout->count() : -1));
 
   if (control_only)
     return m_private_state_listener_sp->GetEventForBroadcaster(
-        &m_private_state_control_broadcaster, event_sp,
-        ConvertTimeout(timeout));
+        &m_private_state_control_broadcaster, event_sp, timeout);
   else
-    return m_private_state_listener_sp->GetEvent(event_sp,
-                                                 ConvertTimeout(timeout));
+    return m_private_state_listener_sp->GetEvent(event_sp, timeout);
 }
 
 bool Process::IsRunning() const {
@@ -1688,8 +1672,8 @@ Error Process::ResumeSynchronous(Stream
 
   Error error = PrivateResume();
   if (error.Success()) {
-    StateType state = WaitForProcessToStop(std::chrono::microseconds(0), NULL,
-                                           true, listener_sp, stream);
+    StateType state =
+        WaitForProcessToStop(llvm::None, NULL, true, listener_sp, stream);
     const bool must_be_alive =
         false; // eStateExited is ok, so this must be false
     if (!StateIsStoppedState(state, must_be_alive))
@@ -2718,14 +2702,14 @@ Error Process::DisableWatchpoint(Watchpo
 }
 
 StateType
-Process::WaitForProcessStopPrivate(const std::chrono::microseconds &timeout,
-                                   EventSP &event_sp) {
+Process::WaitForProcessStopPrivate(EventSP &event_sp,
+                                   const Timeout<std::micro> &timeout) {
   StateType state;
   // Now wait for the process to launch and return control to us, and then
   // call DidLaunch:
   while (true) {
     event_sp.reset();
-    state = WaitForStateChangedEventsPrivate(timeout, event_sp);
+    state = GetStateChangedEventsPrivate(event_sp, timeout);
 
     if (StateIsStoppedState(state, false))
       break;
@@ -2801,8 +2785,7 @@ Error Process::Launch(ProcessLaunchInfo
           }
         } else {
           EventSP event_sp;
-          StateType state =
-              WaitForProcessStopPrivate(std::chrono::seconds(10), event_sp);
+          StateType state = WaitForProcessStopPrivate(event_sp, seconds(10));
 
           if (state == eStateInvalid || !event_sp) {
             // We were able to launch the process, but we failed to
@@ -3271,8 +3254,7 @@ Error Process::ConnectRemote(Stream *str
   if (error.Success()) {
     if (GetID() != LLDB_INVALID_PROCESS_ID) {
       EventSP event_sp;
-      StateType state =
-          WaitForProcessStopPrivate(std::chrono::microseconds(0), event_sp);
+      StateType state = WaitForProcessStopPrivate(event_sp, llvm::None);
 
       if (state == eStateStopped || state == eStateCrashed) {
         // If we attached and actually have a process on the other end, then
@@ -3374,9 +3356,8 @@ Error Process::Halt(bool clear_thread_pl
   }
 
   // Wait for 10 second for the process to stop.
-  StateType state =
-      WaitForProcessToStop(std::chrono::seconds(10), &event_sp, true,
-                           halt_listener_sp, nullptr, use_run_lock);
+  StateType state = WaitForProcessToStop(
+      seconds(10), &event_sp, true, halt_listener_sp, nullptr, use_run_lock);
   RestoreProcessEvents();
 
   if (state == eStateInvalid || !event_sp) {
@@ -3408,8 +3389,8 @@ Error Process::StopForDestroyOrDetach(ll
     SendAsyncInterrupt();
 
     // Consume the interrupt event.
-    StateType state = WaitForProcessToStop(std::chrono::seconds(10),
-                                           &exit_event_sp, true, listener_sp);
+    StateType state =
+        WaitForProcessToStop(seconds(10), &exit_event_sp, true, listener_sp);
 
     RestoreProcessEvents();
 
@@ -4038,7 +4019,7 @@ thread_result_t Process::RunPrivateState
   bool interrupt_requested = false;
   while (!exit_now) {
     EventSP event_sp;
-    WaitForEventsPrivate(std::chrono::microseconds(0), event_sp, control_only);
+    GetEventsPrivate(event_sp, llvm::None, control_only);
     if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) {
       if (log)
         log->Printf("Process::%s (arg = %p, pid = %" PRIu64

Modified: lldb/trunk/source/Target/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Target.cpp?rev=288241&r1=288240&r2=288241&view=diff
==============================================================================
--- lldb/trunk/source/Target/Target.cpp (original)
+++ lldb/trunk/source/Target/Target.cpp Wed Nov 30 05:56:32 2016
@@ -2907,8 +2907,7 @@ Error Target::Launch(ProcessLaunchInfo &
       }
 
       StateType state = m_process_sp->WaitForProcessToStop(
-          std::chrono::microseconds(0), nullptr, false, hijack_listener_sp,
-          nullptr);
+          llvm::None, nullptr, false, hijack_listener_sp, nullptr);
 
       if (state == eStateStopped) {
         if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) {
@@ -2916,8 +2915,7 @@ Error Target::Launch(ProcessLaunchInfo &
             error = m_process_sp->PrivateResume();
             if (error.Success()) {
               state = m_process_sp->WaitForProcessToStop(
-                  std::chrono::microseconds(0), nullptr, true,
-                  hijack_listener_sp, stream);
+                  llvm::None, nullptr, true, hijack_listener_sp, stream);
               const bool must_be_alive =
                   false; // eStateExited is ok, so this must be false
               if (!StateIsStoppedState(state, must_be_alive)) {
@@ -3041,8 +3039,7 @@ Error Target::Attach(ProcessAttachInfo &
       process_sp->RestoreProcessEvents();
     } else {
       state = process_sp->WaitForProcessToStop(
-          std::chrono::microseconds(0), nullptr, false,
-          attach_info.GetHijackListener(), stream);
+          llvm::None, nullptr, false, attach_info.GetHijackListener(), stream);
       process_sp->RestoreProcessEvents();
 
       if (state != eStateStopped) {




More information about the lldb-commits mailing list