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>
37 #include <stdio-common/_itoa.h>
40 # include <libio/iolibio.h>
41 # define setvbuf(s, b, f, l) _IO_setvbuf (s, b, f, l)
44 #define TRACE_BUFFER_SIZE 512
46 static FILE *mallstream
;
47 static const char mallenv
[]= "MALLOC_TRACE";
48 static char malloc_trace_buffer
[TRACE_BUFFER_SIZE
];
50 __libc_lock_define_initialized (static, lock
);
52 /* Address to breakpoint on accesses to... */
55 /* File name and line number information, for callers that had
56 the foresight to call through a macro. */
60 /* Old hook values. */
61 static void (*tr_old_free_hook
) __P ((__ptr_t ptr
, const __ptr_t
));
62 static __ptr_t (*tr_old_malloc_hook
) __P ((__malloc_size_t size
,
64 static __ptr_t (*tr_old_realloc_hook
) __P ((__ptr_t ptr
,
68 /* This function is called when the block being alloc'd, realloc'd, or
69 freed has an address matching the variable "mallwatch". In a debugger,
70 set "mallwatch" to the address of interest, then put a breakpoint on
73 void tr_break
__P ((void));
79 static void tr_where
__P ((const __ptr_t
)) internal_function
;
87 fprintf (mallstream
, "@ %s:%d ", _mtrace_file
, _mtrace_line
);
90 else if (caller
!= NULL
)
94 if (_dl_addr (caller
, &info
))
96 char *buf
= (char *) "";
97 if (info
.dli_sname
&& info
.dli_sname
[0])
99 size_t len
= strlen (info
.dli_sname
);
100 buf
= alloca (len
+ 6 + 2 * sizeof (void *));
103 __stpcpy (_fitoa (caller
>= (const __ptr_t
) info
.dli_saddr
104 ? caller
- (const __ptr_t
) info
.dli_saddr
105 : (const __ptr_t
) info
.dli_saddr
- caller
,
106 __stpcpy (__mempcpy (buf
+ 1, info
.dli_sname
,
108 caller
>= (__ptr_t
) info
.dli_saddr
114 fprintf (mallstream
, "@ %s%s%s[%p] ",
115 info
.dli_fname
?: "", info
.dli_fname
? ":" : "",
120 fprintf (mallstream
, "@ [%p] ", caller
);
124 static void tr_freehook
__P ((__ptr_t
, const __ptr_t
));
126 tr_freehook (ptr
, caller
)
128 const __ptr_t caller
;
131 /* Be sure to print it first. */
132 fprintf (mallstream
, "- %p\n", ptr
);
133 if (ptr
== mallwatch
)
135 __libc_lock_lock (lock
);
136 __free_hook
= tr_old_free_hook
;
137 if (tr_old_free_hook
!= NULL
)
138 (*tr_old_free_hook
) (ptr
, caller
);
141 __free_hook
= tr_freehook
;
142 __libc_lock_unlock (lock
);
145 static __ptr_t tr_mallochook
__P ((__malloc_size_t
, const __ptr_t
));
147 tr_mallochook (size
, caller
)
148 __malloc_size_t size
;
149 const __ptr_t caller
;
153 __libc_lock_lock (lock
);
155 __malloc_hook
= tr_old_malloc_hook
;
156 if (tr_old_malloc_hook
!= NULL
)
157 hdr
= (__ptr_t
) (*tr_old_malloc_hook
) (size
, caller
);
159 hdr
= (__ptr_t
) malloc (size
);
160 __malloc_hook
= tr_mallochook
;
162 __libc_lock_unlock (lock
);
165 /* We could be printing a NULL here; that's OK. */
166 fprintf (mallstream
, "+ %p %#lx\n", hdr
, (unsigned long int) size
);
168 if (hdr
== mallwatch
)
174 static __ptr_t tr_reallochook
__P ((__ptr_t
, __malloc_size_t
, const __ptr_t
));
176 tr_reallochook (ptr
, size
, caller
)
178 __malloc_size_t size
;
179 const __ptr_t caller
;
183 if (ptr
== mallwatch
)
186 __libc_lock_lock (lock
);
188 __free_hook
= tr_old_free_hook
;
189 __malloc_hook
= tr_old_malloc_hook
;
190 __realloc_hook
= tr_old_realloc_hook
;
191 if (tr_old_realloc_hook
!= NULL
)
192 hdr
= (__ptr_t
) (*tr_old_realloc_hook
) (ptr
, size
, caller
);
194 hdr
= (__ptr_t
) realloc (ptr
, size
);
195 __free_hook
= tr_freehook
;
196 __malloc_hook
= tr_mallochook
;
197 __realloc_hook
= tr_reallochook
;
199 __libc_lock_unlock (lock
);
203 /* Failed realloc. */
204 fprintf (mallstream
, "! %p %#lx\n", ptr
, (unsigned long int) size
);
205 else if (ptr
== NULL
)
206 fprintf (mallstream
, "+ %p %#lx\n", hdr
, (unsigned long int) size
);
209 fprintf (mallstream
, "< %p\n", ptr
);
211 fprintf (mallstream
, "> %p %#lx\n", hdr
, (unsigned long int) size
);
214 if (hdr
== mallwatch
)
222 extern void __libc_freeres (void);
224 /* This function gets called to make sure all memory the library
225 allocates get freed and so does not irritate the user when studying
226 the mtrace output. */
228 release_libc_mem (void)
230 /* Only call the free function if we still are running in mtrace mode. */
231 if (mallstream
!= NULL
)
237 /* We enable tracing if either the environment variable MALLOC_TRACE
238 is set, or if the variable mallwatch has been patched to an address
239 that the debugging user wants us to stop on. When patching mallwatch,
240 don't forget to set a breakpoint on tr_break! */
246 static int added_atexit_handler
= 0;
250 /* Don't panic if we're called more than once. */
251 if (mallstream
!= NULL
)
255 /* When compiling the GNU libc we use the secure getenv function
256 which prevents the misuse in case of SUID or SGID enabled
258 mallfile
= __secure_getenv (mallenv
);
260 mallfile
= getenv (mallenv
);
262 if (mallfile
!= NULL
|| mallwatch
!= NULL
)
264 mallstream
= fopen (mallfile
!= NULL
? mallfile
: "/dev/null", "w");
265 if (mallstream
!= NULL
)
267 /* Be sure it doesn't malloc its buffer! */
268 setvbuf (mallstream
, malloc_trace_buffer
, _IOFBF
, TRACE_BUFFER_SIZE
);
269 fprintf (mallstream
, "= Start\n");
270 tr_old_free_hook
= __free_hook
;
271 __free_hook
= tr_freehook
;
272 tr_old_malloc_hook
= __malloc_hook
;
273 __malloc_hook
= tr_mallochook
;
274 tr_old_realloc_hook
= __realloc_hook
;
275 __realloc_hook
= tr_reallochook
;
277 if (!added_atexit_handler
)
279 added_atexit_handler
= 1;
280 atexit (release_libc_mem
);
290 if (mallstream
== NULL
)
293 fprintf (mallstream
, "= End\n");
296 __free_hook
= tr_old_free_hook
;
297 __malloc_hook
= tr_old_malloc_hook
;
298 __realloc_hook
= tr_old_realloc_hook
;