1 /* More debugging hooks for `malloc'.
2 Copyright (C) 1991, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
3 Written April 2, 1991 by John Gilmore of Cygnus Support.
4 Based on mcheck.c by Mike Haertel.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 The author may be reached (Email) at the address mike@ai.mit.edu,
22 or (US mail) as Mike Haertel c/o Free Software Foundation. */
24 #ifndef _MALLOC_INTERNAL
25 #define _MALLOC_INTERNAL
28 #include <libc-lock.h>
33 #ifndef __GNU_LIBRARY__
34 extern char *getenv ();
39 static FILE *mallstream
;
40 static char mallenv
[]= "MALLOC_TRACE";
41 static char mallbuf
[BUFSIZ
]; /* Buffer for the output. */
43 __libc_lock_define_initialized (static, lock
);
45 /* Address to breakpoint on accesses to... */
48 /* File name and line number information, for callers that had
49 the foresight to call through a macro. */
53 /* Old hook values. */
54 static void (*tr_old_free_hook
) __P ((__ptr_t ptr
));
55 static __ptr_t (*tr_old_malloc_hook
) __P ((__malloc_size_t size
));
56 static __ptr_t (*tr_old_realloc_hook
) __P ((__ptr_t ptr
, __malloc_size_t size
));
58 /* This function is called when the block being alloc'd, realloc'd, or
59 freed has an address matching the variable "mallwatch". In a debugger,
60 set "mallwatch" to the address of interest, then put a breakpoint on
63 void tr_break
__P ((void));
69 static void tr_where
__P ((void));
75 fprintf (mallstream
, "@ %s:%d ", _mtrace_file
, _mtrace_line
);
80 static void tr_freehook
__P ((__ptr_t
));
86 fprintf (mallstream
, "- %p\n", ptr
); /* Be sure to print it first. */
89 __libc_lock_lock (lock
);
90 __free_hook
= tr_old_free_hook
;
92 __free_hook
= tr_freehook
;
93 __libc_lock_unlock (lock
);
96 static __ptr_t tr_mallochook
__P ((__malloc_size_t
));
103 __libc_lock_lock (lock
);
105 __malloc_hook
= tr_old_malloc_hook
;
106 hdr
= (__ptr_t
) malloc (size
);
107 __malloc_hook
= tr_mallochook
;
109 __libc_lock_unlock (lock
);
112 /* We could be printing a NULL here; that's OK. */
113 fprintf (mallstream
, "+ %p %lx\n", hdr
, (unsigned long)size
);
115 if (hdr
== mallwatch
)
121 static __ptr_t tr_reallochook
__P ((__ptr_t
, __malloc_size_t
));
123 tr_reallochook (ptr
, size
)
125 __malloc_size_t size
;
129 if (ptr
== mallwatch
)
132 __libc_lock_lock (lock
);
134 __free_hook
= tr_old_free_hook
;
135 __malloc_hook
= tr_old_malloc_hook
;
136 __realloc_hook
= tr_old_realloc_hook
;
137 hdr
= (__ptr_t
) realloc (ptr
, size
);
138 __free_hook
= tr_freehook
;
139 __malloc_hook
= tr_mallochook
;
140 __realloc_hook
= tr_reallochook
;
142 __libc_lock_unlock (lock
);
146 /* Failed realloc. */
147 fprintf (mallstream
, "! %p %lx\n", ptr
, (unsigned long)size
);
148 else if (ptr
== NULL
)
149 fprintf (mallstream
, "+ %p %lx\n", hdr
, (unsigned long)size
);
151 fprintf (mallstream
, "< %p\n> %p %lx\n", ptr
, hdr
, (unsigned long)size
);
153 if (hdr
== mallwatch
)
159 /* We enable tracing if either the environment variable MALLOC_TRACE
160 is set, or if the variable mallwatch has been patched to an address
161 that the debugging user wants us to stop on. When patching mallwatch,
162 don't forget to set a breakpoint on tr_break! */
169 /* Don't panic if we're called more than once. */
170 if (mallstream
!= NULL
)
174 /* When compiling the GNU libc we use the secure getenv function
175 which prevents the misuse in case of SUID or SGID enabled
177 mallfile
= __secure_getenv (mallenv
);
179 mallfile
= getenv (mallenv
);
181 if (mallfile
!= NULL
|| mallwatch
!= NULL
)
183 mallstream
= fopen (mallfile
!= NULL
? mallfile
: "/dev/null", "w");
184 if (mallstream
!= NULL
)
186 /* Be sure it doesn't malloc its buffer! */
187 setbuf (mallstream
, mallbuf
);
188 fprintf (mallstream
, "= Start\n");
189 tr_old_free_hook
= __free_hook
;
190 __free_hook
= tr_freehook
;
191 tr_old_malloc_hook
= __malloc_hook
;
192 __malloc_hook
= tr_mallochook
;
193 tr_old_realloc_hook
= __realloc_hook
;
194 __realloc_hook
= tr_reallochook
;
202 if (mallstream
== NULL
)
205 fprintf (mallstream
, "= End\n");
208 __free_hook
= tr_old_free_hook
;
209 __malloc_hook
= tr_old_malloc_hook
;
210 __realloc_hook
= tr_old_realloc_hook
;