180814287SRaphael Isemann //===-- ThreadPlanPython.cpp ----------------------------------------------===// 22bdbfd50SJim Ingham // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 62bdbfd50SJim Ingham // 72bdbfd50SJim Ingham //===----------------------------------------------------------------------===// 82bdbfd50SJim Ingham 92bdbfd50SJim Ingham #include "lldb/Target/ThreadPlan.h" 102bdbfd50SJim Ingham 112bdbfd50SJim Ingham #include "lldb/Core/Debugger.h" 122bdbfd50SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h" 132bdbfd50SJim Ingham #include "lldb/Interpreter/ScriptInterpreter.h" 14b9c1b51eSKate Stone #include "lldb/Target/Process.h" 152bdbfd50SJim Ingham #include "lldb/Target/RegisterContext.h" 16b9c1b51eSKate Stone #include "lldb/Target/Target.h" 172bdbfd50SJim Ingham #include "lldb/Target/Thread.h" 182bdbfd50SJim Ingham #include "lldb/Target/ThreadPlan.h" 192bdbfd50SJim Ingham #include "lldb/Target/ThreadPlanPython.h" 206f9e6901SZachary Turner #include "lldb/Utility/Log.h" 21d821c997SPavel Labath #include "lldb/Utility/State.h" 222bdbfd50SJim Ingham 232bdbfd50SJim Ingham using namespace lldb; 242bdbfd50SJim Ingham using namespace lldb_private; 252bdbfd50SJim Ingham 262bdbfd50SJim Ingham // ThreadPlanPython 272bdbfd50SJim Ingham 2827a14f19SJim Ingham ThreadPlanPython::ThreadPlanPython(Thread &thread, const char *class_name, 2927a14f19SJim Ingham StructuredDataImpl *args_data) 30b9c1b51eSKate Stone : ThreadPlan(ThreadPlan::eKindPython, "Python based Thread Plan", thread, 31b9c1b51eSKate Stone eVoteNoOpinion, eVoteNoOpinion), 3227a14f19SJim Ingham m_class_name(class_name), m_args_data(args_data), m_did_push(false) { 332bdbfd50SJim Ingham SetIsMasterPlan(true); 342bdbfd50SJim Ingham SetOkayToDiscard(true); 352bdbfd50SJim Ingham SetPrivate(false); 362bdbfd50SJim Ingham } 372bdbfd50SJim Ingham 38b9c1b51eSKate Stone ThreadPlanPython::~ThreadPlanPython() { 39b9c1b51eSKate Stone // FIXME, do I need to decrement the ref count on this implementation object 40b9c1b51eSKate Stone // to make it go away? 412bdbfd50SJim Ingham } 422bdbfd50SJim Ingham 43b9c1b51eSKate Stone bool ThreadPlanPython::ValidatePlan(Stream *error) { 44e103ae92SJonas Devlieghere if (!m_did_push) 452bdbfd50SJim Ingham return true; 46e103ae92SJonas Devlieghere 47e103ae92SJonas Devlieghere if (!m_implementation_sp) { 48e103ae92SJonas Devlieghere if (error) 4993c98346SJim Ingham error->Printf("Error constructing Python ThreadPlan: %s", 5093c98346SJim Ingham m_error_str.empty() ? "<unknown error>" 5193c98346SJim Ingham : m_error_str.c_str()); 522bdbfd50SJim Ingham return false; 532bdbfd50SJim Ingham } 542bdbfd50SJim Ingham 55e103ae92SJonas Devlieghere return true; 56e103ae92SJonas Devlieghere } 57e103ae92SJonas Devlieghere 58*e4598dc0SJim Ingham ScriptInterpreter *ThreadPlanPython::GetScriptInterpreter() { 59*e4598dc0SJim Ingham return m_process.GetTarget().GetDebugger().GetScriptInterpreter(); 60*e4598dc0SJim Ingham } 61*e4598dc0SJim Ingham 62b9c1b51eSKate Stone void ThreadPlanPython::DidPush() { 63b9c1b51eSKate Stone // We set up the script side in DidPush, so that it can push other plans in 6405097246SAdrian Prantl // the constructor, and doesn't have to care about the details of DidPush. 65e103ae92SJonas Devlieghere m_did_push = true; 66b9c1b51eSKate Stone if (!m_class_name.empty()) { 67*e4598dc0SJim Ingham ScriptInterpreter *script_interp = GetScriptInterpreter(); 68b9c1b51eSKate Stone if (script_interp) { 69b9c1b51eSKate Stone m_implementation_sp = script_interp->CreateScriptedThreadPlan( 7027a14f19SJim Ingham m_class_name.c_str(), m_args_data, m_error_str, 7127a14f19SJim Ingham this->shared_from_this()); 722bdbfd50SJim Ingham } 732bdbfd50SJim Ingham } 742bdbfd50SJim Ingham } 752bdbfd50SJim Ingham 76b9c1b51eSKate Stone bool ThreadPlanPython::ShouldStop(Event *event_ptr) { 772bdbfd50SJim Ingham Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 7863e5fb76SJonas Devlieghere LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION, 79b9c1b51eSKate Stone m_class_name.c_str()); 802bdbfd50SJim Ingham 812bdbfd50SJim Ingham bool should_stop = true; 82b9c1b51eSKate Stone if (m_implementation_sp) { 83*e4598dc0SJim Ingham ScriptInterpreter *script_interp = GetScriptInterpreter(); 84b9c1b51eSKate Stone if (script_interp) { 852bdbfd50SJim Ingham bool script_error; 86b9c1b51eSKate Stone should_stop = script_interp->ScriptedThreadPlanShouldStop( 87b9c1b51eSKate Stone m_implementation_sp, event_ptr, script_error); 882bdbfd50SJim Ingham if (script_error) 892bdbfd50SJim Ingham SetPlanComplete(false); 902bdbfd50SJim Ingham } 912bdbfd50SJim Ingham } 922bdbfd50SJim Ingham return should_stop; 932bdbfd50SJim Ingham } 942bdbfd50SJim Ingham 95b9c1b51eSKate Stone bool ThreadPlanPython::IsPlanStale() { 96c915a7d2SJim Ingham Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 9763e5fb76SJonas Devlieghere LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION, 98b9c1b51eSKate Stone m_class_name.c_str()); 99c915a7d2SJim Ingham 100c915a7d2SJim Ingham bool is_stale = true; 101b9c1b51eSKate Stone if (m_implementation_sp) { 102*e4598dc0SJim Ingham ScriptInterpreter *script_interp = GetScriptInterpreter(); 103b9c1b51eSKate Stone if (script_interp) { 104c915a7d2SJim Ingham bool script_error; 105b9c1b51eSKate Stone is_stale = script_interp->ScriptedThreadPlanIsStale(m_implementation_sp, 106b9c1b51eSKate Stone script_error); 107c915a7d2SJim Ingham if (script_error) 108c915a7d2SJim Ingham SetPlanComplete(false); 109c915a7d2SJim Ingham } 110c915a7d2SJim Ingham } 111c915a7d2SJim Ingham return is_stale; 112c915a7d2SJim Ingham } 113c915a7d2SJim Ingham 114b9c1b51eSKate Stone bool ThreadPlanPython::DoPlanExplainsStop(Event *event_ptr) { 1152bdbfd50SJim Ingham Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 11663e5fb76SJonas Devlieghere LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION, 117b9c1b51eSKate Stone m_class_name.c_str()); 1182bdbfd50SJim Ingham 1192bdbfd50SJim Ingham bool explains_stop = true; 120b9c1b51eSKate Stone if (m_implementation_sp) { 121*e4598dc0SJim Ingham ScriptInterpreter *script_interp = GetScriptInterpreter(); 122b9c1b51eSKate Stone if (script_interp) { 1232bdbfd50SJim Ingham bool script_error; 124b9c1b51eSKate Stone explains_stop = script_interp->ScriptedThreadPlanExplainsStop( 125b9c1b51eSKate Stone m_implementation_sp, event_ptr, script_error); 1262bdbfd50SJim Ingham if (script_error) 1272bdbfd50SJim Ingham SetPlanComplete(false); 1282bdbfd50SJim Ingham } 1292bdbfd50SJim Ingham } 1302bdbfd50SJim Ingham return explains_stop; 1312bdbfd50SJim Ingham } 1322bdbfd50SJim Ingham 133b9c1b51eSKate Stone bool ThreadPlanPython::MischiefManaged() { 1342bdbfd50SJim Ingham Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 13563e5fb76SJonas Devlieghere LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION, 136b9c1b51eSKate Stone m_class_name.c_str()); 1372bdbfd50SJim Ingham bool mischief_managed = true; 138b9c1b51eSKate Stone if (m_implementation_sp) { 139b9c1b51eSKate Stone // I don't really need mischief_managed, since it's simpler to just call 140b9c1b51eSKate Stone // SetPlanComplete in should_stop. 1412bdbfd50SJim Ingham mischief_managed = IsPlanComplete(); 1422bdbfd50SJim Ingham if (mischief_managed) 1432bdbfd50SJim Ingham m_implementation_sp.reset(); 1442bdbfd50SJim Ingham } 1452bdbfd50SJim Ingham return mischief_managed; 1462bdbfd50SJim Ingham } 1472bdbfd50SJim Ingham 148b9c1b51eSKate Stone lldb::StateType ThreadPlanPython::GetPlanRunState() { 1492bdbfd50SJim Ingham Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 15063e5fb76SJonas Devlieghere LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION, 1512bdbfd50SJim Ingham m_class_name.c_str()); 1522bdbfd50SJim Ingham lldb::StateType run_state = eStateRunning; 153b9c1b51eSKate Stone if (m_implementation_sp) { 154*e4598dc0SJim Ingham ScriptInterpreter *script_interp = GetScriptInterpreter(); 155b9c1b51eSKate Stone if (script_interp) { 1562bdbfd50SJim Ingham bool script_error; 157b9c1b51eSKate Stone run_state = script_interp->ScriptedThreadPlanGetRunState( 158b9c1b51eSKate Stone m_implementation_sp, script_error); 1592bdbfd50SJim Ingham } 1602bdbfd50SJim Ingham } 1612bdbfd50SJim Ingham return run_state; 1622bdbfd50SJim Ingham } 1632bdbfd50SJim Ingham 1642bdbfd50SJim Ingham // The ones below are not currently exported to Python. 1652bdbfd50SJim Ingham 166b9c1b51eSKate Stone bool ThreadPlanPython::StopOthers() { 167b9c1b51eSKate Stone // For now Python plans run all threads, but we should add some controls for 168b9c1b51eSKate Stone // this. 1692bdbfd50SJim Ingham return false; 1702bdbfd50SJim Ingham } 1712bdbfd50SJim Ingham 172b9c1b51eSKate Stone void ThreadPlanPython::GetDescription(Stream *s, lldb::DescriptionLevel level) { 173b9c1b51eSKate Stone s->Printf("Python thread plan implemented by class %s.", 174b9c1b51eSKate Stone m_class_name.c_str()); 1752bdbfd50SJim Ingham } 1762bdbfd50SJim Ingham 177b9c1b51eSKate Stone bool ThreadPlanPython::WillStop() { 1782bdbfd50SJim Ingham Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 17963e5fb76SJonas Devlieghere LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION, 180b9c1b51eSKate Stone m_class_name.c_str()); 1812bdbfd50SJim Ingham return true; 1822bdbfd50SJim Ingham } 183