OLD | NEW |
1 /* | 1 /* |
2 ** A utility for printing content from a write-ahead log file. | 2 ** A utility for printing content from a write-ahead log file. |
3 */ | 3 */ |
4 #include <stdio.h> | 4 #include <stdio.h> |
5 #include <ctype.h> | 5 #include <ctype.h> |
6 #include <sys/types.h> | 6 #include <sys/types.h> |
7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 |
| 10 #if !defined(_MSC_VER) |
9 #include <unistd.h> | 11 #include <unistd.h> |
| 12 #else |
| 13 #include <io.h> |
| 14 #endif |
| 15 |
10 #include <stdlib.h> | 16 #include <stdlib.h> |
11 #include <string.h> | 17 #include <string.h> |
12 | 18 |
13 | 19 |
14 static int pagesize = 1024; /* Size of a database page */ | 20 static int pagesize = 1024; /* Size of a database page */ |
15 static int fd = -1; /* File descriptor for reading the WAL file */ | 21 static int fd = -1; /* File descriptor for reading the WAL file */ |
16 static int mxFrame = 0; /* Last frame */ | 22 static int mxFrame = 0; /* Last frame */ |
17 static int perLine = 16; /* HEX elements to print per line */ | 23 static int perLine = 16; /* HEX elements to print per line */ |
18 | 24 |
19 typedef long long int i64; /* Datatype for 64-bit integers */ | 25 typedef long long int i64; /* Datatype for 64-bit integers */ |
20 | 26 |
| 27 /* Information for computing the checksum */ |
| 28 typedef struct Cksum Cksum; |
| 29 struct Cksum { |
| 30 int bSwap; /* True to do byte swapping on 32-bit words */ |
| 31 unsigned s0, s1; /* Current checksum value */ |
| 32 }; |
| 33 |
| 34 /* |
| 35 ** extract a 32-bit big-endian integer |
| 36 */ |
| 37 static unsigned int getInt32(const unsigned char *a){ |
| 38 unsigned int x = (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 39 return x; |
| 40 } |
| 41 |
| 42 /* |
| 43 ** Swap bytes on a 32-bit unsigned integer |
| 44 */ |
| 45 static unsigned int swab32(unsigned int x){ |
| 46 return (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) |
| 47 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24); |
| 48 } |
| 49 |
| 50 /* Extend the checksum. Reinitialize the checksum if bInit is true. |
| 51 */ |
| 52 static void extendCksum( |
| 53 Cksum *pCksum, |
| 54 unsigned char *aData, |
| 55 unsigned int nByte, |
| 56 int bInit |
| 57 ){ |
| 58 unsigned int *a32; |
| 59 if( bInit ){ |
| 60 int a = 0; |
| 61 *((char*)&a) = 1; |
| 62 if( a==1 ){ |
| 63 /* Host is little-endian */ |
| 64 pCksum->bSwap = getInt32(aData)!=0x377f0682; |
| 65 }else{ |
| 66 /* Host is big-endian */ |
| 67 pCksum->bSwap = getInt32(aData)!=0x377f0683; |
| 68 } |
| 69 pCksum->s0 = 0; |
| 70 pCksum->s1 = 0; |
| 71 } |
| 72 a32 = (unsigned int*)aData; |
| 73 while( nByte>0 ){ |
| 74 unsigned int x0 = a32[0]; |
| 75 unsigned int x1 = a32[1]; |
| 76 if( pCksum->bSwap ){ |
| 77 x0 = swab32(x0); |
| 78 x1 = swab32(x1); |
| 79 } |
| 80 pCksum->s0 += x0 + pCksum->s1; |
| 81 pCksum->s1 += x1 + pCksum->s0; |
| 82 nByte -= 8; |
| 83 a32 += 2; |
| 84 } |
| 85 } |
21 | 86 |
22 /* | 87 /* |
23 ** Convert the var-int format into i64. Return the number of bytes | 88 ** Convert the var-int format into i64. Return the number of bytes |
24 ** in the var-int. Write the var-int value into *pVal. | 89 ** in the var-int. Write the var-int value into *pVal. |
25 */ | 90 */ |
26 static int decodeVarint(const unsigned char *z, i64 *pVal){ | 91 static int decodeVarint(const unsigned char *z, i64 *pVal){ |
27 i64 v = 0; | 92 i64 v = 0; |
28 int i; | 93 int i; |
29 for(i=0; i<8; i++){ | 94 for(i=0; i<8; i++){ |
30 v = (v<<7) + (z[i]&0x7f); | 95 v = (v<<7) + (z[i]&0x7f); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 static void print_decode_line( | 171 static void print_decode_line( |
107 unsigned char *aData, /* Content being decoded */ | 172 unsigned char *aData, /* Content being decoded */ |
108 int ofst, int nByte, /* Start and size of decode */ | 173 int ofst, int nByte, /* Start and size of decode */ |
109 int asHex, /* If true, output value as hex */ | 174 int asHex, /* If true, output value as hex */ |
110 const char *zMsg /* Message to append */ | 175 const char *zMsg /* Message to append */ |
111 ){ | 176 ){ |
112 int i, j; | 177 int i, j; |
113 int val = aData[ofst]; | 178 int val = aData[ofst]; |
114 char zBuf[100]; | 179 char zBuf[100]; |
115 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]); | 180 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]); |
116 i = strlen(zBuf); | 181 i = (int)strlen(zBuf); |
117 for(j=1; j<4; j++){ | 182 for(j=1; j<4; j++){ |
118 if( j>=nByte ){ | 183 if( j>=nByte ){ |
119 sprintf(&zBuf[i], " "); | 184 sprintf(&zBuf[i], " "); |
120 }else{ | 185 }else{ |
121 sprintf(&zBuf[i], " %02x", aData[ofst+j]); | 186 sprintf(&zBuf[i], " %02x", aData[ofst+j]); |
122 val = val*256 + aData[ofst+j]; | 187 val = val*256 + aData[ofst+j]; |
123 } | 188 } |
124 i += strlen(&zBuf[i]); | 189 i += (int)strlen(&zBuf[i]); |
125 } | 190 } |
126 if( asHex ){ | 191 if( asHex ){ |
127 sprintf(&zBuf[i], " 0x%08x", val); | 192 sprintf(&zBuf[i], " 0x%08x", val); |
128 }else{ | 193 }else{ |
129 sprintf(&zBuf[i], " %9d", val); | 194 sprintf(&zBuf[i], " %9d", val); |
130 } | 195 } |
131 printf("%s %s\n", zBuf, zMsg); | 196 printf("%s %s\n", zBuf, zMsg); |
132 } | 197 } |
133 | 198 |
134 /* | 199 /* |
(...skipping 10 matching lines...) Expand all Loading... |
145 print_decode_line(aData, 4, 4, 0, "DB size, or 0 for non-commit"); | 210 print_decode_line(aData, 4, 4, 0, "DB size, or 0 for non-commit"); |
146 print_decode_line(aData, 8, 4, 1, "Salt-1"); | 211 print_decode_line(aData, 8, 4, 1, "Salt-1"); |
147 print_decode_line(aData,12, 4, 1, "Salt-2"); | 212 print_decode_line(aData,12, 4, 1, "Salt-2"); |
148 print_decode_line(aData,16, 4, 1, "Checksum-1"); | 213 print_decode_line(aData,16, 4, 1, "Checksum-1"); |
149 print_decode_line(aData,20, 4, 1, "Checksum-2"); | 214 print_decode_line(aData,20, 4, 1, "Checksum-2"); |
150 print_byte_range(iStart+24, pagesize, aData+24, 0); | 215 print_byte_range(iStart+24, pagesize, aData+24, 0); |
151 free(aData); | 216 free(aData); |
152 } | 217 } |
153 | 218 |
154 /* | 219 /* |
155 ** extract a 32-bit big-endian integer | 220 ** Summarize a single frame on a single line. |
156 */ | 221 */ |
157 static unsigned int getInt32(const unsigned char *a){ | 222 static void print_oneline_frame(int iFrame, Cksum *pCksum){ |
158 unsigned int x = (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; | |
159 return x; | |
160 } | |
161 | |
162 /* | |
163 ** Print an entire page of content as hex | |
164 */ | |
165 static void print_oneline_frame(int iFrame){ | |
166 int iStart; | 223 int iStart; |
167 unsigned char *aData; | 224 unsigned char *aData; |
| 225 unsigned int s0, s1; |
168 iStart = 32 + (iFrame-1)*(pagesize+24); | 226 iStart = 32 + (iFrame-1)*(pagesize+24); |
169 aData = getContent(iStart, 24); | 227 aData = getContent(iStart, 24); |
170 fprintf(stdout, "Frame %4d: %6d %6d 0x%08x 0x%08x 0x%08x 0x%08x\n", | 228 extendCksum(pCksum, aData, 8, 0); |
| 229 extendCksum(pCksum, getContent(iStart+24, pagesize), pagesize, 0); |
| 230 s0 = getInt32(aData+16); |
| 231 s1 = getInt32(aData+20); |
| 232 fprintf(stdout, "Frame %4d: %6d %6d 0x%08x,%08x 0x%08x,%08x %s\n", |
171 iFrame, | 233 iFrame, |
172 getInt32(aData), | 234 getInt32(aData), |
173 getInt32(aData+4), | 235 getInt32(aData+4), |
174 getInt32(aData+8), | 236 getInt32(aData+8), |
175 getInt32(aData+12), | 237 getInt32(aData+12), |
176 getInt32(aData+16), | 238 s0, |
177 getInt32(aData+20) | 239 s1, |
| 240 (s0==pCksum->s0 && s1==pCksum->s1) ? "" : "cksum-fail" |
178 ); | 241 ); |
| 242 |
| 243 /* Reset the checksum so that a single frame checksum failure will not |
| 244 ** cause all subsequent frames to also show a failure. */ |
| 245 pCksum->s0 = s0; |
| 246 pCksum->s1 = s1; |
179 free(aData); | 247 free(aData); |
180 } | 248 } |
181 | 249 |
182 /* | 250 /* |
183 ** Decode the WAL header. | 251 ** Decode the WAL header. |
184 */ | 252 */ |
185 static void print_wal_header(void){ | 253 static void print_wal_header(Cksum *pCksum){ |
186 unsigned char *aData; | 254 unsigned char *aData; |
187 aData = getContent(0, 32); | 255 aData = getContent(0, 32); |
| 256 if( pCksum ){ |
| 257 extendCksum(pCksum, aData, 24, 1); |
| 258 printf("Checksum byte order: %s\n", pCksum->bSwap ? "swapped" : "native"); |
| 259 } |
188 printf("WAL Header:\n"); | 260 printf("WAL Header:\n"); |
189 print_decode_line(aData, 0, 4,1,"Magic. 0x377f0682 (le) or 0x377f0683 (be)"); | 261 print_decode_line(aData, 0, 4,1,"Magic. 0x377f0682 (le) or 0x377f0683 (be)"); |
190 print_decode_line(aData, 4, 4, 0, "File format"); | 262 print_decode_line(aData, 4, 4, 0, "File format"); |
191 print_decode_line(aData, 8, 4, 0, "Database page size"); | 263 print_decode_line(aData, 8, 4, 0, "Database page size"); |
192 print_decode_line(aData, 12,4, 0, "Checkpoint sequence number"); | 264 print_decode_line(aData, 12,4, 0, "Checkpoint sequence number"); |
193 print_decode_line(aData, 16,4, 1, "Salt-1"); | 265 print_decode_line(aData, 16,4, 1, "Salt-1"); |
194 print_decode_line(aData, 20,4, 1, "Salt-2"); | 266 print_decode_line(aData, 20,4, 1, "Salt-2"); |
195 print_decode_line(aData, 24,4, 1, "Checksum-1"); | 267 print_decode_line(aData, 24,4, 1, "Checksum-1"); |
196 print_decode_line(aData, 28,4, 1, "Checksum-2"); | 268 print_decode_line(aData, 28,4, 1, "Checksum-2"); |
| 269 if( pCksum ){ |
| 270 if( pCksum->s0!=getInt32(aData+24) ){ |
| 271 printf("**** cksum-1 mismatch: 0x%08x\n", pCksum->s0); |
| 272 } |
| 273 if( pCksum->s1!=getInt32(aData+28) ){ |
| 274 printf("**** cksum-2 mismatch: 0x%08x\n", pCksum->s1); |
| 275 } |
| 276 } |
197 free(aData); | 277 free(aData); |
198 } | 278 } |
| 279 /* |
| 280 ** Describe cell content. |
| 281 */ |
| 282 static i64 describeContent( |
| 283 unsigned char *a, /* Cell content */ |
| 284 i64 nLocal, /* Bytes in a[] */ |
| 285 char *zDesc /* Write description here */ |
| 286 ){ |
| 287 int nDesc = 0; |
| 288 int n, j; |
| 289 i64 i, x, v; |
| 290 const unsigned char *pData; |
| 291 const unsigned char *pLimit; |
| 292 char sep = ' '; |
| 293 |
| 294 pLimit = &a[nLocal]; |
| 295 n = decodeVarint(a, &x); |
| 296 pData = &a[x]; |
| 297 a += n; |
| 298 i = x - n; |
| 299 while( i>0 && pData<=pLimit ){ |
| 300 n = decodeVarint(a, &x); |
| 301 a += n; |
| 302 i -= n; |
| 303 nLocal -= n; |
| 304 zDesc[0] = sep; |
| 305 sep = ','; |
| 306 nDesc++; |
| 307 zDesc++; |
| 308 if( x==0 ){ |
| 309 sprintf(zDesc, "*"); /* NULL is a "*" */ |
| 310 }else if( x>=1 && x<=6 ){ |
| 311 v = (signed char)pData[0]; |
| 312 pData++; |
| 313 switch( x ){ |
| 314 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
| 315 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
| 316 case 4: v = (v<<8) + pData[0]; pData++; |
| 317 case 3: v = (v<<8) + pData[0]; pData++; |
| 318 case 2: v = (v<<8) + pData[0]; pData++; |
| 319 } |
| 320 sprintf(zDesc, "%lld", v); |
| 321 }else if( x==7 ){ |
| 322 sprintf(zDesc, "real"); |
| 323 pData += 8; |
| 324 }else if( x==8 ){ |
| 325 sprintf(zDesc, "0"); |
| 326 }else if( x==9 ){ |
| 327 sprintf(zDesc, "1"); |
| 328 }else if( x>=12 ){ |
| 329 i64 size = (x-12)/2; |
| 330 if( (x&1)==0 ){ |
| 331 sprintf(zDesc, "blob(%lld)", size); |
| 332 }else{ |
| 333 sprintf(zDesc, "txt(%lld)", size); |
| 334 } |
| 335 pData += size; |
| 336 } |
| 337 j = (int)strlen(zDesc); |
| 338 zDesc += j; |
| 339 nDesc += j; |
| 340 } |
| 341 return nDesc; |
| 342 } |
| 343 |
| 344 /* |
| 345 ** Compute the local payload size given the total payload size and |
| 346 ** the page size. |
| 347 */ |
| 348 static i64 localPayload(i64 nPayload, char cType){ |
| 349 i64 maxLocal; |
| 350 i64 minLocal; |
| 351 i64 surplus; |
| 352 i64 nLocal; |
| 353 if( cType==13 ){ |
| 354 /* Table leaf */ |
| 355 maxLocal = pagesize-35; |
| 356 minLocal = (pagesize-12)*32/255-23; |
| 357 }else{ |
| 358 maxLocal = (pagesize-12)*64/255-23; |
| 359 minLocal = (pagesize-12)*32/255-23; |
| 360 } |
| 361 if( nPayload>maxLocal ){ |
| 362 surplus = minLocal + (nPayload-minLocal)%(pagesize-4); |
| 363 if( surplus<=maxLocal ){ |
| 364 nLocal = surplus; |
| 365 }else{ |
| 366 nLocal = minLocal; |
| 367 } |
| 368 }else{ |
| 369 nLocal = nPayload; |
| 370 } |
| 371 return nLocal; |
| 372 } |
199 | 373 |
200 /* | 374 /* |
201 ** Create a description for a single cell. | 375 ** Create a description for a single cell. |
| 376 ** |
| 377 ** The return value is the local cell size. |
202 */ | 378 */ |
203 static int describeCell(unsigned char cType, unsigned char *a, char **pzDesc){ | 379 static i64 describeCell( |
| 380 unsigned char cType, /* Page type */ |
| 381 unsigned char *a, /* Cell content */ |
| 382 int showCellContent, /* Show cell content if true */ |
| 383 char **pzDesc /* Store description here */ |
| 384 ){ |
204 int i; | 385 int i; |
205 int nDesc = 0; | 386 i64 nDesc = 0; |
206 int n = 0; | 387 int n = 0; |
207 int leftChild; | 388 int leftChild; |
208 i64 nPayload; | 389 i64 nPayload; |
209 i64 rowid; | 390 i64 rowid; |
210 static char zDesc[100]; | 391 i64 nLocal; |
| 392 static char zDesc[1000]; |
211 i = 0; | 393 i = 0; |
212 if( cType<=5 ){ | 394 if( cType<=5 ){ |
213 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3]; | 395 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3]; |
214 a += 4; | 396 a += 4; |
215 n += 4; | 397 n += 4; |
216 sprintf(zDesc, "left-child: %d ", leftChild); | 398 sprintf(zDesc, "lx: %d ", leftChild); |
217 nDesc = strlen(zDesc); | 399 nDesc = strlen(zDesc); |
218 } | 400 } |
219 if( cType!=5 ){ | 401 if( cType!=5 ){ |
220 i = decodeVarint(a, &nPayload); | 402 i = decodeVarint(a, &nPayload); |
221 a += i; | 403 a += i; |
222 n += i; | 404 n += i; |
223 sprintf(&zDesc[nDesc], "sz: %lld ", nPayload); | 405 sprintf(&zDesc[nDesc], "n: %lld ", nPayload); |
224 nDesc += strlen(&zDesc[nDesc]); | 406 nDesc += strlen(&zDesc[nDesc]); |
| 407 nLocal = localPayload(nPayload, cType); |
| 408 }else{ |
| 409 nPayload = nLocal = 0; |
225 } | 410 } |
226 if( cType==5 || cType==13 ){ | 411 if( cType==5 || cType==13 ){ |
227 i = decodeVarint(a, &rowid); | 412 i = decodeVarint(a, &rowid); |
228 a += i; | 413 a += i; |
229 n += i; | 414 n += i; |
230 sprintf(&zDesc[nDesc], "rowid: %lld ", rowid); | 415 sprintf(&zDesc[nDesc], "r: %lld ", rowid); |
231 nDesc += strlen(&zDesc[nDesc]); | 416 nDesc += strlen(&zDesc[nDesc]); |
232 } | 417 } |
| 418 if( nLocal<nPayload ){ |
| 419 int ovfl; |
| 420 unsigned char *b = &a[nLocal]; |
| 421 ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3]; |
| 422 sprintf(&zDesc[nDesc], "ov: %d ", ovfl); |
| 423 nDesc += strlen(&zDesc[nDesc]); |
| 424 n += 4; |
| 425 } |
| 426 if( showCellContent && cType!=5 ){ |
| 427 nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]); |
| 428 } |
233 *pzDesc = zDesc; | 429 *pzDesc = zDesc; |
234 return n; | 430 return nLocal+n; |
235 } | 431 } |
236 | 432 |
237 /* | 433 /* |
238 ** Decode a btree page | 434 ** Decode a btree page |
239 */ | 435 */ |
240 static void decode_btree_page(unsigned char *a, int pgno, int hdrSize){ | 436 static void decode_btree_page( |
| 437 unsigned char *a, /* Content of the btree page to be decoded */ |
| 438 int pgno, /* Page number */ |
| 439 int hdrSize, /* Size of the page1-header in bytes */ |
| 440 const char *zArgs /* Flags to control formatting */ |
| 441 ){ |
241 const char *zType = "unknown"; | 442 const char *zType = "unknown"; |
242 int nCell; | 443 int nCell; |
243 int i; | 444 int i, j; |
244 int iCellPtr; | 445 int iCellPtr; |
| 446 int showCellContent = 0; |
| 447 int showMap = 0; |
| 448 char *zMap = 0; |
245 switch( a[0] ){ | 449 switch( a[0] ){ |
246 case 2: zType = "index interior node"; break; | 450 case 2: zType = "index interior node"; break; |
247 case 5: zType = "table interior node"; break; | 451 case 5: zType = "table interior node"; break; |
248 case 10: zType = "index leaf"; break; | 452 case 10: zType = "index leaf"; break; |
249 case 13: zType = "table leaf"; break; | 453 case 13: zType = "table leaf"; break; |
250 } | 454 } |
| 455 while( zArgs[0] ){ |
| 456 switch( zArgs[0] ){ |
| 457 case 'c': showCellContent = 1; break; |
| 458 case 'm': showMap = 1; break; |
| 459 } |
| 460 zArgs++; |
| 461 } |
251 printf("Decode of btree page %d:\n", pgno); | 462 printf("Decode of btree page %d:\n", pgno); |
252 print_decode_line(a, 0, 1, 0, zType); | 463 print_decode_line(a, 0, 1, 0, zType); |
253 print_decode_line(a, 1, 2, 0, "Offset to first freeblock"); | 464 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"); | 465 print_decode_line(a, 3, 2, 0, "Number of cells on this page"); |
255 nCell = a[3]*256 + a[4]; | 466 nCell = a[3]*256 + a[4]; |
256 print_decode_line(a, 5, 2, 0, "Offset to cell content area"); | 467 print_decode_line(a, 5, 2, 0, "Offset to cell content area"); |
257 print_decode_line(a, 7, 1, 0, "Fragmented byte count"); | 468 print_decode_line(a, 7, 1, 0, "Fragmented byte count"); |
258 if( a[0]==2 || a[0]==5 ){ | 469 if( a[0]==2 || a[0]==5 ){ |
259 print_decode_line(a, 8, 4, 0, "Right child"); | 470 print_decode_line(a, 8, 4, 0, "Right child"); |
260 iCellPtr = 12; | 471 iCellPtr = 12; |
261 }else{ | 472 }else{ |
262 iCellPtr = 8; | 473 iCellPtr = 8; |
263 } | 474 } |
| 475 if( nCell>0 ){ |
| 476 printf(" key: lx=left-child n=payload-size r=rowid\n"); |
| 477 } |
| 478 if( showMap ){ |
| 479 zMap = malloc(pagesize); |
| 480 memset(zMap, '.', pagesize); |
| 481 memset(zMap, '1', hdrSize); |
| 482 memset(&zMap[hdrSize], 'H', iCellPtr); |
| 483 memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell); |
| 484 } |
264 for(i=0; i<nCell; i++){ | 485 for(i=0; i<nCell; i++){ |
265 int cofst = iCellPtr + i*2; | 486 int cofst = iCellPtr + i*2; |
266 char *zDesc; | 487 char *zDesc; |
| 488 i64 n; |
| 489 |
267 cofst = a[cofst]*256 + a[cofst+1]; | 490 cofst = a[cofst]*256 + a[cofst+1]; |
268 describeCell(a[0], &a[cofst-hdrSize], &zDesc); | 491 n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc); |
| 492 if( showMap ){ |
| 493 char zBuf[30]; |
| 494 memset(&zMap[cofst], '*', (size_t)n); |
| 495 zMap[cofst] = '['; |
| 496 zMap[cofst+n-1] = ']'; |
| 497 sprintf(zBuf, "%d", i); |
| 498 j = (int)strlen(zBuf); |
| 499 if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j); |
| 500 } |
269 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc); | 501 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc); |
270 } | 502 } |
| 503 if( showMap ){ |
| 504 for(i=0; i<pagesize; i+=64){ |
| 505 printf(" %03x: %.64s\n", i, &zMap[i]); |
| 506 } |
| 507 free(zMap); |
| 508 } |
271 } | 509 } |
272 | 510 |
273 int main(int argc, char **argv){ | 511 int main(int argc, char **argv){ |
274 struct stat sbuf; | 512 struct stat sbuf; |
275 unsigned char zPgSz[2]; | 513 unsigned char zPgSz[4]; |
276 if( argc<2 ){ | 514 if( argc<2 ){ |
277 fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]); | 515 fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]); |
278 exit(1); | 516 exit(1); |
279 } | 517 } |
280 fd = open(argv[1], O_RDONLY); | 518 fd = open(argv[1], O_RDONLY); |
281 if( fd<0 ){ | 519 if( fd<0 ){ |
282 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); | 520 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); |
283 exit(1); | 521 exit(1); |
284 } | 522 } |
285 zPgSz[0] = 0; | 523 zPgSz[0] = 0; |
286 zPgSz[1] = 0; | 524 zPgSz[1] = 0; |
287 lseek(fd, 10, SEEK_SET); | 525 lseek(fd, 8, SEEK_SET); |
288 read(fd, zPgSz, 2); | 526 read(fd, zPgSz, 4); |
289 pagesize = zPgSz[0]*256 + zPgSz[1]; | 527 pagesize = zPgSz[1]*65536 + zPgSz[2]*256 + zPgSz[3]; |
290 if( pagesize==0 ) pagesize = 1024; | 528 if( pagesize==0 ) pagesize = 1024; |
291 printf("Pagesize: %d\n", pagesize); | 529 printf("Pagesize: %d\n", pagesize); |
292 fstat(fd, &sbuf); | 530 fstat(fd, &sbuf); |
293 if( sbuf.st_size<32 ){ | 531 if( sbuf.st_size<32 ){ |
294 printf("file too small to be a WAL\n"); | 532 printf("file too small to be a WAL\n"); |
295 return 0; | 533 return 0; |
296 } | 534 } |
297 mxFrame = (sbuf.st_size - 32)/(pagesize + 24); | 535 mxFrame = (sbuf.st_size - 32)/(pagesize + 24); |
298 printf("Available pages: 1..%d\n", mxFrame); | 536 printf("Available pages: 1..%d\n", mxFrame); |
299 if( argc==2 ){ | 537 if( argc==2 ){ |
300 int i; | 538 int i; |
301 print_wal_header(); | 539 Cksum x; |
302 for(i=1; i<=mxFrame; i++) print_oneline_frame(i); | 540 print_wal_header(&x); |
| 541 for(i=1; i<=mxFrame; i++){ |
| 542 print_oneline_frame(i, &x); |
| 543 } |
303 }else{ | 544 }else{ |
304 int i; | 545 int i; |
305 for(i=2; i<argc; i++){ | 546 for(i=2; i<argc; i++){ |
306 int iStart, iEnd; | 547 int iStart, iEnd; |
307 char *zLeft; | 548 char *zLeft; |
308 if( strcmp(argv[i], "header")==0 ){ | 549 if( strcmp(argv[i], "header")==0 ){ |
309 print_wal_header(); | 550 print_wal_header(0); |
310 continue; | 551 continue; |
311 } | 552 } |
312 if( !isdigit(argv[i][0]) ){ | 553 if( !isdigit(argv[i][0]) ){ |
313 fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]); | 554 fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]); |
314 continue; | 555 continue; |
315 } | 556 } |
316 iStart = strtol(argv[i], &zLeft, 0); | 557 iStart = strtol(argv[i], &zLeft, 0); |
317 if( zLeft && strcmp(zLeft,"..end")==0 ){ | 558 if( zLeft && strcmp(zLeft,"..end")==0 ){ |
318 iEnd = mxFrame; | 559 iEnd = mxFrame; |
319 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ | 560 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ |
320 iEnd = strtol(&zLeft[2], 0, 0); | 561 iEnd = strtol(&zLeft[2], 0, 0); |
321 #if 0 | |
322 }else if( zLeft && zLeft[0]=='b' ){ | 562 }else if( zLeft && zLeft[0]=='b' ){ |
323 int ofst, nByte, hdrSize; | 563 int ofst, nByte, hdrSize; |
324 unsigned char *a; | 564 unsigned char *a; |
325 if( iStart==1 ){ | 565 if( iStart==1 ){ |
| 566 hdrSize = 100; |
326 ofst = hdrSize = 100; | 567 ofst = hdrSize = 100; |
327 nByte = pagesize-100; | 568 nByte = pagesize-100; |
328 }else{ | 569 }else{ |
329 hdrSize = 0; | 570 hdrSize = 0; |
330 ofst = (iStart-1)*pagesize; | 571 ofst = (iStart-1)*pagesize; |
331 nByte = pagesize; | 572 nByte = pagesize; |
332 } | 573 } |
| 574 ofst = 32 + hdrSize + (iStart-1)*(pagesize+24) + 24; |
333 a = getContent(ofst, nByte); | 575 a = getContent(ofst, nByte); |
334 decode_btree_page(a, iStart, hdrSize); | 576 decode_btree_page(a, iStart, hdrSize, zLeft+1); |
335 free(a); | 577 free(a); |
336 continue; | 578 continue; |
337 #endif | |
338 }else{ | 579 }else{ |
339 iEnd = iStart; | 580 iEnd = iStart; |
340 } | 581 } |
341 if( iStart<1 || iEnd<iStart || iEnd>mxFrame ){ | 582 if( iStart<1 || iEnd<iStart || iEnd>mxFrame ){ |
342 fprintf(stderr, | 583 fprintf(stderr, |
343 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", | 584 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", |
344 mxFrame); | 585 mxFrame); |
345 exit(1); | 586 exit(1); |
346 } | 587 } |
347 while( iStart<=iEnd ){ | 588 while( iStart<=iEnd ){ |
348 print_frame(iStart); | 589 print_frame(iStart); |
349 iStart++; | 590 iStart++; |
350 } | 591 } |
351 } | 592 } |
352 } | 593 } |
353 close(fd); | 594 close(fd); |
354 return 0; | 595 return 0; |
355 } | 596 } |
OLD | NEW |