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> LLDBSwigLuaBreakpointCallbackFunction(
30     lua_State *L, lldb::StackFrameSP stop_frame_sp,
31     lldb::BreakpointLocationSP bp_loc_sp, StructuredDataImpl *extra_args_impl);
32 
33 #if _MSC_VER
34 #pragma warning (pop)
35 #endif
36 
37 #pragma clang diagnostic pop
38 
39 static int lldb_print(lua_State *L) {
40   int n = lua_gettop(L);
41   lua_getglobal(L, "io");
42   lua_getfield(L, -1, "stdout");
43   lua_getfield(L, -1, "write");
44   for (int i = 1; i <= n; i++) {
45     lua_pushvalue(L, -1); // write()
46     lua_pushvalue(L, -3); // io.stdout
47     luaL_tolstring(L, i, nullptr);
48     lua_pushstring(L, i != n ? "\t" : "\n");
49     lua_call(L, 3, 0);
50   }
51   return 0;
52 }
53 
54 Lua::Lua() : m_lua_state(luaL_newstate()) {
55   assert(m_lua_state);
56   luaL_openlibs(m_lua_state);
57   luaopen_lldb(m_lua_state);
58   lua_pushcfunction(m_lua_state, lldb_print);
59   lua_setglobal(m_lua_state, "print");
60 }
61 
62 Lua::~Lua() {
63   assert(m_lua_state);
64   lua_close(m_lua_state);
65 }
66 
67 llvm::Error Lua::Run(llvm::StringRef buffer) {
68   int error =
69       luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer") ||
70       lua_pcall(m_lua_state, 0, 0, 0);
71   if (error == LUA_OK)
72     return llvm::Error::success();
73 
74   llvm::Error e = llvm::make_error<llvm::StringError>(
75       llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
76       llvm::inconvertibleErrorCode());
77   // Pop error message from the stack.
78   lua_pop(m_lua_state, 1);
79   return e;
80 }
81 
82 llvm::Error Lua::RegisterBreakpointCallback(void *baton, const char *body) {
83   lua_pushlightuserdata(m_lua_state, baton);
84   const char *fmt_str = "return function(frame, bp_loc, ...) {0} end";
85   std::string func_str = llvm::formatv(fmt_str, body).str();
86   if (luaL_dostring(m_lua_state, func_str.c_str()) != LUA_OK) {
87     llvm::Error e = llvm::make_error<llvm::StringError>(
88         llvm::formatv("{0}", lua_tostring(m_lua_state, -1)),
89         llvm::inconvertibleErrorCode());
90     // Pop error message from the stack.
91     lua_pop(m_lua_state, 2);
92     return e;
93   }
94   lua_settable(m_lua_state, LUA_REGISTRYINDEX);
95   return llvm::Error::success();
96 }
97 
98 llvm::Expected<bool>
99 Lua::CallBreakpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp,
100                             lldb::BreakpointLocationSP bp_loc_sp,
101                             StructuredData::ObjectSP extra_args_sp) {
102 
103   lua_pushlightuserdata(m_lua_state, baton);
104   lua_gettable(m_lua_state, LUA_REGISTRYINDEX);
105   auto *extra_args_impl = [&]() -> StructuredDataImpl * {
106     if (extra_args_sp == nullptr)
107       return nullptr;
108     auto *extra_args_impl = new StructuredDataImpl();
109     extra_args_impl->SetObjectSP(extra_args_sp);
110     return extra_args_impl;
111   }();
112   return LLDBSwigLuaBreakpointCallbackFunction(m_lua_state, stop_frame_sp,
113                                                bp_loc_sp, extra_args_impl);
114 }
115 
116 llvm::Error Lua::CheckSyntax(llvm::StringRef buffer) {
117   int error =
118       luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer");
119   if (error == LUA_OK) {
120     // Pop buffer
121     lua_pop(m_lua_state, 1);
122     return llvm::Error::success();
123   }
124 
125   llvm::Error e = llvm::make_error<llvm::StringError>(
126       llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
127       llvm::inconvertibleErrorCode());
128   // Pop error message from the stack.
129   lua_pop(m_lua_state, 1);
130   return e;
131 }
132 
133 llvm::Error Lua::LoadModule(llvm::StringRef filename) {
134   FileSpec file(filename);
135   if (!FileSystem::Instance().Exists(file)) {
136     return llvm::make_error<llvm::StringError>("invalid path",
137                                                llvm::inconvertibleErrorCode());
138   }
139 
140   ConstString module_extension = file.GetFileNameExtension();
141   if (module_extension != ".lua") {
142     return llvm::make_error<llvm::StringError>("invalid extension",
143                                                llvm::inconvertibleErrorCode());
144   }
145 
146   int error = luaL_loadfile(m_lua_state, filename.data()) ||
147               lua_pcall(m_lua_state, 0, 1, 0);
148   if (error != LUA_OK) {
149     llvm::Error e = llvm::make_error<llvm::StringError>(
150         llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
151         llvm::inconvertibleErrorCode());
152     // Pop error message from the stack.
153     lua_pop(m_lua_state, 1);
154     return e;
155   }
156 
157   ConstString module_name = file.GetFileNameStrippingExtension();
158   lua_setglobal(m_lua_state, module_name.GetCString());
159   return llvm::Error::success();
160 }
161 
162 llvm::Error Lua::ChangeIO(FILE *out, FILE *err) {
163   assert(out != nullptr);
164   assert(err != nullptr);
165 
166   lua_getglobal(m_lua_state, "io");
167 
168   lua_getfield(m_lua_state, -1, "stdout");
169   if (luaL_Stream *s = static_cast<luaL_Stream *>(
170           luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
171     s->f = out;
172     lua_pop(m_lua_state, 1);
173   } else {
174     lua_pop(m_lua_state, 2);
175     return llvm::make_error<llvm::StringError>("could not get stdout",
176                                                llvm::inconvertibleErrorCode());
177   }
178 
179   lua_getfield(m_lua_state, -1, "stderr");
180   if (luaL_Stream *s = static_cast<luaL_Stream *>(
181           luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
182     s->f = out;
183     lua_pop(m_lua_state, 1);
184   } else {
185     lua_pop(m_lua_state, 2);
186     return llvm::make_error<llvm::StringError>("could not get stderr",
187                                                llvm::inconvertibleErrorCode());
188   }
189 
190   lua_pop(m_lua_state, 1);
191   return llvm::Error::success();
192 }
193