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

相关推荐
LiuYaoheng2 小时前
问题记录:Android Studio Low memory
android·ide·android studio
却道天凉_好个秋2 小时前
音视频学习(九十七):自适应码率(ABR)
学习·音视频·abr
red_redemption2 小时前
自由学习记录(142)
学习
لا معنى له2 小时前
JEPA:联合嵌入预测架构介绍 ——学习笔记
笔记·学习
阳光永恒7362 小时前
Python零基础入门全套资料包免费分享 | 从0到1系统学习路线(含课件+源码+实战案例)
开发语言·python·学习·编程入门·python教程·编程学习·免费资料
weixin_458872612 小时前
东华复试OJ二刷复盘15
学习
却道天凉_好个秋2 小时前
音视频学习(九十六):PLC
学习·音视频·plc
TroubleMakerQi3 小时前
[虚拟机环境配置]07_Ubuntu中安装vscode教程
linux·人工智能·vscode·ubuntu
知识分享小能手3 小时前
Redis入门学习教程,从入门到精通,Redis集群架构:语法知识点、使用方法与综合案例(6)
redis·学习·架构