qemu+gdb kernel调试环境搭建

这个最后是失败了的,各种原因吧,主要是gdb版本的问题,我又不想重新编译gdb就放弃了这个方法,不过供其他想要搭建qemu+gdb的人参考吧,毕竟我也折腾老半天了。。
可能以后哪天成功了回来更新一下。

环境

1
2
3
4
5
parallels@ubuntu:~/ctf/hit/once$ uname -a
Linux ubuntu 4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

parallels@ubuntu:~/ctf/hit/once$ gcc -v
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)

下kernel source code,解压

1
2
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.10.1.tar.gz
tar -xzvf linux-4.10.1.tar.gz //解压tar.gz

安装依赖

1
sudo apt-get install build-essential libncurses5-dev

编译

  1. make menuconfig
    进入kernel hacking
    勾选以下项目
    Kernel debugging
    Compile-time checks and compiler options —> Compile the kernel with debug info和Compile the kernel with frame pointers
    KGDB
    然后保存退出

  2. make bzImage
    提示如下信息则编译成功

    1
    2
    3
    4
    Setup is 17436 bytes (padded to 17920 bytes).
    System is 7063 kB
    CRC 78823741
    Kernel: arch/x86/boot/bzImage is ready (#1)
  3. 从kernel source code根目录取到vmlinux,从arch/x86/boot/取到bzImage

编译busybox

1
2
3
4
5
wget https://busybox.net/downloads/busybox-1.27.2.tar.bz2
tar -jxvf busybox-1.27.2.tar.bz2
cd busybox-1.27.2
make menuconfig # Busybox Settings -> Build Options -> Build Busybox as a static binary
make install

建立文件系统(busybox)

1
2
cd _install
mkdir -pv {bin,sbin,etc,proc,sys,usr/{bin,sbin}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vim etc/inittab
添加以下内容
::sysinit:/etc/init.d/rcS
::askfirst:/bin/ash
::ctrlaltdel:/sbin/reboot
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
::restart:/sbin/init

mkdir etc/init.d
vim etc/init.d/rcS
添加以下内容
#!/bin/sh
mount -t proc none /proc
mount -t sys none /sys
/bin/mount -n -t sysfs none /sys
/bin/mount -t ramfs none /dev
/sbin/mdev -s

chmod +x ./etc/init.d/rcS

find . | cpio -o --format=newc > ~/core/rootfs.img
gzip -c rootfs.img > rootfs.img.gz