130fdc8d8SChris Lattner //===-- ThreadPlanBase.cpp --------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanBase.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
1630fdc8d8SChris Lattner //
1730fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
18b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h"
19b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointSite.h"
20b9c1b51eSKate Stone #include "lldb/Breakpoint/StoppointCallbackContext.h"
210f16e73aSJim Ingham #include "lldb/Core/Log.h"
2230fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2330fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
24f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
25*bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2630fdc8d8SChris Lattner 
2730fdc8d8SChris Lattner using namespace lldb;
2830fdc8d8SChris Lattner using namespace lldb_private;
2930fdc8d8SChris Lattner 
3030fdc8d8SChris Lattner //----------------------------------------------------------------------
3130fdc8d8SChris Lattner // ThreadPlanBase: This one always stops, and never has anything particular
3230fdc8d8SChris Lattner // to do.
3330fdc8d8SChris Lattner // FIXME: The "signal handling" policies should probably go here.
3430fdc8d8SChris Lattner //----------------------------------------------------------------------
3530fdc8d8SChris Lattner 
36b9c1b51eSKate Stone ThreadPlanBase::ThreadPlanBase(Thread &thread)
37b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindBase, "base plan", thread, eVoteYes,
38b9c1b51eSKate Stone                  eVoteNoOpinion) {
3906e827ccSJim Ingham // Set the tracer to a default tracer.
40773d981cSJim Ingham // FIXME: need to add a thread settings variable to pix various tracers...
41773d981cSJim Ingham #define THREAD_PLAN_USE_ASSEMBLY_TRACER 1
42773d981cSJim Ingham 
43773d981cSJim Ingham #ifdef THREAD_PLAN_USE_ASSEMBLY_TRACER
44773d981cSJim Ingham   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanAssemblyTracer(m_thread));
45773d981cSJim Ingham #else
4606e827ccSJim Ingham   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanTracer(m_thread));
47773d981cSJim Ingham #endif
4806e827ccSJim Ingham   new_tracer_sp->EnableTracing(m_thread.GetTraceEnabledState());
4906e827ccSJim Ingham   SetThreadPlanTracer(new_tracer_sp);
50cf274f91SJim Ingham   SetIsMasterPlan(true);
5130fdc8d8SChris Lattner }
5230fdc8d8SChris Lattner 
53b9c1b51eSKate Stone ThreadPlanBase::~ThreadPlanBase() {}
5430fdc8d8SChris Lattner 
55b9c1b51eSKate Stone void ThreadPlanBase::GetDescription(Stream *s, lldb::DescriptionLevel level) {
5630fdc8d8SChris Lattner   s->Printf("Base thread plan.");
5730fdc8d8SChris Lattner }
5830fdc8d8SChris Lattner 
59b9c1b51eSKate Stone bool ThreadPlanBase::ValidatePlan(Stream *error) { return true; }
6030fdc8d8SChris Lattner 
61b9c1b51eSKate Stone bool ThreadPlanBase::DoPlanExplainsStop(Event *event_ptr) {
6206e827ccSJim Ingham   // The base plan should defer to its tracer, since by default it
6306e827ccSJim Ingham   // always handles the stop.
6406e827ccSJim Ingham   if (TracerExplainsStop())
6506e827ccSJim Ingham     return false;
6606e827ccSJim Ingham   else
6730fdc8d8SChris Lattner     return true;
6830fdc8d8SChris Lattner }
6930fdc8d8SChris Lattner 
70b9c1b51eSKate Stone Vote ThreadPlanBase::ShouldReportStop(Event *event_ptr) {
71221d51cfSJim Ingham   StopInfoSP stop_info_sp = m_thread.GetStopInfo();
72b9c1b51eSKate Stone   if (stop_info_sp) {
73221d51cfSJim Ingham     bool should_notify = stop_info_sp->ShouldNotify(event_ptr);
74221d51cfSJim Ingham     if (should_notify)
75221d51cfSJim Ingham       return eVoteYes;
76221d51cfSJim Ingham     else
77221d51cfSJim Ingham       return eVoteNoOpinion;
78b9c1b51eSKate Stone   } else
79221d51cfSJim Ingham     return eVoteNoOpinion;
80221d51cfSJim Ingham }
81221d51cfSJim Ingham 
82b9c1b51eSKate Stone bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
8330fdc8d8SChris Lattner   m_stop_vote = eVoteYes;
8430fdc8d8SChris Lattner   m_run_vote = eVoteYes;
8530fdc8d8SChris Lattner 
865160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
870f16e73aSJim Ingham 
8860c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
89b9c1b51eSKate Stone   if (stop_info_sp) {
90b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
91b9c1b51eSKate Stone     switch (reason) {
9230fdc8d8SChris Lattner     case eStopReasonInvalid:
9330fdc8d8SChris Lattner     case eStopReasonNone:
94444586b5SJim Ingham       // This
95444586b5SJim Ingham       m_run_vote = eVoteNoOpinion;
9630fdc8d8SChris Lattner       m_stop_vote = eVoteNo;
9730fdc8d8SChris Lattner       return false;
98f4b47e15SGreg Clayton 
9930fdc8d8SChris Lattner     case eStopReasonBreakpoint:
100fd158f41SJohnny Chen     case eStopReasonWatchpoint:
101b9c1b51eSKate Stone       if (stop_info_sp->ShouldStopSynchronous(event_ptr)) {
10230fdc8d8SChris Lattner         // If we are going to stop for a breakpoint, then unship the other plans
103b9c1b51eSKate Stone         // at this point.  Don't force the discard, however, so Master plans can
104b9c1b51eSKate Stone         // stay
10530fdc8d8SChris Lattner         // in place if they want to.
1060f16e73aSJim Ingham         if (log)
107b9c1b51eSKate Stone           log->Printf(
108b9c1b51eSKate Stone               "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
109b9c1b51eSKate Stone               " (breakpoint hit.)",
110b9c1b51eSKate Stone               m_thread.GetID());
11130fdc8d8SChris Lattner         m_thread.DiscardThreadPlans(false);
112f4b47e15SGreg Clayton         return true;
11330fdc8d8SChris Lattner       }
114f4b47e15SGreg Clayton       // If we aren't going to stop at this breakpoint, and it is internal,
115f4b47e15SGreg Clayton       // don't report this stop or the subsequent running event.
116b9c1b51eSKate Stone       // Otherwise we will post the stopped & running, but the stopped event
117b9c1b51eSKate Stone       // will get marked
118b9c1b51eSKate Stone       // with "restarted" so the UI will know to wait and expect the consequent
119b9c1b51eSKate Stone       // "running".
120b9c1b51eSKate Stone       if (stop_info_sp->ShouldNotify(event_ptr)) {
121f4b47e15SGreg Clayton         m_stop_vote = eVoteYes;
122f4b47e15SGreg Clayton         m_run_vote = eVoteYes;
123b9c1b51eSKate Stone       } else {
124f4b47e15SGreg Clayton         m_stop_vote = eVoteNo;
125f4b47e15SGreg Clayton         m_run_vote = eVoteNo;
126f4b47e15SGreg Clayton       }
127f4b47e15SGreg Clayton       return false;
12830fdc8d8SChris Lattner 
129f4b47e15SGreg Clayton       // TODO: the break below was missing, was this intentional??? If so
130f4b47e15SGreg Clayton       // please mention it
131f4b47e15SGreg Clayton       break;
132f4b47e15SGreg Clayton 
13330fdc8d8SChris Lattner     case eStopReasonException:
134b9c1b51eSKate Stone       // If we crashed, discard thread plans and stop.  Don't force the discard,
135b9c1b51eSKate Stone       // however,
136b9c1b51eSKate Stone       // since on rerun the target may clean up this exception and continue
137b9c1b51eSKate Stone       // normally from there.
1380f16e73aSJim Ingham       if (log)
139b9c1b51eSKate Stone         log->Printf(
140b9c1b51eSKate Stone             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
141b9c1b51eSKate Stone             " (exception: %s)",
142b9c1b51eSKate Stone             m_thread.GetID(), stop_info_sp->GetDescription());
14330fdc8d8SChris Lattner       m_thread.DiscardThreadPlans(false);
14430fdc8d8SChris Lattner       return true;
145f4b47e15SGreg Clayton 
14690ba8115SGreg Clayton     case eStopReasonExec:
147b9c1b51eSKate Stone       // If we crashed, discard thread plans and stop.  Don't force the discard,
148b9c1b51eSKate Stone       // however,
149b9c1b51eSKate Stone       // since on rerun the target may clean up this exception and continue
150b9c1b51eSKate Stone       // normally from there.
15190ba8115SGreg Clayton       if (log)
152b9c1b51eSKate Stone         log->Printf(
153b9c1b51eSKate Stone             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
154b9c1b51eSKate Stone             " (exec.)",
155b9c1b51eSKate Stone             m_thread.GetID());
15690ba8115SGreg Clayton       m_thread.DiscardThreadPlans(false);
15790ba8115SGreg Clayton       return true;
15890ba8115SGreg Clayton 
159f85defaeSAndrew Kaylor     case eStopReasonThreadExiting:
16030fdc8d8SChris Lattner     case eStopReasonSignal:
161b9c1b51eSKate Stone       if (stop_info_sp->ShouldStop(event_ptr)) {
1620f16e73aSJim Ingham         if (log)
163b9c1b51eSKate Stone           log->Printf(
164b9c1b51eSKate Stone               "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
165b9c1b51eSKate Stone               " (signal: %s)",
166b9c1b51eSKate Stone               m_thread.GetID(), stop_info_sp->GetDescription());
16730fdc8d8SChris Lattner         m_thread.DiscardThreadPlans(false);
16830fdc8d8SChris Lattner         return true;
169b9c1b51eSKate Stone       } else {
17030fdc8d8SChris Lattner         // We're not going to stop, but while we are here, let's figure out
17130fdc8d8SChris Lattner         // whether to report this.
172b15bfc75SJim Ingham         if (stop_info_sp->ShouldNotify(event_ptr))
17330fdc8d8SChris Lattner           m_stop_vote = eVoteYes;
17430fdc8d8SChris Lattner         else
17530fdc8d8SChris Lattner           m_stop_vote = eVoteNo;
176f4b47e15SGreg Clayton       }
17730fdc8d8SChris Lattner       return false;
178f4b47e15SGreg Clayton 
17930fdc8d8SChris Lattner     default:
18030fdc8d8SChris Lattner       return true;
18130fdc8d8SChris Lattner     }
18230fdc8d8SChris Lattner 
183b9c1b51eSKate Stone   } else {
184444586b5SJim Ingham     m_run_vote = eVoteNoOpinion;
185f4b47e15SGreg Clayton     m_stop_vote = eVoteNo;
186f4b47e15SGreg Clayton   }
18730fdc8d8SChris Lattner 
18830fdc8d8SChris Lattner   // If there's no explicit reason to stop, then we will continue.
18930fdc8d8SChris Lattner   return false;
19030fdc8d8SChris Lattner }
19130fdc8d8SChris Lattner 
192b9c1b51eSKate Stone bool ThreadPlanBase::StopOthers() { return false; }
19330fdc8d8SChris Lattner 
194b9c1b51eSKate Stone StateType ThreadPlanBase::GetPlanRunState() { return eStateRunning; }
19530fdc8d8SChris Lattner 
196b9c1b51eSKate Stone bool ThreadPlanBase::WillStop() { return true; }
19730fdc8d8SChris Lattner 
198b9c1b51eSKate Stone bool ThreadPlanBase::DoWillResume(lldb::StateType resume_state,
199b9c1b51eSKate Stone                                   bool current_plan) {
200b9c1b51eSKate Stone   // Reset these to the default values so we don't set them wrong, then not get
201b9c1b51eSKate Stone   // asked
202444586b5SJim Ingham   // for a while, then return the wrong answer.
203444586b5SJim Ingham   m_run_vote = eVoteNoOpinion;
204444586b5SJim Ingham   m_stop_vote = eVoteNo;
205444586b5SJim Ingham   return true;
206444586b5SJim Ingham }
207444586b5SJim Ingham 
20830fdc8d8SChris Lattner // The base plan is never done.
209b9c1b51eSKate Stone bool ThreadPlanBase::MischiefManaged() {
21030fdc8d8SChris Lattner   // The base plan is never done.
21130fdc8d8SChris Lattner   return false;
21230fdc8d8SChris Lattner }
213