Revert accidental commit of unrelated/unfinished changes.
[official-gcc.git] / boehm-gc / dbg_mlc.c
blobaacbb7a1b632e091c6a7cd97f89b587aa2bd83a6
1 /*
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-2004 Hewlett-Packard Development Company, L.P.
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.
17 #include "private/dbg_mlc.h"
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 #ifndef SHORT_DBG_HDRS
26 /* Check whether object with base pointer p has debugging info */
27 /* p is assumed to point to a legitimate object in our part */
28 /* of the heap. */
29 /* This excludes the check as to whether the back pointer is */
30 /* odd, which is added by the GC_HAS_DEBUG_INFO macro. */
31 /* Note that if DBG_HDRS_ALL is set, uncollectable objects */
32 /* on free lists may not have debug information set. Thus it's */
33 /* not always safe to return TRUE, even if the client does */
34 /* its part. */
35 GC_bool GC_has_other_debug_info(p)
36 ptr_t p;
38 register oh * ohdr = (oh *)p;
39 register ptr_t body = (ptr_t)(ohdr + 1);
40 register word sz = GC_size((ptr_t) ohdr);
42 if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
43 || sz < DEBUG_BYTES + EXTRA_BYTES) {
44 return(FALSE);
46 if (ohdr -> oh_sz == sz) {
47 /* Object may have had debug info, but has been deallocated */
48 return(FALSE);
50 if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
51 if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
52 return(TRUE);
54 return(FALSE);
56 #endif
58 #ifdef KEEP_BACK_PTRS
60 # include <stdlib.h>
62 # if defined(LINUX) || defined(SUNOS4) || defined(SUNOS5) \
63 || defined(HPUX) || defined(IRIX5) || defined(OSF1)
64 # define RANDOM() random()
65 # else
66 # define RANDOM() (long)rand()
67 # endif
69 /* Store back pointer to source in dest, if that appears to be possible. */
70 /* This is not completely safe, since we may mistakenly conclude that */
71 /* dest has a debugging wrapper. But the error probability is very */
72 /* small, and this shouldn't be used in production code. */
73 /* We assume that dest is the real base pointer. Source will usually */
74 /* be a pointer to the interior of an object. */
75 void GC_store_back_pointer(ptr_t source, ptr_t dest)
77 if (GC_HAS_DEBUG_INFO(dest)) {
78 ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
82 void GC_marked_for_finalization(ptr_t dest) {
83 GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
86 /* Store information about the object referencing dest in *base_p */
87 /* and *offset_p. */
88 /* source is root ==> *base_p = address, *offset_p = 0 */
89 /* source is heap object ==> *base_p != 0, *offset_p = offset */
90 /* Returns 1 on success, 0 if source couldn't be determined. */
91 /* Dest can be any address within a heap object. */
92 GC_ref_kind GC_get_back_ptr_info(void *dest, void **base_p, size_t *offset_p)
94 oh * hdr = (oh *)GC_base(dest);
95 ptr_t bp;
96 ptr_t bp_base;
97 if (!GC_HAS_DEBUG_INFO((ptr_t) hdr)) return GC_NO_SPACE;
98 bp = REVEAL_POINTER(hdr -> oh_back_ptr);
99 if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
100 if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
101 if (NOT_MARKED == bp) return GC_UNREFERENCED;
102 # if ALIGNMENT == 1
103 /* Heuristically try to fix off by 1 errors we introduced by */
104 /* insisting on even addresses. */
106 ptr_t alternate_ptr = bp + 1;
107 ptr_t target = *(ptr_t *)bp;
108 ptr_t alternate_target = *(ptr_t *)alternate_ptr;
110 if (alternate_target >= GC_least_plausible_heap_addr
111 && alternate_target <= GC_greatest_plausible_heap_addr
112 && (target < GC_least_plausible_heap_addr
113 || target > GC_greatest_plausible_heap_addr)) {
114 bp = alternate_ptr;
117 # endif
118 bp_base = GC_base(bp);
119 if (0 == bp_base) {
120 *base_p = bp;
121 *offset_p = 0;
122 return GC_REFD_FROM_ROOT;
123 } else {
124 if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
125 *base_p = bp_base;
126 *offset_p = bp - bp_base;
127 return GC_REFD_FROM_HEAP;
131 /* Generate a random heap address. */
132 /* The resulting address is in the heap, but */
133 /* not necessarily inside a valid object. */
134 void *GC_generate_random_heap_address(void)
136 int i;
137 long heap_offset = RANDOM();
138 if (GC_heapsize > RAND_MAX) {
139 heap_offset *= RAND_MAX;
140 heap_offset += RANDOM();
142 heap_offset %= GC_heapsize;
143 /* This doesn't yield a uniform distribution, especially if */
144 /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
145 /* it's not too bad. */
146 for (i = 0; i < GC_n_heap_sects; ++ i) {
147 int size = GC_heap_sects[i].hs_bytes;
148 if (heap_offset < size) {
149 return GC_heap_sects[i].hs_start + heap_offset;
150 } else {
151 heap_offset -= size;
154 ABORT("GC_generate_random_heap_address: size inconsistency");
155 /*NOTREACHED*/
156 return 0;
159 /* Generate a random address inside a valid marked heap object. */
160 void *GC_generate_random_valid_address(void)
162 ptr_t result;
163 ptr_t base;
164 for (;;) {
165 result = GC_generate_random_heap_address();
166 base = GC_base(result);
167 if (0 == base) continue;
168 if (!GC_is_marked(base)) continue;
169 return result;
173 /* Print back trace for p */
174 void GC_print_backtrace(void *p)
176 void *current = p;
177 int i;
178 GC_ref_kind source;
179 size_t offset;
180 void *base;
182 GC_print_heap_obj(GC_base(current));
183 GC_err_printf0("\n");
184 for (i = 0; ; ++i) {
185 source = GC_get_back_ptr_info(current, &base, &offset);
186 if (GC_UNREFERENCED == source) {
187 GC_err_printf0("Reference could not be found\n");
188 goto out;
190 if (GC_NO_SPACE == source) {
191 GC_err_printf0("No debug info in object: Can't find reference\n");
192 goto out;
194 GC_err_printf1("Reachable via %d levels of pointers from ",
195 (unsigned long)i);
196 switch(source) {
197 case GC_REFD_FROM_ROOT:
198 GC_err_printf1("root at 0x%lx\n\n", (unsigned long)base);
199 goto out;
200 case GC_REFD_FROM_REG:
201 GC_err_printf0("root in register\n\n");
202 goto out;
203 case GC_FINALIZER_REFD:
204 GC_err_printf0("list of finalizable objects\n\n");
205 goto out;
206 case GC_REFD_FROM_HEAP:
207 GC_err_printf1("offset %ld in object:\n", (unsigned long)offset);
208 /* Take GC_base(base) to get real base, i.e. header. */
209 GC_print_heap_obj(GC_base(base));
210 GC_err_printf0("\n");
211 break;
213 current = base;
215 out:;
218 /* Force a garbage collection and generate a backtrace from a */
219 /* random heap address. */
220 void GC_generate_random_backtrace_no_gc(void)
222 void * current;
223 current = GC_generate_random_valid_address();
224 GC_printf1("\n****Chose address 0x%lx in object\n", (unsigned long)current);
225 GC_print_backtrace(current);
228 void GC_generate_random_backtrace(void)
230 GC_gcollect();
231 GC_generate_random_backtrace_no_gc();
234 #endif /* KEEP_BACK_PTRS */
236 # define CROSSES_HBLK(p, sz) \
237 (((word)(p + sizeof(oh) + sz - 1) ^ (word)p) >= HBLKSIZE)
238 /* Store debugging info into p. Return displaced pointer. */
239 /* Assumes we don't hold allocation lock. */
240 ptr_t GC_store_debug_info(p, sz, string, integer)
241 register ptr_t p; /* base pointer */
242 word sz; /* bytes */
243 GC_CONST char * string;
244 word integer;
246 register word * result = (word *)((oh *)p + 1);
247 DCL_LOCK_STATE;
249 /* There is some argument that we should dissble signals here. */
250 /* But that's expensive. And this way things should only appear */
251 /* inconsistent while we're in the handler. */
252 LOCK();
253 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
254 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
255 # ifdef KEEP_BACK_PTRS
256 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
257 # endif
258 # ifdef MAKE_BACK_GRAPH
259 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
260 # endif
261 ((oh *)p) -> oh_string = string;
262 ((oh *)p) -> oh_int = integer;
263 # ifndef SHORT_DBG_HDRS
264 ((oh *)p) -> oh_sz = sz;
265 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
266 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
267 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
268 # endif
269 UNLOCK();
270 return((ptr_t)result);
273 #ifdef DBG_HDRS_ALL
274 /* Store debugging info into p. Return displaced pointer. */
275 /* This version assumes we do hold the allocation lock. */
276 ptr_t GC_store_debug_info_inner(p, sz, string, integer)
277 register ptr_t p; /* base pointer */
278 word sz; /* bytes */
279 char * string;
280 word integer;
282 register word * result = (word *)((oh *)p + 1);
284 /* There is some argument that we should disable signals here. */
285 /* But that's expensive. And this way things should only appear */
286 /* inconsistent while we're in the handler. */
287 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
288 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
289 # ifdef KEEP_BACK_PTRS
290 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
291 # endif
292 # ifdef MAKE_BACK_GRAPH
293 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
294 # endif
295 ((oh *)p) -> oh_string = string;
296 ((oh *)p) -> oh_int = integer;
297 # ifndef SHORT_DBG_HDRS
298 ((oh *)p) -> oh_sz = sz;
299 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
300 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
301 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
302 # endif
303 return((ptr_t)result);
305 #endif
307 #ifndef SHORT_DBG_HDRS
308 /* Check the object with debugging info at ohdr */
309 /* return NIL if it's OK. Else return clobbered */
310 /* address. */
311 ptr_t GC_check_annotated_obj(ohdr)
312 register oh * ohdr;
314 register ptr_t body = (ptr_t)(ohdr + 1);
315 register word gc_sz = GC_size((ptr_t)ohdr);
316 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
317 return((ptr_t)(&(ohdr -> oh_sz)));
319 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
320 return((ptr_t)(&(ohdr -> oh_sf)));
322 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
323 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
325 if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
326 != (END_FLAG ^ (word)body)) {
327 return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)));
329 return(0);
331 #endif /* !SHORT_DBG_HDRS */
333 static GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
335 void GC_register_describe_type_fn(kind, fn)
336 int kind;
337 GC_describe_type_fn fn;
339 GC_describe_type_fns[kind] = fn;
342 /* Print a type description for the object whose client-visible address */
343 /* is p. */
344 void GC_print_type(p)
345 ptr_t p;
347 hdr * hhdr = GC_find_header(p);
348 char buffer[GC_TYPE_DESCR_LEN + 1];
349 int kind = hhdr -> hb_obj_kind;
351 if (0 != GC_describe_type_fns[kind] && GC_is_marked(GC_base(p))) {
352 /* This should preclude free list objects except with */
353 /* thread-local allocation. */
354 buffer[GC_TYPE_DESCR_LEN] = 0;
355 (GC_describe_type_fns[kind])(p, buffer);
356 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
357 GC_err_puts(buffer);
358 } else {
359 switch(kind) {
360 case PTRFREE:
361 GC_err_puts("PTRFREE");
362 break;
363 case NORMAL:
364 GC_err_puts("NORMAL");
365 break;
366 case UNCOLLECTABLE:
367 GC_err_puts("UNCOLLECTABLE");
368 break;
369 # ifdef ATOMIC_UNCOLLECTABLE
370 case AUNCOLLECTABLE:
371 GC_err_puts("ATOMIC UNCOLLECTABLE");
372 break;
373 # endif
374 case STUBBORN:
375 GC_err_puts("STUBBORN");
376 break;
377 default:
378 GC_err_printf2("kind %ld, descr 0x%lx", kind, hhdr -> hb_descr);
385 void GC_print_obj(p)
386 ptr_t p;
388 register oh * ohdr = (oh *)GC_base(p);
390 GC_ASSERT(!I_HOLD_LOCK());
391 GC_err_printf1("0x%lx (", ((unsigned long)ohdr + sizeof(oh)));
392 GC_err_puts(ohdr -> oh_string);
393 # ifdef SHORT_DBG_HDRS
394 GC_err_printf1(":%ld, ", (unsigned long)(ohdr -> oh_int));
395 # else
396 GC_err_printf2(":%ld, sz=%ld, ", (unsigned long)(ohdr -> oh_int),
397 (unsigned long)(ohdr -> oh_sz));
398 # endif
399 GC_print_type((ptr_t)(ohdr + 1));
400 GC_err_puts(")\n");
401 PRINT_CALL_CHAIN(ohdr);
404 # if defined(__STDC__) || defined(__cplusplus)
405 void GC_debug_print_heap_obj_proc(ptr_t p)
406 # else
407 void GC_debug_print_heap_obj_proc(p)
408 ptr_t p;
409 # endif
411 GC_ASSERT(!I_HOLD_LOCK());
412 if (GC_HAS_DEBUG_INFO(p)) {
413 GC_print_obj(p);
414 } else {
415 GC_default_print_heap_obj_proc(p);
419 #ifndef SHORT_DBG_HDRS
420 void GC_print_smashed_obj(p, clobbered_addr)
421 ptr_t p, clobbered_addr;
423 register oh * ohdr = (oh *)GC_base(p);
425 GC_ASSERT(!I_HOLD_LOCK());
426 GC_err_printf2("0x%lx in object at 0x%lx(", (unsigned long)clobbered_addr,
427 (unsigned long)p);
428 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
429 || ohdr -> oh_string == 0) {
430 GC_err_printf1("<smashed>, appr. sz = %ld)\n",
431 (GC_size((ptr_t)ohdr) - DEBUG_BYTES));
432 } else {
433 if (ohdr -> oh_string[0] == '\0') {
434 GC_err_puts("EMPTY(smashed?)");
435 } else {
436 GC_err_puts(ohdr -> oh_string);
438 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
439 (unsigned long)(ohdr -> oh_sz));
440 PRINT_CALL_CHAIN(ohdr);
443 #endif
445 void GC_check_heap_proc GC_PROTO((void));
447 void GC_print_all_smashed_proc GC_PROTO((void));
449 void GC_do_nothing() {}
451 void GC_start_debugging()
453 # ifndef SHORT_DBG_HDRS
454 GC_check_heap = GC_check_heap_proc;
455 GC_print_all_smashed = GC_print_all_smashed_proc;
456 # else
457 GC_check_heap = GC_do_nothing;
458 GC_print_all_smashed = GC_do_nothing;
459 # endif
460 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
461 GC_debugging_started = TRUE;
462 GC_register_displacement((word)sizeof(oh));
465 size_t GC_debug_header_size = sizeof(oh);
467 # if defined(__STDC__) || defined(__cplusplus)
468 void GC_debug_register_displacement(GC_word offset)
469 # else
470 void GC_debug_register_displacement(offset)
471 GC_word offset;
472 # endif
474 GC_register_displacement(offset);
475 GC_register_displacement((word)sizeof(oh) + offset);
478 # ifdef __STDC__
479 GC_PTR GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS)
480 # else
481 GC_PTR GC_debug_malloc(lb, s, i)
482 size_t lb;
483 char * s;
484 int i;
485 # ifdef GC_ADD_CALLER
486 --> GC_ADD_CALLER not implemented for K&R C
487 # endif
488 # endif
490 GC_PTR result = GC_malloc(lb + DEBUG_BYTES);
492 if (result == 0) {
493 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
494 (unsigned long) lb);
495 GC_err_puts(s);
496 GC_err_printf1(":%ld)\n", (unsigned long)i);
497 return(0);
499 if (!GC_debugging_started) {
500 GC_start_debugging();
502 ADD_CALL_CHAIN(result, ra);
503 return (GC_store_debug_info(result, (word)lb, s, (word)i));
506 # ifdef __STDC__
507 GC_PTR GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
508 # else
509 GC_PTR GC_debug_malloc_ignore_off_page(lb, s, i)
510 size_t lb;
511 char * s;
512 int i;
513 # ifdef GC_ADD_CALLER
514 --> GC_ADD_CALLER not implemented for K&R C
515 # endif
516 # endif
518 GC_PTR result = GC_malloc_ignore_off_page(lb + DEBUG_BYTES);
520 if (result == 0) {
521 GC_err_printf1("GC_debug_malloc_ignore_off_page(%ld) returning NIL (",
522 (unsigned long) lb);
523 GC_err_puts(s);
524 GC_err_printf1(":%ld)\n", (unsigned long)i);
525 return(0);
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));
534 # ifdef __STDC__
535 GC_PTR GC_debug_malloc_atomic_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
536 # else
537 GC_PTR GC_debug_malloc_atomic_ignore_off_page(lb, s, i)
538 size_t lb;
539 char * s;
540 int i;
541 # ifdef GC_ADD_CALLER
542 --> GC_ADD_CALLER not implemented for K&R C
543 # endif
544 # endif
546 GC_PTR result = GC_malloc_atomic_ignore_off_page(lb + DEBUG_BYTES);
548 if (result == 0) {
549 GC_err_printf1("GC_debug_malloc_atomic_ignore_off_page(%ld)"
550 " returning NIL (", (unsigned long) lb);
551 GC_err_puts(s);
552 GC_err_printf1(":%ld)\n", (unsigned long)i);
553 return(0);
555 if (!GC_debugging_started) {
556 GC_start_debugging();
558 ADD_CALL_CHAIN(result, ra);
559 return (GC_store_debug_info(result, (word)lb, s, (word)i));
562 # ifdef DBG_HDRS_ALL
564 * An allocation function for internal use.
565 * Normally internally allocated objects do not have debug information.
566 * But in this case, we need to make sure that all objects have debug
567 * headers.
568 * We assume debugging was started in collector initialization,
569 * and we already hold the GC lock.
571 GC_PTR GC_debug_generic_malloc_inner(size_t lb, int k)
573 GC_PTR result = GC_generic_malloc_inner(lb + DEBUG_BYTES, k);
575 if (result == 0) {
576 GC_err_printf1("GC internal allocation (%ld bytes) returning NIL\n",
577 (unsigned long) lb);
578 return(0);
580 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
581 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
584 GC_PTR GC_debug_generic_malloc_inner_ignore_off_page(size_t lb, int k)
586 GC_PTR result = GC_generic_malloc_inner_ignore_off_page(
587 lb + DEBUG_BYTES, k);
589 if (result == 0) {
590 GC_err_printf1("GC internal allocation (%ld bytes) returning NIL\n",
591 (unsigned long) lb);
592 return(0);
594 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
595 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
597 # endif
599 #ifdef STUBBORN_ALLOC
600 # ifdef __STDC__
601 GC_PTR GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
602 # else
603 GC_PTR GC_debug_malloc_stubborn(lb, s, i)
604 size_t lb;
605 char * s;
606 int i;
607 # endif
609 GC_PTR result = GC_malloc_stubborn(lb + DEBUG_BYTES);
611 if (result == 0) {
612 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
613 (unsigned long) lb);
614 GC_err_puts(s);
615 GC_err_printf1(":%ld)\n", (unsigned long)i);
616 return(0);
618 if (!GC_debugging_started) {
619 GC_start_debugging();
621 ADD_CALL_CHAIN(result, ra);
622 return (GC_store_debug_info(result, (word)lb, s, (word)i));
625 void GC_debug_change_stubborn(p)
626 GC_PTR p;
628 register GC_PTR q = GC_base(p);
629 register hdr * hhdr;
631 if (q == 0) {
632 GC_err_printf1("Bad argument: 0x%lx to GC_debug_change_stubborn\n",
633 (unsigned long) p);
634 ABORT("GC_debug_change_stubborn: bad arg");
636 hhdr = HDR(q);
637 if (hhdr -> hb_obj_kind != STUBBORN) {
638 GC_err_printf1("GC_debug_change_stubborn arg not stubborn: 0x%lx\n",
639 (unsigned long) p);
640 ABORT("GC_debug_change_stubborn: arg not stubborn");
642 GC_change_stubborn(q);
645 void GC_debug_end_stubborn_change(p)
646 GC_PTR p;
648 register GC_PTR q = GC_base(p);
649 register hdr * hhdr;
651 if (q == 0) {
652 GC_err_printf1("Bad argument: 0x%lx to GC_debug_end_stubborn_change\n",
653 (unsigned long) p);
654 ABORT("GC_debug_end_stubborn_change: bad arg");
656 hhdr = HDR(q);
657 if (hhdr -> hb_obj_kind != STUBBORN) {
658 GC_err_printf1("debug_end_stubborn_change arg not stubborn: 0x%lx\n",
659 (unsigned long) p);
660 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
662 GC_end_stubborn_change(q);
665 #else /* !STUBBORN_ALLOC */
667 # ifdef __STDC__
668 GC_PTR GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
669 # else
670 GC_PTR GC_debug_malloc_stubborn(lb, s, i)
671 size_t lb;
672 char * s;
673 int i;
674 # endif
676 return GC_debug_malloc(lb, OPT_RA s, i);
679 void GC_debug_change_stubborn(p)
680 GC_PTR p;
684 void GC_debug_end_stubborn_change(p)
685 GC_PTR p;
689 #endif /* !STUBBORN_ALLOC */
691 # ifdef __STDC__
692 GC_PTR GC_debug_malloc_atomic(size_t lb, GC_EXTRA_PARAMS)
693 # else
694 GC_PTR GC_debug_malloc_atomic(lb, s, i)
695 size_t lb;
696 char * s;
697 int i;
698 # endif
700 GC_PTR result = GC_malloc_atomic(lb + DEBUG_BYTES);
702 if (result == 0) {
703 GC_err_printf1("GC_debug_malloc_atomic(%ld) returning NIL (",
704 (unsigned long) lb);
705 GC_err_puts(s);
706 GC_err_printf1(":%ld)\n", (unsigned long)i);
707 return(0);
709 if (!GC_debugging_started) {
710 GC_start_debugging();
712 ADD_CALL_CHAIN(result, ra);
713 return (GC_store_debug_info(result, (word)lb, s, (word)i));
716 # ifdef __STDC__
717 GC_PTR GC_debug_malloc_uncollectable(size_t lb, GC_EXTRA_PARAMS)
718 # else
719 GC_PTR GC_debug_malloc_uncollectable(lb, s, i)
720 size_t lb;
721 char * s;
722 int i;
723 # endif
725 GC_PTR result = GC_malloc_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
727 if (result == 0) {
728 GC_err_printf1("GC_debug_malloc_uncollectable(%ld) returning NIL (",
729 (unsigned long) lb);
730 GC_err_puts(s);
731 GC_err_printf1(":%ld)\n", (unsigned long)i);
732 return(0);
734 if (!GC_debugging_started) {
735 GC_start_debugging();
737 ADD_CALL_CHAIN(result, ra);
738 return (GC_store_debug_info(result, (word)lb, s, (word)i));
741 #ifdef ATOMIC_UNCOLLECTABLE
742 # ifdef __STDC__
743 GC_PTR GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
744 # else
745 GC_PTR GC_debug_malloc_atomic_uncollectable(lb, s, i)
746 size_t lb;
747 char * s;
748 int i;
749 # endif
751 GC_PTR result =
752 GC_malloc_atomic_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
754 if (result == 0) {
755 GC_err_printf1(
756 "GC_debug_malloc_atomic_uncollectable(%ld) returning NIL (",
757 (unsigned long) lb);
758 GC_err_puts(s);
759 GC_err_printf1(":%ld)\n", (unsigned long)i);
760 return(0);
762 if (!GC_debugging_started) {
763 GC_start_debugging();
765 ADD_CALL_CHAIN(result, ra);
766 return (GC_store_debug_info(result, (word)lb, s, (word)i));
768 #endif /* ATOMIC_UNCOLLECTABLE */
770 # ifdef __STDC__
771 void GC_debug_free(GC_PTR p)
772 # else
773 void GC_debug_free(p)
774 GC_PTR p;
775 # endif
777 register GC_PTR base;
778 register ptr_t clobbered;
780 if (0 == p) return;
781 base = GC_base(p);
782 if (base == 0) {
783 GC_err_printf1("Attempt to free invalid pointer %lx\n",
784 (unsigned long)p);
785 ABORT("free(invalid pointer)");
787 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
788 GC_err_printf1(
789 "GC_debug_free called on pointer %lx wo debugging info\n",
790 (unsigned long)p);
791 } else {
792 # ifndef SHORT_DBG_HDRS
793 clobbered = GC_check_annotated_obj((oh *)base);
794 if (clobbered != 0) {
795 if (((oh *)base) -> oh_sz == GC_size(base)) {
796 GC_err_printf0(
797 "GC_debug_free: found previously deallocated (?) object at ");
798 } else {
799 GC_err_printf0("GC_debug_free: found smashed location at ");
801 GC_print_smashed_obj(p, clobbered);
803 /* Invalidate size */
804 ((oh *)base) -> oh_sz = GC_size(base);
805 # endif /* SHORT_DBG_HDRS */
807 if (GC_find_leak) {
808 GC_free(base);
809 } else {
810 register hdr * hhdr = HDR(p);
811 GC_bool uncollectable = FALSE;
813 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
814 uncollectable = TRUE;
816 # ifdef ATOMIC_UNCOLLECTABLE
817 if (hhdr -> hb_obj_kind == AUNCOLLECTABLE) {
818 uncollectable = TRUE;
820 # endif
821 if (uncollectable) {
822 GC_free(base);
823 } else {
824 size_t i;
825 size_t obj_sz = hhdr -> hb_sz - BYTES_TO_WORDS(sizeof(oh));
827 for (i = 0; i < obj_sz; ++i) ((word *)p)[i] = 0xdeadbeef;
828 GC_ASSERT((word *)p + i == (word *)base + hhdr -> hb_sz);
830 } /* !GC_find_leak */
833 #ifdef THREADS
835 extern void GC_free_inner(GC_PTR p);
837 /* Used internally; we assume it's called correctly. */
838 void GC_debug_free_inner(GC_PTR p)
840 GC_free_inner(GC_base(p));
842 #endif
844 # ifdef __STDC__
845 GC_PTR GC_debug_realloc(GC_PTR p, size_t lb, GC_EXTRA_PARAMS)
846 # else
847 GC_PTR GC_debug_realloc(p, lb, s, i)
848 GC_PTR p;
849 size_t lb;
850 char *s;
851 int i;
852 # endif
854 register GC_PTR base = GC_base(p);
855 register ptr_t clobbered;
856 register GC_PTR result;
857 register size_t copy_sz = lb;
858 register size_t old_sz;
859 register hdr * hhdr;
861 if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
862 if (base == 0) {
863 GC_err_printf1(
864 "Attempt to reallocate invalid pointer %lx\n", (unsigned long)p);
865 ABORT("realloc(invalid pointer)");
867 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
868 GC_err_printf1(
869 "GC_debug_realloc called on pointer %lx wo debugging info\n",
870 (unsigned long)p);
871 return(GC_realloc(p, lb));
873 hhdr = HDR(base);
874 switch (hhdr -> hb_obj_kind) {
875 # ifdef STUBBORN_ALLOC
876 case STUBBORN:
877 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
878 break;
879 # endif
880 case NORMAL:
881 result = GC_debug_malloc(lb, OPT_RA s, i);
882 break;
883 case PTRFREE:
884 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
885 break;
886 case UNCOLLECTABLE:
887 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
888 break;
889 # ifdef ATOMIC_UNCOLLECTABLE
890 case AUNCOLLECTABLE:
891 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
892 break;
893 # endif
894 default:
895 GC_err_printf0("GC_debug_realloc: encountered bad kind\n");
896 ABORT("bad kind");
898 # ifdef SHORT_DBG_HDRS
899 old_sz = GC_size(base) - sizeof(oh);
900 # else
901 clobbered = GC_check_annotated_obj((oh *)base);
902 if (clobbered != 0) {
903 GC_err_printf0("GC_debug_realloc: found smashed location at ");
904 GC_print_smashed_obj(p, clobbered);
906 old_sz = ((oh *)base) -> oh_sz;
907 # endif
908 if (old_sz < copy_sz) copy_sz = old_sz;
909 if (result == 0) return(0);
910 BCOPY(p, result, copy_sz);
911 GC_debug_free(p);
912 return(result);
915 #ifndef SHORT_DBG_HDRS
917 /* List of smashed objects. We defer printing these, since we can't */
918 /* always print them nicely with the allocation lock held. */
919 /* We put them here instead of in GC_arrays, since it may be useful to */
920 /* be able to look at them with the debugger. */
921 #define MAX_SMASHED 20
922 ptr_t GC_smashed[MAX_SMASHED];
923 unsigned GC_n_smashed = 0;
925 # if defined(__STDC__) || defined(__cplusplus)
926 void GC_add_smashed(ptr_t smashed)
927 # else
928 void GC_add_smashed(smashed)
929 ptr_t smashed;
930 #endif
932 GC_ASSERT(GC_is_marked(GC_base(smashed)));
933 GC_smashed[GC_n_smashed] = smashed;
934 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
935 /* In case of overflow, we keep the first MAX_SMASHED-1 */
936 /* entries plus the last one. */
937 GC_have_errors = TRUE;
940 /* Print all objects on the list. Clear the list. */
941 void GC_print_all_smashed_proc ()
943 unsigned i;
945 GC_ASSERT(!I_HOLD_LOCK());
946 if (GC_n_smashed == 0) return;
947 GC_err_printf0("GC_check_heap_block: found smashed heap objects:\n");
948 for (i = 0; i < GC_n_smashed; ++i) {
949 GC_print_smashed_obj(GC_base(GC_smashed[i]), GC_smashed[i]);
950 GC_smashed[i] = 0;
952 GC_n_smashed = 0;
955 /* Check all marked objects in the given block for validity */
956 /*ARGSUSED*/
957 # if defined(__STDC__) || defined(__cplusplus)
958 void GC_check_heap_block(register struct hblk *hbp, word dummy)
959 # else
960 void GC_check_heap_block(hbp, dummy)
961 register struct hblk *hbp; /* ptr to current heap block */
962 word dummy;
963 # endif
965 register struct hblkhdr * hhdr = HDR(hbp);
966 register word sz = hhdr -> hb_sz;
967 register int word_no;
968 register word *p, *plim;
970 p = (word *)(hbp->hb_body);
971 word_no = 0;
972 if (sz > MAXOBJSZ) {
973 plim = p;
974 } else {
975 plim = (word *)((((word)hbp) + HBLKSIZE) - WORDS_TO_BYTES(sz));
977 /* go through all words in block */
978 while( p <= plim ) {
979 if( mark_bit_from_hdr(hhdr, word_no)
980 && GC_HAS_DEBUG_INFO((ptr_t)p)) {
981 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
983 if (clobbered != 0) GC_add_smashed(clobbered);
985 word_no += sz;
986 p += sz;
991 /* This assumes that all accessible objects are marked, and that */
992 /* I hold the allocation lock. Normally called by collector. */
993 void GC_check_heap_proc()
995 # ifndef SMALL_CONFIG
996 # ifdef ALIGN_DOUBLE
997 GC_STATIC_ASSERT((sizeof(oh) & (2 * sizeof(word) - 1)) == 0);
998 # else
999 GC_STATIC_ASSERT((sizeof(oh) & (sizeof(word) - 1)) == 0);
1000 # endif
1001 # endif
1002 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
1005 #endif /* !SHORT_DBG_HDRS */
1007 struct closure {
1008 GC_finalization_proc cl_fn;
1009 GC_PTR cl_data;
1012 # ifdef __STDC__
1013 void * GC_make_closure(GC_finalization_proc fn, void * data)
1014 # else
1015 GC_PTR GC_make_closure(fn, data)
1016 GC_finalization_proc fn;
1017 GC_PTR data;
1018 # endif
1020 struct closure * result =
1021 # ifdef DBG_HDRS_ALL
1022 (struct closure *) GC_debug_malloc(sizeof (struct closure),
1023 GC_EXTRAS);
1024 # else
1025 (struct closure *) GC_malloc(sizeof (struct closure));
1026 # endif
1028 result -> cl_fn = fn;
1029 result -> cl_data = data;
1030 return((GC_PTR)result);
1033 # ifdef __STDC__
1034 void GC_debug_invoke_finalizer(void * obj, void * data)
1035 # else
1036 void GC_debug_invoke_finalizer(obj, data)
1037 char * obj;
1038 char * data;
1039 # endif
1041 register struct closure * cl = (struct closure *) data;
1043 (*(cl -> cl_fn))((GC_PTR)((char *)obj + sizeof(oh)), cl -> cl_data);
1046 /* Set ofn and ocd to reflect the values we got back. */
1047 static void store_old (obj, my_old_fn, my_old_cd, ofn, ocd)
1048 GC_PTR obj;
1049 GC_finalization_proc my_old_fn;
1050 struct closure * my_old_cd;
1051 GC_finalization_proc *ofn;
1052 GC_PTR *ocd;
1054 if (0 != my_old_fn) {
1055 if (my_old_fn != GC_debug_invoke_finalizer) {
1056 GC_err_printf1("Debuggable object at 0x%lx had non-debug finalizer.\n",
1057 obj);
1058 /* This should probably be fatal. */
1059 } else {
1060 if (ofn) *ofn = my_old_cd -> cl_fn;
1061 if (ocd) *ocd = my_old_cd -> cl_data;
1063 } else {
1064 if (ofn) *ofn = 0;
1065 if (ocd) *ocd = 0;
1069 # ifdef __STDC__
1070 void GC_debug_register_finalizer(GC_PTR obj, GC_finalization_proc fn,
1071 GC_PTR cd, GC_finalization_proc *ofn,
1072 GC_PTR *ocd)
1073 # else
1074 void GC_debug_register_finalizer(obj, fn, cd, ofn, ocd)
1075 GC_PTR obj;
1076 GC_finalization_proc fn;
1077 GC_PTR cd;
1078 GC_finalization_proc *ofn;
1079 GC_PTR *ocd;
1080 # endif
1082 GC_finalization_proc my_old_fn;
1083 GC_PTR my_old_cd;
1084 ptr_t base = GC_base(obj);
1085 if (0 == base) return;
1086 if ((ptr_t)obj - base != sizeof(oh)) {
1087 GC_err_printf1(
1088 "GC_debug_register_finalizer called with non-base-pointer 0x%lx\n",
1089 obj);
1091 if (0 == fn) {
1092 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1093 } else {
1094 GC_register_finalizer(base, GC_debug_invoke_finalizer,
1095 GC_make_closure(fn,cd), &my_old_fn, &my_old_cd);
1097 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1100 # ifdef __STDC__
1101 void GC_debug_register_finalizer_no_order
1102 (GC_PTR obj, GC_finalization_proc fn,
1103 GC_PTR cd, GC_finalization_proc *ofn,
1104 GC_PTR *ocd)
1105 # else
1106 void GC_debug_register_finalizer_no_order
1107 (obj, fn, cd, ofn, ocd)
1108 GC_PTR obj;
1109 GC_finalization_proc fn;
1110 GC_PTR cd;
1111 GC_finalization_proc *ofn;
1112 GC_PTR *ocd;
1113 # endif
1115 GC_finalization_proc my_old_fn;
1116 GC_PTR my_old_cd;
1117 ptr_t base = GC_base(obj);
1118 if (0 == base) return;
1119 if ((ptr_t)obj - base != sizeof(oh)) {
1120 GC_err_printf1(
1121 "GC_debug_register_finalizer_no_order called with non-base-pointer 0x%lx\n",
1122 obj);
1124 if (0 == fn) {
1125 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1126 } else {
1127 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1128 GC_make_closure(fn,cd), &my_old_fn,
1129 &my_old_cd);
1131 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1134 # ifdef __STDC__
1135 void GC_debug_register_finalizer_ignore_self
1136 (GC_PTR obj, GC_finalization_proc fn,
1137 GC_PTR cd, GC_finalization_proc *ofn,
1138 GC_PTR *ocd)
1139 # else
1140 void GC_debug_register_finalizer_ignore_self
1141 (obj, fn, cd, ofn, ocd)
1142 GC_PTR obj;
1143 GC_finalization_proc fn;
1144 GC_PTR cd;
1145 GC_finalization_proc *ofn;
1146 GC_PTR *ocd;
1147 # endif
1149 GC_finalization_proc my_old_fn;
1150 GC_PTR my_old_cd;
1151 ptr_t base = GC_base(obj);
1152 if (0 == base) return;
1153 if ((ptr_t)obj - base != sizeof(oh)) {
1154 GC_err_printf1(
1155 "GC_debug_register_finalizer_ignore_self called with non-base-pointer 0x%lx\n",
1156 obj);
1158 if (0 == fn) {
1159 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1160 } else {
1161 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1162 GC_make_closure(fn,cd), &my_old_fn,
1163 &my_old_cd);
1165 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1168 #ifdef GC_ADD_CALLER
1169 # define RA GC_RETURN_ADDR,
1170 #else
1171 # define RA
1172 #endif
1174 GC_PTR GC_debug_malloc_replacement(lb)
1175 size_t lb;
1177 return GC_debug_malloc(lb, RA "unknown", 0);
1180 GC_PTR GC_debug_realloc_replacement(p, lb)
1181 GC_PTR p;
1182 size_t lb;
1184 return GC_debug_realloc(p, lb, RA "unknown", 0);