]>
git.saurik.com Git - redis.git/blob - src/redis-check-dump.c
2 * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
3 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Redis nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
39 #include <arpa/inet.h>
46 #define REDIS_STRING 0
51 #define REDIS_HASH_ZIPMAP 9
52 #define REDIS_LIST_ZIPLIST 10
53 #define REDIS_SET_INTSET 11
54 #define REDIS_ZSET_ZIPLIST 12
55 #define REDIS_HASH_ZIPLIST 13
57 /* Objects encoding. Some kind of objects like Strings and Hashes can be
58 * internally represented in multiple ways. The 'encoding' field of the object
59 * is set to one of this fields for this object. */
60 #define REDIS_ENCODING_RAW 0 /* Raw representation */
61 #define REDIS_ENCODING_INT 1 /* Encoded as integer */
62 #define REDIS_ENCODING_ZIPMAP 2 /* Encoded as zipmap */
63 #define REDIS_ENCODING_HT 3 /* Encoded as an hash table */
65 /* Object types only used for dumping to disk */
66 #define REDIS_EXPIRETIME_MS 252
67 #define REDIS_EXPIRETIME 253
68 #define REDIS_SELECTDB 254
71 /* Defines related to the dump file format. To store 32 bits lengths for short
72 * keys requires a lot of space, so we check the most significant 2 bits of
73 * the first byte to interpreter the length:
75 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
76 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
77 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
78 * 11|000000 this means: specially encoded object will follow. The six bits
79 * number specify the kind of object that follows.
80 * See the REDIS_RDB_ENC_* defines.
82 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
83 * values, will fit inside. */
84 #define REDIS_RDB_6BITLEN 0
85 #define REDIS_RDB_14BITLEN 1
86 #define REDIS_RDB_32BITLEN 2
87 #define REDIS_RDB_ENCVAL 3
88 #define REDIS_RDB_LENERR UINT_MAX
90 /* When a length of a string object stored on disk has the first two bits
91 * set, the remaining two bits specify a special encoding for the object
92 * accordingly to the following defines: */
93 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
94 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
95 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
96 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
98 #define ERROR(...) { \
99 printf(__VA_ARGS__); \
103 /* data type to hold offset in file and size */
110 static unsigned char level
= 0;
111 static pos positions
[16];
113 #define CURR_OFFSET (positions[level].offset)
115 /* Hold a stack of errors */
117 char error
[16][1024];
121 static errors_t errors
;
123 #define SHIFT_ERROR(provided_offset, ...) { \
124 sprintf(errors.error[errors.level], __VA_ARGS__); \
125 errors.offset[errors.level] = provided_offset; \
129 /* Data type to hold opcode with optional key name an success status */
136 /* Global vars that are actally used as constants. The following double
137 * values are used for double on-disk serialization, and are initialized
138 * at runtime to avoid strange compiler optimizations. */
139 static double R_Zero
, R_PosInf
, R_NegInf
, R_Nan
;
141 /* store string types for output */
142 static char types
[256][16];
144 /* Return true if 't' is a valid object type. */
145 int checkType(unsigned char t
) {
146 /* In case a new object type is added, update the following
147 * condition as necessary. */
149 (t
>= REDIS_HASH_ZIPMAP
&& t
<= REDIS_HASH_ZIPLIST
) ||
151 t
>= REDIS_EXPIRETIME_MS
;
154 /* when number of bytes to read is negative, do a peek */
155 int readBytes(void *target
, long num
) {
156 char peek
= (num
< 0) ? 1 : 0;
157 num
= (num
< 0) ? -num
: num
;
159 pos p
= positions
[level
];
160 if (p
.offset
+ num
> p
.size
) {
163 memcpy(target
, (void*)((size_t)p
.data
+ p
.offset
), num
);
164 if (!peek
) positions
[level
].offset
+= num
;
169 int processHeader() {
170 char buf
[10] = "_________";
173 if (!readBytes(buf
, 9)) {
174 ERROR("Cannot read header\n");
177 /* expect the first 5 bytes to equal REDIS */
178 if (memcmp(buf
,"REDIS",5) != 0) {
179 ERROR("Wrong signature in header\n");
182 dump_version
= (int)strtol(buf
+ 5, NULL
, 10);
183 if (dump_version
< 1 || dump_version
> 6) {
184 ERROR("Unknown RDB format version: %d\n", dump_version
);
189 int loadType(entry
*e
) {
190 uint32_t offset
= CURR_OFFSET
;
192 /* this byte needs to qualify as type */
194 if (readBytes(&t
, 1)) {
199 SHIFT_ERROR(offset
, "Unknown type (0x%02x)", t
);
202 SHIFT_ERROR(offset
, "Could not read type");
211 if (readBytes(&t
, -1) && (checkType(t
)))
216 /* discard time, just consume the bytes */
217 int processTime(int type
) {
218 uint32_t offset
= CURR_OFFSET
;
220 int timelen
= (type
== REDIS_EXPIRETIME_MS
) ? 8 : 4;
222 if (readBytes(t
,timelen
)) {
225 SHIFT_ERROR(offset
, "Could not read time");
232 uint32_t loadLength(int *isencoded
) {
233 unsigned char buf
[2];
237 if (isencoded
) *isencoded
= 0;
238 if (!readBytes(buf
, 1)) return REDIS_RDB_LENERR
;
239 type
= (buf
[0] & 0xC0) >> 6;
240 if (type
== REDIS_RDB_6BITLEN
) {
241 /* Read a 6 bit len */
242 return buf
[0] & 0x3F;
243 } else if (type
== REDIS_RDB_ENCVAL
) {
244 /* Read a 6 bit len encoding type */
245 if (isencoded
) *isencoded
= 1;
246 return buf
[0] & 0x3F;
247 } else if (type
== REDIS_RDB_14BITLEN
) {
248 /* Read a 14 bit len */
249 if (!readBytes(buf
+1,1)) return REDIS_RDB_LENERR
;
250 return ((buf
[0] & 0x3F) << 8) | buf
[1];
252 /* Read a 32 bit len */
253 if (!readBytes(&len
, 4)) return REDIS_RDB_LENERR
;
254 return (unsigned int)ntohl(len
);
258 char *loadIntegerObject(int enctype
) {
259 uint32_t offset
= CURR_OFFSET
;
260 unsigned char enc
[4];
263 if (enctype
== REDIS_RDB_ENC_INT8
) {
265 if (!readBytes(enc
, 1)) return NULL
;
268 } else if (enctype
== REDIS_RDB_ENC_INT16
) {
270 if (!readBytes(enc
, 2)) return NULL
;
271 v
= enc
[0]|(enc
[1]<<8);
273 } else if (enctype
== REDIS_RDB_ENC_INT32
) {
275 if (!readBytes(enc
, 4)) return NULL
;
276 v
= enc
[0]|(enc
[1]<<8)|(enc
[2]<<16)|(enc
[3]<<24);
279 SHIFT_ERROR(offset
, "Unknown integer encoding (0x%02x)", enctype
);
283 /* convert val into string */
285 buf
= malloc(sizeof(char) * 128);
286 sprintf(buf
, "%lld", val
);
290 char* loadLzfStringObject() {
291 unsigned int slen
, clen
;
294 if ((clen
= loadLength(NULL
)) == REDIS_RDB_LENERR
) return NULL
;
295 if ((slen
= loadLength(NULL
)) == REDIS_RDB_LENERR
) return NULL
;
298 if (!readBytes(c
, clen
)) {
304 if (lzf_decompress(c
,clen
,s
,slen
) == 0) {
313 /* returns NULL when not processable, char* when valid */
314 char* loadStringObject() {
315 uint32_t offset
= CURR_OFFSET
;
319 len
= loadLength(&isencoded
);
322 case REDIS_RDB_ENC_INT8
:
323 case REDIS_RDB_ENC_INT16
:
324 case REDIS_RDB_ENC_INT32
:
325 return loadIntegerObject(len
);
326 case REDIS_RDB_ENC_LZF
:
327 return loadLzfStringObject();
329 /* unknown encoding */
330 SHIFT_ERROR(offset
, "Unknown string encoding (0x%02x)", len
);
335 if (len
== REDIS_RDB_LENERR
) return NULL
;
337 char *buf
= malloc(sizeof(char) * (len
+1));
339 if (!readBytes(buf
, len
)) {
346 int processStringObject(char** store
) {
347 unsigned long offset
= CURR_OFFSET
;
348 char *key
= loadStringObject();
350 SHIFT_ERROR(offset
, "Error reading string object");
363 double* loadDoubleValue() {
368 if (!readBytes(&len
,1)) return NULL
;
370 val
= malloc(sizeof(double));
372 case 255: *val
= R_NegInf
; return val
;
373 case 254: *val
= R_PosInf
; return val
;
374 case 253: *val
= R_Nan
; return val
;
376 if (!readBytes(buf
, len
)) {
381 sscanf(buf
, "%lg", val
);
386 int processDoubleValue(double** store
) {
387 unsigned long offset
= CURR_OFFSET
;
388 double *val
= loadDoubleValue();
390 SHIFT_ERROR(offset
, "Error reading double value");
403 int loadPair(entry
*e
) {
404 uint32_t offset
= CURR_OFFSET
;
409 if (processStringObject(&key
)) {
412 SHIFT_ERROR(offset
, "Error reading entry key");
417 if (e
->type
== REDIS_LIST
||
418 e
->type
== REDIS_SET
||
419 e
->type
== REDIS_ZSET
||
420 e
->type
== REDIS_HASH
) {
421 if ((length
= loadLength(NULL
)) == REDIS_RDB_LENERR
) {
422 SHIFT_ERROR(offset
, "Error reading %s length", types
[e
->type
]);
429 case REDIS_HASH_ZIPMAP
:
430 case REDIS_LIST_ZIPLIST
:
431 case REDIS_SET_INTSET
:
432 case REDIS_ZSET_ZIPLIST
:
433 case REDIS_HASH_ZIPLIST
:
434 if (!processStringObject(NULL
)) {
435 SHIFT_ERROR(offset
, "Error reading entry value");
441 for (i
= 0; i
< length
; i
++) {
442 offset
= CURR_OFFSET
;
443 if (!processStringObject(NULL
)) {
444 SHIFT_ERROR(offset
, "Error reading element at index %d (length: %d)", i
, length
);
450 for (i
= 0; i
< length
; i
++) {
451 offset
= CURR_OFFSET
;
452 if (!processStringObject(NULL
)) {
453 SHIFT_ERROR(offset
, "Error reading element key at index %d (length: %d)", i
, length
);
456 offset
= CURR_OFFSET
;
457 if (!processDoubleValue(NULL
)) {
458 SHIFT_ERROR(offset
, "Error reading element value at index %d (length: %d)", i
, length
);
464 for (i
= 0; i
< length
; i
++) {
465 offset
= CURR_OFFSET
;
466 if (!processStringObject(NULL
)) {
467 SHIFT_ERROR(offset
, "Error reading element key at index %d (length: %d)", i
, length
);
470 offset
= CURR_OFFSET
;
471 if (!processStringObject(NULL
)) {
472 SHIFT_ERROR(offset
, "Error reading element value at index %d (length: %d)", i
, length
);
478 SHIFT_ERROR(offset
, "Type not implemented");
481 /* because we're done, we assume success */
487 entry e
= { NULL
, -1, 0 };
488 uint32_t length
, offset
[4];
490 /* reset error container */
493 offset
[0] = CURR_OFFSET
;
498 offset
[1] = CURR_OFFSET
;
499 if (e
.type
== REDIS_SELECTDB
) {
500 if ((length
= loadLength(NULL
)) == REDIS_RDB_LENERR
) {
501 SHIFT_ERROR(offset
[1], "Error reading database number");
505 SHIFT_ERROR(offset
[1], "Database number out of range (%d)", length
);
508 } else if (e
.type
== REDIS_EOF
) {
509 if (positions
[level
].offset
< positions
[level
].size
) {
510 SHIFT_ERROR(offset
[0], "Unexpected EOF");
516 /* optionally consume expire */
517 if (e
.type
== REDIS_EXPIRETIME
||
518 e
.type
== REDIS_EXPIRETIME_MS
) {
519 if (!processTime(e
.type
)) return e
;
520 if (!loadType(&e
)) return e
;
523 offset
[1] = CURR_OFFSET
;
525 SHIFT_ERROR(offset
[1], "Error for type %s", types
[e
.type
]);
530 /* all entries are followed by a valid type:
531 * e.g. a new entry, SELECTDB, EXPIRE, EOF */
532 offset
[2] = CURR_OFFSET
;
533 if (peekType() == -1) {
534 SHIFT_ERROR(offset
[2], "Followed by invalid type");
535 SHIFT_ERROR(offset
[0], "Error for type %s", types
[e
.type
]);
544 void printCentered(int indent
, int width
, char* body
) {
545 char head
[256], tail
[256];
546 memset(head
, '\0', 256);
547 memset(tail
, '\0', 256);
549 memset(head
, '=', indent
);
550 memset(tail
, '=', width
- 2 - indent
- strlen(body
));
551 printf("%s %s %s\n", head
, body
, tail
);
554 void printValid(uint64_t ops
, uint64_t bytes
) {
556 sprintf(body
, "Processed %llu valid opcodes (in %llu bytes)",
557 (unsigned long long) ops
, (unsigned long long) bytes
);
558 printCentered(4, 80, body
);
561 void printSkipped(uint64_t bytes
, uint64_t offset
) {
563 sprintf(body
, "Skipped %llu bytes (resuming at 0x%08llx)",
564 (unsigned long long) bytes
, (unsigned long long) offset
);
565 printCentered(4, 80, body
);
568 void printErrorStack(entry
*e
) {
573 sprintf(body
, "Error trace");
574 } else if (e
->type
>= 253) {
575 sprintf(body
, "Error trace (%s)", types
[e
->type
]);
576 } else if (!e
->key
) {
577 sprintf(body
, "Error trace (%s: (unknown))", types
[e
->type
]);
580 strncpy(tmp
, e
->key
, 40);
582 /* display truncation at the last 3 chars */
583 if (strlen(e
->key
) > 40) {
584 memset(&tmp
[37], '.', 3);
587 /* display unprintable characters as ? */
588 for (i
= 0; i
< strlen(tmp
); i
++) {
589 if (tmp
[i
] <= 32) tmp
[i
] = '?';
591 sprintf(body
, "Error trace (%s: %s)", types
[e
->type
], tmp
);
594 printCentered(4, 80, body
);
596 /* display error stack */
597 for (i
= 0; i
< errors
.level
; i
++) {
598 printf("0x%08lx - %s\n",
599 (unsigned long) errors
.offset
[i
], errors
.error
[i
]);
604 uint64_t num_errors
= 0, num_valid_ops
= 0, num_valid_bytes
= 0;
606 int dump_version
= processHeader();
608 /* Exclude the final checksum for RDB >= 5. Will be checked at the end. */
609 if (dump_version
>= 5) {
610 if (positions
[0].size
< 8) {
611 printf("RDB version >= 5 but no room for checksum.\n");
614 positions
[0].size
-= 8;;
618 while(positions
[0].offset
< positions
[0].size
) {
619 positions
[1] = positions
[0];
622 if (!entry
.success
) {
623 printValid(num_valid_ops
, num_valid_bytes
);
624 printErrorStack(&entry
);
629 /* search for next valid entry */
630 uint64_t offset
= positions
[0].offset
+ 1;
633 while (!entry
.success
&& offset
< positions
[0].size
) {
634 positions
[1].offset
= offset
;
636 /* find 3 consecutive valid entries */
637 for (i
= 0; i
< 3; i
++) {
639 if (!entry
.success
) break;
641 /* check if we found 3 consecutive valid entries */
647 /* print how many bytes we have skipped to find a new valid opcode */
648 if (offset
< positions
[0].size
) {
649 printSkipped(offset
- positions
[0].offset
, offset
);
652 positions
[0].offset
= offset
;
655 num_valid_bytes
+= positions
[1].offset
- positions
[0].offset
;
657 /* advance position */
658 positions
[0] = positions
[1];
663 /* because there is another potential error,
664 * print how many valid ops we have processed */
665 printValid(num_valid_ops
, num_valid_bytes
);
668 if (entry
.type
!= REDIS_EOF
) {
669 /* last byte should be EOF, add error */
671 SHIFT_ERROR(positions
[0].offset
, "Expected EOF, got %s", types
[entry
.type
]);
673 /* this is an EOF error so reset type */
675 printErrorStack(&entry
);
680 /* Verify checksum */
681 if (dump_version
>= 5) {
682 uint64_t crc
= crc64(0,positions
[0].data
,positions
[0].size
);
684 unsigned char *p
= (unsigned char*)positions
[0].data
+positions
[0].size
;
685 crc2
= ((uint64_t)p
[0] << 0) |
686 ((uint64_t)p
[1] << 8) |
687 ((uint64_t)p
[2] << 16) |
688 ((uint64_t)p
[3] << 24) |
689 ((uint64_t)p
[4] << 32) |
690 ((uint64_t)p
[5] << 40) |
691 ((uint64_t)p
[6] << 48) |
692 ((uint64_t)p
[7] << 56);
694 SHIFT_ERROR(positions
[0].offset
, "RDB CRC64 does not match.");
696 printf("CRC64 checksum is OK\n");
700 /* print summary on errors */
703 printf("Total unprocessable opcodes: %llu\n",
704 (unsigned long long) num_errors
);
708 int main(int argc
, char **argv
) {
709 /* expect the first argument to be the dump file */
711 printf("Usage: %s <dump.rdb>\n", argv
[0]);
720 fd
= open(argv
[1], O_RDONLY
);
722 ERROR("Cannot open file: %s\n", argv
[1]);
724 if (fstat(fd
, &stat
) == -1) {
725 ERROR("Cannot stat: %s\n", argv
[1]);
730 if (sizeof(size_t) == sizeof(int32_t) && size
>= INT_MAX
) {
731 ERROR("Cannot check dump files >2GB on a 32-bit platform\n");
734 data
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0);
735 if (data
== MAP_FAILED
) {
736 ERROR("Cannot mmap: %s\n", argv
[1]);
739 /* Initialize static vars */
740 positions
[0].data
= data
;
741 positions
[0].size
= size
;
742 positions
[0].offset
= 0;
746 sprintf(types
[REDIS_STRING
], "STRING");
747 sprintf(types
[REDIS_LIST
], "LIST");
748 sprintf(types
[REDIS_SET
], "SET");
749 sprintf(types
[REDIS_ZSET
], "ZSET");
750 sprintf(types
[REDIS_HASH
], "HASH");
752 /* Object types only used for dumping to disk */
753 sprintf(types
[REDIS_EXPIRETIME
], "EXPIRETIME");
754 sprintf(types
[REDIS_SELECTDB
], "SELECTDB");
755 sprintf(types
[REDIS_EOF
], "EOF");
757 /* Double constants initialization */
759 R_PosInf
= 1.0/R_Zero
;
760 R_NegInf
= -1.0/R_Zero
;
761 R_Nan
= R_Zero
/R_Zero
;