heaptrc.tex 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. It is system independent, and works on all supported systems.
  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{verbatim}
  78. type
  79. FillExtraInfoType = procedure(p : pointer);
  80. \end{verbatim}
  81. The following typed constants allow to fine-tune the standard dump of the
  82. memory usage by \seep{DumpHeap}:
  83. \begin{verbatim}
  84. const
  85. tracesize = 8;
  86. quicktrace : boolean = true;
  87. HaltOnError : boolean = true;
  88. keepreleased : boolean = false;
  89. add_tail : boolean = true;
  90. usecrc : boolean = true
  91. \end{verbatim}
  92. \var{Tracesize} specifies how many levels of calls are displayed of the
  93. call stack during the memory dump. If you specify \var{keepreleased:=True}
  94. then half the \var{TraceSize} is reserved for the \var{GetMem} call stack,
  95. and the other half is reserved for the \var{FreeMem} call stack.
  96. For example, the default value of 8 will cause eight levels of call frames
  97. to be dumped for the getmem call if \var{keepreleased} is \var{False}. If
  98. \var{KeepReleased} is true, then 4 levels of call frames will be dumped for
  99. the \var{GetMem} call and 4 frames wil be dumped for the \var{FreeMem} call.
  100. If you want to change this value, you must recode the \file{heaptrc} unit.
  101. \var{Quicktrace} determines whether the memory manager checks whether a
  102. block that is about to be released is allocated correctly. This is a rather
  103. time consuming search, and slows program execution significantly, so by
  104. default it is set to \var{False}.
  105. If \var{HaltOnError} is set to \var{True} then an illegal call to
  106. \var{FreeMem} will cause the memory manager to execute a \var{halt(1)}
  107. instruction, causing a memory dump. By Default it is set to \var{True}.
  108. If \var{keepreleased} is set to true, then a list of freed memory
  109. blocks is kept. This is useful if you suspect that the same memory block is
  110. released twice. However, this option is very memory intensive, so use it
  111. sparingly, and only when it's really necessary.
  112. If \var{add\_tail} is \var{True} (the default) then a check is also
  113. performed on the memory location just behind the allocated memory.
  114. If \var{usecrc} is \var{True} (the default) then a crc check is performed
  115. on locations before and after the allocated memory. This is useful to
  116. detect memory overwrites.
  117. \section{Functions and procedures}
  118. \begin{procedure}{DumpHeap}
  119. \Declaration
  120. procedure DumpHeap;
  121. \Description
  122. \var{DumpHeap} dumps to standard output a summary of memory usage.
  123. It is called automatically by the heaptrc unit when your program exits
  124. (by instaling an exit procedure), but it can be called at any time
  125. \Errors
  126. None.
  127. \SeeAlso
  128. \seep{MarkHeap}
  129. \end{procedure}
  130. \begin{procedure}{MarkHeap}
  131. \Declaration
  132. procedure MarkHeap;
  133. \Description
  134. \var{MarkHeap} marks all memory blocks with a special signature.
  135. You can use this if you think that you corruped the memory.
  136. \Errors
  137. None.
  138. \SeeAlso
  139. \seep{DumpHeap}
  140. \end{procedure}
  141. \begin{procedure}{SetExtraInfo}
  142. \Declaration
  143. procedure SetExtraInfo( size : longint;func : FillExtraInfoType);
  144. \Description
  145. You can use \var{SetExtraInfo} to store extra info in the blocks that
  146. the heaptrc unit reserves when tracing getmem calls. \var{Size} indicates the
  147. size (in bytes) that the trace mechanism should reserve for your extra
  148. information. For each call to \var{getmem}, \var{func} will be called,
  149. and passed a pointer to the memory reserved.
  150. When dumping the memory summary, the extra info is shown as Longint values.
  151. \Errors
  152. You can only call \var{SetExtraInfo} if no memroy has been allocated
  153. yet. If memory was already allocated prior to the call to
  154. \var{SetExtraInfo}, then an error will be displayed on standard error
  155. output, and a \seep{DumpHeap} is executed.
  156. \SeeAlso
  157. \seep{DumpHeap},\seep{SetHeapTraceOutput}
  158. \end{procedure}
  159. \latex{\lstinputlisting{heapex/setinfo.pp}}
  160. \html{\input{heapex/setinfo.tex}}
  161. \begin{procedure}{SetHeapTraceOutput}
  162. \Declaration
  163. Procedure SetHeapTraceOutput(const name : string);
  164. \Description
  165. \var{SetHeapTraceOutput} sets the filename into which heap trace info
  166. will be written. By default information is written to standard output,
  167. this function allows you to redirect the information to a file with
  168. full filename \var{name}.
  169. \Errors
  170. If the file cannot be written to, errors will occur when writing the
  171. trace.
  172. \SeeAlso
  173. \seep{SetExtraInfo}
  174. \end{procedure}
  175. %
  176. % $Log$
  177. % Revision 1.1 2000-07-13 09:10:04 michael
  178. % + Initial import
  179. %
  180. % Revision 1.6 2000/07/11 18:07:26 peter
  181. % * fixes for latex2html 0.99b2
  182. %
  183. % Revision 1.5 2000/05/16 21:07:55 michael
  184. % + Implemented large part of TODO list. Too much to denote
  185. %
  186. % Revision 1.4 2000/02/07 11:21:06 michael
  187. % + Documented heaptrc and lineinfo
  188. %
  189. % Revision 1.3 1999/06/25 22:12:16 michael
  190. % + Update to version 0.19 of listings package
  191. %
  192. % Revision 1.2 1998/12/15 23:50:52 michael
  193. % * Some updates
  194. %
  195. % Revision 1.1 1998/12/14 23:17:02 michael
  196. % + INitial implementation
  197. %
  198. %