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 /* Do we want to and know how to save the call stack at the time of */
19 /* an allocation? How much space do we want to use in each object? */
21 # define START_FLAG ((word)0xfedcedcb)
22 # define END_FLAG ((word)0xbcdecdef)
23 /* Stored both one past the end of user object, and one before */
24 /* the end of the object as seen by the allocator. */
29 char * oh_string; /* object descriptor string */
30 word oh_int; /* object descriptor integers */
32 struct callinfo oh_ci[NFRAMES];
34 word oh_sz; /* Original malloc arg. */
35 word oh_sf; /* start flag */
37 /* The size of the above structure is assumed not to dealign things, */
38 /* and to be a multiple of the word length. */
40 #define DEBUG_BYTES (sizeof (oh) + sizeof (word))
41 #undef ROUNDED_UP_WORDS
42 #define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + WORDS_TO_BYTES(1) - 1)
45 #ifdef SAVE_CALL_CHAIN
46 # define ADD_CALL_CHAIN(base, ra) GC_save_callers(((oh *)(base)) -> oh_ci)
47 # define PRINT_CALL_CHAIN(base) GC_print_callers(((oh *)(base)) -> oh_ci)
50 # define ADD_CALL_CHAIN(base, ra) ((oh *)(base)) -> oh_ci[0].ci_pc = (ra)
51 # define PRINT_CALL_CHAIN(base) GC_print_callers(((oh *)(base)) -> oh_ci)
53 # define ADD_CALL_CHAIN(base, ra)
54 # define PRINT_CALL_CHAIN(base)
58 /* Check whether object with base pointer p has debugging info */
59 /* p is assumed to point to a legitimate object in our part */
61 GC_bool GC_has_debug_info(p)
64 register oh * ohdr = (oh *)p;
65 register ptr_t body = (ptr_t)(ohdr + 1);
66 register word sz = GC_size((ptr_t) ohdr);
68 if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
69 || sz < sizeof (oh)) {
72 if (ohdr -> oh_sz == sz) {
73 /* Object may have had debug info, but has been deallocated */
76 if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
77 if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
83 /* Return start of object that might have debugging info. */
84 ptr_t GC_debug_object_start(p)
87 register word * result = (word *)((oh *)p + 1);
88 if (! GC_has_debug_info(p))
90 return((ptr_t)result);
93 /* Store debugging info into p. Return displaced pointer. */
94 /* Assumes we don't hold allocation lock. */
95 ptr_t GC_store_debug_info(p, sz, string, integer)
96 register ptr_t p; /* base pointer */
101 register word * result = (word *)((oh *)p + 1);
104 /* There is some argument that we should dissble signals here. */
105 /* But that's expensive. And this way things should only appear */
106 /* inconsistent while we're in the handler. */
108 ((oh *)p) -> oh_string = string;
109 ((oh *)p) -> oh_int = integer;
110 ((oh *)p) -> oh_sz = sz;
111 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
112 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
113 result[ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
115 return((ptr_t)result);
118 /* Check the object with debugging info at p */
119 /* return NIL if it's OK. Else return clobbered */
121 ptr_t GC_check_annotated_obj(ohdr)
124 register ptr_t body = (ptr_t)(ohdr + 1);
125 register word gc_sz = GC_size((ptr_t)ohdr);
126 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
127 return((ptr_t)(&(ohdr -> oh_sz)));
129 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
130 return((ptr_t)(&(ohdr -> oh_sf)));
132 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
133 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
135 if (((word *)body)[ROUNDED_UP_WORDS(ohdr -> oh_sz)]
136 != (END_FLAG ^ (word)body)) {
137 return((ptr_t)((word *)body + ROUNDED_UP_WORDS(ohdr -> oh_sz)));
145 register oh * ohdr = (oh *)GC_base(p);
147 GC_err_printf1("0x%lx (", (unsigned long)ohdr + sizeof(oh));
148 GC_err_puts(ohdr -> oh_string);
149 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
150 (unsigned long)(ohdr -> oh_sz));
151 PRINT_CALL_CHAIN(ohdr);
154 void GC_debug_print_heap_obj_proc(p)
157 if (GC_has_debug_info(p)) {
160 GC_default_print_heap_obj_proc(p);
164 void GC_print_smashed_obj(p, clobbered_addr)
165 ptr_t p, clobbered_addr;
167 register oh * ohdr = (oh *)GC_base(p);
169 GC_err_printf2("0x%lx in object at 0x%lx(", (unsigned long)clobbered_addr,
171 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
172 || ohdr -> oh_string == 0) {
173 GC_err_printf1("<smashed>, appr. sz = %ld)\n",
174 GC_size((ptr_t)ohdr) - DEBUG_BYTES);
176 if (ohdr -> oh_string[0] == '\0') {
177 GC_err_puts("EMPTY(smashed?)");
179 GC_err_puts(ohdr -> oh_string);
181 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
182 (unsigned long)(ohdr -> oh_sz));
183 PRINT_CALL_CHAIN(ohdr);
187 void GC_check_heap_proc();
189 void GC_start_debugging()
191 GC_check_heap = GC_check_heap_proc;
192 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
193 GC_debugging_started = TRUE;
194 GC_register_displacement((word)sizeof(oh));
197 # if defined(__STDC__) || defined(__cplusplus)
198 void GC_debug_register_displacement(GC_word offset)
200 void GC_debug_register_displacement(offset)
204 GC_register_displacement(offset);
205 GC_register_displacement((word)sizeof(oh) + offset);
208 # ifdef GC_ADD_CALLER
209 # define EXTRA_ARGS word ra, char * s, int i
212 # define EXTRA_ARGS char * s, int i
217 GC_PTR GC_debug_malloc(size_t lb, EXTRA_ARGS)
219 GC_PTR GC_debug_malloc(lb, s, i)
223 # ifdef GC_ADD_CALLER
224 --> GC_ADD_CALLER not implemented for K&R C
228 GC_PTR result = GC_malloc(lb + DEBUG_BYTES);
231 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
234 GC_err_printf1(":%ld)\n", (unsigned long)i);
237 if (!GC_debugging_started) {
238 GC_start_debugging();
240 ADD_CALL_CHAIN(result, ra);
241 return (GC_store_debug_info(result, (word)lb, s, (word)i));
245 GC_PTR GC_debug_generic_malloc(size_t lb, int k, EXTRA_ARGS)
247 GC_PTR GC_debug_malloc(lb, k, s, i)
252 # ifdef GC_ADD_CALLER
253 --> GC_ADD_CALLER not implemented for K&R C
257 GC_PTR result = GC_generic_malloc(lb + DEBUG_BYTES, k);
260 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
263 GC_err_printf1(":%ld)\n", (unsigned long)i);
266 if (!GC_debugging_started) {
267 GC_start_debugging();
269 ADD_CALL_CHAIN(result, ra);
270 return (GC_store_debug_info(result, (word)lb, s, (word)i));
273 #ifdef STUBBORN_ALLOC
275 GC_PTR GC_debug_malloc_stubborn(size_t lb, EXTRA_ARGS)
277 GC_PTR GC_debug_malloc_stubborn(lb, s, i)
283 GC_PTR result = GC_malloc_stubborn(lb + DEBUG_BYTES);
286 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
289 GC_err_printf1(":%ld)\n", (unsigned long)i);
292 if (!GC_debugging_started) {
293 GC_start_debugging();
295 ADD_CALL_CHAIN(result, ra);
296 return (GC_store_debug_info(result, (word)lb, s, (word)i));
299 void GC_debug_change_stubborn(p)
302 register GC_PTR q = GC_base(p);
306 GC_err_printf1("Bad argument: 0x%lx to GC_debug_change_stubborn\n",
308 ABORT("GC_debug_change_stubborn: bad arg");
311 if (hhdr -> hb_obj_kind != STUBBORN) {
312 GC_err_printf1("GC_debug_change_stubborn arg not stubborn: 0x%lx\n",
314 ABORT("GC_debug_change_stubborn: arg not stubborn");
316 GC_change_stubborn(q);
319 void GC_debug_end_stubborn_change(p)
322 register GC_PTR q = GC_base(p);
326 GC_err_printf1("Bad argument: 0x%lx to GC_debug_end_stubborn_change\n",
328 ABORT("GC_debug_end_stubborn_change: bad arg");
331 if (hhdr -> hb_obj_kind != STUBBORN) {
332 GC_err_printf1("debug_end_stubborn_change arg not stubborn: 0x%lx\n",
334 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
336 GC_end_stubborn_change(q);
339 #endif /* STUBBORN_ALLOC */
342 GC_PTR GC_debug_malloc_atomic(size_t lb, EXTRA_ARGS)
344 GC_PTR GC_debug_malloc_atomic(lb, s, i)
350 GC_PTR result = GC_malloc_atomic(lb + DEBUG_BYTES);
353 GC_err_printf1("GC_debug_malloc_atomic(%ld) returning NIL (",
356 GC_err_printf1(":%ld)\n", (unsigned long)i);
359 if (!GC_debugging_started) {
360 GC_start_debugging();
362 ADD_CALL_CHAIN(result, ra);
363 return (GC_store_debug_info(result, (word)lb, s, (word)i));
367 GC_PTR GC_debug_malloc_uncollectable(size_t lb, EXTRA_ARGS)
369 GC_PTR GC_debug_malloc_uncollectable(lb, s, i)
375 GC_PTR result = GC_malloc_uncollectable(lb + DEBUG_BYTES);
378 GC_err_printf1("GC_debug_malloc_uncollectable(%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 #ifdef ATOMIC_UNCOLLECTABLE
393 GC_PTR GC_debug_malloc_atomic_uncollectable(size_t lb, EXTRA_ARGS)
395 GC_PTR GC_debug_malloc_atomic_uncollectable(lb, s, i)
401 GC_PTR result = GC_malloc_atomic_uncollectable(lb + DEBUG_BYTES);
405 "GC_debug_malloc_atomic_uncollectable(%ld) returning NIL (",
408 GC_err_printf1(":%ld)\n", (unsigned long)i);
411 if (!GC_debugging_started) {
412 GC_start_debugging();
414 ADD_CALL_CHAIN(result, ra);
415 return (GC_store_debug_info(result, (word)lb, s, (word)i));
417 #endif /* ATOMIC_UNCOLLECTABLE */
420 void GC_debug_free(GC_PTR p)
422 void GC_debug_free(p)
426 register GC_PTR base = GC_base(p);
427 register ptr_t clobbered;
430 GC_err_printf1("Attempt to free invalid pointer %lx\n",
432 if (p != 0) ABORT("free(invalid pointer)");
434 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
436 "GC_debug_free called on pointer %lx wo debugging info\n",
439 clobbered = GC_check_annotated_obj((oh *)base);
440 if (clobbered != 0) {
441 if (((oh *)base) -> oh_sz == GC_size(base)) {
443 "GC_debug_free: found previously deallocated (?) object at ");
445 GC_err_printf0("GC_debug_free: found smashed object at ");
447 GC_print_smashed_obj(p, clobbered);
449 /* Invalidate size */
450 ((oh *)base) -> oh_sz = GC_size(base);
456 register hdr * hhdr = HDR(p);
457 GC_bool uncollectable = FALSE;
459 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
460 uncollectable = TRUE;
462 # ifdef ATOMIC_UNCOLLECTABLE
463 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
464 uncollectable = TRUE;
467 if (uncollectable) GC_free(base);
473 GC_PTR GC_debug_realloc(GC_PTR p, size_t lb, EXTRA_ARGS)
475 GC_PTR GC_debug_realloc(p, lb, s, i)
482 register GC_PTR base = GC_base(p);
483 register ptr_t clobbered;
484 register GC_PTR result;
485 register size_t copy_sz = lb;
486 register size_t old_sz;
489 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
492 "Attempt to reallocate invalid pointer %lx\n", (unsigned long)p);
493 ABORT("realloc(invalid pointer)");
495 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
497 "GC_debug_realloc called on pointer %lx wo debugging info\n",
499 return(GC_realloc(p, lb));
502 switch (hhdr -> hb_obj_kind) {
503 # ifdef STUBBORN_ALLOC
505 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
509 result = GC_debug_malloc(lb, OPT_RA s, i);
512 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
515 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
517 # ifdef ATOMIC_UNCOLLECTABLE
519 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
523 GC_err_printf0("GC_debug_realloc: encountered bad kind\n");
526 clobbered = GC_check_annotated_obj((oh *)base);
527 if (clobbered != 0) {
528 GC_err_printf0("GC_debug_realloc: found smashed object at ");
529 GC_print_smashed_obj(p, clobbered);
531 old_sz = ((oh *)base) -> oh_sz;
532 if (old_sz < copy_sz) copy_sz = old_sz;
533 if (result == 0) return(0);
534 BCOPY(p, result, copy_sz);
539 /* Check all marked objects in the given block for validity */
541 void GC_check_heap_block(hbp, dummy)
542 register struct hblk *hbp; /* ptr to current heap block */
545 register struct hblkhdr * hhdr = HDR(hbp);
546 register word sz = hhdr -> hb_sz;
547 register int word_no;
548 register word *p, *plim;
550 p = (word *)(hbp->hb_body);
555 plim = (word *)((((word)hbp) + HBLKSIZE) - WORDS_TO_BYTES(sz));
557 /* go through all words in block */
559 if( mark_bit_from_hdr(hhdr, word_no)
560 && GC_has_debug_info((ptr_t)p)) {
561 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
563 if (clobbered != 0) {
565 "GC_check_heap_block: found smashed object at ");
566 GC_print_smashed_obj((ptr_t)p, clobbered);
575 /* This assumes that all accessible objects are marked, and that */
576 /* I hold the allocation lock. Normally called by collector. */
577 void GC_check_heap_proc()
579 # ifndef SMALL_CONFIG
580 if (sizeof(oh) & (2 * sizeof(word) - 1) != 0) {
581 ABORT("Alignment problem: object header has inappropriate size\n");
584 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
588 GC_finalization_proc cl_fn;
593 void * GC_make_closure(GC_finalization_proc fn, void * data)
595 GC_PTR GC_make_closure(fn, data)
596 GC_finalization_proc fn;
600 struct closure * result =
601 (struct closure *) GC_malloc(sizeof (struct closure));
603 result -> cl_fn = fn;
604 result -> cl_data = data;
605 return((GC_PTR)result);
609 void GC_debug_invoke_finalizer(void * obj, void * data)
611 void GC_debug_invoke_finalizer(obj, data)
616 register struct closure * cl = (struct closure *) data;
618 (*(cl -> cl_fn))((GC_PTR)((char *)obj + sizeof(oh)), cl -> cl_data);
623 void GC_debug_register_finalizer(GC_PTR obj, GC_finalization_proc fn,
624 GC_PTR cd, GC_finalization_proc *ofn,
627 void GC_debug_register_finalizer(obj, fn, cd, ofn, ocd)
629 GC_finalization_proc fn;
631 GC_finalization_proc *ofn;
635 ptr_t base = GC_base(obj);
636 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
638 "GC_register_finalizer called with non-base-pointer 0x%lx\n",
641 GC_register_finalizer(base, GC_debug_invoke_finalizer,
642 GC_make_closure(fn,cd), ofn, ocd);
646 void GC_debug_register_finalizer_ignore_self
647 (GC_PTR obj, GC_finalization_proc fn,
648 GC_PTR cd, GC_finalization_proc *ofn,
651 void GC_debug_register_finalizer_ignore_self
652 (obj, fn, cd, ofn, ocd)
654 GC_finalization_proc fn;
656 GC_finalization_proc *ofn;
660 ptr_t base = GC_base(obj);
661 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
663 "GC_register_finalizer_ignore_self called with non-base-pointer 0x%lx\n",
666 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
667 GC_make_closure(fn,cd), ofn, ocd);
671 void GC_debug_register_finalizer_no_order
672 (GC_PTR obj, GC_finalization_proc fn,
673 GC_PTR cd, GC_finalization_proc *ofn,
676 void GC_debug_register_finalizer_no_order
677 (obj, fn, cd, ofn, ocd)
679 GC_finalization_proc fn;
681 GC_finalization_proc *ofn;
685 ptr_t base = GC_base(obj);
686 if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
688 "GC_register_finalizer_no_order called with non-base-pointer 0x%lx\n",
691 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
692 GC_make_closure(fn,cd), ofn, ocd);