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             m_run_vote = eVoteNo;
94             m_stop_vote = eVoteNo;
95             return false;
96 
97         case eStopReasonBreakpoint:
98             if (stop_info_sp->ShouldStop(event_ptr))
99             {
100                 // If we are going to stop for a breakpoint, then unship the other plans
101                 // at this point.  Don't force the discard, however, so Master plans can stay
102                 // in place if they want to.
103                 m_thread.DiscardThreadPlans(false);
104                 return true;
105             }
106             // If we aren't going to stop at this breakpoint, and it is internal,
107             // don't report this stop or the subsequent running event.
108             // Otherwise we will post the stopped & running, but the stopped event will get marked
109             // with "restarted" so the UI will know to wait and expect the consequent "running".
110             if (stop_info_sp->ShouldNotify (event_ptr))
111             {
112                 m_stop_vote = eVoteYes;
113                 m_run_vote = eVoteYes;
114             }
115             else
116             {
117                 m_stop_vote = eVoteNo;
118                 m_run_vote = eVoteNo;
119             }
120             return false;
121 
122             // TODO: the break below was missing, was this intentional??? If so
123             // please mention it
124             break;
125 
126         case eStopReasonException:
127             // If we crashed, discard thread plans and stop.  Don't force the discard, however,
128             // since on rerun the target may clean up this exception and continue normally from there.
129             m_thread.DiscardThreadPlans(false);
130             return true;
131 
132         case eStopReasonSignal:
133             if (stop_info_sp->ShouldStop(event_ptr))
134             {
135                 m_thread.DiscardThreadPlans(false);
136                 return true;
137             }
138             else
139             {
140                 // We're not going to stop, but while we are here, let's figure out
141                 // whether to report this.
142                  if (stop_info_sp->ShouldNotify(event_ptr))
143                     m_stop_vote = eVoteYes;
144                 else
145                     m_stop_vote = eVoteNo;
146             }
147             return false;
148 
149         default:
150             return true;
151         }
152 
153     }
154     else
155     {
156         m_run_vote = eVoteNo;
157         m_stop_vote = eVoteNo;
158     }
159 
160     // If there's no explicit reason to stop, then we will continue.
161     return false;
162 }
163 
164 bool
165 ThreadPlanBase::StopOthers ()
166 {
167     return false;
168 }
169 
170 StateType
171 ThreadPlanBase::GetPlanRunState ()
172 {
173     return eStateRunning;
174 }
175 
176 bool
177 ThreadPlanBase::WillStop ()
178 {
179     return true;
180 }
181 
182 // The base plan is never done.
183 bool
184 ThreadPlanBase::MischiefManaged ()
185 {
186     // The base plan is never done.
187     return false;
188 }
189 
190