generating suppressions for memory leaks - valgrind

I want suppress Valgrind's reporting of some "definitely lost" memory by the library I'm using. I have tried valgrind --gen-suppressions=yes ./a but it only prompts for errors such as "conditional jump or move depends on uninitialized value".
Is there a way to generate suppressions for straight-up memory leaks? If not, is it difficult to write them by hand? Valgrind's manpage seems to discourage it, at least for errors.

Run valgrind with the --gen-suppressions=all and --log-file=memcheck.log options, and manually copy/paste the logged suppressions into the suppression file.
valgrind --leak-check=full --gen-suppressions=all --log-file=memcheck.log ./a
If you find the output is mixed with the application output then redirect valigrind output to separate file descriptor: --log-fd=9 9>>memcheck.log
valgrind --leak-check=full --gen-suppressions=all --log-fd=9 ./a 9>>memcheck.log

To be prompted for leaks that aren't generating errors, you have to run
valgrind --leak-check=full --gen-suppressions=yes ./a

There is a page on how you can generate such a file based on your errors https://wiki.wxwidgets.org/Valgrind_Suppression_File_Howto
It's not perfect, but you can start from it

You can write a suppression file of your own (but it doesn't seem obvious) :
--suppressions=<filename> [default: $PREFIX/lib/valgrind/default.supp]
If the question was to disable an entire library, see this.
Valgrind's man page.

Related

Show coresponding code line in valgrind (Clion) output

When analysing my code using valgrind (WSL) only output I get is list of problems found by it.
On the contrary in Clion documentation in valgrind section, images show that output (instruction pointer) can be directly interpreted into code line that has triggered it, as shown on the image below.
Whan do I need to do to toggle on this display mode or at least code line which triggered it, I am using CLion 2022.2.4?
I have already tried playing with diffrent flags but I was unable to toggle this view on.
This could be related to this bugzilla item (but not likely).
I tried it with CLion 2022.3.1 on FreeBSD 13.1. It was painful to get a project setup (clion didn't know where clangd or ninja were, and used a load of unrecognized clangd options).
After that no real problems.
I'm fairly certain none of the Valgrind devs uses WSL so the chances of this getting analyzed and fixed are very low.

Valgrind massif - inverse ignore?

Is there a parameter in valgrind massif that allows me to only track allocations made by certain functions and classes? I would like to make a run that only traces (de)allocations made by std::vector.
Regards
Using the option --xtree-memory=full, you might be able to visualise the stacktraces that are interesting you.
See http://www.valgrind.org/docs/manual/manual-core.html#manual-core.xtree for more details.

Configure valgrind to only do leak checks

I am trying to find a memory leak in a C library loaded by the JVM. Is it possible to configure valgrind to only do leak checks and none of the other checks. I ask because the JVM itself has a massive amount of memory errors detected by valgrind which I am not interested in.
I have attempted suppression but it seems like the JVM just keeps coming up with new errors.
Use the following option:
--undef-value-errors=no --show-mismatched-frees=no

How to ignore "possibly lost" in valgrind

I recently use valgrind with glib(with gobject), it doesn't work very well.
I have added G_SLICE=always-malloc G_DEBUG=gc-friendly in the command line,
but there's still many "possibly lost" reported by valgrind.
As I use valgrind in automated testsuit, so I add --error-exitcode=1,
but those "possibly lost" will make valgrind exit with 1, which will
case my test fail.
Does anyone know how to make valgrind not treat "possibly lost" as errors.
With valgrind 3.7.0, use:
--show-possibly-lost=no|yes show possibly lost blocks in leak check?
[yes]
Check https://live.gnome.org/Valgrind for tips on how to use Valgrind with glib/gtk+/gnome. You might be interested in the Suppressions's section.
Does anyone know how to make valgrind not treat "possibly lost" as errors.
Use --errors-for-leak-kinds=definite for this. See Valgrind User Manual, Memcheck Command-Line Options section

Different behaviour under valgrind vs normal execution?

I have a program witch is a xmpp client that connect to a server.
I use gloox library to do that.
When I run the program, it runs ok and connects to the server.
But when I run it under valgrind, the program never sends
<iq id='uid:4efa1893:327b23c6' type='set' from='user#server/ressource' xmlns='jabber:client'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>
to the server.
Had anybody experience such problem?
Are there any parameter I specially need to run valgrind with to make sure that it is the same environement as a normal program execution?
The very first question is: did Valgrind report any errors in the execution of your program?
If your program is well-defined, and Valgrind didn't report any errors in it, then the program is supposed to behave exactly the same way under Valgrind as without it (only slower); no special settings required.
It is somewhat more likely that Valgrind did report some errors, and if so, your program is likely not well-defined, in which case your question is mute -- your program doesn't work the same because it is not well-defined (i.e. depends on undefined behavior).