Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breakpoints not hit when debugging Rust with gdb in Visual Studio Code

I started learning Rust, and I want to set up debugging in Visual Studio Code, but can't get breakpoints working. I use the Native Debug & RustyCode extensions for VS Code.

Here is my launch file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "gdb",
            "request": "launch",
            "target": "target/debug/test",
            "cwd": "${workspaceRoot}"
        }
    ]
}

But when I run this configuration, breakpoints do not get hit. I see in the debug console that the debugger started and the app ran fine, but there is a warning message "No Symbols loaded":

No symbol table is loaded.  Use the "file" command.
No symbol table is loaded.  Use the "file" command.
Running executable
[New Thread 32168.0x81e0]
[New Thread 32168.0x3360]
[New Thread 32168.0x61b8]
[New Thread 32168.0x8040]
The program "+ + * - /" calculates the value 1
[Thread 32168.0x61b8 exited with code 0]
[Thread 32168.0x3360 exited with code 0]
[Thread 32168.0x8040 exited with code 0]
[Inferior 1 (process 32168) exited normally]

Here is the source of the app I am using. How can I make breakpoints work?

like image 519
vmg Avatar asked Jan 04 '17 19:01

vmg


People also ask

How do I run breakpoints in GDB?

You can also set breakpoints on function names. To do this, just type "break [functionname]". gdb will stop your program just before that function is called. Breakpoints stay set when your program ends, so you do not have to reset them unless you quit gdb and restart it.

How do I debug Rust in Vscode?

The rust-analyzer extension has basic debugging support via the Rust Analyzer: Debug command available in the Command Palette (Ctrl+Shift+P) and the Run|Debug CodeLens in the editor.

Does GDB work with Rust?

GDB has Rust-like value and type output. It can print values and types in a way that look like Rust syntax in the output. Or when you print a type as ptype in GDB, it also looks like Rust source code. Checkout the documentation in the manual for GDB/Rust.

How do you hit breakpoints in Visual Studio code?

To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.


2 Answers

I was having the same problem until I realized I was using an msvc build of rustc.

GDB is only supposed to work with gnu builds, so if you use rustup, the solution is as simple as rustup default stable (or beta/nightly). GNU is default, for msvc you would need to do stable-msvc).

If you don't want to use rustup you should just re-install a gnu build of Rust manually.

Edit: As noted in a comment below, don't forget to re-build!

like image 125
VelocityRa Avatar answered Oct 13 '22 17:10

VelocityRa


I had similar problems with breakpoints and found that adding this line to the settings.json fixed it:

{
    "debug.allowBreakpointsEverywhere": true
}

I am also using Microsoft's C/C++ Debugger plugin, not the native one. It still works for GDB.

like image 44
locka Avatar answered Oct 13 '22 18:10

locka