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:
177 return true;
178 default:
179 return false;
180 }
181}
182
183// ThreadPlanNull
184
186 : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
188
190
192 s->PutCString("Null thread plan - thread has been destroyed.");
193}
194
196#ifdef LLDB_CONFIGURATION_DEBUG
197 fprintf(stderr,
198 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
199 ", ptid = 0x%" PRIx64 ")",
200 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
201#else
202 Log *log = GetLog(LLDBLog::Thread);
203 if (log)
204 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
205 ", ptid = 0x%" PRIx64 ")",
206 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
207#endif
208 return true;
209}
210
212#ifdef LLDB_CONFIGURATION_DEBUG
213 fprintf(stderr,
214 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
215 ", ptid = 0x%" PRIx64 ")",
216 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
217#else
218 Log *log = GetLog(LLDBLog::Thread);
219 if (log)
220 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
221 ", ptid = 0x%" PRIx64 ")",
222 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
223#endif
224 return true;
225}
226
228#ifdef LLDB_CONFIGURATION_DEBUG
229 fprintf(stderr,
230 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
231 ", ptid = 0x%" PRIx64 ")",
232 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
233#else
234 Log *log = GetLog(LLDBLog::Thread);
235 if (log)
236 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
237 ", ptid = 0x%" PRIx64 ")",
238 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
239#endif
240 return true;
241}
242
244#ifdef LLDB_CONFIGURATION_DEBUG
245 fprintf(stderr,
246 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
247 ", ptid = 0x%" PRIx64 ")",
248 LLVM_PRETTY_FUNCTION, GetThread().GetID(), GetThread().GetProtocolID());
249#else
250 Log *log = GetLog(LLDBLog::Thread);
251 if (log)
252 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
253 ", ptid = 0x%" PRIx64 ")",
254 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
255#endif
256 return true;
257}
258
259// The null plan is never done.
261// The null plan is never done.
262#ifdef LLDB_CONFIGURATION_DEBUG
263 fprintf(stderr,
264 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
265 ", ptid = 0x%" PRIx64 ")",
266 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
267#else
268 Log *log = GetLog(LLDBLog::Thread);
269 if (log)
270 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
271 ", ptid = 0x%" PRIx64 ")",
272 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
273#endif
274 return false;
275}
276
278// Not sure what to return here. This is a dead thread.
279#ifdef LLDB_CONFIGURATION_DEBUG
280 fprintf(stderr,
281 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
282 ", ptid = 0x%" PRIx64 ")",
283 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
284#else
285 Log *log = GetLog(LLDBLog::Thread);
286 if (log)
287 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
288 ", ptid = 0x%" PRIx64 ")",
289 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
290#endif
291 return eStateRunning;
292}
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:342
#define LLDB_LOGF(log,...)
Definition: Log.h:349
void void void Error(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:174
ThreadList & GetThreadList()
Definition: Process.h:2213
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1277
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:103
bool MischiefManaged() override
Definition: ThreadPlan.cpp:260
lldb::StateType GetPlanRunState() override
Definition: ThreadPlan.cpp:277
ThreadPlanNull(Thread &thread)
Definition: ThreadPlan.cpp:185
bool DoPlanExplainsStop(Event *event_ptr) override
Definition: ThreadPlan.cpp:243
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
Definition: ThreadPlan.cpp:191
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
Definition: ThreadPlan.cpp:195
bool ShouldStop(Event *event_ptr) override
Definition: ThreadPlan.cpp:211
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:493
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:552
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:512
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:559
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:539
std::recursive_mutex m_plan_complete_mutex
Definition: ThreadPlan.h:551
virtual lldb::RegisterContextSP GetRegisterContext()=0
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:314
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:438
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:80
uint64_t addr_t
Definition: lldb-types.h:79
StopReason
Thread stop reasons.
@ eStopReasonInstrumentation
@ eStopReasonExec
Program was re-exec'ed.
@ eStopReasonVForkDone
@ 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