一站式内核开发调试工具链
如果编译失败,每个项目都运行make clean
,重试一遍
比如之前编译过x86,在此之上编译arm的kernel可能会出现奇怪的报错。
clean之后再编译报错就没了
总之,计算机是完全人造的,我们总是要依赖别人的代码,出错很正常,而且有些错误很难搞明白,死磕就是浪费时间,能绕过,走大家都走的路,实现你的目标才最重要。
proxychains make -j$(nproc)
会导致 getaddrinfo bug
配透明代理保险些
vscode C++ 插件难用,建议编译kernel加CC=clang
,配合vscode clangd
在X86编译其他arch的kernel时,还是都加上 ARCH=xxx,比如x86编译arm,加ARCH=arm
不加在编译时总发现奇奇怪怪的问题
sudo apt install make gcc flex bison clang libelf-dev bc libssl-dev rsync pahole qemu-system-x86 e2fsprogs debootstrap -y
sudo git submodule update --init --remote --recursive
sudo git submodule foreach "git reset --hard HEAD && git checkout -b _tmp && git branch -D master && git checkout -b master origin/master && git branch -D _tmp"
# 初始化 linux
cd linux
make CC=clang weichao_x86_64_defconfig
# 如果需要
# make menuconfig
make CC=clang -j$(nproc)
# 初始化buildroot
cd ../buildroot
make weichao_x86_64_defconfig
# 如果需要
# make menuconfig
make
在 x86 编译arm的内核,需要 ARCH=arm
linux/
make ARCH=arm sunxi_deconfig
make ARCH=arm -j$(nproc)
-
make CC=clang menuconfig
-
make CC=clang -j$(nproc)
-
保存
linux/.config
:make savedefconfig cp defconfig arch/x86/configs/weichao_x86_64_defconfig
-
修改代码,跳转 2
buildroot/
make linux-update-defconfig
- make menuconfig
- make savedefconfig
make
ormake linux-rebuild
(加上第二步一共编译两次kernel,幸好内核开发文件变动不频繁,所以linux/ 有compile_commands.json 后不需要经常编译)
bash -c 'cd linux && ./scripts/clang-tools/gen_compile_commands.py'
buildroot用 rsync
将 linux/
同步到 buildroot/output/build/linux-custom
。在 linux/ 修改后 make 并不会复用上次的编译缓存
每个包都有 package-<rebuild|reconfigure>
的形式
-
重新编译运行linux
# 只会在target/生成bzImage # 如果需要rootfs,需要全量编译 make all # 另外此命令会报错 comm files-list.before: No such file or directory # https://yhbt.net/lore/all/[email protected]/T/ # 但还是会生成bzImage,没发现什么印象,现阶段忽略就好 make linux-rebuild
根据测试,rsync同步后还会有编译缓存。在 linux/ 开发编译之后
make linux-rebuild
会重新编译linux/
修改的内容,而不是全量编译 -
编译生成rootfs
# https://buildroot.org/downloads/manual/manual.html#:~:text=8.3.-,Understanding%20how%20to%20rebuild%20packages,-One%20of%20the\ make all
-
启动qemu
qemu-system-x86_64 -m 8G -smp cpus=4,cores=4 --kernel ./linux/arch/x86_64/boot/bzImage -initrd ./buildroot/output/images/rootfs.cpio -drive file=./buildroot/output/images/rootfs.ext4,format=raw,index=0,media=disk -device e1000,netdev=eth0 -netdev user,id=eth0,hostfwd=tcp::5555-:22,net=192.168.76.0/24,dhcpstart=192.168.76.9 -append "nokaslr console=ttyS0" -S -nographic -gdb tcp::1234 -virtfs local,path=/,security_model=none,mount_tag=guestroot
gdb attach
gdb buildroot/output/build/linux-custom/vmlinux target remote localhost:1234
或者一行
gdb buildroot/output/build/linux-custom/vmlinux --ex="target remote localhost:1234"
ssh连接
ssh [email protected] -p 5555
buildroot推荐跨平台编译,不提供编译器等二进制
某些场景希望在自己的内核开发,编译代码。buildroot的能力无法满足需求。解决办法是构建debian rootfs,用自己的内核运行,可以获得熟悉的debian开发环境,apt安装依赖
./create_debian_rootfs.sh
linux/.config只用于开发,最后调试用的.config在 buildroot/output/build/linux-custom/.config
修改开发.config: linux/ 下 make menuconfig
修改编译调试的.config: buildroot下 make linux-menuconfig
注意: make linux-update-config
复制的是 linux-custom/.config,所以在 linux/.config
的修改并不会影响 board/weichao/<arch>/linux.config
Adding breakpoints while the program runs will not interrupt it immediately. For that you need to pause & resume the program once first. However adding breakpoints while its paused works as expected.
- 先点vscode debug的pause
- 设置断点
- 再点continue
.\qemu-system-x86_64.exe --kernel C:\Users\qaq13\Desktop\bzImage -initrd C:\Users/qaq13/Desktop/rootfs.cpio -device e1000,netdev=eth0 -netdev user,id=eth0,hostfwd=tcp::5555-:22,net=192.168.76.0/24,dhcpstart=192.168.76.9 -append "nokaslr console=ttyS0" -S -nographic -gdb tcp::1234
linux/ 和 buildroot/ 都需要编译一次内核,buildroot的编译会复用linux/的编译缓存进行少量编译
buildroot/ 编译依赖 linux-header,而 buildroot 默认的linux-header和 linux/ 的版本可能不一致,为避免不必要的麻烦,buildroot总是使用和linux/版本一致的linux-header编译,所以buildroot需要使用 linux/ 代码重新编译一次
运行
make weichao_x86_64_defconfig
# 这里可能会出现kbuild的提示,是否开启关闭某些选项
# 是因为上面的 make weichao_x86_64_defconfig 并没有加 CC=clang
# 而下面却加了 CC=clang
make CC=clang -j$(nproc)
解决办法是都加 CC=clang
make CC=clang weichao_x86_64_defconfig
# 这里可能会出现kbuild的提示,是否开启关闭某些选项
# 是因为上面的 make weichao_x86_64_defconfig 并没有加 CC=clang
# 而下面却加了 CC=clang
make CC=clang -j$(nproc)
文件系统写100M,但实际物理存储只有30M
其实不是mount的问题,而是你所在的shell哪怕挂载完,还是现实原来的大小,因为init用的就是原来的大小!
已经将 tmux.conf overlay 到rootfs
tmux -f /root/.config/tmux/tmux.conf
常用的 ctrl-a
被 qemu 拦截,需要 ctrl-a+ctrl-a
才能发送到内部shell
所以split window变成
ctrl-a+ctrl+a \
ctrl-a+ctrl+a -
Alt-arrow
尝试重新编译kernel
cd linux
make CC=clang -j$(nproc)