QEMU调试总结

调试步骤

Qemu monitor

为什么要使用 QEMU monitor命令?

QEMU monitor用于向QEMU模拟器提供复杂的命令。你可以用它来:

  • 删除或插入可移动媒体映像(如CD-ROM或软盘)。
  • 冻结/解冻虚拟机,并通过磁盘文件保存或恢复虚拟机状态。
  • 在没有外部调试器的情况下检查VM状态。如查看CPU寄存器信息、打印虚拟/物理地址内容,这个用的最多。

进入退出monitor

C-a c在console和monitor模式切换。其他命令如下:

console是qemu的软件仿真输出打印模式,monitor是qemu的监控调试模式

C-a h    print this help
C-a x    exit emulator
C-a s    save disk data back to file (if -snapshot)
C-a t    toggle console timestamps
C-a b    send break (magic sysrq)
C-a c    switch between console and monitor
C-a C-a  sends C-a

常用命令

进入monitor模式化,可以进行以下操作

  • 地址类
    i /fmt addr -- I/O port read
    o /fmt addr value -- I/O port write
    x /fmt addr -- virtual memory dump starting at 'addr'
    xp /fmt addr -- physical memory dump starting at 'addr'
    print|p /fmt expr -- print expression value (use $reg for CPU register access)
Dump 80 16 bit values at the start of the video memory:
(qemu) xp /80hx 0xb8000
  • 查询
    info [subcommand] -- show various information about the system state
Show the all cpu registers.
(qemu) info registers -a
  • 控制
    system_powerdown -- send system power down event
    system_reset -- reset the system
    system_wakeup -- wakeup guest from suspend

Ref

更详细信息请查看qemu文档

https://qemu-project.gitlab.io/qemu/system/monitor.html

Qemu + GDB

为什么使用GDB调试

GDB相对于Qemu monitor来说,是外部调试器Debugger,能够使用常见的设置断点、查看堆栈,基于代码的调试。看到的软件调试信息更多。

调试步骤

  1. arm不能使用系统自带的gdb,要用gdb-multiarch、或者指定的toolchain下面的gdb
    sudo apt install gdb-multiarch

  2. 确保编译选项加了-g
    kernel要打开选项CONFIG_DEBUG_INFO,CONFIG_GDB_SCRIPTS

  3. qemu命令需要添加以下选项

qemu-system-aarch64  -S -s
-S:表示qemu虚拟机会冻结CPU,直到远程的gdb输入相应控制命令
-s:表示在1234端口接受gdb的调试连接
  1. 在另一终端输入命令
gdb-multiarch --tui -q --se=vmlinux -ex 'target remote :1234'
(gdb) b start_kernel //在内核start_kernel设置断点
(gdb) c //continue 运行
  • 命令解释:
    --tui // 使用终端命令行
    -q //不打印gdb启动信息
    --se=vmlinux //指定符号表和可执行文件

gdb启动命令太长的话,在命令行输入不方便,也可以通过指定命令文件的方式,自动执行多条命令。

  • --command=FILE, -x //执行指定文件中的命令
  • --init-command=FILE, -ix //与-x类似,启动gdb调试前执行命令
  • 或者在工作目录创建初始化文件,每次启动gdb自动执行,/<path-to-workspace>/.gdbinit,例如:
file /path/vmlinux
target remote :1234

Ref

https://www.kernel.org/doc/Documentation/dev-tools/gdb-kernel-debugging.rst

posted @ 2023-07-05 15:30  zephyr~  阅读(1077)  评论(0编辑  收藏  举报