OLD | NEW |
1 /* | 1 /* |
2 ** 2011 March 16 | 2 ** 2011 March 16 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ****************************************************************************** | 11 ****************************************************************************** |
12 ** | 12 ** |
13 ** This file contains code implements a VFS shim that writes diagnostic | 13 ** This file contains code implements a VFS shim that writes diagnostic |
14 ** output for each VFS call, similar to "strace". | 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. |
15 */ | 109 */ |
16 #include <stdlib.h> | 110 #include <stdlib.h> |
17 #include <string.h> | 111 #include <string.h> |
18 #include "sqlite3.h" | 112 #include "sqlite3.h" |
19 | 113 |
20 /* | 114 /* |
21 ** An instance of this structure is attached to the each trace VFS to | 115 ** An instance of this structure is attached to the each trace VFS to |
22 ** provide auxiliary information. | 116 ** provide auxiliary information. |
23 */ | 117 */ |
24 typedef struct vfstrace_info vfstrace_info; | 118 typedef struct vfstrace_info vfstrace_info; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 case SQLITE_IOERR_NOMEM: zVal = "SQLITE_IOERR_NOMEM"; break; | 251 case SQLITE_IOERR_NOMEM: zVal = "SQLITE_IOERR_NOMEM"; break; |
158 case SQLITE_IOERR_ACCESS: zVal = "SQLITE_IOERR_ACCESS"; break; | 252 case SQLITE_IOERR_ACCESS: zVal = "SQLITE_IOERR_ACCESS"; break; |
159 case SQLITE_IOERR_CHECKRESERVEDLOCK: | 253 case SQLITE_IOERR_CHECKRESERVEDLOCK: |
160 zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break; | 254 zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break; |
161 case SQLITE_IOERR_LOCK: zVal = "SQLITE_IOERR_LOCK"; break; | 255 case SQLITE_IOERR_LOCK: zVal = "SQLITE_IOERR_LOCK"; break; |
162 case SQLITE_IOERR_CLOSE: zVal = "SQLITE_IOERR_CLOSE"; break; | 256 case SQLITE_IOERR_CLOSE: zVal = "SQLITE_IOERR_CLOSE"; break; |
163 case SQLITE_IOERR_DIR_CLOSE: zVal = "SQLITE_IOERR_DIR_CLOSE"; break; | 257 case SQLITE_IOERR_DIR_CLOSE: zVal = "SQLITE_IOERR_DIR_CLOSE"; break; |
164 case SQLITE_IOERR_SHMOPEN: zVal = "SQLITE_IOERR_SHMOPEN"; break; | 258 case SQLITE_IOERR_SHMOPEN: zVal = "SQLITE_IOERR_SHMOPEN"; break; |
165 case SQLITE_IOERR_SHMSIZE: zVal = "SQLITE_IOERR_SHMSIZE"; break; | 259 case SQLITE_IOERR_SHMSIZE: zVal = "SQLITE_IOERR_SHMSIZE"; break; |
166 case SQLITE_IOERR_SHMLOCK: zVal = "SQLITE_IOERR_SHMLOCK"; break; | 260 case SQLITE_IOERR_SHMLOCK: zVal = "SQLITE_IOERR_SHMLOCK"; break; |
| 261 case SQLITE_IOERR_SHMMAP: zVal = "SQLITE_IOERR_SHMMAP"; break; |
| 262 case SQLITE_IOERR_SEEK: zVal = "SQLITE_IOERR_SEEK"; break; |
| 263 case SQLITE_IOERR_GETTEMPPATH: zVal = "SQLITE_IOERR_GETTEMPPATH"; break; |
| 264 case SQLITE_IOERR_CONVPATH: zVal = "SQLITE_IOERR_CONVPATH"; break; |
| 265 case SQLITE_READONLY_DBMOVED: zVal = "SQLITE_READONLY_DBMOVED"; break; |
167 case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break; | 266 case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break; |
168 case SQLITE_BUSY_RECOVERY: zVal = "SQLITE_BUSY_RECOVERY"; break; | 267 case SQLITE_BUSY_RECOVERY: zVal = "SQLITE_BUSY_RECOVERY"; break; |
169 case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break; | 268 case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break; |
170 default: { | 269 default: { |
171 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc); | 270 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc); |
172 zVal = zBuf; | 271 zVal = zBuf; |
173 break; | 272 break; |
174 } | 273 } |
175 } | 274 } |
176 vfstrace_printf(pInfo, zFormat, zVal); | 275 vfstrace_printf(pInfo, zFormat, zVal); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 zOp = zBuf; | 469 zOp = zBuf; |
371 break; | 470 break; |
372 } | 471 } |
373 case SQLITE_FCNTL_CHUNK_SIZE: { | 472 case SQLITE_FCNTL_CHUNK_SIZE: { |
374 sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg); | 473 sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg); |
375 zOp = zBuf; | 474 zOp = zBuf; |
376 break; | 475 break; |
377 } | 476 } |
378 case SQLITE_FCNTL_FILE_POINTER: zOp = "FILE_POINTER"; break; | 477 case SQLITE_FCNTL_FILE_POINTER: zOp = "FILE_POINTER"; break; |
379 case SQLITE_FCNTL_SYNC_OMITTED: zOp = "SYNC_OMITTED"; break; | 478 case SQLITE_FCNTL_SYNC_OMITTED: zOp = "SYNC_OMITTED"; break; |
| 479 case SQLITE_FCNTL_WIN32_AV_RETRY: zOp = "WIN32_AV_RETRY"; break; |
| 480 case SQLITE_FCNTL_PERSIST_WAL: zOp = "PERSIST_WAL"; break; |
| 481 case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break; |
| 482 case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break; |
| 483 case SQLITE_FCNTL_TEMPFILENAME: zOp = "TEMPFILENAME"; break; |
380 case 0xca093fa0: zOp = "DB_UNCHANGED"; break; | 484 case 0xca093fa0: zOp = "DB_UNCHANGED"; break; |
| 485 case SQLITE_FCNTL_PRAGMA: { |
| 486 const char *const* a = (const char*const*)pArg; |
| 487 sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]); |
| 488 zOp = zBuf; |
| 489 break; |
| 490 } |
381 default: { | 491 default: { |
382 sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op); | 492 sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op); |
383 zOp = zBuf; | 493 zOp = zBuf; |
384 break; | 494 break; |
385 } | 495 } |
386 } | 496 } |
387 vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)", | 497 vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)", |
388 pInfo->zVfsName, p->zFName, zOp); | 498 pInfo->zVfsName, p->zFName, zOp); |
389 rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg); | 499 rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg); |
390 vfstrace_print_errcode(pInfo, " -> %s\n", rc); | 500 vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 501 if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){ |
| 502 *(char**)pArg = sqlite3_mprintf("vfstrace.%s/%z", |
| 503 pInfo->zVfsName, *(char**)pArg); |
| 504 } |
| 505 if( (op==SQLITE_FCNTL_PRAGMA || op==SQLITE_FCNTL_TEMPFILENAME) |
| 506 && rc==SQLITE_OK && *(char**)pArg ){ |
| 507 vfstrace_printf(pInfo, "%s.xFileControl(%s,%s) returns %s", |
| 508 pInfo->zVfsName, p->zFName, zOp, *(char**)pArg); |
| 509 } |
391 return rc; | 510 return rc; |
392 } | 511 } |
393 | 512 |
394 /* | 513 /* |
395 ** Return the sector-size in bytes for an vfstrace-file. | 514 ** Return the sector-size in bytes for an vfstrace-file. |
396 */ | 515 */ |
397 static int vfstraceSectorSize(sqlite3_file *pFile){ | 516 static int vfstraceSectorSize(sqlite3_file *pFile){ |
398 vfstrace_file *p = (vfstrace_file *)pFile; | 517 vfstrace_file *p = (vfstrace_file *)pFile; |
399 vfstrace_info *pInfo = p->pInfo; | 518 vfstrace_info *pInfo = p->pInfo; |
400 int rc; | 519 int rc; |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 */ | 671 */ |
553 static int vfstraceAccess( | 672 static int vfstraceAccess( |
554 sqlite3_vfs *pVfs, | 673 sqlite3_vfs *pVfs, |
555 const char *zPath, | 674 const char *zPath, |
556 int flags, | 675 int flags, |
557 int *pResOut | 676 int *pResOut |
558 ){ | 677 ){ |
559 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; | 678 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
560 sqlite3_vfs *pRoot = pInfo->pRootVfs; | 679 sqlite3_vfs *pRoot = pInfo->pRootVfs; |
561 int rc; | 680 int rc; |
562 vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)", | 681 vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)", |
563 pInfo->zVfsName, zPath, flags); | 682 pInfo->zVfsName, zPath, flags); |
564 rc = pRoot->xAccess(pRoot, zPath, flags, pResOut); | 683 rc = pRoot->xAccess(pRoot, zPath, flags, pResOut); |
565 vfstrace_print_errcode(pInfo, " -> %s", rc); | 684 vfstrace_print_errcode(pInfo, " -> %s", rc); |
566 vfstrace_printf(pInfo, ", out=%d\n", *pResOut); | 685 vfstrace_printf(pInfo, ", out=%d\n", *pResOut); |
567 return rc; | 686 return rc; |
568 } | 687 } |
569 | 688 |
570 /* | 689 /* |
571 ** Populate buffer zOut with the full canonical pathname corresponding | 690 ** Populate buffer zOut with the full canonical pathname corresponding |
572 ** to the pathname in zPath. zOut is guaranteed to point to a buffer | 691 ** to the pathname in zPath. zOut is guaranteed to point to a buffer |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 } | 883 } |
765 pInfo->pRootVfs = pRoot; | 884 pInfo->pRootVfs = pRoot; |
766 pInfo->xOut = xOut; | 885 pInfo->xOut = xOut; |
767 pInfo->pOutArg = pOutArg; | 886 pInfo->pOutArg = pOutArg; |
768 pInfo->zVfsName = pNew->zName; | 887 pInfo->zVfsName = pNew->zName; |
769 pInfo->pTraceVfs = pNew; | 888 pInfo->pTraceVfs = pNew; |
770 vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n", | 889 vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n", |
771 pInfo->zVfsName, pRoot->zName); | 890 pInfo->zVfsName, pRoot->zName); |
772 return sqlite3_vfs_register(pNew, makeDefault); | 891 return sqlite3_vfs_register(pNew, makeDefault); |
773 } | 892 } |
OLD | NEW |