heaptrc.tex 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1998, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation 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 GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \chapter{The HEAPTRC unit.}
  22. This chapter describes the HEAPTRC unit for \fpc. It was written by
  23. Pierre Muller.
  24. \section{Purpose}
  25. The HEAPTRC unit can be used to debug your memory allocation/deallocation.
  26. It keeps track of the calls to getmem/freemem, and, implicitly, of
  27. New/Dispose statements.
  28. When the program exits, or when you request it explicitly.
  29. It displays the total memory used, and then dumps a list of blocks that
  30. were allocated but not freed. It also displays where the memory was
  31. allocated.
  32. If there are any inconsistencies, such as memory blocks being allocated
  33. or freed twice, or a memory block that is released but with wrong size,
  34. this will be displayed also.
  35. The information that is stored/displayed can be customized using
  36. some constants.
  37. \section{Usage}
  38. All that you need to do is to include \file{heaptrc} in the uses clause
  39. of your program. Make sure that it is the first unit in the clause,
  40. otherwise memory allocated in initialization code of units that precede the
  41. heaptrc unit will not be accounted for, causing an incorrect memory usage
  42. report.
  43. If you use the \var{-gh} switch, the compiler will insert the unit by itself,
  44. so you don't have to include it in your uses clause.
  45. The following example shows how to use the heaptrc unit.
  46. \latex{\lstinputlisting{heapex/heapex.pp}}
  47. \html{\input{heapex/heapex.tex}}
  48. This is the memory dump shown when running this program:
  49. \begin{verbatim}
  50. Marked memory at 0040FA50 invalid
  51. Wrong size : 128 allocated 64 freed
  52. 0x00408708
  53. 0x0040CB49
  54. 0x0040C481
  55. Call trace for block 0x0040FA50 size 128
  56. 0x0040CB3D
  57. 0x0040C481
  58. \end{verbatim}
  59. If you use the \file{lineinfo} unit (or use the \var{-gl} switch) as well,
  60. then \file{heaptrc} will also give you the filenames and line-numbers of
  61. the procedures in the backtrace:
  62. \begin{verbatim}
  63. Marked memory at 00410DA0 invalid
  64. Wrong size : 128 allocated 64 freed
  65. 0x004094B8
  66. 0x0040D8F9 main, line 25 of heapex.pp
  67. 0x0040D231
  68. Call trace for block 0x00410DA0 size 128
  69. 0x0040D8ED main, line 23 of heapex.pp
  70. 0x0040D231
  71. \end{verbatim}
  72. If lines without filename/line-number occur, this means there is a unit which
  73. has no debug info included.
  74. \section{Constants, Types and variables}
  75. The \var{FillExtraInfoType} is a procedural type used in the
  76. \seep{SetExtraInfo} call.
  77. \begin{lstlisting}{}
  78. type
  79. FillExtraInfoType = procedure(p : pointer);
  80. \end{lstlisting}{}
  81. The following typed constants allow to fine-tune the standard dump of the
  82. memory usage by \seep{DumpHeap}:
  83. \begin{lstlisting}{}
  84. const
  85. tracesize = 8;
  86. quicktrace : boolean = true;
  87. HaltOnError : boolean = true;
  88. keepreleased : boolean = false;
  89. \end{lstlisting}{}
  90. \var{Tracesize} specifies how many levels of calls are displayed of the
  91. call stack during the memory dump. If you specify \var{keepreleased:=True}
  92. then half the \var{TraceSize} is reserved for the \var{GetMem} call stack,
  93. and the other half is reserved for the \var{FreeMem} call stack.
  94. For example, the default value of 8 will cause eight levels of call frames
  95. to be dumped for the getmem call if \var{keepreleased} is \var{False}. If
  96. \var{KeepReleased} is true, then 4 levels of call frames will be dumped for
  97. the \var{GetMem} call and 4 frames wil be dumped for the \var{FreeMem} call.
  98. If you want to change this value, you must recode the \file{heaptrc} unit.
  99. \var{Quicktrace} determines whether the memory manager checks whether a
  100. block that is about to be released is allocated correctly. This is a rather
  101. time consuming search, and slows program execution significantly, so by
  102. default it is set to \var{False}.
  103. If \var{HaltOnError} is set to \var{True} then an illegal call to
  104. \var{FreeMem} will cause the memory manager to execute a \var{halt(1)}
  105. instruction, causing a memory dump. By Default it is set to \var{True}.
  106. If \var{keepreleased} is set to true, then a list of freed memory
  107. blocks is kept. This is useful if you suspect that the same memory block is
  108. released twice. However, this option is very memory intensive, so use it
  109. sparingly, and only when it's really necessary.
  110. \section{Functions and procedures}
  111. \begin{procedure}{DumpHeap}
  112. \Declaration
  113. procedure DumpHeap;
  114. \Description
  115. \var{DumpHeap} dumps to standard output a summary of memory usage.
  116. It is called automatically by the heaptrc unit when your program exits
  117. (by instaling an exit procedure), but it can be called at any time
  118. \Errors
  119. None.
  120. \SeeAlso
  121. \seep{MarkHeap}
  122. \end{procedure}
  123. \begin{procedure}{MarkHeap}
  124. \Declaration
  125. procedure MarkHeap;
  126. \Description
  127. \var{MarkHeap} marks all memory blocks with a special signature.
  128. You can use this if you think that you corruped the memory.
  129. \Errors
  130. None.
  131. \SeeAlso
  132. \seep{DumpHeap}
  133. \end{procedure}
  134. \begin{procedure}{SetExtraInfo}
  135. \Declaration
  136. procedure SetExtraInfo( size : longint;func : FillExtraInfoType);
  137. \Description
  138. You can use \var{SetExtraInfo} to store extra info in the blocks that
  139. the heaptrc unit reserves when tracing getmem calls. \var{Size} indicates the
  140. size (in bytes) that the trace mechanism should reserve for your extra
  141. information. For each call to \var{getmem}, \var{func} will be called,
  142. and passed a pointer to the memory reserved.
  143. When dumping the memory summary, the extra info is shown as Longint values.
  144. \Errors
  145. You can only call \var{SetExtraInfo} if no memroy has been allocated
  146. yet. If memory was already allocated prior to the call to
  147. \var{SetExtraInfo}, then an error will be displayed on standard error
  148. output, and a \seep{DumpHeap} is executed.
  149. \SeeAlso
  150. \seep{DumpHeap}
  151. \end{procedure}
  152. \latex{\lstinputlisting{heapex/setinfo.pp}}
  153. \html{\input{heapex/setinfo.tex}}
  154. %
  155. % $Log$
  156. % Revision 1.4 2000-02-07 11:21:06 michael
  157. % + Documented heaptrc and lineinfo
  158. %
  159. % Revision 1.3 1999/06/25 22:12:16 michael
  160. % + Update to version 0.19 of listings package
  161. %
  162. % Revision 1.2 1998/12/15 23:50:52 michael
  163. % * Some updates
  164. %
  165. % Revision 1.1 1998/12/14 23:17:02 michael
  166. % + INitial implementation
  167. %
  168. %