Better tdb tracing, start of decent replay_trace.
[ccan] / ccan / tdb / tdb.c
index db5e5b49e4b663eeb290401d7346342d870b6a28..50ff24a6e6ca4a44ca84a9b97c7ae3b37d440474 100644 (file)
@@ -155,7 +155,7 @@ static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
  * then the TDB_DATA will have zero length but
  * a non-zero pointer
  */
-TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
+static TDB_DATA do_tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
 {
        tdb_off_t rec_ptr;
        struct list_struct rec;
@@ -165,20 +165,20 @@ TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
        /* find which hash bucket it is in */
        hash = tdb->hash_fn(&key);
        if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
-               tdb_trace(tdb, "tdb_fetch ");
-               tdb_trace_record(tdb, key);
-               tdb_trace(tdb, "= ENOENT\n");
                return tdb_null;
        }
        ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
                                  rec.data_len);
        ret.dsize = rec.data_len;
        tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
-       tdb_trace(tdb, "tdb_fetch ");
-       tdb_trace_record(tdb, key);
-       tdb_trace(tdb, "= ");
-       tdb_trace_record(tdb, ret);
-       tdb_trace(tdb, "\n"); 
+       return ret;
+}
+
+TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
+{
+       TDB_DATA ret = do_tdb_fetch(tdb, key);
+
+       tdb_trace_1rec_retrec(tdb, "tdb_fetch", key, ret);
        return ret;
 }
 
@@ -212,15 +212,11 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
        hash = tdb->hash_fn(&key);
 
        if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
-               tdb_trace(tdb, "tdb_parse_record ");
-               tdb_trace_record(tdb, key);
-               tdb_trace(tdb, "= ENOENT\n"); 
+               tdb_trace_1rec_ret(tdb, "tdb_parse_record", key,
+                                  -TDB_ERR_NOEXIST);
                return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
        }
-
-       tdb_trace(tdb, "tdb_parse_record ");
-       tdb_trace_record(tdb, key);
-       tdb_trace(tdb, "= %u\n", rec.data_len); 
+       tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
 
        ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
                             rec.data_len, parser, private_data);
@@ -252,9 +248,7 @@ int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
        int ret;
 
        ret = tdb_exists_hash(tdb, key, hash);
-       tdb_trace(tdb, "tdb_exists ");
-       tdb_trace_record(tdb, key);
-       tdb_trace(tdb, "= %i\n", ret); 
+       tdb_trace_1rec_ret(tdb, "tdb_exists", key, ret);
        return ret;
 }
 
@@ -413,9 +407,7 @@ int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
        int ret;
 
        ret = tdb_delete_hash(tdb, key, hash);
-       tdb_trace(tdb, "tdb_delete ");
-       tdb_trace_record(tdb, key);
-       tdb_trace(tdb, "= %s\n", ret ? "ENOENT" : "0"); 
+       tdb_trace_1rec_ret(tdb, "tdb_delete", key, ret);
        return ret;
 }
 
@@ -448,52 +440,29 @@ static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
        return 0;
 }
 
-/* store an element in the database, replacing any existing element
-   with the same key 
-
-   return 0 on success, -1 on failure
-*/
-int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
+static int _tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
+                     int flag, uint32_t hash)
 {
        struct list_struct rec;
-       uint32_t hash;
        tdb_off_t rec_ptr;
        char *p = NULL;
        int ret = -1;
 
-       if (tdb->read_only || tdb->traverse_read) {
-               tdb->ecode = TDB_ERR_RDONLY;
-               return -1;
-       }
-
-       /* find which hash bucket it is in */
-       hash = tdb->hash_fn(&key);
-       if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
-               return -1;
-
-       tdb_trace(tdb, "tdb_store %s ", flag == TDB_INSERT ? "insert" :
-                 flag == TDB_MODIFY ? "modify" : "normal");
-       tdb_trace_record(tdb, key);
-       tdb_trace_record(tdb, dbuf);
-
        /* check for it existing, on insert. */
        if (flag == TDB_INSERT) {
                if (tdb_exists_hash(tdb, key, hash)) {
                        tdb->ecode = TDB_ERR_EXISTS;
-                       tdb_trace(tdb, "= EEXIST\n"); 
                        goto fail;
                }
        } else {
                /* first try in-place update, on modify or replace. */
                if (tdb_update_hash(tdb, key, hash, dbuf) == 0) {
-                       tdb_trace(tdb, "= inplace\n"); 
                        goto done;
                }
                if (tdb->ecode == TDB_ERR_NOEXIST &&
                    flag == TDB_MODIFY) {
                        /* if the record doesn't exist and we are in TDB_MODIFY mode then
                         we should fail the store */
-                       tdb_trace(tdb, "= ENOENT\n"); 
                        goto fail;
                }
        }
@@ -541,7 +510,6 @@ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
                                goto fail;
                        }
                        goto done;
-                       tdb_trace(tdb, "= fromdead\n"); 
                }
        }
 
@@ -579,8 +547,6 @@ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
        rec.full_hash = hash;
        rec.magic = TDB_MAGIC;
 
-       tdb_trace(tdb, "= allocate\n"); 
-
        /* write out and point the top of the hash chain at it */
        if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
            || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
@@ -597,6 +563,33 @@ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
        }
 
        SAFE_FREE(p); 
+       return ret;
+}
+
+/* store an element in the database, replacing any existing element
+   with the same key 
+
+   return 0 on success, -1 on failure
+*/
+int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
+{
+       uint32_t hash;
+       int ret;
+
+       if (tdb->read_only || tdb->traverse_read) {
+               tdb->ecode = TDB_ERR_RDONLY;
+               tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag,
+                                       -TDB_ERR_RDONLY);
+               return -1;
+       }
+
+       /* find which hash bucket it is in */
+       hash = tdb->hash_fn(&key);
+       if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
+               return -1;
+
+       ret = _tdb_store(tdb, key, dbuf, flag, hash);
+       tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, ret);
        tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
        return ret;
 }
@@ -614,11 +607,7 @@ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
        if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
                return -1;
 
-       dbuf = tdb_fetch(tdb, key);
-       tdb_trace(tdb, "tdb_append ");
-       tdb_trace_record(tdb, key);
-       tdb_trace_record(tdb, dbuf);
-       tdb_trace(tdb, "= %s\n", dbuf.dptr ? "insert" : "append");
+       dbuf = do_tdb_fetch(tdb, key);
 
        if (dbuf.dptr == NULL) {
                dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
@@ -644,7 +633,8 @@ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
        memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
        dbuf.dsize += new_dbuf.dsize;
 
-       ret = tdb_store(tdb, key, dbuf, 0);
+       ret = _tdb_store(tdb, key, dbuf, 0, hash);
+       tdb_trace_2rec_retrec(tdb, "tdb_append", key, new_dbuf, dbuf);
        
 failed:
        tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
@@ -697,7 +687,7 @@ int tdb_get_seqnum(struct tdb_context *tdb)
        tdb_off_t seqnum=0;
 
        tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
-       tdb_trace(tdb, "tdb_get_seqnum = %u\n", seqnum);
+       tdb_trace_ret(tdb, "tdb_get_seqnum", seqnum);
        return seqnum;
 }
 
@@ -779,7 +769,7 @@ int tdb_wipe_all(struct tdb_context *tdb)
                return -1;
        }
 
-       tdb_trace(tdb, "tdb_wipe_all\n");
+       tdb_trace(tdb, "tdb_wipe_all");
 
        /* see if the tdb has a recovery area, and remember its size
           if so. We don't want to lose this as otherwise each
@@ -854,31 +844,141 @@ failed:
 }
 
 #ifdef TDB_TRACE
-#include <stdarg.h>
+static void tdb_trace_write(struct tdb_context *tdb, const char *str)
+{
+       if (write(tdb->tracefd, str, strlen(str)) != strlen(str)) {
+               close(tdb->tracefd);
+               tdb->tracefd = -1;
+       }
+}
 
-void tdb_trace(const struct tdb_context *tdb, const char *fmt, ...)
+static void tdb_trace_start(struct tdb_context *tdb)
 {
-       char msg[256];
-       va_list args;
-       int len, err;
+       tdb_off_t seqnum=0;
+       char msg[sizeof(tdb_off_t) * 4];
+
+       tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
+       sprintf(msg, "%u ", seqnum);
+       tdb_trace_write(tdb, msg);
+}
 
-       va_start(args, fmt);
-       len = vsprintf(msg, fmt, args);
-       va_end(args);
+static void tdb_trace_end(struct tdb_context *tdb)
+{
+       tdb_trace_write(tdb, "\n");
+}
 
-       err = write(tdb->tracefd, msg, len);
+static void tdb_trace_end_ret(struct tdb_context *tdb, int ret)
+{
+       char msg[sizeof(ret) * 4];
+       sprintf(msg, " = %i\n", ret);
+       tdb_trace_write(tdb, msg);
 }
 
-void tdb_trace_record(const struct tdb_context *tdb, TDB_DATA rec)
+static void tdb_trace_record(struct tdb_context *tdb, TDB_DATA rec)
 {
        char msg[20];
        unsigned int i;
-       int err;
 
-       err = write(tdb->tracefd, msg, sprintf(msg, "%zu:", rec.dsize));
-       for (i = 0; i < rec.dsize; i++)
-               err += write(tdb->tracefd, msg, sprintf(msg, "%02x",
-                                                       rec.dptr[i]));
-       err += write(tdb->tracefd, " ", 1);
+       /* We differentiate zero-length records from non-existent ones. */
+       if (rec.dptr == NULL) {
+               tdb_trace_write(tdb, " NULL");
+               return;
+       }
+       sprintf(msg, " %zu:", rec.dsize);
+       tdb_trace_write(tdb, msg);
+       for (i = 0; i < rec.dsize; i++) {
+               sprintf(msg, "%02x", rec.dptr[i]);
+               tdb_trace_write(tdb, msg);
+       }
+}
+
+void tdb_trace(struct tdb_context *tdb, const char *op)
+{
+       tdb_trace_start(tdb);
+       tdb_trace_write(tdb, op);
+       tdb_trace_end(tdb);
+}
+
+void tdb_trace_open(struct tdb_context *tdb, const char *op,
+                   unsigned hash_size, unsigned tdb_flags, unsigned open_flags)
+{
+       char msg[128];
+
+       sprintf(msg, "%s %u %#x %#x", op, hash_size, tdb_flags, open_flags);
+       tdb_trace_start(tdb);
+       tdb_trace_write(tdb, msg);
+       tdb_trace_end(tdb);
+}
+
+void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret)
+{
+       tdb_trace_start(tdb);
+       tdb_trace_write(tdb, op);
+       tdb_trace_end_ret(tdb, ret);
+}
+
+void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret)
+{
+       tdb_trace_start(tdb);
+       tdb_trace_write(tdb, op);
+       tdb_trace_write(tdb, " =");
+       tdb_trace_record(tdb, ret);
+       tdb_trace_end(tdb);
+}
+
+void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
+                   TDB_DATA rec)
+{
+       tdb_trace_start(tdb);
+       tdb_trace_write(tdb, op);
+       tdb_trace_record(tdb, rec);
+       tdb_trace_end(tdb);
+}
+
+void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
+                       TDB_DATA rec, int ret)
+{
+       tdb_trace_start(tdb);
+       tdb_trace_write(tdb, op);
+       tdb_trace_record(tdb, rec);
+       tdb_trace_end_ret(tdb, ret);
+}
+
+void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
+                          TDB_DATA rec, TDB_DATA ret)
+{
+       tdb_trace_start(tdb);
+       tdb_trace_write(tdb, op);
+       tdb_trace_record(tdb, rec);
+       tdb_trace_write(tdb, " =");
+       tdb_trace_record(tdb, ret);
+       tdb_trace_end(tdb);
+}
+
+void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
+                            TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
+                            int ret)
+{
+       char msg[sizeof(ret) * 4];
+
+       sprintf(msg, " %#x", flag); 
+       tdb_trace_start(tdb);
+       tdb_trace_write(tdb, op);
+       tdb_trace_record(tdb, rec1);
+       tdb_trace_record(tdb, rec2);
+       tdb_trace_write(tdb, msg);
+       tdb_trace_end_ret(tdb, ret);
+}
+
+void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
+                          TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret)
+{
+       tdb_trace_start(tdb);
+       tdb_trace_write(tdb, op);
+       tdb_trace_record(tdb, rec1);
+       tdb_trace_record(tdb, rec2);
+       tdb_trace_write(tdb, " =");
+       tdb_trace_record(tdb, ret);
+       tdb_trace_end(tdb);
 }
 #endif