From 4d952dfe7756644a4e7f92081906fd7d4194209c Mon Sep 17 00:00:00 2001 From: Miles Bader Date: Thu, 21 Nov 2002 06:06:22 +0000 Subject: Improve malloc debugging support. --- libc/stdlib/malloc/malloc_debug.c | 85 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 libc/stdlib/malloc/malloc_debug.c (limited to 'libc/stdlib/malloc/malloc_debug.c') diff --git a/libc/stdlib/malloc/malloc_debug.c b/libc/stdlib/malloc/malloc_debug.c new file mode 100644 index 000000000..20a37f2f0 --- /dev/null +++ b/libc/stdlib/malloc/malloc_debug.c @@ -0,0 +1,85 @@ +/* + * libc/stdlib/malloc/malloc_debug.c -- malloc debugging support + * + * Copyright (C) 2002 NEC Corporation + * Copyright (C) 2002 Miles Bader + * + * 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 + */ + +#include +#include +#include +#include + +#include "malloc.h" +#include "heap.h" + +#ifdef MALLOC_DEBUGGING +int __malloc_debug = 0, __malloc_check = 0; +#endif + +#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, ...) +{ + int i; + va_list val; + + for (i = 0; i < __malloc_debug_cur_indent; i++) + fputs (" ", stderr); + + 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; + +#ifdef MALLOC_DEBUGGING + if (val & 2) + __malloc_debug = 1; +#endif +#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)); + } +} -- cgit v1.2.3