mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
wallet: Add tracing for sqlite statements
I found sqlite tracing was useful for debugging a test in #27790, and thought it might be helpful in other contexts too, so this PR adds an option to enable it. Tracing is still disabled by default and only shown with `-debug=walletdb -loglevel=walletdb:trace` options.
This commit is contained in:
parent
9e54dde04c
commit
ff9d961bf3
1 changed files with 22 additions and 0 deletions
|
@ -34,6 +34,21 @@ static void ErrorLogCallback(void* arg, int code, const char* msg)
|
|||
LogPrintf("SQLite Error. Code: %d. Message: %s\n", code, msg);
|
||||
}
|
||||
|
||||
static int TraceSqlCallback(unsigned code, void* context, void* param1, void* param2)
|
||||
{
|
||||
auto* db = static_cast<SQLiteDatabase*>(context);
|
||||
if (code == SQLITE_TRACE_STMT) {
|
||||
auto* stmt = static_cast<sqlite3_stmt*>(param1);
|
||||
// To be conservative and avoid leaking potentially secret information
|
||||
// in the log file, only expand statements that query the database, not
|
||||
// statements that update the database.
|
||||
char* expanded{sqlite3_stmt_readonly(stmt) ? sqlite3_expanded_sql(stmt) : nullptr};
|
||||
LogPrintf("[%s] SQLite Statement: %s\n", db->Filename(), expanded ? expanded : sqlite3_sql(stmt));
|
||||
if (expanded) sqlite3_free(expanded);
|
||||
}
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static bool BindBlobToStatement(sqlite3_stmt* stmt,
|
||||
int index,
|
||||
Span<const std::byte> blob,
|
||||
|
@ -235,6 +250,13 @@ void SQLiteDatabase::Open()
|
|||
if (ret != SQLITE_OK) {
|
||||
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable extended result codes: %s\n", sqlite3_errstr(ret)));
|
||||
}
|
||||
// Trace SQL statements if tracing is enabled with -debug=walletdb -loglevel=walletdb:trace
|
||||
if (LogAcceptCategory(BCLog::WALLETDB, BCLog::Level::Trace)) {
|
||||
ret = sqlite3_trace_v2(m_db, SQLITE_TRACE_STMT, TraceSqlCallback, this);
|
||||
if (ret != SQLITE_OK) {
|
||||
LogPrintf("Failed to enable SQL tracing for %s\n", Filename());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sqlite3_db_readonly(m_db, "main") != 0) {
|
||||
|
|
Loading…
Add table
Reference in a new issue