]> git.saurik.com Git - redis.git/blame - src/debug.c
Don't use an alternative stack for SIGSEGV & co.
[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>
d4d20859 5#include <signal.h>
6
7#ifdef HAVE_BACKTRACE
8#include <execinfo.h>
9#include <ucontext.h>
10#endif /* HAVE_BACKTRACE */
3688d7f3 11
e2641e09 12/* ================================= Debugging ============================== */
13
14/* Compute the sha1 of string at 's' with 'len' bytes long.
15 * The SHA1 is then xored againt the string pointed by digest.
16 * Since xor is commutative, this operation is used in order to
17 * "add" digests relative to unordered elements.
18 *
19 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
20void xorDigest(unsigned char *digest, void *ptr, size_t len) {
21 SHA1_CTX ctx;
22 unsigned char hash[20], *s = ptr;
23 int j;
24
25 SHA1Init(&ctx);
26 SHA1Update(&ctx,s,len);
27 SHA1Final(hash,&ctx);
28
29 for (j = 0; j < 20; j++)
30 digest[j] ^= hash[j];
31}
32
33void xorObjectDigest(unsigned char *digest, robj *o) {
34 o = getDecodedObject(o);
35 xorDigest(digest,o->ptr,sdslen(o->ptr));
36 decrRefCount(o);
37}
38
39/* This function instead of just computing the SHA1 and xoring it
40 * against diget, also perform the digest of "digest" itself and
41 * replace the old value with the new one.
42 *
43 * So the final digest will be:
44 *
45 * digest = SHA1(digest xor SHA1(data))
46 *
47 * This function is used every time we want to preserve the order so
48 * that digest(a,b,c,d) will be different than digest(b,c,d,a)
49 *
50 * Also note that mixdigest("foo") followed by mixdigest("bar")
51 * will lead to a different digest compared to "fo", "obar".
52 */
53void mixDigest(unsigned char *digest, void *ptr, size_t len) {
54 SHA1_CTX ctx;
55 char *s = ptr;
56
57 xorDigest(digest,s,len);
58 SHA1Init(&ctx);
59 SHA1Update(&ctx,digest,20);
60 SHA1Final(digest,&ctx);
61}
62
63void mixObjectDigest(unsigned char *digest, robj *o) {
64 o = getDecodedObject(o);
65 mixDigest(digest,o->ptr,sdslen(o->ptr));
66 decrRefCount(o);
67}
68
69/* Compute the dataset digest. Since keys, sets elements, hashes elements
70 * are not ordered, we use a trick: every aggregate digest is the xor
71 * of the digests of their elements. This way the order will not change
72 * the result. For list instead we use a feedback entering the output digest
73 * as input in order to ensure that a different ordered list will result in
74 * a different digest. */
75void computeDatasetDigest(unsigned char *final) {
76 unsigned char digest[20];
77 char buf[128];
78 dictIterator *di = NULL;
79 dictEntry *de;
80 int j;
81 uint32_t aux;
82
83 memset(final,0,20); /* Start with a clean result */
84
85 for (j = 0; j < server.dbnum; j++) {
86 redisDb *db = server.db+j;
87
88 if (dictSize(db->dict) == 0) continue;
89 di = dictGetIterator(db->dict);
90
91 /* hash the DB id, so the same dataset moved in a different
92 * DB will lead to a different digest */
93 aux = htonl(j);
94 mixDigest(final,&aux,sizeof(aux));
95
96 /* Iterate this DB writing every entry */
97 while((de = dictNext(di)) != NULL) {
98 sds key;
99 robj *keyobj, *o;
4be855e7 100 long long expiretime;
e2641e09 101
102 memset(digest,0,20); /* This key-val digest */
c0ba9ebe 103 key = dictGetKey(de);
e2641e09 104 keyobj = createStringObject(key,sdslen(key));
105
106 mixDigest(digest,key,sdslen(key));
107
c0ba9ebe 108 o = dictGetVal(de);
e2641e09 109
110 aux = htonl(o->type);
111 mixDigest(digest,&aux,sizeof(aux));
112 expiretime = getExpire(db,keyobj);
113
114 /* Save the key and associated value */
115 if (o->type == REDIS_STRING) {
116 mixObjectDigest(digest,o);
117 } else if (o->type == REDIS_LIST) {
118 listTypeIterator *li = listTypeInitIterator(o,0,REDIS_TAIL);
119 listTypeEntry entry;
120 while(listTypeNext(li,&entry)) {
121 robj *eleobj = listTypeGet(&entry);
122 mixObjectDigest(digest,eleobj);
123 decrRefCount(eleobj);
124 }
125 listTypeReleaseIterator(li);
126 } else if (o->type == REDIS_SET) {
cb72d0f1 127 setTypeIterator *si = setTypeInitIterator(o);
2767f1c0 128 robj *ele;
1b508da7 129 while((ele = setTypeNextObject(si)) != NULL) {
2767f1c0
PN
130 xorObjectDigest(digest,ele);
131 decrRefCount(ele);
e2641e09 132 }
2767f1c0 133 setTypeReleaseIterator(si);
e2641e09 134 } else if (o->type == REDIS_ZSET) {
dddf5335
PN
135 unsigned char eledigest[20];
136
137 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
138 unsigned char *zl = o->ptr;
139 unsigned char *eptr, *sptr;
140 unsigned char *vstr;
141 unsigned int vlen;
142 long long vll;
143 double score;
144
145 eptr = ziplistIndex(zl,0);
146 redisAssert(eptr != NULL);
147 sptr = ziplistNext(zl,eptr);
148 redisAssert(sptr != NULL);
149
150 while (eptr != NULL) {
151 redisAssert(ziplistGet(eptr,&vstr,&vlen,&vll));
152 score = zzlGetScore(sptr);
153
154 memset(eledigest,0,20);
155 if (vstr != NULL) {
156 mixDigest(eledigest,vstr,vlen);
157 } else {
158 ll2string(buf,sizeof(buf),vll);
159 mixDigest(eledigest,buf,strlen(buf));
160 }
161
162 snprintf(buf,sizeof(buf),"%.17g",score);
163 mixDigest(eledigest,buf,strlen(buf));
164 xorDigest(digest,eledigest,20);
165 zzlNext(zl,&eptr,&sptr);
166 }
100ed062 167 } else if (o->encoding == REDIS_ENCODING_SKIPLIST) {
dddf5335
PN
168 zset *zs = o->ptr;
169 dictIterator *di = dictGetIterator(zs->dict);
170 dictEntry *de;
171
172 while((de = dictNext(di)) != NULL) {
c0ba9ebe 173 robj *eleobj = dictGetKey(de);
174 double *score = dictGetVal(de);
dddf5335
PN
175
176 snprintf(buf,sizeof(buf),"%.17g",*score);
177 memset(eledigest,0,20);
178 mixObjectDigest(eledigest,eleobj);
179 mixDigest(eledigest,buf,strlen(buf));
180 xorDigest(digest,eledigest,20);
181 }
182 dictReleaseIterator(di);
183 } else {
184 redisPanic("Unknown sorted set encoding");
e2641e09 185 }
e2641e09 186 } else if (o->type == REDIS_HASH) {
187 hashTypeIterator *hi;
188 robj *obj;
189
190 hi = hashTypeInitIterator(o);
191 while (hashTypeNext(hi) != REDIS_ERR) {
192 unsigned char eledigest[20];
193
194 memset(eledigest,0,20);
8c304be3 195 obj = hashTypeCurrentObject(hi,REDIS_HASH_KEY);
e2641e09 196 mixObjectDigest(eledigest,obj);
197 decrRefCount(obj);
8c304be3 198 obj = hashTypeCurrentObject(hi,REDIS_HASH_VALUE);
e2641e09 199 mixObjectDigest(eledigest,obj);
200 decrRefCount(obj);
201 xorDigest(digest,eledigest,20);
202 }
203 hashTypeReleaseIterator(hi);
204 } else {
205 redisPanic("Unknown object type");
206 }
207 /* If the key has an expire, add it to the mix */
208 if (expiretime != -1) xorDigest(digest,"!!expire!!",10);
209 /* We can finally xor the key-val digest to the final digest */
210 xorDigest(final,digest,20);
211 decrRefCount(keyobj);
212 }
213 dictReleaseIterator(di);
214 }
215}
216
217void debugCommand(redisClient *c) {
218 if (!strcasecmp(c->argv[1]->ptr,"segfault")) {
219 *((char*)-1) = 'x';
e3e69935 220 } else if (!strcasecmp(c->argv[1]->ptr,"assert")) {
221 if (c->argc >= 3) c->argv[2] = tryObjectEncoding(c->argv[2]);
bab205f7 222 redisAssertWithInfo(c,c->argv[0],1 == 2);
e2641e09 223 } else if (!strcasecmp(c->argv[1]->ptr,"reload")) {
f48cd4b9 224 if (rdbSave(server.rdb_filename) != REDIS_OK) {
e2641e09 225 addReply(c,shared.err);
226 return;
227 }
228 emptyDb();
f48cd4b9 229 if (rdbLoad(server.rdb_filename) != REDIS_OK) {
80ad7189 230 addReplyError(c,"Error trying to load the RDB dump");
e2641e09 231 return;
232 }
233 redisLog(REDIS_WARNING,"DB reloaded by DEBUG RELOAD");
234 addReply(c,shared.ok);
235 } else if (!strcasecmp(c->argv[1]->ptr,"loadaof")) {
236 emptyDb();
2c915bcf 237 if (loadAppendOnlyFile(server.aof_filename) != REDIS_OK) {
e2641e09 238 addReply(c,shared.err);
239 return;
240 }
efb60225 241 server.dirty = 0; /* Prevent AOF / replication */
e2641e09 242 redisLog(REDIS_WARNING,"Append Only File loaded by DEBUG LOADAOF");
243 addReply(c,shared.ok);
244 } else if (!strcasecmp(c->argv[1]->ptr,"object") && c->argc == 3) {
b39619d8 245 dictEntry *de;
e2641e09 246 robj *val;
5ef64098 247 char *strenc;
e2641e09 248
b39619d8 249 if ((de = dictFind(c->db->dict,c->argv[2]->ptr)) == NULL) {
e2641e09 250 addReply(c,shared.nokeyerr);
251 return;
252 }
c0ba9ebe 253 val = dictGetVal(de);
5ef64098 254 strenc = strEncoding(val->encoding);
3be00d7e 255
5ef64098 256 addReplyStatusFormat(c,
257 "Value at:%p refcount:%d "
258 "encoding:%s serializedlength:%lld "
3be00d7e 259 "lru:%d lru_seconds_idle:%lu",
5ef64098 260 (void*)val, val->refcount,
261 strenc, (long long) rdbSavedObjectLen(val),
3be00d7e 262 val->lru, estimateObjectIdleTime(val));
e2641e09 263 } else if (!strcasecmp(c->argv[1]->ptr,"populate") && c->argc == 3) {
264 long keys, j;
265 robj *key, *val;
266 char buf[128];
267
268 if (getLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != REDIS_OK)
269 return;
270 for (j = 0; j < keys; j++) {
271 snprintf(buf,sizeof(buf),"key:%lu",j);
272 key = createStringObject(buf,strlen(buf));
273 if (lookupKeyRead(c->db,key) != NULL) {
274 decrRefCount(key);
275 continue;
276 }
277 snprintf(buf,sizeof(buf),"value:%lu",j);
278 val = createStringObject(buf,strlen(buf));
279 dbAdd(c->db,key,val);
280 decrRefCount(key);
281 }
282 addReply(c,shared.ok);
283 } else if (!strcasecmp(c->argv[1]->ptr,"digest") && c->argc == 2) {
284 unsigned char digest[20];
3ab20376 285 sds d = sdsempty();
e2641e09 286 int j;
287
288 computeDatasetDigest(digest);
289 for (j = 0; j < 20; j++)
290 d = sdscatprintf(d, "%02x",digest[j]);
3ab20376
PN
291 addReplyStatus(c,d);
292 sdsfree(d);
404345d8 293 } else if (!strcasecmp(c->argv[1]->ptr,"sleep") && c->argc == 3) {
294 double dtime = strtod(c->argv[2]->ptr,NULL);
295 long long utime = dtime*1000000;
296
297 usleep(utime);
298 addReply(c,shared.ok);
e2641e09 299 } else {
3ab20376
PN
300 addReplyError(c,
301 "Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]");
e2641e09 302 }
303}
304
d4d20859 305/* =========================== Crash handling ============================== */
306
e2641e09 307void _redisAssert(char *estr, char *file, int line) {
fa5af017 308 bugReportStart();
e2641e09 309 redisLog(REDIS_WARNING,"=== ASSERTION FAILED ===");
310 redisLog(REDIS_WARNING,"==> %s:%d '%s' is not true",file,line,estr);
311#ifdef HAVE_BACKTRACE
fa5af017 312 server.assert_failed = estr;
313 server.assert_file = file;
314 server.assert_line = line;
315 redisLog(REDIS_WARNING,"(forcing SIGSEGV to print the bug report.)");
e2641e09 316#endif
57be4775 317 *((char*)-1) = 'x';
e2641e09 318}
319
e3e69935 320void _redisAssertPrintClientInfo(redisClient *c) {
bab205f7 321 int j;
e3e69935 322
fa5af017 323 bugReportStart();
bab205f7 324 redisLog(REDIS_WARNING,"=== ASSERTION FAILED CLIENT CONTEXT ===");
325 redisLog(REDIS_WARNING,"client->flags = %d", c->flags);
326 redisLog(REDIS_WARNING,"client->fd = %d", c->fd);
327 redisLog(REDIS_WARNING,"client->argc = %d", c->argc);
328 for (j=0; j < c->argc; j++) {
329 char buf[128];
330 char *arg;
331
332 if (c->argv[j]->type == REDIS_STRING &&
333 c->argv[j]->encoding == REDIS_ENCODING_RAW)
334 {
335 arg = (char*) c->argv[j]->ptr;
336 } else {
337 snprintf(buf,sizeof(buf),"Object type: %d, encoding: %d",
338 c->argv[j]->type, c->argv[j]->encoding);
339 arg = buf;
e3e69935 340 }
bab205f7 341 redisLog(REDIS_WARNING,"client->argv[%d] = \"%s\" (refcount: %d)",
342 j, arg, c->argv[j]->refcount);
343 }
344}
345
00010fa9 346void redisLogObjectDebugInfo(robj *o) {
bab205f7 347 redisLog(REDIS_WARNING,"Object type: %d", o->type);
348 redisLog(REDIS_WARNING,"Object encoding: %d", o->encoding);
349 redisLog(REDIS_WARNING,"Object refcount: %d", o->refcount);
350 if (o->type == REDIS_STRING && o->encoding == REDIS_ENCODING_RAW) {
351 redisLog(REDIS_WARNING,"Object raw string len: %d", sdslen(o->ptr));
352 if (sdslen(o->ptr) < 4096)
353 redisLog(REDIS_WARNING,"Object raw string content: \"%s\"", (char*)o->ptr);
00010fa9 354 } else if (o->type == REDIS_LIST) {
355 redisLog(REDIS_WARNING,"List length: %d", (int) listTypeLength(o));
356 } else if (o->type == REDIS_SET) {
357 redisLog(REDIS_WARNING,"Set size: %d", (int) setTypeSize(o));
358 } else if (o->type == REDIS_HASH) {
359 redisLog(REDIS_WARNING,"Hash size: %d", (int) hashTypeLength(o));
360 } else if (o->type == REDIS_ZSET) {
361 redisLog(REDIS_WARNING,"Sorted set size: %d", (int) zsetLength(o));
362 if (o->encoding == REDIS_ENCODING_SKIPLIST)
363 redisLog(REDIS_WARNING,"Skiplist level: %d", (int) ((zset*)o->ptr)->zsl->level);
e3e69935 364 }
365}
366
00010fa9 367void _redisAssertPrintObject(robj *o) {
368 bugReportStart();
369 redisLog(REDIS_WARNING,"=== ASSERTION FAILED OBJECT CONTEXT ===");
370 redisLogObjectDebugInfo(o);
371}
372
bab205f7 373void _redisAssertWithInfo(redisClient *c, robj *o, char *estr, char *file, int line) {
374 if (c) _redisAssertPrintClientInfo(c);
375 if (o) _redisAssertPrintObject(o);
e3e69935 376 _redisAssert(estr,file,line);
377}
378
e2641e09 379void _redisPanic(char *msg, char *file, int line) {
fa5af017 380 bugReportStart();
120b9ba8 381 redisLog(REDIS_WARNING,"------------------------------------------------");
e2641e09 382 redisLog(REDIS_WARNING,"!!! Software Failure. Press left mouse button to continue");
383 redisLog(REDIS_WARNING,"Guru Meditation: %s #%s:%d",msg,file,line);
384#ifdef HAVE_BACKTRACE
385 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
57be4775 386#endif
120b9ba8 387 redisLog(REDIS_WARNING,"------------------------------------------------");
e2641e09 388 *((char*)-1) = 'x';
e2641e09 389}
d4d20859 390
ac834d23 391void bugReportStart(void) {
392 if (server.bug_report_start == 0) {
393 redisLog(REDIS_WARNING,
394 "\n\n=== REDIS BUG REPORT START: Cut & paste starting from here ===");
395 server.bug_report_start = 1;
396 }
397}
398
d4d20859 399#ifdef HAVE_BACKTRACE
400static void *getMcontextEip(ucontext_t *uc) {
a66a4963 401#if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
402 /* OSX < 10.6 */
403 #if defined(__x86_64__)
d4d20859 404 return (void*) uc->uc_mcontext->__ss.__rip;
a66a4963 405 #elif defined(__i386__)
d4d20859 406 return (void*) uc->uc_mcontext->__ss.__eip;
a66a4963 407 #else
d4d20859 408 return (void*) uc->uc_mcontext->__ss.__srr0;
a66a4963 409 #endif
d4d20859 410#elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
a66a4963 411 /* OSX >= 10.6 */
412 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
d4d20859 413 return (void*) uc->uc_mcontext->__ss.__rip;
a66a4963 414 #else
d4d20859 415 return (void*) uc->uc_mcontext->__ss.__eip;
a66a4963 416 #endif
417#elif defined(__linux__)
418 /* Linux */
419 #if defined(__i386__)
d4d20859 420 return (void*) uc->uc_mcontext.gregs[14]; /* Linux 32 */
a66a4963 421 #elif defined(__X86_64__) || defined(__x86_64__)
d4d20859 422 return (void*) uc->uc_mcontext.gregs[16]; /* Linux 64 */
a66a4963 423 #elif defined(__ia64__) /* Linux IA64 */
d4d20859 424 return (void*) uc->uc_mcontext.sc_ip;
a66a4963 425 #endif
d4d20859 426#else
427 return NULL;
428#endif
429}
430
d4d20859 431void logStackContent(void **sp) {
432 int i;
433 for (i = 15; i >= 0; i--) {
447ebf3b 434 if (sizeof(long) == 4)
435 redisLog(REDIS_WARNING, "(%08lx) -> %08lx", sp+i, sp[i]);
436 else
437 redisLog(REDIS_WARNING, "(%016lx) -> %016lx", sp+i, sp[i]);
d4d20859 438 }
439}
440
441void logRegisters(ucontext_t *uc) {
442 redisLog(REDIS_WARNING, "--- REGISTERS");
a66a4963 443
444/* OSX */
d4d20859 445#if defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
a66a4963 446 /* OSX AMD64 */
447 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
d4d20859 448 redisLog(REDIS_WARNING,
449 "\n"
447ebf3b 450 "RAX:%016lx RBX:%016lx\nRCX:%016lx RDX:%016lx\n"
451 "RDI:%016lx RSI:%016lx\nRBP:%016lx RSP:%016lx\n"
452 "R8 :%016lx R9 :%016lx\nR10:%016lx R11:%016lx\n"
453 "R12:%016lx R13:%016lx\nR14:%016lx R15:%016lx\n"
454 "RIP:%016lx EFL:%016lx\nCS :%016lx FS:%016lx GS:%016lx",
d4d20859 455 uc->uc_mcontext->__ss.__rax,
456 uc->uc_mcontext->__ss.__rbx,
457 uc->uc_mcontext->__ss.__rcx,
458 uc->uc_mcontext->__ss.__rdx,
459 uc->uc_mcontext->__ss.__rdi,
460 uc->uc_mcontext->__ss.__rsi,
461 uc->uc_mcontext->__ss.__rbp,
462 uc->uc_mcontext->__ss.__rsp,
463 uc->uc_mcontext->__ss.__r8,
464 uc->uc_mcontext->__ss.__r9,
465 uc->uc_mcontext->__ss.__r10,
466 uc->uc_mcontext->__ss.__r11,
467 uc->uc_mcontext->__ss.__r12,
468 uc->uc_mcontext->__ss.__r13,
469 uc->uc_mcontext->__ss.__r14,
470 uc->uc_mcontext->__ss.__r15,
471 uc->uc_mcontext->__ss.__rip,
472 uc->uc_mcontext->__ss.__rflags,
473 uc->uc_mcontext->__ss.__cs,
474 uc->uc_mcontext->__ss.__fs,
475 uc->uc_mcontext->__ss.__gs
476 );
477 logStackContent((void**)uc->uc_mcontext->__ss.__rsp);
a66a4963 478 #else
479 /* OSX x86 */
d4d20859 480 redisLog(REDIS_WARNING,
481 "\n"
447ebf3b 482 "EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n"
483 "EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\n"
484 "SS:%08lx EFL:%08lx EIP:%08lx CS :%08lx\n"
485 "DS:%08lx ES:%08lx FS :%08lx GS :%08lx",
d4d20859 486 uc->uc_mcontext->__ss.__eax,
487 uc->uc_mcontext->__ss.__ebx,
488 uc->uc_mcontext->__ss.__ecx,
489 uc->uc_mcontext->__ss.__edx,
490 uc->uc_mcontext->__ss.__edi,
491 uc->uc_mcontext->__ss.__esi,
492 uc->uc_mcontext->__ss.__ebp,
493 uc->uc_mcontext->__ss.__esp,
494 uc->uc_mcontext->__ss.__ss,
495 uc->uc_mcontext->__ss.__eflags,
496 uc->uc_mcontext->__ss.__eip,
497 uc->uc_mcontext->__ss.__cs,
498 uc->uc_mcontext->__ss.__ds,
499 uc->uc_mcontext->__ss.__es,
500 uc->uc_mcontext->__ss.__fs,
501 uc->uc_mcontext->__ss.__gs
502 );
503 logStackContent((void**)uc->uc_mcontext->__ss.__esp);
a66a4963 504 #endif
505/* Linux */
506#elif defined(__linux__)
507 /* Linux x86 */
508 #if defined(__i386__)
632da605 509 redisLog(REDIS_WARNING,
510 "\n"
447ebf3b 511 "EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n"
512 "EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\n"
513 "SS :%08lx EFL:%08lx EIP:%08lx CS:%08lx\n"
514 "DS :%08lx ES :%08lx FS :%08lx GS:%08lx",
632da605 515 uc->uc_mcontext.gregs[11],
516 uc->uc_mcontext.gregs[8],
517 uc->uc_mcontext.gregs[10],
518 uc->uc_mcontext.gregs[9],
519 uc->uc_mcontext.gregs[4],
520 uc->uc_mcontext.gregs[5],
521 uc->uc_mcontext.gregs[6],
522 uc->uc_mcontext.gregs[7],
523 uc->uc_mcontext.gregs[18],
524 uc->uc_mcontext.gregs[17],
525 uc->uc_mcontext.gregs[14],
526 uc->uc_mcontext.gregs[15],
527 uc->uc_mcontext.gregs[3],
528 uc->uc_mcontext.gregs[2],
529 uc->uc_mcontext.gregs[1],
530 uc->uc_mcontext.gregs[0]
531 );
532 logStackContent((void**)uc->uc_mcontext.gregs[7]);
a66a4963 533 #elif defined(__X86_64__) || defined(__x86_64__)
534 /* Linux AMD64 */
eea8c7a4 535 redisLog(REDIS_WARNING,
536 "\n"
447ebf3b 537 "RAX:%016lx RBX:%016lx\nRCX:%016lx RDX:%016lx\n"
538 "RDI:%016lx RSI:%016lx\nRBP:%016lx RSP:%016lx\n"
539 "R8 :%016lx R9 :%016lx\nR10:%016lx R11:%016lx\n"
540 "R12:%016lx R13:%016lx\nR14:%016lx R15:%016lx\n"
541 "RIP:%016lx EFL:%016lx\nCSGSFS:%016lx",
eea8c7a4 542 uc->uc_mcontext.gregs[13],
543 uc->uc_mcontext.gregs[11],
544 uc->uc_mcontext.gregs[14],
545 uc->uc_mcontext.gregs[12],
546 uc->uc_mcontext.gregs[8],
547 uc->uc_mcontext.gregs[9],
548 uc->uc_mcontext.gregs[10],
549 uc->uc_mcontext.gregs[15],
550 uc->uc_mcontext.gregs[0],
551 uc->uc_mcontext.gregs[1],
552 uc->uc_mcontext.gregs[2],
553 uc->uc_mcontext.gregs[3],
554 uc->uc_mcontext.gregs[4],
555 uc->uc_mcontext.gregs[5],
556 uc->uc_mcontext.gregs[6],
557 uc->uc_mcontext.gregs[7],
558 uc->uc_mcontext.gregs[16],
559 uc->uc_mcontext.gregs[17],
560 uc->uc_mcontext.gregs[18]
561 );
562 logStackContent((void**)uc->uc_mcontext.gregs[15]);
a66a4963 563 #endif
d4d20859 564#else
565 redisLog(REDIS_WARNING,
566 " Dumping of registers not supported for this OS/arch");
567#endif
568}
569
a3238704 570/* Logs the stack trace using the backtrace() call. */
23c0cdd2 571sds getStackTrace(ucontext_t *uc) {
d4d20859 572 void *trace[100];
d4d20859 573 int i, trace_size = 0;
a3238704 574 char **messages = NULL;
23c0cdd2 575 sds st = sdsempty();
d4d20859 576
577 /* Generate the stack trace */
578 trace_size = backtrace(trace, 100);
579
580 /* overwrite sigaction with caller's address */
581 if (getMcontextEip(uc) != NULL) {
582 trace[1] = getMcontextEip(uc);
583 }
584 messages = backtrace_symbols(trace, trace_size);
23c0cdd2 585 for (i=1; i<trace_size; ++i) {
586 st = sdscat(st,messages[i]);
587 st = sdscatlen(st,"\n",1);
588 }
589 zlibc_free(messages);
590 return st;
a3238704 591}
592
593/* Log information about the "current" client, that is, the client that is
594 * currently being served by Redis. May be NULL if Redis is not serving a
595 * client right now. */
596void logCurrentClient(void) {
597 if (server.current_client == NULL) return;
598
599 redisClient *cc = server.current_client;
600 sds client;
601 int j;
602
603 redisLog(REDIS_WARNING, "--- CURRENT CLIENT INFO");
604 client = getClientInfoString(cc);
605 redisLog(REDIS_WARNING,"client: %s", client);
606 sdsfree(client);
607 for (j = 0; j < cc->argc; j++) {
608 robj *decoded;
609
610 decoded = getDecodedObject(cc->argv[j]);
611 redisLog(REDIS_WARNING,"argv[%d]: '%s'", j, (char*)decoded->ptr);
612 decrRefCount(decoded);
613 }
614 /* Check if the first argument, usually a key, is found inside the
615 * selected DB, and if so print info about the associated object. */
616 if (cc->argc >= 1) {
617 robj *val, *key;
618 dictEntry *de;
619
620 key = getDecodedObject(cc->argv[1]);
621 de = dictFind(cc->db->dict, key->ptr);
622 if (de) {
623 val = dictGetVal(de);
624 redisLog(REDIS_WARNING,"key '%s' found in DB containing the following object:", key->ptr);
625 redisLogObjectDebugInfo(val);
626 }
627 decrRefCount(key);
628 }
629}
630
631void sigsegvHandler(int sig, siginfo_t *info, void *secret) {
632 ucontext_t *uc = (ucontext_t*) secret;
23c0cdd2 633 sds infostring, clients, st;
a3238704 634 struct sigaction act;
635 REDIS_NOTUSED(info);
636
637 bugReportStart();
638 redisLog(REDIS_WARNING,
639 " Redis %s crashed by signal: %d", REDIS_VERSION, sig);
640 redisLog(REDIS_WARNING,
641 " Failed assertion: %s (%s:%d)", server.assert_failed,
642 server.assert_file, server.assert_line);
643
644 /* Log the stack trace */
23c0cdd2 645 st = getStackTrace(uc);
646 redisLog(REDIS_WARNING, "--- STACK TRACE\n%s", st);
647 sdsfree(st);
d4d20859 648
649 /* Log INFO and CLIENT LIST */
650 redisLog(REDIS_WARNING, "--- INFO OUTPUT");
651 infostring = genRedisInfoString("all");
a48c8d87 652 infostring = sdscatprintf(infostring, "hash_init_value: %u\n",
653 dictGetHashFunctionSeed());
d4d20859 654 redisLogRaw(REDIS_WARNING, infostring);
655 redisLog(REDIS_WARNING, "--- CLIENT LIST OUTPUT");
656 clients = getAllClientsInfoString();
657 redisLogRaw(REDIS_WARNING, clients);
a3238704 658 sdsfree(infostring);
659 sdsfree(clients);
d4d20859 660
a3238704 661 /* Log the current client */
662 logCurrentClient();
d4d20859 663
664 /* Log dump of processor registers */
665 logRegisters(uc);
666
667 redisLog(REDIS_WARNING,
668"\n=== REDIS BUG REPORT END. Make sure to include from START to END. ===\n\n"
525be599 669" Please report the crash opening an issue on github:\n\n"
670" http://github.com/antirez/redis/issues\n\n"
671" Suspect RAM error? Use redis-server --test-memory to veryfy it.\n\n"
d4d20859 672);
673 /* free(messages); Don't call free() with possibly corrupted memory. */
674 if (server.daemonize) unlink(server.pidfile);
675
676 /* Make sure we exit with the right signal at the end. So for instance
677 * the core will be dumped if enabled. */
678 sigemptyset (&act.sa_mask);
d4d20859 679 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;
680 act.sa_handler = SIG_DFL;
681 sigaction (sig, &act, NULL);
682 kill(getpid(),sig);
683}
684#endif /* HAVE_BACKTRACE */
39bd025c 685
686/* =========================== Software Watchdog ============================ */
687#include <sys/time.h>
688
689void watchdogSignalHandler(int sig, siginfo_t *info, void *secret) {
a66a4963 690#ifdef HAVE_BACKTRACE
39bd025c 691 ucontext_t *uc = (ucontext_t*) secret;
a66a4963 692#endif
39bd025c 693 REDIS_NOTUSED(info);
694 REDIS_NOTUSED(sig);
23c0cdd2 695 sds st, log;
39bd025c 696
a7d12cba 697 log = sdsnew("\n--- WATCHDOG TIMER EXPIRED ---\n");
39bd025c 698#ifdef HAVE_BACKTRACE
23c0cdd2 699 st = getStackTrace(uc);
700#else
701 st = sdsnew("Sorry: no support for backtrace().\n");
39bd025c 702#endif
23c0cdd2 703 log = sdscatsds(log,st);
a7d12cba 704 log = sdscat(log,"------\n");
705 redisLogFromHandler(REDIS_WARNING,log);
23c0cdd2 706 sdsfree(st);
707 sdsfree(log);
39bd025c 708}
709
710/* Schedule a SIGALRM delivery after the specified period in milliseconds.
711 * If a timer is already scheduled, this function will re-schedule it to the
712 * specified time. If period is 0 the current timer is disabled. */
713void watchdogScheduleSignal(int period) {
714 struct itimerval it;
715
716 /* Will stop the timer if period is 0. */
717 it.it_value.tv_sec = period/1000;
a354da9a 718 it.it_value.tv_usec = (period%1000)*1000;
39bd025c 719 /* Don't automatically restart. */
720 it.it_interval.tv_sec = 0;
721 it.it_interval.tv_usec = 0;
722 setitimer(ITIMER_REAL, &it, NULL);
723}
724
725/* Enable the software watchdong with the specified period in milliseconds. */
726void enableWatchdog(int period) {
727 if (server.watchdog_period == 0) {
728 struct sigaction act;
729
730 /* Watchdog was actually disabled, so we have to setup the signal
731 * handler. */
732 sigemptyset(&act.sa_mask);
733 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO;
734 act.sa_sigaction = watchdogSignalHandler;
735 sigaction(SIGALRM, &act, NULL);
736 }
737 if (period < 200) period = 200; /* We don't accept periods < 200 ms. */
738 watchdogScheduleSignal(period); /* Adjust the current timer. */
739 server.watchdog_period = period;
740}
741
742/* Disable the software watchdog. */
743void disableWatchdog(void) {
744 struct sigaction act;
745 if (server.watchdog_period == 0) return; /* Already disabled. */
746 watchdogScheduleSignal(0); /* Stop the current timer. */
747
748 /* Set the signal handler to SIG_IGN, this will also remove pending
749 * signals from the queue. */
750 sigemptyset(&act.sa_mask);
751 act.sa_flags = 0;
752 act.sa_handler = SIG_IGN;
753 sigaction(SIGALRM, &act, NULL);
754 server.watchdog_period = 0;
755}