6.2 调试工具以及方法
1. 6.2.1 使用gdb调试qemu
一般调试用户程序调用过程,可使用gdb启动程序,并设置断点进行跟踪,需要保证qemu 配置过程中添加了"--enable-debug"参数.
- 进入gdb,设置参数
gdb --args ./qemu-system-loongarch64 --enable-kvm -m 1024 -boot c -smp 2 -machine loongson7a_v1.0 -cpu Loongson-3A5000 -kernel /boot/vmlinuz -append "console=ttyS0,115200 rdinit=/sbin/init rw" -d int -nographic
- 添加断点
b virtio_blk_pci_instance_init
运行r,命中断点,查看信息等,可参考gdb手册。
2. 6.2.2 qemu monitor查看状态
在命令行方式启动guest后,可能出现内核或者用户态程序不继续执行的情况
在没有-vga qxl参数情况下,可使用 ctrl+A+C的方式进入qemu monitor. 使用 info xxx命令可以查看很多状态信息,比如info cpus, info registers, info block,info pci,info chardev...
如果有-vga qxl参数,会启动qemu UI,鼠标点击进入UI,用ctrl+Alt+1/2/3在显示界面,qemu monitor和 serial输出间相互切换,也可以在启动qemu时附加参数 -monitor stdio/ -serial stdio将监控器或者串口输出到标准字符,二者选一个输出。
通过telnet使用qemu monitor的方法 在命令行启动的方式下,使用如 -monitor telnet:127.0.0.1:3333,server,nowait的参数,其中3333是端口号,如启动多个虚拟机,每个需配置不同的端口号。 然后再在另一个终端启动telnet 127.0.0.1 3333即可使用qemu monitor.
libvirt启动虚拟机时,通过virsh使用qemu monitor方法
virsh qemu-monitor-command domain --hmp info mtree
3. 6.2.3 使用 qemu自带trace工具
- 在需要trace的目录下添加trace-events文件,比如hw/loongarch/trace-events,参照其他目录下的trace-events文件编写
确保 Makefile.objs中trace-events-y中包含当前所要trace的文件夹
trace-events-y += hw/loongarch/trace-events
在需要trace的函数中添加trace方法
- configure添加 --enable-trace-backends=simple 参数重新编译qemu
参考docs/devel/tracing.txt
== Quickstart ==
1. Build with the 'simple' trace backend:
./configure --enable-trace-backends=simple
make
2. Create a file with the events you want to trace:
echo memory_region_ops_read >/tmp/events
3. Run the virtual machine to produce a trace file:
qemu --trace events=/tmp/events ... # your normal QEMU invocation
4. Pretty-print the binary trace file:
./scripts/simpletrace.py trace-events-all trace-* # Override * with QEMU <pid>
== Trace events ==
4. 6.2.4 调试虚拟机内核
qemu版本支持通过插入软件断点调试虚拟机内核,使用方式参考qemu方式
- qemu启动虚拟机
# qemu-system-loongarch64 --enable-kvm -m 1024 -boot c -smp 2 \
-machine loongson7a_v1.0 -cpu Loongson-3A5000 \
-kernel /boot/vmlinux -append "console=ttyS0,115200 rdinit=/sbin/init rw" -initrd initrd.img \
-d int -nographic -S -s
-nographic 表示启动的是非图形界面的 -S 表示guest虚拟机一启动就会暂停 -s 表示监听tcp:1234端口等待GDB的连接 -kernel /boot/vmlinux 调试虚拟机内核最好使用未压缩内核
- gdb连接qemu
# gdb /boot/vmlinux
(gdb) target remote localhost:1234
(gdb) b start_kernel
(gdb) c