vfstrace.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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. ** RUNTIME CONTROL OF VFSTRACE OUTPUT
  111. **
  112. ** The application can use the "vfstrace" pragma to control which VFS
  113. ** APIs are traced. To disable all output:
  114. **
  115. ** PRAGMA vfstrace('-all');
  116. **
  117. ** To enable all output (which is the default setting):
  118. **
  119. ** PRAGMA vfstrace('+all');
  120. **
  121. ** Individual APIs can be enabled or disabled by name, with or without
  122. ** the initial "x" character. For example, to set up for tracing lock
  123. ** primatives only:
  124. **
  125. ** PRAGMA vfstrace('-all, +Lock,Unlock,ShmLock');
  126. **
  127. ** The argument to the vfstrace pragma ignores capitalization and any
  128. ** characters other than alphabetics, '+', and '-'.
  129. */
  130. #include <stdlib.h>
  131. #include <string.h>
  132. #include "sqlite3.h"
  133. /*
  134. ** An instance of this structure is attached to the each trace VFS to
  135. ** provide auxiliary information.
  136. */
  137. typedef struct vfstrace_info vfstrace_info;
  138. struct vfstrace_info {
  139. sqlite3_vfs *pRootVfs; /* The underlying real VFS */
  140. int (*xOut)(const char*, void*); /* Send output here */
  141. unsigned int mTrace; /* Mask of interfaces to trace */
  142. u8 bOn; /* Tracing on/off */
  143. void *pOutArg; /* First argument to xOut */
  144. const char *zVfsName; /* Name of this trace-VFS */
  145. sqlite3_vfs *pTraceVfs; /* Pointer back to the trace VFS */
  146. };
  147. /*
  148. ** The sqlite3_file object for the trace VFS
  149. */
  150. typedef struct vfstrace_file vfstrace_file;
  151. struct vfstrace_file {
  152. sqlite3_file base; /* Base class. Must be first */
  153. vfstrace_info *pInfo; /* The trace-VFS to which this file belongs */
  154. const char *zFName; /* Base name of the file */
  155. sqlite3_file *pReal; /* The real underlying file */
  156. };
  157. /*
  158. ** Bit values for vfstrace_info.mTrace.
  159. */
  160. #define VTR_CLOSE 0x00000001
  161. #define VTR_READ 0x00000002
  162. #define VTR_WRITE 0x00000004
  163. #define VTR_TRUNC 0x00000008
  164. #define VTR_SYNC 0x00000010
  165. #define VTR_FSIZE 0x00000020
  166. #define VTR_LOCK 0x00000040
  167. #define VTR_UNLOCK 0x00000080
  168. #define VTR_CRL 0x00000100
  169. #define VTR_FCTRL 0x00000200
  170. #define VTR_SECSZ 0x00000400
  171. #define VTR_DEVCHAR 0x00000800
  172. #define VTR_SHMLOCK 0x00001000
  173. #define VTR_SHMMAP 0x00002000
  174. #define VTR_SHMBAR 0x00004000
  175. #define VTR_SHMUNMAP 0x00008000
  176. #define VTR_OPEN 0x00010000
  177. #define VTR_DELETE 0x00020000
  178. #define VTR_ACCESS 0x00040000
  179. #define VTR_FULLPATH 0x00080000
  180. #define VTR_DLOPEN 0x00100000
  181. #define VTR_DLERR 0x00200000
  182. #define VTR_DLSYM 0x00400000
  183. #define VTR_DLCLOSE 0x00800000
  184. #define VTR_RAND 0x01000000
  185. #define VTR_SLEEP 0x02000000
  186. #define VTR_CURTIME 0x04000000
  187. #define VTR_LASTERR 0x08000000
  188. /*
  189. ** Method declarations for vfstrace_file.
  190. */
  191. static int vfstraceClose(sqlite3_file*);
  192. static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
  193. static int vfstraceWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
  194. static int vfstraceTruncate(sqlite3_file*, sqlite3_int64 size);
  195. static int vfstraceSync(sqlite3_file*, int flags);
  196. static int vfstraceFileSize(sqlite3_file*, sqlite3_int64 *pSize);
  197. static int vfstraceLock(sqlite3_file*, int);
  198. static int vfstraceUnlock(sqlite3_file*, int);
  199. static int vfstraceCheckReservedLock(sqlite3_file*, int *);
  200. static int vfstraceFileControl(sqlite3_file*, int op, void *pArg);
  201. static int vfstraceSectorSize(sqlite3_file*);
  202. static int vfstraceDeviceCharacteristics(sqlite3_file*);
  203. static int vfstraceShmLock(sqlite3_file*,int,int,int);
  204. static int vfstraceShmMap(sqlite3_file*,int,int,int, void volatile **);
  205. static void vfstraceShmBarrier(sqlite3_file*);
  206. static int vfstraceShmUnmap(sqlite3_file*,int);
  207. /*
  208. ** Method declarations for vfstrace_vfs.
  209. */
  210. static int vfstraceOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
  211. static int vfstraceDelete(sqlite3_vfs*, const char *zName, int syncDir);
  212. static int vfstraceAccess(sqlite3_vfs*, const char *zName, int flags, int *);
  213. static int vfstraceFullPathname(sqlite3_vfs*, const char *zName, int, char *);
  214. static void *vfstraceDlOpen(sqlite3_vfs*, const char *zFilename);
  215. static void vfstraceDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
  216. static void (*vfstraceDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
  217. static void vfstraceDlClose(sqlite3_vfs*, void*);
  218. static int vfstraceRandomness(sqlite3_vfs*, int nByte, char *zOut);
  219. static int vfstraceSleep(sqlite3_vfs*, int microseconds);
  220. static int vfstraceCurrentTime(sqlite3_vfs*, double*);
  221. static int vfstraceGetLastError(sqlite3_vfs*, int, char*);
  222. static int vfstraceCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
  223. static int vfstraceSetSystemCall(sqlite3_vfs*,const char*, sqlite3_syscall_ptr);
  224. static sqlite3_syscall_ptr vfstraceGetSystemCall(sqlite3_vfs*, const char *);
  225. static const char *vfstraceNextSystemCall(sqlite3_vfs*, const char *zName);
  226. /*
  227. ** Return a pointer to the tail of the pathname. Examples:
  228. **
  229. ** /home/drh/xyzzy.txt -> xyzzy.txt
  230. ** xyzzy.txt -> xyzzy.txt
  231. */
  232. static const char *fileTail(const char *z){
  233. size_t i;
  234. if( z==0 ) return 0;
  235. i = strlen(z)-1;
  236. while( i>0 && z[i-1]!='/' ){ i--; }
  237. return &z[i];
  238. }
  239. /*
  240. ** Send trace output defined by zFormat and subsequent arguments.
  241. */
  242. static void vfstrace_printf(
  243. vfstrace_info *pInfo,
  244. const char *zFormat,
  245. ...
  246. ){
  247. va_list ap;
  248. char *zMsg;
  249. if( pInfo->bOn ){
  250. va_start(ap, zFormat);
  251. zMsg = sqlite3_vmprintf(zFormat, ap);
  252. va_end(ap);
  253. pInfo->xOut(zMsg, pInfo->pOutArg);
  254. sqlite3_free(zMsg);
  255. }
  256. }
  257. /*
  258. ** Try to convert an error code into a symbolic name for that error code.
  259. */
  260. static const char *vfstrace_errcode_name(int rc ){
  261. const char *zVal = 0;
  262. switch( rc ){
  263. case SQLITE_OK: zVal = "SQLITE_OK"; break;
  264. case SQLITE_INTERNAL: zVal = "SQLITE_INTERNAL"; break;
  265. case SQLITE_ERROR: zVal = "SQLITE_ERROR"; break;
  266. case SQLITE_PERM: zVal = "SQLITE_PERM"; break;
  267. case SQLITE_ABORT: zVal = "SQLITE_ABORT"; break;
  268. case SQLITE_BUSY: zVal = "SQLITE_BUSY"; break;
  269. case SQLITE_LOCKED: zVal = "SQLITE_LOCKED"; break;
  270. case SQLITE_NOMEM: zVal = "SQLITE_NOMEM"; break;
  271. case SQLITE_READONLY: zVal = "SQLITE_READONLY"; break;
  272. case SQLITE_INTERRUPT: zVal = "SQLITE_INTERRUPT"; break;
  273. case SQLITE_IOERR: zVal = "SQLITE_IOERR"; break;
  274. case SQLITE_CORRUPT: zVal = "SQLITE_CORRUPT"; break;
  275. case SQLITE_NOTFOUND: zVal = "SQLITE_NOTFOUND"; break;
  276. case SQLITE_FULL: zVal = "SQLITE_FULL"; break;
  277. case SQLITE_CANTOPEN: zVal = "SQLITE_CANTOPEN"; break;
  278. case SQLITE_PROTOCOL: zVal = "SQLITE_PROTOCOL"; break;
  279. case SQLITE_EMPTY: zVal = "SQLITE_EMPTY"; break;
  280. case SQLITE_SCHEMA: zVal = "SQLITE_SCHEMA"; break;
  281. case SQLITE_TOOBIG: zVal = "SQLITE_TOOBIG"; break;
  282. case SQLITE_CONSTRAINT: zVal = "SQLITE_CONSTRAINT"; break;
  283. case SQLITE_MISMATCH: zVal = "SQLITE_MISMATCH"; break;
  284. case SQLITE_MISUSE: zVal = "SQLITE_MISUSE"; break;
  285. case SQLITE_NOLFS: zVal = "SQLITE_NOLFS"; break;
  286. case SQLITE_IOERR_READ: zVal = "SQLITE_IOERR_READ"; break;
  287. case SQLITE_IOERR_SHORT_READ: zVal = "SQLITE_IOERR_SHORT_READ"; break;
  288. case SQLITE_IOERR_WRITE: zVal = "SQLITE_IOERR_WRITE"; break;
  289. case SQLITE_IOERR_FSYNC: zVal = "SQLITE_IOERR_FSYNC"; break;
  290. case SQLITE_IOERR_DIR_FSYNC: zVal = "SQLITE_IOERR_DIR_FSYNC"; break;
  291. case SQLITE_IOERR_TRUNCATE: zVal = "SQLITE_IOERR_TRUNCATE"; break;
  292. case SQLITE_IOERR_FSTAT: zVal = "SQLITE_IOERR_FSTAT"; break;
  293. case SQLITE_IOERR_UNLOCK: zVal = "SQLITE_IOERR_UNLOCK"; break;
  294. case SQLITE_IOERR_RDLOCK: zVal = "SQLITE_IOERR_RDLOCK"; break;
  295. case SQLITE_IOERR_DELETE: zVal = "SQLITE_IOERR_DELETE"; break;
  296. case SQLITE_IOERR_BLOCKED: zVal = "SQLITE_IOERR_BLOCKED"; break;
  297. case SQLITE_IOERR_NOMEM: zVal = "SQLITE_IOERR_NOMEM"; break;
  298. case SQLITE_IOERR_ACCESS: zVal = "SQLITE_IOERR_ACCESS"; break;
  299. case SQLITE_IOERR_CHECKRESERVEDLOCK:
  300. zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
  301. case SQLITE_IOERR_LOCK: zVal = "SQLITE_IOERR_LOCK"; break;
  302. case SQLITE_IOERR_CLOSE: zVal = "SQLITE_IOERR_CLOSE"; break;
  303. case SQLITE_IOERR_DIR_CLOSE: zVal = "SQLITE_IOERR_DIR_CLOSE"; break;
  304. case SQLITE_IOERR_SHMOPEN: zVal = "SQLITE_IOERR_SHMOPEN"; break;
  305. case SQLITE_IOERR_SHMSIZE: zVal = "SQLITE_IOERR_SHMSIZE"; break;
  306. case SQLITE_IOERR_SHMLOCK: zVal = "SQLITE_IOERR_SHMLOCK"; break;
  307. case SQLITE_IOERR_SHMMAP: zVal = "SQLITE_IOERR_SHMMAP"; break;
  308. case SQLITE_IOERR_SEEK: zVal = "SQLITE_IOERR_SEEK"; break;
  309. case SQLITE_IOERR_GETTEMPPATH: zVal = "SQLITE_IOERR_GETTEMPPATH"; break;
  310. case SQLITE_IOERR_CONVPATH: zVal = "SQLITE_IOERR_CONVPATH"; break;
  311. case SQLITE_READONLY_DBMOVED: zVal = "SQLITE_READONLY_DBMOVED"; break;
  312. case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break;
  313. case SQLITE_BUSY_RECOVERY: zVal = "SQLITE_BUSY_RECOVERY"; break;
  314. case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break;
  315. }
  316. return zVal;
  317. }
  318. /*
  319. ** Convert value rc into a string and print it using zFormat. zFormat
  320. ** should have exactly one %s
  321. */
  322. static void vfstrace_print_errcode(
  323. vfstrace_info *pInfo,
  324. const char *zFormat,
  325. int rc
  326. ){
  327. const char *zVal;
  328. char zBuf[50];
  329. zVal = vfstrace_errcode_name(rc);
  330. if( zVal==0 ){
  331. zVal = vfstrace_errcode_name(rc&0xff);
  332. if( zVal ){
  333. sqlite3_snprintf(sizeof(zBuf), zBuf, "%s | 0x%x", zVal, rc&0xffff00);
  334. }else{
  335. sqlite3_snprintf(sizeof(zBuf), zBuf, "%d (0x%x)", rc, rc);
  336. }
  337. zVal = zBuf;
  338. }
  339. vfstrace_printf(pInfo, zFormat, zVal);
  340. }
  341. /*
  342. ** Append to a buffer.
  343. */
  344. static void strappend(char *z, int *pI, const char *zAppend){
  345. int i = *pI;
  346. while( zAppend[0] ){ z[i++] = *(zAppend++); }
  347. z[i] = 0;
  348. *pI = i;
  349. }
  350. /*
  351. ** Turn tracing output on or off according to mMask.
  352. */
  353. static void vfstraceOnOff(vfstrace_info *pInfo, unsigned int mMask){
  354. pInfo->bOn = (pInfo->mTrace & mMask)!=0;
  355. }
  356. /*
  357. ** Close an vfstrace-file.
  358. */
  359. static int vfstraceClose(sqlite3_file *pFile){
  360. vfstrace_file *p = (vfstrace_file *)pFile;
  361. vfstrace_info *pInfo = p->pInfo;
  362. int rc;
  363. vfstraceOnOff(pInfo, VTR_CLOSE);
  364. vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
  365. rc = p->pReal->pMethods->xClose(p->pReal);
  366. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  367. if( rc==SQLITE_OK ){
  368. sqlite3_free((void*)p->base.pMethods);
  369. p->base.pMethods = 0;
  370. }
  371. return rc;
  372. }
  373. /*
  374. ** Read data from an vfstrace-file.
  375. */
  376. static int vfstraceRead(
  377. sqlite3_file *pFile,
  378. void *zBuf,
  379. int iAmt,
  380. sqlite_int64 iOfst
  381. ){
  382. vfstrace_file *p = (vfstrace_file *)pFile;
  383. vfstrace_info *pInfo = p->pInfo;
  384. int rc;
  385. vfstraceOnOff(pInfo, VTR_READ);
  386. vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)",
  387. pInfo->zVfsName, p->zFName, iAmt, iOfst);
  388. rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
  389. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  390. return rc;
  391. }
  392. /*
  393. ** Write data to an vfstrace-file.
  394. */
  395. static int vfstraceWrite(
  396. sqlite3_file *pFile,
  397. const void *zBuf,
  398. int iAmt,
  399. sqlite_int64 iOfst
  400. ){
  401. vfstrace_file *p = (vfstrace_file *)pFile;
  402. vfstrace_info *pInfo = p->pInfo;
  403. int rc;
  404. vfstraceOnOff(pInfo, VTR_WRITE);
  405. vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)",
  406. pInfo->zVfsName, p->zFName, iAmt, iOfst);
  407. rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
  408. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  409. return rc;
  410. }
  411. /*
  412. ** Truncate an vfstrace-file.
  413. */
  414. static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
  415. vfstrace_file *p = (vfstrace_file *)pFile;
  416. vfstrace_info *pInfo = p->pInfo;
  417. int rc;
  418. vfstraceOnOff(pInfo, VTR_TRUNC);
  419. vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
  420. size);
  421. rc = p->pReal->pMethods->xTruncate(p->pReal, size);
  422. vfstrace_printf(pInfo, " -> %d\n", rc);
  423. return rc;
  424. }
  425. /*
  426. ** Sync an vfstrace-file.
  427. */
  428. static int vfstraceSync(sqlite3_file *pFile, int flags){
  429. vfstrace_file *p = (vfstrace_file *)pFile;
  430. vfstrace_info *pInfo = p->pInfo;
  431. int rc;
  432. int i;
  433. char zBuf[100];
  434. memcpy(zBuf, "|0", 3);
  435. i = 0;
  436. if( flags & SQLITE_SYNC_FULL ) strappend(zBuf, &i, "|FULL");
  437. else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL");
  438. if( flags & SQLITE_SYNC_DATAONLY ) strappend(zBuf, &i, "|DATAONLY");
  439. if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){
  440. sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags);
  441. }
  442. vfstraceOnOff(pInfo, VTR_SYNC);
  443. vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName,
  444. &zBuf[1]);
  445. rc = p->pReal->pMethods->xSync(p->pReal, flags);
  446. vfstrace_printf(pInfo, " -> %d\n", rc);
  447. return rc;
  448. }
  449. /*
  450. ** Return the current file-size of an vfstrace-file.
  451. */
  452. static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
  453. vfstrace_file *p = (vfstrace_file *)pFile;
  454. vfstrace_info *pInfo = p->pInfo;
  455. int rc;
  456. vfstraceOnOff(pInfo, VTR_FSIZE);
  457. vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
  458. rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
  459. vfstrace_print_errcode(pInfo, " -> %s,", rc);
  460. vfstrace_printf(pInfo, " size=%lld\n", *pSize);
  461. return rc;
  462. }
  463. /*
  464. ** Return the name of a lock.
  465. */
  466. static const char *lockName(int eLock){
  467. const char *azLockNames[] = {
  468. "NONE", "SHARED", "RESERVED", "PENDING", "EXCLUSIVE"
  469. };
  470. if( eLock<0 || eLock>=(int)(sizeof(azLockNames)/sizeof(azLockNames[0])) ){
  471. return "???";
  472. }else{
  473. return azLockNames[eLock];
  474. }
  475. }
  476. /*
  477. ** Lock an vfstrace-file.
  478. */
  479. static int vfstraceLock(sqlite3_file *pFile, int eLock){
  480. vfstrace_file *p = (vfstrace_file *)pFile;
  481. vfstrace_info *pInfo = p->pInfo;
  482. int rc;
  483. vfstraceOnOff(pInfo, VTR_LOCK);
  484. vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName,
  485. lockName(eLock));
  486. rc = p->pReal->pMethods->xLock(p->pReal, eLock);
  487. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  488. return rc;
  489. }
  490. /*
  491. ** Unlock an vfstrace-file.
  492. */
  493. static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
  494. vfstrace_file *p = (vfstrace_file *)pFile;
  495. vfstrace_info *pInfo = p->pInfo;
  496. int rc;
  497. vfstraceOnOff(pInfo, VTR_UNLOCK);
  498. vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName,
  499. lockName(eLock));
  500. rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
  501. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  502. return rc;
  503. }
  504. /*
  505. ** Check if another file-handle holds a RESERVED lock on an vfstrace-file.
  506. */
  507. static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
  508. vfstrace_file *p = (vfstrace_file *)pFile;
  509. vfstrace_info *pInfo = p->pInfo;
  510. int rc;
  511. vfstraceOnOff(pInfo, VTR_CRL);
  512. vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)",
  513. pInfo->zVfsName, p->zFName);
  514. rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
  515. vfstrace_print_errcode(pInfo, " -> %s", rc);
  516. vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
  517. return rc;
  518. }
  519. /*
  520. ** File control method. For custom operations on an vfstrace-file.
  521. */
  522. static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){
  523. vfstrace_file *p = (vfstrace_file *)pFile;
  524. vfstrace_info *pInfo = p->pInfo;
  525. int rc;
  526. char zBuf[100];
  527. char zBuf2[100];
  528. char *zOp;
  529. char *zRVal = 0;
  530. vfstraceOnOff(pInfo, VTR_FCTRL);
  531. switch( op ){
  532. case SQLITE_FCNTL_LOCKSTATE: zOp = "LOCKSTATE"; break;
  533. case SQLITE_GET_LOCKPROXYFILE: zOp = "GET_LOCKPROXYFILE"; break;
  534. case SQLITE_SET_LOCKPROXYFILE: zOp = "SET_LOCKPROXYFILE"; break;
  535. case SQLITE_LAST_ERRNO: zOp = "LAST_ERRNO"; break;
  536. case SQLITE_FCNTL_SIZE_HINT: {
  537. sqlite3_snprintf(sizeof(zBuf), zBuf, "SIZE_HINT,%lld",
  538. *(sqlite3_int64*)pArg);
  539. zOp = zBuf;
  540. break;
  541. }
  542. case SQLITE_FCNTL_CHUNK_SIZE: {
  543. sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg);
  544. zOp = zBuf;
  545. break;
  546. }
  547. case SQLITE_FCNTL_FILE_POINTER: zOp = "FILE_POINTER"; break;
  548. case SQLITE_FCNTL_WIN32_AV_RETRY: zOp = "WIN32_AV_RETRY"; break;
  549. case SQLITE_FCNTL_PERSIST_WAL: {
  550. sqlite3_snprintf(sizeof(zBuf), zBuf, "PERSIST_WAL,%d", *(int*)pArg);
  551. zOp = zBuf;
  552. break;
  553. }
  554. case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break;
  555. case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break;
  556. case SQLITE_FCNTL_POWERSAFE_OVERWRITE: zOp = "POWERSAFE_OVERWRITE"; break;
  557. case SQLITE_FCNTL_PRAGMA: {
  558. const char *const* a = (const char*const*)pArg;
  559. if( a[1] && strcmp(a[1],"vfstrace")==0 && a[2] ){
  560. const u8 *zArg = (const u8*)a[2];
  561. if( zArg[0]>='0' && zArg[0]<=9 ){
  562. pInfo->mTrace = (sqlite3_uint64)strtoll(a[2], 0, 0);
  563. }else{
  564. static const struct {
  565. const char *z;
  566. unsigned int m;
  567. } aKw[] = {
  568. { "all", 0xffffffff },
  569. { "close", VTR_CLOSE },
  570. { "read", VTR_READ },
  571. { "write", VTR_WRITE },
  572. { "truncate", VTR_TRUNC },
  573. { "sync", VTR_SYNC },
  574. { "filesize", VTR_FSIZE },
  575. { "lock", VTR_LOCK },
  576. { "unlock", VTR_UNLOCK },
  577. { "checkreservedlock", VTR_CRL },
  578. { "filecontrol", VTR_FCTRL },
  579. { "sectorsize", VTR_SECSZ },
  580. { "devicecharacteristics", VTR_DEVCHAR },
  581. { "shmlock", VTR_SHMLOCK },
  582. { "shmmap", VTR_SHMMAP },
  583. { "shmummap", VTR_SHMUNMAP },
  584. { "shmbarrier", VTR_SHMBAR },
  585. { "open", VTR_OPEN },
  586. { "delete", VTR_DELETE },
  587. { "access", VTR_ACCESS },
  588. { "fullpathname", VTR_FULLPATH },
  589. { "dlopen", VTR_DLOPEN },
  590. { "dlerror", VTR_DLERR },
  591. { "dlsym", VTR_DLSYM },
  592. { "dlclose", VTR_DLCLOSE },
  593. { "randomness", VTR_RAND },
  594. { "sleep", VTR_SLEEP },
  595. { "currenttime", VTR_CURTIME },
  596. { "currenttimeint64", VTR_CURTIME },
  597. { "getlasterror", VTR_LASTERR },
  598. };
  599. int onOff = 1;
  600. while( zArg[0] ){
  601. int jj, n;
  602. while( zArg[0]!=0 && zArg[0]!='-' && zArg[0]!='+'
  603. && !isalpha(zArg[0]) ) zArg++;
  604. if( zArg[0]==0 ) break;
  605. if( zArg[0]=='-' ){
  606. onOff = 0;
  607. zArg++;
  608. }else if( zArg[0]=='+' ){
  609. onOff = 1;
  610. zArg++;
  611. }
  612. while( !isalpha(zArg[0]) ){
  613. if( zArg[0]==0 ) break;
  614. zArg++;
  615. }
  616. if( zArg[0]=='x' && isalpha(zArg[1]) ) zArg++;
  617. for(n=0; isalpha(zArg[n]); n++){}
  618. for(jj=0; jj<(int)(sizeof(aKw)/sizeof(aKw[0])); jj++){
  619. if( sqlite3_strnicmp(aKw[jj].z,(const char*)zArg,n)==0 ){
  620. if( onOff ){
  621. pInfo->mTrace |= aKw[jj].m;
  622. }else{
  623. pInfo->mTrace &= ~aKw[jj].m;
  624. }
  625. break;
  626. }
  627. }
  628. zArg += n;
  629. }
  630. }
  631. }
  632. sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]);
  633. zOp = zBuf;
  634. break;
  635. }
  636. case SQLITE_FCNTL_BUSYHANDLER: zOp = "BUSYHANDLER"; break;
  637. case SQLITE_FCNTL_TEMPFILENAME: zOp = "TEMPFILENAME"; break;
  638. case SQLITE_FCNTL_MMAP_SIZE: {
  639. sqlite3_int64 iMMap = *(sqlite3_int64*)pArg;
  640. sqlite3_snprintf(sizeof(zBuf), zBuf, "MMAP_SIZE,%lld",iMMap);
  641. zOp = zBuf;
  642. break;
  643. }
  644. case SQLITE_FCNTL_TRACE: zOp = "TRACE"; break;
  645. case SQLITE_FCNTL_HAS_MOVED: zOp = "HAS_MOVED"; break;
  646. case SQLITE_FCNTL_SYNC: zOp = "SYNC"; break;
  647. case SQLITE_FCNTL_COMMIT_PHASETWO: zOp = "COMMIT_PHASETWO"; break;
  648. case SQLITE_FCNTL_WIN32_SET_HANDLE: zOp = "WIN32_SET_HANDLE"; break;
  649. case SQLITE_FCNTL_WAL_BLOCK: zOp = "WAL_BLOCK"; break;
  650. case SQLITE_FCNTL_ZIPVFS: zOp = "ZIPVFS"; break;
  651. case SQLITE_FCNTL_RBU: zOp = "RBU"; break;
  652. case SQLITE_FCNTL_VFS_POINTER: zOp = "VFS_POINTER"; break;
  653. case SQLITE_FCNTL_JOURNAL_POINTER: zOp = "JOURNAL_POINTER"; break;
  654. case SQLITE_FCNTL_WIN32_GET_HANDLE: zOp = "WIN32_GET_HANDLE"; break;
  655. case SQLITE_FCNTL_PDB: zOp = "PDB"; break;
  656. case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: zOp = "BEGIN_ATOMIC_WRITE"; break;
  657. case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: zOp = "COMMIT_ATOMIC_WRITE"; break;
  658. case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
  659. zOp = "ROLLBACK_ATOMIC_WRITE";
  660. break;
  661. }
  662. case SQLITE_FCNTL_LOCK_TIMEOUT: {
  663. sqlite3_snprintf(sizeof(zBuf), zBuf, "LOCK_TIMEOUT,%d", *(int*)pArg);
  664. zOp = zBuf;
  665. break;
  666. }
  667. case SQLITE_FCNTL_DATA_VERSION: zOp = "DATA_VERSION"; break;
  668. case SQLITE_FCNTL_SIZE_LIMIT: zOp = "SIZE_LIMIT"; break;
  669. case SQLITE_FCNTL_CKPT_DONE: zOp = "CKPT_DONE"; break;
  670. case SQLITE_FCNTL_RESERVE_BYTES: zOp = "RESERVED_BYTES"; break;
  671. case SQLITE_FCNTL_CKPT_START: zOp = "CKPT_START"; break;
  672. case SQLITE_FCNTL_EXTERNAL_READER: zOp = "EXTERNAL_READER"; break;
  673. case SQLITE_FCNTL_CKSM_FILE: zOp = "CKSM_FILE"; break;
  674. case SQLITE_FCNTL_RESET_CACHE: zOp = "RESET_CACHE"; break;
  675. case 0xca093fa0: zOp = "DB_UNCHANGED"; break;
  676. default: {
  677. sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op);
  678. zOp = zBuf;
  679. break;
  680. }
  681. }
  682. vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)",
  683. pInfo->zVfsName, p->zFName, zOp);
  684. rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
  685. if( rc==SQLITE_OK ){
  686. switch( op ){
  687. case SQLITE_FCNTL_VFSNAME: {
  688. *(char**)pArg = sqlite3_mprintf("vfstrace.%s/%z",
  689. pInfo->zVfsName, *(char**)pArg);
  690. zRVal = *(char**)pArg;
  691. break;
  692. }
  693. case SQLITE_FCNTL_MMAP_SIZE: {
  694. sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%lld", *(sqlite3_int64*)pArg);
  695. zRVal = zBuf2;
  696. break;
  697. }
  698. case SQLITE_FCNTL_HAS_MOVED:
  699. case SQLITE_FCNTL_PERSIST_WAL: {
  700. sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%d", *(int*)pArg);
  701. zRVal = zBuf2;
  702. break;
  703. }
  704. case SQLITE_FCNTL_PRAGMA:
  705. case SQLITE_FCNTL_TEMPFILENAME: {
  706. zRVal = *(char**)pArg;
  707. break;
  708. }
  709. }
  710. }
  711. if( zRVal ){
  712. vfstrace_print_errcode(pInfo, " -> %s", rc);
  713. vfstrace_printf(pInfo, ", %s\n", zRVal);
  714. }else{
  715. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  716. }
  717. return rc;
  718. }
  719. /*
  720. ** Return the sector-size in bytes for an vfstrace-file.
  721. */
  722. static int vfstraceSectorSize(sqlite3_file *pFile){
  723. vfstrace_file *p = (vfstrace_file *)pFile;
  724. vfstrace_info *pInfo = p->pInfo;
  725. int rc;
  726. vfstraceOnOff(pInfo, VTR_SECSZ);
  727. vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
  728. rc = p->pReal->pMethods->xSectorSize(p->pReal);
  729. vfstrace_printf(pInfo, " -> %d\n", rc);
  730. return rc;
  731. }
  732. /*
  733. ** Return the device characteristic flags supported by an vfstrace-file.
  734. */
  735. static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
  736. vfstrace_file *p = (vfstrace_file *)pFile;
  737. vfstrace_info *pInfo = p->pInfo;
  738. int rc;
  739. vfstraceOnOff(pInfo, VTR_DEVCHAR);
  740. vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
  741. pInfo->zVfsName, p->zFName);
  742. rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
  743. vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
  744. return rc;
  745. }
  746. /*
  747. ** Shared-memory operations.
  748. */
  749. static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
  750. static const char *azLockName[] = {
  751. "WRITE",
  752. "CKPT",
  753. "RECOVER",
  754. "READ0",
  755. "READ1",
  756. "READ2",
  757. "READ3",
  758. "READ4",
  759. };
  760. vfstrace_file *p = (vfstrace_file *)pFile;
  761. vfstrace_info *pInfo = p->pInfo;
  762. int rc;
  763. char zLck[100];
  764. int i = 0;
  765. vfstraceOnOff(pInfo, VTR_SHMLOCK);
  766. memcpy(zLck, "|0", 3);
  767. if( flags & SQLITE_SHM_UNLOCK ) strappend(zLck, &i, "|UNLOCK");
  768. if( flags & SQLITE_SHM_LOCK ) strappend(zLck, &i, "|LOCK");
  769. if( flags & SQLITE_SHM_SHARED ) strappend(zLck, &i, "|SHARED");
  770. if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE");
  771. if( flags & ~(0xf) ){
  772. sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags);
  773. }
  774. if( ofst>=0 && ofst<(int)(sizeof(azLockName)/sizeof(azLockName[0])) ){
  775. vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d(%s),n=%d,%s)",
  776. pInfo->zVfsName, p->zFName, ofst, azLockName[ofst],
  777. n, &zLck[1]);
  778. }else{
  779. vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=5d,n=%d,%s)",
  780. pInfo->zVfsName, p->zFName, ofst,
  781. n, &zLck[1]);
  782. }
  783. rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
  784. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  785. return rc;
  786. }
  787. static int vfstraceShmMap(
  788. sqlite3_file *pFile,
  789. int iRegion,
  790. int szRegion,
  791. int isWrite,
  792. void volatile **pp
  793. ){
  794. vfstrace_file *p = (vfstrace_file *)pFile;
  795. vfstrace_info *pInfo = p->pInfo;
  796. int rc;
  797. vfstraceOnOff(pInfo, VTR_SHMMAP);
  798. vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
  799. pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
  800. rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
  801. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  802. return rc;
  803. }
  804. static void vfstraceShmBarrier(sqlite3_file *pFile){
  805. vfstrace_file *p = (vfstrace_file *)pFile;
  806. vfstrace_info *pInfo = p->pInfo;
  807. vfstraceOnOff(pInfo, VTR_SHMBAR);
  808. vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
  809. p->pReal->pMethods->xShmBarrier(p->pReal);
  810. }
  811. static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
  812. vfstrace_file *p = (vfstrace_file *)pFile;
  813. vfstrace_info *pInfo = p->pInfo;
  814. int rc;
  815. vfstraceOnOff(pInfo, VTR_SHMUNMAP);
  816. vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
  817. pInfo->zVfsName, p->zFName, delFlag);
  818. rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
  819. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  820. return rc;
  821. }
  822. /*
  823. ** Open an vfstrace file handle.
  824. */
  825. static int vfstraceOpen(
  826. sqlite3_vfs *pVfs,
  827. const char *zName,
  828. sqlite3_file *pFile,
  829. int flags,
  830. int *pOutFlags
  831. ){
  832. int rc;
  833. vfstrace_file *p = (vfstrace_file *)pFile;
  834. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  835. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  836. p->pInfo = pInfo;
  837. p->zFName = zName ? fileTail(zName) : "<temp>";
  838. p->pReal = (sqlite3_file *)&p[1];
  839. rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
  840. vfstraceOnOff(pInfo, VTR_OPEN);
  841. vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
  842. pInfo->zVfsName, p->zFName, flags);
  843. if( p->pReal->pMethods ){
  844. sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) );
  845. const sqlite3_io_methods *pSub = p->pReal->pMethods;
  846. memset(pNew, 0, sizeof(*pNew));
  847. pNew->iVersion = pSub->iVersion;
  848. pNew->xClose = vfstraceClose;
  849. pNew->xRead = vfstraceRead;
  850. pNew->xWrite = vfstraceWrite;
  851. pNew->xTruncate = vfstraceTruncate;
  852. pNew->xSync = vfstraceSync;
  853. pNew->xFileSize = vfstraceFileSize;
  854. pNew->xLock = vfstraceLock;
  855. pNew->xUnlock = vfstraceUnlock;
  856. pNew->xCheckReservedLock = vfstraceCheckReservedLock;
  857. pNew->xFileControl = vfstraceFileControl;
  858. pNew->xSectorSize = vfstraceSectorSize;
  859. pNew->xDeviceCharacteristics = vfstraceDeviceCharacteristics;
  860. if( pNew->iVersion>=2 ){
  861. pNew->xShmMap = pSub->xShmMap ? vfstraceShmMap : 0;
  862. pNew->xShmLock = pSub->xShmLock ? vfstraceShmLock : 0;
  863. pNew->xShmBarrier = pSub->xShmBarrier ? vfstraceShmBarrier : 0;
  864. pNew->xShmUnmap = pSub->xShmUnmap ? vfstraceShmUnmap : 0;
  865. }
  866. pFile->pMethods = pNew;
  867. }
  868. vfstrace_print_errcode(pInfo, " -> %s", rc);
  869. if( pOutFlags ){
  870. vfstrace_printf(pInfo, ", outFlags=0x%x\n", *pOutFlags);
  871. }else{
  872. vfstrace_printf(pInfo, "\n");
  873. }
  874. return rc;
  875. }
  876. /*
  877. ** Delete the file located at zPath. If the dirSync argument is true,
  878. ** ensure the file-system modifications are synced to disk before
  879. ** returning.
  880. */
  881. static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
  882. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  883. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  884. int rc;
  885. vfstraceOnOff(pInfo, VTR_DELETE);
  886. vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
  887. pInfo->zVfsName, zPath, dirSync);
  888. rc = pRoot->xDelete(pRoot, zPath, dirSync);
  889. vfstrace_print_errcode(pInfo, " -> %s\n", rc);
  890. return rc;
  891. }
  892. /*
  893. ** Test for access permissions. Return true if the requested permission
  894. ** is available, or false otherwise.
  895. */
  896. static int vfstraceAccess(
  897. sqlite3_vfs *pVfs,
  898. const char *zPath,
  899. int flags,
  900. int *pResOut
  901. ){
  902. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  903. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  904. int rc;
  905. vfstraceOnOff(pInfo, VTR_ACCESS);
  906. vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)",
  907. pInfo->zVfsName, zPath, flags);
  908. rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
  909. vfstrace_print_errcode(pInfo, " -> %s", rc);
  910. vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
  911. return rc;
  912. }
  913. /*
  914. ** Populate buffer zOut with the full canonical pathname corresponding
  915. ** to the pathname in zPath. zOut is guaranteed to point to a buffer
  916. ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
  917. */
  918. static int vfstraceFullPathname(
  919. sqlite3_vfs *pVfs,
  920. const char *zPath,
  921. int nOut,
  922. char *zOut
  923. ){
  924. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  925. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  926. int rc;
  927. vfstraceOnOff(pInfo, VTR_FULLPATH);
  928. vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
  929. pInfo->zVfsName, zPath);
  930. rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
  931. vfstrace_print_errcode(pInfo, " -> %s", rc);
  932. vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
  933. return rc;
  934. }
  935. /*
  936. ** Open the dynamic library located at zPath and return a handle.
  937. */
  938. static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
  939. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  940. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  941. vfstraceOnOff(pInfo, VTR_DLOPEN);
  942. vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
  943. return pRoot->xDlOpen(pRoot, zPath);
  944. }
  945. /*
  946. ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
  947. ** utf-8 string describing the most recent error encountered associated
  948. ** with dynamic libraries.
  949. */
  950. static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
  951. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  952. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  953. vfstraceOnOff(pInfo, VTR_DLERR);
  954. vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
  955. pRoot->xDlError(pRoot, nByte, zErrMsg);
  956. vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
  957. }
  958. /*
  959. ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
  960. */
  961. static void (*vfstraceDlSym(sqlite3_vfs *pVfs,void *p,const char *zSym))(void){
  962. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  963. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  964. vfstrace_printf(pInfo, "%s.xDlSym(\"%s\")\n", pInfo->zVfsName, zSym);
  965. return pRoot->xDlSym(pRoot, p, zSym);
  966. }
  967. /*
  968. ** Close the dynamic library handle pHandle.
  969. */
  970. static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
  971. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  972. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  973. vfstraceOnOff(pInfo, VTR_DLCLOSE);
  974. vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName);
  975. pRoot->xDlClose(pRoot, pHandle);
  976. }
  977. /*
  978. ** Populate the buffer pointed to by zBufOut with nByte bytes of
  979. ** random data.
  980. */
  981. static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
  982. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  983. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  984. vfstraceOnOff(pInfo, VTR_RAND);
  985. vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
  986. return pRoot->xRandomness(pRoot, nByte, zBufOut);
  987. }
  988. /*
  989. ** Sleep for nMicro microseconds. Return the number of microseconds
  990. ** actually slept.
  991. */
  992. static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
  993. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  994. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  995. vfstraceOnOff(pInfo, VTR_SLEEP);
  996. vfstrace_printf(pInfo, "%s.xSleep(%d)\n", pInfo->zVfsName, nMicro);
  997. return pRoot->xSleep(pRoot, nMicro);
  998. }
  999. /*
  1000. ** Return the current time as a Julian Day number in *pTimeOut.
  1001. */
  1002. static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
  1003. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  1004. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  1005. int rc;
  1006. vfstraceOnOff(pInfo, VTR_CURTIME);
  1007. vfstrace_printf(pInfo, "%s.xCurrentTime()", pInfo->zVfsName);
  1008. rc = pRoot->xCurrentTime(pRoot, pTimeOut);
  1009. vfstrace_printf(pInfo, " -> %.17g\n", *pTimeOut);
  1010. return rc;
  1011. }
  1012. static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
  1013. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  1014. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  1015. int rc;
  1016. vfstraceOnOff(pInfo, VTR_CURTIME);
  1017. vfstrace_printf(pInfo, "%s.xCurrentTimeInt64()", pInfo->zVfsName);
  1018. rc = pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
  1019. vfstrace_printf(pInfo, " -> %lld\n", *pTimeOut);
  1020. return rc;
  1021. }
  1022. /*
  1023. ** Return the most recent error code and message
  1024. */
  1025. static int vfstraceGetLastError(sqlite3_vfs *pVfs, int nErr, char *zErr){
  1026. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  1027. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  1028. int rc;
  1029. vfstraceOnOff(pInfo, VTR_LASTERR);
  1030. vfstrace_printf(pInfo, "%s.xGetLastError(%d,zBuf)", pInfo->zVfsName, nErr);
  1031. if( nErr ) zErr[0] = 0;
  1032. rc = pRoot->xGetLastError(pRoot, nErr, zErr);
  1033. vfstrace_printf(pInfo, " -> zBuf[] = \"%s\", rc = %d\n", nErr?zErr:"", rc);
  1034. return rc;
  1035. }
  1036. /*
  1037. ** Override system calls.
  1038. */
  1039. static int vfstraceSetSystemCall(
  1040. sqlite3_vfs *pVfs,
  1041. const char *zName,
  1042. sqlite3_syscall_ptr pFunc
  1043. ){
  1044. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  1045. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  1046. return pRoot->xSetSystemCall(pRoot, zName, pFunc);
  1047. }
  1048. static sqlite3_syscall_ptr vfstraceGetSystemCall(
  1049. sqlite3_vfs *pVfs,
  1050. const char *zName
  1051. ){
  1052. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  1053. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  1054. return pRoot->xGetSystemCall(pRoot, zName);
  1055. }
  1056. static const char *vfstraceNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
  1057. vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
  1058. sqlite3_vfs *pRoot = pInfo->pRootVfs;
  1059. return pRoot->xNextSystemCall(pRoot, zName);
  1060. }
  1061. /*
  1062. ** Clients invoke this routine to construct a new trace-vfs shim.
  1063. **
  1064. ** Return SQLITE_OK on success.
  1065. **
  1066. ** SQLITE_NOMEM is returned in the case of a memory allocation error.
  1067. ** SQLITE_NOTFOUND is returned if zOldVfsName does not exist.
  1068. */
  1069. int vfstrace_register(
  1070. const char *zTraceName, /* Name of the newly constructed VFS */
  1071. const char *zOldVfsName, /* Name of the underlying VFS */
  1072. int (*xOut)(const char*,void*), /* Output routine. ex: fputs */
  1073. void *pOutArg, /* 2nd argument to xOut. ex: stderr */
  1074. int makeDefault /* True to make the new VFS the default */
  1075. ){
  1076. sqlite3_vfs *pNew;
  1077. sqlite3_vfs *pRoot;
  1078. vfstrace_info *pInfo;
  1079. size_t nName;
  1080. size_t nByte;
  1081. pRoot = sqlite3_vfs_find(zOldVfsName);
  1082. if( pRoot==0 ) return SQLITE_NOTFOUND;
  1083. nName = strlen(zTraceName);
  1084. nByte = sizeof(*pNew) + sizeof(*pInfo) + nName + 1;
  1085. pNew = sqlite3_malloc64( nByte );
  1086. if( pNew==0 ) return SQLITE_NOMEM;
  1087. memset(pNew, 0, nByte);
  1088. pInfo = (vfstrace_info*)&pNew[1];
  1089. pNew->iVersion = pRoot->iVersion;
  1090. pNew->szOsFile = pRoot->szOsFile + sizeof(vfstrace_file);
  1091. pNew->mxPathname = pRoot->mxPathname;
  1092. pNew->zName = (char*)&pInfo[1];
  1093. memcpy((char*)&pInfo[1], zTraceName, nName+1);
  1094. pNew->pAppData = pInfo;
  1095. pNew->xOpen = vfstraceOpen;
  1096. pNew->xDelete = vfstraceDelete;
  1097. pNew->xAccess = vfstraceAccess;
  1098. pNew->xFullPathname = vfstraceFullPathname;
  1099. pNew->xDlOpen = pRoot->xDlOpen==0 ? 0 : vfstraceDlOpen;
  1100. pNew->xDlError = pRoot->xDlError==0 ? 0 : vfstraceDlError;
  1101. pNew->xDlSym = pRoot->xDlSym==0 ? 0 : vfstraceDlSym;
  1102. pNew->xDlClose = pRoot->xDlClose==0 ? 0 : vfstraceDlClose;
  1103. pNew->xRandomness = vfstraceRandomness;
  1104. pNew->xSleep = vfstraceSleep;
  1105. pNew->xCurrentTime = vfstraceCurrentTime;
  1106. pNew->xGetLastError = pRoot->xGetLastError==0 ? 0 : vfstraceGetLastError;
  1107. if( pNew->iVersion>=2 ){
  1108. pNew->xCurrentTimeInt64 = pRoot->xCurrentTimeInt64==0 ? 0 :
  1109. vfstraceCurrentTimeInt64;
  1110. if( pNew->iVersion>=3 ){
  1111. pNew->xSetSystemCall = pRoot->xSetSystemCall==0 ? 0 :
  1112. vfstraceSetSystemCall;
  1113. pNew->xGetSystemCall = pRoot->xGetSystemCall==0 ? 0 :
  1114. vfstraceGetSystemCall;
  1115. pNew->xNextSystemCall = pRoot->xNextSystemCall==0 ? 0 :
  1116. vfstraceNextSystemCall;
  1117. }
  1118. }
  1119. pInfo->pRootVfs = pRoot;
  1120. pInfo->xOut = xOut;
  1121. pInfo->pOutArg = pOutArg;
  1122. pInfo->zVfsName = pNew->zName;
  1123. pInfo->pTraceVfs = pNew;
  1124. pInfo->mTrace = 0xffffffff;
  1125. pInfo->bOn = 1;
  1126. vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
  1127. pInfo->zVfsName, pRoot->zName);
  1128. return sqlite3_vfs_register(pNew, makeDefault);
  1129. }
  1130. /*
  1131. ** Look for the named VFS. If it is a TRACEVFS, then unregister it
  1132. ** and delete it.
  1133. */
  1134. void vfstrace_unregister(const char *zTraceName){
  1135. sqlite3_vfs *pVfs = sqlite3_vfs_find(zTraceName);
  1136. if( pVfs==0 ) return;
  1137. if( pVfs->xOpen!=vfstraceOpen ) return;
  1138. sqlite3_vfs_unregister(pVfs);
  1139. sqlite3_free(pVfs);
  1140. }