Valgrind: suppress memory memory leak reporting iff program crashed - valgrind

Let's assume we have automated Valgrind testing setup with maximum diagnostics configured (--leak-check=yes --track-origins=yes etc.) However, on some tests the valgrinded process might crash with SIGSEGV due to some nondeterministic bug. In that case, the immediate diagnostics leading to SIGSEGV are useful, but Valgrind does not stop there and dumps every single live memory allocation as a leak, tens of thousands of them - which are useless in the case of random SIGSEGV.
Is there a way to configure Valgrind to
print any errors during the program execution and do not abort;
on regular exit, dump memory leaks;
on SIGSEGV/SIGABRT/SIGILL/SIG... exit, do not dump memory leaks?
EDIT: this has been logged at Valgrind bug tracker in 2011: https://bugs.kde.org/show_bug.cgi?id=265371

valgrind leak search cannot be activated only for 'program normal exits'
and de-activated for 'crash exits'.
You might maybe obtain this result by using --vgdb-error=0,
then (using a script): use gdb+vgdb to connect to the valgrind gdbserver,
catch the signals you want in gdb, and have gdb killing the process when
the signal is received: the valgrind gdbserver has a command v.kill that will
make valgrind exit without doing its usual 'end of life' leak search.

Related

Memory allocation fails when using Valgrind

I am trying to debug an embedded application with Valgrind.
Unfortunately, this application behaves differently than when I run it without Valgrind. At one point a driver allocates a data block of about 4 MB. This allocation fails even though there is still about 90 MB of memory available. Could it be that Valgrind fragments the memory so much that no contiguous block of that size is available anymore?
Does anyone have an idea how to remedy this?

Is it possible to set a baseline memory usage in valgrind for leak detection?

Is there a way to tell valgrind from inside my code when to start and when to stop checking for memory leaks?
I am using a legacy testing framework which must link with my testing program in order to run. The framework has memory leaks in it - valgrind shows about 50KB of memory that has not been released, but is reachable via heuristic. This is annoying, because I must keep this number in mind to see how much memory is leaked from my code. It would be a lot more convenient if I could tell valgrind to start collecting memory stats when my first test begins, and stop collecting when the last test is over. Is there an API for it?
valgrind memcheck allows to do a "differential" leak search. The differential leak search reports the delta between the previous leak search and the current situation.
You can do such a differential leak search using monitor commands with vgdb, either from the shell or from gdb. See https://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.monitor-commands.
You can also use the client request VALGRIND_DO_CHANGED_LEAK_CHECK from your program, see https://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.clientreqs.

How to measure Valgrind's memory usage?

I have an application written in C which uses the zmalloc (borrowed from Redis) memory wrapper to keep track of the total dynamic allocated memory by my program. I am also using Valgrind on Linux to find memory leaks and invalid memory accesses.
The problem is that zmalloc and top show totally different memory usage reports when I am using Valgrind. This makes me think that Valgrind itself is consuming too much memory.
How do I measure Valgrind's memory usage?
valgrind tools such as memcheck or helgrind use a lot of memory for
tracking various aspects of your program.
So, it is normal that top shows a lot more memory than what your program
allocates itself.
If you want to have an idea about the memory used by valgrind, you can do:
valgrind --stats=yes ...
The lines following
------ Valgrind's internal memory use stats follow ------
will give some info about valgrind memory usage.
Use valgrind --profile-heap=yes ... to have detailed memory use.
Note that if you do not use the standard malloc library, you might need to use the option --soname-synonyms=... to have tools such as memcheck or helgrind working properly.
to

Working of the valgrind tool suite

I had run valgrind on a sample daemon program. The parent exits after allocating a chunk of 1000B, but the child that runs on the background keeps on allocating 200B of memory on the heap through malloc, after every two seconds.
My question is: does valgrind execute the program on the actual processor, or on a synthetic CPU?
Does it allocate the memory on the actual heap or on a synthetic RAm which doesn't exist?
Since I let the program run for a quite a long duration so much so that the child allocated some 2GB of memory on the heap. On implementing the program on massif, I got one output file for the parent, and on killing the daemon process, I got another massif.out. for the child which showed the allocation of the memory on the heap.
Valgrind run program in its own synthetic CPU, nothing from the program machine code reaches the host CPU.
Memory allocation is hooked with Memcheck, if you use it, otherwise Valgrind calls the libc memory allocation routines.
This facts may complicate Valgrind debugging of system services, indeed.
If you turn on the memcheck(which is the default), then Valgrind will manage the heap, i.e. all the memory related methods (malloc/free/memmove etc.) will be replaced by Valgrind's version of the corresponding methods.
As already told, your program is running on virtual CPU created and managed by valgrind.
There is no notion of synthetic RAM as far I know. In any case, all this is very transparent to the running process(your daemon) and shoudl not change the behavior of your program in any way.
So the answer is YES for synthetic CPU and no for synthetic RAM.

Safety of generating a threaddump on a production system

We have a production Java system which is using a lot more threads than usual. I would like to use kill -3 pid to get a threaddump, and if necessary get a binary heap dump using JConsole for offline analysis in Eclipse MAT.
I am running Java 1.5.0_10 on RHEL4.
How likely is it that either of these will kill the JVM? What about adverse effects on its performance while the dumps are produced?
It won't kill the VM, but generating a heap dump will likely freeze the JVM during the dump process, since it has to dump a consistent snapshot. Once the dump has finished, it'll resume all threads from where they were suspended. So it won't be destructive, but it'll briefly stop processing.
I use below command to get heap dump
jmap -heap pid
For details - http://docs.oracle.com/javase/6/docs/technotes/tools/share/jmap.html