2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1997 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
6 * Copyright (C) 2007 Free Software Foundation, Inc
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
11 * Permission is hereby granted to use or copy this program
12 * for any purpose, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
18 #include "private/dbg_mlc.h"
25 GC_INNER void GC_default_print_heap_obj_proc(ptr_t p);
27 GC_API void GC_CALL GC_register_finalizer_no_order(void * obj,
28 GC_finalization_proc fn, void * cd,
29 GC_finalization_proc *ofn, void * *ocd);
31 #ifndef SHORT_DBG_HDRS
32 /* Check whether object with base pointer p has debugging info */
33 /* p is assumed to point to a legitimate object in our part */
35 /* This excludes the check as to whether the back pointer is */
36 /* odd, which is added by the GC_HAS_DEBUG_INFO macro. */
37 /* Note that if DBG_HDRS_ALL is set, uncollectable objects */
38 /* on free lists may not have debug information set. Thus it's */
39 /* not always safe to return TRUE, even if the client does */
41 GC_INNER GC_bool GC_has_other_debug_info(ptr_t p)
44 ptr_t body = (ptr_t)(ohdr + 1);
45 word sz = GC_size((ptr_t) ohdr);
47 if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
48 || sz < DEBUG_BYTES + EXTRA_BYTES) {
51 if (ohdr -> oh_sz == sz) {
52 /* Object may have had debug info, but has been deallocated */
55 if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
56 if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
67 # if defined(__GLIBC__) || defined(SOLARIS) \
68 || defined(HPUX) || defined(IRIX5) || defined(OSF1)
69 # define RANDOM() random()
71 # define RANDOM() (long)rand()
74 /* Store back pointer to source in dest, if that appears to be possible. */
75 /* This is not completely safe, since we may mistakenly conclude that */
76 /* dest has a debugging wrapper. But the error probability is very */
77 /* small, and this shouldn't be used in production code. */
78 /* We assume that dest is the real base pointer. Source will usually */
79 /* be a pointer to the interior of an object. */
80 GC_INNER void GC_store_back_pointer(ptr_t source, ptr_t dest)
82 if (GC_HAS_DEBUG_INFO(dest)) {
83 ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
87 GC_INNER void GC_marked_for_finalization(ptr_t dest)
89 GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
92 /* Store information about the object referencing dest in *base_p */
94 /* source is root ==> *base_p = address, *offset_p = 0 */
95 /* source is heap object ==> *base_p != 0, *offset_p = offset */
96 /* Returns 1 on success, 0 if source couldn't be determined. */
97 /* Dest can be any address within a heap object. */
98 GC_API GC_ref_kind GC_CALL GC_get_back_ptr_info(void *dest, void **base_p,
101 oh * hdr = (oh *)GC_base(dest);
104 if (!GC_HAS_DEBUG_INFO((ptr_t) hdr)) return GC_NO_SPACE;
105 bp = GC_REVEAL_POINTER(hdr -> oh_back_ptr);
106 if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
107 if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
108 if (NOT_MARKED == bp) return GC_UNREFERENCED;
110 /* Heuristically try to fix off by 1 errors we introduced by */
111 /* insisting on even addresses. */
113 ptr_t alternate_ptr = bp + 1;
114 ptr_t target = *(ptr_t *)bp;
115 ptr_t alternate_target = *(ptr_t *)alternate_ptr;
117 if (alternate_target >= GC_least_plausible_heap_addr
118 && alternate_target <= GC_greatest_plausible_heap_addr
119 && (target < GC_least_plausible_heap_addr
120 || target > GC_greatest_plausible_heap_addr)) {
125 bp_base = GC_base(bp);
129 return GC_REFD_FROM_ROOT;
131 if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
133 *offset_p = bp - bp_base;
134 return GC_REFD_FROM_HEAP;
138 /* Generate a random heap address. */
139 /* The resulting address is in the heap, but */
140 /* not necessarily inside a valid object. */
141 GC_API void * GC_CALL GC_generate_random_heap_address(void)
144 word heap_offset = RANDOM();
145 if (GC_heapsize > RAND_MAX) {
146 heap_offset *= RAND_MAX;
147 heap_offset += RANDOM();
149 heap_offset %= GC_heapsize;
150 /* This doesn't yield a uniform distribution, especially if */
151 /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
152 /* it's not too bad. */
153 for (i = 0; i < GC_n_heap_sects; ++ i) {
154 size_t size = GC_heap_sects[i].hs_bytes;
155 if (heap_offset < size) {
156 return GC_heap_sects[i].hs_start + heap_offset;
161 ABORT("GC_generate_random_heap_address: size inconsistency");
166 /* Generate a random address inside a valid marked heap object. */
167 GC_API void * GC_CALL GC_generate_random_valid_address(void)
172 result = GC_generate_random_heap_address();
173 base = GC_base(result);
174 if (0 == base) continue;
175 if (!GC_is_marked(base)) continue;
180 /* Print back trace for p */
181 GC_API void GC_CALL GC_print_backtrace(void *p)
189 GC_print_heap_obj(GC_base(current));
192 source = GC_get_back_ptr_info(current, &base, &offset);
193 if (GC_UNREFERENCED == source) {
194 GC_err_printf("Reference could not be found\n");
197 if (GC_NO_SPACE == source) {
198 GC_err_printf("No debug info in object: Can't find reference\n");
201 GC_err_printf("Reachable via %d levels of pointers from ", i);
203 case GC_REFD_FROM_ROOT:
204 GC_err_printf("root at %p\n\n", base);
206 case GC_REFD_FROM_REG:
207 GC_err_printf("root in register\n\n");
209 case GC_FINALIZER_REFD:
210 GC_err_printf("list of finalizable objects\n\n");
212 case GC_REFD_FROM_HEAP:
213 GC_err_printf("offset %ld in object:\n", (unsigned long)offset);
214 /* Take GC_base(base) to get real base, i.e. header. */
215 GC_print_heap_obj(GC_base(base));
219 GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
227 /* Force a garbage collection and generate a backtrace from a */
228 /* random heap address. */
229 GC_INNER void GC_generate_random_backtrace_no_gc(void)
232 current = GC_generate_random_valid_address();
233 GC_printf("\n****Chose address %p in object\n", current);
234 GC_print_backtrace(current);
237 GC_API void GC_CALL GC_generate_random_backtrace(void)
239 if (GC_try_to_collect(GC_never_stop_func) == 0) {
240 GC_err_printf("Cannot generate a backtrace: "
241 "garbage collection is disabled!\n");
244 GC_generate_random_backtrace_no_gc();
247 #endif /* KEEP_BACK_PTRS */
249 # define CROSSES_HBLK(p, sz) \
250 (((word)(p + sizeof(oh) + sz - 1) ^ (word)p) >= HBLKSIZE)
251 /* Store debugging info into p. Return displaced pointer. */
252 /* Assumes we don't hold allocation lock. */
253 GC_INNER ptr_t GC_store_debug_info(ptr_t p, word sz, const char *string,
256 word * result = (word *)((oh *)p + 1);
260 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
261 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
262 # ifdef KEEP_BACK_PTRS
263 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
265 # ifdef MAKE_BACK_GRAPH
266 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
268 ((oh *)p) -> oh_string = string;
269 ((oh *)p) -> oh_int = integer;
270 # ifndef SHORT_DBG_HDRS
271 ((oh *)p) -> oh_sz = sz;
272 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
273 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
274 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
277 return((ptr_t)result);
281 /* Store debugging info into p. Return displaced pointer. */
282 /* This version assumes we do hold the allocation lock. */
283 STATIC ptr_t GC_store_debug_info_inner(ptr_t p, word sz, char *string,
286 word * result = (word *)((oh *)p + 1);
288 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
289 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
290 # ifdef KEEP_BACK_PTRS
291 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
293 # ifdef MAKE_BACK_GRAPH
294 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
296 ((oh *)p) -> oh_string = string;
297 ((oh *)p) -> oh_int = integer;
298 # ifndef SHORT_DBG_HDRS
299 ((oh *)p) -> oh_sz = sz;
300 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
301 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
302 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
304 return((ptr_t)result);
308 #ifndef SHORT_DBG_HDRS
309 /* Check the object with debugging info at ohdr */
310 /* return NIL if it's OK. Else return clobbered */
312 STATIC ptr_t GC_check_annotated_obj(oh *ohdr)
314 ptr_t body = (ptr_t)(ohdr + 1);
315 word gc_sz = GC_size((ptr_t)ohdr);
316 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
317 return((ptr_t)(&(ohdr -> oh_sz)));
319 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
320 return((ptr_t)(&(ohdr -> oh_sf)));
322 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
323 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
325 if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
326 != (END_FLAG ^ (word)body)) {
327 return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr->oh_sz)));
331 #endif /* !SHORT_DBG_HDRS */
333 STATIC GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
335 GC_API void GC_CALL GC_register_describe_type_fn(int kind,
336 GC_describe_type_fn fn)
338 GC_describe_type_fns[kind] = fn;
341 /* Print a type description for the object whose client-visible address */
343 STATIC void GC_print_type(ptr_t p)
345 hdr * hhdr = GC_find_header(p);
346 char buffer[GC_TYPE_DESCR_LEN + 1];
347 int kind = hhdr -> hb_obj_kind;
349 if (0 != GC_describe_type_fns[kind] && GC_is_marked(GC_base(p))) {
350 /* This should preclude free list objects except with */
351 /* thread-local allocation. */
352 buffer[GC_TYPE_DESCR_LEN] = 0;
353 (GC_describe_type_fns[kind])(p, buffer);
354 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
359 GC_err_puts("PTRFREE");
362 GC_err_puts("NORMAL");
365 GC_err_puts("UNCOLLECTABLE");
367 # ifdef ATOMIC_UNCOLLECTABLE
369 GC_err_puts("ATOMIC UNCOLLECTABLE");
373 GC_err_puts("STUBBORN");
376 GC_err_printf("kind %d, descr 0x%lx", kind,
377 (unsigned long)(hhdr -> hb_descr));
382 /* Print a human-readable description of the object to stderr. p points */
383 /* to somewhere inside an object with the debugging info. */
384 STATIC void GC_print_obj(ptr_t p)
386 oh * ohdr = (oh *)GC_base(p);
388 GC_ASSERT(I_DONT_HOLD_LOCK());
389 GC_err_printf("%p (", ((ptr_t)ohdr + sizeof(oh)));
390 GC_err_puts(ohdr -> oh_string);
391 # ifdef SHORT_DBG_HDRS
392 GC_err_printf(":%ld, ", (unsigned long)(ohdr -> oh_int));
394 GC_err_printf(":%ld, sz=%ld, ", (unsigned long)(ohdr -> oh_int),
395 (unsigned long)(ohdr -> oh_sz));
397 GC_print_type((ptr_t)(ohdr + 1));
399 PRINT_CALL_CHAIN(ohdr);
402 STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
404 GC_ASSERT(I_DONT_HOLD_LOCK());
405 if (GC_HAS_DEBUG_INFO(p)) {
408 GC_default_print_heap_obj_proc(p);
412 #ifndef SHORT_DBG_HDRS
413 /* Use GC_err_printf and friends to print a description of the object */
414 /* whose client-visible address is p, and which was smashed at */
415 /* clobbered_addr. */
416 STATIC void GC_print_smashed_obj(ptr_t p, ptr_t clobbered_addr)
418 oh * ohdr = (oh *)GC_base(p);
420 GC_ASSERT(I_DONT_HOLD_LOCK());
421 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
422 || ohdr -> oh_string == 0) {
424 "%p in or near object at %p(<smashed>, appr. sz = %lu)\n",
426 (unsigned long)(GC_size((ptr_t)ohdr) - DEBUG_BYTES));
428 GC_err_printf("%p in or near object at %p(%s:%lu, sz=%lu)\n",
430 (word)(ohdr -> oh_string) < HBLKSIZE ? "(smashed string)" :
431 ohdr -> oh_string[0] == '\0' ? "EMPTY(smashed?)" :
433 (unsigned long)(ohdr -> oh_int),
434 (unsigned long)(ohdr -> oh_sz));
435 PRINT_CALL_CHAIN(ohdr);
440 #ifndef SHORT_DBG_HDRS
441 STATIC void GC_check_heap_proc (void);
442 STATIC void GC_print_all_smashed_proc (void);
444 STATIC void GC_do_nothing(void) {}
447 GC_INNER void GC_start_debugging(void)
449 # ifndef SHORT_DBG_HDRS
450 GC_check_heap = GC_check_heap_proc;
451 GC_print_all_smashed = GC_print_all_smashed_proc;
453 GC_check_heap = GC_do_nothing;
454 GC_print_all_smashed = GC_do_nothing;
456 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
457 GC_debugging_started = TRUE;
458 GC_register_displacement((word)sizeof(oh));
461 size_t GC_debug_header_size = sizeof(oh);
463 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
465 GC_register_displacement(offset);
466 GC_register_displacement((word)sizeof(oh) + offset);
469 GC_API void * GC_CALL GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS)
471 void * result = GC_malloc(lb + DEBUG_BYTES);
474 GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
477 GC_err_printf(":%ld)\n", (unsigned long)i);
480 if (!GC_debugging_started) {
481 GC_start_debugging();
483 ADD_CALL_CHAIN(result, ra);
484 return (GC_store_debug_info(result, (word)lb, s, (word)i));
487 GC_API void * GC_CALL GC_debug_malloc_ignore_off_page(size_t lb,
490 void * result = GC_malloc_ignore_off_page(lb + DEBUG_BYTES);
493 GC_err_printf("GC_debug_malloc_ignore_off_page(%lu) returning NIL (",
496 GC_err_printf(":%lu)\n", (unsigned long)i);
499 if (!GC_debugging_started) {
500 GC_start_debugging();
502 ADD_CALL_CHAIN(result, ra);
503 return (GC_store_debug_info(result, (word)lb, s, (word)i));
506 GC_API void * GC_CALL GC_debug_malloc_atomic_ignore_off_page(size_t lb,
509 void * result = GC_malloc_atomic_ignore_off_page(lb + DEBUG_BYTES);
512 GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
513 " returning NIL (", (unsigned long) lb);
515 GC_err_printf(":%lu)\n", (unsigned long)i);
518 if (!GC_debugging_started) {
519 GC_start_debugging();
521 ADD_CALL_CHAIN(result, ra);
522 return (GC_store_debug_info(result, (word)lb, s, (word)i));
527 * An allocation function for internal use.
528 * Normally internally allocated objects do not have debug information.
529 * But in this case, we need to make sure that all objects have debug
531 * We assume debugging was started in collector initialization,
532 * and we already hold the GC lock.
534 GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
536 void * result = GC_generic_malloc_inner(lb + DEBUG_BYTES, k);
539 GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
543 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
544 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
547 GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
550 void * result = GC_generic_malloc_inner_ignore_off_page(
551 lb + DEBUG_BYTES, k);
554 GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
558 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
559 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
563 #ifdef STUBBORN_ALLOC
564 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
566 void * result = GC_malloc_stubborn(lb + DEBUG_BYTES);
569 GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
572 GC_err_printf(":%lu)\n", (unsigned long)i);
575 if (!GC_debugging_started) {
576 GC_start_debugging();
578 ADD_CALL_CHAIN(result, ra);
579 return (GC_store_debug_info(result, (word)lb, s, (word)i));
582 GC_API void GC_CALL GC_debug_change_stubborn(void *p)
584 void * q = GC_base(p);
588 GC_err_printf("Bad argument: %p to GC_debug_change_stubborn\n", p);
589 ABORT("GC_debug_change_stubborn: bad arg");
592 if (hhdr -> hb_obj_kind != STUBBORN) {
593 GC_err_printf("GC_debug_change_stubborn arg not stubborn: %p\n", p);
594 ABORT("GC_debug_change_stubborn: arg not stubborn");
596 GC_change_stubborn(q);
599 GC_API void GC_CALL GC_debug_end_stubborn_change(void *p)
601 void * q = GC_base(p);
605 GC_err_printf("Bad argument: %p to GC_debug_end_stubborn_change\n", p);
606 ABORT("GC_debug_end_stubborn_change: bad arg");
609 if (hhdr -> hb_obj_kind != STUBBORN) {
610 GC_err_printf("debug_end_stubborn_change arg not stubborn: %p\n", p);
611 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
613 GC_end_stubborn_change(q);
616 #else /* !STUBBORN_ALLOC */
618 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
620 return GC_debug_malloc(lb, OPT_RA s, i);
624 GC_API void GC_CALL GC_debug_change_stubborn(void *p) {}
627 GC_API void GC_CALL GC_debug_end_stubborn_change(void *p) {}
629 #endif /* !STUBBORN_ALLOC */
631 GC_API void * GC_CALL GC_debug_malloc_atomic(size_t lb, GC_EXTRA_PARAMS)
633 void * result = GC_malloc_atomic(lb + DEBUG_BYTES);
636 GC_err_printf("GC_debug_malloc_atomic(%lu) returning NIL (",
639 GC_err_printf(":%lu)\n", (unsigned long)i);
642 if (!GC_debugging_started) {
643 GC_start_debugging();
645 ADD_CALL_CHAIN(result, ra);
646 return (GC_store_debug_info(result, (word)lb, s, (word)i));
649 GC_API char * GC_CALL GC_debug_strdup(const char *str, GC_EXTRA_PARAMS)
653 if (str == NULL) return NULL;
654 lb = strlen(str) + 1;
655 copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
665 /* strcpy() is deprecated in WinCE */
666 memcpy(copy, str, lb);
671 GC_API void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
674 void * result = GC_malloc_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
677 GC_err_printf("GC_debug_malloc_uncollectable(%lu) returning NIL (",
680 GC_err_printf(":%lu)\n", (unsigned long)i);
683 if (!GC_debugging_started) {
684 GC_start_debugging();
686 ADD_CALL_CHAIN(result, ra);
687 return (GC_store_debug_info(result, (word)lb, s, (word)i));
690 #ifdef ATOMIC_UNCOLLECTABLE
691 void * GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
694 GC_malloc_atomic_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
698 "GC_debug_malloc_atomic_uncollectable(%lu) returning NIL (",
701 GC_err_printf(":%lu)\n", (unsigned long)i);
704 if (!GC_debugging_started) {
705 GC_start_debugging();
707 ADD_CALL_CHAIN(result, ra);
708 return (GC_store_debug_info(result, (word)lb, s, (word)i));
710 #endif /* ATOMIC_UNCOLLECTABLE */
712 GC_API void GC_CALL GC_debug_free(void * p)
715 # ifndef SHORT_DBG_HDRS
722 GC_err_printf("Attempt to free invalid pointer %p\n", p);
723 ABORT("free(invalid pointer)");
725 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
727 "GC_debug_free called on pointer %p w/o debugging info\n", p);
729 # ifndef SHORT_DBG_HDRS
730 clobbered = GC_check_annotated_obj((oh *)base);
731 if (clobbered != 0) {
732 if (((oh *)base) -> oh_sz == GC_size(base)) {
734 "GC_debug_free: found previously deallocated (?) object at ");
736 GC_err_printf("GC_debug_free: found smashed location at ");
738 GC_print_smashed_obj(p, clobbered);
740 /* Invalidate size */
741 ((oh *)base) -> oh_sz = GC_size(base);
742 # endif /* SHORT_DBG_HDRS */
748 GC_bool uncollectable = FALSE;
750 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
751 uncollectable = TRUE;
753 # ifdef ATOMIC_UNCOLLECTABLE
754 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
755 uncollectable = TRUE;
762 size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
764 for (i = 0; i < obj_sz; ++i) ((word *)p)[i] = 0xdeadbeef;
765 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
767 } /* !GC_find_leak */
770 #if defined(THREADS) && defined(DBG_HDRS_ALL)
771 /* Used internally; we assume it's called correctly. */
772 GC_INNER void GC_debug_free_inner(void * p)
774 ptr_t base = GC_base(p);
775 GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
776 # ifndef SHORT_DBG_HDRS
777 /* Invalidate size */
778 ((oh *)base) -> oh_sz = GC_size(base);
784 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
787 # ifndef SHORT_DBG_HDRS
795 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
798 GC_err_printf("Attempt to reallocate invalid pointer %p\n", p);
799 ABORT("realloc(invalid pointer)");
801 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
803 "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
804 return(GC_realloc(p, lb));
807 switch (hhdr -> hb_obj_kind) {
808 # ifdef STUBBORN_ALLOC
810 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
814 result = GC_debug_malloc(lb, OPT_RA s, i);
817 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
820 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
822 # ifdef ATOMIC_UNCOLLECTABLE
824 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
828 result = NULL; /* initialized to prevent warning. */
829 GC_err_printf("GC_debug_realloc: encountered bad kind\n");
832 # ifdef SHORT_DBG_HDRS
833 old_sz = GC_size(base) - sizeof(oh);
835 clobbered = GC_check_annotated_obj((oh *)base);
836 if (clobbered != 0) {
837 GC_err_printf("GC_debug_realloc: found smashed location at ");
838 GC_print_smashed_obj(p, clobbered);
840 old_sz = ((oh *)base) -> oh_sz;
842 if (old_sz < copy_sz) copy_sz = old_sz;
843 if (result == 0) return(0);
844 BCOPY(p, result, copy_sz);
849 #ifndef SHORT_DBG_HDRS
851 /* List of smashed objects. We defer printing these, since we can't */
852 /* always print them nicely with the allocation lock held. */
853 /* We put them here instead of in GC_arrays, since it may be useful to */
854 /* be able to look at them with the debugger. */
855 #define MAX_SMASHED 20
856 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
857 STATIC unsigned GC_n_smashed = 0;
859 STATIC void GC_add_smashed(ptr_t smashed)
861 GC_ASSERT(GC_is_marked(GC_base(smashed)));
862 GC_smashed[GC_n_smashed] = smashed;
863 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
864 /* In case of overflow, we keep the first MAX_SMASHED-1 */
865 /* entries plus the last one. */
866 GC_have_errors = TRUE;
869 /* Print all objects on the list. Clear the list. */
870 STATIC void GC_print_all_smashed_proc(void)
874 GC_ASSERT(I_DONT_HOLD_LOCK());
875 if (GC_n_smashed == 0) return;
876 GC_err_printf("GC_check_heap_block: found smashed heap objects:\n");
877 for (i = 0; i < GC_n_smashed; ++i) {
878 GC_print_smashed_obj((ptr_t)GC_base(GC_smashed[i]) + sizeof(oh),
885 /* Check all marked objects in the given block for validity */
886 /* Avoid GC_apply_to_each_object for performance reasons. */
888 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy)
890 struct hblkhdr * hhdr = HDR(hbp);
891 size_t sz = hhdr -> hb_sz;
897 if (sz > MAXOBJBYTES) {
900 plim = hbp->hb_body + HBLKSIZE - sz;
902 /* go through all words in block */
904 if( mark_bit_from_hdr(hhdr, bit_no)
905 && GC_HAS_DEBUG_INFO((ptr_t)p)) {
906 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
908 if (clobbered != 0) GC_add_smashed(clobbered);
910 bit_no += MARK_BIT_OFFSET(sz);
915 /* This assumes that all accessible objects are marked, and that */
916 /* I hold the allocation lock. Normally called by collector. */
917 STATIC void GC_check_heap_proc(void)
919 # ifndef SMALL_CONFIG
920 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
921 /* FIXME: Should we check for twice that alignment? */
923 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
926 #endif /* !SHORT_DBG_HDRS */
929 GC_finalization_proc cl_fn;
933 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
935 struct closure * result =
937 (struct closure *) GC_debug_malloc(sizeof (struct closure),
940 (struct closure *) GC_malloc(sizeof (struct closure));
943 result -> cl_fn = fn;
944 result -> cl_data = data;
945 return((void *)result);
948 /* An auxiliary fns to make finalization work correctly with displaced */
949 /* pointers introduced by the debugging allocators. */
950 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
952 struct closure * cl = (struct closure *) data;
953 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
956 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
957 #define OFN_UNSET (GC_finalization_proc)(signed_word)-1
959 /* Set ofn and ocd to reflect the values we got back. */
960 static void store_old(void *obj, GC_finalization_proc my_old_fn,
961 struct closure *my_old_cd, GC_finalization_proc *ofn,
964 if (0 != my_old_fn) {
965 if (my_old_fn == OFN_UNSET) {
966 /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
969 if (my_old_fn != GC_debug_invoke_finalizer) {
970 GC_err_printf("Debuggable object at %p had non-debug finalizer.\n",
972 /* This should probably be fatal. */
974 if (ofn) *ofn = my_old_cd -> cl_fn;
975 if (ocd) *ocd = my_old_cd -> cl_data;
983 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
984 GC_finalization_proc fn,
985 void * cd, GC_finalization_proc *ofn,
988 GC_finalization_proc my_old_fn = OFN_UNSET;
990 ptr_t base = GC_base(obj);
992 /* We won't collect it, hence finalizer wouldn't be run. */
997 if ((ptr_t)obj - base != sizeof(oh)) {
999 "GC_debug_register_finalizer called with non-base-pointer %p\n",
1003 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1005 GC_register_finalizer(base, GC_debug_invoke_finalizer,
1006 GC_make_closure(fn,cd), &my_old_fn, &my_old_cd);
1008 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1011 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1012 (void * obj, GC_finalization_proc fn,
1013 void * cd, GC_finalization_proc *ofn,
1016 GC_finalization_proc my_old_fn = OFN_UNSET;
1018 ptr_t base = GC_base(obj);
1020 /* We won't collect it, hence finalizer wouldn't be run. */
1025 if ((ptr_t)obj - base != sizeof(oh)) {
1027 "GC_debug_register_finalizer_no_order called with "
1028 "non-base-pointer %p\n",
1032 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1034 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1035 GC_make_closure(fn,cd), &my_old_fn,
1038 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1041 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1042 (void * obj, GC_finalization_proc fn,
1043 void * cd, GC_finalization_proc *ofn,
1046 GC_finalization_proc my_old_fn = OFN_UNSET;
1048 ptr_t base = GC_base(obj);
1050 /* We won't collect it, hence finalizer wouldn't be run. */
1055 if ((ptr_t)obj - base != sizeof(oh)) {
1057 "GC_debug_register_finalizer_unreachable called with "
1058 "non-base-pointer %p\n",
1062 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1064 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1065 GC_make_closure(fn,cd), &my_old_fn,
1068 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1071 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1072 (void * obj, GC_finalization_proc fn,
1073 void * cd, GC_finalization_proc *ofn,
1076 GC_finalization_proc my_old_fn = OFN_UNSET;
1078 ptr_t base = GC_base(obj);
1080 /* We won't collect it, hence finalizer wouldn't be run. */
1085 if ((ptr_t)obj - base != sizeof(oh)) {
1087 "GC_debug_register_finalizer_ignore_self called with "
1088 "non-base-pointer %p\n", obj);
1091 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1093 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1094 GC_make_closure(fn,cd), &my_old_fn,
1097 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1100 #ifdef GC_ADD_CALLER
1101 # define RA GC_RETURN_ADDR,
1106 GC_API void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1108 return GC_debug_malloc(lb, RA "unknown", 0);
1111 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1113 return GC_debug_realloc(p, lb, RA "unknown", 0);