*** empty log message ***
[binutils-gdb.git] / gdb / mtrace.c
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
26 /* size_t may be defined in the system-supplied stdio.h. */
27 /* So just kludge it. */
28 #define size_t unsigned int
29 #define ptrdiff_t int
30 #define __ONEFILE
31
32 #include <stdlib.h>
33 #include "gmalloc.h"
34
35 extern char *getenv();
36
37 FILE *mallstream;
38 char mallenv[] = "MALLOC_TRACE";
39 static char mallbuf[BUFSIZ]; /* Buffer for the output */
40
41 /* Address to breakpoint on accesses to... */
42 PTR mallwatch;
43
44 /* Old hook values. */
45 static void EXFUN((*old_free_hook), (PTR ptr));
46 static PTR EXFUN((*old_malloc_hook), (size_t size));
47 static PTR EXFUN((*old_realloc_hook), (PTR 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
54 void
55 tr_break()
56 {
57 ;
58 }
59
60 static void
61 DEFUN(tr_freehook, (ptr), PTR ptr)
62 {
63 fprintf(mallstream, "- %08x\n", ptr); /* Be sure to print it first */
64 if (ptr == mallwatch)
65 tr_break();
66 __free_hook = old_free_hook;
67 free(ptr);
68 __free_hook = tr_freehook;
69 }
70
71 static PTR
72 DEFUN(tr_mallochook, (size), size_t size)
73 {
74 PTR hdr;
75
76 __malloc_hook = old_malloc_hook;
77 hdr = malloc(size);
78 __malloc_hook = tr_mallochook;
79
80 /* We could be printing a NULL here; that's OK */
81 fprintf (mallstream, "+ %08x %x\n", hdr, size);
82
83 if (hdr == mallwatch)
84 tr_break();
85
86 return hdr;
87 }
88
89 static PTR
90 DEFUN(tr_reallochook, (ptr, size), PTR ptr AND size_t size)
91 {
92 PTR hdr;
93
94 if (ptr == mallwatch)
95 tr_break();
96
97 __free_hook = old_free_hook;
98 __malloc_hook = old_malloc_hook;
99 __realloc_hook = old_realloc_hook;
100 hdr = realloc(ptr, size);
101 __free_hook = tr_freehook;
102 __malloc_hook = tr_mallochook;
103 __realloc_hook = tr_reallochook;
104 if (hdr == NULL) {
105 fprintf (mallstream, "! %08x %x\n", ptr, size); /* Failed realloc */
106 } else {
107 fprintf (mallstream, "< %08x\n> %08x %x\n", ptr, hdr, size);
108 }
109
110 if (hdr == mallwatch)
111 tr_break();
112
113 return hdr;
114 }
115
116 void
117 mtrace()
118 {
119 char *mallfile;
120
121 mallfile = getenv (mallenv);
122 if (mallfile) {
123 mallstream = fopen (mallfile, "w");
124 if (mallstream) {
125 /* Be sure it doesn't malloc its buffer! */
126 setbuf (mallstream, mallbuf);
127 fprintf (mallstream, "= Start\n");
128 old_free_hook = __free_hook;
129 __free_hook = tr_freehook;
130 old_malloc_hook = __malloc_hook;
131 __malloc_hook = tr_mallochook;
132 old_realloc_hook = __realloc_hook;
133 __realloc_hook = tr_reallochook;
134 }
135 }
136 }