blob: 8dd387365cc217e8d7f6bbee31090b7828cb9f4f [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);
drh4bb77ec2014-06-30 11:14:26 +000069 if( read(db, aData, nByte)<nByte ) memset(aData, 0, nByte);
drh562cedb2010-04-26 15:44:07 +000070 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*/
drhdb718d82014-01-28 20:36:22 +0000122static void print_page(int iPg){
drh562cedb2010-04-26 15:44:07 +0000123 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
drh6a30cd52014-06-19 23:38:53 +0000132
drh562cedb2010-04-26 15:44:07 +0000133/* Print a line of decode output showing a 4-byte integer.
134*/
drhdb718d82014-01-28 20:36:22 +0000135static void print_decode_line(
drh562cedb2010-04-26 15:44:07 +0000136 unsigned char *aData, /* Content being decoded */
137 int ofst, int nByte, /* Start and size of decode */
138 const char *zMsg /* Message to append */
139){
140 int i, j;
141 int val = aData[ofst];
142 char zBuf[100];
143 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
144 i = strlen(zBuf);
145 for(j=1; j<4; j++){
146 if( j>=nByte ){
147 sprintf(&zBuf[i], " ");
148 }else{
149 sprintf(&zBuf[i], " %02x", aData[ofst+j]);
150 val = val*256 + aData[ofst+j];
151 }
152 i += strlen(&zBuf[i]);
153 }
154 sprintf(&zBuf[i], " %9d", val);
drh7ecc1472010-04-26 16:47:12 +0000155 printf("%s %s\n", zBuf, zMsg);
drh562cedb2010-04-26 15:44:07 +0000156}
157
158/*
159** Decode the database header.
160*/
drh7ecc1472010-04-26 16:47:12 +0000161static void print_db_header(void){
drh562cedb2010-04-26 15:44:07 +0000162 unsigned char *aData;
163 aData = print_byte_range(0, 100, 0);
164 printf("Decoded:\n");
165 print_decode_line(aData, 16, 2, "Database page size");
166 print_decode_line(aData, 18, 1, "File format write version");
167 print_decode_line(aData, 19, 1, "File format read version");
168 print_decode_line(aData, 20, 1, "Reserved space at end of page");
169 print_decode_line(aData, 24, 4, "File change counter");
170 print_decode_line(aData, 28, 4, "Size of database in pages");
171 print_decode_line(aData, 32, 4, "Page number of first freelist page");
172 print_decode_line(aData, 36, 4, "Number of freelist pages");
173 print_decode_line(aData, 40, 4, "Schema cookie");
174 print_decode_line(aData, 44, 4, "Schema format version");
175 print_decode_line(aData, 48, 4, "Default page cache size");
176 print_decode_line(aData, 52, 4, "Largest auto-vac root page");
177 print_decode_line(aData, 56, 4, "Text encoding");
178 print_decode_line(aData, 60, 4, "User version");
179 print_decode_line(aData, 64, 4, "Incremental-vacuum mode");
drh4ee09b42013-05-01 19:49:27 +0000180 print_decode_line(aData, 68, 4, "Application ID");
drh562cedb2010-04-26 15:44:07 +0000181 print_decode_line(aData, 72, 4, "meta[8]");
182 print_decode_line(aData, 76, 4, "meta[9]");
183 print_decode_line(aData, 80, 4, "meta[10]");
184 print_decode_line(aData, 84, 4, "meta[11]");
185 print_decode_line(aData, 88, 4, "meta[12]");
drhb28e59b2010-06-17 02:13:39 +0000186 print_decode_line(aData, 92, 4, "Change counter for version number");
drhc6d2b4a2010-04-26 17:30:52 +0000187 print_decode_line(aData, 96, 4, "SQLite version number");
drh562cedb2010-04-26 15:44:07 +0000188}
189
drh7ecc1472010-04-26 16:47:12 +0000190/*
drh100335b2011-01-05 21:20:52 +0000191** Describe cell content.
192*/
193static int describeContent(
drh5240aeb2011-01-06 01:26:38 +0000194 unsigned char *a, /* Cell content */
195 int nLocal, /* Bytes in a[] */
196 char *zDesc /* Write description here */
drh100335b2011-01-05 21:20:52 +0000197){
198 int nDesc = 0;
199 int n, i, j;
200 i64 x, v;
201 const unsigned char *pData;
drh5240aeb2011-01-06 01:26:38 +0000202 const unsigned char *pLimit;
drh100335b2011-01-05 21:20:52 +0000203 char sep = ' ';
204
drh5240aeb2011-01-06 01:26:38 +0000205 pLimit = &a[nLocal];
drh100335b2011-01-05 21:20:52 +0000206 n = decodeVarint(a, &x);
207 pData = &a[x];
208 a += n;
209 i = x - n;
drh5240aeb2011-01-06 01:26:38 +0000210 while( i>0 && pData<=pLimit ){
drh100335b2011-01-05 21:20:52 +0000211 n = decodeVarint(a, &x);
212 a += n;
213 i -= n;
drh5240aeb2011-01-06 01:26:38 +0000214 nLocal -= n;
drh100335b2011-01-05 21:20:52 +0000215 zDesc[0] = sep;
216 sep = ',';
217 nDesc++;
218 zDesc++;
219 if( x==0 ){
drh5240aeb2011-01-06 01:26:38 +0000220 sprintf(zDesc, "*"); /* NULL is a "*" */
drh100335b2011-01-05 21:20:52 +0000221 }else if( x>=1 && x<=6 ){
222 v = (signed char)pData[0];
223 pData++;
224 switch( x ){
225 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
226 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
227 case 4: v = (v<<8) + pData[0]; pData++;
228 case 3: v = (v<<8) + pData[0]; pData++;
229 case 2: v = (v<<8) + pData[0]; pData++;
230 }
231 sprintf(zDesc, "%lld", v);
232 }else if( x==7 ){
233 sprintf(zDesc, "real");
234 pData += 8;
235 }else if( x==8 ){
236 sprintf(zDesc, "0");
237 }else if( x==9 ){
238 sprintf(zDesc, "1");
239 }else if( x>=12 ){
240 int size = (x-12)/2;
drhb2c062d2011-01-05 21:46:52 +0000241 if( (x&1)==0 ){
drh100335b2011-01-05 21:20:52 +0000242 sprintf(zDesc, "blob(%d)", size);
243 }else{
drh5240aeb2011-01-06 01:26:38 +0000244 sprintf(zDesc, "txt(%d)", size);
drh100335b2011-01-05 21:20:52 +0000245 }
246 pData += size;
247 }
248 j = strlen(zDesc);
249 zDesc += j;
250 nDesc += j;
251 }
252 return nDesc;
253}
drh5240aeb2011-01-06 01:26:38 +0000254
255/*
256** Compute the local payload size given the total payload size and
257** the page size.
258*/
259static int localPayload(i64 nPayload, char cType){
260 int maxLocal;
261 int minLocal;
262 int surplus;
263 int nLocal;
264 if( cType==13 ){
265 /* Table leaf */
266 maxLocal = pagesize-35;
267 minLocal = (pagesize-12)*32/255-23;
268 }else{
269 maxLocal = (pagesize-12)*64/255-23;
270 minLocal = (pagesize-12)*32/255-23;
271 }
272 if( nPayload>maxLocal ){
273 surplus = minLocal + (nPayload-minLocal)%(pagesize-4);
274 if( surplus<=maxLocal ){
275 nLocal = surplus;
276 }else{
277 nLocal = minLocal;
278 }
279 }else{
280 nLocal = nPayload;
281 }
282 return nLocal;
283}
drh100335b2011-01-05 21:20:52 +0000284
285
286/*
drh7ecc1472010-04-26 16:47:12 +0000287** Create a description for a single cell.
drh5240aeb2011-01-06 01:26:38 +0000288**
289** The return value is the local cell size.
drh7ecc1472010-04-26 16:47:12 +0000290*/
drh100335b2011-01-05 21:20:52 +0000291static int describeCell(
292 unsigned char cType, /* Page type */
293 unsigned char *a, /* Cell content */
294 int showCellContent, /* Show cell content if true */
295 char **pzDesc /* Store description here */
296){
drh7ecc1472010-04-26 16:47:12 +0000297 int i;
298 int nDesc = 0;
299 int n = 0;
300 int leftChild;
301 i64 nPayload;
302 i64 rowid;
drh100335b2011-01-05 21:20:52 +0000303 int nLocal;
304 static char zDesc[1000];
drh7ecc1472010-04-26 16:47:12 +0000305 i = 0;
306 if( cType<=5 ){
307 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
308 a += 4;
309 n += 4;
drh100335b2011-01-05 21:20:52 +0000310 sprintf(zDesc, "lx: %d ", leftChild);
drh7ecc1472010-04-26 16:47:12 +0000311 nDesc = strlen(zDesc);
312 }
313 if( cType!=5 ){
314 i = decodeVarint(a, &nPayload);
315 a += i;
316 n += i;
drh100335b2011-01-05 21:20:52 +0000317 sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
drh7ecc1472010-04-26 16:47:12 +0000318 nDesc += strlen(&zDesc[nDesc]);
drh5240aeb2011-01-06 01:26:38 +0000319 nLocal = localPayload(nPayload, cType);
320 }else{
321 nPayload = nLocal = 0;
drh7ecc1472010-04-26 16:47:12 +0000322 }
323 if( cType==5 || cType==13 ){
324 i = decodeVarint(a, &rowid);
325 a += i;
326 n += i;
drh100335b2011-01-05 21:20:52 +0000327 sprintf(&zDesc[nDesc], "r: %lld ", rowid);
drh7ecc1472010-04-26 16:47:12 +0000328 nDesc += strlen(&zDesc[nDesc]);
329 }
drhb7787ee2011-01-06 15:51:18 +0000330 if( nLocal<nPayload ){
331 int ovfl;
332 unsigned char *b = &a[nLocal];
333 ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
334 sprintf(&zDesc[nDesc], "ov: %d ", ovfl);
335 nDesc += strlen(&zDesc[nDesc]);
336 n += 4;
337 }
drh100335b2011-01-05 21:20:52 +0000338 if( showCellContent && cType!=5 ){
drh5240aeb2011-01-06 01:26:38 +0000339 nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
drh100335b2011-01-05 21:20:52 +0000340 }
drh7ecc1472010-04-26 16:47:12 +0000341 *pzDesc = zDesc;
drh5240aeb2011-01-06 01:26:38 +0000342 return nLocal+n;
drh7ecc1472010-04-26 16:47:12 +0000343}
344
drh6a30cd52014-06-19 23:38:53 +0000345/* Print an offset followed by nByte bytes. Add extra white-space
346** at the end so that subsequent text is aligned.
347*/
348static void printBytes(
349 unsigned char *aData, /* Content being decoded */
350 unsigned char *aStart, /* Start of content to be printed */
351 int nByte /* Number of bytes to print */
352){
353 int j;
354 printf(" %03x: ", (int)(aStart-aData));
355 for(j=0; j<9; j++){
356 if( j>=nByte ){
357 printf(" ");
358 }else{
359 printf("%02x ", aStart[j]);
360 }
361 }
362}
363
364
365/*
366** Write a full decode on stdout for the cell at a[ofst].
367** Assume the page contains a header of size szPgHdr bytes.
368*/
369static void decodeCell(
370 unsigned char *a, /* Page content (without the page-1 header) */
371 unsigned pgno, /* Page number */
372 int iCell, /* Cell index */
373 int szPgHdr, /* Size of the page header. 0 or 100 */
374 int ofst /* Cell begins at a[ofst] */
375){
376 int i, j, k;
377 int leftChild;
378 i64 nPayload;
379 i64 rowid;
380 i64 nHdr;
381 i64 iType;
382 int nLocal;
383 unsigned char *x = a + ofst;
384 unsigned char *end;
385 unsigned char cType = a[0];
drh419e0402014-06-20 01:32:42 +0000386 int nCol = 0;
387 int szCol[2000];
388 int ofstCol[2000];
389 int typeCol[2000];
drh6a30cd52014-06-19 23:38:53 +0000390
drh419e0402014-06-20 01:32:42 +0000391 printf("Cell[%d]:\n", iCell);
drh6a30cd52014-06-19 23:38:53 +0000392 if( cType<=5 ){
393 leftChild = ((x[0]*256 + x[1])*256 + x[2])*256 + x[3];
394 printBytes(a, x, 4);
395 printf("left child page:: %d\n", leftChild);
396 x += 4;
397 }
398 if( cType!=5 ){
399 i = decodeVarint(x, &nPayload);
400 printBytes(a, x, i);
401 nLocal = localPayload(nPayload, cType);
drh419e0402014-06-20 01:32:42 +0000402 if( nLocal==nPayload ){
403 printf("payload-size: %d\n", (int)nPayload);
404 }else{
405 printf("payload-size: %d (%d local, %d overflow)\n",
406 (int)nPayload, nLocal, (int)(nPayload-nLocal));
407 }
drh6a30cd52014-06-19 23:38:53 +0000408 x += i;
409 }else{
410 nPayload = nLocal = 0;
411 }
412 end = x + nLocal;
413 if( cType==5 || cType==13 ){
414 i = decodeVarint(x, &rowid);
415 printBytes(a, x, i);
416 printf("rowid: %lld\n", rowid);
417 x += i;
418 }
419 if( nLocal>0 ){
420 i = decodeVarint(x, &nHdr);
421 printBytes(a, x, i);
drh419e0402014-06-20 01:32:42 +0000422 printf("record-header-size: %d\n", (int)nHdr);
drh6a30cd52014-06-19 23:38:53 +0000423 j = i;
drh419e0402014-06-20 01:32:42 +0000424 nCol = 0;
425 k = nHdr;
drh6a30cd52014-06-19 23:38:53 +0000426 while( x+j<end && j<nHdr ){
427 const char *zTypeName;
428 int sz = 0;
429 char zNm[30];
430 i = decodeVarint(x+j, &iType);
431 printBytes(a, x+j, i);
drh419e0402014-06-20 01:32:42 +0000432 printf("typecode[%d]: %d - ", nCol, (int)iType);
drh6a30cd52014-06-19 23:38:53 +0000433 switch( iType ){
434 case 0: zTypeName = "NULL"; sz = 0; break;
435 case 1: zTypeName = "int8"; sz = 1; break;
436 case 2: zTypeName = "int16"; sz = 2; break;
437 case 3: zTypeName = "int24"; sz = 3; break;
438 case 4: zTypeName = "int32"; sz = 4; break;
439 case 5: zTypeName = "int48"; sz = 6; break;
440 case 6: zTypeName = "int64"; sz = 8; break;
441 case 7: zTypeName = "double"; sz = 8; break;
442 case 8: zTypeName = "zero"; sz = 0; break;
443 case 9: zTypeName = "one"; sz = 0; break;
444 case 10:
445 case 11: zTypeName = "error"; sz = 0; break;
446 default: {
447 sz = (int)(iType-12)/2;
448 sprintf(zNm, (iType&1)==0 ? "blob(%d)" : "text(%d)", sz);
449 zTypeName = zNm;
450 break;
451 }
452 }
453 printf("%s\n", zTypeName);
drh419e0402014-06-20 01:32:42 +0000454 szCol[nCol] = sz;
455 ofstCol[nCol] = k;
456 typeCol[nCol] = (int)iType;
457 k += sz;
458 nCol++;
drh6a30cd52014-06-19 23:38:53 +0000459 j += i;
460 }
drh419e0402014-06-20 01:32:42 +0000461 for(i=0; i<nCol && ofstCol[i]+szCol[i]<=nLocal; i++){
462 int s = ofstCol[i];
463 i64 v;
464 const unsigned char *pData;
465 if( szCol[i]==0 ) continue;
466 printBytes(a, x+s, szCol[i]);
467 printf("data[%d]: ", i);
468 pData = x+s;
469 if( typeCol[i]<=7 ){
470 v = (signed char)pData[0];
471 for(k=1; k<szCol[i]; k++){
472 v = (v<<8) + pData[k];
473 }
474 if( typeCol[i]==7 ){
475 double r;
476 memcpy(&r, &v, sizeof(r));
477 printf("%#g\n", r);
478 }else{
479 printf("%lld\n", v);
480 }
481 }else{
drh56e67dd2014-06-20 13:55:06 +0000482 int ii, jj;
483 char zConst[32];
484 if( (typeCol[i]&1)==0 ){
485 zConst[0] = 'x';
486 zConst[1] = '\'';
487 for(ii=2, jj=0; jj<szCol[i] && ii<24; jj++, ii+=2){
488 sprintf(zConst+ii, "%02x", pData[jj]);
489 }
490 }else{
491 zConst[0] = '\'';
492 for(ii=1, jj=0; jj<szCol[i] && ii<24; jj++, ii++){
493 zConst[ii] = isprint(pData[jj]) ? pData[jj] : '.';
494 }
495 zConst[ii] = 0;
496 }
497 if( jj<szCol[i] ){
498 memcpy(zConst+ii, "...'", 5);
499 }else{
500 memcpy(zConst+ii, "'", 2);
501 }
502 printf("%s\n", zConst);
drh419e0402014-06-20 01:32:42 +0000503 }
504 j = ofstCol[i] + szCol[i];
505 }
drh6a30cd52014-06-19 23:38:53 +0000506 }
507 if( j<nLocal ){
508 printBytes(a, x+j, 0);
drh419e0402014-06-20 01:32:42 +0000509 printf("... %d bytes of content ...\n", nLocal-j);
drh6a30cd52014-06-19 23:38:53 +0000510 }
511 if( nLocal<nPayload ){
512 printBytes(a, x+nLocal, 4);
drh419e0402014-06-20 01:32:42 +0000513 printf("overflow-page: %d\n", decodeInt32(x+nLocal));
drh6a30cd52014-06-19 23:38:53 +0000514 }
515}
516
517
drh7ecc1472010-04-26 16:47:12 +0000518/*
519** Decode a btree page
520*/
drh100335b2011-01-05 21:20:52 +0000521static void decode_btree_page(
522 unsigned char *a, /* Page content */
523 int pgno, /* Page number */
524 int hdrSize, /* Size of the page header. 0 or 100 */
525 char *zArgs /* Flags to control formatting */
526){
drh7ecc1472010-04-26 16:47:12 +0000527 const char *zType = "unknown";
528 int nCell;
drh5240aeb2011-01-06 01:26:38 +0000529 int i, j;
drh7ecc1472010-04-26 16:47:12 +0000530 int iCellPtr;
drh100335b2011-01-05 21:20:52 +0000531 int showCellContent = 0;
drh5240aeb2011-01-06 01:26:38 +0000532 int showMap = 0;
drh6a30cd52014-06-19 23:38:53 +0000533 int cellToDecode = -2;
drh5240aeb2011-01-06 01:26:38 +0000534 char *zMap = 0;
drh7ecc1472010-04-26 16:47:12 +0000535 switch( a[0] ){
536 case 2: zType = "index interior node"; break;
537 case 5: zType = "table interior node"; break;
538 case 10: zType = "index leaf"; break;
539 case 13: zType = "table leaf"; break;
540 }
drh100335b2011-01-05 21:20:52 +0000541 while( zArgs[0] ){
542 switch( zArgs[0] ){
543 case 'c': showCellContent = 1; break;
drh5240aeb2011-01-06 01:26:38 +0000544 case 'm': showMap = 1; break;
drh6a30cd52014-06-19 23:38:53 +0000545 case 'd': {
546 if( !isdigit(zArgs[1]) ){
547 cellToDecode = -1;
548 }else{
549 cellToDecode = 0;
550 while( isdigit(zArgs[1]) ){
551 zArgs++;
552 cellToDecode = cellToDecode*10 + zArgs[0] - '0';
553 }
554 }
555 break;
556 }
drh100335b2011-01-05 21:20:52 +0000557 }
558 zArgs++;
559 }
drh7ecc1472010-04-26 16:47:12 +0000560 nCell = a[3]*256 + a[4];
drh6a30cd52014-06-19 23:38:53 +0000561 iCellPtr = (a[0]==2 || a[0]==5) ? 12 : 8;
drh419e0402014-06-20 01:32:42 +0000562 if( cellToDecode>=nCell ){
drh6a30cd52014-06-19 23:38:53 +0000563 printf("Page %d has only %d cells\n", pgno, nCell);
564 return;
drh5240aeb2011-01-06 01:26:38 +0000565 }
drh419e0402014-06-20 01:32:42 +0000566 printf("Header on btree page %d:\n", pgno);
567 print_decode_line(a, 0, 1, zType);
568 print_decode_line(a, 1, 2, "Offset to first freeblock");
569 print_decode_line(a, 3, 2, "Number of cells on this page");
570 print_decode_line(a, 5, 2, "Offset to cell content area");
571 print_decode_line(a, 7, 1, "Fragmented byte count");
572 if( a[0]==2 || a[0]==5 ){
573 print_decode_line(a, 8, 4, "Right child");
574 }
575 if( cellToDecode==(-2) && nCell>0 ){
576 printf(" key: lx=left-child n=payload-size r=rowid\n");
577 }
drh5240aeb2011-01-06 01:26:38 +0000578 if( showMap ){
579 zMap = malloc(pagesize);
580 memset(zMap, '.', pagesize);
581 memset(zMap, '1', hdrSize);
582 memset(&zMap[hdrSize], 'H', iCellPtr);
583 memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
584 }
drh7ecc1472010-04-26 16:47:12 +0000585 for(i=0; i<nCell; i++){
586 int cofst = iCellPtr + i*2;
587 char *zDesc;
drh5240aeb2011-01-06 01:26:38 +0000588 int n;
589
drh7ecc1472010-04-26 16:47:12 +0000590 cofst = a[cofst]*256 + a[cofst+1];
drh5240aeb2011-01-06 01:26:38 +0000591 n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
592 if( showMap ){
593 char zBuf[30];
594 memset(&zMap[cofst], '*', n);
595 zMap[cofst] = '[';
596 zMap[cofst+n-1] = ']';
597 sprintf(zBuf, "%d", i);
598 j = strlen(zBuf);
599 if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
600 }
drh6a30cd52014-06-19 23:38:53 +0000601 if( cellToDecode==(-2) ){
602 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
603 }else if( cellToDecode==(-1) || cellToDecode==i ){
604 decodeCell(a, pgno, i, hdrSize, cofst-hdrSize);
605 }
drh7ecc1472010-04-26 16:47:12 +0000606 }
drh5240aeb2011-01-06 01:26:38 +0000607 if( showMap ){
drh419e0402014-06-20 01:32:42 +0000608 printf("Page map: (H=header P=cell-index 1=page-1-header .=free-space)\n");
drh5240aeb2011-01-06 01:26:38 +0000609 for(i=0; i<pagesize; i+=64){
610 printf(" %03x: %.64s\n", i, &zMap[i]);
611 }
612 free(zMap);
drh6a30cd52014-06-19 23:38:53 +0000613 }
drh7ecc1472010-04-26 16:47:12 +0000614}
615
drh7d105f82010-08-23 15:26:49 +0000616/*
617** Decode a freelist trunk page.
618*/
619static void decode_trunk_page(
620 int pgno, /* The page number */
621 int pagesize, /* Size of each page */
622 int detail, /* Show leaf pages if true */
623 int recursive /* Follow the trunk change if true */
624){
drhdb718d82014-01-28 20:36:22 +0000625 int n, i;
drh7d105f82010-08-23 15:26:49 +0000626 unsigned char *a;
627 while( pgno>0 ){
628 a = getContent((pgno-1)*pagesize, pagesize);
629 printf("Decode of freelist trunk page %d:\n", pgno);
630 print_decode_line(a, 0, 4, "Next freelist trunk page");
631 print_decode_line(a, 4, 4, "Number of entries on this page");
632 if( detail ){
633 n = (int)decodeInt32(&a[4]);
634 for(i=0; i<n; i++){
635 unsigned int x = decodeInt32(&a[8+4*i]);
636 char zIdx[10];
637 sprintf(zIdx, "[%d]", i);
638 printf(" %5s %7u", zIdx, x);
639 if( i%5==4 ) printf("\n");
640 }
641 if( i%5!=0 ) printf("\n");
642 }
643 if( !recursive ){
644 pgno = 0;
645 }else{
646 pgno = (int)decodeInt32(&a[0]);
647 }
648 free(a);
649 }
650}
651
652/*
drh3aeea462012-04-03 14:59:50 +0000653** A short text comment on the use of each page.
654*/
655static char **zPageUse;
656
657/*
658** Add a comment on the use of a page.
659*/
660static void page_usage_msg(int pgno, const char *zFormat, ...){
661 va_list ap;
662 char *zMsg;
663
664 va_start(ap, zFormat);
665 zMsg = sqlite3_vmprintf(zFormat, ap);
666 va_end(ap);
667 if( pgno<=0 || pgno>mxPage ){
drhedf9a172013-03-05 01:46:26 +0000668 printf("ERROR: page %d out of range 1..%d: %s\n",
drh3aeea462012-04-03 14:59:50 +0000669 pgno, mxPage, zMsg);
670 sqlite3_free(zMsg);
671 return;
672 }
673 if( zPageUse[pgno]!=0 ){
674 printf("ERROR: page %d used multiple times:\n", pgno);
675 printf("ERROR: previous: %s\n", zPageUse[pgno]);
drh103a70f2013-02-19 20:25:16 +0000676 printf("ERROR: current: %s\n", zMsg);
drh3aeea462012-04-03 14:59:50 +0000677 sqlite3_free(zPageUse[pgno]);
678 }
679 zPageUse[pgno] = zMsg;
680}
681
682/*
683** Find overflow pages of a cell and describe their usage.
684*/
685static void page_usage_cell(
686 unsigned char cType, /* Page type */
687 unsigned char *a, /* Cell content */
688 int pgno, /* page containing the cell */
689 int cellno /* Index of the cell on the page */
690){
691 int i;
drh3aeea462012-04-03 14:59:50 +0000692 int n = 0;
693 i64 nPayload;
694 i64 rowid;
695 int nLocal;
696 i = 0;
697 if( cType<=5 ){
698 a += 4;
699 n += 4;
700 }
701 if( cType!=5 ){
702 i = decodeVarint(a, &nPayload);
703 a += i;
704 n += i;
705 nLocal = localPayload(nPayload, cType);
706 }else{
707 nPayload = nLocal = 0;
708 }
709 if( cType==5 || cType==13 ){
710 i = decodeVarint(a, &rowid);
711 a += i;
712 n += i;
713 }
714 if( nLocal<nPayload ){
715 int ovfl = decodeInt32(a+nLocal);
716 int cnt = 0;
717 while( ovfl && (cnt++)<mxPage ){
718 page_usage_msg(ovfl, "overflow %d from cell %d of page %d",
719 cnt, cellno, pgno);
720 a = getContent((ovfl-1)*pagesize, 4);
721 ovfl = decodeInt32(a);
722 free(a);
723 }
724 }
725}
726
727
728/*
729** Describe the usages of a b-tree page
730*/
731static void page_usage_btree(
732 int pgno, /* Page to describe */
733 int parent, /* Parent of this page. 0 for root pages */
734 int idx, /* Which child of the parent */
735 const char *zName /* Name of the table */
736){
737 unsigned char *a;
738 const char *zType = "corrupt node";
739 int nCell;
740 int i;
741 int hdr = pgno==1 ? 100 : 0;
742
743 if( pgno<=0 || pgno>mxPage ) return;
744 a = getContent((pgno-1)*pagesize, pagesize);
745 switch( a[hdr] ){
746 case 2: zType = "interior node of index"; break;
747 case 5: zType = "interior node of table"; break;
748 case 10: zType = "leaf of index"; break;
749 case 13: zType = "leaf of table"; break;
750 }
751 if( parent ){
752 page_usage_msg(pgno, "%s [%s], child %d of page %d",
753 zType, zName, idx, parent);
754 }else{
755 page_usage_msg(pgno, "root %s [%s]", zType, zName);
756 }
757 nCell = a[hdr+3]*256 + a[hdr+4];
758 if( a[hdr]==2 || a[hdr]==5 ){
759 int cellstart = hdr+12;
760 unsigned int child;
761 for(i=0; i<nCell; i++){
762 int ofst;
763
764 ofst = cellstart + i*2;
765 ofst = a[ofst]*256 + a[ofst+1];
766 child = decodeInt32(a+ofst);
767 page_usage_btree(child, pgno, i, zName);
768 }
769 child = decodeInt32(a+cellstart-4);
770 page_usage_btree(child, pgno, i, zName);
771 }
772 if( a[hdr]==2 || a[hdr]==10 || a[hdr]==13 ){
773 int cellstart = hdr + 8 + 4*(a[hdr]<=5);
774 for(i=0; i<nCell; i++){
775 int ofst;
776 ofst = cellstart + i*2;
777 ofst = a[ofst]*256 + a[ofst+1];
778 page_usage_cell(a[hdr], a+ofst, pgno, i);
779 }
780 }
781 free(a);
782}
783
784/*
785** Determine page usage by the freelist
786*/
787static void page_usage_freelist(int pgno){
788 unsigned char *a;
789 int cnt = 0;
790 int i;
791 int n;
792 int iNext;
793 int parent = 1;
794
795 while( pgno>0 && pgno<=mxPage && (cnt++)<mxPage ){
796 page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent);
797 a = getContent((pgno-1)*pagesize, pagesize);
798 iNext = decodeInt32(a);
799 n = decodeInt32(a+4);
800 for(i=0; i<n; i++){
801 int child = decodeInt32(a + (i*4+8));
802 page_usage_msg(child, "freelist leaf, child %d of trunk page %d",
803 i, pgno);
804 }
805 free(a);
806 parent = pgno;
807 pgno = iNext;
808 }
809}
810
811/*
drh344a97b2013-02-19 22:26:51 +0000812** Determine pages used as PTRMAP pages
813*/
814static void page_usage_ptrmap(unsigned char *a){
815 if( a[55] ){
816 int usable = pagesize - a[20];
817 int pgno = 2;
818 int perPage = usable/5;
819 while( pgno<=mxPage ){
820 page_usage_msg(pgno, "PTRMAP page covering %d..%d",
821 pgno+1, pgno+perPage);
822 pgno += perPage + 1;
823 }
824 }
825}
826
827/*
drh3aeea462012-04-03 14:59:50 +0000828** Try to figure out how every page in the database file is being used.
829*/
830static void page_usage_report(const char *zDbName){
drh00e637f2013-02-19 18:45:11 +0000831 int i, j;
drh3aeea462012-04-03 14:59:50 +0000832 int rc;
833 sqlite3 *db;
834 sqlite3_stmt *pStmt;
835 unsigned char *a;
drh00e637f2013-02-19 18:45:11 +0000836 char zQuery[200];
drh3aeea462012-04-03 14:59:50 +0000837
838 /* Avoid the pathological case */
839 if( mxPage<1 ){
840 printf("empty database\n");
841 return;
842 }
843
844 /* Open the database file */
845 rc = sqlite3_open(zDbName, &db);
846 if( rc ){
847 printf("cannot open database: %s\n", sqlite3_errmsg(db));
848 sqlite3_close(db);
849 return;
850 }
851
852 /* Set up global variables zPageUse[] and mxPage to record page
853 ** usages */
854 zPageUse = sqlite3_malloc( sizeof(zPageUse[0])*(mxPage+1) );
855 if( zPageUse==0 ) out_of_memory();
856 memset(zPageUse, 0, sizeof(zPageUse[0])*(mxPage+1));
857
858 /* Discover the usage of each page */
859 a = getContent(0, 100);
860 page_usage_freelist(decodeInt32(a+32));
drh344a97b2013-02-19 22:26:51 +0000861 page_usage_ptrmap(a);
drh3aeea462012-04-03 14:59:50 +0000862 free(a);
863 page_usage_btree(1, 0, 0, "sqlite_master");
drh00e637f2013-02-19 18:45:11 +0000864 sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0);
865 for(j=0; j<2; j++){
866 sqlite3_snprintf(sizeof(zQuery), zQuery,
867 "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage"
868 " ORDER BY rowid %s", j?"DESC":"");
869 rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0);
870 if( rc==SQLITE_OK ){
871 while( sqlite3_step(pStmt)==SQLITE_ROW ){
872 int pgno = sqlite3_column_int(pStmt, 2);
drhdb718d82014-01-28 20:36:22 +0000873 page_usage_btree(pgno, 0, 0, (const char*)sqlite3_column_text(pStmt,1));
drh00e637f2013-02-19 18:45:11 +0000874 }
875 }else{
876 printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db));
drh3aeea462012-04-03 14:59:50 +0000877 }
drh00e637f2013-02-19 18:45:11 +0000878 rc = sqlite3_finalize(pStmt);
879 if( rc==SQLITE_OK ) break;
drh3aeea462012-04-03 14:59:50 +0000880 }
drh3aeea462012-04-03 14:59:50 +0000881 sqlite3_close(db);
882
883 /* Print the report and free memory used */
884 for(i=1; i<=mxPage; i++){
885 printf("%5d: %s\n", i, zPageUse[i] ? zPageUse[i] : "???");
886 sqlite3_free(zPageUse[i]);
887 }
888 sqlite3_free(zPageUse);
889 zPageUse = 0;
890}
891
892/*
drh344a97b2013-02-19 22:26:51 +0000893** Try to figure out how every page in the database file is being used.
894*/
895static void ptrmap_coverage_report(const char *zDbName){
896 unsigned int pgno;
897 unsigned char *aHdr;
898 unsigned char *a;
899 int usable;
900 int perPage;
901 unsigned int i;
902
903 /* Avoid the pathological case */
904 if( mxPage<1 ){
905 printf("empty database\n");
906 return;
907 }
908
909 /* Make sure PTRMAPs are used in this database */
910 aHdr = getContent(0, 100);
911 if( aHdr[55]==0 ){
912 printf("database does not use PTRMAP pages\n");
913 return;
914 }
915 usable = pagesize - aHdr[20];
916 perPage = usable/5;
917 free(aHdr);
918 printf("%5d: root of sqlite_master\n", 1);
919 for(pgno=2; pgno<=mxPage; pgno += perPage+1){
920 printf("%5d: PTRMAP page covering %d..%d\n", pgno,
921 pgno+1, pgno+perPage);
922 a = getContent((pgno-1)*pagesize, usable);
923 for(i=0; i+5<=usable && pgno+1+i/5<=mxPage; i+=5){
924 const char *zType = "???";
925 unsigned int iFrom = decodeInt32(&a[i+1]);
926 switch( a[i] ){
927 case 1: zType = "b-tree root page"; break;
928 case 2: zType = "freelist page"; break;
929 case 3: zType = "first page of overflow"; break;
930 case 4: zType = "later page of overflow"; break;
931 case 5: zType = "b-tree non-root page"; break;
932 }
933 printf("%5d: %s, parent=%u\n", pgno+1+i/5, zType, iFrom);
934 }
935 free(a);
936 }
937}
938
939/*
drh7d105f82010-08-23 15:26:49 +0000940** Print a usage comment
941*/
942static void usage(const char *argv0){
943 fprintf(stderr, "Usage %s FILENAME ?args...?\n\n", argv0);
944 fprintf(stderr,
945 "args:\n"
946 " dbheader Show database header\n"
drh3aeea462012-04-03 14:59:50 +0000947 " pgidx Index of how each page is used\n"
drh344a97b2013-02-19 22:26:51 +0000948 " ptrmap Show all PTRMAP page content\n"
drh7d105f82010-08-23 15:26:49 +0000949 " NNN..MMM Show hex of pages NNN through MMM\n"
950 " NNN..end Show hex of pages NNN through end of file\n"
951 " NNNb Decode btree page NNN\n"
drh5240aeb2011-01-06 01:26:38 +0000952 " NNNbc Decode btree page NNN and show content\n"
953 " NNNbm Decode btree page NNN and show a layout map\n"
drh6a30cd52014-06-19 23:38:53 +0000954 " NNNbdCCC Decode cell CCC on btree page NNN\n"
drh7d105f82010-08-23 15:26:49 +0000955 " NNNt Decode freelist trunk page NNN\n"
drh47fb0002011-04-13 16:52:41 +0000956 " NNNtd Show leaf freelist pages on the decode\n"
drh7d105f82010-08-23 15:26:49 +0000957 " NNNtr Recurisvely decode freelist starting at NNN\n"
958 );
959}
960
drh0de8c112002-07-06 16:32:14 +0000961int main(int argc, char **argv){
962 struct stat sbuf;
drh562cedb2010-04-26 15:44:07 +0000963 unsigned char zPgSz[2];
drh0de8c112002-07-06 16:32:14 +0000964 if( argc<2 ){
drh7d105f82010-08-23 15:26:49 +0000965 usage(argv[0]);
drh0de8c112002-07-06 16:32:14 +0000966 exit(1);
967 }
968 db = open(argv[1], O_RDONLY);
969 if( db<0 ){
970 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
971 exit(1);
972 }
drh562cedb2010-04-26 15:44:07 +0000973 zPgSz[0] = 0;
974 zPgSz[1] = 0;
975 lseek(db, 16, SEEK_SET);
drh4bb77ec2014-06-30 11:14:26 +0000976 if( read(db, zPgSz, 2)<2 ) memset(zPgSz, 0, 2);
drh7d105f82010-08-23 15:26:49 +0000977 pagesize = zPgSz[0]*256 + zPgSz[1]*65536;
drh562cedb2010-04-26 15:44:07 +0000978 if( pagesize==0 ) pagesize = 1024;
979 printf("Pagesize: %d\n", pagesize);
drh0de8c112002-07-06 16:32:14 +0000980 fstat(db, &sbuf);
drh562cedb2010-04-26 15:44:07 +0000981 mxPage = sbuf.st_size/pagesize;
982 printf("Available pages: 1..%d\n", mxPage);
drh0de8c112002-07-06 16:32:14 +0000983 if( argc==2 ){
984 int i;
985 for(i=1; i<=mxPage; i++) print_page(i);
986 }else{
987 int i;
988 for(i=2; i<argc; i++){
989 int iStart, iEnd;
990 char *zLeft;
drh562cedb2010-04-26 15:44:07 +0000991 if( strcmp(argv[i], "dbheader")==0 ){
992 print_db_header();
993 continue;
994 }
drh3aeea462012-04-03 14:59:50 +0000995 if( strcmp(argv[i], "pgidx")==0 ){
996 page_usage_report(argv[1]);
997 continue;
998 }
drh344a97b2013-02-19 22:26:51 +0000999 if( strcmp(argv[i], "ptrmap")==0 ){
1000 ptrmap_coverage_report(argv[1]);
1001 continue;
1002 }
1003 if( strcmp(argv[i], "help")==0 ){
1004 usage(argv[0]);
1005 continue;
1006 }
drh562cedb2010-04-26 15:44:07 +00001007 if( !isdigit(argv[i][0]) ){
1008 fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]);
1009 continue;
1010 }
drh0de8c112002-07-06 16:32:14 +00001011 iStart = strtol(argv[i], &zLeft, 0);
1012 if( zLeft && strcmp(zLeft,"..end")==0 ){
1013 iEnd = mxPage;
1014 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
1015 iEnd = strtol(&zLeft[2], 0, 0);
drh7ecc1472010-04-26 16:47:12 +00001016 }else if( zLeft && zLeft[0]=='b' ){
1017 int ofst, nByte, hdrSize;
1018 unsigned char *a;
1019 if( iStart==1 ){
1020 ofst = hdrSize = 100;
1021 nByte = pagesize-100;
1022 }else{
1023 hdrSize = 0;
1024 ofst = (iStart-1)*pagesize;
1025 nByte = pagesize;
1026 }
1027 a = getContent(ofst, nByte);
drh100335b2011-01-05 21:20:52 +00001028 decode_btree_page(a, iStart, hdrSize, &zLeft[1]);
drh7ecc1472010-04-26 16:47:12 +00001029 free(a);
1030 continue;
drh7d105f82010-08-23 15:26:49 +00001031 }else if( zLeft && zLeft[0]=='t' ){
drh7d105f82010-08-23 15:26:49 +00001032 int detail = 0;
1033 int recursive = 0;
1034 int i;
1035 for(i=1; zLeft[i]; i++){
1036 if( zLeft[i]=='r' ) recursive = 1;
1037 if( zLeft[i]=='d' ) detail = 1;
1038 }
1039 decode_trunk_page(iStart, pagesize, detail, recursive);
1040 continue;
drh0de8c112002-07-06 16:32:14 +00001041 }else{
1042 iEnd = iStart;
1043 }
1044 if( iStart<1 || iEnd<iStart || iEnd>mxPage ){
1045 fprintf(stderr,
1046 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
1047 mxPage);
1048 exit(1);
1049 }
1050 while( iStart<=iEnd ){
1051 print_page(iStart);
1052 iStart++;
1053 }
1054 }
1055 }
1056 close(db);
drhdb718d82014-01-28 20:36:22 +00001057 return 0;
drh0de8c112002-07-06 16:32:14 +00001058}