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/ErrorExtras.h"
15#include "llvm/Support/FormatVariadic.h"
16
17using namespace lldb_private;
18using namespace lldb;
19
20static int lldb_print(lua_State *L) {
21 int n = lua_gettop(L);
22 lua_getglobal(L, "io");
23 lua_getfield(L, -1, "stdout");
24 lua_getfield(L, -1, "write");
25 for (int i = 1; i <= n; i++) {
26 lua_pushvalue(L, -1); // write()
27 lua_pushvalue(L, -3); // io.stdout
28 luaL_tolstring(L, i, nullptr);
29 lua_pushstring(L, i != n ? "\t" : "\n");
30 lua_call(L, 3, 0);
31 }
32 return 0;
33}
34
35LuaState::LuaState() : m_lua_state(luaL_newstate()) {
36 assert(m_lua_state);
37 luaL_openlibs(m_lua_state);
39 lua_pushcfunction(m_lua_state, lldb_print);
40 lua_setglobal(m_lua_state, "print");
41}
42
44 assert(m_lua_state);
45 lua_close(m_lua_state);
46}
47
48llvm::Error LuaState::Run(llvm::StringRef buffer) {
49 int error =
50 luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer") ||
51 lua_pcall(m_lua_state, 0, 0, 0);
52 if (error == LUA_OK)
53 return llvm::Error::success();
54
55 llvm::Error e =
56 llvm::createStringErrorV("{0}\n", lua_tostring(m_lua_state, -1));
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 =
69 llvm::createStringErrorV("{0}", lua_tostring(m_lua_state, -1));
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
78llvm::Expected<bool>
81 StructuredData::ObjectSP extra_args_sp) {
82
83 lua_pushlightuserdata(m_lua_state, baton);
84 lua_gettable(m_lua_state, LUA_REGISTRYINDEX);
85 StructuredDataImpl extra_args_impl(std::move(extra_args_sp));
87 m_lua_state, stop_frame_sp, bp_loc_sp, extra_args_impl);
88}
89
91 const char *body) {
92 lua_pushlightuserdata(m_lua_state, baton);
93 const char *fmt_str = "return function(frame, wp, ...) {0} end";
94 std::string func_str = llvm::formatv(fmt_str, body).str();
95 if (luaL_dostring(m_lua_state, func_str.c_str()) != LUA_OK) {
96 llvm::Error e =
97 llvm::createStringErrorV("{0}", lua_tostring(m_lua_state, -1));
98 // Pop error message from the stack.
99 lua_pop(m_lua_state, 2);
100 return e;
101 }
102 lua_settable(m_lua_state, LUA_REGISTRYINDEX);
103 return llvm::Error::success();
104}
105
106llvm::Expected<bool>
108 lldb::WatchpointSP wp_sp) {
109
110 lua_pushlightuserdata(m_lua_state, baton);
111 lua_gettable(m_lua_state, LUA_REGISTRYINDEX);
113 m_lua_state, stop_frame_sp, wp_sp);
114}
115
116llvm::Error LuaState::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 =
126 llvm::createStringErrorV("{0}\n", lua_tostring(m_lua_state, -1));
127 // Pop error message from the stack.
128 lua_pop(m_lua_state, 1);
129 return e;
130}
131
132llvm::Error LuaState::LoadModule(llvm::StringRef filename) {
133 const FileSpec file(filename);
134 if (!FileSystem::Instance().Exists(file)) {
135 return llvm::createStringError("invalid path");
136 }
137
138 if (file.GetFileNameExtension() != ".lua") {
139 return llvm::createStringError("invalid extension");
140 }
141
142 int error = luaL_loadfile(m_lua_state, filename.data()) ||
143 lua_pcall(m_lua_state, 0, 1, 0);
144 if (error != LUA_OK) {
145 llvm::Error e =
146 llvm::createStringErrorV("{0}\n", lua_tostring(m_lua_state, -1));
147 // Pop error message from the stack.
148 lua_pop(m_lua_state, 1);
149 return e;
150 }
151
152 ConstString module_name = file.GetFileNameStrippingExtension();
153 lua_setglobal(m_lua_state, module_name.GetCString());
154 return llvm::Error::success();
155}
156
157llvm::Error LuaState::ChangeIO(FILE *out, FILE *err) {
158 assert(out != nullptr);
159 assert(err != nullptr);
160
161 lua_getglobal(m_lua_state, "io");
162
163 lua_getfield(m_lua_state, -1, "stdout");
164 if (luaL_Stream *s = static_cast<luaL_Stream *>(
165 luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
166 s->f = out;
167 lua_pop(m_lua_state, 1);
168 } else {
169 lua_pop(m_lua_state, 2);
170 return llvm::createStringError("could not get stdout");
171 }
172
173 lua_getfield(m_lua_state, -1, "stderr");
174 if (luaL_Stream *s = static_cast<luaL_Stream *>(
175 luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
176 s->f = out;
177 lua_pop(m_lua_state, 1);
178 } else {
179 lua_pop(m_lua_state, 2);
180 return llvm::createStringError("could not get stderr");
181 }
182
183 lua_pop(m_lua_state, 1);
184 return llvm::Error::success();
185}
static llvm::raw_ostream & error(Stream &strm)
static int lldb_print(lua_State *L)
Definition LuaState.cpp:20
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:132
llvm::Expected< bool > CallBreakpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp, lldb::BreakpointLocationSP bp_loc_sp, StructuredData::ObjectSP extra_args_sp)
Definition LuaState.cpp:79
llvm::Expected< bool > CallWatchpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp)
Definition LuaState.cpp:107
llvm::Error RegisterWatchpointCallback(void *baton, const char *body)
Definition LuaState.cpp:90
llvm::Error CheckSyntax(llvm::StringRef buffer)
Definition LuaState.cpp:116
llvm::Error Run(llvm::StringRef buffer)
Definition LuaState.cpp:48
llvm::Error ChangeIO(FILE *out, FILE *err)
Definition LuaState.cpp:157
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