mmtrace.c

Go to the documentation of this file.
00001 /* @(#)root/clib:$Id: mmtrace.c 22527 2008-03-07 16:37:03Z rdm $ */
00002 /* Author: */
00003 
00004 /* More debugging hooks for `mmalloc'.
00005    Copyright 1991, 1992, 1994 Free Software Foundation
00006 
00007    Written April 2, 1991 by John Gilmore of Cygnus Support
00008    Based on mcheck.c by Mike Haertel.
00009    Modified Mar 1992 by Fred Fish.  (fnf@cygnus.com)
00010 
00011 This file is part of the GNU C Library.
00012 
00013 The GNU C Library is free software; you can redistribute it and/or
00014 modify it under the terms of the GNU Library General Public License as
00015 published by the Free Software Foundation; either version 2 of the
00016 License, or (at your option) any later version.
00017 
00018 The GNU C Library is distributed in the hope that it will be useful,
00019 but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 Library General Public License for more details.
00022 
00023 You should have received a copy of the GNU Library General Public
00024 License along with the GNU C Library; see the file COPYING.LIB.  If
00025 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00028 #include <stdio.h>
00029 #include "mmprivate.h"
00030 
00031 #ifndef __GNU_LIBRARY__
00032 extern char *getenv ();
00033 #endif
00034 
00035 #if 0
00036 static FILE *mallstream;
00037 #endif
00038 
00039 #if 0   /* FIXME:  Disabled for now. */
00040 static char mallenv[] = "MALLOC_TRACE";
00041 static char mallbuf[BUFSIZ];    /* Buffer for the output.  */
00042 #endif
00043 
00044 /* Address to breakpoint on accesses to... */
00045 #if 0
00046 static PTR mallwatch;
00047 #endif
00048 
00049 /* Old hook values.  */
00050 
00051 #if 0
00052 static void (*old_mfree_hook) PARAMS ((PTR, PTR));
00053 static PTR (*old_mmalloc_hook) PARAMS ((PTR, size_t));
00054 static PTR (*old_mrealloc_hook) PARAMS ((PTR, PTR, size_t));
00055 #endif
00056 
00057 typedef void (*mmfree_fun_t) PARAMS ((PTR, PTR));
00058 typedef PTR (*mmalloc_fun_t) PARAMS ((PTR, size_t));
00059 typedef PTR (*mmrealloc_fun_t) PARAMS ((PTR, PTR, size_t));
00060 
00061 /* This function is called when the block being alloc'd, realloc'd, or
00062    freed has an address matching the variable "mallwatch".  In a debugger,
00063    set "mallwatch" to the address of interest, then put a breakpoint on
00064    tr_break.  */
00065 
00066 #if 0
00067 static void
00068 tr_break ()
00069 {
00070 }
00071 
00072 static void
00073 tr_freehook (md, ptr)
00074   PTR md;
00075   PTR ptr;
00076 {
00077   struct mdesc *mdp;
00078 
00079   mdp = MD_TO_MDP (md);
00080   /* Be sure to print it first.  */
00081   fprintf (mallstream, "- %08lx\n", (unsigned long) ptr);
00082   if (ptr == mallwatch)
00083     tr_break ();
00084   mdp -> mfree_hook = (mmfree_fun_t) old_mfree_hook;
00085   mfree (md, ptr);
00086   mdp -> mfree_hook = (mmfree_fun_t) tr_freehook;
00087 }
00088 
00089 static PTR
00090 tr_mallochook (md, size)
00091   PTR md;
00092   size_t size;
00093 {
00094   PTR hdr;
00095   struct mdesc *mdp;
00096 
00097   mdp = MD_TO_MDP (md);
00098   mdp -> mmalloc_hook = (mmalloc_fun_t) old_mmalloc_hook;
00099   hdr = (PTR) mmalloc (md, size);
00100   mdp -> mmalloc_hook = (mmalloc_fun_t) tr_mallochook;
00101 
00102   /* We could be printing a NULL here; that's OK.  */
00103   fprintf (mallstream, "+ %08lx %x\n", (unsigned long) hdr, (unsigned) size);
00104 
00105   if (hdr == mallwatch)
00106     tr_break ();
00107 
00108   return (hdr);
00109 }
00110 
00111 static PTR
00112 tr_reallochook (md, ptr, size)
00113   PTR md;
00114   PTR ptr;
00115   size_t size;
00116 {
00117   PTR hdr;
00118   struct mdesc *mdp;
00119 
00120   mdp = MD_TO_MDP (md);
00121 
00122   if (ptr == mallwatch)
00123     tr_break ();
00124 
00125   mdp -> mfree_hook = (mmfree_fun_t) old_mfree_hook;
00126   mdp -> mmalloc_hook = (mmalloc_fun_t) old_mmalloc_hook;
00127   mdp -> mrealloc_hook = (mmrealloc_fun_t) old_mrealloc_hook;
00128   hdr = (PTR) mrealloc (md, ptr, size);
00129   mdp -> mfree_hook = (mmfree_fun_t) tr_freehook;
00130   mdp -> mmalloc_hook = (mmalloc_fun_t) tr_mallochook;
00131   mdp -> mrealloc_hook = (mmrealloc_fun_t) tr_reallochook;
00132   if (hdr == NULL)
00133     /* Failed realloc.  */
00134     fprintf (mallstream, "! %08lx %x\n", (unsigned long) ptr, (unsigned) size);
00135   else
00136     fprintf (mallstream, "< %08lx\n> %08lx %x\n", (unsigned long) ptr,
00137              (unsigned long) hdr, (unsigned) size);
00138 
00139   if (hdr == mallwatch)
00140     tr_break ();
00141 
00142   return hdr;
00143 }
00144 #endif
00145 
00146 /* We enable tracing if either the environment variable MALLOC_TRACE
00147    is set, or if the variable mallwatch has been patched to an address
00148    that the debugging user wants us to stop on.  When patching mallwatch,
00149    don't forget to set a breakpoint on tr_break!  */
00150 
00151 int
00152 mmtrace ()
00153 {
00154 #if 0   /* FIXME!  This is disabled for now until we figure out how to
00155            maintain a stack of hooks per heap, since we might have other
00156            hooks (such as set by mmcheck) active also. */
00157   char *mallfile;
00158 
00159   mallfile = getenv (mallenv);
00160   if (mallfile  != NULL || mallwatch != NULL)
00161     {
00162       mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
00163       if (mallstream != NULL)
00164         {
00165           /* Be sure it doesn't mmalloc its buffer!  */
00166           setbuf (mallstream, mallbuf);
00167           fprintf (mallstream, "= Start\n");
00168           old_mfree_hook = mdp -> mfree_hook;
00169           mdp -> mfree_hook = (mmfree_fun_t) tr_freehook;
00170           old_mmalloc_hook = mdp -> mmalloc_hook;
00171           mdp -> mmalloc_hook = (mmalloc_fun_t) tr_mallochook;
00172           old_mrealloc_hook = mdp -> mrealloc_hook;
00173           mdp -> mrealloc_hook = (mmrealloc_fun_t) tr_reallochook;
00174         }
00175     }
00176 
00177 #endif  /* 0 */
00178 
00179   return (1);
00180 }

Generated on Tue Jul 5 14:11:25 2011 for ROOT_528-00b_version by  doxygen 1.5.1