LLDB mainline
ThreadPlanSingleThreadTimeout.cpp
Go to the documentation of this file.
1//===-- ThreadPlanStepOverRange.cpp ---------------------------------------===//
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
10#include "lldb/Symbol/Block.h"
14#include "lldb/Target/Process.h"
16#include "lldb/Target/Target.h"
17#include "lldb/Target/Thread.h"
21#include "lldb/Utility/Log.h"
22#include "lldb/Utility/Stream.h"
23
24using namespace lldb_private;
25using namespace lldb;
26
28 Thread &thread, TimeoutInfoSP &info)
29 : ThreadPlan(ThreadPlan::eKindSingleThreadTimeout, "Single thread timeout",
30 thread, eVoteNo, eVoteNoOpinion),
31 m_info(info), m_state(State::WaitTimeout) {
32 m_info->m_isAlive = true;
33 m_state = m_info->m_last_state;
34 // TODO: reuse m_timer_thread without recreation.
35 m_timer_thread = std::thread(TimeoutThreadFunc, this);
36}
37
39 m_info->m_isAlive = false;
40}
41
43 uint64_t timeout_in_ms = GetThread().GetSingleThreadPlanTimeout();
44 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
45 std::chrono::milliseconds duration_ms =
46 std::chrono::duration_cast<std::chrono::milliseconds>(now -
48 return timeout_in_ms - duration_ms.count();
49}
50
53 s->Printf("Single thread timeout, state(%s), remaining %" PRIu64 " ms",
55}
56
58 switch (state) {
60 return "WaitTimeout";
62 return "AsyncInterrupt";
63 case State::Done:
64 return "Done";
65 }
66 llvm_unreachable("Uncovered state value!");
67}
68
70 TimeoutInfoSP &info) {
71 uint64_t timeout_in_ms = thread.GetSingleThreadPlanTimeout();
72 if (timeout_in_ms == 0)
73 return;
74
75 // Do not create timeout if we are not stopping other threads.
76 if (!thread.GetCurrentPlan()->StopOthers())
77 return;
78
79 auto timeout_plan = new ThreadPlanSingleThreadTimeout(thread, info);
80 ThreadPlanSP thread_plan_sp(timeout_plan);
81 auto status = thread.QueueThreadPlan(thread_plan_sp,
82 /*abort_other_plans*/ false);
83 Log *log = GetLog(LLDBLog::Step);
85 log,
86 "ThreadPlanSingleThreadTimeout pushing a brand new one with %" PRIu64
87 " ms",
88 timeout_in_ms);
89}
90
92 TimeoutInfoSP &info) {
93 uint64_t timeout_in_ms = thread.GetSingleThreadPlanTimeout();
94 if (timeout_in_ms == 0)
95 return;
96
97 // There is already an instance alive.
98 if (info->m_isAlive)
99 return;
100
101 // Do not create timeout if we are not stopping other threads.
102 if (!thread.GetCurrentPlan()->StopOthers())
103 return;
104
105 auto timeout_plan = new ThreadPlanSingleThreadTimeout(thread, info);
106 ThreadPlanSP thread_plan_sp(timeout_plan);
107 auto status = thread.QueueThreadPlan(thread_plan_sp,
108 /*abort_other_plans*/ false);
109 Log *log = GetLog(LLDBLog::Step);
110 LLDB_LOGF(
111 log,
112 "ThreadPlanSingleThreadTimeout reset from previous state with %" PRIu64
113 " ms",
114 timeout_in_ms);
115}
116
118 Log *log = GetLog(LLDBLog::Step);
119 LLDB_LOGF(log, "ThreadPlanSingleThreadTimeout::WillStop().");
120
121 // Reset the state during stop.
122 m_info->m_last_state = State::WaitTimeout;
123 return true;
124}
125
127 Log *log = GetLog(LLDBLog::Step);
128 {
129 std::lock_guard<std::mutex> lock(m_mutex);
130 LLDB_LOGF(log, "ThreadPlanSingleThreadTimeout::DidPop().");
131 // Tell timer thread to exit.
132 m_info->m_isAlive = false;
133 }
134 m_wakeup_cv.notify_one();
135 // Wait for timer thread to exit.
136 m_timer_thread.join();
137}
138
140 bool is_timeout_interrupt = IsTimeoutAsyncInterrupt(event_ptr);
141 Log *log = GetLog(LLDBLog::Step);
142 LLDB_LOGF(log,
143 "ThreadPlanSingleThreadTimeout::DoPlanExplainsStop() returns %d. "
144 "%" PRIu64 " ms remaining.",
145 is_timeout_interrupt, GetRemainingTimeoutMilliSeconds());
146 return is_timeout_interrupt;
147}
148
151}
152
155 std::unique_lock<std::mutex> lock(self->m_mutex);
156 uint64_t timeout_in_ms = self->GetThread().GetSingleThreadPlanTimeout();
157 // The thread should wakeup either when timeout or
158 // ThreadPlanSingleThreadTimeout has been popped (not alive).
159 Log *log = GetLog(LLDBLog::Step);
160 self->m_timeout_start = std::chrono::steady_clock::now();
161 LLDB_LOGF(
162 log,
163 "ThreadPlanSingleThreadTimeout::TimeoutThreadFunc(), wait for %" PRIu64
164 " ms",
165 timeout_in_ms);
166 self->m_wakeup_cv.wait_for(lock, std::chrono::milliseconds(timeout_in_ms),
167 [self] { return !self->m_info->m_isAlive; });
168 LLDB_LOGF(log,
169 "ThreadPlanSingleThreadTimeout::TimeoutThreadFunc() wake up with "
170 "m_isAlive(%d).",
171 self->m_info->m_isAlive);
172 if (!self->m_info->m_isAlive)
173 return;
174
175 self->HandleTimeout();
176}
177
179 Log *log = GetLog(LLDBLog::Step);
180 LLDB_LOGF(log, "ThreadPlanSingleThreadTimeout::MischiefManaged() called.");
181 // Need to reset timer on each internal stop/execution progress.
182 return true;
183}
184
186 return HandleEvent(event_ptr);
187}
188
190 // Note: this assumes that the SingleThreadTimeout plan is always going to be
191 // pushed on behalf of the plan directly above it.
192 GetPreviousPlan()->SetStopOthers(new_value);
193}
194
196 if (m_state == State::Done)
197 return false;
198 else
199 return GetPreviousPlan()->StopOthers();
200}
201
203 lldb::StateType stop_state =
205 Log *log = GetLog(LLDBLog::Step);
206 LLDB_LOGF(log,
207 "ThreadPlanSingleThreadTimeout::IsTimeoutAsyncInterrupt(): got "
208 "event: %s.",
209 StateAsCString(stop_state));
210
211 lldb::StopInfoSP stop_info = GetThread().GetStopInfo();
212 return (m_state == State::AsyncInterrupt &&
213 stop_state == lldb::eStateStopped && stop_info &&
214 stop_info->GetStopReason() == lldb::eStopReasonInterrupt);
215}
216
218 if (IsTimeoutAsyncInterrupt(event_ptr)) {
219 Log *log = GetLog(LLDBLog::Step);
221 // If we were restarted, we just need to go back up to fetch
222 // another event.
223 LLDB_LOGF(log,
224 "ThreadPlanSingleThreadTimeout::HandleEvent(): Got a stop and "
225 "restart, so we'll continue waiting.");
226
227 } else {
228 LLDB_LOGF(
229 log,
230 "ThreadPlanSingleThreadTimeout::HandleEvent(): Got async interrupt "
231 ", so we will resume all threads.");
235 }
236 }
237 // Should not report stop.
238 return false;
239}
240
242 Log *log = GetLog(LLDBLog::Step);
243 LLDB_LOGF(
244 log,
245 "ThreadPlanSingleThreadTimeout::HandleTimeout() send async interrupt.");
247
248 // Private state thread will only send async interrupt
249 // in running state so no need to check state here.
251}
#define LLDB_LOGF(log,...)
Definition: Log.h:366
static bool GetRestartedFromEvent(const Event *event_ptr)
Definition: Process.cpp:4392
static lldb::StateType GetStateFromEvent(const Event *event_ptr)
Definition: Process.cpp:4384
void SendAsyncInterrupt(Thread *thread=nullptr)
Send an async interrupt request.
Definition: Process.cpp:3878
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
std::shared_ptr< ThreadPlanSingleThreadTimeout::TimeoutInfo > TimeoutInfoSP
static void ResumeFromPrevState(Thread &thread, TimeoutInfoSP &info)
std::chrono::steady_clock::time_point m_timeout_start
static void TimeoutThreadFunc(ThreadPlanSingleThreadTimeout *self)
static void PushNewWithTimeout(Thread &thread, TimeoutInfoSP &info)
ThreadPlanSingleThreadTimeout(Thread &thread, TimeoutInfoSP &info)
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
virtual lldb::StateType GetPlanRunState()=0
Thread & GetThread()
Returns the Thread that is using this thread plan.
Definition: ThreadPlan.cpp:42
virtual bool StopOthers()
Definition: ThreadPlan.cpp:105
ThreadPlan * GetPreviousPlan()
Definition: ThreadPlan.h:520
virtual void SetStopOthers(bool new_value)
Definition: ThreadPlan.cpp:111
uint64_t GetSingleThreadPlanTimeout() const
Definition: Thread.cpp:146
Status QueueThreadPlan(lldb::ThreadPlanSP &plan_sp, bool abort_other_plans)
Queues a generic thread plan.
Definition: Thread.cpp:1167
ThreadPlan * GetCurrentPlan() const
Gets the plan which will execute next on the plan stack.
Definition: Thread.cpp:1135
lldb::StopInfoSP GetStopInfo()
Definition: Thread.cpp:347
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:331
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
Definition: lldb-forward.h:449
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
StateType
Process and Thread States.
@ eStateStopped
Process or thread is stopped and can be examined.
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
Definition: lldb-forward.h:427
@ eStopReasonInterrupt
Thread requested interrupt.