(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=41083972

用户描述了他们在 Linux 内核(特别是 ARM64 系统)中查找错误的过程。 他们利用 QEMU 和 GDB(GNU 调试器)在 Debian 12 主机(amd64 架构)上工作。 以下是所采取步骤的概述: 1。 使用带有特定标志的“make”命令在准备好的内核构建系统中构建arm64内核映像。 构建在 systemd-nspawn 容器内进行,以最大限度地减少依赖项安装。 2。 通过运行额外的 make 命令为 GDB 创建必要的 Python 帮助程序脚本,从而在“${BUILD_DIR}”(vmlinux 和 arch/arm64/boot/Image)中生成所需的调试符号和可执行文件。 3。 在主机上安装标准 `gdb` 工具和 `qemu-system-arm`。 4。 在 QEMU 内启动内核,同时暂停其执行并允许 GDB 通过 TCP 端口 1234 连接,通过附加选项和初始 RAM 磁盘传递相关参数。 如果在引导的最初阶段没有出现问题,请设置适当的 QEMU 选项来模拟包含根文件系统的存储设备。 5。 在构建目录的基本文件夹中启动 GDB,并使用连接的终端附加到挂起的 QEMU 实例。 在“__parse_cmdline”处设置断点。 当进入 GDB shell 时,用户可以利用典型的功能,例如检查内存、检查变量、单步执行代码、检查调用堆栈等。使用 Linux 官方网站上提供的其他资源可以进一步深入了解使用 GDB 进行内核调试。 To allow GDB access to the essential Python scripts (like `vmlinux-gdb。py`), these paths must be authorized via the user's `~/。gdbinit`。 If you found this useful, consider buying me a coffee: [Donate](http://ko-fi。com/matthewthompson)! 感谢您的阅读! --- This response is auto-generated, please visit [me](https://matthewwthompson。github。io/) or [my Patreon page](https://patreon。com/matthewwthompson) for personalized responses or custom work。

相关文章

原文
I use qemu extensively especially for early-stage kernel debugging when no console is available; one such was just this week with v6.8 where, on arm64, any kernel command-line parameter >= 146 characters hangs the kernel instantly and silently.

Here's how I used qemu + gdb (on Debian 12 Bookworm amd64 host) to emulate and execute the arm64 kernel build to single-step the problematic code to identify the cause.

1. In a prepared kernel build system (i.e; all build dependencies and cross-compile tools installed) build the kernel image. I do this in an unprivileged systemd-nspawn amd64 container to avoid messy -dev package installs on the host. Nspawn bind-mounts the host's source-code tree which includes a separate build directory:

  cd "${SRC_DIR}"
  # copy/install/configure a suitable ${BUILD_DIR}/.config; review/edit with:
  make V=1 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=${BUILD_DIR} -j 4 menuconfig
  # build the kernel
  export KBUILD_BUILD_USER=linux; export KBUILD_BUILD_HOST=iam.tj; time make V=1 LOCALVERSION="" ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=${BUILD_DIR} -j 12 Image
  # build gdb helper (Python) scripts 
  export KBUILD_BUILD_USER=linux; export KBUILD_BUILD_HOST=iam.tj; time make V=1 LOCALVERSION="" ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=${BUILD_DIR} scripts_gdb
This will create the debug symbols needed by gdb in ${BUILD_DIR}/vmlinux and the executable kernel in ${BUILD_DIR}/arch/arm64/boot/Image

2. Install "gdb" (and if doing foreign architecture debugging "gdb-multiarch") on the host as well as "qemu-system-arm"

3. Execute the kernel but -S[uspend] it and have QEMU listen for a connection from gdb:

  qemu-system-aarch64 -machine virt,gic-version=3 -cpu max,pauth-impdef=on -smp 2 -m 4096 -nographic -kernel ${BUILD_DIR}/arch/arm64/boot/Image -append "debug $( for l in {144..157}; do echo -n param$l=$(pwgen $((l-9)) 1)' '; done )" -initrd rootfs/boot/initrd.img-6.8.12-arm64-debug -S -gdb tcp::1234
The -append and -initrd shown here are optional; in my case no -initrd is actually needed since the (silent) panic occurs in the first few instructions the kernel executes. If debugging loadable modules however they would be in the initrd and loaded in the usual way. If the problem being diagnosed occurs after the root file-system and userspace proper are active then one would need to add the appropriate qemu options for the emulated storage device where the root file-system lives.

4. In another terminal shell (I use "tmux" and create a new tmux window) start the debugger:

  cd ${BUILD_DIR}
  # this cd is important - gdb needs to be in the base of the BUILD directory
  gdb-multiarch ./vmlinux
5. In the gdb shell:
  target remote :1234
  break __parse_cmdline
  continue
At this point the usual gdb functionality is available to examine memory, variables, single-step, view the stack and so on.

For more details on debugging kernel using gdb and the gdb scripts lx-* see

https://www.kernel.org/doc/html/latest/dev-tools/gdb-kernel-...

Edit: Forgot to note that for gdb to be able to use the lx-* Python scripts it usually needs the path authorising:

  echo "add-auto-load-safe-path ${SRC_DIR}/scripts/gdb/vmlinux-gdb.py" > ~/.gdbinit
联系我们 contact @ memedata.com