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/LLDBLog.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/State.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 // ThreadPlan constructor
23 ThreadPlan::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) {
32  SetID(GetNextID());
33 }
34 
35 // Destructor
36 ThreadPlan::~ThreadPlan() = default;
37 
39 
40 const Target &ThreadPlan::GetTarget() const { return m_process.GetTarget(); }
41 
43  if (m_thread)
44  return *m_thread;
45 
46  ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(m_tid);
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 
66 void 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 
111 void 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 
116 bool 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) {
169  case eStopReasonSignal:
171  case eStopReasonExec:
174  return true;
175  default:
176  return false;
177  }
178 }
179 
180 // ThreadPlanNull
181 
183  : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
185 
187 
189  s->PutCString("Null thread plan - thread has been destroyed.");
190 }
191 
193 #ifdef LLDB_CONFIGURATION_DEBUG
194  fprintf(stderr,
195  "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
196  ", ptid = 0x%" PRIx64 ")",
197  LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
198 #else
199  Log *log = GetLog(LLDBLog::Thread);
200  if (log)
201  log->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 #endif
205  return true;
206 }
207 
209 #ifdef LLDB_CONFIGURATION_DEBUG
210  fprintf(stderr,
211  "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
212  ", ptid = 0x%" PRIx64 ")",
213  LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
214 #else
215  Log *log = GetLog(LLDBLog::Thread);
216  if (log)
217  log->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 #endif
221  return true;
222 }
223 
225 #ifdef LLDB_CONFIGURATION_DEBUG
226  fprintf(stderr,
227  "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
228  ", ptid = 0x%" PRIx64 ")",
229  LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
230 #else
231  Log *log = GetLog(LLDBLog::Thread);
232  if (log)
233  log->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 #endif
237  return true;
238 }
239 
241 #ifdef LLDB_CONFIGURATION_DEBUG
242  fprintf(stderr,
243  "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
244  ", ptid = 0x%" PRIx64 ")",
245  LLVM_PRETTY_FUNCTION, GetThread().GetID(), GetThread().GetProtocolID());
246 #else
247  Log *log = GetLog(LLDBLog::Thread);
248  if (log)
249  log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
250  ", ptid = 0x%" PRIx64 ")",
251  LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
252 #endif
253  return true;
254 }
255 
256 // The null plan is never done.
258 // The null plan is never done.
259 #ifdef LLDB_CONFIGURATION_DEBUG
260  fprintf(stderr,
261  "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
262  ", ptid = 0x%" PRIx64 ")",
263  LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
264 #else
265  Log *log = GetLog(LLDBLog::Thread);
266  if (log)
267  log->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 #endif
271  return false;
272 }
273 
275 // Not sure what to return here. This is a dead thread.
276 #ifdef LLDB_CONFIGURATION_DEBUG
277  fprintf(stderr,
278  "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
279  ", ptid = 0x%" PRIx64 ")",
280  LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
281 #else
282  Log *log = GetLog(LLDBLog::Thread);
283  if (log)
284  log->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 #endif
288  return eStateRunning;
289 }
lldb_private::ThreadPlan::DidPush
virtual void DidPush()
Definition: ThreadPlan.cpp:151
lldb_private::ThreadPlan::GetThread
Thread & GetThread()
Returns the Thread that is using this thread plan.
Definition: ThreadPlan.cpp:42
lldb_private::Event
Definition: Event.h:182
lldb_private::ThreadPlan::IsPlanComplete
bool IsPlanComplete()
Definition: ThreadPlan.cpp:61
lldb_private::ThreadPlanNull::~ThreadPlanNull
~ThreadPlanNull() override
lldb_private::ThreadPlan::m_report_run_vote
Vote m_report_run_vote
Definition: ThreadPlan.h:533
lldb_private::eLazyBoolYes
@ eLazyBoolYes
Definition: lldb-private-enumerations.h:115
lldb::eStopReasonThreadExiting
@ eStopReasonThreadExiting
Definition: lldb-enumerations.h:250
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:344
lldb_private::ThreadPlan::GetPlanRunState
virtual lldb::StateType GetPlanRunState()=0
lldb_private::Process::GetThreadList
ThreadList & GetThreadList()
Definition: Process.h:2086
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1219
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:240
lldb::eStopReasonException
@ eStopReasonException
Definition: lldb-enumerations.h:247
lldb_private::ThreadPlanNull::ThreadPlanNull
ThreadPlanNull(Thread &thread)
Definition: ThreadPlan.cpp:182
lldb_private::ThreadPlan::m_cached_plan_explains_stop
LazyBool m_cached_plan_explains_stop
Definition: ThreadPlan.h:552
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:1251
Debugger.h
lldb_private::ThreadPlan::m_plan_succeeded
bool m_plan_succeeded
Definition: ThreadPlan.h:557
lldb_private::Target
Definition: Target.h:469
lldb_private::ThreadPlan::CachePlanExplainsStop
void CachePlanExplainsStop(bool does_explain)
Definition: ThreadPlan.h:539
Process.h
lldb_private::ThreadPlan::~ThreadPlan
virtual ~ThreadPlan()
Target.h
lldb_private::ThreadPlan::ThreadPlanKind
ThreadPlanKind
Definition: ThreadPlan.h:292
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ThreadPlan::GetTarget
Target & GetTarget()
Definition: ThreadPlan.cpp:38
lldb_private::ThreadPlan
Definition: ThreadPlan.h:287
lldb_private::ThreadList::FindThreadByID
lldb::ThreadSP FindThreadByID(lldb::tid_t tid, bool can_update=true)
Definition: ThreadList.cpp:103
Log.h
lldb_private::RegisterContext::GetPC
uint64_t GetPC(uint64_t fail_value=LLDB_INVALID_ADDRESS)
Definition: RegisterContext.cpp:95
lldb_private::ThreadPlan::MischiefManaged
virtual bool MischiefManaged()
Definition: ThreadPlan.cpp:72
lldb::eStopReasonWatchpoint
@ eStopReasonWatchpoint
Definition: lldb-enumerations.h:245
lldb_private::Thread
Definition: Thread.h:61
lldb_private::ThreadPlan::GetPreviousPlan
ThreadPlan * GetPreviousPlan()
Definition: ThreadPlan.h:512
lldb_private::LLDBLog::Thread
@ Thread
lldb_private::ThreadPlanNull::ValidatePlan
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
Definition: ThreadPlan.cpp:192
lldb::eStopReasonInstrumentation
@ eStopReasonInstrumentation
Definition: lldb-enumerations.h:251
lldb_private::ThreadPlan::DoPlanExplainsStop
virtual bool DoPlanExplainsStop(Event *event_ptr)=0
lldb_private::ThreadPlan::GetNextID
static lldb::user_id_t GetNextID()
Definition: ThreadPlan.cpp:146
lldb_private::ThreadPlan::PlanExplainsStop
bool PlanExplainsStop(Event *event_ptr)
Definition: ThreadPlan.cpp:51
sp
@ sp
Definition: CompactUnwindInfo.cpp:1250
lldb_private::RegisterContext::GetFP
uint64_t GetFP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
Definition: RegisterContext.cpp:164
lldb_private::ThreadPlan::m_plan_complete
bool m_plan_complete
Definition: ThreadPlan.h:553
lldb_private::ThreadPlan::m_plan_complete_mutex
std::recursive_mutex m_plan_complete_mutex
Definition: ThreadPlan.h:551
Thread.h
lldb_private::LLDBLog::Step
@ Step
lldb_private::RegisterContext::GetSP
uint64_t GetSP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
Definition: RegisterContext.cpp:152
lldb_private::RegisterContext
Definition: RegisterContext.h:17
lldb_private::ThreadPlan::OkayToDiscard
virtual bool OkayToDiscard()
Definition: ThreadPlan.cpp:155
lldb_private::ThreadPlanNull::MischiefManaged
bool MischiefManaged() override
Definition: ThreadPlan.cpp:257
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:87
lldb_private::Log::Error
void void Error(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:162
lldb_private::ThreadPlan::StopOthers
virtual bool StopOthers()
Definition: ThreadPlan.cpp:105
lldb::eStopReasonSignal
@ eStopReasonSignal
Definition: lldb-enumerations.h:246
lldb_private::ThreadPlan::SetPlanComplete
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:66
lldb::eStopReasonExec
@ eStopReasonExec
Program was re-exec'ed.
Definition: lldb-enumerations.h:248
lldb_private::ThreadPlan::IsUsuallyUnexplainedStopReason
bool IsUsuallyUnexplainedStopReason(lldb::StopReason)
Definition: ThreadPlan.cpp:166
uint32_t
lldb_private::ThreadPlan::DoWillResume
virtual bool DoWillResume(lldb::StateType resume_state, bool current_plan)
Definition: ThreadPlan.h:493
lldb_private::ThreadPlan::DidPop
virtual void DidPop()
Definition: ThreadPlan.cpp:153
lldb_private::ThreadPlan::SetStopOthers
virtual void SetStopOthers(bool new_value)
Definition: ThreadPlan.cpp:111
lldb_private::ThreadPlan::m_report_stop_vote
Vote m_report_stop_vote
Definition: ThreadPlan.h:532
lldb_private::Vote
Vote
Definition: lldb-private-enumerations.h:59
lldb_private::ThreadPlan::ShouldReportRun
Vote ShouldReportRun(Event *event_ptr)
Definition: ThreadPlan.cpp:94
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:337
lldb_private::ThreadPlan::m_name
std::string m_name
Definition: ThreadPlan.h:550
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:555
lldb_private::ThreadPlan::IsControllingPlan
bool IsControllingPlan()
Definition: ThreadPlan.h:396
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:274
lldb_private::ThreadPlan::WillResume
bool WillResume(lldb::StateType resume_state, bool current_plan)
Definition: ThreadPlan.cpp:116
lldb_private::ThreadPlan::m_tracer_sp
lldb::ThreadPlanTracerSP m_tracer_sp
Definition: ThreadPlan.h:559
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:188
lldb_private::ThreadPlan::m_tid
lldb::tid_t m_tid
Definition: ThreadPlan.h:531
lldb_private::ThreadPlan::ShouldReportStop
virtual Vote ShouldReportStop(Event *event_ptr)
Definition: ThreadPlan.cpp:79
State.h
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:74
lldb_private::Log
Definition: Log.h:115
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:224
lldb_private::Thread::GetRegisterContext
virtual lldb::RegisterContextSP GetRegisterContext()=0
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
lldb_private::ThreadPlan::ClearThreadCache
void ClearThreadCache()
Clear the Thread* cache.
Definition: ThreadPlan.cpp:103
lldb_private::ThreadPlanNull::DoPlanExplainsStop
bool DoPlanExplainsStop(Event *event_ptr) override
Definition: ThreadPlan.cpp:240
fp
@ fp
Definition: CompactUnwindInfo.cpp:1248
lldb_private::ThreadPlan::m_process
Process & m_process
Definition: ThreadPlan.h:530
lldb_private::ThreadPlan::RunState
lldb::StateType RunState()
Definition: ThreadPlan.cpp:159
lldb
Definition: SBAddress.h:15
lldb::eStateRunning
@ eStateRunning
Process or thread is running and can't be examined.
Definition: lldb-enumerations.h:86
RegisterContext.h
LLDBLog.h
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:208
lldb_private::ThreadPlanNull::ShouldStop
bool ShouldStop(Event *event_ptr) override
Definition: ThreadPlan.cpp:208
lldb_private::ThreadPlan::m_thread
Thread * m_thread
Definition: ThreadPlan.h:546