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