fix output location of common symbols
[deliverable/binutils-gdb.git] / mmalloc / mmtrace.c
CommitLineData
e33e766a
FF
1/* More debugging hooks for `mmalloc'.
2 Copyright 1991, 1992 Free Software Foundation
3
4 Written April 2, 1991 by John Gilmore of Cygnus Support
5 Based on mcheck.c by Mike Haertel.
6 Modified Mar 1992 by Fred Fish. (fnf@cygnus.com)
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22#include <stdio.h>
23#include "mmalloc.h"
24
25#ifndef __GNU_LIBRARY__
26extern char *getenv ();
27#endif
28
29static FILE *mallstream;
30
31#if 0 /* FIXME: Disabled for now. */
32static char mallenv[] = "MALLOC_TRACE";
33static char mallbuf[BUFSIZ]; /* Buffer for the output. */
34#endif
35
36/* Address to breakpoint on accesses to... */
37static PTR mallwatch;
38
39/* Old hook values. */
40
41static void (*old_mfree_hook) PARAMS ((PTR, PTR));
42static PTR (*old_mmalloc_hook) PARAMS ((PTR, size_t));
43static PTR (*old_mrealloc_hook) PARAMS ((PTR, PTR, size_t));
44
45/* This function is called when the block being alloc'd, realloc'd, or
46 freed has an address matching the variable "mallwatch". In a debugger,
47 set "mallwatch" to the address of interest, then put a breakpoint on
48 tr_break. */
49
50static void
51tr_break ()
52{
53}
54
55static void
56tr_freehook (md, ptr)
57 PTR md;
58 PTR ptr;
59{
60 struct mdesc *mdp;
61
62 mdp = MD_TO_MDP (md);
63 /* Be sure to print it first. */
64 (void) fprintf (mallstream, "- %08x\n", (unsigned int) ptr);
65 if (ptr == mallwatch)
66 tr_break ();
67 mdp -> mfree_hook = old_mfree_hook;
68 mfree (md, ptr);
69 mdp -> mfree_hook = tr_freehook;
70}
71
72static PTR
73tr_mallochook (md, size)
74 PTR md;
75 size_t size;
76{
77 PTR hdr;
78 struct mdesc *mdp;
79
80 mdp = MD_TO_MDP (md);
81 mdp -> mmalloc_hook = old_mmalloc_hook;
82 hdr = (PTR) mmalloc (md, size);
83 mdp -> mmalloc_hook = tr_mallochook;
84
85 /* We could be printing a NULL here; that's OK. */
86 (void) fprintf (mallstream, "+ %08x %x\n", (unsigned int) hdr, size);
87
88 if (hdr == mallwatch)
89 tr_break ();
90
91 return (hdr);
92}
93
94static PTR
95tr_reallochook (md, ptr, size)
96 PTR md;
97 PTR ptr;
98 size_t size;
99{
100 PTR hdr;
101 struct mdesc *mdp;
102
103 mdp = MD_TO_MDP (md);
104
105 if (ptr == mallwatch)
106 tr_break ();
107
108 mdp -> mfree_hook = old_mfree_hook;
109 mdp -> mmalloc_hook = old_mmalloc_hook;
110 mdp -> mrealloc_hook = old_mrealloc_hook;
111 hdr = (PTR) mrealloc (md, ptr, size);
112 mdp -> mfree_hook = tr_freehook;
113 mdp -> mmalloc_hook = tr_mallochook;
114 mdp -> mrealloc_hook = tr_reallochook;
115 if (hdr == NULL)
116 /* Failed realloc. */
117 (void) fprintf (mallstream, "! %08x %x\n", (unsigned int) ptr, size);
118 else
119 (void) fprintf (mallstream, "< %08x\n> %08x %x\n", (unsigned int) ptr,
120 (unsigned int) hdr, size);
121
122 if (hdr == mallwatch)
123 tr_break ();
124
125 return hdr;
126}
127
128/* We enable tracing if either the environment variable MALLOC_TRACE
129 is set, or if the variable mallwatch has been patched to an address
130 that the debugging user wants us to stop on. When patching mallwatch,
131 don't forget to set a breakpoint on tr_break! */
132
133int
134mmtrace ()
135{
136#if 0 /* FIXME! This is disabled for now until we figure out how to
137 maintain a stack of hooks per heap, since we might have other
138 hooks (such as set by mmcheck) active also. */
139 char *mallfile;
140
141 mallfile = getenv (mallenv);
142 if (mallfile != NULL || mallwatch != NULL)
143 {
144 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
145 if (mallstream != NULL)
146 {
147 /* Be sure it doesn't mmalloc its buffer! */
148 setbuf (mallstream, mallbuf);
149 (void) fprintf (mallstream, "= Start\n");
150 old_mfree_hook = mdp -> mfree_hook;
151 mdp -> mfree_hook = tr_freehook;
152 old_mmalloc_hook = mdp -> mmalloc_hook;
153 mdp -> mmalloc_hook = tr_mallochook;
154 old_mrealloc_hook = mdp -> mrealloc_hook;
155 mdp -> mrealloc_hook = tr_reallochook;
156 }
157 }
158
159#endif /* 0 */
160
161 return (1);
162}
163
This page took 0.141831 seconds and 4 git commands to generate.