Linux上Rust调试环境配置指南
一 安装Rust工具链与验证
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh,安装完成后执行 source $HOME/.cargo/env 使环境变量生效。rustc --version、cargo --version。rustup update;查看本地文档:rustup doc。cargo build 即为调试信息完整的构建。二 安装系统级调试器与Rust增强组件
sudo apt install gdb lldb;在 CentOS/RHEL 系执行 sudo yum install gdb lldb。rustup component add rust-src llvm-tools-preview,然后使用 rust-gdb 或 rust-lldb 启动调试,可获得更好的Rust语法与类型支持。三 命令行调试步骤
cargo build(生成于 target/debug/)。rust-gdb target/debug/<your_binary>break main 或 break <function>,run(可带参数),next/step,print <var>,backtrace(或 bt),info locals,quit。rust-lldb target/debug/<your_binary>break set --name main 或 break set --name <function>,run,bt,frame variable,quit。run 后追加参数即可。四 在VSCode中一键调试
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "cargo",
"args": ["build"]
}
]
}
.vscode/launch.json(Linux,使用GDB)
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "gdb",
"preLaunchTask": "build",
"request": "launch",
"target": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
"cwd": "${workspaceFolder}",
"args": "" // 需要传入的参数写在此处
}
]
}
args 字段。五 常见问题与实用技巧
rust-gdb/rust-lldb 可获得更友好的Rust类型打印与枚举/结构体渲染。[profile.dev] 中设置:[profile.dev]
debug = true
overflow-checks = true
rustflags = ["-Z", "backtrace"]
strace -p <pid>、ltrace -p <pid> 辅助定位。println!、debug_assert!;更规范的做法是引入 log 与 env_logger 等日志库进行分层日志输出。