1 .\" $Id: mandoc_dbg_init.3,v 1.1 2022/04/14 16:43:44 schwarze Exp $
3 .\" Copyright (c) 2021, 2022 Ingo Schwarze <schwarze@openbsd.org>
5 .\" Permission to use, copy, modify, and distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the above
7 .\" copyright notice and this permission notice appear in all copies.
9 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 .Dd $Mdocdate: April 14 2022 $
24 .Nd search for memory leaks in mandoc
27 .Fn mandoc_dbg_init "int argc" "char *argv[]"
29 .Fn mandoc_dbg_name "const char *"
31 .Fn mandoc_dbg_finish void
33 If the mandoc package is built with the line
37 the functions documented in
41 are instrumented to record every memory allocation in a dedicated
42 hash table and to check that every allocation is freed again.
43 This compile time option is only intended for binaries that are
44 used exclusively for debugging.
45 It is not intended for production binaries because it significantly
46 increases run time and memory usage and makes the programs more
47 fragile and more error-prone.
51 initializes the memory debugging subsystem.
52 It is called from the top of the
54 programs, passing through the arguments that
59 section of the present manual page explains how the
61 environment variable controls the amount and destination of reporting.
73 macro is parsed, passing the complete macro line as the argument.
77 performs cleanup and optionally final reporting.
78 It is called from the end of the
80 programs, just before normal termination.
84 directives right for these functions is slightly tricky.
85 If a file already includes
87 no additional directive is needed because
96 If a file does not need
98 but calls a function documented in the present manual page and also calls
100 directly, it needs this code before the other
101 .Xr mandoc_headers 3 :
102 .Bd -literal -offset indent
104 #include "mandoc_dbg.h"
108 If a file calls a function documented in the present manual page
109 but does not directly call
111 it can use this less intrusive idiom:
112 .Bd -literal -offset indent
115 #include "mandoc_dbg.h"
119 The environment variable
121 controls the amount and destination of reporting.
123 If it is unset, diagnostic output is directed to standard error output
124 and only fatal errors are reported.
125 Even though full memory accounting is always performed
126 by any binary that was compiled with
128 resulting in a significant increase in both run time and memory usage,
133 is not set at run time.
137 is set, it is interpreted as a string of flags.
138 The flags are as follows:
141 Log every allocation.
142 This produces huge amounts of output and is usually not needed
143 to find memory leaks.
144 Its main purpose is debugging the memory debugging subsystem itself.
146 When enabled, allocations are logged in this format:
148 .D1 Cm A Ar file Ns .c: Ns Ar line function Ns Po Fa nmemb , size Pc\
151 The meaning of the fields is the same as for the
157 and every reallocation where the memory to be released or reallocated
158 was allocated with one of the functions documented in
159 .Xr mandoc_malloc 3 .
160 Again, this produces huge amounts of output and is usually not
161 needed to find memory leaks, and its main purpose is debugging the
162 memory debugging subsystem itself.
164 The logging format is:
166 .D1 Cm F Ar file Ns .c: Ns Ar line function Ns Pq address
168 It provides the name of the
170 and the number of the
172 in that file which called the
178 that was given as an argument.
184 flags are enabled, calls to reallocation functions often log two lines,
187 line reporting the address passed in as an argument, then an
189 line reporting the adress returned as the function return value.
191 Log every memory leak.
192 For every allocation made after
194 using functions documented in
196 that was not freed before
197 .Fn mandoc_dbg_finish ,
198 print a line in this format:
200 .D1 Cm L Ar file Ns .c: Ns Ar line function Ns Po Fa nmemb , size Pc\
203 It provides the name of the
205 and the number of the
207 in that file which called the allocation
222 At the end of the line, the virtual
224 of the memory returned from the allocation function is reported.
226 Log the names of manual pages processed in the following formats:
227 .Bd -unfilled -offset indent
228 .Cm N Pf . Ic \&Dt Ar name section Op Ar architecture
229 .Cm N Pf . Ic \&TH Ar name section Op Ar additional arguments
232 This is particularly useful if a program crashes, runs out of memory,
233 or enters an infinite loop.
236 line logged often indicates the input file triggering the problem.
238 Interpret the rest of
240 as an absolute path and redirect debugging output to that file,
241 appending to the file if it already exists or creating it otherwise.
246 is set, even if it is empty,
248 always writes the line
250 .D1 Cm P Ar pid Sy \&[ Ns Ar progname Ns Sy \&]\
251 Sy \&[ Ns Ar argument Ns Sy \&] Ar ...
253 enclosing each element of
255 in square brackets, to avoid that arguments containing whitespace
256 appear in the same way as multiple arguments, and
257 .Fn mandoc_dbg_finish
258 always writes the line:
260 .D1 Cm S Ar number No memory leaks found
262 The following is a typical sequence of commands for finding memory
263 leaks in the parsers, in the HTML formatter, and in the regression suite:
264 .Bd -literal -offset indent
266 echo BUILD_CATMAN=1 >> configure.local
267 echo DEBUG_MEMORY=1 >> configure.local
270 export DEBUG_MEMORY=NL/tmp/mandoc.debug.txt
272 export PATH=$PATH:$(pwd)
273 \&./catman -T html /usr/share/man Out
276 less /tmp/mandoc.debug.txt
279 .Xr mandoc_malloc 3 ,