1cfca06d7SDimitry Andric //===-- ScriptInterpreterLua.cpp ------------------------------------------===//
2706b4fc4SDimitry Andric //
3706b4fc4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4706b4fc4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5706b4fc4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6706b4fc4SDimitry Andric //
7706b4fc4SDimitry Andric //===----------------------------------------------------------------------===//
8706b4fc4SDimitry Andric
9706b4fc4SDimitry Andric #include "ScriptInterpreterLua.h"
10706b4fc4SDimitry Andric #include "Lua.h"
11b60736ecSDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
12706b4fc4SDimitry Andric #include "lldb/Core/Debugger.h"
13706b4fc4SDimitry Andric #include "lldb/Core/PluginManager.h"
14b1c73532SDimitry Andric #include "lldb/Host/StreamFile.h"
15706b4fc4SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
16b60736ecSDimitry Andric #include "lldb/Target/ExecutionContext.h"
17706b4fc4SDimitry Andric #include "lldb/Utility/Stream.h"
18706b4fc4SDimitry Andric #include "lldb/Utility/StringList.h"
19706b4fc4SDimitry Andric #include "lldb/Utility/Timer.h"
20b60736ecSDimitry Andric #include "llvm/ADT/StringRef.h"
21cfca06d7SDimitry Andric #include "llvm/Support/FormatAdapters.h"
22b60736ecSDimitry Andric #include <memory>
23b60736ecSDimitry Andric #include <vector>
24706b4fc4SDimitry Andric
25706b4fc4SDimitry Andric using namespace lldb;
26706b4fc4SDimitry Andric using namespace lldb_private;
27706b4fc4SDimitry Andric
28cfca06d7SDimitry Andric LLDB_PLUGIN_DEFINE(ScriptInterpreterLua)
29cfca06d7SDimitry Andric
30b60736ecSDimitry Andric enum ActiveIOHandler {
31b60736ecSDimitry Andric eIOHandlerNone,
32b60736ecSDimitry Andric eIOHandlerBreakpoint,
33b60736ecSDimitry Andric eIOHandlerWatchpoint
34b60736ecSDimitry Andric };
35b60736ecSDimitry Andric
36706b4fc4SDimitry Andric class IOHandlerLuaInterpreter : public IOHandlerDelegate,
37706b4fc4SDimitry Andric public IOHandlerEditline {
38706b4fc4SDimitry Andric public:
IOHandlerLuaInterpreter(Debugger & debugger,ScriptInterpreterLua & script_interpreter,ActiveIOHandler active_io_handler=eIOHandlerNone)39706b4fc4SDimitry Andric IOHandlerLuaInterpreter(Debugger &debugger,
40b60736ecSDimitry Andric ScriptInterpreterLua &script_interpreter,
41b60736ecSDimitry Andric ActiveIOHandler active_io_handler = eIOHandlerNone)
42706b4fc4SDimitry Andric : IOHandlerEditline(debugger, IOHandler::Type::LuaInterpreter, "lua",
43e3b55780SDimitry Andric llvm::StringRef(">>> "), llvm::StringRef("..> "),
44e3b55780SDimitry Andric true, debugger.GetUseColor(), 0, *this),
45b60736ecSDimitry Andric m_script_interpreter(script_interpreter),
46b60736ecSDimitry Andric m_active_io_handler(active_io_handler) {
47cfca06d7SDimitry Andric llvm::cantFail(m_script_interpreter.GetLua().ChangeIO(
48cfca06d7SDimitry Andric debugger.GetOutputFile().GetStream(),
49cfca06d7SDimitry Andric debugger.GetErrorFile().GetStream()));
50706b4fc4SDimitry Andric llvm::cantFail(m_script_interpreter.EnterSession(debugger.GetID()));
51706b4fc4SDimitry Andric }
52706b4fc4SDimitry Andric
~IOHandlerLuaInterpreter()53cfca06d7SDimitry Andric ~IOHandlerLuaInterpreter() override {
54706b4fc4SDimitry Andric llvm::cantFail(m_script_interpreter.LeaveSession());
55706b4fc4SDimitry Andric }
56706b4fc4SDimitry Andric
IOHandlerActivated(IOHandler & io_handler,bool interactive)57b60736ecSDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
58b60736ecSDimitry Andric const char *instructions = nullptr;
59b60736ecSDimitry Andric switch (m_active_io_handler) {
60b60736ecSDimitry Andric case eIOHandlerNone:
61344a3780SDimitry Andric break;
62b60736ecSDimitry Andric case eIOHandlerWatchpoint:
63344a3780SDimitry Andric instructions = "Enter your Lua command(s). Type 'quit' to end.\n"
64344a3780SDimitry Andric "The commands are compiled as the body of the following "
65344a3780SDimitry Andric "Lua function\n"
66344a3780SDimitry Andric "function (frame, wp) end\n";
67344a3780SDimitry Andric SetPrompt(llvm::StringRef("..> "));
68b60736ecSDimitry Andric break;
69b60736ecSDimitry Andric case eIOHandlerBreakpoint:
70b60736ecSDimitry Andric instructions = "Enter your Lua command(s). Type 'quit' to end.\n"
71b60736ecSDimitry Andric "The commands are compiled as the body of the following "
72b60736ecSDimitry Andric "Lua function\n"
73b60736ecSDimitry Andric "function (frame, bp_loc, ...) end\n";
74b60736ecSDimitry Andric SetPrompt(llvm::StringRef("..> "));
75b60736ecSDimitry Andric break;
76b60736ecSDimitry Andric }
77b60736ecSDimitry Andric if (instructions == nullptr)
78b60736ecSDimitry Andric return;
79b60736ecSDimitry Andric if (interactive)
80b60736ecSDimitry Andric *io_handler.GetOutputStreamFileSP() << instructions;
81b60736ecSDimitry Andric }
82b60736ecSDimitry Andric
IOHandlerIsInputComplete(IOHandler & io_handler,StringList & lines)83b60736ecSDimitry Andric bool IOHandlerIsInputComplete(IOHandler &io_handler,
84b60736ecSDimitry Andric StringList &lines) override {
85b60736ecSDimitry Andric size_t last = lines.GetSize() - 1;
86b60736ecSDimitry Andric if (IsQuitCommand(lines.GetStringAtIndex(last))) {
87344a3780SDimitry Andric if (m_active_io_handler == eIOHandlerBreakpoint ||
88344a3780SDimitry Andric m_active_io_handler == eIOHandlerWatchpoint)
89b60736ecSDimitry Andric lines.DeleteStringAtIndex(last);
90b60736ecSDimitry Andric return true;
91b60736ecSDimitry Andric }
92b60736ecSDimitry Andric StreamString str;
93b60736ecSDimitry Andric lines.Join("\n", str);
94b60736ecSDimitry Andric if (llvm::Error E =
95b60736ecSDimitry Andric m_script_interpreter.GetLua().CheckSyntax(str.GetString())) {
96b60736ecSDimitry Andric std::string error_str = toString(std::move(E));
97b60736ecSDimitry Andric // Lua always errors out to incomplete code with '<eof>'
98b60736ecSDimitry Andric return error_str.find("<eof>") == std::string::npos;
99b60736ecSDimitry Andric }
100344a3780SDimitry Andric // The breakpoint and watchpoint handler only exits with a explicit 'quit'
101344a3780SDimitry Andric return m_active_io_handler != eIOHandlerBreakpoint &&
102344a3780SDimitry Andric m_active_io_handler != eIOHandlerWatchpoint;
103b60736ecSDimitry Andric }
104b60736ecSDimitry Andric
IOHandlerInputComplete(IOHandler & io_handler,std::string & data)105706b4fc4SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler,
106706b4fc4SDimitry Andric std::string &data) override {
107b60736ecSDimitry Andric switch (m_active_io_handler) {
108b60736ecSDimitry Andric case eIOHandlerBreakpoint: {
109344a3780SDimitry Andric auto *bp_options_vec =
110344a3780SDimitry Andric static_cast<std::vector<std::reference_wrapper<BreakpointOptions>> *>(
111b60736ecSDimitry Andric io_handler.GetUserData());
112344a3780SDimitry Andric for (BreakpointOptions &bp_options : *bp_options_vec) {
113b60736ecSDimitry Andric Status error = m_script_interpreter.SetBreakpointCommandCallback(
1147fa27ce4SDimitry Andric bp_options, data.c_str(), /*is_callback=*/false);
115b60736ecSDimitry Andric if (error.Fail())
116b60736ecSDimitry Andric *io_handler.GetErrorStreamFileSP() << error.AsCString() << '\n';
117b60736ecSDimitry Andric }
118b60736ecSDimitry Andric io_handler.SetIsDone(true);
119b60736ecSDimitry Andric } break;
120344a3780SDimitry Andric case eIOHandlerWatchpoint: {
121344a3780SDimitry Andric auto *wp_options =
122344a3780SDimitry Andric static_cast<WatchpointOptions *>(io_handler.GetUserData());
123344a3780SDimitry Andric m_script_interpreter.SetWatchpointCommandCallback(wp_options,
1247fa27ce4SDimitry Andric data.c_str(),
1257fa27ce4SDimitry Andric /*is_callback=*/false);
126b60736ecSDimitry Andric io_handler.SetIsDone(true);
127344a3780SDimitry Andric } break;
128b60736ecSDimitry Andric case eIOHandlerNone:
129b60736ecSDimitry Andric if (IsQuitCommand(data)) {
130cfca06d7SDimitry Andric io_handler.SetIsDone(true);
131cfca06d7SDimitry Andric return;
132cfca06d7SDimitry Andric }
133b60736ecSDimitry Andric if (llvm::Error error = m_script_interpreter.GetLua().Run(data))
134b60736ecSDimitry Andric *io_handler.GetErrorStreamFileSP() << toString(std::move(error));
135b60736ecSDimitry Andric break;
136706b4fc4SDimitry Andric }
137706b4fc4SDimitry Andric }
138706b4fc4SDimitry Andric
139706b4fc4SDimitry Andric private:
140706b4fc4SDimitry Andric ScriptInterpreterLua &m_script_interpreter;
141b60736ecSDimitry Andric ActiveIOHandler m_active_io_handler;
142b60736ecSDimitry Andric
IsQuitCommand(llvm::StringRef cmd)143b60736ecSDimitry Andric bool IsQuitCommand(llvm::StringRef cmd) { return cmd.rtrim() == "quit"; }
144706b4fc4SDimitry Andric };
145706b4fc4SDimitry Andric
ScriptInterpreterLua(Debugger & debugger)146706b4fc4SDimitry Andric ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger)
147706b4fc4SDimitry Andric : ScriptInterpreter(debugger, eScriptLanguageLua),
148706b4fc4SDimitry Andric m_lua(std::make_unique<Lua>()) {}
149706b4fc4SDimitry Andric
150344a3780SDimitry Andric ScriptInterpreterLua::~ScriptInterpreterLua() = default;
151706b4fc4SDimitry Andric
GetInterpreterInfo()152c0981da4SDimitry Andric StructuredData::DictionarySP ScriptInterpreterLua::GetInterpreterInfo() {
153c0981da4SDimitry Andric auto info = std::make_shared<StructuredData::Dictionary>();
154c0981da4SDimitry Andric info->AddStringItem("language", "lua");
155c0981da4SDimitry Andric return info;
156c0981da4SDimitry Andric }
157c0981da4SDimitry Andric
ExecuteOneLine(llvm::StringRef command,CommandReturnObject * result,const ExecuteScriptOptions & options)158706b4fc4SDimitry Andric bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command,
159706b4fc4SDimitry Andric CommandReturnObject *result,
160706b4fc4SDimitry Andric const ExecuteScriptOptions &options) {
161cfca06d7SDimitry Andric if (command.empty()) {
162cfca06d7SDimitry Andric if (result)
163cfca06d7SDimitry Andric result->AppendError("empty command passed to lua\n");
164cfca06d7SDimitry Andric return false;
165cfca06d7SDimitry Andric }
166cfca06d7SDimitry Andric
167cfca06d7SDimitry Andric llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
168cfca06d7SDimitry Andric io_redirect_or_error = ScriptInterpreterIORedirect::Create(
169cfca06d7SDimitry Andric options.GetEnableIO(), m_debugger, result);
170cfca06d7SDimitry Andric if (!io_redirect_or_error) {
171cfca06d7SDimitry Andric if (result)
172cfca06d7SDimitry Andric result->AppendErrorWithFormatv(
173cfca06d7SDimitry Andric "failed to redirect I/O: {0}\n",
174cfca06d7SDimitry Andric llvm::fmt_consume(io_redirect_or_error.takeError()));
175cfca06d7SDimitry Andric else
176cfca06d7SDimitry Andric llvm::consumeError(io_redirect_or_error.takeError());
177cfca06d7SDimitry Andric return false;
178cfca06d7SDimitry Andric }
179cfca06d7SDimitry Andric
180cfca06d7SDimitry Andric ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error;
181cfca06d7SDimitry Andric
182cfca06d7SDimitry Andric if (llvm::Error e =
183cfca06d7SDimitry Andric m_lua->ChangeIO(io_redirect.GetOutputFile()->GetStream(),
184cfca06d7SDimitry Andric io_redirect.GetErrorFile()->GetStream())) {
185cfca06d7SDimitry Andric result->AppendErrorWithFormatv("lua failed to redirect I/O: {0}\n",
186cfca06d7SDimitry Andric llvm::toString(std::move(e)));
187cfca06d7SDimitry Andric return false;
188cfca06d7SDimitry Andric }
189cfca06d7SDimitry Andric
190706b4fc4SDimitry Andric if (llvm::Error e = m_lua->Run(command)) {
191706b4fc4SDimitry Andric result->AppendErrorWithFormatv(
192706b4fc4SDimitry Andric "lua failed attempting to evaluate '{0}': {1}\n", command,
193706b4fc4SDimitry Andric llvm::toString(std::move(e)));
194706b4fc4SDimitry Andric return false;
195706b4fc4SDimitry Andric }
196cfca06d7SDimitry Andric
197cfca06d7SDimitry Andric io_redirect.Flush();
198706b4fc4SDimitry Andric return true;
199706b4fc4SDimitry Andric }
200706b4fc4SDimitry Andric
ExecuteInterpreterLoop()201706b4fc4SDimitry Andric void ScriptInterpreterLua::ExecuteInterpreterLoop() {
202b60736ecSDimitry Andric LLDB_SCOPED_TIMER();
203706b4fc4SDimitry Andric
204706b4fc4SDimitry Andric // At the moment, the only time the debugger does not have an input file
205706b4fc4SDimitry Andric // handle is when this is called directly from lua, in which case it is
206706b4fc4SDimitry Andric // both dangerous and unnecessary (not to mention confusing) to try to embed
207706b4fc4SDimitry Andric // a running interpreter loop inside the already running lua interpreter
208706b4fc4SDimitry Andric // loop, so we won't do it.
209cfca06d7SDimitry Andric if (!m_debugger.GetInputFile().IsValid())
210706b4fc4SDimitry Andric return;
211706b4fc4SDimitry Andric
212cfca06d7SDimitry Andric IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(m_debugger, *this));
213cfca06d7SDimitry Andric m_debugger.RunIOHandlerAsync(io_handler_sp);
214706b4fc4SDimitry Andric }
215706b4fc4SDimitry Andric
LoadScriptingModule(const char * filename,const LoadScriptOptions & options,lldb_private::Status & error,StructuredData::ObjectSP * module_sp,FileSpec extra_search_dir)216706b4fc4SDimitry Andric bool ScriptInterpreterLua::LoadScriptingModule(
217344a3780SDimitry Andric const char *filename, const LoadScriptOptions &options,
218344a3780SDimitry Andric lldb_private::Status &error, StructuredData::ObjectSP *module_sp,
219344a3780SDimitry Andric FileSpec extra_search_dir) {
220706b4fc4SDimitry Andric
221706b4fc4SDimitry Andric if (llvm::Error e = m_lua->LoadModule(filename)) {
222706b4fc4SDimitry Andric error.SetErrorStringWithFormatv("lua failed to import '{0}': {1}\n",
223706b4fc4SDimitry Andric filename, llvm::toString(std::move(e)));
224706b4fc4SDimitry Andric return false;
225706b4fc4SDimitry Andric }
226706b4fc4SDimitry Andric return true;
227706b4fc4SDimitry Andric }
228706b4fc4SDimitry Andric
Initialize()229706b4fc4SDimitry Andric void ScriptInterpreterLua::Initialize() {
230706b4fc4SDimitry Andric static llvm::once_flag g_once_flag;
231706b4fc4SDimitry Andric
232706b4fc4SDimitry Andric llvm::call_once(g_once_flag, []() {
233706b4fc4SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(),
234706b4fc4SDimitry Andric GetPluginDescriptionStatic(),
235706b4fc4SDimitry Andric lldb::eScriptLanguageLua, CreateInstance);
236706b4fc4SDimitry Andric });
237706b4fc4SDimitry Andric }
238706b4fc4SDimitry Andric
Terminate()239706b4fc4SDimitry Andric void ScriptInterpreterLua::Terminate() {}
240706b4fc4SDimitry Andric
EnterSession(user_id_t debugger_id)241706b4fc4SDimitry Andric llvm::Error ScriptInterpreterLua::EnterSession(user_id_t debugger_id) {
242706b4fc4SDimitry Andric if (m_session_is_active)
243706b4fc4SDimitry Andric return llvm::Error::success();
244706b4fc4SDimitry Andric
245706b4fc4SDimitry Andric const char *fmt_str =
246706b4fc4SDimitry Andric "lldb.debugger = lldb.SBDebugger.FindDebuggerWithID({0}); "
247706b4fc4SDimitry Andric "lldb.target = lldb.debugger:GetSelectedTarget(); "
248706b4fc4SDimitry Andric "lldb.process = lldb.target:GetProcess(); "
249706b4fc4SDimitry Andric "lldb.thread = lldb.process:GetSelectedThread(); "
250706b4fc4SDimitry Andric "lldb.frame = lldb.thread:GetSelectedFrame()";
251706b4fc4SDimitry Andric return m_lua->Run(llvm::formatv(fmt_str, debugger_id).str());
252706b4fc4SDimitry Andric }
253706b4fc4SDimitry Andric
LeaveSession()254706b4fc4SDimitry Andric llvm::Error ScriptInterpreterLua::LeaveSession() {
255706b4fc4SDimitry Andric if (!m_session_is_active)
256706b4fc4SDimitry Andric return llvm::Error::success();
257706b4fc4SDimitry Andric
258706b4fc4SDimitry Andric m_session_is_active = false;
259706b4fc4SDimitry Andric
260706b4fc4SDimitry Andric llvm::StringRef str = "lldb.debugger = nil; "
261706b4fc4SDimitry Andric "lldb.target = nil; "
262706b4fc4SDimitry Andric "lldb.process = nil; "
263706b4fc4SDimitry Andric "lldb.thread = nil; "
264706b4fc4SDimitry Andric "lldb.frame = nil";
265706b4fc4SDimitry Andric return m_lua->Run(str);
266706b4fc4SDimitry Andric }
267706b4fc4SDimitry Andric
BreakpointCallbackFunction(void * baton,StoppointCallbackContext * context,user_id_t break_id,user_id_t break_loc_id)268b60736ecSDimitry Andric bool ScriptInterpreterLua::BreakpointCallbackFunction(
269b60736ecSDimitry Andric void *baton, StoppointCallbackContext *context, user_id_t break_id,
270b60736ecSDimitry Andric user_id_t break_loc_id) {
271b60736ecSDimitry Andric assert(context);
272b60736ecSDimitry Andric
273b60736ecSDimitry Andric ExecutionContext exe_ctx(context->exe_ctx_ref);
274b60736ecSDimitry Andric Target *target = exe_ctx.GetTargetPtr();
275b60736ecSDimitry Andric if (target == nullptr)
276b60736ecSDimitry Andric return true;
277b60736ecSDimitry Andric
278b60736ecSDimitry Andric StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
279b60736ecSDimitry Andric BreakpointSP breakpoint_sp = target->GetBreakpointByID(break_id);
280b60736ecSDimitry Andric BreakpointLocationSP bp_loc_sp(breakpoint_sp->FindLocationByID(break_loc_id));
281b60736ecSDimitry Andric
282b60736ecSDimitry Andric Debugger &debugger = target->GetDebugger();
283b60736ecSDimitry Andric ScriptInterpreterLua *lua_interpreter = static_cast<ScriptInterpreterLua *>(
284b60736ecSDimitry Andric debugger.GetScriptInterpreter(true, eScriptLanguageLua));
285b60736ecSDimitry Andric Lua &lua = lua_interpreter->GetLua();
286b60736ecSDimitry Andric
287b60736ecSDimitry Andric CommandDataLua *bp_option_data = static_cast<CommandDataLua *>(baton);
288b60736ecSDimitry Andric llvm::Expected<bool> BoolOrErr = lua.CallBreakpointCallback(
289b60736ecSDimitry Andric baton, stop_frame_sp, bp_loc_sp, bp_option_data->m_extra_args_sp);
290b60736ecSDimitry Andric if (llvm::Error E = BoolOrErr.takeError()) {
291b60736ecSDimitry Andric debugger.GetErrorStream() << toString(std::move(E));
292b60736ecSDimitry Andric return true;
293b60736ecSDimitry Andric }
294b60736ecSDimitry Andric
295b60736ecSDimitry Andric return *BoolOrErr;
296b60736ecSDimitry Andric }
297b60736ecSDimitry Andric
WatchpointCallbackFunction(void * baton,StoppointCallbackContext * context,user_id_t watch_id)298344a3780SDimitry Andric bool ScriptInterpreterLua::WatchpointCallbackFunction(
299344a3780SDimitry Andric void *baton, StoppointCallbackContext *context, user_id_t watch_id) {
300344a3780SDimitry Andric assert(context);
301344a3780SDimitry Andric
302344a3780SDimitry Andric ExecutionContext exe_ctx(context->exe_ctx_ref);
303344a3780SDimitry Andric Target *target = exe_ctx.GetTargetPtr();
304344a3780SDimitry Andric if (target == nullptr)
305344a3780SDimitry Andric return true;
306344a3780SDimitry Andric
307344a3780SDimitry Andric StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
308344a3780SDimitry Andric WatchpointSP wp_sp = target->GetWatchpointList().FindByID(watch_id);
309344a3780SDimitry Andric
310344a3780SDimitry Andric Debugger &debugger = target->GetDebugger();
311344a3780SDimitry Andric ScriptInterpreterLua *lua_interpreter = static_cast<ScriptInterpreterLua *>(
312344a3780SDimitry Andric debugger.GetScriptInterpreter(true, eScriptLanguageLua));
313344a3780SDimitry Andric Lua &lua = lua_interpreter->GetLua();
314344a3780SDimitry Andric
315344a3780SDimitry Andric llvm::Expected<bool> BoolOrErr =
316344a3780SDimitry Andric lua.CallWatchpointCallback(baton, stop_frame_sp, wp_sp);
317344a3780SDimitry Andric if (llvm::Error E = BoolOrErr.takeError()) {
318344a3780SDimitry Andric debugger.GetErrorStream() << toString(std::move(E));
319344a3780SDimitry Andric return true;
320344a3780SDimitry Andric }
321344a3780SDimitry Andric
322344a3780SDimitry Andric return *BoolOrErr;
323344a3780SDimitry Andric }
324344a3780SDimitry Andric
CollectDataForBreakpointCommandCallback(std::vector<std::reference_wrapper<BreakpointOptions>> & bp_options_vec,CommandReturnObject & result)325b60736ecSDimitry Andric void ScriptInterpreterLua::CollectDataForBreakpointCommandCallback(
326344a3780SDimitry Andric std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
327b60736ecSDimitry Andric CommandReturnObject &result) {
328b60736ecSDimitry Andric IOHandlerSP io_handler_sp(
329b60736ecSDimitry Andric new IOHandlerLuaInterpreter(m_debugger, *this, eIOHandlerBreakpoint));
330b60736ecSDimitry Andric io_handler_sp->SetUserData(&bp_options_vec);
331b60736ecSDimitry Andric m_debugger.RunIOHandlerAsync(io_handler_sp);
332b60736ecSDimitry Andric }
333b60736ecSDimitry Andric
CollectDataForWatchpointCommandCallback(WatchpointOptions * wp_options,CommandReturnObject & result)334344a3780SDimitry Andric void ScriptInterpreterLua::CollectDataForWatchpointCommandCallback(
335344a3780SDimitry Andric WatchpointOptions *wp_options, CommandReturnObject &result) {
336344a3780SDimitry Andric IOHandlerSP io_handler_sp(
337344a3780SDimitry Andric new IOHandlerLuaInterpreter(m_debugger, *this, eIOHandlerWatchpoint));
338344a3780SDimitry Andric io_handler_sp->SetUserData(wp_options);
339344a3780SDimitry Andric m_debugger.RunIOHandlerAsync(io_handler_sp);
340344a3780SDimitry Andric }
341344a3780SDimitry Andric
SetBreakpointCommandCallbackFunction(BreakpointOptions & bp_options,const char * function_name,StructuredData::ObjectSP extra_args_sp)342b60736ecSDimitry Andric Status ScriptInterpreterLua::SetBreakpointCommandCallbackFunction(
343344a3780SDimitry Andric BreakpointOptions &bp_options, const char *function_name,
344b60736ecSDimitry Andric StructuredData::ObjectSP extra_args_sp) {
345b60736ecSDimitry Andric const char *fmt_str = "return {0}(frame, bp_loc, ...)";
346b60736ecSDimitry Andric std::string oneliner = llvm::formatv(fmt_str, function_name).str();
347b60736ecSDimitry Andric return RegisterBreakpointCallback(bp_options, oneliner.c_str(),
348b60736ecSDimitry Andric extra_args_sp);
349b60736ecSDimitry Andric }
350b60736ecSDimitry Andric
SetBreakpointCommandCallback(BreakpointOptions & bp_options,const char * command_body_text,bool is_callback)351b60736ecSDimitry Andric Status ScriptInterpreterLua::SetBreakpointCommandCallback(
3527fa27ce4SDimitry Andric BreakpointOptions &bp_options, const char *command_body_text,
3537fa27ce4SDimitry Andric bool is_callback) {
354b60736ecSDimitry Andric return RegisterBreakpointCallback(bp_options, command_body_text, {});
355b60736ecSDimitry Andric }
356b60736ecSDimitry Andric
RegisterBreakpointCallback(BreakpointOptions & bp_options,const char * command_body_text,StructuredData::ObjectSP extra_args_sp)357b60736ecSDimitry Andric Status ScriptInterpreterLua::RegisterBreakpointCallback(
358344a3780SDimitry Andric BreakpointOptions &bp_options, const char *command_body_text,
359b60736ecSDimitry Andric StructuredData::ObjectSP extra_args_sp) {
360b60736ecSDimitry Andric Status error;
361b60736ecSDimitry Andric auto data_up = std::make_unique<CommandDataLua>(extra_args_sp);
362b60736ecSDimitry Andric error = m_lua->RegisterBreakpointCallback(data_up.get(), command_body_text);
363b60736ecSDimitry Andric if (error.Fail())
364b60736ecSDimitry Andric return error;
365b60736ecSDimitry Andric auto baton_sp =
366b60736ecSDimitry Andric std::make_shared<BreakpointOptions::CommandBaton>(std::move(data_up));
367344a3780SDimitry Andric bp_options.SetCallback(ScriptInterpreterLua::BreakpointCallbackFunction,
368344a3780SDimitry Andric baton_sp);
369344a3780SDimitry Andric return error;
370344a3780SDimitry Andric }
371344a3780SDimitry Andric
SetWatchpointCommandCallback(WatchpointOptions * wp_options,const char * command_body_text,bool is_callback)372344a3780SDimitry Andric void ScriptInterpreterLua::SetWatchpointCommandCallback(
3737fa27ce4SDimitry Andric WatchpointOptions *wp_options, const char *command_body_text,
3747fa27ce4SDimitry Andric bool is_callback) {
375344a3780SDimitry Andric RegisterWatchpointCallback(wp_options, command_body_text, {});
376344a3780SDimitry Andric }
377344a3780SDimitry Andric
RegisterWatchpointCallback(WatchpointOptions * wp_options,const char * command_body_text,StructuredData::ObjectSP extra_args_sp)378344a3780SDimitry Andric Status ScriptInterpreterLua::RegisterWatchpointCallback(
379344a3780SDimitry Andric WatchpointOptions *wp_options, const char *command_body_text,
380344a3780SDimitry Andric StructuredData::ObjectSP extra_args_sp) {
381344a3780SDimitry Andric Status error;
382344a3780SDimitry Andric auto data_up = std::make_unique<WatchpointOptions::CommandData>();
383344a3780SDimitry Andric error = m_lua->RegisterWatchpointCallback(data_up.get(), command_body_text);
384344a3780SDimitry Andric if (error.Fail())
385344a3780SDimitry Andric return error;
386344a3780SDimitry Andric auto baton_sp =
387344a3780SDimitry Andric std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
388344a3780SDimitry Andric wp_options->SetCallback(ScriptInterpreterLua::WatchpointCallbackFunction,
389b60736ecSDimitry Andric baton_sp);
390b60736ecSDimitry Andric return error;
391b60736ecSDimitry Andric }
392b60736ecSDimitry Andric
393706b4fc4SDimitry Andric lldb::ScriptInterpreterSP
CreateInstance(Debugger & debugger)394706b4fc4SDimitry Andric ScriptInterpreterLua::CreateInstance(Debugger &debugger) {
395706b4fc4SDimitry Andric return std::make_shared<ScriptInterpreterLua>(debugger);
396706b4fc4SDimitry Andric }
397706b4fc4SDimitry Andric
GetPluginDescriptionStatic()398c0981da4SDimitry Andric llvm::StringRef ScriptInterpreterLua::GetPluginDescriptionStatic() {
399706b4fc4SDimitry Andric return "Lua script interpreter";
400706b4fc4SDimitry Andric }
401706b4fc4SDimitry Andric
GetLua()402706b4fc4SDimitry Andric Lua &ScriptInterpreterLua::GetLua() { return *m_lua; }
403