ScriptInterpreterLua.cpp
6.59 KB
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
192
193
194
195
196
197
//===-- ScriptInterpreterLua.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 "ScriptInterpreterLua.h"
#include "Lua.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/StringList.h"
#include "lldb/Utility/Timer.h"
#include "llvm/Support/FormatAdapters.h"
using namespace lldb;
using namespace lldb_private;
LLDB_PLUGIN_DEFINE(ScriptInterpreterLua)
class IOHandlerLuaInterpreter : public IOHandlerDelegate,
public IOHandlerEditline {
public:
IOHandlerLuaInterpreter(Debugger &debugger,
ScriptInterpreterLua &script_interpreter)
: IOHandlerEditline(debugger, IOHandler::Type::LuaInterpreter, "lua",
">>> ", "..> ", true, debugger.GetUseColor(), 0,
*this, nullptr),
m_script_interpreter(script_interpreter) {
llvm::cantFail(m_script_interpreter.GetLua().ChangeIO(
debugger.GetOutputFile().GetStream(),
debugger.GetErrorFile().GetStream()));
llvm::cantFail(m_script_interpreter.EnterSession(debugger.GetID()));
}
~IOHandlerLuaInterpreter() override {
llvm::cantFail(m_script_interpreter.LeaveSession());
}
void IOHandlerInputComplete(IOHandler &io_handler,
std::string &data) override {
if (llvm::StringRef(data).rtrim() == "quit") {
io_handler.SetIsDone(true);
return;
}
if (llvm::Error error = m_script_interpreter.GetLua().Run(data)) {
*GetOutputStreamFileSP() << llvm::toString(std::move(error));
}
}
private:
ScriptInterpreterLua &m_script_interpreter;
};
ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger)
: ScriptInterpreter(debugger, eScriptLanguageLua),
m_lua(std::make_unique<Lua>()) {}
ScriptInterpreterLua::~ScriptInterpreterLua() {}
bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command,
CommandReturnObject *result,
const ExecuteScriptOptions &options) {
if (command.empty()) {
if (result)
result->AppendError("empty command passed to lua\n");
return false;
}
llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
io_redirect_or_error = ScriptInterpreterIORedirect::Create(
options.GetEnableIO(), m_debugger, result);
if (!io_redirect_or_error) {
if (result)
result->AppendErrorWithFormatv(
"failed to redirect I/O: {0}\n",
llvm::fmt_consume(io_redirect_or_error.takeError()));
else
llvm::consumeError(io_redirect_or_error.takeError());
return false;
}
ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error;
if (llvm::Error e =
m_lua->ChangeIO(io_redirect.GetOutputFile()->GetStream(),
io_redirect.GetErrorFile()->GetStream())) {
result->AppendErrorWithFormatv("lua failed to redirect I/O: {0}\n",
llvm::toString(std::move(e)));
return false;
}
if (llvm::Error e = m_lua->Run(command)) {
result->AppendErrorWithFormatv(
"lua failed attempting to evaluate '{0}': {1}\n", command,
llvm::toString(std::move(e)));
return false;
}
io_redirect.Flush();
return true;
}
void ScriptInterpreterLua::ExecuteInterpreterLoop() {
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
// At the moment, the only time the debugger does not have an input file
// handle is when this is called directly from lua, in which case it is
// both dangerous and unnecessary (not to mention confusing) to try to embed
// a running interpreter loop inside the already running lua interpreter
// loop, so we won't do it.
if (!m_debugger.GetInputFile().IsValid())
return;
IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(m_debugger, *this));
m_debugger.RunIOHandlerAsync(io_handler_sp);
}
bool ScriptInterpreterLua::LoadScriptingModule(
const char *filename, bool init_session, lldb_private::Status &error,
StructuredData::ObjectSP *module_sp) {
FileSystem::Instance().Collect(filename);
if (llvm::Error e = m_lua->LoadModule(filename)) {
error.SetErrorStringWithFormatv("lua failed to import '{0}': {1}\n",
filename, llvm::toString(std::move(e)));
return false;
}
return true;
}
void ScriptInterpreterLua::Initialize() {
static llvm::once_flag g_once_flag;
llvm::call_once(g_once_flag, []() {
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(),
lldb::eScriptLanguageLua, CreateInstance);
});
}
void ScriptInterpreterLua::Terminate() {}
llvm::Error ScriptInterpreterLua::EnterSession(user_id_t debugger_id) {
if (m_session_is_active)
return llvm::Error::success();
const char *fmt_str =
"lldb.debugger = lldb.SBDebugger.FindDebuggerWithID({0}); "
"lldb.target = lldb.debugger:GetSelectedTarget(); "
"lldb.process = lldb.target:GetProcess(); "
"lldb.thread = lldb.process:GetSelectedThread(); "
"lldb.frame = lldb.thread:GetSelectedFrame()";
return m_lua->Run(llvm::formatv(fmt_str, debugger_id).str());
}
llvm::Error ScriptInterpreterLua::LeaveSession() {
if (!m_session_is_active)
return llvm::Error::success();
m_session_is_active = false;
llvm::StringRef str = "lldb.debugger = nil; "
"lldb.target = nil; "
"lldb.process = nil; "
"lldb.thread = nil; "
"lldb.frame = nil";
return m_lua->Run(str);
}
lldb::ScriptInterpreterSP
ScriptInterpreterLua::CreateInstance(Debugger &debugger) {
return std::make_shared<ScriptInterpreterLua>(debugger);
}
lldb_private::ConstString ScriptInterpreterLua::GetPluginNameStatic() {
static ConstString g_name("script-lua");
return g_name;
}
const char *ScriptInterpreterLua::GetPluginDescriptionStatic() {
return "Lua script interpreter";
}
lldb_private::ConstString ScriptInterpreterLua::GetPluginName() {
return GetPluginNameStatic();
}
uint32_t ScriptInterpreterLua::GetPluginVersion() { return 1; }
Lua &ScriptInterpreterLua::GetLua() { return *m_lua; }