blob: fe4e9aca0128ecc646771e2e9ea6c9b5b9d30058 [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>
drhc56fac72015-10-29 13:48:15 +00006#define ISDIGIT(X) isdigit((unsigned char)(X))
7#define ISPRINT(X) isprint((unsigned char)(X))
drh0de8c112002-07-06 16:32:14 +00008#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
mistachkin026262b2012-10-13 09:31:20 +000011
12#if !defined(_MSC_VER)
drh0de8c112002-07-06 16:32:14 +000013#include <unistd.h>
mistachkin0461cc42014-07-18 21:16:37 +000014#else
15#include <io.h>
mistachkin026262b2012-10-13 09:31:20 +000016#endif
17
drh0de8c112002-07-06 16:32:14 +000018#include <stdlib.h>
drh562cedb2010-04-26 15:44:07 +000019#include <string.h>
dan871f6e32015-08-03 17:03:31 +000020#include <assert.h>
drh3aeea462012-04-03 14:59:50 +000021#include "sqlite3.h"
drh0de8c112002-07-06 16:32:14 +000022
23
dan871f6e32015-08-03 17:03:31 +000024static struct GlobalData {
25 int pagesize; /* Size of a database page */
26 int dbfd; /* File descriptor for reading the DB */
27 int mxPage; /* Last page number */
28 int perLine; /* HEX elements to print per line */
dan8fb1bd22015-08-04 15:23:49 +000029 int bRaw; /* True to access db file via OS APIs */
dan30c16ad2015-08-04 15:29:43 +000030 sqlite3_file *pFd; /* File descriptor for non-raw mode */
dan871f6e32015-08-03 17:03:31 +000031 sqlite3 *pDb; /* Database handle that owns pFd */
32} g = {1024, -1, 0, 16, 0, 0, 0};
33
drh0de8c112002-07-06 16:32:14 +000034
drh562cedb2010-04-26 15:44:07 +000035typedef long long int i64; /* Datatype for 64-bit integers */
36
37
38/*
39** Convert the var-int format into i64. Return the number of bytes
40** in the var-int. Write the var-int value into *pVal.
41*/
drh7ecc1472010-04-26 16:47:12 +000042static int decodeVarint(const unsigned char *z, i64 *pVal){
drh562cedb2010-04-26 15:44:07 +000043 i64 v = 0;
drh7ecc1472010-04-26 16:47:12 +000044 int i;
45 for(i=0; i<8; i++){
drh562cedb2010-04-26 15:44:07 +000046 v = (v<<7) + (z[i]&0x7f);
47 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
48 }
49 v = (v<<8) + (z[i]&0xff);
50 *pVal = v;
51 return 9;
52}
53
drh7d105f82010-08-23 15:26:49 +000054/*
55** Extract a big-endian 32-bit integer
56*/
57static unsigned int decodeInt32(const unsigned char *z){
58 return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
59}
60
drh562cedb2010-04-26 15:44:07 +000061/* Report an out-of-memory error and die.
62*/
drh0de8c112002-07-06 16:32:14 +000063static void out_of_memory(void){
64 fprintf(stderr,"Out of memory...\n");
65 exit(1);
66}
67
drh562cedb2010-04-26 15:44:07 +000068/*
dan871f6e32015-08-03 17:03:31 +000069** Open a database connection.
70*/
71static sqlite3 *openDatabase(const char *zPrg, const char *zName){
72 sqlite3 *db = 0;
73 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI;
74 int rc = sqlite3_open_v2(zName, &db, flags, 0);
75 if( rc!=SQLITE_OK ){
76 const char *zErr = sqlite3_errmsg(db);
77 fprintf(stderr, "%s: can't open %s (%s)\n", zPrg, zName, zErr);
78 sqlite3_close(db);
79 exit(1);
80 }
81 return db;
82}
83
84/**************************************************************************
85** Beginning of low-level file access functions.
86**
87** All low-level access to the database file read by this program is
88** performed using the following four functions:
89**
90** fileOpen() - open the db file
91** fileClose() - close the db file
92** fileRead() - read raw data from the db file
93** fileGetsize() - return the size of the db file in bytes
94*/
95
96/*
97** Open the database file.
98*/
99static void fileOpen(const char *zPrg, const char *zName){
100 assert( g.dbfd<0 );
dan8fb1bd22015-08-04 15:23:49 +0000101 if( g.bRaw==0 ){
dan871f6e32015-08-03 17:03:31 +0000102 int rc;
103 void *pArg = (void *)(&g.pFd);
104 g.pDb = openDatabase(zPrg, zName);
105 rc = sqlite3_file_control(g.pDb, "main", SQLITE_FCNTL_FILE_POINTER, pArg);
106 if( rc!=SQLITE_OK ){
107 fprintf(stderr,
108 "%s: failed to obtain fd for %s (SQLite too old?)\n", zPrg, zName
109 );
110 exit(1);
111 }
112 }else{
113 g.dbfd = open(zName, O_RDONLY);
114 if( g.dbfd<0 ){
115 fprintf(stderr,"%s: can't open %s\n", zPrg, zName);
116 exit(1);
117 }
118 }
119}
120
121/*
122** Close the database file opened by fileOpen()
123*/
124static void fileClose(){
dan8fb1bd22015-08-04 15:23:49 +0000125 if( g.bRaw==0 ){
dan871f6e32015-08-03 17:03:31 +0000126 sqlite3_close(g.pDb);
127 g.pDb = 0;
128 g.pFd = 0;
129 }else{
130 close(g.dbfd);
131 g.dbfd = -1;
132 }
133}
134
135/*
drh562cedb2010-04-26 15:44:07 +0000136** Read content from the file.
137**
dan871f6e32015-08-03 17:03:31 +0000138** Space to hold the content is obtained from sqlite3_malloc() and needs
139** to be freed by the caller.
drh562cedb2010-04-26 15:44:07 +0000140*/
dan871f6e32015-08-03 17:03:31 +0000141static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){
drh562cedb2010-04-26 15:44:07 +0000142 unsigned char *aData;
drh748c7352015-04-15 15:29:05 +0000143 int got;
dan871f6e32015-08-03 17:03:31 +0000144 aData = sqlite3_malloc(nByte+32);
drh562cedb2010-04-26 15:44:07 +0000145 if( aData==0 ) out_of_memory();
drhb7787ee2011-01-06 15:51:18 +0000146 memset(aData, 0, nByte+32);
dan8fb1bd22015-08-04 15:23:49 +0000147 if( g.bRaw==0 ){
dan871f6e32015-08-03 17:03:31 +0000148 int rc = g.pFd->pMethods->xRead(g.pFd, (void*)aData, nByte, ofst);
149 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
150 fprintf(stderr, "error in xRead() - %d\n", rc);
151 exit(1);
152 }
153 }else{
mistachkin77fac872016-04-12 20:05:06 +0000154 lseek(g.dbfd, (long)ofst, SEEK_SET);
dan871f6e32015-08-03 17:03:31 +0000155 got = read(g.dbfd, aData, nByte);
156 if( got>0 && got<nByte ) memset(aData+got, 0, nByte-got);
157 }
drh562cedb2010-04-26 15:44:07 +0000158 return aData;
159}
160
161/*
dan871f6e32015-08-03 17:03:31 +0000162** Return the size of the file in byte.
163*/
164static sqlite3_int64 fileGetsize(void){
165 sqlite3_int64 res = 0;
dan8fb1bd22015-08-04 15:23:49 +0000166 if( g.bRaw==0 ){
dan871f6e32015-08-03 17:03:31 +0000167 int rc = g.pFd->pMethods->xFileSize(g.pFd, &res);
168 if( rc!=SQLITE_OK ){
169 fprintf(stderr, "error in xFileSize() - %d\n", rc);
170 exit(1);
171 }
172 }else{
173 struct stat sbuf;
174 fstat(g.dbfd, &sbuf);
175 res = (sqlite3_int64)(sbuf.st_size);
176 }
177 return res;
178}
179
180/*
181** End of low-level file access functions.
182**************************************************************************/
183
184/*
drh562cedb2010-04-26 15:44:07 +0000185** Print a range of bytes as hex and as ascii.
186*/
187static unsigned char *print_byte_range(
188 int ofst, /* First byte in the range of bytes to print */
189 int nByte, /* Number of bytes to print */
190 int printOfst /* Add this amount to the index on the left column */
191){
drh0de8c112002-07-06 16:32:14 +0000192 unsigned char *aData;
193 int i, j;
drh562cedb2010-04-26 15:44:07 +0000194 const char *zOfstFmt;
195
196 if( ((printOfst+nByte)&~0xfff)==0 ){
197 zOfstFmt = " %03x: ";
198 }else if( ((printOfst+nByte)&~0xffff)==0 ){
199 zOfstFmt = " %04x: ";
200 }else if( ((printOfst+nByte)&~0xfffff)==0 ){
201 zOfstFmt = " %05x: ";
202 }else if( ((printOfst+nByte)&~0xffffff)==0 ){
203 zOfstFmt = " %06x: ";
204 }else{
205 zOfstFmt = " %08x: ";
206 }
207
dan871f6e32015-08-03 17:03:31 +0000208 aData = fileRead(ofst, nByte);
209 for(i=0; i<nByte; i += g.perLine){
drh562cedb2010-04-26 15:44:07 +0000210 fprintf(stdout, zOfstFmt, i+printOfst);
dan871f6e32015-08-03 17:03:31 +0000211 for(j=0; j<g.perLine; j++){
drh562cedb2010-04-26 15:44:07 +0000212 if( i+j>nByte ){
213 fprintf(stdout, " ");
214 }else{
215 fprintf(stdout,"%02x ", aData[i+j]);
216 }
drh0de8c112002-07-06 16:32:14 +0000217 }
dan871f6e32015-08-03 17:03:31 +0000218 for(j=0; j<g.perLine; j++){
drh562cedb2010-04-26 15:44:07 +0000219 if( i+j>nByte ){
220 fprintf(stdout, " ");
221 }else{
drhc56fac72015-10-29 13:48:15 +0000222 fprintf(stdout,"%c", ISPRINT(aData[i+j]) ? aData[i+j] : '.');
drh562cedb2010-04-26 15:44:07 +0000223 }
drh0de8c112002-07-06 16:32:14 +0000224 }
225 fprintf(stdout,"\n");
226 }
drh562cedb2010-04-26 15:44:07 +0000227 return aData;
228}
229
230/*
231** Print an entire page of content as hex
232*/
drhdb718d82014-01-28 20:36:22 +0000233static void print_page(int iPg){
drh562cedb2010-04-26 15:44:07 +0000234 int iStart;
235 unsigned char *aData;
dan871f6e32015-08-03 17:03:31 +0000236 iStart = (iPg-1)*g.pagesize;
drh562cedb2010-04-26 15:44:07 +0000237 fprintf(stdout, "Page %d: (offsets 0x%x..0x%x)\n",
dan871f6e32015-08-03 17:03:31 +0000238 iPg, iStart, iStart+g.pagesize-1);
239 aData = print_byte_range(iStart, g.pagesize, 0);
240 sqlite3_free(aData);
drh0de8c112002-07-06 16:32:14 +0000241}
242
drh6a30cd52014-06-19 23:38:53 +0000243
drh562cedb2010-04-26 15:44:07 +0000244/* Print a line of decode output showing a 4-byte integer.
245*/
drhdb718d82014-01-28 20:36:22 +0000246static void print_decode_line(
drh562cedb2010-04-26 15:44:07 +0000247 unsigned char *aData, /* Content being decoded */
248 int ofst, int nByte, /* Start and size of decode */
249 const char *zMsg /* Message to append */
250){
251 int i, j;
252 int val = aData[ofst];
253 char zBuf[100];
254 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
mistachkinef607032014-07-19 15:40:39 +0000255 i = (int)strlen(zBuf);
drh562cedb2010-04-26 15:44:07 +0000256 for(j=1; j<4; j++){
257 if( j>=nByte ){
258 sprintf(&zBuf[i], " ");
259 }else{
260 sprintf(&zBuf[i], " %02x", aData[ofst+j]);
261 val = val*256 + aData[ofst+j];
262 }
mistachkinef607032014-07-19 15:40:39 +0000263 i += (int)strlen(&zBuf[i]);
drh562cedb2010-04-26 15:44:07 +0000264 }
265 sprintf(&zBuf[i], " %9d", val);
drh7ecc1472010-04-26 16:47:12 +0000266 printf("%s %s\n", zBuf, zMsg);
drh562cedb2010-04-26 15:44:07 +0000267}
268
269/*
270** Decode the database header.
271*/
drh7ecc1472010-04-26 16:47:12 +0000272static void print_db_header(void){
drh562cedb2010-04-26 15:44:07 +0000273 unsigned char *aData;
274 aData = print_byte_range(0, 100, 0);
275 printf("Decoded:\n");
276 print_decode_line(aData, 16, 2, "Database page size");
277 print_decode_line(aData, 18, 1, "File format write version");
278 print_decode_line(aData, 19, 1, "File format read version");
279 print_decode_line(aData, 20, 1, "Reserved space at end of page");
280 print_decode_line(aData, 24, 4, "File change counter");
281 print_decode_line(aData, 28, 4, "Size of database in pages");
282 print_decode_line(aData, 32, 4, "Page number of first freelist page");
283 print_decode_line(aData, 36, 4, "Number of freelist pages");
284 print_decode_line(aData, 40, 4, "Schema cookie");
285 print_decode_line(aData, 44, 4, "Schema format version");
286 print_decode_line(aData, 48, 4, "Default page cache size");
287 print_decode_line(aData, 52, 4, "Largest auto-vac root page");
288 print_decode_line(aData, 56, 4, "Text encoding");
289 print_decode_line(aData, 60, 4, "User version");
290 print_decode_line(aData, 64, 4, "Incremental-vacuum mode");
drh4ee09b42013-05-01 19:49:27 +0000291 print_decode_line(aData, 68, 4, "Application ID");
drh562cedb2010-04-26 15:44:07 +0000292 print_decode_line(aData, 72, 4, "meta[8]");
293 print_decode_line(aData, 76, 4, "meta[9]");
294 print_decode_line(aData, 80, 4, "meta[10]");
295 print_decode_line(aData, 84, 4, "meta[11]");
296 print_decode_line(aData, 88, 4, "meta[12]");
drhb28e59b2010-06-17 02:13:39 +0000297 print_decode_line(aData, 92, 4, "Change counter for version number");
drhc6d2b4a2010-04-26 17:30:52 +0000298 print_decode_line(aData, 96, 4, "SQLite version number");
drh562cedb2010-04-26 15:44:07 +0000299}
300
drh7ecc1472010-04-26 16:47:12 +0000301/*
drh100335b2011-01-05 21:20:52 +0000302** Describe cell content.
303*/
mistachkin0461cc42014-07-18 21:16:37 +0000304static i64 describeContent(
drh5240aeb2011-01-06 01:26:38 +0000305 unsigned char *a, /* Cell content */
mistachkin0461cc42014-07-18 21:16:37 +0000306 i64 nLocal, /* Bytes in a[] */
drh5240aeb2011-01-06 01:26:38 +0000307 char *zDesc /* Write description here */
drh100335b2011-01-05 21:20:52 +0000308){
mistachkin0461cc42014-07-18 21:16:37 +0000309 i64 nDesc = 0;
310 int n, j;
311 i64 i, x, v;
drh100335b2011-01-05 21:20:52 +0000312 const unsigned char *pData;
drh5240aeb2011-01-06 01:26:38 +0000313 const unsigned char *pLimit;
drh100335b2011-01-05 21:20:52 +0000314 char sep = ' ';
315
drh5240aeb2011-01-06 01:26:38 +0000316 pLimit = &a[nLocal];
drh100335b2011-01-05 21:20:52 +0000317 n = decodeVarint(a, &x);
318 pData = &a[x];
319 a += n;
320 i = x - n;
drh5240aeb2011-01-06 01:26:38 +0000321 while( i>0 && pData<=pLimit ){
drh100335b2011-01-05 21:20:52 +0000322 n = decodeVarint(a, &x);
323 a += n;
324 i -= n;
drh5240aeb2011-01-06 01:26:38 +0000325 nLocal -= n;
drh100335b2011-01-05 21:20:52 +0000326 zDesc[0] = sep;
327 sep = ',';
328 nDesc++;
329 zDesc++;
330 if( x==0 ){
drh5240aeb2011-01-06 01:26:38 +0000331 sprintf(zDesc, "*"); /* NULL is a "*" */
drh100335b2011-01-05 21:20:52 +0000332 }else if( x>=1 && x<=6 ){
333 v = (signed char)pData[0];
334 pData++;
335 switch( x ){
336 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
337 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
338 case 4: v = (v<<8) + pData[0]; pData++;
339 case 3: v = (v<<8) + pData[0]; pData++;
340 case 2: v = (v<<8) + pData[0]; pData++;
341 }
342 sprintf(zDesc, "%lld", v);
343 }else if( x==7 ){
344 sprintf(zDesc, "real");
345 pData += 8;
346 }else if( x==8 ){
347 sprintf(zDesc, "0");
348 }else if( x==9 ){
349 sprintf(zDesc, "1");
350 }else if( x>=12 ){
mistachkin0461cc42014-07-18 21:16:37 +0000351 i64 size = (x-12)/2;
drhb2c062d2011-01-05 21:46:52 +0000352 if( (x&1)==0 ){
mistachkin35683972014-07-19 15:30:01 +0000353 sprintf(zDesc, "blob(%lld)", size);
drh100335b2011-01-05 21:20:52 +0000354 }else{
mistachkin35683972014-07-19 15:30:01 +0000355 sprintf(zDesc, "txt(%lld)", size);
drh100335b2011-01-05 21:20:52 +0000356 }
357 pData += size;
358 }
mistachkinef607032014-07-19 15:40:39 +0000359 j = (int)strlen(zDesc);
drh100335b2011-01-05 21:20:52 +0000360 zDesc += j;
361 nDesc += j;
362 }
363 return nDesc;
364}
drh5240aeb2011-01-06 01:26:38 +0000365
366/*
367** Compute the local payload size given the total payload size and
368** the page size.
369*/
mistachkin0461cc42014-07-18 21:16:37 +0000370static i64 localPayload(i64 nPayload, char cType){
371 i64 maxLocal;
372 i64 minLocal;
373 i64 surplus;
374 i64 nLocal;
drh5240aeb2011-01-06 01:26:38 +0000375 if( cType==13 ){
376 /* Table leaf */
dan871f6e32015-08-03 17:03:31 +0000377 maxLocal = g.pagesize-35;
378 minLocal = (g.pagesize-12)*32/255-23;
drh5240aeb2011-01-06 01:26:38 +0000379 }else{
dan871f6e32015-08-03 17:03:31 +0000380 maxLocal = (g.pagesize-12)*64/255-23;
381 minLocal = (g.pagesize-12)*32/255-23;
drh5240aeb2011-01-06 01:26:38 +0000382 }
383 if( nPayload>maxLocal ){
dan871f6e32015-08-03 17:03:31 +0000384 surplus = minLocal + (nPayload-minLocal)%(g.pagesize-4);
drh5240aeb2011-01-06 01:26:38 +0000385 if( surplus<=maxLocal ){
386 nLocal = surplus;
387 }else{
388 nLocal = minLocal;
389 }
390 }else{
391 nLocal = nPayload;
392 }
393 return nLocal;
394}
drh100335b2011-01-05 21:20:52 +0000395
396
397/*
drh7ecc1472010-04-26 16:47:12 +0000398** Create a description for a single cell.
drh5240aeb2011-01-06 01:26:38 +0000399**
400** The return value is the local cell size.
drh7ecc1472010-04-26 16:47:12 +0000401*/
mistachkin0461cc42014-07-18 21:16:37 +0000402static i64 describeCell(
drh100335b2011-01-05 21:20:52 +0000403 unsigned char cType, /* Page type */
404 unsigned char *a, /* Cell content */
405 int showCellContent, /* Show cell content if true */
406 char **pzDesc /* Store description here */
407){
drh7ecc1472010-04-26 16:47:12 +0000408 int i;
mistachkin0461cc42014-07-18 21:16:37 +0000409 i64 nDesc = 0;
drh7ecc1472010-04-26 16:47:12 +0000410 int n = 0;
411 int leftChild;
412 i64 nPayload;
413 i64 rowid;
mistachkin0461cc42014-07-18 21:16:37 +0000414 i64 nLocal;
drh100335b2011-01-05 21:20:52 +0000415 static char zDesc[1000];
drh7ecc1472010-04-26 16:47:12 +0000416 i = 0;
417 if( cType<=5 ){
418 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
419 a += 4;
420 n += 4;
drh100335b2011-01-05 21:20:52 +0000421 sprintf(zDesc, "lx: %d ", leftChild);
drh7ecc1472010-04-26 16:47:12 +0000422 nDesc = strlen(zDesc);
423 }
424 if( cType!=5 ){
425 i = decodeVarint(a, &nPayload);
426 a += i;
427 n += i;
drh100335b2011-01-05 21:20:52 +0000428 sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
drh7ecc1472010-04-26 16:47:12 +0000429 nDesc += strlen(&zDesc[nDesc]);
drh5240aeb2011-01-06 01:26:38 +0000430 nLocal = localPayload(nPayload, cType);
431 }else{
432 nPayload = nLocal = 0;
drh7ecc1472010-04-26 16:47:12 +0000433 }
434 if( cType==5 || cType==13 ){
435 i = decodeVarint(a, &rowid);
436 a += i;
437 n += i;
drh100335b2011-01-05 21:20:52 +0000438 sprintf(&zDesc[nDesc], "r: %lld ", rowid);
drh7ecc1472010-04-26 16:47:12 +0000439 nDesc += strlen(&zDesc[nDesc]);
440 }
drhb7787ee2011-01-06 15:51:18 +0000441 if( nLocal<nPayload ){
442 int ovfl;
443 unsigned char *b = &a[nLocal];
444 ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
445 sprintf(&zDesc[nDesc], "ov: %d ", ovfl);
446 nDesc += strlen(&zDesc[nDesc]);
447 n += 4;
448 }
drh100335b2011-01-05 21:20:52 +0000449 if( showCellContent && cType!=5 ){
drh5240aeb2011-01-06 01:26:38 +0000450 nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
drh100335b2011-01-05 21:20:52 +0000451 }
drh7ecc1472010-04-26 16:47:12 +0000452 *pzDesc = zDesc;
drh5240aeb2011-01-06 01:26:38 +0000453 return nLocal+n;
drh7ecc1472010-04-26 16:47:12 +0000454}
455
drh6a30cd52014-06-19 23:38:53 +0000456/* Print an offset followed by nByte bytes. Add extra white-space
457** at the end so that subsequent text is aligned.
458*/
459static void printBytes(
460 unsigned char *aData, /* Content being decoded */
461 unsigned char *aStart, /* Start of content to be printed */
462 int nByte /* Number of bytes to print */
463){
464 int j;
465 printf(" %03x: ", (int)(aStart-aData));
466 for(j=0; j<9; j++){
467 if( j>=nByte ){
468 printf(" ");
469 }else{
470 printf("%02x ", aStart[j]);
471 }
472 }
473}
474
475
476/*
477** Write a full decode on stdout for the cell at a[ofst].
478** Assume the page contains a header of size szPgHdr bytes.
479*/
480static void decodeCell(
481 unsigned char *a, /* Page content (without the page-1 header) */
482 unsigned pgno, /* Page number */
483 int iCell, /* Cell index */
484 int szPgHdr, /* Size of the page header. 0 or 100 */
485 int ofst /* Cell begins at a[ofst] */
486){
mistachkin44723ce2015-03-21 02:22:37 +0000487 int i, j = 0;
drh6a30cd52014-06-19 23:38:53 +0000488 int leftChild;
mistachkin0461cc42014-07-18 21:16:37 +0000489 i64 k;
drh6a30cd52014-06-19 23:38:53 +0000490 i64 nPayload;
491 i64 rowid;
492 i64 nHdr;
493 i64 iType;
mistachkin0461cc42014-07-18 21:16:37 +0000494 i64 nLocal;
drh6a30cd52014-06-19 23:38:53 +0000495 unsigned char *x = a + ofst;
496 unsigned char *end;
497 unsigned char cType = a[0];
drh419e0402014-06-20 01:32:42 +0000498 int nCol = 0;
499 int szCol[2000];
500 int ofstCol[2000];
501 int typeCol[2000];
drh6a30cd52014-06-19 23:38:53 +0000502
drh419e0402014-06-20 01:32:42 +0000503 printf("Cell[%d]:\n", iCell);
drh6a30cd52014-06-19 23:38:53 +0000504 if( cType<=5 ){
505 leftChild = ((x[0]*256 + x[1])*256 + x[2])*256 + x[3];
506 printBytes(a, x, 4);
507 printf("left child page:: %d\n", leftChild);
508 x += 4;
509 }
510 if( cType!=5 ){
511 i = decodeVarint(x, &nPayload);
512 printBytes(a, x, i);
513 nLocal = localPayload(nPayload, cType);
drh419e0402014-06-20 01:32:42 +0000514 if( nLocal==nPayload ){
mistachkin35683972014-07-19 15:30:01 +0000515 printf("payload-size: %lld\n", nPayload);
drh419e0402014-06-20 01:32:42 +0000516 }else{
mistachkin35683972014-07-19 15:30:01 +0000517 printf("payload-size: %lld (%lld local, %lld overflow)\n",
518 nPayload, nLocal, nPayload-nLocal);
drh419e0402014-06-20 01:32:42 +0000519 }
drh6a30cd52014-06-19 23:38:53 +0000520 x += i;
521 }else{
522 nPayload = nLocal = 0;
523 }
524 end = x + nLocal;
525 if( cType==5 || cType==13 ){
526 i = decodeVarint(x, &rowid);
527 printBytes(a, x, i);
528 printf("rowid: %lld\n", rowid);
529 x += i;
530 }
531 if( nLocal>0 ){
532 i = decodeVarint(x, &nHdr);
533 printBytes(a, x, i);
drh419e0402014-06-20 01:32:42 +0000534 printf("record-header-size: %d\n", (int)nHdr);
drh6a30cd52014-06-19 23:38:53 +0000535 j = i;
drh419e0402014-06-20 01:32:42 +0000536 nCol = 0;
537 k = nHdr;
drh970ea372017-03-16 13:14:03 +0000538 while( x+j<=end && j<nHdr ){
drh6a30cd52014-06-19 23:38:53 +0000539 const char *zTypeName;
540 int sz = 0;
541 char zNm[30];
542 i = decodeVarint(x+j, &iType);
543 printBytes(a, x+j, i);
drh419e0402014-06-20 01:32:42 +0000544 printf("typecode[%d]: %d - ", nCol, (int)iType);
drh6a30cd52014-06-19 23:38:53 +0000545 switch( iType ){
546 case 0: zTypeName = "NULL"; sz = 0; break;
547 case 1: zTypeName = "int8"; sz = 1; break;
548 case 2: zTypeName = "int16"; sz = 2; break;
549 case 3: zTypeName = "int24"; sz = 3; break;
550 case 4: zTypeName = "int32"; sz = 4; break;
551 case 5: zTypeName = "int48"; sz = 6; break;
552 case 6: zTypeName = "int64"; sz = 8; break;
553 case 7: zTypeName = "double"; sz = 8; break;
554 case 8: zTypeName = "zero"; sz = 0; break;
555 case 9: zTypeName = "one"; sz = 0; break;
556 case 10:
557 case 11: zTypeName = "error"; sz = 0; break;
558 default: {
559 sz = (int)(iType-12)/2;
560 sprintf(zNm, (iType&1)==0 ? "blob(%d)" : "text(%d)", sz);
561 zTypeName = zNm;
562 break;
563 }
564 }
565 printf("%s\n", zTypeName);
drh419e0402014-06-20 01:32:42 +0000566 szCol[nCol] = sz;
mistachkin0461cc42014-07-18 21:16:37 +0000567 ofstCol[nCol] = (int)k;
drh419e0402014-06-20 01:32:42 +0000568 typeCol[nCol] = (int)iType;
569 k += sz;
570 nCol++;
drh6a30cd52014-06-19 23:38:53 +0000571 j += i;
572 }
drh419e0402014-06-20 01:32:42 +0000573 for(i=0; i<nCol && ofstCol[i]+szCol[i]<=nLocal; i++){
574 int s = ofstCol[i];
575 i64 v;
576 const unsigned char *pData;
577 if( szCol[i]==0 ) continue;
578 printBytes(a, x+s, szCol[i]);
579 printf("data[%d]: ", i);
580 pData = x+s;
581 if( typeCol[i]<=7 ){
582 v = (signed char)pData[0];
583 for(k=1; k<szCol[i]; k++){
584 v = (v<<8) + pData[k];
585 }
586 if( typeCol[i]==7 ){
587 double r;
588 memcpy(&r, &v, sizeof(r));
589 printf("%#g\n", r);
590 }else{
591 printf("%lld\n", v);
592 }
593 }else{
drh56e67dd2014-06-20 13:55:06 +0000594 int ii, jj;
595 char zConst[32];
596 if( (typeCol[i]&1)==0 ){
597 zConst[0] = 'x';
598 zConst[1] = '\'';
599 for(ii=2, jj=0; jj<szCol[i] && ii<24; jj++, ii+=2){
600 sprintf(zConst+ii, "%02x", pData[jj]);
601 }
602 }else{
603 zConst[0] = '\'';
604 for(ii=1, jj=0; jj<szCol[i] && ii<24; jj++, ii++){
drhc56fac72015-10-29 13:48:15 +0000605 zConst[ii] = ISPRINT(pData[jj]) ? pData[jj] : '.';
drh56e67dd2014-06-20 13:55:06 +0000606 }
607 zConst[ii] = 0;
608 }
609 if( jj<szCol[i] ){
610 memcpy(zConst+ii, "...'", 5);
611 }else{
612 memcpy(zConst+ii, "'", 2);
613 }
614 printf("%s\n", zConst);
drh419e0402014-06-20 01:32:42 +0000615 }
616 j = ofstCol[i] + szCol[i];
617 }
drh6a30cd52014-06-19 23:38:53 +0000618 }
619 if( j<nLocal ){
620 printBytes(a, x+j, 0);
mistachkin35683972014-07-19 15:30:01 +0000621 printf("... %lld bytes of content ...\n", nLocal-j);
drh6a30cd52014-06-19 23:38:53 +0000622 }
623 if( nLocal<nPayload ){
624 printBytes(a, x+nLocal, 4);
drh419e0402014-06-20 01:32:42 +0000625 printf("overflow-page: %d\n", decodeInt32(x+nLocal));
drh6a30cd52014-06-19 23:38:53 +0000626 }
627}
628
629
drh7ecc1472010-04-26 16:47:12 +0000630/*
631** Decode a btree page
632*/
drh100335b2011-01-05 21:20:52 +0000633static void decode_btree_page(
634 unsigned char *a, /* Page content */
635 int pgno, /* Page number */
636 int hdrSize, /* Size of the page header. 0 or 100 */
637 char *zArgs /* Flags to control formatting */
638){
drh7ecc1472010-04-26 16:47:12 +0000639 const char *zType = "unknown";
640 int nCell;
drh5240aeb2011-01-06 01:26:38 +0000641 int i, j;
drh7ecc1472010-04-26 16:47:12 +0000642 int iCellPtr;
drh100335b2011-01-05 21:20:52 +0000643 int showCellContent = 0;
drh5240aeb2011-01-06 01:26:38 +0000644 int showMap = 0;
drh6a30cd52014-06-19 23:38:53 +0000645 int cellToDecode = -2;
drh5240aeb2011-01-06 01:26:38 +0000646 char *zMap = 0;
drh7ecc1472010-04-26 16:47:12 +0000647 switch( a[0] ){
648 case 2: zType = "index interior node"; break;
649 case 5: zType = "table interior node"; break;
650 case 10: zType = "index leaf"; break;
651 case 13: zType = "table leaf"; break;
652 }
drh100335b2011-01-05 21:20:52 +0000653 while( zArgs[0] ){
654 switch( zArgs[0] ){
655 case 'c': showCellContent = 1; break;
drh5240aeb2011-01-06 01:26:38 +0000656 case 'm': showMap = 1; break;
drh6a30cd52014-06-19 23:38:53 +0000657 case 'd': {
drhc56fac72015-10-29 13:48:15 +0000658 if( !ISDIGIT(zArgs[1]) ){
drh6a30cd52014-06-19 23:38:53 +0000659 cellToDecode = -1;
660 }else{
661 cellToDecode = 0;
drhc56fac72015-10-29 13:48:15 +0000662 while( ISDIGIT(zArgs[1]) ){
drh6a30cd52014-06-19 23:38:53 +0000663 zArgs++;
664 cellToDecode = cellToDecode*10 + zArgs[0] - '0';
665 }
666 }
667 break;
668 }
drh100335b2011-01-05 21:20:52 +0000669 }
670 zArgs++;
671 }
drh7ecc1472010-04-26 16:47:12 +0000672 nCell = a[3]*256 + a[4];
drh6a30cd52014-06-19 23:38:53 +0000673 iCellPtr = (a[0]==2 || a[0]==5) ? 12 : 8;
drh419e0402014-06-20 01:32:42 +0000674 if( cellToDecode>=nCell ){
drh6a30cd52014-06-19 23:38:53 +0000675 printf("Page %d has only %d cells\n", pgno, nCell);
676 return;
drh5240aeb2011-01-06 01:26:38 +0000677 }
drh419e0402014-06-20 01:32:42 +0000678 printf("Header on btree page %d:\n", pgno);
679 print_decode_line(a, 0, 1, zType);
680 print_decode_line(a, 1, 2, "Offset to first freeblock");
681 print_decode_line(a, 3, 2, "Number of cells on this page");
682 print_decode_line(a, 5, 2, "Offset to cell content area");
683 print_decode_line(a, 7, 1, "Fragmented byte count");
684 if( a[0]==2 || a[0]==5 ){
685 print_decode_line(a, 8, 4, "Right child");
686 }
687 if( cellToDecode==(-2) && nCell>0 ){
688 printf(" key: lx=left-child n=payload-size r=rowid\n");
689 }
drh5240aeb2011-01-06 01:26:38 +0000690 if( showMap ){
dan871f6e32015-08-03 17:03:31 +0000691 zMap = sqlite3_malloc(g.pagesize);
692 memset(zMap, '.', g.pagesize);
drh5240aeb2011-01-06 01:26:38 +0000693 memset(zMap, '1', hdrSize);
694 memset(&zMap[hdrSize], 'H', iCellPtr);
695 memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
696 }
drh7ecc1472010-04-26 16:47:12 +0000697 for(i=0; i<nCell; i++){
698 int cofst = iCellPtr + i*2;
699 char *zDesc;
mistachkin0461cc42014-07-18 21:16:37 +0000700 i64 n;
drh5240aeb2011-01-06 01:26:38 +0000701
drh7ecc1472010-04-26 16:47:12 +0000702 cofst = a[cofst]*256 + a[cofst+1];
drh5240aeb2011-01-06 01:26:38 +0000703 n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
704 if( showMap ){
705 char zBuf[30];
mistachkin0461cc42014-07-18 21:16:37 +0000706 memset(&zMap[cofst], '*', (size_t)n);
drh5240aeb2011-01-06 01:26:38 +0000707 zMap[cofst] = '[';
708 zMap[cofst+n-1] = ']';
709 sprintf(zBuf, "%d", i);
mistachkinef607032014-07-19 15:40:39 +0000710 j = (int)strlen(zBuf);
drh5240aeb2011-01-06 01:26:38 +0000711 if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
712 }
drh6a30cd52014-06-19 23:38:53 +0000713 if( cellToDecode==(-2) ){
714 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
715 }else if( cellToDecode==(-1) || cellToDecode==i ){
716 decodeCell(a, pgno, i, hdrSize, cofst-hdrSize);
717 }
drh7ecc1472010-04-26 16:47:12 +0000718 }
drh5240aeb2011-01-06 01:26:38 +0000719 if( showMap ){
drh419e0402014-06-20 01:32:42 +0000720 printf("Page map: (H=header P=cell-index 1=page-1-header .=free-space)\n");
dan871f6e32015-08-03 17:03:31 +0000721 for(i=0; i<g.pagesize; i+=64){
drh5240aeb2011-01-06 01:26:38 +0000722 printf(" %03x: %.64s\n", i, &zMap[i]);
723 }
dan871f6e32015-08-03 17:03:31 +0000724 sqlite3_free(zMap);
drh6a30cd52014-06-19 23:38:53 +0000725 }
drh7ecc1472010-04-26 16:47:12 +0000726}
727
drh7d105f82010-08-23 15:26:49 +0000728/*
729** Decode a freelist trunk page.
730*/
731static void decode_trunk_page(
732 int pgno, /* The page number */
drh7d105f82010-08-23 15:26:49 +0000733 int detail, /* Show leaf pages if true */
734 int recursive /* Follow the trunk change if true */
735){
drhdb718d82014-01-28 20:36:22 +0000736 int n, i;
drh7d105f82010-08-23 15:26:49 +0000737 unsigned char *a;
738 while( pgno>0 ){
dan871f6e32015-08-03 17:03:31 +0000739 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
drh7d105f82010-08-23 15:26:49 +0000740 printf("Decode of freelist trunk page %d:\n", pgno);
741 print_decode_line(a, 0, 4, "Next freelist trunk page");
742 print_decode_line(a, 4, 4, "Number of entries on this page");
743 if( detail ){
744 n = (int)decodeInt32(&a[4]);
745 for(i=0; i<n; i++){
746 unsigned int x = decodeInt32(&a[8+4*i]);
747 char zIdx[10];
748 sprintf(zIdx, "[%d]", i);
749 printf(" %5s %7u", zIdx, x);
750 if( i%5==4 ) printf("\n");
751 }
752 if( i%5!=0 ) printf("\n");
753 }
754 if( !recursive ){
755 pgno = 0;
756 }else{
757 pgno = (int)decodeInt32(&a[0]);
758 }
dan871f6e32015-08-03 17:03:31 +0000759 sqlite3_free(a);
drh7d105f82010-08-23 15:26:49 +0000760 }
761}
762
763/*
drh3aeea462012-04-03 14:59:50 +0000764** A short text comment on the use of each page.
765*/
766static char **zPageUse;
767
768/*
769** Add a comment on the use of a page.
770*/
771static void page_usage_msg(int pgno, const char *zFormat, ...){
772 va_list ap;
773 char *zMsg;
774
775 va_start(ap, zFormat);
776 zMsg = sqlite3_vmprintf(zFormat, ap);
777 va_end(ap);
dan871f6e32015-08-03 17:03:31 +0000778 if( pgno<=0 || pgno>g.mxPage ){
drhedf9a172013-03-05 01:46:26 +0000779 printf("ERROR: page %d out of range 1..%d: %s\n",
dan871f6e32015-08-03 17:03:31 +0000780 pgno, g.mxPage, zMsg);
drh3aeea462012-04-03 14:59:50 +0000781 sqlite3_free(zMsg);
782 return;
783 }
784 if( zPageUse[pgno]!=0 ){
785 printf("ERROR: page %d used multiple times:\n", pgno);
786 printf("ERROR: previous: %s\n", zPageUse[pgno]);
drh103a70f2013-02-19 20:25:16 +0000787 printf("ERROR: current: %s\n", zMsg);
drh3aeea462012-04-03 14:59:50 +0000788 sqlite3_free(zPageUse[pgno]);
789 }
790 zPageUse[pgno] = zMsg;
791}
792
793/*
794** Find overflow pages of a cell and describe their usage.
795*/
796static void page_usage_cell(
797 unsigned char cType, /* Page type */
798 unsigned char *a, /* Cell content */
799 int pgno, /* page containing the cell */
800 int cellno /* Index of the cell on the page */
801){
802 int i;
drh3aeea462012-04-03 14:59:50 +0000803 int n = 0;
804 i64 nPayload;
805 i64 rowid;
mistachkin0461cc42014-07-18 21:16:37 +0000806 i64 nLocal;
drh3aeea462012-04-03 14:59:50 +0000807 i = 0;
808 if( cType<=5 ){
809 a += 4;
810 n += 4;
811 }
812 if( cType!=5 ){
813 i = decodeVarint(a, &nPayload);
814 a += i;
815 n += i;
816 nLocal = localPayload(nPayload, cType);
817 }else{
818 nPayload = nLocal = 0;
819 }
820 if( cType==5 || cType==13 ){
821 i = decodeVarint(a, &rowid);
822 a += i;
823 n += i;
824 }
825 if( nLocal<nPayload ){
826 int ovfl = decodeInt32(a+nLocal);
827 int cnt = 0;
dan871f6e32015-08-03 17:03:31 +0000828 while( ovfl && (cnt++)<g.mxPage ){
drh3aeea462012-04-03 14:59:50 +0000829 page_usage_msg(ovfl, "overflow %d from cell %d of page %d",
830 cnt, cellno, pgno);
drhaaad6962019-03-05 23:49:17 +0000831 a = fileRead((ovfl-1)*(sqlite3_int64)g.pagesize, 4);
drh3aeea462012-04-03 14:59:50 +0000832 ovfl = decodeInt32(a);
dan871f6e32015-08-03 17:03:31 +0000833 sqlite3_free(a);
drh3aeea462012-04-03 14:59:50 +0000834 }
835 }
836}
837
drh8083ef02019-04-17 12:29:45 +0000838/*
839** True if the memory is all zeros
840*/
841static int allZero(unsigned char *a, int n){
842 while( n && (a++)[0]==0 ){ n--; }
843 return n==0;
844}
845
drh3aeea462012-04-03 14:59:50 +0000846
847/*
drh8083ef02019-04-17 12:29:45 +0000848** Describe the usages of a b-tree page.
849**
850** If parent==0, then this is the root of a btree. If parent<0 then
851** this is an orphan page.
drh3aeea462012-04-03 14:59:50 +0000852*/
853static void page_usage_btree(
854 int pgno, /* Page to describe */
855 int parent, /* Parent of this page. 0 for root pages */
856 int idx, /* Which child of the parent */
857 const char *zName /* Name of the table */
858){
859 unsigned char *a;
860 const char *zType = "corrupt node";
861 int nCell;
862 int i;
863 int hdr = pgno==1 ? 100 : 0;
drh25f933a2019-04-17 13:23:28 +0000864 char zEntry[30];
drh3aeea462012-04-03 14:59:50 +0000865
dan871f6e32015-08-03 17:03:31 +0000866 if( pgno<=0 || pgno>g.mxPage ) return;
867 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
drh3aeea462012-04-03 14:59:50 +0000868 switch( a[hdr] ){
drh8083ef02019-04-17 12:29:45 +0000869 case 0: {
870 if( allZero(a, g.pagesize) ){
871 zType = "zeroed page";
872 }else if( parent<0 ){
873 return;
874 }else{
875 zType = "corrupt node";
876 }
877 break;
878 }
drh3aeea462012-04-03 14:59:50 +0000879 case 2: zType = "interior node of index"; break;
880 case 5: zType = "interior node of table"; break;
881 case 10: zType = "leaf of index"; break;
882 case 13: zType = "leaf of table"; break;
drh8083ef02019-04-17 12:29:45 +0000883 default: {
884 if( parent<0 ) return;
885 zType = "corrupt node";
886 }
drh3aeea462012-04-03 14:59:50 +0000887 }
drh3aeea462012-04-03 14:59:50 +0000888 nCell = a[hdr+3]*256 + a[hdr+4];
drh25f933a2019-04-17 13:23:28 +0000889 if( nCell==1 ){
890 sqlite3_snprintf(sizeof(zEntry),zEntry,"1 row");
891 }else{
892 sqlite3_snprintf(sizeof(zEntry),zEntry,"%d rows", nCell);
893 }
894 if( parent>0 ){
895 page_usage_msg(pgno, "%s [%s], child %d of page %d, %s",
896 zType, zName, idx, parent, zEntry);
897 }else if( parent==0 ){
898 page_usage_msg(pgno, "root %s [%s], %s", zType, zName, zEntry);
899 }else{
900 page_usage_msg(pgno, "orphaned %s, %s", zType, zEntry);
901 }
drh3aeea462012-04-03 14:59:50 +0000902 if( a[hdr]==2 || a[hdr]==5 ){
903 int cellstart = hdr+12;
904 unsigned int child;
905 for(i=0; i<nCell; i++){
906 int ofst;
907
908 ofst = cellstart + i*2;
909 ofst = a[ofst]*256 + a[ofst+1];
910 child = decodeInt32(a+ofst);
911 page_usage_btree(child, pgno, i, zName);
912 }
913 child = decodeInt32(a+cellstart-4);
914 page_usage_btree(child, pgno, i, zName);
915 }
916 if( a[hdr]==2 || a[hdr]==10 || a[hdr]==13 ){
917 int cellstart = hdr + 8 + 4*(a[hdr]<=5);
918 for(i=0; i<nCell; i++){
919 int ofst;
920 ofst = cellstart + i*2;
921 ofst = a[ofst]*256 + a[ofst+1];
922 page_usage_cell(a[hdr], a+ofst, pgno, i);
923 }
924 }
dan871f6e32015-08-03 17:03:31 +0000925 sqlite3_free(a);
drh3aeea462012-04-03 14:59:50 +0000926}
927
928/*
929** Determine page usage by the freelist
930*/
931static void page_usage_freelist(int pgno){
932 unsigned char *a;
933 int cnt = 0;
934 int i;
935 int n;
936 int iNext;
937 int parent = 1;
938
dan871f6e32015-08-03 17:03:31 +0000939 while( pgno>0 && pgno<=g.mxPage && (cnt++)<g.mxPage ){
drh3aeea462012-04-03 14:59:50 +0000940 page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent);
dan871f6e32015-08-03 17:03:31 +0000941 a = fileRead((pgno-1)*g.pagesize, g.pagesize);
drh3aeea462012-04-03 14:59:50 +0000942 iNext = decodeInt32(a);
943 n = decodeInt32(a+4);
944 for(i=0; i<n; i++){
945 int child = decodeInt32(a + (i*4+8));
946 page_usage_msg(child, "freelist leaf, child %d of trunk page %d",
947 i, pgno);
948 }
dan871f6e32015-08-03 17:03:31 +0000949 sqlite3_free(a);
drh3aeea462012-04-03 14:59:50 +0000950 parent = pgno;
951 pgno = iNext;
952 }
953}
954
955/*
drh344a97b2013-02-19 22:26:51 +0000956** Determine pages used as PTRMAP pages
957*/
958static void page_usage_ptrmap(unsigned char *a){
drhc1f73e22020-01-14 13:24:14 +0000959 if( decodeInt32(a+52) ){
dan871f6e32015-08-03 17:03:31 +0000960 int usable = g.pagesize - a[20];
drh344a97b2013-02-19 22:26:51 +0000961 int pgno = 2;
962 int perPage = usable/5;
dan871f6e32015-08-03 17:03:31 +0000963 while( pgno<=g.mxPage ){
drh344a97b2013-02-19 22:26:51 +0000964 page_usage_msg(pgno, "PTRMAP page covering %d..%d",
965 pgno+1, pgno+perPage);
966 pgno += perPage + 1;
967 }
968 }
969}
970
971/*
drh3aeea462012-04-03 14:59:50 +0000972** Try to figure out how every page in the database file is being used.
973*/
dan871f6e32015-08-03 17:03:31 +0000974static void page_usage_report(const char *zPrg, const char *zDbName){
drh00e637f2013-02-19 18:45:11 +0000975 int i, j;
drh3aeea462012-04-03 14:59:50 +0000976 int rc;
977 sqlite3 *db;
978 sqlite3_stmt *pStmt;
979 unsigned char *a;
drh00e637f2013-02-19 18:45:11 +0000980 char zQuery[200];
drh3aeea462012-04-03 14:59:50 +0000981
982 /* Avoid the pathological case */
dan871f6e32015-08-03 17:03:31 +0000983 if( g.mxPage<1 ){
drh3aeea462012-04-03 14:59:50 +0000984 printf("empty database\n");
985 return;
986 }
987
988 /* Open the database file */
dan871f6e32015-08-03 17:03:31 +0000989 db = openDatabase(zPrg, zDbName);
drh3aeea462012-04-03 14:59:50 +0000990
dan871f6e32015-08-03 17:03:31 +0000991 /* Set up global variables zPageUse[] and g.mxPage to record page
drh3aeea462012-04-03 14:59:50 +0000992 ** usages */
dan871f6e32015-08-03 17:03:31 +0000993 zPageUse = sqlite3_malloc( sizeof(zPageUse[0])*(g.mxPage+1) );
drh3aeea462012-04-03 14:59:50 +0000994 if( zPageUse==0 ) out_of_memory();
dan871f6e32015-08-03 17:03:31 +0000995 memset(zPageUse, 0, sizeof(zPageUse[0])*(g.mxPage+1));
drh3aeea462012-04-03 14:59:50 +0000996
997 /* Discover the usage of each page */
dan871f6e32015-08-03 17:03:31 +0000998 a = fileRead(0, 100);
drh3aeea462012-04-03 14:59:50 +0000999 page_usage_freelist(decodeInt32(a+32));
drh344a97b2013-02-19 22:26:51 +00001000 page_usage_ptrmap(a);
dan871f6e32015-08-03 17:03:31 +00001001 sqlite3_free(a);
drh3aeea462012-04-03 14:59:50 +00001002 page_usage_btree(1, 0, 0, "sqlite_master");
drh00e637f2013-02-19 18:45:11 +00001003 sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0);
1004 for(j=0; j<2; j++){
1005 sqlite3_snprintf(sizeof(zQuery), zQuery,
1006 "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage"
1007 " ORDER BY rowid %s", j?"DESC":"");
1008 rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0);
1009 if( rc==SQLITE_OK ){
1010 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1011 int pgno = sqlite3_column_int(pStmt, 2);
drhdb718d82014-01-28 20:36:22 +00001012 page_usage_btree(pgno, 0, 0, (const char*)sqlite3_column_text(pStmt,1));
drh00e637f2013-02-19 18:45:11 +00001013 }
1014 }else{
1015 printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db));
drh3aeea462012-04-03 14:59:50 +00001016 }
drh00e637f2013-02-19 18:45:11 +00001017 rc = sqlite3_finalize(pStmt);
1018 if( rc==SQLITE_OK ) break;
drh3aeea462012-04-03 14:59:50 +00001019 }
drh3aeea462012-04-03 14:59:50 +00001020 sqlite3_close(db);
1021
1022 /* Print the report and free memory used */
dan871f6e32015-08-03 17:03:31 +00001023 for(i=1; i<=g.mxPage; i++){
drh8083ef02019-04-17 12:29:45 +00001024 if( zPageUse[i]==0 ) page_usage_btree(i, -1, 0, 0);
drh3aeea462012-04-03 14:59:50 +00001025 printf("%5d: %s\n", i, zPageUse[i] ? zPageUse[i] : "???");
1026 sqlite3_free(zPageUse[i]);
1027 }
1028 sqlite3_free(zPageUse);
1029 zPageUse = 0;
1030}
1031
1032/*
drh344a97b2013-02-19 22:26:51 +00001033** Try to figure out how every page in the database file is being used.
1034*/
1035static void ptrmap_coverage_report(const char *zDbName){
mistachkin0461cc42014-07-18 21:16:37 +00001036 int pgno;
drh344a97b2013-02-19 22:26:51 +00001037 unsigned char *aHdr;
1038 unsigned char *a;
1039 int usable;
1040 int perPage;
mistachkin0461cc42014-07-18 21:16:37 +00001041 int i;
drh344a97b2013-02-19 22:26:51 +00001042
1043 /* Avoid the pathological case */
dan871f6e32015-08-03 17:03:31 +00001044 if( g.mxPage<1 ){
drh344a97b2013-02-19 22:26:51 +00001045 printf("empty database\n");
1046 return;
1047 }
1048
1049 /* Make sure PTRMAPs are used in this database */
dan871f6e32015-08-03 17:03:31 +00001050 aHdr = fileRead(0, 100);
drh344a97b2013-02-19 22:26:51 +00001051 if( aHdr[55]==0 ){
1052 printf("database does not use PTRMAP pages\n");
1053 return;
1054 }
dan871f6e32015-08-03 17:03:31 +00001055 usable = g.pagesize - aHdr[20];
drh344a97b2013-02-19 22:26:51 +00001056 perPage = usable/5;
dan871f6e32015-08-03 17:03:31 +00001057 sqlite3_free(aHdr);
drh344a97b2013-02-19 22:26:51 +00001058 printf("%5d: root of sqlite_master\n", 1);
dan871f6e32015-08-03 17:03:31 +00001059 for(pgno=2; pgno<=g.mxPage; pgno += perPage+1){
drh344a97b2013-02-19 22:26:51 +00001060 printf("%5d: PTRMAP page covering %d..%d\n", pgno,
1061 pgno+1, pgno+perPage);
dan871f6e32015-08-03 17:03:31 +00001062 a = fileRead((pgno-1)*g.pagesize, usable);
1063 for(i=0; i+5<=usable && pgno+1+i/5<=g.mxPage; i+=5){
drh344a97b2013-02-19 22:26:51 +00001064 const char *zType = "???";
1065 unsigned int iFrom = decodeInt32(&a[i+1]);
1066 switch( a[i] ){
1067 case 1: zType = "b-tree root page"; break;
1068 case 2: zType = "freelist page"; break;
1069 case 3: zType = "first page of overflow"; break;
1070 case 4: zType = "later page of overflow"; break;
1071 case 5: zType = "b-tree non-root page"; break;
1072 }
1073 printf("%5d: %s, parent=%u\n", pgno+1+i/5, zType, iFrom);
1074 }
dan871f6e32015-08-03 17:03:31 +00001075 sqlite3_free(a);
drh344a97b2013-02-19 22:26:51 +00001076 }
1077}
1078
1079/*
drh7d105f82010-08-23 15:26:49 +00001080** Print a usage comment
1081*/
1082static void usage(const char *argv0){
dan871f6e32015-08-03 17:03:31 +00001083 fprintf(stderr, "Usage %s ?--uri? FILENAME ?args...?\n\n", argv0);
drh7d105f82010-08-23 15:26:49 +00001084 fprintf(stderr,
dan871f6e32015-08-03 17:03:31 +00001085 "switches:\n"
dan8fb1bd22015-08-04 15:23:49 +00001086 " --raw Read db file directly, bypassing SQLite VFS\n"
drh7d105f82010-08-23 15:26:49 +00001087 "args:\n"
1088 " dbheader Show database header\n"
drh3aeea462012-04-03 14:59:50 +00001089 " pgidx Index of how each page is used\n"
drh344a97b2013-02-19 22:26:51 +00001090 " ptrmap Show all PTRMAP page content\n"
drh7d105f82010-08-23 15:26:49 +00001091 " NNN..MMM Show hex of pages NNN through MMM\n"
1092 " NNN..end Show hex of pages NNN through end of file\n"
1093 " NNNb Decode btree page NNN\n"
drh5240aeb2011-01-06 01:26:38 +00001094 " NNNbc Decode btree page NNN and show content\n"
1095 " NNNbm Decode btree page NNN and show a layout map\n"
drh6a30cd52014-06-19 23:38:53 +00001096 " NNNbdCCC Decode cell CCC on btree page NNN\n"
drh7d105f82010-08-23 15:26:49 +00001097 " NNNt Decode freelist trunk page NNN\n"
drh47fb0002011-04-13 16:52:41 +00001098 " NNNtd Show leaf freelist pages on the decode\n"
dan53d89cd2014-08-20 10:42:16 +00001099 " NNNtr Recursively decode freelist starting at NNN\n"
drh7d105f82010-08-23 15:26:49 +00001100 );
1101}
1102
drh0de8c112002-07-06 16:32:14 +00001103int main(int argc, char **argv){
dan871f6e32015-08-03 17:03:31 +00001104 sqlite3_int64 szFile;
1105 unsigned char *zPgSz;
1106 const char *zPrg = argv[0]; /* Name of this executable */
1107 char **azArg = argv;
1108 int nArg = argc;
1109
1110 /* Check for the "--uri" or "-uri" switch. */
1111 if( nArg>1 ){
dan8fb1bd22015-08-04 15:23:49 +00001112 if( sqlite3_stricmp("-raw", azArg[1])==0
1113 || sqlite3_stricmp("--raw", azArg[1])==0
dan871f6e32015-08-03 17:03:31 +00001114 ){
dan8fb1bd22015-08-04 15:23:49 +00001115 g.bRaw = 1;
dan871f6e32015-08-03 17:03:31 +00001116 azArg++;
1117 nArg--;
1118 }
1119 }
1120
1121 if( nArg<2 ){
1122 usage(zPrg);
drh0de8c112002-07-06 16:32:14 +00001123 exit(1);
1124 }
dan871f6e32015-08-03 17:03:31 +00001125
dan8fb1bd22015-08-04 15:23:49 +00001126 fileOpen(zPrg, azArg[1]);
dan871f6e32015-08-03 17:03:31 +00001127 szFile = fileGetsize();
1128
1129 zPgSz = fileRead(16, 2);
1130 g.pagesize = zPgSz[0]*256 + zPgSz[1]*65536;
1131 if( g.pagesize==0 ) g.pagesize = 1024;
1132 sqlite3_free(zPgSz);
1133
1134 printf("Pagesize: %d\n", g.pagesize);
mistachkin77fac872016-04-12 20:05:06 +00001135 g.mxPage = (int)((szFile+g.pagesize-1)/g.pagesize);
dan871f6e32015-08-03 17:03:31 +00001136
1137 printf("Available pages: 1..%d\n", g.mxPage);
1138 if( nArg==2 ){
drh0de8c112002-07-06 16:32:14 +00001139 int i;
dan871f6e32015-08-03 17:03:31 +00001140 for(i=1; i<=g.mxPage; i++) print_page(i);
drh0de8c112002-07-06 16:32:14 +00001141 }else{
1142 int i;
dan871f6e32015-08-03 17:03:31 +00001143 for(i=2; i<nArg; i++){
drh0de8c112002-07-06 16:32:14 +00001144 int iStart, iEnd;
1145 char *zLeft;
dan871f6e32015-08-03 17:03:31 +00001146 if( strcmp(azArg[i], "dbheader")==0 ){
drh562cedb2010-04-26 15:44:07 +00001147 print_db_header();
1148 continue;
1149 }
dan871f6e32015-08-03 17:03:31 +00001150 if( strcmp(azArg[i], "pgidx")==0 ){
1151 page_usage_report(zPrg, azArg[1]);
drh3aeea462012-04-03 14:59:50 +00001152 continue;
1153 }
dan871f6e32015-08-03 17:03:31 +00001154 if( strcmp(azArg[i], "ptrmap")==0 ){
1155 ptrmap_coverage_report(azArg[1]);
drh344a97b2013-02-19 22:26:51 +00001156 continue;
1157 }
dan871f6e32015-08-03 17:03:31 +00001158 if( strcmp(azArg[i], "help")==0 ){
dan8fb1bd22015-08-04 15:23:49 +00001159 usage(zPrg);
drh344a97b2013-02-19 22:26:51 +00001160 continue;
1161 }
drhc56fac72015-10-29 13:48:15 +00001162 if( !ISDIGIT(azArg[i][0]) ){
dan8fb1bd22015-08-04 15:23:49 +00001163 fprintf(stderr, "%s: unknown option: [%s]\n", zPrg, azArg[i]);
drh562cedb2010-04-26 15:44:07 +00001164 continue;
1165 }
dan871f6e32015-08-03 17:03:31 +00001166 iStart = strtol(azArg[i], &zLeft, 0);
drh0de8c112002-07-06 16:32:14 +00001167 if( zLeft && strcmp(zLeft,"..end")==0 ){
dan871f6e32015-08-03 17:03:31 +00001168 iEnd = g.mxPage;
drh0de8c112002-07-06 16:32:14 +00001169 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
1170 iEnd = strtol(&zLeft[2], 0, 0);
drh7ecc1472010-04-26 16:47:12 +00001171 }else if( zLeft && zLeft[0]=='b' ){
1172 int ofst, nByte, hdrSize;
1173 unsigned char *a;
1174 if( iStart==1 ){
1175 ofst = hdrSize = 100;
dan871f6e32015-08-03 17:03:31 +00001176 nByte = g.pagesize-100;
drh7ecc1472010-04-26 16:47:12 +00001177 }else{
1178 hdrSize = 0;
dan871f6e32015-08-03 17:03:31 +00001179 ofst = (iStart-1)*g.pagesize;
1180 nByte = g.pagesize;
drh7ecc1472010-04-26 16:47:12 +00001181 }
dan871f6e32015-08-03 17:03:31 +00001182 a = fileRead(ofst, nByte);
drh100335b2011-01-05 21:20:52 +00001183 decode_btree_page(a, iStart, hdrSize, &zLeft[1]);
dan871f6e32015-08-03 17:03:31 +00001184 sqlite3_free(a);
drh7ecc1472010-04-26 16:47:12 +00001185 continue;
drh7d105f82010-08-23 15:26:49 +00001186 }else if( zLeft && zLeft[0]=='t' ){
drh7d105f82010-08-23 15:26:49 +00001187 int detail = 0;
1188 int recursive = 0;
mistachkin8ccdef62015-12-16 22:06:52 +00001189 int j;
1190 for(j=1; zLeft[j]; j++){
1191 if( zLeft[j]=='r' ) recursive = 1;
1192 if( zLeft[j]=='d' ) detail = 1;
drh7d105f82010-08-23 15:26:49 +00001193 }
dan871f6e32015-08-03 17:03:31 +00001194 decode_trunk_page(iStart, detail, recursive);
drh7d105f82010-08-23 15:26:49 +00001195 continue;
drh0de8c112002-07-06 16:32:14 +00001196 }else{
1197 iEnd = iStart;
1198 }
dan871f6e32015-08-03 17:03:31 +00001199 if( iStart<1 || iEnd<iStart || iEnd>g.mxPage ){
drh0de8c112002-07-06 16:32:14 +00001200 fprintf(stderr,
1201 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
dan871f6e32015-08-03 17:03:31 +00001202 g.mxPage);
drh0de8c112002-07-06 16:32:14 +00001203 exit(1);
1204 }
1205 while( iStart<=iEnd ){
1206 print_page(iStart);
1207 iStart++;
1208 }
1209 }
1210 }
dan871f6e32015-08-03 17:03:31 +00001211 fileClose();
drhdb718d82014-01-28 20:36:22 +00001212 return 0;
drh0de8c112002-07-06 16:32:14 +00001213}