xref: /sqlite-3.40.0/src/test_vfstrace.c (revision 97ae8ffb)
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 #include <stdlib.h>
17 #include <string.h>
18 #include "sqlite3.h"
19 
20 /*
21 ** An instance of this structure is attached to the each trace VFS to
22 ** provide auxiliary information.
23 */
24 typedef struct vfstrace_info vfstrace_info;
25 struct vfstrace_info {
26   sqlite3_vfs *pRootVfs;              /* The underlying real VFS */
27   int (*xOut)(const char*, void*);    /* Send output here */
28   void *pOutArg;                      /* First argument to xOut */
29   const char *zVfsName;               /* Name of this trace-VFS */
30   sqlite3_vfs *pTraceVfs;             /* Pointer back to the trace VFS */
31 };
32 
33 /*
34 ** The sqlite3_file object for the trace VFS
35 */
36 typedef struct vfstrace_file vfstrace_file;
37 struct vfstrace_file {
38   sqlite3_file base;        /* Base class.  Must be first */
39   vfstrace_info *pInfo;     /* The trace-VFS to which this file belongs */
40   const char *zFName;       /* Base name of the file */
41   sqlite3_file *pReal;      /* The real underlying file */
42 };
43 
44 /*
45 ** Method declarations for vfstrace_file.
46 */
47 static int vfstraceClose(sqlite3_file*);
48 static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
49 static int vfstraceWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
50 static int vfstraceTruncate(sqlite3_file*, sqlite3_int64 size);
51 static int vfstraceSync(sqlite3_file*, int flags);
52 static int vfstraceFileSize(sqlite3_file*, sqlite3_int64 *pSize);
53 static int vfstraceLock(sqlite3_file*, int);
54 static int vfstraceUnlock(sqlite3_file*, int);
55 static int vfstraceCheckReservedLock(sqlite3_file*, int *);
56 static int vfstraceFileControl(sqlite3_file*, int op, void *pArg);
57 static int vfstraceSectorSize(sqlite3_file*);
58 static int vfstraceDeviceCharacteristics(sqlite3_file*);
59 static int vfstraceShmLock(sqlite3_file*,int,int,int);
60 static int vfstraceShmMap(sqlite3_file*,int,int,int, void volatile **);
61 static void vfstraceShmBarrier(sqlite3_file*);
62 static int vfstraceShmUnmap(sqlite3_file*,int);
63 
64 /*
65 ** Method declarations for vfstrace_vfs.
66 */
67 static int vfstraceOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
68 static int vfstraceDelete(sqlite3_vfs*, const char *zName, int syncDir);
69 static int vfstraceAccess(sqlite3_vfs*, const char *zName, int flags, int *);
70 static int vfstraceFullPathname(sqlite3_vfs*, const char *zName, int, char *);
71 static void *vfstraceDlOpen(sqlite3_vfs*, const char *zFilename);
72 static void vfstraceDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
73 static void (*vfstraceDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
74 static void vfstraceDlClose(sqlite3_vfs*, void*);
75 static int vfstraceRandomness(sqlite3_vfs*, int nByte, char *zOut);
76 static int vfstraceSleep(sqlite3_vfs*, int microseconds);
77 static int vfstraceCurrentTime(sqlite3_vfs*, double*);
78 static int vfstraceGetLastError(sqlite3_vfs*, int, char*);
79 static int vfstraceCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
80 static int vfstraceSetSystemCall(sqlite3_vfs*, const char *zName, void *pFunc);
81 static void *vfstraceGetSystemCall(sqlite3_vfs*, const char *zName);
82 static const char *vfstraceNextSystemCall(sqlite3_vfs*, const char *zName);
83 
84 /*
85 ** Return a pointer to the tail of the pathname.  Examples:
86 **
87 **     /home/drh/xyzzy.txt -> xyzzy.txt
88 **     xyzzy.txt           -> xyzzy.txt
89 */
90 static const char *fileTail(const char *z){
91   int i;
92   if( z==0 ) return 0;
93   i = strlen(z)-1;
94   while( i>0 && z[i-1]!='/' ){ i--; }
95   return &z[i];
96 }
97 
98 /*
99 ** Send trace output defined by zFormat and subsequent arguments.
100 */
101 static void vfstrace_printf(
102   vfstrace_info *pInfo,
103   const char *zFormat,
104   ...
105 ){
106   va_list ap;
107   char *zMsg;
108   va_start(ap, zFormat);
109   zMsg = sqlite3_vmprintf(zFormat, ap);
110   va_end(ap);
111   pInfo->xOut(zMsg, pInfo->pOutArg);
112   sqlite3_free(zMsg);
113 }
114 
115 /*
116 ** Convert value rc into a string and print it using zFormat.  zFormat
117 ** should have exactly one %s
118 */
119 static void vfstrace_print_errcode(
120   vfstrace_info *pInfo,
121   const char *zFormat,
122   int rc
123 ){
124   char zBuf[50];
125   char *zVal;
126   switch( rc ){
127     case SQLITE_OK:         zVal = "SQLITE_OK";          break;
128     case SQLITE_ERROR:      zVal = "SQLITE_ERROR";       break;
129     case SQLITE_PERM:       zVal = "SQLITE_PERM";        break;
130     case SQLITE_ABORT:      zVal = "SQLITE_ABORT";       break;
131     case SQLITE_BUSY:       zVal = "SQLITE_BUSY";        break;
132     case SQLITE_NOMEM:      zVal = "SQLITE_NOMEM";       break;
133     case SQLITE_READONLY:   zVal = "SQLITE_READONLY";    break;
134     case SQLITE_INTERRUPT:  zVal = "SQLITE_INTERRUPT";   break;
135     case SQLITE_IOERR:      zVal = "SQLITE_IOERR";       break;
136     case SQLITE_CORRUPT:    zVal = "SQLITE_CORRUPT";     break;
137     case SQLITE_FULL:       zVal = "SQLITE_FULL";        break;
138     case SQLITE_CANTOPEN:   zVal = "SQLITE_CANTOPEN";    break;
139     case SQLITE_PROTOCOL:   zVal = "SQLITE_PROTOCOL";    break;
140     case SQLITE_EMPTY:      zVal = "SQLITE_EMPTY";       break;
141     case SQLITE_SCHEMA:     zVal = "SQLITE_SCHEMA";      break;
142     case SQLITE_CONSTRAINT: zVal = "SQLITE_CONSTRAINT";  break;
143     case SQLITE_MISMATCH:   zVal = "SQLITE_MISMATCH";    break;
144     case SQLITE_MISUSE:     zVal = "SQLITE_MISUSE";      break;
145     case SQLITE_NOLFS:      zVal = "SQLITE_NOLFS";       break;
146     case SQLITE_IOERR_READ:         zVal = "SQLITE_IOERR_READ";         break;
147     case SQLITE_IOERR_SHORT_READ:   zVal = "SQLITE_IOERR_SHORT_READ";   break;
148     case SQLITE_IOERR_WRITE:        zVal = "SQLITE_IOERR_WRITE";        break;
149     case SQLITE_IOERR_FSYNC:        zVal = "SQLITE_IOERR_FSYNC";        break;
150     case SQLITE_IOERR_DIR_FSYNC:    zVal = "SQLITE_IOERR_DIR_FSYNC";    break;
151     case SQLITE_IOERR_TRUNCATE:     zVal = "SQLITE_IOERR_TRUNCATE";     break;
152     case SQLITE_IOERR_FSTAT:        zVal = "SQLITE_IOERR_FSTAT";        break;
153     case SQLITE_IOERR_UNLOCK:       zVal = "SQLITE_IOERR_UNLOCK";       break;
154     case SQLITE_IOERR_RDLOCK:       zVal = "SQLITE_IOERR_RDLOCK";       break;
155     case SQLITE_IOERR_DELETE:       zVal = "SQLITE_IOERR_DELETE";       break;
156     case SQLITE_IOERR_BLOCKED:      zVal = "SQLITE_IOERR_BLOCKED";      break;
157     case SQLITE_IOERR_NOMEM:        zVal = "SQLITE_IOERR_NOMEM";        break;
158     case SQLITE_IOERR_ACCESS:       zVal = "SQLITE_IOERR_ACCESS";       break;
159     case SQLITE_IOERR_CHECKRESERVEDLOCK:
160                                zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
161     case SQLITE_IOERR_LOCK:         zVal = "SQLITE_IOERR_LOCK";         break;
162     case SQLITE_IOERR_CLOSE:        zVal = "SQLITE_IOERR_CLOSE";        break;
163     case SQLITE_IOERR_DIR_CLOSE:    zVal = "SQLITE_IOERR_DIR_CLOSE";    break;
164     case SQLITE_IOERR_SHMOPEN:      zVal = "SQLITE_IOERR_SHMOPEN";      break;
165     case SQLITE_IOERR_SHMSIZE:      zVal = "SQLITE_IOERR_SHMSIZE";      break;
166     case SQLITE_IOERR_SHMLOCK:      zVal = "SQLITE_IOERR_SHMLOCK";      break;
167     case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break;
168     case SQLITE_BUSY_RECOVERY:      zVal = "SQLITE_BUSY_RECOVERY";      break;
169     case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break;
170     default: {
171        sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
172        zVal = zBuf;
173        break;
174     }
175   }
176   vfstrace_printf(pInfo, zFormat, zVal);
177 }
178 
179 /*
180 ** Close an vfstrace-file.
181 */
182 static int vfstraceClose(sqlite3_file *pFile){
183   vfstrace_file *p = (vfstrace_file *)pFile;
184   vfstrace_info *pInfo = p->pInfo;
185   int rc;
186   vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
187   rc = p->pReal->pMethods->xClose(p->pReal);
188   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
189   if( rc==SQLITE_OK ){
190     sqlite3_free((void*)p->base.pMethods);
191     p->base.pMethods = 0;
192   }
193   return rc;
194 }
195 
196 /*
197 ** Read data from an vfstrace-file.
198 */
199 static int vfstraceRead(
200   sqlite3_file *pFile,
201   void *zBuf,
202   int iAmt,
203   sqlite_int64 iOfst
204 ){
205   vfstrace_file *p = (vfstrace_file *)pFile;
206   vfstrace_info *pInfo = p->pInfo;
207   int rc;
208   vfstrace_printf(pInfo, "%s.xRead(%s,*,%d,%lld)", pInfo->zVfsName, p->zFName,
209                   iAmt, iOfst);
210   rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
211   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
212   return rc;
213 }
214 
215 /*
216 ** Write data to an vfstrace-file.
217 */
218 static int vfstraceWrite(
219   sqlite3_file *pFile,
220   const void *zBuf,
221   int iAmt,
222   sqlite_int64 iOfst
223 ){
224   vfstrace_file *p = (vfstrace_file *)pFile;
225   vfstrace_info *pInfo = p->pInfo;
226   int rc;
227   vfstrace_printf(pInfo, "%s.xWrite(%s,*,%d,%lld)", pInfo->zVfsName, p->zFName,
228                   iAmt, iOfst);
229   rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
230   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
231   return rc;
232 }
233 
234 /*
235 ** Truncate an vfstrace-file.
236 */
237 static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
238   vfstrace_file *p = (vfstrace_file *)pFile;
239   vfstrace_info *pInfo = p->pInfo;
240   int rc;
241   vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
242                   size);
243   rc = p->pReal->pMethods->xTruncate(p->pReal, size);
244   vfstrace_printf(pInfo, " -> %d\n", rc);
245   return rc;
246 }
247 
248 /*
249 ** Sync an vfstrace-file.
250 */
251 static int vfstraceSync(sqlite3_file *pFile, int flags){
252   vfstrace_file *p = (vfstrace_file *)pFile;
253   vfstrace_info *pInfo = p->pInfo;
254   int rc;
255   vfstrace_printf(pInfo, "%s.xSync(%s,%d)", pInfo->zVfsName, p->zFName, flags);
256   rc = p->pReal->pMethods->xSync(p->pReal, flags);
257   vfstrace_printf(pInfo, " -> %d\n", rc);
258   return rc;
259 }
260 
261 /*
262 ** Return the current file-size of an vfstrace-file.
263 */
264 static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
265   vfstrace_file *p = (vfstrace_file *)pFile;
266   vfstrace_info *pInfo = p->pInfo;
267   int rc;
268   vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
269   rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
270   vfstrace_print_errcode(pInfo, " -> %s,", rc);
271   vfstrace_printf(pInfo, " size=%lld\n", *pSize);
272   return rc;
273 }
274 
275 /*
276 ** Lock an vfstrace-file.
277 */
278 static int vfstraceLock(sqlite3_file *pFile, int eLock){
279   vfstrace_file *p = (vfstrace_file *)pFile;
280   vfstrace_info *pInfo = p->pInfo;
281   int rc;
282   vfstrace_printf(pInfo, "%s.xLock(%s,%d)", pInfo->zVfsName, p->zFName, eLock);
283   rc = p->pReal->pMethods->xLock(p->pReal, eLock);
284   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
285   return rc;
286 }
287 
288 /*
289 ** Unlock an vfstrace-file.
290 */
291 static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
292   vfstrace_file *p = (vfstrace_file *)pFile;
293   vfstrace_info *pInfo = p->pInfo;
294   int rc;
295   vfstrace_printf(pInfo, "%s.xUnlock(%s,%d)", pInfo->zVfsName, p->zFName,eLock);
296   rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
297   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
298   return rc;
299 }
300 
301 /*
302 ** Check if another file-handle holds a RESERVED lock on an vfstrace-file.
303 */
304 static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
305   vfstrace_file *p = (vfstrace_file *)pFile;
306   vfstrace_info *pInfo = p->pInfo;
307   int rc;
308   vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)",
309                   pInfo->zVfsName, p->zFName);
310   rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
311   vfstrace_print_errcode(pInfo, " -> %s", rc);
312   vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
313   return rc;
314 }
315 
316 /*
317 ** File control method. For custom operations on an vfstrace-file.
318 */
319 static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){
320   vfstrace_file *p = (vfstrace_file *)pFile;
321   vfstrace_info *pInfo = p->pInfo;
322   int rc;
323   vfstrace_printf(pInfo, "%s.xFileControl(%s,op=%d)",
324                   pInfo->zVfsName, p->zFName, op);
325   rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
326   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
327   return rc;
328 }
329 
330 /*
331 ** Return the sector-size in bytes for an vfstrace-file.
332 */
333 static int vfstraceSectorSize(sqlite3_file *pFile){
334   vfstrace_file *p = (vfstrace_file *)pFile;
335   vfstrace_info *pInfo = p->pInfo;
336   int rc;
337   vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
338   rc = p->pReal->pMethods->xSectorSize(p->pReal);
339   vfstrace_printf(pInfo, " -> %d\n", rc);
340   return rc;
341 }
342 
343 /*
344 ** Return the device characteristic flags supported by an vfstrace-file.
345 */
346 static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
347   vfstrace_file *p = (vfstrace_file *)pFile;
348   vfstrace_info *pInfo = p->pInfo;
349   int rc;
350   vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
351                   pInfo->zVfsName, p->zFName);
352   rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
353   vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
354   return rc;
355 }
356 
357 /*
358 ** Shared-memory operations.
359 */
360 static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
361   vfstrace_file *p = (vfstrace_file *)pFile;
362   vfstrace_info *pInfo = p->pInfo;
363   int rc;
364   vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d,n=%d,flags=%d)",
365                   pInfo->zVfsName, p->zFName, ofst, n, flags);
366   rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
367   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
368   return rc;
369 }
370 static int vfstraceShmMap(
371   sqlite3_file *pFile,
372   int iRegion,
373   int szRegion,
374   int isWrite,
375   void volatile **pp
376 ){
377   vfstrace_file *p = (vfstrace_file *)pFile;
378   vfstrace_info *pInfo = p->pInfo;
379   int rc;
380   vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
381                   pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
382   rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
383   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
384   return rc;
385 }
386 static void vfstraceShmBarrier(sqlite3_file *pFile){
387   vfstrace_file *p = (vfstrace_file *)pFile;
388   vfstrace_info *pInfo = p->pInfo;
389   vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
390   p->pReal->pMethods->xShmBarrier(p->pReal);
391 }
392 static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
393   vfstrace_file *p = (vfstrace_file *)pFile;
394   vfstrace_info *pInfo = p->pInfo;
395   int rc;
396   vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
397                   pInfo->zVfsName, p->zFName, delFlag);
398   rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
399   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
400   return rc;
401 }
402 
403 
404 
405 /*
406 ** Open an vfstrace file handle.
407 */
408 static int vfstraceOpen(
409   sqlite3_vfs *pVfs,
410   const char *zName,
411   sqlite3_file *pFile,
412   int flags,
413   int *pOutFlags
414 ){
415   int rc;
416   vfstrace_file *p = (vfstrace_file *)pFile;
417   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
418   sqlite3_vfs *pRoot = pInfo->pRootVfs;
419   p->pInfo = pInfo;
420   p->zFName = fileTail(zName);
421   p->pReal = (sqlite3_file *)&p[1];
422   rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
423   vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
424                   pInfo->zVfsName, p->zFName, flags);
425   if( p->pReal->pMethods ){
426     sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) );
427     const sqlite3_io_methods *pSub = p->pReal->pMethods;
428     memset(pNew, 0, sizeof(*pNew));
429     pNew->iVersion = pSub->iVersion;
430     pNew->xClose = vfstraceClose;
431     pNew->xRead = vfstraceRead;
432     pNew->xWrite = vfstraceWrite;
433     pNew->xTruncate = vfstraceTruncate;
434     pNew->xSync = vfstraceSync;
435     pNew->xFileSize = vfstraceFileSize;
436     pNew->xLock = vfstraceLock;
437     pNew->xUnlock = vfstraceUnlock;
438     pNew->xCheckReservedLock = vfstraceCheckReservedLock;
439     pNew->xFileControl = vfstraceFileControl;
440     pNew->xSectorSize = vfstraceSectorSize;
441     pNew->xDeviceCharacteristics = vfstraceDeviceCharacteristics;
442     if( pNew->iVersion>=2 ){
443       pNew->xShmMap = pSub->xShmMap ? vfstraceShmMap : 0;
444       pNew->xShmLock = pSub->xShmLock ? vfstraceShmLock : 0;
445       pNew->xShmBarrier = pSub->xShmBarrier ? vfstraceShmBarrier : 0;
446       pNew->xShmUnmap = pSub->xShmUnmap ? vfstraceShmUnmap : 0;
447     }
448     pFile->pMethods = pNew;
449   }
450   vfstrace_print_errcode(pInfo, " -> %s", rc);
451   vfstrace_printf(pInfo, ", outFlags=0x%x\n", *pOutFlags);
452   return rc;
453 }
454 
455 /*
456 ** Delete the file located at zPath. If the dirSync argument is true,
457 ** ensure the file-system modifications are synced to disk before
458 ** returning.
459 */
460 static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
461   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
462   sqlite3_vfs *pRoot = pInfo->pRootVfs;
463   int rc;
464   vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
465                   pInfo->zVfsName, zPath, dirSync);
466   rc = pRoot->xDelete(pRoot, zPath, dirSync);
467   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
468   return rc;
469 }
470 
471 /*
472 ** Test for access permissions. Return true if the requested permission
473 ** is available, or false otherwise.
474 */
475 static int vfstraceAccess(
476   sqlite3_vfs *pVfs,
477   const char *zPath,
478   int flags,
479   int *pResOut
480 ){
481   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
482   sqlite3_vfs *pRoot = pInfo->pRootVfs;
483   int rc;
484   vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
485                   pInfo->zVfsName, zPath, flags);
486   rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
487   vfstrace_print_errcode(pInfo, " -> %s", rc);
488   vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
489   return rc;
490 }
491 
492 /*
493 ** Populate buffer zOut with the full canonical pathname corresponding
494 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
495 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
496 */
497 static int vfstraceFullPathname(
498   sqlite3_vfs *pVfs,
499   const char *zPath,
500   int nOut,
501   char *zOut
502 ){
503   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
504   sqlite3_vfs *pRoot = pInfo->pRootVfs;
505   int rc;
506   vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
507                   pInfo->zVfsName, zPath);
508   rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
509   vfstrace_print_errcode(pInfo, " -> %s", rc);
510   vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
511   return rc;
512 }
513 
514 /*
515 ** Open the dynamic library located at zPath and return a handle.
516 */
517 static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
518   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
519   sqlite3_vfs *pRoot = pInfo->pRootVfs;
520   vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
521   return pRoot->xDlOpen(pRoot, zPath);
522 }
523 
524 /*
525 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
526 ** utf-8 string describing the most recent error encountered associated
527 ** with dynamic libraries.
528 */
529 static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
530   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
531   sqlite3_vfs *pRoot = pInfo->pRootVfs;
532   vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
533   pRoot->xDlError(pRoot, nByte, zErrMsg);
534   vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
535 }
536 
537 /*
538 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
539 */
540 static void (*vfstraceDlSym(sqlite3_vfs *pVfs,void *p,const char *zSym))(void){
541   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
542   sqlite3_vfs *pRoot = pInfo->pRootVfs;
543   vfstrace_printf(pInfo, "%s.xDlSym(\"%s\")\n", pInfo->zVfsName, zSym);
544   return pRoot->xDlSym(pRoot, p, zSym);
545 }
546 
547 /*
548 ** Close the dynamic library handle pHandle.
549 */
550 static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
551   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
552   sqlite3_vfs *pRoot = pInfo->pRootVfs;
553   vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName);
554   pRoot->xDlClose(pRoot, pHandle);
555 }
556 
557 /*
558 ** Populate the buffer pointed to by zBufOut with nByte bytes of
559 ** random data.
560 */
561 static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
562   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
563   sqlite3_vfs *pRoot = pInfo->pRootVfs;
564   vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
565   return pRoot->xRandomness(pRoot, nByte, zBufOut);
566 }
567 
568 /*
569 ** Sleep for nMicro microseconds. Return the number of microseconds
570 ** actually slept.
571 */
572 static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
573   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
574   sqlite3_vfs *pRoot = pInfo->pRootVfs;
575   return pRoot->xSleep(pRoot, nMicro);
576 }
577 
578 /*
579 ** Return the current time as a Julian Day number in *pTimeOut.
580 */
581 static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
582   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
583   sqlite3_vfs *pRoot = pInfo->pRootVfs;
584   return pRoot->xCurrentTime(pRoot, pTimeOut);
585 }
586 static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
587   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
588   sqlite3_vfs *pRoot = pInfo->pRootVfs;
589   return pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
590 }
591 
592 /*
593 ** Return th3 emost recent error code and message
594 */
595 static int vfstraceGetLastError(sqlite3_vfs *pVfs, int iErr, char *zErr){
596   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
597   sqlite3_vfs *pRoot = pInfo->pRootVfs;
598   return pRoot->xGetLastError(pRoot, iErr, zErr);
599 }
600 
601 /*
602 ** Override system calls.
603 */
604 static int vfstraceSetSystemCall(
605   sqlite3_vfs *pVfs,
606   const char *zName,
607   void *pFunc
608 ){
609   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
610   sqlite3_vfs *pRoot = pInfo->pRootVfs;
611   return pRoot->xSetSystemCall(pRoot, zName, pFunc);
612 }
613 static void *vfstraceGetSystemCall(sqlite3_vfs *pVfs, const char *zName){
614   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
615   sqlite3_vfs *pRoot = pInfo->pRootVfs;
616   return pRoot->xGetSystemCall(pRoot, zName);
617 }
618 static const char *vfstraceNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
619   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
620   sqlite3_vfs *pRoot = pInfo->pRootVfs;
621   return pRoot->xNextSystemCall(pRoot, zName);
622 }
623 
624 
625 /*
626 ** Clients invoke this routine to construct a new trace-vfs shim.
627 **
628 ** Return SQLITE_OK on success.
629 **
630 ** SQLITE_NOMEM is returned in the case of a memory allocation error.
631 ** SQLITE_NOTFOUND is returned if zOldVfsName does not exist.
632 */
633 int vfstrace_register(
634    const char *zTraceName,           /* Name of the newly constructed VFS */
635    const char *zOldVfsName,          /* Name of the underlying VFS */
636    int (*xOut)(const char*,void*),   /* Output routine.  ex: fputs */
637    void *pOutArg,                    /* 2nd argument to xOut.  ex: stderr */
638    int makeDefault                   /* True to make the new VFS the default */
639 ){
640   sqlite3_vfs *pNew;
641   sqlite3_vfs *pRoot;
642   vfstrace_info *pInfo;
643   int nName;
644   int nByte;
645 
646   pRoot = sqlite3_vfs_find(zOldVfsName);
647   if( pRoot==0 ) return SQLITE_NOTFOUND;
648   nName = strlen(zTraceName);
649   nByte = sizeof(*pNew) + sizeof(*pInfo) + nName + 1;
650   pNew = sqlite3_malloc( nByte );
651   if( pNew==0 ) return SQLITE_NOMEM;
652   memset(pNew, 0, nByte);
653   pInfo = (vfstrace_info*)&pNew[1];
654   pNew->iVersion = pRoot->iVersion;
655   pNew->szOsFile = pRoot->szOsFile + sizeof(vfstrace_file);
656   pNew->mxPathname = pRoot->mxPathname;
657   pNew->zName = (char*)&pInfo[1];
658   memcpy((char*)&pInfo[1], zTraceName, nName+1);
659   pNew->pAppData = pInfo;
660   pNew->xOpen = vfstraceOpen;
661   pNew->xDelete = vfstraceDelete;
662   pNew->xAccess = vfstraceAccess;
663   pNew->xFullPathname = vfstraceFullPathname;
664   pNew->xDlOpen = pRoot->xDlOpen==0 ? 0 : vfstraceDlOpen;
665   pNew->xDlError = pRoot->xDlError==0 ? 0 : vfstraceDlError;
666   pNew->xDlSym = pRoot->xDlSym==0 ? 0 : vfstraceDlSym;
667   pNew->xDlClose = pRoot->xDlClose==0 ? 0 : vfstraceDlClose;
668   pNew->xRandomness = vfstraceRandomness;
669   pNew->xSleep = vfstraceSleep;
670   pNew->xCurrentTime = vfstraceCurrentTime;
671   pNew->xGetLastError = pRoot->xGetLastError==0 ? 0 : vfstraceGetLastError;
672   if( pNew->iVersion>=2 ){
673     pNew->xCurrentTimeInt64 = pRoot->xCurrentTimeInt64==0 ? 0 :
674                                    vfstraceCurrentTimeInt64;
675     if( pNew->iVersion>=3 ){
676       pNew->xSetSystemCall = pRoot->xSetSystemCall==0 ? 0 :
677                                    vfstraceSetSystemCall;
678       pNew->xGetSystemCall = pRoot->xGetSystemCall==0 ? 0 :
679                                    vfstraceGetSystemCall;
680       pNew->xNextSystemCall = pRoot->xNextSystemCall==0 ? 0 :
681                                    vfstraceNextSystemCall;
682     }
683   }
684   pInfo->pRootVfs = pRoot;
685   pInfo->xOut = xOut;
686   pInfo->pOutArg = pOutArg;
687   pInfo->zVfsName = pNew->zName;
688   pInfo->pTraceVfs = pNew;
689   vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
690        pInfo->zVfsName, pRoot->zName);
691   return sqlite3_vfs_register(pNew, makeDefault);
692 }
693