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.
20 #include "private/dbg_mlc.h"
22 void GC_default_print_heap_obj_proc(ptr_t p);
23 GC_API void GC_CALL GC_register_finalizer_no_order
24 (void * obj, GC_finalization_proc fn, void * cd,
25 GC_finalization_proc *ofn, void * *ocd);
28 #ifndef SHORT_DBG_HDRS
29 /* Check whether object with base pointer p has debugging info */
30 /* p is assumed to point to a legitimate object in our part */
32 /* This excludes the check as to whether the back pointer is */
33 /* odd, which is added by the GC_HAS_DEBUG_INFO macro. */
34 /* Note that if DBG_HDRS_ALL is set, uncollectable objects */
35 /* on free lists may not have debug information set. Thus it's */
36 /* not always safe to return TRUE, even if the client does */
38 GC_bool GC_has_other_debug_info(ptr_t p)
40 register oh * ohdr = (oh *)p;
41 register ptr_t body = (ptr_t)(ohdr + 1);
42 register word sz = GC_size((ptr_t) ohdr);
44 if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
45 || sz < DEBUG_BYTES + EXTRA_BYTES) {
48 if (ohdr -> oh_sz == sz) {
49 /* Object may have had debug info, but has been deallocated */
52 if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
53 if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
64 # if defined(__GLIBC__) || defined(SOLARIS) \
65 || defined(HPUX) || defined(IRIX5) || defined(OSF1)
66 # define RANDOM() random()
68 # define RANDOM() (long)rand()
71 /* Store back pointer to source in dest, if that appears to be possible. */
72 /* This is not completely safe, since we may mistakenly conclude that */
73 /* dest has a debugging wrapper. But the error probability is very */
74 /* small, and this shouldn't be used in production code. */
75 /* We assume that dest is the real base pointer. Source will usually */
76 /* be a pointer to the interior of an object. */
77 void GC_store_back_pointer(ptr_t source, ptr_t dest)
79 if (GC_HAS_DEBUG_INFO(dest)) {
80 ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
84 void GC_marked_for_finalization(ptr_t dest) {
85 GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
88 /* Store information about the object referencing dest in *base_p */
90 /* source is root ==> *base_p = address, *offset_p = 0 */
91 /* source is heap object ==> *base_p != 0, *offset_p = offset */
92 /* Returns 1 on success, 0 if source couldn't be determined. */
93 /* Dest can be any address within a heap object. */
94 GC_ref_kind GC_get_back_ptr_info(void *dest, void **base_p, size_t *offset_p)
96 oh * hdr = (oh *)GC_base(dest);
99 if (!GC_HAS_DEBUG_INFO((ptr_t) hdr)) return GC_NO_SPACE;
100 bp = REVEAL_POINTER(hdr -> oh_back_ptr);
101 if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
102 if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
103 if (NOT_MARKED == bp) return GC_UNREFERENCED;
105 /* Heuristically try to fix off by 1 errors we introduced by */
106 /* insisting on even addresses. */
108 ptr_t alternate_ptr = bp + 1;
109 ptr_t target = *(ptr_t *)bp;
110 ptr_t alternate_target = *(ptr_t *)alternate_ptr;
112 if (alternate_target >= GC_least_plausible_heap_addr
113 && alternate_target <= GC_greatest_plausible_heap_addr
114 && (target < GC_least_plausible_heap_addr
115 || target > GC_greatest_plausible_heap_addr)) {
120 bp_base = GC_base(bp);
124 return GC_REFD_FROM_ROOT;
126 if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
128 *offset_p = bp - bp_base;
129 return GC_REFD_FROM_HEAP;
133 /* Generate a random heap address. */
134 /* The resulting address is in the heap, but */
135 /* not necessarily inside a valid object. */
136 void *GC_generate_random_heap_address(void)
139 word heap_offset = RANDOM();
140 if (GC_heapsize > RAND_MAX) {
141 heap_offset *= RAND_MAX;
142 heap_offset += RANDOM();
144 heap_offset %= GC_heapsize;
145 /* This doesn't yield a uniform distribution, especially if */
146 /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
147 /* it's not too bad. */
148 for (i = 0; i < GC_n_heap_sects; ++ i) {
149 size_t size = GC_heap_sects[i].hs_bytes;
150 if (heap_offset < size) {
151 return GC_heap_sects[i].hs_start + heap_offset;
156 ABORT("GC_generate_random_heap_address: size inconsistency");
161 /* Generate a random address inside a valid marked heap object. */
162 void *GC_generate_random_valid_address(void)
167 result = GC_generate_random_heap_address();
168 base = GC_base(result);
169 if (0 == base) continue;
170 if (!GC_is_marked(base)) continue;
175 /* Print back trace for p */
176 void GC_print_backtrace(void *p)
184 GC_print_heap_obj(GC_base(current));
187 source = GC_get_back_ptr_info(current, &base, &offset);
188 if (GC_UNREFERENCED == source) {
189 GC_err_printf("Reference could not be found\n");
192 if (GC_NO_SPACE == source) {
193 GC_err_printf("No debug info in object: Can't find reference\n");
196 GC_err_printf("Reachable via %d levels of pointers from ", i);
198 case GC_REFD_FROM_ROOT:
199 GC_err_printf("root at %p\n\n", base);
201 case GC_REFD_FROM_REG:
202 GC_err_printf("root in register\n\n");
204 case GC_FINALIZER_REFD:
205 GC_err_printf("list of finalizable objects\n\n");
207 case GC_REFD_FROM_HEAP:
208 GC_err_printf("offset %ld in object:\n", (unsigned long)offset);
209 /* Take GC_base(base) to get real base, i.e. header. */
210 GC_print_heap_obj(GC_base(base));
214 GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
222 /* Force a garbage collection and generate a backtrace from a */
223 /* random heap address. */
224 void GC_generate_random_backtrace_no_gc(void)
227 current = GC_generate_random_valid_address();
228 GC_printf("\n****Chose address %p in object\n", current);
229 GC_print_backtrace(current);
232 void GC_generate_random_backtrace(void)
235 GC_generate_random_backtrace_no_gc();
238 #endif /* KEEP_BACK_PTRS */
240 # define CROSSES_HBLK(p, sz) \
241 (((word)(p + sizeof(oh) + sz - 1) ^ (word)p) >= HBLKSIZE)
242 /* Store debugging info into p. Return displaced pointer. */
243 /* Assumes we don't hold allocation lock. */
244 ptr_t GC_store_debug_info(ptr_t p, word sz, const char *string, word integer)
246 register word * result = (word *)((oh *)p + 1);
250 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
251 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
252 # ifdef KEEP_BACK_PTRS
253 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
255 # ifdef MAKE_BACK_GRAPH
256 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
258 ((oh *)p) -> oh_string = string;
259 ((oh *)p) -> oh_int = integer;
260 # ifndef SHORT_DBG_HDRS
261 ((oh *)p) -> oh_sz = sz;
262 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
263 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
264 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
267 return((ptr_t)result);
271 /* Store debugging info into p. Return displaced pointer. */
272 /* This version assumes we do hold the allocation lock. */
273 STATIC ptr_t GC_store_debug_info_inner(ptr_t p, word sz, char *string,
276 register word * result = (word *)((oh *)p + 1);
278 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
279 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
280 # ifdef KEEP_BACK_PTRS
281 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
283 # ifdef MAKE_BACK_GRAPH
284 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
286 ((oh *)p) -> oh_string = string;
287 ((oh *)p) -> oh_int = integer;
288 # ifndef SHORT_DBG_HDRS
289 ((oh *)p) -> oh_sz = sz;
290 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
291 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
292 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
294 return((ptr_t)result);
298 #ifndef SHORT_DBG_HDRS
299 /* Check the object with debugging info at ohdr */
300 /* return NIL if it's OK. Else return clobbered */
302 STATIC ptr_t GC_check_annotated_obj(oh *ohdr)
304 register ptr_t body = (ptr_t)(ohdr + 1);
305 register word gc_sz = GC_size((ptr_t)ohdr);
306 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
307 return((ptr_t)(&(ohdr -> oh_sz)));
309 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
310 return((ptr_t)(&(ohdr -> oh_sf)));
312 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
313 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
315 if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
316 != (END_FLAG ^ (word)body)) {
317 return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)));
321 #endif /* !SHORT_DBG_HDRS */
323 static GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
325 void GC_CALL GC_register_describe_type_fn(int kind, GC_describe_type_fn fn)
327 GC_describe_type_fns[kind] = fn;
330 /* Print a type description for the object whose client-visible address */
332 STATIC void GC_print_type(ptr_t p)
334 hdr * hhdr = GC_find_header(p);
335 char buffer[GC_TYPE_DESCR_LEN + 1];
336 int kind = hhdr -> hb_obj_kind;
338 if (0 != GC_describe_type_fns[kind] && GC_is_marked(GC_base(p))) {
339 /* This should preclude free list objects except with */
340 /* thread-local allocation. */
341 buffer[GC_TYPE_DESCR_LEN] = 0;
342 (GC_describe_type_fns[kind])(p, buffer);
343 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
348 GC_err_puts("PTRFREE");
351 GC_err_puts("NORMAL");
354 GC_err_puts("UNCOLLECTABLE");
356 # ifdef ATOMIC_UNCOLLECTABLE
358 GC_err_puts("ATOMIC UNCOLLECTABLE");
362 GC_err_puts("STUBBORN");
365 GC_err_printf("kind %d, descr 0x%lx", kind,
366 (unsigned long)(hhdr -> hb_descr));
373 void GC_print_obj(ptr_t p)
375 register oh * ohdr = (oh *)GC_base(p);
377 GC_ASSERT(I_DONT_HOLD_LOCK());
378 GC_err_printf("%p (", ((ptr_t)ohdr + sizeof(oh)));
379 GC_err_puts(ohdr -> oh_string);
380 # ifdef SHORT_DBG_HDRS
381 GC_err_printf(":%ld, ", (unsigned long)(ohdr -> oh_int));
383 GC_err_printf(":%ld, sz=%ld, ", (unsigned long)(ohdr -> oh_int),
384 (unsigned long)(ohdr -> oh_sz));
386 GC_print_type((ptr_t)(ohdr + 1));
388 PRINT_CALL_CHAIN(ohdr);
391 STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
393 GC_ASSERT(I_DONT_HOLD_LOCK());
394 if (GC_HAS_DEBUG_INFO(p)) {
397 GC_default_print_heap_obj_proc(p);
401 #ifndef SHORT_DBG_HDRS
402 /* Use GC_err_printf and friends to print a description of the object */
403 /* whose client-visible address is p, and which was smashed at */
404 /* clobbered_addr. */
405 STATIC void GC_print_smashed_obj(ptr_t p, ptr_t clobbered_addr)
407 register oh * ohdr = (oh *)GC_base(p);
409 GC_ASSERT(I_DONT_HOLD_LOCK());
410 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
411 || ohdr -> oh_string == 0) {
413 "%p in or near object at %p(<smashed>, appr. sz = %lu)\n",
415 (unsigned long)(GC_size((ptr_t)ohdr) - DEBUG_BYTES));
417 GC_err_printf("%p in or near object at %p(%s:%lu, sz=%lu)\n",
419 (word)(ohdr -> oh_string) < HBLKSIZE ? "(smashed string)" :
420 ohdr -> oh_string[0] == '\0' ? "EMPTY(smashed?)" :
422 (unsigned long)(ohdr -> oh_int),
423 (unsigned long)(ohdr -> oh_sz));
424 PRINT_CALL_CHAIN(ohdr);
429 #ifndef SHORT_DBG_HDRS
430 STATIC void GC_check_heap_proc (void);
431 STATIC void GC_print_all_smashed_proc (void);
433 STATIC void GC_do_nothing(void) {}
436 void GC_start_debugging(void)
438 # ifndef SHORT_DBG_HDRS
439 GC_check_heap = GC_check_heap_proc;
440 GC_print_all_smashed = GC_print_all_smashed_proc;
442 GC_check_heap = GC_do_nothing;
443 GC_print_all_smashed = GC_do_nothing;
445 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
446 GC_debugging_started = TRUE;
447 GC_register_displacement((word)sizeof(oh));
450 size_t GC_debug_header_size = sizeof(oh);
452 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
454 GC_register_displacement(offset);
455 GC_register_displacement((word)sizeof(oh) + offset);
458 GC_API void * GC_CALL GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS)
460 void * result = GC_malloc(lb + DEBUG_BYTES);
463 GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
466 GC_err_printf(":%ld)\n", (unsigned long)i);
469 if (!GC_debugging_started) {
470 GC_start_debugging();
472 ADD_CALL_CHAIN(result, ra);
473 return (GC_store_debug_info(result, (word)lb, s, (word)i));
476 GC_API void * GC_CALL GC_debug_malloc_ignore_off_page(size_t lb,
479 void * result = GC_malloc_ignore_off_page(lb + DEBUG_BYTES);
482 GC_err_printf("GC_debug_malloc_ignore_off_page(%lu) returning NIL (",
485 GC_err_printf(":%lu)\n", (unsigned long)i);
488 if (!GC_debugging_started) {
489 GC_start_debugging();
491 ADD_CALL_CHAIN(result, ra);
492 return (GC_store_debug_info(result, (word)lb, s, (word)i));
495 GC_API void * GC_CALL GC_debug_malloc_atomic_ignore_off_page(size_t lb,
498 void * result = GC_malloc_atomic_ignore_off_page(lb + DEBUG_BYTES);
501 GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
502 " returning NIL (", (unsigned long) lb);
504 GC_err_printf(":%lu)\n", (unsigned long)i);
507 if (!GC_debugging_started) {
508 GC_start_debugging();
510 ADD_CALL_CHAIN(result, ra);
511 return (GC_store_debug_info(result, (word)lb, s, (word)i));
516 * An allocation function for internal use.
517 * Normally internally allocated objects do not have debug information.
518 * But in this case, we need to make sure that all objects have debug
520 * We assume debugging was started in collector initialization,
521 * and we already hold the GC lock.
523 void * GC_debug_generic_malloc_inner(size_t lb, int k)
525 void * result = GC_generic_malloc_inner(lb + DEBUG_BYTES, k);
528 GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
532 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
533 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
536 void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb, int k)
538 void * result = GC_generic_malloc_inner_ignore_off_page(
539 lb + DEBUG_BYTES, k);
542 GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
546 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
547 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
551 #ifdef STUBBORN_ALLOC
552 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
554 void * result = GC_malloc_stubborn(lb + DEBUG_BYTES);
557 GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
560 GC_err_printf(":%lu)\n", (unsigned long)i);
563 if (!GC_debugging_started) {
564 GC_start_debugging();
566 ADD_CALL_CHAIN(result, ra);
567 return (GC_store_debug_info(result, (word)lb, s, (word)i));
570 GC_API void GC_CALL GC_debug_change_stubborn(void *p)
572 void * q = GC_base(p);
576 GC_err_printf("Bad argument: %p to GC_debug_change_stubborn\n", p);
577 ABORT("GC_debug_change_stubborn: bad arg");
580 if (hhdr -> hb_obj_kind != STUBBORN) {
581 GC_err_printf("GC_debug_change_stubborn arg not stubborn: %p\n", p);
582 ABORT("GC_debug_change_stubborn: arg not stubborn");
584 GC_change_stubborn(q);
587 GC_API void GC_CALL GC_debug_end_stubborn_change(void *p)
589 register void * q = GC_base(p);
593 GC_err_printf("Bad argument: %p to GC_debug_end_stubborn_change\n", p);
594 ABORT("GC_debug_end_stubborn_change: bad arg");
597 if (hhdr -> hb_obj_kind != STUBBORN) {
598 GC_err_printf("debug_end_stubborn_change arg not stubborn: %p\n", p);
599 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
601 GC_end_stubborn_change(q);
604 #else /* !STUBBORN_ALLOC */
606 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
608 return GC_debug_malloc(lb, OPT_RA s, i);
612 GC_API void GC_CALL GC_debug_change_stubborn(void *p)
617 GC_API void GC_CALL GC_debug_end_stubborn_change(void *p)
621 #endif /* !STUBBORN_ALLOC */
623 GC_API void * GC_CALL GC_debug_malloc_atomic(size_t lb, GC_EXTRA_PARAMS)
625 void * result = GC_malloc_atomic(lb + DEBUG_BYTES);
628 GC_err_printf("GC_debug_malloc_atomic(%lu) returning NIL (",
631 GC_err_printf(":%lu)\n", (unsigned long)i);
634 if (!GC_debugging_started) {
635 GC_start_debugging();
637 ADD_CALL_CHAIN(result, ra);
638 return (GC_store_debug_info(result, (word)lb, s, (word)i));
641 GC_API char * GC_CALL GC_debug_strdup(const char *str, GC_EXTRA_PARAMS)
644 if (str == NULL) return NULL;
645 copy = GC_debug_malloc_atomic(strlen(str) + 1, OPT_RA s, i);
654 GC_API void * GC_CALL GC_debug_malloc_uncollectable(size_t lb, GC_EXTRA_PARAMS)
656 void * result = GC_malloc_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
659 GC_err_printf("GC_debug_malloc_uncollectable(%lu) returning NIL (",
662 GC_err_printf(":%lu)\n", (unsigned long)i);
665 if (!GC_debugging_started) {
666 GC_start_debugging();
668 ADD_CALL_CHAIN(result, ra);
669 return (GC_store_debug_info(result, (word)lb, s, (word)i));
672 #ifdef ATOMIC_UNCOLLECTABLE
673 void * GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
676 GC_malloc_atomic_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
680 "GC_debug_malloc_atomic_uncollectable(%lu) returning NIL (",
683 GC_err_printf(":%lu)\n", (unsigned long)i);
686 if (!GC_debugging_started) {
687 GC_start_debugging();
689 ADD_CALL_CHAIN(result, ra);
690 return (GC_store_debug_info(result, (word)lb, s, (word)i));
692 #endif /* ATOMIC_UNCOLLECTABLE */
694 GC_API void GC_CALL GC_debug_free(void * p)
697 # ifndef SHORT_DBG_HDRS
704 GC_err_printf("Attempt to free invalid pointer %p\n", p);
705 ABORT("free(invalid pointer)");
707 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
709 "GC_debug_free called on pointer %p w/o debugging info\n", p);
711 # ifndef SHORT_DBG_HDRS
712 clobbered = GC_check_annotated_obj((oh *)base);
713 if (clobbered != 0) {
714 if (((oh *)base) -> oh_sz == GC_size(base)) {
716 "GC_debug_free: found previously deallocated (?) object at ");
718 GC_err_printf("GC_debug_free: found smashed location at ");
720 GC_print_smashed_obj(p, clobbered);
722 /* Invalidate size */
723 ((oh *)base) -> oh_sz = GC_size(base);
724 # endif /* SHORT_DBG_HDRS */
730 GC_bool uncollectable = FALSE;
732 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
733 uncollectable = TRUE;
735 # ifdef ATOMIC_UNCOLLECTABLE
736 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
737 uncollectable = TRUE;
744 size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
746 for (i = 0; i < obj_sz; ++i) ((word *)p)[i] = 0xdeadbeef;
747 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
749 } /* !GC_find_leak */
754 extern void GC_free_inner(void * p);
756 /* Used internally; we assume it's called correctly. */
757 void GC_debug_free_inner(void * p)
759 ptr_t base = GC_base(p);
760 GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
761 # ifndef SHORT_DBG_HDRS
762 /* Invalidate size */
763 ((oh *)base) -> oh_sz = GC_size(base);
769 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
772 # ifndef SHORT_DBG_HDRS
780 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
783 GC_err_printf("Attempt to reallocate invalid pointer %p\n", p);
784 ABORT("realloc(invalid pointer)");
786 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
788 "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
789 return(GC_realloc(p, lb));
792 switch (hhdr -> hb_obj_kind) {
793 # ifdef STUBBORN_ALLOC
795 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
799 result = GC_debug_malloc(lb, OPT_RA s, i);
802 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
805 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
807 # ifdef ATOMIC_UNCOLLECTABLE
809 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
813 result = NULL; /* initialized to prevent warning. */
814 GC_err_printf("GC_debug_realloc: encountered bad kind\n");
817 # ifdef SHORT_DBG_HDRS
818 old_sz = GC_size(base) - sizeof(oh);
820 clobbered = GC_check_annotated_obj((oh *)base);
821 if (clobbered != 0) {
822 GC_err_printf("GC_debug_realloc: found smashed location at ");
823 GC_print_smashed_obj(p, clobbered);
825 old_sz = ((oh *)base) -> oh_sz;
827 if (old_sz < copy_sz) copy_sz = old_sz;
828 if (result == 0) return(0);
829 BCOPY(p, result, copy_sz);
834 #ifndef SHORT_DBG_HDRS
836 /* List of smashed objects. We defer printing these, since we can't */
837 /* always print them nicely with the allocation lock held. */
838 /* We put them here instead of in GC_arrays, since it may be useful to */
839 /* be able to look at them with the debugger. */
840 #define MAX_SMASHED 20
841 ptr_t GC_smashed[MAX_SMASHED];
842 unsigned GC_n_smashed = 0;
844 STATIC void GC_add_smashed(ptr_t smashed)
846 GC_ASSERT(GC_is_marked(GC_base(smashed)));
847 GC_smashed[GC_n_smashed] = smashed;
848 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
849 /* In case of overflow, we keep the first MAX_SMASHED-1 */
850 /* entries plus the last one. */
851 GC_have_errors = TRUE;
854 /* Print all objects on the list. Clear the list. */
855 STATIC void GC_print_all_smashed_proc(void)
859 GC_ASSERT(I_DONT_HOLD_LOCK());
860 if (GC_n_smashed == 0) return;
861 GC_err_printf("GC_check_heap_block: found smashed heap objects:\n");
862 for (i = 0; i < GC_n_smashed; ++i) {
863 GC_print_smashed_obj((ptr_t)GC_base(GC_smashed[i]) + sizeof(oh),
870 /* Check all marked objects in the given block for validity */
871 /* Avoid GC_apply_to_each_object for performance reasons. */
873 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy)
875 struct hblkhdr * hhdr = HDR(hbp);
876 size_t sz = hhdr -> hb_sz;
882 if (sz > MAXOBJBYTES) {
885 plim = hbp->hb_body + HBLKSIZE - sz;
887 /* go through all words in block */
889 if( mark_bit_from_hdr(hhdr, bit_no)
890 && GC_HAS_DEBUG_INFO((ptr_t)p)) {
891 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
893 if (clobbered != 0) GC_add_smashed(clobbered);
895 bit_no += MARK_BIT_OFFSET(sz);
901 /* This assumes that all accessible objects are marked, and that */
902 /* I hold the allocation lock. Normally called by collector. */
903 STATIC void GC_check_heap_proc(void)
905 # ifndef SMALL_CONFIG
906 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
907 /* FIXME: Should we check for twice that alignment? */
909 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
912 #endif /* !SHORT_DBG_HDRS */
915 GC_finalization_proc cl_fn;
919 void * GC_make_closure(GC_finalization_proc fn, void * data)
921 struct closure * result =
923 (struct closure *) GC_debug_malloc(sizeof (struct closure),
926 (struct closure *) GC_malloc(sizeof (struct closure));
929 result -> cl_fn = fn;
930 result -> cl_data = data;
931 return((void *)result);
934 void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
936 register struct closure * cl = (struct closure *) data;
938 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
941 /* Set ofn and ocd to reflect the values we got back. */
942 static void store_old (void *obj, GC_finalization_proc my_old_fn,
943 struct closure *my_old_cd, GC_finalization_proc *ofn,
946 if (0 != my_old_fn) {
947 if (my_old_fn != GC_debug_invoke_finalizer) {
948 GC_err_printf("Debuggable object at %p had non-debug finalizer.\n",
950 /* This should probably be fatal. */
952 if (ofn) *ofn = my_old_cd -> cl_fn;
953 if (ocd) *ocd = my_old_cd -> cl_data;
961 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
962 GC_finalization_proc fn,
963 void * cd, GC_finalization_proc *ofn,
966 GC_finalization_proc my_old_fn;
968 ptr_t base = GC_base(obj);
969 if (0 == base) return;
970 if ((ptr_t)obj - base != sizeof(oh)) {
972 "GC_debug_register_finalizer called with non-base-pointer %p\n",
976 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
978 GC_register_finalizer(base, GC_debug_invoke_finalizer,
979 GC_make_closure(fn,cd), &my_old_fn, &my_old_cd);
981 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
984 GC_API void GC_CALL GC_debug_register_finalizer_no_order
985 (void * obj, GC_finalization_proc fn,
986 void * cd, GC_finalization_proc *ofn,
989 GC_finalization_proc my_old_fn;
991 ptr_t base = GC_base(obj);
992 if (0 == base) return;
993 if ((ptr_t)obj - base != sizeof(oh)) {
995 "GC_debug_register_finalizer_no_order called with "
996 "non-base-pointer %p\n",
1000 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1002 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1003 GC_make_closure(fn,cd), &my_old_fn,
1006 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1009 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1010 (void * obj, GC_finalization_proc fn,
1011 void * cd, GC_finalization_proc *ofn,
1014 GC_finalization_proc my_old_fn;
1016 ptr_t base = GC_base(obj);
1017 if (0 == base) return;
1018 if ((ptr_t)obj - base != sizeof(oh)) {
1020 "GC_debug_register_finalizer_unreachable called with "
1021 "non-base-pointer %p\n",
1025 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1027 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1028 GC_make_closure(fn,cd), &my_old_fn,
1031 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1034 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1035 (void * obj, GC_finalization_proc fn,
1036 void * cd, GC_finalization_proc *ofn,
1039 GC_finalization_proc my_old_fn;
1041 ptr_t base = GC_base(obj);
1042 if (0 == base) return;
1043 if ((ptr_t)obj - base != sizeof(oh)) {
1045 "GC_debug_register_finalizer_ignore_self called with "
1046 "non-base-pointer %p\n", obj);
1049 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1051 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1052 GC_make_closure(fn,cd), &my_old_fn,
1055 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1058 #ifdef GC_ADD_CALLER
1059 # define RA GC_RETURN_ADDR,
1064 GC_API void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1066 return GC_debug_malloc(lb, RA "unknown", 0);
1069 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1071 return GC_debug_realloc(p, lb, RA "unknown", 0);