Debian Codegraph @

ext/misc/vfstrace.c

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