xref: /sqlite-3.40.0/src/test_vfstrace.c (revision 3fa97302)
1 /*
2 ** 2011 March 16
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** This file contains code implements a VFS shim that writes diagnostic
14 ** output for each VFS call, similar to "strace".
15 **
16 ** USAGE:
17 **
18 ** This source file exports a single symbol which is the name of a
19 ** function:
20 **
21 **   int vfstrace_register(
22 **     const char *zTraceName,         // Name of the newly constructed VFS
23 **     const char *zOldVfsName,        // Name of the underlying VFS
24 **     int (*xOut)(const char*,void*), // Output routine.  ex: fputs
25 **     void *pOutArg,                  // 2nd argument to xOut.  ex: stderr
26 **     int makeDefault                 // Make the new VFS the default
27 **   );
28 **
29 ** Applications that want to trace their VFS usage must provide a callback
30 ** function with this prototype:
31 **
32 **   int traceOutput(const char *zMessage, void *pAppData);
33 **
34 ** This function will "output" the trace messages, where "output" can
35 ** mean different things to different applications.  The traceOutput function
36 ** for the command-line shell (see shell.c) is "fputs" from the standard
37 ** library, which means that all trace output is written on the stream
38 ** specified by the second argument.  In the case of the command-line shell
39 ** the second argument is stderr.  Other applications might choose to output
40 ** trace information to a file, over a socket, or write it into a buffer.
41 **
42 ** The vfstrace_register() function creates a new "shim" VFS named by
43 ** the zTraceName parameter.  A "shim" VFS is an SQLite backend that does
44 ** not really perform the duties of a true backend, but simply filters or
45 ** interprets VFS calls before passing them off to another VFS which does
46 ** the actual work.  In this case the other VFS - the one that does the
47 ** real work - is identified by the second parameter, zOldVfsName.  If
48 ** the the 2nd parameter is NULL then the default VFS is used.  The common
49 ** case is for the 2nd parameter to be NULL.
50 **
51 ** The third and fourth parameters are the pointer to the output function
52 ** and the second argument to the output function.  For the SQLite
53 ** command-line shell, when the -vfstrace option is used, these parameters
54 ** are fputs and stderr, respectively.
55 **
56 ** The fifth argument is true (non-zero) to cause the newly created VFS
57 ** to become the default VFS.  The common case is for the fifth parameter
58 ** to be true.
59 **
60 ** The call to vfstrace_register() simply creates the shim VFS that does
61 ** tracing.  The application must also arrange to use the new VFS for
62 ** all database connections that are created and for which tracing is
63 ** desired.  This can be done by specifying the trace VFS using URI filename
64 ** notation, or by specifying the trace VFS as the 4th parameter to
65 ** sqlite3_open_v2() or by making the trace VFS be the default (by setting
66 ** the 5th parameter of vfstrace_register() to 1).
67 **
68 **
69 ** ENABLING VFSTRACE IN A COMMAND-LINE SHELL
70 **
71 ** The SQLite command line shell implemented by the shell.c source file
72 ** can be used with this module.  To compile in -vfstrace support, first
73 ** gather this file (test_vfstrace.c), the shell source file (shell.c),
74 ** and the SQLite amalgamation source files (sqlite3.c, sqlite3.h) into
75 ** the working directory.  Then compile using a command like the following:
76 **
77 **    gcc -o sqlite3 -Os -I. -DSQLITE_ENABLE_VFSTRACE \
78 **        -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE \
79 **        -DHAVE_READLINE -DHAVE_USLEEP=1 \
80 **        shell.c test_vfstrace.c sqlite3.c -ldl -lreadline -lncurses
81 **
82 ** The gcc command above works on Linux and provides (in addition to the
83 ** -vfstrace option) support for FTS3 and FTS4, RTREE, and command-line
84 ** editing using the readline library.  The command-line shell does not
85 ** use threads so we added -DSQLITE_THREADSAFE=0 just to make the code
86 ** run a little faster.   For compiling on a Mac, you'll probably need
87 ** to omit the -DHAVE_READLINE, the -lreadline, and the -lncurses options.
88 ** The compilation could be simplified to just this:
89 **
90 **    gcc -DSQLITE_ENABLE_VFSTRACE \
91 **         shell.c test_vfstrace.c sqlite3.c -ldl -lpthread
92 **
93 ** In this second example, all unnecessary options have been removed
94 ** Note that since the code is now threadsafe, we had to add the -lpthread
95 ** option to pull in the pthreads library.
96 **
97 ** To cross-compile for windows using MinGW, a command like this might
98 ** work:
99 **
100 **    /opt/mingw/bin/i386-mingw32msvc-gcc -o sqlite3.exe -Os -I \
101 **         -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_VFSTRACE \
102 **         shell.c test_vfstrace.c sqlite3.c
103 **
104 ** Similar compiler commands will work on different systems.  The key
105 ** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that
106 ** the shell.c source file will know to include the -vfstrace command-line
107 ** option and (2) you must compile and link the three source files
108 ** shell,c, test_vfstrace.c, and sqlite3.c.
109 */
110 #include <stdlib.h>
111 #include <string.h>
112 #include "sqlite3.h"
113 
114 /*
115 ** An instance of this structure is attached to the each trace VFS to
116 ** provide auxiliary information.
117 */
118 typedef struct vfstrace_info vfstrace_info;
119 struct vfstrace_info {
120   sqlite3_vfs *pRootVfs;              /* The underlying real VFS */
121   int (*xOut)(const char*, void*);    /* Send output here */
122   void *pOutArg;                      /* First argument to xOut */
123   const char *zVfsName;               /* Name of this trace-VFS */
124   sqlite3_vfs *pTraceVfs;             /* Pointer back to the trace VFS */
125 };
126 
127 /*
128 ** The sqlite3_file object for the trace VFS
129 */
130 typedef struct vfstrace_file vfstrace_file;
131 struct vfstrace_file {
132   sqlite3_file base;        /* Base class.  Must be first */
133   vfstrace_info *pInfo;     /* The trace-VFS to which this file belongs */
134   const char *zFName;       /* Base name of the file */
135   sqlite3_file *pReal;      /* The real underlying file */
136 };
137 
138 /*
139 ** Method declarations for vfstrace_file.
140 */
141 static int vfstraceClose(sqlite3_file*);
142 static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
143 static int vfstraceWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
144 static int vfstraceTruncate(sqlite3_file*, sqlite3_int64 size);
145 static int vfstraceSync(sqlite3_file*, int flags);
146 static int vfstraceFileSize(sqlite3_file*, sqlite3_int64 *pSize);
147 static int vfstraceLock(sqlite3_file*, int);
148 static int vfstraceUnlock(sqlite3_file*, int);
149 static int vfstraceCheckReservedLock(sqlite3_file*, int *);
150 static int vfstraceFileControl(sqlite3_file*, int op, void *pArg);
151 static int vfstraceSectorSize(sqlite3_file*);
152 static int vfstraceDeviceCharacteristics(sqlite3_file*);
153 static int vfstraceShmLock(sqlite3_file*,int,int,int);
154 static int vfstraceShmMap(sqlite3_file*,int,int,int, void volatile **);
155 static void vfstraceShmBarrier(sqlite3_file*);
156 static int vfstraceShmUnmap(sqlite3_file*,int);
157 
158 /*
159 ** Method declarations for vfstrace_vfs.
160 */
161 static int vfstraceOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
162 static int vfstraceDelete(sqlite3_vfs*, const char *zName, int syncDir);
163 static int vfstraceAccess(sqlite3_vfs*, const char *zName, int flags, int *);
164 static int vfstraceFullPathname(sqlite3_vfs*, const char *zName, int, char *);
165 static void *vfstraceDlOpen(sqlite3_vfs*, const char *zFilename);
166 static void vfstraceDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
167 static void (*vfstraceDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
168 static void vfstraceDlClose(sqlite3_vfs*, void*);
169 static int vfstraceRandomness(sqlite3_vfs*, int nByte, char *zOut);
170 static int vfstraceSleep(sqlite3_vfs*, int microseconds);
171 static int vfstraceCurrentTime(sqlite3_vfs*, double*);
172 static int vfstraceGetLastError(sqlite3_vfs*, int, char*);
173 static int vfstraceCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
174 static int vfstraceSetSystemCall(sqlite3_vfs*,const char*, sqlite3_syscall_ptr);
175 static sqlite3_syscall_ptr vfstraceGetSystemCall(sqlite3_vfs*, const char *);
176 static const char *vfstraceNextSystemCall(sqlite3_vfs*, const char *zName);
177 
178 /*
179 ** Return a pointer to the tail of the pathname.  Examples:
180 **
181 **     /home/drh/xyzzy.txt -> xyzzy.txt
182 **     xyzzy.txt           -> xyzzy.txt
183 */
184 static const char *fileTail(const char *z){
185   int i;
186   if( z==0 ) return 0;
187   i = strlen(z)-1;
188   while( i>0 && z[i-1]!='/' ){ i--; }
189   return &z[i];
190 }
191 
192 /*
193 ** Send trace output defined by zFormat and subsequent arguments.
194 */
195 static void vfstrace_printf(
196   vfstrace_info *pInfo,
197   const char *zFormat,
198   ...
199 ){
200   va_list ap;
201   char *zMsg;
202   va_start(ap, zFormat);
203   zMsg = sqlite3_vmprintf(zFormat, ap);
204   va_end(ap);
205   pInfo->xOut(zMsg, pInfo->pOutArg);
206   sqlite3_free(zMsg);
207 }
208 
209 /*
210 ** Convert value rc into a string and print it using zFormat.  zFormat
211 ** should have exactly one %s
212 */
213 static void vfstrace_print_errcode(
214   vfstrace_info *pInfo,
215   const char *zFormat,
216   int rc
217 ){
218   char zBuf[50];
219   char *zVal;
220   switch( rc ){
221     case SQLITE_OK:         zVal = "SQLITE_OK";          break;
222     case SQLITE_ERROR:      zVal = "SQLITE_ERROR";       break;
223     case SQLITE_PERM:       zVal = "SQLITE_PERM";        break;
224     case SQLITE_ABORT:      zVal = "SQLITE_ABORT";       break;
225     case SQLITE_BUSY:       zVal = "SQLITE_BUSY";        break;
226     case SQLITE_NOMEM:      zVal = "SQLITE_NOMEM";       break;
227     case SQLITE_READONLY:   zVal = "SQLITE_READONLY";    break;
228     case SQLITE_INTERRUPT:  zVal = "SQLITE_INTERRUPT";   break;
229     case SQLITE_IOERR:      zVal = "SQLITE_IOERR";       break;
230     case SQLITE_CORRUPT:    zVal = "SQLITE_CORRUPT";     break;
231     case SQLITE_FULL:       zVal = "SQLITE_FULL";        break;
232     case SQLITE_CANTOPEN:   zVal = "SQLITE_CANTOPEN";    break;
233     case SQLITE_PROTOCOL:   zVal = "SQLITE_PROTOCOL";    break;
234     case SQLITE_EMPTY:      zVal = "SQLITE_EMPTY";       break;
235     case SQLITE_SCHEMA:     zVal = "SQLITE_SCHEMA";      break;
236     case SQLITE_CONSTRAINT: zVal = "SQLITE_CONSTRAINT";  break;
237     case SQLITE_MISMATCH:   zVal = "SQLITE_MISMATCH";    break;
238     case SQLITE_MISUSE:     zVal = "SQLITE_MISUSE";      break;
239     case SQLITE_NOLFS:      zVal = "SQLITE_NOLFS";       break;
240     case SQLITE_IOERR_READ:         zVal = "SQLITE_IOERR_READ";         break;
241     case SQLITE_IOERR_SHORT_READ:   zVal = "SQLITE_IOERR_SHORT_READ";   break;
242     case SQLITE_IOERR_WRITE:        zVal = "SQLITE_IOERR_WRITE";        break;
243     case SQLITE_IOERR_FSYNC:        zVal = "SQLITE_IOERR_FSYNC";        break;
244     case SQLITE_IOERR_DIR_FSYNC:    zVal = "SQLITE_IOERR_DIR_FSYNC";    break;
245     case SQLITE_IOERR_TRUNCATE:     zVal = "SQLITE_IOERR_TRUNCATE";     break;
246     case SQLITE_IOERR_FSTAT:        zVal = "SQLITE_IOERR_FSTAT";        break;
247     case SQLITE_IOERR_UNLOCK:       zVal = "SQLITE_IOERR_UNLOCK";       break;
248     case SQLITE_IOERR_RDLOCK:       zVal = "SQLITE_IOERR_RDLOCK";       break;
249     case SQLITE_IOERR_DELETE:       zVal = "SQLITE_IOERR_DELETE";       break;
250     case SQLITE_IOERR_BLOCKED:      zVal = "SQLITE_IOERR_BLOCKED";      break;
251     case SQLITE_IOERR_NOMEM:        zVal = "SQLITE_IOERR_NOMEM";        break;
252     case SQLITE_IOERR_ACCESS:       zVal = "SQLITE_IOERR_ACCESS";       break;
253     case SQLITE_IOERR_CHECKRESERVEDLOCK:
254                                zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
255     case SQLITE_IOERR_LOCK:         zVal = "SQLITE_IOERR_LOCK";         break;
256     case SQLITE_IOERR_CLOSE:        zVal = "SQLITE_IOERR_CLOSE";        break;
257     case SQLITE_IOERR_DIR_CLOSE:    zVal = "SQLITE_IOERR_DIR_CLOSE";    break;
258     case SQLITE_IOERR_SHMOPEN:      zVal = "SQLITE_IOERR_SHMOPEN";      break;
259     case SQLITE_IOERR_SHMSIZE:      zVal = "SQLITE_IOERR_SHMSIZE";      break;
260     case SQLITE_IOERR_SHMLOCK:      zVal = "SQLITE_IOERR_SHMLOCK";      break;
261     case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break;
262     case SQLITE_BUSY_RECOVERY:      zVal = "SQLITE_BUSY_RECOVERY";      break;
263     case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break;
264     default: {
265        sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
266        zVal = zBuf;
267        break;
268     }
269   }
270   vfstrace_printf(pInfo, zFormat, zVal);
271 }
272 
273 /*
274 ** Append to a buffer.
275 */
276 static void strappend(char *z, int *pI, const char *zAppend){
277   int i = *pI;
278   while( zAppend[0] ){ z[i++] = *(zAppend++); }
279   z[i] = 0;
280   *pI = i;
281 }
282 
283 /*
284 ** Close an vfstrace-file.
285 */
286 static int vfstraceClose(sqlite3_file *pFile){
287   vfstrace_file *p = (vfstrace_file *)pFile;
288   vfstrace_info *pInfo = p->pInfo;
289   int rc;
290   vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
291   rc = p->pReal->pMethods->xClose(p->pReal);
292   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
293   if( rc==SQLITE_OK ){
294     sqlite3_free((void*)p->base.pMethods);
295     p->base.pMethods = 0;
296   }
297   return rc;
298 }
299 
300 /*
301 ** Read data from an vfstrace-file.
302 */
303 static int vfstraceRead(
304   sqlite3_file *pFile,
305   void *zBuf,
306   int iAmt,
307   sqlite_int64 iOfst
308 ){
309   vfstrace_file *p = (vfstrace_file *)pFile;
310   vfstrace_info *pInfo = p->pInfo;
311   int rc;
312   vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)",
313                   pInfo->zVfsName, p->zFName, iAmt, iOfst);
314   rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
315   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
316   return rc;
317 }
318 
319 /*
320 ** Write data to an vfstrace-file.
321 */
322 static int vfstraceWrite(
323   sqlite3_file *pFile,
324   const void *zBuf,
325   int iAmt,
326   sqlite_int64 iOfst
327 ){
328   vfstrace_file *p = (vfstrace_file *)pFile;
329   vfstrace_info *pInfo = p->pInfo;
330   int rc;
331   vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)",
332                   pInfo->zVfsName, p->zFName, iAmt, iOfst);
333   rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
334   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
335   return rc;
336 }
337 
338 /*
339 ** Truncate an vfstrace-file.
340 */
341 static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
342   vfstrace_file *p = (vfstrace_file *)pFile;
343   vfstrace_info *pInfo = p->pInfo;
344   int rc;
345   vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
346                   size);
347   rc = p->pReal->pMethods->xTruncate(p->pReal, size);
348   vfstrace_printf(pInfo, " -> %d\n", rc);
349   return rc;
350 }
351 
352 /*
353 ** Sync an vfstrace-file.
354 */
355 static int vfstraceSync(sqlite3_file *pFile, int flags){
356   vfstrace_file *p = (vfstrace_file *)pFile;
357   vfstrace_info *pInfo = p->pInfo;
358   int rc;
359   int i;
360   char zBuf[100];
361   memcpy(zBuf, "|0", 3);
362   i = 0;
363   if( flags & SQLITE_SYNC_FULL )        strappend(zBuf, &i, "|FULL");
364   else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL");
365   if( flags & SQLITE_SYNC_DATAONLY )    strappend(zBuf, &i, "|DATAONLY");
366   if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){
367     sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags);
368   }
369   vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName,
370                   &zBuf[1]);
371   rc = p->pReal->pMethods->xSync(p->pReal, flags);
372   vfstrace_printf(pInfo, " -> %d\n", rc);
373   return rc;
374 }
375 
376 /*
377 ** Return the current file-size of an vfstrace-file.
378 */
379 static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
380   vfstrace_file *p = (vfstrace_file *)pFile;
381   vfstrace_info *pInfo = p->pInfo;
382   int rc;
383   vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
384   rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
385   vfstrace_print_errcode(pInfo, " -> %s,", rc);
386   vfstrace_printf(pInfo, " size=%lld\n", *pSize);
387   return rc;
388 }
389 
390 /*
391 ** Return the name of a lock.
392 */
393 static const char *lockName(int eLock){
394   const char *azLockNames[] = {
395      "NONE", "SHARED", "RESERVED", "PENDING", "EXCLUSIVE"
396   };
397   if( eLock<0 || eLock>=sizeof(azLockNames)/sizeof(azLockNames[0]) ){
398     return "???";
399   }else{
400     return azLockNames[eLock];
401   }
402 }
403 
404 /*
405 ** Lock an vfstrace-file.
406 */
407 static int vfstraceLock(sqlite3_file *pFile, int eLock){
408   vfstrace_file *p = (vfstrace_file *)pFile;
409   vfstrace_info *pInfo = p->pInfo;
410   int rc;
411   vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName,
412                   lockName(eLock));
413   rc = p->pReal->pMethods->xLock(p->pReal, eLock);
414   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
415   return rc;
416 }
417 
418 /*
419 ** Unlock an vfstrace-file.
420 */
421 static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
422   vfstrace_file *p = (vfstrace_file *)pFile;
423   vfstrace_info *pInfo = p->pInfo;
424   int rc;
425   vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName,
426                   lockName(eLock));
427   rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
428   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
429   return rc;
430 }
431 
432 /*
433 ** Check if another file-handle holds a RESERVED lock on an vfstrace-file.
434 */
435 static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
436   vfstrace_file *p = (vfstrace_file *)pFile;
437   vfstrace_info *pInfo = p->pInfo;
438   int rc;
439   vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)",
440                   pInfo->zVfsName, p->zFName);
441   rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
442   vfstrace_print_errcode(pInfo, " -> %s", rc);
443   vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
444   return rc;
445 }
446 
447 /*
448 ** File control method. For custom operations on an vfstrace-file.
449 */
450 static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){
451   vfstrace_file *p = (vfstrace_file *)pFile;
452   vfstrace_info *pInfo = p->pInfo;
453   int rc;
454   char zBuf[100];
455   char *zOp;
456   switch( op ){
457     case SQLITE_FCNTL_LOCKSTATE:    zOp = "LOCKSTATE";          break;
458     case SQLITE_GET_LOCKPROXYFILE:  zOp = "GET_LOCKPROXYFILE";  break;
459     case SQLITE_SET_LOCKPROXYFILE:  zOp = "SET_LOCKPROXYFILE";  break;
460     case SQLITE_LAST_ERRNO:         zOp = "LAST_ERRNO";         break;
461     case SQLITE_FCNTL_SIZE_HINT: {
462       sqlite3_snprintf(sizeof(zBuf), zBuf, "SIZE_HINT,%lld",
463                        *(sqlite3_int64*)pArg);
464       zOp = zBuf;
465       break;
466     }
467     case SQLITE_FCNTL_CHUNK_SIZE: {
468       sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg);
469       zOp = zBuf;
470       break;
471     }
472     case SQLITE_FCNTL_FILE_POINTER: zOp = "FILE_POINTER";       break;
473     case SQLITE_FCNTL_SYNC_OMITTED: zOp = "SYNC_OMITTED";       break;
474     case SQLITE_FCNTL_WIN32_AV_RETRY: zOp = "WIN32_AV_RETRY";   break;
475     case SQLITE_FCNTL_PERSIST_WAL:  zOp = "PERSIST_WAL";        break;
476     case SQLITE_FCNTL_OVERWRITE:    zOp = "OVERWRITE";          break;
477     case SQLITE_FCNTL_VFSNAME:      zOp = "VFSNAME";            break;
478     case 0xca093fa0:                zOp = "DB_UNCHANGED";       break;
479     case SQLITE_FCNTL_PRAGMA: {
480       const char *const* a = (const char*const*)pArg;
481       sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]);
482       zOp = zBuf;
483       break;
484     }
485     default: {
486       sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op);
487       zOp = zBuf;
488       break;
489     }
490   }
491   vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)",
492                   pInfo->zVfsName, p->zFName, zOp);
493   rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
494   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
495   if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){
496     *(char**)pArg = sqlite3_mprintf("vfstrace.%s/%z",
497                                     pInfo->zVfsName, *(char**)pArg);
498   }
499   if( op==SQLITE_FCNTL_PRAGMA && rc==SQLITE_OK && *(char**)pArg ){
500     vfstrace_printf(pInfo, "%s.xFileControl(%s,%s) returns %s",
501                     pInfo->zVfsName, p->zFNmae, zOp, *(char**)pArg);
502   }
503   return rc;
504 }
505 
506 /*
507 ** Return the sector-size in bytes for an vfstrace-file.
508 */
509 static int vfstraceSectorSize(sqlite3_file *pFile){
510   vfstrace_file *p = (vfstrace_file *)pFile;
511   vfstrace_info *pInfo = p->pInfo;
512   int rc;
513   vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
514   rc = p->pReal->pMethods->xSectorSize(p->pReal);
515   vfstrace_printf(pInfo, " -> %d\n", rc);
516   return rc;
517 }
518 
519 /*
520 ** Return the device characteristic flags supported by an vfstrace-file.
521 */
522 static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
523   vfstrace_file *p = (vfstrace_file *)pFile;
524   vfstrace_info *pInfo = p->pInfo;
525   int rc;
526   vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
527                   pInfo->zVfsName, p->zFName);
528   rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
529   vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
530   return rc;
531 }
532 
533 /*
534 ** Shared-memory operations.
535 */
536 static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
537   vfstrace_file *p = (vfstrace_file *)pFile;
538   vfstrace_info *pInfo = p->pInfo;
539   int rc;
540   char zLck[100];
541   int i = 0;
542   memcpy(zLck, "|0", 3);
543   if( flags & SQLITE_SHM_UNLOCK )    strappend(zLck, &i, "|UNLOCK");
544   if( flags & SQLITE_SHM_LOCK )      strappend(zLck, &i, "|LOCK");
545   if( flags & SQLITE_SHM_SHARED )    strappend(zLck, &i, "|SHARED");
546   if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE");
547   if( flags & ~(0xf) ){
548      sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags);
549   }
550   vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d,n=%d,%s)",
551                   pInfo->zVfsName, p->zFName, ofst, n, &zLck[1]);
552   rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
553   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
554   return rc;
555 }
556 static int vfstraceShmMap(
557   sqlite3_file *pFile,
558   int iRegion,
559   int szRegion,
560   int isWrite,
561   void volatile **pp
562 ){
563   vfstrace_file *p = (vfstrace_file *)pFile;
564   vfstrace_info *pInfo = p->pInfo;
565   int rc;
566   vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
567                   pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
568   rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
569   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
570   return rc;
571 }
572 static void vfstraceShmBarrier(sqlite3_file *pFile){
573   vfstrace_file *p = (vfstrace_file *)pFile;
574   vfstrace_info *pInfo = p->pInfo;
575   vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
576   p->pReal->pMethods->xShmBarrier(p->pReal);
577 }
578 static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
579   vfstrace_file *p = (vfstrace_file *)pFile;
580   vfstrace_info *pInfo = p->pInfo;
581   int rc;
582   vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
583                   pInfo->zVfsName, p->zFName, delFlag);
584   rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
585   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
586   return rc;
587 }
588 
589 
590 
591 /*
592 ** Open an vfstrace file handle.
593 */
594 static int vfstraceOpen(
595   sqlite3_vfs *pVfs,
596   const char *zName,
597   sqlite3_file *pFile,
598   int flags,
599   int *pOutFlags
600 ){
601   int rc;
602   vfstrace_file *p = (vfstrace_file *)pFile;
603   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
604   sqlite3_vfs *pRoot = pInfo->pRootVfs;
605   p->pInfo = pInfo;
606   p->zFName = zName ? fileTail(zName) : "<temp>";
607   p->pReal = (sqlite3_file *)&p[1];
608   rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
609   vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
610                   pInfo->zVfsName, p->zFName, flags);
611   if( p->pReal->pMethods ){
612     sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) );
613     const sqlite3_io_methods *pSub = p->pReal->pMethods;
614     memset(pNew, 0, sizeof(*pNew));
615     pNew->iVersion = pSub->iVersion;
616     pNew->xClose = vfstraceClose;
617     pNew->xRead = vfstraceRead;
618     pNew->xWrite = vfstraceWrite;
619     pNew->xTruncate = vfstraceTruncate;
620     pNew->xSync = vfstraceSync;
621     pNew->xFileSize = vfstraceFileSize;
622     pNew->xLock = vfstraceLock;
623     pNew->xUnlock = vfstraceUnlock;
624     pNew->xCheckReservedLock = vfstraceCheckReservedLock;
625     pNew->xFileControl = vfstraceFileControl;
626     pNew->xSectorSize = vfstraceSectorSize;
627     pNew->xDeviceCharacteristics = vfstraceDeviceCharacteristics;
628     if( pNew->iVersion>=2 ){
629       pNew->xShmMap = pSub->xShmMap ? vfstraceShmMap : 0;
630       pNew->xShmLock = pSub->xShmLock ? vfstraceShmLock : 0;
631       pNew->xShmBarrier = pSub->xShmBarrier ? vfstraceShmBarrier : 0;
632       pNew->xShmUnmap = pSub->xShmUnmap ? vfstraceShmUnmap : 0;
633     }
634     pFile->pMethods = pNew;
635   }
636   vfstrace_print_errcode(pInfo, " -> %s", rc);
637   if( pOutFlags ){
638     vfstrace_printf(pInfo, ", outFlags=0x%x\n", *pOutFlags);
639   }else{
640     vfstrace_printf(pInfo, "\n");
641   }
642   return rc;
643 }
644 
645 /*
646 ** Delete the file located at zPath. If the dirSync argument is true,
647 ** ensure the file-system modifications are synced to disk before
648 ** returning.
649 */
650 static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
651   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
652   sqlite3_vfs *pRoot = pInfo->pRootVfs;
653   int rc;
654   vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
655                   pInfo->zVfsName, zPath, dirSync);
656   rc = pRoot->xDelete(pRoot, zPath, dirSync);
657   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
658   return rc;
659 }
660 
661 /*
662 ** Test for access permissions. Return true if the requested permission
663 ** is available, or false otherwise.
664 */
665 static int vfstraceAccess(
666   sqlite3_vfs *pVfs,
667   const char *zPath,
668   int flags,
669   int *pResOut
670 ){
671   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
672   sqlite3_vfs *pRoot = pInfo->pRootVfs;
673   int rc;
674   vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
675                   pInfo->zVfsName, zPath, flags);
676   rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
677   vfstrace_print_errcode(pInfo, " -> %s", rc);
678   vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
679   return rc;
680 }
681 
682 /*
683 ** Populate buffer zOut with the full canonical pathname corresponding
684 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
685 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
686 */
687 static int vfstraceFullPathname(
688   sqlite3_vfs *pVfs,
689   const char *zPath,
690   int nOut,
691   char *zOut
692 ){
693   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
694   sqlite3_vfs *pRoot = pInfo->pRootVfs;
695   int rc;
696   vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
697                   pInfo->zVfsName, zPath);
698   rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
699   vfstrace_print_errcode(pInfo, " -> %s", rc);
700   vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
701   return rc;
702 }
703 
704 /*
705 ** Open the dynamic library located at zPath and return a handle.
706 */
707 static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
708   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
709   sqlite3_vfs *pRoot = pInfo->pRootVfs;
710   vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
711   return pRoot->xDlOpen(pRoot, zPath);
712 }
713 
714 /*
715 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
716 ** utf-8 string describing the most recent error encountered associated
717 ** with dynamic libraries.
718 */
719 static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
720   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
721   sqlite3_vfs *pRoot = pInfo->pRootVfs;
722   vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
723   pRoot->xDlError(pRoot, nByte, zErrMsg);
724   vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
725 }
726 
727 /*
728 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
729 */
730 static void (*vfstraceDlSym(sqlite3_vfs *pVfs,void *p,const char *zSym))(void){
731   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
732   sqlite3_vfs *pRoot = pInfo->pRootVfs;
733   vfstrace_printf(pInfo, "%s.xDlSym(\"%s\")\n", pInfo->zVfsName, zSym);
734   return pRoot->xDlSym(pRoot, p, zSym);
735 }
736 
737 /*
738 ** Close the dynamic library handle pHandle.
739 */
740 static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
741   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
742   sqlite3_vfs *pRoot = pInfo->pRootVfs;
743   vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName);
744   pRoot->xDlClose(pRoot, pHandle);
745 }
746 
747 /*
748 ** Populate the buffer pointed to by zBufOut with nByte bytes of
749 ** random data.
750 */
751 static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
752   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
753   sqlite3_vfs *pRoot = pInfo->pRootVfs;
754   vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
755   return pRoot->xRandomness(pRoot, nByte, zBufOut);
756 }
757 
758 /*
759 ** Sleep for nMicro microseconds. Return the number of microseconds
760 ** actually slept.
761 */
762 static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
763   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
764   sqlite3_vfs *pRoot = pInfo->pRootVfs;
765   return pRoot->xSleep(pRoot, nMicro);
766 }
767 
768 /*
769 ** Return the current time as a Julian Day number in *pTimeOut.
770 */
771 static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
772   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
773   sqlite3_vfs *pRoot = pInfo->pRootVfs;
774   return pRoot->xCurrentTime(pRoot, pTimeOut);
775 }
776 static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
777   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
778   sqlite3_vfs *pRoot = pInfo->pRootVfs;
779   return pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
780 }
781 
782 /*
783 ** Return th3 emost recent error code and message
784 */
785 static int vfstraceGetLastError(sqlite3_vfs *pVfs, int iErr, char *zErr){
786   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
787   sqlite3_vfs *pRoot = pInfo->pRootVfs;
788   return pRoot->xGetLastError(pRoot, iErr, zErr);
789 }
790 
791 /*
792 ** Override system calls.
793 */
794 static int vfstraceSetSystemCall(
795   sqlite3_vfs *pVfs,
796   const char *zName,
797   sqlite3_syscall_ptr pFunc
798 ){
799   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
800   sqlite3_vfs *pRoot = pInfo->pRootVfs;
801   return pRoot->xSetSystemCall(pRoot, zName, pFunc);
802 }
803 static sqlite3_syscall_ptr vfstraceGetSystemCall(
804   sqlite3_vfs *pVfs,
805   const char *zName
806 ){
807   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
808   sqlite3_vfs *pRoot = pInfo->pRootVfs;
809   return pRoot->xGetSystemCall(pRoot, zName);
810 }
811 static const char *vfstraceNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
812   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
813   sqlite3_vfs *pRoot = pInfo->pRootVfs;
814   return pRoot->xNextSystemCall(pRoot, zName);
815 }
816 
817 
818 /*
819 ** Clients invoke this routine to construct a new trace-vfs shim.
820 **
821 ** Return SQLITE_OK on success.
822 **
823 ** SQLITE_NOMEM is returned in the case of a memory allocation error.
824 ** SQLITE_NOTFOUND is returned if zOldVfsName does not exist.
825 */
826 int vfstrace_register(
827    const char *zTraceName,           /* Name of the newly constructed VFS */
828    const char *zOldVfsName,          /* Name of the underlying VFS */
829    int (*xOut)(const char*,void*),   /* Output routine.  ex: fputs */
830    void *pOutArg,                    /* 2nd argument to xOut.  ex: stderr */
831    int makeDefault                   /* True to make the new VFS the default */
832 ){
833   sqlite3_vfs *pNew;
834   sqlite3_vfs *pRoot;
835   vfstrace_info *pInfo;
836   int nName;
837   int nByte;
838 
839   pRoot = sqlite3_vfs_find(zOldVfsName);
840   if( pRoot==0 ) return SQLITE_NOTFOUND;
841   nName = strlen(zTraceName);
842   nByte = sizeof(*pNew) + sizeof(*pInfo) + nName + 1;
843   pNew = sqlite3_malloc( nByte );
844   if( pNew==0 ) return SQLITE_NOMEM;
845   memset(pNew, 0, nByte);
846   pInfo = (vfstrace_info*)&pNew[1];
847   pNew->iVersion = pRoot->iVersion;
848   pNew->szOsFile = pRoot->szOsFile + sizeof(vfstrace_file);
849   pNew->mxPathname = pRoot->mxPathname;
850   pNew->zName = (char*)&pInfo[1];
851   memcpy((char*)&pInfo[1], zTraceName, nName+1);
852   pNew->pAppData = pInfo;
853   pNew->xOpen = vfstraceOpen;
854   pNew->xDelete = vfstraceDelete;
855   pNew->xAccess = vfstraceAccess;
856   pNew->xFullPathname = vfstraceFullPathname;
857   pNew->xDlOpen = pRoot->xDlOpen==0 ? 0 : vfstraceDlOpen;
858   pNew->xDlError = pRoot->xDlError==0 ? 0 : vfstraceDlError;
859   pNew->xDlSym = pRoot->xDlSym==0 ? 0 : vfstraceDlSym;
860   pNew->xDlClose = pRoot->xDlClose==0 ? 0 : vfstraceDlClose;
861   pNew->xRandomness = vfstraceRandomness;
862   pNew->xSleep = vfstraceSleep;
863   pNew->xCurrentTime = vfstraceCurrentTime;
864   pNew->xGetLastError = pRoot->xGetLastError==0 ? 0 : vfstraceGetLastError;
865   if( pNew->iVersion>=2 ){
866     pNew->xCurrentTimeInt64 = pRoot->xCurrentTimeInt64==0 ? 0 :
867                                    vfstraceCurrentTimeInt64;
868     if( pNew->iVersion>=3 ){
869       pNew->xSetSystemCall = pRoot->xSetSystemCall==0 ? 0 :
870                                    vfstraceSetSystemCall;
871       pNew->xGetSystemCall = pRoot->xGetSystemCall==0 ? 0 :
872                                    vfstraceGetSystemCall;
873       pNew->xNextSystemCall = pRoot->xNextSystemCall==0 ? 0 :
874                                    vfstraceNextSystemCall;
875     }
876   }
877   pInfo->pRootVfs = pRoot;
878   pInfo->xOut = xOut;
879   pInfo->pOutArg = pOutArg;
880   pInfo->zVfsName = pNew->zName;
881   pInfo->pTraceVfs = pNew;
882   vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
883        pInfo->zVfsName, pRoot->zName);
884   return sqlite3_vfs_register(pNew, makeDefault);
885 }
886