ubuntu20.04 搭建kernel调试环境第五篇--命令行调试kernel

前一篇文章介绍了eclipse图形化调试kernel,操作比较繁琐。这篇文章介绍gdb命令行方式,更为简单。

一、编译rootfs及kernel iamge

参考ubuntu20.04 搭建kernel调试环境第二篇--制作rootfs,制作rootfs。

参考ubuntu20.04 搭建kernel调试环境第三篇--kernel编译及运行,编译kernel image。

二、GDB调试内核

一个终端启动qemu命令:

root@linux:/home/gsf/run-kernel/linux-5.10.3# qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -drive file=rootfs.f2fs,if=ide,format=raw,id=myid0 --nographic -append "root=/dev/sda console=ttyS0" -hdb ext4.img -device nvme,drive=nvme1,serial=deadbeaf,num_queues=8 -drive file=disk.qcow2,if=none,id=nvme1 -smp 4 -S -s

另一个终端启动GDB:

oot@linux:/home/gsf/run-kernel/linux-5.10.3# gdb

……

For help, type "help".

Type "apropos word" to search for commands related to "word".

(gdb) file vmlinux

Reading symbols from vmlinux...

(gdb) target remote:1234

Remote debugging using :1234

0x000000000000fff0 in exception_stacks ()

(gdb) b nvme_alloc_admin_tags

Breakpoint 1 at 0xffffffff817040b6: file drivers/nvme/host/pci.c, line 2577.

(gdb) c

Continuing.

 

Thread 1 hit Breakpoint 1, nvme_alloc_admin_tags (dev=0xffff888003d10000) at drivers/nvme/host/pci.c:2577

2577 result = nvme_alloc_admin_tags(dev);

 

附一,qemu参数说明

qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -drive file=rootfs.f2fs,if=ide,format=raw,id=myid0 --nographic -append "root=/dev/sda console=ttyS0" -hdb ext4.img -device nvme,drive=nvme1,serial=deadbeaf,num_queues=8 -drive file=disk.qcow2,if=none,id=nvme1 -smp 4

-kernel   要运行的内核镜像

-drive    理解成一个虚拟的disk。

drive option之 file:代表该disk的文件,由“qemu-img create -f 格式”生成。

drive option之 if:interface,指定disk的接口,有ide, scsi, sd, mtd, floppy, pflash, virtio, none这些类型。

drive option之 format:format:代码disk的file的格式,创建文件时由qemu-img create -f指定。

drive option之 id:一个字符串标识。有多个dirve时,通过id来识别用哪个drive。

--nographic:关闭图形调试,将打印信息输出到串口。

-append :kernel的命令行参数。

append之root:rootfs存储设备。

append之console:串口名称。

-hdb:创建一个hard disk。注意,字符串hdb不是/dev/hdb(这个个IDE disk),这里的hdb启动后,是/dev/sdb(scsi disk)。hdb image可以通过mkfs格式化成需要的文件系统。

-device:创建一个总线设备。将子参数drive=id指定的disk挂在总线下。

devie值num_queues:nvme的队列深度。

-smp:多核数量。

附二,中文符号导致qemu启动失败

错误命令:

qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -drive file=rootfs.ext2,if=ide,format=raw -nographic -append “root=/dev/sda console=ttyS0”

 

出错信息:
qemu-system-x86_64: console=ttyS0”: drive with bus=0, unit=0 (index=0) exists

 

原因:

命令是预先在LibreOfferce writer编辑器中写的,虽然输入法是英文键盘,但输入的双引号依然是中文属性,这个编辑器在我的环境中竟然不能输入英文双引号。

 

正确命令(注意红字部分):

qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -drive file=rootfs.ext2,if=ide,format=raw -nographic -append "root=/dev/sda console=ttyS0"

posted @ 2022-12-12 14:29  geshifei  阅读(46)  评论(0编辑  收藏  举报  来源