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