Home | History | Annotate | Download | only in e2fsck
      1 /* More debugging hooks for `malloc'.
      2    Copyright (C) 1991, 1992 Free Software Foundation, Inc.
      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
     18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
     19 Cambridge, MA 02139, USA.
     20 
     21    The author may be reached (Email) at the address mike (at) ai.mit.edu,
     22    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
     23 
     24 #include "config.h"
     25 
     26 #ifndef	_MALLOC_INTERNAL
     27 #define	_MALLOC_INTERNAL
     28 #include "./mtrace.h"
     29 #endif
     30 
     31 #include <stdio.h>
     32 
     33 #ifndef	__GNU_LIBRARY__
     34 extern char *getenv ();
     35 #else
     36 #include <stdlib.h>
     37 #endif
     38 
     39 static FILE *mallstream;
     40 static char mallenv[]= "MALLOC_TRACE";
     41 static char mallbuf[BUFSIZ];	/* Buffer for the output.  */
     42 
     43 /* Address to breakpoint on accesses to... */
     44 __ptr_t mallwatch;
     45 
     46 /* Old hook values.  */
     47 static void (*tr_old_free_hook) __P ((__ptr_t ptr));
     48 static __ptr_t (*tr_old_malloc_hook) __P ((size_t size));
     49 static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr, size_t size));
     50 
     51 /*
     52  * Added by TYT, 10/10/93 --- so that we can print
     53  */
     54 FILE *malloc_get_mallstream()
     55 {
     56 	return mallstream;
     57 }
     58 
     59 /* This function is called when the block being alloc'd, realloc'd, or
     60    freed has an address matching the variable "mallwatch".  In a debugger,
     61    set "mallwatch" to the address of interest, then put a breakpoint on
     62    tr_break.  */
     63 
     64 void tr_break __P ((void));
     65 void
     66 tr_break ()
     67 {
     68 }
     69 
     70 static void tr_freehook __P ((__ptr_t));
     71 static void
     72 tr_freehook (ptr)
     73      __ptr_t ptr;
     74 {
     75   fprintf (mallstream, "- %p\n", ptr);	/* Be sure to print it first.  */
     76   if (ptr == mallwatch)
     77     tr_break ();
     78   __free_hook = tr_old_free_hook;
     79   free (ptr);
     80   __free_hook = tr_freehook;
     81 }
     82 
     83 static __ptr_t tr_mallochook __P ((size_t));
     84 static __ptr_t
     85 tr_mallochook (size)
     86      size_t size;
     87 {
     88   __ptr_t hdr;
     89 
     90   __malloc_hook = tr_old_malloc_hook;
     91   hdr = (__ptr_t) malloc (size);
     92   __malloc_hook = tr_mallochook;
     93 
     94   /* We could be printing a NULL here; that's OK.  */
     95   fprintf (mallstream, "+ %p %d\n", hdr, size);
     96 
     97   if (hdr == mallwatch)
     98     tr_break ();
     99 
    100   return hdr;
    101 }
    102 
    103 static __ptr_t tr_reallochook __P ((__ptr_t, size_t));
    104 static __ptr_t
    105 tr_reallochook (ptr, size)
    106      __ptr_t ptr;
    107      size_t size;
    108 {
    109   __ptr_t hdr;
    110 
    111   if (ptr == mallwatch)
    112     tr_break ();
    113 
    114   __free_hook = tr_old_free_hook;
    115   __malloc_hook = tr_old_malloc_hook;
    116   __realloc_hook = tr_old_realloc_hook;
    117   hdr = (__ptr_t) realloc (ptr, size);
    118   __free_hook = tr_freehook;
    119   __malloc_hook = tr_mallochook;
    120   __realloc_hook = tr_reallochook;
    121   if (hdr == NULL)
    122     /* Failed realloc.  */
    123     fprintf (mallstream, "! %p %d\n", ptr, size);
    124   else
    125     fprintf (mallstream, "< %p\n> %p %d\n", ptr, hdr, size);
    126 
    127   if (hdr == mallwatch)
    128     tr_break ();
    129 
    130   return hdr;
    131 }
    132 
    133 /* We enable tracing if either the environment variable MALLOC_TRACE
    134    is set, or if the variable mallwatch has been patched to an address
    135    that the debugging user wants us to stop on.  When patching mallwatch,
    136    don't forget to set a breakpoint on tr_break!  */
    137 
    138 void
    139 mtrace ()
    140 {
    141   char *mallfile;
    142 
    143   mallfile = getenv (mallenv);
    144   if (mallfile != NULL || mallwatch != NULL)
    145     {
    146       mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
    147       if (mallstream != NULL)
    148 	{
    149 	  /* Be sure it doesn't malloc its buffer!  */
    150 	  setbuf (mallstream, mallbuf);
    151 	  fprintf (mallstream, "= Start\n");
    152 	  tr_old_free_hook = __free_hook;
    153 	  __free_hook = tr_freehook;
    154 	  tr_old_malloc_hook = __malloc_hook;
    155 	  __malloc_hook = tr_mallochook;
    156 	  tr_old_realloc_hook = __realloc_hook;
    157 	  __realloc_hook = tr_reallochook;
    158 	}
    159     }
    160 }
    161