xref: /llvm-project/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp (revision a0d7406ae800c45dd9cb438c7ae1f55329d198e2)
1 //===-- Lua.cpp -----------------------------------------------------------===//
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 #include "Lua.h"
10 #include "lldb/Host/FileSystem.h"
11 #include "lldb/Utility/FileSpec.h"
12 #include "llvm/Support/Error.h"
13 #include "llvm/Support/FormatVariadic.h"
14 
15 using namespace lldb_private;
16 using namespace lldb;
17 
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
20 
21 // Disable warning C4190: 'LLDBSwigPythonBreakpointCallbackFunction' has
22 // C-linkage specified, but returns UDT 'llvm::Expected<bool>' which is
23 // incompatible with C
24 #if _MSC_VER
25 #pragma warning (push)
26 #pragma warning (disable : 4190)
27 #endif
28 
29 extern "C" llvm::Expected<bool>
30 LLDBSwigLuaBreakpointCallbackFunction(lua_State *L,
31                                       lldb::StackFrameSP stop_frame_sp,
32                                       lldb::BreakpointLocationSP bp_loc_sp);
33 
34 #if _MSC_VER
35 #pragma warning (pop)
36 #endif
37 
38 #pragma clang diagnostic pop
39 
40 static int lldb_print(lua_State *L) {
41   int n = lua_gettop(L);
42   lua_getglobal(L, "io");
43   lua_getfield(L, -1, "stdout");
44   lua_getfield(L, -1, "write");
45   for (int i = 1; i <= n; i++) {
46     lua_pushvalue(L, -1); // write()
47     lua_pushvalue(L, -3); // io.stdout
48     luaL_tolstring(L, i, nullptr);
49     lua_pushstring(L, i != n ? "\t" : "\n");
50     lua_call(L, 3, 0);
51   }
52   return 0;
53 }
54 
55 Lua::Lua() : m_lua_state(luaL_newstate()) {
56   assert(m_lua_state);
57   luaL_openlibs(m_lua_state);
58   luaopen_lldb(m_lua_state);
59   lua_pushcfunction(m_lua_state, lldb_print);
60   lua_setglobal(m_lua_state, "print");
61 }
62 
63 Lua::~Lua() {
64   assert(m_lua_state);
65   lua_close(m_lua_state);
66 }
67 
68 llvm::Error Lua::Run(llvm::StringRef buffer) {
69   int error =
70       luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer") ||
71       lua_pcall(m_lua_state, 0, 0, 0);
72   if (error == LUA_OK)
73     return llvm::Error::success();
74 
75   llvm::Error e = llvm::make_error<llvm::StringError>(
76       llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
77       llvm::inconvertibleErrorCode());
78   // Pop error message from the stack.
79   lua_pop(m_lua_state, 1);
80   return e;
81 }
82 
83 llvm::Error Lua::RegisterBreakpointCallback(void *baton, const char *body) {
84   lua_pushlightuserdata(m_lua_state, baton);
85   const char *fmt_str = "return function(frame, bp_loc, ...) {0} end";
86   std::string func_str = llvm::formatv(fmt_str, body).str();
87   if (luaL_dostring(m_lua_state, func_str.c_str()) != LUA_OK) {
88     llvm::Error e = llvm::make_error<llvm::StringError>(
89         llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
90         llvm::inconvertibleErrorCode());
91     // Pop error message from the stack.
92     lua_pop(m_lua_state, 2);
93     return e;
94   }
95   lua_settable(m_lua_state, LUA_REGISTRYINDEX);
96   return llvm::Error::success();
97 }
98 
99 llvm::Expected<bool>
100 Lua::CallBreakpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp,
101                             lldb::BreakpointLocationSP bp_loc_sp) {
102   lua_pushlightuserdata(m_lua_state, baton);
103   lua_gettable(m_lua_state, LUA_REGISTRYINDEX);
104   return LLDBSwigLuaBreakpointCallbackFunction(m_lua_state, stop_frame_sp,
105                                                bp_loc_sp);
106 }
107 
108 llvm::Error Lua::LoadModule(llvm::StringRef filename) {
109   FileSpec file(filename);
110   if (!FileSystem::Instance().Exists(file)) {
111     return llvm::make_error<llvm::StringError>("invalid path",
112                                                llvm::inconvertibleErrorCode());
113   }
114 
115   ConstString module_extension = file.GetFileNameExtension();
116   if (module_extension != ".lua") {
117     return llvm::make_error<llvm::StringError>("invalid extension",
118                                                llvm::inconvertibleErrorCode());
119   }
120 
121   int error = luaL_loadfile(m_lua_state, filename.data()) ||
122               lua_pcall(m_lua_state, 0, 1, 0);
123   if (error != LUA_OK) {
124     llvm::Error e = llvm::make_error<llvm::StringError>(
125         llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
126         llvm::inconvertibleErrorCode());
127     // Pop error message from the stack.
128     lua_pop(m_lua_state, 1);
129     return e;
130   }
131 
132   ConstString module_name = file.GetFileNameStrippingExtension();
133   lua_setglobal(m_lua_state, module_name.GetCString());
134   return llvm::Error::success();
135 }
136 
137 llvm::Error Lua::ChangeIO(FILE *out, FILE *err) {
138   assert(out != nullptr);
139   assert(err != nullptr);
140 
141   lua_getglobal(m_lua_state, "io");
142 
143   lua_getfield(m_lua_state, -1, "stdout");
144   if (luaL_Stream *s = static_cast<luaL_Stream *>(
145           luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
146     s->f = out;
147     lua_pop(m_lua_state, 1);
148   } else {
149     lua_pop(m_lua_state, 2);
150     return llvm::make_error<llvm::StringError>("could not get stdout",
151                                                llvm::inconvertibleErrorCode());
152   }
153 
154   lua_getfield(m_lua_state, -1, "stderr");
155   if (luaL_Stream *s = static_cast<luaL_Stream *>(
156           luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
157     s->f = out;
158     lua_pop(m_lua_state, 1);
159   } else {
160     lua_pop(m_lua_state, 2);
161     return llvm::make_error<llvm::StringError>("could not get stderr",
162                                                llvm::inconvertibleErrorCode());
163   }
164 
165   lua_pop(m_lua_state, 1);
166   return llvm::Error::success();
167 }
168