• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..09-Aug-2007-

LeakCheckH A D16-Jan-2004416 2316

LeakCheckAnalyzeH A D20-Apr-2007217 149

LeakTracer.ccH A D16-Jan-200413.7 KiB579346

MakefileH A D16-Jan-20041.4 KiB6532

READMEH A D16-Jan-20048.7 KiB231167

VERSIONH A D16-Jan-20044 21

leak-analyzeH A D17-Jan-20043.2 KiB11767

test.ccH A D16-Jan-2004353 2812

README

1Introduction
2------------
3
4LeakTracer is a small tool I wrote when checking a C++ program for memory
5leaks. I couldn't get dmalloc to display what I wanted, and I just saw the
6__builtin_return_address gcc-extension mentioned.
7
8To use LeakTracer, run your program using the provided LeakCheck script. It
9uses the LD_PRELOAD feature to "overlay" some functions on top of your
10functions (no recompile needed). If your platform does not support LD_PRELOAD,
11you can add the LeakTracer.o object file to the objects in your Makefile and
12run your application.
13
14LeakTracer uses gdb to print out the exact line where the memory was allocated
15and not freed - this of course means you have to free all dynamically
16allocated data. LeakTracer also overrides the global operator new and operator
17delete - this will give problems if you override them as well.
18
19LeakTracer traces only new/new[] and delete calls - it does not look at
20malloc/free/realloc.
21
22Here is some example output:
23
24Gathered 8 (8 unique) points of data.
25(gdb)
26Allocations: 1 / Size: 36
270x80608e6 is in NullArcableInstance::NullArcableInstance(void) (Machine.cc:40).
2839      public:
2940          NullArcableInstance() : ArcableInstance(new NullArcable) {}
30
31Allocations: 1 / Size: 8
320x8055b02 is in init_types(void) (Type.cc:119).
33118     void init_types() {
34119         Type::Integer = new IntegerType;
35
36Allocations: 1 / Size: 132 (new[])
370x805f4ab is in Hashtable<NativeCallable, String, false, true>::Hashtable(unsigned int) (ea/h/Hashtable.h:15).
3814          Hashtable (uint _size = 32) : size(_size), count(0) {
3915              table = new List<E, own> [size];
40
41[...]
42
43Requirements
44------------
45
46You need Perl5 and gdb installed to run the leak-analyzer. You need gcc -- I
47currently use 2.95 but have used it with previous older versions without
48problems.
49
50
51You also need to run this on an architecture which supports
52__builtin_return_address arguments that are greater than 0 - there may be
53some problems on MIPS there.
54
55So far this code has been tested under Linux 2.2, x86 system, Solaris and
56HP-UX.
57
58
59Installation
60------------
61
62Just type make. There is no install target; you should put LeakTracer
63some place you can remember.
64
65Since version 2.0, it is possible to preload the LeakTracer object on
66architectures that support LD_PRELOAD (this is at least Linux and probably
67others -- please report success/failure). This means it is much easier to use
68the program: you do not need to relink your program with LeakTracer.o.
69
70In case your platform does not support LD_PRELOAD, you can use LeakTracer in
71the old pre 2.0 way: add LeakTracer.o to your object files -- at the very end
72of them (also after -llibrary lines).
73
74In any case your application must also be compiled with debugging enabled
75(i.e. -g).
76
77Running with LeakTracer
78-----------------------
79
80If you are using the shared object, run the LeakCheck script. This script
81should stay in the directory where you install LeakCheck -- it will search for
82LeakTracer.so file there and load it. E.g.:
83
84~/src/LeakTracer/LeakCheck yourApplication
85
86(if you put LeakTracer in ~/src/LeakTracer/)
87
88Run your application as normal, performing tasks that you want to be traced
89for memory leaks. While the application runs, LeakTracer will write data about
90memory allocation to the file "leak.out" in the current directory. You can
91override the location of that file by setting the LEAKTRACE_FILE environment
92variable.
93
94If you cannot use LD_PRELOAD, just run your application as normal after
95relinking it. It will also produce a "leak.out" file when it finishes.
96
97
98Detectable errors
99-----------------
100
101LeakTracer is capable to detect the following problems with your program
102
103  1) memory which is allocated but not freed
104  2) (limited support for) overwritten memory at the end of the allocated
105     block  ( reason = 1 )
106  3) memory which is tried to be deleted but which is not allocated
107     (either because of a garbage pointer or twice deletion)
108     (reason = 2)
109  4) memory which is allocated with new[] but deleted with simple delete
110     and vice versa (reason = 4)
111
112For the last three problems, LeakTracer can abort() your program if you
113tell it so; the resulting core-dump allows to debug the problem. By default,
114only the overwrite memory condition results in an abort of the program
115because it is inherently critical. The two other conditions are not critical.
116You can influence what LeakTracer does with the environment variable
117   LT_ABORTREASON
118which you can set to some numeric value which is the result of the
119sum of the reasons you find in the parentesis in the enumeration above.
120To abort on any reason, for example, you would set LT_ABORTREASON to 7.
121
122
123Analyzing output
124----------------
125
126You should then run leak-analyze, since looking at the raw leak.out file will
127not help you much. To run leak-analyze, you need Perl as well as gdb
128installed (any version of gdb will do). For example:
129
130leak-analyze myprog leak.out
131
132You don't have to specify the leak.out filename if you just use the default
133one. leak-analyze will run gdb on the file, sending it a number of commands
134that will show the source lines with the memory leaks.
135
136leak-analyze should show you something like this:
137
138Gathered 2 (2 unique) points of data.
139
140#-- Alloc: Different allocation schemes
141alloc here :0x80485b7 is in main (test.cc:6).
1425
1436               int *wrong = new int[10];
144..free here :0x80485d9 is in main (test.cc:11).
14511              delete wrong;
146
147#-- Leak: Allocations: 1 / Size: 168
1480x8048593 is in main (test.cc:3).
1492       int main() {
1503               int *array = new int [42] ;
151
152#-- Leak: Allocations: 1 / Size: 4
1530x80485a5 is in main (test.cc:4).
1543               int *array = new int [42] ;
1554               int *foo = new int;
156
157
158This means that total of two allocations happened, in two different places.
159
160First a delete error is shown: you allocated some memory using new[] but you
161freed it using delete. leak-analyze will show where you allocated the memory and where you freed it.
162
163Afterwards each allocation is shown in turn. There was 1 allocation from this
164line of code (test.cc:3), and it was 168 bytes in size. Note that of the two
165lines of code shown, it's the bottom one that created the allocation.
166
167That's all there is to it - now you should find those memory leaks, fix them
168and rerun Leak tracer.
169
170Shared libraries and objects
171----------------------------
172
173If you want to analyze the leaks in shared libraries in your file, it may be
174necessary to make leak-analyze run your program and thus load the shared
175libraries before searching for addresses.
176
177To do that, run leak-analyze with the program name, leak name AND another
178argument which is where to set the breakpoint, e.g.:
179
180leak-analyze myprog leak.out main
181
182This will make leak-analyze tell gdb to set a breakpoint on "main" and then
183run the program. After the analysis is complete, the program will be killed.
184
185If you want to load some shared libraries, you can set a breakpoint on a
186different location, e.g. main.cc:42 if you know that once line 42 is reached,
187all shared objects have been loaded.
188
189If your program needs some command line arguments, supply them after "main".
190
191
192Licensing
193---------
194
195LeakTracer is public domain (i.e. do with it whatever you feel like).
196
197Credits
198-------
199
200Initial version of LeakTracer was written by Erwin Andreasen. Henner Zeller
201(foobar@to.com) contributed a rewrite of the code which
202introduced dynamic loading of LeakTracer and more.
203
204
205Revision history
206----------------
207
208February 21, 1999		v1.0 - only tested internally
209February 23, 1999		v1.1 - added operator new[] / delete[]
210February 23, 1999       	v1.2 - Oops, forgot to free() the memory..
211February 26, 1999		v1.3 - allow delete 0
212March 27, 1999			v1.4 - Allow %p format without leading 0x for non-GNU
213                                       libc. Option to leak-analyze to run the program.
214July 21, 1999           	v1.5 - Fix for the above suggested by Alan Gonzalez
215August 21, 2000			v1.6 - use a destructor instead of
216                                       __attribute__(destructor)
217November 19, 2000               v2.0 - Rewrite by Henner Zeller introduces LD_PRELOAD
218                                       and much more
219February 27, 2001               v2.1 - Further update by Henner: optional thread safety,
220                                       choose what should make LeakTracer abort(), better
221                                       tracing of delete on non-new'ed pointers
222March 2, 2001                   v2.2 - Another updated by Henner: hash table to increase
223                                       performance with many allocations
224June 13, 2001                   v2.3 - Made LT more resistant to being called before init
225                                       and after destruction
226
227Authors: 	Erwin Andreasen <erwin@andreasen.org>
228		Henner Zeller <foobar@to.com>
229Homepage:	http://www.andreasen.org/LeakTracer/
230
231