Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
856004c787884a1a905d8471b764be54f3cef6de
[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 PARAMS ((void));
29 static void tr_freehook PARAMS ((PTR, PTR));
30 static PTR tr_mallochook PARAMS ((PTR, size_t));
31 static PTR tr_reallochook PARAMS ((PTR, PTR, size_t));
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 PTR mallwatch;
46
47 /* Old hook values.  */
48
49 static void (*old_mfree_hook) PARAMS ((PTR, PTR));
50 static PTR (*old_mmalloc_hook) PARAMS ((PTR, size_t));
51 static PTR (*old_mrealloc_hook) PARAMS ((PTR, PTR, size_t));
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 ()
60 {
61 }
62
63 static void
64 tr_freehook (md, ptr)
65   PTR md;
66   PTR ptr;
67 {
68   struct mdesc *mdp;
69
70   mdp = MD_TO_MDP (md);
71   /* Be sure to print it first.  */
72   fprintf (mallstream, "- %08lx\n", (unsigned long) ptr);
73   if (ptr == mallwatch)
74     tr_break ();
75   mdp -> mfree_hook = old_mfree_hook;
76   mfree (md, ptr);
77   mdp -> mfree_hook = tr_freehook;
78 }
79
80 static PTR
81 tr_mallochook (md, size)
82   PTR md;
83   size_t size;
84 {
85   PTR hdr;
86   struct mdesc *mdp;
87
88   mdp = MD_TO_MDP (md);
89   mdp -> mmalloc_hook = old_mmalloc_hook;
90   hdr = (PTR) mmalloc (md, size);
91   mdp -> mmalloc_hook = tr_mallochook;
92
93   /* We could be printing a NULL here; that's OK.  */
94   fprintf (mallstream, "+ %p 0x%lx\n", hdr, (unsigned long)size);
95
96   if (hdr == mallwatch)
97     tr_break ();
98
99   return (hdr);
100 }
101
102 static PTR
103 tr_reallochook (md, ptr, size)
104   PTR md;
105   PTR ptr;
106   size_t size;
107 {
108   PTR hdr;
109   struct mdesc *mdp;
110
111   mdp = MD_TO_MDP (md);
112
113   if (ptr == mallwatch)
114     tr_break ();
115
116   mdp -> mfree_hook = old_mfree_hook;
117   mdp -> mmalloc_hook = old_mmalloc_hook;
118   mdp -> mrealloc_hook = old_mrealloc_hook;
119   hdr = (PTR) mrealloc (md, ptr, size);
120   mdp -> mfree_hook = tr_freehook;
121   mdp -> mmalloc_hook = tr_mallochook;
122   mdp -> mrealloc_hook = tr_reallochook;
123   if (hdr == NULL)
124     /* Failed realloc.  */
125     fprintf (mallstream, "! %p 0x%lx\n", ptr, (unsigned long) size);
126   else
127     fprintf (mallstream, "< %p\n> %p 0x%lx\n", ptr,
128              hdr, (unsigned long) size);
129
130   if (hdr == mallwatch)
131     tr_break ();
132
133   return hdr;
134 }
135
136 /* We enable tracing if either the environment variable MALLOC_TRACE
137    is set, or if the variable mallwatch has been patched to an address
138    that the debugging user wants us to stop on.  When patching mallwatch,
139    don't forget to set a breakpoint on tr_break!  */
140
141 int
142 mmtrace ()
143 {
144 #if 0   /* FIXME!  This is disabled for now until we figure out how to
145            maintain a stack of hooks per heap, since we might have other
146            hooks (such as set by mmcheck/mmcheckf) active also. */
147   char *mallfile;
148
149   mallfile = getenv (mallenv);
150   if (mallfile  != NULL || mallwatch != NULL)
151     {
152       mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
153       if (mallstream != NULL)
154         {
155           /* Be sure it doesn't mmalloc its buffer!  */
156           setbuf (mallstream, mallbuf);
157           fprintf (mallstream, "= Start\n");
158           old_mfree_hook = mdp -> mfree_hook;
159           mdp -> mfree_hook = tr_freehook;
160           old_mmalloc_hook = mdp -> mmalloc_hook;
161           mdp -> mmalloc_hook = tr_mallochook;
162           old_mrealloc_hook = mdp -> mrealloc_hook;
163           mdp -> mrealloc_hook = tr_reallochook;
164         }
165     }
166
167 #endif  /* 0 */
168
169   return (1);
170 }
171