Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/tool/showdb.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 5 #include 6 #define ISDIGIT(X) isdigit((unsigned char)(X)) 7 #define ISPRINT(X) isprint((unsigned char)(X)) 8 #include 9 #include 10 #include 11 #include 12 13 #if !defined(_MSC_VER) 14 #include 15 #else 16 #include 17 #endif 18 19 #include 20 #include 21 #include 22 #include "sqlite3.h" 23 24 typedef unsigned char u8; /* unsigned 8-bit */ 25 typedef unsigned int u32; /* unsigned 32-bit */ 26 typedef sqlite3_int64 i64; /* signed 64-bit */ 27 typedef sqlite3_uint64 u64; /* unsigned 64-bit */ 28 29 30 static 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 */ 52 static 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 */ 67 static 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 */ 73 static void out_of_memory(void){ 74 fprintf(stderr,"Out of memory...\n"); 75 exit(1); 76 } 77 78 /* 79 ** Open a database connection. 80 */ 81 static 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 */ 109 static 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 */ 134 static 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 */ 151 static 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 && got0 && 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 */ 189 static 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 */ 212 static 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; inByte ){ break; } 238 if( aData[i+j] ){ go = 1; break; } 239 } 240 if( !go && i>0 && i+g.perLinenByte ){ 244 fprintf(stdout, " "); 245 }else{ 246 fprintf(stdout,"%02x ", aData[i+j]); 247 } 248 } 249 for(j=0; jnByte ){ 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 */ 264 static 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 */ 277 static 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 */ 303 static 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 */ 336 static 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 */ 402 static 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 */ 434 static 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=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 */ 512 static 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=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; i0 ){ 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; ig.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 */ 824 static 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( nLocalg.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= 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=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; i0 && pgno<=g.mxPage && (u32)(cnt++)(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; i4102444800000LL ){ /* 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 */ 1072 static 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 */ 1196 static 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 */ 1255 static 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 */ 1266 static 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 1289 int 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; ig.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 }