Debugging Rust with GDB and Visual Studio Code

Version Infomation

  • VS Code: 1.36.1
  • GDB: 7.11.1
  • rustc 1.36.0

Installing Steps

  • Install Rust.
  • Install C/C++ Extension of VS Code (Rust Extension is recommended though).
  • Use cargo new Project-folder to create a new project folder.
  • Open the project folder (not the src sub-folder of the project folder) with VS Code.

Debugging Example

  • Select main.rs file. Then press F5, and select “C++ (GDB/LLDB)”.
  • Modified the launch.json configuration file as the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
// 使用 IntelliSense 以得知可用的屬性。
// 暫留以檢視現有屬性的描述。
// 如需詳細資訊,請瀏覽: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "cppdbg",
"request": "launch",
"name": "(Linux) Launch",
"program": "${workspaceRoot}/target/debug/${workspaceFolderBasename}",
"args": [],
"cwd": "${workspaceRoot}",
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "Build",
}
]
}
  • Because we want to compile the project (cargo build) before debugging it, we should add a task to tasks.json:
1
2
3
4
5
6
7
8
9
10
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"command": "cargo",
"args": ["build"],
}
]
}

Debugging Demo

PS

The debugging function seems to be imcomplete.

References