在 CentOS 上调试 Rust 程序,通常分三步:安装工具链 → 编译带调试信息 → 用调试器(gdb/lldb)或 IDE 配置。下面按常见场景说明。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
验证:
rustc --version
cargo --version
CentOS 默认一般没有 gdb:
sudo yum install -y gdb
# 或 CentOS 8 / Stream
sudo dnf install -y gdb
如果使用 lldb(Rust 官方更推荐,但 CentOS 支持一般):
sudo dnf install -y lldb
cargo build
生成文件在:
target/debug/your_program
✅ 默认已包含:
Cargo.toml:
[profile.dev]
debug = true
opt-level = 0
gdb target/debug/your_program
常用命令:
break main
run
next
step
print variable_name
backtrace
Rust 官方提供 pretty-printer:
rust-gdb target/debug/your_program
或:
rust-gdb --args target/debug/your_program arg1 arg2
✅ rust-gdb 能正确显示:
StringVecOptionResultlaunch.json{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug Rust",
"program": "${workspaceFolder}/target/debug/your_program",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
按 F5 即可调试。
确认:
file target/debug/your_program
应包含 with debug_info
使用:
cargo build --target x86_64-unknown-linux-gnu
info threads
thread 2
| 场景 | 推荐 |
|---|---|
| 命令行调试 | rust-gdb |
| 图形化调试 | VS Code + CodeLLDB |
| 服务器无 GUI | gdb + tmux |
如果你有 CentOS 具体版本(7/8/Stream) 或 使用场景(服务器 / 容器 / 嵌入式),我可以给你更精确配置。