blob: 063a1615bbe55bc99475ad0c117981261aebb634 [file] [log] [blame]
nethercoteef0abd12004-04-10 00:29:58 +00001Valgrind FAQ, version 2.1.2
2~~~~~~~~~~~~~~~~~~~~~~~~~~~
nethercote8deae812004-07-18 10:35:36 +00003Last revised 18 July 2004
nethercoteef0abd12004-04-10 00:29:58 +00004~~~~~~~~~~~~~~~~~~~~~~~~~
njn4e59bd92003-04-22 20:58:47 +00005
nethercoteef0abd12004-04-10 00:29:58 +000061. Background
72. Compiling, installing and configuring
83. Valgrind aborts unexpectedly
94. Valgrind behaves unexpectedly
105. Memcheck doesn't find my bug
116. Miscellaneous
12
13
14-----------------------------------------------------------------
151. Background
16-----------------------------------------------------------------
17
181.1. How do you pronounce "Valgrind"?
19
20The "Val" as in the world "value". The "grind" is pronounced with a
21short 'i' -- ie. "grinned" (rhymes with "tinned") rather than "grined"
22(rhymes with "find").
23
24Don't feel bad: almost everyone gets it wrong at first.
njn4e59bd92003-04-22 20:58:47 +000025
sewardj36a53ad2003-04-22 23:26:24 +000026-----------------------------------------------------------------
27
nethercoteef0abd12004-04-10 00:29:58 +0000281.2. Where does the name "Valgrind" come from?
29
30From Nordic mythology. Originally (before release) the project was
31named Heimdall, after the watchman of the Nordic gods. He could "see a
32hundred miles by day or night, hear the grass growing, see the wool
33growing on a sheep's back" (etc). This would have been a great name,
34but it was already taken by a security package "Heimdal".
35
36Keeping with the Nordic theme, Valgrind was chosen. Valgrind is the
37name of the main entrance to Valhalla (the Hall of the Chosen Slain in
38Asgard). Over this entrance there resides a wolf and over it there is
39the head of a boar and on it perches a huge eagle, whose eyes can see to
40the far regions of the nine worlds. Only those judged worthy by the
41guardians are allowed to pass through Valgrind. All others are refused
42entrance.
43
44It's not short for "value grinder", although that's not a bad guess.
45
46
47-----------------------------------------------------------------
482. Compiling, installing and configuring
49-----------------------------------------------------------------
50
512.1. When I trying building Valgrind, 'make' dies partway with an
52 assertion failure, something like this: make: expand.c:489:
53
54 allocated_variable_append: Assertion
55 `current_variable_set_list->next != 0' failed.
56
57It's probably a bug in 'make'. Some, but not all, instances of version 3.79.1
58have this bug, see www.mail-archive.com/bug-make@gnu.org/msg01658.html. Try
59upgrading to a more recent version of 'make'. Alternatively, we have heard
60that unsetting the CFLAGS environment variable avoids the problem.
61
62
63-----------------------------------------------------------------
643. Valgrind aborts unexpectedly
65-----------------------------------------------------------------
66
673.1. Programs run OK on Valgrind, but at exit produce a bunch of errors a bit
68 like this
njn4e59bd92003-04-22 20:58:47 +000069
70 ==20755== Invalid read of size 4
71 ==20755== at 0x40281C8A: _nl_unload_locale (loadlocale.c:238)
72 ==20755== by 0x4028179D: free_mem (findlocale.c:257)
73 ==20755== by 0x402E0962: __libc_freeres (set-freeres.c:34)
74 ==20755== by 0x40048DCC: vgPlain___libc_freeres_wrapper
75 (vg_clientfuncs.c:585)
76 ==20755== Address 0x40CC304C is 8 bytes inside a block of size 380 free'd
77 ==20755== at 0x400484C9: free (vg_clientfuncs.c:180)
78 ==20755== by 0x40281CBA: _nl_unload_locale (loadlocale.c:246)
79 ==20755== by 0x40281218: free_mem (setlocale.c:461)
80 ==20755== by 0x402E0962: __libc_freeres (set-freeres.c:34)
81
82 and then die with a segmentation fault.
83
nethercoteef0abd12004-04-10 00:29:58 +000084When the program exits, Valgrind runs the procedure __libc_freeres() in
85glibc. This is a hook for memory debuggers, so they can ask glibc to
86free up any memory it has used. Doing that is needed to ensure that
87Valgrind doesn't incorrectly report space leaks in glibc.
njn4e59bd92003-04-22 20:58:47 +000088
nethercoteef0abd12004-04-10 00:29:58 +000089Problem is that running __libc_freeres() in older glibc versions causes
90this crash.
njn4e59bd92003-04-22 20:58:47 +000091
nethercoteef0abd12004-04-10 00:29:58 +000092WORKAROUND FOR 1.1.X and later versions of Valgrind: use the
93--run-libc-freeres=no flag. You may then get space leak reports for
94glibc-allocations (please _don't_ report these to the glibc people,
95since they are not real leaks), but at least the program runs.
njn4e59bd92003-04-22 20:58:47 +000096
sewardj36a53ad2003-04-22 23:26:24 +000097-----------------------------------------------------------------
njn4e59bd92003-04-22 20:58:47 +000098
nethercoteef0abd12004-04-10 00:29:58 +0000993.2. My (buggy) program dies like this:
njn4e59bd92003-04-22 20:58:47 +0000100 valgrind: vg_malloc2.c:442 (bszW_to_pszW):
101 Assertion `pszW >= 0' failed.
njn4e59bd92003-04-22 20:58:47 +0000102
nethercoteef0abd12004-04-10 00:29:58 +0000103If Memcheck (the memory checker) shows any invalid reads, invalid writes
104and invalid frees in your program, the above may happen. Reason is that
105your program may trash Valgrind's low-level memory manager, which then
106dies with the above assertion, or something like this. The cure is to
107fix your program so that it doesn't do any illegal memory accesses. The
108above failure will hopefully go away after that.
njn4e59bd92003-04-22 20:58:47 +0000109
sewardj36a53ad2003-04-22 23:26:24 +0000110-----------------------------------------------------------------
njn4e59bd92003-04-22 20:58:47 +0000111
nethercoteef0abd12004-04-10 00:29:58 +00001123.3. My program dies, printing a message like this along the way:
sewardj36a53ad2003-04-22 23:26:24 +0000113
nethercote31788872003-11-02 16:32:05 +0000114 disInstr: unhandled instruction bytes: 0x66 0xF 0x2E 0x5
sewardj36a53ad2003-04-22 23:26:24 +0000115
nethercoteef0abd12004-04-10 00:29:58 +0000116Older versions did not support some x86 instructions, particularly
117SSE/SSE2 instructions. Try a newer Valgrind; we now support almost all
118instructions. If it still happens with newer versions, if the failing
119instruction is an SSE/SSE2 instruction, you might be able to recompile
nethercote8deae812004-07-18 10:35:36 +0000120your program without it by using the flag -march to gcc. Either way,
nethercoteef0abd12004-04-10 00:29:58 +0000121let us know and we'll try to fix it.
sewardj36a53ad2003-04-22 23:26:24 +0000122
nethercote8deae812004-07-18 10:35:36 +0000123Another possibility is that your program has a bug and erroneously jumps
124to a non-code address, in which case you'll get a SIGILL signal.
125Memcheck/Addrcheck may issue a warning just before this happens, but they
126might not if the jump happens to land in addressable memory.
127
nethercoteef0abd12004-04-10 00:29:58 +0000128
129-----------------------------------------------------------------
1304. Valgrind behaves unexpectedly
131-----------------------------------------------------------------
132
njna11b9b02005-03-27 17:05:08 +00001334.1. My threaded server process runs unbelievably slowly on Valgrind.
nethercoteef0abd12004-04-10 00:29:58 +0000134 So slowly, in fact, that at first I thought it had completely
135 locked up.
sewardj03272ff2003-04-26 22:23:35 +0000136
nethercoteef0abd12004-04-10 00:29:58 +0000137We are not completely sure about this, but one possibility is that
138laptops with power management fool Valgrind's timekeeping mechanism,
139which is (somewhat in error) based on the x86 RDTSC instruction. A
140"fix" which is claimed to work is to run some other cpu-intensive
141process at the same time, so that the laptop's power-management
142clock-slowing does not kick in. We would be interested in hearing more
143feedback on this.
sewardj03272ff2003-04-26 22:23:35 +0000144
nethercoteef0abd12004-04-10 00:29:58 +0000145Another possible cause is that versions prior to 1.9.6 did not support
146threading on glibc 2.3.X systems well. Hopefully the situation is much
147improved with 1.9.6 and later versions.
sewardj03272ff2003-04-26 22:23:35 +0000148
149-----------------------------------------------------------------
150
njna11b9b02005-03-27 17:05:08 +00001514.2. My program uses the C++ STL and string classes. Valgrind
nethercoteef0abd12004-04-10 00:29:58 +0000152 reports 'still reachable' memory leaks involving these classes
153 at the exit of the program, but there should be none.
njnae34aef2003-08-07 21:24:24 +0000154
nethercoteef0abd12004-04-10 00:29:58 +0000155First of all: relax, it's probably not a bug, but a feature. Many
156implementations of the C++ standard libraries use their own memory pool
157allocators. Memory for quite a number of destructed objects is not
158immediately freed and given back to the OS, but kept in the pool(s) for
159later re-use. The fact that the pools are not freed at the exit() of
160the program cause Valgrind to report this memory as still reachable.
161The behaviour not to free pools at the exit() could be called a bug of
162the library though.
njnae34aef2003-08-07 21:24:24 +0000163
nethercoteef0abd12004-04-10 00:29:58 +0000164Using gcc, you can force the STL to use malloc and to free memory as
165soon as possible by globally disabling memory caching. Beware! Doing
166so will probably slow down your program, sometimes drastically.
njnae34aef2003-08-07 21:24:24 +0000167
nethercoteef0abd12004-04-10 00:29:58 +0000168- With gcc 2.91, 2.95, 3.0 and 3.1, compile all source using the STL
169 with -D__USE_MALLOC. Beware! This is removed from gcc starting with
170 version 3.3.
171
njn8a5ad762005-05-12 13:45:56 +0000172- With gcc 3.2.2 and later, you should export the environment variable
nethercoteef0abd12004-04-10 00:29:58 +0000173 GLIBCPP_FORCE_NEW before running your program.
174
njn8a5ad762005-05-12 13:45:56 +0000175- With gcc 3.4 and later, that variable has changed name to
176 GLIBCXX_FORCE_NEW.
177
nethercoteef0abd12004-04-10 00:29:58 +0000178There are other ways to disable memory pooling: using the malloc_alloc
179template with your objects (not portable, but should work for gcc) or
180even writing your own memory allocators. But all this goes beyond the
181scope of this FAQ. Start by reading
182http://gcc.gnu.org/onlinedocs/libstdc++/ext/howto.html#3 if you
183absolutely want to do that. But beware:
184
1851) there are currently changes underway for gcc which are not totally
186 reflected in the docs right now ("now" == 26 Apr 03)
187
1882) allocators belong to the more messy parts of the STL and people went
189 at great lengths to make it portable across platforms. Chances are
190 good that your solution will work on your platform, but not on
191 others.
192
193-----------------------------------------------------------------------------
njna11b9b02005-03-27 17:05:08 +00001944.3. The stack traces given by Memcheck (or another tool) aren't helpful.
nethercoteef0abd12004-04-10 00:29:58 +0000195 How can I improve them?
196
197If they're not long enough, use --num-callers to make them longer.
198
199If they're not detailed enough, make sure you are compiling with -g to add
200debug information. And don't strip symbol tables (programs should be
201unstripped unless you run 'strip' on them; some libraries ship stripped).
202
njn0211ff32005-05-15 14:49:24 +0000203Also, for leak reports involving shared objects, if the shared object is
204unloaded before the program terminates, Valgrind will discard the debug
205information and the error message will be full of "???" entries. The
206workaround here is to avoid calling dlclose() on these shared objects.
207
nethercoteef0abd12004-04-10 00:29:58 +0000208Also, -fomit-frame-pointer and -fstack-check can make stack traces worse.
209
210Some example sub-traces:
211
212 With debug information and unstripped (best):
213
214 Invalid write of size 1
215 at 0x80483BF: really (malloc1.c:20)
216 by 0x8048370: main (malloc1.c:9)
217
218 With no debug information, unstripped:
219
220 Invalid write of size 1
221 at 0x80483BF: really (in /auto/homes/njn25/grind/head5/a.out)
222 by 0x8048370: main (in /auto/homes/njn25/grind/head5/a.out)
223
224 With no debug information, stripped:
225
226 Invalid write of size 1
227 at 0x80483BF: (within /auto/homes/njn25/grind/head5/a.out)
228 by 0x8048370: (within /auto/homes/njn25/grind/head5/a.out)
229 by 0x42015703: __libc_start_main (in /lib/tls/libc-2.3.2.so)
230 by 0x80482CC: (within /auto/homes/njn25/grind/head5/a.out)
231
232 With debug information and -fomit-frame-pointer:
233
234 Invalid write of size 1
235 at 0x80483C4: really (malloc1.c:20)
236 by 0x42015703: __libc_start_main (in /lib/tls/libc-2.3.2.so)
237 by 0x80482CC: ??? (start.S:81)
238
njn0211ff32005-05-15 14:49:24 +0000239 A leak error message involving an unloaded shared object:
240
241 84 bytes in 1 blocks are possibly lost in loss record 488 of 713
242 at 0x1B9036DA: operator new(unsigned) (vg_replace_malloc.c:132)
243 by 0x1DB63EEB: ???
244 by 0x1DB4B800: ???
245 by 0x1D65E007: ???
246 by 0x8049EE6: main (main.cpp:24)
247
njn16eeb4e2005-06-16 03:56:58 +0000248-----------------------------------------------------------------------------
2494.4. The stack traces given by Memcheck (or another tool) seem to
250 have the wrong function name in them. What's happening?
251
252Occasionally Valgrind stack traces get the wrong function names.
253This is caused by glibc using aliases to effectively give one function
254two names. Most of the time Valgrind chooses a suitable name, but
255very occasionally it gets it wrong.
256
257Examples we know of are printing 'bcmp' instead of 'memcmp', 'index'
258instead of 'strchr', and 'rindex' instead of 'strrchr'.
259
nethercoteef0abd12004-04-10 00:29:58 +0000260-----------------------------------------------------------------
2615. Memcheck doesn't find my bug
262-----------------------------------------------------------------
263
2645.1. I try running "valgrind --tool=memcheck my_program" and get
265 Valgrind's startup message, but I don't get any errors and I know
266 my program has errors.
njna11b9b02005-03-27 17:05:08 +0000267
268There are two possible causes of this.
nethercoteef0abd12004-04-10 00:29:58 +0000269
njna11b9b02005-03-27 17:05:08 +0000270First, by default, Valgrind only traces the top-level process. So if your
nethercoteef0abd12004-04-10 00:29:58 +0000271program spawns children, they won't be traced by Valgrind by default.
272Also, if your program is started by a shell script, Perl script, or
273something similar, Valgrind will trace the shell, or the Perl
274interpreter, or equivalent.
275
276To trace child processes, use the --trace-children=yes option.
277
278If you are tracing large trees of processes, it can be less disruptive
279to have the output sent over the network. Give Valgrind the flag
nethercotef8548672004-06-21 12:42:35 +0000280--log-socket=127.0.0.1:12345 (if you want logging output sent to port
nethercoteef0abd12004-04-10 00:29:58 +000028112345 on localhost). You can use the valgrind-listener program to
282listen on that port:
283
284 valgrind-listener 12345
285
286Obviously you have to start the listener process first. See the
287documentation for more details.
njnae34aef2003-08-07 21:24:24 +0000288
njna11b9b02005-03-27 17:05:08 +0000289Second, if your program is statically linked, most Valgrind tools won't
290work as well, because they won't be able to replace certain functions,
291such as malloc(), with their own versions. A key indicator of this is
292if Memcheck says:
293
294 No malloc'd blocks -- no leaks are possible
295
296when you know your program calls malloc(). The workaround is to avoid
297statically linking your program.
298
njnae34aef2003-08-07 21:24:24 +0000299-----------------------------------------------------------------
300
nethercoteef0abd12004-04-10 00:29:58 +00003015.2. Why doesn't Memcheck find the array overruns in this program?
302
303 int static[5];
304
305 int main(void)
306 {
307 int stack[5];
308
309 static[5] = 0;
310 stack [5] = 0;
311
312 return 0;
313 }
314
315Unfortunately, Memcheck doesn't do bounds checking on static or stack
316arrays. We'd like to, but it's just not possible to do in a reasonable
317way that fits with how Memcheck works. Sorry.
njn1aa18502003-08-15 07:35:20 +0000318
nethercoteef0abd12004-04-10 00:29:58 +0000319
320-----------------------------------------------------------------
3216. Miscellaneous
322-----------------------------------------------------------------
323
3246.1. I tried writing a suppression but it didn't work. Can you
325 write my suppression for me?
326
327Yes! Use the --gen-suppressions=yes feature to spit out suppressions
328automatically for you. You can then edit them if you like, eg.
329combining similar automatically generated suppressions using wildcards
330like '*'.
331
332If you really want to write suppressions by hand, read the manual
333carefully. Note particularly that C++ function names must be _mangled_.
334
335-----------------------------------------------------------------
336
3376.2. With Memcheck/Addrcheck's memory leak detector, what's the
338 difference between "definitely lost", "possibly lost", "still
339 reachable", and "suppressed"?
340
341The details are in section 3.6 of the manual.
342
343In short:
344
345 - "definitely lost" means your program is leaking memory -- fix it!
346
347 - "possibly lost" means your program is probably leaking memory,
348 unless you're doing funny things with pointers.
349
350 - "still reachable" means your program is probably ok -- it didn't
351 free some memory it could have. This is quite common and often
352 reasonable. Don't use --show-reachable=yes if you don't want to see
353 these reports.
354
355 - "suppressed" means that a leak error has been suppressed. There are
356 some suppressions in the default suppression files. You can ignore
357 suppressed errors.
njna8fb5a32003-08-20 11:19:17 +0000358
359-----------------------------------------------------------------
360
njn4e59bd92003-04-22 20:58:47 +0000361(this is the end of the FAQ.)