0

In Qt you can integrate valgrind to analyze your code. I'm using the Valgrind Function Profiler in the Analyze mode and hit the Start button. The problem is, that I have a huge startup sequence which I'm not interested in.

I found defines in the valgrind/callgrind.h that should help me:

  • CALLGRIND_START_INSTRUMENTATION
  • CALLGRIND_STOP_INSTRUMENTATION
  • CALLGRIND_DUMP_STATS

According to this article I have to execute valgrind with the following options:

valgrind --tool=callgrind --instr-atstart=no ./application

But how do I do that within Qt? I still want to use the nice GUI and navigation. Thanks!

FrozenTarzan
  • 834
  • 10
  • 30

2 Answers2

1

Assume you have a project with a "main.cpp" and a sub-folder "build". Create a text file called "my_valgrind.sh" next to the "main.cpp" and put the following lines in it:

#!/bin/bash
valgrind --instr-atstart=no $@ ./build/[...]

replace the [...] with the name of your executable. Qt provides the remaining arguments through the $@ Afterwards make the sh file run-able: Rightclick on the .sh file->Properties->Permissions->Allow Execution as Program (or use command line solution) Maybe you need to change the permissions ( chown [username] my_valgrind.sh )

In Qtcreator you go to the Tools->Options->Analyzer and replace the valgrind executable with the full path to the my_valgrind.sh file

Then it should work.

Important:

  • If you change the name of the generated executeable you have to change the .sh file.
  • When valgrind is running your program with this method, it is not started from the build folder but from the folder where the .sh file is in (one level up). Hence you have to rename relative paths like "../data/" to "data/"
abc
  • 11
  • 1
  • Hey abc, thanks for your answer! I created the script and chose to set the path to the exectuable absolute (should be no problem!?). My folder structure is a bit different: I have a *.pro and my main.cpp in a folder but my build folder is outside this folder. That's why I use the absolute path. When using the Valgrind Function Profiler I get two callgrind.out files (one at the start, one at the end). But QtCreator does not seem to visualize any of the results. The log says that "Analyzing finished" but all the dialogs are empty :-( – FrozenTarzan Mar 16 '16 at 09:59
0

You could try this approach which I used once. Create a bash script.

#!/bin/bash
valgrind --tool=callgrind --instr-atstart=no $@

make this an executable (chmod +x) and use this as the executable Qt Creator -> Tools -> Options -> Analyzer.

Just worth a try.

ramtheconqueror
  • 1,907
  • 1
  • 22
  • 35
  • I tried it but I get some weird errors: Profiling /home/tarzan/foo/build/application Failed to start program. Path or permissions wrong? Analyzing finished. Error: "/home/tarzan/valgrind_custom_settings.sh" could not be started: Exec format error – FrozenTarzan Oct 02 '15 at 10:18
  • My new approach is to use kcachegrind to visualize the results of callgrind. But I have problems, again. See my new [question](http://stackoverflow.com/questions/32905212/how-to-use-kcachegrind-and-callgrind-to-measure-only-parts-of-my-code) – FrozenTarzan Oct 02 '15 at 10:27
  • can you show the `valgrind_custom_settings.sh` content? – ramtheconqueror Oct 02 '15 at 10:29
  • valgrind --tool=callgrind --instr-atstart=no /home/tarzan/foo/build/application – FrozenTarzan Oct 02 '15 at 10:31
  • I have to admit that I tried: valgrind --tool=callgrind --instr-atstart=no $@ because I'm not that solid with the command line and some of the scripting tricks ;-) – FrozenTarzan Oct 02 '15 at 10:32