15ffd83dbSDimitry Andric //===-- ThreadPlanBase.cpp ------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanBase.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
130b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
140b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointSite.h"
150b57cec5SDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
160b57cec5SDimitry Andric #include "lldb/Target/Process.h"
170b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
180b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
190b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
200b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace lldb;
230b57cec5SDimitry Andric using namespace lldb_private;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric // ThreadPlanBase: This one always stops, and never has anything particular to
260b57cec5SDimitry Andric // do.
270b57cec5SDimitry Andric // FIXME: The "signal handling" policies should probably go here.
280b57cec5SDimitry Andric 
ThreadPlanBase(Thread & thread)290b57cec5SDimitry Andric ThreadPlanBase::ThreadPlanBase(Thread &thread)
300b57cec5SDimitry Andric     : ThreadPlan(ThreadPlan::eKindBase, "base plan", thread, eVoteYes,
310b57cec5SDimitry Andric                  eVoteNoOpinion) {
320b57cec5SDimitry Andric // Set the tracer to a default tracer.
330b57cec5SDimitry Andric // FIXME: need to add a thread settings variable to pix various tracers...
340b57cec5SDimitry Andric #define THREAD_PLAN_USE_ASSEMBLY_TRACER 1
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric #ifdef THREAD_PLAN_USE_ASSEMBLY_TRACER
375ffd83dbSDimitry Andric   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanAssemblyTracer(thread));
380b57cec5SDimitry Andric #else
390b57cec5SDimitry Andric   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanTracer(m_thread));
400b57cec5SDimitry Andric #endif
415ffd83dbSDimitry Andric   new_tracer_sp->EnableTracing(thread.GetTraceEnabledState());
420b57cec5SDimitry Andric   SetThreadPlanTracer(new_tracer_sp);
430b57cec5SDimitry Andric   SetIsMasterPlan(true);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
46*5f7ddb14SDimitry Andric ThreadPlanBase::~ThreadPlanBase() = default;
470b57cec5SDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)480b57cec5SDimitry Andric void ThreadPlanBase::GetDescription(Stream *s, lldb::DescriptionLevel level) {
490b57cec5SDimitry Andric   s->Printf("Base thread plan.");
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric 
ValidatePlan(Stream * error)520b57cec5SDimitry Andric bool ThreadPlanBase::ValidatePlan(Stream *error) { return true; }
530b57cec5SDimitry Andric 
DoPlanExplainsStop(Event * event_ptr)540b57cec5SDimitry Andric bool ThreadPlanBase::DoPlanExplainsStop(Event *event_ptr) {
550b57cec5SDimitry Andric   // The base plan should defer to its tracer, since by default it always
560b57cec5SDimitry Andric   // handles the stop.
570b57cec5SDimitry Andric   return !TracerExplainsStop();
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric 
ShouldReportStop(Event * event_ptr)600b57cec5SDimitry Andric Vote ThreadPlanBase::ShouldReportStop(Event *event_ptr) {
615ffd83dbSDimitry Andric   StopInfoSP stop_info_sp = GetThread().GetStopInfo();
620b57cec5SDimitry Andric   if (stop_info_sp) {
630b57cec5SDimitry Andric     bool should_notify = stop_info_sp->ShouldNotify(event_ptr);
640b57cec5SDimitry Andric     if (should_notify)
650b57cec5SDimitry Andric       return eVoteYes;
660b57cec5SDimitry Andric     else
670b57cec5SDimitry Andric       return eVoteNoOpinion;
680b57cec5SDimitry Andric   } else
690b57cec5SDimitry Andric     return eVoteNoOpinion;
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
ShouldStop(Event * event_ptr)720b57cec5SDimitry Andric bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
73*5f7ddb14SDimitry Andric   m_report_stop_vote = eVoteYes;
74*5f7ddb14SDimitry Andric   m_report_run_vote = eVoteYes;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   StopInfoSP stop_info_sp = GetPrivateStopInfo();
790b57cec5SDimitry Andric   if (stop_info_sp) {
800b57cec5SDimitry Andric     StopReason reason = stop_info_sp->GetStopReason();
810b57cec5SDimitry Andric     switch (reason) {
820b57cec5SDimitry Andric     case eStopReasonInvalid:
830b57cec5SDimitry Andric     case eStopReasonNone:
840b57cec5SDimitry Andric       // This
85*5f7ddb14SDimitry Andric       m_report_run_vote = eVoteNoOpinion;
86*5f7ddb14SDimitry Andric       m_report_stop_vote = eVoteNo;
870b57cec5SDimitry Andric       return false;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric     case eStopReasonBreakpoint:
900b57cec5SDimitry Andric     case eStopReasonWatchpoint:
910b57cec5SDimitry Andric       if (stop_info_sp->ShouldStopSynchronous(event_ptr)) {
920b57cec5SDimitry Andric         // If we are going to stop for a breakpoint, then unship the other
930b57cec5SDimitry Andric         // plans at this point.  Don't force the discard, however, so Master
940b57cec5SDimitry Andric         // plans can stay in place if they want to.
959dba64beSDimitry Andric         LLDB_LOGF(
969dba64beSDimitry Andric             log,
970b57cec5SDimitry Andric             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
980b57cec5SDimitry Andric             " (breakpoint hit.)",
995ffd83dbSDimitry Andric             m_tid);
1005ffd83dbSDimitry Andric         GetThread().DiscardThreadPlans(false);
1010b57cec5SDimitry Andric         return true;
1020b57cec5SDimitry Andric       }
1030b57cec5SDimitry Andric       // If we aren't going to stop at this breakpoint, and it is internal,
1040b57cec5SDimitry Andric       // don't report this stop or the subsequent running event. Otherwise we
1050b57cec5SDimitry Andric       // will post the stopped & running, but the stopped event will get marked
1060b57cec5SDimitry Andric       // with "restarted" so the UI will know to wait and expect the consequent
1070b57cec5SDimitry Andric       // "running".
1080b57cec5SDimitry Andric       if (stop_info_sp->ShouldNotify(event_ptr)) {
109*5f7ddb14SDimitry Andric         m_report_stop_vote = eVoteYes;
110*5f7ddb14SDimitry Andric         m_report_run_vote = eVoteYes;
1110b57cec5SDimitry Andric       } else {
112*5f7ddb14SDimitry Andric         m_report_stop_vote = eVoteNo;
113*5f7ddb14SDimitry Andric         m_report_run_vote = eVoteNo;
1140b57cec5SDimitry Andric       }
1150b57cec5SDimitry Andric       return false;
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric       // TODO: the break below was missing, was this intentional??? If so
1180b57cec5SDimitry Andric       // please mention it
1190b57cec5SDimitry Andric       break;
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric     case eStopReasonException:
1220b57cec5SDimitry Andric       // If we crashed, discard thread plans and stop.  Don't force the
1230b57cec5SDimitry Andric       // discard, however, since on rerun the target may clean up this
1240b57cec5SDimitry Andric       // exception and continue normally from there.
1259dba64beSDimitry Andric       LLDB_LOGF(
1269dba64beSDimitry Andric           log,
1270b57cec5SDimitry Andric           "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
1280b57cec5SDimitry Andric           " (exception: %s)",
1295ffd83dbSDimitry Andric           m_tid, stop_info_sp->GetDescription());
1305ffd83dbSDimitry Andric       GetThread().DiscardThreadPlans(false);
1310b57cec5SDimitry Andric       return true;
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric     case eStopReasonExec:
1340b57cec5SDimitry Andric       // If we crashed, discard thread plans and stop.  Don't force the
1350b57cec5SDimitry Andric       // discard, however, since on rerun the target may clean up this
1360b57cec5SDimitry Andric       // exception and continue normally from there.
1379dba64beSDimitry Andric       LLDB_LOGF(
1389dba64beSDimitry Andric           log,
1390b57cec5SDimitry Andric           "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
1400b57cec5SDimitry Andric           " (exec.)",
1415ffd83dbSDimitry Andric           m_tid);
1425ffd83dbSDimitry Andric       GetThread().DiscardThreadPlans(false);
1430b57cec5SDimitry Andric       return true;
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric     case eStopReasonThreadExiting:
1460b57cec5SDimitry Andric     case eStopReasonSignal:
1470b57cec5SDimitry Andric       if (stop_info_sp->ShouldStop(event_ptr)) {
1489dba64beSDimitry Andric         LLDB_LOGF(
1499dba64beSDimitry Andric             log,
1500b57cec5SDimitry Andric             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
1510b57cec5SDimitry Andric             " (signal: %s)",
1525ffd83dbSDimitry Andric             m_tid, stop_info_sp->GetDescription());
1535ffd83dbSDimitry Andric         GetThread().DiscardThreadPlans(false);
1540b57cec5SDimitry Andric         return true;
1550b57cec5SDimitry Andric       } else {
1560b57cec5SDimitry Andric         // We're not going to stop, but while we are here, let's figure out
1570b57cec5SDimitry Andric         // whether to report this.
1580b57cec5SDimitry Andric         if (stop_info_sp->ShouldNotify(event_ptr))
159*5f7ddb14SDimitry Andric           m_report_stop_vote = eVoteYes;
1600b57cec5SDimitry Andric         else
161*5f7ddb14SDimitry Andric           m_report_stop_vote = eVoteNo;
1620b57cec5SDimitry Andric       }
1630b57cec5SDimitry Andric       return false;
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric     default:
1660b57cec5SDimitry Andric       return true;
1670b57cec5SDimitry Andric     }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   } else {
170*5f7ddb14SDimitry Andric     m_report_run_vote = eVoteNoOpinion;
171*5f7ddb14SDimitry Andric     m_report_stop_vote = eVoteNo;
1720b57cec5SDimitry Andric   }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   // If there's no explicit reason to stop, then we will continue.
1750b57cec5SDimitry Andric   return false;
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric 
StopOthers()1780b57cec5SDimitry Andric bool ThreadPlanBase::StopOthers() { return false; }
1790b57cec5SDimitry Andric 
GetPlanRunState()1800b57cec5SDimitry Andric StateType ThreadPlanBase::GetPlanRunState() { return eStateRunning; }
1810b57cec5SDimitry Andric 
WillStop()1820b57cec5SDimitry Andric bool ThreadPlanBase::WillStop() { return true; }
1830b57cec5SDimitry Andric 
DoWillResume(lldb::StateType resume_state,bool current_plan)1840b57cec5SDimitry Andric bool ThreadPlanBase::DoWillResume(lldb::StateType resume_state,
1850b57cec5SDimitry Andric                                   bool current_plan) {
1860b57cec5SDimitry Andric   // Reset these to the default values so we don't set them wrong, then not get
1870b57cec5SDimitry Andric   // asked for a while, then return the wrong answer.
188*5f7ddb14SDimitry Andric   m_report_run_vote = eVoteNoOpinion;
189*5f7ddb14SDimitry Andric   m_report_stop_vote = eVoteNo;
1900b57cec5SDimitry Andric   return true;
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric // The base plan is never done.
MischiefManaged()1940b57cec5SDimitry Andric bool ThreadPlanBase::MischiefManaged() {
1950b57cec5SDimitry Andric   // The base plan is never done.
1960b57cec5SDimitry Andric   return false;
1970b57cec5SDimitry Andric }
198