]>
Commit | Line | Data |
---|---|---|
6d52618b | 1 | /* More debugging hooks for `malloc'. |
c4563d2d | 2 | Copyright (C) 1991,92,93,94,96,97,98,99 Free Software Foundation, Inc. |
6d52618b UD |
3 | Written April 2, 1991 by John Gilmore of Cygnus Support. |
4 | Based on mcheck.c by Mike Haertel. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
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. */ | |
23 | ||
24 | #ifndef _MALLOC_INTERNAL | |
25 | #define _MALLOC_INTERNAL | |
26 | #include <malloc.h> | |
27 | #include <mcheck.h> | |
5107cf1d | 28 | #include <bits/libc-lock.h> |
6d52618b UD |
29 | #endif |
30 | ||
ce85d65b | 31 | #include <ldsodefs.h> |
a2b08ee5 | 32 | |
6d52618b | 33 | #include <stdio.h> |
e03c3361 | 34 | #include <string.h> |
6d52618b | 35 | #include <stdlib.h> |
6d52618b | 36 | |
d5cabaa4 UD |
37 | #include <stdio-common/_itoa.h> |
38 | ||
39 | #ifdef USE_IN_LIBIO | |
08a0d60a | 40 | # include <libio/iolibio.h> |
50304ef0 UD |
41 | # define setvbuf(s, b, f, l) _IO_setvbuf (s, b, f, l) |
42 | #endif | |
43 | ||
e7993f20 UD |
44 | #define TRACE_BUFFER_SIZE 512 |
45 | ||
6d52618b | 46 | static FILE *mallstream; |
bd355af0 | 47 | static const char mallenv[]= "MALLOC_TRACE"; |
e7993f20 | 48 | static char malloc_trace_buffer[TRACE_BUFFER_SIZE]; |
6d52618b UD |
49 | |
50 | __libc_lock_define_initialized (static, lock); | |
51 | ||
52 | /* Address to breakpoint on accesses to... */ | |
53 | __ptr_t mallwatch; | |
54 | ||
55 | /* File name and line number information, for callers that had | |
56 | the foresight to call through a macro. */ | |
57 | char *_mtrace_file; | |
58 | int _mtrace_line; | |
59 | ||
60 | /* Old hook values. */ | |
a2b08ee5 UD |
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, | |
63 | const __ptr_t)); | |
64 | static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr, | |
65 | __malloc_size_t size, | |
66 | const __ptr_t)); | |
6d52618b UD |
67 | |
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 | |
71 | tr_break. */ | |
72 | ||
73 | void tr_break __P ((void)); | |
74 | void | |
75 | tr_break () | |
76 | { | |
77 | } | |
78 | ||
dfd2257a | 79 | static void tr_where __P ((const __ptr_t)) internal_function; |
6d52618b | 80 | static void |
dfd2257a | 81 | internal_function |
a2b08ee5 UD |
82 | tr_where (caller) |
83 | const __ptr_t caller; | |
6d52618b UD |
84 | { |
85 | if (_mtrace_file) | |
86 | { | |
87 | fprintf (mallstream, "@ %s:%d ", _mtrace_file, _mtrace_line); | |
88 | _mtrace_file = NULL; | |
89 | } | |
a2b08ee5 UD |
90 | else if (caller != NULL) |
91 | { | |
92 | #ifdef HAVE_ELF | |
93 | Dl_info info; | |
94 | if (_dl_addr (caller, &info)) | |
95 | { | |
604510f7 UD |
96 | char *buf = (char *) ""; |
97 | if (info.dli_sname && info.dli_sname[0]) | |
98 | { | |
d5cabaa4 UD |
99 | size_t len = strlen (info.dli_sname); |
100 | buf = alloca (len + 6 + 2 * sizeof (void *)); | |
101 | ||
102 | buf[0] = '('; | |
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, | |
107 | len), | |
108 | caller >= (__ptr_t) info.dli_saddr | |
109 | ? "+0x" : "-0x"), | |
110 | 16, 0), | |
111 | ")"); | |
604510f7 UD |
112 | } |
113 | ||
114 | fprintf (mallstream, "@ %s%s%s[%p] ", | |
a2b08ee5 | 115 | info.dli_fname ?: "", info.dli_fname ? ":" : "", |
604510f7 | 116 | buf, caller); |
a2b08ee5 UD |
117 | } |
118 | else | |
119 | #endif | |
120 | fprintf (mallstream, "@ [%p] ", caller); | |
121 | } | |
6d52618b UD |
122 | } |
123 | ||
a2b08ee5 | 124 | static void tr_freehook __P ((__ptr_t, const __ptr_t)); |
6d52618b | 125 | static void |
a2b08ee5 | 126 | tr_freehook (ptr, caller) |
6d52618b | 127 | __ptr_t ptr; |
a2b08ee5 | 128 | const __ptr_t caller; |
6d52618b | 129 | { |
92e4472f | 130 | __libc_lock_lock (lock); |
a2b08ee5 UD |
131 | tr_where (caller); |
132 | /* Be sure to print it first. */ | |
133 | fprintf (mallstream, "- %p\n", ptr); | |
92e4472f | 134 | __libc_lock_unlock (lock); |
6d52618b UD |
135 | if (ptr == mallwatch) |
136 | tr_break (); | |
137 | __libc_lock_lock (lock); | |
138 | __free_hook = tr_old_free_hook; | |
a2b08ee5 UD |
139 | if (tr_old_free_hook != NULL) |
140 | (*tr_old_free_hook) (ptr, caller); | |
141 | else | |
142 | free (ptr); | |
6d52618b UD |
143 | __free_hook = tr_freehook; |
144 | __libc_lock_unlock (lock); | |
145 | } | |
146 | ||
a2b08ee5 | 147 | static __ptr_t tr_mallochook __P ((__malloc_size_t, const __ptr_t)); |
6d52618b | 148 | static __ptr_t |
a2b08ee5 | 149 | tr_mallochook (size, caller) |
6d52618b | 150 | __malloc_size_t size; |
a2b08ee5 | 151 | const __ptr_t caller; |
6d52618b UD |
152 | { |
153 | __ptr_t hdr; | |
154 | ||
155 | __libc_lock_lock (lock); | |
156 | ||
157 | __malloc_hook = tr_old_malloc_hook; | |
a2b08ee5 UD |
158 | if (tr_old_malloc_hook != NULL) |
159 | hdr = (__ptr_t) (*tr_old_malloc_hook) (size, caller); | |
160 | else | |
161 | hdr = (__ptr_t) malloc (size); | |
6d52618b UD |
162 | __malloc_hook = tr_mallochook; |
163 | ||
a2b08ee5 | 164 | tr_where (caller); |
6d52618b | 165 | /* We could be printing a NULL here; that's OK. */ |
c0fb8a56 | 166 | fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size); |
6d52618b | 167 | |
92e4472f UD |
168 | __libc_lock_unlock (lock); |
169 | ||
6d52618b UD |
170 | if (hdr == mallwatch) |
171 | tr_break (); | |
172 | ||
173 | return hdr; | |
174 | } | |
175 | ||
a2b08ee5 | 176 | static __ptr_t tr_reallochook __P ((__ptr_t, __malloc_size_t, const __ptr_t)); |
6d52618b | 177 | static __ptr_t |
a2b08ee5 | 178 | tr_reallochook (ptr, size, caller) |
6d52618b UD |
179 | __ptr_t ptr; |
180 | __malloc_size_t size; | |
a2b08ee5 | 181 | const __ptr_t caller; |
6d52618b UD |
182 | { |
183 | __ptr_t hdr; | |
184 | ||
185 | if (ptr == mallwatch) | |
186 | tr_break (); | |
187 | ||
188 | __libc_lock_lock (lock); | |
189 | ||
190 | __free_hook = tr_old_free_hook; | |
191 | __malloc_hook = tr_old_malloc_hook; | |
192 | __realloc_hook = tr_old_realloc_hook; | |
a2b08ee5 UD |
193 | if (tr_old_realloc_hook != NULL) |
194 | hdr = (__ptr_t) (*tr_old_realloc_hook) (ptr, size, caller); | |
195 | else | |
196 | hdr = (__ptr_t) realloc (ptr, size); | |
6d52618b UD |
197 | __free_hook = tr_freehook; |
198 | __malloc_hook = tr_mallochook; | |
199 | __realloc_hook = tr_reallochook; | |
200 | ||
a2b08ee5 | 201 | tr_where (caller); |
6d52618b UD |
202 | if (hdr == NULL) |
203 | /* Failed realloc. */ | |
c0fb8a56 | 204 | fprintf (mallstream, "! %p %#lx\n", ptr, (unsigned long int) size); |
6d52618b | 205 | else if (ptr == NULL) |
c0fb8a56 | 206 | fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size); |
6d52618b | 207 | else |
8261bc4b UD |
208 | { |
209 | fprintf (mallstream, "< %p\n", ptr); | |
210 | tr_where (caller); | |
211 | fprintf (mallstream, "> %p %#lx\n", hdr, (unsigned long int) size); | |
212 | } | |
6d52618b | 213 | |
92e4472f UD |
214 | __libc_lock_unlock (lock); |
215 | ||
6d52618b UD |
216 | if (hdr == mallwatch) |
217 | tr_break (); | |
218 | ||
219 | return hdr; | |
220 | } | |
221 | ||
a5a0310d UD |
222 | |
223 | #ifdef _LIBC | |
224 | extern void __libc_freeres (void); | |
225 | ||
226 | /* This function gets called to make sure all memory the library | |
227 | allocates get freed and so does not irritate the user when studying | |
228 | the mtrace output. */ | |
229 | static void | |
230 | release_libc_mem (void) | |
231 | { | |
232 | /* Only call the free function if we still are running in mtrace mode. */ | |
233 | if (mallstream != NULL) | |
234 | __libc_freeres (); | |
235 | } | |
236 | #endif | |
237 | ||
238 | ||
6d52618b UD |
239 | /* We enable tracing if either the environment variable MALLOC_TRACE |
240 | is set, or if the variable mallwatch has been patched to an address | |
241 | that the debugging user wants us to stop on. When patching mallwatch, | |
242 | don't forget to set a breakpoint on tr_break! */ | |
243 | ||
244 | void | |
245 | mtrace () | |
246 | { | |
a5a0310d | 247 | #ifdef _LIBC |
c4563d2d | 248 | static int added_atexit_handler; |
a5a0310d | 249 | #endif |
6d52618b UD |
250 | char *mallfile; |
251 | ||
252 | /* Don't panic if we're called more than once. */ | |
253 | if (mallstream != NULL) | |
254 | return; | |
255 | ||
256 | #ifdef _LIBC | |
257 | /* When compiling the GNU libc we use the secure getenv function | |
258 | which prevents the misuse in case of SUID or SGID enabled | |
259 | programs. */ | |
260 | mallfile = __secure_getenv (mallenv); | |
261 | #else | |
262 | mallfile = getenv (mallenv); | |
263 | #endif | |
264 | if (mallfile != NULL || mallwatch != NULL) | |
265 | { | |
266 | mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w"); | |
267 | if (mallstream != NULL) | |
268 | { | |
269 | /* Be sure it doesn't malloc its buffer! */ | |
479e9b3f | 270 | setvbuf (mallstream, malloc_trace_buffer, _IOFBF, TRACE_BUFFER_SIZE); |
6d52618b UD |
271 | fprintf (mallstream, "= Start\n"); |
272 | tr_old_free_hook = __free_hook; | |
273 | __free_hook = tr_freehook; | |
274 | tr_old_malloc_hook = __malloc_hook; | |
275 | __malloc_hook = tr_mallochook; | |
276 | tr_old_realloc_hook = __realloc_hook; | |
277 | __realloc_hook = tr_reallochook; | |
a5a0310d UD |
278 | #ifdef _LIBC |
279 | if (!added_atexit_handler) | |
280 | { | |
281 | added_atexit_handler = 1; | |
282 | atexit (release_libc_mem); | |
283 | } | |
284 | #endif | |
6d52618b UD |
285 | } |
286 | } | |
287 | } | |
288 | ||
289 | void | |
290 | muntrace () | |
291 | { | |
292 | if (mallstream == NULL) | |
293 | return; | |
294 | ||
295 | fprintf (mallstream, "= End\n"); | |
296 | fclose (mallstream); | |
297 | mallstream = NULL; | |
298 | __free_hook = tr_old_free_hook; | |
299 | __malloc_hook = tr_old_malloc_hook; | |
300 | __realloc_hook = tr_old_realloc_hook; | |
301 | } |