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