Search code examples
c++profilingbazelpprof

Viewing source code in pprof with bazel built binary


I am trying to profile a binary that I've built under bazel using pprof. I am able to generate a profile, however when I view it in the web UI, I cannot see my code in the source code view. I see ??s instead.

Here are the commands I'm using:

CPUPROFILE=/tmp/cpu_profile bazel run --linkopt='-lprofiler' //my:binary

pprof -http=localhost:8000 -lines bazel-bin/my/binary /tmp/cpu_profile

How can I access the source code information?


Solution

  • By default, bazel builds binaries using the fastbuild compilation_mode which doesn't have all of the necessary debug information needed to link the source.

    To fix this, profile using dbg mode:

    CPUPROFILE=/tmp/cpu_profile bazel run -c dbg \
      --linkopt='-lprofiler' \
      //my:binary
    

    Or include debug information in an optimized build:

    CPUPROFILE=/tmp/cpu_profile bazel run -c opt \
      --copt=-g \
      --linkopt='-lprofiler' \
      //my:binary