2 * Copyright (c) 1992-1994 by Xerox Corporation. All rights reserved.
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
14 #include "private/gc_priv.h"
18 /* This is debugging code intended to verify the results of dirty bit */
19 /* computations. Works only in a single threaded environment. */
22 #define OFFSET 0x10000
28 struct hblk * block; /* Block to which this refers + OFFSET */
29 /* to hide it from collector. */
32 page_entry GC_sums[NSUMS];
34 STATIC word GC_faulted[NSUMS] = { 0 };
35 /* Record of pages on which we saw a write fault. */
37 STATIC size_t GC_n_faulted = 0;
39 #if defined(MPROTECT_VDB) && !defined(DARWIN)
40 void GC_record_fault(struct hblk * h)
42 word page = (word)h & ~(GC_page_size - 1);
44 GC_ASSERT(GC_page_size != 0);
45 if (GC_n_faulted >= NSUMS) ABORT("write fault log overflowed");
46 GC_faulted[GC_n_faulted++] = page;
50 STATIC GC_bool GC_was_faulted(struct hblk *h)
53 word page = (word)h & ~(GC_page_size - 1);
55 for (i = 0; i < GC_n_faulted; ++i) {
56 if (GC_faulted[i] == page) return TRUE;
61 STATIC word GC_checksum(struct hblk *h)
64 word *lim = (word *)(h+1);
67 while ((word)p < (word)lim) {
70 return(result | 0x80000000 /* doesn't look like pointer */);
73 int GC_n_dirty_errors = 0;
74 int GC_n_faulted_dirty_errors = 0;
75 unsigned long GC_n_clean = 0;
76 unsigned long GC_n_dirty = 0;
78 STATIC void GC_update_check_page(struct hblk *h, int index)
80 page_entry *pe = GC_sums + index;
84 if (pe -> block != 0 && pe -> block != h + OFFSET) ABORT("goofed");
85 pe -> old_sum = pe -> new_sum;
86 pe -> new_sum = GC_checksum(h);
87 # if !defined(MSWIN32) && !defined(MSWINCE)
88 if (pe -> new_sum != 0x80000000 && !GC_page_was_ever_dirty(h)) {
89 GC_err_printf("GC_page_was_ever_dirty(%p) is wrong\n", (void *)h);
92 if (GC_page_was_dirty(h)) {
98 while (IS_FORWARDING_ADDR_OR_NIL(hhdr) && hhdr != 0) {
103 && hhdr != 0 && hhdr -> hb_descr != 0 /* may contain pointers */
104 && pe -> old_sum != pe -> new_sum) {
105 if (!GC_page_was_dirty(h) || !GC_page_was_ever_dirty(h)) {
106 GC_bool was_faulted = GC_was_faulted(h);
107 /* Set breakpoint here */GC_n_dirty_errors++;
108 if (was_faulted) GC_n_faulted_dirty_errors++;
111 pe -> new_valid = TRUE;
112 pe -> block = h + OFFSET;
115 word GC_bytes_in_used_blocks = 0;
117 STATIC void GC_add_block(struct hblk *h, word dummy GC_ATTR_UNUSED)
121 GC_bytes_in_used_blocks += (hhdr->hb_sz + HBLKSIZE-1) & ~(HBLKSIZE-1);
124 STATIC void GC_check_blocks(void)
126 word bytes_in_free_blocks = GC_large_free_bytes;
128 GC_bytes_in_used_blocks = 0;
129 GC_apply_to_all_blocks(GC_add_block, (word)0);
130 GC_COND_LOG_PRINTF("GC_bytes_in_used_blocks = %lu,"
131 " bytes_in_free_blocks = %lu, heapsize = %lu\n",
132 (unsigned long)GC_bytes_in_used_blocks,
133 (unsigned long)bytes_in_free_blocks,
134 (unsigned long)GC_heapsize);
135 if (GC_bytes_in_used_blocks + bytes_in_free_blocks != GC_heapsize) {
136 GC_err_printf("LOST SOME BLOCKS!!\n");
140 /* Should be called immediately after GC_read_dirty. */
141 void GC_check_dirty(void)
150 GC_n_dirty_errors = 0;
151 GC_n_faulted_dirty_errors = 0;
156 for (i = 0; i < GC_n_heap_sects; i++) {
157 start = GC_heap_sects[i].hs_start;
158 for (h = (struct hblk *)start;
159 (word)h < (word)(start + GC_heap_sects[i].hs_bytes); h++) {
160 GC_update_check_page(h, index);
162 if (index >= NSUMS) goto out;
166 GC_COND_LOG_PRINTF("Checked %lu clean and %lu dirty pages\n",
167 GC_n_clean, GC_n_dirty);
168 if (GC_n_dirty_errors > 0) {
169 GC_err_printf("Found %d dirty bit errors (%d were faulted)\n",
170 GC_n_dirty_errors, GC_n_faulted_dirty_errors);
172 for (i = 0; i < GC_n_faulted; ++i) {
173 GC_faulted[i] = 0; /* Don't expose block pointers to GC */
178 #endif /* CHECKSUMS */