在 CentOS 上调试 Rust 程序,常用方法和步骤如下:
sudo yum install -y gcc gdb
如果是 CentOS 8 / Stream:
sudo dnf install -y gcc gdb
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
cargo build
生成的可执行文件在:
target/debug/<程序名>
✅ 不要使用 cargo build --release 调试,优化会打乱变量和行号。
gdb target/debug/<程序名>
run # 运行程序
break main # 在 main 函数打断点
break 文件名:行号 # 如 break main.rs:10
next # 单步(不进入函数)
step # 单步(进入函数)
print 变量名 # 打印变量
backtrace # 查看调用栈
continue # 继续执行
quit # 退出
launch.json{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug Rust",
"program": "${workspaceFolder}/target/debug/<程序名>",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
然后按 F5 即可调试。
println!println!("x = {}", x);
cargo add log env_logger
fn main() {
env_logger::init();
log::info!("启动程序");
}
运行:
RUST_LOG=info cargo run
RUST_BACKTRACE=1 cargo run
cargo build --features debug
dbg! 宏let x = dbg!(some_function());
CodeLLDB 替代 GDB如果你愿意,可以告诉我:
我可以给你更具体的调试方案。