]> git.saurik.com Git - redis.git/blame - src/debug.c
error in comment fixed
[redis.git] / src / debug.c
CommitLineData
e2641e09 1#include "redis.h"
2#include "sha1.h" /* SHA1 is used for DEBUG DIGEST */
3
3688d7f3 4#include <arpa/inet.h>
5
e2641e09 6/* ================================= Debugging ============================== */
7
8/* Compute the sha1 of string at 's' with 'len' bytes long.
9 * The SHA1 is then xored againt the string pointed by digest.
10 * Since xor is commutative, this operation is used in order to
11 * "add" digests relative to unordered elements.
12 *
13 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
14void xorDigest(unsigned char *digest, void *ptr, size_t len) {
15 SHA1_CTX ctx;
16 unsigned char hash[20], *s = ptr;
17 int j;
18
19 SHA1Init(&ctx);
20 SHA1Update(&ctx,s,len);
21 SHA1Final(hash,&ctx);
22
23 for (j = 0; j < 20; j++)
24 digest[j] ^= hash[j];
25}
26
27void xorObjectDigest(unsigned char *digest, robj *o) {
28 o = getDecodedObject(o);
29 xorDigest(digest,o->ptr,sdslen(o->ptr));
30 decrRefCount(o);
31}
32
33/* This function instead of just computing the SHA1 and xoring it
34 * against diget, also perform the digest of "digest" itself and
35 * replace the old value with the new one.
36 *
37 * So the final digest will be:
38 *
39 * digest = SHA1(digest xor SHA1(data))
40 *
41 * This function is used every time we want to preserve the order so
42 * that digest(a,b,c,d) will be different than digest(b,c,d,a)
43 *
44 * Also note that mixdigest("foo") followed by mixdigest("bar")
45 * will lead to a different digest compared to "fo", "obar".
46 */
47void mixDigest(unsigned char *digest, void *ptr, size_t len) {
48 SHA1_CTX ctx;
49 char *s = ptr;
50
51 xorDigest(digest,s,len);
52 SHA1Init(&ctx);
53 SHA1Update(&ctx,digest,20);
54 SHA1Final(digest,&ctx);
55}
56
57void mixObjectDigest(unsigned char *digest, robj *o) {
58 o = getDecodedObject(o);
59 mixDigest(digest,o->ptr,sdslen(o->ptr));
60 decrRefCount(o);
61}
62
63/* Compute the dataset digest. Since keys, sets elements, hashes elements
64 * are not ordered, we use a trick: every aggregate digest is the xor
65 * of the digests of their elements. This way the order will not change
66 * the result. For list instead we use a feedback entering the output digest
67 * as input in order to ensure that a different ordered list will result in
68 * a different digest. */
69void computeDatasetDigest(unsigned char *final) {
70 unsigned char digest[20];
71 char buf[128];
72 dictIterator *di = NULL;
73 dictEntry *de;
74 int j;
75 uint32_t aux;
76
77 memset(final,0,20); /* Start with a clean result */
78
79 for (j = 0; j < server.dbnum; j++) {
80 redisDb *db = server.db+j;
81
82 if (dictSize(db->dict) == 0) continue;
83 di = dictGetIterator(db->dict);
84
85 /* hash the DB id, so the same dataset moved in a different
86 * DB will lead to a different digest */
87 aux = htonl(j);
88 mixDigest(final,&aux,sizeof(aux));
89
90 /* Iterate this DB writing every entry */
91 while((de = dictNext(di)) != NULL) {
92 sds key;
93 robj *keyobj, *o;
4be855e7 94 long long expiretime;
e2641e09 95
96 memset(digest,0,20); /* This key-val digest */
c0ba9ebe 97 key = dictGetKey(de);
e2641e09 98 keyobj = createStringObject(key,sdslen(key));
99
100 mixDigest(digest,key,sdslen(key));
101
102 /* Make sure the key is loaded if VM is active */
c0ba9ebe 103 o = dictGetVal(de);
e2641e09 104
105 aux = htonl(o->type);
106 mixDigest(digest,&aux,sizeof(aux));
107 expiretime = getExpire(db,keyobj);
108
109 /* Save the key and associated value */
110 if (o->type == REDIS_STRING) {
111 mixObjectDigest(digest,o);
112 } else if (o->type == REDIS_LIST) {
113 listTypeIterator *li = listTypeInitIterator(o,0,REDIS_TAIL);
114 listTypeEntry entry;
115 while(listTypeNext(li,&entry)) {
116 robj *eleobj = listTypeGet(&entry);
117 mixObjectDigest(digest,eleobj);
118 decrRefCount(eleobj);
119 }
120 listTypeReleaseIterator(li);
121 } else if (o->type == REDIS_SET) {
cb72d0f1 122 setTypeIterator *si = setTypeInitIterator(o);
2767f1c0 123 robj *ele;
1b508da7 124 while((ele = setTypeNextObject(si)) != NULL) {
2767f1c0
PN
125 xorObjectDigest(digest,ele);
126 decrRefCount(ele);
e2641e09 127 }
2767f1c0 128 setTypeReleaseIterator(si);
e2641e09 129 } else if (o->type == REDIS_ZSET) {
dddf5335
PN
130 unsigned char eledigest[20];
131
132 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
133 unsigned char *zl = o->ptr;
134 unsigned char *eptr, *sptr;
135 unsigned char *vstr;
136 unsigned int vlen;
137 long long vll;
138 double score;
139
140 eptr = ziplistIndex(zl,0);
141 redisAssert(eptr != NULL);
142 sptr = ziplistNext(zl,eptr);
143 redisAssert(sptr != NULL);
144
145 while (eptr != NULL) {
146 redisAssert(ziplistGet(eptr,&vstr,&vlen,&vll));
147 score = zzlGetScore(sptr);
148
149 memset(eledigest,0,20);
150 if (vstr != NULL) {
151 mixDigest(eledigest,vstr,vlen);
152 } else {
153 ll2string(buf,sizeof(buf),vll);
154 mixDigest(eledigest,buf,strlen(buf));
155 }
156
157 snprintf(buf,sizeof(buf),"%.17g",score);
158 mixDigest(eledigest,buf,strlen(buf));
159 xorDigest(digest,eledigest,20);
160 zzlNext(zl,&eptr,&sptr);
161 }
100ed062 162 } else if (o->encoding == REDIS_ENCODING_SKIPLIST) {
dddf5335
PN
163 zset *zs = o->ptr;
164 dictIterator *di = dictGetIterator(zs->dict);
165 dictEntry *de;
166
167 while((de = dictNext(di)) != NULL) {
c0ba9ebe 168 robj *eleobj = dictGetKey(de);
169 double *score = dictGetVal(de);
dddf5335
PN
170
171 snprintf(buf,sizeof(buf),"%.17g",*score);
172 memset(eledigest,0,20);
173 mixObjectDigest(eledigest,eleobj);
174 mixDigest(eledigest,buf,strlen(buf));
175 xorDigest(digest,eledigest,20);
176 }
177 dictReleaseIterator(di);
178 } else {
179 redisPanic("Unknown sorted set encoding");
e2641e09 180 }
e2641e09 181 } else if (o->type == REDIS_HASH) {
182 hashTypeIterator *hi;
183 robj *obj;
184
185 hi = hashTypeInitIterator(o);
186 while (hashTypeNext(hi) != REDIS_ERR) {
187 unsigned char eledigest[20];
188
189 memset(eledigest,0,20);
8c304be3 190 obj = hashTypeCurrentObject(hi,REDIS_HASH_KEY);
e2641e09 191 mixObjectDigest(eledigest,obj);
192 decrRefCount(obj);
8c304be3 193 obj = hashTypeCurrentObject(hi,REDIS_HASH_VALUE);
e2641e09 194 mixObjectDigest(eledigest,obj);
195 decrRefCount(obj);
196 xorDigest(digest,eledigest,20);
197 }
198 hashTypeReleaseIterator(hi);
199 } else {
200 redisPanic("Unknown object type");
201 }
202 /* If the key has an expire, add it to the mix */
203 if (expiretime != -1) xorDigest(digest,"!!expire!!",10);
204 /* We can finally xor the key-val digest to the final digest */
205 xorDigest(final,digest,20);
206 decrRefCount(keyobj);
207 }
208 dictReleaseIterator(di);
209 }
210}
211
212void debugCommand(redisClient *c) {
213 if (!strcasecmp(c->argv[1]->ptr,"segfault")) {
214 *((char*)-1) = 'x';
e3e69935 215 } else if (!strcasecmp(c->argv[1]->ptr,"assert")) {
216 if (c->argc >= 3) c->argv[2] = tryObjectEncoding(c->argv[2]);
bab205f7 217 redisAssertWithInfo(c,c->argv[0],1 == 2);
e2641e09 218 } else if (!strcasecmp(c->argv[1]->ptr,"reload")) {
f48cd4b9 219 if (rdbSave(server.rdb_filename) != REDIS_OK) {
e2641e09 220 addReply(c,shared.err);
221 return;
222 }
223 emptyDb();
f48cd4b9 224 if (rdbLoad(server.rdb_filename) != REDIS_OK) {
80ad7189 225 addReplyError(c,"Error trying to load the RDB dump");
e2641e09 226 return;
227 }
228 redisLog(REDIS_WARNING,"DB reloaded by DEBUG RELOAD");
229 addReply(c,shared.ok);
230 } else if (!strcasecmp(c->argv[1]->ptr,"loadaof")) {
231 emptyDb();
2c915bcf 232 if (loadAppendOnlyFile(server.aof_filename) != REDIS_OK) {
e2641e09 233 addReply(c,shared.err);
234 return;
235 }
efb60225 236 server.dirty = 0; /* Prevent AOF / replication */
e2641e09 237 redisLog(REDIS_WARNING,"Append Only File loaded by DEBUG LOADAOF");
238 addReply(c,shared.ok);
239 } else if (!strcasecmp(c->argv[1]->ptr,"object") && c->argc == 3) {
b39619d8 240 dictEntry *de;
e2641e09 241 robj *val;
5ef64098 242 char *strenc;
e2641e09 243
b39619d8 244 if ((de = dictFind(c->db->dict,c->argv[2]->ptr)) == NULL) {
e2641e09 245 addReply(c,shared.nokeyerr);
246 return;
247 }
c0ba9ebe 248 val = dictGetVal(de);
5ef64098 249 strenc = strEncoding(val->encoding);
3be00d7e 250
5ef64098 251 addReplyStatusFormat(c,
252 "Value at:%p refcount:%d "
253 "encoding:%s serializedlength:%lld "
3be00d7e 254 "lru:%d lru_seconds_idle:%lu",
5ef64098 255 (void*)val, val->refcount,
256 strenc, (long long) rdbSavedObjectLen(val),
3be00d7e 257 val->lru, estimateObjectIdleTime(val));
e2641e09 258 } else if (!strcasecmp(c->argv[1]->ptr,"populate") && c->argc == 3) {
259 long keys, j;
260 robj *key, *val;
261 char buf[128];
262
263 if (getLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != REDIS_OK)
264 return;
265 for (j = 0; j < keys; j++) {
266 snprintf(buf,sizeof(buf),"key:%lu",j);
267 key = createStringObject(buf,strlen(buf));
268 if (lookupKeyRead(c->db,key) != NULL) {
269 decrRefCount(key);
270 continue;
271 }
272 snprintf(buf,sizeof(buf),"value:%lu",j);
273 val = createStringObject(buf,strlen(buf));
274 dbAdd(c->db,key,val);
275 decrRefCount(key);
276 }
277 addReply(c,shared.ok);
278 } else if (!strcasecmp(c->argv[1]->ptr,"digest") && c->argc == 2) {
279 unsigned char digest[20];
3ab20376 280 sds d = sdsempty();
e2641e09 281 int j;
282
283 computeDatasetDigest(digest);
284 for (j = 0; j < 20; j++)
285 d = sdscatprintf(d, "%02x",digest[j]);
3ab20376
PN
286 addReplyStatus(c,d);
287 sdsfree(d);
404345d8 288 } else if (!strcasecmp(c->argv[1]->ptr,"sleep") && c->argc == 3) {
289 double dtime = strtod(c->argv[2]->ptr,NULL);
290 long long utime = dtime*1000000;
291
292 usleep(utime);
293 addReply(c,shared.ok);
e2641e09 294 } else {
3ab20376
PN
295 addReplyError(c,
296 "Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]");
e2641e09 297 }
298}
299
300void _redisAssert(char *estr, char *file, int line) {
fa5af017 301 bugReportStart();
e2641e09 302 redisLog(REDIS_WARNING,"=== ASSERTION FAILED ===");
303 redisLog(REDIS_WARNING,"==> %s:%d '%s' is not true",file,line,estr);
304#ifdef HAVE_BACKTRACE
fa5af017 305 server.assert_failed = estr;
306 server.assert_file = file;
307 server.assert_line = line;
308 redisLog(REDIS_WARNING,"(forcing SIGSEGV to print the bug report.)");
e2641e09 309 *((char*)-1) = 'x';
310#endif
311}
312
e3e69935 313void _redisAssertPrintClientInfo(redisClient *c) {
bab205f7 314 int j;
e3e69935 315
fa5af017 316 bugReportStart();
bab205f7 317 redisLog(REDIS_WARNING,"=== ASSERTION FAILED CLIENT CONTEXT ===");
318 redisLog(REDIS_WARNING,"client->flags = %d", c->flags);
319 redisLog(REDIS_WARNING,"client->fd = %d", c->fd);
320 redisLog(REDIS_WARNING,"client->argc = %d", c->argc);
321 for (j=0; j < c->argc; j++) {
322 char buf[128];
323 char *arg;
324
325 if (c->argv[j]->type == REDIS_STRING &&
326 c->argv[j]->encoding == REDIS_ENCODING_RAW)
327 {
328 arg = (char*) c->argv[j]->ptr;
329 } else {
330 snprintf(buf,sizeof(buf),"Object type: %d, encoding: %d",
331 c->argv[j]->type, c->argv[j]->encoding);
332 arg = buf;
e3e69935 333 }
bab205f7 334 redisLog(REDIS_WARNING,"client->argv[%d] = \"%s\" (refcount: %d)",
335 j, arg, c->argv[j]->refcount);
336 }
337}
338
339void _redisAssertPrintObject(robj *o) {
fa5af017 340 bugReportStart();
bab205f7 341 redisLog(REDIS_WARNING,"=== ASSERTION FAILED OBJECT CONTEXT ===");
342 redisLog(REDIS_WARNING,"Object type: %d", o->type);
343 redisLog(REDIS_WARNING,"Object encoding: %d", o->encoding);
344 redisLog(REDIS_WARNING,"Object refcount: %d", o->refcount);
345 if (o->type == REDIS_STRING && o->encoding == REDIS_ENCODING_RAW) {
346 redisLog(REDIS_WARNING,"Object raw string len: %d", sdslen(o->ptr));
347 if (sdslen(o->ptr) < 4096)
348 redisLog(REDIS_WARNING,"Object raw string content: \"%s\"", (char*)o->ptr);
e3e69935 349 }
350}
351
bab205f7 352void _redisAssertWithInfo(redisClient *c, robj *o, char *estr, char *file, int line) {
353 if (c) _redisAssertPrintClientInfo(c);
354 if (o) _redisAssertPrintObject(o);
e3e69935 355 _redisAssert(estr,file,line);
356}
357
e2641e09 358void _redisPanic(char *msg, char *file, int line) {
fa5af017 359 bugReportStart();
120b9ba8 360 redisLog(REDIS_WARNING,"------------------------------------------------");
e2641e09 361 redisLog(REDIS_WARNING,"!!! Software Failure. Press left mouse button to continue");
362 redisLog(REDIS_WARNING,"Guru Meditation: %s #%s:%d",msg,file,line);
363#ifdef HAVE_BACKTRACE
364 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
120b9ba8 365 redisLog(REDIS_WARNING,"------------------------------------------------");
e2641e09 366 *((char*)-1) = 'x';
367#endif
368}