QEMU调试总结

发布时间 2023-07-05 15:37:40作者: zephyr~

调试步骤

Qemu monitor

为什么要使用 QEMU monitor命令?

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

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

进入退出monitor

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

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

常用命令

  • 地址类
    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

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

  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 // 使用命令行,不用gui
    -q //不打印gdb启动信息
    --se=vmlinux //指定符号表
    -ex //通过1234端口连接qemu平台

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

Initial commands and command files:
--command=FILE, -x Execute GDB commands from FILE.
--init-command=FILE, -ix Like -x but execute commands before loading inferior.
--eval-command=COMMAND, -ex
Execute a single GDB command.
May be used multiple times and in conjunction
with --command.
--init-eval-command=COMMAND, -iex

Ref