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