LLDB mainline
ThreadPlan.cpp
Go to the documentation of this file.
1//===-- ThreadPlan.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/Core/Debugger.h"
11#include "lldb/Target/Process.h"
13#include "lldb/Target/Target.h"
14#include "lldb/Target/Thread.h"
16#include "lldb/Utility/Log.h"
17#include "lldb/Utility/State.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
22// ThreadPlan constructor
23ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
24 Vote report_stop_vote, Vote report_run_vote)
25 : m_process(*thread.GetProcess().get()), m_tid(thread.GetID()),
26 m_report_stop_vote(report_stop_vote), m_report_run_vote(report_run_vote),
27 m_takes_iteration_count(false), m_could_not_resolve_hw_bp(false),
28 m_thread(&thread), m_kind(kind), m_name(name), m_plan_complete_mutex(),
29 m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false),
30 m_plan_private(false), m_okay_to_discard(true),
31 m_is_controlling_plan(false), m_plan_succeeded(true) {
33}
34
35// Destructor
36ThreadPlan::~ThreadPlan() = default;
37
39
40const Target &ThreadPlan::GetTarget() const { return m_process.GetTarget(); }
41
43 if (m_thread)
44 return *m_thread;
45
47 m_thread = thread_sp.get();
48 return *m_thread;
49}
50
53 bool actual_value = DoPlanExplainsStop(event_ptr);
54 CachePlanExplainsStop(actual_value);
55 return actual_value;
56 } else {
58 }
59}
60
62 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
63 return m_plan_complete;
64}
65
66void ThreadPlan::SetPlanComplete(bool success) {
67 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
68 m_plan_complete = true;
69 m_plan_succeeded = success;
70}
71
73 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
74 // Mark the plan is complete, but don't override the success flag.
75 m_plan_complete = true;
76 return true;
77}
78
80 Log *log = GetLog(LLDBLog::Step);
81
83 ThreadPlan *prev_plan = GetPreviousPlan();
84 if (prev_plan) {
85 Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
86 LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
87 return prev_vote;
88 }
89 }
90 LLDB_LOG(log, "Returning vote: {0}", m_report_stop_vote);
91 return m_report_stop_vote;
92}
93
96 ThreadPlan *prev_plan = GetPreviousPlan();
97 if (prev_plan)
98 return prev_plan->ShouldReportRun(event_ptr);
99 }
100 return m_report_run_vote;
101}
102
104
106 ThreadPlan *prev_plan;
107 prev_plan = GetPreviousPlan();
108 return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
109}
110
111void ThreadPlan::SetStopOthers(bool new_value) {
112 // SetStopOthers doesn't work up the hierarchy. You have to set the explicit
113 // ThreadPlan you want to affect.
114}
115
116bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
118
119 if (current_plan) {
120 Log *log = GetLog(LLDBLog::Step);
121
122 if (log) {
123 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get();
124 assert(reg_ctx);
125 addr_t pc = reg_ctx->GetPC();
126 addr_t sp = reg_ctx->GetSP();
127 addr_t fp = reg_ctx->GetFP();
128 LLDB_LOGF(
129 log,
130 "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
131 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
132 "plan = '%s', state = %s, stop others = %d",
133 __FUNCTION__, GetThread().GetIndexID(),
134 static_cast<void *>(&GetThread()), m_tid, static_cast<uint64_t>(pc),
135 static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
136 StateAsCString(resume_state), StopOthers());
137 }
138 }
139 bool success = DoWillResume(resume_state, current_plan);
140 ClearThreadCache(); // We don't cache the thread pointer over resumes. This
141 // Thread might go away, and another Thread represent
142 // the same underlying object on a later stop.
143 return success;
144}
145
147 static uint32_t g_nextPlanID = 0;
148 return ++g_nextPlanID;
149}
150
152
154
156 return IsControllingPlan() ? m_okay_to_discard : true;
157}
158
160 if (m_tracer_sp && m_tracer_sp->TracingEnabled())
161 return eStateStepping;
162 else
163 return GetPlanRunState();
164}
165
167 switch (reason) {
171 case eStopReasonExec:
174 case eStopReasonFork:
175 case eStopReasonVFork:
178 return true;
179 default:
180 return false;
181 }
182}
183
184// ThreadPlanNull
185
187 : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
189
191
193 s->PutCString("Null thread plan - thread has been destroyed.");
194}
195
197#ifdef LLDB_CONFIGURATION_DEBUG
198 fprintf(stderr,
199 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
200 ", ptid = 0x%" PRIx64 ")",
201 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
202#else
203 Log *log = GetLog(LLDBLog::Thread);
204 if (log)
205 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
206 ", ptid = 0x%" PRIx64 ")",
207 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
208#endif
209 return true;
210}
211
213#ifdef LLDB_CONFIGURATION_DEBUG
214 fprintf(stderr,
215 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
216 ", ptid = 0x%" PRIx64 ")",
217 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
218#else
219 Log *log = GetLog(LLDBLog::Thread);
220 if (log)
221 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
222 ", ptid = 0x%" PRIx64 ")",
223 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
224#endif
225 return true;
226}
227
229#ifdef LLDB_CONFIGURATION_DEBUG
230 fprintf(stderr,
231 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
232 ", ptid = 0x%" PRIx64 ")",
233 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
234#else
235 Log *log = GetLog(LLDBLog::Thread);
236 if (log)
237 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
238 ", ptid = 0x%" PRIx64 ")",
239 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
240#endif
241 return true;
242}
243
245#ifdef LLDB_CONFIGURATION_DEBUG
246 fprintf(stderr,
247 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
248 ", ptid = 0x%" PRIx64 ")",
249 LLVM_PRETTY_FUNCTION, GetThread().GetID(), GetThread().GetProtocolID());
250#else
251 Log *log = GetLog(LLDBLog::Thread);
252 if (log)
253 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
254 ", ptid = 0x%" PRIx64 ")",
255 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
256#endif
257 return true;
258}
259
260// The null plan is never done.
262// The null plan is never done.
263#ifdef LLDB_CONFIGURATION_DEBUG
264 fprintf(stderr,
265 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
266 ", ptid = 0x%" PRIx64 ")",
267 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
268#else
269 Log *log = GetLog(LLDBLog::Thread);
270 if (log)
271 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
272 ", ptid = 0x%" PRIx64 ")",
273 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
274#endif
275 return false;
276}
277
279// Not sure what to return here. This is a dead thread.
280#ifdef LLDB_CONFIGURATION_DEBUG
281 fprintf(stderr,
282 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
283 ", ptid = 0x%" PRIx64 ")",
284 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
285#else
286 Log *log = GetLog(LLDBLog::Thread);
287 if (log)
288 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
289 ", ptid = 0x%" PRIx64 ")",
290 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
291#endif
292 return eStateRunning;
293}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:369
#define LLDB_LOGF(log,...)
Definition: Log.h:376
void void void Error(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:185
ThreadList & GetThreadList()
Definition: Process.h:2182
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1246
uint64_t GetPC(uint64_t fail_value=LLDB_INVALID_ADDRESS)
uint64_t GetSP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
uint64_t GetFP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
lldb::ThreadSP FindThreadByID(lldb::tid_t tid, bool can_update=true)
Definition: ThreadList.cpp:102
bool MischiefManaged() override
Definition: ThreadPlan.cpp:261
lldb::StateType GetPlanRunState() override
Definition: ThreadPlan.cpp:278
ThreadPlanNull(Thread &thread)
Definition: ThreadPlan.cpp:186
bool DoPlanExplainsStop(Event *event_ptr) override
Definition: ThreadPlan.cpp:244
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
Definition: ThreadPlan.cpp:192
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
Definition: ThreadPlan.cpp:196
bool ShouldStop(Event *event_ptr) override
Definition: ThreadPlan.cpp:212
virtual Vote ShouldReportStop(Event *event_ptr)
Definition: ThreadPlan.cpp:79
virtual bool DoPlanExplainsStop(Event *event_ptr)=0
virtual bool OkayToDiscard()
Definition: ThreadPlan.cpp:155
ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote report_stop_vote, Vote report_run_vote)
Definition: ThreadPlan.cpp:23
virtual bool DoWillResume(lldb::StateType resume_state, bool current_plan)
Definition: ThreadPlan.h:507
virtual lldb::StateType GetPlanRunState()=0
Vote ShouldReportRun(Event *event_ptr)
Definition: ThreadPlan.cpp:94
bool IsUsuallyUnexplainedStopReason(lldb::StopReason)
Definition: ThreadPlan.cpp:166
LazyBool m_cached_plan_explains_stop
Definition: ThreadPlan.h:564
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:66
Thread & GetThread()
Returns the Thread that is using this thread plan.
Definition: ThreadPlan.cpp:42
bool PlanExplainsStop(Event *event_ptr)
Definition: ThreadPlan.cpp:51
virtual bool StopOthers()
Definition: ThreadPlan.cpp:105
static lldb::user_id_t GetNextID()
Definition: ThreadPlan.cpp:146
ThreadPlan * GetPreviousPlan()
Definition: ThreadPlan.h:526
void ClearThreadCache()
Clear the Thread* cache.
Definition: ThreadPlan.cpp:103
bool WillResume(lldb::StateType resume_state, bool current_plan)
Definition: ThreadPlan.cpp:116
virtual bool MischiefManaged()
Definition: ThreadPlan.cpp:72
virtual void DidPush()
Definition: ThreadPlan.cpp:151
lldb::ThreadPlanTracerSP m_tracer_sp
Definition: ThreadPlan.h:571
lldb::StateType RunState()
Definition: ThreadPlan.cpp:159
virtual void SetStopOthers(bool new_value)
Definition: ThreadPlan.cpp:111
void CachePlanExplainsStop(bool does_explain)
Definition: ThreadPlan.h:551
std::recursive_mutex m_plan_complete_mutex
Definition: ThreadPlan.h:563
virtual lldb::RegisterContextSP GetRegisterContext()=0
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:332
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
Definition: SBAddress.h:15
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:450
StateType
Process and Thread States.
@ eStateRunning
Process or thread is running and can't be examined.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
uint64_t user_id_t
Definition: lldb-types.h:82
uint64_t addr_t
Definition: lldb-types.h:80
StopReason
Thread stop reasons.
@ eStopReasonInstrumentation
@ eStopReasonExec
Program was re-exec'ed.
@ eStopReasonVForkDone
@ eStopReasonInterrupt
Thread requested interrupt.
@ eStopReasonThreadExiting
@ eStopReasonException
@ eStopReasonWatchpoint
@ eStopReasonVFork
@ eStopReasonSignal
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47
void SetID(lldb::user_id_t uid)
Set accessor for the user ID.
Definition: UserID.h:53