fpc/docs/heaptrc.tex
peter 356a3d1609 * support for hevea
* provided local copies or required styles since debian does not
    supply the listings.sty anymore
2003-03-16 15:22:18 +00:00

236 lines
7.9 KiB
TeX

%
% $Id$
% This file is part of the FPC documentation.
% Copyright (C) 1998, by Michael Van Canneyt
%
% The FPC documentation is free text; you can redistribute it and/or
% modify it under the terms of the GNU Library General Public License as
% published by the Free Software Foundation; either version 2 of the
% License, or (at your option) any later version.
%
% The FPC Documentation is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% Library General Public License for more details.
%
% You should have received a copy of the GNU Library General Public
% License along with the FPC documentation; see the file COPYING.LIB. If not,
% write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
% Boston, MA 02111-1307, USA.
%
\chapter{The HEAPTRC unit.}
\FPCexampledir{heapex}
This chapter describes the HEAPTRC unit for \fpc. It was written by
Pierre Muller. It is system independent, and works on all supported systems.
\section{Purpose}
The HEAPTRC unit can be used to debug your memory allocation/deallocation.
It keeps track of the calls to getmem/freemem, and, implicitly, of
New/Dispose statements.
When the program exits, or when you request it explicitly.
It displays the total memory used, and then dumps a list of blocks that
were allocated but not freed. It also displays where the memory was
allocated.
If there are any inconsistencies, such as memory blocks being allocated
or freed twice, or a memory block that is released but with wrong size,
this will be displayed also.
The information that is stored/displayed can be customized using
some constants.
\section{Usage}
All that you need to do is to include \file{heaptrc} in the uses clause
of your program. Make sure that it is the first unit in the clause,
otherwise memory allocated in initialization code of units that precede the
heaptrc unit will not be accounted for, causing an incorrect memory usage
report.
If you use the \var{-gh} switch, the compiler will insert the unit by itself,
so you don't have to include it in your uses clause.
The following example shows how to use the heaptrc unit.
\FPCexample{heapex}
This is the memory dump shown when running this program:
\begin{verbatim}
Marked memory at 0040FA50 invalid
Wrong size : 128 allocated 64 freed
0x00408708
0x0040CB49
0x0040C481
Call trace for block 0x0040FA50 size 128
0x0040CB3D
0x0040C481
\end{verbatim}
If you use the \file{lineinfo} unit (or use the \var{-gl} switch) as well,
then \file{heaptrc} will also give you the filenames and line-numbers of
the procedures in the backtrace:
\begin{verbatim}
Marked memory at 00410DA0 invalid
Wrong size : 128 allocated 64 freed
0x004094B8
0x0040D8F9 main, line 25 of heapex.pp
0x0040D231
Call trace for block 0x00410DA0 size 128
0x0040D8ED main, line 23 of heapex.pp
0x0040D231
\end{verbatim}
If lines without filename/line-number occur, this means there is a unit which
has no debug info included.
\section{Constants, Types and variables}
The \var{FillExtraInfoType} is a procedural type used in the
\seep{SetExtraInfo} call.
\begin{verbatim}
type
FillExtraInfoType = procedure(p : pointer);
\end{verbatim}
The following typed constants allow to fine-tune the standard dump of the
memory usage by \seep{DumpHeap}:
\begin{verbatim}
const
tracesize = 8;
quicktrace : boolean = true;
HaltOnError : boolean = true;
keepreleased : boolean = false;
add_tail : boolean = true;
usecrc : boolean = true
\end{verbatim}
\var{Tracesize} specifies how many levels of calls are displayed of the
call stack during the memory dump. If you specify \var{keepreleased:=True}
then half the \var{TraceSize} is reserved for the \var{GetMem} call stack,
and the other half is reserved for the \var{FreeMem} call stack.
For example, the default value of 8 will cause eight levels of call frames
to be dumped for the getmem call if \var{keepreleased} is \var{False}. If
\var{KeepReleased} is true, then 4 levels of call frames will be dumped for
the \var{GetMem} call and 4 frames wil be dumped for the \var{FreeMem} call.
If you want to change this value, you must recode the \file{heaptrc} unit.
\var{Quicktrace} determines whether the memory manager checks whether a
block that is about to be released is allocated correctly. This is a rather
time consuming search, and slows program execution significantly, so by
default it is set to \var{False}.
If \var{HaltOnError} is set to \var{True} then an illegal call to
\var{FreeMem} will cause the memory manager to execute a \var{halt(1)}
instruction, causing a memory dump. By Default it is set to \var{True}.
If \var{keepreleased} is set to true, then a list of freed memory
blocks is kept. This is useful if you suspect that the same memory block is
released twice. However, this option is very memory intensive, so use it
sparingly, and only when it's really necessary.
If \var{add\_tail} is \var{True} (the default) then a check is also
performed on the memory location just behind the allocated memory.
If \var{usecrc} is \var{True} (the default) then a crc check is performed
on locations before and after the allocated memory. This is useful to
detect memory overwrites.
\section{Functions and procedures}
\begin{procedure}{DumpHeap}
\Declaration
procedure DumpHeap;
\Description
\var{DumpHeap} dumps to standard output a summary of memory usage.
It is called automatically by the heaptrc unit when your program exits
(by instaling an exit procedure), but it can be called at any time
\Errors
None.
\SeeAlso
\seep{MarkHeap}
\end{procedure}
\begin{procedure}{MarkHeap}
\Declaration
procedure MarkHeap;
\Description
\var{MarkHeap} marks all memory blocks with a special signature.
You can use this if you think that you corruped the memory.
\Errors
None.
\SeeAlso
\seep{DumpHeap}
\end{procedure}
\begin{procedure}{SetExtraInfo}
\Declaration
procedure SetExtraInfo( size : longint;func : FillExtraInfoType);
\Description
You can use \var{SetExtraInfo} to store extra info in the blocks that
the heaptrc unit reserves when tracing getmem calls. \var{Size} indicates the
size (in bytes) that the trace mechanism should reserve for your extra
information. For each call to \var{getmem}, \var{func} will be called,
and passed a pointer to the memory reserved.
When dumping the memory summary, the extra info is shown as Longint values.
\Errors
You can only call \var{SetExtraInfo} if no memroy has been allocated
yet. If memory was already allocated prior to the call to
\var{SetExtraInfo}, then an error will be displayed on standard error
output, and a \seep{DumpHeap} is executed.
\SeeAlso
\seep{DumpHeap},\seep{SetHeapTraceOutput}
\end{procedure}
\FPCexample{setinfo}
\begin{procedure}{SetHeapTraceOutput}
\Declaration
Procedure SetHeapTraceOutput(const name : string);
\Description
\var{SetHeapTraceOutput} sets the filename into which heap trace info
will be written. By default information is written to standard output,
this function allows you to redirect the information to a file with
full filename \var{name}.
\Errors
If the file cannot be written to, errors will occur when writing the
trace.
\SeeAlso
\seep{SetExtraInfo}
\end{procedure}
%
% $Log$
% Revision 1.2 2003-03-16 15:22:18 peter
% * support for hevea
% * provided local copies or required styles since debian does not
% supply the listings.sty anymore
%
% Revision 1.1 2000/07/13 09:10:04 michael
% + Initial import
%
% Revision 1.6 2000/07/11 18:07:26 peter
% * fixes for latex2html 0.99b2
%
% Revision 1.5 2000/05/16 21:07:55 michael
% + Implemented large part of TODO list. Too much to denote
%
% Revision 1.4 2000/02/07 11:21:06 michael
% + Documented heaptrc and lineinfo
%
% Revision 1.3 1999/06/25 22:12:16 michael
% + Update to version 0.19 of listings package
%
% Revision 1.2 1998/12/15 23:50:52 michael
% * Some updates
%
% Revision 1.1 1998/12/14 23:17:02 michael
% + INitial implementation
%