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"
21#include "lldb/Utility/Log.h"
22#include "lldb/Utility/State.h"
23#include "llvm/Support/FormatVariadic.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
29 Thread &thread, const ScriptedMetadata &scripted_metadata)
30 : ThreadPlan(ThreadPlan::eKindPython, "Script based Thread Plan", thread,
32 m_scripted_metadata(scripted_metadata), 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
75 return m_process.GetTarget().GetDebugger().GetScriptInterpreter();
76}
77
79 if (m_interface && m_interface->GetScriptedMetadata())
80 return m_interface->GetScriptedMetadata()->GetClassName();
81 return "<unknown>";
82}
83
85 // We set up the script side in DidPush, so that it can push other plans in
86 // the constructor, and doesn't have to care about the details of DidPush.
87 m_did_push = true;
88 if (m_interface) {
89 auto obj_or_err = m_interface->CreatePluginObject(m_scripted_metadata,
90 this->shared_from_this());
91 if (!obj_or_err) {
92 m_error_str = llvm::toString(obj_or_err.takeError());
94 llvm::formatv("failed to create ScriptedThreadPlan: {0}", m_error_str)
95 .str(),
96 m_process.GetTarget().GetDebugger().GetID());
97 SetPlanComplete(false);
98 } else
99 m_implementation_sp = *obj_or_err;
100 }
101}
102
104 Log *log = GetLog(LLDBLog::Thread);
105 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
106
107 bool should_stop = true;
109 auto should_stop_or_err = m_interface->ShouldStop(event_ptr);
110 if (!should_stop_or_err) {
111 LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), should_stop_or_err.takeError(),
112 "Can't call ScriptedThreadPlan::ShouldStop.");
113 SetPlanComplete(false);
114 } else
115 should_stop = *should_stop_or_err;
116 }
117 return should_stop;
118}
119
121 Log *log = GetLog(LLDBLog::Thread);
122 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
123
124 bool is_stale = true;
126 auto is_stale_or_err = m_interface->IsStale();
127 if (!is_stale_or_err) {
128 LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), is_stale_or_err.takeError(),
129 "Can't call ScriptedThreadPlan::IsStale.");
130 SetPlanComplete(false);
131 } else
132 is_stale = *is_stale_or_err;
133 }
134 return is_stale;
135}
136
138 Log *log = GetLog(LLDBLog::Thread);
139 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
140
141 bool explains_stop = true;
143 auto explains_stop_or_error = m_interface->ExplainsStop(event_ptr);
144 if (!explains_stop_or_error) {
146 explains_stop_or_error.takeError(),
147 "Can't call ScriptedThreadPlan::ExplainsStop.");
148 SetPlanComplete(false);
149 } else
150 explains_stop = *explains_stop_or_error;
151 }
152 return explains_stop;
153}
154
156 Log *log = GetLog(LLDBLog::Thread);
157 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
158 bool mischief_managed = true;
160 // I don't really need mischief_managed, since it's simpler to just call
161 // SetPlanComplete in should_stop.
162 mischief_managed = IsPlanComplete();
163 if (mischief_managed) {
164 // We need to cache the stop reason here we'll need it in GetDescription.
166 m_implementation_sp.reset();
167 }
168 }
169 return mischief_managed;
170}
171
173 Log *log = GetLog(LLDBLog::Thread);
174 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
175 lldb::StateType run_state = eStateRunning;
177 run_state = m_interface->GetRunState();
178 return run_state;
179}
180
183 Log *log = GetLog(LLDBLog::Thread);
184 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
186 ScriptInterpreter *script_interp = GetScriptInterpreter();
187 if (script_interp) {
188 lldb::StreamSP stream = std::make_shared<lldb_private::StreamString>();
189 llvm::Error err = m_interface->GetStopDescription(stream);
190 if (err) {
192 GetLog(LLDBLog::Thread), std::move(err),
193 "Can't call ScriptedThreadPlan::GetStopDescription: {0}");
194 s->Format("Scripted thread plan implemented by class {0}.",
196 } else
197 s->PutCString(
198 reinterpret_cast<StreamString *>(stream.get())->GetData());
199 }
200 return;
201 }
202 // It's an error not to have a description, so if we get here, we should
203 // add something.
204 if (m_stop_description.Empty())
205 s->Format("Scripted thread plan implemented by class {0}.",
207 s->PutCString(m_stop_description.GetData());
208}
209
210// The ones below are not currently exported to Python.
212 Log *log = GetLog(LLDBLog::Thread);
213 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
214 return true;
215}
216
218 bool current_plan) {
219 m_stop_description.Clear();
220 return true;
221}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:405
static void ReportError(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report error events.
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
llvm::StringRef GetScriptClassName() const
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
lldb::StateType GetPlanRunState() override
ScriptedThreadPlan(Thread &thread, const ScriptedMetadata &scripted_metadata)
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:370
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
void SetPrivate(bool input)
Definition ThreadPlan.h:440
ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote report_stop_vote, Vote report_run_vote)
void SetPlanComplete(bool success=true)
void SetOkayToDiscard(bool value)
Definition ThreadPlan.h:427
bool SetIsControllingPlan(bool value)
Definition ThreadPlan.h:419
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:338
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