]> Git Repo - binutils.git/blob - gdb/mtrace.c
Change GDB over to GNU General Public License version 2.
[binutils.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 2 of the License, or
9 (at your option) 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 #include <stdio.h>
21 #include "ansidecl.h"
22
23 /* size_t may be defined in the system-supplied stdio.h.  */
24 /* So just kludge it.  */
25 #define size_t unsigned int
26 #define ptrdiff_t int
27 #define __ONEFILE
28
29 #include <stdlib.h>
30 #include "gmalloc.h"
31
32 extern char *getenv();
33
34 FILE *mallstream;
35 char mallenv[] = "MALLOC_TRACE";
36 static char mallbuf[BUFSIZ];            /* Buffer for the output */
37
38 /* Address to breakpoint on accesses to... */
39 PTR mallwatch;
40
41 /* Old hook values.  */
42 static void EXFUN((*old_free_hook), (PTR ptr));
43 static PTR EXFUN((*old_malloc_hook), (size_t size));
44 static PTR EXFUN((*old_realloc_hook), (PTR ptr, size_t size));
45
46 /* This function is called when the block being alloc'd, realloc'd, or
47    freed has an address matching the variable "mallwatch".  In a debugger,
48    set "mallwatch" to the address of interest, then put a breakpoint on
49    tr_break.  */
50
51 void
52 tr_break()
53 {
54   ;
55 }
56
57 static void
58 DEFUN(tr_freehook, (ptr), PTR ptr)
59 {
60   fprintf(mallstream, "- %08x\n", ptr); /* Be sure to print it first */
61   if (ptr == mallwatch)
62     tr_break();
63   __free_hook = old_free_hook;
64   free(ptr);
65   __free_hook = tr_freehook;
66 }
67
68 static PTR
69 DEFUN(tr_mallochook, (size), size_t size)
70 {
71   PTR hdr;
72
73   __malloc_hook = old_malloc_hook;
74   hdr = malloc(size);
75   __malloc_hook = tr_mallochook;
76
77   /* We could be printing a NULL here; that's OK */
78   fprintf (mallstream, "+ %08x %x\n", hdr, size);
79
80   if (hdr == mallwatch)
81     tr_break();
82
83   return hdr;
84 }
85
86 static PTR
87 DEFUN(tr_reallochook, (ptr, size), PTR ptr AND size_t size)
88 {
89   PTR hdr;
90
91   if (ptr == mallwatch)
92     tr_break();
93
94   __free_hook = old_free_hook;
95   __malloc_hook = old_malloc_hook;
96   __realloc_hook = old_realloc_hook;
97   hdr = realloc(ptr, size);
98   __free_hook = tr_freehook;
99   __malloc_hook = tr_mallochook;
100   __realloc_hook = tr_reallochook;
101   if (hdr == NULL) {
102     fprintf (mallstream, "! %08x %x\n", ptr, size);     /* Failed realloc */
103   } else {
104     fprintf (mallstream, "< %08x\n> %08x %x\n", ptr, hdr, size);
105   }
106
107   if (hdr == mallwatch)
108     tr_break();
109
110   return hdr;
111 }
112
113 void
114 mtrace()
115 {
116   char *mallfile;
117
118   mallfile = getenv (mallenv);
119   if (mallfile) {
120     mallstream = fopen (mallfile, "w");
121     if (mallstream) {
122       /* Be sure it doesn't malloc its buffer! */
123       setbuf (mallstream, mallbuf);
124       fprintf (mallstream, "= Start\n");
125       old_free_hook = __free_hook;
126       __free_hook = tr_freehook;
127       old_malloc_hook = __malloc_hook;
128       __malloc_hook = tr_mallochook;
129       old_realloc_hook = __realloc_hook;
130       __realloc_hook = tr_reallochook;
131     }
132   }
133 }
This page took 0.026654 seconds and 4 git commands to generate.