interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / tool / showdb.c

1420 lines · 39605 bytes Raw Blame History
1/*
2** A utility for printing all or part of an SQLite database file.
3*/
4#include <stdio.h>
5#include <ctype.h>
6#define ISDIGIT(X) isdigit((unsigned char)(X))
7#define ISPRINT(X) isprint((unsigned char)(X))
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
11#include <stdint.h>
12
13#if !defined(_MSC_VER)
14#include <unistd.h>
15#else
16#include <io.h>
17#endif
18
19#include <stdlib.h>
20#include <string.h>
21#include <assert.h>
22#include "sqlite3.h"
23
24typedef unsigned char u8; /* unsigned 8-bit */
25typedef unsigned int u32; /* unsigned 32-bit */
26typedef sqlite3_int64 i64; /* signed 64-bit */
27typedef sqlite3_uint64 u64; /* unsigned 64-bit */
28
29
30static struct GlobalData {
31 i64 pagesize; /* Size of a database page */
32 i64 usablesize; /* pagesize-nRes */
33 int dbfd; /* File descriptor for reading the DB */
34 u32 mxPage; /* Last page number */
35 u32 nRes; /* Amount of reserve space */
36 int perLine; /* HEX elements to print per line */
37 int bRaw; /* True to access db file via OS APIs */
38 int bCSV; /* CSV output for "pgidx" */
39 int bTmstmp; /* Interpret tmstmpvfs tags on "pgidx" */
40 sqlite3_file *pFd; /* File descriptor for non-raw mode */
41 sqlite3 *pDb; /* Database handle that owns pFd */
42 char **zPageUse; /* Use for each page */
43 struct TmstmpTag {
44 unsigned char a[16]; /* tmstmpvfs tag for each page */
45 } *aPageTag;
46} g = {4096, 4096, -1, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0};
47
48/*
49** Convert the var-int format into i64. Return the number of bytes
50** in the var-int. Write the var-int value into *pVal.
51*/
52static int decodeVarint(const unsigned char *z, i64 *pVal){
53 i64 v = 0;
54 int i;
55 for(i=0; i<8; i++){
56 v = (v<<7) + (z[i]&0x7f);
57 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
58 }
59 v = (v<<8) + (z[i]&0xff);
60 *pVal = v;
61 return 9;
62}
63
64/*
65** Extract a big-endian 32-bit integer
66*/
67static u32 decodeInt32(const u8 *z){
68 return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
69}
70
71/* Report an out-of-memory error and die.
72*/
73static void out_of_memory(void){
74 fprintf(stderr,"Out of memory...\n");
75 exit(1);
76}
77
78/*
79** Open a database connection.
80*/
81static sqlite3 *openDatabase(const char *zPrg, const char *zName){
82 sqlite3 *db = 0;
83 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI;
84 int rc = sqlite3_open_v2(zName, &db, flags, 0);
85 if( rc!=SQLITE_OK ){
86 const char *zErr = sqlite3_errmsg(db);
87 fprintf(stderr, "%s: can't open %s (%s)\n", zPrg, zName, zErr);
88 sqlite3_close(db);
89 exit(1);
90 }
91 return db;
92}
93
94/**************************************************************************
95** Beginning of low-level file access functions.
96**
97** All low-level access to the database file read by this program is
98** performed using the following four functions:
99**
100** fileOpen() - open the db file
101** fileClose() - close the db file
102** fileRead() - read raw data from the db file
103** fileGetsize() - return the size of the db file in bytes
104*/
105
106/*
107** Open the database file.
108*/
109static void fileOpen(const char *zPrg, const char *zName){
110 assert( g.dbfd<0 );
111 if( g.bRaw==0 ){
112 int rc;
113 void *pArg = (void *)(&g.pFd);
114 g.pDb = openDatabase(zPrg, zName);
115 rc = sqlite3_file_control(g.pDb, "main", SQLITE_FCNTL_FILE_POINTER, pArg);
116 if( rc!=SQLITE_OK ){
117 fprintf(stderr,
118 "%s: failed to obtain fd for %s (SQLite too old?)\n", zPrg, zName
119 );
120 exit(1);
121 }
122 }else{
123 g.dbfd = open(zName, O_RDONLY);
124 if( g.dbfd<0 ){
125 fprintf(stderr,"%s: can't open %s\n", zPrg, zName);
126 exit(1);
127 }
128 }
129}
130
131/*
132** Close the database file opened by fileOpen()
133*/
134static void fileClose(){
135 if( g.bRaw==0 ){
136 sqlite3_close(g.pDb);
137 g.pDb = 0;
138 g.pFd = 0;
139 }else{
140 close(g.dbfd);
141 g.dbfd = -1;
142 }
143}
144
145/*
146** Read content from the file.
147**
148** Space to hold the content is obtained from sqlite3_malloc() and needs
149** to be freed by the caller.
150*/
151static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){
152 unsigned char *aData;
153 int got;
154 int rc;
155 aData = sqlite3_malloc64(32+(i64)nByte);
156 if( aData==0 ) out_of_memory();
157 memset(aData, 0, nByte+32);
158 if( g.bRaw==0 ){
159 rc = g.pFd->pMethods->xRead(g.pFd, (void*)aData, nByte, ofst);
160 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
161 fprintf(stderr, "error in xRead() - %d\n", rc);
162 exit(1);
163 }
164 }else{
165 lseek(g.dbfd, (long)ofst, SEEK_SET);
166 got = read(g.dbfd, aData, nByte);
167 if( got==nByte ){
168 rc = SQLITE_OK;
169 }else if( got>0 && got<nByte ){
170 memset(aData+got, 0, nByte-got);
171 rc = SQLITE_IOERR_SHORT_READ;
172 }else{
173 memset(aData,0,nByte);
174 rc = SQLITE_IOERR;
175 }
176 }
177 if( g.aPageTag && nByte==(int)g.pagesize ){
178 unsigned int pgno = (unsigned int)(ofst/g.pagesize) + 1;
179 if( pgno>0 && pgno<=g.mxPage ){
180 memcpy(g.aPageTag[pgno].a, &aData[nByte-16], 16);
181 }
182 }
183 return aData;
184}
185
186/*
187** Return the size of the file in byte.
188*/
189static i64 fileGetsize(void){
190 i64 res = 0;
191 if( g.bRaw==0 ){
192 int rc = g.pFd->pMethods->xFileSize(g.pFd, &res);
193 if( rc!=SQLITE_OK ){
194 fprintf(stderr, "error in xFileSize() - %d\n", rc);
195 exit(1);
196 }
197 }else{
198 struct stat sbuf;
199 fstat(g.dbfd, &sbuf);
200 res = (sqlite3_int64)(sbuf.st_size);
201 }
202 return res;
203}
204
205/*
206** End of low-level file access functions.
207**************************************************************************/
208
209/*
210** Print a range of bytes as hex and as ascii.
211*/
212static unsigned char *print_byte_range(
213 sqlite3_int64 ofst, /* First byte in the range of bytes to print */
214 int nByte, /* Number of bytes to print */
215 int printOfst /* Add this amount to the index on the left column */
216){
217 unsigned char *aData;
218 int i, j;
219 const char *zOfstFmt;
220
221 if( ((printOfst+nByte)&~0xfff)==0 ){
222 zOfstFmt = " %03x: ";
223 }else if( ((printOfst+nByte)&~0xffff)==0 ){
224 zOfstFmt = " %04x: ";
225 }else if( ((printOfst+nByte)&~0xfffff)==0 ){
226 zOfstFmt = " %05x: ";
227 }else if( ((printOfst+nByte)&~0xffffff)==0 ){
228 zOfstFmt = " %06x: ";
229 }else{
230 zOfstFmt = " %08x: ";
231 }
232
233 aData = fileRead(ofst, nByte);
234 for(i=0; i<nByte; i += g.perLine){
235 int go = 0;
236 for(j=0; j<g.perLine; j++){
237 if( i+j>nByte ){ break; }
238 if( aData[i+j] ){ go = 1; break; }
239 }
240 if( !go && i>0 && i+g.perLine<nByte ) continue;
241 fprintf(stdout, zOfstFmt, i+printOfst);
242 for(j=0; j<g.perLine; j++){
243 if( i+j>nByte ){
244 fprintf(stdout, " ");
245 }else{
246 fprintf(stdout,"%02x ", aData[i+j]);
247 }
248 }
249 for(j=0; j<g.perLine; j++){
250 if( i+j>nByte ){
251 fprintf(stdout, " ");
252 }else{
253 fprintf(stdout,"%c", ISPRINT(aData[i+j]) ? aData[i+j] : '.');
254 }
255 }
256 fprintf(stdout,"\n");
257 }
258 return aData;
259}
260
261/*
262** Print an entire page of content as hex
263*/
264static void print_page(u32 iPg){
265 i64 iStart;
266 unsigned char *aData;
267 iStart = ((i64)(iPg-1))*g.pagesize;
268 fprintf(stdout, "Page %u: (offsets 0x%llx..0x%llx)\n",
269 iPg, iStart, iStart+g.pagesize-1);
270 aData = print_byte_range(iStart, g.pagesize, 0);
271 sqlite3_free(aData);
272}
273
274
275/* Print a line of decoded output showing a 4-byte unsigned integer.
276*/
277static void print_decode_line(
278 unsigned char *aData, /* Content being decoded */
279 int ofst, int nByte, /* Start and size of decode */
280 const char *zMsg /* Message to append */
281){
282 int i, j;
283 u32 val = aData[ofst];
284 char zBuf[100];
285 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
286 i = (int)strlen(zBuf);
287 for(j=1; j<4; j++){
288 if( j>=nByte ){
289 sprintf(&zBuf[i], " ");
290 }else{
291 sprintf(&zBuf[i], " %02x", aData[ofst+j]);
292 val = val*256 + aData[ofst+j];
293 }
294 i += (int)strlen(&zBuf[i]);
295 }
296 sprintf(&zBuf[i], " %10u", val);
297 printf("%s %s\n", zBuf, zMsg);
298}
299
300/*
301** Decode the database header.
302*/
303static void print_db_header(void){
304 unsigned char *aData;
305 aData = print_byte_range(0, 100, 0);
306 printf("Decoded:\n");
307 print_decode_line(aData, 16, 2, "Database page size");
308 print_decode_line(aData, 18, 1, "File format write version");
309 print_decode_line(aData, 19, 1, "File format read version");
310 print_decode_line(aData, 20, 1, "Reserved space at end of page");
311 print_decode_line(aData, 24, 4, "File change counter");
312 print_decode_line(aData, 28, 4, "Size of database in pages");
313 print_decode_line(aData, 32, 4, "Page number of first freelist page");
314 print_decode_line(aData, 36, 4, "Number of freelist pages");
315 print_decode_line(aData, 40, 4, "Schema cookie");
316 print_decode_line(aData, 44, 4, "Schema format version");
317 print_decode_line(aData, 48, 4, "Default page cache size");
318 print_decode_line(aData, 52, 4, "Largest auto-vac root page");
319 print_decode_line(aData, 56, 4, "Text encoding");
320 print_decode_line(aData, 60, 4, "User version");
321 print_decode_line(aData, 64, 4, "Incremental-vacuum mode");
322 print_decode_line(aData, 68, 4, "Application ID");
323 print_decode_line(aData, 72, 4, "meta[8]");
324 print_decode_line(aData, 76, 4, "meta[9]");
325 print_decode_line(aData, 80, 4, "meta[10]");
326 print_decode_line(aData, 84, 4, "meta[11]");
327 print_decode_line(aData, 88, 4, "meta[12]");
328 print_decode_line(aData, 92, 4, "Change counter for version number");
329 print_decode_line(aData, 96, 4, "SQLite version number");
330 sqlite3_free(aData);
331}
332
333/*
334** Describe cell content.
335*/
336static i64 describeContent(
337 unsigned char *a, /* Cell content */
338 i64 nLocal, /* Bytes in a[] */
339 char *zDesc /* Write description here */
340){
341 i64 nDesc = 0;
342 int n, j;
343 i64 i, x, v;
344 const unsigned char *pData;
345 const unsigned char *pLimit;
346 char sep = ' ';
347
348 pLimit = &a[nLocal];
349 n = decodeVarint(a, &x);
350 pData = &a[x];
351 a += n;
352 i = x - n;
353 while( i>0 && pData<=pLimit ){
354 n = decodeVarint(a, &x);
355 a += n;
356 i -= n;
357 nLocal -= n;
358 zDesc[0] = sep;
359 sep = ',';
360 nDesc++;
361 zDesc++;
362 if( x==0 ){
363 sprintf(zDesc, "*"); /* NULL is a "*" */
364 }else if( x>=1 && x<=6 ){
365 v = (signed char)pData[0];
366 pData++;
367 switch( x ){
368 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
369 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
370 case 4: v = (v<<8) + pData[0]; pData++;
371 case 3: v = (v<<8) + pData[0]; pData++;
372 case 2: v = (v<<8) + pData[0]; pData++;
373 }
374 sprintf(zDesc, "%lld", v);
375 }else if( x==7 ){
376 sprintf(zDesc, "real");
377 pData += 8;
378 }else if( x==8 ){
379 sprintf(zDesc, "0");
380 }else if( x==9 ){
381 sprintf(zDesc, "1");
382 }else if( x>=12 ){
383 i64 size = (x-12)/2;
384 if( (x&1)==0 ){
385 sprintf(zDesc, "blob(%lld)", size);
386 }else{
387 sprintf(zDesc, "txt(%lld)", size);
388 }
389 pData += size;
390 }
391 j = (int)strlen(zDesc);
392 zDesc += j;
393 nDesc += j;
394 }
395 return nDesc;
396}
397
398/*
399** Compute the local payload size given the total payload size and
400** the page size.
401*/
402static i64 localPayload(i64 nPayload, char cType){
403 i64 maxLocal;
404 i64 minLocal;
405 i64 surplus;
406 i64 nLocal;
407 if( cType==13 ){
408 /* Table leaf */
409 maxLocal = g.usablesize-35;
410 minLocal = (g.usablesize-12)*32/255-23;
411 }else{
412 maxLocal = (g.usablesize-12)*64/255-23;
413 minLocal = (g.usablesize-12)*32/255-23;
414 }
415 if( nPayload>maxLocal ){
416 surplus = minLocal + (nPayload-minLocal)%(g.usablesize-4);
417 if( surplus<=maxLocal ){
418 nLocal = surplus;
419 }else{
420 nLocal = minLocal;
421 }
422 }else{
423 nLocal = nPayload;
424 }
425 return nLocal;
426}
427
428
429/*
430** Create a description for a single cell.
431**
432** The return value is the local cell size.
433*/
434static i64 describeCell(
435 unsigned char cType, /* Page type */
436 unsigned char *a, /* Cell content */
437 int showCellContent, /* Show cell content if true */
438 char **pzDesc /* Store description here */
439){
440 int i;
441 i64 nDesc = 0;
442 int n = 0;
443 u32 leftChild;
444 i64 nPayload;
445 i64 rowid;
446 i64 nLocal;
447 static char zDesc[1000];
448 i = 0;
449 if( cType<=5 ){
450 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
451 a += 4;
452 n += 4;
453 sprintf(zDesc, "lx: %u ", leftChild);
454 nDesc = strlen(zDesc);
455 }
456 if( cType!=5 ){
457 i = decodeVarint(a, &nPayload);
458 a += i;
459 n += i;
460 sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
461 nDesc += strlen(&zDesc[nDesc]);
462 nLocal = localPayload(nPayload, cType);
463 }else{
464 nPayload = nLocal = 0;
465 }
466 if( cType==5 || cType==13 ){
467 i = decodeVarint(a, &rowid);
468 a += i;
469 n += i;
470 sprintf(&zDesc[nDesc], "r: %lld ", rowid);
471 nDesc += strlen(&zDesc[nDesc]);
472 }
473 if( nLocal<nPayload ){
474 u32 ovfl;
475 unsigned char *b = &a[nLocal];
476 ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
477 sprintf(&zDesc[nDesc], "ov: %u ", ovfl);
478 nDesc += strlen(&zDesc[nDesc]);
479 n += 4;
480 }
481 if( showCellContent && cType!=5 ){
482 nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
483 }
484 *pzDesc = zDesc;
485 return nLocal+n;
486}
487
488/* Print an offset followed by nByte bytes. Add extra white-space
489** at the end so that subsequent text is aligned.
490*/
491static void printBytes(
492 unsigned char *aData, /* Content being decoded */
493 unsigned char *aStart, /* Start of content to be printed */
494 int nByte /* Number of bytes to print */
495){
496 int j;
497 printf(" %03x: ", (int)(aStart-aData));
498 for(j=0; j<9; j++){
499 if( j>=nByte ){
500 printf(" ");
501 }else{
502 printf("%02x ", aStart[j]);
503 }
504 }
505}
506
507
508/*
509** Write a full decode on stdout for the cell at a[ofst].
510** Assume the page contains a header of size szPgHdr bytes.
511*/
512static void decodeCell(
513 unsigned char *a, /* Page content (without the page-1 header) */
514 unsigned pgno, /* Page number */
515 int iCell, /* Cell index */
516 int szPgHdr, /* Size of the page header. 0 or 100 */
517 int ofst /* Cell begins at a[ofst] */
518){
519 int i, j = 0;
520 u32 leftChild;
521 i64 k;
522 i64 nPayload;
523 i64 rowid;
524 i64 nHdr;
525 i64 iType;
526 i64 nLocal;
527 unsigned char *x = a + ofst;
528 unsigned char *end;
529 unsigned char cType = a[0];
530 int nCol = 0;
531 int szCol[2000];
532 int ofstCol[2000];
533 int typeCol[2000];
534
535 printf("Cell[%d]:\n", iCell);
536 if( cType<=5 ){
537 leftChild = ((x[0]*256 + x[1])*256 + x[2])*256 + x[3];
538 printBytes(a, x, 4);
539 printf("left child page:: %u\n", leftChild);
540 x += 4;
541 }
542 if( cType!=5 ){
543 i = decodeVarint(x, &nPayload);
544 printBytes(a, x, i);
545 nLocal = localPayload(nPayload, cType);
546 if( nLocal==nPayload ){
547 printf("payload-size: %lld\n", nPayload);
548 }else{
549 printf("payload-size: %lld (%lld local, %lld overflow)\n",
550 nPayload, nLocal, nPayload-nLocal);
551 }
552 x += i;
553 }else{
554 nPayload = nLocal = 0;
555 }
556 end = x + nLocal;
557 if( cType==5 || cType==13 ){
558 i = decodeVarint(x, &rowid);
559 printBytes(a, x, i);
560 printf("rowid: %lld\n", rowid);
561 x += i;
562 }
563 if( nLocal>0 ){
564 i = decodeVarint(x, &nHdr);
565 printBytes(a, x, i);
566 printf("record-header-size: %d\n", (int)nHdr);
567 j = i;
568 nCol = 0;
569 k = nHdr;
570 while( x+j<=end && j<nHdr ){
571 const char *zTypeName;
572 int sz = 0;
573 char zNm[30];
574 i = decodeVarint(x+j, &iType);
575 printBytes(a, x+j, i);
576 printf("typecode[%d]: %d - ", nCol, (int)iType);
577 switch( iType ){
578 case 0: zTypeName = "NULL"; sz = 0; break;
579 case 1: zTypeName = "int8"; sz = 1; break;
580 case 2: zTypeName = "int16"; sz = 2; break;
581 case 3: zTypeName = "int24"; sz = 3; break;
582 case 4: zTypeName = "int32"; sz = 4; break;
583 case 5: zTypeName = "int48"; sz = 6; break;
584 case 6: zTypeName = "int64"; sz = 8; break;
585 case 7: zTypeName = "double"; sz = 8; break;
586 case 8: zTypeName = "zero"; sz = 0; break;
587 case 9: zTypeName = "one"; sz = 0; break;
588 case 10:
589 case 11: zTypeName = "error"; sz = 0; break;
590 default: {
591 sz = (int)(iType-12)/2;
592 sprintf(zNm, (iType&1)==0 ? "blob(%d)" : "text(%d)", sz);
593 zTypeName = zNm;
594 break;
595 }
596 }
597 printf("%s\n", zTypeName);
598 szCol[nCol] = sz;
599 ofstCol[nCol] = (int)k;
600 typeCol[nCol] = (int)iType;
601 k += sz;
602 nCol++;
603 j += i;
604 }
605 for(i=0; i<nCol && ofstCol[i]+szCol[i]<=nLocal; i++){
606 int s = ofstCol[i];
607 i64 v;
608 const unsigned char *pData;
609 if( szCol[i]==0 ) continue;
610 printBytes(a, x+s, szCol[i]);
611 printf("data[%d]: ", i);
612 pData = x+s;
613 if( typeCol[i]<=7 ){
614 v = (signed char)pData[0];
615 for(k=1; k<szCol[i]; k++){
616 v = (v<<8) + pData[k];
617 }
618 if( typeCol[i]==7 ){
619 double r;
620 memcpy(&r, &v, sizeof(r));
621 printf("%#g\n", r);
622 }else{
623 printf("%lld\n", v);
624 }
625 }else{
626 int ii, jj;
627 char zConst[32];
628 if( (typeCol[i]&1)==0 ){
629 zConst[0] = 'x';
630 zConst[1] = '\'';
631 for(ii=2, jj=0; jj<szCol[i] && ii<24; jj++, ii+=2){
632 sprintf(zConst+ii, "%02x", pData[jj]);
633 }
634 }else{
635 zConst[0] = '\'';
636 for(ii=1, jj=0; jj<szCol[i] && ii<24; jj++, ii++){
637 zConst[ii] = ISPRINT(pData[jj]) ? pData[jj] : '.';
638 }
639 zConst[ii] = 0;
640 }
641 if( jj<szCol[i] ){
642 memcpy(zConst+ii, "...'", 5);
643 }else{
644 memcpy(zConst+ii, "'", 2);
645 }
646 printf("%s\n", zConst);
647 }
648 j = ofstCol[i] + szCol[i];
649 }
650 }
651 if( j<nLocal ){
652 printBytes(a, x+j, 0);
653 printf("... %lld bytes of content ...\n", nLocal-j);
654 }
655 if( nLocal<nPayload ){
656 printBytes(a, x+nLocal, 4);
657 printf("overflow-page: %u\n", decodeInt32(x+nLocal));
658 }
659}
660
661
662/*
663** Decode a btree page
664*/
665static void decode_btree_page(
666 unsigned char *a, /* Page content */
667 int pgno, /* Page number */
668 int hdrSize, /* Size of the page header. 0 or 100 */
669 char *zArgs /* Flags to control formatting */
670){
671 const char *zType = "unknown";
672 int nCell;
673 int i, j;
674 int iCellPtr;
675 int showCellContent = 0;
676 int showMap = 0;
677 int cellToDecode = -2;
678 char *zMap = 0;
679 switch( a[0] ){
680 case 2: zType = "index interior node"; break;
681 case 5: zType = "table interior node"; break;
682 case 10: zType = "index leaf"; break;
683 case 13: zType = "table leaf"; break;
684 }
685 while( zArgs[0] ){
686 switch( zArgs[0] ){
687 case 'c': showCellContent = 1; break;
688 case 'm': showMap = 1; break;
689 case 'd': {
690 if( !ISDIGIT(zArgs[1]) ){
691 cellToDecode = -1;
692 }else{
693 cellToDecode = 0;
694 while( ISDIGIT(zArgs[1]) ){
695 zArgs++;
696 cellToDecode = cellToDecode*10 + zArgs[0] - '0';
697 }
698 }
699 break;
700 }
701 }
702 zArgs++;
703 }
704 nCell = a[3]*256 + a[4];
705 iCellPtr = (a[0]==2 || a[0]==5) ? 12 : 8;
706 if( cellToDecode>=nCell ){
707 printf("Page %d has only %d cells\n", pgno, nCell);
708 return;
709 }
710 printf("Header on btree page %d:\n", pgno);
711 print_decode_line(a, 0, 1, zType);
712 print_decode_line(a, 1, 2, "Offset to first freeblock");
713 print_decode_line(a, 3, 2, "Number of cells on this page");
714 print_decode_line(a, 5, 2, "Offset to cell content area");
715 print_decode_line(a, 7, 1, "Fragmented byte count");
716 if( a[0]==2 || a[0]==5 ){
717 print_decode_line(a, 8, 4, "Right child");
718 }
719 if( cellToDecode==(-2) && nCell>0 ){
720 printf(" key: lx=left-child n=payload-size r=rowid\n");
721 }
722 if( showMap ){
723 zMap = sqlite3_malloc(g.pagesize);
724 memset(zMap, '.', g.pagesize);
725 memset(zMap, '1', hdrSize);
726 memset(&zMap[hdrSize], 'H', iCellPtr);
727 memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
728 }
729 for(i=0; i<nCell; i++){
730 int cofst = iCellPtr + i*2;
731 char *zDesc;
732 i64 n;
733
734 cofst = a[cofst]*256 + a[cofst+1];
735 n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
736 if( showMap ){
737 char zBuf[30];
738 memset(&zMap[cofst], '*', (size_t)n);
739 zMap[cofst] = '[';
740 zMap[cofst+n-1] = ']';
741 sprintf(zBuf, "%d", i);
742 j = (int)strlen(zBuf);
743 if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
744 }
745 if( cellToDecode==(-2) ){
746 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
747 }else if( cellToDecode==(-1) || cellToDecode==i ){
748 decodeCell(a, pgno, i, hdrSize, cofst-hdrSize);
749 }
750 }
751 if( showMap ){
752 printf("Page map: (H=header P=cell-index 1=page-1-header .=free-space)\n");
753 for(i=0; (u32)i<g.pagesize; i+=64){
754 printf(" %03x: %.64s\n", i, &zMap[i]);
755 }
756 sqlite3_free(zMap);
757 }
758}
759
760/*
761** Decode a freelist trunk page.
762*/
763static void decode_trunk_page(
764 u32 pgno, /* The page number */
765 int detail, /* Show leaf pages if true */
766 int recursive /* Follow the trunk change if true */
767){
768 u32 i;
769 u32 n;
770 unsigned char *a;
771 while( pgno>0 ){
772 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
773 printf("Decode of freelist trunk page %d:\n", pgno);
774 print_decode_line(a, 0, 4, "Next freelist trunk page");
775 print_decode_line(a, 4, 4, "Number of entries on this page");
776 if( detail ){
777 n = decodeInt32(&a[4]);
778 for(i=0; i<n && i<g.usablesize/4; i++){
779 u32 x = decodeInt32(&a[8+4*i]);
780 char zIdx[13];
781 sprintf(zIdx, "[%d]", i);
782 printf(" %5s %7u", zIdx, x);
783 if( i%5==4 ) printf("\n");
784 }
785 if( i%5!=0 ) printf("\n");
786 }
787 if( !recursive ){
788 pgno = 0;
789 }else{
790 pgno = decodeInt32(&a[0]);
791 }
792 sqlite3_free(a);
793 }
794}
795
796/*
797** Add a comment on the use of a page.
798*/
799static void page_usage_msg(u32 pgno, const char *zFormat, ...){
800 va_list ap;
801 char *zMsg;
802
803 va_start(ap, zFormat);
804 zMsg = sqlite3_vmprintf(zFormat, ap);
805 va_end(ap);
806 if( pgno<=0 || pgno>g.mxPage ){
807 printf("ERROR: page %d out of range 1..%u: %s\n",
808 pgno, g.mxPage, zMsg);
809 sqlite3_free(zMsg);
810 return;
811 }
812 if( g.zPageUse[pgno]!=0 ){
813 printf("ERROR: page %d used multiple times:\n", pgno);
814 printf("ERROR: previous: %s\n", g.zPageUse[pgno]);
815 printf("ERROR: current: %s\n", zMsg);
816 sqlite3_free(g.zPageUse[pgno]);
817 }
818 g.zPageUse[pgno] = zMsg;
819}
820
821/*
822** Find overflow pages of a cell and describe their usage.
823*/
824static void page_usage_cell(
825 unsigned char cType, /* Page type */
826 unsigned char *a, /* Cell content */
827 u32 pgno, /* page containing the cell */
828 int cellno /* Index of the cell on the page */
829){
830 int i;
831 int n = 0;
832 i64 nPayload;
833 i64 rowid;
834 i64 nLocal;
835 i = 0;
836 if( cType<=5 ){
837 a += 4;
838 n += 4;
839 }
840 if( cType!=5 ){
841 i = decodeVarint(a, &nPayload);
842 a += i;
843 n += i;
844 nLocal = localPayload(nPayload, cType);
845 }else{
846 nPayload = nLocal = 0;
847 }
848 if( cType==5 || cType==13 ){
849 i = decodeVarint(a, &rowid);
850 a += i;
851 n += i;
852 }
853 if( nLocal<nPayload ){
854 u32 ovfl = decodeInt32(a+nLocal);
855 u32 cnt = 0;
856 while( ovfl && (cnt++)<g.mxPage ){
857 page_usage_msg(ovfl, "overflow %d from cell %d of page %u",
858 cnt, cellno, pgno);
859 a = fileRead((ovfl-1)*(sqlite3_int64)g.pagesize, g.pagesize);
860 ovfl = decodeInt32(a);
861 sqlite3_free(a);
862 }
863 }
864}
865
866/*
867** True if the memory is all zeros
868*/
869static int allZero(unsigned char *a, int n){
870 while( n && (a++)[0]==0 ){ n--; }
871 return n==0;
872}
873
874
875/*
876** Describe the usages of a b-tree page.
877**
878** If parent==0, then this is the root of a btree. If parent<0 then
879** this is an orphan page.
880*/
881static void page_usage_btree(
882 u32 pgno, /* Page to describe */
883 int parent, /* Parent of this page. 0 for root pages */
884 int idx, /* Which child of the parent */
885 const char *zName /* Name of the table */
886){
887 unsigned char *a;
888 const char *zType = "corrupt node";
889 int nCell;
890 int i;
891 int hdr = pgno==1 ? 100 : 0;
892 char zEntry[30];
893
894 if( pgno<=0 || pgno>g.mxPage ) return;
895 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
896 switch( a[hdr] ){
897 case 0: {
898 if( allZero(a, g.pagesize) ){
899 zType = "zeroed page";
900 }else if( parent<0 ){
901 return;
902 }else{
903 zType = "corrupt node";
904 }
905 break;
906 }
907 case 2: zType = "interior node of index"; break;
908 case 5: zType = "interior node of table"; break;
909 case 10: zType = "leaf of index"; break;
910 case 13: zType = "leaf of table"; break;
911 default: {
912 if( parent<0 ) return;
913 zType = "corrupt node";
914 }
915 }
916 nCell = a[hdr+3]*256 + a[hdr+4];
917 if( nCell==1 ){
918 sqlite3_snprintf(sizeof(zEntry),zEntry,"1 row");
919 }else{
920 sqlite3_snprintf(sizeof(zEntry),zEntry,"%d rows", nCell);
921 }
922 if( parent>0 ){
923 page_usage_msg(pgno, "%s [%s], child %d of page %d, %s",
924 zType, zName, idx, parent, zEntry);
925 }else if( parent==0 ){
926 page_usage_msg(pgno, "root %s [%s], %s", zType, zName, zEntry);
927 }else{
928 page_usage_msg(pgno, "orphaned %s, %s", zType, zEntry);
929 }
930 if( a[hdr]==2 || a[hdr]==5 ){
931 int cellstart = hdr+12;
932 u32 child;
933 for(i=0; i<nCell; i++){
934 u32 cellidx;
935 u32 ofst;
936
937 cellidx = cellstart + i*2;
938 if( cellidx+1 >= g.usablesize ){
939 printf("ERROR: page %d too many cells (%d)\n", pgno, nCell);
940 break;
941 }
942 ofst = a[cellidx]*256 + a[cellidx+1];
943 if( ofst<cellidx+2 || ofst+4>=g.usablesize ){
944 printf("ERROR: page %d cell %d out of bounds\n", pgno, i);
945 continue;
946 }
947 child = decodeInt32(a+ofst);
948 page_usage_btree(child, pgno, i, zName);
949 }
950 child = decodeInt32(a+cellstart-4);
951 page_usage_btree(child, pgno, i, zName);
952 }
953 if( a[hdr]==2 || a[hdr]==10 || a[hdr]==13 ){
954 int cellstart = hdr + 8 + 4*(a[hdr]<=5);
955 for(i=0; i<nCell; i++){
956 int ofst;
957 ofst = cellstart + i*2;
958 ofst = a[ofst]*256 + a[ofst+1];
959 page_usage_cell(a[hdr], a+ofst, pgno, i);
960 }
961 }
962 sqlite3_free(a);
963}
964
965/*
966** Determine page usage by the freelist
967*/
968static void page_usage_freelist(u32 pgno){
969 unsigned char *a;
970 int cnt = 0;
971 int i;
972 int n;
973 int iNext;
974 int parent = 1;
975
976 while( pgno>0 && pgno<=g.mxPage && (u32)(cnt++)<g.mxPage ){
977 page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent);
978 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
979 iNext = decodeInt32(a);
980 n = decodeInt32(a+4);
981 if( n>(g.usablesize - 8)/4 ){
982 printf("ERROR: page %d too many freelist entries (%d)\n", pgno, n);
983 n = (g.usablesize - 8)/4;
984 }
985 for(i=0; i<n; i++){
986 int child = decodeInt32(a + (i*4+8));
987 page_usage_msg(child, "freelist leaf, child %d of trunk page %d",
988 i, pgno);
989 }
990 sqlite3_free(a);
991 parent = pgno;
992 pgno = iNext;
993 }
994}
995
996/*
997** Determine pages used as PTRMAP pages
998*/
999static void page_usage_ptrmap(u8 *a){
1000 if( decodeInt32(a+52) ){
1001 int usable = g.pagesize - a[20];
1002 u64 pgno = 2;
1003 int perPage = usable/5;
1004 while( pgno<=g.mxPage ){
1005 page_usage_msg((u32)pgno, "PTRMAP page covering %llu..%llu",
1006 pgno+1, pgno+perPage);
1007 pgno += perPage + 1;
1008 }
1009 }
1010}
1011
1012/*
1013** The six bytes at a[] are a big-endian unsigned integer which is the
1014** number of milliseconds since 1970. Decode that value into an ISO 8601
1015** date/time string stored in static space and return a pointer to that
1016** string.
1017*/
1018static const char *decodeTimestamp(const unsigned char *a){
1019 uint64_t ms; /* Milliseconds since 1970 */
1020 uint64_t days; /* Days since 1970-01-01 */
1021 uint64_t sod; /* Start of date specified by ms */
1022 uint64_t z; /* Days since 0000-03-01 */
1023 uint64_t era; /* 400-year era */
1024 int i; /* Loop counter */
1025 int h; /* hour */
1026 int m; /* minute */
1027 int s; /* second */
1028 int f; /* millisecond */
1029 int Y; /* year */
1030 int M; /* month */
1031 int D; /* day */
1032 int y; /* year assuming March is first month */
1033 unsigned int doe; /* day of 400-year era */
1034 unsigned int yoe; /* year of 400-year era */
1035 unsigned int doy; /* day of year */
1036 unsigned int mp; /* month with March==0 */
1037 static char zOut[50]; /* Return results here */
1038
1039 for(ms=0, i=0; i<=5; i++) ms = (ms<<8) + a[i];
1040 if( ms==0 ){
1041 return " ";
1042 }else if( ms>4102444800000LL ){ /* 2100-01-01 */
1043 /* YYYY-MM-DD HH:MM:SS.SSS */
1044 return " (bad date) ";
1045 }
1046 days = ms/86400000;
1047 sod = (ms%86400000)/1000;
1048 f = (int)(ms%1000);
1049
1050 h = sod/3600;
1051 m = (sod%3600)/60;
1052 s = sod%60;
1053 z = days + 719468;
1054 era = z/146097;
1055 doe = (unsigned)(z - era*146097);
1056 yoe = (doe - doe/1460 + doe/36524 - doe/146096)/365;
1057 y = (int)yoe + era*400;
1058 doy = doe - (365*yoe + yoe/4 - yoe/100);
1059 mp = (5*doy + 2)/153;
1060 D = doy - (153*mp + 2)/5 + 1;
1061 M = mp + (mp<10 ? 3 : -9);
1062 Y = y + (M <=2);
1063 snprintf(zOut, sizeof(zOut),
1064 "%04d-%02d-%02d %02d:%02d:%02d.%03d",
1065 Y, M, D, h, m, s, f);
1066 return zOut;
1067}
1068
1069/*
1070** Try to figure out how every page in the database file is being used.
1071*/
1072static void page_usage_report(const char *zPrg, const char *zDbName){
1073 u32 i, j;
1074 int rc;
1075 sqlite3 *db;
1076 sqlite3_stmt *pStmt;
1077 unsigned char *a;
1078 char zQuery[200];
1079
1080 /* Avoid the pathological case */
1081 if( g.mxPage<1 ){
1082 printf("empty database\n");
1083 return;
1084 }
1085
1086 /* Open the database file */
1087 db = openDatabase(zPrg, zDbName);
1088
1089 /* Set up global variables g.zPageUse[] and g.mxPage to record page
1090 ** usages */
1091 g.zPageUse = sqlite3_malloc64( sizeof(g.zPageUse[0])*(g.mxPage+1) );
1092 if( g.zPageUse==0 ) out_of_memory();
1093 memset(g.zPageUse, 0, sizeof(g.zPageUse[0])*(g.mxPage+1));
1094
1095 /* Discover the usage of each page */
1096 a = fileRead(0, 100);
1097 if( g.bTmstmp && a[20]==16 ){
1098 g.aPageTag = sqlite3_malloc64( sizeof(struct TmstmpTag)*(g.mxPage+1) );
1099 if( g.aPageTag==0 ) out_of_memory();
1100 memset(g.aPageTag, 0, sizeof(struct TmstmpTag)*(g.mxPage+1) );
1101 }else{
1102 g.bTmstmp = 0;
1103 g.aPageTag = 0;
1104 }
1105 page_usage_freelist(decodeInt32(a+32));
1106 page_usage_ptrmap(a);
1107 sqlite3_free(a);
1108 page_usage_btree(1, 0, 0, "sqlite_schema");
1109 sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0);
1110 for(j=0; j<2; j++){
1111 sqlite3_snprintf(sizeof(zQuery), zQuery,
1112 "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage"
1113 " ORDER BY rowid %s", j?"DESC":"");
1114 rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0);
1115 if( rc==SQLITE_OK ){
1116 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1117 u32 pgno = (u32)sqlite3_column_int64(pStmt, 2);
1118 page_usage_btree(pgno, 0, 0, (const char*)sqlite3_column_text(pStmt,1));
1119 }
1120 }else{
1121 printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db));
1122 }
1123 rc = sqlite3_finalize(pStmt);
1124 if( rc==SQLITE_OK ) break;
1125 }
1126 sqlite3_close(db);
1127
1128 /* Print the report and free memory used */
1129 if( g.bCSV ){
1130 if( g.bTmstmp ){
1131 printf("pgno,tm,frame,flg,salt,parent,child,ovfl,txt\r\n");
1132 }else{
1133 printf("pgno,parent,child,ovfl,txt\r\n");
1134 }
1135 }
1136 for(i=1; i<=g.mxPage; i++){
1137 if( g.zPageUse[i]==0 ){
1138 g.zPageUse[i] = sqlite3_mprintf("???");
1139 if( g.zPageUse[i]==0 ) continue;
1140 }
1141 if( g.bCSV ){
1142 const char *z = g.zPageUse[i];
1143 const char *s;
1144 printf("%u,", i);
1145 if( g.bTmstmp ){
1146 const unsigned char *a = g.aPageTag[i].a;
1147 sqlite3_uint64 tm = 0;
1148 unsigned int x;
1149 int k;
1150 for(k=2; k<=7; k++) tm = (tm<<8)+a[k];
1151 printf("%llu.%03u,", tm/1000, (unsigned int)(tm%1000));
1152 for(x=0, k=8; k<=11; k++) x = (x<<8)+a[k];
1153 printf("%u,", x);
1154 printf("%u,", a[12]);
1155 for(x=0, k=13; k<=15; k++) x = (x<<8)+a[k];
1156 printf("%u,", x);
1157 }
1158 if( (s = strstr(z, " of page "))!=0 ){
1159 printf("%d,", atoi(s+9));
1160 }else if( (s = strstr(z, " of trunk page "))!=0 ){
1161 printf("%d,", atoi(s+15));
1162 }else{
1163 printf("0,");
1164 }
1165 if( (s = strstr(z, "], child "))!=0 ){
1166 printf("%d,", atoi(s+9));
1167 }else if( (s = strstr(z, " from cell "))!=0 ){
1168 printf("%d,", atoi(s+12));
1169 }else{
1170 printf("-1,");
1171 }
1172 if( strncmp(z,"overflow ", 9)==0 ){
1173 printf("%d,", atoi(z+9));
1174 }else{
1175 printf("-1,");
1176 }
1177 printf("\"%s\"\r\n", z);
1178 }else if( g.bTmstmp ){
1179 printf("%5u: %s %s\n", i,
1180 decodeTimestamp(&g.aPageTag[i].a[2]),
1181 g.zPageUse[i]);
1182 }else{
1183 printf("%5u: %s\n", i, g.zPageUse[i]);
1184 }
1185 }
1186 for(i=1; i<=g.mxPage; i++){
1187 sqlite3_free(g.zPageUse[i]);
1188 }
1189 sqlite3_free(g.zPageUse);
1190 g.zPageUse = 0;
1191}
1192
1193/*
1194** Try to figure out how every page in the database file is being used.
1195*/
1196static void ptrmap_coverage_report(const char *zDbName){
1197 u64 pgno;
1198 unsigned char *aHdr;
1199 unsigned char *a;
1200 int usable;
1201 int perPage;
1202 int i;
1203
1204 /* Avoid the pathological case */
1205 if( g.mxPage<1 ){
1206 printf("empty database\n");
1207 return;
1208 }
1209
1210 /* Make sure PTRMAPs are used in this database */
1211 aHdr = fileRead(0, 100);
1212 if( aHdr[55]==0 ){
1213 printf("database does not use PTRMAP pages\n");
1214 return;
1215 }
1216 usable = g.pagesize - aHdr[20];
1217 perPage = usable/5;
1218 sqlite3_free(aHdr);
1219 printf("%5d: root of sqlite_schema\n", 1);
1220 for(pgno=2; pgno<=g.mxPage; pgno += perPage+1){
1221 printf("%5llu: PTRMAP page covering %llu..%llu\n", pgno,
1222 pgno+1, pgno+perPage);
1223 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
1224 for(i=0; i+5<=usable; i+=5){
1225 const char *zType;
1226 u32 iFrom = decodeInt32(&a[i+1]);
1227 const char *zExtra = pgno+1+i/5>g.mxPage ? " (off end of DB)" : "";
1228 switch( a[i] ){
1229 case 1: zType = "b-tree root page"; break;
1230 case 2: zType = "freelist page"; break;
1231 case 3: zType = "first page of overflow"; break;
1232 case 4: zType = "later page of overflow"; break;
1233 case 5: zType = "b-tree non-root page"; break;
1234 default: {
1235 if( zExtra[0]==0 ){
1236 printf("%5llu: invalid (0x%02x), parent=%u\n",
1237 pgno+1+i/5, a[i], iFrom);
1238 }
1239 zType = 0;
1240 break;
1241 }
1242 }
1243 if( zType ){
1244 printf("%5llu: %s, parent=%u%s\n", pgno+1+i/5, zType, iFrom, zExtra);
1245 }
1246 }
1247 sqlite3_free(a);
1248 }
1249}
1250
1251/*
1252** Check the range validity for a page number. Print an error and
1253** exit if the page is out of range.
1254*/
1255static void checkPageValidity(unsigned int iPage){
1256 if( iPage<1 || iPage>g.mxPage ){
1257 fprintf(stderr, "Invalid page number %d: valid range is 1..%d\n",
1258 iPage, g.mxPage);
1259 exit(1);
1260 }
1261}
1262
1263/*
1264** Print a usage comment
1265*/
1266static void usage(const char *argv0){
1267 fprintf(stderr, "Usage %s ?--uri? FILENAME ?args...?\n\n", argv0);
1268 fprintf(stderr,
1269 "switches:\n"
1270 " --csv CSV output for \"pgidx\"\n"
1271 " --raw Read db file directly, bypassing SQLite VFS\n"
1272 " --tmstmp Interpret tmstmpvfs tags\n"
1273 "args:\n"
1274 " dbheader Show database header\n"
1275 " pgidx Index of how each page is used\n"
1276 " ptrmap Show all PTRMAP page content\n"
1277 " NNN..MMM Show hex of pages NNN through MMM\n"
1278 " NNN..end Show hex of pages NNN through end of file\n"
1279 " NNNb Decode btree page NNN\n"
1280 " NNNbc Decode btree page NNN and show content\n"
1281 " NNNbm Decode btree page NNN and show a layout map\n"
1282 " NNNbdCCC Decode cell CCC on btree page NNN\n"
1283 " NNNt Decode freelist trunk page NNN\n"
1284 " NNNtd Show leaf freelist pages on the decode\n"
1285 " NNNtr Recursively decode freelist starting at NNN\n"
1286 );
1287}
1288
1289int main(int argc, char **argv){
1290 sqlite3_int64 szFile;
1291 unsigned char *zPgSz;
1292 const char *zPrg = argv[0]; /* Name of this executable */
1293 char **azArg = argv;
1294 int nArg = argc;
1295
1296 /* Check for the switches. */
1297 while( nArg>1 && azArg[1][0]=='-' ){
1298 const char *z = azArg[1];
1299 if( z[1]=='-' && z[2]!=0 ) z++;
1300 if( sqlite3_stricmp("-raw", z)==0 ){
1301 g.bRaw = 1;
1302 azArg++;
1303 nArg--;
1304 }else
1305 if( strcmp("-csv", z)==0 ){
1306 g.bCSV = 1;
1307 azArg++;
1308 nArg--;
1309 }else
1310 if( strcmp("-tmstmp", z)==0 ){
1311 g.bTmstmp = 1;
1312 azArg++;
1313 nArg--;
1314 }else
1315 {
1316 usage(zPrg);
1317 exit(1);
1318 }
1319 }
1320
1321 if( nArg<2 ){
1322 usage(zPrg);
1323 exit(1);
1324 }
1325
1326 fileOpen(zPrg, azArg[1]);
1327 szFile = fileGetsize();
1328
1329 zPgSz = fileRead(0, 24);
1330 g.pagesize = zPgSz[16]*256 + zPgSz[17]*65536;
1331 if( g.pagesize==0 ) g.pagesize = 4096;
1332 g.nRes = zPgSz[20];
1333 g.usablesize = g.pagesize - g.nRes;
1334 sqlite3_free(zPgSz);
1335 g.mxPage = (u32)((szFile+g.pagesize-1)/g.pagesize);
1336
1337 if( !g.bCSV ){
1338 printf("Pagesize: %d\n", (int)g.pagesize);
1339 if( g.nRes ) printf("Useable-size: %d\n", (int)g.usablesize);
1340 printf("Available pages: 1..%u\n", g.mxPage);
1341 }
1342 if( nArg==2 ){
1343 u32 i;
1344 for(i=1; i<=g.mxPage; i++) print_page(i);
1345 }else{
1346 int i;
1347 for(i=2; i<nArg; i++){
1348 u32 iStart, iEnd;
1349 char *zLeft;
1350 if( strcmp(azArg[i], "dbheader")==0 ){
1351 print_db_header();
1352 continue;
1353 }
1354 if( strcmp(azArg[i], "pgidx")==0 ){
1355 page_usage_report(zPrg, azArg[1]);
1356 continue;
1357 }
1358 if( strcmp(azArg[i], "ptrmap")==0 ){
1359 ptrmap_coverage_report(azArg[1]);
1360 continue;
1361 }
1362 if( strcmp(azArg[i], "help")==0 ){
1363 usage(zPrg);
1364 continue;
1365 }
1366 if( !ISDIGIT(azArg[i][0]) ){
1367 fprintf(stderr, "%s: unknown option: [%s]\n", zPrg, azArg[i]);
1368 continue;
1369 }
1370 iStart = strtoul(azArg[i], &zLeft, 0);
1371 checkPageValidity(iStart);
1372 if( zLeft && strcmp(zLeft,"..end")==0 ){
1373 iEnd = g.mxPage;
1374 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
1375 iEnd = strtol(&zLeft[2], 0, 0);
1376 checkPageValidity(iEnd);
1377 }else if( zLeft && zLeft[0]=='b' ){
1378 i64 ofst;
1379 int nByte, hdrSize;
1380 unsigned char *a;
1381 if( iStart==1 ){
1382 ofst = hdrSize = 100;
1383 nByte = g.pagesize-100;
1384 }else{
1385 hdrSize = 0;
1386 ofst = (iStart-1)*g.pagesize;
1387 nByte = g.pagesize;
1388 }
1389 a = fileRead(ofst, nByte);
1390 decode_btree_page(a, iStart, hdrSize, &zLeft[1]);
1391 sqlite3_free(a);
1392 continue;
1393 }else if( zLeft && zLeft[0]=='t' ){
1394 int detail = 0;
1395 int recursive = 0;
1396 int j;
1397 for(j=1; zLeft[j]; j++){
1398 if( zLeft[j]=='r' ) recursive = 1;
1399 if( zLeft[j]=='d' ) detail = 1;
1400 }
1401 decode_trunk_page(iStart, detail, recursive);
1402 continue;
1403 }else{
1404 iEnd = iStart;
1405 }
1406 if( iStart<1 || iEnd<iStart || iEnd>g.mxPage ){
1407 fprintf(stderr,
1408 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
1409 g.mxPage);
1410 exit(1);
1411 }
1412 while( iStart<=iEnd ){
1413 print_page(iStart);
1414 iStart++;
1415 }
1416 }
1417 }
1418 fileClose();
1419 return 0;
1420}