QEMU and GDB in long mode
文章目录
如果你运行在 Long Mode 并使用 QEMU 作为你的模拟器,你很可能会在尝试使用 QEMU 的 gdb-stub 时遇到以下错误:
Remote 'g' packet reply is too long: ...
当 QEMU 切换到 long mode 时会发生此错误。有两种变通方法:你可以断开并重新连接以强制 GDB 使用 x86_64 架构,或者给 GDB 打补丁以在运行时切换架构。
变通方法 1:重新连接
当你启动 GDB 时,使用以下命令连接到 QEMU:
$ gdb (gdb) set arch i386:x86-64:intel (gdb) target remote localhost:1234 (gdb) symbol-file YOUR_KERNEL_FILE (gdb) break SOME_FUNCTION_IN_LONG_MODE (gdb) continue
当断点命中并出现 "packet reply is too long" 时,执行以下操作:
(gdb) disconnect (gdb) set arch i386:x86-64 (gdb) target remote localhost:1234
注意,你必须在这两个 "set arch" 命令中使用不同的架构名称。
变通方法 2:给 GDB 打补丁
这是 this thread 中发布的补丁的略微修改版本。额外的好处是,当该补丁怀疑发生了模式切换时会产生警告。
[NOTE]: 该补丁有一个更新版本(适用于 GDB 12.1),位于 https://github.com/mduft/tachyon3/blob/master/tools/patches/gdb-12.1-archswitch.patch - 已确认可与 QEMU 7.2.0 一起工作。
--- gdb/remote.c 2016-04-14 11:13:49.962628700 +0300 +++ gdb/remote.c 2016-04-14 11:15:38.257783400 +0300 @@ -7181,8 +7181,28 @@ buf_len = strlen (rs->buf); /* Further sanity checks, with knowledge of the architecture. */ +// HACKFIX for changing architectures for qemu. It's ugly. Don't use, unless you have to. + // Just a tiny modification of the patch of Matias Vara (http://forum.osdev.org/viewtopic.php?f=13&p=177644) if (buf_len > 2 * rsa->sizeof_g_packet) - error (_("Remote 'g' packet reply is too long: %s"), rs->buf); + { + warning (_("Assuming long-mode change. [Remote 'g' packet reply is too long: %s]"), rs->buf); + rsa->sizeof_g_packet = buf_len ; + + for (i = 0; i < gdbarch_num_regs (gdbarch); i++) + { + if (rsa->regs[i].pnum == -1) + continue; + + if (rsa->regs[i].offset >= rsa->sizeof_g_packet) + rsa->regs[i].in_g_packet = 0; + else + rsa->regs[i].in_g_packet = 1; + } + + // HACKFIX: Make sure at least the lower half of EIP is set correctly, so the proper + // breakpoint is recognized (and triggered). + rsa->regs[8].offset = 16*8; + } /* Save the size of the packet sent to us by the target. It is used as a heuristic when determining the max size of packets that the
打过补丁的 GDB 会将该错误作为警告输出,但会继续在模式切换期间正常工作。此补丁已在 gdb 7.11 和 7.12 上测试过。
或者,你可以给 QEMU 打补丁,这样就可以使用未打补丁的 gdb。请注意,这可能会破坏对调试 32 位代码的支持(另外,编译 QEMU 是一项耗时数小时的任务)。为此,请将文件 gdbstub.c 中出现的每个 if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) { 替换为 if (TARGET_LONG_BITS == 64) {。不过,这种方法源自 a thread back in 2011,此后未经测试。
变通方法 3:在启用 long mode 之后从 GDB 连接
如果你不介意在模拟开始时损失几秒钟的计算时间,这是一个简单的解决方案。问题在于 gdb 太笨,无法理解处理器执行模式已更改,因此变通方法就是在启用 long mode 之后再连接,这样 gdb 就永远看不到这次切换。你可以创建这个 debug-with-qemu.sh 脚本(或将其放入 make 目标中):
#!/bin/env bash
# use setsid so that ctrl+c in gdb doesn't kill qemu
setsid qemu-system-x86_64 -s -boot d -cdrom kernel.iso &
sleep 5
gdb kernel.bin -x qemudbg
其中你的 qemudbg 应该包含类似这样的内容:
target remote localhost:1234
就这样(只要你不在模式之间来回切换)。
其他问题
有些人遇到过 issues with breakpoints (使用 break 命令)不触发的问题。请改用 hbreak 。
如果使用 gentoo,在 emerge sys-devel/gdb [1] 时如果没有 xml USE 标志,可能会出现此错误。
另请参阅
Threads
- Emulator for Mac OS X - 处理 macOS 上的问题
- Debugging long mode kernel (gdb + qemu)? - 不给 gdb 打补丁的变通方法
- GDB and QEMU on Win64 - 针对 gdb 或 qemu 的补丁
- GDB not breaking at my breakpoints - 处理被“跳过”的断点
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:QEMU and GDB in long mode;离线修订号:28041。对应原文版本

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论