drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1 | /* |
| 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> |
mistachkin | 026262b | 2012-10-13 09:31:20 +0000 | [diff] [blame] | 9 | |
| 10 | #if !defined(_MSC_VER) |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 11 | #include <unistd.h> |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 12 | #else |
| 13 | #include <io.h> |
mistachkin | 026262b | 2012-10-13 09:31:20 +0000 | [diff] [blame] | 14 | #endif |
| 15 | |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 17 | #include <string.h> |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 18 | #include <assert.h> |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 19 | #include "sqlite3.h" |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 20 | |
| 21 | |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 22 | static 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 */ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 27 | int bRaw; /* True to access db file via OS APIs */ |
dan | 30c16ad | 2015-08-04 15:29:43 +0000 | [diff] [blame] | 28 | sqlite3_file *pFd; /* File descriptor for non-raw mode */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 29 | sqlite3 *pDb; /* Database handle that owns pFd */ |
| 30 | } g = {1024, -1, 0, 16, 0, 0, 0}; |
| 31 | |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 32 | |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 33 | typedef 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 | */ |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 40 | static int decodeVarint(const unsigned char *z, i64 *pVal){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 41 | i64 v = 0; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 42 | int i; |
| 43 | for(i=0; i<8; i++){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 44 | 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 | |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 52 | /* |
| 53 | ** Extract a big-endian 32-bit integer |
| 54 | */ |
| 55 | static unsigned int decodeInt32(const unsigned char *z){ |
| 56 | return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3]; |
| 57 | } |
| 58 | |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 59 | /* Report an out-of-memory error and die. |
| 60 | */ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 61 | static void out_of_memory(void){ |
| 62 | fprintf(stderr,"Out of memory...\n"); |
| 63 | exit(1); |
| 64 | } |
| 65 | |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 66 | /* |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 67 | ** Open a database connection. |
| 68 | */ |
| 69 | static 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 | */ |
| 97 | static void fileOpen(const char *zPrg, const char *zName){ |
| 98 | assert( g.dbfd<0 ); |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 99 | if( g.bRaw==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 100 | 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 | */ |
| 122 | static void fileClose(){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 123 | if( g.bRaw==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 124 | 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 | /* |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 134 | ** Read content from the file. |
| 135 | ** |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 136 | ** Space to hold the content is obtained from sqlite3_malloc() and needs |
| 137 | ** to be freed by the caller. |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 138 | */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 139 | static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 140 | unsigned char *aData; |
drh | 748c735 | 2015-04-15 15:29:05 +0000 | [diff] [blame] | 141 | int got; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 142 | aData = sqlite3_malloc(nByte+32); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 143 | if( aData==0 ) out_of_memory(); |
drh | b7787ee | 2011-01-06 15:51:18 +0000 | [diff] [blame] | 144 | memset(aData, 0, nByte+32); |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 145 | if( g.bRaw==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 146 | 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 | } |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 156 | return aData; |
| 157 | } |
| 158 | |
| 159 | /* |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 160 | ** Return the size of the file in byte. |
| 161 | */ |
| 162 | static sqlite3_int64 fileGetsize(void){ |
| 163 | sqlite3_int64 res = 0; |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 164 | if( g.bRaw==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 165 | 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 | /* |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 183 | ** Print a range of bytes as hex and as ascii. |
| 184 | */ |
| 185 | static 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 | ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 190 | unsigned char *aData; |
| 191 | int i, j; |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 192 | 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 | |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 206 | aData = fileRead(ofst, nByte); |
| 207 | for(i=0; i<nByte; i += g.perLine){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 208 | fprintf(stdout, zOfstFmt, i+printOfst); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 209 | for(j=0; j<g.perLine; j++){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 210 | if( i+j>nByte ){ |
| 211 | fprintf(stdout, " "); |
| 212 | }else{ |
| 213 | fprintf(stdout,"%02x ", aData[i+j]); |
| 214 | } |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 215 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 216 | for(j=0; j<g.perLine; j++){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 217 | if( i+j>nByte ){ |
| 218 | fprintf(stdout, " "); |
| 219 | }else{ |
| 220 | fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.'); |
| 221 | } |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 222 | } |
| 223 | fprintf(stdout,"\n"); |
| 224 | } |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 225 | return aData; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | ** Print an entire page of content as hex |
| 230 | */ |
drh | db718d8 | 2014-01-28 20:36:22 +0000 | [diff] [blame] | 231 | static void print_page(int iPg){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 232 | int iStart; |
| 233 | unsigned char *aData; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 234 | iStart = (iPg-1)*g.pagesize; |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 235 | fprintf(stdout, "Page %d: (offsets 0x%x..0x%x)\n", |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 236 | iPg, iStart, iStart+g.pagesize-1); |
| 237 | aData = print_byte_range(iStart, g.pagesize, 0); |
| 238 | sqlite3_free(aData); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 239 | } |
| 240 | |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 241 | |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 242 | /* Print a line of decode output showing a 4-byte integer. |
| 243 | */ |
drh | db718d8 | 2014-01-28 20:36:22 +0000 | [diff] [blame] | 244 | static void print_decode_line( |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 245 | 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]); |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame] | 253 | i = (int)strlen(zBuf); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 254 | 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 | } |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame] | 261 | i += (int)strlen(&zBuf[i]); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 262 | } |
| 263 | sprintf(&zBuf[i], " %9d", val); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 264 | printf("%s %s\n", zBuf, zMsg); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
| 268 | ** Decode the database header. |
| 269 | */ |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 270 | static void print_db_header(void){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 271 | 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"); |
drh | 4ee09b4 | 2013-05-01 19:49:27 +0000 | [diff] [blame] | 289 | print_decode_line(aData, 68, 4, "Application ID"); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 290 | 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]"); |
drh | b28e59b | 2010-06-17 02:13:39 +0000 | [diff] [blame] | 295 | print_decode_line(aData, 92, 4, "Change counter for version number"); |
drh | c6d2b4a | 2010-04-26 17:30:52 +0000 | [diff] [blame] | 296 | print_decode_line(aData, 96, 4, "SQLite version number"); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 297 | } |
| 298 | |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 299 | /* |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 300 | ** Describe cell content. |
| 301 | */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 302 | static i64 describeContent( |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 303 | unsigned char *a, /* Cell content */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 304 | i64 nLocal, /* Bytes in a[] */ |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 305 | char *zDesc /* Write description here */ |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 306 | ){ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 307 | i64 nDesc = 0; |
| 308 | int n, j; |
| 309 | i64 i, x, v; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 310 | const unsigned char *pData; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 311 | const unsigned char *pLimit; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 312 | char sep = ' '; |
| 313 | |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 314 | pLimit = &a[nLocal]; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 315 | n = decodeVarint(a, &x); |
| 316 | pData = &a[x]; |
| 317 | a += n; |
| 318 | i = x - n; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 319 | while( i>0 && pData<=pLimit ){ |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 320 | n = decodeVarint(a, &x); |
| 321 | a += n; |
| 322 | i -= n; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 323 | nLocal -= n; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 324 | zDesc[0] = sep; |
| 325 | sep = ','; |
| 326 | nDesc++; |
| 327 | zDesc++; |
| 328 | if( x==0 ){ |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 329 | sprintf(zDesc, "*"); /* NULL is a "*" */ |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 330 | }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 ){ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 349 | i64 size = (x-12)/2; |
drh | b2c062d | 2011-01-05 21:46:52 +0000 | [diff] [blame] | 350 | if( (x&1)==0 ){ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 351 | sprintf(zDesc, "blob(%lld)", size); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 352 | }else{ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 353 | sprintf(zDesc, "txt(%lld)", size); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 354 | } |
| 355 | pData += size; |
| 356 | } |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame] | 357 | j = (int)strlen(zDesc); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 358 | zDesc += j; |
| 359 | nDesc += j; |
| 360 | } |
| 361 | return nDesc; |
| 362 | } |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | ** Compute the local payload size given the total payload size and |
| 366 | ** the page size. |
| 367 | */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 368 | static i64 localPayload(i64 nPayload, char cType){ |
| 369 | i64 maxLocal; |
| 370 | i64 minLocal; |
| 371 | i64 surplus; |
| 372 | i64 nLocal; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 373 | if( cType==13 ){ |
| 374 | /* Table leaf */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 375 | maxLocal = g.pagesize-35; |
| 376 | minLocal = (g.pagesize-12)*32/255-23; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 377 | }else{ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 378 | maxLocal = (g.pagesize-12)*64/255-23; |
| 379 | minLocal = (g.pagesize-12)*32/255-23; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 380 | } |
| 381 | if( nPayload>maxLocal ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 382 | surplus = minLocal + (nPayload-minLocal)%(g.pagesize-4); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 383 | if( surplus<=maxLocal ){ |
| 384 | nLocal = surplus; |
| 385 | }else{ |
| 386 | nLocal = minLocal; |
| 387 | } |
| 388 | }else{ |
| 389 | nLocal = nPayload; |
| 390 | } |
| 391 | return nLocal; |
| 392 | } |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 393 | |
| 394 | |
| 395 | /* |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 396 | ** Create a description for a single cell. |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 397 | ** |
| 398 | ** The return value is the local cell size. |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 399 | */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 400 | static i64 describeCell( |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 401 | 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 | ){ |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 406 | int i; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 407 | i64 nDesc = 0; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 408 | int n = 0; |
| 409 | int leftChild; |
| 410 | i64 nPayload; |
| 411 | i64 rowid; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 412 | i64 nLocal; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 413 | static char zDesc[1000]; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 414 | 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; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 419 | sprintf(zDesc, "lx: %d ", leftChild); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 420 | nDesc = strlen(zDesc); |
| 421 | } |
| 422 | if( cType!=5 ){ |
| 423 | i = decodeVarint(a, &nPayload); |
| 424 | a += i; |
| 425 | n += i; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 426 | sprintf(&zDesc[nDesc], "n: %lld ", nPayload); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 427 | nDesc += strlen(&zDesc[nDesc]); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 428 | nLocal = localPayload(nPayload, cType); |
| 429 | }else{ |
| 430 | nPayload = nLocal = 0; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 431 | } |
| 432 | if( cType==5 || cType==13 ){ |
| 433 | i = decodeVarint(a, &rowid); |
| 434 | a += i; |
| 435 | n += i; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 436 | sprintf(&zDesc[nDesc], "r: %lld ", rowid); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 437 | nDesc += strlen(&zDesc[nDesc]); |
| 438 | } |
drh | b7787ee | 2011-01-06 15:51:18 +0000 | [diff] [blame] | 439 | 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 | } |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 447 | if( showCellContent && cType!=5 ){ |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 448 | nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 449 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 450 | *pzDesc = zDesc; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 451 | return nLocal+n; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 452 | } |
| 453 | |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 454 | /* Print an offset followed by nByte bytes. Add extra white-space |
| 455 | ** at the end so that subsequent text is aligned. |
| 456 | */ |
| 457 | static 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 | */ |
| 478 | static 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 | ){ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 485 | int i, j = 0; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 486 | int leftChild; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 487 | i64 k; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 488 | i64 nPayload; |
| 489 | i64 rowid; |
| 490 | i64 nHdr; |
| 491 | i64 iType; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 492 | i64 nLocal; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 493 | unsigned char *x = a + ofst; |
| 494 | unsigned char *end; |
| 495 | unsigned char cType = a[0]; |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 496 | int nCol = 0; |
| 497 | int szCol[2000]; |
| 498 | int ofstCol[2000]; |
| 499 | int typeCol[2000]; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 500 | |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 501 | printf("Cell[%d]:\n", iCell); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 502 | 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); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 512 | if( nLocal==nPayload ){ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 513 | printf("payload-size: %lld\n", nPayload); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 514 | }else{ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 515 | printf("payload-size: %lld (%lld local, %lld overflow)\n", |
| 516 | nPayload, nLocal, nPayload-nLocal); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 517 | } |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 518 | 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); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 532 | printf("record-header-size: %d\n", (int)nHdr); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 533 | j = i; |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 534 | nCol = 0; |
| 535 | k = nHdr; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 536 | 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); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 542 | printf("typecode[%d]: %d - ", nCol, (int)iType); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 543 | 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); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 564 | szCol[nCol] = sz; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 565 | ofstCol[nCol] = (int)k; |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 566 | typeCol[nCol] = (int)iType; |
| 567 | k += sz; |
| 568 | nCol++; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 569 | j += i; |
| 570 | } |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 571 | 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{ |
drh | 56e67dd | 2014-06-20 13:55:06 +0000 | [diff] [blame] | 592 | 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); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 613 | } |
| 614 | j = ofstCol[i] + szCol[i]; |
| 615 | } |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 616 | } |
| 617 | if( j<nLocal ){ |
| 618 | printBytes(a, x+j, 0); |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 619 | printf("... %lld bytes of content ...\n", nLocal-j); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 620 | } |
| 621 | if( nLocal<nPayload ){ |
| 622 | printBytes(a, x+nLocal, 4); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 623 | printf("overflow-page: %d\n", decodeInt32(x+nLocal)); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 628 | /* |
| 629 | ** Decode a btree page |
| 630 | */ |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 631 | static 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 | ){ |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 637 | const char *zType = "unknown"; |
| 638 | int nCell; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 639 | int i, j; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 640 | int iCellPtr; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 641 | int showCellContent = 0; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 642 | int showMap = 0; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 643 | int cellToDecode = -2; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 644 | char *zMap = 0; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 645 | 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 | } |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 651 | while( zArgs[0] ){ |
| 652 | switch( zArgs[0] ){ |
| 653 | case 'c': showCellContent = 1; break; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 654 | case 'm': showMap = 1; break; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 655 | 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 | } |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 667 | } |
| 668 | zArgs++; |
| 669 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 670 | nCell = a[3]*256 + a[4]; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 671 | iCellPtr = (a[0]==2 || a[0]==5) ? 12 : 8; |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 672 | if( cellToDecode>=nCell ){ |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 673 | printf("Page %d has only %d cells\n", pgno, nCell); |
| 674 | return; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 675 | } |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 676 | 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 | } |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 688 | if( showMap ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 689 | zMap = sqlite3_malloc(g.pagesize); |
| 690 | memset(zMap, '.', g.pagesize); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 691 | memset(zMap, '1', hdrSize); |
| 692 | memset(&zMap[hdrSize], 'H', iCellPtr); |
| 693 | memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell); |
| 694 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 695 | for(i=0; i<nCell; i++){ |
| 696 | int cofst = iCellPtr + i*2; |
| 697 | char *zDesc; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 698 | i64 n; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 699 | |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 700 | cofst = a[cofst]*256 + a[cofst+1]; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 701 | n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc); |
| 702 | if( showMap ){ |
| 703 | char zBuf[30]; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 704 | memset(&zMap[cofst], '*', (size_t)n); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 705 | zMap[cofst] = '['; |
| 706 | zMap[cofst+n-1] = ']'; |
| 707 | sprintf(zBuf, "%d", i); |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame] | 708 | j = (int)strlen(zBuf); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 709 | if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j); |
| 710 | } |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 711 | 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 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 716 | } |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 717 | if( showMap ){ |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 718 | printf("Page map: (H=header P=cell-index 1=page-1-header .=free-space)\n"); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 719 | for(i=0; i<g.pagesize; i+=64){ |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 720 | printf(" %03x: %.64s\n", i, &zMap[i]); |
| 721 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 722 | sqlite3_free(zMap); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 723 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 724 | } |
| 725 | |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 726 | /* |
| 727 | ** Decode a freelist trunk page. |
| 728 | */ |
| 729 | static void decode_trunk_page( |
| 730 | int pgno, /* The page number */ |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 731 | int detail, /* Show leaf pages if true */ |
| 732 | int recursive /* Follow the trunk change if true */ |
| 733 | ){ |
drh | db718d8 | 2014-01-28 20:36:22 +0000 | [diff] [blame] | 734 | int n, i; |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 735 | unsigned char *a; |
| 736 | while( pgno>0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 737 | a = fileRead((pgno-1)*g.pagesize, g.pagesize); |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 738 | 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 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 757 | sqlite3_free(a); |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
| 761 | /* |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 762 | ** A short text comment on the use of each page. |
| 763 | */ |
| 764 | static char **zPageUse; |
| 765 | |
| 766 | /* |
| 767 | ** Add a comment on the use of a page. |
| 768 | */ |
| 769 | static 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); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 776 | if( pgno<=0 || pgno>g.mxPage ){ |
drh | edf9a17 | 2013-03-05 01:46:26 +0000 | [diff] [blame] | 777 | printf("ERROR: page %d out of range 1..%d: %s\n", |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 778 | pgno, g.mxPage, zMsg); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 779 | 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]); |
drh | 103a70f | 2013-02-19 20:25:16 +0000 | [diff] [blame] | 785 | printf("ERROR: current: %s\n", zMsg); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 786 | sqlite3_free(zPageUse[pgno]); |
| 787 | } |
| 788 | zPageUse[pgno] = zMsg; |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | ** Find overflow pages of a cell and describe their usage. |
| 793 | */ |
| 794 | static 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; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 801 | int n = 0; |
| 802 | i64 nPayload; |
| 803 | i64 rowid; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 804 | i64 nLocal; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 805 | 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; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 826 | while( ovfl && (cnt++)<g.mxPage ){ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 827 | page_usage_msg(ovfl, "overflow %d from cell %d of page %d", |
| 828 | cnt, cellno, pgno); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 829 | a = fileRead((ovfl-1)*g.pagesize, 4); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 830 | ovfl = decodeInt32(a); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 831 | sqlite3_free(a); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | |
| 837 | /* |
| 838 | ** Describe the usages of a b-tree page |
| 839 | */ |
| 840 | static 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 | |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 852 | if( pgno<=0 || pgno>g.mxPage ) return; |
| 853 | a = fileRead((pgno-1)*g.pagesize, g.pagesize); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 854 | 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 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 890 | sqlite3_free(a); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /* |
| 894 | ** Determine page usage by the freelist |
| 895 | */ |
| 896 | static 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 | |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 904 | while( pgno>0 && pgno<=g.mxPage && (cnt++)<g.mxPage ){ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 905 | page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 906 | a = fileRead((pgno-1)*g.pagesize, g.pagesize); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 907 | 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 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 914 | sqlite3_free(a); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 915 | parent = pgno; |
| 916 | pgno = iNext; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | /* |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 921 | ** Determine pages used as PTRMAP pages |
| 922 | */ |
| 923 | static void page_usage_ptrmap(unsigned char *a){ |
| 924 | if( a[55] ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 925 | int usable = g.pagesize - a[20]; |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 926 | int pgno = 2; |
| 927 | int perPage = usable/5; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 928 | while( pgno<=g.mxPage ){ |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 929 | page_usage_msg(pgno, "PTRMAP page covering %d..%d", |
| 930 | pgno+1, pgno+perPage); |
| 931 | pgno += perPage + 1; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | /* |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 937 | ** Try to figure out how every page in the database file is being used. |
| 938 | */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 939 | static void page_usage_report(const char *zPrg, const char *zDbName){ |
drh | 00e637f | 2013-02-19 18:45:11 +0000 | [diff] [blame] | 940 | int i, j; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 941 | int rc; |
| 942 | sqlite3 *db; |
| 943 | sqlite3_stmt *pStmt; |
| 944 | unsigned char *a; |
drh | 00e637f | 2013-02-19 18:45:11 +0000 | [diff] [blame] | 945 | char zQuery[200]; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 946 | |
| 947 | /* Avoid the pathological case */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 948 | if( g.mxPage<1 ){ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 949 | printf("empty database\n"); |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | /* Open the database file */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 954 | db = openDatabase(zPrg, zDbName); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 955 | |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 956 | /* Set up global variables zPageUse[] and g.mxPage to record page |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 957 | ** usages */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 958 | zPageUse = sqlite3_malloc( sizeof(zPageUse[0])*(g.mxPage+1) ); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 959 | if( zPageUse==0 ) out_of_memory(); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 960 | memset(zPageUse, 0, sizeof(zPageUse[0])*(g.mxPage+1)); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 961 | |
| 962 | /* Discover the usage of each page */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 963 | a = fileRead(0, 100); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 964 | page_usage_freelist(decodeInt32(a+32)); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 965 | page_usage_ptrmap(a); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 966 | sqlite3_free(a); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 967 | page_usage_btree(1, 0, 0, "sqlite_master"); |
drh | 00e637f | 2013-02-19 18:45:11 +0000 | [diff] [blame] | 968 | 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); |
drh | db718d8 | 2014-01-28 20:36:22 +0000 | [diff] [blame] | 977 | page_usage_btree(pgno, 0, 0, (const char*)sqlite3_column_text(pStmt,1)); |
drh | 00e637f | 2013-02-19 18:45:11 +0000 | [diff] [blame] | 978 | } |
| 979 | }else{ |
| 980 | printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db)); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 981 | } |
drh | 00e637f | 2013-02-19 18:45:11 +0000 | [diff] [blame] | 982 | rc = sqlite3_finalize(pStmt); |
| 983 | if( rc==SQLITE_OK ) break; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 984 | } |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 985 | sqlite3_close(db); |
| 986 | |
| 987 | /* Print the report and free memory used */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 988 | for(i=1; i<=g.mxPage; i++){ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 989 | 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 | /* |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 997 | ** Try to figure out how every page in the database file is being used. |
| 998 | */ |
| 999 | static void ptrmap_coverage_report(const char *zDbName){ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 1000 | int pgno; |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1001 | unsigned char *aHdr; |
| 1002 | unsigned char *a; |
| 1003 | int usable; |
| 1004 | int perPage; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 1005 | int i; |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1006 | |
| 1007 | /* Avoid the pathological case */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1008 | if( g.mxPage<1 ){ |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1009 | printf("empty database\n"); |
| 1010 | return; |
| 1011 | } |
| 1012 | |
| 1013 | /* Make sure PTRMAPs are used in this database */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1014 | aHdr = fileRead(0, 100); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1015 | if( aHdr[55]==0 ){ |
| 1016 | printf("database does not use PTRMAP pages\n"); |
| 1017 | return; |
| 1018 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1019 | usable = g.pagesize - aHdr[20]; |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1020 | perPage = usable/5; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1021 | sqlite3_free(aHdr); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1022 | printf("%5d: root of sqlite_master\n", 1); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1023 | for(pgno=2; pgno<=g.mxPage; pgno += perPage+1){ |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1024 | printf("%5d: PTRMAP page covering %d..%d\n", pgno, |
| 1025 | pgno+1, pgno+perPage); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1026 | a = fileRead((pgno-1)*g.pagesize, usable); |
| 1027 | for(i=0; i+5<=usable && pgno+1+i/5<=g.mxPage; i+=5){ |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1028 | 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 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1039 | sqlite3_free(a); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | /* |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1044 | ** Print a usage comment |
| 1045 | */ |
| 1046 | static void usage(const char *argv0){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1047 | fprintf(stderr, "Usage %s ?--uri? FILENAME ?args...?\n\n", argv0); |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1048 | fprintf(stderr, |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1049 | "switches:\n" |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1050 | " --raw Read db file directly, bypassing SQLite VFS\n" |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1051 | "args:\n" |
| 1052 | " dbheader Show database header\n" |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1053 | " pgidx Index of how each page is used\n" |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1054 | " ptrmap Show all PTRMAP page content\n" |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1055 | " 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" |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 1058 | " NNNbc Decode btree page NNN and show content\n" |
| 1059 | " NNNbm Decode btree page NNN and show a layout map\n" |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 1060 | " NNNbdCCC Decode cell CCC on btree page NNN\n" |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1061 | " NNNt Decode freelist trunk page NNN\n" |
drh | 47fb000 | 2011-04-13 16:52:41 +0000 | [diff] [blame] | 1062 | " NNNtd Show leaf freelist pages on the decode\n" |
dan | 53d89cd | 2014-08-20 10:42:16 +0000 | [diff] [blame] | 1063 | " NNNtr Recursively decode freelist starting at NNN\n" |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1064 | ); |
| 1065 | } |
| 1066 | |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1067 | int main(int argc, char **argv){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1068 | 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 ){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1076 | if( sqlite3_stricmp("-raw", azArg[1])==0 |
| 1077 | || sqlite3_stricmp("--raw", azArg[1])==0 |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1078 | ){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1079 | g.bRaw = 1; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1080 | azArg++; |
| 1081 | nArg--; |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | if( nArg<2 ){ |
| 1086 | usage(zPrg); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1087 | exit(1); |
| 1088 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1089 | |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1090 | fileOpen(zPrg, azArg[1]); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1091 | 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 ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1103 | int i; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1104 | for(i=1; i<=g.mxPage; i++) print_page(i); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1105 | }else{ |
| 1106 | int i; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1107 | for(i=2; i<nArg; i++){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1108 | int iStart, iEnd; |
| 1109 | char *zLeft; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1110 | if( strcmp(azArg[i], "dbheader")==0 ){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 1111 | print_db_header(); |
| 1112 | continue; |
| 1113 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1114 | if( strcmp(azArg[i], "pgidx")==0 ){ |
| 1115 | page_usage_report(zPrg, azArg[1]); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1116 | continue; |
| 1117 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1118 | if( strcmp(azArg[i], "ptrmap")==0 ){ |
| 1119 | ptrmap_coverage_report(azArg[1]); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1120 | continue; |
| 1121 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1122 | if( strcmp(azArg[i], "help")==0 ){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1123 | usage(zPrg); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1124 | continue; |
| 1125 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1126 | if( !isdigit(azArg[i][0]) ){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1127 | fprintf(stderr, "%s: unknown option: [%s]\n", zPrg, azArg[i]); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 1128 | continue; |
| 1129 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1130 | iStart = strtol(azArg[i], &zLeft, 0); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1131 | if( zLeft && strcmp(zLeft,"..end")==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1132 | iEnd = g.mxPage; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1133 | }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ |
| 1134 | iEnd = strtol(&zLeft[2], 0, 0); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 1135 | }else if( zLeft && zLeft[0]=='b' ){ |
| 1136 | int ofst, nByte, hdrSize; |
| 1137 | unsigned char *a; |
| 1138 | if( iStart==1 ){ |
| 1139 | ofst = hdrSize = 100; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1140 | nByte = g.pagesize-100; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 1141 | }else{ |
| 1142 | hdrSize = 0; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1143 | ofst = (iStart-1)*g.pagesize; |
| 1144 | nByte = g.pagesize; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 1145 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1146 | a = fileRead(ofst, nByte); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 1147 | decode_btree_page(a, iStart, hdrSize, &zLeft[1]); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1148 | sqlite3_free(a); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 1149 | continue; |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1150 | }else if( zLeft && zLeft[0]=='t' ){ |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1151 | 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 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1158 | decode_trunk_page(iStart, detail, recursive); |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1159 | continue; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1160 | }else{ |
| 1161 | iEnd = iStart; |
| 1162 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1163 | if( iStart<1 || iEnd<iStart || iEnd>g.mxPage ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1164 | fprintf(stderr, |
| 1165 | "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1166 | g.mxPage); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1167 | exit(1); |
| 1168 | } |
| 1169 | while( iStart<=iEnd ){ |
| 1170 | print_page(iStart); |
| 1171 | iStart++; |
| 1172 | } |
| 1173 | } |
| 1174 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1175 | fileClose(); |
drh | db718d8 | 2014-01-28 20:36:22 +0000 | [diff] [blame] | 1176 | return 0; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1177 | } |