1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

First cut at implementing the new sqlite3_trace_v2() interface.

FossilOrigin-Name: cb0062feb018f52689938a58cb76886d431c33f0
This commit is contained in:
drh
2016-07-13 22:55:01 +00:00
parent ed916ba025
commit 3d2a529df6
9 changed files with 98 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
C Interface\sdesign\sfor\sa\snew\ssqlite3_trace_v2()\smethod\sthat\ssupersedes\nsqlite3_trace()\sand\ssqlite3_profile().
D 2016-07-13T21:30:03.340
C First\scut\sat\simplementing\sthe\snew\ssqlite3_trace_v2()\sinterface.
D 2016-07-13T22:55:01.845
F Makefile.in 6c20d44f72d4564f11652b26291a214c8367e5db
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc d66d0395c38571aab3804f8db0fa20707ae4609a
@@ -348,7 +348,7 @@ F src/hwtime.h 747c1bbe9df21a92e9c50f3bbec1de841dc5e5da
F src/insert.c 8f4e9fcbd8e95e85f15647ba8b413b18d556ec2b
F src/legacy.c 75d3023be8f0d2b99d60f905090341a03358c58e
F src/loadext.c 4237fd37ca589f1d90b3ea925dd9df20da8e0c84
F src/main.c 405d13e3a4f7c5add9fb27702ae70ed0a6e32cca
F src/main.c 05658dfa4be5704e4d6542fe0f2452be5b9df09e
F src/malloc.c 1443d1ad95d67c21d77af7ae3f44678252f0efec
F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
F src/mem1.c 6919bcf12f221868ea066eec27e579fed95ce98b
@@ -385,10 +385,10 @@ F src/resolve.c cca3aa77b95706df5d635a2141a4d1de60ae6598
F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
F src/select.c f3c6e9065fb34f6a23af27ec7f1f717ffbfc2ee4
F src/shell.c a8a9e392a6a2777fabf5feb536931cb190f235e5
F src/sqlite.h.in 63774172623fe82336ad0757f373343a87bd0b36
F src/sqlite.h.in 0fde7379e6a9d900802976de51cb1256f0fdac59
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h 2a170163d121095c6ab1ef05ed0413722f391d01
F src/sqliteInt.h dcf43b8abc5605b70f54ba80f42b6ad054b8ba95
F src/sqlite3ext.h 3875aa5b3c4bb5d8d14c387628c5fe852b2846bf
F src/sqliteInt.h 5b8a3e1dc92e7459b69954cce9f653bd2391deb8
F src/sqliteLimit.h c0373387c287c8d0932510b5547ecde31b5da247
F src/status.c 5b18f9526900f61189ab0b83f1ef41d9f871a2ab
F src/table.c 5226df15ab9179b9ed558d89575ea0ce37b03fc9
@@ -448,11 +448,11 @@ F src/trigger.c e14840ee0c3e549e758ec9bf3e4146e166002280
F src/update.c 4f05ea8cddfa367d045e03589756c02199e8f9bd
F src/utf.c 699001c79f28e48e9bcdf8a463da029ea660540c
F src/util.c 810ec3f22e2d1b62e66c30fe3621ebdedd23584d
F src/vacuum.c feb1eabb20987983d9350cad98299b21fa811f52
F src/vdbe.c 22b46c3b725e950e9f2760e2d76953d592600ad4
F src/vacuum.c 459ff7cb3c589451111ff2d3b6eb31be83a46a54
F src/vdbe.c c6bb62aa69a6faf1197eee36336c6de99bb4733f
F src/vdbe.h 67bc551f7faf04c33493892e4b378aada823ed10
F src/vdbeInt.h c59381049af5c7751a83456c39b80d1a6fde1f9d
F src/vdbeapi.c 02bcbc2ca5d2004b029088b05b468b394881e103
F src/vdbeapi.c a466743c8e6de100c11e044b693c9469d2cf598a
F src/vdbeaux.c c90275b0e55a2b32c03dc09314194fe46f2429d8
F src/vdbeblob.c 83d2d266383157b02e2b809350bb197e89d7895b
F src/vdbemem.c 1ecaa5ee0caff07255f25d04e8dc88befb6f88d1
@@ -1505,10 +1505,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 824b39e54fb9ba562be4d92cc9a54aee1cdf84cb
R eb1a84643390ea5aba50eced8805fb76
T *branch * sqlite3_trace_v2
T *sym-sqlite3_trace_v2 *
T -sym-trunk *
P 0c569f759f6c4701321d7fea5e7ccb371743bb6b
R eeeb6e6908fe861e59edc045bbf20a73
U drh
Z 6025cf3e994164a26dea4fb0e96d3f78
Z 1f8408ee484498502acfe0ea8cf0fbae

View File

@@ -1 +1 @@
0c569f759f6c4701321d7fea5e7ccb371743bb6b
cb0062feb018f52689938a58cb76886d431c33f0

View File

@@ -1033,6 +1033,9 @@ static int sqlite3Close(sqlite3 *db, int forceZombie){
return SQLITE_MISUSE_BKPT;
}
sqlite3_mutex_enter(db->mutex);
if( db->mTrace & SQLITE_TRACE_CLOSE ){
db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
}
/* Force xDisconnect calls on all virtual tables */
disconnectAllVtab(db);
@@ -1801,6 +1804,7 @@ int sqlite3_overload_function(
** trace is a pointer to a function that is invoked at the start of each
** SQL statement.
*/
#ifndef SQLITE_OMIT_DEPRECATED
void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
void *pOld;
@@ -1812,11 +1816,36 @@ void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
#endif
sqlite3_mutex_enter(db->mutex);
pOld = db->pTraceArg;
db->xTrace = xTrace;
db->mTrace = SQLITE_TRACE_LEGACY;
db->xTrace = (int(*)(u32,void*,void*,i64))xTrace;
db->pTraceArg = pArg;
sqlite3_mutex_leave(db->mutex);
return pOld;
}
#endif /* SQLITE_OMIT_DEPRECATED */
/* Register a trace callback using the version-2 interface.
*/
int sqlite3_trace_v2(
sqlite3 *db, /* Trace this connection */
int(*xTrace)(unsigned,void*,void*,sqlite3_int64), /* Callback to invoke */
unsigned mTrace, /* OPs to be traced */
void *pArg /* Context */
){
#ifdef SQLITE_ENABLE_API_ARMOR
if( !sqlite3SafetyCheckOk(db) ){
(void)SQLITE_MISUSE_BKPT;
return 0;
}
#endif
sqlite3_mutex_enter(db->mutex);
db->mTrace = mTrace;
db->xTrace = xTrace;
db->pTraceArg = pArg;
sqlite3_mutex_leave(db->mutex);
return SQLITE_OK;
}
/*
** Register a profile function. The pArg from the previously registered
** profile function is returned.

View File

@@ -2805,7 +2805,7 @@ SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
** The C argument is a copy of the context pointer passed in as the
** fourth argument to [sqlite3_trace_v2()].
** The P argument is a pointer whose meaning depends on T.
** The X argument is an unsigned 64-bit integer whose meaning also
** The X argument is an 64-bit integer whose meaning also
** depends on T.
**
** <dl>
@@ -2874,7 +2874,7 @@ SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
** The T argument is one of the [SQLITE_TRACE]
** constants to indicate why the callback was invoked.
** The C argument is a copy of the context pointer.
** The P and X arguments are a pointer and an unsigned 64-bit integer
** The P and X arguments are a pointer and a 64-bit integer
** whose meanings depend on T.
**
** The sqlite3_trace_v2() interface is intended to replace the legacy
@@ -2883,7 +2883,7 @@ SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
*/
int sqlite3_trace_v2(
sqlite3*,
int(*xCallback)(unsigned,void*,void*,sqlite3_uint64),
int(*xCallback)(unsigned,void*,void*,sqlite3_int64),
unsigned uMask,
void *pCtx
);

View File

@@ -281,6 +281,9 @@ struct sqlite3_api_routines {
int (*db_cacheflush)(sqlite3*);
/* Version 3.12.0 and later */
int (*system_errno)(sqlite3*);
/* Version 3.14.0 and later */
int (*trace_v2)(sqlite3*,int(*)(unsigned,void*,void*,sqlite3_uint64),
unsigned,void*);
};
/*
@@ -526,6 +529,8 @@ struct sqlite3_api_routines {
#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
/* Version 3.12.0 and later */
#define sqlite3_system_errno sqlite3_api->system_errno
/* Version 3.14.0 and later */
#define sqlite3_trace_v2 sqlite3_api->trace_v2
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)

View File

@@ -1241,6 +1241,15 @@ void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
const char*);
#endif
#ifndef SQLITE_OMIT_DEPRECATED
/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
** in the style of sqlite3_trace()
*/
#define SQLITE_TRACE_LEGACY 0x80
#else
#define SQLITE_TRACE_LEGACY 0
#endif /* SQLITE_OMIT_DEPRECATED */
/*
** Each database connection is an instance of the following structure.
@@ -1270,6 +1279,7 @@ struct sqlite3 {
u8 suppressErr; /* Do not issue error messages if true */
u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
u8 mTrace; /* zero or more SQLITE_TRACE flags */
int nextPagesize; /* Pagesize after VACUUM if >0 */
u32 magic; /* Magic number for detect library misuse */
int nChange; /* Value returned by sqlite3_changes() */
@@ -1290,7 +1300,7 @@ struct sqlite3 {
int nVDestroy; /* Number of active OP_VDestroy operations */
int nExtension; /* Number of loaded extensions */
void **aExtension; /* Array of shared library handles */
void (*xTrace)(void*,const char*); /* Trace function */
int (*xTrace)(u32,void*,void*,i64); /* Trace function */
void *pTraceArg; /* Argument to the trace function */
void (*xProfile)(void*,const char*,u64); /* Profiling function */
void *pProfileArg; /* Argument to profile function */

View File

@@ -121,7 +121,7 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
int saved_flags; /* Saved value of the db->flags */
int saved_nChange; /* Saved value of db->nChange */
int saved_nTotalChange; /* Saved value of db->nTotalChange */
void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */
u8 saved_mTrace; /* Saved trace settings */
Db *pDb = 0; /* Database to detach at end of vacuum */
int isMemDb; /* True if vacuuming a :memory: database */
int nRes; /* Bytes of reserved space at the end of each page */
@@ -142,7 +142,7 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
saved_flags = db->flags;
saved_nChange = db->nChange;
saved_nTotalChange = db->nTotalChange;
saved_xTrace = db->xTrace;
saved_mTrace = db->mTrace;
db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
db->xTrace = 0;
@@ -345,7 +345,7 @@ end_of_vacuum:
db->flags = saved_flags;
db->nChange = saved_nChange;
db->nTotalChange = saved_nTotalChange;
db->xTrace = saved_xTrace;
db->mTrace = saved_mTrace;
sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
/* Currently there is an SQL level transaction open on the vacuum

View File

@@ -1383,6 +1383,10 @@ case OP_ResultRow: {
}
if( db->mallocFailed ) goto no_mem;
if( db->mTrace & SQLITE_TRACE_ROW ){
db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
}
/* Return SQLITE_ROW
*/
p->pc = (int)(pOp - aOp) + 1;
@@ -6781,13 +6785,27 @@ case OP_Init: { /* jump */
char *z;
#ifndef SQLITE_OMIT_TRACE
if( db->xTrace
if( (db->mTrace & (SQLITE_TRACE_SQL|SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
&& !p->doingRerun
&& (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
){
z = sqlite3VdbeExpandSql(p, zTrace);
db->xTrace(db->pTraceArg, z);
sqlite3DbFree(db, z);
if( db->mTrace & (SQLITE_TRACE_SQL|SQLITE_TRACE_LEGACY) ){
z = sqlite3VdbeExpandSql(p, zTrace);
#ifndef SQLITE_OMIT_DEPRECATED
if( SQLITE_TRACE_LEGACY ){
void (*x)(void*,const char*);
x = (void(*)(void*,const char*))db->xTrace;
x(db->pTraceArg, z);
}else
#endif
{
db->xTrace(SQLITE_TRACE_SQL,db->pTraceArg,z,0);
}
sqlite3DbFree(db, z);
}
if( db->mTrace & SQLITE_TRACE_STMT ){
(void)db->xTrace(SQLITE_TRACE_STMT,db->pTraceArg,p,0);
}
}
#ifdef SQLITE_USE_FCNTL_TRACE
zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);

View File

@@ -60,12 +60,19 @@ static int vdbeSafetyNotNull(Vdbe *p){
*/
static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
sqlite3_int64 iNow;
sqlite3_int64 iElapse;
assert( p->startTime>0 );
assert( db->xProfile!=0 );
assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
assert( db->init.busy==0 );
assert( p->zSql!=0 );
sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
iElapse = (iNow - p->startTime)*1000000;
if( db->xProfile ){
db->xProfile(db->pProfileArg, p->zSql, iElapse);
}
if( db->mTrace & SQLITE_TRACE_PROFILE ){
db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, iElapse);
}
p->startTime = 0;
}
/*
@@ -569,7 +576,8 @@ static int sqlite3Step(Vdbe *p){
);
#ifndef SQLITE_OMIT_TRACE
if( db->xProfile && !db->init.busy && p->zSql ){
if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
&& !db->init.busy && p->zSql ){
sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
}else{
assert( p->startTime==0 );