2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid[] = "@(#)db_pr.c 10.17 (Sleepycat) 9/15/97";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
31 static void __db_proff __P((void *));
32 static void __db_psize __P((DB_MPOOLFILE *));
36 * Force loading of this file.
38 * PUBLIC: void __db_loadme __P((void));
49 * 64K is the maximum page size, so by default we check for offsets
50 * larger than that, and, where possible, we refine the test.
52 #define PSIZE_BOUNDARY (64 * 1024 + 1)
53 static size_t set_psize = PSIZE_BOUNDARY;
57 * Initialize tree printing routines.
59 * PUBLIC: FILE *__db_prinit __P((FILE *));
66 set_fp = fp == NULL ? stdout : fp;
72 * Dump the tree to a file.
74 * PUBLIC: int __db_dump __P((DB *, char *, int));
77 __db_dump(dbp, name, all)
84 save_fp = NULL; /* XXX: Shut the compiler up. */
86 if (set_psize == PSIZE_BOUNDARY)
90 if ((fp = fopen(name, "w")) == NULL)
95 fp = __db_prinit(NULL);
98 if (dbp->type == DB_HASH)
99 (void)__db_prhash(dbp);
101 (void)__db_prbtree(dbp);
102 fprintf(fp, "%s\n", DB_LINE);
103 __db_prtree(dbp->mpf, all);
114 * Print out the DB structure information.
116 * PUBLIC: int __db_prdb __P((DB *));
122 static const FN fn[] = {
123 { DB_AM_DUP, "duplicates" },
124 { DB_AM_INMEM, "in-memory" },
125 { DB_AM_LOCKING, "locking" },
126 { DB_AM_LOGGING, "logging" },
127 { DB_AM_MLOCAL, "local mpool" },
128 { DB_AM_PGDEF, "default page size" },
129 { DB_AM_RDONLY, "read-only" },
130 { DB_AM_RECOVER, "recover" },
131 { DB_AM_SWAP, "needswap" },
132 { DB_AM_THREAD, "thread" },
133 { DB_BT_RECNUM, "btree:records" },
134 { DB_HS_DIRTYMETA, "hash:dirty-meta" },
135 { DB_RE_DELIMITER, "recno:delimiter" },
136 { DB_RE_FIXEDLEN, "recno:fixed-length" },
137 { DB_RE_PAD, "recno:pad" },
138 { DB_RE_RENUMBER, "recno:renumber" },
139 { DB_RE_SNAPSHOT, "recno:snapshot" },
145 fp = __db_prinit(NULL);
162 fprintf(fp, "%s ", t);
163 __db_prflags(dbp->flags, fn);
171 * Print out the btree internal information.
173 * PUBLIC: int __db_prbtree __P((DB *));
179 static const FN mfn[] = {
180 { BTM_DUP, "duplicates" },
181 { BTM_RECNO, "recno" },
194 fp = __db_prinit(NULL);
196 (void)fprintf(fp, "%s\nOn-page metadata:\n", DB_LINE);
198 if ((ret = __bam_lget(dbp, 0, PGNO_METADATA, DB_LOCK_READ, &lock)) != 0)
201 if ((ret = __bam_pget(dbp, (PAGE **)&mp, &i, 0)) != 0)
204 (void)fprintf(fp, "magic %#lx\n", (u_long)mp->magic);
205 (void)fprintf(fp, "version %lu\n", (u_long)mp->version);
206 (void)fprintf(fp, "pagesize %lu\n", (u_long)mp->pagesize);
207 (void)fprintf(fp, "maxkey: %lu minkey: %lu\n",
208 (u_long)mp->maxkey, (u_long)mp->minkey);
209 (void)fprintf(fp, "free %lu\n", (u_long)mp->free);
210 (void)fprintf(fp, "flags %lu", (u_long)mp->flags);
211 __db_prflags(mp->flags, mfn);
212 (void)fprintf(fp, "\n");
213 (void)memp_fput(dbp->mpf, mp, 0);
214 (void)__bam_lput(dbp, lock);
216 (void)fprintf(fp, "%s\nDB_INFO:\n", DB_LINE);
217 (void)fprintf(fp, "bt_maxkey: %lu bt_minkey: %lu\n",
218 (u_long)t->bt_maxkey, (u_long)t->bt_minkey);
219 (void)fprintf(fp, "bt_compare: %#lx bt_prefix: %#lx\n",
220 (u_long)t->bt_compare, (u_long)t->bt_prefix);
221 if ((rp = t->bt_recno) != NULL) {
223 "re_delim: %#lx re_pad: %#lx re_len: %lu re_source: %s\n",
224 (u_long)rp->re_delim, (u_long)rp->re_pad,
226 rp->re_source == NULL ? "" : rp->re_source);
228 "cmap: %#lx smap: %#lx emap: %#lx msize: %lu\n",
229 (u_long)rp->re_cmap, (u_long)rp->re_smap,
230 (u_long)rp->re_emap, (u_long)rp->re_msize);
232 (void)fprintf(fp, "stack:");
233 for (epg = t->bt_stack; epg < t->bt_sp; ++epg)
234 (void)fprintf(fp, " %lu", (u_long)epg->page->pgno);
235 (void)fprintf(fp, "\n");
236 (void)fprintf(fp, "ovflsize: %lu\n", (u_long)t->bt_ovflsize);
243 * Print out the hash internal information.
245 * PUBLIC: int __db_prhash __P((DB *));
253 int i, put_page, ret;
258 fp = __db_prinit(NULL);
260 fprintf(fp, "\thash_accesses %lu\n", (u_long)t->hash_accesses);
261 fprintf(fp, "\thash_collisions %lu\n", (u_long)t->hash_collisions);
262 fprintf(fp, "\thash_expansions %lu\n", (u_long)t->hash_expansions);
263 fprintf(fp, "\thash_overflows %lu\n", (u_long)t->hash_overflows);
264 fprintf(fp, "\thash_bigpages %lu\n", (u_long)t->hash_bigpages);
267 if (t->hdr == NULL) {
268 pgno = PGNO_METADATA;
269 if ((ret = memp_fget(dbp->mpf, &pgno, 0, &t->hdr)) != 0)
275 fprintf(fp, "\tmagic %#lx\n", (u_long)t->hdr->magic);
276 fprintf(fp, "\tversion %lu\n", (u_long)t->hdr->version);
277 fprintf(fp, "\tpagesize %lu\n", (u_long)t->hdr->pagesize);
278 fprintf(fp, "\tovfl_point %lu\n", (u_long)t->hdr->ovfl_point);
279 fprintf(fp, "\tlast_freed %lu\n", (u_long)t->hdr->last_freed);
280 fprintf(fp, "\tmax_bucket %lu\n", (u_long)t->hdr->max_bucket);
281 fprintf(fp, "\thigh_mask %#lx\n", (u_long)t->hdr->high_mask);
282 fprintf(fp, "\tlow_mask %#lx\n", (u_long)t->hdr->low_mask);
283 fprintf(fp, "\tffactor %lu\n", (u_long)t->hdr->ffactor);
284 fprintf(fp, "\tnelem %lu\n", (u_long)t->hdr->nelem);
285 fprintf(fp, "\th_charkey %#lx\n", (u_long)t->hdr->h_charkey);
287 for (i = 0; i < NCACHED; i++)
288 fprintf(fp, "%lu ", (u_long)t->hdr->spares[i]);
293 (void)memp_fput(dbp->mpf, (PAGE *)t->hdr, 0);
301 * Print out the entire tree.
303 * PUBLIC: int __db_prtree __P((DB_MPOOLFILE *, int));
306 __db_prtree(mpf, all)
314 if (set_psize == PSIZE_BOUNDARY)
318 for (i = PGNO_ROOT;; ++i) {
319 if ((ret = memp_fget(mpf, &i, 0, &h)) != 0)
321 if (TYPE(h) != P_INVALID)
322 if ((t_ret = __db_prpage(h, all)) != 0 && ret == 0)
324 (void)memp_fput(mpf, h, 0);
326 (void)fflush(__db_prinit(NULL));
332 * -- Print out a specific page.
334 * PUBLIC: int __db_prnpage __P((DB_MPOOLFILE *, db_pgno_t));
337 __db_prnpage(mpf, pgno)
344 if (set_psize == PSIZE_BOUNDARY)
347 if ((ret = memp_fget(mpf, &pgno, 0, &h)) != 0)
350 ret = __db_prpage(h, 1);
351 (void)fflush(__db_prinit(NULL));
353 (void)memp_fput(mpf, h, 0);
359 * -- Print out a page.
361 * PUBLIC: int __db_prpage __P((PAGE *, int));
373 db_indx_t dlen, len, i;
377 u_int8_t *ep, *hk, *p;
380 fp = __db_prinit(NULL);
390 s = "btree internal";
396 s = "recno internal";
408 fprintf(fp, "ILLEGAL PAGE TYPE: page: %lu type: %lu\n",
409 (u_long)h->pgno, (u_long)TYPE(h));
412 fprintf(fp, "page %4lu: (%s)\n", (u_long)h->pgno, s);
413 fprintf(fp, " lsn.file: %lu lsn.offset: %lu",
414 (u_long)LSN(h).file, (u_long)LSN(h).offset);
415 if (TYPE(h) == P_IBTREE || TYPE(h) == P_IRECNO ||
416 (TYPE(h) == P_LRECNO && h->pgno == PGNO_ROOT))
417 fprintf(fp, " total records: %4lu", (u_long)RE_NREC(h));
419 if (TYPE(h) == P_LBTREE || TYPE(h) == P_LRECNO)
420 fprintf(fp, " prev: %4lu next: %4lu",
421 (u_long)PREV_PGNO(h), (u_long)NEXT_PGNO(h));
422 if (TYPE(h) == P_IBTREE || TYPE(h) == P_LBTREE)
423 fprintf(fp, " level: %2lu", (u_long)h->level);
424 if (TYPE(h) == P_OVERFLOW) {
425 fprintf(fp, " ref cnt: %4lu ", (u_long)OV_REF(h));
426 __db_pr((u_int8_t *)h + P_OVERHEAD, OV_LEN(h));
429 fprintf(fp, " entries: %4lu", (u_long)NUM_ENT(h));
430 fprintf(fp, " offset: %4lu\n", (u_long)HOFFSET(h));
432 if (!all || TYPE(h) == P_INVALID)
436 for (i = 0; i < NUM_ENT(h); i++) {
437 if (P_ENTRY(h, i) - (u_int8_t *)h < P_OVERHEAD ||
438 (size_t)(P_ENTRY(h, i) - (u_int8_t *)h) >= set_psize) {
440 "ILLEGAL PAGE OFFSET: indx: %lu of %lu\n",
441 (u_long)i, (u_long)h->inp[i]);
454 deleted = i % 2 == 0 &&
455 B_DISSET(GET_BKEYDATA(h, i + O_INDX)->type);
460 deleted = B_DISSET(GET_BKEYDATA(h, i)->type);
464 "ILLEGAL PAGE ITEM: %lu\n", (u_long)TYPE(h));
468 fprintf(fp, " %s[%03lu] %4lu ",
469 deleted ? "D" : " ", (u_long)i, (u_long)h->inp[i]);
473 switch (HPAGE_PTYPE(hk)) {
476 HOFFDUP_PGNO(hk), sizeof(db_pgno_t));
478 "%4lu [offpage dups]\n", (u_long)pgno);
482 * If this is the first item on a page, then
483 * we cannot figure out how long it is, so
484 * we only print the first one in the duplicate
488 len = LEN_HKEYDATA(h, 0, i);
492 fprintf(fp, "Duplicates:\n");
493 for (p = HKEYDATA_DATA(hk),
494 ep = p + len; p < ep;) {
495 memcpy(&dlen, p, sizeof(db_indx_t));
496 p += sizeof(db_indx_t);
499 p += sizeof(db_indx_t) + dlen;
504 __db_pr(HKEYDATA_DATA(hk),
505 LEN_HKEYDATA(h, 0, i));
507 fprintf(fp, "%s\n", HKEYDATA_DATA(hk));
510 memcpy(&a_hkd, hk, HOFFPAGE_SIZE);
512 "overflow: total len: %4lu page: %4lu\n",
513 (u_long)a_hkd.tlen, (u_long)a_hkd.pgno);
519 fprintf(fp, "count: %4lu pgno: %4lu ",
520 (u_long)bi->nrecs, (u_long)bi->pgno);
521 switch (B_TYPE(bi->type)) {
523 __db_pr(bi->data, bi->len);
527 __db_proff(bi->data);
530 fprintf(fp, "ILLEGAL BINTERNAL TYPE: %lu\n",
531 (u_long)B_TYPE(bi->type));
538 fprintf(fp, "entries %4lu pgno %4lu\n",
539 (u_long)ri->nrecs, (u_long)ri->pgno);
545 switch (B_TYPE(bk->type)) {
547 __db_pr(bk->data, bk->len);
555 "ILLEGAL DUPLICATE/LBTREE/LRECNO TYPE: %lu\n",
556 (u_long)B_TYPE(bk->type));
569 * -- Decide if a page is corrupted.
571 * PUBLIC: int __db_isbad __P((PAGE *, int));
584 fp = __db_prinit(NULL);
597 fprintf(fp, "ILLEGAL PAGE TYPE: page: %lu type: %lu\n",
598 (u_long)h->pgno, (u_long)TYPE(h));
602 for (i = 0; i < NUM_ENT(h); i++) {
603 if (P_ENTRY(h, i) - (u_int8_t *)h < P_OVERHEAD ||
604 (size_t)(P_ENTRY(h, i) - (u_int8_t *)h) >= set_psize) {
606 "ILLEGAL PAGE OFFSET: indx: %lu of %lu\n",
607 (u_long)i, (u_long)h->inp[i]);
612 type = HPAGE_TYPE(h, i);
613 if (type != H_OFFDUP &&
614 type != H_DUPLICATE &&
617 fprintf(fp, "ILLEGAL HASH TYPE: %lu\n",
623 bi = GET_BINTERNAL(h, i);
624 if (B_TYPE(bi->type) != B_KEYDATA &&
625 B_TYPE(bi->type) != B_DUPLICATE &&
626 B_TYPE(bi->type) != B_OVERFLOW) {
627 fprintf(fp, "ILLEGAL BINTERNAL TYPE: %lu\n",
628 (u_long)B_TYPE(bi->type));
637 bk = GET_BKEYDATA(h, i);
638 if (B_TYPE(bk->type) != B_KEYDATA &&
639 B_TYPE(bk->type) != B_DUPLICATE &&
640 B_TYPE(bk->type) != B_OVERFLOW) {
642 "ILLEGAL DUPLICATE/LBTREE/LRECNO TYPE: %lu\n",
643 (u_long)B_TYPE(bk->type));
649 "ILLEGAL PAGE ITEM: %lu\n", (u_long)TYPE(h));
664 * Print out a data element.
666 * PUBLIC: void __db_pr __P((u_int8_t *, u_int32_t));
676 fp = __db_prinit(NULL);
678 fprintf(fp, "len: %3lu", (u_long)len);
681 fprintf(fp, " data: ");
682 for (i = len <= 20 ? len : 20; i > 0; --i, ++p) {
684 if (isprint(*p) || *p == '\n')
685 fprintf(fp, "%c", *p);
687 fprintf(fp, "%#x", (u_int)*p);
700 * Print out an off-page element.
709 fp = __db_prinit(NULL);
712 switch (B_TYPE(bo->type)) {
714 fprintf(fp, "overflow: total len: %4lu page: %4lu\n",
715 (u_long)bo->tlen, (u_long)bo->pgno);
718 fprintf(fp, "duplicate: page: %4lu\n", (u_long)bo->pgno);
725 * Print out flags values.
727 * PUBLIC: void __db_prflags __P((u_int32_t, const FN *));
730 __db_prflags(flags, fn)
739 fp = __db_prinit(NULL);
742 for (found = 0, fnp = fn; fnp->mask != 0; ++fnp)
743 if (fnp->mask & flags) {
744 fprintf(fp, "%s%s", sep, fnp->name);
763 set_psize = PSIZE_BOUNDARY - 1;
765 pgno = PGNO_METADATA;
766 if (memp_fget(mpf, &pgno, 0, &mp) != 0)
772 set_psize = mp->pagesize;
775 (void)memp_fput(mpf, mp, 0);