Browse Source

db: full location tags for callers, make it implicit.

For better leak tracking.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
ae17c64c4a
  1. 15
      wallet/db.c
  2. 9
      wallet/db.h
  3. 17
      wallet/wallet.c

15
wallet/db.c

@ -396,7 +396,7 @@ void db_stmt_done(sqlite3_stmt *stmt)
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} }
sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query) sqlite3_stmt *db_prepare_(const char *location, struct db *db, const char *query)
{ {
int err; int err;
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
@ -406,9 +406,9 @@ sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query)
err = sqlite3_prepare_v2(db->sql, query, -1, &stmt, NULL); err = sqlite3_prepare_v2(db->sql, query, -1, &stmt, NULL);
if (err != SQLITE_OK) if (err != SQLITE_OK)
fatal("%s: %s: %s", caller, query, sqlite3_errmsg(db->sql)); fatal("%s: %s: %s", location, query, sqlite3_errmsg(db->sql));
dev_statement_start(stmt, caller); dev_statement_start(stmt, location);
return stmt; return stmt;
} }
@ -468,7 +468,7 @@ fail:
} }
sqlite3_stmt *PRINTF_FMT(3, 4) sqlite3_stmt *PRINTF_FMT(3, 4)
db_query(const char *caller, struct db *db, const char *fmt, ...) db_query_(const char *location, struct db *db, const char *fmt, ...)
{ {
va_list ap; va_list ap;
char *query; char *query;
@ -484,7 +484,7 @@ sqlite3_stmt *PRINTF_FMT(3, 4)
sqlite3_prepare_v2(db->sql, query, -1, &stmt, NULL); sqlite3_prepare_v2(db->sql, query, -1, &stmt, NULL);
tal_free(query); tal_free(query);
if (stmt) if (stmt)
dev_statement_start(stmt, caller); dev_statement_start(stmt, location);
return stmt; return stmt;
} }
@ -554,8 +554,7 @@ static int db_get_version(struct db *db)
{ {
int err; int err;
u64 res = -1; u64 res = -1;
sqlite3_stmt *stmt = sqlite3_stmt *stmt = db_query(db, "SELECT version FROM version LIMIT 1");
db_query(__func__, db, "SELECT version FROM version LIMIT 1");
if (!stmt) if (!stmt)
return -1; return -1;
@ -658,7 +657,7 @@ s64 db_get_intvar(struct db *db, char *varname, s64 defval)
int err; int err;
s64 res = defval; s64 res = defval;
sqlite3_stmt *stmt = sqlite3_stmt *stmt =
db_query(__func__, db, db_query(db,
"SELECT val FROM vars WHERE name='%s' LIMIT 1", varname); "SELECT val FROM vars WHERE name='%s' LIMIT 1", varname);
if (!stmt) if (!stmt)

9
wallet/db.h

@ -37,7 +37,9 @@ struct db *db_setup(const tal_t *ctx, struct log *log);
* db_query - Prepare and execute a query, and return the result (or NULL) * db_query - Prepare and execute a query, and return the result (or NULL)
*/ */
sqlite3_stmt *PRINTF_FMT(3, 4) sqlite3_stmt *PRINTF_FMT(3, 4)
db_query(const char *caller, struct db *db, const char *fmt, ...); db_query_(const char *location, struct db *db, const char *fmt, ...);
#define db_query(db, ...) \
db_query_(__FILE__ ":" stringify(__LINE__), db, __VA_ARGS__)
/** /**
* db_begin_transaction - Begin a transaction * db_begin_transaction - Begin a transaction
@ -84,8 +86,9 @@ s64 db_get_intvar(struct db *db, char *varname, s64 defval);
* @db: Database to query/exec * @db: Database to query/exec
* @query: The SQL statement to compile * @query: The SQL statement to compile
*/ */
#define db_prepare(db,query) db_prepare_(__func__,db,query) #define db_prepare(db,query) \
sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query); db_prepare_(__FILE__ ":" stringify(__LINE__), db, query)
sqlite3_stmt *db_prepare_(const char *location, struct db *db, const char *query);
/** /**
* db_exec_prepared -- Execute a prepared statement * db_exec_prepared -- Execute a prepared statement

17
wallet/wallet.c

@ -496,7 +496,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
struct wireaddr *addrp, addr; struct wireaddr *addrp, addr;
sqlite3_stmt *stmt = sqlite3_stmt *stmt =
db_query(__func__, w->db, db_query(w->db,
"SELECT id, node_id, address FROM peers WHERE id=%"PRIu64";", dbid); "SELECT id, node_id, address FROM peers WHERE id=%"PRIu64";", dbid);
if (!stmt || sqlite3_step(stmt) != SQLITE_ROW) { if (!stmt || sqlite3_step(stmt) != SQLITE_ROW) {
@ -685,9 +685,8 @@ bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w)
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
/* We load all channels */ /* We load all channels */
stmt = db_query( stmt = db_query(w->db, "SELECT %s FROM channels;",
__func__, w->db, "SELECT %s FROM channels;", channel_fields);
channel_fields);
w->max_channel_dbid = 0; w->max_channel_dbid = 0;
@ -828,7 +827,7 @@ bool wallet_channel_config_load(struct wallet *w, const u64 id,
"SELECT id, dust_limit_satoshis, max_htlc_value_in_flight_msat, " "SELECT id, dust_limit_satoshis, max_htlc_value_in_flight_msat, "
"channel_reserve_satoshis, htlc_minimum_msat, to_self_delay, " "channel_reserve_satoshis, htlc_minimum_msat, to_self_delay, "
"max_accepted_htlcs FROM channel_configs WHERE id=%" PRIu64 ";"; "max_accepted_htlcs FROM channel_configs WHERE id=%" PRIu64 ";";
sqlite3_stmt *stmt = db_query(__func__, w->db, query, id); sqlite3_stmt *stmt = db_query(w->db, query, id);
if (!stmt || sqlite3_step(stmt) != SQLITE_ROW) { if (!stmt || sqlite3_step(stmt) != SQLITE_ROW) {
db_stmt_done(stmt); db_stmt_done(stmt);
return false; return false;
@ -1008,7 +1007,7 @@ void wallet_peer_delete(struct wallet *w, u64 peer_dbid)
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
/* Must not have any channels still using this peer */ /* Must not have any channels still using this peer */
stmt = db_query(__func__, w->db, stmt = db_query(w->db,
"SELECT * FROM channels WHERE peer_id = %"PRIu64, "SELECT * FROM channels WHERE peer_id = %"PRIu64,
peer_dbid); peer_dbid);
assert(sqlite3_step(stmt) == SQLITE_DONE); assert(sqlite3_step(stmt) == SQLITE_DONE);
@ -1280,7 +1279,7 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet,
log_debug(wallet->log, "Loading HTLCs for channel %"PRIu64, chan->dbid); log_debug(wallet->log, "Loading HTLCs for channel %"PRIu64, chan->dbid);
sqlite3_stmt *stmt = db_query( sqlite3_stmt *stmt = db_query(
__func__, wallet->db, wallet->db,
"SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, " "SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, "
"payment_hash, shared_secret, payment_key, routing_onion FROM channel_htlcs WHERE " "payment_hash, shared_secret, payment_key, routing_onion FROM channel_htlcs WHERE "
"direction=%d AND channel_id=%" PRIu64 " AND hstate != %d", "direction=%d AND channel_id=%" PRIu64 " AND hstate != %d",
@ -1301,7 +1300,7 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet,
db_stmt_done(stmt); db_stmt_done(stmt);
stmt = db_query( stmt = db_query(
__func__, wallet->db, wallet->db,
"SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, " "SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, "
"payment_hash, origin_htlc, payment_key, routing_onion FROM channel_htlcs WHERE " "payment_hash, origin_htlc, payment_key, routing_onion FROM channel_htlcs WHERE "
"direction=%d AND channel_id=%" PRIu64 " AND hstate != %d", "direction=%d AND channel_id=%" PRIu64 " AND hstate != %d",
@ -1907,7 +1906,7 @@ void wallet_htlc_sigs_save(struct wallet *w, u64 channel_id,
bool wallet_network_check(struct wallet *w, bool wallet_network_check(struct wallet *w,
const struct chainparams *chainparams) const struct chainparams *chainparams)
{ {
sqlite3_stmt *stmt = db_query(__func__, w->db, sqlite3_stmt *stmt = db_query(w->db,
"SELECT val FROM vars WHERE name='genesis_hash'"); "SELECT val FROM vars WHERE name='genesis_hash'");
struct bitcoin_blkid chainhash; struct bitcoin_blkid chainhash;

Loading…
Cancel
Save