180814287SRaphael Isemann //===-- ThreadPlanBase.cpp ------------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
930fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanBase.h"
1030fdc8d8SChris Lattner 
1130fdc8d8SChris Lattner //
1230fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
13b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h"
14b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointSite.h"
15b9c1b51eSKate Stone #include "lldb/Breakpoint/StoppointCallbackContext.h"
1630fdc8d8SChris Lattner #include "lldb/Target/Process.h"
1730fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
18f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
196f9e6901SZachary Turner #include "lldb/Utility/Log.h"
20bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2130fdc8d8SChris Lattner 
2230fdc8d8SChris Lattner using namespace lldb;
2330fdc8d8SChris Lattner using namespace lldb_private;
2430fdc8d8SChris Lattner 
2505097246SAdrian Prantl // ThreadPlanBase: This one always stops, and never has anything particular to
2605097246SAdrian Prantl // do.
2730fdc8d8SChris Lattner // FIXME: The "signal handling" policies should probably go here.
2830fdc8d8SChris Lattner 
29b9c1b51eSKate Stone ThreadPlanBase::ThreadPlanBase(Thread &thread)
30b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindBase, "base plan", thread, eVoteYes,
31b9c1b51eSKate Stone                  eVoteNoOpinion) {
3206e827ccSJim Ingham // Set the tracer to a default tracer.
33773d981cSJim Ingham // FIXME: need to add a thread settings variable to pix various tracers...
34773d981cSJim Ingham #define THREAD_PLAN_USE_ASSEMBLY_TRACER 1
35773d981cSJim Ingham 
36773d981cSJim Ingham #ifdef THREAD_PLAN_USE_ASSEMBLY_TRACER
37e4598dc0SJim Ingham   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanAssemblyTracer(thread));
38773d981cSJim Ingham #else
3906e827ccSJim Ingham   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanTracer(m_thread));
40773d981cSJim Ingham #endif
41e4598dc0SJim Ingham   new_tracer_sp->EnableTracing(thread.GetTraceEnabledState());
4206e827ccSJim Ingham   SetThreadPlanTracer(new_tracer_sp);
43*04cbfa95SQuinn Pham   SetIsControllingPlan(true);
4430fdc8d8SChris Lattner }
4530fdc8d8SChris Lattner 
46fd2433e1SJonas Devlieghere ThreadPlanBase::~ThreadPlanBase() = default;
4730fdc8d8SChris Lattner 
48b9c1b51eSKate Stone void ThreadPlanBase::GetDescription(Stream *s, lldb::DescriptionLevel level) {
4930fdc8d8SChris Lattner   s->Printf("Base thread plan.");
5030fdc8d8SChris Lattner }
5130fdc8d8SChris Lattner 
52b9c1b51eSKate Stone bool ThreadPlanBase::ValidatePlan(Stream *error) { return true; }
5330fdc8d8SChris Lattner 
54b9c1b51eSKate Stone bool ThreadPlanBase::DoPlanExplainsStop(Event *event_ptr) {
5505097246SAdrian Prantl   // The base plan should defer to its tracer, since by default it always
5605097246SAdrian Prantl   // handles the stop.
57a6682a41SJonas Devlieghere   return !TracerExplainsStop();
5830fdc8d8SChris Lattner }
5930fdc8d8SChris Lattner 
60b9c1b51eSKate Stone Vote ThreadPlanBase::ShouldReportStop(Event *event_ptr) {
61e4598dc0SJim Ingham   StopInfoSP stop_info_sp = GetThread().GetStopInfo();
62b9c1b51eSKate Stone   if (stop_info_sp) {
63221d51cfSJim Ingham     bool should_notify = stop_info_sp->ShouldNotify(event_ptr);
64221d51cfSJim Ingham     if (should_notify)
65221d51cfSJim Ingham       return eVoteYes;
66221d51cfSJim Ingham     else
67221d51cfSJim Ingham       return eVoteNoOpinion;
68b9c1b51eSKate Stone   } else
69221d51cfSJim Ingham     return eVoteNoOpinion;
70221d51cfSJim Ingham }
71221d51cfSJim Ingham 
72b9c1b51eSKate Stone bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
739d3b9e57SDave Lee   m_report_stop_vote = eVoteYes;
749d3b9e57SDave Lee   m_report_run_vote = eVoteYes;
7530fdc8d8SChris Lattner 
765160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
770f16e73aSJim Ingham 
7860c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
79b9c1b51eSKate Stone   if (stop_info_sp) {
80b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
81b9c1b51eSKate Stone     switch (reason) {
8230fdc8d8SChris Lattner     case eStopReasonInvalid:
8330fdc8d8SChris Lattner     case eStopReasonNone:
84444586b5SJim Ingham       // This
859d3b9e57SDave Lee       m_report_run_vote = eVoteNoOpinion;
869d3b9e57SDave Lee       m_report_stop_vote = eVoteNo;
8730fdc8d8SChris Lattner       return false;
88f4b47e15SGreg Clayton 
8930fdc8d8SChris Lattner     case eStopReasonBreakpoint:
90fd158f41SJohnny Chen     case eStopReasonWatchpoint:
91b9c1b51eSKate Stone       if (stop_info_sp->ShouldStopSynchronous(event_ptr)) {
9205097246SAdrian Prantl         // If we are going to stop for a breakpoint, then unship the other
93*04cbfa95SQuinn Pham         // plans at this point.  Don't force the discard, however, so
94*04cbfa95SQuinn Pham         // Controlling plans can stay in place if they want to.
9563e5fb76SJonas Devlieghere         LLDB_LOGF(
9663e5fb76SJonas Devlieghere             log,
97b9c1b51eSKate Stone             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
98b9c1b51eSKate Stone             " (breakpoint hit.)",
99e4598dc0SJim Ingham             m_tid);
100e4598dc0SJim Ingham         GetThread().DiscardThreadPlans(false);
101f4b47e15SGreg Clayton         return true;
10230fdc8d8SChris Lattner       }
103f4b47e15SGreg Clayton       // If we aren't going to stop at this breakpoint, and it is internal,
10405097246SAdrian Prantl       // don't report this stop or the subsequent running event. Otherwise we
10505097246SAdrian Prantl       // will post the stopped & running, but the stopped event will get marked
106b9c1b51eSKate Stone       // with "restarted" so the UI will know to wait and expect the consequent
107b9c1b51eSKate Stone       // "running".
108b9c1b51eSKate Stone       if (stop_info_sp->ShouldNotify(event_ptr)) {
1099d3b9e57SDave Lee         m_report_stop_vote = eVoteYes;
1109d3b9e57SDave Lee         m_report_run_vote = eVoteYes;
111b9c1b51eSKate Stone       } else {
1129d3b9e57SDave Lee         m_report_stop_vote = eVoteNo;
1139d3b9e57SDave Lee         m_report_run_vote = eVoteNo;
114f4b47e15SGreg Clayton       }
115f4b47e15SGreg Clayton       return false;
11630fdc8d8SChris Lattner 
117f4b47e15SGreg Clayton       // TODO: the break below was missing, was this intentional??? If so
118f4b47e15SGreg Clayton       // please mention it
119f4b47e15SGreg Clayton       break;
120f4b47e15SGreg Clayton 
12130fdc8d8SChris Lattner     case eStopReasonException:
12205097246SAdrian Prantl       // If we crashed, discard thread plans and stop.  Don't force the
12305097246SAdrian Prantl       // discard, however, since on rerun the target may clean up this
12405097246SAdrian Prantl       // exception and continue normally from there.
12563e5fb76SJonas Devlieghere       LLDB_LOGF(
12663e5fb76SJonas Devlieghere           log,
127b9c1b51eSKate Stone           "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
128b9c1b51eSKate Stone           " (exception: %s)",
129e4598dc0SJim Ingham           m_tid, stop_info_sp->GetDescription());
130e4598dc0SJim Ingham       GetThread().DiscardThreadPlans(false);
13130fdc8d8SChris Lattner       return true;
132f4b47e15SGreg Clayton 
13390ba8115SGreg Clayton     case eStopReasonExec:
13405097246SAdrian Prantl       // If we crashed, discard thread plans and stop.  Don't force the
13505097246SAdrian Prantl       // discard, however, since on rerun the target may clean up this
13605097246SAdrian Prantl       // exception and continue normally from there.
13763e5fb76SJonas Devlieghere       LLDB_LOGF(
13863e5fb76SJonas Devlieghere           log,
139b9c1b51eSKate Stone           "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
140b9c1b51eSKate Stone           " (exec.)",
141e4598dc0SJim Ingham           m_tid);
142e4598dc0SJim Ingham       GetThread().DiscardThreadPlans(false);
14390ba8115SGreg Clayton       return true;
14490ba8115SGreg Clayton 
145f85defaeSAndrew Kaylor     case eStopReasonThreadExiting:
14630fdc8d8SChris Lattner     case eStopReasonSignal:
147b9c1b51eSKate Stone       if (stop_info_sp->ShouldStop(event_ptr)) {
14863e5fb76SJonas Devlieghere         LLDB_LOGF(
14963e5fb76SJonas Devlieghere             log,
150b9c1b51eSKate Stone             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
151b9c1b51eSKate Stone             " (signal: %s)",
152e4598dc0SJim Ingham             m_tid, stop_info_sp->GetDescription());
153e4598dc0SJim Ingham         GetThread().DiscardThreadPlans(false);
15430fdc8d8SChris Lattner         return true;
155b9c1b51eSKate Stone       } else {
15630fdc8d8SChris Lattner         // We're not going to stop, but while we are here, let's figure out
15730fdc8d8SChris Lattner         // whether to report this.
158b15bfc75SJim Ingham         if (stop_info_sp->ShouldNotify(event_ptr))
1599d3b9e57SDave Lee           m_report_stop_vote = eVoteYes;
16030fdc8d8SChris Lattner         else
1619d3b9e57SDave Lee           m_report_stop_vote = eVoteNo;
162f4b47e15SGreg Clayton       }
16330fdc8d8SChris Lattner       return false;
164f4b47e15SGreg Clayton 
16530fdc8d8SChris Lattner     default:
16630fdc8d8SChris Lattner       return true;
16730fdc8d8SChris Lattner     }
16830fdc8d8SChris Lattner 
169b9c1b51eSKate Stone   } else {
1709d3b9e57SDave Lee     m_report_run_vote = eVoteNoOpinion;
1719d3b9e57SDave Lee     m_report_stop_vote = eVoteNo;
172f4b47e15SGreg Clayton   }
17330fdc8d8SChris Lattner 
17430fdc8d8SChris Lattner   // If there's no explicit reason to stop, then we will continue.
17530fdc8d8SChris Lattner   return false;
17630fdc8d8SChris Lattner }
17730fdc8d8SChris Lattner 
178b9c1b51eSKate Stone bool ThreadPlanBase::StopOthers() { return false; }
17930fdc8d8SChris Lattner 
180b9c1b51eSKate Stone StateType ThreadPlanBase::GetPlanRunState() { return eStateRunning; }
18130fdc8d8SChris Lattner 
182b9c1b51eSKate Stone bool ThreadPlanBase::WillStop() { return true; }
18330fdc8d8SChris Lattner 
184b9c1b51eSKate Stone bool ThreadPlanBase::DoWillResume(lldb::StateType resume_state,
185b9c1b51eSKate Stone                                   bool current_plan) {
186b9c1b51eSKate Stone   // Reset these to the default values so we don't set them wrong, then not get
18705097246SAdrian Prantl   // asked for a while, then return the wrong answer.
1889d3b9e57SDave Lee   m_report_run_vote = eVoteNoOpinion;
1899d3b9e57SDave Lee   m_report_stop_vote = eVoteNo;
190444586b5SJim Ingham   return true;
191444586b5SJim Ingham }
192444586b5SJim Ingham 
19330fdc8d8SChris Lattner // The base plan is never done.
194b9c1b51eSKate Stone bool ThreadPlanBase::MischiefManaged() {
19530fdc8d8SChris Lattner   // The base plan is never done.
19630fdc8d8SChris Lattner   return false;
19730fdc8d8SChris Lattner }
198