15ffd83dbSDimitry Andric //===-- ScriptInterpreterLua.cpp ------------------------------------------===//
2480093f4SDimitry Andric //
3480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6480093f4SDimitry Andric //
7480093f4SDimitry Andric //===----------------------------------------------------------------------===//
8480093f4SDimitry Andric
9480093f4SDimitry Andric #include "ScriptInterpreterLua.h"
10480093f4SDimitry Andric #include "Lua.h"
11af732203SDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
12480093f4SDimitry Andric #include "lldb/Core/Debugger.h"
13480093f4SDimitry Andric #include "lldb/Core/PluginManager.h"
14480093f4SDimitry Andric #include "lldb/Core/StreamFile.h"
15480093f4SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
16af732203SDimitry Andric #include "lldb/Target/ExecutionContext.h"
17480093f4SDimitry Andric #include "lldb/Utility/Stream.h"
18480093f4SDimitry Andric #include "lldb/Utility/StringList.h"
19480093f4SDimitry Andric #include "lldb/Utility/Timer.h"
20af732203SDimitry Andric #include "llvm/ADT/StringRef.h"
215ffd83dbSDimitry Andric #include "llvm/Support/FormatAdapters.h"
22af732203SDimitry Andric #include <memory>
23af732203SDimitry Andric #include <vector>
24480093f4SDimitry Andric
25480093f4SDimitry Andric using namespace lldb;
26480093f4SDimitry Andric using namespace lldb_private;
27480093f4SDimitry Andric
285ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE(ScriptInterpreterLua)
295ffd83dbSDimitry Andric
30af732203SDimitry Andric enum ActiveIOHandler {
31af732203SDimitry Andric eIOHandlerNone,
32af732203SDimitry Andric eIOHandlerBreakpoint,
33af732203SDimitry Andric eIOHandlerWatchpoint
34af732203SDimitry Andric };
35af732203SDimitry Andric
36480093f4SDimitry Andric class IOHandlerLuaInterpreter : public IOHandlerDelegate,
37480093f4SDimitry Andric public IOHandlerEditline {
38480093f4SDimitry Andric public:
IOHandlerLuaInterpreter(Debugger & debugger,ScriptInterpreterLua & script_interpreter,ActiveIOHandler active_io_handler=eIOHandlerNone)39480093f4SDimitry Andric IOHandlerLuaInterpreter(Debugger &debugger,
40af732203SDimitry Andric ScriptInterpreterLua &script_interpreter,
41af732203SDimitry Andric ActiveIOHandler active_io_handler = eIOHandlerNone)
42480093f4SDimitry Andric : IOHandlerEditline(debugger, IOHandler::Type::LuaInterpreter, "lua",
43480093f4SDimitry Andric ">>> ", "..> ", true, debugger.GetUseColor(), 0,
44480093f4SDimitry Andric *this, nullptr),
45af732203SDimitry Andric m_script_interpreter(script_interpreter),
46af732203SDimitry Andric m_active_io_handler(active_io_handler) {
475ffd83dbSDimitry Andric llvm::cantFail(m_script_interpreter.GetLua().ChangeIO(
485ffd83dbSDimitry Andric debugger.GetOutputFile().GetStream(),
495ffd83dbSDimitry Andric debugger.GetErrorFile().GetStream()));
50480093f4SDimitry Andric llvm::cantFail(m_script_interpreter.EnterSession(debugger.GetID()));
51480093f4SDimitry Andric }
52480093f4SDimitry Andric
~IOHandlerLuaInterpreter()535ffd83dbSDimitry Andric ~IOHandlerLuaInterpreter() override {
54480093f4SDimitry Andric llvm::cantFail(m_script_interpreter.LeaveSession());
55480093f4SDimitry Andric }
56480093f4SDimitry Andric
IOHandlerActivated(IOHandler & io_handler,bool interactive)57af732203SDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
58af732203SDimitry Andric const char *instructions = nullptr;
59af732203SDimitry Andric switch (m_active_io_handler) {
60af732203SDimitry Andric case eIOHandlerNone:
61*5f7ddb14SDimitry Andric break;
62af732203SDimitry Andric case eIOHandlerWatchpoint:
63*5f7ddb14SDimitry Andric instructions = "Enter your Lua command(s). Type 'quit' to end.\n"
64*5f7ddb14SDimitry Andric "The commands are compiled as the body of the following "
65*5f7ddb14SDimitry Andric "Lua function\n"
66*5f7ddb14SDimitry Andric "function (frame, wp) end\n";
67*5f7ddb14SDimitry Andric SetPrompt(llvm::StringRef("..> "));
68af732203SDimitry Andric break;
69af732203SDimitry Andric case eIOHandlerBreakpoint:
70af732203SDimitry Andric instructions = "Enter your Lua command(s). Type 'quit' to end.\n"
71af732203SDimitry Andric "The commands are compiled as the body of the following "
72af732203SDimitry Andric "Lua function\n"
73af732203SDimitry Andric "function (frame, bp_loc, ...) end\n";
74af732203SDimitry Andric SetPrompt(llvm::StringRef("..> "));
75af732203SDimitry Andric break;
76af732203SDimitry Andric }
77af732203SDimitry Andric if (instructions == nullptr)
78af732203SDimitry Andric return;
79af732203SDimitry Andric if (interactive)
80af732203SDimitry Andric *io_handler.GetOutputStreamFileSP() << instructions;
81af732203SDimitry Andric }
82af732203SDimitry Andric
IOHandlerIsInputComplete(IOHandler & io_handler,StringList & lines)83af732203SDimitry Andric bool IOHandlerIsInputComplete(IOHandler &io_handler,
84af732203SDimitry Andric StringList &lines) override {
85af732203SDimitry Andric size_t last = lines.GetSize() - 1;
86af732203SDimitry Andric if (IsQuitCommand(lines.GetStringAtIndex(last))) {
87*5f7ddb14SDimitry Andric if (m_active_io_handler == eIOHandlerBreakpoint ||
88*5f7ddb14SDimitry Andric m_active_io_handler == eIOHandlerWatchpoint)
89af732203SDimitry Andric lines.DeleteStringAtIndex(last);
90af732203SDimitry Andric return true;
91af732203SDimitry Andric }
92af732203SDimitry Andric StreamString str;
93af732203SDimitry Andric lines.Join("\n", str);
94af732203SDimitry Andric if (llvm::Error E =
95af732203SDimitry Andric m_script_interpreter.GetLua().CheckSyntax(str.GetString())) {
96af732203SDimitry Andric std::string error_str = toString(std::move(E));
97af732203SDimitry Andric // Lua always errors out to incomplete code with '<eof>'
98af732203SDimitry Andric return error_str.find("<eof>") == std::string::npos;
99af732203SDimitry Andric }
100*5f7ddb14SDimitry Andric // The breakpoint and watchpoint handler only exits with a explicit 'quit'
101*5f7ddb14SDimitry Andric return m_active_io_handler != eIOHandlerBreakpoint &&
102*5f7ddb14SDimitry Andric m_active_io_handler != eIOHandlerWatchpoint;
103af732203SDimitry Andric }
104af732203SDimitry Andric
IOHandlerInputComplete(IOHandler & io_handler,std::string & data)105480093f4SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler,
106480093f4SDimitry Andric std::string &data) override {
107af732203SDimitry Andric switch (m_active_io_handler) {
108af732203SDimitry Andric case eIOHandlerBreakpoint: {
109*5f7ddb14SDimitry Andric auto *bp_options_vec =
110*5f7ddb14SDimitry Andric static_cast<std::vector<std::reference_wrapper<BreakpointOptions>> *>(
111af732203SDimitry Andric io_handler.GetUserData());
112*5f7ddb14SDimitry Andric for (BreakpointOptions &bp_options : *bp_options_vec) {
113af732203SDimitry Andric Status error = m_script_interpreter.SetBreakpointCommandCallback(
114af732203SDimitry Andric bp_options, data.c_str());
115af732203SDimitry Andric if (error.Fail())
116af732203SDimitry Andric *io_handler.GetErrorStreamFileSP() << error.AsCString() << '\n';
117af732203SDimitry Andric }
118af732203SDimitry Andric io_handler.SetIsDone(true);
119af732203SDimitry Andric } break;
120*5f7ddb14SDimitry Andric case eIOHandlerWatchpoint: {
121*5f7ddb14SDimitry Andric auto *wp_options =
122*5f7ddb14SDimitry Andric static_cast<WatchpointOptions *>(io_handler.GetUserData());
123*5f7ddb14SDimitry Andric m_script_interpreter.SetWatchpointCommandCallback(wp_options,
124*5f7ddb14SDimitry Andric data.c_str());
125af732203SDimitry Andric io_handler.SetIsDone(true);
126*5f7ddb14SDimitry Andric } break;
127af732203SDimitry Andric case eIOHandlerNone:
128af732203SDimitry Andric if (IsQuitCommand(data)) {
1295ffd83dbSDimitry Andric io_handler.SetIsDone(true);
1305ffd83dbSDimitry Andric return;
1315ffd83dbSDimitry Andric }
132af732203SDimitry Andric if (llvm::Error error = m_script_interpreter.GetLua().Run(data))
133af732203SDimitry Andric *io_handler.GetErrorStreamFileSP() << toString(std::move(error));
134af732203SDimitry Andric break;
135480093f4SDimitry Andric }
136480093f4SDimitry Andric }
137480093f4SDimitry Andric
138480093f4SDimitry Andric private:
139480093f4SDimitry Andric ScriptInterpreterLua &m_script_interpreter;
140af732203SDimitry Andric ActiveIOHandler m_active_io_handler;
141af732203SDimitry Andric
IsQuitCommand(llvm::StringRef cmd)142af732203SDimitry Andric bool IsQuitCommand(llvm::StringRef cmd) { return cmd.rtrim() == "quit"; }
143480093f4SDimitry Andric };
144480093f4SDimitry Andric
ScriptInterpreterLua(Debugger & debugger)145480093f4SDimitry Andric ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger)
146480093f4SDimitry Andric : ScriptInterpreter(debugger, eScriptLanguageLua),
147480093f4SDimitry Andric m_lua(std::make_unique<Lua>()) {}
148480093f4SDimitry Andric
149*5f7ddb14SDimitry Andric ScriptInterpreterLua::~ScriptInterpreterLua() = default;
150480093f4SDimitry Andric
ExecuteOneLine(llvm::StringRef command,CommandReturnObject * result,const ExecuteScriptOptions & options)151480093f4SDimitry Andric bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command,
152480093f4SDimitry Andric CommandReturnObject *result,
153480093f4SDimitry Andric const ExecuteScriptOptions &options) {
1545ffd83dbSDimitry Andric if (command.empty()) {
1555ffd83dbSDimitry Andric if (result)
1565ffd83dbSDimitry Andric result->AppendError("empty command passed to lua\n");
1575ffd83dbSDimitry Andric return false;
1585ffd83dbSDimitry Andric }
1595ffd83dbSDimitry Andric
1605ffd83dbSDimitry Andric llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
1615ffd83dbSDimitry Andric io_redirect_or_error = ScriptInterpreterIORedirect::Create(
1625ffd83dbSDimitry Andric options.GetEnableIO(), m_debugger, result);
1635ffd83dbSDimitry Andric if (!io_redirect_or_error) {
1645ffd83dbSDimitry Andric if (result)
1655ffd83dbSDimitry Andric result->AppendErrorWithFormatv(
1665ffd83dbSDimitry Andric "failed to redirect I/O: {0}\n",
1675ffd83dbSDimitry Andric llvm::fmt_consume(io_redirect_or_error.takeError()));
1685ffd83dbSDimitry Andric else
1695ffd83dbSDimitry Andric llvm::consumeError(io_redirect_or_error.takeError());
1705ffd83dbSDimitry Andric return false;
1715ffd83dbSDimitry Andric }
1725ffd83dbSDimitry Andric
1735ffd83dbSDimitry Andric ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error;
1745ffd83dbSDimitry Andric
1755ffd83dbSDimitry Andric if (llvm::Error e =
1765ffd83dbSDimitry Andric m_lua->ChangeIO(io_redirect.GetOutputFile()->GetStream(),
1775ffd83dbSDimitry Andric io_redirect.GetErrorFile()->GetStream())) {
1785ffd83dbSDimitry Andric result->AppendErrorWithFormatv("lua failed to redirect I/O: {0}\n",
1795ffd83dbSDimitry Andric llvm::toString(std::move(e)));
1805ffd83dbSDimitry Andric return false;
1815ffd83dbSDimitry Andric }
1825ffd83dbSDimitry Andric
183480093f4SDimitry Andric if (llvm::Error e = m_lua->Run(command)) {
184480093f4SDimitry Andric result->AppendErrorWithFormatv(
185480093f4SDimitry Andric "lua failed attempting to evaluate '{0}': {1}\n", command,
186480093f4SDimitry Andric llvm::toString(std::move(e)));
187480093f4SDimitry Andric return false;
188480093f4SDimitry Andric }
1895ffd83dbSDimitry Andric
1905ffd83dbSDimitry Andric io_redirect.Flush();
191480093f4SDimitry Andric return true;
192480093f4SDimitry Andric }
193480093f4SDimitry Andric
ExecuteInterpreterLoop()194480093f4SDimitry Andric void ScriptInterpreterLua::ExecuteInterpreterLoop() {
195af732203SDimitry Andric LLDB_SCOPED_TIMER();
196480093f4SDimitry Andric
197480093f4SDimitry Andric // At the moment, the only time the debugger does not have an input file
198480093f4SDimitry Andric // handle is when this is called directly from lua, in which case it is
199480093f4SDimitry Andric // both dangerous and unnecessary (not to mention confusing) to try to embed
200480093f4SDimitry Andric // a running interpreter loop inside the already running lua interpreter
201480093f4SDimitry Andric // loop, so we won't do it.
2025ffd83dbSDimitry Andric if (!m_debugger.GetInputFile().IsValid())
203480093f4SDimitry Andric return;
204480093f4SDimitry Andric
2055ffd83dbSDimitry Andric IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(m_debugger, *this));
2065ffd83dbSDimitry Andric m_debugger.RunIOHandlerAsync(io_handler_sp);
207480093f4SDimitry Andric }
208480093f4SDimitry Andric
LoadScriptingModule(const char * filename,const LoadScriptOptions & options,lldb_private::Status & error,StructuredData::ObjectSP * module_sp,FileSpec extra_search_dir)209480093f4SDimitry Andric bool ScriptInterpreterLua::LoadScriptingModule(
210*5f7ddb14SDimitry Andric const char *filename, const LoadScriptOptions &options,
211*5f7ddb14SDimitry Andric lldb_private::Status &error, StructuredData::ObjectSP *module_sp,
212*5f7ddb14SDimitry Andric FileSpec extra_search_dir) {
213480093f4SDimitry Andric
2145ffd83dbSDimitry Andric FileSystem::Instance().Collect(filename);
215480093f4SDimitry Andric if (llvm::Error e = m_lua->LoadModule(filename)) {
216480093f4SDimitry Andric error.SetErrorStringWithFormatv("lua failed to import '{0}': {1}\n",
217480093f4SDimitry Andric filename, llvm::toString(std::move(e)));
218480093f4SDimitry Andric return false;
219480093f4SDimitry Andric }
220480093f4SDimitry Andric return true;
221480093f4SDimitry Andric }
222480093f4SDimitry Andric
Initialize()223480093f4SDimitry Andric void ScriptInterpreterLua::Initialize() {
224480093f4SDimitry Andric static llvm::once_flag g_once_flag;
225480093f4SDimitry Andric
226480093f4SDimitry Andric llvm::call_once(g_once_flag, []() {
227480093f4SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(),
228480093f4SDimitry Andric GetPluginDescriptionStatic(),
229480093f4SDimitry Andric lldb::eScriptLanguageLua, CreateInstance);
230480093f4SDimitry Andric });
231480093f4SDimitry Andric }
232480093f4SDimitry Andric
Terminate()233480093f4SDimitry Andric void ScriptInterpreterLua::Terminate() {}
234480093f4SDimitry Andric
EnterSession(user_id_t debugger_id)235480093f4SDimitry Andric llvm::Error ScriptInterpreterLua::EnterSession(user_id_t debugger_id) {
236480093f4SDimitry Andric if (m_session_is_active)
237480093f4SDimitry Andric return llvm::Error::success();
238480093f4SDimitry Andric
239480093f4SDimitry Andric const char *fmt_str =
240480093f4SDimitry Andric "lldb.debugger = lldb.SBDebugger.FindDebuggerWithID({0}); "
241480093f4SDimitry Andric "lldb.target = lldb.debugger:GetSelectedTarget(); "
242480093f4SDimitry Andric "lldb.process = lldb.target:GetProcess(); "
243480093f4SDimitry Andric "lldb.thread = lldb.process:GetSelectedThread(); "
244480093f4SDimitry Andric "lldb.frame = lldb.thread:GetSelectedFrame()";
245480093f4SDimitry Andric return m_lua->Run(llvm::formatv(fmt_str, debugger_id).str());
246480093f4SDimitry Andric }
247480093f4SDimitry Andric
LeaveSession()248480093f4SDimitry Andric llvm::Error ScriptInterpreterLua::LeaveSession() {
249480093f4SDimitry Andric if (!m_session_is_active)
250480093f4SDimitry Andric return llvm::Error::success();
251480093f4SDimitry Andric
252480093f4SDimitry Andric m_session_is_active = false;
253480093f4SDimitry Andric
254480093f4SDimitry Andric llvm::StringRef str = "lldb.debugger = nil; "
255480093f4SDimitry Andric "lldb.target = nil; "
256480093f4SDimitry Andric "lldb.process = nil; "
257480093f4SDimitry Andric "lldb.thread = nil; "
258480093f4SDimitry Andric "lldb.frame = nil";
259480093f4SDimitry Andric return m_lua->Run(str);
260480093f4SDimitry Andric }
261480093f4SDimitry Andric
BreakpointCallbackFunction(void * baton,StoppointCallbackContext * context,user_id_t break_id,user_id_t break_loc_id)262af732203SDimitry Andric bool ScriptInterpreterLua::BreakpointCallbackFunction(
263af732203SDimitry Andric void *baton, StoppointCallbackContext *context, user_id_t break_id,
264af732203SDimitry Andric user_id_t break_loc_id) {
265af732203SDimitry Andric assert(context);
266af732203SDimitry Andric
267af732203SDimitry Andric ExecutionContext exe_ctx(context->exe_ctx_ref);
268af732203SDimitry Andric Target *target = exe_ctx.GetTargetPtr();
269af732203SDimitry Andric if (target == nullptr)
270af732203SDimitry Andric return true;
271af732203SDimitry Andric
272af732203SDimitry Andric StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
273af732203SDimitry Andric BreakpointSP breakpoint_sp = target->GetBreakpointByID(break_id);
274af732203SDimitry Andric BreakpointLocationSP bp_loc_sp(breakpoint_sp->FindLocationByID(break_loc_id));
275af732203SDimitry Andric
276af732203SDimitry Andric Debugger &debugger = target->GetDebugger();
277af732203SDimitry Andric ScriptInterpreterLua *lua_interpreter = static_cast<ScriptInterpreterLua *>(
278af732203SDimitry Andric debugger.GetScriptInterpreter(true, eScriptLanguageLua));
279af732203SDimitry Andric Lua &lua = lua_interpreter->GetLua();
280af732203SDimitry Andric
281af732203SDimitry Andric CommandDataLua *bp_option_data = static_cast<CommandDataLua *>(baton);
282af732203SDimitry Andric llvm::Expected<bool> BoolOrErr = lua.CallBreakpointCallback(
283af732203SDimitry Andric baton, stop_frame_sp, bp_loc_sp, bp_option_data->m_extra_args_sp);
284af732203SDimitry Andric if (llvm::Error E = BoolOrErr.takeError()) {
285af732203SDimitry Andric debugger.GetErrorStream() << toString(std::move(E));
286af732203SDimitry Andric return true;
287af732203SDimitry Andric }
288af732203SDimitry Andric
289af732203SDimitry Andric return *BoolOrErr;
290af732203SDimitry Andric }
291af732203SDimitry Andric
WatchpointCallbackFunction(void * baton,StoppointCallbackContext * context,user_id_t watch_id)292*5f7ddb14SDimitry Andric bool ScriptInterpreterLua::WatchpointCallbackFunction(
293*5f7ddb14SDimitry Andric void *baton, StoppointCallbackContext *context, user_id_t watch_id) {
294*5f7ddb14SDimitry Andric assert(context);
295*5f7ddb14SDimitry Andric
296*5f7ddb14SDimitry Andric ExecutionContext exe_ctx(context->exe_ctx_ref);
297*5f7ddb14SDimitry Andric Target *target = exe_ctx.GetTargetPtr();
298*5f7ddb14SDimitry Andric if (target == nullptr)
299*5f7ddb14SDimitry Andric return true;
300*5f7ddb14SDimitry Andric
301*5f7ddb14SDimitry Andric StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
302*5f7ddb14SDimitry Andric WatchpointSP wp_sp = target->GetWatchpointList().FindByID(watch_id);
303*5f7ddb14SDimitry Andric
304*5f7ddb14SDimitry Andric Debugger &debugger = target->GetDebugger();
305*5f7ddb14SDimitry Andric ScriptInterpreterLua *lua_interpreter = static_cast<ScriptInterpreterLua *>(
306*5f7ddb14SDimitry Andric debugger.GetScriptInterpreter(true, eScriptLanguageLua));
307*5f7ddb14SDimitry Andric Lua &lua = lua_interpreter->GetLua();
308*5f7ddb14SDimitry Andric
309*5f7ddb14SDimitry Andric llvm::Expected<bool> BoolOrErr =
310*5f7ddb14SDimitry Andric lua.CallWatchpointCallback(baton, stop_frame_sp, wp_sp);
311*5f7ddb14SDimitry Andric if (llvm::Error E = BoolOrErr.takeError()) {
312*5f7ddb14SDimitry Andric debugger.GetErrorStream() << toString(std::move(E));
313*5f7ddb14SDimitry Andric return true;
314*5f7ddb14SDimitry Andric }
315*5f7ddb14SDimitry Andric
316*5f7ddb14SDimitry Andric return *BoolOrErr;
317*5f7ddb14SDimitry Andric }
318*5f7ddb14SDimitry Andric
CollectDataForBreakpointCommandCallback(std::vector<std::reference_wrapper<BreakpointOptions>> & bp_options_vec,CommandReturnObject & result)319af732203SDimitry Andric void ScriptInterpreterLua::CollectDataForBreakpointCommandCallback(
320*5f7ddb14SDimitry Andric std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
321af732203SDimitry Andric CommandReturnObject &result) {
322af732203SDimitry Andric IOHandlerSP io_handler_sp(
323af732203SDimitry Andric new IOHandlerLuaInterpreter(m_debugger, *this, eIOHandlerBreakpoint));
324af732203SDimitry Andric io_handler_sp->SetUserData(&bp_options_vec);
325af732203SDimitry Andric m_debugger.RunIOHandlerAsync(io_handler_sp);
326af732203SDimitry Andric }
327af732203SDimitry Andric
CollectDataForWatchpointCommandCallback(WatchpointOptions * wp_options,CommandReturnObject & result)328*5f7ddb14SDimitry Andric void ScriptInterpreterLua::CollectDataForWatchpointCommandCallback(
329*5f7ddb14SDimitry Andric WatchpointOptions *wp_options, CommandReturnObject &result) {
330*5f7ddb14SDimitry Andric IOHandlerSP io_handler_sp(
331*5f7ddb14SDimitry Andric new IOHandlerLuaInterpreter(m_debugger, *this, eIOHandlerWatchpoint));
332*5f7ddb14SDimitry Andric io_handler_sp->SetUserData(wp_options);
333*5f7ddb14SDimitry Andric m_debugger.RunIOHandlerAsync(io_handler_sp);
334*5f7ddb14SDimitry Andric }
335*5f7ddb14SDimitry Andric
SetBreakpointCommandCallbackFunction(BreakpointOptions & bp_options,const char * function_name,StructuredData::ObjectSP extra_args_sp)336af732203SDimitry Andric Status ScriptInterpreterLua::SetBreakpointCommandCallbackFunction(
337*5f7ddb14SDimitry Andric BreakpointOptions &bp_options, const char *function_name,
338af732203SDimitry Andric StructuredData::ObjectSP extra_args_sp) {
339af732203SDimitry Andric const char *fmt_str = "return {0}(frame, bp_loc, ...)";
340af732203SDimitry Andric std::string oneliner = llvm::formatv(fmt_str, function_name).str();
341af732203SDimitry Andric return RegisterBreakpointCallback(bp_options, oneliner.c_str(),
342af732203SDimitry Andric extra_args_sp);
343af732203SDimitry Andric }
344af732203SDimitry Andric
SetBreakpointCommandCallback(BreakpointOptions & bp_options,const char * command_body_text)345af732203SDimitry Andric Status ScriptInterpreterLua::SetBreakpointCommandCallback(
346*5f7ddb14SDimitry Andric BreakpointOptions &bp_options, const char *command_body_text) {
347af732203SDimitry Andric return RegisterBreakpointCallback(bp_options, command_body_text, {});
348af732203SDimitry Andric }
349af732203SDimitry Andric
RegisterBreakpointCallback(BreakpointOptions & bp_options,const char * command_body_text,StructuredData::ObjectSP extra_args_sp)350af732203SDimitry Andric Status ScriptInterpreterLua::RegisterBreakpointCallback(
351*5f7ddb14SDimitry Andric BreakpointOptions &bp_options, const char *command_body_text,
352af732203SDimitry Andric StructuredData::ObjectSP extra_args_sp) {
353af732203SDimitry Andric Status error;
354af732203SDimitry Andric auto data_up = std::make_unique<CommandDataLua>(extra_args_sp);
355af732203SDimitry Andric error = m_lua->RegisterBreakpointCallback(data_up.get(), command_body_text);
356af732203SDimitry Andric if (error.Fail())
357af732203SDimitry Andric return error;
358af732203SDimitry Andric auto baton_sp =
359af732203SDimitry Andric std::make_shared<BreakpointOptions::CommandBaton>(std::move(data_up));
360*5f7ddb14SDimitry Andric bp_options.SetCallback(ScriptInterpreterLua::BreakpointCallbackFunction,
361*5f7ddb14SDimitry Andric baton_sp);
362*5f7ddb14SDimitry Andric return error;
363*5f7ddb14SDimitry Andric }
364*5f7ddb14SDimitry Andric
SetWatchpointCommandCallback(WatchpointOptions * wp_options,const char * command_body_text)365*5f7ddb14SDimitry Andric void ScriptInterpreterLua::SetWatchpointCommandCallback(
366*5f7ddb14SDimitry Andric WatchpointOptions *wp_options, const char *command_body_text) {
367*5f7ddb14SDimitry Andric RegisterWatchpointCallback(wp_options, command_body_text, {});
368*5f7ddb14SDimitry Andric }
369*5f7ddb14SDimitry Andric
RegisterWatchpointCallback(WatchpointOptions * wp_options,const char * command_body_text,StructuredData::ObjectSP extra_args_sp)370*5f7ddb14SDimitry Andric Status ScriptInterpreterLua::RegisterWatchpointCallback(
371*5f7ddb14SDimitry Andric WatchpointOptions *wp_options, const char *command_body_text,
372*5f7ddb14SDimitry Andric StructuredData::ObjectSP extra_args_sp) {
373*5f7ddb14SDimitry Andric Status error;
374*5f7ddb14SDimitry Andric auto data_up = std::make_unique<WatchpointOptions::CommandData>();
375*5f7ddb14SDimitry Andric error = m_lua->RegisterWatchpointCallback(data_up.get(), command_body_text);
376*5f7ddb14SDimitry Andric if (error.Fail())
377*5f7ddb14SDimitry Andric return error;
378*5f7ddb14SDimitry Andric auto baton_sp =
379*5f7ddb14SDimitry Andric std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
380*5f7ddb14SDimitry Andric wp_options->SetCallback(ScriptInterpreterLua::WatchpointCallbackFunction,
381af732203SDimitry Andric baton_sp);
382af732203SDimitry Andric return error;
383af732203SDimitry Andric }
384af732203SDimitry Andric
385480093f4SDimitry Andric lldb::ScriptInterpreterSP
CreateInstance(Debugger & debugger)386480093f4SDimitry Andric ScriptInterpreterLua::CreateInstance(Debugger &debugger) {
387480093f4SDimitry Andric return std::make_shared<ScriptInterpreterLua>(debugger);
388480093f4SDimitry Andric }
389480093f4SDimitry Andric
GetPluginNameStatic()390480093f4SDimitry Andric lldb_private::ConstString ScriptInterpreterLua::GetPluginNameStatic() {
391480093f4SDimitry Andric static ConstString g_name("script-lua");
392480093f4SDimitry Andric return g_name;
393480093f4SDimitry Andric }
394480093f4SDimitry Andric
GetPluginDescriptionStatic()395480093f4SDimitry Andric const char *ScriptInterpreterLua::GetPluginDescriptionStatic() {
396480093f4SDimitry Andric return "Lua script interpreter";
397480093f4SDimitry Andric }
398480093f4SDimitry Andric
GetPluginName()399480093f4SDimitry Andric lldb_private::ConstString ScriptInterpreterLua::GetPluginName() {
400480093f4SDimitry Andric return GetPluginNameStatic();
401480093f4SDimitry Andric }
402480093f4SDimitry Andric
GetPluginVersion()403480093f4SDimitry Andric uint32_t ScriptInterpreterLua::GetPluginVersion() { return 1; }
404480093f4SDimitry Andric
GetLua()405480093f4SDimitry Andric Lua &ScriptInterpreterLua::GetLua() { return *m_lua; }
406