1 //===-- ThreadPlanPython.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlan.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Debugger.h"
17 #include "lldb/Core/State.h"
18 #include "lldb/Interpreter/CommandInterpreter.h"
19 #include "lldb/Interpreter/ScriptInterpreter.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/RegisterContext.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/Thread.h"
24 #include "lldb/Target/ThreadPlan.h"
25 #include "lldb/Target/ThreadPlanPython.h"
26 #include "lldb/Utility/Log.h"
29 using namespace lldb_private;
31 //----------------------------------------------------------------------
33 //----------------------------------------------------------------------
35 ThreadPlanPython::ThreadPlanPython(Thread &thread, const char *class_name)
36 : ThreadPlan(ThreadPlan::eKindPython, "Python based Thread Plan", thread,
37 eVoteNoOpinion, eVoteNoOpinion),
38 m_class_name(class_name) {
39 SetIsMasterPlan(true);
40 SetOkayToDiscard(true);
44 ThreadPlanPython::~ThreadPlanPython() {
45 // FIXME, do I need to decrement the ref count on this implementation object
46 // to make it go away?
49 bool ThreadPlanPython::ValidatePlan(Stream *error) {
50 // I have to postpone setting up the implementation till after the constructor
51 // because I need to call
52 // shared_from_this, which you can't do in the constructor. So I'll do it
54 if (m_implementation_sp)
60 void ThreadPlanPython::DidPush() {
61 // We set up the script side in DidPush, so that it can push other plans in
63 // and doesn't have to care about the details of DidPush.
65 if (!m_class_name.empty()) {
66 ScriptInterpreter *script_interp = m_thread.GetProcess()
69 .GetCommandInterpreter()
70 .GetScriptInterpreter();
72 m_implementation_sp = script_interp->CreateScriptedThreadPlan(
73 m_class_name.c_str(), this->shared_from_this());
78 bool ThreadPlanPython::ShouldStop(Event *event_ptr) {
79 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
81 log->Printf("%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
82 m_class_name.c_str());
84 bool should_stop = true;
85 if (m_implementation_sp) {
86 ScriptInterpreter *script_interp = m_thread.GetProcess()
89 .GetCommandInterpreter()
90 .GetScriptInterpreter();
93 should_stop = script_interp->ScriptedThreadPlanShouldStop(
94 m_implementation_sp, event_ptr, script_error);
96 SetPlanComplete(false);
102 bool ThreadPlanPython::IsPlanStale() {
103 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
105 log->Printf("%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
106 m_class_name.c_str());
108 bool is_stale = true;
109 if (m_implementation_sp) {
110 ScriptInterpreter *script_interp = m_thread.GetProcess()
113 .GetCommandInterpreter()
114 .GetScriptInterpreter();
117 is_stale = script_interp->ScriptedThreadPlanIsStale(m_implementation_sp,
120 SetPlanComplete(false);
126 bool ThreadPlanPython::DoPlanExplainsStop(Event *event_ptr) {
127 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
129 log->Printf("%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
130 m_class_name.c_str());
132 bool explains_stop = true;
133 if (m_implementation_sp) {
134 ScriptInterpreter *script_interp = m_thread.GetProcess()
137 .GetCommandInterpreter()
138 .GetScriptInterpreter();
141 explains_stop = script_interp->ScriptedThreadPlanExplainsStop(
142 m_implementation_sp, event_ptr, script_error);
144 SetPlanComplete(false);
147 return explains_stop;
150 bool ThreadPlanPython::MischiefManaged() {
151 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
153 log->Printf("%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
154 m_class_name.c_str());
155 bool mischief_managed = true;
156 if (m_implementation_sp) {
157 // I don't really need mischief_managed, since it's simpler to just call
158 // SetPlanComplete in should_stop.
159 mischief_managed = IsPlanComplete();
160 if (mischief_managed)
161 m_implementation_sp.reset();
163 return mischief_managed;
166 lldb::StateType ThreadPlanPython::GetPlanRunState() {
167 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
169 log->Printf("%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
170 m_class_name.c_str());
171 lldb::StateType run_state = eStateRunning;
172 if (m_implementation_sp) {
173 ScriptInterpreter *script_interp = m_thread.GetProcess()
176 .GetCommandInterpreter()
177 .GetScriptInterpreter();
180 run_state = script_interp->ScriptedThreadPlanGetRunState(
181 m_implementation_sp, script_error);
187 // The ones below are not currently exported to Python.
189 bool ThreadPlanPython::StopOthers() {
190 // For now Python plans run all threads, but we should add some controls for
195 void ThreadPlanPython::GetDescription(Stream *s, lldb::DescriptionLevel level) {
196 s->Printf("Python thread plan implemented by class %s.",
197 m_class_name.c_str());
200 bool ThreadPlanPython::WillStop() {
201 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
203 log->Printf("%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
204 m_class_name.c_str());