Kernel下载编译与调试

kernel:download、build、debug

引文

之前经常听福哥说linux kernel是他见过的写的最优雅的代码。在项目中也看到福哥对kernel做了不少修改。脱离android的环境,我拉代码编译一下。

官网在:https://www.kernel.org/
参考:
https://kernelnewbies.org/KernelBuild
https://www.binss.me/blog/how-to-debug-linux-kernel/

这只是针对ubuntu的一份操作记录

安装依赖

sudo apt-get install libncurses5-dev gcc make git exuberant-ctags
sudo apt-get install bc libssl-dev libelf 

获取代码

# 稳定版本(官方)
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git

# 开发中的版本(官方)
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

# 开发中的版本(github)
https://github.com/torvalds/linux.git

编译

cd linux-stable
make mrproper
make x86_64_defconfig

cat <<EOF >.config-fragment
CONFIG_DEBUG_INFO=y
CONFIG_GDB_SCRIPTS=y
EOF

./scripts/kconfig/merge_config.sh .config .config-fragment
make -j1

修改以及调试

sudo apt-get install  qemu-kvm

# create initrd.img
qemu-img create initrd.img 128K
# or copy from host machine
cp /boot/initrd.img-XXX-generic ./

# exec
qemu-system-x86_64 -kernel ./arch/x86/boot/bzImage -initrd ./initrd.img -smp 2 -gdb tcp::1234  -S

# 用gdb调试
gdb vmlinux
(gdb) b start_kernel
(gdb) c
(gdb) n 

PS:如果对kernel不熟的话,建议先从《Linxu内核设计与实现》看起~

猜你喜欢

转载自blog.csdn.net/yeshennet/article/details/80424563