home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / mmalloc / mmtrace.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  4KB  |  167 lines

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