Search code examples
valgrindbazel

Default output directory of bazel run targets - finding valgrind callgrind.out file


When using the 'bazel run' command to build and execute a target, the argument '--run_under=' can be used to prefix the execution with valgrind. Valgrind usually outputs its result file in the directory of the target, but when running with bazel run, the output file is nowhere to be found.

Example:

bazel run //path/to/package:target_name --copt="-g" --run_under='valgrind --tool=callgrind'

will run callgrind on my target:

INFO: Build completed successfully, 1 total action
INFO: Running command line: /bin/bash -c 'valgrind --tool=callgrind   /home/user/.cache/bazel/_bazel_user/8d1c200e573b8da3ee18c97f4656aca6/execroot/workspace/bazel-out/k8-opt/bin/path/to/package/s
INFO: Build completed successfully, 1 total action
==15166== Callgrind, a call-graph generating cache profiler
==15166== Copyright (C) 2002-2017, and GNU GPL'd, by Josef Weidendorfer et al.
==15166== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==15166== Command: /home/user/.cache/bazel/_bazel_user/8d1c200e573b8da3ee18c97f4656aca6/execroot/workspace/bazel-out/k8-opt/bin/path/to/package/target_name
... (further output omitted) ...

Usually the file 'callgrind.out' will be written into the current directory, but I cannot find it anywhere.


Solution

  • The output you give above seems incomplete. Callgrind output terminates e.g by the events captured.

    In any case, you can control where callgrind creates the resulting files using the option:

     --callgrind-out-file=<f>  Output file name [callgrind.out.%p]
    

    The given filename can start with a directory name. Do not forget to use %p if several processes will be launched (and also, use --trace-children=yes in such cases).