[debug] qemu linux调试配置
1. 安装Ubuntu
2.安装部分工具
apt install vim tmux openssh-server git -y
apt install openssl bc-y
3.启动 ssh. 避免后面 qemu 调试导致界面卡死,可以远程关闭进程。
ps -e | grep ssh
sudo /etc/init.d/ssh start
4.下载编译内核
可以直接点开网站
cd /root
#wget https://cdn.kernel.org/pub/linux/kernel/v5.x/内核版本
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.4.205.tar.xz -e "https_proxy=http://127.0.0.1:xxxx"
apt install build-essential flex bison libssl-dev libelf-dev libncurses-dev -y
xz -d linux-5.4.205.tar.xz
tar -xvf linux-5.4.205.tar
cd linux-5.4.205
apt install build-essential flex bison libssl-dev libelf-dev libncurses-dev -y
# 设置调试的编译菜单。
make menuconfig
# 下面选项如果没有选上的,选上,然后 save 保存设置,退出 exit。
Kernel hacking --->
Compile-time checks and compiler options --->
[*] Compile the kernel with debug info
[*] Provide GDB scripts for kernel debugging
Processor type and features --->
[*] Randomize the address of the kernel image (KASLR)
# 编译内核。
make -j8
mkdir rootfs
编译可能出现的错误
In your kernel configuration file you will find this line:
CONFIG_SYSTEM_TRUSTED_KEYS="debian/canonical-certs.pem"
Change it to this:
CONFIG_SYSTEM_TRUSTED_KEYS=""
Depending on your source structure you might be able to do it via command line. Examples:
scripts/config --disable SYSTEM_TRUSTED_KEYS
or
scripts/config --set-str SYSTEM_TRUSTED_KEYS ""
EDIT: Another key has been added to the default Canonical kernel configuration since this answer was posted:
CONFIG_SYSTEM_REVOCATION_KEYS="debian/canonical-revoked-certs.pem"
So, it also needs to be dealt with for user kernel compiles to complete:
scripts/config --disable SYSTEM_REVOCATION_KEYS
See also git based mainline kernel compile notes.
可能出现的错误2:
BTF: .tmp_vmlinux.btf: pahole (pahole) is not available
Failed to generate BTF for vmlinux
Try to disable CONFIG_DEBUG_INFO_BTF
sudo apt-get install -y dwarves
可能需要自己手动make bzImage
5.调试内核
# 下载测试项目。
cd ..
git clone https://github.com/mengning/menu.git
# git clone https://gitee.com/xiongnudahan/menu.git
cd menu
vim Makefile
# 修改编译项:
# qemu-system-x86_64 -kernel ../linux-5.0.1/arch/x86/boot/bzImage -initrd ../rootfs.img
# 安装模拟器 qemu 和编译环境。
apt install qemu libc6-dev-i386
apt install qemu-system-x86
# 编译测试项目。
make rootfs
# 关闭界面,使用远程gdb调试
# 调试 kernel
# 这里的qemu-system-x86_64 -s和-S分别指gdb server(端口1234),-S指的是不自动运行模拟
qemu-system-x86_64 -kernel ../linux-5.4.205/arch/x86/boot/bzImage -initrd ../rootfs.img -append nokaslr -S -s
gdb
file ~/LinuxKernel/linux-5.4.205/vmlinux
break start_kernel
target remote:1234
调试网络
# 注: 这里编译时用gcc11会报错, 换回gcc7即可
git clone https://gitee.com/hu_yu_xiang/linuxnet.git
cd linuxnet/lab2
cd linuxnet/lab3
# 修改一下Makefile的路径即可
vim Makefile
make rootfs
# 这里注意区分bzImage的路径和生成的rootfs.img的路径即可
qemu-system-x86_64 -kernel ../../linux-5.4.205/arch/x86/boot/bzImage -initrd ../rootfs.img -append nokaslr -S -s