]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Target/ThreadPlan.cpp
Merge ^/head r343807 through r343955.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Target / ThreadPlan.cpp
1 //===-- ThreadPlan.cpp ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Target/ThreadPlan.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Target/Process.h"
13 #include "lldb/Target/RegisterContext.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Target/Thread.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 //----------------------------------------------------------------------
23 // ThreadPlan constructor
24 //----------------------------------------------------------------------
25 ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
26                        Vote stop_vote, Vote run_vote)
27     : m_thread(thread), m_stop_vote(stop_vote), m_run_vote(run_vote),
28       m_takes_iteration_count(false), m_could_not_resolve_hw_bp(false),
29       m_kind(kind), m_name(name), m_plan_complete_mutex(),
30       m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false),
31       m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false),
32       m_plan_succeeded(true) {
33   SetID(GetNextID());
34 }
35
36 //----------------------------------------------------------------------
37 // Destructor
38 //----------------------------------------------------------------------
39 ThreadPlan::~ThreadPlan() = default;
40
41 bool ThreadPlan::PlanExplainsStop(Event *event_ptr) {
42   if (m_cached_plan_explains_stop == eLazyBoolCalculate) {
43     bool actual_value = DoPlanExplainsStop(event_ptr);
44     m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
45     return actual_value;
46   } else {
47     return m_cached_plan_explains_stop == eLazyBoolYes;
48   }
49 }
50
51 bool ThreadPlan::IsPlanComplete() {
52   std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
53   return m_plan_complete;
54 }
55
56 void ThreadPlan::SetPlanComplete(bool success) {
57   std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
58   m_plan_complete = true;
59   m_plan_succeeded = success;
60 }
61
62 bool ThreadPlan::MischiefManaged() {
63   std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
64   // Mark the plan is complete, but don't override the success flag.
65   m_plan_complete = true;
66   return true;
67 }
68
69 Vote ThreadPlan::ShouldReportStop(Event *event_ptr) {
70   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
71
72   if (m_stop_vote == eVoteNoOpinion) {
73     ThreadPlan *prev_plan = GetPreviousPlan();
74     if (prev_plan) {
75       Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
76       LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
77       return prev_vote;
78     }
79   }
80   LLDB_LOG(log, "Returning vote: {0}", m_stop_vote);
81   return m_stop_vote;
82 }
83
84 Vote ThreadPlan::ShouldReportRun(Event *event_ptr) {
85   if (m_run_vote == eVoteNoOpinion) {
86     ThreadPlan *prev_plan = GetPreviousPlan();
87     if (prev_plan)
88       return prev_plan->ShouldReportRun(event_ptr);
89   }
90   return m_run_vote;
91 }
92
93 bool ThreadPlan::StopOthers() {
94   ThreadPlan *prev_plan;
95   prev_plan = GetPreviousPlan();
96   return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
97 }
98
99 void ThreadPlan::SetStopOthers(bool new_value) {
100   // SetStopOthers doesn't work up the hierarchy.  You have to set the explicit
101   // ThreadPlan you want to affect.
102 }
103
104 bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
105   m_cached_plan_explains_stop = eLazyBoolCalculate;
106
107   if (current_plan) {
108     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
109
110     if (log) {
111       RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
112       assert(reg_ctx);
113       addr_t pc = reg_ctx->GetPC();
114       addr_t sp = reg_ctx->GetSP();
115       addr_t fp = reg_ctx->GetFP();
116       log->Printf(
117           "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
118           ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
119           "plan = '%s', state = %s, stop others = %d",
120           __FUNCTION__, m_thread.GetIndexID(), static_cast<void *>(&m_thread),
121           m_thread.GetID(), static_cast<uint64_t>(pc),
122           static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
123           StateAsCString(resume_state), StopOthers());
124     }
125   }
126   return DoWillResume(resume_state, current_plan);
127 }
128
129 lldb::user_id_t ThreadPlan::GetNextID() {
130   static uint32_t g_nextPlanID = 0;
131   return ++g_nextPlanID;
132 }
133
134 void ThreadPlan::DidPush() {}
135
136 void ThreadPlan::WillPop() {}
137
138 bool ThreadPlan::OkayToDiscard() {
139   return IsMasterPlan() ? m_okay_to_discard : true;
140 }
141
142 lldb::StateType ThreadPlan::RunState() {
143   if (m_tracer_sp && m_tracer_sp->TracingEnabled() &&
144       m_tracer_sp->SingleStepEnabled())
145     return eStateStepping;
146   else
147     return GetPlanRunState();
148 }
149
150 bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) {
151   switch (reason) {
152   case eStopReasonWatchpoint:
153   case eStopReasonSignal:
154   case eStopReasonException:
155   case eStopReasonExec:
156   case eStopReasonThreadExiting:
157   case eStopReasonInstrumentation:
158     return true;
159   default:
160     return false;
161   }
162 }
163
164 //----------------------------------------------------------------------
165 // ThreadPlanNull
166 //----------------------------------------------------------------------
167
168 ThreadPlanNull::ThreadPlanNull(Thread &thread)
169     : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
170                  eVoteNoOpinion, eVoteNoOpinion) {}
171
172 ThreadPlanNull::~ThreadPlanNull() = default;
173
174 void ThreadPlanNull::GetDescription(Stream *s, lldb::DescriptionLevel level) {
175   s->PutCString("Null thread plan - thread has been destroyed.");
176 }
177
178 bool ThreadPlanNull::ValidatePlan(Stream *error) {
179 #ifdef LLDB_CONFIGURATION_DEBUG
180   fprintf(stderr,
181           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
182           ", ptid = 0x%" PRIx64 ")",
183           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
184 #else
185   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
186   if (log)
187     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
188                ", ptid = 0x%" PRIx64 ")",
189                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
190                m_thread.GetProtocolID());
191 #endif
192   return true;
193 }
194
195 bool ThreadPlanNull::ShouldStop(Event *event_ptr) {
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_thread.GetID(), m_thread.GetProtocolID());
201 #else
202   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_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_thread.GetID(),
207                m_thread.GetProtocolID());
208 #endif
209   return true;
210 }
211
212 bool ThreadPlanNull::WillStop() {
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_thread.GetID(), m_thread.GetProtocolID());
218 #else
219   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_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_thread.GetID(),
224                m_thread.GetProtocolID());
225 #endif
226   return true;
227 }
228
229 bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) {
230 #ifdef LLDB_CONFIGURATION_DEBUG
231   fprintf(stderr,
232           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
233           ", ptid = 0x%" PRIx64 ")",
234           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
235 #else
236   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
237   if (log)
238     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
239                ", ptid = 0x%" PRIx64 ")",
240                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
241                m_thread.GetProtocolID());
242 #endif
243   return true;
244 }
245
246 // The null plan is never done.
247 bool ThreadPlanNull::MischiefManaged() {
248 // The null plan is never done.
249 #ifdef LLDB_CONFIGURATION_DEBUG
250   fprintf(stderr,
251           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
252           ", ptid = 0x%" PRIx64 ")",
253           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
254 #else
255   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
256   if (log)
257     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
258                ", ptid = 0x%" PRIx64 ")",
259                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
260                m_thread.GetProtocolID());
261 #endif
262   return false;
263 }
264
265 lldb::StateType ThreadPlanNull::GetPlanRunState() {
266 // Not sure what to return here.  This is a dead thread.
267 #ifdef LLDB_CONFIGURATION_DEBUG
268   fprintf(stderr,
269           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
270           ", ptid = 0x%" PRIx64 ")",
271           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
272 #else
273   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
274   if (log)
275     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
276                ", ptid = 0x%" PRIx64 ")",
277                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
278                m_thread.GetProtocolID());
279 #endif
280   return eStateRunning;
281 }