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> |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 6 | #define ISDIGIT(X) isdigit((unsigned char)(X)) |
| 7 | #define ISPRINT(X) isprint((unsigned char)(X)) |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <fcntl.h> |
mistachkin | 026262b | 2012-10-13 09:31:20 +0000 | [diff] [blame] | 11 | |
| 12 | #if !defined(_MSC_VER) |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 13 | #include <unistd.h> |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 14 | #else |
| 15 | #include <io.h> |
mistachkin | 026262b | 2012-10-13 09:31:20 +0000 | [diff] [blame] | 16 | #endif |
| 17 | |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 18 | #include <stdlib.h> |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 19 | #include <string.h> |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 20 | #include <assert.h> |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 21 | #include "sqlite3.h" |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 22 | |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 23 | typedef unsigned char u8; /* unsigned 8-bit */ |
| 24 | typedef unsigned int u32; /* unsigned 32-bit */ |
| 25 | typedef sqlite3_int64 i64; /* signed 64-bit */ |
| 26 | typedef sqlite3_uint64 u64; /* unsigned 64-bit */ |
| 27 | |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 28 | |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 29 | static struct GlobalData { |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 30 | u32 pagesize; /* Size of a database page */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 31 | int dbfd; /* File descriptor for reading the DB */ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 32 | u32 mxPage; /* Last page number */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 33 | int perLine; /* HEX elements to print per line */ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 34 | int bRaw; /* True to access db file via OS APIs */ |
dan | 30c16ad | 2015-08-04 15:29:43 +0000 | [diff] [blame] | 35 | sqlite3_file *pFd; /* File descriptor for non-raw mode */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 36 | sqlite3 *pDb; /* Database handle that owns pFd */ |
| 37 | } g = {1024, -1, 0, 16, 0, 0, 0}; |
| 38 | |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 39 | /* |
| 40 | ** Convert the var-int format into i64. Return the number of bytes |
| 41 | ** in the var-int. Write the var-int value into *pVal. |
| 42 | */ |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 43 | static int decodeVarint(const unsigned char *z, i64 *pVal){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 44 | i64 v = 0; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 45 | int i; |
| 46 | for(i=0; i<8; i++){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 47 | v = (v<<7) + (z[i]&0x7f); |
| 48 | if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; } |
| 49 | } |
| 50 | v = (v<<8) + (z[i]&0xff); |
| 51 | *pVal = v; |
| 52 | return 9; |
| 53 | } |
| 54 | |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 55 | /* |
| 56 | ** Extract a big-endian 32-bit integer |
| 57 | */ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 58 | static u32 decodeInt32(const u8 *z){ |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 59 | return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3]; |
| 60 | } |
| 61 | |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 62 | /* Report an out-of-memory error and die. |
| 63 | */ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 64 | static void out_of_memory(void){ |
| 65 | fprintf(stderr,"Out of memory...\n"); |
| 66 | exit(1); |
| 67 | } |
| 68 | |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 69 | /* |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 70 | ** Open a database connection. |
| 71 | */ |
| 72 | static sqlite3 *openDatabase(const char *zPrg, const char *zName){ |
| 73 | sqlite3 *db = 0; |
| 74 | int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI; |
| 75 | int rc = sqlite3_open_v2(zName, &db, flags, 0); |
| 76 | if( rc!=SQLITE_OK ){ |
| 77 | const char *zErr = sqlite3_errmsg(db); |
| 78 | fprintf(stderr, "%s: can't open %s (%s)\n", zPrg, zName, zErr); |
| 79 | sqlite3_close(db); |
| 80 | exit(1); |
| 81 | } |
| 82 | return db; |
| 83 | } |
| 84 | |
| 85 | /************************************************************************** |
| 86 | ** Beginning of low-level file access functions. |
| 87 | ** |
| 88 | ** All low-level access to the database file read by this program is |
| 89 | ** performed using the following four functions: |
| 90 | ** |
| 91 | ** fileOpen() - open the db file |
| 92 | ** fileClose() - close the db file |
| 93 | ** fileRead() - read raw data from the db file |
| 94 | ** fileGetsize() - return the size of the db file in bytes |
| 95 | */ |
| 96 | |
| 97 | /* |
| 98 | ** Open the database file. |
| 99 | */ |
| 100 | static void fileOpen(const char *zPrg, const char *zName){ |
| 101 | assert( g.dbfd<0 ); |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 102 | if( g.bRaw==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 103 | int rc; |
| 104 | void *pArg = (void *)(&g.pFd); |
| 105 | g.pDb = openDatabase(zPrg, zName); |
| 106 | rc = sqlite3_file_control(g.pDb, "main", SQLITE_FCNTL_FILE_POINTER, pArg); |
| 107 | if( rc!=SQLITE_OK ){ |
| 108 | fprintf(stderr, |
| 109 | "%s: failed to obtain fd for %s (SQLite too old?)\n", zPrg, zName |
| 110 | ); |
| 111 | exit(1); |
| 112 | } |
| 113 | }else{ |
| 114 | g.dbfd = open(zName, O_RDONLY); |
| 115 | if( g.dbfd<0 ){ |
| 116 | fprintf(stderr,"%s: can't open %s\n", zPrg, zName); |
| 117 | exit(1); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | ** Close the database file opened by fileOpen() |
| 124 | */ |
| 125 | static void fileClose(){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 126 | if( g.bRaw==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 127 | sqlite3_close(g.pDb); |
| 128 | g.pDb = 0; |
| 129 | g.pFd = 0; |
| 130 | }else{ |
| 131 | close(g.dbfd); |
| 132 | g.dbfd = -1; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 137 | ** Read content from the file. |
| 138 | ** |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 139 | ** Space to hold the content is obtained from sqlite3_malloc() and needs |
| 140 | ** to be freed by the caller. |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 141 | */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 142 | static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 143 | unsigned char *aData; |
drh | 748c735 | 2015-04-15 15:29:05 +0000 | [diff] [blame] | 144 | int got; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 145 | aData = sqlite3_malloc64(32+(i64)nByte); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 146 | if( aData==0 ) out_of_memory(); |
drh | b7787ee | 2011-01-06 15:51:18 +0000 | [diff] [blame] | 147 | memset(aData, 0, nByte+32); |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 148 | if( g.bRaw==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 149 | int rc = g.pFd->pMethods->xRead(g.pFd, (void*)aData, nByte, ofst); |
| 150 | if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ |
| 151 | fprintf(stderr, "error in xRead() - %d\n", rc); |
| 152 | exit(1); |
| 153 | } |
| 154 | }else{ |
mistachkin | 77fac87 | 2016-04-12 20:05:06 +0000 | [diff] [blame] | 155 | lseek(g.dbfd, (long)ofst, SEEK_SET); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 156 | got = read(g.dbfd, aData, nByte); |
| 157 | if( got>0 && got<nByte ) memset(aData+got, 0, nByte-got); |
| 158 | } |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 159 | return aData; |
| 160 | } |
| 161 | |
| 162 | /* |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 163 | ** Return the size of the file in byte. |
| 164 | */ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 165 | static i64 fileGetsize(void){ |
| 166 | i64 res = 0; |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 167 | if( g.bRaw==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 168 | int rc = g.pFd->pMethods->xFileSize(g.pFd, &res); |
| 169 | if( rc!=SQLITE_OK ){ |
| 170 | fprintf(stderr, "error in xFileSize() - %d\n", rc); |
| 171 | exit(1); |
| 172 | } |
| 173 | }else{ |
| 174 | struct stat sbuf; |
| 175 | fstat(g.dbfd, &sbuf); |
| 176 | res = (sqlite3_int64)(sbuf.st_size); |
| 177 | } |
| 178 | return res; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | ** End of low-level file access functions. |
| 183 | **************************************************************************/ |
| 184 | |
| 185 | /* |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 186 | ** Print a range of bytes as hex and as ascii. |
| 187 | */ |
| 188 | static unsigned char *print_byte_range( |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 189 | sqlite3_int64 ofst, /* First byte in the range of bytes to print */ |
| 190 | int nByte, /* Number of bytes to print */ |
| 191 | int printOfst /* Add this amount to the index on the left column */ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 192 | ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 193 | unsigned char *aData; |
| 194 | int i, j; |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 195 | const char *zOfstFmt; |
| 196 | |
| 197 | if( ((printOfst+nByte)&~0xfff)==0 ){ |
| 198 | zOfstFmt = " %03x: "; |
| 199 | }else if( ((printOfst+nByte)&~0xffff)==0 ){ |
| 200 | zOfstFmt = " %04x: "; |
| 201 | }else if( ((printOfst+nByte)&~0xfffff)==0 ){ |
| 202 | zOfstFmt = " %05x: "; |
| 203 | }else if( ((printOfst+nByte)&~0xffffff)==0 ){ |
| 204 | zOfstFmt = " %06x: "; |
| 205 | }else{ |
| 206 | zOfstFmt = " %08x: "; |
| 207 | } |
| 208 | |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 209 | aData = fileRead(ofst, nByte); |
| 210 | for(i=0; i<nByte; i += g.perLine){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 211 | int go = 0; |
| 212 | for(j=0; j<g.perLine; j++){ |
| 213 | if( i+j>nByte ){ break; } |
| 214 | if( aData[i+j] ){ go = 1; break; } |
| 215 | } |
| 216 | if( !go && i>0 && i+g.perLine<nByte ) continue; |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 217 | fprintf(stdout, zOfstFmt, i+printOfst); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 218 | for(j=0; j<g.perLine; j++){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 219 | if( i+j>nByte ){ |
| 220 | fprintf(stdout, " "); |
| 221 | }else{ |
| 222 | fprintf(stdout,"%02x ", aData[i+j]); |
| 223 | } |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 224 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 225 | for(j=0; j<g.perLine; j++){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 226 | if( i+j>nByte ){ |
| 227 | fprintf(stdout, " "); |
| 228 | }else{ |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 229 | fprintf(stdout,"%c", ISPRINT(aData[i+j]) ? aData[i+j] : '.'); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 230 | } |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 231 | } |
| 232 | fprintf(stdout,"\n"); |
| 233 | } |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 234 | return aData; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | ** Print an entire page of content as hex |
| 239 | */ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 240 | static void print_page(u32 iPg){ |
| 241 | i64 iStart; |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 242 | unsigned char *aData; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 243 | iStart = ((i64)(iPg-1))*g.pagesize; |
| 244 | fprintf(stdout, "Page %u: (offsets 0x%llx..0x%llx)\n", |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 245 | iPg, iStart, iStart+g.pagesize-1); |
| 246 | aData = print_byte_range(iStart, g.pagesize, 0); |
| 247 | sqlite3_free(aData); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 248 | } |
| 249 | |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 250 | |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 251 | /* Print a line of decoded output showing a 4-byte unsigned integer. |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 252 | */ |
drh | db718d8 | 2014-01-28 20:36:22 +0000 | [diff] [blame] | 253 | static void print_decode_line( |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 254 | unsigned char *aData, /* Content being decoded */ |
| 255 | int ofst, int nByte, /* Start and size of decode */ |
| 256 | const char *zMsg /* Message to append */ |
| 257 | ){ |
| 258 | int i, j; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 259 | u32 val = aData[ofst]; |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 260 | char zBuf[100]; |
| 261 | sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]); |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame] | 262 | i = (int)strlen(zBuf); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 263 | for(j=1; j<4; j++){ |
| 264 | if( j>=nByte ){ |
| 265 | sprintf(&zBuf[i], " "); |
| 266 | }else{ |
| 267 | sprintf(&zBuf[i], " %02x", aData[ofst+j]); |
| 268 | val = val*256 + aData[ofst+j]; |
| 269 | } |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame] | 270 | i += (int)strlen(&zBuf[i]); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 271 | } |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 272 | sprintf(&zBuf[i], " %10u", val); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 273 | printf("%s %s\n", zBuf, zMsg); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | ** Decode the database header. |
| 278 | */ |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 279 | static void print_db_header(void){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 280 | unsigned char *aData; |
| 281 | aData = print_byte_range(0, 100, 0); |
| 282 | printf("Decoded:\n"); |
| 283 | print_decode_line(aData, 16, 2, "Database page size"); |
| 284 | print_decode_line(aData, 18, 1, "File format write version"); |
| 285 | print_decode_line(aData, 19, 1, "File format read version"); |
| 286 | print_decode_line(aData, 20, 1, "Reserved space at end of page"); |
| 287 | print_decode_line(aData, 24, 4, "File change counter"); |
| 288 | print_decode_line(aData, 28, 4, "Size of database in pages"); |
| 289 | print_decode_line(aData, 32, 4, "Page number of first freelist page"); |
| 290 | print_decode_line(aData, 36, 4, "Number of freelist pages"); |
| 291 | print_decode_line(aData, 40, 4, "Schema cookie"); |
| 292 | print_decode_line(aData, 44, 4, "Schema format version"); |
| 293 | print_decode_line(aData, 48, 4, "Default page cache size"); |
| 294 | print_decode_line(aData, 52, 4, "Largest auto-vac root page"); |
| 295 | print_decode_line(aData, 56, 4, "Text encoding"); |
| 296 | print_decode_line(aData, 60, 4, "User version"); |
| 297 | print_decode_line(aData, 64, 4, "Incremental-vacuum mode"); |
drh | 4ee09b4 | 2013-05-01 19:49:27 +0000 | [diff] [blame] | 298 | print_decode_line(aData, 68, 4, "Application ID"); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 299 | print_decode_line(aData, 72, 4, "meta[8]"); |
| 300 | print_decode_line(aData, 76, 4, "meta[9]"); |
| 301 | print_decode_line(aData, 80, 4, "meta[10]"); |
| 302 | print_decode_line(aData, 84, 4, "meta[11]"); |
| 303 | print_decode_line(aData, 88, 4, "meta[12]"); |
drh | b28e59b | 2010-06-17 02:13:39 +0000 | [diff] [blame] | 304 | print_decode_line(aData, 92, 4, "Change counter for version number"); |
drh | c6d2b4a | 2010-04-26 17:30:52 +0000 | [diff] [blame] | 305 | print_decode_line(aData, 96, 4, "SQLite version number"); |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 306 | sqlite3_free(aData); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 307 | } |
| 308 | |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 309 | /* |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 310 | ** Describe cell content. |
| 311 | */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 312 | static i64 describeContent( |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 313 | unsigned char *a, /* Cell content */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 314 | i64 nLocal, /* Bytes in a[] */ |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 315 | char *zDesc /* Write description here */ |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 316 | ){ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 317 | i64 nDesc = 0; |
| 318 | int n, j; |
| 319 | i64 i, x, v; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 320 | const unsigned char *pData; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 321 | const unsigned char *pLimit; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 322 | char sep = ' '; |
| 323 | |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 324 | pLimit = &a[nLocal]; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 325 | n = decodeVarint(a, &x); |
| 326 | pData = &a[x]; |
| 327 | a += n; |
| 328 | i = x - n; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 329 | while( i>0 && pData<=pLimit ){ |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 330 | n = decodeVarint(a, &x); |
| 331 | a += n; |
| 332 | i -= n; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 333 | nLocal -= n; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 334 | zDesc[0] = sep; |
| 335 | sep = ','; |
| 336 | nDesc++; |
| 337 | zDesc++; |
| 338 | if( x==0 ){ |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 339 | sprintf(zDesc, "*"); /* NULL is a "*" */ |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 340 | }else if( x>=1 && x<=6 ){ |
| 341 | v = (signed char)pData[0]; |
| 342 | pData++; |
| 343 | switch( x ){ |
| 344 | case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
| 345 | case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
| 346 | case 4: v = (v<<8) + pData[0]; pData++; |
| 347 | case 3: v = (v<<8) + pData[0]; pData++; |
| 348 | case 2: v = (v<<8) + pData[0]; pData++; |
| 349 | } |
| 350 | sprintf(zDesc, "%lld", v); |
| 351 | }else if( x==7 ){ |
| 352 | sprintf(zDesc, "real"); |
| 353 | pData += 8; |
| 354 | }else if( x==8 ){ |
| 355 | sprintf(zDesc, "0"); |
| 356 | }else if( x==9 ){ |
| 357 | sprintf(zDesc, "1"); |
| 358 | }else if( x>=12 ){ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 359 | i64 size = (x-12)/2; |
drh | b2c062d | 2011-01-05 21:46:52 +0000 | [diff] [blame] | 360 | if( (x&1)==0 ){ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 361 | sprintf(zDesc, "blob(%lld)", size); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 362 | }else{ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 363 | sprintf(zDesc, "txt(%lld)", size); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 364 | } |
| 365 | pData += size; |
| 366 | } |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame] | 367 | j = (int)strlen(zDesc); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 368 | zDesc += j; |
| 369 | nDesc += j; |
| 370 | } |
| 371 | return nDesc; |
| 372 | } |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | ** Compute the local payload size given the total payload size and |
| 376 | ** the page size. |
| 377 | */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 378 | static i64 localPayload(i64 nPayload, char cType){ |
| 379 | i64 maxLocal; |
| 380 | i64 minLocal; |
| 381 | i64 surplus; |
| 382 | i64 nLocal; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 383 | if( cType==13 ){ |
| 384 | /* Table leaf */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 385 | maxLocal = g.pagesize-35; |
| 386 | minLocal = (g.pagesize-12)*32/255-23; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 387 | }else{ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 388 | maxLocal = (g.pagesize-12)*64/255-23; |
| 389 | minLocal = (g.pagesize-12)*32/255-23; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 390 | } |
| 391 | if( nPayload>maxLocal ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 392 | surplus = minLocal + (nPayload-minLocal)%(g.pagesize-4); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 393 | if( surplus<=maxLocal ){ |
| 394 | nLocal = surplus; |
| 395 | }else{ |
| 396 | nLocal = minLocal; |
| 397 | } |
| 398 | }else{ |
| 399 | nLocal = nPayload; |
| 400 | } |
| 401 | return nLocal; |
| 402 | } |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 403 | |
| 404 | |
| 405 | /* |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 406 | ** Create a description for a single cell. |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 407 | ** |
| 408 | ** The return value is the local cell size. |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 409 | */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 410 | static i64 describeCell( |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 411 | unsigned char cType, /* Page type */ |
| 412 | unsigned char *a, /* Cell content */ |
| 413 | int showCellContent, /* Show cell content if true */ |
| 414 | char **pzDesc /* Store description here */ |
| 415 | ){ |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 416 | int i; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 417 | i64 nDesc = 0; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 418 | int n = 0; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 419 | u32 leftChild; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 420 | i64 nPayload; |
| 421 | i64 rowid; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 422 | i64 nLocal; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 423 | static char zDesc[1000]; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 424 | i = 0; |
| 425 | if( cType<=5 ){ |
| 426 | leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3]; |
| 427 | a += 4; |
| 428 | n += 4; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 429 | sprintf(zDesc, "lx: %u ", leftChild); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 430 | nDesc = strlen(zDesc); |
| 431 | } |
| 432 | if( cType!=5 ){ |
| 433 | i = decodeVarint(a, &nPayload); |
| 434 | a += i; |
| 435 | n += i; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 436 | sprintf(&zDesc[nDesc], "n: %lld ", nPayload); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 437 | nDesc += strlen(&zDesc[nDesc]); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 438 | nLocal = localPayload(nPayload, cType); |
| 439 | }else{ |
| 440 | nPayload = nLocal = 0; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 441 | } |
| 442 | if( cType==5 || cType==13 ){ |
| 443 | i = decodeVarint(a, &rowid); |
| 444 | a += i; |
| 445 | n += i; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 446 | sprintf(&zDesc[nDesc], "r: %lld ", rowid); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 447 | nDesc += strlen(&zDesc[nDesc]); |
| 448 | } |
drh | b7787ee | 2011-01-06 15:51:18 +0000 | [diff] [blame] | 449 | if( nLocal<nPayload ){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 450 | u32 ovfl; |
drh | b7787ee | 2011-01-06 15:51:18 +0000 | [diff] [blame] | 451 | unsigned char *b = &a[nLocal]; |
| 452 | ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3]; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 453 | sprintf(&zDesc[nDesc], "ov: %u ", ovfl); |
drh | b7787ee | 2011-01-06 15:51:18 +0000 | [diff] [blame] | 454 | nDesc += strlen(&zDesc[nDesc]); |
| 455 | n += 4; |
| 456 | } |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 457 | if( showCellContent && cType!=5 ){ |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 458 | nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 459 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 460 | *pzDesc = zDesc; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 461 | return nLocal+n; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 462 | } |
| 463 | |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 464 | /* Print an offset followed by nByte bytes. Add extra white-space |
| 465 | ** at the end so that subsequent text is aligned. |
| 466 | */ |
| 467 | static void printBytes( |
| 468 | unsigned char *aData, /* Content being decoded */ |
| 469 | unsigned char *aStart, /* Start of content to be printed */ |
| 470 | int nByte /* Number of bytes to print */ |
| 471 | ){ |
| 472 | int j; |
| 473 | printf(" %03x: ", (int)(aStart-aData)); |
| 474 | for(j=0; j<9; j++){ |
| 475 | if( j>=nByte ){ |
| 476 | printf(" "); |
| 477 | }else{ |
| 478 | printf("%02x ", aStart[j]); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | |
| 484 | /* |
| 485 | ** Write a full decode on stdout for the cell at a[ofst]. |
| 486 | ** Assume the page contains a header of size szPgHdr bytes. |
| 487 | */ |
| 488 | static void decodeCell( |
| 489 | unsigned char *a, /* Page content (without the page-1 header) */ |
| 490 | unsigned pgno, /* Page number */ |
| 491 | int iCell, /* Cell index */ |
| 492 | int szPgHdr, /* Size of the page header. 0 or 100 */ |
| 493 | int ofst /* Cell begins at a[ofst] */ |
| 494 | ){ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 495 | int i, j = 0; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 496 | u32 leftChild; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 497 | i64 k; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 498 | i64 nPayload; |
| 499 | i64 rowid; |
| 500 | i64 nHdr; |
| 501 | i64 iType; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 502 | i64 nLocal; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 503 | unsigned char *x = a + ofst; |
| 504 | unsigned char *end; |
| 505 | unsigned char cType = a[0]; |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 506 | int nCol = 0; |
| 507 | int szCol[2000]; |
| 508 | int ofstCol[2000]; |
| 509 | int typeCol[2000]; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 510 | |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 511 | printf("Cell[%d]:\n", iCell); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 512 | if( cType<=5 ){ |
| 513 | leftChild = ((x[0]*256 + x[1])*256 + x[2])*256 + x[3]; |
| 514 | printBytes(a, x, 4); |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 515 | printf("left child page:: %u\n", leftChild); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 516 | x += 4; |
| 517 | } |
| 518 | if( cType!=5 ){ |
| 519 | i = decodeVarint(x, &nPayload); |
| 520 | printBytes(a, x, i); |
| 521 | nLocal = localPayload(nPayload, cType); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 522 | if( nLocal==nPayload ){ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 523 | printf("payload-size: %lld\n", nPayload); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 524 | }else{ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 525 | printf("payload-size: %lld (%lld local, %lld overflow)\n", |
| 526 | nPayload, nLocal, nPayload-nLocal); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 527 | } |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 528 | x += i; |
| 529 | }else{ |
| 530 | nPayload = nLocal = 0; |
| 531 | } |
| 532 | end = x + nLocal; |
| 533 | if( cType==5 || cType==13 ){ |
| 534 | i = decodeVarint(x, &rowid); |
| 535 | printBytes(a, x, i); |
| 536 | printf("rowid: %lld\n", rowid); |
| 537 | x += i; |
| 538 | } |
| 539 | if( nLocal>0 ){ |
| 540 | i = decodeVarint(x, &nHdr); |
| 541 | printBytes(a, x, i); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 542 | printf("record-header-size: %d\n", (int)nHdr); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 543 | j = i; |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 544 | nCol = 0; |
| 545 | k = nHdr; |
drh | 970ea37 | 2017-03-16 13:14:03 +0000 | [diff] [blame] | 546 | while( x+j<=end && j<nHdr ){ |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 547 | const char *zTypeName; |
| 548 | int sz = 0; |
| 549 | char zNm[30]; |
| 550 | i = decodeVarint(x+j, &iType); |
| 551 | printBytes(a, x+j, i); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 552 | printf("typecode[%d]: %d - ", nCol, (int)iType); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 553 | switch( iType ){ |
| 554 | case 0: zTypeName = "NULL"; sz = 0; break; |
| 555 | case 1: zTypeName = "int8"; sz = 1; break; |
| 556 | case 2: zTypeName = "int16"; sz = 2; break; |
| 557 | case 3: zTypeName = "int24"; sz = 3; break; |
| 558 | case 4: zTypeName = "int32"; sz = 4; break; |
| 559 | case 5: zTypeName = "int48"; sz = 6; break; |
| 560 | case 6: zTypeName = "int64"; sz = 8; break; |
| 561 | case 7: zTypeName = "double"; sz = 8; break; |
| 562 | case 8: zTypeName = "zero"; sz = 0; break; |
| 563 | case 9: zTypeName = "one"; sz = 0; break; |
| 564 | case 10: |
| 565 | case 11: zTypeName = "error"; sz = 0; break; |
| 566 | default: { |
| 567 | sz = (int)(iType-12)/2; |
| 568 | sprintf(zNm, (iType&1)==0 ? "blob(%d)" : "text(%d)", sz); |
| 569 | zTypeName = zNm; |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | printf("%s\n", zTypeName); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 574 | szCol[nCol] = sz; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 575 | ofstCol[nCol] = (int)k; |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 576 | typeCol[nCol] = (int)iType; |
| 577 | k += sz; |
| 578 | nCol++; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 579 | j += i; |
| 580 | } |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 581 | for(i=0; i<nCol && ofstCol[i]+szCol[i]<=nLocal; i++){ |
| 582 | int s = ofstCol[i]; |
| 583 | i64 v; |
| 584 | const unsigned char *pData; |
| 585 | if( szCol[i]==0 ) continue; |
| 586 | printBytes(a, x+s, szCol[i]); |
| 587 | printf("data[%d]: ", i); |
| 588 | pData = x+s; |
| 589 | if( typeCol[i]<=7 ){ |
| 590 | v = (signed char)pData[0]; |
| 591 | for(k=1; k<szCol[i]; k++){ |
| 592 | v = (v<<8) + pData[k]; |
| 593 | } |
| 594 | if( typeCol[i]==7 ){ |
| 595 | double r; |
| 596 | memcpy(&r, &v, sizeof(r)); |
| 597 | printf("%#g\n", r); |
| 598 | }else{ |
| 599 | printf("%lld\n", v); |
| 600 | } |
| 601 | }else{ |
drh | 56e67dd | 2014-06-20 13:55:06 +0000 | [diff] [blame] | 602 | int ii, jj; |
| 603 | char zConst[32]; |
| 604 | if( (typeCol[i]&1)==0 ){ |
| 605 | zConst[0] = 'x'; |
| 606 | zConst[1] = '\''; |
| 607 | for(ii=2, jj=0; jj<szCol[i] && ii<24; jj++, ii+=2){ |
| 608 | sprintf(zConst+ii, "%02x", pData[jj]); |
| 609 | } |
| 610 | }else{ |
| 611 | zConst[0] = '\''; |
| 612 | for(ii=1, jj=0; jj<szCol[i] && ii<24; jj++, ii++){ |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 613 | zConst[ii] = ISPRINT(pData[jj]) ? pData[jj] : '.'; |
drh | 56e67dd | 2014-06-20 13:55:06 +0000 | [diff] [blame] | 614 | } |
| 615 | zConst[ii] = 0; |
| 616 | } |
| 617 | if( jj<szCol[i] ){ |
| 618 | memcpy(zConst+ii, "...'", 5); |
| 619 | }else{ |
| 620 | memcpy(zConst+ii, "'", 2); |
| 621 | } |
| 622 | printf("%s\n", zConst); |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 623 | } |
| 624 | j = ofstCol[i] + szCol[i]; |
| 625 | } |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 626 | } |
| 627 | if( j<nLocal ){ |
| 628 | printBytes(a, x+j, 0); |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 629 | printf("... %lld bytes of content ...\n", nLocal-j); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 630 | } |
| 631 | if( nLocal<nPayload ){ |
| 632 | printBytes(a, x+nLocal, 4); |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 633 | printf("overflow-page: %u\n", decodeInt32(x+nLocal)); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 638 | /* |
| 639 | ** Decode a btree page |
| 640 | */ |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 641 | static void decode_btree_page( |
| 642 | unsigned char *a, /* Page content */ |
| 643 | int pgno, /* Page number */ |
| 644 | int hdrSize, /* Size of the page header. 0 or 100 */ |
| 645 | char *zArgs /* Flags to control formatting */ |
| 646 | ){ |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 647 | const char *zType = "unknown"; |
| 648 | int nCell; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 649 | int i, j; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 650 | int iCellPtr; |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 651 | int showCellContent = 0; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 652 | int showMap = 0; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 653 | int cellToDecode = -2; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 654 | char *zMap = 0; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 655 | switch( a[0] ){ |
| 656 | case 2: zType = "index interior node"; break; |
| 657 | case 5: zType = "table interior node"; break; |
| 658 | case 10: zType = "index leaf"; break; |
| 659 | case 13: zType = "table leaf"; break; |
| 660 | } |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 661 | while( zArgs[0] ){ |
| 662 | switch( zArgs[0] ){ |
| 663 | case 'c': showCellContent = 1; break; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 664 | case 'm': showMap = 1; break; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 665 | case 'd': { |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 666 | if( !ISDIGIT(zArgs[1]) ){ |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 667 | cellToDecode = -1; |
| 668 | }else{ |
| 669 | cellToDecode = 0; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 670 | while( ISDIGIT(zArgs[1]) ){ |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 671 | zArgs++; |
| 672 | cellToDecode = cellToDecode*10 + zArgs[0] - '0'; |
| 673 | } |
| 674 | } |
| 675 | break; |
| 676 | } |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 677 | } |
| 678 | zArgs++; |
| 679 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 680 | nCell = a[3]*256 + a[4]; |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 681 | iCellPtr = (a[0]==2 || a[0]==5) ? 12 : 8; |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 682 | if( cellToDecode>=nCell ){ |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 683 | printf("Page %d has only %d cells\n", pgno, nCell); |
| 684 | return; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 685 | } |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 686 | printf("Header on btree page %d:\n", pgno); |
| 687 | print_decode_line(a, 0, 1, zType); |
| 688 | print_decode_line(a, 1, 2, "Offset to first freeblock"); |
| 689 | print_decode_line(a, 3, 2, "Number of cells on this page"); |
| 690 | print_decode_line(a, 5, 2, "Offset to cell content area"); |
| 691 | print_decode_line(a, 7, 1, "Fragmented byte count"); |
| 692 | if( a[0]==2 || a[0]==5 ){ |
| 693 | print_decode_line(a, 8, 4, "Right child"); |
| 694 | } |
| 695 | if( cellToDecode==(-2) && nCell>0 ){ |
| 696 | printf(" key: lx=left-child n=payload-size r=rowid\n"); |
| 697 | } |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 698 | if( showMap ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 699 | zMap = sqlite3_malloc(g.pagesize); |
| 700 | memset(zMap, '.', g.pagesize); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 701 | memset(zMap, '1', hdrSize); |
| 702 | memset(&zMap[hdrSize], 'H', iCellPtr); |
| 703 | memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell); |
| 704 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 705 | for(i=0; i<nCell; i++){ |
| 706 | int cofst = iCellPtr + i*2; |
| 707 | char *zDesc; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 708 | i64 n; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 709 | |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 710 | cofst = a[cofst]*256 + a[cofst+1]; |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 711 | n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc); |
| 712 | if( showMap ){ |
| 713 | char zBuf[30]; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 714 | memset(&zMap[cofst], '*', (size_t)n); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 715 | zMap[cofst] = '['; |
| 716 | zMap[cofst+n-1] = ']'; |
| 717 | sprintf(zBuf, "%d", i); |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame] | 718 | j = (int)strlen(zBuf); |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 719 | if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j); |
| 720 | } |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 721 | if( cellToDecode==(-2) ){ |
| 722 | printf(" %03x: cell[%d] %s\n", cofst, i, zDesc); |
| 723 | }else if( cellToDecode==(-1) || cellToDecode==i ){ |
| 724 | decodeCell(a, pgno, i, hdrSize, cofst-hdrSize); |
| 725 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 726 | } |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 727 | if( showMap ){ |
drh | 419e040 | 2014-06-20 01:32:42 +0000 | [diff] [blame] | 728 | printf("Page map: (H=header P=cell-index 1=page-1-header .=free-space)\n"); |
mistachkin | 2b5fbb2 | 2021-12-31 18:26:50 +0000 | [diff] [blame] | 729 | for(i=0; (u32)i<g.pagesize; i+=64){ |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 730 | printf(" %03x: %.64s\n", i, &zMap[i]); |
| 731 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 732 | sqlite3_free(zMap); |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 733 | } |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 734 | } |
| 735 | |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 736 | /* |
| 737 | ** Decode a freelist trunk page. |
| 738 | */ |
| 739 | static void decode_trunk_page( |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 740 | u32 pgno, /* The page number */ |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 741 | int detail, /* Show leaf pages if true */ |
| 742 | int recursive /* Follow the trunk change if true */ |
| 743 | ){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 744 | u32 i; |
| 745 | u32 n; |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 746 | unsigned char *a; |
| 747 | while( pgno>0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 748 | a = fileRead((pgno-1)*g.pagesize, g.pagesize); |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 749 | printf("Decode of freelist trunk page %d:\n", pgno); |
| 750 | print_decode_line(a, 0, 4, "Next freelist trunk page"); |
| 751 | print_decode_line(a, 4, 4, "Number of entries on this page"); |
| 752 | if( detail ){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 753 | n = decodeInt32(&a[4]); |
| 754 | for(i=0; i<n && i<g.pagesize/4; i++){ |
| 755 | u32 x = decodeInt32(&a[8+4*i]); |
larrybr | d95a652 | 2021-06-16 13:26:40 +0000 | [diff] [blame] | 756 | char zIdx[13]; |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 757 | sprintf(zIdx, "[%d]", i); |
| 758 | printf(" %5s %7u", zIdx, x); |
| 759 | if( i%5==4 ) printf("\n"); |
| 760 | } |
| 761 | if( i%5!=0 ) printf("\n"); |
| 762 | } |
| 763 | if( !recursive ){ |
| 764 | pgno = 0; |
| 765 | }else{ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 766 | pgno = decodeInt32(&a[0]); |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 767 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 768 | sqlite3_free(a); |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
| 772 | /* |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 773 | ** A short text comment on the use of each page. |
| 774 | */ |
| 775 | static char **zPageUse; |
| 776 | |
| 777 | /* |
| 778 | ** Add a comment on the use of a page. |
| 779 | */ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 780 | static void page_usage_msg(u32 pgno, const char *zFormat, ...){ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 781 | va_list ap; |
| 782 | char *zMsg; |
| 783 | |
| 784 | va_start(ap, zFormat); |
| 785 | zMsg = sqlite3_vmprintf(zFormat, ap); |
| 786 | va_end(ap); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 787 | if( pgno<=0 || pgno>g.mxPage ){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 788 | printf("ERROR: page %d out of range 1..%u: %s\n", |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 789 | pgno, g.mxPage, zMsg); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 790 | sqlite3_free(zMsg); |
| 791 | return; |
| 792 | } |
| 793 | if( zPageUse[pgno]!=0 ){ |
| 794 | printf("ERROR: page %d used multiple times:\n", pgno); |
| 795 | printf("ERROR: previous: %s\n", zPageUse[pgno]); |
drh | 103a70f | 2013-02-19 20:25:16 +0000 | [diff] [blame] | 796 | printf("ERROR: current: %s\n", zMsg); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 797 | sqlite3_free(zPageUse[pgno]); |
| 798 | } |
| 799 | zPageUse[pgno] = zMsg; |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | ** Find overflow pages of a cell and describe their usage. |
| 804 | */ |
| 805 | static void page_usage_cell( |
| 806 | unsigned char cType, /* Page type */ |
| 807 | unsigned char *a, /* Cell content */ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 808 | u32 pgno, /* page containing the cell */ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 809 | int cellno /* Index of the cell on the page */ |
| 810 | ){ |
| 811 | int i; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 812 | int n = 0; |
| 813 | i64 nPayload; |
| 814 | i64 rowid; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 815 | i64 nLocal; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 816 | i = 0; |
| 817 | if( cType<=5 ){ |
| 818 | a += 4; |
| 819 | n += 4; |
| 820 | } |
| 821 | if( cType!=5 ){ |
| 822 | i = decodeVarint(a, &nPayload); |
| 823 | a += i; |
| 824 | n += i; |
| 825 | nLocal = localPayload(nPayload, cType); |
| 826 | }else{ |
| 827 | nPayload = nLocal = 0; |
| 828 | } |
| 829 | if( cType==5 || cType==13 ){ |
| 830 | i = decodeVarint(a, &rowid); |
| 831 | a += i; |
| 832 | n += i; |
| 833 | } |
| 834 | if( nLocal<nPayload ){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 835 | u32 ovfl = decodeInt32(a+nLocal); |
| 836 | u32 cnt = 0; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 837 | while( ovfl && (cnt++)<g.mxPage ){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 838 | page_usage_msg(ovfl, "overflow %d from cell %d of page %u", |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 839 | cnt, cellno, pgno); |
drh | aaad696 | 2019-03-05 23:49:17 +0000 | [diff] [blame] | 840 | a = fileRead((ovfl-1)*(sqlite3_int64)g.pagesize, 4); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 841 | ovfl = decodeInt32(a); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 842 | sqlite3_free(a); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
drh | 8083ef0 | 2019-04-17 12:29:45 +0000 | [diff] [blame] | 847 | /* |
| 848 | ** True if the memory is all zeros |
| 849 | */ |
| 850 | static int allZero(unsigned char *a, int n){ |
| 851 | while( n && (a++)[0]==0 ){ n--; } |
| 852 | return n==0; |
| 853 | } |
| 854 | |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 855 | |
| 856 | /* |
drh | 8083ef0 | 2019-04-17 12:29:45 +0000 | [diff] [blame] | 857 | ** Describe the usages of a b-tree page. |
| 858 | ** |
| 859 | ** If parent==0, then this is the root of a btree. If parent<0 then |
| 860 | ** this is an orphan page. |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 861 | */ |
| 862 | static void page_usage_btree( |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 863 | u32 pgno, /* Page to describe */ |
mistachkin | 2b5fbb2 | 2021-12-31 18:26:50 +0000 | [diff] [blame] | 864 | int parent, /* Parent of this page. 0 for root pages */ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 865 | int idx, /* Which child of the parent */ |
| 866 | const char *zName /* Name of the table */ |
| 867 | ){ |
| 868 | unsigned char *a; |
| 869 | const char *zType = "corrupt node"; |
| 870 | int nCell; |
| 871 | int i; |
| 872 | int hdr = pgno==1 ? 100 : 0; |
drh | 25f933a | 2019-04-17 13:23:28 +0000 | [diff] [blame] | 873 | char zEntry[30]; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 874 | |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 875 | if( pgno<=0 || pgno>g.mxPage ) return; |
| 876 | a = fileRead((pgno-1)*g.pagesize, g.pagesize); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 877 | switch( a[hdr] ){ |
drh | 8083ef0 | 2019-04-17 12:29:45 +0000 | [diff] [blame] | 878 | case 0: { |
| 879 | if( allZero(a, g.pagesize) ){ |
| 880 | zType = "zeroed page"; |
| 881 | }else if( parent<0 ){ |
| 882 | return; |
| 883 | }else{ |
| 884 | zType = "corrupt node"; |
| 885 | } |
| 886 | break; |
| 887 | } |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 888 | case 2: zType = "interior node of index"; break; |
| 889 | case 5: zType = "interior node of table"; break; |
| 890 | case 10: zType = "leaf of index"; break; |
| 891 | case 13: zType = "leaf of table"; break; |
drh | 8083ef0 | 2019-04-17 12:29:45 +0000 | [diff] [blame] | 892 | default: { |
| 893 | if( parent<0 ) return; |
| 894 | zType = "corrupt node"; |
| 895 | } |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 896 | } |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 897 | nCell = a[hdr+3]*256 + a[hdr+4]; |
drh | 25f933a | 2019-04-17 13:23:28 +0000 | [diff] [blame] | 898 | if( nCell==1 ){ |
| 899 | sqlite3_snprintf(sizeof(zEntry),zEntry,"1 row"); |
| 900 | }else{ |
| 901 | sqlite3_snprintf(sizeof(zEntry),zEntry,"%d rows", nCell); |
| 902 | } |
| 903 | if( parent>0 ){ |
| 904 | page_usage_msg(pgno, "%s [%s], child %d of page %d, %s", |
| 905 | zType, zName, idx, parent, zEntry); |
| 906 | }else if( parent==0 ){ |
| 907 | page_usage_msg(pgno, "root %s [%s], %s", zType, zName, zEntry); |
| 908 | }else{ |
| 909 | page_usage_msg(pgno, "orphaned %s, %s", zType, zEntry); |
| 910 | } |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 911 | if( a[hdr]==2 || a[hdr]==5 ){ |
| 912 | int cellstart = hdr+12; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 913 | u32 child; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 914 | for(i=0; i<nCell; i++){ |
drh | 19a7f72 | 2021-04-26 23:57:02 +0000 | [diff] [blame] | 915 | u32 cellidx; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 916 | u32 ofst; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 917 | |
drh | 19a7f72 | 2021-04-26 23:57:02 +0000 | [diff] [blame] | 918 | cellidx = cellstart + i*2; |
| 919 | if( cellidx+1 >= g.pagesize ){ |
drh | 2f40dc9 | 2021-04-27 00:05:03 +0000 | [diff] [blame] | 920 | printf("ERROR: page %d too many cells (%d)\n", pgno, nCell); |
drh | 19a7f72 | 2021-04-26 23:57:02 +0000 | [diff] [blame] | 921 | break; |
| 922 | } |
| 923 | ofst = a[cellidx]*256 + a[cellidx+1]; |
| 924 | if( ofst<cellidx+2 || ofst+4>=g.pagesize ){ |
drh | 2f40dc9 | 2021-04-27 00:05:03 +0000 | [diff] [blame] | 925 | printf("ERROR: page %d cell %d out of bounds\n", pgno, i); |
drh | 19a7f72 | 2021-04-26 23:57:02 +0000 | [diff] [blame] | 926 | continue; |
| 927 | } |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 928 | child = decodeInt32(a+ofst); |
| 929 | page_usage_btree(child, pgno, i, zName); |
| 930 | } |
| 931 | child = decodeInt32(a+cellstart-4); |
| 932 | page_usage_btree(child, pgno, i, zName); |
| 933 | } |
| 934 | if( a[hdr]==2 || a[hdr]==10 || a[hdr]==13 ){ |
| 935 | int cellstart = hdr + 8 + 4*(a[hdr]<=5); |
| 936 | for(i=0; i<nCell; i++){ |
| 937 | int ofst; |
| 938 | ofst = cellstart + i*2; |
| 939 | ofst = a[ofst]*256 + a[ofst+1]; |
| 940 | page_usage_cell(a[hdr], a+ofst, pgno, i); |
| 941 | } |
| 942 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 943 | sqlite3_free(a); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /* |
| 947 | ** Determine page usage by the freelist |
| 948 | */ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 949 | static void page_usage_freelist(u32 pgno){ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 950 | unsigned char *a; |
| 951 | int cnt = 0; |
| 952 | int i; |
| 953 | int n; |
| 954 | int iNext; |
| 955 | int parent = 1; |
| 956 | |
mistachkin | 2b5fbb2 | 2021-12-31 18:26:50 +0000 | [diff] [blame] | 957 | while( pgno>0 && pgno<=g.mxPage && (u32)(cnt++)<g.mxPage ){ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 958 | page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 959 | a = fileRead((pgno-1)*g.pagesize, g.pagesize); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 960 | iNext = decodeInt32(a); |
| 961 | n = decodeInt32(a+4); |
| 962 | for(i=0; i<n; i++){ |
| 963 | int child = decodeInt32(a + (i*4+8)); |
| 964 | page_usage_msg(child, "freelist leaf, child %d of trunk page %d", |
| 965 | i, pgno); |
| 966 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 967 | sqlite3_free(a); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 968 | parent = pgno; |
| 969 | pgno = iNext; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /* |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 974 | ** Determine pages used as PTRMAP pages |
| 975 | */ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 976 | static void page_usage_ptrmap(u8 *a){ |
drh | c1f73e2 | 2020-01-14 13:24:14 +0000 | [diff] [blame] | 977 | if( decodeInt32(a+52) ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 978 | int usable = g.pagesize - a[20]; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 979 | u64 pgno = 2; |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 980 | int perPage = usable/5; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 981 | while( pgno<=g.mxPage ){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 982 | page_usage_msg((u32)pgno, "PTRMAP page covering %llu..%llu", |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 983 | pgno+1, pgno+perPage); |
| 984 | pgno += perPage + 1; |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | /* |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 990 | ** Try to figure out how every page in the database file is being used. |
| 991 | */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 992 | static void page_usage_report(const char *zPrg, const char *zDbName){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 993 | u32 i, j; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 994 | int rc; |
| 995 | sqlite3 *db; |
| 996 | sqlite3_stmt *pStmt; |
| 997 | unsigned char *a; |
drh | 00e637f | 2013-02-19 18:45:11 +0000 | [diff] [blame] | 998 | char zQuery[200]; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 999 | |
| 1000 | /* Avoid the pathological case */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1001 | if( g.mxPage<1 ){ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1002 | printf("empty database\n"); |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | /* Open the database file */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1007 | db = openDatabase(zPrg, zDbName); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1008 | |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1009 | /* Set up global variables zPageUse[] and g.mxPage to record page |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1010 | ** usages */ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1011 | zPageUse = sqlite3_malloc64( sizeof(zPageUse[0])*(g.mxPage+1) ); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1012 | if( zPageUse==0 ) out_of_memory(); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1013 | memset(zPageUse, 0, sizeof(zPageUse[0])*(g.mxPage+1)); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1014 | |
| 1015 | /* Discover the usage of each page */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1016 | a = fileRead(0, 100); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1017 | page_usage_freelist(decodeInt32(a+32)); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1018 | page_usage_ptrmap(a); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1019 | sqlite3_free(a); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 1020 | page_usage_btree(1, 0, 0, "sqlite_schema"); |
drh | 00e637f | 2013-02-19 18:45:11 +0000 | [diff] [blame] | 1021 | sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0); |
| 1022 | for(j=0; j<2; j++){ |
| 1023 | sqlite3_snprintf(sizeof(zQuery), zQuery, |
| 1024 | "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage" |
| 1025 | " ORDER BY rowid %s", j?"DESC":""); |
| 1026 | rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0); |
| 1027 | if( rc==SQLITE_OK ){ |
| 1028 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1029 | u32 pgno = (u32)sqlite3_column_int64(pStmt, 2); |
drh | db718d8 | 2014-01-28 20:36:22 +0000 | [diff] [blame] | 1030 | page_usage_btree(pgno, 0, 0, (const char*)sqlite3_column_text(pStmt,1)); |
drh | 00e637f | 2013-02-19 18:45:11 +0000 | [diff] [blame] | 1031 | } |
| 1032 | }else{ |
| 1033 | printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db)); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1034 | } |
drh | 00e637f | 2013-02-19 18:45:11 +0000 | [diff] [blame] | 1035 | rc = sqlite3_finalize(pStmt); |
| 1036 | if( rc==SQLITE_OK ) break; |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1037 | } |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1038 | sqlite3_close(db); |
| 1039 | |
| 1040 | /* Print the report and free memory used */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1041 | for(i=1; i<=g.mxPage; i++){ |
drh | 8083ef0 | 2019-04-17 12:29:45 +0000 | [diff] [blame] | 1042 | if( zPageUse[i]==0 ) page_usage_btree(i, -1, 0, 0); |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1043 | printf("%5u: %s\n", i, zPageUse[i] ? zPageUse[i] : "???"); |
drh | 26ec568 | 2021-06-08 17:56:16 +0000 | [diff] [blame] | 1044 | } |
| 1045 | for(i=1; i<=g.mxPage; i++){ |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1046 | sqlite3_free(zPageUse[i]); |
| 1047 | } |
| 1048 | sqlite3_free(zPageUse); |
| 1049 | zPageUse = 0; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1053 | ** Try to figure out how every page in the database file is being used. |
| 1054 | */ |
| 1055 | static void ptrmap_coverage_report(const char *zDbName){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1056 | u64 pgno; |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1057 | unsigned char *aHdr; |
| 1058 | unsigned char *a; |
| 1059 | int usable; |
| 1060 | int perPage; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 1061 | int i; |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1062 | |
| 1063 | /* Avoid the pathological case */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1064 | if( g.mxPage<1 ){ |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1065 | printf("empty database\n"); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | /* Make sure PTRMAPs are used in this database */ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1070 | aHdr = fileRead(0, 100); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1071 | if( aHdr[55]==0 ){ |
| 1072 | printf("database does not use PTRMAP pages\n"); |
| 1073 | return; |
| 1074 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1075 | usable = g.pagesize - aHdr[20]; |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1076 | perPage = usable/5; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1077 | sqlite3_free(aHdr); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 1078 | printf("%5d: root of sqlite_schema\n", 1); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1079 | for(pgno=2; pgno<=g.mxPage; pgno += perPage+1){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1080 | printf("%5llu: PTRMAP page covering %llu..%llu\n", pgno, |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1081 | pgno+1, pgno+perPage); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1082 | a = fileRead((pgno-1)*g.pagesize, usable); |
| 1083 | 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] | 1084 | const char *zType = "???"; |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1085 | u32 iFrom = decodeInt32(&a[i+1]); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1086 | switch( a[i] ){ |
| 1087 | case 1: zType = "b-tree root page"; break; |
| 1088 | case 2: zType = "freelist page"; break; |
| 1089 | case 3: zType = "first page of overflow"; break; |
| 1090 | case 4: zType = "later page of overflow"; break; |
| 1091 | case 5: zType = "b-tree non-root page"; break; |
| 1092 | } |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1093 | printf("%5llu: %s, parent=%u\n", pgno+1+i/5, zType, iFrom); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1094 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1095 | sqlite3_free(a); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | /* |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1100 | ** Print a usage comment |
| 1101 | */ |
| 1102 | static void usage(const char *argv0){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1103 | fprintf(stderr, "Usage %s ?--uri? FILENAME ?args...?\n\n", argv0); |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1104 | fprintf(stderr, |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1105 | "switches:\n" |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1106 | " --raw Read db file directly, bypassing SQLite VFS\n" |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1107 | "args:\n" |
| 1108 | " dbheader Show database header\n" |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1109 | " pgidx Index of how each page is used\n" |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1110 | " ptrmap Show all PTRMAP page content\n" |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1111 | " NNN..MMM Show hex of pages NNN through MMM\n" |
| 1112 | " NNN..end Show hex of pages NNN through end of file\n" |
| 1113 | " NNNb Decode btree page NNN\n" |
drh | 5240aeb | 2011-01-06 01:26:38 +0000 | [diff] [blame] | 1114 | " NNNbc Decode btree page NNN and show content\n" |
| 1115 | " NNNbm Decode btree page NNN and show a layout map\n" |
drh | 6a30cd5 | 2014-06-19 23:38:53 +0000 | [diff] [blame] | 1116 | " NNNbdCCC Decode cell CCC on btree page NNN\n" |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1117 | " NNNt Decode freelist trunk page NNN\n" |
drh | 47fb000 | 2011-04-13 16:52:41 +0000 | [diff] [blame] | 1118 | " NNNtd Show leaf freelist pages on the decode\n" |
dan | 53d89cd | 2014-08-20 10:42:16 +0000 | [diff] [blame] | 1119 | " NNNtr Recursively decode freelist starting at NNN\n" |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1120 | ); |
| 1121 | } |
| 1122 | |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1123 | int main(int argc, char **argv){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1124 | sqlite3_int64 szFile; |
| 1125 | unsigned char *zPgSz; |
| 1126 | const char *zPrg = argv[0]; /* Name of this executable */ |
| 1127 | char **azArg = argv; |
| 1128 | int nArg = argc; |
| 1129 | |
| 1130 | /* Check for the "--uri" or "-uri" switch. */ |
| 1131 | if( nArg>1 ){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1132 | if( sqlite3_stricmp("-raw", azArg[1])==0 |
| 1133 | || sqlite3_stricmp("--raw", azArg[1])==0 |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1134 | ){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1135 | g.bRaw = 1; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1136 | azArg++; |
| 1137 | nArg--; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | if( nArg<2 ){ |
| 1142 | usage(zPrg); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1143 | exit(1); |
| 1144 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1145 | |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1146 | fileOpen(zPrg, azArg[1]); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1147 | szFile = fileGetsize(); |
| 1148 | |
| 1149 | zPgSz = fileRead(16, 2); |
| 1150 | g.pagesize = zPgSz[0]*256 + zPgSz[1]*65536; |
| 1151 | if( g.pagesize==0 ) g.pagesize = 1024; |
| 1152 | sqlite3_free(zPgSz); |
| 1153 | |
| 1154 | printf("Pagesize: %d\n", g.pagesize); |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1155 | g.mxPage = (u32)((szFile+g.pagesize-1)/g.pagesize); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1156 | |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1157 | printf("Available pages: 1..%u\n", g.mxPage); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1158 | if( nArg==2 ){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1159 | u32 i; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1160 | for(i=1; i<=g.mxPage; i++) print_page(i); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1161 | }else{ |
| 1162 | int i; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1163 | for(i=2; i<nArg; i++){ |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1164 | u32 iStart, iEnd; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1165 | char *zLeft; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1166 | if( strcmp(azArg[i], "dbheader")==0 ){ |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 1167 | print_db_header(); |
| 1168 | continue; |
| 1169 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1170 | if( strcmp(azArg[i], "pgidx")==0 ){ |
| 1171 | page_usage_report(zPrg, azArg[1]); |
drh | 3aeea46 | 2012-04-03 14:59:50 +0000 | [diff] [blame] | 1172 | continue; |
| 1173 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1174 | if( strcmp(azArg[i], "ptrmap")==0 ){ |
| 1175 | ptrmap_coverage_report(azArg[1]); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1176 | continue; |
| 1177 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1178 | if( strcmp(azArg[i], "help")==0 ){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1179 | usage(zPrg); |
drh | 344a97b | 2013-02-19 22:26:51 +0000 | [diff] [blame] | 1180 | continue; |
| 1181 | } |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 1182 | if( !ISDIGIT(azArg[i][0]) ){ |
dan | 8fb1bd2 | 2015-08-04 15:23:49 +0000 | [diff] [blame] | 1183 | fprintf(stderr, "%s: unknown option: [%s]\n", zPrg, azArg[i]); |
drh | 562cedb | 2010-04-26 15:44:07 +0000 | [diff] [blame] | 1184 | continue; |
| 1185 | } |
drh | 55550b7 | 2020-07-22 11:42:50 +0000 | [diff] [blame] | 1186 | iStart = strtoul(azArg[i], &zLeft, 0); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1187 | if( zLeft && strcmp(zLeft,"..end")==0 ){ |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1188 | iEnd = g.mxPage; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1189 | }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ |
| 1190 | iEnd = strtol(&zLeft[2], 0, 0); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 1191 | }else if( zLeft && zLeft[0]=='b' ){ |
| 1192 | int ofst, nByte, hdrSize; |
| 1193 | unsigned char *a; |
| 1194 | if( iStart==1 ){ |
| 1195 | ofst = hdrSize = 100; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1196 | nByte = g.pagesize-100; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 1197 | }else{ |
| 1198 | hdrSize = 0; |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1199 | ofst = (iStart-1)*g.pagesize; |
| 1200 | nByte = g.pagesize; |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 1201 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1202 | a = fileRead(ofst, nByte); |
drh | 100335b | 2011-01-05 21:20:52 +0000 | [diff] [blame] | 1203 | decode_btree_page(a, iStart, hdrSize, &zLeft[1]); |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1204 | sqlite3_free(a); |
drh | 7ecc147 | 2010-04-26 16:47:12 +0000 | [diff] [blame] | 1205 | continue; |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1206 | }else if( zLeft && zLeft[0]=='t' ){ |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1207 | int detail = 0; |
| 1208 | int recursive = 0; |
mistachkin | 8ccdef6 | 2015-12-16 22:06:52 +0000 | [diff] [blame] | 1209 | int j; |
| 1210 | for(j=1; zLeft[j]; j++){ |
| 1211 | if( zLeft[j]=='r' ) recursive = 1; |
| 1212 | if( zLeft[j]=='d' ) detail = 1; |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1213 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1214 | decode_trunk_page(iStart, detail, recursive); |
drh | 7d105f8 | 2010-08-23 15:26:49 +0000 | [diff] [blame] | 1215 | continue; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1216 | }else{ |
| 1217 | iEnd = iStart; |
| 1218 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1219 | if( iStart<1 || iEnd<iStart || iEnd>g.mxPage ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1220 | fprintf(stderr, |
| 1221 | "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1222 | g.mxPage); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1223 | exit(1); |
| 1224 | } |
| 1225 | while( iStart<=iEnd ){ |
| 1226 | print_page(iStart); |
| 1227 | iStart++; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
dan | 871f6e3 | 2015-08-03 17:03:31 +0000 | [diff] [blame] | 1231 | fileClose(); |
drh | db718d8 | 2014-01-28 20:36:22 +0000 | [diff] [blame] | 1232 | return 0; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1233 | } |