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();
23 GC_API void 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 long 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 ",
199 case GC_REFD_FROM_ROOT:
200 GC_err_printf("root at %p\n\n", base);
202 case GC_REFD_FROM_REG:
203 GC_err_printf("root in register\n\n");
205 case GC_FINALIZER_REFD:
206 GC_err_printf("list of finalizable objects\n\n");
208 case GC_REFD_FROM_HEAP:
209 GC_err_printf("offset %ld in object:\n", (unsigned long)offset);
210 /* Take GC_base(base) to get real base, i.e. header. */
211 GC_print_heap_obj(GC_base(base));
220 /* Force a garbage collection and generate a backtrace from a */
221 /* random heap address. */
222 void GC_generate_random_backtrace_no_gc(void)
225 current = GC_generate_random_valid_address();
226 GC_printf("\n****Chose address %p in object\n", current);
227 GC_print_backtrace(current);
230 void GC_generate_random_backtrace(void)
233 GC_generate_random_backtrace_no_gc();
236 #endif /* KEEP_BACK_PTRS */
238 # define CROSSES_HBLK(p, sz) \
239 (((word)(p + sizeof(oh) + sz - 1) ^ (word)p) >= HBLKSIZE)
240 /* Store debugging info into p. Return displaced pointer. */
241 /* Assumes we don't hold allocation lock. */
242 ptr_t GC_store_debug_info(ptr_t p, word sz, const char *string, word integer)
244 register word * result = (word *)((oh *)p + 1);
247 /* There is some argument that we should dissble signals here. */
248 /* But that's expensive. And this way things should only appear */
249 /* inconsistent while we're in the handler. */
251 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
252 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
253 # ifdef KEEP_BACK_PTRS
254 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
256 # ifdef MAKE_BACK_GRAPH
257 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
259 ((oh *)p) -> oh_string = string;
260 ((oh *)p) -> oh_int = integer;
261 # ifndef SHORT_DBG_HDRS
262 ((oh *)p) -> oh_sz = sz;
263 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
264 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
265 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
268 return((ptr_t)result);
272 /* Store debugging info into p. Return displaced pointer. */
273 /* This version assumes we do hold the allocation lock. */
274 ptr_t GC_store_debug_info_inner(ptr_t p, word sz, char *string, word integer)
276 register word * result = (word *)((oh *)p + 1);
278 /* There is some argument that we should disable signals here. */
279 /* But that's expensive. And this way things should only appear */
280 /* inconsistent while we're in the handler. */
281 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
282 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
283 # ifdef KEEP_BACK_PTRS
284 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
286 # ifdef MAKE_BACK_GRAPH
287 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
289 ((oh *)p) -> oh_string = string;
290 ((oh *)p) -> oh_int = integer;
291 # ifndef SHORT_DBG_HDRS
292 ((oh *)p) -> oh_sz = sz;
293 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
294 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
295 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
297 return((ptr_t)result);
301 #ifndef SHORT_DBG_HDRS
302 /* Check the object with debugging info at ohdr */
303 /* return NIL if it's OK. Else return clobbered */
305 ptr_t GC_check_annotated_obj(oh *ohdr)
307 register ptr_t body = (ptr_t)(ohdr + 1);
308 register word gc_sz = GC_size((ptr_t)ohdr);
309 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
310 return((ptr_t)(&(ohdr -> oh_sz)));
312 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
313 return((ptr_t)(&(ohdr -> oh_sf)));
315 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
316 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
318 if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
319 != (END_FLAG ^ (word)body)) {
320 return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)));
324 #endif /* !SHORT_DBG_HDRS */
326 static GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
328 void GC_register_describe_type_fn(int kind, GC_describe_type_fn fn)
330 GC_describe_type_fns[kind] = fn;
333 /* Print a type description for the object whose client-visible address */
335 void GC_print_type(ptr_t p)
337 hdr * hhdr = GC_find_header(p);
338 char buffer[GC_TYPE_DESCR_LEN + 1];
339 int kind = hhdr -> hb_obj_kind;
341 if (0 != GC_describe_type_fns[kind] && GC_is_marked(GC_base(p))) {
342 /* This should preclude free list objects except with */
343 /* thread-local allocation. */
344 buffer[GC_TYPE_DESCR_LEN] = 0;
345 (GC_describe_type_fns[kind])(p, buffer);
346 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
351 GC_err_puts("PTRFREE");
354 GC_err_puts("NORMAL");
357 GC_err_puts("UNCOLLECTABLE");
359 # ifdef ATOMIC_UNCOLLECTABLE
361 GC_err_puts("ATOMIC UNCOLLECTABLE");
365 GC_err_puts("STUBBORN");
368 GC_err_printf("kind %d, descr 0x%lx", kind,
369 (unsigned long)(hhdr -> hb_descr));
376 void GC_print_obj(ptr_t p)
378 register oh * ohdr = (oh *)GC_base(p);
380 GC_ASSERT(I_DONT_HOLD_LOCK());
381 GC_err_printf("%p (", ((ptr_t)ohdr + sizeof(oh)));
382 GC_err_puts(ohdr -> oh_string);
383 # ifdef SHORT_DBG_HDRS
384 GC_err_printf(":%ld, ", (unsigned long)(ohdr -> oh_int));
386 GC_err_printf(":%ld, sz=%ld, ", (unsigned long)(ohdr -> oh_int),
387 (unsigned long)(ohdr -> oh_sz));
389 GC_print_type((ptr_t)(ohdr + 1));
391 PRINT_CALL_CHAIN(ohdr);
394 void GC_debug_print_heap_obj_proc(ptr_t p)
396 GC_ASSERT(I_DONT_HOLD_LOCK());
397 if (GC_HAS_DEBUG_INFO(p)) {
400 GC_default_print_heap_obj_proc(p);
404 #ifndef SHORT_DBG_HDRS
405 /* Use GC_err_printf and friends to print a description of the object */
406 /* whose client-visible address is p, and which was smashed at */
407 /* clobbered_addr. */
408 void GC_print_smashed_obj(ptr_t p, ptr_t clobbered_addr)
410 register oh * ohdr = (oh *)GC_base(p);
412 GC_ASSERT(I_DONT_HOLD_LOCK());
413 GC_err_printf("%p in or near object at %p(", clobbered_addr, p);
414 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
415 || ohdr -> oh_string == 0) {
416 GC_err_printf("<smashed>, appr. sz = %ld)\n",
417 (GC_size((ptr_t)ohdr) - DEBUG_BYTES));
419 if (ohdr -> oh_string[0] == '\0') {
420 GC_err_puts("EMPTY(smashed?)");
422 GC_err_puts(ohdr -> oh_string);
424 GC_err_printf(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
425 (unsigned long)(ohdr -> oh_sz));
426 PRINT_CALL_CHAIN(ohdr);
431 void GC_check_heap_proc (void);
433 void GC_print_all_smashed_proc (void);
435 void GC_do_nothing(void) {}
437 void GC_start_debugging(void)
439 # ifndef SHORT_DBG_HDRS
440 GC_check_heap = GC_check_heap_proc;
441 GC_print_all_smashed = GC_print_all_smashed_proc;
443 GC_check_heap = GC_do_nothing;
444 GC_print_all_smashed = GC_do_nothing;
446 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
447 GC_debugging_started = TRUE;
448 GC_register_displacement((word)sizeof(oh));
451 size_t GC_debug_header_size = sizeof(oh);
453 void GC_debug_register_displacement(size_t offset)
455 GC_register_displacement(offset);
456 GC_register_displacement((word)sizeof(oh) + offset);
459 void * GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS)
461 void * result = GC_malloc(lb + DEBUG_BYTES);
464 GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
467 GC_err_printf(":%ld)\n", (unsigned long)i);
470 if (!GC_debugging_started) {
471 GC_start_debugging();
473 ADD_CALL_CHAIN(result, ra);
474 return (GC_store_debug_info(result, (word)lb, s, (word)i));
477 void * GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
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 void * GC_debug_malloc_atomic_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
497 void * result = GC_malloc_atomic_ignore_off_page(lb + DEBUG_BYTES);
500 GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
501 " returning NIL (", (unsigned long) lb);
503 GC_err_printf(":%lu)\n", (unsigned long)i);
506 if (!GC_debugging_started) {
507 GC_start_debugging();
509 ADD_CALL_CHAIN(result, ra);
510 return (GC_store_debug_info(result, (word)lb, s, (word)i));
515 * An allocation function for internal use.
516 * Normally internally allocated objects do not have debug information.
517 * But in this case, we need to make sure that all objects have debug
519 * We assume debugging was started in collector initialization,
520 * and we already hold the GC lock.
522 void * GC_debug_generic_malloc_inner(size_t lb, int k)
524 void * result = GC_generic_malloc_inner(lb + DEBUG_BYTES, k);
527 GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
531 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
532 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
535 void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb, int k)
537 void * result = GC_generic_malloc_inner_ignore_off_page(
538 lb + DEBUG_BYTES, k);
541 GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
545 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
546 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
550 #ifdef STUBBORN_ALLOC
551 void * GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
553 void * result = GC_malloc_stubborn(lb + DEBUG_BYTES);
556 GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
559 GC_err_printf(":%lu)\n", (unsigned long)i);
562 if (!GC_debugging_started) {
563 GC_start_debugging();
565 ADD_CALL_CHAIN(result, ra);
566 return (GC_store_debug_info(result, (word)lb, s, (word)i));
569 void GC_debug_change_stubborn(void *p)
571 void * q = GC_base(p);
575 GC_err_printf("Bad argument: %p to GC_debug_change_stubborn\n", p);
576 ABORT("GC_debug_change_stubborn: bad arg");
579 if (hhdr -> hb_obj_kind != STUBBORN) {
580 GC_err_printf("GC_debug_change_stubborn arg not stubborn: %p\n", p);
581 ABORT("GC_debug_change_stubborn: arg not stubborn");
583 GC_change_stubborn(q);
586 void GC_debug_end_stubborn_change(void *p)
588 register void * q = GC_base(p);
592 GC_err_printf("Bad argument: %p to GC_debug_end_stubborn_change\n", p);
593 ABORT("GC_debug_end_stubborn_change: bad arg");
596 if (hhdr -> hb_obj_kind != STUBBORN) {
597 GC_err_printf("debug_end_stubborn_change arg not stubborn: %p\n", p);
598 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
600 GC_end_stubborn_change(q);
603 #else /* !STUBBORN_ALLOC */
605 void * GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
607 return GC_debug_malloc(lb, OPT_RA s, i);
610 void GC_debug_change_stubborn(void *p)
614 void GC_debug_end_stubborn_change(void *p)
618 #endif /* !STUBBORN_ALLOC */
620 void * GC_debug_malloc_atomic(size_t lb, GC_EXTRA_PARAMS)
622 void * result = GC_malloc_atomic(lb + DEBUG_BYTES);
625 GC_err_printf("GC_debug_malloc_atomic(%lu) returning NIL (",
628 GC_err_printf(":%lu)\n", (unsigned long)i);
631 if (!GC_debugging_started) {
632 GC_start_debugging();
634 ADD_CALL_CHAIN(result, ra);
635 return (GC_store_debug_info(result, (word)lb, s, (word)i));
638 char *GC_debug_strdup(const char *str, GC_EXTRA_PARAMS)
641 if (str == NULL) return NULL;
642 copy = GC_debug_malloc_atomic(strlen(str) + 1, OPT_RA s, i);
651 void * GC_debug_malloc_uncollectable(size_t lb, GC_EXTRA_PARAMS)
653 void * result = GC_malloc_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
656 GC_err_printf("GC_debug_malloc_uncollectable(%lu) returning NIL (",
659 GC_err_printf(":%lu)\n", (unsigned long)i);
662 if (!GC_debugging_started) {
663 GC_start_debugging();
665 ADD_CALL_CHAIN(result, ra);
666 return (GC_store_debug_info(result, (word)lb, s, (word)i));
669 #ifdef ATOMIC_UNCOLLECTABLE
670 void * GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
673 GC_malloc_atomic_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
677 "GC_debug_malloc_atomic_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));
689 #endif /* ATOMIC_UNCOLLECTABLE */
691 void GC_debug_free(void * p)
699 GC_err_printf("Attempt to free invalid pointer %p\n", p);
700 ABORT("free(invalid pointer)");
702 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
704 "GC_debug_free called on pointer %p wo debugging info\n", p);
706 # ifndef SHORT_DBG_HDRS
707 clobbered = GC_check_annotated_obj((oh *)base);
708 if (clobbered != 0) {
709 if (((oh *)base) -> oh_sz == GC_size(base)) {
711 "GC_debug_free: found previously deallocated (?) object at ");
713 GC_err_printf("GC_debug_free: found smashed location at ");
715 GC_print_smashed_obj(p, clobbered);
717 /* Invalidate size */
718 ((oh *)base) -> oh_sz = GC_size(base);
719 # endif /* SHORT_DBG_HDRS */
725 GC_bool uncollectable = FALSE;
727 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
728 uncollectable = TRUE;
730 # ifdef ATOMIC_UNCOLLECTABLE
731 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
732 uncollectable = TRUE;
739 size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
741 for (i = 0; i < obj_sz; ++i) ((word *)p)[i] = 0xdeadbeef;
742 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
744 } /* !GC_find_leak */
749 extern void GC_free_inner(void * p);
751 /* Used internally; we assume it's called correctly. */
752 void GC_debug_free_inner(void * p)
754 GC_free_inner(GC_base(p));
758 void * GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
760 void * base = GC_base(p);
767 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
769 GC_err_printf("Attempt to reallocate invalid pointer %p\n", p);
770 ABORT("realloc(invalid pointer)");
772 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
774 "GC_debug_realloc called on pointer %p wo debugging info\n", p);
775 return(GC_realloc(p, lb));
778 switch (hhdr -> hb_obj_kind) {
779 # ifdef STUBBORN_ALLOC
781 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
785 result = GC_debug_malloc(lb, OPT_RA s, i);
788 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
791 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
793 # ifdef ATOMIC_UNCOLLECTABLE
795 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
799 GC_err_printf("GC_debug_realloc: encountered bad kind\n");
802 # ifdef SHORT_DBG_HDRS
803 old_sz = GC_size(base) - sizeof(oh);
805 clobbered = GC_check_annotated_obj((oh *)base);
806 if (clobbered != 0) {
807 GC_err_printf("GC_debug_realloc: found smashed location at ");
808 GC_print_smashed_obj(p, clobbered);
810 old_sz = ((oh *)base) -> oh_sz;
812 if (old_sz < copy_sz) copy_sz = old_sz;
813 if (result == 0) return(0);
814 BCOPY(p, result, copy_sz);
819 #ifndef SHORT_DBG_HDRS
821 /* List of smashed objects. We defer printing these, since we can't */
822 /* always print them nicely with the allocation lock held. */
823 /* We put them here instead of in GC_arrays, since it may be useful to */
824 /* be able to look at them with the debugger. */
825 #define MAX_SMASHED 20
826 ptr_t GC_smashed[MAX_SMASHED];
827 unsigned GC_n_smashed = 0;
829 void GC_add_smashed(ptr_t smashed)
831 GC_ASSERT(GC_is_marked(GC_base(smashed)));
832 GC_smashed[GC_n_smashed] = smashed;
833 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
834 /* In case of overflow, we keep the first MAX_SMASHED-1 */
835 /* entries plus the last one. */
836 GC_have_errors = TRUE;
839 /* Print all objects on the list. Clear the list. */
840 void GC_print_all_smashed_proc(void)
844 GC_ASSERT(I_DONT_HOLD_LOCK());
845 if (GC_n_smashed == 0) return;
846 GC_err_printf("GC_check_heap_block: found smashed heap objects:\n");
847 for (i = 0; i < GC_n_smashed; ++i) {
848 GC_print_smashed_obj((ptr_t)GC_base(GC_smashed[i]) + sizeof(oh),
855 /* Check all marked objects in the given block for validity */
856 /* Avoid GC_apply_to_each_object for performance reasons. */
858 void GC_check_heap_block(struct hblk *hbp, word dummy)
860 struct hblkhdr * hhdr = HDR(hbp);
861 size_t sz = hhdr -> hb_sz;
867 if (sz > MAXOBJBYTES) {
870 plim = hbp->hb_body + HBLKSIZE - sz;
872 /* go through all words in block */
874 if( mark_bit_from_hdr(hhdr, bit_no)
875 && GC_HAS_DEBUG_INFO((ptr_t)p)) {
876 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
878 if (clobbered != 0) GC_add_smashed(clobbered);
880 bit_no += MARK_BIT_OFFSET(sz);
886 /* This assumes that all accessible objects are marked, and that */
887 /* I hold the allocation lock. Normally called by collector. */
888 void GC_check_heap_proc(void)
890 # ifndef SMALL_CONFIG
891 /* Ignore gcc no effect warning on the following. */
892 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
893 /* FIXME: Should we check for twice that alignment? */
895 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
898 #endif /* !SHORT_DBG_HDRS */
901 GC_finalization_proc cl_fn;
905 void * GC_make_closure(GC_finalization_proc fn, void * data)
907 struct closure * result =
909 (struct closure *) GC_debug_malloc(sizeof (struct closure),
912 (struct closure *) GC_malloc(sizeof (struct closure));
915 result -> cl_fn = fn;
916 result -> cl_data = data;
917 return((void *)result);
920 void GC_debug_invoke_finalizer(void * obj, void * data)
922 register struct closure * cl = (struct closure *) data;
924 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
927 /* Set ofn and ocd to reflect the values we got back. */
928 static void store_old (void *obj, GC_finalization_proc my_old_fn,
929 struct closure *my_old_cd, GC_finalization_proc *ofn,
932 if (0 != my_old_fn) {
933 if (my_old_fn != GC_debug_invoke_finalizer) {
934 GC_err_printf("Debuggable object at %p had non-debug finalizer.\n",
936 /* This should probably be fatal. */
938 if (ofn) *ofn = my_old_cd -> cl_fn;
939 if (ocd) *ocd = my_old_cd -> cl_data;
947 void GC_debug_register_finalizer(void * obj, GC_finalization_proc fn,
948 void * cd, GC_finalization_proc *ofn,
951 GC_finalization_proc my_old_fn;
953 ptr_t base = GC_base(obj);
954 if (0 == base) return;
955 if ((ptr_t)obj - base != sizeof(oh)) {
957 "GC_debug_register_finalizer called with non-base-pointer %p\n",
961 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
963 GC_register_finalizer(base, GC_debug_invoke_finalizer,
964 GC_make_closure(fn,cd), &my_old_fn, &my_old_cd);
966 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
969 void GC_debug_register_finalizer_no_order
970 (void * obj, GC_finalization_proc fn,
971 void * cd, GC_finalization_proc *ofn,
974 GC_finalization_proc my_old_fn;
976 ptr_t base = GC_base(obj);
977 if (0 == base) return;
978 if ((ptr_t)obj - base != sizeof(oh)) {
980 "GC_debug_register_finalizer_no_order called with "
981 "non-base-pointer %p\n",
985 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
987 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
988 GC_make_closure(fn,cd), &my_old_fn,
991 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
994 void GC_debug_register_finalizer_unreachable
995 (void * obj, GC_finalization_proc fn,
996 void * cd, GC_finalization_proc *ofn,
999 GC_finalization_proc my_old_fn;
1001 ptr_t base = GC_base(obj);
1002 if (0 == base) return;
1003 if ((ptr_t)obj - base != sizeof(oh)) {
1005 "GC_debug_register_finalizer_unreachable called with "
1006 "non-base-pointer %p\n",
1010 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1012 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1013 GC_make_closure(fn,cd), &my_old_fn,
1016 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1019 void GC_debug_register_finalizer_ignore_self
1020 (void * obj, GC_finalization_proc fn,
1021 void * cd, GC_finalization_proc *ofn,
1024 GC_finalization_proc my_old_fn;
1026 ptr_t base = GC_base(obj);
1027 if (0 == base) return;
1028 if ((ptr_t)obj - base != sizeof(oh)) {
1030 "GC_debug_register_finalizer_ignore_self called with "
1031 "non-base-pointer %p\n", obj);
1034 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1036 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1037 GC_make_closure(fn,cd), &my_old_fn,
1040 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1043 #ifdef GC_ADD_CALLER
1044 # define RA GC_RETURN_ADDR,
1049 void * GC_debug_malloc_replacement(size_t lb)
1051 return GC_debug_malloc(lb, RA "unknown", 0);
1054 void * GC_debug_realloc_replacement(void *p, size_t lb)
1056 return GC_debug_realloc(p, lb, RA "unknown", 0);