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/ThreadPlanContinue.h"
25 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
30 //----------------------------------------------------------------------
31 // ThreadPlanBase: This one always stops, and never has anything particular
32 // to do.
33 // FIXME: The "signal handling" policies should probably go here.
34 //----------------------------------------------------------------------
35 
36 ThreadPlanBase::ThreadPlanBase (Thread &thread) :
37     ThreadPlan("base plan", thread, eVoteYes, eVoteNoOpinion)
38 {
39 
40 }
41 
42 ThreadPlanBase::~ThreadPlanBase ()
43 {
44 
45 }
46 
47 void
48 ThreadPlanBase::GetDescription (Stream *s, lldb::DescriptionLevel level)
49 {
50     s->Printf ("Base thread plan.");
51 }
52 
53 bool
54 ThreadPlanBase::ValidatePlan (Stream *error)
55 {
56     return true;
57 }
58 
59 bool
60 ThreadPlanBase::PlanExplainsStop ()
61 {
62     return true;
63 }
64 
65 bool
66 ThreadPlanBase::ShouldStop (Event *event_ptr)
67 {
68     m_stop_vote = eVoteYes;
69     m_run_vote = eVoteYes;
70 
71     Thread::StopInfo stop_info;
72     if (m_thread.GetStopInfo(&stop_info))
73     {
74         StopReason reason = stop_info.GetStopReason();
75         switch (reason)
76         {
77             case eStopReasonInvalid:
78             case eStopReasonNone:
79             {
80                 m_run_vote = eVoteNo;
81                 m_stop_vote = eVoteNo;
82                 return false;
83             }
84             case eStopReasonBreakpoint:
85             {
86                 // The base plan checks for breakpoint hits.
87 
88                 BreakpointSiteSP bp_site_sp;
89                 //RegisterContext *reg_ctx = m_thread.GetRegisterContext();
90                 //lldb::addr_t pc = reg_ctx->GetPC();
91                 bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID (stop_info.GetBreakpointSiteID());
92 
93                 if (bp_site_sp && bp_site_sp->IsEnabled())
94                 {
95                     // We want to step over the breakpoint and then continue.  So push these two plans.
96 
97                     StoppointCallbackContext hit_context(event_ptr, &m_thread.GetProcess(), &m_thread, m_thread.GetStackFrameAtIndex(0).get());
98                     bool should_stop = m_thread.GetProcess().GetBreakpointSiteList().ShouldStop(&hit_context, bp_site_sp->GetID());
99 
100                     if (!should_stop)
101                     {
102                         // If we aren't going to stop at this breakpoint, and it is internal, don't report this stop or the subsequent
103                         // running event.  Otherwise we will post the stopped & running, but the stopped event will get marked
104                         // with "restarted" so the UI will know to wait and expect the consequent "running".
105                         uint32_t i;
106                         bool is_wholly_internal = true;
107 
108                         for (i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
109                         {
110                             if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
111                             {
112                                 is_wholly_internal = false;
113                                 break;
114                             }
115                         }
116                         if (is_wholly_internal)
117                         {
118                             m_stop_vote = eVoteNo;
119                             m_run_vote = eVoteNo;
120                         }
121                         else
122                         {
123                             m_stop_vote = eVoteYes;
124                             m_run_vote = eVoteYes;
125                         }
126 
127                     }
128                     else
129                     {
130                         // If we are going to stop for a breakpoint, then unship the other plans
131                         // at this point.  Don't force the discard, however, so Master plans can stay
132                         // in place if they want to.
133                         m_thread.DiscardThreadPlans(false);
134                     }
135 
136                     return should_stop;
137                 }
138             }
139             case eStopReasonException:
140                 // If we crashed, discard thread plans and stop.  Don't force the discard, however,
141                 // since on rerun the target may clean up this exception and continue normally from there.
142                 m_thread.DiscardThreadPlans(false);
143                 return true;
144             case eStopReasonSignal:
145             {
146                 // Check the signal handling, and if we are stopping for the signal,
147                 // discard the plans and stop.
148                 UnixSignals &signals = m_thread.GetProcess().GetUnixSignals();
149                 uint32_t signo = stop_info.GetSignal();
150                 if (signals.GetShouldStop(signo))
151                 {
152                     m_thread.DiscardThreadPlans(false);
153                     return true;
154                 }
155                 else
156                 {
157                     // We're not going to stop, but while we are here, let's figure out
158                     // whether to report this.
159                     if (signals.GetShouldNotify(signo))
160                         m_stop_vote = eVoteYes;
161                     else
162                         m_stop_vote = eVoteNo;
163 
164                     return false;
165                 }
166             }
167             default:
168                 return true;
169         }
170 
171     }
172 
173     // If there's no explicit reason to stop, then we will continue.
174     return false;
175 }
176 
177 bool
178 ThreadPlanBase::StopOthers ()
179 {
180     return false;
181 }
182 
183 StateType
184 ThreadPlanBase::RunState ()
185 {
186     return eStateRunning;
187 }
188 
189 bool
190 ThreadPlanBase::WillStop ()
191 {
192     return true;
193 }
194 
195 // The base plan is never done.
196 bool
197 ThreadPlanBase::MischiefManaged ()
198 {
199     // The base plan is never done.
200     return false;
201 }
202 
203