TIVI-153: Add as dependency for Iputils
[profile/ivi/gc.git] / 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  * Copyright (C) 2007 Free Software Foundation, Inc
7  *
8  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
10  *
11  * Permission is hereby granted to use or copy this program
12  * for any purpose,  provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  */
17
18 #include <errno.h>
19 #include <string.h>
20 #include "private/dbg_mlc.h"
21
22 void GC_default_print_heap_obj_proc();
23 GC_API void GC_register_finalizer_no_order
24         (void * obj, GC_finalization_proc fn, void * cd,
25          GC_finalization_proc *ofn, void * *ocd);
26
27
28 #ifndef SHORT_DBG_HDRS
29 /* Check whether object with base pointer p has debugging info  */ 
30 /* p is assumed to point to a legitimate object in our part     */
31 /* of the heap.                                                 */
32 /* This excludes the check as to whether the back pointer is    */
33 /* odd, which is added by the GC_HAS_DEBUG_INFO macro.          */
34 /* Note that if DBG_HDRS_ALL is set, uncollectable objects      */
35 /* on free lists may not have debug information set.  Thus it's */
36 /* not always safe to return TRUE, even if the client does      */
37 /* its part.                                                    */
38 GC_bool GC_has_other_debug_info(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(__GLIBC__) || defined(SOLARIS) \
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         size_t 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_printf("\n");
186     for (i = 0; ; ++i) {
187       source = GC_get_back_ptr_info(current, &base, &offset);
188       if (GC_UNREFERENCED == source) {
189         GC_err_printf("Reference could not be found\n");
190         goto out;
191       }
192       if (GC_NO_SPACE == source) {
193         GC_err_printf("No debug info in object: Can't find reference\n");
194         goto out;
195       }
196       GC_err_printf("Reachable via %d levels of pointers from ",
197                  (unsigned long)i);
198       switch(source) {
199         case GC_REFD_FROM_ROOT:
200           GC_err_printf("root at %p\n\n", base);
201           goto out;
202         case GC_REFD_FROM_REG:
203           GC_err_printf("root in register\n\n");
204           goto out;
205         case GC_FINALIZER_REFD:
206           GC_err_printf("list of finalizable objects\n\n");
207           goto out;
208         case GC_REFD_FROM_HEAP:
209           GC_err_printf("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_printf("\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_printf("\n****Chose address %p in object\n", 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(ptr_t p, word sz, const char *string, word integer)
243 {
244     register word * result = (word *)((oh *)p + 1);
245     DCL_LOCK_STATE;
246     
247     /* There is some argument that we should dissble signals here.      */
248     /* But that's expensive.  And this way things should only appear    */
249     /* inconsistent while we're in the handler.                         */
250     LOCK();
251     GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
252     GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
253 #   ifdef KEEP_BACK_PTRS
254       ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
255 #   endif
256 #   ifdef MAKE_BACK_GRAPH
257       ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
258 #   endif
259     ((oh *)p) -> oh_string = string;
260     ((oh *)p) -> oh_int = integer;
261 #   ifndef SHORT_DBG_HDRS
262       ((oh *)p) -> oh_sz = sz;
263       ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
264       ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
265          result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
266 #   endif
267     UNLOCK();
268     return((ptr_t)result);
269 }
270
271 #ifdef DBG_HDRS_ALL
272 /* Store debugging info into p.  Return displaced pointer.         */
273 /* This version assumes we do hold the allocation lock.            */
274 ptr_t GC_store_debug_info_inner(ptr_t p, word sz, char *string, word integer)
275 {
276     register word * result = (word *)((oh *)p + 1);
277     
278     /* There is some argument that we should disable signals here.      */
279     /* But that's expensive.  And this way things should only appear    */
280     /* inconsistent while we're in the handler.                         */
281     GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
282     GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
283 #   ifdef KEEP_BACK_PTRS
284       ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
285 #   endif
286 #   ifdef MAKE_BACK_GRAPH
287       ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
288 #   endif
289     ((oh *)p) -> oh_string = string;
290     ((oh *)p) -> oh_int = integer;
291 #   ifndef SHORT_DBG_HDRS
292       ((oh *)p) -> oh_sz = sz;
293       ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
294       ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
295          result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
296 #   endif
297     return((ptr_t)result);
298 }
299 #endif
300
301 #ifndef SHORT_DBG_HDRS
302 /* Check the object with debugging info at ohdr         */
303 /* return NIL if it's OK.  Else return clobbered        */
304 /* address.                                             */
305 ptr_t GC_check_annotated_obj(oh *ohdr)
306 {
307     register ptr_t body = (ptr_t)(ohdr + 1);
308     register word gc_sz = GC_size((ptr_t)ohdr);
309     if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
310         return((ptr_t)(&(ohdr -> oh_sz)));
311     }
312     if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
313         return((ptr_t)(&(ohdr -> oh_sf)));
314     }
315     if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
316         return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
317     }
318     if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
319         != (END_FLAG ^ (word)body)) {
320         return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)));
321     }
322     return(0);
323 }
324 #endif /* !SHORT_DBG_HDRS */
325
326 static GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
327
328 void GC_register_describe_type_fn(int kind, GC_describe_type_fn fn)
329 {
330   GC_describe_type_fns[kind] = fn;
331 }
332
333 /* Print a type description for the object whose client-visible address */
334 /* is p.                                                                */
335 void GC_print_type(ptr_t p)
336 {
337     hdr * hhdr = GC_find_header(p);
338     char buffer[GC_TYPE_DESCR_LEN + 1];
339     int kind = hhdr -> hb_obj_kind;
340
341     if (0 != GC_describe_type_fns[kind] && GC_is_marked(GC_base(p))) {
342         /* This should preclude free list objects except with   */
343         /* thread-local allocation.                             */
344         buffer[GC_TYPE_DESCR_LEN] = 0;
345         (GC_describe_type_fns[kind])(p, buffer);
346         GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
347         GC_err_puts(buffer);
348     } else {
349         switch(kind) {
350           case PTRFREE:
351             GC_err_puts("PTRFREE");
352             break;
353           case NORMAL:
354             GC_err_puts("NORMAL");
355             break;
356           case UNCOLLECTABLE:
357             GC_err_puts("UNCOLLECTABLE");
358             break;
359 #         ifdef ATOMIC_UNCOLLECTABLE
360             case AUNCOLLECTABLE:
361               GC_err_puts("ATOMIC UNCOLLECTABLE");
362               break;
363 #         endif
364           case STUBBORN:
365             GC_err_puts("STUBBORN");
366             break;
367           default:
368             GC_err_printf("kind %d, descr 0x%lx", kind,
369                           (unsigned long)(hhdr -> hb_descr));
370         }
371     }
372 }
373
374     
375
376 void GC_print_obj(ptr_t p)
377 {
378     register oh * ohdr = (oh *)GC_base(p);
379     
380     GC_ASSERT(I_DONT_HOLD_LOCK());
381     GC_err_printf("%p (", ((ptr_t)ohdr + sizeof(oh)));
382     GC_err_puts(ohdr -> oh_string);
383 #   ifdef SHORT_DBG_HDRS
384       GC_err_printf(":%ld, ", (unsigned long)(ohdr -> oh_int));
385 #   else
386       GC_err_printf(":%ld, sz=%ld, ", (unsigned long)(ohdr -> oh_int),
387                                         (unsigned long)(ohdr -> oh_sz));
388 #   endif
389     GC_print_type((ptr_t)(ohdr + 1));
390     GC_err_puts(")\n");
391     PRINT_CALL_CHAIN(ohdr);
392 }
393
394 void GC_debug_print_heap_obj_proc(ptr_t p)
395 {
396     GC_ASSERT(I_DONT_HOLD_LOCK());
397     if (GC_HAS_DEBUG_INFO(p)) {
398         GC_print_obj(p);
399     } else {
400         GC_default_print_heap_obj_proc(p);
401     }
402 }
403
404 #ifndef SHORT_DBG_HDRS
405 /* Use GC_err_printf and friends to print a description of the object   */
406 /* whose client-visible address is p, and which was smashed at          */
407 /* clobbered_addr.                                                      */
408 void GC_print_smashed_obj(ptr_t p, ptr_t clobbered_addr)
409 {
410     register oh * ohdr = (oh *)GC_base(p);
411     
412     GC_ASSERT(I_DONT_HOLD_LOCK());
413     GC_err_printf("%p in or near object at %p(", clobbered_addr, p);
414     if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
415         || ohdr -> oh_string == 0) {
416         GC_err_printf("<smashed>, appr. sz = %ld)\n",
417                        (GC_size((ptr_t)ohdr) - DEBUG_BYTES));
418     } else {
419         if (ohdr -> oh_string[0] == '\0') {
420             GC_err_puts("EMPTY(smashed?)");
421         } else {
422             GC_err_puts(ohdr -> oh_string);
423         }
424         GC_err_printf(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
425                                           (unsigned long)(ohdr -> oh_sz));
426         PRINT_CALL_CHAIN(ohdr);
427     }
428 }
429 #endif
430
431 void GC_check_heap_proc (void);
432
433 void GC_print_all_smashed_proc (void);
434
435 void GC_do_nothing(void) {}
436
437 void GC_start_debugging(void)
438 {
439 #   ifndef SHORT_DBG_HDRS
440       GC_check_heap = GC_check_heap_proc;
441       GC_print_all_smashed = GC_print_all_smashed_proc;
442 #   else
443       GC_check_heap = GC_do_nothing;
444       GC_print_all_smashed = GC_do_nothing;
445 #   endif
446     GC_print_heap_obj = GC_debug_print_heap_obj_proc;
447     GC_debugging_started = TRUE;
448     GC_register_displacement((word)sizeof(oh));
449 }
450
451 size_t GC_debug_header_size = sizeof(oh);
452
453 void GC_debug_register_displacement(size_t offset)
454 {
455     GC_register_displacement(offset);
456     GC_register_displacement((word)sizeof(oh) + offset);
457 }
458
459 void * GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS)
460 {
461     void * result = GC_malloc(lb + DEBUG_BYTES);
462     
463     if (result == 0) {
464         GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
465                       (unsigned long) lb);
466         GC_err_puts(s);
467         GC_err_printf(":%ld)\n", (unsigned long)i);
468         return(0);
469     }
470     if (!GC_debugging_started) {
471         GC_start_debugging();
472     }
473     ADD_CALL_CHAIN(result, ra);
474     return (GC_store_debug_info(result, (word)lb, s, (word)i));
475 }
476
477 void * GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
478 {
479     void * result = GC_malloc_ignore_off_page(lb + DEBUG_BYTES);
480     
481     if (result == 0) {
482         GC_err_printf("GC_debug_malloc_ignore_off_page(%lu) returning NIL (",
483                        (unsigned long) lb);
484         GC_err_puts(s);
485         GC_err_printf(":%lu)\n", (unsigned long)i);
486         return(0);
487     }
488     if (!GC_debugging_started) {
489         GC_start_debugging();
490     }
491     ADD_CALL_CHAIN(result, ra);
492     return (GC_store_debug_info(result, (word)lb, s, (word)i));
493 }
494
495 void * GC_debug_malloc_atomic_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
496 {
497     void * result = GC_malloc_atomic_ignore_off_page(lb + DEBUG_BYTES);
498     
499     if (result == 0) {
500         GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
501                        " returning NIL (", (unsigned long) lb);
502         GC_err_puts(s);
503         GC_err_printf(":%lu)\n", (unsigned long)i);
504         return(0);
505     }
506     if (!GC_debugging_started) {
507         GC_start_debugging();
508     }
509     ADD_CALL_CHAIN(result, ra);
510     return (GC_store_debug_info(result, (word)lb, s, (word)i));
511 }
512
513 # ifdef DBG_HDRS_ALL
514 /* 
515  * An allocation function for internal use.
516  * Normally internally allocated objects do not have debug information.
517  * But in this case, we need to make sure that all objects have debug
518  * headers.
519  * We assume debugging was started in collector initialization,
520  * and we already hold the GC lock.
521  */
522   void * GC_debug_generic_malloc_inner(size_t lb, int k)
523   {
524     void * result = GC_generic_malloc_inner(lb + DEBUG_BYTES, k);
525     
526     if (result == 0) {
527         GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
528                        (unsigned long) lb);
529         return(0);
530     }
531     ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
532     return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
533   }
534
535   void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb, int k)
536   {
537     void * result = GC_generic_malloc_inner_ignore_off_page(
538                                                 lb + DEBUG_BYTES, k);
539     
540     if (result == 0) {
541         GC_err_printf("GC internal allocation (%lu bytes) returning NIL\n",
542                        (unsigned long) lb);
543         return(0);
544     }
545     ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
546     return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", (word)0));
547   }
548 # endif
549
550 #ifdef STUBBORN_ALLOC
551 void * GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
552 {
553     void * result = GC_malloc_stubborn(lb + DEBUG_BYTES);
554     
555     if (result == 0) {
556         GC_err_printf("GC_debug_malloc(%lu) returning NIL (",
557                       (unsigned long) lb);
558         GC_err_puts(s);
559         GC_err_printf(":%lu)\n", (unsigned long)i);
560         return(0);
561     }
562     if (!GC_debugging_started) {
563         GC_start_debugging();
564     }
565     ADD_CALL_CHAIN(result, ra);
566     return (GC_store_debug_info(result, (word)lb, s, (word)i));
567 }
568
569 void GC_debug_change_stubborn(void *p)
570 {
571     void * q = GC_base(p);
572     hdr * hhdr;
573     
574     if (q == 0) {
575         GC_err_printf("Bad argument: %p to GC_debug_change_stubborn\n", p);
576         ABORT("GC_debug_change_stubborn: bad arg");
577     }
578     hhdr = HDR(q);
579     if (hhdr -> hb_obj_kind != STUBBORN) {
580         GC_err_printf("GC_debug_change_stubborn arg not stubborn: %p\n", p);
581         ABORT("GC_debug_change_stubborn: arg not stubborn");
582     }
583     GC_change_stubborn(q);
584 }
585
586 void GC_debug_end_stubborn_change(void *p)
587 {
588     register void * q = GC_base(p);
589     register hdr * hhdr;
590     
591     if (q == 0) {
592         GC_err_printf("Bad argument: %p to GC_debug_end_stubborn_change\n", p);
593         ABORT("GC_debug_end_stubborn_change: bad arg");
594     }
595     hhdr = HDR(q);
596     if (hhdr -> hb_obj_kind != STUBBORN) {
597         GC_err_printf("debug_end_stubborn_change arg not stubborn: %p\n", p);
598         ABORT("GC_debug_end_stubborn_change: arg not stubborn");
599     }
600     GC_end_stubborn_change(q);
601 }
602
603 #else /* !STUBBORN_ALLOC */
604
605 void * GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
606 {
607     return GC_debug_malloc(lb, OPT_RA s, i);
608 }
609
610 void GC_debug_change_stubborn(void *p)
611 {
612 }
613
614 void GC_debug_end_stubborn_change(void *p)
615 {
616 }
617
618 #endif /* !STUBBORN_ALLOC */
619
620 void * GC_debug_malloc_atomic(size_t lb, GC_EXTRA_PARAMS)
621 {
622     void * result = GC_malloc_atomic(lb + DEBUG_BYTES);
623     
624     if (result == 0) {
625         GC_err_printf("GC_debug_malloc_atomic(%lu) returning NIL (",
626                       (unsigned long) lb);
627         GC_err_puts(s);
628         GC_err_printf(":%lu)\n", (unsigned long)i);
629         return(0);
630     }
631     if (!GC_debugging_started) {
632         GC_start_debugging();
633     }
634     ADD_CALL_CHAIN(result, ra);
635     return (GC_store_debug_info(result, (word)lb, s, (word)i));
636 }
637
638 char *GC_debug_strdup(const char *str, GC_EXTRA_PARAMS)
639 {
640     char *copy;
641     if (str == NULL) return NULL;
642     copy = GC_debug_malloc_atomic(strlen(str) + 1, OPT_RA s, i);
643     if (copy == NULL) {
644       errno = ENOMEM;
645       return NULL;
646     }
647     strcpy(copy, str);
648     return copy;
649 }
650
651 void * GC_debug_malloc_uncollectable(size_t lb, GC_EXTRA_PARAMS)
652 {
653     void * result = GC_malloc_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
654     
655     if (result == 0) {
656         GC_err_printf("GC_debug_malloc_uncollectable(%lu) returning NIL (",
657                       (unsigned long) lb);
658         GC_err_puts(s);
659         GC_err_printf(":%lu)\n", (unsigned long)i);
660         return(0);
661     }
662     if (!GC_debugging_started) {
663         GC_start_debugging();
664     }
665     ADD_CALL_CHAIN(result, ra);
666     return (GC_store_debug_info(result, (word)lb, s, (word)i));
667 }
668
669 #ifdef ATOMIC_UNCOLLECTABLE
670 void * GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
671 {
672     void * result =
673         GC_malloc_atomic_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
674     
675     if (result == 0) {
676         GC_err_printf(
677                 "GC_debug_malloc_atomic_uncollectable(%lu) returning NIL (",
678                 (unsigned long) lb);
679         GC_err_puts(s);
680         GC_err_printf(":%lu)\n", (unsigned long)i);
681         return(0);
682     }
683     if (!GC_debugging_started) {
684         GC_start_debugging();
685     }
686     ADD_CALL_CHAIN(result, ra);
687     return (GC_store_debug_info(result, (word)lb, s, (word)i));
688 }
689 #endif /* ATOMIC_UNCOLLECTABLE */
690
691 void GC_debug_free(void * p)
692 {
693     ptr_t base;
694     ptr_t clobbered;
695     
696     if (0 == p) return;
697     base = GC_base(p);
698     if (base == 0) {
699         GC_err_printf("Attempt to free invalid pointer %p\n", p);
700         ABORT("free(invalid pointer)");
701     }
702     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
703         GC_err_printf(
704                   "GC_debug_free called on pointer %p wo debugging info\n", p);
705     } else {
706 #     ifndef SHORT_DBG_HDRS
707         clobbered = GC_check_annotated_obj((oh *)base);
708         if (clobbered != 0) {
709           if (((oh *)base) -> oh_sz == GC_size(base)) {
710             GC_err_printf(
711                   "GC_debug_free: found previously deallocated (?) object at ");
712           } else {
713             GC_err_printf("GC_debug_free: found smashed location at ");
714           }
715           GC_print_smashed_obj(p, clobbered);
716         }
717         /* Invalidate size */
718         ((oh *)base) -> oh_sz = GC_size(base);
719 #     endif /* SHORT_DBG_HDRS */
720     }
721     if (GC_find_leak) {
722         GC_free(base);
723     } else {
724         hdr * hhdr = HDR(p);
725         GC_bool uncollectable = FALSE;
726
727         if (hhdr ->  hb_obj_kind == UNCOLLECTABLE) {
728             uncollectable = TRUE;
729         }
730 #       ifdef ATOMIC_UNCOLLECTABLE
731             if (hhdr ->  hb_obj_kind == AUNCOLLECTABLE) {
732                     uncollectable = TRUE;
733             }
734 #       endif
735         if (uncollectable) {
736             GC_free(base);
737         } else {
738             size_t i;
739             size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
740
741             for (i = 0; i < obj_sz; ++i) ((word *)p)[i] = 0xdeadbeef;
742             GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
743         }
744     } /* !GC_find_leak */
745 }
746
747 #ifdef THREADS
748
749 extern void GC_free_inner(void * p);
750
751 /* Used internally; we assume it's called correctly.    */
752 void GC_debug_free_inner(void * p)
753 {
754     GC_free_inner(GC_base(p));
755 }
756 #endif
757
758 void * GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
759 {
760     void * base = GC_base(p);
761     ptr_t clobbered;
762     void * result;
763     size_t copy_sz = lb;
764     size_t old_sz;
765     hdr * hhdr;
766     
767     if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
768     if (base == 0) {
769         GC_err_printf("Attempt to reallocate invalid pointer %p\n", p);
770         ABORT("realloc(invalid pointer)");
771     }
772     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
773         GC_err_printf(
774                 "GC_debug_realloc called on pointer %p wo debugging info\n", p);
775         return(GC_realloc(p, lb));
776     }
777     hhdr = HDR(base);
778     switch (hhdr -> hb_obj_kind) {
779 #    ifdef STUBBORN_ALLOC
780       case STUBBORN:
781         result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
782         break;
783 #    endif
784       case NORMAL:
785         result = GC_debug_malloc(lb, OPT_RA s, i);
786         break;
787       case PTRFREE:
788         result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
789         break;
790       case UNCOLLECTABLE:
791         result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
792         break;
793 #    ifdef ATOMIC_UNCOLLECTABLE
794       case AUNCOLLECTABLE:
795         result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
796         break;
797 #    endif
798       default:
799         GC_err_printf("GC_debug_realloc: encountered bad kind\n");
800         ABORT("bad kind");
801     }
802 #   ifdef SHORT_DBG_HDRS
803       old_sz = GC_size(base) - sizeof(oh);
804 #   else
805       clobbered = GC_check_annotated_obj((oh *)base);
806       if (clobbered != 0) {
807         GC_err_printf("GC_debug_realloc: found smashed location at ");
808         GC_print_smashed_obj(p, clobbered);
809       }
810       old_sz = ((oh *)base) -> oh_sz;
811 #   endif
812     if (old_sz < copy_sz) copy_sz = old_sz;
813     if (result == 0) return(0);
814     BCOPY(p, result,  copy_sz);
815     GC_debug_free(p);
816     return(result);
817 }
818
819 #ifndef SHORT_DBG_HDRS
820
821 /* List of smashed objects.  We defer printing these, since we can't    */
822 /* always print them nicely with the allocation lock held.              */
823 /* We put them here instead of in GC_arrays, since it may be useful to  */
824 /* be able to look at them with the debugger.                           */
825 #define MAX_SMASHED 20
826 ptr_t GC_smashed[MAX_SMASHED];
827 unsigned GC_n_smashed = 0;
828
829 void GC_add_smashed(ptr_t smashed)
830 {
831     GC_ASSERT(GC_is_marked(GC_base(smashed)));
832     GC_smashed[GC_n_smashed] = smashed;
833     if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
834       /* In case of overflow, we keep the first MAX_SMASHED-1   */
835       /* entries plus the last one.                             */
836     GC_have_errors = TRUE;
837 }
838
839 /* Print all objects on the list.  Clear the list.      */
840 void GC_print_all_smashed_proc(void)
841 {
842     unsigned i;
843
844     GC_ASSERT(I_DONT_HOLD_LOCK());
845     if (GC_n_smashed == 0) return;
846     GC_err_printf("GC_check_heap_block: found smashed heap objects:\n");
847     for (i = 0; i < GC_n_smashed; ++i) {
848         GC_print_smashed_obj((ptr_t)GC_base(GC_smashed[i]) + sizeof(oh),
849                              GC_smashed[i]);
850         GC_smashed[i] = 0;
851     }
852     GC_n_smashed = 0;
853 }
854
855 /* Check all marked objects in the given block for validity     */
856 /* Avoid GC_apply_to_each_object for performance reasons.       */
857 /*ARGSUSED*/
858 void GC_check_heap_block(struct hblk *hbp, word dummy)
859 {
860     struct hblkhdr * hhdr = HDR(hbp);
861     size_t sz = hhdr -> hb_sz;
862     size_t bit_no;
863     char *p, *plim;
864     
865     p = hbp->hb_body;
866     bit_no = 0;
867     if (sz > MAXOBJBYTES) {
868         plim = p;
869     } else {
870         plim = hbp->hb_body + HBLKSIZE - sz;
871     }
872     /* go through all words in block */
873         while( p <= plim ) {
874             if( mark_bit_from_hdr(hhdr, bit_no)
875                 && GC_HAS_DEBUG_INFO((ptr_t)p)) {
876                 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
877                 
878                 if (clobbered != 0) GC_add_smashed(clobbered);
879             }
880             bit_no += MARK_BIT_OFFSET(sz);
881             p += sz;
882         }
883 }
884
885
886 /* This assumes that all accessible objects are marked, and that        */
887 /* I hold the allocation lock.  Normally called by collector.           */
888 void GC_check_heap_proc(void)
889 {
890 #   ifndef SMALL_CONFIG
891       /* Ignore gcc no effect warning on the following.         */
892       GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
893       /* FIXME: Should we check for twice that alignment?       */
894 #   endif
895     GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
896 }
897
898 #endif /* !SHORT_DBG_HDRS */
899
900 struct closure {
901     GC_finalization_proc cl_fn;
902     void * cl_data;
903 };
904
905 void * GC_make_closure(GC_finalization_proc fn, void * data)
906 {
907     struct closure * result =
908 #   ifdef DBG_HDRS_ALL
909       (struct closure *) GC_debug_malloc(sizeof (struct closure),
910                                          GC_EXTRAS);
911 #   else
912       (struct closure *) GC_malloc(sizeof (struct closure));
913 #   endif
914     
915     result -> cl_fn = fn;
916     result -> cl_data = data;
917     return((void *)result);
918 }
919
920 void GC_debug_invoke_finalizer(void * obj, void * data)
921 {
922     register struct closure * cl = (struct closure *) data;
923     
924     (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
925
926
927 /* Set ofn and ocd to reflect the values we got back.   */
928 static void store_old (void *obj, GC_finalization_proc my_old_fn,
929                        struct closure *my_old_cd, GC_finalization_proc *ofn,
930                        void **ocd)
931 {
932     if (0 != my_old_fn) {
933       if (my_old_fn != GC_debug_invoke_finalizer) {
934         GC_err_printf("Debuggable object at %p had non-debug finalizer.\n",
935                       obj);
936         /* This should probably be fatal. */
937       } else {
938         if (ofn) *ofn = my_old_cd -> cl_fn;
939         if (ocd) *ocd = my_old_cd -> cl_data;
940       }
941     } else {
942       if (ofn) *ofn = 0;
943       if (ocd) *ocd = 0;
944     }
945 }
946
947 void GC_debug_register_finalizer(void * obj, GC_finalization_proc fn,
948                                  void * cd, GC_finalization_proc *ofn,
949                                  void * *ocd)
950 {
951     GC_finalization_proc my_old_fn;
952     void * my_old_cd;
953     ptr_t base = GC_base(obj);
954     if (0 == base) return;
955     if ((ptr_t)obj - base != sizeof(oh)) {
956         GC_err_printf(
957             "GC_debug_register_finalizer called with non-base-pointer %p\n",
958             obj);
959     }
960     if (0 == fn) {
961       GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
962     } else {
963       GC_register_finalizer(base, GC_debug_invoke_finalizer,
964                             GC_make_closure(fn,cd), &my_old_fn, &my_old_cd);
965     }
966     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
967 }
968
969 void GC_debug_register_finalizer_no_order
970                                     (void * obj, GC_finalization_proc fn,
971                                      void * cd, GC_finalization_proc *ofn,
972                                      void * *ocd)
973 {
974     GC_finalization_proc my_old_fn;
975     void * my_old_cd;
976     ptr_t base = GC_base(obj);
977     if (0 == base) return;
978     if ((ptr_t)obj - base != sizeof(oh)) {
979         GC_err_printf(
980           "GC_debug_register_finalizer_no_order called with "
981           "non-base-pointer %p\n",
982           obj);
983     }
984     if (0 == fn) {
985       GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
986     } else {
987       GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
988                                      GC_make_closure(fn,cd), &my_old_fn,
989                                      &my_old_cd);
990     }
991     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
992 }
993
994 void GC_debug_register_finalizer_unreachable
995                                     (void * obj, GC_finalization_proc fn,
996                                      void * cd, GC_finalization_proc *ofn,
997                                      void * *ocd)
998 {
999     GC_finalization_proc my_old_fn;
1000     void * my_old_cd;
1001     ptr_t base = GC_base(obj);
1002     if (0 == base) return;
1003     if ((ptr_t)obj - base != sizeof(oh)) {
1004         GC_err_printf(
1005             "GC_debug_register_finalizer_unreachable called with "
1006             "non-base-pointer %p\n",
1007             obj);
1008     }
1009     if (0 == fn) {
1010       GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1011     } else {
1012       GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1013                                         GC_make_closure(fn,cd), &my_old_fn,
1014                                         &my_old_cd);
1015     }
1016     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1017 }
1018
1019 void GC_debug_register_finalizer_ignore_self
1020                                     (void * obj, GC_finalization_proc fn,
1021                                      void * cd, GC_finalization_proc *ofn,
1022                                      void * *ocd)
1023 {
1024     GC_finalization_proc my_old_fn;
1025     void * my_old_cd;
1026     ptr_t base = GC_base(obj);
1027     if (0 == base) return;
1028     if ((ptr_t)obj - base != sizeof(oh)) {
1029         GC_err_printf(
1030             "GC_debug_register_finalizer_ignore_self called with "
1031             "non-base-pointer %p\n", obj);
1032     }
1033     if (0 == fn) {
1034       GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1035     } else {
1036       GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1037                                      GC_make_closure(fn,cd), &my_old_fn,
1038                                      &my_old_cd);
1039     }
1040     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1041 }
1042
1043 #ifdef GC_ADD_CALLER
1044 # define RA GC_RETURN_ADDR,
1045 #else
1046 # define RA
1047 #endif
1048
1049 void * GC_debug_malloc_replacement(size_t lb)
1050 {
1051     return GC_debug_malloc(lb, RA "unknown", 0);
1052 }
1053
1054 void * GC_debug_realloc_replacement(void *p, size_t lb)
1055 {
1056     return GC_debug_realloc(p, lb, RA "unknown", 0);
1057 }