]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Enhancements to the showdb.c utility program. Automatically detect the
authordrh <drh@noemail.net>
Mon, 26 Apr 2010 15:44:07 +0000 (15:44 +0000)
committerdrh <drh@noemail.net>
Mon, 26 Apr 2010 15:44:07 +0000 (15:44 +0000)
page size and adjust the display accordingly.  Add the "dbheader" display
option.

FossilOrigin-Name: 23eb408b5d08b89b41aaf245a7be8e235f30af8a

manifest
manifest.uuid
tool/showdb.c

index a99faf14fc8d7af02647e560175d58177445e988..43c5db3d5b83f7be8d203debdc6a4af758ca78c5 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,8 +1,8 @@
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
-C Remove\san\sunreachable\sbranch\sfrom\sthe\sfunction\sinitialization\slogic.
-D 2010-04-25T20:58:37
+C Enhancements\sto\sthe\sshowdb.c\sutility\sprogram.\s\sAutomatically\sdetect\sthe\npage\ssize\sand\sadjust\sthe\sdisplay\saccordingly.\s\sAdd\sthe\s"dbheader"\sdisplay\noption.
+D 2010-04-26T15:44:07
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 4f2f967b7e58a35bb74fb7ec8ae90e0f4ca7868b
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -790,7 +790,7 @@ F tool/restore_jrnl.tcl 6957a34f8f1f0f8285e07536225ec3b292a9024a
 F tool/shell1.test ef08a3e738b9fee4fc228920956950bc35db0575
 F tool/shell2.test 8f51f61c13b88618e71c17439fe0847c2421c5d1
 F tool/shell3.test ff663e83100670a295d473515c12beb8103a78b6
-F tool/showdb.c 8ab8b3b53884312aafb7ef60982e255a6c31d238
+F tool/showdb.c f36a0d6250cd66da80c70c5f68db504b6004ffae
 F tool/showjournal.c ec3b171be148656827c4949fbfb8ab4370822f87
 F tool/soak1.tcl 8d407956e1a45b485a8e072470a3e629a27037fe
 F tool/space_used.tcl f714c41a59e326b8b9042f415b628b561bafa06b
@@ -801,14 +801,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P 0291ed974d5bf1e344e2c38422530cc961b897da
-R fd6e4addad7e1a8279cfaf819615b3fa
+P 064d3ddd6199b7b049b1706974e4295f78fb5ab9
+R 8b381715211fe751bde39cc29f2ff9cd
 U drh
-Z 7d20f9f474a13977b8eb713940e9f8af
+Z 764a1f25e347ea0d4e5e531b573f483f
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.6 (GNU/Linux)
 
-iD8DBQFL1Kz/oxKgR168RlERAgG0AJoDjmhXedAET5V0L1Z5n0cXBHnSMwCff/bm
-c8fjZiKrCLhVKjzQJ+FPaRw=
-=pPi4
+iD8DBQFL1bTKoxKgR168RlERAo5cAJ9Eb+lGq+Qu8+YZ7K+hoPtgssOPdwCfc3aA
+gSmF+BvN/QcbZjim662ior8=
+=k+Sx
 -----END PGP SIGNATURE-----
index ccbbcfba665192b185b241268118bc8f259b125e..5f6a0740c2566dbc9c2d2f6eb123d89ca3e449d3 100644 (file)
@@ -1 +1 @@
-064d3ddd6199b7b049b1706974e4295f78fb5ab9
\ No newline at end of file
+23eb408b5d08b89b41aaf245a7be8e235f30af8a
\ No newline at end of file
index 26e7ea2be25c69251f19e35e732a1bda184da582..d8f9d79d815c4a15bd501189c77cc265f7e1fd60 100644 (file)
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <string.h>
 
 
-static int pagesize = 1024;
-static int db = -1;
-static int mxPage = 0;
-static int perLine = 32;
+static int pagesize = 1024;     /* Size of a database page */
+static int db = -1;             /* File descriptor for reading the DB */
+static int mxPage = 0;          /* Last page number */
+static int perLine = 16;        /* HEX elements to print per line */
 
+typedef long long int i64;      /* Datatype for 64-bit integers */
+
+
+/*
+** Convert the var-int format into i64.  Return the number of bytes
+** in the var-int.  Write the var-int value into *pVal.
+*/
+static int decodeVarint(const char *z, i64 *pVal){
+  i64 v = 0;
+  int i = 0;
+  while( i<8 ){
+    v = (v<<7) + (z[i]&0x7f);
+    if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
+  }
+  v = (v<<8) + (z[i]&0xff);
+  *pVal = v;
+  return 9;
+}
+
+/* Report an out-of-memory error and die.
+*/
 static void out_of_memory(void){
   fprintf(stderr,"Out of memory...\n");
   exit(1);
 }
 
-static print_page(int iPg){
+/*
+** Read content from the file.
+**
+** Space to hold the content is obtained from malloc() and needs to be
+** freed by the caller.
+*/
+static unsigned char *getContent(int ofst, int nByte){
   unsigned char *aData;
-  int i, j;
-  aData = malloc(pagesize);
+  aData = malloc(nByte);
   if( aData==0 ) out_of_memory();
-  lseek(db, (iPg-1)*(long long int)pagesize, SEEK_SET);
-  read(db, aData, pagesize);
-  fprintf(stdout, "Page %d:\n", iPg);
-  for(i=0; i<pagesize; i += perLine){
-    fprintf(stdout, " %03x: ",i);
+  lseek(db, ofst, SEEK_SET);
+  read(db, aData, nByte);
+  return aData;
+}
+
+/*
+** Print a range of bytes as hex and as ascii.
+*/
+static unsigned char *print_byte_range(
+  int ofst,          /* First byte in the range of bytes to print */
+  int nByte,         /* Number of bytes to print */
+  int printOfst      /* Add this amount to the index on the left column */
+){
+  unsigned char *aData;
+  int i, j;
+  const char *zOfstFmt;
+
+  if( ((printOfst+nByte)&~0xfff)==0 ){
+    zOfstFmt = " %03x: ";
+  }else if( ((printOfst+nByte)&~0xffff)==0 ){
+    zOfstFmt = " %04x: ";
+  }else if( ((printOfst+nByte)&~0xfffff)==0 ){
+    zOfstFmt = " %05x: ";
+  }else if( ((printOfst+nByte)&~0xffffff)==0 ){
+    zOfstFmt = " %06x: ";
+  }else{
+    zOfstFmt = " %08x: ";
+  }
+
+  aData = getContent(ofst, nByte);
+  for(i=0; i<nByte; i += perLine){
+    fprintf(stdout, zOfstFmt, i+printOfst);
     for(j=0; j<perLine; j++){
-      fprintf(stdout,"%02x ", aData[i+j]);
+      if( i+j>nByte ){
+        fprintf(stdout, "   ");
+      }else{
+        fprintf(stdout,"%02x ", aData[i+j]);
+      }
     }
     for(j=0; j<perLine; j++){
-      fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
+      if( i+j>nByte ){
+        fprintf(stdout, " ");
+      }else{
+        fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
+      }
     }
     fprintf(stdout,"\n");
   }
+  return aData;
+}
+
+/*
+** Print an entire page of content as hex
+*/
+static print_page(int iPg){
+  int iStart;
+  unsigned char *aData;
+  iStart = (iPg-1)*pagesize;
+  fprintf(stdout, "Page %d:   (offsets 0x%x..0x%x)\n",
+          iPg, iStart, iStart+pagesize-1);
+  aData = print_byte_range(iStart, pagesize, 0);
   free(aData);
 }
 
+/* Print a line of decode output showing a 4-byte integer.
+*/
+static print_decode_line(
+  unsigned char *aData,      /* Content being decoded */
+  int ofst, int nByte,       /* Start and size of decode */
+  const char *zMsg           /* Message to append */
+){
+  int i, j;
+  int val = aData[ofst];
+  char zBuf[100];
+  sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
+  i = strlen(zBuf);
+  for(j=1; j<4; j++){
+    if( j>=nByte ){
+      sprintf(&zBuf[i], "   ");
+    }else{
+      sprintf(&zBuf[i], " %02x", aData[ofst+j]);
+      val = val*256 + aData[ofst+j];
+    }
+    i += strlen(&zBuf[i]);
+  }
+  sprintf(&zBuf[i], "   %9d", val);
+  printf(" %s  %s\n", zBuf, zMsg);
+}
+
+/*
+** Decode the database header.
+*/
+static print_db_header(void){
+  unsigned char *aData;
+  aData = print_byte_range(0, 100, 0);
+  printf("Decoded:\n");
+  print_decode_line(aData, 16, 2, "Database page size");
+  print_decode_line(aData, 18, 1, "File format write version");
+  print_decode_line(aData, 19, 1, "File format read version");
+  print_decode_line(aData, 20, 1, "Reserved space at end of page");
+  print_decode_line(aData, 24, 4, "File change counter");
+  print_decode_line(aData, 28, 4, "Size of database in pages");
+  print_decode_line(aData, 32, 4, "Page number of first freelist page");
+  print_decode_line(aData, 36, 4, "Number of freelist pages");
+  print_decode_line(aData, 40, 4, "Schema cookie");
+  print_decode_line(aData, 44, 4, "Schema format version");
+  print_decode_line(aData, 48, 4, "Default page cache size");
+  print_decode_line(aData, 52, 4, "Largest auto-vac root page");
+  print_decode_line(aData, 56, 4, "Text encoding");
+  print_decode_line(aData, 60, 4, "User version");
+  print_decode_line(aData, 64, 4, "Incremental-vacuum mode");
+  print_decode_line(aData, 68, 4, "meta[7]");
+  print_decode_line(aData, 72, 4, "meta[8]");
+  print_decode_line(aData, 76, 4, "meta[9]");
+  print_decode_line(aData, 80, 4, "meta[10]");
+  print_decode_line(aData, 84, 4, "meta[11]");
+  print_decode_line(aData, 88, 4, "meta[12]");
+  print_decode_line(aData, 92, 4, "meta[13]");
+  print_decode_line(aData, 96, 4, "meta[14]");
+}
+
 int main(int argc, char **argv){
   struct stat sbuf;
+  unsigned char zPgSz[2];
   if( argc<2 ){
     fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]);
     exit(1);
@@ -52,8 +185,16 @@ int main(int argc, char **argv){
     fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
     exit(1);
   }
+  zPgSz[0] = 0;
+  zPgSz[1] = 0;
+  lseek(db, 16, SEEK_SET);
+  read(db, zPgSz, 2);
+  pagesize = zPgSz[0]*256 + zPgSz[1];
+  if( pagesize==0 ) pagesize = 1024;
+  printf("Pagesize: %d\n", pagesize);
   fstat(db, &sbuf);
-  mxPage = sbuf.st_size/pagesize + 1;
+  mxPage = sbuf.st_size/pagesize;
+  printf("Available pages: 1..%d\n", mxPage);
   if( argc==2 ){
     int i;
     for(i=1; i<=mxPage; i++) print_page(i);
@@ -62,6 +203,14 @@ int main(int argc, char **argv){
     for(i=2; i<argc; i++){
       int iStart, iEnd;
       char *zLeft;
+      if( strcmp(argv[i], "dbheader")==0 ){
+        print_db_header();
+        continue;
+      }
+      if( !isdigit(argv[i][0]) ){
+        fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]);
+        continue;
+      }
       iStart = strtol(argv[i], &zLeft, 0);
       if( zLeft && strcmp(zLeft,"..end")==0 ){
         iEnd = mxPage;