5

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

8
  • Pretty sure you'll have better luck building and/or getting an answer if you throw out visual-studio-code. Run bazel from the command line, and show us that command, so we have an idea how it's actually being executed. Also tell us what you've done with respect to getting bazel to use compile options that are useful to debug. Commented Oct 8, 2018 at 22:23
  • @Mark I've added pure Bazel command to the updated part Commented Oct 8, 2018 at 22:28
  • so the actual problem is that you have no debug symbols? you say it "does not work" but it looks to me like you are able to attach just fine albeit with no symbols. Commented Oct 8, 2018 at 22:42
  • @BradAllred I am able to attach debugger though the problem is that debugging in the IDE does not work Commented Oct 8, 2018 at 22:46
  • define "does not work" its completely unclear to me from you post. You have not provided an error at all. Commented Oct 8, 2018 at 22:50

3 Answers 3

3

For anyone else stumbling upon this issue, the following worked for me (March 2022):

.bazelrc:

build:debug -c dbg
build:debug --features=oso_prefix_is_pwd

Then build using:

bazel build --config debug //project/path:name

The .bazelrc can be either in your home directory, or in the same directory as bazel's WORKSPACE file

Sign up to request clarification or add additional context in comments.

1 Comment

It made a big difference! Before I was not able to get dbgger to stop at any breakpoints, not it can. The sad thing is vscode still cannot load source file, saying error: 'SourceRequest' not supported
2

(Promoting updates to answer): Looks like a known issue: #6327

Comments

0

Let GDB find the correct file.

.vscode/launch.json

{
    // ...
    "configurations": [
        {
            // ...
            "preLaunchCommands": [
                "directory ${workspaceFolder}" // or absolute path to your workspace
            ],
        }
    ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.