1 //-------------------------- DEBUG STAFF
2
iwkvd_trigger_xor(uint64_t val)3 void iwkvd_trigger_xor(uint64_t val) {
4 g_trigger ^= val;
5 }
6
iwkvd_kvblk(FILE * f,KVBLK * kb,int maxvlen)7 void iwkvd_kvblk(FILE *f, KVBLK *kb, int maxvlen) {
8 assert(f && kb && kb->addr);
9 uint8_t *mm, *vbuf, *kbuf;
10 uint32_t klen, vlen;
11 IWFS_FSM *fsm = &kb->db->iwkv->fsm;
12 blkn_t blkn = ADDR2BLK(kb->addr);
13 fprintf(f, "\n === KVBLK[%u] maxoff=%" PRIx64 ", zidx=%d, idxsz=%d, szpow=%u, flg=%x, db=%d\n", // -V576
14 blkn, (int64_t) kb->maxoff, kb->zidx, kb->idxsz, kb->szpow, kb->flags, kb->db->id);
15
16 iwrc rc = fsm->probe_mmap(fsm, 0, &mm, 0);
17 if (rc) {
18 iwlog_ecode_error3(rc);
19 return;
20 }
21 for (int i = 0; i < KVBLK_IDXNUM; ++i) {
22 KVP *kvp = &kb->pidx[i];
23 rc = _kvblk_key_peek(kb, i, mm, &kbuf, &klen);
24 if (rc) {
25 iwlog_ecode_error3(rc);
26 return;
27 }
28 _kvblk_value_peek(kb, i, mm, &vbuf, &vlen);
29 fprintf(f, "\n %02d: [%04" PRIx64 ", %02u, %02d]: %.*s:%.*s",
30 i, (int64_t) kvp->off, kvp->len, kvp->ridx,
31 klen, kbuf, MIN(vlen, maxvlen), vbuf);
32 }
33 fprintf(f, "\n");
34 }
35
36 #define IWKVD_MAX_VALSZ 96
37
iwkvd_sblk(FILE * f,IWLCTX * lx,SBLK * sb,int flags)38 iwrc iwkvd_sblk(FILE *f, IWLCTX *lx, SBLK *sb, int flags) {
39 assert(sb && sb->addr);
40 uint32_t lkl = 0;
41 char lkbuf[PREFIX_KEY_LEN_V1 + 1] = {0};
42 uint8_t *mm, *vbuf, *kbuf;
43 uint32_t klen, vlen;
44 IWFS_FSM *fsm = &sb->db->iwkv->fsm;
45 blkn_t blkn = ADDR2BLK(sb->addr);
46 iwrc rc = fsm->probe_mmap(fsm, 0, &mm, 0);
47 if (rc) {
48 iwlog_ecode_error3(rc);
49 return rc;
50 }
51 rc = _sblk_loadkvblk_mm(lx, sb, mm);
52 if (rc) {
53 iwlog_ecode_error3(rc);
54 return rc;
55 }
56 assert(sb->kvblk);
57 if (sb->flags & SBLK_DB) {
58 lkl = 0;
59 } else {
60 memcpy(&lkl, mm + sb->addr + SOFF_LKL_U1, 1);
61 lkl = IW_ITOHL(lkl);
62 if (lx->db->iwkv->fmt_version > 1) {
63 memcpy(lkbuf, mm + sb->addr + SOFF_LK_V2, lkl);
64 } else {
65 memcpy(lkbuf, mm + sb->addr + SOFF_LK_V1, lkl);
66 }
67 }
68 fprintf(f, "\n === SBLK[%u] lvl=%d, pnum=%d, flg=%x, kvzidx=%d, p0=%u, db=%u",
69 blkn,
70 ((IWKVD_PRINT_NO_LEVEVELS & flags) ? -1 : sb->lvl),
71 sb->pnum, sb->flags, sb->kvblk->zidx,
72 sb->p0,
73 sb->kvblk->db->id);
74
75 fprintf(f, "\n === SBLK[%u] szpow=%d, lkl=%d, lk=%s\n", blkn, sb->kvblk->szpow, lkl, lkbuf); // -V576
76
77 for (int i = 0, j = 0; i < sb->pnum; ++i, ++j) {
78 if (j == 3) {
79 fputc('\n', f);
80 j = 0;
81 }
82 if (j == 0) {
83 fprintf(f, " === SBLK[%u]", blkn);
84 }
85 rc = _kvblk_key_peek(sb->kvblk, sb->pi[i], mm, &kbuf, &klen);
86 if (rc) {
87 iwlog_ecode_error3(rc);
88 return rc;
89 }
90 if (flags & IWKVD_PRINT_VALS) {
91 _kvblk_value_peek(sb->kvblk, sb->pi[i], mm, &vbuf, &vlen);
92 fprintf(f, " [%03d,%03d] %.*s:%.*s", i, sb->pi[i], klen, kbuf, MIN(vlen, IWKVD_MAX_VALSZ), vbuf);
93 } else {
94 fprintf(f, " [%03d,%03d] %.*s", i, sb->pi[i], klen, kbuf);
95 }
96 }
97 fprintf(f, "\n\n");
98 return rc;
99 }
100
iwkvd_fsm(IWKV kv)101 IWFS_FSM *iwkvd_fsm(IWKV kv) {
102 return &kv->fsm;
103 }
104
iwkvd_db(FILE * f,IWDB db,int flags,int plvl)105 void iwkvd_db(FILE *f, IWDB db, int flags, int plvl) {
106 assert(db);
107 SBLK *sb, *tail;
108 IWLCTX lx = {
109 .db = db,
110 .nlvl = -1
111 };
112 iwrc rc = _sblk_at(&lx, db->addr, 0, &sb);
113 if (rc) {
114 iwlog_ecode_error3(rc);
115 return;
116 }
117 rc = _sblk_at(&lx, 0, 0, &tail);
118 if (rc) {
119 iwlog_ecode_error3(rc);
120 return;
121 }
122 fprintf(f, "\n\n== DB[%u] lvl=%d, blk=%u, dbflg=%x, p0=%u",
123 db->id,
124 ((IWKVD_PRINT_NO_LEVEVELS & flags) ? -1 : sb->lvl),
125 (unsigned int) ADDR2BLK(sb->addr),
126 db->dbflg,
127 tail->p0);
128 if (!(IWKVD_PRINT_NO_LEVEVELS & flags)) {
129 fprintf(f, "\n== DB[%u]->n=[", db->id);
130 for (int i = 0; i <= sb->lvl; ++i) {
131 if (i > 0) {
132 fprintf(f, ", %d:%u", i, sb->n[i]);
133 } else {
134 fprintf(f, "%d:%u", i, sb->n[i]);
135 }
136 }
137 fprintf(f, "]");
138 }
139 blkn_t blk = sb->n[plvl];
140 while (blk) {
141 rc = _sblk_at(&lx, BLK2ADDR(blk), 0, &sb);
142 if (rc) {
143 iwlog_ecode_error3(rc);
144 return;
145 }
146 iwkvd_sblk(f, &lx, sb, flags);
147 blk = sb->n[plvl];
148 _sblk_release(&lx, &sb);
149 }
150 fflush(f);
151 }
152
153
154