|
| 1 | +//===-- ThreadPlanSingleThreadTimeout.h -------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLDB_TARGET_THREADPLANSINGLETHREADTIMEOUT_H |
| 10 | +#define LLDB_TARGET_THREADPLANSINGLETHREADTIMEOUT_H |
| 11 | + |
| 12 | +#include "lldb/Target/Thread.h" |
| 13 | +#include "lldb/Target/ThreadPlan.h" |
| 14 | +#include "lldb/Utility/Event.h" |
| 15 | +#include "lldb/Utility/LLDBLog.h" |
| 16 | +#include "lldb/Utility/State.h" |
| 17 | + |
| 18 | +#include <chrono> |
| 19 | +#include <thread> |
| 20 | + |
| 21 | +namespace lldb_private { |
| 22 | + |
| 23 | +// |
| 24 | +// Thread plan used by single thread execution to issue timeout. This is useful |
| 25 | +// to detect potential deadlock in single thread execution. The timeout measures |
| 26 | +// the elapsed time from the last internal stop and gets reset by each internal |
| 27 | +// stop to ensure we are accurately detecting execution not moving forward. |
| 28 | +// This means this thread plan may be created/destroyed multiple times by the |
| 29 | +// parent execution plan. |
| 30 | +// |
| 31 | +// When a timeout happens, the thread plan resolves the potential deadlock by |
| 32 | +// issuing a thread specific async interrupt to enter stop state, then execution |
| 33 | +// is resumed with all threads running to resolve the potential deadlock |
| 34 | +// |
| 35 | +class ThreadPlanSingleThreadTimeout : public ThreadPlan { |
| 36 | + enum class State { |
| 37 | + WaitTimeout, // Waiting for timeout. |
| 38 | + AsyncInterrupt, // Async interrupt has been issued. |
| 39 | + Done, // Finished resume all threads. |
| 40 | + }; |
| 41 | + |
| 42 | +public: |
| 43 | + // TODO: allow timeout to be set on per thread plan basis. |
| 44 | + struct TimeoutInfo { |
| 45 | + // Whether there is a ThreadPlanSingleThreadTimeout instance alive. |
| 46 | + bool m_isAlive = false; |
| 47 | + ThreadPlanSingleThreadTimeout::State m_last_state = State::WaitTimeout; |
| 48 | + }; |
| 49 | + |
| 50 | + ~ThreadPlanSingleThreadTimeout() override; |
| 51 | + |
| 52 | + // If input \param thread is running in single thread mode, push a |
| 53 | + // new ThreadPlanSingleThreadTimeout based on timeout setting from fresh new |
| 54 | + // state. The reference of \param info is passed in so that when |
| 55 | + // ThreadPlanSingleThreadTimeout got popped its last state can be stored |
| 56 | + // in it for future resume. |
| 57 | + static void PushNewWithTimeout(Thread &thread, TimeoutInfo &info); |
| 58 | + |
| 59 | + // Push a new ThreadPlanSingleThreadTimeout by restoring state from |
| 60 | + // input \param info and resume execution. |
| 61 | + static void ResumeFromPrevState(Thread &thread, TimeoutInfo &info); |
| 62 | + |
| 63 | + void GetDescription(Stream *s, lldb::DescriptionLevel level) override; |
| 64 | + bool ValidatePlan(Stream *error) override { return true; } |
| 65 | + bool WillStop() override; |
| 66 | + void DidPop() override; |
| 67 | + |
| 68 | + bool IsLeafPlan() override { return true; } |
| 69 | + bool DoPlanExplainsStop(Event *event_ptr) override; |
| 70 | + |
| 71 | + lldb::StateType GetPlanRunState() override; |
| 72 | + static void TimeoutThreadFunc(ThreadPlanSingleThreadTimeout *self); |
| 73 | + |
| 74 | + bool MischiefManaged() override; |
| 75 | + |
| 76 | + bool ShouldStop(Event *event_ptr) override; |
| 77 | + void SetStopOthers(bool new_value) override; |
| 78 | + bool StopOthers() override; |
| 79 | + |
| 80 | +private: |
| 81 | + ThreadPlanSingleThreadTimeout(Thread &thread, TimeoutInfo &info); |
| 82 | + |
| 83 | + bool IsTimeoutAsyncInterrupt(Event *event_ptr); |
| 84 | + bool HandleEvent(Event *event_ptr); |
| 85 | + void HandleTimeout(); |
| 86 | + uint64_t GetRemainingTimeoutMilliSeconds(); |
| 87 | + |
| 88 | + static std::string StateToString(State state); |
| 89 | + |
| 90 | + ThreadPlanSingleThreadTimeout(const ThreadPlanSingleThreadTimeout &) = delete; |
| 91 | + const ThreadPlanSingleThreadTimeout & |
| 92 | + operator=(const ThreadPlanSingleThreadTimeout &) = delete; |
| 93 | + |
| 94 | + TimeoutInfo &m_info; // Reference to controlling ThreadPlan's TimeoutInfo. |
| 95 | + State m_state; |
| 96 | + |
| 97 | + // Lock for m_wakeup_cv and m_exit_flag between thread plan thread and timer |
| 98 | + // thread |
| 99 | + std::mutex m_mutex; |
| 100 | + std::condition_variable m_wakeup_cv; |
| 101 | + std::thread m_timer_thread; |
| 102 | + std::chrono::steady_clock::time_point m_timeout_start; |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace lldb_private |
| 106 | + |
| 107 | +#endif // LLDB_TARGET_THREADPLANSINGLETHREADTIMEOUT_H |
0 commit comments