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 = Status::FromErrorStringWithFormat(
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 = Status::FromErrorStringWithFormat(
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) {
188 GetLog(LLDBLog::Thread), std::move(err),
189 "Can't call ScriptedThreadPlan::GetStopDescription: {0}");
190 s->Printf("Scripted thread plan implemented by class %s.",
191 m_class_name.c_str());
192 } else
193 s->PutCString(
194 reinterpret_cast<StreamString *>(stream.get())->GetData());
195 }
196 return;
197 }
198 // It's an error not to have a description, so if we get here, we should
199 // add something.
201 s->Printf("Scripted thread plan implemented by class %s.",
202 m_class_name.c_str());
204}
205
206// The ones below are not currently exported to Python.
208 Log *log = GetLog(LLDBLog::Thread);
209 LLDB_LOGF(log, "%s called on Scripted Thread Plan: %s )",
210 LLVM_PRETTY_FUNCTION, m_class_name.c_str());
211 return true;
212}
213
215 bool current_plan) {
217 return true;
218}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:376
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:392
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, std::optional< lldb::ScriptLanguage > language={})
Definition: Debugger.cpp:1719
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1246
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:1080
void SetPrivate(bool input)
Definition: ThreadPlan.h:431
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:66
void SetOkayToDiscard(bool value)
Definition: ThreadPlan.h:418
bool SetIsControllingPlan(bool value)
Definition: ThreadPlan.h:410
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:332
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:432