Commit | Line | Data |
---|---|---|
07d021a6 JG |
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. | |
5 | ||
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 1, or (at your option) | |
9 | any later version. | |
10 | ||
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. | |
15 | ||
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. | |
19 | ||
20 | The author may be reached (Email) at the address mike@ai.mit.edu, | |
21 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ | |
22 | ||
23 | #include <stdio.h> | |
24 | #include "ansidecl.h" | |
25 | #include <stdlib.h> | |
26 | #include "gmalloc.h" | |
27 | ||
28 | extern char *getenv(); | |
29 | ||
30 | FILE *mallstream; | |
31 | char mallenv[] = "MALLOC_TRACE"; | |
32 | static char mallbuf[BUFSIZ]; /* Buffer for the output */ | |
33 | ||
34 | /* Address to breakpoint on accesses to... */ | |
35 | PTR mallwatch; | |
36 | ||
37 | /* Old hook values. */ | |
38 | static void EXFUN((*old_free_hook), (PTR ptr)); | |
39 | static PTR EXFUN((*old_malloc_hook), (size_t size)); | |
40 | static PTR EXFUN((*old_realloc_hook), (PTR ptr, size_t size)); | |
41 | ||
42 | /* This function is called when the block being alloc'd, realloc'd, or | |
43 | freed has an address matching the variable "mallwatch". In a debugger, | |
44 | set "mallwatch" to the address of interest, then put a breakpoint on | |
45 | tr_break. */ | |
46 | ||
47 | void | |
48 | tr_break() | |
49 | { | |
50 | ; | |
51 | } | |
52 | ||
53 | static void | |
54 | DEFUN(tr_freehook, (ptr), PTR ptr) | |
55 | { | |
56 | fprintf(mallstream, "- %08x\n", ptr); /* Be sure to print it first */ | |
57 | if (ptr == mallwatch) | |
58 | tr_break(); | |
59 | __free_hook = old_free_hook; | |
60 | free(ptr); | |
61 | __free_hook = tr_freehook; | |
62 | } | |
63 | ||
64 | static PTR | |
65 | DEFUN(tr_mallochook, (size), size_t size) | |
66 | { | |
67 | PTR hdr; | |
68 | ||
69 | __malloc_hook = old_malloc_hook; | |
70 | hdr = malloc(size); | |
71 | __malloc_hook = tr_mallochook; | |
72 | ||
73 | /* We could be printing a NULL here; that's OK */ | |
74 | fprintf (mallstream, "+ %08x %x\n", hdr, size); | |
75 | ||
76 | if (hdr == mallwatch) | |
77 | tr_break(); | |
78 | ||
79 | return hdr; | |
80 | } | |
81 | ||
82 | static PTR | |
83 | DEFUN(tr_reallochook, (ptr, size), PTR ptr AND size_t size) | |
84 | { | |
85 | PTR hdr; | |
86 | ||
87 | if (ptr == mallwatch) | |
88 | tr_break(); | |
89 | ||
90 | __free_hook = old_free_hook; | |
91 | __malloc_hook = old_malloc_hook; | |
92 | __realloc_hook = old_realloc_hook; | |
93 | hdr = realloc(ptr, size); | |
94 | __free_hook = tr_freehook; | |
95 | __malloc_hook = tr_mallochook; | |
96 | __realloc_hook = tr_reallochook; | |
97 | if (hdr == NULL) { | |
98 | fprintf (mallstream, "! %08x %x\n", ptr, size); /* Failed realloc */ | |
99 | } else { | |
100 | fprintf (mallstream, "< %08x\n> %08x %x\n", ptr, hdr, size); | |
101 | } | |
102 | ||
103 | if (hdr == mallwatch) | |
104 | tr_break(); | |
105 | ||
106 | return hdr; | |
107 | } | |
108 | ||
109 | void | |
110 | mtrace() | |
111 | { | |
112 | char *mallfile; | |
113 | ||
114 | mallfile = getenv (mallenv); | |
115 | if (mallfile) { | |
116 | mallstream = fopen (mallfile, "w"); | |
117 | if (mallstream) { | |
118 | /* Be sure it doesn't malloc its buffer! */ | |
119 | setbuf (mallstream, mallbuf); | |
120 | fprintf (mallstream, "= Start\n"); | |
121 | old_free_hook = __free_hook; | |
122 | __free_hook = tr_freehook; | |
123 | old_malloc_hook = __malloc_hook; | |
124 | __malloc_hook = tr_mallochook; | |
125 | old_realloc_hook = __realloc_hook; | |
126 | __realloc_hook = tr_reallochook; | |
127 | } | |
128 | } | |
129 | } |