]>
git.saurik.com Git - redis.git/blob - src/debug.c
2 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
31 #include "sha1.h" /* SHA1 is used for DEBUG DIGEST */
34 #include <arpa/inet.h>
42 #endif /* HAVE_BACKTRACE */
44 /* ================================= Debugging ============================== */
46 /* Compute the sha1 of string at 's' with 'len' bytes long.
47 * The SHA1 is then xored againt the string pointed by digest.
48 * Since xor is commutative, this operation is used in order to
49 * "add" digests relative to unordered elements.
51 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
52 void xorDigest(unsigned char *digest
, void *ptr
, size_t len
) {
54 unsigned char hash
[20], *s
= ptr
;
58 SHA1Update(&ctx
,s
,len
);
61 for (j
= 0; j
< 20; j
++)
65 void xorObjectDigest(unsigned char *digest
, robj
*o
) {
66 o
= getDecodedObject(o
);
67 xorDigest(digest
,o
->ptr
,sdslen(o
->ptr
));
71 /* This function instead of just computing the SHA1 and xoring it
72 * against diget, also perform the digest of "digest" itself and
73 * replace the old value with the new one.
75 * So the final digest will be:
77 * digest = SHA1(digest xor SHA1(data))
79 * This function is used every time we want to preserve the order so
80 * that digest(a,b,c,d) will be different than digest(b,c,d,a)
82 * Also note that mixdigest("foo") followed by mixdigest("bar")
83 * will lead to a different digest compared to "fo", "obar".
85 void mixDigest(unsigned char *digest
, void *ptr
, size_t len
) {
89 xorDigest(digest
,s
,len
);
91 SHA1Update(&ctx
,digest
,20);
92 SHA1Final(digest
,&ctx
);
95 void mixObjectDigest(unsigned char *digest
, robj
*o
) {
96 o
= getDecodedObject(o
);
97 mixDigest(digest
,o
->ptr
,sdslen(o
->ptr
));
101 /* Compute the dataset digest. Since keys, sets elements, hashes elements
102 * are not ordered, we use a trick: every aggregate digest is the xor
103 * of the digests of their elements. This way the order will not change
104 * the result. For list instead we use a feedback entering the output digest
105 * as input in order to ensure that a different ordered list will result in
106 * a different digest. */
107 void computeDatasetDigest(unsigned char *final
) {
108 unsigned char digest
[20];
110 dictIterator
*di
= NULL
;
115 memset(final
,0,20); /* Start with a clean result */
117 for (j
= 0; j
< server
.dbnum
; j
++) {
118 redisDb
*db
= server
.db
+j
;
120 if (dictSize(db
->dict
) == 0) continue;
121 di
= dictGetIterator(db
->dict
);
123 /* hash the DB id, so the same dataset moved in a different
124 * DB will lead to a different digest */
126 mixDigest(final
,&aux
,sizeof(aux
));
128 /* Iterate this DB writing every entry */
129 while((de
= dictNext(di
)) != NULL
) {
132 long long expiretime
;
134 memset(digest
,0,20); /* This key-val digest */
135 key
= dictGetKey(de
);
136 keyobj
= createStringObject(key
,sdslen(key
));
138 mixDigest(digest
,key
,sdslen(key
));
142 aux
= htonl(o
->type
);
143 mixDigest(digest
,&aux
,sizeof(aux
));
144 expiretime
= getExpire(db
,keyobj
);
146 /* Save the key and associated value */
147 if (o
->type
== REDIS_STRING
) {
148 mixObjectDigest(digest
,o
);
149 } else if (o
->type
== REDIS_LIST
) {
150 listTypeIterator
*li
= listTypeInitIterator(o
,0,REDIS_TAIL
);
152 while(listTypeNext(li
,&entry
)) {
153 robj
*eleobj
= listTypeGet(&entry
);
154 mixObjectDigest(digest
,eleobj
);
155 decrRefCount(eleobj
);
157 listTypeReleaseIterator(li
);
158 } else if (o
->type
== REDIS_SET
) {
159 setTypeIterator
*si
= setTypeInitIterator(o
);
161 while((ele
= setTypeNextObject(si
)) != NULL
) {
162 xorObjectDigest(digest
,ele
);
165 setTypeReleaseIterator(si
);
166 } else if (o
->type
== REDIS_ZSET
) {
167 unsigned char eledigest
[20];
169 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
170 unsigned char *zl
= o
->ptr
;
171 unsigned char *eptr
, *sptr
;
177 eptr
= ziplistIndex(zl
,0);
178 redisAssert(eptr
!= NULL
);
179 sptr
= ziplistNext(zl
,eptr
);
180 redisAssert(sptr
!= NULL
);
182 while (eptr
!= NULL
) {
183 redisAssert(ziplistGet(eptr
,&vstr
,&vlen
,&vll
));
184 score
= zzlGetScore(sptr
);
186 memset(eledigest
,0,20);
188 mixDigest(eledigest
,vstr
,vlen
);
190 ll2string(buf
,sizeof(buf
),vll
);
191 mixDigest(eledigest
,buf
,strlen(buf
));
194 snprintf(buf
,sizeof(buf
),"%.17g",score
);
195 mixDigest(eledigest
,buf
,strlen(buf
));
196 xorDigest(digest
,eledigest
,20);
197 zzlNext(zl
,&eptr
,&sptr
);
199 } else if (o
->encoding
== REDIS_ENCODING_SKIPLIST
) {
201 dictIterator
*di
= dictGetIterator(zs
->dict
);
204 while((de
= dictNext(di
)) != NULL
) {
205 robj
*eleobj
= dictGetKey(de
);
206 double *score
= dictGetVal(de
);
208 snprintf(buf
,sizeof(buf
),"%.17g",*score
);
209 memset(eledigest
,0,20);
210 mixObjectDigest(eledigest
,eleobj
);
211 mixDigest(eledigest
,buf
,strlen(buf
));
212 xorDigest(digest
,eledigest
,20);
214 dictReleaseIterator(di
);
216 redisPanic("Unknown sorted set encoding");
218 } else if (o
->type
== REDIS_HASH
) {
219 hashTypeIterator
*hi
;
222 hi
= hashTypeInitIterator(o
);
223 while (hashTypeNext(hi
) != REDIS_ERR
) {
224 unsigned char eledigest
[20];
226 memset(eledigest
,0,20);
227 obj
= hashTypeCurrentObject(hi
,REDIS_HASH_KEY
);
228 mixObjectDigest(eledigest
,obj
);
230 obj
= hashTypeCurrentObject(hi
,REDIS_HASH_VALUE
);
231 mixObjectDigest(eledigest
,obj
);
233 xorDigest(digest
,eledigest
,20);
235 hashTypeReleaseIterator(hi
);
237 redisPanic("Unknown object type");
239 /* If the key has an expire, add it to the mix */
240 if (expiretime
!= -1) xorDigest(digest
,"!!expire!!",10);
241 /* We can finally xor the key-val digest to the final digest */
242 xorDigest(final
,digest
,20);
243 decrRefCount(keyobj
);
245 dictReleaseIterator(di
);
249 void debugCommand(redisClient
*c
) {
250 if (!strcasecmp(c
->argv
[1]->ptr
,"segfault")) {
252 } else if (!strcasecmp(c
->argv
[1]->ptr
,"oom")) {
253 void *ptr
= zmalloc(ULONG_MAX
); /* Should trigger an out of memory. */
255 addReply(c
,shared
.ok
);
256 } else if (!strcasecmp(c
->argv
[1]->ptr
,"assert")) {
257 if (c
->argc
>= 3) c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
258 redisAssertWithInfo(c
,c
->argv
[0],1 == 2);
259 } else if (!strcasecmp(c
->argv
[1]->ptr
,"reload")) {
260 if (rdbSave(server
.rdb_filename
) != REDIS_OK
) {
261 addReply(c
,shared
.err
);
265 if (rdbLoad(server
.rdb_filename
) != REDIS_OK
) {
266 addReplyError(c
,"Error trying to load the RDB dump");
269 redisLog(REDIS_WARNING
,"DB reloaded by DEBUG RELOAD");
270 addReply(c
,shared
.ok
);
271 } else if (!strcasecmp(c
->argv
[1]->ptr
,"loadaof")) {
273 if (loadAppendOnlyFile(server
.aof_filename
) != REDIS_OK
) {
274 addReply(c
,shared
.err
);
277 server
.dirty
= 0; /* Prevent AOF / replication */
278 redisLog(REDIS_WARNING
,"Append Only File loaded by DEBUG LOADAOF");
279 addReply(c
,shared
.ok
);
280 } else if (!strcasecmp(c
->argv
[1]->ptr
,"object") && c
->argc
== 3) {
285 if ((de
= dictFind(c
->db
->dict
,c
->argv
[2]->ptr
)) == NULL
) {
286 addReply(c
,shared
.nokeyerr
);
289 val
= dictGetVal(de
);
290 strenc
= strEncoding(val
->encoding
);
292 addReplyStatusFormat(c
,
293 "Value at:%p refcount:%d "
294 "encoding:%s serializedlength:%lld "
295 "lru:%d lru_seconds_idle:%lu",
296 (void*)val
, val
->refcount
,
297 strenc
, (long long) rdbSavedObjectLen(val
),
298 val
->lru
, estimateObjectIdleTime(val
));
299 } else if (!strcasecmp(c
->argv
[1]->ptr
,"populate") && c
->argc
== 3) {
304 if (getLongFromObjectOrReply(c
, c
->argv
[2], &keys
, NULL
) != REDIS_OK
)
306 for (j
= 0; j
< keys
; j
++) {
307 snprintf(buf
,sizeof(buf
),"key:%lu",j
);
308 key
= createStringObject(buf
,strlen(buf
));
309 if (lookupKeyRead(c
->db
,key
) != NULL
) {
313 snprintf(buf
,sizeof(buf
),"value:%lu",j
);
314 val
= createStringObject(buf
,strlen(buf
));
315 dbAdd(c
->db
,key
,val
);
318 addReply(c
,shared
.ok
);
319 } else if (!strcasecmp(c
->argv
[1]->ptr
,"digest") && c
->argc
== 2) {
320 unsigned char digest
[20];
324 computeDatasetDigest(digest
);
325 for (j
= 0; j
< 20; j
++)
326 d
= sdscatprintf(d
, "%02x",digest
[j
]);
329 } else if (!strcasecmp(c
->argv
[1]->ptr
,"sleep") && c
->argc
== 3) {
330 double dtime
= strtod(c
->argv
[2]->ptr
,NULL
);
331 long long utime
= dtime
*1000000;
334 addReply(c
,shared
.ok
);
337 "Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]");
341 /* =========================== Crash handling ============================== */
343 void _redisAssert(char *estr
, char *file
, int line
) {
345 redisLog(REDIS_WARNING
,"=== ASSERTION FAILED ===");
346 redisLog(REDIS_WARNING
,"==> %s:%d '%s' is not true",file
,line
,estr
);
347 #ifdef HAVE_BACKTRACE
348 server
.assert_failed
= estr
;
349 server
.assert_file
= file
;
350 server
.assert_line
= line
;
351 redisLog(REDIS_WARNING
,"(forcing SIGSEGV to print the bug report.)");
356 void _redisAssertPrintClientInfo(redisClient
*c
) {
360 redisLog(REDIS_WARNING
,"=== ASSERTION FAILED CLIENT CONTEXT ===");
361 redisLog(REDIS_WARNING
,"client->flags = %d", c
->flags
);
362 redisLog(REDIS_WARNING
,"client->fd = %d", c
->fd
);
363 redisLog(REDIS_WARNING
,"client->argc = %d", c
->argc
);
364 for (j
=0; j
< c
->argc
; j
++) {
368 if (c
->argv
[j
]->type
== REDIS_STRING
&&
369 c
->argv
[j
]->encoding
== REDIS_ENCODING_RAW
)
371 arg
= (char*) c
->argv
[j
]->ptr
;
373 snprintf(buf
,sizeof(buf
),"Object type: %d, encoding: %d",
374 c
->argv
[j
]->type
, c
->argv
[j
]->encoding
);
377 redisLog(REDIS_WARNING
,"client->argv[%d] = \"%s\" (refcount: %d)",
378 j
, arg
, c
->argv
[j
]->refcount
);
382 void redisLogObjectDebugInfo(robj
*o
) {
383 redisLog(REDIS_WARNING
,"Object type: %d", o
->type
);
384 redisLog(REDIS_WARNING
,"Object encoding: %d", o
->encoding
);
385 redisLog(REDIS_WARNING
,"Object refcount: %d", o
->refcount
);
386 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_RAW
) {
387 redisLog(REDIS_WARNING
,"Object raw string len: %d", sdslen(o
->ptr
));
388 if (sdslen(o
->ptr
) < 4096)
389 redisLog(REDIS_WARNING
,"Object raw string content: \"%s\"", (char*)o
->ptr
);
390 } else if (o
->type
== REDIS_LIST
) {
391 redisLog(REDIS_WARNING
,"List length: %d", (int) listTypeLength(o
));
392 } else if (o
->type
== REDIS_SET
) {
393 redisLog(REDIS_WARNING
,"Set size: %d", (int) setTypeSize(o
));
394 } else if (o
->type
== REDIS_HASH
) {
395 redisLog(REDIS_WARNING
,"Hash size: %d", (int) hashTypeLength(o
));
396 } else if (o
->type
== REDIS_ZSET
) {
397 redisLog(REDIS_WARNING
,"Sorted set size: %d", (int) zsetLength(o
));
398 if (o
->encoding
== REDIS_ENCODING_SKIPLIST
)
399 redisLog(REDIS_WARNING
,"Skiplist level: %d", (int) ((zset
*)o
->ptr
)->zsl
->level
);
403 void _redisAssertPrintObject(robj
*o
) {
405 redisLog(REDIS_WARNING
,"=== ASSERTION FAILED OBJECT CONTEXT ===");
406 redisLogObjectDebugInfo(o
);
409 void _redisAssertWithInfo(redisClient
*c
, robj
*o
, char *estr
, char *file
, int line
) {
410 if (c
) _redisAssertPrintClientInfo(c
);
411 if (o
) _redisAssertPrintObject(o
);
412 _redisAssert(estr
,file
,line
);
415 void _redisPanic(char *msg
, char *file
, int line
) {
417 redisLog(REDIS_WARNING
,"------------------------------------------------");
418 redisLog(REDIS_WARNING
,"!!! Software Failure. Press left mouse button to continue");
419 redisLog(REDIS_WARNING
,"Guru Meditation: %s #%s:%d",msg
,file
,line
);
420 #ifdef HAVE_BACKTRACE
421 redisLog(REDIS_WARNING
,"(forcing SIGSEGV in order to print the stack trace)");
423 redisLog(REDIS_WARNING
,"------------------------------------------------");
427 void bugReportStart(void) {
428 if (server
.bug_report_start
== 0) {
429 redisLog(REDIS_WARNING
,
430 "\n\n=== REDIS BUG REPORT START: Cut & paste starting from here ===");
431 server
.bug_report_start
= 1;
435 #ifdef HAVE_BACKTRACE
436 static void *getMcontextEip(ucontext_t
*uc
) {
437 #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
439 #if defined(__x86_64__)
440 return (void*) uc
->uc_mcontext
->__ss
.__rip
;
441 #elif defined(__i386__)
442 return (void*) uc
->uc_mcontext
->__ss
.__eip
;
444 return (void*) uc
->uc_mcontext
->__ss
.__srr0
;
446 #elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
448 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
449 return (void*) uc
->uc_mcontext
->__ss
.__rip
;
451 return (void*) uc
->uc_mcontext
->__ss
.__eip
;
453 #elif defined(__linux__)
455 #if defined(__i386__)
456 return (void*) uc
->uc_mcontext
.gregs
[14]; /* Linux 32 */
457 #elif defined(__X86_64__) || defined(__x86_64__)
458 return (void*) uc
->uc_mcontext
.gregs
[16]; /* Linux 64 */
459 #elif defined(__ia64__) /* Linux IA64 */
460 return (void*) uc
->uc_mcontext
.sc_ip
;
467 void logStackContent(void **sp
) {
469 for (i
= 15; i
>= 0; i
--) {
470 if (sizeof(long) == 4)
471 redisLog(REDIS_WARNING
, "(%08lx) -> %08lx", sp
+i
, sp
[i
]);
473 redisLog(REDIS_WARNING
, "(%016lx) -> %016lx", sp
+i
, sp
[i
]);
477 void logRegisters(ucontext_t
*uc
) {
478 redisLog(REDIS_WARNING
, "--- REGISTERS");
481 #if defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
483 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
484 redisLog(REDIS_WARNING
,
486 "RAX:%016lx RBX:%016lx\nRCX:%016lx RDX:%016lx\n"
487 "RDI:%016lx RSI:%016lx\nRBP:%016lx RSP:%016lx\n"
488 "R8 :%016lx R9 :%016lx\nR10:%016lx R11:%016lx\n"
489 "R12:%016lx R13:%016lx\nR14:%016lx R15:%016lx\n"
490 "RIP:%016lx EFL:%016lx\nCS :%016lx FS:%016lx GS:%016lx",
491 uc
->uc_mcontext
->__ss
.__rax
,
492 uc
->uc_mcontext
->__ss
.__rbx
,
493 uc
->uc_mcontext
->__ss
.__rcx
,
494 uc
->uc_mcontext
->__ss
.__rdx
,
495 uc
->uc_mcontext
->__ss
.__rdi
,
496 uc
->uc_mcontext
->__ss
.__rsi
,
497 uc
->uc_mcontext
->__ss
.__rbp
,
498 uc
->uc_mcontext
->__ss
.__rsp
,
499 uc
->uc_mcontext
->__ss
.__r8
,
500 uc
->uc_mcontext
->__ss
.__r9
,
501 uc
->uc_mcontext
->__ss
.__r10
,
502 uc
->uc_mcontext
->__ss
.__r11
,
503 uc
->uc_mcontext
->__ss
.__r12
,
504 uc
->uc_mcontext
->__ss
.__r13
,
505 uc
->uc_mcontext
->__ss
.__r14
,
506 uc
->uc_mcontext
->__ss
.__r15
,
507 uc
->uc_mcontext
->__ss
.__rip
,
508 uc
->uc_mcontext
->__ss
.__rflags
,
509 uc
->uc_mcontext
->__ss
.__cs
,
510 uc
->uc_mcontext
->__ss
.__fs
,
511 uc
->uc_mcontext
->__ss
.__gs
513 logStackContent((void**)uc
->uc_mcontext
->__ss
.__rsp
);
516 redisLog(REDIS_WARNING
,
518 "EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n"
519 "EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\n"
520 "SS:%08lx EFL:%08lx EIP:%08lx CS :%08lx\n"
521 "DS:%08lx ES:%08lx FS :%08lx GS :%08lx",
522 uc
->uc_mcontext
->__ss
.__eax
,
523 uc
->uc_mcontext
->__ss
.__ebx
,
524 uc
->uc_mcontext
->__ss
.__ecx
,
525 uc
->uc_mcontext
->__ss
.__edx
,
526 uc
->uc_mcontext
->__ss
.__edi
,
527 uc
->uc_mcontext
->__ss
.__esi
,
528 uc
->uc_mcontext
->__ss
.__ebp
,
529 uc
->uc_mcontext
->__ss
.__esp
,
530 uc
->uc_mcontext
->__ss
.__ss
,
531 uc
->uc_mcontext
->__ss
.__eflags
,
532 uc
->uc_mcontext
->__ss
.__eip
,
533 uc
->uc_mcontext
->__ss
.__cs
,
534 uc
->uc_mcontext
->__ss
.__ds
,
535 uc
->uc_mcontext
->__ss
.__es
,
536 uc
->uc_mcontext
->__ss
.__fs
,
537 uc
->uc_mcontext
->__ss
.__gs
539 logStackContent((void**)uc
->uc_mcontext
->__ss
.__esp
);
542 #elif defined(__linux__)
544 #if defined(__i386__)
545 redisLog(REDIS_WARNING
,
547 "EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n"
548 "EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\n"
549 "SS :%08lx EFL:%08lx EIP:%08lx CS:%08lx\n"
550 "DS :%08lx ES :%08lx FS :%08lx GS:%08lx",
551 uc
->uc_mcontext
.gregs
[11],
552 uc
->uc_mcontext
.gregs
[8],
553 uc
->uc_mcontext
.gregs
[10],
554 uc
->uc_mcontext
.gregs
[9],
555 uc
->uc_mcontext
.gregs
[4],
556 uc
->uc_mcontext
.gregs
[5],
557 uc
->uc_mcontext
.gregs
[6],
558 uc
->uc_mcontext
.gregs
[7],
559 uc
->uc_mcontext
.gregs
[18],
560 uc
->uc_mcontext
.gregs
[17],
561 uc
->uc_mcontext
.gregs
[14],
562 uc
->uc_mcontext
.gregs
[15],
563 uc
->uc_mcontext
.gregs
[3],
564 uc
->uc_mcontext
.gregs
[2],
565 uc
->uc_mcontext
.gregs
[1],
566 uc
->uc_mcontext
.gregs
[0]
568 logStackContent((void**)uc
->uc_mcontext
.gregs
[7]);
569 #elif defined(__X86_64__) || defined(__x86_64__)
571 redisLog(REDIS_WARNING
,
573 "RAX:%016lx RBX:%016lx\nRCX:%016lx RDX:%016lx\n"
574 "RDI:%016lx RSI:%016lx\nRBP:%016lx RSP:%016lx\n"
575 "R8 :%016lx R9 :%016lx\nR10:%016lx R11:%016lx\n"
576 "R12:%016lx R13:%016lx\nR14:%016lx R15:%016lx\n"
577 "RIP:%016lx EFL:%016lx\nCSGSFS:%016lx",
578 uc
->uc_mcontext
.gregs
[13],
579 uc
->uc_mcontext
.gregs
[11],
580 uc
->uc_mcontext
.gregs
[14],
581 uc
->uc_mcontext
.gregs
[12],
582 uc
->uc_mcontext
.gregs
[8],
583 uc
->uc_mcontext
.gregs
[9],
584 uc
->uc_mcontext
.gregs
[10],
585 uc
->uc_mcontext
.gregs
[15],
586 uc
->uc_mcontext
.gregs
[0],
587 uc
->uc_mcontext
.gregs
[1],
588 uc
->uc_mcontext
.gregs
[2],
589 uc
->uc_mcontext
.gregs
[3],
590 uc
->uc_mcontext
.gregs
[4],
591 uc
->uc_mcontext
.gregs
[5],
592 uc
->uc_mcontext
.gregs
[6],
593 uc
->uc_mcontext
.gregs
[7],
594 uc
->uc_mcontext
.gregs
[16],
595 uc
->uc_mcontext
.gregs
[17],
596 uc
->uc_mcontext
.gregs
[18]
598 logStackContent((void**)uc
->uc_mcontext
.gregs
[15]);
601 redisLog(REDIS_WARNING
,
602 " Dumping of registers not supported for this OS/arch");
606 /* Logs the stack trace using the backtrace() call. This function is designed
607 * to be called from signal handlers safely. */
608 void logStackTrace(ucontext_t
*uc
) {
610 int trace_size
= 0, fd
;
612 /* Open the log file in append mode. */
613 fd
= server
.logfile
?
614 open(server
.logfile
, O_APPEND
|O_CREAT
|O_WRONLY
, 0644) :
616 if (fd
== -1) return;
618 /* Generate the stack trace */
619 trace_size
= backtrace(trace
, 100);
621 /* overwrite sigaction with caller's address */
622 if (getMcontextEip(uc
) != NULL
)
623 trace
[1] = getMcontextEip(uc
);
625 /* Write symbols to log file */
626 backtrace_symbols_fd(trace
, trace_size
, fd
);
629 if (server
.logfile
) close(fd
);
632 /* Log information about the "current" client, that is, the client that is
633 * currently being served by Redis. May be NULL if Redis is not serving a
634 * client right now. */
635 void logCurrentClient(void) {
636 if (server
.current_client
== NULL
) return;
638 redisClient
*cc
= server
.current_client
;
642 redisLog(REDIS_WARNING
, "--- CURRENT CLIENT INFO");
643 client
= getClientInfoString(cc
);
644 redisLog(REDIS_WARNING
,"client: %s", client
);
646 for (j
= 0; j
< cc
->argc
; j
++) {
649 decoded
= getDecodedObject(cc
->argv
[j
]);
650 redisLog(REDIS_WARNING
,"argv[%d]: '%s'", j
, (char*)decoded
->ptr
);
651 decrRefCount(decoded
);
653 /* Check if the first argument, usually a key, is found inside the
654 * selected DB, and if so print info about the associated object. */
659 key
= getDecodedObject(cc
->argv
[1]);
660 de
= dictFind(cc
->db
->dict
, key
->ptr
);
662 val
= dictGetVal(de
);
663 redisLog(REDIS_WARNING
,"key '%s' found in DB containing the following object:", key
->ptr
);
664 redisLogObjectDebugInfo(val
);
670 #if defined(HAVE_PROC_MAPS)
671 void memtest_non_destructive_invert(void *addr
, size_t size
);
672 void memtest_non_destructive_swap(void *addr
, size_t size
);
673 #define MEMTEST_MAX_REGIONS 128
675 int memtest_test_linux_anonymous_maps(void) {
676 FILE *fp
= fopen("/proc/self/maps","r");
678 size_t start_addr
, end_addr
, size
;
679 size_t start_vect
[MEMTEST_MAX_REGIONS
];
680 size_t size_vect
[MEMTEST_MAX_REGIONS
];
682 uint64_t crc1
= 0, crc2
= 0, crc3
= 0;
684 while(fgets(line
,sizeof(line
),fp
) != NULL
) {
685 char *start
, *end
, *p
= line
;
695 if (strstr(p
,"stack") ||
697 strstr(p
,"vsyscall")) continue;
698 if (!strstr(p
,"00:00")) continue;
699 if (!strstr(p
,"rw")) continue;
701 start_addr
= strtoul(start
,NULL
,16);
702 end_addr
= strtoul(end
,NULL
,16);
703 size
= end_addr
-start_addr
;
705 start_vect
[regions
] = start_addr
;
706 size_vect
[regions
] = size
;
707 printf("Testing %lx %lu\n", start_vect
[regions
], size_vect
[regions
]);
711 /* Test all the regions as an unique sequential region.
712 * 1) Take the CRC64 of the memory region. */
713 for (j
= 0; j
< regions
; j
++) {
714 crc1
= crc64(crc1
,(void*)start_vect
[j
],size_vect
[j
]);
717 /* 2) Invert bits, swap adiacent words, swap again, invert bits.
718 * This is the error amplification step. */
719 for (j
= 0; j
< regions
; j
++)
720 memtest_non_destructive_invert((void*)start_vect
[j
],size_vect
[j
]);
721 for (j
= 0; j
< regions
; j
++)
722 memtest_non_destructive_swap((void*)start_vect
[j
],size_vect
[j
]);
723 for (j
= 0; j
< regions
; j
++)
724 memtest_non_destructive_swap((void*)start_vect
[j
],size_vect
[j
]);
725 for (j
= 0; j
< regions
; j
++)
726 memtest_non_destructive_invert((void*)start_vect
[j
],size_vect
[j
]);
728 /* 3) Take the CRC64 sum again. */
729 for (j
= 0; j
< regions
; j
++)
730 crc2
= crc64(crc2
,(void*)start_vect
[j
],size_vect
[j
]);
732 /* 4) Swap + Swap again */
733 for (j
= 0; j
< regions
; j
++)
734 memtest_non_destructive_swap((void*)start_vect
[j
],size_vect
[j
]);
735 for (j
= 0; j
< regions
; j
++)
736 memtest_non_destructive_swap((void*)start_vect
[j
],size_vect
[j
]);
738 /* 5) Take the CRC64 sum again. */
739 for (j
= 0; j
< regions
; j
++)
740 crc3
= crc64(crc3
,(void*)start_vect
[j
],size_vect
[j
]);
742 /* NOTE: It is very important to close the file descriptor only now
743 * because closing it before may result into unmapping of some memory
744 * region that we are testing. */
747 /* If the two CRC are not the same, we trapped a memory error. */
748 return crc1
!= crc2
|| crc2
!= crc3
;
752 void sigsegvHandler(int sig
, siginfo_t
*info
, void *secret
) {
753 ucontext_t
*uc
= (ucontext_t
*) secret
;
754 sds infostring
, clients
;
755 struct sigaction act
;
759 redisLog(REDIS_WARNING
,
760 " Redis %s crashed by signal: %d", REDIS_VERSION
, sig
);
761 redisLog(REDIS_WARNING
,
762 " Failed assertion: %s (%s:%d)", server
.assert_failed
,
763 server
.assert_file
, server
.assert_line
);
765 /* Log the stack trace */
766 redisLog(REDIS_WARNING
, "--- STACK TRACE");
769 /* Log INFO and CLIENT LIST */
770 redisLog(REDIS_WARNING
, "--- INFO OUTPUT");
771 infostring
= genRedisInfoString("all");
772 infostring
= sdscatprintf(infostring
, "hash_init_value: %u\n",
773 dictGetHashFunctionSeed());
774 redisLogRaw(REDIS_WARNING
, infostring
);
775 redisLog(REDIS_WARNING
, "--- CLIENT LIST OUTPUT");
776 clients
= getAllClientsInfoString();
777 redisLogRaw(REDIS_WARNING
, clients
);
781 /* Log the current client */
784 /* Log dump of processor registers */
787 #if defined(HAVE_PROC_MAPS)
789 redisLog(REDIS_WARNING
, "--- FAST MEMORY TEST");
791 if (memtest_test_linux_anonymous_maps()) {
792 redisLog(REDIS_WARNING
,
793 "!!! MEMORY ERROR DETECTED! Check your memory ASAP !!!");
795 redisLog(REDIS_WARNING
,
796 "Fast memory test PASSED, however your memory can still be broken. Please run a memory test for several hours if possible.");
800 redisLog(REDIS_WARNING
,
801 "\n=== REDIS BUG REPORT END. Make sure to include from START to END. ===\n\n"
802 " Please report the crash opening an issue on github:\n\n"
803 " http://github.com/antirez/redis/issues\n\n"
804 " Suspect RAM error? Use redis-server --test-memory to veryfy it.\n\n"
806 /* free(messages); Don't call free() with possibly corrupted memory. */
807 if (server
.daemonize
) unlink(server
.pidfile
);
809 /* Make sure we exit with the right signal at the end. So for instance
810 * the core will be dumped if enabled. */
811 sigemptyset (&act
.sa_mask
);
812 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
;
813 act
.sa_handler
= SIG_DFL
;
814 sigaction (sig
, &act
, NULL
);
817 #endif /* HAVE_BACKTRACE */
819 /* ==================== Logging functions for debugging ===================== */
821 void redisLogHexDump(int level
, char *descr
, void *value
, size_t len
) {
823 unsigned char *v
= value
;
824 char charset
[] = "0123456789abcdef";
826 redisLog(level
,"%s (hexdump):", descr
);
829 b
[0] = charset
[(*v
)>>4];
830 b
[1] = charset
[(*v
)&0xf];
835 if (b
-buf
== 64 || len
== 0) {
836 redisLogRaw(level
|REDIS_LOG_RAW
,buf
);
840 redisLogRaw(level
|REDIS_LOG_RAW
,"\n");
843 /* =========================== Software Watchdog ============================ */
844 #include <sys/time.h>
846 void watchdogSignalHandler(int sig
, siginfo_t
*info
, void *secret
) {
847 #ifdef HAVE_BACKTRACE
848 ucontext_t
*uc
= (ucontext_t
*) secret
;
853 redisLogFromHandler(REDIS_WARNING
,"\n--- WATCHDOG TIMER EXPIRED ---");
854 #ifdef HAVE_BACKTRACE
857 redisLogFromHandler(REDIS_WARNING
,"Sorry: no support for backtrace().");
859 redisLogFromHandler(REDIS_WARNING
,"--------\n");
862 /* Schedule a SIGALRM delivery after the specified period in milliseconds.
863 * If a timer is already scheduled, this function will re-schedule it to the
864 * specified time. If period is 0 the current timer is disabled. */
865 void watchdogScheduleSignal(int period
) {
868 /* Will stop the timer if period is 0. */
869 it
.it_value
.tv_sec
= period
/1000;
870 it
.it_value
.tv_usec
= (period%1000
)*1000;
871 /* Don't automatically restart. */
872 it
.it_interval
.tv_sec
= 0;
873 it
.it_interval
.tv_usec
= 0;
874 setitimer(ITIMER_REAL
, &it
, NULL
);
877 /* Enable the software watchdog with the specified period in milliseconds. */
878 void enableWatchdog(int period
) {
881 if (server
.watchdog_period
== 0) {
882 struct sigaction act
;
884 /* Watchdog was actually disabled, so we have to setup the signal
886 sigemptyset(&act
.sa_mask
);
887 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_SIGINFO
;
888 act
.sa_sigaction
= watchdogSignalHandler
;
889 sigaction(SIGALRM
, &act
, NULL
);
891 /* If the configured period is smaller than twice the timer period, it is
892 * too short for the software watchdog to work reliably. Fix it now
894 min_period
= (1000/REDIS_HZ
)*2;
895 if (period
< min_period
) period
= min_period
;
896 watchdogScheduleSignal(period
); /* Adjust the current timer. */
897 server
.watchdog_period
= period
;
900 /* Disable the software watchdog. */
901 void disableWatchdog(void) {
902 struct sigaction act
;
903 if (server
.watchdog_period
== 0) return; /* Already disabled. */
904 watchdogScheduleSignal(0); /* Stop the current timer. */
906 /* Set the signal handler to SIG_IGN, this will also remove pending
907 * signals from the queue. */
908 sigemptyset(&act
.sa_mask
);
910 act
.sa_handler
= SIG_IGN
;
911 sigaction(SIGALRM
, &act
, NULL
);
912 server
.watchdog_period
= 0;