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