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
19#include <atomic>
20
21using namespace lldb;
22using namespace lldb_private;
23
24// ThreadPlan constructor
25ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
26 Vote report_stop_vote, Vote report_run_vote)
27 : m_process(*thread.GetProcess().get()), m_tid(thread.GetID()),
28 m_report_stop_vote(report_stop_vote), m_report_run_vote(report_run_vote),
30 m_thread(&thread), m_kind(kind), m_name(name), m_plan_complete_mutex(),
35}
36
37// Destructor
38ThreadPlan::~ThreadPlan() = default;
39
40Target &ThreadPlan::GetTarget() { return m_process.GetTarget(); }
41
42const Target &ThreadPlan::GetTarget() const { return m_process.GetTarget(); }
43
45 if (m_thread)
46 return *m_thread;
47
48 ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(m_tid);
49 m_thread = thread_sp.get();
50 return *m_thread;
51}
52
55 bool actual_value = DoPlanExplainsStop(event_ptr);
56 CachePlanExplainsStop(actual_value);
57 return actual_value;
58 } else {
60 }
61}
62
64 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
65 return m_plan_complete;
66}
67
68void ThreadPlan::SetPlanComplete(bool success) {
69 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
70 m_plan_complete = true;
71 m_plan_succeeded = success;
72}
73
75 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
76 // Mark the plan is complete, but don't override the success flag.
77 m_plan_complete = true;
78 return true;
79}
80
82 Log *log = GetLog(LLDBLog::Step);
83
85 ThreadPlan *prev_plan = GetPreviousPlan();
86 if (prev_plan) {
87 Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
88 LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
89 return prev_vote;
90 }
91 }
92 LLDB_LOG(log, "Returning vote: {0}", m_report_stop_vote);
93 return m_report_stop_vote;
94}
95
98 ThreadPlan *prev_plan = GetPreviousPlan();
99 if (prev_plan)
100 return prev_plan->ShouldReportRun(event_ptr);
101 }
102 return m_report_run_vote;
103}
104
106
108 ThreadPlan *prev_plan;
109 prev_plan = GetPreviousPlan();
110 return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
111}
112
113void ThreadPlan::SetStopOthers(bool new_value) {
114 // SetStopOthers doesn't work up the hierarchy. You have to set the explicit
115 // ThreadPlan you want to affect.
116}
117
118bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
120
121 if (current_plan) {
122 Log *log = GetLog(LLDBLog::Step);
123
124 if (log) {
125 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get();
126 assert(reg_ctx);
127 addr_t pc = reg_ctx->GetPC();
128 addr_t sp = reg_ctx->GetSP();
129 addr_t fp = reg_ctx->GetFP();
130 LLDB_LOGF(
131 log,
132 "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
133 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
134 "plan = '%s', state = %s, stop others = %d",
135 __FUNCTION__, GetThread().GetIndexID(),
136 static_cast<void *>(&GetThread()), m_tid, static_cast<uint64_t>(pc),
137 static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
138 StateAsCString(resume_state), StopOthers());
139 }
140 }
141 bool success = DoWillResume(resume_state, current_plan);
142 ClearThreadCache(); // We don't cache the thread pointer over resumes. This
143 // Thread might go away, and another Thread represent
144 // the same underlying object on a later stop.
145 return success;
146}
147
149 static std::atomic<uint32_t> g_nextPlanID{0};
150 return ++g_nextPlanID;
151}
152
154
156
160
162 if (m_tracer_sp && m_tracer_sp->TracingEnabled())
163 return eStateStepping;
164 else
165 return GetPlanRunState();
166}
167
169 switch (reason) {
173 case eStopReasonExec:
176 case eStopReasonFork:
177 case eStopReasonVFork:
180 return true;
181 default:
182 return false;
183 }
184}
185
186// ThreadPlanNull
187
191
193
195 s->PutCString("Null thread plan - thread has been destroyed.");
196}
197
199#ifdef LLDB_CONFIGURATION_DEBUG
200 fprintf(stderr,
201 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
202 ", ptid = 0x%" PRIx64 ")",
203 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
204#else
205 Log *log = GetLog(LLDBLog::Thread);
206 LLDB_LOGF(log,
207 "error: %s called on thread that has been destroyed "
208 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
209 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
210#endif
211 return true;
212}
213
215#ifdef LLDB_CONFIGURATION_DEBUG
216 fprintf(stderr,
217 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
218 ", ptid = 0x%" PRIx64 ")",
219 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
220#else
221 Log *log = GetLog(LLDBLog::Thread);
222 LLDB_LOGF(log,
223 "error: %s called on thread that has been destroyed "
224 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
225 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
226#endif
227 return true;
228}
229
231#ifdef LLDB_CONFIGURATION_DEBUG
232 fprintf(stderr,
233 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
234 ", ptid = 0x%" PRIx64 ")",
235 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
236#else
237 Log *log = GetLog(LLDBLog::Thread);
238 LLDB_LOGF(log,
239 "error: %s called on thread that has been destroyed "
240 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
241 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
242#endif
243 return true;
244}
245
247#ifdef LLDB_CONFIGURATION_DEBUG
248 fprintf(stderr,
249 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
250 ", ptid = 0x%" PRIx64 ")",
251 LLVM_PRETTY_FUNCTION, GetThread().GetID(), GetThread().GetProtocolID());
252#else
253 Log *log = GetLog(LLDBLog::Thread);
254 LLDB_LOGF(log,
255 "error: %s called on thread that has been destroyed "
256 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
257 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
258#endif
259 return true;
260}
261
262// The null plan is never done.
264// The null plan is never done.
265#ifdef LLDB_CONFIGURATION_DEBUG
266 fprintf(stderr,
267 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
268 ", ptid = 0x%" PRIx64 ")",
269 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
270#else
271 Log *log = GetLog(LLDBLog::Thread);
272 LLDB_LOGF(log,
273 "error: %s called on thread that has been destroyed "
274 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
275 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
276#endif
277 return false;
278}
279
281// Not sure what to return here. This is a dead thread.
282#ifdef LLDB_CONFIGURATION_DEBUG
283 fprintf(stderr,
284 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
285 ", ptid = 0x%" PRIx64 ")",
286 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
287#else
288 Log *log = GetLog(LLDBLog::Thread);
289 LLDB_LOGF(log,
290 "error: %s called on thread that has been destroyed "
291 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
292 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
293#endif
294 return eStateRunning;
295}
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:364
#define LLDB_LOGF(log,...)
Definition Log.h:378
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:63
bool MischiefManaged() override
lldb::StateType GetPlanRunState() override
bool DoPlanExplainsStop(Event *event_ptr) override
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
bool ShouldStop(Event *event_ptr) override
virtual Vote ShouldReportStop(Event *event_ptr)
ThreadPlanKind m_kind
Definition ThreadPlan.h:574
virtual bool DoPlanExplainsStop(Event *event_ptr)=0
virtual bool OkayToDiscard()
ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote report_stop_vote, Vote report_run_vote)
virtual bool DoWillResume(lldb::StateType resume_state, bool current_plan)
Definition ThreadPlan.h:520
virtual lldb::StateType GetPlanRunState()=0
Vote ShouldReportRun(Event *event_ptr)
bool IsUsuallyUnexplainedStopReason(lldb::StopReason)
LazyBool m_cached_plan_explains_stop
Definition ThreadPlan.h:577
void SetPlanComplete(bool success=true)
Thread & GetThread()
Returns the Thread that is using this thread plan.
bool PlanExplainsStop(Event *event_ptr)
virtual bool StopOthers()
static lldb::user_id_t GetNextID()
ThreadPlan * GetPreviousPlan()
Definition ThreadPlan.h:539
void ClearThreadCache()
Clear the Thread* cache.
bool WillResume(lldb::StateType resume_state, bool current_plan)
virtual bool MischiefManaged()
lldb::ThreadPlanTracerSP m_tracer_sp
Definition ThreadPlan.h:584
lldb::StateType RunState()
virtual void SetStopOthers(bool new_value)
void CachePlanExplainsStop(bool does_explain)
Definition ThreadPlan.h:564
std::recursive_mutex m_plan_complete_mutex
Definition ThreadPlan.h:576
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:327
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition State.cpp:14
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
std::shared_ptr< lldb_private::Thread > ThreadSP
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
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