1 /* More debugging hooks for `mmalloc'.
2 Copyright 1991, 1992, 1994 Free Software Foundation
4 Written April 2, 1991 by John Gilmore of Cygnus Support
5 Based on mcheck.c by Mike Haertel.
6 Modified Mar 1992 by Fred Fish. (fnf@cygnus.com)
8 This file is part of the GNU C Library.
10 The GNU C Library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 The GNU C Library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with the GNU C Library; see the file COPYING.LIB. If
22 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
26 #include "mmprivate.h"
28 static void tr_break (void);
29 static void tr_freehook (void*md, void*ptr);
30 static void* tr_mallochook (void* md, size_t size);
31 static void* tr_reallochook (void* md, void* ptr, size_t size);
33 #ifndef __GNU_LIBRARY__
34 extern char *getenv ();
37 static FILE *mallstream;
39 #if 0 /* FIXME: Disabled for now. */
40 static char mallenv[] = "MALLOC_TRACE";
41 static char mallbuf[BUFSIZ]; /* Buffer for the output. */
44 /* Address to breakpoint on accesses to... */
45 static void* mallwatch;
47 /* Old hook values. */
49 static void (*old_mfree_hook) (void* md, void* ptr);
50 static void* (*old_mmalloc_hook) (void* md, size_t size);
51 static void* (*old_mrealloc_hook) (void* md, void* ptr, size_t size);
53 /* This function is called when the block being alloc'd, realloc'd, or
54 freed has an address matching the variable "mallwatch". In a debugger,
55 set "mallwatch" to the address of interest, then put a breakpoint on
64 tr_freehook (void *md, void *ptr)
69 /* Be sure to print it first. */
70 fprintf (mallstream, "- %08lx\n", (unsigned long) ptr);
73 mdp -> mfree_hook = old_mfree_hook;
75 mdp -> mfree_hook = tr_freehook;
79 tr_mallochook (void* md, size_t size)
85 mdp -> mmalloc_hook = old_mmalloc_hook;
86 hdr = (void*) mmalloc (md, size);
87 mdp -> mmalloc_hook = tr_mallochook;
89 /* We could be printing a NULL here; that's OK. */
90 fprintf (mallstream, "+ %p 0x%lx\n", hdr, (unsigned long)size);
99 tr_reallochook (void *md, void *ptr, size_t size)
104 mdp = MD_TO_MDP (md);
106 if (ptr == mallwatch)
109 mdp -> mfree_hook = old_mfree_hook;
110 mdp -> mmalloc_hook = old_mmalloc_hook;
111 mdp -> mrealloc_hook = old_mrealloc_hook;
112 hdr = (void*) mrealloc (md, ptr, size);
113 mdp -> mfree_hook = tr_freehook;
114 mdp -> mmalloc_hook = tr_mallochook;
115 mdp -> mrealloc_hook = tr_reallochook;
117 /* Failed realloc. */
118 fprintf (mallstream, "! %p 0x%lx\n", ptr, (unsigned long) size);
120 fprintf (mallstream, "< %p\n> %p 0x%lx\n", ptr,
121 hdr, (unsigned long) size);
123 if (hdr == mallwatch)
129 /* We enable tracing if either the environment variable MALLOC_TRACE
130 is set, or if the variable mallwatch has been patched to an address
131 that the debugging user wants us to stop on. When patching mallwatch,
132 don't forget to set a breakpoint on tr_break! */
137 #if 0 /* FIXME! This is disabled for now until we figure out how to
138 maintain a stack of hooks per heap, since we might have other
139 hooks (such as set by mmcheck/mmcheckf) active also. */
142 mallfile = getenv (mallenv);
143 if (mallfile != NULL || mallwatch != NULL)
145 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
146 if (mallstream != NULL)
148 /* Be sure it doesn't mmalloc its buffer! */
149 setbuf (mallstream, mallbuf);
150 fprintf (mallstream, "= Start\n");
151 old_mfree_hook = mdp -> mfree_hook;
152 mdp -> mfree_hook = tr_freehook;
153 old_mmalloc_hook = mdp -> mmalloc_hook;
154 mdp -> mmalloc_hook = tr_mallochook;
155 old_mrealloc_hook = mdp -> mrealloc_hook;
156 mdp -> mrealloc_hook = tr_reallochook;