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));
381 #ifdef STUBBORN_ALLOC
383 GC_PTR GC_debug_malloc_stubborn(size_t lb, EXTRA_ARGS)
385 GC_PTR GC_debug_malloc_stubborn(lb, s, i)
391 GC_PTR result = GC_malloc_stubborn(lb + DEBUG_BYTES);
394 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
397 GC_err_printf1(":%ld)\n", (unsigned long)i);
400 if (!GC_debugging_started) {
401 GC_start_debugging();
403 ADD_CALL_CHAIN(result, ra);
404 return (GC_store_debug_info(result, (word)lb, s, (word)i));
407 void GC_debug_change_stubborn(p)
410 register GC_PTR q = GC_base(p);
414 GC_err_printf1("Bad argument: 0x%lx to GC_debug_change_stubborn\n",
416 ABORT("GC_debug_change_stubborn: bad arg");
419 if (hhdr -> hb_obj_kind != STUBBORN) {
420 GC_err_printf1("GC_debug_change_stubborn arg not stubborn: 0x%lx\n",
422 ABORT("GC_debug_change_stubborn: arg not stubborn");
424 GC_change_stubborn(q);
427 void GC_debug_end_stubborn_change(p)
430 register GC_PTR q = GC_base(p);
434 GC_err_printf1("Bad argument: 0x%lx to GC_debug_end_stubborn_change\n",
436 ABORT("GC_debug_end_stubborn_change: bad arg");
439 if (hhdr -> hb_obj_kind != STUBBORN) {
440 GC_err_printf1("debug_end_stubborn_change arg not stubborn: 0x%lx\n",
442 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
444 GC_end_stubborn_change(q);
447 #endif /* STUBBORN_ALLOC */
450 GC_PTR GC_debug_malloc_atomic(size_t lb, EXTRA_ARGS)
452 GC_PTR GC_debug_malloc_atomic(lb, s, i)
458 GC_PTR result = GC_malloc_atomic(lb + DEBUG_BYTES);
461 GC_err_printf1("GC_debug_malloc_atomic(%ld) returning NIL (",
464 GC_err_printf1(":%ld)\n", (unsigned long)i);
467 if (!GC_debugging_started) {
468 GC_start_debugging();
470 ADD_CALL_CHAIN(result, ra);
471 return (GC_store_debug_info(result, (word)lb, s, (word)i));
475 GC_PTR GC_debug_malloc_uncollectable(size_t lb, EXTRA_ARGS)
477 GC_PTR GC_debug_malloc_uncollectable(lb, s, i)
483 GC_PTR result = GC_malloc_uncollectable(lb + DEBUG_BYTES);
486 GC_err_printf1("GC_debug_malloc_uncollectable(%ld) returning NIL (",
489 GC_err_printf1(":%ld)\n", (unsigned long)i);
492 if (!GC_debugging_started) {
493 GC_start_debugging();
495 ADD_CALL_CHAIN(result, ra);
496 return (GC_store_debug_info(result, (word)lb, s, (word)i));
499 #ifdef ATOMIC_UNCOLLECTABLE
501 GC_PTR GC_debug_malloc_atomic_uncollectable(size_t lb, EXTRA_ARGS)
503 GC_PTR GC_debug_malloc_atomic_uncollectable(lb, s, i)
509 GC_PTR result = GC_malloc_atomic_uncollectable(lb + DEBUG_BYTES);
513 "GC_debug_malloc_atomic_uncollectable(%ld) returning NIL (",
516 GC_err_printf1(":%ld)\n", (unsigned long)i);
519 if (!GC_debugging_started) {
520 GC_start_debugging();
522 ADD_CALL_CHAIN(result, ra);
523 return (GC_store_debug_info(result, (word)lb, s, (word)i));
525 #endif /* ATOMIC_UNCOLLECTABLE */
528 void GC_debug_free(GC_PTR p)
530 void GC_debug_free(p)
534 register GC_PTR base;
535 register ptr_t clobbered;
540 GC_err_printf1("Attempt to free invalid pointer %lx\n",
542 ABORT("free(invalid pointer)");
544 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
546 "GC_debug_free called on pointer %lx wo debugging info\n",
549 clobbered = GC_check_annotated_obj((oh *)base);
550 if (clobbered != 0) {
551 if (((oh *)base) -> oh_sz == GC_size(base)) {
553 "GC_debug_free: found previously deallocated (?) object at ");
555 GC_err_printf0("GC_debug_free: found smashed location at ");
557 GC_print_smashed_obj(p, clobbered);
559 /* Invalidate size */
560 ((oh *)base) -> oh_sz = GC_size(base);
565 register hdr * hhdr = HDR(p);
566 GC_bool uncollectable = FALSE;
568 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
569 uncollectable = TRUE;
571 # ifdef ATOMIC_UNCOLLECTABLE
572 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
573 uncollectable = TRUE;
576 if (uncollectable) GC_free(base);
577 } /* !GC_find_leak */
581 GC_PTR GC_debug_realloc(GC_PTR p, size_t lb, EXTRA_ARGS)
583 GC_PTR GC_debug_realloc(p, lb, s, i)
590 register GC_PTR base = GC_base(p);
591 register ptr_t clobbered;
592 register GC_PTR result;
593 register size_t copy_sz = lb;
594 register size_t old_sz;
597 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
600 "Attempt to reallocate invalid pointer %lx\n", (unsigned long)p);
601 ABORT("realloc(invalid pointer)");
603 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
605 "GC_debug_realloc called on pointer %lx wo debugging info\n",
607 return(GC_realloc(p, lb));
610 switch (hhdr -> hb_obj_kind) {
611 # ifdef STUBBORN_ALLOC
613 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
617 result = GC_debug_malloc(lb, OPT_RA s, i);
620 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
623 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
625 # ifdef ATOMIC_UNCOLLECTABLE
627 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
631 GC_err_printf0("GC_debug_realloc: encountered bad kind\n");
634 clobbered = GC_check_annotated_obj((oh *)base);
635 if (clobbered != 0) {
636 GC_err_printf0("GC_debug_realloc: found smashed location at ");
637 GC_print_smashed_obj(p, clobbered);
639 old_sz = ((oh *)base) -> oh_sz;
640 if (old_sz < copy_sz) copy_sz = old_sz;
641 if (result == 0) return(0);
642 BCOPY(p, result, copy_sz);
647 /* Check all marked objects in the given block for validity */
649 void GC_check_heap_block(hbp, dummy)
650 register struct hblk *hbp; /* ptr to current heap block */
653 register struct hblkhdr * hhdr = HDR(hbp);
654 register word sz = hhdr -> hb_sz;
655 register int word_no;
656 register word *p, *plim;
658 p = (word *)(hbp->hb_body);
663 plim = (word *)((((word)hbp) + HBLKSIZE) - WORDS_TO_BYTES(sz));
665 /* go through all words in block */
667 if( mark_bit_from_hdr(hhdr, word_no)
668 && GC_has_debug_info((ptr_t)p)) {
669 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
671 if (clobbered != 0) {
673 "GC_check_heap_block: found smashed location at ");
674 GC_print_smashed_obj((ptr_t)p, clobbered);
683 /* This assumes that all accessible objects are marked, and that */
684 /* I hold the allocation lock. Normally called by collector. */
685 void GC_check_heap_proc()
687 # ifndef SMALL_CONFIG
688 if (sizeof(oh) & (2 * sizeof(word) - 1) != 0) {
689 ABORT("Alignment problem: object header has inappropriate size\n");
692 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
696 GC_finalization_proc cl_fn;
701 void * GC_make_closure(GC_finalization_proc fn, void * data)
703 GC_PTR GC_make_closure(fn, data)
704 GC_finalization_proc fn;
708 struct closure * result =
709 (struct closure *) GC_malloc(sizeof (struct closure));
711 result -> cl_fn = fn;
712 result -> cl_data = data;
713 return((GC_PTR)result);
717 void GC_debug_invoke_finalizer(void * obj, void * data)
719 void GC_debug_invoke_finalizer(obj, data)
724 register struct closure * cl = (struct closure *) data;
726 (*(cl -> cl_fn))((GC_PTR)((char *)obj + sizeof(oh)), cl -> cl_data);
731 void GC_debug_register_finalizer(GC_PTR obj, GC_finalization_proc fn,
732 GC_PTR cd, GC_finalization_proc *ofn,
735 void GC_debug_register_finalizer(obj, fn, cd, ofn, ocd)
737 GC_finalization_proc fn;
739 GC_finalization_proc *ofn;
743 ptr_t base = GC_base(obj);
744 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
746 "GC_register_finalizer called with non-base-pointer 0x%lx\n",
749 GC_register_finalizer(base, GC_debug_invoke_finalizer,
750 GC_make_closure(fn,cd), ofn, ocd);
754 void GC_debug_register_finalizer_no_order
755 (GC_PTR obj, GC_finalization_proc fn,
756 GC_PTR cd, GC_finalization_proc *ofn,
759 void GC_debug_register_finalizer_no_order
760 (obj, fn, cd, ofn, ocd)
762 GC_finalization_proc fn;
764 GC_finalization_proc *ofn;
768 ptr_t base = GC_base(obj);
769 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
771 "GC_register_finalizer_no_order called with non-base-pointer 0x%lx\n",
774 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
775 GC_make_closure(fn,cd), ofn, ocd);
779 void GC_debug_register_finalizer_ignore_self
780 (GC_PTR obj, GC_finalization_proc fn,
781 GC_PTR cd, GC_finalization_proc *ofn,
784 void GC_debug_register_finalizer_ignore_self
785 (obj, fn, cd, ofn, ocd)
787 GC_finalization_proc fn;
789 GC_finalization_proc *ofn;
793 ptr_t base = GC_base(obj);
794 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
796 "GC_register_finalizer_ignore_self called with non-base-pointer 0x%lx\n",
799 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
800 GC_make_closure(fn,cd), ofn, ocd);