1 /* More debugging hooks for `malloc'.
2 Copyright (C) 1991, 92, 93, 94, 96, 97 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 <bits/libc-lock.h>
37 #ifndef __GNU_LIBRARY__
38 extern char *getenv ();
43 static FILE *mallstream;
44 static char mallenv[]= "MALLOC_TRACE";
45 static char mallbuf[BUFSIZ]; /* Buffer for the output. */
47 __libc_lock_define_initialized (static, lock);
49 /* Address to breakpoint on accesses to... */
52 /* File name and line number information, for callers that had
53 the foresight to call through a macro. */
57 /* Old hook values. */
58 static void (*tr_old_free_hook) __P ((__ptr_t ptr, const __ptr_t));
59 static __ptr_t (*tr_old_malloc_hook) __P ((__malloc_size_t size,
61 static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr,
65 /* This function is called when the block being alloc'd, realloc'd, or
66 freed has an address matching the variable "mallwatch". In a debugger,
67 set "mallwatch" to the address of interest, then put a breakpoint on
70 void tr_break __P ((void));
76 static void tr_where __P ((const __ptr_t));
83 fprintf (mallstream, "@ %s:%d ", _mtrace_file, _mtrace_line);
86 else if (caller != NULL)
90 if (_dl_addr (caller, &info))
92 fprintf (mallstream, "@ %s%s%s%s%s[%p]",
93 info.dli_fname ?: "", info.dli_fname ? ":" : "",
94 info.dli_sname ? "(" : "",
95 info.dli_sname ?: "", info.dli_sname ? ") " : " ",
100 fprintf (mallstream, "@ [%p] ", caller);
104 static void tr_freehook __P ((__ptr_t, const __ptr_t));
106 tr_freehook (ptr, caller)
108 const __ptr_t caller;
111 /* Be sure to print it first. */
112 fprintf (mallstream, "- %p\n", ptr);
113 if (ptr == mallwatch)
115 __libc_lock_lock (lock);
116 __free_hook = tr_old_free_hook;
117 if (tr_old_free_hook != NULL)
118 (*tr_old_free_hook) (ptr, caller);
121 __free_hook = tr_freehook;
122 __libc_lock_unlock (lock);
125 static __ptr_t tr_mallochook __P ((__malloc_size_t, const __ptr_t));
127 tr_mallochook (size, caller)
128 __malloc_size_t size;
129 const __ptr_t caller;
133 __libc_lock_lock (lock);
135 __malloc_hook = tr_old_malloc_hook;
136 if (tr_old_malloc_hook != NULL)
137 hdr = (__ptr_t) (*tr_old_malloc_hook) (size, caller);
139 hdr = (__ptr_t) malloc (size);
140 __malloc_hook = tr_mallochook;
142 __libc_lock_unlock (lock);
145 /* We could be printing a NULL here; that's OK. */
146 fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long)size);
148 if (hdr == mallwatch)
154 static __ptr_t tr_reallochook __P ((__ptr_t, __malloc_size_t, const __ptr_t));
156 tr_reallochook (ptr, size, caller)
158 __malloc_size_t size;
159 const __ptr_t caller;
163 if (ptr == mallwatch)
166 __libc_lock_lock (lock);
168 __free_hook = tr_old_free_hook;
169 __malloc_hook = tr_old_malloc_hook;
170 __realloc_hook = tr_old_realloc_hook;
171 if (tr_old_realloc_hook != NULL)
172 hdr = (__ptr_t) (*tr_old_realloc_hook) (ptr, size, caller);
174 hdr = (__ptr_t) realloc (ptr, size);
175 __free_hook = tr_freehook;
176 __malloc_hook = tr_mallochook;
177 __realloc_hook = tr_reallochook;
179 __libc_lock_unlock (lock);
183 /* Failed realloc. */
184 fprintf (mallstream, "! %p %#lx\n", ptr, (unsigned long)size);
185 else if (ptr == NULL)
186 fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long)size);
188 fprintf (mallstream, "< %p\n> %p %#lx\n", ptr, hdr, (unsigned long)size);
190 if (hdr == mallwatch)
196 /* We enable tracing if either the environment variable MALLOC_TRACE
197 is set, or if the variable mallwatch has been patched to an address
198 that the debugging user wants us to stop on. When patching mallwatch,
199 don't forget to set a breakpoint on tr_break! */
206 /* Don't panic if we're called more than once. */
207 if (mallstream != NULL)
211 /* When compiling the GNU libc we use the secure getenv function
212 which prevents the misuse in case of SUID or SGID enabled
214 mallfile = __secure_getenv (mallenv);
216 mallfile = getenv (mallenv);
218 if (mallfile != NULL || mallwatch != NULL)
220 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
221 if (mallstream != NULL)
223 /* Be sure it doesn't malloc its buffer! */
224 setbuf (mallstream, mallbuf);
225 fprintf (mallstream, "= Start\n");
226 tr_old_free_hook = __free_hook;
227 __free_hook = tr_freehook;
228 tr_old_malloc_hook = __malloc_hook;
229 __malloc_hook = tr_mallochook;
230 tr_old_realloc_hook = __realloc_hook;
231 __realloc_hook = tr_reallochook;
239 if (mallstream == NULL)
242 fprintf (mallstream, "= End\n");
245 __free_hook = tr_old_free_hook;
246 __malloc_hook = tr_old_malloc_hook;
247 __realloc_hook = tr_old_realloc_hook;