gdb 8.0.1连接qemu时,出现“Remote 'g' packet reply is too long”

本文介绍了一个关于QEMU模拟器与gdb调试器交互的问题:使用-S参数启动QEMU时,gdb会出现Remote 'g' packet reply is too long:错误。文章提供了详细的解决方案,涉及gdb源码的修改,并适用于x86_64 CPU环境。

1 原因

Note that other tutorials also add a "-S" parameter so QEMU starts the kernel stopped, however this is ommitted deliberately. The "-S" parameter would allow gdb to set an initial breakpoint anywhere in the kernel before kernel execution begins. Unfortunately, a change made to the gdbserver in QEMU, to support debugging 32- and 16-bit guest code in an x86_64 session breaks the -S functionality. The symptoms are that gdb prints out "Remote 'g' packet reply is too long:", and fails to interact successfully with QEMU. The suggested fix is to run the QEMU until it is in 64-bit code (i.e. after the boot loader has finished and the kernel started) before connecting from gdb (omitting the -S parameter). To debug a running kernel, this is sufficient; it is the method we will take.”

2 解决办法

gdb源码根目录/gdb/remote.c里面,将

if (buf_len > 2 * rsa->sizeof_g_packet)
error (_(“Remote ‘g’ packet reply is too long: %s”), rs->buf);
修改为
if (buf_len > 2 * rsa->sizeof_g_packet) {
    rsa->sizeof_g_packet = buf_len ;
    for (i = 0; i < gdbarch_num_regs (gdbarch); i++) {
        if (rsa->regs->pnum == -1)
            continue;
        if (rsa->regs->offset >= rsa->sizeof_g_packet)
            rsa->regs->in_g_packet = 0;
        else  
            rsa->regs->in_g_packet = 1;
    }     
}
也就是说,不是作为error来处理,而是按照下面的处理方式进行处理。
 
3 cpu是x86_64
 
 
 

 

转载于:https://www.cnblogs.com/hustdc/p/8146117.html

### 配置 GDB 与 VSCode 和 QEMU 的联合调试环境 为了实现使用 GDB 在 Visual Studio Code 中调试由 QEMU 运行的程序,以下是详细的配置方法: #### 工具准备 确保已安装以下工具并验证其版本: - **QEMU**: 使用 `qemu-arm` 或其他适合目标架构的模拟器。可通过命令 `qemu-arm --version` 检查是否已正确安装[^1]。 - **GDB**: 确保安装了适用于目标平台的交叉编译 GDB 版本,例如 `arm-linux-gnueabihf-gdb`。同样可执行 `arm-linux-gnueabihf-gdb --version` 来确认安装状态。 - **VSCode 插件**: - 安装扩展包 `C/C++ Extension Pack`,它提供了基础的 C/C++ 支持以及调试功能。 - 推荐额外安装插件如 `Native Debug` 或 `CodeLLDB`,以便更好地处理远程 GDB 调试会话。 #### 创建启动脚本 编写一个简单的 Shell 脚本来启动 QEMU 并使其进入监听模式等待连接来自外部调试器的指令。下面是一个基本例子: ```bash #!/bin/bash qemu-system-arm \ -M versatilepb \ -cpu cortex-a9 \ -kernel /path/to/your/binary \ -S -s & ``` 此脚本中的选项解释如下: - `-M`: 设置机器模型为 Versatile PB(可根据实际需求调整); - `-cpu`: 明确指定 CPU 类型; - `-kernel`: 提供要加载的目标二进制文件路径; - `-S`: 让虚拟机暂停直到收到继续信号; - `-s`: 开启 gdbserver,默认端口为 1234[^1]。 #### 配置 VSCode Launch.json 文件 打开 `.vscode/launch.json` 文件,在其中定义一个新的调试配置项来匹配前面设置好的参数条件。这里给出一段示范代码作为参考依据之一: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Attach to QEMU", "type": "cppdbg", "request": "attach", "program": "/path/to/your/binary", "processId": "${command:pickProcess}", "miDebuggerServerAddress": "localhost:1234", "miDebuggerPath": "/usr/bin/arm-linux-gnueabihf-gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "internalConsoleOptions": "openOnSessionStart" } ] } ``` 以上 JSON 数据片段里包含了几个重要字段说明: - `"miDebuggerServerAddress"` 应指向运行着 qemu 实例所在主机地址及其开放给客户端接入使用的具体端口号组合形式字符串表达式;此处默认采用本地回环接口加标准调试服务端口构成的整体描述方式即 'localhost:1234'[^3]。 - `"miDebuggerPath"` 则需填写对应体系结构所适配版本号下的 GNU Debugger 执行档绝对定位全名位置信息串值表示法样例展示为 '/usr/bin/arm-linux-gnueabihf-gdb'^[3]^. 完成上述操作之后保存更改再回到 IDE 主界面尝试激活关联名称标签页下新增设项目录条目即可触发整个流程运转起来从而达成预期目的效果表现出来。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值