1 /* More debugging hooks for `malloc'.
2 Copyright (C) 1991, 92, 93, 94, 96, 97, 98 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>
32 #include <elf/ldsodefs.h>
37 #ifndef __GNU_LIBRARY__
38 extern char *getenv ();
43 #if defined _LIBC && defined USE_IN_LIBIO
44 # include <libio/iolibio.h>
45 # define setvbuf(s, b, f, l) _IO_setvbuf (s, b, f, l)
48 #define TRACE_BUFFER_SIZE 512
50 static FILE *mallstream;
51 static const char mallenv[]= "MALLOC_TRACE";
52 static char malloc_trace_buffer[TRACE_BUFFER_SIZE];
54 __libc_lock_define_initialized (static, lock);
56 /* Address to breakpoint on accesses to... */
59 /* File name and line number information, for callers that had
60 the foresight to call through a macro. */
64 /* Old hook values. */
65 static void (*tr_old_free_hook) __P ((__ptr_t ptr, const __ptr_t));
66 static __ptr_t (*tr_old_malloc_hook) __P ((__malloc_size_t size,
68 static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr,
72 /* This function is called when the block being alloc'd, realloc'd, or
73 freed has an address matching the variable "mallwatch". In a debugger,
74 set "mallwatch" to the address of interest, then put a breakpoint on
77 void tr_break __P ((void));
83 static void tr_where __P ((const __ptr_t)) internal_function;
91 fprintf (mallstream, "@ %s:%d ", _mtrace_file, _mtrace_line);
94 else if (caller != NULL)
98 if (_dl_addr (caller, &info))
100 char *buf = (char *) "";
101 if (info.dli_sname && info.dli_sname[0])
103 size_t len = strlen (info.dli_sname) + 22;
105 if (caller >= (const __ptr_t) info.dli_saddr)
106 snprintf (buf, len, "(%s+0x%x)", info.dli_sname,
107 caller - (const __ptr_t) info.dli_saddr);
109 snprintf (buf, len, "(%s-0x%x)", info.dli_sname,
110 (const __ptr_t) info.dli_saddr - caller);
113 fprintf (mallstream, "@ %s%s%s[%p] ",
114 info.dli_fname ?: "", info.dli_fname ? ":" : "",
119 fprintf (mallstream, "@ [%p] ", caller);
123 static void tr_freehook __P ((__ptr_t, const __ptr_t));
125 tr_freehook (ptr, caller)
127 const __ptr_t caller;
130 /* Be sure to print it first. */
131 fprintf (mallstream, "- %p\n", ptr);
132 if (ptr == mallwatch)
134 __libc_lock_lock (lock);
135 __free_hook = tr_old_free_hook;
136 if (tr_old_free_hook != NULL)
137 (*tr_old_free_hook) (ptr, caller);
140 __free_hook = tr_freehook;
141 __libc_lock_unlock (lock);
144 static __ptr_t tr_mallochook __P ((__malloc_size_t, const __ptr_t));
146 tr_mallochook (size, caller)
147 __malloc_size_t size;
148 const __ptr_t caller;
152 __libc_lock_lock (lock);
154 __malloc_hook = tr_old_malloc_hook;
155 if (tr_old_malloc_hook != NULL)
156 hdr = (__ptr_t) (*tr_old_malloc_hook) (size, caller);
158 hdr = (__ptr_t) malloc (size);
159 __malloc_hook = tr_mallochook;
161 __libc_lock_unlock (lock);
164 /* We could be printing a NULL here; that's OK. */
165 fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
167 if (hdr == mallwatch)
173 static __ptr_t tr_reallochook __P ((__ptr_t, __malloc_size_t, const __ptr_t));
175 tr_reallochook (ptr, size, caller)
177 __malloc_size_t size;
178 const __ptr_t caller;
182 if (ptr == mallwatch)
185 __libc_lock_lock (lock);
187 __free_hook = tr_old_free_hook;
188 __malloc_hook = tr_old_malloc_hook;
189 __realloc_hook = tr_old_realloc_hook;
190 if (tr_old_realloc_hook != NULL)
191 hdr = (__ptr_t) (*tr_old_realloc_hook) (ptr, size, caller);
193 hdr = (__ptr_t) realloc (ptr, size);
194 __free_hook = tr_freehook;
195 __malloc_hook = tr_mallochook;
196 __realloc_hook = tr_reallochook;
198 __libc_lock_unlock (lock);
202 /* Failed realloc. */
203 fprintf (mallstream, "! %p %#lx\n", ptr, (unsigned long int) size);
204 else if (ptr == NULL)
205 fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
208 fprintf (mallstream, "< %p\n", ptr);
210 fprintf (mallstream, "> %p %#lx\n", hdr, (unsigned long int) size);
213 if (hdr == mallwatch)
221 extern void __libc_freeres (void);
223 /* This function gets called to make sure all memory the library
224 allocates get freed and so does not irritate the user when studying
225 the mtrace output. */
227 release_libc_mem (void)
229 /* Only call the free function if we still are running in mtrace mode. */
230 if (mallstream != NULL)
236 /* We enable tracing if either the environment variable MALLOC_TRACE
237 is set, or if the variable mallwatch has been patched to an address
238 that the debugging user wants us to stop on. When patching mallwatch,
239 don't forget to set a breakpoint on tr_break! */
245 static int added_atexit_handler = 0;
249 /* Don't panic if we're called more than once. */
250 if (mallstream != NULL)
254 /* When compiling the GNU libc we use the secure getenv function
255 which prevents the misuse in case of SUID or SGID enabled
257 mallfile = __secure_getenv (mallenv);
259 mallfile = getenv (mallenv);
261 if (mallfile != NULL || mallwatch != NULL)
263 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
264 if (mallstream != NULL)
266 /* Be sure it doesn't malloc its buffer! */
267 setvbuf (mallstream, malloc_trace_buffer, _IOFBF, TRACE_BUFFER_SIZE);
268 fprintf (mallstream, "= Start\n");
269 tr_old_free_hook = __free_hook;
270 __free_hook = tr_freehook;
271 tr_old_malloc_hook = __malloc_hook;
272 __malloc_hook = tr_mallochook;
273 tr_old_realloc_hook = __realloc_hook;
274 __realloc_hook = tr_reallochook;
276 if (!added_atexit_handler)
278 added_atexit_handler = 1;
279 atexit (release_libc_mem);
289 if (mallstream == NULL)
292 fprintf (mallstream, "= End\n");
295 __free_hook = tr_old_free_hook;
296 __malloc_hook = tr_old_malloc_hook;
297 __realloc_hook = tr_old_realloc_hook;