1 /* More debugging hooks for `malloc'.
2 Copyright 1991 Free Software Foundation
3 Written April 2, 1991 by John Gilmore of Cygnus Support
4 Based on mcheck.c by Mike Haertel.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* size_t may be defined in the system-supplied stdio.h. */
24 /* So just kludge it. */
25 #define size_t unsigned int
29 /* We can't declare malloc and realloc here because we don't know
30 if they are char * or void *, and the compiler will give an error
31 if we get it wrong and they happen to be defined in some header
32 file e.g. <stdio.h>. We can't include <stdlib.h> here because
33 it has some incompatability with our own includes, e.g. size_t or
34 whatever. So we just punt. This causes malloc and realloc to
35 default to returning "int", which works for most cases we care
36 about. FIXME-somehow. */
37 /* #include <stdlib.h> */
40 extern char *getenv();
43 char mallenv
[] = "MALLOC_TRACE";
44 static char mallbuf
[BUFSIZ
]; /* Buffer for the output */
46 /* Address to breakpoint on accesses to... */
49 /* Old hook values. */
50 static void EXFUN((*old_free_hook
), (PTR ptr
));
51 static PTR
EXFUN((*old_malloc_hook
), (size_t size
));
52 static PTR
EXFUN((*old_realloc_hook
), (PTR ptr
, size_t size
));
54 /* This function is called when the block being alloc'd, realloc'd, or
55 freed has an address matching the variable "mallwatch". In a debugger,
56 set "mallwatch" to the address of interest, then put a breakpoint on
66 DEFUN(tr_freehook
, (ptr
), PTR ptr
)
68 fprintf(mallstream
, "- %08x\n", ptr
); /* Be sure to print it first */
71 __free_hook
= old_free_hook
;
73 __free_hook
= tr_freehook
;
77 DEFUN(tr_mallochook
, (size
), size_t size
)
81 __malloc_hook
= old_malloc_hook
;
82 hdr
= (PTR
) malloc(size
);
83 __malloc_hook
= tr_mallochook
;
85 /* We could be printing a NULL here; that's OK */
86 fprintf (mallstream
, "+ %08x %x\n", hdr
, size
);
95 DEFUN(tr_reallochook
, (ptr
, size
), PTR ptr AND
size_t size
)
102 __free_hook
= old_free_hook
;
103 __malloc_hook
= old_malloc_hook
;
104 __realloc_hook
= old_realloc_hook
;
105 hdr
= (PTR
) realloc(ptr
, size
);
106 __free_hook
= tr_freehook
;
107 __malloc_hook
= tr_mallochook
;
108 __realloc_hook
= tr_reallochook
;
110 fprintf (mallstream
, "! %08x %x\n", ptr
, size
); /* Failed realloc */
112 fprintf (mallstream
, "< %08x\n> %08x %x\n", ptr
, hdr
, size
);
115 if (hdr
== mallwatch
)
126 mallfile
= getenv (mallenv
);
128 mallstream
= fopen (mallfile
, "w");
130 /* Be sure it doesn't malloc its buffer! */
131 setbuf (mallstream
, mallbuf
);
132 fprintf (mallstream
, "= Start\n");
133 old_free_hook
= __free_hook
;
134 __free_hook
= tr_freehook
;
135 old_malloc_hook
= __malloc_hook
;
136 __malloc_hook
= tr_mallochook
;
137 old_realloc_hook
= __realloc_hook
;
138 __realloc_hook
= tr_reallochook
;
This page took 0.033379 seconds and 5 git commands to generate.