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文件中添加断点,进行调试

相关推荐
V搜xhliang024617 小时前
AI智能体的数据安全与合规实践
人工智能·学习·数据分析·自动化·ai编程
无敌的牛17 小时前
redis学习过程
数据库·redis·学习
旅僧19 小时前
Π环境部署(运行 且 无理论讲解)
学习
jushi899919 小时前
Lucas Chess R国际象棋、中国象棋、日本将棋、五子棋训练学习工具游戏软件
学习
自传.20 小时前
尚硅谷 Vibe Coding|第一章 AI 编程基础理论 学习笔记
笔记·学习·尚硅谷·vibe coding
吃好睡好便好21 小时前
改变时间轴的跨度
学习·生活
fox_lht21 小时前
15.3.改进我们之前的输入、输出项目
开发语言·后端·学习·rust
chase。21 小时前
【学习笔记】SimpleVLA-RL:通过强化学习扩展 VLA 训练
笔记·学习
C语言小火车21 小时前
什么时候用智能指针?什么时候用裸指针?
c语言·c++·学习·指针
嵌入式小站1 天前
STM32 可移植教程 02:按键状态机,消抖、长按、释放一行也不用多写(实战篇)
chrome·vscode·stm32·单片机·嵌入式硬件