]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Enhance the "showdb" utility program with the "pgidx" option. Now requires
authordrh <drh@noemail.net>
Tue, 3 Apr 2012 14:59:50 +0000 (14:59 +0000)
committerdrh <drh@noemail.net>
Tue, 3 Apr 2012 14:59:50 +0000 (14:59 +0000)
linkage with the amalgamation.

FossilOrigin-Name: 4b5737014c6f1638de9dc162463508ea7dfe333d

manifest
manifest.uuid
tool/showdb.c

index bebaba541e1a4681a2fc696a117b2fa1c1ab6705..095f368fee7610351ef001e0583cc76315f8499f 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C The\sSQLITE_RTREE_INT_ONLY\scompile-time\soption\scauses\sthe\sRTree\sextension\s\nto\suse\sonly\sinteger\smath\sand\sstore\sonly\sinteger\scoordinates.
-D 2012-04-02T21:35:42.939
+C Enhance\sthe\s"showdb"\sutility\sprogram\swith\sthe\s"pgidx"\soption.\s\sNow\srequires\nlinkage\swith\sthe\samalgamation.
+D 2012-04-03T14:59:50.844
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 2f37e468503dbe79d35c9f6dffcf3fae1ae9ec20
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -982,7 +982,7 @@ F tool/shell2.test 5dc76b8005b465f420fed8241621da7513060ff3
 F tool/shell3.test 4fad469e8003938426355afdf34155f08c587836
 F tool/shell4.test 35f9c3d452b4e76d5013c63e1fd07478a62f14ce
 F tool/shell5.test 0e987fb8d40638bb5c90163cb58cbe3e07dbed56
-F tool/showdb.c 43e913d954684c2f5007dcab46d1a1308852a0ad
+F tool/showdb.c 9cdc45377f7512902232bdc18bd5119cc2512a1a
 F tool/showjournal.c b62cecaab86a4053d944c276bb5232e4d17ece02
 F tool/showwal.c f09e5a80a293919290ec85a6a37c85a5ddcf37d9
 F tool/soak1.tcl 8d407956e1a45b485a8e072470a3e629a27037fe
@@ -1000,7 +1000,7 @@ F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
 F tool/warnings-clang.sh 9f406d66e750e8ac031c63a9ef3248aaa347ef2a
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
-P fb121980e48af368353431fd04924e414b65c852
-R 5f487522a2da2d1baf5cda41d792b333
+P 02b7640f5118e0a635b68f65765191bb3171b7bd
+R 4634bc5c716828f15113d298489e79da
 U drh
-Z f6050a33b89113e7c2cd85615563f7a3
+Z b9b2c57600f8181cc1cad22a91aba3cd
index 211e12f14c7b4184e5122ce35541fdda7473487a..fb9d6b2b223bf7e2c31253e3bebb008855fbf015 100644 (file)
@@ -1 +1 @@
-02b7640f5118e0a635b68f65765191bb3171b7bd
\ No newline at end of file
+4b5737014c6f1638de9dc162463508ea7dfe333d
\ No newline at end of file
index 057abd32bae3d2991b7d16572f23a6634d1d2378..a6e3060a14bff9c3435d03bcf070344e991aba4f 100644 (file)
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include "sqlite3.h"
 
 
 static int pagesize = 1024;     /* Size of a database page */
@@ -450,6 +451,222 @@ static void decode_trunk_page(
   }
 }
 
+/*
+** A short text comment on the use of each page.
+*/
+static char **zPageUse;
+
+/*
+** Add a comment on the use of a page.
+*/
+static void page_usage_msg(int pgno, const char *zFormat, ...){
+  va_list ap;
+  char *zMsg;
+
+  va_start(ap, zFormat);
+  zMsg = sqlite3_vmprintf(zFormat, ap);
+  va_end(ap);
+  if( pgno<=0 || pgno>mxPage ){
+    printf("ERROR: page %d out of bounds.  Range=1..%d.  Msg: %s\n",
+            pgno, mxPage, zMsg);
+    sqlite3_free(zMsg);
+    return;
+  }
+  if( zPageUse[pgno]!=0 ){
+    printf("ERROR: page %d used multiple times:\n", pgno);
+    printf("ERROR:    previous: %s\n", zPageUse[pgno]);
+    printf("ERROR:    current:  %s\n", zPageUse[pgno]);
+    sqlite3_free(zPageUse[pgno]);
+  }
+  zPageUse[pgno] = zMsg;
+}
+
+/*
+** Find overflow pages of a cell and describe their usage.
+*/
+static void page_usage_cell(
+  unsigned char cType,    /* Page type */
+  unsigned char *a,       /* Cell content */
+  int pgno,               /* page containing the cell */
+  int cellno              /* Index of the cell on the page */
+){
+  int i;
+  int nDesc = 0;
+  int n = 0;
+  i64 nPayload;
+  i64 rowid;
+  int nLocal;
+  i = 0;
+  if( cType<=5 ){
+    a += 4;
+    n += 4;
+  }
+  if( cType!=5 ){
+    i = decodeVarint(a, &nPayload);
+    a += i;
+    n += i;
+    nLocal = localPayload(nPayload, cType);
+  }else{
+    nPayload = nLocal = 0;
+  }
+  if( cType==5 || cType==13 ){
+    i = decodeVarint(a, &rowid);
+    a += i;
+    n += i;
+  }
+  if( nLocal<nPayload ){
+    int ovfl = decodeInt32(a+nLocal);
+    int cnt = 0;
+    while( ovfl && (cnt++)<mxPage ){
+      page_usage_msg(ovfl, "overflow %d from cell %d of page %d",
+                     cnt, cellno, pgno);
+      a = getContent((ovfl-1)*pagesize, 4);
+      ovfl = decodeInt32(a);
+      free(a);
+    }
+  }
+}
+
+
+/*
+** Describe the usages of a b-tree page
+*/
+static void page_usage_btree(
+  int pgno,             /* Page to describe */
+  int parent,           /* Parent of this page.  0 for root pages */
+  int idx,              /* Which child of the parent */
+  const char *zName     /* Name of the table */
+){
+  unsigned char *a;
+  const char *zType = "corrupt node";
+  int nCell;
+  int i;
+  int hdr = pgno==1 ? 100 : 0;
+
+  if( pgno<=0 || pgno>mxPage ) return;
+  a = getContent((pgno-1)*pagesize, pagesize);
+  switch( a[hdr] ){
+    case 2:  zType = "interior node of index";  break;
+    case 5:  zType = "interior node of table";  break;
+    case 10: zType = "leaf of index";           break;
+    case 13: zType = "leaf of table";           break;
+  }
+  if( parent ){
+    page_usage_msg(pgno, "%s [%s], child %d of page %d",
+                   zType, zName, idx, parent);
+  }else{
+    page_usage_msg(pgno, "root %s [%s]", zType, zName);
+  }
+  nCell = a[hdr+3]*256 + a[hdr+4];
+  if( a[hdr]==2 || a[hdr]==5 ){
+    int cellstart = hdr+12;
+    unsigned int child;
+    for(i=0; i<nCell; i++){
+      int ofst;
+
+      ofst = cellstart + i*2;
+      ofst = a[ofst]*256 + a[ofst+1];
+      child = decodeInt32(a+ofst);
+      page_usage_btree(child, pgno, i, zName);
+    }
+    child = decodeInt32(a+cellstart-4);
+    page_usage_btree(child, pgno, i, zName);
+  }
+  if( a[hdr]==2 || a[hdr]==10 || a[hdr]==13 ){
+    int cellstart = hdr + 8 + 4*(a[hdr]<=5);
+    for(i=0; i<nCell; i++){
+      int ofst;
+      ofst = cellstart + i*2;
+      ofst = a[ofst]*256 + a[ofst+1];
+      page_usage_cell(a[hdr], a+ofst, pgno, i);
+    }
+  }
+  free(a);
+}
+
+/*
+** Determine page usage by the freelist
+*/
+static void page_usage_freelist(int pgno){
+  unsigned char *a;
+  int cnt = 0;
+  int i;
+  int n;
+  int iNext;
+  int parent = 1;
+
+  while( pgno>0 && pgno<=mxPage && (cnt++)<mxPage ){
+    page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent);
+    a = getContent((pgno-1)*pagesize, pagesize);
+    iNext = decodeInt32(a);
+    n = decodeInt32(a+4);
+    for(i=0; i<n; i++){
+      int child = decodeInt32(a + (i*4+8));
+      page_usage_msg(child, "freelist leaf, child %d of trunk page %d",
+                     i, pgno);
+    }
+    free(a);
+    parent = pgno;
+    pgno = iNext;
+  }
+}
+
+/*
+** Try to figure out how every page in the database file is being used.
+*/
+static void page_usage_report(const char *zDbName){
+  int i;
+  int rc;
+  sqlite3 *db;
+  sqlite3_stmt *pStmt;
+  unsigned char *a;
+
+  /* Avoid the pathological case */
+  if( mxPage<1 ){
+    printf("empty database\n");
+    return;
+  }
+
+  /* Open the database file */
+  rc = sqlite3_open(zDbName, &db);
+  if( rc ){
+    printf("cannot open database: %s\n", sqlite3_errmsg(db));
+    sqlite3_close(db);
+    return;
+  }
+
+  /* Set up global variables zPageUse[] and mxPage to record page
+  ** usages */
+  zPageUse = sqlite3_malloc( sizeof(zPageUse[0])*(mxPage+1) );
+  if( zPageUse==0 ) out_of_memory();
+  memset(zPageUse, 0, sizeof(zPageUse[0])*(mxPage+1));
+
+  /* Discover the usage of each page */
+  a = getContent(0, 100);
+  page_usage_freelist(decodeInt32(a+32));
+  free(a);
+  page_usage_btree(1, 0, 0, "sqlite_master");
+  rc = sqlite3_prepare_v2(db,
+           "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage",
+           -1, &pStmt, 0);
+  if( rc==SQLITE_OK ){
+    while( sqlite3_step(pStmt)==SQLITE_ROW ){
+      int pgno = sqlite3_column_int(pStmt, 2);
+      page_usage_btree(pgno, 0, 0, sqlite3_column_text(pStmt, 1));
+    }
+  }
+  sqlite3_finalize(pStmt);
+  sqlite3_close(db);
+
+  /* Print the report and free memory used */
+  for(i=1; i<=mxPage; i++){
+    printf("%5d: %s\n", i, zPageUse[i] ? zPageUse[i] : "???");
+    sqlite3_free(zPageUse[i]);
+  }
+  sqlite3_free(zPageUse);
+  zPageUse = 0;
+}
+
 /*
 ** Print a usage comment
 */
@@ -458,6 +675,7 @@ static void usage(const char *argv0){
   fprintf(stderr,
     "args:\n"
     "    dbheader        Show database header\n"
+    "    pgidx           Index of how each page is used\n"
     "    NNN..MMM        Show hex of pages NNN through MMM\n"
     "    NNN..end        Show hex of pages NNN through end of file\n"
     "    NNNb            Decode btree page NNN\n"
@@ -503,6 +721,10 @@ int main(int argc, char **argv){
         print_db_header();
         continue;
       }
+      if( strcmp(argv[i], "pgidx")==0 ){
+        page_usage_report(argv[1]);
+        continue;
+      }
       if( !isdigit(argv[i][0]) ){
         fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]);
         continue;