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.
22 #include "private/dbg_mlc.h"
24 void GC_default_print_heap_obj_proc();
25 GC_API void GC_register_finalizer_no_order
26 (void * obj, GC_finalization_proc fn, void * cd,
27 GC_finalization_proc *ofn, void * *ocd);
30 #ifndef SHORT_DBG_HDRS
31 /* Check whether object with base pointer p has debugging info */
32 /* p is assumed to point to a legitimate object in our part */
34 /* This excludes the check as to whether the back pointer is */
35 /* odd, which is added by the GC_HAS_DEBUG_INFO macro. */
36 /* Note that if DBG_HDRS_ALL is set, uncollectable objects */
37 /* on free lists may not have debug information set. Thus it's */
38 /* not always safe to return TRUE, even if the client does */
40 GC_bool GC_has_other_debug_info(ptr_t p)
42 register oh * ohdr = (oh *)p;
43 register ptr_t body = (ptr_t)(ohdr + 1);
44 register word sz = GC_size((ptr_t) ohdr);
46 if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
47 || sz < DEBUG_BYTES + EXTRA_BYTES) {
50 if (ohdr -> oh_sz == sz) {
51 /* Object may have had debug info, but has been deallocated */
54 if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
55 if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
66 # if defined(__GLIBC__) || defined(SOLARIS) \
67 || defined(HPUX) || defined(IRIX5) || defined(OSF1)
68 # define RANDOM() random()
70 # define RANDOM() (long)rand()
73 /* Store back pointer to source in dest, if that appears to be possible. */
74 /* This is not completely safe, since we may mistakenly conclude that */
75 /* dest has a debugging wrapper. But the error probability is very */
76 /* small, and this shouldn't be used in production code. */
77 /* We assume that dest is the real base pointer. Source will usually */
78 /* be a pointer to the interior of an object. */
79 void GC_store_back_pointer(ptr_t source, ptr_t dest)
81 if (GC_HAS_DEBUG_INFO(dest)) {
82 ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
86 void GC_marked_for_finalization(ptr_t dest) {
87 GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
90 /* Store information about the object referencing dest in *base_p */
92 /* source is root ==> *base_p = address, *offset_p = 0 */
93 /* source is heap object ==> *base_p != 0, *offset_p = offset */
94 /* Returns 1 on success, 0 if source couldn't be determined. */
95 /* Dest can be any address within a heap object. */
96 GC_ref_kind GC_get_back_ptr_info(void *dest, void **base_p, size_t *offset_p)
98 oh * hdr = (oh *)GC_base(dest);
101 if (!GC_HAS_DEBUG_INFO((ptr_t) hdr)) return GC_NO_SPACE;
102 bp = REVEAL_POINTER(hdr -> oh_back_ptr);
103 if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
104 if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
105 if (NOT_MARKED == bp) return GC_UNREFERENCED;
107 /* Heuristically try to fix off by 1 errors we introduced by */
108 /* insisting on even addresses. */
110 ptr_t alternate_ptr = bp + 1;
111 ptr_t target = *(ptr_t *)bp;
112 ptr_t alternate_target = *(ptr_t *)alternate_ptr;
114 if (alternate_target >= GC_least_plausible_heap_addr
115 && alternate_target <= GC_greatest_plausible_heap_addr
116 && (target < GC_least_plausible_heap_addr
117 || target > GC_greatest_plausible_heap_addr)) {
122 bp_base = GC_base(bp);
126 return GC_REFD_FROM_ROOT;
128 if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
130 *offset_p = bp - bp_base;
131 return GC_REFD_FROM_HEAP;
135 /* Generate a random heap address. */
136 /* The resulting address is in the heap, but */
137 /* not necessarily inside a valid object. */
138 void *GC_generate_random_heap_address(void)
141 long heap_offset = RANDOM();
142 if (GC_heapsize > RAND_MAX) {
143 heap_offset *= RAND_MAX;
144 heap_offset += RANDOM();
146 heap_offset %= GC_heapsize;
147 /* This doesn't yield a uniform distribution, especially if */
148 /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
149 /* it's not too bad. */
150 for (i = 0; i < GC_n_heap_sects; ++ i) {
151 size_t size = GC_heap_sects[i].hs_bytes;
152 if (heap_offset < size) {
153 return GC_heap_sects[i].hs_start + heap_offset;
158 ABORT("GC_generate_random_heap_address: size inconsistency");
163 /* Generate a random address inside a valid marked heap object. */
164 void *GC_generate_random_valid_address(void)
169 result = GC_generate_random_heap_address();
170 base = GC_base(result);
171 if (0 == base) continue;
172 if (!GC_is_marked(base)) continue;
177 /* Print back trace for p */
178 void GC_print_backtrace(void *p)
186 GC_print_heap_obj(GC_base(current));
189 source = GC_get_back_ptr_info(current, &base, &offset);
190 if (GC_UNREFERENCED == source) {
191 GC_err_printf("Reference could not be found\n");
194 if (GC_NO_SPACE == source) {
195 GC_err_printf("No debug info in object: Can't find reference\n");
198 GC_err_printf("Reachable via %d levels of pointers from ",
201 case GC_REFD_FROM_ROOT:
202 GC_err_printf("root at %p\n\n", base);
204 case GC_REFD_FROM_REG:
205 GC_err_printf("root in register\n\n");
207 case GC_FINALIZER_REFD:
208 GC_err_printf("list of finalizable objects\n\n");
210 case GC_REFD_FROM_HEAP:
211 GC_err_printf("offset %ld in object:\n", (unsigned long)offset);
212 /* Take GC_base(base) to get real base, i.e. header. */
213 GC_print_heap_obj(GC_base(base));
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);
249 /* There is some argument that we should dissble signals here. */
250 /* But that's expensive. And this way things should only appear */
251 /* inconsistent while we're in the handler. */
253 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
254 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
255 # ifdef KEEP_BACK_PTRS
256 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
258 # ifdef MAKE_BACK_GRAPH
259 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
261 ((oh *)p) -> oh_string = string;
262 ((oh *)p) -> oh_int = integer;
263 # ifndef SHORT_DBG_HDRS
264 ((oh *)p) -> oh_sz = sz;
265 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
266 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
267 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
270 return((ptr_t)result);
274 /* Store debugging info into p. Return displaced pointer. */
275 /* This version assumes we do hold the allocation lock. */
276 ptr_t GC_store_debug_info_inner(ptr_t p, word sz, char *string, word integer)
278 register word * result = (word *)((oh *)p + 1);
280 /* There is some argument that we should disable signals here. */
281 /* But that's expensive. And this way things should only appear */
282 /* inconsistent while we're in the handler. */
283 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
284 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
285 # ifdef KEEP_BACK_PTRS
286 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
288 # ifdef MAKE_BACK_GRAPH
289 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
291 ((oh *)p) -> oh_string = string;
292 ((oh *)p) -> oh_int = integer;
293 # ifndef SHORT_DBG_HDRS
294 ((oh *)p) -> oh_sz = sz;
295 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
296 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
297 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
299 return((ptr_t)result);
303 #ifndef SHORT_DBG_HDRS
304 /* Check the object with debugging info at ohdr */
305 /* return NIL if it's OK. Else return clobbered */
307 ptr_t GC_check_annotated_obj(oh *ohdr)
309 register ptr_t body = (ptr_t)(ohdr + 1);
310 register word gc_sz = GC_size((ptr_t)ohdr);
311 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
312 return((ptr_t)(&(ohdr -> oh_sz)));
314 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
315 return((ptr_t)(&(ohdr -> oh_sf)));
317 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
318 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
320 if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
321 != (END_FLAG ^ (word)body)) {
322 return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)));
326 #endif /* !SHORT_DBG_HDRS */
328 static GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
330 void GC_register_describe_type_fn(int kind, GC_describe_type_fn fn)
332 GC_describe_type_fns[kind] = fn;
335 /* Print a type description for the object whose client-visible address */
337 void GC_print_type(ptr_t p)
339 hdr * hhdr = GC_find_header(p);
340 char buffer[GC_TYPE_DESCR_LEN + 1];
341 int kind = hhdr -> hb_obj_kind;
343 if (0 != GC_describe_type_fns[kind] && GC_is_marked(GC_base(p))) {
344 /* This should preclude free list objects except with */
345 /* thread-local allocation. */
346 buffer[GC_TYPE_DESCR_LEN] = 0;
347 (GC_describe_type_fns[kind])(p, buffer);
348 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
353 GC_err_puts("PTRFREE");
356 GC_err_puts("NORMAL");
359 GC_err_puts("UNCOLLECTABLE");
361 # ifdef ATOMIC_UNCOLLECTABLE
363 GC_err_puts("ATOMIC UNCOLLECTABLE");
367 GC_err_puts("STUBBORN");
370 GC_err_printf("kind %d, descr 0x%lx", kind,
371 (unsigned long)(hhdr -> hb_descr));
378 void GC_print_obj(ptr_t p)
380 register oh * ohdr = (oh *)GC_base(p);
382 GC_ASSERT(I_DONT_HOLD_LOCK());
383 GC_err_printf("%p (", ((ptr_t)ohdr + sizeof(oh)));
384 GC_err_puts(ohdr -> oh_string);
385 # ifdef SHORT_DBG_HDRS
386 GC_err_printf(":%ld, ", (unsigned long)(ohdr -> oh_int));
388 GC_err_printf(":%ld, sz=%ld, ", (unsigned long)(ohdr -> oh_int),
389 (unsigned long)(ohdr -> oh_sz));
391 GC_print_type((ptr_t)(ohdr + 1));
393 PRINT_CALL_CHAIN(ohdr);
396 void GC_debug_print_heap_obj_proc(ptr_t p)
398 GC_ASSERT(I_DONT_HOLD_LOCK());
399 if (GC_HAS_DEBUG_INFO(p)) {
402 GC_default_print_heap_obj_proc(p);
406 #ifndef SHORT_DBG_HDRS
407 /* Use GC_err_printf and friends to print a description of the object */
408 /* whose client-visible address is p, and which was smashed at */
409 /* clobbered_addr. */
410 void GC_print_smashed_obj(ptr_t p, ptr_t clobbered_addr)
412 register oh * ohdr = (oh *)GC_base(p);
414 GC_ASSERT(I_DONT_HOLD_LOCK());
415 GC_err_printf("%p in or near object at %p(", clobbered_addr, p);
416 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
417 || ohdr -> oh_string == 0) {
418 GC_err_printf("<smashed>, appr. sz = %ld)\n",
419 (GC_size((ptr_t)ohdr) - DEBUG_BYTES));
421 if (ohdr -> oh_string[0] == '\0') {
422 GC_err_puts("EMPTY(smashed?)");
424 GC_err_puts(ohdr -> oh_string);
426 GC_err_printf(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
427 (unsigned long)(ohdr -> oh_sz));
428 PRINT_CALL_CHAIN(ohdr);
433 void GC_check_heap_proc (void);
435 void GC_print_all_smashed_proc (void);
437 void GC_do_nothing(void) {}
439 void GC_start_debugging(void)
441 # ifndef SHORT_DBG_HDRS
442 GC_check_heap = GC_check_heap_proc;
443 GC_print_all_smashed = GC_print_all_smashed_proc;
445 GC_check_heap = GC_do_nothing;
446 GC_print_all_smashed = GC_do_nothing;
448 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
449 GC_debugging_started = TRUE;
450 GC_register_displacement((word)sizeof(oh));
453 size_t GC_debug_header_size = sizeof(oh);
455 void GC_debug_register_displacement(size_t offset)
457 GC_register_displacement(offset);
458 GC_register_displacement((word)sizeof(oh) + offset);
461 void * GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS)
463 void * result = GC_malloc(lb + DEBUG_BYTES);
466 GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
469 GC_err_printf(":%ld)\n", (unsigned long)i);
472 if (!GC_debugging_started) {
473 GC_start_debugging();
475 ADD_CALL_CHAIN(result, ra);
476 return (GC_store_debug_info(result, (word)lb, s, (word)i));
479 void * GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
481 void * result = GC_malloc_ignore_off_page(lb + DEBUG_BYTES);
484 GC_err_printf("GC_debug_malloc_ignore_off_page(%lu) returning NIL (",
487 GC_err_printf(":%lu)\n", (unsigned long)i);
490 if (!GC_debugging_started) {
491 GC_start_debugging();
493 ADD_CALL_CHAIN(result, ra);
494 return (GC_store_debug_info(result, (word)lb, s, (word)i));
497 void * GC_debug_malloc_atomic_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
499 void * result = GC_malloc_atomic_ignore_off_page(lb + DEBUG_BYTES);
502 GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
503 " returning NIL (", (unsigned long) lb);
505 GC_err_printf(":%lu)\n", (unsigned long)i);
508 if (!GC_debugging_started) {
509 GC_start_debugging();
511 ADD_CALL_CHAIN(result, ra);
512 return (GC_store_debug_info(result, (word)lb, s, (word)i));
517 * An allocation function for internal use.
518 * Normally internally allocated objects do not have debug information.
519 * But in this case, we need to make sure that all objects have debug
521 * We assume debugging was started in collector initialization,
522 * and we already hold the GC lock.
524 void * GC_debug_generic_malloc_inner(size_t lb, int k)
526 void * result = GC_generic_malloc_inner(lb + DEBUG_BYTES, k);
529 GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
533 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
534 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
537 void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb, int k)
539 void * result = GC_generic_malloc_inner_ignore_off_page(
540 lb + DEBUG_BYTES, k);
543 GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
547 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
548 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
552 #ifdef STUBBORN_ALLOC
553 void * GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
555 void * result = GC_malloc_stubborn(lb + DEBUG_BYTES);
558 GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
561 GC_err_printf(":%lu)\n", (unsigned long)i);
564 if (!GC_debugging_started) {
565 GC_start_debugging();
567 ADD_CALL_CHAIN(result, ra);
568 return (GC_store_debug_info(result, (word)lb, s, (word)i));
571 void GC_debug_change_stubborn(void *p)
573 void * q = GC_base(p);
577 GC_err_printf("Bad argument: %p to GC_debug_change_stubborn\n", p);
578 ABORT("GC_debug_change_stubborn: bad arg");
581 if (hhdr -> hb_obj_kind != STUBBORN) {
582 GC_err_printf("GC_debug_change_stubborn arg not stubborn: %p\n", p);
583 ABORT("GC_debug_change_stubborn: arg not stubborn");
585 GC_change_stubborn(q);
588 void GC_debug_end_stubborn_change(void *p)
590 register void * q = GC_base(p);
594 GC_err_printf("Bad argument: %p to GC_debug_end_stubborn_change\n", p);
595 ABORT("GC_debug_end_stubborn_change: bad arg");
598 if (hhdr -> hb_obj_kind != STUBBORN) {
599 GC_err_printf("debug_end_stubborn_change arg not stubborn: %p\n", p);
600 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
602 GC_end_stubborn_change(q);
605 #else /* !STUBBORN_ALLOC */
607 void * GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
609 return GC_debug_malloc(lb, OPT_RA s, i);
612 void GC_debug_change_stubborn(void *p)
616 void GC_debug_end_stubborn_change(void *p)
620 #endif /* !STUBBORN_ALLOC */
622 void * GC_debug_malloc_atomic(size_t lb, GC_EXTRA_PARAMS)
624 void * result = GC_malloc_atomic(lb + DEBUG_BYTES);
627 GC_err_printf("GC_debug_malloc_atomic(%lu) returning NIL (",
630 GC_err_printf(":%lu)\n", (unsigned long)i);
633 if (!GC_debugging_started) {
634 GC_start_debugging();
636 ADD_CALL_CHAIN(result, ra);
637 return (GC_store_debug_info(result, (word)lb, s, (word)i));
640 char *GC_debug_strdup(const char *str, GC_EXTRA_PARAMS)
643 if (str == NULL) return NULL;
644 copy = GC_debug_malloc_atomic(strlen(str) + 1, OPT_RA s, i);
653 void * GC_debug_malloc_uncollectable(size_t lb, GC_EXTRA_PARAMS)
655 void * result = GC_malloc_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
658 GC_err_printf("GC_debug_malloc_uncollectable(%lu) returning NIL (",
661 GC_err_printf(":%lu)\n", (unsigned long)i);
664 if (!GC_debugging_started) {
665 GC_start_debugging();
667 ADD_CALL_CHAIN(result, ra);
668 return (GC_store_debug_info(result, (word)lb, s, (word)i));
671 #ifdef ATOMIC_UNCOLLECTABLE
672 void * GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
675 GC_malloc_atomic_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
679 "GC_debug_malloc_atomic_uncollectable(%lu) returning NIL (",
682 GC_err_printf(":%lu)\n", (unsigned long)i);
685 if (!GC_debugging_started) {
686 GC_start_debugging();
688 ADD_CALL_CHAIN(result, ra);
689 return (GC_store_debug_info(result, (word)lb, s, (word)i));
691 #endif /* ATOMIC_UNCOLLECTABLE */
693 void GC_debug_free(void * p)
701 GC_err_printf("Attempt to free invalid pointer %p\n", p);
702 ABORT("free(invalid pointer)");
704 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
706 "GC_debug_free called on pointer %p wo debugging info\n", p);
708 # ifndef SHORT_DBG_HDRS
709 clobbered = GC_check_annotated_obj((oh *)base);
710 if (clobbered != 0) {
711 if (((oh *)base) -> oh_sz == GC_size(base)) {
713 "GC_debug_free: found previously deallocated (?) object at ");
715 GC_err_printf("GC_debug_free: found smashed location at ");
717 GC_print_smashed_obj(p, clobbered);
719 /* Invalidate size */
720 ((oh *)base) -> oh_sz = GC_size(base);
721 # endif /* SHORT_DBG_HDRS */
727 GC_bool uncollectable = FALSE;
729 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
730 uncollectable = TRUE;
732 # ifdef ATOMIC_UNCOLLECTABLE
733 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
734 uncollectable = TRUE;
741 size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
743 for (i = 0; i < obj_sz; ++i) ((word *)p)[i] = 0xdeadbeef;
744 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
746 } /* !GC_find_leak */
751 extern void GC_free_inner(void * p);
753 /* Used internally; we assume it's called correctly. */
754 void GC_debug_free_inner(void * p)
756 GC_free_inner(GC_base(p));
760 void * GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
762 void * base = GC_base(p);
769 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
771 GC_err_printf("Attempt to reallocate invalid pointer %p\n", p);
772 ABORT("realloc(invalid pointer)");
774 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
776 "GC_debug_realloc called on pointer %p wo debugging info\n", p);
777 return(GC_realloc(p, lb));
780 switch (hhdr -> hb_obj_kind) {
781 # ifdef STUBBORN_ALLOC
783 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
787 result = GC_debug_malloc(lb, OPT_RA s, i);
790 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
793 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
795 # ifdef ATOMIC_UNCOLLECTABLE
797 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
801 GC_err_printf("GC_debug_realloc: encountered bad kind\n");
804 # ifdef SHORT_DBG_HDRS
805 old_sz = GC_size(base) - sizeof(oh);
807 clobbered = GC_check_annotated_obj((oh *)base);
808 if (clobbered != 0) {
809 GC_err_printf("GC_debug_realloc: found smashed location at ");
810 GC_print_smashed_obj(p, clobbered);
812 old_sz = ((oh *)base) -> oh_sz;
814 if (old_sz < copy_sz) copy_sz = old_sz;
815 if (result == 0) return(0);
816 BCOPY(p, result, copy_sz);
821 #ifndef SHORT_DBG_HDRS
823 /* List of smashed objects. We defer printing these, since we can't */
824 /* always print them nicely with the allocation lock held. */
825 /* We put them here instead of in GC_arrays, since it may be useful to */
826 /* be able to look at them with the debugger. */
827 #define MAX_SMASHED 20
828 ptr_t GC_smashed[MAX_SMASHED];
829 unsigned GC_n_smashed = 0;
831 void GC_add_smashed(ptr_t smashed)
833 GC_ASSERT(GC_is_marked(GC_base(smashed)));
834 GC_smashed[GC_n_smashed] = smashed;
835 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
836 /* In case of overflow, we keep the first MAX_SMASHED-1 */
837 /* entries plus the last one. */
838 GC_have_errors = TRUE;
841 /* Print all objects on the list. Clear the list. */
842 void GC_print_all_smashed_proc(void)
846 GC_ASSERT(I_DONT_HOLD_LOCK());
847 if (GC_n_smashed == 0) return;
848 GC_err_printf("GC_check_heap_block: found smashed heap objects:\n");
849 for (i = 0; i < GC_n_smashed; ++i) {
850 GC_print_smashed_obj((ptr_t)GC_base(GC_smashed[i]) + sizeof(oh),
857 /* Check all marked objects in the given block for validity */
858 /* Avoid GC_apply_to_each_object for performance reasons. */
860 void GC_check_heap_block(struct hblk *hbp, word dummy)
862 struct hblkhdr * hhdr = HDR(hbp);
863 size_t sz = hhdr -> hb_sz;
869 if (sz > MAXOBJBYTES) {
872 plim = hbp->hb_body + HBLKSIZE - sz;
874 /* go through all words in block */
876 if( mark_bit_from_hdr(hhdr, bit_no)
877 && GC_HAS_DEBUG_INFO((ptr_t)p)) {
878 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
880 if (clobbered != 0) GC_add_smashed(clobbered);
882 bit_no += MARK_BIT_OFFSET(sz);
888 /* This assumes that all accessible objects are marked, and that */
889 /* I hold the allocation lock. Normally called by collector. */
890 void GC_check_heap_proc(void)
892 # ifndef SMALL_CONFIG
893 /* Ignore gcc no effect warning on the following. */
894 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
895 /* FIXME: Should we check for twice that alignment? */
897 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
900 #endif /* !SHORT_DBG_HDRS */
903 GC_finalization_proc cl_fn;
907 void * GC_make_closure(GC_finalization_proc fn, void * data)
909 struct closure * result =
911 (struct closure *) GC_debug_malloc(sizeof (struct closure),
914 (struct closure *) GC_malloc(sizeof (struct closure));
917 result -> cl_fn = fn;
918 result -> cl_data = data;
919 return((void *)result);
922 void GC_debug_invoke_finalizer(void * obj, void * data)
924 register struct closure * cl = (struct closure *) data;
926 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
929 /* Set ofn and ocd to reflect the values we got back. */
930 static void store_old (void *obj, GC_finalization_proc my_old_fn,
931 struct closure *my_old_cd, GC_finalization_proc *ofn,
934 if (0 != my_old_fn) {
935 if (my_old_fn != GC_debug_invoke_finalizer) {
936 GC_err_printf("Debuggable object at %p had non-debug finalizer.\n",
938 /* This should probably be fatal. */
940 if (ofn) *ofn = my_old_cd -> cl_fn;
941 if (ocd) *ocd = my_old_cd -> cl_data;
949 void GC_debug_register_finalizer(void * obj, GC_finalization_proc fn,
950 void * cd, GC_finalization_proc *ofn,
953 GC_finalization_proc my_old_fn;
955 ptr_t base = GC_base(obj);
956 if (0 == base) return;
957 if ((ptr_t)obj - base != sizeof(oh)) {
959 "GC_debug_register_finalizer called with non-base-pointer %p\n",
963 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
965 GC_register_finalizer(base, GC_debug_invoke_finalizer,
966 GC_make_closure(fn,cd), &my_old_fn, &my_old_cd);
968 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
971 void GC_debug_register_finalizer_no_order
972 (void * obj, GC_finalization_proc fn,
973 void * cd, GC_finalization_proc *ofn,
976 GC_finalization_proc my_old_fn;
978 ptr_t base = GC_base(obj);
979 if (0 == base) return;
980 if ((ptr_t)obj - base != sizeof(oh)) {
982 "GC_debug_register_finalizer_no_order called with "
983 "non-base-pointer %p\n",
987 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
989 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
990 GC_make_closure(fn,cd), &my_old_fn,
993 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
996 void GC_debug_register_finalizer_unreachable
997 (void * obj, GC_finalization_proc fn,
998 void * cd, GC_finalization_proc *ofn,
1001 GC_finalization_proc my_old_fn;
1003 ptr_t base = GC_base(obj);
1004 if (0 == base) return;
1005 if ((ptr_t)obj - base != sizeof(oh)) {
1007 "GC_debug_register_finalizer_unreachable called with "
1008 "non-base-pointer %p\n",
1012 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1014 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1015 GC_make_closure(fn,cd), &my_old_fn,
1018 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1021 void GC_debug_register_finalizer_ignore_self
1022 (void * obj, GC_finalization_proc fn,
1023 void * cd, GC_finalization_proc *ofn,
1026 GC_finalization_proc my_old_fn;
1028 ptr_t base = GC_base(obj);
1029 if (0 == base) return;
1030 if ((ptr_t)obj - base != sizeof(oh)) {
1032 "GC_debug_register_finalizer_ignore_self called with "
1033 "non-base-pointer %p\n", obj);
1036 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1038 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1039 GC_make_closure(fn,cd), &my_old_fn,
1042 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1045 #ifdef GC_ADD_CALLER
1046 # define RA GC_RETURN_ADDR,
1051 void * GC_debug_malloc_replacement(size_t lb)
1053 return GC_debug_malloc(lb, RA "unknown", 0);
1056 void * GC_debug_realloc_replacement(void *p, size_t lb)
1058 return GC_debug_realloc(p, lb, RA "unknown", 0);