drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** A utility for printing content from a write-ahead log 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 | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 9 | |
| 10 | #if !defined(_MSC_VER) |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 11 | #include <unistd.h> |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 12 | #else |
| 13 | #include <io.h> |
| 14 | #endif |
| 15 | |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | |
| 20 | static int pagesize = 1024; /* Size of a database page */ |
| 21 | static int fd = -1; /* File descriptor for reading the WAL file */ |
| 22 | static int mxFrame = 0; /* Last frame */ |
| 23 | static int perLine = 16; /* HEX elements to print per line */ |
| 24 | |
| 25 | typedef long long int i64; /* Datatype for 64-bit integers */ |
| 26 | |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 27 | /* Information for computing the checksum */ |
| 28 | typedef struct Cksum Cksum; |
| 29 | struct Cksum { |
| 30 | int bSwap; /* True to do byte swapping on 32-bit words */ |
| 31 | unsigned s0, s1; /* Current checksum value */ |
| 32 | }; |
| 33 | |
| 34 | /* |
| 35 | ** extract a 32-bit big-endian integer |
| 36 | */ |
| 37 | static unsigned int getInt32(const unsigned char *a){ |
| 38 | unsigned int x = (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 39 | return x; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | ** Swap bytes on a 32-bit unsigned integer |
| 44 | */ |
| 45 | static unsigned int swab32(unsigned int x){ |
| 46 | return (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) |
| 47 | + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24); |
| 48 | } |
| 49 | |
| 50 | /* Extend the checksum. Reinitialize the checksum if bInit is true. |
| 51 | */ |
| 52 | static void extendCksum( |
| 53 | Cksum *pCksum, |
| 54 | unsigned char *aData, |
| 55 | unsigned int nByte, |
| 56 | int bInit |
| 57 | ){ |
| 58 | unsigned int *a32; |
| 59 | if( bInit ){ |
| 60 | int a = 0; |
| 61 | *((char*)&a) = 1; |
| 62 | if( a==1 ){ |
| 63 | /* Host is little-endian */ |
| 64 | pCksum->bSwap = getInt32(aData)!=0x377f0682; |
| 65 | }else{ |
| 66 | /* Host is big-endian */ |
| 67 | pCksum->bSwap = getInt32(aData)!=0x377f0683; |
| 68 | } |
| 69 | pCksum->s0 = 0; |
| 70 | pCksum->s1 = 0; |
| 71 | } |
| 72 | a32 = (unsigned int*)aData; |
| 73 | while( nByte>0 ){ |
| 74 | unsigned int x0 = a32[0]; |
| 75 | unsigned int x1 = a32[1]; |
| 76 | if( pCksum->bSwap ){ |
| 77 | x0 = swab32(x0); |
| 78 | x1 = swab32(x1); |
| 79 | } |
| 80 | pCksum->s0 += x0 + pCksum->s1; |
| 81 | pCksum->s1 += x1 + pCksum->s0; |
| 82 | nByte -= 8; |
| 83 | a32 += 2; |
| 84 | } |
| 85 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | ** Convert the var-int format into i64. Return the number of bytes |
| 89 | ** in the var-int. Write the var-int value into *pVal. |
| 90 | */ |
| 91 | static int decodeVarint(const unsigned char *z, i64 *pVal){ |
| 92 | i64 v = 0; |
| 93 | int i; |
| 94 | for(i=0; i<8; i++){ |
| 95 | v = (v<<7) + (z[i]&0x7f); |
| 96 | if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; } |
| 97 | } |
| 98 | v = (v<<8) + (z[i]&0xff); |
| 99 | *pVal = v; |
| 100 | return 9; |
| 101 | } |
| 102 | |
| 103 | /* Report an out-of-memory error and die. |
| 104 | */ |
| 105 | static void out_of_memory(void){ |
| 106 | fprintf(stderr,"Out of memory...\n"); |
| 107 | exit(1); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | ** Read content from the file. |
| 112 | ** |
| 113 | ** Space to hold the content is obtained from malloc() and needs to be |
| 114 | ** freed by the caller. |
| 115 | */ |
| 116 | static unsigned char *getContent(int ofst, int nByte){ |
| 117 | unsigned char *aData; |
| 118 | aData = malloc(nByte); |
| 119 | if( aData==0 ) out_of_memory(); |
| 120 | lseek(fd, ofst, SEEK_SET); |
| 121 | read(fd, aData, nByte); |
| 122 | return aData; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | ** Print a range of bytes as hex and as ascii. |
| 127 | */ |
| 128 | static void print_byte_range( |
| 129 | int ofst, /* First byte in the range of bytes to print */ |
| 130 | int nByte, /* Number of bytes to print */ |
| 131 | unsigned char *aData, /* Content to print */ |
| 132 | int printOfst /* Add this amount to the index on the left column */ |
| 133 | ){ |
| 134 | int i, j; |
| 135 | const char *zOfstFmt; |
| 136 | |
| 137 | if( ((printOfst+nByte)&~0xfff)==0 ){ |
| 138 | zOfstFmt = " %03x: "; |
| 139 | }else if( ((printOfst+nByte)&~0xffff)==0 ){ |
| 140 | zOfstFmt = " %04x: "; |
| 141 | }else if( ((printOfst+nByte)&~0xfffff)==0 ){ |
| 142 | zOfstFmt = " %05x: "; |
| 143 | }else if( ((printOfst+nByte)&~0xffffff)==0 ){ |
| 144 | zOfstFmt = " %06x: "; |
| 145 | }else{ |
| 146 | zOfstFmt = " %08x: "; |
| 147 | } |
| 148 | |
| 149 | for(i=0; i<nByte; i += perLine){ |
| 150 | fprintf(stdout, zOfstFmt, i+printOfst); |
| 151 | for(j=0; j<perLine; j++){ |
| 152 | if( i+j>nByte ){ |
| 153 | fprintf(stdout, " "); |
| 154 | }else{ |
| 155 | fprintf(stdout,"%02x ", aData[i+j]); |
| 156 | } |
| 157 | } |
| 158 | for(j=0; j<perLine; j++){ |
| 159 | if( i+j>nByte ){ |
| 160 | fprintf(stdout, " "); |
| 161 | }else{ |
| 162 | fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.'); |
| 163 | } |
| 164 | } |
| 165 | fprintf(stdout,"\n"); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* Print a line of decode output showing a 4-byte integer. |
| 170 | */ |
| 171 | static void print_decode_line( |
| 172 | unsigned char *aData, /* Content being decoded */ |
| 173 | int ofst, int nByte, /* Start and size of decode */ |
| 174 | int asHex, /* If true, output value as hex */ |
| 175 | const char *zMsg /* Message to append */ |
| 176 | ){ |
| 177 | int i, j; |
| 178 | int val = aData[ofst]; |
| 179 | char zBuf[100]; |
| 180 | sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]); |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame^] | 181 | i = (int)strlen(zBuf); |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 182 | for(j=1; j<4; j++){ |
| 183 | if( j>=nByte ){ |
| 184 | sprintf(&zBuf[i], " "); |
| 185 | }else{ |
| 186 | sprintf(&zBuf[i], " %02x", aData[ofst+j]); |
| 187 | val = val*256 + aData[ofst+j]; |
| 188 | } |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame^] | 189 | i += (int)strlen(&zBuf[i]); |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 190 | } |
| 191 | if( asHex ){ |
| 192 | sprintf(&zBuf[i], " 0x%08x", val); |
| 193 | }else{ |
| 194 | sprintf(&zBuf[i], " %9d", val); |
| 195 | } |
| 196 | printf("%s %s\n", zBuf, zMsg); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | ** Print an entire page of content as hex |
| 201 | */ |
| 202 | static void print_frame(int iFrame){ |
| 203 | int iStart; |
| 204 | unsigned char *aData; |
| 205 | iStart = 32 + (iFrame-1)*(pagesize+24); |
| 206 | fprintf(stdout, "Frame %d: (offsets 0x%x..0x%x)\n", |
| 207 | iFrame, iStart, iStart+pagesize+24); |
| 208 | aData = getContent(iStart, pagesize+24); |
| 209 | print_decode_line(aData, 0, 4, 0, "Page number"); |
| 210 | print_decode_line(aData, 4, 4, 0, "DB size, or 0 for non-commit"); |
| 211 | print_decode_line(aData, 8, 4, 1, "Salt-1"); |
| 212 | print_decode_line(aData,12, 4, 1, "Salt-2"); |
| 213 | print_decode_line(aData,16, 4, 1, "Checksum-1"); |
| 214 | print_decode_line(aData,20, 4, 1, "Checksum-2"); |
| 215 | print_byte_range(iStart+24, pagesize, aData+24, 0); |
| 216 | free(aData); |
| 217 | } |
| 218 | |
| 219 | /* |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 220 | ** Summarize a single frame on a single line. |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 221 | */ |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 222 | static void print_oneline_frame(int iFrame, Cksum *pCksum){ |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 223 | int iStart; |
| 224 | unsigned char *aData; |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 225 | unsigned int s0, s1; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 226 | iStart = 32 + (iFrame-1)*(pagesize+24); |
| 227 | aData = getContent(iStart, 24); |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 228 | extendCksum(pCksum, aData, 8, 0); |
| 229 | extendCksum(pCksum, getContent(iStart+24, pagesize), pagesize, 0); |
| 230 | s0 = getInt32(aData+16); |
| 231 | s1 = getInt32(aData+20); |
| 232 | fprintf(stdout, "Frame %4d: %6d %6d 0x%08x,%08x 0x%08x,%08x %s\n", |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 233 | iFrame, |
| 234 | getInt32(aData), |
| 235 | getInt32(aData+4), |
| 236 | getInt32(aData+8), |
| 237 | getInt32(aData+12), |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 238 | s0, |
| 239 | s1, |
drh | 03c41c0 | 2013-01-25 15:31:44 +0000 | [diff] [blame] | 240 | (s0==pCksum->s0 && s1==pCksum->s1) ? "" : "cksum-fail" |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 241 | ); |
drh | 03c41c0 | 2013-01-25 15:31:44 +0000 | [diff] [blame] | 242 | |
| 243 | /* Reset the checksum so that a single frame checksum failure will not |
| 244 | ** cause all subsequent frames to also show a failure. */ |
| 245 | pCksum->s0 = s0; |
| 246 | pCksum->s1 = s1; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 247 | free(aData); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | ** Decode the WAL header. |
| 252 | */ |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 253 | static void print_wal_header(Cksum *pCksum){ |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 254 | unsigned char *aData; |
| 255 | aData = getContent(0, 32); |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 256 | if( pCksum ){ |
| 257 | extendCksum(pCksum, aData, 24, 1); |
| 258 | printf("Checksum byte order: %s\n", pCksum->bSwap ? "swapped" : "native"); |
| 259 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 260 | printf("WAL Header:\n"); |
| 261 | print_decode_line(aData, 0, 4,1,"Magic. 0x377f0682 (le) or 0x377f0683 (be)"); |
| 262 | print_decode_line(aData, 4, 4, 0, "File format"); |
| 263 | print_decode_line(aData, 8, 4, 0, "Database page size"); |
| 264 | print_decode_line(aData, 12,4, 0, "Checkpoint sequence number"); |
| 265 | print_decode_line(aData, 16,4, 1, "Salt-1"); |
| 266 | print_decode_line(aData, 20,4, 1, "Salt-2"); |
| 267 | print_decode_line(aData, 24,4, 1, "Checksum-1"); |
| 268 | print_decode_line(aData, 28,4, 1, "Checksum-2"); |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 269 | if( pCksum ){ |
| 270 | if( pCksum->s0!=getInt32(aData+24) ){ |
| 271 | printf("**** cksum-1 mismatch: 0x%08x\n", pCksum->s0); |
| 272 | } |
| 273 | if( pCksum->s1!=getInt32(aData+28) ){ |
| 274 | printf("**** cksum-2 mismatch: 0x%08x\n", pCksum->s1); |
| 275 | } |
| 276 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 277 | free(aData); |
| 278 | } |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 279 | /* |
| 280 | ** Describe cell content. |
| 281 | */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 282 | static i64 describeContent( |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 283 | unsigned char *a, /* Cell content */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 284 | i64 nLocal, /* Bytes in a[] */ |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 285 | char *zDesc /* Write description here */ |
| 286 | ){ |
| 287 | int nDesc = 0; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 288 | int n, j; |
| 289 | i64 i, x, v; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 290 | const unsigned char *pData; |
| 291 | const unsigned char *pLimit; |
| 292 | char sep = ' '; |
| 293 | |
| 294 | pLimit = &a[nLocal]; |
| 295 | n = decodeVarint(a, &x); |
| 296 | pData = &a[x]; |
| 297 | a += n; |
| 298 | i = x - n; |
| 299 | while( i>0 && pData<=pLimit ){ |
| 300 | n = decodeVarint(a, &x); |
| 301 | a += n; |
| 302 | i -= n; |
| 303 | nLocal -= n; |
| 304 | zDesc[0] = sep; |
| 305 | sep = ','; |
| 306 | nDesc++; |
| 307 | zDesc++; |
| 308 | if( x==0 ){ |
| 309 | sprintf(zDesc, "*"); /* NULL is a "*" */ |
| 310 | }else if( x>=1 && x<=6 ){ |
| 311 | v = (signed char)pData[0]; |
| 312 | pData++; |
| 313 | switch( x ){ |
| 314 | case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
| 315 | case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
| 316 | case 4: v = (v<<8) + pData[0]; pData++; |
| 317 | case 3: v = (v<<8) + pData[0]; pData++; |
| 318 | case 2: v = (v<<8) + pData[0]; pData++; |
| 319 | } |
| 320 | sprintf(zDesc, "%lld", v); |
| 321 | }else if( x==7 ){ |
| 322 | sprintf(zDesc, "real"); |
| 323 | pData += 8; |
| 324 | }else if( x==8 ){ |
| 325 | sprintf(zDesc, "0"); |
| 326 | }else if( x==9 ){ |
| 327 | sprintf(zDesc, "1"); |
| 328 | }else if( x>=12 ){ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 329 | i64 size = (x-12)/2; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 330 | if( (x&1)==0 ){ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 331 | sprintf(zDesc, "blob(%lld)", size); |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 332 | }else{ |
mistachkin | 3568397 | 2014-07-19 15:30:01 +0000 | [diff] [blame] | 333 | sprintf(zDesc, "txt(%lld)", size); |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 334 | } |
| 335 | pData += size; |
| 336 | } |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame^] | 337 | j = (int)strlen(zDesc); |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 338 | zDesc += j; |
| 339 | nDesc += j; |
| 340 | } |
| 341 | return nDesc; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | ** Compute the local payload size given the total payload size and |
| 346 | ** the page size. |
| 347 | */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 348 | static i64 localPayload(i64 nPayload, char cType){ |
| 349 | i64 maxLocal; |
| 350 | i64 minLocal; |
| 351 | i64 surplus; |
| 352 | i64 nLocal; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 353 | if( cType==13 ){ |
| 354 | /* Table leaf */ |
| 355 | maxLocal = pagesize-35; |
| 356 | minLocal = (pagesize-12)*32/255-23; |
| 357 | }else{ |
| 358 | maxLocal = (pagesize-12)*64/255-23; |
| 359 | minLocal = (pagesize-12)*32/255-23; |
| 360 | } |
| 361 | if( nPayload>maxLocal ){ |
| 362 | surplus = minLocal + (nPayload-minLocal)%(pagesize-4); |
| 363 | if( surplus<=maxLocal ){ |
| 364 | nLocal = surplus; |
| 365 | }else{ |
| 366 | nLocal = minLocal; |
| 367 | } |
| 368 | }else{ |
| 369 | nLocal = nPayload; |
| 370 | } |
| 371 | return nLocal; |
| 372 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | ** Create a description for a single cell. |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 376 | ** |
| 377 | ** The return value is the local cell size. |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 378 | */ |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 379 | static i64 describeCell( |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 380 | unsigned char cType, /* Page type */ |
| 381 | unsigned char *a, /* Cell content */ |
| 382 | int showCellContent, /* Show cell content if true */ |
| 383 | char **pzDesc /* Store description here */ |
| 384 | ){ |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 385 | int i; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 386 | i64 nDesc = 0; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 387 | int n = 0; |
| 388 | int leftChild; |
| 389 | i64 nPayload; |
| 390 | i64 rowid; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 391 | i64 nLocal; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 392 | static char zDesc[1000]; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 393 | i = 0; |
| 394 | if( cType<=5 ){ |
| 395 | leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3]; |
| 396 | a += 4; |
| 397 | n += 4; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 398 | sprintf(zDesc, "lx: %d ", leftChild); |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 399 | nDesc = strlen(zDesc); |
| 400 | } |
| 401 | if( cType!=5 ){ |
| 402 | i = decodeVarint(a, &nPayload); |
| 403 | a += i; |
| 404 | n += i; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 405 | sprintf(&zDesc[nDesc], "n: %lld ", nPayload); |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 406 | nDesc += strlen(&zDesc[nDesc]); |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 407 | nLocal = localPayload(nPayload, cType); |
| 408 | }else{ |
| 409 | nPayload = nLocal = 0; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 410 | } |
| 411 | if( cType==5 || cType==13 ){ |
| 412 | i = decodeVarint(a, &rowid); |
| 413 | a += i; |
| 414 | n += i; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 415 | sprintf(&zDesc[nDesc], "r: %lld ", rowid); |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 416 | nDesc += strlen(&zDesc[nDesc]); |
| 417 | } |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 418 | if( nLocal<nPayload ){ |
| 419 | int ovfl; |
| 420 | unsigned char *b = &a[nLocal]; |
| 421 | ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3]; |
| 422 | sprintf(&zDesc[nDesc], "ov: %d ", ovfl); |
| 423 | nDesc += strlen(&zDesc[nDesc]); |
| 424 | n += 4; |
| 425 | } |
| 426 | if( showCellContent && cType!=5 ){ |
| 427 | nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]); |
| 428 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 429 | *pzDesc = zDesc; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 430 | return nLocal+n; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* |
| 434 | ** Decode a btree page |
| 435 | */ |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 436 | static void decode_btree_page( |
| 437 | unsigned char *a, /* Content of the btree page to be decoded */ |
| 438 | int pgno, /* Page number */ |
| 439 | int hdrSize, /* Size of the page1-header in bytes */ |
| 440 | const char *zArgs /* Flags to control formatting */ |
| 441 | ){ |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 442 | const char *zType = "unknown"; |
| 443 | int nCell; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 444 | int i, j; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 445 | int iCellPtr; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 446 | int showCellContent = 0; |
| 447 | int showMap = 0; |
| 448 | char *zMap = 0; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 449 | switch( a[0] ){ |
| 450 | case 2: zType = "index interior node"; break; |
| 451 | case 5: zType = "table interior node"; break; |
| 452 | case 10: zType = "index leaf"; break; |
| 453 | case 13: zType = "table leaf"; break; |
| 454 | } |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 455 | while( zArgs[0] ){ |
| 456 | switch( zArgs[0] ){ |
| 457 | case 'c': showCellContent = 1; break; |
| 458 | case 'm': showMap = 1; break; |
| 459 | } |
| 460 | zArgs++; |
| 461 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 462 | printf("Decode of btree page %d:\n", pgno); |
| 463 | print_decode_line(a, 0, 1, 0, zType); |
| 464 | print_decode_line(a, 1, 2, 0, "Offset to first freeblock"); |
| 465 | print_decode_line(a, 3, 2, 0, "Number of cells on this page"); |
| 466 | nCell = a[3]*256 + a[4]; |
| 467 | print_decode_line(a, 5, 2, 0, "Offset to cell content area"); |
| 468 | print_decode_line(a, 7, 1, 0, "Fragmented byte count"); |
| 469 | if( a[0]==2 || a[0]==5 ){ |
| 470 | print_decode_line(a, 8, 4, 0, "Right child"); |
| 471 | iCellPtr = 12; |
| 472 | }else{ |
| 473 | iCellPtr = 8; |
| 474 | } |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 475 | if( nCell>0 ){ |
| 476 | printf(" key: lx=left-child n=payload-size r=rowid\n"); |
| 477 | } |
| 478 | if( showMap ){ |
| 479 | zMap = malloc(pagesize); |
| 480 | memset(zMap, '.', pagesize); |
| 481 | memset(zMap, '1', hdrSize); |
| 482 | memset(&zMap[hdrSize], 'H', iCellPtr); |
| 483 | memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell); |
| 484 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 485 | for(i=0; i<nCell; i++){ |
| 486 | int cofst = iCellPtr + i*2; |
| 487 | char *zDesc; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 488 | i64 n; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 489 | |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 490 | cofst = a[cofst]*256 + a[cofst+1]; |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 491 | n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc); |
| 492 | if( showMap ){ |
| 493 | char zBuf[30]; |
mistachkin | 0461cc4 | 2014-07-18 21:16:37 +0000 | [diff] [blame] | 494 | memset(&zMap[cofst], '*', (size_t)n); |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 495 | zMap[cofst] = '['; |
| 496 | zMap[cofst+n-1] = ']'; |
| 497 | sprintf(zBuf, "%d", i); |
mistachkin | ef60703 | 2014-07-19 15:40:39 +0000 | [diff] [blame^] | 498 | j = (int)strlen(zBuf); |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 499 | if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j); |
| 500 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 501 | printf(" %03x: cell[%d] %s\n", cofst, i, zDesc); |
| 502 | } |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 503 | if( showMap ){ |
| 504 | for(i=0; i<pagesize; i+=64){ |
| 505 | printf(" %03x: %.64s\n", i, &zMap[i]); |
| 506 | } |
| 507 | free(zMap); |
| 508 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | int main(int argc, char **argv){ |
| 512 | struct stat sbuf; |
| 513 | unsigned char zPgSz[2]; |
| 514 | if( argc<2 ){ |
| 515 | fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]); |
| 516 | exit(1); |
| 517 | } |
| 518 | fd = open(argv[1], O_RDONLY); |
| 519 | if( fd<0 ){ |
| 520 | fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); |
| 521 | exit(1); |
| 522 | } |
| 523 | zPgSz[0] = 0; |
| 524 | zPgSz[1] = 0; |
| 525 | lseek(fd, 10, SEEK_SET); |
| 526 | read(fd, zPgSz, 2); |
| 527 | pagesize = zPgSz[0]*256 + zPgSz[1]; |
| 528 | if( pagesize==0 ) pagesize = 1024; |
| 529 | printf("Pagesize: %d\n", pagesize); |
| 530 | fstat(fd, &sbuf); |
| 531 | if( sbuf.st_size<32 ){ |
| 532 | printf("file too small to be a WAL\n"); |
| 533 | return 0; |
| 534 | } |
| 535 | mxFrame = (sbuf.st_size - 32)/(pagesize + 24); |
| 536 | printf("Available pages: 1..%d\n", mxFrame); |
| 537 | if( argc==2 ){ |
| 538 | int i; |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 539 | Cksum x; |
| 540 | print_wal_header(&x); |
| 541 | for(i=1; i<=mxFrame; i++){ |
| 542 | print_oneline_frame(i, &x); |
| 543 | } |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 544 | }else{ |
| 545 | int i; |
| 546 | for(i=2; i<argc; i++){ |
| 547 | int iStart, iEnd; |
| 548 | char *zLeft; |
| 549 | if( strcmp(argv[i], "header")==0 ){ |
drh | d63ce04 | 2013-01-25 15:09:41 +0000 | [diff] [blame] | 550 | print_wal_header(0); |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 551 | continue; |
| 552 | } |
| 553 | if( !isdigit(argv[i][0]) ){ |
| 554 | fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]); |
| 555 | continue; |
| 556 | } |
| 557 | iStart = strtol(argv[i], &zLeft, 0); |
| 558 | if( zLeft && strcmp(zLeft,"..end")==0 ){ |
| 559 | iEnd = mxFrame; |
| 560 | }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ |
| 561 | iEnd = strtol(&zLeft[2], 0, 0); |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 562 | }else if( zLeft && zLeft[0]=='b' ){ |
| 563 | int ofst, nByte, hdrSize; |
| 564 | unsigned char *a; |
| 565 | if( iStart==1 ){ |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 566 | hdrSize = 100; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 567 | ofst = hdrSize = 100; |
| 568 | nByte = pagesize-100; |
| 569 | }else{ |
| 570 | hdrSize = 0; |
| 571 | ofst = (iStart-1)*pagesize; |
| 572 | nByte = pagesize; |
| 573 | } |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 574 | ofst = 32 + hdrSize + (iStart-1)*(pagesize+24) + 24; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 575 | a = getContent(ofst, nByte); |
drh | 1590d10 | 2013-01-25 15:59:55 +0000 | [diff] [blame] | 576 | decode_btree_page(a, iStart, hdrSize, zLeft+1); |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 577 | free(a); |
| 578 | continue; |
drh | 12c7e1a | 2010-07-07 20:38:26 +0000 | [diff] [blame] | 579 | }else{ |
| 580 | iEnd = iStart; |
| 581 | } |
| 582 | if( iStart<1 || iEnd<iStart || iEnd>mxFrame ){ |
| 583 | fprintf(stderr, |
| 584 | "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", |
| 585 | mxFrame); |
| 586 | exit(1); |
| 587 | } |
| 588 | while( iStart<=iEnd ){ |
| 589 | print_frame(iStart); |
| 590 | iStart++; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | close(fd); |
| 595 | return 0; |
| 596 | } |