I want to debug an executable generated with Bazel. The gdb debugger is lost with the links generated by Bazel and is not able to show me the C++ source code. How to fix that?
The project root directory is /home/.../Cpp/
./Cpp/
├── bazel-bin -> /home/picaud/.cache/bazel/_bazel_picaud...
├── bazel-Cpp -> /home/picaud/.cache/bazel/_bazel_picaud...
├── bazel-genfiles -> /home/picaud/.cache/bazel/_bazel_picaud...
├── bazel-out -> /home/picaud/.cache/bazel/_bazel_picaud...
├── bin
│ ├── BUILD
│ └── main.cpp
├── MyLib
│ ├── BUILD
│ ├── ....hpp
│ ├── ...cpp
└── WORKSPACE
Gdb is a debugger for C (and C++). It allows you to do things like run the program up to a certain point then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line.
The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, C, C++, Objective-C, Free Pascal, Fortran, Go, and partially others.
Debugging is a methodical process of finding and reducing the number of bugs (or defects) in a computer program, thus making it behave as originally expected.
The first step is to generate executables using the debug mode:
bazel build ... --compilation_mode=dbg -s
(the -s option is not mandatory it only shows the executed commands, you can remove it if you want)
gdb debugging from the command line:
You can start gdb with this command (from your project root directory):
gdbtui bazel-bin/bin/main
-> everything is okay, you should see your C++ source code.
The error would be to do:
cd bazel-bin/bin/
gdbtui main
In that case, because of the links, gdb is not able to retrieve the source code.
gdb debugging from Emacs:
Do as usual
M-x gdb
In the emacs prompt define the complete absolute path to the executable:
gdb -i=mi /home/picaud/.../Cpp/bazel-bin/bin/main
Now in the gdb buffer you must tell gdb where to find source by defining your absolute path to the project root directory (where your WORKSPACE file is):
set directories /home/picaud/.../Cpp
Now the emacs gdb command should work properly and you can debug as usual.
(well this was an easy fix, just a note that maybe can help...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With