]>
git.saurik.com Git - redis.git/blob - src/debug.c
2 #include "sha1.h" /* SHA1 is used for DEBUG DIGEST */
6 /* ================================= Debugging ============================== */
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.
13 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
14 void xorDigest(unsigned char *digest
, void *ptr
, size_t len
) {
16 unsigned char hash
[20], *s
= ptr
;
20 SHA1Update(&ctx
,s
,len
);
23 for (j
= 0; j
< 20; j
++)
27 void xorObjectDigest(unsigned char *digest
, robj
*o
) {
28 o
= getDecodedObject(o
);
29 xorDigest(digest
,o
->ptr
,sdslen(o
->ptr
));
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.
37 * So the final digest will be:
39 * digest = SHA1(digest xor SHA1(data))
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)
44 * Also note that mixdigest("foo") followed by mixdigest("bar")
45 * will lead to a different digest compared to "fo", "obar".
47 void mixDigest(unsigned char *digest
, void *ptr
, size_t len
) {
51 xorDigest(digest
,s
,len
);
53 SHA1Update(&ctx
,digest
,20);
54 SHA1Final(digest
,&ctx
);
57 void mixObjectDigest(unsigned char *digest
, robj
*o
) {
58 o
= getDecodedObject(o
);
59 mixDigest(digest
,o
->ptr
,sdslen(o
->ptr
));
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. */
69 void computeDatasetDigest(unsigned char *final
) {
70 unsigned char digest
[20];
72 dictIterator
*di
= NULL
;
77 memset(final
,0,20); /* Start with a clean result */
79 for (j
= 0; j
< server
.dbnum
; j
++) {
80 redisDb
*db
= server
.db
+j
;
82 if (dictSize(db
->dict
) == 0) continue;
83 di
= dictGetIterator(db
->dict
);
85 /* hash the DB id, so the same dataset moved in a different
86 * DB will lead to a different digest */
88 mixDigest(final
,&aux
,sizeof(aux
));
90 /* Iterate this DB writing every entry */
91 while((de
= dictNext(di
)) != NULL
) {
96 memset(digest
,0,20); /* This key-val digest */
97 key
= dictGetEntryKey(de
);
98 keyobj
= createStringObject(key
,sdslen(key
));
100 mixDigest(digest
,key
,sdslen(key
));
102 /* Make sure the key is loaded if VM is active */
103 o
= lookupKeyRead(db
,keyobj
);
105 aux
= htonl(o
->type
);
106 mixDigest(digest
,&aux
,sizeof(aux
));
107 expiretime
= getExpire(db
,keyobj
);
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
);
115 while(listTypeNext(li
,&entry
)) {
116 robj
*eleobj
= listTypeGet(&entry
);
117 mixObjectDigest(digest
,eleobj
);
118 decrRefCount(eleobj
);
120 listTypeReleaseIterator(li
);
121 } else if (o
->type
== REDIS_SET
) {
123 dictIterator
*di
= dictGetIterator(set
);
126 while((de
= dictNext(di
)) != NULL
) {
127 robj
*eleobj
= dictGetEntryKey(de
);
129 xorObjectDigest(digest
,eleobj
);
131 dictReleaseIterator(di
);
132 } else if (o
->type
== REDIS_ZSET
) {
134 dictIterator
*di
= dictGetIterator(zs
->dict
);
137 while((de
= dictNext(di
)) != NULL
) {
138 robj
*eleobj
= dictGetEntryKey(de
);
139 double *score
= dictGetEntryVal(de
);
140 unsigned char eledigest
[20];
142 snprintf(buf
,sizeof(buf
),"%.17g",*score
);
143 memset(eledigest
,0,20);
144 mixObjectDigest(eledigest
,eleobj
);
145 mixDigest(eledigest
,buf
,strlen(buf
));
146 xorDigest(digest
,eledigest
,20);
148 dictReleaseIterator(di
);
149 } else if (o
->type
== REDIS_HASH
) {
150 hashTypeIterator
*hi
;
153 hi
= hashTypeInitIterator(o
);
154 while (hashTypeNext(hi
) != REDIS_ERR
) {
155 unsigned char eledigest
[20];
157 memset(eledigest
,0,20);
158 obj
= hashTypeCurrent(hi
,REDIS_HASH_KEY
);
159 mixObjectDigest(eledigest
,obj
);
161 obj
= hashTypeCurrent(hi
,REDIS_HASH_VALUE
);
162 mixObjectDigest(eledigest
,obj
);
164 xorDigest(digest
,eledigest
,20);
166 hashTypeReleaseIterator(hi
);
168 redisPanic("Unknown object type");
170 /* If the key has an expire, add it to the mix */
171 if (expiretime
!= -1) xorDigest(digest
,"!!expire!!",10);
172 /* We can finally xor the key-val digest to the final digest */
173 xorDigest(final
,digest
,20);
174 decrRefCount(keyobj
);
176 dictReleaseIterator(di
);
180 void debugCommand(redisClient
*c
) {
181 if (!strcasecmp(c
->argv
[1]->ptr
,"segfault")) {
183 } else if (!strcasecmp(c
->argv
[1]->ptr
,"reload")) {
184 if (rdbSave(server
.dbfilename
) != REDIS_OK
) {
185 addReply(c
,shared
.err
);
189 if (rdbLoad(server
.dbfilename
) != REDIS_OK
) {
190 addReply(c
,shared
.err
);
193 redisLog(REDIS_WARNING
,"DB reloaded by DEBUG RELOAD");
194 addReply(c
,shared
.ok
);
195 } else if (!strcasecmp(c
->argv
[1]->ptr
,"loadaof")) {
197 if (loadAppendOnlyFile(server
.appendfilename
) != REDIS_OK
) {
198 addReply(c
,shared
.err
);
201 redisLog(REDIS_WARNING
,"Append Only File loaded by DEBUG LOADAOF");
202 addReply(c
,shared
.ok
);
203 } else if (!strcasecmp(c
->argv
[1]->ptr
,"object") && c
->argc
== 3) {
204 dictEntry
*de
= dictFind(c
->db
->dict
,c
->argv
[2]->ptr
);
208 addReply(c
,shared
.nokeyerr
);
211 val
= dictGetEntryVal(de
);
212 if (!server
.vm_enabled
|| (val
->storage
== REDIS_VM_MEMORY
||
213 val
->storage
== REDIS_VM_SWAPPING
)) {
216 strenc
= strEncoding(val
->encoding
);
217 addReplySds(c
,sdscatprintf(sdsempty(),
218 "+Value at:%p refcount:%d "
219 "encoding:%s serializedlength:%lld\r\n",
220 (void*)val
, val
->refcount
,
221 strenc
, (long long) rdbSavedObjectLen(val
,NULL
)));
223 vmpointer
*vp
= (vmpointer
*) val
;
224 addReplySds(c
,sdscatprintf(sdsempty(),
225 "+Value swapped at: page %llu "
226 "using %llu pages\r\n",
227 (unsigned long long) vp
->page
,
228 (unsigned long long) vp
->usedpages
));
230 } else if (!strcasecmp(c
->argv
[1]->ptr
,"swapin") && c
->argc
== 3) {
231 lookupKeyRead(c
->db
,c
->argv
[2]);
232 addReply(c
,shared
.ok
);
233 } else if (!strcasecmp(c
->argv
[1]->ptr
,"swapout") && c
->argc
== 3) {
234 dictEntry
*de
= dictFind(c
->db
->dict
,c
->argv
[2]->ptr
);
238 if (!server
.vm_enabled
) {
239 addReplySds(c
,sdsnew("-ERR Virtual Memory is disabled\r\n"));
243 addReply(c
,shared
.nokeyerr
);
246 val
= dictGetEntryVal(de
);
248 if (val
->storage
!= REDIS_VM_MEMORY
) {
249 addReplySds(c
,sdsnew("-ERR This key is not in memory\r\n"));
250 } else if (val
->refcount
!= 1) {
251 addReplySds(c
,sdsnew("-ERR Object is shared\r\n"));
252 } else if ((vp
= vmSwapObjectBlocking(val
)) != NULL
) {
253 dictGetEntryVal(de
) = vp
;
254 addReply(c
,shared
.ok
);
256 addReply(c
,shared
.err
);
258 } else if (!strcasecmp(c
->argv
[1]->ptr
,"populate") && c
->argc
== 3) {
263 if (getLongFromObjectOrReply(c
, c
->argv
[2], &keys
, NULL
) != REDIS_OK
)
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
) {
272 snprintf(buf
,sizeof(buf
),"value:%lu",j
);
273 val
= createStringObject(buf
,strlen(buf
));
274 dbAdd(c
->db
,key
,val
);
277 addReply(c
,shared
.ok
);
278 } else if (!strcasecmp(c
->argv
[1]->ptr
,"digest") && c
->argc
== 2) {
279 unsigned char digest
[20];
283 computeDatasetDigest(digest
);
284 for (j
= 0; j
< 20; j
++)
285 d
= sdscatprintf(d
, "%02x",digest
[j
]);
287 d
= sdscatlen(d
,"\r\n",2);
290 addReplySds(c
,sdsnew(
291 "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]\r\n"));
295 void _redisAssert(char *estr
, char *file
, int line
) {
296 redisLog(REDIS_WARNING
,"=== ASSERTION FAILED ===");
297 redisLog(REDIS_WARNING
,"==> %s:%d '%s' is not true",file
,line
,estr
);
298 #ifdef HAVE_BACKTRACE
299 redisLog(REDIS_WARNING
,"(forcing SIGSEGV in order to print the stack trace)");
304 void _redisPanic(char *msg
, char *file
, int line
) {
305 redisLog(REDIS_WARNING
,"!!! Software Failure. Press left mouse button to continue");
306 redisLog(REDIS_WARNING
,"Guru Meditation: %s #%s:%d",msg
,file
,line
);
307 #ifdef HAVE_BACKTRACE
308 redisLog(REDIS_WARNING
,"(forcing SIGSEGV in order to print the stack trace)");