Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebrand the Lua debugger #603

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1497,19 +1497,19 @@ void ldbRedis(lua_State *lua, sds *argv, int argc) {
/* Increase the Lua stack if needed to make sure there is enough room
* to push 'argc + 1' elements to the stack. On failure, return error.
* Notice that we need, in worst case, 'argc + 1' elements because we push all the arguments
* given by the user (without the first argument) and we also push the 'redis' global table and
* 'redis.call' function so:
* (1 (redis table)) + (1 (redis.call function)) + (argc - 1 (all arguments without the first)) = argc + 1*/
* given by the user (without the first argument) and we also push the 'server' global table and
* 'server.call' function so:
* (1 (server table)) + (1 (server.call function)) + (argc - 1 (all arguments without the first)) = argc + 1*/
ldbLogRedisReply("max lua stack reached");
return;
}

lua_getglobal(lua, "redis");
lua_getglobal(lua, "server");
lua_pushstring(lua, "call");
lua_gettable(lua, -2); /* Stack: redis, redis.call */
lua_gettable(lua, -2); /* Stack: server, server.call */
for (j = 1; j < argc; j++) lua_pushlstring(lua, argv[j], sdslen(argv[j]));
ldb.step = 1; /* Force redis.call() to log. */
lua_pcall(lua, argc - 1, 1, 0); /* Stack: redis, result */
ldb.step = 1; /* Force server.call() to log. */
lua_pcall(lua, argc - 1, 1, 0); /* Stack: server, result */
ldb.step = 0; /* Disable logging. */
lua_pop(lua, 2); /* Discard the result and clean the stack. */
}
Expand Down Expand Up @@ -1612,15 +1612,15 @@ int ldbRepl(lua_State *lua) {
ldbLog(sdsnew("[b]reak 0 Remove all breakpoints."));
ldbLog(sdsnew("[t]race Show a backtrace."));
ldbLog(sdsnew("[e]val <code> Execute some Lua code (in a different callframe)."));
ldbLog(sdsnew("[r]edis <cmd> Execute a Redis command."));
ldbLog(sdsnew("[m]axlen [len] Trim logged Redis replies and Lua var dumps to len."));
ldbLog(sdsnew("[v]alkey <cmd> Execute a command."));
ldbLog(sdsnew("[m]axlen [len] Trim logged replies and Lua var dumps to len."));
ldbLog(sdsnew(" Specifying zero as <len> means unlimited."));
ldbLog(sdsnew("[a]bort Stop the execution of the script. In sync"));
ldbLog(sdsnew(" mode dataset changes will be retained."));
ldbLog(sdsnew(""));
ldbLog(sdsnew("Debugger functions you can call from Lua scripts:"));
ldbLog(sdsnew("redis.debug() Produce logs in the debugger console."));
ldbLog(sdsnew("redis.breakpoint() Stop execution like if there was a breakpoint in the"));
ldbLog(sdsnew("server.debug() Produce logs in the debugger console."));
ldbLog(sdsnew("server.breakpoint() Stop execution like if there was a breakpoint in the"));
ldbLog(sdsnew(" next line of code."));
ldbSendLogs();
} else if (!strcasecmp(argv[0], "s") || !strcasecmp(argv[0], "step") || !strcasecmp(argv[0], "n") ||
Expand All @@ -1644,8 +1644,12 @@ int ldbRepl(lua_State *lua) {
} else if (!strcasecmp(argv[0], "a") || !strcasecmp(argv[0], "abort")) {
luaPushError(lua, "script aborted for user request");
luaError(lua);
} else if (argc > 1 && (!strcasecmp(argv[0], "r") || !strcasecmp(argv[0], REDIS_API_NAME) ||
} else if (argc > 1 && ((!strcasecmp(argv[0], "r") || !strcasecmp(argv[0], "redis")) ||
(!strcasecmp(argv[0], "v") || !strcasecmp(argv[0], "valkey")) ||
!strcasecmp(argv[0], SERVER_API_NAME))) {
/* [r]redis or [v]alkey calls a command. We accept "server" too, but
* not "s" because that's "step". Neither can we use [c]all because
* "c" is continue. */
ldbRedis(lua, argv, argc);
ldbSendLogs();
} else if ((!strcasecmp(argv[0], "p") || !strcasecmp(argv[0], "print"))) {
Expand All @@ -1667,7 +1671,7 @@ int ldbRepl(lua_State *lua) {
ldbList(1, 1000000);
ldbSendLogs();
} else {
ldbLog(sdsnew("<error> Unknown Redis Lua debugger command or "
ldbLog(sdsnew("<error> Unknown Lua debugger command or "
"wrong number of arguments."));
ldbSendLogs();
}
Expand Down Expand Up @@ -1711,7 +1715,7 @@ void luaLdbLineHook(lua_State *lua, lua_Debug *ar) {
if (ldb.step || bp) {
char *reason = "step over";
if (bp)
reason = ldb.luabp ? "redis.breakpoint() called" : "break point";
reason = ldb.luabp ? "server.breakpoint() called" : "break point";
else if (timeout)
reason = "timeout reached, infinite loop?";
ldb.step = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/script_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ static int luaRedisGenericCommand(lua_State *lua, int raise_error) {

/* Log the command if debugging is active. */
if (ldbIsEnabled()) {
sds cmdlog = sdsnew("<redis>");
sds cmdlog = sdsnew("<command>");
for (j = 0; j < c->argc; j++) {
if (j == 10) {
cmdlog = sdscatprintf(cmdlog, " ... (%d more)", c->argc - j - 1);
Expand Down
1 change: 1 addition & 0 deletions src/valkey-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,7 @@ sds sdsCatColorizedLdbReply(sds o, char *s, size_t len) {
char *color = "white";

if (strstr(s, "<debug>")) color = "bold";
if (strstr(s, "<command>")) color = "green";
if (strstr(s, "<redis>")) color = "green";
if (strstr(s, "<reply>")) color = "cyan";
if (strstr(s, "<error>")) color = "red";
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/scripting.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -1525,13 +1525,13 @@ start_server {tags {"scripting needs:debug external:skip"}} {
r script debug sync
r eval {return 'hello'} 0
catch {r 'hello\0world'} e
assert_match {*Unknown Redis Lua debugger command*} $e
assert_match {*Unknown Lua debugger command*} $e
catch {r 'hello\0'} e
assert_match {*Unknown Redis Lua debugger command*} $e
assert_match {*Unknown Lua debugger command*} $e
catch {r '\0hello'} e
assert_match {*Unknown Redis Lua debugger command*} $e
assert_match {*Unknown Lua debugger command*} $e
catch {r '\0hello\0'} e
assert_match {*Unknown Redis Lua debugger command*} $e
assert_match {*Unknown Lua debugger command*} $e
}

test {Test scripting debug lua stack overflow} {
Expand Down
Loading