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 by Hewlett-Packard Company. All rights reserved.
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
19 void GC_default_print_heap_obj_proc();
20 GC_API void GC_register_finalizer_no_order
21 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
22 GC_finalization_proc *ofn, GC_PTR *ocd));
25 /* Check whether object with base pointer p has debugging info */
26 /* p is assumed to point to a legitimate object in our part */
28 GC_bool GC_has_debug_info(p)
31 register oh * ohdr = (oh *)p;
32 register ptr_t body = (ptr_t)(ohdr + 1);
33 register word sz = GC_size((ptr_t) ohdr);
35 if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
36 || sz < sizeof (oh)) {
39 if (ohdr -> oh_sz == sz) {
40 /* Object may have had debug info, but has been deallocated */
43 if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
44 if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
51 /* Store back pointer to source in dest, if that appears to be possible. */
52 /* This is not completely safe, since we may mistakenly conclude that */
53 /* dest has a debugging wrapper. But the error probability is very */
54 /* small, and this shouldn't be used in production code. */
55 /* We assume that dest is the real base pointer. Source will usually */
56 /* be a pointer to the interior of an object. */
57 void GC_store_back_pointer(ptr_t source, ptr_t dest)
59 if (GC_has_debug_info(dest)) {
60 ((oh *)dest) -> oh_back_ptr = (ptr_t)HIDE_POINTER(source);
64 void GC_marked_for_finalization(ptr_t dest) {
65 GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
68 /* Store information about the object referencing dest in *base_p */
70 /* source is root ==> *base_p = address, *offset_p = 0 */
71 /* source is heap object ==> *base_p != 0, *offset_p = offset */
72 /* Returns 1 on success, 0 if source couldn't be determined. */
73 /* Dest can be any address within a heap object. */
74 GC_ref_kind GC_get_back_ptr_info(void *dest, void **base_p, size_t *offset_p)
76 oh * hdr = (oh *)GC_base(dest);
79 if (!GC_has_debug_info((ptr_t) hdr)) return GC_NO_SPACE;
80 bp = hdr -> oh_back_ptr;
81 if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
82 if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
83 if (0 == bp) return GC_UNREFERENCED;
84 bp = REVEAL_POINTER(bp);
85 bp_base = GC_base(bp);
89 return GC_REFD_FROM_ROOT;
91 if (GC_has_debug_info(bp_base)) bp_base += sizeof(oh);
93 *offset_p = bp - bp_base;
94 return GC_REFD_FROM_HEAP;
98 /* Generate a random heap address. */
99 /* The resulting address is in the heap, but */
100 /* not necessarily inside a valid object. */
101 void *GC_generate_random_heap_address(void)
104 int heap_offset = random() % GC_heapsize;
105 for (i = 0; i < GC_n_heap_sects; ++ i) {
106 int size = GC_heap_sects[i].hs_bytes;
107 if (heap_offset < size) {
108 return GC_heap_sects[i].hs_start + heap_offset;
113 ABORT("GC_generate_random_heap_address: size inconsistency");
118 /* Generate a random address inside a valid marked heap object. */
119 void *GC_generate_random_valid_address(void)
124 result = GC_generate_random_heap_address();
125 base = GC_base(result);
126 if (0 == base) continue;
127 if (!GC_is_marked(base)) continue;
132 /* Print back trace for p */
133 void GC_print_backtrace(void *p)
141 GC_print_heap_obj(GC_base(current));
142 GC_err_printf0("\n");
144 source = GC_get_back_ptr_info(current, &base, &offset);
145 if (GC_UNREFERENCED == source) {
146 GC_err_printf0("Reference could not be found\n");
149 if (GC_NO_SPACE == source) {
150 GC_err_printf0("No debug info in object: Can't find reference\n");
153 GC_err_printf1("Reachable via %d levels of pointers from ",
156 case GC_REFD_FROM_ROOT:
157 GC_err_printf1("root at 0x%lx\n", (unsigned long)base);
159 case GC_REFD_FROM_REG:
160 GC_err_printf0("root in register\n");
162 case GC_FINALIZER_REFD:
163 GC_err_printf0("list of finalizable objects\n");
165 case GC_REFD_FROM_HEAP:
166 GC_err_printf1("offset %ld in object:\n", (unsigned long)offset);
167 /* Take GC_base(base) to get real base, i.e. header. */
168 GC_print_heap_obj(GC_base(base));
169 GC_err_printf0("\n");
177 /* Force a garbage collection and generate a backtrace from a */
178 /* random heap address. */
179 void GC_generate_random_backtrace(void)
183 current = GC_generate_random_valid_address();
184 GC_printf1("Chose address 0x%lx in object\n", (unsigned long)current);
185 GC_print_backtrace(current);
188 #endif /* KEEP_BACK_PTRS */
190 /* Store debugging info into p. Return displaced pointer. */
191 /* Assumes we don't hold allocation lock. */
192 ptr_t GC_store_debug_info(p, sz, string, integer)
193 register ptr_t p; /* base pointer */
198 register word * result = (word *)((oh *)p + 1);
201 /* There is some argument that we should dissble signals here. */
202 /* But that's expensive. And this way things should only appear */
203 /* inconsistent while we're in the handler. */
205 # ifdef KEEP_BACK_PTRS
206 ((oh *)p) -> oh_back_ptr = 0;
208 ((oh *)p) -> oh_string = string;
209 ((oh *)p) -> oh_int = integer;
210 ((oh *)p) -> oh_sz = sz;
211 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
212 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
213 result[ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
215 return((ptr_t)result);
218 /* Check the object with debugging info at ohdr */
219 /* return NIL if it's OK. Else return clobbered */
221 ptr_t GC_check_annotated_obj(ohdr)
224 register ptr_t body = (ptr_t)(ohdr + 1);
225 register word gc_sz = GC_size((ptr_t)ohdr);
226 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
227 return((ptr_t)(&(ohdr -> oh_sz)));
229 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
230 return((ptr_t)(&(ohdr -> oh_sf)));
232 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
233 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
235 if (((word *)body)[ROUNDED_UP_WORDS(ohdr -> oh_sz)]
236 != (END_FLAG ^ (word)body)) {
237 return((ptr_t)((word *)body + ROUNDED_UP_WORDS(ohdr -> oh_sz)));
245 register oh * ohdr = (oh *)GC_base(p);
247 GC_err_printf1("0x%lx (", ((unsigned long)ohdr + sizeof(oh)));
248 GC_err_puts(ohdr -> oh_string);
249 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
250 (unsigned long)(ohdr -> oh_sz));
251 PRINT_CALL_CHAIN(ohdr);
254 void GC_debug_print_heap_obj_proc(p)
257 if (GC_has_debug_info(p)) {
260 GC_default_print_heap_obj_proc(p);
264 void GC_print_smashed_obj(p, clobbered_addr)
265 ptr_t p, clobbered_addr;
267 register oh * ohdr = (oh *)GC_base(p);
269 GC_err_printf2("0x%lx in object at 0x%lx(", (unsigned long)clobbered_addr,
271 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
272 || ohdr -> oh_string == 0) {
273 GC_err_printf1("<smashed>, appr. sz = %ld)\n",
274 (GC_size((ptr_t)ohdr) - DEBUG_BYTES));
276 if (ohdr -> oh_string[0] == '\0') {
277 GC_err_puts("EMPTY(smashed?)");
279 GC_err_puts(ohdr -> oh_string);
281 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
282 (unsigned long)(ohdr -> oh_sz));
283 PRINT_CALL_CHAIN(ohdr);
287 void GC_check_heap_proc();
289 void GC_start_debugging()
291 GC_check_heap = GC_check_heap_proc;
292 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
293 GC_debugging_started = TRUE;
294 GC_register_displacement((word)sizeof(oh));
297 # if defined(__STDC__) || defined(__cplusplus)
298 void GC_debug_register_displacement(GC_word offset)
300 void GC_debug_register_displacement(offset)
304 GC_register_displacement(offset);
305 GC_register_displacement((word)sizeof(oh) + offset);
309 GC_PTR GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS)
311 GC_PTR GC_debug_malloc(lb, s, i)
315 # ifdef GC_ADD_CALLER
316 --> GC_ADD_CALLER not implemented for K&R C
320 GC_PTR result = GC_malloc(lb + DEBUG_BYTES);
323 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
326 GC_err_printf1(":%ld)\n", (unsigned long)i);
329 if (!GC_debugging_started) {
330 GC_start_debugging();
332 ADD_CALL_CHAIN(result, ra);
333 return (GC_store_debug_info(result, (word)lb, s, (word)i));
337 GC_PTR GC_debug_generic_malloc(size_t lb, int k, GC_EXTRA_PARAMS)
339 GC_PTR GC_debug_malloc(lb, k, s, i)
344 # ifdef GC_ADD_CALLER
345 --> GC_ADD_CALLER not implemented for K&R C
349 GC_PTR result = GC_generic_malloc(lb + DEBUG_BYTES, k);
352 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
355 GC_err_printf1(":%ld)\n", (unsigned long)i);
358 if (!GC_debugging_started) {
359 GC_start_debugging();
361 ADD_CALL_CHAIN(result, ra);
362 return (GC_store_debug_info(result, (word)lb, s, (word)i));
365 #ifdef STUBBORN_ALLOC
367 GC_PTR GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
369 GC_PTR GC_debug_malloc_stubborn(lb, s, i)
375 GC_PTR result = GC_malloc_stubborn(lb + DEBUG_BYTES);
378 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
381 GC_err_printf1(":%ld)\n", (unsigned long)i);
384 if (!GC_debugging_started) {
385 GC_start_debugging();
387 ADD_CALL_CHAIN(result, ra);
388 return (GC_store_debug_info(result, (word)lb, s, (word)i));
391 void GC_debug_change_stubborn(p)
394 register GC_PTR q = GC_base(p);
398 GC_err_printf1("Bad argument: 0x%lx to GC_debug_change_stubborn\n",
400 ABORT("GC_debug_change_stubborn: bad arg");
403 if (hhdr -> hb_obj_kind != STUBBORN) {
404 GC_err_printf1("GC_debug_change_stubborn arg not stubborn: 0x%lx\n",
406 ABORT("GC_debug_change_stubborn: arg not stubborn");
408 GC_change_stubborn(q);
411 void GC_debug_end_stubborn_change(p)
414 register GC_PTR q = GC_base(p);
418 GC_err_printf1("Bad argument: 0x%lx to GC_debug_end_stubborn_change\n",
420 ABORT("GC_debug_end_stubborn_change: bad arg");
423 if (hhdr -> hb_obj_kind != STUBBORN) {
424 GC_err_printf1("debug_end_stubborn_change arg not stubborn: 0x%lx\n",
426 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
428 GC_end_stubborn_change(q);
431 #else /* !STUBBORN_ALLOC */
434 GC_PTR GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
436 GC_PTR GC_debug_malloc_stubborn(lb, s, i)
442 return GC_debug_malloc(lb, OPT_RA s, i);
445 void GC_debug_change_stubborn(p)
450 void GC_debug_end_stubborn_change(p)
455 #endif /* !STUBBORN_ALLOC */
458 GC_PTR GC_debug_malloc_atomic(size_t lb, GC_EXTRA_PARAMS)
460 GC_PTR GC_debug_malloc_atomic(lb, s, i)
466 GC_PTR result = GC_malloc_atomic(lb + DEBUG_BYTES);
469 GC_err_printf1("GC_debug_malloc_atomic(%ld) returning NIL (",
472 GC_err_printf1(":%ld)\n", (unsigned long)i);
475 if (!GC_debugging_started) {
476 GC_start_debugging();
478 ADD_CALL_CHAIN(result, ra);
479 return (GC_store_debug_info(result, (word)lb, s, (word)i));
483 GC_PTR GC_debug_malloc_uncollectable(size_t lb, GC_EXTRA_PARAMS)
485 GC_PTR GC_debug_malloc_uncollectable(lb, s, i)
491 GC_PTR result = GC_malloc_uncollectable(lb + DEBUG_BYTES);
494 GC_err_printf1("GC_debug_malloc_uncollectable(%ld) returning NIL (",
497 GC_err_printf1(":%ld)\n", (unsigned long)i);
500 if (!GC_debugging_started) {
501 GC_start_debugging();
503 ADD_CALL_CHAIN(result, ra);
504 return (GC_store_debug_info(result, (word)lb, s, (word)i));
507 #ifdef ATOMIC_UNCOLLECTABLE
509 GC_PTR GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
511 GC_PTR GC_debug_malloc_atomic_uncollectable(lb, s, i)
517 GC_PTR result = GC_malloc_atomic_uncollectable(lb + DEBUG_BYTES);
521 "GC_debug_malloc_atomic_uncollectable(%ld) returning NIL (",
524 GC_err_printf1(":%ld)\n", (unsigned long)i);
527 if (!GC_debugging_started) {
528 GC_start_debugging();
530 ADD_CALL_CHAIN(result, ra);
531 return (GC_store_debug_info(result, (word)lb, s, (word)i));
533 #endif /* ATOMIC_UNCOLLECTABLE */
536 void GC_debug_free(GC_PTR p)
538 void GC_debug_free(p)
542 register GC_PTR base;
543 register ptr_t clobbered;
548 GC_err_printf1("Attempt to free invalid pointer %lx\n",
550 ABORT("free(invalid pointer)");
552 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
554 "GC_debug_free called on pointer %lx wo debugging info\n",
557 clobbered = GC_check_annotated_obj((oh *)base);
558 if (clobbered != 0) {
559 if (((oh *)base) -> oh_sz == GC_size(base)) {
561 "GC_debug_free: found previously deallocated (?) object at ");
563 GC_err_printf0("GC_debug_free: found smashed location at ");
565 GC_print_smashed_obj(p, clobbered);
567 /* Invalidate size */
568 ((oh *)base) -> oh_sz = GC_size(base);
573 register hdr * hhdr = HDR(p);
574 GC_bool uncollectable = FALSE;
576 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
577 uncollectable = TRUE;
579 # ifdef ATOMIC_UNCOLLECTABLE
580 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
581 uncollectable = TRUE;
584 if (uncollectable) GC_free(base);
585 } /* !GC_find_leak */
589 GC_PTR GC_debug_realloc(GC_PTR p, size_t lb, GC_EXTRA_PARAMS)
591 GC_PTR GC_debug_realloc(p, lb, s, i)
598 register GC_PTR base = GC_base(p);
599 register ptr_t clobbered;
600 register GC_PTR result;
601 register size_t copy_sz = lb;
602 register size_t old_sz;
605 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
608 "Attempt to reallocate invalid pointer %lx\n", (unsigned long)p);
609 ABORT("realloc(invalid pointer)");
611 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
613 "GC_debug_realloc called on pointer %lx wo debugging info\n",
615 return(GC_realloc(p, lb));
618 switch (hhdr -> hb_obj_kind) {
619 # ifdef STUBBORN_ALLOC
621 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
625 result = GC_debug_malloc(lb, OPT_RA s, i);
628 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
631 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
633 # ifdef ATOMIC_UNCOLLECTABLE
635 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
639 GC_err_printf0("GC_debug_realloc: encountered bad kind\n");
642 clobbered = GC_check_annotated_obj((oh *)base);
643 if (clobbered != 0) {
644 GC_err_printf0("GC_debug_realloc: found smashed location at ");
645 GC_print_smashed_obj(p, clobbered);
647 old_sz = ((oh *)base) -> oh_sz;
648 if (old_sz < copy_sz) copy_sz = old_sz;
649 if (result == 0) return(0);
650 BCOPY(p, result, copy_sz);
655 /* Check all marked objects in the given block for validity */
657 void GC_check_heap_block(hbp, dummy)
658 register struct hblk *hbp; /* ptr to current heap block */
661 register struct hblkhdr * hhdr = HDR(hbp);
662 register word sz = hhdr -> hb_sz;
663 register int word_no;
664 register word *p, *plim;
666 p = (word *)(hbp->hb_body);
671 plim = (word *)((((word)hbp) + HBLKSIZE) - WORDS_TO_BYTES(sz));
673 /* go through all words in block */
675 if( mark_bit_from_hdr(hhdr, word_no)
676 && GC_has_debug_info((ptr_t)p)) {
677 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
679 if (clobbered != 0) {
681 "GC_check_heap_block: found smashed location at ");
682 GC_print_smashed_obj((ptr_t)p, clobbered);
691 /* This assumes that all accessible objects are marked, and that */
692 /* I hold the allocation lock. Normally called by collector. */
693 void GC_check_heap_proc()
695 # ifndef SMALL_CONFIG
696 if (sizeof(oh) & (2 * sizeof(word) - 1) != 0) {
697 ABORT("Alignment problem: object header has inappropriate size\n");
700 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
704 GC_finalization_proc cl_fn;
709 void * GC_make_closure(GC_finalization_proc fn, void * data)
711 GC_PTR GC_make_closure(fn, data)
712 GC_finalization_proc fn;
716 struct closure * result =
717 (struct closure *) GC_malloc(sizeof (struct closure));
719 result -> cl_fn = fn;
720 result -> cl_data = data;
721 return((GC_PTR)result);
725 void GC_debug_invoke_finalizer(void * obj, void * data)
727 void GC_debug_invoke_finalizer(obj, data)
732 register struct closure * cl = (struct closure *) data;
734 (*(cl -> cl_fn))((GC_PTR)((char *)obj + sizeof(oh)), cl -> cl_data);
739 void GC_debug_register_finalizer(GC_PTR obj, GC_finalization_proc fn,
740 GC_PTR cd, GC_finalization_proc *ofn,
743 void GC_debug_register_finalizer(obj, fn, cd, ofn, ocd)
745 GC_finalization_proc fn;
747 GC_finalization_proc *ofn;
751 ptr_t base = GC_base(obj);
752 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
754 "GC_register_finalizer called with non-base-pointer 0x%lx\n",
757 GC_register_finalizer(base, GC_debug_invoke_finalizer,
758 GC_make_closure(fn,cd), ofn, ocd);
762 void GC_debug_register_finalizer_no_order
763 (GC_PTR obj, GC_finalization_proc fn,
764 GC_PTR cd, GC_finalization_proc *ofn,
767 void GC_debug_register_finalizer_no_order
768 (obj, fn, cd, ofn, ocd)
770 GC_finalization_proc fn;
772 GC_finalization_proc *ofn;
776 ptr_t base = GC_base(obj);
777 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
779 "GC_register_finalizer_no_order called with non-base-pointer 0x%lx\n",
782 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
783 GC_make_closure(fn,cd), ofn, ocd);
787 void GC_debug_register_finalizer_ignore_self
788 (GC_PTR obj, GC_finalization_proc fn,
789 GC_PTR cd, GC_finalization_proc *ofn,
792 void GC_debug_register_finalizer_ignore_self
793 (obj, fn, cd, ofn, ocd)
795 GC_finalization_proc fn;
797 GC_finalization_proc *ofn;
801 ptr_t base = GC_base(obj);
802 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
804 "GC_register_finalizer_ignore_self called with non-base-pointer 0x%lx\n",
807 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
808 GC_make_closure(fn,cd), ofn, ocd);