15ffd83dbSDimitry Andric //===-- ThreadPlanPython.cpp ----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Target/ThreadPlan.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
120b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
130b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
140b57cec5SDimitry Andric #include "lldb/Target/Process.h"
150b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
160b57cec5SDimitry Andric #include "lldb/Target/Target.h"
170b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
180b57cec5SDimitry Andric #include "lldb/Target/ThreadPlan.h"
190b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanPython.h"
2081ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
210b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
220b57cec5SDimitry Andric #include "lldb/Utility/State.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace lldb;
250b57cec5SDimitry Andric using namespace lldb_private;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric // ThreadPlanPython
280b57cec5SDimitry Andric 
ThreadPlanPython(Thread & thread,const char * class_name,const StructuredDataImpl & args_data)299dba64beSDimitry Andric ThreadPlanPython::ThreadPlanPython(Thread &thread, const char *class_name,
300eae32dcSDimitry Andric                                    const StructuredDataImpl &args_data)
310b57cec5SDimitry Andric     : ThreadPlan(ThreadPlan::eKindPython, "Python based Thread Plan", thread,
320b57cec5SDimitry 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);
360b57cec5SDimitry Andric   SetOkayToDiscard(true);
370b57cec5SDimitry Andric   SetPrivate(false);
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric 
ValidatePlan(Stream * error)400b57cec5SDimitry Andric bool ThreadPlanPython::ValidatePlan(Stream *error) {
410b57cec5SDimitry Andric   if (!m_did_push)
420b57cec5SDimitry Andric     return true;
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric   if (!m_implementation_sp) {
450b57cec5SDimitry 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());
490b57cec5SDimitry Andric     return false;
500b57cec5SDimitry Andric   }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   return true;
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
GetScriptInterpreter()555ffd83dbSDimitry Andric ScriptInterpreter *ThreadPlanPython::GetScriptInterpreter() {
565ffd83dbSDimitry Andric   return m_process.GetTarget().GetDebugger().GetScriptInterpreter();
575ffd83dbSDimitry Andric }
585ffd83dbSDimitry Andric 
DidPush()590b57cec5SDimitry Andric void ThreadPlanPython::DidPush() {
600b57cec5SDimitry Andric   // We set up the script side in DidPush, so that it can push other plans in
610b57cec5SDimitry Andric   // the constructor, and doesn't have to care about the details of DidPush.
620b57cec5SDimitry Andric   m_did_push = true;
630b57cec5SDimitry Andric   if (!m_class_name.empty()) {
645ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
650b57cec5SDimitry Andric     if (script_interp) {
660b57cec5SDimitry 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());
690b57cec5SDimitry Andric     }
700b57cec5SDimitry Andric   }
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
ShouldStop(Event * event_ptr)730b57cec5SDimitry 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,
760b57cec5SDimitry Andric             m_class_name.c_str());
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   bool should_stop = true;
790b57cec5SDimitry Andric   if (m_implementation_sp) {
805ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
810b57cec5SDimitry Andric     if (script_interp) {
820b57cec5SDimitry Andric       bool script_error;
830b57cec5SDimitry Andric       should_stop = script_interp->ScriptedThreadPlanShouldStop(
840b57cec5SDimitry Andric           m_implementation_sp, event_ptr, script_error);
850b57cec5SDimitry Andric       if (script_error)
860b57cec5SDimitry Andric         SetPlanComplete(false);
870b57cec5SDimitry Andric     }
880b57cec5SDimitry Andric   }
890b57cec5SDimitry Andric   return should_stop;
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric 
IsPlanStale()920b57cec5SDimitry 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,
950b57cec5SDimitry Andric             m_class_name.c_str());
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   bool is_stale = true;
980b57cec5SDimitry Andric   if (m_implementation_sp) {
995ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
1000b57cec5SDimitry Andric     if (script_interp) {
1010b57cec5SDimitry Andric       bool script_error;
1020b57cec5SDimitry Andric       is_stale = script_interp->ScriptedThreadPlanIsStale(m_implementation_sp,
1030b57cec5SDimitry Andric                                                           script_error);
1040b57cec5SDimitry Andric       if (script_error)
1050b57cec5SDimitry Andric         SetPlanComplete(false);
1060b57cec5SDimitry Andric     }
1070b57cec5SDimitry Andric   }
1080b57cec5SDimitry Andric   return is_stale;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
DoPlanExplainsStop(Event * event_ptr)1110b57cec5SDimitry 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,
1140b57cec5SDimitry Andric             m_class_name.c_str());
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   bool explains_stop = true;
1170b57cec5SDimitry Andric   if (m_implementation_sp) {
1185ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
1190b57cec5SDimitry Andric     if (script_interp) {
1200b57cec5SDimitry Andric       bool script_error;
1210b57cec5SDimitry Andric       explains_stop = script_interp->ScriptedThreadPlanExplainsStop(
1220b57cec5SDimitry Andric           m_implementation_sp, event_ptr, script_error);
1230b57cec5SDimitry Andric       if (script_error)
1240b57cec5SDimitry Andric         SetPlanComplete(false);
1250b57cec5SDimitry Andric     }
1260b57cec5SDimitry Andric   }
1270b57cec5SDimitry Andric   return explains_stop;
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric 
MischiefManaged()1300b57cec5SDimitry 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,
1330b57cec5SDimitry Andric             m_class_name.c_str());
1340b57cec5SDimitry Andric   bool mischief_managed = true;
1350b57cec5SDimitry Andric   if (m_implementation_sp) {
1360b57cec5SDimitry Andric     // I don't really need mischief_managed, since it's simpler to just call
1370b57cec5SDimitry Andric     // SetPlanComplete in should_stop.
1380b57cec5SDimitry Andric     mischief_managed = IsPlanComplete();
139*fe013be4SDimitry Andric     if (mischief_managed) {
140*fe013be4SDimitry Andric       // We need to cache the stop reason here we'll need it in GetDescription.
141*fe013be4SDimitry Andric       GetDescription(&m_stop_description, eDescriptionLevelBrief);
1420b57cec5SDimitry Andric       m_implementation_sp.reset();
1430b57cec5SDimitry Andric     }
144*fe013be4SDimitry Andric   }
1450b57cec5SDimitry Andric   return mischief_managed;
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric 
GetPlanRunState()1480b57cec5SDimitry Andric lldb::StateType ThreadPlanPython::GetPlanRunState() {
14981ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Thread);
1509dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
1510b57cec5SDimitry Andric             m_class_name.c_str());
1520b57cec5SDimitry Andric   lldb::StateType run_state = eStateRunning;
1530b57cec5SDimitry Andric   if (m_implementation_sp) {
1545ffd83dbSDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
1550b57cec5SDimitry Andric     if (script_interp) {
1560b57cec5SDimitry Andric       bool script_error;
1570b57cec5SDimitry Andric       run_state = script_interp->ScriptedThreadPlanGetRunState(
1580b57cec5SDimitry Andric           m_implementation_sp, script_error);
1590b57cec5SDimitry Andric     }
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric   return run_state;
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)1640b57cec5SDimitry Andric void ThreadPlanPython::GetDescription(Stream *s, lldb::DescriptionLevel level) {
165*fe013be4SDimitry Andric   Log *log = GetLog(LLDBLog::Thread);
166*fe013be4SDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
167*fe013be4SDimitry Andric             m_class_name.c_str());
168*fe013be4SDimitry Andric   if (m_implementation_sp) {
169*fe013be4SDimitry Andric     ScriptInterpreter *script_interp = GetScriptInterpreter();
170*fe013be4SDimitry Andric     if (script_interp) {
171*fe013be4SDimitry Andric       bool script_error;
172*fe013be4SDimitry Andric       bool added_desc = script_interp->ScriptedThreadPlanGetStopDescription(
173*fe013be4SDimitry Andric           m_implementation_sp, s, script_error);
174*fe013be4SDimitry Andric       if (script_error || !added_desc)
1750b57cec5SDimitry Andric         s->Printf("Python thread plan implemented by class %s.",
1760b57cec5SDimitry Andric             m_class_name.c_str());
1770b57cec5SDimitry Andric     }
178*fe013be4SDimitry Andric     return;
179*fe013be4SDimitry Andric   }
180*fe013be4SDimitry Andric   // It's an error not to have a description, so if we get here, we should
181*fe013be4SDimitry Andric   // add something.
182*fe013be4SDimitry Andric   if (m_stop_description.Empty())
183*fe013be4SDimitry Andric     s->Printf("Python thread plan implemented by class %s.",
184*fe013be4SDimitry Andric               m_class_name.c_str());
185*fe013be4SDimitry Andric   s->PutCString(m_stop_description.GetData());
186*fe013be4SDimitry Andric }
1870b57cec5SDimitry Andric 
188*fe013be4SDimitry Andric // The ones below are not currently exported to Python.
WillStop()1890b57cec5SDimitry Andric bool ThreadPlanPython::WillStop() {
19081ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Thread);
1919dba64beSDimitry Andric   LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
1920b57cec5SDimitry Andric             m_class_name.c_str());
1930b57cec5SDimitry Andric   return true;
1940b57cec5SDimitry Andric }
195*fe013be4SDimitry Andric 
DoWillResume(lldb::StateType resume_state,bool current_plan)196*fe013be4SDimitry Andric bool ThreadPlanPython::DoWillResume(lldb::StateType resume_state,
197*fe013be4SDimitry Andric                                   bool current_plan) {
198*fe013be4SDimitry Andric   m_stop_description.Clear();
199*fe013be4SDimitry Andric   return true;
200*fe013be4SDimitry Andric }
201