[英]Debugging C++ code does not work when built with Bazel on MacOS
我已經使用 Bazel 構建系統在 Visual Studio Code 中啟動了一個 C++ 項目。 但是當使用 Bazel 構建二進制文件時,調試在 IDE 中不起作用。
使用clang++ -g main.cpp -o sample
構建時,我可以調試應用程序。
我的設置:操作系統:MacOS,Bazel:發布 0.17.2-homebrew,VS Code:1.27.2
這是由 Bazel 編譯的方式引起的。 是否有任何解決方法可以使調試工作?
這是一個最小的例子。 只需在編輯器中放置一個斷點,運行調試,然后觀察一個斷點沒有被命中:
├── .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": {}
}
更新。 1
嘗試調試 bazel 構建的可執行文件並直接使用 lldb 手動構建:
Bazel 構建的二進制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.
手動構建的二進制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.
在可執行文件中查找“調試說明”后(感謝這個答案)
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
我發現這些路徑指向 Bazel 用來構建的沙盒環境。 而且這條路已經無法通行了。
這可能是由 Bazel #5545中的一個已知問題引起的
更新。 2
將 Bazel 更新到 0.17.2 版后(問題#5545已修復),此問題仍然存在。
更新。 3
最后證明是巴澤爾問題。 我在這里創建了一個問題#6327
(促進更新回答):看起來像一個已知問題: #6327
對於遇到此問題的其他任何人,以下內容對我有用(2022 年 3 月):
.bazelrc:
build:debug -c dbg
build:debug --features=oso_prefix_is_pwd
然后使用構建:
bazel build --config debug //project/path:name
.bazelrc 可以在您的主目錄中,也可以在與 bazel 的 WORKSPACE 文件相同的目錄中
讓 GDB 找到正確的文件。
.vscode/launch.json
{
// ...
"configurations": [
{
// ...
"preLaunchCommands": [
"directory ${workspaceFolder}" // or absolute path to your workspace
],
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.