QEMU学习之路(11)— 使用VSCode调试qemu-system-riscv64

QEMU学习之路(11)--- 使用VSCode调试qemu-system-riscv64

一、前言

VSCode调试参考:Visual Studio Code (VSCode) 中使用 GDB 进行调试

二、安装QEMU

使用源码安装qemu,参考:QEMU学习之路(3)--- qemu-system-riscv64安装

三、调试qemu程序

使用VSCode打开qemu源码工程,创建launch.json 文件如下所示:

javascript 复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "QEMU Debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "/opt/qemu/bin/qemu-system-riscv64",
            "args": [
                "-machine", "virt",
                "-m", "128M",
                "-nographic",
                "-bios", "/home/vbox/project/learn/riscv64/01_hello/build/bootrom.bin"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "logging": {
                "engineLogging": false
            }
        }

    ]
}

hw/char/serial.c文件中添加断点,进行调试

四、调试RISCV程序

使用VSCode打开riscv源码工程,创建launch.json 文件如下所示:

javascript 复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "RISC-V QEMU Debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/bootrom.elf",      // 待调试的 ELF 文件路径
            "args": [],
            "stopAtEntry": true,                                    // 启动后立即停在程序入口(main/_start)
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",                                        // 调试器模式为 GDB
            "miDebuggerPath": "riscv64-unknown-linux-gnu-gdb",      // GDB调试器路径
            "miDebuggerServerAddress": "localhost:1234",            // 连接 QEMU 的 GDB 端口
            "preLaunchTask": "qemu-riscv64-debug",                  // 关联 tasks.json 中的任务名称
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set architecture for RISC-V 64",
                    "text": "set architecture riscv:rv64",
                    "ignoreFailures": true
                }
            ],
            "logging": {
                "engineLogging": false
            }
        }
    ]
}

创建tasks.json 文件如下所示:

javascript 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "qemu-riscv64-debug",                          // 任务名称,在 launch.json 会引用
            "type": "shell",
            "command": "/opt/qemu/bin/qemu-system-riscv64",         // QEMU 可执行文件路径
            "args": [
                "-machine", "virt",
                "-m", "128M",
                "-bios", "${workspaceFolder}/build/bootrom.elf",
                "-nographic",
                "-s",                                               // 等价于 -gdb tcp::1234,监听 1234 端口
                "-S"                                                // 启动后暂停,等待 GDB 连接
            ],
            "problemMatcher": [],
            "isBackground": true,                                   // 标记为后台任务(VSCode 不阻塞)
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "dedicated"                                // 专用终端面板,避免输出混乱
            }
        }
    ]
}

source/main.c文件中添加断点,进行调试

相关推荐
xskukuku1 天前
VSCode中的Codex插件如何调用第三方API
vscode·ai·codex
我的xiaodoujiao1 天前
API 接口自动化测试详细图文教程学习系列16--项目实战演练3
python·学习·测试工具·pytest
复利人生 复利日知录 赋能循环1 天前
2026年复利精进:我的每日觉醒与成长密码
学习·思维模型·知识复利·复利·独立
sakiko_1 天前
UIKit学习笔记4-使用UITableView制作滚动视图
笔记·学习·ios·swift·uikit
晓梦林1 天前
MAZESEC-X1靶场学习笔记
笔记·学习
我的xiaodoujiao1 天前
API 接口自动化测试详细图文教程学习系列15--项目实战演练2
python·学习·测试工具·pytest
TImCheng06091 天前
职场人AI学习周期评估:不同学习路径的时间成本
人工智能·学习
周末也要写八哥1 天前
C4D/Cinema 4D 2026超详细下载与安装教程(附资源包)
学习·数学建模
xinzheng新政1 天前
openclaw dashboard报错 internal server error
学习
nashane1 天前
HarmonyOS 6学习:RichEditor宽度“暴力”计算与富文本截图避坑
学习·harmonyos 5