xref: /f-stack/app/redis-5.0.5/src/scripting.c (revision 572c4311)
1 /*
2  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Redis nor the names of its contributors may be used
14  *     to endorse or promote products derived from this software without
15  *     specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "server.h"
31 #include "sha1.h"
32 #include "rand.h"
33 #include "cluster.h"
34 
35 #include <lua.h>
36 #include <lauxlib.h>
37 #include <lualib.h>
38 #include <ctype.h>
39 #include <math.h>
40 
41 char *redisProtocolToLuaType_Int(lua_State *lua, char *reply);
42 char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply);
43 char *redisProtocolToLuaType_Status(lua_State *lua, char *reply);
44 char *redisProtocolToLuaType_Error(lua_State *lua, char *reply);
45 char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply);
46 int redis_math_random (lua_State *L);
47 int redis_math_randomseed (lua_State *L);
48 void ldbInit(void);
49 void ldbDisable(client *c);
50 void ldbEnable(client *c);
51 void evalGenericCommandWithDebugging(client *c, int evalsha);
52 void luaLdbLineHook(lua_State *lua, lua_Debug *ar);
53 void ldbLog(sds entry);
54 void ldbLogRedisReply(char *reply);
55 sds ldbCatStackValue(sds s, lua_State *lua, int idx);
56 
57 /* Debugger shared state is stored inside this global structure. */
58 #define LDB_BREAKPOINTS_MAX 64  /* Max number of breakpoints. */
59 #define LDB_MAX_LEN_DEFAULT 256 /* Default len limit for replies / var dumps. */
60 struct ldbState {
61     int fd;     /* Socket of the debugging client. */
62     int active; /* Are we debugging EVAL right now? */
63     int forked; /* Is this a fork()ed debugging session? */
64     list *logs; /* List of messages to send to the client. */
65     list *traces; /* Messages about Redis commands executed since last stop.*/
66     list *children; /* All forked debugging sessions pids. */
67     int bp[LDB_BREAKPOINTS_MAX]; /* An array of breakpoints line numbers. */
68     int bpcount; /* Number of valid entries inside bp. */
69     int step;   /* Stop at next line ragardless of breakpoints. */
70     int luabp;  /* Stop at next line because redis.breakpoint() was called. */
71     sds *src;   /* Lua script source code split by line. */
72     int lines;  /* Number of lines in 'src'. */
73     int currentline;    /* Current line number. */
74     sds cbuf;   /* Debugger client command buffer. */
75     size_t maxlen;  /* Max var dump / reply length. */
76     int maxlen_hint_sent; /* Did we already hint about "set maxlen"? */
77 } ldb;
78 
79 /* ---------------------------------------------------------------------------
80  * Utility functions.
81  * ------------------------------------------------------------------------- */
82 
83 /* Perform the SHA1 of the input string. We use this both for hashing script
84  * bodies in order to obtain the Lua function name, and in the implementation
85  * of redis.sha1().
86  *
87  * 'digest' should point to a 41 bytes buffer: 40 for SHA1 converted into an
88  * hexadecimal number, plus 1 byte for null term. */
sha1hex(char * digest,char * script,size_t len)89 void sha1hex(char *digest, char *script, size_t len) {
90     SHA1_CTX ctx;
91     unsigned char hash[20];
92     char *cset = "0123456789abcdef";
93     int j;
94 
95     SHA1Init(&ctx);
96     SHA1Update(&ctx,(unsigned char*)script,len);
97     SHA1Final(hash,&ctx);
98 
99     for (j = 0; j < 20; j++) {
100         digest[j*2] = cset[((hash[j]&0xF0)>>4)];
101         digest[j*2+1] = cset[(hash[j]&0xF)];
102     }
103     digest[40] = '\0';
104 }
105 
106 /* ---------------------------------------------------------------------------
107  * Redis reply to Lua type conversion functions.
108  * ------------------------------------------------------------------------- */
109 
110 /* Take a Redis reply in the Redis protocol format and convert it into a
111  * Lua type. Thanks to this function, and the introduction of not connected
112  * clients, it is trivial to implement the redis() lua function.
113  *
114  * Basically we take the arguments, execute the Redis command in the context
115  * of a non connected client, then take the generated reply and convert it
116  * into a suitable Lua type. With this trick the scripting feature does not
117  * need the introduction of a full Redis internals API. The script
118  * is like a normal client that bypasses all the slow I/O paths.
119  *
120  * Note: in this function we do not do any sanity check as the reply is
121  * generated by Redis directly. This allows us to go faster.
122  *
123  * Errors are returned as a table with a single 'err' field set to the
124  * error string.
125  */
126 
redisProtocolToLuaType(lua_State * lua,char * reply)127 char *redisProtocolToLuaType(lua_State *lua, char* reply) {
128     char *p = reply;
129 
130     switch(*p) {
131     case ':': p = redisProtocolToLuaType_Int(lua,reply); break;
132     case '$': p = redisProtocolToLuaType_Bulk(lua,reply); break;
133     case '+': p = redisProtocolToLuaType_Status(lua,reply); break;
134     case '-': p = redisProtocolToLuaType_Error(lua,reply); break;
135     case '*': p = redisProtocolToLuaType_MultiBulk(lua,reply); break;
136     }
137     return p;
138 }
139 
redisProtocolToLuaType_Int(lua_State * lua,char * reply)140 char *redisProtocolToLuaType_Int(lua_State *lua, char *reply) {
141     char *p = strchr(reply+1,'\r');
142     long long value;
143 
144     string2ll(reply+1,p-reply-1,&value);
145     lua_pushnumber(lua,(lua_Number)value);
146     return p+2;
147 }
148 
redisProtocolToLuaType_Bulk(lua_State * lua,char * reply)149 char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply) {
150     char *p = strchr(reply+1,'\r');
151     long long bulklen;
152 
153     string2ll(reply+1,p-reply-1,&bulklen);
154     if (bulklen == -1) {
155         lua_pushboolean(lua,0);
156         return p+2;
157     } else {
158         lua_pushlstring(lua,p+2,bulklen);
159         return p+2+bulklen+2;
160     }
161 }
162 
redisProtocolToLuaType_Status(lua_State * lua,char * reply)163 char *redisProtocolToLuaType_Status(lua_State *lua, char *reply) {
164     char *p = strchr(reply+1,'\r');
165 
166     lua_newtable(lua);
167     lua_pushstring(lua,"ok");
168     lua_pushlstring(lua,reply+1,p-reply-1);
169     lua_settable(lua,-3);
170     return p+2;
171 }
172 
redisProtocolToLuaType_Error(lua_State * lua,char * reply)173 char *redisProtocolToLuaType_Error(lua_State *lua, char *reply) {
174     char *p = strchr(reply+1,'\r');
175 
176     lua_newtable(lua);
177     lua_pushstring(lua,"err");
178     lua_pushlstring(lua,reply+1,p-reply-1);
179     lua_settable(lua,-3);
180     return p+2;
181 }
182 
redisProtocolToLuaType_MultiBulk(lua_State * lua,char * reply)183 char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply) {
184     char *p = strchr(reply+1,'\r');
185     long long mbulklen;
186     int j = 0;
187 
188     string2ll(reply+1,p-reply-1,&mbulklen);
189     p += 2;
190     if (mbulklen == -1) {
191         lua_pushboolean(lua,0);
192         return p;
193     }
194     lua_newtable(lua);
195     for (j = 0; j < mbulklen; j++) {
196         lua_pushnumber(lua,j+1);
197         p = redisProtocolToLuaType(lua,p);
198         lua_settable(lua,-3);
199     }
200     return p;
201 }
202 
203 /* This function is used in order to push an error on the Lua stack in the
204  * format used by redis.pcall to return errors, which is a lua table
205  * with a single "err" field set to the error string. Note that this
206  * table is never a valid reply by proper commands, since the returned
207  * tables are otherwise always indexed by integers, never by strings. */
luaPushError(lua_State * lua,char * error)208 void luaPushError(lua_State *lua, char *error) {
209     lua_Debug dbg;
210 
211     /* If debugging is active and in step mode, log errors resulting from
212      * Redis commands. */
213     if (ldb.active && ldb.step) {
214         ldbLog(sdscatprintf(sdsempty(),"<error> %s",error));
215     }
216 
217     lua_newtable(lua);
218     lua_pushstring(lua,"err");
219 
220     /* Attempt to figure out where this function was called, if possible */
221     if(lua_getstack(lua, 1, &dbg) && lua_getinfo(lua, "nSl", &dbg)) {
222         sds msg = sdscatprintf(sdsempty(), "%s: %d: %s",
223             dbg.source, dbg.currentline, error);
224         lua_pushstring(lua, msg);
225         sdsfree(msg);
226     } else {
227         lua_pushstring(lua, error);
228     }
229     lua_settable(lua,-3);
230 }
231 
232 /* In case the error set into the Lua stack by luaPushError() was generated
233  * by the non-error-trapping version of redis.pcall(), which is redis.call(),
234  * this function will raise the Lua error so that the execution of the
235  * script will be halted. */
luaRaiseError(lua_State * lua)236 int luaRaiseError(lua_State *lua) {
237     lua_pushstring(lua,"err");
238     lua_gettable(lua,-2);
239     return lua_error(lua);
240 }
241 
242 /* Sort the array currently in the stack. We do this to make the output
243  * of commands like KEYS or SMEMBERS something deterministic when called
244  * from Lua (to play well with AOf/replication).
245  *
246  * The array is sorted using table.sort itself, and assuming all the
247  * list elements are strings. */
luaSortArray(lua_State * lua)248 void luaSortArray(lua_State *lua) {
249     /* Initial Stack: array */
250     lua_getglobal(lua,"table");
251     lua_pushstring(lua,"sort");
252     lua_gettable(lua,-2);       /* Stack: array, table, table.sort */
253     lua_pushvalue(lua,-3);      /* Stack: array, table, table.sort, array */
254     if (lua_pcall(lua,1,0,0)) {
255         /* Stack: array, table, error */
256 
257         /* We are not interested in the error, we assume that the problem is
258          * that there are 'false' elements inside the array, so we try
259          * again with a slower function but able to handle this case, that
260          * is: table.sort(table, __redis__compare_helper) */
261         lua_pop(lua,1);             /* Stack: array, table */
262         lua_pushstring(lua,"sort"); /* Stack: array, table, sort */
263         lua_gettable(lua,-2);       /* Stack: array, table, table.sort */
264         lua_pushvalue(lua,-3);      /* Stack: array, table, table.sort, array */
265         lua_getglobal(lua,"__redis__compare_helper");
266         /* Stack: array, table, table.sort, array, __redis__compare_helper */
267         lua_call(lua,2,0);
268     }
269     /* Stack: array (sorted), table */
270     lua_pop(lua,1);             /* Stack: array (sorted) */
271 }
272 
273 /* ---------------------------------------------------------------------------
274  * Lua reply to Redis reply conversion functions.
275  * ------------------------------------------------------------------------- */
276 
luaReplyToRedisReply(client * c,lua_State * lua)277 void luaReplyToRedisReply(client *c, lua_State *lua) {
278     int t = lua_type(lua,-1);
279 
280     switch(t) {
281     case LUA_TSTRING:
282         addReplyBulkCBuffer(c,(char*)lua_tostring(lua,-1),lua_strlen(lua,-1));
283         break;
284     case LUA_TBOOLEAN:
285         addReply(c,lua_toboolean(lua,-1) ? shared.cone : shared.nullbulk);
286         break;
287     case LUA_TNUMBER:
288         addReplyLongLong(c,(long long)lua_tonumber(lua,-1));
289         break;
290     case LUA_TTABLE:
291         /* We need to check if it is an array, an error, or a status reply.
292          * Error are returned as a single element table with 'err' field.
293          * Status replies are returned as single element table with 'ok'
294          * field. */
295         lua_pushstring(lua,"err");
296         lua_gettable(lua,-2);
297         t = lua_type(lua,-1);
298         if (t == LUA_TSTRING) {
299             sds err = sdsnew(lua_tostring(lua,-1));
300             sdsmapchars(err,"\r\n","  ",2);
301             addReplySds(c,sdscatprintf(sdsempty(),"-%s\r\n",err));
302             sdsfree(err);
303             lua_pop(lua,2);
304             return;
305         }
306 
307         lua_pop(lua,1);
308         lua_pushstring(lua,"ok");
309         lua_gettable(lua,-2);
310         t = lua_type(lua,-1);
311         if (t == LUA_TSTRING) {
312             sds ok = sdsnew(lua_tostring(lua,-1));
313             sdsmapchars(ok,"\r\n","  ",2);
314             addReplySds(c,sdscatprintf(sdsempty(),"+%s\r\n",ok));
315             sdsfree(ok);
316             lua_pop(lua,1);
317         } else {
318             void *replylen = addDeferredMultiBulkLength(c);
319             int j = 1, mbulklen = 0;
320 
321             lua_pop(lua,1); /* Discard the 'ok' field value we popped */
322             while(1) {
323                 lua_pushnumber(lua,j++);
324                 lua_gettable(lua,-2);
325                 t = lua_type(lua,-1);
326                 if (t == LUA_TNIL) {
327                     lua_pop(lua,1);
328                     break;
329                 }
330                 luaReplyToRedisReply(c, lua);
331                 mbulklen++;
332             }
333             setDeferredMultiBulkLength(c,replylen,mbulklen);
334         }
335         break;
336     default:
337         addReply(c,shared.nullbulk);
338     }
339     lua_pop(lua,1);
340 }
341 
342 /* ---------------------------------------------------------------------------
343  * Lua redis.* functions implementations.
344  * ------------------------------------------------------------------------- */
345 
346 #define LUA_CMD_OBJCACHE_SIZE 32
347 #define LUA_CMD_OBJCACHE_MAX_LEN 64
luaRedisGenericCommand(lua_State * lua,int raise_error)348 int luaRedisGenericCommand(lua_State *lua, int raise_error) {
349     int j, argc = lua_gettop(lua);
350     struct redisCommand *cmd;
351     client *c = server.lua_client;
352     sds reply;
353 
354     /* Cached across calls. */
355     static robj **argv = NULL;
356     static int argv_size = 0;
357     static robj *cached_objects[LUA_CMD_OBJCACHE_SIZE];
358     static size_t cached_objects_len[LUA_CMD_OBJCACHE_SIZE];
359     static int inuse = 0;   /* Recursive calls detection. */
360 
361     /* Reflect MULTI state */
362     if (server.lua_multi_emitted || (server.lua_caller->flags & CLIENT_MULTI)) {
363         c->flags |= CLIENT_MULTI;
364     } else {
365         c->flags &= ~CLIENT_MULTI;
366     }
367 
368     /* By using Lua debug hooks it is possible to trigger a recursive call
369      * to luaRedisGenericCommand(), which normally should never happen.
370      * To make this function reentrant is futile and makes it slower, but
371      * we should at least detect such a misuse, and abort. */
372     if (inuse) {
373         char *recursion_warning =
374             "luaRedisGenericCommand() recursive call detected. "
375             "Are you doing funny stuff with Lua debug hooks?";
376         serverLog(LL_WARNING,"%s",recursion_warning);
377         luaPushError(lua,recursion_warning);
378         return 1;
379     }
380     inuse++;
381 
382     /* Require at least one argument */
383     if (argc == 0) {
384         luaPushError(lua,
385             "Please specify at least one argument for redis.call()");
386         inuse--;
387         return raise_error ? luaRaiseError(lua) : 1;
388     }
389 
390     /* Build the arguments vector */
391     if (argv_size < argc) {
392         argv = zrealloc(argv,sizeof(robj*)*argc);
393         argv_size = argc;
394     }
395 
396     for (j = 0; j < argc; j++) {
397         char *obj_s;
398         size_t obj_len;
399         char dbuf[64];
400 
401         if (lua_type(lua,j+1) == LUA_TNUMBER) {
402             /* We can't use lua_tolstring() for number -> string conversion
403              * since Lua uses a format specifier that loses precision. */
404             lua_Number num = lua_tonumber(lua,j+1);
405 
406             obj_len = snprintf(dbuf,sizeof(dbuf),"%.17g",(double)num);
407             obj_s = dbuf;
408         } else {
409             obj_s = (char*)lua_tolstring(lua,j+1,&obj_len);
410             if (obj_s == NULL) break; /* Not a string. */
411         }
412 
413         /* Try to use a cached object. */
414         if (j < LUA_CMD_OBJCACHE_SIZE && cached_objects[j] &&
415             cached_objects_len[j] >= obj_len)
416         {
417             sds s = cached_objects[j]->ptr;
418             argv[j] = cached_objects[j];
419             cached_objects[j] = NULL;
420             memcpy(s,obj_s,obj_len+1);
421             sdssetlen(s, obj_len);
422         } else {
423             argv[j] = createStringObject(obj_s, obj_len);
424         }
425     }
426 
427     /* Check if one of the arguments passed by the Lua script
428      * is not a string or an integer (lua_isstring() return true for
429      * integers as well). */
430     if (j != argc) {
431         j--;
432         while (j >= 0) {
433             decrRefCount(argv[j]);
434             j--;
435         }
436         luaPushError(lua,
437             "Lua redis() command arguments must be strings or integers");
438         inuse--;
439         return raise_error ? luaRaiseError(lua) : 1;
440     }
441 
442     /* Setup our fake client for command execution */
443     c->argv = argv;
444     c->argc = argc;
445 
446     /* Process module hooks */
447     moduleCallCommandFilters(c);
448     argv = c->argv;
449     argc = c->argc;
450 
451     /* Log the command if debugging is active. */
452     if (ldb.active && ldb.step) {
453         sds cmdlog = sdsnew("<redis>");
454         for (j = 0; j < c->argc; j++) {
455             if (j == 10) {
456                 cmdlog = sdscatprintf(cmdlog," ... (%d more)",
457                     c->argc-j-1);
458                 break;
459             } else {
460                 cmdlog = sdscatlen(cmdlog," ",1);
461                 cmdlog = sdscatsds(cmdlog,c->argv[j]->ptr);
462             }
463         }
464         ldbLog(cmdlog);
465     }
466 
467     /* Command lookup */
468     cmd = lookupCommand(argv[0]->ptr);
469     if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) ||
470                    (argc < -cmd->arity)))
471     {
472         if (cmd)
473             luaPushError(lua,
474                 "Wrong number of args calling Redis command From Lua script");
475         else
476             luaPushError(lua,"Unknown Redis command called from Lua script");
477         goto cleanup;
478     }
479     c->cmd = c->lastcmd = cmd;
480 
481     /* There are commands that are not allowed inside scripts. */
482     if (cmd->flags & CMD_NOSCRIPT) {
483         luaPushError(lua, "This Redis command is not allowed from scripts");
484         goto cleanup;
485     }
486 
487     /* Write commands are forbidden against read-only slaves, or if a
488      * command marked as non-deterministic was already called in the context
489      * of this script. */
490     if (cmd->flags & CMD_WRITE) {
491         int deny_write_type = writeCommandsDeniedByDiskError();
492         if (server.lua_random_dirty && !server.lua_replicate_commands) {
493             luaPushError(lua,
494                 "Write commands not allowed after non deterministic commands. Call redis.replicate_commands() at the start of your script in order to switch to single commands replication mode.");
495             goto cleanup;
496         } else if (server.masterhost && server.repl_slave_ro &&
497                    !server.loading &&
498                    !(server.lua_caller->flags & CLIENT_MASTER))
499         {
500             luaPushError(lua, shared.roslaveerr->ptr);
501             goto cleanup;
502         } else if (deny_write_type != DISK_ERROR_TYPE_NONE) {
503             if (deny_write_type == DISK_ERROR_TYPE_RDB) {
504                 luaPushError(lua, shared.bgsaveerr->ptr);
505             } else {
506                 sds aof_write_err = sdscatfmt(sdsempty(),
507                     "-MISCONF Errors writing to the AOF file: %s\r\n",
508                     strerror(server.aof_last_write_errno));
509                 luaPushError(lua, aof_write_err);
510                 sdsfree(aof_write_err);
511             }
512             goto cleanup;
513         }
514     }
515 
516     /* If we reached the memory limit configured via maxmemory, commands that
517      * could enlarge the memory usage are not allowed, but only if this is the
518      * first write in the context of this script, otherwise we can't stop
519      * in the middle. */
520     if (server.maxmemory &&             /* Maxmemory is actually enabled. */
521         !server.loading &&              /* Don't care about mem if loading. */
522         !server.masterhost &&           /* Slave must execute the script. */
523         server.lua_write_dirty == 0 &&  /* Script had no side effects so far. */
524         (cmd->flags & CMD_DENYOOM))
525     {
526         if (getMaxmemoryState(NULL,NULL,NULL,NULL) != C_OK) {
527             luaPushError(lua, shared.oomerr->ptr);
528             goto cleanup;
529         }
530     }
531 
532     if (cmd->flags & CMD_RANDOM) server.lua_random_dirty = 1;
533     if (cmd->flags & CMD_WRITE) server.lua_write_dirty = 1;
534 
535     /* If this is a Redis Cluster node, we need to make sure Lua is not
536      * trying to access non-local keys, with the exception of commands
537      * received from our master or when loading the AOF back in memory. */
538     if (server.cluster_enabled && !server.loading &&
539         !(server.lua_caller->flags & CLIENT_MASTER))
540     {
541         /* Duplicate relevant flags in the lua client. */
542         c->flags &= ~(CLIENT_READONLY|CLIENT_ASKING);
543         c->flags |= server.lua_caller->flags & (CLIENT_READONLY|CLIENT_ASKING);
544         if (getNodeByQuery(c,c->cmd,c->argv,c->argc,NULL,NULL) !=
545                            server.cluster->myself)
546         {
547             luaPushError(lua,
548                 "Lua script attempted to access a non local key in a "
549                 "cluster node");
550             goto cleanup;
551         }
552     }
553 
554     /* If we are using single commands replication, we need to wrap what
555      * we propagate into a MULTI/EXEC block, so that it will be atomic like
556      * a Lua script in the context of AOF and slaves. */
557     if (server.lua_replicate_commands &&
558         !server.lua_multi_emitted &&
559         !(server.lua_caller->flags & CLIENT_MULTI) &&
560         server.lua_write_dirty &&
561         server.lua_repl != PROPAGATE_NONE)
562     {
563         execCommandPropagateMulti(server.lua_caller);
564         server.lua_multi_emitted = 1;
565     }
566 
567     /* Run the command */
568     int call_flags = CMD_CALL_SLOWLOG | CMD_CALL_STATS;
569     if (server.lua_replicate_commands) {
570         /* Set flags according to redis.set_repl() settings. */
571         if (server.lua_repl & PROPAGATE_AOF)
572             call_flags |= CMD_CALL_PROPAGATE_AOF;
573         if (server.lua_repl & PROPAGATE_REPL)
574             call_flags |= CMD_CALL_PROPAGATE_REPL;
575     }
576     call(c,call_flags);
577 
578     /* Convert the result of the Redis command into a suitable Lua type.
579      * The first thing we need is to create a single string from the client
580      * output buffers. */
581     if (listLength(c->reply) == 0 && c->bufpos < PROTO_REPLY_CHUNK_BYTES) {
582         /* This is a fast path for the common case of a reply inside the
583          * client static buffer. Don't create an SDS string but just use
584          * the client buffer directly. */
585         c->buf[c->bufpos] = '\0';
586         reply = c->buf;
587         c->bufpos = 0;
588     } else {
589         reply = sdsnewlen(c->buf,c->bufpos);
590         c->bufpos = 0;
591         while(listLength(c->reply)) {
592             clientReplyBlock *o = listNodeValue(listFirst(c->reply));
593 
594             reply = sdscatlen(reply,o->buf,o->used);
595             listDelNode(c->reply,listFirst(c->reply));
596         }
597     }
598     if (raise_error && reply[0] != '-') raise_error = 0;
599     redisProtocolToLuaType(lua,reply);
600 
601     /* If the debugger is active, log the reply from Redis. */
602     if (ldb.active && ldb.step)
603         ldbLogRedisReply(reply);
604 
605     /* Sort the output array if needed, assuming it is a non-null multi bulk
606      * reply as expected. */
607     if ((cmd->flags & CMD_SORT_FOR_SCRIPT) &&
608         (server.lua_replicate_commands == 0) &&
609         (reply[0] == '*' && reply[1] != '-')) {
610             luaSortArray(lua);
611     }
612     if (reply != c->buf) sdsfree(reply);
613     c->reply_bytes = 0;
614 
615 cleanup:
616     /* Clean up. Command code may have changed argv/argc so we use the
617      * argv/argc of the client instead of the local variables. */
618     for (j = 0; j < c->argc; j++) {
619         robj *o = c->argv[j];
620 
621         /* Try to cache the object in the cached_objects array.
622          * The object must be small, SDS-encoded, and with refcount = 1
623          * (we must be the only owner) for us to cache it. */
624         if (j < LUA_CMD_OBJCACHE_SIZE &&
625             o->refcount == 1 &&
626             (o->encoding == OBJ_ENCODING_RAW ||
627              o->encoding == OBJ_ENCODING_EMBSTR) &&
628             sdslen(o->ptr) <= LUA_CMD_OBJCACHE_MAX_LEN)
629         {
630             sds s = o->ptr;
631             if (cached_objects[j]) decrRefCount(cached_objects[j]);
632             cached_objects[j] = o;
633             cached_objects_len[j] = sdsalloc(s);
634         } else {
635             decrRefCount(o);
636         }
637     }
638 
639     if (c->argv != argv) {
640         zfree(c->argv);
641         argv = NULL;
642         argv_size = 0;
643     }
644 
645     if (raise_error) {
646         /* If we are here we should have an error in the stack, in the
647          * form of a table with an "err" field. Extract the string to
648          * return the plain error. */
649         inuse--;
650         return luaRaiseError(lua);
651     }
652     inuse--;
653     return 1;
654 }
655 
656 /* redis.call() */
luaRedisCallCommand(lua_State * lua)657 int luaRedisCallCommand(lua_State *lua) {
658     return luaRedisGenericCommand(lua,1);
659 }
660 
661 /* redis.pcall() */
luaRedisPCallCommand(lua_State * lua)662 int luaRedisPCallCommand(lua_State *lua) {
663     return luaRedisGenericCommand(lua,0);
664 }
665 
666 /* This adds redis.sha1hex(string) to Lua scripts using the same hashing
667  * function used for sha1ing lua scripts. */
luaRedisSha1hexCommand(lua_State * lua)668 int luaRedisSha1hexCommand(lua_State *lua) {
669     int argc = lua_gettop(lua);
670     char digest[41];
671     size_t len;
672     char *s;
673 
674     if (argc != 1) {
675         lua_pushstring(lua, "wrong number of arguments");
676         return lua_error(lua);
677     }
678 
679     s = (char*)lua_tolstring(lua,1,&len);
680     sha1hex(digest,s,len);
681     lua_pushstring(lua,digest);
682     return 1;
683 }
684 
685 /* Returns a table with a single field 'field' set to the string value
686  * passed as argument. This helper function is handy when returning
687  * a Redis Protocol error or status reply from Lua:
688  *
689  * return redis.error_reply("ERR Some Error")
690  * return redis.status_reply("ERR Some Error")
691  */
luaRedisReturnSingleFieldTable(lua_State * lua,char * field)692 int luaRedisReturnSingleFieldTable(lua_State *lua, char *field) {
693     if (lua_gettop(lua) != 1 || lua_type(lua,-1) != LUA_TSTRING) {
694         luaPushError(lua, "wrong number or type of arguments");
695         return 1;
696     }
697 
698     lua_newtable(lua);
699     lua_pushstring(lua, field);
700     lua_pushvalue(lua, -3);
701     lua_settable(lua, -3);
702     return 1;
703 }
704 
705 /* redis.error_reply() */
luaRedisErrorReplyCommand(lua_State * lua)706 int luaRedisErrorReplyCommand(lua_State *lua) {
707     return luaRedisReturnSingleFieldTable(lua,"err");
708 }
709 
710 /* redis.status_reply() */
luaRedisStatusReplyCommand(lua_State * lua)711 int luaRedisStatusReplyCommand(lua_State *lua) {
712     return luaRedisReturnSingleFieldTable(lua,"ok");
713 }
714 
715 /* redis.replicate_commands()
716  *
717  * Turn on single commands replication if the script never called
718  * a write command so far, and returns true. Otherwise if the script
719  * already started to write, returns false and stick to whole scripts
720  * replication, which is our default. */
luaRedisReplicateCommandsCommand(lua_State * lua)721 int luaRedisReplicateCommandsCommand(lua_State *lua) {
722     if (server.lua_write_dirty) {
723         lua_pushboolean(lua,0);
724     } else {
725         server.lua_replicate_commands = 1;
726         /* When we switch to single commands replication, we can provide
727          * different math.random() sequences at every call, which is what
728          * the user normally expects. */
729         redisSrand48(rand());
730         lua_pushboolean(lua,1);
731     }
732     return 1;
733 }
734 
735 /* redis.breakpoint()
736  *
737  * Allows to stop execution during a debuggign session from within
738  * the Lua code implementation, like if a breakpoint was set in the code
739  * immediately after the function. */
luaRedisBreakpointCommand(lua_State * lua)740 int luaRedisBreakpointCommand(lua_State *lua) {
741     if (ldb.active) {
742         ldb.luabp = 1;
743         lua_pushboolean(lua,1);
744     } else {
745         lua_pushboolean(lua,0);
746     }
747     return 1;
748 }
749 
750 /* redis.debug()
751  *
752  * Log a string message into the output console.
753  * Can take multiple arguments that will be separated by commas.
754  * Nothing is returned to the caller. */
luaRedisDebugCommand(lua_State * lua)755 int luaRedisDebugCommand(lua_State *lua) {
756     if (!ldb.active) return 0;
757     int argc = lua_gettop(lua);
758     sds log = sdscatprintf(sdsempty(),"<debug> line %d: ", ldb.currentline);
759     while(argc--) {
760         log = ldbCatStackValue(log,lua,-1 - argc);
761         if (argc != 0) log = sdscatlen(log,", ",2);
762     }
763     ldbLog(log);
764     return 0;
765 }
766 
767 /* redis.set_repl()
768  *
769  * Set the propagation of write commands executed in the context of the
770  * script to on/off for AOF and slaves. */
luaRedisSetReplCommand(lua_State * lua)771 int luaRedisSetReplCommand(lua_State *lua) {
772     int argc = lua_gettop(lua);
773     int flags;
774 
775     if (server.lua_replicate_commands == 0) {
776         lua_pushstring(lua, "You can set the replication behavior only after turning on single commands replication with redis.replicate_commands().");
777         return lua_error(lua);
778     } else if (argc != 1) {
779         lua_pushstring(lua, "redis.set_repl() requires two arguments.");
780         return lua_error(lua);
781     }
782 
783     flags = lua_tonumber(lua,-1);
784     if ((flags & ~(PROPAGATE_AOF|PROPAGATE_REPL)) != 0) {
785         lua_pushstring(lua, "Invalid replication flags. Use REPL_AOF, REPL_REPLICA, REPL_ALL or REPL_NONE.");
786         return lua_error(lua);
787     }
788     server.lua_repl = flags;
789     return 0;
790 }
791 
792 /* redis.log() */
luaLogCommand(lua_State * lua)793 int luaLogCommand(lua_State *lua) {
794     int j, argc = lua_gettop(lua);
795     int level;
796     sds log;
797 
798     if (argc < 2) {
799         lua_pushstring(lua, "redis.log() requires two arguments or more.");
800         return lua_error(lua);
801     } else if (!lua_isnumber(lua,-argc)) {
802         lua_pushstring(lua, "First argument must be a number (log level).");
803         return lua_error(lua);
804     }
805     level = lua_tonumber(lua,-argc);
806     if (level < LL_DEBUG || level > LL_WARNING) {
807         lua_pushstring(lua, "Invalid debug level.");
808         return lua_error(lua);
809     }
810 
811     /* Glue together all the arguments */
812     log = sdsempty();
813     for (j = 1; j < argc; j++) {
814         size_t len;
815         char *s;
816 
817         s = (char*)lua_tolstring(lua,(-argc)+j,&len);
818         if (s) {
819             if (j != 1) log = sdscatlen(log," ",1);
820             log = sdscatlen(log,s,len);
821         }
822     }
823     serverLogRaw(level,log);
824     sdsfree(log);
825     return 0;
826 }
827 
828 /* ---------------------------------------------------------------------------
829  * Lua engine initialization and reset.
830  * ------------------------------------------------------------------------- */
831 
luaLoadLib(lua_State * lua,const char * libname,lua_CFunction luafunc)832 void luaLoadLib(lua_State *lua, const char *libname, lua_CFunction luafunc) {
833   lua_pushcfunction(lua, luafunc);
834   lua_pushstring(lua, libname);
835   lua_call(lua, 1, 0);
836 }
837 
838 LUALIB_API int (luaopen_cjson) (lua_State *L);
839 LUALIB_API int (luaopen_struct) (lua_State *L);
840 LUALIB_API int (luaopen_cmsgpack) (lua_State *L);
841 LUALIB_API int (luaopen_bit) (lua_State *L);
842 
luaLoadLibraries(lua_State * lua)843 void luaLoadLibraries(lua_State *lua) {
844     luaLoadLib(lua, "", luaopen_base);
845     luaLoadLib(lua, LUA_TABLIBNAME, luaopen_table);
846     luaLoadLib(lua, LUA_STRLIBNAME, luaopen_string);
847     luaLoadLib(lua, LUA_MATHLIBNAME, luaopen_math);
848     luaLoadLib(lua, LUA_DBLIBNAME, luaopen_debug);
849     luaLoadLib(lua, "cjson", luaopen_cjson);
850     luaLoadLib(lua, "struct", luaopen_struct);
851     luaLoadLib(lua, "cmsgpack", luaopen_cmsgpack);
852     luaLoadLib(lua, "bit", luaopen_bit);
853 
854 #if 0 /* Stuff that we don't load currently, for sandboxing concerns. */
855     luaLoadLib(lua, LUA_LOADLIBNAME, luaopen_package);
856     luaLoadLib(lua, LUA_OSLIBNAME, luaopen_os);
857 #endif
858 }
859 
860 /* Remove a functions that we don't want to expose to the Redis scripting
861  * environment. */
luaRemoveUnsupportedFunctions(lua_State * lua)862 void luaRemoveUnsupportedFunctions(lua_State *lua) {
863     lua_pushnil(lua);
864     lua_setglobal(lua,"loadfile");
865     lua_pushnil(lua);
866     lua_setglobal(lua,"dofile");
867 }
868 
869 /* This function installs metamethods in the global table _G that prevent
870  * the creation of globals accidentally.
871  *
872  * It should be the last to be called in the scripting engine initialization
873  * sequence, because it may interact with creation of globals. */
scriptingEnableGlobalsProtection(lua_State * lua)874 void scriptingEnableGlobalsProtection(lua_State *lua) {
875     char *s[32];
876     sds code = sdsempty();
877     int j = 0;
878 
879     /* strict.lua from: http://metalua.luaforge.net/src/lib/strict.lua.html.
880      * Modified to be adapted to Redis. */
881     s[j++]="local dbg=debug\n";
882     s[j++]="local mt = {}\n";
883     s[j++]="setmetatable(_G, mt)\n";
884     s[j++]="mt.__newindex = function (t, n, v)\n";
885     s[j++]="  if dbg.getinfo(2) then\n";
886     s[j++]="    local w = dbg.getinfo(2, \"S\").what\n";
887     s[j++]="    if w ~= \"main\" and w ~= \"C\" then\n";
888     s[j++]="      error(\"Script attempted to create global variable '\"..tostring(n)..\"'\", 2)\n";
889     s[j++]="    end\n";
890     s[j++]="  end\n";
891     s[j++]="  rawset(t, n, v)\n";
892     s[j++]="end\n";
893     s[j++]="mt.__index = function (t, n)\n";
894     s[j++]="  if dbg.getinfo(2) and dbg.getinfo(2, \"S\").what ~= \"C\" then\n";
895     s[j++]="    error(\"Script attempted to access nonexistent global variable '\"..tostring(n)..\"'\", 2)\n";
896     s[j++]="  end\n";
897     s[j++]="  return rawget(t, n)\n";
898     s[j++]="end\n";
899     s[j++]="debug = nil\n";
900     s[j++]=NULL;
901 
902     for (j = 0; s[j] != NULL; j++) code = sdscatlen(code,s[j],strlen(s[j]));
903     luaL_loadbuffer(lua,code,sdslen(code),"@enable_strict_lua");
904     lua_pcall(lua,0,0,0);
905     sdsfree(code);
906 }
907 
908 /* Initialize the scripting environment.
909  *
910  * This function is called the first time at server startup with
911  * the 'setup' argument set to 1.
912  *
913  * It can be called again multiple times during the lifetime of the Redis
914  * process, with 'setup' set to 0, and following a scriptingRelease() call,
915  * in order to reset the Lua scripting environment.
916  *
917  * However it is simpler to just call scriptingReset() that does just that. */
scriptingInit(int setup)918 void scriptingInit(int setup) {
919     lua_State *lua = lua_open();
920 
921     if (setup) {
922         server.lua_client = NULL;
923         server.lua_caller = NULL;
924         server.lua_timedout = 0;
925         ldbInit();
926     }
927 
928     luaLoadLibraries(lua);
929     luaRemoveUnsupportedFunctions(lua);
930 
931     /* Initialize a dictionary we use to map SHAs to scripts.
932      * This is useful for replication, as we need to replicate EVALSHA
933      * as EVAL, so we need to remember the associated script. */
934     server.lua_scripts = dictCreate(&shaScriptObjectDictType,NULL);
935     server.lua_scripts_mem = 0;
936 
937     /* Register the redis commands table and fields */
938     lua_newtable(lua);
939 
940     /* redis.call */
941     lua_pushstring(lua,"call");
942     lua_pushcfunction(lua,luaRedisCallCommand);
943     lua_settable(lua,-3);
944 
945     /* redis.pcall */
946     lua_pushstring(lua,"pcall");
947     lua_pushcfunction(lua,luaRedisPCallCommand);
948     lua_settable(lua,-3);
949 
950     /* redis.log and log levels. */
951     lua_pushstring(lua,"log");
952     lua_pushcfunction(lua,luaLogCommand);
953     lua_settable(lua,-3);
954 
955     lua_pushstring(lua,"LOG_DEBUG");
956     lua_pushnumber(lua,LL_DEBUG);
957     lua_settable(lua,-3);
958 
959     lua_pushstring(lua,"LOG_VERBOSE");
960     lua_pushnumber(lua,LL_VERBOSE);
961     lua_settable(lua,-3);
962 
963     lua_pushstring(lua,"LOG_NOTICE");
964     lua_pushnumber(lua,LL_NOTICE);
965     lua_settable(lua,-3);
966 
967     lua_pushstring(lua,"LOG_WARNING");
968     lua_pushnumber(lua,LL_WARNING);
969     lua_settable(lua,-3);
970 
971     /* redis.sha1hex */
972     lua_pushstring(lua, "sha1hex");
973     lua_pushcfunction(lua, luaRedisSha1hexCommand);
974     lua_settable(lua, -3);
975 
976     /* redis.error_reply and redis.status_reply */
977     lua_pushstring(lua, "error_reply");
978     lua_pushcfunction(lua, luaRedisErrorReplyCommand);
979     lua_settable(lua, -3);
980     lua_pushstring(lua, "status_reply");
981     lua_pushcfunction(lua, luaRedisStatusReplyCommand);
982     lua_settable(lua, -3);
983 
984     /* redis.replicate_commands */
985     lua_pushstring(lua, "replicate_commands");
986     lua_pushcfunction(lua, luaRedisReplicateCommandsCommand);
987     lua_settable(lua, -3);
988 
989     /* redis.set_repl and associated flags. */
990     lua_pushstring(lua,"set_repl");
991     lua_pushcfunction(lua,luaRedisSetReplCommand);
992     lua_settable(lua,-3);
993 
994     lua_pushstring(lua,"REPL_NONE");
995     lua_pushnumber(lua,PROPAGATE_NONE);
996     lua_settable(lua,-3);
997 
998     lua_pushstring(lua,"REPL_AOF");
999     lua_pushnumber(lua,PROPAGATE_AOF);
1000     lua_settable(lua,-3);
1001 
1002     lua_pushstring(lua,"REPL_SLAVE");
1003     lua_pushnumber(lua,PROPAGATE_REPL);
1004     lua_settable(lua,-3);
1005 
1006     lua_pushstring(lua,"REPL_REPLICA");
1007     lua_pushnumber(lua,PROPAGATE_REPL);
1008     lua_settable(lua,-3);
1009 
1010     lua_pushstring(lua,"REPL_ALL");
1011     lua_pushnumber(lua,PROPAGATE_AOF|PROPAGATE_REPL);
1012     lua_settable(lua,-3);
1013 
1014     /* redis.breakpoint */
1015     lua_pushstring(lua,"breakpoint");
1016     lua_pushcfunction(lua,luaRedisBreakpointCommand);
1017     lua_settable(lua,-3);
1018 
1019     /* redis.debug */
1020     lua_pushstring(lua,"debug");
1021     lua_pushcfunction(lua,luaRedisDebugCommand);
1022     lua_settable(lua,-3);
1023 
1024     /* Finally set the table as 'redis' global var. */
1025     lua_setglobal(lua,"redis");
1026 
1027     /* Replace math.random and math.randomseed with our implementations. */
1028     lua_getglobal(lua,"math");
1029 
1030     lua_pushstring(lua,"random");
1031     lua_pushcfunction(lua,redis_math_random);
1032     lua_settable(lua,-3);
1033 
1034     lua_pushstring(lua,"randomseed");
1035     lua_pushcfunction(lua,redis_math_randomseed);
1036     lua_settable(lua,-3);
1037 
1038     lua_setglobal(lua,"math");
1039 
1040     /* Add a helper function that we use to sort the multi bulk output of non
1041      * deterministic commands, when containing 'false' elements. */
1042     {
1043         char *compare_func =    "function __redis__compare_helper(a,b)\n"
1044                                 "  if a == false then a = '' end\n"
1045                                 "  if b == false then b = '' end\n"
1046                                 "  return a<b\n"
1047                                 "end\n";
1048         luaL_loadbuffer(lua,compare_func,strlen(compare_func),"@cmp_func_def");
1049         lua_pcall(lua,0,0,0);
1050     }
1051 
1052     /* Add a helper function we use for pcall error reporting.
1053      * Note that when the error is in the C function we want to report the
1054      * information about the caller, that's what makes sense from the point
1055      * of view of the user debugging a script. */
1056     {
1057         char *errh_func =       "local dbg = debug\n"
1058                                 "function __redis__err__handler(err)\n"
1059                                 "  local i = dbg.getinfo(2,'nSl')\n"
1060                                 "  if i and i.what == 'C' then\n"
1061                                 "    i = dbg.getinfo(3,'nSl')\n"
1062                                 "  end\n"
1063                                 "  if i then\n"
1064                                 "    return i.source .. ':' .. i.currentline .. ': ' .. err\n"
1065                                 "  else\n"
1066                                 "    return err\n"
1067                                 "  end\n"
1068                                 "end\n";
1069         luaL_loadbuffer(lua,errh_func,strlen(errh_func),"@err_handler_def");
1070         lua_pcall(lua,0,0,0);
1071     }
1072 
1073     /* Create the (non connected) client that we use to execute Redis commands
1074      * inside the Lua interpreter.
1075      * Note: there is no need to create it again when this function is called
1076      * by scriptingReset(). */
1077     if (server.lua_client == NULL) {
1078         server.lua_client = createClient(-1);
1079         server.lua_client->flags |= CLIENT_LUA;
1080     }
1081 
1082     /* Lua beginners often don't use "local", this is likely to introduce
1083      * subtle bugs in their code. To prevent problems we protect accesses
1084      * to global variables. */
1085     scriptingEnableGlobalsProtection(lua);
1086 
1087     server.lua = lua;
1088 }
1089 
1090 /* Release resources related to Lua scripting.
1091  * This function is used in order to reset the scripting environment. */
scriptingRelease(void)1092 void scriptingRelease(void) {
1093     dictRelease(server.lua_scripts);
1094     server.lua_scripts_mem = 0;
1095     lua_close(server.lua);
1096 }
1097 
scriptingReset(void)1098 void scriptingReset(void) {
1099     scriptingRelease();
1100     scriptingInit(0);
1101 }
1102 
1103 /* Set an array of Redis String Objects as a Lua array (table) stored into a
1104  * global variable. */
luaSetGlobalArray(lua_State * lua,char * var,robj ** elev,int elec)1105 void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) {
1106     int j;
1107 
1108     lua_newtable(lua);
1109     for (j = 0; j < elec; j++) {
1110         lua_pushlstring(lua,(char*)elev[j]->ptr,sdslen(elev[j]->ptr));
1111         lua_rawseti(lua,-2,j+1);
1112     }
1113     lua_setglobal(lua,var);
1114 }
1115 
1116 /* ---------------------------------------------------------------------------
1117  * Redis provided math.random
1118  * ------------------------------------------------------------------------- */
1119 
1120 /* We replace math.random() with our implementation that is not affected
1121  * by specific libc random() implementations and will output the same sequence
1122  * (for the same seed) in every arch. */
1123 
1124 /* The following implementation is the one shipped with Lua itself but with
1125  * rand() replaced by redisLrand48(). */
redis_math_random(lua_State * L)1126 int redis_math_random (lua_State *L) {
1127   /* the `%' avoids the (rare) case of r==1, and is needed also because on
1128      some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
1129   lua_Number r = (lua_Number)(redisLrand48()%REDIS_LRAND48_MAX) /
1130                                 (lua_Number)REDIS_LRAND48_MAX;
1131   switch (lua_gettop(L)) {  /* check number of arguments */
1132     case 0: {  /* no arguments */
1133       lua_pushnumber(L, r);  /* Number between 0 and 1 */
1134       break;
1135     }
1136     case 1: {  /* only upper limit */
1137       int u = luaL_checkint(L, 1);
1138       luaL_argcheck(L, 1<=u, 1, "interval is empty");
1139       lua_pushnumber(L, floor(r*u)+1);  /* int between 1 and `u' */
1140       break;
1141     }
1142     case 2: {  /* lower and upper limits */
1143       int l = luaL_checkint(L, 1);
1144       int u = luaL_checkint(L, 2);
1145       luaL_argcheck(L, l<=u, 2, "interval is empty");
1146       lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */
1147       break;
1148     }
1149     default: return luaL_error(L, "wrong number of arguments");
1150   }
1151   return 1;
1152 }
1153 
redis_math_randomseed(lua_State * L)1154 int redis_math_randomseed (lua_State *L) {
1155   redisSrand48(luaL_checkint(L, 1));
1156   return 0;
1157 }
1158 
1159 /* ---------------------------------------------------------------------------
1160  * EVAL and SCRIPT commands implementation
1161  * ------------------------------------------------------------------------- */
1162 
1163 /* Define a Lua function with the specified body.
1164  * The function name will be generated in the following form:
1165  *
1166  *   f_<hex sha1 sum>
1167  *
1168  * The function increments the reference count of the 'body' object as a
1169  * side effect of a successful call.
1170  *
1171  * On success a pointer to an SDS string representing the function SHA1 of the
1172  * just added function is returned (and will be valid until the next call
1173  * to scriptingReset() function), otherwise NULL is returned.
1174  *
1175  * The function handles the fact of being called with a script that already
1176  * exists, and in such a case, it behaves like in the success case.
1177  *
1178  * If 'c' is not NULL, on error the client is informed with an appropriate
1179  * error describing the nature of the problem and the Lua interpreter error. */
luaCreateFunction(client * c,lua_State * lua,robj * body)1180 sds luaCreateFunction(client *c, lua_State *lua, robj *body) {
1181     char funcname[43];
1182     dictEntry *de;
1183 
1184     funcname[0] = 'f';
1185     funcname[1] = '_';
1186     sha1hex(funcname+2,body->ptr,sdslen(body->ptr));
1187 
1188     sds sha = sdsnewlen(funcname+2,40);
1189     if ((de = dictFind(server.lua_scripts,sha)) != NULL) {
1190         sdsfree(sha);
1191         return dictGetKey(de);
1192     }
1193 
1194     sds funcdef = sdsempty();
1195     funcdef = sdscat(funcdef,"function ");
1196     funcdef = sdscatlen(funcdef,funcname,42);
1197     funcdef = sdscatlen(funcdef,"() ",3);
1198     funcdef = sdscatlen(funcdef,body->ptr,sdslen(body->ptr));
1199     funcdef = sdscatlen(funcdef,"\nend",4);
1200 
1201     if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),"@user_script")) {
1202         if (c != NULL) {
1203             addReplyErrorFormat(c,
1204                 "Error compiling script (new function): %s\n",
1205                 lua_tostring(lua,-1));
1206         }
1207         lua_pop(lua,1);
1208         sdsfree(sha);
1209         sdsfree(funcdef);
1210         return NULL;
1211     }
1212     sdsfree(funcdef);
1213 
1214     if (lua_pcall(lua,0,0,0)) {
1215         if (c != NULL) {
1216             addReplyErrorFormat(c,"Error running script (new function): %s\n",
1217                 lua_tostring(lua,-1));
1218         }
1219         lua_pop(lua,1);
1220         sdsfree(sha);
1221         return NULL;
1222     }
1223 
1224     /* We also save a SHA1 -> Original script map in a dictionary
1225      * so that we can replicate / write in the AOF all the
1226      * EVALSHA commands as EVAL using the original script. */
1227     int retval = dictAdd(server.lua_scripts,sha,body);
1228     serverAssertWithInfo(c ? c : server.lua_client,NULL,retval == DICT_OK);
1229     server.lua_scripts_mem += sdsZmallocSize(sha) + getStringObjectSdsUsedMemory(body);
1230     incrRefCount(body);
1231     return sha;
1232 }
1233 
1234 /* This is the Lua script "count" hook that we use to detect scripts timeout. */
luaMaskCountHook(lua_State * lua,lua_Debug * ar)1235 void luaMaskCountHook(lua_State *lua, lua_Debug *ar) {
1236     long long elapsed = mstime() - server.lua_time_start;
1237     UNUSED(ar);
1238     UNUSED(lua);
1239 
1240     /* Set the timeout condition if not already set and the maximum
1241      * execution time was reached. */
1242     if (elapsed >= server.lua_time_limit && server.lua_timedout == 0) {
1243         serverLog(LL_WARNING,"Lua slow script detected: still in execution after %lld milliseconds. You can try killing the script using the SCRIPT KILL command.",elapsed);
1244         server.lua_timedout = 1;
1245         /* Once the script timeouts we reenter the event loop to permit others
1246          * to call SCRIPT KILL or SHUTDOWN NOSAVE if needed. For this reason
1247          * we need to mask the client executing the script from the event loop.
1248          * If we don't do that the client may disconnect and could no longer be
1249          * here when the EVAL command will return. */
1250         protectClient(server.lua_caller);
1251     }
1252     if (server.lua_timedout) processEventsWhileBlocked();
1253     if (server.lua_kill) {
1254         serverLog(LL_WARNING,"Lua script killed by user with SCRIPT KILL.");
1255         lua_pushstring(lua,"Script killed by user with SCRIPT KILL...");
1256         lua_error(lua);
1257     }
1258 }
1259 
evalGenericCommand(client * c,int evalsha)1260 void evalGenericCommand(client *c, int evalsha) {
1261     lua_State *lua = server.lua;
1262     char funcname[43];
1263     long long numkeys;
1264     long long initial_server_dirty = server.dirty;
1265     int delhook = 0, err;
1266 
1267     /* When we replicate whole scripts, we want the same PRNG sequence at
1268      * every call so that our PRNG is not affected by external state. */
1269     redisSrand48(0);
1270 
1271     /* We set this flag to zero to remember that so far no random command
1272      * was called. This way we can allow the user to call commands like
1273      * SRANDMEMBER or RANDOMKEY from Lua scripts as far as no write command
1274      * is called (otherwise the replication and AOF would end with non
1275      * deterministic sequences).
1276      *
1277      * Thanks to this flag we'll raise an error every time a write command
1278      * is called after a random command was used. */
1279     server.lua_random_dirty = 0;
1280     server.lua_write_dirty = 0;
1281     server.lua_replicate_commands = server.lua_always_replicate_commands;
1282     server.lua_multi_emitted = 0;
1283     server.lua_repl = PROPAGATE_AOF|PROPAGATE_REPL;
1284 
1285     /* Get the number of arguments that are keys */
1286     if (getLongLongFromObjectOrReply(c,c->argv[2],&numkeys,NULL) != C_OK)
1287         return;
1288     if (numkeys > (c->argc - 3)) {
1289         addReplyError(c,"Number of keys can't be greater than number of args");
1290         return;
1291     } else if (numkeys < 0) {
1292         addReplyError(c,"Number of keys can't be negative");
1293         return;
1294     }
1295 
1296     /* We obtain the script SHA1, then check if this function is already
1297      * defined into the Lua state */
1298     funcname[0] = 'f';
1299     funcname[1] = '_';
1300     if (!evalsha) {
1301         /* Hash the code if this is an EVAL call */
1302         sha1hex(funcname+2,c->argv[1]->ptr,sdslen(c->argv[1]->ptr));
1303     } else {
1304         /* We already have the SHA if it is a EVALSHA */
1305         int j;
1306         char *sha = c->argv[1]->ptr;
1307 
1308         /* Convert to lowercase. We don't use tolower since the function
1309          * managed to always show up in the profiler output consuming
1310          * a non trivial amount of time. */
1311         for (j = 0; j < 40; j++)
1312             funcname[j+2] = (sha[j] >= 'A' && sha[j] <= 'Z') ?
1313                 sha[j]+('a'-'A') : sha[j];
1314         funcname[42] = '\0';
1315     }
1316 
1317     /* Push the pcall error handler function on the stack. */
1318     lua_getglobal(lua, "__redis__err__handler");
1319 
1320     /* Try to lookup the Lua function */
1321     lua_getglobal(lua, funcname);
1322     if (lua_isnil(lua,-1)) {
1323         lua_pop(lua,1); /* remove the nil from the stack */
1324         /* Function not defined... let's define it if we have the
1325          * body of the function. If this is an EVALSHA call we can just
1326          * return an error. */
1327         if (evalsha) {
1328             lua_pop(lua,1); /* remove the error handler from the stack. */
1329             addReply(c, shared.noscripterr);
1330             return;
1331         }
1332         if (luaCreateFunction(c,lua,c->argv[1]) == NULL) {
1333             lua_pop(lua,1); /* remove the error handler from the stack. */
1334             /* The error is sent to the client by luaCreateFunction()
1335              * itself when it returns NULL. */
1336             return;
1337         }
1338         /* Now the following is guaranteed to return non nil */
1339         lua_getglobal(lua, funcname);
1340         serverAssert(!lua_isnil(lua,-1));
1341     }
1342 
1343     /* Populate the argv and keys table accordingly to the arguments that
1344      * EVAL received. */
1345     luaSetGlobalArray(lua,"KEYS",c->argv+3,numkeys);
1346     luaSetGlobalArray(lua,"ARGV",c->argv+3+numkeys,c->argc-3-numkeys);
1347 
1348     /* Select the right DB in the context of the Lua client */
1349     selectDb(server.lua_client,c->db->id);
1350 
1351     /* Set a hook in order to be able to stop the script execution if it
1352      * is running for too much time.
1353      * We set the hook only if the time limit is enabled as the hook will
1354      * make the Lua script execution slower.
1355      *
1356      * If we are debugging, we set instead a "line" hook so that the
1357      * debugger is call-back at every line executed by the script. */
1358     server.lua_caller = c;
1359     server.lua_time_start = mstime();
1360     server.lua_kill = 0;
1361     if (server.lua_time_limit > 0 && ldb.active == 0) {
1362         lua_sethook(lua,luaMaskCountHook,LUA_MASKCOUNT,100000);
1363         delhook = 1;
1364     } else if (ldb.active) {
1365         lua_sethook(server.lua,luaLdbLineHook,LUA_MASKLINE|LUA_MASKCOUNT,100000);
1366         delhook = 1;
1367     }
1368 
1369     /* At this point whether this script was never seen before or if it was
1370      * already defined, we can call it. We have zero arguments and expect
1371      * a single return value. */
1372     err = lua_pcall(lua,0,1,-2);
1373 
1374     /* Perform some cleanup that we need to do both on error and success. */
1375     if (delhook) lua_sethook(lua,NULL,0,0); /* Disable hook */
1376     if (server.lua_timedout) {
1377         server.lua_timedout = 0;
1378         /* Restore the client that was protected when the script timeout
1379          * was detected. */
1380         unprotectClient(c);
1381         if (server.masterhost && server.master)
1382             queueClientForReprocessing(server.master);
1383     }
1384     server.lua_caller = NULL;
1385 
1386     /* Call the Lua garbage collector from time to time to avoid a
1387      * full cycle performed by Lua, which adds too latency.
1388      *
1389      * The call is performed every LUA_GC_CYCLE_PERIOD executed commands
1390      * (and for LUA_GC_CYCLE_PERIOD collection steps) because calling it
1391      * for every command uses too much CPU. */
1392     #define LUA_GC_CYCLE_PERIOD 50
1393     {
1394         static long gc_count = 0;
1395 
1396         gc_count++;
1397         if (gc_count == LUA_GC_CYCLE_PERIOD) {
1398             lua_gc(lua,LUA_GCSTEP,LUA_GC_CYCLE_PERIOD);
1399             gc_count = 0;
1400         }
1401     }
1402 
1403     if (err) {
1404         addReplyErrorFormat(c,"Error running script (call to %s): %s\n",
1405             funcname, lua_tostring(lua,-1));
1406         lua_pop(lua,2); /* Consume the Lua reply and remove error handler. */
1407     } else {
1408         /* On success convert the Lua return value into Redis protocol, and
1409          * send it to * the client. */
1410         luaReplyToRedisReply(c,lua); /* Convert and consume the reply. */
1411         lua_pop(lua,1); /* Remove the error handler. */
1412     }
1413 
1414     /* If we are using single commands replication, emit EXEC if there
1415      * was at least a write. */
1416     if (server.lua_replicate_commands) {
1417         preventCommandPropagation(c);
1418         if (server.lua_multi_emitted) {
1419             robj *propargv[1];
1420             propargv[0] = createStringObject("EXEC",4);
1421             alsoPropagate(server.execCommand,c->db->id,propargv,1,
1422                 PROPAGATE_AOF|PROPAGATE_REPL);
1423             decrRefCount(propargv[0]);
1424         }
1425     }
1426 
1427     /* EVALSHA should be propagated to Slave and AOF file as full EVAL, unless
1428      * we are sure that the script was already in the context of all the
1429      * attached slaves *and* the current AOF file if enabled.
1430      *
1431      * To do so we use a cache of SHA1s of scripts that we already propagated
1432      * as full EVAL, that's called the Replication Script Cache.
1433      *
1434      * For repliation, everytime a new slave attaches to the master, we need to
1435      * flush our cache of scripts that can be replicated as EVALSHA, while
1436      * for AOF we need to do so every time we rewrite the AOF file. */
1437     if (evalsha && !server.lua_replicate_commands) {
1438         if (!replicationScriptCacheExists(c->argv[1]->ptr)) {
1439             /* This script is not in our script cache, replicate it as
1440              * EVAL, then add it into the script cache, as from now on
1441              * slaves and AOF know about it. */
1442             robj *script = dictFetchValue(server.lua_scripts,c->argv[1]->ptr);
1443 
1444             replicationScriptCacheAdd(c->argv[1]->ptr);
1445             serverAssertWithInfo(c,NULL,script != NULL);
1446 
1447             /* If the script did not produce any changes in the dataset we want
1448              * just to replicate it as SCRIPT LOAD, otherwise we risk running
1449              * an aborted script on slaves (that may then produce results there)
1450              * or just running a CPU costly read-only script on the slaves. */
1451             if (server.dirty == initial_server_dirty) {
1452                 rewriteClientCommandVector(c,3,
1453                     resetRefCount(createStringObject("SCRIPT",6)),
1454                     resetRefCount(createStringObject("LOAD",4)),
1455                     script);
1456             } else {
1457                 rewriteClientCommandArgument(c,0,
1458                     resetRefCount(createStringObject("EVAL",4)));
1459                 rewriteClientCommandArgument(c,1,script);
1460             }
1461             forceCommandPropagation(c,PROPAGATE_REPL|PROPAGATE_AOF);
1462         }
1463     }
1464 }
1465 
evalCommand(client * c)1466 void evalCommand(client *c) {
1467     if (!(c->flags & CLIENT_LUA_DEBUG))
1468         evalGenericCommand(c,0);
1469     else
1470         evalGenericCommandWithDebugging(c,0);
1471 }
1472 
evalShaCommand(client * c)1473 void evalShaCommand(client *c) {
1474     if (sdslen(c->argv[1]->ptr) != 40) {
1475         /* We know that a match is not possible if the provided SHA is
1476          * not the right length. So we return an error ASAP, this way
1477          * evalGenericCommand() can be implemented without string length
1478          * sanity check */
1479         addReply(c, shared.noscripterr);
1480         return;
1481     }
1482     if (!(c->flags & CLIENT_LUA_DEBUG))
1483         evalGenericCommand(c,1);
1484     else {
1485         addReplyError(c,"Please use EVAL instead of EVALSHA for debugging");
1486         return;
1487     }
1488 }
1489 
scriptCommand(client * c)1490 void scriptCommand(client *c) {
1491     if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) {
1492         const char *help[] = {
1493 "DEBUG (yes|sync|no) -- Set the debug mode for subsequent scripts executed.",
1494 "EXISTS <sha1> [<sha1> ...] -- Return information about the existence of the scripts in the script cache.",
1495 "FLUSH -- Flush the Lua scripts cache. Very dangerous on replicas.",
1496 "KILL -- Kill the currently executing Lua script.",
1497 "LOAD <script> -- Load a script into the scripts cache, without executing it.",
1498 NULL
1499         };
1500         addReplyHelp(c, help);
1501     } else if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"flush")) {
1502         scriptingReset();
1503         addReply(c,shared.ok);
1504         replicationScriptCacheFlush();
1505         server.dirty++; /* Propagating this command is a good idea. */
1506     } else if (c->argc >= 2 && !strcasecmp(c->argv[1]->ptr,"exists")) {
1507         int j;
1508 
1509         addReplyMultiBulkLen(c, c->argc-2);
1510         for (j = 2; j < c->argc; j++) {
1511             if (dictFind(server.lua_scripts,c->argv[j]->ptr))
1512                 addReply(c,shared.cone);
1513             else
1514                 addReply(c,shared.czero);
1515         }
1516     } else if (c->argc == 3 && !strcasecmp(c->argv[1]->ptr,"load")) {
1517         sds sha = luaCreateFunction(c,server.lua,c->argv[2]);
1518         if (sha == NULL) return; /* The error was sent by luaCreateFunction(). */
1519         addReplyBulkCBuffer(c,sha,40);
1520         forceCommandPropagation(c,PROPAGATE_REPL|PROPAGATE_AOF);
1521     } else if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"kill")) {
1522         if (server.lua_caller == NULL) {
1523             addReplySds(c,sdsnew("-NOTBUSY No scripts in execution right now.\r\n"));
1524         } else if (server.lua_caller->flags & CLIENT_MASTER) {
1525             addReplySds(c,sdsnew("-UNKILLABLE The busy script was sent by a master instance in the context of replication and cannot be killed.\r\n"));
1526         } else if (server.lua_write_dirty) {
1527             addReplySds(c,sdsnew("-UNKILLABLE Sorry the script already executed write commands against the dataset. You can either wait the script termination or kill the server in a hard way using the SHUTDOWN NOSAVE command.\r\n"));
1528         } else {
1529             server.lua_kill = 1;
1530             addReply(c,shared.ok);
1531         }
1532     } else if (c->argc == 3 && !strcasecmp(c->argv[1]->ptr,"debug")) {
1533         if (clientHasPendingReplies(c)) {
1534             addReplyError(c,"SCRIPT DEBUG must be called outside a pipeline");
1535             return;
1536         }
1537         if (!strcasecmp(c->argv[2]->ptr,"no")) {
1538             ldbDisable(c);
1539             addReply(c,shared.ok);
1540         } else if (!strcasecmp(c->argv[2]->ptr,"yes")) {
1541             ldbEnable(c);
1542             addReply(c,shared.ok);
1543         } else if (!strcasecmp(c->argv[2]->ptr,"sync")) {
1544             ldbEnable(c);
1545             addReply(c,shared.ok);
1546             c->flags |= CLIENT_LUA_DEBUG_SYNC;
1547         } else {
1548             addReplyError(c,"Use SCRIPT DEBUG yes/sync/no");
1549             return;
1550         }
1551     } else {
1552         addReplySubcommandSyntaxError(c);
1553     }
1554 }
1555 
1556 /* ---------------------------------------------------------------------------
1557  * LDB: Redis Lua debugging facilities
1558  * ------------------------------------------------------------------------- */
1559 
1560 /* Initialize Lua debugger data structures. */
ldbInit(void)1561 void ldbInit(void) {
1562     ldb.fd = -1;
1563     ldb.active = 0;
1564     ldb.logs = listCreate();
1565     listSetFreeMethod(ldb.logs,(void (*)(void*))sdsfree);
1566     ldb.children = listCreate();
1567     ldb.src = NULL;
1568     ldb.lines = 0;
1569     ldb.cbuf = sdsempty();
1570 }
1571 
1572 /* Remove all the pending messages in the specified list. */
ldbFlushLog(list * log)1573 void ldbFlushLog(list *log) {
1574     listNode *ln;
1575 
1576     while((ln = listFirst(log)) != NULL)
1577         listDelNode(log,ln);
1578 }
1579 
1580 /* Enable debug mode of Lua scripts for this client. */
ldbEnable(client * c)1581 void ldbEnable(client *c) {
1582     c->flags |= CLIENT_LUA_DEBUG;
1583     ldbFlushLog(ldb.logs);
1584     ldb.fd = c->fd;
1585     ldb.step = 1;
1586     ldb.bpcount = 0;
1587     ldb.luabp = 0;
1588     sdsfree(ldb.cbuf);
1589     ldb.cbuf = sdsempty();
1590     ldb.maxlen = LDB_MAX_LEN_DEFAULT;
1591     ldb.maxlen_hint_sent = 0;
1592 }
1593 
1594 /* Exit debugging mode from the POV of client. This function is not enough
1595  * to properly shut down a client debugging session, see ldbEndSession()
1596  * for more information. */
ldbDisable(client * c)1597 void ldbDisable(client *c) {
1598     c->flags &= ~(CLIENT_LUA_DEBUG|CLIENT_LUA_DEBUG_SYNC);
1599 }
1600 
1601 /* Append a log entry to the specified LDB log. */
ldbLog(sds entry)1602 void ldbLog(sds entry) {
1603     listAddNodeTail(ldb.logs,entry);
1604 }
1605 
1606 /* A version of ldbLog() which prevents producing logs greater than
1607  * ldb.maxlen. The first time the limit is reached an hint is generated
1608  * to inform the user that reply trimming can be disabled using the
1609  * debugger "maxlen" command. */
ldbLogWithMaxLen(sds entry)1610 void ldbLogWithMaxLen(sds entry) {
1611     int trimmed = 0;
1612     if (ldb.maxlen && sdslen(entry) > ldb.maxlen) {
1613         sdsrange(entry,0,ldb.maxlen-1);
1614         entry = sdscatlen(entry," ...",4);
1615         trimmed = 1;
1616     }
1617     ldbLog(entry);
1618     if (trimmed && ldb.maxlen_hint_sent == 0) {
1619         ldb.maxlen_hint_sent = 1;
1620         ldbLog(sdsnew(
1621         "<hint> The above reply was trimmed. Use 'maxlen 0' to disable trimming."));
1622     }
1623 }
1624 
1625 /* Send ldb.logs to the debugging client as a multi-bulk reply
1626  * consisting of simple strings. Log entries which include newlines have them
1627  * replaced with spaces. The entries sent are also consumed. */
ldbSendLogs(void)1628 void ldbSendLogs(void) {
1629     sds proto = sdsempty();
1630     proto = sdscatfmt(proto,"*%i\r\n", (int)listLength(ldb.logs));
1631     while(listLength(ldb.logs)) {
1632         listNode *ln = listFirst(ldb.logs);
1633         proto = sdscatlen(proto,"+",1);
1634         sdsmapchars(ln->value,"\r\n","  ",2);
1635         proto = sdscatsds(proto,ln->value);
1636         proto = sdscatlen(proto,"\r\n",2);
1637         listDelNode(ldb.logs,ln);
1638     }
1639     if (write(ldb.fd,proto,sdslen(proto)) == -1) {
1640         /* Avoid warning. We don't check the return value of write()
1641          * since the next read() will catch the I/O error and will
1642          * close the debugging session. */
1643     }
1644     sdsfree(proto);
1645 }
1646 
1647 /* Start a debugging session before calling EVAL implementation.
1648  * The techique we use is to capture the client socket file descriptor,
1649  * in order to perform direct I/O with it from within Lua hooks. This
1650  * way we don't have to re-enter Redis in order to handle I/O.
1651  *
1652  * The function returns 1 if the caller should proceed to call EVAL,
1653  * and 0 if instead the caller should abort the operation (this happens
1654  * for the parent in a forked session, since it's up to the children
1655  * to continue, or when fork returned an error).
1656  *
1657  * The caller should call ldbEndSession() only if ldbStartSession()
1658  * returned 1. */
ldbStartSession(client * c)1659 int ldbStartSession(client *c) {
1660     ldb.forked = (c->flags & CLIENT_LUA_DEBUG_SYNC) == 0;
1661     if (ldb.forked) {
1662         pid_t cp = fork();
1663         if (cp == -1) {
1664             addReplyError(c,"Fork() failed: can't run EVAL in debugging mode.");
1665             return 0;
1666         } else if (cp == 0) {
1667             /* Child. Let's ignore important signals handled by the parent. */
1668             struct sigaction act;
1669             sigemptyset(&act.sa_mask);
1670             act.sa_flags = 0;
1671             act.sa_handler = SIG_IGN;
1672             sigaction(SIGTERM, &act, NULL);
1673             sigaction(SIGINT, &act, NULL);
1674 
1675             /* Log the creation of the child and close the listening
1676              * socket to make sure if the parent crashes a reset is sent
1677              * to the clients. */
1678             serverLog(LL_WARNING,"Redis forked for debugging eval");
1679             closeListeningSockets(0);
1680             resetCpuAffinity("redis-dbg");
1681         } else {
1682             /* Parent */
1683             listAddNodeTail(ldb.children,(void*)(unsigned long)cp);
1684             freeClientAsync(c); /* Close the client in the parent side. */
1685             return 0;
1686         }
1687     } else {
1688         serverLog(LL_WARNING,
1689             "Redis synchronous debugging eval session started");
1690     }
1691 
1692     /* Setup our debugging session. */
1693     anetBlock(NULL,ldb.fd);
1694     anetSendTimeout(NULL,ldb.fd,5000);
1695     ldb.active = 1;
1696 
1697     /* First argument of EVAL is the script itself. We split it into different
1698      * lines since this is the way the debugger accesses the source code. */
1699     sds srcstring = sdsdup(c->argv[1]->ptr);
1700     size_t srclen = sdslen(srcstring);
1701     while(srclen && (srcstring[srclen-1] == '\n' ||
1702                      srcstring[srclen-1] == '\r'))
1703     {
1704         srcstring[--srclen] = '\0';
1705     }
1706     sdssetlen(srcstring,srclen);
1707     ldb.src = sdssplitlen(srcstring,sdslen(srcstring),"\n",1,&ldb.lines);
1708     sdsfree(srcstring);
1709     return 1;
1710 }
1711 
1712 /* End a debugging session after the EVAL call with debugging enabled
1713  * returned. */
ldbEndSession(client * c)1714 void ldbEndSession(client *c) {
1715     /* Emit the remaining logs and an <endsession> mark. */
1716     ldbLog(sdsnew("<endsession>"));
1717     ldbSendLogs();
1718 
1719     /* If it's a fork()ed session, we just exit. */
1720     if (ldb.forked) {
1721         writeToClient(c->fd, c, 0);
1722         serverLog(LL_WARNING,"Lua debugging session child exiting");
1723         exitFromChild(0);
1724     } else {
1725         serverLog(LL_WARNING,
1726             "Redis synchronous debugging eval session ended");
1727     }
1728 
1729     /* Otherwise let's restore client's state. */
1730     anetNonBlock(NULL,ldb.fd);
1731     anetSendTimeout(NULL,ldb.fd,0);
1732 
1733     /* Close the client connectin after sending the final EVAL reply
1734      * in order to signal the end of the debugging session. */
1735     c->flags |= CLIENT_CLOSE_AFTER_REPLY;
1736 
1737     /* Cleanup. */
1738     sdsfreesplitres(ldb.src,ldb.lines);
1739     ldb.lines = 0;
1740     ldb.active = 0;
1741 }
1742 
1743 /* If the specified pid is among the list of children spawned for
1744  * forked debugging sessions, it is removed from the children list.
1745  * If the pid was found non-zero is returned. */
ldbRemoveChild(pid_t pid)1746 int ldbRemoveChild(pid_t pid) {
1747     listNode *ln = listSearchKey(ldb.children,(void*)(unsigned long)pid);
1748     if (ln) {
1749         listDelNode(ldb.children,ln);
1750         return 1;
1751     }
1752     return 0;
1753 }
1754 
1755 /* Return the number of children we still did not receive termination
1756  * acknowledge via wait() in the parent process. */
ldbPendingChildren(void)1757 int ldbPendingChildren(void) {
1758     return listLength(ldb.children);
1759 }
1760 
1761 /* Kill all the forked sessions. */
ldbKillForkedSessions(void)1762 void ldbKillForkedSessions(void) {
1763     listIter li;
1764     listNode *ln;
1765 
1766     listRewind(ldb.children,&li);
1767     while((ln = listNext(&li))) {
1768         pid_t pid = (unsigned long) ln->value;
1769         serverLog(LL_WARNING,"Killing debugging session %ld",(long)pid);
1770         kill(pid,SIGKILL);
1771     }
1772     listRelease(ldb.children);
1773     ldb.children = listCreate();
1774 }
1775 
1776 /* Wrapper for EVAL / EVALSHA that enables debugging, and makes sure
1777  * that when EVAL returns, whatever happened, the session is ended. */
evalGenericCommandWithDebugging(client * c,int evalsha)1778 void evalGenericCommandWithDebugging(client *c, int evalsha) {
1779     if (ldbStartSession(c)) {
1780         evalGenericCommand(c,evalsha);
1781         ldbEndSession(c);
1782     } else {
1783         ldbDisable(c);
1784     }
1785 }
1786 
1787 /* Return a pointer to ldb.src source code line, considering line to be
1788  * one-based, and returning a special string for out of range lines. */
ldbGetSourceLine(int line)1789 char *ldbGetSourceLine(int line) {
1790     int idx = line-1;
1791     if (idx < 0 || idx >= ldb.lines) return "<out of range source code line>";
1792     return ldb.src[idx];
1793 }
1794 
1795 /* Return true if there is a breakpoint in the specified line. */
ldbIsBreakpoint(int line)1796 int ldbIsBreakpoint(int line) {
1797     int j;
1798 
1799     for (j = 0; j < ldb.bpcount; j++)
1800         if (ldb.bp[j] == line) return 1;
1801     return 0;
1802 }
1803 
1804 /* Add the specified breakpoint. Ignore it if we already reached the max.
1805  * Returns 1 if the breakpoint was added (or was already set). 0 if there is
1806  * no space for the breakpoint or if the line is invalid. */
ldbAddBreakpoint(int line)1807 int ldbAddBreakpoint(int line) {
1808     if (line <= 0 || line > ldb.lines) return 0;
1809     if (!ldbIsBreakpoint(line) && ldb.bpcount != LDB_BREAKPOINTS_MAX) {
1810         ldb.bp[ldb.bpcount++] = line;
1811         return 1;
1812     }
1813     return 0;
1814 }
1815 
1816 /* Remove the specified breakpoint, returning 1 if the operation was
1817  * performed or 0 if there was no such breakpoint. */
ldbDelBreakpoint(int line)1818 int ldbDelBreakpoint(int line) {
1819     int j;
1820 
1821     for (j = 0; j < ldb.bpcount; j++) {
1822         if (ldb.bp[j] == line) {
1823             ldb.bpcount--;
1824             memmove(ldb.bp+j,ldb.bp+j+1,ldb.bpcount-j);
1825             return 1;
1826         }
1827     }
1828     return 0;
1829 }
1830 
1831 /* Expect a valid multi-bulk command in the debugging client query buffer.
1832  * On success the command is parsed and returned as an array of SDS strings,
1833  * otherwise NULL is returned and there is to read more buffer. */
ldbReplParseCommand(int * argcp)1834 sds *ldbReplParseCommand(int *argcp) {
1835     sds *argv = NULL;
1836     int argc = 0;
1837     if (sdslen(ldb.cbuf) == 0) return NULL;
1838 
1839     /* Working on a copy is simpler in this case. We can modify it freely
1840      * for the sake of simpler parsing. */
1841     sds copy = sdsdup(ldb.cbuf);
1842     char *p = copy;
1843 
1844     /* This Redis protocol parser is a joke... just the simplest thing that
1845      * works in this context. It is also very forgiving regarding broken
1846      * protocol. */
1847 
1848     /* Seek and parse *<count>\r\n. */
1849     p = strchr(p,'*'); if (!p) goto protoerr;
1850     char *plen = p+1; /* Multi bulk len pointer. */
1851     p = strstr(p,"\r\n"); if (!p) goto protoerr;
1852     *p = '\0'; p += 2;
1853     *argcp = atoi(plen);
1854     if (*argcp <= 0 || *argcp > 1024) goto protoerr;
1855 
1856     /* Parse each argument. */
1857     argv = zmalloc(sizeof(sds)*(*argcp));
1858     argc = 0;
1859     while(argc < *argcp) {
1860         if (*p != '$') goto protoerr;
1861         plen = p+1; /* Bulk string len pointer. */
1862         p = strstr(p,"\r\n"); if (!p) goto protoerr;
1863         *p = '\0'; p += 2;
1864         int slen = atoi(plen); /* Length of this arg. */
1865         if (slen <= 0 || slen > 1024) goto protoerr;
1866         argv[argc++] = sdsnewlen(p,slen);
1867         p += slen; /* Skip the already parsed argument. */
1868         if (p[0] != '\r' || p[1] != '\n') goto protoerr;
1869         p += 2; /* Skip \r\n. */
1870     }
1871     sdsfree(copy);
1872     return argv;
1873 
1874 protoerr:
1875     sdsfreesplitres(argv,argc);
1876     sdsfree(copy);
1877     return NULL;
1878 }
1879 
1880 /* Log the specified line in the Lua debugger output. */
ldbLogSourceLine(int lnum)1881 void ldbLogSourceLine(int lnum) {
1882     char *line = ldbGetSourceLine(lnum);
1883     char *prefix;
1884     int bp = ldbIsBreakpoint(lnum);
1885     int current = ldb.currentline == lnum;
1886 
1887     if (current && bp)
1888         prefix = "->#";
1889     else if (current)
1890         prefix = "-> ";
1891     else if (bp)
1892         prefix = "  #";
1893     else
1894         prefix = "   ";
1895     sds thisline = sdscatprintf(sdsempty(),"%s%-3d %s", prefix, lnum, line);
1896     ldbLog(thisline);
1897 }
1898 
1899 /* Implement the "list" command of the Lua debugger. If around is 0
1900  * the whole file is listed, otherwise only a small portion of the file
1901  * around the specified line is shown. When a line number is specified
1902  * the amonut of context (lines before/after) is specified via the
1903  * 'context' argument. */
ldbList(int around,int context)1904 void ldbList(int around, int context) {
1905     int j;
1906 
1907     for (j = 1; j <= ldb.lines; j++) {
1908         if (around != 0 && abs(around-j) > context) continue;
1909         ldbLogSourceLine(j);
1910     }
1911 }
1912 
1913 /* Append an human readable representation of the Lua value at position 'idx'
1914  * on the stack of the 'lua' state, to the SDS string passed as argument.
1915  * The new SDS string with the represented value attached is returned.
1916  * Used in order to implement ldbLogStackValue().
1917  *
1918  * The element is not automatically removed from the stack, nor it is
1919  * converted to a different type. */
1920 #define LDB_MAX_VALUES_DEPTH (LUA_MINSTACK/2)
ldbCatStackValueRec(sds s,lua_State * lua,int idx,int level)1921 sds ldbCatStackValueRec(sds s, lua_State *lua, int idx, int level) {
1922     int t = lua_type(lua,idx);
1923 
1924     if (level++ == LDB_MAX_VALUES_DEPTH)
1925         return sdscat(s,"<max recursion level reached! Nested table?>");
1926 
1927     switch(t) {
1928     case LUA_TSTRING:
1929         {
1930         size_t strl;
1931         char *strp = (char*)lua_tolstring(lua,idx,&strl);
1932         s = sdscatrepr(s,strp,strl);
1933         }
1934         break;
1935     case LUA_TBOOLEAN:
1936         s = sdscat(s,lua_toboolean(lua,idx) ? "true" : "false");
1937         break;
1938     case LUA_TNUMBER:
1939         s = sdscatprintf(s,"%g",(double)lua_tonumber(lua,idx));
1940         break;
1941     case LUA_TNIL:
1942         s = sdscatlen(s,"nil",3);
1943         break;
1944     case LUA_TTABLE:
1945         {
1946         int expected_index = 1; /* First index we expect in an array. */
1947         int is_array = 1; /* Will be set to null if check fails. */
1948         /* Note: we create two representations at the same time, one
1949          * assuming the table is an array, one assuming it is not. At the
1950          * end we know what is true and select the right one. */
1951         sds repr1 = sdsempty();
1952         sds repr2 = sdsempty();
1953         lua_pushnil(lua); /* The first key to start the iteration is nil. */
1954         while (lua_next(lua,idx-1)) {
1955             /* Test if so far the table looks like an array. */
1956             if (is_array &&
1957                 (lua_type(lua,-2) != LUA_TNUMBER ||
1958                  lua_tonumber(lua,-2) != expected_index)) is_array = 0;
1959             /* Stack now: table, key, value */
1960             /* Array repr. */
1961             repr1 = ldbCatStackValueRec(repr1,lua,-1,level);
1962             repr1 = sdscatlen(repr1,"; ",2);
1963             /* Full repr. */
1964             repr2 = sdscatlen(repr2,"[",1);
1965             repr2 = ldbCatStackValueRec(repr2,lua,-2,level);
1966             repr2 = sdscatlen(repr2,"]=",2);
1967             repr2 = ldbCatStackValueRec(repr2,lua,-1,level);
1968             repr2 = sdscatlen(repr2,"; ",2);
1969             lua_pop(lua,1); /* Stack: table, key. Ready for next iteration. */
1970             expected_index++;
1971         }
1972         /* Strip the last " ;" from both the representations. */
1973         if (sdslen(repr1)) sdsrange(repr1,0,-3);
1974         if (sdslen(repr2)) sdsrange(repr2,0,-3);
1975         /* Select the right one and discard the other. */
1976         s = sdscatlen(s,"{",1);
1977         s = sdscatsds(s,is_array ? repr1 : repr2);
1978         s = sdscatlen(s,"}",1);
1979         sdsfree(repr1);
1980         sdsfree(repr2);
1981         }
1982         break;
1983     case LUA_TFUNCTION:
1984     case LUA_TUSERDATA:
1985     case LUA_TTHREAD:
1986     case LUA_TLIGHTUSERDATA:
1987         {
1988         const void *p = lua_topointer(lua,idx);
1989         char *typename = "unknown";
1990         if (t == LUA_TFUNCTION) typename = "function";
1991         else if (t == LUA_TUSERDATA) typename = "userdata";
1992         else if (t == LUA_TTHREAD) typename = "thread";
1993         else if (t == LUA_TLIGHTUSERDATA) typename = "light-userdata";
1994         s = sdscatprintf(s,"\"%s@%p\"",typename,p);
1995         }
1996         break;
1997     default:
1998         s = sdscat(s,"\"<unknown-lua-type>\"");
1999         break;
2000     }
2001     return s;
2002 }
2003 
2004 /* Higher level wrapper for ldbCatStackValueRec() that just uses an initial
2005  * recursion level of '0'. */
ldbCatStackValue(sds s,lua_State * lua,int idx)2006 sds ldbCatStackValue(sds s, lua_State *lua, int idx) {
2007     return ldbCatStackValueRec(s,lua,idx,0);
2008 }
2009 
2010 /* Produce a debugger log entry representing the value of the Lua object
2011  * currently on the top of the stack. The element is ot popped nor modified.
2012  * Check ldbCatStackValue() for the actual implementation. */
ldbLogStackValue(lua_State * lua,char * prefix)2013 void ldbLogStackValue(lua_State *lua, char *prefix) {
2014     sds s = sdsnew(prefix);
2015     s = ldbCatStackValue(s,lua,-1);
2016     ldbLogWithMaxLen(s);
2017 }
2018 
2019 char *ldbRedisProtocolToHuman_Int(sds *o, char *reply);
2020 char *ldbRedisProtocolToHuman_Bulk(sds *o, char *reply);
2021 char *ldbRedisProtocolToHuman_Status(sds *o, char *reply);
2022 char *ldbRedisProtocolToHuman_MultiBulk(sds *o, char *reply);
2023 
2024 /* Get Redis protocol from 'reply' and appends it in human readable form to
2025  * the passed SDS string 'o'.
2026  *
2027  * Note that the SDS string is passed by reference (pointer of pointer to
2028  * char*) so that we can return a modified pointer, as for SDS semantics. */
ldbRedisProtocolToHuman(sds * o,char * reply)2029 char *ldbRedisProtocolToHuman(sds *o, char *reply) {
2030     char *p = reply;
2031     switch(*p) {
2032     case ':': p = ldbRedisProtocolToHuman_Int(o,reply); break;
2033     case '$': p = ldbRedisProtocolToHuman_Bulk(o,reply); break;
2034     case '+': p = ldbRedisProtocolToHuman_Status(o,reply); break;
2035     case '-': p = ldbRedisProtocolToHuman_Status(o,reply); break;
2036     case '*': p = ldbRedisProtocolToHuman_MultiBulk(o,reply); break;
2037     }
2038     return p;
2039 }
2040 
2041 /* The following functions are helpers for ldbRedisProtocolToHuman(), each
2042  * take care of a given Redis return type. */
2043 
ldbRedisProtocolToHuman_Int(sds * o,char * reply)2044 char *ldbRedisProtocolToHuman_Int(sds *o, char *reply) {
2045     char *p = strchr(reply+1,'\r');
2046     *o = sdscatlen(*o,reply+1,p-reply-1);
2047     return p+2;
2048 }
2049 
ldbRedisProtocolToHuman_Bulk(sds * o,char * reply)2050 char *ldbRedisProtocolToHuman_Bulk(sds *o, char *reply) {
2051     char *p = strchr(reply+1,'\r');
2052     long long bulklen;
2053 
2054     string2ll(reply+1,p-reply-1,&bulklen);
2055     if (bulklen == -1) {
2056         *o = sdscatlen(*o,"NULL",4);
2057         return p+2;
2058     } else {
2059         *o = sdscatrepr(*o,p+2,bulklen);
2060         return p+2+bulklen+2;
2061     }
2062 }
2063 
ldbRedisProtocolToHuman_Status(sds * o,char * reply)2064 char *ldbRedisProtocolToHuman_Status(sds *o, char *reply) {
2065     char *p = strchr(reply+1,'\r');
2066 
2067     *o = sdscatrepr(*o,reply,p-reply);
2068     return p+2;
2069 }
2070 
ldbRedisProtocolToHuman_MultiBulk(sds * o,char * reply)2071 char *ldbRedisProtocolToHuman_MultiBulk(sds *o, char *reply) {
2072     char *p = strchr(reply+1,'\r');
2073     long long mbulklen;
2074     int j = 0;
2075 
2076     string2ll(reply+1,p-reply-1,&mbulklen);
2077     p += 2;
2078     if (mbulklen == -1) {
2079         *o = sdscatlen(*o,"NULL",4);
2080         return p;
2081     }
2082     *o = sdscatlen(*o,"[",1);
2083     for (j = 0; j < mbulklen; j++) {
2084         p = ldbRedisProtocolToHuman(o,p);
2085         if (j != mbulklen-1) *o = sdscatlen(*o,",",1);
2086     }
2087     *o = sdscatlen(*o,"]",1);
2088     return p;
2089 }
2090 
2091 /* Log a Redis reply as debugger output, in an human readable format.
2092  * If the resulting string is longer than 'len' plus a few more chars
2093  * used as prefix, it gets truncated. */
ldbLogRedisReply(char * reply)2094 void ldbLogRedisReply(char *reply) {
2095     sds log = sdsnew("<reply> ");
2096     ldbRedisProtocolToHuman(&log,reply);
2097     ldbLogWithMaxLen(log);
2098 }
2099 
2100 /* Implements the "print <var>" command of the Lua debugger. It scans for Lua
2101  * var "varname" starting from the current stack frame up to the top stack
2102  * frame. The first matching variable is printed. */
ldbPrint(lua_State * lua,char * varname)2103 void ldbPrint(lua_State *lua, char *varname) {
2104     lua_Debug ar;
2105 
2106     int l = 0; /* Stack level. */
2107     while (lua_getstack(lua,l,&ar) != 0) {
2108         l++;
2109         const char *name;
2110         int i = 1; /* Variable index. */
2111         while((name = lua_getlocal(lua,&ar,i)) != NULL) {
2112             i++;
2113             if (strcmp(varname,name) == 0) {
2114                 ldbLogStackValue(lua,"<value> ");
2115                 lua_pop(lua,1);
2116                 return;
2117             } else {
2118                 lua_pop(lua,1); /* Discard the var name on the stack. */
2119             }
2120         }
2121     }
2122 
2123     /* Let's try with global vars in two selected cases */
2124     if (!strcmp(varname,"ARGV") || !strcmp(varname,"KEYS")) {
2125         lua_getglobal(lua, varname);
2126         ldbLogStackValue(lua,"<value> ");
2127         lua_pop(lua,1);
2128     } else {
2129         ldbLog(sdsnew("No such variable."));
2130     }
2131 }
2132 
2133 /* Implements the "print" command (without arguments) of the Lua debugger.
2134  * Prints all the variables in the current stack frame. */
ldbPrintAll(lua_State * lua)2135 void ldbPrintAll(lua_State *lua) {
2136     lua_Debug ar;
2137     int vars = 0;
2138 
2139     if (lua_getstack(lua,0,&ar) != 0) {
2140         const char *name;
2141         int i = 1; /* Variable index. */
2142         while((name = lua_getlocal(lua,&ar,i)) != NULL) {
2143             i++;
2144             if (!strstr(name,"(*temporary)")) {
2145                 sds prefix = sdscatprintf(sdsempty(),"<value> %s = ",name);
2146                 ldbLogStackValue(lua,prefix);
2147                 sdsfree(prefix);
2148                 vars++;
2149             }
2150             lua_pop(lua,1);
2151         }
2152     }
2153 
2154     if (vars == 0) {
2155         ldbLog(sdsnew("No local variables in the current context."));
2156     }
2157 }
2158 
2159 /* Implements the break command to list, add and remove breakpoints. */
ldbBreak(sds * argv,int argc)2160 void ldbBreak(sds *argv, int argc) {
2161     if (argc == 1) {
2162         if (ldb.bpcount == 0) {
2163             ldbLog(sdsnew("No breakpoints set. Use 'b <line>' to add one."));
2164             return;
2165         } else {
2166             ldbLog(sdscatfmt(sdsempty(),"%i breakpoints set:",ldb.bpcount));
2167             int j;
2168             for (j = 0; j < ldb.bpcount; j++)
2169                 ldbLogSourceLine(ldb.bp[j]);
2170         }
2171     } else {
2172         int j;
2173         for (j = 1; j < argc; j++) {
2174             char *arg = argv[j];
2175             long line;
2176             if (!string2l(arg,sdslen(arg),&line)) {
2177                 ldbLog(sdscatfmt(sdsempty(),"Invalid argument:'%s'",arg));
2178             } else {
2179                 if (line == 0) {
2180                     ldb.bpcount = 0;
2181                     ldbLog(sdsnew("All breakpoints removed."));
2182                 } else if (line > 0) {
2183                     if (ldb.bpcount == LDB_BREAKPOINTS_MAX) {
2184                         ldbLog(sdsnew("Too many breakpoints set."));
2185                     } else if (ldbAddBreakpoint(line)) {
2186                         ldbList(line,1);
2187                     } else {
2188                         ldbLog(sdsnew("Wrong line number."));
2189                     }
2190                 } else if (line < 0) {
2191                     if (ldbDelBreakpoint(-line))
2192                         ldbLog(sdsnew("Breakpoint removed."));
2193                     else
2194                         ldbLog(sdsnew("No breakpoint in the specified line."));
2195                 }
2196             }
2197         }
2198     }
2199 }
2200 
2201 /* Implements the Lua debugger "eval" command. It just compiles the user
2202  * passed fragment of code and executes it, showing the result left on
2203  * the stack. */
ldbEval(lua_State * lua,sds * argv,int argc)2204 void ldbEval(lua_State *lua, sds *argv, int argc) {
2205     /* Glue the script together if it is composed of multiple arguments. */
2206     sds code = sdsjoinsds(argv+1,argc-1," ",1);
2207     sds expr = sdscatsds(sdsnew("return "),code);
2208 
2209     /* Try to compile it as an expression, prepending "return ". */
2210     if (luaL_loadbuffer(lua,expr,sdslen(expr),"@ldb_eval")) {
2211         lua_pop(lua,1);
2212         /* Failed? Try as a statement. */
2213         if (luaL_loadbuffer(lua,code,sdslen(code),"@ldb_eval")) {
2214             ldbLog(sdscatfmt(sdsempty(),"<error> %s",lua_tostring(lua,-1)));
2215             lua_pop(lua,1);
2216             sdsfree(code);
2217             return;
2218         }
2219     }
2220 
2221     /* Call it. */
2222     sdsfree(code);
2223     sdsfree(expr);
2224     if (lua_pcall(lua,0,1,0)) {
2225         ldbLog(sdscatfmt(sdsempty(),"<error> %s",lua_tostring(lua,-1)));
2226         lua_pop(lua,1);
2227         return;
2228     }
2229     ldbLogStackValue(lua,"<retval> ");
2230     lua_pop(lua,1);
2231 }
2232 
2233 /* Implement the debugger "redis" command. We use a trick in order to make
2234  * the implementation very simple: we just call the Lua redis.call() command
2235  * implementation, with ldb.step enabled, so as a side effect the Redis command
2236  * and its reply are logged. */
ldbRedis(lua_State * lua,sds * argv,int argc)2237 void ldbRedis(lua_State *lua, sds *argv, int argc) {
2238     int j, saved_rc = server.lua_replicate_commands;
2239 
2240     lua_getglobal(lua,"redis");
2241     lua_pushstring(lua,"call");
2242     lua_gettable(lua,-2);       /* Stack: redis, redis.call */
2243     for (j = 1; j < argc; j++)
2244         lua_pushlstring(lua,argv[j],sdslen(argv[j]));
2245     ldb.step = 1;               /* Force redis.call() to log. */
2246     server.lua_replicate_commands = 1;
2247     lua_pcall(lua,argc-1,1,0);  /* Stack: redis, result */
2248     ldb.step = 0;               /* Disable logging. */
2249     server.lua_replicate_commands = saved_rc;
2250     lua_pop(lua,2);             /* Discard the result and clean the stack. */
2251 }
2252 
2253 /* Implements "trace" command of the Lua debugger. It just prints a backtrace
2254  * querying Lua starting from the current callframe back to the outer one. */
ldbTrace(lua_State * lua)2255 void ldbTrace(lua_State *lua) {
2256     lua_Debug ar;
2257     int level = 0;
2258 
2259     while(lua_getstack(lua,level,&ar)) {
2260         lua_getinfo(lua,"Snl",&ar);
2261         if(strstr(ar.short_src,"user_script") != NULL) {
2262             ldbLog(sdscatprintf(sdsempty(),"%s %s:",
2263                 (level == 0) ? "In" : "From",
2264                 ar.name ? ar.name : "top level"));
2265             ldbLogSourceLine(ar.currentline);
2266         }
2267         level++;
2268     }
2269     if (level == 0) {
2270         ldbLog(sdsnew("<error> Can't retrieve Lua stack."));
2271     }
2272 }
2273 
2274 /* Impleemnts the debugger "maxlen" command. It just queries or sets the
2275  * ldb.maxlen variable. */
ldbMaxlen(sds * argv,int argc)2276 void ldbMaxlen(sds *argv, int argc) {
2277     if (argc == 2) {
2278         int newval = atoi(argv[1]);
2279         ldb.maxlen_hint_sent = 1; /* User knows about this command. */
2280         if (newval != 0 && newval <= 60) newval = 60;
2281         ldb.maxlen = newval;
2282     }
2283     if (ldb.maxlen) {
2284         ldbLog(sdscatprintf(sdsempty(),"<value> replies are truncated at %d bytes.",(int)ldb.maxlen));
2285     } else {
2286         ldbLog(sdscatprintf(sdsempty(),"<value> replies are unlimited."));
2287     }
2288 }
2289 
2290 /* Read debugging commands from client.
2291  * Return C_OK if the debugging session is continuing, otherwise
2292  * C_ERR if the client closed the connection or is timing out. */
ldbRepl(lua_State * lua)2293 int ldbRepl(lua_State *lua) {
2294     sds *argv;
2295     int argc;
2296 
2297     /* We continue processing commands until a command that should return
2298      * to the Lua interpreter is found. */
2299     while(1) {
2300         while((argv = ldbReplParseCommand(&argc)) == NULL) {
2301             char buf[1024];
2302             int nread = read(ldb.fd,buf,sizeof(buf));
2303             if (nread <= 0) {
2304                 /* Make sure the script runs without user input since the
2305                  * client is no longer connected. */
2306                 ldb.step = 0;
2307                 ldb.bpcount = 0;
2308                 return C_ERR;
2309             }
2310             ldb.cbuf = sdscatlen(ldb.cbuf,buf,nread);
2311         }
2312 
2313         /* Flush the old buffer. */
2314         sdsfree(ldb.cbuf);
2315         ldb.cbuf = sdsempty();
2316 
2317         /* Execute the command. */
2318         if (!strcasecmp(argv[0],"h") || !strcasecmp(argv[0],"help")) {
2319 ldbLog(sdsnew("Redis Lua debugger help:"));
2320 ldbLog(sdsnew("[h]elp               Show this help."));
2321 ldbLog(sdsnew("[s]tep               Run current line and stop again."));
2322 ldbLog(sdsnew("[n]ext               Alias for step."));
2323 ldbLog(sdsnew("[c]continue          Run till next breakpoint."));
2324 ldbLog(sdsnew("[l]list              List source code around current line."));
2325 ldbLog(sdsnew("[l]list [line]       List source code around [line]."));
2326 ldbLog(sdsnew("                     line = 0 means: current position."));
2327 ldbLog(sdsnew("[l]list [line] [ctx] In this form [ctx] specifies how many lines"));
2328 ldbLog(sdsnew("                     to show before/after [line]."));
2329 ldbLog(sdsnew("[w]hole              List all source code. Alias for 'list 1 1000000'."));
2330 ldbLog(sdsnew("[p]rint              Show all the local variables."));
2331 ldbLog(sdsnew("[p]rint <var>        Show the value of the specified variable."));
2332 ldbLog(sdsnew("                     Can also show global vars KEYS and ARGV."));
2333 ldbLog(sdsnew("[b]reak              Show all breakpoints."));
2334 ldbLog(sdsnew("[b]reak <line>       Add a breakpoint to the specified line."));
2335 ldbLog(sdsnew("[b]reak -<line>      Remove breakpoint from the specified line."));
2336 ldbLog(sdsnew("[b]reak 0            Remove all breakpoints."));
2337 ldbLog(sdsnew("[t]race              Show a backtrace."));
2338 ldbLog(sdsnew("[e]eval <code>       Execute some Lua code (in a different callframe)."));
2339 ldbLog(sdsnew("[r]edis <cmd>        Execute a Redis command."));
2340 ldbLog(sdsnew("[m]axlen [len]       Trim logged Redis replies and Lua var dumps to len."));
2341 ldbLog(sdsnew("                     Specifying zero as <len> means unlimited."));
2342 ldbLog(sdsnew("[a]bort              Stop the execution of the script. In sync"));
2343 ldbLog(sdsnew("                     mode dataset changes will be retained."));
2344 ldbLog(sdsnew(""));
2345 ldbLog(sdsnew("Debugger functions you can call from Lua scripts:"));
2346 ldbLog(sdsnew("redis.debug()        Produce logs in the debugger console."));
2347 ldbLog(sdsnew("redis.breakpoint()   Stop execution like if there was a breakpoing."));
2348 ldbLog(sdsnew("                     in the next line of code."));
2349             ldbSendLogs();
2350         } else if (!strcasecmp(argv[0],"s") || !strcasecmp(argv[0],"step") ||
2351                    !strcasecmp(argv[0],"n") || !strcasecmp(argv[0],"next")) {
2352             ldb.step = 1;
2353             break;
2354         } else if (!strcasecmp(argv[0],"c") || !strcasecmp(argv[0],"continue")){
2355             break;
2356         } else if (!strcasecmp(argv[0],"t") || !strcasecmp(argv[0],"trace")) {
2357             ldbTrace(lua);
2358             ldbSendLogs();
2359         } else if (!strcasecmp(argv[0],"m") || !strcasecmp(argv[0],"maxlen")) {
2360             ldbMaxlen(argv,argc);
2361             ldbSendLogs();
2362         } else if (!strcasecmp(argv[0],"b") || !strcasecmp(argv[0],"break")) {
2363             ldbBreak(argv,argc);
2364             ldbSendLogs();
2365         } else if (!strcasecmp(argv[0],"e") || !strcasecmp(argv[0],"eval")) {
2366             ldbEval(lua,argv,argc);
2367             ldbSendLogs();
2368         } else if (!strcasecmp(argv[0],"a") || !strcasecmp(argv[0],"abort")) {
2369             lua_pushstring(lua, "script aborted for user request");
2370             lua_error(lua);
2371         } else if (argc > 1 &&
2372                    (!strcasecmp(argv[0],"r") || !strcasecmp(argv[0],"redis"))) {
2373             ldbRedis(lua,argv,argc);
2374             ldbSendLogs();
2375         } else if ((!strcasecmp(argv[0],"p") || !strcasecmp(argv[0],"print"))) {
2376             if (argc == 2)
2377                 ldbPrint(lua,argv[1]);
2378             else
2379                 ldbPrintAll(lua);
2380             ldbSendLogs();
2381         } else if (!strcasecmp(argv[0],"l") || !strcasecmp(argv[0],"list")){
2382             int around = ldb.currentline, ctx = 5;
2383             if (argc > 1) {
2384                 int num = atoi(argv[1]);
2385                 if (num > 0) around = num;
2386             }
2387             if (argc > 2) ctx = atoi(argv[2]);
2388             ldbList(around,ctx);
2389             ldbSendLogs();
2390         } else if (!strcasecmp(argv[0],"w") || !strcasecmp(argv[0],"whole")){
2391             ldbList(1,1000000);
2392             ldbSendLogs();
2393         } else {
2394             ldbLog(sdsnew("<error> Unknown Redis Lua debugger command or "
2395                           "wrong number of arguments."));
2396             ldbSendLogs();
2397         }
2398 
2399         /* Free the command vector. */
2400         sdsfreesplitres(argv,argc);
2401     }
2402 
2403     /* Free the current command argv if we break inside the while loop. */
2404     sdsfreesplitres(argv,argc);
2405     return C_OK;
2406 }
2407 
2408 /* This is the core of our Lua debugger, called each time Lua is about
2409  * to start executing a new line. */
luaLdbLineHook(lua_State * lua,lua_Debug * ar)2410 void luaLdbLineHook(lua_State *lua, lua_Debug *ar) {
2411     lua_getstack(lua,0,ar);
2412     lua_getinfo(lua,"Sl",ar);
2413     ldb.currentline = ar->currentline;
2414 
2415     int bp = ldbIsBreakpoint(ldb.currentline) || ldb.luabp;
2416     int timeout = 0;
2417 
2418     /* Events outside our script are not interesting. */
2419     if(strstr(ar->short_src,"user_script") == NULL) return;
2420 
2421     /* Check if a timeout occurred. */
2422     if (ar->event == LUA_HOOKCOUNT && ldb.step == 0 && bp == 0) {
2423         mstime_t elapsed = mstime() - server.lua_time_start;
2424         mstime_t timelimit = server.lua_time_limit ?
2425                              server.lua_time_limit : 5000;
2426         if (elapsed >= timelimit) {
2427             timeout = 1;
2428             ldb.step = 1;
2429         } else {
2430             return; /* No timeout, ignore the COUNT event. */
2431         }
2432     }
2433 
2434     if (ldb.step || bp) {
2435         char *reason = "step over";
2436         if (bp) reason = ldb.luabp ? "redis.breakpoint() called" :
2437                                      "break point";
2438         else if (timeout) reason = "timeout reached, infinite loop?";
2439         ldb.step = 0;
2440         ldb.luabp = 0;
2441         ldbLog(sdscatprintf(sdsempty(),
2442             "* Stopped at %d, stop reason = %s",
2443             ldb.currentline, reason));
2444         ldbLogSourceLine(ldb.currentline);
2445         ldbSendLogs();
2446         if (ldbRepl(lua) == C_ERR && timeout) {
2447             /* If the client closed the connection and we have a timeout
2448              * connection, let's kill the script otherwise the process
2449              * will remain blocked indefinitely. */
2450             lua_pushstring(lua, "timeout during Lua debugging with client closing connection");
2451             lua_error(lua);
2452         }
2453         server.lua_time_start = mstime();
2454     }
2455 }
2456 
2457