summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc/malloc_debug.c
blob: c19059b434e98d0127aec9c8556a3fbdad1038af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * libc/stdlib/malloc/malloc_debug.c -- malloc debugging support
 *
 *  Copyright (C) 2002  NEC Corporation
 *  Copyright (C) 2002  Miles Bader <miles@gnu.org>
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License.  See the file COPYING.LIB in the main
 * directory of this archive for more details.
 *
 * Written by Miles Bader <miles@gnu.org>
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>

libc_hidden_proto(atoi)
/* libc_hidden_proto(vfprintf) */
libc_hidden_proto(putc)
/* libc_hidden_proto(getenv) */

#include "malloc.h"
#include "heap.h"

int __malloc_debug = 0, __malloc_check = 0;

#ifdef MALLOC_MMB_DEBUGGING
int __malloc_mmb_debug = 0;
#endif

/* Debugging output is indented this may levels.  */
int __malloc_debug_cur_indent = 0;


/* Print FMT and args indented at the current debug print level, followed
   by a newline, and change the level by INDENT.  */
void
__malloc_debug_printf (int indent, const char *fmt, ...)
{
  unsigned spaces = __malloc_debug_cur_indent * MALLOC_DEBUG_INDENT_SIZE;
  va_list val;

  while (spaces > 0)
    {
      putc (' ', stderr);
      spaces--;
    }

  va_start (val, fmt);
  vfprintf (stderr, fmt, val);
  va_end (val);

  putc ('\n', stderr);

  __malloc_debug_indent (indent);
}

void
__malloc_debug_init (void)
{
  char *ev = getenv ("MALLOC_DEBUG");
  if (ev)
    {
      int val = atoi (ev);

      if (val & 1)
	__malloc_check = 1;

      if (val & 2)
	__malloc_debug = 1;

#ifdef MALLOC_MMB_DEBUGGING
      if (val & 4)
	__malloc_mmb_debug = 1;
#endif

#ifdef HEAP_DEBUGGING
      if (val & 8)
	__heap_debug = 1;
#endif

      if (val)
	__malloc_debug_printf
	  (0, "malloc_debug: initialized to %d (check = %d, dump = %d, dump_mmb = %d, dump_heap = %d)",
	   val,
	   !!(val & 1), !!(val & 2),
	   !!(val & 4), !!(val & 8));
    }
}