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 /* Boehm, October 9, 1995 1:16 pm PDT */
18 void GC_default_print_heap_obj_proc();
19 GC_API void GC_register_finalizer_no_order
20 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
21 GC_finalization_proc *ofn, GC_PTR *ocd));
23 /* Do we want to and know how to save the call stack at the time of */
24 /* an allocation? How much space do we want to use in each object? */
26 # define START_FLAG ((word)0xfedcedcb)
27 # define END_FLAG ((word)0xbcdecdef)
28 /* Stored both one past the end of user object, and one before */
29 /* the end of the object as seen by the allocator. */
34 char * oh_string; /* object descriptor string */
35 word oh_int; /* object descriptor integers */
37 struct callinfo oh_ci[NFRAMES];
39 word oh_sz; /* Original malloc arg. */
40 word oh_sf; /* start flag */
42 /* The size of the above structure is assumed not to dealign things, */
43 /* and to be a multiple of the word length. */
45 #define DEBUG_BYTES (sizeof (oh) + sizeof (word))
46 #undef ROUNDED_UP_WORDS
47 #define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + WORDS_TO_BYTES(1) - 1)
50 #ifdef SAVE_CALL_CHAIN
51 # define ADD_CALL_CHAIN(base, ra) GC_save_callers(((oh *)(base)) -> oh_ci)
52 # define PRINT_CALL_CHAIN(base) GC_print_callers(((oh *)(base)) -> oh_ci)
55 # define ADD_CALL_CHAIN(base, ra) ((oh *)(base)) -> oh_ci[0].ci_pc = (ra)
56 # define PRINT_CALL_CHAIN(base) GC_print_callers(((oh *)(base)) -> oh_ci)
58 # define ADD_CALL_CHAIN(base, ra)
59 # define PRINT_CALL_CHAIN(base)
63 /* Check whether object with base pointer p has debugging info */
64 /* p is assumed to point to a legitimate object in our part */
66 GC_bool GC_has_debug_info(p)
69 register oh * ohdr = (oh *)p;
70 register ptr_t body = (ptr_t)(ohdr + 1);
71 register word sz = GC_size((ptr_t) ohdr);
73 if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
74 || sz < sizeof (oh)) {
77 if (ohdr -> oh_sz == sz) {
78 /* Object may have had debug info, but has been deallocated */
81 if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
82 if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
88 /* Store debugging info into p. Return displaced pointer. */
89 /* Assumes we don't hold allocation lock. */
90 ptr_t GC_store_debug_info(p, sz, string, integer)
91 register ptr_t p; /* base pointer */
96 register word * result = (word *)((oh *)p + 1);
99 /* There is some argument that we should dissble signals here. */
100 /* But that's expensive. And this way things should only appear */
101 /* inconsistent while we're in the handler. */
103 ((oh *)p) -> oh_string = string;
104 ((oh *)p) -> oh_int = integer;
105 ((oh *)p) -> oh_sz = sz;
106 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
107 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
108 result[ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
110 return((ptr_t)result);
113 /* Check the object with debugging info at ohdr */
114 /* return NIL if it's OK. Else return clobbered */
116 ptr_t GC_check_annotated_obj(ohdr)
119 register ptr_t body = (ptr_t)(ohdr + 1);
120 register word gc_sz = GC_size((ptr_t)ohdr);
121 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
122 return((ptr_t)(&(ohdr -> oh_sz)));
124 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
125 return((ptr_t)(&(ohdr -> oh_sf)));
127 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
128 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
130 if (((word *)body)[ROUNDED_UP_WORDS(ohdr -> oh_sz)]
131 != (END_FLAG ^ (word)body)) {
132 return((ptr_t)((word *)body + ROUNDED_UP_WORDS(ohdr -> oh_sz)));
140 register oh * ohdr = (oh *)GC_base(p);
142 GC_err_printf1("0x%lx (", ((unsigned long)ohdr + sizeof(oh)));
143 GC_err_puts(ohdr -> oh_string);
144 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
145 (unsigned long)(ohdr -> oh_sz));
146 PRINT_CALL_CHAIN(ohdr);
149 void GC_debug_print_heap_obj_proc(p)
152 if (GC_has_debug_info(p)) {
155 GC_default_print_heap_obj_proc(p);
159 void GC_print_smashed_obj(p, clobbered_addr)
160 ptr_t p, clobbered_addr;
162 register oh * ohdr = (oh *)GC_base(p);
164 GC_err_printf2("0x%lx in object at 0x%lx(", (unsigned long)clobbered_addr,
166 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
167 || ohdr -> oh_string == 0) {
168 GC_err_printf1("<smashed>, appr. sz = %ld)\n",
169 (GC_size((ptr_t)ohdr) - DEBUG_BYTES));
171 if (ohdr -> oh_string[0] == '\0') {
172 GC_err_puts("EMPTY(smashed?)");
174 GC_err_puts(ohdr -> oh_string);
176 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
177 (unsigned long)(ohdr -> oh_sz));
178 PRINT_CALL_CHAIN(ohdr);
182 void GC_check_heap_proc();
184 void GC_start_debugging()
186 GC_check_heap = GC_check_heap_proc;
187 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
188 GC_debugging_started = TRUE;
189 GC_register_displacement((word)sizeof(oh));
192 # if defined(__STDC__) || defined(__cplusplus)
193 void GC_debug_register_displacement(GC_word offset)
195 void GC_debug_register_displacement(offset)
199 GC_register_displacement(offset);
200 GC_register_displacement((word)sizeof(oh) + offset);
203 # ifdef GC_ADD_CALLER
204 # define EXTRA_ARGS word ra, char * s, int i
207 # define EXTRA_ARGS char * s, int i
212 GC_PTR GC_debug_malloc(size_t lb, EXTRA_ARGS)
214 GC_PTR GC_debug_malloc(lb, s, i)
218 # ifdef GC_ADD_CALLER
219 --> GC_ADD_CALLER not implemented for K&R C
223 GC_PTR result = GC_malloc(lb + DEBUG_BYTES);
226 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
229 GC_err_printf1(":%ld)\n", (unsigned long)i);
232 if (!GC_debugging_started) {
233 GC_start_debugging();
235 ADD_CALL_CHAIN(result, ra);
236 return (GC_store_debug_info(result, (word)lb, s, (word)i));
239 #ifdef STUBBORN_ALLOC
241 GC_PTR GC_debug_malloc_stubborn(size_t lb, EXTRA_ARGS)
243 GC_PTR GC_debug_malloc_stubborn(lb, s, i)
249 GC_PTR result = GC_malloc_stubborn(lb + DEBUG_BYTES);
252 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
255 GC_err_printf1(":%ld)\n", (unsigned long)i);
258 if (!GC_debugging_started) {
259 GC_start_debugging();
261 ADD_CALL_CHAIN(result, ra);
262 return (GC_store_debug_info(result, (word)lb, s, (word)i));
265 void GC_debug_change_stubborn(p)
268 register GC_PTR q = GC_base(p);
272 GC_err_printf1("Bad argument: 0x%lx to GC_debug_change_stubborn\n",
274 ABORT("GC_debug_change_stubborn: bad arg");
277 if (hhdr -> hb_obj_kind != STUBBORN) {
278 GC_err_printf1("GC_debug_change_stubborn arg not stubborn: 0x%lx\n",
280 ABORT("GC_debug_change_stubborn: arg not stubborn");
282 GC_change_stubborn(q);
285 void GC_debug_end_stubborn_change(p)
288 register GC_PTR q = GC_base(p);
292 GC_err_printf1("Bad argument: 0x%lx to GC_debug_end_stubborn_change\n",
294 ABORT("GC_debug_end_stubborn_change: bad arg");
297 if (hhdr -> hb_obj_kind != STUBBORN) {
298 GC_err_printf1("debug_end_stubborn_change arg not stubborn: 0x%lx\n",
300 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
302 GC_end_stubborn_change(q);
305 #endif /* STUBBORN_ALLOC */
308 GC_PTR GC_debug_malloc_atomic(size_t lb, EXTRA_ARGS)
310 GC_PTR GC_debug_malloc_atomic(lb, s, i)
316 GC_PTR result = GC_malloc_atomic(lb + DEBUG_BYTES);
319 GC_err_printf1("GC_debug_malloc_atomic(%ld) returning NIL (",
322 GC_err_printf1(":%ld)\n", (unsigned long)i);
325 if (!GC_debugging_started) {
326 GC_start_debugging();
328 ADD_CALL_CHAIN(result, ra);
329 return (GC_store_debug_info(result, (word)lb, s, (word)i));
333 GC_PTR GC_debug_malloc_uncollectable(size_t lb, EXTRA_ARGS)
335 GC_PTR GC_debug_malloc_uncollectable(lb, s, i)
341 GC_PTR result = GC_malloc_uncollectable(lb + DEBUG_BYTES);
344 GC_err_printf1("GC_debug_malloc_uncollectable(%ld) returning NIL (",
347 GC_err_printf1(":%ld)\n", (unsigned long)i);
350 if (!GC_debugging_started) {
351 GC_start_debugging();
353 ADD_CALL_CHAIN(result, ra);
354 return (GC_store_debug_info(result, (word)lb, s, (word)i));
357 #ifdef ATOMIC_UNCOLLECTABLE
359 GC_PTR GC_debug_malloc_atomic_uncollectable(size_t lb, EXTRA_ARGS)
361 GC_PTR GC_debug_malloc_atomic_uncollectable(lb, s, i)
367 GC_PTR result = GC_malloc_atomic_uncollectable(lb + DEBUG_BYTES);
371 "GC_debug_malloc_atomic_uncollectable(%ld) returning NIL (",
374 GC_err_printf1(":%ld)\n", (unsigned long)i);
377 if (!GC_debugging_started) {
378 GC_start_debugging();
380 ADD_CALL_CHAIN(result, ra);
381 return (GC_store_debug_info(result, (word)lb, s, (word)i));
383 #endif /* ATOMIC_UNCOLLECTABLE */
386 void GC_debug_free(GC_PTR p)
388 void GC_debug_free(p)
392 register GC_PTR base = GC_base(p);
393 register ptr_t clobbered;
396 GC_err_printf1("Attempt to free invalid pointer %lx\n",
398 if (p != 0) ABORT("free(invalid pointer)");
400 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
402 "GC_debug_free called on pointer %lx wo debugging info\n",
405 clobbered = GC_check_annotated_obj((oh *)base);
406 if (clobbered != 0) {
407 if (((oh *)base) -> oh_sz == GC_size(base)) {
409 "GC_debug_free: found previously deallocated (?) object at ");
411 GC_err_printf0("GC_debug_free: found smashed location at ");
413 GC_print_smashed_obj(p, clobbered);
415 /* Invalidate size */
416 ((oh *)base) -> oh_sz = GC_size(base);
422 register hdr * hhdr = HDR(p);
423 GC_bool uncollectable = FALSE;
425 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
426 uncollectable = TRUE;
428 # ifdef ATOMIC_UNCOLLECTABLE
429 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
430 uncollectable = TRUE;
433 if (uncollectable) GC_free(base);
439 GC_PTR GC_debug_realloc(GC_PTR p, size_t lb, EXTRA_ARGS)
441 GC_PTR GC_debug_realloc(p, lb, s, i)
448 register GC_PTR base = GC_base(p);
449 register ptr_t clobbered;
450 register GC_PTR result;
451 register size_t copy_sz = lb;
452 register size_t old_sz;
455 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
458 "Attempt to reallocate invalid pointer %lx\n", (unsigned long)p);
459 ABORT("realloc(invalid pointer)");
461 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
463 "GC_debug_realloc called on pointer %lx wo debugging info\n",
465 return(GC_realloc(p, lb));
468 switch (hhdr -> hb_obj_kind) {
469 # ifdef STUBBORN_ALLOC
471 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
475 result = GC_debug_malloc(lb, OPT_RA s, i);
478 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
481 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
483 # ifdef ATOMIC_UNCOLLECTABLE
485 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
489 GC_err_printf0("GC_debug_realloc: encountered bad kind\n");
492 clobbered = GC_check_annotated_obj((oh *)base);
493 if (clobbered != 0) {
494 GC_err_printf0("GC_debug_realloc: found smashed location at ");
495 GC_print_smashed_obj(p, clobbered);
497 old_sz = ((oh *)base) -> oh_sz;
498 if (old_sz < copy_sz) copy_sz = old_sz;
499 if (result == 0) return(0);
500 BCOPY(p, result, copy_sz);
505 /* Check all marked objects in the given block for validity */
507 void GC_check_heap_block(hbp, dummy)
508 register struct hblk *hbp; /* ptr to current heap block */
511 register struct hblkhdr * hhdr = HDR(hbp);
512 register word sz = hhdr -> hb_sz;
513 register int word_no;
514 register word *p, *plim;
516 p = (word *)(hbp->hb_body);
521 plim = (word *)((((word)hbp) + HBLKSIZE) - WORDS_TO_BYTES(sz));
523 /* go through all words in block */
525 if( mark_bit_from_hdr(hhdr, word_no)
526 && GC_has_debug_info((ptr_t)p)) {
527 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
529 if (clobbered != 0) {
531 "GC_check_heap_block: found smashed location at ");
532 GC_print_smashed_obj((ptr_t)p, clobbered);
541 /* This assumes that all accessible objects are marked, and that */
542 /* I hold the allocation lock. Normally called by collector. */
543 void GC_check_heap_proc()
545 # ifndef SMALL_CONFIG
546 if (sizeof(oh) & (2 * sizeof(word) - 1) != 0) {
547 ABORT("Alignment problem: object header has inappropriate size\n");
550 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
554 GC_finalization_proc cl_fn;
559 void * GC_make_closure(GC_finalization_proc fn, void * data)
561 GC_PTR GC_make_closure(fn, data)
562 GC_finalization_proc fn;
566 struct closure * result =
567 (struct closure *) GC_malloc(sizeof (struct closure));
569 result -> cl_fn = fn;
570 result -> cl_data = data;
571 return((GC_PTR)result);
575 void GC_debug_invoke_finalizer(void * obj, void * data)
577 void GC_debug_invoke_finalizer(obj, data)
582 register struct closure * cl = (struct closure *) data;
584 (*(cl -> cl_fn))((GC_PTR)((char *)obj + sizeof(oh)), cl -> cl_data);
589 void GC_debug_register_finalizer(GC_PTR obj, GC_finalization_proc fn,
590 GC_PTR cd, GC_finalization_proc *ofn,
593 void GC_debug_register_finalizer(obj, fn, cd, ofn, ocd)
595 GC_finalization_proc fn;
597 GC_finalization_proc *ofn;
601 ptr_t base = GC_base(obj);
602 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
604 "GC_register_finalizer called with non-base-pointer 0x%lx\n",
607 GC_register_finalizer(base, GC_debug_invoke_finalizer,
608 GC_make_closure(fn,cd), ofn, ocd);
612 void GC_debug_register_finalizer_no_order
613 (GC_PTR obj, GC_finalization_proc fn,
614 GC_PTR cd, GC_finalization_proc *ofn,
617 void GC_debug_register_finalizer_no_order
618 (obj, fn, cd, ofn, ocd)
620 GC_finalization_proc fn;
622 GC_finalization_proc *ofn;
626 ptr_t base = GC_base(obj);
627 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
629 "GC_register_finalizer_no_order called with non-base-pointer 0x%lx\n",
632 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
633 GC_make_closure(fn,cd), ofn, ocd);
637 void GC_debug_register_finalizer_ignore_self
638 (GC_PTR obj, GC_finalization_proc fn,
639 GC_PTR cd, GC_finalization_proc *ofn,
642 void GC_debug_register_finalizer_ignore_self
643 (obj, fn, cd, ofn, ocd)
645 GC_finalization_proc fn;
647 GC_finalization_proc *ofn;
651 ptr_t base = GC_base(obj);
652 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
654 "GC_register_finalizer_ignore_self called with non-base-pointer 0x%lx\n",
657 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
658 GC_make_closure(fn,cd), ofn, ocd);