1/* More debugging hooks for `malloc'. 2 Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc. 3 Written April 2, 1991 by John Gilmore of Cygnus Support. 4 Based on mcheck.c by Mike Haertel. 5 6This library is free software; you can redistribute it and/or 7modify it under the terms of the GNU Library General Public License as 8published by the Free Software Foundation; either version 2 of the 9License, or (at your option) any later version. 10 11This library is distributed in the hope that it will be useful, 12but WITHOUT ANY WARRANTY; without even the implied warranty of 13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14Library General Public License for more details. 15 16You should have received a copy of the GNU Library General Public 17License along with this library; see the file COPYING.LIB. If 18not, write to the Free Software Foundation, Inc., 675 Mass Ave, 19Cambridge, MA 02139, USA. 20 21 The author may be reached (Email) at the address mike@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 <malloc.h> 27#endif 28 29#include <stdio.h> 30 31#ifndef __GNU_LIBRARY__ 32extern char *getenv (); 33#else 34#include <stdlib.h> 35#endif 36 37static FILE *mallstream; 38static char mallenv[]= "MALLOC_TRACE"; 39static char mallbuf[BUFSIZ]; /* Buffer for the output. */ 40 41/* Address to breakpoint on accesses to... */ 42__ptr_t mallwatch; 43 44/* Old hook values. */ 45static void (*tr_old_free_hook) __P ((__ptr_t ptr)); 46static __ptr_t (*tr_old_malloc_hook) __P ((size_t size)); 47static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr, size_t size)); 48 49/* This function is called when the block being alloc'd, realloc'd, or 50 freed has an address matching the variable "mallwatch". In a debugger, 51 set "mallwatch" to the address of interest, then put a breakpoint on 52 tr_break. */ 53 54void tr_break __P ((void)); 55void 56tr_break () 57{ 58} 59 60static void tr_freehook __P ((__ptr_t)); 61static void 62tr_freehook (ptr) 63 __ptr_t ptr; 64{ 65 fprintf (mallstream, "- %p\n", ptr); /* Be sure to print it first. */ 66 if (ptr == mallwatch) 67 tr_break (); 68 __free_hook = tr_old_free_hook; 69 free (ptr); 70 __free_hook = tr_freehook; 71} 72 73static __ptr_t tr_mallochook __P ((size_t)); 74static __ptr_t 75tr_mallochook(size_t size) 76{ 77 __ptr_t hdr; 78 79 __malloc_hook = tr_old_malloc_hook; 80 hdr = (__ptr_t) malloc (size); 81 __malloc_hook = tr_mallochook; 82 83 /* We could be printing a NULL here; that's OK. */ 84 fprintf (mallstream, "+ %p %zx\n", hdr, size); 85 86 if (hdr == mallwatch) 87 tr_break (); 88 89 return hdr; 90} 91 92static __ptr_t tr_reallochook __P ((__ptr_t, size_t)); 93static __ptr_t 94tr_reallochook (__ptr_t ptr, size_t size) 95{ 96 __ptr_t hdr; 97 98 if (ptr == mallwatch) 99 tr_break (); 100 101 __free_hook = tr_old_free_hook; 102 __malloc_hook = tr_old_malloc_hook; 103 __realloc_hook = tr_old_realloc_hook; 104 hdr = (__ptr_t) realloc (ptr, size); 105 __free_hook = tr_freehook; 106 __malloc_hook = tr_mallochook; 107 __realloc_hook = tr_reallochook; 108 if (hdr == NULL) 109 /* Failed realloc. */ 110 fprintf (mallstream, "! %p %zx\n", ptr, size); 111 else 112 fprintf (mallstream, "< %p\n> %p %zx\n", ptr, hdr, size); 113 114 if (hdr == mallwatch) 115 tr_break (); 116 117 return hdr; 118} 119 120/* We enable tracing if either the environment variable MALLOC_TRACE 121 is set, or if the variable mallwatch has been patched to an address 122 that the debugging user wants us to stop on. When patching mallwatch, 123 don't forget to set a breakpoint on tr_break! */ 124 125void 126mtrace () 127{ 128 char *mallfile; 129 130 mallfile = getenv (mallenv); 131 if (mallfile != NULL || mallwatch != NULL) 132 { 133 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w"); 134 if (mallstream != NULL) 135 { 136 /* Be sure it doesn't malloc its buffer! */ 137 setbuf (mallstream, mallbuf); 138 fprintf (mallstream, "= Start\n"); 139 tr_old_free_hook = __free_hook; 140 __free_hook = tr_freehook; 141 tr_old_malloc_hook = __malloc_hook; 142 __malloc_hook = tr_mallochook; 143 tr_old_realloc_hook = __realloc_hook; 144 __realloc_hook = tr_reallochook; 145 } 146 } 147} 148