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"
200b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
210b57cec5SDimitry Andric #include "lldb/Utility/State.h"
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric using namespace lldb;
240b57cec5SDimitry Andric using namespace lldb_private;
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric // ThreadPlanPython
270b57cec5SDimitry Andric
ThreadPlanPython(Thread & thread,const char * class_name,StructuredDataImpl * args_data)289dba64beSDimitry Andric ThreadPlanPython::ThreadPlanPython(Thread &thread, const char *class_name,
299dba64beSDimitry Andric StructuredDataImpl *args_data)
300b57cec5SDimitry Andric : ThreadPlan(ThreadPlan::eKindPython, "Python based Thread Plan", thread,
310b57cec5SDimitry Andric eVoteNoOpinion, eVoteNoOpinion),
32*af732203SDimitry Andric m_class_name(class_name), m_args_data(args_data), m_did_push(false),
33*af732203SDimitry Andric m_stop_others(false) {
340b57cec5SDimitry Andric SetIsMasterPlan(true);
350b57cec5SDimitry Andric SetOkayToDiscard(true);
360b57cec5SDimitry Andric SetPrivate(false);
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
~ThreadPlanPython()390b57cec5SDimitry Andric ThreadPlanPython::~ThreadPlanPython() {
400b57cec5SDimitry Andric // FIXME, do I need to decrement the ref count on this implementation object
410b57cec5SDimitry Andric // to make it go away?
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric
ValidatePlan(Stream * error)440b57cec5SDimitry Andric bool ThreadPlanPython::ValidatePlan(Stream *error) {
450b57cec5SDimitry Andric if (!m_did_push)
460b57cec5SDimitry Andric return true;
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric if (!m_implementation_sp) {
490b57cec5SDimitry Andric if (error)
509dba64beSDimitry Andric error->Printf("Error constructing Python ThreadPlan: %s",
519dba64beSDimitry Andric m_error_str.empty() ? "<unknown error>"
529dba64beSDimitry Andric : m_error_str.c_str());
530b57cec5SDimitry Andric return false;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric return true;
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric
GetScriptInterpreter()595ffd83dbSDimitry Andric ScriptInterpreter *ThreadPlanPython::GetScriptInterpreter() {
605ffd83dbSDimitry Andric return m_process.GetTarget().GetDebugger().GetScriptInterpreter();
615ffd83dbSDimitry Andric }
625ffd83dbSDimitry Andric
DidPush()630b57cec5SDimitry Andric void ThreadPlanPython::DidPush() {
640b57cec5SDimitry Andric // We set up the script side in DidPush, so that it can push other plans in
650b57cec5SDimitry Andric // the constructor, and doesn't have to care about the details of DidPush.
660b57cec5SDimitry Andric m_did_push = true;
670b57cec5SDimitry Andric if (!m_class_name.empty()) {
685ffd83dbSDimitry Andric ScriptInterpreter *script_interp = GetScriptInterpreter();
690b57cec5SDimitry Andric if (script_interp) {
700b57cec5SDimitry Andric m_implementation_sp = script_interp->CreateScriptedThreadPlan(
719dba64beSDimitry Andric m_class_name.c_str(), m_args_data, m_error_str,
729dba64beSDimitry Andric this->shared_from_this());
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric
ShouldStop(Event * event_ptr)770b57cec5SDimitry Andric bool ThreadPlanPython::ShouldStop(Event *event_ptr) {
780b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
799dba64beSDimitry Andric LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
800b57cec5SDimitry Andric m_class_name.c_str());
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric bool should_stop = true;
830b57cec5SDimitry Andric if (m_implementation_sp) {
845ffd83dbSDimitry Andric ScriptInterpreter *script_interp = GetScriptInterpreter();
850b57cec5SDimitry Andric if (script_interp) {
860b57cec5SDimitry Andric bool script_error;
870b57cec5SDimitry Andric should_stop = script_interp->ScriptedThreadPlanShouldStop(
880b57cec5SDimitry Andric m_implementation_sp, event_ptr, script_error);
890b57cec5SDimitry Andric if (script_error)
900b57cec5SDimitry Andric SetPlanComplete(false);
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric return should_stop;
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric
IsPlanStale()960b57cec5SDimitry Andric bool ThreadPlanPython::IsPlanStale() {
970b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
989dba64beSDimitry Andric LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
990b57cec5SDimitry Andric m_class_name.c_str());
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric bool is_stale = true;
1020b57cec5SDimitry Andric if (m_implementation_sp) {
1035ffd83dbSDimitry Andric ScriptInterpreter *script_interp = GetScriptInterpreter();
1040b57cec5SDimitry Andric if (script_interp) {
1050b57cec5SDimitry Andric bool script_error;
1060b57cec5SDimitry Andric is_stale = script_interp->ScriptedThreadPlanIsStale(m_implementation_sp,
1070b57cec5SDimitry Andric script_error);
1080b57cec5SDimitry Andric if (script_error)
1090b57cec5SDimitry Andric SetPlanComplete(false);
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric return is_stale;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
DoPlanExplainsStop(Event * event_ptr)1150b57cec5SDimitry Andric bool ThreadPlanPython::DoPlanExplainsStop(Event *event_ptr) {
1160b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
1179dba64beSDimitry Andric LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
1180b57cec5SDimitry Andric m_class_name.c_str());
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric bool explains_stop = true;
1210b57cec5SDimitry Andric if (m_implementation_sp) {
1225ffd83dbSDimitry Andric ScriptInterpreter *script_interp = GetScriptInterpreter();
1230b57cec5SDimitry Andric if (script_interp) {
1240b57cec5SDimitry Andric bool script_error;
1250b57cec5SDimitry Andric explains_stop = script_interp->ScriptedThreadPlanExplainsStop(
1260b57cec5SDimitry Andric m_implementation_sp, event_ptr, script_error);
1270b57cec5SDimitry Andric if (script_error)
1280b57cec5SDimitry Andric SetPlanComplete(false);
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric return explains_stop;
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric
MischiefManaged()1340b57cec5SDimitry Andric bool ThreadPlanPython::MischiefManaged() {
1350b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
1369dba64beSDimitry Andric LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
1370b57cec5SDimitry Andric m_class_name.c_str());
1380b57cec5SDimitry Andric bool mischief_managed = true;
1390b57cec5SDimitry Andric if (m_implementation_sp) {
1400b57cec5SDimitry Andric // I don't really need mischief_managed, since it's simpler to just call
1410b57cec5SDimitry Andric // SetPlanComplete in should_stop.
1420b57cec5SDimitry Andric mischief_managed = IsPlanComplete();
1430b57cec5SDimitry Andric if (mischief_managed)
1440b57cec5SDimitry Andric m_implementation_sp.reset();
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric return mischief_managed;
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric
GetPlanRunState()1490b57cec5SDimitry Andric lldb::StateType ThreadPlanPython::GetPlanRunState() {
1500b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
1519dba64beSDimitry Andric LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
1520b57cec5SDimitry Andric m_class_name.c_str());
1530b57cec5SDimitry Andric lldb::StateType run_state = eStateRunning;
1540b57cec5SDimitry Andric if (m_implementation_sp) {
1555ffd83dbSDimitry Andric ScriptInterpreter *script_interp = GetScriptInterpreter();
1560b57cec5SDimitry Andric if (script_interp) {
1570b57cec5SDimitry Andric bool script_error;
1580b57cec5SDimitry Andric run_state = script_interp->ScriptedThreadPlanGetRunState(
1590b57cec5SDimitry Andric m_implementation_sp, script_error);
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric return run_state;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric // The ones below are not currently exported to Python.
GetDescription(Stream * s,lldb::DescriptionLevel level)1660b57cec5SDimitry Andric void ThreadPlanPython::GetDescription(Stream *s, lldb::DescriptionLevel level) {
1670b57cec5SDimitry Andric s->Printf("Python thread plan implemented by class %s.",
1680b57cec5SDimitry Andric m_class_name.c_str());
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric
WillStop()1710b57cec5SDimitry Andric bool ThreadPlanPython::WillStop() {
1720b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
1739dba64beSDimitry Andric LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
1740b57cec5SDimitry Andric m_class_name.c_str());
1750b57cec5SDimitry Andric return true;
1760b57cec5SDimitry Andric }
177