xref: /sqlite-3.40.0/src/test_sqllog.c (revision b9830a14)
1ac455939Sdan /*
2ac455939Sdan ** 2012 November 26
3ac455939Sdan **
4ac455939Sdan ** The author disclaims copyright to this source code.  In place of
5ac455939Sdan ** a legal notice, here is a blessing:
6ac455939Sdan **
7ac455939Sdan **    May you do good and not evil.
8ac455939Sdan **    May you find forgiveness for yourself and forgive others.
9ac455939Sdan **    May you share freely, never taking more than you give.
10ac455939Sdan **
11ac455939Sdan *************************************************************************
12ac455939Sdan **
13ac455939Sdan ** OVERVIEW
14ac455939Sdan **
15ac455939Sdan **   This file contains experimental code used to record data from live
16*b9830a14Sdrh **   SQLite applications that may be useful for offline analysis.
17*b9830a14Sdrh **   Specifically, this module can be used to capture the following
18*b9830a14Sdrh **   information:
19ac455939Sdan **
20ac455939Sdan **     1) The initial contents of all database files opened by the
21ac455939Sdan **        application, and
22ac455939Sdan **
23ac455939Sdan **     2) All SQL statements executed by the application.
24ac455939Sdan **
25*b9830a14Sdrh **   The captured information can then be used to run (for example)
26*b9830a14Sdrh **   performance analysis looking for slow queries or to look for
27*b9830a14Sdrh **   optimization opportunities in either the application or in SQLite
28*b9830a14Sdrh **   itself.
29*b9830a14Sdrh **
30ac455939Sdan ** USAGE
31ac455939Sdan **
32ac455939Sdan **   To use this module, SQLite must be compiled with the SQLITE_ENABLE_SQLLOG
33*b9830a14Sdrh **   pre-processor symbol defined and this file linked into the application.
34*b9830a14Sdrh **   One way to link this file into the application is to append the content
35*b9830a14Sdrh **   of this file onto the end of the "sqlite3.c" amalgamation and then
36*b9830a14Sdrh **   recompile the application as normal except with the addition  of the
37*b9830a14Sdrh **   -DSQLITE_ENABLE_SQLLOG option.
38ac455939Sdan **
39ac455939Sdan **   At runtime, logging is enabled by setting environment variable
40ac455939Sdan **   SQLITE_SQLLOG_DIR to the name of a directory in which to store logged
41*b9830a14Sdrh **   data. The logging directory must already exist.
42ac455939Sdan **
43ac455939Sdan **   Usually, if the application opens the same database file more than once
44ac455939Sdan **   (either by attaching it or by using more than one database handle), only
4548864df9Smistachkin **   a single copy is made. This behavior may be overridden (so that a
46ac455939Sdan **   separate copy is taken each time the database file is opened or attached)
47ac455939Sdan **   by setting the environment variable SQLITE_SQLLOG_REUSE_FILES to 0.
48ac455939Sdan **
49ac455939Sdan ** OUTPUT:
50ac455939Sdan **
51ac455939Sdan **   The SQLITE_SQLLOG_DIR is populated with three types of files:
52ac455939Sdan **
53ac455939Sdan **      sqllog_N.db   - Copies of database files. N may be any integer.
54ac455939Sdan **
55ac455939Sdan **      sqllog_N.sql  - A list of SQL statements executed by a single
56ac455939Sdan **                      connection. N may be any integer.
57ac455939Sdan **
58ac455939Sdan **      sqllog.idx    - An index mapping from integer N to a database
59ac455939Sdan **                      file name - indicating the full path of the
60ac455939Sdan **                      database from which sqllog_N.db was copied.
6171ba10d3Sdan **
6271ba10d3Sdan ** ERROR HANDLING:
6371ba10d3Sdan **
6471ba10d3Sdan **   This module attempts to make a best effort to continue logging if an
6571ba10d3Sdan **   IO or other error is encountered. For example, if a log file cannot
6671ba10d3Sdan **   be opened logs are not collected for that connection, but other
6771ba10d3Sdan **   logging proceeds as expected. Errors are logged by calling sqlite3_log().
68ac455939Sdan */
69ac455939Sdan 
70*b9830a14Sdrh #ifndef _SQLITE3_H_
71ac455939Sdan #include "sqlite3.h"
72*b9830a14Sdrh #endif
73*b9830a14Sdrh #include <stdio.h>
74*b9830a14Sdrh #include <stdlib.h>
75*b9830a14Sdrh #include <string.h>
76*b9830a14Sdrh #include <assert.h>
77ac455939Sdan 
78*b9830a14Sdrh #include <sys/types.h>
79*b9830a14Sdrh #include <unistd.h>
8071ba10d3Sdan static int getProcessId(void){
817d1f0c79Sdrh #if SQLITE_OS_WIN
827d1f0c79Sdrh   return (int)_getpid();
837d1f0c79Sdrh #else
8471ba10d3Sdan   return (int)getpid();
857d1f0c79Sdrh #endif
8671ba10d3Sdan }
8771ba10d3Sdan 
88*b9830a14Sdrh /* Names of environment variables to be used */
89ac455939Sdan #define ENVIRONMENT_VARIABLE1_NAME "SQLITE_SQLLOG_DIR"
90ac455939Sdan #define ENVIRONMENT_VARIABLE2_NAME "SQLITE_SQLLOG_REUSE_FILES"
91ac455939Sdan 
92ac455939Sdan /* Assume that all database and database file names are shorted than this. */
93ac455939Sdan #define SQLLOG_NAMESZ 512
94ac455939Sdan 
95ac455939Sdan /* Maximum number of simultaneous database connections the process may
96ac455939Sdan ** open (if any more are opened an error is logged using sqlite3_log()
97ac455939Sdan ** and processing is halted).
98ac455939Sdan */
99ac455939Sdan #define MAX_CONNECTIONS 256
100ac455939Sdan 
101*b9830a14Sdrh /* There is one instance of this object for each SQLite database connection
102*b9830a14Sdrh ** that is being logged.
103*b9830a14Sdrh */
104ac455939Sdan struct SLConn {
105ac455939Sdan   int isErr;                      /* True if an error has occurred */
106ac455939Sdan   sqlite3 *db;                    /* Connection handle */
107ac455939Sdan   int iLog;                       /* First integer value used in file names */
108ac455939Sdan   FILE *fd;                       /* File descriptor for log file */
109ac455939Sdan };
110ac455939Sdan 
111*b9830a14Sdrh /* This object is a singleton that keeps track of all data loggers.
112*b9830a14Sdrh */
113*b9830a14Sdrh static struct SLGlobal {
114ac455939Sdan   /* Protected by MUTEX_STATIC_MASTER */
115ac455939Sdan   sqlite3_mutex *mutex;           /* Recursive mutex */
116ac455939Sdan   int nConn;                      /* Size of aConn[] array */
117ac455939Sdan 
118ac455939Sdan   /* Protected by SLGlobal.mutex */
119ac455939Sdan   int bReuse;                     /* True to avoid extra copies of db files */
12071ba10d3Sdan   char zPrefix[SQLLOG_NAMESZ];    /* Prefix for all created files */
121ac455939Sdan   char zIdx[SQLLOG_NAMESZ];       /* Full path to *.idx file */
122ac455939Sdan   int iNextLog;                   /* Used to allocate file names */
123ac455939Sdan   int iNextDb;                    /* Used to allocate database file names */
124ac455939Sdan   int bRec;                       /* True if testSqllog() is called rec. */
125ac455939Sdan   int iClock;                     /* Clock value */
126ac455939Sdan   struct SLConn aConn[MAX_CONNECTIONS];
127ac455939Sdan } sqllogglobal;
128ac455939Sdan 
129ac455939Sdan /*
130ac455939Sdan ** Return true if c is an ASCII whitespace character.
131ac455939Sdan */
132ac455939Sdan static int sqllog_isspace(char c){
133ac455939Sdan   return (c==' ' || c=='\t' || c=='\n' || c=='\v' || c=='\f' || c=='\r');
134ac455939Sdan }
135ac455939Sdan 
136ac455939Sdan /*
137ac455939Sdan ** The first argument points to a nul-terminated string containing an SQL
138ac455939Sdan ** command. Before returning, this function sets *pz to point to the start
139ac455939Sdan ** of the first token in this command, and *pn to the number of bytes in
140ac455939Sdan ** the token. This is used to check if the SQL command is an "ATTACH" or
141ac455939Sdan ** not.
142ac455939Sdan */
143ac455939Sdan static void sqllogTokenize(const char *z, const char **pz, int *pn){
144ac455939Sdan   const char *p = z;
145ac455939Sdan   int n;
146ac455939Sdan 
147ac455939Sdan   /* Skip past any whitespace */
148ac455939Sdan   while( sqllog_isspace(*p) ){
149ac455939Sdan     p++;
150ac455939Sdan   }
151ac455939Sdan 
152ac455939Sdan   /* Figure out how long the first token is */
153ac455939Sdan   *pz = p;
154ac455939Sdan   n = 0;
155ac455939Sdan   while( (p[n]>='a' && p[n]<='z') || (p[n]>='A' && p[n]<='Z') ) n++;
156ac455939Sdan   *pn = n;
157ac455939Sdan }
158ac455939Sdan 
159ac455939Sdan /*
160ac455939Sdan ** Check if the logs directory already contains a copy of database file
161ac455939Sdan ** zFile. If so, return a pointer to the full path of the copy. Otherwise,
162ac455939Sdan ** return NULL.
163ac455939Sdan **
164ac455939Sdan ** If a non-NULL value is returned, then the caller must arrange to
165ac455939Sdan ** eventually free it using sqlite3_free().
166ac455939Sdan */
167ac455939Sdan static char *sqllogFindFile(const char *zFile){
168ac455939Sdan   char *zRet = 0;
169ac455939Sdan   FILE *fd = 0;
170ac455939Sdan 
171ac455939Sdan   /* Open the index file for reading */
172ac455939Sdan   fd = fopen(sqllogglobal.zIdx, "r");
17371ba10d3Sdan   if( fd==0 ){
17471ba10d3Sdan     sqlite3_log(SQLITE_IOERR, "sqllogFindFile(): error in fopen()");
17571ba10d3Sdan     return 0;
17671ba10d3Sdan   }
177ac455939Sdan 
17871ba10d3Sdan   /* Loop through each entry in the index file. If zFile is not NULL and the
17971ba10d3Sdan   ** entry is a match, then set zRet to point to the filename of the existing
18071ba10d3Sdan   ** copy and break out of the loop.  */
181ac455939Sdan   while( feof(fd)==0 ){
182ac455939Sdan     char zLine[SQLLOG_NAMESZ*2+5];
18371ba10d3Sdan     if( fgets(zLine, sizeof(zLine), fd) ){
18471ba10d3Sdan       int n;
18571ba10d3Sdan       char *z;
186ac455939Sdan 
18771ba10d3Sdan       zLine[sizeof(zLine)-1] = '\0';
188ac455939Sdan       z = zLine;
189ac455939Sdan       while( *z>='0' && *z<='9' ) z++;
190ac455939Sdan       while( *z==' ' ) z++;
191ac455939Sdan 
192ac455939Sdan       n = strlen(z);
193ac455939Sdan       while( n>0 && sqllog_isspace(z[n-1]) ) n--;
19471ba10d3Sdan 
195ac455939Sdan       if( n==strlen(zFile) && 0==memcmp(zFile, z, n) ){
196ac455939Sdan         char zBuf[16];
19771ba10d3Sdan         memset(zBuf, 0, sizeof(zBuf));
198ac455939Sdan         z = zLine;
199ac455939Sdan         while( *z>='0' && *z<='9' ){
200ac455939Sdan           zBuf[z-zLine] = *z;
201ac455939Sdan           z++;
202ac455939Sdan         }
20371ba10d3Sdan         zRet = sqlite3_mprintf("%s_%s.db", sqllogglobal.zPrefix, zBuf);
204ac455939Sdan         break;
205ac455939Sdan       }
206ac455939Sdan     }
20771ba10d3Sdan   }
20871ba10d3Sdan 
20971ba10d3Sdan   if( ferror(fd) ){
21071ba10d3Sdan     sqlite3_log(SQLITE_IOERR, "sqllogFindFile(): error reading index file");
21171ba10d3Sdan   }
212ac455939Sdan 
213ac455939Sdan   fclose(fd);
214ac455939Sdan   return zRet;
215ac455939Sdan }
216ac455939Sdan 
21771ba10d3Sdan static int sqllogFindAttached(
218ac455939Sdan   struct SLConn *p,               /* Database connection */
219ac455939Sdan   const char *zSearch,            /* Name to search for (or NULL) */
220ac455939Sdan   char *zName,                    /* OUT: Name of attached database */
221ac455939Sdan   char *zFile                     /* OUT: Name of attached file */
222ac455939Sdan ){
223ac455939Sdan   sqlite3_stmt *pStmt;
224ac455939Sdan   int rc;
225ac455939Sdan 
226ac455939Sdan   /* The "PRAGMA database_list" command returns a list of databases in the
227ac455939Sdan   ** order that they were attached. So a newly attached database is
228ac455939Sdan   ** described by the last row returned.  */
229ac455939Sdan   assert( sqllogglobal.bRec==0 );
230ac455939Sdan   sqllogglobal.bRec = 1;
231ac455939Sdan   rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
232ac455939Sdan   if( rc==SQLITE_OK ){
233ac455939Sdan     while( SQLITE_ROW==sqlite3_step(pStmt) ){
2347d1f0c79Sdrh       const char *zVal1; int nVal1;
2357d1f0c79Sdrh       const char *zVal2; int nVal2;
236ac455939Sdan 
2377d1f0c79Sdrh       zVal1 = (const char*)sqlite3_column_text(pStmt, 1);
238ac455939Sdan       nVal1 = sqlite3_column_bytes(pStmt, 1);
239ac455939Sdan       memcpy(zName, zVal1, nVal1+1);
240ac455939Sdan 
2417d1f0c79Sdrh       zVal2 = (const char*)sqlite3_column_text(pStmt, 2);
242ac455939Sdan       nVal2 = sqlite3_column_bytes(pStmt, 2);
243ac455939Sdan       memcpy(zFile, zVal2, nVal2+1);
244ac455939Sdan 
245ac455939Sdan       if( zSearch && strlen(zSearch)==nVal1
246ac455939Sdan        && 0==sqlite3_strnicmp(zSearch, zVal1, nVal1)
247ac455939Sdan       ){
248ac455939Sdan         break;
249ac455939Sdan       }
250ac455939Sdan     }
251ac455939Sdan     rc = sqlite3_finalize(pStmt);
252ac455939Sdan   }
253ac455939Sdan   sqllogglobal.bRec = 0;
25471ba10d3Sdan 
25571ba10d3Sdan   if( rc!=SQLITE_OK ){
25671ba10d3Sdan     sqlite3_log(rc, "sqllogFindAttached(): error in \"PRAGMA database_list\"");
25771ba10d3Sdan   }
25871ba10d3Sdan   return rc;
259ac455939Sdan }
260ac455939Sdan 
261ac455939Sdan 
262ac455939Sdan /*
263ac455939Sdan ** Parameter zSearch is the name of a database attached to the database
264ac455939Sdan ** connection associated with the first argument. This function creates
265ac455939Sdan ** a backup of this database in the logs directory.
266ac455939Sdan **
267ac455939Sdan ** The name used for the backup file is automatically generated. Call
268ac455939Sdan ** it zFile.
269ac455939Sdan **
270ac455939Sdan ** If the bLog parameter is true, then a statement of the following form
271ac455939Sdan ** is written to the log file associated with *p:
272ac455939Sdan **
273ac455939Sdan **    ATTACH 'zFile' AS 'zName';
274ac455939Sdan **
275ac455939Sdan ** Otherwise, if bLog is false, a comment is added to the log file:
276ac455939Sdan **
277ac455939Sdan **    -- Main database file is 'zFile'
278ac455939Sdan **
279ac455939Sdan ** The SLGlobal.mutex mutex is always held when this function is called.
280ac455939Sdan */
281ac455939Sdan static void sqllogCopydb(struct SLConn *p, const char *zSearch, int bLog){
282ac455939Sdan   char zName[SQLLOG_NAMESZ];      /* Attached database name */
283ac455939Sdan   char zFile[SQLLOG_NAMESZ];      /* Database file name */
284ac455939Sdan   char *zFree;
285ac455939Sdan   char *zInit = 0;
28671ba10d3Sdan   int rc;
287ac455939Sdan 
28871ba10d3Sdan   rc = sqllogFindAttached(p, zSearch, zName, zFile);
28971ba10d3Sdan   if( rc!=SQLITE_OK ) return;
29071ba10d3Sdan 
291ac455939Sdan   if( zFile[0]=='\0' ){
292ac455939Sdan     zInit = sqlite3_mprintf("");
293ac455939Sdan   }else{
294ac455939Sdan     if( sqllogglobal.bReuse ){
295ac455939Sdan       zInit = sqllogFindFile(zFile);
296ac455939Sdan     }else{
297ac455939Sdan       zInit = 0;
298ac455939Sdan     }
299ac455939Sdan     if( zInit==0 ){
300ac455939Sdan       int rc;
301ac455939Sdan       sqlite3 *copy = 0;
302ac455939Sdan       int iDb;
303ac455939Sdan 
304ac455939Sdan       /* Generate a file-name to use for the copy of this database */
305ac455939Sdan       iDb = sqllogglobal.iNextDb++;
30671ba10d3Sdan       zInit = sqlite3_mprintf("%s_%d.db", sqllogglobal.zPrefix, iDb);
307ac455939Sdan 
308ac455939Sdan       /* Create the backup */
309ac455939Sdan       assert( sqllogglobal.bRec==0 );
310ac455939Sdan       sqllogglobal.bRec = 1;
311ac455939Sdan       rc = sqlite3_open(zInit, &copy);
312ac455939Sdan       if( rc==SQLITE_OK ){
313ac455939Sdan         sqlite3_backup *pBak;
314ac455939Sdan         sqlite3_exec(copy, "PRAGMA synchronous = 0", 0, 0, 0);
315ac455939Sdan         pBak = sqlite3_backup_init(copy, "main", p->db, zName);
316ac455939Sdan         if( pBak ){
317ac455939Sdan           sqlite3_backup_step(pBak, -1);
31871ba10d3Sdan           rc = sqlite3_backup_finish(pBak);
31971ba10d3Sdan         }else{
32071ba10d3Sdan           rc = sqlite3_errcode(copy);
321ac455939Sdan         }
322ac455939Sdan         sqlite3_close(copy);
323ac455939Sdan       }
324ac455939Sdan       sqllogglobal.bRec = 0;
325ac455939Sdan 
32671ba10d3Sdan       if( rc==SQLITE_OK ){
327ac455939Sdan         /* Write an entry into the database index file */
32871ba10d3Sdan         FILE *fd = fopen(sqllogglobal.zIdx, "a");
32971ba10d3Sdan         if( fd ){
330ac455939Sdan           fprintf(fd, "%d %s\n", iDb, zFile);
331ac455939Sdan           fclose(fd);
332ac455939Sdan         }
33371ba10d3Sdan       }else{
33471ba10d3Sdan         sqlite3_log(rc, "sqllogCopydb(): error backing up database");
33571ba10d3Sdan       }
33671ba10d3Sdan     }
337ac455939Sdan   }
338ac455939Sdan 
339ac455939Sdan   if( bLog ){
34071ba10d3Sdan     zFree = sqlite3_mprintf("ATTACH '%q' AS '%q'; -- clock=%d\n",
341ac455939Sdan         zInit, zName, sqllogglobal.iClock++
342ac455939Sdan     );
343ac455939Sdan   }else{
344ac455939Sdan     zFree = sqlite3_mprintf("-- Main database is '%q'\n", zInit);
345ac455939Sdan   }
346ac455939Sdan   fprintf(p->fd, "%s", zFree);
347ac455939Sdan   sqlite3_free(zFree);
348ac455939Sdan 
349ac455939Sdan   sqlite3_free(zInit);
350ac455939Sdan }
351ac455939Sdan 
352ac455939Sdan /*
353ac455939Sdan ** If it is not already open, open the log file for connection *p.
354ac455939Sdan **
355ac455939Sdan ** The SLGlobal.mutex mutex is always held when this function is called.
356ac455939Sdan */
357ac455939Sdan static void sqllogOpenlog(struct SLConn *p){
358ac455939Sdan   /* If the log file has not yet been opened, open it now. */
359ac455939Sdan   if( p->fd==0 ){
360ac455939Sdan     char *zLog;
361ac455939Sdan 
36271ba10d3Sdan     /* If it is still NULL, have global.zPrefix point to a copy of
36371ba10d3Sdan     ** environment variable $ENVIRONMENT_VARIABLE1_NAME.  */
36471ba10d3Sdan     if( sqllogglobal.zPrefix[0]==0 ){
365ac455939Sdan       FILE *fd;
366ac455939Sdan       char *zVar = getenv(ENVIRONMENT_VARIABLE1_NAME);
36771ba10d3Sdan       if( zVar==0 || strlen(zVar)+10>=(sizeof(sqllogglobal.zPrefix)) ) return;
36871ba10d3Sdan       sprintf(sqllogglobal.zPrefix, "%s/sqllog_%d", zVar, getProcessId());
36971ba10d3Sdan       sprintf(sqllogglobal.zIdx, "%s.idx", sqllogglobal.zPrefix);
370ac455939Sdan       if( getenv(ENVIRONMENT_VARIABLE2_NAME) ){
371ac455939Sdan         sqllogglobal.bReuse = atoi(getenv(ENVIRONMENT_VARIABLE2_NAME));
372ac455939Sdan       }
373ac455939Sdan       fd = fopen(sqllogglobal.zIdx, "w");
37471ba10d3Sdan       if( fd ) fclose(fd);
375ac455939Sdan     }
376ac455939Sdan 
377ac455939Sdan     /* Open the log file */
37871ba10d3Sdan     zLog = sqlite3_mprintf("%s_%d.sql", sqllogglobal.zPrefix, p->iLog);
379ac455939Sdan     p->fd = fopen(zLog, "w");
380ac455939Sdan     sqlite3_free(zLog);
38171ba10d3Sdan     if( p->fd==0 ){
38271ba10d3Sdan       sqlite3_log(SQLITE_IOERR, "sqllogOpenlog(): Failed to open log file");
38371ba10d3Sdan     }
384ac455939Sdan   }
385ac455939Sdan }
386ac455939Sdan 
387ac455939Sdan /*
388ac455939Sdan ** This function is called if the SQLLOG callback is invoked to report
389ac455939Sdan ** execution of an SQL statement. Parameter p is the connection the statement
39071ba10d3Sdan ** was executed by and parameter zSql is the text of the statement itself.
391ac455939Sdan */
39271ba10d3Sdan static void testSqllogStmt(struct SLConn *p, const char *zSql){
393ac455939Sdan   const char *zFirst;             /* Pointer to first token in zSql */
394ac455939Sdan   int nFirst;                     /* Size of token zFirst in bytes */
395ac455939Sdan 
396ac455939Sdan   sqllogTokenize(zSql, &zFirst, &nFirst);
397ac455939Sdan   if( nFirst!=6 || 0!=sqlite3_strnicmp("ATTACH", zFirst, 6) ){
398ac455939Sdan     /* Not an ATTACH statement. Write this directly to the log. */
39971ba10d3Sdan     fprintf(p->fd, "%s; -- clock=%d\n", zSql, sqllogglobal.iClock++);
400ac455939Sdan   }else{
401ac455939Sdan     /* This is an ATTACH statement. Copy the database. */
402ac455939Sdan     sqllogCopydb(p, 0, 1);
403ac455939Sdan   }
404ac455939Sdan }
405ac455939Sdan 
406ac455939Sdan /*
407ac455939Sdan ** The SQLITE_CONFIG_SQLLOG callback registered by sqlite3_init_sqllog().
408*b9830a14Sdrh **
409*b9830a14Sdrh ** The eType parameter has the following values:
410*b9830a14Sdrh **
411*b9830a14Sdrh **    0:  Opening a new database connection.  zSql is the name of the
412*b9830a14Sdrh **        file being opened.  db is a pointer to the newly created database
413*b9830a14Sdrh **        connection.
414*b9830a14Sdrh **
415*b9830a14Sdrh **    1:  An SQL statement has run to completion.  zSql is the text of the
416*b9830a14Sdrh **        SQL statement with all parameters expanded to their actual values.
417*b9830a14Sdrh **
418*b9830a14Sdrh **    2:  Closing a database connection.  zSql is NULL.  The db pointer to
419*b9830a14Sdrh **        the database connection being closed has already been shut down
420*b9830a14Sdrh **        and cannot be used for any further SQL.
421*b9830a14Sdrh **
422*b9830a14Sdrh ** The pCtx parameter is a copy of the pointer that was originally passed
423*b9830a14Sdrh ** into the sqlite3_config(SQLITE_CONFIG_SQLLOG) statement.  In this
424*b9830a14Sdrh ** particular implementation, pCtx is always a pointer to the
425*b9830a14Sdrh ** sqllogglobal global variable define above.
426ac455939Sdan */
42771ba10d3Sdan static void testSqllog(void *pCtx, sqlite3 *db, const char *zSql, int eType){
428696b33e6Sdrh   struct SLConn *p = 0;
429ac455939Sdan   sqlite3_mutex *master = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
430ac455939Sdan 
43171ba10d3Sdan   assert( eType==0 || eType==1 || eType==2 );
43271ba10d3Sdan   assert( (eType==2)==(zSql==0) );
433ac455939Sdan 
434ac455939Sdan   /* This is a database open command. */
43571ba10d3Sdan   if( eType==0 ){
436ac455939Sdan     sqlite3_mutex_enter(master);
437ac455939Sdan     if( sqllogglobal.mutex==0 ){
438ac455939Sdan       sqllogglobal.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
439ac455939Sdan     }
440ac455939Sdan     p = &sqllogglobal.aConn[sqllogglobal.nConn++];
441ac455939Sdan     p->fd = 0;
442ac455939Sdan     p->db = db;
443ac455939Sdan     p->iLog = sqllogglobal.iNextLog++;
444ac455939Sdan     sqlite3_mutex_leave(master);
445ac455939Sdan 
446ac455939Sdan     /* Open the log and take a copy of the main database file */
447ac455939Sdan     sqlite3_mutex_enter(sqllogglobal.mutex);
448ac455939Sdan     if( sqllogglobal.bRec==0 ){
449ac455939Sdan       sqllogOpenlog(p);
45071ba10d3Sdan       if( p->fd ) sqllogCopydb(p, "main", 0);
451ac455939Sdan     }
452ac455939Sdan     sqlite3_mutex_leave(sqllogglobal.mutex);
453ac455939Sdan   }
454ac455939Sdan 
455ac455939Sdan   else{
456ac455939Sdan 
457ac455939Sdan     int i;
458ac455939Sdan     for(i=0; i<sqllogglobal.nConn; i++){
459ac455939Sdan       p = &sqllogglobal.aConn[i];
460ac455939Sdan       if( p->db==db ) break;
461ac455939Sdan     }
462ac455939Sdan     if( i==sqllogglobal.nConn ) return;
463ac455939Sdan 
464ac455939Sdan     /* A database handle close command */
46571ba10d3Sdan     if( eType==2 ){
466ac455939Sdan       sqlite3_mutex_enter(master);
467ac455939Sdan       if( p->fd ) fclose(p->fd);
468ac455939Sdan       p->db = 0;
469ac455939Sdan       p->fd = 0;
470ac455939Sdan 
471ac455939Sdan       sqllogglobal.nConn--;
472ac455939Sdan       if( sqllogglobal.nConn==0 ){
473ac455939Sdan         sqlite3_mutex_free(sqllogglobal.mutex);
474ac455939Sdan         sqllogglobal.mutex = 0;
475ac455939Sdan       }else{
476ac455939Sdan         int nShift = &sqllogglobal.aConn[sqllogglobal.nConn] - p;
477ac455939Sdan         if( nShift>0 ){
478ac455939Sdan           memmove(p, &p[1], nShift*sizeof(struct SLConn));
479ac455939Sdan         }
480ac455939Sdan       }
481ac455939Sdan       sqlite3_mutex_leave(master);
482ac455939Sdan 
483ac455939Sdan     /* An ordinary SQL command. */
48471ba10d3Sdan     }else if( p->fd ){
485ac455939Sdan       sqlite3_mutex_enter(sqllogglobal.mutex);
486ac455939Sdan       if( sqllogglobal.bRec==0 ){
48771ba10d3Sdan         testSqllogStmt(p, zSql);
488ac455939Sdan       }
489ac455939Sdan       sqlite3_mutex_leave(sqllogglobal.mutex);
490ac455939Sdan     }
491ac455939Sdan   }
492ac455939Sdan }
493ac455939Sdan 
494ac455939Sdan /*
495ac455939Sdan ** This function is called either before sqlite3_initialized() or by it.
496ac455939Sdan ** It checks if the SQLITE_SQLLOG_DIR variable is defined, and if so
497ac455939Sdan ** registers an SQLITE_CONFIG_SQLLOG callback to record the applications
498ac455939Sdan ** database activity.
499ac455939Sdan */
500ac455939Sdan void sqlite3_init_sqllog(void){
501ac455939Sdan   if( getenv(ENVIRONMENT_VARIABLE1_NAME) ){
502ac455939Sdan     if( SQLITE_OK==sqlite3_config(SQLITE_CONFIG_SQLLOG, testSqllog, 0) ){
503ac455939Sdan       memset(&sqllogglobal, 0, sizeof(sqllogglobal));
504ac455939Sdan       sqllogglobal.bReuse = 1;
505ac455939Sdan     }
506ac455939Sdan   }
507ac455939Sdan }
508