1cfca06d7SDimitry Andric //===-- ThreadPlanBase.cpp ------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Target/ThreadPlanBase.h"
10f034231aSEd Maste
11f034231aSEd Maste //
12f034231aSEd Maste #include "lldb/Breakpoint/Breakpoint.h"
1314f1b3e8SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
1414f1b3e8SDimitry Andric #include "lldb/Breakpoint/BreakpointSite.h"
1514f1b3e8SDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
16f034231aSEd Maste #include "lldb/Target/Process.h"
17f034231aSEd Maste #include "lldb/Target/RegisterContext.h"
18f034231aSEd Maste #include "lldb/Target/StopInfo.h"
19145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
2074a628f7SDimitry Andric #include "lldb/Utility/Log.h"
2174a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
22f034231aSEd Maste
23f034231aSEd Maste using namespace lldb;
24f034231aSEd Maste using namespace lldb_private;
25f034231aSEd Maste
26f73363f1SDimitry Andric // ThreadPlanBase: This one always stops, and never has anything particular to
27f73363f1SDimitry Andric // do.
28f034231aSEd Maste // FIXME: The "signal handling" policies should probably go here.
29f034231aSEd Maste
ThreadPlanBase(Thread & thread)3014f1b3e8SDimitry Andric ThreadPlanBase::ThreadPlanBase(Thread &thread)
3114f1b3e8SDimitry Andric : ThreadPlan(ThreadPlan::eKindBase, "base plan", thread, eVoteYes,
3214f1b3e8SDimitry Andric eVoteNoOpinion) {
33f034231aSEd Maste // Set the tracer to a default tracer.
34f034231aSEd Maste // FIXME: need to add a thread settings variable to pix various tracers...
35f034231aSEd Maste #define THREAD_PLAN_USE_ASSEMBLY_TRACER 1
36f034231aSEd Maste
37f034231aSEd Maste #ifdef THREAD_PLAN_USE_ASSEMBLY_TRACER
38cfca06d7SDimitry Andric ThreadPlanTracerSP new_tracer_sp(new ThreadPlanAssemblyTracer(thread));
39f034231aSEd Maste #else
40f034231aSEd Maste ThreadPlanTracerSP new_tracer_sp(new ThreadPlanTracer(m_thread));
41f034231aSEd Maste #endif
42cfca06d7SDimitry Andric new_tracer_sp->EnableTracing(thread.GetTraceEnabledState());
43f034231aSEd Maste SetThreadPlanTracer(new_tracer_sp);
44c0981da4SDimitry Andric SetIsControllingPlan(true);
45f034231aSEd Maste }
46f034231aSEd Maste
47344a3780SDimitry Andric ThreadPlanBase::~ThreadPlanBase() = default;
48f034231aSEd Maste
GetDescription(Stream * s,lldb::DescriptionLevel level)4914f1b3e8SDimitry Andric void ThreadPlanBase::GetDescription(Stream *s, lldb::DescriptionLevel level) {
50f034231aSEd Maste s->Printf("Base thread plan.");
51f034231aSEd Maste }
52f034231aSEd Maste
ValidatePlan(Stream * error)5314f1b3e8SDimitry Andric bool ThreadPlanBase::ValidatePlan(Stream *error) { return true; }
54f034231aSEd Maste
DoPlanExplainsStop(Event * event_ptr)5514f1b3e8SDimitry Andric bool ThreadPlanBase::DoPlanExplainsStop(Event *event_ptr) {
56f73363f1SDimitry Andric // The base plan should defer to its tracer, since by default it always
57f73363f1SDimitry Andric // handles the stop.
5894994d37SDimitry Andric return !TracerExplainsStop();
59f034231aSEd Maste }
60f034231aSEd Maste
ShouldReportStop(Event * event_ptr)6114f1b3e8SDimitry Andric Vote ThreadPlanBase::ShouldReportStop(Event *event_ptr) {
62cfca06d7SDimitry Andric StopInfoSP stop_info_sp = GetThread().GetStopInfo();
6314f1b3e8SDimitry Andric if (stop_info_sp) {
64f034231aSEd Maste bool should_notify = stop_info_sp->ShouldNotify(event_ptr);
65f034231aSEd Maste if (should_notify)
66f034231aSEd Maste return eVoteYes;
67f034231aSEd Maste else
68f034231aSEd Maste return eVoteNoOpinion;
6914f1b3e8SDimitry Andric } else
70f034231aSEd Maste return eVoteNoOpinion;
71f034231aSEd Maste }
72f034231aSEd Maste
ShouldStop(Event * event_ptr)7314f1b3e8SDimitry Andric bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
74344a3780SDimitry Andric m_report_stop_vote = eVoteYes;
75344a3780SDimitry Andric m_report_run_vote = eVoteYes;
76f034231aSEd Maste
77145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
78f034231aSEd Maste
79f034231aSEd Maste StopInfoSP stop_info_sp = GetPrivateStopInfo();
8014f1b3e8SDimitry Andric if (stop_info_sp) {
81f034231aSEd Maste StopReason reason = stop_info_sp->GetStopReason();
8214f1b3e8SDimitry Andric switch (reason) {
83f034231aSEd Maste case eStopReasonInvalid:
84f034231aSEd Maste case eStopReasonNone:
85f034231aSEd Maste // This
86344a3780SDimitry Andric m_report_run_vote = eVoteNoOpinion;
87344a3780SDimitry Andric m_report_stop_vote = eVoteNo;
88f034231aSEd Maste return false;
89f034231aSEd Maste
90f034231aSEd Maste case eStopReasonBreakpoint:
91f034231aSEd Maste case eStopReasonWatchpoint:
9214f1b3e8SDimitry Andric if (stop_info_sp->ShouldStopSynchronous(event_ptr)) {
93f73363f1SDimitry Andric // If we are going to stop for a breakpoint, then unship the other
94c0981da4SDimitry Andric // plans at this point. Don't force the discard, however, so
95c0981da4SDimitry Andric // Controlling plans can stay in place if they want to.
96ead24645SDimitry Andric LLDB_LOGF(
97ead24645SDimitry Andric log,
9814f1b3e8SDimitry Andric "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
9914f1b3e8SDimitry Andric " (breakpoint hit.)",
100cfca06d7SDimitry Andric m_tid);
101cfca06d7SDimitry Andric GetThread().DiscardThreadPlans(false);
102f034231aSEd Maste return true;
103f034231aSEd Maste }
104f034231aSEd Maste // If we aren't going to stop at this breakpoint, and it is internal,
105f73363f1SDimitry Andric // don't report this stop or the subsequent running event. Otherwise we
106f73363f1SDimitry Andric // will post the stopped & running, but the stopped event will get marked
10714f1b3e8SDimitry Andric // with "restarted" so the UI will know to wait and expect the consequent
10814f1b3e8SDimitry Andric // "running".
10914f1b3e8SDimitry Andric if (stop_info_sp->ShouldNotify(event_ptr)) {
110344a3780SDimitry Andric m_report_stop_vote = eVoteYes;
111344a3780SDimitry Andric m_report_run_vote = eVoteYes;
11214f1b3e8SDimitry Andric } else {
113344a3780SDimitry Andric m_report_stop_vote = eVoteNo;
114344a3780SDimitry Andric m_report_run_vote = eVoteNo;
115f034231aSEd Maste }
116f034231aSEd Maste return false;
117f034231aSEd Maste
118f034231aSEd Maste // TODO: the break below was missing, was this intentional??? If so
119f034231aSEd Maste // please mention it
120f034231aSEd Maste break;
121f034231aSEd Maste
122f034231aSEd Maste case eStopReasonException:
123f73363f1SDimitry Andric // If we crashed, discard thread plans and stop. Don't force the
124f73363f1SDimitry Andric // discard, however, since on rerun the target may clean up this
125f73363f1SDimitry Andric // exception and continue normally from there.
126ead24645SDimitry Andric LLDB_LOGF(
127ead24645SDimitry Andric log,
12814f1b3e8SDimitry Andric "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
12914f1b3e8SDimitry Andric " (exception: %s)",
130cfca06d7SDimitry Andric m_tid, stop_info_sp->GetDescription());
131cfca06d7SDimitry Andric GetThread().DiscardThreadPlans(false);
132f034231aSEd Maste return true;
133f034231aSEd Maste
134f034231aSEd Maste case eStopReasonExec:
135f73363f1SDimitry Andric // If we crashed, discard thread plans and stop. Don't force the
136f73363f1SDimitry Andric // discard, however, since on rerun the target may clean up this
137f73363f1SDimitry Andric // exception and continue normally from there.
138ead24645SDimitry Andric LLDB_LOGF(
139ead24645SDimitry Andric log,
14014f1b3e8SDimitry Andric "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
14114f1b3e8SDimitry Andric " (exec.)",
142cfca06d7SDimitry Andric m_tid);
143cfca06d7SDimitry Andric GetThread().DiscardThreadPlans(false);
144f034231aSEd Maste return true;
145f034231aSEd Maste
146f034231aSEd Maste case eStopReasonThreadExiting:
147f034231aSEd Maste case eStopReasonSignal:
14814f1b3e8SDimitry Andric if (stop_info_sp->ShouldStop(event_ptr)) {
149ead24645SDimitry Andric LLDB_LOGF(
150ead24645SDimitry Andric log,
15114f1b3e8SDimitry Andric "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
15214f1b3e8SDimitry Andric " (signal: %s)",
153cfca06d7SDimitry Andric m_tid, stop_info_sp->GetDescription());
154cfca06d7SDimitry Andric GetThread().DiscardThreadPlans(false);
155f034231aSEd Maste return true;
15614f1b3e8SDimitry Andric } else {
157f034231aSEd Maste // We're not going to stop, but while we are here, let's figure out
158f034231aSEd Maste // whether to report this.
159f034231aSEd Maste if (stop_info_sp->ShouldNotify(event_ptr))
160344a3780SDimitry Andric m_report_stop_vote = eVoteYes;
161f034231aSEd Maste else
162344a3780SDimitry Andric m_report_stop_vote = eVoteNo;
163f034231aSEd Maste }
164f034231aSEd Maste return false;
165f034231aSEd Maste
166f034231aSEd Maste default:
167f034231aSEd Maste return true;
168f034231aSEd Maste }
169f034231aSEd Maste
17014f1b3e8SDimitry Andric } else {
171344a3780SDimitry Andric m_report_run_vote = eVoteNoOpinion;
172344a3780SDimitry Andric m_report_stop_vote = eVoteNo;
173f034231aSEd Maste }
174f034231aSEd Maste
175f034231aSEd Maste // If there's no explicit reason to stop, then we will continue.
176f034231aSEd Maste return false;
177f034231aSEd Maste }
178f034231aSEd Maste
StopOthers()17914f1b3e8SDimitry Andric bool ThreadPlanBase::StopOthers() { return false; }
180f034231aSEd Maste
GetPlanRunState()18114f1b3e8SDimitry Andric StateType ThreadPlanBase::GetPlanRunState() { return eStateRunning; }
182f034231aSEd Maste
WillStop()18314f1b3e8SDimitry Andric bool ThreadPlanBase::WillStop() { return true; }
184f034231aSEd Maste
DoWillResume(lldb::StateType resume_state,bool current_plan)18514f1b3e8SDimitry Andric bool ThreadPlanBase::DoWillResume(lldb::StateType resume_state,
18614f1b3e8SDimitry Andric bool current_plan) {
18714f1b3e8SDimitry Andric // Reset these to the default values so we don't set them wrong, then not get
188f73363f1SDimitry Andric // asked for a while, then return the wrong answer.
189344a3780SDimitry Andric m_report_run_vote = eVoteNoOpinion;
190344a3780SDimitry Andric m_report_stop_vote = eVoteNo;
191f034231aSEd Maste return true;
192f034231aSEd Maste }
193f034231aSEd Maste
194f034231aSEd Maste // The base plan is never done.
MischiefManaged()19514f1b3e8SDimitry Andric bool ThreadPlanBase::MischiefManaged() {
196f034231aSEd Maste // The base plan is never done.
197f034231aSEd Maste return false;
198f034231aSEd Maste }
199