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