Update.
[glibc.git] / malloc / mtrace.c
blob64fb62a13129d13e78a6c8dc4da24370f4a870cc
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
26 #include <malloc.h>
27 #include <mcheck.h>
28 #include <bits/libc-lock.h>
29 #endif
31 #ifdef HAVE_ELF
32 #include <elf/ldsodefs.h>
33 #endif
35 #include <stdio.h>
37 #ifndef __GNU_LIBRARY__
38 extern char *getenv ();
39 #else
40 #include <stdlib.h>
41 #endif
43 #define TRACE_BUFFER_SIZE 512
45 static FILE *mallstream;
46 static const char mallenv[]= "MALLOC_TRACE";
47 static char malloc_trace_buffer[TRACE_BUFFER_SIZE];
49 __libc_lock_define_initialized (static, lock);
51 /* Address to breakpoint on accesses to... */
52 __ptr_t mallwatch;
54 /* File name and line number information, for callers that had
55 the foresight to call through a macro. */
56 char *_mtrace_file;
57 int _mtrace_line;
59 /* Old hook values. */
60 static void (*tr_old_free_hook) __P ((__ptr_t ptr, const __ptr_t));
61 static __ptr_t (*tr_old_malloc_hook) __P ((__malloc_size_t size,
62 const __ptr_t));
63 static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr,
64 __malloc_size_t size,
65 const __ptr_t));
67 /* This function is called when the block being alloc'd, realloc'd, or
68 freed has an address matching the variable "mallwatch". In a debugger,
69 set "mallwatch" to the address of interest, then put a breakpoint on
70 tr_break. */
72 void tr_break __P ((void));
73 void
74 tr_break ()
78 static void tr_where __P ((const __ptr_t)) internal_function;
79 static void
80 internal_function
81 tr_where (caller)
82 const __ptr_t caller;
84 if (_mtrace_file)
86 fprintf (mallstream, "@ %s:%d ", _mtrace_file, _mtrace_line);
87 _mtrace_file = NULL;
89 else if (caller != NULL)
91 #ifdef HAVE_ELF
92 Dl_info info;
93 if (_dl_addr (caller, &info))
95 fprintf (mallstream, "@ %s%s%s%s%s[%p]",
96 info.dli_fname ?: "", info.dli_fname ? ":" : "",
97 info.dli_sname ? "(" : "",
98 info.dli_sname ?: "", info.dli_sname ? ") " : " ",
99 caller);
101 else
102 #endif
103 fprintf (mallstream, "@ [%p] ", caller);
107 static void tr_freehook __P ((__ptr_t, const __ptr_t));
108 static void
109 tr_freehook (ptr, caller)
110 __ptr_t ptr;
111 const __ptr_t caller;
113 tr_where (caller);
114 /* Be sure to print it first. */
115 fprintf (mallstream, "- %p\n", ptr);
116 if (ptr == mallwatch)
117 tr_break ();
118 __libc_lock_lock (lock);
119 __free_hook = tr_old_free_hook;
120 if (tr_old_free_hook != NULL)
121 (*tr_old_free_hook) (ptr, caller);
122 else
123 free (ptr);
124 __free_hook = tr_freehook;
125 __libc_lock_unlock (lock);
128 static __ptr_t tr_mallochook __P ((__malloc_size_t, const __ptr_t));
129 static __ptr_t
130 tr_mallochook (size, caller)
131 __malloc_size_t size;
132 const __ptr_t caller;
134 __ptr_t hdr;
136 __libc_lock_lock (lock);
138 __malloc_hook = tr_old_malloc_hook;
139 if (tr_old_malloc_hook != NULL)
140 hdr = (__ptr_t) (*tr_old_malloc_hook) (size, caller);
141 else
142 hdr = (__ptr_t) malloc (size);
143 __malloc_hook = tr_mallochook;
145 __libc_lock_unlock (lock);
147 tr_where (caller);
148 /* We could be printing a NULL here; that's OK. */
149 fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
151 if (hdr == mallwatch)
152 tr_break ();
154 return hdr;
157 static __ptr_t tr_reallochook __P ((__ptr_t, __malloc_size_t, const __ptr_t));
158 static __ptr_t
159 tr_reallochook (ptr, size, caller)
160 __ptr_t ptr;
161 __malloc_size_t size;
162 const __ptr_t caller;
164 __ptr_t hdr;
166 if (ptr == mallwatch)
167 tr_break ();
169 __libc_lock_lock (lock);
171 __free_hook = tr_old_free_hook;
172 __malloc_hook = tr_old_malloc_hook;
173 __realloc_hook = tr_old_realloc_hook;
174 if (tr_old_realloc_hook != NULL)
175 hdr = (__ptr_t) (*tr_old_realloc_hook) (ptr, size, caller);
176 else
177 hdr = (__ptr_t) realloc (ptr, size);
178 __free_hook = tr_freehook;
179 __malloc_hook = tr_mallochook;
180 __realloc_hook = tr_reallochook;
182 __libc_lock_unlock (lock);
184 tr_where (caller);
185 if (hdr == NULL)
186 /* Failed realloc. */
187 fprintf (mallstream, "! %p %#lx\n", ptr, (unsigned long int) size);
188 else if (ptr == NULL)
189 fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
190 else
191 fprintf (mallstream, "< %p\n> %p %#lx\n", ptr, hdr,
192 (unsigned long int) size);
194 if (hdr == mallwatch)
195 tr_break ();
197 return hdr;
201 #ifdef _LIBC
202 extern void __libc_freeres (void);
204 /* This function gets called to make sure all memory the library
205 allocates get freed and so does not irritate the user when studying
206 the mtrace output. */
207 static void
208 release_libc_mem (void)
210 /* Only call the free function if we still are running in mtrace mode. */
211 if (mallstream != NULL)
212 __libc_freeres ();
214 #endif
217 /* We enable tracing if either the environment variable MALLOC_TRACE
218 is set, or if the variable mallwatch has been patched to an address
219 that the debugging user wants us to stop on. When patching mallwatch,
220 don't forget to set a breakpoint on tr_break! */
222 void
223 mtrace ()
225 #ifdef _LIBC
226 static int added_atexit_handler = 0;
227 #endif
228 char *mallfile;
230 /* Don't panic if we're called more than once. */
231 if (mallstream != NULL)
232 return;
234 #ifdef _LIBC
235 /* When compiling the GNU libc we use the secure getenv function
236 which prevents the misuse in case of SUID or SGID enabled
237 programs. */
238 mallfile = __secure_getenv (mallenv);
239 #else
240 mallfile = getenv (mallenv);
241 #endif
242 if (mallfile != NULL || mallwatch != NULL)
244 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
245 if (mallstream != NULL)
247 /* Be sure it doesn't malloc its buffer! */
248 setvbuf (mallstream, malloc_trace_buffer, _IOFBF, TRACE_BUFFER_SIZE);
249 fprintf (mallstream, "= Start\n");
250 tr_old_free_hook = __free_hook;
251 __free_hook = tr_freehook;
252 tr_old_malloc_hook = __malloc_hook;
253 __malloc_hook = tr_mallochook;
254 tr_old_realloc_hook = __realloc_hook;
255 __realloc_hook = tr_reallochook;
256 #ifdef _LIBC
257 if (!added_atexit_handler)
259 added_atexit_handler = 1;
260 atexit (release_libc_mem);
262 #endif
267 void
268 muntrace ()
270 if (mallstream == NULL)
271 return;
273 fprintf (mallstream, "= End\n");
274 fclose (mallstream);
275 mallstream = NULL;
276 __free_hook = tr_old_free_hook;
277 __malloc_hook = tr_old_malloc_hook;
278 __realloc_hook = tr_old_realloc_hook;