Home | History | Annotate | Download | only in src
      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*, sqlite3_syscall_ptr);
     81 static sqlite3_syscall_ptr vfstraceGetSystemCall(sqlite3_vfs*, const char *);
     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 ** Append to a buffer.
    181 */
    182 static void strappend(char *z, int *pI, const char *zAppend){
    183   int i = *pI;
    184   while( zAppend[0] ){ z[i++] = *(zAppend++); }
    185   z[i] = 0;
    186   *pI = i;
    187 }
    188 
    189 /*
    190 ** Close an vfstrace-file.
    191 */
    192 static int vfstraceClose(sqlite3_file *pFile){
    193   vfstrace_file *p = (vfstrace_file *)pFile;
    194   vfstrace_info *pInfo = p->pInfo;
    195   int rc;
    196   vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
    197   rc = p->pReal->pMethods->xClose(p->pReal);
    198   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    199   if( rc==SQLITE_OK ){
    200     sqlite3_free((void*)p->base.pMethods);
    201     p->base.pMethods = 0;
    202   }
    203   return rc;
    204 }
    205 
    206 /*
    207 ** Read data from an vfstrace-file.
    208 */
    209 static int vfstraceRead(
    210   sqlite3_file *pFile,
    211   void *zBuf,
    212   int iAmt,
    213   sqlite_int64 iOfst
    214 ){
    215   vfstrace_file *p = (vfstrace_file *)pFile;
    216   vfstrace_info *pInfo = p->pInfo;
    217   int rc;
    218   vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)",
    219                   pInfo->zVfsName, p->zFName, iAmt, iOfst);
    220   rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
    221   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    222   return rc;
    223 }
    224 
    225 /*
    226 ** Write data to an vfstrace-file.
    227 */
    228 static int vfstraceWrite(
    229   sqlite3_file *pFile,
    230   const void *zBuf,
    231   int iAmt,
    232   sqlite_int64 iOfst
    233 ){
    234   vfstrace_file *p = (vfstrace_file *)pFile;
    235   vfstrace_info *pInfo = p->pInfo;
    236   int rc;
    237   vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)",
    238                   pInfo->zVfsName, p->zFName, iAmt, iOfst);
    239   rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
    240   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    241   return rc;
    242 }
    243 
    244 /*
    245 ** Truncate an vfstrace-file.
    246 */
    247 static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
    248   vfstrace_file *p = (vfstrace_file *)pFile;
    249   vfstrace_info *pInfo = p->pInfo;
    250   int rc;
    251   vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
    252                   size);
    253   rc = p->pReal->pMethods->xTruncate(p->pReal, size);
    254   vfstrace_printf(pInfo, " -> %d\n", rc);
    255   return rc;
    256 }
    257 
    258 /*
    259 ** Sync an vfstrace-file.
    260 */
    261 static int vfstraceSync(sqlite3_file *pFile, int flags){
    262   vfstrace_file *p = (vfstrace_file *)pFile;
    263   vfstrace_info *pInfo = p->pInfo;
    264   int rc;
    265   int i;
    266   char zBuf[100];
    267   memcpy(zBuf, "|0", 3);
    268   i = 0;
    269   if( flags & SQLITE_SYNC_FULL )        strappend(zBuf, &i, "|FULL");
    270   else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL");
    271   if( flags & SQLITE_SYNC_DATAONLY )    strappend(zBuf, &i, "|DATAONLY");
    272   if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){
    273     sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags);
    274   }
    275   vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName,
    276                   &zBuf[1]);
    277   rc = p->pReal->pMethods->xSync(p->pReal, flags);
    278   vfstrace_printf(pInfo, " -> %d\n", rc);
    279   return rc;
    280 }
    281 
    282 /*
    283 ** Return the current file-size of an vfstrace-file.
    284 */
    285 static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
    286   vfstrace_file *p = (vfstrace_file *)pFile;
    287   vfstrace_info *pInfo = p->pInfo;
    288   int rc;
    289   vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
    290   rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
    291   vfstrace_print_errcode(pInfo, " -> %s,", rc);
    292   vfstrace_printf(pInfo, " size=%lld\n", *pSize);
    293   return rc;
    294 }
    295 
    296 /*
    297 ** Return the name of a lock.
    298 */
    299 static const char *lockName(int eLock){
    300   const char *azLockNames[] = {
    301      "NONE", "SHARED", "RESERVED", "PENDING", "EXCLUSIVE"
    302   };
    303   if( eLock<0 || eLock>=sizeof(azLockNames)/sizeof(azLockNames[0]) ){
    304     return "???";
    305   }else{
    306     return azLockNames[eLock];
    307   }
    308 }
    309 
    310 /*
    311 ** Lock an vfstrace-file.
    312 */
    313 static int vfstraceLock(sqlite3_file *pFile, int eLock){
    314   vfstrace_file *p = (vfstrace_file *)pFile;
    315   vfstrace_info *pInfo = p->pInfo;
    316   int rc;
    317   vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName,
    318                   lockName(eLock));
    319   rc = p->pReal->pMethods->xLock(p->pReal, eLock);
    320   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    321   return rc;
    322 }
    323 
    324 /*
    325 ** Unlock an vfstrace-file.
    326 */
    327 static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
    328   vfstrace_file *p = (vfstrace_file *)pFile;
    329   vfstrace_info *pInfo = p->pInfo;
    330   int rc;
    331   vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName,
    332                   lockName(eLock));
    333   rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
    334   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    335   return rc;
    336 }
    337 
    338 /*
    339 ** Check if another file-handle holds a RESERVED lock on an vfstrace-file.
    340 */
    341 static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
    342   vfstrace_file *p = (vfstrace_file *)pFile;
    343   vfstrace_info *pInfo = p->pInfo;
    344   int rc;
    345   vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)",
    346                   pInfo->zVfsName, p->zFName);
    347   rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
    348   vfstrace_print_errcode(pInfo, " -> %s", rc);
    349   vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
    350   return rc;
    351 }
    352 
    353 /*
    354 ** File control method. For custom operations on an vfstrace-file.
    355 */
    356 static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){
    357   vfstrace_file *p = (vfstrace_file *)pFile;
    358   vfstrace_info *pInfo = p->pInfo;
    359   int rc;
    360   char zBuf[100];
    361   char *zOp;
    362   switch( op ){
    363     case SQLITE_FCNTL_LOCKSTATE:    zOp = "LOCKSTATE";          break;
    364     case SQLITE_GET_LOCKPROXYFILE:  zOp = "GET_LOCKPROXYFILE";  break;
    365     case SQLITE_SET_LOCKPROXYFILE:  zOp = "SET_LOCKPROXYFILE";  break;
    366     case SQLITE_LAST_ERRNO:         zOp = "LAST_ERRNO";         break;
    367     case SQLITE_FCNTL_SIZE_HINT: {
    368       sqlite3_snprintf(sizeof(zBuf), zBuf, "SIZE_HINT,%lld",
    369                        *(sqlite3_int64*)pArg);
    370       zOp = zBuf;
    371       break;
    372     }
    373     case SQLITE_FCNTL_CHUNK_SIZE: {
    374       sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg);
    375       zOp = zBuf;
    376       break;
    377     }
    378     case SQLITE_FCNTL_FILE_POINTER: zOp = "FILE_POINTER";       break;
    379     case SQLITE_FCNTL_SYNC_OMITTED: zOp = "SYNC_OMITTED";       break;
    380     case 0xca093fa0:                zOp = "DB_UNCHANGED";       break;
    381     default: {
    382       sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op);
    383       zOp = zBuf;
    384       break;
    385     }
    386   }
    387   vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)",
    388                   pInfo->zVfsName, p->zFName, zOp);
    389   rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
    390   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    391   return rc;
    392 }
    393 
    394 /*
    395 ** Return the sector-size in bytes for an vfstrace-file.
    396 */
    397 static int vfstraceSectorSize(sqlite3_file *pFile){
    398   vfstrace_file *p = (vfstrace_file *)pFile;
    399   vfstrace_info *pInfo = p->pInfo;
    400   int rc;
    401   vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
    402   rc = p->pReal->pMethods->xSectorSize(p->pReal);
    403   vfstrace_printf(pInfo, " -> %d\n", rc);
    404   return rc;
    405 }
    406 
    407 /*
    408 ** Return the device characteristic flags supported by an vfstrace-file.
    409 */
    410 static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
    411   vfstrace_file *p = (vfstrace_file *)pFile;
    412   vfstrace_info *pInfo = p->pInfo;
    413   int rc;
    414   vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
    415                   pInfo->zVfsName, p->zFName);
    416   rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
    417   vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
    418   return rc;
    419 }
    420 
    421 /*
    422 ** Shared-memory operations.
    423 */
    424 static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
    425   vfstrace_file *p = (vfstrace_file *)pFile;
    426   vfstrace_info *pInfo = p->pInfo;
    427   int rc;
    428   char zLck[100];
    429   int i = 0;
    430   memcpy(zLck, "|0", 3);
    431   if( flags & SQLITE_SHM_UNLOCK )    strappend(zLck, &i, "|UNLOCK");
    432   if( flags & SQLITE_SHM_LOCK )      strappend(zLck, &i, "|LOCK");
    433   if( flags & SQLITE_SHM_SHARED )    strappend(zLck, &i, "|SHARED");
    434   if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE");
    435   if( flags & ~(0xf) ){
    436      sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags);
    437   }
    438   vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d,n=%d,%s)",
    439                   pInfo->zVfsName, p->zFName, ofst, n, &zLck[1]);
    440   rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
    441   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    442   return rc;
    443 }
    444 static int vfstraceShmMap(
    445   sqlite3_file *pFile,
    446   int iRegion,
    447   int szRegion,
    448   int isWrite,
    449   void volatile **pp
    450 ){
    451   vfstrace_file *p = (vfstrace_file *)pFile;
    452   vfstrace_info *pInfo = p->pInfo;
    453   int rc;
    454   vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
    455                   pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
    456   rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
    457   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    458   return rc;
    459 }
    460 static void vfstraceShmBarrier(sqlite3_file *pFile){
    461   vfstrace_file *p = (vfstrace_file *)pFile;
    462   vfstrace_info *pInfo = p->pInfo;
    463   vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
    464   p->pReal->pMethods->xShmBarrier(p->pReal);
    465 }
    466 static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
    467   vfstrace_file *p = (vfstrace_file *)pFile;
    468   vfstrace_info *pInfo = p->pInfo;
    469   int rc;
    470   vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
    471                   pInfo->zVfsName, p->zFName, delFlag);
    472   rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
    473   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    474   return rc;
    475 }
    476 
    477 
    478 
    479 /*
    480 ** Open an vfstrace file handle.
    481 */
    482 static int vfstraceOpen(
    483   sqlite3_vfs *pVfs,
    484   const char *zName,
    485   sqlite3_file *pFile,
    486   int flags,
    487   int *pOutFlags
    488 ){
    489   int rc;
    490   vfstrace_file *p = (vfstrace_file *)pFile;
    491   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    492   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    493   p->pInfo = pInfo;
    494   p->zFName = zName ? fileTail(zName) : "<temp>";
    495   p->pReal = (sqlite3_file *)&p[1];
    496   rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
    497   vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
    498                   pInfo->zVfsName, p->zFName, flags);
    499   if( p->pReal->pMethods ){
    500     sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) );
    501     const sqlite3_io_methods *pSub = p->pReal->pMethods;
    502     memset(pNew, 0, sizeof(*pNew));
    503     pNew->iVersion = pSub->iVersion;
    504     pNew->xClose = vfstraceClose;
    505     pNew->xRead = vfstraceRead;
    506     pNew->xWrite = vfstraceWrite;
    507     pNew->xTruncate = vfstraceTruncate;
    508     pNew->xSync = vfstraceSync;
    509     pNew->xFileSize = vfstraceFileSize;
    510     pNew->xLock = vfstraceLock;
    511     pNew->xUnlock = vfstraceUnlock;
    512     pNew->xCheckReservedLock = vfstraceCheckReservedLock;
    513     pNew->xFileControl = vfstraceFileControl;
    514     pNew->xSectorSize = vfstraceSectorSize;
    515     pNew->xDeviceCharacteristics = vfstraceDeviceCharacteristics;
    516     if( pNew->iVersion>=2 ){
    517       pNew->xShmMap = pSub->xShmMap ? vfstraceShmMap : 0;
    518       pNew->xShmLock = pSub->xShmLock ? vfstraceShmLock : 0;
    519       pNew->xShmBarrier = pSub->xShmBarrier ? vfstraceShmBarrier : 0;
    520       pNew->xShmUnmap = pSub->xShmUnmap ? vfstraceShmUnmap : 0;
    521     }
    522     pFile->pMethods = pNew;
    523   }
    524   vfstrace_print_errcode(pInfo, " -> %s", rc);
    525   if( pOutFlags ){
    526     vfstrace_printf(pInfo, ", outFlags=0x%x\n", *pOutFlags);
    527   }else{
    528     vfstrace_printf(pInfo, "\n");
    529   }
    530   return rc;
    531 }
    532 
    533 /*
    534 ** Delete the file located at zPath. If the dirSync argument is true,
    535 ** ensure the file-system modifications are synced to disk before
    536 ** returning.
    537 */
    538 static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
    539   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    540   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    541   int rc;
    542   vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
    543                   pInfo->zVfsName, zPath, dirSync);
    544   rc = pRoot->xDelete(pRoot, zPath, dirSync);
    545   vfstrace_print_errcode(pInfo, " -> %s\n", rc);
    546   return rc;
    547 }
    548 
    549 /*
    550 ** Test for access permissions. Return true if the requested permission
    551 ** is available, or false otherwise.
    552 */
    553 static int vfstraceAccess(
    554   sqlite3_vfs *pVfs,
    555   const char *zPath,
    556   int flags,
    557   int *pResOut
    558 ){
    559   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    560   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    561   int rc;
    562   vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
    563                   pInfo->zVfsName, zPath, flags);
    564   rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
    565   vfstrace_print_errcode(pInfo, " -> %s", rc);
    566   vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
    567   return rc;
    568 }
    569 
    570 /*
    571 ** Populate buffer zOut with the full canonical pathname corresponding
    572 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
    573 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
    574 */
    575 static int vfstraceFullPathname(
    576   sqlite3_vfs *pVfs,
    577   const char *zPath,
    578   int nOut,
    579   char *zOut
    580 ){
    581   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    582   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    583   int rc;
    584   vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
    585                   pInfo->zVfsName, zPath);
    586   rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
    587   vfstrace_print_errcode(pInfo, " -> %s", rc);
    588   vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
    589   return rc;
    590 }
    591 
    592 /*
    593 ** Open the dynamic library located at zPath and return a handle.
    594 */
    595 static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
    596   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    597   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    598   vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
    599   return pRoot->xDlOpen(pRoot, zPath);
    600 }
    601 
    602 /*
    603 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
    604 ** utf-8 string describing the most recent error encountered associated
    605 ** with dynamic libraries.
    606 */
    607 static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
    608   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    609   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    610   vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
    611   pRoot->xDlError(pRoot, nByte, zErrMsg);
    612   vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
    613 }
    614 
    615 /*
    616 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
    617 */
    618 static void (*vfstraceDlSym(sqlite3_vfs *pVfs,void *p,const char *zSym))(void){
    619   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    620   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    621   vfstrace_printf(pInfo, "%s.xDlSym(\"%s\")\n", pInfo->zVfsName, zSym);
    622   return pRoot->xDlSym(pRoot, p, zSym);
    623 }
    624 
    625 /*
    626 ** Close the dynamic library handle pHandle.
    627 */
    628 static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
    629   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    630   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    631   vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName);
    632   pRoot->xDlClose(pRoot, pHandle);
    633 }
    634 
    635 /*
    636 ** Populate the buffer pointed to by zBufOut with nByte bytes of
    637 ** random data.
    638 */
    639 static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
    640   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    641   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    642   vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
    643   return pRoot->xRandomness(pRoot, nByte, zBufOut);
    644 }
    645 
    646 /*
    647 ** Sleep for nMicro microseconds. Return the number of microseconds
    648 ** actually slept.
    649 */
    650 static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
    651   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    652   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    653   return pRoot->xSleep(pRoot, nMicro);
    654 }
    655 
    656 /*
    657 ** Return the current time as a Julian Day number in *pTimeOut.
    658 */
    659 static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
    660   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    661   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    662   return pRoot->xCurrentTime(pRoot, pTimeOut);
    663 }
    664 static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
    665   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    666   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    667   return pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
    668 }
    669 
    670 /*
    671 ** Return th3 emost recent error code and message
    672 */
    673 static int vfstraceGetLastError(sqlite3_vfs *pVfs, int iErr, char *zErr){
    674   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    675   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    676   return pRoot->xGetLastError(pRoot, iErr, zErr);
    677 }
    678 
    679 /*
    680 ** Override system calls.
    681 */
    682 static int vfstraceSetSystemCall(
    683   sqlite3_vfs *pVfs,
    684   const char *zName,
    685   sqlite3_syscall_ptr pFunc
    686 ){
    687   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    688   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    689   return pRoot->xSetSystemCall(pRoot, zName, pFunc);
    690 }
    691 static sqlite3_syscall_ptr vfstraceGetSystemCall(
    692   sqlite3_vfs *pVfs,
    693   const char *zName
    694 ){
    695   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    696   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    697   return pRoot->xGetSystemCall(pRoot, zName);
    698 }
    699 static const char *vfstraceNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
    700   vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
    701   sqlite3_vfs *pRoot = pInfo->pRootVfs;
    702   return pRoot->xNextSystemCall(pRoot, zName);
    703 }
    704 
    705 
    706 /*
    707 ** Clients invoke this routine to construct a new trace-vfs shim.
    708 **
    709 ** Return SQLITE_OK on success.
    710 **
    711 ** SQLITE_NOMEM is returned in the case of a memory allocation error.
    712 ** SQLITE_NOTFOUND is returned if zOldVfsName does not exist.
    713 */
    714 int vfstrace_register(
    715    const char *zTraceName,           /* Name of the newly constructed VFS */
    716    const char *zOldVfsName,          /* Name of the underlying VFS */
    717    int (*xOut)(const char*,void*),   /* Output routine.  ex: fputs */
    718    void *pOutArg,                    /* 2nd argument to xOut.  ex: stderr */
    719    int makeDefault                   /* True to make the new VFS the default */
    720 ){
    721   sqlite3_vfs *pNew;
    722   sqlite3_vfs *pRoot;
    723   vfstrace_info *pInfo;
    724   int nName;
    725   int nByte;
    726 
    727   pRoot = sqlite3_vfs_find(zOldVfsName);
    728   if( pRoot==0 ) return SQLITE_NOTFOUND;
    729   nName = strlen(zTraceName);
    730   nByte = sizeof(*pNew) + sizeof(*pInfo) + nName + 1;
    731   pNew = sqlite3_malloc( nByte );
    732   if( pNew==0 ) return SQLITE_NOMEM;
    733   memset(pNew, 0, nByte);
    734   pInfo = (vfstrace_info*)&pNew[1];
    735   pNew->iVersion = pRoot->iVersion;
    736   pNew->szOsFile = pRoot->szOsFile + sizeof(vfstrace_file);
    737   pNew->mxPathname = pRoot->mxPathname;
    738   pNew->zName = (char*)&pInfo[1];
    739   memcpy((char*)&pInfo[1], zTraceName, nName+1);
    740   pNew->pAppData = pInfo;
    741   pNew->xOpen = vfstraceOpen;
    742   pNew->xDelete = vfstraceDelete;
    743   pNew->xAccess = vfstraceAccess;
    744   pNew->xFullPathname = vfstraceFullPathname;
    745   pNew->xDlOpen = pRoot->xDlOpen==0 ? 0 : vfstraceDlOpen;
    746   pNew->xDlError = pRoot->xDlError==0 ? 0 : vfstraceDlError;
    747   pNew->xDlSym = pRoot->xDlSym==0 ? 0 : vfstraceDlSym;
    748   pNew->xDlClose = pRoot->xDlClose==0 ? 0 : vfstraceDlClose;
    749   pNew->xRandomness = vfstraceRandomness;
    750   pNew->xSleep = vfstraceSleep;
    751   pNew->xCurrentTime = vfstraceCurrentTime;
    752   pNew->xGetLastError = pRoot->xGetLastError==0 ? 0 : vfstraceGetLastError;
    753   if( pNew->iVersion>=2 ){
    754     pNew->xCurrentTimeInt64 = pRoot->xCurrentTimeInt64==0 ? 0 :
    755                                    vfstraceCurrentTimeInt64;
    756     if( pNew->iVersion>=3 ){
    757       pNew->xSetSystemCall = pRoot->xSetSystemCall==0 ? 0 :
    758                                    vfstraceSetSystemCall;
    759       pNew->xGetSystemCall = pRoot->xGetSystemCall==0 ? 0 :
    760                                    vfstraceGetSystemCall;
    761       pNew->xNextSystemCall = pRoot->xNextSystemCall==0 ? 0 :
    762                                    vfstraceNextSystemCall;
    763     }
    764   }
    765   pInfo->pRootVfs = pRoot;
    766   pInfo->xOut = xOut;
    767   pInfo->pOutArg = pOutArg;
    768   pInfo->zVfsName = pNew->zName;
    769   pInfo->pTraceVfs = pNew;
    770   vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
    771        pInfo->zVfsName, pRoot->zName);
    772   return sqlite3_vfs_register(pNew, makeDefault);
    773 }
    774