test_vfstrace.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 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. ** An instance of this structure is attached to the each trace VFS to
  115. ** provide auxiliary information.
  116. */
  117. typedef struct vfstrace_info vfstrace_info;
  118. struct vfstrace_info {
  119. sqlite3_vfs *pRootVfs; /* The underlying real VFS */
  120. int (*xOut)(const char*, void*); /* Send output here */
  121. void *pOutArg; /* First argument to xOut */
  122. const char *zVfsName; /* Name of this trace-VFS */
  123. sqlite3_vfs *pTraceVfs; /* Pointer back to the trace VFS */
  124. };
  125. /*
  126. ** The sqlite3_file object for the trace VFS
  127. */
  128. typedef struct vfstrace_file vfstrace_file;
  129. struct vfstrace_file {
  130. sqlite3_file base; /* Base class. Must be first */
  131. vfstrace_info *pInfo; /* The trace-VFS to which this file belongs */
  132. const char *zFName; /* Base name of the file */
  133. sqlite3_file *pReal; /* The real underlying file */
  134. };
  135. /*
  136. ** Method declarations for vfstrace_file.
  137. */
  138. static int vfstraceClose(sqlite3_file*);
  139. static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
  140. static int vfstraceWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
  141. static int vfstraceTruncate(sqlite3_file*, sqlite3_int64 size);
  142. static int vfstraceSync(sqlite3_file*, int flags);
  143. static int vfstraceFileSize(sqlite3_file*, sqlite3_int64 *pSize);
  144. static int vfstraceLock(sqlite3_file*, int);
  145. static int vfstraceUnlock(sqlite3_file*, int);
  146. static int vfstraceCheckReservedLock(sqlite3_file*, int *);
  147. static int vfstraceFileControl(sqlite3_file*, int op, void *pArg);
  148. static int vfstraceSectorSize(sqlite3_file*);
  149. static int vfstraceDeviceCharacteristics(sqlite3_file*);
  150. static int vfstraceShmLock(sqlite3_file*,int,int,int);
  151. static int vfstraceShmMap(sqlite3_file*,int,int,int, void volatile **);
  152. static void vfstraceShmBarrier(sqlite3_file*);
  153. static int vfstraceShmUnmap(sqlite3_file*,int);
  154. /*
  155. ** Method declarations for vfstrace_vfs.
  156. */
  157. static int vfstraceOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
  158. static int vfstraceDelete(sqlite3_vfs*, const char *zName, int syncDir);
  159. static int vfstraceAccess(sqlite3_vfs*, const char *zName, int flags, int *);
  160. static int vfstraceFullPathname(sqlite3_vfs*, const char *zName, int, char *);
  161. static void *vfstraceDlOpen(sqlite3_vfs*, const char *zFilename);
  162. static void vfstraceDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
  163. static void (*vfstraceDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
  164. static void vfstraceDlClose(sqlite3_vfs*, void*);
  165. static int vfstraceRandomness(sqlite3_vfs*, int nByte, char *zOut);
  166. static int vfstraceSleep(sqlite3_vfs*, int microseconds);
  167. static int vfstraceCurrentTime(sqlite3_vfs*, double*);
  168. static int vfstraceGetLastError(sqlite3_vfs*, int, char*);
  169. static int vfstraceCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
  170. static int vfstraceSetSystemCall(sqlite3_vfs*,const char*, sqlite3_syscall_ptr);
  171. static sqlite3_syscall_ptr vfstraceGetSystemCall(sqlite3_vfs*, const char *);
  172. static const char *vfstraceNextSystemCall(sqlite3_vfs*, const char *zName);
  173. /*
  174. ** Return a pointer to the tail of the pathname. Examples:
  175. **
  176. ** /home/drh/xyzzy.txt -> xyzzy.txt
  177. ** xyzzy.txt -> xyzzy.txt
  178. */
  179. static const char *fileTail(const char *z){
  180. int i;
  181. if( z==0 ) return 0;
  182. i = strlen(z)-1;
  183. while( i>0 && z[i-1]!='/' ){ i--; }
  184. return &z[i];
  185. }
  186. /*
  187. ** Send trace output defined by zFormat and subsequent arguments.
  188. */
  189. static void vfstrace_printf(
  190. vfstrace_info *pInfo,
  191. const char *zFormat,
  192. ...
  193. ){
  194. va_list ap;
  195. char *zMsg;
  196. va_start(ap, zFormat);
  197. zMsg = sqlite3_vmprintf(zFormat, ap);
  198. va_end(ap);
  199. pInfo->xOut(zMsg, pInfo->pOutArg);
  200. sqlite3_free(zMsg);
  201. }
  202. /*
  203. ** Convert value rc into a string and print it using zFormat. zFormat
  204. ** should have exactly one %s
  205. */
  206. static void vfstrace_print_errcode(
  207. vfstrace_info *pInfo,
  208. const char *zFormat,
  209. int rc
  210. ){
  211. char zBuf[50];
  212. char *zVal;
  213. switch( rc ){
  214. case SQLITE_OK: zVal = "SQLITE_OK"; break;
  215. case SQLITE_ERROR: zVal = "SQLITE_ERROR"; break;
  216. case SQLITE_PERM: zVal = "SQLITE_PERM"; break;
  217. case SQLITE_ABORT: zVal = "SQLITE_ABORT"; break;
  218. case SQLITE_BUSY: zVal = "SQLITE_BUSY"; break;
  219. case SQLITE_NOMEM: zVal = "SQLITE_NOMEM"; break;
  220. case SQLITE_READONLY: zVal = "SQLITE_READONLY"; break;
  221. case SQLITE_INTERRUPT: zVal = "SQLITE_INTERRUPT"; break;
  222. case SQLITE_IOERR: zVal = "SQLITE_IOERR"; break;
  223. case SQLITE_CORRUPT: zVal = "SQLITE_CORRUPT"; break;
  224. case SQLITE_FULL: zVal = "SQLITE_FULL"; break;
  225. case SQLITE_CANTOPEN: zVal = "SQLITE_CANTOPEN"; break;
  226. case SQLITE_PROTOCOL: zVal = "SQLITE_PROTOCOL"; break;
  227. case SQLITE_EMPTY: zVal = "SQLITE_EMPTY"; break;
  228. case SQLITE_SCHEMA: zVal = "SQLITE_SCHEMA"; break;
  229. case SQLITE_CONSTRAINT: zVal = "SQLITE_CONSTRAINT"; break;
  230. case SQLITE_MISMATCH: zVal = "SQLITE_MISMATCH"; break;
  231. case SQLITE_MISUSE: zVal = "SQLITE_MISUSE"; break;
  232. case SQLITE_NOLFS: zVal = "SQLITE_NOLFS"; break;
  233. case SQLITE_IOERR_READ: zVal = "SQLITE_IOERR_READ"; break;
  234. case SQLITE_IOERR_SHORT_READ: zVal = "SQLITE_IOERR_SHORT_READ"; break;
  235. case SQLITE_IOERR_WRITE: zVal = "SQLITE_IOERR_WRITE"; break;
  236. case SQLITE_IOERR_FSYNC: zVal = "SQLITE_IOERR_FSYNC"; break;
  237. case SQLITE_IOERR_DIR_FSYNC: zVal = "SQLITE_IOERR_DIR_FSYNC"; break;
  238. case SQLITE_IOERR_TRUNCATE: zVal = "SQLITE_IOERR_TRUNCATE"; break;
  239. case SQLITE_IOERR_FSTAT: zVal = "SQLITE_IOERR_FSTAT"; break;
  240. case SQLITE_IOERR_UNLOCK: zVal = "SQLITE_IOERR_UNLOCK"; break;
  241. case SQLITE_IOERR_RDLOCK: zVal = "SQLITE_IOERR_RDLOCK"; break;
  242. case SQLITE_IOERR_DELETE: zVal = "SQLITE_IOERR_DELETE"; break;
  243. case SQLITE_IOERR_BLOCKED: zVal = "SQLITE_IOERR_BLOCKED"; break;
  244. case SQLITE_IOERR_NOMEM: zVal = "SQLITE_IOERR_NOMEM"; break;
  245. case SQLITE_IOERR_ACCESS: zVal = "SQLITE_IOERR_ACCESS"; break;
  246. case SQLITE_IOERR_CHECKRESERVEDLOCK:
  247. zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
  248. case SQLITE_IOERR_LOCK: zVal = "SQLITE_IOERR_LOCK"; break;
  249. case SQLITE_IOERR_CLOSE: zVal = "SQLITE_IOERR_CLOSE"; break;
  250. case SQLITE_IOERR_DIR_CLOSE: zVal = "SQLITE_IOERR_DIR_CLOSE"; break;
  251. case SQLITE_IOERR_SHMOPEN: zVal = "SQLITE_IOERR_SHMOPEN"; break;
  252. case SQLITE_IOERR_SHMSIZE: zVal = "SQLITE_IOERR_SHMSIZE"; break;
  253. case SQLITE_IOERR_SHMLOCK: zVal = "SQLITE_IOERR_SHMLOCK"; break;
  254. case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break;
  255. case SQLITE_BUSY_RECOVERY: zVal = "SQLITE_BUSY_RECOVERY"; break;
  256. case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break;
  257. default: {
  258. sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
  259. zVal = zBuf;
  260. break;
  261. }
  262. }
  263. vfstrace_printf(pInfo, zFormat, zVal);
  264. }
  265. /*
  266. ** Append to a buffer.
  267. */
  268. static void strappend(char *z, int *pI, const char *zAppend){
  269. int i = *pI;
  270. while( zAppend[0] ){ z[i++] = *(zAppend++); }
  271. z[i] = 0;
  272. *pI = i;
  273. }
  274. /*
  275. ** Close an vfstrace-file.
  276. */
  277. static int vfstraceClose(sqlite3_file *pFile){
  278. vfstrace_file *p = (vfstrace_file *)pFile;
  279. vfstrace_info *pInfo = p->pInfo;
  280. int rc;
  281. vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
  282. rc = p->pReal->pMethods->xClose(p->pReal);
  283. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  284. if( rc==SQLITE_OK ){
  285. sqlite3_free((void*)p->base.pMethods);
  286. p->base.pMethods = 0;
  287. }
  288. return rc;
  289. }
  290. /*
  291. ** Read data from an vfstrace-file.
  292. */
  293. static int vfstraceRead(
  294. sqlite3_file *pFile,
  295. void *zBuf,
  296. int iAmt,
  297. sqlite_int64 iOfst
  298. ){
  299. vfstrace_file *p = (vfstrace_file *)pFile;
  300. vfstrace_info *pInfo = p->pInfo;
  301. int rc;
  302. vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)",
  303. pInfo->zVfsName, p->zFName, iAmt, iOfst);
  304. rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
  305. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  306. return rc;
  307. }
  308. /*
  309. ** Write data to an vfstrace-file.
  310. */
  311. static int vfstraceWrite(
  312. sqlite3_file *pFile,
  313. const void *zBuf,
  314. int iAmt,
  315. sqlite_int64 iOfst
  316. ){
  317. vfstrace_file *p = (vfstrace_file *)pFile;
  318. vfstrace_info *pInfo = p->pInfo;
  319. int rc;
  320. vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)",
  321. pInfo->zVfsName, p->zFName, iAmt, iOfst);
  322. rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
  323. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  324. return rc;
  325. }
  326. /*
  327. ** Truncate an vfstrace-file.
  328. */
  329. static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
  330. vfstrace_file *p = (vfstrace_file *)pFile;
  331. vfstrace_info *pInfo = p->pInfo;
  332. int rc;
  333. vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
  334. size);
  335. rc = p->pReal->pMethods->xTruncate(p->pReal, size);
  336. vfstrace_printf(pInfo, " -> %d\n", rc);
  337. return rc;
  338. }
  339. /*
  340. ** Sync an vfstrace-file.
  341. */
  342. static int vfstraceSync(sqlite3_file *pFile, int flags){
  343. vfstrace_file *p = (vfstrace_file *)pFile;
  344. vfstrace_info *pInfo = p->pInfo;
  345. int rc;
  346. int i;
  347. char zBuf[100];
  348. memcpy(zBuf, "|0", 3);
  349. i = 0;
  350. if( flags & SQLITE_SYNC_FULL ) strappend(zBuf, &i, "|FULL");
  351. else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL");
  352. if( flags & SQLITE_SYNC_DATAONLY ) strappend(zBuf, &i, "|DATAONLY");
  353. if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){
  354. sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags);
  355. }
  356. vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName,
  357. &zBuf[1]);
  358. rc = p->pReal->pMethods->xSync(p->pReal, flags);
  359. vfstrace_printf(pInfo, " -> %d\n", rc);
  360. return rc;
  361. }
  362. /*
  363. ** Return the current file-size of an vfstrace-file.
  364. */
  365. static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
  366. vfstrace_file *p = (vfstrace_file *)pFile;
  367. vfstrace_info *pInfo = p->pInfo;
  368. int rc;
  369. vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
  370. rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
  371. vfstrace_print_errcode(pInfo, " -> %s,", rc);
  372. vfstrace_printf(pInfo, " size=%lld\n", *pSize);
  373. return rc;
  374. }
  375. /*
  376. ** Return the name of a lock.
  377. */
  378. static const char *lockName(int eLock){
  379. const char *azLockNames[] = {
  380. "NONE", "SHARED", "RESERVED", "PENDING", "EXCLUSIVE"
  381. };
  382. if( eLock<0 || eLock>=sizeof(azLockNames)/sizeof(azLockNames[0]) ){
  383. return "???";
  384. }else{
  385. return azLockNames[eLock];
  386. }
  387. }
  388. /*
  389. ** Lock an vfstrace-file.
  390. */
  391. static int vfstraceLock(sqlite3_file *pFile, int eLock){
  392. vfstrace_file *p = (vfstrace_file *)pFile;
  393. vfstrace_info *pInfo = p->pInfo;
  394. int rc;
  395. vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName,
  396. lockName(eLock));
  397. rc = p->pReal->pMethods->xLock(p->pReal, eLock);
  398. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  399. return rc;
  400. }
  401. /*
  402. ** Unlock an vfstrace-file.
  403. */
  404. static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
  405. vfstrace_file *p = (vfstrace_file *)pFile;
  406. vfstrace_info *pInfo = p->pInfo;
  407. int rc;
  408. vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName,
  409. lockName(eLock));
  410. rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
  411. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  412. return rc;
  413. }
  414. /*
  415. ** Check if another file-handle holds a RESERVED lock on an vfstrace-file.
  416. */
  417. static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
  418. vfstrace_file *p = (vfstrace_file *)pFile;
  419. vfstrace_info *pInfo = p->pInfo;
  420. int rc;
  421. vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)",
  422. pInfo->zVfsName, p->zFName);
  423. rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
  424. vfstrace_print_errcode(pInfo, " -> %s", rc);
  425. vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
  426. return rc;
  427. }
  428. /*
  429. ** File control method. For custom operations on an vfstrace-file.
  430. */
  431. static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){
  432. vfstrace_file *p = (vfstrace_file *)pFile;
  433. vfstrace_info *pInfo = p->pInfo;
  434. int rc;
  435. char zBuf[100];
  436. char *zOp;
  437. switch( op ){
  438. case SQLITE_FCNTL_LOCKSTATE: zOp = "LOCKSTATE"; break;
  439. case SQLITE_GET_LOCKPROXYFILE: zOp = "GET_LOCKPROXYFILE"; break;
  440. case SQLITE_SET_LOCKPROXYFILE: zOp = "SET_LOCKPROXYFILE"; break;
  441. case SQLITE_LAST_ERRNO: zOp = "LAST_ERRNO"; break;
  442. case SQLITE_FCNTL_SIZE_HINT: {
  443. sqlite3_snprintf(sizeof(zBuf), zBuf, "SIZE_HINT,%lld",
  444. *(sqlite3_int64*)pArg);
  445. zOp = zBuf;
  446. break;
  447. }
  448. case SQLITE_FCNTL_CHUNK_SIZE: {
  449. sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg);
  450. zOp = zBuf;
  451. break;
  452. }
  453. case SQLITE_FCNTL_FILE_POINTER: zOp = "FILE_POINTER"; break;
  454. case SQLITE_FCNTL_SYNC_OMITTED: zOp = "SYNC_OMITTED"; break;
  455. case SQLITE_FCNTL_WIN32_AV_RETRY: zOp = "WIN32_AV_RETRY"; break;
  456. case SQLITE_FCNTL_PERSIST_WAL: zOp = "PERSIST_WAL"; break;
  457. case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break;
  458. case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break;
  459. case SQLITE_FCNTL_TEMPFILENAME: zOp = "TEMPFILENAME"; break;
  460. case 0xca093fa0: zOp = "DB_UNCHANGED"; break;
  461. case SQLITE_FCNTL_PRAGMA: {
  462. const char *const* a = (const char*const*)pArg;
  463. sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]);
  464. zOp = zBuf;
  465. break;
  466. }
  467. default: {
  468. sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op);
  469. zOp = zBuf;
  470. break;
  471. }
  472. }
  473. vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)",
  474. pInfo->zVfsName, p->zFName, zOp);
  475. rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
  476. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  477. if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){
  478. *(char**)pArg = sqlite3_mprintf("vfstrace.%s/%z",
  479. pInfo->zVfsName, *(char**)pArg);
  480. }
  481. if( (op==SQLITE_FCNTL_PRAGMA || op==SQLITE_FCNTL_TEMPFILENAME)
  482. && rc==SQLITE_OK && *(char**)pArg ){
  483. vfstrace_printf(pInfo, "%s.xFileControl(%s,%s) returns %s",
  484. pInfo->zVfsName, p->zFName, zOp, *(char**)pArg);
  485. }
  486. return rc;
  487. }
  488. /*
  489. ** Return the sector-size in bytes for an vfstrace-file.
  490. */
  491. static int vfstraceSectorSize(sqlite3_file *pFile){
  492. vfstrace_file *p = (vfstrace_file *)pFile;
  493. vfstrace_info *pInfo = p->pInfo;
  494. int rc;
  495. vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
  496. rc = p->pReal->pMethods->xSectorSize(p->pReal);
  497. vfstrace_printf(pInfo, " -> %d\n", rc);
  498. return rc;
  499. }
  500. /*
  501. ** Return the device characteristic flags supported by an vfstrace-file.
  502. */
  503. static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
  504. vfstrace_file *p = (vfstrace_file *)pFile;
  505. vfstrace_info *pInfo = p->pInfo;
  506. int rc;
  507. vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
  508. pInfo->zVfsName, p->zFName);
  509. rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
  510. vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
  511. return rc;
  512. }
  513. /*
  514. ** Shared-memory operations.
  515. */
  516. static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
  517. vfstrace_file *p = (vfstrace_file *)pFile;
  518. vfstrace_info *pInfo = p->pInfo;
  519. int rc;
  520. char zLck[100];
  521. int i = 0;
  522. memcpy(zLck, "|0", 3);
  523. if( flags & SQLITE_SHM_UNLOCK ) strappend(zLck, &i, "|UNLOCK");
  524. if( flags & SQLITE_SHM_LOCK ) strappend(zLck, &i, "|LOCK");
  525. if( flags & SQLITE_SHM_SHARED ) strappend(zLck, &i, "|SHARED");
  526. if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE");
  527. if( flags & ~(0xf) ){
  528. sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags);
  529. }
  530. vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d,n=%d,%s)",
  531. pInfo->zVfsName, p->zFName, ofst, n, &zLck[1]);
  532. rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
  533. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  534. return rc;
  535. }
  536. static int vfstraceShmMap(
  537. sqlite3_file *pFile,
  538. int iRegion,
  539. int szRegion,
  540. int isWrite,
  541. void volatile **pp
  542. ){
  543. vfstrace_file *p = (vfstrace_file *)pFile;
  544. vfstrace_info *pInfo = p->pInfo;
  545. int rc;
  546. vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
  547. pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
  548. rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
  549. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  550. return rc;
  551. }
  552. static void vfstraceShmBarrier(sqlite3_file *pFile){
  553. vfstrace_file *p = (vfstrace_file *)pFile;
  554. vfstrace_info *pInfo = p->pInfo;
  555. vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
  556. p->pReal->pMethods->xShmBarrier(p->pReal);
  557. }
  558. static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
  559. vfstrace_file *p = (vfstrace_file *)pFile;
  560. vfstrace_info *pInfo = p->pInfo;
  561. int rc;
  562. vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
  563. pInfo->zVfsName, p->zFName, delFlag);
  564. rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
  565. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  566. return rc;
  567. }
  568. /*
  569. ** Open an vfstrace file handle.
  570. */
  571. static int vfstraceOpen(
  572. sqlite3_vfs *pVfs,
  573. const char *zName,
  574. sqlite3_file *pFile,
  575. int flags,
  576. int *pOutFlags
  577. ){
  578. int rc;
  579. vfstrace_file *p = (vfstrace_file *)pFile;
  580. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  581. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  582. p->pInfo = pInfo;
  583. p->zFName = zName ? fileTail(zName) : "<temp>";
  584. p->pReal = (sqlite3_file *)&p[1];
  585. rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
  586. vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
  587. pInfo->zVfsName, p->zFName, flags);
  588. if( p->pReal->pMethods ){
  589. sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) );
  590. const sqlite3_io_methods *pSub = p->pReal->pMethods;
  591. memset(pNew, 0, sizeof(*pNew));
  592. pNew->iVersion = pSub->iVersion;
  593. pNew->xClose = vfstraceClose;
  594. pNew->xRead = vfstraceRead;
  595. pNew->xWrite = vfstraceWrite;
  596. pNew->xTruncate = vfstraceTruncate;
  597. pNew->xSync = vfstraceSync;
  598. pNew->xFileSize = vfstraceFileSize;
  599. pNew->xLock = vfstraceLock;
  600. pNew->xUnlock = vfstraceUnlock;
  601. pNew->xCheckReservedLock = vfstraceCheckReservedLock;
  602. pNew->xFileControl = vfstraceFileControl;
  603. pNew->xSectorSize = vfstraceSectorSize;
  604. pNew->xDeviceCharacteristics = vfstraceDeviceCharacteristics;
  605. if( pNew->iVersion>=2 ){
  606. pNew->xShmMap = pSub->xShmMap ? vfstraceShmMap : 0;
  607. pNew->xShmLock = pSub->xShmLock ? vfstraceShmLock : 0;
  608. pNew->xShmBarrier = pSub->xShmBarrier ? vfstraceShmBarrier : 0;
  609. pNew->xShmUnmap = pSub->xShmUnmap ? vfstraceShmUnmap : 0;
  610. }
  611. pFile->pMethods = pNew;
  612. }
  613. vfstrace_print_errcode(pInfo, " -> %s", rc);
  614. if( pOutFlags ){
  615. vfstrace_printf(pInfo, ", outFlags=0x%x\n", *pOutFlags);
  616. }else{
  617. vfstrace_printf(pInfo, "\n");
  618. }
  619. return rc;
  620. }
  621. /*
  622. ** Delete the file located at zPath. If the dirSync argument is true,
  623. ** ensure the file-system modifications are synced to disk before
  624. ** returning.
  625. */
  626. static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
  627. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  628. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  629. int rc;
  630. vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
  631. pInfo->zVfsName, zPath, dirSync);
  632. rc = pRoot->xDelete(pRoot, zPath, dirSync);
  633. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  634. return rc;
  635. }
  636. /*
  637. ** Test for access permissions. Return true if the requested permission
  638. ** is available, or false otherwise.
  639. */
  640. static int vfstraceAccess(
  641. sqlite3_vfs *pVfs,
  642. const char *zPath,
  643. int flags,
  644. int *pResOut
  645. ){
  646. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  647. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  648. int rc;
  649. vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
  650. pInfo->zVfsName, zPath, flags);
  651. rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
  652. vfstrace_print_errcode(pInfo, " -> %s", rc);
  653. vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
  654. return rc;
  655. }
  656. /*
  657. ** Populate buffer zOut with the full canonical pathname corresponding
  658. ** to the pathname in zPath. zOut is guaranteed to point to a buffer
  659. ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
  660. */
  661. static int vfstraceFullPathname(
  662. sqlite3_vfs *pVfs,
  663. const char *zPath,
  664. int nOut,
  665. char *zOut
  666. ){
  667. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  668. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  669. int rc;
  670. vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
  671. pInfo->zVfsName, zPath);
  672. rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
  673. vfstrace_print_errcode(pInfo, " -> %s", rc);
  674. vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
  675. return rc;
  676. }
  677. /*
  678. ** Open the dynamic library located at zPath and return a handle.
  679. */
  680. static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
  681. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  682. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  683. vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
  684. return pRoot->xDlOpen(pRoot, zPath);
  685. }
  686. /*
  687. ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
  688. ** utf-8 string describing the most recent error encountered associated
  689. ** with dynamic libraries.
  690. */
  691. static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
  692. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  693. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  694. vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
  695. pRoot->xDlError(pRoot, nByte, zErrMsg);
  696. vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
  697. }
  698. /*
  699. ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
  700. */
  701. static void (*vfstraceDlSym(sqlite3_vfs *pVfs,void *p,const char *zSym))(void){
  702. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  703. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  704. vfstrace_printf(pInfo, "%s.xDlSym(\"%s\")\n", pInfo->zVfsName, zSym);
  705. return pRoot->xDlSym(pRoot, p, zSym);
  706. }
  707. /*
  708. ** Close the dynamic library handle pHandle.
  709. */
  710. static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
  711. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  712. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  713. vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName);
  714. pRoot->xDlClose(pRoot, pHandle);
  715. }
  716. /*
  717. ** Populate the buffer pointed to by zBufOut with nByte bytes of
  718. ** random data.
  719. */
  720. static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
  721. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  722. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  723. vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
  724. return pRoot->xRandomness(pRoot, nByte, zBufOut);
  725. }
  726. /*
  727. ** Sleep for nMicro microseconds. Return the number of microseconds
  728. ** actually slept.
  729. */
  730. static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
  731. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  732. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  733. return pRoot->xSleep(pRoot, nMicro);
  734. }
  735. /*
  736. ** Return the current time as a Julian Day number in *pTimeOut.
  737. */
  738. static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
  739. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  740. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  741. return pRoot->xCurrentTime(pRoot, pTimeOut);
  742. }
  743. static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
  744. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  745. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  746. return pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
  747. }
  748. /*
  749. ** Return th3 emost recent error code and message
  750. */
  751. static int vfstraceGetLastError(sqlite3_vfs *pVfs, int iErr, char *zErr){
  752. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  753. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  754. return pRoot->xGetLastError(pRoot, iErr, zErr);
  755. }
  756. /*
  757. ** Override system calls.
  758. */
  759. static int vfstraceSetSystemCall(
  760. sqlite3_vfs *pVfs,
  761. const char *zName,
  762. sqlite3_syscall_ptr pFunc
  763. ){
  764. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  765. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  766. return pRoot->xSetSystemCall(pRoot, zName, pFunc);
  767. }
  768. static sqlite3_syscall_ptr vfstraceGetSystemCall(
  769. sqlite3_vfs *pVfs,
  770. const char *zName
  771. ){
  772. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  773. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  774. return pRoot->xGetSystemCall(pRoot, zName);
  775. }
  776. static const char *vfstraceNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
  777. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  778. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  779. return pRoot->xNextSystemCall(pRoot, zName);
  780. }
  781. /*
  782. ** Clients invoke this routine to construct a new trace-vfs shim.
  783. **
  784. ** Return SQLITE_OK on success.
  785. **
  786. ** SQLITE_NOMEM is returned in the case of a memory allocation error.
  787. ** SQLITE_NOTFOUND is returned if zOldVfsName does not exist.
  788. */
  789. int vfstrace_register(
  790. const char *zTraceName, /* Name of the newly constructed VFS */
  791. const char *zOldVfsName, /* Name of the underlying VFS */
  792. int (*xOut)(const char*,void*), /* Output routine. ex: fputs */
  793. void *pOutArg, /* 2nd argument to xOut. ex: stderr */
  794. int makeDefault /* True to make the new VFS the default */
  795. ){
  796. sqlite3_vfs *pNew;
  797. sqlite3_vfs *pRoot;
  798. vfstrace_info *pInfo;
  799. int nName;
  800. int nByte;
  801. pRoot = sqlite3_vfs_find(zOldVfsName);
  802. if( pRoot==0 ) return SQLITE_NOTFOUND;
  803. nName = strlen(zTraceName);
  804. nByte = sizeof(*pNew) + sizeof(*pInfo) + nName + 1;
  805. pNew = sqlite3_malloc( nByte );
  806. if( pNew==0 ) return SQLITE_NOMEM;
  807. memset(pNew, 0, nByte);
  808. pInfo = (vfstrace_info*)&pNew[1];
  809. pNew->iVersion = pRoot->iVersion;
  810. pNew->szOsFile = pRoot->szOsFile + sizeof(vfstrace_file);
  811. pNew->mxPathname = pRoot->mxPathname;
  812. pNew->zName = (char*)&pInfo[1];
  813. memcpy((char*)&pInfo[1], zTraceName, nName+1);
  814. pNew->pAppData = pInfo;
  815. pNew->xOpen = vfstraceOpen;
  816. pNew->xDelete = vfstraceDelete;
  817. pNew->xAccess = vfstraceAccess;
  818. pNew->xFullPathname = vfstraceFullPathname;
  819. pNew->xDlOpen = pRoot->xDlOpen==0 ? 0 : vfstraceDlOpen;
  820. pNew->xDlError = pRoot->xDlError==0 ? 0 : vfstraceDlError;
  821. pNew->xDlSym = pRoot->xDlSym==0 ? 0 : vfstraceDlSym;
  822. pNew->xDlClose = pRoot->xDlClose==0 ? 0 : vfstraceDlClose;
  823. pNew->xRandomness = vfstraceRandomness;
  824. pNew->xSleep = vfstraceSleep;
  825. pNew->xCurrentTime = vfstraceCurrentTime;
  826. pNew->xGetLastError = pRoot->xGetLastError==0 ? 0 : vfstraceGetLastError;
  827. if( pNew->iVersion>=2 ){
  828. pNew->xCurrentTimeInt64 = pRoot->xCurrentTimeInt64==0 ? 0 :
  829. vfstraceCurrentTimeInt64;
  830. if( pNew->iVersion>=3 ){
  831. pNew->xSetSystemCall = pRoot->xSetSystemCall==0 ? 0 :
  832. vfstraceSetSystemCall;
  833. pNew->xGetSystemCall = pRoot->xGetSystemCall==0 ? 0 :
  834. vfstraceGetSystemCall;
  835. pNew->xNextSystemCall = pRoot->xNextSystemCall==0 ? 0 :
  836. vfstraceNextSystemCall;
  837. }
  838. }
  839. pInfo->pRootVfs = pRoot;
  840. pInfo->xOut = xOut;
  841. pInfo->pOutArg = pOutArg;
  842. pInfo->zVfsName = pNew->zName;
  843. pInfo->pTraceVfs = pNew;
  844. vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
  845. pInfo->zVfsName, pRoot->zName);
  846. return sqlite3_vfs_register(pNew, makeDefault);
  847. }