summaryrefslogtreecommitdiff
path: root/src/btree/bt_debug.c
blob: a3f9511a6ee30e50f41fd5a31969905406d2e209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
/*-
 * Copyright (c) 2008-2012 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

#ifdef HAVE_DIAGNOSTIC
/*
 * We pass around a session handle and output information, group it together.
 */
typedef struct {
	WT_SESSION_IMPL *session;		/* Enclosing session */

	/*
	 * When using the standard event handlers, the debugging output has to
	 * do its own message handling because its output isn't line-oriented.
	 */
	FILE		*fp;			/* Output file stream */
	WT_ITEM		*msg;			/* Buffered message */

	WT_ITEM		*tmp;			/* Temporary space */
} WT_DBG;

/* Diagnostic output separator. */
static const char *sep = "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n";

static int  __debug_cell(WT_DBG *, WT_PAGE_HEADER *, WT_CELL_UNPACK *);
static int  __debug_cell_data(WT_DBG *, const char *, WT_CELL_UNPACK *);
static void __debug_col_skip(WT_DBG *, WT_INSERT_HEAD *, const char *, int);
static int  __debug_config(WT_SESSION_IMPL *, WT_DBG *, const char *);
static int  __debug_dsk_cell(WT_DBG *, WT_PAGE_HEADER *);
static void __debug_dsk_col_fix(WT_DBG *, WT_PAGE_HEADER *);
static void __debug_ikey(WT_DBG *, WT_IKEY *);
static void __debug_item(WT_DBG *, const char *, const void *, size_t);
static int  __debug_page(WT_DBG *, WT_PAGE *, uint32_t);
static void __debug_page_col_fix(WT_DBG *, WT_PAGE *);
static int  __debug_page_col_int(WT_DBG *, WT_PAGE *, uint32_t);
static int  __debug_page_col_var(WT_DBG *, WT_PAGE *);
static int  __debug_page_hdr(WT_DBG *, WT_PAGE *);
static int  __debug_page_modify(WT_DBG *, WT_PAGE *);
static int  __debug_page_row_int(WT_DBG *, WT_PAGE *, uint32_t);
static int  __debug_page_row_leaf(WT_DBG *, WT_PAGE *);
static int  __debug_ref(WT_DBG *, WT_REF *, WT_PAGE *);
static void __debug_row_skip(WT_DBG *, WT_INSERT_HEAD *);
static int  __debug_tree(WT_SESSION_IMPL *, WT_PAGE *, const char *, uint32_t);
static void __debug_update(WT_DBG *, WT_UPDATE *, int);
static void __dmsg(WT_DBG *, const char *, ...)
	WT_GCC_ATTRIBUTE((format (printf, 2, 3)));
static void __dmsg_wrapup(WT_DBG *);

/*
 * __debug_hex_byte --
 *	Output a single byte in hex.
 */
static inline void
__debug_hex_byte(WT_DBG *ds, uint8_t v)
{
	static const char hex[] = "0123456789abcdef";

	__dmsg(ds, "#%c%c", hex[(v & 0xf0) >> 4], hex[v & 0x0f]);
}

/*
 * __debug_config --
 *	Configure debugging output.
 */
static int
__debug_config(WT_SESSION_IMPL *session, WT_DBG *ds, const char *ofile)
{
	memset(ds, 0, sizeof(WT_DBG));

	ds->session = session;

	WT_RET(__wt_scr_alloc(session, 512, &ds->tmp));

	/*
	 * If we weren't given a file, we use the default event handler, and
	 * we'll have to buffer messages.
	 */
	if (ofile == NULL)
		return (__wt_scr_alloc(session, 512, &ds->msg));

	/* If we're using a file, flush on each line. */
	if ((ds->fp = fopen(ofile, "w")) == NULL)
		WT_RET_MSG(session, __wt_errno(), "%s", ofile);

	(void)setvbuf(ds->fp, NULL, _IOLBF, 0);
	return (0);
}

/*
 * __dmsg_wrapup --
 *	Flush any remaining output, release resources.
 */
static void
__dmsg_wrapup(WT_DBG *ds)
{
	WT_SESSION_IMPL *session;
	WT_ITEM *msg;

	session = ds->session;
	msg = ds->msg;

	__wt_scr_free(&ds->tmp);

	/*
	 * Discard the buffer -- it shouldn't have anything in it, but might
	 * as well be cautious.
	 */
	if (msg != NULL) {
		if (msg->size != 0)
			__wt_msg(session, "%s", (char *)msg->mem);
		__wt_scr_free(&ds->msg);
	}

	/* Close any file we opened. */
	if (ds->fp != NULL)
		(void)fclose(ds->fp);
}

/*
 * __dmsg --
 *	Debug message.
 */
static void
__dmsg(WT_DBG *ds, const char *fmt, ...)
{
	va_list ap;
	WT_ITEM *msg;
	WT_SESSION_IMPL *session;
	size_t len, space;
	char *p;

	session = ds->session;

	/*
	 * Debug output chunks are not necessarily terminated with a newline
	 * character.  It's easy if we're dumping to a stream, but if we're
	 * dumping to an event handler, which is line-oriented, we must buffer
	 * the output chunk, and pass it to the event handler once we see a
	 * terminating newline.
	 */
	if (ds->fp == NULL) {
		msg = ds->msg;
		for (;;) {
			p = (char *)msg->mem + msg->size;
			space = msg->memsize - msg->size;
			va_start(ap, fmt);
			len = (size_t)vsnprintf(p, space, fmt, ap);
			va_end(ap);

			/* Check if there was enough space. */
			if (len < space) {
				msg->size += (uint32_t)len;
				break;
			}

			/*
			 * There's not much to do on error without checking for
			 * an error return on every single printf.  Anyway, it's
			 * pretty unlikely and this is debugging output, I'm not
			 * going to worry about it.
			 */
			if (__wt_buf_grow(
			    session, msg, msg->memsize + len + 128) != 0)
				return;
		}
		if (((uint8_t *)msg->mem)[msg->size - 1] == '\n') {
			((uint8_t *)msg->mem)[msg->size - 1] = '\0';
			__wt_msg(session, "%s", (char *)msg->mem);
			msg->size = 0;
		}
	} else {
		va_start(ap, fmt);
		(void)vfprintf(ds->fp, fmt, ap);
		va_end(ap);
	}
}

/*
 * __wt_debug_addr --
 *	Read and dump a disk page in debugging mode.
 */
int
__wt_debug_addr(
    WT_SESSION_IMPL *session, uint32_t addr, uint32_t size, const char *ofile)
{
	WT_ITEM *buf;
	int ret;

	buf = NULL;
	ret = 0;

	WT_RET(__wt_scr_alloc(session, size, &buf));
	WT_ERR(__wt_block_read(
	    session, session->btree->block, buf, addr, size, 0));
	ret = __wt_debug_disk(session, buf->mem, ofile);
err:	__wt_scr_free(&buf);

	return (ret);
}

/*
 * __wt_debug_disk --
 *	Dump a disk page in debugging mode.
 */
int
__wt_debug_disk(
    WT_SESSION_IMPL *session, WT_PAGE_HEADER *dsk, const char *ofile)
{
	WT_DBG *ds, _ds;
	int ret;

	ret = 0;

	ds = &_ds;
	WT_RET(__debug_config(session, ds, ofile));

	__dmsg(ds, "%s page", __wt_page_type_string(dsk->type));
	switch (dsk->type) {
	case WT_PAGE_COL_FIX:
	case WT_PAGE_COL_INT:
	case WT_PAGE_COL_VAR:
		__dmsg(ds, ", recno %" PRIu64, dsk->recno);
		/* FALLTHROUGH */
	case WT_PAGE_ROW_INT:
	case WT_PAGE_ROW_LEAF:
		__dmsg(ds, ", entries %" PRIu32 "\n", dsk->u.entries);
		break;
	case WT_PAGE_OVFL:
		__dmsg(ds, ", datalen %" PRIu32 "\n", dsk->u.datalen);
		break;
	WT_ILLEGAL_VALUE(session);
	}

	switch (dsk->type) {
	case WT_PAGE_COL_FIX:
		__debug_dsk_col_fix(ds, dsk);
		break;
	case WT_PAGE_COL_INT:
	case WT_PAGE_COL_VAR:
	case WT_PAGE_ROW_INT:
	case WT_PAGE_ROW_LEAF:
		ret = __debug_dsk_cell(ds, dsk);
		break;
	default:
		break;
	}

	__dmsg_wrapup(ds);

	return (ret);
}

/*
 * __debug_dsk_col_fix --
 *	Dump a WT_PAGE_COL_FIX page.
 */
static void
__debug_dsk_col_fix(WT_DBG *ds, WT_PAGE_HEADER *dsk)
{
	WT_BTREE *btree;
	uint32_t i;
	uint8_t v;

	btree = ds->session->btree;

	WT_FIX_FOREACH(btree, dsk, v, i) {
		__dmsg(ds, "\t{");
		__debug_hex_byte(ds, v);
		__dmsg(ds, "}\n");
	}
}

/*
 * __debug_dsk_cell --
 *	Dump a page of WT_CELL's.
 */
static int
__debug_dsk_cell(WT_DBG *ds, WT_PAGE_HEADER *dsk)
{
	WT_BTREE *btree;
	WT_CELL *cell;
	WT_CELL_UNPACK *unpack, _unpack;
	uint32_t i;

	btree = ds->session->btree;
	unpack = &_unpack;

	WT_CELL_FOREACH(btree, dsk, cell, unpack, i) {
		__wt_cell_unpack(cell, unpack);
		WT_RET(__debug_cell(ds, dsk, unpack));
	}
	return (0);
}

#define	WT_DEBUG_TREE_LEAF	0x01			/* Debug leaf pages */
#define	WT_DEBUG_TREE_WALK	0x02			/* Descend the tree */

/*
 * __wt_debug_tree_all --
 *	Dump the in-memory information for a tree, including leaf pages.
 */
int
__wt_debug_tree_all(WT_SESSION_IMPL *session, WT_PAGE *page, const char *ofile)
{
	return (__debug_tree(
	    session, page, ofile, WT_DEBUG_TREE_LEAF | WT_DEBUG_TREE_WALK));
}

/*
 * __wt_debug_tree --
 *	Dump the in-memory information for a tree, not including leaf pages.
 */
int
__wt_debug_tree(WT_SESSION_IMPL *session, WT_PAGE *page, const char *ofile)
{
	return (__debug_tree(session, page, ofile, WT_DEBUG_TREE_WALK));
}

/*
 * __wt_debug_page --
 *	Dump the in-memory information for a page.
 */
int
__wt_debug_page(WT_SESSION_IMPL *session, WT_PAGE *page, const char *ofile)
{
	WT_DBG *ds, _ds;
	int ret;

	ds = &_ds;
	WT_RET(__debug_config(session, ds, ofile));

	ret = __debug_page(ds, page, WT_DEBUG_TREE_LEAF);

	__dmsg_wrapup(ds);

	return (ret);
}

/*
 * __debug_tree --
 *	Dump the in-memory information for a tree.
 */
static int
__debug_tree(
    WT_SESSION_IMPL *session, WT_PAGE *page, const char *ofile, uint32_t flags)
{
	WT_DBG *ds, _ds;
	int ret;

	ds = &_ds;
	WT_RET(__debug_config(session, ds, ofile));

	/* A NULL page starts at the top of the tree -- it's a convenience. */
	if (page == NULL)
		page = session->btree->root_page;

	ret = __debug_page(ds, page, flags);

	__dmsg_wrapup(ds);

	return (ret);
}

/*
 * __debug_page --
 *	Dump the in-memory information for an in-memory page.
 */
static int
__debug_page(WT_DBG *ds, WT_PAGE *page, uint32_t flags)
{
	WT_SESSION_IMPL *session;

	session = ds->session;

	/* Dump the page header. */
	WT_RET(__debug_page_hdr(ds, page));

	/* Dump the page modification structure. */
	WT_RET(__debug_page_modify(ds, page));

	/* Dump the page. */
	switch (page->type) {
	case WT_PAGE_COL_FIX:
		if (LF_ISSET(WT_DEBUG_TREE_LEAF))
			__debug_page_col_fix(ds, page);
		break;
	case WT_PAGE_COL_INT:
		WT_RET(__debug_page_col_int(ds, page, flags));
		break;
	case WT_PAGE_COL_VAR:
		if (LF_ISSET(WT_DEBUG_TREE_LEAF))
			WT_RET(__debug_page_col_var(ds, page));
		break;
	case WT_PAGE_ROW_INT:
		WT_RET(__debug_page_row_int(ds, page, flags));
		break;
	case WT_PAGE_ROW_LEAF:
		if (LF_ISSET(WT_DEBUG_TREE_LEAF))
			WT_RET(__debug_page_row_leaf(ds, page));
		break;
	WT_ILLEGAL_VALUE(session);
	}

	return (0);
}

/*
 * __debug_page_hdr --
 *	Dump an in-memory page's metadata.
 */
static int
__debug_page_hdr(WT_DBG *ds, WT_PAGE *page)
{
	WT_SESSION_IMPL *session;

	session = ds->session;

	__dmsg(ds, "%p %s",
	    page, __wt_page_addr_string(session, ds->tmp, page));

	switch (page->type) {
	case WT_PAGE_COL_INT:
		__dmsg(ds, " recno %" PRIu64, page->u.intl.recno);
		break;
	case WT_PAGE_COL_FIX:
		__dmsg(ds, " recno %" PRIu64, page->u.col_fix.recno);
		break;
	case WT_PAGE_COL_VAR:
		__dmsg(ds, " recno %" PRIu64, page->u.col_var.recno);
		break;
	case WT_PAGE_ROW_INT:
	case WT_PAGE_ROW_LEAF:
		break;
	WT_ILLEGAL_VALUE(session);
	}

	__dmsg(ds, ": %s", __wt_page_type_string(page->type));

	__dmsg(ds, " (%s", __wt_page_is_modified(page) ? "dirty" : "clean");
	if (F_ISSET(page, WT_PAGE_FORCE_EVICT))
		__dmsg(ds, ", force-evict");
	if (F_ISSET(page, WT_PAGE_PINNED))
		__dmsg(ds, ", pinned");
	if (F_ISSET(page, WT_PAGE_REC_EMPTY))
		__dmsg(ds, ", empty");
	if (F_ISSET(page, WT_PAGE_REC_REPLACE))
		__dmsg(ds, ", replaced");
	if (F_ISSET(page, WT_PAGE_REC_SPLIT))
		__dmsg(ds, ", split");
	if (F_ISSET(page, WT_PAGE_REC_SPLIT_MERGE))
		__dmsg(ds, ", split-merge");
	__dmsg(ds, ")\n");

	if (WT_PAGE_IS_ROOT(page))
		__dmsg(ds, "\troot");
	else
		__dmsg(ds, "\tparent %p", page->parent);
	__dmsg(ds,
	    ", disk %p, entries %" PRIu32 "\n", page->dsk, page->entries);

	return (0);
}

/*
 * __debug_page_modify --
 *	Dump an in-memory page's modification structure.
 */
static int
__debug_page_modify(WT_DBG *ds, WT_PAGE *page)
{
	WT_PAGE_MODIFY *mod;
	WT_PAGE_TRACK *track;
	WT_SESSION_IMPL *session;
	uint32_t i;

	session = ds->session;

	if ((mod = page->modify) == NULL)
		return (0);

	__dmsg(ds,
	    "\t" "write/disk generations: %" PRIu32 "/%" PRIu32 "\n",
	    mod->write_gen, mod->disk_gen);

	switch (F_ISSET(page, WT_PAGE_REC_MASK)) {
	case 0:
		break;
	case WT_PAGE_REC_EMPTY:
		__dmsg(ds, "\t" "empty page\n");
		break;
	case WT_PAGE_REC_REPLACE:
		__dmsg(ds, "\t" "replacement %s\n",
		    __wt_addr_string(session, ds->tmp,
		    mod->u.replace.addr, mod->u.replace.size));
		break;
	case WT_PAGE_REC_SPLIT:
		__dmsg(ds, "\t" "split page %p\n", mod->u.split);
		break;
	case WT_PAGE_REC_SPLIT_MERGE:
		__dmsg(ds, "\t" "split-merge page %p\n", mod->u.split);
		break;
	WT_ILLEGAL_VALUE(session);
	}

	if (mod->track_entries != 0)
		__dmsg(ds, "\t" "tracking list:\n");
	for (track = mod->track, i = 0; i < mod->track_entries; ++track, ++i) {
		switch (track->type) {
		case WT_PT_BLOCK:
			__dmsg(ds, "\t\t" "block");
			break;
		case WT_PT_BLOCK_EVICT:
			__dmsg(ds, "\t\t" "block-evict");
			break;
		case WT_PT_OVFL:
			__dmsg(ds, "\t\t" "overflow (on)");
			break;
		case WT_PT_OVFL_DISCARD:
			__dmsg(ds, "\t\t" "overflow (off)");
			break;
		case WT_PT_EMPTY:
			continue;
		WT_ILLEGAL_VALUE(session);
		}
		__dmsg(ds, " %s\n", __wt_addr_string(
		    session, ds->tmp, track->addr.addr, track->addr.size));
	}

	return (0);
}

/*
 * __debug_page_col_fix --
 *	Dump an in-memory WT_PAGE_COL_FIX page.
 */
static void
__debug_page_col_fix(WT_DBG *ds, WT_PAGE *page)
{
	WT_BTREE *btree;
	WT_INSERT *ins;
	WT_PAGE_HEADER *dsk;
	WT_SESSION_IMPL *session;
	uint64_t recno;
	uint32_t i;
	uint8_t v;

	session = ds->session;
	btree = session->btree;
	dsk = page->dsk;
	recno = page->u.col_fix.recno;

	if (dsk != NULL) {
		ins = WT_SKIP_FIRST(WT_COL_UPDATE_SINGLE(page));
		WT_FIX_FOREACH(btree, dsk, v, i) {
			__dmsg(ds, "\t%" PRIu64 "\t{", recno);
			__debug_hex_byte(ds, v);
			__dmsg(ds, "}\n");

			/* Check for a match on the update list. */
			if (ins != NULL && WT_INSERT_RECNO(ins) == recno) {
				__dmsg(ds,
				    "\tupdate %" PRIu64 "\n",
				    WT_INSERT_RECNO(ins));
				__debug_update(ds, ins->upd, 1);
				ins = WT_SKIP_NEXT(ins);
			}
			++recno;
		}
	}

	if (WT_COL_UPDATE_SINGLE(page) != NULL) {
		__dmsg(ds, "%s", sep);
		__debug_col_skip(ds, WT_COL_UPDATE_SINGLE(page), "update", 1);
	}
	if (WT_COL_APPEND(btree, page) != NULL) {
		__dmsg(ds, "%s", sep);
		__debug_col_skip(ds, WT_COL_APPEND(btree, page), "append", 1);
	}
}

/*
 * __debug_page_col_int --
 *	Dump an in-memory WT_PAGE_COL_INT page.
 */
static int
__debug_page_col_int(WT_DBG *ds, WT_PAGE *page, uint32_t flags)
{
	WT_REF *ref;
	uint32_t i;

	WT_REF_FOREACH(page, ref, i) {
		__dmsg(ds, "\trecno %" PRIu64 "\n", ref->u.recno);
		WT_RET(__debug_ref(ds, ref, page));
	}

	if (LF_ISSET(WT_DEBUG_TREE_WALK))
		WT_REF_FOREACH(page, ref, i)
			if (ref->state == WT_REF_MEM) {
				__dmsg(ds, "\n");
				WT_RET(__debug_page(ds, ref->page, flags));
			}

	return (0);
}

/*
 * __debug_page_col_var --
 *	Dump an in-memory WT_PAGE_COL_VAR page.
 */
static int
__debug_page_col_var(WT_DBG *ds, WT_PAGE *page)
{
	WT_BTREE *btree;
	WT_CELL *cell;
	WT_CELL_UNPACK *unpack, _unpack;
	WT_COL *cip;
	WT_INSERT_HEAD *update;
	uint64_t recno, rle;
	uint32_t i;
	char tag[64];

	btree = ds->session->btree;
	unpack = &_unpack;
	recno = page->u.col_var.recno;

	WT_COL_FOREACH(page, cip, i) {
		if ((cell = WT_COL_PTR(page, cip)) == NULL) {
			unpack = NULL;
			rle = 1;
		} else {
			__wt_cell_unpack(cell, unpack);
			rle = __wt_cell_rle(unpack);
		}
		snprintf(tag, sizeof(tag), "%" PRIu64 " %" PRIu64, recno, rle);
		WT_RET(__debug_cell_data(ds, tag, unpack));

		if ((update = WT_COL_UPDATE(page, cip)) != NULL)
			__debug_col_skip(ds, update, "update", 0);
		recno += rle;
	}

	if (WT_COL_APPEND(btree, page) != NULL) {
		__dmsg(ds, "%s", sep);
		__debug_col_skip(ds, WT_COL_APPEND(btree, page), "append", 0);
	}

	return (0);
}

/*
 * __debug_page_row_int --
 *	Dump an in-memory WT_PAGE_ROW_INT page.
 */
static int
__debug_page_row_int(WT_DBG *ds, WT_PAGE *page, uint32_t flags)
{
	WT_REF *ref;
	uint32_t i;

	WT_REF_FOREACH(page, ref, i) {
		__debug_ikey(ds, ref->u.key);
		WT_RET(__debug_ref(ds, ref, page));
	}

	if (LF_ISSET(WT_DEBUG_TREE_WALK))
		WT_REF_FOREACH(page, ref, i)
			if (ref->state == WT_REF_MEM) {
				__dmsg(ds, "\n");
				WT_RET(__debug_page(ds, ref->page, flags));
			}
	return (0);
}

/*
 * __debug_page_row_leaf --
 *	Dump an in-memory WT_PAGE_ROW_LEAF page.
 */
static int
__debug_page_row_leaf(WT_DBG *ds, WT_PAGE *page)
{
	WT_CELL *cell;
	WT_CELL_UNPACK *unpack, _unpack;
	WT_INSERT_HEAD *insert;
	WT_ROW *rip;
	WT_UPDATE *upd;
	uint32_t i;

	unpack = &_unpack;

	/*
	 * Dump any K/V pairs inserted into the page before the first from-disk
	 * key on the page.
	 */
	if ((insert = WT_ROW_INSERT_SMALLEST(page)) != NULL)
		__debug_row_skip(ds, insert);

	/* Dump the page's K/V pairs. */
	WT_ROW_FOREACH(page, rip, i) {
		if (__wt_off_page(page, rip->key))
			__debug_ikey(ds, rip->key);
		else {
			__wt_cell_unpack(rip->key, unpack);
			WT_RET(__debug_cell_data(ds, "K", unpack));
		}

		if ((cell = __wt_row_value(page, rip)) == NULL)
			__dmsg(ds, "\tV {}\n");
		else {
			__wt_cell_unpack(cell, unpack);
			WT_RET(__debug_cell_data(ds, "V", unpack));
		}

		if ((upd = WT_ROW_UPDATE(page, rip)) != NULL)
			__debug_update(ds, upd, 0);

		if ((insert = WT_ROW_INSERT(page, rip)) != NULL)
			__debug_row_skip(ds, insert);
	}

	return (0);
}

/*
 * __debug_col_skip --
 *	Dump a column-store skiplist.
 */
static void
__debug_col_skip(WT_DBG *ds, WT_INSERT_HEAD *head, const char *tag, int hexbyte)
{
	WT_INSERT *ins;

	WT_SKIP_FOREACH(ins, head) {
		__dmsg(ds,
		    "\t%s %" PRIu64 "\n", tag, WT_INSERT_RECNO(ins));
		__debug_update(ds, ins->upd, hexbyte);
	}
}

/*
 * __debug_row_skip --
 *	Dump an insert array.
 */
static void
__debug_row_skip(WT_DBG *ds, WT_INSERT_HEAD *head)
{
	WT_INSERT *ins;

	WT_SKIP_FOREACH(ins, head) {
		__debug_item(ds,
		    "insert", WT_INSERT_KEY(ins), WT_INSERT_KEY_SIZE(ins));
		__debug_update(ds, ins->upd, 0);
	}
}

/*
 * __debug_update --
 *	Dump an update array.
 */
static void
__debug_update(WT_DBG *ds, WT_UPDATE *upd, int hexbyte)
{
	for (; upd != NULL; upd = upd->next)
		if (WT_UPDATE_DELETED_ISSET(upd))
			__dmsg(ds, "\tvalue {deleted}\n");
		else if (hexbyte) {
			__dmsg(ds, "\t{");
			__debug_hex_byte(ds,
			    ((uint8_t *)WT_UPDATE_DATA(upd))[0]);
			__dmsg(ds, "}\n");
		} else
			__debug_item(ds,
			    "value", WT_UPDATE_DATA(upd), upd->size);
}

/*
 * __debug_ref --
 *	Dump a WT_REF structure.
 */
static int
__debug_ref(WT_DBG *ds, WT_REF *ref, WT_PAGE *page)
{
	WT_SESSION_IMPL *session;
	uint32_t size;
	const uint8_t *addr;

	session = ds->session;

	__dmsg(ds, "\t");
	switch (ref->state) {
	case WT_REF_DISK:
		__dmsg(ds, "disk");
		break;
	case WT_REF_EVICTING:
		__dmsg(ds, "evicting %p", ref->page);
		break;
	case WT_REF_LOCKED:
		__dmsg(ds, "locked %p", ref->page);
		break;
	case WT_REF_MEM:
		__dmsg(ds, "memory %p", ref->page);
		break;
	case WT_REF_READING:
		__dmsg(ds, "reading");
		break;
	WT_ILLEGAL_VALUE(session);
	}

	if (ref->addr == NULL)
		__dmsg(ds, " %s\n", "[NoAddr]");
	else {
		__wt_get_addr(page, ref, &addr, &size);
		__dmsg(ds,
		    " %s\n", __wt_addr_string(session, ds->tmp, addr, size));
	}
	return (0);
}

/*
 * __debug_cell --
 *	Dump a single unpacked WT_CELL.
 */
static int
__debug_cell(WT_DBG *ds, WT_PAGE_HEADER *dsk, WT_CELL_UNPACK *unpack)
{
	WT_ITEM *buf;
	WT_SESSION_IMPL *session;
	int ret;

	session = ds->session;
	buf = NULL;

	__dmsg(ds, "\t%s: len %" PRIu32,
	    __wt_cell_type_string(unpack->raw), unpack->size);

	switch (unpack->type) {
	case WT_CELL_DEL:
	case WT_CELL_VALUE:
		/*
		 * Column-store internal page value cells include a record
		 * number, column-store leaf page value cells include a RLE;
		 * row-store leaf page value cells have no associated value.
		 */
		if (unpack->v != 0)
			__dmsg(ds, ", %s: %" PRIu64,
			    dsk->type == WT_PAGE_COL_INT ? "recno" : "rle",
			    unpack->v);
		break;
	case WT_CELL_KEY:
		__dmsg(ds, ", pfx: %" PRIu8, unpack->prefix);
		break;
	case WT_CELL_ADDR:
	case WT_CELL_KEY_OVFL:
	case WT_CELL_VALUE_OVFL:
		WT_RET(__wt_scr_alloc(session, 64, &buf));
		if ((ret = __wt_bm_addr_string(
		    session, buf, unpack->data, unpack->size)) == 0)
			__dmsg(ds, ", %s %s",
			    unpack->type == WT_CELL_ADDR ? "addr" : "ovfl",
			    (char *)buf->data);
		__wt_scr_free(&buf);
		WT_RET(ret);
		break;
	WT_ILLEGAL_VALUE(session);
	}
	__dmsg(ds, "\n");

	return (__debug_cell_data(ds, NULL, unpack));
}

/*
 * __debug_cell_data --
 *	Dump a single cell's data in debugging mode.
 */
static int
__debug_cell_data(WT_DBG *ds, const char *tag, WT_CELL_UNPACK *unpack)
{
	WT_ITEM *buf;
	WT_SESSION_IMPL *session;
	int ret;

	session = ds->session;
	buf = NULL;
	ret = 0;

	/*
	 * Column-store references to deleted cells return a NULL cell
	 * reference.
	 */
	if (unpack == NULL)
		goto deleted;

	switch (unpack->type) {
	case WT_CELL_ADDR:
		__debug_item(ds, tag, "addr", strlen("addr"));
		break;
	case WT_CELL_DEL:
deleted:	__debug_item(ds, tag, "deleted", strlen("deleted"));
		break;
	case WT_CELL_KEY:
	case WT_CELL_KEY_OVFL:
	case WT_CELL_VALUE:
	case WT_CELL_VALUE_OVFL:
		WT_RET(__wt_scr_alloc(session, 256, &buf));
		if ((ret = __wt_cell_unpack_copy(session, unpack, buf)) == 0)
			__debug_item(ds, tag, buf->data, buf->size);
		__wt_scr_free(&buf);
		break;
	WT_ILLEGAL_VALUE(session);
	}

	return (ret);
}

/*
 * __debug_ikey --
 *	Dump a single WT_IKEY in debugging mode, with an optional tag.
 */
static void
__debug_ikey(WT_DBG *ds, WT_IKEY *ikey)
{
	__debug_item(ds, "K", WT_IKEY_DATA(ikey), ikey->size);
}

/*
 * __debug_item --
 *	Dump a single data/size pair, with an optional tag.
 */
static void
__debug_item(WT_DBG *ds, const char *tag, const void *data_arg, size_t size)
{
	const uint8_t *data;
	int ch;

	__dmsg(ds, "\t%s%s{", tag == NULL ? "" : tag, tag == NULL ? "" : " ");
	for (data = data_arg; size > 0; --size, ++data) {
		ch = data[0];
		if (isprint(ch))
			__dmsg(ds, "%c", ch);
		else
			__debug_hex_byte(ds, data[0]);
	}
	__dmsg(ds, "}\n");
}
#endif