如何在linux环境下使用gdb调试bazel构建生成的可执行二进制文件

问题描述 投票:0回答:1

有一个简单的源代码 hello_world.cpp,bazel 构建它以便使用 gdb 进行调试,如下所示:

$ bazel build --cxxopt=-std=c++11 -c dbg --strip=never //examples/hello_world:hello_world

这样我就可以调试了,一切都很顺利,源代码符号可以显示了:

$ gdb bazel-bin/examples/hello_world/hello_world
Reading symbols from bazel-bin/examples/hello_world/hello_world...done.
(gdb) b main
Breakpoint 1 at 0x51e9: file examples/hello_world/main.cpp, line 46.
(gdb) r
Starting program: /home/wss/.cache/bazel/_bazel_wss/4110822ab2eb09dee5980f588bc10e42/execroot/com_minieye_aiplorer/bazel-out/k8-dbg/bin/examples/hello_world/hello_world 

Breakpoint 1, main (argc=1, argv=0x7fffffffe1c8) at examples/hello_world/main.cpp:46
warning: Source file is more recent than executable.
46    std::cout << "WITH LINUX~~~" << std::endl;
(gdb) l
41    return 0;
42  }
43  
44  int main(int argc, char *argv[]) {
45  #ifdef WITH_LINUX_PC
46    std::cout << "WITH LINUX~~~" << std::endl;
47  #endif
48    if (jsonHandle(argc, argv) < 0) {
49      return -1;
50    }

但是当我来到 bazel-bin/examples/hello_world 和 gdb hello-world 时,它会显示如下:

$ cd bazel-bin/examples/hello_world/
$ gdb hello_world
(gdb) b main
Breakpoint 1 at 0x51e9: file examples/hello_world/main.cpp, line 46.
(gdb) l
32  examples/hello_world/main.cpp: No such file or directory
(gdb) l
32  in examples/hello_world/main.cpp

谁能告诉我发生了什么事?如果我想直接在可执行二进制文件的目录下继续调试,该怎么做?

debugging gdb symbols bazel
1个回答
0
投票

编译器不得将编译目录包含在调试信息中。相反,它只是存储源文件的相对路径。这就是您看到消息的原因:

examples/hello_world/main.cpp: No such file or directory

在不知道编译目录的情况下,GDB 只能根据当前工作目录查找文件。

但是,您可以在这里帮助 GDB。如果你这样做:

(gdb) directory /path/to/project/directory

然后GDB也会查找

/path/to/project/directory
中的文件——记住,这个目录应该是
examples/hello_world/main.cpp
有效的目录。

您可以使用

show directories
查看 GDB 正在使用哪些目录,或者使用
set directories
完全重写存储的目录列表(
directory
命令只是将新目录添加到列表中)。

© www.soinside.com 2019 - 2024. All rights reserved.