showwal.c 15 KB

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