1 /* More debugging hooks for `mmalloc'.
2 Copyright 1991, 1992 Free Software Foundation
4 Written April 2, 1991 by John Gilmore of Cygnus Support
5 Based on mcheck.c by Mike Haertel.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #ifndef __GNU_LIBRARY__
26 extern char *getenv ();
29 static FILE *mallstream;
31 #if 0 /* FIXME: Disabled for now. */
32 static char mallenv[] = "MALLOC_TRACE";
33 static char mallbuf[BUFSIZ]; /* Buffer for the output. */
36 /* Address to breakpoint on accesses to... */
39 /* Old hook values. */
41 static void (*old_mfree_hook) PARAMS ((PTR, PTR));
42 static PTR (*old_mmalloc_hook) PARAMS ((PTR, size_t));
43 static PTR (*old_mrealloc_hook) PARAMS ((PTR, PTR, size_t));
45 /* This function is called when the block being alloc'd, realloc'd, or
46 freed has an address matching the variable "mallwatch". In a debugger,
47 set "mallwatch" to the address of interest, then put a breakpoint on
63 /* Be sure to print it first. */
64 (void) fprintf (mallstream, "- %08x\n", (unsigned int) ptr);
67 mdp -> mfree_hook = old_mfree_hook;
69 mdp -> mfree_hook = tr_freehook;
73 tr_mallochook (md, size)
81 mdp -> mmalloc_hook = old_mmalloc_hook;
82 hdr = (PTR) mmalloc (md, size);
83 mdp -> mmalloc_hook = tr_mallochook;
85 /* We could be printing a NULL here; that's OK. */
86 (void) fprintf (mallstream, "+ %08x %x\n", (unsigned int) hdr, size);
95 tr_reallochook (md, ptr, size)
103 mdp = MD_TO_MDP (md);
105 if (ptr == mallwatch)
108 mdp -> mfree_hook = old_mfree_hook;
109 mdp -> mmalloc_hook = old_mmalloc_hook;
110 mdp -> mrealloc_hook = old_mrealloc_hook;
111 hdr = (PTR) mrealloc (md, ptr, size);
112 mdp -> mfree_hook = tr_freehook;
113 mdp -> mmalloc_hook = tr_mallochook;
114 mdp -> mrealloc_hook = tr_reallochook;
116 /* Failed realloc. */
117 (void) fprintf (mallstream, "! %08x %x\n", (unsigned int) ptr, size);
119 (void) fprintf (mallstream, "< %08x\n> %08x %x\n", (unsigned int) ptr,
120 (unsigned int) hdr, size);
122 if (hdr == mallwatch)
128 /* We enable tracing if either the environment variable MALLOC_TRACE
129 is set, or if the variable mallwatch has been patched to an address
130 that the debugging user wants us to stop on. When patching mallwatch,
131 don't forget to set a breakpoint on tr_break! */
136 #if 0 /* FIXME! This is disabled for now until we figure out how to
137 maintain a stack of hooks per heap, since we might have other
138 hooks (such as set by mmcheck) active also. */
141 mallfile = getenv (mallenv);
142 if (mallfile != NULL || mallwatch != NULL)
144 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
145 if (mallstream != NULL)
147 /* Be sure it doesn't mmalloc its buffer! */
148 setbuf (mallstream, mallbuf);
149 (void) fprintf (mallstream, "= Start\n");
150 old_mfree_hook = mdp -> mfree_hook;
151 mdp -> mfree_hook = tr_freehook;
152 old_mmalloc_hook = mdp -> mmalloc_hook;
153 mdp -> mmalloc_hook = tr_mallochook;
154 old_mrealloc_hook = mdp -> mrealloc_hook;
155 mdp -> mrealloc_hook = tr_reallochook;