1 /* More debugging hooks for `mmalloc'.
2 Copyright 1991, 1992, 1994 Free Software Foundation
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) */
8 /* Copyright (c) 2010. The SimGrid Team.
9 * All rights reserved. */
11 /* This program is free software; you can redistribute it and/or modify it
12 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "mmprivate.h"
17 static void tr_break (void);
18 static void tr_freehook (void*md, void*ptr);
19 static void* tr_mallochook (void* md, size_t size);
20 static void* tr_reallochook (void* md, void* ptr, size_t size);
22 #ifndef __GNU_LIBRARY__
23 extern char *getenv ();
26 static FILE *mallstream;
28 #if 0 /* FIXME: Disabled for now. */
29 static char mallenv[] = "MALLOC_TRACE";
30 static char mallbuf[BUFSIZ]; /* Buffer for the output. */
33 /* Address to breakpoint on accesses to... */
34 static void* mallwatch;
36 /* Old hook values. */
38 static void (*old_mfree_hook) (void* md, void* ptr);
39 static void* (*old_mmalloc_hook) (void* md, size_t size);
40 static void* (*old_mrealloc_hook) (void* md, void* ptr, size_t size);
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
53 tr_freehook (void *md, void *ptr)
58 /* Be sure to print it first. */
59 fprintf (mallstream, "- %08lx\n", (unsigned long) ptr);
62 mdp -> mfree_hook = old_mfree_hook;
64 mdp -> mfree_hook = tr_freehook;
68 tr_mallochook (void* md, size_t size)
74 mdp -> mmalloc_hook = old_mmalloc_hook;
75 hdr = (void*) mmalloc (md, size);
76 mdp -> mmalloc_hook = tr_mallochook;
78 /* We could be printing a NULL here; that's OK. */
79 fprintf (mallstream, "+ %p 0x%lx\n", hdr, (unsigned long)size);
88 tr_reallochook (void *md, void *ptr, size_t size)
98 mdp -> mfree_hook = old_mfree_hook;
99 mdp -> mmalloc_hook = old_mmalloc_hook;
100 mdp -> mrealloc_hook = old_mrealloc_hook;
101 hdr = (void*) mrealloc (md, ptr, size);
102 mdp -> mfree_hook = tr_freehook;
103 mdp -> mmalloc_hook = tr_mallochook;
104 mdp -> mrealloc_hook = tr_reallochook;
106 /* Failed realloc. */
107 fprintf (mallstream, "! %p 0x%lx\n", ptr, (unsigned long) size);
109 fprintf (mallstream, "< %p\n> %p 0x%lx\n", ptr,
110 hdr, (unsigned long) size);
112 if (hdr == mallwatch)
118 /* We enable tracing if either the environment variable MALLOC_TRACE
119 is set, or if the variable mallwatch has been patched to an address
120 that the debugging user wants us to stop on. When patching mallwatch,
121 don't forget to set a breakpoint on tr_break! */
126 #if 0 /* FIXME! This is disabled for now until we figure out how to
127 maintain a stack of hooks per heap, since we might have other
128 hooks (such as set by mmcheck/mmcheckf) active also. */
131 mallfile = getenv (mallenv);
132 if (mallfile != NULL || mallwatch != NULL)
134 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
135 if (mallstream != NULL)
137 /* Be sure it doesn't mmalloc its buffer! */
138 setbuf (mallstream, mallbuf);
139 fprintf (mallstream, "= Start\n");
140 old_mfree_hook = mdp -> mfree_hook;
141 mdp -> mfree_hook = tr_freehook;
142 old_mmalloc_hook = mdp -> mmalloc_hook;
143 mdp -> mmalloc_hook = tr_mallochook;
144 old_mrealloc_hook = mdp -> mrealloc_hook;
145 mdp -> mrealloc_hook = tr_reallochook;