1. 准备编译环境 apt-get build-dep linux-image-`uname -r`
2. 下载源码
git clone https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
或者使用当前版本的源码apt-get install linux-source
3. 准备.config文件
在当前基础上修改
cp /boot/config-`uname -r` ./.config
交互方式生成: make localmodconfig
图形方式修改: make menuconfig
4. 编译
make -j4
make modules_install -j4
5. 安装
make install -j4
6. 一些 选项的说明:
在内核配置菜单的Kernel hacking选项中选择kgdb调试项,例如:
[*] KGDB: kernel debugging with remote gdb
Method for KGDB communication (KGDB: On generic serial port (8250)) --->
[*] KGDB: Thread analysis
[*] KGDB: Console messages through gdb
编译内核之前请注意Linux目录下Makefile中的优化选项,默认的Linux内核的编译都以-O2的优化级别进行。在这个优化级别之下,编译器要对内核中的某些代码的执行顺序进行改动,所以在调试时会出现程序运行与代码顺序不一致的情况。可以把Makefile中的-O2选项改为-O,但不可去掉-O,否则编译会出问题。为了使编译后的内核带有调试信息,注意在编译内核的时候需要加上-g选项。
不过,当选择"Kernel debugging->Compile the kernel with debug info"选项后配置系统将自动打开调试选项。另外,选择"kernel debugging with remote gdb"后,配置系统将自动打开"Compile the kernel with debug info"选项。