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 PARAMS ((void));
29 static void tr_freehook PARAMS ((PTR, PTR));
30 static PTR tr_mallochook PARAMS ((PTR, size_t));
31 static PTR tr_reallochook PARAMS ((PTR, PTR, size_t));
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... */
47 /* Old hook values. */
49 static void (*old_mfree_hook) PARAMS ((PTR, PTR));
50 static PTR (*old_mmalloc_hook) PARAMS ((PTR, size_t));
51 static PTR (*old_mrealloc_hook) PARAMS ((PTR, PTR, size_t));
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
71 /* Be sure to print it first. */
72 fprintf (mallstream, "- %08lx\n", (unsigned long) ptr);
75 mdp -> mfree_hook = old_mfree_hook;
77 mdp -> mfree_hook = tr_freehook;
81 tr_mallochook (md, size)
89 mdp -> mmalloc_hook = old_mmalloc_hook;
90 hdr = (PTR) mmalloc (md, size);
91 mdp -> mmalloc_hook = tr_mallochook;
93 /* We could be printing a NULL here; that's OK. */
94 fprintf (mallstream, "+ %08lx %x\n", (unsigned long) hdr, size);
103 tr_reallochook (md, ptr, size)
111 mdp = MD_TO_MDP (md);
113 if (ptr == mallwatch)
116 mdp -> mfree_hook = old_mfree_hook;
117 mdp -> mmalloc_hook = old_mmalloc_hook;
118 mdp -> mrealloc_hook = old_mrealloc_hook;
119 hdr = (PTR) mrealloc (md, ptr, size);
120 mdp -> mfree_hook = tr_freehook;
121 mdp -> mmalloc_hook = tr_mallochook;
122 mdp -> mrealloc_hook = tr_reallochook;
124 /* Failed realloc. */
125 fprintf (mallstream, "! %08lx %x\n", (unsigned long) ptr, size);
127 fprintf (mallstream, "< %08lx\n> %08lx %x\n", (unsigned long) ptr,
128 (unsigned long) hdr, size);
130 if (hdr == mallwatch)
136 /* We enable tracing if either the environment variable MALLOC_TRACE
137 is set, or if the variable mallwatch has been patched to an address
138 that the debugging user wants us to stop on. When patching mallwatch,
139 don't forget to set a breakpoint on tr_break! */
144 #if 0 /* FIXME! This is disabled for now until we figure out how to
145 maintain a stack of hooks per heap, since we might have other
146 hooks (such as set by mmcheck/mmcheckf) active also. */
149 mallfile = getenv (mallenv);
150 if (mallfile != NULL || mallwatch != NULL)
152 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
153 if (mallstream != NULL)
155 /* Be sure it doesn't mmalloc its buffer! */
156 setbuf (mallstream, mallbuf);
157 fprintf (mallstream, "= Start\n");
158 old_mfree_hook = mdp -> mfree_hook;
159 mdp -> mfree_hook = tr_freehook;
160 old_mmalloc_hook = mdp -> mmalloc_hook;
161 mdp -> mmalloc_hook = tr_mallochook;
162 old_mrealloc_hook = mdp -> mrealloc_hook;
163 mdp -> mrealloc_hook = tr_reallochook;