Skip to content

Commit e81ba28

Browse files
ttyS0tammela
authored andcommitted
[lldb/lua] Add scripted watchpoints for Lua
Add support for Lua scripted watchpoints, with basic tests. Differential Revision: https://reviews.llvm.org/D105034
1 parent 3ebfeb2 commit e81ba28

File tree

8 files changed

+194
-8
lines changed

8 files changed

+194
-8
lines changed

lldb/bindings/lua/lua-swigsafecast.swig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ PushSBClass (lua_State* L, lldb::SBBreakpointLocation* breakpoint_location_sb)
1414
SWIG_NewPointerObj(L, breakpoint_location_sb, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
1515
}
1616

17+
void
18+
PushSBClass (lua_State* L, lldb::SBWatchpoint* watchpoint_sb)
19+
{
20+
SWIG_NewPointerObj(L, watchpoint_sb, SWIGTYPE_p_lldb__SBWatchpoint, 0);
21+
}
22+
1723
void
1824
PushSBClass (lua_State* L, lldb::SBStructuredData* structured_data_sb)
1925
{

lldb/bindings/lua/lua-wrapper.swig

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,40 @@ LLDBSwigLuaBreakpointCallbackFunction
5353
return stop;
5454
}
5555

56+
// This function is called from Lua::CallWatchpointCallback
57+
SWIGEXPORT llvm::Expected<bool>
58+
LLDBSwigLuaWatchpointCallbackFunction
59+
(
60+
lua_State *L,
61+
lldb::StackFrameSP stop_frame_sp,
62+
lldb::WatchpointSP wp_sp
63+
)
64+
{
65+
lldb::SBFrame sb_frame(stop_frame_sp);
66+
lldb::SBWatchpoint sb_wp(wp_sp);
67+
int nargs = 2;
68+
69+
// Push the Lua wrappers
70+
PushSBClass(L, &sb_frame);
71+
PushSBClass(L, &sb_wp);
72+
73+
// Call into the Lua callback passing 'sb_frame' and 'sb_wp'.
74+
// Expects a boolean return.
75+
if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
76+
llvm::Error E = llvm::make_error<llvm::StringError>(
77+
llvm::formatv("{0}\n", lua_tostring(L, -1)),
78+
llvm::inconvertibleErrorCode());
79+
// Pop error message from the stack.
80+
lua_pop(L, 1);
81+
return std::move(E);
82+
}
83+
84+
// Boolean return from the callback
85+
bool stop = lua_toboolean(L, -1);
86+
lua_pop(L, 1);
87+
88+
return stop;
89+
}
90+
5691

5792
%}

lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ extern "C" llvm::Expected<bool> LLDBSwigLuaBreakpointCallbackFunction(
3030
lua_State *L, lldb::StackFrameSP stop_frame_sp,
3131
lldb::BreakpointLocationSP bp_loc_sp, StructuredDataImpl *extra_args_impl);
3232

33+
extern "C" llvm::Expected<bool> LLDBSwigLuaWatchpointCallbackFunction(
34+
lua_State *L, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp);
35+
3336
#if _MSC_VER
3437
#pragma warning (pop)
3538
#endif
@@ -113,6 +116,32 @@ Lua::CallBreakpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp,
113116
bp_loc_sp, extra_args_impl);
114117
}
115118

119+
llvm::Error Lua::RegisterWatchpointCallback(void *baton, const char *body) {
120+
lua_pushlightuserdata(m_lua_state, baton);
121+
const char *fmt_str = "return function(frame, wp, ...) {0} end";
122+
std::string func_str = llvm::formatv(fmt_str, body).str();
123+
if (luaL_dostring(m_lua_state, func_str.c_str()) != LUA_OK) {
124+
llvm::Error e = llvm::make_error<llvm::StringError>(
125+
llvm::formatv("{0}", lua_tostring(m_lua_state, -1)),
126+
llvm::inconvertibleErrorCode());
127+
// Pop error message from the stack.
128+
lua_pop(m_lua_state, 2);
129+
return e;
130+
}
131+
lua_settable(m_lua_state, LUA_REGISTRYINDEX);
132+
return llvm::Error::success();
133+
}
134+
135+
llvm::Expected<bool>
136+
Lua::CallWatchpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp,
137+
lldb::WatchpointSP wp_sp) {
138+
139+
lua_pushlightuserdata(m_lua_state, baton);
140+
lua_gettable(m_lua_state, LUA_REGISTRYINDEX);
141+
return LLDBSwigLuaWatchpointCallbackFunction(m_lua_state, stop_frame_sp,
142+
wp_sp);
143+
}
144+
116145
llvm::Error Lua::CheckSyntax(llvm::StringRef buffer) {
117146
int error =
118147
luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer");

lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class Lua {
3737
CallBreakpointCallback(void *baton, lldb::StackFrameSP stop_frame_sp,
3838
lldb::BreakpointLocationSP bp_loc_sp,
3939
StructuredData::ObjectSP extra_args_sp);
40+
llvm::Error RegisterWatchpointCallback(void *baton, const char *body);
41+
llvm::Expected<bool> CallWatchpointCallback(void *baton,
42+
lldb::StackFrameSP stop_frame_sp,
43+
lldb::WatchpointSP wp_sp);
4044
llvm::Error LoadModule(llvm::StringRef filename);
4145
llvm::Error CheckSyntax(llvm::StringRef buffer);
4246
llvm::Error ChangeIO(FILE *out, FILE *err);

lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ class IOHandlerLuaInterpreter : public IOHandlerDelegate,
5858
const char *instructions = nullptr;
5959
switch (m_active_io_handler) {
6060
case eIOHandlerNone:
61+
break;
6162
case eIOHandlerWatchpoint:
63+
instructions = "Enter your Lua command(s). Type 'quit' to end.\n"
64+
"The commands are compiled as the body of the following "
65+
"Lua function\n"
66+
"function (frame, wp) end\n";
67+
SetPrompt(llvm::StringRef("..> "));
6268
break;
6369
case eIOHandlerBreakpoint:
6470
instructions = "Enter your Lua command(s). Type 'quit' to end.\n"
@@ -78,7 +84,8 @@ class IOHandlerLuaInterpreter : public IOHandlerDelegate,
7884
StringList &lines) override {
7985
size_t last = lines.GetSize() - 1;
8086
if (IsQuitCommand(lines.GetStringAtIndex(last))) {
81-
if (m_active_io_handler == eIOHandlerBreakpoint)
87+
if (m_active_io_handler == eIOHandlerBreakpoint ||
88+
m_active_io_handler == eIOHandlerWatchpoint)
8289
lines.DeleteStringAtIndex(last);
8390
return true;
8491
}
@@ -90,8 +97,9 @@ class IOHandlerLuaInterpreter : public IOHandlerDelegate,
9097
// Lua always errors out to incomplete code with '<eof>'
9198
return error_str.find("<eof>") == std::string::npos;
9299
}
93-
// The breakpoint handler only exits with a explicit 'quit'
94-
return m_active_io_handler != eIOHandlerBreakpoint;
100+
// The breakpoint and watchpoint handler only exits with a explicit 'quit'
101+
return m_active_io_handler != eIOHandlerBreakpoint &&
102+
m_active_io_handler != eIOHandlerWatchpoint;
95103
}
96104

97105
void IOHandlerInputComplete(IOHandler &io_handler,
@@ -109,9 +117,13 @@ class IOHandlerLuaInterpreter : public IOHandlerDelegate,
109117
}
110118
io_handler.SetIsDone(true);
111119
} break;
112-
case eIOHandlerWatchpoint:
120+
case eIOHandlerWatchpoint: {
121+
auto *wp_options =
122+
static_cast<WatchpointOptions *>(io_handler.GetUserData());
123+
m_script_interpreter.SetWatchpointCommandCallback(wp_options,
124+
data.c_str());
113125
io_handler.SetIsDone(true);
114-
break;
126+
} break;
115127
case eIOHandlerNone:
116128
if (IsQuitCommand(data)) {
117129
io_handler.SetIsDone(true);
@@ -276,6 +288,33 @@ bool ScriptInterpreterLua::BreakpointCallbackFunction(
276288
return *BoolOrErr;
277289
}
278290

291+
bool ScriptInterpreterLua::WatchpointCallbackFunction(
292+
void *baton, StoppointCallbackContext *context, user_id_t watch_id) {
293+
assert(context);
294+
295+
ExecutionContext exe_ctx(context->exe_ctx_ref);
296+
Target *target = exe_ctx.GetTargetPtr();
297+
if (target == nullptr)
298+
return true;
299+
300+
StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
301+
WatchpointSP wp_sp = target->GetWatchpointList().FindByID(watch_id);
302+
303+
Debugger &debugger = target->GetDebugger();
304+
ScriptInterpreterLua *lua_interpreter = static_cast<ScriptInterpreterLua *>(
305+
debugger.GetScriptInterpreter(true, eScriptLanguageLua));
306+
Lua &lua = lua_interpreter->GetLua();
307+
308+
llvm::Expected<bool> BoolOrErr =
309+
lua.CallWatchpointCallback(baton, stop_frame_sp, wp_sp);
310+
if (llvm::Error E = BoolOrErr.takeError()) {
311+
debugger.GetErrorStream() << toString(std::move(E));
312+
return true;
313+
}
314+
315+
return *BoolOrErr;
316+
}
317+
279318
void ScriptInterpreterLua::CollectDataForBreakpointCommandCallback(
280319
std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
281320
CommandReturnObject &result) {
@@ -285,6 +324,14 @@ void ScriptInterpreterLua::CollectDataForBreakpointCommandCallback(
285324
m_debugger.RunIOHandlerAsync(io_handler_sp);
286325
}
287326

327+
void ScriptInterpreterLua::CollectDataForWatchpointCommandCallback(
328+
WatchpointOptions *wp_options, CommandReturnObject &result) {
329+
IOHandlerSP io_handler_sp(
330+
new IOHandlerLuaInterpreter(m_debugger, *this, eIOHandlerWatchpoint));
331+
io_handler_sp->SetUserData(wp_options);
332+
m_debugger.RunIOHandlerAsync(io_handler_sp);
333+
}
334+
288335
Status ScriptInterpreterLua::SetBreakpointCommandCallbackFunction(
289336
BreakpointOptions &bp_options, const char *function_name,
290337
StructuredData::ObjectSP extra_args_sp) {
@@ -314,6 +361,26 @@ Status ScriptInterpreterLua::RegisterBreakpointCallback(
314361
return error;
315362
}
316363

364+
void ScriptInterpreterLua::SetWatchpointCommandCallback(
365+
WatchpointOptions *wp_options, const char *command_body_text) {
366+
RegisterWatchpointCallback(wp_options, command_body_text, {});
367+
}
368+
369+
Status ScriptInterpreterLua::RegisterWatchpointCallback(
370+
WatchpointOptions *wp_options, const char *command_body_text,
371+
StructuredData::ObjectSP extra_args_sp) {
372+
Status error;
373+
auto data_up = std::make_unique<WatchpointOptions::CommandData>();
374+
error = m_lua->RegisterWatchpointCallback(data_up.get(), command_body_text);
375+
if (error.Fail())
376+
return error;
377+
auto baton_sp =
378+
std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
379+
wp_options->SetCallback(ScriptInterpreterLua::WatchpointCallbackFunction,
380+
baton_sp);
381+
return error;
382+
}
383+
317384
lldb::ScriptInterpreterSP
318385
ScriptInterpreterLua::CreateInstance(Debugger &debugger) {
319386
return std::make_shared<ScriptInterpreterLua>(debugger);

lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <vector>
1313

14+
#include "lldb/Breakpoint/WatchpointOptions.h"
1415
#include "lldb/Core/StructuredDataImpl.h"
1516
#include "lldb/Interpreter/ScriptInterpreter.h"
1617
#include "lldb/Utility/Status.h"
@@ -63,6 +64,10 @@ class ScriptInterpreterLua : public ScriptInterpreter {
6364
lldb::user_id_t break_id,
6465
lldb::user_id_t break_loc_id);
6566

67+
static bool WatchpointCallbackFunction(void *baton,
68+
StoppointCallbackContext *context,
69+
lldb::user_id_t watch_id);
70+
6671
// PluginInterface protocol
6772
lldb_private::ConstString GetPluginName() override;
6873

@@ -77,9 +82,16 @@ class ScriptInterpreterLua : public ScriptInterpreter {
7782
std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
7883
CommandReturnObject &result) override;
7984

85+
void
86+
CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
87+
CommandReturnObject &result) override;
88+
8089
Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
8190
const char *command_body_text) override;
8291

92+
void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
93+
const char *command_body_text) override;
94+
8395
Status SetBreakpointCommandCallbackFunction(
8496
BreakpointOptions &bp_options, const char *function_name,
8597
StructuredData::ObjectSP extra_args_sp) override;
@@ -91,6 +103,10 @@ class ScriptInterpreterLua : public ScriptInterpreter {
91103
Status RegisterBreakpointCallback(BreakpointOptions &bp_options,
92104
const char *command_body_text,
93105
StructuredData::ObjectSP extra_args_sp);
106+
107+
Status RegisterWatchpointCallback(WatchpointOptions *wp_options,
108+
const char *command_body_text,
109+
StructuredData::ObjectSP extra_args_sp);
94110
};
95111

96112
} // namespace lldb_private
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
# REQUIRES: lua
22
# XFAIL: system-netbsd
3-
# RUN: echo "int main() { return 0; }" | %clang_host -x c - -o %t
3+
# RUN: echo "int main() { int val = 1; val++; return 0; }" | %clang_host -x c - -g -o %t
44
# RUN: %lldb -s %s --script-language lua %t 2>&1 | FileCheck %s
55
b main
66
r
7-
watchpoint set expr 0x0
7+
watchpoint set variable val
88
watchpoint command add -s lua
9-
# CHECK: error: This script interpreter does not support watchpoint callbacks
9+
print("val=" .. tostring(frame:FindVariable("val"):GetValue()))
10+
quit
11+
c
12+
# CHECK: val=1
13+
# CHECK: val=2
14+
# CHECK: Process {{[0-9]+}} exited
15+
r
16+
watchpoint set variable val
17+
watchpoint modify 1 -c "(val == 1)"
18+
watchpoint command add -s lua
19+
print("conditional watchpoint")
20+
wp:SetEnabled(false)
21+
quit
22+
c
23+
# CHECK-COUNT-1: conditional watchpoint
24+
# CHECK-NOT: conditional watchpoint
25+
# CHECK: Process {{[0-9]+}} exited
26+
r
27+
watchpoint set expr 0x00
28+
watchpoint command add -s lua
29+
print("never triggers")
30+
quit
31+
c
32+
# CHECK-NOT: never triggers
33+
# CHECK: Process {{[0-9]+}} exited

lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ extern "C" llvm::Expected<bool> LLDBSwigLuaBreakpointCallbackFunction(
3030
return false;
3131
}
3232

33+
extern "C" llvm::Expected<bool> LLDBSwigLuaWatchpointCallbackFunction(
34+
lua_State *L, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp) {
35+
return false;
36+
}
37+
3338
#if _MSC_VER
3439
#pragma warning (pop)
3540
#endif

0 commit comments

Comments
 (0)