No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / lib / libmalloc / mtrace.c
blob5dae86aa808224f9f252164076fabedf6c3e277b
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.
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.
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.
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.
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. */
24 #ifndef _MALLOC_INTERNAL
25 #define _MALLOC_INTERNAL
26 #include <malloc.h>
27 #endif
29 #include <stdio.h>
31 #ifndef __GNU_LIBRARY__
32 extern char *getenv ();
33 #else
34 #include <stdlib.h>
35 #endif
37 static FILE *mallstream;
38 static char mallenv[]= "MALLOC_TRACE";
39 static char mallbuf[BUFSIZ]; /* Buffer for the output. */
41 /* Address to breakpoint on accesses to... */
42 __ptr_t mallwatch;
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));
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. */
54 void tr_break __P ((void));
55 void
56 tr_break ()
60 static void tr_freehook __P ((__ptr_t));
61 static void
62 tr_freehook (ptr)
63 __ptr_t ptr;
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;
73 static __ptr_t tr_mallochook __P ((size_t));
74 static __ptr_t
75 tr_mallochook (size)
76 size_t size;
78 __ptr_t hdr;
80 __malloc_hook = tr_old_malloc_hook;
81 hdr = (__ptr_t) malloc (size);
82 __malloc_hook = tr_mallochook;
84 /* We could be printing a NULL here; that's OK. */
85 fprintf (mallstream, "+ %p %x\n", hdr, size);
87 if (hdr == mallwatch)
88 tr_break ();
90 return hdr;
93 static __ptr_t tr_reallochook __P ((__ptr_t, size_t));
94 static __ptr_t
95 tr_reallochook (ptr, size)
96 __ptr_t ptr;
97 size_t size;
99 __ptr_t hdr;
101 if (ptr == mallwatch)
102 tr_break ();
104 __free_hook = tr_old_free_hook;
105 __malloc_hook = tr_old_malloc_hook;
106 __realloc_hook = tr_old_realloc_hook;
107 hdr = (__ptr_t) realloc (ptr, size);
108 __free_hook = tr_freehook;
109 __malloc_hook = tr_mallochook;
110 __realloc_hook = tr_reallochook;
111 if (hdr == NULL)
112 /* Failed realloc. */
113 fprintf (mallstream, "! %p %x\n", ptr, size);
114 else
115 fprintf (mallstream, "< %p\n> %p %x\n", ptr, hdr, size);
117 if (hdr == mallwatch)
118 tr_break ();
120 return hdr;
123 /* We enable tracing if either the environment variable MALLOC_TRACE
124 is set, or if the variable mallwatch has been patched to an address
125 that the debugging user wants us to stop on. When patching mallwatch,
126 don't forget to set a breakpoint on tr_break! */
128 void
129 mtrace ()
131 char *mallfile;
133 mallfile = getenv (mallenv);
134 if (mallfile != NULL || mallwatch != NULL)
136 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
137 if (mallstream != NULL)
139 /* Be sure it doesn't malloc its buffer! */
140 setbuf (mallstream, mallbuf);
141 fprintf (mallstream, "= Start\n");
142 tr_old_free_hook = __free_hook;
143 __free_hook = tr_freehook;
144 tr_old_malloc_hook = __malloc_hook;
145 __malloc_hook = tr_mallochook;
146 tr_old_realloc_hook = __realloc_hook;
147 __realloc_hook = tr_reallochook;