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}", 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::CheckSyntax(llvm::StringRef buffer) { 109 int error = 110 luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer"); 111 if (error == LUA_OK) { 112 // Pop buffer 113 lua_pop(m_lua_state, 1); 114 return llvm::Error::success(); 115 } 116 117 llvm::Error e = llvm::make_error<llvm::StringError>( 118 llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)), 119 llvm::inconvertibleErrorCode()); 120 // Pop error message from the stack. 121 lua_pop(m_lua_state, 1); 122 return e; 123 } 124 125 llvm::Error Lua::LoadModule(llvm::StringRef filename) { 126 FileSpec file(filename); 127 if (!FileSystem::Instance().Exists(file)) { 128 return llvm::make_error<llvm::StringError>("invalid path", 129 llvm::inconvertibleErrorCode()); 130 } 131 132 ConstString module_extension = file.GetFileNameExtension(); 133 if (module_extension != ".lua") { 134 return llvm::make_error<llvm::StringError>("invalid extension", 135 llvm::inconvertibleErrorCode()); 136 } 137 138 int error = luaL_loadfile(m_lua_state, filename.data()) || 139 lua_pcall(m_lua_state, 0, 1, 0); 140 if (error != LUA_OK) { 141 llvm::Error e = llvm::make_error<llvm::StringError>( 142 llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)), 143 llvm::inconvertibleErrorCode()); 144 // Pop error message from the stack. 145 lua_pop(m_lua_state, 1); 146 return e; 147 } 148 149 ConstString module_name = file.GetFileNameStrippingExtension(); 150 lua_setglobal(m_lua_state, module_name.GetCString()); 151 return llvm::Error::success(); 152 } 153 154 llvm::Error Lua::ChangeIO(FILE *out, FILE *err) { 155 assert(out != nullptr); 156 assert(err != nullptr); 157 158 lua_getglobal(m_lua_state, "io"); 159 160 lua_getfield(m_lua_state, -1, "stdout"); 161 if (luaL_Stream *s = static_cast<luaL_Stream *>( 162 luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) { 163 s->f = out; 164 lua_pop(m_lua_state, 1); 165 } else { 166 lua_pop(m_lua_state, 2); 167 return llvm::make_error<llvm::StringError>("could not get stdout", 168 llvm::inconvertibleErrorCode()); 169 } 170 171 lua_getfield(m_lua_state, -1, "stderr"); 172 if (luaL_Stream *s = static_cast<luaL_Stream *>( 173 luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) { 174 s->f = out; 175 lua_pop(m_lua_state, 1); 176 } else { 177 lua_pop(m_lua_state, 2); 178 return llvm::make_error<llvm::StringError>("could not get stderr", 179 llvm::inconvertibleErrorCode()); 180 } 181 182 lua_pop(m_lua_state, 1); 183 return llvm::Error::success(); 184 } 185