blob: 2888c10aa590718b1ebf9a499745d0568e5fc6e3 [file] [log] [blame]
drh12c7e1a2010-07-07 20:38:26 +00001/*
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
14static int pagesize = 1024; /* Size of a database page */
15static int fd = -1; /* File descriptor for reading the WAL file */
16static int mxFrame = 0; /* Last frame */
17static int perLine = 16; /* HEX elements to print per line */
18
19typedef long long int i64; /* Datatype for 64-bit integers */
20
drhd63ce042013-01-25 15:09:41 +000021/* Information for computing the checksum */
22typedef struct Cksum Cksum;
23struct 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*/
31static 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*/
39static 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*/
46static 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}
drh12c7e1a2010-07-07 20:38:26 +000080
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*/
85static 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*/
99static 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*/
110static 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*/
122static 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*/
165static 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*/
196static 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/*
drhd63ce042013-01-25 15:09:41 +0000214** Summarize a single frame on a single line.
drh12c7e1a2010-07-07 20:38:26 +0000215*/
drhd63ce042013-01-25 15:09:41 +0000216static void print_oneline_frame(int iFrame, Cksum *pCksum){
drh12c7e1a2010-07-07 20:38:26 +0000217 int iStart;
218 unsigned char *aData;
drhd63ce042013-01-25 15:09:41 +0000219 unsigned int s0, s1;
drh12c7e1a2010-07-07 20:38:26 +0000220 iStart = 32 + (iFrame-1)*(pagesize+24);
221 aData = getContent(iStart, 24);
drhd63ce042013-01-25 15:09:41 +0000222 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",
drh12c7e1a2010-07-07 20:38:26 +0000227 iFrame,
228 getInt32(aData),
229 getInt32(aData+4),
230 getInt32(aData+8),
231 getInt32(aData+12),
drhd63ce042013-01-25 15:09:41 +0000232 s0,
233 s1,
drh03c41c02013-01-25 15:31:44 +0000234 (s0==pCksum->s0 && s1==pCksum->s1) ? "" : "cksum-fail"
drh12c7e1a2010-07-07 20:38:26 +0000235 );
drh03c41c02013-01-25 15:31:44 +0000236
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;
drh12c7e1a2010-07-07 20:38:26 +0000241 free(aData);
242}
243
244/*
245** Decode the WAL header.
246*/
drhd63ce042013-01-25 15:09:41 +0000247static void print_wal_header(Cksum *pCksum){
drh12c7e1a2010-07-07 20:38:26 +0000248 unsigned char *aData;
249 aData = getContent(0, 32);
drhd63ce042013-01-25 15:09:41 +0000250 if( pCksum ){
251 extendCksum(pCksum, aData, 24, 1);
252 printf("Checksum byte order: %s\n", pCksum->bSwap ? "swapped" : "native");
253 }
drh12c7e1a2010-07-07 20:38:26 +0000254 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");
drhd63ce042013-01-25 15:09:41 +0000263 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 }
drh12c7e1a2010-07-07 20:38:26 +0000271 free(aData);
272}
drh1590d102013-01-25 15:59:55 +0000273/*
274** Describe cell content.
275*/
276static int describeContent(
277 unsigned char *a, /* Cell content */
278 int nLocal, /* Bytes in a[] */
279 char *zDesc /* Write description here */
280){
281 int nDesc = 0;
282 int n, i, j;
283 i64 x, v;
284 const unsigned char *pData;
285 const unsigned char *pLimit;
286 char sep = ' ';
287
288 pLimit = &a[nLocal];
289 n = decodeVarint(a, &x);
290 pData = &a[x];
291 a += n;
292 i = x - n;
293 while( i>0 && pData<=pLimit ){
294 n = decodeVarint(a, &x);
295 a += n;
296 i -= n;
297 nLocal -= n;
298 zDesc[0] = sep;
299 sep = ',';
300 nDesc++;
301 zDesc++;
302 if( x==0 ){
303 sprintf(zDesc, "*"); /* NULL is a "*" */
304 }else if( x>=1 && x<=6 ){
305 v = (signed char)pData[0];
306 pData++;
307 switch( x ){
308 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
309 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
310 case 4: v = (v<<8) + pData[0]; pData++;
311 case 3: v = (v<<8) + pData[0]; pData++;
312 case 2: v = (v<<8) + pData[0]; pData++;
313 }
314 sprintf(zDesc, "%lld", v);
315 }else if( x==7 ){
316 sprintf(zDesc, "real");
317 pData += 8;
318 }else if( x==8 ){
319 sprintf(zDesc, "0");
320 }else if( x==9 ){
321 sprintf(zDesc, "1");
322 }else if( x>=12 ){
323 int size = (x-12)/2;
324 if( (x&1)==0 ){
325 sprintf(zDesc, "blob(%d)", size);
326 }else{
327 sprintf(zDesc, "txt(%d)", size);
328 }
329 pData += size;
330 }
331 j = strlen(zDesc);
332 zDesc += j;
333 nDesc += j;
334 }
335 return nDesc;
336}
337
338/*
339** Compute the local payload size given the total payload size and
340** the page size.
341*/
342static int localPayload(i64 nPayload, char cType){
343 int maxLocal;
344 int minLocal;
345 int surplus;
346 int nLocal;
347 if( cType==13 ){
348 /* Table leaf */
349 maxLocal = pagesize-35;
350 minLocal = (pagesize-12)*32/255-23;
351 }else{
352 maxLocal = (pagesize-12)*64/255-23;
353 minLocal = (pagesize-12)*32/255-23;
354 }
355 if( nPayload>maxLocal ){
356 surplus = minLocal + (nPayload-minLocal)%(pagesize-4);
357 if( surplus<=maxLocal ){
358 nLocal = surplus;
359 }else{
360 nLocal = minLocal;
361 }
362 }else{
363 nLocal = nPayload;
364 }
365 return nLocal;
366}
drh12c7e1a2010-07-07 20:38:26 +0000367
368/*
369** Create a description for a single cell.
drh1590d102013-01-25 15:59:55 +0000370**
371** The return value is the local cell size.
drh12c7e1a2010-07-07 20:38:26 +0000372*/
drh1590d102013-01-25 15:59:55 +0000373static int describeCell(
374 unsigned char cType, /* Page type */
375 unsigned char *a, /* Cell content */
376 int showCellContent, /* Show cell content if true */
377 char **pzDesc /* Store description here */
378){
drh12c7e1a2010-07-07 20:38:26 +0000379 int i;
380 int nDesc = 0;
381 int n = 0;
382 int leftChild;
383 i64 nPayload;
384 i64 rowid;
drh1590d102013-01-25 15:59:55 +0000385 int nLocal;
386 static char zDesc[1000];
drh12c7e1a2010-07-07 20:38:26 +0000387 i = 0;
388 if( cType<=5 ){
389 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
390 a += 4;
391 n += 4;
drh1590d102013-01-25 15:59:55 +0000392 sprintf(zDesc, "lx: %d ", leftChild);
drh12c7e1a2010-07-07 20:38:26 +0000393 nDesc = strlen(zDesc);
394 }
395 if( cType!=5 ){
396 i = decodeVarint(a, &nPayload);
397 a += i;
398 n += i;
drh1590d102013-01-25 15:59:55 +0000399 sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
drh12c7e1a2010-07-07 20:38:26 +0000400 nDesc += strlen(&zDesc[nDesc]);
drh1590d102013-01-25 15:59:55 +0000401 nLocal = localPayload(nPayload, cType);
402 }else{
403 nPayload = nLocal = 0;
drh12c7e1a2010-07-07 20:38:26 +0000404 }
405 if( cType==5 || cType==13 ){
406 i = decodeVarint(a, &rowid);
407 a += i;
408 n += i;
drh1590d102013-01-25 15:59:55 +0000409 sprintf(&zDesc[nDesc], "r: %lld ", rowid);
drh12c7e1a2010-07-07 20:38:26 +0000410 nDesc += strlen(&zDesc[nDesc]);
411 }
drh1590d102013-01-25 15:59:55 +0000412 if( nLocal<nPayload ){
413 int ovfl;
414 unsigned char *b = &a[nLocal];
415 ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
416 sprintf(&zDesc[nDesc], "ov: %d ", ovfl);
417 nDesc += strlen(&zDesc[nDesc]);
418 n += 4;
419 }
420 if( showCellContent && cType!=5 ){
421 nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
422 }
drh12c7e1a2010-07-07 20:38:26 +0000423 *pzDesc = zDesc;
drh1590d102013-01-25 15:59:55 +0000424 return nLocal+n;
drh12c7e1a2010-07-07 20:38:26 +0000425}
426
427/*
428** Decode a btree page
429*/
drh1590d102013-01-25 15:59:55 +0000430static void decode_btree_page(
431 unsigned char *a, /* Content of the btree page to be decoded */
432 int pgno, /* Page number */
433 int hdrSize, /* Size of the page1-header in bytes */
434 const char *zArgs /* Flags to control formatting */
435){
drh12c7e1a2010-07-07 20:38:26 +0000436 const char *zType = "unknown";
437 int nCell;
drh1590d102013-01-25 15:59:55 +0000438 int i, j;
drh12c7e1a2010-07-07 20:38:26 +0000439 int iCellPtr;
drh1590d102013-01-25 15:59:55 +0000440 int showCellContent = 0;
441 int showMap = 0;
442 char *zMap = 0;
drh12c7e1a2010-07-07 20:38:26 +0000443 switch( a[0] ){
444 case 2: zType = "index interior node"; break;
445 case 5: zType = "table interior node"; break;
446 case 10: zType = "index leaf"; break;
447 case 13: zType = "table leaf"; break;
448 }
drh1590d102013-01-25 15:59:55 +0000449 while( zArgs[0] ){
450 switch( zArgs[0] ){
451 case 'c': showCellContent = 1; break;
452 case 'm': showMap = 1; break;
453 }
454 zArgs++;
455 }
drh12c7e1a2010-07-07 20:38:26 +0000456 printf("Decode of btree page %d:\n", pgno);
457 print_decode_line(a, 0, 1, 0, zType);
458 print_decode_line(a, 1, 2, 0, "Offset to first freeblock");
459 print_decode_line(a, 3, 2, 0, "Number of cells on this page");
460 nCell = a[3]*256 + a[4];
461 print_decode_line(a, 5, 2, 0, "Offset to cell content area");
462 print_decode_line(a, 7, 1, 0, "Fragmented byte count");
463 if( a[0]==2 || a[0]==5 ){
464 print_decode_line(a, 8, 4, 0, "Right child");
465 iCellPtr = 12;
466 }else{
467 iCellPtr = 8;
468 }
drh1590d102013-01-25 15:59:55 +0000469 if( nCell>0 ){
470 printf(" key: lx=left-child n=payload-size r=rowid\n");
471 }
472 if( showMap ){
473 zMap = malloc(pagesize);
474 memset(zMap, '.', pagesize);
475 memset(zMap, '1', hdrSize);
476 memset(&zMap[hdrSize], 'H', iCellPtr);
477 memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
478 }
drh12c7e1a2010-07-07 20:38:26 +0000479 for(i=0; i<nCell; i++){
480 int cofst = iCellPtr + i*2;
481 char *zDesc;
drh1590d102013-01-25 15:59:55 +0000482 int n;
483
drh12c7e1a2010-07-07 20:38:26 +0000484 cofst = a[cofst]*256 + a[cofst+1];
drh1590d102013-01-25 15:59:55 +0000485 n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
486 if( showMap ){
487 char zBuf[30];
488 memset(&zMap[cofst], '*', n);
489 zMap[cofst] = '[';
490 zMap[cofst+n-1] = ']';
491 sprintf(zBuf, "%d", i);
492 j = strlen(zBuf);
493 if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
494 }
drh12c7e1a2010-07-07 20:38:26 +0000495 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
496 }
drh1590d102013-01-25 15:59:55 +0000497 if( showMap ){
498 for(i=0; i<pagesize; i+=64){
499 printf(" %03x: %.64s\n", i, &zMap[i]);
500 }
501 free(zMap);
502 }
drh12c7e1a2010-07-07 20:38:26 +0000503}
504
505int main(int argc, char **argv){
506 struct stat sbuf;
507 unsigned char zPgSz[2];
508 if( argc<2 ){
509 fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]);
510 exit(1);
511 }
512 fd = open(argv[1], O_RDONLY);
513 if( fd<0 ){
514 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
515 exit(1);
516 }
517 zPgSz[0] = 0;
518 zPgSz[1] = 0;
519 lseek(fd, 10, SEEK_SET);
520 read(fd, zPgSz, 2);
521 pagesize = zPgSz[0]*256 + zPgSz[1];
522 if( pagesize==0 ) pagesize = 1024;
523 printf("Pagesize: %d\n", pagesize);
524 fstat(fd, &sbuf);
525 if( sbuf.st_size<32 ){
526 printf("file too small to be a WAL\n");
527 return 0;
528 }
529 mxFrame = (sbuf.st_size - 32)/(pagesize + 24);
530 printf("Available pages: 1..%d\n", mxFrame);
531 if( argc==2 ){
532 int i;
drhd63ce042013-01-25 15:09:41 +0000533 Cksum x;
534 print_wal_header(&x);
535 for(i=1; i<=mxFrame; i++){
536 print_oneline_frame(i, &x);
537 }
drh12c7e1a2010-07-07 20:38:26 +0000538 }else{
539 int i;
540 for(i=2; i<argc; i++){
541 int iStart, iEnd;
542 char *zLeft;
543 if( strcmp(argv[i], "header")==0 ){
drhd63ce042013-01-25 15:09:41 +0000544 print_wal_header(0);
drh12c7e1a2010-07-07 20:38:26 +0000545 continue;
546 }
547 if( !isdigit(argv[i][0]) ){
548 fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]);
549 continue;
550 }
551 iStart = strtol(argv[i], &zLeft, 0);
552 if( zLeft && strcmp(zLeft,"..end")==0 ){
553 iEnd = mxFrame;
554 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
555 iEnd = strtol(&zLeft[2], 0, 0);
drh12c7e1a2010-07-07 20:38:26 +0000556 }else if( zLeft && zLeft[0]=='b' ){
557 int ofst, nByte, hdrSize;
558 unsigned char *a;
559 if( iStart==1 ){
drh1590d102013-01-25 15:59:55 +0000560 hdrSize = 100;
drh12c7e1a2010-07-07 20:38:26 +0000561 ofst = hdrSize = 100;
562 nByte = pagesize-100;
563 }else{
564 hdrSize = 0;
565 ofst = (iStart-1)*pagesize;
566 nByte = pagesize;
567 }
drh1590d102013-01-25 15:59:55 +0000568 ofst = 32 + hdrSize + (iStart-1)*(pagesize+24) + 24;
drh12c7e1a2010-07-07 20:38:26 +0000569 a = getContent(ofst, nByte);
drh1590d102013-01-25 15:59:55 +0000570 decode_btree_page(a, iStart, hdrSize, zLeft+1);
drh12c7e1a2010-07-07 20:38:26 +0000571 free(a);
572 continue;
drh12c7e1a2010-07-07 20:38:26 +0000573 }else{
574 iEnd = iStart;
575 }
576 if( iStart<1 || iEnd<iStart || iEnd>mxFrame ){
577 fprintf(stderr,
578 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
579 mxFrame);
580 exit(1);
581 }
582 while( iStart<=iEnd ){
583 print_frame(iStart);
584 iStart++;
585 }
586 }
587 }
588 close(fd);
589 return 0;
590}