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