1*0b57cec5SDimitry Andric //===-- ThreadPlanBase.cpp ------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanBase.h"
10*0b57cec5SDimitry Andric 
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
13*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
14*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointSite.h"
15*0b57cec5SDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
16*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
17*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
18*0b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
19*0b57cec5SDimitry Andric #include "lldb/Utility/LLDBLog.h"
20*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
21*0b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric using namespace lldb;
24*0b57cec5SDimitry Andric using namespace lldb_private;
25*0b57cec5SDimitry Andric 
26*0b57cec5SDimitry Andric // ThreadPlanBase: This one always stops, and never has anything particular to
27*0b57cec5SDimitry Andric // do.
28*0b57cec5SDimitry Andric // FIXME: The "signal handling" policies should probably go here.
29*0b57cec5SDimitry Andric 
ThreadPlanBase(Thread & thread)30*0b57cec5SDimitry Andric ThreadPlanBase::ThreadPlanBase(Thread &thread)
31*0b57cec5SDimitry Andric     : ThreadPlan(ThreadPlan::eKindBase, "base plan", thread, eVoteYes,
32*0b57cec5SDimitry Andric                  eVoteNoOpinion) {
33*0b57cec5SDimitry Andric // Set the tracer to a default tracer.
34*0b57cec5SDimitry Andric // FIXME: need to add a thread settings variable to pix various tracers...
35*0b57cec5SDimitry Andric #define THREAD_PLAN_USE_ASSEMBLY_TRACER 1
36*0b57cec5SDimitry Andric 
37*0b57cec5SDimitry Andric #ifdef THREAD_PLAN_USE_ASSEMBLY_TRACER
38*0b57cec5SDimitry Andric   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanAssemblyTracer(thread));
39*0b57cec5SDimitry Andric #else
40*0b57cec5SDimitry Andric   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanTracer(m_thread));
41*0b57cec5SDimitry Andric #endif
42*0b57cec5SDimitry Andric   new_tracer_sp->EnableTracing(thread.GetTraceEnabledState());
43*0b57cec5SDimitry Andric   SetThreadPlanTracer(new_tracer_sp);
44*0b57cec5SDimitry Andric   SetIsControllingPlan(true);
45*0b57cec5SDimitry Andric }
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric ThreadPlanBase::~ThreadPlanBase() = default;
48*0b57cec5SDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)49*0b57cec5SDimitry Andric void ThreadPlanBase::GetDescription(Stream *s, lldb::DescriptionLevel level) {
50*0b57cec5SDimitry Andric   s->Printf("Base thread plan.");
51*0b57cec5SDimitry Andric }
52*0b57cec5SDimitry Andric 
ValidatePlan(Stream * error)53*0b57cec5SDimitry Andric bool ThreadPlanBase::ValidatePlan(Stream *error) { return true; }
54*0b57cec5SDimitry Andric 
DoPlanExplainsStop(Event * event_ptr)55*0b57cec5SDimitry Andric bool ThreadPlanBase::DoPlanExplainsStop(Event *event_ptr) {
56*0b57cec5SDimitry Andric   // The base plan should defer to its tracer, since by default it always
57*0b57cec5SDimitry Andric   // handles the stop.
58*0b57cec5SDimitry Andric   return !TracerExplainsStop();
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric 
ShouldReportStop(Event * event_ptr)61*0b57cec5SDimitry Andric Vote ThreadPlanBase::ShouldReportStop(Event *event_ptr) {
62*0b57cec5SDimitry Andric   StopInfoSP stop_info_sp = GetThread().GetStopInfo();
63*0b57cec5SDimitry Andric   if (stop_info_sp) {
64*0b57cec5SDimitry Andric     bool should_notify = stop_info_sp->ShouldNotify(event_ptr);
65*0b57cec5SDimitry Andric     if (should_notify)
66*0b57cec5SDimitry Andric       return eVoteYes;
67*0b57cec5SDimitry Andric     else
68*0b57cec5SDimitry Andric       return eVoteNoOpinion;
69*0b57cec5SDimitry Andric   } else
70*0b57cec5SDimitry Andric     return eVoteNoOpinion;
71*0b57cec5SDimitry Andric }
72*0b57cec5SDimitry Andric 
ShouldStop(Event * event_ptr)73*0b57cec5SDimitry Andric bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
74*0b57cec5SDimitry Andric   m_report_stop_vote = eVoteYes;
75*0b57cec5SDimitry Andric   m_report_run_vote = eVoteYes;
76*0b57cec5SDimitry Andric 
77*0b57cec5SDimitry Andric   Log *log = GetLog(LLDBLog::Step);
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric   StopInfoSP stop_info_sp = GetPrivateStopInfo();
80*0b57cec5SDimitry Andric   if (stop_info_sp) {
81*0b57cec5SDimitry Andric     StopReason reason = stop_info_sp->GetStopReason();
82*0b57cec5SDimitry Andric     switch (reason) {
83*0b57cec5SDimitry Andric     case eStopReasonInvalid:
84*0b57cec5SDimitry Andric     case eStopReasonNone:
85*0b57cec5SDimitry Andric       // This
86*0b57cec5SDimitry Andric       m_report_run_vote = eVoteNoOpinion;
87*0b57cec5SDimitry Andric       m_report_stop_vote = eVoteNo;
88*0b57cec5SDimitry Andric       return false;
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric     case eStopReasonBreakpoint:
91*0b57cec5SDimitry Andric     case eStopReasonWatchpoint:
92*0b57cec5SDimitry Andric       if (stop_info_sp->ShouldStopSynchronous(event_ptr)) {
93*0b57cec5SDimitry Andric         // If we are going to stop for a breakpoint, then unship the other
94*0b57cec5SDimitry Andric         // plans at this point.  Don't force the discard, however, so
95*0b57cec5SDimitry Andric         // Controlling plans can stay in place if they want to.
96*0b57cec5SDimitry Andric         LLDB_LOGF(
97*0b57cec5SDimitry Andric             log,
98*0b57cec5SDimitry Andric             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
99*0b57cec5SDimitry Andric             " (breakpoint hit.)",
100*0b57cec5SDimitry Andric             m_tid);
101*0b57cec5SDimitry Andric         GetThread().DiscardThreadPlans(false);
102*0b57cec5SDimitry Andric         return true;
103*0b57cec5SDimitry Andric       }
104*0b57cec5SDimitry Andric       // If we aren't going to stop at this breakpoint, and it is internal,
105*0b57cec5SDimitry Andric       // don't report this stop or the subsequent running event. Otherwise we
106*0b57cec5SDimitry Andric       // will post the stopped & running, but the stopped event will get marked
107*0b57cec5SDimitry Andric       // with "restarted" so the UI will know to wait and expect the consequent
108*0b57cec5SDimitry Andric       // "running".
109*0b57cec5SDimitry Andric       if (stop_info_sp->ShouldNotify(event_ptr)) {
110*0b57cec5SDimitry Andric         m_report_stop_vote = eVoteYes;
111*0b57cec5SDimitry Andric         m_report_run_vote = eVoteYes;
112*0b57cec5SDimitry Andric       } else {
113*0b57cec5SDimitry Andric         m_report_stop_vote = eVoteNo;
114*0b57cec5SDimitry Andric         m_report_run_vote = eVoteNo;
115*0b57cec5SDimitry Andric       }
116*0b57cec5SDimitry Andric       return false;
117*0b57cec5SDimitry Andric 
118*0b57cec5SDimitry Andric       // TODO: the break below was missing, was this intentional??? If so
119*0b57cec5SDimitry Andric       // please mention it
120*0b57cec5SDimitry Andric       break;
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric     case eStopReasonException:
123*0b57cec5SDimitry Andric       // If we crashed, discard thread plans and stop.  Don't force the
124*0b57cec5SDimitry Andric       // discard, however, since on rerun the target may clean up this
125*0b57cec5SDimitry Andric       // exception and continue normally from there.
126*0b57cec5SDimitry Andric       LLDB_LOGF(
127*0b57cec5SDimitry Andric           log,
128*0b57cec5SDimitry Andric           "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
129*0b57cec5SDimitry Andric           " (exception: %s)",
130*0b57cec5SDimitry Andric           m_tid, stop_info_sp->GetDescription());
131*0b57cec5SDimitry Andric       GetThread().DiscardThreadPlans(false);
132*0b57cec5SDimitry Andric       return true;
133*0b57cec5SDimitry Andric 
134*0b57cec5SDimitry Andric     case eStopReasonExec:
135*0b57cec5SDimitry Andric       // If we crashed, discard thread plans and stop.  Don't force the
136*0b57cec5SDimitry Andric       // discard, however, since on rerun the target may clean up this
137*0b57cec5SDimitry Andric       // exception and continue normally from there.
138*0b57cec5SDimitry Andric       LLDB_LOGF(
139*0b57cec5SDimitry Andric           log,
140*0b57cec5SDimitry Andric           "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
141*0b57cec5SDimitry Andric           " (exec.)",
142*0b57cec5SDimitry Andric           m_tid);
143*0b57cec5SDimitry Andric       GetThread().DiscardThreadPlans(false);
144*0b57cec5SDimitry Andric       return true;
145*0b57cec5SDimitry Andric 
146*0b57cec5SDimitry Andric     case eStopReasonThreadExiting:
147*0b57cec5SDimitry Andric     case eStopReasonSignal:
148*0b57cec5SDimitry Andric       if (stop_info_sp->ShouldStop(event_ptr)) {
149*0b57cec5SDimitry Andric         LLDB_LOGF(
150*0b57cec5SDimitry Andric             log,
151*0b57cec5SDimitry Andric             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
152*0b57cec5SDimitry Andric             " (signal: %s)",
153*0b57cec5SDimitry Andric             m_tid, stop_info_sp->GetDescription());
154*0b57cec5SDimitry Andric         GetThread().DiscardThreadPlans(false);
155*0b57cec5SDimitry Andric         return true;
156*0b57cec5SDimitry Andric       } else {
157*0b57cec5SDimitry Andric         // We're not going to stop, but while we are here, let's figure out
158*0b57cec5SDimitry Andric         // whether to report this.
159*0b57cec5SDimitry Andric         if (stop_info_sp->ShouldNotify(event_ptr))
160*0b57cec5SDimitry Andric           m_report_stop_vote = eVoteYes;
161*0b57cec5SDimitry Andric         else
162*0b57cec5SDimitry Andric           m_report_stop_vote = eVoteNo;
163*0b57cec5SDimitry Andric       }
164*0b57cec5SDimitry Andric       return false;
165*0b57cec5SDimitry Andric 
166*0b57cec5SDimitry Andric     default:
167*0b57cec5SDimitry Andric       return true;
168*0b57cec5SDimitry Andric     }
169*0b57cec5SDimitry Andric 
170*0b57cec5SDimitry Andric   } else {
171*0b57cec5SDimitry Andric     m_report_run_vote = eVoteNoOpinion;
172*0b57cec5SDimitry Andric     m_report_stop_vote = eVoteNo;
173*0b57cec5SDimitry Andric   }
174*0b57cec5SDimitry Andric 
175*0b57cec5SDimitry Andric   // If there's no explicit reason to stop, then we will continue.
176*0b57cec5SDimitry Andric   return false;
177*0b57cec5SDimitry Andric }
178*0b57cec5SDimitry Andric 
StopOthers()179*0b57cec5SDimitry Andric bool ThreadPlanBase::StopOthers() { return false; }
180*0b57cec5SDimitry Andric 
GetPlanRunState()181*0b57cec5SDimitry Andric StateType ThreadPlanBase::GetPlanRunState() { return eStateRunning; }
182*0b57cec5SDimitry Andric 
WillStop()183*0b57cec5SDimitry Andric bool ThreadPlanBase::WillStop() { return true; }
184*0b57cec5SDimitry Andric 
DoWillResume(lldb::StateType resume_state,bool current_plan)185*0b57cec5SDimitry Andric bool ThreadPlanBase::DoWillResume(lldb::StateType resume_state,
186*0b57cec5SDimitry Andric                                   bool current_plan) {
187*0b57cec5SDimitry Andric   // Reset these to the default values so we don't set them wrong, then not get
188*0b57cec5SDimitry Andric   // asked for a while, then return the wrong answer.
189*0b57cec5SDimitry Andric   m_report_run_vote = eVoteNoOpinion;
190*0b57cec5SDimitry Andric   m_report_stop_vote = eVoteNo;
191*0b57cec5SDimitry Andric   return true;
192*0b57cec5SDimitry Andric }
193*0b57cec5SDimitry Andric 
194*0b57cec5SDimitry Andric // The base plan is never done.
MischiefManaged()195*0b57cec5SDimitry Andric bool ThreadPlanBase::MischiefManaged() {
196*0b57cec5SDimitry Andric   // The base plan is never done.
197*0b57cec5SDimitry Andric   return false;
198 }
199