1*6a54128fSAndroid Build Coastguard Worker /* More debugging hooks for `malloc'.
2*6a54128fSAndroid Build Coastguard Worker Copyright (C) 1991, 1992 Free Software Foundation, Inc.
3*6a54128fSAndroid Build Coastguard Worker Written April 2, 1991 by John Gilmore of Cygnus Support.
4*6a54128fSAndroid Build Coastguard Worker Based on mcheck.c by Mike Haertel.
5*6a54128fSAndroid Build Coastguard Worker
6*6a54128fSAndroid Build Coastguard Worker This library is free software; you can redistribute it and/or
7*6a54128fSAndroid Build Coastguard Worker modify it under the terms of the GNU Library General Public License as
8*6a54128fSAndroid Build Coastguard Worker published by the Free Software Foundation; either version 2 of the
9*6a54128fSAndroid Build Coastguard Worker License, or (at your option) any later version.
10*6a54128fSAndroid Build Coastguard Worker
11*6a54128fSAndroid Build Coastguard Worker This library is distributed in the hope that it will be useful,
12*6a54128fSAndroid Build Coastguard Worker but WITHOUT ANY WARRANTY; without even the implied warranty of
13*6a54128fSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*6a54128fSAndroid Build Coastguard Worker Library General Public License for more details.
15*6a54128fSAndroid Build Coastguard Worker
16*6a54128fSAndroid Build Coastguard Worker You should have received a copy of the GNU Library General Public
17*6a54128fSAndroid Build Coastguard Worker License along with this library; see the file COPYING.LIB. If
18*6a54128fSAndroid Build Coastguard Worker not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19*6a54128fSAndroid Build Coastguard Worker Cambridge, MA 02139, USA.
20*6a54128fSAndroid Build Coastguard Worker
21*6a54128fSAndroid Build Coastguard Worker The author may be reached (Email) at the address [email protected],
22*6a54128fSAndroid Build Coastguard Worker or (US mail) as Mike Haertel c/o Free Software Foundation. */
23*6a54128fSAndroid Build Coastguard Worker
24*6a54128fSAndroid Build Coastguard Worker #include "config.h"
25*6a54128fSAndroid Build Coastguard Worker
26*6a54128fSAndroid Build Coastguard Worker #ifndef _MALLOC_INTERNAL
27*6a54128fSAndroid Build Coastguard Worker #define _MALLOC_INTERNAL
28*6a54128fSAndroid Build Coastguard Worker #include "./mtrace.h"
29*6a54128fSAndroid Build Coastguard Worker #endif
30*6a54128fSAndroid Build Coastguard Worker
31*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
32*6a54128fSAndroid Build Coastguard Worker
33*6a54128fSAndroid Build Coastguard Worker #ifndef __GNU_LIBRARY__
34*6a54128fSAndroid Build Coastguard Worker extern char *getenv ();
35*6a54128fSAndroid Build Coastguard Worker #else
36*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
37*6a54128fSAndroid Build Coastguard Worker #endif
38*6a54128fSAndroid Build Coastguard Worker
39*6a54128fSAndroid Build Coastguard Worker static FILE *mallstream;
40*6a54128fSAndroid Build Coastguard Worker static char mallenv[]= "MALLOC_TRACE";
41*6a54128fSAndroid Build Coastguard Worker static char mallbuf[BUFSIZ]; /* Buffer for the output. */
42*6a54128fSAndroid Build Coastguard Worker
43*6a54128fSAndroid Build Coastguard Worker /* Address to breakpoint on accesses to... */
44*6a54128fSAndroid Build Coastguard Worker __ptr_t mallwatch;
45*6a54128fSAndroid Build Coastguard Worker
46*6a54128fSAndroid Build Coastguard Worker /* Old hook values. */
47*6a54128fSAndroid Build Coastguard Worker static void (*tr_old_free_hook) __P ((__ptr_t ptr));
48*6a54128fSAndroid Build Coastguard Worker static __ptr_t (*tr_old_malloc_hook) __P ((size_t size));
49*6a54128fSAndroid Build Coastguard Worker static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr, size_t size));
50*6a54128fSAndroid Build Coastguard Worker
51*6a54128fSAndroid Build Coastguard Worker /*
52*6a54128fSAndroid Build Coastguard Worker * Added by TYT, 10/10/93 --- so that we can print
53*6a54128fSAndroid Build Coastguard Worker */
malloc_get_mallstream()54*6a54128fSAndroid Build Coastguard Worker FILE *malloc_get_mallstream()
55*6a54128fSAndroid Build Coastguard Worker {
56*6a54128fSAndroid Build Coastguard Worker return mallstream;
57*6a54128fSAndroid Build Coastguard Worker }
58*6a54128fSAndroid Build Coastguard Worker
59*6a54128fSAndroid Build Coastguard Worker /* This function is called when the block being alloc'd, realloc'd, or
60*6a54128fSAndroid Build Coastguard Worker freed has an address matching the variable "mallwatch". In a debugger,
61*6a54128fSAndroid Build Coastguard Worker set "mallwatch" to the address of interest, then put a breakpoint on
62*6a54128fSAndroid Build Coastguard Worker tr_break. */
63*6a54128fSAndroid Build Coastguard Worker
64*6a54128fSAndroid Build Coastguard Worker void tr_break __P ((void));
65*6a54128fSAndroid Build Coastguard Worker void
tr_break()66*6a54128fSAndroid Build Coastguard Worker tr_break ()
67*6a54128fSAndroid Build Coastguard Worker {
68*6a54128fSAndroid Build Coastguard Worker }
69*6a54128fSAndroid Build Coastguard Worker
70*6a54128fSAndroid Build Coastguard Worker static void tr_freehook __P ((__ptr_t));
71*6a54128fSAndroid Build Coastguard Worker static void
tr_freehook(ptr)72*6a54128fSAndroid Build Coastguard Worker tr_freehook (ptr)
73*6a54128fSAndroid Build Coastguard Worker __ptr_t ptr;
74*6a54128fSAndroid Build Coastguard Worker {
75*6a54128fSAndroid Build Coastguard Worker fprintf (mallstream, "- %p\n", ptr); /* Be sure to print it first. */
76*6a54128fSAndroid Build Coastguard Worker if (ptr == mallwatch)
77*6a54128fSAndroid Build Coastguard Worker tr_break ();
78*6a54128fSAndroid Build Coastguard Worker __free_hook = tr_old_free_hook;
79*6a54128fSAndroid Build Coastguard Worker free (ptr);
80*6a54128fSAndroid Build Coastguard Worker __free_hook = tr_freehook;
81*6a54128fSAndroid Build Coastguard Worker }
82*6a54128fSAndroid Build Coastguard Worker
83*6a54128fSAndroid Build Coastguard Worker static __ptr_t tr_mallochook __P ((size_t));
84*6a54128fSAndroid Build Coastguard Worker static __ptr_t
tr_mallochook(size)85*6a54128fSAndroid Build Coastguard Worker tr_mallochook (size)
86*6a54128fSAndroid Build Coastguard Worker size_t size;
87*6a54128fSAndroid Build Coastguard Worker {
88*6a54128fSAndroid Build Coastguard Worker __ptr_t hdr;
89*6a54128fSAndroid Build Coastguard Worker
90*6a54128fSAndroid Build Coastguard Worker __malloc_hook = tr_old_malloc_hook;
91*6a54128fSAndroid Build Coastguard Worker hdr = (__ptr_t) malloc (size);
92*6a54128fSAndroid Build Coastguard Worker __malloc_hook = tr_mallochook;
93*6a54128fSAndroid Build Coastguard Worker
94*6a54128fSAndroid Build Coastguard Worker /* We could be printing a NULL here; that's OK. */
95*6a54128fSAndroid Build Coastguard Worker fprintf (mallstream, "+ %p %d\n", hdr, size);
96*6a54128fSAndroid Build Coastguard Worker
97*6a54128fSAndroid Build Coastguard Worker if (hdr == mallwatch)
98*6a54128fSAndroid Build Coastguard Worker tr_break ();
99*6a54128fSAndroid Build Coastguard Worker
100*6a54128fSAndroid Build Coastguard Worker return hdr;
101*6a54128fSAndroid Build Coastguard Worker }
102*6a54128fSAndroid Build Coastguard Worker
103*6a54128fSAndroid Build Coastguard Worker static __ptr_t tr_reallochook __P ((__ptr_t, size_t));
104*6a54128fSAndroid Build Coastguard Worker static __ptr_t
tr_reallochook(ptr,size)105*6a54128fSAndroid Build Coastguard Worker tr_reallochook (ptr, size)
106*6a54128fSAndroid Build Coastguard Worker __ptr_t ptr;
107*6a54128fSAndroid Build Coastguard Worker size_t size;
108*6a54128fSAndroid Build Coastguard Worker {
109*6a54128fSAndroid Build Coastguard Worker __ptr_t hdr;
110*6a54128fSAndroid Build Coastguard Worker
111*6a54128fSAndroid Build Coastguard Worker if (ptr == mallwatch)
112*6a54128fSAndroid Build Coastguard Worker tr_break ();
113*6a54128fSAndroid Build Coastguard Worker
114*6a54128fSAndroid Build Coastguard Worker __free_hook = tr_old_free_hook;
115*6a54128fSAndroid Build Coastguard Worker __malloc_hook = tr_old_malloc_hook;
116*6a54128fSAndroid Build Coastguard Worker __realloc_hook = tr_old_realloc_hook;
117*6a54128fSAndroid Build Coastguard Worker hdr = (__ptr_t) realloc (ptr, size);
118*6a54128fSAndroid Build Coastguard Worker __free_hook = tr_freehook;
119*6a54128fSAndroid Build Coastguard Worker __malloc_hook = tr_mallochook;
120*6a54128fSAndroid Build Coastguard Worker __realloc_hook = tr_reallochook;
121*6a54128fSAndroid Build Coastguard Worker if (hdr == NULL)
122*6a54128fSAndroid Build Coastguard Worker /* Failed realloc. */
123*6a54128fSAndroid Build Coastguard Worker fprintf (mallstream, "! %p %d\n", ptr, size);
124*6a54128fSAndroid Build Coastguard Worker else
125*6a54128fSAndroid Build Coastguard Worker fprintf (mallstream, "< %p\n> %p %d\n", ptr, hdr, size);
126*6a54128fSAndroid Build Coastguard Worker
127*6a54128fSAndroid Build Coastguard Worker if (hdr == mallwatch)
128*6a54128fSAndroid Build Coastguard Worker tr_break ();
129*6a54128fSAndroid Build Coastguard Worker
130*6a54128fSAndroid Build Coastguard Worker return hdr;
131*6a54128fSAndroid Build Coastguard Worker }
132*6a54128fSAndroid Build Coastguard Worker
133*6a54128fSAndroid Build Coastguard Worker /* We enable tracing if either the environment variable MALLOC_TRACE
134*6a54128fSAndroid Build Coastguard Worker is set, or if the variable mallwatch has been patched to an address
135*6a54128fSAndroid Build Coastguard Worker that the debugging user wants us to stop on. When patching mallwatch,
136*6a54128fSAndroid Build Coastguard Worker don't forget to set a breakpoint on tr_break! */
137*6a54128fSAndroid Build Coastguard Worker
138*6a54128fSAndroid Build Coastguard Worker void
mtrace()139*6a54128fSAndroid Build Coastguard Worker mtrace ()
140*6a54128fSAndroid Build Coastguard Worker {
141*6a54128fSAndroid Build Coastguard Worker char *mallfile;
142*6a54128fSAndroid Build Coastguard Worker
143*6a54128fSAndroid Build Coastguard Worker mallfile = getenv (mallenv);
144*6a54128fSAndroid Build Coastguard Worker if (mallfile != NULL || mallwatch != NULL)
145*6a54128fSAndroid Build Coastguard Worker {
146*6a54128fSAndroid Build Coastguard Worker mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
147*6a54128fSAndroid Build Coastguard Worker if (mallstream != NULL)
148*6a54128fSAndroid Build Coastguard Worker {
149*6a54128fSAndroid Build Coastguard Worker /* Be sure it doesn't malloc its buffer! */
150*6a54128fSAndroid Build Coastguard Worker setbuf (mallstream, mallbuf);
151*6a54128fSAndroid Build Coastguard Worker fprintf (mallstream, "= Start\n");
152*6a54128fSAndroid Build Coastguard Worker tr_old_free_hook = __free_hook;
153*6a54128fSAndroid Build Coastguard Worker __free_hook = tr_freehook;
154*6a54128fSAndroid Build Coastguard Worker tr_old_malloc_hook = __malloc_hook;
155*6a54128fSAndroid Build Coastguard Worker __malloc_hook = tr_mallochook;
156*6a54128fSAndroid Build Coastguard Worker tr_old_realloc_hook = __realloc_hook;
157*6a54128fSAndroid Build Coastguard Worker __realloc_hook = tr_reallochook;
158*6a54128fSAndroid Build Coastguard Worker }
159*6a54128fSAndroid Build Coastguard Worker }
160*6a54128fSAndroid Build Coastguard Worker }
161