drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** A utility for printing content from a write-ahead log file. |
| 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <ctype.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <unistd.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | |
| 14 | static int pagesize = 1024; /* Size of a database page */ |
| 15 | static int fd = -1; /* File descriptor for reading the WAL file */ |
| 16 | static int mxFrame = 0; /* Last frame */ |
| 17 | static int perLine = 16; /* HEX elements to print per line */ |
| 18 | |
| 19 | typedef long long int i64; /* Datatype for 64-bit integers */ |
| 20 | |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 21 | /* Information for computing the checksum */ |
| 22 | typedef struct Cksum Cksum; |
| 23 | struct Cksum { |
| 24 | int bSwap; /* True to do byte swapping on 32-bit words */ |
| 25 | unsigned s0, s1; /* Current checksum value */ |
| 26 | }; |
| 27 | |
| 28 | /* |
| 29 | ** extract a 32-bit big-endian integer |
| 30 | */ |
| 31 | static unsigned int getInt32(const unsigned char *a){ |
| 32 | unsigned int x = (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 33 | return x; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | ** Swap bytes on a 32-bit unsigned integer |
| 38 | */ |
| 39 | static unsigned int swab32(unsigned int x){ |
| 40 | return (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) |
| 41 | + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24); |
| 42 | } |
| 43 | |
| 44 | /* Extend the checksum. Reinitialize the checksum if bInit is true. |
| 45 | */ |
| 46 | static void extendCksum( |
| 47 | Cksum *pCksum, |
| 48 | unsigned char *aData, |
| 49 | unsigned int nByte, |
| 50 | int bInit |
| 51 | ){ |
| 52 | unsigned int *a32; |
| 53 | if( bInit ){ |
| 54 | int a = 0; |
| 55 | *((char*)&a) = 1; |
| 56 | if( a==1 ){ |
| 57 | /* Host is little-endian */ |
| 58 | pCksum->bSwap = getInt32(aData)!=0x377f0682; |
| 59 | }else{ |
| 60 | /* Host is big-endian */ |
| 61 | pCksum->bSwap = getInt32(aData)!=0x377f0683; |
| 62 | } |
| 63 | pCksum->s0 = 0; |
| 64 | pCksum->s1 = 0; |
| 65 | } |
| 66 | a32 = (unsigned int*)aData; |
| 67 | while( nByte>0 ){ |
| 68 | unsigned int x0 = a32[0]; |
| 69 | unsigned int x1 = a32[1]; |
| 70 | if( pCksum->bSwap ){ |
| 71 | x0 = swab32(x0); |
| 72 | x1 = swab32(x1); |
| 73 | } |
| 74 | pCksum->s0 += x0 + pCksum->s1; |
| 75 | pCksum->s1 += x1 + pCksum->s0; |
| 76 | nByte -= 8; |
| 77 | a32 += 2; |
| 78 | } |
| 79 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | ** Convert the var-int format into i64. Return the number of bytes |
| 83 | ** in the var-int. Write the var-int value into *pVal. |
| 84 | */ |
| 85 | static int decodeVarint(const unsigned char *z, i64 *pVal){ |
| 86 | i64 v = 0; |
| 87 | int i; |
| 88 | for(i=0; i<8; i++){ |
| 89 | v = (v<<7) + (z[i]&0x7f); |
| 90 | if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; } |
| 91 | } |
| 92 | v = (v<<8) + (z[i]&0xff); |
| 93 | *pVal = v; |
| 94 | return 9; |
| 95 | } |
| 96 | |
| 97 | /* Report an out-of-memory error and die. |
| 98 | */ |
| 99 | static void out_of_memory(void){ |
| 100 | fprintf(stderr,"Out of memory...\n"); |
| 101 | exit(1); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | ** Read content from the file. |
| 106 | ** |
| 107 | ** Space to hold the content is obtained from malloc() and needs to be |
| 108 | ** freed by the caller. |
| 109 | */ |
| 110 | static unsigned char *getContent(int ofst, int nByte){ |
| 111 | unsigned char *aData; |
| 112 | aData = malloc(nByte); |
| 113 | if( aData==0 ) out_of_memory(); |
| 114 | lseek(fd, ofst, SEEK_SET); |
| 115 | read(fd, aData, nByte); |
| 116 | return aData; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | ** Print a range of bytes as hex and as ascii. |
| 121 | */ |
| 122 | static void print_byte_range( |
| 123 | int ofst, /* First byte in the range of bytes to print */ |
| 124 | int nByte, /* Number of bytes to print */ |
| 125 | unsigned char *aData, /* Content to print */ |
| 126 | int printOfst /* Add this amount to the index on the left column */ |
| 127 | ){ |
| 128 | int i, j; |
| 129 | const char *zOfstFmt; |
| 130 | |
| 131 | if( ((printOfst+nByte)&~0xfff)==0 ){ |
| 132 | zOfstFmt = " %03x: "; |
| 133 | }else if( ((printOfst+nByte)&~0xffff)==0 ){ |
| 134 | zOfstFmt = " %04x: "; |
| 135 | }else if( ((printOfst+nByte)&~0xfffff)==0 ){ |
| 136 | zOfstFmt = " %05x: "; |
| 137 | }else if( ((printOfst+nByte)&~0xffffff)==0 ){ |
| 138 | zOfstFmt = " %06x: "; |
| 139 | }else{ |
| 140 | zOfstFmt = " %08x: "; |
| 141 | } |
| 142 | |
| 143 | for(i=0; i<nByte; i += perLine){ |
| 144 | fprintf(stdout, zOfstFmt, i+printOfst); |
| 145 | for(j=0; j<perLine; j++){ |
| 146 | if( i+j>nByte ){ |
| 147 | fprintf(stdout, " "); |
| 148 | }else{ |
| 149 | fprintf(stdout,"%02x ", aData[i+j]); |
| 150 | } |
| 151 | } |
| 152 | for(j=0; j<perLine; j++){ |
| 153 | if( i+j>nByte ){ |
| 154 | fprintf(stdout, " "); |
| 155 | }else{ |
| 156 | fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.'); |
| 157 | } |
| 158 | } |
| 159 | fprintf(stdout,"\n"); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* Print a line of decode output showing a 4-byte integer. |
| 164 | */ |
| 165 | static void print_decode_line( |
| 166 | unsigned char *aData, /* Content being decoded */ |
| 167 | int ofst, int nByte, /* Start and size of decode */ |
| 168 | int asHex, /* If true, output value as hex */ |
| 169 | const char *zMsg /* Message to append */ |
| 170 | ){ |
| 171 | int i, j; |
| 172 | int val = aData[ofst]; |
| 173 | char zBuf[100]; |
| 174 | sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]); |
| 175 | i = strlen(zBuf); |
| 176 | for(j=1; j<4; j++){ |
| 177 | if( j>=nByte ){ |
| 178 | sprintf(&zBuf[i], " "); |
| 179 | }else{ |
| 180 | sprintf(&zBuf[i], " %02x", aData[ofst+j]); |
| 181 | val = val*256 + aData[ofst+j]; |
| 182 | } |
| 183 | i += strlen(&zBuf[i]); |
| 184 | } |
| 185 | if( asHex ){ |
| 186 | sprintf(&zBuf[i], " 0x%08x", val); |
| 187 | }else{ |
| 188 | sprintf(&zBuf[i], " %9d", val); |
| 189 | } |
| 190 | printf("%s %s\n", zBuf, zMsg); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | ** Print an entire page of content as hex |
| 195 | */ |
| 196 | static void print_frame(int iFrame){ |
| 197 | int iStart; |
| 198 | unsigned char *aData; |
| 199 | iStart = 32 + (iFrame-1)*(pagesize+24); |
| 200 | fprintf(stdout, "Frame %d: (offsets 0x%x..0x%x)\n", |
| 201 | iFrame, iStart, iStart+pagesize+24); |
| 202 | aData = getContent(iStart, pagesize+24); |
| 203 | print_decode_line(aData, 0, 4, 0, "Page number"); |
| 204 | print_decode_line(aData, 4, 4, 0, "DB size, or 0 for non-commit"); |
| 205 | print_decode_line(aData, 8, 4, 1, "Salt-1"); |
| 206 | print_decode_line(aData,12, 4, 1, "Salt-2"); |
| 207 | print_decode_line(aData,16, 4, 1, "Checksum-1"); |
| 208 | print_decode_line(aData,20, 4, 1, "Checksum-2"); |
| 209 | print_byte_range(iStart+24, pagesize, aData+24, 0); |
| 210 | free(aData); |
| 211 | } |
| 212 | |
| 213 | /* |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 214 | ** Summarize a single frame on a single line. |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 215 | */ |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 216 | static void print_oneline_frame(int iFrame, Cksum *pCksum){ |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 217 | int iStart; |
| 218 | unsigned char *aData; |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 219 | unsigned int s0, s1; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 220 | iStart = 32 + (iFrame-1)*(pagesize+24); |
| 221 | aData = getContent(iStart, 24); |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 222 | extendCksum(pCksum, aData, 8, 0); |
| 223 | extendCksum(pCksum, getContent(iStart+24, pagesize), pagesize, 0); |
| 224 | s0 = getInt32(aData+16); |
| 225 | s1 = getInt32(aData+20); |
| 226 | fprintf(stdout, "Frame %4d: %6d %6d 0x%08x,%08x 0x%08x,%08x %s\n", |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 227 | iFrame, |
| 228 | getInt32(aData), |
| 229 | getInt32(aData+4), |
| 230 | getInt32(aData+8), |
| 231 | getInt32(aData+12), |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 232 | s0, |
| 233 | s1, |
drh | 03c41c0 | 2013-01-25 15:31:44 +0000 | [diff] [blame^] | 234 | (s0==pCksum->s0 && s1==pCksum->s1) ? "" : "cksum-fail" |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 235 | ); |
drh | 03c41c0 | 2013-01-25 15:31:44 +0000 | [diff] [blame^] | 236 | |
| 237 | /* Reset the checksum so that a single frame checksum failure will not |
| 238 | ** cause all subsequent frames to also show a failure. */ |
| 239 | pCksum->s0 = s0; |
| 240 | pCksum->s1 = s1; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 241 | free(aData); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | ** Decode the WAL header. |
| 246 | */ |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 247 | static void print_wal_header(Cksum *pCksum){ |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 248 | unsigned char *aData; |
| 249 | aData = getContent(0, 32); |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 250 | if( pCksum ){ |
| 251 | extendCksum(pCksum, aData, 24, 1); |
| 252 | printf("Checksum byte order: %s\n", pCksum->bSwap ? "swapped" : "native"); |
| 253 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 254 | printf("WAL Header:\n"); |
| 255 | print_decode_line(aData, 0, 4,1,"Magic. 0x377f0682 (le) or 0x377f0683 (be)"); |
| 256 | print_decode_line(aData, 4, 4, 0, "File format"); |
| 257 | print_decode_line(aData, 8, 4, 0, "Database page size"); |
| 258 | print_decode_line(aData, 12,4, 0, "Checkpoint sequence number"); |
| 259 | print_decode_line(aData, 16,4, 1, "Salt-1"); |
| 260 | print_decode_line(aData, 20,4, 1, "Salt-2"); |
| 261 | print_decode_line(aData, 24,4, 1, "Checksum-1"); |
| 262 | print_decode_line(aData, 28,4, 1, "Checksum-2"); |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 263 | if( pCksum ){ |
| 264 | if( pCksum->s0!=getInt32(aData+24) ){ |
| 265 | printf("**** cksum-1 mismatch: 0x%08x\n", pCksum->s0); |
| 266 | } |
| 267 | if( pCksum->s1!=getInt32(aData+28) ){ |
| 268 | printf("**** cksum-2 mismatch: 0x%08x\n", pCksum->s1); |
| 269 | } |
| 270 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 271 | free(aData); |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | ** Create a description for a single cell. |
| 276 | */ |
| 277 | static int describeCell(unsigned char cType, unsigned char *a, char **pzDesc){ |
| 278 | int i; |
| 279 | int nDesc = 0; |
| 280 | int n = 0; |
| 281 | int leftChild; |
| 282 | i64 nPayload; |
| 283 | i64 rowid; |
| 284 | static char zDesc[100]; |
| 285 | i = 0; |
| 286 | if( cType<=5 ){ |
| 287 | leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3]; |
| 288 | a += 4; |
| 289 | n += 4; |
| 290 | sprintf(zDesc, "left-child: %d ", leftChild); |
| 291 | nDesc = strlen(zDesc); |
| 292 | } |
| 293 | if( cType!=5 ){ |
| 294 | i = decodeVarint(a, &nPayload); |
| 295 | a += i; |
| 296 | n += i; |
| 297 | sprintf(&zDesc[nDesc], "sz: %lld ", nPayload); |
| 298 | nDesc += strlen(&zDesc[nDesc]); |
| 299 | } |
| 300 | if( cType==5 || cType==13 ){ |
| 301 | i = decodeVarint(a, &rowid); |
| 302 | a += i; |
| 303 | n += i; |
| 304 | sprintf(&zDesc[nDesc], "rowid: %lld ", rowid); |
| 305 | nDesc += strlen(&zDesc[nDesc]); |
| 306 | } |
| 307 | *pzDesc = zDesc; |
| 308 | return n; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | ** Decode a btree page |
| 313 | */ |
| 314 | static void decode_btree_page(unsigned char *a, int pgno, int hdrSize){ |
| 315 | const char *zType = "unknown"; |
| 316 | int nCell; |
| 317 | int i; |
| 318 | int iCellPtr; |
| 319 | switch( a[0] ){ |
| 320 | case 2: zType = "index interior node"; break; |
| 321 | case 5: zType = "table interior node"; break; |
| 322 | case 10: zType = "index leaf"; break; |
| 323 | case 13: zType = "table leaf"; break; |
| 324 | } |
| 325 | printf("Decode of btree page %d:\n", pgno); |
| 326 | print_decode_line(a, 0, 1, 0, zType); |
| 327 | print_decode_line(a, 1, 2, 0, "Offset to first freeblock"); |
| 328 | print_decode_line(a, 3, 2, 0, "Number of cells on this page"); |
| 329 | nCell = a[3]*256 + a[4]; |
| 330 | print_decode_line(a, 5, 2, 0, "Offset to cell content area"); |
| 331 | print_decode_line(a, 7, 1, 0, "Fragmented byte count"); |
| 332 | if( a[0]==2 || a[0]==5 ){ |
| 333 | print_decode_line(a, 8, 4, 0, "Right child"); |
| 334 | iCellPtr = 12; |
| 335 | }else{ |
| 336 | iCellPtr = 8; |
| 337 | } |
| 338 | for(i=0; i<nCell; i++){ |
| 339 | int cofst = iCellPtr + i*2; |
| 340 | char *zDesc; |
| 341 | cofst = a[cofst]*256 + a[cofst+1]; |
| 342 | describeCell(a[0], &a[cofst-hdrSize], &zDesc); |
| 343 | printf(" %03x: cell[%d] %s\n", cofst, i, zDesc); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | int main(int argc, char **argv){ |
| 348 | struct stat sbuf; |
| 349 | unsigned char zPgSz[2]; |
| 350 | if( argc<2 ){ |
| 351 | fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]); |
| 352 | exit(1); |
| 353 | } |
| 354 | fd = open(argv[1], O_RDONLY); |
| 355 | if( fd<0 ){ |
| 356 | fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); |
| 357 | exit(1); |
| 358 | } |
| 359 | zPgSz[0] = 0; |
| 360 | zPgSz[1] = 0; |
| 361 | lseek(fd, 10, SEEK_SET); |
| 362 | read(fd, zPgSz, 2); |
| 363 | pagesize = zPgSz[0]*256 + zPgSz[1]; |
| 364 | if( pagesize==0 ) pagesize = 1024; |
| 365 | printf("Pagesize: %d\n", pagesize); |
| 366 | fstat(fd, &sbuf); |
| 367 | if( sbuf.st_size<32 ){ |
| 368 | printf("file too small to be a WAL\n"); |
| 369 | return 0; |
| 370 | } |
| 371 | mxFrame = (sbuf.st_size - 32)/(pagesize + 24); |
| 372 | printf("Available pages: 1..%d\n", mxFrame); |
| 373 | if( argc==2 ){ |
| 374 | int i; |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 375 | Cksum x; |
| 376 | print_wal_header(&x); |
| 377 | for(i=1; i<=mxFrame; i++){ |
| 378 | print_oneline_frame(i, &x); |
| 379 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 380 | }else{ |
| 381 | int i; |
| 382 | for(i=2; i<argc; i++){ |
| 383 | int iStart, iEnd; |
| 384 | char *zLeft; |
| 385 | if( strcmp(argv[i], "header")==0 ){ |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 386 | print_wal_header(0); |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 387 | continue; |
| 388 | } |
| 389 | if( !isdigit(argv[i][0]) ){ |
| 390 | fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]); |
| 391 | continue; |
| 392 | } |
| 393 | iStart = strtol(argv[i], &zLeft, 0); |
| 394 | if( zLeft && strcmp(zLeft,"..end")==0 ){ |
| 395 | iEnd = mxFrame; |
| 396 | }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ |
| 397 | iEnd = strtol(&zLeft[2], 0, 0); |
| 398 | #if 0 |
| 399 | }else if( zLeft && zLeft[0]=='b' ){ |
| 400 | int ofst, nByte, hdrSize; |
| 401 | unsigned char *a; |
| 402 | if( iStart==1 ){ |
| 403 | ofst = hdrSize = 100; |
| 404 | nByte = pagesize-100; |
| 405 | }else{ |
| 406 | hdrSize = 0; |
| 407 | ofst = (iStart-1)*pagesize; |
| 408 | nByte = pagesize; |
| 409 | } |
| 410 | a = getContent(ofst, nByte); |
| 411 | decode_btree_page(a, iStart, hdrSize); |
| 412 | free(a); |
| 413 | continue; |
| 414 | #endif |
| 415 | }else{ |
| 416 | iEnd = iStart; |
| 417 | } |
| 418 | if( iStart<1 || iEnd<iStart || iEnd>mxFrame ){ |
| 419 | fprintf(stderr, |
| 420 | "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", |
| 421 | mxFrame); |
| 422 | exit(1); |
| 423 | } |
| 424 | while( iStart<=iEnd ){ |
| 425 | print_frame(iStart); |
| 426 | iStart++; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | close(fd); |
| 431 | return 0; |
| 432 | } |