]>
git.saurik.com Git - redis.git/blob - src/redis-check-dump.c
14 #define REDIS_STRING 0
19 #define REDIS_HASH_ZIPMAP 9
20 #define REDIS_LIST_ZIPLIST 10
21 #define REDIS_SET_INTSET 11
22 #define REDIS_ZSET_ZIPLIST 12
24 /* Objects encoding. Some kind of objects like Strings and Hashes can be
25 * internally represented in multiple ways. The 'encoding' field of the object
26 * is set to one of this fields for this object. */
27 #define REDIS_ENCODING_RAW 0 /* Raw representation */
28 #define REDIS_ENCODING_INT 1 /* Encoded as integer */
29 #define REDIS_ENCODING_ZIPMAP 2 /* Encoded as zipmap */
30 #define REDIS_ENCODING_HT 3 /* Encoded as an hash table */
32 /* Object types only used for dumping to disk */
33 #define REDIS_EXPIRETIME 253
34 #define REDIS_SELECTDB 254
37 /* Defines related to the dump file format. To store 32 bits lengths for short
38 * keys requires a lot of space, so we check the most significant 2 bits of
39 * the first byte to interpreter the length:
41 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
42 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
43 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
44 * 11|000000 this means: specially encoded object will follow. The six bits
45 * number specify the kind of object that follows.
46 * See the REDIS_RDB_ENC_* defines.
48 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
49 * values, will fit inside. */
50 #define REDIS_RDB_6BITLEN 0
51 #define REDIS_RDB_14BITLEN 1
52 #define REDIS_RDB_32BITLEN 2
53 #define REDIS_RDB_ENCVAL 3
54 #define REDIS_RDB_LENERR UINT_MAX
56 /* When a length of a string object stored on disk has the first two bits
57 * set, the remaining two bits specify a special encoding for the object
58 * accordingly to the following defines: */
59 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
60 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
61 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
62 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
64 #define ERROR(...) { \
65 printf(__VA_ARGS__); \
69 /* data type to hold offset in file and size */
76 static unsigned char level
= 0;
77 static pos positions
[16];
79 #define CURR_OFFSET (positions[level].offset)
81 /* Hold a stack of errors */
87 static errors_t errors
;
89 #define SHIFT_ERROR(provided_offset, ...) { \
90 sprintf(errors.error[errors.level], __VA_ARGS__); \
91 errors.offset[errors.level] = provided_offset; \
95 /* Data type to hold opcode with optional key name an success status */
102 /* Global vars that are actally used as constants. The following double
103 * values are used for double on-disk serialization, and are initialized
104 * at runtime to avoid strange compiler optimizations. */
105 static double R_Zero
, R_PosInf
, R_NegInf
, R_Nan
;
107 /* store string types for output */
108 static char types
[256][16];
110 /* when number of bytes to read is negative, do a peek */
111 int readBytes(void *target
, long num
) {
112 char peek
= (num
< 0) ? 1 : 0;
113 num
= (num
< 0) ? -num
: num
;
115 pos p
= positions
[level
];
116 if (p
.offset
+ num
> p
.size
) {
119 memcpy(target
, (void*)((size_t)p
.data
+ p
.offset
), num
);
120 if (!peek
) positions
[level
].offset
+= num
;
125 int processHeader() {
126 char buf
[10] = "_________";
129 if (!readBytes(buf
, 9)) {
130 ERROR("Cannot read header\n");
133 /* expect the first 5 bytes to equal REDIS */
134 if (memcmp(buf
,"REDIS",5) != 0) {
135 ERROR("Wrong signature in header\n");
138 dump_version
= (int)strtol(buf
+ 5, NULL
, 10);
139 if (dump_version
< 1 || dump_version
> 2) {
140 ERROR("Unknown RDB format version: %d\n", dump_version
);
145 int loadType(entry
*e
) {
146 uint32_t offset
= CURR_OFFSET
;
148 /* this byte needs to qualify as type */
150 if (readBytes(&t
, 1)) {
151 if (t
<= 4 || (t
>=9 && t
<= 12) || t
>= 253) {
155 SHIFT_ERROR(offset
, "Unknown type (0x%02x)", t
);
158 SHIFT_ERROR(offset
, "Could not read type");
167 if (readBytes(&t
, -1) && (t
<= 4 || (t
>=9 && t
<= 12) || t
>= 253))
172 /* discard time, just consume the bytes */
174 uint32_t offset
= CURR_OFFSET
;
176 if (readBytes(t
, 4)) {
179 SHIFT_ERROR(offset
, "Could not read time");
186 uint32_t loadLength(int *isencoded
) {
187 unsigned char buf
[2];
191 if (isencoded
) *isencoded
= 0;
192 if (!readBytes(buf
, 1)) return REDIS_RDB_LENERR
;
193 type
= (buf
[0] & 0xC0) >> 6;
194 if (type
== REDIS_RDB_6BITLEN
) {
195 /* Read a 6 bit len */
196 return buf
[0] & 0x3F;
197 } else if (type
== REDIS_RDB_ENCVAL
) {
198 /* Read a 6 bit len encoding type */
199 if (isencoded
) *isencoded
= 1;
200 return buf
[0] & 0x3F;
201 } else if (type
== REDIS_RDB_14BITLEN
) {
202 /* Read a 14 bit len */
203 if (!readBytes(buf
+1,1)) return REDIS_RDB_LENERR
;
204 return ((buf
[0] & 0x3F) << 8) | buf
[1];
206 /* Read a 32 bit len */
207 if (!readBytes(&len
, 4)) return REDIS_RDB_LENERR
;
208 return (unsigned int)ntohl(len
);
212 char *loadIntegerObject(int enctype
) {
213 uint32_t offset
= CURR_OFFSET
;
214 unsigned char enc
[4];
217 if (enctype
== REDIS_RDB_ENC_INT8
) {
219 if (!readBytes(enc
, 1)) return NULL
;
222 } else if (enctype
== REDIS_RDB_ENC_INT16
) {
224 if (!readBytes(enc
, 2)) return NULL
;
225 v
= enc
[0]|(enc
[1]<<8);
227 } else if (enctype
== REDIS_RDB_ENC_INT32
) {
229 if (!readBytes(enc
, 4)) return NULL
;
230 v
= enc
[0]|(enc
[1]<<8)|(enc
[2]<<16)|(enc
[3]<<24);
233 SHIFT_ERROR(offset
, "Unknown integer encoding (0x%02x)", enctype
);
237 /* convert val into string */
239 buf
= malloc(sizeof(char) * 128);
240 sprintf(buf
, "%lld", val
);
244 char* loadLzfStringObject() {
245 unsigned int slen
, clen
;
248 if ((clen
= loadLength(NULL
)) == REDIS_RDB_LENERR
) return NULL
;
249 if ((slen
= loadLength(NULL
)) == REDIS_RDB_LENERR
) return NULL
;
252 if (!readBytes(c
, clen
)) {
258 if (lzf_decompress(c
,clen
,s
,slen
) == 0) {
267 /* returns NULL when not processable, char* when valid */
268 char* loadStringObject() {
269 uint32_t offset
= CURR_OFFSET
;
273 len
= loadLength(&isencoded
);
276 case REDIS_RDB_ENC_INT8
:
277 case REDIS_RDB_ENC_INT16
:
278 case REDIS_RDB_ENC_INT32
:
279 return loadIntegerObject(len
);
280 case REDIS_RDB_ENC_LZF
:
281 return loadLzfStringObject();
283 /* unknown encoding */
284 SHIFT_ERROR(offset
, "Unknown string encoding (0x%02x)", len
);
289 if (len
== REDIS_RDB_LENERR
) return NULL
;
291 char *buf
= malloc(sizeof(char) * (len
+1));
293 if (!readBytes(buf
, len
)) {
300 int processStringObject(char** store
) {
301 unsigned long offset
= CURR_OFFSET
;
302 char *key
= loadStringObject();
304 SHIFT_ERROR(offset
, "Error reading string object");
317 double* loadDoubleValue() {
322 if (!readBytes(&len
,1)) return NULL
;
324 val
= malloc(sizeof(double));
326 case 255: *val
= R_NegInf
; return val
;
327 case 254: *val
= R_PosInf
; return val
;
328 case 253: *val
= R_Nan
; return val
;
330 if (!readBytes(buf
, len
)) {
335 sscanf(buf
, "%lg", val
);
340 int processDoubleValue(double** store
) {
341 unsigned long offset
= CURR_OFFSET
;
342 double *val
= loadDoubleValue();
344 SHIFT_ERROR(offset
, "Error reading double value");
357 int loadPair(entry
*e
) {
358 uint32_t offset
= CURR_OFFSET
;
363 if (processStringObject(&key
)) {
366 SHIFT_ERROR(offset
, "Error reading entry key");
371 if (e
->type
== REDIS_LIST
||
372 e
->type
== REDIS_SET
||
373 e
->type
== REDIS_ZSET
||
374 e
->type
== REDIS_HASH
) {
375 if ((length
= loadLength(NULL
)) == REDIS_RDB_LENERR
) {
376 SHIFT_ERROR(offset
, "Error reading %s length", types
[e
->type
]);
383 case REDIS_HASH_ZIPMAP
:
384 case REDIS_LIST_ZIPLIST
:
385 case REDIS_SET_INTSET
:
386 case REDIS_ZSET_ZIPLIST
:
387 if (!processStringObject(NULL
)) {
388 SHIFT_ERROR(offset
, "Error reading entry value");
394 for (i
= 0; i
< length
; i
++) {
395 offset
= CURR_OFFSET
;
396 if (!processStringObject(NULL
)) {
397 SHIFT_ERROR(offset
, "Error reading element at index %d (length: %d)", i
, length
);
403 for (i
= 0; i
< length
; i
++) {
404 offset
= CURR_OFFSET
;
405 if (!processStringObject(NULL
)) {
406 SHIFT_ERROR(offset
, "Error reading element key at index %d (length: %d)", i
, length
);
409 offset
= CURR_OFFSET
;
410 if (!processDoubleValue(NULL
)) {
411 SHIFT_ERROR(offset
, "Error reading element value at index %d (length: %d)", i
, length
);
417 for (i
= 0; i
< length
; i
++) {
418 offset
= CURR_OFFSET
;
419 if (!processStringObject(NULL
)) {
420 SHIFT_ERROR(offset
, "Error reading element key at index %d (length: %d)", i
, length
);
423 offset
= CURR_OFFSET
;
424 if (!processStringObject(NULL
)) {
425 SHIFT_ERROR(offset
, "Error reading element value at index %d (length: %d)", i
, length
);
431 SHIFT_ERROR(offset
, "Type not implemented");
434 /* because we're done, we assume success */
440 entry e
= { NULL
, -1, 0 };
441 uint32_t length
, offset
[4];
443 /* reset error container */
446 offset
[0] = CURR_OFFSET
;
451 offset
[1] = CURR_OFFSET
;
452 if (e
.type
== REDIS_SELECTDB
) {
453 if ((length
= loadLength(NULL
)) == REDIS_RDB_LENERR
) {
454 SHIFT_ERROR(offset
[1], "Error reading database number");
458 SHIFT_ERROR(offset
[1], "Database number out of range (%d)", length
);
461 } else if (e
.type
== REDIS_EOF
) {
462 if (positions
[level
].offset
< positions
[level
].size
) {
463 SHIFT_ERROR(offset
[0], "Unexpected EOF");
469 /* optionally consume expire */
470 if (e
.type
== REDIS_EXPIRETIME
) {
471 if (!processTime()) return e
;
472 if (!loadType(&e
)) return e
;
475 offset
[1] = CURR_OFFSET
;
477 SHIFT_ERROR(offset
[1], "Error for type %s", types
[e
.type
]);
482 /* all entries are followed by a valid type:
483 * e.g. a new entry, SELECTDB, EXPIRE, EOF */
484 offset
[2] = CURR_OFFSET
;
485 if (peekType() == -1) {
486 SHIFT_ERROR(offset
[2], "Followed by invalid type");
487 SHIFT_ERROR(offset
[0], "Error for type %s", types
[e
.type
]);
496 void printCentered(int indent
, int width
, char* body
) {
497 char head
[256], tail
[256];
498 memset(head
, '\0', 256);
499 memset(tail
, '\0', 256);
501 memset(head
, '=', indent
);
502 memset(tail
, '=', width
- 2 - indent
- strlen(body
));
503 printf("%s %s %s\n", head
, body
, tail
);
506 void printValid(uint64_t ops
, uint64_t bytes
) {
508 sprintf(body
, "Processed %llu valid opcodes (in %llu bytes)",
509 (unsigned long long) ops
, (unsigned long long) bytes
);
510 printCentered(4, 80, body
);
513 void printSkipped(uint64_t bytes
, uint64_t offset
) {
515 sprintf(body
, "Skipped %llu bytes (resuming at 0x%08llx)",
516 (unsigned long long) bytes
, (unsigned long long) offset
);
517 printCentered(4, 80, body
);
520 void printErrorStack(entry
*e
) {
525 sprintf(body
, "Error trace");
526 } else if (e
->type
>= 253) {
527 sprintf(body
, "Error trace (%s)", types
[e
->type
]);
528 } else if (!e
->key
) {
529 sprintf(body
, "Error trace (%s: (unknown))", types
[e
->type
]);
532 strncpy(tmp
, e
->key
, 40);
534 /* display truncation at the last 3 chars */
535 if (strlen(e
->key
) > 40) {
536 memset(&tmp
[37], '.', 3);
539 /* display unprintable characters as ? */
540 for (i
= 0; i
< strlen(tmp
); i
++) {
541 if (tmp
[i
] <= 32) tmp
[i
] = '?';
543 sprintf(body
, "Error trace (%s: %s)", types
[e
->type
], tmp
);
546 printCentered(4, 80, body
);
548 /* display error stack */
549 for (i
= 0; i
< errors
.level
; i
++) {
550 printf("0x%08lx - %s\n",
551 (unsigned long) errors
.offset
[i
], errors
.error
[i
]);
556 uint64_t num_errors
= 0, num_valid_ops
= 0, num_valid_bytes
= 0;
561 while(positions
[0].offset
< positions
[0].size
) {
562 positions
[1] = positions
[0];
565 if (!entry
.success
) {
566 printValid(num_valid_ops
, num_valid_bytes
);
567 printErrorStack(&entry
);
572 /* search for next valid entry */
573 uint64_t offset
= positions
[0].offset
+ 1;
576 while (!entry
.success
&& offset
< positions
[0].size
) {
577 positions
[1].offset
= offset
;
579 /* find 3 consecutive valid entries */
580 for (i
= 0; i
< 3; i
++) {
582 if (!entry
.success
) break;
584 /* check if we found 3 consecutive valid entries */
590 /* print how many bytes we have skipped to find a new valid opcode */
591 if (offset
< positions
[0].size
) {
592 printSkipped(offset
- positions
[0].offset
, offset
);
595 positions
[0].offset
= offset
;
598 num_valid_bytes
+= positions
[1].offset
- positions
[0].offset
;
600 /* advance position */
601 positions
[0] = positions
[1];
605 /* because there is another potential error,
606 * print how many valid ops we have processed */
607 printValid(num_valid_ops
, num_valid_bytes
);
610 if (entry
.type
!= REDIS_EOF
) {
611 /* last byte should be EOF, add error */
613 SHIFT_ERROR(positions
[0].offset
, "Expected EOF, got %s", types
[entry
.type
]);
615 /* this is an EOF error so reset type */
617 printErrorStack(&entry
);
622 /* print summary on errors */
625 printf("Total unprocessable opcodes: %llu\n",
626 (unsigned long long) num_errors
);
630 int main(int argc
, char **argv
) {
631 /* expect the first argument to be the dump file */
633 printf("Usage: %s <dump.rdb>\n", argv
[0]);
642 fd
= open(argv
[1], O_RDONLY
);
644 ERROR("Cannot open file: %s\n", argv
[1]);
646 if (fstat(fd
, &stat
) == -1) {
647 ERROR("Cannot stat: %s\n", argv
[1]);
652 if (sizeof(size_t) == sizeof(int32_t) && size
>= INT_MAX
) {
653 ERROR("Cannot check dump files >2GB on a 32-bit platform\n");
656 data
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0);
657 if (data
== MAP_FAILED
) {
658 ERROR("Cannot mmap: %s\n", argv
[1]);
661 /* Initialize static vars */
662 positions
[0].data
= data
;
663 positions
[0].size
= size
;
664 positions
[0].offset
= 0;
668 sprintf(types
[REDIS_STRING
], "STRING");
669 sprintf(types
[REDIS_LIST
], "LIST");
670 sprintf(types
[REDIS_SET
], "SET");
671 sprintf(types
[REDIS_ZSET
], "ZSET");
672 sprintf(types
[REDIS_HASH
], "HASH");
674 /* Object types only used for dumping to disk */
675 sprintf(types
[REDIS_EXPIRETIME
], "EXPIRETIME");
676 sprintf(types
[REDIS_SELECTDB
], "SELECTDB");
677 sprintf(types
[REDIS_EOF
], "EOF");
679 /* Double constants initialization */
681 R_PosInf
= 1.0/R_Zero
;
682 R_NegInf
= -1.0/R_Zero
;
683 R_Nan
= R_Zero
/R_Zero
;