LLDB mainline
LuaState.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "LuaState.h"
10#include "SWIGLuaBridge.h"
13#include "llvm/Support/Error.h"
14#include "llvm/Support/FormatVariadic.h"
15
16using namespace lldb_private;
17using namespace lldb;
18
19static 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
34LuaState::LuaState() : m_lua_state(luaL_newstate()) {
35 assert(m_lua_state);
36 luaL_openlibs(m_lua_state);
38 lua_pushcfunction(m_lua_state, lldb_print);
39 lua_setglobal(m_lua_state, "print");
40}
41
43 assert(m_lua_state);
44 lua_close(m_lua_state);
45}
46
47llvm::Error LuaState::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
63 const char *body) {
64 lua_pushlightuserdata(m_lua_state, baton);
65 const char *fmt_str = "return function(frame, bp_loc, ...) {0} end";
66 std::string func_str = llvm::formatv(fmt_str, body).str();
67 if (luaL_dostring(m_lua_state, func_str.c_str()) != LUA_OK) {
68 llvm::Error e = llvm::make_error<llvm::StringError>(
69 llvm::formatv("{0}", lua_tostring(m_lua_state, -1)),
70 llvm::inconvertibleErrorCode());
71 // Pop error message from the stack.
72 lua_pop(m_lua_state, 2);
73 return e;
74 }
75 lua_settable(m_lua_state, LUA_REGISTRYINDEX);
76 return llvm::Error::success();
77}
78
79llvm::Expected<bool>
82 StructuredData::ObjectSP extra_args_sp) {
83
84 lua_pushlightuserdata(m_lua_state, baton);
85 lua_gettable(m_lua_state, LUA_REGISTRYINDEX);
86 StructuredDataImpl extra_args_impl(std::move(extra_args_sp));
88 m_lua_state, stop_frame_sp, bp_loc_sp, extra_args_impl);
89}
90
92 const char *body) {
93 lua_pushlightuserdata(m_lua_state, baton);
94 const char *fmt_str = "return function(frame, wp, ...) {0} end";
95 std::string func_str = llvm::formatv(fmt_str, body).str();
96 if (luaL_dostring(m_lua_state, func_str.c_str()) != LUA_OK) {
97 llvm::Error e = llvm::make_error<llvm::StringError>(
98 llvm::formatv("{0}", lua_tostring(m_lua_state, -1)),
99 llvm::inconvertibleErrorCode());
100 // Pop error message from the stack.
101 lua_pop(m_lua_state, 2);
102 return e;
103 }
104 lua_settable(m_lua_state, LUA_REGISTRYINDEX);
105 return llvm::Error::success();
106}
107
108llvm::Expected<bool>
110 lldb::WatchpointSP wp_sp) {
111
112 lua_pushlightuserdata(m_lua_state, baton);
113 lua_gettable(m_lua_state, LUA_REGISTRYINDEX);
115 m_lua_state, stop_frame_sp, wp_sp);
116}
117
118llvm::Error LuaState::CheckSyntax(llvm::StringRef buffer) {
119 int error =
120 luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer");
121 if (error == LUA_OK) {
122 // Pop buffer
123 lua_pop(m_lua_state, 1);
124 return llvm::Error::success();
125 }
126
127 llvm::Error e = llvm::make_error<llvm::StringError>(
128 llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
129 llvm::inconvertibleErrorCode());
130 // Pop error message from the stack.
131 lua_pop(m_lua_state, 1);
132 return e;
133}
134
135llvm::Error LuaState::LoadModule(llvm::StringRef filename) {
136 const FileSpec file(filename);
137 if (!FileSystem::Instance().Exists(file)) {
138 return llvm::make_error<llvm::StringError>("invalid path",
139 llvm::inconvertibleErrorCode());
140 }
141
142 if (file.GetFileNameExtension() != ".lua") {
143 return llvm::make_error<llvm::StringError>("invalid extension",
144 llvm::inconvertibleErrorCode());
145 }
146
147 int error = luaL_loadfile(m_lua_state, filename.data()) ||
148 lua_pcall(m_lua_state, 0, 1, 0);
149 if (error != LUA_OK) {
150 llvm::Error e = llvm::make_error<llvm::StringError>(
151 llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
152 llvm::inconvertibleErrorCode());
153 // Pop error message from the stack.
154 lua_pop(m_lua_state, 1);
155 return e;
156 }
157
158 ConstString module_name = file.GetFileNameStrippingExtension();
159 lua_setglobal(m_lua_state, module_name.GetCString());
160 return llvm::Error::success();
161}
162
163llvm::Error LuaState::ChangeIO(FILE *out, FILE *err) {
164 assert(out != nullptr);
165 assert(err != nullptr);
166
167 lua_getglobal(m_lua_state, "io");
168
169 lua_getfield(m_lua_state, -1, "stdout");
170 if (luaL_Stream *s = static_cast<luaL_Stream *>(
171 luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
172 s->f = out;
173 lua_pop(m_lua_state, 1);
174 } else {
175 lua_pop(m_lua_state, 2);
176 return llvm::make_error<llvm::StringError>("could not get stdout",
177 llvm::inconvertibleErrorCode());
178 }
179
180 lua_getfield(m_lua_state, -1, "stderr");
181 if (luaL_Stream *s = static_cast<luaL_Stream *>(
182 luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
183 s->f = out;
184 lua_pop(m_lua_state, 1);
185 } else {
186 lua_pop(m_lua_state, 2);
187 return llvm::make_error<llvm::StringError>("could not get stderr",
188 llvm::inconvertibleErrorCode());
189 }
190
191 lua_pop(m_lua_state, 1);
192 return llvm::Error::success();
193}
static llvm::raw_ostream & error(Stream &strm)
static int lldb_print(lua_State *L)
Definition LuaState.cpp:19
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
A file utility class.
Definition FileSpec.h:57
ConstString GetFileNameStrippingExtension() const
Return the filename without the extension part.
Definition FileSpec.cpp:414
llvm::StringRef GetFileNameExtension() const
Extract the extension of the file.
Definition FileSpec.cpp:410
static FileSystem & Instance()
lua_State * m_lua_state
Definition LuaState.h:45
llvm::Error LoadModule(llvm::StringRef filename)
Definition LuaState.cpp:135
llvm::Expected< bool > CallBreakpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp, lldb::BreakpointLocationSP bp_loc_sp, StructuredData::ObjectSP extra_args_sp)
Definition LuaState.cpp:80
llvm::Expected< bool > CallWatchpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp)
Definition LuaState.cpp:109
llvm::Error RegisterWatchpointCallback(void *baton, const char *body)
Definition LuaState.cpp:91
llvm::Error CheckSyntax(llvm::StringRef buffer)
Definition LuaState.cpp:118
llvm::Error Run(llvm::StringRef buffer)
Definition LuaState.cpp:47
llvm::Error ChangeIO(FILE *out, FILE *err)
Definition LuaState.cpp:163
llvm::Error RegisterBreakpointCallback(void *baton, const char *body)
Definition LuaState.cpp:62
std::shared_ptr< Object > ObjectSP
static llvm::Expected< bool > LLDBSwigLuaWatchpointCallbackFunction(lua_State *L, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp)
static llvm::Expected< bool > LLDBSwigLuaBreakpointCallbackFunction(lua_State *L, lldb::StackFrameSP stop_frame_sp, lldb::BreakpointLocationSP bp_loc_sp, const StructuredDataImpl &extra_args_impl)
A class that represents a running process on the host machine.
int luaopen_lldb(lua_State *L)
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
std::shared_ptr< lldb_private::Watchpoint > WatchpointSP