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