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.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
15 # define I_HIDE_POINTERS
17 # ifdef KEEP_BACK_PTRS
21 void GC_default_print_heap_obj_proc();
22 GC_API void GC_register_finalizer_no_order
23 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
24 GC_finalization_proc *ofn, GC_PTR *ocd));
26 /* Do we want to and know how to save the call stack at the time of */
27 /* an allocation? How much space do we want to use in each object? */
29 # define START_FLAG ((word)0xfedcedcb)
30 # define END_FLAG ((word)0xbcdecdef)
31 /* Stored both one past the end of user object, and one before */
32 /* the end of the object as seen by the allocator. */
37 # ifdef KEEP_BACK_PTRS
39 # define MARKED_FOR_FINALIZATION (ptr_t)(-1)
40 /* Object was marked because it is finalizable. */
45 char * oh_string; /* object descriptor string */
46 word oh_int; /* object descriptor integers */
48 struct callinfo oh_ci[NFRAMES];
50 word oh_sz; /* Original malloc arg. */
51 word oh_sf; /* start flag */
53 /* The size of the above structure is assumed not to dealign things, */
54 /* and to be a multiple of the word length. */
56 #define DEBUG_BYTES (sizeof (oh) + sizeof (word))
57 #undef ROUNDED_UP_WORDS
58 #define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + WORDS_TO_BYTES(1) - 1)
61 #ifdef SAVE_CALL_CHAIN
62 # define ADD_CALL_CHAIN(base, ra) GC_save_callers(((oh *)(base)) -> oh_ci)
63 # define PRINT_CALL_CHAIN(base) GC_print_callers(((oh *)(base)) -> oh_ci)
66 # define ADD_CALL_CHAIN(base, ra) ((oh *)(base)) -> oh_ci[0].ci_pc = (ra)
67 # define PRINT_CALL_CHAIN(base) GC_print_callers(((oh *)(base)) -> oh_ci)
69 # define ADD_CALL_CHAIN(base, ra)
70 # define PRINT_CALL_CHAIN(base)
74 /* Check whether object with base pointer p has debugging info */
75 /* p is assumed to point to a legitimate object in our part */
77 GC_bool GC_has_debug_info(p)
80 register oh * ohdr = (oh *)p;
81 register ptr_t body = (ptr_t)(ohdr + 1);
82 register word sz = GC_size((ptr_t) ohdr);
84 if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
85 || sz < sizeof (oh)) {
88 if (ohdr -> oh_sz == sz) {
89 /* Object may have had debug info, but has been deallocated */
92 if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
93 if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
100 /* Store back pointer to source in dest, if that appears to be possible. */
101 /* This is not completely safe, since we may mistakenly conclude that */
102 /* dest has a debugging wrapper. But the error probability is very */
103 /* small, and this shouldn't be used in production code. */
104 /* We assume that dest is the real base pointer. Source will usually */
105 /* be a pointer to the interior of an object. */
106 void GC_store_back_pointer(ptr_t source, ptr_t dest)
108 if (GC_has_debug_info(dest)) {
109 ((oh *)dest) -> oh_back_ptr = (ptr_t)HIDE_POINTER(source);
113 void GC_marked_for_finalization(ptr_t dest) {
114 GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
117 /* Store information about the object referencing dest in *base_p */
119 /* source is root ==> *base_p = 0, *offset_p = address */
120 /* source is heap object ==> *base_p != 0, *offset_p = offset */
121 /* Returns 1 on success, 0 if source couldn't be determined. */
122 /* Dest can be any address within a heap object. */
123 GC_ref_kind GC_get_back_ptr_info(void *dest, void **base_p, size_t *offset_p)
125 oh * hdr = (oh *)GC_base(dest);
128 if (!GC_has_debug_info((ptr_t) hdr)) return GC_NO_SPACE;
129 bp = hdr -> oh_back_ptr;
130 if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
131 if (0 == bp) return GC_UNREFERENCED;
132 bp = REVEAL_POINTER(bp);
133 bp_base = GC_base(bp);
137 return GC_REFD_FROM_ROOT;
139 if (GC_has_debug_info(bp_base)) bp_base += sizeof(oh);
141 *offset_p = bp - bp_base;
142 return GC_REFD_FROM_HEAP;
146 /* Generate a random heap address. */
147 /* The resulting address is in the heap, but */
148 /* not necessarily inside a valid object. */
149 void *GC_generate_random_heap_address(void)
152 int heap_offset = random() % GC_heapsize;
153 for (i = 0; i < GC_n_heap_sects; ++ i) {
154 int 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 void *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 /* Force a garbage collection and generate a backtrace from a */
181 /* random heap address. */
182 void GC_generate_random_backtrace(void)
190 current = GC_generate_random_valid_address();
191 GC_printf1("Chose address 0x%lx in object\n", (unsigned long)current);
192 GC_print_heap_obj(GC_base(current));
193 GC_err_printf0("\n");
195 source = GC_get_back_ptr_info(current, &base, &offset);
196 if (GC_UNREFERENCED == source) {
197 GC_err_printf0("Reference could not be found\n");
200 if (GC_NO_SPACE == source) {
201 GC_err_printf0("No debug info in object: Can't find reference\n");
204 GC_err_printf1("Reachable via %d levels of pointers from ",
207 case GC_REFD_FROM_ROOT:
208 GC_err_printf1("root at 0x%lx\n", (unsigned long)base);
210 case GC_FINALIZER_REFD:
211 GC_err_printf0("list of finalizable objects\n");
213 case GC_REFD_FROM_HEAP:
214 GC_err_printf1("offset %ld in object:\n", (unsigned long)offset);
215 /* Take GC_base(base) to get real base, i.e. header. */
216 GC_print_heap_obj(GC_base(base));
217 GC_err_printf0("\n");
225 #endif /* KEEP_BACK_PTRS */
227 /* Store debugging info into p. Return displaced pointer. */
228 /* Assumes we don't hold allocation lock. */
229 ptr_t GC_store_debug_info(p, sz, string, integer)
230 register ptr_t p; /* base pointer */
235 register word * result = (word *)((oh *)p + 1);
238 /* There is some argument that we should dissble signals here. */
239 /* But that's expensive. And this way things should only appear */
240 /* inconsistent while we're in the handler. */
242 # ifdef KEEP_BACK_PTRS
243 ((oh *)p) -> oh_back_ptr = 0;
245 ((oh *)p) -> oh_string = string;
246 ((oh *)p) -> oh_int = integer;
247 ((oh *)p) -> oh_sz = sz;
248 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
249 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
250 result[ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
252 return((ptr_t)result);
255 /* Check the object with debugging info at ohdr */
256 /* return NIL if it's OK. Else return clobbered */
258 ptr_t GC_check_annotated_obj(ohdr)
261 register ptr_t body = (ptr_t)(ohdr + 1);
262 register word gc_sz = GC_size((ptr_t)ohdr);
263 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
264 return((ptr_t)(&(ohdr -> oh_sz)));
266 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
267 return((ptr_t)(&(ohdr -> oh_sf)));
269 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
270 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
272 if (((word *)body)[ROUNDED_UP_WORDS(ohdr -> oh_sz)]
273 != (END_FLAG ^ (word)body)) {
274 return((ptr_t)((word *)body + ROUNDED_UP_WORDS(ohdr -> oh_sz)));
282 register oh * ohdr = (oh *)GC_base(p);
284 GC_err_printf1("0x%lx (", ((unsigned long)ohdr + sizeof(oh)));
285 GC_err_puts(ohdr -> oh_string);
286 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
287 (unsigned long)(ohdr -> oh_sz));
288 PRINT_CALL_CHAIN(ohdr);
291 void GC_debug_print_heap_obj_proc(p)
294 if (GC_has_debug_info(p)) {
297 GC_default_print_heap_obj_proc(p);
301 void GC_print_smashed_obj(p, clobbered_addr)
302 ptr_t p, clobbered_addr;
304 register oh * ohdr = (oh *)GC_base(p);
306 GC_err_printf2("0x%lx in object at 0x%lx(", (unsigned long)clobbered_addr,
308 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
309 || ohdr -> oh_string == 0) {
310 GC_err_printf1("<smashed>, appr. sz = %ld)\n",
311 (GC_size((ptr_t)ohdr) - DEBUG_BYTES));
313 if (ohdr -> oh_string[0] == '\0') {
314 GC_err_puts("EMPTY(smashed?)");
316 GC_err_puts(ohdr -> oh_string);
318 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
319 (unsigned long)(ohdr -> oh_sz));
320 PRINT_CALL_CHAIN(ohdr);
324 void GC_check_heap_proc();
326 void GC_start_debugging()
328 GC_check_heap = GC_check_heap_proc;
329 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
330 GC_debugging_started = TRUE;
331 GC_register_displacement((word)sizeof(oh));
334 # if defined(__STDC__) || defined(__cplusplus)
335 void GC_debug_register_displacement(GC_word offset)
337 void GC_debug_register_displacement(offset)
341 GC_register_displacement(offset);
342 GC_register_displacement((word)sizeof(oh) + offset);
345 # ifdef GC_ADD_CALLER
346 # define EXTRA_ARGS word ra, CONST char * s, int i
349 # define EXTRA_ARGS CONST char * s, int i
354 GC_PTR GC_debug_malloc(size_t lb, EXTRA_ARGS)
356 GC_PTR GC_debug_malloc(lb, s, i)
360 # ifdef GC_ADD_CALLER
361 --> GC_ADD_CALLER not implemented for K&R C
365 GC_PTR result = GC_malloc(lb + DEBUG_BYTES);
368 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
371 GC_err_printf1(":%ld)\n", (unsigned long)i);
374 if (!GC_debugging_started) {
375 GC_start_debugging();
377 ADD_CALL_CHAIN(result, ra);
378 return (GC_store_debug_info(result, (word)lb, s, (word)i));
382 GC_PTR GC_debug_generic_malloc(size_t lb, int k, EXTRA_ARGS)
384 GC_PTR GC_debug_malloc(lb, k, s, i)
389 # ifdef GC_ADD_CALLER
390 --> GC_ADD_CALLER not implemented for K&R C
394 GC_PTR result = GC_generic_malloc(lb + DEBUG_BYTES, k);
397 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
400 GC_err_printf1(":%ld)\n", (unsigned long)i);
403 if (!GC_debugging_started) {
404 GC_start_debugging();
406 ADD_CALL_CHAIN(result, ra);
407 return (GC_store_debug_info(result, (word)lb, s, (word)i));
410 #ifdef STUBBORN_ALLOC
412 GC_PTR GC_debug_malloc_stubborn(size_t lb, EXTRA_ARGS)
414 GC_PTR GC_debug_malloc_stubborn(lb, s, i)
420 GC_PTR result = GC_malloc_stubborn(lb + DEBUG_BYTES);
423 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
426 GC_err_printf1(":%ld)\n", (unsigned long)i);
429 if (!GC_debugging_started) {
430 GC_start_debugging();
432 ADD_CALL_CHAIN(result, ra);
433 return (GC_store_debug_info(result, (word)lb, s, (word)i));
436 void GC_debug_change_stubborn(p)
439 register GC_PTR q = GC_base(p);
443 GC_err_printf1("Bad argument: 0x%lx to GC_debug_change_stubborn\n",
445 ABORT("GC_debug_change_stubborn: bad arg");
448 if (hhdr -> hb_obj_kind != STUBBORN) {
449 GC_err_printf1("GC_debug_change_stubborn arg not stubborn: 0x%lx\n",
451 ABORT("GC_debug_change_stubborn: arg not stubborn");
453 GC_change_stubborn(q);
456 void GC_debug_end_stubborn_change(p)
459 register GC_PTR q = GC_base(p);
463 GC_err_printf1("Bad argument: 0x%lx to GC_debug_end_stubborn_change\n",
465 ABORT("GC_debug_end_stubborn_change: bad arg");
468 if (hhdr -> hb_obj_kind != STUBBORN) {
469 GC_err_printf1("debug_end_stubborn_change arg not stubborn: 0x%lx\n",
471 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
473 GC_end_stubborn_change(q);
476 #endif /* STUBBORN_ALLOC */
479 GC_PTR GC_debug_malloc_atomic(size_t lb, EXTRA_ARGS)
481 GC_PTR GC_debug_malloc_atomic(lb, s, i)
487 GC_PTR result = GC_malloc_atomic(lb + DEBUG_BYTES);
490 GC_err_printf1("GC_debug_malloc_atomic(%ld) returning NIL (",
493 GC_err_printf1(":%ld)\n", (unsigned long)i);
496 if (!GC_debugging_started) {
497 GC_start_debugging();
499 ADD_CALL_CHAIN(result, ra);
500 return (GC_store_debug_info(result, (word)lb, s, (word)i));
504 GC_PTR GC_debug_malloc_uncollectable(size_t lb, EXTRA_ARGS)
506 GC_PTR GC_debug_malloc_uncollectable(lb, s, i)
512 GC_PTR result = GC_malloc_uncollectable(lb + DEBUG_BYTES);
515 GC_err_printf1("GC_debug_malloc_uncollectable(%ld) returning NIL (",
518 GC_err_printf1(":%ld)\n", (unsigned long)i);
521 if (!GC_debugging_started) {
522 GC_start_debugging();
524 ADD_CALL_CHAIN(result, ra);
525 return (GC_store_debug_info(result, (word)lb, s, (word)i));
528 #ifdef ATOMIC_UNCOLLECTABLE
530 GC_PTR GC_debug_malloc_atomic_uncollectable(size_t lb, EXTRA_ARGS)
532 GC_PTR GC_debug_malloc_atomic_uncollectable(lb, s, i)
538 GC_PTR result = GC_malloc_atomic_uncollectable(lb + DEBUG_BYTES);
542 "GC_debug_malloc_atomic_uncollectable(%ld) returning NIL (",
545 GC_err_printf1(":%ld)\n", (unsigned long)i);
548 if (!GC_debugging_started) {
549 GC_start_debugging();
551 ADD_CALL_CHAIN(result, ra);
552 return (GC_store_debug_info(result, (word)lb, s, (word)i));
554 #endif /* ATOMIC_UNCOLLECTABLE */
557 void GC_debug_free(GC_PTR p)
559 void GC_debug_free(p)
563 register GC_PTR base;
564 register ptr_t clobbered;
569 GC_err_printf1("Attempt to free invalid pointer %lx\n",
571 ABORT("free(invalid pointer)");
573 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
575 "GC_debug_free called on pointer %lx wo debugging info\n",
578 clobbered = GC_check_annotated_obj((oh *)base);
579 if (clobbered != 0) {
580 if (((oh *)base) -> oh_sz == GC_size(base)) {
582 "GC_debug_free: found previously deallocated (?) object at ");
584 GC_err_printf0("GC_debug_free: found smashed location at ");
586 GC_print_smashed_obj(p, clobbered);
588 /* Invalidate size */
589 ((oh *)base) -> oh_sz = GC_size(base);
594 register hdr * hhdr = HDR(p);
595 GC_bool uncollectable = FALSE;
597 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
598 uncollectable = TRUE;
600 # ifdef ATOMIC_UNCOLLECTABLE
601 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
602 uncollectable = TRUE;
605 if (uncollectable) GC_free(base);
606 } /* !GC_find_leak */
610 GC_PTR GC_debug_realloc(GC_PTR p, size_t lb, EXTRA_ARGS)
612 GC_PTR GC_debug_realloc(p, lb, s, i)
619 register GC_PTR base = GC_base(p);
620 register ptr_t clobbered;
621 register GC_PTR result;
622 register size_t copy_sz = lb;
623 register size_t old_sz;
626 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
629 "Attempt to reallocate invalid pointer %lx\n", (unsigned long)p);
630 ABORT("realloc(invalid pointer)");
632 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
634 "GC_debug_realloc called on pointer %lx wo debugging info\n",
636 return(GC_realloc(p, lb));
639 switch (hhdr -> hb_obj_kind) {
640 # ifdef STUBBORN_ALLOC
642 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
646 result = GC_debug_malloc(lb, OPT_RA s, i);
649 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
652 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
654 # ifdef ATOMIC_UNCOLLECTABLE
656 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
660 GC_err_printf0("GC_debug_realloc: encountered bad kind\n");
663 clobbered = GC_check_annotated_obj((oh *)base);
664 if (clobbered != 0) {
665 GC_err_printf0("GC_debug_realloc: found smashed location at ");
666 GC_print_smashed_obj(p, clobbered);
668 old_sz = ((oh *)base) -> oh_sz;
669 if (old_sz < copy_sz) copy_sz = old_sz;
670 if (result == 0) return(0);
671 BCOPY(p, result, copy_sz);
676 /* Check all marked objects in the given block for validity */
678 void GC_check_heap_block(hbp, dummy)
679 register struct hblk *hbp; /* ptr to current heap block */
682 register struct hblkhdr * hhdr = HDR(hbp);
683 register word sz = hhdr -> hb_sz;
684 register int word_no;
685 register word *p, *plim;
687 p = (word *)(hbp->hb_body);
692 plim = (word *)((((word)hbp) + HBLKSIZE) - WORDS_TO_BYTES(sz));
694 /* go through all words in block */
696 if( mark_bit_from_hdr(hhdr, word_no)
697 && GC_has_debug_info((ptr_t)p)) {
698 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
700 if (clobbered != 0) {
702 "GC_check_heap_block: found smashed location at ");
703 GC_print_smashed_obj((ptr_t)p, clobbered);
712 /* This assumes that all accessible objects are marked, and that */
713 /* I hold the allocation lock. Normally called by collector. */
714 void GC_check_heap_proc()
716 # ifndef SMALL_CONFIG
717 if (sizeof(oh) & (2 * sizeof(word) - 1) != 0) {
718 ABORT("Alignment problem: object header has inappropriate size\n");
721 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
725 GC_finalization_proc cl_fn;
730 void * GC_make_closure(GC_finalization_proc fn, void * data)
732 GC_PTR GC_make_closure(fn, data)
733 GC_finalization_proc fn;
737 struct closure * result =
738 (struct closure *) GC_malloc(sizeof (struct closure));
740 result -> cl_fn = fn;
741 result -> cl_data = data;
742 return((GC_PTR)result);
746 void GC_debug_invoke_finalizer(void * obj, void * data)
748 void GC_debug_invoke_finalizer(obj, data)
753 register struct closure * cl = (struct closure *) data;
755 (*(cl -> cl_fn))((GC_PTR)((char *)obj + sizeof(oh)), cl -> cl_data);
760 void GC_debug_register_finalizer(GC_PTR obj, GC_finalization_proc fn,
761 GC_PTR cd, GC_finalization_proc *ofn,
764 void GC_debug_register_finalizer(obj, fn, cd, ofn, ocd)
766 GC_finalization_proc fn;
768 GC_finalization_proc *ofn;
772 ptr_t base = GC_base(obj);
773 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
775 "GC_register_finalizer called with non-base-pointer 0x%lx\n",
778 GC_register_finalizer(base, GC_debug_invoke_finalizer,
779 GC_make_closure(fn,cd), ofn, ocd);
783 void GC_debug_register_finalizer_no_order
784 (GC_PTR obj, GC_finalization_proc fn,
785 GC_PTR cd, GC_finalization_proc *ofn,
788 void GC_debug_register_finalizer_no_order
789 (obj, fn, cd, ofn, ocd)
791 GC_finalization_proc fn;
793 GC_finalization_proc *ofn;
797 ptr_t base = GC_base(obj);
798 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
800 "GC_register_finalizer_no_order called with non-base-pointer 0x%lx\n",
803 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
804 GC_make_closure(fn,cd), ofn, ocd);
808 void GC_debug_register_finalizer_ignore_self
809 (GC_PTR obj, GC_finalization_proc fn,
810 GC_PTR cd, GC_finalization_proc *ofn,
813 void GC_debug_register_finalizer_no_order
814 (obj, fn, cd, ofn, ocd)
816 GC_finalization_proc fn;
818 GC_finalization_proc *ofn;
822 ptr_t base = GC_base(obj);
823 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
825 "GC_register_finalizer_no_order called with non-base-pointer 0x%lx\n",
828 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
829 GC_make_closure(fn,cd), ofn, ocd);