blob: dbd79e9586750a25ebc73c58d0422ab6afc824fc [file] [log] [blame]
drh0de8c112002-07-06 16:32:14 +00001/*
2** A utility for printing all or part of an SQLite database file.
3*/
4#include <stdio.h>
5#include <ctype.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
mistachkin026262b2012-10-13 09:31:20 +00009
10#if !defined(_MSC_VER)
drh0de8c112002-07-06 16:32:14 +000011#include <unistd.h>
mistachkin026262b2012-10-13 09:31:20 +000012#endif
13
drh0de8c112002-07-06 16:32:14 +000014#include <stdlib.h>
drh562cedb2010-04-26 15:44:07 +000015#include <string.h>
drh3aeea462012-04-03 14:59:50 +000016#include "sqlite3.h"
drh0de8c112002-07-06 16:32:14 +000017
18
drh562cedb2010-04-26 15:44:07 +000019static int pagesize = 1024; /* Size of a database page */
20static int db = -1; /* File descriptor for reading the DB */
21static int mxPage = 0; /* Last page number */
22static int perLine = 16; /* HEX elements to print per line */
drh0de8c112002-07-06 16:32:14 +000023
drh562cedb2010-04-26 15:44:07 +000024typedef long long int i64; /* Datatype for 64-bit integers */
25
26
27/*
28** Convert the var-int format into i64. Return the number of bytes
29** in the var-int. Write the var-int value into *pVal.
30*/
drh7ecc1472010-04-26 16:47:12 +000031static int decodeVarint(const unsigned char *z, i64 *pVal){
drh562cedb2010-04-26 15:44:07 +000032 i64 v = 0;
drh7ecc1472010-04-26 16:47:12 +000033 int i;
34 for(i=0; i<8; i++){
drh562cedb2010-04-26 15:44:07 +000035 v = (v<<7) + (z[i]&0x7f);
36 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
37 }
38 v = (v<<8) + (z[i]&0xff);
39 *pVal = v;
40 return 9;
41}
42
drh7d105f82010-08-23 15:26:49 +000043/*
44** Extract a big-endian 32-bit integer
45*/
46static unsigned int decodeInt32(const unsigned char *z){
47 return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
48}
49
drh562cedb2010-04-26 15:44:07 +000050/* Report an out-of-memory error and die.
51*/
drh0de8c112002-07-06 16:32:14 +000052static void out_of_memory(void){
53 fprintf(stderr,"Out of memory...\n");
54 exit(1);
55}
56
drh562cedb2010-04-26 15:44:07 +000057/*
58** Read content from the file.
59**
60** Space to hold the content is obtained from malloc() and needs to be
61** freed by the caller.
62*/
63static unsigned char *getContent(int ofst, int nByte){
64 unsigned char *aData;
drh5240aeb2011-01-06 01:26:38 +000065 aData = malloc(nByte+32);
drh562cedb2010-04-26 15:44:07 +000066 if( aData==0 ) out_of_memory();
drhb7787ee2011-01-06 15:51:18 +000067 memset(aData, 0, nByte+32);
drh562cedb2010-04-26 15:44:07 +000068 lseek(db, ofst, SEEK_SET);
69 read(db, aData, nByte);
70 return aData;
71}
72
73/*
74** Print a range of bytes as hex and as ascii.
75*/
76static unsigned char *print_byte_range(
77 int ofst, /* First byte in the range of bytes to print */
78 int nByte, /* Number of bytes to print */
79 int printOfst /* Add this amount to the index on the left column */
80){
drh0de8c112002-07-06 16:32:14 +000081 unsigned char *aData;
82 int i, j;
drh562cedb2010-04-26 15:44:07 +000083 const char *zOfstFmt;
84
85 if( ((printOfst+nByte)&~0xfff)==0 ){
86 zOfstFmt = " %03x: ";
87 }else if( ((printOfst+nByte)&~0xffff)==0 ){
88 zOfstFmt = " %04x: ";
89 }else if( ((printOfst+nByte)&~0xfffff)==0 ){
90 zOfstFmt = " %05x: ";
91 }else if( ((printOfst+nByte)&~0xffffff)==0 ){
92 zOfstFmt = " %06x: ";
93 }else{
94 zOfstFmt = " %08x: ";
95 }
96
97 aData = getContent(ofst, nByte);
98 for(i=0; i<nByte; i += perLine){
99 fprintf(stdout, zOfstFmt, i+printOfst);
drhc9ac5ca2005-11-04 22:03:30 +0000100 for(j=0; j<perLine; j++){
drh562cedb2010-04-26 15:44:07 +0000101 if( i+j>nByte ){
102 fprintf(stdout, " ");
103 }else{
104 fprintf(stdout,"%02x ", aData[i+j]);
105 }
drh0de8c112002-07-06 16:32:14 +0000106 }
drhc9ac5ca2005-11-04 22:03:30 +0000107 for(j=0; j<perLine; j++){
drh562cedb2010-04-26 15:44:07 +0000108 if( i+j>nByte ){
109 fprintf(stdout, " ");
110 }else{
111 fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
112 }
drh0de8c112002-07-06 16:32:14 +0000113 }
114 fprintf(stdout,"\n");
115 }
drh562cedb2010-04-26 15:44:07 +0000116 return aData;
117}
118
119/*
120** Print an entire page of content as hex
121*/
122static print_page(int iPg){
123 int iStart;
124 unsigned char *aData;
125 iStart = (iPg-1)*pagesize;
126 fprintf(stdout, "Page %d: (offsets 0x%x..0x%x)\n",
127 iPg, iStart, iStart+pagesize-1);
128 aData = print_byte_range(iStart, pagesize, 0);
drh0de8c112002-07-06 16:32:14 +0000129 free(aData);
130}
131
drh562cedb2010-04-26 15:44:07 +0000132/* Print a line of decode output showing a 4-byte integer.
133*/
134static print_decode_line(
135 unsigned char *aData, /* Content being decoded */
136 int ofst, int nByte, /* Start and size of decode */
137 const char *zMsg /* Message to append */
138){
139 int i, j;
140 int val = aData[ofst];
141 char zBuf[100];
142 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
143 i = strlen(zBuf);
144 for(j=1; j<4; j++){
145 if( j>=nByte ){
146 sprintf(&zBuf[i], " ");
147 }else{
148 sprintf(&zBuf[i], " %02x", aData[ofst+j]);
149 val = val*256 + aData[ofst+j];
150 }
151 i += strlen(&zBuf[i]);
152 }
153 sprintf(&zBuf[i], " %9d", val);
drh7ecc1472010-04-26 16:47:12 +0000154 printf("%s %s\n", zBuf, zMsg);
drh562cedb2010-04-26 15:44:07 +0000155}
156
157/*
158** Decode the database header.
159*/
drh7ecc1472010-04-26 16:47:12 +0000160static void print_db_header(void){
drh562cedb2010-04-26 15:44:07 +0000161 unsigned char *aData;
162 aData = print_byte_range(0, 100, 0);
163 printf("Decoded:\n");
164 print_decode_line(aData, 16, 2, "Database page size");
165 print_decode_line(aData, 18, 1, "File format write version");
166 print_decode_line(aData, 19, 1, "File format read version");
167 print_decode_line(aData, 20, 1, "Reserved space at end of page");
168 print_decode_line(aData, 24, 4, "File change counter");
169 print_decode_line(aData, 28, 4, "Size of database in pages");
170 print_decode_line(aData, 32, 4, "Page number of first freelist page");
171 print_decode_line(aData, 36, 4, "Number of freelist pages");
172 print_decode_line(aData, 40, 4, "Schema cookie");
173 print_decode_line(aData, 44, 4, "Schema format version");
174 print_decode_line(aData, 48, 4, "Default page cache size");
175 print_decode_line(aData, 52, 4, "Largest auto-vac root page");
176 print_decode_line(aData, 56, 4, "Text encoding");
177 print_decode_line(aData, 60, 4, "User version");
178 print_decode_line(aData, 64, 4, "Incremental-vacuum mode");
179 print_decode_line(aData, 68, 4, "meta[7]");
180 print_decode_line(aData, 72, 4, "meta[8]");
181 print_decode_line(aData, 76, 4, "meta[9]");
182 print_decode_line(aData, 80, 4, "meta[10]");
183 print_decode_line(aData, 84, 4, "meta[11]");
184 print_decode_line(aData, 88, 4, "meta[12]");
drhb28e59b2010-06-17 02:13:39 +0000185 print_decode_line(aData, 92, 4, "Change counter for version number");
drhc6d2b4a2010-04-26 17:30:52 +0000186 print_decode_line(aData, 96, 4, "SQLite version number");
drh562cedb2010-04-26 15:44:07 +0000187}
188
drh7ecc1472010-04-26 16:47:12 +0000189/*
drh100335b2011-01-05 21:20:52 +0000190** Describe cell content.
191*/
192static int describeContent(
drh5240aeb2011-01-06 01:26:38 +0000193 unsigned char *a, /* Cell content */
194 int nLocal, /* Bytes in a[] */
195 char *zDesc /* Write description here */
drh100335b2011-01-05 21:20:52 +0000196){
197 int nDesc = 0;
198 int n, i, j;
199 i64 x, v;
200 const unsigned char *pData;
drh5240aeb2011-01-06 01:26:38 +0000201 const unsigned char *pLimit;
drh100335b2011-01-05 21:20:52 +0000202 char sep = ' ';
203
drh5240aeb2011-01-06 01:26:38 +0000204 pLimit = &a[nLocal];
drh100335b2011-01-05 21:20:52 +0000205 n = decodeVarint(a, &x);
206 pData = &a[x];
207 a += n;
208 i = x - n;
drh5240aeb2011-01-06 01:26:38 +0000209 while( i>0 && pData<=pLimit ){
drh100335b2011-01-05 21:20:52 +0000210 n = decodeVarint(a, &x);
211 a += n;
212 i -= n;
drh5240aeb2011-01-06 01:26:38 +0000213 nLocal -= n;
drh100335b2011-01-05 21:20:52 +0000214 zDesc[0] = sep;
215 sep = ',';
216 nDesc++;
217 zDesc++;
218 if( x==0 ){
drh5240aeb2011-01-06 01:26:38 +0000219 sprintf(zDesc, "*"); /* NULL is a "*" */
drh100335b2011-01-05 21:20:52 +0000220 }else if( x>=1 && x<=6 ){
221 v = (signed char)pData[0];
222 pData++;
223 switch( x ){
224 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
225 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
226 case 4: v = (v<<8) + pData[0]; pData++;
227 case 3: v = (v<<8) + pData[0]; pData++;
228 case 2: v = (v<<8) + pData[0]; pData++;
229 }
230 sprintf(zDesc, "%lld", v);
231 }else if( x==7 ){
232 sprintf(zDesc, "real");
233 pData += 8;
234 }else if( x==8 ){
235 sprintf(zDesc, "0");
236 }else if( x==9 ){
237 sprintf(zDesc, "1");
238 }else if( x>=12 ){
239 int size = (x-12)/2;
drhb2c062d2011-01-05 21:46:52 +0000240 if( (x&1)==0 ){
drh100335b2011-01-05 21:20:52 +0000241 sprintf(zDesc, "blob(%d)", size);
242 }else{
drh5240aeb2011-01-06 01:26:38 +0000243 sprintf(zDesc, "txt(%d)", size);
drh100335b2011-01-05 21:20:52 +0000244 }
245 pData += size;
246 }
247 j = strlen(zDesc);
248 zDesc += j;
249 nDesc += j;
250 }
251 return nDesc;
252}
drh5240aeb2011-01-06 01:26:38 +0000253
254/*
255** Compute the local payload size given the total payload size and
256** the page size.
257*/
258static int localPayload(i64 nPayload, char cType){
259 int maxLocal;
260 int minLocal;
261 int surplus;
262 int nLocal;
263 if( cType==13 ){
264 /* Table leaf */
265 maxLocal = pagesize-35;
266 minLocal = (pagesize-12)*32/255-23;
267 }else{
268 maxLocal = (pagesize-12)*64/255-23;
269 minLocal = (pagesize-12)*32/255-23;
270 }
271 if( nPayload>maxLocal ){
272 surplus = minLocal + (nPayload-minLocal)%(pagesize-4);
273 if( surplus<=maxLocal ){
274 nLocal = surplus;
275 }else{
276 nLocal = minLocal;
277 }
278 }else{
279 nLocal = nPayload;
280 }
281 return nLocal;
282}
drh100335b2011-01-05 21:20:52 +0000283
284
285/*
drh7ecc1472010-04-26 16:47:12 +0000286** Create a description for a single cell.
drh5240aeb2011-01-06 01:26:38 +0000287**
288** The return value is the local cell size.
drh7ecc1472010-04-26 16:47:12 +0000289*/
drh100335b2011-01-05 21:20:52 +0000290static int describeCell(
291 unsigned char cType, /* Page type */
292 unsigned char *a, /* Cell content */
293 int showCellContent, /* Show cell content if true */
294 char **pzDesc /* Store description here */
295){
drh7ecc1472010-04-26 16:47:12 +0000296 int i;
297 int nDesc = 0;
298 int n = 0;
299 int leftChild;
300 i64 nPayload;
301 i64 rowid;
drh100335b2011-01-05 21:20:52 +0000302 int nLocal;
303 static char zDesc[1000];
drh7ecc1472010-04-26 16:47:12 +0000304 i = 0;
305 if( cType<=5 ){
306 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
307 a += 4;
308 n += 4;
drh100335b2011-01-05 21:20:52 +0000309 sprintf(zDesc, "lx: %d ", leftChild);
drh7ecc1472010-04-26 16:47:12 +0000310 nDesc = strlen(zDesc);
311 }
312 if( cType!=5 ){
313 i = decodeVarint(a, &nPayload);
314 a += i;
315 n += i;
drh100335b2011-01-05 21:20:52 +0000316 sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
drh7ecc1472010-04-26 16:47:12 +0000317 nDesc += strlen(&zDesc[nDesc]);
drh5240aeb2011-01-06 01:26:38 +0000318 nLocal = localPayload(nPayload, cType);
319 }else{
320 nPayload = nLocal = 0;
drh7ecc1472010-04-26 16:47:12 +0000321 }
322 if( cType==5 || cType==13 ){
323 i = decodeVarint(a, &rowid);
324 a += i;
325 n += i;
drh100335b2011-01-05 21:20:52 +0000326 sprintf(&zDesc[nDesc], "r: %lld ", rowid);
drh7ecc1472010-04-26 16:47:12 +0000327 nDesc += strlen(&zDesc[nDesc]);
328 }
drhb7787ee2011-01-06 15:51:18 +0000329 if( nLocal<nPayload ){
330 int ovfl;
331 unsigned char *b = &a[nLocal];
332 ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
333 sprintf(&zDesc[nDesc], "ov: %d ", ovfl);
334 nDesc += strlen(&zDesc[nDesc]);
335 n += 4;
336 }
drh100335b2011-01-05 21:20:52 +0000337 if( showCellContent && cType!=5 ){
drh5240aeb2011-01-06 01:26:38 +0000338 nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
drh100335b2011-01-05 21:20:52 +0000339 }
drh7ecc1472010-04-26 16:47:12 +0000340 *pzDesc = zDesc;
drh5240aeb2011-01-06 01:26:38 +0000341 return nLocal+n;
drh7ecc1472010-04-26 16:47:12 +0000342}
343
344/*
345** Decode a btree page
346*/
drh100335b2011-01-05 21:20:52 +0000347static void decode_btree_page(
348 unsigned char *a, /* Page content */
349 int pgno, /* Page number */
350 int hdrSize, /* Size of the page header. 0 or 100 */
351 char *zArgs /* Flags to control formatting */
352){
drh7ecc1472010-04-26 16:47:12 +0000353 const char *zType = "unknown";
354 int nCell;
drh5240aeb2011-01-06 01:26:38 +0000355 int i, j;
drh7ecc1472010-04-26 16:47:12 +0000356 int iCellPtr;
drh100335b2011-01-05 21:20:52 +0000357 int showCellContent = 0;
drh5240aeb2011-01-06 01:26:38 +0000358 int showMap = 0;
359 char *zMap = 0;
drh7ecc1472010-04-26 16:47:12 +0000360 switch( a[0] ){
361 case 2: zType = "index interior node"; break;
362 case 5: zType = "table interior node"; break;
363 case 10: zType = "index leaf"; break;
364 case 13: zType = "table leaf"; break;
365 }
drh100335b2011-01-05 21:20:52 +0000366 while( zArgs[0] ){
367 switch( zArgs[0] ){
368 case 'c': showCellContent = 1; break;
drh5240aeb2011-01-06 01:26:38 +0000369 case 'm': showMap = 1; break;
drh100335b2011-01-05 21:20:52 +0000370 }
371 zArgs++;
372 }
drh7ecc1472010-04-26 16:47:12 +0000373 printf("Decode of btree page %d:\n", pgno);
374 print_decode_line(a, 0, 1, zType);
375 print_decode_line(a, 1, 2, "Offset to first freeblock");
376 print_decode_line(a, 3, 2, "Number of cells on this page");
377 nCell = a[3]*256 + a[4];
378 print_decode_line(a, 5, 2, "Offset to cell content area");
379 print_decode_line(a, 7, 1, "Fragmented byte count");
380 if( a[0]==2 || a[0]==5 ){
381 print_decode_line(a, 8, 4, "Right child");
382 iCellPtr = 12;
383 }else{
384 iCellPtr = 8;
385 }
drh5240aeb2011-01-06 01:26:38 +0000386 if( nCell>0 ){
387 printf(" key: lx=left-child n=payload-size r=rowid\n");
388 }
389 if( showMap ){
390 zMap = malloc(pagesize);
391 memset(zMap, '.', pagesize);
392 memset(zMap, '1', hdrSize);
393 memset(&zMap[hdrSize], 'H', iCellPtr);
394 memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
395 }
drh7ecc1472010-04-26 16:47:12 +0000396 for(i=0; i<nCell; i++){
397 int cofst = iCellPtr + i*2;
398 char *zDesc;
drh5240aeb2011-01-06 01:26:38 +0000399 int n;
400
drh7ecc1472010-04-26 16:47:12 +0000401 cofst = a[cofst]*256 + a[cofst+1];
drh5240aeb2011-01-06 01:26:38 +0000402 n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
403 if( showMap ){
404 char zBuf[30];
405 memset(&zMap[cofst], '*', n);
406 zMap[cofst] = '[';
407 zMap[cofst+n-1] = ']';
408 sprintf(zBuf, "%d", i);
409 j = strlen(zBuf);
410 if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
411 }
drh7ecc1472010-04-26 16:47:12 +0000412 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
413 }
drh5240aeb2011-01-06 01:26:38 +0000414 if( showMap ){
415 for(i=0; i<pagesize; i+=64){
416 printf(" %03x: %.64s\n", i, &zMap[i]);
417 }
418 free(zMap);
419 }
drh7ecc1472010-04-26 16:47:12 +0000420}
421
drh7d105f82010-08-23 15:26:49 +0000422/*
423** Decode a freelist trunk page.
424*/
425static void decode_trunk_page(
426 int pgno, /* The page number */
427 int pagesize, /* Size of each page */
428 int detail, /* Show leaf pages if true */
429 int recursive /* Follow the trunk change if true */
430){
431 int n, i, k;
432 unsigned char *a;
433 while( pgno>0 ){
434 a = getContent((pgno-1)*pagesize, pagesize);
435 printf("Decode of freelist trunk page %d:\n", pgno);
436 print_decode_line(a, 0, 4, "Next freelist trunk page");
437 print_decode_line(a, 4, 4, "Number of entries on this page");
438 if( detail ){
439 n = (int)decodeInt32(&a[4]);
440 for(i=0; i<n; i++){
441 unsigned int x = decodeInt32(&a[8+4*i]);
442 char zIdx[10];
443 sprintf(zIdx, "[%d]", i);
444 printf(" %5s %7u", zIdx, x);
445 if( i%5==4 ) printf("\n");
446 }
447 if( i%5!=0 ) printf("\n");
448 }
449 if( !recursive ){
450 pgno = 0;
451 }else{
452 pgno = (int)decodeInt32(&a[0]);
453 }
454 free(a);
455 }
456}
457
458/*
drh3aeea462012-04-03 14:59:50 +0000459** A short text comment on the use of each page.
460*/
461static char **zPageUse;
462
463/*
464** Add a comment on the use of a page.
465*/
466static void page_usage_msg(int pgno, const char *zFormat, ...){
467 va_list ap;
468 char *zMsg;
469
470 va_start(ap, zFormat);
471 zMsg = sqlite3_vmprintf(zFormat, ap);
472 va_end(ap);
473 if( pgno<=0 || pgno>mxPage ){
drhedf9a172013-03-05 01:46:26 +0000474 printf("ERROR: page %d out of range 1..%d: %s\n",
drh3aeea462012-04-03 14:59:50 +0000475 pgno, mxPage, zMsg);
476 sqlite3_free(zMsg);
477 return;
478 }
479 if( zPageUse[pgno]!=0 ){
480 printf("ERROR: page %d used multiple times:\n", pgno);
481 printf("ERROR: previous: %s\n", zPageUse[pgno]);
drh103a70f2013-02-19 20:25:16 +0000482 printf("ERROR: current: %s\n", zMsg);
drh3aeea462012-04-03 14:59:50 +0000483 sqlite3_free(zPageUse[pgno]);
484 }
485 zPageUse[pgno] = zMsg;
486}
487
488/*
489** Find overflow pages of a cell and describe their usage.
490*/
491static void page_usage_cell(
492 unsigned char cType, /* Page type */
493 unsigned char *a, /* Cell content */
494 int pgno, /* page containing the cell */
495 int cellno /* Index of the cell on the page */
496){
497 int i;
498 int nDesc = 0;
499 int n = 0;
500 i64 nPayload;
501 i64 rowid;
502 int nLocal;
503 i = 0;
504 if( cType<=5 ){
505 a += 4;
506 n += 4;
507 }
508 if( cType!=5 ){
509 i = decodeVarint(a, &nPayload);
510 a += i;
511 n += i;
512 nLocal = localPayload(nPayload, cType);
513 }else{
514 nPayload = nLocal = 0;
515 }
516 if( cType==5 || cType==13 ){
517 i = decodeVarint(a, &rowid);
518 a += i;
519 n += i;
520 }
521 if( nLocal<nPayload ){
522 int ovfl = decodeInt32(a+nLocal);
523 int cnt = 0;
524 while( ovfl && (cnt++)<mxPage ){
525 page_usage_msg(ovfl, "overflow %d from cell %d of page %d",
526 cnt, cellno, pgno);
527 a = getContent((ovfl-1)*pagesize, 4);
528 ovfl = decodeInt32(a);
529 free(a);
530 }
531 }
532}
533
534
535/*
536** Describe the usages of a b-tree page
537*/
538static void page_usage_btree(
539 int pgno, /* Page to describe */
540 int parent, /* Parent of this page. 0 for root pages */
541 int idx, /* Which child of the parent */
542 const char *zName /* Name of the table */
543){
544 unsigned char *a;
545 const char *zType = "corrupt node";
546 int nCell;
547 int i;
548 int hdr = pgno==1 ? 100 : 0;
549
550 if( pgno<=0 || pgno>mxPage ) return;
551 a = getContent((pgno-1)*pagesize, pagesize);
552 switch( a[hdr] ){
553 case 2: zType = "interior node of index"; break;
554 case 5: zType = "interior node of table"; break;
555 case 10: zType = "leaf of index"; break;
556 case 13: zType = "leaf of table"; break;
557 }
558 if( parent ){
559 page_usage_msg(pgno, "%s [%s], child %d of page %d",
560 zType, zName, idx, parent);
561 }else{
562 page_usage_msg(pgno, "root %s [%s]", zType, zName);
563 }
564 nCell = a[hdr+3]*256 + a[hdr+4];
565 if( a[hdr]==2 || a[hdr]==5 ){
566 int cellstart = hdr+12;
567 unsigned int child;
568 for(i=0; i<nCell; i++){
569 int ofst;
570
571 ofst = cellstart + i*2;
572 ofst = a[ofst]*256 + a[ofst+1];
573 child = decodeInt32(a+ofst);
574 page_usage_btree(child, pgno, i, zName);
575 }
576 child = decodeInt32(a+cellstart-4);
577 page_usage_btree(child, pgno, i, zName);
578 }
579 if( a[hdr]==2 || a[hdr]==10 || a[hdr]==13 ){
580 int cellstart = hdr + 8 + 4*(a[hdr]<=5);
581 for(i=0; i<nCell; i++){
582 int ofst;
583 ofst = cellstart + i*2;
584 ofst = a[ofst]*256 + a[ofst+1];
585 page_usage_cell(a[hdr], a+ofst, pgno, i);
586 }
587 }
588 free(a);
589}
590
591/*
592** Determine page usage by the freelist
593*/
594static void page_usage_freelist(int pgno){
595 unsigned char *a;
596 int cnt = 0;
597 int i;
598 int n;
599 int iNext;
600 int parent = 1;
601
602 while( pgno>0 && pgno<=mxPage && (cnt++)<mxPage ){
603 page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent);
604 a = getContent((pgno-1)*pagesize, pagesize);
605 iNext = decodeInt32(a);
606 n = decodeInt32(a+4);
607 for(i=0; i<n; i++){
608 int child = decodeInt32(a + (i*4+8));
609 page_usage_msg(child, "freelist leaf, child %d of trunk page %d",
610 i, pgno);
611 }
612 free(a);
613 parent = pgno;
614 pgno = iNext;
615 }
616}
617
618/*
drh344a97b2013-02-19 22:26:51 +0000619** Determine pages used as PTRMAP pages
620*/
621static void page_usage_ptrmap(unsigned char *a){
622 if( a[55] ){
623 int usable = pagesize - a[20];
624 int pgno = 2;
625 int perPage = usable/5;
626 while( pgno<=mxPage ){
627 page_usage_msg(pgno, "PTRMAP page covering %d..%d",
628 pgno+1, pgno+perPage);
629 pgno += perPage + 1;
630 }
631 }
632}
633
634/*
drh3aeea462012-04-03 14:59:50 +0000635** Try to figure out how every page in the database file is being used.
636*/
637static void page_usage_report(const char *zDbName){
drh00e637f2013-02-19 18:45:11 +0000638 int i, j;
drh3aeea462012-04-03 14:59:50 +0000639 int rc;
640 sqlite3 *db;
641 sqlite3_stmt *pStmt;
642 unsigned char *a;
drh00e637f2013-02-19 18:45:11 +0000643 char zQuery[200];
drh3aeea462012-04-03 14:59:50 +0000644
645 /* Avoid the pathological case */
646 if( mxPage<1 ){
647 printf("empty database\n");
648 return;
649 }
650
651 /* Open the database file */
652 rc = sqlite3_open(zDbName, &db);
653 if( rc ){
654 printf("cannot open database: %s\n", sqlite3_errmsg(db));
655 sqlite3_close(db);
656 return;
657 }
658
659 /* Set up global variables zPageUse[] and mxPage to record page
660 ** usages */
661 zPageUse = sqlite3_malloc( sizeof(zPageUse[0])*(mxPage+1) );
662 if( zPageUse==0 ) out_of_memory();
663 memset(zPageUse, 0, sizeof(zPageUse[0])*(mxPage+1));
664
665 /* Discover the usage of each page */
666 a = getContent(0, 100);
667 page_usage_freelist(decodeInt32(a+32));
drh344a97b2013-02-19 22:26:51 +0000668 page_usage_ptrmap(a);
drh3aeea462012-04-03 14:59:50 +0000669 free(a);
670 page_usage_btree(1, 0, 0, "sqlite_master");
drh00e637f2013-02-19 18:45:11 +0000671 sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0);
672 for(j=0; j<2; j++){
673 sqlite3_snprintf(sizeof(zQuery), zQuery,
674 "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage"
675 " ORDER BY rowid %s", j?"DESC":"");
676 rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0);
677 if( rc==SQLITE_OK ){
678 while( sqlite3_step(pStmt)==SQLITE_ROW ){
679 int pgno = sqlite3_column_int(pStmt, 2);
680 page_usage_btree(pgno, 0, 0, sqlite3_column_text(pStmt, 1));
681 }
682 }else{
683 printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db));
drh3aeea462012-04-03 14:59:50 +0000684 }
drh00e637f2013-02-19 18:45:11 +0000685 rc = sqlite3_finalize(pStmt);
686 if( rc==SQLITE_OK ) break;
drh3aeea462012-04-03 14:59:50 +0000687 }
drh3aeea462012-04-03 14:59:50 +0000688 sqlite3_close(db);
689
690 /* Print the report and free memory used */
691 for(i=1; i<=mxPage; i++){
692 printf("%5d: %s\n", i, zPageUse[i] ? zPageUse[i] : "???");
693 sqlite3_free(zPageUse[i]);
694 }
695 sqlite3_free(zPageUse);
696 zPageUse = 0;
697}
698
699/*
drh344a97b2013-02-19 22:26:51 +0000700** Try to figure out how every page in the database file is being used.
701*/
702static void ptrmap_coverage_report(const char *zDbName){
703 unsigned int pgno;
704 unsigned char *aHdr;
705 unsigned char *a;
706 int usable;
707 int perPage;
708 unsigned int i;
709
710 /* Avoid the pathological case */
711 if( mxPage<1 ){
712 printf("empty database\n");
713 return;
714 }
715
716 /* Make sure PTRMAPs are used in this database */
717 aHdr = getContent(0, 100);
718 if( aHdr[55]==0 ){
719 printf("database does not use PTRMAP pages\n");
720 return;
721 }
722 usable = pagesize - aHdr[20];
723 perPage = usable/5;
724 free(aHdr);
725 printf("%5d: root of sqlite_master\n", 1);
726 for(pgno=2; pgno<=mxPage; pgno += perPage+1){
727 printf("%5d: PTRMAP page covering %d..%d\n", pgno,
728 pgno+1, pgno+perPage);
729 a = getContent((pgno-1)*pagesize, usable);
730 for(i=0; i+5<=usable && pgno+1+i/5<=mxPage; i+=5){
731 const char *zType = "???";
732 unsigned int iFrom = decodeInt32(&a[i+1]);
733 switch( a[i] ){
734 case 1: zType = "b-tree root page"; break;
735 case 2: zType = "freelist page"; break;
736 case 3: zType = "first page of overflow"; break;
737 case 4: zType = "later page of overflow"; break;
738 case 5: zType = "b-tree non-root page"; break;
739 }
740 printf("%5d: %s, parent=%u\n", pgno+1+i/5, zType, iFrom);
741 }
742 free(a);
743 }
744}
745
746/*
drh7d105f82010-08-23 15:26:49 +0000747** Print a usage comment
748*/
749static void usage(const char *argv0){
750 fprintf(stderr, "Usage %s FILENAME ?args...?\n\n", argv0);
751 fprintf(stderr,
752 "args:\n"
753 " dbheader Show database header\n"
drh3aeea462012-04-03 14:59:50 +0000754 " pgidx Index of how each page is used\n"
drh344a97b2013-02-19 22:26:51 +0000755 " ptrmap Show all PTRMAP page content\n"
drh7d105f82010-08-23 15:26:49 +0000756 " NNN..MMM Show hex of pages NNN through MMM\n"
757 " NNN..end Show hex of pages NNN through end of file\n"
758 " NNNb Decode btree page NNN\n"
drh5240aeb2011-01-06 01:26:38 +0000759 " NNNbc Decode btree page NNN and show content\n"
760 " NNNbm Decode btree page NNN and show a layout map\n"
drh7d105f82010-08-23 15:26:49 +0000761 " NNNt Decode freelist trunk page NNN\n"
drh47fb0002011-04-13 16:52:41 +0000762 " NNNtd Show leaf freelist pages on the decode\n"
drh7d105f82010-08-23 15:26:49 +0000763 " NNNtr Recurisvely decode freelist starting at NNN\n"
764 );
765}
766
drh0de8c112002-07-06 16:32:14 +0000767int main(int argc, char **argv){
768 struct stat sbuf;
drh562cedb2010-04-26 15:44:07 +0000769 unsigned char zPgSz[2];
drh0de8c112002-07-06 16:32:14 +0000770 if( argc<2 ){
drh7d105f82010-08-23 15:26:49 +0000771 usage(argv[0]);
drh0de8c112002-07-06 16:32:14 +0000772 exit(1);
773 }
774 db = open(argv[1], O_RDONLY);
775 if( db<0 ){
776 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
777 exit(1);
778 }
drh562cedb2010-04-26 15:44:07 +0000779 zPgSz[0] = 0;
780 zPgSz[1] = 0;
781 lseek(db, 16, SEEK_SET);
782 read(db, zPgSz, 2);
drh7d105f82010-08-23 15:26:49 +0000783 pagesize = zPgSz[0]*256 + zPgSz[1]*65536;
drh562cedb2010-04-26 15:44:07 +0000784 if( pagesize==0 ) pagesize = 1024;
785 printf("Pagesize: %d\n", pagesize);
drh0de8c112002-07-06 16:32:14 +0000786 fstat(db, &sbuf);
drh562cedb2010-04-26 15:44:07 +0000787 mxPage = sbuf.st_size/pagesize;
788 printf("Available pages: 1..%d\n", mxPage);
drh0de8c112002-07-06 16:32:14 +0000789 if( argc==2 ){
790 int i;
791 for(i=1; i<=mxPage; i++) print_page(i);
792 }else{
793 int i;
794 for(i=2; i<argc; i++){
795 int iStart, iEnd;
796 char *zLeft;
drh562cedb2010-04-26 15:44:07 +0000797 if( strcmp(argv[i], "dbheader")==0 ){
798 print_db_header();
799 continue;
800 }
drh3aeea462012-04-03 14:59:50 +0000801 if( strcmp(argv[i], "pgidx")==0 ){
802 page_usage_report(argv[1]);
803 continue;
804 }
drh344a97b2013-02-19 22:26:51 +0000805 if( strcmp(argv[i], "ptrmap")==0 ){
806 ptrmap_coverage_report(argv[1]);
807 continue;
808 }
809 if( strcmp(argv[i], "help")==0 ){
810 usage(argv[0]);
811 continue;
812 }
drh562cedb2010-04-26 15:44:07 +0000813 if( !isdigit(argv[i][0]) ){
814 fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]);
815 continue;
816 }
drh0de8c112002-07-06 16:32:14 +0000817 iStart = strtol(argv[i], &zLeft, 0);
818 if( zLeft && strcmp(zLeft,"..end")==0 ){
819 iEnd = mxPage;
820 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
821 iEnd = strtol(&zLeft[2], 0, 0);
drh7ecc1472010-04-26 16:47:12 +0000822 }else if( zLeft && zLeft[0]=='b' ){
823 int ofst, nByte, hdrSize;
824 unsigned char *a;
825 if( iStart==1 ){
826 ofst = hdrSize = 100;
827 nByte = pagesize-100;
828 }else{
829 hdrSize = 0;
830 ofst = (iStart-1)*pagesize;
831 nByte = pagesize;
832 }
833 a = getContent(ofst, nByte);
drh100335b2011-01-05 21:20:52 +0000834 decode_btree_page(a, iStart, hdrSize, &zLeft[1]);
drh7ecc1472010-04-26 16:47:12 +0000835 free(a);
836 continue;
drh7d105f82010-08-23 15:26:49 +0000837 }else if( zLeft && zLeft[0]=='t' ){
838 unsigned char *a;
839 int detail = 0;
840 int recursive = 0;
841 int i;
842 for(i=1; zLeft[i]; i++){
843 if( zLeft[i]=='r' ) recursive = 1;
844 if( zLeft[i]=='d' ) detail = 1;
845 }
846 decode_trunk_page(iStart, pagesize, detail, recursive);
847 continue;
drh0de8c112002-07-06 16:32:14 +0000848 }else{
849 iEnd = iStart;
850 }
851 if( iStart<1 || iEnd<iStart || iEnd>mxPage ){
852 fprintf(stderr,
853 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
854 mxPage);
855 exit(1);
856 }
857 while( iStart<=iEnd ){
858 print_page(iStart);
859 iStart++;
860 }
861 }
862 }
863 close(db);
864}