2 ** A utility for printing content from a write-ahead log file.
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 */
19 typedef long long int i64; /* Datatype for 64-bit integers */
23 ** Convert the var-int format into i64. Return the number of bytes
24 ** in the var-int. Write the var-int value into *pVal.
26 static int decodeVarint(const unsigned char *z, i64 *pVal){
30 v = (v<<7) + (z[i]&0x7f);
31 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
33 v = (v<<8) + (z[i]&0xff);
38 /* Report an out-of-memory error and die.
40 static void out_of_memory(void){
41 fprintf(stderr,"Out of memory...\n");
46 ** Read content from the file.
48 ** Space to hold the content is obtained from malloc() and needs to be
49 ** freed by the caller.
51 static unsigned char *getContent(int ofst, int nByte){
53 aData = malloc(nByte);
54 if( aData==0 ) out_of_memory();
55 lseek(fd, ofst, SEEK_SET);
56 read(fd, aData, nByte);
61 ** Print a range of bytes as hex and as ascii.
63 static void print_byte_range(
64 int ofst, /* First byte in the range of bytes to print */
65 int nByte, /* Number of bytes to print */
66 unsigned char *aData, /* Content to print */
67 int printOfst /* Add this amount to the index on the left column */
72 if( ((printOfst+nByte)&~0xfff)==0 ){
74 }else if( ((printOfst+nByte)&~0xffff)==0 ){
76 }else if( ((printOfst+nByte)&~0xfffff)==0 ){
78 }else if( ((printOfst+nByte)&~0xffffff)==0 ){
84 for(i=0; i<nByte; i += perLine){
85 fprintf(stdout, zOfstFmt, i+printOfst);
86 for(j=0; j<perLine; j++){
90 fprintf(stdout,"%02x ", aData[i+j]);
93 for(j=0; j<perLine; j++){
97 fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
100 fprintf(stdout,"\n");
104 /* Print a line of decode output showing a 4-byte integer.
106 static void print_decode_line(
107 unsigned char *aData, /* Content being decoded */
108 int ofst, int nByte, /* Start and size of decode */
109 int asHex, /* If true, output value as hex */
110 const char *zMsg /* Message to append */
113 int val = aData[ofst];
115 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
119 sprintf(&zBuf[i], " ");
121 sprintf(&zBuf[i], " %02x", aData[ofst+j]);
122 val = val*256 + aData[ofst+j];
124 i += strlen(&zBuf[i]);
127 sprintf(&zBuf[i], " 0x%08x", val);
129 sprintf(&zBuf[i], " %9d", val);
131 printf("%s %s\n", zBuf, zMsg);
135 ** Print an entire page of content as hex
137 static void print_frame(int iFrame){
139 unsigned char *aData;
140 iStart = 32 + (iFrame-1)*(pagesize+24);
141 fprintf(stdout, "Frame %d: (offsets 0x%x..0x%x)\n",
142 iFrame, iStart, iStart+pagesize+24);
143 aData = getContent(iStart, pagesize+24);
144 print_decode_line(aData, 0, 4, 0, "Page number");
145 print_decode_line(aData, 4, 4, 0, "DB size, or 0 for non-commit");
146 print_decode_line(aData, 8, 4, 1, "Salt-1");
147 print_decode_line(aData,12, 4, 1, "Salt-2");
148 print_decode_line(aData,16, 4, 1, "Checksum-1");
149 print_decode_line(aData,20, 4, 1, "Checksum-2");
150 print_byte_range(iStart+24, pagesize, aData+24, 0);
155 ** extract a 32-bit big-endian integer
157 static unsigned int getInt32(const unsigned char *a){
158 unsigned int x = (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
163 ** Print an entire page of content as hex
165 static void print_oneline_frame(int iFrame){
167 unsigned char *aData;
168 iStart = 32 + (iFrame-1)*(pagesize+24);
169 aData = getContent(iStart, 24);
170 fprintf(stdout, "Frame %4d: %6d %6d 0x%08x 0x%08x 0x%08x 0x%08x\n",
183 ** Decode the WAL header.
185 static void print_wal_header(void){
186 unsigned char *aData;
187 aData = getContent(0, 32);
188 printf("WAL Header:\n");
189 print_decode_line(aData, 0, 4,1,"Magic. 0x377f0682 (le) or 0x377f0683 (be)");
190 print_decode_line(aData, 4, 4, 0, "File format");
191 print_decode_line(aData, 8, 4, 0, "Database page size");
192 print_decode_line(aData, 12,4, 0, "Checkpoint sequence number");
193 print_decode_line(aData, 16,4, 1, "Salt-1");
194 print_decode_line(aData, 20,4, 1, "Salt-2");
195 print_decode_line(aData, 24,4, 1, "Checksum-1");
196 print_decode_line(aData, 28,4, 1, "Checksum-2");
201 ** Create a description for a single cell.
203 static int describeCell(unsigned char cType, unsigned char *a, char **pzDesc){
210 static char zDesc[100];
213 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
216 sprintf(zDesc, "left-child: %d ", leftChild);
217 nDesc = strlen(zDesc);
220 i = decodeVarint(a, &nPayload);
223 sprintf(&zDesc[nDesc], "sz: %lld ", nPayload);
224 nDesc += strlen(&zDesc[nDesc]);
226 if( cType==5 || cType==13 ){
227 i = decodeVarint(a, &rowid);
230 sprintf(&zDesc[nDesc], "rowid: %lld ", rowid);
231 nDesc += strlen(&zDesc[nDesc]);
238 ** Decode a btree page
240 static void decode_btree_page(unsigned char *a, int pgno, int hdrSize){
241 const char *zType = "unknown";
246 case 2: zType = "index interior node"; break;
247 case 5: zType = "table interior node"; break;
248 case 10: zType = "index leaf"; break;
249 case 13: zType = "table leaf"; break;
251 printf("Decode of btree page %d:\n", pgno);
252 print_decode_line(a, 0, 1, 0, zType);
253 print_decode_line(a, 1, 2, 0, "Offset to first freeblock");
254 print_decode_line(a, 3, 2, 0, "Number of cells on this page");
255 nCell = a[3]*256 + a[4];
256 print_decode_line(a, 5, 2, 0, "Offset to cell content area");
257 print_decode_line(a, 7, 1, 0, "Fragmented byte count");
258 if( a[0]==2 || a[0]==5 ){
259 print_decode_line(a, 8, 4, 0, "Right child");
264 for(i=0; i<nCell; i++){
265 int cofst = iCellPtr + i*2;
267 cofst = a[cofst]*256 + a[cofst+1];
268 describeCell(a[0], &a[cofst-hdrSize], &zDesc);
269 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
273 int main(int argc, char **argv){
275 unsigned char zPgSz[2];
277 fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]);
280 fd = open(argv[1], O_RDONLY);
282 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
287 lseek(fd, 10, SEEK_SET);
289 pagesize = zPgSz[0]*256 + zPgSz[1];
290 if( pagesize==0 ) pagesize = 1024;
291 printf("Pagesize: %d\n", pagesize);
293 if( sbuf.st_size<32 ){
294 printf("file too small to be a WAL\n");
297 mxFrame = (sbuf.st_size - 32)/(pagesize + 24);
298 printf("Available pages: 1..%d\n", mxFrame);
302 for(i=1; i<=mxFrame; i++) print_oneline_frame(i);
305 for(i=2; i<argc; i++){
308 if( strcmp(argv[i], "header")==0 ){
312 if( !isdigit(argv[i][0]) ){
313 fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]);
316 iStart = strtol(argv[i], &zLeft, 0);
317 if( zLeft && strcmp(zLeft,"..end")==0 ){
319 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
320 iEnd = strtol(&zLeft[2], 0, 0);
322 }else if( zLeft && zLeft[0]=='b' ){
323 int ofst, nByte, hdrSize;
326 ofst = hdrSize = 100;
327 nByte = pagesize-100;
330 ofst = (iStart-1)*pagesize;
333 a = getContent(ofst, nByte);
334 decode_btree_page(a, iStart, hdrSize);
341 if( iStart<1 || iEnd<iStart || iEnd>mxFrame ){
343 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
347 while( iStart<=iEnd ){