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