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 //
1330fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
14b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h"
15b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointSite.h"
16b9c1b51eSKate Stone #include "lldb/Breakpoint/StoppointCallbackContext.h"
1730fdc8d8SChris Lattner #include "lldb/Target/Process.h"
1830fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
19f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
206f9e6901SZachary Turner #include "lldb/Utility/Log.h"
21bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2230fdc8d8SChris Lattner 
2330fdc8d8SChris Lattner using namespace lldb;
2430fdc8d8SChris Lattner using namespace lldb_private;
2530fdc8d8SChris Lattner 
2630fdc8d8SChris Lattner //----------------------------------------------------------------------
2705097246SAdrian Prantl // ThreadPlanBase: This one always stops, and never has anything particular to
2805097246SAdrian Prantl // do.
2930fdc8d8SChris Lattner // FIXME: The "signal handling" policies should probably go here.
3030fdc8d8SChris Lattner //----------------------------------------------------------------------
3130fdc8d8SChris Lattner 
32b9c1b51eSKate Stone ThreadPlanBase::ThreadPlanBase(Thread &thread)
33b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindBase, "base plan", thread, eVoteYes,
34b9c1b51eSKate Stone                  eVoteNoOpinion) {
3506e827ccSJim Ingham // Set the tracer to a default tracer.
36773d981cSJim Ingham // FIXME: need to add a thread settings variable to pix various tracers...
37773d981cSJim Ingham #define THREAD_PLAN_USE_ASSEMBLY_TRACER 1
38773d981cSJim Ingham 
39773d981cSJim Ingham #ifdef THREAD_PLAN_USE_ASSEMBLY_TRACER
40773d981cSJim Ingham   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanAssemblyTracer(m_thread));
41773d981cSJim Ingham #else
4206e827ccSJim Ingham   ThreadPlanTracerSP new_tracer_sp(new ThreadPlanTracer(m_thread));
43773d981cSJim Ingham #endif
4406e827ccSJim Ingham   new_tracer_sp->EnableTracing(m_thread.GetTraceEnabledState());
4506e827ccSJim Ingham   SetThreadPlanTracer(new_tracer_sp);
46cf274f91SJim Ingham   SetIsMasterPlan(true);
4730fdc8d8SChris Lattner }
4830fdc8d8SChris Lattner 
49b9c1b51eSKate Stone ThreadPlanBase::~ThreadPlanBase() {}
5030fdc8d8SChris Lattner 
51b9c1b51eSKate Stone void ThreadPlanBase::GetDescription(Stream *s, lldb::DescriptionLevel level) {
5230fdc8d8SChris Lattner   s->Printf("Base thread plan.");
5330fdc8d8SChris Lattner }
5430fdc8d8SChris Lattner 
55b9c1b51eSKate Stone bool ThreadPlanBase::ValidatePlan(Stream *error) { return true; }
5630fdc8d8SChris Lattner 
57b9c1b51eSKate Stone bool ThreadPlanBase::DoPlanExplainsStop(Event *event_ptr) {
5805097246SAdrian Prantl   // The base plan should defer to its tracer, since by default it always
5905097246SAdrian Prantl   // handles the stop.
60*a6682a41SJonas Devlieghere   return !TracerExplainsStop();
6130fdc8d8SChris Lattner }
6230fdc8d8SChris Lattner 
63b9c1b51eSKate Stone Vote ThreadPlanBase::ShouldReportStop(Event *event_ptr) {
64221d51cfSJim Ingham   StopInfoSP stop_info_sp = m_thread.GetStopInfo();
65b9c1b51eSKate Stone   if (stop_info_sp) {
66221d51cfSJim Ingham     bool should_notify = stop_info_sp->ShouldNotify(event_ptr);
67221d51cfSJim Ingham     if (should_notify)
68221d51cfSJim Ingham       return eVoteYes;
69221d51cfSJim Ingham     else
70221d51cfSJim Ingham       return eVoteNoOpinion;
71b9c1b51eSKate Stone   } else
72221d51cfSJim Ingham     return eVoteNoOpinion;
73221d51cfSJim Ingham }
74221d51cfSJim Ingham 
75b9c1b51eSKate Stone bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
7630fdc8d8SChris Lattner   m_stop_vote = eVoteYes;
7730fdc8d8SChris Lattner   m_run_vote = eVoteYes;
7830fdc8d8SChris Lattner 
795160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
800f16e73aSJim Ingham 
8160c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
82b9c1b51eSKate Stone   if (stop_info_sp) {
83b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
84b9c1b51eSKate Stone     switch (reason) {
8530fdc8d8SChris Lattner     case eStopReasonInvalid:
8630fdc8d8SChris Lattner     case eStopReasonNone:
87444586b5SJim Ingham       // This
88444586b5SJim Ingham       m_run_vote = eVoteNoOpinion;
8930fdc8d8SChris Lattner       m_stop_vote = eVoteNo;
9030fdc8d8SChris Lattner       return false;
91f4b47e15SGreg Clayton 
9230fdc8d8SChris Lattner     case eStopReasonBreakpoint:
93fd158f41SJohnny Chen     case eStopReasonWatchpoint:
94b9c1b51eSKate Stone       if (stop_info_sp->ShouldStopSynchronous(event_ptr)) {
9505097246SAdrian Prantl         // If we are going to stop for a breakpoint, then unship the other
9605097246SAdrian Prantl         // plans at this point.  Don't force the discard, however, so Master
9705097246SAdrian Prantl         // plans can stay in place if they want to.
980f16e73aSJim Ingham         if (log)
99b9c1b51eSKate Stone           log->Printf(
100b9c1b51eSKate Stone               "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
101b9c1b51eSKate Stone               " (breakpoint hit.)",
102b9c1b51eSKate Stone               m_thread.GetID());
10330fdc8d8SChris Lattner         m_thread.DiscardThreadPlans(false);
104f4b47e15SGreg Clayton         return true;
10530fdc8d8SChris Lattner       }
106f4b47e15SGreg Clayton       // If we aren't going to stop at this breakpoint, and it is internal,
10705097246SAdrian Prantl       // don't report this stop or the subsequent running event. Otherwise we
10805097246SAdrian Prantl       // will post the stopped & running, but the stopped event will get marked
109b9c1b51eSKate Stone       // with "restarted" so the UI will know to wait and expect the consequent
110b9c1b51eSKate Stone       // "running".
111b9c1b51eSKate Stone       if (stop_info_sp->ShouldNotify(event_ptr)) {
112f4b47e15SGreg Clayton         m_stop_vote = eVoteYes;
113f4b47e15SGreg Clayton         m_run_vote = eVoteYes;
114b9c1b51eSKate Stone       } else {
115f4b47e15SGreg Clayton         m_stop_vote = eVoteNo;
116f4b47e15SGreg Clayton         m_run_vote = eVoteNo;
117f4b47e15SGreg Clayton       }
118f4b47e15SGreg Clayton       return false;
11930fdc8d8SChris Lattner 
120f4b47e15SGreg Clayton       // TODO: the break below was missing, was this intentional??? If so
121f4b47e15SGreg Clayton       // please mention it
122f4b47e15SGreg Clayton       break;
123f4b47e15SGreg Clayton 
12430fdc8d8SChris Lattner     case eStopReasonException:
12505097246SAdrian Prantl       // If we crashed, discard thread plans and stop.  Don't force the
12605097246SAdrian Prantl       // discard, however, since on rerun the target may clean up this
12705097246SAdrian Prantl       // exception and continue normally from there.
1280f16e73aSJim Ingham       if (log)
129b9c1b51eSKate Stone         log->Printf(
130b9c1b51eSKate Stone             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
131b9c1b51eSKate Stone             " (exception: %s)",
132b9c1b51eSKate Stone             m_thread.GetID(), stop_info_sp->GetDescription());
13330fdc8d8SChris Lattner       m_thread.DiscardThreadPlans(false);
13430fdc8d8SChris Lattner       return true;
135f4b47e15SGreg Clayton 
13690ba8115SGreg Clayton     case eStopReasonExec:
13705097246SAdrian Prantl       // If we crashed, discard thread plans and stop.  Don't force the
13805097246SAdrian Prantl       // discard, however, since on rerun the target may clean up this
13905097246SAdrian Prantl       // exception and continue normally from there.
14090ba8115SGreg Clayton       if (log)
141b9c1b51eSKate Stone         log->Printf(
142b9c1b51eSKate Stone             "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
143b9c1b51eSKate Stone             " (exec.)",
144b9c1b51eSKate Stone             m_thread.GetID());
14590ba8115SGreg Clayton       m_thread.DiscardThreadPlans(false);
14690ba8115SGreg Clayton       return true;
14790ba8115SGreg Clayton 
148f85defaeSAndrew Kaylor     case eStopReasonThreadExiting:
14930fdc8d8SChris Lattner     case eStopReasonSignal:
150b9c1b51eSKate Stone       if (stop_info_sp->ShouldStop(event_ptr)) {
1510f16e73aSJim Ingham         if (log)
152b9c1b51eSKate Stone           log->Printf(
153b9c1b51eSKate Stone               "Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
154b9c1b51eSKate Stone               " (signal: %s)",
155b9c1b51eSKate Stone               m_thread.GetID(), stop_info_sp->GetDescription());
15630fdc8d8SChris Lattner         m_thread.DiscardThreadPlans(false);
15730fdc8d8SChris Lattner         return true;
158b9c1b51eSKate Stone       } else {
15930fdc8d8SChris Lattner         // We're not going to stop, but while we are here, let's figure out
16030fdc8d8SChris Lattner         // whether to report this.
161b15bfc75SJim Ingham         if (stop_info_sp->ShouldNotify(event_ptr))
16230fdc8d8SChris Lattner           m_stop_vote = eVoteYes;
16330fdc8d8SChris Lattner         else
16430fdc8d8SChris Lattner           m_stop_vote = eVoteNo;
165f4b47e15SGreg Clayton       }
16630fdc8d8SChris Lattner       return false;
167f4b47e15SGreg Clayton 
16830fdc8d8SChris Lattner     default:
16930fdc8d8SChris Lattner       return true;
17030fdc8d8SChris Lattner     }
17130fdc8d8SChris Lattner 
172b9c1b51eSKate Stone   } else {
173444586b5SJim Ingham     m_run_vote = eVoteNoOpinion;
174f4b47e15SGreg Clayton     m_stop_vote = eVoteNo;
175f4b47e15SGreg Clayton   }
17630fdc8d8SChris Lattner 
17730fdc8d8SChris Lattner   // If there's no explicit reason to stop, then we will continue.
17830fdc8d8SChris Lattner   return false;
17930fdc8d8SChris Lattner }
18030fdc8d8SChris Lattner 
181b9c1b51eSKate Stone bool ThreadPlanBase::StopOthers() { return false; }
18230fdc8d8SChris Lattner 
183b9c1b51eSKate Stone StateType ThreadPlanBase::GetPlanRunState() { return eStateRunning; }
18430fdc8d8SChris Lattner 
185b9c1b51eSKate Stone bool ThreadPlanBase::WillStop() { return true; }
18630fdc8d8SChris Lattner 
187b9c1b51eSKate Stone bool ThreadPlanBase::DoWillResume(lldb::StateType resume_state,
188b9c1b51eSKate Stone                                   bool current_plan) {
189b9c1b51eSKate Stone   // Reset these to the default values so we don't set them wrong, then not get
19005097246SAdrian Prantl   // asked for a while, then return the wrong answer.
191444586b5SJim Ingham   m_run_vote = eVoteNoOpinion;
192444586b5SJim Ingham   m_stop_vote = eVoteNo;
193444586b5SJim Ingham   return true;
194444586b5SJim Ingham }
195444586b5SJim Ingham 
19630fdc8d8SChris Lattner // The base plan is never done.
197b9c1b51eSKate Stone bool ThreadPlanBase::MischiefManaged() {
19830fdc8d8SChris Lattner   // The base plan is never done.
19930fdc8d8SChris Lattner   return false;
20030fdc8d8SChris Lattner }
201