There is a simple source code hello_world.cpp, and bazel build it for debuging with gdb like this:
$ bazel build --cxxopt=-std=c++11 -c dbg --strip=never //examples/hello_world:hello_world
so I can debug it like this, and everything is going well, the source code symbols can display:
$ 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 }
But when I came to bazel-bin/examples/hello_world, and gdb hello-world, it'll show it as below:
$ 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
who can tell me what happned? If I want to go on to debug under the executable binary's directory directly, how to do it?
The compiler must not be including the compilation directory into the debug information. Instead it is just storing the relative path to the source files. This is why you see the message:
examples/hello_world/main.cpp: No such file or directory
Without knowing the compilation directory, GDB is stuck just looking for files based on the current working directory.
However, you can help GDB out here. If you do:
(gdb) directory /path/to/project/directory
Then GDB will also look for the files within /path/to/project/directory
-- remember, this directory should be the directory from which examples/hello_world/main.cpp
is valid.
You can use show directories
to see which directories GDB is using, or use set directories
to completely rewrite the stored directory list (the directory
command just adds a new directory to the list).