xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- ScriptInterpreterLua.cpp ------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "ScriptInterpreterLua.h"
10061da546Spatrick #include "Lua.h"
11be691f3bSpatrick #include "lldb/Breakpoint/StoppointCallbackContext.h"
12061da546Spatrick #include "lldb/Core/Debugger.h"
13061da546Spatrick #include "lldb/Core/PluginManager.h"
14061da546Spatrick #include "lldb/Core/StreamFile.h"
15061da546Spatrick #include "lldb/Interpreter/CommandReturnObject.h"
16be691f3bSpatrick #include "lldb/Target/ExecutionContext.h"
17061da546Spatrick #include "lldb/Utility/Stream.h"
18061da546Spatrick #include "lldb/Utility/StringList.h"
19061da546Spatrick #include "lldb/Utility/Timer.h"
20be691f3bSpatrick #include "llvm/ADT/StringRef.h"
21dda28197Spatrick #include "llvm/Support/FormatAdapters.h"
22be691f3bSpatrick #include <memory>
23be691f3bSpatrick #include <vector>
24061da546Spatrick 
25061da546Spatrick using namespace lldb;
26061da546Spatrick using namespace lldb_private;
27061da546Spatrick 
28dda28197Spatrick LLDB_PLUGIN_DEFINE(ScriptInterpreterLua)
29dda28197Spatrick 
30be691f3bSpatrick enum ActiveIOHandler {
31be691f3bSpatrick   eIOHandlerNone,
32be691f3bSpatrick   eIOHandlerBreakpoint,
33be691f3bSpatrick   eIOHandlerWatchpoint
34be691f3bSpatrick };
35be691f3bSpatrick 
36061da546Spatrick class IOHandlerLuaInterpreter : public IOHandlerDelegate,
37061da546Spatrick                                 public IOHandlerEditline {
38061da546Spatrick public:
IOHandlerLuaInterpreter(Debugger & debugger,ScriptInterpreterLua & script_interpreter,ActiveIOHandler active_io_handler=eIOHandlerNone)39061da546Spatrick   IOHandlerLuaInterpreter(Debugger &debugger,
40be691f3bSpatrick                           ScriptInterpreterLua &script_interpreter,
41be691f3bSpatrick                           ActiveIOHandler active_io_handler = eIOHandlerNone)
42061da546Spatrick       : IOHandlerEditline(debugger, IOHandler::Type::LuaInterpreter, "lua",
43*f6aab3d8Srobert                           llvm::StringRef(">>> "), llvm::StringRef("..> "),
44*f6aab3d8Srobert                           true, debugger.GetUseColor(), 0, *this),
45be691f3bSpatrick         m_script_interpreter(script_interpreter),
46be691f3bSpatrick         m_active_io_handler(active_io_handler) {
47dda28197Spatrick     llvm::cantFail(m_script_interpreter.GetLua().ChangeIO(
48dda28197Spatrick         debugger.GetOutputFile().GetStream(),
49dda28197Spatrick         debugger.GetErrorFile().GetStream()));
50061da546Spatrick     llvm::cantFail(m_script_interpreter.EnterSession(debugger.GetID()));
51061da546Spatrick   }
52061da546Spatrick 
~IOHandlerLuaInterpreter()53dda28197Spatrick   ~IOHandlerLuaInterpreter() override {
54061da546Spatrick     llvm::cantFail(m_script_interpreter.LeaveSession());
55061da546Spatrick   }
56061da546Spatrick 
IOHandlerActivated(IOHandler & io_handler,bool interactive)57be691f3bSpatrick   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
58be691f3bSpatrick     const char *instructions = nullptr;
59be691f3bSpatrick     switch (m_active_io_handler) {
60be691f3bSpatrick     case eIOHandlerNone:
61be691f3bSpatrick       break;
62be691f3bSpatrick     case eIOHandlerWatchpoint:
63be691f3bSpatrick       instructions = "Enter your Lua command(s). Type 'quit' to end.\n"
64be691f3bSpatrick                      "The commands are compiled as the body of the following "
65be691f3bSpatrick                      "Lua function\n"
66be691f3bSpatrick                      "function (frame, wp) end\n";
67be691f3bSpatrick       SetPrompt(llvm::StringRef("..> "));
68be691f3bSpatrick       break;
69be691f3bSpatrick     case eIOHandlerBreakpoint:
70be691f3bSpatrick       instructions = "Enter your Lua command(s). Type 'quit' to end.\n"
71be691f3bSpatrick                      "The commands are compiled as the body of the following "
72be691f3bSpatrick                      "Lua function\n"
73be691f3bSpatrick                      "function (frame, bp_loc, ...) end\n";
74be691f3bSpatrick       SetPrompt(llvm::StringRef("..> "));
75be691f3bSpatrick       break;
76be691f3bSpatrick     }
77be691f3bSpatrick     if (instructions == nullptr)
78be691f3bSpatrick       return;
79be691f3bSpatrick     if (interactive)
80be691f3bSpatrick       *io_handler.GetOutputStreamFileSP() << instructions;
81be691f3bSpatrick   }
82be691f3bSpatrick 
IOHandlerIsInputComplete(IOHandler & io_handler,StringList & lines)83be691f3bSpatrick   bool IOHandlerIsInputComplete(IOHandler &io_handler,
84be691f3bSpatrick                                 StringList &lines) override {
85be691f3bSpatrick     size_t last = lines.GetSize() - 1;
86be691f3bSpatrick     if (IsQuitCommand(lines.GetStringAtIndex(last))) {
87be691f3bSpatrick       if (m_active_io_handler == eIOHandlerBreakpoint ||
88be691f3bSpatrick           m_active_io_handler == eIOHandlerWatchpoint)
89be691f3bSpatrick         lines.DeleteStringAtIndex(last);
90be691f3bSpatrick       return true;
91be691f3bSpatrick     }
92be691f3bSpatrick     StreamString str;
93be691f3bSpatrick     lines.Join("\n", str);
94be691f3bSpatrick     if (llvm::Error E =
95be691f3bSpatrick             m_script_interpreter.GetLua().CheckSyntax(str.GetString())) {
96be691f3bSpatrick       std::string error_str = toString(std::move(E));
97be691f3bSpatrick       // Lua always errors out to incomplete code with '<eof>'
98be691f3bSpatrick       return error_str.find("<eof>") == std::string::npos;
99be691f3bSpatrick     }
100be691f3bSpatrick     // The breakpoint and watchpoint handler only exits with a explicit 'quit'
101be691f3bSpatrick     return m_active_io_handler != eIOHandlerBreakpoint &&
102be691f3bSpatrick            m_active_io_handler != eIOHandlerWatchpoint;
103be691f3bSpatrick   }
104be691f3bSpatrick 
IOHandlerInputComplete(IOHandler & io_handler,std::string & data)105061da546Spatrick   void IOHandlerInputComplete(IOHandler &io_handler,
106061da546Spatrick                               std::string &data) override {
107be691f3bSpatrick     switch (m_active_io_handler) {
108be691f3bSpatrick     case eIOHandlerBreakpoint: {
109be691f3bSpatrick       auto *bp_options_vec =
110be691f3bSpatrick           static_cast<std::vector<std::reference_wrapper<BreakpointOptions>> *>(
111be691f3bSpatrick               io_handler.GetUserData());
112be691f3bSpatrick       for (BreakpointOptions &bp_options : *bp_options_vec) {
113be691f3bSpatrick         Status error = m_script_interpreter.SetBreakpointCommandCallback(
114be691f3bSpatrick             bp_options, data.c_str());
115be691f3bSpatrick         if (error.Fail())
116be691f3bSpatrick           *io_handler.GetErrorStreamFileSP() << error.AsCString() << '\n';
117be691f3bSpatrick       }
118be691f3bSpatrick       io_handler.SetIsDone(true);
119be691f3bSpatrick     } break;
120be691f3bSpatrick     case eIOHandlerWatchpoint: {
121be691f3bSpatrick       auto *wp_options =
122be691f3bSpatrick           static_cast<WatchpointOptions *>(io_handler.GetUserData());
123be691f3bSpatrick       m_script_interpreter.SetWatchpointCommandCallback(wp_options,
124be691f3bSpatrick                                                         data.c_str());
125be691f3bSpatrick       io_handler.SetIsDone(true);
126be691f3bSpatrick     } break;
127be691f3bSpatrick     case eIOHandlerNone:
128be691f3bSpatrick       if (IsQuitCommand(data)) {
129dda28197Spatrick         io_handler.SetIsDone(true);
130dda28197Spatrick         return;
131dda28197Spatrick       }
132be691f3bSpatrick       if (llvm::Error error = m_script_interpreter.GetLua().Run(data))
133be691f3bSpatrick         *io_handler.GetErrorStreamFileSP() << toString(std::move(error));
134be691f3bSpatrick       break;
135061da546Spatrick     }
136061da546Spatrick   }
137061da546Spatrick 
138061da546Spatrick private:
139061da546Spatrick   ScriptInterpreterLua &m_script_interpreter;
140be691f3bSpatrick   ActiveIOHandler m_active_io_handler;
141be691f3bSpatrick 
IsQuitCommand(llvm::StringRef cmd)142be691f3bSpatrick   bool IsQuitCommand(llvm::StringRef cmd) { return cmd.rtrim() == "quit"; }
143061da546Spatrick };
144061da546Spatrick 
ScriptInterpreterLua(Debugger & debugger)145061da546Spatrick ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger)
146061da546Spatrick     : ScriptInterpreter(debugger, eScriptLanguageLua),
147061da546Spatrick       m_lua(std::make_unique<Lua>()) {}
148061da546Spatrick 
149be691f3bSpatrick ScriptInterpreterLua::~ScriptInterpreterLua() = default;
150061da546Spatrick 
GetInterpreterInfo()151*f6aab3d8Srobert StructuredData::DictionarySP ScriptInterpreterLua::GetInterpreterInfo() {
152*f6aab3d8Srobert   auto info = std::make_shared<StructuredData::Dictionary>();
153*f6aab3d8Srobert   info->AddStringItem("language", "lua");
154*f6aab3d8Srobert   return info;
155*f6aab3d8Srobert }
156*f6aab3d8Srobert 
ExecuteOneLine(llvm::StringRef command,CommandReturnObject * result,const ExecuteScriptOptions & options)157061da546Spatrick bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command,
158061da546Spatrick                                           CommandReturnObject *result,
159061da546Spatrick                                           const ExecuteScriptOptions &options) {
160dda28197Spatrick   if (command.empty()) {
161dda28197Spatrick     if (result)
162dda28197Spatrick       result->AppendError("empty command passed to lua\n");
163dda28197Spatrick     return false;
164dda28197Spatrick   }
165dda28197Spatrick 
166dda28197Spatrick   llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
167dda28197Spatrick       io_redirect_or_error = ScriptInterpreterIORedirect::Create(
168dda28197Spatrick           options.GetEnableIO(), m_debugger, result);
169dda28197Spatrick   if (!io_redirect_or_error) {
170dda28197Spatrick     if (result)
171dda28197Spatrick       result->AppendErrorWithFormatv(
172dda28197Spatrick           "failed to redirect I/O: {0}\n",
173dda28197Spatrick           llvm::fmt_consume(io_redirect_or_error.takeError()));
174dda28197Spatrick     else
175dda28197Spatrick       llvm::consumeError(io_redirect_or_error.takeError());
176dda28197Spatrick     return false;
177dda28197Spatrick   }
178dda28197Spatrick 
179dda28197Spatrick   ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error;
180dda28197Spatrick 
181dda28197Spatrick   if (llvm::Error e =
182dda28197Spatrick           m_lua->ChangeIO(io_redirect.GetOutputFile()->GetStream(),
183dda28197Spatrick                           io_redirect.GetErrorFile()->GetStream())) {
184dda28197Spatrick     result->AppendErrorWithFormatv("lua failed to redirect I/O: {0}\n",
185dda28197Spatrick                                    llvm::toString(std::move(e)));
186dda28197Spatrick     return false;
187dda28197Spatrick   }
188dda28197Spatrick 
189061da546Spatrick   if (llvm::Error e = m_lua->Run(command)) {
190061da546Spatrick     result->AppendErrorWithFormatv(
191061da546Spatrick         "lua failed attempting to evaluate '{0}': {1}\n", command,
192061da546Spatrick         llvm::toString(std::move(e)));
193061da546Spatrick     return false;
194061da546Spatrick   }
195dda28197Spatrick 
196dda28197Spatrick   io_redirect.Flush();
197061da546Spatrick   return true;
198061da546Spatrick }
199061da546Spatrick 
ExecuteInterpreterLoop()200061da546Spatrick void ScriptInterpreterLua::ExecuteInterpreterLoop() {
201be691f3bSpatrick   LLDB_SCOPED_TIMER();
202061da546Spatrick 
203061da546Spatrick   // At the moment, the only time the debugger does not have an input file
204061da546Spatrick   // handle is when this is called directly from lua, in which case it is
205061da546Spatrick   // both dangerous and unnecessary (not to mention confusing) to try to embed
206061da546Spatrick   // a running interpreter loop inside the already running lua interpreter
207061da546Spatrick   // loop, so we won't do it.
208dda28197Spatrick   if (!m_debugger.GetInputFile().IsValid())
209061da546Spatrick     return;
210061da546Spatrick 
211dda28197Spatrick   IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(m_debugger, *this));
212dda28197Spatrick   m_debugger.RunIOHandlerAsync(io_handler_sp);
213061da546Spatrick }
214061da546Spatrick 
LoadScriptingModule(const char * filename,const LoadScriptOptions & options,lldb_private::Status & error,StructuredData::ObjectSP * module_sp,FileSpec extra_search_dir)215061da546Spatrick bool ScriptInterpreterLua::LoadScriptingModule(
216be691f3bSpatrick     const char *filename, const LoadScriptOptions &options,
217be691f3bSpatrick     lldb_private::Status &error, StructuredData::ObjectSP *module_sp,
218be691f3bSpatrick     FileSpec extra_search_dir) {
219061da546Spatrick 
220061da546Spatrick   if (llvm::Error e = m_lua->LoadModule(filename)) {
221061da546Spatrick     error.SetErrorStringWithFormatv("lua failed to import '{0}': {1}\n",
222061da546Spatrick                                     filename, llvm::toString(std::move(e)));
223061da546Spatrick     return false;
224061da546Spatrick   }
225061da546Spatrick   return true;
226061da546Spatrick }
227061da546Spatrick 
Initialize()228061da546Spatrick void ScriptInterpreterLua::Initialize() {
229061da546Spatrick   static llvm::once_flag g_once_flag;
230061da546Spatrick 
231061da546Spatrick   llvm::call_once(g_once_flag, []() {
232061da546Spatrick     PluginManager::RegisterPlugin(GetPluginNameStatic(),
233061da546Spatrick                                   GetPluginDescriptionStatic(),
234061da546Spatrick                                   lldb::eScriptLanguageLua, CreateInstance);
235061da546Spatrick   });
236061da546Spatrick }
237061da546Spatrick 
Terminate()238061da546Spatrick void ScriptInterpreterLua::Terminate() {}
239061da546Spatrick 
EnterSession(user_id_t debugger_id)240061da546Spatrick llvm::Error ScriptInterpreterLua::EnterSession(user_id_t debugger_id) {
241061da546Spatrick   if (m_session_is_active)
242061da546Spatrick     return llvm::Error::success();
243061da546Spatrick 
244061da546Spatrick   const char *fmt_str =
245061da546Spatrick       "lldb.debugger = lldb.SBDebugger.FindDebuggerWithID({0}); "
246061da546Spatrick       "lldb.target = lldb.debugger:GetSelectedTarget(); "
247061da546Spatrick       "lldb.process = lldb.target:GetProcess(); "
248061da546Spatrick       "lldb.thread = lldb.process:GetSelectedThread(); "
249061da546Spatrick       "lldb.frame = lldb.thread:GetSelectedFrame()";
250061da546Spatrick   return m_lua->Run(llvm::formatv(fmt_str, debugger_id).str());
251061da546Spatrick }
252061da546Spatrick 
LeaveSession()253061da546Spatrick llvm::Error ScriptInterpreterLua::LeaveSession() {
254061da546Spatrick   if (!m_session_is_active)
255061da546Spatrick     return llvm::Error::success();
256061da546Spatrick 
257061da546Spatrick   m_session_is_active = false;
258061da546Spatrick 
259061da546Spatrick   llvm::StringRef str = "lldb.debugger = nil; "
260061da546Spatrick                         "lldb.target = nil; "
261061da546Spatrick                         "lldb.process = nil; "
262061da546Spatrick                         "lldb.thread = nil; "
263061da546Spatrick                         "lldb.frame = nil";
264061da546Spatrick   return m_lua->Run(str);
265061da546Spatrick }
266061da546Spatrick 
BreakpointCallbackFunction(void * baton,StoppointCallbackContext * context,user_id_t break_id,user_id_t break_loc_id)267be691f3bSpatrick bool ScriptInterpreterLua::BreakpointCallbackFunction(
268be691f3bSpatrick     void *baton, StoppointCallbackContext *context, user_id_t break_id,
269be691f3bSpatrick     user_id_t break_loc_id) {
270be691f3bSpatrick   assert(context);
271be691f3bSpatrick 
272be691f3bSpatrick   ExecutionContext exe_ctx(context->exe_ctx_ref);
273be691f3bSpatrick   Target *target = exe_ctx.GetTargetPtr();
274be691f3bSpatrick   if (target == nullptr)
275be691f3bSpatrick     return true;
276be691f3bSpatrick 
277be691f3bSpatrick   StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
278be691f3bSpatrick   BreakpointSP breakpoint_sp = target->GetBreakpointByID(break_id);
279be691f3bSpatrick   BreakpointLocationSP bp_loc_sp(breakpoint_sp->FindLocationByID(break_loc_id));
280be691f3bSpatrick 
281be691f3bSpatrick   Debugger &debugger = target->GetDebugger();
282be691f3bSpatrick   ScriptInterpreterLua *lua_interpreter = static_cast<ScriptInterpreterLua *>(
283be691f3bSpatrick       debugger.GetScriptInterpreter(true, eScriptLanguageLua));
284be691f3bSpatrick   Lua &lua = lua_interpreter->GetLua();
285be691f3bSpatrick 
286be691f3bSpatrick   CommandDataLua *bp_option_data = static_cast<CommandDataLua *>(baton);
287be691f3bSpatrick   llvm::Expected<bool> BoolOrErr = lua.CallBreakpointCallback(
288be691f3bSpatrick       baton, stop_frame_sp, bp_loc_sp, bp_option_data->m_extra_args_sp);
289be691f3bSpatrick   if (llvm::Error E = BoolOrErr.takeError()) {
290be691f3bSpatrick     debugger.GetErrorStream() << toString(std::move(E));
291be691f3bSpatrick     return true;
292be691f3bSpatrick   }
293be691f3bSpatrick 
294be691f3bSpatrick   return *BoolOrErr;
295be691f3bSpatrick }
296be691f3bSpatrick 
WatchpointCallbackFunction(void * baton,StoppointCallbackContext * context,user_id_t watch_id)297be691f3bSpatrick bool ScriptInterpreterLua::WatchpointCallbackFunction(
298be691f3bSpatrick     void *baton, StoppointCallbackContext *context, user_id_t watch_id) {
299be691f3bSpatrick   assert(context);
300be691f3bSpatrick 
301be691f3bSpatrick   ExecutionContext exe_ctx(context->exe_ctx_ref);
302be691f3bSpatrick   Target *target = exe_ctx.GetTargetPtr();
303be691f3bSpatrick   if (target == nullptr)
304be691f3bSpatrick     return true;
305be691f3bSpatrick 
306be691f3bSpatrick   StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
307be691f3bSpatrick   WatchpointSP wp_sp = target->GetWatchpointList().FindByID(watch_id);
308be691f3bSpatrick 
309be691f3bSpatrick   Debugger &debugger = target->GetDebugger();
310be691f3bSpatrick   ScriptInterpreterLua *lua_interpreter = static_cast<ScriptInterpreterLua *>(
311be691f3bSpatrick       debugger.GetScriptInterpreter(true, eScriptLanguageLua));
312be691f3bSpatrick   Lua &lua = lua_interpreter->GetLua();
313be691f3bSpatrick 
314be691f3bSpatrick   llvm::Expected<bool> BoolOrErr =
315be691f3bSpatrick       lua.CallWatchpointCallback(baton, stop_frame_sp, wp_sp);
316be691f3bSpatrick   if (llvm::Error E = BoolOrErr.takeError()) {
317be691f3bSpatrick     debugger.GetErrorStream() << toString(std::move(E));
318be691f3bSpatrick     return true;
319be691f3bSpatrick   }
320be691f3bSpatrick 
321be691f3bSpatrick   return *BoolOrErr;
322be691f3bSpatrick }
323be691f3bSpatrick 
CollectDataForBreakpointCommandCallback(std::vector<std::reference_wrapper<BreakpointOptions>> & bp_options_vec,CommandReturnObject & result)324be691f3bSpatrick void ScriptInterpreterLua::CollectDataForBreakpointCommandCallback(
325be691f3bSpatrick     std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
326be691f3bSpatrick     CommandReturnObject &result) {
327be691f3bSpatrick   IOHandlerSP io_handler_sp(
328be691f3bSpatrick       new IOHandlerLuaInterpreter(m_debugger, *this, eIOHandlerBreakpoint));
329be691f3bSpatrick   io_handler_sp->SetUserData(&bp_options_vec);
330be691f3bSpatrick   m_debugger.RunIOHandlerAsync(io_handler_sp);
331be691f3bSpatrick }
332be691f3bSpatrick 
CollectDataForWatchpointCommandCallback(WatchpointOptions * wp_options,CommandReturnObject & result)333be691f3bSpatrick void ScriptInterpreterLua::CollectDataForWatchpointCommandCallback(
334be691f3bSpatrick     WatchpointOptions *wp_options, CommandReturnObject &result) {
335be691f3bSpatrick   IOHandlerSP io_handler_sp(
336be691f3bSpatrick       new IOHandlerLuaInterpreter(m_debugger, *this, eIOHandlerWatchpoint));
337be691f3bSpatrick   io_handler_sp->SetUserData(wp_options);
338be691f3bSpatrick   m_debugger.RunIOHandlerAsync(io_handler_sp);
339be691f3bSpatrick }
340be691f3bSpatrick 
SetBreakpointCommandCallbackFunction(BreakpointOptions & bp_options,const char * function_name,StructuredData::ObjectSP extra_args_sp)341be691f3bSpatrick Status ScriptInterpreterLua::SetBreakpointCommandCallbackFunction(
342be691f3bSpatrick     BreakpointOptions &bp_options, const char *function_name,
343be691f3bSpatrick     StructuredData::ObjectSP extra_args_sp) {
344be691f3bSpatrick   const char *fmt_str = "return {0}(frame, bp_loc, ...)";
345be691f3bSpatrick   std::string oneliner = llvm::formatv(fmt_str, function_name).str();
346be691f3bSpatrick   return RegisterBreakpointCallback(bp_options, oneliner.c_str(),
347be691f3bSpatrick                                     extra_args_sp);
348be691f3bSpatrick }
349be691f3bSpatrick 
SetBreakpointCommandCallback(BreakpointOptions & bp_options,const char * command_body_text)350be691f3bSpatrick Status ScriptInterpreterLua::SetBreakpointCommandCallback(
351be691f3bSpatrick     BreakpointOptions &bp_options, const char *command_body_text) {
352be691f3bSpatrick   return RegisterBreakpointCallback(bp_options, command_body_text, {});
353be691f3bSpatrick }
354be691f3bSpatrick 
RegisterBreakpointCallback(BreakpointOptions & bp_options,const char * command_body_text,StructuredData::ObjectSP extra_args_sp)355be691f3bSpatrick Status ScriptInterpreterLua::RegisterBreakpointCallback(
356be691f3bSpatrick     BreakpointOptions &bp_options, const char *command_body_text,
357be691f3bSpatrick     StructuredData::ObjectSP extra_args_sp) {
358be691f3bSpatrick   Status error;
359be691f3bSpatrick   auto data_up = std::make_unique<CommandDataLua>(extra_args_sp);
360be691f3bSpatrick   error = m_lua->RegisterBreakpointCallback(data_up.get(), command_body_text);
361be691f3bSpatrick   if (error.Fail())
362be691f3bSpatrick     return error;
363be691f3bSpatrick   auto baton_sp =
364be691f3bSpatrick       std::make_shared<BreakpointOptions::CommandBaton>(std::move(data_up));
365be691f3bSpatrick   bp_options.SetCallback(ScriptInterpreterLua::BreakpointCallbackFunction,
366be691f3bSpatrick                          baton_sp);
367be691f3bSpatrick   return error;
368be691f3bSpatrick }
369be691f3bSpatrick 
SetWatchpointCommandCallback(WatchpointOptions * wp_options,const char * command_body_text)370be691f3bSpatrick void ScriptInterpreterLua::SetWatchpointCommandCallback(
371be691f3bSpatrick     WatchpointOptions *wp_options, const char *command_body_text) {
372be691f3bSpatrick   RegisterWatchpointCallback(wp_options, command_body_text, {});
373be691f3bSpatrick }
374be691f3bSpatrick 
RegisterWatchpointCallback(WatchpointOptions * wp_options,const char * command_body_text,StructuredData::ObjectSP extra_args_sp)375be691f3bSpatrick Status ScriptInterpreterLua::RegisterWatchpointCallback(
376be691f3bSpatrick     WatchpointOptions *wp_options, const char *command_body_text,
377be691f3bSpatrick     StructuredData::ObjectSP extra_args_sp) {
378be691f3bSpatrick   Status error;
379be691f3bSpatrick   auto data_up = std::make_unique<WatchpointOptions::CommandData>();
380be691f3bSpatrick   error = m_lua->RegisterWatchpointCallback(data_up.get(), command_body_text);
381be691f3bSpatrick   if (error.Fail())
382be691f3bSpatrick     return error;
383be691f3bSpatrick   auto baton_sp =
384be691f3bSpatrick       std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
385be691f3bSpatrick   wp_options->SetCallback(ScriptInterpreterLua::WatchpointCallbackFunction,
386be691f3bSpatrick                           baton_sp);
387be691f3bSpatrick   return error;
388be691f3bSpatrick }
389be691f3bSpatrick 
390061da546Spatrick lldb::ScriptInterpreterSP
CreateInstance(Debugger & debugger)391061da546Spatrick ScriptInterpreterLua::CreateInstance(Debugger &debugger) {
392061da546Spatrick   return std::make_shared<ScriptInterpreterLua>(debugger);
393061da546Spatrick }
394061da546Spatrick 
GetPluginDescriptionStatic()395*f6aab3d8Srobert llvm::StringRef ScriptInterpreterLua::GetPluginDescriptionStatic() {
396061da546Spatrick   return "Lua script interpreter";
397061da546Spatrick }
398061da546Spatrick 
GetLua()399061da546Spatrick Lua &ScriptInterpreterLua::GetLua() { return *m_lua; }
400