15ffd83dbSDimitry Andric //===-- ThreadPlanPython.cpp ----------------------------------------------===//
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"
2081ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
21*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
22*0b57cec5SDimitry Andric #include "lldb/Utility/State.h"
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric using namespace lldb;
25*0b57cec5SDimitry Andric using namespace lldb_private;
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric // ThreadPlanPython
28*0b57cec5SDimitry Andric 
299dba64beSDimitry Andric ThreadPlanPython::ThreadPlanPython(Thread &thread, const char *class_name,
300eae32dcSDimitry Andric                                    const StructuredDataImpl &args_data)
31*0b57cec5SDimitry Andric     : ThreadPlan(ThreadPlan::eKindPython, "Python based Thread Plan", thread,
32*0b57cec5SDimitry Andric                  eVoteNoOpinion, eVoteNoOpinion),
33e8d8bef9SDimitry Andric       m_class_name(class_name), m_args_data(args_data), m_did_push(false),
34e8d8bef9SDimitry Andric       m_stop_others(false) {
35349cc55cSDimitry Andric   SetIsControllingPlan(true);
36*0b57cec5SDimitry Andric   SetOkayToDiscard(true);
37*0b57cec5SDimitry Andric   SetPrivate(false);
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric bool ThreadPlanPython::ValidatePlan(Stream *error) {
41*0b57cec5SDimitry Andric   if (!m_did_push)
42*0b57cec5SDimitry Andric     return true;
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric   if (!m_implementation_sp) {
45*0b57cec5SDimitry Andric     if (error)
469dba64beSDimitry Andric       error->Printf("Error constructing Python ThreadPlan: %s",
479dba64beSDimitry Andric           m_error_str.empty() ? "<unknown error>"
489dba64beSDimitry Andric                                 : m_error_str.c_str());
49*0b57cec5SDimitry Andric     return false;
50*0b57cec5SDimitry Andric   }
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   return true;
53*0b57cec5SDimitry Andric }
54*0b57cec5SDimitry Andric 
555ffd83dbSDimitry Andric ScriptInterpreter *ThreadPlanPython::GetScriptInterpreter() {
565ffd83dbSDimitry Andric   return m_process.GetTarget().GetDebugger().GetScriptInterpreter();
575ffd83dbSDimitry Andric }
585ffd83dbSDimitry Andric 
59*0b57cec5SDimitry Andric void ThreadPlanPython::DidPush() {
60*0b57cec5SDimitry Andric   // We set up the script side in DidPush, so that it can push other plans in
61*0b57cec5SDimitry Andric   // the constructor, and doesn't have to care about the details of DidPush.
62*0b57cec5SDimitry Andric   m_did_push = true;
63*0b57cec5SDimitry Andric   if (!m_class_name.empty()) {
645ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
65*0b57cec5SDimitry Andric     if (script_interp) {
66*0b57cec5SDimitry Andric       m_implementation_sp = script_interp->CreateScriptedThreadPlan(
679dba64beSDimitry Andric           m_class_name.c_str(), m_args_data, m_error_str,
689dba64beSDimitry Andric           this->shared_from_this());
69*0b57cec5SDimitry Andric     }
70*0b57cec5SDimitry Andric   }
71*0b57cec5SDimitry Andric }
72*0b57cec5SDimitry Andric 
73*0b57cec5SDimitry Andric bool ThreadPlanPython::ShouldStop(Event *event_ptr) {
7481ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Thread);
759dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
76*0b57cec5SDimitry Andric             m_class_name.c_str());
77*0b57cec5SDimitry Andric 
78*0b57cec5SDimitry Andric   bool should_stop = true;
79*0b57cec5SDimitry Andric   if (m_implementation_sp) {
805ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
81*0b57cec5SDimitry Andric     if (script_interp) {
82*0b57cec5SDimitry Andric       bool script_error;
83*0b57cec5SDimitry Andric       should_stop = script_interp->ScriptedThreadPlanShouldStop(
84*0b57cec5SDimitry Andric           m_implementation_sp, event_ptr, script_error);
85*0b57cec5SDimitry Andric       if (script_error)
86*0b57cec5SDimitry Andric         SetPlanComplete(false);
87*0b57cec5SDimitry Andric     }
88*0b57cec5SDimitry Andric   }
89*0b57cec5SDimitry Andric   return should_stop;
90*0b57cec5SDimitry Andric }
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric bool ThreadPlanPython::IsPlanStale() {
9381ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Thread);
949dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
95*0b57cec5SDimitry Andric             m_class_name.c_str());
96*0b57cec5SDimitry Andric 
97*0b57cec5SDimitry Andric   bool is_stale = true;
98*0b57cec5SDimitry Andric   if (m_implementation_sp) {
995ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
100*0b57cec5SDimitry Andric     if (script_interp) {
101*0b57cec5SDimitry Andric       bool script_error;
102*0b57cec5SDimitry Andric       is_stale = script_interp->ScriptedThreadPlanIsStale(m_implementation_sp,
103*0b57cec5SDimitry Andric                                                           script_error);
104*0b57cec5SDimitry Andric       if (script_error)
105*0b57cec5SDimitry Andric         SetPlanComplete(false);
106*0b57cec5SDimitry Andric     }
107*0b57cec5SDimitry Andric   }
108*0b57cec5SDimitry Andric   return is_stale;
109*0b57cec5SDimitry Andric }
110*0b57cec5SDimitry Andric 
111*0b57cec5SDimitry Andric bool ThreadPlanPython::DoPlanExplainsStop(Event *event_ptr) {
11281ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Thread);
1139dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
114*0b57cec5SDimitry Andric             m_class_name.c_str());
115*0b57cec5SDimitry Andric 
116*0b57cec5SDimitry Andric   bool explains_stop = true;
117*0b57cec5SDimitry Andric   if (m_implementation_sp) {
1185ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
119*0b57cec5SDimitry Andric     if (script_interp) {
120*0b57cec5SDimitry Andric       bool script_error;
121*0b57cec5SDimitry Andric       explains_stop = script_interp->ScriptedThreadPlanExplainsStop(
122*0b57cec5SDimitry Andric           m_implementation_sp, event_ptr, script_error);
123*0b57cec5SDimitry Andric       if (script_error)
124*0b57cec5SDimitry Andric         SetPlanComplete(false);
125*0b57cec5SDimitry Andric     }
126*0b57cec5SDimitry Andric   }
127*0b57cec5SDimitry Andric   return explains_stop;
128*0b57cec5SDimitry Andric }
129*0b57cec5SDimitry Andric 
130*0b57cec5SDimitry Andric bool ThreadPlanPython::MischiefManaged() {
13181ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Thread);
1329dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
133*0b57cec5SDimitry Andric             m_class_name.c_str());
134*0b57cec5SDimitry Andric   bool mischief_managed = true;
135*0b57cec5SDimitry Andric   if (m_implementation_sp) {
136*0b57cec5SDimitry Andric     // I don't really need mischief_managed, since it's simpler to just call
137*0b57cec5SDimitry Andric     // SetPlanComplete in should_stop.
138*0b57cec5SDimitry Andric     mischief_managed = IsPlanComplete();
139*0b57cec5SDimitry Andric     if (mischief_managed)
140*0b57cec5SDimitry Andric       m_implementation_sp.reset();
141*0b57cec5SDimitry Andric   }
142*0b57cec5SDimitry Andric   return mischief_managed;
143*0b57cec5SDimitry Andric }
144*0b57cec5SDimitry Andric 
145*0b57cec5SDimitry Andric lldb::StateType ThreadPlanPython::GetPlanRunState() {
14681ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Thread);
1479dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
148*0b57cec5SDimitry Andric             m_class_name.c_str());
149*0b57cec5SDimitry Andric   lldb::StateType run_state = eStateRunning;
150*0b57cec5SDimitry Andric   if (m_implementation_sp) {
1515ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
152*0b57cec5SDimitry Andric     if (script_interp) {
153*0b57cec5SDimitry Andric       bool script_error;
154*0b57cec5SDimitry Andric       run_state = script_interp->ScriptedThreadPlanGetRunState(
155*0b57cec5SDimitry Andric           m_implementation_sp, script_error);
156*0b57cec5SDimitry Andric     }
157*0b57cec5SDimitry Andric   }
158*0b57cec5SDimitry Andric   return run_state;
159*0b57cec5SDimitry Andric }
160*0b57cec5SDimitry Andric 
161*0b57cec5SDimitry Andric // The ones below are not currently exported to Python.
162*0b57cec5SDimitry Andric void ThreadPlanPython::GetDescription(Stream *s, lldb::DescriptionLevel level) {
163*0b57cec5SDimitry Andric   s->Printf("Python thread plan implemented by class %s.",
164*0b57cec5SDimitry Andric             m_class_name.c_str());
165*0b57cec5SDimitry Andric }
166*0b57cec5SDimitry Andric 
167*0b57cec5SDimitry Andric bool ThreadPlanPython::WillStop() {
16881ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Thread);
1699dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
170*0b57cec5SDimitry Andric             m_class_name.c_str());
171*0b57cec5SDimitry Andric   return true;
172*0b57cec5SDimitry Andric }
173