1 /*
2 * libc/stdlib/malloc/malloc_debug.c -- malloc debugging support
3 *
4 * Copyright (C) 2002 NEC Corporation
5 * Copyright (C) 2002 Miles Bader <miles@gnu.org>
6 *
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.
10 *
11 * Written by Miles Bader <miles@gnu.org>
12 */
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <stdarg.h>
18
19
20 #include "malloc.h"
21 #include "heap.h"
22
23 int __malloc_debug = 0, __malloc_check = 0;
24
25 #ifdef MALLOC_MMB_DEBUGGING
26 int __malloc_mmb_debug = 0;
27 #endif
28
29 /* Debugging output is indented this may levels. */
30 int __malloc_debug_cur_indent = 0;
31
32
33 /* Print FMT and args indented at the current debug print level, followed
34 by a newline, and change the level by INDENT. */
35 void
__malloc_debug_printf(int indent,const char * fmt,...)36 __malloc_debug_printf (int indent, const char *fmt, ...)
37 {
38 unsigned spaces = __malloc_debug_cur_indent * MALLOC_DEBUG_INDENT_SIZE;
39 va_list val;
40
41 while (spaces > 0)
42 {
43 putc (' ', stderr);
44 spaces--;
45 }
46
47 va_start (val, fmt);
48 vfprintf (stderr, fmt, val);
49 va_end (val);
50
51 putc ('\n', stderr);
52
53 __malloc_debug_indent (indent);
54 }
55
56 void
__malloc_debug_init(void)57 __malloc_debug_init (void)
58 {
59 char *ev = getenv ("MALLOC_DEBUG");
60 if (ev)
61 {
62 int val = atoi (ev);
63
64 if (val & 1)
65 __malloc_check = 1;
66
67 if (val & 2)
68 __malloc_debug = 1;
69
70 #ifdef MALLOC_MMB_DEBUGGING
71 if (val & 4)
72 __malloc_mmb_debug = 1;
73 #endif
74
75 #ifdef HEAP_DEBUGGING
76 if (val & 8)
77 __heap_debug = 1;
78 #endif
79
80 if (val)
81 __malloc_debug_printf
82 (0, "malloc_debug: initialized to %d (check = %d, dump = %d, dump_mmb = %d, dump_heap = %d)",
83 val,
84 !!(val & 1), !!(val & 2),
85 !!(val & 4), !!(val & 8));
86 }
87 }
88