vscode 在终端使用gdb,如何如何用gdb单步执行程序
时间: 2024-09-16 21:00:53 浏览: 77
Visual Studio Code (VSCode) 配合 GDB(GNU调试器)可以让你在开发环境中有效地调试 C/C++ 代码。以下是使用 VSCode 调试器(C/C++ Extension)配置并单步执行程序的基本步骤:
1. **安装插件**:
确保已经安装了 "C/C++" 或 "CodeLLDB" 插件,这将提供对 GDB 的支持。可以在VSCode的 Extensions市场搜索安装。
2. **启动任务配置**:
在`tasks.json`文件中创建一个新的调试任务,例如:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": {},
"externalConsole": true, // 选择是否打开终端
"MIMode": "gdb", // 设置调试模式为GDB
"preLaunchTask": "build" // 如果有编译任务,需要指定
}
]
}
```
3. **设置断点**:
在源代码中设置你需要测试的断点,使用快捷键 `F9` 或通过调试工具栏添加。
4. **运行调试会话**:
使用快捷键 `F5` 或点击 Debug 工具栏中的绿色三角形图标开始调试。VSCode 会在终端启动 GDB 并连接到正在运行的程序。
5. **单步执行**:
- `F10`: 单步进入函数
- `F11`: 进入下一行
- `Step Out`: 出栈,直到离开当前函数
- `Step Over`: 当前行如果是函数调用,直接跳过该行执行函数
6. **查看变量值**:
在调试器控制台(通常是终端),你可以使用 `print` 或 `info locals` 来检查变量的值。
7. **结束调试**:
要退出调试会话,可以按下 `Ctrl+C` 或者在调试控制面板上关闭进程。
阅读全文
相关推荐


















