]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/tools/build/src/engine/boehm_gc/doc/leak.html
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / tools / build / src / engine / boehm_gc / doc / leak.html
CommitLineData
7c673cae
FG
1<HTML>
2<HEAD>
3<TITLE>Using the Garbage Collector as Leak Detector</title>
4</head>
5<BODY>
6<H1>Using the Garbage Collector as Leak Detector</h1>
7The garbage collector may be used as a leak detector.
8In this case, the primary function of the collector is to report
9objects that were allocated (typically with <TT>GC_MALLOC</tt>),
10not deallocated (normally with <TT>GC_FREE</tt>), but are
11no longer accessible. Since the object is no longer accessible,
12there in normally no way to deallocate the object at a later time;
13thus it can safely be assumed that the object has been "leaked".
14<P>
15This is substantially different from counting leak detectors,
16which simply verify that all allocated objects are eventually
17deallocated. A garbage-collector based leak detector can provide
18somewhat more precise information when an object was leaked.
19More importantly, it does not report objects that are never
20deallocated because they are part of "permanent" data structures.
21Thus it does not require all objects to be deallocated at process
22exit time, a potentially useless activity that often triggers
23large amounts of paging.
24<P>
25All non-ancient versions of the garbage collector provide
26leak detection support. Version 5.3 adds the following
27features:
28<OL>
29<LI> Leak detection mode can be initiated at run-time by
30setting GC_find_leak instead of building the collector with FIND_LEAK
31defined. This variable should be set to a nonzero value
32at program startup.
33<LI> Leaked objects should be reported and then correctly garbage collected.
34Prior versions either reported leaks or functioned as a garbage collector.
35</ol>
36For the rest of this description we will give instructions that work
37with any reasonable version of the collector.
38<P>
39To use the collector as a leak detector, follow the following steps:
40<OL>
41<LI> Build the collector with -DFIND_LEAK. Otherwise use default
42build options.
43<LI> Change the program so that all allocation and deallocation goes
44through the garbage collector.
45<LI> Arrange to call <TT>GC_gcollect</tt> at appropriate points to check
46for leaks.
47(For sufficiently long running programs, this will happen implicitly,
48but probably not with sufficient frequency.)
49</ol>
50The second step can usually be accomplished with the
51<TT>-DREDIRECT_MALLOC=GC_malloc</tt> option when the collector is built,
52or by defining <TT>malloc</tt>, <TT>calloc</tt>,
53<TT>realloc</tt> and <TT>free</tt>
54to call the corresponding garbage collector functions.
55But this, by itself, will not yield very informative diagnostics,
56since the collector does not keep track of information about
57how objects were allocated. The error reports will include
58only object addresses.
59<P>
60For more precise error reports, as much of the program as possible
61should use the all uppercase variants of these functions, after
62defining <TT>GC_DEBUG</tt>, and then including <TT>gc.h</tt>.
63In this environment <TT>GC_MALLOC</tt> is a macro which causes
64at least the file name and line number at the allocation point to
65be saved as part of the object. Leak reports will then also include
66this information.
67<P>
68Many collector features (<I>e.g</i> stubborn objects, finalization,
69and disappearing links) are less useful in this context, and are not
70fully supported. Their use will usually generate additional bogus
71leak reports, since the collector itself drops some associated objects.
72<P>
73The same is generally true of thread support. However, as of 6.0alpha4,
74correct leak reports should be generated with linuxthreads.
75<P>
76On a few platforms (currently Solaris/SPARC, Irix, and, with -DSAVE_CALL_CHAIN,
77Linux/X86), <TT>GC_MALLOC</tt>
78also causes some more information about its call stack to be saved
79in the object. Such information is reproduced in the error
80reports in very non-symbolic form, but it can be very useful with the
81aid of a debugger.
82<H2>An Example</h2>
83The following header file <TT>leak_detector.h</tt> is included in the
84"include" subdirectory of the distribution:
85<PRE>
86#define GC_DEBUG
87#include "gc.h"
88#define malloc(n) GC_MALLOC(n)
89#define calloc(m,n) GC_MALLOC((m)*(n))
90#define free(p) GC_FREE(p)
91#define realloc(p,n) GC_REALLOC((p),(n))
92#define CHECK_LEAKS() GC_gcollect()
93</pre>
94<P>
95Assume the collector has been built with -DFIND_LEAK. (For very
96new versions of the collector, we could instead add the statement
97<TT>GC_find_leak = 1</tt> as the first statement in <TT>main</tt>.
98<P>
99The program to be tested for leaks can then look like:
100<PRE>
101#include "leak_detector.h"
102
103main() {
104 int *p[10];
105 int i;
106 /* GC_find_leak = 1; for new collector versions not */
107 /* compiled with -DFIND_LEAK. */
108 for (i = 0; i < 10; ++i) {
109 p[i] = malloc(sizeof(int)+i);
110 }
111 for (i = 1; i < 10; ++i) {
112 free(p[i]);
113 }
114 for (i = 0; i < 9; ++i) {
115 p[i] = malloc(sizeof(int)+i);
116 }
117 CHECK_LEAKS();
118}
119</pre>
120<P>
121On an Intel X86 Linux system this produces on the stderr stream:
122<PRE>
123Leaked composite object at 0x806dff0 (leak_test.c:8, sz=4)
124</pre>
125(On most unmentioned operating systems, the output is similar to this.
126If the collector had been built on Linux/X86 with -DSAVE_CALL_CHAIN,
127the output would be closer to the Solaris example. For this to work,
128the program should not be compiled with -fomit_frame_pointer.)
129<P>
130On Irix it reports
131<PRE>
132Leaked composite object at 0x10040fe0 (leak_test.c:8, sz=4)
133 Caller at allocation:
134 ##PC##= 0x10004910
135</pre>
136and on Solaris the error report is
137<PRE>
138Leaked composite object at 0xef621fc8 (leak_test.c:8, sz=4)
139 Call chain at allocation:
140 args: 4 (0x4), 200656 (0x30FD0)
141 ##PC##= 0x14ADC
142 args: 1 (0x1), -268436012 (0xEFFFFDD4)
143 ##PC##= 0x14A64
144</pre>
145In the latter two cases some additional information is given about
146how malloc was called when the leaked object was allocated. For
147Solaris, the first line specifies the arguments to <TT>GC_debug_malloc</tt>
148(the actual allocation routine), The second the program counter inside
149main, the third the arguments to <TT>main</tt>, and finally the program
150counter inside the caller to main (i.e. in the C startup code).
151<P>
152In the Irix case, only the address inside the caller to main is given.
153<P>
154In many cases, a debugger is needed to interpret the additional information.
155On systems supporting the "adb" debugger, the <TT>callprocs</tt> script
156can be used to replace program counter values with symbolic names.
157As of version 6.1, the collector tries to generate symbolic names for
158call stacks if it knows how to do so on the platform. This is true on
159Linux/X86, but not on most other platforms.
160<H2>Simplified leak detection under Linux</h2>
161Since version 6.1, it should be possible to run the collector in leak
162detection mode on a program a.out under Linux/X86 as follows:
163<OL>
164<LI> <I>Ensure that a.out is a single-threaded executable, or you are using
165a very recent (7.0alpha7+) collector version on Linux.</i>
166On most platforms this does not
167work at all for multithreaded programs.
168<LI> If possible, ensure that the addr2line program is installed in
169/usr/bin. (It comes with RedHat Linux.)
170<LI> If possible, compile a.out with full debug information.
171This will improve the quality of the leak reports. With this approach, it is
172no longer necessary to call GC_ routines explicitly, though that can also
173improve the quality of the leak reports.
174<LI> Build the collector and install it in directory <I>foo</i> as follows:
175<UL>
176<LI> <TT>configure --prefix=<I>foo</i> --enable-full-debug --enable-redirect-malloc
177--disable-threads</tt>
178<LI> <TT>make</tt>
179<LI> <TT>make install</tt>
180</ul>
181With a very recent collector on Linux, it may be safe to omit the <TT>--disable-threads</tt>.
182<LI> Set environment variables as follows:
183<UL>
184<LI> LD_PRELOAD=<I>foo</i>/lib/libgc.so
185<LI> GC_FIND_LEAK
186<LI> You may also want to set GC_PRINT_STATS (to confirm that the collector
187is running) and/or GC_LOOP_ON_ABORT (to facilitate debugging from another
188window if something goes wrong).
189</ul
190<LI> Simply run a.out as you normally would. Note that if you run anything
191else (<I>e.g.</i> your editor) with those environment variables set,
192it will also be leak tested. This may or may not be useful and/or
193embarrassing. It can generate
194mountains of leak reports if the application wasn't designed to avoid leaks,
195<I>e.g.</i> because it's always short-lived.
196</ol>
197This has not yet been thoroughly tested on large applications, but it's known
198to do the right thing on at least some small ones.
199</body>
200</html>