2 * libc/stdlib/malloc/heap_debug.c -- optional heap debugging routines
4 * Copyright (C) 2002 NEC Corporation
5 * Copyright (C) 2002 Miles Bader <miles@gnu.org>
7 * This file is subject to the terms and conditions of the GNU Lesser
8 * General Public License. See the file COPYING.LIB in the main
9 * directory of this archive for more details.
11 * Written by Miles Bader <miles@gnu.org>
31 __heap_dump_freelist (struct heap_free_area *heap)
33 struct heap_free_area *fa;
34 for (fa = heap; fa; fa = fa->next)
35 __malloc_debug_printf (0,
36 "0x%lx: 0x%lx - 0x%lx (%d)\tP=0x%lx, N=0x%lx",
38 (long)HEAP_FREE_AREA_START (fa),
39 (long)HEAP_FREE_AREA_END (fa),
45 /* Output a text representation of HEAP to stderr, labelling it with STR. */
47 __heap_dump (struct heap_free_area *heap, const char *str)
49 static smallint recursed;
53 __heap_check (heap, str);
57 __malloc_debug_printf (1, "%s: heap @0x%lx:", str, (long)heap);
58 __heap_dump_freelist (heap);
59 __malloc_debug_indent (-1);
66 /* Output an error message to stderr, and exit. STR is printed with the
68 static void attribute_noreturn
69 __heap_check_failure (struct heap_free_area *heap, struct heap_free_area *fa,
70 const char *str, char *fmt, ...)
75 fprintf (stderr, "\nHEAP CHECK FAILURE %s: ", str);
77 fprintf (stderr, "\nHEAP CHECK FAILURE: ");
80 vfprintf (stderr, fmt, val);
83 fprintf (stderr, "\n");
85 __malloc_debug_set_indent (0);
86 __malloc_debug_printf (1, "heap dump:");
87 __heap_dump_freelist (heap);
92 /* Do some consistency checks on HEAP. If they fail, output an error
93 message to stderr, and exit. STR is printed with the failure message. */
95 __heap_check (struct heap_free_area *heap, const char *str)
97 typedef unsigned long ul_t;
98 struct heap_free_area *fa, *prev;
99 struct heap_free_area *first_fa = heap;
101 if (first_fa && first_fa->prev)
102 __heap_check_failure (heap, first_fa, str,
103 "first free-area has non-zero prev pointer:\n\
104 first free-area = 0x%lx\n\
105 (0x%lx)->prev = 0x%lx\n",
107 (ul_t)first_fa, (ul_t)first_fa->prev);
109 for (prev = 0, fa = first_fa; fa; prev = fa, fa = fa->next)
111 if (((ul_t)HEAP_FREE_AREA_END (fa) & (HEAP_GRANULARITY - 1))
112 || (fa->size & (HEAP_GRANULARITY - 1)))
113 __heap_check_failure (heap, fa, str, "alignment error:\n\
114 (0x%lx)->start = 0x%lx\n\
115 (0x%lx)->size = 0x%lx\n",
117 (ul_t)HEAP_FREE_AREA_START (fa),
120 if (fa->prev != prev)
121 __heap_check_failure (heap, fa, str, "prev pointer corrupted:\n\
122 (0x%lx)->next = 0x%lx\n\
123 (0x%lx)->prev = 0x%lx\n",
124 (ul_t)prev, (ul_t)prev->next,
125 (ul_t)fa, (ul_t)fa->prev);
129 ul_t start = (ul_t)HEAP_FREE_AREA_START (fa);
130 ul_t prev_end = (ul_t)HEAP_FREE_AREA_END (prev);
132 if (prev_end >= start)
133 __heap_check_failure (heap, fa, str,
134 "start %s with prev free-area end:\n\
135 (0x%lx)->prev = 0x%lx\n\
136 (0x%lx)->start = 0x%lx\n\
137 (0x%lx)->end = 0x%lx\n",
138 (prev_end == start ? "unmerged" : "overlaps"),
139 (ul_t)fa, (ul_t)prev,
141 (ul_t)prev, prev_end);