20#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/FormatAdapters.h"
43 llvm::StringRef(
">>> "),
llvm::StringRef(
"..> "),
44 true, debugger.GetUseColor(), 0, *this),
58 const char *instructions =
nullptr;
63 instructions =
"Enter your Lua command(s). Type 'quit' to end.\n"
64 "The commands are compiled as the body of the following "
66 "function (frame, wp) end\n";
70 instructions =
"Enter your Lua command(s). Type 'quit' to end.\n"
71 "The commands are compiled as the body of the following "
73 "function (frame, bp_loc, ...) end\n";
77 if (instructions ==
nullptr)
85 size_t last = lines.
GetSize() - 1;
93 lines.
Join(
"\n", str);
96 std::string error_str =
toString(std::move(E));
98 return error_str.find(
"<eof>") == std::string::npos;
106 std::string &data)
override {
109 auto *bp_options_vec =
110 static_cast<std::vector<std::reference_wrapper<BreakpointOptions>
> *>(
114 bp_options, data.c_str(),
false);
148 m_lua(std::make_unique<
Lua>()) {}
153 auto info = std::make_shared<StructuredData::Dictionary>();
154 info->AddStringItem(
"language",
"lua");
161 if (command.empty()) {
163 result->
AppendError(
"empty command passed to lua\n");
167 llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
170 if (!io_redirect_or_error) {
173 "failed to redirect I/O: {0}\n",
174 llvm::fmt_consume(io_redirect_or_error.takeError()));
176 llvm::consumeError(io_redirect_or_error.takeError());
186 llvm::toString(std::move(e)));
190 if (llvm::Error e =
m_lua->Run(command)) {
192 "lua failed attempting to evaluate '{0}': {1}\n", command,
193 llvm::toString(std::move(e)));
221 if (llvm::Error e =
m_lua->LoadModule(filename)) {
222 error.SetErrorStringWithFormatv(
"lua failed to import '{0}': {1}\n",
223 filename, llvm::toString(std::move(e)));
230 static llvm::once_flag g_once_flag;
232 llvm::call_once(g_once_flag, []() {
243 return llvm::Error::success();
245 const char *fmt_str =
246 "lldb.debugger = lldb.SBDebugger.FindDebuggerWithID({0}); "
247 "lldb.target = lldb.debugger:GetSelectedTarget(); "
248 "lldb.process = lldb.target:GetProcess(); "
249 "lldb.thread = lldb.process:GetSelectedThread(); "
250 "lldb.frame = lldb.thread:GetSelectedFrame()";
251 return m_lua->Run(llvm::formatv(fmt_str, debugger_id).str());
256 return llvm::Error::success();
260 llvm::StringRef str =
"lldb.debugger = nil; "
261 "lldb.target = nil; "
262 "lldb.process = nil; "
263 "lldb.thread = nil; "
265 return m_lua->Run(str);
275 if (target ==
nullptr)
290 if (llvm::Error E = BoolOrErr.takeError()) {
304 if (target ==
nullptr)
315 llvm::Expected<bool> BoolOrErr =
317 if (llvm::Error E = BoolOrErr.takeError()) {
326 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
330 io_handler_sp->SetUserData(&bp_options_vec);
338 io_handler_sp->SetUserData(wp_options);
345 const char *fmt_str =
"return {0}(frame, bp_loc, ...)";
346 std::string oneliner = llvm::formatv(fmt_str, function_name).str();
361 auto data_up = std::make_unique<CommandDataLua>(extra_args_sp);
362 error =
m_lua->RegisterBreakpointCallback(data_up.get(), command_body_text);
366 std::make_shared<BreakpointOptions::CommandBaton>(std::move(data_up));
382 auto data_up = std::make_unique<WatchpointOptions::CommandData>();
383 error =
m_lua->RegisterWatchpointCallback(data_up.get(), command_body_text);
387 std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
395 return std::make_shared<ScriptInterpreterLua>(debugger);
399 return "Lua script interpreter";
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_PLUGIN_DEFINE(PluginName)
#define LLDB_SCOPED_TIMER()
IOHandlerLuaInterpreter(Debugger &debugger, ScriptInterpreterLua &script_interpreter, ActiveIOHandler active_io_handler=eIOHandlerNone)
~IOHandlerLuaInterpreter() override
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override
ActiveIOHandler m_active_io_handler
ScriptInterpreterLua & m_script_interpreter
bool IOHandlerIsInputComplete(IOHandler &io_handler, StringList &lines) override
Called to determine whether typing enter after the last line in lines should end input.
bool IsQuitCommand(llvm::StringRef cmd)
void IOHandlerInputComplete(IOHandler &io_handler, std::string &data) override
Called when a line or lines have been retrieved.
"lldb/Breakpoint/BreakpointOptions.h" Class that manages the options on a breakpoint or breakpoint lo...
void SetCallback(BreakpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous=false)
Adds a callback to the breakpoint option set.
void AppendErrorWithFormatv(const char *format, Args &&... args)
void void AppendError(llvm::StringRef in_string)
A class to manage flag bits.
StreamFile & GetErrorStream()
void RunIOHandlerAsync(const lldb::IOHandlerSP &reader_sp, bool cancel_top_handler=true)
Run the given IO handler and return immediately.
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, std::optional< lldb::ScriptLanguage > language={})
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const lldb::StackFrameSP & GetFrameSP() const
Get accessor to get the frame shared pointer.
Target * GetTargetPtr() const
Returns a pointer to the target object.
virtual FILE * GetStream()
Get the underlying libc stream for this file, or NULL.
bool IsValid() const override
IsValid.
A delegate class for use with IOHandler subclasses.
bool SetPrompt(llvm::StringRef prompt) override
lldb::StreamFileSP GetErrorStreamFileSP()
lldb::StreamFileSP GetOutputStreamFileSP()
llvm::Expected< bool > CallWatchpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp)
llvm::Error CheckSyntax(llvm::StringRef buffer)
llvm::Error ChangeIO(FILE *out, FILE *err)
llvm::Error Run(llvm::StringRef buffer)
llvm::Expected< bool > CallBreakpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp, lldb::BreakpointLocationSP bp_loc_sp, StructuredData::ObjectSP extra_args_sp)
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
lldb::FileSP GetOutputFile() const
lldb::FileSP GetErrorFile() const
void Flush()
Flush our output and error file handles.
static llvm::Expected< std::unique_ptr< ScriptInterpreterIORedirect > > Create(bool enable_io, Debugger &debugger, CommandReturnObject *result)
Create an IO redirect.
StructuredData::ObjectSP m_extra_args_sp
Status SetBreakpointCommandCallbackFunction(BreakpointOptions &bp_options, const char *function_name, StructuredData::ObjectSP extra_args_sp) override
Set a script function as the callback for the breakpoint.
llvm::Error EnterSession(lldb::user_id_t debugger_id)
static bool WatchpointCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id)
std::unique_ptr< Lua > m_lua
static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger)
Status SetBreakpointCommandCallback(BreakpointOptions &bp_options, const char *command_body_text, bool is_callback) override
void SetWatchpointCommandCallback(WatchpointOptions *wp_options, const char *command_body_text, bool is_callback) override
Set a one-liner as the callback for the watchpoint.
static bool BreakpointCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
bool LoadScriptingModule(const char *filename, const LoadScriptOptions &options, lldb_private::Status &error, StructuredData::ObjectSP *module_sp=nullptr, FileSpec extra_search_dir={}) override
void ExecuteInterpreterLoop() override
void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result) override
llvm::Error LeaveSession()
ScriptInterpreterLua(Debugger &debugger)
~ScriptInterpreterLua() override
bool ExecuteOneLine(llvm::StringRef command, CommandReturnObject *result, const ExecuteScriptOptions &options=ExecuteScriptOptions()) override
Status RegisterBreakpointCallback(BreakpointOptions &bp_options, const char *command_body_text, StructuredData::ObjectSP extra_args_sp)
static llvm::StringRef GetPluginNameStatic()
void CollectDataForBreakpointCommandCallback(std::vector< std::reference_wrapper< BreakpointOptions > > &bp_options_vec, CommandReturnObject &result) override
StructuredData::DictionarySP GetInterpreterInfo() override
static llvm::StringRef GetPluginDescriptionStatic()
Status RegisterWatchpointCallback(WatchpointOptions *wp_options, const char *command_body_text, StructuredData::ObjectSP extra_args_sp)
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
ExecutionContextRef exe_ctx_ref
llvm::StringRef GetString() const
const char * GetStringAtIndex(size_t idx) const
void DeleteStringAtIndex(size_t id)
void Join(const char *separator, Stream &strm)
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
lldb::BreakpointSP GetBreakpointByID(lldb::break_id_t break_id)
WatchpointList & GetWatchpointList()
lldb::WatchpointSP FindByID(lldb::watch_id_t watchID) const
Returns a shared pointer to the watchpoint with id watchID, const version.
"lldb/Breakpoint/WatchpointOptions.h" Class that manages the options on a watchpoint.
void SetCallback(WatchpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous=false)
Adds a callback to the watchpoint option set.
A class that represents a running process on the host machine.
const char * toString(AppleArm64ExceptionClass EC)
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
std::shared_ptr< lldb_private::IOHandler > IOHandlerSP
std::shared_ptr< lldb_private::ScriptInterpreter > ScriptInterpreterSP
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::Watchpoint > WatchpointSP
lldb::user_id_t GetID() const
Get accessor for the user ID.