Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ debugging with gdb & bazel (& emacs)

Tags:

c++

emacs

gdb

bazel

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
like image 276
Picaud Vincent Avatar asked Aug 22 '17 08:08

Picaud Vincent


People also ask

Does GDB work for C?

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.

Is GDB only for C?

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.

What is debugging in C?

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.


1 Answers

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...)

like image 66
Picaud Vincent Avatar answered Oct 19 '22 17:10

Picaud Vincent