1 //===-- ThreadPlanPython.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/ThreadPlan.h"
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/LLDBLog.h"
21 #include "lldb/Utility/Log.h"
22 #include "lldb/Utility/State.h"
25 using namespace lldb_private
;
29 ThreadPlanPython::ThreadPlanPython(Thread
&thread
, const char *class_name
,
30 const StructuredDataImpl
&args_data
)
31 : ThreadPlan(ThreadPlan::eKindPython
, "Python based Thread Plan", thread
,
32 eVoteNoOpinion
, eVoteNoOpinion
),
33 m_class_name(class_name
), m_args_data(args_data
), m_did_push(false),
34 m_stop_others(false) {
35 SetIsControllingPlan(true);
36 SetOkayToDiscard(true);
40 bool ThreadPlanPython::ValidatePlan(Stream
*error
) {
44 if (!m_implementation_sp
) {
46 error
->Printf("Error constructing Python ThreadPlan: %s",
47 m_error_str
.empty() ? "<unknown error>"
48 : m_error_str
.c_str());
55 ScriptInterpreter
*ThreadPlanPython::GetScriptInterpreter() {
56 return m_process
.GetTarget().GetDebugger().GetScriptInterpreter();
59 void ThreadPlanPython::DidPush() {
60 // We set up the script side in DidPush, so that it can push other plans in
61 // the constructor, and doesn't have to care about the details of DidPush.
63 if (!m_class_name
.empty()) {
64 ScriptInterpreter
*script_interp
= GetScriptInterpreter();
66 m_implementation_sp
= script_interp
->CreateScriptedThreadPlan(
67 m_class_name
.c_str(), m_args_data
, m_error_str
,
68 this->shared_from_this());
73 bool ThreadPlanPython::ShouldStop(Event
*event_ptr
) {
74 Log
*log
= GetLog(LLDBLog::Thread
);
75 LLDB_LOGF(log
, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION
,
76 m_class_name
.c_str());
78 bool should_stop
= true;
79 if (m_implementation_sp
) {
80 ScriptInterpreter
*script_interp
= GetScriptInterpreter();
83 should_stop
= script_interp
->ScriptedThreadPlanShouldStop(
84 m_implementation_sp
, event_ptr
, script_error
);
86 SetPlanComplete(false);
92 bool ThreadPlanPython::IsPlanStale() {
93 Log
*log
= GetLog(LLDBLog::Thread
);
94 LLDB_LOGF(log
, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION
,
95 m_class_name
.c_str());
98 if (m_implementation_sp
) {
99 ScriptInterpreter
*script_interp
= GetScriptInterpreter();
102 is_stale
= script_interp
->ScriptedThreadPlanIsStale(m_implementation_sp
,
105 SetPlanComplete(false);
111 bool ThreadPlanPython::DoPlanExplainsStop(Event
*event_ptr
) {
112 Log
*log
= GetLog(LLDBLog::Thread
);
113 LLDB_LOGF(log
, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION
,
114 m_class_name
.c_str());
116 bool explains_stop
= true;
117 if (m_implementation_sp
) {
118 ScriptInterpreter
*script_interp
= GetScriptInterpreter();
121 explains_stop
= script_interp
->ScriptedThreadPlanExplainsStop(
122 m_implementation_sp
, event_ptr
, script_error
);
124 SetPlanComplete(false);
127 return explains_stop
;
130 bool ThreadPlanPython::MischiefManaged() {
131 Log
*log
= GetLog(LLDBLog::Thread
);
132 LLDB_LOGF(log
, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION
,
133 m_class_name
.c_str());
134 bool mischief_managed
= true;
135 if (m_implementation_sp
) {
136 // I don't really need mischief_managed, since it's simpler to just call
137 // SetPlanComplete in should_stop.
138 mischief_managed
= IsPlanComplete();
139 if (mischief_managed
) {
140 // We need to cache the stop reason here we'll need it in GetDescription.
141 GetDescription(&m_stop_description
, eDescriptionLevelBrief
);
142 m_implementation_sp
.reset();
145 return mischief_managed
;
148 lldb::StateType
ThreadPlanPython::GetPlanRunState() {
149 Log
*log
= GetLog(LLDBLog::Thread
);
150 LLDB_LOGF(log
, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION
,
151 m_class_name
.c_str());
152 lldb::StateType run_state
= eStateRunning
;
153 if (m_implementation_sp
) {
154 ScriptInterpreter
*script_interp
= GetScriptInterpreter();
157 run_state
= script_interp
->ScriptedThreadPlanGetRunState(
158 m_implementation_sp
, script_error
);
164 void ThreadPlanPython::GetDescription(Stream
*s
, lldb::DescriptionLevel level
) {
165 Log
*log
= GetLog(LLDBLog::Thread
);
166 LLDB_LOGF(log
, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION
,
167 m_class_name
.c_str());
168 if (m_implementation_sp
) {
169 ScriptInterpreter
*script_interp
= GetScriptInterpreter();
172 bool added_desc
= script_interp
->ScriptedThreadPlanGetStopDescription(
173 m_implementation_sp
, s
, script_error
);
174 if (script_error
|| !added_desc
)
175 s
->Printf("Python thread plan implemented by class %s.",
176 m_class_name
.c_str());
180 // It's an error not to have a description, so if we get here, we should
182 if (m_stop_description
.Empty())
183 s
->Printf("Python thread plan implemented by class %s.",
184 m_class_name
.c_str());
185 s
->PutCString(m_stop_description
.GetData());
188 // The ones below are not currently exported to Python.
189 bool ThreadPlanPython::WillStop() {
190 Log
*log
= GetLog(LLDBLog::Thread
);
191 LLDB_LOGF(log
, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION
,
192 m_class_name
.c_str());
196 bool ThreadPlanPython::DoWillResume(lldb::StateType resume_state
,
198 m_stop_description
.Clear();