1 //===-- ScriptInterpreterLua.h ----------------------------------*- C++ -*-===//
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 
9 #ifndef liblldb_ScriptInterpreterLua_h_
10 #define liblldb_ScriptInterpreterLua_h_
11 
12 #include <vector>
13 
14 #include "lldb/Breakpoint/WatchpointOptions.h"
15 #include "lldb/Core/StructuredDataImpl.h"
16 #include "lldb/Interpreter/ScriptInterpreter.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/lldb-enumerations.h"
19 
20 namespace lldb_private {
21 class Lua;
22 class ScriptInterpreterLua : public ScriptInterpreter {
23 public:
24   class CommandDataLua : public BreakpointOptions::CommandData {
25   public:
26     CommandDataLua() : BreakpointOptions::CommandData() {
27       interpreter = lldb::eScriptLanguageLua;
28     }
29     CommandDataLua(StructuredData::ObjectSP extra_args_sp)
30         : BreakpointOptions::CommandData(), m_extra_args_sp(extra_args_sp) {
31       interpreter = lldb::eScriptLanguageLua;
32     }
33     StructuredData::ObjectSP m_extra_args_sp;
34   };
35 
36   ScriptInterpreterLua(Debugger &debugger);
37 
38   ~ScriptInterpreterLua() override;
39 
40   bool ExecuteOneLine(
41       llvm::StringRef command, CommandReturnObject *result,
42       const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
43 
44   void ExecuteInterpreterLoop() override;
45 
46   bool LoadScriptingModule(const char *filename,
47                            const LoadScriptOptions &options,
48                            lldb_private::Status &error,
49                            StructuredData::ObjectSP *module_sp = nullptr,
50                            FileSpec extra_search_dir = {}) override;
51 
52   StructuredData::DictionarySP GetInterpreterInfo() override;
53 
54   // Static Functions
55   static void Initialize();
56 
57   static void Terminate();
58 
59   static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger);
60 
61   static llvm::StringRef GetPluginNameStatic() { return "script-lua"; }
62 
63   static llvm::StringRef GetPluginDescriptionStatic();
64 
65   static bool BreakpointCallbackFunction(void *baton,
66                                          StoppointCallbackContext *context,
67                                          lldb::user_id_t break_id,
68                                          lldb::user_id_t break_loc_id);
69 
70   static bool WatchpointCallbackFunction(void *baton,
71                                          StoppointCallbackContext *context,
72                                          lldb::user_id_t watch_id);
73 
74   // PluginInterface protocol
75   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
76 
77   Lua &GetLua();
78 
79   llvm::Error EnterSession(lldb::user_id_t debugger_id);
80   llvm::Error LeaveSession();
81 
82   void CollectDataForBreakpointCommandCallback(
83       std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
84       CommandReturnObject &result) override;
85 
86   void
87   CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
88                                           CommandReturnObject &result) override;
89 
90   Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
91                                       const char *command_body_text,
92                                       bool is_callback) override;
93 
94   void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
95                                     const char *command_body_text,
96                                     bool is_callback) override;
97 
98   Status SetBreakpointCommandCallbackFunction(
99       BreakpointOptions &bp_options, const char *function_name,
100       StructuredData::ObjectSP extra_args_sp) override;
101 
102 private:
103   std::unique_ptr<Lua> m_lua;
104   bool m_session_is_active = false;
105 
106   Status RegisterBreakpointCallback(BreakpointOptions &bp_options,
107                                     const char *command_body_text,
108                                     StructuredData::ObjectSP extra_args_sp);
109 
110   Status RegisterWatchpointCallback(WatchpointOptions *wp_options,
111                                     const char *command_body_text,
112                                     StructuredData::ObjectSP extra_args_sp);
113 };
114 
115 } // namespace lldb_private
116 
117 #endif // liblldb_ScriptInterpreterLua_h_
118