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
24using namespace lldb;
25using namespace lldb_private;
26
28 Thread &thread, const ScriptedMetadata &scripted_metadata)
29 : ThreadPlan(ThreadPlan::eKindPython, "Script based Thread Plan", thread,
31 m_scripted_metadata(scripted_metadata), m_did_push(false),
32 m_stop_others(false) {
34 if (!interpreter) {
35 SetPlanComplete(false);
36 // FIXME: error handling
37 // error = Status::FromErrorStringWithFormat(
38 // "ScriptedThreadPlan::%s () - ERROR: %s", __FUNCTION__,
39 // "Couldn't get script interpreter");
40 return;
41 }
42
44 if (!m_interface) {
45 SetPlanComplete(false);
46 // FIXME: error handling
47 // error = Status::FromErrorStringWithFormat(
48 // "ScriptedThreadPlan::%s () - ERROR: %s", __FUNCTION__,
49 // "Script interpreter couldn't create Scripted Thread Plan Interface");
50 return;
51 }
52
54 SetOkayToDiscard(true);
55 SetPrivate(false);
56}
57
59 if (!m_did_push)
60 return true;
61
63 if (error)
64 error->Printf("Error constructing Python ThreadPlan: %s",
65 m_error_str.empty() ? "<unknown error>"
66 : m_error_str.c_str());
67 return false;
68 }
69
70 return true;
71}
72
74 return m_process.GetTarget().GetDebugger().GetScriptInterpreter();
75}
76
78 if (m_interface && m_interface->GetScriptedMetadata())
79 return m_interface->GetScriptedMetadata()->GetClassName();
80 return "<unknown>";
81}
82
84 // We set up the script side in DidPush, so that it can push other plans in
85 // the constructor, and doesn't have to care about the details of DidPush.
86 m_did_push = true;
87 if (m_interface) {
88 auto obj_or_err = m_interface->CreatePluginObject(m_scripted_metadata,
89 this->shared_from_this());
90 if (!obj_or_err) {
91 m_error_str = llvm::toString(obj_or_err.takeError());
92 SetPlanComplete(false);
93 } else
94 m_implementation_sp = *obj_or_err;
95 }
96}
97
100 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
101
102 bool should_stop = true;
104 auto should_stop_or_err = m_interface->ShouldStop(event_ptr);
105 if (!should_stop_or_err) {
106 LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), should_stop_or_err.takeError(),
107 "Can't call ScriptedThreadPlan::ShouldStop.");
108 SetPlanComplete(false);
109 } else
110 should_stop = *should_stop_or_err;
111 }
112 return should_stop;
113}
114
116 Log *log = GetLog(LLDBLog::Thread);
117 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
118
119 bool is_stale = true;
121 auto is_stale_or_err = m_interface->IsStale();
122 if (!is_stale_or_err) {
123 LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), is_stale_or_err.takeError(),
124 "Can't call ScriptedThreadPlan::IsStale.");
125 SetPlanComplete(false);
126 } else
127 is_stale = *is_stale_or_err;
128 }
129 return is_stale;
130}
131
133 Log *log = GetLog(LLDBLog::Thread);
134 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
135
136 bool explains_stop = true;
138 auto explains_stop_or_error = m_interface->ExplainsStop(event_ptr);
139 if (!explains_stop_or_error) {
141 explains_stop_or_error.takeError(),
142 "Can't call ScriptedThreadPlan::ExplainsStop.");
143 SetPlanComplete(false);
144 } else
145 explains_stop = *explains_stop_or_error;
146 }
147 return explains_stop;
148}
149
151 Log *log = GetLog(LLDBLog::Thread);
152 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
153 bool mischief_managed = true;
155 // I don't really need mischief_managed, since it's simpler to just call
156 // SetPlanComplete in should_stop.
157 mischief_managed = IsPlanComplete();
158 if (mischief_managed) {
159 // We need to cache the stop reason here we'll need it in GetDescription.
161 m_implementation_sp.reset();
162 }
163 }
164 return mischief_managed;
165}
166
168 Log *log = GetLog(LLDBLog::Thread);
169 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
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_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
181 ScriptInterpreter *script_interp = GetScriptInterpreter();
182 if (script_interp) {
183 lldb::StreamSP stream = std::make_shared<lldb_private::StreamString>();
184 llvm::Error err = m_interface->GetStopDescription(stream);
185 if (err) {
187 GetLog(LLDBLog::Thread), std::move(err),
188 "Can't call ScriptedThreadPlan::GetStopDescription: {0}");
189 s->Format("Scripted thread plan implemented by class {0}.",
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.
199 if (m_stop_description.Empty())
200 s->Format("Scripted thread plan implemented by class {0}.",
202 s->PutCString(m_stop_description.GetData());
203}
204
205// The ones below are not currently exported to Python.
207 Log *log = GetLog(LLDBLog::Thread);
208 LLDB_LOG(log, "called on Scripted Thread Plan: {0}", GetScriptClassName());
209 return true;
210}
211
213 bool current_plan) {
214 m_stop_description.Clear();
215 return true;
216}
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:364
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
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:327
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