Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
We don't intend to support pre-ansi platforms, so cleanup mmalloc code
[simgrid.git] / src / xbt / mmalloc / mmtrace.c
1 /* More debugging hooks for `mmalloc'.
2    Copyright 1991, 1992, 1994 Free Software Foundation
3
4    Written April 2, 1991 by John Gilmore of Cygnus Support
5    Based on mcheck.c by Mike Haertel.
6    Modified Mar 1992 by Fred Fish.  (fnf@cygnus.com)
7
8 This file is part of the GNU C Library.
9
10 The GNU C Library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
14
15 The GNU C Library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 Library General Public License for more details.
19
20 You should have received a copy of the GNU Library General Public
21 License along with the GNU C Library; see the file COPYING.LIB.  If
22 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.  */
24
25 #include <stdio.h>
26 #include "mmprivate.h"
27
28 static void tr_break (void);
29 static void tr_freehook (void*md, void*ptr);
30 static void* tr_mallochook (void* md, size_t size);
31 static void* tr_reallochook (void* md, void* ptr, size_t size);
32
33 #ifndef __GNU_LIBRARY__
34 extern char *getenv ();
35 #endif
36
37 static FILE *mallstream;
38
39 #if 0   /* FIXME:  Disabled for now. */
40 static char mallenv[] = "MALLOC_TRACE";
41 static char mallbuf[BUFSIZ];    /* Buffer for the output.  */
42 #endif
43
44 /* Address to breakpoint on accesses to... */
45 static void* mallwatch;
46
47 /* Old hook values.  */
48
49 static void (*old_mfree_hook) (void* md, void* ptr);
50 static void* (*old_mmalloc_hook) (void* md, size_t size);
51 static void* (*old_mrealloc_hook) (void* md, void* ptr, size_t size);
52
53 /* This function is called when the block being alloc'd, realloc'd, or
54    freed has an address matching the variable "mallwatch".  In a debugger,
55    set "mallwatch" to the address of interest, then put a breakpoint on
56    tr_break.  */
57
58 static void
59 tr_break (void)
60 {
61 }
62
63 static void
64 tr_freehook (void *md, void *ptr)
65 {
66   struct mdesc *mdp;
67
68   mdp = MD_TO_MDP (md);
69   /* Be sure to print it first.  */
70   fprintf (mallstream, "- %08lx\n", (unsigned long) ptr);
71   if (ptr == mallwatch)
72     tr_break ();
73   mdp -> mfree_hook = old_mfree_hook;
74   mfree (md, ptr);
75   mdp -> mfree_hook = tr_freehook;
76 }
77
78 static void*
79 tr_mallochook (void* md, size_t size)
80 {
81   void* hdr;
82   struct mdesc *mdp;
83
84   mdp = MD_TO_MDP (md);
85   mdp -> mmalloc_hook = old_mmalloc_hook;
86   hdr = (void*) mmalloc (md, size);
87   mdp -> mmalloc_hook = tr_mallochook;
88
89   /* We could be printing a NULL here; that's OK.  */
90   fprintf (mallstream, "+ %p 0x%lx\n", hdr, (unsigned long)size);
91
92   if (hdr == mallwatch)
93     tr_break ();
94
95   return (hdr);
96 }
97
98 static void*
99 tr_reallochook (void *md, void *ptr, size_t size)
100 {
101   void* hdr;
102   struct mdesc *mdp;
103
104   mdp = MD_TO_MDP (md);
105
106   if (ptr == mallwatch)
107     tr_break ();
108
109   mdp -> mfree_hook = old_mfree_hook;
110   mdp -> mmalloc_hook = old_mmalloc_hook;
111   mdp -> mrealloc_hook = old_mrealloc_hook;
112   hdr = (void*) mrealloc (md, ptr, size);
113   mdp -> mfree_hook = tr_freehook;
114   mdp -> mmalloc_hook = tr_mallochook;
115   mdp -> mrealloc_hook = tr_reallochook;
116   if (hdr == NULL)
117     /* Failed realloc.  */
118     fprintf (mallstream, "! %p 0x%lx\n", ptr, (unsigned long) size);
119   else
120     fprintf (mallstream, "< %p\n> %p 0x%lx\n", ptr,
121              hdr, (unsigned long) size);
122
123   if (hdr == mallwatch)
124     tr_break ();
125
126   return hdr;
127 }
128
129 /* We enable tracing if either the environment variable MALLOC_TRACE
130    is set, or if the variable mallwatch has been patched to an address
131    that the debugging user wants us to stop on.  When patching mallwatch,
132    don't forget to set a breakpoint on tr_break!  */
133
134 int
135 mmtrace (void)
136 {
137 #if 0   /* FIXME!  This is disabled for now until we figure out how to
138            maintain a stack of hooks per heap, since we might have other
139            hooks (such as set by mmcheck/mmcheckf) active also. */
140   char *mallfile;
141
142   mallfile = getenv (mallenv);
143   if (mallfile  != NULL || mallwatch != NULL)
144     {
145       mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
146       if (mallstream != NULL)
147         {
148           /* Be sure it doesn't mmalloc its buffer!  */
149           setbuf (mallstream, mallbuf);
150           fprintf (mallstream, "= Start\n");
151           old_mfree_hook = mdp -> mfree_hook;
152           mdp -> mfree_hook = tr_freehook;
153           old_mmalloc_hook = mdp -> mmalloc_hook;
154           mdp -> mmalloc_hook = tr_mallochook;
155           old_mrealloc_hook = mdp -> mrealloc_hook;
156           mdp -> mrealloc_hook = tr_reallochook;
157         }
158     }
159
160 #endif  /* 0 */
161
162   return (1);
163 }
164