Run and analyze a program with a Valgrind tool, using the specified Valgrind and program options
valgrind general_options [--tool=tool_name [tool_options]] program_name program_options
QNX Neutrino
The valgrind utility lets you use a Valgrind tool to analyze a program's runtime behavior. The utility takes an executable binary and uses the selected tool to add instrumentation code, then runs the program on a synthetic CPU. This design means you don't have to recompile the program to analyze it with Valgrind. Because Valgrind simulates the execution of every machine instruction, the active tool analyzes the code not only in your program but also in all linked libraries, including the C library.
Your program binary and the libraries it uses must contain debug symbols if you want to see accurate file names and line numbers in the results. The exception is the libc library; its debug symbols must be provided to Valgrind for it to run at all. There are two methods of making debug symbols available, as explained in Providing debug symbols to Valgrind.
For Memcheck, it's recommended to use -O1 compiler optimization but no higher, because higher settings make the tool report false positives.
--memcheck:leak-check=yes
The online Valgrind reference describes all current tools in the distribution. Below, we provide examples of common use cases for Memcheck, Massif, Helgrind, and Cachegrind.
When possible, you should copy the Valgrind binary and libraries to your target's filesystem or to a location mounted on the target; modifying the image is complex and time-consuming (because you have to rebuild and redeploy it). Also, you must be sure that your target image has enough space to hold the Valgrind files in addition to the rest of the image content.
/usr/bin/valgrind=${QNX_TARGET}/${PROCESSOR}/usr/bin/valgrind
/usr/lib/valgrind=${QNX_TARGET}/${PROCESSOR}/usr/lib/valgrind
In general, you should store the binary in /usr/bin/valgrind and the libraries and error suppression files in /usr/lib/valgrind. If your target uses a different directory structure, you must specify the alternative locations that store these files in the PATH and VALGRIND_LIB variables. Note that you must export their settings (e.g., export VALGRIND_LIB=path) to make them visible to Valgrind.
For general information on modifying your BSP buildfile and building your target image, see Building Embedded Systems or your BSP User's Guide.
[+raw] libc.so
For details about determining which shared object library files you need, see the Shared libraries section of Building Embedded Systems.
For the second method, you can copy the debug symbol (.sym) files to either the same target directory where you intend to run the program (i.e., the working directory) or a directory in the LD_LIBRARY_PATH or QNX_TARGET environment variables (to use mounted locations). The valgrind binary looks for these files at startup, and always looks in the working directory before searching those other paths. You can also store the symbol files in a directory named in the extra-debuginfo-path option.
If you use LD_LIBRARY_PATH, you must store the symbol files in the same directory as the libraries themselves (i.e., the .so files), but they can be in any directory listed in the variable definition. With QNX_TARGET, you must store the files in a subdirectory within the single path listed in this variable, and this subdirectory must be named for the target architecture. For instance, if QNX_TARGET is /var/tmp and your target is an x86_64 system, the proper path for storing the files is /var/tmp/x86_64.
When setting either of these environment variables, you must export the setting (e.g., export QNX_TARGET=path) to make it visible to Valgrind.
For QNX SDP components delivered as libraries (e.g., audio drivers, video capture), the symbol files are available from the QNX Software Center. Some component packages contain the stripped libraries and the debug symbol files. For others, there's a separate package for the symbols. Typically, symbol packages are automatically installed, but you should confirm that the symbols you need are installed. For details, see the QNX Software Center User's Guide, available from the QNX download center.
valgrind --log-file=cpu_burner.log --time-stamp=yes -v -v cpu_burner
Run the valgrind binary to analyze the cpu_burner program. Because the --tool option isn't specified, the default tool, Memcheck, is used for analysis. Valgrind preceeds each output message with a timestamp and sends the messages enabled for two levels of verbosity to the cpu_burner.log file in the same directory in which it runs.
valgrind --trace-children=yes --child-silent-after-fork=yes NeutrinoThreads_Example -t 4
Run the default tool of Memcheck on the NeutrinoThreads_Example program, while tracing into the images of child processes and outputting their debug and log messages only after exec*() is called, not right after fork(). No log file is named, so the output messages are directed to the terminal from where Valgrind is run. Here, the program is given one option (-t, which is set to 4 to request four threads).
valgrind --suppressions=$HOME/glibc_glitches.supp --track-origins=yes --undef-value-errors=yes ip_cam --res=low
Pass a resolution (res) setting of low onto ip_cam and run Memcheck on this program. Any errors listed in the specified suppressions file will not be outputted. The two tool-specific options tell Memcheck to track and report the origins of any uninitialized memory used by the program.
valgrind --tool=memcheck --show-leak-kinds=definite,indirect,possible --leak-check=full PeaksAndValleys
Perform a thorough leak check on PeaksAndValleys, looking for definite, indirect, and possible leaks with Memcheck and reporting details about each leak. Although Memcheck is the default tool, it can be useful to name it explicitly if you're running Valgrind test cases from a script file and you want to easily find commands that use this tool.
valgrind --tool=massif --time-unit=B --massif-out-file=cpu_burner_heap.log cpu_burner
Run Massif to analyze the heap usage of cpu_burner. Use bytes, not machine instructions, as the time unit to mark execution progress, and direct the output messages from Massif to the cpu_burner_heap.log file in the same directory in which the tool (and the program) runs.
valgrind --tool=massif --time-unit=B --max-snapshots=50 --detailed-freq=5 ip_cam --res=med
Pass a resolution (res) setting of medium (med) to ip_cam and run Massif on this program, using bytes as the time unit. The Valgrind tool will keep at most 50 heap snapshots in the results and will perform a detailed heap analysis every fifth snapshot.
valgrind --tool=helgrind --history-level=full --conflict-cache-size=5000000 road_race -t 4 -h 4
Find the causes of data races in a multithreaded program using Helgrind. To uncover many potential data races by showing many stack trace entries, the access history cache size is increased to 5,000,000 (five million) entries, or five times its default size. The program being analyzed is named road_race, and it takes -t and -h arguments to create 4 tortoise and 4 hare threads.
valgrind --log-file=road_race.log -v -v --tool=helgrind --history-level=none --track-lockorders=yes road_race -t 2 -h 2
Use Helgrind to find thread synchronization problems other than data races, such as misuses of the pthreads API and inconsistent lock ordering. Here, Valgrind writes the messages enabled for two levels of verbosity to road_race.log in the same directory in which it runs.
valgrind --tool=cachegrind --branch-sim=yes --cachegrind-out-file=%q{HOME}/cachegrind.out.%p HTMLGears
Run Cachegrind on the HTMLGears program and report branch counts in addition to cache counts (which are reported by default). The analysis output is directed to a file with a name ending with the PID of the Valgrind process and stored in the $HOME directory.
valgrind --tool=cachegrind --I1=65536,2,256 --D1=524288,4,512 HTMLGears
Run Cachegrind on HTMLGears using a simulated first-level instruction cache of 65536 bytes, 2-way associativity, and a 256-byte line size, and first-level data cache of 524288 bytes, 4-way associativity, and a 512-byte line size. Because --LL isn't specified, Cachegrind uses the CPUID instruction to determine the configuration of the last-level cache. The default output file is used; this file is named cachegrind.out.PID and is written to the directory in which the program runs.