]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add an option to register global hooks used for logging all SQL executed by an applic...
authordan <dan@noemail.net>
Mon, 26 Nov 2012 19:50:41 +0000 (19:50 +0000)
committerdan <dan@noemail.net>
Mon, 26 Nov 2012 19:50:41 +0000 (19:50 +0000)
FossilOrigin-Name: cd501bbccf3e62b002317592cc331770b32c129a

main.mk
manifest
manifest.uuid
src/global.c
src/main.c
src/sqlite.h.in
src/sqliteInt.h
src/test_sqllog.c [new file with mode: 0644]
src/vdbeaux.c

diff --git a/main.mk b/main.mk
index 76a9f406a212478a112871e0d93475cdd667a73f..cbae400a5037177bf3bcccbb50b5278aa27bfc46 100644 (file)
--- a/main.mk
+++ b/main.mk
@@ -257,6 +257,7 @@ TESTSRC = \
   $(TOP)/src/test_schema.c \
   $(TOP)/src/test_server.c \
   $(TOP)/src/test_stat.c \
+  $(TOP)/src/test_sqllog.c \
   $(TOP)/src/test_superlock.c \
   $(TOP)/src/test_syscall.c \
   $(TOP)/src/test_tclvar.c \
index 42664c084e92e65ca8ef13ddd30026049f6fbcb1..56f3032e28877217ab39f97e670bdb2aabbb2461 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Have\sthe\swindows\simplementation\sof\sxDelete\sreturn\sSQLITE_IOERR_DELETE_NOENT\nif\sthe\sfile\sto\sbe\sdeleted\sdoes\snot\sexist.\s\sThe\sunix\simplementation\swas\npreviously\smodified\sto\sbehave\sthis\sway.\s\sThe\scurrent\schanges\ssimply\sbrings\nthe\stwo\simplementations\sinto\salignment.
-D 2012-11-20T15:06:57.977
+C Add\san\soption\sto\sregister\sglobal\shooks\sused\sfor\slogging\sall\sSQL\sexecuted\sby\san\sapplication.
+D 2012-11-26T19:50:41.133
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 82c41c0ed4cc94dd3cc7d498575b84c57c2c2384
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -103,7 +103,7 @@ F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de
 F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024
 F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x
 F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8
-F main.mk 615aeaf7474cb2f141547e0c3ac08bcb75dd2b55
+F main.mk a0d170ae1a8a8683688e281194e09d47a68eaa3f
 F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a
 F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f
 F mkextw.sh 4123480947681d9b434a5e7b1ee08135abe409ac
@@ -134,7 +134,7 @@ F src/expr.c 3b25a95f3d309403940ba4a3212f197b8b6251d5
 F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
 F src/fkey.c c82a04e7a92bb728f9ab972b76590403283be2af
 F src/func.c 1755cafdb8f2a291681f1ea55e0215518ebd6e52
-F src/global.c fb44b11e02e06c995e6ed6642509edd23599d584
+F src/global.c e59ecd2c553ad0d4bfbc84ca71231336f8993a7a
 F src/hash.c ac3470bbf1ca4ae4e306a8ecb0fdf1731810ffe4
 F src/hash.h 2894c932d84d9f892d4b4023a75e501f83050970
 F src/hwtime.h d32741c8f4df852c7d959236615444e2b1063b08
@@ -143,7 +143,7 @@ F src/journal.c 552839e54d1bf76fb8f7abe51868b66acacf6a0e
 F src/legacy.c a199d7683d60cef73089e892409113e69c23a99f
 F src/lempar.c cdf0a000315332fc9b50b62f3b5e22e080a0952b
 F src/loadext.c f20382fbaeec832438a1ba7797bee3d3c8a6d51d
-F src/main.c 5249486037f88e765cce7c4870846dc5a2cd2ed1
+F src/main.c c0b8cfbe397801dfae1f63b56b6dd30b8726b199
 F src/malloc.c fe085aa851b666b7c375c1ff957643dc20a04bf6
 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
 F src/mem1.c 437c7c4af964895d4650f29881df63535caaa1fa
@@ -176,10 +176,10 @@ F src/resolve.c 7b986a715ac281643309c29257bb58cfae7aa810
 F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0
 F src/select.c 3a8baf4719f9723b4e0b43f2baa60692d0d921f8
 F src/shell.c 24cd0aa74aff73ea08594629faead564c4c2a286
-F src/sqlite.h.in 2a0d1234242d75e19e297db99f0925fc9f021e85
+F src/sqlite.h.in c0ab54580c16be8618fd9af48041dfc56cf071b2
 F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0
 F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477
-F src/sqliteInt.h 79c00e24d84541c3117ef34ce09c5749dcdcba25
+F src/sqliteInt.h 68f23dcfc3fb6559c07b327ab6d8d2e63e4bb69e
 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
 F src/status.c bedc37ec1a6bb9399944024d63f4c769971955a9
 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
@@ -221,6 +221,7 @@ F src/test_rtree.c aba603c949766c4193f1068b91c787f57274e0d9
 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0
 F src/test_server.c 2f99eb2837dfa06a4aacf24af24c6affdf66a84f
 F src/test_spellfix.c 76dd8d3111d2f5354c374f71fa23b752bd0b029c
+F src/test_sqllog.c 5e8e25829947eee74ebb68a484d22a71c54b9c2f
 F src/test_stat.c d1569c7a4839f13e80187e2c26b2ab4da2d03935
 F src/test_superlock.c 2b97936ca127d13962c3605dbc9a4ef269c424cd
 F src/test_syscall.c a992d8c80ea91fbf21fb2dd570db40e77dd7e6ae
@@ -240,7 +241,7 @@ F src/vdbe.c e1b2f881f85598b8e5d3366a3cdba60526d40144
 F src/vdbe.h b52887278cb173e66188da84dfab216bea61119d
 F src/vdbeInt.h 79abf9b31be406d35ca77d6999cb2d42aaf91e78
 F src/vdbeapi.c 4c2418161cf45392ba76a7ca92f9a5f06b96f89c
-F src/vdbeaux.c 8c8cfd30063e9b3664e2faa0e3785102bf131a10
+F src/vdbeaux.c 552a878f5d9d3484ff6fd09acc3d566403f5db4b
 F src/vdbeblob.c 32f2a4899d67f69634ea4dd93e3f651936d732cb
 F src/vdbemem.c cb55e84b8e2c15704968ee05f0fae25883299b74
 F src/vdbesort.c c61ca318681c0e7267da8be3abfca8469652a7e9
@@ -1024,7 +1025,10 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
-P 29980b08ec591f695386b715df72d4afb6ffc3fb
-R b34ddf6d9c41f9627ecb66c931c8da70
-U drh
-Z d5d273e9e92a86e1ab421c06a18a3cfd
+P d4c36d4991b048133efb21b251ab57fa66764d9d
+R a9a22eaa36217a482e6b6184420d709e
+T *branch * sqllog
+T *sym-sqllog *
+T -sym-trunk *
+U dan
+Z 0aa6ee962f53d4f3c0dddb8a9d2ec33e
index 6a072bfe5932dc3d14ea8ed6f0c0383683aceb5d..25b4f98be53448c191547b55db4f17ca8415ddbf 100644 (file)
@@ -1 +1 @@
-d4c36d4991b048133efb21b251ab57fa66764d9d
\ No newline at end of file
+cd501bbccf3e62b002317592cc331770b32c129a
\ No newline at end of file
index dc86e1e081339d6a09ddd1ca2fec754100a24558..f5da7e7f1f42619e98602cca8a98d33dff72fb0f 100644 (file)
@@ -175,6 +175,10 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = {
    0,                         /* xLog */
    0,                         /* pLogArg */
    0,                         /* bLocaltimeFault */
+#ifdef SQLITE_ENABLE_SQLLOG
+   0,                         /* xSqllog */
+   0                          /* pSqllogArg */
+#endif
 };
 
 
index b2826c0c7b97ff7bbe919d81a237393706cd1880..7ef10605b5c3e1b42ac44aae5327fa3f126ff448 100644 (file)
@@ -132,6 +132,13 @@ int sqlite3_initialize(void){
   */
   if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
 
+#ifdef SQLITE_ENABLE_SQLLOG
+  {
+    extern sqlite3_init_sqllog(void);
+    sqlite3_init_sqllog();
+  }
+#endif
+
   /* Make sure the mutex subsystem is initialized.  If unable to 
   ** initialize the mutex subsystem, return early with the error.
   ** If the system is so sick that we are unable to allocate a mutex,
@@ -480,6 +487,15 @@ int sqlite3_config(int op, ...){
       break;
     }
 
+#ifdef SQLITE_ENABLE_SQLLOG
+    case SQLITE_CONFIG_SQLLOG: {
+      typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
+      sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
+      sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
+      break;
+    }
+#endif
+
     default: {
       rc = SQLITE_ERROR;
       break;
@@ -819,6 +835,12 @@ static int sqlite3Close(sqlite3 *db, int forceZombie){
     return SQLITE_BUSY;
   }
 
+#ifdef SQLITE_ENABLE_SQLLOG
+  if( sqlite3GlobalConfig.xSqllog ){
+    sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 0);
+  }
+#endif
+
   /* Convert the connection into a zombie and then close it.
   */
   db->magic = SQLITE_MAGIC_ZOMBIE;
@@ -2451,6 +2473,13 @@ opendb_out:
     db->magic = SQLITE_MAGIC_SICK;
   }
   *ppDb = db;
+#ifdef SQLITE_ENABLE_SQLLOG
+  if( sqlite3GlobalConfig.xSqllog ){
+    sqlite3GlobalConfig.xSqllog(
+        sqlite3GlobalConfig.pSqllogArg, db, zFilename, -1
+    );
+  }
+#endif
   return sqlite3ApiExit(0, rc);
 }
 
index 520cccd04248c1d47dfa911d4e7481649cf44462..9770a7fc03ad89bc610ed2406bb102d06be92338 100644 (file)
@@ -1593,6 +1593,14 @@ struct sqlite3_mem_methods {
 ** <dd> These options are obsolete and should not be used by new code.
 ** They are retained for backwards compatibility but are now no-ops.
 ** </dl>
+**
+** [[SQLITE_CONFIG_SQLLOG]]
+** <dt>SQLITE_CONFIG_SQLLOG
+** <dd>This option is only available if sqlite is compiled with the
+** SQLITE_ENABLE_SQLLOG pre-processor macro defined. The first argument should
+** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
+** The second should be of type (void*).
+** </dl>
 */
 #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
 #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
@@ -1614,6 +1622,7 @@ struct sqlite3_mem_methods {
 #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
 #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
+#define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
 
 /*
 ** CAPI3REF: Database Connection Configuration Options
index eb779253c98344594f1a1ac33d45fedcd18f4a6c..14dce8ed7c8f53b51652aa391b2816b828088a00 100644 (file)
@@ -2515,6 +2515,10 @@ struct Sqlite3Config {
   void (*xLog)(void*,int,const char*); /* Function for logging */
   void *pLogArg;                       /* First argument to xLog() */
   int bLocaltimeFault;              /* True to fail localtime() calls */
+#ifdef SQLITE_ENABLE_SQLLOG
+  void(*xSqllog)(void*,sqlite3*,const char*, int);
+  void *pSqllogArg;
+#endif
 };
 
 /*
diff --git a/src/test_sqllog.c b/src/test_sqllog.c
new file mode 100644 (file)
index 0000000..ba03670
--- /dev/null
@@ -0,0 +1,433 @@
+/*
+** 2012 November 26
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+**
+** OVERVIEW
+**
+**   This file contains experimental code used to record data from live
+**   SQLite applications that may be useful for offline analysis. Specifically:
+**
+**     1) The initial contents of all database files opened by the 
+**        application, and
+**
+**     2) All SQL statements executed by the application.
+**
+** USAGE
+**
+**   To use this module, SQLite must be compiled with the SQLITE_ENABLE_SQLLOG
+**   pre-processor symbol defined and this file linked into the application
+**   somehow.
+**
+**   At runtime, logging is enabled by setting environment variable
+**   SQLITE_SQLLOG_DIR to the name of a directory in which to store logged 
+**   data. The directory must already exist.
+**
+**   Usually, if the application opens the same database file more than once
+**   (either by attaching it or by using more than one database handle), only
+**   a single copy is made. This behaviour may be overridden (so that a 
+**   separate copy is taken each time the database file is opened or attached)
+**   by setting the environment variable SQLITE_SQLLOG_REUSE_FILES to 0.
+**
+** OUTPUT:
+**
+**   The SQLITE_SQLLOG_DIR is populated with three types of files:
+**
+**      sqllog_N.db   - Copies of database files. N may be any integer.
+**
+**      sqllog_N.sql  - A list of SQL statements executed by a single
+**                      connection. N may be any integer.
+**
+**      sqllog.idx    - An index mapping from integer N to a database
+**                      file name - indicating the full path of the
+**                      database from which sqllog_N.db was copied.
+*/
+
+#include "sqlite3.h"
+#include "stdio.h"
+#include "stdlib.h"
+#include "string.h"
+#include "assert.h"
+
+#define ENVIRONMENT_VARIABLE1_NAME "SQLITE_SQLLOG_DIR"
+#define ENVIRONMENT_VARIABLE2_NAME "SQLITE_SQLLOG_REUSE_FILES"
+
+/* Assume that all database and database file names are shorted than this. */
+#define SQLLOG_NAMESZ 512
+
+/* Maximum number of simultaneous database connections the process may
+** open (if any more are opened an error is logged using sqlite3_log()
+** and processing is halted).
+*/
+#define MAX_CONNECTIONS 256
+
+struct SLConn {
+  int isErr;                      /* True if an error has occurred */
+  sqlite3 *db;                    /* Connection handle */
+  int iLog;                       /* First integer value used in file names */
+  FILE *fd;                       /* File descriptor for log file */
+};
+
+struct SLGlobal {
+  /* Protected by MUTEX_STATIC_MASTER */
+  sqlite3_mutex *mutex;           /* Recursive mutex */
+  int nConn;                      /* Size of aConn[] array */
+
+  /* Protected by SLGlobal.mutex */
+  int bReuse;                     /* True to avoid extra copies of db files */
+  char zDir[SQLLOG_NAMESZ];       /* Directory to create files in */
+  char zIdx[SQLLOG_NAMESZ];       /* Full path to *.idx file */
+  int iNextLog;                   /* Used to allocate file names */
+  int iNextDb;                    /* Used to allocate database file names */
+  int bRec;                       /* True if testSqllog() is called rec. */
+  int iClock;                     /* Clock value */
+  struct SLConn aConn[MAX_CONNECTIONS];
+} sqllogglobal;
+
+/*
+** Return true if c is an ASCII whitespace character.
+*/
+static int sqllog_isspace(char c){
+  return (c==' ' || c=='\t' || c=='\n' || c=='\v' || c=='\f' || c=='\r');
+}
+
+/*
+** The first argument points to a nul-terminated string containing an SQL
+** command. Before returning, this function sets *pz to point to the start
+** of the first token in this command, and *pn to the number of bytes in 
+** the token. This is used to check if the SQL command is an "ATTACH" or 
+** not.
+*/
+static void sqllogTokenize(const char *z, const char **pz, int *pn){
+  const char *p = z;
+  int n;
+
+  /* Skip past any whitespace */
+  while( sqllog_isspace(*p) ){
+    p++;
+  }
+
+  /* Figure out how long the first token is */
+  *pz = p;
+  n = 0;
+  while( (p[n]>='a' && p[n]<='z') || (p[n]>='A' && p[n]<='Z') ) n++;
+  *pn = n;
+}
+
+/*
+** Check if the logs directory already contains a copy of database file 
+** zFile. If so, return a pointer to the full path of the copy. Otherwise,
+** return NULL.
+**
+** If a non-NULL value is returned, then the caller must arrange to 
+** eventually free it using sqlite3_free().
+*/
+static char *sqllogFindFile(const char *zFile){
+  char *zRet = 0;
+  FILE *fd = 0;
+
+  /* Open the index file for reading */
+  fd = fopen(sqllogglobal.zIdx, "r");
+
+  while( feof(fd)==0 ){
+    char *z;
+    int n;
+    char zLine[SQLLOG_NAMESZ*2+5];
+
+    fgets(zLine, sizeof(zLine), fd);
+    z = zLine;
+    while( *z>='0' && *z<='9' ) z++;
+    while( *z==' ' ) z++;
+
+    n = strlen(z);
+    while( n>0 && sqllog_isspace(z[n-1]) ) n--;
+    if( n==strlen(zFile) && 0==memcmp(zFile, z, n) ){
+      char zBuf[16];
+      z = zLine;
+      while( *z>='0' && *z<='9' ){
+        zBuf[z-zLine] = *z;
+        z++;
+      }
+      zRet = sqlite3_mprintf("%s/sqllog_%s.db", sqllogglobal.zDir, zBuf);
+      break;
+    }
+  }
+
+  fclose(fd);
+  return zRet;
+}
+
+static void sqllogFindAttached(
+  struct SLConn *p,               /* Database connection */
+  const char *zSearch,            /* Name to search for (or NULL) */
+  char *zName,                    /* OUT: Name of attached database */
+  char *zFile                     /* OUT: Name of attached file */
+){
+  sqlite3_stmt *pStmt;
+  int rc;
+
+  /* The "PRAGMA database_list" command returns a list of databases in the
+  ** order that they were attached. So a newly attached database is 
+  ** described by the last row returned.  */
+  assert( sqllogglobal.bRec==0 );
+  sqllogglobal.bRec = 1;
+  rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
+  if( rc==SQLITE_OK ){
+    while( SQLITE_ROW==sqlite3_step(pStmt) ){
+      char *zVal1; int nVal1;
+      char *zVal2; int nVal2;
+
+      zVal1 = sqlite3_column_text(pStmt, 1);
+      nVal1 = sqlite3_column_bytes(pStmt, 1);
+      memcpy(zName, zVal1, nVal1+1);
+
+      zVal2 = sqlite3_column_text(pStmt, 2);
+      nVal2 = sqlite3_column_bytes(pStmt, 2);
+      memcpy(zFile, zVal2, nVal2+1);
+
+      if( zSearch && strlen(zSearch)==nVal1 
+       && 0==sqlite3_strnicmp(zSearch, zVal1, nVal1)
+      ){
+        break;
+      }
+    }
+    rc = sqlite3_finalize(pStmt);
+  }
+  sqllogglobal.bRec = 0;
+}
+
+
+/*
+** Parameter zSearch is the name of a database attached to the database 
+** connection associated with the first argument. This function creates
+** a backup of this database in the logs directory.
+**
+** The name used for the backup file is automatically generated. Call
+** it zFile.
+**
+** If the bLog parameter is true, then a statement of the following form
+** is written to the log file associated with *p:
+**
+**    ATTACH 'zFile' AS 'zName';
+**
+** Otherwise, if bLog is false, a comment is added to the log file:
+**
+**    -- Main database file is 'zFile'
+**
+** The SLGlobal.mutex mutex is always held when this function is called.
+*/
+static void sqllogCopydb(struct SLConn *p, const char *zSearch, int bLog){
+  char zName[SQLLOG_NAMESZ];      /* Attached database name */
+  char zFile[SQLLOG_NAMESZ];      /* Database file name */
+  char *zFree;
+  char *zInit = 0;
+
+  sqllogFindAttached(p, zSearch, zName, zFile);
+  if( zFile[0]=='\0' ){
+    zInit = sqlite3_mprintf("");
+  }else{
+    if( sqllogglobal.bReuse ){
+      zInit = sqllogFindFile(zFile);
+    }else{
+      zInit = 0;
+    }
+    if( zInit==0 ){
+      int rc;
+      sqlite3 *copy = 0;
+      FILE *fd;
+      int iDb;
+
+      /* Generate a file-name to use for the copy of this database */
+      iDb = sqllogglobal.iNextDb++;
+      zInit = sqlite3_mprintf("%s/sqllog_%d.db", sqllogglobal.zDir, iDb);
+
+      /* Create the backup */
+      assert( sqllogglobal.bRec==0 );
+      sqllogglobal.bRec = 1;
+      rc = sqlite3_open(zInit, &copy);
+      if( rc==SQLITE_OK ){
+        sqlite3_backup *pBak;
+        sqlite3_exec(copy, "PRAGMA synchronous = 0", 0, 0, 0);
+        pBak = sqlite3_backup_init(copy, "main", p->db, zName);
+        if( pBak ){
+          sqlite3_backup_step(pBak, -1);
+          sqlite3_backup_finish(pBak);
+        }
+        sqlite3_close(copy);
+      }
+      sqllogglobal.bRec = 0;
+
+      /* Write an entry into the database index file */
+      fd = fopen(sqllogglobal.zIdx, "a");
+      fprintf(fd, "%d %s\n", iDb, zFile);
+      fclose(fd);
+    }
+  }
+
+  if( bLog ){
+    zFree = sqlite3_mprintf("ATTACH '%q' AS '%q'; -- clock=%d steps=1\n", 
+        zInit, zName, sqllogglobal.iClock++
+    );
+  }else{
+    zFree = sqlite3_mprintf("-- Main database is '%q'\n", zInit);
+  }
+  fprintf(p->fd, "%s", zFree);
+  sqlite3_free(zFree);
+
+  sqlite3_free(zInit);
+}
+
+/*
+** If it is not already open, open the log file for connection *p. 
+**
+** The SLGlobal.mutex mutex is always held when this function is called.
+*/
+static void sqllogOpenlog(struct SLConn *p){
+  /* If the log file has not yet been opened, open it now. */
+  if( p->fd==0 ){
+    char *zLog;
+
+    /* If it is still NULL, have global.zDir point to a copy of environment
+    ** variable $ENVIRONMENT_VARIABLE1_NAME.  */
+    if( sqllogglobal.zDir[0]==0 ){
+      FILE *fd;
+      char *zVar = getenv(ENVIRONMENT_VARIABLE1_NAME);
+      if( zVar==0 || strlen(zVar)>=(sizeof(sqllogglobal.zDir)) ) return;
+      memcpy(sqllogglobal.zDir, zVar, strlen(zVar)+1);
+      sprintf(sqllogglobal.zIdx, "%s/sqllog.idx", sqllogglobal.zDir);
+      if( getenv(ENVIRONMENT_VARIABLE2_NAME) ){
+        sqllogglobal.bReuse = atoi(getenv(ENVIRONMENT_VARIABLE2_NAME));
+      }
+      fd = fopen(sqllogglobal.zIdx, "w");
+      close(fd);
+    }
+
+    /* Open the log file */
+    zLog = sqlite3_mprintf("%s/sqllog_%d.sql", sqllogglobal.zDir, p->iLog);
+    p->fd = fopen(zLog, "w");
+    assert( p->fd );
+    sqlite3_free(zLog);
+  }
+}
+
+/*
+** This function is called if the SQLLOG callback is invoked to report
+** execution of an SQL statement. Parameter p is the connection the statement
+** was executed by, parameter zSql is the text of the statement itself and
+** parameter nStep is the number of times sqlite3_step() was called.
+*/
+static void testSqllogStmt(struct SLConn *p, const char *zSql, int nStep){
+  const char *zFirst;             /* Pointer to first token in zSql */
+  int nFirst;                     /* Size of token zFirst in bytes */
+
+  assert( nStep>0 );
+
+  sqllogTokenize(zSql, &zFirst, &nFirst);
+  if( nFirst!=6 || 0!=sqlite3_strnicmp("ATTACH", zFirst, 6) ){
+    /* Not an ATTACH statement. Write this directly to the log. */
+    fprintf(p->fd, "%s; -- clock=%d steps=%d\n", 
+        zSql, sqllogglobal.iClock++, nStep
+    );
+  }else{
+    /* This is an ATTACH statement. Copy the database. */
+    sqllogCopydb(p, 0, 1);
+  }
+}
+
+/*
+** The SQLITE_CONFIG_SQLLOG callback registered by sqlite3_init_sqllog().
+*/
+static void testSqllog(void *pCtx, sqlite3 *db, const char *zSql, int nStep){
+  struct SLConn *p;
+  sqlite3_mutex *master = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+
+#if 0
+  if( sqllogglobal.isErr && zSql ){
+    /* If an error has already occurred, ignore all callbacks except
+    ** those reporting calls to sqlite3_close().  */
+    return;
+  }
+#endif
+
+  /* This is a database open command. */
+  if( zSql && nStep<0 ){
+    sqlite3_mutex_enter(master);
+    if( sqllogglobal.mutex==0 ){
+      sqllogglobal.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
+    }
+    p = &sqllogglobal.aConn[sqllogglobal.nConn++];
+    p->fd = 0;
+    p->db = db;
+    p->iLog = sqllogglobal.iNextLog++;
+    sqlite3_mutex_leave(master);
+
+    /* Open the log and take a copy of the main database file */
+    sqlite3_mutex_enter(sqllogglobal.mutex);
+    if( sqllogglobal.bRec==0 ){
+      sqllogOpenlog(p);
+      sqllogCopydb(p, "main", 0);
+    }
+    sqlite3_mutex_leave(sqllogglobal.mutex);
+  }
+
+  else{
+
+    int i;
+    for(i=0; i<sqllogglobal.nConn; i++){
+      p = &sqllogglobal.aConn[i];
+      if( p->db==db ) break;
+    }
+    if( i==sqllogglobal.nConn ) return;
+
+    /* A database handle close command */
+    if( zSql==0 ){
+      sqlite3_mutex_enter(master);
+      if( p->fd ) fclose(p->fd);
+      p->db = 0;
+      p->fd = 0;
+
+      sqllogglobal.nConn--;
+      if( sqllogglobal.nConn==0 ){
+        sqlite3_mutex_free(sqllogglobal.mutex);
+        sqllogglobal.mutex = 0;
+      }else{
+        int nShift = &sqllogglobal.aConn[sqllogglobal.nConn] - p;
+        if( nShift>0 ){
+          memmove(p, &p[1], nShift*sizeof(struct SLConn));
+        }
+      }
+      sqlite3_mutex_leave(master);
+
+    /* An ordinary SQL command. */
+    }else{
+      sqlite3_mutex_enter(sqllogglobal.mutex);
+      if( sqllogglobal.bRec==0 ){
+        testSqllogStmt(p, zSql, nStep);
+      }
+      sqlite3_mutex_leave(sqllogglobal.mutex);
+    }
+  }
+}
+
+/*
+** This function is called either before sqlite3_initialized() or by it.
+** It checks if the SQLITE_SQLLOG_DIR variable is defined, and if so 
+** registers an SQLITE_CONFIG_SQLLOG callback to record the applications
+** database activity.
+*/
+void sqlite3_init_sqllog(void){
+  if( getenv(ENVIRONMENT_VARIABLE1_NAME) ){
+    if( SQLITE_OK==sqlite3_config(SQLITE_CONFIG_SQLLOG, testSqllog, 0) ){
+      memset(&sqllogglobal, 0, sizeof(sqllogglobal));
+      sqllogglobal.bReuse = 1;
+    }
+  }
+}
+
index efcc7476c054e7903decf2a8315134b6b9c8298f..286c733592a78a3809a5bac75f1001935183fa18 100644 (file)
@@ -53,7 +53,7 @@ Vdbe *sqlite3VdbeCreate(sqlite3 *db){
 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
   assert( isPrepareV2==1 || isPrepareV2==0 );
   if( p==0 ) return;
-#ifdef SQLITE_OMIT_TRACE
+#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
   if( !isPrepareV2 ) return;
 #endif
   assert( p->zSql==0 );
@@ -2326,6 +2326,27 @@ int sqlite3VdbeTransferError(Vdbe *p){
   return rc;
 }
 
+#ifdef SQLITE_ENABLE_SQLLOG
+/*
+** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, 
+** invoke it.
+*/
+static void vdbeInvokeSqllog(Vdbe *v){
+  if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
+    char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
+    assert( v->db->init.busy==0 );
+    if( zExpanded ){
+      sqlite3GlobalConfig.xSqllog(
+          sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
+      );
+      sqlite3DbFree(v->db, zExpanded);
+    }
+  }
+}
+#else
+# define vdbeInvokeSqllog(x)
+#endif
+
 /*
 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
 ** Write any error messages into *pzErrMsg.  Return the result code.
@@ -2353,6 +2374,7 @@ int sqlite3VdbeReset(Vdbe *p){
   ** instructions yet, leave the main database error information unchanged.
   */
   if( p->pc>=0 ){
+    vdbeInvokeSqllog(p);
     sqlite3VdbeTransferError(p);
     sqlite3DbFree(db, p->zErrMsg);
     p->zErrMsg = 0;