1*0b57cec5SDimitry Andric //===-- ThreadPlanPython.cpp ------------------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlan.h"
10*0b57cec5SDimitry Andric 
11*0b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
12*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
13*0b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
14*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
15*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
16*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
17*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
18*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlan.h"
19*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanPython.h"
20*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
21*0b57cec5SDimitry Andric #include "lldb/Utility/State.h"
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric using namespace lldb;
24*0b57cec5SDimitry Andric using namespace lldb_private;
25*0b57cec5SDimitry Andric 
26*0b57cec5SDimitry Andric // ThreadPlanPython
27*0b57cec5SDimitry Andric 
289dba64beSDimitry Andric ThreadPlanPython::ThreadPlanPython(Thread &thread, const char *class_name,
299dba64beSDimitry Andric                                    StructuredDataImpl *args_data)
30*0b57cec5SDimitry Andric     : ThreadPlan(ThreadPlan::eKindPython, "Python based Thread Plan", thread,
31*0b57cec5SDimitry Andric                  eVoteNoOpinion, eVoteNoOpinion),
329dba64beSDimitry Andric       m_class_name(class_name), m_args_data(args_data), m_did_push(false) {
33*0b57cec5SDimitry Andric   SetIsMasterPlan(true);
34*0b57cec5SDimitry Andric   SetOkayToDiscard(true);
35*0b57cec5SDimitry Andric   SetPrivate(false);
36*0b57cec5SDimitry Andric }
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric ThreadPlanPython::~ThreadPlanPython() {
39*0b57cec5SDimitry Andric   // FIXME, do I need to decrement the ref count on this implementation object
40*0b57cec5SDimitry Andric   // to make it go away?
41*0b57cec5SDimitry Andric }
42*0b57cec5SDimitry Andric 
43*0b57cec5SDimitry Andric bool ThreadPlanPython::ValidatePlan(Stream *error) {
44*0b57cec5SDimitry Andric   if (!m_did_push)
45*0b57cec5SDimitry Andric     return true;
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric   if (!m_implementation_sp) {
48*0b57cec5SDimitry Andric     if (error)
499dba64beSDimitry Andric       error->Printf("Error constructing Python ThreadPlan: %s",
509dba64beSDimitry Andric           m_error_str.empty() ? "<unknown error>"
519dba64beSDimitry Andric                                 : m_error_str.c_str());
52*0b57cec5SDimitry Andric     return false;
53*0b57cec5SDimitry Andric   }
54*0b57cec5SDimitry Andric 
55*0b57cec5SDimitry Andric   return true;
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric 
58*0b57cec5SDimitry Andric void ThreadPlanPython::DidPush() {
59*0b57cec5SDimitry Andric   // We set up the script side in DidPush, so that it can push other plans in
60*0b57cec5SDimitry Andric   // the constructor, and doesn't have to care about the details of DidPush.
61*0b57cec5SDimitry Andric   m_did_push = true;
62*0b57cec5SDimitry Andric   if (!m_class_name.empty()) {
63*0b57cec5SDimitry Andric     ScriptInterpreter *script_interp = m_thread.GetProcess()
64*0b57cec5SDimitry Andric                                            ->GetTarget()
65*0b57cec5SDimitry Andric                                            .GetDebugger()
66*0b57cec5SDimitry Andric                                            .GetScriptInterpreter();
67*0b57cec5SDimitry Andric     if (script_interp) {
68*0b57cec5SDimitry Andric       m_implementation_sp = script_interp->CreateScriptedThreadPlan(
699dba64beSDimitry Andric           m_class_name.c_str(), m_args_data, m_error_str,
709dba64beSDimitry Andric           this->shared_from_this());
71*0b57cec5SDimitry Andric     }
72*0b57cec5SDimitry Andric   }
73*0b57cec5SDimitry Andric }
74*0b57cec5SDimitry Andric 
75*0b57cec5SDimitry Andric bool ThreadPlanPython::ShouldStop(Event *event_ptr) {
76*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
779dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
78*0b57cec5SDimitry Andric             m_class_name.c_str());
79*0b57cec5SDimitry Andric 
80*0b57cec5SDimitry Andric   bool should_stop = true;
81*0b57cec5SDimitry Andric   if (m_implementation_sp) {
82*0b57cec5SDimitry Andric     ScriptInterpreter *script_interp = m_thread.GetProcess()
83*0b57cec5SDimitry Andric                                            ->GetTarget()
84*0b57cec5SDimitry Andric                                            .GetDebugger()
85*0b57cec5SDimitry Andric                                            .GetScriptInterpreter();
86*0b57cec5SDimitry Andric     if (script_interp) {
87*0b57cec5SDimitry Andric       bool script_error;
88*0b57cec5SDimitry Andric       should_stop = script_interp->ScriptedThreadPlanShouldStop(
89*0b57cec5SDimitry Andric           m_implementation_sp, event_ptr, script_error);
90*0b57cec5SDimitry Andric       if (script_error)
91*0b57cec5SDimitry Andric         SetPlanComplete(false);
92*0b57cec5SDimitry Andric     }
93*0b57cec5SDimitry Andric   }
94*0b57cec5SDimitry Andric   return should_stop;
95*0b57cec5SDimitry Andric }
96*0b57cec5SDimitry Andric 
97*0b57cec5SDimitry Andric bool ThreadPlanPython::IsPlanStale() {
98*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
999dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
100*0b57cec5SDimitry Andric             m_class_name.c_str());
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric   bool is_stale = true;
103*0b57cec5SDimitry Andric   if (m_implementation_sp) {
104*0b57cec5SDimitry Andric     ScriptInterpreter *script_interp = m_thread.GetProcess()
105*0b57cec5SDimitry Andric                                            ->GetTarget()
106*0b57cec5SDimitry Andric                                            .GetDebugger()
107*0b57cec5SDimitry Andric                                            .GetScriptInterpreter();
108*0b57cec5SDimitry Andric     if (script_interp) {
109*0b57cec5SDimitry Andric       bool script_error;
110*0b57cec5SDimitry Andric       is_stale = script_interp->ScriptedThreadPlanIsStale(m_implementation_sp,
111*0b57cec5SDimitry Andric                                                           script_error);
112*0b57cec5SDimitry Andric       if (script_error)
113*0b57cec5SDimitry Andric         SetPlanComplete(false);
114*0b57cec5SDimitry Andric     }
115*0b57cec5SDimitry Andric   }
116*0b57cec5SDimitry Andric   return is_stale;
117*0b57cec5SDimitry Andric }
118*0b57cec5SDimitry Andric 
119*0b57cec5SDimitry Andric bool ThreadPlanPython::DoPlanExplainsStop(Event *event_ptr) {
120*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
1219dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
122*0b57cec5SDimitry Andric             m_class_name.c_str());
123*0b57cec5SDimitry Andric 
124*0b57cec5SDimitry Andric   bool explains_stop = true;
125*0b57cec5SDimitry Andric   if (m_implementation_sp) {
126*0b57cec5SDimitry Andric     ScriptInterpreter *script_interp = m_thread.GetProcess()
127*0b57cec5SDimitry Andric                                            ->GetTarget()
128*0b57cec5SDimitry Andric                                            .GetDebugger()
129*0b57cec5SDimitry Andric                                            .GetScriptInterpreter();
130*0b57cec5SDimitry Andric     if (script_interp) {
131*0b57cec5SDimitry Andric       bool script_error;
132*0b57cec5SDimitry Andric       explains_stop = script_interp->ScriptedThreadPlanExplainsStop(
133*0b57cec5SDimitry Andric           m_implementation_sp, event_ptr, script_error);
134*0b57cec5SDimitry Andric       if (script_error)
135*0b57cec5SDimitry Andric         SetPlanComplete(false);
136*0b57cec5SDimitry Andric     }
137*0b57cec5SDimitry Andric   }
138*0b57cec5SDimitry Andric   return explains_stop;
139*0b57cec5SDimitry Andric }
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric bool ThreadPlanPython::MischiefManaged() {
142*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
1439dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
144*0b57cec5SDimitry Andric             m_class_name.c_str());
145*0b57cec5SDimitry Andric   bool mischief_managed = true;
146*0b57cec5SDimitry Andric   if (m_implementation_sp) {
147*0b57cec5SDimitry Andric     // I don't really need mischief_managed, since it's simpler to just call
148*0b57cec5SDimitry Andric     // SetPlanComplete in should_stop.
149*0b57cec5SDimitry Andric     mischief_managed = IsPlanComplete();
150*0b57cec5SDimitry Andric     if (mischief_managed)
151*0b57cec5SDimitry Andric       m_implementation_sp.reset();
152*0b57cec5SDimitry Andric   }
153*0b57cec5SDimitry Andric   return mischief_managed;
154*0b57cec5SDimitry Andric }
155*0b57cec5SDimitry Andric 
156*0b57cec5SDimitry Andric lldb::StateType ThreadPlanPython::GetPlanRunState() {
157*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
1589dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
159*0b57cec5SDimitry Andric             m_class_name.c_str());
160*0b57cec5SDimitry Andric   lldb::StateType run_state = eStateRunning;
161*0b57cec5SDimitry Andric   if (m_implementation_sp) {
162*0b57cec5SDimitry Andric     ScriptInterpreter *script_interp = m_thread.GetProcess()
163*0b57cec5SDimitry Andric                                            ->GetTarget()
164*0b57cec5SDimitry Andric                                            .GetDebugger()
165*0b57cec5SDimitry Andric                                            .GetScriptInterpreter();
166*0b57cec5SDimitry Andric     if (script_interp) {
167*0b57cec5SDimitry Andric       bool script_error;
168*0b57cec5SDimitry Andric       run_state = script_interp->ScriptedThreadPlanGetRunState(
169*0b57cec5SDimitry Andric           m_implementation_sp, script_error);
170*0b57cec5SDimitry Andric     }
171*0b57cec5SDimitry Andric   }
172*0b57cec5SDimitry Andric   return run_state;
173*0b57cec5SDimitry Andric }
174*0b57cec5SDimitry Andric 
175*0b57cec5SDimitry Andric // The ones below are not currently exported to Python.
176*0b57cec5SDimitry Andric 
177*0b57cec5SDimitry Andric bool ThreadPlanPython::StopOthers() {
178*0b57cec5SDimitry Andric   // For now Python plans run all threads, but we should add some controls for
179*0b57cec5SDimitry Andric   // this.
180*0b57cec5SDimitry Andric   return false;
181*0b57cec5SDimitry Andric }
182*0b57cec5SDimitry Andric 
183*0b57cec5SDimitry Andric void ThreadPlanPython::GetDescription(Stream *s, lldb::DescriptionLevel level) {
184*0b57cec5SDimitry Andric   s->Printf("Python thread plan implemented by class %s.",
185*0b57cec5SDimitry Andric             m_class_name.c_str());
186*0b57cec5SDimitry Andric }
187*0b57cec5SDimitry Andric 
188*0b57cec5SDimitry Andric bool ThreadPlanPython::WillStop() {
189*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
1909dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
191*0b57cec5SDimitry Andric             m_class_name.c_str());
192*0b57cec5SDimitry Andric   return true;
193*0b57cec5SDimitry Andric }
194