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

拥有回忆 提交于 2020-01-29 10:53:36

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/45812725/c-debugging-with-gdb-bazel-emacs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!