heaptrc.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?xml version="1.0" encoding="ISO8859-1"?>
  2. <fpdoc-descriptions>
  3. <!--
  4. $Id$
  5. This file is part of the FPC documentation.
  6. Copyright (C) 1997, by Michael Van Canneyt
  7. The FPC documentation is free text; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  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. You should have received a copy of the GNU Library General Public
  16. License along with the FPC documentation; see the file COPYING.LIB. If not,
  17. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. Boston, MA 02111-1307, USA.
  19. -->
  20. <package name="rtl">
  21. <module name="heaptrc">
  22. <short>Heap debugging functionality.</short>
  23. <!-- \FPCexampledir{heapex} -->
  24. <descr>
  25. <p>
  26. This document describes the HEAPTRC unit for Free Pascal. It was written by
  27. Pierre Muller. It is system independent, and works on all supported systems.
  28. </p>
  29. <p>
  30. The HEAPTRC unit can be used to debug your memory allocation/deallocation.
  31. It keeps track of the calls to getmem/freemem, and, implicitly, of
  32. New/Dispose statements.
  33. </p>
  34. <p>
  35. When the program exits, or when you request it explicitly.
  36. It displays the total memory used, and then dumps a list of blocks that
  37. were allocated but not freed. It also displays where the memory was
  38. allocated.
  39. </p>
  40. <p>
  41. If there are any inconsistencies, such as memory blocks being allocated
  42. or freed twice, or a memory block that is released but with wrong size,
  43. this will be displayed also.
  44. </p>
  45. <p>
  46. The information that is stored/displayed can be customized using
  47. some constants.
  48. </p>
  49. </descr>
  50. <topic name="usage">
  51. <short>HeapTrc Usage</short>
  52. <descr>
  53. <p>
  54. All that you need to do is to include <file>heaptrc</file> in the uses clause
  55. of your program. Make sure that it is the first unit in the clause,
  56. otherwise memory allocated in initialization code of units that precede the
  57. heaptrc unit will not be accounted for, causing an incorrect memory usage
  58. report.
  59. </p>
  60. <p>
  61. If you use the <var>-gh</var> switch, the compiler will insert the unit by itself,
  62. so you don't have to include it in your uses clause.
  63. </p>
  64. <p>
  65. The below example shows how to use the heaptrc unit.
  66. </p>
  67. <p>
  68. This is the memory dump shown when running this program in a standard way:
  69. </p>
  70. <code>
  71. Marked memory at 0040FA50 invalid
  72. Wrong size : 128 allocated 64 freed
  73. 0x00408708
  74. 0x0040CB49
  75. 0x0040C481
  76. Call trace for block 0x0040FA50 size 128
  77. 0x0040CB3D
  78. 0x0040C481
  79. </code>
  80. <p>
  81. If you use the <file>lineinfo</file> unit (or use the <var>-gl</var> switch) as well,
  82. then <file>heaptrc</file> will also give you the filenames and line-numbers of
  83. the procedures in the backtrace:
  84. </p>
  85. <code>
  86. Marked memory at 00410DA0 invalid
  87. Wrong size : 128 allocated 64 freed
  88. 0x004094B8
  89. 0x0040D8F9 main, line 25 of heapex.pp
  90. 0x0040D231
  91. Call trace for block 0x00410DA0 size 128
  92. 0x0040D8ED main, line 23 of heapex.pp
  93. 0x0040D231
  94. </code>
  95. <p>
  96. If lines without filename/line-number occur, this means there is a unit which
  97. has no debug info included.
  98. </p>
  99. </descr>
  100. <example file="heapex/heapex"/>
  101. </topic>
  102. <topic name="environment">
  103. <short>Controlling HeapTrc with environment variables</short>
  104. <descr>
  105. <p>The <file>HeapTrc</file> unit can be controlled with the <var>HEAPTRC</var>
  106. environment variable. The contents of this variable controls the initial setting
  107. of some constants in the unit. <var>HEAPTRC</var> consists of one or more of the
  108. following strings, separated by spaces:
  109. </p>
  110. <dl>
  111. <dt>keepreleased</dt>
  112. <dd>If this string occurs, then the <link id="KeepReleased"/> variable is set
  113. to <var>True</var></dd>
  114. <dt>disabled</dt>
  115. <dd>If this string occurs, then the <link id="UseHeapTrace"/> variable is set
  116. to <var>False</var> and the heap trace is disabled. It does not make sense to
  117. combine this value with other values.</dd>
  118. <dt>nohalt</dt>
  119. <dd>If this string occurs, then the <link id="HaltOnError"/> variable is set
  120. to <var>False</var>, so the program continues executing even in case of a
  121. heap error.</dd>
  122. <dt>log=filename</dt>
  123. <dd>If this string occurs, then the output of heaptrc is sent
  124. to the specified <var>Filename</var>. (see also <link id="SetHeapTraceOutput"/>)
  125. </dd>
  126. </dl>
  127. <p>
  128. The following are valid values for the HEAPTRC variable:
  129. </p>
  130. <code>
  131. HEAPTRC=disabled
  132. HEAPTRC="keepreleased log=heap.log"
  133. HEAPTRC="log=myheap.log nohalt"
  134. </code>
  135. <p>
  136. Note that these strings are case sensitive, and the name of the variable too.
  137. </p>
  138. </descr>
  139. </topic>
  140. <element name="TFillExtraInfoProc">
  141. <short>The <var>FillExtraInfoProc</var> is a procedural type used in the <link id="SetHeapExtraInfo"/> call.</short>
  142. <descr>
  143. The <var>TFillExtraInfoProc</var> is a procedural type used in the
  144. <link id="SetHeapExtraInfo"/> call to fill a memory location with
  145. extra data for displaying.
  146. </descr>
  147. <seealso>
  148. <link id="SetHeapExtraInfo"/>
  149. <link id="TDisplayExtraInfoProc"/>
  150. </seealso>
  151. </element>
  152. <element name="TDisplayExtraInfoProc">
  153. <short>The <var>TDisplayExtraInfoProc</var> is a procedural type used in the <link id="SetHeapExtraInfo"/> call.</short>
  154. <descr>
  155. The <var>TDisplayExtraInfoType</var> is a procedural type used in the
  156. <link id="SetHeapExtraInfo"/> call to display a memory location which was
  157. previously filled with <link id="TFillExtraInfoProc"/>
  158. </descr>
  159. <seealso>
  160. <link id="SetHeapExtraInfo"/>
  161. <link id="TFillExtraInfoProc"/>
  162. </seealso>
  163. </element>
  164. <element name="TraceSize">
  165. <short>Specify size of callback trace</short>
  166. <descr>
  167. <var>Tracesize</var> specifies how many levels of calls are displayed of the
  168. call stack during the memory dump. If you specify <var>keepreleased:=True</var>
  169. then half the <var>TraceSize</var> is reserved for the <var>GetMem</var> call stack,
  170. and the other half is reserved for the <var>FreeMem</var> call stack.
  171. For example, the default value of 8 will cause eight levels of call frames
  172. to be dumped for the getmem call if <var>keepreleased</var> is <var>False</var>. If
  173. <var>KeepReleased</var> is true, then 4 levels of call frames will be dumped for
  174. the <var>GetMem</var> call and 4 frames wil be dumped for the <var>FreeMem</var> call.
  175. If you want to change this value, you must recode the <file>heaptrc</file> unit.
  176. </descr>
  177. </element>
  178. <element name="QuickTrace">
  179. <short>Do quick trace or extensive trace</short>
  180. <descr>
  181. <var>Quicktrace</var> determines whether the memory manager checks whether a
  182. block that is about to be released is allocated correctly. This is a rather
  183. time consuming search, and slows program execution significantly, so by
  184. default it is set to <var>True</var>.
  185. </descr>
  186. </element>
  187. <element name="useHeapTrace">
  188. <short>If set to <var>false</var>, the heap trace will be disabled. </short>
  189. <descr>
  190. This variable must be set at program startup, through the help of an environment variable.
  191. </descr>
  192. <seealso>
  193. <link id="environment"/>
  194. </seealso>
  195. </element>
  196. <element name="HaltOnError">
  197. <short>Halt program on memory fault</short>
  198. <descr>
  199. If <var>HaltOnError</var> is set to <var>True</var> then an illegal call to
  200. <var>FreeMem</var> will cause the memory manager to execute a <var>halt(1)</var>
  201. instruction, causing a memory dump. By Default it is set to <var>True</var>.
  202. </descr>
  203. </element>
  204. <element name="KeepReleased">
  205. <short>Keep released blocks in memory</short>
  206. <descr>
  207. If <var>keepreleased</var> is set to true, then a list of freed memory
  208. blocks is kept. This is useful if you suspect that the same memory block is
  209. released twice. However, this option is very memory intensive, so use it
  210. sparingly, and only when it's really necessary.
  211. </descr>
  212. </element>
  213. <element name="Add_Tail">
  214. <short>Check for memory overwrites</short>
  215. <descr>
  216. If <var>add\_tail</var> is <var>True</var> (the default) then a check is also
  217. performed on the memory location just behind the allocated memory.
  218. </descr>
  219. </element>
  220. <element name="usecrc">
  221. <short>Use CRC check on allocated memory</short>
  222. <descr>
  223. If <var>usecrc</var> is <var>True</var> (the default) then a crc check is performed
  224. on locations before and after the allocated memory. This is useful to
  225. detect memory overwrites.
  226. </descr>
  227. </element>
  228. <element name="DumpHeap">
  229. <short>Dump memory usage report to stderr.</short>
  230. <descr>
  231. <var>DumpHeap</var> dumps to standard output a summary of memory usage.
  232. It is called automatically by the heaptrc unit when your program exits
  233. (by installing an exit procedure), but it can be called at any time.
  234. </descr>
  235. <errors>
  236. None.
  237. </errors>
  238. <seealso>
  239. <link id="MarkHeap"/>
  240. </seealso>
  241. </element>
  242. <element name="MarkHeap">
  243. <short>Mark memory blocks with a signature.</short>
  244. <descr>
  245. <var>MarkHeap</var> marks all memory blocks with a special signature.
  246. You can use this if you think that you corruped the memory.
  247. </descr>
  248. <errors>
  249. None.
  250. </errors>
  251. <seealso>
  252. <link id="DumpHeap"/>
  253. </seealso>
  254. </element>
  255. <element name="SetHeapExtraInfo">
  256. <short>Store extra information in blocks.</short>
  257. <descr>
  258. <p>
  259. You can use <var>SetHeapExtraInfo</var> to store extra info in the blocks that
  260. the heaptrc unit reserves when tracing getmem calls. <var>Size</var> indicates the
  261. size (in bytes) that the trace mechanism should reserve for your extra
  262. information. For each call to <var>getmem</var>, <var>FillProc</var> will be called,
  263. and passed a pointer to the memory reserved.
  264. </p>
  265. <p>
  266. When dumping the memory summary, the extra info is shown by calling
  267. <var>displayproc</var> and passing it the memory location which was
  268. filled by <var>fillproc</var>. It should write the information in
  269. readable form to the text file provided in the call to <var>displayproc</var>
  270. </p>
  271. </descr>
  272. <errors>
  273. You can only call <var>SetHeapExtraInfo</var> if no memroy has been allocated
  274. yet. If memory was already allocated prior to the call to
  275. <var>SetHeapExtraInfo</var>, then an error will be displayed on standard error
  276. output, and a <link id="DumpHeap"/> is executed.
  277. </errors>
  278. <seealso>
  279. <link id="DumpHeap"/>
  280. <link id="SetHeapTraceOutput"/>
  281. </seealso>
  282. <example file="heapex/setinfo"/>
  283. </element>
  284. <element name="SetHeapTraceOutput">
  285. <short>Specify filename for heap trace output.</short>
  286. <descr>
  287. <var>SetHeapTraceOutput</var> sets the filename into which heap trace info
  288. will be written. By default information is written to standard output,
  289. this function allows you to redirect the information to a file with
  290. full filename <var>name</var>.
  291. </descr>
  292. <errors>
  293. If the file cannot be written to, errors will occur when writing the
  294. trace.
  295. </errors>
  296. <seealso>
  297. <link id="SetHeapExtraInfo"/>
  298. </seealso>
  299. </element>
  300. </module>
  301. </package>
  302. </fpdoc-descriptions>