blob: 681291a7dba6262346c58a7fa79558a1c96413be [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,
234 (s0==pCksum->s0 && s1==pCksum->s1) ? "cksum-ok" : "cksum-fail"
drh12c7e1a2010-07-07 20:38:26 +0000235 );
236 free(aData);
237}
238
239/*
240** Decode the WAL header.
241*/
drhd63ce042013-01-25 15:09:41 +0000242static void print_wal_header(Cksum *pCksum){
drh12c7e1a2010-07-07 20:38:26 +0000243 unsigned char *aData;
244 aData = getContent(0, 32);
drhd63ce042013-01-25 15:09:41 +0000245 if( pCksum ){
246 extendCksum(pCksum, aData, 24, 1);
247 printf("Checksum byte order: %s\n", pCksum->bSwap ? "swapped" : "native");
248 }
drh12c7e1a2010-07-07 20:38:26 +0000249 printf("WAL Header:\n");
250 print_decode_line(aData, 0, 4,1,"Magic. 0x377f0682 (le) or 0x377f0683 (be)");
251 print_decode_line(aData, 4, 4, 0, "File format");
252 print_decode_line(aData, 8, 4, 0, "Database page size");
253 print_decode_line(aData, 12,4, 0, "Checkpoint sequence number");
254 print_decode_line(aData, 16,4, 1, "Salt-1");
255 print_decode_line(aData, 20,4, 1, "Salt-2");
256 print_decode_line(aData, 24,4, 1, "Checksum-1");
257 print_decode_line(aData, 28,4, 1, "Checksum-2");
drhd63ce042013-01-25 15:09:41 +0000258 if( pCksum ){
259 if( pCksum->s0!=getInt32(aData+24) ){
260 printf("**** cksum-1 mismatch: 0x%08x\n", pCksum->s0);
261 }
262 if( pCksum->s1!=getInt32(aData+28) ){
263 printf("**** cksum-2 mismatch: 0x%08x\n", pCksum->s1);
264 }
265 }
drh12c7e1a2010-07-07 20:38:26 +0000266 free(aData);
267}
268
269/*
270** Create a description for a single cell.
271*/
272static int describeCell(unsigned char cType, unsigned char *a, char **pzDesc){
273 int i;
274 int nDesc = 0;
275 int n = 0;
276 int leftChild;
277 i64 nPayload;
278 i64 rowid;
279 static char zDesc[100];
280 i = 0;
281 if( cType<=5 ){
282 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
283 a += 4;
284 n += 4;
285 sprintf(zDesc, "left-child: %d ", leftChild);
286 nDesc = strlen(zDesc);
287 }
288 if( cType!=5 ){
289 i = decodeVarint(a, &nPayload);
290 a += i;
291 n += i;
292 sprintf(&zDesc[nDesc], "sz: %lld ", nPayload);
293 nDesc += strlen(&zDesc[nDesc]);
294 }
295 if( cType==5 || cType==13 ){
296 i = decodeVarint(a, &rowid);
297 a += i;
298 n += i;
299 sprintf(&zDesc[nDesc], "rowid: %lld ", rowid);
300 nDesc += strlen(&zDesc[nDesc]);
301 }
302 *pzDesc = zDesc;
303 return n;
304}
305
306/*
307** Decode a btree page
308*/
309static void decode_btree_page(unsigned char *a, int pgno, int hdrSize){
310 const char *zType = "unknown";
311 int nCell;
312 int i;
313 int iCellPtr;
314 switch( a[0] ){
315 case 2: zType = "index interior node"; break;
316 case 5: zType = "table interior node"; break;
317 case 10: zType = "index leaf"; break;
318 case 13: zType = "table leaf"; break;
319 }
320 printf("Decode of btree page %d:\n", pgno);
321 print_decode_line(a, 0, 1, 0, zType);
322 print_decode_line(a, 1, 2, 0, "Offset to first freeblock");
323 print_decode_line(a, 3, 2, 0, "Number of cells on this page");
324 nCell = a[3]*256 + a[4];
325 print_decode_line(a, 5, 2, 0, "Offset to cell content area");
326 print_decode_line(a, 7, 1, 0, "Fragmented byte count");
327 if( a[0]==2 || a[0]==5 ){
328 print_decode_line(a, 8, 4, 0, "Right child");
329 iCellPtr = 12;
330 }else{
331 iCellPtr = 8;
332 }
333 for(i=0; i<nCell; i++){
334 int cofst = iCellPtr + i*2;
335 char *zDesc;
336 cofst = a[cofst]*256 + a[cofst+1];
337 describeCell(a[0], &a[cofst-hdrSize], &zDesc);
338 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
339 }
340}
341
342int main(int argc, char **argv){
343 struct stat sbuf;
344 unsigned char zPgSz[2];
345 if( argc<2 ){
346 fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]);
347 exit(1);
348 }
349 fd = open(argv[1], O_RDONLY);
350 if( fd<0 ){
351 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
352 exit(1);
353 }
354 zPgSz[0] = 0;
355 zPgSz[1] = 0;
356 lseek(fd, 10, SEEK_SET);
357 read(fd, zPgSz, 2);
358 pagesize = zPgSz[0]*256 + zPgSz[1];
359 if( pagesize==0 ) pagesize = 1024;
360 printf("Pagesize: %d\n", pagesize);
361 fstat(fd, &sbuf);
362 if( sbuf.st_size<32 ){
363 printf("file too small to be a WAL\n");
364 return 0;
365 }
366 mxFrame = (sbuf.st_size - 32)/(pagesize + 24);
367 printf("Available pages: 1..%d\n", mxFrame);
368 if( argc==2 ){
369 int i;
drhd63ce042013-01-25 15:09:41 +0000370 Cksum x;
371 print_wal_header(&x);
372 for(i=1; i<=mxFrame; i++){
373 print_oneline_frame(i, &x);
374 }
drh12c7e1a2010-07-07 20:38:26 +0000375 }else{
376 int i;
377 for(i=2; i<argc; i++){
378 int iStart, iEnd;
379 char *zLeft;
380 if( strcmp(argv[i], "header")==0 ){
drhd63ce042013-01-25 15:09:41 +0000381 print_wal_header(0);
drh12c7e1a2010-07-07 20:38:26 +0000382 continue;
383 }
384 if( !isdigit(argv[i][0]) ){
385 fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]);
386 continue;
387 }
388 iStart = strtol(argv[i], &zLeft, 0);
389 if( zLeft && strcmp(zLeft,"..end")==0 ){
390 iEnd = mxFrame;
391 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
392 iEnd = strtol(&zLeft[2], 0, 0);
393#if 0
394 }else if( zLeft && zLeft[0]=='b' ){
395 int ofst, nByte, hdrSize;
396 unsigned char *a;
397 if( iStart==1 ){
398 ofst = hdrSize = 100;
399 nByte = pagesize-100;
400 }else{
401 hdrSize = 0;
402 ofst = (iStart-1)*pagesize;
403 nByte = pagesize;
404 }
405 a = getContent(ofst, nByte);
406 decode_btree_page(a, iStart, hdrSize);
407 free(a);
408 continue;
409#endif
410 }else{
411 iEnd = iStart;
412 }
413 if( iStart<1 || iEnd<iStart || iEnd>mxFrame ){
414 fprintf(stderr,
415 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
416 mxFrame);
417 exit(1);
418 }
419 while( iStart<=iEnd ){
420 print_frame(iStart);
421 iStart++;
422 }
423 }
424 }
425 close(fd);
426 return 0;
427}