CD86
CD86

Reputation: 1079

debug c++ application with bazel and gdb

I am a bit confused. There seem to be several options when it comes to debugging a bazel-built app with gdb:

  1. dazel run --config=relwithdebinfo --run_under=gdb

  2. dazel run --compilation_mode=dbg --run_under=gdb

this puts me in gdb but only the second option seems to enable a real debug mode in that the entire application runs slower and writes debug information to the terminal which the first option does not.

Can somebody please clarify when to use what option?

also, it seems that bazel prevents me from executing the application standalone, i.e. with ./app when build via

dazel build ...

instead of using

dazel run ...

why is that?

Upvotes: 0

Views: 3842

Answers (1)

ahumesky
ahumesky

Reputation: 5006

A few things that may help you to figure out what's going on:

  1. --config is a way to use groups of other Bazel command-line flags in a convenient way. These groups are defined in bazelrc files. So in some bazelrc file there's a definition of relwithdebinfo that adds some other flags. You can use --announce_rc to have Bazel print each config and bazelrc it's using.
    See:
    https://docs.bazel.build/versions/main/user-manual.html#flag--config
    https://docs.bazel.build/versions/main/guide.html#bazelrc-the-bazel-configuration-file

  2. --compilation_mode is separate from --config. Compiler toolchains in Bazel can define different modes, and the flags to use with those modes. See https://docs.bazel.build/versions/main/user-manual.html#flag--compilation_mode

    With the default C++ toolchain, when --compilation_mode is dbg, it adds -g: https://github.com/bazelbuild/bazel/blob/b03bb844b10d5d0d29f5859293e91b5bd739c64f/tools/cpp/cc_toolchain_config.bzl#L659-L660

    When it's opt, it adds other flags: https://github.com/bazelbuild/bazel/blob/b03bb844b10d5d0d29f5859293e91b5bd739c64f/tools/cpp/cc_toolchain_config.bzl#L675-L686

    One could add these flags like -g manually with --cxxopt (and even create configs like build:dbg --cxxopt=-g in a bazelrc, then do --config=dbg on Bazel's command line), but it's less portable. (E.g. the toolchain also defines compiler flags for --compilation_mode=dbg for windows: https://github.com/bazelbuild/bazel/blob/master/tools/cpp/cc_toolchain_config.bzl#L850-L855)

  3. For dazel build vs dazel run, the problem could be application-specific. For example, with run, files in the data attributes of targets are made available, whereas with build they are not (since resource packaging is application / language / domain specific). So the application might be making assumptions about how it's being run or where its dependencies are. Or it could be something specific with dazel, I'm not really familiar with it. With more information on what's going wrong, it might be possible to diagnose further.

Upvotes: 1

Related Questions