编译内核
- 下载kernel源码
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
cd linux-stable/
-配置内核
make menuconfig:
在kernel hacking下的选项中选中(实用空格键,星号*代表选选中)compile the kernel with debug info
注意【记得提前安装libncurses5-dev】,或者其他编译内核需要的依赖包
│ │ [*] 64-bit kernel │ │
│ │ General setup —> │ │
│ │ [*] Enable loadable module support —> │ │
│ │ -*- Enable the block layer —> │ │
│ │ Processor type and features —> │ │
│ │ Power management and ACPI options —> │ │
│ │ Bus options (PCI etc.) —> │ │
│ │ Executable file formats / Emulations —> │ │
│ │ [*] Networking support —> │ │
│ │ Device Drivers —> │ │
│ │ Firmware Drivers —> │ │
│ │ File systems —> │ │
│ │ Kernel hacking —> │ │
│ │ Security options —> │ │
│ │ -*- Cryptographic API —> │ │
│ │ [*] Virtualization —> │ │
│ │ Library routines —> │ │
按K键选中Kernel hacking之后按enter键进入子选项,按K键选移动到 Kernel debugging选项并按空格键选中为星号*(如下)
│ │
[*] Magic SysRq key │ │
│ │ (0x1) Enable magic SysRq key functions by default │ │
│ │ [*] Enable magic SysRq key over serial │ │
│ │ [*] Kernel debugging │ │
│ │ Memory Debugging —> │ │
│ │ [ ] Code coverage for fuzzing │ │
│ │ [ ] Debug shared IRQ handlers │ │
│ │ Debug Lockups and Hangs —>
进入kernel debuging 选中compile the kernel with debug info选项
保存退出
- 执行编译
make -j16
编译内核
编译完成之后生成bzImage和vmlinux二进制内核镜像就可以用来调试。
linux-stable$ ls arch/x86_64/boot/bzImage -lh
lrwxrwxrwx 1 emindsoft emindsoft 22 10月 22 14:32 arch/x86_64/boot/bzImage -> ../../x86/boot/bzImage
linux-stable$ ls -lh
-rwxrwxr-x 1 emindsoft emindsoft 443M 10月 21 11:52 vmlinux
更新gdb
详细信息可以查看这里修改方法这里写代码片
由gdb-7.8.tar.gz下载最新的gdb(7.8)到/opt目录下【目录随意选】
解压缩:tar zxvf gdb-7.8.tar.gz
修改代码【否则调试内核会出现remote ‘g’ packet reply is too long的问题】
cd gdb-7.8/gdb
vim remote.c
按照如图所示修改代码【针对7.8版本】:
代码如下:
if (buf_len > 2 * rsa->sizeof_g_packet) {
rsa->sizeof_g_packet = buf_len ;
for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
{
if (rsa->regs->pnum == -1)
continue;
if (rsa->regs->offset >= rsa->sizeof_g_packet)
rsa->regs->in_g_packet = 0;
else
rsa->regs->in_g_packet = 1;
}
}
安装gdb
在主目录gdb-7.8下依次运行命令(你也可以选择自己想要的version):
./configure
make
make install
启动qemu
- 在启动之前,我们需要initrd.img文件,至于该文件的作用大家可自行百度。这里我们直接从宿主ubuntu系统中cp过来:
cd linux-stable/
cp /boot/initrd.img-4.4.0-97-generic ./ - 开始启动
cd linux-stable/
qemu-system-x86_64 -S -kernel arch/x86_64/boot/bzImage -initrd ./initrd.img-4.4.0-97-generic -m 1024
// -m表示非配内存大小为128Mb, -S表示以调试模式启动,暂定在启动界面
// -S表示“冷冻”虚拟机,等待调试器发出继续运行命令;
// -kernel表示要调试的内核镜像
注: 这里并没有制作文件系统,大家可以更具自己的需求添加
同时摁ctrl+alt+2进入qemu命令行界面, 输入gdbserver tcp::1234
,建立并等待gdb连接(如下图)
- 摁ctrl+alt+1返回qemu
另外开一个终端到linux-stable主目录下,输入命令:gdb vmlinux
在gdb下输入:target remote localhost:1234
连接qemu中开启的gdbserver(如下图)
设置断点【举例:b do_fork】
输入命令c开始执行到断点处
输入命令n执行下一个语句