LLDB mainline
ScriptedThreadPlan.cpp
Go to the documentation of this file.
1//===-- ScriptedThreadPlan.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"
15#include "lldb/Target/Process.h"
18#include "lldb/Target/Target.h"
19#include "lldb/Target/Thread.h"
22#include "lldb/Utility/Log.h"
23#include "lldb/Utility/State.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
28ScriptedThreadPlan::ScriptedThreadPlan(Thread &thread, const char *class_name,
29 const StructuredDataImpl &args_data)
30 : ThreadPlan(ThreadPlan::eKindPython, "Script based Thread Plan", thread,
32 m_class_name(class_name), m_args_data(args_data), m_did_push(false),
33 m_stop_others(false) {
35 if (!interpreter) {
36 SetPlanComplete(false);
37 // FIXME: error handling
38 // error.SetErrorStringWithFormat(
39 // "ScriptedThreadPlan::%s () - ERROR: %s", __FUNCTION__,
40 // "Couldn't get script interpreter");
41 return;
42 }
43
45 if (!m_interface) {
46 SetPlanComplete(false);
47 // FIXME: error handling
48 // error.SetErrorStringWithFormat(
49 // "ScriptedThreadPlan::%s () - ERROR: %s", __FUNCTION__,
50 // "Script interpreter couldn't create Scripted Thread Plan Interface");
51 return;
52 }
53
55 SetOkayToDiscard(true);
56 SetPrivate(false);
57}
58
60 if (!m_did_push)
61 return true;
62
64 if (error)
65 error->Printf("Error constructing Python ThreadPlan: %s",
66 m_error_str.empty() ? "<unknown error>"
67 : m_error_str.c_str());
68 return false;
69 }
70
71 return true;
72}
73
76}
77
79 // We set up the script side in DidPush, so that it can push other plans in
80 // the constructor, and doesn't have to care about the details of DidPush.
81 m_did_push = true;
82 if (m_interface) {
83 auto obj_or_err = m_interface->CreatePluginObject(
84 m_class_name, this->shared_from_this(), m_args_data);
85 if (!obj_or_err) {
86 m_error_str = llvm::toString(obj_or_err.takeError());
87 SetPlanComplete(false);
88 } else
89 m_implementation_sp = *obj_or_err;
90 }
91}
92
95 LLDB_LOGF(log, "%s called on Scripted Thread Plan: %s )",
96 LLVM_PRETTY_FUNCTION, m_class_name.c_str());
97
98 bool should_stop = true;
100 auto should_stop_or_err = m_interface->ShouldStop(event_ptr);
101 if (!should_stop_or_err) {
102 LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), should_stop_or_err.takeError(),
103 "Can't call ScriptedThreadPlan::ShouldStop.");
104 SetPlanComplete(false);
105 } else
106 should_stop = *should_stop_or_err;
107 }
108 return should_stop;
109}
110
112 Log *log = GetLog(LLDBLog::Thread);
113 LLDB_LOGF(log, "%s called on Scripted Thread Plan: %s )",
114 LLVM_PRETTY_FUNCTION, m_class_name.c_str());
115
116 bool is_stale = true;
118 auto is_stale_or_err = m_interface->IsStale();
119 if (!is_stale_or_err) {
120 LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), is_stale_or_err.takeError(),
121 "Can't call ScriptedThreadPlan::IsStale.");
122 SetPlanComplete(false);
123 } else
124 is_stale = *is_stale_or_err;
125 }
126 return is_stale;
127}
128
130 Log *log = GetLog(LLDBLog::Thread);
131 LLDB_LOGF(log, "%s called on Scripted Thread Plan: %s )",
132 LLVM_PRETTY_FUNCTION, m_class_name.c_str());
133
134 bool explains_stop = true;
136 auto explains_stop_or_error = m_interface->ExplainsStop(event_ptr);
137 if (!explains_stop_or_error) {
139 explains_stop_or_error.takeError(),
140 "Can't call ScriptedThreadPlan::ExplainsStop.");
141 SetPlanComplete(false);
142 } else
143 explains_stop = *explains_stop_or_error;
144 }
145 return explains_stop;
146}
147
149 Log *log = GetLog(LLDBLog::Thread);
150 LLDB_LOGF(log, "%s called on Scripted Thread Plan: %s )",
151 LLVM_PRETTY_FUNCTION, m_class_name.c_str());
152 bool mischief_managed = true;
154 // I don't really need mischief_managed, since it's simpler to just call
155 // SetPlanComplete in should_stop.
156 mischief_managed = IsPlanComplete();
157 if (mischief_managed) {
158 // We need to cache the stop reason here we'll need it in GetDescription.
160 m_implementation_sp.reset();
161 }
162 }
163 return mischief_managed;
164}
165
167 Log *log = GetLog(LLDBLog::Thread);
168 LLDB_LOGF(log, "%s called on Scripted Thread Plan: %s )",
169 LLVM_PRETTY_FUNCTION, m_class_name.c_str());
170 lldb::StateType run_state = eStateRunning;
172 run_state = m_interface->GetRunState();
173 return run_state;
174}
175
178 Log *log = GetLog(LLDBLog::Thread);
179 LLDB_LOGF(log, "%s called on Scripted Thread Plan: %s )",
180 LLVM_PRETTY_FUNCTION, m_class_name.c_str());
182 ScriptInterpreter *script_interp = GetScriptInterpreter();
183 if (script_interp) {
184 lldb::StreamSP stream = std::make_shared<lldb_private::StreamString>();
185 llvm::Error err = m_interface->GetStopDescription(stream);
186 if (err) {
187 LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), std::move(err),
188 "Can't call ScriptedThreadPlan::GetStopDescription.");
189 s->Printf("Scripted thread plan implemented by class %s.",
190 m_class_name.c_str());
191 } else
192 s->PutCString(
193 reinterpret_cast<StreamString *>(stream.get())->GetData());
194 }
195 return;
196 }
197 // It's an error not to have a description, so if we get here, we should
198 // add something.
200 s->Printf("Scripted thread plan implemented by class %s.",
201 m_class_name.c_str());
203}
204
205// The ones below are not currently exported to Python.
207 Log *log = GetLog(LLDBLog::Thread);
208 LLDB_LOGF(log, "%s called on Scripted Thread Plan: %s )",
209 LLVM_PRETTY_FUNCTION, m_class_name.c_str());
210 return true;
211}
212
214 bool current_plan) {
216 return true;
217}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:366
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:382
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, std::optional< lldb::ScriptLanguage > language={})
Definition: Debugger.cpp:1684
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1285
virtual lldb::ScriptedThreadPlanInterfaceSP CreateScriptedThreadPlanInterface()
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
lldb::ScriptedThreadPlanInterfaceSP m_interface
bool ShouldStop(Event *event_ptr) override
StructuredData::ObjectSP m_implementation_sp
bool DoPlanExplainsStop(Event *event_ptr) override
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
ScriptInterpreter * GetScriptInterpreter()
bool DoWillResume(lldb::StateType resume_state, bool current_plan) override
ScriptedThreadPlan(Thread &thread, const char *class_name, const StructuredDataImpl &args_data)
lldb::StateType GetPlanRunState() override
const char * GetData() const
Definition: StreamString.h:45
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:1069
void SetPrivate(bool input)
Definition: ThreadPlan.h:425
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:66
void SetOkayToDiscard(bool value)
Definition: ThreadPlan.h:412
bool SetIsControllingPlan(bool value)
Definition: ThreadPlan.h:404
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:331
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.
std::shared_ptr< lldb_private::Stream > StreamSP
Definition: lldb-forward.h:428