参考书籍《奔跑吧Linux内核》–笨叔
下载Linux-5.0源码
https://benshushu.coding.net/public/runninglinuxkernel_5.0/runninglinuxkernel_5.0/git/files
或者直接git源码
git clone https://e.coding.net/benshushu/runninglinuxkernel_5.0/runninglinuxkernel_5.0.git
安装必要的包
sudo apt-get install qemu libncurses5-dev gcc-aarch64-linux-gnu build-essential bison flex libssl-dev qemu-system-arm
切换到rlk_5.0分支
git checkout rlk_5.0
编译内核
~/kernel/runninglinuxkernel_5.0$ ./run_debian_arm64.sh build_kernel
编译文件系统
~/kernel/runninglinuxkernel_5.0$ sudo ./run_debian_arm64.sh build_rootfs
使用qemu运行Debian系统
/kernel/runninglinuxkernel_5.0$ ./run_debian_arm64.sh run
running:
或者使用:
qemu-system-aarch64 -m 1024 -cpu max,sve=on,sve256=on -M virt,gic-version=3,its=on,iommu=smmuv3 -nographic -smp 4 -kernel arch/arm64/boot/Image -append "noinitrd nokaslr loglevel=8 sched_debug root=/dev/vda rootfstype=ext4 rw crashkernel=256M vfio.dyndbg=+pflmt irq_gic_v3_its.dyndbg=+pflmt iommu.dyndbg=+pflmt irqdomain.dyndbg=+pflmt" -drive if=none,file=/home/book/kernel/runninglinuxkernel_5.0/rootfs_debian_arm64.ext4,id=hd0 -device virtio-blk-device,drive=hd0 --fsdev local,id=kmod_dev,path=./kmodules,security_model=none -device virtio-9p-pci,fsdev=kmod_dev,mount_tag=kmod_mount
进入虚拟机界面
登录
用户名:root或benshushu
密码:123
成功进入qemu
benshushu:~# ifconfig
benshushu:~# gcc -v
如果没有可以在线安装
在qemu和Ubuntu之间共享文件
:~/kernel$ cp test.txt runninglinuxkernel_5.0/kmodules/
benshushu:~# ls /mnt/
README arm64_virt.dts rlk_lab test.txt
benshushu:~# vi /mnt/test.txt
退出qemu
Ctrl +a x
或者之间结束qemu
ps -e | grep qemu
kill -9 48977
使用GDB调试
安装支持不同处理器架构的gdb
sudo apt install gdb-multiarch
运行脚本启动qemu和GDB
:~/kernel/runninglinuxkernel_5.0$ ./run_debian_arm64.sh run debug
开启另外一个终端:
远程调用GDB调试
:~/kernel/runninglinuxkernel_5.0$ gdb-multiarch --tui vmlinux
设置 aarch64架构
(gdb)set architecture aarch64
通过1234端口远程连接到qemu虚拟机。
(gdb)target remote localhost:1234
在内核的start_kernel 处设置断点
(gdb)b start_kernel
运行测试
(gdb)c
将跳转到断点 start_kernelchu
退出 GDB
(gdb)q/quit
方法二:图形化界面调试工具
安装 Eclipse-CDT插件
下载Eclipse-CDT插件
https://projects.eclipse.org/projects/tools.cdt
Download Packages
https://projects.eclipse.org/projects/iot.embed-cdt
下载x86_64 Linux安装包
Download
Linux x86_64 | AArch64
https://www.eclipse.org/downloads/download.php?file=/oomph/epp/2023-09/R/eclipse-inst-jre-linux64.tar.gz
安装java运行环境。
sudo apt-get install openjdk-13-jre
安装
hudahua@VirtualBox:~/workspace/tools$ tar xzf eclipse-inst-jre-linux64.tar.gz
hudahua@VirtualBox:~/workspace/tools/eclipse-installer$ ./eclipse-inst