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"
19*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
206f9e6901SZachary Turner #include "lldb/Utility/Log.h"
21bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2230fdc8d8SChris Lattner
2330fdc8d8SChris Lattner using namespace lldb;
2430fdc8d8SChris Lattner using namespace lldb_private;
2530fdc8d8SChris Lattner
2605097246SAdrian Prantl // ThreadPlanBase: This one always stops, and never has anything particular to
2705097246SAdrian Prantl // do.
2830fdc8d8SChris Lattner // FIXME: The "signal handling" policies should probably go here.
2930fdc8d8SChris Lattner
ThreadPlanBase(Thread & thread)30b9c1b51eSKate Stone ThreadPlanBase::ThreadPlanBase(Thread &thread)
31b9c1b51eSKate Stone : ThreadPlan(ThreadPlan::eKindBase, "base plan", thread, eVoteYes,
32b9c1b51eSKate Stone eVoteNoOpinion) {
3306e827ccSJim Ingham // Set the tracer to a default tracer.
34773d981cSJim Ingham // FIXME: need to add a thread settings variable to pix various tracers...
35773d981cSJim Ingham #define THREAD_PLAN_USE_ASSEMBLY_TRACER 1
36773d981cSJim Ingham
37773d981cSJim Ingham #ifdef THREAD_PLAN_USE_ASSEMBLY_TRACER
38e4598dc0SJim Ingham ThreadPlanTracerSP new_tracer_sp(new ThreadPlanAssemblyTracer(thread));
39773d981cSJim Ingham #else
4006e827ccSJim Ingham ThreadPlanTracerSP new_tracer_sp(new ThreadPlanTracer(m_thread));
41773d981cSJim Ingham #endif
42e4598dc0SJim Ingham new_tracer_sp->EnableTracing(thread.GetTraceEnabledState());
4306e827ccSJim Ingham SetThreadPlanTracer(new_tracer_sp);
4404cbfa95SQuinn Pham SetIsControllingPlan(true);
4530fdc8d8SChris Lattner }
4630fdc8d8SChris Lattner
47fd2433e1SJonas Devlieghere ThreadPlanBase::~ThreadPlanBase() = default;
4830fdc8d8SChris Lattner
GetDescription(Stream * s,lldb::DescriptionLevel level)49b9c1b51eSKate Stone void ThreadPlanBase::GetDescription(Stream *s, lldb::DescriptionLevel level) {
5030fdc8d8SChris Lattner s->Printf("Base thread plan.");
5130fdc8d8SChris Lattner }
5230fdc8d8SChris Lattner
ValidatePlan(Stream * error)53b9c1b51eSKate Stone bool ThreadPlanBase::ValidatePlan(Stream *error) { return true; }
5430fdc8d8SChris Lattner
DoPlanExplainsStop(Event * event_ptr)55b9c1b51eSKate Stone bool ThreadPlanBase::DoPlanExplainsStop(Event *event_ptr) {
5605097246SAdrian Prantl // The base plan should defer to its tracer, since by default it always
5705097246SAdrian Prantl // handles the stop.
58a6682a41SJonas Devlieghere return !TracerExplainsStop();
5930fdc8d8SChris Lattner }
6030fdc8d8SChris Lattner
ShouldReportStop(Event * event_ptr)61b9c1b51eSKate Stone Vote ThreadPlanBase::ShouldReportStop(Event *event_ptr) {
62e4598dc0SJim Ingham StopInfoSP stop_info_sp = GetThread().GetStopInfo();
63b9c1b51eSKate Stone if (stop_info_sp) {
64221d51cfSJim Ingham bool should_notify = stop_info_sp->ShouldNotify(event_ptr);
65221d51cfSJim Ingham if (should_notify)
66221d51cfSJim Ingham return eVoteYes;
67221d51cfSJim Ingham else
68221d51cfSJim Ingham return eVoteNoOpinion;
69b9c1b51eSKate Stone } else
70221d51cfSJim Ingham return eVoteNoOpinion;
71221d51cfSJim Ingham }
72221d51cfSJim Ingham
ShouldStop(Event * event_ptr)73b9c1b51eSKate Stone bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
749d3b9e57SDave Lee m_report_stop_vote = eVoteYes;
759d3b9e57SDave Lee m_report_run_vote = eVoteYes;
7630fdc8d8SChris Lattner
77a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Step);
780f16e73aSJim Ingham
7960c4118cSJim Ingham StopInfoSP stop_info_sp = GetPrivateStopInfo();
80b9c1b51eSKate Stone if (stop_info_sp) {
81b15bfc75SJim Ingham StopReason reason = stop_info_sp->GetStopReason();
82b9c1b51eSKate Stone switch (reason) {
8330fdc8d8SChris Lattner case eStopReasonInvalid:
8430fdc8d8SChris Lattner case eStopReasonNone:
85444586b5SJim Ingham // This
869d3b9e57SDave Lee m_report_run_vote = eVoteNoOpinion;
879d3b9e57SDave Lee m_report_stop_vote = eVoteNo;
8830fdc8d8SChris Lattner return false;
89f4b47e15SGreg Clayton
9030fdc8d8SChris Lattner case eStopReasonBreakpoint:
91fd158f41SJohnny Chen case eStopReasonWatchpoint:
92b9c1b51eSKate Stone if (stop_info_sp->ShouldStopSynchronous(event_ptr)) {
9305097246SAdrian Prantl // If we are going to stop for a breakpoint, then unship the other
9404cbfa95SQuinn Pham // plans at this point. Don't force the discard, however, so
9504cbfa95SQuinn Pham // Controlling plans can stay in place if they want to.
9663e5fb76SJonas Devlieghere LLDB_LOGF(
9763e5fb76SJonas Devlieghere log,
98b9c1b51eSKate Stone "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
99b9c1b51eSKate Stone " (breakpoint hit.)",
100e4598dc0SJim Ingham m_tid);
101e4598dc0SJim Ingham GetThread().DiscardThreadPlans(false);
102f4b47e15SGreg Clayton return true;
10330fdc8d8SChris Lattner }
104f4b47e15SGreg Clayton // If we aren't going to stop at this breakpoint, and it is internal,
10505097246SAdrian Prantl // don't report this stop or the subsequent running event. Otherwise we
10605097246SAdrian Prantl // will post the stopped & running, but the stopped event will get marked
107b9c1b51eSKate Stone // with "restarted" so the UI will know to wait and expect the consequent
108b9c1b51eSKate Stone // "running".
109b9c1b51eSKate Stone if (stop_info_sp->ShouldNotify(event_ptr)) {
1109d3b9e57SDave Lee m_report_stop_vote = eVoteYes;
1119d3b9e57SDave Lee m_report_run_vote = eVoteYes;
112b9c1b51eSKate Stone } else {
1139d3b9e57SDave Lee m_report_stop_vote = eVoteNo;
1149d3b9e57SDave Lee m_report_run_vote = eVoteNo;
115f4b47e15SGreg Clayton }
116f4b47e15SGreg Clayton return false;
11730fdc8d8SChris Lattner
118f4b47e15SGreg Clayton // TODO: the break below was missing, was this intentional??? If so
119f4b47e15SGreg Clayton // please mention it
120f4b47e15SGreg Clayton break;
121f4b47e15SGreg Clayton
12230fdc8d8SChris Lattner case eStopReasonException:
12305097246SAdrian Prantl // If we crashed, discard thread plans and stop. Don't force the
12405097246SAdrian Prantl // discard, however, since on rerun the target may clean up this
12505097246SAdrian Prantl // exception and continue normally from there.
12663e5fb76SJonas Devlieghere LLDB_LOGF(
12763e5fb76SJonas Devlieghere log,
128b9c1b51eSKate Stone "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
129b9c1b51eSKate Stone " (exception: %s)",
130e4598dc0SJim Ingham m_tid, stop_info_sp->GetDescription());
131e4598dc0SJim Ingham GetThread().DiscardThreadPlans(false);
13230fdc8d8SChris Lattner return true;
133f4b47e15SGreg Clayton
13490ba8115SGreg Clayton case eStopReasonExec:
13505097246SAdrian Prantl // If we crashed, discard thread plans and stop. Don't force the
13605097246SAdrian Prantl // discard, however, since on rerun the target may clean up this
13705097246SAdrian Prantl // exception and continue normally from there.
13863e5fb76SJonas Devlieghere LLDB_LOGF(
13963e5fb76SJonas Devlieghere log,
140b9c1b51eSKate Stone "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
141b9c1b51eSKate Stone " (exec.)",
142e4598dc0SJim Ingham m_tid);
143e4598dc0SJim Ingham GetThread().DiscardThreadPlans(false);
14490ba8115SGreg Clayton return true;
14590ba8115SGreg Clayton
146f85defaeSAndrew Kaylor case eStopReasonThreadExiting:
14730fdc8d8SChris Lattner case eStopReasonSignal:
148b9c1b51eSKate Stone if (stop_info_sp->ShouldStop(event_ptr)) {
14963e5fb76SJonas Devlieghere LLDB_LOGF(
15063e5fb76SJonas Devlieghere log,
151b9c1b51eSKate Stone "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
152b9c1b51eSKate Stone " (signal: %s)",
153e4598dc0SJim Ingham m_tid, stop_info_sp->GetDescription());
154e4598dc0SJim Ingham GetThread().DiscardThreadPlans(false);
15530fdc8d8SChris Lattner return true;
156b9c1b51eSKate Stone } else {
15730fdc8d8SChris Lattner // We're not going to stop, but while we are here, let's figure out
15830fdc8d8SChris Lattner // whether to report this.
159b15bfc75SJim Ingham if (stop_info_sp->ShouldNotify(event_ptr))
1609d3b9e57SDave Lee m_report_stop_vote = eVoteYes;
16130fdc8d8SChris Lattner else
1629d3b9e57SDave Lee m_report_stop_vote = eVoteNo;
163f4b47e15SGreg Clayton }
16430fdc8d8SChris Lattner return false;
165f4b47e15SGreg Clayton
16630fdc8d8SChris Lattner default:
16730fdc8d8SChris Lattner return true;
16830fdc8d8SChris Lattner }
16930fdc8d8SChris Lattner
170b9c1b51eSKate Stone } else {
1719d3b9e57SDave Lee m_report_run_vote = eVoteNoOpinion;
1729d3b9e57SDave Lee m_report_stop_vote = eVoteNo;
173f4b47e15SGreg Clayton }
17430fdc8d8SChris Lattner
17530fdc8d8SChris Lattner // If there's no explicit reason to stop, then we will continue.
17630fdc8d8SChris Lattner return false;
17730fdc8d8SChris Lattner }
17830fdc8d8SChris Lattner
StopOthers()179b9c1b51eSKate Stone bool ThreadPlanBase::StopOthers() { return false; }
18030fdc8d8SChris Lattner
GetPlanRunState()181b9c1b51eSKate Stone StateType ThreadPlanBase::GetPlanRunState() { return eStateRunning; }
18230fdc8d8SChris Lattner
WillStop()183b9c1b51eSKate Stone bool ThreadPlanBase::WillStop() { return true; }
18430fdc8d8SChris Lattner
DoWillResume(lldb::StateType resume_state,bool current_plan)185b9c1b51eSKate Stone bool ThreadPlanBase::DoWillResume(lldb::StateType resume_state,
186b9c1b51eSKate Stone bool current_plan) {
187b9c1b51eSKate Stone // Reset these to the default values so we don't set them wrong, then not get
18805097246SAdrian Prantl // asked for a while, then return the wrong answer.
1899d3b9e57SDave Lee m_report_run_vote = eVoteNoOpinion;
1909d3b9e57SDave Lee m_report_stop_vote = eVoteNo;
191444586b5SJim Ingham return true;
192444586b5SJim Ingham }
193444586b5SJim Ingham
19430fdc8d8SChris Lattner // The base plan is never done.
MischiefManaged()195b9c1b51eSKate Stone bool ThreadPlanBase::MischiefManaged() {
19630fdc8d8SChris Lattner // The base plan is never done.
19730fdc8d8SChris Lattner return false;
19830fdc8d8SChris Lattner }
199