mtrace.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* More debugging hooks for `malloc'.
  2. Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3. Written April 2, 1991 by John Gilmore of Cygnus Support.
  4. Based on mcheck.c by Mike Haertel.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; see the file COPYING.LIB. If
  15. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16. Cambridge, MA 02139, USA.
  17. The author may be reached (Email) at the address mike@ai.mit.edu,
  18. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  19. #ifndef _MALLOC_INTERNAL
  20. #define _MALLOC_INTERNAL
  21. #include <malloc.h>
  22. #endif
  23. #include <stdio.h>
  24. #ifndef __GNU_LIBRARY__
  25. extern char *getenv ();
  26. #else
  27. #include <stdlib.h>
  28. #endif
  29. static FILE *mallstream;
  30. static char mallenv[]= "MALLOC_TRACE";
  31. static char mallbuf[BUFSIZ]; /* Buffer for the output. */
  32. /* Address to breakpoint on accesses to... */
  33. __ptr_t mallwatch;
  34. /* File name and line number information, for callers that had
  35. the foresight to call through a macro. */
  36. char *_mtrace_file;
  37. int _mtrace_line;
  38. /* Old hook values. */
  39. static void (*tr_old_free_hook) __P ((__ptr_t ptr));
  40. static __ptr_t (*tr_old_malloc_hook) __P ((__malloc_size_t size));
  41. static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr, __malloc_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
  45. tr_break. */
  46. void tr_break __P ((void));
  47. void
  48. tr_break ()
  49. {
  50. }
  51. static void tr_where __P ((void));
  52. static void
  53. tr_where ()
  54. {
  55. if (_mtrace_file)
  56. {
  57. fprintf (mallstream, "@ %s:%d ", _mtrace_file, _mtrace_line);
  58. _mtrace_file = NULL;
  59. }
  60. }
  61. static void tr_freehook __P ((__ptr_t));
  62. static void
  63. tr_freehook (ptr)
  64. __ptr_t ptr;
  65. {
  66. tr_where ();
  67. fprintf (mallstream, "- %p\n", ptr); /* Be sure to print it first. */
  68. if (ptr == mallwatch)
  69. tr_break ();
  70. __free_hook = tr_old_free_hook;
  71. free (ptr);
  72. __free_hook = tr_freehook;
  73. }
  74. static __ptr_t tr_mallochook __P ((__malloc_size_t));
  75. static __ptr_t
  76. tr_mallochook (size)
  77. __malloc_size_t size;
  78. {
  79. __ptr_t hdr;
  80. __malloc_hook = tr_old_malloc_hook;
  81. hdr = (__ptr_t) malloc (size);
  82. __malloc_hook = tr_mallochook;
  83. tr_where ();
  84. /* We could be printing a NULL here; that's OK. */
  85. fprintf (mallstream, "+ %p %lx\n", hdr, (unsigned long)size);
  86. if (hdr == mallwatch)
  87. tr_break ();
  88. return hdr;
  89. }
  90. static __ptr_t tr_reallochook __P ((__ptr_t, __malloc_size_t));
  91. static __ptr_t
  92. tr_reallochook (ptr, size)
  93. __ptr_t ptr;
  94. __malloc_size_t size;
  95. {
  96. __ptr_t hdr;
  97. if (ptr == mallwatch)
  98. tr_break ();
  99. __free_hook = tr_old_free_hook;
  100. __malloc_hook = tr_old_malloc_hook;
  101. __realloc_hook = tr_old_realloc_hook;
  102. hdr = (__ptr_t) realloc (ptr, size);
  103. __free_hook = tr_freehook;
  104. __malloc_hook = tr_mallochook;
  105. __realloc_hook = tr_reallochook;
  106. tr_where ();
  107. if (hdr == NULL)
  108. /* Failed realloc. */
  109. fprintf (mallstream, "! %p %lx\n", ptr, (unsigned long)size);
  110. else
  111. fprintf (mallstream, "< %p\n> %p %lx\n", ptr, hdr, (unsigned long)size);
  112. if (hdr == mallwatch)
  113. tr_break ();
  114. return hdr;
  115. }
  116. /* We enable tracing if either the environment variable MALLOC_TRACE
  117. is set, or if the variable mallwatch has been patched to an address
  118. that the debugging user wants us to stop on. When patching mallwatch,
  119. don't forget to set a breakpoint on tr_break! */
  120. void
  121. mtrace ()
  122. {
  123. char *mallfile;
  124. /* Don't panic if we're called more than once. */
  125. if (mallstream != NULL)
  126. return;
  127. mallfile = getenv (mallenv);
  128. if (mallfile != NULL || mallwatch != NULL)
  129. {
  130. mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
  131. if (mallstream != NULL)
  132. {
  133. /* Be sure it doesn't malloc its buffer! */
  134. setbuf (mallstream, mallbuf);
  135. fprintf (mallstream, "= Start\n");
  136. tr_old_free_hook = __free_hook;
  137. __free_hook = tr_freehook;
  138. tr_old_malloc_hook = __malloc_hook;
  139. __malloc_hook = tr_mallochook;
  140. tr_old_realloc_hook = __realloc_hook;
  141. __realloc_hook = tr_reallochook;
  142. }
  143. }
  144. }
  145. void
  146. muntrace ()
  147. {
  148. if (mallstream == NULL)
  149. return;
  150. fprintf (mallstream, "= End\n");
  151. fclose (mallstream);
  152. mallstream = NULL;
  153. __free_hook = tr_old_free_hook;
  154. __malloc_hook = tr_old_malloc_hook;
  155. __realloc_hook = tr_old_realloc_hook;
  156. }