LLDB  mainline
Lua.cpp
Go to the documentation of this file.
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);
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 
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  StructuredDataImpl extra_args_impl(std::move(extra_args_sp));
87  bp_loc_sp, extra_args_impl);
88 }
89 
90 llvm::Error Lua::RegisterWatchpointCallback(void *baton, const char *body) {
91  lua_pushlightuserdata(m_lua_state, baton);
92  const char *fmt_str = "return function(frame, wp, ...) {0} end";
93  std::string func_str = llvm::formatv(fmt_str, body).str();
94  if (luaL_dostring(m_lua_state, func_str.c_str()) != LUA_OK) {
95  llvm::Error e = llvm::make_error<llvm::StringError>(
96  llvm::formatv("{0}", lua_tostring(m_lua_state, -1)),
97  llvm::inconvertibleErrorCode());
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 
106 llvm::Expected<bool>
107 Lua::CallWatchpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp,
108  lldb::WatchpointSP wp_sp) {
109 
110  lua_pushlightuserdata(m_lua_state, baton);
111  lua_gettable(m_lua_state, LUA_REGISTRYINDEX);
113  wp_sp);
114 }
115 
116 llvm::Error Lua::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 = llvm::make_error<llvm::StringError>(
126  llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
127  llvm::inconvertibleErrorCode());
128  // Pop error message from the stack.
129  lua_pop(m_lua_state, 1);
130  return e;
131 }
132 
133 llvm::Error Lua::LoadModule(llvm::StringRef filename) {
134  FileSpec file(filename);
135  if (!FileSystem::Instance().Exists(file)) {
136  return llvm::make_error<llvm::StringError>("invalid path",
137  llvm::inconvertibleErrorCode());
138  }
139 
140  ConstString module_extension = file.GetFileNameExtension();
141  if (module_extension != ".lua") {
142  return llvm::make_error<llvm::StringError>("invalid extension",
143  llvm::inconvertibleErrorCode());
144  }
145 
146  int error = luaL_loadfile(m_lua_state, filename.data()) ||
147  lua_pcall(m_lua_state, 0, 1, 0);
148  if (error != LUA_OK) {
149  llvm::Error e = llvm::make_error<llvm::StringError>(
150  llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
151  llvm::inconvertibleErrorCode());
152  // Pop error message from the stack.
153  lua_pop(m_lua_state, 1);
154  return e;
155  }
156 
157  ConstString module_name = file.GetFileNameStrippingExtension();
158  lua_setglobal(m_lua_state, module_name.GetCString());
159  return llvm::Error::success();
160 }
161 
162 llvm::Error Lua::ChangeIO(FILE *out, FILE *err) {
163  assert(out != nullptr);
164  assert(err != nullptr);
165 
166  lua_getglobal(m_lua_state, "io");
167 
168  lua_getfield(m_lua_state, -1, "stdout");
169  if (luaL_Stream *s = static_cast<luaL_Stream *>(
170  luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
171  s->f = out;
172  lua_pop(m_lua_state, 1);
173  } else {
174  lua_pop(m_lua_state, 2);
175  return llvm::make_error<llvm::StringError>("could not get stdout",
176  llvm::inconvertibleErrorCode());
177  }
178 
179  lua_getfield(m_lua_state, -1, "stderr");
180  if (luaL_Stream *s = static_cast<luaL_Stream *>(
181  luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) {
182  s->f = out;
183  lua_pop(m_lua_state, 1);
184  } else {
185  lua_pop(m_lua_state, 2);
186  return llvm::make_error<llvm::StringError>("could not get stderr",
187  llvm::inconvertibleErrorCode());
188  }
189 
190  lua_pop(m_lua_state, 1);
191  return llvm::Error::success();
192 }
FileSystem.h
Lua.h
lldb_private::Lua::CallWatchpointCallback
llvm::Expected< bool > CallWatchpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp)
Definition: Lua.cpp:107
lldb_private::FileSpec
Definition: FileSpec.h:55
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Lua::CheckSyntax
llvm::Error CheckSyntax(llvm::StringRef buffer)
Definition: Lua.cpp:116
lldb_private::Lua::CallBreakpointCallback
llvm::Expected< bool > CallBreakpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp, lldb::BreakpointLocationSP bp_loc_sp, StructuredData::ObjectSP extra_args_sp)
Definition: Lua.cpp:79
lldb_private::Lua::ChangeIO
llvm::Error ChangeIO(FILE *out, FILE *err)
Definition: Lua.cpp:162
lldb_print
static int lldb_print(lua_State *L)
Definition: Lua.cpp:19
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
lldb_private::Lua::~Lua
~Lua()
Definition: Lua.cpp:42
lldb_private::ConstString
Definition: ConstString.h:39
lldb_private::Lua::Run
llvm::Error Run(llvm::StringRef buffer)
Definition: Lua.cpp:47
lldb_private::StructuredDataImpl
Definition: StructuredDataImpl.h:26
SWIGLuaBridge.h
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::Lua::RegisterBreakpointCallback
llvm::Error RegisterBreakpointCallback(void *baton, const char *body)
Definition: Lua.cpp:62
lldb_private::luaopen_lldb
int luaopen_lldb(lua_State *L)
lldb_private::Lua::m_lua_state
lua_State * m_lua_state
Definition: Lua.h:49
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:215
lldb_private::LLDBSwigLuaBreakpointCallbackFunction
llvm::Expected< bool > LLDBSwigLuaBreakpointCallbackFunction(lua_State *L, lldb::StackFrameSP stop_frame_sp, lldb::BreakpointLocationSP bp_loc_sp, const StructuredDataImpl &extra_args_impl)
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:46
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:34
lldb_private::LLDBSwigLuaWatchpointCallbackFunction
llvm::Expected< bool > LLDBSwigLuaWatchpointCallbackFunction(lua_State *L, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp)
FileSpec.h
lldb_private::Lua::RegisterWatchpointCallback
llvm::Error RegisterWatchpointCallback(void *baton, const char *body)
Definition: Lua.cpp:90
lldb_private::Lua::LoadModule
llvm::Error LoadModule(llvm::StringRef filename)
Definition: Lua.cpp:133
lldb_private::Lua::Lua
Lua()
Definition: Lua.cpp:34
lldb
Definition: SBAddress.h:15
lldb_private::FileSpec::GetFileNameExtension
ConstString GetFileNameExtension() const
Extract the extension of the file.
Definition: FileSpec.cpp:400
lldb_private::FileSpec::GetFileNameStrippingExtension
ConstString GetFileNameStrippingExtension() const
Return the filename without the extension part.
Definition: FileSpec.cpp:405