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