LLDB mainline
ThreadPlanPython.cpp
Go to the documentation of this file.
1//===-- ThreadPlanPython.cpp ----------------------------------------------===//
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
10
11#include "lldb/Core/Debugger.h"
14#include "lldb/Target/Process.h"
16#include "lldb/Target/Target.h"
17#include "lldb/Target/Thread.h"
21#include "lldb/Utility/Log.h"
22#include "lldb/Utility/State.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
27// ThreadPlanPython
28
29ThreadPlanPython::ThreadPlanPython(Thread &thread, const char *class_name,
30 const StructuredDataImpl &args_data)
31 : ThreadPlan(ThreadPlan::eKindPython, "Python based Thread Plan", thread,
33 m_class_name(class_name), m_args_data(args_data), m_did_push(false),
34 m_stop_others(false) {
36 SetOkayToDiscard(true);
37 SetPrivate(false);
38}
39
41 if (!m_did_push)
42 return true;
43
45 if (error)
46 error->Printf("Error constructing Python ThreadPlan: %s",
47 m_error_str.empty() ? "<unknown error>"
48 : m_error_str.c_str());
49 return false;
50 }
51
52 return true;
53}
54
57}
58
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.
62 m_did_push = true;
63 if (!m_class_name.empty()) {
64 ScriptInterpreter *script_interp = GetScriptInterpreter();
65 if (script_interp) {
68 this->shared_from_this());
69 }
70 }
71}
72
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;
80 ScriptInterpreter *script_interp = GetScriptInterpreter();
81 if (script_interp) {
82 bool script_error;
83 should_stop = script_interp->ScriptedThreadPlanShouldStop(
84 m_implementation_sp, event_ptr, script_error);
85 if (script_error)
86 SetPlanComplete(false);
87 }
88 }
89 return should_stop;
90}
91
94 LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
95 m_class_name.c_str());
96
97 bool is_stale = true;
99 ScriptInterpreter *script_interp = GetScriptInterpreter();
100 if (script_interp) {
101 bool script_error;
102 is_stale = script_interp->ScriptedThreadPlanIsStale(m_implementation_sp,
103 script_error);
104 if (script_error)
105 SetPlanComplete(false);
106 }
107 }
108 return is_stale;
109}
110
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());
115
116 bool explains_stop = true;
118 ScriptInterpreter *script_interp = GetScriptInterpreter();
119 if (script_interp) {
120 bool script_error;
121 explains_stop = script_interp->ScriptedThreadPlanExplainsStop(
122 m_implementation_sp, event_ptr, script_error);
123 if (script_error)
124 SetPlanComplete(false);
125 }
126 }
127 return explains_stop;
128}
129
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;
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.
142 m_implementation_sp.reset();
143 }
144 }
145 return mischief_managed;
146}
147
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;
154 ScriptInterpreter *script_interp = GetScriptInterpreter();
155 if (script_interp) {
156 bool script_error;
157 run_state = script_interp->ScriptedThreadPlanGetRunState(
158 m_implementation_sp, script_error);
159 }
160 }
161 return run_state;
162}
163
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());
169 ScriptInterpreter *script_interp = GetScriptInterpreter();
170 if (script_interp) {
171 bool script_error;
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());
177 }
178 return;
179 }
180 // It's an error not to have a description, so if we get here, we should
181 // add something.
183 s->Printf("Python thread plan implemented by class %s.",
184 m_class_name.c_str());
186}
187
188// The ones below are not currently exported to Python.
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());
193 return true;
194}
195
197 bool current_plan) {
199 return true;
200}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:349
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, std::optional< lldb::ScriptLanguage > language={})
Definition: Debugger.cpp:1644
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1276
virtual bool ScriptedThreadPlanGetStopDescription(StructuredData::ObjectSP implementor_sp, lldb_private::Stream *stream, bool &script_error)
virtual StructuredData::ObjectSP CreateScriptedThreadPlan(const char *class_name, const StructuredDataImpl &args_data, std::string &error_str, lldb::ThreadPlanSP thread_plan_sp)
virtual bool ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error)
virtual lldb::StateType ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp, bool &script_error)
virtual bool ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error)
virtual bool ScriptedThreadPlanIsStale(StructuredData::ObjectSP implementor_sp, bool &script_error)
const char * GetData() const
Definition: StreamString.h:43
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
Debugger & GetDebugger()
Definition: Target.h:1053
ScriptInterpreter * GetScriptInterpreter()
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
bool DoPlanExplainsStop(Event *event_ptr) override
StructuredData::ObjectSP m_implementation_sp
bool ShouldStop(Event *event_ptr) override
bool DoWillResume(lldb::StateType resume_state, bool current_plan) override
ThreadPlanPython(Thread &thread, const char *class_name, const StructuredDataImpl &args_data)
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
lldb::StateType GetPlanRunState() override
void SetPrivate(bool input)
Definition: ThreadPlan.h:419
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:66
void SetOkayToDiscard(bool value)
Definition: ThreadPlan.h:406
bool SetIsControllingPlan(bool value)
Definition: ThreadPlan.h:398
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:314
Definition: SBAddress.h:15
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
StateType
Process and Thread States.
@ eStateRunning
Process or thread is running and can't be examined.