blob: 892cacd5520f10e638ed60783dab51ba2ca16ceb [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>
mistachkin0461cc42014-07-18 21:16:37 +000012#else
13#include <io.h>
mistachkin026262b2012-10-13 09:31:20 +000014#endif
15
drh0de8c112002-07-06 16:32:14 +000016#include <stdlib.h>
drh562cedb2010-04-26 15:44:07 +000017#include <string.h>
dan871f6e32015-08-03 17:03:31 +000018#include <assert.h>
drh3aeea462012-04-03 14:59:50 +000019#include "sqlite3.h"
drh0de8c112002-07-06 16:32:14 +000020
21
dan871f6e32015-08-03 17:03:31 +000022static struct GlobalData {
23 int pagesize; /* Size of a database page */
24 int dbfd; /* File descriptor for reading the DB */
25 int mxPage; /* Last page number */
26 int perLine; /* HEX elements to print per line */
dan8fb1bd22015-08-04 15:23:49 +000027 int bRaw; /* True to access db file via OS APIs */
dan30c16ad2015-08-04 15:29:43 +000028 sqlite3_file *pFd; /* File descriptor for non-raw mode */
dan871f6e32015-08-03 17:03:31 +000029 sqlite3 *pDb; /* Database handle that owns pFd */
30} g = {1024, -1, 0, 16, 0, 0, 0};
31
drh0de8c112002-07-06 16:32:14 +000032
drh562cedb2010-04-26 15:44:07 +000033typedef long long int i64; /* Datatype for 64-bit integers */
34
35
36/*
37** Convert the var-int format into i64. Return the number of bytes
38** in the var-int. Write the var-int value into *pVal.
39*/
drh7ecc1472010-04-26 16:47:12 +000040static int decodeVarint(const unsigned char *z, i64 *pVal){
drh562cedb2010-04-26 15:44:07 +000041 i64 v = 0;
drh7ecc1472010-04-26 16:47:12 +000042 int i;
43 for(i=0; i<8; i++){
drh562cedb2010-04-26 15:44:07 +000044 v = (v<<7) + (z[i]&0x7f);
45 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
46 }
47 v = (v<<8) + (z[i]&0xff);
48 *pVal = v;
49 return 9;
50}
51
drh7d105f82010-08-23 15:26:49 +000052/*
53** Extract a big-endian 32-bit integer
54*/
55static unsigned int decodeInt32(const unsigned char *z){
56 return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
57}
58
drh562cedb2010-04-26 15:44:07 +000059/* Report an out-of-memory error and die.
60*/
drh0de8c112002-07-06 16:32:14 +000061static void out_of_memory(void){
62 fprintf(stderr,"Out of memory...\n");
63 exit(1);
64}
65
drh562cedb2010-04-26 15:44:07 +000066/*
dan871f6e32015-08-03 17:03:31 +000067** Open a database connection.
68*/
69static sqlite3 *openDatabase(const char *zPrg, const char *zName){
70 sqlite3 *db = 0;
71 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI;
72 int rc = sqlite3_open_v2(zName, &db, flags, 0);
73 if( rc!=SQLITE_OK ){
74 const char *zErr = sqlite3_errmsg(db);
75 fprintf(stderr, "%s: can't open %s (%s)\n", zPrg, zName, zErr);
76 sqlite3_close(db);
77 exit(1);
78 }
79 return db;
80}
81
82/**************************************************************************
83** Beginning of low-level file access functions.
84**
85** All low-level access to the database file read by this program is
86** performed using the following four functions:
87**
88** fileOpen() - open the db file
89** fileClose() - close the db file
90** fileRead() - read raw data from the db file
91** fileGetsize() - return the size of the db file in bytes
92*/
93
94/*
95** Open the database file.
96*/
97static void fileOpen(const char *zPrg, const char *zName){
98 assert( g.dbfd<0 );
dan8fb1bd22015-08-04 15:23:49 +000099 if( g.bRaw==0 ){
dan871f6e32015-08-03 17:03:31 +0000100 int rc;
101 void *pArg = (void *)(&g.pFd);
102 g.pDb = openDatabase(zPrg, zName);
103 rc = sqlite3_file_control(g.pDb, "main", SQLITE_FCNTL_FILE_POINTER, pArg);
104 if( rc!=SQLITE_OK ){
105 fprintf(stderr,
106 "%s: failed to obtain fd for %s (SQLite too old?)\n", zPrg, zName
107 );
108 exit(1);
109 }
110 }else{
111 g.dbfd = open(zName, O_RDONLY);
112 if( g.dbfd<0 ){
113 fprintf(stderr,"%s: can't open %s\n", zPrg, zName);
114 exit(1);
115 }
116 }
117}
118
119/*
120** Close the database file opened by fileOpen()
121*/
122static void fileClose(){
dan8fb1bd22015-08-04 15:23:49 +0000123 if( g.bRaw==0 ){
dan871f6e32015-08-03 17:03:31 +0000124 sqlite3_close(g.pDb);
125 g.pDb = 0;
126 g.pFd = 0;
127 }else{
128 close(g.dbfd);
129 g.dbfd = -1;
130 }
131}
132
133/*
drh562cedb2010-04-26 15:44:07 +0000134** Read content from the file.
135**
dan871f6e32015-08-03 17:03:31 +0000136** Space to hold the content is obtained from sqlite3_malloc() and needs
137** to be freed by the caller.
drh562cedb2010-04-26 15:44:07 +0000138*/
dan871f6e32015-08-03 17:03:31 +0000139static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){
drh562cedb2010-04-26 15:44:07 +0000140 unsigned char *aData;
drh748c7352015-04-15 15:29:05 +0000141 int got;
dan871f6e32015-08-03 17:03:31 +0000142 aData = sqlite3_malloc(nByte+32);
drh562cedb2010-04-26 15:44:07 +0000143 if( aData==0 ) out_of_memory();
drhb7787ee2011-01-06 15:51:18 +0000144 memset(aData, 0, nByte+32);
dan8fb1bd22015-08-04 15:23:49 +0000145 if( g.bRaw==0 ){
dan871f6e32015-08-03 17:03:31 +0000146 int rc = g.pFd->pMethods->xRead(g.pFd, (void*)aData, nByte, ofst);
147 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
148 fprintf(stderr, "error in xRead() - %d\n", rc);
149 exit(1);
150 }
151 }else{
152 lseek(g.dbfd, ofst, SEEK_SET);
153 got = read(g.dbfd, aData, nByte);
154 if( got>0 && got<nByte ) memset(aData+got, 0, nByte-got);
155 }
drh562cedb2010-04-26 15:44:07 +0000156 return aData;
157}
158
159/*
dan871f6e32015-08-03 17:03:31 +0000160** Return the size of the file in byte.
161*/
162static sqlite3_int64 fileGetsize(void){
163 sqlite3_int64 res = 0;
dan8fb1bd22015-08-04 15:23:49 +0000164 if( g.bRaw==0 ){
dan871f6e32015-08-03 17:03:31 +0000165 int rc = g.pFd->pMethods->xFileSize(g.pFd, &res);
166 if( rc!=SQLITE_OK ){
167 fprintf(stderr, "error in xFileSize() - %d\n", rc);
168 exit(1);
169 }
170 }else{
171 struct stat sbuf;
172 fstat(g.dbfd, &sbuf);
173 res = (sqlite3_int64)(sbuf.st_size);
174 }
175 return res;
176}
177
178/*
179** End of low-level file access functions.
180**************************************************************************/
181
182/*
drh562cedb2010-04-26 15:44:07 +0000183** Print a range of bytes as hex and as ascii.
184*/
185static unsigned char *print_byte_range(
186 int ofst, /* First byte in the range of bytes to print */
187 int nByte, /* Number of bytes to print */
188 int printOfst /* Add this amount to the index on the left column */
189){
drh0de8c112002-07-06 16:32:14 +0000190 unsigned char *aData;
191 int i, j;
drh562cedb2010-04-26 15:44:07 +0000192 const char *zOfstFmt;
193
194 if( ((printOfst+nByte)&~0xfff)==0 ){
195 zOfstFmt = " %03x: ";
196 }else if( ((printOfst+nByte)&~0xffff)==0 ){
197 zOfstFmt = " %04x: ";
198 }else if( ((printOfst+nByte)&~0xfffff)==0 ){
199 zOfstFmt = " %05x: ";
200 }else if( ((printOfst+nByte)&~0xffffff)==0 ){
201 zOfstFmt = " %06x: ";
202 }else{
203 zOfstFmt = " %08x: ";
204 }
205
dan871f6e32015-08-03 17:03:31 +0000206 aData = fileRead(ofst, nByte);
207 for(i=0; i<nByte; i += g.perLine){
drh562cedb2010-04-26 15:44:07 +0000208 fprintf(stdout, zOfstFmt, i+printOfst);
dan871f6e32015-08-03 17:03:31 +0000209 for(j=0; j<g.perLine; j++){
drh562cedb2010-04-26 15:44:07 +0000210 if( i+j>nByte ){
211 fprintf(stdout, " ");
212 }else{
213 fprintf(stdout,"%02x ", aData[i+j]);
214 }
drh0de8c112002-07-06 16:32:14 +0000215 }
dan871f6e32015-08-03 17:03:31 +0000216 for(j=0; j<g.perLine; j++){
drh562cedb2010-04-26 15:44:07 +0000217 if( i+j>nByte ){
218 fprintf(stdout, " ");
219 }else{
220 fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
221 }
drh0de8c112002-07-06 16:32:14 +0000222 }
223 fprintf(stdout,"\n");
224 }
drh562cedb2010-04-26 15:44:07 +0000225 return aData;
226}
227
228/*
229** Print an entire page of content as hex
230*/
drhdb718d82014-01-28 20:36:22 +0000231static void print_page(int iPg){
drh562cedb2010-04-26 15:44:07 +0000232 int iStart;
233 unsigned char *aData;
dan871f6e32015-08-03 17:03:31 +0000234 iStart = (iPg-1)*g.pagesize;
drh562cedb2010-04-26 15:44:07 +0000235 fprintf(stdout, "Page %d: (offsets 0x%x..0x%x)\n",
dan871f6e32015-08-03 17:03:31 +0000236 iPg, iStart, iStart+g.pagesize-1);
237 aData = print_byte_range(iStart, g.pagesize, 0);
238 sqlite3_free(aData);
drh0de8c112002-07-06 16:32:14 +0000239}
240
drh6a30cd52014-06-19 23:38:53 +0000241
drh562cedb2010-04-26 15:44:07 +0000242/* Print a line of decode output showing a 4-byte integer.
243*/
drhdb718d82014-01-28 20:36:22 +0000244static void print_decode_line(
drh562cedb2010-04-26 15:44:07 +0000245 unsigned char *aData, /* Content being decoded */
246 int ofst, int nByte, /* Start and size of decode */
247 const char *zMsg /* Message to append */
248){
249 int i, j;
250 int val = aData[ofst];
251 char zBuf[100];
252 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
mistachkinef607032014-07-19 15:40:39 +0000253 i = (int)strlen(zBuf);
drh562cedb2010-04-26 15:44:07 +0000254 for(j=1; j<4; j++){
255 if( j>=nByte ){
256 sprintf(&zBuf[i], " ");
257 }else{
258 sprintf(&zBuf[i], " %02x", aData[ofst+j]);
259 val = val*256 + aData[ofst+j];
260 }
mistachkinef607032014-07-19 15:40:39 +0000261 i += (int)strlen(&zBuf[i]);
drh562cedb2010-04-26 15:44:07 +0000262 }
263 sprintf(&zBuf[i], " %9d", val);
drh7ecc1472010-04-26 16:47:12 +0000264 printf("%s %s\n", zBuf, zMsg);
drh562cedb2010-04-26 15:44:07 +0000265}
266
267/*
268** Decode the database header.
269*/
drh7ecc1472010-04-26 16:47:12 +0000270static void print_db_header(void){
drh562cedb2010-04-26 15:44:07 +0000271 unsigned char *aData;
272 aData = print_byte_range(0, 100, 0);
273 printf("Decoded:\n");
274 print_decode_line(aData, 16, 2, "Database page size");
275 print_decode_line(aData, 18, 1, "File format write version");
276 print_decode_line(aData, 19, 1, "File format read version");
277 print_decode_line(aData, 20, 1, "Reserved space at end of page");
278 print_decode_line(aData, 24, 4, "File change counter");
279 print_decode_line(aData, 28, 4, "Size of database in pages");
280 print_decode_line(aData, 32, 4, "Page number of first freelist page");
281 print_decode_line(aData, 36, 4, "Number of freelist pages");
282 print_decode_line(aData, 40, 4, "Schema cookie");
283 print_decode_line(aData, 44, 4, "Schema format version");
284 print_decode_line(aData, 48, 4, "Default page cache size");
285 print_decode_line(aData, 52, 4, "Largest auto-vac root page");
286 print_decode_line(aData, 56, 4, "Text encoding");
287 print_decode_line(aData, 60, 4, "User version");
288 print_decode_line(aData, 64, 4, "Incremental-vacuum mode");
drh4ee09b42013-05-01 19:49:27 +0000289 print_decode_line(aData, 68, 4, "Application ID");
drh562cedb2010-04-26 15:44:07 +0000290 print_decode_line(aData, 72, 4, "meta[8]");
291 print_decode_line(aData, 76, 4, "meta[9]");
292 print_decode_line(aData, 80, 4, "meta[10]");
293 print_decode_line(aData, 84, 4, "meta[11]");
294 print_decode_line(aData, 88, 4, "meta[12]");
drhb28e59b2010-06-17 02:13:39 +0000295 print_decode_line(aData, 92, 4, "Change counter for version number");
drhc6d2b4a2010-04-26 17:30:52 +0000296 print_decode_line(aData, 96, 4, "SQLite version number");
drh562cedb2010-04-26 15:44:07 +0000297}
298
drh7ecc1472010-04-26 16:47:12 +0000299/*
drh100335b2011-01-05 21:20:52 +0000300** Describe cell content.
301*/
mistachkin0461cc42014-07-18 21:16:37 +0000302static i64 describeContent(
drh5240aeb2011-01-06 01:26:38 +0000303 unsigned char *a, /* Cell content */
mistachkin0461cc42014-07-18 21:16:37 +0000304 i64 nLocal, /* Bytes in a[] */
drh5240aeb2011-01-06 01:26:38 +0000305 char *zDesc /* Write description here */
drh100335b2011-01-05 21:20:52 +0000306){
mistachkin0461cc42014-07-18 21:16:37 +0000307 i64 nDesc = 0;
308 int n, j;
309 i64 i, x, v;
drh100335b2011-01-05 21:20:52 +0000310 const unsigned char *pData;
drh5240aeb2011-01-06 01:26:38 +0000311 const unsigned char *pLimit;
drh100335b2011-01-05 21:20:52 +0000312 char sep = ' ';
313
drh5240aeb2011-01-06 01:26:38 +0000314 pLimit = &a[nLocal];
drh100335b2011-01-05 21:20:52 +0000315 n = decodeVarint(a, &x);
316 pData = &a[x];
317 a += n;
318 i = x - n;
drh5240aeb2011-01-06 01:26:38 +0000319 while( i>0 && pData<=pLimit ){
drh100335b2011-01-05 21:20:52 +0000320 n = decodeVarint(a, &x);
321 a += n;
322 i -= n;
drh5240aeb2011-01-06 01:26:38 +0000323 nLocal -= n;
drh100335b2011-01-05 21:20:52 +0000324 zDesc[0] = sep;
325 sep = ',';
326 nDesc++;
327 zDesc++;
328 if( x==0 ){
drh5240aeb2011-01-06 01:26:38 +0000329 sprintf(zDesc, "*"); /* NULL is a "*" */
drh100335b2011-01-05 21:20:52 +0000330 }else if( x>=1 && x<=6 ){
331 v = (signed char)pData[0];
332 pData++;
333 switch( x ){
334 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
335 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
336 case 4: v = (v<<8) + pData[0]; pData++;
337 case 3: v = (v<<8) + pData[0]; pData++;
338 case 2: v = (v<<8) + pData[0]; pData++;
339 }
340 sprintf(zDesc, "%lld", v);
341 }else if( x==7 ){
342 sprintf(zDesc, "real");
343 pData += 8;
344 }else if( x==8 ){
345 sprintf(zDesc, "0");
346 }else if( x==9 ){
347 sprintf(zDesc, "1");
348 }else if( x>=12 ){
mistachkin0461cc42014-07-18 21:16:37 +0000349 i64 size = (x-12)/2;
drhb2c062d2011-01-05 21:46:52 +0000350 if( (x&1)==0 ){
mistachkin35683972014-07-19 15:30:01 +0000351 sprintf(zDesc, "blob(%lld)", size);
drh100335b2011-01-05 21:20:52 +0000352 }else{
mistachkin35683972014-07-19 15:30:01 +0000353 sprintf(zDesc, "txt(%lld)", size);
drh100335b2011-01-05 21:20:52 +0000354 }
355 pData += size;
356 }
mistachkinef607032014-07-19 15:40:39 +0000357 j = (int)strlen(zDesc);
drh100335b2011-01-05 21:20:52 +0000358 zDesc += j;
359 nDesc += j;
360 }
361 return nDesc;
362}
drh5240aeb2011-01-06 01:26:38 +0000363
364/*
365** Compute the local payload size given the total payload size and
366** the page size.
367*/
mistachkin0461cc42014-07-18 21:16:37 +0000368static i64 localPayload(i64 nPayload, char cType){
369 i64 maxLocal;
370 i64 minLocal;
371 i64 surplus;
372 i64 nLocal;
drh5240aeb2011-01-06 01:26:38 +0000373 if( cType==13 ){
374 /* Table leaf */
dan871f6e32015-08-03 17:03:31 +0000375 maxLocal = g.pagesize-35;
376 minLocal = (g.pagesize-12)*32/255-23;
drh5240aeb2011-01-06 01:26:38 +0000377 }else{
dan871f6e32015-08-03 17:03:31 +0000378 maxLocal = (g.pagesize-12)*64/255-23;
379 minLocal = (g.pagesize-12)*32/255-23;
drh5240aeb2011-01-06 01:26:38 +0000380 }
381 if( nPayload>maxLocal ){
dan871f6e32015-08-03 17:03:31 +0000382 surplus = minLocal + (nPayload-minLocal)%(g.pagesize-4);
drh5240aeb2011-01-06 01:26:38 +0000383 if( surplus<=maxLocal ){
384 nLocal = surplus;
385 }else{
386 nLocal = minLocal;
387 }
388 }else{
389 nLocal = nPayload;
390 }
391 return nLocal;
392}
drh100335b2011-01-05 21:20:52 +0000393
394
395/*
drh7ecc1472010-04-26 16:47:12 +0000396** Create a description for a single cell.
drh5240aeb2011-01-06 01:26:38 +0000397**
398** The return value is the local cell size.
drh7ecc1472010-04-26 16:47:12 +0000399*/
mistachkin0461cc42014-07-18 21:16:37 +0000400static i64 describeCell(
drh100335b2011-01-05 21:20:52 +0000401 unsigned char cType, /* Page type */
402 unsigned char *a, /* Cell content */
403 int showCellContent, /* Show cell content if true */
404 char **pzDesc /* Store description here */
405){
drh7ecc1472010-04-26 16:47:12 +0000406 int i;
mistachkin0461cc42014-07-18 21:16:37 +0000407 i64 nDesc = 0;
drh7ecc1472010-04-26 16:47:12 +0000408 int n = 0;
409 int leftChild;
410 i64 nPayload;
411 i64 rowid;
mistachkin0461cc42014-07-18 21:16:37 +0000412 i64 nLocal;
drh100335b2011-01-05 21:20:52 +0000413 static char zDesc[1000];
drh7ecc1472010-04-26 16:47:12 +0000414 i = 0;
415 if( cType<=5 ){
416 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
417 a += 4;
418 n += 4;
drh100335b2011-01-05 21:20:52 +0000419 sprintf(zDesc, "lx: %d ", leftChild);
drh7ecc1472010-04-26 16:47:12 +0000420 nDesc = strlen(zDesc);
421 }
422 if( cType!=5 ){
423 i = decodeVarint(a, &nPayload);
424 a += i;
425 n += i;
drh100335b2011-01-05 21:20:52 +0000426 sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
drh7ecc1472010-04-26 16:47:12 +0000427 nDesc += strlen(&zDesc[nDesc]);
drh5240aeb2011-01-06 01:26:38 +0000428 nLocal = localPayload(nPayload, cType);
429 }else{
430 nPayload = nLocal = 0;
drh7ecc1472010-04-26 16:47:12 +0000431 }
432 if( cType==5 || cType==13 ){
433 i = decodeVarint(a, &rowid);
434 a += i;
435 n += i;
drh100335b2011-01-05 21:20:52 +0000436 sprintf(&zDesc[nDesc], "r: %lld ", rowid);
drh7ecc1472010-04-26 16:47:12 +0000437 nDesc += strlen(&zDesc[nDesc]);
438 }
drhb7787ee2011-01-06 15:51:18 +0000439 if( nLocal<nPayload ){
440 int ovfl;
441 unsigned char *b = &a[nLocal];
442 ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
443 sprintf(&zDesc[nDesc], "ov: %d ", ovfl);
444 nDesc += strlen(&zDesc[nDesc]);
445 n += 4;
446 }
drh100335b2011-01-05 21:20:52 +0000447 if( showCellContent && cType!=5 ){
drh5240aeb2011-01-06 01:26:38 +0000448 nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
drh100335b2011-01-05 21:20:52 +0000449 }
drh7ecc1472010-04-26 16:47:12 +0000450 *pzDesc = zDesc;
drh5240aeb2011-01-06 01:26:38 +0000451 return nLocal+n;
drh7ecc1472010-04-26 16:47:12 +0000452}
453
drh6a30cd52014-06-19 23:38:53 +0000454/* Print an offset followed by nByte bytes. Add extra white-space
455** at the end so that subsequent text is aligned.
456*/
457static void printBytes(
458 unsigned char *aData, /* Content being decoded */
459 unsigned char *aStart, /* Start of content to be printed */
460 int nByte /* Number of bytes to print */
461){
462 int j;
463 printf(" %03x: ", (int)(aStart-aData));
464 for(j=0; j<9; j++){
465 if( j>=nByte ){
466 printf(" ");
467 }else{
468 printf("%02x ", aStart[j]);
469 }
470 }
471}
472
473
474/*
475** Write a full decode on stdout for the cell at a[ofst].
476** Assume the page contains a header of size szPgHdr bytes.
477*/
478static void decodeCell(
479 unsigned char *a, /* Page content (without the page-1 header) */
480 unsigned pgno, /* Page number */
481 int iCell, /* Cell index */
482 int szPgHdr, /* Size of the page header. 0 or 100 */
483 int ofst /* Cell begins at a[ofst] */
484){
mistachkin44723ce2015-03-21 02:22:37 +0000485 int i, j = 0;
drh6a30cd52014-06-19 23:38:53 +0000486 int leftChild;
mistachkin0461cc42014-07-18 21:16:37 +0000487 i64 k;
drh6a30cd52014-06-19 23:38:53 +0000488 i64 nPayload;
489 i64 rowid;
490 i64 nHdr;
491 i64 iType;
mistachkin0461cc42014-07-18 21:16:37 +0000492 i64 nLocal;
drh6a30cd52014-06-19 23:38:53 +0000493 unsigned char *x = a + ofst;
494 unsigned char *end;
495 unsigned char cType = a[0];
drh419e0402014-06-20 01:32:42 +0000496 int nCol = 0;
497 int szCol[2000];
498 int ofstCol[2000];
499 int typeCol[2000];
drh6a30cd52014-06-19 23:38:53 +0000500
drh419e0402014-06-20 01:32:42 +0000501 printf("Cell[%d]:\n", iCell);
drh6a30cd52014-06-19 23:38:53 +0000502 if( cType<=5 ){
503 leftChild = ((x[0]*256 + x[1])*256 + x[2])*256 + x[3];
504 printBytes(a, x, 4);
505 printf("left child page:: %d\n", leftChild);
506 x += 4;
507 }
508 if( cType!=5 ){
509 i = decodeVarint(x, &nPayload);
510 printBytes(a, x, i);
511 nLocal = localPayload(nPayload, cType);
drh419e0402014-06-20 01:32:42 +0000512 if( nLocal==nPayload ){
mistachkin35683972014-07-19 15:30:01 +0000513 printf("payload-size: %lld\n", nPayload);
drh419e0402014-06-20 01:32:42 +0000514 }else{
mistachkin35683972014-07-19 15:30:01 +0000515 printf("payload-size: %lld (%lld local, %lld overflow)\n",
516 nPayload, nLocal, nPayload-nLocal);
drh419e0402014-06-20 01:32:42 +0000517 }
drh6a30cd52014-06-19 23:38:53 +0000518 x += i;
519 }else{
520 nPayload = nLocal = 0;
521 }
522 end = x + nLocal;
523 if( cType==5 || cType==13 ){
524 i = decodeVarint(x, &rowid);
525 printBytes(a, x, i);
526 printf("rowid: %lld\n", rowid);
527 x += i;
528 }
529 if( nLocal>0 ){
530 i = decodeVarint(x, &nHdr);
531 printBytes(a, x, i);
drh419e0402014-06-20 01:32:42 +0000532 printf("record-header-size: %d\n", (int)nHdr);
drh6a30cd52014-06-19 23:38:53 +0000533 j = i;
drh419e0402014-06-20 01:32:42 +0000534 nCol = 0;
535 k = nHdr;
drh6a30cd52014-06-19 23:38:53 +0000536 while( x+j<end && j<nHdr ){
537 const char *zTypeName;
538 int sz = 0;
539 char zNm[30];
540 i = decodeVarint(x+j, &iType);
541 printBytes(a, x+j, i);
drh419e0402014-06-20 01:32:42 +0000542 printf("typecode[%d]: %d - ", nCol, (int)iType);
drh6a30cd52014-06-19 23:38:53 +0000543 switch( iType ){
544 case 0: zTypeName = "NULL"; sz = 0; break;
545 case 1: zTypeName = "int8"; sz = 1; break;
546 case 2: zTypeName = "int16"; sz = 2; break;
547 case 3: zTypeName = "int24"; sz = 3; break;
548 case 4: zTypeName = "int32"; sz = 4; break;
549 case 5: zTypeName = "int48"; sz = 6; break;
550 case 6: zTypeName = "int64"; sz = 8; break;
551 case 7: zTypeName = "double"; sz = 8; break;
552 case 8: zTypeName = "zero"; sz = 0; break;
553 case 9: zTypeName = "one"; sz = 0; break;
554 case 10:
555 case 11: zTypeName = "error"; sz = 0; break;
556 default: {
557 sz = (int)(iType-12)/2;
558 sprintf(zNm, (iType&1)==0 ? "blob(%d)" : "text(%d)", sz);
559 zTypeName = zNm;
560 break;
561 }
562 }
563 printf("%s\n", zTypeName);
drh419e0402014-06-20 01:32:42 +0000564 szCol[nCol] = sz;
mistachkin0461cc42014-07-18 21:16:37 +0000565 ofstCol[nCol] = (int)k;
drh419e0402014-06-20 01:32:42 +0000566 typeCol[nCol] = (int)iType;
567 k += sz;
568 nCol++;
drh6a30cd52014-06-19 23:38:53 +0000569 j += i;
570 }
drh419e0402014-06-20 01:32:42 +0000571 for(i=0; i<nCol && ofstCol[i]+szCol[i]<=nLocal; i++){
572 int s = ofstCol[i];
573 i64 v;
574 const unsigned char *pData;
575 if( szCol[i]==0 ) continue;
576 printBytes(a, x+s, szCol[i]);
577 printf("data[%d]: ", i);
578 pData = x+s;
579 if( typeCol[i]<=7 ){
580 v = (signed char)pData[0];
581 for(k=1; k<szCol[i]; k++){
582 v = (v<<8) + pData[k];
583 }
584 if( typeCol[i]==7 ){
585 double r;
586 memcpy(&r, &v, sizeof(r));
587 printf("%#g\n", r);
588 }else{
589 printf("%lld\n", v);
590 }
591 }else{
drh56e67dd2014-06-20 13:55:06 +0000592 int ii, jj;
593 char zConst[32];
594 if( (typeCol[i]&1)==0 ){
595 zConst[0] = 'x';
596 zConst[1] = '\'';
597 for(ii=2, jj=0; jj<szCol[i] && ii<24; jj++, ii+=2){
598 sprintf(zConst+ii, "%02x", pData[jj]);
599 }
600 }else{
601 zConst[0] = '\'';
602 for(ii=1, jj=0; jj<szCol[i] && ii<24; jj++, ii++){
603 zConst[ii] = isprint(pData[jj]) ? pData[jj] : '.';
604 }
605 zConst[ii] = 0;
606 }
607 if( jj<szCol[i] ){
608 memcpy(zConst+ii, "...'", 5);
609 }else{
610 memcpy(zConst+ii, "'", 2);
611 }
612 printf("%s\n", zConst);
drh419e0402014-06-20 01:32:42 +0000613 }
614 j = ofstCol[i] + szCol[i];
615 }
drh6a30cd52014-06-19 23:38:53 +0000616 }
617 if( j<nLocal ){
618 printBytes(a, x+j, 0);
mistachkin35683972014-07-19 15:30:01 +0000619 printf("... %lld bytes of content ...\n", nLocal-j);
drh6a30cd52014-06-19 23:38:53 +0000620 }
621 if( nLocal<nPayload ){
622 printBytes(a, x+nLocal, 4);
drh419e0402014-06-20 01:32:42 +0000623 printf("overflow-page: %d\n", decodeInt32(x+nLocal));
drh6a30cd52014-06-19 23:38:53 +0000624 }
625}
626
627
drh7ecc1472010-04-26 16:47:12 +0000628/*
629** Decode a btree page
630*/
drh100335b2011-01-05 21:20:52 +0000631static void decode_btree_page(
632 unsigned char *a, /* Page content */
633 int pgno, /* Page number */
634 int hdrSize, /* Size of the page header. 0 or 100 */
635 char *zArgs /* Flags to control formatting */
636){
drh7ecc1472010-04-26 16:47:12 +0000637 const char *zType = "unknown";
638 int nCell;
drh5240aeb2011-01-06 01:26:38 +0000639 int i, j;
drh7ecc1472010-04-26 16:47:12 +0000640 int iCellPtr;
drh100335b2011-01-05 21:20:52 +0000641 int showCellContent = 0;
drh5240aeb2011-01-06 01:26:38 +0000642 int showMap = 0;
drh6a30cd52014-06-19 23:38:53 +0000643 int cellToDecode = -2;
drh5240aeb2011-01-06 01:26:38 +0000644 char *zMap = 0;
drh7ecc1472010-04-26 16:47:12 +0000645 switch( a[0] ){
646 case 2: zType = "index interior node"; break;
647 case 5: zType = "table interior node"; break;
648 case 10: zType = "index leaf"; break;
649 case 13: zType = "table leaf"; break;
650 }
drh100335b2011-01-05 21:20:52 +0000651 while( zArgs[0] ){
652 switch( zArgs[0] ){
653 case 'c': showCellContent = 1; break;
drh5240aeb2011-01-06 01:26:38 +0000654 case 'm': showMap = 1; break;
drh6a30cd52014-06-19 23:38:53 +0000655 case 'd': {
656 if( !isdigit(zArgs[1]) ){
657 cellToDecode = -1;
658 }else{
659 cellToDecode = 0;
660 while( isdigit(zArgs[1]) ){
661 zArgs++;
662 cellToDecode = cellToDecode*10 + zArgs[0] - '0';
663 }
664 }
665 break;
666 }
drh100335b2011-01-05 21:20:52 +0000667 }
668 zArgs++;
669 }
drh7ecc1472010-04-26 16:47:12 +0000670 nCell = a[3]*256 + a[4];
drh6a30cd52014-06-19 23:38:53 +0000671 iCellPtr = (a[0]==2 || a[0]==5) ? 12 : 8;
drh419e0402014-06-20 01:32:42 +0000672 if( cellToDecode>=nCell ){
drh6a30cd52014-06-19 23:38:53 +0000673 printf("Page %d has only %d cells\n", pgno, nCell);
674 return;
drh5240aeb2011-01-06 01:26:38 +0000675 }
drh419e0402014-06-20 01:32:42 +0000676 printf("Header on btree page %d:\n", pgno);
677 print_decode_line(a, 0, 1, zType);
678 print_decode_line(a, 1, 2, "Offset to first freeblock");
679 print_decode_line(a, 3, 2, "Number of cells on this page");
680 print_decode_line(a, 5, 2, "Offset to cell content area");
681 print_decode_line(a, 7, 1, "Fragmented byte count");
682 if( a[0]==2 || a[0]==5 ){
683 print_decode_line(a, 8, 4, "Right child");
684 }
685 if( cellToDecode==(-2) && nCell>0 ){
686 printf(" key: lx=left-child n=payload-size r=rowid\n");
687 }
drh5240aeb2011-01-06 01:26:38 +0000688 if( showMap ){
dan871f6e32015-08-03 17:03:31 +0000689 zMap = sqlite3_malloc(g.pagesize);
690 memset(zMap, '.', g.pagesize);
drh5240aeb2011-01-06 01:26:38 +0000691 memset(zMap, '1', hdrSize);
692 memset(&zMap[hdrSize], 'H', iCellPtr);
693 memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
694 }
drh7ecc1472010-04-26 16:47:12 +0000695 for(i=0; i<nCell; i++){
696 int cofst = iCellPtr + i*2;
697 char *zDesc;
mistachkin0461cc42014-07-18 21:16:37 +0000698 i64 n;
drh5240aeb2011-01-06 01:26:38 +0000699
drh7ecc1472010-04-26 16:47:12 +0000700 cofst = a[cofst]*256 + a[cofst+1];
drh5240aeb2011-01-06 01:26:38 +0000701 n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
702 if( showMap ){
703 char zBuf[30];
mistachkin0461cc42014-07-18 21:16:37 +0000704 memset(&zMap[cofst], '*', (size_t)n);
drh5240aeb2011-01-06 01:26:38 +0000705 zMap[cofst] = '[';
706 zMap[cofst+n-1] = ']';
707 sprintf(zBuf, "%d", i);
mistachkinef607032014-07-19 15:40:39 +0000708 j = (int)strlen(zBuf);
drh5240aeb2011-01-06 01:26:38 +0000709 if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
710 }
drh6a30cd52014-06-19 23:38:53 +0000711 if( cellToDecode==(-2) ){
712 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
713 }else if( cellToDecode==(-1) || cellToDecode==i ){
714 decodeCell(a, pgno, i, hdrSize, cofst-hdrSize);
715 }
drh7ecc1472010-04-26 16:47:12 +0000716 }
drh5240aeb2011-01-06 01:26:38 +0000717 if( showMap ){
drh419e0402014-06-20 01:32:42 +0000718 printf("Page map: (H=header P=cell-index 1=page-1-header .=free-space)\n");
dan871f6e32015-08-03 17:03:31 +0000719 for(i=0; i<g.pagesize; i+=64){
drh5240aeb2011-01-06 01:26:38 +0000720 printf(" %03x: %.64s\n", i, &zMap[i]);
721 }
dan871f6e32015-08-03 17:03:31 +0000722 sqlite3_free(zMap);
drh6a30cd52014-06-19 23:38:53 +0000723 }
drh7ecc1472010-04-26 16:47:12 +0000724}
725
drh7d105f82010-08-23 15:26:49 +0000726/*
727** Decode a freelist trunk page.
728*/
729static void decode_trunk_page(
730 int pgno, /* The page number */
drh7d105f82010-08-23 15:26:49 +0000731 int detail, /* Show leaf pages if true */
732 int recursive /* Follow the trunk change if true */
733){
drhdb718d82014-01-28 20:36:22 +0000734 int n, i;
drh7d105f82010-08-23 15:26:49 +0000735 unsigned char *a;
736 while( pgno>0 ){
dan871f6e32015-08-03 17:03:31 +0000737 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
drh7d105f82010-08-23 15:26:49 +0000738 printf("Decode of freelist trunk page %d:\n", pgno);
739 print_decode_line(a, 0, 4, "Next freelist trunk page");
740 print_decode_line(a, 4, 4, "Number of entries on this page");
741 if( detail ){
742 n = (int)decodeInt32(&a[4]);
743 for(i=0; i<n; i++){
744 unsigned int x = decodeInt32(&a[8+4*i]);
745 char zIdx[10];
746 sprintf(zIdx, "[%d]", i);
747 printf(" %5s %7u", zIdx, x);
748 if( i%5==4 ) printf("\n");
749 }
750 if( i%5!=0 ) printf("\n");
751 }
752 if( !recursive ){
753 pgno = 0;
754 }else{
755 pgno = (int)decodeInt32(&a[0]);
756 }
dan871f6e32015-08-03 17:03:31 +0000757 sqlite3_free(a);
drh7d105f82010-08-23 15:26:49 +0000758 }
759}
760
761/*
drh3aeea462012-04-03 14:59:50 +0000762** A short text comment on the use of each page.
763*/
764static char **zPageUse;
765
766/*
767** Add a comment on the use of a page.
768*/
769static void page_usage_msg(int pgno, const char *zFormat, ...){
770 va_list ap;
771 char *zMsg;
772
773 va_start(ap, zFormat);
774 zMsg = sqlite3_vmprintf(zFormat, ap);
775 va_end(ap);
dan871f6e32015-08-03 17:03:31 +0000776 if( pgno<=0 || pgno>g.mxPage ){
drhedf9a172013-03-05 01:46:26 +0000777 printf("ERROR: page %d out of range 1..%d: %s\n",
dan871f6e32015-08-03 17:03:31 +0000778 pgno, g.mxPage, zMsg);
drh3aeea462012-04-03 14:59:50 +0000779 sqlite3_free(zMsg);
780 return;
781 }
782 if( zPageUse[pgno]!=0 ){
783 printf("ERROR: page %d used multiple times:\n", pgno);
784 printf("ERROR: previous: %s\n", zPageUse[pgno]);
drh103a70f2013-02-19 20:25:16 +0000785 printf("ERROR: current: %s\n", zMsg);
drh3aeea462012-04-03 14:59:50 +0000786 sqlite3_free(zPageUse[pgno]);
787 }
788 zPageUse[pgno] = zMsg;
789}
790
791/*
792** Find overflow pages of a cell and describe their usage.
793*/
794static void page_usage_cell(
795 unsigned char cType, /* Page type */
796 unsigned char *a, /* Cell content */
797 int pgno, /* page containing the cell */
798 int cellno /* Index of the cell on the page */
799){
800 int i;
drh3aeea462012-04-03 14:59:50 +0000801 int n = 0;
802 i64 nPayload;
803 i64 rowid;
mistachkin0461cc42014-07-18 21:16:37 +0000804 i64 nLocal;
drh3aeea462012-04-03 14:59:50 +0000805 i = 0;
806 if( cType<=5 ){
807 a += 4;
808 n += 4;
809 }
810 if( cType!=5 ){
811 i = decodeVarint(a, &nPayload);
812 a += i;
813 n += i;
814 nLocal = localPayload(nPayload, cType);
815 }else{
816 nPayload = nLocal = 0;
817 }
818 if( cType==5 || cType==13 ){
819 i = decodeVarint(a, &rowid);
820 a += i;
821 n += i;
822 }
823 if( nLocal<nPayload ){
824 int ovfl = decodeInt32(a+nLocal);
825 int cnt = 0;
dan871f6e32015-08-03 17:03:31 +0000826 while( ovfl && (cnt++)<g.mxPage ){
drh3aeea462012-04-03 14:59:50 +0000827 page_usage_msg(ovfl, "overflow %d from cell %d of page %d",
828 cnt, cellno, pgno);
dan871f6e32015-08-03 17:03:31 +0000829 a = fileRead((ovfl-1)*g.pagesize, 4);
drh3aeea462012-04-03 14:59:50 +0000830 ovfl = decodeInt32(a);
dan871f6e32015-08-03 17:03:31 +0000831 sqlite3_free(a);
drh3aeea462012-04-03 14:59:50 +0000832 }
833 }
834}
835
836
837/*
838** Describe the usages of a b-tree page
839*/
840static void page_usage_btree(
841 int pgno, /* Page to describe */
842 int parent, /* Parent of this page. 0 for root pages */
843 int idx, /* Which child of the parent */
844 const char *zName /* Name of the table */
845){
846 unsigned char *a;
847 const char *zType = "corrupt node";
848 int nCell;
849 int i;
850 int hdr = pgno==1 ? 100 : 0;
851
dan871f6e32015-08-03 17:03:31 +0000852 if( pgno<=0 || pgno>g.mxPage ) return;
853 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
drh3aeea462012-04-03 14:59:50 +0000854 switch( a[hdr] ){
855 case 2: zType = "interior node of index"; break;
856 case 5: zType = "interior node of table"; break;
857 case 10: zType = "leaf of index"; break;
858 case 13: zType = "leaf of table"; break;
859 }
860 if( parent ){
861 page_usage_msg(pgno, "%s [%s], child %d of page %d",
862 zType, zName, idx, parent);
863 }else{
864 page_usage_msg(pgno, "root %s [%s]", zType, zName);
865 }
866 nCell = a[hdr+3]*256 + a[hdr+4];
867 if( a[hdr]==2 || a[hdr]==5 ){
868 int cellstart = hdr+12;
869 unsigned int child;
870 for(i=0; i<nCell; i++){
871 int ofst;
872
873 ofst = cellstart + i*2;
874 ofst = a[ofst]*256 + a[ofst+1];
875 child = decodeInt32(a+ofst);
876 page_usage_btree(child, pgno, i, zName);
877 }
878 child = decodeInt32(a+cellstart-4);
879 page_usage_btree(child, pgno, i, zName);
880 }
881 if( a[hdr]==2 || a[hdr]==10 || a[hdr]==13 ){
882 int cellstart = hdr + 8 + 4*(a[hdr]<=5);
883 for(i=0; i<nCell; i++){
884 int ofst;
885 ofst = cellstart + i*2;
886 ofst = a[ofst]*256 + a[ofst+1];
887 page_usage_cell(a[hdr], a+ofst, pgno, i);
888 }
889 }
dan871f6e32015-08-03 17:03:31 +0000890 sqlite3_free(a);
drh3aeea462012-04-03 14:59:50 +0000891}
892
893/*
894** Determine page usage by the freelist
895*/
896static void page_usage_freelist(int pgno){
897 unsigned char *a;
898 int cnt = 0;
899 int i;
900 int n;
901 int iNext;
902 int parent = 1;
903
dan871f6e32015-08-03 17:03:31 +0000904 while( pgno>0 && pgno<=g.mxPage && (cnt++)<g.mxPage ){
drh3aeea462012-04-03 14:59:50 +0000905 page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent);
dan871f6e32015-08-03 17:03:31 +0000906 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
drh3aeea462012-04-03 14:59:50 +0000907 iNext = decodeInt32(a);
908 n = decodeInt32(a+4);
909 for(i=0; i<n; i++){
910 int child = decodeInt32(a + (i*4+8));
911 page_usage_msg(child, "freelist leaf, child %d of trunk page %d",
912 i, pgno);
913 }
dan871f6e32015-08-03 17:03:31 +0000914 sqlite3_free(a);
drh3aeea462012-04-03 14:59:50 +0000915 parent = pgno;
916 pgno = iNext;
917 }
918}
919
920/*
drh344a97b2013-02-19 22:26:51 +0000921** Determine pages used as PTRMAP pages
922*/
923static void page_usage_ptrmap(unsigned char *a){
924 if( a[55] ){
dan871f6e32015-08-03 17:03:31 +0000925 int usable = g.pagesize - a[20];
drh344a97b2013-02-19 22:26:51 +0000926 int pgno = 2;
927 int perPage = usable/5;
dan871f6e32015-08-03 17:03:31 +0000928 while( pgno<=g.mxPage ){
drh344a97b2013-02-19 22:26:51 +0000929 page_usage_msg(pgno, "PTRMAP page covering %d..%d",
930 pgno+1, pgno+perPage);
931 pgno += perPage + 1;
932 }
933 }
934}
935
936/*
drh3aeea462012-04-03 14:59:50 +0000937** Try to figure out how every page in the database file is being used.
938*/
dan871f6e32015-08-03 17:03:31 +0000939static void page_usage_report(const char *zPrg, const char *zDbName){
drh00e637f2013-02-19 18:45:11 +0000940 int i, j;
drh3aeea462012-04-03 14:59:50 +0000941 int rc;
942 sqlite3 *db;
943 sqlite3_stmt *pStmt;
944 unsigned char *a;
drh00e637f2013-02-19 18:45:11 +0000945 char zQuery[200];
drh3aeea462012-04-03 14:59:50 +0000946
947 /* Avoid the pathological case */
dan871f6e32015-08-03 17:03:31 +0000948 if( g.mxPage<1 ){
drh3aeea462012-04-03 14:59:50 +0000949 printf("empty database\n");
950 return;
951 }
952
953 /* Open the database file */
dan871f6e32015-08-03 17:03:31 +0000954 db = openDatabase(zPrg, zDbName);
drh3aeea462012-04-03 14:59:50 +0000955
dan871f6e32015-08-03 17:03:31 +0000956 /* Set up global variables zPageUse[] and g.mxPage to record page
drh3aeea462012-04-03 14:59:50 +0000957 ** usages */
dan871f6e32015-08-03 17:03:31 +0000958 zPageUse = sqlite3_malloc( sizeof(zPageUse[0])*(g.mxPage+1) );
drh3aeea462012-04-03 14:59:50 +0000959 if( zPageUse==0 ) out_of_memory();
dan871f6e32015-08-03 17:03:31 +0000960 memset(zPageUse, 0, sizeof(zPageUse[0])*(g.mxPage+1));
drh3aeea462012-04-03 14:59:50 +0000961
962 /* Discover the usage of each page */
dan871f6e32015-08-03 17:03:31 +0000963 a = fileRead(0, 100);
drh3aeea462012-04-03 14:59:50 +0000964 page_usage_freelist(decodeInt32(a+32));
drh344a97b2013-02-19 22:26:51 +0000965 page_usage_ptrmap(a);
dan871f6e32015-08-03 17:03:31 +0000966 sqlite3_free(a);
drh3aeea462012-04-03 14:59:50 +0000967 page_usage_btree(1, 0, 0, "sqlite_master");
drh00e637f2013-02-19 18:45:11 +0000968 sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0);
969 for(j=0; j<2; j++){
970 sqlite3_snprintf(sizeof(zQuery), zQuery,
971 "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage"
972 " ORDER BY rowid %s", j?"DESC":"");
973 rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0);
974 if( rc==SQLITE_OK ){
975 while( sqlite3_step(pStmt)==SQLITE_ROW ){
976 int pgno = sqlite3_column_int(pStmt, 2);
drhdb718d82014-01-28 20:36:22 +0000977 page_usage_btree(pgno, 0, 0, (const char*)sqlite3_column_text(pStmt,1));
drh00e637f2013-02-19 18:45:11 +0000978 }
979 }else{
980 printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db));
drh3aeea462012-04-03 14:59:50 +0000981 }
drh00e637f2013-02-19 18:45:11 +0000982 rc = sqlite3_finalize(pStmt);
983 if( rc==SQLITE_OK ) break;
drh3aeea462012-04-03 14:59:50 +0000984 }
drh3aeea462012-04-03 14:59:50 +0000985 sqlite3_close(db);
986
987 /* Print the report and free memory used */
dan871f6e32015-08-03 17:03:31 +0000988 for(i=1; i<=g.mxPage; i++){
drh3aeea462012-04-03 14:59:50 +0000989 printf("%5d: %s\n", i, zPageUse[i] ? zPageUse[i] : "???");
990 sqlite3_free(zPageUse[i]);
991 }
992 sqlite3_free(zPageUse);
993 zPageUse = 0;
994}
995
996/*
drh344a97b2013-02-19 22:26:51 +0000997** Try to figure out how every page in the database file is being used.
998*/
999static void ptrmap_coverage_report(const char *zDbName){
mistachkin0461cc42014-07-18 21:16:37 +00001000 int pgno;
drh344a97b2013-02-19 22:26:51 +00001001 unsigned char *aHdr;
1002 unsigned char *a;
1003 int usable;
1004 int perPage;
mistachkin0461cc42014-07-18 21:16:37 +00001005 int i;
drh344a97b2013-02-19 22:26:51 +00001006
1007 /* Avoid the pathological case */
dan871f6e32015-08-03 17:03:31 +00001008 if( g.mxPage<1 ){
drh344a97b2013-02-19 22:26:51 +00001009 printf("empty database\n");
1010 return;
1011 }
1012
1013 /* Make sure PTRMAPs are used in this database */
dan871f6e32015-08-03 17:03:31 +00001014 aHdr = fileRead(0, 100);
drh344a97b2013-02-19 22:26:51 +00001015 if( aHdr[55]==0 ){
1016 printf("database does not use PTRMAP pages\n");
1017 return;
1018 }
dan871f6e32015-08-03 17:03:31 +00001019 usable = g.pagesize - aHdr[20];
drh344a97b2013-02-19 22:26:51 +00001020 perPage = usable/5;
dan871f6e32015-08-03 17:03:31 +00001021 sqlite3_free(aHdr);
drh344a97b2013-02-19 22:26:51 +00001022 printf("%5d: root of sqlite_master\n", 1);
dan871f6e32015-08-03 17:03:31 +00001023 for(pgno=2; pgno<=g.mxPage; pgno += perPage+1){
drh344a97b2013-02-19 22:26:51 +00001024 printf("%5d: PTRMAP page covering %d..%d\n", pgno,
1025 pgno+1, pgno+perPage);
dan871f6e32015-08-03 17:03:31 +00001026 a = fileRead((pgno-1)*g.pagesize, usable);
1027 for(i=0; i+5<=usable && pgno+1+i/5<=g.mxPage; i+=5){
drh344a97b2013-02-19 22:26:51 +00001028 const char *zType = "???";
1029 unsigned int iFrom = decodeInt32(&a[i+1]);
1030 switch( a[i] ){
1031 case 1: zType = "b-tree root page"; break;
1032 case 2: zType = "freelist page"; break;
1033 case 3: zType = "first page of overflow"; break;
1034 case 4: zType = "later page of overflow"; break;
1035 case 5: zType = "b-tree non-root page"; break;
1036 }
1037 printf("%5d: %s, parent=%u\n", pgno+1+i/5, zType, iFrom);
1038 }
dan871f6e32015-08-03 17:03:31 +00001039 sqlite3_free(a);
drh344a97b2013-02-19 22:26:51 +00001040 }
1041}
1042
1043/*
drh7d105f82010-08-23 15:26:49 +00001044** Print a usage comment
1045*/
1046static void usage(const char *argv0){
dan871f6e32015-08-03 17:03:31 +00001047 fprintf(stderr, "Usage %s ?--uri? FILENAME ?args...?\n\n", argv0);
drh7d105f82010-08-23 15:26:49 +00001048 fprintf(stderr,
dan871f6e32015-08-03 17:03:31 +00001049 "switches:\n"
dan8fb1bd22015-08-04 15:23:49 +00001050 " --raw Read db file directly, bypassing SQLite VFS\n"
drh7d105f82010-08-23 15:26:49 +00001051 "args:\n"
1052 " dbheader Show database header\n"
drh3aeea462012-04-03 14:59:50 +00001053 " pgidx Index of how each page is used\n"
drh344a97b2013-02-19 22:26:51 +00001054 " ptrmap Show all PTRMAP page content\n"
drh7d105f82010-08-23 15:26:49 +00001055 " NNN..MMM Show hex of pages NNN through MMM\n"
1056 " NNN..end Show hex of pages NNN through end of file\n"
1057 " NNNb Decode btree page NNN\n"
drh5240aeb2011-01-06 01:26:38 +00001058 " NNNbc Decode btree page NNN and show content\n"
1059 " NNNbm Decode btree page NNN and show a layout map\n"
drh6a30cd52014-06-19 23:38:53 +00001060 " NNNbdCCC Decode cell CCC on btree page NNN\n"
drh7d105f82010-08-23 15:26:49 +00001061 " NNNt Decode freelist trunk page NNN\n"
drh47fb0002011-04-13 16:52:41 +00001062 " NNNtd Show leaf freelist pages on the decode\n"
dan53d89cd2014-08-20 10:42:16 +00001063 " NNNtr Recursively decode freelist starting at NNN\n"
drh7d105f82010-08-23 15:26:49 +00001064 );
1065}
1066
drh0de8c112002-07-06 16:32:14 +00001067int main(int argc, char **argv){
dan871f6e32015-08-03 17:03:31 +00001068 sqlite3_int64 szFile;
1069 unsigned char *zPgSz;
1070 const char *zPrg = argv[0]; /* Name of this executable */
1071 char **azArg = argv;
1072 int nArg = argc;
1073
1074 /* Check for the "--uri" or "-uri" switch. */
1075 if( nArg>1 ){
dan8fb1bd22015-08-04 15:23:49 +00001076 if( sqlite3_stricmp("-raw", azArg[1])==0
1077 || sqlite3_stricmp("--raw", azArg[1])==0
dan871f6e32015-08-03 17:03:31 +00001078 ){
dan8fb1bd22015-08-04 15:23:49 +00001079 g.bRaw = 1;
dan871f6e32015-08-03 17:03:31 +00001080 azArg++;
1081 nArg--;
1082 }
1083 }
1084
1085 if( nArg<2 ){
1086 usage(zPrg);
drh0de8c112002-07-06 16:32:14 +00001087 exit(1);
1088 }
dan871f6e32015-08-03 17:03:31 +00001089
dan8fb1bd22015-08-04 15:23:49 +00001090 fileOpen(zPrg, azArg[1]);
dan871f6e32015-08-03 17:03:31 +00001091 szFile = fileGetsize();
1092
1093 zPgSz = fileRead(16, 2);
1094 g.pagesize = zPgSz[0]*256 + zPgSz[1]*65536;
1095 if( g.pagesize==0 ) g.pagesize = 1024;
1096 sqlite3_free(zPgSz);
1097
1098 printf("Pagesize: %d\n", g.pagesize);
1099 g.mxPage = (szFile+g.pagesize-1)/g.pagesize;
1100
1101 printf("Available pages: 1..%d\n", g.mxPage);
1102 if( nArg==2 ){
drh0de8c112002-07-06 16:32:14 +00001103 int i;
dan871f6e32015-08-03 17:03:31 +00001104 for(i=1; i<=g.mxPage; i++) print_page(i);
drh0de8c112002-07-06 16:32:14 +00001105 }else{
1106 int i;
dan871f6e32015-08-03 17:03:31 +00001107 for(i=2; i<nArg; i++){
drh0de8c112002-07-06 16:32:14 +00001108 int iStart, iEnd;
1109 char *zLeft;
dan871f6e32015-08-03 17:03:31 +00001110 if( strcmp(azArg[i], "dbheader")==0 ){
drh562cedb2010-04-26 15:44:07 +00001111 print_db_header();
1112 continue;
1113 }
dan871f6e32015-08-03 17:03:31 +00001114 if( strcmp(azArg[i], "pgidx")==0 ){
1115 page_usage_report(zPrg, azArg[1]);
drh3aeea462012-04-03 14:59:50 +00001116 continue;
1117 }
dan871f6e32015-08-03 17:03:31 +00001118 if( strcmp(azArg[i], "ptrmap")==0 ){
1119 ptrmap_coverage_report(azArg[1]);
drh344a97b2013-02-19 22:26:51 +00001120 continue;
1121 }
dan871f6e32015-08-03 17:03:31 +00001122 if( strcmp(azArg[i], "help")==0 ){
dan8fb1bd22015-08-04 15:23:49 +00001123 usage(zPrg);
drh344a97b2013-02-19 22:26:51 +00001124 continue;
1125 }
dan871f6e32015-08-03 17:03:31 +00001126 if( !isdigit(azArg[i][0]) ){
dan8fb1bd22015-08-04 15:23:49 +00001127 fprintf(stderr, "%s: unknown option: [%s]\n", zPrg, azArg[i]);
drh562cedb2010-04-26 15:44:07 +00001128 continue;
1129 }
dan871f6e32015-08-03 17:03:31 +00001130 iStart = strtol(azArg[i], &zLeft, 0);
drh0de8c112002-07-06 16:32:14 +00001131 if( zLeft && strcmp(zLeft,"..end")==0 ){
dan871f6e32015-08-03 17:03:31 +00001132 iEnd = g.mxPage;
drh0de8c112002-07-06 16:32:14 +00001133 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
1134 iEnd = strtol(&zLeft[2], 0, 0);
drh7ecc1472010-04-26 16:47:12 +00001135 }else if( zLeft && zLeft[0]=='b' ){
1136 int ofst, nByte, hdrSize;
1137 unsigned char *a;
1138 if( iStart==1 ){
1139 ofst = hdrSize = 100;
dan871f6e32015-08-03 17:03:31 +00001140 nByte = g.pagesize-100;
drh7ecc1472010-04-26 16:47:12 +00001141 }else{
1142 hdrSize = 0;
dan871f6e32015-08-03 17:03:31 +00001143 ofst = (iStart-1)*g.pagesize;
1144 nByte = g.pagesize;
drh7ecc1472010-04-26 16:47:12 +00001145 }
dan871f6e32015-08-03 17:03:31 +00001146 a = fileRead(ofst, nByte);
drh100335b2011-01-05 21:20:52 +00001147 decode_btree_page(a, iStart, hdrSize, &zLeft[1]);
dan871f6e32015-08-03 17:03:31 +00001148 sqlite3_free(a);
drh7ecc1472010-04-26 16:47:12 +00001149 continue;
drh7d105f82010-08-23 15:26:49 +00001150 }else if( zLeft && zLeft[0]=='t' ){
drh7d105f82010-08-23 15:26:49 +00001151 int detail = 0;
1152 int recursive = 0;
1153 int i;
1154 for(i=1; zLeft[i]; i++){
1155 if( zLeft[i]=='r' ) recursive = 1;
1156 if( zLeft[i]=='d' ) detail = 1;
1157 }
dan871f6e32015-08-03 17:03:31 +00001158 decode_trunk_page(iStart, detail, recursive);
drh7d105f82010-08-23 15:26:49 +00001159 continue;
drh0de8c112002-07-06 16:32:14 +00001160 }else{
1161 iEnd = iStart;
1162 }
dan871f6e32015-08-03 17:03:31 +00001163 if( iStart<1 || iEnd<iStart || iEnd>g.mxPage ){
drh0de8c112002-07-06 16:32:14 +00001164 fprintf(stderr,
1165 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
dan871f6e32015-08-03 17:03:31 +00001166 g.mxPage);
drh0de8c112002-07-06 16:32:14 +00001167 exit(1);
1168 }
1169 while( iStart<=iEnd ){
1170 print_page(iStart);
1171 iStart++;
1172 }
1173 }
1174 }
dan871f6e32015-08-03 17:03:31 +00001175 fileClose();
drhdb718d82014-01-28 20:36:22 +00001176 return 0;
drh0de8c112002-07-06 16:32:14 +00001177}