I have started a C++ project in Visual Studio Code with Bazel build system. But debugging does not work in the IDE when the binary is built using Bazel.
I can debug application when built with clang++ -g main.cpp -o sample
.
My setup: OS: MacOS, Bazel: release 0.17.2-homebrew, VS Code: 1.27.2
Which is caused by the way Bazel compiles. Is there any workaround to make debugging work?
Here is a minimal example. Just put a breakpoint in the editor, run debugging, and observe a breakpoint not being hit:
├── .vscode
│ ├── launch.json
│ └── tasks.json
├── BUILD
├── WORKSPACE
├── main.cpp
└── sample.code-workspace
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/bazel-bin/sample",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "bazel build",
}
]
}
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "bazel build",
"type": "shell",
"command": "bazel",
"args": [
"build",
"--compilation_mode=dbg",
"sample"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
BUILD
cc_binary(
name = "sample",
srcs = ["main.cpp"],
visibility = ["//main:__pkg__"],
)
main.cpp
#include <iostream>
int main() {
std::cout << "tests" << std::endl;
return 0;
}
sample.code-workspace
{
"folders": [ {"path": "."} ],
"settings": {}
}
Upd. 1
Tried debugging bazel-built executable and manually-built directly with lldb:
Bazel-built binary bazel build --compilation_mode=dbg sample
lldb bazel-bin/sample
(lldb) breakpoint set -n main
Breakpoint 1: 13 locations.
(lldb) r
Process 24391 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x00000001000021b0 sample`main
sample`main:
-> 0x1000021b0 <+0>: pushq %rbp
0x1000021b1 <+1>: movq %rsp, %rbp
0x1000021b4 <+4>: subq $0x20, %rsp
0x1000021b8 <+8>: movq 0xe51(%rip), %rdi ; (void *)0x00007fff9c93a660: std::__1::cout
Target 0: (sample) stopped.
Manually-built binary clang++ -g main.cpp -o sample
lldb sample
(lldb) breakpoint set -n main
Breakpoint 1: where = sample`main + 29 at main.cpp:4, address = 0x0000000100000f9d
(lldb) r
Process 24410 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100000f9d sample`main at main.cpp:4
1 #include <iostream>
2
3 int main() {
-> 4 std::cout << "tests" << std::endl;
5 return 0;
6 }
Target 0: (sample) stopped.
After looking up the "debug notes" in the executable (thanks to this answer)
nm -pa bazel-bin/sample | grep OSO
000000005bb7c96b - 03 0001 OSO /private/var/tmp/_bazel_teivaz/1e731ee5ae5a3ce6177976984318ec76/bazel-sandbox/1688134534644271312/execroot/__main__/bazel-out/darwin-dbg/bin/_objs/sample/main.o
I figured out that these paths point to the sandbox environment that Bazel used to build. And this path is no longer accessible.
This is likely caused by a known issue in Bazel #5545
Upd. 2
After updating Bazel to version 0.17.2 (where the issue #5545 is already fixed) this issue still occurs.
Upd. 3
In the end it turns out to be the Bazel problem. I have created an issue here #6327