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